LLVM API Documentation
00001 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a base class that indicates that a specified class is a 00011 // transformation pass implementation. 00012 // 00013 // Passes are designed this way so that it is possible to run passes in a cache 00014 // and organizationally optimal order without having to specify it at the front 00015 // end. This allows arbitrary passes to be strung together and have them 00016 // executed as efficiently as possible. 00017 // 00018 // Passes should extend one of the classes below, depending on the guarantees 00019 // that it can make about what will be modified as it is run. For example, most 00020 // global optimizations should derive from FunctionPass, because they do not add 00021 // or delete functions, they operate on the internals of the function. 00022 // 00023 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the 00024 // bottom), so the APIs exposed by these files are also automatically available 00025 // to all users of this file. 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 #ifndef LLVM_PASS_H 00030 #define LLVM_PASS_H 00031 00032 #include "llvm/Support/Compiler.h" 00033 #include <string> 00034 00035 namespace llvm { 00036 00037 class BasicBlock; 00038 class Function; 00039 class Module; 00040 class AnalysisUsage; 00041 class PassInfo; 00042 class ImmutablePass; 00043 class PMStack; 00044 class AnalysisResolver; 00045 class PMDataManager; 00046 class raw_ostream; 00047 class StringRef; 00048 00049 // AnalysisID - Use the PassInfo to identify a pass... 00050 typedef const void* AnalysisID; 00051 00052 /// Different types of internal pass managers. External pass managers 00053 /// (PassManager and FunctionPassManager) are not represented here. 00054 /// Ordering of pass manager types is important here. 00055 enum PassManagerType { 00056 PMT_Unknown = 0, 00057 PMT_ModulePassManager = 1, ///< MPPassManager 00058 PMT_CallGraphPassManager, ///< CGPassManager 00059 PMT_FunctionPassManager, ///< FPPassManager 00060 PMT_LoopPassManager, ///< LPPassManager 00061 PMT_RegionPassManager, ///< RGPassManager 00062 PMT_BasicBlockPassManager, ///< BBPassManager 00063 PMT_Last 00064 }; 00065 00066 // Different types of passes. 00067 enum PassKind { 00068 PT_BasicBlock, 00069 PT_Region, 00070 PT_Loop, 00071 PT_Function, 00072 PT_CallGraphSCC, 00073 PT_Module, 00074 PT_PassManager 00075 }; 00076 00077 //===----------------------------------------------------------------------===// 00078 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an 00079 /// interprocedural optimization or you do not fit into any of the more 00080 /// constrained passes described below. 00081 /// 00082 class Pass { 00083 AnalysisResolver *Resolver; // Used to resolve analysis 00084 const void *PassID; 00085 PassKind Kind; 00086 void operator=(const Pass&) LLVM_DELETED_FUNCTION; 00087 Pass(const Pass &) LLVM_DELETED_FUNCTION; 00088 00089 public: 00090 explicit Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { } 00091 virtual ~Pass(); 00092 00093 00094 PassKind getPassKind() const { return Kind; } 00095 00096 /// getPassName - Return a nice clean name for a pass. This usually 00097 /// implemented in terms of the name that is registered by one of the 00098 /// Registration templates, but can be overloaded directly. 00099 /// 00100 virtual const char *getPassName() const; 00101 00102 /// getPassID - Return the PassID number that corresponds to this pass. 00103 AnalysisID getPassID() const { 00104 return PassID; 00105 } 00106 00107 /// doInitialization - Virtual method overridden by subclasses to do 00108 /// any necessary initialization before any pass is run. 00109 /// 00110 virtual bool doInitialization(Module &) { return false; } 00111 00112 /// doFinalization - Virtual method overriden by subclasses to do any 00113 /// necessary clean up after all passes have run. 00114 /// 00115 virtual bool doFinalization(Module &) { return false; } 00116 00117 /// print - Print out the internal state of the pass. This is called by 00118 /// Analyze to print out the contents of an analysis. Otherwise it is not 00119 /// necessary to implement this method. Beware that the module pointer MAY be 00120 /// null. This automatically forwards to a virtual function that does not 00121 /// provide the Module* in case the analysis doesn't need it it can just be 00122 /// ignored. 00123 /// 00124 virtual void print(raw_ostream &O, const Module *M) const; 00125 void dump() const; // dump - Print to stderr. 00126 00127 /// createPrinterPass - Get a Pass appropriate to print the IR this 00128 /// pass operates on (Module, Function or MachineFunction). 00129 virtual Pass *createPrinterPass(raw_ostream &O, 00130 const std::string &Banner) const = 0; 00131 00132 /// Each pass is responsible for assigning a pass manager to itself. 00133 /// PMS is the stack of available pass manager. 00134 virtual void assignPassManager(PMStack &, 00135 PassManagerType) {} 00136 /// Check if available pass managers are suitable for this pass or not. 00137 virtual void preparePassManager(PMStack &); 00138 00139 /// Return what kind of Pass Manager can manage this pass. 00140 virtual PassManagerType getPotentialPassManagerType() const; 00141 00142 // Access AnalysisResolver 00143 void setResolver(AnalysisResolver *AR); 00144 AnalysisResolver *getResolver() const { return Resolver; } 00145 00146 /// getAnalysisUsage - This function should be overriden by passes that need 00147 /// analysis information to do their job. If a pass specifies that it uses a 00148 /// particular analysis result to this function, it can then use the 00149 /// getAnalysis<AnalysisType>() function, below. 00150 /// 00151 virtual void getAnalysisUsage(AnalysisUsage &) const; 00152 00153 /// releaseMemory() - This member can be implemented by a pass if it wants to 00154 /// be able to release its memory when it is no longer needed. The default 00155 /// behavior of passes is to hold onto memory for the entire duration of their 00156 /// lifetime (which is the entire compile time). For pipelined passes, this 00157 /// is not a big deal because that memory gets recycled every time the pass is 00158 /// invoked on another program unit. For IP passes, it is more important to 00159 /// free memory when it is unused. 00160 /// 00161 /// Optionally implement this function to release pass memory when it is no 00162 /// longer used. 00163 /// 00164 virtual void releaseMemory(); 00165 00166 /// getAdjustedAnalysisPointer - This method is used when a pass implements 00167 /// an analysis interface through multiple inheritance. If needed, it should 00168 /// override this to adjust the this pointer as needed for the specified pass 00169 /// info. 00170 virtual void *getAdjustedAnalysisPointer(AnalysisID ID); 00171 virtual ImmutablePass *getAsImmutablePass(); 00172 virtual PMDataManager *getAsPMDataManager(); 00173 00174 /// verifyAnalysis() - This member can be implemented by a analysis pass to 00175 /// check state of analysis information. 00176 virtual void verifyAnalysis() const; 00177 00178 // dumpPassStructure - Implement the -debug-passes=PassStructure option 00179 virtual void dumpPassStructure(unsigned Offset = 0); 00180 00181 // lookupPassInfo - Return the pass info object for the specified pass class, 00182 // or null if it is not known. 00183 static const PassInfo *lookupPassInfo(const void *TI); 00184 00185 // lookupPassInfo - Return the pass info object for the pass with the given 00186 // argument string, or null if it is not known. 00187 static const PassInfo *lookupPassInfo(StringRef Arg); 00188 00189 // createPass - Create a object for the specified pass class, 00190 // or null if it is not known. 00191 static Pass *createPass(AnalysisID ID); 00192 00193 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to 00194 /// get analysis information that might be around, for example to update it. 00195 /// This is different than getAnalysis in that it can fail (if the analysis 00196 /// results haven't been computed), so should only be used if you can handle 00197 /// the case when the analysis is not available. This method is often used by 00198 /// transformation APIs to update analysis results for a pass automatically as 00199 /// the transform is performed. 00200 /// 00201 template<typename AnalysisType> AnalysisType * 00202 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h 00203 00204 /// mustPreserveAnalysisID - This method serves the same function as 00205 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This 00206 /// obviously cannot give you a properly typed instance of the class if you 00207 /// don't have the class name available (use getAnalysisIfAvailable if you 00208 /// do), but it can tell you if you need to preserve the pass at least. 00209 /// 00210 bool mustPreserveAnalysisID(char &AID) const; 00211 00212 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 00213 /// to the analysis information that they claim to use by overriding the 00214 /// getAnalysisUsage function. 00215 /// 00216 template<typename AnalysisType> 00217 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h 00218 00219 template<typename AnalysisType> 00220 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h 00221 00222 template<typename AnalysisType> 00223 AnalysisType &getAnalysisID(AnalysisID PI) const; 00224 00225 template<typename AnalysisType> 00226 AnalysisType &getAnalysisID(AnalysisID PI, Function &F); 00227 }; 00228 00229 00230 //===----------------------------------------------------------------------===// 00231 /// ModulePass class - This class is used to implement unstructured 00232 /// interprocedural optimizations and analyses. ModulePasses may do anything 00233 /// they want to the program. 00234 /// 00235 class ModulePass : public Pass { 00236 public: 00237 /// createPrinterPass - Get a module printer pass. 00238 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 00239 00240 /// runOnModule - Virtual method overriden by subclasses to process the module 00241 /// being operated on. 00242 virtual bool runOnModule(Module &M) = 0; 00243 00244 virtual void assignPassManager(PMStack &PMS, 00245 PassManagerType T); 00246 00247 /// Return what kind of Pass Manager can manage this pass. 00248 virtual PassManagerType getPotentialPassManagerType() const; 00249 00250 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {} 00251 // Force out-of-line virtual method. 00252 virtual ~ModulePass(); 00253 }; 00254 00255 00256 //===----------------------------------------------------------------------===// 00257 /// ImmutablePass class - This class is used to provide information that does 00258 /// not need to be run. This is useful for things like target information and 00259 /// "basic" versions of AnalysisGroups. 00260 /// 00261 class ImmutablePass : public ModulePass { 00262 public: 00263 /// initializePass - This method may be overriden by immutable passes to allow 00264 /// them to perform various initialization actions they require. This is 00265 /// primarily because an ImmutablePass can "require" another ImmutablePass, 00266 /// and if it does, the overloaded version of initializePass may get access to 00267 /// these passes with getAnalysis<>. 00268 /// 00269 virtual void initializePass(); 00270 00271 virtual ImmutablePass *getAsImmutablePass() { return this; } 00272 00273 /// ImmutablePasses are never run. 00274 /// 00275 bool runOnModule(Module &) { return false; } 00276 00277 explicit ImmutablePass(char &pid) 00278 : ModulePass(pid) {} 00279 00280 // Force out-of-line virtual method. 00281 virtual ~ImmutablePass(); 00282 }; 00283 00284 //===----------------------------------------------------------------------===// 00285 /// FunctionPass class - This class is used to implement most global 00286 /// optimizations. Optimizations should subclass this class if they meet the 00287 /// following constraints: 00288 /// 00289 /// 1. Optimizations are organized globally, i.e., a function at a time 00290 /// 2. Optimizing a function does not cause the addition or removal of any 00291 /// functions in the module 00292 /// 00293 class FunctionPass : public Pass { 00294 public: 00295 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {} 00296 00297 /// createPrinterPass - Get a function printer pass. 00298 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 00299 00300 /// runOnFunction - Virtual method overriden by subclasses to do the 00301 /// per-function processing of the pass. 00302 /// 00303 virtual bool runOnFunction(Function &F) = 0; 00304 00305 virtual void assignPassManager(PMStack &PMS, 00306 PassManagerType T); 00307 00308 /// Return what kind of Pass Manager can manage this pass. 00309 virtual PassManagerType getPotentialPassManagerType() const; 00310 }; 00311 00312 00313 00314 //===----------------------------------------------------------------------===// 00315 /// BasicBlockPass class - This class is used to implement most local 00316 /// optimizations. Optimizations should subclass this class if they 00317 /// meet the following constraints: 00318 /// 1. Optimizations are local, operating on either a basic block or 00319 /// instruction at a time. 00320 /// 2. Optimizations do not modify the CFG of the contained function, or any 00321 /// other basic block in the function. 00322 /// 3. Optimizations conform to all of the constraints of FunctionPasses. 00323 /// 00324 class BasicBlockPass : public Pass { 00325 public: 00326 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {} 00327 00328 /// createPrinterPass - Get a basic block printer pass. 00329 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 00330 00331 using llvm::Pass::doInitialization; 00332 using llvm::Pass::doFinalization; 00333 00334 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses 00335 /// to do any necessary per-function initialization. 00336 /// 00337 virtual bool doInitialization(Function &); 00338 00339 /// runOnBasicBlock - Virtual method overriden by subclasses to do the 00340 /// per-basicblock processing of the pass. 00341 /// 00342 virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 00343 00344 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to 00345 /// do any post processing needed after all passes have run. 00346 /// 00347 virtual bool doFinalization(Function &); 00348 00349 virtual void assignPassManager(PMStack &PMS, 00350 PassManagerType T); 00351 00352 /// Return what kind of Pass Manager can manage this pass. 00353 virtual PassManagerType getPotentialPassManagerType() const; 00354 }; 00355 00356 /// If the user specifies the -time-passes argument on an LLVM tool command line 00357 /// then the value of this boolean will be true, otherwise false. 00358 /// @brief This is the storage for the -time-passes option. 00359 extern bool TimePassesIsEnabled; 00360 00361 } // End llvm namespace 00362 00363 // Include support files that contain important APIs commonly used by Passes, 00364 // but that we want to separate out to make it easier to read the header files. 00365 // 00366 #include "llvm/PassSupport.h" 00367 #include "llvm/PassAnalysisSupport.h" 00368 00369 #endif