[llvm] r278859 - [InstCombine] use m_APInt to allow icmp (sub X, Y), C folds for splat constant vectors

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 14:53:20 PDT 2016


Author: spatel
Date: Tue Aug 16 16:53:19 2016
New Revision: 278859

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

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=278859&r1=278858&r2=278859&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Tue Aug 16 16:53:19 2016
@@ -2170,32 +2170,27 @@ Instruction *InstCombiner::foldICmpDivCo
 /// Fold icmp (sub X, Y), C.
 Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub,
                                                const APInt *C) {
-  // FIXME: This check restricts all folds under here to scalar types.
-  ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
-  if (!RHS)
+  const APInt *C2;
+  if (!match(Sub->getOperand(0), m_APInt(C2)) || !Sub->hasOneUse())
     return nullptr;
 
-  ConstantInt *SubC = dyn_cast<ConstantInt>(Sub->getOperand(0));
-  if (!SubC)
-    return nullptr;
-
-  const APInt &C2 = SubC->getValue();
-
   // C-X <u C2 -> (X|(C2-1)) == C
   //   iff C & (C2-1) == C2-1
   //       C2 is a power of 2
-  if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && Sub->hasOneUse() &&
-      C->isPowerOf2() && (C2 & (*C - 1)) == (*C - 1))
+  if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && C->isPowerOf2() &&
+      (*C2 & (*C - 1)) == (*C - 1))
     return new ICmpInst(ICmpInst::ICMP_EQ,
-                        Builder->CreateOr(Sub->getOperand(1), *C - 1), SubC);
+                        Builder->CreateOr(Sub->getOperand(1), *C - 1),
+                        Sub->getOperand(0));
 
   // C-X >u C2 -> (X|C2) != C
   //   iff C & C2 == C2
   //       C2+1 is a power of 2
-  if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && Sub->hasOneUse() &&
-      (*C + 1).isPowerOf2() && (C2 & *C) == *C)
+  if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() &&
+      (*C2 & *C) == *C)
     return new ICmpInst(ICmpInst::ICMP_NE,
-                        Builder->CreateOr(Sub->getOperand(1), *C), SubC);
+                        Builder->CreateOr(Sub->getOperand(1), *C),
+                        Sub->getOperand(0));
 
   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=278859&r1=278858&r2=278859&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Tue Aug 16 16:53:19 2016
@@ -1750,11 +1750,10 @@ define i1 @icmp_sub_3_X_ult_2(i32 %X) {
   ret i1 %cmp
 }
 
-; FIXME: Vectors should fold too.
 define <2 x i1> @icmp_sub_3_X_ult_2_vec(<2 x i32> %X) {
 ; CHECK-LABEL: @icmp_sub_3_X_ult_2_vec(
-; CHECK-NEXT:    [[ADD:%.*]] = sub <2 x i32> <i32 3, i32 3>, %X
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <2 x i32> [[ADD]], <i32 2, i32 2>
+; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> %X, <i32 1, i32 1>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[TMP1]], <i32 3, i32 3>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %add = sub <2 x i32> <i32 3, i32 3>, %X
@@ -1796,11 +1795,10 @@ define i1 @icmp_sub_3_X_uge_2(i32 %X) {
   ret i1 %cmp
 }
 
-; FIXME: Vectors should fold too.
 define <2 x i1> @icmp_sub_3_X_uge_2_vec(<2 x i32> %X) {
 ; CHECK-LABEL: @icmp_sub_3_X_uge_2_vec(
-; CHECK-NEXT:    [[ADD:%.*]] = sub <2 x i32> <i32 3, i32 3>, %X
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt <2 x i32> [[ADD]], <i32 1, i32 1>
+; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> %X, <i32 1, i32 1>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i32> [[TMP1]], <i32 3, i32 3>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %add = sub <2 x i32> <i32 3, i32 3>, %X




More information about the llvm-commits mailing list