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::getMetadata;
148 using Value::hasMetadata;
149 using Value::setMetadata;
150
152
153 /// Copy metadata from Src, adjusting offsets by Offset.
154 LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset);
155
159
160 /// Returns true if the alignment of the value can be unilaterally
161 /// increased.
162 ///
163 /// Note that for functions this is the alignment of the code, not the
164 /// alignment of a function pointer.
165 LLVM_ABI bool canIncreaseAlignment() const;
166
167protected:
169
170public:
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 ||
175 V->getValueID() == Value::GlobalIFuncVal;
176 }
177
178private:
179 void setGlobalObjectFlag(unsigned Bit, bool Val) {
180 unsigned Mask = 1 << Bit;
182 (Val ? Mask : 0u));
183 }
184
185 LLVM_ABI StringRef getSectionImpl() const;
186};
187
188} // end namespace llvm
189
190#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:394
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:294
static bool classof(const Value *V)
static const unsigned GlobalObjectSubClassDataBits
GlobalObject(const GlobalObject &)=delete
LLVM_ABI void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition Globals.cpp:148
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:217
bool hasComdat() const
LLVM_ABI VCallVisibility getVCallVisibility() const
LLVM_ABI void copyAttributesFrom(const GlobalObject *Src)
Definition Globals.cpp:158
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:278
const Comdat * getComdat() const
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:312
void setGlobalObjectSubClassData(unsigned Val)
unsigned getGlobalObjectSubClassData() const
bool hasSection() const
Check if this global has a custom object file section.
friend class Value
LLVM_ABI bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition Globals.cpp:345
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
Root of the metadata hierarchy.
Definition Metadata.h:64
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
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition Value.h:608
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 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 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.
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition Value.h:577
ValueTy
Concrete subclass of this.
Definition Value.h:525
@ 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