Index: include/llvm/Transforms/Utils/LoopUtils.h =================================================================== --- include/llvm/Transforms/Utils/LoopUtils.h +++ include/llvm/Transforms/Utils/LoopUtils.h @@ -461,10 +461,10 @@ void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V = 0); -/// \brief Get a loop's estimated trip count based on branch weight metadata. -/// Returns 0 when the count is estimated to be 0, or None when a meaningful -/// estimate can not be made. -Optional getLoopEstimatedTripCount(Loop *L); +/// \brief Get a loop's estimated BE taken count based on branch weight +/// metadata. Returns 0 when the count is estimated to be 0, or None when a +/// meaningful estimate can not be made. +Optional getLoopEstimatedBackedgeTakenCount(Loop *L); /// Helper to consistently add the set of standard passes to a loop pass's \c /// AnalysisUsage. Index: lib/Transforms/Scalar/LoopUnrollPass.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnrollPass.cpp +++ lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -850,8 +850,10 @@ // Check if the runtime trip count is too small when profile is available. if (L->getHeader()->getParent()->getEntryCount()) { - if (auto ProfileTripCount = getLoopEstimatedTripCount(L)) { - if (*ProfileTripCount < FlatLoopTripCountThreshold) + auto ProfileBETakenCount = getLoopEstimatedBackedgeTakenCount(L); + if (ProfileBETakenCount) { + // Trip count == BE taken count + 1. + if (*ProfileBETakenCount + 1 < FlatLoopTripCountThreshold) return false; else UP.AllowExpensiveTripCount = true; Index: lib/Transforms/Utils/LoopUnrollPeel.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollPeel.cpp +++ lib/Transforms/Utils/LoopUnrollPeel.cpp @@ -84,7 +84,9 @@ // We only do this in the presence of profile information, since otherwise // our estimates of the trip count are not reliable enough. if (UP.AllowPeeling && L->getHeader()->getParent()->getEntryCount()) { - Optional PeelCount = getLoopEstimatedTripCount(L); + // We only peel backedge taken count # of times as the original loop + // body will become the last iteration. + Optional PeelCount = getLoopEstimatedBackedgeTakenCount(L); if (!PeelCount) return; Index: lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- lib/Transforms/Utils/LoopUtils.cpp +++ lib/Transforms/Utils/LoopUtils.cpp @@ -1068,7 +1068,7 @@ return true; } -Optional llvm::getLoopEstimatedTripCount(Loop *L) { +Optional llvm::getLoopEstimatedBackedgeTakenCount(Loop *L) { // Only support loops with a unique exiting block, and a latch. if (!L->getExitingBlock()) return None;