From 7d9ebaf3373f18536793e265bf8b81848442fbd4 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 31 Aug 2016 00:19:35 +0000 Subject: [PATCH] [InstCombine] clean up InsertRangeTest; NFCI It's much less code and easier to read if we don't duplicate everything between the 'Inside' and not 'Inside' cases. As noted with the FIXME, the goal is to make this vector-friendly in a follow-up patch. llvm-svn: 280183 --- .../Transforms/InstCombine/InstCombineAndOrXor.cpp | 50 +++++++--------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 2001af3..65fb288 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -283,51 +283,31 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, } /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise -/// (V < Lo || V >= Hi). In practice, we emit the more efficient -/// (V-Lo) \= Hi). This method expects that Lo <= Hi. IsSigned indicates +/// whether to treat V, Lo, and Hi as signed or not. Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside) { + // FIXME: This could use APInt and work with vector splat constants. assert(cast(ConstantExpr::getICmp((isSigned ? ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && "Lo is not <= Hi in range emission code!"); - if (Inside) { - if (Lo == Hi) // Trivially false. - return Builder->getFalse(); + if (Lo == Hi) + return Inside ? Builder->getFalse() : Builder->getTrue(); - // V >= Min && V < Hi --> V < Hi - if (cast(Lo)->isMinValue(isSigned)) { - ICmpInst::Predicate pred = (isSigned ? - ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); - return Builder->CreateICmp(pred, V, Hi); - } - - // Emit V-Lo CreateAdd(V, NegLo, V->getName()+".off"); - Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); - return Builder->CreateICmpULT(Add, UpperBound); - } - - if (Lo == Hi) // Trivially true. - return Builder->getTrue(); - - // V < Min || V >= Hi -> V > Hi-1 - Hi = SubOne(cast(Hi)); + // V >= Min && V < Hi --> V < Hi + // V < Min || V >= Hi --> V >= Hi + ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE; if (cast(Lo)->isMinValue(isSigned)) { - ICmpInst::Predicate pred = (isSigned ? - ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); - return Builder->CreateICmp(pred, V, Hi); + Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred; + return Builder->CreateICmp(Pred, V, Hi); } - // Emit V-Lo >u Hi-1-Lo - // Note that Hi has already had one subtracted from it, above. - ConstantInt *NegLo = cast(ConstantExpr::getNeg(Lo)); - Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off"); - Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); - return Builder->CreateICmpUGT(Add, LowerBound); + // V >= Lo && V < Hi --> V - Lo u< Hi - Lo + // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo + Value *VMinusLo = Builder->CreateSub(V, Lo, V->getName() + ".off"); + Constant *HiMinusLo = ConstantExpr::getSub(Hi, Lo); + return Builder->CreateICmp(Pred, VMinusLo, HiMinusLo); } /// Returns true iff Val consists of one contiguous run of 1s with any number -- 2.7.4