Pass.h revision 3f917b6f99d1835f8bd9be6f7c74876dacf486c8
1//===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=// 2// 3// This file defines a base class that indicates that a specified class is a 4// transformation pass implementation. 5// 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. 10// 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. 15// 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. 19// 20//===----------------------------------------------------------------------===// 21 22#ifndef LLVM_PASS_H 23#define LLVM_PASS_H 24 25#include <vector> 26#include <map> 27#include <iosfwd> 28#include <typeinfo> 29class Value; 30class BasicBlock; 31class Function; 32class Module; 33class AnalysisUsage; 34class PassInfo; 35template<class UnitType> class PassManagerT; 36struct AnalysisResolver; 37 38// AnalysisID - Use the PassInfo to identify a pass... 39typedef const PassInfo* AnalysisID; 40 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. 45/// 46class Pass { 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 52public: 53 Pass() : Resolver(0), PassInfoCache(0) {} 54 virtual ~Pass() {} // Destructor is virtual so we can be subclassed 55 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 59 /// else is available, C++ RTTI will be consulted to get a SOMEWHAT 60 /// intelligable name for the pass. 61 /// 62 virtual const char *getPassName() const; 63 64 /// getPassInfo - Return the PassInfo data structure that corresponds to this 65 /// pass... If the pass has not been registered, this will return null. 66 /// 67 const PassInfo *getPassInfo() const; 68 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. 71 /// 72 virtual bool run(Module &M) = 0; 73 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 79 /// ignored. 80 /// 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); 84 85 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. 90 /// 91 virtual void getAnalysisUsage(AnalysisUsage &Info) const { 92 // By default, no analysis results are used, all are invalidated. 93 } 94 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. 102 /// 103 /// Optionally implement this function to release pass memory when it is no 104 /// longer used. 105 /// 106 virtual void releaseMemory() {} 107 108 // dumpPassStructure - Implement the -debug-passes=PassStructure option 109 virtual void dumpPassStructure(unsigned Offset = 0); 110 111 112 // getPassInfo - Static method to get the pass information from a class name. 113 template<typename AnalysisClass> 114 static const PassInfo *getClassPassInfo() { 115 return lookupPassInfo(typeid(AnalysisClass)); 116 } 117 118 // lookupPassInfo - Return the pass info object for the specified pass class, 119 // or null if it is not known. 120 static const PassInfo *lookupPassInfo(const std::type_info &TI); 121 122protected: 123 124 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 125 /// to the analysis information that they claim to use by overriding the 126 /// getAnalysisUsage function. 127 /// 128 template<typename AnalysisType> 129 AnalysisType &getAnalysis() const { 130 assert(Resolver && "Pass has not been inserted into a PassManager object!"); 131 const PassInfo *PI = getClassPassInfo<AnalysisType>(); 132 assert(PI && "getAnalysis for unregistered pass!"); 133 134 // Because the AnalysisType may not be a subclass of pass (for 135 // AnalysisGroups), we must use dynamic_cast here to potentially adjust the 136 // return pointer (because the class may multiply inherit, once from pass, 137 // once from AnalysisType). 138 // 139 AnalysisType *Result = 140 dynamic_cast<AnalysisType*>(Resolver->getAnalysis(PI)); 141 assert(Result && "Pass does not implement interface required!"); 142 return *Result; 143 } 144 145 template<typename AnalysisType> 146 AnalysisType &getAnalysisID(const PassInfo *PI) const { 147 assert(Resolver && "Pass has not been inserted into a PassManager object!"); 148 assert(PI && "getAnalysis for unregistered pass!"); 149 return *dynamic_cast<AnalysisType*>(Resolver->getAnalysis(PI)); 150 } 151 152 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses 153 /// to get to the analysis information that might be around that needs to be 154 /// updated. This is different than getAnalysis in that it can fail (ie the 155 /// analysis results haven't been computed), so should only be used if you 156 /// provide the capability to update an analysis that exists. 157 /// 158 template<typename AnalysisType> 159 AnalysisType *getAnalysisToUpdate() const { 160 assert(Resolver && "Pass not resident in a PassManager object!"); 161 const PassInfo *PI = getClassPassInfo<AnalysisType>(); 162 if (PI == 0) return 0; 163 return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI)); 164 } 165 166 167private: 168 friend class PassManagerT<Module>; 169 friend class PassManagerT<Function>; 170 friend class PassManagerT<BasicBlock>; 171 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU); 172}; 173 174inline std::ostream &operator<<(std::ostream &OS, const Pass &P) { 175 P.print(OS, 0); return OS; 176} 177 178//===----------------------------------------------------------------------===// 179/// FunctionPass class - This class is used to implement most global 180/// optimizations. Optimizations should subclass this class if they meet the 181/// following constraints: 182/// 183/// 1. Optimizations are organized globally, ie a function at a time 184/// 2. Optimizing a function does not cause the addition or removal of any 185/// functions in the module 186/// 187struct FunctionPass : public Pass { 188 /// doInitialization - Virtual method overridden by subclasses to do 189 /// any neccesary per-module initialization. 190 /// 191 virtual bool doInitialization(Module &M) { return false; } 192 193 /// runOnFunction - Virtual method overriden by subclasses to do the 194 /// per-function processing of the pass. 195 /// 196 virtual bool runOnFunction(Function &F) = 0; 197 198 /// doFinalization - Virtual method overriden by subclasses to do any post 199 /// processing needed after all passes have run. 200 /// 201 virtual bool doFinalization(Module &M) { return false; } 202 203 /// run - On a module, we run this pass by initializing, ronOnFunction'ing 204 /// once for every function in the module, then by finalizing. 205 /// 206 virtual bool run(Module &M); 207 208 /// run - On a function, we simply initialize, run the function, then 209 /// finalize. 210 /// 211 bool run(Function &F); 212 213private: 214 friend class PassManagerT<Module>; 215 friend class PassManagerT<Function>; 216 friend class PassManagerT<BasicBlock>; 217 virtual void addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU); 218 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU); 219}; 220 221 222 223//===----------------------------------------------------------------------===// 224/// BasicBlockPass class - This class is used to implement most local 225/// optimizations. Optimizations should subclass this class if they 226/// meet the following constraints: 227/// 1. Optimizations are local, operating on either a basic block or 228/// instruction at a time. 229/// 2. Optimizations do not modify the CFG of the contained function, or any 230/// other basic block in the function. 231/// 3. Optimizations conform to all of the contstraints of FunctionPass's. 232/// 233struct BasicBlockPass : public FunctionPass { 234 /// runOnBasicBlock - Virtual method overriden by subclasses to do the 235 /// per-basicblock processing of the pass. 236 /// 237 virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 238 239 /// To run this pass on a function, we simply call runOnBasicBlock once for 240 /// each function. 241 /// 242 virtual bool runOnFunction(Function &F); 243 244 /// To run directly on the basic block, we initialize, runOnBasicBlock, then 245 /// finalize. 246 /// 247 bool run(BasicBlock &BB); 248 249private: 250 friend class PassManagerT<Function>; 251 friend class PassManagerT<BasicBlock>; 252 virtual void addToPassManager(PassManagerT<Function> *PM, AnalysisUsage &AU); 253 virtual void addToPassManager(PassManagerT<BasicBlock> *PM,AnalysisUsage &AU); 254}; 255 256// Include support files that contain important APIs commonly used by Passes, 257// but that we want to seperate out to make it easier to read the header files. 258// 259#include "llvm/PassSupport.h" 260#include "llvm/PassAnalysisSupport.h" 261 262#endif 263