LLVM 22.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:
45 const Twine &Name, unsigned AddressSpace = 0)
48 }
50
51 Comdat *ObjComdat = nullptr;
52 enum {
56
58 };
59 static const unsigned GlobalObjectSubClassDataBits =
61
62private:
63 static const unsigned AlignmentBits = LastAlignmentBit + 1;
64 static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
65 static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
66
67public:
68 GlobalObject(const GlobalObject &) = delete;
69
70protected:
71 /// Returns the alignment of the given variable or function.
72 ///
73 /// Note that for functions this is the alignment of the code, not the
74 /// alignment of a function pointer.
77 unsigned AlignmentData = Data & AlignmentMask;
78 return decodeMaybeAlign(AlignmentData);
79 }
80
81 /// Sets the alignment attribute of the GlobalObject.
83
84 /// Sets the alignment attribute of the GlobalObject.
85 /// This method will be deprecated as the alignment property should always be
86 /// defined.
88
89 unsigned getGlobalObjectSubClassData() const {
90 unsigned ValueData = getGlobalValueSubClassData();
91 return ValueData >> GlobalObjectBits;
92 }
93
94 void setGlobalObjectSubClassData(unsigned Val) {
95 unsigned OldData = getGlobalValueSubClassData();
96 setGlobalValueSubClassData((OldData & GlobalObjectMask) |
97 (Val << GlobalObjectBits));
98 assert(getGlobalObjectSubClassData() == Val && "representation error");
99 }
100
101public:
102 /// Check if this global has a custom object file section.
103 ///
104 /// This is more efficient than calling getSection() and checking for an empty
105 /// string.
106 bool hasSection() const {
108 }
109
110 /// Get the custom section of this global if it has one.
111 ///
112 /// If this global does not have a custom section, this will be empty and the
113 /// default object file section (.text, .data, etc) will be used.
115 return hasSection() ? getSectionImpl() : StringRef();
116 }
117
118 /// Change the section for this global.
119 ///
120 /// Setting the section to the empty string tells LLVM to choose an
121 /// appropriate default object file section.
123
124 /// Set the section prefix for this global object.
126
127 /// Get the section prefix for this global object.
128 LLVM_ABI std::optional<StringRef> getSectionPrefix() const;
129
130 bool hasComdat() const { return getComdat() != nullptr; }
131 const Comdat *getComdat() const { return ObjComdat; }
133 LLVM_ABI void setComdat(Comdat *C);
134
135 using Value::addMetadata;
140 using Value::getMetadata;
141 using Value::hasMetadata;
142 using Value::setMetadata;
143
144 /// Copy metadata from Src, adjusting offsets by Offset.
145 LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset);
146
150
151 /// Returns true if the alignment of the value can be unilaterally
152 /// increased.
153 ///
154 /// Note that for functions this is the alignment of the code, not the
155 /// alignment of a function pointer.
156 LLVM_ABI bool canIncreaseAlignment() const;
157
158protected:
160
161public:
162 // Methods for support type inquiry through isa, cast, and dyn_cast:
163 static bool classof(const Value *V) {
164 return V->getValueID() == Value::FunctionVal ||
165 V->getValueID() == Value::GlobalVariableVal ||
166 V->getValueID() == Value::GlobalIFuncVal;
167 }
168
169private:
170 void setGlobalObjectFlag(unsigned Bit, bool Val) {
171 unsigned Mask = 1 << Bit;
173 (Val ? Mask : 0u));
174 }
175
176 LLVM_ABI StringRef getSectionImpl() const;
177};
178
179} // end namespace llvm
180
181#endif // LLVM_IR_GLOBALOBJECT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
dxil translate DXIL Translate Metadata
std::string Name
Type::TypeID TypeID
Comdat * getComdat()
Definition: GlobalObject.h:132
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:114
LLVM_ABI void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1881
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:75
static bool classof(const Value *V)
Definition: GlobalObject.h:163
static const unsigned GlobalObjectSubClassDataBits
Definition: GlobalObject.h:59
GlobalObject(const GlobalObject &)=delete
LLVM_ABI void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:145
GlobalObject(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
Definition: GlobalObject.h:44
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1840
LLVM_ABI void setComdat(Comdat *C)
Definition: Globals.cpp:214
bool hasComdat() const
Definition: GlobalObject.h:130
LLVM_ABI VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1900
LLVM_ABI void copyAttributesFrom(const GlobalObject *Src)
Definition: Globals.cpp:155
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:275
const Comdat * getComdat() const
Definition: GlobalObject.h:131
LLVM_ABI void setSectionPrefix(StringRef Prefix)
Set the section prefix for this global object.
Definition: Globals.cpp:291
LLVM_ABI ~GlobalObject()
Definition: Globals.cpp:105
LLVM_ABI std::optional< StringRef > getSectionPrefix() const
Get the section prefix for this global object.
Definition: Globals.cpp:297
void setGlobalObjectSubClassData(unsigned Val)
Definition: GlobalObject.h:94
unsigned getGlobalObjectSubClassData() const
Definition: GlobalObject.h:89
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:106
LLVM_ABI bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition: Globals.cpp:330
LLVM_ABI void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1890
unsigned Visibility
Definition: GlobalValue.h:101
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:96
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:177
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:180
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:52
Root of the metadata hierarchy.
Definition: Metadata.h:63
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:45
LLVM Value Representation.
Definition: Value.h:75
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:602
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1571
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1561
LLVM_ABI bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1616
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1605
LLVM_ABI void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata attachments matching the given predicate.
Definition: Metadata.cpp:1628
LLVM_ABI void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1643
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:576
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.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition: Alignment.h:220
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:117
Information about how a User object was allocated, to be passed into the User constructor.
Definition: User.h:79