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/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 = NULL)
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(0),
74 TargetMachineCtor(0) {}
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 std::atomic<int> initialized; \
152 if (initialized.compare_exchange_strong(old_val, 1)) { \
153 function(Registry); \
154 std::atomic_thread_fence(std::memory_order_seq_cst); \
155 TsanIgnoreWritesBegin(); \
156 TsanHappensBefore(&initialized); \
158 TsanIgnoreWritesEnd(); \
160 int tmp = initialized.load(); \
161 std::atomic_thread_fence(std::memory_order_seq_cst); \
163 tmp = initialized.load(); \
164 std::atomic_thread_fence(std::memory_order_seq_cst); \
167 TsanHappensAfter(&initialized);
169 #define INITIALIZE_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 Registry.registerPass(*PI, true); \
176 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
177 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
180 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
181 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
183 #define INITIALIZE_PASS_DEPENDENCY(depName) \
184 initialize##depName##Pass(Registry);
185 #define INITIALIZE_AG_DEPENDENCY(depName) \
186 initialize##depName##AnalysisGroup(Registry);
188 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
189 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
190 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
191 Registry.registerPass(*PI, true); \
194 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
195 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
198 template<typename PassName>
199 Pass *callDefaultCtor() { return new PassName(); }
201 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
202 return new PassName(TM);
205 //===---------------------------------------------------------------------------
206 /// RegisterPass<t> template - This template class is used to notify the system
207 /// that a Pass is available for use, and registers it into the internal
208 /// database maintained by the PassManager. Unless this template is used, opt,
209 /// for example will not be able to see the pass and attempts to create the pass
210 /// will fail. This template is used in the follow manner (at global scope, in
213 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
215 /// This statement will cause your pass to be created by calling the default
216 /// constructor exposed by the pass. If you have a different constructor that
217 /// must be called, create a global constructor function (which takes the
218 /// arguments you need and returns a Pass*) and register your pass like this:
220 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
222 template<typename passName>
223 struct RegisterPass : public PassInfo {
225 // Register Pass using default constructor...
226 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
227 bool is_analysis = false)
228 : PassInfo(Name, PassArg, &passName::ID,
229 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
230 CFGOnly, is_analysis) {
231 PassRegistry::getPassRegistry()->registerPass(*this);
236 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
237 /// Analysis groups are used to define an interface (which need not derive from
238 /// Pass) that is required by passes to do their job. Analysis Groups differ
239 /// from normal analyses because any available implementation of the group will
240 /// be used if it is available.
242 /// If no analysis implementing the interface is available, a default
243 /// implementation is created and added. A pass registers itself as the default
244 /// implementation by specifying 'true' as the second template argument of this
247 /// In addition to registering itself as an analysis group member, a pass must
248 /// register itself normally as well. Passes may be members of multiple groups
249 /// and may still be "required" specifically by name.
251 /// The actual interface may also be registered as well (by not specifying the
252 /// second template argument). The interface should be registered to associate
253 /// a nice name with the interface.
255 class RegisterAGBase : public PassInfo {
257 RegisterAGBase(const char *Name,
258 const void *InterfaceID,
259 const void *PassID = 0,
260 bool isDefault = false);
263 template<typename Interface, bool Default = false>
264 struct RegisterAnalysisGroup : public RegisterAGBase {
265 explicit RegisterAnalysisGroup(PassInfo &RPB)
266 : RegisterAGBase(RPB.getPassName(),
267 &Interface::ID, RPB.getTypeInfo(),
271 explicit RegisterAnalysisGroup(const char *Name)
272 : RegisterAGBase(Name, &Interface::ID) {
276 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
277 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
278 initialize##defaultPass##Pass(Registry); \
279 PassInfo *AI = new PassInfo(name, & agName :: ID); \
280 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
283 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
284 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
288 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
289 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
290 if (!def) initialize##agName##AnalysisGroup(Registry); \
291 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
292 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
293 Registry.registerPass(*PI, true); \
295 PassInfo *AI = new PassInfo(name, & agName :: ID); \
296 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
300 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
301 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
305 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
306 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
307 if (!def) initialize##agName##AnalysisGroup(Registry);
309 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
310 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
311 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
312 Registry.registerPass(*PI, true); \
314 PassInfo *AI = new PassInfo(n, & agName :: ID); \
315 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
319 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
320 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
323 //===---------------------------------------------------------------------------
324 /// PassRegistrationListener class - This class is meant to be derived from by
325 /// clients that are interested in which passes get registered and unregistered
326 /// at runtime (which can be because of the RegisterPass constructors being run
327 /// as the program starts up, or may be because a shared object just got
328 /// loaded). Deriving from the PassRegistrationListener class automatically
329 /// registers your object to receive callbacks indicating when passes are loaded
332 struct PassRegistrationListener {
334 /// PassRegistrationListener ctor - Add the current object to the list of
335 /// PassRegistrationListeners...
336 PassRegistrationListener();
338 /// dtor - Remove object from list of listeners...
340 virtual ~PassRegistrationListener();
342 /// Callback functions - These functions are invoked whenever a pass is loaded
343 /// or removed from the current executable.
345 virtual void passRegistered(const PassInfo *) {}
347 /// enumeratePasses - Iterate over the registered passes, calling the
348 /// passEnumerate callback on each PassInfo object.
350 void enumeratePasses();
352 /// passEnumerate - Callback function invoked when someone calls
353 /// enumeratePasses on this PassRegistrationListener object.
355 virtual void passEnumerate(const PassInfo *) {}
359 } // End llvm namespace