]>
git.proxmox.com Git - rustc.git/blob - src/llvm/include/llvm/PassSupport.h
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"
33 //===---------------------------------------------------------------------------
34 /// PassInfo class - An instance of this class exists for every pass known by
35 /// the system, and can be obtained from a live Pass by calling its
36 /// getPassInfo() method. These objects are set up by the RegisterPass<>
37 /// template, defined below.
41 typedef Pass
* (*NormalCtor_t
)();
44 const char *const PassName
; // Nice name for Pass
45 const char *const PassArgument
; // Command Line argument to run this pass
47 const bool IsCFGOnlyPass
; // Pass only looks at the CFG.
48 const bool IsAnalysis
; // True if an analysis pass.
49 const bool IsAnalysisGroup
; // True if an analysis group.
50 std::vector
<const PassInfo
*> ItfImpl
;// Interfaces implemented by this pass
52 NormalCtor_t NormalCtor
;
55 /// PassInfo ctor - Do not call this directly, this should only be invoked
56 /// through RegisterPass.
57 PassInfo(const char *name
, const char *arg
, const void *pi
,
58 NormalCtor_t normal
, bool isCFGOnly
, bool is_analysis
)
59 : PassName(name
), PassArgument(arg
), PassID(pi
),
60 IsCFGOnlyPass(isCFGOnly
),
61 IsAnalysis(is_analysis
), IsAnalysisGroup(false), NormalCtor(normal
) { }
62 /// PassInfo ctor - Do not call this directly, this should only be invoked
63 /// through RegisterPass. This version is for use by analysis groups; it
64 /// does not auto-register the pass.
65 PassInfo(const char *name
, const void *pi
)
66 : PassName(name
), PassArgument(""), PassID(pi
),
68 IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { }
70 /// getPassName - Return the friendly name for the pass, never returns null
72 const char *getPassName() const { return PassName
; }
74 /// getPassArgument - Return the command line option that may be passed to
75 /// 'opt' that will cause this pass to be run. This will return null if there
78 const char *getPassArgument() const { return PassArgument
; }
80 /// getTypeInfo - Return the id object for the pass...
82 const void *getTypeInfo() const { return PassID
; }
84 /// Return true if this PassID implements the specified ID pointer.
85 bool isPassID(const void *IDPtr
) const {
86 return PassID
== IDPtr
;
89 /// isAnalysisGroup - Return true if this is an analysis group, not a normal
92 bool isAnalysisGroup() const { return IsAnalysisGroup
; }
93 bool isAnalysis() const { return IsAnalysis
; }
95 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
97 bool isCFGOnlyPass() const { return IsCFGOnlyPass
; }
99 /// getNormalCtor - Return a pointer to a function, that when called, creates
100 /// an instance of the pass and returns it. This pointer may be null if there
101 /// is no default constructor for the pass.
103 NormalCtor_t
getNormalCtor() const {
106 void setNormalCtor(NormalCtor_t Ctor
) {
110 /// createPass() - Use this method to create an instance of this pass.
111 Pass
*createPass() const;
113 /// addInterfaceImplemented - This method is called when this pass is
114 /// registered as a member of an analysis group with the RegisterAnalysisGroup
117 void addInterfaceImplemented(const PassInfo
*ItfPI
) {
118 ItfImpl
.push_back(ItfPI
);
121 /// getInterfacesImplemented - Return a list of all of the analysis group
122 /// interfaces implemented by this pass.
124 const std::vector
<const PassInfo
*> &getInterfacesImplemented() const {
129 void operator=(const PassInfo
&) LLVM_DELETED_FUNCTION
;
130 PassInfo(const PassInfo
&) LLVM_DELETED_FUNCTION
;
133 #define CALL_ONCE_INITIALIZATION(function) \
134 static volatile sys::cas_flag initialized = 0; \
135 sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
136 if (old_val == 0) { \
137 function(Registry); \
138 sys::MemoryFence(); \
139 TsanIgnoreWritesBegin(); \
140 TsanHappensBefore(&initialized); \
142 TsanIgnoreWritesEnd(); \
144 sys::cas_flag tmp = initialized; \
145 sys::MemoryFence(); \
148 sys::MemoryFence(); \
151 TsanHappensAfter(&initialized);
153 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
154 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
155 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
156 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
157 Registry.registerPass(*PI, true); \
160 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
161 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
164 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
165 static void* initialize##passName##PassOnce(PassRegistry &Registry) {
167 #define INITIALIZE_PASS_DEPENDENCY(depName) \
168 initialize##depName##Pass(Registry);
169 #define INITIALIZE_AG_DEPENDENCY(depName) \
170 initialize##depName##AnalysisGroup(Registry);
172 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
173 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
174 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
175 Registry.registerPass(*PI, true); \
178 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
179 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
182 template<typename PassName
>
183 Pass
*callDefaultCtor() { return new PassName(); }
185 //===---------------------------------------------------------------------------
186 /// RegisterPass<t> template - This template class is used to notify the system
187 /// that a Pass is available for use, and registers it into the internal
188 /// database maintained by the PassManager. Unless this template is used, opt,
189 /// for example will not be able to see the pass and attempts to create the pass
190 /// will fail. This template is used in the follow manner (at global scope, in
193 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
195 /// This statement will cause your pass to be created by calling the default
196 /// constructor exposed by the pass. If you have a different constructor that
197 /// must be called, create a global constructor function (which takes the
198 /// arguments you need and returns a Pass*) and register your pass like this:
200 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
202 template<typename passName
>
203 struct RegisterPass
: public PassInfo
{
205 // Register Pass using default constructor...
206 RegisterPass(const char *PassArg
, const char *Name
, bool CFGOnly
= false,
207 bool is_analysis
= false)
208 : PassInfo(Name
, PassArg
, &passName::ID
,
209 PassInfo::NormalCtor_t(callDefaultCtor
<passName
>),
210 CFGOnly
, is_analysis
) {
211 PassRegistry::getPassRegistry()->registerPass(*this);
216 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
217 /// Analysis groups are used to define an interface (which need not derive from
218 /// Pass) that is required by passes to do their job. Analysis Groups differ
219 /// from normal analyses because any available implementation of the group will
220 /// be used if it is available.
222 /// If no analysis implementing the interface is available, a default
223 /// implementation is created and added. A pass registers itself as the default
224 /// implementation by specifying 'true' as the second template argument of this
227 /// In addition to registering itself as an analysis group member, a pass must
228 /// register itself normally as well. Passes may be members of multiple groups
229 /// and may still be "required" specifically by name.
231 /// The actual interface may also be registered as well (by not specifying the
232 /// second template argument). The interface should be registered to associate
233 /// a nice name with the interface.
235 class RegisterAGBase
: public PassInfo
{
237 RegisterAGBase(const char *Name
,
238 const void *InterfaceID
,
239 const void *PassID
= 0,
240 bool isDefault
= false);
243 template<typename Interface
, bool Default
= false>
244 struct RegisterAnalysisGroup
: public RegisterAGBase
{
245 explicit RegisterAnalysisGroup(PassInfo
&RPB
)
246 : RegisterAGBase(RPB
.getPassName(),
247 &Interface::ID
, RPB
.getTypeInfo(),
251 explicit RegisterAnalysisGroup(const char *Name
)
252 : RegisterAGBase(Name
, &Interface::ID
) {
256 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
257 static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
258 initialize##defaultPass##Pass(Registry); \
259 PassInfo *AI = new PassInfo(name, & agName :: ID); \
260 Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
263 void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
264 CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
268 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
269 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
270 if (!def) initialize##agName##AnalysisGroup(Registry); \
271 PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
272 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
273 Registry.registerPass(*PI, true); \
275 PassInfo *AI = new PassInfo(name, & agName :: ID); \
276 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
280 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
281 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
285 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
286 static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
287 if (!def) initialize##agName##AnalysisGroup(Registry);
289 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
290 PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
291 PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
292 Registry.registerPass(*PI, true); \
294 PassInfo *AI = new PassInfo(n, & agName :: ID); \
295 Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
299 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
300 CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
303 //===---------------------------------------------------------------------------
304 /// PassRegistrationListener class - This class is meant to be derived from by
305 /// clients that are interested in which passes get registered and unregistered
306 /// at runtime (which can be because of the RegisterPass constructors being run
307 /// as the program starts up, or may be because a shared object just got
308 /// loaded). Deriving from the PassRegistrationListener class automatically
309 /// registers your object to receive callbacks indicating when passes are loaded
312 struct PassRegistrationListener
{
314 /// PassRegistrationListener ctor - Add the current object to the list of
315 /// PassRegistrationListeners...
316 PassRegistrationListener();
318 /// dtor - Remove object from list of listeners...
320 virtual ~PassRegistrationListener();
322 /// Callback functions - These functions are invoked whenever a pass is loaded
323 /// or removed from the current executable.
325 virtual void passRegistered(const PassInfo
*) {}
327 /// enumeratePasses - Iterate over the registered passes, calling the
328 /// passEnumerate callback on each PassInfo object.
330 void enumeratePasses();
332 /// passEnumerate - Callback function invoked when someone calls
333 /// enumeratePasses on this PassRegistrationListener object.
335 virtual void passEnumerate(const PassInfo
*) {}
339 } // End llvm namespace