[llvm] [InstCombine] Add combines for `(icmp eq/ne (and X, P2), (and X, -P2))` (PR #94867)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 12:08:12 PDT 2024


================
@@ -5570,10 +5570,35 @@ 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) {
+      // (X&P2) == (X&-P2)
+      //    -> X u< P2*2
+      // (X&P2) != (X&-P2)
+      //    -> X u>= P2*2
+      // iff P2 is not INT_MIN
----------------
nikic wrote:

I don't understand why you need the specific high mask + low mask pattern. Why is my suggestion to check that `X^Y` is a high mask (or as @dtcxzyw pointed out, a negative power of two) and then fold to `<= ~(X^Y)` not sufficient?

This covers the case where X is a high mask and Y is a low mask of the high mask as a special case, but also covers other cases. E.g. `X = 0b1010, Y = 0b0100, X^Y = 0b1110` is also valid for this transform, does not have X or Y being a high mask themselves.

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


More information about the llvm-commits mailing list