From d3c7bb28be79af7367ca0fa9adb9fe458c8da0fa Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 26 Aug 2016 16:42:33 +0000 Subject: [PATCH] [InstCombine] add helper function for folding of icmp (and X, C2), C; NFC llvm-svn: 279834 --- .../Transforms/InstCombine/InstCombineCompares.cpp | 25 ++++++++++++++++------ .../Transforms/InstCombine/InstCombineInternal.h | 2 ++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 5566528..557869f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1389,10 +1389,10 @@ Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp, 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(Cmp.getOperand(1)); if (!RHS) @@ -1410,8 +1410,7 @@ Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp, // 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::foldICmpAndConstant(ICmpInst &Cmp, 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(Cmp.getOperand(1)); + if (!RHS) + return nullptr; // Try to optimize things like "A[i]&42 == 0" to index computations. if (LoadInst *LI = dyn_cast(And->getOperand(0))) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index 32e0bdd..8619ac6 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -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); -- 2.7.4