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