1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a base class that indicates that a specified class is a
10 // transformation pass implementation.
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.
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.
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.
26 //===----------------------------------------------------------------------===//
35 class AnalysisResolver;
46 // AnalysisID - Use the PassInfo to identify a pass...
47 using AnalysisID = const void *;
49 /// Different types of internal pass managers. External pass managers
50 /// (PassManager and FunctionPassManager) are not represented here.
51 /// Ordering of pass manager types is important here.
52 enum PassManagerType {
54 PMT_ModulePassManager = 1, ///< MPPassManager
55 PMT_CallGraphPassManager, ///< CGPassManager
56 PMT_FunctionPassManager, ///< FPPassManager
57 PMT_LoopPassManager, ///< LPPassManager
58 PMT_RegionPassManager, ///< RGPassManager
62 // Different types of passes.
72 //===----------------------------------------------------------------------===//
73 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
74 /// interprocedural optimization or you do not fit into any of the more
75 /// constrained passes described below.
78 AnalysisResolver *Resolver = nullptr; // Used to resolve analysis
83 explicit Pass(PassKind K, char &pid) : PassID(&pid), Kind(K) {}
84 Pass(const Pass &) = delete;
85 Pass &operator=(const Pass &) = delete;
88 PassKind getPassKind() const { return Kind; }
90 /// getPassName - Return a nice clean name for a pass. This usually
91 /// implemented in terms of the name that is registered by one of the
92 /// Registration templates, but can be overloaded directly.
93 virtual StringRef getPassName() const;
95 /// getPassID - Return the PassID number that corresponds to this pass.
96 AnalysisID getPassID() const {
100 /// doInitialization - Virtual method overridden by subclasses to do
101 /// any necessary initialization before any pass is run.
102 virtual bool doInitialization(Module &) { return false; }
104 /// doFinalization - Virtual method overriden by subclasses to do any
105 /// necessary clean up after all passes have run.
106 virtual bool doFinalization(Module &) { return false; }
108 /// print - Print out the internal state of the pass. This is called by
109 /// Analyze to print out the contents of an analysis. Otherwise it is not
110 /// necessary to implement this method. Beware that the module pointer MAY be
111 /// null. This automatically forwards to a virtual function that does not
112 /// provide the Module* in case the analysis doesn't need it it can just be
114 virtual void print(raw_ostream &OS, const Module *M) const;
116 void dump() const; // dump - Print to stderr.
118 /// createPrinterPass - Get a Pass appropriate to print the IR this
119 /// pass operates on (Module, Function or MachineFunction).
120 virtual Pass *createPrinterPass(raw_ostream &OS,
121 const std::string &Banner) const = 0;
123 /// Each pass is responsible for assigning a pass manager to itself.
124 /// PMS is the stack of available pass manager.
125 virtual void assignPassManager(PMStack &,
128 /// Check if available pass managers are suitable for this pass or not.
129 virtual void preparePassManager(PMStack &);
131 /// Return what kind of Pass Manager can manage this pass.
132 virtual PassManagerType getPotentialPassManagerType() const;
134 // Access AnalysisResolver
135 void setResolver(AnalysisResolver *AR);
136 AnalysisResolver *getResolver() const { return Resolver; }
138 /// getAnalysisUsage - This function should be overriden by passes that need
139 /// analysis information to do their job. If a pass specifies that it uses a
140 /// particular analysis result to this function, it can then use the
141 /// getAnalysis<AnalysisType>() function, below.
142 virtual void getAnalysisUsage(AnalysisUsage &) const;
144 /// releaseMemory() - This member can be implemented by a pass if it wants to
145 /// be able to release its memory when it is no longer needed. The default
146 /// behavior of passes is to hold onto memory for the entire duration of their
147 /// lifetime (which is the entire compile time). For pipelined passes, this
148 /// is not a big deal because that memory gets recycled every time the pass is
149 /// invoked on another program unit. For IP passes, it is more important to
150 /// free memory when it is unused.
152 /// Optionally implement this function to release pass memory when it is no
154 virtual void releaseMemory();
156 /// getAdjustedAnalysisPointer - This method is used when a pass implements
157 /// an analysis interface through multiple inheritance. If needed, it should
158 /// override this to adjust the this pointer as needed for the specified pass
160 virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
161 virtual ImmutablePass *getAsImmutablePass();
162 virtual PMDataManager *getAsPMDataManager();
164 /// verifyAnalysis() - This member can be implemented by a analysis pass to
165 /// check state of analysis information.
166 virtual void verifyAnalysis() const;
168 // dumpPassStructure - Implement the -debug-passes=PassStructure option
169 virtual void dumpPassStructure(unsigned Offset = 0);
171 // lookupPassInfo - Return the pass info object for the specified pass class,
172 // or null if it is not known.
173 static const PassInfo *lookupPassInfo(const void *TI);
175 // lookupPassInfo - Return the pass info object for the pass with the given
176 // argument string, or null if it is not known.
177 static const PassInfo *lookupPassInfo(StringRef Arg);
179 // createPass - Create a object for the specified pass class,
180 // or null if it is not known.
181 static Pass *createPass(AnalysisID ID);
183 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
184 /// get analysis information that might be around, for example to update it.
185 /// This is different than getAnalysis in that it can fail (if the analysis
186 /// results haven't been computed), so should only be used if you can handle
187 /// the case when the analysis is not available. This method is often used by
188 /// transformation APIs to update analysis results for a pass automatically as
189 /// the transform is performed.
190 template<typename AnalysisType> AnalysisType *
191 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
193 /// mustPreserveAnalysisID - This method serves the same function as
194 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
195 /// obviously cannot give you a properly typed instance of the class if you
196 /// don't have the class name available (use getAnalysisIfAvailable if you
197 /// do), but it can tell you if you need to preserve the pass at least.
198 bool mustPreserveAnalysisID(char &AID) const;
200 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
201 /// to the analysis information that they claim to use by overriding the
202 /// getAnalysisUsage function.
203 template<typename AnalysisType>
204 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
206 template <typename AnalysisType>
208 getAnalysis(Function &F,
209 bool *Changed = nullptr); // Defined in PassAnalysisSupport.h
211 template<typename AnalysisType>
212 AnalysisType &getAnalysisID(AnalysisID PI) const;
214 template <typename AnalysisType>
215 AnalysisType &getAnalysisID(AnalysisID PI, Function &F,
216 bool *Changed = nullptr);
219 //===----------------------------------------------------------------------===//
220 /// ModulePass class - This class is used to implement unstructured
221 /// interprocedural optimizations and analyses. ModulePasses may do anything
222 /// they want to the program.
224 class ModulePass : public Pass {
226 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
228 // Force out-of-line virtual method.
229 ~ModulePass() override;
231 /// createPrinterPass - Get a module printer pass.
232 Pass *createPrinterPass(raw_ostream &OS,
233 const std::string &Banner) const override;
235 /// runOnModule - Virtual method overriden by subclasses to process the module
236 /// being operated on.
237 virtual bool runOnModule(Module &M) = 0;
239 void assignPassManager(PMStack &PMS, PassManagerType T) override;
241 /// Return what kind of Pass Manager can manage this pass.
242 PassManagerType getPotentialPassManagerType() const override;
245 /// Optional passes call this function to check whether the pass should be
246 /// skipped. This is the case when optimization bisect is over the limit.
247 bool skipModule(Module &M) const;
250 //===----------------------------------------------------------------------===//
251 /// ImmutablePass class - This class is used to provide information that does
252 /// not need to be run. This is useful for things like target information and
253 /// "basic" versions of AnalysisGroups.
255 class ImmutablePass : public ModulePass {
257 explicit ImmutablePass(char &pid) : ModulePass(pid) {}
259 // Force out-of-line virtual method.
260 ~ImmutablePass() override;
262 /// initializePass - This method may be overriden by immutable passes to allow
263 /// them to perform various initialization actions they require. This is
264 /// primarily because an ImmutablePass can "require" another ImmutablePass,
265 /// and if it does, the overloaded version of initializePass may get access to
266 /// these passes with getAnalysis<>.
267 virtual void initializePass();
269 ImmutablePass *getAsImmutablePass() override { return this; }
271 /// ImmutablePasses are never run.
272 bool runOnModule(Module &) override { return false; }
275 //===----------------------------------------------------------------------===//
276 /// FunctionPass class - This class is used to implement most global
277 /// optimizations. Optimizations should subclass this class if they meet the
278 /// following constraints:
280 /// 1. Optimizations are organized globally, i.e., a function at a time
281 /// 2. Optimizing a function does not cause the addition or removal of any
282 /// functions in the module
284 class FunctionPass : public Pass {
286 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
288 /// createPrinterPass - Get a function printer pass.
289 Pass *createPrinterPass(raw_ostream &OS,
290 const std::string &Banner) const override;
292 /// runOnFunction - Virtual method overriden by subclasses to do the
293 /// per-function processing of the pass.
294 virtual bool runOnFunction(Function &F) = 0;
296 void assignPassManager(PMStack &PMS, PassManagerType T) override;
298 /// Return what kind of Pass Manager can manage this pass.
299 PassManagerType getPotentialPassManagerType() const override;
302 /// Optional passes call this function to check whether the pass should be
303 /// skipped. This is the case when Attribute::OptimizeNone is set or when
304 /// optimization bisect is over the limit.
305 bool skipFunction(const Function &F) const;
308 /// If the user specifies the -time-passes argument on an LLVM tool command line
309 /// then the value of this boolean will be true, otherwise false.
310 /// This is the storage for the -time-passes option.
311 extern bool TimePassesIsEnabled;
313 } // end namespace llvm
315 // Include support files that contain important APIs commonly used by Passes,
316 // but that we want to separate out to make it easier to read the header files.
317 #include "llvm/PassAnalysisSupport.h"
318 #include "llvm/PassSupport.h"
320 #endif // LLVM_PASS_H