From 928128281f5399da0761e1fc3696d89dd85316b8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 8 May 2005 18:45:26 +0000 Subject: [PATCH] clean up and modernize this pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21776 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/DCE.cpp | 42 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp index 759180a3712..2783f12f05d 100644 --- a/lib/Transforms/Scalar/DCE.cpp +++ b/lib/Transforms/Scalar/DCE.cpp @@ -77,20 +77,19 @@ namespace { bool DCE::runOnFunction(Function &F) { // Start out with all of the instructions in the worklist... std::vector WorkList; - for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { - WorkList.push_back(&*i); - } - std::set DeadInsts; + for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) + WorkList.push_back(&*i); // Loop over the worklist finding instructions that are dead. If they are // dead make them drop all of their uses, making other instructions // potentially dead, and work until the worklist is empty. // + bool MadeChange = false; while (!WorkList.empty()) { Instruction *I = WorkList.back(); WorkList.pop_back(); - if (isInstructionTriviallyDead(I)) { // If the instruction is dead... + if (isInstructionTriviallyDead(I)) { // If the instruction is dead. // Loop over all of the values that the instruction uses, if there are // instructions being used, add them to the worklist, because they might // go dead after this one is removed. @@ -99,28 +98,23 @@ bool DCE::runOnFunction(Function &F) { if (Instruction *Used = dyn_cast(*OI)) WorkList.push_back(Used); - // Tell the instruction to let go of all of the values it uses... - I->dropAllReferences(); + // Remove the instruction. + I->eraseFromParent(); + + // Remove the instruction from the worklist if it still exists in it. + for (std::vector::iterator WI = WorkList.begin(), + E = WorkList.end(); WI != E; ++WI) + if (*WI == I) { + WorkList.erase(WI); + --E; + --WI; + } - // Keep track of this instruction, because we are going to delete it later - DeadInsts.insert(I); + MadeChange = true; + ++DCEEliminated; } } - - // If we found no dead instructions, we haven't changed the function... - if (DeadInsts.empty()) return false; - - // Otherwise, loop over the program, removing and deleting the instructions... - for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) - for (BasicBlock::iterator BI = I->begin(); BI != I->end(); ) - if (DeadInsts.count(BI)) { // Is this instruction dead? - BI = I->getInstList().erase(BI); // Yup, remove and delete inst - ++DCEEliminated; - } else { // This instruction is not dead - ++BI; // Continue on to the next one... - } - - return true; + return MadeChange; } FunctionPass *llvm::createDeadCodeEliminationPass() { -- 2.34.1