PassSupport.h 12 KB

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