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