1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This represents an independent object. That is, a function or a global
11 // variable, but not an alias.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_GLOBALOBJECT_H
16 #define LLVM_IR_GLOBALOBJECT_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/Value.h"
30 class GlobalObject : public GlobalValue {
32 GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
33 LinkageTypes Linkage, const Twine &Name,
34 unsigned AddressSpace = 0)
35 : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
37 setGlobalValueSubClassData(0);
43 HasMetadataHashEntryBit,
44 HasSectionHashEntryBit,
48 static const unsigned GlobalObjectSubClassDataBits =
49 GlobalValueSubClassDataBits - GlobalObjectBits;
52 static const unsigned AlignmentBits = LastAlignmentBit + 1;
53 static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
54 static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
57 GlobalObject(const GlobalObject &) = delete;
59 unsigned getAlignment() const {
60 unsigned Data = getGlobalValueSubClassData();
61 unsigned AlignmentData = Data & AlignmentMask;
62 return (1u << AlignmentData) >> 1;
64 void setAlignment(unsigned Align);
66 unsigned getGlobalObjectSubClassData() const;
67 void setGlobalObjectSubClassData(unsigned Val);
69 /// Check if this global has a custom object file section.
71 /// This is more efficient than calling getSection() and checking for an empty
73 bool hasSection() const {
74 return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
77 /// Get the custom section of this global if it has one.
79 /// If this global does not have a custom section, this will be empty and the
80 /// default object file section (.text, .data, etc) will be used.
81 StringRef getSection() const {
82 return hasSection() ? getSectionImpl() : StringRef();
85 /// Change the section for this global.
87 /// Setting the section to the empty string tells LLVM to choose an
88 /// appropriate default object file section.
89 void setSection(StringRef S);
91 bool hasComdat() const { return getComdat() != nullptr; }
92 const Comdat *getComdat() const { return ObjComdat; }
93 Comdat *getComdat() { return ObjComdat; }
94 void setComdat(Comdat *C) { ObjComdat = C; }
96 /// Check if this has any metadata.
97 bool hasMetadata() const { return hasMetadataHashEntry(); }
99 /// Get the current metadata attachments for the given kind, if any.
101 /// These functions require that the function have at most a single attachment
102 /// of the given kind, and return \c nullptr if such an attachment is missing.
104 MDNode *getMetadata(unsigned KindID) const;
105 MDNode *getMetadata(StringRef Kind) const;
108 /// Appends all attachments with the given ID to \c MDs in insertion order.
109 /// If the global has no attachments with the given ID, or if ID is invalid,
110 /// leaves MDs unchanged.
112 void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
113 void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
116 /// Set a particular kind of metadata attachment.
118 /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
119 /// replacing it if it already exists.
121 void setMetadata(unsigned KindID, MDNode *MD);
122 void setMetadata(StringRef Kind, MDNode *MD);
125 /// Add a metadata attachment.
127 void addMetadata(unsigned KindID, MDNode &MD);
128 void addMetadata(StringRef Kind, MDNode &MD);
131 /// Appends all attachments for the global to \c MDs, sorting by attachment
132 /// ID. Attachments with the same ID appear in insertion order.
134 getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
136 /// Erase all metadata attachments with the given kind.
137 void eraseMetadata(unsigned KindID);
139 /// Copy metadata from Src, adjusting offsets by Offset.
140 void copyMetadata(const GlobalObject *Src, unsigned Offset);
142 void addTypeMetadata(unsigned Offset, Metadata *TypeID);
144 void copyAttributesFrom(const GlobalValue *Src) override;
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const Value *V) {
148 return V->getValueID() == Value::FunctionVal ||
149 V->getValueID() == Value::GlobalVariableVal;
152 void clearMetadata();
155 void setGlobalObjectFlag(unsigned Bit, bool Val) {
156 unsigned Mask = 1 << Bit;
157 setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
161 bool hasMetadataHashEntry() const {
162 return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
164 void setHasMetadataHashEntry(bool HasEntry) {
165 setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
168 StringRef getSectionImpl() const;
171 } // end namespace llvm
173 #endif // LLVM_IR_GLOBALOBJECT_H