@@ -330,10 +330,6 @@ static unsigned peelToTurnInvariantLoadsDerefencebale(Loop &L,
330330
331331bool  llvm::canPeelLastIteration (const  Loop &L, ScalarEvolution &SE) {
332332  const  SCEV *BTC = SE.getBackedgeTakenCount (&L);
333-   Value *Inc;
334-   CmpPredicate Pred;
335-   BasicBlock *Succ1;
336-   BasicBlock *Succ2;
337333  //  The loop must execute at least 2 iterations to guarantee that peeled
338334  //  iteration executes.
339335  //  TODO: Add checks during codegen.
@@ -347,12 +343,18 @@ bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
347343  //  * the exit condition must be a NE/EQ compare of an induction with step
348344  //  of 1 and must only be used by the exiting branch.
349345  BasicBlock *Latch = L.getLoopLatch ();
346+   Value *Inc;
347+   Value *Bound;
348+   CmpPredicate Pred;
349+   BasicBlock *Succ1;
350+   BasicBlock *Succ2;
350351  return  Latch && Latch == L.getExitingBlock () &&
351352         match (Latch->getTerminator (),
352-                m_Br (m_OneUse (m_ICmp (Pred, m_Value (Inc), m_Value ())),
353+                m_Br (m_OneUse (m_ICmp (Pred, m_Value (Inc), m_Value (Bound ))),
353354                    m_BasicBlock (Succ1), m_BasicBlock (Succ2))) &&
354355         ((Pred == CmpInst::ICMP_EQ && Succ2 == L.getHeader ()) ||
355356          (Pred == CmpInst::ICMP_NE && Succ1 == L.getHeader ())) &&
357+          SE.isLoopInvariant (SE.getSCEV (Bound), &L) &&
356358         match (SE.getSCEV (Inc),
357359               m_scev_AffineAddRec (m_SCEV (), m_scev_One (), m_SpecificLoop (&L)));
358360}
0 commit comments