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