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
32243830Sdim#include "llvm/Support/Compiler.h"
33206124Srdivacky#include <string>
34193323Sed
35193323Sednamespace llvm {
36193323Sed
37193323Sedclass BasicBlock;
38193323Sedclass Function;
39193323Sedclass Module;
40193323Sedclass AnalysisUsage;
41193323Sedclass PassInfo;
42193323Sedclass ImmutablePass;
43193323Sedclass PMStack;
44193323Sedclass AnalysisResolver;
45193323Sedclass PMDataManager;
46198090Srdivackyclass raw_ostream;
47198090Srdivackyclass StringRef;
48193323Sed
49193323Sed// AnalysisID - Use the PassInfo to identify a pass...
50212904Sdimtypedef const void* AnalysisID;
51193323Sed
52193323Sed/// Different types of internal pass managers. External pass managers
53193323Sed/// (PassManager and FunctionPassManager) are not represented here.
54193323Sed/// Ordering of pass manager types is important here.
55193323Sedenum PassManagerType {
56193323Sed  PMT_Unknown = 0,
57234353Sdim  PMT_ModulePassManager = 1, ///< MPPassManager
58203954Srdivacky  PMT_CallGraphPassManager,  ///< CGPassManager
59203954Srdivacky  PMT_FunctionPassManager,   ///< FPPassManager
60203954Srdivacky  PMT_LoopPassManager,       ///< LPPassManager
61218893Sdim  PMT_RegionPassManager,     ///< RGPassManager
62203954Srdivacky  PMT_BasicBlockPassManager, ///< BBPassManager
63193323Sed  PMT_Last
64193323Sed};
65193323Sed
66202878Srdivacky// Different types of passes.
67202878Srdivackyenum PassKind {
68202878Srdivacky  PT_BasicBlock,
69218893Sdim  PT_Region,
70202878Srdivacky  PT_Loop,
71202878Srdivacky  PT_Function,
72202878Srdivacky  PT_CallGraphSCC,
73202878Srdivacky  PT_Module,
74202878Srdivacky  PT_PassManager
75202878Srdivacky};
76218893Sdim
77193323Sed//===----------------------------------------------------------------------===//
78193323Sed/// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
79193323Sed/// interprocedural optimization or you do not fit into any of the more
80193323Sed/// constrained passes described below.
81193323Sed///
82193323Sedclass Pass {
83193323Sed  AnalysisResolver *Resolver;  // Used to resolve analysis
84212904Sdim  const void *PassID;
85202878Srdivacky  PassKind Kind;
86243830Sdim  void operator=(const Pass&) LLVM_DELETED_FUNCTION;
87243830Sdim  Pass(const Pass &) LLVM_DELETED_FUNCTION;
88234353Sdim
89193323Sedpublic:
90234353Sdim  explicit Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
91193323Sed  virtual ~Pass();
92193323Sed
93234353Sdim
94202878Srdivacky  PassKind getPassKind() const { return Kind; }
95234353Sdim
96193323Sed  /// getPassName - Return a nice clean name for a pass.  This usually
97193323Sed  /// implemented in terms of the name that is registered by one of the
98193323Sed  /// Registration templates, but can be overloaded directly.
99193323Sed  ///
100193323Sed  virtual const char *getPassName() const;
101193323Sed
102212904Sdim  /// getPassID - Return the PassID number that corresponds to this pass.
103234353Sdim  AnalysisID getPassID() const {
104212904Sdim    return PassID;
105212904Sdim  }
106193323Sed
107249423Sdim  /// doInitialization - Virtual method overridden by subclasses to do
108249423Sdim  /// any necessary initialization before any pass is run.
109249423Sdim  ///
110249423Sdim  virtual bool doInitialization(Module &)  { return false; }
111249423Sdim
112249423Sdim  /// doFinalization - Virtual method overriden by subclasses to do any
113249423Sdim  /// necessary clean up after all passes have run.
114249423Sdim  ///
115249423Sdim  virtual bool doFinalization(Module &) { return false; }
116249423Sdim
117193323Sed  /// print - Print out the internal state of the pass.  This is called by
118193323Sed  /// Analyze to print out the contents of an analysis.  Otherwise it is not
119193323Sed  /// necessary to implement this method.  Beware that the module pointer MAY be
120193323Sed  /// null.  This automatically forwards to a virtual function that does not
121193323Sed  /// provide the Module* in case the analysis doesn't need it it can just be
122193323Sed  /// ignored.
123193323Sed  ///
124198090Srdivacky  virtual void print(raw_ostream &O, const Module *M) const;
125198090Srdivacky  void dump() const; // dump - Print to stderr.
126193323Sed
127206124Srdivacky  /// createPrinterPass - Get a Pass appropriate to print the IR this
128221345Sdim  /// pass operates on (Module, Function or MachineFunction).
129206124Srdivacky  virtual Pass *createPrinterPass(raw_ostream &O,
130206124Srdivacky                                  const std::string &Banner) const = 0;
131206124Srdivacky
132193323Sed  /// Each pass is responsible for assigning a pass manager to itself.
133234353Sdim  /// PMS is the stack of available pass manager.
134234353Sdim  virtual void assignPassManager(PMStack &,
135212904Sdim                                 PassManagerType) {}
136193323Sed  /// Check if available pass managers are suitable for this pass or not.
137200581Srdivacky  virtual void preparePassManager(PMStack &);
138234353Sdim
139193323Sed  ///  Return what kind of Pass Manager can manage this pass.
140200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
141193323Sed
142193323Sed  // Access AnalysisResolver
143210299Sed  void setResolver(AnalysisResolver *AR);
144210299Sed  AnalysisResolver *getResolver() const { return Resolver; }
145193323Sed
146193323Sed  /// getAnalysisUsage - This function should be overriden by passes that need
147193323Sed  /// analysis information to do their job.  If a pass specifies that it uses a
148193323Sed  /// particular analysis result to this function, it can then use the
149193323Sed  /// getAnalysis<AnalysisType>() function, below.
150193323Sed  ///
151200581Srdivacky  virtual void getAnalysisUsage(AnalysisUsage &) const;
152193323Sed
153193323Sed  /// releaseMemory() - This member can be implemented by a pass if it wants to
154193323Sed  /// be able to release its memory when it is no longer needed.  The default
155193323Sed  /// behavior of passes is to hold onto memory for the entire duration of their
156193323Sed  /// lifetime (which is the entire compile time).  For pipelined passes, this
157193323Sed  /// is not a big deal because that memory gets recycled every time the pass is
158193323Sed  /// invoked on another program unit.  For IP passes, it is more important to
159193323Sed  /// free memory when it is unused.
160193323Sed  ///
161193323Sed  /// Optionally implement this function to release pass memory when it is no
162193323Sed  /// longer used.
163193323Sed  ///
164200581Srdivacky  virtual void releaseMemory();
165193323Sed
166202878Srdivacky  /// getAdjustedAnalysisPointer - This method is used when a pass implements
167202878Srdivacky  /// an analysis interface through multiple inheritance.  If needed, it should
168202878Srdivacky  /// override this to adjust the this pointer as needed for the specified pass
169202878Srdivacky  /// info.
170212904Sdim  virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
171210299Sed  virtual ImmutablePass *getAsImmutablePass();
172210299Sed  virtual PMDataManager *getAsPMDataManager();
173234353Sdim
174193323Sed  /// verifyAnalysis() - This member can be implemented by a analysis pass to
175234353Sdim  /// check state of analysis information.
176200581Srdivacky  virtual void verifyAnalysis() const;
177193323Sed
178193323Sed  // dumpPassStructure - Implement the -debug-passes=PassStructure option
179193323Sed  virtual void dumpPassStructure(unsigned Offset = 0);
180193323Sed
181193323Sed  // lookupPassInfo - Return the pass info object for the specified pass class,
182193323Sed  // or null if it is not known.
183212904Sdim  static const PassInfo *lookupPassInfo(const void *TI);
184193323Sed
185198090Srdivacky  // lookupPassInfo - Return the pass info object for the pass with the given
186198090Srdivacky  // argument string, or null if it is not known.
187199481Srdivacky  static const PassInfo *lookupPassInfo(StringRef Arg);
188198090Srdivacky
189234353Sdim  // createPass - Create a object for the specified pass class,
190234353Sdim  // or null if it is not known.
191234353Sdim  static Pass *createPass(AnalysisID ID);
192234353Sdim
193193323Sed  /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
194193323Sed  /// get analysis information that might be around, for example to update it.
195193323Sed  /// This is different than getAnalysis in that it can fail (if the analysis
196193323Sed  /// results haven't been computed), so should only be used if you can handle
197193323Sed  /// the case when the analysis is not available.  This method is often used by
198193323Sed  /// transformation APIs to update analysis results for a pass automatically as
199193323Sed  /// the transform is performed.
200193323Sed  ///
201193323Sed  template<typename AnalysisType> AnalysisType *
202193323Sed    getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
203193323Sed
204193323Sed  /// mustPreserveAnalysisID - This method serves the same function as
205193323Sed  /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
206193323Sed  /// obviously cannot give you a properly typed instance of the class if you
207193323Sed  /// don't have the class name available (use getAnalysisIfAvailable if you
208193323Sed  /// do), but it can tell you if you need to preserve the pass at least.
209193323Sed  ///
210212904Sdim  bool mustPreserveAnalysisID(char &AID) const;
211193323Sed
212193323Sed  /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
213193323Sed  /// to the analysis information that they claim to use by overriding the
214193323Sed  /// getAnalysisUsage function.
215193323Sed  ///
216193323Sed  template<typename AnalysisType>
217193323Sed  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
218193323Sed
219193323Sed  template<typename AnalysisType>
220198090Srdivacky  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
221193323Sed
222193323Sed  template<typename AnalysisType>
223212904Sdim  AnalysisType &getAnalysisID(AnalysisID PI) const;
224193323Sed
225193323Sed  template<typename AnalysisType>
226212904Sdim  AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
227193323Sed};
228193323Sed
229193323Sed
230193323Sed//===----------------------------------------------------------------------===//
231193323Sed/// ModulePass class - This class is used to implement unstructured
232193323Sed/// interprocedural optimizations and analyses.  ModulePasses may do anything
233193323Sed/// they want to the program.
234193323Sed///
235193323Sedclass ModulePass : public Pass {
236193323Sedpublic:
237206124Srdivacky  /// createPrinterPass - Get a module printer pass.
238206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
239206124Srdivacky
240193323Sed  /// runOnModule - Virtual method overriden by subclasses to process the module
241193323Sed  /// being operated on.
242193323Sed  virtual bool runOnModule(Module &M) = 0;
243193323Sed
244234353Sdim  virtual void assignPassManager(PMStack &PMS,
245212904Sdim                                 PassManagerType T);
246193323Sed
247193323Sed  ///  Return what kind of Pass Manager can manage this pass.
248200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
249193323Sed
250212904Sdim  explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
251193323Sed  // Force out-of-line virtual method.
252193323Sed  virtual ~ModulePass();
253193323Sed};
254193323Sed
255193323Sed
256193323Sed//===----------------------------------------------------------------------===//
257193323Sed/// ImmutablePass class - This class is used to provide information that does
258193323Sed/// not need to be run.  This is useful for things like target information and
259193323Sed/// "basic" versions of AnalysisGroups.
260193323Sed///
261193323Sedclass ImmutablePass : public ModulePass {
262193323Sedpublic:
263193323Sed  /// initializePass - This method may be overriden by immutable passes to allow
264193323Sed  /// them to perform various initialization actions they require.  This is
265193323Sed  /// primarily because an ImmutablePass can "require" another ImmutablePass,
266193323Sed  /// and if it does, the overloaded version of initializePass may get access to
267193323Sed  /// these passes with getAnalysis<>.
268193323Sed  ///
269200581Srdivacky  virtual void initializePass();
270193323Sed
271202878Srdivacky  virtual ImmutablePass *getAsImmutablePass() { return this; }
272202878Srdivacky
273193323Sed  /// ImmutablePasses are never run.
274193323Sed  ///
275193323Sed  bool runOnModule(Module &) { return false; }
276193323Sed
277234353Sdim  explicit ImmutablePass(char &pid)
278193323Sed  : ModulePass(pid) {}
279234353Sdim
280193323Sed  // Force out-of-line virtual method.
281193323Sed  virtual ~ImmutablePass();
282193323Sed};
283193323Sed
284193323Sed//===----------------------------------------------------------------------===//
285193323Sed/// FunctionPass class - This class is used to implement most global
286193323Sed/// optimizations.  Optimizations should subclass this class if they meet the
287193323Sed/// following constraints:
288193323Sed///
289193323Sed///  1. Optimizations are organized globally, i.e., a function at a time
290193323Sed///  2. Optimizing a function does not cause the addition or removal of any
291193323Sed///     functions in the module
292193323Sed///
293193323Sedclass FunctionPass : public Pass {
294193323Sedpublic:
295212904Sdim  explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
296193323Sed
297206124Srdivacky  /// createPrinterPass - Get a function printer pass.
298206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
299206124Srdivacky
300193323Sed  /// runOnFunction - Virtual method overriden by subclasses to do the
301193323Sed  /// per-function processing of the pass.
302193323Sed  ///
303193323Sed  virtual bool runOnFunction(Function &F) = 0;
304193323Sed
305234353Sdim  virtual void assignPassManager(PMStack &PMS,
306212904Sdim                                 PassManagerType T);
307193323Sed
308193323Sed  ///  Return what kind of Pass Manager can manage this pass.
309200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
310193323Sed};
311193323Sed
312193323Sed
313193323Sed
314193323Sed//===----------------------------------------------------------------------===//
315193323Sed/// BasicBlockPass class - This class is used to implement most local
316193323Sed/// optimizations.  Optimizations should subclass this class if they
317193323Sed/// meet the following constraints:
318193323Sed///   1. Optimizations are local, operating on either a basic block or
319193323Sed///      instruction at a time.
320193323Sed///   2. Optimizations do not modify the CFG of the contained function, or any
321193323Sed///      other basic block in the function.
322193323Sed///   3. Optimizations conform to all of the constraints of FunctionPasses.
323193323Sed///
324193323Sedclass BasicBlockPass : public Pass {
325193323Sedpublic:
326212904Sdim  explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
327193323Sed
328221345Sdim  /// createPrinterPass - Get a basic block printer pass.
329206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
330206124Srdivacky
331249423Sdim  using llvm::Pass::doInitialization;
332249423Sdim  using llvm::Pass::doFinalization;
333193323Sed
334193323Sed  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
335193323Sed  /// to do any necessary per-function initialization.
336193323Sed  ///
337200581Srdivacky  virtual bool doInitialization(Function &);
338193323Sed
339193323Sed  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
340193323Sed  /// per-basicblock processing of the pass.
341193323Sed  ///
342193323Sed  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
343193323Sed
344193323Sed  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
345193323Sed  /// do any post processing needed after all passes have run.
346193323Sed  ///
347200581Srdivacky  virtual bool doFinalization(Function &);
348193323Sed
349234353Sdim  virtual void assignPassManager(PMStack &PMS,
350212904Sdim                                 PassManagerType T);
351193323Sed
352193323Sed  ///  Return what kind of Pass Manager can manage this pass.
353200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
354193323Sed};
355193323Sed
356193323Sed/// If the user specifies the -time-passes argument on an LLVM tool command line
357193323Sed/// then the value of this boolean will be true, otherwise false.
358193323Sed/// @brief This is the storage for the -time-passes option.
359193323Sedextern bool TimePassesIsEnabled;
360193323Sed
361193323Sed} // End llvm namespace
362193323Sed
363193323Sed// Include support files that contain important APIs commonly used by Passes,
364193323Sed// but that we want to separate out to make it easier to read the header files.
365193323Sed//
366193323Sed#include "llvm/PassSupport.h"
367193323Sed#include "llvm/PassAnalysisSupport.h"
368193323Sed
369193323Sed#endif
370