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.

299 lines
12 KiB

  1. //===-- llvm/GlobalValue.h - Class to represent a global value --*- 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 is a common base class of all globally definable objects. As such,
  11. // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is
  12. // used because you can do certain things with these global objects that you
  13. // can't do to anything else. For example, use the address of one as a
  14. // constant.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_GLOBALVALUE_H
  18. #define LLVM_IR_GLOBALVALUE_H
  19. #include "llvm/IR/Constant.h"
  20. namespace llvm {
  21. class PointerType;
  22. class Module;
  23. class GlobalValue : public Constant {
  24. GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
  25. public:
  26. /// @brief An enumeration for the kinds of linkage for global values.
  27. enum LinkageTypes {
  28. ExternalLinkage = 0,///< Externally visible function
  29. AvailableExternallyLinkage, ///< Available for inspection, not emission.
  30. LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
  31. LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
  32. LinkOnceODRAutoHideLinkage, ///< Like LinkOnceODRLinkage but addr not taken.
  33. WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
  34. WeakODRLinkage, ///< Same, but only replaced by something equivalent.
  35. AppendingLinkage, ///< Special purpose, only applies to global arrays
  36. InternalLinkage, ///< Rename collisions when linking (static functions).
  37. PrivateLinkage, ///< Like Internal, but omit from symbol table.
  38. LinkerPrivateLinkage, ///< Like Private, but linker removes.
  39. LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
  40. DLLImportLinkage, ///< Function to be imported from DLL
  41. DLLExportLinkage, ///< Function to be accessible from DLL.
  42. ExternalWeakLinkage,///< ExternalWeak linkage description.
  43. CommonLinkage ///< Tentative definitions.
  44. };
  45. /// @brief An enumeration for the kinds of visibility of global values.
  46. enum VisibilityTypes {
  47. DefaultVisibility = 0, ///< The GV is visible
  48. HiddenVisibility, ///< The GV is hidden
  49. ProtectedVisibility ///< The GV is protected
  50. };
  51. protected:
  52. GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
  53. LinkageTypes linkage, const Twine &Name)
  54. : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
  55. Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) {
  56. setName(Name);
  57. }
  58. // Note: VC++ treats enums as signed, so an extra bit is required to prevent
  59. // Linkage and Visibility from turning into negative values.
  60. LinkageTypes Linkage : 5; // The linkage of this global
  61. unsigned Visibility : 2; // The visibility style of this global
  62. unsigned Alignment : 16; // Alignment of this symbol, must be power of two
  63. unsigned UnnamedAddr : 1; // This value's address is not significant
  64. Module *Parent; // The containing module.
  65. std::string Section; // Section to emit this into, empty mean default
  66. public:
  67. ~GlobalValue() {
  68. removeDeadConstantUsers(); // remove any dead constants using this.
  69. }
  70. unsigned getAlignment() const {
  71. return (1u << Alignment) >> 1;
  72. }
  73. void setAlignment(unsigned Align);
  74. bool hasUnnamedAddr() const { return UnnamedAddr; }
  75. void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
  76. VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
  77. bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
  78. bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
  79. bool hasProtectedVisibility() const {
  80. return Visibility == ProtectedVisibility;
  81. }
  82. void setVisibility(VisibilityTypes V) { Visibility = V; }
  83. bool hasSection() const { return !Section.empty(); }
  84. const std::string &getSection() const { return Section; }
  85. void setSection(StringRef S) { Section = S; }
  86. /// If the usage is empty (except transitively dead constants), then this
  87. /// global value can be safely deleted since the destructor will
  88. /// delete the dead constants as well.
  89. /// @brief Determine if the usage of this global value is empty except
  90. /// for transitively dead constants.
  91. bool use_empty_except_constants();
  92. /// getType - Global values are always pointers.
  93. inline PointerType *getType() const {
  94. return reinterpret_cast<PointerType*>(User::getType());
  95. }
  96. static LinkageTypes getLinkOnceLinkage(bool ODR) {
  97. return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
  98. }
  99. static LinkageTypes getWeakLinkage(bool ODR) {
  100. return ODR ? WeakODRLinkage : WeakAnyLinkage;
  101. }
  102. static bool isExternalLinkage(LinkageTypes Linkage) {
  103. return Linkage == ExternalLinkage;
  104. }
  105. static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
  106. return Linkage == AvailableExternallyLinkage;
  107. }
  108. static bool isLinkOnceLinkage(LinkageTypes Linkage) {
  109. return Linkage == LinkOnceAnyLinkage ||
  110. Linkage == LinkOnceODRLinkage ||
  111. Linkage == LinkOnceODRAutoHideLinkage;
  112. }
  113. static bool isLinkOnceODRAutoHideLinkage(LinkageTypes Linkage) {
  114. return Linkage == LinkOnceODRAutoHideLinkage;
  115. }
  116. static bool isWeakLinkage(LinkageTypes Linkage) {
  117. return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
  118. }
  119. static bool isAppendingLinkage(LinkageTypes Linkage) {
  120. return Linkage == AppendingLinkage;
  121. }
  122. static bool isInternalLinkage(LinkageTypes Linkage) {
  123. return Linkage == InternalLinkage;
  124. }
  125. static bool isPrivateLinkage(LinkageTypes Linkage) {
  126. return Linkage == PrivateLinkage;
  127. }
  128. static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
  129. return Linkage == LinkerPrivateLinkage;
  130. }
  131. static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
  132. return Linkage == LinkerPrivateWeakLinkage;
  133. }
  134. static bool isLocalLinkage(LinkageTypes Linkage) {
  135. return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
  136. isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
  137. }
  138. static bool isDLLImportLinkage(LinkageTypes Linkage) {
  139. return Linkage == DLLImportLinkage;
  140. }
  141. static bool isDLLExportLinkage(LinkageTypes Linkage) {
  142. return Linkage == DLLExportLinkage;
  143. }
  144. static bool isExternalWeakLinkage(LinkageTypes Linkage) {
  145. return Linkage == ExternalWeakLinkage;
  146. }
  147. static bool isCommonLinkage(LinkageTypes Linkage) {
  148. return Linkage == CommonLinkage;
  149. }
  150. /// isDiscardableIfUnused - Whether the definition of this global may be
  151. /// discarded if it is not used in its compilation unit.
  152. static bool isDiscardableIfUnused(LinkageTypes Linkage) {
  153. return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
  154. }
  155. /// mayBeOverridden - Whether the definition of this global may be replaced
  156. /// by something non-equivalent at link time. For example, if a function has
  157. /// weak linkage then the code defining it may be replaced by different code.
  158. static bool mayBeOverridden(LinkageTypes Linkage) {
  159. return Linkage == WeakAnyLinkage ||
  160. Linkage == LinkOnceAnyLinkage ||
  161. Linkage == CommonLinkage ||
  162. Linkage == ExternalWeakLinkage ||
  163. Linkage == LinkerPrivateWeakLinkage;
  164. }
  165. /// isWeakForLinker - Whether the definition of this global may be replaced at
  166. /// link time. NB: Using this method outside of the code generators is almost
  167. /// always a mistake: when working at the IR level use mayBeOverridden instead
  168. /// as it knows about ODR semantics.
  169. static bool isWeakForLinker(LinkageTypes Linkage) {
  170. return Linkage == AvailableExternallyLinkage ||
  171. Linkage == WeakAnyLinkage ||
  172. Linkage == WeakODRLinkage ||
  173. Linkage == LinkOnceAnyLinkage ||
  174. Linkage == LinkOnceODRLinkage ||
  175. Linkage == LinkOnceODRAutoHideLinkage ||
  176. Linkage == CommonLinkage ||
  177. Linkage == ExternalWeakLinkage ||
  178. Linkage == LinkerPrivateWeakLinkage;
  179. }
  180. bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
  181. bool hasAvailableExternallyLinkage() const {
  182. return isAvailableExternallyLinkage(Linkage);
  183. }
  184. bool hasLinkOnceLinkage() const {
  185. return isLinkOnceLinkage(Linkage);
  186. }
  187. bool hasLinkOnceODRAutoHideLinkage() const {
  188. return isLinkOnceODRAutoHideLinkage(Linkage);
  189. }
  190. bool hasWeakLinkage() const {
  191. return isWeakLinkage(Linkage);
  192. }
  193. bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
  194. bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
  195. bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
  196. bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
  197. bool hasLinkerPrivateWeakLinkage() const {
  198. return isLinkerPrivateWeakLinkage(Linkage);
  199. }
  200. bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
  201. bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
  202. bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
  203. bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
  204. bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
  205. void setLinkage(LinkageTypes LT) { Linkage = LT; }
  206. LinkageTypes getLinkage() const { return Linkage; }
  207. bool isDiscardableIfUnused() const {
  208. return isDiscardableIfUnused(Linkage);
  209. }
  210. bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
  211. bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
  212. /// copyAttributesFrom - copy all additional attributes (those not needed to
  213. /// create a GlobalValue) from the GlobalValue Src to this one.
  214. virtual void copyAttributesFrom(const GlobalValue *Src);
  215. /// @name Materialization
  216. /// Materialization is used to construct functions only as they're needed. This
  217. /// is useful to reduce memory usage in LLVM or parsing work done by the
  218. /// BitcodeReader to load the Module.
  219. /// @{
  220. /// isMaterializable - If this function's Module is being lazily streamed in
  221. /// functions from disk or some other source, this method can be used to check
  222. /// to see if the function has been read in yet or not.
  223. bool isMaterializable() const;
  224. /// isDematerializable - Returns true if this function was loaded from a
  225. /// GVMaterializer that's still attached to its Module and that knows how to
  226. /// dematerialize the function.
  227. bool isDematerializable() const;
  228. /// Materialize - make sure this GlobalValue is fully read. If the module is
  229. /// corrupt, this returns true and fills in the optional string with
  230. /// information about the problem. If successful, this returns false.
  231. bool Materialize(std::string *ErrInfo = 0);
  232. /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
  233. /// supports it, release the memory for the function, and set it up to be
  234. /// materialized lazily. If !isDematerializable(), this method is a noop.
  235. void Dematerialize();
  236. /// @}
  237. /// Override from Constant class.
  238. virtual void destroyConstant();
  239. /// isDeclaration - Return true if the primary definition of this global
  240. /// value is outside of the current translation unit.
  241. bool isDeclaration() const;
  242. /// removeFromParent - This method unlinks 'this' from the containing module,
  243. /// but does not delete it.
  244. virtual void removeFromParent() = 0;
  245. /// eraseFromParent - This method unlinks 'this' from the containing module
  246. /// and deletes it.
  247. virtual void eraseFromParent() = 0;
  248. /// getParent - Get the module that this global value is contained inside
  249. /// of...
  250. inline Module *getParent() { return Parent; }
  251. inline const Module *getParent() const { return Parent; }
  252. // Methods for support type inquiry through isa, cast, and dyn_cast:
  253. static inline bool classof(const Value *V) {
  254. return V->getValueID() == Value::FunctionVal ||
  255. V->getValueID() == Value::GlobalVariableVal ||
  256. V->getValueID() == Value::GlobalAliasVal;
  257. }
  258. };
  259. } // End llvm namespace
  260. #endif