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