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"
23249423Sdim#include "llvm/ADT/Statistic.h"
24193323Sed#include "llvm/Analysis/ConstantFolding.h"
25249423Sdim#include "llvm/IR/Constant.h"
26249423Sdim#include "llvm/IR/DataLayout.h"
27249423Sdim#include "llvm/IR/Instruction.h"
28193323Sed#include "llvm/Pass.h"
29249423Sdim#include "llvm/Support/InstIterator.h"
30234353Sdim#include "llvm/Target/TargetLibraryInfo.h"
31193323Sed#include <set>
32193323Sedusing namespace llvm;
33193323Sed
34193323SedSTATISTIC(NumInstKilled, "Number of instructions killed");
35193323Sed
36193323Sednamespace {
37198090Srdivacky  struct ConstantPropagation : public FunctionPass {
38193323Sed    static char ID; // Pass identification, replacement for typeid
39218893Sdim    ConstantPropagation() : FunctionPass(ID) {
40218893Sdim      initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
41218893Sdim    }
42193323Sed
43193323Sed    bool runOnFunction(Function &F);
44193323Sed
45193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46193323Sed      AU.setPreservesCFG();
47234353Sdim      AU.addRequired<TargetLibraryInfo>();
48193323Sed    }
49193323Sed  };
50193323Sed}
51193323Sed
52193323Sedchar ConstantPropagation::ID = 0;
53234353SdimINITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54218893Sdim                "Simple constant propagation", false, false)
55234353SdimINITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
56234353SdimINITIALIZE_PASS_END(ConstantPropagation, "constprop",
57234353Sdim                "Simple constant propagation", false, false)
58193323Sed
59193323SedFunctionPass *llvm::createConstantPropagationPass() {
60193323Sed  return new ConstantPropagation();
61193323Sed}
62193323Sed
63193323Sedbool ConstantPropagation::runOnFunction(Function &F) {
64193323Sed  // Initialize the worklist to all of the instructions ready to process...
65193323Sed  std::set<Instruction*> WorkList;
66193323Sed  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
67193323Sed      WorkList.insert(&*i);
68193323Sed  }
69193323Sed  bool Changed = false;
70243830Sdim  DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
71234353Sdim  TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
72193323Sed
73193323Sed  while (!WorkList.empty()) {
74193323Sed    Instruction *I = *WorkList.begin();
75193323Sed    WorkList.erase(WorkList.begin());    // Get an element from the worklist...
76193323Sed
77193323Sed    if (!I->use_empty())                 // Don't muck with dead instructions...
78234353Sdim      if (Constant *C = ConstantFoldInstruction(I, TD, TLI)) {
79193323Sed        // Add all of the users of this instruction to the worklist, they might
80193323Sed        // be constant propagatable now...
81193323Sed        for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
82193323Sed             UI != UE; ++UI)
83193323Sed          WorkList.insert(cast<Instruction>(*UI));
84193323Sed
85193323Sed        // Replace all of the uses of a variable with uses of the constant.
86193323Sed        I->replaceAllUsesWith(C);
87193323Sed
88193323Sed        // Remove the dead instruction.
89193323Sed        WorkList.erase(I);
90193323Sed        I->eraseFromParent();
91193323Sed
92193323Sed        // We made a change to the function...
93193323Sed        Changed = true;
94193323Sed        ++NumInstKilled;
95193323Sed      }
96193323Sed  }
97193323Sed  return Changed;
98193323Sed}
99