blob: 65cdf3885210a683b6b695cbab5e3e62e182f5e6 [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
24MDNode *MDBuilder::createFPMath(float Accuracy) {
25 if (Accuracy == 0.0)
Craig Topper2617dcc2014-04-15 06:32:26 +000026 return nullptr;
Benjamin Kramer6b841f12014-04-12 14:26:59 +000027 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
28 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
29 return MDNode::get(Context, Op);
30}
31
32MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
33 uint32_t FalseWeight) {
34 uint32_t Weights[] = {TrueWeight, FalseWeight};
35 return createBranchWeights(Weights);
36}
37
38MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
39 assert(Weights.size() >= 2 && "Need at least two branch weights!");
40
41 SmallVector<Value *, 4> Vals(Weights.size() + 1);
42 Vals[0] = createString("branch_weights");
43
44 Type *Int32Ty = Type::getInt32Ty(Context);
45 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
46 Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]);
47
48 return MDNode::get(Context, Vals);
49}
50
51MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
52 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
53 // If the range is everything then it is useless.
54 if (Hi == Lo)
55 return nullptr;
56
57 // Return the range [Lo, Hi).
58 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
59 Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)};
60 return MDNode::get(Context, Range);
61}
62
63MDNode *MDBuilder::createAnonymousTBAARoot() {
64 // To ensure uniqueness the root node is self-referential.
65 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value *>());
66 MDNode *Root = MDNode::get(Context, Dummy);
67 // At this point we have
68 // !0 = metadata !{} <- dummy
69 // !1 = metadata !{metadata !0} <- root
70 // Replace the dummy operand with the root node itself and delete the dummy.
71 Root->replaceOperandWith(0, Root);
72 MDNode::deleteTemporary(Dummy);
73 // We now have
74 // !1 = metadata !{metadata !1} <- self-referential root
75 return Root;
76}
77
78MDNode *MDBuilder::createTBAARoot(StringRef Name) {
79 return MDNode::get(Context, createString(Name));
80}
81
82/// \brief Return metadata for a non-root TBAA node with the given name,
83/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
84MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
85 bool isConstant) {
86 if (isConstant) {
87 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
88 Value *Ops[3] = {createString(Name), Parent, Flags};
89 return MDNode::get(Context, Ops);
90 } else {
91 Value *Ops[2] = {createString(Name), Parent};
92 return MDNode::get(Context, Ops);
93 }
94}
95
96/// \brief Return metadata for a tbaa.struct node with the given
97/// struct field descriptions.
98MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
99 SmallVector<Value *, 4> Vals(Fields.size() * 3);
100 Type *Int64 = Type::getInt64Ty(Context);
101 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
102 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
103 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
104 Vals[i * 3 + 2] = Fields[i].TBAA;
105 }
106 return MDNode::get(Context, Vals);
107}
108
109/// \brief Return metadata for a TBAA struct node in the type DAG
110/// with the given name, a list of pairs (offset, field type in the type DAG).
111MDNode *MDBuilder::createTBAAStructTypeNode(
112 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
113 SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
114 Type *Int64 = Type::getInt64Ty(Context);
115 Ops[0] = createString(Name);
116 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
117 Ops[i * 2 + 1] = Fields[i].first;
118 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
119 }
120 return MDNode::get(Context, Ops);
121}
122
123/// \brief Return metadata for a TBAA scalar type node with the
124/// given name, an offset and a parent in the TBAA type DAG.
125MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
126 uint64_t Offset) {
127 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
128 Value *Ops[3] = {createString(Name), Parent, Off};
129 return MDNode::get(Context, Ops);
130}
131
132/// \brief Return metadata for a TBAA tag node with the given
133/// base type, access type and offset relative to the base type.
134MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
135 uint64_t Offset) {
136 Type *Int64 = Type::getInt64Ty(Context);
137 Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
138 return MDNode::get(Context, Ops);
139}