blob: f4bfd59921516ae199217048b725483c58c6f734 [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
Diego Novillo2567f3d2015-05-13 15:13:45 +000059MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {
Diego Novillo2567f3d2015-05-13 15:13:45 +000060 Type *Int64Ty = Type::getInt64Ty(Context);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000061 return MDNode::get(Context,
62 {createString("function_entry_count"),
63 createConstant(ConstantInt::get(Int64Ty, Count))});
Diego Novillo2567f3d2015-05-13 15:13:45 +000064}
65
Dehao Chen302b69c2016-10-18 20:42:47 +000066MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
67 return MDNode::get(Context,
68 {createString("function_section_prefix"),
69 createString(Prefix)});
70}
71
Benjamin Kramer6b841f12014-04-12 14:26:59 +000072MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
73 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
Charles Davis33d1dc02015-02-25 05:10:25 +000074
75 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
76 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
77}
78
79MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000080 // If the range is everything then it is useless.
81 if (Hi == Lo)
82 return nullptr;
83
84 // Return the range [Lo, Hi).
Benjamin Kramer0969a2a2015-11-22 18:03:17 +000085 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +000086}
87
Hal Finkel029cde62014-07-25 15:50:02 +000088MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000089 // To ensure uniqueness the root node is self-referential.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +000090 auto Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +000091
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +000092 SmallVector<Metadata *, 3> Args(1, Dummy.get());
Hal Finkel029cde62014-07-25 15:50:02 +000093 if (Extra)
94 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +000095 if (!Name.empty())
96 Args.push_back(createString(Name));
97 MDNode *Root = MDNode::get(Context, Args);
98
Benjamin Kramer6b841f12014-04-12 14:26:59 +000099 // At this point we have
100 // !0 = metadata !{} <- dummy
101 // !1 = metadata !{metadata !0} <- root
102 // Replace the dummy operand with the root node itself and delete the dummy.
103 Root->replaceOperandWith(0, Root);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000104
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000105 // We now have
106 // !1 = metadata !{metadata !1} <- self-referential root
107 return Root;
108}
109
110MDNode *MDBuilder::createTBAARoot(StringRef Name) {
111 return MDNode::get(Context, createString(Name));
112}
113
114/// \brief Return metadata for a non-root TBAA node with the given name,
115/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
116MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
117 bool isConstant) {
118 if (isConstant) {
119 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000120 return MDNode::get(Context,
121 {createString(Name), Parent, createConstant(Flags)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000122 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000123 return MDNode::get(Context, {createString(Name), Parent});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000124}
125
Hal Finkel029cde62014-07-25 15:50:02 +0000126MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000127 return MDNode::get(Context, createString(Name));
128}
129
Hal Finkel029cde62014-07-25 15:50:02 +0000130MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000131 return MDNode::get(Context, {createString(Name), Domain});
Hal Finkel94146652014-07-24 14:25:39 +0000132}
133
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000134/// \brief Return metadata for a tbaa.struct node with the given
135/// struct field descriptions.
136MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000137 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000138 Type *Int64 = Type::getInt64Ty(Context);
139 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000140 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
141 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000142 Vals[i * 3 + 2] = Fields[i].TBAA;
143 }
144 return MDNode::get(Context, Vals);
145}
146
147/// \brief Return metadata for a TBAA struct node in the type DAG
148/// with the given name, a list of pairs (offset, field type in the type DAG).
149MDNode *MDBuilder::createTBAAStructTypeNode(
150 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000151 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000152 Type *Int64 = Type::getInt64Ty(Context);
153 Ops[0] = createString(Name);
154 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
155 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000156 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000157 }
158 return MDNode::get(Context, Ops);
159}
160
161/// \brief Return metadata for a TBAA scalar type node with the
162/// given name, an offset and a parent in the TBAA type DAG.
163MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
164 uint64_t Offset) {
165 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000166 return MDNode::get(Context,
167 {createString(Name), Parent, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000168}
169
170/// \brief Return metadata for a TBAA tag node with the given
171/// base type, access type and offset relative to the base type.
172MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000173 uint64_t Offset, bool IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000174 IntegerType *Int64 = Type::getInt64Ty(Context);
175 ConstantInt *Off = ConstantInt::get(Int64, Offset);
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000176 if (IsConstant) {
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000177 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
178 createConstant(ConstantInt::get(Int64, 1))});
Artur Pilipenkoa82f8db2015-06-01 14:53:55 +0000179 }
Benjamin Kramer0969a2a2015-11-22 18:03:17 +0000180 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000181}