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

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 27 01:40:52 PDT 2026


================
@@ -8105,6 +8105,69 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
     }
   }
 
+  // Fold icmp eq/ne X, select(icmp pred X, P, C1, C2)
+  // When the select condition is based on X and C1/C2 are constants,
+  // we can fold this to a simple set membership test.
+  // Example: X == select(X >s 0, 2, 0) -> (X == 2) | (X == 0)
+  // This is valid when C1 satisfies the select condition (C1 >s 0) and
+  // C2 does not satisfy it (C2 <=s 0).
+  if (I.isEquality()) {
+    // Try both operand orderings for the commutative icmp eq/ne
+    for (int Swap = 0; Swap < 2; ++Swap) {
+      Value *X = Swap ? Op1 : Op0;
+      Value *Sel = Swap ? Op0 : Op1;
+
+      Value *SelCond, *TrueVal, *FalseVal;
----------------
andjo403 wrote:

looks like this can be simplified to 
```suggestion
  if (!match(SI,
             m_OneUse(m_Select(m_c_ICmp(CondPred, m_Specific(X), m_APInt(P)),
                               m_APInt(C1), m_APInt(C2)))))
    continue;
```

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


More information about the llvm-commits mailing list