Implement Transforms/InstCombine/cast-load-gep.ll, which allows us to devirtualize
[oota-llvm.git] / lib / Transforms / Scalar / ConstantProp.cpp
index d78556c00403f0907efa58145293452a2c12b823..a3fa4a9665d381fb02da06eb8e07128b4277483f 100644 (file)
 
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils/Local.h"
+#include "llvm/Constant.h"
 #include "llvm/Instruction.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/InstIterator.h"
-#include "Support/Statistic.h"
+#include "llvm/ADT/Statistic.h"
 #include <set>
 using namespace llvm;
 
@@ -41,14 +42,17 @@ namespace {
   RegisterOpt<ConstantPropagation> X("constprop","Simple constant propagation");
 }
 
-Pass *llvm::createConstantPropagationPass() {
+FunctionPass *llvm::createConstantPropagationPass() {
   return new ConstantPropagation();
 }
 
 
 bool ConstantPropagation::runOnFunction(Function &F) {
   // Initialize the worklist to all of the instructions ready to process...
-  std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
+  std::set<Instruction*> WorkList;
+  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
+      WorkList.insert(&*i);
+  }
   bool Changed = false;
 
   while (!WorkList.empty()) {
@@ -66,6 +70,10 @@ bool ConstantPropagation::runOnFunction(Function &F) {
         // Replace all of the uses of a variable with uses of the constant.
         I->replaceAllUsesWith(C);
 
+        // Remove the dead instruction.
+        WorkList.erase(I);
+        I->getParent()->getInstList().erase(I);
+
         // We made a change to the function...
         Changed = true;
         ++NumInstKilled;