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