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