Avoid deleting individual instructions until AFTER dead blocks have dropped
[oota-llvm.git] / lib / Transforms / Scalar / DCE.cpp
index 5983910c18228636d402ac1e41d967a605341202..fa2392fbb4248b0f44e8fe235d3158bf09857566 100644 (file)
@@ -9,31 +9,16 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Scalar/DCE.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Local.h"
+#include "llvm/Instruction.h"
 #include "llvm/Pass.h"
-#include "llvm/InstrTypes.h"
-#include "llvm/Function.h"
 #include "llvm/Support/InstIterator.h"
+#include "Support/StatisticReporter.h"
 #include <set>
 
-static inline bool isInstDead(Instruction *I) {
-  return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
-}
-
-// dceInstruction - Inspect the instruction at *BBI and figure out if it's
-// [trivially] dead.  If so, remove the instruction and update the iterator
-// to point to the instruction that immediately succeeded the original
-// instruction.
-//
-bool dceInstruction(BasicBlock::InstListType &BBIL,
-                    BasicBlock::iterator &BBI) {
-  // Look for un"used" definitions...
-  if (isInstDead(*BBI)) {
-    delete BBIL.remove(BBI);   // Bye bye
-    return true;
-  }
-  return false;
-}
+static Statistic<> DIEEliminated("die\t\t- Number of insts removed");
+static Statistic<> DCEEliminated("dce\t\t- Number of insts removed");
 
 //===----------------------------------------------------------------------===//
 // DeadInstElimination pass implementation
@@ -47,9 +32,10 @@ namespace {
       BasicBlock::InstListType &Vals = BB->getInstList();
       bool Changed = false;
       for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
-        if (dceInstruction(Vals, DI))
+        if (dceInstruction(DI)) {
           Changed = true;
-        else
+          ++DIEEliminated;
+        } else
           ++DI;
       return Changed;
     }
@@ -95,7 +81,7 @@ bool DCE::runOnFunction(Function *F) {
     Instruction *I = WorkList.back();
     WorkList.pop_back();
     
-    if (isInstDead(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.
@@ -122,6 +108,7 @@ bool DCE::runOnFunction(Function *F) {
     for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
       if (DeadInsts.count(*BI)) {            // Is this instruction dead?
         delete BBIL.remove(BI);              // Yup, remove and delete inst
+        ++DCEEliminated;
       } else {                               // This instruction is not dead
         ++BI;                                // Continue on to the next one...
       }