0ff286a50d2b0609d91f1df58302f8405c2da84d
[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 class Value;
29 class BasicBlock;
30 class Function;
31 class Module;
32 class AnalysisUsage;
33 class PassInfo;
34 template<class UnitType> class PassManagerT;
35 struct AnalysisResolver;
36
37 // AnalysisID - Use the PassInfo to identify a pass...
38 typedef const PassInfo* AnalysisID;
39
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   void operator=(const Pass&);  // DO NOT IMPLEMENT
51   Pass(const Pass &);           // DO NOT IMPLEMENT
52 public:
53   Pass() : Resolver(0), PassInfoCache(0) {}
54   virtual ~Pass() {} // Destructor is virtual so we can be subclassed
55
56   // getPassName - Return a nice clean name for a pass.  This usually
57   // implemented in terms of the name that is registered by one of the
58   // Registration templates, but can be overloaded directly, and if nothing else
59   // is available, C++ RTTI will be consulted to get a SOMEWHAT intelligable
60   // name for the pass.
61   //
62   virtual const char *getPassName() const;
63
64   // getPassInfo - Return the PassInfo data structure that corresponds to this
65   // pass...  If the pass has not been registered, this will return null.
66   //
67   const PassInfo *getPassInfo() const;
68
69   // run - Run this pass, returning true if a modification was made to the
70   // module argument.  This should be implemented by all concrete subclasses.
71   //
72   virtual bool run(Module &M) = 0;
73
74   // print - Print out the internal state of the pass.  This is called by
75   // Analyze to print out the contents of an analysis.  Otherwise it is not
76   // neccesary to implement this method.  Beware that the module pointer MAY be
77   // null.  This automatically forwards to a virtual function that does not
78   // provide the Module* in case the analysis doesn't need it it can just be
79   // ignored.
80   //
81   virtual void print(std::ostream &O, const Module *M) const { print(O); }
82   virtual void print(std::ostream &O) const;
83   void dump() const; // dump - call print(std::cerr, 0);
84
85
86   // getAnalysisUsage - This function should be overriden by passes that need
87   // analysis information to do their job.  If a pass specifies that it uses a
88   // particular analysis result to this function, it can then use the
89   // getAnalysis<AnalysisType>() function, below.
90   //
91   virtual void getAnalysisUsage(AnalysisUsage &Info) const {
92     // By default, no analysis results are used, all are invalidated.
93   }
94
95   // releaseMemory() - This member can be implemented by a pass if it wants to
96   // be able to release its memory when it is no longer needed.  The default
97   // behavior of passes is to hold onto memory for the entire duration of their
98   // lifetime (which is the entire compile time).  For pipelined passes, this
99   // is not a big deal because that memory gets recycled every time the pass is
100   // invoked on another program unit.  For IP passes, it is more important to
101   // free memory when it is unused.
102   //
103   // Optionally implement this function to release pass memory when it is no
104   // longer used.
105   //
106   virtual void releaseMemory() {}
107
108   // dumpPassStructure - Implement the -debug-passes=PassStructure option
109   virtual void dumpPassStructure(unsigned Offset = 0);
110
111 protected:
112   // getAnalysis<AnalysisType>() - This function is used by subclasses to get to
113   // the analysis information that they claim to use by overriding the
114   // getAnalysisUsage function.
115   //
116   template<typename AnalysisType>
117   AnalysisType &getAnalysis(AnalysisID AID = AnalysisType::ID) {
118     assert(Resolver && "Pass not resident in a PassManager object!");
119     return *(AnalysisType*)Resolver->getAnalysis(AID);
120   }
121
122   // getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
123   // to get to the analysis information that might be around that needs to be
124   // updated.  This is different than getAnalysis in that it can fail (ie the
125   // analysis results haven't been computed), so should only be used if you
126   // provide the capability to update an analysis that exists.
127   //
128   template<typename AnalysisType>
129   AnalysisType *getAnalysisToUpdate(AnalysisID AID = AnalysisType::ID) {
130     assert(Resolver && "Pass not resident in a PassManager object!");
131     return (AnalysisType*)Resolver->getAnalysisToUpdate(AID);
132   }
133
134
135 private:
136   friend class PassManagerT<Module>;
137   friend class PassManagerT<Function>;
138   friend class PassManagerT<BasicBlock>;
139   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
140 };
141
142 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
143   P.print(OS, 0); return OS;
144 }
145
146 //===----------------------------------------------------------------------===//
147 // FunctionPass class - This class is used to implement most global
148 // optimizations.  Optimizations should subclass this class if they meet the
149 // following constraints:
150 //
151 //  1. Optimizations are organized globally, ie a function at a time
152 //  2. Optimizing a function does not cause the addition or removal of any
153 //     functions in the module
154 //
155 struct FunctionPass : public Pass {
156   // doInitialization - Virtual method overridden by subclasses to do
157   // any neccesary per-module initialization.
158   //
159   virtual bool doInitialization(Module &M) { return false; }
160
161   // runOnFunction - Virtual method overriden by subclasses to do the
162   // per-function processing of the pass.
163   //
164   virtual bool runOnFunction(Function &F) = 0;
165
166   // doFinalization - Virtual method overriden by subclasses to do any post
167   // processing needed after all passes have run.
168   //
169   virtual bool doFinalization(Module &M) { return false; }
170
171   // run - On a module, we run this pass by initializing, ronOnFunction'ing once
172   // for every function in the module, then by finalizing.
173   //
174   virtual bool run(Module &M);
175
176   // run - On a function, we simply initialize, run the function, then finalize.
177   //
178   bool run(Function &F);
179
180 private:
181   friend class PassManagerT<Module>;
182   friend class PassManagerT<Function>;
183   friend class PassManagerT<BasicBlock>;
184   virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
185   virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
186 };
187
188
189
190 //===----------------------------------------------------------------------===//
191 // BasicBlockPass class - This class is used to implement most local
192 // optimizations.  Optimizations should subclass this class if they
193 // meet the following constraints:
194 //   1. Optimizations are local, operating on either a basic block or
195 //      instruction at a time.
196 //   2. Optimizations do not modify the CFG of the contained function, or any
197 //      other basic block in the function.
198 //   3. Optimizations conform to all of the contstraints of FunctionPass's.
199 //
200 struct BasicBlockPass : public FunctionPass {
201   // runOnBasicBlock - Virtual method overriden by subclasses to do the
202   // per-basicblock processing of the pass.
203   //
204   virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
205
206   // To run this pass on a function, we simply call runOnBasicBlock once for
207   // each function.
208   //
209   virtual bool runOnFunction(Function &F);
210
211   // To run directly on the basic block, we initialize, runOnBasicBlock, then
212   // finalize.
213   //
214   bool run(BasicBlock &BB);
215
216 private:
217   friend class PassManagerT<Function>;
218   friend class PassManagerT<BasicBlock>;
219   virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
220   virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU);
221 };
222
223 // Include support files that contain important APIs commonly used by Passes,
224 // but that we want to seperate out to make it easier to read the header files.
225 //
226 #include "llvm/PassSupport.h"
227 #include "llvm/PassAnalysisSupport.h"
228
229 #endif