Do not register and de-register PassRegistrationListeners during
[oota-llvm.git] / include / llvm / PassSupport.h
1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines stuff that is used to define and "use" Passes.  This file
11 // is automatically #included by Pass.h, so:
12 //
13 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 //
15 // Instead, #include Pass.h.
16 //
17 // This file defines Pass registration code and classes used for it.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_PASSSUPPORT_H
22 #define LLVM_PASSSUPPORT_H
23
24 #include "Pass.h"
25 #include "llvm/InitializePasses.h"
26 #include "llvm/PassRegistry.h"
27 #include "llvm/Support/Atomic.h"
28 #include "llvm/Support/Valgrind.h"
29 #include <vector>
30
31 namespace llvm {
32
33 class TargetMachine;
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.
39 ///
40 class PassInfo {
41 public:
42   typedef Pass* (*NormalCtor_t)();
43   typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
44
45 private:
46   const char      *const PassName;     // Nice name for Pass
47   const char      *const PassArgument; // Command Line argument to run this pass
48   const void *PassID;      
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
53
54   NormalCtor_t NormalCtor;
55   TargetMachineCtor_t TargetMachineCtor;
56
57 public:
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), 
72       IsCFGOnlyPass(false), 
73       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
74       TargetMachineCtor(nullptr) {}
75
76   /// getPassName - Return the friendly name for the pass, never returns null
77   ///
78   const char *getPassName() const { return PassName; }
79
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
82   /// is no argument.
83   ///
84   const char *getPassArgument() const { return PassArgument; }
85
86   /// getTypeInfo - Return the id object for the pass...
87   /// TODO : Rename
88   const void *getTypeInfo() const { return PassID; }
89
90   /// Return true if this PassID implements the specified ID pointer.
91   bool isPassID(const void *IDPtr) const {
92     return PassID == IDPtr;
93   }
94   
95   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
96   /// pass.
97   ///
98   bool isAnalysisGroup() const { return IsAnalysisGroup; }
99   bool isAnalysis() const { return IsAnalysis; }
100
101   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
102   /// function.
103   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
104   
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.
108   ///
109   NormalCtor_t getNormalCtor() const {
110     return NormalCtor;
111   }
112   void setNormalCtor(NormalCtor_t Ctor) {
113     NormalCtor = Ctor;
114   }
115
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
119   /// for the pass.
120   ///
121   TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
122   void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
123     TargetMachineCtor = Ctor;
124   }
125
126   /// createPass() - Use this method to create an instance of this pass.
127   Pass *createPass() const;
128
129   /// addInterfaceImplemented - This method is called when this pass is
130   /// registered as a member of an analysis group with the RegisterAnalysisGroup
131   /// template.
132   ///
133   void addInterfaceImplemented(const PassInfo *ItfPI) {
134     ItfImpl.push_back(ItfPI);
135   }
136
137   /// getInterfacesImplemented - Return a list of all of the analysis group
138   /// interfaces implemented by this pass.
139   ///
140   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
141     return ItfImpl;
142   }
143
144 private:
145   void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
146   PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
147 };
148
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); \
157     initialized = 2; \
158     TsanIgnoreWritesEnd(); \
159   } else { \
160     sys::cas_flag tmp = initialized; \
161     sys::MemoryFence(); \
162     while (tmp != 2) { \
163       tmp = initialized; \
164       sys::MemoryFence(); \
165     } \
166   } \
167   TsanHappensAfter(&initialized);
168
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); \
175     return PI; \
176   } \
177   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
178     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
179   }
180
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); \
186     return PI; \
187   } \
188   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
189     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
190   }
191
192 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
193   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
194
195 #define INITIALIZE_PASS_DEPENDENCY(depName) \
196     initialize##depName##Pass(Registry);
197 #define INITIALIZE_AG_DEPENDENCY(depName) \
198     initialize##depName##AnalysisGroup(Registry);
199
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); \
204     return PI; \
205   } \
206   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
207     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
208   }
209
210 template<typename PassName>
211 Pass *callDefaultCtor() { return new PassName(); }
212
213 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
214   return new PassName(TM);
215 }
216
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
223 /// your .cpp file):
224 ///
225 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
226 ///
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:
231 ///
232 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
233 ///
234 template<typename passName>
235 struct RegisterPass : public PassInfo {
236
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);
244   }
245 };
246
247
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.
253 ///
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
257 /// class.
258 ///
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.
262 ///
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.
266 ///
267 class RegisterAGBase : public PassInfo {
268 public:
269   RegisterAGBase(const char *Name,
270                  const void *InterfaceID,
271                  const void *PassID = nullptr,
272                  bool isDefault = false);
273 };
274
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(),
280                      Default) {
281   }
282
283   explicit RegisterAnalysisGroup(const char *Name)
284     : RegisterAGBase(Name, &Interface::ID) {
285   }
286 };
287
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); \
293     return AI; \
294   } \
295   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
296     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
297   }
298
299
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); \
306     \
307     PassInfo *AI = new PassInfo(name, & agName :: ID); \
308     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
309                                    *AI, def, true); \
310     return AI; \
311   } \
312   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
313     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
314   }
315
316
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);
320
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); \
325     \
326     PassInfo *AI = new PassInfo(n, & agName :: ID); \
327     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
328                                    *AI, def, true); \
329     return AI; \
330   } \
331   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
332     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
333   }
334
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).
341 ///
342 struct PassRegistrationListener {
343
344   PassRegistrationListener() {}
345   virtual ~PassRegistrationListener() {}
346
347   /// Callback functions - These functions are invoked whenever a pass is loaded
348   /// or removed from the current executable.
349   ///
350   virtual void passRegistered(const PassInfo *) {}
351
352   /// enumeratePasses - Iterate over the registered passes, calling the
353   /// passEnumerate callback on each PassInfo object.
354   ///
355   void enumeratePasses();
356
357   /// passEnumerate - Callback function invoked when someone calls
358   /// enumeratePasses on this PassRegistrationListener object.
359   ///
360   virtual void passEnumerate(const PassInfo *) {}
361 };
362
363
364 } // End llvm namespace
365
366 #endif