LLVM 17.0.0git
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/Function.h"
19#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/OptBisect.h"
23#include "llvm/PassInfo.h"
24#include "llvm/PassRegistry.h"
26#include "llvm/Support/Debug.h"
28#include <cassert>
29
30#ifdef EXPENSIVE_CHECKS
32#endif
33
34using 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.
48ModulePass::~ModulePass() = default;
49
51 const std::string &Banner) const {
52 return createPrintModulePass(OS, Banner);
53}
54
57}
58
59static std::string getDescription(const Module &M) {
60 return "module (" + M.getName().str() + ")";
61}
62
64 OptPassGate &Gate = M.getContext().getOptPassGate();
65 return Gate.isEnabled() &&
66 !Gate.shouldRunPass(this->getPassName(), getDescription(M));
67}
68
69bool Pass::mustPreserveAnalysisID(char &AID) const {
70 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
71}
72
73// dumpPassStructure - Implement the -debug-pass=Structure option
75 dbgs().indent(Offset*2) << getPassName() << "\n";
76}
77
78/// getPassName - Return a nice clean name for a pass. This usually
79/// implemented in terms of the name that is registered by one of the
80/// Registration templates, but can be overloaded directly.
82 AnalysisID AID = getPassID();
84 if (PI)
85 return PI->getPassName();
86 return "Unnamed pass: implement Pass::getPassName()";
87}
88
90 // By default, don't do anything.
91}
92
94 // Default implementation.
95 return PMT_Unknown;
96}
97
99 // By default, no analysis results are used, all are invalidated.
100}
101
103 // By default, don't do anything.
104}
105
107 // By default, don't do anything.
108}
109
111 return this;
112}
113
115 return nullptr;
116}
117
119 return nullptr;
120}
121
123 assert(!Resolver && "Resolver is already set");
124 Resolver = AR;
125}
126
127// print - Print out the internal state of the pass. This is called by Analyze
128// to print out the contents of an analysis. Otherwise it is not necessary to
129// implement this method.
130void Pass::print(raw_ostream &OS, const Module *) const {
131 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
132}
133
134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135// dump - call print(cerr);
137 print(dbgs(), nullptr);
138}
139#endif
140
141#ifdef EXPENSIVE_CHECKS
142uint64_t Pass::structuralHash(Module &M) const { return StructuralHash(M); }
143
144uint64_t Pass::structuralHash(Function &F) const { return StructuralHash(F); }
145#endif
146
147//===----------------------------------------------------------------------===//
148// ImmutablePass Implementation
149//
150// Force out-of-line virtual method.
152
154 // By default, don't do anything.
155}
156
157//===----------------------------------------------------------------------===//
158// FunctionPass Implementation
159//
160
162 const std::string &Banner) const {
163 return createPrintFunctionPass(OS, Banner);
164}
165
168}
169
170static std::string getDescription(const Function &F) {
171 return "function (" + F.getName().str() + ")";
172}
173
175 OptPassGate &Gate = F.getContext().getOptPassGate();
176 if (Gate.isEnabled() &&
177 !Gate.shouldRunPass(this->getPassName(), getDescription(F)))
178 return true;
179
180 if (F.hasOptNone()) {
181 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
182 << F.getName() << "\n");
183 return true;
184 }
185 return false;
186}
187
188const PassInfo *Pass::lookupPassInfo(const void *TI) {
190}
191
194}
195
198 if (!PI)
199 return nullptr;
200 return PI->createPass();
201}
202
203//===----------------------------------------------------------------------===//
204// Analysis Group Implementation Code
205//===----------------------------------------------------------------------===//
206
207// RegisterAGBase implementation
208
210 const void *PassID, bool isDefault)
211 : PassInfo(Name, InterfaceID) {
213 *this, isDefault);
214}
215
216//===----------------------------------------------------------------------===//
217// PassRegistrationListener implementation
218//
219
220// enumeratePasses - Iterate over the registered passes, calling the
221// passEnumerate callback on each PassInfo object.
224}
225
227 : cl::parser<const PassInfo *>(O) {
229}
230
231// This only gets called during static destruction, in which case the
232// PassRegistry will have already been destroyed by llvm_shutdown(). So
233// attempting to remove the registration listener is an error.
235
236//===----------------------------------------------------------------------===//
237// AnalysisUsage Class Implementation
238//
239
240namespace {
241
242struct GetCFGOnlyPasses : public PassRegistrationListener {
244
245 VectorType &CFGOnlyList;
246
247 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
248
249 void passEnumerate(const PassInfo *P) override {
250 if (P->isCFGOnlyPass())
251 CFGOnlyList.push_back(P->getTypeInfo());
252 }
253};
254
255} // end anonymous namespace
256
257// setPreservesCFG - This function should be called to by the pass, iff they do
258// not:
259//
260// 1. Add or remove basic blocks from the function
261// 2. Modify terminator instructions in any way.
262//
263// This function annotates the AnalysisUsage info object to say that analyses
264// that only depend on the CFG are preserved by this pass.
266 // Since this transformation doesn't modify the CFG, it preserves all analyses
267 // that only depend on the CFG (like dominators, loop info, etc...)
268 GetCFGOnlyPasses(Preserved).enumeratePasses();
269}
270
272 const PassInfo *PI = Pass::lookupPassInfo(Arg);
273 // If the pass exists, preserve it. Otherwise silently do nothing.
274 if (PI)
275 pushUnique(Preserved, PI->getTypeInfo());
276 return *this;
277}
278
280 pushUnique(Required, ID);
281 return *this;
282}
283
285 pushUnique(Required, &ID);
286 return *this;
287}
288
290 pushUnique(Required, &ID);
291 pushUnique(RequiredTransitive, &ID);
292 return *this;
293}
aarch64 promote const
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static std::string getDescription(const CallGraphSCC &SCC)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
This file contains an interface for creating legacy passes to print out IR in various granularities.
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
This file declares the interface for bisecting optimizations.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
Represent the analysis usage information of a pass.
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:279
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:289
SmallVectorImpl< AnalysisID > VectorType
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:166
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:161
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:174
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:279
~ImmutablePass() override
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:153
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:55
~ModulePass() override
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:63
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:50
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:26
virtual bool isEnabled() const
isEnabled() should return true before calling shouldRunPass().
Definition: OptBisect.h:38
virtual bool shouldRunPass(const StringRef PassName, StringRef IRDescription)
IRDescription is a textual description of the IR unit the pass is running over.
Definition: OptBisect.h:32
PMDataManager provides the common place to manage the analysis data used by pass managers.
PMStack - This class implements a stack data structure of PMDataManager pointers.
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:62
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:96
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass... TODO : Rename
Definition: PassInfo.h:71
PassNameParser(cl::Option &O)
Definition: Pass.cpp:226
~PassNameParser() override
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' 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
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:110
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:93
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable,...
Definition: Pass.cpp:69
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:122
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:196
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:118
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:110
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
void dump() const
Definition: Pass.cpp:136
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:89
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:188
virtual ~Pass()
Definition: Pass.cpp:43
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:106
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:74
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:114
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:102
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:209
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2137
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Base class of all SIMD vector types.
Definition: DerivedTypes.h:389
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:52
@ PMT_Unknown
Definition: Pass.h:53
@ PMT_ModulePassManager
MPPassManager.
Definition: Pass.h:54
@ PMT_FunctionPassManager
FPPassManager.
Definition: Pass.h:56
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
const void * AnalysisID
Definition: Pass.h:47
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.
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.
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:215
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:222