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