LLVM 23.0.0git
GlobalObject.h
Go to the documentation of this file.
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.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_GLOBALOBJECT_H
15#define LLVM_IR_GLOBALOBJECT_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/IR/Value.h"
22
23namespace llvm {
24
25class Comdat;
26class Metadata;
27
28class GlobalObject : public GlobalValue {
29public:
30 // VCallVisibility - values for visibility metadata attached to vtables. This
31 // describes the scope in which a virtual call could end up being dispatched
32 // through this vtable.
34 // Type is potentially visible to external code.
36 // Type is only visible to code which will be in the current Module after
37 // LTO internalization.
39 // Type is only visible to code in the current Module.
41 };
42
43protected:
50
51 Comdat *ObjComdat = nullptr;
52
53 friend class Value;
54 /// Index of first metadata attachment in context, or zero.
55 unsigned MetadataIndex = 0;
56
57 enum {
61
63 };
64 static const unsigned GlobalObjectSubClassDataBits =
66
67private:
68 static const unsigned AlignmentBits = LastAlignmentBit + 1;
69 static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
70 static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
71
72public:
73 GlobalObject(const GlobalObject &) = delete;
74
75protected:
76 /// Returns the alignment of the given variable or function.
77 ///
78 /// Note that for functions this is the alignment of the code, not the
79 /// alignment of a function pointer.
82 unsigned AlignmentData = Data & AlignmentMask;
83 return decodeMaybeAlign(AlignmentData);
84 }
85
86 /// Sets the alignment attribute of the GlobalObject.
88
89 /// Sets the alignment attribute of the GlobalObject.
90 /// This method will be deprecated as the alignment property should always be
91 /// defined.
93
94 unsigned getGlobalObjectSubClassData() const {
95 unsigned ValueData = getGlobalValueSubClassData();
96 return ValueData >> GlobalObjectBits;
97 }
98
99 void setGlobalObjectSubClassData(unsigned Val) {
100 unsigned OldData = getGlobalValueSubClassData();
101 setGlobalValueSubClassData((OldData & GlobalObjectMask) |
102 (Val << GlobalObjectBits));
103 assert(getGlobalObjectSubClassData() == Val && "representation error");
104 }
105
106public:
107 /// Check if this global has a custom object file section.
108 ///
109 /// This is more efficient than calling getSection() and checking for an empty
110 /// string.
111 bool hasSection() const {
113 }
114
115 /// Get the custom section of this global if it has one.
116 ///
117 /// If this global does not have a custom section, this will be empty and the
118 /// default object file section (.text, .data, etc) will be used.
120 return hasSection() ? getSectionImpl() : StringRef();
121 }
122
123 /// Change the section for this global.
124 ///
125 /// Setting the section to the empty string tells LLVM to choose an
126 /// appropriate default object file section.
128
129 /// If existing prefix is different from \p Prefix, set it to \p Prefix. If \p
130 /// Prefix is empty, the set clears the existing metadata. Returns true if
131 /// section prefix changed and false otherwise.
133
134 /// Get the section prefix for this global object.
135 LLVM_ABI std::optional<StringRef> getSectionPrefix() const;
136
137 bool hasComdat() const { return getComdat() != nullptr; }
138 const Comdat *getComdat() const { return ObjComdat; }
140 LLVM_ABI void setComdat(Comdat *C);
141
142 using Value::addMetadata;
147 using Value::setMetadata;
148
149 /// Return true if this GlobalObject has any metadata attached to it.
150 bool hasMetadata() const { return MetadataIndex != 0; }
151
152 /// Return true if this instruction has the given type of metadata attached.
153 bool hasMetadata(unsigned KindID) const {
154 return getMetadata(KindID) != nullptr;
155 }
156
157 /// Return true if this instruction has the given type of metadata attached.
158 bool hasMetadata(StringRef Kind) const {
159 return getMetadata(Kind) != nullptr;
160 }
161
162 /// Get the metadata of given kind attached to this GlobalObject.
163 /// If the metadata is not found then return null.
164 MDNode *getMetadata(unsigned KindID) const {
165 return hasMetadata() ? getMetadataImpl(KindID) : nullptr;
166 }
167
168 /// Get the metadata of given kind attached to this GlobalObject.
169 /// If the metadata is not found then return null.
171 return hasMetadata() ? Value::getMetadata(Kind) : nullptr;
172 }
173
174 /// Appends all attachments with the given ID to \c MDs in insertion order.
175 /// If the Value has no attachments with the given ID, or if ID is invalid,
176 /// leaves MDs unchanged.
177 /// @{
178 LLVM_ABI void getMetadata(unsigned KindID,
179 SmallVectorImpl<MDNode *> &MDs) const;
181 SmallVectorImpl<MDNode *> &MDs) const;
182 /// @}
183
185
186 /// Copy metadata from Src, adjusting offsets by Offset.
187 LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset);
188
192
193 /// Returns true if the alignment of the value can be unilaterally
194 /// increased.
195 ///
196 /// Note that for functions this is the alignment of the code, not the
197 /// alignment of a function pointer.
198 LLVM_ABI bool canIncreaseAlignment() const;
199
200protected:
202
203public:
204 // Methods for support type inquiry through isa, cast, and dyn_cast:
205 static bool classof(const Value *V) {
206 return V->getValueID() == Value::FunctionVal ||
207 V->getValueID() == Value::GlobalVariableVal ||
208 V->getValueID() == Value::GlobalIFuncVal;
209 }
210
211private:
212 void setGlobalObjectFlag(unsigned Bit, bool Val) {
213 unsigned Mask = 1 << Bit;
215 (Val ? Mask : 0u));
216 }
217
218 LLVM_ABI StringRef getSectionImpl() const;
219};
220
221} // end namespace llvm
222
223#endif // LLVM_IR_GLOBALOBJECT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
Type::TypeID TypeID
LLVM_ABI bool hasMetadataOtherThanDebugLoc() const
Definition Globals.cpp:400
StringRef getSection() const
Get the custom section of this global if it has one.
LLVM_ABI void addTypeMetadata(unsigned Offset, Metadata *TypeID)
unsigned MetadataIndex
Index of first metadata attachment in context, or zero.
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
LLVM_ABI bool setSectionPrefix(StringRef Prefix)
If existing prefix is different from Prefix, set it to Prefix.
Definition Globals.cpp:300
static bool classof(const Value *V)
static const unsigned GlobalObjectSubClassDataBits
GlobalObject(const GlobalObject &)=delete
bool hasMetadata() const
Return true if this GlobalObject has any metadata attached to it.
LLVM_ABI void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition Globals.cpp:154
GlobalObject(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:223
MDNode * getMetadata(StringRef Kind) const
Get the metadata of given kind attached to this GlobalObject.
bool hasComdat() const
LLVM_ABI VCallVisibility getVCallVisibility() const
LLVM_ABI void copyAttributesFrom(const GlobalObject *Src)
Definition Globals.cpp:164
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:284
const Comdat * getComdat() const
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
LLVM_ABI ~GlobalObject()
Definition Globals.cpp:108
LLVM_ABI std::optional< StringRef > getSectionPrefix() const
Get the section prefix for this global object.
Definition Globals.cpp:318
void setGlobalObjectSubClassData(unsigned Val)
unsigned getGlobalObjectSubClassData() const
bool hasSection() const
Check if this global has a custom object file section.
bool hasMetadata(StringRef Kind) const
Return true if this instruction has the given type of metadata attached.
bool hasMetadata(unsigned KindID) const
Return true if this instruction has the given type of metadata attached.
friend class Value
LLVM_ABI bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition Globals.cpp:351
LLVM_ABI void setVCallVisibilityMetadata(VCallVisibility Visibility)
static const unsigned GlobalValueSubClassDataBits
Definition GlobalValue.h:96
unsigned getGlobalValueSubClassData() const
void setGlobalValueSubClassData(unsigned V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
GlobalValue(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace)
Definition GlobalValue.h:81
Metadata node.
Definition Metadata.h:1080
Root of the metadata hierarchy.
Definition Metadata.h:64
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
LLVM_ABI MDNode * getMetadataImpl(unsigned KindID) const LLVM_READONLY
Get metadata for the given kind, if any.
LLVM_ABI bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
LLVM_ABI MDNode * getMetadata(StringRef Kind) const LLVM_READONLY
Get the current metadata attachments for the given kind, if any.
LLVM_ABI void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata attachments matching the given predicate.
LLVM_ABI void clearMetadata()
Erase all metadata attached to this Value.
ValueTy
Concrete subclass of this.
Definition Value.h:524
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition Alignment.h:209
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Information about how a User object was allocated, to be passed into the User constructor.
Definition User.h:79