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