16 #define DEBUG_TYPE "indvars"
33 STATISTIC(NumElimIdentity,
"Number of IV identities eliminated");
34 STATISTIC(NumElimOperand,
"Number of IV operands folded into a use");
35 STATISTIC(NumElimRem ,
"Number of IV remainder operations eliminated");
36 STATISTIC(NumElimCmp ,
"Number of IV comparisons eliminated");
43 class SimplifyIndvar {
62 assert(
LI &&
"IV simplification requires LoopInfo");
65 bool hasChanged()
const {
return Changed; }
92 const SCEV *FoldedExpr = 0;
96 case Instruction::UDiv:
97 case Instruction::LShr:
100 if (IVOperand != UseInst->
getOperand(OperIdx) ||
106 if (!isa<BinaryOperator>(IVOperand)
107 || !isa<ConstantInt>(IVOperand->
getOperand(1)))
112 assert(SE->isSCEVable(IVSrc->
getType()) &&
"Expect SCEVable IV operand");
115 if (UseInst->
getOpcode() == Instruction::LShr) {
124 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
127 if (!SE->isSCEVable(UseInst->
getType()))
131 if (SE->getSCEV(UseInst) != FoldedExpr)
134 DEBUG(
dbgs() <<
"INDVARS: Eliminated IV operand: " << *IVOperand
135 <<
" -> " << *UseInst <<
'\n');
138 assert(SE->getSCEV(UseInst) == FoldedExpr &&
"bad SCEV with folded oper");
143 DeadInsts.push_back(IVOperand);
149 void SimplifyIndvar::eliminateIVComparison(
ICmpInst *ICmp,
Value *IVOperand) {
150 unsigned IVOperIdx = 0;
154 assert(IVOperand == ICmp->
getOperand(1) &&
"Can't find IVOperand");
165 S = SE->getSCEVAtScope(S, ICmpLoop);
166 X = SE->getSCEVAtScope(X, ICmpLoop);
170 if (SE->isKnownPredicate(Pred, S, X))
177 DEBUG(
dbgs() <<
"INDVARS: Eliminated comparison: " << *ICmp <<
'\n');
180 DeadInsts.push_back(ICmp);
199 S = SE->getSCEVAtScope(S, ICmpLoop);
200 X = SE->getSCEVAtScope(X, ICmpLoop);
203 if ((!IsSigned || SE->isKnownNonNegative(S)) &&
209 const SCEV *LessOne =
210 SE->getMinusSCEV(S, SE->getConstant(S->
getType(), 1));
211 if (IsSigned && !SE->isKnownNonNegative(LessOne))
214 if (!SE->isKnownPredicate(IsSigned ?
228 DEBUG(
dbgs() <<
"INDVARS: Simplified rem: " << *Rem <<
'\n');
231 DeadInsts.push_back(Rem);
237 bool SimplifyIndvar::eliminateIVUser(
Instruction *UseInst,
239 if (
ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
240 eliminateIVComparison(ICmp, IVOperand);
244 bool IsSigned = Rem->
getOpcode() == Instruction::SRem;
245 if (IsSigned || Rem->
getOpcode() == Instruction::URem) {
246 eliminateIVRemainder(Rem, IVOperand, IsSigned);
252 if (!SE->isSCEVable(UseInst->
getType()) ||
254 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
257 DEBUG(
dbgs() <<
"INDVARS: Eliminated identity: " << *UseInst <<
'\n');
262 DeadInsts.push_back(UseInst);
271 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
281 if (User != Def && Simplified.
insert(User))
282 SimpleIVUsers.push_back(std::make_pair(User, Def));
320 if (!SE->isSCEVable(CurrIV->
getType()))
334 while (!SimpleIVUsers.
empty()) {
335 std::pair<Instruction*, Instruction*> UseOper =
338 if (UseOper.first == CurrIV)
continue;
341 for (
unsigned N = 0; IVOperand; ++
N) {
342 assert(
N <= Simplified.
size() &&
"runaway iteration");
344 Value *NewOper = foldIVUser(UseOper.first, IVOperand);
352 if (eliminateIVUser(UseOper.first, IVOperand)) {
362 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
369 void IVVisitor::anchor() { }
378 SIV.simplifyUsers(CurrIV, V);
379 return SIV.hasChanged();
386 bool Changed =
false;
static ConstantInt * getFalse(LLVMContext &Context)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=0)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Predicate getInversePredicate() const
Return the inverse of the instruction's predicate.
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
Base class of casting instructions.
const APInt & getValue() const
Return the constant's value.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
uint64_t getZExtValue() const
Return the zero extended value.
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead, IVVisitor *V=NULL)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Loop * getLoopFor(const BasicBlock *BB) const
void replaceAllUsesWith(Value *V)
bool isSCEVable(Type *Ty) const
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM, SmallVectorImpl< WeakVH > &Dead)
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Represent an integer comparison operator.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Value * getOperand(unsigned i) const
Predicate getPredicate() const
Return the predicate for this instruction.
LLVMContext & getContext() const
All values hold a context through their type.
BinaryOps getOpcode() const
AnalysisType & getAnalysis() const
Class for constant integers.
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static ConstantInt * getTrue(LLVMContext &Context)
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE)
STATISTIC(NumElimIdentity,"Number of IV identities eliminated")
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)
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD