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