Skip to content

Commit 3fcfce4

Browse files
committed
Reapply "[LoopPeel] Implement initial peeling off the last loop iteration. (#139551)"
This reverts the revert commit bf92b12. This adds missing initialization of PeelLast in gatherPeelingPreferences. Original message: Generalize countToEliminateCompares to also consider peeling off the last iteration if it eliminates a compare. At the moment, codegen for peeling off the last iteration is quite restrictive and callers have to make sure that the exit condition can be adjusted when peeling and that the loop executes at least 2 iterations. Both will be relaxed in follow-ups. PR: #139551
1 parent 676e660 commit 3fcfce4

File tree

6 files changed

+428
-156
lines changed

6 files changed

+428
-156
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@ class TargetTransformInfo {
680680
/// If the value is true the peeling cost model can decide to peel only
681681
/// some iterations and in this case it will set this to false.
682682
bool PeelProfiledIterations;
683+
684+
/// Peel off the last PeelCount loop iterations.
685+
bool PeelLast;
683686
};
684687

685688
/// Get target-customized preferences for the generic loop peeling

llvm/include/llvm/Transforms/Utils/LoopPeel.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ namespace llvm {
2121

2222
bool canPeel(const Loop *L);
2323

24+
/// Returns true if the last iteration of \p L can be peeled off. It makes sure
25+
/// the loop exit condition can be adjusted when peeling and that the loop
26+
/// executes at least 2 iterations.
27+
bool canPeelLastIteration(const Loop &L, ScalarEvolution &SE);
28+
2429
/// VMap is the value-map that maps instructions from the original loop to
25-
/// instructions in the last peeled-off iteration.
26-
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
27-
DominatorTree &DT, AssumptionCache *AC, bool PreserveLCSSA,
28-
ValueToValueMapTy &VMap);
30+
/// instructions in the last peeled-off iteration. If \p PeelLast is true, peel
31+
/// off the last \p PeelCount iterations from \p L (canPeelLastIteration must be
32+
/// true for \p L), otherwise peel off the first \p PeelCount iterations.
33+
bool peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
34+
ScalarEvolution *SE, DominatorTree &DT, AssumptionCache *AC,
35+
bool PreserveLCSSA, ValueToValueMapTy &VMap);
2936

3037
TargetTransformInfo::PeelingPreferences
3138
gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,

llvm/lib/Transforms/Scalar/LoopFuse.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ struct LoopFuser {
790790
<< " iterations of the first loop. \n");
791791

792792
ValueToValueMapTy VMap;
793-
FC0.Peeled = peelLoop(FC0.L, PeelCount, &LI, &SE, DT, &AC, true, VMap);
793+
FC0.Peeled =
794+
peelLoop(FC0.L, PeelCount, false, &LI, &SE, DT, &AC, true, VMap);
794795
if (FC0.Peeled) {
795796
LLVM_DEBUG(dbgs() << "Done Peeling\n");
796797

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
13141314
});
13151315

13161316
ValueToValueMapTy VMap;
1317-
if (peelLoop(L, PP.PeelCount, LI, &SE, DT, &AC, PreserveLCSSA, VMap)) {
1317+
if (peelLoop(L, PP.PeelCount, PP.PeelLast, LI, &SE, DT, &AC, PreserveLCSSA,
1318+
VMap)) {
13181319
simplifyLoopAfterUnroll(L, true, LI, &SE, &DT, &AC, &TTI, nullptr);
13191320
// If the loop was peeled, we already "used up" the profile information
13201321
// we had, so we don't want to unroll or peel again.

0 commit comments

Comments
 (0)