Fix memory leak.
[oota-llvm.git] / include / llvm / Pass.h
1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Support/Streams.h"
33 #include <vector>
34 #include <deque>
35 #include <map>
36 #include <iosfwd>
37 #include <cassert>
38
39 namespace llvm {
40
41 class Value;
42 class BasicBlock;
43 class Function;
44 class Module;
45 class AnalysisUsage;
46 class PassInfo;
47 class ImmutablePass;
48 class PMStack;
49 class AnalysisResolver;
50 class PMDataManager;
51
52 // AnalysisID - Use the PassInfo to identify a pass...
53 typedef const PassInfo* 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.
58 enum 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 typedef enum PassManagerType PassManagerType;
69
70 //===----------------------------------------------------------------------===//
71 /// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
72 /// interprocedural optimization or you do not fit into any of the more
73 /// constrained passes described below.
74 ///
75 class Pass {
76   AnalysisResolver *Resolver;  // Used to resolve analysis
77   intptr_t PassID;
78
79   // AnalysisImpls - This keeps track of which passes implement the interfaces
80   // that are required by the current pass (to implement getAnalysis()).
81   //
82   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
83
84   void operator=(const Pass&);  // DO NOT IMPLEMENT
85   Pass(const Pass &);           // DO NOT IMPLEMENT
86 public:
87   explicit Pass(intptr_t pid) : Resolver(0), PassID(pid) {}
88   virtual ~Pass();
89
90   /// getPassName - Return a nice clean name for a pass.  This usually
91   /// implemented in terms of the name that is registered by one of the
92   /// Registration templates, but can be overloaded directly, and if nothing
93   /// else is available, C++ RTTI will be consulted to get a SOMEWHAT
94   /// intelligible name for the pass.
95   ///
96   virtual const char *getPassName() const;
97
98   /// getPassInfo - Return the PassInfo data structure that corresponds to this
99   /// pass...  If the pass has not been registered, this will return null.
100   ///
101   const PassInfo *getPassInfo() const;
102
103   /// runPass - Run this pass, returning true if a modification was made to the
104   /// module argument.  This should be implemented by all concrete subclasses.
105   ///
106   virtual bool runPass(Module &M) { return false; }
107   virtual bool runPass(BasicBlock&) { return false; }
108
109   /// print - Print out the internal state of the pass.  This is called by
110   /// Analyze to print out the contents of an analysis.  Otherwise it is not
111   /// necessary to implement this method.  Beware that the module pointer MAY be
112   /// null.  This automatically forwards to a virtual function that does not
113   /// provide the Module* in case the analysis doesn't need it it can just be
114   /// ignored.
115   ///
116   virtual void print(std::ostream &O, const Module *M) const;
117   void print(std::ostream *O, const Module *M) const { if (O) print(*O, M); }
118   void dump() const; // dump - call print(std::cerr, 0);
119
120   /// Each pass is responsible for assigning a pass manager to itself.
121   /// PMS is the stack of available pass manager. 
122   virtual void assignPassManager(PMStack &PMS, 
123                                  PassManagerType T = PMT_Unknown) {}
124   /// Check if available pass managers are suitable for this pass or not.
125   virtual void preparePassManager(PMStack &PMS) {}
126   
127   ///  Return what kind of Pass Manager can manage this pass.
128   virtual PassManagerType getPotentialPassManagerType() const {
129     return PMT_Unknown; 
130   }
131
132   // Access AnalysisResolver
133   inline void setResolver(AnalysisResolver *AR) { 
134     assert (!Resolver && "Resolver is already set");
135     Resolver = AR; 
136   }
137   inline AnalysisResolver *getResolver() { 
138     assert (Resolver && "Resolver is not set");
139     return Resolver; 
140   }
141
142   /// getAnalysisUsage - This function should be overriden by passes that need
143   /// analysis information to do their job.  If a pass specifies that it uses a
144   /// particular analysis result to this function, it can then use the
145   /// getAnalysis<AnalysisType>() function, below.
146   ///
147   virtual void getAnalysisUsage(AnalysisUsage &Info) const {
148     // By default, no analysis results are used, all are invalidated.
149   }
150
151   /// releaseMemory() - This member can be implemented by a pass if it wants to
152   /// be able to release its memory when it is no longer needed.  The default
153   /// behavior of passes is to hold onto memory for the entire duration of their
154   /// lifetime (which is the entire compile time).  For pipelined passes, this
155   /// is not a big deal because that memory gets recycled every time the pass is
156   /// invoked on another program unit.  For IP passes, it is more important to
157   /// free memory when it is unused.
158   ///
159   /// Optionally implement this function to release pass memory when it is no
160   /// longer used.
161   ///
162   virtual void releaseMemory() {}
163
164   // dumpPassStructure - Implement the -debug-passes=PassStructure option
165   virtual void dumpPassStructure(unsigned Offset = 0);
166
167   template<typename AnalysisClass>
168   static const PassInfo *getClassPassInfo() {
169     return lookupPassInfo((intptr_t)&AnalysisClass::ID);
170   }
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(intptr_t TI);
175
176   /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
177   /// to get to the analysis information that might be around that needs to be
178   /// updated.  This is different than getAnalysis in that it can fail (ie the
179   /// analysis results haven't been computed), so should only be used if you
180   /// provide the capability to update an analysis that exists.  This method is
181   /// often used by transformation APIs to update analysis results for a pass
182   /// automatically as the transform is performed.
183   ///
184   template<typename AnalysisType>
185   AnalysisType *getAnalysisToUpdate() const; // Defined in PassAnalysisSupport.h
186
187   /// mustPreserveAnalysisID - This method serves the same function as
188   /// getAnalysisToUpdate, but works if you just have an AnalysisID.  This
189   /// obviously cannot give you a properly typed instance of the class if you
190   /// don't have the class name available (use getAnalysisToUpdate if you do),
191   /// but it can tell you if you need to preserve the pass at least.
192   ///
193   bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const;
194
195   /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
196   /// to the analysis information that they claim to use by overriding the
197   /// getAnalysisUsage function.
198   ///
199   template<typename AnalysisType>
200   AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
201
202   template<typename AnalysisType>
203   AnalysisType &getAnalysis(Function &F); // Defined in PassanalysisSupport.h
204
205   template<typename AnalysisType>
206   AnalysisType &getAnalysisID(const PassInfo *PI) const;
207
208   template<typename AnalysisType>
209   AnalysisType &getAnalysisID(const PassInfo *PI, Function &F);
210 };
211
212 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
213   P.print(OS, 0); return OS;
214 }
215
216 //===----------------------------------------------------------------------===//
217 /// ModulePass class - This class is used to implement unstructured
218 /// interprocedural optimizations and analyses.  ModulePasses may do anything
219 /// they want to the program.
220 ///
221 class ModulePass : public Pass {
222 public:
223   /// runOnModule - Virtual method overriden by subclasses to process the module
224   /// being operated on.
225   virtual bool runOnModule(Module &M) = 0;
226
227   virtual bool runPass(Module &M) { return runOnModule(M); }
228   virtual bool runPass(BasicBlock&) { return false; }
229
230   virtual void assignPassManager(PMStack &PMS, 
231                                  PassManagerType T = PMT_ModulePassManager);
232
233   ///  Return what kind of Pass Manager can manage this pass.
234   virtual PassManagerType getPotentialPassManagerType() const {
235     return PMT_ModulePassManager;
236   }
237
238   explicit ModulePass(intptr_t pid) : Pass(pid) {}
239   // Force out-of-line virtual method.
240   virtual ~ModulePass();
241 };
242
243
244 //===----------------------------------------------------------------------===//
245 /// ImmutablePass class - This class is used to provide information that does
246 /// not need to be run.  This is useful for things like target information and
247 /// "basic" versions of AnalysisGroups.
248 ///
249 class ImmutablePass : public ModulePass {
250 public:
251   /// initializePass - This method may be overriden by immutable passes to allow
252   /// them to perform various initialization actions they require.  This is
253   /// primarily because an ImmutablePass can "require" another ImmutablePass,
254   /// and if it does, the overloaded version of initializePass may get access to
255   /// these passes with getAnalysis<>.
256   ///
257   virtual void initializePass() {}
258
259   /// ImmutablePasses are never run.
260   ///
261   virtual bool runOnModule(Module &M) { return false; }
262
263   explicit ImmutablePass(intptr_t pid) : ModulePass(pid) {}
264   // Force out-of-line virtual method.
265   virtual ~ImmutablePass();
266 };
267
268 //===----------------------------------------------------------------------===//
269 /// FunctionPass class - This class is used to implement most global
270 /// optimizations.  Optimizations should subclass this class if they meet the
271 /// following constraints:
272 ///
273 ///  1. Optimizations are organized globally, i.e., a function at a time
274 ///  2. Optimizing a function does not cause the addition or removal of any
275 ///     functions in the module
276 ///
277 class FunctionPass : public Pass {
278 public:
279   explicit FunctionPass(intptr_t pid) : Pass(pid) {}
280
281   /// doInitialization - Virtual method overridden by subclasses to do
282   /// any necessary per-module initialization.
283   ///
284   virtual bool doInitialization(Module &M) { return false; }
285
286   /// runOnFunction - Virtual method overriden by subclasses to do the
287   /// per-function processing of the pass.
288   ///
289   virtual bool runOnFunction(Function &F) = 0;
290
291   /// doFinalization - Virtual method overriden by subclasses to do any post
292   /// processing needed after all passes have run.
293   ///
294   virtual bool doFinalization(Module &M) { return false; }
295
296   /// runOnModule - On a module, we run this pass by initializing,
297   /// ronOnFunction'ing once for every function in the module, then by
298   /// finalizing.
299   ///
300   virtual bool runOnModule(Module &M);
301
302   /// run - On a function, we simply initialize, run the function, then
303   /// finalize.
304   ///
305   bool run(Function &F);
306
307   virtual void assignPassManager(PMStack &PMS, 
308                                  PassManagerType T = PMT_FunctionPassManager);
309
310   ///  Return what kind of Pass Manager can manage this pass.
311   virtual PassManagerType getPotentialPassManagerType() const {
312     return PMT_FunctionPassManager;
313   }
314 };
315
316
317
318 //===----------------------------------------------------------------------===//
319 /// BasicBlockPass class - This class is used to implement most local
320 /// optimizations.  Optimizations should subclass this class if they
321 /// meet the following constraints:
322 ///   1. Optimizations are local, operating on either a basic block or
323 ///      instruction at a time.
324 ///   2. Optimizations do not modify the CFG of the contained function, or any
325 ///      other basic block in the function.
326 ///   3. Optimizations conform to all of the constraints of FunctionPasses.
327 ///
328 class BasicBlockPass : public Pass {
329 public:
330   explicit BasicBlockPass(intptr_t pid) : Pass(pid) {}
331
332   /// doInitialization - Virtual method overridden by subclasses to do
333   /// any necessary per-module initialization.
334   ///
335   virtual bool doInitialization(Module &M) { return false; }
336
337   /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
338   /// to do any necessary per-function initialization.
339   ///
340   virtual bool doInitialization(Function &F) { return false; }
341
342   /// runOnBasicBlock - Virtual method overriden by subclasses to do the
343   /// per-basicblock processing of the pass.
344   ///
345   virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
346
347   /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
348   /// do any post processing needed after all passes have run.
349   ///
350   virtual bool doFinalization(Function &F) { return false; }
351
352   /// doFinalization - Virtual method overriden by subclasses to do any post
353   /// processing needed after all passes have run.
354   ///
355   virtual bool doFinalization(Module &M) { return false; }
356
357
358   // To run this pass on a function, we simply call runOnBasicBlock once for
359   // each function.
360   //
361   bool runOnFunction(Function &F);
362
363   /// To run directly on the basic block, we initialize, runOnBasicBlock, then
364   /// finalize.
365   ///
366   virtual bool runPass(Module &M) { return false; }
367   virtual bool runPass(BasicBlock &BB);
368
369   virtual void assignPassManager(PMStack &PMS, 
370                                  PassManagerType T = PMT_BasicBlockPassManager);
371
372   ///  Return what kind of Pass Manager can manage this pass.
373   virtual PassManagerType getPotentialPassManagerType() const {
374     return PMT_BasicBlockPassManager; 
375   }
376 };
377
378 /// PMStack
379 /// Top level pass manager (see PasManager.cpp) maintains active Pass Managers 
380 /// using PMStack. Each Pass implements assignPassManager() to connect itself
381 /// with appropriate manager. assignPassManager() walks PMStack to find
382 /// suitable manager.
383 ///
384 /// PMStack is just a wrapper around standard deque that overrides pop() and
385 /// push() methods.
386 class PMStack {
387 public:
388   typedef std::deque<PMDataManager *>::reverse_iterator iterator;
389   iterator begin() { return S.rbegin(); }
390   iterator end() { return S.rend(); }
391
392   void handleLastUserOverflow();
393
394   void pop();
395   inline PMDataManager *top() { return S.back(); }
396   void push(Pass *P);
397   inline bool empty() { return S.empty(); }
398
399   void dump();
400 private:
401   std::deque<PMDataManager *> S;
402 };
403
404
405 /// If the user specifies the -time-passes argument on an LLVM tool command line
406 /// then the value of this boolean will be true, otherwise false.
407 /// @brief This is the storage for the -time-passes option.
408 extern bool TimePassesIsEnabled;
409
410 } // End llvm namespace
411
412 // Include support files that contain important APIs commonly used by Passes,
413 // but that we want to separate out to make it easier to read the header files.
414 //
415 #include "llvm/PassSupport.h"
416 #include "llvm/PassAnalysisSupport.h"
417
418 #endif