This patch fixes PR24922.
If the mask of a select instruction is a ConstantVector, method SimplifyDemandedVectorElts iterates over its elements to identify which are selected from the first value argument and which are selected from the second value argument.
The problem with this logic is that method Constant::isNullValue() is used to check if a Constant value in the mask is zero. Unfortunately, that method always returns false if invoked on a ConstantExpr.
Before this patch, function @PR24922 in vec_demanded_elts.ll was wrongly folded to:
{code}
ret <2 x i64> %v
{code}
This patch fixes the problem in SimplifyDemandedVectorElts by adding an explicit check for ConstantExpr values. Now, if a value in the mask is a ConstantExpr, we avoid propagating wrong facts.
There are however two fundamental questions here:
- Why InstructionSimplify didn't fold that constant expression?
- Should ConstantExpr be simplified before we call SimplifyDemandedVectorElts?
It turns out that instsimplify cannot always simplify complex constant expressions. This was also observed in PR24965.
So, at least for now, we cannot promise that a ConstantVector doesn't have ConstantExpr elements in it. Therefore, relying on isNullValue() in this context is actually wrong (at least, it would propagate wrong results in the presence of ConstantExpr).
Please let me know what you think.
Thanks,
Andrea