From 60ea1b43d60ff72e23bf35c50d8df29c22c5476d Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 16 Aug 2016 22:34:42 +0000 Subject: [PATCH] [InstCombine] clean up foldICmpAddConstant(); NFCI 1. Fix variable names 2. Add local variables to reduce code 3. Fix code comments 4. Add early exit to reduce indentation 5. Remove 'else' after if -> return 6. Hoist common predicate llvm-svn: 278864 --- .../Transforms/InstCombine/InstCombineCompares.cpp | 85 +++++++++++----------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index e2e47c1..5cd4e0e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2195,58 +2195,55 @@ Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub, return nullptr; } -Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &ICI, Instruction *LHSI, - const APInt *RHSV) { +/// Fold icmp (add X, Y), C. +Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, Instruction *Add, + const APInt *C) { // FIXME: This check restricts all folds under here to scalar types. - ConstantInt *RHS = dyn_cast(ICI.getOperand(1)); + ConstantInt *RHS = dyn_cast(Cmp.getOperand(1)); if (!RHS) return nullptr; - // Fold: icmp pred (add X, C1), C2 - if (!ICI.isEquality()) { - ConstantInt *LHSC = dyn_cast(LHSI->getOperand(1)); - if (!LHSC) - return nullptr; + if (Cmp.isEquality()) + return nullptr; - const APInt &LHSV = LHSC->getValue(); - ConstantRange CR = - ICI.makeConstantRange(ICI.getPredicate(), *RHSV).subtract(LHSV); - - if (ICI.isSigned()) { - if (CR.getLower().isSignBit()) { - return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), - Builder->getInt(CR.getUpper())); - } else if (CR.getUpper().isSignBit()) { - return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), - Builder->getInt(CR.getLower())); - } - } else { - if (CR.getLower().isMinValue()) { - return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), - Builder->getInt(CR.getUpper())); - } else if (CR.getUpper().isMinValue()) { - return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), - Builder->getInt(CR.getLower())); - } - } + // Fold: icmp pred (add X, C2), C + Value *X = Add->getOperand(0); + ConstantInt *AddC = dyn_cast(Add->getOperand(1)); + if (!AddC) + return nullptr; - // X-C1 (X & -C2) == C1 - // iff C1 & (C2-1) == 0 + const APInt &C2 = AddC->getValue(); + ConstantRange CR = Cmp.makeConstantRange(Cmp.getPredicate(), *C).subtract(C2); + const APInt &Upper = CR.getUpper(); + const APInt &Lower = CR.getLower(); + if (Cmp.isSigned()) { + if (Lower.isSignBit()) + return new ICmpInst(ICmpInst::ICMP_SLT, X, Builder->getInt(Upper)); + if (Upper.isSignBit()) + return new ICmpInst(ICmpInst::ICMP_SGE, X, Builder->getInt(Lower)); + } else { + if (Lower.isMinValue()) + return new ICmpInst(ICmpInst::ICMP_ULT, X, Builder->getInt(Upper)); + if (Upper.isMinValue()) + return new ICmpInst(ICmpInst::ICMP_UGE, X, Builder->getInt(Lower)); + } + + if (Add->hasOneUse()) { + // X+C (X & -C2) == C + // iff C & (C2-1) == 0 // C2 is a power of 2 - if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() && - RHSV->isPowerOf2() && (LHSV & (*RHSV - 1)) == 0) - return new ICmpInst(ICmpInst::ICMP_EQ, - Builder->CreateAnd(LHSI->getOperand(0), -(*RHSV)), - ConstantExpr::getNeg(LHSC)); - - // X-C1 >u C2 -> (X & ~C2) != C1 - // iff C1 & C2 == 0 + if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && C->isPowerOf2() && + (C2 & (*C - 1)) == 0) + return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateAnd(X, -(*C)), + ConstantExpr::getNeg(AddC)); + + // X+C >u C2 -> (X & ~C2) != C + // iff C & C2 == 0 // C2+1 is a power of 2 - if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() && - (*RHSV + 1).isPowerOf2() && (LHSV & *RHSV) == 0) - return new ICmpInst(ICmpInst::ICMP_NE, - Builder->CreateAnd(LHSI->getOperand(0), ~(*RHSV)), - ConstantExpr::getNeg(LHSC)); + if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && + (C2 & *C) == 0) + return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateAnd(X, ~(*C)), + ConstantExpr::getNeg(AddC)); } return nullptr; } -- 2.7.4