[llvm] r281107 - [InstCombine] use m_APInt to allow icmp ult X, C folds for splat constant vectors

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 9 14:59:41 PDT 2016


Author: spatel
Date: Fri Sep  9 16:59:37 2016
New Revision: 281107

URL: http://llvm.org/viewvc/llvm-project?rev=281107&view=rev
Log:
[InstCombine] use m_APInt to allow icmp ult X, C folds for splat constant vectors

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/exact.ll
    llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll
    llvm/trunk/test/Transforms/InstCombine/icmp.ll
    llvm/trunk/test/Transforms/InstCombine/pr17827.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=281107&r1=281106&r2=281107&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Fri Sep  9 16:59:37 2016
@@ -3540,24 +3540,29 @@ Instruction *InstCombiner::visitICmpInst
       }
       break;
     }
-    case ICmpInst::ICMP_ULT:
+    case ICmpInst::ICMP_ULT: {
       if (Op0Max.ult(Op1Min))          // A <u B -> true if max(A) < min(B)
         return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
       if (Op0Min.uge(Op1Max))          // A <u B -> false if min(A) >= max(B)
         return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
       if (Op1Min == Op0Max)            // A <u B -> A != B if max(A) == min(B)
         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
-      if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
-        if (Op1Max == Op0Min+1)        // A <u C -> A == C-1 if min(A)+1 == C
-          return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
-                              Builder->getInt(CI->getValue()-1));
 
+      const APInt *CmpC;
+      if (match(Op1, m_APInt(CmpC))) {
+        // A <u C -> A == C-1 if min(A)+1 == C
+        if (Op1Max == Op0Min + 1) {
+          Constant *CMinus1 = ConstantInt::get(Op0->getType(), *CmpC - 1);
+          return new ICmpInst(ICmpInst::ICMP_EQ, Op0, CMinus1);
+        }
         // (x <u 2147483648) -> (x >s -1)  -> true if sign bit clear
-        if (CI->isMinValue(true))
-          return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
-                           Constant::getAllOnesValue(Op0->getType()));
+        if (CmpC->isMinSignedValue()) {
+          Constant *AllOnes = Constant::getAllOnesValue(Op0->getType());
+          return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
+        }
       }
       break;
+    }
     case ICmpInst::ICMP_UGT: {
       if (Op0Min.ugt(Op1Max))          // A >u B -> true if min(A) > max(B)
         return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));

Modified: llvm/trunk/test/Transforms/InstCombine/exact.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/exact.ll?rev=281107&r1=281106&r2=281107&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/exact.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/exact.ll Fri Sep  9 16:59:37 2016
@@ -175,10 +175,9 @@ define i1 @udiv_icmp2(i64 %X) {
   ret i1 %B
 }
 
-; FIXME: missing vector fold for ult 1 -> eq 0
 define <2 x i1> @udiv_icmp2_vec(<2 x i64> %X) {
 ; CHECK-LABEL: @udiv_icmp2_vec(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i64> %X, <i64 1, i64 1>
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq <2 x i64> %X, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
 ;
   %A = udiv exact <2 x i64> %X, <i64 5, i64 5>
@@ -196,10 +195,9 @@ define i1 @sdiv_icmp1(i64 %X) {
   ret i1 %B
 }
 
-; FIXME: missing vector fold for ult 1 -> eq 0
 define <2 x i1> @sdiv_icmp1_vec(<2 x i64> %X) {
 ; CHECK-LABEL: @sdiv_icmp1_vec(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i64> %X, <i64 1, i64 1>
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq <2 x i64> %X, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
 ;
   %A = sdiv exact <2 x i64> %X, <i64 5, i64 5>
@@ -257,10 +255,9 @@ define i1 @sdiv_icmp4(i64 %X) {
   ret i1 %B
 }
 
-; FIXME: missing vector fold for ult 1 -> eq 0
 define <2 x i1> @sdiv_icmp4_vec(<2 x i64> %X) {
 ; CHECK-LABEL: @sdiv_icmp4_vec(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i64> %X, <i64 1, i64 1>
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq <2 x i64> %X, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
 ;
   %A = sdiv exact <2 x i64> %X, <i64 -5, i64 -5>

Modified: llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll?rev=281107&r1=281106&r2=281107&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll Fri Sep  9 16:59:37 2016
@@ -42,6 +42,15 @@ define <2 x i1> @ule(<2 x i8> %x) {
   ret <2 x i1> %cmp
 }
 
+define <2 x i1> @ult_min_signed_value(<2 x i8> %x) {
+; CHECK-LABEL: @ult_min_signed_value(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt <2 x i8> %x, <i8 -1, i8 -1>
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %cmp = icmp ult <2 x i8> %x, <i8 128, i8 128>
+  ret <2 x i1> %cmp
+}
+
 ; Zeros are special: they're ConstantAggregateZero.
 
 define <2 x i1> @sge_zero(<2 x i8> %x) {
@@ -72,7 +81,7 @@ define <2 x i1> @sle_zero(<2 x i8> %x) {
 
 define <2 x i1> @ule_zero(<2 x i8> %x) {
 ; CHECK-LABEL: @ule_zero(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <2 x i8> %x, <i8 1, i8 1>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i8> %x, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %cmp = icmp ule <2 x i8> %x, <i8 0, i8 0>

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=281107&r1=281106&r2=281107&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Fri Sep  9 16:59:37 2016
@@ -1377,7 +1377,6 @@ define i1 @icmp_mul_neq0(i32 %x) {
   ret i1 %cmp
 }
 
-; FIXME: Vectors should fold the same way.
 define <2 x i1> @icmp_mul_neq0_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @icmp_mul_neq0_vec(
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i32> %x, zeroinitializer

Modified: llvm/trunk/test/Transforms/InstCombine/pr17827.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/pr17827.ll?rev=281107&r1=281106&r2=281107&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/pr17827.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/pr17827.ll Fri Sep  9 16:59:37 2016
@@ -80,11 +80,10 @@ define i1 @test_shift_and_cmp_changed2(i
   ret i1 %cmp
 }
 
-; FIXME: icmp ult X, 1 -> icmp eq X, 0 
 define <2 x i1> @test_shift_and_cmp_changed2_vec(<2 x i8> %p) {
 ; CHECK-LABEL: @test_shift_and_cmp_changed2_vec(
 ; CHECK-NEXT:    [[ANDP:%.*]] = and <2 x i8> %p, <i8 6, i8 6>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <2 x i8> [[ANDP]], <i8 1, i8 1>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i8> [[ANDP]], zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %shlp = shl <2 x i8> %p, <i8 5, i8 5>




More information about the llvm-commits mailing list