1//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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 run a DIE pass sometime after running this pass.
18//
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/ConstantFolding.h"
24#include "llvm/IR/Constant.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/InstIterator.h"
27#include "llvm/IR/Instruction.h"
28#include "llvm/Pass.h"
29#include "llvm/Target/TargetLibraryInfo.h"
30#include <set>
31using namespace llvm;
32
33#define DEBUG_TYPE "constprop"
34
35STATISTIC(NumInstKilled, "Number of instructions killed");
36
37namespace {
38  struct ConstantPropagation : public FunctionPass {
39    static char ID; // Pass identification, replacement for typeid
40    ConstantPropagation() : FunctionPass(ID) {
41      initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
42    }
43
44    bool runOnFunction(Function &F) override;
45
46    void getAnalysisUsage(AnalysisUsage &AU) const override {
47      AU.setPreservesCFG();
48      AU.addRequired<TargetLibraryInfo>();
49    }
50  };
51}
52
53char ConstantPropagation::ID = 0;
54INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
55                "Simple constant propagation", false, false)
56INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
57INITIALIZE_PASS_END(ConstantPropagation, "constprop",
58                "Simple constant propagation", false, false)
59
60FunctionPass *llvm::createConstantPropagationPass() {
61  return new ConstantPropagation();
62}
63
64bool ConstantPropagation::runOnFunction(Function &F) {
65  // Initialize the worklist to all of the instructions ready to process...
66  std::set<Instruction*> WorkList;
67  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
68      WorkList.insert(&*i);
69  }
70  bool Changed = false;
71  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
72  const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
73  TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
74
75  while (!WorkList.empty()) {
76    Instruction *I = *WorkList.begin();
77    WorkList.erase(WorkList.begin());    // Get an element from the worklist...
78
79    if (!I->use_empty())                 // Don't muck with dead instructions...
80      if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
81        // Add all of the users of this instruction to the worklist, they might
82        // be constant propagatable now...
83        for (User *U : I->users())
84          WorkList.insert(cast<Instruction>(U));
85
86        // Replace all of the uses of a variable with uses of the constant.
87        I->replaceAllUsesWith(C);
88
89        // Remove the dead instruction.
90        WorkList.erase(I);
91        I->eraseFromParent();
92
93        // We made a change to the function...
94        Changed = true;
95        ++NumInstKilled;
96      }
97  }
98  return Changed;
99}
100