[llvm] r305487 - [InstCombine] Handle (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) when the one of the Ands is commuted relative to the other

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 15 10:55:21 PDT 2017


Author: ctopper
Date: Thu Jun 15 12:55:20 2017
New Revision: 305487

URL: http://llvm.org/viewvc/llvm-project?rev=305487&view=rev
Log:
[InstCombine] Handle (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) when the one of the Ands is commuted relative to the other

Currently we expect A to be on the same side in both Ands but nothing guarantees that.

While there also switch to using matchers for some of the code.

Differential Revision: https://reviews.llvm.org/D34230

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/trunk/test/Transforms/InstCombine/onehot_merge.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=305487&r1=305486&r2=305487&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Thu Jun 15 12:55:20 2017
@@ -1599,31 +1599,25 @@ Value *InstCombiner::foldOrOfICmps(ICmpI
   ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1));
   ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1));
 
+  // TODO support vector splats
   if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero() &&
       RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) {
 
-    BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
-    BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
-    if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
-        LAnd->getOpcode() == Instruction::And &&
-        RAnd->getOpcode() == Instruction::And) {
+    Value *A, *B, *C, *D;
+    if (match(LHS->getOperand(0), m_And(m_Value(A), m_Value(B))) &&
+        match(RHS->getOperand(0), m_And(m_Value(C), m_Value(D)))) {
+      if (A == D || B == D)
+        std::swap(C, D);
+      if (B == C)
+        std::swap(A, B);
 
-      Value *Mask = nullptr;
-      Value *Masked = nullptr;
-      if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
-          isKnownToBeAPowerOfTwo(LAnd->getOperand(1), false, 0, CxtI) &&
-          isKnownToBeAPowerOfTwo(RAnd->getOperand(1), false, 0, CxtI)) {
-        Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
-        Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
-      } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
-                 isKnownToBeAPowerOfTwo(LAnd->getOperand(0), false, 0, CxtI) &&
-                 isKnownToBeAPowerOfTwo(RAnd->getOperand(0), false, 0, CxtI)) {
-        Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
-        Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
-      }
-
-      if (Masked)
+      if (A == C &&
+          isKnownToBeAPowerOfTwo(B, false, 0, CxtI) &&
+          isKnownToBeAPowerOfTwo(D, false, 0, CxtI)) {
+        Value *Mask = Builder->CreateOr(B, D);
+        Value *Masked = Builder->CreateAnd(A, Mask);
         return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
+      }
     }
   }
 

Modified: llvm/trunk/test/Transforms/InstCombine/onehot_merge.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/onehot_merge.ll?rev=305487&r1=305486&r2=305487&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/onehot_merge.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/onehot_merge.ll Thu Jun 15 12:55:20 2017
@@ -34,21 +34,16 @@ bb:
 }
 
 ; Same as above but with operands commuted one of the ands, but not the other.
-; TODO handle this form correctly
 define i1 @foo1_and_commuted(i32 %k, i32 %c1, i32 %c2) {
 ; CHECK-LABEL: @foo1_and_commuted(
-; CHECK-NEXT:  bb:
 ; CHECK-NEXT:    [[K2:%.*]] = mul i32 [[K:%.*]], [[K]]
 ; CHECK-NEXT:    [[TMP:%.*]] = shl i32 1, [[C1:%.*]]
 ; CHECK-NEXT:    [[TMP4:%.*]] = lshr i32 -2147483648, [[C2:%.*]]
-; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[K2]], [[TMP]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0
-; CHECK-NEXT:    [[TMP5:%.*]] = and i32 [[TMP4]], [[K2]]
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i32 [[TMP5]], 0
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[TMP2]], [[TMP6]]
-; CHECK-NEXT:    ret i1 [[OR]]
+; CHECK-NEXT:    [[TMP0:%.*]] = or i32 [[TMP]], [[TMP4]]
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[K2]], [[TMP0]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[TMP1]], [[TMP0]]
+; CHECK-NEXT:    ret i1 [[TMP2]]
 ;
-bb:
   %k2 = mul i32 %k, %k ; to trick the complexity sorting
   %tmp = shl i32 1, %c1
   %tmp4 = lshr i32 -2147483648, %c2




More information about the llvm-commits mailing list