1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines a base class that indicates that a specified class is a 11 // transformation pass implementation. 12 // 13 // Passes are designed this way so that it is possible to run passes in a cache 14 // and organizationally optimal order without having to specify it at the front 15 // end. This allows arbitrary passes to be strung together and have them 16 // executed as effeciently as possible. 17 // 18 // Passes should extend one of the classes below, depending on the guarantees 19 // that it can make about what will be modified as it is run. For example, most 20 // global optimizations should derive from FunctionPass, because they do not add 21 // or delete functions, they operate on the internals of the function. 22 // 23 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the 24 // bottom), so the APIs exposed by these files are also automatically available 25 // to all users of this file. 26 // 27 //===----------------------------------------------------------------------===// 28 29 #ifndef LLVM_PASS_H 30 #define LLVM_PASS_H 31 32 #include <string> 33 34 namespace llvm { 35 36 class BasicBlock; 37 class Function; 38 class Module; 39 class AnalysisUsage; 40 class PassInfo; 41 class ImmutablePass; 42 class PMStack; 43 class AnalysisResolver; 44 class PMDataManager; 45 class raw_ostream; 46 class StringRef; 47 48 // AnalysisID - Use the PassInfo to identify a pass... 49 typedef const void* AnalysisID; 50 51 /// Different types of internal pass managers. External pass managers 52 /// (PassManager and FunctionPassManager) are not represented here. 53 /// Ordering of pass manager types is important here. 54 enum PassManagerType { 55 PMT_Unknown = 0, 56 PMT_ModulePassManager = 1, ///< MPPassManager 57 PMT_CallGraphPassManager, ///< CGPassManager 58 PMT_FunctionPassManager, ///< FPPassManager 59 PMT_LoopPassManager, ///< LPPassManager 60 PMT_BasicBlockPassManager, ///< BBPassManager 61 PMT_Last 62 }; 63 64 // Different types of passes. 65 enum PassKind { 66 PT_BasicBlock, 67 PT_Loop, 68 PT_Function, 69 PT_CallGraphSCC, 70 PT_Module, 71 PT_PassManager 72 }; 73 74 //===----------------------------------------------------------------------===// 75 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an 76 /// interprocedural optimization or you do not fit into any of the more 77 /// constrained passes described below. 78 /// 79 class Pass { 80 AnalysisResolver *Resolver; // Used to resolve analysis 81 const void *PassID; 82 PassKind Kind; 83 void operator=(const Pass&); // DO NOT IMPLEMENT 84 Pass(const Pass &); // DO NOT IMPLEMENT 85 86 public: 87 explicit Pass(PassKind K, char &pid); 88 virtual ~Pass(); 89 90 getPassKind()91 PassKind getPassKind() const { return Kind; } 92 93 /// getPassName - Return a nice clean name for a pass. This usually 94 /// implemented in terms of the name that is registered by one of the 95 /// Registration templates, but can be overloaded directly. 96 /// 97 virtual const char *getPassName() const; 98 99 /// getPassID - Return the PassID number that corresponds to this pass. getPassID()100 virtual AnalysisID getPassID() const { 101 return PassID; 102 } 103 104 /// print - Print out the internal state of the pass. This is called by 105 /// Analyze to print out the contents of an analysis. Otherwise it is not 106 /// necessary to implement this method. Beware that the module pointer MAY be 107 /// null. This automatically forwards to a virtual function that does not 108 /// provide the Module* in case the analysis doesn't need it it can just be 109 /// ignored. 110 /// 111 virtual void print(raw_ostream &O, const Module *M) const; 112 void dump() const; // dump - Print to stderr. 113 114 /// createPrinterPass - Get a Pass appropriate to print the IR this 115 /// pass operates one (Module, Function or MachineFunction). 116 virtual Pass *createPrinterPass(raw_ostream &O, 117 const std::string &Banner) const = 0; 118 119 /// Each pass is responsible for assigning a pass manager to itself. 120 /// PMS is the stack of available pass manager. assignPassManager(PMStack &,PassManagerType)121 virtual void assignPassManager(PMStack &, 122 PassManagerType) {} 123 /// Check if available pass managers are suitable for this pass or not. 124 virtual void preparePassManager(PMStack &); 125 126 /// Return what kind of Pass Manager can manage this pass. 127 virtual PassManagerType getPotentialPassManagerType() const; 128 129 // Access AnalysisResolver 130 void setResolver(AnalysisResolver *AR); getResolver()131 AnalysisResolver *getResolver() const { return Resolver; } 132 133 /// getAnalysisUsage - This function should be overridden by passes that need 134 /// analysis information to do their job. If a pass specifies that it uses a 135 /// particular analysis result to this function, it can then use the 136 /// getAnalysis<AnalysisType>() function, below. 137 /// 138 virtual void getAnalysisUsage(AnalysisUsage &) const; 139 140 /// releaseMemory() - This member can be implemented by a pass if it wants to 141 /// be able to release its memory when it is no longer needed. The default 142 /// behavior of passes is to hold onto memory for the entire duration of their 143 /// lifetime (which is the entire compile time). For pipelined passes, this 144 /// is not a big deal because that memory gets recycled every time the pass is 145 /// invoked on another program unit. For IP passes, it is more important to 146 /// free memory when it is unused. 147 /// 148 /// Optionally implement this function to release pass memory when it is no 149 /// longer used. 150 /// 151 virtual void releaseMemory(); 152 153 /// getAdjustedAnalysisPointer - This method is used when a pass implements 154 /// an analysis interface through multiple inheritance. If needed, it should 155 /// override this to adjust the this pointer as needed for the specified pass 156 /// info. 157 virtual void *getAdjustedAnalysisPointer(AnalysisID ID); 158 virtual ImmutablePass *getAsImmutablePass(); 159 virtual PMDataManager *getAsPMDataManager(); 160 161 /// verifyAnalysis() - This member can be implemented by a analysis pass to 162 /// check state of analysis information. 163 virtual void verifyAnalysis() const; 164 165 // dumpPassStructure - Implement the -debug-passes=PassStructure option 166 virtual void dumpPassStructure(unsigned Offset = 0); 167 168 // lookupPassInfo - Return the pass info object for the specified pass class, 169 // or null if it is not known. 170 static const PassInfo *lookupPassInfo(const void *TI); 171 172 // lookupPassInfo - Return the pass info object for the pass with the given 173 // argument string, or null if it is not known. 174 static const PassInfo *lookupPassInfo(StringRef Arg); 175 176 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to 177 /// get analysis information that might be around, for example to update it. 178 /// This is different than getAnalysis in that it can fail (if the analysis 179 /// results haven't been computed), so should only be used if you can handle 180 /// the case when the analysis is not available. This method is often used by 181 /// transformation APIs to update analysis results for a pass automatically as 182 /// the transform is performed. 183 /// 184 template<typename AnalysisType> AnalysisType * 185 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h 186 187 /// mustPreserveAnalysisID - This method serves the same function as 188 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This 189 /// obviously cannot give you a properly typed instance of the class if you 190 /// don't have the class name available (use getAnalysisIfAvailable if you 191 /// do), but it can tell you if you need to preserve the pass at least. 192 /// 193 bool mustPreserveAnalysisID(char &AID) const; 194 195 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 196 /// to the analysis information that they claim to use by overriding the 197 /// getAnalysisUsage function. 198 /// 199 template<typename AnalysisType> 200 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h 201 202 template<typename AnalysisType> 203 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h 204 205 template<typename AnalysisType> 206 AnalysisType &getAnalysisID(AnalysisID PI) const; 207 208 template<typename AnalysisType> 209 AnalysisType &getAnalysisID(AnalysisID PI, Function &F); 210 }; 211 212 213 //===----------------------------------------------------------------------===// 214 /// ModulePass class - This class is used to implement unstructured 215 /// interprocedural optimizations and analyses. ModulePasses may do anything 216 /// they want to the program. 217 /// 218 class ModulePass : public Pass { 219 public: 220 /// createPrinterPass - Get a module printer pass. 221 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 222 223 /// runOnModule - Virtual method overridden by subclasses to process the module 224 /// being operated on. 225 virtual bool runOnModule(Module &M) = 0; 226 227 virtual void assignPassManager(PMStack &PMS, 228 PassManagerType T); 229 230 /// Return what kind of Pass Manager can manage this pass. 231 virtual PassManagerType getPotentialPassManagerType() const; 232 ModulePass(char & pid)233 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {} 234 // Force out-of-line virtual method. 235 virtual ~ModulePass(); 236 }; 237 238 239 //===----------------------------------------------------------------------===// 240 /// ImmutablePass class - This class is used to provide information that does 241 /// not need to be run. This is useful for things like target information and 242 /// "basic" versions of AnalysisGroups. 243 /// 244 class ImmutablePass : public ModulePass { 245 public: 246 /// initializePass - This method may be overridden by immutable passes to allow 247 /// them to perform various initialization actions they require. This is 248 /// primarily because an ImmutablePass can "require" another ImmutablePass, 249 /// and if it does, the overloaded version of initializePass may get access to 250 /// these passes with getAnalysis<>. 251 /// 252 virtual void initializePass(); 253 getAsImmutablePass()254 virtual ImmutablePass *getAsImmutablePass() { return this; } 255 256 /// ImmutablePasses are never run. 257 /// runOnModule(Module &)258 bool runOnModule(Module &) { return false; } 259 ImmutablePass(char & pid)260 explicit ImmutablePass(char &pid) 261 : ModulePass(pid) {} 262 263 // Force out-of-line virtual method. 264 virtual ~ImmutablePass(); 265 }; 266 267 //===----------------------------------------------------------------------===// 268 /// FunctionPass class - This class is used to implement most global 269 /// optimizations. Optimizations should subclass this class if they meet the 270 /// following constraints: 271 /// 272 /// 1. Optimizations are organized globally, i.e., a function at a time 273 /// 2. Optimizing a function does not cause the addition or removal of any 274 /// functions in the module 275 /// 276 class FunctionPass : public Pass { 277 public: FunctionPass(char & pid)278 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {} 279 280 /// createPrinterPass - Get a function printer pass. 281 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 282 283 /// doInitialization - Virtual method overridden by subclasses to do 284 /// any necessary per-module initialization. 285 /// 286 virtual bool doInitialization(Module &); 287 288 /// runOnFunction - Virtual method overridden by subclasses to do the 289 /// per-function processing of the pass. 290 /// 291 virtual bool runOnFunction(Function &F) = 0; 292 293 /// doFinalization - Virtual method overridden by subclasses to do any post 294 /// processing needed after all passes have run. 295 /// 296 virtual bool doFinalization(Module &); 297 298 virtual void assignPassManager(PMStack &PMS, 299 PassManagerType T); 300 301 /// Return what kind of Pass Manager can manage this pass. 302 virtual PassManagerType getPotentialPassManagerType() const; 303 }; 304 305 306 307 //===----------------------------------------------------------------------===// 308 /// BasicBlockPass class - This class is used to implement most local 309 /// optimizations. Optimizations should subclass this class if they 310 /// meet the following constraints: 311 /// 1. Optimizations are local, operating on either a basic block or 312 /// instruction at a time. 313 /// 2. Optimizations do not modify the CFG of the contained function, or any 314 /// other basic block in the function. 315 /// 3. Optimizations conform to all of the constraints of FunctionPasses. 316 /// 317 class BasicBlockPass : public Pass { 318 public: BasicBlockPass(char & pid)319 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {} 320 321 /// createPrinterPass - Get a function printer pass. 322 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 323 324 /// doInitialization - Virtual method overridden by subclasses to do 325 /// any necessary per-module initialization. 326 /// 327 virtual bool doInitialization(Module &); 328 329 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses 330 /// to do any necessary per-function initialization. 331 /// 332 virtual bool doInitialization(Function &); 333 334 /// runOnBasicBlock - Virtual method overridden by subclasses to do the 335 /// per-basicblock processing of the pass. 336 /// 337 virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 338 339 /// doFinalization - Virtual method overridden by BasicBlockPass subclasses to 340 /// do any post processing needed after all passes have run. 341 /// 342 virtual bool doFinalization(Function &); 343 344 /// doFinalization - Virtual method overridden by subclasses to do any post 345 /// processing needed after all passes have run. 346 /// 347 virtual bool doFinalization(Module &); 348 349 virtual void assignPassManager(PMStack &PMS, 350 PassManagerType T); 351 352 /// Return what kind of Pass Manager can manage this pass. 353 virtual PassManagerType getPotentialPassManagerType() const; 354 }; 355 356 /// If the user specifies the -time-passes argument on an LLVM tool command line 357 /// then the value of this boolean will be true, otherwise false. 358 /// @brief This is the storage for the -time-passes option. 359 extern bool TimePassesIsEnabled; 360 361 } // End llvm namespace 362 363 // Include support files that contain important APIs commonly used by Passes, 364 // but that we want to separate out to make it easier to read the header files. 365 // 366 #include "llvm/PassSupport.h" 367 #include "llvm/PassAnalysisSupport.h" 368 369 #endif 370