PassSupport.h revision 6b5fe1962b7da7e8d3d4800396065f2f26d6112d
1//===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 2// 3// This file defines stuff that is used to define and "use" Passes. This file 4// is automatically #included by Pass.h, so: 5// 6// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 7// 8// Instead, #include Pass.h. 9// 10// This file defines Pass registration code and classes used for it. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_PASS_SUPPORT_H 15#define LLVM_PASS_SUPPORT_H 16 17// No need to include Pass.h, we are being included by it! 18 19#include <typeinfo> 20class TargetData; 21class TargetMachine; 22 23//===--------------------------------------------------------------------------- 24// PassInfo class - An instance of this class exists for every pass known by the 25// system, and can be obtained from a live Pass by calling its getPassInfo() 26// method. These objects are set up by the RegisterPass<> template, defined 27// below. 28// 29class PassInfo { 30 const char *PassName; // Nice name for Pass 31 const char *PassArgument; // Command Line argument to run this pass 32 const std::type_info &TypeInfo; // type_info object for this Pass class 33 unsigned char PassType; // Set of enums values below... 34 35 Pass *(*NormalCtor)(); // No argument ctor 36 Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object... 37 38public: 39 // PassType - Define symbolic constants that can be used to test to see if 40 // this pass should be listed by analyze or opt. Passes can use none, one or 41 // many of these flags or'd together. 42 // 43 enum { 44 Analysis = 1, Optimization = 2, LLC = 4 45 }; 46 47 // PassInfo ctor - Do not call this directly, this should only be invoked 48 // through RegisterPass. 49 PassInfo(const char *name, const char *arg, const std::type_info &ti, 50 unsigned pt, Pass *(*normal)(), Pass *(*data)(const TargetData &)) 51 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt), 52 NormalCtor(normal), DataCtor(data) { 53 } 54 55 // getPassName - Return the friendly name for the pass, never returns null 56 const char *getPassName() const { return PassName; } 57 58 // getPassArgument - Return the command line option that may be passed to 59 // 'opt' that will cause this pass to be run. This will return null if there 60 // is no argument. 61 // 62 const char *getPassArgument() const { return PassArgument; } 63 64 // getTypeInfo - Return the type_info object for the pass... 65 const std::type_info &getTypeInfo() const { return TypeInfo; } 66 67 // getPassType - Return the PassType of a pass. Note that this can be several 68 // different types or'd together. This is _strictly_ for use by opt, analyze 69 // and llc for deciding which passes to use as command line options. 70 // 71 unsigned getPassType() const { return PassType; } 72 73 // getNormalCtor - Return a pointer to a function, that when called, creates 74 // an instance of the pass and returns it. This pointer may be null if there 75 // is no default constructor for the pass. 76 77 Pass *(*getNormalCtor() const)() { 78 return NormalCtor; 79 } 80 81 // createPass() - Use this 82 Pass *createPass() const { 83 assert(NormalCtor && 84 "Cannot call createPass on PassInfo without default ctor!"); 85 return NormalCtor(); 86 } 87 88 // getDataCtor - Return a pointer to a function that creates an instance of 89 // the pass and returns it. This returns a constructor for a version of the 90 // pass that takes a TArgetData object as a parameter. 91 // 92 Pass *(*getDataCtor() const)(const TargetData &) { 93 return DataCtor; 94 } 95}; 96 97 98//===--------------------------------------------------------------------------- 99// RegisterPass<t> template - This template class is used to notify the system 100// that a Pass is available for use, and registers it into the internal database 101// maintained by the PassManager. Unless this template is used, opt, for 102// example will not be able to see the pass and attempts to create the pass will 103// fail. This template is used in the follow manner (at global scope, in your 104// .cpp file): 105// 106// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 107// 108// This statement will cause your pass to be created by calling the default 109// constructor exposed by the pass. If you have a different constructor that 110// must be called, create a global constructor function (which takes the 111// arguments you need and returns a Pass*) and register your pass like this: 112// 113// Pass *createMyPass(foo &opt) { return new MyPass(opt); } 114// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass); 115// 116struct RegisterPassBase { 117 // getPassInfo - Get the pass info for the registered class... 118 const PassInfo *getPassInfo() const { return PIObj; } 119 120 ~RegisterPassBase(); // Intentionally non-virtual... 121 122 inline operator PassInfo* () const { return PIObj; } 123 124protected: 125 PassInfo *PIObj; // The PassInfo object for this pass 126 void registerPass(PassInfo *); 127 128 // setPreservesCFG - Notice that this pass only depends on the CFG, so 129 // transformations that do not modify the CFG do not invalidate this pass. 130 // 131 void setPreservesCFG(); 132}; 133 134template<typename PassName> 135Pass *callDefaultCtor() { return new PassName(); } 136 137template<typename PassName> 138struct RegisterPass : public RegisterPassBase { 139 140 // Register Pass using default constructor... 141 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) { 142 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 143 callDefaultCtor<PassName>, 0)); 144 } 145 146 // Register Pass using default constructor explicitly... 147 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy, 148 Pass *(*ctor)()) { 149 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor,0)); 150 } 151 152 // Register Pass using TargetData constructor... 153 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy, 154 Pass *(*datactor)(const TargetData &)) { 155 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 156 0, datactor)); 157 } 158 159 // Generic constructor version that has an unknown ctor type... 160 template<typename CtorType> 161 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy, 162 CtorType *Fn) { 163 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0, 0)); 164 } 165}; 166 167// RegisterOpt - Register something that is to show up in Opt, this is just a 168// shortcut for specifying RegisterPass... 169// 170template<typename PassName> 171struct RegisterOpt : public RegisterPassBase { 172 RegisterOpt(const char *PassArg, const char *Name) { 173 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 174 PassInfo::Optimization, 175 callDefaultCtor<PassName>, 0)); 176 } 177 178 // Register Pass using default constructor explicitly... 179 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) { 180 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 181 PassInfo::Optimization, ctor, 0)); 182 } 183 184 // Register Pass using TargetData constructor... 185 RegisterOpt(const char *PassArg, const char *Name, 186 Pass *(*datactor)(const TargetData &)) { 187 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 188 PassInfo::Optimization, 0, datactor)); 189 } 190}; 191 192// RegisterAnalysis - Register something that is to show up in Analysis, this is 193// just a shortcut for specifying RegisterPass... Analyses take a special 194// argument that, when set to true, tells the system that the analysis ONLY 195// depends on the shape of the CFG, so if a transformation preserves the CFG 196// that the analysis is not invalidated. 197// 198template<typename PassName> 199struct RegisterAnalysis : public RegisterPassBase { 200 RegisterAnalysis(const char *PassArg, const char *Name, 201 bool CFGOnly = false) { 202 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 203 PassInfo::Analysis, 204 callDefaultCtor<PassName>, 0)); 205 if (CFGOnly) 206 setPreservesCFG(); 207 } 208}; 209 210// RegisterLLC - Register something that is to show up in LLC, this is just a 211// shortcut for specifying RegisterPass... 212// 213template<typename PassName> 214struct RegisterLLC : public RegisterPassBase { 215 RegisterLLC(const char *PassArg, const char *Name) { 216 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 217 PassInfo::LLC, 218 callDefaultCtor<PassName>, 0)); 219 } 220 221 // Register Pass using default constructor explicitly... 222 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) { 223 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 224 PassInfo::LLC, ctor, 0)); 225 } 226 227 // Register Pass using TargetData constructor... 228 RegisterLLC(const char *PassArg, const char *Name, 229 Pass *(*datactor)(const TargetData &)) { 230 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 231 PassInfo::LLC, 0, datactor)); 232 } 233 234 // Register Pass using TargetMachine constructor... 235 RegisterLLC(const char *PassArg, const char *Name, 236 Pass *(*datactor)(TargetMachine &)) { 237 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 238 PassInfo::LLC, 0, 0)); 239 } 240}; 241 242 243//===--------------------------------------------------------------------------- 244// PassRegistrationListener class - This class is meant to be derived from by 245// clients that are interested in which passes get registered and unregistered 246// at runtime (which can be because of the RegisterPass constructors being run 247// as the program starts up, or may be because a shared object just got loaded). 248// Deriving from the PassRegistationListener class automatically registers your 249// object to receive callbacks indicating when passes are loaded and removed. 250// 251struct PassRegistrationListener { 252 253 // PassRegistrationListener ctor - Add the current object to the list of 254 // PassRegistrationListeners... 255 PassRegistrationListener(); 256 257 // dtor - Remove object from list of listeners... 258 virtual ~PassRegistrationListener(); 259 260 // Callback functions - These functions are invoked whenever a pass is loaded 261 // or removed from the current executable. 262 // 263 virtual void passRegistered(const PassInfo *P) {} 264 virtual void passUnregistered(const PassInfo *P) {} 265 266 // enumeratePasses - Iterate over the registered passes, calling the 267 // passEnumerate callback on each PassInfo object. 268 // 269 void enumeratePasses(); 270 271 // passEnumerate - Callback function invoked when someone calls 272 // enumeratePasses on this PassRegistrationListener object. 273 // 274 virtual void passEnumerate(const PassInfo *P) {} 275}; 276 277#endif 278