LLVM API Documentation
00001 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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_PASSSUPPORT_H 00022 #define LLVM_PASSSUPPORT_H 00023 00024 #include "Pass.h" 00025 #include "llvm/InitializePasses.h" 00026 #include "llvm/PassRegistry.h" 00027 #include "llvm/Support/Atomic.h" 00028 #include "llvm/Support/Valgrind.h" 00029 #include <vector> 00030 00031 namespace llvm { 00032 00033 //===--------------------------------------------------------------------------- 00034 /// PassInfo class - An instance of this class exists for every pass known by 00035 /// the system, and can be obtained from a live Pass by calling its 00036 /// getPassInfo() method. These objects are set up by the RegisterPass<> 00037 /// template, defined below. 00038 /// 00039 class PassInfo { 00040 public: 00041 typedef Pass* (*NormalCtor_t)(); 00042 00043 private: 00044 const char *const PassName; // Nice name for Pass 00045 const char *const PassArgument; // Command Line argument to run this pass 00046 const void *PassID; 00047 const bool IsCFGOnlyPass; // Pass only looks at the CFG. 00048 const bool IsAnalysis; // True if an analysis pass. 00049 const bool IsAnalysisGroup; // True if an analysis group. 00050 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass 00051 00052 NormalCtor_t NormalCtor; 00053 00054 public: 00055 /// PassInfo ctor - Do not call this directly, this should only be invoked 00056 /// through RegisterPass. 00057 PassInfo(const char *name, const char *arg, const void *pi, 00058 NormalCtor_t normal, bool isCFGOnly, bool is_analysis) 00059 : PassName(name), PassArgument(arg), PassID(pi), 00060 IsCFGOnlyPass(isCFGOnly), 00061 IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { } 00062 /// PassInfo ctor - Do not call this directly, this should only be invoked 00063 /// through RegisterPass. This version is for use by analysis groups; it 00064 /// does not auto-register the pass. 00065 PassInfo(const char *name, const void *pi) 00066 : PassName(name), PassArgument(""), PassID(pi), 00067 IsCFGOnlyPass(false), 00068 IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { } 00069 00070 /// getPassName - Return the friendly name for the pass, never returns null 00071 /// 00072 const char *getPassName() const { return PassName; } 00073 00074 /// getPassArgument - Return the command line option that may be passed to 00075 /// 'opt' that will cause this pass to be run. This will return null if there 00076 /// is no argument. 00077 /// 00078 const char *getPassArgument() const { return PassArgument; } 00079 00080 /// getTypeInfo - Return the id object for the pass... 00081 /// TODO : Rename 00082 const void *getTypeInfo() const { return PassID; } 00083 00084 /// Return true if this PassID implements the specified ID pointer. 00085 bool isPassID(const void *IDPtr) const { 00086 return PassID == IDPtr; 00087 } 00088 00089 /// isAnalysisGroup - Return true if this is an analysis group, not a normal 00090 /// pass. 00091 /// 00092 bool isAnalysisGroup() const { return IsAnalysisGroup; } 00093 bool isAnalysis() const { return IsAnalysis; } 00094 00095 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the 00096 /// function. 00097 bool isCFGOnlyPass() const { return IsCFGOnlyPass; } 00098 00099 /// getNormalCtor - Return a pointer to a function, that when called, creates 00100 /// an instance of the pass and returns it. This pointer may be null if there 00101 /// is no default constructor for the pass. 00102 /// 00103 NormalCtor_t getNormalCtor() const { 00104 return NormalCtor; 00105 } 00106 void setNormalCtor(NormalCtor_t Ctor) { 00107 NormalCtor = Ctor; 00108 } 00109 00110 /// createPass() - Use this method to create an instance of this pass. 00111 Pass *createPass() const; 00112 00113 /// addInterfaceImplemented - This method is called when this pass is 00114 /// registered as a member of an analysis group with the RegisterAnalysisGroup 00115 /// template. 00116 /// 00117 void addInterfaceImplemented(const PassInfo *ItfPI) { 00118 ItfImpl.push_back(ItfPI); 00119 } 00120 00121 /// getInterfacesImplemented - Return a list of all of the analysis group 00122 /// interfaces implemented by this pass. 00123 /// 00124 const std::vector<const PassInfo*> &getInterfacesImplemented() const { 00125 return ItfImpl; 00126 } 00127 00128 private: 00129 void operator=(const PassInfo &) LLVM_DELETED_FUNCTION; 00130 PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION; 00131 }; 00132 00133 #define CALL_ONCE_INITIALIZATION(function) \ 00134 static volatile sys::cas_flag initialized = 0; \ 00135 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \ 00136 if (old_val == 0) { \ 00137 function(Registry); \ 00138 sys::MemoryFence(); \ 00139 TsanIgnoreWritesBegin(); \ 00140 TsanHappensBefore(&initialized); \ 00141 initialized = 2; \ 00142 TsanIgnoreWritesEnd(); \ 00143 } else { \ 00144 sys::cas_flag tmp = initialized; \ 00145 sys::MemoryFence(); \ 00146 while (tmp != 2) { \ 00147 tmp = initialized; \ 00148 sys::MemoryFence(); \ 00149 } \ 00150 } \ 00151 TsanHappensAfter(&initialized); 00152 00153 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \ 00154 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00155 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00156 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00157 Registry.registerPass(*PI, true); \ 00158 return PI; \ 00159 } \ 00160 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00161 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00162 } 00163 00164 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \ 00165 static void* initialize##passName##PassOnce(PassRegistry &Registry) { 00166 00167 #define INITIALIZE_PASS_DEPENDENCY(depName) \ 00168 initialize##depName##Pass(Registry); 00169 #define INITIALIZE_AG_DEPENDENCY(depName) \ 00170 initialize##depName##AnalysisGroup(Registry); 00171 00172 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \ 00173 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00174 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00175 Registry.registerPass(*PI, true); \ 00176 return PI; \ 00177 } \ 00178 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00179 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00180 } 00181 00182 template<typename PassName> 00183 Pass *callDefaultCtor() { return new PassName(); } 00184 00185 //===--------------------------------------------------------------------------- 00186 /// RegisterPass<t> template - This template class is used to notify the system 00187 /// that a Pass is available for use, and registers it into the internal 00188 /// database maintained by the PassManager. Unless this template is used, opt, 00189 /// for example will not be able to see the pass and attempts to create the pass 00190 /// will fail. This template is used in the follow manner (at global scope, in 00191 /// your .cpp file): 00192 /// 00193 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 00194 /// 00195 /// This statement will cause your pass to be created by calling the default 00196 /// constructor exposed by the pass. If you have a different constructor that 00197 /// must be called, create a global constructor function (which takes the 00198 /// arguments you need and returns a Pass*) and register your pass like this: 00199 /// 00200 /// static RegisterPass<PassClassName> tmp("passopt", "My Name"); 00201 /// 00202 template<typename passName> 00203 struct RegisterPass : public PassInfo { 00204 00205 // Register Pass using default constructor... 00206 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false, 00207 bool is_analysis = false) 00208 : PassInfo(Name, PassArg, &passName::ID, 00209 PassInfo::NormalCtor_t(callDefaultCtor<passName>), 00210 CFGOnly, is_analysis) { 00211 PassRegistry::getPassRegistry()->registerPass(*this); 00212 } 00213 }; 00214 00215 00216 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 00217 /// Analysis groups are used to define an interface (which need not derive from 00218 /// Pass) that is required by passes to do their job. Analysis Groups differ 00219 /// from normal analyses because any available implementation of the group will 00220 /// be used if it is available. 00221 /// 00222 /// If no analysis implementing the interface is available, a default 00223 /// implementation is created and added. A pass registers itself as the default 00224 /// implementation by specifying 'true' as the second template argument of this 00225 /// class. 00226 /// 00227 /// In addition to registering itself as an analysis group member, a pass must 00228 /// register itself normally as well. Passes may be members of multiple groups 00229 /// and may still be "required" specifically by name. 00230 /// 00231 /// The actual interface may also be registered as well (by not specifying the 00232 /// second template argument). The interface should be registered to associate 00233 /// a nice name with the interface. 00234 /// 00235 class RegisterAGBase : public PassInfo { 00236 public: 00237 RegisterAGBase(const char *Name, 00238 const void *InterfaceID, 00239 const void *PassID = 0, 00240 bool isDefault = false); 00241 }; 00242 00243 template<typename Interface, bool Default = false> 00244 struct RegisterAnalysisGroup : public RegisterAGBase { 00245 explicit RegisterAnalysisGroup(PassInfo &RPB) 00246 : RegisterAGBase(RPB.getPassName(), 00247 &Interface::ID, RPB.getTypeInfo(), 00248 Default) { 00249 } 00250 00251 explicit RegisterAnalysisGroup(const char *Name) 00252 : RegisterAGBase(Name, &Interface::ID) { 00253 } 00254 }; 00255 00256 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \ 00257 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \ 00258 initialize##defaultPass##Pass(Registry); \ 00259 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 00260 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \ 00261 return AI; \ 00262 } \ 00263 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \ 00264 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \ 00265 } 00266 00267 00268 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \ 00269 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00270 if (!def) initialize##agName##AnalysisGroup(Registry); \ 00271 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00272 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00273 Registry.registerPass(*PI, true); \ 00274 \ 00275 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 00276 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 00277 *AI, def, true); \ 00278 return AI; \ 00279 } \ 00280 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00281 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00282 } 00283 00284 00285 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \ 00286 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00287 if (!def) initialize##agName##AnalysisGroup(Registry); 00288 00289 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \ 00290 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \ 00291 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00292 Registry.registerPass(*PI, true); \ 00293 \ 00294 PassInfo *AI = new PassInfo(n, & agName :: ID); \ 00295 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 00296 *AI, def, true); \ 00297 return AI; \ 00298 } \ 00299 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00300 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00301 } 00302 00303 //===--------------------------------------------------------------------------- 00304 /// PassRegistrationListener class - This class is meant to be derived from by 00305 /// clients that are interested in which passes get registered and unregistered 00306 /// at runtime (which can be because of the RegisterPass constructors being run 00307 /// as the program starts up, or may be because a shared object just got 00308 /// loaded). Deriving from the PassRegistrationListener class automatically 00309 /// registers your object to receive callbacks indicating when passes are loaded 00310 /// and removed. 00311 /// 00312 struct PassRegistrationListener { 00313 00314 /// PassRegistrationListener ctor - Add the current object to the list of 00315 /// PassRegistrationListeners... 00316 PassRegistrationListener(); 00317 00318 /// dtor - Remove object from list of listeners... 00319 /// 00320 virtual ~PassRegistrationListener(); 00321 00322 /// Callback functions - These functions are invoked whenever a pass is loaded 00323 /// or removed from the current executable. 00324 /// 00325 virtual void passRegistered(const PassInfo *) {} 00326 00327 /// enumeratePasses - Iterate over the registered passes, calling the 00328 /// passEnumerate callback on each PassInfo object. 00329 /// 00330 void enumeratePasses(); 00331 00332 /// passEnumerate - Callback function invoked when someone calls 00333 /// enumeratePasses on this PassRegistrationListener object. 00334 /// 00335 virtual void passEnumerate(const PassInfo *) {} 00336 }; 00337 00338 00339 } // End llvm namespace 00340 00341 #endif