InstCombine: Turn (x != 0 & x <u C) into the canonical range check form (x-1 <u C-1)
authorBenjamin Kramer <benny.kra@googlemail.com>
Sun, 12 Oct 2014 14:02:34 +0000 (14:02 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sun, 12 Oct 2014 14:02:34 +0000 (14:02 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219585 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
test/Transforms/InstCombine/and2.ll

index 0a9bb2d1087eb6bdc3e9d88944d11fc96d1fabaa..79cf288b338034a92f5746b9c78deb038fb0696e 100644 (file)
@@ -930,6 +930,8 @@ Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
     case ICmpInst::ICMP_ULT:
       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
         return Builder->CreateICmpULT(Val, LHSCst);
+      if (LHSCst->isNullValue())    // (X !=  0 & X u< 14) -> X-1 u< 13
+        return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
       break;                        // (X != 13 & X u< 15) -> no change
     case ICmpInst::ICMP_SLT:
       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
index c76bb4499e818469add220076eb587f608855aca..96b535dda99d2ba9eb48c5f6544743105d6853d1 100644 (file)
@@ -66,3 +66,14 @@ define i1 @test7(i32 %i, i1 %b) {
   %and2 = and i1 %and1, %cmp2
   ret i1 %and2
 }
+
+define i1 @test8(i32 %i) {
+; CHECK-LABEL: @test8(
+; CHECK-NEXT: [[DEC:%.*]] = add i32 %i, -1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[DEC]], 13
+; CHECK-NEXT: ret i1 [[CMP]]
+  %cmp1 = icmp ne i32 %i, 0
+  %cmp2 = icmp ult i32 %i, 14
+  %cond = and i1 %cmp1, %cmp2
+  ret i1 %cond
+}