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