[PM] Add pass run listeners to the pass manager.
[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 class LLVMContext;
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.
40 ///
41 class PassInfo {
42 public:
43   typedef Pass* (*NormalCtor_t)();
44   typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
45
46 private:
47   const char      *const PassName;     // Nice name for Pass
48   const char      *const PassArgument; // Command Line argument to run this pass
49   const void *PassID;      
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
54
55   NormalCtor_t NormalCtor;
56   TargetMachineCtor_t TargetMachineCtor;
57
58 public:
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), 
73       IsCFGOnlyPass(false), 
74       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
75       TargetMachineCtor(nullptr) {}
76
77   /// getPassName - Return the friendly name for the pass, never returns null
78   ///
79   const char *getPassName() const { return PassName; }
80
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
83   /// is no argument.
84   ///
85   const char *getPassArgument() const { return PassArgument; }
86
87   /// getTypeInfo - Return the id object for the pass...
88   /// TODO : Rename
89   const void *getTypeInfo() const { return PassID; }
90
91   /// Return true if this PassID implements the specified ID pointer.
92   bool isPassID(const void *IDPtr) const {
93     return PassID == IDPtr;
94   }
95   
96   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
97   /// pass.
98   ///
99   bool isAnalysisGroup() const { return IsAnalysisGroup; }
100   bool isAnalysis() const { return IsAnalysis; }
101
102   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
103   /// function.
104   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
105   
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.
109   ///
110   NormalCtor_t getNormalCtor() const {
111     return NormalCtor;
112   }
113   void setNormalCtor(NormalCtor_t Ctor) {
114     NormalCtor = Ctor;
115   }
116
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
120   /// for the pass.
121   ///
122   TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
123   void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
124     TargetMachineCtor = Ctor;
125   }
126
127   /// createPass() - Use this method to create an instance of this pass.
128   Pass *createPass() const;
129
130   /// addInterfaceImplemented - This method is called when this pass is
131   /// registered as a member of an analysis group with the RegisterAnalysisGroup
132   /// template.
133   ///
134   void addInterfaceImplemented(const PassInfo *ItfPI) {
135     ItfImpl.push_back(ItfPI);
136   }
137
138   /// getInterfacesImplemented - Return a list of all of the analysis group
139   /// interfaces implemented by this pass.
140   ///
141   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
142     return ItfImpl;
143   }
144
145 private:
146   void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
147   PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
148 };
149
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); \
158     initialized = 2; \
159     TsanIgnoreWritesEnd(); \
160   } else { \
161     sys::cas_flag tmp = initialized; \
162     sys::MemoryFence(); \
163     while (tmp != 2) { \
164       tmp = initialized; \
165       sys::MemoryFence(); \
166     } \
167   } \
168   TsanHappensAfter(&initialized);
169
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); \
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_BEGIN(passName, arg, name, cfg, analysis) \
182   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
183
184 #define INITIALIZE_PASS_DEPENDENCY(depName) \
185     initialize##depName##Pass(Registry);
186 #define INITIALIZE_AG_DEPENDENCY(depName) \
187     initialize##depName##AnalysisGroup(Registry);
188
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); \
193     return PI; \
194   } \
195   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
196     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
197   }
198
199 template<typename PassName>
200 Pass *callDefaultCtor() { return new PassName(); }
201
202 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
203   return new PassName(TM);
204 }
205
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
212 /// your .cpp file):
213 ///
214 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
215 ///
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:
220 ///
221 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
222 ///
223 template<typename passName>
224 struct RegisterPass : public PassInfo {
225
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);
233   }
234 };
235
236
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.
242 ///
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
246 /// class.
247 ///
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.
251 ///
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.
255 ///
256 class RegisterAGBase : public PassInfo {
257 public:
258   RegisterAGBase(const char *Name,
259                  const void *InterfaceID,
260                  const void *PassID = nullptr,
261                  bool isDefault = false);
262 };
263
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(),
269                      Default) {
270   }
271
272   explicit RegisterAnalysisGroup(const char *Name)
273     : RegisterAGBase(Name, &Interface::ID) {
274   }
275 };
276
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); \
282     return AI; \
283   } \
284   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
285     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
286   }
287
288
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); \
295     \
296     PassInfo *AI = new PassInfo(name, & agName :: ID); \
297     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
298                                    *AI, def, true); \
299     return AI; \
300   } \
301   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
302     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
303   }
304
305
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);
309
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); \
314     \
315     PassInfo *AI = new PassInfo(n, & agName :: ID); \
316     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
317                                    *AI, def, true); \
318     return AI; \
319   } \
320   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
321     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
322   }
323
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
331 /// and removed.
332 ///
333 struct PassRegistrationListener {
334
335   /// PassRegistrationListener ctor - Add the current object to the list of
336   /// PassRegistrationListeners...
337   PassRegistrationListener();
338
339   /// dtor - Remove object from list of listeners...
340   ///
341   virtual ~PassRegistrationListener();
342
343   /// Callback functions - These functions are invoked whenever a pass is loaded
344   /// or removed from the current executable.
345   ///
346   virtual void passRegistered(const PassInfo *) {}
347
348   /// enumeratePasses - Iterate over the registered passes, calling the
349   /// passEnumerate callback on each PassInfo object.
350   ///
351   void enumeratePasses();
352
353   /// passEnumerate - Callback function invoked when someone calls
354   /// enumeratePasses on this PassRegistrationListener object.
355   ///
356   virtual void passEnumerate(const PassInfo *) {}
357 };
358
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 *);
366
367   virtual ~PassRunListener();
368
369   /// Callback function - This functions is invoked whenever a pass has run.
370   virtual void passRun(LLVMContext *, Pass *, Module *, Function *,
371                        BasicBlock *) {}
372 };
373
374
375 } // End llvm namespace
376
377 #endif