LLVM  4.0.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/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 // dump - call print(cerr);
123  print(dbgs(), nullptr);
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // ImmutablePass Implementation
128 //
129 // Force out-of-line virtual method.
131 
133  // By default, don't do anything.
134 }
135 
136 //===----------------------------------------------------------------------===//
137 // FunctionPass Implementation
138 //
139 
141  const std::string &Banner) const {
142  return createPrintFunctionPass(O, Banner);
143 }
144 
147 }
148 
150  if (!F.getContext().getOptBisect().shouldRunPass(this, F))
151  return true;
152 
153  if (F.hasFnAttribute(Attribute::OptimizeNone)) {
154  DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
155  << F.getName() << "\n");
156  return true;
157  }
158  return false;
159 }
160 
161 //===----------------------------------------------------------------------===//
162 // BasicBlockPass Implementation
163 //
164 
166  const std::string &Banner) const {
167  return createPrintBasicBlockPass(O, Banner);
168 }
169 
171  // By default, don't do anything.
172  return false;
173 }
174 
176  // By default, don't do anything.
177  return false;
178 }
179 
181  const Function *F = BB.getParent();
182  if (!F)
183  return false;
184  if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
185  return true;
186  if (F->hasFnAttribute(Attribute::OptimizeNone)) {
187  // Report this only once per function.
188  if (&BB == &F->getEntryBlock())
189  DEBUG(dbgs() << "Skipping pass '" << getPassName()
190  << "' on function " << F->getName() << "\n");
191  return true;
192  }
193  return false;
194 }
195 
198 }
199 
200 const PassInfo *Pass::lookupPassInfo(const void *TI) {
202 }
203 
206 }
207 
210  if (!PI)
211  return nullptr;
212  return PI->createPass();
213 }
214 
215 //===----------------------------------------------------------------------===//
216 // Analysis Group Implementation Code
217 //===----------------------------------------------------------------------===//
218 
219 // RegisterAGBase implementation
220 //
222  const void *PassID, bool isDefault)
223  : PassInfo(Name, InterfaceID) {
225  *this, isDefault);
226 }
227 
228 //===----------------------------------------------------------------------===//
229 // PassRegistrationListener implementation
230 //
231 
232 // enumeratePasses - Iterate over the registered passes, calling the
233 // passEnumerate callback on each PassInfo object.
234 //
237 }
238 
240  : cl::parser<const PassInfo *>(O) {
242 }
243 
245  // This only gets called during static destruction, in which case the
246  // PassRegistry will have already been destroyed by llvm_shutdown(). So
247  // attempting to remove the registration listener is an error.
248 }
249 
250 //===----------------------------------------------------------------------===//
251 // AnalysisUsage Class Implementation
252 //
253 
254 namespace {
255  struct GetCFGOnlyPasses : public PassRegistrationListener {
257  VectorType &CFGOnlyList;
258  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
259 
260  void passEnumerate(const PassInfo *P) override {
261  if (P->isCFGOnlyPass())
262  CFGOnlyList.push_back(P->getTypeInfo());
263  }
264  };
265 }
266 
267 // setPreservesCFG - This function should be called to by the pass, iff they do
268 // not:
269 //
270 // 1. Add or remove basic blocks from the function
271 // 2. Modify terminator instructions in any way.
272 //
273 // This function annotates the AnalysisUsage info object to say that analyses
274 // that only depend on the CFG are preserved by this pass.
275 //
277  // Since this transformation doesn't modify the CFG, it preserves all analyses
278  // that only depend on the CFG (like dominators, loop info, etc...)
279  GetCFGOnlyPasses(Preserved).enumeratePasses();
280 }
281 
283  const PassInfo *PI = Pass::lookupPassInfo(Arg);
284  // If the pass exists, preserve it. Otherwise silently do nothing.
285  if (PI) Preserved.push_back(PI->getTypeInfo());
286  return *this;
287 }
288 
290  Required.push_back(ID);
291  return *this;
292 }
293 
295  Required.push_back(&ID);
296  return *this;
297 }
298 
300  Required.push_back(&ID);
301  RequiredTransitive.push_back(&ID);
302  return *this;
303 }
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:211
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:235
bool shouldRunPass(const Pass *P, const UnitT &U)
Checks the bisect limit to determine if the specified pass should run.
Definition: OptBisect.cpp:96
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...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:226
PassNameParser(cl::Option &O)
Definition: Pass.cpp:239
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
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:52
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:103
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 verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:92
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:84
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:170
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
bool isCFGOnlyPass() const
isCFGOnlyPass - return true if this pass only looks at the CFG for the function.
Definition: PassInfo.h:91
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:78
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:67
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:221
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:75
void dump() const
Definition: Pass.cpp:122
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:132
bool skipBasicBlock(const BasicBlock &BB) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:180
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:108
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:115
~PassNameParser() override
Definition: Pass.cpp:244
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:165
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
MPPassManager.
Definition: Pass.h:56
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
#define F(x, y, z)
Definition: MD5.cpp:51
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:175
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:100
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:200
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:145
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Represent the analysis usage information of a pass.
uint32_t Offset
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:149
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
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:50
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:289
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:276
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
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:369
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
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:226
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:210
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:68
~ImmutablePass() override
Definition: Pass.cpp:130
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:299
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.
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:208
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:140
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:100
virtual void print(raw_ostream &O, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:117
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:196
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:79
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:222