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_BasicBlockPassManager, ///< BBPassManager
64 // Different types of passes.
74 //===----------------------------------------------------------------------===//
75 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
76 /// interprocedural optimization or you do not fit into any of the more
77 /// constrained passes described below.
80 AnalysisResolver *Resolver; // Used to resolve analysis
83 void operator=(const Pass&); // DO NOT IMPLEMENT
84 Pass(const Pass &); // DO NOT IMPLEMENT
87 explicit Pass(PassKind K, char &pid);
91 PassKind getPassKind() const { return Kind; }
93 /// getPassName - Return a nice clean name for a pass. This usually
94 /// implemented in terms of the name that is registered by one of the
95 /// Registration templates, but can be overloaded directly.
97 virtual const char *getPassName() const;
99 /// getPassID - Return the PassID number that corresponds to this pass.
100 virtual AnalysisID getPassID() const {
104 /// print - Print out the internal state of the pass. This is called by
105 /// Analyze to print out the contents of an analysis. Otherwise it is not
106 /// necessary to implement this method. Beware that the module pointer MAY be
107 /// null. This automatically forwards to a virtual function that does not
108 /// provide the Module* in case the analysis doesn't need it it can just be
111 virtual void print(raw_ostream &O, const Module *M) const;
112 void dump() const; // dump - Print to stderr.
114 /// createPrinterPass - Get a Pass appropriate to print the IR this
115 /// pass operates one (Module, Function or MachineFunction).
116 virtual Pass *createPrinterPass(raw_ostream &O,
117 const std::string &Banner) const = 0;
119 /// Each pass is responsible for assigning a pass manager to itself.
120 /// PMS is the stack of available pass manager.
121 virtual void assignPassManager(PMStack &,
123 /// Check if available pass managers are suitable for this pass or not.
124 virtual void preparePassManager(PMStack &);
126 /// Return what kind of Pass Manager can manage this pass.
127 virtual PassManagerType getPotentialPassManagerType() const;
129 // Access AnalysisResolver
130 void setResolver(AnalysisResolver *AR);
131 AnalysisResolver *getResolver() const { return Resolver; }
133 /// getAnalysisUsage - This function should be overriden by passes that need
134 /// analysis information to do their job. If a pass specifies that it uses a
135 /// particular analysis result to this function, it can then use the
136 /// getAnalysis<AnalysisType>() function, below.
138 virtual void getAnalysisUsage(AnalysisUsage &) const;
140 /// releaseMemory() - This member can be implemented by a pass if it wants to
141 /// be able to release its memory when it is no longer needed. The default
142 /// behavior of passes is to hold onto memory for the entire duration of their
143 /// lifetime (which is the entire compile time). For pipelined passes, this
144 /// is not a big deal because that memory gets recycled every time the pass is
145 /// invoked on another program unit. For IP passes, it is more important to
146 /// free memory when it is unused.
148 /// Optionally implement this function to release pass memory when it is no
151 virtual void releaseMemory();
153 /// getAdjustedAnalysisPointer - This method is used when a pass implements
154 /// an analysis interface through multiple inheritance. If needed, it should
155 /// override this to adjust the this pointer as needed for the specified pass
157 virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
158 virtual ImmutablePass *getAsImmutablePass();
159 virtual PMDataManager *getAsPMDataManager();
161 /// verifyAnalysis() - This member can be implemented by a analysis pass to
162 /// check state of analysis information.
163 virtual void verifyAnalysis() const;
165 // dumpPassStructure - Implement the -debug-passes=PassStructure option
166 virtual void dumpPassStructure(unsigned Offset = 0);
168 // lookupPassInfo - Return the pass info object for the specified pass class,
169 // or null if it is not known.
170 static const PassInfo *lookupPassInfo(const void *TI);
172 // lookupPassInfo - Return the pass info object for the pass with the given
173 // argument string, or null if it is not known.
174 static const PassInfo *lookupPassInfo(StringRef Arg);
176 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
177 /// get analysis information that might be around, for example to update it.
178 /// This is different than getAnalysis in that it can fail (if the analysis
179 /// results haven't been computed), so should only be used if you can handle
180 /// the case when the analysis is not available. This method is often used by
181 /// transformation APIs to update analysis results for a pass automatically as
182 /// the transform is performed.
184 template<typename AnalysisType> AnalysisType *
185 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
187 /// mustPreserveAnalysisID - This method serves the same function as
188 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
189 /// obviously cannot give you a properly typed instance of the class if you
190 /// don't have the class name available (use getAnalysisIfAvailable if you
191 /// do), but it can tell you if you need to preserve the pass at least.
193 bool mustPreserveAnalysisID(char &AID) const;
195 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
196 /// to the analysis information that they claim to use by overriding the
197 /// getAnalysisUsage function.
199 template<typename AnalysisType>
200 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
202 template<typename AnalysisType>
203 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
205 template<typename AnalysisType>
206 AnalysisType &getAnalysisID(AnalysisID PI) const;
208 template<typename AnalysisType>
209 AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
213 //===----------------------------------------------------------------------===//
214 /// ModulePass class - This class is used to implement unstructured
215 /// interprocedural optimizations and analyses. ModulePasses may do anything
216 /// they want to the program.
218 class ModulePass : public Pass {
220 /// createPrinterPass - Get a module printer pass.
221 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
223 /// runOnModule - Virtual method overriden by subclasses to process the module
224 /// being operated on.
225 virtual bool runOnModule(Module &M) = 0;
227 virtual void assignPassManager(PMStack &PMS,
230 /// Return what kind of Pass Manager can manage this pass.
231 virtual PassManagerType getPotentialPassManagerType() const;
233 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
234 // Force out-of-line virtual method.
235 virtual ~ModulePass();
239 //===----------------------------------------------------------------------===//
240 /// ImmutablePass class - This class is used to provide information that does
241 /// not need to be run. This is useful for things like target information and
242 /// "basic" versions of AnalysisGroups.
244 class ImmutablePass : public ModulePass {
246 /// initializePass - This method may be overriden by immutable passes to allow
247 /// them to perform various initialization actions they require. This is
248 /// primarily because an ImmutablePass can "require" another ImmutablePass,
249 /// and if it does, the overloaded version of initializePass may get access to
250 /// these passes with getAnalysis<>.
252 virtual void initializePass();
254 virtual ImmutablePass *getAsImmutablePass() { return this; }
256 /// ImmutablePasses are never run.
258 bool runOnModule(Module &) { return false; }
260 explicit ImmutablePass(char &pid)
263 // Force out-of-line virtual method.
264 virtual ~ImmutablePass();
267 //===----------------------------------------------------------------------===//
268 /// FunctionPass class - This class is used to implement most global
269 /// optimizations. Optimizations should subclass this class if they meet the
270 /// following constraints:
272 /// 1. Optimizations are organized globally, i.e., a function at a time
273 /// 2. Optimizing a function does not cause the addition or removal of any
274 /// functions in the module
276 class FunctionPass : public Pass {
278 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
280 /// createPrinterPass - Get a function printer pass.
281 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
283 /// doInitialization - Virtual method overridden by subclasses to do
284 /// any necessary per-module initialization.
286 virtual bool doInitialization(Module &);
288 /// runOnFunction - Virtual method overriden by subclasses to do the
289 /// per-function processing of the pass.
291 virtual bool runOnFunction(Function &F) = 0;
293 /// doFinalization - Virtual method overriden by subclasses to do any post
294 /// processing needed after all passes have run.
296 virtual bool doFinalization(Module &);
298 virtual void assignPassManager(PMStack &PMS,
301 /// Return what kind of Pass Manager can manage this pass.
302 virtual PassManagerType getPotentialPassManagerType() const;
307 //===----------------------------------------------------------------------===//
308 /// BasicBlockPass class - This class is used to implement most local
309 /// optimizations. Optimizations should subclass this class if they
310 /// meet the following constraints:
311 /// 1. Optimizations are local, operating on either a basic block or
312 /// instruction at a time.
313 /// 2. Optimizations do not modify the CFG of the contained function, or any
314 /// other basic block in the function.
315 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
317 class BasicBlockPass : public Pass {
319 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
321 /// createPrinterPass - Get a function printer pass.
322 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
324 /// doInitialization - Virtual method overridden by subclasses to do
325 /// any necessary per-module initialization.
327 virtual bool doInitialization(Module &);
329 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
330 /// to do any necessary per-function initialization.
332 virtual bool doInitialization(Function &);
334 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
335 /// per-basicblock processing of the pass.
337 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
339 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
340 /// do any post processing needed after all passes have run.
342 virtual bool doFinalization(Function &);
344 /// doFinalization - Virtual method overriden by subclasses to do any post
345 /// processing needed after all passes have run.
347 virtual bool doFinalization(Module &);
349 virtual void assignPassManager(PMStack &PMS,
352 /// Return what kind of Pass Manager can manage this pass.
353 virtual PassManagerType getPotentialPassManagerType() const;
356 /// If the user specifies the -time-passes argument on an LLVM tool command line
357 /// then the value of this boolean will be true, otherwise false.
358 /// @brief This is the storage for the -time-passes option.
359 extern bool TimePassesIsEnabled;
361 } // End llvm namespace
363 // Include support files that contain important APIs commonly used by Passes,
364 // but that we want to separate out to make it easier to read the header files.
366 #include "llvm/PassSupport.h"
367 #include "llvm/PassAnalysisSupport.h"