Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:01

0001 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file is a common base class of all globally definable objects.  As such,
0010 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
0011 // used because you can do certain things with these global objects that you
0012 // can't do to anything else.  For example, use the address of one as a
0013 // constant.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_IR_GLOBALVALUE_H
0018 #define LLVM_IR_GLOBALVALUE_H
0019 
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/ADT/Twine.h"
0022 #include "llvm/IR/Constant.h"
0023 #include "llvm/IR/DerivedTypes.h"
0024 #include "llvm/IR/Value.h"
0025 #include "llvm/Support/Casting.h"
0026 #include "llvm/Support/ErrorHandling.h"
0027 #include <cassert>
0028 #include <cstdint>
0029 #include <string>
0030 
0031 namespace llvm {
0032 
0033 class Comdat;
0034 class ConstantRange;
0035 class DataLayout;
0036 class Error;
0037 class GlobalObject;
0038 class Module;
0039 
0040 namespace Intrinsic {
0041 typedef unsigned ID;
0042 } // end namespace Intrinsic
0043 
0044 // Choose ';' as the delimiter. ':' was used once but it doesn't work well for
0045 // Objective-C functions which commonly have :'s in their names.
0046 inline constexpr char GlobalIdentifierDelimiter = ';';
0047 
0048 class GlobalValue : public Constant {
0049 public:
0050   /// An enumeration for the kinds of linkage for global values.
0051   enum LinkageTypes {
0052     ExternalLinkage = 0,///< Externally visible function
0053     AvailableExternallyLinkage, ///< Available for inspection, not emission.
0054     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
0055     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
0056     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
0057     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
0058     AppendingLinkage,   ///< Special purpose, only applies to global arrays
0059     InternalLinkage,    ///< Rename collisions when linking (static functions).
0060     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
0061     ExternalWeakLinkage,///< ExternalWeak linkage description.
0062     CommonLinkage       ///< Tentative definitions.
0063   };
0064 
0065   /// An enumeration for the kinds of visibility of global values.
0066   enum VisibilityTypes {
0067     DefaultVisibility = 0,  ///< The GV is visible
0068     HiddenVisibility,       ///< The GV is hidden
0069     ProtectedVisibility     ///< The GV is protected
0070   };
0071 
0072   /// Storage classes of global values for PE targets.
0073   enum DLLStorageClassTypes {
0074     DefaultStorageClass   = 0,
0075     DLLImportStorageClass = 1, ///< Function to be imported from DLL
0076     DLLExportStorageClass = 2  ///< Function to be accessible from DLL.
0077   };
0078 
0079 protected:
0080   GlobalValue(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage,
0081               const Twine &Name, unsigned AddressSpace)
0082       : Constant(PointerType::get(Ty->getContext(), AddressSpace), VTy,
0083                  AllocInfo),
0084         ValueType(Ty), Visibility(DefaultVisibility),
0085         UnnamedAddrVal(unsigned(UnnamedAddr::None)),
0086         DllStorageClass(DefaultStorageClass), ThreadLocal(NotThreadLocal),
0087         HasLLVMReservedName(false), IsDSOLocal(false), HasPartition(false),
0088         HasSanitizerMetadata(false) {
0089     setLinkage(Linkage);
0090     setName(Name);
0091   }
0092 
0093   Type *ValueType;
0094 
0095   static const unsigned GlobalValueSubClassDataBits = 15;
0096 
0097   // All bitfields use unsigned as the underlying type so that MSVC will pack
0098   // them.
0099   unsigned Linkage : 4;       // The linkage of this global
0100   unsigned Visibility : 2;    // The visibility style of this global
0101   unsigned UnnamedAddrVal : 2; // This value's address is not significant
0102   unsigned DllStorageClass : 2; // DLL storage class
0103 
0104   unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
0105                             // the desired model?
0106 
0107   /// True if the function's name starts with "llvm.".  This corresponds to the
0108   /// value of Function::isIntrinsic(), which may be true even if
0109   /// Function::intrinsicID() returns Intrinsic::not_intrinsic.
0110   unsigned HasLLVMReservedName : 1;
0111 
0112   /// If true then there is a definition within the same linkage unit and that
0113   /// definition cannot be runtime preempted.
0114   unsigned IsDSOLocal : 1;
0115 
0116   /// True if this symbol has a partition name assigned (see
0117   /// https://lld.llvm.org/Partitions.html).
0118   unsigned HasPartition : 1;
0119 
0120   /// True if this symbol has sanitizer metadata available. Should only happen
0121   /// if sanitizers were enabled when building the translation unit which
0122   /// contains this GV.
0123   unsigned HasSanitizerMetadata : 1;
0124 
0125 private:
0126   // Give subclasses access to what otherwise would be wasted padding.
0127   // (15 + 4 + 2 + 2 + 2 + 3 + 1 + 1 + 1 + 1) == 32.
0128   unsigned SubClassData : GlobalValueSubClassDataBits;
0129 
0130   friend class Constant;
0131 
0132   void destroyConstantImpl();
0133   Value *handleOperandChangeImpl(Value *From, Value *To);
0134 
0135   /// Returns true if the definition of this global may be replaced by a
0136   /// differently optimized variant of the same source level function at link
0137   /// time.
0138   bool mayBeDerefined() const {
0139     switch (getLinkage()) {
0140     case WeakODRLinkage:
0141     case LinkOnceODRLinkage:
0142     case AvailableExternallyLinkage:
0143       return true;
0144 
0145     case WeakAnyLinkage:
0146     case LinkOnceAnyLinkage:
0147     case CommonLinkage:
0148     case ExternalWeakLinkage:
0149     case ExternalLinkage:
0150     case AppendingLinkage:
0151     case InternalLinkage:
0152     case PrivateLinkage:
0153       // Optimizations may assume builtin semantics for functions defined as
0154       // nobuiltin due to attributes at call-sites. To avoid applying IPO based
0155       // on nobuiltin semantics, treat such function definitions as maybe
0156       // derefined.
0157       return isInterposable() || isNobuiltinFnDef();
0158     }
0159 
0160     llvm_unreachable("Fully covered switch above!");
0161   }
0162 
0163   /// Returns true if the global is a function definition with the nobuiltin
0164   /// attribute.
0165   bool isNobuiltinFnDef() const;
0166 
0167 protected:
0168   /// The intrinsic ID for this subclass (which must be a Function).
0169   ///
0170   /// This member is defined by this class, but not used for anything.
0171   /// Subclasses can use it to store their intrinsic ID, if they have one.
0172   ///
0173   /// This is stored here to save space in Function on 64-bit hosts.
0174   Intrinsic::ID IntID = (Intrinsic::ID)0U;
0175 
0176   unsigned getGlobalValueSubClassData() const {
0177     return SubClassData;
0178   }
0179   void setGlobalValueSubClassData(unsigned V) {
0180     assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit");
0181     SubClassData = V;
0182   }
0183 
0184   Module *Parent = nullptr; // The containing module.
0185 
0186   // Used by SymbolTableListTraits.
0187   void setParent(Module *parent) {
0188     Parent = parent;
0189   }
0190 
0191   ~GlobalValue() {
0192     removeDeadConstantUsers();   // remove any dead constants using this.
0193   }
0194 
0195 public:
0196   enum ThreadLocalMode {
0197     NotThreadLocal = 0,
0198     GeneralDynamicTLSModel,
0199     LocalDynamicTLSModel,
0200     InitialExecTLSModel,
0201     LocalExecTLSModel
0202   };
0203 
0204   GlobalValue(const GlobalValue &) = delete;
0205 
0206   unsigned getAddressSpace() const {
0207     return getType()->getAddressSpace();
0208   }
0209 
0210   enum class UnnamedAddr {
0211     None,
0212     Local,
0213     Global,
0214   };
0215 
0216   bool hasGlobalUnnamedAddr() const {
0217     return getUnnamedAddr() == UnnamedAddr::Global;
0218   }
0219 
0220   /// Returns true if this value's address is not significant in this module.
0221   /// This attribute is intended to be used only by the code generator and LTO
0222   /// to allow the linker to decide whether the global needs to be in the symbol
0223   /// table. It should probably not be used in optimizations, as the value may
0224   /// have uses outside the module; use hasGlobalUnnamedAddr() instead.
0225   bool hasAtLeastLocalUnnamedAddr() const {
0226     return getUnnamedAddr() != UnnamedAddr::None;
0227   }
0228 
0229   UnnamedAddr getUnnamedAddr() const {
0230     return UnnamedAddr(UnnamedAddrVal);
0231   }
0232   void setUnnamedAddr(UnnamedAddr Val) { UnnamedAddrVal = unsigned(Val); }
0233 
0234   static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) {
0235     if (A == UnnamedAddr::None || B == UnnamedAddr::None)
0236       return UnnamedAddr::None;
0237     if (A == UnnamedAddr::Local || B == UnnamedAddr::Local)
0238       return UnnamedAddr::Local;
0239     return UnnamedAddr::Global;
0240   }
0241 
0242   bool hasComdat() const { return getComdat() != nullptr; }
0243   const Comdat *getComdat() const;
0244   Comdat *getComdat() {
0245     return const_cast<Comdat *>(
0246                            static_cast<const GlobalValue *>(this)->getComdat());
0247   }
0248 
0249   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
0250   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
0251   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
0252   bool hasProtectedVisibility() const {
0253     return Visibility == ProtectedVisibility;
0254   }
0255   void setVisibility(VisibilityTypes V) {
0256     assert((!hasLocalLinkage() || V == DefaultVisibility) &&
0257            "local linkage requires default visibility");
0258     Visibility = V;
0259     if (isImplicitDSOLocal())
0260       setDSOLocal(true);
0261   }
0262 
0263   /// If the value is "Thread Local", its value isn't shared by the threads.
0264   bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
0265   void setThreadLocal(bool Val) {
0266     setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
0267   }
0268   void setThreadLocalMode(ThreadLocalMode Val) {
0269     assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
0270     ThreadLocal = Val;
0271   }
0272   ThreadLocalMode getThreadLocalMode() const {
0273     return static_cast<ThreadLocalMode>(ThreadLocal);
0274   }
0275 
0276   DLLStorageClassTypes getDLLStorageClass() const {
0277     return DLLStorageClassTypes(DllStorageClass);
0278   }
0279   bool hasDLLImportStorageClass() const {
0280     return DllStorageClass == DLLImportStorageClass;
0281   }
0282   bool hasDLLExportStorageClass() const {
0283     return DllStorageClass == DLLExportStorageClass;
0284   }
0285   void setDLLStorageClass(DLLStorageClassTypes C) {
0286     assert((!hasLocalLinkage() || C == DefaultStorageClass) &&
0287            "local linkage requires DefaultStorageClass");
0288     DllStorageClass = C;
0289   }
0290 
0291   bool hasSection() const { return !getSection().empty(); }
0292   StringRef getSection() const;
0293 
0294   /// Global values are always pointers.
0295   PointerType *getType() const { return cast<PointerType>(User::getType()); }
0296 
0297   Type *getValueType() const { return ValueType; }
0298 
0299   bool isImplicitDSOLocal() const {
0300     return hasLocalLinkage() ||
0301            (!hasDefaultVisibility() && !hasExternalWeakLinkage());
0302   }
0303 
0304   void setDSOLocal(bool Local) { IsDSOLocal = Local; }
0305 
0306   bool isDSOLocal() const {
0307     return IsDSOLocal;
0308   }
0309 
0310   bool hasPartition() const {
0311     return HasPartition;
0312   }
0313   StringRef getPartition() const;
0314   void setPartition(StringRef Part);
0315 
0316   // ASan, HWASan and Memtag sanitizers have some instrumentation that applies
0317   // specifically to global variables.
0318   struct SanitizerMetadata {
0319     SanitizerMetadata()
0320         : NoAddress(false), NoHWAddress(false),
0321           Memtag(false), IsDynInit(false) {}
0322     // For ASan and HWASan, this instrumentation is implicitly applied to all
0323     // global variables when built with -fsanitize=*. What we need is a way to
0324     // persist the information that a certain global variable should *not* have
0325     // sanitizers applied, which occurs if:
0326     //   1. The global variable is in the sanitizer ignore list, or
0327     //   2. The global variable is created by the sanitizers itself for internal
0328     //      usage, or
0329     //   3. The global variable has __attribute__((no_sanitize("..."))) or
0330     //      __attribute__((disable_sanitizer_instrumentation)).
0331     //
0332     // This is important, a some IR passes like GlobalMerge can delete global
0333     // variables and replace them with new ones. If the old variables were
0334     // marked to be unsanitized, then the new ones should also be.
0335     unsigned NoAddress : 1;
0336     unsigned NoHWAddress : 1;
0337 
0338     // Memtag sanitization works differently: sanitization is requested by clang
0339     // when `-fsanitize=memtag-globals` is provided, and the request can be
0340     // denied (and the attribute removed) by the AArch64 global tagging pass if
0341     // it can't be fulfilled (e.g. the global variable is a TLS variable).
0342     // Memtag sanitization has to interact with other parts of LLVM (like
0343     // supressing certain optimisations, emitting assembly directives, or
0344     // creating special relocation sections).
0345     //
0346     // Use `GlobalValue::isTagged()` to check whether tagging should be enabled
0347     // for a global variable.
0348     unsigned Memtag : 1;
0349 
0350     // ASan-specific metadata. Is this global variable dynamically initialized
0351     // (from a C++ language perspective), and should therefore be checked for
0352     // ODR violations.
0353     unsigned IsDynInit : 1;
0354   };
0355 
0356   bool hasSanitizerMetadata() const { return HasSanitizerMetadata; }
0357   const SanitizerMetadata &getSanitizerMetadata() const;
0358   // Note: Not byref as it's a POD and otherwise it's too easy to call
0359   // G.setSanitizerMetadata(G2.getSanitizerMetadata()), and the argument becomes
0360   // dangling when the backing storage allocates the metadata for `G`, as the
0361   // storage is shared between `G1` and `G2`.
0362   void setSanitizerMetadata(SanitizerMetadata Meta);
0363   void removeSanitizerMetadata();
0364   void setNoSanitizeMetadata();
0365 
0366   bool isTagged() const {
0367     return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;
0368   }
0369 
0370   static LinkageTypes getLinkOnceLinkage(bool ODR) {
0371     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
0372   }
0373   static LinkageTypes getWeakLinkage(bool ODR) {
0374     return ODR ? WeakODRLinkage : WeakAnyLinkage;
0375   }
0376 
0377   static bool isExternalLinkage(LinkageTypes Linkage) {
0378     return Linkage == ExternalLinkage;
0379   }
0380   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
0381     return Linkage == AvailableExternallyLinkage;
0382   }
0383   static bool isLinkOnceAnyLinkage(LinkageTypes Linkage) {
0384     return Linkage == LinkOnceAnyLinkage;
0385   }
0386   static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
0387     return Linkage == LinkOnceODRLinkage;
0388   }
0389   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
0390     return isLinkOnceAnyLinkage(Linkage) || isLinkOnceODRLinkage(Linkage);
0391   }
0392   static bool isWeakAnyLinkage(LinkageTypes Linkage) {
0393     return Linkage == WeakAnyLinkage;
0394   }
0395   static bool isWeakODRLinkage(LinkageTypes Linkage) {
0396     return Linkage == WeakODRLinkage;
0397   }
0398   static bool isWeakLinkage(LinkageTypes Linkage) {
0399     return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
0400   }
0401   static bool isAppendingLinkage(LinkageTypes Linkage) {
0402     return Linkage == AppendingLinkage;
0403   }
0404   static bool isInternalLinkage(LinkageTypes Linkage) {
0405     return Linkage == InternalLinkage;
0406   }
0407   static bool isPrivateLinkage(LinkageTypes Linkage) {
0408     return Linkage == PrivateLinkage;
0409   }
0410   static bool isLocalLinkage(LinkageTypes Linkage) {
0411     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
0412   }
0413   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
0414     return Linkage == ExternalWeakLinkage;
0415   }
0416   static bool isCommonLinkage(LinkageTypes Linkage) {
0417     return Linkage == CommonLinkage;
0418   }
0419   static bool isValidDeclarationLinkage(LinkageTypes Linkage) {
0420     return isExternalWeakLinkage(Linkage) || isExternalLinkage(Linkage);
0421   }
0422 
0423   /// Whether the definition of this global may be replaced by something
0424   /// non-equivalent at link time. For example, if a function has weak linkage
0425   /// then the code defining it may be replaced by different code.
0426   static bool isInterposableLinkage(LinkageTypes Linkage) {
0427     switch (Linkage) {
0428     case WeakAnyLinkage:
0429     case LinkOnceAnyLinkage:
0430     case CommonLinkage:
0431     case ExternalWeakLinkage:
0432       return true;
0433 
0434     case AvailableExternallyLinkage:
0435     case LinkOnceODRLinkage:
0436     case WeakODRLinkage:
0437     // The above three cannot be overridden but can be de-refined.
0438 
0439     case ExternalLinkage:
0440     case AppendingLinkage:
0441     case InternalLinkage:
0442     case PrivateLinkage:
0443       return false;
0444     }
0445     llvm_unreachable("Fully covered switch above!");
0446   }
0447 
0448   /// Whether the definition of this global may be discarded if it is not used
0449   /// in its compilation unit.
0450   static bool isDiscardableIfUnused(LinkageTypes Linkage) {
0451     return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage) ||
0452            isAvailableExternallyLinkage(Linkage);
0453   }
0454 
0455   /// Whether the definition of this global may be replaced at link time.  NB:
0456   /// Using this method outside of the code generators is almost always a
0457   /// mistake: when working at the IR level use isInterposable instead as it
0458   /// knows about ODR semantics.
0459   static bool isWeakForLinker(LinkageTypes Linkage)  {
0460     return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage ||
0461            Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
0462            Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
0463   }
0464 
0465   /// Return true if the currently visible definition of this global (if any) is
0466   /// exactly the definition we will see at runtime.
0467   ///
0468   /// Non-exact linkage types inhibits most non-inlining IPO, since a
0469   /// differently optimized variant of the same function can have different
0470   /// observable or undefined behavior than in the variant currently visible.
0471   /// For instance, we could have started with
0472   ///
0473   ///   void foo(int *v) {
0474   ///     int t = 5 / v[0];
0475   ///     (void) t;
0476   ///   }
0477   ///
0478   /// and "refined" it to
0479   ///
0480   ///   void foo(int *v) { }
0481   ///
0482   /// However, we cannot infer readnone for `foo`, since that would justify
0483   /// DSE'ing a store to `v[0]` across a call to `foo`, which can cause
0484   /// undefined behavior if the linker replaces the actual call destination with
0485   /// the unoptimized `foo`.
0486   ///
0487   /// Inlining is okay across non-exact linkage types as long as they're not
0488   /// interposable (see \c isInterposable), since in such cases the currently
0489   /// visible variant is *a* correct implementation of the original source
0490   /// function; it just isn't the *only* correct implementation.
0491   bool isDefinitionExact() const {
0492     return !mayBeDerefined();
0493   }
0494 
0495   /// Return true if this global has an exact defintion.
0496   bool hasExactDefinition() const {
0497     // While this computes exactly the same thing as
0498     // isStrongDefinitionForLinker, the intended uses are different.  This
0499     // function is intended to help decide if specific inter-procedural
0500     // transforms are correct, while isStrongDefinitionForLinker's intended use
0501     // is in low level code generation.
0502     return !isDeclaration() && isDefinitionExact();
0503   }
0504 
0505   /// Return true if this global's definition can be substituted with an
0506   /// *arbitrary* definition at link time or load time. We cannot do any IPO or
0507   /// inlining across interposable call edges, since the callee can be
0508   /// replaced with something arbitrary.
0509   bool isInterposable() const;
0510   bool canBenefitFromLocalAlias() const;
0511 
0512   bool hasExternalLinkage() const { return isExternalLinkage(getLinkage()); }
0513   bool hasAvailableExternallyLinkage() const {
0514     return isAvailableExternallyLinkage(getLinkage());
0515   }
0516   bool hasLinkOnceLinkage() const { return isLinkOnceLinkage(getLinkage()); }
0517   bool hasLinkOnceAnyLinkage() const {
0518     return isLinkOnceAnyLinkage(getLinkage());
0519   }
0520   bool hasLinkOnceODRLinkage() const {
0521     return isLinkOnceODRLinkage(getLinkage());
0522   }
0523   bool hasWeakLinkage() const { return isWeakLinkage(getLinkage()); }
0524   bool hasWeakAnyLinkage() const { return isWeakAnyLinkage(getLinkage()); }
0525   bool hasWeakODRLinkage() const { return isWeakODRLinkage(getLinkage()); }
0526   bool hasAppendingLinkage() const { return isAppendingLinkage(getLinkage()); }
0527   bool hasInternalLinkage() const { return isInternalLinkage(getLinkage()); }
0528   bool hasPrivateLinkage() const { return isPrivateLinkage(getLinkage()); }
0529   bool hasLocalLinkage() const { return isLocalLinkage(getLinkage()); }
0530   bool hasExternalWeakLinkage() const {
0531     return isExternalWeakLinkage(getLinkage());
0532   }
0533   bool hasCommonLinkage() const { return isCommonLinkage(getLinkage()); }
0534   bool hasValidDeclarationLinkage() const {
0535     return isValidDeclarationLinkage(getLinkage());
0536   }
0537 
0538   void setLinkage(LinkageTypes LT) {
0539     if (isLocalLinkage(LT)) {
0540       Visibility = DefaultVisibility;
0541       DllStorageClass = DefaultStorageClass;
0542     }
0543     Linkage = LT;
0544     if (isImplicitDSOLocal())
0545       setDSOLocal(true);
0546   }
0547   LinkageTypes getLinkage() const { return LinkageTypes(Linkage); }
0548 
0549   bool isDiscardableIfUnused() const {
0550     return isDiscardableIfUnused(getLinkage());
0551   }
0552 
0553   bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); }
0554 
0555 protected:
0556   /// Copy all additional attributes (those not needed to create a GlobalValue)
0557   /// from the GlobalValue Src to this one.
0558   void copyAttributesFrom(const GlobalValue *Src);
0559 
0560 public:
0561   /// If the given string begins with the GlobalValue name mangling escape
0562   /// character '\1', drop it.
0563   ///
0564   /// This function applies a specific mangling that is used in PGO profiles,
0565   /// among other things. If you're trying to get a symbol name for an
0566   /// arbitrary GlobalValue, this is not the function you're looking for; see
0567   /// Mangler.h.
0568   static StringRef dropLLVMManglingEscape(StringRef Name) {
0569     Name.consume_front("\1");
0570     return Name;
0571   }
0572 
0573   /// Return the modified name for a global value suitable to be
0574   /// used as the key for a global lookup (e.g. profile or ThinLTO).
0575   /// The value's original name is \c Name and has linkage of type
0576   /// \c Linkage. The value is defined in module \c FileName.
0577   static std::string getGlobalIdentifier(StringRef Name,
0578                                          GlobalValue::LinkageTypes Linkage,
0579                                          StringRef FileName);
0580 
0581   /// Return the modified name for this global value suitable to be
0582   /// used as the key for a global lookup (e.g. profile or ThinLTO).
0583   std::string getGlobalIdentifier() const;
0584 
0585   /// Declare a type to represent a global unique identifier for a global value.
0586   /// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
0587   /// unique way to identify a symbol.
0588   using GUID = uint64_t;
0589 
0590   /// Return a 64-bit global unique ID constructed from global value name
0591   /// (i.e. returned by getGlobalIdentifier()).
0592   static GUID getGUID(StringRef GlobalName);
0593 
0594   /// Return a 64-bit global unique ID constructed from global value name
0595   /// (i.e. returned by getGlobalIdentifier()).
0596   GUID getGUID() const { return getGUID(getGlobalIdentifier()); }
0597 
0598   /// @name Materialization
0599   /// Materialization is used to construct functions only as they're needed.
0600   /// This
0601   /// is useful to reduce memory usage in LLVM or parsing work done by the
0602   /// BitcodeReader to load the Module.
0603   /// @{
0604 
0605   /// If this function's Module is being lazily streamed in functions from disk
0606   /// or some other source, this method can be used to check to see if the
0607   /// function has been read in yet or not.
0608   bool isMaterializable() const;
0609 
0610   /// Make sure this GlobalValue is fully read.
0611   Error materialize();
0612 
0613 /// @}
0614 
0615   /// Return true if the primary definition of this global value is outside of
0616   /// the current translation unit.
0617   bool isDeclaration() const;
0618 
0619   bool isDeclarationForLinker() const {
0620     if (hasAvailableExternallyLinkage())
0621       return true;
0622 
0623     return isDeclaration();
0624   }
0625 
0626   /// Returns true if this global's definition will be the one chosen by the
0627   /// linker.
0628   ///
0629   /// NB! Ideally this should not be used at the IR level at all.  If you're
0630   /// interested in optimization constraints implied by the linker's ability to
0631   /// choose an implementation, prefer using \c hasExactDefinition.
0632   bool isStrongDefinitionForLinker() const {
0633     return !(isDeclarationForLinker() || isWeakForLinker());
0634   }
0635 
0636   const GlobalObject *getAliaseeObject() const;
0637   GlobalObject *getAliaseeObject() {
0638     return const_cast<GlobalObject *>(
0639         static_cast<const GlobalValue *>(this)->getAliaseeObject());
0640   }
0641 
0642   /// Returns whether this is a reference to an absolute symbol.
0643   bool isAbsoluteSymbolRef() const;
0644 
0645   /// If this is an absolute symbol reference, returns the range of the symbol,
0646   /// otherwise returns std::nullopt.
0647   std::optional<ConstantRange> getAbsoluteSymbolRange() const;
0648 
0649   /// This method unlinks 'this' from the containing module, but does not delete
0650   /// it.
0651   void removeFromParent();
0652 
0653   /// This method unlinks 'this' from the containing module and deletes it.
0654   void eraseFromParent();
0655 
0656   /// Get the module that this global value is contained inside of...
0657   Module *getParent() { return Parent; }
0658   const Module *getParent() const { return Parent; }
0659 
0660   /// Get the data layout of the module this global belongs to.
0661   ///
0662   /// Requires the global to have a parent module.
0663   const DataLayout &getDataLayout() const;
0664 
0665   // Methods for support type inquiry through isa, cast, and dyn_cast:
0666   static bool classof(const Value *V) {
0667     return V->getValueID() == Value::FunctionVal ||
0668            V->getValueID() == Value::GlobalVariableVal ||
0669            V->getValueID() == Value::GlobalAliasVal ||
0670            V->getValueID() == Value::GlobalIFuncVal;
0671   }
0672 
0673   /// True if GV can be left out of the object symbol table. This is the case
0674   /// for linkonce_odr values whose address is not significant. While legal, it
0675   /// is not normally profitable to omit them from the .o symbol table. Using
0676   /// this analysis makes sense when the information can be passed down to the
0677   /// linker or we are in LTO.
0678   bool canBeOmittedFromSymbolTable() const;
0679 };
0680 
0681 } // end namespace llvm
0682 
0683 #endif // LLVM_IR_GLOBALVALUE_H