1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/MDBuilder.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Metadata.h"
20 MDString
*MDBuilder::createString(StringRef Str
) {
21 return MDString::get(Context
, Str
);
24 ConstantAsMetadata
*MDBuilder::createConstant(Constant
*C
) {
25 return ConstantAsMetadata::get(C
);
28 MDNode
*MDBuilder::createFPMath(float Accuracy
) {
31 assert(Accuracy
> 0.0 && "Invalid fpmath accuracy!");
33 createConstant(ConstantFP::get(Type::getFloatTy(Context
), Accuracy
));
34 return MDNode::get(Context
, Op
);
37 MDNode
*MDBuilder::createBranchWeights(uint32_t TrueWeight
,
38 uint32_t FalseWeight
) {
39 return createBranchWeights({TrueWeight
, FalseWeight
});
42 MDNode
*MDBuilder::createBranchWeights(ArrayRef
<uint32_t> Weights
) {
43 assert(Weights
.size() >= 1 && "Need at least one branch weights!");
45 SmallVector
<Metadata
*, 4> Vals(Weights
.size() + 1);
46 Vals
[0] = createString("branch_weights");
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
]));
52 return MDNode::get(Context
, Vals
);
55 MDNode
*MDBuilder::createUnpredictable() {
56 return MDNode::get(Context
, std::nullopt
);
59 MDNode
*MDBuilder::createFunctionEntryCount(
60 uint64_t Count
, bool Synthetic
,
61 const DenseSet
<GlobalValue::GUID
> *Imports
) {
62 Type
*Int64Ty
= Type::getInt64Ty(Context
);
63 SmallVector
<Metadata
*, 8> Ops
;
65 Ops
.push_back(createString("synthetic_function_entry_count"));
67 Ops
.push_back(createString("function_entry_count"));
68 Ops
.push_back(createConstant(ConstantInt::get(Int64Ty
, Count
)));
70 SmallVector
<GlobalValue::GUID
, 2> OrderID(Imports
->begin(), Imports
->end());
72 for (auto ID
: OrderID
)
73 Ops
.push_back(createConstant(ConstantInt::get(Int64Ty
, ID
)));
75 return MDNode::get(Context
, Ops
);
78 MDNode
*MDBuilder::createFunctionSectionPrefix(StringRef Prefix
) {
79 return MDNode::get(Context
,
80 {createString("function_section_prefix"),
81 createString(Prefix
)});
84 MDNode
*MDBuilder::createRange(const APInt
&Lo
, const APInt
&Hi
) {
85 assert(Lo
.getBitWidth() == Hi
.getBitWidth() && "Mismatched bitwidths!");
87 Type
*Ty
= IntegerType::get(Context
, Lo
.getBitWidth());
88 return createRange(ConstantInt::get(Ty
, Lo
), ConstantInt::get(Ty
, Hi
));
91 MDNode
*MDBuilder::createRange(Constant
*Lo
, Constant
*Hi
) {
92 // If the range is everything then it is useless.
96 // Return the range [Lo, Hi).
97 return MDNode::get(Context
, {createConstant(Lo
), createConstant(Hi
)});
100 MDNode
*MDBuilder::createCallees(ArrayRef
<Function
*> Callees
) {
101 SmallVector
<Metadata
*, 4> Ops
;
102 for (Function
*F
: Callees
)
103 Ops
.push_back(createConstant(F
));
104 return MDNode::get(Context
, Ops
);
107 MDNode
*MDBuilder::createCallbackEncoding(unsigned CalleeArgNo
,
108 ArrayRef
<int> Arguments
,
109 bool VarArgArePassed
) {
110 SmallVector
<Metadata
*, 4> Ops
;
112 Type
*Int64
= Type::getInt64Ty(Context
);
113 Ops
.push_back(createConstant(ConstantInt::get(Int64
, CalleeArgNo
)));
115 for (int ArgNo
: Arguments
)
116 Ops
.push_back(createConstant(ConstantInt::get(Int64
, ArgNo
, true)));
118 Type
*Int1
= Type::getInt1Ty(Context
);
119 Ops
.push_back(createConstant(ConstantInt::get(Int1
, VarArgArePassed
)));
121 return MDNode::get(Context
, Ops
);
124 MDNode
*MDBuilder::mergeCallbackEncodings(MDNode
*ExistingCallbacks
,
126 if (!ExistingCallbacks
)
127 return MDNode::get(Context
, {NewCB
});
129 auto *NewCBCalleeIdxAsCM
= cast
<ConstantAsMetadata
>(NewCB
->getOperand(0));
130 uint64_t NewCBCalleeIdx
=
131 cast
<ConstantInt
>(NewCBCalleeIdxAsCM
->getValue())->getZExtValue();
132 (void)NewCBCalleeIdx
;
134 SmallVector
<Metadata
*, 4> Ops
;
135 unsigned NumExistingOps
= ExistingCallbacks
->getNumOperands();
136 Ops
.resize(NumExistingOps
+ 1);
138 for (unsigned u
= 0; u
< NumExistingOps
; u
++) {
139 Ops
[u
] = ExistingCallbacks
->getOperand(u
);
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!");
149 Ops
[NumExistingOps
] = NewCB
;
150 return MDNode::get(Context
, Ops
);
153 MDNode
*MDBuilder::createRTTIPointerPrologue(Constant
*PrologueSig
,
155 SmallVector
<Metadata
*, 4> Ops
;
156 Ops
.push_back(createConstant(PrologueSig
));
157 Ops
.push_back(createConstant(RTTI
));
158 return MDNode::get(Context
, Ops
);
161 MDNode
*MDBuilder::createPCSections(ArrayRef
<PCSection
> Sections
) {
162 SmallVector
<Metadata
*, 2> Ops
;
164 for (const auto &Entry
: Sections
) {
165 const StringRef
&Sec
= Entry
.first
;
166 Ops
.push_back(createString(Sec
));
168 // If auxiliary data for this section exists, append it.
169 const SmallVector
<Constant
*> &AuxConsts
= Entry
.second
;
170 if (!AuxConsts
.empty()) {
171 SmallVector
<Metadata
*, 1> AuxMDs
;
172 AuxMDs
.reserve(AuxConsts
.size());
173 for (Constant
*C
: AuxConsts
)
174 AuxMDs
.push_back(createConstant(C
));
175 Ops
.push_back(MDNode::get(Context
, AuxMDs
));
179 return MDNode::get(Context
, Ops
);
182 MDNode
*MDBuilder::createAnonymousAARoot(StringRef Name
, MDNode
*Extra
) {
183 SmallVector
<Metadata
*, 3> Args(1, nullptr);
185 Args
.push_back(Extra
);
187 Args
.push_back(createString(Name
));
188 MDNode
*Root
= MDNode::getDistinct(Context
, Args
);
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
);
196 // !0 = distinct !{!0} <- root
200 MDNode
*MDBuilder::createTBAARoot(StringRef Name
) {
201 return MDNode::get(Context
, createString(Name
));
204 /// Return metadata for a non-root TBAA node with the given name,
205 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
206 MDNode
*MDBuilder::createTBAANode(StringRef Name
, MDNode
*Parent
,
209 Constant
*Flags
= ConstantInt::get(Type::getInt64Ty(Context
), 1);
210 return MDNode::get(Context
,
211 {createString(Name
), Parent
, createConstant(Flags
)});
213 return MDNode::get(Context
, {createString(Name
), Parent
});
216 MDNode
*MDBuilder::createAliasScopeDomain(StringRef Name
) {
217 return MDNode::get(Context
, createString(Name
));
220 MDNode
*MDBuilder::createAliasScope(StringRef Name
, MDNode
*Domain
) {
221 return MDNode::get(Context
, {createString(Name
), Domain
});
224 /// Return metadata for a tbaa.struct node with the given
225 /// struct field descriptions.
226 MDNode
*MDBuilder::createTBAAStructNode(ArrayRef
<TBAAStructField
> Fields
) {
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
;
234 return MDNode::get(Context
, Vals
);
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).
239 MDNode
*MDBuilder::createTBAAStructTypeNode(
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
));
248 return MDNode::get(Context
, Ops
);
251 /// Return metadata for a TBAA scalar type node with the
252 /// given name, an offset and a parent in the TBAA type DAG.
253 MDNode
*MDBuilder::createTBAAScalarTypeNode(StringRef Name
, MDNode
*Parent
,
255 ConstantInt
*Off
= ConstantInt::get(Type::getInt64Ty(Context
), Offset
);
256 return MDNode::get(Context
,
257 {createString(Name
), Parent
, createConstant(Off
)});
260 /// Return metadata for a TBAA tag node with the given
261 /// base type, access type and offset relative to the base type.
262 MDNode
*MDBuilder::createTBAAStructTagNode(MDNode
*BaseType
, MDNode
*AccessType
,
263 uint64_t Offset
, bool IsConstant
) {
264 IntegerType
*Int64
= Type::getInt64Ty(Context
);
265 ConstantInt
*Off
= ConstantInt::get(Int64
, Offset
);
267 return MDNode::get(Context
, {BaseType
, AccessType
, createConstant(Off
),
268 createConstant(ConstantInt::get(Int64
, 1))});
270 return MDNode::get(Context
, {BaseType
, AccessType
, createConstant(Off
)});
273 MDNode
*MDBuilder::createTBAATypeNode(MDNode
*Parent
, uint64_t Size
,
275 ArrayRef
<TBAAStructField
> Fields
) {
276 SmallVector
<Metadata
*, 4> Ops(3 + Fields
.size() * 3);
277 Type
*Int64
= Type::getInt64Ty(Context
);
279 Ops
[1] = createConstant(ConstantInt::get(Int64
, Size
));
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
));
286 return MDNode::get(Context
, Ops
);
289 MDNode
*MDBuilder::createTBAAAccessTag(MDNode
*BaseType
, MDNode
*AccessType
,
290 uint64_t Offset
, uint64_t Size
,
292 IntegerType
*Int64
= Type::getInt64Ty(Context
);
293 auto *OffsetNode
= createConstant(ConstantInt::get(Int64
, Offset
));
294 auto *SizeNode
= createConstant(ConstantInt::get(Int64
, Size
));
296 auto *ImmutabilityFlagNode
= createConstant(ConstantInt::get(Int64
, 1));
297 return MDNode::get(Context
, {BaseType
, AccessType
, OffsetNode
, SizeNode
,
298 ImmutabilityFlagNode
});
300 return MDNode::get(Context
, {BaseType
, AccessType
, OffsetNode
, SizeNode
});
303 MDNode
*MDBuilder::createMutableTBAAAccessTag(MDNode
*Tag
) {
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();
309 bool NewFormat
= isa
<MDNode
>(AccessType
->getOperand(0));
311 // See if the tag is already mutable.
312 unsigned ImmutabilityFlagOp
= NewFormat
? 4 : 3;
313 if (Tag
->getNumOperands() <= ImmutabilityFlagOp
)
316 // If Tag is already mutable then return it.
317 Metadata
*ImmutabilityFlagNode
= Tag
->getOperand(ImmutabilityFlagOp
);
318 if (!mdconst::extract
<ConstantInt
>(ImmutabilityFlagNode
)->getValue())
321 // Otherwise, create another node.
323 return createTBAAStructTagNode(BaseType
, AccessType
, Offset
);
325 Metadata
*SizeNode
= Tag
->getOperand(3);
326 uint64_t Size
= mdconst::extract
<ConstantInt
>(SizeNode
)->getZExtValue();
327 return createTBAAAccessTag(BaseType
, AccessType
, Offset
, Size
);
330 MDNode
*MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight
) {
332 createString("loop_header_weight"),
333 createConstant(ConstantInt::get(Type::getInt64Ty(Context
), Weight
)),
335 return MDNode::get(Context
, Vals
);
338 MDNode
*MDBuilder::createPseudoProbeDesc(uint64_t GUID
, uint64_t Hash
,
340 auto *Int64Ty
= Type::getInt64Ty(Context
);
341 SmallVector
<Metadata
*, 3> Ops(3);
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
);
349 MDBuilder::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
);
355 createConstant(ConstantInt::get(Int64Ty
, LLVMStats
[I
].second
));
357 return MDNode::get(Context
, Ops
);