LLVM 20.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"
21
22namespace llvm {
23
24class Comdat;
25class Metadata;
26
27class GlobalObject : public GlobalValue {
28public:
29 // VCallVisibility - values for visibility metadata attached to vtables. This
30 // describes the scope in which a virtual call could end up being dispatched
31 // through this vtable.
33 // Type is potentially visible to external code.
35 // Type is only visible to code which will be in the current Module after
36 // LTO internalization.
38 // Type is only visible to code in the current Module.
40 };
41
42protected:
43 GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
45 unsigned AddressSpace = 0)
46 : GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace) {
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
70 /// FIXME: Remove this function once transition to Align is over.
73 return Align ? Align->value() : 0;
74 }
75
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
106 /// Check if this global has a custom object file section.
107 ///
108 /// This is more efficient than calling getSection() and checking for an empty
109 /// string.
110 bool hasSection() const {
112 }
113
114 /// Get the custom section of this global if it has one.
115 ///
116 /// If this global does not have a custom section, this will be empty and the
117 /// default object file section (.text, .data, etc) will be used.
119 return hasSection() ? getSectionImpl() : StringRef();
120 }
121
122 /// Change the section for this global.
123 ///
124 /// Setting the section to the empty string tells LLVM to choose an
125 /// appropriate default object file section.
126 void setSection(StringRef S);
127
128 bool hasComdat() const { return getComdat() != nullptr; }
129 const Comdat *getComdat() const { return ObjComdat; }
131 void setComdat(Comdat *C);
132
133 using Value::addMetadata;
138 using Value::getMetadata;
139 using Value::hasMetadata;
140 using Value::setMetadata;
141
142 /// Copy metadata from Src, adjusting offsets by Offset.
143 void copyMetadata(const GlobalObject *Src, unsigned Offset);
144
145 void addTypeMetadata(unsigned Offset, Metadata *TypeID);
148
149 /// Returns true if the alignment of the value can be unilaterally
150 /// increased.
151 ///
152 /// Note that for functions this is the alignment of the code, not the
153 /// alignment of a function pointer.
154 bool canIncreaseAlignment() const;
155
156protected:
157 void copyAttributesFrom(const GlobalObject *Src);
158
159public:
160 // Methods for support type inquiry through isa, cast, and dyn_cast:
161 static bool classof(const Value *V) {
162 return V->getValueID() == Value::FunctionVal ||
163 V->getValueID() == Value::GlobalVariableVal ||
164 V->getValueID() == Value::GlobalIFuncVal;
165 }
166
167private:
168 void setGlobalObjectFlag(unsigned Bit, bool Val) {
169 unsigned Mask = 1 << Bit;
171 (Val ? Mask : 0u));
172 }
173
174 StringRef getSectionImpl() const;
175};
176
177} // end namespace llvm
178
179#endif // LLVM_IR_GLOBALOBJECT_H
std::string Name
Type::TypeID TypeID
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Comdat * getComdat()
Definition: GlobalObject.h:130
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:118
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1803
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
static bool classof(const Value *V)
Definition: GlobalObject.h:161
static const unsigned GlobalObjectSubClassDataBits
Definition: GlobalObject.h:59
GlobalObject(const GlobalObject &)=delete
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:137
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1762
void setComdat(Comdat *C)
Definition: Globals.cpp:206
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
Definition: GlobalObject.h:43
bool hasComdat() const
Definition: GlobalObject.h:128
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1822
void copyAttributesFrom(const GlobalObject *Src)
Definition: Globals.cpp:147
uint64_t getAlignment() const
FIXME: Remove this function once transition to Align is over.
Definition: GlobalObject.h:71
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:267
const Comdat * getComdat() const
Definition: GlobalObject.h:129
void setGlobalObjectSubClassData(unsigned Val)
Definition: GlobalObject.h:99
unsigned getGlobalObjectSubClassData() const
Definition: GlobalObject.h:94
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:110
bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition: Globals.cpp:304
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1812
unsigned Visibility
Definition: GlobalValue.h:99
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:94
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:175
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:178
unsigned Linkage
Definition: GlobalValue.h:98
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
Root of the metadata hierarchy.
Definition: Metadata.h:62
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:589
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1494
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1484
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1539
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1528
void eraseMetadataIf(function_ref< bool(unsigned, MDNode *)> Pred)
Erase all metadata attachments matching the given predicate.
Definition: Metadata.cpp:1551
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1566
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
ValueTy
Concrete subclass of this.
Definition: Value.h:513
@ 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:480
AddressSpace
Definition: NVPTXBaseInfo.h:21
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
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117