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