ConstantProp.cpp revision 198090
1301238Sdelphij//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2301238Sdelphij//
3301238Sdelphij//                     The LLVM Compiler Infrastructure
4301238Sdelphij//
5301238Sdelphij// This file is distributed under the University of Illinois Open Source
6301238Sdelphij// License. See LICENSE.TXT for details.
7301238Sdelphij//
8301238Sdelphij//===----------------------------------------------------------------------===//
9301238Sdelphij//
10301238Sdelphij// This file implements constant propagation and merging:
11301238Sdelphij//
12301238Sdelphij// Specifically, this:
13301238Sdelphij//   * Converts instructions like "add int 1, 2" into 3
14301238Sdelphij//
15301238Sdelphij// Notice that:
16301238Sdelphij//   * This pass has a habit of making definitions be dead.  It is a good idea
17301238Sdelphij//     to run a DIE pass sometime after running this pass.
18301238Sdelphij//
19301238Sdelphij//===----------------------------------------------------------------------===//
20301238Sdelphij
21301238Sdelphij#define DEBUG_TYPE "constprop"
22301238Sdelphij#include "llvm/Transforms/Scalar.h"
23301238Sdelphij#include "llvm/Analysis/ConstantFolding.h"
24301238Sdelphij#include "llvm/Constant.h"
25301238Sdelphij#include "llvm/Instruction.h"
26301238Sdelphij#include "llvm/Pass.h"
27301238Sdelphij#include "llvm/Support/InstIterator.h"
28301238Sdelphij#include "llvm/ADT/Statistic.h"
29301238Sdelphij#include <set>
30301238Sdelphijusing namespace llvm;
31301238Sdelphij
32301238SdelphijSTATISTIC(NumInstKilled, "Number of instructions killed");
33301238Sdelphij
34301238Sdelphijnamespace {
35301238Sdelphij  struct ConstantPropagation : public FunctionPass {
36301238Sdelphij    static char ID; // Pass identification, replacement for typeid
37310419Sdelphij    ConstantPropagation() : FunctionPass(&ID) {}
38310419Sdelphij
39301238Sdelphij    bool runOnFunction(Function &F);
40301238Sdelphij
41301238Sdelphij    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42310419Sdelphij      AU.setPreservesCFG();
43310419Sdelphij    }
44301238Sdelphij  };
45301238Sdelphij}
46310419Sdelphij
47301238Sdelphijchar ConstantPropagation::ID = 0;
48301238Sdelphijstatic RegisterPass<ConstantPropagation>
49301238SdelphijX("constprop", "Simple constant propagation");
50301238Sdelphij
51301238SdelphijFunctionPass *llvm::createConstantPropagationPass() {
52301238Sdelphij  return new ConstantPropagation();
53310419Sdelphij}
54301238Sdelphij
55301238Sdelphij
56301238Sdelphijbool ConstantPropagation::runOnFunction(Function &F) {
57301238Sdelphij  // Initialize the worklist to all of the instructions ready to process...
58301238Sdelphij  std::set<Instruction*> WorkList;
59301238Sdelphij  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
60301238Sdelphij      WorkList.insert(&*i);
61301238Sdelphij  }
62301238Sdelphij  bool Changed = false;
63301238Sdelphij
64301238Sdelphij  while (!WorkList.empty()) {
65301238Sdelphij    Instruction *I = *WorkList.begin();
66301238Sdelphij    WorkList.erase(WorkList.begin());    // Get an element from the worklist...
67301238Sdelphij
68301238Sdelphij    if (!I->use_empty())                 // Don't muck with dead instructions...
69301238Sdelphij      if (Constant *C = ConstantFoldInstruction(I, F.getContext())) {
70301238Sdelphij        // Add all of the users of this instruction to the worklist, they might
71301238Sdelphij        // be constant propagatable now...
72301238Sdelphij        for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
73301238Sdelphij             UI != UE; ++UI)
74310419Sdelphij          WorkList.insert(cast<Instruction>(*UI));
75301238Sdelphij
76301238Sdelphij        // Replace all of the uses of a variable with uses of the constant.
77301238Sdelphij        I->replaceAllUsesWith(C);
78301238Sdelphij
79301238Sdelphij        // Remove the dead instruction.
80301238Sdelphij        WorkList.erase(I);
81301238Sdelphij        I->eraseFromParent();
82301238Sdelphij
83301238Sdelphij        // We made a change to the function...
84301238Sdelphij        Changed = true;
85        ++NumInstKilled;
86      }
87  }
88  return Changed;
89}
90