blob: 84bad3185914d6f09994a9e9991031169a19433c [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"
17#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(
60 uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
Diego Novillo2567f3d2015-05-13 15:13:45 +000061 Type *Int64Ty = Type::getInt64Ty(Context);
Dehao Chena60cdd32017-02-28 18:09:44 +000062 SmallVector<Metadata *, 8> Ops;
63 Ops.push_back(createString("function_entry_count"));
64 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
Ana Pazos90b17422017-08-29 17:13:24 +000065 if (Imports) {
66 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
67 std::stable_sort(OrderID.begin(), OrderID.end(),
68 [] (GlobalValue::GUID A, GlobalValue::GUID B) {
69 return A < B;});
70 for (auto ID : OrderID)
Dehao Chena60cdd32017-02-28 18:09:44 +000071 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
Ana Pazos90b17422017-08-29 17:13:24 +000072 }
Dehao Chena60cdd32017-02-28 18:09:44 +000073 return MDNode::get(Context, Ops);
Diego Novillo2567f3d2015-05-13 15:13:45 +000074}
75
Dehao Chen302b69c2016-10-18 20:42:47 +000076MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
77 return MDNode::get(Context,
78 {createString("function_section_prefix"),
79 createString(Prefix)});
80}
81
Benjamin Kramer6b841f12014-04-12 14:26:59 +000082MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
83 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000084
85 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
86 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
87}
88
89MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000090 // If the range is everything then it is useless.
91 if (Hi == Lo)
92 return nullptr;
93
94 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000095 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +000096}
97
Hal Finkel029cde62014-07-25 15:50:02 +000098MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000099 // To ensure uniqueness the root node is self-referential.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000100 auto Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +0000101
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000102 SmallVector<Metadata *, 3> Args(1, Dummy.get());
Hal Finkel029cde62014-07-25 15:50:02 +0000103 if (Extra)
104 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +0000105 if (!Name.empty())
106 Args.push_back(createString(Name));
107 MDNode *Root = MDNode::get(Context, Args);
108
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000109 // At this point we have
110 // !0 = metadata !{} <- dummy
111 // !1 = metadata !{metadata !0} <- root
112 // Replace the dummy operand with the root node itself and delete the dummy.
113 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000114
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000115 // We now have
116 // !1 = metadata !{metadata !1} <- self-referential root
117 return Root;
118}
119
120MDNode *MDBuilder::createTBAARoot(StringRef Name) {
121 return MDNode::get(Context, createString(Name));
122}
123
124/// \brief Return metadata for a non-root TBAA node with the given name,
125/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
126MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
127 bool isConstant) {
128 if (isConstant) {
129 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000130 return MDNode::get(Context,
131 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000132 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000133 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000134}
135
Hal Finkel029cde62014-07-25 15:50:02 +0000136MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000137 return MDNode::get(Context, createString(Name));
138}
139
Hal Finkel029cde62014-07-25 15:50:02 +0000140MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000141 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000142}
143
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000144/// \brief Return metadata for a tbaa.struct node with the given
145/// struct field descriptions.
146MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000148 Type *Int64 = Type::getInt64Ty(Context);
149 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000150 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
151 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000152 Vals[i * 3 + 2] = Fields[i].TBAA;
153 }
154 return MDNode::get(Context, Vals);
155}
156
157/// \brief Return metadata for a TBAA struct node in the type DAG
158/// with the given name, a list of pairs (offset, field type in the type DAG).
159MDNode *MDBuilder::createTBAAStructTypeNode(
160 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000161 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000162 Type *Int64 = Type::getInt64Ty(Context);
163 Ops[0] = createString(Name);
164 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
165 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000166 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000167 }
168 return MDNode::get(Context, Ops);
169}
170
171/// \brief Return metadata for a TBAA scalar type node with the
172/// given name, an offset and a parent in the TBAA type DAG.
173MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
174 uint64_t Offset) {
175 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000176 return MDNode::get(Context,
177 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000178}
179
180/// \brief Return metadata for a TBAA tag node with the given
181/// base type, access type and offset relative to the base type.
182MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000183 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000184 IntegerType *Int64 = Type::getInt64Ty(Context);
185 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000186 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000187 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
188 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000189 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000190 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000191}