Switch over Transforms/Scalar to use the STATISTIC macro. For each statistic
[oota-llvm.git] / lib / Transforms / Scalar / ConstantProp.cpp
1 //===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements constant propagation and merging:
11 //
12 // Specifically, this:
13 //   * Converts instructions like "add int 1, 2" into 3
14 //
15 // Notice that:
16 //   * This pass has a habit of making definitions be dead.  It is a good idea
17 //     to to run a DIE pass sometime after running this pass.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #define DEBUG_TYPE "constprop"
22 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Transforms/Utils/Local.h"
24 #include "llvm/Constant.h"
25 #include "llvm/Instruction.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/InstIterator.h"
28 #include "llvm/ADT/Statistic.h"
29 #include <set>
30 using namespace llvm;
31
32 STATISTIC(NumInstKilled, "Number of instructions killed");
33
34 namespace {
35   struct ConstantPropagation : public FunctionPass {
36     bool runOnFunction(Function &F);
37
38     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39       AU.setPreservesCFG();
40     }
41   };
42
43   RegisterPass<ConstantPropagation> X("constprop",
44                                       "Simple constant propagation");
45 }
46
47 FunctionPass *llvm::createConstantPropagationPass() {
48   return new ConstantPropagation();
49 }
50
51
52 bool ConstantPropagation::runOnFunction(Function &F) {
53   // Initialize the worklist to all of the instructions ready to process...
54   std::set<Instruction*> WorkList;
55   for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
56       WorkList.insert(&*i);
57   }
58   bool Changed = false;
59
60   while (!WorkList.empty()) {
61     Instruction *I = *WorkList.begin();
62     WorkList.erase(WorkList.begin());    // Get an element from the worklist...
63
64     if (!I->use_empty())                 // Don't muck with dead instructions...
65       if (Constant *C = ConstantFoldInstruction(I)) {
66         // Add all of the users of this instruction to the worklist, they might
67         // be constant propagatable now...
68         for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
69              UI != UE; ++UI)
70           WorkList.insert(cast<Instruction>(*UI));
71
72         // Replace all of the uses of a variable with uses of the constant.
73         I->replaceAllUsesWith(C);
74
75         // Remove the dead instruction.
76         WorkList.erase(I);
77         I->getParent()->getInstList().erase(I);
78
79         // We made a change to the function...
80         Changed = true;
81         ++NumInstKilled;
82       }
83   }
84   return Changed;
85 }