LLVM 20.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, bool IsExpected) {
39 return createBranchWeights({TrueWeight, FalseWeight}, IsExpected);
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 bool IsExpected) {
54 assert(Weights.size() >= 1 && "Need at least one branch weights!");
55
56 unsigned int Offset = IsExpected ? 2 : 1;
57 SmallVector<Metadata *, 4> Vals(Weights.size() + Offset);
58 Vals[0] = createString("branch_weights");
59 if (IsExpected)
60 Vals[1] = createString("expected");
61
62 Type *Int32Ty = Type::getInt32Ty(Context);
63 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
64 Vals[i + Offset] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
65
66 return MDNode::get(Context, Vals);
67}
68
70 return MDNode::get(Context, std::nullopt);
71}
72
74 uint64_t Count, bool Synthetic,
75 const DenseSet<GlobalValue::GUID> *Imports) {
76 Type *Int64Ty = Type::getInt64Ty(Context);
78 if (Synthetic)
79 Ops.push_back(createString("synthetic_function_entry_count"));
80 else
81 Ops.push_back(createString("function_entry_count"));
82 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
83 if (Imports) {
84 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
85 llvm::sort(OrderID);
86 for (auto ID : OrderID)
87 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
88 }
89 return MDNode::get(Context, Ops);
90}
91
93 return MDNode::get(
94 Context, {createString("function_section_prefix"), createString(Prefix)});
95}
96
98 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
99
100 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
101 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
102}
103
105 // If the range is everything then it is useless.
106 if (Hi == Lo)
107 return nullptr;
108
109 // Return the range [Lo, Hi).
110 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
111}
112
115 for (Function *F : Callees)
117 return MDNode::get(Context, Ops);
118}
119
122 bool VarArgArePassed) {
124
125 Type *Int64 = Type::getInt64Ty(Context);
126 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
127
128 for (int ArgNo : Arguments)
129 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
130
131 Type *Int1 = Type::getInt1Ty(Context);
132 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
133
134 return MDNode::get(Context, Ops);
135}
136
138 MDNode *NewCB) {
139 if (!ExistingCallbacks)
140 return MDNode::get(Context, {NewCB});
141
142 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
143 uint64_t NewCBCalleeIdx =
144 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
145 (void)NewCBCalleeIdx;
146
148 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
149 Ops.resize(NumExistingOps + 1);
150
151 for (unsigned u = 0; u < NumExistingOps; u++) {
152 Ops[u] = ExistingCallbacks->getOperand(u);
153
154 auto *OldCBCalleeIdxAsCM =
155 cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0));
156 uint64_t OldCBCalleeIdx =
157 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
158 (void)OldCBCalleeIdx;
159 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
160 "Cannot map a callback callee index twice!");
161 }
162
163 Ops[NumExistingOps] = NewCB;
164 return MDNode::get(Context, Ops);
165}
166
168 Constant *RTTI) {
170 Ops.push_back(createConstant(PrologueSig));
171 Ops.push_back(createConstant(RTTI));
172 return MDNode::get(Context, Ops);
173}
174
177
178 for (const auto &Entry : Sections) {
179 const StringRef &Sec = Entry.first;
180 Ops.push_back(createString(Sec));
181
182 // If auxiliary data for this section exists, append it.
183 const SmallVector<Constant *> &AuxConsts = Entry.second;
184 if (!AuxConsts.empty()) {
186 AuxMDs.reserve(AuxConsts.size());
187 for (Constant *C : AuxConsts)
188 AuxMDs.push_back(createConstant(C));
189 Ops.push_back(MDNode::get(Context, AuxMDs));
190 }
191 }
192
193 return MDNode::get(Context, Ops);
194}
195
197 SmallVector<Metadata *, 3> Args(1, nullptr);
198 if (Extra)
199 Args.push_back(Extra);
200 if (!Name.empty())
201 Args.push_back(createString(Name));
202 MDNode *Root = MDNode::getDistinct(Context, Args);
203
204 // At this point we have
205 // !0 = distinct !{null} <- root
206 // Replace the reserved operand with the root node itself.
207 Root->replaceOperandWith(0, Root);
208
209 // We now have
210 // !0 = distinct !{!0} <- root
211 return Root;
212}
213
215 return MDNode::get(Context, createString(Name));
216}
217
218/// Return metadata for a non-root TBAA node with the given name,
219/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
221 bool isConstant) {
222 if (isConstant) {
223 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
224 return MDNode::get(Context,
225 {createString(Name), Parent, createConstant(Flags)});
226 }
227 return MDNode::get(Context, {createString(Name), Parent});
228}
229
231 return MDNode::get(Context, createString(Name));
232}
233
235 return MDNode::get(Context, {createString(Name), Domain});
236}
237
238/// Return metadata for a tbaa.struct node with the given
239/// struct field descriptions.
241 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
242 Type *Int64 = Type::getInt64Ty(Context);
243 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
244 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
245 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
246 Vals[i * 3 + 2] = Fields[i].Type;
247 }
248 return MDNode::get(Context, Vals);
249}
250
251/// Return metadata for a TBAA struct node in the type DAG
252/// with the given name, a list of pairs (offset, field type in the type DAG).
254 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
255 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
256 Type *Int64 = Type::getInt64Ty(Context);
257 Ops[0] = createString(Name);
258 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
259 Ops[i * 2 + 1] = Fields[i].first;
260 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
261 }
262 return MDNode::get(Context, Ops);
263}
264
265/// Return metadata for a TBAA scalar type node with the
266/// given name, an offset and a parent in the TBAA type DAG.
269 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
270 return MDNode::get(Context,
271 {createString(Name), Parent, createConstant(Off)});
272}
273
274/// Return metadata for a TBAA tag node with the given
275/// base type, access type and offset relative to the base type.
277 uint64_t Offset, bool IsConstant) {
279 ConstantInt *Off = ConstantInt::get(Int64, Offset);
280 if (IsConstant) {
281 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
282 createConstant(ConstantInt::get(Int64, 1))});
283 }
284 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
285}
286
288 Metadata *Id,
290 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
291 Type *Int64 = Type::getInt64Ty(Context);
292 Ops[0] = Parent;
293 Ops[1] = createConstant(ConstantInt::get(Int64, Size));
294 Ops[2] = Id;
295 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
296 Ops[I * 3 + 3] = Fields[I].Type;
297 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
298 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
299 }
300 return MDNode::get(Context, Ops);
301}
302
305 bool IsImmutable) {
307 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
308 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
309 if (IsImmutable) {
310 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
311 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
312 ImmutabilityFlagNode});
313 }
314 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
315}
316
318 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
319 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
320 Metadata *OffsetNode = Tag->getOperand(2);
321 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
322
323 bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
324
325 // See if the tag is already mutable.
326 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
327 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
328 return Tag;
329
330 // If Tag is already mutable then return it.
331 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
332 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
333 return Tag;
334
335 // Otherwise, create another node.
336 if (!NewFormat)
337 return createTBAAStructTagNode(BaseType, AccessType, Offset);
338
339 Metadata *SizeNode = Tag->getOperand(3);
340 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
341 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
342}
343
345 Metadata *Vals[] = {
346 createString("loop_header_weight"),
347 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
348 };
349 return MDNode::get(Context, Vals);
350}
351
353 StringRef FName) {
354 auto *Int64Ty = Type::getInt64Ty(Context);
356 Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
357 Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
358 Ops[2] = createString(FName);
359 return MDNode::get(Context, Ops);
360}
361
362MDNode *
363MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {
364 auto *Int64Ty = Type::getInt64Ty(Context);
365 SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);
366 for (size_t I = 0; I < LLVMStats.size(); I++) {
367 Ops[I * 2] = createString(LLVMStats[I].first);
368 Ops[I * 2 + 1] =
369 createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second));
370 }
371 return MDNode::get(Context, Ops);
372}
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:78
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:81
This is an important base class in LLVM.
Definition: Constant.h:42
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:266
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:303
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:120
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:196
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:73
MDNode * createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, StringRef FName)
Return metadata containing the pseudo probe descriptor for a function.
Definition: MDBuilder.cpp:352
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:175
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:220
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:214
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
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:267
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:344
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:92
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:69
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:253
MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:113
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:230
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:97
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:240
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:137
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:317
MDNode * createLLVMStats(ArrayRef< std::pair< StringRef, uint64_t > > LLVMStatsVec)
Return metadata containing llvm statistics.
Definition: MDBuilder.cpp:363
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:167
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:234
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:276
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:287
Metadata node.
Definition: Metadata.h:1069
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:1077
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1550
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1430
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1542
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1436
A single uniqued string.
Definition: Metadata.h:720
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
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:480
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647