[llvm] 364d80e - [LoopPeel] Make sure bound in exit condition is loop invariant.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sun May 25 11:22:11 PDT 2025


Author: Florian Hahn
Date: 2025-05-25T19:21:49+01:00
New Revision: 364d80e5c52db2b001dc2807fb78b4e0df397f55

URL: https://github.com/llvm/llvm-project/commit/364d80e5c52db2b001dc2807fb78b4e0df397f55
DIFF: https://github.com/llvm/llvm-project/commit/364d80e5c52db2b001dc2807fb78b4e0df397f55.diff

LOG: [LoopPeel] Make sure bound in exit condition is loop invariant.

Follow-up to post-commit comment for
(https://github.com/llvm/llvm-project/pull/139551.

This should effectively be NFC, given the other existing restrictions.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/LoopPeel.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/LoopPeel.cpp b/llvm/lib/Transforms/Utils/LoopPeel.cpp
index 52ce7af4252be..f348d24ec24fb 100644
--- a/llvm/lib/Transforms/Utils/LoopPeel.cpp
+++ b/llvm/lib/Transforms/Utils/LoopPeel.cpp
@@ -330,10 +330,6 @@ static unsigned peelToTurnInvariantLoadsDerefencebale(Loop &L,
 
 bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
   const SCEV *BTC = SE.getBackedgeTakenCount(&L);
-  Value *Inc;
-  CmpPredicate Pred;
-  BasicBlock *Succ1;
-  BasicBlock *Succ2;
   // The loop must execute at least 2 iterations to guarantee that peeled
   // iteration executes.
   // TODO: Add checks during codegen.
@@ -347,12 +343,18 @@ bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
   // * the exit condition must be a NE/EQ compare of an induction with step
   // of 1 and must only be used by the exiting branch.
   BasicBlock *Latch = L.getLoopLatch();
+  Value *Inc;
+  Value *Bound;
+  CmpPredicate Pred;
+  BasicBlock *Succ1;
+  BasicBlock *Succ2;
   return Latch && Latch == L.getExitingBlock() &&
          match(Latch->getTerminator(),
-               m_Br(m_OneUse(m_ICmp(Pred, m_Value(Inc), m_Value())),
+               m_Br(m_OneUse(m_ICmp(Pred, m_Value(Inc), m_Value(Bound))),
                     m_BasicBlock(Succ1), m_BasicBlock(Succ2))) &&
          ((Pred == CmpInst::ICMP_EQ && Succ2 == L.getHeader()) ||
           (Pred == CmpInst::ICMP_NE && Succ1 == L.getHeader())) &&
+         SE.isLoopInvariant(SE.getSCEV(Bound), &L) &&
          match(SE.getSCEV(Inc),
                m_scev_AffineAddRec(m_SCEV(), m_scev_One(), m_SpecificLoop(&L)));
 }


        


More information about the llvm-commits mailing list