PassSupport.h revision 1ac2d518067f8341fb59c1c5ba1f5787fd78abe2
1//===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines stuff that is used to define and "use" Passes. This file 11// is automatically #included by Pass.h, so: 12// 13// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 14// 15// Instead, #include Pass.h. 16// 17// This file defines Pass registration code and classes used for it. 18// 19//===----------------------------------------------------------------------===// 20 21#ifndef LLVM_PASS_SUPPORT_H 22#define LLVM_PASS_SUPPORT_H 23 24// No need to include Pass.h, we are being included by it! 25 26namespace llvm { 27 28class TargetMachine; 29 30//===--------------------------------------------------------------------------- 31/// PassInfo class - An instance of this class exists for every pass known by 32/// the system, and can be obtained from a live Pass by calling its 33/// getPassInfo() method. These objects are set up by the RegisterPass<> 34/// template, defined below. 35/// 36class PassInfo { 37 const char *PassName; // Nice name for Pass 38 const char *PassArgument; // Command Line argument to run this pass 39 const std::type_info &TypeInfo; // type_info object for this Pass class 40 unsigned char PassType; // Set of enums values below... 41 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass 42 43 Pass *(*NormalCtor)(); // No argument ctor 44 Pass *(*TargetCtor)(TargetMachine&); // Ctor taking TargetMachine object... 45 46public: 47 /// PassType - Define symbolic constants that can be used to test to see if 48 /// this pass should be listed by analyze or opt. Passes can use none, one or 49 /// many of these flags or'd together. It is not legal to combine the 50 /// AnalysisGroup flag with others. 51 /// 52 enum { 53 Analysis = 1, Optimization = 2, AnalysisGroup = 4 54 }; 55 56 /// PassInfo ctor - Do not call this directly, this should only be invoked 57 /// through RegisterPass. 58 PassInfo(const char *name, const char *arg, const std::type_info &ti, 59 unsigned char pt, Pass *(*normal)() = 0, 60 Pass *(*targetctor)(TargetMachine &) = 0) 61 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt), 62 NormalCtor(normal), TargetCtor(targetctor) { 63 } 64 65 /// getPassName - Return the friendly name for the pass, never returns null 66 /// 67 const char *getPassName() const { return PassName; } 68 void setPassName(const char *Name) { PassName = Name; } 69 70 /// getPassArgument - Return the command line option that may be passed to 71 /// 'opt' that will cause this pass to be run. This will return null if there 72 /// is no argument. 73 /// 74 const char *getPassArgument() const { return PassArgument; } 75 76 /// getTypeInfo - Return the type_info object for the pass... 77 /// 78 const std::type_info &getTypeInfo() const { return TypeInfo; } 79 80 /// getPassType - Return the PassType of a pass. Note that this can be 81 /// several different types or'd together. This is _strictly_ for use by opt, 82 /// analyze and llc for deciding which passes to use as command line options. 83 /// 84 unsigned getPassType() const { return PassType; } 85 86 /// getNormalCtor - Return a pointer to a function, that when called, creates 87 /// an instance of the pass and returns it. This pointer may be null if there 88 /// is no default constructor for the pass. 89 /// 90 Pass *(*getNormalCtor() const)() { 91 return NormalCtor; 92 } 93 void setNormalCtor(Pass *(*Ctor)()) { 94 NormalCtor = Ctor; 95 } 96 97 /// createPass() - Use this method to create an instance of this pass. 98 Pass *createPass() const { 99 assert((PassType != AnalysisGroup || NormalCtor) && 100 "No default implementation found for analysis group!"); 101 assert(NormalCtor && 102 "Cannot call createPass on PassInfo without default ctor!"); 103 return NormalCtor(); 104 } 105 106 /// getTargetCtor - Return a pointer to a function that creates an instance of 107 /// the pass and returns it. This returns a constructor for a version of the 108 /// pass that takes a TargetMachine object as a parameter. 109 /// 110 Pass *(*getTargetCtor() const)(TargetMachine &) { 111 return TargetCtor; 112 } 113 114 /// addInterfaceImplemented - This method is called when this pass is 115 /// registered as a member of an analysis group with the RegisterAnalysisGroup 116 /// template. 117 /// 118 void addInterfaceImplemented(const PassInfo *ItfPI) { 119 ItfImpl.push_back(ItfPI); 120 } 121 122 /// getInterfacesImplemented - Return a list of all of the analysis group 123 /// interfaces implemented by this pass. 124 /// 125 const std::vector<const PassInfo*> &getInterfacesImplemented() const { 126 return ItfImpl; 127 } 128}; 129 130 131//===--------------------------------------------------------------------------- 132/// RegisterPass<t> template - This template class is used to notify the system 133/// that a Pass is available for use, and registers it into the internal 134/// database maintained by the PassManager. Unless this template is used, opt, 135/// for example will not be able to see the pass and attempts to create the pass 136/// will fail. This template is used in the follow manner (at global scope, in 137/// your .cpp file): 138/// 139/// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 140/// 141/// This statement will cause your pass to be created by calling the default 142/// constructor exposed by the pass. If you have a different constructor that 143/// must be called, create a global constructor function (which takes the 144/// arguments you need and returns a Pass*) and register your pass like this: 145/// 146/// Pass *createMyPass(foo &opt) { return new MyPass(opt); } 147/// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass); 148/// 149struct RegisterPassBase { 150 /// getPassInfo - Get the pass info for the registered class... 151 /// 152 const PassInfo *getPassInfo() const { return &PIObj; } 153 154 RegisterPassBase(const char *Name, const char *Arg, const std::type_info &TI, 155 unsigned char PT, Pass *(*Normal)() = 0, 156 Pass *(*TargetCtor)(TargetMachine &) = 0) 157 : PIObj(Name, Arg, TI, PT, Normal, TargetCtor) { 158 registerPass(); 159 } 160 RegisterPassBase(const std::type_info &TI, unsigned char PT) 161 : PIObj("", "", TI, PT, 0, 0) { 162 // This ctor may only be used for analysis groups: it does not auto-register 163 // the pass. 164 assert(PT == PassInfo::AnalysisGroup && "Not an AnalysisGroup!"); 165 } 166 167 ~RegisterPassBase() { // Intentionally non-virtual. 168 // Analysis groups are registered/unregistered by their dtor. 169 if (PIObj.getPassType() != PassInfo::AnalysisGroup) 170 unregisterPass(); 171 } 172 173protected: 174 PassInfo PIObj; // The PassInfo object for this pass 175 void registerPass(); 176 void unregisterPass(); 177 178 /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so 179 /// transformations that do not modify the CFG do not invalidate this pass. 180 /// 181 void setOnlyUsesCFG(); 182}; 183 184template<typename PassName> 185Pass *callDefaultCtor() { return new PassName(); } 186 187template<typename PassName> 188struct RegisterPass : public RegisterPassBase { 189 190 // Register Pass using default constructor... 191 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy = 0) 192 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 193 callDefaultCtor<PassName>) {} 194 195 // Register Pass using default constructor explicitly... 196 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 197 Pass *(*ctor)()) 198 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, ctor) {} 199 200 // Register Pass using TargetMachine constructor... 201 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 202 Pass *(*targetctor)(TargetMachine &)) 203 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 204 0, targetctor) {} 205 206 // Generic constructor version that has an unknown ctor type... 207 template<typename CtorType> 208 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy, 209 CtorType *Fn) 210 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 0) {} 211}; 212 213/// RegisterOpt - Register something that is to show up in Opt, this is just a 214/// shortcut for specifying RegisterPass... 215/// 216template<typename PassName> 217struct RegisterOpt : public RegisterPassBase { 218 RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false) 219 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 220 callDefaultCtor<PassName>) { 221 if (CFGOnly) setOnlyUsesCFG(); 222 } 223 224 /// Register Pass using default constructor explicitly... 225 /// 226 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(), 227 bool CFGOnly = false) 228 : RegisterPassBase(Name, PassArg, typeid(PassName), 229 PassInfo::Optimization, ctor) { 230 if (CFGOnly) setOnlyUsesCFG(); 231 } 232 233 /// Register FunctionPass using default constructor explicitly... 234 /// 235 RegisterOpt(const char *PassArg, const char *Name, FunctionPass *(*ctor)(), 236 bool CFGOnly = false) 237 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 238 static_cast<Pass*(*)()>(ctor)) { 239 if (CFGOnly) setOnlyUsesCFG(); 240 } 241 242 /// Register Pass using TargetMachine constructor... 243 /// 244 RegisterOpt(const char *PassArg, const char *Name, 245 Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false) 246 : RegisterPassBase(Name, PassArg, typeid(PassName), 247 PassInfo::Optimization, 0, targetctor) { 248 if (CFGOnly) setOnlyUsesCFG(); 249 } 250 251 /// Register FunctionPass using TargetMachine constructor... 252 /// 253 RegisterOpt(const char *PassArg, const char *Name, 254 FunctionPass *(*targetctor)(TargetMachine &), 255 bool CFGOnly = false) 256 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 0, 257 static_cast<Pass*(*)(TargetMachine&)>(targetctor)) { 258 if (CFGOnly) setOnlyUsesCFG(); 259 } 260}; 261 262/// RegisterAnalysis - Register something that is to show up in Analysis, this 263/// is just a shortcut for specifying RegisterPass... Analyses take a special 264/// argument that, when set to true, tells the system that the analysis ONLY 265/// depends on the shape of the CFG, so if a transformation preserves the CFG 266/// that the analysis is not invalidated. 267/// 268template<typename PassName> 269struct RegisterAnalysis : public RegisterPassBase { 270 RegisterAnalysis(const char *PassArg, const char *Name, 271 bool CFGOnly = false) 272 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Analysis, 273 callDefaultCtor<PassName>) { 274 if (CFGOnly) setOnlyUsesCFG(); 275 } 276}; 277 278 279/// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 280/// Analysis groups are used to define an interface (which need not derive from 281/// Pass) that is required by passes to do their job. Analysis Groups differ 282/// from normal analyses because any available implementation of the group will 283/// be used if it is available. 284/// 285/// If no analysis implementing the interface is available, a default 286/// implementation is created and added. A pass registers itself as the default 287/// implementation by specifying 'true' as the third template argument of this 288/// class. 289/// 290/// In addition to registering itself as an analysis group member, a pass must 291/// register itself normally as well. Passes may be members of multiple groups 292/// and may still be "required" specifically by name. 293/// 294/// The actual interface may also be registered as well (by not specifying the 295/// second template argument). The interface should be registered to associate 296/// a nice name with the interface. 297/// 298class RegisterAGBase : public RegisterPassBase { 299 PassInfo *InterfaceInfo; 300 const PassInfo *ImplementationInfo; 301 bool isDefaultImplementation; 302protected: 303 RegisterAGBase(const std::type_info &Interface, 304 const std::type_info *Pass = 0, 305 bool isDefault = false); 306 void setGroupName(const char *Name); 307public: 308 ~RegisterAGBase(); 309}; 310 311 312template<typename Interface, typename DefaultImplementationPass = void, 313 bool Default = false> 314struct RegisterAnalysisGroup : public RegisterAGBase { 315 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface), 316 &typeid(DefaultImplementationPass), 317 Default) { 318 } 319}; 320 321/// Define a specialization of RegisterAnalysisGroup that is used to set the 322/// name for the analysis group. 323/// 324template<typename Interface> 325struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase { 326 RegisterAnalysisGroup(const char *Name) 327 : RegisterAGBase(typeid(Interface)) { 328 setGroupName(Name); 329 } 330}; 331 332 333 334//===--------------------------------------------------------------------------- 335/// PassRegistrationListener class - This class is meant to be derived from by 336/// clients that are interested in which passes get registered and unregistered 337/// at runtime (which can be because of the RegisterPass constructors being run 338/// as the program starts up, or may be because a shared object just got 339/// loaded). Deriving from the PassRegistationListener class automatically 340/// registers your object to receive callbacks indicating when passes are loaded 341/// and removed. 342/// 343struct PassRegistrationListener { 344 345 /// PassRegistrationListener ctor - Add the current object to the list of 346 /// PassRegistrationListeners... 347 PassRegistrationListener(); 348 349 /// dtor - Remove object from list of listeners... 350 /// 351 virtual ~PassRegistrationListener(); 352 353 /// Callback functions - These functions are invoked whenever a pass is loaded 354 /// or removed from the current executable. 355 /// 356 virtual void passRegistered(const PassInfo *P) {} 357 virtual void passUnregistered(const PassInfo *P) {} 358 359 /// enumeratePasses - Iterate over the registered passes, calling the 360 /// passEnumerate callback on each PassInfo object. 361 /// 362 void enumeratePasses(); 363 364 /// passEnumerate - Callback function invoked when someone calls 365 /// enumeratePasses on this PassRegistrationListener object. 366 /// 367 virtual void passEnumerate(const PassInfo *P) {} 368}; 369 370 371//===--------------------------------------------------------------------------- 372/// IncludeFile class - This class is used as a hack to make sure that the 373/// implementation of a header file is included into a tool that uses the 374/// header. This is solely to overcome problems linking .a files and not 375/// getting the implementation of passes we need. 376/// 377struct IncludeFile { 378 IncludeFile(void *); 379}; 380 381} // End llvm namespace 382 383#endif 384