LLVM API Documentation
00001 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines stuff that is used to define and "use" Passes. This file 00011 // is automatically #included by Pass.h, so: 00012 // 00013 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 00014 // 00015 // Instead, #include Pass.h. 00016 // 00017 // This file defines Pass registration code and classes used for it. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #ifndef LLVM_PASS_SUPPORT_H 00022 #define LLVM_PASS_SUPPORT_H 00023 00024 #include "llvm/System/IncludeFile.h" 00025 // No need to include Pass.h, we are being included by it! 00026 00027 namespace llvm { 00028 00029 class TargetMachine; 00030 00031 //===--------------------------------------------------------------------------- 00032 /// PassInfo class - An instance of this class exists for every pass known by 00033 /// the system, and can be obtained from a live Pass by calling its 00034 /// getPassInfo() method. These objects are set up by the RegisterPass<> 00035 /// template, defined below. 00036 /// 00037 class PassInfo { 00038 const char *PassName; // Nice name for Pass 00039 const char *PassArgument; // Command Line argument to run this pass 00040 const std::type_info &TypeInfo; // type_info object for this Pass class 00041 unsigned char PassType; // Set of enums values below... 00042 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass 00043 00044 Pass *(*NormalCtor)(); // No argument ctor 00045 Pass *(*TargetCtor)(TargetMachine&); // Ctor taking TargetMachine object... 00046 00047 public: 00048 /// PassType - Define symbolic constants that can be used to test to see if 00049 /// this pass should be listed by analyze or opt. Passes can use none, one or 00050 /// many of these flags or'd together. It is not legal to combine the 00051 /// AnalysisGroup flag with others. 00052 /// 00053 enum { 00054 Analysis = 1, Optimization = 2, AnalysisGroup = 4 00055 }; 00056 00057 /// PassInfo ctor - Do not call this directly, this should only be invoked 00058 /// through RegisterPass. 00059 PassInfo(const char *name, const char *arg, const std::type_info &ti, 00060 unsigned char pt, Pass *(*normal)() = 0, 00061 Pass *(*targetctor)(TargetMachine &) = 0) 00062 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt), 00063 NormalCtor(normal), TargetCtor(targetctor) { 00064 } 00065 00066 /// getPassName - Return the friendly name for the pass, never returns null 00067 /// 00068 const char *getPassName() const { return PassName; } 00069 void setPassName(const char *Name) { PassName = Name; } 00070 00071 /// getPassArgument - Return the command line option that may be passed to 00072 /// 'opt' that will cause this pass to be run. This will return null if there 00073 /// is no argument. 00074 /// 00075 const char *getPassArgument() const { return PassArgument; } 00076 00077 /// getTypeInfo - Return the type_info object for the pass... 00078 /// 00079 const std::type_info &getTypeInfo() const { return TypeInfo; } 00080 00081 /// getPassType - Return the PassType of a pass. Note that this can be 00082 /// several different types or'd together. This is _strictly_ for use by opt, 00083 /// analyze and llc for deciding which passes to use as command line options. 00084 /// 00085 unsigned getPassType() const { return PassType; } 00086 00087 /// getNormalCtor - Return a pointer to a function, that when called, creates 00088 /// an instance of the pass and returns it. This pointer may be null if there 00089 /// is no default constructor for the pass. 00090 /// 00091 Pass *(*getNormalCtor() const)() { 00092 return NormalCtor; 00093 } 00094 void setNormalCtor(Pass *(*Ctor)()) { 00095 NormalCtor = Ctor; 00096 } 00097 00098 /// createPass() - Use this method to create an instance of this pass. 00099 Pass *createPass() const { 00100 assert((PassType != AnalysisGroup || NormalCtor) && 00101 "No default implementation found for analysis group!"); 00102 assert(NormalCtor && 00103 "Cannot call createPass on PassInfo without default ctor!"); 00104 return NormalCtor(); 00105 } 00106 00107 /// getTargetCtor - Return a pointer to a function that creates an instance of 00108 /// the pass and returns it. This returns a constructor for a version of the 00109 /// pass that takes a TargetMachine object as a parameter. 00110 /// 00111 Pass *(*getTargetCtor() const)(TargetMachine &) { 00112 return TargetCtor; 00113 } 00114 00115 /// addInterfaceImplemented - This method is called when this pass is 00116 /// registered as a member of an analysis group with the RegisterAnalysisGroup 00117 /// template. 00118 /// 00119 void addInterfaceImplemented(const PassInfo *ItfPI) { 00120 ItfImpl.push_back(ItfPI); 00121 } 00122 00123 /// getInterfacesImplemented - Return a list of all of the analysis group 00124 /// interfaces implemented by this pass. 00125 /// 00126 const std::vector<const PassInfo*> &getInterfacesImplemented() const { 00127 return ItfImpl; 00128 } 00129 }; 00130 00131 00132 //===--------------------------------------------------------------------------- 00133 /// RegisterPass<t> template - This template class is used to notify the system 00134 /// that a Pass is available for use, and registers it into the internal 00135 /// database maintained by the PassManager. Unless this template is used, opt, 00136 /// for example will not be able to see the pass and attempts to create the pass 00137 /// will fail. This template is used in the follow manner (at global scope, in 00138 /// your .cpp file): 00139 /// 00140 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 00141 /// 00142 /// This statement will cause your pass to be created by calling the default 00143 /// constructor exposed by the pass. If you have a different constructor that 00144 /// must be called, create a global constructor function (which takes the 00145 /// arguments you need and returns a Pass*) and register your pass like this: 00146 /// 00147 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); } 00148 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass); 00149 /// 00150 struct RegisterPassBase { 00151 /// getPassInfo - Get the pass info for the registered class... 00152 /// 00153 const PassInfo *getPassInfo() const { return &PIObj; } 00154 00155 RegisterPassBase(const char *Name, const char *Arg, const std::type_info &TI, 00156 unsigned char PT, Pass *(*Normal)() = 0, 00157 Pass *(*TargetCtor)(TargetMachine &) = 0) 00158 : PIObj(Name, Arg, TI, PT, Normal, TargetCtor) { 00159 registerPass(); 00160 } 00161 RegisterPassBase(const std::type_info &TI, unsigned char PT) 00162 : PIObj("", "", TI, PT, 0, 0) { 00163 // This ctor may only be used for analysis groups: it does not auto-register 00164 // the pass. 00165 assert(PT == PassInfo::AnalysisGroup && "Not an AnalysisGroup!"); 00166 } 00167 00168 ~RegisterPassBase() { // Intentionally non-virtual. 00169 // Analysis groups are registered/unregistered by their dtor. 00170 if (PIObj.getPassType() != PassInfo::AnalysisGroup) 00171 unregisterPass(); 00172 } 00173 00174 protected: 00175 PassInfo PIObj; // The PassInfo object for this pass 00176 void registerPass(); 00177 void unregisterPass(); 00178 00179 /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so 00180 /// transformations that do not modify the CFG do not invalidate this pass. 00181 /// 00182 void setOnlyUsesCFG(); 00183 }; 00184 00185 template<typename PassName> 00186 Pass *callDefaultCtor() { return new PassName(); } 00187 00188 template<typename PassName> 00189 struct RegisterPass : public RegisterPassBase { 00190 00191 // Register Pass using default constructor... 00192 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy = 0) 00193 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 00194 callDefaultCtor<PassName>) {} 00195 00196 // Register Pass using default constructor explicitly... 00197 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 00198 Pass *(*ctor)()) 00199 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, ctor) {} 00200 00201 // Register Pass using TargetMachine constructor... 00202 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 00203 Pass *(*targetctor)(TargetMachine &)) 00204 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 00205 0, targetctor) {} 00206 00207 // Generic constructor version that has an unknown ctor type... 00208 template<typename CtorType> 00209 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 00210 CtorType *Fn) 00211 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 0) {} 00212 }; 00213 00214 /// RegisterOpt - Register something that is to show up in Opt, this is just a 00215 /// shortcut for specifying RegisterPass... 00216 /// 00217 template<typename PassName> 00218 struct RegisterOpt : public RegisterPassBase { 00219 RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false) 00220 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 00221 callDefaultCtor<PassName>) { 00222 if (CFGOnly) setOnlyUsesCFG(); 00223 } 00224 00225 /// Register Pass using default constructor explicitly... 00226 /// 00227 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(), 00228 bool CFGOnly = false) 00229 : RegisterPassBase(Name, PassArg, typeid(PassName), 00230 PassInfo::Optimization, ctor) { 00231 if (CFGOnly) setOnlyUsesCFG(); 00232 } 00233 00234 /// Register FunctionPass using default constructor explicitly... 00235 /// 00236 RegisterOpt(const char *PassArg, const char *Name, FunctionPass *(*ctor)(), 00237 bool CFGOnly = false) 00238 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 00239 static_cast<Pass*(*)()>(ctor)) { 00240 if (CFGOnly) setOnlyUsesCFG(); 00241 } 00242 00243 /// Register Pass using TargetMachine constructor... 00244 /// 00245 RegisterOpt(const char *PassArg, const char *Name, 00246 Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false) 00247 : RegisterPassBase(Name, PassArg, typeid(PassName), 00248 PassInfo::Optimization, 0, targetctor) { 00249 if (CFGOnly) setOnlyUsesCFG(); 00250 } 00251 00252 /// Register FunctionPass using TargetMachine constructor... 00253 /// 00254 RegisterOpt(const char *PassArg, const char *Name, 00255 FunctionPass *(*targetctor)(TargetMachine &), 00256 bool CFGOnly = false) 00257 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 0, 00258 static_cast<Pass*(*)(TargetMachine&)>(targetctor)) { 00259 if (CFGOnly) setOnlyUsesCFG(); 00260 } 00261 }; 00262 00263 /// RegisterAnalysis - Register something that is to show up in Analysis, this 00264 /// is just a shortcut for specifying RegisterPass... Analyses take a special 00265 /// argument that, when set to true, tells the system that the analysis ONLY 00266 /// depends on the shape of the CFG, so if a transformation preserves the CFG 00267 /// that the analysis is not invalidated. 00268 /// 00269 template<typename PassName> 00270 struct RegisterAnalysis : public RegisterPassBase { 00271 RegisterAnalysis(const char *PassArg, const char *Name, 00272 bool CFGOnly = false) 00273 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Analysis, 00274 callDefaultCtor<PassName>) { 00275 if (CFGOnly) setOnlyUsesCFG(); 00276 } 00277 }; 00278 00279 00280 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 00281 /// Analysis groups are used to define an interface (which need not derive from 00282 /// Pass) that is required by passes to do their job. Analysis Groups differ 00283 /// from normal analyses because any available implementation of the group will 00284 /// be used if it is available. 00285 /// 00286 /// If no analysis implementing the interface is available, a default 00287 /// implementation is created and added. A pass registers itself as the default 00288 /// implementation by specifying 'true' as the third template argument of this 00289 /// class. 00290 /// 00291 /// In addition to registering itself as an analysis group member, a pass must 00292 /// register itself normally as well. Passes may be members of multiple groups 00293 /// and may still be "required" specifically by name. 00294 /// 00295 /// The actual interface may also be registered as well (by not specifying the 00296 /// second template argument). The interface should be registered to associate 00297 /// a nice name with the interface. 00298 /// 00299 class RegisterAGBase : public RegisterPassBase { 00300 PassInfo *InterfaceInfo; 00301 const PassInfo *ImplementationInfo; 00302 bool isDefaultImplementation; 00303 protected: 00304 RegisterAGBase(const std::type_info &Interface, 00305 const std::type_info *Pass = 0, 00306 bool isDefault = false); 00307 void setGroupName(const char *Name); 00308 public: 00309 ~RegisterAGBase(); 00310 }; 00311 00312 00313 template<typename Interface, typename DefaultImplementationPass = void, 00314 bool Default = false> 00315 struct RegisterAnalysisGroup : public RegisterAGBase { 00316 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface), 00317 &typeid(DefaultImplementationPass), 00318 Default) { 00319 } 00320 }; 00321 00322 /// Define a specialization of RegisterAnalysisGroup that is used to set the 00323 /// name for the analysis group. 00324 /// 00325 template<typename Interface> 00326 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase { 00327 RegisterAnalysisGroup(const char *Name) 00328 : RegisterAGBase(typeid(Interface)) { 00329 setGroupName(Name); 00330 } 00331 }; 00332 00333 00334 00335 //===--------------------------------------------------------------------------- 00336 /// PassRegistrationListener class - This class is meant to be derived from by 00337 /// clients that are interested in which passes get registered and unregistered 00338 /// at runtime (which can be because of the RegisterPass constructors being run 00339 /// as the program starts up, or may be because a shared object just got 00340 /// loaded). Deriving from the PassRegistationListener class automatically 00341 /// registers your object to receive callbacks indicating when passes are loaded 00342 /// and removed. 00343 /// 00344 struct PassRegistrationListener { 00345 00346 /// PassRegistrationListener ctor - Add the current object to the list of 00347 /// PassRegistrationListeners... 00348 PassRegistrationListener(); 00349 00350 /// dtor - Remove object from list of listeners... 00351 /// 00352 virtual ~PassRegistrationListener(); 00353 00354 /// Callback functions - These functions are invoked whenever a pass is loaded 00355 /// or removed from the current executable. 00356 /// 00357 virtual void passRegistered(const PassInfo *P) {} 00358 virtual void passUnregistered(const PassInfo *P) {} 00359 00360 /// enumeratePasses - Iterate over the registered passes, calling the 00361 /// passEnumerate callback on each PassInfo object. 00362 /// 00363 void enumeratePasses(); 00364 00365 /// passEnumerate - Callback function invoked when someone calls 00366 /// enumeratePasses on this PassRegistrationListener object. 00367 /// 00368 virtual void passEnumerate(const PassInfo *P) {} 00369 }; 00370 00371 } // End llvm namespace 00372 00373 #endif