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/Attributes.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/Function.h"
21 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/OptBisect.h"
25 #include "llvm/PassInfo.h"
26 #include "llvm/PassRegistry.h"
27 #include "llvm/PassSupport.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/Debug.h"
31 #include <cassert>
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "ir"
36 
37 //===----------------------------------------------------------------------===//
38 // Pass Implementation
39 //
40 
41 // Force out-of-line virtual method.
43  delete Resolver;
44 }
45 
46 // Force out-of-line virtual method.
47 ModulePass::~ModulePass() = default;
48 
50  const std::string &Banner) const {
51  return createPrintModulePass(OS, Banner);
52 }
53 
55  return PMT_ModulePassManager;
56 }
57 
59  return !M.getContext().getOptBisect().shouldRunPass(this, M);
60 }
61 
62 bool Pass::mustPreserveAnalysisID(char &AID) const {
63  return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
64 }
65 
66 // dumpPassStructure - Implement the -debug-pass=Structure option
68  dbgs().indent(Offset*2) << getPassName() << "\n";
69 }
70 
71 /// getPassName - Return a nice clean name for a pass. This usually
72 /// implemented in terms of the name that is registered by one of the
73 /// Registration templates, but can be overloaded directly.
75  AnalysisID AID = getPassID();
77  if (PI)
78  return PI->getPassName();
79  return "Unnamed pass: implement Pass::getPassName()";
80 }
81 
83  // By default, don't do anything.
84 }
85 
87  // Default implementation.
88  return PMT_Unknown;
89 }
90 
92  // By default, no analysis results are used, all are invalidated.
93 }
94 
96  // By default, don't do anything.
97 }
98 
99 void Pass::verifyAnalysis() const {
100  // By default, don't do anything.
101 }
102 
104  return this;
105 }
106 
108  return nullptr;
109 }
110 
112  return nullptr;
113 }
114 
116  assert(!Resolver && "Resolver is already set");
117  Resolver = AR;
118 }
119 
120 // print - Print out the internal state of the pass. This is called by Analyze
121 // to print out the contents of an analysis. Otherwise it is not necessary to
122 // implement this method.
123 void Pass::print(raw_ostream &OS, const Module *) const {
124  OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
125 }
126 
127 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
128 // dump - call print(cerr);
130  print(dbgs(), nullptr);
131 }
132 #endif
133 
134 //===----------------------------------------------------------------------===//
135 // ImmutablePass Implementation
136 //
137 // Force out-of-line virtual method.
139 
141  // By default, don't do anything.
142 }
143 
144 //===----------------------------------------------------------------------===//
145 // FunctionPass Implementation
146 //
147 
149  const std::string &Banner) const {
150  return createPrintFunctionPass(OS, Banner);
151 }
152 
155 }
156 
158  if (!F.getContext().getOptBisect().shouldRunPass(this, F))
159  return true;
160 
161  if (F.hasFnAttribute(Attribute::OptimizeNone)) {
162  DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
163  << F.getName() << "\n");
164  return true;
165  }
166  return false;
167 }
168 
169 //===----------------------------------------------------------------------===//
170 // BasicBlockPass Implementation
171 //
172 
174  const std::string &Banner) const {
175  return createPrintBasicBlockPass(OS, Banner);
176 }
177 
179  // By default, don't do anything.
180  return false;
181 }
182 
184  // By default, don't do anything.
185  return false;
186 }
187 
189  const Function *F = BB.getParent();
190  if (!F)
191  return false;
192  if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
193  return true;
194  if (F->hasFnAttribute(Attribute::OptimizeNone)) {
195  // Report this only once per function.
196  if (&BB == &F->getEntryBlock())
197  DEBUG(dbgs() << "Skipping pass '" << getPassName()
198  << "' on function " << F->getName() << "\n");
199  return true;
200  }
201  return false;
202 }
203 
206 }
207 
208 const PassInfo *Pass::lookupPassInfo(const void *TI) {
210 }
211 
214 }
215 
218  if (!PI)
219  return nullptr;
220  return PI->createPass();
221 }
222 
223 //===----------------------------------------------------------------------===//
224 // Analysis Group Implementation Code
225 //===----------------------------------------------------------------------===//
226 
227 // RegisterAGBase implementation
228 
230  const void *PassID, bool isDefault)
231  : PassInfo(Name, InterfaceID) {
233  *this, isDefault);
234 }
235 
236 //===----------------------------------------------------------------------===//
237 // PassRegistrationListener implementation
238 //
239 
240 // enumeratePasses - Iterate over the registered passes, calling the
241 // passEnumerate callback on each PassInfo object.
244 }
245 
247  : cl::parser<const PassInfo *>(O) {
249 }
250 
251 // This only gets called during static destruction, in which case the
252 // PassRegistry will have already been destroyed by llvm_shutdown(). So
253 // attempting to remove the registration listener is an error.
255 
256 //===----------------------------------------------------------------------===//
257 // AnalysisUsage Class Implementation
258 //
259 
260 namespace {
261 
262 struct GetCFGOnlyPasses : public PassRegistrationListener {
264 
265  VectorType &CFGOnlyList;
266 
267  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
268 
269  void passEnumerate(const PassInfo *P) override {
270  if (P->isCFGOnlyPass())
271  CFGOnlyList.push_back(P->getTypeInfo());
272  }
273 };
274 
275 } // end anonymous namespace
276 
277 // setPreservesCFG - This function should be called to by the pass, iff they do
278 // not:
279 //
280 // 1. Add or remove basic blocks from the function
281 // 2. Modify terminator instructions in any way.
282 //
283 // This function annotates the AnalysisUsage info object to say that analyses
284 // that only depend on the CFG are preserved by this pass.
286  // Since this transformation doesn't modify the CFG, it preserves all analyses
287  // that only depend on the CFG (like dominators, loop info, etc...)
288  GetCFGOnlyPasses(Preserved).enumeratePasses();
289 }
290 
292  const PassInfo *PI = Pass::lookupPassInfo(Arg);
293  // If the pass exists, preserve it. Otherwise silently do nothing.
294  if (PI) Preserved.push_back(PI->getTypeInfo());
295  return *this;
296 }
297 
299  Required.push_back(ID);
300  return *this;
301 }
302 
304  Required.push_back(&ID);
305  return *this;
306 }
307 
309  Required.push_back(&ID);
310  RequiredTransitive.push_back(&ID);
311  return *this;
312 }
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:242
bool shouldRunPass(const Pass *P, const UnitT &U)
Checks the bisect limit to determine if the specified pass should run.
Definition: OptBisect.cpp:104
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:246
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:111
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:67
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:95
SmallVectorImpl< AnalysisID > VectorType
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:178
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:148
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:262
F(f)
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:229
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:82
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:74
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:62
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:54
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:140
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:115
~PassNameParser() override
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:99
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:86
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
This file contains the simple types necessary to represent the attributes associated with functions a...
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:188
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:91
void dump() const
Definition: Pass.cpp:129
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:72
virtual bool doFinalization(Function &)
doFinalization - Virtual method overriden by BasicBlockPass subclasses to do any post processing need...
Definition: Pass.cpp:183
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:107
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:208
const BasicBlock & getEntryBlock() const
Definition: Function.h:572
#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:100
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:49
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:153
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:31
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:256
FPPassManager.
Definition: Pass.h:58
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:298
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:285
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
const void * AnalysisID
Definition: Pass.h:49
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:97
virtual ~Pass()
Definition: Pass.cpp:42
amdgpu Simplify well known AMD library false Value Value * Arg
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:58
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:197
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:123
~ImmutablePass() override
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:308
bool isCFGOnlyPass() const
isCFGOnlyPass - return true if this pass only looks at the CFG for the function.
Definition: PassInfo.h:84
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:216
aarch64 promote const
~ModulePass() override
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:103
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
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:173
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:204
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:157