Use a worklist-driven algorithm instead of a recursive one.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35680 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2007-04-05 01:27:02 +00:00
parent 3f108cb555
commit 1984a32867

View File

@@ -47,7 +47,16 @@ FunctionPass *llvm::createCFGSimplificationPass() {
static bool MarkAliveBlocks(BasicBlock *BB,
SmallPtrSet<BasicBlock*, 16> &Reachable) {
if (!Reachable.insert(BB)) return false;
std::vector<BasicBlock*> Worklist;
Worklist.push_back(BB);
bool Changed = false;
while (!Worklist.empty()) {
BB = Worklist.back();
Worklist.pop_back();
if (!Reachable.insert(BB))
continue;
// Do a quick scan of the basic block, turning any obviously unreachable
// instructions into LLVM unreachable insts. The instruction combining pass
@@ -58,13 +67,13 @@ static bool MarkAliveBlocks(BasicBlock *BB,
isa<UndefValue>(SI->getOperand(1))) {
// Loop over all of the successors, removing BB's entry from any PHI
// nodes.
for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE;++I)
(*I)->removePredecessor(BB);
new UnreachableInst(SI);
// All instructions after this are dead.
for (; BBI != E; ) {
while (BBI != E) {
if (!BBI->use_empty())
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
BB->getInstList().erase(BBI++);
@@ -73,10 +82,10 @@ static bool MarkAliveBlocks(BasicBlock *BB,
}
bool Changed = ConstantFoldTerminator(BB);
Changed |= ConstantFoldTerminator(BB);
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
Changed |= MarkAliveBlocks(*SI, Reachable);
Worklist.push_back(*SI);
}
return Changed;
}