summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/DCE.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2005-05-08 18:45:26 +0000
committerChris Lattner <sabre@nondot.org>2005-05-08 18:45:26 +0000
commit928128281f5399da0761e1fc3696d89dd85316b8 (patch)
tree19a25ddc6f71ca8da6f7db40a7dbbbd97da91e68 /lib/Transforms/Scalar/DCE.cpp
parent120347e8d115cf40aed10e63d406713b46f327df (diff)
downloadllvm-928128281f5399da0761e1fc3696d89dd85316b8.tar.gz
llvm-928128281f5399da0761e1fc3696d89dd85316b8.tar.bz2
llvm-928128281f5399da0761e1fc3696d89dd85316b8.tar.xz
clean up and modernize this pass.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21776 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/DCE.cpp')
-rw-r--r--lib/Transforms/Scalar/DCE.cpp42
1 files changed, 18 insertions, 24 deletions
diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp
index 759180a371..2783f12f05 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<Instruction*> WorkList;
- for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
- WorkList.push_back(&*i);
- }
- std::set<Instruction*> 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<Instruction>(*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<Instruction*>::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() {