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/PassInfo.h" 00027 #include "llvm/PassRegistry.h" 00028 #include "llvm/Support/Atomic.h" 00029 #include "llvm/Support/Valgrind.h" 00030 #include <vector> 00031 00032 namespace llvm { 00033 00034 class TargetMachine; 00035 00036 #define CALL_ONCE_INITIALIZATION(function) \ 00037 static volatile sys::cas_flag initialized = 0; \ 00038 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \ 00039 if (old_val == 0) { \ 00040 function(Registry); \ 00041 sys::MemoryFence(); \ 00042 TsanIgnoreWritesBegin(); \ 00043 TsanHappensBefore(&initialized); \ 00044 initialized = 2; \ 00045 TsanIgnoreWritesEnd(); \ 00046 } else { \ 00047 sys::cas_flag tmp = initialized; \ 00048 sys::MemoryFence(); \ 00049 while (tmp != 2) { \ 00050 tmp = initialized; \ 00051 sys::MemoryFence(); \ 00052 } \ 00053 } \ 00054 TsanHappensAfter(&initialized); 00055 00056 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \ 00057 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00058 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00059 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00060 Registry.registerPass(*PI, true); \ 00061 return PI; \ 00062 } \ 00063 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00064 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00065 } 00066 00067 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \ 00068 static void* initialize##passName##PassOnce(PassRegistry &Registry) { 00069 00070 #define INITIALIZE_PASS_DEPENDENCY(depName) \ 00071 initialize##depName##Pass(Registry); 00072 #define INITIALIZE_AG_DEPENDENCY(depName) \ 00073 initialize##depName##AnalysisGroup(Registry); 00074 00075 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \ 00076 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00077 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00078 Registry.registerPass(*PI, true); \ 00079 return PI; \ 00080 } \ 00081 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00082 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00083 } 00084 00085 template<typename PassName> 00086 Pass *callDefaultCtor() { return new PassName(); } 00087 00088 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) { 00089 return new PassName(TM); 00090 } 00091 00092 //===--------------------------------------------------------------------------- 00093 /// RegisterPass<t> template - This template class is used to notify the system 00094 /// that a Pass is available for use, and registers it into the internal 00095 /// database maintained by the PassManager. Unless this template is used, opt, 00096 /// for example will not be able to see the pass and attempts to create the pass 00097 /// will fail. This template is used in the follow manner (at global scope, in 00098 /// your .cpp file): 00099 /// 00100 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 00101 /// 00102 /// This statement will cause your pass to be created by calling the default 00103 /// constructor exposed by the pass. If you have a different constructor that 00104 /// must be called, create a global constructor function (which takes the 00105 /// arguments you need and returns a Pass*) and register your pass like this: 00106 /// 00107 /// static RegisterPass<PassClassName> tmp("passopt", "My Name"); 00108 /// 00109 template<typename passName> 00110 struct RegisterPass : public PassInfo { 00111 00112 // Register Pass using default constructor... 00113 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false, 00114 bool is_analysis = false) 00115 : PassInfo(Name, PassArg, &passName::ID, 00116 PassInfo::NormalCtor_t(callDefaultCtor<passName>), 00117 CFGOnly, is_analysis) { 00118 PassRegistry::getPassRegistry()->registerPass(*this); 00119 } 00120 }; 00121 00122 00123 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 00124 /// Analysis groups are used to define an interface (which need not derive from 00125 /// Pass) that is required by passes to do their job. Analysis Groups differ 00126 /// from normal analyses because any available implementation of the group will 00127 /// be used if it is available. 00128 /// 00129 /// If no analysis implementing the interface is available, a default 00130 /// implementation is created and added. A pass registers itself as the default 00131 /// implementation by specifying 'true' as the second template argument of this 00132 /// class. 00133 /// 00134 /// In addition to registering itself as an analysis group member, a pass must 00135 /// register itself normally as well. Passes may be members of multiple groups 00136 /// and may still be "required" specifically by name. 00137 /// 00138 /// The actual interface may also be registered as well (by not specifying the 00139 /// second template argument). The interface should be registered to associate 00140 /// a nice name with the interface. 00141 /// 00142 class RegisterAGBase : public PassInfo { 00143 public: 00144 RegisterAGBase(const char *Name, 00145 const void *InterfaceID, 00146 const void *PassID = nullptr, 00147 bool isDefault = false); 00148 }; 00149 00150 template<typename Interface, bool Default = false> 00151 struct RegisterAnalysisGroup : public RegisterAGBase { 00152 explicit RegisterAnalysisGroup(PassInfo &RPB) 00153 : RegisterAGBase(RPB.getPassName(), 00154 &Interface::ID, RPB.getTypeInfo(), 00155 Default) { 00156 } 00157 00158 explicit RegisterAnalysisGroup(const char *Name) 00159 : RegisterAGBase(Name, &Interface::ID) { 00160 } 00161 }; 00162 00163 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \ 00164 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \ 00165 initialize##defaultPass##Pass(Registry); \ 00166 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 00167 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \ 00168 return AI; \ 00169 } \ 00170 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \ 00171 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \ 00172 } 00173 00174 00175 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \ 00176 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00177 if (!def) initialize##agName##AnalysisGroup(Registry); \ 00178 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 00179 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00180 Registry.registerPass(*PI, true); \ 00181 \ 00182 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 00183 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 00184 *AI, def, true); \ 00185 return AI; \ 00186 } \ 00187 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00188 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00189 } 00190 00191 00192 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \ 00193 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 00194 if (!def) initialize##agName##AnalysisGroup(Registry); 00195 00196 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \ 00197 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \ 00198 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 00199 Registry.registerPass(*PI, true); \ 00200 \ 00201 PassInfo *AI = new PassInfo(n, & agName :: ID); \ 00202 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 00203 *AI, def, true); \ 00204 return AI; \ 00205 } \ 00206 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 00207 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 00208 } 00209 00210 //===--------------------------------------------------------------------------- 00211 /// PassRegistrationListener class - This class is meant to be derived from by 00212 /// clients that are interested in which passes get registered and unregistered 00213 /// at runtime (which can be because of the RegisterPass constructors being run 00214 /// as the program starts up, or may be because a shared object just got 00215 /// loaded). 00216 /// 00217 struct PassRegistrationListener { 00218 00219 PassRegistrationListener() {} 00220 virtual ~PassRegistrationListener() {} 00221 00222 /// Callback functions - These functions are invoked whenever a pass is loaded 00223 /// or removed from the current executable. 00224 /// 00225 virtual void passRegistered(const PassInfo *) {} 00226 00227 /// enumeratePasses - Iterate over the registered passes, calling the 00228 /// passEnumerate callback on each PassInfo object. 00229 /// 00230 void enumeratePasses(); 00231 00232 /// passEnumerate - Callback function invoked when someone calls 00233 /// enumeratePasses on this PassRegistrationListener object. 00234 /// 00235 virtual void passEnumerate(const PassInfo *) {} 00236 }; 00237 00238 00239 } // End llvm namespace 00240 00241 #endif