ConstantProp.cpp revision 193323
1193323Sed//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements constant propagation and merging:
11193323Sed//
12193323Sed// Specifically, this:
13193323Sed//   * Converts instructions like "add int 1, 2" into 3
14193323Sed//
15193323Sed// Notice that:
16193323Sed//   * This pass has a habit of making definitions be dead.  It is a good idea
17193323Sed//     to run a DIE pass sometime after running this pass.
18193323Sed//
19193323Sed//===----------------------------------------------------------------------===//
20193323Sed
21193323Sed#define DEBUG_TYPE "constprop"
22193323Sed#include "llvm/Transforms/Scalar.h"
23193323Sed#include "llvm/Analysis/ConstantFolding.h"
24193323Sed#include "llvm/Constant.h"
25193323Sed#include "llvm/Instruction.h"
26193323Sed#include "llvm/Pass.h"
27193323Sed#include "llvm/Support/Compiler.h"
28193323Sed#include "llvm/Support/InstIterator.h"
29193323Sed#include "llvm/ADT/Statistic.h"
30193323Sed#include <set>
31193323Sedusing namespace llvm;
32193323Sed
33193323SedSTATISTIC(NumInstKilled, "Number of instructions killed");
34193323Sed
35193323Sednamespace {
36193323Sed  struct VISIBILITY_HIDDEN ConstantPropagation : public FunctionPass {
37193323Sed    static char ID; // Pass identification, replacement for typeid
38193323Sed    ConstantPropagation() : FunctionPass(&ID) {}
39193323Sed
40193323Sed    bool runOnFunction(Function &F);
41193323Sed
42193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43193323Sed      AU.setPreservesCFG();
44193323Sed    }
45193323Sed  };
46193323Sed}
47193323Sed
48193323Sedchar ConstantPropagation::ID = 0;
49193323Sedstatic RegisterPass<ConstantPropagation>
50193323SedX("constprop", "Simple constant propagation");
51193323Sed
52193323SedFunctionPass *llvm::createConstantPropagationPass() {
53193323Sed  return new ConstantPropagation();
54193323Sed}
55193323Sed
56193323Sed
57193323Sedbool ConstantPropagation::runOnFunction(Function &F) {
58193323Sed  // Initialize the worklist to all of the instructions ready to process...
59193323Sed  std::set<Instruction*> WorkList;
60193323Sed  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
61193323Sed      WorkList.insert(&*i);
62193323Sed  }
63193323Sed  bool Changed = false;
64193323Sed
65193323Sed  while (!WorkList.empty()) {
66193323Sed    Instruction *I = *WorkList.begin();
67193323Sed    WorkList.erase(WorkList.begin());    // Get an element from the worklist...
68193323Sed
69193323Sed    if (!I->use_empty())                 // Don't muck with dead instructions...
70193323Sed      if (Constant *C = ConstantFoldInstruction(I)) {
71193323Sed        // Add all of the users of this instruction to the worklist, they might
72193323Sed        // be constant propagatable now...
73193323Sed        for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
74193323Sed             UI != UE; ++UI)
75193323Sed          WorkList.insert(cast<Instruction>(*UI));
76193323Sed
77193323Sed        // Replace all of the uses of a variable with uses of the constant.
78193323Sed        I->replaceAllUsesWith(C);
79193323Sed
80193323Sed        // Remove the dead instruction.
81193323Sed        WorkList.erase(I);
82193323Sed        I->eraseFromParent();
83193323Sed
84193323Sed        // We made a change to the function...
85193323Sed        Changed = true;
86193323Sed        ++NumInstKilled;
87193323Sed      }
88193323Sed  }
89193323Sed  return Changed;
90193323Sed}
91