[llvm] [InstCombine] Extend (icmp eq/ne (and Z, X), (and Z, Y)) folds (PR #94867)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 10 04:54:51 PDT 2024


================
@@ -5570,10 +5570,25 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
       Z = B;
     }
 
-    if (X) { // Build (X^Y) & Z
-      Op1 = Builder.CreateXor(X, Y);
-      Op1 = Builder.CreateAnd(Op1, Z);
-      return new ICmpInst(Pred, Op1, Constant::getNullValue(Op1->getType()));
+    if (X) {
+      // If X^Y is a negative power of two, then `icmp eq/ne (Z & NegP2), 0`
+      // will fold to `icmp ult/uge Z, -NegP2` incuring no additional uses.
+      const APInt *C0, *C1;
+      bool XorIsNegP2 = match(X, m_APInt(C0)) && match(Y, m_APInt(C1)) &&
+                        (*C0 ^ *C1).isNegatedPowerOf2();
+
+      // If either Op0/Op1 are both one use or X^Y will constant fold and one of
+      // Op0/Op1 are one use proceeed. In those cases we are instruction neutral
----------------
nikic wrote:

```suggestion
      // Op0/Op1 are one use, proceed. In those cases we are instruction neutral
```

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


More information about the llvm-commits mailing list