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