[llvm] r279834 - [InstCombine] add helper function for folding of icmp (and X, C2), C; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 26 09:42:34 PDT 2016


Author: spatel
Date: Fri Aug 26 11:42:33 2016
New Revision: 279834

URL: http://llvm.org/viewvc/llvm-project?rev=279834&view=rev
Log:
[InstCombine] add helper function for folding of icmp (and X, C2), C; NFC

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=279834&r1=279833&r2=279834&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Fri Aug 26 11:42:33 2016
@@ -1389,10 +1389,10 @@ Instruction *InstCombiner::foldICmpXorCo
   return nullptr;
 }
 
-/// Fold icmp (and X, Y), C.
-Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp,
-                                               BinaryOperator *And,
-                                               const APInt *C) {
+/// Fold icmp (and X, C2), C.
+Instruction *InstCombiner::foldICmpAndConstConst(ICmpInst &Cmp,
+                                                 BinaryOperator *And,
+                                                 const APInt *C) {
   // FIXME: This check restricts all folds under here to scalar types.
   ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
   if (!RHS)
@@ -1410,8 +1410,7 @@ Instruction *InstCombiner::foldICmpAndCo
       // have its sign bit set or if it is an equality comparison.
       // Extending a relational comparison when we're checking the sign
       // bit would not work.
-      if (Cmp.isEquality() ||
-          (!AndCst->isNegative() && C->isNonNegative())) {
+      if (Cmp.isEquality() || (!AndCst->isNegative() && C->isNonNegative())) {
         Value *NewAnd =
             Builder->CreateAnd(Cast->getOperand(0),
                                ConstantExpr::getZExt(AndCst, Cast->getSrcTy()));
@@ -1590,6 +1589,20 @@ Instruction *InstCombiner::foldICmpAndCo
                             Constant::getNullValue(RHS->getType()));
     }
   }
+  return nullptr;
+}
+
+/// Fold icmp (and X, Y), C.
+Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp,
+                                               BinaryOperator *And,
+                                               const APInt *C) {
+  if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
+    return I;
+
+  // FIXME: This check restricts all folds under here to scalar types.
+  ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
+  if (!RHS)
+    return nullptr;
 
   // Try to optimize things like "A[i]&42 == 0" to index computations.
   if (LoadInst *LI = dyn_cast<LoadInst>(And->getOperand(0))) {

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=279834&r1=279833&r2=279834&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Fri Aug 26 11:42:33 2016
@@ -579,6 +579,8 @@ private:
                                    const APInt *C);
   Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
                                    const APInt *C);
+  Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
+                                     const APInt *C);
 
   Instruction *foldICmpEqualityWithConstant(ICmpInst &ICI);
   Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI);




More information about the llvm-commits mailing list