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