1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines stuff that is used to define and "use" Passes. This file
11 // is automatically #included by Pass.h, so:
13 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
15 // Instead, #include Pass.h.
17 // This file defines Pass registration code and classes used for it.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_PASSSUPPORT_H
22 #define LLVM_PASSSUPPORT_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"
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); \
42 TsanIgnoreWritesBegin(); \
43 TsanHappensBefore(&initialized); \
45 TsanIgnoreWritesEnd(); \
47 sys::cas_flag tmp = initialized; \
54 TsanHappensAfter(&initialized);
56 #define INITIALIZE_TM_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 PassInfo::TargetMachineCtor_t(callTargetMachineCtor< passName >)); \
61 Registry.registerPass(*PI, true); \
64 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
65 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
68 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
69 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
70 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
71 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
72 Registry.registerPass(*PI, true); \
75 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
76 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
79 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
80 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
82 #define INITIALIZE_PASS_DEPENDENCY(depName) \
83 initialize##depName##Pass(Registry);
84 #define INITIALIZE_AG_DEPENDENCY(depName) \
85 initialize##depName##AnalysisGroup(Registry);
87 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
88 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
89 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
90 Registry.registerPass(*PI, true); \
93 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
94 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
97 template<typename PassName>
98 Pass *callDefaultCtor() { return new PassName(); }
100 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
101 return new PassName(TM);
104 //===---------------------------------------------------------------------------
105 /// RegisterPass<t> template - This template class is used to notify the system
106 /// that a Pass is available for use, and registers it into the internal
107 /// database maintained by the PassManager. Unless this template is used, opt,
108 /// for example will not be able to see the pass and attempts to create the pass
109 /// will fail. This template is used in the follow manner (at global scope, in
112 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
114 /// This statement will cause your pass to be created by calling the default
115 /// constructor exposed by the pass. If you have a different constructor that
116 /// must be called, create a global constructor function (which takes the
117 /// arguments you need and returns a Pass*) and register your pass like this:
119 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
121 template<typename passName>
122 struct RegisterPass : public PassInfo {
124 // Register Pass using default constructor...
125 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
126 bool is_analysis = false)
127 : PassInfo(Name, PassArg, &passName::ID,
128 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
129 CFGOnly, is_analysis) {
130 PassRegistry::getPassRegistry()->registerPass(*this);
135 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
136 /// Analysis groups are used to define an interface (which need not derive from
137 /// Pass) that is required by passes to do their job. Analysis Groups differ
138 /// from normal analyses because any available implementation of the group will
139 /// be used if it is available.
141 /// If no analysis implementing the interface is available, a default
142 /// implementation is created and added. A pass registers itself as the default
143 /// implementation by specifying 'true' as the second template argument of this
146 /// In addition to registering itself as an analysis group member, a pass must
147 /// register itself normally as well. Passes may be members of multiple groups
148 /// and may still be "required" specifically by name.
150 /// The actual interface may also be registered as well (by not specifying the
151 /// second template argument). The interface should be registered to associate
152 /// a nice name with the interface.
154 class RegisterAGBase : public PassInfo {
156 RegisterAGBase(const char *Name,
157 const void *InterfaceID,
158 const void *PassID = nullptr,
159 bool isDefault = false);
162 template<typename Interface, bool Default = false>
163 struct RegisterAnalysisGroup : public RegisterAGBase {
164 explicit RegisterAnalysisGroup(PassInfo &RPB)
165 : RegisterAGBase(RPB.getPassName(),
166 &Interface::ID, RPB.getTypeInfo(),
170 explicit RegisterAnalysisGroup(const char *Name)
171 : RegisterAGBase(Name, &Interface::ID) {
175 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
176 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
177 initialize##defaultPass##Pass(Registry); \
178 PassInfo *AI = new PassInfo(name, & agName :: ID); \
179 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
182 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
183 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
187 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
188 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
189 if (!def) initialize##agName##AnalysisGroup(Registry); \
190 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
191 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
192 Registry.registerPass(*PI, true); \
194 PassInfo *AI = new PassInfo(name, & agName :: ID); \
195 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
199 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
200 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
204 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
205 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
206 if (!def) initialize##agName##AnalysisGroup(Registry);
208 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
209 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
210 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
211 Registry.registerPass(*PI, true); \
213 PassInfo *AI = new PassInfo(n, & agName :: ID); \
214 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
218 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
219 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
222 //===---------------------------------------------------------------------------
223 /// PassRegistrationListener class - This class is meant to be derived from by
224 /// clients that are interested in which passes get registered and unregistered
225 /// at runtime (which can be because of the RegisterPass constructors being run
226 /// as the program starts up, or may be because a shared object just got
229 struct PassRegistrationListener {
231 PassRegistrationListener() {}
232 virtual ~PassRegistrationListener() {}
234 /// Callback functions - These functions are invoked whenever a pass is loaded
235 /// or removed from the current executable.
237 virtual void passRegistered(const PassInfo *) {}
239 /// enumeratePasses - Iterate over the registered passes, calling the
240 /// passEnumerate callback on each PassInfo object.
242 void enumeratePasses();
244 /// passEnumerate - Callback function invoked when someone calls
245 /// enumeratePasses on this PassRegistrationListener object.
247 virtual void passEnumerate(const PassInfo *) {}
251 } // End llvm namespace