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