PassSupport.h 9.3 KB

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