1//===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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_PASSSUPPORT_H 22#define LLVM_PASSSUPPORT_H 23 24#include "Pass.h" 25#include "llvm/InitializePasses.h" 26#include "llvm/PassInfo.h" 27#include "llvm/PassRegistry.h" 28#include "llvm/Support/Atomic.h" 29#include "llvm/Support/Valgrind.h" 30#include <vector> 31 32namespace llvm { 33 34class TargetMachine; 35 36#define CALL_ONCE_INITIALIZATION(function) \ 37 static volatile sys::cas_flag initialized = 0; \ 38 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \ 39 if (old_val == 0) { \ 40 function(Registry); \ 41 sys::MemoryFence(); \ 42 TsanIgnoreWritesBegin(); \ 43 TsanHappensBefore(&initialized); \ 44 initialized = 2; \ 45 TsanIgnoreWritesEnd(); \ 46 } else { \ 47 sys::cas_flag tmp = initialized; \ 48 sys::MemoryFence(); \ 49 while (tmp != 2) { \ 50 tmp = initialized; \ 51 sys::MemoryFence(); \ 52 } \ 53 } \ 54 TsanHappensAfter(&initialized); 55 56#define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \ 57 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 58 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 59 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 60 Registry.registerPass(*PI, true); \ 61 return PI; \ 62 } \ 63 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 64 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 65 } 66 67#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \ 68 static void* initialize##passName##PassOnce(PassRegistry &Registry) { 69 70#define INITIALIZE_PASS_DEPENDENCY(depName) \ 71 initialize##depName##Pass(Registry); 72#define INITIALIZE_AG_DEPENDENCY(depName) \ 73 initialize##depName##AnalysisGroup(Registry); 74 75#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \ 76 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 77 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 78 Registry.registerPass(*PI, true); \ 79 return PI; \ 80 } \ 81 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 82 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 83 } 84 85template<typename PassName> 86Pass *callDefaultCtor() { return new PassName(); } 87 88template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) { 89 return new PassName(TM); 90} 91 92//===--------------------------------------------------------------------------- 93/// RegisterPass<t> template - This template class is used to notify the system 94/// that a Pass is available for use, and registers it into the internal 95/// database maintained by the PassManager. Unless this template is used, opt, 96/// for example will not be able to see the pass and attempts to create the pass 97/// will fail. This template is used in the follow manner (at global scope, in 98/// your .cpp file): 99/// 100/// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 101/// 102/// This statement will cause your pass to be created by calling the default 103/// constructor exposed by the pass. If you have a different constructor that 104/// must be called, create a global constructor function (which takes the 105/// arguments you need and returns a Pass*) and register your pass like this: 106/// 107/// static RegisterPass<PassClassName> tmp("passopt", "My Name"); 108/// 109template<typename passName> 110struct RegisterPass : public PassInfo { 111 112 // Register Pass using default constructor... 113 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false, 114 bool is_analysis = false) 115 : PassInfo(Name, PassArg, &passName::ID, 116 PassInfo::NormalCtor_t(callDefaultCtor<passName>), 117 CFGOnly, is_analysis) { 118 PassRegistry::getPassRegistry()->registerPass(*this); 119 } 120}; 121 122 123/// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 124/// Analysis groups are used to define an interface (which need not derive from 125/// Pass) that is required by passes to do their job. Analysis Groups differ 126/// from normal analyses because any available implementation of the group will 127/// be used if it is available. 128/// 129/// If no analysis implementing the interface is available, a default 130/// implementation is created and added. A pass registers itself as the default 131/// implementation by specifying 'true' as the second template argument of this 132/// class. 133/// 134/// In addition to registering itself as an analysis group member, a pass must 135/// register itself normally as well. Passes may be members of multiple groups 136/// and may still be "required" specifically by name. 137/// 138/// The actual interface may also be registered as well (by not specifying the 139/// second template argument). The interface should be registered to associate 140/// a nice name with the interface. 141/// 142class RegisterAGBase : public PassInfo { 143public: 144 RegisterAGBase(const char *Name, 145 const void *InterfaceID, 146 const void *PassID = nullptr, 147 bool isDefault = false); 148}; 149 150template<typename Interface, bool Default = false> 151struct RegisterAnalysisGroup : public RegisterAGBase { 152 explicit RegisterAnalysisGroup(PassInfo &RPB) 153 : RegisterAGBase(RPB.getPassName(), 154 &Interface::ID, RPB.getTypeInfo(), 155 Default) { 156 } 157 158 explicit RegisterAnalysisGroup(const char *Name) 159 : RegisterAGBase(Name, &Interface::ID) { 160 } 161}; 162 163#define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \ 164 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \ 165 initialize##defaultPass##Pass(Registry); \ 166 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 167 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \ 168 return AI; \ 169 } \ 170 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \ 171 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \ 172 } 173 174 175#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \ 176 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 177 if (!def) initialize##agName##AnalysisGroup(Registry); \ 178 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 179 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 180 Registry.registerPass(*PI, true); \ 181 \ 182 PassInfo *AI = new PassInfo(name, & agName :: ID); \ 183 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 184 *AI, def, true); \ 185 return AI; \ 186 } \ 187 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 188 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 189 } 190 191 192#define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \ 193 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 194 if (!def) initialize##agName##AnalysisGroup(Registry); 195 196#define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \ 197 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \ 198 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 199 Registry.registerPass(*PI, true); \ 200 \ 201 PassInfo *AI = new PassInfo(n, & agName :: ID); \ 202 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 203 *AI, def, true); \ 204 return AI; \ 205 } \ 206 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 207 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 208 } 209 210//===--------------------------------------------------------------------------- 211/// PassRegistrationListener class - This class is meant to be derived from by 212/// clients that are interested in which passes get registered and unregistered 213/// at runtime (which can be because of the RegisterPass constructors being run 214/// as the program starts up, or may be because a shared object just got 215/// loaded). 216/// 217struct PassRegistrationListener { 218 219 PassRegistrationListener() {} 220 virtual ~PassRegistrationListener() {} 221 222 /// Callback functions - These functions are invoked whenever a pass is loaded 223 /// or removed from the current executable. 224 /// 225 virtual void passRegistered(const PassInfo *) {} 226 227 /// enumeratePasses - Iterate over the registered passes, calling the 228 /// passEnumerate callback on each PassInfo object. 229 /// 230 void enumeratePasses(); 231 232 /// passEnumerate - Callback function invoked when someone calls 233 /// enumeratePasses on this PassRegistrationListener object. 234 /// 235 virtual void passEnumerate(const PassInfo *) {} 236}; 237 238 239} // End llvm namespace 240 241#endif 242