LLVM 20.0.0git
Pass.h
Go to the documentation of this file.
1//===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
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 defines a base class that indicates that a specified class is a
10// transformation pass implementation.
11//
12// Passes are designed this way so that it is possible to run passes in a cache
13// and organizationally optimal order without having to specify it at the front
14// end. This allows arbitrary passes to be strung together and have them
15// executed as efficiently as possible.
16//
17// Passes should extend one of the classes below, depending on the guarantees
18// that it can make about what will be modified as it is run. For example, most
19// global optimizations should derive from FunctionPass, because they do not add
20// or delete functions, they operate on the internals of the function.
21//
22// Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
23// bottom), so the APIs exposed by these files are also automatically available
24// to all users of this file.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_PASS_H
29#define LLVM_PASS_H
30
31#ifdef EXPENSIVE_CHECKS
32#include <cstdint>
33#endif
34#include <string>
35
36namespace llvm {
37
38class AnalysisResolver;
39class AnalysisUsage;
40class Function;
41class ImmutablePass;
42class Module;
43class PassInfo;
44class PMDataManager;
45class PMStack;
46class raw_ostream;
47class StringRef;
48
49// AnalysisID - Use the PassInfo to identify a pass...
50using AnalysisID = const void *;
51
52/// Different types of internal pass managers. External pass managers
53/// (PassManager and FunctionPassManager) are not represented here.
54/// Ordering of pass manager types is important here.
57 PMT_ModulePassManager = 1, ///< MPPassManager
58 PMT_CallGraphPassManager, ///< CGPassManager
59 PMT_FunctionPassManager, ///< FPPassManager
60 PMT_LoopPassManager, ///< LPPassManager
61 PMT_RegionPassManager, ///< RGPassManager
63};
64
65// Different types of passes.
73};
74
75/// This enumerates the LLVM full LTO or ThinLTO optimization phases.
77 /// No LTO/ThinLTO behavior needed.
78 None,
79 /// ThinLTO prelink (summary) phase.
81 /// ThinLTO postlink (backend compile) phase.
83 /// Full LTO prelink phase.
85 /// Full LTO postlink (backend compile) phase.
87};
88
89//===----------------------------------------------------------------------===//
90/// Pass interface - Implemented by all 'passes'. Subclass this if you are an
91/// interprocedural optimization or you do not fit into any of the more
92/// constrained passes described below.
93///
94class Pass {
95 AnalysisResolver *Resolver = nullptr; // Used to resolve analysis
96 const void *PassID;
97 PassKind Kind;
98
99public:
100 explicit Pass(PassKind K, char &pid) : PassID(&pid), Kind(K) {}
101 Pass(const Pass &) = delete;
102 Pass &operator=(const Pass &) = delete;
103 virtual ~Pass();
104
105 PassKind getPassKind() const { return Kind; }
106
107 /// getPassName - Return a nice clean name for a pass. This usually
108 /// implemented in terms of the name that is registered by one of the
109 /// Registration templates, but can be overloaded directly.
110 virtual StringRef getPassName() const;
111
112 /// getPassID - Return the PassID number that corresponds to this pass.
114 return PassID;
115 }
116
117 /// doInitialization - Virtual method overridden by subclasses to do
118 /// any necessary initialization before any pass is run.
119 virtual bool doInitialization(Module &) { return false; }
120
121 /// doFinalization - Virtual method overriden by subclasses to do any
122 /// necessary clean up after all passes have run.
123 virtual bool doFinalization(Module &) { return false; }
124
125 /// print - Print out the internal state of the pass. This is called by
126 /// Analyze to print out the contents of an analysis. Otherwise it is not
127 /// necessary to implement this method. Beware that the module pointer MAY be
128 /// null. This automatically forwards to a virtual function that does not
129 /// provide the Module* in case the analysis doesn't need it it can just be
130 /// ignored.
131 virtual void print(raw_ostream &OS, const Module *M) const;
132
133 void dump() const; // dump - Print to stderr.
134
135 /// createPrinterPass - Get a Pass appropriate to print the IR this
136 /// pass operates on (Module, Function or MachineFunction).
138 const std::string &Banner) const = 0;
139
140 /// Each pass is responsible for assigning a pass manager to itself.
141 /// PMS is the stack of available pass manager.
144
145 /// Check if available pass managers are suitable for this pass or not.
146 virtual void preparePassManager(PMStack &);
147
148 /// Return what kind of Pass Manager can manage this pass.
150
151 // Access AnalysisResolver
154
155 /// getAnalysisUsage - This function should be overriden by passes that need
156 /// analysis information to do their job. If a pass specifies that it uses a
157 /// particular analysis result to this function, it can then use the
158 /// getAnalysis<AnalysisType>() function, below.
159 virtual void getAnalysisUsage(AnalysisUsage &) const;
160
161 /// releaseMemory() - This member can be implemented by a pass if it wants to
162 /// be able to release its memory when it is no longer needed. The default
163 /// behavior of passes is to hold onto memory for the entire duration of their
164 /// lifetime (which is the entire compile time). For pipelined passes, this
165 /// is not a big deal because that memory gets recycled every time the pass is
166 /// invoked on another program unit. For IP passes, it is more important to
167 /// free memory when it is unused.
168 ///
169 /// Optionally implement this function to release pass memory when it is no
170 /// longer used.
171 virtual void releaseMemory();
172
173 /// getAdjustedAnalysisPointer - This method is used when a pass implements
174 /// an analysis interface through multiple inheritance. If needed, it should
175 /// override this to adjust the this pointer as needed for the specified pass
176 /// info.
180
181 /// verifyAnalysis() - This member can be implemented by a analysis pass to
182 /// check state of analysis information.
183 virtual void verifyAnalysis() const;
184
185 // dumpPassStructure - Implement the -debug-passes=PassStructure option
186 virtual void dumpPassStructure(unsigned Offset = 0);
187
188 // lookupPassInfo - Return the pass info object for the specified pass class,
189 // or null if it is not known.
190 static const PassInfo *lookupPassInfo(const void *TI);
191
192 // lookupPassInfo - Return the pass info object for the pass with the given
193 // argument string, or null if it is not known.
194 static const PassInfo *lookupPassInfo(StringRef Arg);
195
196 // createPass - Create a object for the specified pass class,
197 // or null if it is not known.
198 static Pass *createPass(AnalysisID ID);
199
200 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
201 /// get analysis information that might be around, for example to update it.
202 /// This is different than getAnalysis in that it can fail (if the analysis
203 /// results haven't been computed), so should only be used if you can handle
204 /// the case when the analysis is not available. This method is often used by
205 /// transformation APIs to update analysis results for a pass automatically as
206 /// the transform is performed.
207 template<typename AnalysisType> AnalysisType *
208 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
209
210 /// mustPreserveAnalysisID - This method serves the same function as
211 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
212 /// obviously cannot give you a properly typed instance of the class if you
213 /// don't have the class name available (use getAnalysisIfAvailable if you
214 /// do), but it can tell you if you need to preserve the pass at least.
215 bool mustPreserveAnalysisID(char &AID) const;
216
217 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
218 /// to the analysis information that they claim to use by overriding the
219 /// getAnalysisUsage function.
220 template<typename AnalysisType>
221 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
222
223 template <typename AnalysisType>
224 AnalysisType &
226 bool *Changed = nullptr); // Defined in PassAnalysisSupport.h
227
228 template<typename AnalysisType>
229 AnalysisType &getAnalysisID(AnalysisID PI) const;
230
231 template <typename AnalysisType>
232 AnalysisType &getAnalysisID(AnalysisID PI, Function &F,
233 bool *Changed = nullptr);
234
235#ifdef EXPENSIVE_CHECKS
236 /// Hash a module in order to detect when a module (or more specific) pass has
237 /// modified it.
238 uint64_t structuralHash(Module &M) const;
239
240 /// Hash a function in order to detect when a function (or more specific) pass
241 /// has modified it.
242 virtual uint64_t structuralHash(Function &F) const;
243#endif
244};
245
246//===----------------------------------------------------------------------===//
247/// ModulePass class - This class is used to implement unstructured
248/// interprocedural optimizations and analyses. ModulePasses may do anything
249/// they want to the program.
250///
251class ModulePass : public Pass {
252public:
253 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
254
255 // Force out-of-line virtual method.
256 ~ModulePass() override;
257
258 /// createPrinterPass - Get a module printer pass.
260 const std::string &Banner) const override;
261
262 /// runOnModule - Virtual method overriden by subclasses to process the module
263 /// being operated on.
264 virtual bool runOnModule(Module &M) = 0;
265
266 void assignPassManager(PMStack &PMS, PassManagerType T) override;
267
268 /// Return what kind of Pass Manager can manage this pass.
270
271protected:
272 /// Optional passes call this function to check whether the pass should be
273 /// skipped. This is the case when optimization bisect is over the limit.
274 bool skipModule(Module &M) const;
275};
276
277//===----------------------------------------------------------------------===//
278/// ImmutablePass class - This class is used to provide information that does
279/// not need to be run. This is useful for things like target information.
280///
281class ImmutablePass : public ModulePass {
282public:
283 explicit ImmutablePass(char &pid) : ModulePass(pid) {}
284
285 // Force out-of-line virtual method.
286 ~ImmutablePass() override;
287
288 /// initializePass - This method may be overriden by immutable passes to allow
289 /// them to perform various initialization actions they require. This is
290 /// primarily because an ImmutablePass can "require" another ImmutablePass,
291 /// and if it does, the overloaded version of initializePass may get access to
292 /// these passes with getAnalysis<>.
293 virtual void initializePass();
294
295 ImmutablePass *getAsImmutablePass() override { return this; }
296
297 /// ImmutablePasses are never run.
298 bool runOnModule(Module &) override { return false; }
299};
300
301//===----------------------------------------------------------------------===//
302/// FunctionPass class - This class is used to implement most global
303/// optimizations. Optimizations should subclass this class if they meet the
304/// following constraints:
305///
306/// 1. Optimizations are organized globally, i.e., a function at a time
307/// 2. Optimizing a function does not cause the addition or removal of any
308/// functions in the module
309///
310class FunctionPass : public Pass {
311public:
312 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
313
314 /// createPrinterPass - Get a function printer pass.
316 const std::string &Banner) const override;
317
318 /// runOnFunction - Virtual method overriden by subclasses to do the
319 /// per-function processing of the pass.
320 virtual bool runOnFunction(Function &F) = 0;
321
322 void assignPassManager(PMStack &PMS, PassManagerType T) override;
323
324 /// Return what kind of Pass Manager can manage this pass.
326
327protected:
328 /// Optional passes call this function to check whether the pass should be
329 /// skipped. This is the case when Attribute::OptimizeNone is set or when
330 /// optimization bisect is over the limit.
331 bool skipFunction(const Function &F) const;
332};
333
334/// If the user specifies the -time-passes argument on an LLVM tool command line
335/// then the value of this boolean will be true, otherwise false.
336/// This is the storage for the -time-passes option.
337extern bool TimePassesIsEnabled;
338/// If TimePassesPerRun is true, there would be one line of report for
339/// each pass invocation.
340/// If TimePassesPerRun is false, there would be only one line of
341/// report for each pass (even there are more than one pass objects).
342/// (For new pass manager only)
343extern bool TimePassesPerRun;
344
345} // end namespace llvm
346
347// Include support files that contain important APIs commonly used by Passes,
348// but that we want to separate out to make it easier to read the header files.
350#include "llvm/PassSupport.h"
351
352#endif // LLVM_PASS_H
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
raw_pwrite_stream & OS
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:170
void assignPassManager(PMStack &PMS, PassManagerType T) override
Find appropriate Function Pass Manager or Call Graph Pass Manager in the PM Stack and add self into t...
FunctionPass(char &pid)
Definition: Pass.h:312
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:165
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:281
ImmutablePass(char &pid)
Definition: Pass.h:283
~ImmutablePass() override
ImmutablePass * getAsImmutablePass() override
Definition: Pass.h:295
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:157
bool runOnModule(Module &) override
ImmutablePasses are never run.
Definition: Pass.h:298
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:55
ModulePass(char &pid)
Definition: Pass.h:253
~ModulePass() override
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:63
void assignPassManager(PMStack &PMS, PassManagerType T) override
Find appropriate Module Pass Manager in the PM Stack and add self into that manager.
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
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
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
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
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
Pass(PassKind K, char &pid)
Definition: Pass.h:100
void dump() const
Definition: Pass.cpp:136
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:122
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:200
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:118
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:113
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
AnalysisType & getAnalysisID(AnalysisID PI) const
virtual void assignPassManager(PMStack &, PassManagerType)
Each pass is responsible for assigning a pass manager to itself.
Definition: Pass.h:142
AnalysisResolver * getResolver() const
Definition: Pass.h:153
PassKind getPassKind() const
Definition: Pass.h:105
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:192
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:119
virtual ~Pass()
Definition: Pass.cpp:43
virtual Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const =0
createPrinterPass - Get a Pass appropriate to print the IR this pass operates on (Module,...
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:123
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
Pass(const Pass &)=delete
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
Pass & operator=(const Pass &)=delete
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2203
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
PassKind
Definition: Pass.h:66
@ PT_Region
Definition: Pass.h:67
@ PT_CallGraphSCC
Definition: Pass.h:70
@ PT_Module
Definition: Pass.h:71
@ PT_Function
Definition: Pass.h:69
@ PT_Loop
Definition: Pass.h:68
@ PT_PassManager
Definition: Pass.h:72
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:55
@ PMT_LoopPassManager
LPPassManager.
Definition: Pass.h:60
@ PMT_RegionPassManager
RGPassManager.
Definition: Pass.h:61
@ PMT_Unknown
Definition: Pass.h:56
@ PMT_Last
Definition: Pass.h:62
@ PMT_CallGraphPassManager
CGPassManager.
Definition: Pass.h:58
@ PMT_ModulePassManager
MPPassManager.
Definition: Pass.h:57
@ PMT_FunctionPassManager
FPPassManager.
Definition: Pass.h:59
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
bool TimePassesPerRun
If TimePassesPerRun is true, there would be one line of report for each pass invocation.
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition: Pass.h:76
@ FullLTOPreLink
Full LTO prelink phase.
@ ThinLTOPostLink
ThinLTO postlink (backend compile) phase.
@ FullLTOPostLink
Full LTO postlink (backend compile) phase.
@ ThinLTOPreLink
ThinLTO prelink (summary) phase.
@ None
Definition: CodeGenData.h:106
const void * AnalysisID
Definition: Pass.h:50