Apply the VISIBILITY_HIDDEN field to the remaining anonymous classes in
[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/Analysis/ConstantFolding.h"
24 #include "llvm/Constant.h"
25 #include "llvm/Instruction.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/InstIterator.h"
29 #include "llvm/ADT/Statistic.h"
30 #include <set>
31 using namespace llvm;
32
33 STATISTIC(NumInstKilled, "Number of instructions killed");
34
35 namespace {
36   struct VISIBILITY_HIDDEN ConstantPropagation : public FunctionPass {
37     bool runOnFunction(Function &F);
38
39     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40       AU.setPreservesCFG();
41     }
42   };
43
44   RegisterPass<ConstantPropagation> X("constprop",
45                                       "Simple constant propagation");
46 }
47
48 FunctionPass *llvm::createConstantPropagationPass() {
49   return new ConstantPropagation();
50 }
51
52
53 bool ConstantPropagation::runOnFunction(Function &F) {
54   // Initialize the worklist to all of the instructions ready to process...
55   std::set<Instruction*> WorkList;
56   for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
57       WorkList.insert(&*i);
58   }
59   bool Changed = false;
60
61   while (!WorkList.empty()) {
62     Instruction *I = *WorkList.begin();
63     WorkList.erase(WorkList.begin());    // Get an element from the worklist...
64
65     if (!I->use_empty())                 // Don't muck with dead instructions...
66       if (Constant *C = ConstantFoldInstruction(I)) {
67         // Add all of the users of this instruction to the worklist, they might
68         // be constant propagatable now...
69         for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
70              UI != UE; ++UI)
71           WorkList.insert(cast<Instruction>(*UI));
72
73         // Replace all of the uses of a variable with uses of the constant.
74         I->replaceAllUsesWith(C);
75
76         // Remove the dead instruction.
77         WorkList.erase(I);
78         I->getParent()->getInstList().erase(I);
79
80         // We made a change to the function...
81         Changed = true;
82         ++NumInstKilled;
83       }
84   }
85   return Changed;
86 }