blob: b6aa8844a7eafa7b83351cef2f0a4e5a0f1a4ffa [file] [log] [blame]
Benjamin Kramer6b841f12014-04-12 14:26:59 +00001//===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Benjamin Kramer6b841f12014-04-12 14:26:59 +00006//
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"
Matthew Simpson36bbc8c2017-10-16 22:22:11 +000016#include "llvm/IR/Function.h"
Benjamin Kramer6b841f12014-04-12 14:26:59 +000017#include "llvm/IR/Metadata.h"
18using namespace llvm;
19
20MDString *MDBuilder::createString(StringRef Str) {
21 return MDString::get(Context, Str);
22}
23
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000024ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
25 return ConstantAsMetadata::get(C);
26}
27
Benjamin Kramer6b841f12014-04-12 14:26:59 +000028MDNode *MDBuilder::createFPMath(float Accuracy) {
29 if (Accuracy == 0.0)
Craig Topper2617dcc2014-04-15 06:32:26 +000030 return nullptr;
Benjamin Kramer6b841f12014-04-12 14:26:59 +000031 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000032 auto *Op =
33 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
Benjamin Kramer6b841f12014-04-12 14:26:59 +000034 return MDNode::get(Context, Op);
35}
36
Arthur Eubanks5c31b8b2020-10-31 00:15:46 -070037MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
Paul Kirth294f3ce2024-06-12 12:52:28 -070038 uint32_t FalseWeight, bool IsExpected) {
39 return createBranchWeights({TrueWeight, FalseWeight}, IsExpected);
Benjamin Kramer6b841f12014-04-12 14:26:59 +000040}
41
Vitaly Bukac60aa432024-04-19 17:03:23 -070042MDNode *MDBuilder::createLikelyBranchWeights() {
43 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
44 return createBranchWeights((1U << 20) - 1, 1);
45}
46
47MDNode *MDBuilder::createUnlikelyBranchWeights() {
48 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
49 return createBranchWeights(1, (1U << 20) - 1);
50}
51
Paul Kirth294f3ce2024-06-12 12:52:28 -070052MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights,
53 bool IsExpected) {
Dehao Chen71021cd2016-07-11 17:36:02 +000054 assert(Weights.size() >= 1 && "Need at least one branch weights!");
Benjamin Kramer6b841f12014-04-12 14:26:59 +000055
Paul Kirth294f3ce2024-06-12 12:52:28 -070056 unsigned int Offset = IsExpected ? 2 : 1;
57 SmallVector<Metadata *, 4> Vals(Weights.size() + Offset);
Benjamin Kramer6b841f12014-04-12 14:26:59 +000058 Vals[0] = createString("branch_weights");
Paul Kirth294f3ce2024-06-12 12:52:28 -070059 if (IsExpected)
60 Vals[1] = createString("expected");
Benjamin Kramer6b841f12014-04-12 14:26:59 +000061
Arthur Eubanks5c31b8b2020-10-31 00:15:46 -070062 Type *Int32Ty = Type::getInt32Ty(Context);
Benjamin Kramer6b841f12014-04-12 14:26:59 +000063 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
Paul Kirth294f3ce2024-06-12 12:52:28 -070064 Vals[i + Offset] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
Benjamin Kramer6b841f12014-04-12 14:26:59 +000065
66 return MDNode::get(Context, Vals);
67}
68
Jay Foade03f4272024-09-19 16:16:38 +010069MDNode *MDBuilder::createUnpredictable() { return MDNode::get(Context, {}); }
Sanjay Patela99ab1f2015-09-02 19:06:43 +000070
Dehao Chena60cdd32017-02-28 18:09:44 +000071MDNode *MDBuilder::createFunctionEntryCount(
Easwaran Ramanbdf20262018-01-09 19:39:35 +000072 uint64_t Count, bool Synthetic,
73 const DenseSet<GlobalValue::GUID> *Imports) {
Diego Novillo2567f3d2015-05-13 15:13:45 +000074 Type *Int64Ty = Type::getInt64Ty(Context);
Dehao Chena60cdd32017-02-28 18:09:44 +000075 SmallVector<Metadata *, 8> Ops;
Easwaran Ramanbdf20262018-01-09 19:39:35 +000076 if (Synthetic)
77 Ops.push_back(createString("synthetic_function_entry_count"));
78 else
79 Ops.push_back(createString("function_entry_count"));
Dehao Chena60cdd32017-02-28 18:09:44 +000080 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
Ana Pazos90b17422017-08-29 17:13:24 +000081 if (Imports) {
82 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
Benjamin Kramerba2e72c2020-03-28 21:16:51 +010083 llvm::sort(OrderID);
Ana Pazos90b17422017-08-29 17:13:24 +000084 for (auto ID : OrderID)
Dehao Chena60cdd32017-02-28 18:09:44 +000085 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
Ana Pazos90b17422017-08-29 17:13:24 +000086 }
Dehao Chena60cdd32017-02-28 18:09:44 +000087 return MDNode::get(Context, Ops);
Diego Novillo2567f3d2015-05-13 15:13:45 +000088}
89
Mingming Liu53997822025-02-06 14:51:13 -080090MDNode *MDBuilder::createGlobalObjectSectionPrefix(StringRef Prefix) {
91 return MDNode::get(Context,
92 {createString("section_prefix"), createString(Prefix)});
Dehao Chen302b69c2016-10-18 20:42:47 +000093}
94
Benjamin Kramer6b841f12014-04-12 14:26:59 +000095MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
96 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000097
98 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
99 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
100}
101
102MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000103 // If the range is everything then it is useless.
104 if (Hi == Lo)
105 return nullptr;
106
107 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000108 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000109}
110
Matthew Simpson36bbc8c2017-10-16 22:22:11 +0000111MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
112 SmallVector<Metadata *, 4> Ops;
113 for (Function *F : Callees)
114 Ops.push_back(createConstant(F));
115 return MDNode::get(Context, Ops);
116}
117
Johannes Doerfert18251842019-01-19 05:19:06 +0000118MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
119 ArrayRef<int> Arguments,
120 bool VarArgArePassed) {
121 SmallVector<Metadata *, 4> Ops;
122
123 Type *Int64 = Type::getInt64Ty(Context);
124 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
125
126 for (int ArgNo : Arguments)
127 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
128
129 Type *Int1 = Type::getInt1Ty(Context);
130 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
131
132 return MDNode::get(Context, Ops);
133}
134
135MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
136 MDNode *NewCB) {
137 if (!ExistingCallbacks)
138 return MDNode::get(Context, {NewCB});
139
140 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
141 uint64_t NewCBCalleeIdx =
142 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
Johannes Doerfert043a0872019-01-19 09:39:57 +0000143 (void)NewCBCalleeIdx;
Johannes Doerfert18251842019-01-19 05:19:06 +0000144
145 SmallVector<Metadata *, 4> Ops;
146 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
147 Ops.resize(NumExistingOps + 1);
148
149 for (unsigned u = 0; u < NumExistingOps; u++) {
150 Ops[u] = ExistingCallbacks->getOperand(u);
151
Joachim Meyer0170bd52024-05-21 17:31:50 -0700152 auto *OldCBCalleeIdxAsCM =
153 cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0));
Johannes Doerfert18251842019-01-19 05:19:06 +0000154 uint64_t OldCBCalleeIdx =
Joachim Meyer0170bd52024-05-21 17:31:50 -0700155 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
Johannes Doerfert043a0872019-01-19 09:39:57 +0000156 (void)OldCBCalleeIdx;
Johannes Doerfert18251842019-01-19 05:19:06 +0000157 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
158 "Cannot map a callback callee index twice!");
159 }
160
161 Ops[NumExistingOps] = NewCB;
162 return MDNode::get(Context, Ops);
163}
164
Yuanfang Chen6678f8e2022-06-27 11:33:45 -0700165MDNode *MDBuilder::createRTTIPointerPrologue(Constant *PrologueSig,
166 Constant *RTTI) {
167 SmallVector<Metadata *, 4> Ops;
168 Ops.push_back(createConstant(PrologueSig));
169 Ops.push_back(createConstant(RTTI));
170 return MDNode::get(Context, Ops);
171}
172
Marco Elverc70f6e12022-09-06 15:48:23 +0200173MDNode *MDBuilder::createPCSections(ArrayRef<PCSection> Sections) {
174 SmallVector<Metadata *, 2> Ops;
175
176 for (const auto &Entry : Sections) {
177 const StringRef &Sec = Entry.first;
178 Ops.push_back(createString(Sec));
179
180 // If auxiliary data for this section exists, append it.
181 const SmallVector<Constant *> &AuxConsts = Entry.second;
182 if (!AuxConsts.empty()) {
183 SmallVector<Metadata *, 1> AuxMDs;
184 AuxMDs.reserve(AuxConsts.size());
185 for (Constant *C : AuxConsts)
186 AuxMDs.push_back(createConstant(C));
187 Ops.push_back(MDNode::get(Context, AuxMDs));
188 }
189 }
190
191 return MDNode::get(Context, Ops);
192}
193
Hal Finkel029cde62014-07-25 15:50:02 +0000194MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Duncan P. N. Exon Smithd4c667c9a2020-10-23 17:55:41 -0400195 SmallVector<Metadata *, 3> Args(1, nullptr);
Hal Finkel029cde62014-07-25 15:50:02 +0000196 if (Extra)
197 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +0000198 if (!Name.empty())
199 Args.push_back(createString(Name));
Duncan P. N. Exon Smithd4c667c9a2020-10-23 17:55:41 -0400200 MDNode *Root = MDNode::getDistinct(Context, Args);
Hal Finkel94146652014-07-24 14:25:39 +0000201
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000202 // At this point we have
Duncan P. N. Exon Smithd4c667c9a2020-10-23 17:55:41 -0400203 // !0 = distinct !{null} <- root
204 // Replace the reserved operand with the root node itself.
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000205 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000206
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000207 // We now have
Duncan P. N. Exon Smithd4c667c9a2020-10-23 17:55:41 -0400208 // !0 = distinct !{!0} <- root
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000209 return Root;
210}
211
212MDNode *MDBuilder::createTBAARoot(StringRef Name) {
213 return MDNode::get(Context, createString(Name));
214}
215
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000216/// Return metadata for a non-root TBAA node with the given name,
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000217/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
218MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
219 bool isConstant) {
220 if (isConstant) {
221 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000222 return MDNode::get(Context,
223 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000224 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000225 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000226}
227
Hal Finkel029cde62014-07-25 15:50:02 +0000228MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000229 return MDNode::get(Context, createString(Name));
230}
231
Hal Finkel029cde62014-07-25 15:50:02 +0000232MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000233 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000234}
235
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000236/// Return metadata for a tbaa.struct node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000237/// struct field descriptions.
238MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000239 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000240 Type *Int64 = Type::getInt64Ty(Context);
241 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000242 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
243 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000244 Vals[i * 3 + 2] = Fields[i].Type;
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000245 }
246 return MDNode::get(Context, Vals);
247}
248
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000249/// Return metadata for a TBAA struct node in the type DAG
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000250/// with the given name, a list of pairs (offset, field type in the type DAG).
251MDNode *MDBuilder::createTBAAStructTypeNode(
252 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000253 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000254 Type *Int64 = Type::getInt64Ty(Context);
255 Ops[0] = createString(Name);
256 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
257 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000258 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000259 }
260 return MDNode::get(Context, Ops);
261}
262
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000263/// Return metadata for a TBAA scalar type node with the
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000264/// given name, an offset and a parent in the TBAA type DAG.
265MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
266 uint64_t Offset) {
267 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000268 return MDNode::get(Context,
269 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000270}
271
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000272/// Return metadata for a TBAA tag node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000273/// base type, access type and offset relative to the base type.
274MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000275 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000276 IntegerType *Int64 = Type::getInt64Ty(Context);
277 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000278 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000279 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
280 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000281 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000282 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000283}
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000284
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000285MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
286 Metadata *Id,
287 ArrayRef<TBAAStructField> Fields) {
288 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
289 Type *Int64 = Type::getInt64Ty(Context);
290 Ops[0] = Parent;
291 Ops[1] = createConstant(ConstantInt::get(Int64, Size));
292 Ops[2] = Id;
293 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
294 Ops[I * 3 + 3] = Fields[I].Type;
295 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
296 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
297 }
298 return MDNode::get(Context, Ops);
299}
300
301MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
302 uint64_t Offset, uint64_t Size,
303 bool IsImmutable) {
304 IntegerType *Int64 = Type::getInt64Ty(Context);
305 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
306 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
307 if (IsImmutable) {
308 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
309 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
310 ImmutabilityFlagNode});
311 }
312 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
313}
314
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000315MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
Ivan A. Kosarev4a381b42018-02-13 14:44:25 +0000316 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000317 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
318 Metadata *OffsetNode = Tag->getOperand(2);
319 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
320
321 bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
322
323 // See if the tag is already mutable.
324 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
325 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
326 return Tag;
327
328 // If Tag is already mutable then return it.
329 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
330 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
331 return Tag;
332
333 // Otherwise, create another node.
334 if (!NewFormat)
335 return createTBAAStructTagNode(BaseType, AccessType, Offset);
336
337 Metadata *SizeNode = Tag->getOperand(3);
338 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
339 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
340}
341
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000342MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
George Burgess IV30831052018-08-15 22:15:35 +0000343 Metadata *Vals[] = {
Joachim Meyer0170bd52024-05-21 17:31:50 -0700344 createString("loop_header_weight"),
345 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
George Burgess IV30831052018-08-15 22:15:35 +0000346 };
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000347 return MDNode::get(Context, Vals);
348}
Hongtao Yu705a4c12020-12-08 15:37:32 -0800349
350MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
wleifd29a4d2023-03-22 13:13:27 -0700351 StringRef FName) {
Hongtao Yu705a4c12020-12-08 15:37:32 -0800352 auto *Int64Ty = Type::getInt64Ty(Context);
353 SmallVector<Metadata *, 3> Ops(3);
354 Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
355 Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
wleifd29a4d2023-03-22 13:13:27 -0700356 Ops[2] = createString(FName);
Hongtao Yu705a4c12020-12-08 15:37:32 -0800357 return MDNode::get(Context, Ops);
358}
wlei47b07582022-10-25 00:31:46 -0700359
360MDNode *
361MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {
362 auto *Int64Ty = Type::getInt64Ty(Context);
363 SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);
364 for (size_t I = 0; I < LLVMStats.size(); I++) {
365 Ops[I * 2] = createString(LLVMStats[I].first);
366 Ops[I * 2 + 1] =
367 createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second));
368 }
369 return MDNode::get(Context, Ops);
370}