PassSupport.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines stuff that is used to define and "use" Passes. This file
  10. // is automatically #included by Pass.h, so:
  11. //
  12. // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
  13. //
  14. // Instead, #include Pass.h.
  15. //
  16. // This file defines Pass registration code and classes used for it.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #if !defined(LLVM_PASS_H) || defined(LLVM_PASSSUPPORT_H)
  20. #error "Do not include <PassSupport.h>; include <Pass.h> instead"
  21. #endif
  22. #ifndef LLVM_PASSSUPPORT_H
  23. #define LLVM_PASSSUPPORT_H
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/PassInfo.h"
  26. #include "llvm/PassRegistry.h"
  27. #include "llvm/Support/Threading.h"
  28. #include <functional>
  29. namespace llvm {
  30. class Pass;
  31. #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
  32. static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
  33. PassInfo *PI = new PassInfo( \
  34. name, arg, &passName::ID, \
  35. PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
  36. Registry.registerPass(*PI, true); \
  37. return PI; \
  38. } \
  39. static llvm::once_flag Initialize##passName##PassFlag; \
  40. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  41. llvm::call_once(Initialize##passName##PassFlag, \
  42. initialize##passName##PassOnce, std::ref(Registry)); \
  43. }
  44. #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
  45. static void *initialize##passName##PassOnce(PassRegistry &Registry) {
  46. #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
  47. #define INITIALIZE_AG_DEPENDENCY(depName) \
  48. initialize##depName##AnalysisGroup(Registry);
  49. #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
  50. PassInfo *PI = new PassInfo( \
  51. name, arg, &passName::ID, \
  52. PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
  53. Registry.registerPass(*PI, true); \
  54. return PI; \
  55. } \
  56. static llvm::once_flag Initialize##passName##PassFlag; \
  57. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  58. llvm::call_once(Initialize##passName##PassFlag, \
  59. initialize##passName##PassOnce, std::ref(Registry)); \
  60. }
  61. #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
  62. INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
  63. PassName::registerOptions(); \
  64. INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
  65. #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
  66. INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
  67. PassName::registerOptions();
  68. template <typename PassName> Pass *callDefaultCtor() { return new PassName(); }
  69. //===---------------------------------------------------------------------------
  70. /// RegisterPass<t> template - This template class is used to notify the system
  71. /// that a Pass is available for use, and registers it into the internal
  72. /// database maintained by the PassManager. Unless this template is used, opt,
  73. /// for example will not be able to see the pass and attempts to create the pass
  74. /// will fail. This template is used in the follow manner (at global scope, in
  75. /// your .cpp file):
  76. ///
  77. /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
  78. ///
  79. /// This statement will cause your pass to be created by calling the default
  80. /// constructor exposed by the pass.
  81. template <typename passName> struct RegisterPass : public PassInfo {
  82. // Register Pass using default constructor...
  83. RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
  84. bool is_analysis = false)
  85. : PassInfo(Name, PassArg, &passName::ID,
  86. PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
  87. is_analysis) {
  88. PassRegistry::getPassRegistry()->registerPass(*this);
  89. }
  90. };
  91. /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
  92. /// Analysis groups are used to define an interface (which need not derive from
  93. /// Pass) that is required by passes to do their job. Analysis Groups differ
  94. /// from normal analyses because any available implementation of the group will
  95. /// be used if it is available.
  96. ///
  97. /// If no analysis implementing the interface is available, a default
  98. /// implementation is created and added. A pass registers itself as the default
  99. /// implementation by specifying 'true' as the second template argument of this
  100. /// class.
  101. ///
  102. /// In addition to registering itself as an analysis group member, a pass must
  103. /// register itself normally as well. Passes may be members of multiple groups
  104. /// and may still be "required" specifically by name.
  105. ///
  106. /// The actual interface may also be registered as well (by not specifying the
  107. /// second template argument). The interface should be registered to associate
  108. /// a nice name with the interface.
  109. class RegisterAGBase : public PassInfo {
  110. public:
  111. RegisterAGBase(StringRef Name, const void *InterfaceID,
  112. const void *PassID = nullptr, bool isDefault = false);
  113. };
  114. template <typename Interface, bool Default = false>
  115. struct RegisterAnalysisGroup : public RegisterAGBase {
  116. explicit RegisterAnalysisGroup(PassInfo &RPB)
  117. : RegisterAGBase(RPB.getPassName(), &Interface::ID, RPB.getTypeInfo(),
  118. Default) {}
  119. explicit RegisterAnalysisGroup(const char *Name)
  120. : RegisterAGBase(Name, &Interface::ID) {}
  121. };
  122. #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
  123. static void *initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
  124. initialize##defaultPass##Pass(Registry); \
  125. PassInfo *AI = new PassInfo(name, &agName::ID); \
  126. Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true); \
  127. return AI; \
  128. } \
  129. static llvm::once_flag Initialize##agName##AnalysisGroupFlag; \
  130. void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
  131. llvm::call_once(Initialize##agName##AnalysisGroupFlag, \
  132. initialize##agName##AnalysisGroupOnce, \
  133. std::ref(Registry)); \
  134. }
  135. #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
  136. static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
  137. if (!def) \
  138. initialize##agName##AnalysisGroup(Registry); \
  139. PassInfo *PI = new PassInfo( \
  140. name, arg, &passName::ID, \
  141. PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
  142. Registry.registerPass(*PI, true); \
  143. \
  144. PassInfo *AI = new PassInfo(name, &agName::ID); \
  145. Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, \
  146. true); \
  147. return AI; \
  148. } \
  149. static llvm::once_flag Initialize##passName##PassFlag; \
  150. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  151. llvm::call_once(Initialize##passName##PassFlag, \
  152. initialize##passName##PassOnce, std::ref(Registry)); \
  153. }
  154. #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
  155. static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
  156. if (!def) \
  157. initialize##agName##AnalysisGroup(Registry);
  158. #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
  159. PassInfo *PI = new PassInfo( \
  160. n, arg, &passName::ID, \
  161. PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
  162. Registry.registerPass(*PI, true); \
  163. \
  164. PassInfo *AI = new PassInfo(n, &agName::ID); \
  165. Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true); \
  166. return AI; \
  167. } \
  168. static llvm::once_flag Initialize##passName##PassFlag; \
  169. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  170. llvm::call_once(Initialize##passName##PassFlag, \
  171. initialize##passName##PassOnce, std::ref(Registry)); \
  172. }
  173. //===---------------------------------------------------------------------------
  174. /// PassRegistrationListener class - This class is meant to be derived from by
  175. /// clients that are interested in which passes get registered and unregistered
  176. /// at runtime (which can be because of the RegisterPass constructors being run
  177. /// as the program starts up, or may be because a shared object just got
  178. /// loaded).
  179. struct PassRegistrationListener {
  180. PassRegistrationListener() = default;
  181. virtual ~PassRegistrationListener() = default;
  182. /// Callback functions - These functions are invoked whenever a pass is loaded
  183. /// or removed from the current executable.
  184. virtual void passRegistered(const PassInfo *) {}
  185. /// enumeratePasses - Iterate over the registered passes, calling the
  186. /// passEnumerate callback on each PassInfo object.
  187. void enumeratePasses();
  188. /// passEnumerate - Callback function invoked when someone calls
  189. /// enumeratePasses on this PassRegistrationListener object.
  190. virtual void passEnumerate(const PassInfo *) {}
  191. };
  192. } // end namespace llvm
  193. #endif // LLVM_PASSSUPPORT_H