Change the ExitBlocks list from being explicitly contained in the Loop

structure to being dynamically computed on demand.  This makes updating
loop information MUCH easier.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13045 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2004-04-18 22:14:10 +00:00
parent 7c8781e71f
commit f1ab4b4eac
7 changed files with 45 additions and 184 deletions

View File

@ -92,8 +92,10 @@ bool LoopExtractor::runOnFunction(Function &F) {
else {
// Check to see if any exits from the loop are more than just return
// blocks.
for (unsigned i = 0, e = TLL->getExitBlocks().size(); i != e; ++i)
if (!isa<ReturnInst>(TLL->getExitBlocks()[i]->getTerminator())) {
std::vector<BasicBlock*> ExitBlocks;
TLL->getExitBlocks(ExitBlocks);
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
ShouldExtractLoop = true;
break;
}

View File

@ -185,8 +185,10 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
ScalarEvolutionRewriter &RW) {
// Find the exit block for the loop. We can currently only handle loops with
// a single exit.
if (L->getExitBlocks().size() != 1) return;
BasicBlock *ExitBlock = L->getExitBlocks()[0];
std::vector<BasicBlock*> ExitBlocks;
L->getExitBlocks(ExitBlocks);
if (ExitBlocks.size() != 1) return;
BasicBlock *ExitBlock = ExitBlocks[0];
// Make sure there is only one predecessor block in the loop.
BasicBlock *ExitingBlock = 0;
@ -269,8 +271,10 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L) {
// We insert the code into the preheader of the loop if the loop contains
// multiple exit blocks, or in the exit block if there is exactly one.
BasicBlock *BlockToInsertInto;
if (L->getExitBlocks().size() == 1)
BlockToInsertInto = L->getExitBlocks()[0];
std::vector<BasicBlock*> ExitBlocks;
L->getExitBlocks(ExitBlocks);
if (ExitBlocks.size() == 1)
BlockToInsertInto = ExitBlocks[0];
else
BlockToInsertInto = Preheader;
BasicBlock::iterator InsertPt = BlockToInsertInto->begin();

View File

@ -109,18 +109,6 @@ static inline void RemapInstruction(Instruction *I,
}
}
static void ChangeExitBlocksFromTo(Loop::iterator I, Loop::iterator E,
BasicBlock *Old, BasicBlock *New) {
for (; I != E; ++I) {
Loop *L = *I;
if (L->hasExitBlock(Old)) {
L->changeExitBlock(Old, New);
ChangeExitBlocksFromTo(L->begin(), L->end(), Old, New);
}
}
}
bool LoopUnroll::visitLoop(Loop *L) {
bool Changed = false;
@ -157,8 +145,7 @@ bool LoopUnroll::visitLoop(Loop *L) {
}
DEBUG(std::cerr << "UNROLLING!\n");
assert(L->getExitBlocks().size() == 1 && "Must have exactly one exit block!");
BasicBlock *LoopExit = L->getExitBlocks()[0];
BasicBlock *LoopExit = BI->getSuccessor(L->contains(BI->getSuccessor(0)));
// Create a new basic block to temporarily hold all of the cloned code.
BasicBlock *NewBlock = new BasicBlock();
@ -292,14 +279,6 @@ bool LoopUnroll::visitLoop(Loop *L) {
LI->removeBlock(Preheader);
LI->removeBlock(BB);
// If any loops used Preheader as an exit block, update them to use LoopExit.
if (Parent)
ChangeExitBlocksFromTo(Parent->begin(), Parent->end(),
Preheader, LoopExit);
else
ChangeExitBlocksFromTo(LI->begin(), LI->end(),
Preheader, LoopExit);
// If the preheader was the entry block of this function, move the exit block
// to be the new entry of the loop.
Function *F = LoopExit->getParent();

View File

@ -151,8 +151,10 @@ bool LoopSimplify::ProcessLoop(Loop *L) {
// predecessors that are inside of the loop. This check guarantees that the
// loop preheader/header will dominate the exit blocks. If the exit block has
// predecessors from outside of the loop, split the edge now.
for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i) {
BasicBlock *ExitBlock = L->getExitBlocks()[i];
std::vector<BasicBlock*> ExitBlocks;
L->getExitBlocks(ExitBlocks);
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
BasicBlock *ExitBlock = ExitBlocks[i];
for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
PI != PE; ++PI)
if (!L->contains(*PI)) {
@ -269,19 +271,6 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB,
return NewBB;
}
// ChangeExitBlock - This recursive function is used to change any exit blocks
// that use OldExit to use NewExit instead. This is recursive because children
// may need to be processed as well.
//
static void ChangeExitBlock(Loop *L, BasicBlock *OldExit, BasicBlock *NewExit) {
if (L->hasExitBlock(OldExit)) {
L->changeExitBlock(OldExit, NewExit);
for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
ChangeExitBlock(*I, OldExit, NewExit);
}
}
/// InsertPreheaderForLoop - Once we discover that a loop doesn't have a
/// preheader, this method is called to insert one. This method has two phases:
/// preheader insertion and analysis updating.
@ -323,11 +312,6 @@ void LoopSimplify::InsertPreheaderForLoop(Loop *L) {
ParentLoopsE = getAnalysis<LoopInfo>().end();
}
// Loop over all sibling loops, performing the substitution (recursively to
// include child loops)...
for (; ParentLoops != ParentLoopsE; ++ParentLoops)
ChangeExitBlock(*ParentLoops, Header, NewBB);
DominatorSet &DS = getAnalysis<DominatorSet>(); // Update dominator info
DominatorTree &DT = getAnalysis<DominatorTree>();
@ -405,8 +389,6 @@ void LoopSimplify::InsertPreheaderForLoop(Loop *L) {
/// outside of the loop.
void LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) {
DominatorSet &DS = getAnalysis<DominatorSet>();
assert(std::find(L->getExitBlocks().begin(), L->getExitBlocks().end(), Exit)
!= L->getExitBlocks().end() && "Not a current exit block!");
std::vector<BasicBlock*> LoopBlocks;
for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I)
@ -421,11 +403,6 @@ void LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) {
if (Loop *Parent = L->getParentLoop())
Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>());
// Replace any instances of Exit with NewBB in this and any nested loops...
for (df_iterator<Loop*> I = df_begin(L), E = df_end(L); I != E; ++I)
if (I->hasExitBlock(Exit))
I->changeExitBlock(Exit, NewBB); // Update exit block information
// Update dominator information (set, immdom, domtree, and domfrontier)
UpdateDomInfoForRevectoredPreds(NewBB, LoopBlocks);
}
@ -442,33 +419,6 @@ static void AddBlockAndPredsToSet(BasicBlock *BB, BasicBlock *StopBlock,
AddBlockAndPredsToSet(*I, StopBlock, Blocks);
}
static void ReplaceExitBlocksOfLoopAndParents(Loop *L, BasicBlock *Old,
BasicBlock *New) {
if (!L->hasExitBlock(Old)) return;
L->changeExitBlock(Old, New);
ReplaceExitBlocksOfLoopAndParents(L->getParentLoop(), Old, New);
}
/// VerifyExitBlocks - This is a function which can be useful for hacking on the
/// LoopSimplify Code.
static void VerifyExitBlocks(Loop *L) {
std::vector<BasicBlock*> ExitBlocks;
for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
BasicBlock *BB = L->getBlocks()[i];
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
if (!L->contains(*SI))
ExitBlocks.push_back(*SI);
}
std::vector<BasicBlock*> EB = L->getExitBlocks();
std::sort(EB.begin(), EB.end());
std::sort(ExitBlocks.begin(), ExitBlocks.end());
assert(EB == ExitBlocks && "Exit blocks were incorrectly updated!");
for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
VerifyExitBlocks(*I);
}
/// FindPHIToPartitionLoops - The first part of loop-nestification is to find a
/// PHI node that tells us how to partition the loops.
static PHINode *FindPHIToPartitionLoops(Loop *L) {
@ -545,16 +495,6 @@ Loop *LoopSimplify::SeparateNestedLoop(Loop *L) {
// L is now a subloop of our outer loop.
NewOuter->addChildLoop(L);
// Add all of L's exit blocks to the outer loop.
for (unsigned i = 0, e = L->getExitBlocks().size(); i != e; ++i)
NewOuter->addExitBlock(L->getExitBlocks()[i]);
// Add temporary exit block entries for NewBB. Add one for each edge in L
// that goes to NewBB.
for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); PI != E; ++PI)
if (L->contains(*PI))
L->addExitBlock(NewBB);
for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i)
NewOuter->addBlockEntry(L->getBlocks()[i]);
@ -588,16 +528,6 @@ Loop *LoopSimplify::SeparateNestedLoop(Loop *L) {
}
}
// Check all subloops of this loop, replacing any exit blocks that got
// revectored with the new basic block.
for (pred_iterator I = pred_begin(NewBB), E = pred_end(NewBB); I != E; ++I)
if (NewOuter->contains(*I)) {
// Change any exit blocks that used to go to Header to go to NewBB
// instead.
ReplaceExitBlocksOfLoopAndParents((Loop*)LI[*I], Header, NewBB);
}
//VerifyExitBlocks(NewOuter);
return NewOuter;
}
@ -693,11 +623,6 @@ void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) {
// loop and all parent loops.
L->addBasicBlockToLoop(BEBlock, getAnalysis<LoopInfo>());
// Replace any instances of Exit with NewBB in this and any nested loops...
for (df_iterator<Loop*> I = df_begin(L), E = df_end(L); I != E; ++I)
if (I->hasExitBlock(Header))
I->changeExitBlock(Header, BEBlock); // Update exit block information
// Update dominator information (set, immdom, domtree, and domfrontier)
UpdateDomInfoForRevectoredPreds(BEBlock, BackedgeBlocks);
}