llvm.org GIT mirror llvm / 46bdfb0
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65382 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
21 changed file(s) with 190 addition(s) and 158 deletion(s). Raw diff Collapse all Expand all
297297 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
298298 SCEV *LHS, SCEV *RHS);
299299
300 /// getIterationCount - If the specified loop has a predictable iteration
301 /// count, return it, otherwise return a SCEVCouldNotCompute object.
302 SCEVHandle getIterationCount(const Loop *L) const;
303
304 /// hasLoopInvariantIterationCount - Return true if the specified loop has
305 /// an analyzable loop-invariant iteration count.
306 bool hasLoopInvariantIterationCount(const Loop *L) const;
307
308 /// forgetLoopIterationCount - This method should be called by the
300 /// getBackedgeTakenCount - If the specified loop has a predictable
301 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
302 /// object. The backedge-taken count is the number of times the loop header
303 /// will be branched to from within the loop. This is one less than the
304 /// trip count of the loop, since it doesn't count the first iteration,
305 /// when the header is branched to from outside the loop.
306 ///
307 /// Note that it is not valid to call this method on a loop without a
308 /// loop-invariant backedge-taken count (see
309 /// hasLoopInvariantBackedgeTakenCount).
310 ///
311 SCEVHandle getBackedgeTakenCount(const Loop *L) const;
312
313 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
314 /// has an analyzable loop-invariant backedge-taken count.
315 bool hasLoopInvariantBackedgeTakenCount(const Loop *L) const;
316
317 /// forgetLoopBackedgeTakenCount - This method should be called by the
309318 /// client when it has changed a loop in a way that may effect
310 /// ScalarEvolution's ability to compute a trip count.
311 void forgetLoopIterationCount(const Loop *L);
319 /// ScalarEvolution's ability to compute a trip count, or if the loop
320 /// is deleted.
321 void forgetLoopBackedgeTakenCount(const Loop *L);
312322
313323 /// deleteValueFromRecords - This method should be called by the
314324 /// client before it removes a Value from the program, to make sure
2626
2727 /// getRange - determine the range for a particular SCEV within a given Loop
2828 ConstantRange LoopVR::getRange(SCEVHandle S, Loop *L, ScalarEvolution &SE) {
29 SCEVHandle T = SE.getIterationCount(L);
29 SCEVHandle T = SE.getBackedgeTakenCount(L);
3030 if (isa(T))
3131 return ConstantRange(cast(S->getType())->getBitWidth(), true);
3232
14181418 ///
14191419 std::map Scalars;
14201420
1421 /// IterationCounts - Cache the iteration count of the loops for this
1422 /// function as they are computed.
1423 std::map IterationCounts;
1421 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
1422 /// this function as they are computed.
1423 std::map BackedgeTakenCounts;
14241424
14251425 /// ConstantEvolutionLoopExitValue - This map contains entries for all of
14261426 /// the PHI instructions that we attempt to compute constant evolutions for.
14631463 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
14641464 SCEV *LHS, SCEV *RHS);
14651465
1466 /// hasLoopInvariantIterationCount - Return true if the specified loop has
1467 /// an analyzable loop-invariant iteration count.
1468 bool hasLoopInvariantIterationCount(const Loop *L);
1469
1470 /// forgetLoopIterationCount - This method should be called by the
1466 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
1467 /// has an analyzable loop-invariant backedge-taken count.
1468 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
1469
1470 /// forgetLoopBackedgeTakenCount - This method should be called by the
14711471 /// client when it has changed a loop in a way that may effect
1472 /// ScalarEvolution's ability to compute a trip count.
1473 void forgetLoopIterationCount(const Loop *L);
1474
1475 /// getIterationCount - If the specified loop has a predictable iteration
1476 /// count, return it. Note that it is not valid to call this method on a
1477 /// loop without a loop-invariant iteration count.
1478 SCEVHandle getIterationCount(const Loop *L);
1472 /// ScalarEvolution's ability to compute a trip count, or if the loop
1473 /// is deleted.
1474 void forgetLoopBackedgeTakenCount(const Loop *L);
1475
1476 /// getBackedgeTakenCount - If the specified loop has a predictable
1477 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
1478 /// object. The backedge-taken count is the number of times the loop header
1479 /// will be branched to from within the loop. This is one less than the
1480 /// trip count of the loop, since it doesn't count the first iteration,
1481 /// when the header is branched to from outside the loop.
1482 ///
1483 /// Note that it is not valid to call this method on a loop without a
1484 /// loop-invariant backedge-taken count (see
1485 /// hasLoopInvariantBackedgeTakenCount).
1486 ///
1487 SCEVHandle getBackedgeTakenCount(const Loop *L);
14791488
14801489 /// deleteValueFromRecords - This method should be called by the
14811490 /// client before it removes a value from the program, to make sure
14991508 const SCEVHandle &SymName,
15001509 const SCEVHandle &NewVal);
15011510
1502 /// ComputeIterationCount - Compute the number of times the specified loop
1503 /// will iterate.
1504 SCEVHandle ComputeIterationCount(const Loop *L);
1505
1506 /// ComputeLoadConstantCompareIterationCount - Given an exit condition of
1507 /// 'icmp op load X, cst', try to see if we can compute the trip count.
1508 SCEVHandle ComputeLoadConstantCompareIterationCount(LoadInst *LI,
1509 Constant *RHS,
1510 const Loop *L,
1511 ICmpInst::Predicate p);
1512
1513 /// ComputeIterationCountExhaustively - If the trip is known to execute a
1514 /// constant number of times (the condition evolves only from constants),
1511 /// ComputeBackedgeTakenCount - Compute the number of times the specified
1512 /// loop will iterate.
1513 SCEVHandle ComputeBackedgeTakenCount(const Loop *L);
1514
1515 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
1516 /// of 'icmp op load X, cst', try to see if we can compute the trip count.
1517 SCEVHandle
1518 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
1519 Constant *RHS,
1520 const Loop *L,
1521 ICmpInst::Predicate p);
1522
1523 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute
1524 /// a constant number of times (the condition evolves only from constants),
15151525 /// try to evaluate a few iterations of the loop until we get the exit
15161526 /// condition gets a value of ExitWhen (true or false). If we cannot
15171527 /// evaluate the trip count of the loop, return UnknownValue.
1518 SCEVHandle ComputeIterationCountExhaustively(const Loop *L, Value *Cond,
1519 bool ExitWhen);
1528 SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond,
1529 bool ExitWhen);
15201530
15211531 /// HowFarToZero - Return the number of times a backedge comparing the
15221532 /// specified value to zero will execute. If not computable, return
15441554 /// in the header of its containing loop, we know the loop executes a
15451555 /// constant number of times, and the PHI node is just a recurrence
15461556 /// involving constants, fold it.
1547 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its,
1557 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
15481558 const Loop *L);
15491559 };
15501560 }
19301940 // Iteration Count Computation Code
19311941 //
19321942
1933 /// getIterationCount - If the specified loop has a predictable iteration
1934 /// count, return it. Note that it is not valid to call this method on a
1935 /// loop without a loop-invariant iteration count.
1936 SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
1937 std::map::iterator I = IterationCounts.find(L);
1938 if (I == IterationCounts.end()) {
1939 SCEVHandle ItCount = ComputeIterationCount(L);
1940 I = IterationCounts.insert(std::make_pair(L, ItCount)).first;
1943 /// getBackedgeTakenCount - If the specified loop has a predictable
1944 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
1945 /// object. The backedge-taken count is the number of times the loop header
1946 /// will be branched to from within the loop. This is one less than the
1947 /// trip count of the loop, since it doesn't count the first iteration,
1948 /// when the header is branched to from outside the loop.
1949 ///
1950 /// Note that it is not valid to call this method on a loop without a
1951 /// loop-invariant backedge-taken count (see
1952 /// hasLoopInvariantBackedgeTakenCount).
1953 ///
1954 SCEVHandle ScalarEvolutionsImpl::getBackedgeTakenCount(const Loop *L) {
1955 std::map::iterator I = BackedgeTakenCounts.find(L);
1956 if (I == BackedgeTakenCounts.end()) {
1957 SCEVHandle ItCount = ComputeBackedgeTakenCount(L);
1958 I = BackedgeTakenCounts.insert(std::make_pair(L, ItCount)).first;
19411959 if (ItCount != UnknownValue) {
19421960 assert(ItCount->isLoopInvariant(L) &&
19431961 "Computed trip count isn't loop invariant for loop!");
19501968 return I->second;
19511969 }
19521970
1953 /// forgetLoopIterationCount - This method should be called by the
1971 /// forgetLoopBackedgeTakenCount - This method should be called by the
19541972 /// client when it has changed a loop in a way that may effect
1955 /// ScalarEvolution's ability to compute a trip count.
1956 void ScalarEvolutionsImpl::forgetLoopIterationCount(const Loop *L) {
1957 IterationCounts.erase(L);
1958 }
1959
1960 /// ComputeIterationCount - Compute the number of times the specified loop
1961 /// will iterate.
1962 SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
1973 /// ScalarEvolution's ability to compute a trip count, or if the loop
1974 /// is deleted.
1975 void ScalarEvolutionsImpl::forgetLoopBackedgeTakenCount(const Loop *L) {
1976 BackedgeTakenCounts.erase(L);
1977 }
1978
1979 /// ComputeBackedgeTakenCount - Compute the number of times the backedge
1980 /// of the specified loop will execute.
1981 SCEVHandle ScalarEvolutionsImpl::ComputeBackedgeTakenCount(const Loop *L) {
19631982 // If the loop has a non-one exit block count, we can't analyze it.
19641983 SmallVector ExitBlocks;
19651984 L->getExitBlocks(ExitBlocks);
20092028 // Note that ICmpInst deals with pointer comparisons too so we must check
20102029 // the type of the operand.
20112030 if (ExitCond == 0 || isa(ExitCond->getOperand(0)->getType()))
2012 return ComputeIterationCountExhaustively(L, ExitBr->getCondition(),
2031 return ComputeBackedgeTakenCountExhaustively(L, ExitBr->getCondition(),
20132032 ExitBr->getSuccessor(0) == ExitBlock);
20142033
20152034 // If the condition was exit on true, convert the condition to exit on false
20232042 if (LoadInst *LI = dyn_cast(ExitCond->getOperand(0)))
20242043 if (Constant *RHS = dyn_cast(ExitCond->getOperand(1))) {
20252044 SCEVHandle ItCnt =
2026 ComputeLoadConstantCompareIterationCount(LI, RHS, L, Cond);
2045 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
20272046 if (!isa(ItCnt)) return ItCnt;
20282047 }
20292048
21062125 }
21072126 default:
21082127 #if 0
2109 cerr << "ComputeIterationCount ";
2128 cerr << "ComputeBackedgeTakenCount ";
21102129 if (ExitCond->getOperand(0)->getType()->isUnsigned())
21112130 cerr << "[unsigned] ";
21122131 cerr << *LHS << " "
21152134 #endif
21162135 break;
21172136 }
2118 return ComputeIterationCountExhaustively(L, ExitCond,
2119 ExitBr->getSuccessor(0) == ExitBlock);
2137 return
2138 ComputeBackedgeTakenCountExhaustively(L, ExitCond,
2139 ExitBr->getSuccessor(0) == ExitBlock);
21202140 }
21212141
21222142 static ConstantInt *
21632183 return Init;
21642184 }
21652185
2166 /// ComputeLoadConstantCompareIterationCount - Given an exit condition of
2167 /// 'icmp op load X, cst', try to see if we can compute the trip count.
2186 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
2187 /// 'icmp op load X, cst', try to see if we can compute the backedge
2188 /// execution count.
21682189 SCEVHandle ScalarEvolutionsImpl::
2169 ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS,
2170 const Loop *L,
2171 ICmpInst::Predicate predicate) {
2190 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, Constant *RHS,
2191 const Loop *L,
2192 ICmpInst::Predicate predicate) {
21722193 if (LI->isVolatile()) return UnknownValue;
21732194
21742195 // Check to see if the loaded pointer is a getelementptr of a global.
23252346 /// constant number of times, and the PHI node is just a recurrence
23262347 /// involving constants, fold it.
23272348 Constant *ScalarEvolutionsImpl::
2328 getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& Its, const Loop *L){
2349 getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, const Loop *L){
23292350 std::map::iterator I =
23302351 ConstantEvolutionLoopExitValue.find(PN);
23312352 if (I != ConstantEvolutionLoopExitValue.end())
23322353 return I->second;
23332354
2334 if (Its.ugt(APInt(Its.getBitWidth(),MaxBruteForceIterations)))
2355 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
23352356 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
23362357
23372358 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
23512372 return RetVal = 0; // Not derived from same PHI.
23522373
23532374 // Execute the loop symbolically to determine the exit value.
2354 if (Its.getActiveBits() >= 32)
2375 if (BEs.getActiveBits() >= 32)
23552376 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
23562377
2357 unsigned NumIterations = Its.getZExtValue(); // must be in range
2378 unsigned NumIterations = BEs.getZExtValue(); // must be in range
23582379 unsigned IterationNum = 0;
23592380 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
23602381 if (IterationNum == NumIterations)
23702391 }
23712392 }
23722393
2373 /// ComputeIterationCountExhaustively - If the trip is known to execute a
2394 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a
23742395 /// constant number of times (the condition evolves only from constants),
23752396 /// try to evaluate a few iterations of the loop until we get the exit
23762397 /// condition gets a value of ExitWhen (true or false). If we cannot
23772398 /// evaluate the trip count of the loop, return UnknownValue.
23782399 SCEVHandle ScalarEvolutionsImpl::
2379 ComputeIterationCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
2400 ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
23802401 PHINode *PN = getConstantEvolvingPHI(Cond, L);
23812402 if (PN == 0) return UnknownValue;
23822403
24392460 if (PHINode *PN = dyn_cast(I))
24402461 if (PN->getParent() == LI->getHeader()) {
24412462 // Okay, there is no closed form solution for the PHI node. Check
2442 // to see if the loop that contains it has a known iteration count.
2443 // If so, we may be able to force computation of the exit value.
2444 SCEVHandle IterationCount = getIterationCount(LI);
2445 if (SCEVConstant *ICC = dyn_cast(IterationCount)) {
2463 // to see if the loop that contains it has a known backedge-taken
2464 // count. If so, we may be able to force computation of the exit
2465 // value.
2466 SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(LI);
2467 if (SCEVConstant *BTCC =
2468 dyn_cast(BackedgeTakenCount)) {
24462469 // Okay, we know how many times the containing loop executes. If
24472470 // this is a constant evolving PHI node, get the final value at
24482471 // the specified iteration number.
24492472 Constant *RV = getConstantEvolutionLoopExitValue(PN,
2450 ICC->getValue()->getValue(),
2473 BTCC->getValue()->getValue(),
24512474 LI);
24522475 if (RV) return SE.getUnknown(RV);
24532476 }
25512574 if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
25522575 // To evaluate this recurrence, we need to know how many times the AddRec
25532576 // loop iterates. Compute this now.
2554 SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
2555 if (IterationCount == UnknownValue) return UnknownValue;
2577 SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
2578 if (BackedgeTakenCount == UnknownValue) return UnknownValue;
25562579
25572580 // Then, evaluate the AddRec.
2558 return AddRec->evaluateAtIteration(IterationCount, SE);
2581 return AddRec->evaluateAtIteration(BackedgeTakenCount, SE);
25592582 }
25602583 return UnknownValue;
25612584 }
31093132 LHS, RHS);
31103133 }
31113134
3112 SCEVHandle ScalarEvolution::getIterationCount(const Loop *L) const {
3113 return ((ScalarEvolutionsImpl*)Impl)->getIterationCount(L);
3114 }
3115
3116 bool ScalarEvolution::hasLoopInvariantIterationCount(const Loop *L) const {
3117 return !isa(getIterationCount(L));
3118 }
3119
3120 void ScalarEvolution::forgetLoopIterationCount(const Loop *L) {
3121 return ((ScalarEvolutionsImpl*)Impl)->forgetLoopIterationCount(L);
3135 SCEVHandle ScalarEvolution::getBackedgeTakenCount(const Loop *L) const {
3136 return ((ScalarEvolutionsImpl*)Impl)->getBackedgeTakenCount(L);
3137 }
3138
3139 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) const {
3140 return !isa(getBackedgeTakenCount(L));
3141 }
3142
3143 void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
3144 return ((ScalarEvolutionsImpl*)Impl)->forgetLoopBackedgeTakenCount(L);
31223145 }
31233146
31243147 SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) const {
31423165 if (ExitBlocks.size() != 1)
31433166 OS << " ";
31443167
3145 if (SE->hasLoopInvariantIterationCount(L)) {
3146 OS << *SE->getIterationCount(L) << " iterations! ";
3168 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
3169 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
31473170 } else {
3148 OS << "Unpredictable iteration count. ";
3171 OS << "Unpredictable backedge-taken count. ";
31493172 }
31503173
31513174 OS << "\n";
9292
9393 void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
9494 SmallPtrSet &DeadInsts);
95 void LinearFunctionTestReplace(Loop *L, SCEVHandle IterationCount,
95 void LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
9696 Value *IndVar,
9797 BasicBlock *ExitingBlock,
9898 BranchInst *BI,
9999 SCEVExpander &Rewriter);
100 void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
100 void RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount);
101101
102102 void DeleteTriviallyDeadInstructions(SmallPtrSet &Insts);
103103
231231 /// SCEV analysis can determine a loop-invariant trip count of the loop, which
232232 /// is actually a much broader range than just linear tests.
233233 void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
234 SCEVHandle IterationCount,
234 SCEVHandle BackedgeTakenCount,
235235 Value *IndVar,
236236 BasicBlock *ExitingBlock,
237237 BranchInst *BI,
240240 // against the preincremented value, otherwise we prefer to compare against
241241 // the post-incremented value.
242242 Value *CmpIndVar;
243 SCEVHandle RHS = BackedgeTakenCount;
243244 if (ExitingBlock == L->getLoopLatch()) {
244 // What ScalarEvolution calls the "iteration count" is actually the
245 // number of times the branch is taken. Add one to get the number
246 // of times the branch is executed. If this addition may overflow,
247 // we have to be more pessimistic and cast the induction variable
248 // before doing the add.
249 SCEVHandle Zero = SE->getIntegerSCEV(0, IterationCount->getType());
245 // Add one to the "backedge-taken" count to get the trip count.
246 // If this addition may overflow, we have to be more pessimistic and
247 // cast the induction variable before doing the add.
248 SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
250249 SCEVHandle N =
251 SE->getAddExpr(IterationCount,
252 SE->getIntegerSCEV(1, IterationCount->getType()));
250 SE->getAddExpr(BackedgeTakenCount,
251 SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
253252 if ((isa(N) && !N->isZero()) ||
254253 SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
255254 // No overflow. Cast the sum.
256 IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
255 RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
257256 } else {
258257 // Potential overflow. Cast before doing the add.
259 IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
260 IndVar->getType());
261 IterationCount =
262 SE->getAddExpr(IterationCount,
263 SE->getIntegerSCEV(1, IndVar->getType()));
258 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
259 IndVar->getType());
260 RHS = SE->getAddExpr(RHS,
261 SE->getIntegerSCEV(1, IndVar->getType()));
264262 }
265263
266 // The IterationCount expression contains the number of times that the
267 // backedge actually branches to the loop header. This is one less than the
268 // number of times the loop executes, so add one to it.
264 // The BackedgeTaken expression contains the number of times that the
265 // backedge branches to the loop header. This is one less than the
266 // number of times the loop executes, so use the incremented indvar.
269267 CmpIndVar = L->getCanonicalInductionVariableIncrement();
270268 } else {
271269 // We have to use the preincremented value...
272 IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
273 IndVar->getType());
270 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
271 IndVar->getType());
274272 CmpIndVar = IndVar;
275273 }
276274
277275 // Expand the code for the iteration count into the preheader of the loop.
278276 BasicBlock *Preheader = L->getLoopPreheader();
279 Value *ExitCnt = Rewriter.expandCodeFor(IterationCount,
277 Value *ExitCnt = Rewriter.expandCodeFor(RHS,
280278 Preheader->getTerminator());
281279
282280 // Insert a new icmp_ne or icmp_eq instruction before the branch.
290288 << " LHS:" << *CmpIndVar // includes a newline
291289 << " op:\t"
292290 << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
293 << " RHS:\t" << *IterationCount << "\n";
291 << " RHS:\t" << *RHS << "\n";
294292
295293 Value *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI);
296294 BI->setCondition(Cond);
303301 /// final value of any expressions that are recurrent in the loop, and
304302 /// substitute the exit values from the loop into any instructions outside of
305303 /// the loop that use the final values of the current expressions.
306 void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
304 void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount) {
307305 BasicBlock *Preheader = L->getLoopPreheader();
308306
309307 // Scan all of the instructions in the loop, looking at those that have
321319 BlockToInsertInto = Preheader;
322320 BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
323321
324 bool HasConstantItCount = isa(IterationCount);
322 bool HasConstantItCount = isa(BackedgeTakenCount);
325323
326324 SmallPtrSet InstructionsToDelete;
327325 std::map ExitValues;
434432 // may not have been able to compute a trip count. Now that we've done some
435433 // re-writing, the trip count may be computable.
436434 if (Changed)
437 SE->forgetLoopIterationCount(L);
435 SE->forgetLoopBackedgeTakenCount(L);
438436
439437 if (!DeadInsts.empty())
440438 DeleteTriviallyDeadInstructions(DeadInsts);
472470 /// variables, return the PHI for this induction variable.
473471 ///
474472 /// TODO: This duplicates a fair amount of ScalarEvolution logic.
475 /// Perhaps this can be merged with ScalarEvolution::getIterationCount
473 /// Perhaps this can be merged with
474 /// ScalarEvolution::getBackedgeTakenCount
476475 /// and/or ScalarEvolution::get{Sign,Zero}ExtendExpr.
477476 ///
478477 static const PHINode *TestOrigIVForWrap(const Loop *L,
621620 // loop into any instructions outside of the loop that use the final values of
622621 // the current expressions.
623622 //
624 SCEVHandle IterationCount = SE->getIterationCount(L);
625 if (!isa(IterationCount))
626 RewriteLoopExitValues(L, IterationCount);
623 SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
624 if (!isa(BackedgeTakenCount))
625 RewriteLoopExitValues(L, BackedgeTakenCount);
627626
628627 // Next, analyze all of the induction variables in the loop, canonicalizing
629628 // auxillary induction variables.
648647 // the set of the types of the other recurrence expressions.
649648 const Type *LargestType = 0;
650649 SmallSetVector SizesToInsert;
651 if (!isa(IterationCount)) {
652 LargestType = IterationCount->getType();
653 SizesToInsert.insert(IterationCount->getType());
650 if (!isa(BackedgeTakenCount)) {
651 LargestType = BackedgeTakenCount->getType();
652 SizesToInsert.insert(BackedgeTakenCount->getType());
654653 }
655654 for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
656655 const PHINode *PN = IndVars[i].first;
681680 bool NoSignedWrap = false;
682681 bool NoUnsignedWrap = false;
683682 const PHINode *OrigControllingPHI = 0;
684 if (!isa(IterationCount) && ExitingBlock)
683 if (!isa(BackedgeTakenCount) && ExitingBlock)
685684 // Can't rewrite non-branch yet.
686685 if (BranchInst *BI = dyn_cast(ExitingBlock->getTerminator())) {
687686 if (Instruction *OrigCond = dyn_cast(BI->getCondition())) {
694693 DeadInsts.insert(OrigCond);
695694 }
696695
697 LinearFunctionTestReplace(L, IterationCount, IndVar,
696 LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
698697 ExitingBlock, BI, Rewriter);
699698 }
700699
189189 // Don't remove loops for which we can't solve the trip count.
190190 // They could be infinite, in which case we'd be changing program behavior.
191191 ScalarEvolution& SE = getAnalysis();
192 SCEVHandle S = SE.getIterationCount(L);
192 SCEVHandle S = SE.getBackedgeTakenCount(L);
193193 if (isa(S))
194194 return false;
195195
266266 (*LI)->eraseFromParent();
267267
268268 // Tell ScalarEvolution that the loop is deleted.
269 SE.forgetLoopIterationCount(L);
269 SE.forgetLoopBackedgeTakenCount(L);
270270
271271 // Finally, the blocks from loopinfo. This has to happen late because
272272 // otherwise our loop iterators won't work.
23502350 SelectInst *Sel = dyn_cast(Cond->getOperand(1));
23512351 if (!Sel || !Sel->hasOneUse()) return Cond;
23522352
2353 SCEVHandle IterationCount = SE->getIterationCount(L);
2354 if (isa(IterationCount))
2353 SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
2354 if (isa(BackedgeTakenCount))
23552355 return Cond;
2356 SCEVHandle One = SE->getIntegerSCEV(1, IterationCount->getType());
2357
2358 // Adjust for an annoying getIterationCount quirk.
2359 IterationCount = SE->getAddExpr(IterationCount, One);
2356 SCEVHandle One = SE->getIntegerSCEV(1, BackedgeTakenCount->getType());
2357
2358 // Add one to the backedge-taken count to get the trip count.
2359 SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One);
23602360
23612361 // Check for a max calculation that matches the pattern.
23622362 SCEVSMaxExpr *SMax = dyn_cast(IterationCount);
24112411 /// inside the loop then try to eliminate the cast opeation.
24122412 void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
24132413
2414 SCEVHandle IterationCount = SE->getIterationCount(L);
2415 if (isa(IterationCount))
2414 SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
2415 if (isa(BackedgeTakenCount))
24162416 return;
24172417
24182418 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e;
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {Loop bb: 100 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {Loop bb: backedge-taken count is 100}
22 ; PR1533
33
44 @array = weak global [101 x i32] zeroinitializer, align 32 ; <[100 x i32]*> [#uses=1]
None ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb: (-1 + (-1 \\* %x) + %y) iterations!}
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb: backedge-taken count is (-1 + (-1 \\* %x) + %y)}
11 ; PR1597
22
33 define i32 @f(i32 %x, i32 %y) {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {13 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 13}
22 ; PR1706
33
44 define i32 @f() {
None ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop header: (0 smax %n) iterations!}
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop header: backedge-taken count is (0 smax %n)}
11
22 define void @foo(i32 %n) {
33 entry:
None ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop loop: (100 + (-100 smax %n)) iterations!}
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop loop: backedge-taken count is (100 + (-100 smax %n))}
11 ; PR2002
22
33 define void @foo(i8 %n) {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {61 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 61}
22 ; PR2364
33
44 define i32 @func_6() nounwind {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {113 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 113}
22 ; PR2088
33
44 define void @fun() {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
11 ; RUN: -scalar-evolution-max-iterations=0 | \
2 ; RUN: grep -F "(-1 + (-1 * %j)) iterations"
2 ; RUN: grep -F "backedge-taken count is (-1 + (-1 * %j))"
33 ; PR2607
44
55 define i32 @_Z1aj(i32 %j) nounwind {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
11 ; RUN: -scalar-evolution-max-iterations=0 | \
2 ; RUN: grep -F "(-2147483632 + (2147483632 smax (-1 + (-1 * %x)) smax (-1 + (-1 * %y)))) iterations"
2 ; RUN: grep -F "backedge-taken count is (-2147483632 + (2147483632 smax (-1 + (-1 * %x)) smax (-1 + (-1 * %y))))"
33 ; PR2607
44
55 define i32 @b(i32 %x, i32 %y) {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
1 ; RUN: grep {Loop bb: (7 + (-1 \\* %argc)) iterations!}
1 ; RUN: grep {Loop bb: backedge-taken count is (7 + (-1 \\* %argc))}
22 ; XFAIL: *
33
44 define i32 @main(i32 %argc, i8** %argv) nounwind {
None ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {255 iterations}
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {backedge-taken count is 255}
11 ; XFAIL: *
22
33 define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind {
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {100 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 100}
22 ; PR1101
33
44 @A = weak global [1000 x i32] zeroinitializer, align 32
None ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb3: (-1 + %n) iterations!}
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb3: backedge-taken count is (-1 + %n)}
11
22 ; We don't want to use a max in the trip count expression in
33 ; this testcase.
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution \
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {10000 iterations}
1 ; RUN: -scalar-evolution-max-iterations=0 | grep {backedge-taken count is 10000}
22 ; PR1101
33
44 @A = weak global [1000 x i32] zeroinitializer, align 32
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | \
1 ; RUN: grep {4 iterations}
1 ; RUN: grep {backedge-taken count is 4}
22 ; PR1101
33
44 @A = weak global [1000 x i32] zeroinitializer, align 32