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 MethodPass, because they do not add
14 // or delete methods, they operate on the internals of the method.
16 //===----------------------------------------------------------------------===//
29 template<class UnitType> class PassManagerT;
30 struct AnalysisResolver;
32 // PassManager - Top level PassManagerT instantiation intended to be used.
33 // Implemented in PassManager.h
34 typedef PassManagerT<Module> PassManager;
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 typedef std::vector<AnalysisID> AnalysisSet;
48 inline Pass(AnalysisResolver *AR = 0) : Resolver(AR) {}
49 inline virtual ~Pass() {} // Destructor is virtual so we can be subclassed
52 // run - Run this pass, returning true if a modification was made to the
53 // module argument. This should be implemented by all concrete subclasses.
55 virtual bool run(Module *M) = 0;
57 // getAnalysisUsageInfo - This function should be overriden by passes that
58 // need analysis information to do their job. If a pass specifies that it
59 // uses a particular analysis result to this function, it can then use the
60 // getAnalysis<AnalysisType>() function, below.
62 // The Destroyed vector is used to communicate what analyses are invalidated
63 // by this pass. This is critical to specify so that the PassManager knows
64 // which analysis must be rerun after this pass has proceeded. Analysis are
65 // only invalidated if run() returns true.
67 // The Provided vector is used for passes that provide analysis information,
68 // these are the analysis passes themselves. All analysis passes should
69 // override this method to return themselves in the provided set.
71 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
72 AnalysisSet &Destroyed,
73 AnalysisSet &Provided) {
74 // By default, no analysis results are used or destroyed.
77 // releaseMemory() - This member can be implemented by a pass if it wants to
78 // be able to release its memory when it is no longer needed. The default
79 // behavior of passes is to hold onto memory for the entire duration of their
80 // lifetime (which is the entire compile time). For pipelined passes, this
81 // is not a big deal because that memory gets recycled every time the pass is
82 // invoked on another program unit. For IP passes, it is more important to
83 // free memory when it is unused.
85 // Optionally implement this function to release pass memory when it is no
88 virtual void releaseMemory() {}
91 // dumpPassStructure - Implement the -debug-passes=PassStructure option
92 virtual void dumpPassStructure(unsigned Offset = 0);
96 // getAnalysis<AnalysisType>() - This function is used by subclasses to get to
97 // the analysis information that they claim to use by overriding the
98 // getAnalysisUsageInfo function.
100 template<typename AnalysisType>
101 AnalysisType &getAnalysis(AnalysisID AID = AnalysisType::ID) {
102 assert(Resolver && "Pass not resident in a PassManager object!");
103 return *(AnalysisType*)Resolver->getAnalysis(AID);
107 friend class PassManagerT<Module>;
108 friend class PassManagerT<Method>;
109 friend class PassManagerT<BasicBlock>;
110 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Req,
111 AnalysisSet &Destroyed, AnalysisSet &Provided);
115 //===----------------------------------------------------------------------===//
116 // MethodPass class - This class is used to implement most global optimizations.
117 // Optimizations should subclass this class if they meet the following
119 // 1. Optimizations are organized globally, ie a method at a time
120 // 2. Optimizing a method does not cause the addition or removal of any methods
123 struct MethodPass : public Pass {
124 // doInitialization - Virtual method overridden by subclasses to do
125 // any neccesary per-module initialization.
127 virtual bool doInitialization(Module *M) { return false; }
129 // runOnMethod - Virtual method overriden by subclasses to do the per-method
130 // processing of the pass.
132 virtual bool runOnMethod(Method *M) = 0;
134 // doFinalization - Virtual method overriden by subclasses to do any post
135 // processing needed after all passes have run.
137 virtual bool doFinalization(Module *M) { return false; }
139 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
140 // for every method in the module, then by finalizing.
142 virtual bool run(Module *M);
144 // run - On a method, we simply initialize, run the method, then finalize.
149 friend class PassManagerT<Module>;
150 friend class PassManagerT<Method>;
151 friend class PassManagerT<BasicBlock>;
152 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Req,
153 AnalysisSet &Dest, AnalysisSet &Prov);
154 virtual void addToPassManager(PassManagerT<Method> *PM,AnalysisSet &Req,
155 AnalysisSet &Dest, AnalysisSet &Prov);
160 //===----------------------------------------------------------------------===//
161 // BasicBlockPass class - This class is used to implement most local
162 // optimizations. Optimizations should subclass this class if they
163 // meet the following constraints:
164 // 1. Optimizations are local, operating on either a basic block or
165 // instruction at a time.
166 // 2. Optimizations do not modify the CFG of the contained method, or any
167 // other basic block in the method.
168 // 3. Optimizations conform to all of the contstraints of MethodPass's.
170 struct BasicBlockPass : public MethodPass {
171 // runOnBasicBlock - Virtual method overriden by subclasses to do the
172 // per-basicblock processing of the pass.
174 virtual bool runOnBasicBlock(BasicBlock *M) = 0;
176 // To run this pass on a method, we simply call runOnBasicBlock once for each
179 virtual bool runOnMethod(Method *BB);
181 // To run directly on the basic block, we initialize, runOnBasicBlock, then
184 bool run(BasicBlock *BB);
187 friend class PassManagerT<Method>;
188 friend class PassManagerT<BasicBlock>;
189 virtual void addToPassManager(PassManagerT<Method> *PM, AnalysisSet &,
190 AnalysisSet &, AnalysisSet &);
191 virtual void addToPassManager(PassManagerT<BasicBlock> *PM, AnalysisSet &,
192 AnalysisSet &, AnalysisSet &);
196 // CreatePass - Helper template to invoke the constructor for the AnalysisID
197 // class. Note that this should be a template internal to AnalysisID, but
198 // GCC 2.95.3 crashes if we do that, doh.
200 template<class AnalysisType>
201 static Pass *CreatePass(AnalysisID ID) { return new AnalysisType(ID); }
203 //===----------------------------------------------------------------------===//
204 // AnalysisID - This class is used to uniquely identify an analysis pass that
205 // is referenced by a transformation.
208 static unsigned NextID; // Next ID # to deal out...
209 unsigned ID; // Unique ID for this analysis
210 Pass *(*Constructor)(AnalysisID); // Constructor to return the Analysis
212 AnalysisID(); // Disable default ctor
213 AnalysisID(unsigned id, Pass *(*Ct)(AnalysisID)) : ID(id), Constructor(Ct) {}
215 // create - the only way to define a new AnalysisID. This static method is
216 // supposed to be used to define the class static AnalysisID's that are
217 // provided by analysis passes. In the implementation (.cpp) file for the
218 // class, there should be a line that looks like this (using CallGraph as an
221 // AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
223 template<class AnalysisType>
224 static AnalysisID create() {
225 return AnalysisID(NextID++, CreatePass<AnalysisType>);
228 inline Pass *createPass() const { return Constructor(*this); }
230 inline bool operator==(const AnalysisID &A) const {
233 inline bool operator!=(const AnalysisID &A) const {
236 inline bool operator<(const AnalysisID &A) const {
242 //===----------------------------------------------------------------------===//
243 // AnalysisResolver - Simple interface implemented by PassManagers objects that
244 // is used to pull analysis information out of them.
246 struct AnalysisResolver {
247 virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
248 virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
249 Pass *getAnalysis(AnalysisID ID) {
250 Pass *Result = getAnalysisOrNullUp(ID);
251 assert(Result && "Pass has an incorrect analysis uses set!");
254 virtual unsigned getDepth() const = 0;
256 virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
258 void setAnalysisResolver(Pass *P, AnalysisResolver *AR);