Add helper method
[oota-llvm.git] / include / llvm / Pass.h
1 //===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=//
2 //
3 // This file defines a base class that indicates that a specified class is a
4 // transformation pass implementation.
5 //
6 // Pass's are designed this way so that it is possible to run passes in a cache
7 // and organizationally optimal order without having to specify it at the front
8 // end.  This allows arbitrary passes to be strung together and have them
9 // executed as effeciently as possible.
10 //
11 // Passes should extend one of the classes below, depending on the guarantees
12 // that it can make about what will be modified as it is run.  For example, most
13 // global optimizations should derive from FunctionPass, because they do not add
14 // or delete functions, they operate on the internals of the function.
15 //
16 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
17 // bottom), so the APIs exposed by these files are also automatically available
18 // to all users of this file.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_PASS_H
23 #define LLVM_PASS_H
24
25 #include <vector>
26 #include <map>
27 #include <iosfwd>
28 #include <typeinfo>
29 class Value;
30 class BasicBlock;
31 class Function;
32 class Module;
33 class AnalysisUsage;
34 class PassInfo;
35 class ImmutablePass;
36 template<class UnitType> class PassManagerT;
37 struct AnalysisResolver;
38
39 // AnalysisID - Use the PassInfo to identify a pass...
40 typedef const PassInfo* AnalysisID;
41
42 //===----------------------------------------------------------------------===//
43 /// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
44 /// interprocedural optimization or you do not fit into any of the more
45 /// constrained passes described below.
46 ///
47 class Pass {
48   friend class AnalysisResolver;
49   AnalysisResolver *Resolver;  // AnalysisResolver this pass is owned by...
50   const PassInfo *PassInfoCache;
51
52   // AnalysisImpls - This keeps track of which passes implement the interfaces
53   // that are required by the current pass (to implement getAnalysis()).
54   //
55   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
56
57   void operator=(const Pass&);  // DO NOT IMPLEMENT
58   Pass(const Pass &);           // DO NOT IMPLEMENT
59 public:
60   Pass() : Resolver(0), PassInfoCache(0) {}
61   virtual ~Pass() {} // Destructor is virtual so we can be subclassed
62
63   /// getPassName - Return a nice clean name for a pass.  This usually
64   /// implemented in terms of the name that is registered by one of the
65   /// Registration templates, but can be overloaded directly, and if nothing
66   /// else is available, C++ RTTI will be consulted to get a SOMEWHAT
67   /// intelligable name for the pass.
68   ///
69   virtual const char *getPassName() const;
70
71   /// getPassInfo - Return the PassInfo data structure that corresponds to this
72   /// pass...  If the pass has not been registered, this will return null.
73   ///
74   const PassInfo *getPassInfo() const;
75
76   /// run - Run this pass, returning true if a modification was made to the
77   /// module argument.  This should be implemented by all concrete subclasses.
78   ///
79   virtual bool run(Module &M) = 0;
80
81   /// print - Print out the internal state of the pass.  This is called by
82   /// Analyze to print out the contents of an analysis.  Otherwise it is not
83   /// neccesary to implement this method.  Beware that the module pointer MAY be
84   /// null.  This automatically forwards to a virtual function that does not
85   /// provide the Module* in case the analysis doesn't need it it can just be
86   /// ignored.
87   ///
88   virtual void print(std::ostream &O, const Module *M) const { print(O); }
89   virtual void print(std::ostream &O) const;
90   void dump() const; // dump - call print(std::cerr, 0);
91
92
93   /// getAnalysisUsage - This function should be overriden by passes that need
94   /// analysis information to do their job.  If a pass specifies that it uses a
95   /// particular analysis result to this function, it can then use the
96   /// getAnalysis<AnalysisType>() function, below.
97   ///
98   virtual void getAnalysisUsage(AnalysisUsage &Info) const {
99     // By default, no analysis results are used, all are invalidated.
100   }
101
102   /// releaseMemory() - This member can be implemented by a pass if it wants to
103   /// be able to release its memory when it is no longer needed.  The default
104   /// behavior of passes is to hold onto memory for the entire duration of their
105   /// lifetime (which is the entire compile time).  For pipelined passes, this
106   /// is not a big deal because that memory gets recycled every time the pass is
107   /// invoked on another program unit.  For IP passes, it is more important to
108   /// free memory when it is unused.
109   ///
110   /// Optionally implement this function to release pass memory when it is no
111   /// longer used.
112   ///
113   virtual void releaseMemory() {}
114
115   // dumpPassStructure - Implement the -debug-passes=PassStructure option
116   virtual void dumpPassStructure(unsigned Offset = 0);
117
118
119   // getPassInfo - Static method to get the pass information from a class name.
120   template<typename AnalysisClass>
121   static const PassInfo *getClassPassInfo() {
122     return lookupPassInfo(typeid(AnalysisClass));
123   }
124
125   // lookupPassInfo - Return the pass info object for the specified pass class,
126   // or null if it is not known.
127   static const PassInfo *lookupPassInfo(const std::type_info &TI);
128
129   /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
130   /// to get to the analysis information that might be around that needs to be
131   /// updated.  This is different than getAnalysis in that it can fail (ie the
132   /// analysis results haven't been computed), so should only be used if you
133   /// provide the capability to update an analysis that exists.  This method is
134   /// often used by transformation APIs to update analysis results for a pass
135   /// automatically as the transform is performed.
136   ///
137   template<typename AnalysisType>
138   AnalysisType *getAnalysisToUpdate() const {
139     assert(Resolver && "Pass not resident in a PassManager object!");
140     const PassInfo *PI = getClassPassInfo<AnalysisType>();
141     if (PI == 0) return 0;
142     return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
143   }
144
145   /// mustPreserveAnalysisID - This method serves the same function as
146   /// getAnalysisToUpdate, but works if you just have an AnalysisID.  This
147   /// obviously cannot give you a properly typed instance of the class if you
148   /// don't have the class name available (use getAnalysisToUpdate if you do),
149   /// but it can tell you if you need to preserve the pass at least.
150   ///
151   bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const;
152
153   /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
154   /// to the analysis information that they claim to use by overriding the
155   /// getAnalysisUsage function.
156   ///
157   template<typename AnalysisType>
158   AnalysisType &getAnalysis() const {
159     assert(Resolver && "Pass has not been inserted into a PassManager object!");
160     const PassInfo *PI = getClassPassInfo<AnalysisType>();
161     return getAnalysisID<AnalysisType>(PI);
162   }
163
164   template<typename AnalysisType>
165   AnalysisType &getAnalysisID(const PassInfo *PI) const {
166     assert(Resolver && "Pass has not been inserted into a PassManager object!");
167     assert(PI && "getAnalysis for unregistered pass!");
168
169     // PI *must* appear in AnalysisImpls.  Because the number of passes used
170     // should be a small number, we just do a linear search over a (dense)
171     // vector.
172     Pass *ResultPass = 0;
173     for (unsigned i = 0; ; ++i) {
174       assert(i != AnalysisImpls.size() &&
175              "getAnalysis*() called on an analysis that we not "
176              "'required' by pass!");
177       if (AnalysisImpls[i].first == PI) {
178         ResultPass = AnalysisImpls[i].second;
179         break;
180       }
181     }
182
183     // Because the AnalysisType may not be a subclass of pass (for
184     // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
185     // return pointer (because the class may multiply inherit, once from pass,
186     // once from AnalysisType).
187     //
188     AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
189     assert(Result && "Pass does not implement interface required!");
190     return *Result;
191   }
192
193 private:
194   friend class PassManagerT<Module>;
195   friend class PassManagerT<Function>;
196   friend class PassManagerT<BasicBlock>;
197   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
198 };
199
200 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
201   P.print(OS, 0); return OS;
202 }
203
204
205
206 //===----------------------------------------------------------------------===//
207 /// ImmutablePass class - This class is used to provide information that does
208 /// not need to be run.  This is useful for things like target information and
209 /// "basic" versions of AnalysisGroups.
210 ///
211 struct ImmutablePass : public Pass {
212   /// initializePass - This method may be overriden by immutable passes to allow
213   /// them to perform various initialization actions they require.  This is
214   /// primarily because an ImmutablePass can "require" another ImmutablePass,
215   /// and if it does, the overloaded version of initializePass may get access to
216   /// these passes with getAnalysis<>.
217   ///
218   virtual void initializePass() {}
219
220   /// ImmutablePasses are never run.
221   ///
222   virtual bool run(Module &M) { return false; }
223
224 private:
225   friend class PassManagerT<Module>;
226   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
227 };
228
229
230 //===----------------------------------------------------------------------===//
231 /// FunctionPass class - This class is used to implement most global
232 /// optimizations.  Optimizations should subclass this class if they meet the
233 /// following constraints:
234 ///
235 ///  1. Optimizations are organized globally, ie a function at a time
236 ///  2. Optimizing a function does not cause the addition or removal of any
237 ///     functions in the module
238 ///
239 struct FunctionPass : public Pass {
240   /// doInitialization - Virtual method overridden by subclasses to do
241   /// any neccesary per-module initialization.
242   ///
243   virtual bool doInitialization(Module &M) { return false; }
244
245   /// runOnFunction - Virtual method overriden by subclasses to do the
246   /// per-function processing of the pass.
247   ///
248   virtual bool runOnFunction(Function &F) = 0;
249
250   /// doFinalization - Virtual method overriden by subclasses to do any post
251   /// processing needed after all passes have run.
252   ///
253   virtual bool doFinalization(Module &M) { return false; }
254
255   /// run - On a module, we run this pass by initializing, ronOnFunction'ing
256   /// once for every function in the module, then by finalizing.
257   ///
258   virtual bool run(Module &M);
259
260   /// run - On a function, we simply initialize, run the function, then
261   /// finalize.
262   ///
263   bool run(Function &F);
264
265 private:
266   friend class PassManagerT<Module>;
267   friend class PassManagerT<Function>;
268   friend class PassManagerT<BasicBlock>;
269   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
270   virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
271 };
272
273
274
275 //===----------------------------------------------------------------------===//
276 /// BasicBlockPass class - This class is used to implement most local
277 /// optimizations.  Optimizations should subclass this class if they
278 /// meet the following constraints:
279 ///   1. Optimizations are local, operating on either a basic block or
280 ///      instruction at a time.
281 ///   2. Optimizations do not modify the CFG of the contained function, or any
282 ///      other basic block in the function.
283 ///   3. Optimizations conform to all of the constraints of FunctionPass's.
284 ///
285 struct BasicBlockPass : public FunctionPass {
286   /// doInitialization - Virtual method overridden by subclasses to do
287   /// any neccesary per-module initialization.
288   ///
289   virtual bool doInitialization(Module &M) { return false; }
290
291   /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
292   /// to do any neccesary per-function initialization.
293   ///
294   virtual bool doInitialization(Function &F) { return false; }
295
296   /// runOnBasicBlock - Virtual method overriden by subclasses to do the
297   /// per-basicblock processing of the pass.
298   ///
299   virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
300
301   /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
302   /// do any post processing needed after all passes have run.
303   ///
304   virtual bool doFinalization(Function &F) { return false; }
305
306   /// doFinalization - Virtual method overriden by subclasses to do any post
307   /// processing needed after all passes have run.
308   ///
309   virtual bool doFinalization(Module &M) { return false; }
310
311
312   // To run this pass on a function, we simply call runOnBasicBlock once for
313   // each function.
314   //
315   bool runOnFunction(Function &F);
316
317   /// To run directly on the basic block, we initialize, runOnBasicBlock, then
318   /// finalize.
319   ///
320   bool run(BasicBlock &BB);
321
322 private:
323   friend class PassManagerT<Function>;
324   friend class PassManagerT<BasicBlock>;
325   virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
326   virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU);
327 };
328
329 // Include support files that contain important APIs commonly used by Passes,
330 // but that we want to seperate out to make it easier to read the header files.
331 //
332 #include "llvm/PassSupport.h"
333 #include "llvm/PassAnalysisSupport.h"
334
335 #endif