LLVM  4.0.0
ConstantProp.cpp
Go to the documentation of this file.
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"
23 #include "llvm/ADT/Statistic.h"
25 #include "llvm/IR/Constant.h"
26 #include "llvm/IR/InstIterator.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/Pass.h"
30 #include <set>
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "constprop"
34 
35 STATISTIC(NumInstKilled, "Number of instructions killed");
36 
37 namespace {
38  struct ConstantPropagation : public FunctionPass {
39  static char ID; // Pass identification, replacement for typeid
40  ConstantPropagation() : FunctionPass(ID) {
42  }
43 
44  bool runOnFunction(Function &F) override;
45 
46  void getAnalysisUsage(AnalysisUsage &AU) const override {
47  AU.setPreservesCFG();
49  }
50  };
51 }
52 
54 INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
55  "Simple constant propagation", false, false)
57 INITIALIZE_PASS_END(ConstantPropagation, "constprop",
58  "Simple constant propagation", false, false)
59 
61  return new ConstantPropagation();
62 }
63 
64 bool ConstantPropagation::runOnFunction(Function &F) {
65  if (skipFunction(F))
66  return false;
67 
68  // Initialize the worklist to all of the instructions ready to process...
69  std::set<Instruction*> WorkList;
70  for (Instruction &I: instructions(&F))
71  WorkList.insert(&I);
72 
73  bool Changed = false;
74  const DataLayout &DL = F.getParent()->getDataLayout();
75  TargetLibraryInfo *TLI =
76  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
77 
78  while (!WorkList.empty()) {
79  Instruction *I = *WorkList.begin();
80  WorkList.erase(WorkList.begin()); // Get an element from the worklist...
81 
82  if (!I->use_empty()) // Don't muck with dead instructions...
83  if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
84  // Add all of the users of this instruction to the worklist, they might
85  // be constant propagatable now...
86  for (User *U : I->users())
87  WorkList.insert(cast<Instruction>(U));
88 
89  // Replace all of the uses of a variable with uses of the constant.
91 
92  // Remove the dead instruction.
93  WorkList.erase(I);
94  if (isInstructionTriviallyDead(I, TLI)) {
95  I->eraseFromParent();
96  ++NumInstKilled;
97  }
98 
99  // We made a change to the function...
100  Changed = true;
101  }
102  }
103  return Changed;
104 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:76
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
FunctionPass * createConstantPropagationPass()
Simple constant false
#define F(x, y, z)
Definition: MD5.cpp:51
void initializeConstantPropagationPass(PassRegistry &)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
This is an important base class in LLVM.
Definition: Constant.h:42
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
INITIALIZE_PASS_BEGIN(ConstantPropagation,"constprop","Simple constant propagation", false, false) INITIALIZE_PASS_END(ConstantPropagation
Provides information about what library functions are available for the current target.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
iterator_range< user_iterator > users()
Definition: Value.h:370
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
Simple constant propagation
#define I(x, y, z)
Definition: MD5.cpp:54
constprop
bool use_empty() const
Definition: Value.h:299
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:288
inst_range instructions(Function *F)
Definition: InstIterator.h:132