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"
18#include "llvm/IR/ProfDataUtils.h"
19using namespace llvm;
20
21MDString *MDBuilder::createString(StringRef Str) {
22 return MDString::get(Context, Str);
23}
24
25ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
26 return ConstantAsMetadata::get(C);
27}
28
29MDNode *MDBuilder::createFPMath(float Accuracy) {
30 if (Accuracy == 0.0)
31 return nullptr;
32 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
33 auto *Op =
34 createConstant(C: ConstantFP::get(Ty: Type::getFloatTy(C&: Context), V: Accuracy));
35 return MDNode::get(Context, MDs: Op);
36}
37
38MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
39 uint32_t FalseWeight, bool IsExpected) {
40 return createBranchWeights(Weights: {TrueWeight, FalseWeight}, IsExpected);
41}
42
43MDNode *MDBuilder::createLikelyBranchWeights() {
44 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
45 return createBranchWeights(TrueWeight: kLikelyBranchWeight, FalseWeight: kUnlikelyBranchWeight);
46}
47
48MDNode *MDBuilder::createUnlikelyBranchWeights() {
49 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
50 return createBranchWeights(TrueWeight: kUnlikelyBranchWeight, FalseWeight: kLikelyBranchWeight);
51}
52
53MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights,
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);
59 Vals[0] = createString(Str: MDProfLabels::BranchWeights);
60 if (IsExpected)
61 Vals[1] = createString(Str: MDProfLabels::ExpectedBranchWeights);
62
63 Type *Int32Ty = Type::getInt32Ty(C&: Context);
64 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
65 Vals[i + Offset] = createConstant(C: ConstantInt::get(Ty: Int32Ty, V: Weights[i]));
66
67 return MDNode::get(Context, MDs: Vals);
68}
69
70MDNode *MDBuilder::createUnpredictable() { return MDNode::get(Context, MDs: {}); }
71
72MDNode *MDBuilder::createFunctionEntryCount(
73 uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
74 Type *Int64Ty = Type::getInt64Ty(C&: Context);
75 SmallVector<Metadata *, 8> Ops;
76 Ops.push_back(Elt: createString(Str: MDProfLabels::FunctionEntryCount));
77 Ops.push_back(Elt: createConstant(C: ConstantInt::get(Ty: Int64Ty, V: Count)));
78 if (Imports) {
79 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
80 llvm::sort(C&: OrderID);
81 for (auto ID : OrderID)
82 Ops.push_back(Elt: createConstant(C: ConstantInt::get(Ty: Int64Ty, V: ID)));
83 }
84 return MDNode::get(Context, MDs: Ops);
85}
86
87MDNode *MDBuilder::createGlobalObjectSectionPrefix(StringRef Prefix) {
88 return MDNode::get(Context,
89 MDs: {createString(Str: "section_prefix"), createString(Str: Prefix)});
90}
91
92MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
93 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
94
95 Type *Ty = IntegerType::get(C&: Context, NumBits: Lo.getBitWidth());
96 return createRange(Lo: ConstantInt::get(Ty, V: Lo), Hi: ConstantInt::get(Ty, V: Hi));
97}
98
99MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
100 // If the range is everything then it is useless.
101 if (Hi == Lo)
102 return nullptr;
103
104 // Return the range [Lo, Hi).
105 return MDNode::get(Context, MDs: {createConstant(C: Lo), createConstant(C: Hi)});
106}
107
108MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
109 SmallVector<Metadata *, 4> Ops;
110 for (Function *F : Callees)
111 Ops.push_back(Elt: createConstant(C: F));
112 return MDNode::get(Context, MDs: Ops);
113}
114
115MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
116 ArrayRef<int> Arguments,
117 bool VarArgArePassed) {
118 SmallVector<Metadata *, 4> Ops;
119
120 Type *Int64 = Type::getInt64Ty(C&: Context);
121 Ops.push_back(Elt: createConstant(C: ConstantInt::get(Ty: Int64, V: CalleeArgNo)));
122
123 for (int ArgNo : Arguments)
124 Ops.push_back(Elt: createConstant(C: ConstantInt::get(Ty: Int64, V: ArgNo, IsSigned: true)));
125
126 Type *Int1 = Type::getInt1Ty(C&: Context);
127 Ops.push_back(Elt: createConstant(C: ConstantInt::get(Ty: Int1, V: VarArgArePassed)));
128
129 return MDNode::get(Context, MDs: Ops);
130}
131
132MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
133 MDNode *NewCB) {
134 if (!ExistingCallbacks)
135 return MDNode::get(Context, MDs: {NewCB});
136
137 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Val: NewCB->getOperand(I: 0));
138 uint64_t NewCBCalleeIdx =
139 cast<ConstantInt>(Val: NewCBCalleeIdxAsCM->getValue())->getZExtValue();
140 (void)NewCBCalleeIdx;
141
142 SmallVector<Metadata *, 4> Ops;
143 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
144 Ops.resize(N: NumExistingOps + 1);
145
146 for (unsigned u = 0; u < NumExistingOps; u++) {
147 Ops[u] = ExistingCallbacks->getOperand(I: u);
148
149 auto *OldCBCalleeIdxAsCM =
150 cast<ConstantAsMetadata>(Val: cast<MDNode>(Val: Ops[u])->getOperand(I: 0));
151 uint64_t OldCBCalleeIdx =
152 cast<ConstantInt>(Val: OldCBCalleeIdxAsCM->getValue())->getZExtValue();
153 (void)OldCBCalleeIdx;
154 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
155 "Cannot map a callback callee index twice!");
156 }
157
158 Ops[NumExistingOps] = NewCB;
159 return MDNode::get(Context, MDs: Ops);
160}
161
162MDNode *MDBuilder::createRTTIPointerPrologue(Constant *PrologueSig,
163 Constant *RTTI) {
164 SmallVector<Metadata *, 4> Ops;
165 Ops.push_back(Elt: createConstant(C: PrologueSig));
166 Ops.push_back(Elt: createConstant(C: RTTI));
167 return MDNode::get(Context, MDs: Ops);
168}
169
170MDNode *MDBuilder::createPCSections(ArrayRef<PCSection> Sections) {
171 SmallVector<Metadata *, 2> Ops;
172
173 for (const auto &Entry : Sections) {
174 const StringRef &Sec = Entry.first;
175 Ops.push_back(Elt: createString(Str: Sec));
176
177 // If auxiliary data for this section exists, append it.
178 const SmallVector<Constant *> &AuxConsts = Entry.second;
179 if (!AuxConsts.empty()) {
180 SmallVector<Metadata *, 1> AuxMDs;
181 AuxMDs.reserve(N: AuxConsts.size());
182 for (Constant *C : AuxConsts)
183 AuxMDs.push_back(Elt: createConstant(C));
184 Ops.push_back(Elt: MDNode::get(Context, MDs: AuxMDs));
185 }
186 }
187
188 return MDNode::get(Context, MDs: Ops);
189}
190
191MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
192 SmallVector<Metadata *, 3> Args(1, nullptr);
193 if (Extra)
194 Args.push_back(Elt: Extra);
195 if (!Name.empty())
196 Args.push_back(Elt: createString(Str: Name));
197 MDNode *Root = MDNode::getDistinct(Context, MDs: Args);
198
199 // At this point we have
200 // !0 = distinct !{null} <- root
201 // Replace the reserved operand with the root node itself.
202 Root->replaceOperandWith(I: 0, New: Root);
203
204 // We now have
205 // !0 = distinct !{!0} <- root
206 return Root;
207}
208
209MDNode *MDBuilder::createAnonymousAliasScopeDomain(StringRef Description,
210 bool DisjointScopes) {
211 SmallVector<Metadata *, 3> Args = {
212 nullptr, createConstant(C: ConstantInt::getBool(Context, V: DisjointScopes))};
213 if (!Description.empty())
214 Args.push_back(Elt: createString(Str: Description));
215 MDNode *Domain = MDNode::getDistinct(Context, MDs: Args);
216
217 // Replace the reserved operand with the domain node itself, as is done for
218 // any other self-referential root.
219 Domain->replaceOperandWith(I: 0, New: Domain);
220 return Domain;
221}
222
223MDNode *MDBuilder::createTBAARoot(StringRef Name) {
224 return MDNode::get(Context, MDs: createString(Str: Name));
225}
226
227/// Return metadata for a non-root TBAA node with the given name,
228/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
229MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
230 bool isConstant) {
231 if (isConstant) {
232 Constant *Flags = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: 1);
233 return MDNode::get(Context,
234 MDs: {createString(Str: Name), Parent, createConstant(C: Flags)});
235 }
236 return MDNode::get(Context, MDs: {createString(Str: Name), Parent});
237}
238
239MDNode *MDBuilder::createAliasScopeDomain(StringRef Name, bool DisjointScopes) {
240 return MDNode::get(
241 Context, MDs: {createString(Str: Name),
242 createConstant(C: ConstantInt::getBool(Context, V: DisjointScopes))});
243}
244
245MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
246 return MDNode::get(Context, MDs: {createString(Str: Name), Domain});
247}
248
249/// Return metadata for a tbaa.struct node with the given
250/// struct field descriptions.
251MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
252 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
253 Type *Int64 = Type::getInt64Ty(C&: Context);
254 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
255 Vals[i * 3 + 0] = createConstant(C: ConstantInt::get(Ty: Int64, V: Fields[i].Offset));
256 Vals[i * 3 + 1] = createConstant(C: ConstantInt::get(Ty: Int64, V: Fields[i].Size));
257 Vals[i * 3 + 2] = Fields[i].Type;
258 }
259 return MDNode::get(Context, MDs: Vals);
260}
261
262/// Return metadata for a TBAA struct node in the type DAG
263/// with the given name, a list of pairs (offset, field type in the type DAG).
264MDNode *MDBuilder::createTBAAStructTypeNode(
265 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
266 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
267 Type *Int64 = Type::getInt64Ty(C&: Context);
268 Ops[0] = createString(Str: Name);
269 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
270 Ops[i * 2 + 1] = Fields[i].first;
271 Ops[i * 2 + 2] = createConstant(C: ConstantInt::get(Ty: Int64, V: Fields[i].second));
272 }
273 return MDNode::get(Context, MDs: Ops);
274}
275
276/// Return metadata for a TBAA scalar type node with the
277/// given name, an offset and a parent in the TBAA type DAG.
278MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
279 uint64_t Offset) {
280 ConstantInt *Off = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Offset);
281 return MDNode::get(Context,
282 MDs: {createString(Str: Name), Parent, createConstant(C: Off)});
283}
284
285/// Return metadata for a TBAA tag node with the given
286/// base type, access type and offset relative to the base type.
287MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
288 uint64_t Offset, bool IsConstant) {
289 IntegerType *Int64 = Type::getInt64Ty(C&: Context);
290 ConstantInt *Off = ConstantInt::get(Ty: Int64, V: Offset);
291 if (IsConstant) {
292 return MDNode::get(Context, MDs: {BaseType, AccessType, createConstant(C: Off),
293 createConstant(C: ConstantInt::get(Ty: Int64, V: 1))});
294 }
295 return MDNode::get(Context, MDs: {BaseType, AccessType, createConstant(C: Off)});
296}
297
298MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
299 Metadata *Id,
300 ArrayRef<TBAAStructField> Fields) {
301 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
302 Type *Int64 = Type::getInt64Ty(C&: Context);
303 Ops[0] = Parent;
304 Ops[1] = createConstant(C: ConstantInt::get(Ty: Int64, V: Size));
305 Ops[2] = Id;
306 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
307 Ops[I * 3 + 3] = Fields[I].Type;
308 Ops[I * 3 + 4] = createConstant(C: ConstantInt::get(Ty: Int64, V: Fields[I].Offset));
309 Ops[I * 3 + 5] = createConstant(C: ConstantInt::get(Ty: Int64, V: Fields[I].Size));
310 }
311 return MDNode::get(Context, MDs: Ops);
312}
313
314MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
315 uint64_t Offset, uint64_t Size,
316 bool IsImmutable) {
317 IntegerType *Int64 = Type::getInt64Ty(C&: Context);
318 auto *OffsetNode = createConstant(C: ConstantInt::get(Ty: Int64, V: Offset));
319 auto *SizeNode = createConstant(C: ConstantInt::get(Ty: Int64, V: Size));
320 if (IsImmutable) {
321 auto *ImmutabilityFlagNode = createConstant(C: ConstantInt::get(Ty: Int64, V: 1));
322 return MDNode::get(Context, MDs: {BaseType, AccessType, OffsetNode, SizeNode,
323 ImmutabilityFlagNode});
324 }
325 return MDNode::get(Context, MDs: {BaseType, AccessType, OffsetNode, SizeNode});
326}
327
328MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
329 MDNode *BaseType = cast<MDNode>(Val: Tag->getOperand(I: 0));
330 MDNode *AccessType = cast<MDNode>(Val: Tag->getOperand(I: 1));
331 Metadata *OffsetNode = Tag->getOperand(I: 2);
332 uint64_t Offset = mdconst::extract<ConstantInt>(MD&: OffsetNode)->getZExtValue();
333
334 bool NewFormat = isa<MDNode>(Val: AccessType->getOperand(I: 0));
335
336 // See if the tag is already mutable.
337 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
338 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
339 return Tag;
340
341 // If Tag is already mutable then return it.
342 Metadata *ImmutabilityFlagNode = Tag->getOperand(I: ImmutabilityFlagOp);
343 if (!mdconst::extract<ConstantInt>(MD&: ImmutabilityFlagNode)->getValue())
344 return Tag;
345
346 // Otherwise, create another node.
347 if (!NewFormat)
348 return createTBAAStructTagNode(BaseType, AccessType, Offset);
349
350 Metadata *SizeNode = Tag->getOperand(I: 3);
351 uint64_t Size = mdconst::extract<ConstantInt>(MD&: SizeNode)->getZExtValue();
352 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
353}
354
355MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
356 Metadata *Vals[] = {
357 createString(Str: "loop_header_weight"),
358 createConstant(C: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Weight)),
359 };
360 return MDNode::get(Context, MDs: Vals);
361}
362
363MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
364 StringRef FName) {
365 auto *Int64Ty = Type::getInt64Ty(C&: Context);
366 SmallVector<Metadata *, 3> Ops(3);
367 Ops[0] = createConstant(C: ConstantInt::get(Ty: Int64Ty, V: GUID));
368 Ops[1] = createConstant(C: ConstantInt::get(Ty: Int64Ty, V: Hash));
369 Ops[2] = createString(Str: FName);
370 return MDNode::get(Context, MDs: Ops);
371}
372
373MDNode *
374MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {
375 auto *Int64Ty = Type::getInt64Ty(C&: Context);
376 SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);
377 for (size_t I = 0; I < LLVMStats.size(); I++) {
378 Ops[I * 2] = createString(Str: LLVMStats[I].first);
379 Ops[I * 2 + 1] =
380 createConstant(C: ConstantInt::get(Ty: Int64Ty, V: LLVMStats[I].second));
381 }
382 return MDNode::get(Context, MDs: Ops);
383}
384