Rename ScalarEvolution's getIterationCount to getBackedgeTakenCount,

to more accurately describe what it does. Expand its doxygen comment
to describe what the backedge-taken count is and how it differs
from the actual iteration count of the loop. Adjust names and
comments in associated code accordingly.

llvm-svn: 65382
This commit is contained in:
Dan Gohman 2009-02-24 18:55:53 +00:00
parent d4ecca135a
commit 0bddac16a8
21 changed files with 177 additions and 145 deletions

View File

@ -298,18 +298,28 @@ namespace llvm {
bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
SCEV *LHS, SCEV *RHS);
/// getIterationCount - If the specified loop has a predictable iteration
/// count, return it, otherwise return a SCEVCouldNotCompute object.
SCEVHandle getIterationCount(const Loop *L) const;
/// getBackedgeTakenCount - If the specified loop has a predictable
/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
/// object. The backedge-taken count is the number of times the loop header
/// will be branched to from within the loop. This is one less than the
/// trip count of the loop, since it doesn't count the first iteration,
/// when the header is branched to from outside the loop.
///
/// Note that it is not valid to call this method on a loop without a
/// loop-invariant backedge-taken count (see
/// hasLoopInvariantBackedgeTakenCount).
///
SCEVHandle getBackedgeTakenCount(const Loop *L) const;
/// hasLoopInvariantIterationCount - Return true if the specified loop has
/// an analyzable loop-invariant iteration count.
bool hasLoopInvariantIterationCount(const Loop *L) const;
/// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
/// has an analyzable loop-invariant backedge-taken count.
bool hasLoopInvariantBackedgeTakenCount(const Loop *L) const;
/// forgetLoopIterationCount - This method should be called by the
/// forgetLoopBackedgeTakenCount - This method should be called by the
/// client when it has changed a loop in a way that may effect
/// ScalarEvolution's ability to compute a trip count.
void forgetLoopIterationCount(const Loop *L);
/// ScalarEvolution's ability to compute a trip count, or if the loop
/// is deleted.
void forgetLoopBackedgeTakenCount(const Loop *L);
/// deleteValueFromRecords - This method should be called by the
/// client before it removes a Value from the program, to make sure

View File

@ -27,7 +27,7 @@ static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", true, true);
/// getRange - determine the range for a particular SCEV within a given Loop
ConstantRange LoopVR::getRange(SCEVHandle S, Loop *L, ScalarEvolution &SE) {
SCEVHandle T = SE.getIterationCount(L);
SCEVHandle T = SE.getBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(T))
return ConstantRange(cast<IntegerType>(S->getType())->getBitWidth(), true);

View File

