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 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
25 return ConstantAsMetadata::get(C);
28 MDNode *MDBuilder::createFPMath(float Accuracy) {
31 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
33 createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
34 return MDNode::get(Context, Op);
37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38 uint32_t FalseWeight) {
39 return createBranchWeights({TrueWeight, FalseWeight});
42 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
43 assert(Weights.size() >= 2 && "Need at least two branch weights!");
45 SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
46 Vals[0] = createString("branch_weights");
48 Type *Int32Ty = Type::getInt32Ty(Context);
49 for (unsigned i = 0, e = Weights.size(); i != e; ++i)
50 Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
52 return MDNode::get(Context, Vals);
55 MDNode *MDBuilder::createUnpredictable() {
56 return MDNode::get(Context, None);
59 MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {
60 Type *Int64Ty = Type::getInt64Ty(Context);
61 return MDNode::get(Context,
62 {createString("function_entry_count"),
63 createConstant(ConstantInt::get(Int64Ty, Count))});
66 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
67 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
69 Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
70 return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
73 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
74 // If the range is everything then it is useless.
78 // Return the range [Lo, Hi).
79 return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
82 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
83 // To ensure uniqueness the root node is self-referential.
84 auto Dummy = MDNode::getTemporary(Context, None);
86 SmallVector<Metadata *, 3> Args(1, Dummy.get());
88 Args.push_back(Extra);
90 Args.push_back(createString(Name));
91 MDNode *Root = MDNode::get(Context, Args);
93 // At this point we have
94 // !0 = metadata !{} <- dummy
95 // !1 = metadata !{metadata !0} <- root
96 // Replace the dummy operand with the root node itself and delete the dummy.
97 Root->replaceOperandWith(0, Root);
100 // !1 = metadata !{metadata !1} <- self-referential root
104 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
105 return MDNode::get(Context, createString(Name));
108 /// \brief Return metadata for a non-root TBAA node with the given name,
109 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
110 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
113 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
114 return MDNode::get(Context,
115 {createString(Name), Parent, createConstant(Flags)});
117 return MDNode::get(Context, {createString(Name), Parent});
120 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
121 return MDNode::get(Context, createString(Name));
124 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
125 return MDNode::get(Context, {createString(Name), Domain});
128 /// \brief Return metadata for a tbaa.struct node with the given
129 /// struct field descriptions.
130 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
131 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
132 Type *Int64 = Type::getInt64Ty(Context);
133 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
134 Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
135 Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
136 Vals[i * 3 + 2] = Fields[i].TBAA;
138 return MDNode::get(Context, Vals);
141 /// \brief Return metadata for a TBAA struct node in the type DAG
142 /// with the given name, a list of pairs (offset, field type in the type DAG).
143 MDNode *MDBuilder::createTBAAStructTypeNode(
144 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
145 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
146 Type *Int64 = Type::getInt64Ty(Context);
147 Ops[0] = createString(Name);
148 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
149 Ops[i * 2 + 1] = Fields[i].first;
150 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
152 return MDNode::get(Context, Ops);
155 /// \brief Return metadata for a TBAA scalar type node with the
156 /// given name, an offset and a parent in the TBAA type DAG.
157 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
159 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
160 return MDNode::get(Context,
161 {createString(Name), Parent, createConstant(Off)});
164 /// \brief Return metadata for a TBAA tag node with the given
165 /// base type, access type and offset relative to the base type.
166 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
167 uint64_t Offset, bool IsConstant) {
168 IntegerType *Int64 = Type::getInt64Ty(Context);
169 ConstantInt *Off = ConstantInt::get(Int64, Offset);
171 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
172 createConstant(ConstantInt::get(Int64, 1))});
174 return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});