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