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/Function.h"
18 #include "llvm/IR/Metadata.h"
21 MDString
*MDBuilder::createString(StringRef Str
) {
22 return MDString::get(Context
, Str
);
25 ConstantAsMetadata
*MDBuilder::createConstant(Constant
*C
) {
26 return ConstantAsMetadata::get(C
);
29 MDNode
*MDBuilder::createFPMath(float Accuracy
) {
32 assert(Accuracy
> 0.0 && "Invalid fpmath accuracy!");
34 createConstant(ConstantFP::get(Type::getFloatTy(Context
), Accuracy
));
35 return MDNode::get(Context
, Op
);
38 MDNode
*MDBuilder::createBranchWeights(uint32_t TrueWeight
,
39 uint32_t FalseWeight
) {
40 return createBranchWeights({TrueWeight
, FalseWeight
});
43 MDNode
*MDBuilder::createBranchWeights(ArrayRef
<uint32_t> Weights
) {
44 assert(Weights
.size() >= 1 && "Need at least one branch weights!");
46 SmallVector
<Metadata
*, 4> Vals(Weights
.size() + 1);
47 Vals
[0] = createString("branch_weights");
49 Type
*Int32Ty
= Type::getInt32Ty(Context
);
50 for (unsigned i
= 0, e
= Weights
.size(); i
!= e
; ++i
)
51 Vals
[i
+ 1] = createConstant(ConstantInt::get(Int32Ty
, Weights
[i
]));
53 return MDNode::get(Context
, Vals
);
56 MDNode
*MDBuilder::createUnpredictable() {
57 return MDNode::get(Context
, None
);
60 MDNode
*MDBuilder::createFunctionEntryCount(
61 uint64_t Count
, bool Synthetic
,
62 const DenseSet
<GlobalValue::GUID
> *Imports
) {
63 Type
*Int64Ty
= Type::getInt64Ty(Context
);
64 SmallVector
<Metadata
*, 8> Ops
;
66 Ops
.push_back(createString("synthetic_function_entry_count"));
68 Ops
.push_back(createString("function_entry_count"));
69 Ops
.push_back(createConstant(ConstantInt::get(Int64Ty
, Count
)));
71 SmallVector
<GlobalValue::GUID
, 2> OrderID(Imports
->begin(), Imports
->end());
72 std::stable_sort(OrderID
.begin(), OrderID
.end(),
73 [] (GlobalValue::GUID A
, GlobalValue::GUID B
) {
75 for (auto ID
: OrderID
)
76 Ops
.push_back(createConstant(ConstantInt::get(Int64Ty
, ID
)));
78 return MDNode::get(Context
, Ops
);
81 MDNode
*MDBuilder::createFunctionSectionPrefix(StringRef Prefix
) {
82 return MDNode::get(Context
,
83 {createString("function_section_prefix"),
84 createString(Prefix
)});
87 MDNode
*MDBuilder::createRange(const APInt
&Lo
, const APInt
&Hi
) {
88 assert(Lo
.getBitWidth() == Hi
.getBitWidth() && "Mismatched bitwidths!");
90 Type
*Ty
= IntegerType::get(Context
, Lo
.getBitWidth());
91 return createRange(ConstantInt::get(Ty
, Lo
), ConstantInt::get(Ty
, Hi
));
94 MDNode
*MDBuilder::createRange(Constant
*Lo
, Constant
*Hi
) {
95 // If the range is everything then it is useless.
99 // Return the range [Lo, Hi).
100 return MDNode::get(Context
, {createConstant(Lo
), createConstant(Hi
)});
103 MDNode
*MDBuilder::createCallees(ArrayRef
<Function
*> Callees
) {
104 SmallVector
<Metadata
*, 4> Ops
;
105 for (Function
*F
: Callees
)
106 Ops
.push_back(createConstant(F
));
107 return MDNode::get(Context
, Ops
);
110 MDNode
*MDBuilder::createAnonymousAARoot(StringRef Name
, MDNode
*Extra
) {
111 // To ensure uniqueness the root node is self-referential.
112 auto Dummy
= MDNode::getTemporary(Context
, None
);
114 SmallVector
<Metadata
*, 3> Args(1, Dummy
.get());
116 Args
.push_back(Extra
);
118 Args
.push_back(createString(Name
));
119 MDNode
*Root
= MDNode::get(Context
, Args
);
121 // At this point we have
122 // !0 = metadata !{} <- dummy
123 // !1 = metadata !{metadata !0} <- root
124 // Replace the dummy operand with the root node itself and delete the dummy.
125 Root
->replaceOperandWith(0, Root
);
128 // !1 = metadata !{metadata !1} <- self-referential root
132 MDNode
*MDBuilder::createTBAARoot(StringRef Name
) {
133 return MDNode::get(Context
, createString(Name
));
136 /// Return metadata for a non-root TBAA node with the given name,
137 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
138 MDNode
*MDBuilder::createTBAANode(StringRef Name
, MDNode
*Parent
,
141 Constant
*Flags
= ConstantInt::get(Type::getInt64Ty(Context
), 1);
142 return MDNode::get(Context
,
143 {createString(Name
), Parent
, createConstant(Flags
)});
145 return MDNode::get(Context
, {createString(Name
), Parent
});
148 MDNode
*MDBuilder::createAliasScopeDomain(StringRef Name
) {
149 return MDNode::get(Context
, createString(Name
));
152 MDNode
*MDBuilder::createAliasScope(StringRef Name
, MDNode
*Domain
) {
153 return MDNode::get(Context
, {createString(Name
), Domain
});
156 /// Return metadata for a tbaa.struct node with the given
157 /// struct field descriptions.
158 MDNode
*MDBuilder::createTBAAStructNode(ArrayRef
<TBAAStructField
> Fields
) {
159 SmallVector
<Metadata
*, 4> Vals(Fields
.size() * 3);
160 Type
*Int64
= Type::getInt64Ty(Context
);
161 for (unsigned i
= 0, e
= Fields
.size(); i
!= e
; ++i
) {
162 Vals
[i
* 3 + 0] = createConstant(ConstantInt::get(Int64
, Fields
[i
].Offset
));
163 Vals
[i
* 3 + 1] = createConstant(ConstantInt::get(Int64
, Fields
[i
].Size
));
164 Vals
[i
* 3 + 2] = Fields
[i
].Type
;
166 return MDNode::get(Context
, Vals
);
169 /// Return metadata for a TBAA struct node in the type DAG
170 /// with the given name, a list of pairs (offset, field type in the type DAG).
171 MDNode
*MDBuilder::createTBAAStructTypeNode(
172 StringRef Name
, ArrayRef
<std::pair
<MDNode
*, uint64_t>> Fields
) {
173 SmallVector
<Metadata
*, 4> Ops(Fields
.size() * 2 + 1);
174 Type
*Int64
= Type::getInt64Ty(Context
);
175 Ops
[0] = createString(Name
);
176 for (unsigned i
= 0, e
= Fields
.size(); i
!= e
; ++i
) {
177 Ops
[i
* 2 + 1] = Fields
[i
].first
;
178 Ops
[i
* 2 + 2] = createConstant(ConstantInt::get(Int64
, Fields
[i
].second
));
180 return MDNode::get(Context
, Ops
);
183 /// Return metadata for a TBAA scalar type node with the
184 /// given name, an offset and a parent in the TBAA type DAG.
185 MDNode
*MDBuilder::createTBAAScalarTypeNode(StringRef Name
, MDNode
*Parent
,
187 ConstantInt
*Off
= ConstantInt::get(Type::getInt64Ty(Context
), Offset
);
188 return MDNode::get(Context
,
189 {createString(Name
), Parent
, createConstant(Off
)});
192 /// Return metadata for a TBAA tag node with the given
193 /// base type, access type and offset relative to the base type.
194 MDNode
*MDBuilder::createTBAAStructTagNode(MDNode
*BaseType
, MDNode
*AccessType
,
195 uint64_t Offset
, bool IsConstant
) {
196 IntegerType
*Int64
= Type::getInt64Ty(Context
);
197 ConstantInt
*Off
= ConstantInt::get(Int64
, Offset
);
199 return MDNode::get(Context
, {BaseType
, AccessType
, createConstant(Off
),
200 createConstant(ConstantInt::get(Int64
, 1))});
202 return MDNode::get(Context
, {BaseType
, AccessType
, createConstant(Off
)});
205 MDNode
*MDBuilder::createTBAATypeNode(MDNode
*Parent
, uint64_t Size
,
207 ArrayRef
<TBAAStructField
> Fields
) {
208 SmallVector
<Metadata
*, 4> Ops(3 + Fields
.size() * 3);
209 Type
*Int64
= Type::getInt64Ty(Context
);
211 Ops
[1] = createConstant(ConstantInt::get(Int64
, Size
));
213 for (unsigned I
= 0, E
= Fields
.size(); I
!= E
; ++I
) {
214 Ops
[I
* 3 + 3] = Fields
[I
].Type
;
215 Ops
[I
* 3 + 4] = createConstant(ConstantInt::get(Int64
, Fields
[I
].Offset
));
216 Ops
[I
* 3 + 5] = createConstant(ConstantInt::get(Int64
, Fields
[I
].Size
));
218 return MDNode::get(Context
, Ops
);
221 MDNode
*MDBuilder::createTBAAAccessTag(MDNode
*BaseType
, MDNode
*AccessType
,
222 uint64_t Offset
, uint64_t Size
,
224 IntegerType
*Int64
= Type::getInt64Ty(Context
);
225 auto *OffsetNode
= createConstant(ConstantInt::get(Int64
, Offset
));
226 auto *SizeNode
= createConstant(ConstantInt::get(Int64
, Size
));
228 auto *ImmutabilityFlagNode
= createConstant(ConstantInt::get(Int64
, 1));
229 return MDNode::get(Context
, {BaseType
, AccessType
, OffsetNode
, SizeNode
,
230 ImmutabilityFlagNode
});
232 return MDNode::get(Context
, {BaseType
, AccessType
, OffsetNode
, SizeNode
});
235 MDNode
*MDBuilder::createMutableTBAAAccessTag(MDNode
*Tag
) {
236 MDNode
*BaseType
= cast
<MDNode
>(Tag
->getOperand(0));
237 MDNode
*AccessType
= cast
<MDNode
>(Tag
->getOperand(1));
238 Metadata
*OffsetNode
= Tag
->getOperand(2);
239 uint64_t Offset
= mdconst::extract
<ConstantInt
>(OffsetNode
)->getZExtValue();
241 bool NewFormat
= isa
<MDNode
>(AccessType
->getOperand(0));
243 // See if the tag is already mutable.
244 unsigned ImmutabilityFlagOp
= NewFormat
? 4 : 3;
245 if (Tag
->getNumOperands() <= ImmutabilityFlagOp
)
248 // If Tag is already mutable then return it.
249 Metadata
*ImmutabilityFlagNode
= Tag
->getOperand(ImmutabilityFlagOp
);
250 if (!mdconst::extract
<ConstantInt
>(ImmutabilityFlagNode
)->getValue())
253 // Otherwise, create another node.
255 return createTBAAStructTagNode(BaseType
, AccessType
, Offset
);
257 Metadata
*SizeNode
= Tag
->getOperand(3);
258 uint64_t Size
= mdconst::extract
<ConstantInt
>(SizeNode
)->getZExtValue();
259 return createTBAAAccessTag(BaseType
, AccessType
, Offset
, Size
);
262 MDNode
*MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight
) {
264 createString("loop_header_weight"),
265 createConstant(ConstantInt::get(Type::getInt64Ty(Context
), Weight
)),
267 return MDNode::get(Context
, Vals
);