Fix bug: ADCE/2003-01-22-PredecessorProblem.ll
[oota-llvm.git] / lib / Transforms / Scalar / ADCE.cpp
index 618ff23b4fbb70482af6be38011a41a227591e89..24ccee60344bd76b9663b9ebbd1e1639d413d780 100644 (file)
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Type.h"
-#include "llvm/Analysis/Dominators.h"
+#include "llvm/Analysis/PostDominators.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iPHINode.h"
 #include "llvm/Constant.h"
 #include "llvm/Support/CFG.h"
 #include "Support/STLExtras.h"
 #include "Support/DepthFirstIterator.h"
-#include "Support/StatisticReporter.h"
+#include "Support/Statistic.h"
 #include <algorithm>
-#include <iostream>
 using std::cerr;
 using std::vector;
 
-static Statistic<> NumBlockRemoved("adce\t\t- Number of basic blocks removed");
-static Statistic<> NumInstRemoved ("adce\t\t- Number of instructions removed");
-
 namespace {
+  Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
+  Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
 
 //===----------------------------------------------------------------------===//
 // ADCE Class
@@ -333,10 +331,17 @@ bool ADCE::doADCE() {
       }
   }
 
-  // Loop over all of the basic blocks in the function, dropping references of
-  // the dead basic blocks
+  // We make changes if there are any dead blocks in the function...
+  if (unsigned NumDeadBlocks = Func->size() - AliveBlocks.size()) {
+    MadeChanges = true;
+    NumBlockRemoved += NumDeadBlocks;
+  }
+
+  // Loop over all of the basic blocks in the function, removing control flow
+  // edges to live blocks (also eliminating any entries in PHI functions in
+  // referenced blocks).
   //
-  for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB) {
+  for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
     if (!AliveBlocks.count(BB)) {
       // Remove all outgoing edges from this basic block and convert the
       // terminator into a return instruction.
@@ -353,16 +358,19 @@ bool ADCE::doADCE() {
         // Delete the old terminator instruction...
         BB->getInstList().pop_back();
         const Type *RetTy = Func->getReturnType();
-        Instruction *New = new ReturnInst(RetTy != Type::VoidTy ?
-                                          Constant::getNullValue(RetTy) : 0);
-        BB->getInstList().push_back(New);
+        BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ?
+                                           Constant::getNullValue(RetTy) : 0));
       }
+    }
 
+
+  // Loop over all of the basic blocks in the function, dropping references of
+  // the dead basic blocks.  We must do this after the previous step to avoid
+  // dropping references to PHIs which still have entries...
+  //
+  for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
+    if (!AliveBlocks.count(BB))
       BB->dropAllReferences();
-      ++NumBlockRemoved;
-      MadeChanges = true;
-    }
-  }
 
   // Now loop through all of the blocks and delete the dead ones.  We can safely
   // do this now because we know that there are no references to dead blocks