PassSupport.h 12 KB

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