[llvm-commits] CVS: llvm/lib/Transforms/Scalar/DCE.cpp

Chris Lattner lattner at cs.uiuc.edu
Sun May 8 11:45:43 PDT 2005



Changes in directory llvm/lib/Transforms/Scalar:

DCE.cpp updated: 1.57 -> 1.58
---
Log message:

clean up and modernize this pass.


---
Diffs of the changes:  (+18 -24)

 DCE.cpp |   42 ++++++++++++++++++------------------------
 1 files changed, 18 insertions(+), 24 deletions(-)


Index: llvm/lib/Transforms/Scalar/DCE.cpp
diff -u llvm/lib/Transforms/Scalar/DCE.cpp:1.57 llvm/lib/Transforms/Scalar/DCE.cpp:1.58
--- llvm/lib/Transforms/Scalar/DCE.cpp:1.57	Thu Apr 21 18:45:12 2005
+++ llvm/lib/Transforms/Scalar/DCE.cpp	Sun May  8 13:45:26 2005
@@ -77,20 +77,19 @@
 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 @@
         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() {






More information about the llvm-commits mailing list