LLVM 19.0.0git
MDBuilder.cpp
Go to the documentation of this file.
1//===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
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 file defines the MDBuilder class, which is used as a convenient way to
10// create LLVM metadata with a consistent and simplified interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/MDBuilder.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Metadata.h"
18using namespace llvm;
19
21 return MDString::get(Context, Str);
22}
23
26}
27
29 if (Accuracy == 0.0)
30 return nullptr;
31 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
32 auto *Op =
33 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
34 return MDNode::get(Context, Op);
35}
36
38 uint32_t FalseWeight) {
39 return createBranchWeights({TrueWeight, FalseWeight});
40}
41
43 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
44 return createBranchWeights((1U << 20) - 1, 1);
45}
46
48 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
49 return createBranchWeights(1, (1U << 20) - 1);
50}
51
53 assert(Weights.size() >= 1 && "Need at least one branch weights!");
54
55 SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
56 Vals[0] = createString("branch_weights");
57
58 Type *Int32Ty = Type::getInt32Ty(Context);
59 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
60 Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
61
62 return MDNode::get(Context, Vals);
63}
64
66 return MDNode::get(Context, std::nullopt);
67}
68
70 uint64_t Count, bool Synthetic,
71 const DenseSet<GlobalValue::GUID> *Imports) {
72 Type *Int64Ty = Type::getInt64Ty(Context);
74 if (Synthetic)
75 Ops.push_back(createString("synthetic_function_entry_count"));
76 else
77 Ops.push_back(createString("function_entry_count"));
78 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
79 if (Imports) {
80 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
81 llvm::sort(OrderID);
82 for (auto ID : OrderID)
83 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
84 }
85 return MDNode::get(Context, Ops);
86}
87
89 return MDNode::get(
90 Context, {createString("function_section_prefix"), createString(Prefix)});
91}
92
94 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
95
96 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
97 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
98}
99
101 // If the range is everything then it is useless.
102 if (Hi == Lo)
103 return nullptr;
104
105 // Return the range [Lo, Hi).
106 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
107}
108
111 for (Function *F : Callees)
113 return MDNode::get(Context, Ops);
114}
115
118 bool VarArgArePassed) {
120
121 Type *Int64 = Type::getInt64Ty(Context);
122 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
123
124 for (int ArgNo : Arguments)
125 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
126
127 Type *Int1 = Type::getInt1Ty(Context);
128 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
129
130 return MDNode::get(Context, Ops);
131}
132
134 MDNode *NewCB) {
135 if (!ExistingCallbacks)
136 return MDNode::get(Context, {NewCB});
137
138 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
139 uint64_t NewCBCalleeIdx =
140 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
141 (void)NewCBCalleeIdx;
142
144 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
145 Ops.resize(NumExistingOps + 1);
146
147 for (unsigned u = 0; u < NumExistingOps; u++) {
148 Ops[u] = ExistingCallbacks->getOperand(u);
149
150 auto *OldCBCalleeIdxAsCM =
151 cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0));
152 uint64_t OldCBCalleeIdx =
153 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
154 (void)OldCBCalleeIdx;
155 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
156 "Cannot map a callback callee index twice!");
157 }
158
159 Ops[NumExistingOps] = NewCB;
160 return MDNode::get(Context, Ops);
161}
162
164 Constant *RTTI) {
166 Ops.push_back(createConstant(PrologueSig));
167 Ops.push_back(createConstant(RTTI));
168 return MDNode::get(Context, Ops);
169}
170
173
174 for (const auto &Entry : Sections) {
175 const StringRef &Sec = Entry.first;
176 Ops.push_back(createString(Sec));
177
178 // If auxiliary data for this section exists, append it.
179 const SmallVector<Constant *> &AuxConsts = Entry.second;
180 if (!AuxConsts.empty()) {
182 AuxMDs.reserve(AuxConsts.size());
183 for (Constant *C : AuxConsts)
184 AuxMDs.push_back(createConstant(C));
185 Ops.push_back(MDNode::get(Context, AuxMDs));
186 }
187 }
188
189 return MDNode::get(Context, Ops);
190}
191
193 SmallVector<Metadata *, 3> Args(1, nullptr);
194 if (Extra)
195 Args.push_back(Extra);
196 if (!Name.empty())
197 Args.push_back(createString(Name));
198 MDNode *Root = MDNode::getDistinct(Context, Args);
199
200 // At this point we have
201 // !0 = distinct !{null} <- root
202 // Replace the reserved operand with the root node itself.
203 Root->replaceOperandWith(0, Root);
204
205 // We now have
206 // !0 = distinct !{!0} <- root
207 return Root;
208}
209
211 return MDNode::get(Context, createString(Name));
212}
213
214/// Return metadata for a non-root TBAA node with the given name,
215/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
217 bool isConstant) {
218 if (isConstant) {
219 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
220 return MDNode::get(Context,
221 {createString(Name), Parent, createConstant(Flags)});
222 }
223 return MDNode::get(Context, {createString(Name), Parent});
224}
225
227 return MDNode::get(Context, createString(Name));
228}
229
231 return MDNode::get(Context, {createString(Name), Domain});
232}
233
234/// Return metadata for a tbaa.struct node with the given
235/// struct field descriptions.
237 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
238 Type *Int64 = Type::getInt64Ty(Context);
239 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
240 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
241 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
242 Vals[i * 3 + 2] = Fields[i].Type;
243 }
244 return MDNode::get(Context, Vals);
245}
246
247/// Return metadata for a TBAA struct node in the type DAG
248/// with the given name, a list of pairs (offset, field type in the type DAG).
250 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
251 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
252 Type *Int64 = Type::getInt64Ty(Context);
253 Ops[0] = createString(Name);
254 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
255 Ops[i * 2 + 1] = Fields[i].first;
256 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
257 }
258 return MDNode::get(Context, Ops);
259}
260
261/// Return metadata for a TBAA scalar type node with the
262/// given name, an offset and a parent in the TBAA type DAG.
265 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
266 return MDNode::get(Context,
267 {createString(Name), Parent, createConstant(Off)});
268}
269
270/// Return metadata for a TBAA tag node with the given
271/// base type, access type and offset relative to the base type.
273 uint64_t Offset, bool IsConstant) {
275 ConstantInt *Off = ConstantInt::get(Int64, Offset);
276 if (IsConstant) {
277 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
278 createConstant(ConstantInt::get(Int64, 1))});
279 }
280 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
281}
282
284 Metadata *Id,
286 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
287 Type *Int64 = Type::getInt64Ty(Context);
288 Ops[0] = Parent;
289 Ops[1] = createConstant(ConstantInt::get(Int64, Size));
290 Ops[2] = Id;
291 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
292 Ops[I * 3 + 3] = Fields[I].Type;
293 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
294 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
295 }
296 return MDNode::get(Context, Ops);
297}
298
301 bool IsImmutable) {
303 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
304 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
305 if (IsImmutable) {
306 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
307 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
308 ImmutabilityFlagNode});
309 }
310 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
311}
312
314 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
315 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
316 Metadata *OffsetNode = Tag->getOperand(2);
317 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
318
319 bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
320
321 // See if the tag is already mutable.
322 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
323 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
324 return Tag;
325
326 // If Tag is already mutable then return it.
327 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
328 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
329 return Tag;
330
331 // Otherwise, create another node.
332 if (!NewFormat)
333 return createTBAAStructTagNode(BaseType, AccessType, Offset);
334
335 Metadata *SizeNode = Tag->getOperand(3);
336 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
337 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
338}
339
341 Metadata *Vals[] = {
342 createString("loop_header_weight"),
343 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
344 };
345 return MDNode::get(Context, Vals);
346}
347
349 StringRef FName) {
350 auto *Int64Ty = Type::getInt64Ty(Context);
352 Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
353 Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
354 Ops[2] = createString(FName);
355 return MDNode::get(Context, Ops);
356}
357
358MDNode *
359MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {
360 auto *Int64Ty = Type::getInt64Ty(Context);
361 SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);
362 for (size_t I = 0; I < LLVMStats.size(); I++) {
363 Ops[I * 2] = createString(LLVMStats[I].first);
364 Ops[I * 2 + 1] =
365 createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second));
366 }
367 return MDNode::get(Context, Ops);
368}
static bool isConstant(const MachineInstr &MI)
AMDGPU Lower Kernel Arguments
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::string Name
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
IntegerType * Int32Ty
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:76
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Class to represent integer types.
Definition: DerivedTypes.h:40
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
MDNode * createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, uint64_t Size, bool IsImmutable=false)
Return metadata for a TBAA access tag with the given base type, final access type,...
Definition: MDBuilder.cpp:299
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:116
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:192
MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean \Synthetic indicating whether th...
Definition: MDBuilder.cpp:69
MDNode * createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName)
Return metadata containing the pseudo probe descriptor for a function.
Definition: MDBuilder.cpp:348
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
MDNode * createPCSections(ArrayRef< PCSection > Sections)
Return metadata for PC sections.
Definition: MDBuilder.cpp:171
MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree,...
Definition: MDBuilder.cpp:216
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:210
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
Definition: MDBuilder.cpp:263
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:340
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:88
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:65
MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t > > Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset,...
Definition: MDBuilder.cpp:249
MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:109
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:226
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:93
MDNode * createLikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards true destination.
Definition: MDBuilder.cpp:42
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:236
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:133
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:313
MDNode * createLLVMStats(ArrayRef< std::pair< StringRef, uint64_t > > LLVMStatsVec)
Return metadata containing llvm statistics.
Definition: MDBuilder.cpp:359
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
MDNode * createRTTIPointerPrologue(Constant *PrologueSig, Constant *RTTI)
Return metadata feeding to the CodeGen about how to generate a function prologue for the "function" s...
Definition: MDBuilder.cpp:163
MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition: MDBuilder.cpp:47
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:230
MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, bool IsConstant=false)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
Definition: MDBuilder.cpp:272
MDNode * createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id, ArrayRef< TBAAStructField > Fields=ArrayRef< TBAAStructField >())
Return metadata for a TBAA type node in the TBAA type DAG with the given parent type,...
Definition: MDBuilder.cpp:283
Metadata node.
Definition: Metadata.h:1067
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:1071
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1549
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1434
A single uniqued string.
Definition: Metadata.h:720
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:600
Root of the metadata hierarchy.
Definition: Metadata.h:62
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
void reserve(size_type N)
Definition: SmallVector.h:676
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt1Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
static Type * getFloatTy(LLVMContext &C)
@ 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:456
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647