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 //===----------------------------------------------------------------------===//
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.
43 friend class AnalysisResolver;
44 AnalysisResolver *Resolver; // AnalysisResolver this pass is owned by...
46 Pass(AnalysisResolver *AR = 0) : Resolver(AR) {}
47 virtual ~Pass() {} // Destructor is virtual so we can be subclassed
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.
53 virtual const char *getPassName() const;
55 // getPassInfo - Return the PassInfo data structure that corresponds to this
57 const PassInfo *getPassInfo() const;
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.
62 virtual bool run(Module &M) = 0;
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.
69 virtual void getAnalysisUsage(AnalysisUsage &Info) const {
70 // By default, no analysis results are used, all are invalidated.
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.
81 // Optionally implement this function to release pass memory when it is no
84 virtual void releaseMemory() {}
86 // dumpPassStructure - Implement the -debug-passes=PassStructure option
87 virtual void dumpPassStructure(unsigned Offset = 0);
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.
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);
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.
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);
114 friend class PassManagerT<Module>;
115 friend class PassManagerT<Function>;
116 friend class PassManagerT<BasicBlock>;
117 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU);
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:
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
130 struct FunctionPass : public Pass {
131 // doInitialization - Virtual method overridden by subclasses to do
132 // any neccesary per-module initialization.
134 virtual bool doInitialization(Module &M) { return false; }
136 // runOnFunction - Virtual method overriden by subclasses to do the
137 // per-function processing of the pass.
139 virtual bool runOnFunction(Function &F) = 0;
141 // doFinalization - Virtual method overriden by subclasses to do any post
142 // processing needed after all passes have run.
144 virtual bool doFinalization(Module &M) { return false; }
146 // run - On a module, we run this pass by initializing, ronOnFunction'ing once
147 // for every function in the module, then by finalizing.
149 virtual bool run(Module &M);
151 // run - On a function, we simply initialize, run the function, then finalize.
153 bool run(Function &F);
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);
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.
175 struct BasicBlockPass : public FunctionPass {
176 // runOnBasicBlock - Virtual method overriden by subclasses to do the
177 // per-basicblock processing of the pass.
179 virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
181 // To run this pass on a function, we simply call runOnBasicBlock once for
184 virtual bool runOnFunction(Function &F);
186 // To run directly on the basic block, we initialize, runOnBasicBlock, then
189 bool run(BasicBlock &BB);
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);
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.
201 #include "llvm/PassSupport.h"
202 #include "llvm/PassAnalysisSupport.h"