Index: include/llvm/Analysis/ScalarEvolution.h =================================================================== --- include/llvm/Analysis/ScalarEvolution.h +++ include/llvm/Analysis/ScalarEvolution.h @@ -726,6 +726,11 @@ unsigned getSmallConstantTripMultiple(const Loop *L, BasicBlock *ExitingBlock); + /// Returns the upper bound of the loop trip count as a normal unsigned + /// value. The upper bound may be larger than the actual trip count. + /// Returns 0 if the trip count is unknown or cannot be estimated. + unsigned getSmallConstantTripCountUpperBound(const Loop *L); + /// Get the expression for the number of loop iterations for which this loop /// is guaranteed not to exit via ExitingBlock. Otherwise return /// SCEVCouldNotCompute. Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -6390,6 +6390,27 @@ return getConstantTripCount(MaxExitCount); } +unsigned ScalarEvolution::getSmallConstantTripCountUpperBound(const Loop *L) { + if (const SCEVConstant *TakenCount = + dyn_cast(getBackedgeTakenCount(L))) { + if (unsigned C = getConstantTripCount(TakenCount)) + return C; + } + + if (unsigned C = getSmallConstantMaxTripCount(L)) + return C; + + SCEVUnionPredicate Ps; + const SCEV *PredBEC = getPredicatedBackedgeTakenCount(L, Ps); + if (PredBEC != getCouldNotCompute()) { + APInt S = getUnsignedRange(PredBEC).getSetSize(); + if (S.getActiveBits() < 32) + return S.getZExtValue(); + } + + return 0; +} + unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) { if (BasicBlock *ExitingBB = L->getExitingBlock()) return getSmallConstantTripMultiple(L, ExitingBB); Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -5148,7 +5148,7 @@ return 1; // Do not interleave loops with a relatively small trip count. - unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop); + unsigned TC = PSE.getSE()->getSmallConstantTripCountUpperBound(TheLoop); if (TC > 1 && TC < TinyTripCountInterleaveThreshold) return 1; @@ -7346,7 +7346,7 @@ } } if (!HasExpectedTC) { - ExpectedTC = SE->getSmallConstantMaxTripCount(L); + ExpectedTC = SE->getSmallConstantTripCountUpperBound(L); HasExpectedTC = (ExpectedTC > 0); }