diff --git a/include/llvm/Analysis/ScalarEvolution.h b/include/llvm/Analysis/ScalarEvolution.h index 7aa540a5e40..125f034fc21 100644 --- a/include/llvm/Analysis/ScalarEvolution.h +++ b/include/llvm/Analysis/ScalarEvolution.h @@ -705,6 +705,13 @@ public: bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS); + /// Convert from an "exit count" (i.e. "backedge taken count") to a "trip + /// count". A "trip count" is the number of times the header of the loop + /// will execute if an exit is taken after the specified number of backedges + /// have been taken. (e.g. TripCount = ExitCount + 1) A zero result + /// must be interpreted as a loop having an unknown trip count. + const SCEV *getTripCountFromExitCount(const SCEV *ExitCount); + /// Returns the maximum trip count of the loop if it is a single-exit /// loop and we can compute a small maximum for that loop. /// diff --git a/lib/Analysis/LoopCacheAnalysis.cpp b/lib/Analysis/LoopCacheAnalysis.cpp index cf68596bfbc..8a613647bbe 100644 --- a/lib/Analysis/LoopCacheAnalysis.cpp +++ b/lib/Analysis/LoopCacheAnalysis.cpp @@ -109,9 +109,7 @@ static const SCEV *computeTripCount(const Loop &L, ScalarEvolution &SE) { if (isa(BackedgeTakenCount) || !isa(BackedgeTakenCount)) return nullptr; - - return SE.getAddExpr(BackedgeTakenCount, - SE.getOne(BackedgeTakenCount->getType())); + return SE.getTripCountFromExitCount(BackedgeTakenCount); } //===----------------------------------------------------------------------===// diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 5a2a4b8ddfe..86630513726 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -6927,6 +6927,12 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { // Iteration Count Computation Code // +const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount) { + // Get the trip count from the BE count by adding 1. Overflow, results + // in zero which means "unknown". + return getAddExpr(ExitCount, getOne(ExitCount->getType())); +} + static unsigned getConstantTripCount(const SCEVConstant *ExitCount) { if (!ExitCount) return 0; @@ -6979,8 +6985,8 @@ unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, if (ExitCount == getCouldNotCompute()) return 1; - // Get the trip count from the BE count by adding 1. - const SCEV *TCExpr = getAddExpr(ExitCount, getOne(ExitCount->getType())); + // Get the trip count + const SCEV *TCExpr = getTripCountFromExitCount(ExitCount); const SCEVConstant *TC = dyn_cast(TCExpr); if (!TC)