blob: f65ac31ca5ae7144435aaae5dd78e7694addaaed [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
37MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38 uint32_t FalseWeight) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000039 return createBranchWeights({TrueWeight, FalseWeight});
Benjamin Kramer6b841f12014-04-12 14:26:59 +000040}
41
42MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
Dehao Chen71021cd2016-07-11 17:36:02 +000043 assert(Weights.size() >= 1 && "Need at least one branch weights!");
Benjamin Kramer6b841f12014-04-12 14:26:59 +000044
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000045 SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +000046 Vals[0] = createString("branch_weights");
47
48 Type *Int32Ty = Type::getInt32Ty(Context);
49 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000050 Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
Benjamin Kramer6b841f12014-04-12 14:26:59 +000051
52 return MDNode::get(Context, Vals);
53}
54
Sanjay Patela99ab1f2015-09-02 19:06:43 +000055MDNode *MDBuilder::createUnpredictable() {
56 return MDNode::get(Context, None);
57}
58
Dehao Chena60cdd32017-02-28 18:09:44 +000059MDNode *MDBuilder::createFunctionEntryCount(
Easwaran Ramanbdf20262018-01-09 19:39:35 +000060 uint64_t Count, bool Synthetic,
61 const DenseSet<GlobalValue::GUID> *Imports) {
Diego Novillo2567f3d2015-05-13 15:13:45 +000062 Type *Int64Ty = Type::getInt64Ty(Context);
Dehao Chena60cdd32017-02-28 18:09:44 +000063 SmallVector<Metadata *, 8> Ops;
Easwaran Ramanbdf20262018-01-09 19:39:35 +000064 if (Synthetic)
65 Ops.push_back(createString("synthetic_function_entry_count"));
66 else
67 Ops.push_back(createString("function_entry_count"));
Dehao Chena60cdd32017-02-28 18:09:44 +000068 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
Ana Pazos90b17422017-08-29 17:13:24 +000069 if (Imports) {
70 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
71 std::stable_sort(OrderID.begin(), OrderID.end(),
72 [] (GlobalValue::GUID A, GlobalValue::GUID B) {
73 return A < B;});
74 for (auto ID : OrderID)
Dehao Chena60cdd32017-02-28 18:09:44 +000075 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
Ana Pazos90b17422017-08-29 17:13:24 +000076 }
Dehao Chena60cdd32017-02-28 18:09:44 +000077 return MDNode::get(Context, Ops);
Diego Novillo2567f3d2015-05-13 15:13:45 +000078}
79
Dehao Chen302b69c2016-10-18 20:42:47 +000080MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
81 return MDNode::get(Context,
82 {createString("function_section_prefix"),
83 createString(Prefix)});
84}
85
Benjamin Kramer6b841f12014-04-12 14:26:59 +000086MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
87 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000088
89 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
90 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
91}
92
93MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000094 // If the range is everything then it is useless.
95 if (Hi == Lo)
96 return nullptr;
97
98 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000099 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000100}
101
Matthew Simpson36bbc8c2017-10-16 22:22:11 +0000102MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
103 SmallVector<Metadata *, 4> Ops;
104 for (Function *F : Callees)
105 Ops.push_back(createConstant(F));
106 return MDNode::get(Context, Ops);
107}
108
Johannes Doerfert18251842019-01-19 05:19:06 +0000109MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
110 ArrayRef<int> Arguments,
111 bool VarArgArePassed) {
112 SmallVector<Metadata *, 4> Ops;
113
114 Type *Int64 = Type::getInt64Ty(Context);
115 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
116
117 for (int ArgNo : Arguments)
118 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
119
120 Type *Int1 = Type::getInt1Ty(Context);
121 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
122
123 return MDNode::get(Context, Ops);
124}
125
126MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
127 MDNode *NewCB) {
128 if (!ExistingCallbacks)
129 return MDNode::get(Context, {NewCB});
130
131 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
132 uint64_t NewCBCalleeIdx =
133 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
Johannes Doerfert043a0872019-01-19 09:39:57 +0000134 (void)NewCBCalleeIdx;
Johannes Doerfert18251842019-01-19 05:19:06 +0000135
136 SmallVector<Metadata *, 4> Ops;
137 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
138 Ops.resize(NumExistingOps + 1);
139
140 for (unsigned u = 0; u < NumExistingOps; u++) {
141 Ops[u] = ExistingCallbacks->getOperand(u);
142
143 auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
144 uint64_t OldCBCalleeIdx =
145 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
Johannes Doerfert043a0872019-01-19 09:39:57 +0000146 (void)OldCBCalleeIdx;
Johannes Doerfert18251842019-01-19 05:19:06 +0000147 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
148 "Cannot map a callback callee index twice!");
149 }
150
151 Ops[NumExistingOps] = NewCB;
152 return MDNode::get(Context, Ops);
153}
154
Hal Finkel029cde62014-07-25 15:50:02 +0000155MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000156 // To ensure uniqueness the root node is self-referential.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000157 auto Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +0000158
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000159 SmallVector<Metadata *, 3> Args(1, Dummy.get());
Hal Finkel029cde62014-07-25 15:50:02 +0000160 if (Extra)
161 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +0000162 if (!Name.empty())
163 Args.push_back(createString(Name));
164 MDNode *Root = MDNode::get(Context, Args);
165
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000166 // At this point we have
167 // !0 = metadata !{} <- dummy
168 // !1 = metadata !{metadata !0} <- root
169 // Replace the dummy operand with the root node itself and delete the dummy.
170 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000171
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000172 // We now have
173 // !1 = metadata !{metadata !1} <- self-referential root
174 return Root;
175}
176
177MDNode *MDBuilder::createTBAARoot(StringRef Name) {
178 return MDNode::get(Context, createString(Name));
179}
180
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000181/// Return metadata for a non-root TBAA node with the given name,
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000182/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
183MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
184 bool isConstant) {
185 if (isConstant) {
186 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000187 return MDNode::get(Context,
188 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000189 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000190 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000191}
192
Hal Finkel029cde62014-07-25 15:50:02 +0000193MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000194 return MDNode::get(Context, createString(Name));
195}
196
Hal Finkel029cde62014-07-25 15:50:02 +0000197MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000198 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000199}
200
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000201/// Return metadata for a tbaa.struct node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000202/// struct field descriptions.
203MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000204 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000205 Type *Int64 = Type::getInt64Ty(Context);
206 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000207 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
208 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000209 Vals[i * 3 + 2] = Fields[i].Type;
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000210 }
211 return MDNode::get(Context, Vals);
212}
213
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000214/// Return metadata for a TBAA struct node in the type DAG
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000215/// with the given name, a list of pairs (offset, field type in the type DAG).
216MDNode *MDBuilder::createTBAAStructTypeNode(
217 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000218 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000219 Type *Int64 = Type::getInt64Ty(Context);
220 Ops[0] = createString(Name);
221 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
222 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000223 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000224 }
225 return MDNode::get(Context, Ops);
226}
227
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000228/// Return metadata for a TBAA scalar type node with the
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000229/// given name, an offset and a parent in the TBAA type DAG.
230MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
231 uint64_t Offset) {
232 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000233 return MDNode::get(Context,
234 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000235}
236
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000237/// Return metadata for a TBAA tag node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000238/// base type, access type and offset relative to the base type.
239MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000240 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000241 IntegerType *Int64 = Type::getInt64Ty(Context);
242 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000243 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000244 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
245 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000246 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000247 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000248}
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000249
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000250MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
251 Metadata *Id,
252 ArrayRef<TBAAStructField> Fields) {
253 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
254 Type *Int64 = Type::getInt64Ty(Context);
255 Ops[0] = Parent;
256 Ops[1] = createConstant(ConstantInt::get(Int64, Size));
257 Ops[2] = Id;
258 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
259 Ops[I * 3 + 3] = Fields[I].Type;
260 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
261 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
262 }
263 return MDNode::get(Context, Ops);
264}
265
266MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
267 uint64_t Offset, uint64_t Size,
268 bool IsImmutable) {
269 IntegerType *Int64 = Type::getInt64Ty(Context);
270 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
271 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
272 if (IsImmutable) {
273 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
274 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
275 ImmutabilityFlagNode});
276 }
277 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
278}
279
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000280MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
Ivan A. Kosarev4a381b42018-02-13 14:44:25 +0000281 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000282 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
283 Metadata *OffsetNode = Tag->getOperand(2);
284 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
285
286 bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
287
288 // See if the tag is already mutable.
289 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
290 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
291 return Tag;
292
293 // If Tag is already mutable then return it.
294 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
295 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
296 return Tag;
297
298 // Otherwise, create another node.
299 if (!NewFormat)
300 return createTBAAStructTagNode(BaseType, AccessType, Offset);
301
302 Metadata *SizeNode = Tag->getOperand(3);
303 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
304 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
305}
306
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000307MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
George Burgess IV30831052018-08-15 22:15:35 +0000308 Metadata *Vals[] = {
309 createString("loop_header_weight"),
310 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
311 };
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000312 return MDNode::get(Context, Vals);
313}