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