blob: 3b5f2fec971874793abe9c1a6b131e57b8635a4e [file] [log] [blame]
Benjamin Kramer6b841f12014-04-12 14:26:59 +00001//===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MDBuilder class, which is used as a convenient way to
11// create LLVM metadata with a consistent and simplified interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/IR/MDBuilder.h"
16#include "llvm/IR/Constants.h"
Matthew Simpson36bbc8c2017-10-16 22:22:11 +000017#include "llvm/IR/Function.h"
Benjamin Kramer6b841f12014-04-12 14:26:59 +000018#include "llvm/IR/Metadata.h"
19using namespace llvm;
20
21MDString *MDBuilder::createString(StringRef Str) {
22 return MDString::get(Context, Str);
23}
24
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000025ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
26 return ConstantAsMetadata::get(C);
27}
28
Benjamin Kramer6b841f12014-04-12 14:26:59 +000029MDNode *MDBuilder::createFPMath(float Accuracy) {
30 if (Accuracy == 0.0)
Craig Topper2617dcc2014-04-15 06:32:26 +000031 return nullptr;
Benjamin Kramer6b841f12014-04-12 14:26:59 +000032 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000033 auto *Op =
34 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
Benjamin Kramer6b841f12014-04-12 14:26:59 +000035 return MDNode::get(Context, Op);
36}
37
38MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
39 uint32_t FalseWeight) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000040 return createBranchWeights({TrueWeight, FalseWeight});
Benjamin Kramer6b841f12014-04-12 14:26:59 +000041}
42
43MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
Dehao Chen71021cd2016-07-11 17:36:02 +000044 assert(Weights.size() >= 1 && "Need at least one branch weights!");
Benjamin Kramer6b841f12014-04-12 14:26:59 +000045
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000046 SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +000047 Vals[0] = createString("branch_weights");
48
49 Type *Int32Ty = Type::getInt32Ty(Context);
50 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000051 Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
Benjamin Kramer6b841f12014-04-12 14:26:59 +000052
53 return MDNode::get(Context, Vals);
54}
55
Sanjay Patela99ab1f2015-09-02 19:06:43 +000056MDNode *MDBuilder::createUnpredictable() {
57 return MDNode::get(Context, None);
58}
59
Dehao Chena60cdd32017-02-28 18:09:44 +000060MDNode *MDBuilder::createFunctionEntryCount(
Easwaran Ramanbdf20262018-01-09 19:39:35 +000061 uint64_t Count, bool Synthetic,
62 const DenseSet<GlobalValue::GUID> *Imports) {
Diego Novillo2567f3d2015-05-13 15:13:45 +000063 Type *Int64Ty = Type::getInt64Ty(Context);
Dehao Chena60cdd32017-02-28 18:09:44 +000064 SmallVector<Metadata *, 8> Ops;
Easwaran Ramanbdf20262018-01-09 19:39:35 +000065 if (Synthetic)
66 Ops.push_back(createString("synthetic_function_entry_count"));
67 else
68 Ops.push_back(createString("function_entry_count"));
Dehao Chena60cdd32017-02-28 18:09:44 +000069 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
Ana Pazos90b17422017-08-29 17:13:24 +000070 if (Imports) {
71 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
72 std::stable_sort(OrderID.begin(), OrderID.end(),
73 [] (GlobalValue::GUID A, GlobalValue::GUID B) {
74 return A < B;});
75 for (auto ID : OrderID)
Dehao Chena60cdd32017-02-28 18:09:44 +000076 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
Ana Pazos90b17422017-08-29 17:13:24 +000077 }
Dehao Chena60cdd32017-02-28 18:09:44 +000078 return MDNode::get(Context, Ops);
Diego Novillo2567f3d2015-05-13 15:13:45 +000079}
80
Dehao Chen302b69c2016-10-18 20:42:47 +000081MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
82 return MDNode::get(Context,
83 {createString("function_section_prefix"),
84 createString(Prefix)});
85}
86
Benjamin Kramer6b841f12014-04-12 14:26:59 +000087MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
88 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000089
90 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
91 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
92}
93
94MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000095 // If the range is everything then it is useless.
96 if (Hi == Lo)
97 return nullptr;
98
99 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000100 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000101}
102
Matthew Simpson36bbc8c2017-10-16 22:22:11 +0000103MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
104 SmallVector<Metadata *, 4> Ops;
105 for (Function *F : Callees)
106 Ops.push_back(createConstant(F));
107 return MDNode::get(Context, Ops);
108}
109
Johannes Doerfert18251842019-01-19 05:19:06 +0000110MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
111 ArrayRef<int> Arguments,
112 bool VarArgArePassed) {
113 SmallVector<Metadata *, 4> Ops;
114
115 Type *Int64 = Type::getInt64Ty(Context);
116 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
117
118 for (int ArgNo : Arguments)
119 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
120
121 Type *Int1 = Type::getInt1Ty(Context);
122 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
123
124 return MDNode::get(Context, Ops);
125}
126
127MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
128 MDNode *NewCB) {
129 if (!ExistingCallbacks)
130 return MDNode::get(Context, {NewCB});
131
132 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
133 uint64_t NewCBCalleeIdx =
134 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
135
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();
146 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
147 "Cannot map a callback callee index twice!");
148 }
149
150 Ops[NumExistingOps] = NewCB;
151 return MDNode::get(Context, Ops);
152}
153
Hal Finkel029cde62014-07-25 15:50:02 +0000154MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000155 // To ensure uniqueness the root node is self-referential.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000156 auto Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +0000157
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000158 SmallVector<Metadata *, 3> Args(1, Dummy.get());
Hal Finkel029cde62014-07-25 15:50:02 +0000159 if (Extra)
160 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +0000161 if (!Name.empty())
162 Args.push_back(createString(Name));
163 MDNode *Root = MDNode::get(Context, Args);
164
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000165 // At this point we have
166 // !0 = metadata !{} <- dummy
167 // !1 = metadata !{metadata !0} <- root
168 // Replace the dummy operand with the root node itself and delete the dummy.
169 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000170
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000171 // We now have
172 // !1 = metadata !{metadata !1} <- self-referential root
173 return Root;
174}
175
176MDNode *MDBuilder::createTBAARoot(StringRef Name) {
177 return MDNode::get(Context, createString(Name));
178}
179
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000180/// Return metadata for a non-root TBAA node with the given name,
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000181/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
182MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
183 bool isConstant) {
184 if (isConstant) {
185 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000186 return MDNode::get(Context,
187 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000188 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000189 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000190}
191
Hal Finkel029cde62014-07-25 15:50:02 +0000192MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000193 return MDNode::get(Context, createString(Name));
194}
195
Hal Finkel029cde62014-07-25 15:50:02 +0000196MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000197 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000198}
199
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000200/// Return metadata for a tbaa.struct node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000201/// struct field descriptions.
202MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000203 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000204 Type *Int64 = Type::getInt64Ty(Context);
205 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000206 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
207 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000208 Vals[i * 3 + 2] = Fields[i].Type;
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000209 }
210 return MDNode::get(Context, Vals);
211}
212
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000213/// Return metadata for a TBAA struct node in the type DAG
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000214/// with the given name, a list of pairs (offset, field type in the type DAG).
215MDNode *MDBuilder::createTBAAStructTypeNode(
216 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000217 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000218 Type *Int64 = Type::getInt64Ty(Context);
219 Ops[0] = createString(Name);
220 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
221 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000222 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000223 }
224 return MDNode::get(Context, Ops);
225}
226
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000227/// Return metadata for a TBAA scalar type node with the
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000228/// given name, an offset and a parent in the TBAA type DAG.
229MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
230 uint64_t Offset) {
231 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000232 return MDNode::get(Context,
233 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000234}
235
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000236/// Return metadata for a TBAA tag node with the given
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000237/// base type, access type and offset relative to the base type.
238MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000239 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000240 IntegerType *Int64 = Type::getInt64Ty(Context);
241 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000242 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000243 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
244 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000245 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000246 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000247}
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000248
Ivan A. Kosarev04e1d012017-12-18 16:49:39 +0000249MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
250 Metadata *Id,
251 ArrayRef<TBAAStructField> Fields) {
252 SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
253 Type *Int64 = Type::getInt64Ty(Context);
254 Ops[0] = Parent;
255 Ops[1] = createConstant(ConstantInt::get(Int64, Size));
256 Ops[2] = Id;
257 for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
258 Ops[I * 3 + 3] = Fields[I].Type;
259 Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
260 Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
261 }
262 return MDNode::get(Context, Ops);
263}
264
265MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
266 uint64_t Offset, uint64_t Size,
267 bool IsImmutable) {
268 IntegerType *Int64 = Type::getInt64Ty(Context);
269 auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
270 auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
271 if (IsImmutable) {
272 auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
273 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
274 ImmutabilityFlagNode});
275 }
276 return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
277}
278
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000279MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
Ivan A. Kosarev4a381b42018-02-13 14:44:25 +0000280 MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
Ivan A. Kosarev4d0ff0c2018-01-17 13:29:54 +0000281 MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
282 Metadata *OffsetNode = Tag->getOperand(2);
283 uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
284
285 bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
286
287 // See if the tag is already mutable.
288 unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
289 if (Tag->getNumOperands() <= ImmutabilityFlagOp)
290 return Tag;
291
292 // If Tag is already mutable then return it.
293 Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
294 if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
295 return Tag;
296
297 // Otherwise, create another node.
298 if (!NewFormat)
299 return createTBAAStructTagNode(BaseType, AccessType, Offset);
300
301 Metadata *SizeNode = Tag->getOperand(3);
302 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
303 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
304}
305
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000306MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
George Burgess IV30831052018-08-15 22:15:35 +0000307 Metadata *Vals[] = {
308 createString("loop_header_weight"),
309 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
310 };
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +0000311 return MDNode::get(Context, Vals);
312}