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/Compiler.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_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); \
63 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
64 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
67 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
68 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
70 #define INITIALIZE_PASS_DEPENDENCY(depName) \
71 initialize##depName##Pass(Registry);
72 #define INITIALIZE_AG_DEPENDENCY(depName) \
73 initialize##depName##AnalysisGroup(Registry);
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); \
81 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
82 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
85 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
86 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
87 PassName::registerOptions(); \
88 INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
90 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
91 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
92 PassName::registerOptions(); \
94 template<typename PassName>
95 Pass *callDefaultCtor() { return new PassName(); }
97 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
98 return new PassName(TM);
101 //===---------------------------------------------------------------------------
102 /// RegisterPass<t> template - This template class is used to notify the system
103 /// that a Pass is available for use, and registers it into the internal
104 /// database maintained by the PassManager. Unless this template is used, opt,
105 /// for example will not be able to see the pass and attempts to create the pass
106 /// will fail. This template is used in the follow manner (at global scope, in
109 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
111 /// This statement will cause your pass to be created by calling the default
112 /// constructor exposed by the pass. If you have a different constructor that
113 /// must be called, create a global constructor function (which takes the
114 /// arguments you need and returns a Pass*) and register your pass like this:
116 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
118 template<typename passName>
119 struct RegisterPass : public PassInfo {
121 // Register Pass using default constructor...
122 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
123 bool is_analysis = false)
124 : PassInfo(Name, PassArg, &passName::ID,
125 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
126 CFGOnly, is_analysis) {
127 PassRegistry::getPassRegistry()->registerPass(*this);
132 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
133 /// Analysis groups are used to define an interface (which need not derive from
134 /// Pass) that is required by passes to do their job. Analysis Groups differ
135 /// from normal analyses because any available implementation of the group will
136 /// be used if it is available.
138 /// If no analysis implementing the interface is available, a default
139 /// implementation is created and added. A pass registers itself as the default
140 /// implementation by specifying 'true' as the second template argument of this
143 /// In addition to registering itself as an analysis group member, a pass must
144 /// register itself normally as well. Passes may be members of multiple groups
145 /// and may still be "required" specifically by name.
147 /// The actual interface may also be registered as well (by not specifying the
148 /// second template argument). The interface should be registered to associate
149 /// a nice name with the interface.
151 class RegisterAGBase : public PassInfo {
153 RegisterAGBase(const char *Name,
154 const void *InterfaceID,
155 const void *PassID = nullptr,
156 bool isDefault = false);
159 template<typename Interface, bool Default = false>
160 struct RegisterAnalysisGroup : public RegisterAGBase {
161 explicit RegisterAnalysisGroup(PassInfo &RPB)
162 : RegisterAGBase(RPB.getPassName(),
163 &Interface::ID, RPB.getTypeInfo(),
167 explicit RegisterAnalysisGroup(const char *Name)
168 : RegisterAGBase(Name, &Interface::ID) {
172 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
173 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
174 initialize##defaultPass##Pass(Registry); \
175 PassInfo *AI = new PassInfo(name, & agName :: ID); \
176 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
179 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
180 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
184 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
185 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
186 if (!def) initialize##agName##AnalysisGroup(Registry); \
187 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
188 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
189 Registry.registerPass(*PI, true); \
191 PassInfo *AI = new PassInfo(name, & agName :: ID); \
192 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
196 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
197 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
201 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
202 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
203 if (!def) initialize##agName##AnalysisGroup(Registry);
205 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
206 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
207 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
208 Registry.registerPass(*PI, true); \
210 PassInfo *AI = new PassInfo(n, & agName :: ID); \
211 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
215 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
216 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
219 //===---------------------------------------------------------------------------
220 /// PassRegistrationListener class - This class is meant to be derived from by
221 /// clients that are interested in which passes get registered and unregistered
222 /// at runtime (which can be because of the RegisterPass constructors being run
223 /// as the program starts up, or may be because a shared object just got
226 struct PassRegistrationListener {
228 PassRegistrationListener() {}
229 virtual ~PassRegistrationListener() {}
231 /// Callback functions - These functions are invoked whenever a pass is loaded
232 /// or removed from the current executable.
234 virtual void passRegistered(const PassInfo *) {}
236 /// enumeratePasses - Iterate over the registered passes, calling the
237 /// passEnumerate callback on each PassInfo object.
239 void enumeratePasses();
241 /// passEnumerate - Callback function invoked when someone calls
242 /// enumeratePasses on this PassRegistrationListener object.
244 virtual void passEnumerate(const PassInfo *) {}
248 } // End llvm namespace