if (LI->getLoopFor(BB)) continue;
bool BlockUnreachable = false;
- TerminatorInst *TI = BB->getTerminator();
// Check to see if any successors of this block are non-loop-header loops
// that are not the header.
- for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
+ for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
// If this successor is not in a loop, BB is clearly ok.
- Loop *L = LI->getLoopFor(TI->getSuccessor(i));
+ Loop *L = LI->getLoopFor(*I);
if (!L) continue;
// If the succ is the loop header, and if L is a top-level loop, then this
// is an entrance into a loop through the header, which is also ok.
- if (L->getHeader() == TI->getSuccessor(i) && L->getParentLoop() == 0)
+ if (L->getHeader() == *I && L->getParentLoop() == 0)
continue;
// Otherwise, this is an entrance into a loop from some place invalid.
// loop by replacing the terminator.
// Remove PHI entries from the successors.
- for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
- TI->getSuccessor(i)->removePredecessor(BB);
+ for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
+ (*I)->removePredecessor(BB);
// Add a new unreachable instruction before the old terminator.
+ TerminatorInst *TI = BB->getTerminator();
new UnreachableInst(TI);
// Delete the dead terminator.
const std::vector<BasicBlock*> &Preds) {
// Create new basic block, insert right before the original block...
- BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB->getParent(), BB);
+ BasicBlock *NewBB = BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB);
// The preheader first gets an unconditional branch to the loop header...
- BranchInst *BI = new BranchInst(BB, NewBB);
+ BranchInst *BI = BranchInst::Create(BB, NewBB);
// For every PHI node in the block, insert a PHI node into NewBB where the
// incoming values from the out of loop edges are moved to NewBB. We have two
// If the values coming into the block are not the same, we need a PHI.
if (InVal == 0) {
// Create the new PHI node, insert it into NewBB at the end of the block
- PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI);
+ PHINode *NewPHI = PHINode::Create(PN->getType(), PN->getName()+".ph", BI);
if (AA) AA->copyValue(PN, NewPHI);
// Move all of the edges from blocks outside the loop to the new PHI
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s)
if (TI->getSuccessor(s) == BB)
TI->setSuccessor(s, NewBB);
+
+ if (Preds[i]->getUnwindDest() == BB)
+ Preds[i]->setUnwindDest(NewBB);
}
} else { // Otherwise the loop is dead...
if (*I != Preheader) BackedgeBlocks.push_back(*I);
// Create and insert the new backedge block...
- BasicBlock *BEBlock = new BasicBlock(Header->getName()+".backedge", F);
- BranchInst *BETerminator = new BranchInst(Header, BEBlock);
+ BasicBlock *BEBlock = BasicBlock::Create(Header->getName()+".backedge", F);
+ BranchInst *BETerminator = BranchInst::Create(Header, BEBlock);
// Move the new backedge block to right after the last backedge block.
Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos;
// the backedge block which correspond to any PHI nodes in the header block.
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I);
- PHINode *NewPN = new PHINode(PN->getType(), PN->getName()+".be",
- BETerminator);
+ PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".be",
+ BETerminator);
NewPN->reserveOperandSpace(BackedgeBlocks.size());
if (AA) AA->copyValue(PN, NewPN);
}
// Now that all of the PHI nodes have been inserted and adjusted, modify the
- // backedge blocks to just to the BEBlock instead of the header.
+ // backedge blocks to branch to the BEBlock instead of the header.
for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
if (TI->getSuccessor(Op) == Header)
TI->setSuccessor(Op, BEBlock);
+
+ if (BackedgeBlocks[i]->getUnwindDest() == Header)
+ BackedgeBlocks[i]->setUnwindDest(BEBlock);
}
//===--- Update all analyses which we must preserve now -----------------===//