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