diff --git a/lib/Transforms/Scalar/ADCE.cpp b/lib/Transforms/Scalar/ADCE.cpp index c633f930581..11fb1e33242 100644 --- a/lib/Transforms/Scalar/ADCE.cpp +++ b/lib/Transforms/Scalar/ADCE.cpp @@ -191,10 +191,18 @@ bool ADCE::doADCE() { // be eliminated later, along with the instructions inside. // std::set ReachableBBs; - for (df_ext_iterator - BBI = df_ext_begin(&Func->front(), ReachableBBs), - BBE = df_ext_end(&Func->front(), ReachableBBs); BBI != BBE; ++BBI) { - BasicBlock *BB = *BBI; + std::vector Stack; + Stack.push_back(&Func->getEntryBlock()); + + while (!Stack.empty()) { + BasicBlock* BB = Stack.back(); + if (ReachableBBs.count(BB)) { + Stack.pop_back(); + continue; + } else { + ReachableBBs.insert(BB); + } + for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { Instruction *I = II++; if (CallInst *CI = dyn_cast(I)) { @@ -217,6 +225,15 @@ bool ADCE::doADCE() { ++NumInstRemoved; } } + + for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) { + // Back edges (as opposed to cross edges) indicate loops, so implicitly + // mark them live. + if (std::find(Stack.begin(), Stack.end(), *SI) != Stack.end()) + markInstructionLive(BB->getTerminator()); + if (!ReachableBBs.count(*SI)) + Stack.push_back(*SI); + } } // Check to ensure we have an exit node for this CFG. If we don't, we won't