LLVM  3.7.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"
22 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/InstIterator.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/Pass.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
39  ConstantPropagation() : FunctionPass(ID) {
41  }
42 
43  bool runOnFunction(Function &F) override;
44 
45  void getAnalysisUsage(AnalysisUsage &AU) const override {
46  AU.setPreservesCFG();
48  }
49  };
50 }
51 
53 INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54  "Simple constant propagation", false, false)
56 INITIALIZE_PASS_END(ConstantPropagation, "constprop",
57  "Simple constant propagation", false, false)
58 
60  return new ConstantPropagation();
61 }
62 
63 bool ConstantPropagation::runOnFunction(Function &F) {
64  // Initialize the worklist to all of the instructions ready to process...
65  std::set<Instruction*> WorkList;
66  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
67  WorkList.insert(&*i);
68  }
69  bool Changed = false;
70  const DataLayout &DL = F.getParent()->getDataLayout();
71  TargetLibraryInfo *TLI =
72  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
73 
74  while (!WorkList.empty()) {
75  Instruction *I = *WorkList.begin();
76  WorkList.erase(WorkList.begin()); // Get an element from the worklist...
77 
78  if (!I->use_empty()) // Don't muck with dead instructions...
79  if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
80  // Add all of the users of this instruction to the worklist, they might
81  // be constant propagatable now...
82  for (User *U : I->users())
83  WorkList.insert(cast<Instruction>(U));
84 
85  // Replace all of the uses of a variable with uses of the constant.
86  I->replaceAllUsesWith(C);
87 
88  // Remove the dead instruction.
89  WorkList.erase(I);
90  I->eraseFromParent();
91 
92  // We made a change to the function...
93  Changed = true;
94  ++NumInstKilled;
95  }
96  }
97  return Changed;
98 }
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:127
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
FunctionPass * createConstantPropagationPass()
Simple constant false
void initializeConstantPropagationPass(PassRegistry &)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
This is an important base class in LLVM.
Definition: Constant.h:41
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.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
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:263
iterator_range< user_iterator > users()
Definition: Value.h:300
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
Simple constant propagation
#define I(x, y, z)
Definition: MD5.cpp:54
constprop
bool use_empty() const
Definition: Value.h:275
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:128