LLVM  6.0.0svn
PassSupport.h
Go to the documentation of this file.
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/Threading.h"
30 #include <functional>
31 
32 namespace llvm {
33 
34 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
35  static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
36  PassInfo *PI = new PassInfo( \
37  name, arg, &passName::ID, \
38  PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
39  Registry.registerPass(*PI, true); \
40  return PI; \
41  } \
42  static llvm::once_flag Initialize##passName##PassFlag; \
43  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
44  llvm::call_once(Initialize##passName##PassFlag, \
45  initialize##passName##PassOnce, std::ref(Registry)); \
46  }
47 
48 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
49  static void *initialize##passName##PassOnce(PassRegistry &Registry) {
50 
51 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
52 #define INITIALIZE_AG_DEPENDENCY(depName) \
53  initialize##depName##AnalysisGroup(Registry);
54 
55 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
56  PassInfo *PI = new PassInfo( \
57  name, arg, &passName::ID, \
58  PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
59  Registry.registerPass(*PI, true); \
60  return PI; \
61  } \
62  static llvm::once_flag Initialize##passName##PassFlag; \
63  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
64  llvm::call_once(Initialize##passName##PassFlag, \
65  initialize##passName##PassOnce, std::ref(Registry)); \
66  }
67 
68 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
69  INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
70  PassName::registerOptions(); \
71  INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
72 
73 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
74  INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
75  PassName::registerOptions();
76 
77 template <typename PassName> Pass *callDefaultCtor() { return new PassName(); }
78 
79 //===---------------------------------------------------------------------------
80 /// RegisterPass<t> template - This template class is used to notify the system
81 /// that a Pass is available for use, and registers it into the internal
82 /// database maintained by the PassManager. Unless this template is used, opt,
83 /// for example will not be able to see the pass and attempts to create the pass
84 /// will fail. This template is used in the follow manner (at global scope, in
85 /// your .cpp file):
86 ///
87 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
88 ///
89 /// This statement will cause your pass to be created by calling the default
90 /// constructor exposed by the pass.
91 ///
92 template <typename passName> struct RegisterPass : public PassInfo {
93  // Register Pass using default constructor...
94  RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
95  bool is_analysis = false)
96  : PassInfo(Name, PassArg, &passName::ID,
97  PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
98  is_analysis) {
100  }
101 };
102 
103 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
104 /// Analysis groups are used to define an interface (which need not derive from
105 /// Pass) that is required by passes to do their job. Analysis Groups differ
106 /// from normal analyses because any available implementation of the group will
107 /// be used if it is available.
108 ///
109 /// If no analysis implementing the interface is available, a default
110 /// implementation is created and added. A pass registers itself as the default
111 /// implementation by specifying 'true' as the second template argument of this
112 /// class.
113 ///
114 /// In addition to registering itself as an analysis group member, a pass must
115 /// register itself normally as well. Passes may be members of multiple groups
116 /// and may still be "required" specifically by name.
117 ///
118 /// The actual interface may also be registered as well (by not specifying the
119 /// second template argument). The interface should be registered to associate
120 /// a nice name with the interface.
121 ///
122 class RegisterAGBase : public PassInfo {
123 public:
124  RegisterAGBase(StringRef Name, const void *InterfaceID,
125  const void *PassID = nullptr, bool isDefault = false);
126 };
127 
128 template <typename Interface, bool Default = false>
131  : RegisterAGBase(RPB.getPassName(), &Interface::ID, RPB.getTypeInfo(),
132  Default) {}
133 
134  explicit RegisterAnalysisGroup(const char *Name)
135  : RegisterAGBase(Name, &Interface::ID) {}
136 };
137 
138 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
139  static void *initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
140  initialize##defaultPass##Pass(Registry); \
141  PassInfo *AI = new PassInfo(name, &agName::ID); \
142  Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true); \
143  return AI; \
144  } \
145  static llvm::once_flag Initialize##agName##AnalysisGroupFlag; \
146  void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
147  llvm::call_once(Initialize##agName##AnalysisGroupFlag, \
148  initialize##agName##AnalysisGroupOnce, \
149  std::ref(Registry)); \
150  }
151 
152 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
153  static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
154  if (!def) \
155  initialize##agName##AnalysisGroup(Registry); \
156  PassInfo *PI = new PassInfo( \
157  name, arg, &passName::ID, \
158  PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
159  Registry.registerPass(*PI, true); \
160  \
161  PassInfo *AI = new PassInfo(name, &agName::ID); \
162  Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, \
163  true); \
164  return AI; \
165  } \
166  static llvm::once_flag Initialize##passName##PassFlag; \
167  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
168  llvm::call_once(Initialize##passName##PassFlag, \
169  initialize##passName##PassOnce, std::ref(Registry)); \
170  }
171 
172 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
173  static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
174  if (!def) \
175  initialize##agName##AnalysisGroup(Registry);
176 
177 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
178  PassInfo *PI = new PassInfo( \
179  n, arg, &passName::ID, \
180  PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
181  Registry.registerPass(*PI, true); \
182  \
183  PassInfo *AI = new PassInfo(n, &agName::ID); \
184  Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true); \
185  return AI; \
186  } \
187  static llvm::once_flag Initialize##passName##PassFlag; \
188  void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
189  llvm::call_once(Initialize##passName##PassFlag, \
190  initialize##passName##PassOnce, std::ref(Registry)); \
191  }
192 
193 //===---------------------------------------------------------------------------
194 /// PassRegistrationListener class - This class is meant to be derived from by
195 /// clients that are interested in which passes get registered and unregistered
196 /// at runtime (which can be because of the RegisterPass constructors being run
197 /// as the program starts up, or may be because a shared object just got
198 /// loaded).
199 ///
203 
204  /// Callback functions - These functions are invoked whenever a pass is loaded
205  /// or removed from the current executable.
206  ///
207  virtual void passRegistered(const PassInfo *) {}
208 
209  /// enumeratePasses - Iterate over the registered passes, calling the
210  /// passEnumerate callback on each PassInfo object.
211  ///
212  void enumeratePasses();
213 
214  /// passEnumerate - Callback function invoked when someone calls
215  /// enumeratePasses on this PassRegistrationListener object.
216  ///
217  virtual void passEnumerate(const PassInfo *) {}
218 };
219 
220 } // End llvm namespace
221 
222 #endif
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition: PassSupport.h:92
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly=false, bool is_analysis=false)
Definition: PassSupport.h:94
RegisterAnalysisGroup(const char *Name)
Definition: PassSupport.h:134
Pass *(* NormalCtor_t)()
Definition: PassInfo.h:34
Pass * callDefaultCtor()
Definition: PassSupport.h:77
virtual void passRegistered(const PassInfo *)
Callback functions - These functions are invoked whenever a pass is loaded or removed from the curren...
Definition: PassSupport.h:207
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:73
const char * Name
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:217
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:32
RegisterAnalysisGroup - Register a Pass as a member of an analysis group.
Definition: PassSupport.h:122
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:63
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void registerPass(const PassInfo &PI, bool ShouldFree=false)
registerPass - Register a pass (by means of its PassInfo) with the registry.
RegisterAnalysisGroup(PassInfo &RPB)
Definition: PassSupport.h:130