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/InstIterator.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include <set>
30 using namespace llvm;
31
32 #define DEBUG_TYPE "constprop"
33
34 STATISTIC(NumInstKilled, "Number of instructions killed");
35
36 namespace {
37 struct ConstantPropagation : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
ConstantPropagation__anon1a04cc9f0111::ConstantPropagation39 ConstantPropagation() : FunctionPass(ID) {
40 initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
41 }
42
43 bool runOnFunction(Function &F) override;
44
getAnalysisUsage__anon1a04cc9f0111::ConstantPropagation45 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 AU.setPreservesCFG();
47 AU.addRequired<TargetLibraryInfoWrapperPass>();
48 }
49 };
50 }
51
52 char ConstantPropagation::ID = 0;
53 INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54 "Simple constant propagation", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)55 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
56 INITIALIZE_PASS_END(ConstantPropagation, "constprop",
57 "Simple constant propagation", false, false)
58
59 FunctionPass *llvm::createConstantPropagationPass() {
60 return new ConstantPropagation();
61 }
62
runOnFunction(Function & F)63 bool ConstantPropagation::runOnFunction(Function &F) {
64 if (skipFunction(F))
65 return false;
66
67 // Initialize the worklist to all of the instructions ready to process...
68 std::set<Instruction*> WorkList;
69 for (Instruction &I: instructions(&F))
70 WorkList.insert(&I);
71
72 bool Changed = false;
73 const DataLayout &DL = F.getParent()->getDataLayout();
74 TargetLibraryInfo *TLI =
75 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
76
77 while (!WorkList.empty()) {
78 Instruction *I = *WorkList.begin();
79 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
80
81 if (!I->use_empty()) // Don't muck with dead instructions...
82 if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
83 // Add all of the users of this instruction to the worklist, they might
84 // be constant propagatable now...
85 for (User *U : I->users())
86 WorkList.insert(cast<Instruction>(U));
87
88 // Replace all of the uses of a variable with uses of the constant.
89 I->replaceAllUsesWith(C);
90
91 // Remove the dead instruction.
92 WorkList.erase(I);
93 I->eraseFromParent();
94
95 // We made a change to the function...
96 Changed = true;
97 ++NumInstKilled;
98 }
99 }
100 return Changed;
101 }
102