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 //===----------------------------------------------------------------------===//
34 template<class UnitType> class PassManagerT;
35 struct AnalysisResolver;
37 // AnalysisID - Use the PassInfo to identify a pass...
38 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;
50 void operator=(const Pass&); // DO NOT IMPLEMENT
51 Pass(const Pass &); // DO NOT IMPLEMENT
53 Pass() : Resolver(0), PassInfoCache(0) {}
54 virtual ~Pass() {} // Destructor is virtual so we can be subclassed
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
62 virtual const char *getPassName() const;
64 // getPassInfo - Return the PassInfo data structure that corresponds to this
65 // pass... If the pass has not been registered, this will return null.
67 const PassInfo *getPassInfo() const;
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.
72 virtual bool run(Module &M) = 0;
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
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);
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.
91 virtual void getAnalysisUsage(AnalysisUsage &Info) const {
92 // By default, no analysis results are used, all are invalidated.
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.
103 // Optionally implement this function to release pass memory when it is no
106 virtual void releaseMemory() {}
108 // dumpPassStructure - Implement the -debug-passes=PassStructure option
109 virtual void dumpPassStructure(unsigned Offset = 0);
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.
116 template<typename AnalysisType>
117 AnalysisType &getAnalysis() {
118 assert(Resolver && "Pass not resident in a PassManager object!");
119 return *(AnalysisType*)Resolver->getAnalysis(AnalysisType::ID);
122 template<typename AnalysisType>
123 AnalysisType &getAnalysisID(const PassInfo *PI) {
124 assert(Resolver && "Pass not resident in a PassManager object!");
125 return *(AnalysisType*)Resolver->getAnalysis(PI);
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.
134 template<typename AnalysisType>
135 AnalysisType *getAnalysisToUpdate() {
136 assert(Resolver && "Pass not resident in a PassManager object!");
137 return (AnalysisType*)Resolver->getAnalysisToUpdate(AnalysisType::ID);
142 friend class PassManagerT<Module>;
143 friend class PassManagerT<Function>;
144 friend class PassManagerT<BasicBlock>;
145 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
148 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
149 P.print(OS, 0); return OS;
152 //===----------------------------------------------------------------------===//
153 // FunctionPass class - This class is used to implement most global
154 // optimizations. Optimizations should subclass this class if they meet the
155 // following constraints:
157 // 1. Optimizations are organized globally, ie a function at a time
158 // 2. Optimizing a function does not cause the addition or removal of any
159 // functions in the module
161 struct FunctionPass : public Pass {
162 // doInitialization - Virtual method overridden by subclasses to do
163 // any neccesary per-module initialization.
165 virtual bool doInitialization(Module &M) { return false; }
167 // runOnFunction - Virtual method overriden by subclasses to do the
168 // per-function processing of the pass.
170 virtual bool runOnFunction(Function &F) = 0;
172 // doFinalization - Virtual method overriden by subclasses to do any post
173 // processing needed after all passes have run.
175 virtual bool doFinalization(Module &M) { return false; }
177 // run - On a module, we run this pass by initializing, ronOnFunction'ing once
178 // for every function in the module, then by finalizing.
180 virtual bool run(Module &M);
182 // run - On a function, we simply initialize, run the function, then finalize.
184 bool run(Function &F);
187 friend class PassManagerT<Module>;
188 friend class PassManagerT<Function>;
189 friend class PassManagerT<BasicBlock>;
190 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
191 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
196 //===----------------------------------------------------------------------===//
197 // BasicBlockPass class - This class is used to implement most local
198 // optimizations. Optimizations should subclass this class if they
199 // meet the following constraints:
200 // 1. Optimizations are local, operating on either a basic block or
201 // instruction at a time.
202 // 2. Optimizations do not modify the CFG of the contained function, or any
203 // other basic block in the function.
204 // 3. Optimizations conform to all of the contstraints of FunctionPass's.
206 struct BasicBlockPass : public FunctionPass {
207 // runOnBasicBlock - Virtual method overriden by subclasses to do the
208 // per-basicblock processing of the pass.
210 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
212 // To run this pass on a function, we simply call runOnBasicBlock once for
215 virtual bool runOnFunction(Function &F);
217 // To run directly on the basic block, we initialize, runOnBasicBlock, then
220 bool run(BasicBlock &BB);
223 friend class PassManagerT<Function>;
224 friend class PassManagerT<BasicBlock>;
225 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU);
226 virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU);
229 // Include support files that contain important APIs commonly used by Passes,
230 // but that we want to seperate out to make it easier to read the header files.
232 #include "llvm/PassSupport.h"
233 #include "llvm/PassAnalysisSupport.h"