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/PassRegistry.h"
27 #include "llvm/Support/Atomic.h"
28 #include "llvm/Support/Valgrind.h"
34 //===---------------------------------------------------------------------------
35 /// PassInfo class - An instance of this class exists for every pass known by
36 /// the system, and can be obtained from a live Pass by calling its
37 /// getPassInfo() method. These objects are set up by the RegisterPass<>
38 /// template, defined below.
42 typedef Pass* (*NormalCtor_t)();
43 typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
46 const char *const PassName; // Nice name for Pass
47 const char *const PassArgument; // Command Line argument to run this pass
49 const bool IsCFGOnlyPass; // Pass only looks at the CFG.
50 const bool IsAnalysis; // True if an analysis pass.
51 const bool IsAnalysisGroup; // True if an analysis group.
52 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
54 NormalCtor_t NormalCtor;
55 TargetMachineCtor_t TargetMachineCtor;
58 /// PassInfo ctor - Do not call this directly, this should only be invoked
59 /// through RegisterPass.
60 PassInfo(const char *name, const char *arg, const void *pi,
61 NormalCtor_t normal, bool isCFGOnly, bool is_analysis,
62 TargetMachineCtor_t machine = nullptr)
63 : PassName(name), PassArgument(arg), PassID(pi),
64 IsCFGOnlyPass(isCFGOnly),
65 IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal),
66 TargetMachineCtor(machine) {}
67 /// PassInfo ctor - Do not call this directly, this should only be invoked
68 /// through RegisterPass. This version is for use by analysis groups; it
69 /// does not auto-register the pass.
70 PassInfo(const char *name, const void *pi)
71 : PassName(name), PassArgument(""), PassID(pi),
73 IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
74 TargetMachineCtor(nullptr) {}
76 /// getPassName - Return the friendly name for the pass, never returns null
78 const char *getPassName() const { return PassName; }
80 /// getPassArgument - Return the command line option that may be passed to
81 /// 'opt' that will cause this pass to be run. This will return null if there
84 const char *getPassArgument() const { return PassArgument; }
86 /// getTypeInfo - Return the id object for the pass...
88 const void *getTypeInfo() const { return PassID; }
90 /// Return true if this PassID implements the specified ID pointer.
91 bool isPassID(const void *IDPtr) const {
92 return PassID == IDPtr;
95 /// isAnalysisGroup - Return true if this is an analysis group, not a normal
98 bool isAnalysisGroup() const { return IsAnalysisGroup; }
99 bool isAnalysis() const { return IsAnalysis; }
101 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
103 bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
105 /// getNormalCtor - Return a pointer to a function, that when called, creates
106 /// an instance of the pass and returns it. This pointer may be null if there
107 /// is no default constructor for the pass.
109 NormalCtor_t getNormalCtor() const {
112 void setNormalCtor(NormalCtor_t Ctor) {
116 /// getTargetMachineCtor - Return a pointer to a function, that when called
117 /// with a TargetMachine, creates an instance of the pass and returns it.
118 /// This pointer may be null if there is no constructor with a TargetMachine
121 TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
122 void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
123 TargetMachineCtor = Ctor;
126 /// createPass() - Use this method to create an instance of this pass.
127 Pass *createPass() const;
129 /// addInterfaceImplemented - This method is called when this pass is
130 /// registered as a member of an analysis group with the RegisterAnalysisGroup
133 void addInterfaceImplemented(const PassInfo *ItfPI) {
134 ItfImpl.push_back(ItfPI);
137 /// getInterfacesImplemented - Return a list of all of the analysis group
138 /// interfaces implemented by this pass.
140 const std::vector<const PassInfo*> &getInterfacesImplemented() const {
145 void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
146 PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
149 #define CALL_ONCE_INITIALIZATION(function) \
150 static volatile sys::cas_flag initialized = 0; \
151 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
152 if (old_val == 0) { \
153 function(Registry); \
154 sys::MemoryFence(); \
155 TsanIgnoreWritesBegin(); \
156 TsanHappensBefore(&initialized); \
158 TsanIgnoreWritesEnd(); \
160 sys::cas_flag tmp = initialized; \
161 sys::MemoryFence(); \
164 sys::MemoryFence(); \
167 TsanHappensAfter(&initialized);
169 #define INITIALIZE_TM_PASS(passName, arg, name, cfg, analysis) \
170 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
171 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
172 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis, \
173 PassInfo::TargetMachineCtor_t(callTargetMachineCtor< passName >)); \
174 Registry.registerPass(*PI, true); \
177 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
178 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
181 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
182 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
183 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
184 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
185 Registry.registerPass(*PI, true); \
188 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
189 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
192 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
193 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
195 #define INITIALIZE_PASS_DEPENDENCY(depName) \
196 initialize##depName##Pass(Registry);
197 #define INITIALIZE_AG_DEPENDENCY(depName) \
198 initialize##depName##AnalysisGroup(Registry);
200 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
201 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
202 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
203 Registry.registerPass(*PI, true); \
206 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
207 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
210 template<typename PassName>
211 Pass *callDefaultCtor() { return new PassName(); }
213 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
214 return new PassName(TM);
217 //===---------------------------------------------------------------------------
218 /// RegisterPass<t> template - This template class is used to notify the system
219 /// that a Pass is available for use, and registers it into the internal
220 /// database maintained by the PassManager. Unless this template is used, opt,
221 /// for example will not be able to see the pass and attempts to create the pass
222 /// will fail. This template is used in the follow manner (at global scope, in
225 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
227 /// This statement will cause your pass to be created by calling the default
228 /// constructor exposed by the pass. If you have a different constructor that
229 /// must be called, create a global constructor function (which takes the
230 /// arguments you need and returns a Pass*) and register your pass like this:
232 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
234 template<typename passName>
235 struct RegisterPass : public PassInfo {
237 // Register Pass using default constructor...
238 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
239 bool is_analysis = false)
240 : PassInfo(Name, PassArg, &passName::ID,
241 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
242 CFGOnly, is_analysis) {
243 PassRegistry::getPassRegistry()->registerPass(*this);
248 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
249 /// Analysis groups are used to define an interface (which need not derive from
250 /// Pass) that is required by passes to do their job. Analysis Groups differ
251 /// from normal analyses because any available implementation of the group will
252 /// be used if it is available.
254 /// If no analysis implementing the interface is available, a default
255 /// implementation is created and added. A pass registers itself as the default
256 /// implementation by specifying 'true' as the second template argument of this
259 /// In addition to registering itself as an analysis group member, a pass must
260 /// register itself normally as well. Passes may be members of multiple groups
261 /// and may still be "required" specifically by name.
263 /// The actual interface may also be registered as well (by not specifying the
264 /// second template argument). The interface should be registered to associate
265 /// a nice name with the interface.
267 class RegisterAGBase : public PassInfo {
269 RegisterAGBase(const char *Name,
270 const void *InterfaceID,
271 const void *PassID = nullptr,
272 bool isDefault = false);
275 template<typename Interface, bool Default = false>
276 struct RegisterAnalysisGroup : public RegisterAGBase {
277 explicit RegisterAnalysisGroup(PassInfo &RPB)
278 : RegisterAGBase(RPB.getPassName(),
279 &Interface::ID, RPB.getTypeInfo(),
283 explicit RegisterAnalysisGroup(const char *Name)
284 : RegisterAGBase(Name, &Interface::ID) {
288 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
289 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
290 initialize##defaultPass##Pass(Registry); \
291 PassInfo *AI = new PassInfo(name, & agName :: ID); \
292 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
295 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
296 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
300 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
301 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
302 if (!def) initialize##agName##AnalysisGroup(Registry); \
303 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
304 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
305 Registry.registerPass(*PI, true); \
307 PassInfo *AI = new PassInfo(name, & agName :: ID); \
308 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
312 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
313 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
317 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
318 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
319 if (!def) initialize##agName##AnalysisGroup(Registry);
321 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
322 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
323 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
324 Registry.registerPass(*PI, true); \
326 PassInfo *AI = new PassInfo(n, & agName :: ID); \
327 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
331 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
332 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
335 //===---------------------------------------------------------------------------
336 /// PassRegistrationListener class - This class is meant to be derived from by
337 /// clients that are interested in which passes get registered and unregistered
338 /// at runtime (which can be because of the RegisterPass constructors being run
339 /// as the program starts up, or may be because a shared object just got
340 /// loaded). Deriving from the PassRegistrationListener class automatically
341 /// registers your object to receive callbacks indicating when passes are loaded
344 struct PassRegistrationListener {
346 /// PassRegistrationListener ctor - Add the current object to the list of
347 /// PassRegistrationListeners...
348 PassRegistrationListener();
350 /// dtor - Remove object from list of listeners...
352 virtual ~PassRegistrationListener();
354 /// Callback functions - These functions are invoked whenever a pass is loaded
355 /// or removed from the current executable.
357 virtual void passRegistered(const PassInfo *) {}
359 /// enumeratePasses - Iterate over the registered passes, calling the
360 /// passEnumerate callback on each PassInfo object.
362 void enumeratePasses();
364 /// passEnumerate - Callback function invoked when someone calls
365 /// enumeratePasses on this PassRegistrationListener object.
367 virtual void passEnumerate(const PassInfo *) {}
371 } // End llvm namespace