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