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