Pass.h revision 90c579de5a383cee278acc3f7e7b9d0a656e6a35
1//===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a base class that indicates that a specified class is a
11// transformation pass implementation.
12//
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.
17//
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.
22//
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.
26//
27//===----------------------------------------------------------------------===//
28
29#ifndef LLVM_PASS_H
30#define LLVM_PASS_H
31
32#include "llvm/System/DataTypes.h"
33
34#include <string>
35#include <utility>
36#include <vector>
37
38namespace llvm {
39
40class BasicBlock;
41class Function;
42class Module;
43class AnalysisUsage;
44class PassInfo;
45class ImmutablePass;
46class PMStack;
47class AnalysisResolver;
48class PMDataManager;
49class raw_ostream;
50class StringRef;
51
52// AnalysisID - Use the PassInfo to identify a pass...
53typedef const void* AnalysisID;
54
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.
58enum PassManagerType {
59  PMT_Unknown = 0,
60  PMT_ModulePassManager = 1, ///< MPPassManager
61  PMT_CallGraphPassManager,  ///< CGPassManager
62  PMT_FunctionPassManager,   ///< FPPassManager
63  PMT_LoopPassManager,       ///< LPPassManager
64  PMT_BasicBlockPassManager, ///< BBPassManager
65  PMT_Last
66};
67
68// Different types of passes.
69enum PassKind {
70  PT_BasicBlock,
71  PT_Loop,
72  PT_Function,
73  PT_CallGraphSCC,
74  PT_Module,
75  PT_PassManager
76};
77
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.
82///
83class Pass {
84  AnalysisResolver *Resolver;  // Used to resolve analysis
85  const void *PassID;
86  PassKind Kind;
87  void operator=(const Pass&);  // DO NOT IMPLEMENT
88  Pass(const Pass &);           // DO NOT IMPLEMENT
89
90public:
91  explicit Pass(PassKind K, char &pid);
92  virtual ~Pass();
93
94
95  PassKind getPassKind() const { return Kind; }
96
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.
100  ///
101  virtual const char *getPassName() const;
102
103  /// getPassID - Return the PassID number that corresponds to this pass.
104  virtual AnalysisID getPassID() const {
105    return PassID;
106  }
107
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
113  /// ignored.
114  ///
115  virtual void print(raw_ostream &O, const Module *M) const;
116  void dump() const; // dump - Print to stderr.
117
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;
122
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 &);
129
130  ///  Return what kind of Pass Manager can manage this pass.
131  virtual PassManagerType getPotentialPassManagerType() const;
132
133  // Access AnalysisResolver
134  void setResolver(AnalysisResolver *AR);
135  AnalysisResolver *getResolver() const { return Resolver; }
136
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.
141  ///
142  virtual void getAnalysisUsage(AnalysisUsage &) const;
143
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.
151  ///
152  /// Optionally implement this function to release pass memory when it is no
153  /// longer used.
154  ///
155  virtual void releaseMemory();
156
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
160  /// info.
161  virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
162  virtual ImmutablePass *getAsImmutablePass();
163  virtual PMDataManager *getAsPMDataManager();
164
165  /// verifyAnalysis() - This member can be implemented by a analysis pass to
166  /// check state of analysis information.
167  virtual void verifyAnalysis() const;
168
169  // dumpPassStructure - Implement the -debug-passes=PassStructure option
170  virtual void dumpPassStructure(unsigned Offset = 0);
171
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);
175
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);
179
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.
187  ///
188  template<typename AnalysisType> AnalysisType *
189    getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
190
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.
196  ///
197  bool mustPreserveAnalysisID(char &AID) const;
198
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.
202  ///
203  template<typename AnalysisType>
204  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
205
206  template<typename AnalysisType>
207  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
208
209  template<typename AnalysisType>
210  AnalysisType &getAnalysisID(AnalysisID PI) const;
211
212  template<typename AnalysisType>
213  AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
214};
215
216
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.
221///
222class ModulePass : public Pass {
223public:
224  /// createPrinterPass - Get a module printer pass.
225  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
226
227  /// runOnModule - Virtual method overriden by subclasses to process the module
228  /// being operated on.
229  virtual bool runOnModule(Module &M) = 0;
230
231  virtual void assignPassManager(PMStack &PMS,
232                                 PassManagerType T = PMT_ModulePassManager);
233
234  ///  Return what kind of Pass Manager can manage this pass.
235  virtual PassManagerType getPotentialPassManagerType() const;
236
237  explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
238  // Force out-of-line virtual method.
239  virtual ~ModulePass();
240};
241
242
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.
247///
248class ImmutablePass : public ModulePass {
249public:
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<>.
255  ///
256  virtual void initializePass();
257
258  virtual ImmutablePass *getAsImmutablePass() { return this; }
259
260  /// ImmutablePasses are never run.
261  ///
262  bool runOnModule(Module &) { return false; }
263
264  explicit ImmutablePass(char &pid)
265  : ModulePass(pid) {}
266
267  // Force out-of-line virtual method.
268  virtual ~ImmutablePass();
269};
270
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:
275///
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
279///
280class FunctionPass : public Pass {
281public:
282  explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
283
284  /// createPrinterPass - Get a function printer pass.
285  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
286
287  /// doInitialization - Virtual method overridden by subclasses to do
288  /// any necessary per-module initialization.
289  ///
290  virtual bool doInitialization(Module &);
291
292  /// runOnFunction - Virtual method overriden by subclasses to do the
293  /// per-function processing of the pass.
294  ///
295  virtual bool runOnFunction(Function &F) = 0;
296
297  /// doFinalization - Virtual method overriden by subclasses to do any post
298  /// processing needed after all passes have run.
299  ///
300  virtual bool doFinalization(Module &);
301
302  /// runOnModule - On a module, we run this pass by initializing,
303  /// ronOnFunction'ing once for every function in the module, then by
304  /// finalizing.
305  ///
306  virtual bool runOnModule(Module &M);
307
308  /// run - On a function, we simply initialize, run the function, then
309  /// finalize.
310  ///
311  bool run(Function &F);
312
313  virtual void assignPassManager(PMStack &PMS,
314                                 PassManagerType T = PMT_FunctionPassManager);
315
316  ///  Return what kind of Pass Manager can manage this pass.
317  virtual PassManagerType getPotentialPassManagerType() const;
318};
319
320
321
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.
331///
332class BasicBlockPass : public Pass {
333public:
334  explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
335
336  /// createPrinterPass - Get a function printer pass.
337  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
338
339  /// doInitialization - Virtual method overridden by subclasses to do
340  /// any necessary per-module initialization.
341  ///
342  virtual bool doInitialization(Module &);
343
344  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
345  /// to do any necessary per-function initialization.
346  ///
347  virtual bool doInitialization(Function &);
348
349  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
350  /// per-basicblock processing of the pass.
351  ///
352  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
353
354  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
355  /// do any post processing needed after all passes have run.
356  ///
357  virtual bool doFinalization(Function &);
358
359  /// doFinalization - Virtual method overriden by subclasses to do any post
360  /// processing needed after all passes have run.
361  ///
362  virtual bool doFinalization(Module &);
363
364
365  // To run this pass on a function, we simply call runOnBasicBlock once for
366  // each function.
367  //
368  bool runOnFunction(Function &F);
369
370  virtual void assignPassManager(PMStack &PMS,
371                                 PassManagerType T = PMT_BasicBlockPassManager);
372
373  ///  Return what kind of Pass Manager can manage this pass.
374  virtual PassManagerType getPotentialPassManagerType() const;
375};
376
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.
380extern bool TimePassesIsEnabled;
381
382} // End llvm namespace
383
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.
386//
387#include "llvm/PassSupport.h"
388#include "llvm/PassAnalysisSupport.h"
389
390#endif
391