Home | History | Annotate | Download | only in llvm
      1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a common base class of all globally definable objects.  As such,
     11 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
     12 // used because you can do certain things with these global objects that you
     13 // can't do to anything else.  For example, use the address of one as a
     14 // constant.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #ifndef LLVM_GLOBALVALUE_H
     19 #define LLVM_GLOBALVALUE_H
     20 
     21 #include "llvm/Constant.h"
     22 
     23 namespace llvm {
     24 
     25 class PointerType;
     26 class Module;
     27 
     28 class GlobalValue : public Constant {
     29   GlobalValue(const GlobalValue &);             // do not implement
     30 public:
     31   /// @brief An enumeration for the kinds of linkage for global values.
     32   enum LinkageTypes {
     33     ExternalLinkage = 0,///< Externally visible function
     34     AvailableExternallyLinkage, ///< Available for inspection, not emission.
     35     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
     36     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
     37     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
     38     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
     39     AppendingLinkage,   ///< Special purpose, only applies to global arrays
     40     InternalLinkage,    ///< Rename collisions when linking (static functions).
     41     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
     42     LinkerPrivateLinkage, ///< Like Private, but linker removes.
     43     LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
     44     LinkerPrivateWeakDefAutoLinkage, ///< Like LinkerPrivateWeak, but possibly
     45                                      ///  hidden.
     46     DLLImportLinkage,   ///< Function to be imported from DLL
     47     DLLExportLinkage,   ///< Function to be accessible from DLL.
     48     ExternalWeakLinkage,///< ExternalWeak linkage description.
     49     CommonLinkage       ///< Tentative definitions.
     50   };
     51 
     52   /// @brief An enumeration for the kinds of visibility of global values.
     53   enum VisibilityTypes {
     54     DefaultVisibility = 0,  ///< The GV is visible
     55     HiddenVisibility,       ///< The GV is hidden
     56     ProtectedVisibility     ///< The GV is protected
     57   };
     58 
     59 protected:
     60   GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
     61               LinkageTypes linkage, const Twine &Name)
     62     : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
     63       Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) {
     64     setName(Name);
     65   }
     66 
     67   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
     68   // Linkage and Visibility from turning into negative values.
     69   LinkageTypes Linkage : 5;   // The linkage of this global
     70   unsigned Visibility : 2;    // The visibility style of this global
     71   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
     72   unsigned UnnamedAddr : 1;   // This value's address is not significant
     73   Module *Parent;             // The containing module.
     74   std::string Section;        // Section to emit this into, empty mean default
     75 public:
     76   ~GlobalValue() {
     77     removeDeadConstantUsers();   // remove any dead constants using this.
     78   }
     79 
     80   unsigned getAlignment() const {
     81     return (1u << Alignment) >> 1;
     82   }
     83   void setAlignment(unsigned Align);
     84 
     85   bool hasUnnamedAddr() const { return UnnamedAddr; }
     86   void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
     87 
     88   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
     89   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
     90   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
     91   bool hasProtectedVisibility() const {
     92     return Visibility == ProtectedVisibility;
     93   }
     94   void setVisibility(VisibilityTypes V) { Visibility = V; }
     95 
     96   bool hasSection() const { return !Section.empty(); }
     97   const std::string &getSection() const { return Section; }
     98   void setSection(StringRef S) { Section = S; }
     99 
    100   /// If the usage is empty (except transitively dead constants), then this
    101   /// global value can be safely deleted since the destructor will
    102   /// delete the dead constants as well.
    103   /// @brief Determine if the usage of this global value is empty except
    104   /// for transitively dead constants.
    105   bool use_empty_except_constants();
    106 
    107   /// getType - Global values are always pointers.
    108   inline PointerType *getType() const {
    109     return reinterpret_cast<PointerType*>(User::getType());
    110   }
    111 
    112   static LinkageTypes getLinkOnceLinkage(bool ODR) {
    113     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
    114   }
    115   static LinkageTypes getWeakLinkage(bool ODR) {
    116     return ODR ? WeakODRLinkage : WeakAnyLinkage;
    117   }
    118 
    119   static bool isExternalLinkage(LinkageTypes Linkage) {
    120     return Linkage == ExternalLinkage;
    121   }
    122   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
    123     return Linkage == AvailableExternallyLinkage;
    124   }
    125   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
    126     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
    127   }
    128   static bool isWeakLinkage(LinkageTypes Linkage) {
    129     return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
    130   }
    131   static bool isAppendingLinkage(LinkageTypes Linkage) {
    132     return Linkage == AppendingLinkage;
    133   }
    134   static bool isInternalLinkage(LinkageTypes Linkage) {
    135     return Linkage == InternalLinkage;
    136   }
    137   static bool isPrivateLinkage(LinkageTypes Linkage) {
    138     return Linkage == PrivateLinkage;
    139   }
    140   static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
    141     return Linkage == LinkerPrivateLinkage;
    142   }
    143   static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
    144     return Linkage == LinkerPrivateWeakLinkage;
    145   }
    146   static bool isLinkerPrivateWeakDefAutoLinkage(LinkageTypes Linkage) {
    147     return Linkage == LinkerPrivateWeakDefAutoLinkage;
    148   }
    149   static bool isLocalLinkage(LinkageTypes Linkage) {
    150     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
    151       isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage) ||
    152       isLinkerPrivateWeakDefAutoLinkage(Linkage);
    153   }
    154   static bool isDLLImportLinkage(LinkageTypes Linkage) {
    155     return Linkage == DLLImportLinkage;
    156   }
    157   static bool isDLLExportLinkage(LinkageTypes Linkage) {
    158     return Linkage == DLLExportLinkage;
    159   }
    160   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
    161     return Linkage == ExternalWeakLinkage;
    162   }
    163   static bool isCommonLinkage(LinkageTypes Linkage) {
    164     return Linkage == CommonLinkage;
    165   }
    166 
    167   /// mayBeOverridden - Whether the definition of this global may be replaced
    168   /// by something non-equivalent at link time.  For example, if a function has
    169   /// weak linkage then the code defining it may be replaced by different code.
    170   static bool mayBeOverridden(LinkageTypes Linkage) {
    171     return Linkage == WeakAnyLinkage ||
    172            Linkage == LinkOnceAnyLinkage ||
    173            Linkage == CommonLinkage ||
    174            Linkage == ExternalWeakLinkage ||
    175            Linkage == LinkerPrivateWeakLinkage ||
    176            Linkage == LinkerPrivateWeakDefAutoLinkage;
    177   }
    178 
    179   /// isWeakForLinker - Whether the definition of this global may be replaced at
    180   /// link time.  NB: Using this method outside of the code generators is almost
    181   /// always a mistake: when working at the IR level use mayBeOverridden instead
    182   /// as it knows about ODR semantics.
    183   static bool isWeakForLinker(LinkageTypes Linkage)  {
    184     return Linkage == AvailableExternallyLinkage ||
    185            Linkage == WeakAnyLinkage ||
    186            Linkage == WeakODRLinkage ||
    187            Linkage == LinkOnceAnyLinkage ||
    188            Linkage == LinkOnceODRLinkage ||
    189            Linkage == CommonLinkage ||
    190            Linkage == ExternalWeakLinkage ||
    191            Linkage == LinkerPrivateWeakLinkage ||
    192            Linkage == LinkerPrivateWeakDefAutoLinkage;
    193   }
    194 
    195   bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
    196   bool hasAvailableExternallyLinkage() const {
    197     return isAvailableExternallyLinkage(Linkage);
    198   }
    199   bool hasLinkOnceLinkage() const {
    200     return isLinkOnceLinkage(Linkage);
    201   }
    202   bool hasWeakLinkage() const {
    203     return isWeakLinkage(Linkage);
    204   }
    205   bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
    206   bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
    207   bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
    208   bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
    209   bool hasLinkerPrivateWeakLinkage() const {
    210     return isLinkerPrivateWeakLinkage(Linkage);
    211   }
    212   bool hasLinkerPrivateWeakDefAutoLinkage() const {
    213     return isLinkerPrivateWeakDefAutoLinkage(Linkage);
    214   }
    215   bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
    216   bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
    217   bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
    218   bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
    219   bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
    220 
    221   void setLinkage(LinkageTypes LT) { Linkage = LT; }
    222   LinkageTypes getLinkage() const { return Linkage; }
    223 
    224   bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
    225 
    226   bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
    227 
    228   /// copyAttributesFrom - copy all additional attributes (those not needed to
    229   /// create a GlobalValue) from the GlobalValue Src to this one.
    230   virtual void copyAttributesFrom(const GlobalValue *Src);
    231 
    232 /// @name Materialization
    233 /// Materialization is used to construct functions only as they're needed. This
    234 /// is useful to reduce memory usage in LLVM or parsing work done by the
    235 /// BitcodeReader to load the Module.
    236 /// @{
    237 
    238   /// isMaterializable - If this function's Module is being lazily streamed in
    239   /// functions from disk or some other source, this method can be used to check
    240   /// to see if the function has been read in yet or not.
    241   bool isMaterializable() const;
    242 
    243   /// isDematerializable - Returns true if this function was loaded from a
    244   /// GVMaterializer that's still attached to its Module and that knows how to
    245   /// dematerialize the function.
    246   bool isDematerializable() const;
    247 
    248   /// Materialize - make sure this GlobalValue is fully read.  If the module is
    249   /// corrupt, this returns true and fills in the optional string with
    250   /// information about the problem.  If successful, this returns false.
    251   bool Materialize(std::string *ErrInfo = 0);
    252 
    253   /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
    254   /// supports it, release the memory for the function, and set it up to be
    255   /// materialized lazily.  If !isDematerializable(), this method is a noop.
    256   void Dematerialize();
    257 
    258 /// @}
    259 
    260   /// Override from Constant class.
    261   virtual void destroyConstant();
    262 
    263   /// isDeclaration - Return true if the primary definition of this global
    264   /// value is outside of the current translation unit.
    265   bool isDeclaration() const;
    266 
    267   /// removeFromParent - This method unlinks 'this' from the containing module,
    268   /// but does not delete it.
    269   virtual void removeFromParent() = 0;
    270 
    271   /// eraseFromParent - This method unlinks 'this' from the containing module
    272   /// and deletes it.
    273   virtual void eraseFromParent() = 0;
    274 
    275   /// getParent - Get the module that this global value is contained inside
    276   /// of...
    277   inline Module *getParent() { return Parent; }
    278   inline const Module *getParent() const { return Parent; }
    279 
    280   // Methods for support type inquiry through isa, cast, and dyn_cast:
    281   static inline bool classof(const GlobalValue *) { return true; }
    282   static inline bool classof(const Value *V) {
    283     return V->getValueID() == Value::FunctionVal ||
    284            V->getValueID() == Value::GlobalVariableVal ||
    285            V->getValueID() == Value::GlobalAliasVal;
    286   }
    287 };
    288 
    289 } // End llvm namespace
    290 
    291 #endif
    292