GlobalValue.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/GlobalValue.h - Class to represent a global value --*- 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 is a common base class of all globally definable objects. As such,
  15. // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is
  16. // used because you can do certain things with these global objects that you
  17. // can't do to anything else. For example, use the address of one as a
  18. // constant.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_IR_GLOBALVALUE_H
  22. #define LLVM_IR_GLOBALVALUE_H
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/Twine.h"
  25. #include "llvm/IR/Constant.h"
  26. #include "llvm/IR/DerivedTypes.h"
  27. #include "llvm/IR/Value.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/MD5.h"
  31. #include <cassert>
  32. #include <cstdint>
  33. #include <string>
  34. namespace llvm {
  35. class Comdat;
  36. class ConstantRange;
  37. class Error;
  38. class GlobalObject;
  39. class Module;
  40. namespace Intrinsic {
  41. typedef unsigned ID;
  42. } // end namespace Intrinsic
  43. class GlobalValue : public Constant {
  44. public:
  45. /// An enumeration for the kinds of linkage for global values.
  46. enum LinkageTypes {
  47. ExternalLinkage = 0,///< Externally visible function
  48. AvailableExternallyLinkage, ///< Available for inspection, not emission.
  49. LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
  50. LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
  51. WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
  52. WeakODRLinkage, ///< Same, but only replaced by something equivalent.
  53. AppendingLinkage, ///< Special purpose, only applies to global arrays
  54. InternalLinkage, ///< Rename collisions when linking (static functions).
  55. PrivateLinkage, ///< Like Internal, but omit from symbol table.
  56. ExternalWeakLinkage,///< ExternalWeak linkage description.
  57. CommonLinkage ///< Tentative definitions.
  58. };
  59. /// An enumeration for the kinds of visibility of global values.
  60. enum VisibilityTypes {
  61. DefaultVisibility = 0, ///< The GV is visible
  62. HiddenVisibility, ///< The GV is hidden
  63. ProtectedVisibility ///< The GV is protected
  64. };
  65. /// Storage classes of global values for PE targets.
  66. enum DLLStorageClassTypes {
  67. DefaultStorageClass = 0,
  68. DLLImportStorageClass = 1, ///< Function to be imported from DLL
  69. DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
  70. };
  71. protected:
  72. GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
  73. LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace)
  74. : Constant(PointerType::get(Ty, AddressSpace), VTy, Ops, NumOps),
  75. ValueType(Ty), Visibility(DefaultVisibility),
  76. UnnamedAddrVal(unsigned(UnnamedAddr::None)),
  77. DllStorageClass(DefaultStorageClass), ThreadLocal(NotThreadLocal),
  78. HasLLVMReservedName(false), IsDSOLocal(false), HasPartition(false),
  79. HasSanitizerMetadata(false) {
  80. setLinkage(Linkage);
  81. setName(Name);
  82. }
  83. Type *ValueType;
  84. static const unsigned GlobalValueSubClassDataBits = 15;
  85. // All bitfields use unsigned as the underlying type so that MSVC will pack
  86. // them.
  87. unsigned Linkage : 4; // The linkage of this global
  88. unsigned Visibility : 2; // The visibility style of this global
  89. unsigned UnnamedAddrVal : 2; // This value's address is not significant
  90. unsigned DllStorageClass : 2; // DLL storage class
  91. unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
  92. // the desired model?
  93. /// True if the function's name starts with "llvm.". This corresponds to the
  94. /// value of Function::isIntrinsic(), which may be true even if
  95. /// Function::intrinsicID() returns Intrinsic::not_intrinsic.
  96. unsigned HasLLVMReservedName : 1;
  97. /// If true then there is a definition within the same linkage unit and that
  98. /// definition cannot be runtime preempted.
  99. unsigned IsDSOLocal : 1;
  100. /// True if this symbol has a partition name assigned (see
  101. /// https://lld.llvm.org/Partitions.html).
  102. unsigned HasPartition : 1;
  103. /// True if this symbol has sanitizer metadata available. Should only happen
  104. /// if sanitizers were enabled when building the translation unit which
  105. /// contains this GV.
  106. unsigned HasSanitizerMetadata : 1;
  107. private:
  108. // Give subclasses access to what otherwise would be wasted padding.
  109. // (15 + 4 + 2 + 2 + 2 + 3 + 1 + 1 + 1 + 1) == 32.
  110. unsigned SubClassData : GlobalValueSubClassDataBits;
  111. friend class Constant;
  112. void destroyConstantImpl();
  113. Value *handleOperandChangeImpl(Value *From, Value *To);
  114. /// Returns true if the definition of this global may be replaced by a
  115. /// differently optimized variant of the same source level function at link
  116. /// time.
  117. bool mayBeDerefined() const {
  118. switch (getLinkage()) {
  119. case WeakODRLinkage:
  120. case LinkOnceODRLinkage:
  121. case AvailableExternallyLinkage:
  122. return true;
  123. case WeakAnyLinkage:
  124. case LinkOnceAnyLinkage:
  125. case CommonLinkage:
  126. case ExternalWeakLinkage:
  127. case ExternalLinkage:
  128. case AppendingLinkage:
  129. case InternalLinkage:
  130. case PrivateLinkage:
  131. // Optimizations may assume builtin semantics for functions defined as
  132. // nobuiltin due to attributes at call-sites. To avoid applying IPO based
  133. // on nobuiltin semantics, treat such function definitions as maybe
  134. // derefined.
  135. return isInterposable() || isNobuiltinFnDef();
  136. }
  137. llvm_unreachable("Fully covered switch above!");
  138. }
  139. /// Returns true if the global is a function definition with the nobuiltin
  140. /// attribute.
  141. bool isNobuiltinFnDef() const;
  142. protected:
  143. /// The intrinsic ID for this subclass (which must be a Function).
  144. ///
  145. /// This member is defined by this class, but not used for anything.
  146. /// Subclasses can use it to store their intrinsic ID, if they have one.
  147. ///
  148. /// This is stored here to save space in Function on 64-bit hosts.
  149. Intrinsic::ID IntID = (Intrinsic::ID)0U;
  150. unsigned getGlobalValueSubClassData() const {
  151. return SubClassData;
  152. }
  153. void setGlobalValueSubClassData(unsigned V) {
  154. assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit");
  155. SubClassData = V;
  156. }
  157. Module *Parent = nullptr; // The containing module.
  158. // Used by SymbolTableListTraits.
  159. void setParent(Module *parent) {
  160. Parent = parent;
  161. }
  162. ~GlobalValue() {
  163. removeDeadConstantUsers(); // remove any dead constants using this.
  164. }
  165. public:
  166. enum ThreadLocalMode {
  167. NotThreadLocal = 0,
  168. GeneralDynamicTLSModel,
  169. LocalDynamicTLSModel,
  170. InitialExecTLSModel,
  171. LocalExecTLSModel
  172. };
  173. GlobalValue(const GlobalValue &) = delete;
  174. unsigned getAddressSpace() const {
  175. return getType()->getAddressSpace();
  176. }
  177. enum class UnnamedAddr {
  178. None,
  179. Local,
  180. Global,
  181. };
  182. bool hasGlobalUnnamedAddr() const {
  183. return getUnnamedAddr() == UnnamedAddr::Global;
  184. }
  185. /// Returns true if this value's address is not significant in this module.
  186. /// This attribute is intended to be used only by the code generator and LTO
  187. /// to allow the linker to decide whether the global needs to be in the symbol
  188. /// table. It should probably not be used in optimizations, as the value may
  189. /// have uses outside the module; use hasGlobalUnnamedAddr() instead.
  190. bool hasAtLeastLocalUnnamedAddr() const {
  191. return getUnnamedAddr() != UnnamedAddr::None;
  192. }
  193. UnnamedAddr getUnnamedAddr() const {
  194. return UnnamedAddr(UnnamedAddrVal);
  195. }
  196. void setUnnamedAddr(UnnamedAddr Val) { UnnamedAddrVal = unsigned(Val); }
  197. static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) {
  198. if (A == UnnamedAddr::None || B == UnnamedAddr::None)
  199. return UnnamedAddr::None;
  200. if (A == UnnamedAddr::Local || B == UnnamedAddr::Local)
  201. return UnnamedAddr::Local;
  202. return UnnamedAddr::Global;
  203. }
  204. bool hasComdat() const { return getComdat() != nullptr; }
  205. const Comdat *getComdat() const;
  206. Comdat *getComdat() {
  207. return const_cast<Comdat *>(
  208. static_cast<const GlobalValue *>(this)->getComdat());
  209. }
  210. VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
  211. bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
  212. bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
  213. bool hasProtectedVisibility() const {
  214. return Visibility == ProtectedVisibility;
  215. }
  216. void setVisibility(VisibilityTypes V) {
  217. assert((!hasLocalLinkage() || V == DefaultVisibility) &&
  218. "local linkage requires default visibility");
  219. Visibility = V;
  220. if (isImplicitDSOLocal())
  221. setDSOLocal(true);
  222. }
  223. /// If the value is "Thread Local", its value isn't shared by the threads.
  224. bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
  225. void setThreadLocal(bool Val) {
  226. setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
  227. }
  228. void setThreadLocalMode(ThreadLocalMode Val) {
  229. assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
  230. ThreadLocal = Val;
  231. }
  232. ThreadLocalMode getThreadLocalMode() const {
  233. return static_cast<ThreadLocalMode>(ThreadLocal);
  234. }
  235. DLLStorageClassTypes getDLLStorageClass() const {
  236. return DLLStorageClassTypes(DllStorageClass);
  237. }
  238. bool hasDLLImportStorageClass() const {
  239. return DllStorageClass == DLLImportStorageClass;
  240. }
  241. bool hasDLLExportStorageClass() const {
  242. return DllStorageClass == DLLExportStorageClass;
  243. }
  244. void setDLLStorageClass(DLLStorageClassTypes C) {
  245. assert((!hasLocalLinkage() || C == DefaultStorageClass) &&
  246. "local linkage requires DefaultStorageClass");
  247. DllStorageClass = C;
  248. }
  249. bool hasSection() const { return !getSection().empty(); }
  250. StringRef getSection() const;
  251. /// Global values are always pointers.
  252. PointerType *getType() const { return cast<PointerType>(User::getType()); }
  253. Type *getValueType() const { return ValueType; }
  254. bool isImplicitDSOLocal() const {
  255. return hasLocalLinkage() ||
  256. (!hasDefaultVisibility() && !hasExternalWeakLinkage());
  257. }
  258. void setDSOLocal(bool Local) { IsDSOLocal = Local; }
  259. bool isDSOLocal() const {
  260. return IsDSOLocal;
  261. }
  262. bool hasPartition() const {
  263. return HasPartition;
  264. }
  265. StringRef getPartition() const;
  266. void setPartition(StringRef Part);
  267. // ASan, HWASan and Memtag sanitizers have some instrumentation that applies
  268. // specifically to global variables.
  269. struct SanitizerMetadata {
  270. SanitizerMetadata()
  271. : NoAddress(false), NoHWAddress(false),
  272. Memtag(false), IsDynInit(false) {}
  273. // For ASan and HWASan, this instrumentation is implicitly applied to all
  274. // global variables when built with -fsanitize=*. What we need is a way to
  275. // persist the information that a certain global variable should *not* have
  276. // sanitizers applied, which occurs if:
  277. // 1. The global variable is in the sanitizer ignore list, or
  278. // 2. The global variable is created by the sanitizers itself for internal
  279. // usage, or
  280. // 3. The global variable has __attribute__((no_sanitize("..."))) or
  281. // __attribute__((disable_sanitizer_instrumentation)).
  282. //
  283. // This is important, a some IR passes like GlobalMerge can delete global
  284. // variables and replace them with new ones. If the old variables were
  285. // marked to be unsanitized, then the new ones should also be.
  286. unsigned NoAddress : 1;
  287. unsigned NoHWAddress : 1;
  288. // Memtag sanitization works differently: sanitization is requested by clang
  289. // when `-fsanitize=memtag-globals` is provided, and the request can be
  290. // denied (and the attribute removed) by the AArch64 global tagging pass if
  291. // it can't be fulfilled (e.g. the global variable is a TLS variable).
  292. // Memtag sanitization has to interact with other parts of LLVM (like
  293. // supressing certain optimisations, emitting assembly directives, or
  294. // creating special relocation sections).
  295. //
  296. // Use `GlobalValue::isTagged()` to check whether tagging should be enabled
  297. // for a global variable.
  298. unsigned Memtag : 1;
  299. // ASan-specific metadata. Is this global variable dynamically initialized
  300. // (from a C++ language perspective), and should therefore be checked for
  301. // ODR violations.
  302. unsigned IsDynInit : 1;
  303. };
  304. bool hasSanitizerMetadata() const { return HasSanitizerMetadata; }
  305. const SanitizerMetadata &getSanitizerMetadata() const;
  306. // Note: Not byref as it's a POD and otherwise it's too easy to call
  307. // G.setSanitizerMetadata(G2.getSanitizerMetadata()), and the argument becomes
  308. // dangling when the backing storage allocates the metadata for `G`, as the
  309. // storage is shared between `G1` and `G2`.
  310. void setSanitizerMetadata(SanitizerMetadata Meta);
  311. void removeSanitizerMetadata();
  312. bool isTagged() const {
  313. return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;
  314. }
  315. static LinkageTypes getLinkOnceLinkage(bool ODR) {
  316. return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
  317. }
  318. static LinkageTypes getWeakLinkage(bool ODR) {
  319. return ODR ? WeakODRLinkage : WeakAnyLinkage;
  320. }
  321. static bool isExternalLinkage(LinkageTypes Linkage) {
  322. return Linkage == ExternalLinkage;
  323. }
  324. static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
  325. return Linkage == AvailableExternallyLinkage;
  326. }
  327. static bool isLinkOnceAnyLinkage(LinkageTypes Linkage) {
  328. return Linkage == LinkOnceAnyLinkage;
  329. }
  330. static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
  331. return Linkage == LinkOnceODRLinkage;
  332. }
  333. static bool isLinkOnceLinkage(LinkageTypes Linkage) {
  334. return isLinkOnceAnyLinkage(Linkage) || isLinkOnceODRLinkage(Linkage);
  335. }
  336. static bool isWeakAnyLinkage(LinkageTypes Linkage) {
  337. return Linkage == WeakAnyLinkage;
  338. }
  339. static bool isWeakODRLinkage(LinkageTypes Linkage) {
  340. return Linkage == WeakODRLinkage;
  341. }
  342. static bool isWeakLinkage(LinkageTypes Linkage) {
  343. return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
  344. }
  345. static bool isAppendingLinkage(LinkageTypes Linkage) {
  346. return Linkage == AppendingLinkage;
  347. }
  348. static bool isInternalLinkage(LinkageTypes Linkage) {
  349. return Linkage == InternalLinkage;
  350. }
  351. static bool isPrivateLinkage(LinkageTypes Linkage) {
  352. return Linkage == PrivateLinkage;
  353. }
  354. static bool isLocalLinkage(LinkageTypes Linkage) {
  355. return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
  356. }
  357. static bool isExternalWeakLinkage(LinkageTypes Linkage) {
  358. return Linkage == ExternalWeakLinkage;
  359. }
  360. static bool isCommonLinkage(LinkageTypes Linkage) {
  361. return Linkage == CommonLinkage;
  362. }
  363. static bool isValidDeclarationLinkage(LinkageTypes Linkage) {
  364. return isExternalWeakLinkage(Linkage) || isExternalLinkage(Linkage);
  365. }
  366. /// Whether the definition of this global may be replaced by something
  367. /// non-equivalent at link time. For example, if a function has weak linkage
  368. /// then the code defining it may be replaced by different code.
  369. static bool isInterposableLinkage(LinkageTypes Linkage) {
  370. switch (Linkage) {
  371. case WeakAnyLinkage:
  372. case LinkOnceAnyLinkage:
  373. case CommonLinkage:
  374. case ExternalWeakLinkage:
  375. return true;
  376. case AvailableExternallyLinkage:
  377. case LinkOnceODRLinkage:
  378. case WeakODRLinkage:
  379. // The above three cannot be overridden but can be de-refined.
  380. case ExternalLinkage:
  381. case AppendingLinkage:
  382. case InternalLinkage:
  383. case PrivateLinkage:
  384. return false;
  385. }
  386. llvm_unreachable("Fully covered switch above!");
  387. }
  388. /// Whether the definition of this global may be discarded if it is not used
  389. /// in its compilation unit.
  390. static bool isDiscardableIfUnused(LinkageTypes Linkage) {
  391. return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage) ||
  392. isAvailableExternallyLinkage(Linkage);
  393. }
  394. /// Whether the definition of this global may be replaced at link time. NB:
  395. /// Using this method outside of the code generators is almost always a
  396. /// mistake: when working at the IR level use isInterposable instead as it
  397. /// knows about ODR semantics.
  398. static bool isWeakForLinker(LinkageTypes Linkage) {
  399. return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage ||
  400. Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
  401. Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
  402. }
  403. /// Return true if the currently visible definition of this global (if any) is
  404. /// exactly the definition we will see at runtime.
  405. ///
  406. /// Non-exact linkage types inhibits most non-inlining IPO, since a
  407. /// differently optimized variant of the same function can have different
  408. /// observable or undefined behavior than in the variant currently visible.
  409. /// For instance, we could have started with
  410. ///
  411. /// void foo(int *v) {
  412. /// int t = 5 / v[0];
  413. /// (void) t;
  414. /// }
  415. ///
  416. /// and "refined" it to
  417. ///
  418. /// void foo(int *v) { }
  419. ///
  420. /// However, we cannot infer readnone for `foo`, since that would justify
  421. /// DSE'ing a store to `v[0]` across a call to `foo`, which can cause
  422. /// undefined behavior if the linker replaces the actual call destination with
  423. /// the unoptimized `foo`.
  424. ///
  425. /// Inlining is okay across non-exact linkage types as long as they're not
  426. /// interposable (see \c isInterposable), since in such cases the currently
  427. /// visible variant is *a* correct implementation of the original source
  428. /// function; it just isn't the *only* correct implementation.
  429. bool isDefinitionExact() const {
  430. return !mayBeDerefined();
  431. }
  432. /// Return true if this global has an exact defintion.
  433. bool hasExactDefinition() const {
  434. // While this computes exactly the same thing as
  435. // isStrongDefinitionForLinker, the intended uses are different. This
  436. // function is intended to help decide if specific inter-procedural
  437. // transforms are correct, while isStrongDefinitionForLinker's intended use
  438. // is in low level code generation.
  439. return !isDeclaration() && isDefinitionExact();
  440. }
  441. /// Return true if this global's definition can be substituted with an
  442. /// *arbitrary* definition at link time or load time. We cannot do any IPO or
  443. /// inlining across interposable call edges, since the callee can be
  444. /// replaced with something arbitrary.
  445. bool isInterposable() const;
  446. bool canBenefitFromLocalAlias() const;
  447. bool hasExternalLinkage() const { return isExternalLinkage(getLinkage()); }
  448. bool hasAvailableExternallyLinkage() const {
  449. return isAvailableExternallyLinkage(getLinkage());
  450. }
  451. bool hasLinkOnceLinkage() const { return isLinkOnceLinkage(getLinkage()); }
  452. bool hasLinkOnceAnyLinkage() const {
  453. return isLinkOnceAnyLinkage(getLinkage());
  454. }
  455. bool hasLinkOnceODRLinkage() const {
  456. return isLinkOnceODRLinkage(getLinkage());
  457. }
  458. bool hasWeakLinkage() const { return isWeakLinkage(getLinkage()); }
  459. bool hasWeakAnyLinkage() const { return isWeakAnyLinkage(getLinkage()); }
  460. bool hasWeakODRLinkage() const { return isWeakODRLinkage(getLinkage()); }
  461. bool hasAppendingLinkage() const { return isAppendingLinkage(getLinkage()); }
  462. bool hasInternalLinkage() const { return isInternalLinkage(getLinkage()); }
  463. bool hasPrivateLinkage() const { return isPrivateLinkage(getLinkage()); }
  464. bool hasLocalLinkage() const { return isLocalLinkage(getLinkage()); }
  465. bool hasExternalWeakLinkage() const {
  466. return isExternalWeakLinkage(getLinkage());
  467. }
  468. bool hasCommonLinkage() const { return isCommonLinkage(getLinkage()); }
  469. bool hasValidDeclarationLinkage() const {
  470. return isValidDeclarationLinkage(getLinkage());
  471. }
  472. void setLinkage(LinkageTypes LT) {
  473. if (isLocalLinkage(LT)) {
  474. Visibility = DefaultVisibility;
  475. DllStorageClass = DefaultStorageClass;
  476. }
  477. Linkage = LT;
  478. if (isImplicitDSOLocal())
  479. setDSOLocal(true);
  480. }
  481. LinkageTypes getLinkage() const { return LinkageTypes(Linkage); }
  482. bool isDiscardableIfUnused() const {
  483. return isDiscardableIfUnused(getLinkage());
  484. }
  485. bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); }
  486. protected:
  487. /// Copy all additional attributes (those not needed to create a GlobalValue)
  488. /// from the GlobalValue Src to this one.
  489. void copyAttributesFrom(const GlobalValue *Src);
  490. public:
  491. /// If the given string begins with the GlobalValue name mangling escape
  492. /// character '\1', drop it.
  493. ///
  494. /// This function applies a specific mangling that is used in PGO profiles,
  495. /// among other things. If you're trying to get a symbol name for an
  496. /// arbitrary GlobalValue, this is not the function you're looking for; see
  497. /// Mangler.h.
  498. static StringRef dropLLVMManglingEscape(StringRef Name) {
  499. if (!Name.empty() && Name[0] == '\1')
  500. return Name.substr(1);
  501. return Name;
  502. }
  503. /// Return the modified name for a global value suitable to be
  504. /// used as the key for a global lookup (e.g. profile or ThinLTO).
  505. /// The value's original name is \c Name and has linkage of type
  506. /// \c Linkage. The value is defined in module \c FileName.
  507. static std::string getGlobalIdentifier(StringRef Name,
  508. GlobalValue::LinkageTypes Linkage,
  509. StringRef FileName);
  510. /// Return the modified name for this global value suitable to be
  511. /// used as the key for a global lookup (e.g. profile or ThinLTO).
  512. std::string getGlobalIdentifier() const;
  513. /// Declare a type to represent a global unique identifier for a global value.
  514. /// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
  515. /// unique way to identify a symbol.
  516. using GUID = uint64_t;
  517. /// Return a 64-bit global unique ID constructed from global value name
  518. /// (i.e. returned by getGlobalIdentifier()).
  519. static GUID getGUID(StringRef GlobalName) { return MD5Hash(GlobalName); }
  520. /// Return a 64-bit global unique ID constructed from global value name
  521. /// (i.e. returned by getGlobalIdentifier()).
  522. GUID getGUID() const { return getGUID(getGlobalIdentifier()); }
  523. /// @name Materialization
  524. /// Materialization is used to construct functions only as they're needed.
  525. /// This
  526. /// is useful to reduce memory usage in LLVM or parsing work done by the
  527. /// BitcodeReader to load the Module.
  528. /// @{
  529. /// If this function's Module is being lazily streamed in functions from disk
  530. /// or some other source, this method can be used to check to see if the
  531. /// function has been read in yet or not.
  532. bool isMaterializable() const;
  533. /// Make sure this GlobalValue is fully read.
  534. Error materialize();
  535. /// @}
  536. /// Return true if the primary definition of this global value is outside of
  537. /// the current translation unit.
  538. bool isDeclaration() const;
  539. bool isDeclarationForLinker() const {
  540. if (hasAvailableExternallyLinkage())
  541. return true;
  542. return isDeclaration();
  543. }
  544. /// Returns true if this global's definition will be the one chosen by the
  545. /// linker.
  546. ///
  547. /// NB! Ideally this should not be used at the IR level at all. If you're
  548. /// interested in optimization constraints implied by the linker's ability to
  549. /// choose an implementation, prefer using \c hasExactDefinition.
  550. bool isStrongDefinitionForLinker() const {
  551. return !(isDeclarationForLinker() || isWeakForLinker());
  552. }
  553. const GlobalObject *getAliaseeObject() const;
  554. GlobalObject *getAliaseeObject() {
  555. return const_cast<GlobalObject *>(
  556. static_cast<const GlobalValue *>(this)->getAliaseeObject());
  557. }
  558. /// Returns whether this is a reference to an absolute symbol.
  559. bool isAbsoluteSymbolRef() const;
  560. /// If this is an absolute symbol reference, returns the range of the symbol,
  561. /// otherwise returns std::nullopt.
  562. std::optional<ConstantRange> getAbsoluteSymbolRange() const;
  563. /// This method unlinks 'this' from the containing module, but does not delete
  564. /// it.
  565. void removeFromParent();
  566. /// This method unlinks 'this' from the containing module and deletes it.
  567. void eraseFromParent();
  568. /// Get the module that this global value is contained inside of...
  569. Module *getParent() { return Parent; }
  570. const Module *getParent() const { return Parent; }
  571. // Methods for support type inquiry through isa, cast, and dyn_cast:
  572. static bool classof(const Value *V) {
  573. return V->getValueID() == Value::FunctionVal ||
  574. V->getValueID() == Value::GlobalVariableVal ||
  575. V->getValueID() == Value::GlobalAliasVal ||
  576. V->getValueID() == Value::GlobalIFuncVal;
  577. }
  578. /// True if GV can be left out of the object symbol table. This is the case
  579. /// for linkonce_odr values whose address is not significant. While legal, it
  580. /// is not normally profitable to omit them from the .o symbol table. Using
  581. /// this analysis makes sense when the information can be passed down to the
  582. /// linker or we are in LTO.
  583. bool canBeOmittedFromSymbolTable() const;
  584. };
  585. } // end namespace llvm
  586. #endif // LLVM_IR_GLOBALVALUE_H
  587. #ifdef __GNUC__
  588. #pragma GCC diagnostic pop
  589. #endif