@@ -330,10 +330,6 @@ static unsigned peelToTurnInvariantLoadsDerefencebale(Loop &L,
330
330
331
331
bool llvm::canPeelLastIteration (const Loop &L, ScalarEvolution &SE) {
332
332
const SCEV *BTC = SE.getBackedgeTakenCount (&L);
333
- Value *Inc;
334
- CmpPredicate Pred;
335
- BasicBlock *Succ1;
336
- BasicBlock *Succ2;
337
333
// The loop must execute at least 2 iterations to guarantee that peeled
338
334
// iteration executes.
339
335
// TODO: Add checks during codegen.
@@ -347,12 +343,18 @@ bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
347
343
// * the exit condition must be a NE/EQ compare of an induction with step
348
344
// of 1 and must only be used by the exiting branch.
349
345
BasicBlock *Latch = L.getLoopLatch ();
346
+ Value *Inc;
347
+ Value *Bound;
348
+ CmpPredicate Pred;
349
+ BasicBlock *Succ1;
350
+ BasicBlock *Succ2;
350
351
return Latch && Latch == L.getExitingBlock () &&
351
352
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 ))),
353
354
m_BasicBlock (Succ1), m_BasicBlock (Succ2))) &&
354
355
((Pred == CmpInst::ICMP_EQ && Succ2 == L.getHeader ()) ||
355
356
(Pred == CmpInst::ICMP_NE && Succ1 == L.getHeader ())) &&
357
+ SE.isLoopInvariant (SE.getSCEV (Bound), &L) &&
356
358
match (SE.getSCEV (Inc),
357
359
m_scev_AffineAddRec (m_SCEV (), m_scev_One (), m_SpecificLoop (&L)));
358
360
}
0 commit comments