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