1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/MDBuilder.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Metadata.h"
20 MDString *MDBuilder::createString(StringRef Str) {
21 return MDString::get(Context, Str);
24 MDNode *MDBuilder::createFPMath(float Accuracy) {
27 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
28 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
29 return MDNode::get(Context, Op);
32 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
33 uint32_t FalseWeight) {
34 uint32_t Weights[] = {TrueWeight, FalseWeight};
35 return createBranchWeights(Weights);
38 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
39 assert(Weights.size() >= 2 && "Need at least two branch weights!");
41 SmallVector<Value *, 4> Vals(Weights.size() + 1);
42 Vals[0] = createString("branch_weights");
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]);
48 return MDNode::get(Context, Vals);
51 MDNode *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.
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);
63 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
64 // To ensure uniqueness the root node is self-referential.
65 MDNode *Dummy = MDNode::getTemporary(Context, None);
67 SmallVector<Value *, 3> Args(1, Dummy);
69 Args.push_back(Extra);
71 Args.push_back(createString(Name));
72 MDNode *Root = MDNode::get(Context, Args);
74 // At this point we have
75 // !0 = metadata !{} <- dummy
76 // !1 = metadata !{metadata !0} <- root
77 // Replace the dummy operand with the root node itself and delete the dummy.
78 Root->replaceOperandWith(0, Root);
79 MDNode::deleteTemporary(Dummy);
81 // !1 = metadata !{metadata !1} <- self-referential root
85 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
86 return MDNode::get(Context, createString(Name));
89 /// \brief Return metadata for a non-root TBAA node with the given name,
90 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
91 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
94 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
95 Value *Ops[3] = {createString(Name), Parent, Flags};
96 return MDNode::get(Context, Ops);
98 Value *Ops[2] = {createString(Name), Parent};
99 return MDNode::get(Context, Ops);
103 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
104 return MDNode::get(Context, createString(Name));
107 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
108 Value *Ops[2] = { createString(Name), Domain };
109 return MDNode::get(Context, Ops);
112 /// \brief Return metadata for a tbaa.struct node with the given
113 /// struct field descriptions.
114 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
115 SmallVector<Value *, 4> Vals(Fields.size() * 3);
116 Type *Int64 = Type::getInt64Ty(Context);
117 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
118 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
119 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
120 Vals[i * 3 + 2] = Fields[i].TBAA;
122 return MDNode::get(Context, Vals);
125 /// \brief Return metadata for a TBAA struct node in the type DAG
126 /// with the given name, a list of pairs (offset, field type in the type DAG).
127 MDNode *MDBuilder::createTBAAStructTypeNode(
128 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
129 SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
130 Type *Int64 = Type::getInt64Ty(Context);
131 Ops[0] = createString(Name);
132 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
133 Ops[i * 2 + 1] = Fields[i].first;
134 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
136 return MDNode::get(Context, Ops);
139 /// \brief Return metadata for a TBAA scalar type node with the
140 /// given name, an offset and a parent in the TBAA type DAG.
141 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
143 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
144 Value *Ops[3] = {createString(Name), Parent, Off};
145 return MDNode::get(Context, Ops);
148 /// \brief Return metadata for a TBAA tag node with the given
149 /// base type, access type and offset relative to the base type.
150 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
152 Type *Int64 = Type::getInt64Ty(Context);
153 Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
154 return MDNode::get(Context, Ops);