LLVM API Documentation

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