Fix SCEVExpander's canonical addrec expansion code to work on loops that

aren't in canonical loop-simplify form, since it doesn't itself depend
on LoopSimplify. This means handling loops without preheaders and loops
with multiple backedges.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82905 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-09-27 17:46:40 +00:00
parent 22bf8f21b4
commit 83d577490b

View File

@ -680,29 +680,22 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
// Create and insert the PHI node for the induction variable in the // Create and insert the PHI node for the induction variable in the
// specified loop. // specified loop.
BasicBlock *Header = L->getHeader(); BasicBlock *Header = L->getHeader();
BasicBlock *Preheader = L->getLoopPreheader();
PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
InsertedValues.insert(PN); InsertedValues.insert(PN);
PN->addIncoming(Constant::getNullValue(Ty), Preheader);
pred_iterator HPI = pred_begin(Header);
assert(HPI != pred_end(Header) && "Loop with zero preds???");
if (!L->contains(*HPI)) ++HPI;
assert(HPI != pred_end(Header) && L->contains(*HPI) &&
"No backedge in loop?");
// Insert a unit add instruction right before the terminator corresponding
// to the back-edge.
Constant *One = ConstantInt::get(Ty, 1); Constant *One = ConstantInt::get(Ty, 1);
Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", for (pred_iterator HPI = pred_begin(Header), HPE = pred_end(Header);
(*HPI)->getTerminator()); HPI != HPE; ++HPI)
InsertedValues.insert(Add); if (L->contains(*HPI)) {
// Insert a unit add instruction right before the terminator corresponding
pred_iterator PI = pred_begin(Header); // to the back-edge.
if (*PI == Preheader) Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
++PI; (*HPI)->getTerminator());
PN->addIncoming(Add, *PI); InsertedValues.insert(Add);
return PN; PN->addIncoming(Add, *HPI);
} else {
PN->addIncoming(Constant::getNullValue(Ty), *HPI);
}
} }
// {0,+,F} --> {0,+,1} * F // {0,+,F} --> {0,+,1} * F