[llvm] r313819 - [InstCombine] Handle (X & C2) < C1 --> (X & C2) == 0

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 14:18:17 PDT 2017


Author: ctopper
Date: Wed Sep 20 14:18:17 2017
New Revision: 313819

URL: http://llvm.org/viewvc/llvm-project?rev=313819&view=rev
Log:
[InstCombine] Handle (X & C2) < C1 --> (X & C2) == 0

We already did (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a result greater than C1. But there is an equivalent inverse condition with <= C1 (which will be canonicalized to < C1+1)

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

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=313819&r1=313818&r2=313819&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Sep 20 14:18:17 2017
@@ -1715,12 +1715,19 @@ Instruction *InstCombiner::foldICmpAndCo
   }
 
   // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a
-  // result greater than C1.
-  unsigned NumTZ = C2->countTrailingZeros();
-  if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && NumTZ < C2->getBitWidth() &&
-      NumTZ >= C1->getActiveBits()) {
-    Constant *Zero = Constant::getNullValue(And->getType());
-    return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
+ // result greater than C1. Also handle (X & C2) < C1 --> (X & C2) == 0.
+  if (!C2->isNullValue()) {
+    unsigned NumTZ = C2->countTrailingZeros();
+    if (Cmp.getPredicate() == ICmpInst::ICMP_UGT &&
+        NumTZ >= C1->getActiveBits()) {
+      Constant *Zero = Constant::getNullValue(And->getType());
+      return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
+    }
+    if (Cmp.getPredicate() == ICmpInst::ICMP_ULT &&
+        NumTZ >= C1->ceilLogBase2()) {
+      Constant *Zero = Constant::getNullValue(And->getType());
+      return new ICmpInst(ICmpInst::ICMP_EQ, And, Zero);
+    }
   }
 
   return nullptr;

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=313819&r1=313818&r2=313819&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Wed Sep 20 14:18:17 2017
@@ -1141,7 +1141,7 @@ define i1 @test67(i32 %x) {
 define i1 @test67inverse(i32 %x) {
 ; CHECK-LABEL: @test67inverse(
 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 96
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[AND]], 32
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[AND]], 0
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %and = and i32 %x, 127
@@ -1178,7 +1178,7 @@ define <2 x i1> @test67vec2(<2 x i32> %x
 define <2 x i1> @test67vecinverse(<2 x i32> %x) {
 ; CHECK-LABEL: @test67vecinverse(
 ; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[X:%.*]], <i32 96, i32 96>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <2 x i32> [[AND]], <i32 32, i32 32>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[AND]], zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %and = and <2 x i32> %x, <i32 96, i32 96>




More information about the llvm-commits mailing list