34 #define DEBUG_TYPE "indvars"
36 STATISTIC(NumElimIdentity,
"Number of IV identities eliminated");
37 STATISTIC(NumElimOperand,
"Number of IV operands folded into a use");
38 STATISTIC(NumElimRem ,
"Number of IV remainder operations eliminated");
39 STATISTIC(NumElimCmp ,
"Number of IV comparisons eliminated");
46 class SimplifyIndvar {
58 : L(Loop), LI(LI), SE(SE), DeadInsts(Dead), Changed(
false) {
59 assert(LI &&
"IV simplification requires LoopInfo");
62 bool hasChanged()
const {
return Changed; }
91 Value *IVSrc =
nullptr;
93 const SCEV *FoldedExpr =
nullptr;
97 case Instruction::UDiv:
98 case Instruction::LShr:
101 if (IVOperand != UseInst->
getOperand(OperIdx) ||
107 if (!isa<BinaryOperator>(IVOperand)
108 || !isa<ConstantInt>(IVOperand->
getOperand(1)))
113 assert(SE->isSCEVable(IVSrc->
getType()) &&
"Expect SCEVable IV operand");
116 if (UseInst->
getOpcode() == Instruction::LShr) {
125 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
128 if (!SE->isSCEVable(UseInst->
getType()))
132 if (SE->getSCEV(UseInst) != FoldedExpr)
135 DEBUG(
dbgs() <<
"INDVARS: Eliminated IV operand: " << *IVOperand
136 <<
" -> " << *UseInst <<
'\n');
139 assert(SE->getSCEV(UseInst) == FoldedExpr &&
"bad SCEV with folded oper");
144 DeadInsts.emplace_back(IVOperand);
150 void SimplifyIndvar::eliminateIVComparison(
ICmpInst *ICmp,
Value *IVOperand) {
151 unsigned IVOperIdx = 0;
155 assert(IVOperand == ICmp->
getOperand(1) &&
"Can't find IVOperand");
166 S = SE->getSCEVAtScope(S, ICmpLoop);
167 X = SE->getSCEVAtScope(X, ICmpLoop);
171 if (SE->isKnownPredicate(Pred, S, X))
178 DEBUG(
dbgs() <<
"INDVARS: Eliminated comparison: " << *ICmp <<
'\n');
181 DeadInsts.emplace_back(ICmp);
200 S = SE->getSCEVAtScope(S, ICmpLoop);
201 X = SE->getSCEVAtScope(X, ICmpLoop);
204 if ((!IsSigned || SE->isKnownNonNegative(S)) &&
210 const SCEV *LessOne =
211 SE->getMinusSCEV(S, SE->getConstant(S->
getType(), 1));
212 if (IsSigned && !SE->isKnownNonNegative(LessOne))
215 if (!SE->isKnownPredicate(IsSigned ?
229 DEBUG(
dbgs() <<
"INDVARS: Simplified rem: " << *Rem <<
'\n');
232 DeadInsts.emplace_back(Rem);
238 bool SimplifyIndvar::eliminateIVUser(
Instruction *UseInst,
240 if (
ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
241 eliminateIVComparison(ICmp, IVOperand);
245 bool IsSigned = Rem->
getOpcode() == Instruction::SRem;
246 if (IsSigned || Rem->
getOpcode() == Instruction::URem) {
247 eliminateIVRemainder(Rem, IVOperand, IsSigned);
253 if (!SE->isSCEVable(UseInst->
getType()) ||
255 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
258 DEBUG(
dbgs() <<
"INDVARS: Eliminated identity: " << *UseInst <<
'\n');
263 DeadInsts.emplace_back(UseInst);
269 bool SimplifyIndvar::strengthenOverflowingOperation(
BinaryOperator *BO,
283 case Instruction::Add:
287 case Instruction::Sub:
291 case Instruction::Mul:
301 bool Changed =
false;
304 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
305 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
306 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
308 if (ExtendAfterOp == OpAfterExtend) {
316 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
317 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
318 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
320 if (ExtendAfterOp == OpAfterExtend) {
346 if (ExtractInst->getNumIndices() != 1)
348 if (ExtractInst->getIndices()[0] == 0)
349 AddVal = ExtractInst;
350 else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse())
351 Branch = dyn_cast<BranchInst>(ExtractInst->user_back());
354 if (!AddVal || !Branch)
363 for (
Use &U : AddVal->
uses()) {
364 if (
Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) {
366 if (
PHINode *
PHI = dyn_cast<PHINode>(UseInst))
367 UseBB =
PHI->getIncomingBlock(U);
384 assert((AddInst->
getOpcode() == Instruction::Add &&
386 "Bad add instruction created from overflow intrinsic.");
389 DeadInsts.emplace_back(AddVal);
397 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
406 if (UI != Def && Simplified.
insert(UI).second)
407 SimpleIVUsers.push_back(std::make_pair(UI, Def));
445 if (!SE->isSCEVable(CurrIV->
getType()))
459 while (!SimpleIVUsers.
empty()) {
460 std::pair<Instruction*, Instruction*> UseOper =
465 if (UseInst == CurrIV)
continue;
468 UseInst = splitOverflowIntrinsic(UseInst, V->
getDomTree());
474 for (
unsigned N = 0; IVOperand; ++
N) {
475 assert(
N <= Simplified.
size() &&
"runaway iteration");
477 Value *NewOper = foldIVUser(UseOper.first, IVOperand);
485 if (eliminateIVUser(UseOper.first, IVOperand)) {
490 if (
BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
491 if (isa<OverflowingBinaryOperator>(BO) &&
492 strengthenOverflowingOperation(BO, IVOperand)) {
505 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
521 SIV.simplifyUsers(CurrIV, V);
522 return SIV.hasChanged();
529 bool Changed =
false;
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
static ConstantInt * getFalse(LLVMContext &Context)
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
iterator_range< use_iterator > uses()
STATISTIC(NumFunctions,"Total number of functions")
Intrinsic::ID getIntrinsicID() const
getIntrinsicID - Return the intrinsic ID of this intrinsic.
ScalarEvolution - This class is the main scalar evolution driver.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
const DominatorTree * getDomTree() const
BlockT * getHeader() const
Interface for visiting interesting IV users that are recognized but not simplified by this utility...
SelectInst - This class represents the LLVM 'select' instruction.
This is the base class for all instructions that perform data casts.
const APInt & getValue() const
Return the constant as an APInt value reference.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
A Use represents the edge between a Value definition and its users.
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
bool shouldSplitOverflowInstrinsics() const
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
BasicBlock * getSuccessor(unsigned i) const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
bool isSCEVable(Type *Ty) const
isSCEVable - Test if values of the given type are analyzable within the SCEV framework.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
BranchInst - Conditional or Unconditional Branch instruction.
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead, IVVisitor *V=nullptr)
simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution t...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead)
SimplifyLoopIVs - Simplify users of induction variables within this loop.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
Predicate getPredicate() const
Return the predicate for this instruction.
LLVMContext & getContext() const
All values hold a context through their type.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
BinaryOps getOpcode() const
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static ConstantInt * getTrue(LLVMContext &Context)
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE)
Return true if this instruction generates a simple SCEV expression in terms of that IV...
iterator_range< user_iterator > users()
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
SCEV - This class represents an analyzed expression in the program.
virtual void visitCast(CastInst *Cast)=0
const Loop * getLoop() const
static void pushIVUsers(Instruction *Def, SmallPtrSet< Instruction *, 16 > &Simplified, SmallVectorImpl< std::pair< Instruction *, Instruction * > > &SimpleIVUsers)
Add all uses of Def to the current IV's worklist.
LLVM Value Representation.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
const SCEV * getSCEV(Value *V)
getSCEV - Return a SCEV expression for the full generality of the specified expression.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
The legacy pass manager's analysis pass to compute loop information.
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMulExpr - Get a canonical multiply expression, or something simpler if possible.
const BasicBlock * getParent() const
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.