1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines stuff that is used to define and "use" Passes. This file
10 // is automatically #included by Pass.h, so:
11 //
12 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
13 //
14 // Instead, #include Pass.h.
15 //
16 // This file defines Pass registration code and classes used for it.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #if !defined(LLVM_PASS_H) || defined(LLVM_PASSSUPPORT_H)
21 #error "Do not include <PassSupport.h>; include <Pass.h> instead"
22 #endif
23
24 #ifndef LLVM_PASSSUPPORT_H
25 #define LLVM_PASSSUPPORT_H
26
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/PassInfo.h"
29 #include "llvm/PassRegistry.h"
30 #include "llvm/Support/Threading.h"
31 #include <functional>
32
33 namespace llvm {
34
35 class Pass;
36
37 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
38 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
39 PassInfo *PI = new PassInfo( \
40 name, arg, &passName::ID, \
41 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
42 Registry.registerPass(*PI, true); \
43 return PI; \
44 } \
45 static llvm::once_flag Initialize##passName##PassFlag; \
46 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
47 llvm::call_once(Initialize##passName##PassFlag, \
48 initialize##passName##PassOnce, std::ref(Registry)); \
49 }
50
51 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
52 static void *initialize##passName##PassOnce(PassRegistry &Registry) {
53
54 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
55 #define INITIALIZE_AG_DEPENDENCY(depName) \
56 initialize##depName##AnalysisGroup(Registry);
57
58 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
59 PassInfo *PI = new PassInfo( \
60 name, arg, &passName::ID, \
61 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
62 Registry.registerPass(*PI, true); \
63 return PI; \
64 } \
65 static llvm::once_flag Initialize##passName##PassFlag; \
66 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
67 llvm::call_once(Initialize##passName##PassFlag, \
68 initialize##passName##PassOnce, std::ref(Registry)); \
69 }
70
71 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
72 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
73 PassName::registerOptions(); \
74 INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
75
76 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
77 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
78 PassName::registerOptions();
79
callDefaultCtor()80 template <typename PassName> Pass *callDefaultCtor() { return new PassName(); }
81
82 //===---------------------------------------------------------------------------
83 /// RegisterPass<t> template - This template class is used to notify the system
84 /// that a Pass is available for use, and registers it into the internal
85 /// database maintained by the PassManager. Unless this template is used, opt,
86 /// for example will not be able to see the pass and attempts to create the pass
87 /// will fail. This template is used in the follow manner (at global scope, in
88 /// your .cpp file):
89 ///
90 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
91 ///
92 /// This statement will cause your pass to be created by calling the default
93 /// constructor exposed by the pass.
94 template <typename passName> struct RegisterPass : public PassInfo {
95 // Register Pass using default constructor...
96 RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
97 bool is_analysis = false)
98 : PassInfo(Name, PassArg, &passName::ID,
99 PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
100 is_analysis) {
101 PassRegistry::getPassRegistry()->registerPass(*this);
102 }
103 };
104
105 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
106 /// Analysis groups are used to define an interface (which need not derive from
107 /// Pass) that is required by passes to do their job. Analysis Groups differ
108 /// from normal analyses because any available implementation of the group will
109 /// be used if it is available.
110 ///
111 /// If no analysis implementing the interface is available, a default
112 /// implementation is created and added. A pass registers itself as the default
113 /// implementation by specifying 'true' as the second template argument of this
114 /// class.
115 ///
116 /// In addition to registering itself as an analysis group member, a pass must
117 /// register itself normally as well. Passes may be members of multiple groups
118 /// and may still be "required" specifically by name.
119 ///
120 /// The actual interface may also be registered as well (by not specifying the
121 /// second template argument). The interface should be registered to associate
122 /// a nice name with the interface.
123 class RegisterAGBase : public PassInfo {
124 public:
125 RegisterAGBase(StringRef Name, const void *InterfaceID,
126 const void *PassID = nullptr, bool isDefault = false);
127 };
128
129 template <typename Interface, bool Default = false>
130 struct RegisterAnalysisGroup : public RegisterAGBase {
RegisterAnalysisGroupRegisterAnalysisGroup131 explicit RegisterAnalysisGroup(PassInfo &RPB)
132 : RegisterAGBase(RPB.getPassName(), &Interface::ID, RPB.getTypeInfo(),
133 Default) {}
134
RegisterAnalysisGroupRegisterAnalysisGroup135 explicit RegisterAnalysisGroup(const char *Name)
136 : RegisterAGBase(Name, &Interface::ID) {}
137 };
138
139 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
140 static void *initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
141 initialize##defaultPass##Pass(Registry); \
142 PassInfo *AI = new PassInfo(name, &agName::ID); \
143 Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true); \
144 return AI; \
145 } \
146 static llvm::once_flag Initialize##agName##AnalysisGroupFlag; \
147 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
148 llvm::call_once(Initialize##agName##AnalysisGroupFlag, \
149 initialize##agName##AnalysisGroupOnce, \
150 std::ref(Registry)); \
151 }
152
153 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
154 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
155 if (!def) \
156 initialize##agName##AnalysisGroup(Registry); \
157 PassInfo *PI = new PassInfo( \
158 name, arg, &passName::ID, \
159 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
160 Registry.registerPass(*PI, true); \
161 \
162 PassInfo *AI = new PassInfo(name, &agName::ID); \
163 Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, \
164 true); \
165 return AI; \
166 } \
167 static llvm::once_flag Initialize##passName##PassFlag; \
168 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
169 llvm::call_once(Initialize##passName##PassFlag, \
170 initialize##passName##PassOnce, std::ref(Registry)); \
171 }
172
173 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
174 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
175 if (!def) \
176 initialize##agName##AnalysisGroup(Registry);
177
178 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
179 PassInfo *PI = new PassInfo( \
180 n, arg, &passName::ID, \
181 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
182 Registry.registerPass(*PI, true); \
183 \
184 PassInfo *AI = new PassInfo(n, &agName::ID); \
185 Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true); \
186 return AI; \
187 } \
188 static llvm::once_flag Initialize##passName##PassFlag; \
189 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
190 llvm::call_once(Initialize##passName##PassFlag, \
191 initialize##passName##PassOnce, std::ref(Registry)); \
192 }
193
194 //===---------------------------------------------------------------------------
195 /// PassRegistrationListener class - This class is meant to be derived from by
196 /// clients that are interested in which passes get registered and unregistered
197 /// at runtime (which can be because of the RegisterPass constructors being run
198 /// as the program starts up, or may be because a shared object just got
199 /// loaded).
200 struct PassRegistrationListener {
201 PassRegistrationListener() = default;
202 virtual ~PassRegistrationListener() = default;
203
204 /// Callback functions - These functions are invoked whenever a pass is loaded
205 /// or removed from the current executable.
passRegisteredPassRegistrationListener206 virtual void passRegistered(const PassInfo *) {}
207
208 /// enumeratePasses - Iterate over the registered passes, calling the
209 /// passEnumerate callback on each PassInfo object.
210 void enumeratePasses();
211
212 /// passEnumerate - Callback function invoked when someone calls
213 /// enumeratePasses on this PassRegistrationListener object.
passEnumeratePassRegistrationListener214 virtual void passEnumerate(const PassInfo *) {}
215 };
216
217 } // end namespace llvm
218
219 #endif // LLVM_PASSSUPPORT_H
220