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