blob: 54783e884e990ac9c64912c7850d9b88f640b145 [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(
61 uint64_t Count, 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;
64 Ops.push_back(createString("function_entry_count"));
65 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
Ana Pazos90b17422017-08-29 17:13:24 +000066 if (Imports) {
67 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
68 std::stable_sort(OrderID.begin(), OrderID.end(),
69 [] (GlobalValue::GUID A, GlobalValue::GUID B) {
70 return A < B;});
71 for (auto ID : OrderID)
Dehao Chena60cdd32017-02-28 18:09:44 +000072 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
Ana Pazos90b17422017-08-29 17:13:24 +000073 }
Dehao Chena60cdd32017-02-28 18:09:44 +000074 return MDNode::get(Context, Ops);
Diego Novillo2567f3d2015-05-13 15:13:45 +000075}
76
Dehao Chen302b69c2016-10-18 20:42:47 +000077MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
78 return MDNode::get(Context,
79 {createString("function_section_prefix"),
80 createString(Prefix)});
81}
82
Benjamin Kramer6b841f12014-04-12 14:26:59 +000083MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
84 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000085
86 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
87 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
88}
89
90MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000091 // If the range is everything then it is useless.
92 if (Hi == Lo)
93 return nullptr;
94
95 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000096 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +000097}
98
Matthew Simpson36bbc8c2017-10-16 22:22:11 +000099MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
100 SmallVector<Metadata *, 4> Ops;
101 for (Function *F : Callees)
102 Ops.push_back(createConstant(F));
103 return MDNode::get(Context, Ops);
104}
105
Hal Finkel029cde62014-07-25 15:50:02 +0000106MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000107 // To ensure uniqueness the root node is self-referential.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000108 auto Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +0000109
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000110 SmallVector<Metadata *, 3> Args(1, Dummy.get());
Hal Finkel029cde62014-07-25 15:50:02 +0000111 if (Extra)
112 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +0000113 if (!Name.empty())
114 Args.push_back(createString(Name));
115 MDNode *Root = MDNode::get(Context, Args);
116
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000117 // At this point we have
118 // !0 = metadata !{} <- dummy
119 // !1 = metadata !{metadata !0} <- root
120 // Replace the dummy operand with the root node itself and delete the dummy.
121 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000122
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000123 // We now have
124 // !1 = metadata !{metadata !1} <- self-referential root
125 return Root;
126}
127
128MDNode *MDBuilder::createTBAARoot(StringRef Name) {
129 return MDNode::get(Context, createString(Name));
130}
131
132/// \brief Return metadata for a non-root TBAA node with the given name,
133/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
134MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
135 bool isConstant) {
136 if (isConstant) {
137 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000138 return MDNode::get(Context,
139 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000140 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000141 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000142}
143
Hal Finkel029cde62014-07-25 15:50:02 +0000144MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000145 return MDNode::get(Context, createString(Name));
146}
147
Hal Finkel029cde62014-07-25 15:50:02 +0000148MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000149 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000150}
151
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000152/// \brief Return metadata for a tbaa.struct node with the given
153/// struct field descriptions.
154MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000155 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000156 Type *Int64 = Type::getInt64Ty(Context);
157 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000158 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
159 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000160 Vals[i * 3 + 2] = Fields[i].TBAA;
161 }
162 return MDNode::get(Context, Vals);
163}
164
165/// \brief Return metadata for a TBAA struct node in the type DAG
166/// with the given name, a list of pairs (offset, field type in the type DAG).
167MDNode *MDBuilder::createTBAAStructTypeNode(
168 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000169 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000170 Type *Int64 = Type::getInt64Ty(Context);
171 Ops[0] = createString(Name);
172 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
173 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000174 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000175 }
176 return MDNode::get(Context, Ops);
177}
178
179/// \brief Return metadata for a TBAA scalar type node with the
180/// given name, an offset and a parent in the TBAA type DAG.
181MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
182 uint64_t Offset) {
183 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000184 return MDNode::get(Context,
185 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000186}
187
188/// \brief Return metadata for a TBAA tag node with the given
189/// base type, access type and offset relative to the base type.
190MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000191 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000192 IntegerType *Int64 = Type::getInt64Ty(Context);
193 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000194 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000195 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
196 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000197 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000198 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000199}