a4ef09d679a30401f9e5d0d9cf148f4579e4c213
[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/PassInfo.h"
27 #include "llvm/PassRegistry.h"
28 #include "llvm/Support/Atomic.h"
29 #include "llvm/Support/Valgrind.h"
30 #include <vector>
31
32 namespace llvm {
33
34 class TargetMachine;
35
36 #define CALL_ONCE_INITIALIZATION(function) \
37   static volatile sys::cas_flag initialized = 0; \
38   sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
39   if (old_val == 0) { \
40     function(Registry); \
41     sys::MemoryFence(); \
42     TsanIgnoreWritesBegin(); \
43     TsanHappensBefore(&initialized); \
44     initialized = 2; \
45     TsanIgnoreWritesEnd(); \
46   } else { \
47     sys::cas_flag tmp = initialized; \
48     sys::MemoryFence(); \
49     while (tmp != 2) { \
50       tmp = initialized; \
51       sys::MemoryFence(); \
52     } \
53   } \
54   TsanHappensAfter(&initialized);
55
56 #define INITIALIZE_TM_PASS(passName, arg, name, cfg, analysis) \
57   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
58     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
59       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis, \
60       PassInfo::TargetMachineCtor_t(callTargetMachineCtor< passName >)); \
61     Registry.registerPass(*PI, true); \
62     return PI; \
63   } \
64   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
65     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
66   }
67
68 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
69   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
70     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
71       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
72     Registry.registerPass(*PI, true); \
73     return PI; \
74   } \
75   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
76     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
77   }
78
79 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
80   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
81
82 #define INITIALIZE_PASS_DEPENDENCY(depName) \
83     initialize##depName##Pass(Registry);
84 #define INITIALIZE_AG_DEPENDENCY(depName) \
85     initialize##depName##AnalysisGroup(Registry);
86
87 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
88     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
89       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
90     Registry.registerPass(*PI, true); \
91     return PI; \
92   } \
93   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
94     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
95   }
96
97 template<typename PassName>
98 Pass *callDefaultCtor() { return new PassName(); }
99
100 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
101   return new PassName(TM);
102 }
103
104 //===---------------------------------------------------------------------------
105 /// RegisterPass<t> template - This template class is used to notify the system
106 /// that a Pass is available for use, and registers it into the internal
107 /// database maintained by the PassManager.  Unless this template is used, opt,
108 /// for example will not be able to see the pass and attempts to create the pass
109 /// will fail. This template is used in the follow manner (at global scope, in
110 /// your .cpp file):
111 ///
112 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
113 ///
114 /// This statement will cause your pass to be created by calling the default
115 /// constructor exposed by the pass.  If you have a different constructor that
116 /// must be called, create a global constructor function (which takes the
117 /// arguments you need and returns a Pass*) and register your pass like this:
118 ///
119 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
120 ///
121 template<typename passName>
122 struct RegisterPass : public PassInfo {
123
124   // Register Pass using default constructor...
125   RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
126                bool is_analysis = false)
127     : PassInfo(Name, PassArg, &passName::ID,
128                PassInfo::NormalCtor_t(callDefaultCtor<passName>),
129                CFGOnly, is_analysis) {
130     PassRegistry::getPassRegistry()->registerPass(*this);
131   }
132 };
133
134
135 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
136 /// Analysis groups are used to define an interface (which need not derive from
137 /// Pass) that is required by passes to do their job.  Analysis Groups differ
138 /// from normal analyses because any available implementation of the group will
139 /// be used if it is available.
140 ///
141 /// If no analysis implementing the interface is available, a default
142 /// implementation is created and added.  A pass registers itself as the default
143 /// implementation by specifying 'true' as the second template argument of this
144 /// class.
145 ///
146 /// In addition to registering itself as an analysis group member, a pass must
147 /// register itself normally as well.  Passes may be members of multiple groups
148 /// and may still be "required" specifically by name.
149 ///
150 /// The actual interface may also be registered as well (by not specifying the
151 /// second template argument).  The interface should be registered to associate
152 /// a nice name with the interface.
153 ///
154 class RegisterAGBase : public PassInfo {
155 public:
156   RegisterAGBase(const char *Name,
157                  const void *InterfaceID,
158                  const void *PassID = nullptr,
159                  bool isDefault = false);
160 };
161
162 template<typename Interface, bool Default = false>
163 struct RegisterAnalysisGroup : public RegisterAGBase {
164   explicit RegisterAnalysisGroup(PassInfo &RPB)
165     : RegisterAGBase(RPB.getPassName(),
166                      &Interface::ID, RPB.getTypeInfo(),
167                      Default) {
168   }
169
170   explicit RegisterAnalysisGroup(const char *Name)
171     : RegisterAGBase(Name, &Interface::ID) {
172   }
173 };
174
175 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
176   static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
177     initialize##defaultPass##Pass(Registry); \
178     PassInfo *AI = new PassInfo(name, & agName :: ID); \
179     Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
180     return AI; \
181   } \
182   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
183     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
184   }
185
186
187 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
188   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
189     if (!def) initialize##agName##AnalysisGroup(Registry); \
190     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
191       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
192     Registry.registerPass(*PI, true); \
193     \
194     PassInfo *AI = new PassInfo(name, & agName :: ID); \
195     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
196                                    *AI, def, true); \
197     return AI; \
198   } \
199   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
200     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
201   }
202
203
204 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
205   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
206     if (!def) initialize##agName##AnalysisGroup(Registry);
207
208 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
209     PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
210       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
211     Registry.registerPass(*PI, true); \
212     \
213     PassInfo *AI = new PassInfo(n, & agName :: ID); \
214     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
215                                    *AI, def, true); \
216     return AI; \
217   } \
218   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
219     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
220   }
221
222 //===---------------------------------------------------------------------------
223 /// PassRegistrationListener class - This class is meant to be derived from by
224 /// clients that are interested in which passes get registered and unregistered
225 /// at runtime (which can be because of the RegisterPass constructors being run
226 /// as the program starts up, or may be because a shared object just got
227 /// loaded).
228 ///
229 struct PassRegistrationListener {
230
231   PassRegistrationListener() {}
232   virtual ~PassRegistrationListener() {}
233
234   /// Callback functions - These functions are invoked whenever a pass is loaded
235   /// or removed from the current executable.
236   ///
237   virtual void passRegistered(const PassInfo *) {}
238
239   /// enumeratePasses - Iterate over the registered passes, calling the
240   /// passEnumerate callback on each PassInfo object.
241   ///
242   void enumeratePasses();
243
244   /// passEnumerate - Callback function invoked when someone calls
245   /// enumeratePasses on this PassRegistrationListener object.
246   ///
247   virtual void passEnumerate(const PassInfo *) {}
248 };
249
250
251 } // End llvm namespace
252
253 #endif