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"
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); \
44 static llvm::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)); \
50 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
51 static void *initialize##passName##PassOnce(PassRegistry &Registry) {
53 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
54 #define INITIALIZE_AG_DEPENDENCY(depName) \
55 initialize##depName##AnalysisGroup(Registry);
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); \
64 static llvm::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)); \
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)
75 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
76 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
77 PassName::registerOptions();
79 template <typename PassName> Pass *callDefaultCtor() { return new PassName(); }
81 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
82 return new PassName(TM);
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
93 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
95 /// This statement will cause your pass to be created by calling the default
96 /// constructor exposed by the pass.
98 template <typename passName> struct RegisterPass : public PassInfo {
99 // Register Pass using default constructor...
100 RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
101 bool is_analysis = false)
102 : PassInfo(Name, PassArg, &passName::ID,
103 PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
105 PassRegistry::getPassRegistry()->registerPass(*this);
109 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
110 /// Analysis groups are used to define an interface (which need not derive from
111 /// Pass) that is required by passes to do their job. Analysis Groups differ
112 /// from normal analyses because any available implementation of the group will
113 /// be used if it is available.
115 /// If no analysis implementing the interface is available, a default
116 /// implementation is created and added. A pass registers itself as the default
117 /// implementation by specifying 'true' as the second template argument of this
120 /// In addition to registering itself as an analysis group member, a pass must
121 /// register itself normally as well. Passes may be members of multiple groups
122 /// and may still be "required" specifically by name.
124 /// The actual interface may also be registered as well (by not specifying the
125 /// second template argument). The interface should be registered to associate
126 /// a nice name with the interface.
128 class RegisterAGBase : public PassInfo {
130 RegisterAGBase(StringRef Name, const void *InterfaceID,
131 const void *PassID = nullptr, bool isDefault = false);
134 template <typename Interface, bool Default = false>
135 struct RegisterAnalysisGroup : public RegisterAGBase {
136 explicit RegisterAnalysisGroup(PassInfo &RPB)
137 : RegisterAGBase(RPB.getPassName(), &Interface::ID, RPB.getTypeInfo(),
140 explicit RegisterAnalysisGroup(const char *Name)
141 : RegisterAGBase(Name, &Interface::ID) {}
144 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
145 static void *initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
146 initialize##defaultPass##Pass(Registry); \
147 PassInfo *AI = new PassInfo(name, &agName::ID); \
148 Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true); \
151 static llvm::once_flag Initialize##agName##AnalysisGroupFlag; \
152 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
153 llvm::call_once(Initialize##agName##AnalysisGroupFlag, \
154 initialize##agName##AnalysisGroupOnce, \
155 std::ref(Registry)); \
158 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
159 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
161 initialize##agName##AnalysisGroup(Registry); \
162 PassInfo *PI = new PassInfo( \
163 name, arg, &passName::ID, \
164 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
165 Registry.registerPass(*PI, true); \
167 PassInfo *AI = new PassInfo(name, &agName::ID); \
168 Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, \
172 static llvm::once_flag Initialize##passName##PassFlag; \
173 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
174 llvm::call_once(Initialize##passName##PassFlag, \
175 initialize##passName##PassOnce, std::ref(Registry)); \
178 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
179 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
181 initialize##agName##AnalysisGroup(Registry);
183 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
184 PassInfo *PI = new PassInfo( \
185 n, arg, &passName::ID, \
186 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
187 Registry.registerPass(*PI, true); \
189 PassInfo *AI = new PassInfo(n, &agName::ID); \
190 Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true); \
193 static llvm::once_flag Initialize##passName##PassFlag; \
194 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
195 llvm::call_once(Initialize##passName##PassFlag, \
196 initialize##passName##PassOnce, std::ref(Registry)); \
199 //===---------------------------------------------------------------------------
200 /// PassRegistrationListener class - This class is meant to be derived from by
201 /// clients that are interested in which passes get registered and unregistered
202 /// at runtime (which can be because of the RegisterPass constructors being run
203 /// as the program starts up, or may be because a shared object just got
206 struct PassRegistrationListener {
207 PassRegistrationListener() {}
208 virtual ~PassRegistrationListener() {}
210 /// Callback functions - These functions are invoked whenever a pass is loaded
211 /// or removed from the current executable.
213 virtual void passRegistered(const PassInfo *) {}
215 /// enumeratePasses - Iterate over the registered passes, calling the
216 /// passEnumerate callback on each PassInfo object.
218 void enumeratePasses();
220 /// passEnumerate - Callback function invoked when someone calls
221 /// enumeratePasses on this PassRegistrationListener object.
223 virtual void passEnumerate(const PassInfo *) {}
226 } // End llvm namespace