From: Chris Lattner Date: Fri, 23 Mar 2007 19:17:18 +0000 (+0000) Subject: switch AddReachableCodeToWorklist from being recursive to being iterative. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=2c7718a5391fc9d453548e1aae4f9c72aed7c029;p=oota-llvm.git 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 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 4208e968562..eabd3bf47b6 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -9989,58 +9989,66 @@ static void AddReachableCodeToWorklist(BasicBlock *BB, SmallPtrSet &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; - - for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { - Instruction *Inst = BBI++; + std::vector Worklist; + Worklist.push_back(BB); + + while (!Worklist.empty()) { + BB = Worklist.back(); + Worklist.pop_back(); - // DCE instruction if trivially dead. - if (isInstructionTriviallyDead(Inst)) { - ++NumDeadInst; - DOUT << "IC: DCE: " << *Inst; - Inst->eraseFromParent(); - continue; - } + // We have now visited this block! If we've already been here, ignore it. + if (!Visited.insert(BB)) continue; - // ConstantProp instruction if trivially constant. - if (Constant *C = ConstantFoldInstruction(Inst, TD)) { - DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; - Inst->replaceAllUsesWith(C); - ++NumConstProp; - Inst->eraseFromParent(); - continue; + for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { + Instruction *Inst = BBI++; + + // DCE instruction if trivially dead. + if (isInstructionTriviallyDead(Inst)) { + ++NumDeadInst; + DOUT << "IC: DCE: " << *Inst; + Inst->eraseFromParent(); + continue; + } + + // ConstantProp instruction if trivially constant. + if (Constant *C = ConstantFoldInstruction(Inst, TD)) { + DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; + Inst->replaceAllUsesWith(C); + ++NumConstProp; + Inst->eraseFromParent(); + continue; + } + + IC.AddToWorkList(Inst); } - - IC.AddToWorkList(Inst); - } - // 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(TI)) { - if (BI->isConditional() && isa(BI->getCondition())) { - bool CondVal = cast(BI->getCondition())->getZExtValue(); - AddReachableCodeToWorklist(BI->getSuccessor(!CondVal), Visited, IC, TD); - return; - } - } else if (SwitchInst *SI = dyn_cast(TI)) { - if (ConstantInt *Cond = dyn_cast(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; - } - - // Otherwise it is the default destination. - AddReachableCodeToWorklist(SI->getSuccessor(0), Visited, IC, TD); - return; + // 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(TI)) { + if (BI->isConditional() && isa(BI->getCondition())) { + bool CondVal = cast(BI->getCondition())->getZExtValue(); + Worklist.push_back(BI->getSuccessor(!CondVal)); + continue; + } + } else if (SwitchInst *SI = dyn_cast(TI)) { + if (ConstantInt *Cond = dyn_cast(SI->getCondition())) { + // See if this is an explicit destination. + for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) + if (SI->getCaseValue(i) == Cond) { + Worklist.push_back(SI->getSuccessor(i)); + continue; + } + + // Otherwise it is the default destination. + Worklist.push_back(SI->getSuccessor(0)); + continue; + } } + + for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) + Worklist.push_back(TI->getSuccessor(i)); } - - for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) - AddReachableCodeToWorklist(TI->getSuccessor(i), Visited, IC, TD); } bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {