switch AddReachableCodeToWorklist from being recursive to being iterative.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35282 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-03-23 19:17:18 +00:00
parent b35ae0371a
commit 2c7718a539

View File

@ -9989,8 +9989,15 @@ static void AddReachableCodeToWorklist(BasicBlock *BB,
SmallPtrSet<BasicBlock*, 64> &Visited,
InstCombiner &IC,
const TargetData *TD) {
// We have now visited this block! If we've already been here, bail out.
if (!Visited.insert(BB)) return;
std::vector<BasicBlock*> Worklist;
Worklist.push_back(BB);
while (!Worklist.empty()) {
BB = Worklist.back();
Worklist.pop_back();
// We have now visited this block! If we've already been here, ignore it.
if (!Visited.insert(BB)) continue;
for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
Instruction *Inst = BBI++;
@ -10015,32 +10022,33 @@ static void AddReachableCodeToWorklist(BasicBlock *BB,
IC.AddToWorkList(Inst);
}
// Recursively visit successors. If this is a branch or switch on a constant,
// only visit the reachable successor.
// Recursively visit successors. If this is a branch or switch on a
// constant, only visit the reachable successor.
TerminatorInst *TI = BB->getTerminator();
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, IC, TD);
return;
Worklist.push_back(BI->getSuccessor(!CondVal));
continue;
}
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
// See if this is an explicit destination.
for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
if (SI->getCaseValue(i) == Cond) {
AddReachableCodeToWorklist(SI->getSuccessor(i), Visited, IC, TD);
return;
Worklist.push_back(SI->getSuccessor(i));
continue;
}
// Otherwise it is the default destination.
AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, IC, TD);
return;
Worklist.push_back(SI->getSuccessor(0));
continue;
}
}
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, IC, TD);
Worklist.push_back(TI->getSuccessor(i));
}
}
bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {