[llvm] [InstSimplify] Discard unnecessary `select` when the conditions are `ICmps` with EQ (PR #179183)
Gábor Spaits via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 10 06:43:13 PST 2026
================
@@ -5142,6 +5167,36 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
return ConstantVector::get(NewC);
}
+ CmpPredicate Pred1, Pred2;
+ Value *V1, *V2, *EQV;
+ if (match(Cond,
+ m_And(m_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(V1), m_Value(EQV)),
+ m_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(V2),
+ m_Deferred(EQV)))))
+ if (Value *V = simplifySelectWithEquivalence(
+ {{V2, EQV}, {V1, EQV}}, TrueVal, FalseVal, Q, MaxRecurse)) {
+ // We should only throw away a select if we get a select in place of that
+ // because of the poison barrier property.
+ SelectInst *VasSI = dyn_cast<SelectInst>(V);
+ // Also we have to check if the removing of the current select doesn't
+ // introduce any new poison. We should only remove the current select if
+ // its condition is poison in every case where the new select condition is
+ // poison.
+ if (VasSI && impliesPoison(VasSI->getCondition(), Cond))
----------------
spaits wrote:
I use it for test `selectSelectAndICmpFirstSelectParam`. That is a msicompile without this.
I don't think think the issue is with `simplifyWithOpsReplaced`. It does only see either the true value of the false value of the `select`. And simplifies them. Not the select using those.
When we know how the true or false value can be simplified we have to make the decision if we want simplification. In our case that simplification is basically the omission of the second `select`.
If the second select acted as a barrier for poison, then it must be kept, but if it did not do that (so every time the first select was poison the second was also poison, for example because they use all the same values to calculate the conditions) then the second select may be discarded.
So we have to check poison "dependency" between the two conditions.
Thinking about it a bit more I may move this to `simplifySelectWithEquivalence`
https://github.com/llvm/llvm-project/pull/179183
More information about the llvm-commits
mailing list