From: Sanjay Patel Date: Thu, 15 Sep 2016 22:26:31 +0000 (+0000) Subject: [InstCombine] move folds for icmp (sh C2, Y), C1 in with other icmp+sh folds; NFCI X-Git-Tag: llvmorg-4.0.0-rc1~9619 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8da42cc5d31384361de9abf645c2e6f95c94198c;p=platform%2Fupstream%2Fllvm.git [InstCombine] move folds for icmp (sh C2, Y), C1 in with other icmp+sh folds; NFCI llvm-svn: 281672 --- diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index c86486c..ca887ea 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(Op); + + bool IsAShr = isa(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(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; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index d214939..78b79db 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -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,