[llvm] [LoopPeel] Fix BFI when peeling last iteration without guard (PR #168250)
Joel E. Denny via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 19 10:03:44 PST 2025
================
@@ -1183,8 +1183,33 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
// If the original loop may only execute a single iteration we need to
// insert a trip count check and skip the original loop with the last
- // iteration peeled off if necessary.
- if (!SE->isKnownNonZero(BTC)) {
+ // iteration peeled off if necessary. Either way, we must update branch
+ // weights to maintain the loop body frequency.
+ if (SE->isKnownNonZero(BTC)) {
+ // We have just proven that, when reached, the original loop always
+ // executes at least two iterations. Thus, we unconditionally execute
+ // both the remaining loop's initial iteration and the peeled iteration.
+ // But that increases the latter's frequency above its frequency in the
+ // original loop. To maintain the total frequency, we compensate by
+ // decreasing the remaining loop body's frequency to indicate one less
+ // iteration.
+ //
+ // We use this formula to convert probability to/from frequency:
+ // Sum(i=0..inf)(P^i) = 1/(1-P) = Freq.
+ if (BranchProbability P = getLoopProbability(L); !P.isUnknown()) {
+ // Trying to subtract one from an infinite loop is pointless, and our
+ // formulas then produce division by zero, so skip that case.
+ if (BranchProbability ExitP = P.getCompl(); !ExitP.isZero()) {
----------------
jdenny-ornl wrote:
> why would exitP be 0?
An always infinite loop has P=1 and so ExitP=0. The new test case gives an example.
> oh, maybe message pumps...
I'm not sure what this means.
https://github.com/llvm/llvm-project/pull/168250
More information about the llvm-commits
mailing list