GlobalValue.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #include "llvm/IR/DerivedTypes.h"
  21. #include <system_error>
  22. namespace llvm {
  23. class Comdat;
  24. class PointerType;
  25. class Module;
  26. namespace Intrinsic {
  27. enum ID : unsigned;
  28. }
  29. class GlobalValue : public Constant {
  30. GlobalValue(const GlobalValue &) = delete;
  31. public:
  32. /// @brief An enumeration for the kinds of linkage for global values.
  33. enum LinkageTypes {
  34. ExternalLinkage = 0,///< Externally visible function
  35. AvailableExternallyLinkage, ///< Available for inspection, not emission.
  36. LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
  37. LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
  38. WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
  39. WeakODRLinkage, ///< Same, but only replaced by something equivalent.
  40. AppendingLinkage, ///< Special purpose, only applies to global arrays
  41. InternalLinkage, ///< Rename collisions when linking (static functions).
  42. PrivateLinkage, ///< Like Internal, but omit from symbol table.
  43. ExternalWeakLinkage,///< ExternalWeak linkage description.
  44. CommonLinkage ///< Tentative definitions.
  45. };
  46. /// @brief An enumeration for the kinds of visibility of global values.
  47. enum VisibilityTypes {
  48. DefaultVisibility = 0, ///< The GV is visible
  49. HiddenVisibility, ///< The GV is hidden
  50. ProtectedVisibility ///< The GV is protected
  51. };
  52. /// @brief Storage classes of global values for PE targets.
  53. enum DLLStorageClassTypes {
  54. DefaultStorageClass = 0,
  55. DLLImportStorageClass = 1, ///< Function to be imported from DLL
  56. DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
  57. };
  58. protected:
  59. GlobalValue(PointerType *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
  60. LinkageTypes Linkage, const Twine &Name)
  61. : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
  62. Visibility(DefaultVisibility), UnnamedAddr(0),
  63. DllStorageClass(DefaultStorageClass),
  64. ThreadLocal(NotThreadLocal), IntID((Intrinsic::ID)0U), Parent(nullptr) {
  65. setName(Name);
  66. }
  67. // Note: VC++ treats enums as signed, so an extra bit is required to prevent
  68. // Linkage and Visibility from turning into negative values.
  69. LinkageTypes Linkage : 5; // The linkage of this global
  70. unsigned Visibility : 2; // The visibility style of this global
  71. unsigned UnnamedAddr : 1; // This value's address is not significant
  72. unsigned DllStorageClass : 2; // DLL storage class
  73. unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
  74. // the desired model?
  75. static const unsigned GlobalValueSubClassDataBits = 19;
  76. private:
  77. // Give subclasses access to what otherwise would be wasted padding.
  78. // (19 + 3 + 2 + 1 + 2 + 5) == 32.
  79. unsigned SubClassData : GlobalValueSubClassDataBits;
  80. friend class Constant;
  81. void destroyConstantImpl();
  82. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  83. protected:
  84. /// \brief The intrinsic ID for this subclass (which must be a Function).
  85. ///
  86. /// This member is defined by this class, but not used for anything.
  87. /// Subclasses can use it to store their intrinsic ID, if they have one.
  88. ///
  89. /// This is stored here to save space in Function on 64-bit hosts.
  90. Intrinsic::ID IntID;
  91. unsigned getGlobalValueSubClassData() const {
  92. return SubClassData;
  93. }
  94. void setGlobalValueSubClassData(unsigned V) {
  95. assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit");
  96. SubClassData = V;
  97. }
  98. Module *Parent; // The containing module.
  99. public:
  100. enum ThreadLocalMode {
  101. NotThreadLocal = 0,
  102. GeneralDynamicTLSModel,
  103. LocalDynamicTLSModel,
  104. InitialExecTLSModel,
  105. LocalExecTLSModel
  106. };
  107. ~GlobalValue() override {
  108. removeDeadConstantUsers(); // remove any dead constants using this.
  109. }
  110. unsigned getAlignment() const;
  111. bool hasUnnamedAddr() const { return UnnamedAddr; }
  112. void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
  113. bool hasComdat() const { return getComdat() != nullptr; }
  114. Comdat *getComdat();
  115. const Comdat *getComdat() const {
  116. return const_cast<GlobalValue *>(this)->getComdat();
  117. }
  118. VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
  119. bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
  120. bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
  121. bool hasProtectedVisibility() const {
  122. return Visibility == ProtectedVisibility;
  123. }
  124. void setVisibility(VisibilityTypes V) {
  125. assert((!hasLocalLinkage() || V == DefaultVisibility) &&
  126. "local linkage requires default visibility");
  127. Visibility = V;
  128. }
  129. /// If the value is "Thread Local", its value isn't shared by the threads.
  130. bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
  131. void setThreadLocal(bool Val) {
  132. setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
  133. }
  134. void setThreadLocalMode(ThreadLocalMode Val) {
  135. assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
  136. ThreadLocal = Val;
  137. }
  138. ThreadLocalMode getThreadLocalMode() const {
  139. return static_cast<ThreadLocalMode>(ThreadLocal);
  140. }
  141. DLLStorageClassTypes getDLLStorageClass() const {
  142. return DLLStorageClassTypes(DllStorageClass);
  143. }
  144. bool hasDLLImportStorageClass() const {
  145. return DllStorageClass == DLLImportStorageClass;
  146. }
  147. bool hasDLLExportStorageClass() const {
  148. return DllStorageClass == DLLExportStorageClass;
  149. }
  150. void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
  151. bool hasSection() const { return !StringRef(getSection()).empty(); }
  152. // It is unfortunate that we have to use "char *" in here since this is
  153. // always non NULL, but:
  154. // * The C API expects a null terminated string, so we cannot use StringRef.
  155. // * The C API expects us to own it, so we cannot use a std:string.
  156. // * For GlobalAliases we can fail to find the section and we have to
  157. // return "", so we cannot use a "const std::string &".
  158. const char *getSection() const;
  159. /// Global values are always pointers.
  160. PointerType *getType() const { return cast<PointerType>(User::getType()); }
  161. Type *getValueType() const { return getType()->getElementType(); }
  162. static LinkageTypes getLinkOnceLinkage(bool ODR) {
  163. return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
  164. }
  165. static LinkageTypes getWeakLinkage(bool ODR) {
  166. return ODR ? WeakODRLinkage : WeakAnyLinkage;
  167. }
  168. static bool isExternalLinkage(LinkageTypes Linkage) {
  169. return Linkage == ExternalLinkage;
  170. }
  171. static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
  172. return Linkage == AvailableExternallyLinkage;
  173. }
  174. static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
  175. return Linkage == LinkOnceODRLinkage;
  176. }
  177. static bool isLinkOnceLinkage(LinkageTypes Linkage) {
  178. return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
  179. }
  180. static bool isWeakAnyLinkage(LinkageTypes Linkage) {
  181. return Linkage == WeakAnyLinkage;
  182. }
  183. static bool isWeakODRLinkage(LinkageTypes Linkage) {
  184. return Linkage == WeakODRLinkage;
  185. }
  186. static bool isWeakLinkage(LinkageTypes Linkage) {
  187. return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
  188. }
  189. static bool isAppendingLinkage(LinkageTypes Linkage) {
  190. return Linkage == AppendingLinkage;
  191. }
  192. static bool isInternalLinkage(LinkageTypes Linkage) {
  193. return Linkage == InternalLinkage;
  194. }
  195. static bool isPrivateLinkage(LinkageTypes Linkage) {
  196. return Linkage == PrivateLinkage;
  197. }
  198. static bool isLocalLinkage(LinkageTypes Linkage) {
  199. return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
  200. }
  201. static bool isExternalWeakLinkage(LinkageTypes Linkage) {
  202. return Linkage == ExternalWeakLinkage;
  203. }
  204. static bool isCommonLinkage(LinkageTypes Linkage) {
  205. return Linkage == CommonLinkage;
  206. }
  207. /// Whether the definition of this global may be discarded if it is not used
  208. /// in its compilation unit.
  209. static bool isDiscardableIfUnused(LinkageTypes Linkage) {
  210. return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
  211. }
  212. /// Whether the definition of this global may be replaced by something
  213. /// non-equivalent at link time. For example, if a function has weak linkage
  214. /// then the code defining it may be replaced by different code.
  215. static bool mayBeOverridden(LinkageTypes Linkage) {
  216. return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
  217. Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
  218. }
  219. /// Whether the definition of this global may be replaced at link time. NB:
  220. /// Using this method outside of the code generators is almost always a
  221. /// mistake: when working at the IR level use mayBeOverridden instead as it
  222. /// knows about ODR semantics.
  223. static bool isWeakForLinker(LinkageTypes Linkage) {
  224. return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage ||
  225. Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
  226. Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
  227. }
  228. bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
  229. bool hasAvailableExternallyLinkage() const {
  230. return isAvailableExternallyLinkage(Linkage);
  231. }
  232. bool hasLinkOnceLinkage() const {
  233. return isLinkOnceLinkage(Linkage);
  234. }
  235. bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
  236. bool hasWeakLinkage() const {
  237. return isWeakLinkage(Linkage);
  238. }
  239. bool hasWeakAnyLinkage() const {
  240. return isWeakAnyLinkage(Linkage);
  241. }
  242. bool hasWeakODRLinkage() const {
  243. return isWeakODRLinkage(Linkage);
  244. }
  245. bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
  246. bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
  247. bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
  248. bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
  249. bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
  250. bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
  251. void setLinkage(LinkageTypes LT) {
  252. if (isLocalLinkage(LT))
  253. Visibility = DefaultVisibility;
  254. Linkage = LT;
  255. }
  256. LinkageTypes getLinkage() const { return Linkage; }
  257. bool isDiscardableIfUnused() const {
  258. return isDiscardableIfUnused(Linkage);
  259. }
  260. bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
  261. bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
  262. /// Copy all additional attributes (those not needed to create a GlobalValue)
  263. /// from the GlobalValue Src to this one.
  264. virtual void copyAttributesFrom(const GlobalValue *Src);
  265. /// If special LLVM prefix that is used to inform the asm printer to not emit
  266. /// usual symbol prefix before the symbol name is used then return linkage
  267. /// name after skipping this special LLVM prefix.
  268. static StringRef getRealLinkageName(StringRef Name) {
  269. if (!Name.empty() && Name[0] == '\1')
  270. return Name.substr(1);
  271. return Name;
  272. }
  273. /// @name Materialization
  274. /// Materialization is used to construct functions only as they're needed. This
  275. /// is useful to reduce memory usage in LLVM or parsing work done by the
  276. /// BitcodeReader to load the Module.
  277. /// @{
  278. /// If this function's Module is being lazily streamed in functions from disk
  279. /// or some other source, this method can be used to check to see if the
  280. /// function has been read in yet or not.
  281. bool isMaterializable() const;
  282. /// Returns true if this function was loaded from a GVMaterializer that's
  283. /// still attached to its Module and that knows how to dematerialize the
  284. /// function.
  285. bool isDematerializable() const;
  286. /// Make sure this GlobalValue is fully read. If the module is corrupt, this
  287. /// returns true and fills in the optional string with information about the
  288. /// problem. If successful, this returns false.
  289. std::error_code materialize();
  290. /// If this GlobalValue is read in, and if the GVMaterializer supports it,
  291. /// release the memory for the function, and set it up to be materialized
  292. /// lazily. If !isDematerializable(), this method is a noop.
  293. void dematerialize();
  294. /// @}
  295. /// Return true if the primary definition of this global value is outside of
  296. /// the current translation unit.
  297. bool isDeclaration() const;
  298. bool isDeclarationForLinker() const {
  299. if (hasAvailableExternallyLinkage())
  300. return true;
  301. return isDeclaration();
  302. }
  303. /// Returns true if this global's definition will be the one chosen by the
  304. /// linker.
  305. bool isStrongDefinitionForLinker() const {
  306. return !(isDeclarationForLinker() || isWeakForLinker());
  307. }
  308. /// This method unlinks 'this' from the containing module, but does not delete
  309. /// it.
  310. virtual void removeFromParent() = 0;
  311. /// This method unlinks 'this' from the containing module and deletes it.
  312. virtual void eraseFromParent() = 0;
  313. /// Get the module that this global value is contained inside of...
  314. Module *getParent() { return Parent; }
  315. const Module *getParent() const { return Parent; }
  316. // Methods for support type inquiry through isa, cast, and dyn_cast:
  317. static bool classof(const Value *V) {
  318. return V->getValueID() == Value::FunctionVal ||
  319. V->getValueID() == Value::GlobalVariableVal ||
  320. V->getValueID() == Value::GlobalAliasVal;
  321. }
  322. };
  323. } // End llvm namespace
  324. #endif