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 "llvm/Support/Compiler.h" 33#include <string> 34 35namespace llvm { 36 37class BasicBlock; 38class Function; 39class Module; 40class AnalysisUsage; 41class PassInfo; 42class ImmutablePass; 43class PMStack; 44class AnalysisResolver; 45class PMDataManager; 46class raw_ostream; 47class StringRef; 48 49// AnalysisID - Use the PassInfo to identify a pass... 50typedef const void* AnalysisID; 51 52/// Different types of internal pass managers. External pass managers 53/// (PassManager and FunctionPassManager) are not represented here. 54/// Ordering of pass manager types is important here. 55enum PassManagerType { 56 PMT_Unknown = 0, 57 PMT_ModulePassManager = 1, ///< MPPassManager 58 PMT_CallGraphPassManager, ///< CGPassManager 59 PMT_FunctionPassManager, ///< FPPassManager 60 PMT_LoopPassManager, ///< LPPassManager 61 PMT_RegionPassManager, ///< RGPassManager 62 PMT_BasicBlockPassManager, ///< BBPassManager 63 PMT_Last 64}; 65 66// Different types of passes. 67enum PassKind { 68 PT_BasicBlock, 69 PT_Region, 70 PT_Loop, 71 PT_Function, 72 PT_CallGraphSCC, 73 PT_Module, 74 PT_PassManager 75}; 76 77//===----------------------------------------------------------------------===// 78/// Pass interface - Implemented by all 'passes'. Subclass this if you are an 79/// interprocedural optimization or you do not fit into any of the more 80/// constrained passes described below. 81/// 82class Pass { 83 AnalysisResolver *Resolver; // Used to resolve analysis 84 const void *PassID; 85 PassKind Kind; 86 void operator=(const Pass&) LLVM_DELETED_FUNCTION; 87 Pass(const Pass &) LLVM_DELETED_FUNCTION; 88 89public: 90 explicit Pass(PassKind K, char &pid) : Resolver(0), 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 const char *getPassName() const; 101 102 /// getPassID - Return the PassID number that corresponds to this pass. 103 AnalysisID getPassID() const { 104 return PassID; 105 } 106 107 /// print - Print out the internal state of the pass. This is called by 108 /// Analyze to print out the contents of an analysis. Otherwise it is not 109 /// necessary to implement this method. Beware that the module pointer MAY be 110 /// null. This automatically forwards to a virtual function that does not 111 /// provide the Module* in case the analysis doesn't need it it can just be 112 /// ignored. 113 /// 114 virtual void print(raw_ostream &O, const Module *M) const; 115 void dump() const; // dump - Print to stderr. 116 117 /// createPrinterPass - Get a Pass appropriate to print the IR this 118 /// pass operates on (Module, Function or MachineFunction). 119 virtual Pass *createPrinterPass(raw_ostream &O, 120 const std::string &Banner) const = 0; 121 122 /// Each pass is responsible for assigning a pass manager to itself. 123 /// PMS is the stack of available pass manager. 124 virtual void assignPassManager(PMStack &, 125 PassManagerType) {} 126 /// Check if available pass managers are suitable for this pass or not. 127 virtual void preparePassManager(PMStack &); 128 129 /// Return what kind of Pass Manager can manage this pass. 130 virtual PassManagerType getPotentialPassManagerType() const; 131 132 // Access AnalysisResolver 133 void setResolver(AnalysisResolver *AR); 134 AnalysisResolver *getResolver() const { return Resolver; } 135 136 /// getAnalysisUsage - This function should be overriden by passes that need 137 /// analysis information to do their job. If a pass specifies that it uses a 138 /// particular analysis result to this function, it can then use the 139 /// getAnalysis<AnalysisType>() function, below. 140 /// 141 virtual void getAnalysisUsage(AnalysisUsage &) const; 142 143 /// releaseMemory() - This member can be implemented by a pass if it wants to 144 /// be able to release its memory when it is no longer needed. The default 145 /// behavior of passes is to hold onto memory for the entire duration of their 146 /// lifetime (which is the entire compile time). For pipelined passes, this 147 /// is not a big deal because that memory gets recycled every time the pass is 148 /// invoked on another program unit. For IP passes, it is more important to 149 /// free memory when it is unused. 150 /// 151 /// Optionally implement this function to release pass memory when it is no 152 /// longer used. 153 /// 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 /// 191 template<typename AnalysisType> AnalysisType * 192 getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h 193 194 /// mustPreserveAnalysisID - This method serves the same function as 195 /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This 196 /// obviously cannot give you a properly typed instance of the class if you 197 /// don't have the class name available (use getAnalysisIfAvailable if you 198 /// do), but it can tell you if you need to preserve the pass at least. 199 /// 200 bool mustPreserveAnalysisID(char &AID) const; 201 202 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 203 /// to the analysis information that they claim to use by overriding the 204 /// getAnalysisUsage function. 205 /// 206 template<typename AnalysisType> 207 AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h 208 209 template<typename AnalysisType> 210 AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h 211 212 template<typename AnalysisType> 213 AnalysisType &getAnalysisID(AnalysisID PI) const; 214 215 template<typename AnalysisType> 216 AnalysisType &getAnalysisID(AnalysisID PI, Function &F); 217}; 218 219 220//===----------------------------------------------------------------------===// 221/// ModulePass class - This class is used to implement unstructured 222/// interprocedural optimizations and analyses. ModulePasses may do anything 223/// they want to the program. 224/// 225class ModulePass : public Pass { 226public: 227 /// createPrinterPass - Get a module printer pass. 228 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 229 230 /// runOnModule - Virtual method overriden by subclasses to process the module 231 /// being operated on. 232 virtual bool runOnModule(Module &M) = 0; 233 234 virtual void assignPassManager(PMStack &PMS, 235 PassManagerType T); 236 237 /// Return what kind of Pass Manager can manage this pass. 238 virtual PassManagerType getPotentialPassManagerType() const; 239 240 explicit ModulePass(char &pid) : Pass(PT_Module, pid) {} 241 // Force out-of-line virtual method. 242 virtual ~ModulePass(); 243}; 244 245 246//===----------------------------------------------------------------------===// 247/// ImmutablePass class - This class is used to provide information that does 248/// not need to be run. This is useful for things like target information and 249/// "basic" versions of AnalysisGroups. 250/// 251class ImmutablePass : public ModulePass { 252public: 253 /// initializePass - This method may be overriden by immutable passes to allow 254 /// them to perform various initialization actions they require. This is 255 /// primarily because an ImmutablePass can "require" another ImmutablePass, 256 /// and if it does, the overloaded version of initializePass may get access to 257 /// these passes with getAnalysis<>. 258 /// 259 virtual void initializePass(); 260 261 virtual ImmutablePass *getAsImmutablePass() { return this; } 262 263 /// ImmutablePasses are never run. 264 /// 265 bool runOnModule(Module &) { return false; } 266 267 explicit ImmutablePass(char &pid) 268 : ModulePass(pid) {} 269 270 // Force out-of-line virtual method. 271 virtual ~ImmutablePass(); 272}; 273 274//===----------------------------------------------------------------------===// 275/// FunctionPass class - This class is used to implement most global 276/// optimizations. Optimizations should subclass this class if they meet the 277/// following constraints: 278/// 279/// 1. Optimizations are organized globally, i.e., a function at a time 280/// 2. Optimizing a function does not cause the addition or removal of any 281/// functions in the module 282/// 283class FunctionPass : public Pass { 284public: 285 explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {} 286 287 /// createPrinterPass - Get a function printer pass. 288 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 289 290 /// doInitialization - Virtual method overridden by subclasses to do 291 /// any necessary per-module initialization. 292 /// 293 virtual bool doInitialization(Module &); 294 295 /// runOnFunction - Virtual method overriden by subclasses to do the 296 /// per-function processing of the pass. 297 /// 298 virtual bool runOnFunction(Function &F) = 0; 299 300 /// doFinalization - Virtual method overriden by subclasses to do any post 301 /// processing needed after all passes have run. 302 /// 303 virtual bool doFinalization(Module &); 304 305 virtual void assignPassManager(PMStack &PMS, 306 PassManagerType T); 307 308 /// Return what kind of Pass Manager can manage this pass. 309 virtual PassManagerType getPotentialPassManagerType() const; 310}; 311 312 313 314//===----------------------------------------------------------------------===// 315/// BasicBlockPass class - This class is used to implement most local 316/// optimizations. Optimizations should subclass this class if they 317/// meet the following constraints: 318/// 1. Optimizations are local, operating on either a basic block or 319/// instruction at a time. 320/// 2. Optimizations do not modify the CFG of the contained function, or any 321/// other basic block in the function. 322/// 3. Optimizations conform to all of the constraints of FunctionPasses. 323/// 324class BasicBlockPass : public Pass { 325public: 326 explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {} 327 328 /// createPrinterPass - Get a basic block printer pass. 329 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 330 331 /// doInitialization - Virtual method overridden by subclasses to do 332 /// any necessary per-module initialization. 333 /// 334 virtual bool doInitialization(Module &); 335 336 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses 337 /// to do any necessary per-function initialization. 338 /// 339 virtual bool doInitialization(Function &); 340 341 /// runOnBasicBlock - Virtual method overriden by subclasses to do the 342 /// per-basicblock processing of the pass. 343 /// 344 virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 345 346 /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to 347 /// do any post processing needed after all passes have run. 348 /// 349 virtual bool doFinalization(Function &); 350 351 /// doFinalization - Virtual method overriden by subclasses to do any post 352 /// processing needed after all passes have run. 353 /// 354 virtual bool doFinalization(Module &); 355 356 virtual void assignPassManager(PMStack &PMS, 357 PassManagerType T); 358 359 /// Return what kind of Pass Manager can manage this pass. 360 virtual PassManagerType getPotentialPassManagerType() const; 361}; 362 363/// If the user specifies the -time-passes argument on an LLVM tool command line 364/// then the value of this boolean will be true, otherwise false. 365/// @brief This is the storage for the -time-passes option. 366extern bool TimePassesIsEnabled; 367 368} // End llvm namespace 369 370// Include support files that contain important APIs commonly used by Passes, 371// but that we want to separate out to make it easier to read the header files. 372// 373#include "llvm/PassSupport.h" 374#include "llvm/PassAnalysisSupport.h" 375 376#endif 377