LLVM  mainline
GlobalValue.h
Go to the documentation of this file.
00001 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file is a common base class of all globally definable objects.  As such,
00011 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
00012 // used because you can do certain things with these global objects that you
00013 // can't do to anything else.  For example, use the address of one as a
00014 // constant.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_IR_GLOBALVALUE_H
00019 #define LLVM_IR_GLOBALVALUE_H
00020 
00021 #include "llvm/IR/Constant.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include <system_error>
00024 
00025 namespace llvm {
00026 
00027 class Comdat;
00028 class PointerType;
00029 class Module;
00030 
00031 namespace Intrinsic {
00032   enum ID : unsigned;
00033 }
00034 
00035 class GlobalValue : public Constant {
00036   GlobalValue(const GlobalValue &) = delete;
00037 public:
00038   /// @brief An enumeration for the kinds of linkage for global values.
00039   enum LinkageTypes {
00040     ExternalLinkage = 0,///< Externally visible function
00041     AvailableExternallyLinkage, ///< Available for inspection, not emission.
00042     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
00043     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
00044     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
00045     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
00046     AppendingLinkage,   ///< Special purpose, only applies to global arrays
00047     InternalLinkage,    ///< Rename collisions when linking (static functions).
00048     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
00049     ExternalWeakLinkage,///< ExternalWeak linkage description.
00050     CommonLinkage       ///< Tentative definitions.
00051   };
00052 
00053   /// @brief An enumeration for the kinds of visibility of global values.
00054   enum VisibilityTypes {
00055     DefaultVisibility = 0,  ///< The GV is visible
00056     HiddenVisibility,       ///< The GV is hidden
00057     ProtectedVisibility     ///< The GV is protected
00058   };
00059 
00060   /// @brief Storage classes of global values for PE targets.
00061   enum DLLStorageClassTypes {
00062     DefaultStorageClass   = 0,
00063     DLLImportStorageClass = 1, ///< Function to be imported from DLL
00064     DLLExportStorageClass = 2  ///< Function to be accessible from DLL.
00065   };
00066 
00067 protected:
00068   GlobalValue(PointerType *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
00069               LinkageTypes Linkage, const Twine &Name)
00070       : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
00071         Visibility(DefaultVisibility), UnnamedAddr(0),
00072         DllStorageClass(DefaultStorageClass),
00073         ThreadLocal(NotThreadLocal), IntID((Intrinsic::ID)0U), Parent(nullptr) {
00074     setName(Name);
00075   }
00076 
00077   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
00078   // Linkage and Visibility from turning into negative values.
00079   LinkageTypes Linkage : 5;   // The linkage of this global
00080   unsigned Visibility : 2;    // The visibility style of this global
00081   unsigned UnnamedAddr : 1;   // This value's address is not significant
00082   unsigned DllStorageClass : 2; // DLL storage class
00083 
00084   unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
00085                             // the desired model?
00086 
00087 private:
00088   // Give subclasses access to what otherwise would be wasted padding.
00089   // (19 + 3 + 2 + 1 + 2 + 5) == 32.
00090   unsigned SubClassData : 19;
00091 
00092 protected:
00093   /// \brief The intrinsic ID for this subclass (which must be a Function).
00094   ///
00095   /// This member is defined by this class, but not used for anything.
00096   /// Subclasses can use it to store their intrinsic ID, if they have one.
00097   ///
00098   /// This is stored here to save space in Function on 64-bit hosts.
00099   Intrinsic::ID IntID;
00100 
00101   static const unsigned GlobalValueSubClassDataBits = 19;
00102   unsigned getGlobalValueSubClassData() const {
00103     return SubClassData;
00104   }
00105   void setGlobalValueSubClassData(unsigned V) {
00106     assert(V < (1 << 19) && "It will not fit");
00107     SubClassData = V;
00108   }
00109 
00110   Module *Parent;             // The containing module.
00111 public:
00112   enum ThreadLocalMode {
00113     NotThreadLocal = 0,
00114     GeneralDynamicTLSModel,
00115     LocalDynamicTLSModel,
00116     InitialExecTLSModel,
00117     LocalExecTLSModel
00118   };
00119 
00120   ~GlobalValue() override {
00121     removeDeadConstantUsers();   // remove any dead constants using this.
00122   }
00123 
00124   unsigned getAlignment() const;
00125 
00126   bool hasUnnamedAddr() const { return UnnamedAddr; }
00127   void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
00128 
00129   bool hasComdat() const { return getComdat() != nullptr; }
00130   Comdat *getComdat();
00131   const Comdat *getComdat() const {
00132     return const_cast<GlobalValue *>(this)->getComdat();
00133   }
00134 
00135   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
00136   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
00137   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
00138   bool hasProtectedVisibility() const {
00139     return Visibility == ProtectedVisibility;
00140   }
00141   void setVisibility(VisibilityTypes V) {
00142     assert((!hasLocalLinkage() || V == DefaultVisibility) &&
00143            "local linkage requires default visibility");
00144     Visibility = V;
00145   }
00146 
00147   /// If the value is "Thread Local", its value isn't shared by the threads.
00148   bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
00149   void setThreadLocal(bool Val) {
00150     setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
00151   }
00152   void setThreadLocalMode(ThreadLocalMode Val) {
00153     assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
00154     ThreadLocal = Val;
00155   }
00156   ThreadLocalMode getThreadLocalMode() const {
00157     return static_cast<ThreadLocalMode>(ThreadLocal);
00158   }
00159 
00160   DLLStorageClassTypes getDLLStorageClass() const {
00161     return DLLStorageClassTypes(DllStorageClass);
00162   }
00163   bool hasDLLImportStorageClass() const {
00164     return DllStorageClass == DLLImportStorageClass;
00165   }
00166   bool hasDLLExportStorageClass() const {
00167     return DllStorageClass == DLLExportStorageClass;
00168   }
00169   void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
00170 
00171   bool hasSection() const { return !StringRef(getSection()).empty(); }
00172   // It is unfortunate that we have to use "char *" in here since this is
00173   // always non NULL, but:
00174   // * The C API expects a null terminated string, so we cannot use StringRef.
00175   // * The C API expects us to own it, so we cannot use a std:string.
00176   // * For GlobalAliases we can fail to find the section and we have to
00177   //   return "", so we cannot use a "const std::string &".
00178   const char *getSection() const;
00179 
00180   /// Global values are always pointers.
00181   PointerType *getType() const { return cast<PointerType>(User::getType()); }
00182 
00183   Type *getValueType() const { return getType()->getElementType(); }
00184 
00185   static LinkageTypes getLinkOnceLinkage(bool ODR) {
00186     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
00187   }
00188   static LinkageTypes getWeakLinkage(bool ODR) {
00189     return ODR ? WeakODRLinkage : WeakAnyLinkage;
00190   }
00191 
00192   static bool isExternalLinkage(LinkageTypes Linkage) {
00193     return Linkage == ExternalLinkage;
00194   }
00195   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
00196     return Linkage == AvailableExternallyLinkage;
00197   }
00198   static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
00199     return Linkage == LinkOnceODRLinkage;
00200   }
00201   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
00202     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
00203   }
00204   static bool isWeakAnyLinkage(LinkageTypes Linkage) {
00205     return Linkage == WeakAnyLinkage;
00206   }
00207   static bool isWeakODRLinkage(LinkageTypes Linkage) {
00208     return Linkage == WeakODRLinkage;
00209   }
00210   static bool isWeakLinkage(LinkageTypes Linkage) {
00211     return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
00212   }
00213   static bool isAppendingLinkage(LinkageTypes Linkage) {
00214     return Linkage == AppendingLinkage;
00215   }
00216   static bool isInternalLinkage(LinkageTypes Linkage) {
00217     return Linkage == InternalLinkage;
00218   }
00219   static bool isPrivateLinkage(LinkageTypes Linkage) {
00220     return Linkage == PrivateLinkage;
00221   }
00222   static bool isLocalLinkage(LinkageTypes Linkage) {
00223     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
00224   }
00225   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
00226     return Linkage == ExternalWeakLinkage;
00227   }
00228   static bool isCommonLinkage(LinkageTypes Linkage) {
00229     return Linkage == CommonLinkage;
00230   }
00231 
00232   /// Whether the definition of this global may be discarded if it is not used
00233   /// in its compilation unit.
00234   static bool isDiscardableIfUnused(LinkageTypes Linkage) {
00235     return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
00236   }
00237 
00238   /// Whether the definition of this global may be replaced by something
00239   /// non-equivalent at link time. For example, if a function has weak linkage
00240   /// then the code defining it may be replaced by different code.
00241   static bool mayBeOverridden(LinkageTypes Linkage) {
00242     return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
00243            Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
00244   }
00245 
00246   /// Whether the definition of this global may be replaced at link time.  NB:
00247   /// Using this method outside of the code generators is almost always a
00248   /// mistake: when working at the IR level use mayBeOverridden instead as it
00249   /// knows about ODR semantics.
00250   static bool isWeakForLinker(LinkageTypes Linkage)  {
00251     return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
00252            Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
00253            Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
00254            Linkage == ExternalWeakLinkage;
00255   }
00256 
00257   bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
00258   bool hasAvailableExternallyLinkage() const {
00259     return isAvailableExternallyLinkage(Linkage);
00260   }
00261   bool hasLinkOnceLinkage() const {
00262     return isLinkOnceLinkage(Linkage);
00263   }
00264   bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
00265   bool hasWeakLinkage() const {
00266     return isWeakLinkage(Linkage);
00267   }
00268   bool hasWeakAnyLinkage() const {
00269     return isWeakAnyLinkage(Linkage);
00270   }
00271   bool hasWeakODRLinkage() const {
00272     return isWeakODRLinkage(Linkage);
00273   }
00274   bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
00275   bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
00276   bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
00277   bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
00278   bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
00279   bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
00280 
00281   void setLinkage(LinkageTypes LT) {
00282     if (isLocalLinkage(LT))
00283       Visibility = DefaultVisibility;
00284     Linkage = LT;
00285   }
00286   LinkageTypes getLinkage() const { return Linkage; }
00287 
00288   bool isDiscardableIfUnused() const {
00289     return isDiscardableIfUnused(Linkage);
00290   }
00291 
00292   bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
00293 
00294   bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
00295 
00296   /// Copy all additional attributes (those not needed to create a GlobalValue)
00297   /// from the GlobalValue Src to this one.
00298   virtual void copyAttributesFrom(const GlobalValue *Src);
00299 
00300   /// If special LLVM prefix that is used to inform the asm printer to not emit
00301   /// usual symbol prefix before the symbol name is used then return linkage
00302   /// name after skipping this special LLVM prefix.
00303   static StringRef getRealLinkageName(StringRef Name) {
00304     if (!Name.empty() && Name[0] == '\1')
00305       return Name.substr(1);
00306     return Name;
00307   }
00308 
00309 /// @name Materialization
00310 /// Materialization is used to construct functions only as they're needed. This
00311 /// is useful to reduce memory usage in LLVM or parsing work done by the
00312 /// BitcodeReader to load the Module.
00313 /// @{
00314 
00315   /// If this function's Module is being lazily streamed in functions from disk
00316   /// or some other source, this method can be used to check to see if the
00317   /// function has been read in yet or not.
00318   bool isMaterializable() const;
00319 
00320   /// Returns true if this function was loaded from a GVMaterializer that's
00321   /// still attached to its Module and that knows how to dematerialize the
00322   /// function.
00323   bool isDematerializable() const;
00324 
00325   /// Make sure this GlobalValue is fully read. If the module is corrupt, this
00326   /// returns true and fills in the optional string with information about the
00327   /// problem.  If successful, this returns false.
00328   std::error_code materialize();
00329 
00330   /// If this GlobalValue is read in, and if the GVMaterializer supports it,
00331   /// release the memory for the function, and set it up to be materialized
00332   /// lazily. If !isDematerializable(), this method is a noop.
00333   void dematerialize();
00334 
00335 /// @}
00336 
00337   /// Override from Constant class.
00338   void destroyConstant() override;
00339 
00340   /// Return true if the primary definition of this global value is outside of
00341   /// the current translation unit.
00342   bool isDeclaration() const;
00343 
00344   bool isDeclarationForLinker() const {
00345     if (hasAvailableExternallyLinkage())
00346       return true;
00347 
00348     return isDeclaration();
00349   }
00350 
00351   /// This method unlinks 'this' from the containing module, but does not delete
00352   /// it.
00353   virtual void removeFromParent() = 0;
00354 
00355   /// This method unlinks 'this' from the containing module and deletes it.
00356   virtual void eraseFromParent() = 0;
00357 
00358   /// Get the module that this global value is contained inside of...
00359   Module *getParent() { return Parent; }
00360   const Module *getParent() const { return Parent; }
00361 
00362   // Methods for support type inquiry through isa, cast, and dyn_cast:
00363   static bool classof(const Value *V) {
00364     return V->getValueID() == Value::FunctionVal ||
00365            V->getValueID() == Value::GlobalVariableVal ||
00366            V->getValueID() == Value::GlobalAliasVal;
00367   }
00368 };
00369 
00370 } // End llvm namespace
00371 
00372 #endif