LLVM  10.0.0svn
Pass.cpp
Go to the documentation of this file.
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LLVM Pass infrastructure. It is primarily
10 // responsible with ensuring that passes are executed and batched together
11 // optimally.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Pass.h"
16 #include "llvm/Config/llvm-config.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 
58 static std::string getDescription(const Module &M) {
59  return "module (" + M.getName().str() + ")";
60 }
61 
64  return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
65 }
66 
67 bool Pass::mustPreserveAnalysisID(char &AID) const {
68  return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
69 }
70 
71 // dumpPassStructure - Implement the -debug-pass=Structure option
73  dbgs().indent(Offset*2) << getPassName() << "\n";
74 }
75 
76 /// getPassName - Return a nice clean name for a pass. This usually
77 /// implemented in terms of the name that is registered by one of the
78 /// Registration templates, but can be overloaded directly.
80  AnalysisID AID = getPassID();
82  if (PI)
83  return PI->getPassName();
84  return "Unnamed pass: implement Pass::getPassName()";
85 }
86 
88  // By default, don't do anything.
89 }
90 
92  // Default implementation.
93  return PMT_Unknown;
94 }
95 
97  // By default, no analysis results are used, all are invalidated.
98 }
99 
101  // By default, don't do anything.
102 }
103 
104 void Pass::verifyAnalysis() const {
105  // By default, don't do anything.
106 }
107 
109  return this;
110 }
111 
113  return nullptr;
114 }
115 
117  return nullptr;
118 }
119 
121  assert(!Resolver && "Resolver is already set");
122  Resolver = AR;
123 }
124 
125 // print - Print out the internal state of the pass. This is called by Analyze
126 // to print out the contents of an analysis. Otherwise it is not necessary to
127 // implement this method.
128 void Pass::print(raw_ostream &OS, const Module *) const {
129  OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
130 }
131 
132 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
133 // dump - call print(cerr);
135  print(dbgs(), nullptr);
136 }
137 #endif
138 
139 //===----------------------------------------------------------------------===//
140 // ImmutablePass Implementation
141 //
142 // Force out-of-line virtual method.
144 
146  // By default, don't do anything.
147 }
148 
149 //===----------------------------------------------------------------------===//
150 // FunctionPass Implementation
151 //
152 
154  const std::string &Banner) const {
155  return createPrintFunctionPass(OS, Banner);
156 }
157 
160 }
161 
162 static std::string getDescription(const Function &F) {
163  return "function (" + F.getName().str() + ")";
164 }
165 
167  OptPassGate &Gate = F.getContext().getOptPassGate();
168  if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
169  return true;
170 
171  if (F.hasOptNone()) {
172  LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
173  << F.getName() << "\n");
174  return true;
175  }
176  return false;
177 }
178 
179 //===----------------------------------------------------------------------===//
180 // BasicBlockPass Implementation
181 //
182 
184  const std::string &Banner) const {
185  return createPrintBasicBlockPass(OS, Banner);
186 }
187 
189  // By default, don't do anything.
190  return false;
191 }
192 
194  // By default, don't do anything.
195  return false;
196 }
197 
198 static std::string getDescription(const BasicBlock &BB) {
199  return "basic block (" + BB.getName().str() + ") in function (" +
200  BB.getParent()->getName().str() + ")";
201 }
202 
204  const Function *F = BB.getParent();
205  if (!F)
206  return false;
207  OptPassGate &Gate = F->getContext().getOptPassGate();
208  if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(BB)))
209  return true;
210  if (F->hasOptNone()) {
211  // Report this only once per function.
212  if (&BB == &F->getEntryBlock())
213  LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
214  << "' on function " << F->getName() << "\n");
215  return true;
216  }
217  return false;
218 }
219 
222 }
223 
224 const PassInfo *Pass::lookupPassInfo(const void *TI) {
226 }
227 
230 }
231 
234  if (!PI)
235  return nullptr;
236  return PI->createPass();
237 }
238 
239 //===----------------------------------------------------------------------===//
240 // Analysis Group Implementation Code
241 //===----------------------------------------------------------------------===//
242 
243 // RegisterAGBase implementation
244 
246  const void *PassID, bool isDefault)
247  : PassInfo(Name, InterfaceID) {
249  *this, isDefault);
250 }
251 
252 //===----------------------------------------------------------------------===//
253 // PassRegistrationListener implementation
254 //
255 
256 // enumeratePasses - Iterate over the registered passes, calling the
257 // passEnumerate callback on each PassInfo object.
260 }
261 
263  : cl::parser<const PassInfo *>(O) {
265 }
266 
267 // This only gets called during static destruction, in which case the
268 // PassRegistry will have already been destroyed by llvm_shutdown(). So
269 // attempting to remove the registration listener is an error.
271 
272 //===----------------------------------------------------------------------===//
273 // AnalysisUsage Class Implementation
274 //
275 
276 namespace {
277 
278 struct GetCFGOnlyPasses : public PassRegistrationListener {
280 
281  VectorType &CFGOnlyList;
282 
283  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
284 
285  void passEnumerate(const PassInfo *P) override {
286  if (P->isCFGOnlyPass())
287  CFGOnlyList.push_back(P->getTypeInfo());
288  }
289 };
290 
291 } // end anonymous namespace
292 
293 // setPreservesCFG - This function should be called to by the pass, iff they do
294 // not:
295 //
296 // 1. Add or remove basic blocks from the function
297 // 2. Modify terminator instructions in any way.
298 //
299 // This function annotates the AnalysisUsage info object to say that analyses
300 // that only depend on the CFG are preserved by this pass.
302  // Since this transformation doesn't modify the CFG, it preserves all analyses
303  // that only depend on the CFG (like dominators, loop info, etc...)
304  GetCFGOnlyPasses(Preserved).enumeratePasses();
305 }
306 
308  const PassInfo *PI = Pass::lookupPassInfo(Arg);
309  // If the pass exists, preserve it. Otherwise silently do nothing.
310  if (PI) Preserved.push_back(PI->getTypeInfo());
311  return *this;
312 }
313 
315  Required.push_back(ID);
316  return *this;
317 }
318 
320  Required.push_back(&ID);
321  return *this;
322 }
323 
325  Required.push_back(&ID);
326  RequiredTransitive.push_back(&ID);
327  return *this;
328 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
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:53
BBPassManager.
Definition: Pass.h:60
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:258
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...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:232
PassNameParser(cl::Option &O)
Definition: Pass.cpp:262
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:23
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:616
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:484
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:116
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:72
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:100
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:25
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
SmallVectorImpl< AnalysisID > VectorType
static std::string getDescription(const Module &M)
Definition: Pass.cpp:58
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:188
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:153
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:227
F(f)
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:245
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:87
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:79
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:67
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:145
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:120
virtual bool shouldRunPass(const Pass *P, StringRef IRDescription)
IRDescription is a textual description of the IR unit the pass is running over.
Definition: OptBisect.h:31
~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:104
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:91
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:55
bool skipBasicBlock(const BasicBlock &BB) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:203
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
virtual bool isEnabled() const
isEnabled should return true before calling shouldRunPass
Definition: OptBisect.h:36
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:96
void dump() const
Definition: Pass.cpp:134
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:71
virtual bool doFinalization(Function &)
doFinalization - Virtual method overriden by BasicBlockPass subclasses to do any post processing need...
Definition: Pass.cpp:193
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:112
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:224
const BasicBlock & getEntryBlock() const
Definition: Function.h:664
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:99
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:49
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1864
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:158
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...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
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:255
FPPassManager.
Definition: Pass.h:57
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:314
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:301
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:432
const void * AnalysisID
Definition: Pass.h:48
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:96
virtual ~Pass()
Definition: Pass.cpp:42
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:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:62
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:196
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:128
~ImmutablePass() override
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:324
bool isCFGOnlyPass() const
isCFGOnlyPass - return true if this pass only looks at the CFG for the function.
Definition: PassInfo.h:83
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:232
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:108
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:183
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:220
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:166
#define LLVM_DEBUG(X)
Definition: Debug.h:122