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