8b45c0e917f228a36165dd6e81e6d4b9debcc89a
[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 MethodPass, because they do not add
14 // or delete methods, they operate on the internals of the method.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_PASS_H
19 #define LLVM_PASS_H
20
21 #include <vector>
22 #include <map>
23 class Value;
24 class BasicBlock;
25 class Function;
26 class Module;
27 class AnalysisID;
28 class Pass;
29 template<class UnitType> class PassManagerT;
30 struct AnalysisResolver;
31
32 // PassManager - Top level PassManagerT instantiation intended to be used.
33 // Implemented in PassManager.h
34 typedef PassManagerT<Module> PassManager;
35
36
37 //===----------------------------------------------------------------------===//
38 // Pass interface - Implemented by all 'passes'.  Subclass this if you are an
39 // interprocedural optimization or you do not fit into any of the more
40 // constrained passes described below.
41 //
42 class Pass {
43   friend class AnalysisResolver;
44   AnalysisResolver *Resolver;  // AnalysisResolver this pass is owned by...
45 public:
46   typedef std::vector<AnalysisID> AnalysisSet;
47
48   inline Pass(AnalysisResolver *AR = 0) : Resolver(AR) {}
49   inline virtual ~Pass() {} // Destructor is virtual so we can be subclassed
50
51
52   // run - Run this pass, returning true if a modification was made to the
53   // module argument.  This should be implemented by all concrete subclasses.
54   //
55   virtual bool run(Module *M) = 0;
56
57   // getAnalysisUsageInfo - This function should be overriden by passes that
58   // need analysis information to do their job.  If a pass specifies that it
59   // uses a particular analysis result to this function, it can then use the
60   // getAnalysis<AnalysisType>() function, below.
61   //
62   // The Destroyed vector is used to communicate what analyses are invalidated
63   // by this pass.  This is critical to specify so that the PassManager knows
64   // which analysis must be rerun after this pass has proceeded.  Analysis are
65   // only invalidated if run() returns true.
66   //
67   // The Provided vector is used for passes that provide analysis information,
68   // these are the analysis passes themselves.  All analysis passes should
69   // override this method to return themselves in the provided set.
70   //
71   virtual void getAnalysisUsageInfo(AnalysisSet &Required,
72                                     AnalysisSet &Destroyed,
73                                     AnalysisSet &Provided) {
74     // By default, no analysis results are used or destroyed.
75   }
76
77   // releaseMemory() - This member can be implemented by a pass if it wants to
78   // be able to release its memory when it is no longer needed.  The default
79   // behavior of passes is to hold onto memory for the entire duration of their
80   // lifetime (which is the entire compile time).  For pipelined passes, this
81   // is not a big deal because that memory gets recycled every time the pass is
82   // invoked on another program unit.  For IP passes, it is more important to
83   // free memory when it is unused.
84   //
85   // Optionally implement this function to release pass memory when it is no
86   // longer used.
87   //
88   virtual void releaseMemory() {}
89
90 #ifndef NDEBUG
91   // dumpPassStructure - Implement the -debug-passes=PassStructure option
92   virtual void dumpPassStructure(unsigned Offset = 0);
93 #endif
94
95 protected:
96   // getAnalysis<AnalysisType>() - This function is used by subclasses to get to
97   // the analysis information that they claim to use by overriding the
98   // getAnalysisUsageInfo function.
99   //
100   template<typename AnalysisType>
101   AnalysisType &getAnalysis(AnalysisID AID = AnalysisType::ID) {
102     assert(Resolver && "Pass not resident in a PassManager object!");
103     return *(AnalysisType*)Resolver->getAnalysis(AID);
104   }
105
106 private:
107   friend class PassManagerT<Module>;
108   friend class PassManagerT<Function>;
109   friend class PassManagerT<BasicBlock>;
110   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Req,
111                                 AnalysisSet &Destroyed, AnalysisSet &Provided);
112 };
113
114
115 //===----------------------------------------------------------------------===//
116 // MethodPass class - This class is used to implement most global optimizations.
117 // Optimizations should subclass this class if they meet the following
118 // constraints:
119 //  1. Optimizations are organized globally, ie a method at a time
120 //  2. Optimizing a method does not cause the addition or removal of any methods
121 //     in the module
122 //
123 struct MethodPass : public Pass {
124   // doInitialization - Virtual method overridden by subclasses to do
125   // any neccesary per-module initialization.
126   //
127   virtual bool doInitialization(Module *M) { return false; }
128
129   // runOnMethod - Virtual method overriden by subclasses to do the per-method
130   // processing of the pass.
131   //
132   virtual bool runOnMethod(Function *M) = 0;
133
134   // doFinalization - Virtual method overriden by subclasses to do any post
135   // processing needed after all passes have run.
136   //
137   virtual bool doFinalization(Module *M) { return false; }
138
139   // run - On a module, we run this pass by initializing, ronOnMethod'ing once
140   // for every method in the module, then by finalizing.
141   //
142   virtual bool run(Module *M);
143
144   // run - On a method, we simply initialize, run the method, then finalize.
145   //
146   bool run(Function *M);
147
148 private:
149   friend class PassManagerT<Module>;
150   friend class PassManagerT<Function>;
151   friend class PassManagerT<BasicBlock>;
152   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Req,
153                                 AnalysisSet &Dest, AnalysisSet &Prov);
154   virtual void addToPassManager(PassManagerT<Function> *PM,AnalysisSet &Req,
155                                 AnalysisSet &Dest, AnalysisSet &Prov);
156 };
157
158
159
160 //===----------------------------------------------------------------------===//
161 // BasicBlockPass class - This class is used to implement most local
162 // optimizations.  Optimizations should subclass this class if they
163 // meet the following constraints:
164 //   1. Optimizations are local, operating on either a basic block or
165 //      instruction at a time.
166 //   2. Optimizations do not modify the CFG of the contained method, or any
167 //      other basic block in the method.
168 //   3. Optimizations conform to all of the contstraints of MethodPass's.
169 //
170 struct BasicBlockPass : public MethodPass {
171   // runOnBasicBlock - Virtual method overriden by subclasses to do the
172   // per-basicblock processing of the pass.
173   //
174   virtual bool runOnBasicBlock(BasicBlock *M) = 0;
175
176   // To run this pass on a method, we simply call runOnBasicBlock once for each
177   // method.
178   //
179   virtual bool runOnMethod(Function *F);
180
181   // To run directly on the basic block, we initialize, runOnBasicBlock, then
182   // finalize.
183   //
184   bool run(BasicBlock *BB);
185
186 private:
187   friend class PassManagerT<Function>;
188   friend class PassManagerT<BasicBlock>;
189   virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisSet &,
190                                 AnalysisSet &, AnalysisSet &);
191   virtual void addToPassManager(PassManagerT<BasicBlock> *PM, AnalysisSet &,
192                                 AnalysisSet &, AnalysisSet &);
193 };
194
195
196 // CreatePass - Helper template to invoke the constructor for the AnalysisID
197 // class. Note that this should be a template internal to AnalysisID, but
198 // GCC 2.95.3 crashes if we do that, doh.
199 //
200 template<class AnalysisType>
201 static Pass *CreatePass(AnalysisID ID) { return new AnalysisType(ID); }
202
203 //===----------------------------------------------------------------------===//
204 // AnalysisID - This class is used to uniquely identify an analysis pass that
205 //              is referenced by a transformation.
206 //
207 class AnalysisID {
208   static unsigned NextID;               // Next ID # to deal out...
209   unsigned ID;                          // Unique ID for this analysis
210   Pass *(*Constructor)(AnalysisID);     // Constructor to return the Analysis
211
212   AnalysisID();                         // Disable default ctor
213   AnalysisID(unsigned id, Pass *(*Ct)(AnalysisID)) : ID(id), Constructor(Ct) {}
214 public:
215   // create - the only way to define a new AnalysisID.  This static method is
216   // supposed to be used to define the class static AnalysisID's that are
217   // provided by analysis passes.  In the implementation (.cpp) file for the
218   // class, there should be a line that looks like this (using CallGraph as an
219   // example):
220   //
221   //  AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
222   //
223   template<class AnalysisType>
224   static AnalysisID create() {
225     return AnalysisID(NextID++, CreatePass<AnalysisType>);
226   }
227
228   inline Pass *createPass() const { return Constructor(*this); }
229
230   inline bool operator==(const AnalysisID &A) const {
231     return A.ID == ID;
232   }
233   inline bool operator!=(const AnalysisID &A) const {
234     return A.ID != ID;
235   }
236   inline bool operator<(const AnalysisID &A) const {
237     return ID < A.ID;
238   }
239 };
240
241
242 //===----------------------------------------------------------------------===//
243 // AnalysisResolver - Simple interface implemented by PassManagers objects that
244 // is used to pull analysis information out of them.
245 //
246 struct AnalysisResolver {
247   virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
248   virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
249   Pass *getAnalysis(AnalysisID ID) {
250     Pass *Result = getAnalysisOrNullUp(ID);
251     assert(Result && "Pass has an incorrect analysis uses set!");
252     return Result;
253   }
254   virtual unsigned getDepth() const = 0;
255
256   virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
257 protected:
258   void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
259 };
260
261
262
263 #endif