[llvm] r281672 - [InstCombine] move folds for icmp (sh C2, Y), C1 in with other icmp+sh folds; NFCI

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 15:26:31 PDT 2016


Author: spatel
Date: Thu Sep 15 17:26:31 2016
New Revision: 281672

URL: http://llvm.org/viewvc/llvm-project?rev=281672&view=rev
Log:
[InstCombine] move folds for icmp (sh C2, Y), C1 in with other icmp+sh folds; NFCI 

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=281672&r1=281671&r2=281672&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Sep 15 17:26:31 2016
@@ -1173,12 +1173,12 @@ Instruction *InstCombiner::foldICmpAddOp
   return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
 }
 
-/// Handle "(icmp eq/ne (ashr/lshr const2, A), const1)" ->
-/// (icmp eq/ne A, Log2(const2/const1)) ->
-/// (icmp eq/ne A, Log2(const2) - Log2(const1)).
-Instruction *InstCombiner::foldICmpCstShrConst(ICmpInst &I, Value *Op, Value *A,
-                                               const APInt &AP1,
-                                               const APInt &AP2) {
+/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
+/// (icmp eq/ne A, Log2(AP2/AP1)) ->
+/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
+Instruction *InstCombiner::foldICmpShrConstConst(ICmpInst &I, Value *A,
+                                                 const APInt &AP1,
+                                                 const APInt &AP2) {
   assert(I.isEquality() && "Cannot fold icmp gt/lt");
 
   auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
@@ -1190,7 +1190,8 @@ Instruction *InstCombiner::foldICmpCstSh
   // Don't bother doing any work for cases which InstSimplify handles.
   if (AP2 == 0)
     return nullptr;
-  bool IsAShr = isa<AShrOperator>(Op);
+
+  bool IsAShr = isa<AShrOperator>(I.getOperand(0));
   if (IsAShr) {
     if (AP2.isAllOnesValue())
       return nullptr;
@@ -1232,11 +1233,11 @@ Instruction *InstCombiner::foldICmpCstSh
   return replaceInstUsesWith(I, TorF);
 }
 
-/// Handle "(icmp eq/ne (shl const2, A), const1)" ->
-/// (icmp eq/ne A, TrailingZeros(const1) - TrailingZeros(const2)).
-Instruction *InstCombiner::foldICmpCstShlConst(ICmpInst &I, Value *Op, Value *A,
-                                               const APInt &AP1,
-                                               const APInt &AP2) {
+/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
+/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
+Instruction *InstCombiner::foldICmpShlConstConst(ICmpInst &I, Value *A,
+                                                 const APInt &AP1,
+                                                 const APInt &AP2) {
   assert(I.isEquality() && "Cannot fold icmp gt/lt");
 
   auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
@@ -1403,21 +1404,6 @@ Instruction *InstCombiner::foldICmpWithC
     }
   }
 
-  if (Cmp.isEquality()) {
-    const APInt *C2;
-    if (match(X, m_AShr(m_APInt(C2), m_Value(A))) ||
-        match(X, m_LShr(m_APInt(C2), m_Value(A)))) {
-      // (icmp eq/ne (ashr/lshr const2, A), const1)
-      if (Instruction *Inst = foldICmpCstShrConst(Cmp, X, A, *C, *C2))
-        return Inst;
-    }
-    if (match(X, m_Shl(m_APInt(C2), m_Value(A)))) {
-      // (icmp eq/ne (shl const2, A), const1)
-      if (Instruction *Inst = foldICmpCstShlConst(Cmp, X, A, *C, *C2))
-        return Inst;
-    }
-  }
-
   // FIXME: Use m_APInt to allow folds for splat constants.
   ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1));
   if (!CI)
@@ -1908,6 +1894,10 @@ static Instruction *foldICmpShlOne(ICmpI
 Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp,
                                                BinaryOperator *Shl,
                                                const APInt *C) {
+  const APInt *ShiftVal;
+  if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
+    return foldICmpShlConstConst(Cmp, Shl->getOperand(1), *C, *ShiftVal);
+
   const APInt *ShiftAmt;
   if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
     return foldICmpShlOne(Cmp, Shl, C);
@@ -1990,6 +1980,10 @@ Instruction *InstCombiner::foldICmpShrCo
   if (Cmp.isEquality() && Shr->isExact() && Shr->hasOneUse() && *C == 0)
     return new ICmpInst(Pred, X, Cmp.getOperand(1));
 
+  const APInt *ShiftVal;
+  if (Cmp.isEquality() && match(Shr->getOperand(0), m_APInt(ShiftVal)))
+    return foldICmpShrConstConst(Cmp, Shr->getOperand(1), *C, *ShiftVal);
+
   const APInt *ShiftAmt;
   if (!match(Shr->getOperand(1), m_APInt(ShiftAmt)))
     return nullptr;

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=281672&r1=281671&r2=281672&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Thu Sep 15 17:26:31 2016
@@ -548,10 +548,6 @@ private:
                                             ConstantInt *AndCst = nullptr);
   Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
                                     Constant *RHSC);
-  Instruction *foldICmpCstShrConst(ICmpInst &I, Value *Op, Value *A,
-                                   const APInt &C1, const APInt &C2);
-  Instruction *foldICmpCstShlConst(ICmpInst &I, Value *Op, Value *A,
-                                   const APInt &C1, const APInt &C2);
   Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI,
                                   ICmpInst::Predicate Pred);
   Instruction *foldICmpWithCastAndCast(ICmpInst &ICI);
@@ -586,6 +582,10 @@ private:
                                      const APInt *C1);
   Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
                                 const APInt *C1, const APInt *C2);
+  Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
+                                     const APInt &C2);
+  Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
+                                     const APInt &C2);
 
   Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
                                                  BinaryOperator *BO,




More information about the llvm-commits mailing list