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 effeciently 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 //===----------------------------------------------------------------------===//
43 class AnalysisResolver;
48 // AnalysisID - Use the PassInfo to identify a pass...
49 typedef const void* AnalysisID;
51 /// Different types of internal pass managers. External pass managers
52 /// (PassManager and FunctionPassManager) are not represented here.
53 /// Ordering of pass manager types is important here.
54 enum PassManagerType {
56 PMT_ModulePassManager = 1, ///< MPPassManager
57 PMT_CallGraphPassManager, ///< CGPassManager
58 PMT_FunctionPassManager, ///< FPPassManager
59 PMT_LoopPassManager, ///< LPPassManager
60 PMT_RegionPassManager, ///< RGPassManager
61 PMT_BasicBlockPassManager, ///< BBPassManager
65 // Different types of passes.
76 //===----------------------------------------------------------------------===//
77 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
78 /// interprocedural optimization or you do not fit into any of the more
79 /// constrained passes described below.
82 AnalysisResolver *Resolver; // Used to resolve analysis
85 void operator=(const Pass&); // DO NOT IMPLEMENT
86 Pass(const Pass &); // DO NOT IMPLEMENT
89 explicit Pass(PassKind K, char &pid);
93 PassKind getPassKind() const { return Kind; }
95 /// getPassName - Return a nice clean name for a pass. This usually
96 /// implemented in terms of the name that is registered by one of the
97 /// Registration templates, but can be overloaded directly.
99 virtual const char *getPassName() const;
101 /// getPassID - Return the PassID number that corresponds to this pass.
102 virtual AnalysisID getPassID() const {
106 /// print - Print out the internal state of the pass. This is called by
107 /// Analyze to print out the contents of an analysis. Otherwise it is not
108 /// necessary to implement this method. Beware that the module pointer MAY be
109 /// null. This automatically forwards to a virtual function that does not
110 /// provide the Module* in case the analysis doesn't need it it can just be
113 virtual void print(raw_ostream &O, const Module *M) const;
114 void dump() const; // dump - Print to stderr.
116 /// createPrinterPass - Get a Pass appropriate to print the IR this
117 /// pass operates one (Module, Function or MachineFunction).
118 virtual Pass *createPrinterPass(raw_ostream &O,
119 const std::string &Banner) const = 0;
121 /// Each pass is responsible for assigning a pass manager to itself.
122 /// PMS is the stack of available pass manager.
123 virtual void assignPassManager(PMStack &,
125 /// Check if available pass managers are suitable for this pass or not.
126 virtual void preparePassManager(PMStack &);
128 /// Return what kind of Pass Manager can manage this pass.
129 virtual PassManagerType getPotentialPassManagerType() const;
131 // Access AnalysisResolver
132 void setResolver(AnalysisResolver *AR);
133 AnalysisResolver *getResolver() const { return Resolver; }
135 /// getAnalysisUsage - This function should be overriden by passes that need
136 /// analysis information to do their job. If a pass specifies that it uses a
137 /// particular analysis result to this function, it can then use the
138 /// getAnalysis<AnalysisType>() function, below.
140 virtual void getAnalysisUsage(AnalysisUsage &) const;
142 /// releaseMemory() - This member can be implemented by a pass if it wants to
143 /// be able to release its memory when it is no longer needed. The default
144 /// behavior of passes is to hold onto memory for the entire duration of their
145 /// lifetime (which is the entire compile time). For pipelined passes, this
146 /// is not a big deal because that memory gets recycled every time the pass is
147 /// invoked on another program unit. For IP passes, it is more important to
148 /// free memory when it is unused.
150 /// Optionally implement this function to release pass memory when it is no
153 virtual void releaseMemory();
155 /// getAdjustedAnalysisPointer - This method is used when a pass implements
156 /// an analysis interface through multiple inheritance. If needed, it should
157 /// override this to adjust the this pointer as needed for the specified pass
159 virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
160 virtual ImmutablePass *getAsImmutablePass();
161 virtual PMDataManager *getAsPMDataManager();
163 /// verifyAnalysis() - This member can be implemented by a analysis pass to
164 /// check state of analysis information.
165 virtual void verifyAnalysis() const;
167 // dumpPassStructure - Implement the -debug-passes=PassStructure option
168 virtual void dumpPassStructure(unsigned Offset = 0);
170 // lookupPassInfo - Return the pass info object for the specified pass class,
171 // or null if it is not known.
172 static const PassInfo *lookupPassInfo(const void *TI);
174 // lookupPassInfo - Return the pass info object for the pass with the given
175 // argument string, or null if it is not known.
176 static const PassInfo *lookupPassInfo(StringRef Arg);
178 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
179 /// get analysis information that might be around, for example to update it.
180 /// This is different than getAnalysis in that it can fail (if the analysis
181 /// results haven't been computed), so should only be used if you can handle
182 /// the case when the analysis is not available. This method is often used by
183 /// transformation APIs to update analysis results for a pass automatically as
184 /// the transform is performed.
186 template<typename AnalysisType> AnalysisType *
187 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
189 /// mustPreserveAnalysisID - This method serves the same function as
190 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
191 /// obviously cannot give you a properly typed instance of the class if you
192 /// don't have the class name available (use getAnalysisIfAvailable if you
193 /// do), but it can tell you if you need to preserve the pass at least.
195 bool mustPreserveAnalysisID(char &AID) const;
197 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
198 /// to the analysis information that they claim to use by overriding the
199 /// getAnalysisUsage function.
201 template<typename AnalysisType>
202 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
204 template<typename AnalysisType>
205 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
207 template<typename AnalysisType>
208 AnalysisType &getAnalysisID(AnalysisID PI) const;
210 template<typename AnalysisType>
211 AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
215 //===----------------------------------------------------------------------===//
216 /// ModulePass class - This class is used to implement unstructured
217 /// interprocedural optimizations and analyses. ModulePasses may do anything
218 /// they want to the program.
220 class ModulePass : public Pass {
222 /// createPrinterPass - Get a module printer pass.
223 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
225 /// runOnModule - Virtual method overriden by subclasses to process the module
226 /// being operated on.
227 virtual bool runOnModule(Module &M) = 0;
229 virtual void assignPassManager(PMStack &PMS,
232 /// Return what kind of Pass Manager can manage this pass.
233 virtual PassManagerType getPotentialPassManagerType() const;
235 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
236 // Force out-of-line virtual method.
237 virtual ~ModulePass();
241 //===----------------------------------------------------------------------===//
242 /// ImmutablePass class - This class is used to provide information that does
243 /// not need to be run. This is useful for things like target information and
244 /// "basic" versions of AnalysisGroups.
246 class ImmutablePass : public ModulePass {
248 /// initializePass - This method may be overriden by immutable passes to allow
249 /// them to perform various initialization actions they require. This is
250 /// primarily because an ImmutablePass can "require" another ImmutablePass,
251 /// and if it does, the overloaded version of initializePass may get access to
252 /// these passes with getAnalysis<>.
254 virtual void initializePass();
256 virtual ImmutablePass *getAsImmutablePass() { return this; }
258 /// ImmutablePasses are never run.
260 bool runOnModule(Module &) { return false; }
262 explicit ImmutablePass(char &pid)
265 // Force out-of-line virtual method.
266 virtual ~ImmutablePass();
269 //===----------------------------------------------------------------------===//
270 /// FunctionPass class - This class is used to implement most global
271 /// optimizations. Optimizations should subclass this class if they meet the
272 /// following constraints:
274 /// 1. Optimizations are organized globally, i.e., a function at a time
275 /// 2. Optimizing a function does not cause the addition or removal of any
276 /// functions in the module
278 class FunctionPass : public Pass {
280 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
282 /// createPrinterPass - Get a function printer pass.
283 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
285 /// doInitialization - Virtual method overridden by subclasses to do
286 /// any necessary per-module initialization.
288 virtual bool doInitialization(Module &);
290 /// runOnFunction - Virtual method overriden by subclasses to do the
291 /// per-function processing of the pass.
293 virtual bool runOnFunction(Function &F) = 0;
295 /// doFinalization - Virtual method overriden by subclasses to do any post
296 /// processing needed after all passes have run.
298 virtual bool doFinalization(Module &);
300 virtual void assignPassManager(PMStack &PMS,
303 /// Return what kind of Pass Manager can manage this pass.
304 virtual PassManagerType getPotentialPassManagerType() const;
309 //===----------------------------------------------------------------------===//
310 /// BasicBlockPass class - This class is used to implement most local
311 /// optimizations. Optimizations should subclass this class if they
312 /// meet the following constraints:
313 /// 1. Optimizations are local, operating on either a basic block or
314 /// instruction at a time.
315 /// 2. Optimizations do not modify the CFG of the contained function, or any
316 /// other basic block in the function.
317 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
319 class BasicBlockPass : public Pass {
321 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
323 /// createPrinterPass - Get a function printer pass.
324 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
326 /// doInitialization - Virtual method overridden by subclasses to do
327 /// any necessary per-module initialization.
329 virtual bool doInitialization(Module &);
331 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
332 /// to do any necessary per-function initialization.
334 virtual bool doInitialization(Function &);
336 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
337 /// per-basicblock processing of the pass.
339 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
341 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
342 /// do any post processing needed after all passes have run.
344 virtual bool doFinalization(Function &);
346 /// doFinalization - Virtual method overriden by subclasses to do any post
347 /// processing needed after all passes have run.
349 virtual bool doFinalization(Module &);
351 virtual void assignPassManager(PMStack &PMS,
354 /// Return what kind of Pass Manager can manage this pass.
355 virtual PassManagerType getPotentialPassManagerType() const;
358 /// If the user specifies the -time-passes argument on an LLVM tool command line
359 /// then the value of this boolean will be true, otherwise false.
360 /// @brief This is the storage for the -time-passes option.
361 extern bool TimePassesIsEnabled;
363 } // End llvm namespace
365 // Include support files that contain important APIs commonly used by Passes,
366 // but that we want to separate out to make it easier to read the header files.
368 #include "llvm/PassSupport.h"
369 #include "llvm/PassAnalysisSupport.h"