[llvm] [InstCombine] Fold icmp eq/ne X, select(icmp pred X, P, C1, C2) to set membership (PR #226756)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 27 11:14:21 PDT 2026


================
@@ -4553,6 +4553,32 @@ Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI,
     return SelectInst::Create(SI->getOperand(0), Op1, Op2, "", nullptr, SI);
   }
 
+  // Fold icmp eq/ne X, select(icmp pred X, P, C1, C2)
+  // When the select condition compares X with a constant P and the select
+  // arms are constants C1/C2, we can fold to a set membership test.
+  // Example: X == select(X >s 0, 2, 0) -> (X == 2) | (X == 0)
+  // This is valid when C1 satisfies the condition (C1 >s 0) and C2 does not.
+  if (ICmpInst::isEquality(Pred)) {
----------------
addmisol wrote:

thanks @dtcxzyw for this comment, should I do this in a follow-up PR or in this PR itself?

https://github.com/llvm/llvm-project/pull/226756


More information about the llvm-commits mailing list