@ -1419,9 +1419,9 @@ namespace {
///
std::map<Value*, SCEVHandle> Scalars;
/// IterationCounts - Cache the iteration count of the loops for this
/// function as they are computed.
std::map<const Loop*, SCEVHandle> IterationCounts;
/// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
/// this function as they are computed.
std::map<const Loop*, SCEVHandle> BackedgeTakenCounts;
/// ConstantEvolutionLoopExitValue - This map contains entries for all of
/// the PHI instructions that we attempt to compute constant evolutions for.
@ -1464,19 +1464,28 @@ namespace {
bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
SCEV *LHS, SCEV *RHS);
/// hasLoopInvariantIterationCount - Return true if the specified loop has
/// an analyzable loop-invariant iteration count.
bool hasLoopInvariantIterationCount(const Loop *L);
/// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
/// has an analyzable loop-invariant backedge-taken count.
bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
/// forgetLoopIterationCount - This method should be called by the
/// forgetLoopBackedgeTakenCount - This method should be called by the
/// client when it has changed a loop in a way that may effect
/// ScalarEvolution's ability to compute a trip count.
void forgetLoopIterationCount(const Loop *L);
/// ScalarEvolution's ability to compute a trip count, or if the loop
/// is deleted.
void forgetLoopBackedgeTakenCount(const Loop *L);
/// getIterationCount - If the specified loop has a predictable iteration
/// count, return it. Note that it is not valid to call this method on a
/// loop without a loop-invariant iteration count.
SCEVHandle getIterationCount(const Loop *L);
/// getBackedgeTakenCount - If the specified loop has a predictable
/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
/// object. The backedge-taken count is the number of times the loop header
/// will be branched to from within the loop. This is one less than the
/// trip count of the loop, since it doesn't count the first iteration,
/// when the header is branched to from outside the loop.
///
/// Note that it is not valid to call this method on a loop without a
/// loop-invariant backedge-taken count (see
/// hasLoopInvariantBackedgeTakenCount).
///
SCEVHandle getBackedgeTakenCount(const Loop *L);
/// deleteValueFromRecords - This method should be called by the
/// client before it removes a value from the program, to make sure
@ -1500,24 +1509,25 @@ namespace {
const SCEVHandle &SymName,
const SCEVHandle &NewVal);
/// ComputeIterationCount - Compute the number of times the specified loop
/// will iterate.
SCEVHandle ComputeIterationCount(const Loop *L);
/// ComputeBackedgeTakenCount - Compute the number of times the specified
/// loop will iterate.
SCEVHandle ComputeBackedgeTakenCount(const Loop *L);
/// ComputeLoadConstantCompareIterationCount - Given an exit condition of
/// 'icmp op load X, cst', try to see if we can compute the trip count.
SCEVHandle ComputeLoadConstantCompareIterationCount(LoadInst *LI,
Constant *RHS,
const Loop *L,
ICmpInst::Predicate p);
/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
/// of 'icmp op load X, cst', try to see if we can compute the trip count.
SCEVHandle
ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
Constant *RHS,
const Loop *L,
ICmpInst::Predicate p);
/// ComputeIterationCountExhaustively - If the trip is known to execute a
/// constant number of times (the condition evolves only from constants),
/// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute
/// a constant number of times (the condition evolves only from constants),
/// try to evaluate a few iterations of the loop until we get the exit
/// condition gets a value of ExitWhen (true or false). If we cannot
/// evaluate the trip count of the loop, return UnknownValue.
SCEVHandle ComputeIterationCountExhaustively(const Loop *L, Value *Cond,
bool ExitWhen);
SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond,
bool ExitWhen);
/// HowFarToZero - Return the number of times a backedge comparing the
/// specified value to zero will execute. If not computable, return
@ -1545,7 +1555,7 @@ namespace {
/// in the header of its containing loop, we know the loop executes a
/// constant number of times, and the PHI node is just a recurrence
/// involving constants, fold it.
Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its,
Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
const Loop *L);
};
}
@ -1931,14 +1941,22 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
// Iteration Count Computation Code
//
/// getIterationCount - If the specified loop has a predictable iteration
/// count, return it. Note that it is not valid to call this method on a
/// loop without a loop-invariant iteration count.
SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
std::map<const Loop*, SCEVHandle>::iterator I = IterationCounts.find(L);
if (I == IterationCounts.end()) {
SCEVHandle ItCount = ComputeIterationCount(L);
I = IterationCounts.insert(std::make_pair(L, ItCount)).first;
/// getBackedgeTakenCount - If the specified loop has a predictable
/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
/// object. The backedge-taken count is the number of times the loop header
/// will be branched to from within the loop. This is one less than the
/// trip count of the loop, since it doesn't count the first iteration,
/// when the header is branched to from outside the loop.
///
/// Note that it is not valid to call this method on a loop without a
/// loop-invariant backedge-taken count (see
/// hasLoopInvariantBackedgeTakenCount).
///
SCEVHandle ScalarEvolutionsImpl::getBackedgeTakenCount(const Loop *L) {
std::map<const Loop*, SCEVHandle>::iterator I = BackedgeTakenCounts.find(L);
if (I == BackedgeTakenCounts.end()) {
SCEVHandle ItCount = ComputeBackedgeTakenCount(L);
I = BackedgeTakenCounts.insert(std::make_pair(L, ItCount)).first;
if (ItCount != UnknownValue) {
assert(ItCount->isLoopInvariant(L) &&
"Computed trip count isn't loop invariant for loop!");
@ -1951,16 +1969,17 @@ SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
return I->second;
}
/// forgetLoopIterationCount - This method should be called by the
/// forgetLoopBackedgeTakenCount - This method should be called by the
/// client when it has changed a loop in a way that may effect
/// ScalarEvolution's ability to compute a trip count.
void ScalarEvolutionsImpl::forgetLoopIterationCount(const Loop *L) {
IterationCounts.erase(L);
/// ScalarEvolution's ability to compute a trip count, or if the loop
/// is deleted.
void ScalarEvolutionsImpl::forgetLoopBackedgeTakenCount(const Loop *L) {
BackedgeTakenCounts.erase(L);
}
/// ComputeIterationCount - Compute the number of times the specified loop
/// will iterate.
SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
/// ComputeBackedgeTakenCount - Compute the number of times the backedge
/// of the specified loop will execute.
SCEVHandle ScalarEvolutionsImpl::ComputeBackedgeTakenCount(const Loop *L) {
// If the loop has a non-one exit block count, we can't analyze it.
SmallVector<BasicBlock*, 8> ExitBlocks;
L->getExitBlocks(ExitBlocks);
@ -2010,7 +2029,7 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
// Note that ICmpInst deals with pointer comparisons too so we must check
// the type of the operand.
if (ExitCond == 0 || isa<PointerType>(ExitCond->getOperand(0)->getType()))
return ComputeIterationCountExhaustively(L, ExitBr->getCondition(),
return ComputeBackedgeTakenCountExhaustively(L, ExitBr->getCondition(),
ExitBr->getSuccessor(0) == ExitBlock);
// If the condition was exit on true, convert the condition to exit on false
@ -2024,7 +2043,7 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
SCEVHandle ItCnt =
ComputeLoadConstantCompareIterationCount(LI, RHS, L, Cond);
ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
if (!isa<SCEVCouldNotCompute>(ItCnt)) return ItCnt;
}
@ -2107,7 +2126,7 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
}
default:
#if 0
cerr << "ComputeIterationCount ";
cerr << "ComputeBackedgeTakenCount ";
if (ExitCond->getOperand(0)->getType()->isUnsigned())
cerr << "[unsigned] ";
cerr << *LHS << " "
@ -2116,8 +2135,9 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
#endif
break;
}
return ComputeIterationCountExhaustively(L, ExitCond,
ExitBr->getSuccessor(0) == ExitBlock);
return
ComputeBackedgeTakenCountExhaustively(L, ExitCond,
ExitBr->getSuccessor(0) == ExitBlock);
}
static ConstantInt *
@ -2164,12 +2184,13 @@ GetAddressedElementFromGlobal(GlobalVariable *GV,
return Init;
}
/// ComputeLoadConstantCompareIterationCount - Given an exit condition of
/// 'icmp op load X, cst', try to see if we can compute the trip count.
/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
/// 'icmp op load X, cst', try to see if we can compute the backedge
/// execution count.
SCEVHandle ScalarEvolutionsImpl::
ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS,
const Loop *L,
ICmpInst::Predicate predicate) {
ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, Constant *RHS,
const Loop *L,
ICmpInst::Predicate predicate) {
if (LI->isVolatile()) return UnknownValue;
// Check to see if the loaded pointer is a getelementptr of a global.
@ -2326,13 +2347,13 @@ static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
/// constant number of times, and the PHI node is just a recurrence
/// involving constants, fold it.
Constant *ScalarEvolutionsImpl::
getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its, const Loop *L){
getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, const Loop *L){
std::map<PHINode*, Constant*>::iterator I =
ConstantEvolutionLoopExitValue.find(PN);
if (I != ConstantEvolutionLoopExitValue.end())
return I->second;
if (Its.ugt(APInt(Its.getBitWidth(),MaxBruteForceIterations)))
if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
@ -2352,10 +2373,10 @@ getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its, const Loop *L){
return RetVal = 0; // Not derived from same PHI.
// Execute the loop symbolically to determine the exit value.
if (Its.getActiveBits() >= 32)
if (BEs.getActiveBits() >= 32)
return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
unsigned NumIterations = Its.getZExtValue(); // must be in range
unsigned NumIterations = BEs.getZExtValue(); // must be in range
unsigned IterationNum = 0;
for (Constant *PHIVal = StartCST; ; ++IterationNum) {
if (IterationNum == NumIterations)
@ -2371,13 +2392,13 @@ getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its, const Loop *L){
}
}
/// ComputeIterationCountExhaustively - If the trip is known to execute a
/// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a
/// constant number of times (the condition evolves only from constants),
/// try to evaluate a few iterations of the loop until we get the exit
/// condition gets a value of ExitWhen (true or false). If we cannot
/// evaluate the trip count of the loop, return UnknownValue.
SCEVHandle ScalarEvolutionsImpl::
ComputeIterationCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
PHINode *PN = getConstantEvolvingPHI(Cond, L);
if (PN == 0) return UnknownValue;
@ -2440,15 +2461,17 @@ SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
if (PHINode *PN = dyn_cast<PHINode>(I))
if (PN->getParent() == LI->getHeader()) {
// Okay, there is no closed form solution for the PHI node. Check
// to see if the loop that contains it has a known iteration count.
// If so, we may be able to force computation of the exit value.
SCEVHandle IterationCount = getIterationCount(LI);
if (SCEVConstant *ICC = dyn_cast<SCEVConstant>(IterationCount)) {
// to see if the loop that contains it has a known backedge-taken
// count. If so, we may be able to force computation of the exit
// value.
SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(LI);
if (SCEVConstant *BTCC =
dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
// Okay, we know how many times the containing loop executes. If
// this is a constant evolving PHI node, get the final value at
// the specified iteration number.
Constant *RV = getConstantEvolutionLoopExitValue(PN,
ICC->getValue()->getValue(),
BTCC->getValue()->getValue(),
LI);
if (RV) return SE.getUnknown(RV);
}
@ -2552,11 +2575,11 @@ SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
// To evaluate this recurrence, we need to know how many times the AddRec
// loop iterates. Compute this now.
SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
if (IterationCount == UnknownValue) return UnknownValue;
SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
if (BackedgeTakenCount == UnknownValue) return UnknownValue;
// Then, evaluate the AddRec.
return AddRec->evaluateAtIteration(IterationCount, SE);
return AddRec->evaluateAtIteration(BackedgeTakenCount, SE);
}
return UnknownValue;
}
@ -3110,16 +3133,16 @@ bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
LHS, RHS);
}
SCEVHandle ScalarEvolution::getIterationCount(const Loop *L) const {
return ((ScalarEvolutionsImpl*)Impl)->getIterationCount(L);
SCEVHandle ScalarEvolution::getBackedgeTakenCount(const Loop *L) const {
return ((ScalarEvolutionsImpl*)Impl)->getBackedgeTakenCount(L);
}
bool ScalarEvolution::hasLoopInvariantIterationCount(const Loop *L) const {
return !isa<SCEVCouldNotCompute>(getIterationCount(L));
bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) const {
return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
}
void ScalarEvolution::forgetLoopIterationCount(const Loop *L) {
return ((ScalarEvolutionsImpl*)Impl)->forgetLoopIterationCount(L);
void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
return ((ScalarEvolutionsImpl*)Impl)->forgetLoopBackedgeTakenCount(L);
}
SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) const {
@ -3143,10 +3166,10 @@ static void PrintLoopInfo(std::ostream &OS, const ScalarEvolution *SE,
if (ExitBlocks.size() != 1)
OS << "<multiple exits> ";
if (SE->hasLoopInvariantIterationCount(L)) {
OS << *SE->getIterationCount(L) << " iterations! ";
if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
} else {
OS << "Unpredictable iteration count. ";
OS << "Unpredictable backedge-taken count. ";
}
OS << "\n";

View File

@ -93,12 +93,12 @@ namespace {
void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
SmallPtrSet<Instruction*, 16> &DeadInsts);
void LinearFunctionTestReplace(Loop *L, SCEVHandle IterationCount,
void LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
Value *IndVar,
BasicBlock *ExitingBlock,
BranchInst *BI,
SCEVExpander &Rewriter);
void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
void RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount);
void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts);
@ -232,7 +232,7 @@ void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
/// SCEV analysis can determine a loop-invariant trip count of the loop, which
/// is actually a much broader range than just linear tests.
void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
SCEVHandle IterationCount,
SCEVHandle BackedgeTakenCount,
Value *IndVar,
BasicBlock *ExitingBlock,
BranchInst *BI,
@ -241,43 +241,41 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
// against the preincremented value, otherwise we prefer to compare against
// the post-incremented value.
Value *CmpIndVar;
SCEVHandle RHS = BackedgeTakenCount;
if (ExitingBlock == L->getLoopLatch()) {
// What ScalarEvolution calls the "iteration count" is actually the
// number of times the branch is taken. Add one to get the number
// of times the branch is executed. If this addition may overflow,
// we have to be more pessimistic and cast the induction variable
// before doing the add.
SCEVHandle Zero = SE->getIntegerSCEV(0, IterationCount->getType());
// Add one to the "backedge-taken" count to get the trip count.
// If this addition may overflow, we have to be more pessimistic and
// cast the induction variable before doing the add.
SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
SCEVHandle N =
SE->getAddExpr(IterationCount,
SE->getIntegerSCEV(1, IterationCount->getType()));
SE->getAddExpr(BackedgeTakenCount,
SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
if ((isa<SCEVConstant>(N) && !N->isZero()) ||
SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
// No overflow. Cast the sum.
IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
} else {
// Potential overflow. Cast before doing the add.
IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
IndVar->getType());
IterationCount =
SE->getAddExpr(IterationCount,
SE->getIntegerSCEV(1, IndVar->getType()));
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
IndVar->getType());
RHS = SE->getAddExpr(RHS,
SE->getIntegerSCEV(1, IndVar->getType()));
}
// The IterationCount expression contains the number of times that the
// backedge actually branches to the loop header. This is one less than the
// number of times the loop executes, so add one to it.
// The BackedgeTaken expression contains the number of times that the
// backedge branches to the loop header. This is one less than the
// number of times the loop executes, so use the incremented indvar.
CmpIndVar = L->getCanonicalInductionVariableIncrement();
} else {
// We have to use the preincremented value...
IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
IndVar->getType());
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
IndVar->getType());
CmpIndVar = IndVar;
}
// Expand the code for the iteration count into the preheader of the loop.
BasicBlock *Preheader = L->getLoopPreheader();
Value *ExitCnt = Rewriter.expandCodeFor(IterationCount,
Value *ExitCnt = Rewriter.expandCodeFor(RHS,
Preheader->getTerminator());
// Insert a new icmp_ne or icmp_eq instruction before the branch.
@ -291,7 +289,7 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
<< " LHS:" << *CmpIndVar // includes a newline
<< " op:\t"
<< (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
<< " RHS:\t" << *IterationCount << "\n";
<< " RHS:\t" << *RHS << "\n";
Value *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI);
BI->setCondition(Cond);
@ -304,7 +302,7 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
/// final value of any expressions that are recurrent in the loop, and
/// substitute the exit values from the loop into any instructions outside of
/// the loop that use the final values of the current expressions.
void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount) {
BasicBlock *Preheader = L->getLoopPreheader();
// Scan all of the instructions in the loop, looking at those that have
@ -322,7 +320,7 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
BlockToInsertInto = Preheader;
BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
bool HasConstantItCount = isa<SCEVConstant>(IterationCount);
bool HasConstantItCount = isa<SCEVConstant>(BackedgeTakenCount);
SmallPtrSet<Instruction*, 16> InstructionsToDelete;
std::map<Instruction*, Value*> ExitValues;
@ -435,7 +433,7 @@ void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
// may not have been able to compute a trip count. Now that we've done some
// re-writing, the trip count may be computable.
if (Changed)
SE->forgetLoopIterationCount(L);
SE->forgetLoopBackedgeTakenCount(L);
if (!DeadInsts.empty())
DeleteTriviallyDeadInstructions(DeadInsts);
@ -473,7 +471,8 @@ static const Type *getEffectiveIndvarType(const PHINode *Phi) {
/// variables, return the PHI for this induction variable.
///
/// TODO: This duplicates a fair amount of ScalarEvolution logic.
/// Perhaps this can be merged with ScalarEvolution::getIterationCount
/// Perhaps this can be merged with
/// ScalarEvolution::getBackedgeTakenCount
/// and/or ScalarEvolution::get{Sign,Zero}ExtendExpr.
///
static const PHINode *TestOrigIVForWrap(const Loop *L,
@ -622,9 +621,9 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
// loop into any instructions outside of the loop that use the final values of
// the current expressions.
//
SCEVHandle IterationCount = SE->getIterationCount(L);
if (!isa<SCEVCouldNotCompute>(IterationCount))
RewriteLoopExitValues(L, IterationCount);
SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
RewriteLoopExitValues(L, BackedgeTakenCount);
// Next, analyze all of the induction variables in the loop, canonicalizing
// auxillary induction variables.
@ -649,9 +648,9 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
// the set of the types of the other recurrence expressions.
const Type *LargestType = 0;
SmallSetVector<const Type *, 4> SizesToInsert;
if (!isa<SCEVCouldNotCompute>(IterationCount)) {
LargestType = IterationCount->getType();
SizesToInsert.insert(IterationCount->getType());
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
LargestType = BackedgeTakenCount->getType();
SizesToInsert.insert(BackedgeTakenCount->getType());
}
for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
const PHINode *PN = IndVars[i].first;
@ -682,7 +681,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
bool NoSignedWrap = false;
bool NoUnsignedWrap = false;
const PHINode *OrigControllingPHI = 0;
if (!isa<SCEVCouldNotCompute>(IterationCount) && ExitingBlock)
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock)
// Can't rewrite non-branch yet.
if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) {
if (Instruction *OrigCond = dyn_cast<Instruction>(BI->getCondition())) {
@ -695,7 +694,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
DeadInsts.insert(OrigCond);
}
LinearFunctionTestReplace(L, IterationCount, IndVar,
LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
ExitingBlock, BI, Rewriter);
}

View File

@ -190,7 +190,7 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
// Don't remove loops for which we can't solve the trip count.
// They could be infinite, in which case we'd be changing program behavior.
ScalarEvolution& SE = getAnalysis<ScalarEvolution>();
SCEVHandle S = SE.getIterationCount(L);
SCEVHandle S = SE.getBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(S))
return false;
@ -267,7 +267,7 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
(*LI)->eraseFromParent();
// Tell ScalarEvolution that the loop is deleted.
SE.forgetLoopIterationCount(L);
SE.forgetLoopBackedgeTakenCount(L);
// Finally, the blocks from loopinfo. This has to happen late because
// otherwise our loop iterators won't work.

