1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a base class that indicates that a specified class is a
11 // transformation pass implementation.
13 // Passes are designed this way so that it is possible to run passes in a cache
14 // and organizationally optimal order without having to specify it at the front
15 // end. This allows arbitrary passes to be strung together and have them
16 // executed as efficiently as possible.
18 // Passes should extend one of the classes below, depending on the guarantees
19 // that it can make about what will be modified as it is run. For example, most
20 // global optimizations should derive from FunctionPass, because they do not add
21 // or delete functions, they operate on the internals of the function.
23 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
24 // bottom), so the APIs exposed by these files are also automatically available
25 // to all users of this file.
27 //===----------------------------------------------------------------------===//
32 #include "llvm/Support/Compiler.h"
44 class AnalysisResolver;
49 // AnalysisID - Use the PassInfo to identify a pass...
50 typedef const void* AnalysisID;
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.
55 enum PassManagerType {
57 PMT_ModulePassManager = 1, ///< MPPassManager
58 PMT_CallGraphPassManager, ///< CGPassManager
59 PMT_FunctionPassManager, ///< FPPassManager
60 PMT_LoopPassManager, ///< LPPassManager
61 PMT_RegionPassManager, ///< RGPassManager
62 PMT_BasicBlockPassManager, ///< BBPassManager
66 // Different types of passes.
77 //===----------------------------------------------------------------------===//
78 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
79 /// interprocedural optimization or you do not fit into any of the more
80 /// constrained passes described below.
83 AnalysisResolver *Resolver; // Used to resolve analysis
86 void operator=(const Pass&) = delete;
87 Pass(const Pass &) = delete;
90 explicit Pass(PassKind K, char &pid)
91 : Resolver(nullptr), PassID(&pid), Kind(K) { }
95 PassKind getPassKind() const { return Kind; }
97 /// getPassName - Return a nice clean name for a pass. This usually
98 /// implemented in terms of the name that is registered by one of the
99 /// Registration templates, but can be overloaded directly.
101 virtual const char *getPassName() const;
103 /// getPassID - Return the PassID number that corresponds to this pass.
104 AnalysisID getPassID() const {
108 /// doInitialization - Virtual method overridden by subclasses to do
109 /// any necessary initialization before any pass is run.
111 virtual bool doInitialization(Module &) { return false; }
113 /// doFinalization - Virtual method overriden by subclasses to do any
114 /// necessary clean up after all passes have run.
116 virtual bool doFinalization(Module &) { return false; }
118 /// print - Print out the internal state of the pass. This is called by
119 /// Analyze to print out the contents of an analysis. Otherwise it is not
120 /// necessary to implement this method. Beware that the module pointer MAY be
121 /// null. This automatically forwards to a virtual function that does not
122 /// provide the Module* in case the analysis doesn't need it it can just be
125 virtual void print(raw_ostream &O, const Module *M) const;
126 void dump() const; // dump - Print to stderr.
128 /// createPrinterPass - Get a Pass appropriate to print the IR this
129 /// pass operates on (Module, Function or MachineFunction).
130 virtual Pass *createPrinterPass(raw_ostream &O,
131 const std::string &Banner) const = 0;
133 /// Each pass is responsible for assigning a pass manager to itself.
134 /// PMS is the stack of available pass manager.
135 virtual void assignPassManager(PMStack &,
137 /// Check if available pass managers are suitable for this pass or not.
138 virtual void preparePassManager(PMStack &);
140 /// Return what kind of Pass Manager can manage this pass.
141 virtual PassManagerType getPotentialPassManagerType() const;
143 // Access AnalysisResolver
144 void setResolver(AnalysisResolver *AR);
145 AnalysisResolver *getResolver() const { return Resolver; }
147 /// getAnalysisUsage - This function should be overriden by passes that need
148 /// analysis information to do their job. If a pass specifies that it uses a
149 /// particular analysis result to this function, it can then use the
150 /// getAnalysis<AnalysisType>() function, below.
152 virtual void getAnalysisUsage(AnalysisUsage &) const;
154 /// releaseMemory() - This member can be implemented by a pass if it wants to
155 /// be able to release its memory when it is no longer needed. The default
156 /// behavior of passes is to hold onto memory for the entire duration of their
157 /// lifetime (which is the entire compile time). For pipelined passes, this
158 /// is not a big deal because that memory gets recycled every time the pass is
159 /// invoked on another program unit. For IP passes, it is more important to
160 /// free memory when it is unused.
162 /// Optionally implement this function to release pass memory when it is no
165 virtual void releaseMemory();
167 /// getAdjustedAnalysisPointer - This method is used when a pass implements
168 /// an analysis interface through multiple inheritance. If needed, it should
169 /// override this to adjust the this pointer as needed for the specified pass
171 virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
172 virtual ImmutablePass *getAsImmutablePass();
173 virtual PMDataManager *getAsPMDataManager();
175 /// verifyAnalysis() - This member can be implemented by a analysis pass to
176 /// check state of analysis information.
177 virtual void verifyAnalysis() const;
179 // dumpPassStructure - Implement the -debug-passes=PassStructure option
180 virtual void dumpPassStructure(unsigned Offset = 0);
182 // lookupPassInfo - Return the pass info object for the specified pass class,
183 // or null if it is not known.
184 static const PassInfo *lookupPassInfo(const void *TI);
186 // lookupPassInfo - Return the pass info object for the pass with the given
187 // argument string, or null if it is not known.
188 static const PassInfo *lookupPassInfo(StringRef Arg);
190 // createPass - Create a object for the specified pass class,
191 // or null if it is not known.
192 static Pass *createPass(AnalysisID ID);
194 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
195 /// get analysis information that might be around, for example to update it.
196 /// This is different than getAnalysis in that it can fail (if the analysis
197 /// results haven't been computed), so should only be used if you can handle
198 /// the case when the analysis is not available. This method is often used by
199 /// transformation APIs to update analysis results for a pass automatically as
200 /// the transform is performed.
202 template<typename AnalysisType> AnalysisType *
203 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
205 /// mustPreserveAnalysisID - This method serves the same function as
206 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
207 /// obviously cannot give you a properly typed instance of the class if you
208 /// don't have the class name available (use getAnalysisIfAvailable if you
209 /// do), but it can tell you if you need to preserve the pass at least.
211 bool mustPreserveAnalysisID(char &AID) const;
213 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
214 /// to the analysis information that they claim to use by overriding the
215 /// getAnalysisUsage function.
217 template<typename AnalysisType>
218 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
220 template<typename AnalysisType>
221 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
223 template<typename AnalysisType>
224 AnalysisType &getAnalysisID(AnalysisID PI) const;
226 template<typename AnalysisType>
227 AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
231 //===----------------------------------------------------------------------===//
232 /// ModulePass class - This class is used to implement unstructured
233 /// interprocedural optimizations and analyses. ModulePasses may do anything
234 /// they want to the program.
236 class ModulePass : public Pass {
238 /// createPrinterPass - Get a module printer pass.
239 Pass *createPrinterPass(raw_ostream &O,
240 const std::string &Banner) const override;
242 /// runOnModule - Virtual method overriden by subclasses to process the module
243 /// being operated on.
244 virtual bool runOnModule(Module &M) = 0;
246 void assignPassManager(PMStack &PMS, PassManagerType T) override;
248 /// Return what kind of Pass Manager can manage this pass.
249 PassManagerType getPotentialPassManagerType() const override;
251 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
252 // Force out-of-line virtual method.
253 ~ModulePass() override;
257 //===----------------------------------------------------------------------===//
258 /// ImmutablePass class - This class is used to provide information that does
259 /// not need to be run. This is useful for things like target information and
260 /// "basic" versions of AnalysisGroups.
262 class ImmutablePass : public ModulePass {
264 /// initializePass - This method may be overriden by immutable passes to allow
265 /// them to perform various initialization actions they require. This is
266 /// primarily because an ImmutablePass can "require" another ImmutablePass,
267 /// and if it does, the overloaded version of initializePass may get access to
268 /// these passes with getAnalysis<>.
270 virtual void initializePass();
272 ImmutablePass *getAsImmutablePass() override { return this; }
274 /// ImmutablePasses are never run.
276 bool runOnModule(Module &) override { return false; }
278 explicit ImmutablePass(char &pid)
281 // Force out-of-line virtual method.
282 ~ImmutablePass() override;
285 //===----------------------------------------------------------------------===//
286 /// FunctionPass class - This class is used to implement most global
287 /// optimizations. Optimizations should subclass this class if they meet the
288 /// following constraints:
290 /// 1. Optimizations are organized globally, i.e., a function at a time
291 /// 2. Optimizing a function does not cause the addition or removal of any
292 /// functions in the module
294 class FunctionPass : public Pass {
296 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
298 /// createPrinterPass - Get a function printer pass.
299 Pass *createPrinterPass(raw_ostream &O,
300 const std::string &Banner) const override;
302 /// runOnFunction - Virtual method overriden by subclasses to do the
303 /// per-function processing of the pass.
305 virtual bool runOnFunction(Function &F) = 0;
307 void assignPassManager(PMStack &PMS, PassManagerType T) override;
309 /// Return what kind of Pass Manager can manage this pass.
310 PassManagerType getPotentialPassManagerType() const override;
313 /// skipOptnoneFunction - This function has Attribute::OptimizeNone
314 /// and most transformation passes should skip it.
315 bool skipOptnoneFunction(const Function &F) const;
320 //===----------------------------------------------------------------------===//
321 /// BasicBlockPass class - This class is used to implement most local
322 /// optimizations. Optimizations should subclass this class if they
323 /// meet the following constraints:
324 /// 1. Optimizations are local, operating on either a basic block or
325 /// instruction at a time.
326 /// 2. Optimizations do not modify the CFG of the contained function, or any
327 /// other basic block in the function.
328 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
330 class BasicBlockPass : public Pass {
332 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
334 /// createPrinterPass - Get a basic block printer pass.
335 Pass *createPrinterPass(raw_ostream &O,
336 const std::string &Banner) const override;
338 using llvm::Pass::doInitialization;
339 using llvm::Pass::doFinalization;
341 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
342 /// to do any necessary per-function initialization.
344 virtual bool doInitialization(Function &);
346 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
347 /// per-basicblock processing of the pass.
349 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
351 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
352 /// do any post processing needed after all passes have run.
354 virtual bool doFinalization(Function &);
356 void assignPassManager(PMStack &PMS, PassManagerType T) override;
358 /// Return what kind of Pass Manager can manage this pass.
359 PassManagerType getPotentialPassManagerType() const override;
362 /// skipOptnoneFunction - Containing function has Attribute::OptimizeNone
363 /// and most transformation passes should skip it.
364 bool skipOptnoneFunction(const BasicBlock &BB) const;
367 /// If the user specifies the -time-passes argument on an LLVM tool command line
368 /// then the value of this boolean will be true, otherwise false.
369 /// @brief This is the storage for the -time-passes option.
370 extern bool TimePassesIsEnabled;
372 } // End llvm namespace
374 // Include support files that contain important APIs commonly used by Passes,
375 // but that we want to separate out to make it easier to read the header files.
377 #include "llvm/PassSupport.h"
378 #include "llvm/PassAnalysisSupport.h"