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 //===----------------------------------------------------------------------===//
38 template<class UnitType> class PassManagerT;
39 struct AnalysisResolver;
41 // AnalysisID - Use the PassInfo to identify a pass...
42 typedef const PassInfo* AnalysisID;
44 //===----------------------------------------------------------------------===//
45 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
46 /// interprocedural optimization or you do not fit into any of the more
47 /// constrained passes described below.
50 friend class AnalysisResolver;
51 AnalysisResolver *Resolver; // AnalysisResolver this pass is owned by...
52 const PassInfo *PassInfoCache;
54 // AnalysisImpls - This keeps track of which passes implement the interfaces
55 // that are required by the current pass (to implement getAnalysis()).
57 std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
59 void operator=(const Pass&); // DO NOT IMPLEMENT
60 Pass(const Pass &); // DO NOT IMPLEMENT
62 Pass() : Resolver(0), PassInfoCache(0) {}
63 virtual ~Pass() {} // Destructor is virtual so we can be subclassed
65 /// getPassName - Return a nice clean name for a pass. This usually
66 /// implemented in terms of the name that is registered by one of the
67 /// Registration templates, but can be overloaded directly, and if nothing
68 /// else is available, C++ RTTI will be consulted to get a SOMEWHAT
69 /// intelligable name for the pass.
71 virtual const char *getPassName() const;
73 /// getPassInfo - Return the PassInfo data structure that corresponds to this
74 /// pass... If the pass has not been registered, this will return null.
76 const PassInfo *getPassInfo() const;
78 /// run - Run this pass, returning true if a modification was made to the
79 /// module argument. This should be implemented by all concrete subclasses.
81 virtual bool run(Module &M) = 0;
83 /// print - Print out the internal state of the pass. This is called by
84 /// Analyze to print out the contents of an analysis. Otherwise it is not
85 /// neccesary to implement this method. Beware that the module pointer MAY be
86 /// null. This automatically forwards to a virtual function that does not
87 /// provide the Module* in case the analysis doesn't need it it can just be
90 virtual void print(std::ostream &O, const Module *M) const { print(O); }
91 virtual void print(std::ostream &O) const;
92 void dump() const; // dump - call print(std::cerr, 0);
95 /// getAnalysisUsage - This function should be overriden by passes that need
96 /// analysis information to do their job. If a pass specifies that it uses a
97 /// particular analysis result to this function, it can then use the
98 /// getAnalysis<AnalysisType>() function, below.
100 virtual void getAnalysisUsage(AnalysisUsage &Info) const {
101 // By default, no analysis results are used, all are invalidated.
104 /// releaseMemory() - This member can be implemented by a pass if it wants to
105 /// be able to release its memory when it is no longer needed. The default
106 /// behavior of passes is to hold onto memory for the entire duration of their
107 /// lifetime (which is the entire compile time). For pipelined passes, this
108 /// is not a big deal because that memory gets recycled every time the pass is
109 /// invoked on another program unit. For IP passes, it is more important to
110 /// free memory when it is unused.
112 /// Optionally implement this function to release pass memory when it is no
115 virtual void releaseMemory() {}
117 // dumpPassStructure - Implement the -debug-passes=PassStructure option
118 virtual void dumpPassStructure(unsigned Offset = 0);
121 // getPassInfo - Static method to get the pass information from a class name.
122 template<typename AnalysisClass>
123 static const PassInfo *getClassPassInfo() {
124 return lookupPassInfo(typeid(AnalysisClass));
127 // lookupPassInfo - Return the pass info object for the specified pass class,
128 // or null if it is not known.
129 static const PassInfo *lookupPassInfo(const std::type_info &TI);
131 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
132 /// to get to the analysis information that might be around that needs to be
133 /// updated. This is different than getAnalysis in that it can fail (ie the
134 /// analysis results haven't been computed), so should only be used if you
135 /// provide the capability to update an analysis that exists. This method is
136 /// often used by transformation APIs to update analysis results for a pass
137 /// automatically as the transform is performed.
139 template<typename AnalysisType>
140 AnalysisType *getAnalysisToUpdate() const {
141 assert(Resolver && "Pass not resident in a PassManager object!");
142 const PassInfo *PI = getClassPassInfo<AnalysisType>();
143 if (PI == 0) return 0;
144 return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
147 /// mustPreserveAnalysisID - This method serves the same function as
148 /// getAnalysisToUpdate, but works if you just have an AnalysisID. This
149 /// obviously cannot give you a properly typed instance of the class if you
150 /// don't have the class name available (use getAnalysisToUpdate if you do),
151 /// but it can tell you if you need to preserve the pass at least.
153 bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const;
155 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
156 /// to the analysis information that they claim to use by overriding the
157 /// getAnalysisUsage function.
159 template<typename AnalysisType>
160 AnalysisType &getAnalysis() const {
161 assert(Resolver && "Pass has not been inserted into a PassManager object!");
162 const PassInfo *PI = getClassPassInfo<AnalysisType>();
163 return getAnalysisID<AnalysisType>(PI);
166 template<typename AnalysisType>
167 AnalysisType &getAnalysisID(const PassInfo *PI) const {
168 assert(Resolver && "Pass has not been inserted into a PassManager object!");
169 assert(PI && "getAnalysis for unregistered pass!");
171 // PI *must* appear in AnalysisImpls. Because the number of passes used
172 // should be a small number, we just do a linear search over a (dense)
174 Pass *ResultPass = 0;
175 for (unsigned i = 0; ; ++i) {
176 assert(i != AnalysisImpls.size() &&
177 "getAnalysis*() called on an analysis that we not "
178 "'required' by pass!");
179 if (AnalysisImpls[i].first == PI) {
180 ResultPass = AnalysisImpls[i].second;
185 // Because the AnalysisType may not be a subclass of pass (for
186 // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
187 // return pointer (because the class may multiply inherit, once from pass,
188 // once from AnalysisType).
190 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
191 assert(Result && "Pass does not implement interface required!");
196 friend class PassManagerT<Module>;
197 friend class PassManagerT<Function>;
198 friend class PassManagerT<BasicBlock>;
199 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
202 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
203 P.print(OS, 0); return OS;
208 //===----------------------------------------------------------------------===//
209 /// ImmutablePass class - This class is used to provide information that does
210 /// not need to be run. This is useful for things like target information and
211 /// "basic" versions of AnalysisGroups.
213 struct ImmutablePass : public Pass {
214 /// initializePass - This method may be overriden by immutable passes to allow
215 /// them to perform various initialization actions they require. This is
216 /// primarily because an ImmutablePass can "require" another ImmutablePass,
217 /// and if it does, the overloaded version of initializePass may get access to
218 /// these passes with getAnalysis<>.
220 virtual void initializePass() {}
222 /// ImmutablePasses are never run.
224 virtual bool run(Module &M) { return false; }
227 friend class PassManagerT<Module>;
228 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
232 //===----------------------------------------------------------------------===//
233 /// FunctionPass class - This class is used to implement most global
234 /// optimizations. Optimizations should subclass this class if they meet the
235 /// following constraints:
237 /// 1. Optimizations are organized globally, i.e., a function at a time
238 /// 2. Optimizing a function does not cause the addition or removal of any
239 /// functions in the module
241 struct FunctionPass : public Pass {
242 /// doInitialization - Virtual method overridden by subclasses to do
243 /// any neccesary per-module initialization.
245 virtual bool doInitialization(Module &M) { return false; }
247 /// runOnFunction - Virtual method overriden by subclasses to do the
248 /// per-function processing of the pass.
250 virtual bool runOnFunction(Function &F) = 0;
252 /// doFinalization - Virtual method overriden by subclasses to do any post
253 /// processing needed after all passes have run.
255 virtual bool doFinalization(Module &M) { return false; }
257 /// run - On a module, we run this pass by initializing, ronOnFunction'ing
258 /// once for every function in the module, then by finalizing.
260 virtual bool run(Module &M);
262 /// run - On a function, we simply initialize, run the function, then
265 bool run(Function &F);
268 friend class PassManagerT<Module>;
269 friend class PassManagerT<Function>;
270 friend class PassManagerT<BasicBlock>;
271 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
272 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
277 //===----------------------------------------------------------------------===//
278 /// BasicBlockPass class - This class is used to implement most local
279 /// optimizations. Optimizations should subclass this class if they
280 /// meet the following constraints:
281 /// 1. Optimizations are local, operating on either a basic block or
282 /// instruction at a time.
283 /// 2. Optimizations do not modify the CFG of the contained function, or any
284 /// other basic block in the function.
285 /// 3. Optimizations conform to all of the constraints of FunctionPass's.
287 struct BasicBlockPass : public FunctionPass {
288 /// doInitialization - Virtual method overridden by subclasses to do
289 /// any neccesary per-module initialization.
291 virtual bool doInitialization(Module &M) { return false; }
293 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
294 /// to do any neccesary per-function initialization.
296 virtual bool doInitialization(Function &F) { return false; }
298 /// runOnBasicBlock - Virtual method overriden by subclasses to do the
299 /// per-basicblock processing of the pass.
301 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
303 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
304 /// do any post processing needed after all passes have run.
306 virtual bool doFinalization(Function &F) { return false; }
308 /// doFinalization - Virtual method overriden by subclasses to do any post
309 /// processing needed after all passes have run.
311 virtual bool doFinalization(Module &M) { return false; }
314 // To run this pass on a function, we simply call runOnBasicBlock once for
317 bool runOnFunction(Function &F);
319 /// To run directly on the basic block, we initialize, runOnBasicBlock, then
322 bool run(BasicBlock &BB);
325 friend class PassManagerT<Function>;
326 friend class PassManagerT<BasicBlock>;
327 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
328 virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU);
331 // Include support files that contain important APIs commonly used by Passes,
332 // but that we want to separate out to make it easier to read the header files.
334 #include "llvm/PassSupport.h"
335 #include "llvm/PassAnalysisSupport.h"