View File

@ -2351,13 +2351,13 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
if (!Sel || !Sel->hasOneUse()) return Cond;
SCEVHandle IterationCount = SE->getIterationCount(L);
if (isa<SCEVCouldNotCompute>(IterationCount))
SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
return Cond;
SCEVHandle One = SE->getIntegerSCEV(1, IterationCount->getType());
SCEVHandle One = SE->getIntegerSCEV(1, BackedgeTakenCount->getType());
// Adjust for an annoying getIterationCount quirk.
IterationCount = SE->getAddExpr(IterationCount, One);
// Add one to the backedge-taken count to get the trip count.
SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One);
// Check for a max calculation that matches the pattern.
SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount);
@ -2412,8 +2412,8 @@ ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
/// inside the loop then try to eliminate the cast opeation.
void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
SCEVHandle IterationCount = SE->getIterationCount(L);
if (isa<SCEVCouldNotCompute>(IterationCount))
SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
return;
for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e;

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {Loop bb: 100 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {Loop bb: backedge-taken count is 100}
; PR1533
@array = weak global [101 x i32] zeroinitializer, align 32 ; <[100 x i32]*> [#uses=1]

View File

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb: (-1 + (-1 \\* %x) + %y) iterations!}
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb: backedge-taken count is (-1 + (-1 \\* %x) + %y)}
; PR1597
define i32 @f(i32 %x, i32 %y) {

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {13 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 13}
; PR1706
define i32 @f() {

View File

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop header: (0 smax %n) iterations!}
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop header: backedge-taken count is (0 smax %n)}
define void @foo(i32 %n) {
entry:

View File

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop loop: (100 + (-100 smax %n)) iterations!}
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop loop: backedge-taken count is (100 + (-100 smax %n))}
; PR2002
define void @foo(i8 %n) {

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {61 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 61}
; PR2364
define i32 @func_6() nounwind {

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {113 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 113}
; PR2088
define void @fun() {

View File

@ -1,6 +1,6 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
; RUN: -scalar-evolution-max-iterations=0 | \
; RUN: grep -F "(-1 + (-1 * %j)) iterations"
; RUN: grep -F "backedge-taken count is (-1 + (-1 * %j))"
; PR2607
define i32 @_Z1aj(i32 %j) nounwind {

View File

@ -1,6 +1,6 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
; RUN: -scalar-evolution-max-iterations=0 | \
; RUN: grep -F "(-2147483632 + (2147483632 smax (-1 + (-1 * %x)) smax (-1 + (-1 * %y)))) iterations"
; RUN: grep -F "backedge-taken count is (-2147483632 + (2147483632 smax (-1 + (-1 * %x)) smax (-1 + (-1 * %y))))"
; PR2607
define i32 @b(i32 %x, i32 %y) {

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
; RUN: grep {Loop bb: (7 + (-1 \\* %argc)) iterations!}
; RUN: grep {Loop bb: backedge-taken count is (7 + (-1 \\* %argc))}
; XFAIL: *
define i32 @main(i32 %argc, i8** %argv) nounwind {

View File

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {255 iterations}
; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {backedge-taken count is 255}
; XFAIL: *
define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind {

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {100 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 100}
; PR1101
@A = weak global [1000 x i32] zeroinitializer, align 32

View File

@ -1,4 +1,4 @@
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb3: (-1 + %n) iterations!}
; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb3: backedge-taken count is (-1 + %n)}
; We don't want to use a max in the trip count expression in
; this testcase.

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
; RUN: -scalar-evolution-max-iterations=0 | grep {10000 iterations}
; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 10000}
; PR1101
@A = weak global [1000 x i32] zeroinitializer, align 32

View File

@ -1,5 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution | \
; RUN: grep {4 iterations}
; RUN: grep {backedge-taken count is 4}
; PR1101
@A = weak global [1000 x i32] zeroinitializer, align 32