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) {
64 // To ensure uniqueness the root node is self-referential.
65 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value*>());
67 SmallVector<Value *, 2> Args(1, Dummy);
69 Args.push_back(createString(Name));
70 MDNode *Root = MDNode::get(Context, Args);
72 // At this point we have
73 // !0 = metadata !{} <- dummy
74 // !1 = metadata !{metadata !0} <- root
75 // Replace the dummy operand with the root node itself and delete the dummy.
76 Root->replaceOperandWith(0, Root);
77 MDNode::deleteTemporary(Dummy);
79 // !1 = metadata !{metadata !1} <- self-referential root
83 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
84 return MDNode::get(Context, createString(Name));
87 /// \brief Return metadata for a non-root TBAA node with the given name,
88 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
89 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
92 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
93 Value *Ops[3] = {createString(Name), Parent, Flags};
94 return MDNode::get(Context, Ops);
96 Value *Ops[2] = {createString(Name), Parent};
97 return MDNode::get(Context, Ops);
101 MDNode *MDBuilder::createAliasScopeRoot(StringRef Name) {
102 return MDNode::get(Context, createString(Name));
105 MDNode *MDBuilder::createAliasScopeNode(StringRef Name, MDNode *Parent) {
106 Value *Ops[2] = { createString(Name), Parent };
107 return MDNode::get(Context, Ops);
110 /// \brief Return metadata for a tbaa.struct node with the given
111 /// struct field descriptions.
112 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
113 SmallVector<Value *, 4> Vals(Fields.size() * 3);
114 Type *Int64 = Type::getInt64Ty(Context);
115 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
116 Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
117 Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
118 Vals[i * 3 + 2] = Fields[i].TBAA;
120 return MDNode::get(Context, Vals);
123 /// \brief Return metadata for a TBAA struct node in the type DAG
124 /// with the given name, a list of pairs (offset, field type in the type DAG).
125 MDNode *MDBuilder::createTBAAStructTypeNode(
126 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
127 SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
128 Type *Int64 = Type::getInt64Ty(Context);
129 Ops[0] = createString(Name);
130 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
131 Ops[i * 2 + 1] = Fields[i].first;
132 Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
134 return MDNode::get(Context, Ops);
137 /// \brief Return metadata for a TBAA scalar type node with the
138 /// given name, an offset and a parent in the TBAA type DAG.
139 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
141 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
142 Value *Ops[3] = {createString(Name), Parent, Off};
143 return MDNode::get(Context, Ops);
146 /// \brief Return metadata for a TBAA tag node with the given
147 /// base type, access type and offset relative to the base type.
148 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
150 Type *Int64 = Type::getInt64Ty(Context);
151 Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
152 return MDNode::get(Context, Ops);