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