[Alignment][NFC] Remove dependency on GlobalObject::setAlignment(unsigned)
[llvm-core.git] / include / llvm / IR / GlobalObject.h
blob739ef75dd8bde9fc75438db3a11ab3dcc514bef8
1 //===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
21 #include <string>
22 #include <utility>
24 namespace llvm {
26 class Comdat;
27 class MDNode;
28 class Metadata;
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)
35 : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
36 ObjComdat(nullptr) {
37 setGlobalValueSubClassData(0);
40 Comdat *ObjComdat;
41 enum {
42 LastAlignmentBit = 4,
43 HasMetadataHashEntryBit,
44 HasSectionHashEntryBit,
46 GlobalObjectBits,
48 static const unsigned GlobalObjectSubClassDataBits =
49 GlobalValueSubClassDataBits - GlobalObjectBits;
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;
56 public:
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.
84 ///
85 /// This is more efficient than calling getSection() and checking for an empty
86 /// string.
87 bool hasSection() const {
88 return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
91 /// Get the custom section of this global if it has one.
92 ///
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.
125 /// @{
126 MDNode *getMetadata(unsigned KindID) const;
127 MDNode *getMetadata(StringRef Kind) const;
128 /// @}
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.
133 /// @{
134 void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
135 void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
136 /// @}
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.
142 /// @{
143 void setMetadata(unsigned KindID, MDNode *MD);
144 void setMetadata(StringRef Kind, MDNode *MD);
145 /// @}
147 /// Add a metadata attachment.
148 /// @{
149 void addMetadata(unsigned KindID, MDNode &MD);
150 void addMetadata(StringRef Kind, MDNode &MD);
151 /// @}
153 /// Appends all attachments for the global to \c MDs, sorting by attachment
154 /// ID. Attachments with the same ID appear in insertion order.
155 void
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);
168 protected:
169 void copyAttributesFrom(const GlobalObject *Src);
171 public:
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();
180 private:
181 void setGlobalObjectFlag(unsigned Bit, bool Val) {
182 unsigned Mask = 1 << Bit;
183 setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
184 (Val ? Mask : 0u));
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