LLVM  3.7.0
Pass.cpp
Go to the documentation of this file.
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
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 the LLVM Pass infrastructure. It is primarily
11 // responsible with ensuring that passes are executed and batched together
12 // optimally.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Pass.h"
17 #include "llvm/IR/Function.h"
20 #include "llvm/PassRegistry.h"
21 #include "llvm/Support/Debug.h"
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "ir"
26 
27 //===----------------------------------------------------------------------===//
28 // Pass Implementation
29 //
30 
31 // Force out-of-line virtual method.
33  delete Resolver;
34 }
35 
36 // Force out-of-line virtual method.
38 
40  const std::string &Banner) const {
41  return createPrintModulePass(O, Banner);
42 }
43 
45  return PMT_ModulePassManager;
46 }
47 
48 bool Pass::mustPreserveAnalysisID(char &AID) const {
49  return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
50 }
51 
52 // dumpPassStructure - Implement the -debug-pass=Structure option
53 void Pass::dumpPassStructure(unsigned Offset) {
54  dbgs().indent(Offset*2) << getPassName() << "\n";
55 }
56 
57 /// getPassName - Return a nice clean name for a pass. This usually
58 /// implemented in terms of the name that is registered by one of the
59 /// Registration templates, but can be overloaded directly.
60 ///
61 const char *Pass::getPassName() const {
62  AnalysisID AID = getPassID();
64  if (PI)
65  return PI->getPassName();
66  return "Unnamed pass: implement Pass::getPassName()";
67 }
68 
70  // By default, don't do anything.
71 }
72 
74  // Default implementation.
75  return PMT_Unknown;
76 }
77 
79  // By default, no analysis results are used, all are invalidated.
80 }
81 
83  // By default, don't do anything.
84 }
85 
86 void Pass::verifyAnalysis() const {
87  // By default, don't do anything.
88 }
89 
91  return this;
92 }
93 
95  return nullptr;
96 }
97 
99  return nullptr;
100 }
101 
103  assert(!Resolver && "Resolver is already set");
104  Resolver = AR;
105 }
106 
107 // print - Print out the internal state of the pass. This is called by Analyze
108 // to print out the contents of an analysis. Otherwise it is not necessary to
109 // implement this method.
110 //
111 void Pass::print(raw_ostream &O,const Module*) const {
112  O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
113 }
114 
115 // dump - call print(cerr);
116 void Pass::dump() const {
117  print(dbgs(), nullptr);
118 }
119 
120 //===----------------------------------------------------------------------===//
121 // ImmutablePass Implementation
122 //
123 // Force out-of-line virtual method.
125 
127  // By default, don't do anything.
128 }
129 
130 //===----------------------------------------------------------------------===//
131 // FunctionPass Implementation
132 //
133 
135  const std::string &Banner) const {
136  return createPrintFunctionPass(O, Banner);
137 }
138 
141 }
142 
145  DEBUG(dbgs() << "Skipping pass '" << getPassName()
146  << "' on function " << F.getName() << "\n");
147  return true;
148  }
149  return false;
150 }
151 
152 //===----------------------------------------------------------------------===//
153 // BasicBlockPass Implementation
154 //
155 
157  const std::string &Banner) const {
158  return createPrintBasicBlockPass(O, Banner);
159 }
160 
162  // By default, don't do anything.
163  return false;
164 }
165 
167  // By default, don't do anything.
168  return false;
169 }
170 
172  const Function *F = BB.getParent();
174  // Report this only once per function.
175  if (&BB == &F->getEntryBlock())
176  DEBUG(dbgs() << "Skipping pass '" << getPassName()
177  << "' on function " << F->getName() << "\n");
178  return true;
179  }
180  return false;
181 }
182 
185 }
186 
187 const PassInfo *Pass::lookupPassInfo(const void *TI) {
189 }
190 
193 }
194 
197  if (!PI)
198  return nullptr;
199  return PI->createPass();
200 }
201 
202 //===----------------------------------------------------------------------===//
203 // Analysis Group Implementation Code
204 //===----------------------------------------------------------------------===//
205 
206 // RegisterAGBase implementation
207 //
208 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
209  const void *PassID, bool isDefault)
210  : PassInfo(Name, InterfaceID) {
212  *this, isDefault);
213 }
214 
215 //===----------------------------------------------------------------------===//
216 // PassRegistrationListener implementation
217 //
218 
219 // enumeratePasses - Iterate over the registered passes, calling the
220 // passEnumerate callback on each PassInfo object.
221 //
224 }
225 
227  : cl::parser<const PassInfo *>(O) {
229 }
230 
232  // This only gets called during static destruction, in which case the
233  // PassRegistry will have already been destroyed by llvm_shutdown(). So
234  // attempting to remove the registration listener is an error.
235 }
236 
237 //===----------------------------------------------------------------------===//
238 // AnalysisUsage Class Implementation
239 //
240 
241 namespace {
242  struct GetCFGOnlyPasses : public PassRegistrationListener {
244  VectorType &CFGOnlyList;
245  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
246 
247  void passEnumerate(const PassInfo *P) override {
248  if (P->isCFGOnlyPass())
249  CFGOnlyList.push_back(P->getTypeInfo());
250  }
251  };
252 }
253 
254 // setPreservesCFG - This function should be called to by the pass, iff they do
255 // not:
256 //
257 // 1. Add or remove basic blocks from the function
258 // 2. Modify terminator instructions in any way.
259 //
260 // This function annotates the AnalysisUsage info object to say that analyses
261 // that only depend on the CFG are preserved by this pass.
262 //
264  // Since this transformation doesn't modify the CFG, it preserves all analyses
265  // that only depend on the CFG (like dominators, loop info, etc...)
266  GetCFGOnlyPasses(Preserved).enumeratePasses();
267 }
268 
270  const PassInfo *PI = Pass::lookupPassInfo(Arg);
271  // If the pass exists, preserve it. Otherwise silently do nothing.
272  if (PI) Preserved.push_back(PI->getTypeInfo());
273  return *this;
274 }
275 
277  Required.push_back(ID);
278  return *this;
279 }
280 
282  Required.push_back(&ID);
283  return *this;
284 }
285 
287  Required.push_back(&ID);
288  RequiredTransitive.push_back(&ID);
289  return *this;
290 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
void push_back(const T &Elt)
Definition: SmallVector.h:222
void registerAnalysisGroup(const void *InterfaceID, const void *PassID, PassInfo &Registeree, bool isDefault, bool ShouldFree=false)
registerAnalysisGroup - Register an analysis group (or a pass implementing
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:55
BBPassManager.
Definition: Pass.h:62
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:222
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassNameParser(cl::Option &O)
Definition: Pass.cpp:226
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
const char * getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:68
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:98
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:53
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:104
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: Pass.cpp:82
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:86
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:78
FunctionPass * createPrintFunctionPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that prints functions to the specified raw_ostream as they are processed...
virtual bool doInitialization(Function &)
doInitialization - Virtual method overridden by BasicBlockPass subclasses to do any necessary per-fun...
Definition: Pass.cpp:161
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
virtual const char * getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:61
bool isCFGOnlyPass() const
isCFGOnlyPass - return true if this pass only looks at the CFG for the function.
Definition: PassInfo.h:93
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:78
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
F(f)
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:69
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:44
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:126
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:102
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:117
~PassNameParser() override
Definition: Pass.cpp:231
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
RegisterAGBase(const char *Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:208
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:156
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable, but works if you just have an AnalysisID.
Definition: Pass.cpp:48
MPPassManager.
Definition: Pass.h:57
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
Return analysis result or null if it doesn't exist.
virtual bool doFinalization(Function &)
doFinalization - Virtual method overriden by BasicBlockPass subclasses to do any post processing need...
Definition: Pass.cpp:166
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:94
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:187
bool skipOptnoneFunction(const BasicBlock &BB) const
skipOptnoneFunction - Containing function has Attribute::OptimizeNone and most transformation passes ...
Definition: Pass.cpp:171
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:139
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Represent the analysis usage information of a pass.
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:30
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
FPPassManager.
Definition: Pass.h:59
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:276
BasicBlockPass * createPrintBasicBlockPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that writes the BB to the specified raw_ostream.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
bool skipOptnoneFunction(const Function &F) const
skipOptnoneFunction - This function has Attribute::OptimizeNone and most transformation passes should...
Definition: Pass.cpp:143
Function must not be optimized.
Definition: Attributes.h:98
virtual ~Pass()
Definition: Pass.cpp:32
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:39
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:217
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:226
~ImmutablePass() override
Definition: Pass.cpp:124
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:286
const void * AnalysisID
Definition: Pass.h:47
PMDataManager provides the common place to manage the analysis data used by pass managers.
ModulePass * createPrintModulePass(raw_ostream &OS, const std::string &Banner="", bool ShouldPreserveUseListOrder=false)
Create and return a pass that writes the module to the specified raw_ostream.
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' type identifier (&MyPass::...
This file defines passes to print out IR in various granularities.
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:195
aarch64 promote const
~ModulePass() override
Definition: Pass.cpp:37
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:134
void dump() const
Definition: Pass.cpp:116
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:90
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
#define DEBUG(X)
Definition: Debug.h:92
virtual void print(raw_ostream &O, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:183
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:73