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 efficiently 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_RegionPassManager, ///< RGPassManager 61 PMT_BasicBlockPassManager, ///< BBPassManager 62 PMT_Last 63 }; 64 65 // Different types of passes. 66 enum PassKind { 67 PT_BasicBlock, 68 PT_Region, 69 PT_Loop, 70 PT_Function, 71 PT_CallGraphSCC, 72 PT_Module, 73 PT_PassManager 74 }; 75 76 //===----------------------------------------------------------------------===// 77 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an 78 /// interprocedural optimization or you do not fit into any of the more 79 /// constrained passes described below. 80 /// 81 class Pass { 82 AnalysisResolver *Resolver; // Used to resolve analysis 83 const void *PassID; 84 PassKind Kind; 85 void operator=(const Pass&) = delete; 86 Pass(const Pass &) = delete; 87 88 public: 89 explicit Pass(PassKind K, char &pid) 90 : Resolver(nullptr), PassID(&pid), Kind(K) { } 91 virtual ~Pass(); 92 93 94 PassKind getPassKind() const { return Kind; } 95 96 /// getPassName - Return a nice clean name for a pass. This usually 97 /// implemented in terms of the name that is registered by one of the 98 /// Registration templates, but can be overloaded directly. 99 /// 100 virtual StringRef getPassName() const; 101 102 /// getPassID - Return the PassID number that corresponds to this pass. 103 AnalysisID getPassID() const { 104 return PassID; 105 } 106 107 /// doInitialization - Virtual method overridden by subclasses to do 108 /// any necessary initialization before any pass is run. 109 /// 110 virtual bool doInitialization(Module &) { return false; } 111 112 /// doFinalization - Virtual method overriden by subclasses to do any 113 /// necessary clean up after all passes have run. 114 /// 115 virtual bool doFinalization(Module &) { return false; } 116 117 /// print - Print out the internal state of the pass. This is called by 118 /// Analyze to print out the contents of an analysis. Otherwise it is not 119 /// necessary to implement this method. Beware that the module pointer MAY be 120 /// null. This automatically forwards to a virtual function that does not 121 /// provide the Module* in case the analysis doesn't need it it can just be 122 /// ignored. 123 /// 124 virtual void print(raw_ostream &O, const Module *M) const; 125 void dump() const; // dump - Print to stderr. 126 127 /// createPrinterPass - Get a Pass appropriate to print the IR this 128 /// pass operates on (Module, Function or MachineFunction). 129 virtual Pass *createPrinterPass(raw_ostream &O, 130 const std::string &Banner) const = 0; 131 132 /// Each pass is responsible for assigning a pass manager to itself. 133 /// PMS is the stack of available pass manager. 134 virtual void assignPassManager(PMStack &, 135 PassManagerType) {} 136 /// Check if available pass managers are suitable for this pass or not. 137 virtual void preparePassManager(PMStack &); 138 139 /// Return what kind of Pass Manager can manage this pass. 140 virtual PassManagerType getPotentialPassManagerType() const; 141 142 // Access AnalysisResolver 143 void setResolver(AnalysisResolver *AR); 144 AnalysisResolver *getResolver() const { return Resolver; } 145 146 /// getAnalysisUsage - This function should be overriden by passes that need 147 /// analysis information to do their job. If a pass specifies that it uses a 148 /// particular analysis result to this function, it can then use the 149 /// getAnalysis<AnalysisType>() function, below. 150 /// 151 virtual void getAnalysisUsage(AnalysisUsage &) const; 152 153 /// releaseMemory() - This member can be implemented by a pass if it wants to 154 /// be able to release its memory when it is no longer needed. The default 155 /// behavior of passes is to hold onto memory for the entire duration of their 156 /// lifetime (which is the entire compile time). For pipelined passes, this 157 /// is not a big deal because that memory gets recycled every time the pass is 158 /// invoked on another program unit. For IP passes, it is more important to 159 /// free memory when it is unused. 160 /// 161 /// Optionally implement this function to release pass memory when it is no 162 /// longer used. 163 /// 164 virtual void releaseMemory(); 165 166 /// getAdjustedAnalysisPointer - This method is used when a pass implements 167 /// an analysis interface through multiple inheritance. If needed, it should 168 /// override this to adjust the this pointer as needed for the specified pass 169 /// info. 170 virtual void *getAdjustedAnalysisPointer(AnalysisID ID); 171 virtual ImmutablePass *getAsImmutablePass(); 172 virtual PMDataManager *getAsPMDataManager(); 173 174 /// verifyAnalysis() - This member can be implemented by a analysis pass to 175 /// check state of analysis information. 176 virtual void verifyAnalysis() const; 177 178 // dumpPassStructure - Implement the -debug-passes=PassStructure option 179 virtual void dumpPassStructure(unsigned Offset = 0); 180 181 // lookupPassInfo - Return the pass info object for the specified pass class, 182 // or null if it is not known. 183 static const PassInfo *lookupPassInfo(const void *TI); 184 185 // lookupPassInfo - Return the pass info object for the pass with the given 186 // argument string, or null if it is not known. 187 static const PassInfo *lookupPassInfo(StringRef Arg); 188 189 // createPass - Create a object for the specified pass class, 190 // or null if it is not known. 191 static Pass *createPass(AnalysisID ID); 192 193 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to 194 /// get analysis information that might be around, for example to update it. 195 /// This is different than getAnalysis in that it can fail (if the analysis 196 /// results haven't been computed), so should only be used if you can handle 197 /// the case when the analysis is not available. This method is often used by 198 /// transformation APIs to update analysis results for a pass automatically as 199 /// the transform is performed. 200 /// 201 template<typename AnalysisType> AnalysisType * 202 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h 203 204 /// mustPreserveAnalysisID - This method serves the same function as 205 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This 206 /// obviously cannot give you a properly typed instance of the class if you 207 /// don't have the class name available (use getAnalysisIfAvailable if you 208 /// do), but it can tell you if you need to preserve the pass at least. 209 /// 210 bool mustPreserveAnalysisID(char &AID) const; 211 212 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 213 /// to the analysis information that they claim to use by overriding the 214 /// getAnalysisUsage function. 215 /// 216 template<typename AnalysisType> 217 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h 218 219 template<typename AnalysisType> 220 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h 221 222 template<typename AnalysisType> 223 AnalysisType &getAnalysisID(AnalysisID PI) const; 224 225 template<typename AnalysisType> 226 AnalysisType &getAnalysisID(AnalysisID PI, Function &F); 227 }; 228 229 230 //===----------------------------------------------------------------------===// 231 /// ModulePass class - This class is used to implement unstructured 232 /// interprocedural optimizations and analyses. ModulePasses may do anything 233 /// they want to the program. 234 /// 235 class ModulePass : public Pass { 236 public: 237 /// createPrinterPass - Get a module printer pass. 238 Pass *createPrinterPass(raw_ostream &O, 239 const std::string &Banner) const override; 240 241 /// runOnModule - Virtual method overriden by subclasses to process the module 242 /// being operated on. 243 virtual bool runOnModule(Module &M) = 0; 244 245 void assignPassManager(PMStack &PMS, PassManagerType T) override; 246 247 /// Return what kind of Pass Manager can manage this pass. 248 PassManagerType getPotentialPassManagerType() const override; 249 250 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {} 251 // Force out-of-line virtual method. 252 ~ModulePass() override; 253 254 protected: 255 /// Optional passes call this function to check whether the pass should be 256 /// skipped. This is the case when optimization bisect is over the limit. 257 bool skipModule(Module &M) const; 258 }; 259 260 261 //===----------------------------------------------------------------------===// 262 /// ImmutablePass class - This class is used to provide information that does 263 /// not need to be run. This is useful for things like target information and 264 /// "basic" versions of AnalysisGroups. 265 /// 266 class ImmutablePass : public ModulePass { 267 public: 268 /// initializePass - This method may be overriden by immutable passes to allow 269 /// them to perform various initialization actions they require. This is 270 /// primarily because an ImmutablePass can "require" another ImmutablePass, 271 /// and if it does, the overloaded version of initializePass may get access to 272 /// these passes with getAnalysis<>. 273 /// 274 virtual void initializePass(); 275 276 ImmutablePass *getAsImmutablePass() override { return this; } 277 278 /// ImmutablePasses are never run. 279 /// 280 bool runOnModule(Module &) override { return false; } 281 282 explicit ImmutablePass(char &pid) 283 : ModulePass(pid) {} 284 285 // Force out-of-line virtual method. 286 ~ImmutablePass() override; 287 }; 288 289 //===----------------------------------------------------------------------===// 290 /// FunctionPass class - This class is used to implement most global 291 /// optimizations. Optimizations should subclass this class if they meet the 292 /// following constraints: 293 /// 294 /// 1. Optimizations are organized globally, i.e., a function at a time 295 /// 2. Optimizing a function does not cause the addition or removal of any 296 /// functions in the module 297 /// 298 class FunctionPass : public Pass { 299 public: 300 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {} 301 302 /// createPrinterPass - Get a function printer pass. 303 Pass *createPrinterPass(raw_ostream &O, 304 const std::string &Banner) const override; 305 306 /// runOnFunction - Virtual method overriden by subclasses to do the 307 /// per-function processing of the pass. 308 /// 309 virtual bool runOnFunction(Function &F) = 0; 310 311 void assignPassManager(PMStack &PMS, PassManagerType T) override; 312 313 /// Return what kind of Pass Manager can manage this pass. 314 PassManagerType getPotentialPassManagerType() const override; 315 316 protected: 317 /// Optional passes call this function to check whether the pass should be 318 /// skipped. This is the case when Attribute::OptimizeNone is set or when 319 /// optimization bisect is over the limit. 320 bool skipFunction(const Function &F) const; 321 }; 322 323 324 325 //===----------------------------------------------------------------------===// 326 /// BasicBlockPass class - This class is used to implement most local 327 /// optimizations. Optimizations should subclass this class if they 328 /// meet the following constraints: 329 /// 1. Optimizations are local, operating on either a basic block or 330 /// instruction at a time. 331 /// 2. Optimizations do not modify the CFG of the contained function, or any 332 /// other basic block in the function. 333 /// 3. Optimizations conform to all of the constraints of FunctionPasses. 334 /// 335 class BasicBlockPass : public Pass { 336 public: 337 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {} 338 339 /// createPrinterPass - Get a basic block printer pass. 340 Pass *createPrinterPass(raw_ostream &O, 341 const std::string &Banner) const override; 342 343 using llvm::Pass::doInitialization; 344 using llvm::Pass::doFinalization; 345 346 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses 347 /// to do any necessary per-function initialization. 348 /// 349 virtual bool doInitialization(Function &); 350 351 /// runOnBasicBlock - Virtual method overriden by subclasses to do the 352 /// per-basicblock processing of the pass. 353 /// 354 virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 355 356 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to 357 /// do any post processing needed after all passes have run. 358 /// 359 virtual bool doFinalization(Function &); 360 361 void assignPassManager(PMStack &PMS, PassManagerType T) override; 362 363 /// Return what kind of Pass Manager can manage this pass. 364 PassManagerType getPotentialPassManagerType() const override; 365 366 protected: 367 /// Optional passes call this function to check whether the pass should be 368 /// skipped. This is the case when Attribute::OptimizeNone is set or when 369 /// optimization bisect is over the limit. 370 bool skipBasicBlock(const BasicBlock &BB) const; 371 }; 372 373 /// If the user specifies the -time-passes argument on an LLVM tool command line 374 /// then the value of this boolean will be true, otherwise false. 375 /// @brief This is the storage for the -time-passes option. 376 extern bool TimePassesIsEnabled; 377 378 /// isFunctionInPrintList - returns true if a function should be printed via 379 // debugging options like -print-after-all/-print-before-all. 380 // @brief Tells if the function IR should be printed by PrinterPass. 381 extern bool isFunctionInPrintList(StringRef FunctionName); 382 } // End llvm namespace 383 384 // Include support files that contain important APIs commonly used by Passes, 385 // but that we want to separate out to make it easier to read the header files. 386 // 387 #include "llvm/PassAnalysisSupport.h" 388 #include "llvm/PassSupport.h" 389 390 #endif 391