summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2016-09-15 22:26:31 +0000
committerSanjay Patel <spatel@rotateright.com>2016-09-15 22:26:31 +0000
commit8da42cc5d31384361de9abf645c2e6f95c94198c (patch)
tree8c2fcafb5cea3e450a25d6fbff31537261d86861 /llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
parent8e8296fad81963bea2c92772dd4c0cbf01273141 (diff)
downloadbcm5719-llvm-8da42cc5d31384361de9abf645c2e6f95c94198c.tar.gz
bcm5719-llvm-8da42cc5d31384361de9abf645c2e6f95c94198c.zip
[InstCombine] move folds for icmp (sh C2, Y), C1 in with other icmp+sh folds; NFCI
llvm-svn: 281672
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp48
1 files changed, 21 insertions, 27 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index c86486c264f..ca887ead32b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1173,12 +1173,12 @@ Instruction *InstCombiner::foldICmpAddOpConst(Instruction &ICI,
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::foldICmpCstShrConst(ICmpInst &I, Value *Op, Value *A,
// 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::foldICmpCstShrConst(ICmpInst &I, Value *Op, Value *A,
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::foldICmpWithConstant(ICmpInst &Cmp) {
}
}
- 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(ICmpInst &Cmp, Instruction *Shl,
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::foldICmpShrConstant(ICmpInst &Cmp,
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;
OpenPOWER on IntegriCloud