#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;
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()) {
// 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;