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