[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / include / llvm / IR / GlobalObject.h
bloba47faac5d4158647814e2aa279b286e052b8d687
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 void setAlignment(unsigned Align);
68 void setAlignment(MaybeAlign Align);
70 unsigned getGlobalObjectSubClassData() const {
71 unsigned ValueData = getGlobalValueSubClassData();
72 return ValueData >> GlobalObjectBits;
75 void setGlobalObjectSubClassData(unsigned Val) {
76 unsigned OldData = getGlobalValueSubClassData();
77 setGlobalValueSubClassData((OldData & GlobalObjectMask) |
78 (Val << GlobalObjectBits));
79 assert(getGlobalObjectSubClassData() == Val && "representation error");
82 /// Check if this global has a custom object file section.
83 ///
84 /// This is more efficient than calling getSection() and checking for an empty
85 /// string.
86 bool hasSection() const {
87 return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
90 /// Get the custom section of this global if it has one.
91 ///
92 /// If this global does not have a custom section, this will be empty and the
93 /// default object file section (.text, .data, etc) will be used.
94 StringRef getSection() const {
95 return hasSection() ? getSectionImpl() : StringRef();
98 /// Change the section for this global.
99 ///
100 /// Setting the section to the empty string tells LLVM to choose an
101 /// appropriate default object file section.
102 void setSection(StringRef S);
104 bool hasComdat() const { return getComdat() != nullptr; }
105 const Comdat *getComdat() const { return ObjComdat; }
106 Comdat *getComdat() { return ObjComdat; }
107 void setComdat(Comdat *C) { ObjComdat = C; }
109 /// Check if this has any metadata.
110 bool hasMetadata() const { return hasMetadataHashEntry(); }
112 /// Check if this has any metadata of the given kind.
113 bool hasMetadata(unsigned KindID) const {
114 return getMetadata(KindID) != nullptr;
116 bool hasMetadata(StringRef Kind) const {
117 return getMetadata(Kind) != nullptr;
120 /// Get the current metadata attachments for the given kind, if any.
122 /// These functions require that the function have at most a single attachment
123 /// of the given kind, and return \c nullptr if such an attachment is missing.
124 /// @{
125 MDNode *getMetadata(unsigned KindID) const;
126 MDNode *getMetadata(StringRef Kind) const;
127 /// @}
129 /// Appends all attachments with the given ID to \c MDs in insertion order.
130 /// If the global has no attachments with the given ID, or if ID is invalid,
131 /// leaves MDs unchanged.
132 /// @{
133 void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
134 void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
135 /// @}
137 /// Set a particular kind of metadata attachment.
139 /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
140 /// replacing it if it already exists.
141 /// @{
142 void setMetadata(unsigned KindID, MDNode *MD);
143 void setMetadata(StringRef Kind, MDNode *MD);
144 /// @}
146 /// Add a metadata attachment.
147 /// @{
148 void addMetadata(unsigned KindID, MDNode &MD);
149 void addMetadata(StringRef Kind, MDNode &MD);
150 /// @}
152 /// Appends all attachments for the global to \c MDs, sorting by attachment
153 /// ID. Attachments with the same ID appear in insertion order.
154 void
155 getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
157 /// Erase all metadata attachments with the given kind.
159 /// \returns true if any metadata was removed.
160 bool eraseMetadata(unsigned KindID);
162 /// Copy metadata from Src, adjusting offsets by Offset.
163 void copyMetadata(const GlobalObject *Src, unsigned Offset);
165 void addTypeMetadata(unsigned Offset, Metadata *TypeID);
167 protected:
168 void copyAttributesFrom(const GlobalObject *Src);
170 public:
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static bool classof(const Value *V) {
173 return V->getValueID() == Value::FunctionVal ||
174 V->getValueID() == Value::GlobalVariableVal;
177 void clearMetadata();
179 private:
180 void setGlobalObjectFlag(unsigned Bit, bool Val) {
181 unsigned Mask = 1 << Bit;
182 setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
183 (Val ? Mask : 0u));
186 bool hasMetadataHashEntry() const {
187 return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
189 void setHasMetadataHashEntry(bool HasEntry) {
190 setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
193 StringRef getSectionImpl() const;
196 } // end namespace llvm
198 #endif // LLVM_IR_GLOBALOBJECT_H