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 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 MaybeAlign Align
= decodeMaybeAlign(AlignmentData
);
63 return Align
? Align
->value() : 0;
66 /// FIXME: Remove this setter once the migration to MaybeAlign is over.
67 LLVM_ATTRIBUTE_DEPRECATED(void setAlignment(unsigned Align
),
68 "Please use `void setAlignment(MaybeAlign Align)`");
69 void setAlignment(MaybeAlign Align
);
71 unsigned getGlobalObjectSubClassData() const {
72 unsigned ValueData
= getGlobalValueSubClassData();
73 return ValueData
>> GlobalObjectBits
;
76 void setGlobalObjectSubClassData(unsigned Val
) {
77 unsigned OldData
= getGlobalValueSubClassData();
78 setGlobalValueSubClassData((OldData
& GlobalObjectMask
) |
79 (Val
<< GlobalObjectBits
));
80 assert(getGlobalObjectSubClassData() == Val
&& "representation error");
83 /// Check if this global has a custom object file section.
85 /// This is more efficient than calling getSection() and checking for an empty
87 bool hasSection() const {
88 return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit
);
91 /// Get the custom section of this global if it has one.
93 /// If this global does not have a custom section, this will be empty and the
94 /// default object file section (.text, .data, etc) will be used.
95 StringRef
getSection() const {
96 return hasSection() ? getSectionImpl() : StringRef();
99 /// Change the section for this global.
101 /// Setting the section to the empty string tells LLVM to choose an
102 /// appropriate default object file section.
103 void setSection(StringRef S
);
105 bool hasComdat() const { return getComdat() != nullptr; }
106 const Comdat
*getComdat() const { return ObjComdat
; }
107 Comdat
*getComdat() { return ObjComdat
; }
108 void setComdat(Comdat
*C
) { ObjComdat
= C
; }
110 /// Check if this has any metadata.
111 bool hasMetadata() const { return hasMetadataHashEntry(); }
113 /// Check if this has any metadata of the given kind.
114 bool hasMetadata(unsigned KindID
) const {
115 return getMetadata(KindID
) != nullptr;
117 bool hasMetadata(StringRef Kind
) const {
118 return getMetadata(Kind
) != nullptr;
121 /// Get the current metadata attachments for the given kind, if any.
123 /// These functions require that the function have at most a single attachment
124 /// of the given kind, and return \c nullptr if such an attachment is missing.
126 MDNode
*getMetadata(unsigned KindID
) const;
127 MDNode
*getMetadata(StringRef Kind
) const;
130 /// Appends all attachments with the given ID to \c MDs in insertion order.
131 /// If the global has no attachments with the given ID, or if ID is invalid,
132 /// leaves MDs unchanged.
134 void getMetadata(unsigned KindID
, SmallVectorImpl
<MDNode
*> &MDs
) const;
135 void getMetadata(StringRef Kind
, SmallVectorImpl
<MDNode
*> &MDs
) const;
138 /// Set a particular kind of metadata attachment.
140 /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
141 /// replacing it if it already exists.
143 void setMetadata(unsigned KindID
, MDNode
*MD
);
144 void setMetadata(StringRef Kind
, MDNode
*MD
);
147 /// Add a metadata attachment.
149 void addMetadata(unsigned KindID
, MDNode
&MD
);
150 void addMetadata(StringRef Kind
, MDNode
&MD
);
153 /// Appends all attachments for the global to \c MDs, sorting by attachment
154 /// ID. Attachments with the same ID appear in insertion order.
156 getAllMetadata(SmallVectorImpl
<std::pair
<unsigned, MDNode
*>> &MDs
) const;
158 /// Erase all metadata attachments with the given kind.
160 /// \returns true if any metadata was removed.
161 bool eraseMetadata(unsigned KindID
);
163 /// Copy metadata from Src, adjusting offsets by Offset.
164 void copyMetadata(const GlobalObject
*Src
, unsigned Offset
);
166 void addTypeMetadata(unsigned Offset
, Metadata
*TypeID
);
169 void copyAttributesFrom(const GlobalObject
*Src
);
172 // Methods for support type inquiry through isa, cast, and dyn_cast:
173 static bool classof(const Value
*V
) {
174 return V
->getValueID() == Value::FunctionVal
||
175 V
->getValueID() == Value::GlobalVariableVal
;
178 void clearMetadata();
181 void setGlobalObjectFlag(unsigned Bit
, bool Val
) {
182 unsigned Mask
= 1 << Bit
;
183 setGlobalValueSubClassData((~Mask
& getGlobalValueSubClassData()) |
187 bool hasMetadataHashEntry() const {
188 return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit
);
190 void setHasMetadataHashEntry(bool HasEntry
) {
191 setGlobalObjectFlag(HasMetadataHashEntryBit
, HasEntry
);
194 StringRef
getSectionImpl() const;
197 } // end namespace llvm
199 #endif // LLVM_IR_GLOBALOBJECT_H