blob: c7fcf7a2c347eedbcf650218ec8a63a68320459f [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) {
39 uint32_t Weights[] = {TrueWeight, FalseWeight};
40 return createBranchWeights(Weights);
41}
42
43MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
44 assert(Weights.size() >= 2 && "Need at least two branch weights!");
45
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
56MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
57 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
58 // If the range is everything then it is useless.
59 if (Hi == Lo)
60 return nullptr;
61
62 // Return the range [Lo, Hi).
63 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000064 Metadata *Range[2] = {createConstant(ConstantInt::get(Ty, Lo)),
65 createConstant(ConstantInt::get(Ty, Hi))};
Benjamin Kramer6b841f12014-04-12 14:26:59 +000066 return MDNode::get(Context, Range);
67}
68
Hal Finkel029cde62014-07-25 15:50:02 +000069MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
Benjamin Kramer6b841f12014-04-12 14:26:59 +000070 // To ensure uniqueness the root node is self-referential.
Craig Toppere1d12942014-08-27 05:25:25 +000071 MDNode *Dummy = MDNode::getTemporary(Context, None);
Hal Finkel94146652014-07-24 14:25:39 +000072
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000073 SmallVector<Metadata *, 3> Args(1, Dummy);
Hal Finkel029cde62014-07-25 15:50:02 +000074 if (Extra)
75 Args.push_back(Extra);
Hal Finkel94146652014-07-24 14:25:39 +000076 if (!Name.empty())
77 Args.push_back(createString(Name));
78 MDNode *Root = MDNode::get(Context, Args);
79
Benjamin Kramer6b841f12014-04-12 14:26:59 +000080 // At this point we have
81 // !0 = metadata !{} <- dummy
82 // !1 = metadata !{metadata !0} <- root
83 // Replace the dummy operand with the root node itself and delete the dummy.
84 Root->replaceOperandWith(0, Root);
85 MDNode::deleteTemporary(Dummy);
86 // We now have
87 // !1 = metadata !{metadata !1} <- self-referential root
88 return Root;
89}
90
91MDNode *MDBuilder::createTBAARoot(StringRef Name) {
92 return MDNode::get(Context, createString(Name));
93}
94
95/// \brief Return metadata for a non-root TBAA node with the given name,
96/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
97MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
98 bool isConstant) {
99 if (isConstant) {
100 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000101 Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000102 return MDNode::get(Context, Ops);
103 } else {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000104 Metadata *Ops[2] = {createString(Name), Parent};
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000105 return MDNode::get(Context, Ops);
106 }
107}
108
Hal Finkel029cde62014-07-25 15:50:02 +0000109MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
Hal Finkel94146652014-07-24 14:25:39 +0000110 return MDNode::get(Context, createString(Name));
111}
112
Hal Finkel029cde62014-07-25 15:50:02 +0000113MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000114 Metadata *Ops[2] = {createString(Name), Domain};
Hal Finkel94146652014-07-24 14:25:39 +0000115 return MDNode::get(Context, Ops);
116}
117
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000118/// \brief Return metadata for a tbaa.struct node with the given
119/// struct field descriptions.
120MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000122 Type *Int64 = Type::getInt64Ty(Context);
123 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000124 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
125 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000126 Vals[i * 3 + 2] = Fields[i].TBAA;
127 }
128 return MDNode::get(Context, Vals);
129}
130
131/// \brief Return metadata for a TBAA struct node in the type DAG
132/// with the given name, a list of pairs (offset, field type in the type DAG).
133MDNode *MDBuilder::createTBAAStructTypeNode(
134 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000135 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000136 Type *Int64 = Type::getInt64Ty(Context);
137 Ops[0] = createString(Name);
138 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
139 Ops[i * 2 + 1] = Fields[i].first;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000140 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000141 }
142 return MDNode::get(Context, Ops);
143}
144
145/// \brief Return metadata for a TBAA scalar type node with the
146/// given name, an offset and a parent in the TBAA type DAG.
147MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
148 uint64_t Offset) {
149 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000150 Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000151 return MDNode::get(Context, Ops);
152}
153
154/// \brief Return metadata for a TBAA tag node with the given
155/// base type, access type and offset relative to the base type.
156MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
157 uint64_t Offset) {
158 Type *Int64 = Type::getInt64Ty(Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000159 Metadata *Ops[3] = {BaseType, AccessType,
160 createConstant(ConstantInt::get(Int64, Offset))};
Benjamin Kramer6b841f12014-04-12 14:26:59 +0000161 return MDNode::get(Context, Ops);
162}