Home | History | Annotate | Download | only in IR
      1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- 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 represents an independent object. That is, a function or a global
     11 // variable, but not an alias.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_IR_GLOBALOBJECT_H
     16 #define LLVM_IR_GLOBALOBJECT_H
     17 
     18 #include "llvm/IR/DerivedTypes.h"
     19 #include "llvm/IR/GlobalValue.h"
     20 
     21 namespace llvm {
     22 class Comdat;
     23 class MDNode;
     24 class Metadata;
     25 class Module;
     26 
     27 class GlobalObject : public GlobalValue {
     28   GlobalObject(const GlobalObject &) = delete;
     29 
     30 protected:
     31   GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
     32                LinkageTypes Linkage, const Twine &Name,
     33                unsigned AddressSpace = 0)
     34       : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
     35         ObjComdat(nullptr) {
     36     setGlobalValueSubClassData(0);
     37   }
     38 
     39   std::string Section;     // Section to emit this into, empty means default
     40   Comdat *ObjComdat;
     41   enum {
     42     LastAlignmentBit = 4,
     43     HasMetadataHashEntryBit,
     44 
     45     GlobalObjectBits,
     46   };
     47   static const unsigned GlobalObjectSubClassDataBits =
     48       GlobalValueSubClassDataBits - GlobalObjectBits;
     49 
     50 private:
     51   static const unsigned AlignmentBits = LastAlignmentBit + 1;
     52   static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
     53   static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
     54 
     55 public:
     56   unsigned getAlignment() const {
     57     unsigned Data = getGlobalValueSubClassData();
     58     unsigned AlignmentData = Data & AlignmentMask;
     59     return (1u << AlignmentData) >> 1;
     60   }
     61   void setAlignment(unsigned Align);
     62 
     63   unsigned getGlobalObjectSubClassData() const;
     64   void setGlobalObjectSubClassData(unsigned Val);
     65 
     66   bool hasSection() const { return !getSection().empty(); }
     67   StringRef getSection() const { return Section; }
     68   void setSection(StringRef S);
     69 
     70   bool hasComdat() const { return getComdat() != nullptr; }
     71   const Comdat *getComdat() const { return ObjComdat; }
     72   Comdat *getComdat() { return ObjComdat; }
     73   void setComdat(Comdat *C) { ObjComdat = C; }
     74 
     75   /// Check if this has any metadata.
     76   bool hasMetadata() const { return hasMetadataHashEntry(); }
     77 
     78   /// Get the current metadata attachments for the given kind, if any.
     79   ///
     80   /// These functions require that the function have at most a single attachment
     81   /// of the given kind, and return \c nullptr if such an attachment is missing.
     82   /// @{
     83   MDNode *getMetadata(unsigned KindID) const;
     84   MDNode *getMetadata(StringRef Kind) const;
     85   /// @}
     86 
     87   /// Appends all attachments with the given ID to \c MDs in insertion order.
     88   /// If the global has no attachments with the given ID, or if ID is invalid,
     89   /// leaves MDs unchanged.
     90   /// @{
     91   void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
     92   void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
     93   /// @}
     94 
     95   /// Set a particular kind of metadata attachment.
     96   ///
     97   /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
     98   /// replacing it if it already exists.
     99   /// @{
    100   void setMetadata(unsigned KindID, MDNode *MD);
    101   void setMetadata(StringRef Kind, MDNode *MD);
    102   /// @}
    103 
    104   /// Add a metadata attachment.
    105   /// @{
    106   void addMetadata(unsigned KindID, MDNode &MD);
    107   void addMetadata(StringRef Kind, MDNode &MD);
    108   /// @}
    109 
    110   /// Appends all attachments for the global to \c MDs, sorting by attachment
    111   /// ID. Attachments with the same ID appear in insertion order.
    112   void
    113   getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
    114 
    115   /// Erase all metadata attachments with the given kind.
    116   void eraseMetadata(unsigned KindID);
    117 
    118   /// Copy metadata from Src, adjusting offsets by Offset.
    119   void copyMetadata(const GlobalObject *Src, unsigned Offset);
    120 
    121   void addTypeMetadata(unsigned Offset, Metadata *TypeID);
    122 
    123   void copyAttributesFrom(const GlobalValue *Src) override;
    124 
    125   // Methods for support type inquiry through isa, cast, and dyn_cast:
    126   static inline bool classof(const Value *V) {
    127     return V->getValueID() == Value::FunctionVal ||
    128            V->getValueID() == Value::GlobalVariableVal;
    129   }
    130 
    131   void clearMetadata();
    132 
    133 private:
    134   bool hasMetadataHashEntry() const {
    135     return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
    136   }
    137   void setHasMetadataHashEntry(bool HasEntry) {
    138     unsigned Mask = 1 << HasMetadataHashEntryBit;
    139     setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
    140                                (HasEntry ? Mask : 0u));
    141   }
    142 };
    143 
    144 } // End llvm namespace
    145 
    146 #endif
    147