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 //===----------------------------------------------------------------------===//
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) : Resolver(0), PassID(&pid), Kind(K) { }
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 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 on (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 // createPass - Create a object for the specified pass class,
179 // or null if it is not known.
180 static Pass *createPass(AnalysisID ID);
182 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
183 /// get analysis information that might be around, for example to update it.
184 /// This is different than getAnalysis in that it can fail (if the analysis
185 /// results haven't been computed), so should only be used if you can handle
186 /// the case when the analysis is not available. This method is often used by
187 /// transformation APIs to update analysis results for a pass automatically as
188 /// 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.
199 bool mustPreserveAnalysisID(char &AID) const;
201 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
202 /// to the analysis information that they claim to use by overriding the
203 /// getAnalysisUsage function.
205 template<typename AnalysisType>
206 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
208 template<typename AnalysisType>
209 AnalysisType &getAnalysis(Function &F); // 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);
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 /// createPrinterPass - Get a module printer pass.
227 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
229 /// runOnModule - Virtual method overriden by subclasses to process the module
230 /// being operated on.
231 virtual bool runOnModule(Module &M) = 0;
233 virtual void assignPassManager(PMStack &PMS,
236 /// Return what kind of Pass Manager can manage this pass.
237 virtual PassManagerType getPotentialPassManagerType() const;
239 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
240 // Force out-of-line virtual method.
241 virtual ~ModulePass();
245 //===----------------------------------------------------------------------===//
246 /// ImmutablePass class - This class is used to provide information that does
247 /// not need to be run. This is useful for things like target information and
248 /// "basic" versions of AnalysisGroups.
250 class ImmutablePass : public ModulePass {
252 /// initializePass - This method may be overriden by immutable passes to allow
253 /// them to perform various initialization actions they require. This is
254 /// primarily because an ImmutablePass can "require" another ImmutablePass,
255 /// and if it does, the overloaded version of initializePass may get access to
256 /// these passes with getAnalysis<>.
258 virtual void initializePass();
260 virtual ImmutablePass *getAsImmutablePass() { return this; }
262 /// ImmutablePasses are never run.
264 bool runOnModule(Module &) { return false; }
266 explicit ImmutablePass(char &pid)
269 // Force out-of-line virtual method.
270 virtual ~ImmutablePass();
273 //===----------------------------------------------------------------------===//
274 /// FunctionPass class - This class is used to implement most global
275 /// optimizations. Optimizations should subclass this class if they meet the
276 /// following constraints:
278 /// 1. Optimizations are organized globally, i.e., a function at a time
279 /// 2. Optimizing a function does not cause the addition or removal of any
280 /// functions in the module
282 class FunctionPass : public Pass {
284 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
286 /// createPrinterPass - Get a function printer pass.
287 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
289 /// doInitialization - Virtual method overridden by subclasses to do
290 /// any necessary per-module initialization.
292 virtual bool doInitialization(Module &);
294 /// runOnFunction - Virtual method overriden by subclasses to do the
295 /// per-function processing of the pass.
297 virtual bool runOnFunction(Function &F) = 0;
299 /// doFinalization - Virtual method overriden by subclasses to do any post
300 /// processing needed after all passes have run.
302 virtual bool doFinalization(Module &);
304 virtual void assignPassManager(PMStack &PMS,
307 /// Return what kind of Pass Manager can manage this pass.
308 virtual PassManagerType getPotentialPassManagerType() const;
313 //===----------------------------------------------------------------------===//
314 /// BasicBlockPass class - This class is used to implement most local
315 /// optimizations. Optimizations should subclass this class if they
316 /// meet the following constraints:
317 /// 1. Optimizations are local, operating on either a basic block or
318 /// instruction at a time.
319 /// 2. Optimizations do not modify the CFG of the contained function, or any
320 /// other basic block in the function.
321 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
323 class BasicBlockPass : public Pass {
325 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
327 /// createPrinterPass - Get a basic block printer pass.
328 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
330 /// doInitialization - Virtual method overridden by subclasses to do
331 /// any necessary per-module initialization.
333 virtual bool doInitialization(Module &);
335 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
336 /// to do any necessary per-function initialization.
338 virtual bool doInitialization(Function &);
340 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
341 /// per-basicblock processing of the pass.
343 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
345 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
346 /// do any post processing needed after all passes have run.
348 virtual bool doFinalization(Function &);
350 /// doFinalization - Virtual method overriden by subclasses to do any post
351 /// processing needed after all passes have run.
353 virtual bool doFinalization(Module &);
355 virtual void assignPassManager(PMStack &PMS,
358 /// Return what kind of Pass Manager can manage this pass.
359 virtual PassManagerType getPotentialPassManagerType() const;
362 /// If the user specifies the -time-passes argument on an LLVM tool command line
363 /// then the value of this boolean will be true, otherwise false.
364 /// @brief This is the storage for the -time-passes option.
365 extern bool TimePassesIsEnabled;
367 } // End llvm namespace
369 // Include support files that contain important APIs commonly used by Passes,
370 // but that we want to separate out to make it easier to read the header files.
372 #include "llvm/PassSupport.h"
373 #include "llvm/PassAnalysisSupport.h"