aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/LoopPeel.cpp
diff options
context:
space:
mode:
authorRamkumar Ramachandra <ramkumar.ramachandra@codasip.com>2025-03-04 08:43:08 +0000
committerGitHub <noreply@github.com>2025-03-04 08:43:08 +0000
commit80bdfcd411cd8197b0a8b6139b89a87d3a4528fa (patch)
treea24c9065fa4d698a257bf1228c58733e6e625eaa /llvm/lib/Transforms/Utils/LoopPeel.cpp
parent23a30e68888e764b2f4d32e51d415b50fa5f5cac (diff)
downloadllvm-80bdfcd411cd8197b0a8b6139b89a87d3a4528fa.zip
llvm-80bdfcd411cd8197b0a8b6139b89a87d3a4528fa.tar.gz
llvm-80bdfcd411cd8197b0a8b6139b89a87d3a4528fa.tar.bz2
[LoopUtils] Don't wrap in getLoopEstimatedTripCount (#129080)
getLoopEstimatedTripCount returns the trip count based on profiling data, and its documentation says that it could return 0 when the trip count is zero, but this is not the case: a valid trip count can never be zero, and it returns 0 when the unsigned ExitCount is incremented by 1 and wraps. Some callers are careful about checking for this zero value in an std::optional, but it makes for an API with footguns, as a std::optional return value indicates that a non-nullopt value would be a valid trip count. Fix this by explicitly returning std::nullopt when the return value would wrap, and strip additional checks in callers. This also fixes a minor bug in LoopVectorize.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopPeel.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/LoopPeel.cpp24
1 files changed, 11 insertions, 13 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopPeel.cpp b/llvm/lib/Transforms/Utils/LoopPeel.cpp
index 9a24c1b..0f3a92b 100644
--- a/llvm/lib/Transforms/Utils/LoopPeel.cpp
+++ b/llvm/lib/Transforms/Utils/LoopPeel.cpp
@@ -640,20 +640,18 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
LLVM_DEBUG(dbgs() << "Profile-based estimated trip count is "
<< *EstimatedTripCount << "\n");
- if (*EstimatedTripCount) {
- if (*EstimatedTripCount + AlreadyPeeled <= MaxPeelCount) {
- unsigned PeelCount = *EstimatedTripCount;
- LLVM_DEBUG(dbgs() << "Peeling first " << PeelCount << " iterations.\n");
- PP.PeelCount = PeelCount;
- return;
- }
- LLVM_DEBUG(dbgs() << "Already peel count: " << AlreadyPeeled << "\n");
- LLVM_DEBUG(dbgs() << "Max peel count: " << UnrollPeelMaxCount << "\n");
- LLVM_DEBUG(dbgs() << "Loop cost: " << LoopSize << "\n");
- LLVM_DEBUG(dbgs() << "Max peel cost: " << Threshold << "\n");
- LLVM_DEBUG(dbgs() << "Max peel count by cost: "
- << (Threshold / LoopSize - 1) << "\n");
+ if (*EstimatedTripCount + AlreadyPeeled <= MaxPeelCount) {
+ unsigned PeelCount = *EstimatedTripCount;
+ LLVM_DEBUG(dbgs() << "Peeling first " << PeelCount << " iterations.\n");
+ PP.PeelCount = PeelCount;
+ return;
}
+ LLVM_DEBUG(dbgs() << "Already peel count: " << AlreadyPeeled << "\n");
+ LLVM_DEBUG(dbgs() << "Max peel count: " << UnrollPeelMaxCount << "\n");
+ LLVM_DEBUG(dbgs() << "Loop cost: " << LoopSize << "\n");
+ LLVM_DEBUG(dbgs() << "Max peel cost: " << Threshold << "\n");
+ LLVM_DEBUG(dbgs() << "Max peel count by cost: "
+ << (Threshold / LoopSize - 1) << "\n");
}
}