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