Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
13 KiB

  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/PassRegistry.h"
  25. #include "llvm/Support/Atomic.h"
  26. #include "llvm/Support/Valgrind.h"
  27. #include <vector>
  28. namespace llvm {
  29. //===---------------------------------------------------------------------------
  30. /// PassInfo class - An instance of this class exists for every pass known by
  31. /// the system, and can be obtained from a live Pass by calling its
  32. /// getPassInfo() method. These objects are set up by the RegisterPass<>
  33. /// template, defined below.
  34. ///
  35. class PassInfo {
  36. public:
  37. typedef Pass* (*NormalCtor_t)();
  38. private:
  39. const char *const PassName; // Nice name for Pass
  40. const char *const PassArgument; // Command Line argument to run this pass
  41. const void *PassID;
  42. const bool IsCFGOnlyPass; // Pass only looks at the CFG.
  43. const bool IsAnalysis; // True if an analysis pass.
  44. const bool IsAnalysisGroup; // True if an analysis group.
  45. std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
  46. NormalCtor_t NormalCtor;
  47. public:
  48. /// PassInfo ctor - Do not call this directly, this should only be invoked
  49. /// through RegisterPass.
  50. PassInfo(const char *name, const char *arg, const void *pi,
  51. NormalCtor_t normal, bool isCFGOnly, bool is_analysis)
  52. : PassName(name), PassArgument(arg), PassID(pi),
  53. IsCFGOnlyPass(isCFGOnly),
  54. IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { }
  55. /// PassInfo ctor - Do not call this directly, this should only be invoked
  56. /// through RegisterPass. This version is for use by analysis groups; it
  57. /// does not auto-register the pass.
  58. PassInfo(const char *name, const void *pi)
  59. : PassName(name), PassArgument(""), PassID(pi),
  60. IsCFGOnlyPass(false),
  61. IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { }
  62. /// getPassName - Return the friendly name for the pass, never returns null
  63. ///
  64. const char *getPassName() const { return PassName; }
  65. /// getPassArgument - Return the command line option that may be passed to
  66. /// 'opt' that will cause this pass to be run. This will return null if there
  67. /// is no argument.
  68. ///
  69. const char *getPassArgument() const { return PassArgument; }
  70. /// getTypeInfo - Return the id object for the pass...
  71. /// TODO : Rename
  72. const void *getTypeInfo() const { return PassID; }
  73. /// Return true if this PassID implements the specified ID pointer.
  74. bool isPassID(const void *IDPtr) const {
  75. return PassID == IDPtr;
  76. }
  77. /// isAnalysisGroup - Return true if this is an analysis group, not a normal
  78. /// pass.
  79. ///
  80. bool isAnalysisGroup() const { return IsAnalysisGroup; }
  81. bool isAnalysis() const { return IsAnalysis; }
  82. /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
  83. /// function.
  84. bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
  85. /// getNormalCtor - Return a pointer to a function, that when called, creates
  86. /// an instance of the pass and returns it. This pointer may be null if there
  87. /// is no default constructor for the pass.
  88. ///
  89. NormalCtor_t getNormalCtor() const {
  90. return NormalCtor;
  91. }
  92. void setNormalCtor(NormalCtor_t Ctor) {
  93. NormalCtor = Ctor;
  94. }
  95. /// createPass() - Use this method to create an instance of this pass.
  96. Pass *createPass() const;
  97. /// addInterfaceImplemented - This method is called when this pass is
  98. /// registered as a member of an analysis group with the RegisterAnalysisGroup
  99. /// template.
  100. ///
  101. void addInterfaceImplemented(const PassInfo *ItfPI) {
  102. ItfImpl.push_back(ItfPI);
  103. }
  104. /// getInterfacesImplemented - Return a list of all of the analysis group
  105. /// interfaces implemented by this pass.
  106. ///
  107. const std::vector<const PassInfo*> &getInterfacesImplemented() const {
  108. return ItfImpl;
  109. }
  110. private:
  111. void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
  112. PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
  113. };
  114. #define CALL_ONCE_INITIALIZATION(function) \
  115. static volatile sys::cas_flag initialized = 0; \
  116. sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
  117. if (old_val == 0) { \
  118. function(Registry); \
  119. sys::MemoryFence(); \
  120. TsanIgnoreWritesBegin(); \
  121. TsanHappensBefore(&initialized); \
  122. initialized = 2; \
  123. TsanIgnoreWritesEnd(); \
  124. } else { \
  125. sys::cas_flag tmp = initialized; \
  126. sys::MemoryFence(); \
  127. while (tmp != 2) { \
  128. tmp = initialized; \
  129. sys::MemoryFence(); \
  130. } \
  131. } \
  132. TsanHappensAfter(&initialized);
  133. #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
  134. static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
  135. PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
  136. PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
  137. Registry.registerPass(*PI, true); \
  138. return PI; \
  139. } \
  140. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  141. CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
  142. }
  143. #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
  144. static void* initialize##passName##PassOnce(PassRegistry &Registry) {
  145. #define INITIALIZE_PASS_DEPENDENCY(depName) \
  146. initialize##depName##Pass(Registry);
  147. #define INITIALIZE_AG_DEPENDENCY(depName) \
  148. initialize##depName##AnalysisGroup(Registry);
  149. #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
  150. PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
  151. PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
  152. Registry.registerPass(*PI, true); \
  153. return PI; \
  154. } \
  155. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  156. CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
  157. }
  158. template<typename PassName>
  159. Pass *callDefaultCtor() { return new PassName(); }
  160. //===---------------------------------------------------------------------------
  161. /// RegisterPass<t> template - This template class is used to notify the system
  162. /// that a Pass is available for use, and registers it into the internal
  163. /// database maintained by the PassManager. Unless this template is used, opt,
  164. /// for example will not be able to see the pass and attempts to create the pass
  165. /// will fail. This template is used in the follow manner (at global scope, in
  166. /// your .cpp file):
  167. ///
  168. /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
  169. ///
  170. /// This statement will cause your pass to be created by calling the default
  171. /// constructor exposed by the pass. If you have a different constructor that
  172. /// must be called, create a global constructor function (which takes the
  173. /// arguments you need and returns a Pass*) and register your pass like this:
  174. ///
  175. /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
  176. ///
  177. template<typename passName>
  178. struct RegisterPass : public PassInfo {
  179. // Register Pass using default constructor...
  180. RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
  181. bool is_analysis = false)
  182. : PassInfo(Name, PassArg, &passName::ID,
  183. PassInfo::NormalCtor_t(callDefaultCtor<passName>),
  184. CFGOnly, is_analysis) {
  185. PassRegistry::getPassRegistry()->registerPass(*this);
  186. }
  187. };
  188. /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
  189. /// Analysis groups are used to define an interface (which need not derive from
  190. /// Pass) that is required by passes to do their job. Analysis Groups differ
  191. /// from normal analyses because any available implementation of the group will
  192. /// be used if it is available.
  193. ///
  194. /// If no analysis implementing the interface is available, a default
  195. /// implementation is created and added. A pass registers itself as the default
  196. /// implementation by specifying 'true' as the second template argument of this
  197. /// class.
  198. ///
  199. /// In addition to registering itself as an analysis group member, a pass must
  200. /// register itself normally as well. Passes may be members of multiple groups
  201. /// and may still be "required" specifically by name.
  202. ///
  203. /// The actual interface may also be registered as well (by not specifying the
  204. /// second template argument). The interface should be registered to associate
  205. /// a nice name with the interface.
  206. ///
  207. class RegisterAGBase : public PassInfo {
  208. public:
  209. RegisterAGBase(const char *Name,
  210. const void *InterfaceID,
  211. const void *PassID = 0,
  212. bool isDefault = false);
  213. };
  214. template<typename Interface, bool Default = false>
  215. struct RegisterAnalysisGroup : public RegisterAGBase {
  216. explicit RegisterAnalysisGroup(PassInfo &RPB)
  217. : RegisterAGBase(RPB.getPassName(),
  218. &Interface::ID, RPB.getTypeInfo(),
  219. Default) {
  220. }
  221. explicit RegisterAnalysisGroup(const char *Name)
  222. : RegisterAGBase(Name, &Interface::ID) {
  223. }
  224. };
  225. #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
  226. static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
  227. initialize##defaultPass##Pass(Registry); \
  228. PassInfo *AI = new PassInfo(name, & agName :: ID); \
  229. Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
  230. return AI; \
  231. } \
  232. void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
  233. CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
  234. }
  235. #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
  236. static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
  237. if (!def) initialize##agName##AnalysisGroup(Registry); \
  238. PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
  239. PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
  240. Registry.registerPass(*PI, true); \
  241. \
  242. PassInfo *AI = new PassInfo(name, & agName :: ID); \
  243. Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
  244. *AI, def, true); \
  245. return AI; \
  246. } \
  247. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  248. CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
  249. }
  250. #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
  251. static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
  252. if (!def) initialize##agName##AnalysisGroup(Registry);
  253. #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
  254. PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
  255. PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
  256. Registry.registerPass(*PI, true); \
  257. \
  258. PassInfo *AI = new PassInfo(n, & agName :: ID); \
  259. Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
  260. *AI, def, true); \
  261. return AI; \
  262. } \
  263. void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
  264. CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
  265. }
  266. //===---------------------------------------------------------------------------
  267. /// PassRegistrationListener class - This class is meant to be derived from by
  268. /// clients that are interested in which passes get registered and unregistered
  269. /// at runtime (which can be because of the RegisterPass constructors being run
  270. /// as the program starts up, or may be because a shared object just got
  271. /// loaded). Deriving from the PassRegistrationListener class automatically
  272. /// registers your object to receive callbacks indicating when passes are loaded
  273. /// and removed.
  274. ///
  275. struct PassRegistrationListener {
  276. /// PassRegistrationListener ctor - Add the current object to the list of
  277. /// PassRegistrationListeners...
  278. PassRegistrationListener();
  279. /// dtor - Remove object from list of listeners...
  280. ///
  281. virtual ~PassRegistrationListener();
  282. /// Callback functions - These functions are invoked whenever a pass is loaded
  283. /// or removed from the current executable.
  284. ///
  285. virtual void passRegistered(const PassInfo *) {}
  286. /// enumeratePasses - Iterate over the registered passes, calling the
  287. /// passEnumerate callback on each PassInfo object.
  288. ///
  289. void enumeratePasses();
  290. /// passEnumerate - Callback function invoked when someone calls
  291. /// enumeratePasses on this PassRegistrationListener object.
  292. ///
  293. virtual void passEnumerate(const PassInfo *) {}
  294. };
  295. } // End llvm namespace
  296. #endif