[llvm] r281624 - [InstCombine] allow (icmp sgt smin(PosA, B), 0) fold for vectors

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 09:23:21 PDT 2016


Author: spatel
Date: Thu Sep 15 11:23:20 2016
New Revision: 281624

URL: http://llvm.org/viewvc/llvm-project?rev=281624&view=rev
Log:
[InstCombine] allow (icmp sgt smin(PosA, B), 0) fold for vectors

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/min-positive.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=281624&r1=281623&r2=281624&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Sep 15 11:23:20 2016
@@ -1376,11 +1376,10 @@ static Instruction *ProcessUGT_ADDCST_AD
 // Fold icmp Pred X, C.
 Instruction *InstCombiner::foldICmpWithConstant(ICmpInst &Cmp) {
   CmpInst::Predicate Pred = Cmp.getPredicate();
-  Value *X = Cmp.getOperand(0), *C = Cmp.getOperand(1);
+  Value *X = Cmp.getOperand(0);
 
-  // FIXME: Use m_APInt to allow folds for splat constants.
-  ConstantInt *CI = dyn_cast<ConstantInt>(C);
-  if (!CI)
+  const APInt *C;
+  if (!match(Cmp.getOperand(1), m_APInt(C)))
     return nullptr;
 
   Value *A = nullptr, *B = nullptr;
@@ -1400,21 +1399,26 @@ Instruction *InstCombiner::foldICmpWithC
     ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
     if (Pred == ICmpInst::ICMP_UGT &&
         match(X, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
-      if (Instruction *Res = ProcessUGT_ADDCST_ADD(Cmp, A, B, CI2, CI, *this))
+      if (Instruction *Res = ProcessUGT_ADDCST_ADD(
+              Cmp, A, B, CI2, cast<ConstantInt>(Cmp.getOperand(1)), *this))
         return Res;
   }
 
   // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
-  if (CI->isZero() && Pred == ICmpInst::ICMP_SGT)
-    if (auto *SI = dyn_cast<SelectInst>(X)) {
-      SelectPatternResult SPR = matchSelectPattern(SI, A, B);
-      if (SPR.Flavor == SPF_SMIN) {
-        if (isKnownPositive(A, DL))
-          return new ICmpInst(Pred, B, CI);
-        if (isKnownPositive(B, DL))
-          return new ICmpInst(Pred, A, CI);
-      }
+  if (*C == 0 && Pred == ICmpInst::ICMP_SGT) {
+    SelectPatternResult SPR = matchSelectPattern(X, A, B);
+    if (SPR.Flavor == SPF_SMIN) {
+      if (isKnownPositive(A, DL))
+        return new ICmpInst(Pred, B, Cmp.getOperand(1));
+      if (isKnownPositive(B, DL))
+        return new ICmpInst(Pred, A, Cmp.getOperand(1));
     }
+  }
+
+  // FIXME: Use m_APInt to allow folds for splat constants.
+  ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1));
+  if (!CI)
+    return nullptr;
 
   // The following transforms are only worth it if the only user of the subtract
   // is the icmp.

Modified: llvm/trunk/test/Transforms/InstCombine/min-positive.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/min-positive.ll?rev=281624&r1=281623&r2=281624&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/min-positive.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/min-positive.ll Thu Sep 15 11:23:20 2016
@@ -19,11 +19,7 @@ define i1 @smin(i32 %other) {
 
 define <2 x i1> @smin_vec(<2 x i32> %x, <2 x i32> %other) {
 ; CHECK-LABEL: @smin_vec(
-; CHECK-NEXT:    [[NOTNEG:%.*]] = and <2 x i32> %x, <i32 7, i32 7>
-; CHECK-NEXT:    [[POSITIVE:%.*]] = or <2 x i32> [[NOTNEG]], <i32 1, i32 1>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i32> [[POSITIVE]], %other
-; CHECK-NEXT:    [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[POSITIVE]], <2 x i32> %other
-; CHECK-NEXT:    [[TEST:%.*]] = icmp sgt <2 x i32> [[SEL]], zeroinitializer
+; CHECK-NEXT:    [[TEST:%.*]] = icmp sgt <2 x i32> %other, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[TEST]]
 ;
   %notneg = and <2 x i32> %x, <i32 7, i32 7>
@@ -48,16 +44,12 @@ define i1 @smin_commute(i32 %other) {
 
 define <2 x i1> @smin_commute_vec(<2 x i32> %x, <2 x i32> %other) {
 ; CHECK-LABEL: @smin_commute_vec(
-; CHECK-NEXT:    [[NOTNEG:%.*]] = and <2 x i32> %x, <i32 7, i32 7>
-; CHECK-NEXT:    [[POSITIVE:%.*]] = or <2 x i32> [[NOTNEG]], <i32 1, i32 1>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i32> [[POSITIVE]], %other
-; CHECK-NEXT:    [[SEL:%.*]] = select <2 x i1> [[CMP]], <2 x i32> %other, <2 x i32> [[POSITIVE]]
-; CHECK-NEXT:    [[TEST:%.*]] = icmp sgt <2 x i32> [[SEL]], zeroinitializer
+; CHECK-NEXT:    [[TEST:%.*]] = icmp sgt <2 x i32> %other, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[TEST]]
 ;
   %notneg = and <2 x i32> %x, <i32 7, i32 7>
   %positive = or <2 x i32> %notneg, <i32 1, i32 1>
-  %cmp = icmp slt <2 x i32> %positive, %other
+  %cmp = icmp slt <2 x i32> %other, %positive
   %sel = select <2 x i1> %cmp, <2 x i32> %other, <2 x i32> %positive
   %test = icmp sgt <2 x i32> %sel, zeroinitializer
   ret <2 x i1> %test




More information about the llvm-commits mailing list