PassSupport.h revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
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
31namespace llvm {
32
33class 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///
40class PassInfo {
41public:
42  typedef Pass* (*NormalCtor_t)();
43  typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
44
45private:
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
57public:
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
144private:
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_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    Registry.registerPass(*PI, true); \
174    return PI; \
175  } \
176  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
177    CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
178  }
179
180#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
181  static void* initialize##passName##PassOnce(PassRegistry &Registry) {
182
183#define INITIALIZE_PASS_DEPENDENCY(depName) \
184    initialize##depName##Pass(Registry);
185#define INITIALIZE_AG_DEPENDENCY(depName) \
186    initialize##depName##AnalysisGroup(Registry);
187
188#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
189    PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
190      PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
191    Registry.registerPass(*PI, true); \
192    return PI; \
193  } \
194  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
195    CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
196  }
197
198template<typename PassName>
199Pass *callDefaultCtor() { return new PassName(); }
200
201template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
202  return new PassName(TM);
203}
204
205//===---------------------------------------------------------------------------
206/// RegisterPass<t> template - This template class is used to notify the system
207/// that a Pass is available for use, and registers it into the internal
208/// database maintained by the PassManager.  Unless this template is used, opt,
209/// for example will not be able to see the pass and attempts to create the pass
210/// will fail. This template is used in the follow manner (at global scope, in
211/// your .cpp file):
212///
213/// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
214///
215/// This statement will cause your pass to be created by calling the default
216/// constructor exposed by the pass.  If you have a different constructor that
217/// must be called, create a global constructor function (which takes the
218/// arguments you need and returns a Pass*) and register your pass like this:
219///
220/// static RegisterPass<PassClassName> tmp("passopt", "My Name");
221///
222template<typename passName>
223struct RegisterPass : public PassInfo {
224
225  // Register Pass using default constructor...
226  RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
227               bool is_analysis = false)
228    : PassInfo(Name, PassArg, &passName::ID,
229               PassInfo::NormalCtor_t(callDefaultCtor<passName>),
230               CFGOnly, is_analysis) {
231    PassRegistry::getPassRegistry()->registerPass(*this);
232  }
233};
234
235
236/// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
237/// Analysis groups are used to define an interface (which need not derive from
238/// Pass) that is required by passes to do their job.  Analysis Groups differ
239/// from normal analyses because any available implementation of the group will
240/// be used if it is available.
241///
242/// If no analysis implementing the interface is available, a default
243/// implementation is created and added.  A pass registers itself as the default
244/// implementation by specifying 'true' as the second template argument of this
245/// class.
246///
247/// In addition to registering itself as an analysis group member, a pass must
248/// register itself normally as well.  Passes may be members of multiple groups
249/// and may still be "required" specifically by name.
250///
251/// The actual interface may also be registered as well (by not specifying the
252/// second template argument).  The interface should be registered to associate
253/// a nice name with the interface.
254///
255class RegisterAGBase : public PassInfo {
256public:
257  RegisterAGBase(const char *Name,
258                 const void *InterfaceID,
259                 const void *PassID = nullptr,
260                 bool isDefault = false);
261};
262
263template<typename Interface, bool Default = false>
264struct RegisterAnalysisGroup : public RegisterAGBase {
265  explicit RegisterAnalysisGroup(PassInfo &RPB)
266    : RegisterAGBase(RPB.getPassName(),
267                     &Interface::ID, RPB.getTypeInfo(),
268                     Default) {
269  }
270
271  explicit RegisterAnalysisGroup(const char *Name)
272    : RegisterAGBase(Name, &Interface::ID) {
273  }
274};
275
276#define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
277  static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
278    initialize##defaultPass##Pass(Registry); \
279    PassInfo *AI = new PassInfo(name, & agName :: ID); \
280    Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
281    return AI; \
282  } \
283  void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
284    CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
285  }
286
287
288#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
289  static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
290    if (!def) initialize##agName##AnalysisGroup(Registry); \
291    PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
292      PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
293    Registry.registerPass(*PI, true); \
294    \
295    PassInfo *AI = new PassInfo(name, & agName :: ID); \
296    Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
297                                   *AI, def, true); \
298    return AI; \
299  } \
300  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
301    CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
302  }
303
304
305#define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
306  static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
307    if (!def) initialize##agName##AnalysisGroup(Registry);
308
309#define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
310    PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
311      PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
312    Registry.registerPass(*PI, true); \
313    \
314    PassInfo *AI = new PassInfo(n, & agName :: ID); \
315    Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
316                                   *AI, def, true); \
317    return AI; \
318  } \
319  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
320    CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
321  }
322
323//===---------------------------------------------------------------------------
324/// PassRegistrationListener class - This class is meant to be derived from by
325/// clients that are interested in which passes get registered and unregistered
326/// at runtime (which can be because of the RegisterPass constructors being run
327/// as the program starts up, or may be because a shared object just got
328/// loaded).  Deriving from the PassRegistrationListener class automatically
329/// registers your object to receive callbacks indicating when passes are loaded
330/// and removed.
331///
332struct PassRegistrationListener {
333
334  /// PassRegistrationListener ctor - Add the current object to the list of
335  /// PassRegistrationListeners...
336  PassRegistrationListener();
337
338  /// dtor - Remove object from list of listeners...
339  ///
340  virtual ~PassRegistrationListener();
341
342  /// Callback functions - These functions are invoked whenever a pass is loaded
343  /// or removed from the current executable.
344  ///
345  virtual void passRegistered(const PassInfo *) {}
346
347  /// enumeratePasses - Iterate over the registered passes, calling the
348  /// passEnumerate callback on each PassInfo object.
349  ///
350  void enumeratePasses();
351
352  /// passEnumerate - Callback function invoked when someone calls
353  /// enumeratePasses on this PassRegistrationListener object.
354  ///
355  virtual void passEnumerate(const PassInfo *) {}
356};
357
358
359} // End llvm namespace
360
361#endif
362