Pass.h revision 210299
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
16193323Sed// executed as effeciently 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
32198892Srdivacky#include "llvm/System/DataTypes.h"
33206124Srdivacky
34206124Srdivacky#include <string>
35193323Sed#include <utility>
36193323Sed#include <vector>
37193323Sed
38193323Sednamespace llvm {
39193323Sed
40193323Sedclass BasicBlock;
41193323Sedclass Function;
42193323Sedclass Module;
43193323Sedclass AnalysisUsage;
44193323Sedclass PassInfo;
45193323Sedclass ImmutablePass;
46193323Sedclass PMStack;
47193323Sedclass AnalysisResolver;
48193323Sedclass PMDataManager;
49198090Srdivackyclass raw_ostream;
50198090Srdivackyclass StringRef;
51193323Sed
52193323Sed// AnalysisID - Use the PassInfo to identify a pass...
53193323Sedtypedef const PassInfo* AnalysisID;
54193323Sed
55193323Sed/// Different types of internal pass managers. External pass managers
56193323Sed/// (PassManager and FunctionPassManager) are not represented here.
57193323Sed/// Ordering of pass manager types is important here.
58193323Sedenum PassManagerType {
59193323Sed  PMT_Unknown = 0,
60203954Srdivacky  PMT_ModulePassManager = 1, ///< MPPassManager
61203954Srdivacky  PMT_CallGraphPassManager,  ///< CGPassManager
62203954Srdivacky  PMT_FunctionPassManager,   ///< FPPassManager
63203954Srdivacky  PMT_LoopPassManager,       ///< LPPassManager
64203954Srdivacky  PMT_BasicBlockPassManager, ///< BBPassManager
65193323Sed  PMT_Last
66193323Sed};
67193323Sed
68202878Srdivacky// Different types of passes.
69202878Srdivackyenum PassKind {
70202878Srdivacky  PT_BasicBlock,
71202878Srdivacky  PT_Loop,
72202878Srdivacky  PT_Function,
73202878Srdivacky  PT_CallGraphSCC,
74202878Srdivacky  PT_Module,
75202878Srdivacky  PT_PassManager
76202878Srdivacky};
77202878Srdivacky
78193323Sed//===----------------------------------------------------------------------===//
79193323Sed/// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
80193323Sed/// interprocedural optimization or you do not fit into any of the more
81193323Sed/// constrained passes described below.
82193323Sed///
83193323Sedclass Pass {
84193323Sed  AnalysisResolver *Resolver;  // Used to resolve analysis
85193323Sed  intptr_t PassID;
86202878Srdivacky  PassKind Kind;
87193323Sed  void operator=(const Pass&);  // DO NOT IMPLEMENT
88193323Sed  Pass(const Pass &);           // DO NOT IMPLEMENT
89195340Sed
90193323Sedpublic:
91210299Sed  explicit Pass(PassKind K, intptr_t pid);
92210299Sed  explicit Pass(PassKind K, const void *pid);
93193323Sed  virtual ~Pass();
94193323Sed
95202878Srdivacky
96202878Srdivacky  PassKind getPassKind() const { return Kind; }
97202878Srdivacky
98193323Sed  /// getPassName - Return a nice clean name for a pass.  This usually
99193323Sed  /// implemented in terms of the name that is registered by one of the
100193323Sed  /// Registration templates, but can be overloaded directly.
101193323Sed  ///
102193323Sed  virtual const char *getPassName() const;
103193323Sed
104193323Sed  /// getPassInfo - Return the PassInfo data structure that corresponds to this
105193323Sed  /// pass...  If the pass has not been registered, this will return null.
106193323Sed  ///
107193323Sed  const PassInfo *getPassInfo() const;
108193323Sed
109193323Sed  /// print - Print out the internal state of the pass.  This is called by
110193323Sed  /// Analyze to print out the contents of an analysis.  Otherwise it is not
111193323Sed  /// necessary to implement this method.  Beware that the module pointer MAY be
112193323Sed  /// null.  This automatically forwards to a virtual function that does not
113193323Sed  /// provide the Module* in case the analysis doesn't need it it can just be
114193323Sed  /// ignored.
115193323Sed  ///
116198090Srdivacky  virtual void print(raw_ostream &O, const Module *M) const;
117198090Srdivacky  void dump() const; // dump - Print to stderr.
118193323Sed
119206124Srdivacky  /// createPrinterPass - Get a Pass appropriate to print the IR this
120206124Srdivacky  /// pass operates one (Module, Function or MachineFunction).
121206124Srdivacky  virtual Pass *createPrinterPass(raw_ostream &O,
122206124Srdivacky                                  const std::string &Banner) const = 0;
123206124Srdivacky
124193323Sed  /// Each pass is responsible for assigning a pass manager to itself.
125193323Sed  /// PMS is the stack of available pass manager.
126193323Sed  virtual void assignPassManager(PMStack &,
127193323Sed                                 PassManagerType = PMT_Unknown) {}
128193323Sed  /// Check if available pass managers are suitable for this pass or not.
129200581Srdivacky  virtual void preparePassManager(PMStack &);
130193323Sed
131193323Sed  ///  Return what kind of Pass Manager can manage this pass.
132200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
133193323Sed
134193323Sed  // Access AnalysisResolver
135210299Sed  void setResolver(AnalysisResolver *AR);
136210299Sed  AnalysisResolver *getResolver() const { return Resolver; }
137193323Sed
138193323Sed  /// getAnalysisUsage - This function should be overriden by passes that need
139193323Sed  /// analysis information to do their job.  If a pass specifies that it uses a
140193323Sed  /// particular analysis result to this function, it can then use the
141193323Sed  /// getAnalysis<AnalysisType>() function, below.
142193323Sed  ///
143200581Srdivacky  virtual void getAnalysisUsage(AnalysisUsage &) const;
144193323Sed
145193323Sed  /// releaseMemory() - This member can be implemented by a pass if it wants to
146193323Sed  /// be able to release its memory when it is no longer needed.  The default
147193323Sed  /// behavior of passes is to hold onto memory for the entire duration of their
148193323Sed  /// lifetime (which is the entire compile time).  For pipelined passes, this
149193323Sed  /// is not a big deal because that memory gets recycled every time the pass is
150193323Sed  /// invoked on another program unit.  For IP passes, it is more important to
151193323Sed  /// free memory when it is unused.
152193323Sed  ///
153193323Sed  /// Optionally implement this function to release pass memory when it is no
154193323Sed  /// longer used.
155193323Sed  ///
156200581Srdivacky  virtual void releaseMemory();
157193323Sed
158202878Srdivacky  /// getAdjustedAnalysisPointer - This method is used when a pass implements
159202878Srdivacky  /// an analysis interface through multiple inheritance.  If needed, it should
160202878Srdivacky  /// override this to adjust the this pointer as needed for the specified pass
161202878Srdivacky  /// info.
162210299Sed  virtual void *getAdjustedAnalysisPointer(const PassInfo *);
163210299Sed  virtual ImmutablePass *getAsImmutablePass();
164210299Sed  virtual PMDataManager *getAsPMDataManager();
165202878Srdivacky
166193323Sed  /// verifyAnalysis() - This member can be implemented by a analysis pass to
167193323Sed  /// check state of analysis information.
168200581Srdivacky  virtual void verifyAnalysis() const;
169193323Sed
170193323Sed  // dumpPassStructure - Implement the -debug-passes=PassStructure option
171193323Sed  virtual void dumpPassStructure(unsigned Offset = 0);
172193323Sed
173193323Sed  template<typename AnalysisClass>
174193323Sed  static const PassInfo *getClassPassInfo() {
175193323Sed    return lookupPassInfo(intptr_t(&AnalysisClass::ID));
176193323Sed  }
177193323Sed
178193323Sed  // lookupPassInfo - Return the pass info object for the specified pass class,
179193323Sed  // or null if it is not known.
180193323Sed  static const PassInfo *lookupPassInfo(intptr_t TI);
181193323Sed
182198090Srdivacky  // lookupPassInfo - Return the pass info object for the pass with the given
183198090Srdivacky  // argument string, or null if it is not known.
184199481Srdivacky  static const PassInfo *lookupPassInfo(StringRef Arg);
185198090Srdivacky
186193323Sed  /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
187193323Sed  /// get analysis information that might be around, for example to update it.
188193323Sed  /// This is different than getAnalysis in that it can fail (if the analysis
189193323Sed  /// results haven't been computed), so should only be used if you can handle
190193323Sed  /// the case when the analysis is not available.  This method is often used by
191193323Sed  /// transformation APIs to update analysis results for a pass automatically as
192193323Sed  /// the transform is performed.
193193323Sed  ///
194193323Sed  template<typename AnalysisType> AnalysisType *
195193323Sed    getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
196193323Sed
197193323Sed  /// mustPreserveAnalysisID - This method serves the same function as
198193323Sed  /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
199193323Sed  /// obviously cannot give you a properly typed instance of the class if you
200193323Sed  /// don't have the class name available (use getAnalysisIfAvailable if you
201193323Sed  /// do), but it can tell you if you need to preserve the pass at least.
202193323Sed  ///
203193323Sed  bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const;
204193323Sed
205193323Sed  /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
206193323Sed  /// to the analysis information that they claim to use by overriding the
207193323Sed  /// getAnalysisUsage function.
208193323Sed  ///
209193323Sed  template<typename AnalysisType>
210193323Sed  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
211193323Sed
212193323Sed  template<typename AnalysisType>
213198090Srdivacky  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
214193323Sed
215193323Sed  template<typename AnalysisType>
216193323Sed  AnalysisType &getAnalysisID(const PassInfo *PI) const;
217193323Sed
218193323Sed  template<typename AnalysisType>
219193323Sed  AnalysisType &getAnalysisID(const PassInfo *PI, Function &F);
220193323Sed};
221193323Sed
222193323Sed
223193323Sed//===----------------------------------------------------------------------===//
224193323Sed/// ModulePass class - This class is used to implement unstructured
225193323Sed/// interprocedural optimizations and analyses.  ModulePasses may do anything
226193323Sed/// they want to the program.
227193323Sed///
228193323Sedclass ModulePass : public Pass {
229193323Sedpublic:
230206124Srdivacky  /// createPrinterPass - Get a module printer pass.
231206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
232206124Srdivacky
233193323Sed  /// runOnModule - Virtual method overriden by subclasses to process the module
234193323Sed  /// being operated on.
235193323Sed  virtual bool runOnModule(Module &M) = 0;
236193323Sed
237193323Sed  virtual void assignPassManager(PMStack &PMS,
238193323Sed                                 PassManagerType T = PMT_ModulePassManager);
239193323Sed
240193323Sed  ///  Return what kind of Pass Manager can manage this pass.
241200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
242193323Sed
243202878Srdivacky  explicit ModulePass(intptr_t pid) : Pass(PT_Module, pid) {}
244202878Srdivacky  explicit ModulePass(const void *pid) : Pass(PT_Module, pid) {}
245193323Sed  // Force out-of-line virtual method.
246193323Sed  virtual ~ModulePass();
247193323Sed};
248193323Sed
249193323Sed
250193323Sed//===----------------------------------------------------------------------===//
251193323Sed/// ImmutablePass class - This class is used to provide information that does
252193323Sed/// not need to be run.  This is useful for things like target information and
253193323Sed/// "basic" versions of AnalysisGroups.
254193323Sed///
255193323Sedclass ImmutablePass : public ModulePass {
256193323Sedpublic:
257193323Sed  /// initializePass - This method may be overriden by immutable passes to allow
258193323Sed  /// them to perform various initialization actions they require.  This is
259193323Sed  /// primarily because an ImmutablePass can "require" another ImmutablePass,
260193323Sed  /// and if it does, the overloaded version of initializePass may get access to
261193323Sed  /// these passes with getAnalysis<>.
262193323Sed  ///
263200581Srdivacky  virtual void initializePass();
264193323Sed
265202878Srdivacky  virtual ImmutablePass *getAsImmutablePass() { return this; }
266202878Srdivacky
267193323Sed  /// ImmutablePasses are never run.
268193323Sed  ///
269193323Sed  bool runOnModule(Module &) { return false; }
270193323Sed
271193323Sed  explicit ImmutablePass(intptr_t pid) : ModulePass(pid) {}
272193323Sed  explicit ImmutablePass(const void *pid)
273193323Sed  : ModulePass(pid) {}
274193323Sed
275193323Sed  // Force out-of-line virtual method.
276193323Sed  virtual ~ImmutablePass();
277193323Sed};
278193323Sed
279193323Sed//===----------------------------------------------------------------------===//
280193323Sed/// FunctionPass class - This class is used to implement most global
281193323Sed/// optimizations.  Optimizations should subclass this class if they meet the
282193323Sed/// following constraints:
283193323Sed///
284193323Sed///  1. Optimizations are organized globally, i.e., a function at a time
285193323Sed///  2. Optimizing a function does not cause the addition or removal of any
286193323Sed///     functions in the module
287193323Sed///
288193323Sedclass FunctionPass : public Pass {
289193323Sedpublic:
290202878Srdivacky  explicit FunctionPass(intptr_t pid) : Pass(PT_Function, pid) {}
291202878Srdivacky  explicit FunctionPass(const void *pid) : Pass(PT_Function, pid) {}
292193323Sed
293206124Srdivacky  /// createPrinterPass - Get a function printer pass.
294206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
295206124Srdivacky
296193323Sed  /// doInitialization - Virtual method overridden by subclasses to do
297193323Sed  /// any necessary per-module initialization.
298193323Sed  ///
299200581Srdivacky  virtual bool doInitialization(Module &);
300198090Srdivacky
301193323Sed  /// runOnFunction - Virtual method overriden by subclasses to do the
302193323Sed  /// per-function processing of the pass.
303193323Sed  ///
304193323Sed  virtual bool runOnFunction(Function &F) = 0;
305193323Sed
306193323Sed  /// doFinalization - Virtual method overriden by subclasses to do any post
307193323Sed  /// processing needed after all passes have run.
308193323Sed  ///
309200581Srdivacky  virtual bool doFinalization(Module &);
310193323Sed
311193323Sed  /// runOnModule - On a module, we run this pass by initializing,
312193323Sed  /// ronOnFunction'ing once for every function in the module, then by
313193323Sed  /// finalizing.
314193323Sed  ///
315193323Sed  virtual bool runOnModule(Module &M);
316193323Sed
317193323Sed  /// run - On a function, we simply initialize, run the function, then
318193323Sed  /// finalize.
319193323Sed  ///
320193323Sed  bool run(Function &F);
321193323Sed
322193323Sed  virtual void assignPassManager(PMStack &PMS,
323193323Sed                                 PassManagerType T = PMT_FunctionPassManager);
324193323Sed
325193323Sed  ///  Return what kind of Pass Manager can manage this pass.
326200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
327193323Sed};
328193323Sed
329193323Sed
330193323Sed
331193323Sed//===----------------------------------------------------------------------===//
332193323Sed/// BasicBlockPass class - This class is used to implement most local
333193323Sed/// optimizations.  Optimizations should subclass this class if they
334193323Sed/// meet the following constraints:
335193323Sed///   1. Optimizations are local, operating on either a basic block or
336193323Sed///      instruction at a time.
337193323Sed///   2. Optimizations do not modify the CFG of the contained function, or any
338193323Sed///      other basic block in the function.
339193323Sed///   3. Optimizations conform to all of the constraints of FunctionPasses.
340193323Sed///
341193323Sedclass BasicBlockPass : public Pass {
342193323Sedpublic:
343202878Srdivacky  explicit BasicBlockPass(intptr_t pid) : Pass(PT_BasicBlock, pid) {}
344202878Srdivacky  explicit BasicBlockPass(const void *pid) : Pass(PT_BasicBlock, pid) {}
345193323Sed
346206124Srdivacky  /// createPrinterPass - Get a function printer pass.
347206124Srdivacky  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
348206124Srdivacky
349193323Sed  /// doInitialization - Virtual method overridden by subclasses to do
350193323Sed  /// any necessary per-module initialization.
351193323Sed  ///
352200581Srdivacky  virtual bool doInitialization(Module &);
353193323Sed
354193323Sed  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
355193323Sed  /// to do any necessary per-function initialization.
356193323Sed  ///
357200581Srdivacky  virtual bool doInitialization(Function &);
358193323Sed
359193323Sed  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
360193323Sed  /// per-basicblock processing of the pass.
361193323Sed  ///
362193323Sed  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
363193323Sed
364193323Sed  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
365193323Sed  /// do any post processing needed after all passes have run.
366193323Sed  ///
367200581Srdivacky  virtual bool doFinalization(Function &);
368193323Sed
369193323Sed  /// doFinalization - Virtual method overriden by subclasses to do any post
370193323Sed  /// processing needed after all passes have run.
371193323Sed  ///
372200581Srdivacky  virtual bool doFinalization(Module &);
373193323Sed
374193323Sed
375193323Sed  // To run this pass on a function, we simply call runOnBasicBlock once for
376193323Sed  // each function.
377193323Sed  //
378193323Sed  bool runOnFunction(Function &F);
379193323Sed
380193323Sed  virtual void assignPassManager(PMStack &PMS,
381193323Sed                                 PassManagerType T = PMT_BasicBlockPassManager);
382193323Sed
383193323Sed  ///  Return what kind of Pass Manager can manage this pass.
384200581Srdivacky  virtual PassManagerType getPotentialPassManagerType() const;
385193323Sed};
386193323Sed
387193323Sed/// If the user specifies the -time-passes argument on an LLVM tool command line
388193323Sed/// then the value of this boolean will be true, otherwise false.
389193323Sed/// @brief This is the storage for the -time-passes option.
390193323Sedextern bool TimePassesIsEnabled;
391193323Sed
392193323Sed} // End llvm namespace
393193323Sed
394193323Sed// Include support files that contain important APIs commonly used by Passes,
395193323Sed// but that we want to separate out to make it easier to read the header files.
396193323Sed//
397193323Sed#include "llvm/PassSupport.h"
398193323Sed#include "llvm/PassAnalysisSupport.h"
399193323Sed
400193323Sed#endif
401