1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This represents an independent object. That is, a function or a global
10 // variable, but not an alias.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_GLOBALOBJECT_H
15 #define LLVM_IR_GLOBALOBJECT_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/Value.h"
20 #include "llvm/Support/Alignment.h"
30 class GlobalObject
: public GlobalValue
{
32 // VCallVisibility - values for visibility metadata attached to vtables. This
33 // describes the scope in which a virtual call could end up being dispatched
34 // through this vtable.
35 enum VCallVisibility
{
36 // Type is potentially visible to external code.
37 VCallVisibilityPublic
= 0,
38 // Type is only visible to code which will be in the current Module after
39 // LTO internalization.
40 VCallVisibilityLinkageUnit
= 1,
41 // Type is only visible to code in the current Module.
42 VCallVisibilityTranslationUnit
= 2,
46 GlobalObject(Type
*Ty
, ValueTy VTy
, Use
*Ops
, unsigned NumOps
,
47 LinkageTypes Linkage
, const Twine
&Name
,
48 unsigned AddressSpace
= 0)
49 : GlobalValue(Ty
, VTy
, Ops
, NumOps
, Linkage
, Name
, AddressSpace
),
51 setGlobalValueSubClassData(0);
57 HasMetadataHashEntryBit
,
58 HasSectionHashEntryBit
,
62 static const unsigned GlobalObjectSubClassDataBits
=
63 GlobalValueSubClassDataBits
- GlobalObjectBits
;
66 static const unsigned AlignmentBits
= LastAlignmentBit
+ 1;
67 static const unsigned AlignmentMask
= (1 << AlignmentBits
) - 1;
68 static const unsigned GlobalObjectMask
= (1 << GlobalObjectBits
) - 1;
71 GlobalObject(const GlobalObject
&) = delete;
73 unsigned getAlignment() const {
74 unsigned Data
= getGlobalValueSubClassData();
75 unsigned AlignmentData
= Data
& AlignmentMask
;
76 MaybeAlign Align
= decodeMaybeAlign(AlignmentData
);
77 return Align
? Align
->value() : 0;
80 /// FIXME: Remove this setter once the migration to MaybeAlign is over.
81 void setAlignment(unsigned Align
);
82 void setAlignment(MaybeAlign Align
);
84 unsigned getGlobalObjectSubClassData() const {
85 unsigned ValueData
= getGlobalValueSubClassData();
86 return ValueData
>> GlobalObjectBits
;
89 void setGlobalObjectSubClassData(unsigned Val
) {
90 unsigned OldData
= getGlobalValueSubClassData();
91 setGlobalValueSubClassData((OldData
& GlobalObjectMask
) |
92 (Val
<< GlobalObjectBits
));
93 assert(getGlobalObjectSubClassData() == Val
&& "representation error");
96 /// Check if this global has a custom object file section.
98 /// This is more efficient than calling getSection() and checking for an empty
100 bool hasSection() const {
101 return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit
);
104 /// Get the custom section of this global if it has one.
106 /// If this global does not have a custom section, this will be empty and the
107 /// default object file section (.text, .data, etc) will be used.
108 StringRef
getSection() const {
109 return hasSection() ? getSectionImpl() : StringRef();
112 /// Change the section for this global.
114 /// Setting the section to the empty string tells LLVM to choose an
115 /// appropriate default object file section.
116 void setSection(StringRef S
);
118 bool hasComdat() const { return getComdat() != nullptr; }
119 const Comdat
*getComdat() const { return ObjComdat
; }
120 Comdat
*getComdat() { return ObjComdat
; }
121 void setComdat(Comdat
*C
) { ObjComdat
= C
; }
123 /// Check if this has any metadata.
124 bool hasMetadata() const { return hasMetadataHashEntry(); }
126 /// Check if this has any metadata of the given kind.
127 bool hasMetadata(unsigned KindID
) const {
128 return getMetadata(KindID
) != nullptr;
130 bool hasMetadata(StringRef Kind
) const {
131 return getMetadata(Kind
) != nullptr;
134 /// Get the current metadata attachments for the given kind, if any.
136 /// These functions require that the function have at most a single attachment
137 /// of the given kind, and return \c nullptr if such an attachment is missing.
139 MDNode
*getMetadata(unsigned KindID
) const;
140 MDNode
*getMetadata(StringRef Kind
) const;
143 /// Appends all attachments with the given ID to \c MDs in insertion order.
144 /// If the global has no attachments with the given ID, or if ID is invalid,
145 /// leaves MDs unchanged.
147 void getMetadata(unsigned KindID
, SmallVectorImpl
<MDNode
*> &MDs
) const;
148 void getMetadata(StringRef Kind
, SmallVectorImpl
<MDNode
*> &MDs
) const;
151 /// Set a particular kind of metadata attachment.
153 /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
154 /// replacing it if it already exists.
156 void setMetadata(unsigned KindID
, MDNode
*MD
);
157 void setMetadata(StringRef Kind
, MDNode
*MD
);
160 /// Add a metadata attachment.
162 void addMetadata(unsigned KindID
, MDNode
&MD
);
163 void addMetadata(StringRef Kind
, MDNode
&MD
);
166 /// Appends all attachments for the global to \c MDs, sorting by attachment
167 /// ID. Attachments with the same ID appear in insertion order.
169 getAllMetadata(SmallVectorImpl
<std::pair
<unsigned, MDNode
*>> &MDs
) const;
171 /// Erase all metadata attachments with the given kind.
173 /// \returns true if any metadata was removed.
174 bool eraseMetadata(unsigned KindID
);
176 /// Copy metadata from Src, adjusting offsets by Offset.
177 void copyMetadata(const GlobalObject
*Src
, unsigned Offset
);
179 void addTypeMetadata(unsigned Offset
, Metadata
*TypeID
);
180 void addVCallVisibilityMetadata(VCallVisibility Visibility
);
181 VCallVisibility
getVCallVisibility() const;
184 void copyAttributesFrom(const GlobalObject
*Src
);
187 // Methods for support type inquiry through isa, cast, and dyn_cast:
188 static bool classof(const Value
*V
) {
189 return V
->getValueID() == Value::FunctionVal
||
190 V
->getValueID() == Value::GlobalVariableVal
;
193 void clearMetadata();
196 void setGlobalObjectFlag(unsigned Bit
, bool Val
) {
197 unsigned Mask
= 1 << Bit
;
198 setGlobalValueSubClassData((~Mask
& getGlobalValueSubClassData()) |
202 bool hasMetadataHashEntry() const {
203 return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit
);
205 void setHasMetadataHashEntry(bool HasEntry
) {
206 setGlobalObjectFlag(HasMetadataHashEntryBit
, HasEntry
);
209 StringRef
getSectionImpl() const;
212 } // end namespace llvm
214 #endif // LLVM_IR_GLOBALOBJECT_H