1 //===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=//
3 // This file defines a base class that indicates that a specified class is a
4 // transformation pass implementation.
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.
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.
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.
20 //===----------------------------------------------------------------------===//
35 template<class UnitType> class PassManagerT;
36 struct AnalysisResolver;
38 // AnalysisID - Use the PassInfo to identify a pass...
39 typedef const PassInfo* AnalysisID;
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.
47 friend class AnalysisResolver;
48 AnalysisResolver *Resolver; // AnalysisResolver this pass is owned by...
49 const PassInfo *PassInfoCache;
51 // AnalysisImpls - This keeps track of which passes implement the interfaces
52 // that are required by the current pass (to implement getAnalysis()).
54 std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
56 void operator=(const Pass&); // DO NOT IMPLEMENT
57 Pass(const Pass &); // DO NOT IMPLEMENT
59 Pass() : Resolver(0), PassInfoCache(0) {}
60 virtual ~Pass() {} // Destructor is virtual so we can be subclassed
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.
68 virtual const char *getPassName() const;
70 /// getPassInfo - Return the PassInfo data structure that corresponds to this
71 /// pass... If the pass has not been registered, this will return null.
73 const PassInfo *getPassInfo() const;
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.
78 virtual bool run(Module &M) = 0;
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
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);
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.
97 virtual void getAnalysisUsage(AnalysisUsage &Info) const {
98 // By default, no analysis results are used, all are invalidated.
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.
109 /// Optionally implement this function to release pass memory when it is no
112 virtual void releaseMemory() {}
114 // dumpPassStructure - Implement the -debug-passes=PassStructure option
115 virtual void dumpPassStructure(unsigned Offset = 0);
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));
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);
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.
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));
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.
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);
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!");
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)
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;
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).
181 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
182 assert(Result && "Pass does not implement interface required!");
187 friend class PassManagerT<Module>;
188 friend class PassManagerT<Function>;
189 friend class PassManagerT<BasicBlock>;
190 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
193 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
194 P.print(OS, 0); return OS;
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:
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
206 struct FunctionPass : public Pass {
207 /// doInitialization - Virtual method overridden by subclasses to do
208 /// any neccesary per-module initialization.
210 virtual bool doInitialization(Module &M) { return false; }
212 /// runOnFunction - Virtual method overriden by subclasses to do the
213 /// per-function processing of the pass.
215 virtual bool runOnFunction(Function &F) = 0;
217 /// doFinalization - Virtual method overriden by subclasses to do any post
218 /// processing needed after all passes have run.
220 virtual bool doFinalization(Module &M) { return false; }
222 /// run - On a module, we run this pass by initializing, ronOnFunction'ing
223 /// once for every function in the module, then by finalizing.
225 virtual bool run(Module &M);
227 /// run - On a function, we simply initialize, run the function, then
230 bool run(Function &F);
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);
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.
252 struct BasicBlockPass : public FunctionPass {
253 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
254 /// per-basicblock processing of the pass.
256 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
258 /// To run this pass on a function, we simply call runOnBasicBlock once for
261 virtual bool runOnFunction(Function &F);
263 /// To run directly on the basic block, we initialize, runOnBasicBlock, then
266 bool run(BasicBlock &BB);
269 friend class PassManagerT<Function>;
270 friend class PassManagerT<BasicBlock>;
271 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
272 virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU);
275 // Include support files that contain important APIs commonly used by Passes,
276 // but that we want to seperate out to make it easier to read the header files.
278 #include "llvm/PassSupport.h"
279 #include "llvm/PassAnalysisSupport.h"