LLVM  6.0.0svn
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/IR/Module.h"
21 #include "llvm/IR/OptBisect.h"
22 #include "llvm/PassRegistry.h"
23 #include "llvm/Support/Debug.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "ir"
28 
29 //===----------------------------------------------------------------------===//
30 // Pass Implementation
31 //
32 
33 // Force out-of-line virtual method.
35  delete Resolver;
36 }
37 
38 // Force out-of-line virtual method.
40 
42  const std::string &Banner) const {
43  return createPrintModulePass(O, Banner);
44 }
45 
47  return PMT_ModulePassManager;
48 }
49 
51  return !M.getContext().getOptBisect().shouldRunPass(this, M);
52 }
53 
54 bool Pass::mustPreserveAnalysisID(char &AID) const {
55  return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
56 }
57 
58 // dumpPassStructure - Implement the -debug-pass=Structure option
60  dbgs().indent(Offset*2) << getPassName() << "\n";
61 }
62 
63 /// getPassName - Return a nice clean name for a pass. This usually
64 /// implemented in terms of the name that is registered by one of the
65 /// Registration templates, but can be overloaded directly.
66 ///
68  AnalysisID AID = getPassID();
70  if (PI)
71  return PI->getPassName();
72  return "Unnamed pass: implement Pass::getPassName()";
73 }
74 
76  // By default, don't do anything.
77 }
78 
80  // Default implementation.
81  return PMT_Unknown;
82 }
83 
85  // By default, no analysis results are used, all are invalidated.
86 }
87 
89  // By default, don't do anything.
90 }
91 
92 void Pass::verifyAnalysis() const {
93  // By default, don't do anything.
94 }
95 
97  return this;
98 }
99 
101  return nullptr;
102 }
103 
105  return nullptr;
106 }
107 
109  assert(!Resolver && "Resolver is already set");
110  Resolver = AR;
111 }
112 
113 // print - Print out the internal state of the pass. This is called by Analyze
114 // to print out the contents of an analysis. Otherwise it is not necessary to
115 // implement this method.
116 //
117 void Pass::print(raw_ostream &O,const Module*) const {
118  O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
119 }
120 
121 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122 // dump - call print(cerr);
124  print(dbgs(), nullptr);
125 }
126 #endif
127 
128 //===----------------------------------------------------------------------===//
129 // ImmutablePass Implementation
130 //
131 // Force out-of-line virtual method.
133 
135  // By default, don't do anything.
136 }
137 
138 //===----------------------------------------------------------------------===//
139 // FunctionPass Implementation
140 //
141 
143  const std::string &Banner) const {
144  return createPrintFunctionPass(O, Banner);
145 }
146 
149 }
150 
152  if (!F.getContext().getOptBisect().shouldRunPass(this, F))
153  return true;
154 
155  if (F.hasFnAttribute(Attribute::OptimizeNone)) {
156  DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
157  << F.getName() << "\n");
158  return true;
159  }
160  return false;
161 }
162 
163 //===----------------------------------------------------------------------===//
164 // BasicBlockPass Implementation
165 //
166 
168  const std::string &Banner) const {
169  return createPrintBasicBlockPass(O, Banner);
170 }
171 
173  // By default, don't do anything.
174  return false;
175 }
176 
178  // By default, don't do anything.
179  return false;
180 }
181 
183  const Function *F = BB.getParent();
184  if (!F)
185  return false;
186  if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
187  return true;
188  if (F->hasFnAttribute(Attribute::OptimizeNone)) {
189  // Report this only once per function.
190  if (&BB == &F->getEntryBlock())
191  DEBUG(dbgs() << "Skipping pass '" << getPassName()
192  << "' on function " << F->getName() << "\n");
193  return true;
194  }
195  return false;
196 }
197 
200 }
201 
202 const PassInfo *Pass::lookupPassInfo(const void *TI) {
204 }
205 
208 }
209 
212  if (!PI)
213  return nullptr;
214  return PI->createPass();
215 }
216 
217 //===----------------------------------------------------------------------===//
218 // Analysis Group Implementation Code
219 //===----------------------------------------------------------------------===//
220 
221 // RegisterAGBase implementation
222 //
224  const void *PassID, bool isDefault)
225  : PassInfo(Name, InterfaceID) {
227  *this, isDefault);
228 }
229 
230 //===----------------------------------------------------------------------===//
231 // PassRegistrationListener implementation
232 //
233 
234 // enumeratePasses - Iterate over the registered passes, calling the
235 // passEnumerate callback on each PassInfo object.
236 //
239 }
240 
242  : cl::parser<const PassInfo *>(O) {
244 }
245 
247  // This only gets called during static destruction, in which case the
248  // PassRegistry will have already been destroyed by llvm_shutdown(). So
249  // attempting to remove the registration listener is an error.
250 }
251 
252 //===----------------------------------------------------------------------===//
253 // AnalysisUsage Class Implementation
254 //
255 
256 namespace {
257  struct GetCFGOnlyPasses : public PassRegistrationListener {
259  VectorType &CFGOnlyList;
260  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
261 
262  void passEnumerate(const PassInfo *P) override {
263  if (P->isCFGOnlyPass())
264  CFGOnlyList.push_back(P->getTypeInfo());
265  }
266  };
267 }
268 
269 // setPreservesCFG - This function should be called to by the pass, iff they do
270 // not:
271 //
272 // 1. Add or remove basic blocks from the function
273 // 2. Modify terminator instructions in any way.
274 //
275 // This function annotates the AnalysisUsage info object to say that analyses
276 // that only depend on the CFG are preserved by this pass.
277 //
279  // Since this transformation doesn't modify the CFG, it preserves all analyses
280  // that only depend on the CFG (like dominators, loop info, etc...)
281  GetCFGOnlyPasses(Preserved).enumeratePasses();
282 }
283 
285  const PassInfo *PI = Pass::lookupPassInfo(Arg);
286  // If the pass exists, preserve it. Otherwise silently do nothing.
287  if (PI) Preserved.push_back(PI->getTypeInfo());
288  return *this;
289 }
290 
292  Required.push_back(ID);
293  return *this;
294 }
295 
297  Required.push_back(&ID);
298  return *this;
299 }
300 
302  Required.push_back(&ID);
303  RequiredTransitive.push_back(&ID);
304  return *this;
305 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass&#39; corresponding PassInfo, indexed by the pass&#39; type identifier (&MyPass::...
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:54
BBPassManager.
Definition: Pass.h:61
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:237
bool shouldRunPass(const Pass *P, const UnitT &U)
Checks the bisect limit to determine if the specified pass should run.
Definition: OptBisect.cpp:97
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:241
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener&#39;s pass...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:104
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:59
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:88
virtual void print(raw_ostream &O, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:117
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:172
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:254
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:223
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:75
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:67
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:54
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:46
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:134
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:108
~PassNameParser() override
Definition: Pass.cpp:246
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:92
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:79
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:167
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
MPPassManager.
Definition: Pass.h:56
bool skipBasicBlock(const BasicBlock &BB) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:182
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:84
#define F(x, y, z)
Definition: MD5.cpp:55
void dump() const
Definition: Pass.cpp:123
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:73
virtual bool doFinalization(Function &)
doFinalization - Virtual method overriden by BasicBlockPass subclasses to do any post processing need...
Definition: Pass.cpp:177
const char * Name
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:100
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:202
const BasicBlock & getEntryBlock() const
Definition: Function.h:564
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:103
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:147
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Represent the analysis usage information of a pass.
void passEnumerate(const PassInfo *P) override
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
Return analysis result or null if it doesn&#39;t exist.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
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:32
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
FPPassManager.
Definition: Pass.h:58
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:291
Module.h This file contains the declarations for the Module class.
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:278
This file declares the interface for bisecting optimizations.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Class to represent vector types.
Definition: DerivedTypes.h:393
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:100
virtual ~Pass()
Definition: Pass.cpp:34
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:41
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:218
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:63
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:50
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:200
~ImmutablePass() override
Definition: Pass.cpp:132
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:301
bool isCFGOnlyPass() const
isCFGOnlyPass - return true if this pass only looks at the CFG for the function.
Definition: PassInfo.h:86
const void * AnalysisID
Definition: Pass.h:46
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.
This file defines passes to print out IR in various granularities.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:210
aarch64 promote const
~ModulePass() override
Definition: Pass.cpp:39
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:142
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:96
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
#define DEBUG(X)
Definition: Debug.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:198
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:151