• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===---------------------------------------------------------------------------
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.
38 ///
39 class PassInfo {
40 public:
41   typedef Pass* (*NormalCtor_t)();
42 
43 private:
44   const char      *const PassName;     // Nice name for Pass
45   const char      *const PassArgument; // Command Line argument to run this pass
46   const void *PassID;
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
51 
52   NormalCtor_t NormalCtor;
53 
54 public:
55   /// PassInfo ctor - Do not call this directly, this should only be invoked
56   /// through RegisterPass.
PassInfo(const char * name,const char * arg,const void * pi,NormalCtor_t normal,bool isCFGOnly,bool is_analysis)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.
PassInfo(const char * name,const void * pi)65   PassInfo(const char *name, const void *pi)
66     : PassName(name), PassArgument(""), PassID(pi),
67       IsCFGOnlyPass(false),
68       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { }
69 
70   /// getPassName - Return the friendly name for the pass, never returns null
71   ///
getPassName()72   const char *getPassName() const { return PassName; }
73 
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
76   /// is no argument.
77   ///
getPassArgument()78   const char *getPassArgument() const { return PassArgument; }
79 
80   /// getTypeInfo - Return the id object for the pass...
81   /// TODO : Rename
getTypeInfo()82   const void *getTypeInfo() const { return PassID; }
83 
84   /// Return true if this PassID implements the specified ID pointer.
isPassID(const void * IDPtr)85   bool isPassID(const void *IDPtr) const {
86     return PassID == IDPtr;
87   }
88 
89   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
90   /// pass.
91   ///
isAnalysisGroup()92   bool isAnalysisGroup() const { return IsAnalysisGroup; }
isAnalysis()93   bool isAnalysis() const { return IsAnalysis; }
94 
95   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
96   /// function.
isCFGOnlyPass()97   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
98 
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.
102   ///
getNormalCtor()103   NormalCtor_t getNormalCtor() const {
104     return NormalCtor;
105   }
setNormalCtor(NormalCtor_t Ctor)106   void setNormalCtor(NormalCtor_t Ctor) {
107     NormalCtor = Ctor;
108   }
109 
110   /// createPass() - Use this method to create an instance of this pass.
111   Pass *createPass() const;
112 
113   /// addInterfaceImplemented - This method is called when this pass is
114   /// registered as a member of an analysis group with the RegisterAnalysisGroup
115   /// template.
116   ///
addInterfaceImplemented(const PassInfo * ItfPI)117   void addInterfaceImplemented(const PassInfo *ItfPI) {
118     ItfImpl.push_back(ItfPI);
119   }
120 
121   /// getInterfacesImplemented - Return a list of all of the analysis group
122   /// interfaces implemented by this pass.
123   ///
getInterfacesImplemented()124   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
125     return ItfImpl;
126   }
127 
128 private:
129   void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
130   PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
131 };
132 
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); \
141     initialized = 2; \
142     TsanIgnoreWritesEnd(); \
143   } else { \
144     sys::cas_flag tmp = initialized; \
145     sys::MemoryFence(); \
146     while (tmp != 2) { \
147       tmp = initialized; \
148       sys::MemoryFence(); \
149     } \
150   } \
151   TsanHappensAfter(&initialized);
152 
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); \
158     return PI; \
159   } \
160   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
161     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
162   }
163 
164 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
165   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
166 
167 #define INITIALIZE_PASS_DEPENDENCY(depName) \
168     initialize##depName##Pass(Registry);
169 #define INITIALIZE_AG_DEPENDENCY(depName) \
170     initialize##depName##AnalysisGroup(Registry);
171 
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); \
176     return PI; \
177   } \
178   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
179     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
180   }
181 
182 template<typename PassName>
callDefaultCtor()183 Pass *callDefaultCtor() { return new PassName(); }
184 
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
191 /// your .cpp file):
192 ///
193 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
194 ///
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:
199 ///
200 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
201 ///
202 template<typename passName>
203 struct RegisterPass : public PassInfo {
204 
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);
212   }
213 };
214 
215 
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.
221 ///
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
225 /// class.
226 ///
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.
230 ///
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.
234 ///
235 class RegisterAGBase : public PassInfo {
236 public:
237   RegisterAGBase(const char *Name,
238                  const void *InterfaceID,
239                  const void *PassID = 0,
240                  bool isDefault = false);
241 };
242 
243 template<typename Interface, bool Default = false>
244 struct RegisterAnalysisGroup : public RegisterAGBase {
RegisterAnalysisGroupRegisterAnalysisGroup245   explicit RegisterAnalysisGroup(PassInfo &RPB)
246     : RegisterAGBase(RPB.getPassName(),
247                      &Interface::ID, RPB.getTypeInfo(),
248                      Default) {
249   }
250 
RegisterAnalysisGroupRegisterAnalysisGroup251   explicit RegisterAnalysisGroup(const char *Name)
252     : RegisterAGBase(Name, &Interface::ID) {
253   }
254 };
255 
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); \
261     return AI; \
262   } \
263   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
264     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
265   }
266 
267 
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); \
274     \
275     PassInfo *AI = new PassInfo(name, & agName :: ID); \
276     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
277                                    *AI, def, true); \
278     return AI; \
279   } \
280   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
281     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
282   }
283 
284 
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);
288 
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); \
293     \
294     PassInfo *AI = new PassInfo(n, & agName :: ID); \
295     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
296                                    *AI, def, true); \
297     return AI; \
298   } \
299   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
300     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
301   }
302 
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
310 /// and removed.
311 ///
312 struct PassRegistrationListener {
313 
314   /// PassRegistrationListener ctor - Add the current object to the list of
315   /// PassRegistrationListeners...
316   PassRegistrationListener();
317 
318   /// dtor - Remove object from list of listeners...
319   ///
320   virtual ~PassRegistrationListener();
321 
322   /// Callback functions - These functions are invoked whenever a pass is loaded
323   /// or removed from the current executable.
324   ///
passRegisteredPassRegistrationListener325   virtual void passRegistered(const PassInfo *) {}
326 
327   /// enumeratePasses - Iterate over the registered passes, calling the
328   /// passEnumerate callback on each PassInfo object.
329   ///
330   void enumeratePasses();
331 
332   /// passEnumerate - Callback function invoked when someone calls
333   /// enumeratePasses on this PassRegistrationListener object.
334   ///
passEnumeratePassRegistrationListener335   virtual void passEnumerate(const PassInfo *) {}
336 };
337 
338 
339 } // End llvm namespace
340 
341 #endif
342