[llvm] [LoopPeel] Use loop guards when checking if last iter can be peeled. (PR #142605)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 3 13:59:07 PDT 2025


================
@@ -484,8 +480,19 @@ countToEliminateCompares(Loop &L, unsigned MaxPeelCount, ScalarEvolution &SE,
     const SCEV *Step = LeftAR->getStepRecurrence(SE);
     if (!PeelWhilePredicateIsKnown(NewPeelCount, IterVal, RightSCEV, Step,
                                    Pred)) {
-      if (shouldPeelLastIteration(L, Pred, LeftAR, RightSCEV, SE, TTI))
+      if (!canPeelLastIteration(L, SE))
+        return;
+
+      const SCEV *BTC = SE.getBackedgeTakenCount(&L);
+      auto Guards = ScalarEvolution::LoopGuards::collect(&L, SE);
+      if (shouldPeelLastIteration(L, Pred, LeftAR,
+                                  SE.applyLoopGuards(RightSCEV, Guards),
+                                  SE.applyLoopGuards(BTC, Guards), SE, TTI))
         DesiredPeelCountLast = 1;
+      else
+        assert(!shouldPeelLastIteration(L, Pred, LeftAR, RightSCEV, BTC, SE,
+                                        TTI) &&
+               "loop guards pessimized result");
----------------
fhahn wrote:

Yeah fair enough, I removed it. Still wondering if there's a nice way to have some kind of verification as opt-in?

https://github.com/llvm/llvm-project/pull/142605


More information about the llvm-commits mailing list