Pass.h revision 234353
1193323Sed//===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines a base class that indicates that a specified class is a
11193323Sed// transformation pass implementation.
12193323Sed//
13193323Sed// Passes are designed this way so that it is possible to run passes in a cache
14193323Sed// and organizationally optimal order without having to specify it at the front
15193323Sed// end.  This allows arbitrary passes to be strung together and have them
16221345Sdim// executed as efficiently as possible.
17193323Sed//
18193323Sed// Passes should extend one of the classes below, depending on the guarantees
19193323Sed// that it can make about what will be modified as it is run.  For example, most
20193323Sed// global optimizations should derive from FunctionPass, because they do not add
21193323Sed// or delete functions, they operate on the internals of the function.
22193323Sed//
23193323Sed// Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
24193323Sed// bottom), so the APIs exposed by these files are also automatically available
25193323Sed// to all users of this file.
26193323Sed//
27193323Sed//===----------------------------------------------------------------------===//
28193323Sed
29193323Sed#ifndef LLVM_PASS_H
30193323Sed#define LLVM_PASS_H
31193323Sed
32206124Srdivacky#include <string>
33193323Sed
34193323Sednamespace llvm {
35193323Sed
36193323Sedclass BasicBlock;
37193323Sedclass Function;
38193323Sedclass Module;
39193323Sedclass AnalysisUsage;
40193323Sedclass PassInfo;
41193323Sedclass ImmutablePass;
42193323Sedclass PMStack;
43193323Sedclass AnalysisResolver;
44193323Sedclass PMDataManager;
45198090Srdivackyclass raw_ostream;
46198090Srdivackyclass StringRef;
47193323Sed
48193323Sed// AnalysisID - Use the PassInfo to identify a pass...
49212904Sdimtypedef const void* AnalysisID;
50193323Sed
51193323Sed/// Different types of internal pass managers. External pass managers
52193323Sed/// (PassManager and FunctionPassManager) are not represented here.
53193323Sed/// Ordering of pass manager types is important here.
54193323Sedenum PassManagerType {
55193323Sed  PMT_Unknown = 0,
56234353Sdim  PMT_ModulePassManager = 1, ///< MPPassManager
57203954Srdivacky  PMT_CallGraphPassManager,  ///< CGPassManager
58203954Srdivacky  PMT_FunctionPassManager,   ///< FPPassManager
59203954Srdivacky  PMT_LoopPassManager,       ///< LPPassManager
60218893Sdim  PMT_RegionPassManager,     ///< RGPassManager
61203954Srdivacky  PMT_BasicBlockPassManager, ///< BBPassManager
62193323Sed  PMT_Last
63193323Sed};
64193323Sed
65202878Srdivacky// Different types of passes.
66202878Srdivackyenum PassKind {
67202878Srdivacky  PT_BasicBlock,
68218893Sdim  PT_Region,
69202878Srdivacky  PT_Loop,
70202878Srdivacky  PT_Function,
71202878Srdivacky  PT_CallGraphSCC,
72202878Srdivacky  PT_Module,
73202878Srdivacky  PT_PassManager
74202878Srdivacky};
75218893Sdim
76193323Sed//===----------------------------------------------------------------------===//
77193323Sed/// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
78193323Sed/// interprocedural optimization or you do not fit into any of the more
79193323Sed/// constrained passes described below.
80193323Sed///
81193323Sedclass Pass {
82193323Sed  AnalysisResolver *Resolver;  // Used to resolve analysis
83212904Sdim  const void *PassID;
84202878Srdivacky  PassKind Kind;
85193323Sed  void operator=(const Pass&);  // DO NOT IMPLEMENT
86193323Sed  Pass(const Pass &);           // DO NOT IMPLEMENT
87234353Sdim
88193323Sedpublic:
89234353Sdim  explicit Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
90193323Sed  virtual ~Pass();
91193323Sed
92234353Sdim
93202878Srdivacky  PassKind getPassKind() const { return Kind; }
94234353Sdim
95193323Sed  /// getPassName - Return a nice clean name for a pass.  This usually
96193323Sed  /// implemented in terms of the name that is registered by one of the
97193323Sed  /// Registration templates, but can be overloaded directly.
98193323Sed  ///
99193323Sed  virtual const char *getPassName() const;
100193323Sed
101212904Sdim  /// getPassID - Return the PassID number that corresponds to this pass.
102234353Sdim  AnalysisID getPassID() const {
103212904Sdim    return PassID;
104212904Sdim  }
105193323Sed
106193323Sed  /// print - Print out the internal state of the pass.  This is called by
107193323Sed  /// Analyze to print out the contents of an analysis.  Otherwise it is not
108193323Sed  /// necessary to implement this method.  Beware that the module pointer MAY be
109193323Sed  /// null.  This automatically forwards to a virtual function that does not
110193323Sed  /// provide the Module* in case the analysis doesn't need it it can just be
111193323Sed  /// ignored.
112193323Sed  ///
113198090Srdivacky  virtual void print(raw_ostream &O, const Module *M) const;
114198090Srdivacky  void dump() const; // dump - Print to stderr.
115193323Sed
116206124Srdivacky  /// createPrinterPass - Get a Pass appropriate to print the IR this
117221345Sdim  /// pass operates on (Module, Function or MachineFunction).
118206124Srdivacky  virtual Pass *createPrinterPass(raw_ostream &O,
119206124Srdivacky                                  const std::string &Banner) const = 0;
120206124Srdivacky
121193323Sed  /// Each pass is responsible for assigning a pass manager to itself.
122234353Sdim  /// PMS is the stack of available pass manager.
123234353Sdim  virtual void assignPassManager(PMStack &,
124212904Sdim                                 PassManagerType) {}
125193323Sed  /// Check if available pass managers are suitable for this pass or not.
126200581Srdivacky  virtual void preparePassManager(PMStack &);
127234353Sdim
128193323Sed  ///  Return what kind of Pass Manager can manage this pass.
129200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
130193323Sed
131193323Sed  // Access AnalysisResolver
132210299Sed  void setResolver(AnalysisResolver *AR);
133210299Sed  AnalysisResolver *getResolver() const { return Resolver; }
134193323Sed
135193323Sed  /// getAnalysisUsage - This function should be overriden by passes that need
136193323Sed  /// analysis information to do their job.  If a pass specifies that it uses a
137193323Sed  /// particular analysis result to this function, it can then use the
138193323Sed  /// getAnalysis<AnalysisType>() function, below.
139193323Sed  ///
140200581Srdivacky  virtual void getAnalysisUsage(AnalysisUsage &) const;
141193323Sed
142193323Sed  /// releaseMemory() - This member can be implemented by a pass if it wants to
143193323Sed  /// be able to release its memory when it is no longer needed.  The default
144193323Sed  /// behavior of passes is to hold onto memory for the entire duration of their
145193323Sed  /// lifetime (which is the entire compile time).  For pipelined passes, this
146193323Sed  /// is not a big deal because that memory gets recycled every time the pass is
147193323Sed  /// invoked on another program unit.  For IP passes, it is more important to
148193323Sed  /// free memory when it is unused.
149193323Sed  ///
150193323Sed  /// Optionally implement this function to release pass memory when it is no
151193323Sed  /// longer used.
152193323Sed  ///
153200581Srdivacky  virtual void releaseMemory();
154193323Sed
155202878Srdivacky  /// getAdjustedAnalysisPointer - This method is used when a pass implements
156202878Srdivacky  /// an analysis interface through multiple inheritance.  If needed, it should
157202878Srdivacky  /// override this to adjust the this pointer as needed for the specified pass
158202878Srdivacky  /// info.
159212904Sdim  virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
160210299Sed  virtual ImmutablePass *getAsImmutablePass();
161210299Sed  virtual PMDataManager *getAsPMDataManager();
162234353Sdim
163193323Sed  /// verifyAnalysis() - This member can be implemented by a analysis pass to
164234353Sdim  /// check state of analysis information.
165200581Srdivacky  virtual void verifyAnalysis() const;
166193323Sed
167193323Sed  // dumpPassStructure - Implement the -debug-passes=PassStructure option
168193323Sed  virtual void dumpPassStructure(unsigned Offset = 0);
169193323Sed
170193323Sed  // lookupPassInfo - Return the pass info object for the specified pass class,
171193323Sed  // or null if it is not known.
172212904Sdim  static const PassInfo *lookupPassInfo(const void *TI);
173193323Sed
174198090Srdivacky  // lookupPassInfo - Return the pass info object for the pass with the given
175198090Srdivacky  // argument string, or null if it is not known.
176199481Srdivacky  static const PassInfo *lookupPassInfo(StringRef Arg);
177198090Srdivacky
178234353Sdim  // createPass - Create a object for the specified pass class,
179234353Sdim  // or null if it is not known.
180234353Sdim  static Pass *createPass(AnalysisID ID);
181234353Sdim
182193323Sed  /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
183193323Sed  /// get analysis information that might be around, for example to update it.
184193323Sed  /// This is different than getAnalysis in that it can fail (if the analysis
185193323Sed  /// results haven't been computed), so should only be used if you can handle
186193323Sed  /// the case when the analysis is not available.  This method is often used by
187193323Sed  /// transformation APIs to update analysis results for a pass automatically as
188193323Sed  /// the transform is performed.
189193323Sed  ///
190193323Sed  template<typename AnalysisType> AnalysisType *
191193323Sed    getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
192193323Sed
193193323Sed  /// mustPreserveAnalysisID - This method serves the same function as
194193323Sed  /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
195193323Sed  /// obviously cannot give you a properly typed instance of the class if you
196193323Sed  /// don't have the class name available (use getAnalysisIfAvailable if you
197193323Sed  /// do), but it can tell you if you need to preserve the pass at least.
198193323Sed  ///
199212904Sdim  bool mustPreserveAnalysisID(char &AID) const;
200193323Sed
201193323Sed  /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
202193323Sed  /// to the analysis information that they claim to use by overriding the
203193323Sed  /// getAnalysisUsage function.
204193323Sed  ///
205193323Sed  template<typename AnalysisType>
206193323Sed  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
207193323Sed
208193323Sed  template<typename AnalysisType>
209198090Srdivacky  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
210193323Sed
211193323Sed  template<typename AnalysisType>
212212904Sdim  AnalysisType &getAnalysisID(AnalysisID PI) const;
213193323Sed
214193323Sed  template<typename AnalysisType>
215212904Sdim  AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
216193323Sed};
217193323Sed
218193323Sed
219193323Sed//===----------------------------------------------------------------------===//
220193323Sed/// ModulePass class - This class is used to implement unstructured
221193323Sed/// interprocedural optimizations and analyses.  ModulePasses may do anything
222193323Sed/// they want to the program.
223193323Sed///
224193323Sedclass ModulePass : public Pass {
225193323Sedpublic:
226206124Srdivacky  /// createPrinterPass - Get a module printer pass.
227206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
228206124Srdivacky
229193323Sed  /// runOnModule - Virtual method overriden by subclasses to process the module
230193323Sed  /// being operated on.
231193323Sed  virtual bool runOnModule(Module &M) = 0;
232193323Sed
233234353Sdim  virtual void assignPassManager(PMStack &PMS,
234212904Sdim                                 PassManagerType T);
235193323Sed
236193323Sed  ///  Return what kind of Pass Manager can manage this pass.
237200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
238193323Sed
239212904Sdim  explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
240193323Sed  // Force out-of-line virtual method.
241193323Sed  virtual ~ModulePass();
242193323Sed};
243193323Sed
244193323Sed
245193323Sed//===----------------------------------------------------------------------===//
246193323Sed/// ImmutablePass class - This class is used to provide information that does
247193323Sed/// not need to be run.  This is useful for things like target information and
248193323Sed/// "basic" versions of AnalysisGroups.
249193323Sed///
250193323Sedclass ImmutablePass : public ModulePass {
251193323Sedpublic:
252193323Sed  /// initializePass - This method may be overriden by immutable passes to allow
253193323Sed  /// them to perform various initialization actions they require.  This is
254193323Sed  /// primarily because an ImmutablePass can "require" another ImmutablePass,
255193323Sed  /// and if it does, the overloaded version of initializePass may get access to
256193323Sed  /// these passes with getAnalysis<>.
257193323Sed  ///
258200581Srdivacky  virtual void initializePass();
259193323Sed
260202878Srdivacky  virtual ImmutablePass *getAsImmutablePass() { return this; }
261202878Srdivacky
262193323Sed  /// ImmutablePasses are never run.
263193323Sed  ///
264193323Sed  bool runOnModule(Module &) { return false; }
265193323Sed
266234353Sdim  explicit ImmutablePass(char &pid)
267193323Sed  : ModulePass(pid) {}
268234353Sdim
269193323Sed  // Force out-of-line virtual method.
270193323Sed  virtual ~ImmutablePass();
271193323Sed};
272193323Sed
273193323Sed//===----------------------------------------------------------------------===//
274193323Sed/// FunctionPass class - This class is used to implement most global
275193323Sed/// optimizations.  Optimizations should subclass this class if they meet the
276193323Sed/// following constraints:
277193323Sed///
278193323Sed///  1. Optimizations are organized globally, i.e., a function at a time
279193323Sed///  2. Optimizing a function does not cause the addition or removal of any
280193323Sed///     functions in the module
281193323Sed///
282193323Sedclass FunctionPass : public Pass {
283193323Sedpublic:
284212904Sdim  explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
285193323Sed
286206124Srdivacky  /// createPrinterPass - Get a function printer pass.
287206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
288206124Srdivacky
289193323Sed  /// doInitialization - Virtual method overridden by subclasses to do
290193323Sed  /// any necessary per-module initialization.
291193323Sed  ///
292200581Srdivacky  virtual bool doInitialization(Module &);
293234353Sdim
294193323Sed  /// runOnFunction - Virtual method overriden by subclasses to do the
295193323Sed  /// per-function processing of the pass.
296193323Sed  ///
297193323Sed  virtual bool runOnFunction(Function &F) = 0;
298193323Sed
299193323Sed  /// doFinalization - Virtual method overriden by subclasses to do any post
300193323Sed  /// processing needed after all passes have run.
301193323Sed  ///
302200581Srdivacky  virtual bool doFinalization(Module &);
303193323Sed
304234353Sdim  virtual void assignPassManager(PMStack &PMS,
305212904Sdim                                 PassManagerType T);
306193323Sed
307193323Sed  ///  Return what kind of Pass Manager can manage this pass.
308200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
309193323Sed};
310193323Sed
311193323Sed
312193323Sed
313193323Sed//===----------------------------------------------------------------------===//
314193323Sed/// BasicBlockPass class - This class is used to implement most local
315193323Sed/// optimizations.  Optimizations should subclass this class if they
316193323Sed/// meet the following constraints:
317193323Sed///   1. Optimizations are local, operating on either a basic block or
318193323Sed///      instruction at a time.
319193323Sed///   2. Optimizations do not modify the CFG of the contained function, or any
320193323Sed///      other basic block in the function.
321193323Sed///   3. Optimizations conform to all of the constraints of FunctionPasses.
322193323Sed///
323193323Sedclass BasicBlockPass : public Pass {
324193323Sedpublic:
325212904Sdim  explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
326193323Sed
327221345Sdim  /// createPrinterPass - Get a basic block printer pass.
328206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
329206124Srdivacky
330193323Sed  /// doInitialization - Virtual method overridden by subclasses to do
331193323Sed  /// any necessary per-module initialization.
332193323Sed  ///
333200581Srdivacky  virtual bool doInitialization(Module &);
334193323Sed
335193323Sed  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
336193323Sed  /// to do any necessary per-function initialization.
337193323Sed  ///
338200581Srdivacky  virtual bool doInitialization(Function &);
339193323Sed
340193323Sed  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
341193323Sed  /// per-basicblock processing of the pass.
342193323Sed  ///
343193323Sed  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
344193323Sed
345193323Sed  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
346193323Sed  /// do any post processing needed after all passes have run.
347193323Sed  ///
348200581Srdivacky  virtual bool doFinalization(Function &);
349193323Sed
350193323Sed  /// doFinalization - Virtual method overriden by subclasses to do any post
351193323Sed  /// processing needed after all passes have run.
352193323Sed  ///
353200581Srdivacky  virtual bool doFinalization(Module &);
354193323Sed
355234353Sdim  virtual void assignPassManager(PMStack &PMS,
356212904Sdim                                 PassManagerType T);
357193323Sed
358193323Sed  ///  Return what kind of Pass Manager can manage this pass.
359200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
360193323Sed};
361193323Sed
362193323Sed/// If the user specifies the -time-passes argument on an LLVM tool command line
363193323Sed/// then the value of this boolean will be true, otherwise false.
364193323Sed/// @brief This is the storage for the -time-passes option.
365193323Sedextern bool TimePassesIsEnabled;
366193323Sed
367193323Sed} // End llvm namespace
368193323Sed
369193323Sed// Include support files that contain important APIs commonly used by Passes,
370193323Sed// but that we want to separate out to make it easier to read the header files.
371193323Sed//
372193323Sed#include "llvm/PassSupport.h"
373193323Sed#include "llvm/PassAnalysisSupport.h"
374193323Sed
375193323Sed#endif
376