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"
35 //===---------------------------------------------------------------------------
36 /// PassInfo class - An instance of this class exists for every pass known by
37 /// the system, and can be obtained from a live Pass by calling its
38 /// getPassInfo() method. These objects are set up by the RegisterPass<>
39 /// template, defined below.
43 typedef Pass* (*NormalCtor_t)();
44 typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
47 const char *const PassName; // Nice name for Pass
48 const char *const PassArgument; // Command Line argument to run this pass
50 const bool IsCFGOnlyPass; // Pass only looks at the CFG.
51 const bool IsAnalysis; // True if an analysis pass.
52 const bool IsAnalysisGroup; // True if an analysis group.
53 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
55 NormalCtor_t NormalCtor;
56 TargetMachineCtor_t TargetMachineCtor;
59 /// PassInfo ctor - Do not call this directly, this should only be invoked
60 /// through RegisterPass.
61 PassInfo(const char *name, const char *arg, const void *pi,
62 NormalCtor_t normal, bool isCFGOnly, bool is_analysis,
63 TargetMachineCtor_t machine = nullptr)
64 : PassName(name), PassArgument(arg), PassID(pi),
65 IsCFGOnlyPass(isCFGOnly),
66 IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal),
67 TargetMachineCtor(machine) {}
68 /// PassInfo ctor - Do not call this directly, this should only be invoked
69 /// through RegisterPass. This version is for use by analysis groups; it
70 /// does not auto-register the pass.
71 PassInfo(const char *name, const void *pi)
72 : PassName(name), PassArgument(""), PassID(pi),
74 IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
75 TargetMachineCtor(nullptr) {}
77 /// getPassName - Return the friendly name for the pass, never returns null
79 const char *getPassName() const { return PassName; }
81 /// getPassArgument - Return the command line option that may be passed to
82 /// 'opt' that will cause this pass to be run. This will return null if there
85 const char *getPassArgument() const { return PassArgument; }
87 /// getTypeInfo - Return the id object for the pass...
89 const void *getTypeInfo() const { return PassID; }
91 /// Return true if this PassID implements the specified ID pointer.
92 bool isPassID(const void *IDPtr) const {
93 return PassID == IDPtr;
96 /// isAnalysisGroup - Return true if this is an analysis group, not a normal
99 bool isAnalysisGroup() const { return IsAnalysisGroup; }
100 bool isAnalysis() const { return IsAnalysis; }
102 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
104 bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
106 /// getNormalCtor - Return a pointer to a function, that when called, creates
107 /// an instance of the pass and returns it. This pointer may be null if there
108 /// is no default constructor for the pass.
110 NormalCtor_t getNormalCtor() const {
113 void setNormalCtor(NormalCtor_t Ctor) {
117 /// getTargetMachineCtor - Return a pointer to a function, that when called
118 /// with a TargetMachine, creates an instance of the pass and returns it.
119 /// This pointer may be null if there is no constructor with a TargetMachine
122 TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
123 void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
124 TargetMachineCtor = Ctor;
127 /// createPass() - Use this method to create an instance of this pass.
128 Pass *createPass() const;
130 /// addInterfaceImplemented - This method is called when this pass is
131 /// registered as a member of an analysis group with the RegisterAnalysisGroup
134 void addInterfaceImplemented(const PassInfo *ItfPI) {
135 ItfImpl.push_back(ItfPI);
138 /// getInterfacesImplemented - Return a list of all of the analysis group
139 /// interfaces implemented by this pass.
141 const std::vector<const PassInfo*> &getInterfacesImplemented() const {
146 void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
147 PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
150 #define CALL_ONCE_INITIALIZATION(function) \
151 static volatile sys::cas_flag initialized = 0; \
152 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
153 if (old_val == 0) { \
154 function(Registry); \
155 sys::MemoryFence(); \
156 TsanIgnoreWritesBegin(); \
157 TsanHappensBefore(&initialized); \
159 TsanIgnoreWritesEnd(); \
161 sys::cas_flag tmp = initialized; \
162 sys::MemoryFence(); \
165 sys::MemoryFence(); \
168 TsanHappensAfter(&initialized);
170 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
171 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
172 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
173 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
174 Registry.registerPass(*PI, true); \
177 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
178 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
181 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
182 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
184 #define INITIALIZE_PASS_DEPENDENCY(depName) \
185 initialize##depName##Pass(Registry);
186 #define INITIALIZE_AG_DEPENDENCY(depName) \
187 initialize##depName##AnalysisGroup(Registry);
189 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
190 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
191 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
192 Registry.registerPass(*PI, true); \
195 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
196 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
199 template<typename PassName>
200 Pass *callDefaultCtor() { return new PassName(); }
202 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
203 return new PassName(TM);
206 //===---------------------------------------------------------------------------
207 /// RegisterPass<t> template - This template class is used to notify the system
208 /// that a Pass is available for use, and registers it into the internal
209 /// database maintained by the PassManager. Unless this template is used, opt,
210 /// for example will not be able to see the pass and attempts to create the pass
211 /// will fail. This template is used in the follow manner (at global scope, in
214 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
216 /// This statement will cause your pass to be created by calling the default
217 /// constructor exposed by the pass. If you have a different constructor that
218 /// must be called, create a global constructor function (which takes the
219 /// arguments you need and returns a Pass*) and register your pass like this:
221 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
223 template<typename passName>
224 struct RegisterPass : public PassInfo {
226 // Register Pass using default constructor...
227 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
228 bool is_analysis = false)
229 : PassInfo(Name, PassArg, &passName::ID,
230 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
231 CFGOnly, is_analysis) {
232 PassRegistry::getPassRegistry()->registerPass(*this);
237 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
238 /// Analysis groups are used to define an interface (which need not derive from
239 /// Pass) that is required by passes to do their job. Analysis Groups differ
240 /// from normal analyses because any available implementation of the group will
241 /// be used if it is available.
243 /// If no analysis implementing the interface is available, a default
244 /// implementation is created and added. A pass registers itself as the default
245 /// implementation by specifying 'true' as the second template argument of this
248 /// In addition to registering itself as an analysis group member, a pass must
249 /// register itself normally as well. Passes may be members of multiple groups
250 /// and may still be "required" specifically by name.
252 /// The actual interface may also be registered as well (by not specifying the
253 /// second template argument). The interface should be registered to associate
254 /// a nice name with the interface.
256 class RegisterAGBase : public PassInfo {
258 RegisterAGBase(const char *Name,
259 const void *InterfaceID,
260 const void *PassID = nullptr,
261 bool isDefault = false);
264 template<typename Interface, bool Default = false>
265 struct RegisterAnalysisGroup : public RegisterAGBase {
266 explicit RegisterAnalysisGroup(PassInfo &RPB)
267 : RegisterAGBase(RPB.getPassName(),
268 &Interface::ID, RPB.getTypeInfo(),
272 explicit RegisterAnalysisGroup(const char *Name)
273 : RegisterAGBase(Name, &Interface::ID) {
277 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
278 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
279 initialize##defaultPass##Pass(Registry); \
280 PassInfo *AI = new PassInfo(name, & agName :: ID); \
281 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
284 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
285 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
289 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
290 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
291 if (!def) initialize##agName##AnalysisGroup(Registry); \
292 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
293 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
294 Registry.registerPass(*PI, true); \
296 PassInfo *AI = new PassInfo(name, & agName :: ID); \
297 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
301 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
302 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
306 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
307 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
308 if (!def) initialize##agName##AnalysisGroup(Registry);
310 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
311 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
312 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
313 Registry.registerPass(*PI, true); \
315 PassInfo *AI = new PassInfo(n, & agName :: ID); \
316 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
320 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
321 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
324 //===---------------------------------------------------------------------------
325 /// PassRegistrationListener class - This class is meant to be derived from by
326 /// clients that are interested in which passes get registered and unregistered
327 /// at runtime (which can be because of the RegisterPass constructors being run
328 /// as the program starts up, or may be because a shared object just got
329 /// loaded). Deriving from the PassRegistrationListener class automatically
330 /// registers your object to receive callbacks indicating when passes are loaded
333 struct PassRegistrationListener {
335 /// PassRegistrationListener ctor - Add the current object to the list of
336 /// PassRegistrationListeners...
337 PassRegistrationListener();
339 /// dtor - Remove object from list of listeners...
341 virtual ~PassRegistrationListener();
343 /// Callback functions - These functions are invoked whenever a pass is loaded
344 /// or removed from the current executable.
346 virtual void passRegistered(const PassInfo *) {}
348 /// enumeratePasses - Iterate over the registered passes, calling the
349 /// passEnumerate callback on each PassInfo object.
351 void enumeratePasses();
353 /// passEnumerate - Callback function invoked when someone calls
354 /// enumeratePasses on this PassRegistrationListener object.
356 virtual void passEnumerate(const PassInfo *) {}
359 //===---------------------------------------------------------------------------
360 /// PassRunListener class - This class is meant to be derived from by
361 /// clients that are interested in which and when passes are run at runtime.
362 struct PassRunListener {
363 /// PassRunListener ctor - Add the current object to the list of
364 /// PassRunListeners...
365 PassRunListener(LLVMContext *);
367 virtual ~PassRunListener();
369 /// Callback function - This functions is invoked whenever a pass has run.
370 virtual void passRun(LLVMContext *, Pass *, Module *, Function *,
375 } // End llvm namespace