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 //===----------------------------------------------------------------------===//
32 #include "llvm/System/DataTypes.h"
47 class AnalysisResolver;
52 // AnalysisID - Use the PassInfo to identify a pass...
53 typedef const void* AnalysisID;
55 /// Different types of internal pass managers. External pass managers
56 /// (PassManager and FunctionPassManager) are not represented here.
57 /// Ordering of pass manager types is important here.
58 enum PassManagerType {
60 PMT_ModulePassManager = 1, ///< MPPassManager
61 PMT_CallGraphPassManager, ///< CGPassManager
62 PMT_FunctionPassManager, ///< FPPassManager
63 PMT_LoopPassManager, ///< LPPassManager
64 PMT_BasicBlockPassManager, ///< BBPassManager
68 // Different types of passes.
78 //===----------------------------------------------------------------------===//
79 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
80 /// interprocedural optimization or you do not fit into any of the more
81 /// constrained passes described below.
84 AnalysisResolver *Resolver; // Used to resolve analysis
87 void operator=(const Pass&); // DO NOT IMPLEMENT
88 Pass(const Pass &); // DO NOT IMPLEMENT
91 explicit Pass(PassKind K, char &pid);
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 virtual AnalysisID getPassID() const {
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
115 virtual void print(raw_ostream &O, 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 one (Module, Function or MachineFunction).
120 virtual Pass *createPrinterPass(raw_ostream &O,
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 &,
126 PassManagerType = PMT_Unknown) {}
127 /// Check if available pass managers are suitable for this pass or not.
128 virtual void preparePassManager(PMStack &);
130 /// Return what kind of Pass Manager can manage this pass.
131 virtual PassManagerType getPotentialPassManagerType() const;
133 // Access AnalysisResolver
134 void setResolver(AnalysisResolver *AR);
135 AnalysisResolver *getResolver() const { return Resolver; }
137 /// getAnalysisUsage - This function should be overriden by passes that need
138 /// analysis information to do their job. If a pass specifies that it uses a
139 /// particular analysis result to this function, it can then use the
140 /// 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
155 virtual void releaseMemory();
157 /// getAdjustedAnalysisPointer - This method is used when a pass implements
158 /// an analysis interface through multiple inheritance. If needed, it should
159 /// override this to adjust the this pointer as needed for the specified pass
161 virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
162 virtual ImmutablePass *getAsImmutablePass();
163 virtual PMDataManager *getAsPMDataManager();
165 /// verifyAnalysis() - This member can be implemented by a analysis pass to
166 /// check state of analysis information.
167 virtual void verifyAnalysis() const;
169 // dumpPassStructure - Implement the -debug-passes=PassStructure option
170 virtual void dumpPassStructure(unsigned Offset = 0);
172 // lookupPassInfo - Return the pass info object for the specified pass class,
173 // or null if it is not known.
174 static const PassInfo *lookupPassInfo(const void *TI);
176 // lookupPassInfo - Return the pass info object for the pass with the given
177 // argument string, or null if it is not known.
178 static const PassInfo *lookupPassInfo(StringRef Arg);
180 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
181 /// get analysis information that might be around, for example to update it.
182 /// This is different than getAnalysis in that it can fail (if the analysis
183 /// results haven't been computed), so should only be used if you can handle
184 /// the case when the analysis is not available. This method is often used by
185 /// transformation APIs to update analysis results for a pass automatically as
186 /// the transform is performed.
188 template<typename AnalysisType> AnalysisType *
189 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
191 /// mustPreserveAnalysisID - This method serves the same function as
192 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
193 /// obviously cannot give you a properly typed instance of the class if you
194 /// don't have the class name available (use getAnalysisIfAvailable if you
195 /// do), but it can tell you if you need to preserve the pass at least.
197 bool mustPreserveAnalysisID(char &AID) const;
199 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
200 /// to the analysis information that they claim to use by overriding the
201 /// getAnalysisUsage function.
203 template<typename AnalysisType>
204 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
206 template<typename AnalysisType>
207 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
209 template<typename AnalysisType>
210 AnalysisType &getAnalysisID(AnalysisID PI) const;
212 template<typename AnalysisType>
213 AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
217 //===----------------------------------------------------------------------===//
218 /// ModulePass class - This class is used to implement unstructured
219 /// interprocedural optimizations and analyses. ModulePasses may do anything
220 /// they want to the program.
222 class ModulePass : public Pass {
224 /// createPrinterPass - Get a module printer pass.
225 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
227 /// runOnModule - Virtual method overriden by subclasses to process the module
228 /// being operated on.
229 virtual bool runOnModule(Module &M) = 0;
231 virtual void assignPassManager(PMStack &PMS,
232 PassManagerType T = PMT_ModulePassManager);
234 /// Return what kind of Pass Manager can manage this pass.
235 virtual PassManagerType getPotentialPassManagerType() const;
237 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
238 // Force out-of-line virtual method.
239 virtual ~ModulePass();
243 //===----------------------------------------------------------------------===//
244 /// ImmutablePass class - This class is used to provide information that does
245 /// not need to be run. This is useful for things like target information and
246 /// "basic" versions of AnalysisGroups.
248 class ImmutablePass : public ModulePass {
250 /// initializePass - This method may be overriden by immutable passes to allow
251 /// them to perform various initialization actions they require. This is
252 /// primarily because an ImmutablePass can "require" another ImmutablePass,
253 /// and if it does, the overloaded version of initializePass may get access to
254 /// these passes with getAnalysis<>.
256 virtual void initializePass();
258 virtual ImmutablePass *getAsImmutablePass() { return this; }
260 /// ImmutablePasses are never run.
262 bool runOnModule(Module &) { return false; }
264 explicit ImmutablePass(char &pid)
267 // Force out-of-line virtual method.
268 virtual ~ImmutablePass();
271 //===----------------------------------------------------------------------===//
272 /// FunctionPass class - This class is used to implement most global
273 /// optimizations. Optimizations should subclass this class if they meet the
274 /// following constraints:
276 /// 1. Optimizations are organized globally, i.e., a function at a time
277 /// 2. Optimizing a function does not cause the addition or removal of any
278 /// functions in the module
280 class FunctionPass : public Pass {
282 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
284 /// createPrinterPass - Get a function printer pass.
285 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
287 /// doInitialization - Virtual method overridden by subclasses to do
288 /// any necessary per-module initialization.
290 virtual bool doInitialization(Module &);
292 /// runOnFunction - Virtual method overriden by subclasses to do the
293 /// per-function processing of the pass.
295 virtual bool runOnFunction(Function &F) = 0;
297 /// doFinalization - Virtual method overriden by subclasses to do any post
298 /// processing needed after all passes have run.
300 virtual bool doFinalization(Module &);
302 /// runOnModule - On a module, we run this pass by initializing,
303 /// ronOnFunction'ing once for every function in the module, then by
306 virtual bool runOnModule(Module &M);
308 /// run - On a function, we simply initialize, run the function, then
311 bool run(Function &F);
313 virtual void assignPassManager(PMStack &PMS,
314 PassManagerType T = PMT_FunctionPassManager);
316 /// Return what kind of Pass Manager can manage this pass.
317 virtual PassManagerType getPotentialPassManagerType() const;
322 //===----------------------------------------------------------------------===//
323 /// BasicBlockPass class - This class is used to implement most local
324 /// optimizations. Optimizations should subclass this class if they
325 /// meet the following constraints:
326 /// 1. Optimizations are local, operating on either a basic block or
327 /// instruction at a time.
328 /// 2. Optimizations do not modify the CFG of the contained function, or any
329 /// other basic block in the function.
330 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
332 class BasicBlockPass : public Pass {
334 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
336 /// createPrinterPass - Get a function printer pass.
337 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
339 /// doInitialization - Virtual method overridden by subclasses to do
340 /// any necessary per-module initialization.
342 virtual bool doInitialization(Module &);
344 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
345 /// to do any necessary per-function initialization.
347 virtual bool doInitialization(Function &);
349 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
350 /// per-basicblock processing of the pass.
352 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
354 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
355 /// do any post processing needed after all passes have run.
357 virtual bool doFinalization(Function &);
359 /// doFinalization - Virtual method overriden by subclasses to do any post
360 /// processing needed after all passes have run.
362 virtual bool doFinalization(Module &);
365 // To run this pass on a function, we simply call runOnBasicBlock once for
368 bool runOnFunction(Function &F);
370 virtual void assignPassManager(PMStack &PMS,
371 PassManagerType T = PMT_BasicBlockPassManager);
373 /// Return what kind of Pass Manager can manage this pass.
374 virtual PassManagerType getPotentialPassManagerType() const;
377 /// If the user specifies the -time-passes argument on an LLVM tool command line
378 /// then the value of this boolean will be true, otherwise false.
379 /// @brief This is the storage for the -time-passes option.
380 extern bool TimePassesIsEnabled;
382 } // End llvm namespace
384 // Include support files that contain important APIs commonly used by Passes,
385 // but that we want to separate out to make it easier to read the header files.
387 #include "llvm/PassSupport.h"
388 #include "llvm/PassAnalysisSupport.h"