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