Line data Source code
1 : //===---- 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/Function.h"
18 : #include "llvm/IR/Metadata.h"
19 : using namespace llvm;
20 :
21 66512 : MDString *MDBuilder::createString(StringRef Str) {
22 66512 : return MDString::get(Context, Str);
23 : }
24 :
25 72009 : ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
26 72009 : return ConstantAsMetadata::get(C);
27 : }
28 :
29 17 : MDNode *MDBuilder::createFPMath(float Accuracy) {
30 17 : if (Accuracy == 0.0)
31 : return nullptr;
32 : assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
33 : auto *Op =
34 16 : createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
35 16 : return MDNode::get(Context, Op);
36 : }
37 :
38 6339 : MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
39 : uint32_t FalseWeight) {
40 12678 : return createBranchWeights({TrueWeight, FalseWeight});
41 : }
42 :
43 6738 : MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
44 : assert(Weights.size() >= 1 && "Need at least one branch weights!");
45 :
46 6738 : SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
47 6738 : Vals[0] = createString("branch_weights");
48 :
49 6738 : Type *Int32Ty = Type::getInt32Ty(Context);
50 20338 : for (unsigned i = 0, e = Weights.size(); i != e; ++i)
51 27200 : Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
52 :
53 6738 : return MDNode::get(Context, Vals);
54 : }
55 :
56 2 : MDNode *MDBuilder::createUnpredictable() {
57 2 : return MDNode::get(Context, None);
58 : }
59 :
60 506 : MDNode *MDBuilder::createFunctionEntryCount(
61 : uint64_t Count, bool Synthetic,
62 : const DenseSet<GlobalValue::GUID> *Imports) {
63 506 : Type *Int64Ty = Type::getInt64Ty(Context);
64 : SmallVector<Metadata *, 8> Ops;
65 506 : if (Synthetic)
66 18 : Ops.push_back(createString("synthetic_function_entry_count"));
67 : else
68 488 : Ops.push_back(createString("function_entry_count"));
69 506 : Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
70 506 : if (Imports) {
71 88 : SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
72 : std::stable_sort(OrderID.begin(), OrderID.end(),
73 : [] (GlobalValue::GUID A, GlobalValue::GUID B) {
74 0 : return A < B;});
75 102 : for (auto ID : OrderID)
76 14 : Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
77 : }
78 506 : return MDNode::get(Context, Ops);
79 : }
80 :
81 19 : MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
82 19 : return MDNode::get(Context,
83 19 : {createString("function_section_prefix"),
84 19 : createString(Prefix)});
85 : }
86 :
87 5443 : MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
88 : assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
89 :
90 5443 : Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
91 5443 : return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
92 : }
93 :
94 5447 : MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
95 : // If the range is everything then it is useless.
96 5447 : if (Hi == Lo)
97 : return nullptr;
98 :
99 : // Return the range [Lo, Hi).
100 5443 : return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
101 : }
102 :
103 3 : MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
104 : SmallVector<Metadata *, 4> Ops;
105 9 : for (Function *F : Callees)
106 6 : Ops.push_back(createConstant(F));
107 3 : return MDNode::get(Context, Ops);
108 : }
109 :
110 32168 : MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
111 : // To ensure uniqueness the root node is self-referential.
112 32168 : auto Dummy = MDNode::getTemporary(Context, None);
113 :
114 32168 : SmallVector<Metadata *, 3> Args(1, Dummy.get());
115 32168 : if (Extra)
116 17218 : Args.push_back(Extra);
117 32168 : if (!Name.empty())
118 31638 : Args.push_back(createString(Name));
119 32168 : MDNode *Root = MDNode::get(Context, Args);
120 :
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 32168 : Root->replaceOperandWith(0, Root);
126 :
127 : // We now have
128 : // !1 = metadata !{metadata !1} <- self-referential root
129 32168 : return Root;
130 : }
131 :
132 733 : MDNode *MDBuilder::createTBAARoot(StringRef Name) {
133 733 : return MDNode::get(Context, createString(Name));
134 : }
135 :
136 : /// Return metadata for a non-root TBAA node with the given name,
137 : /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
138 8 : MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
139 : bool isConstant) {
140 8 : if (isConstant) {
141 1 : Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
142 1 : return MDNode::get(Context,
143 1 : {createString(Name), Parent, createConstant(Flags)});
144 : }
145 7 : return MDNode::get(Context, {createString(Name), Parent});
146 : }
147 :
148 0 : MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
149 0 : return MDNode::get(Context, createString(Name));
150 : }
151 :
152 0 : MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
153 0 : return MDNode::get(Context, {createString(Name), Domain});
154 : }
155 :
156 : /// Return metadata for a tbaa.struct node with the given
157 : /// struct field descriptions.
158 3087 : MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
159 3087 : SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
160 3087 : Type *Int64 = Type::getInt64Ty(Context);
161 7159 : for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
162 8144 : Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
163 4072 : Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
164 8144 : Vals[i * 3 + 2] = Fields[i].Type;
165 : }
166 3087 : return MDNode::get(Context, Vals);
167 : }
168 :
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 5931 : MDNode *MDBuilder::createTBAAStructTypeNode(
172 : StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
173 5931 : SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
174 5931 : Type *Int64 = Type::getInt64Ty(Context);
175 5931 : Ops[0] = createString(Name);
176 15634 : for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
177 19406 : Ops[i * 2 + 1] = Fields[i].first;
178 9703 : Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
179 : }
180 5931 : return MDNode::get(Context, Ops);
181 : }
182 :
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 20746 : MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
186 : uint64_t Offset) {
187 20746 : ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
188 20746 : return MDNode::get(Context,
189 20746 : {createString(Name), Parent, createConstant(Off)});
190 : }
191 :
192 : /// Return metadata for a TBAA tag node with the given
193 : /// base type, access type and offset relative to the base type.
194 7614 : MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
195 : uint64_t Offset, bool IsConstant) {
196 7614 : IntegerType *Int64 = Type::getInt64Ty(Context);
197 7614 : ConstantInt *Off = ConstantInt::get(Int64, Offset);
198 7614 : if (IsConstant) {
199 0 : return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
200 0 : createConstant(ConstantInt::get(Int64, 1))});
201 : }
202 7614 : return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
203 : }
204 :
205 104 : MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
206 : Metadata *Id,
207 : ArrayRef<TBAAStructField> Fields) {
208 104 : SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
209 104 : Type *Int64 = Type::getInt64Ty(Context);
210 104 : Ops[0] = Parent;
211 104 : Ops[1] = createConstant(ConstantInt::get(Int64, Size));
212 104 : Ops[2] = Id;
213 181 : for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
214 154 : Ops[I * 3 + 3] = Fields[I].Type;
215 77 : Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
216 77 : Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
217 : }
218 104 : return MDNode::get(Context, Ops);
219 : }
220 :
221 68 : MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
222 : uint64_t Offset, uint64_t Size,
223 : bool IsImmutable) {
224 68 : IntegerType *Int64 = Type::getInt64Ty(Context);
225 68 : auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
226 68 : auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
227 68 : if (IsImmutable) {
228 0 : auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
229 0 : return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
230 : ImmutabilityFlagNode});
231 : }
232 68 : return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
233 : }
234 :
235 4 : 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();
240 :
241 : bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
242 :
243 : // See if the tag is already mutable.
244 : unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
245 4 : if (Tag->getNumOperands() <= ImmutabilityFlagOp)
246 : return Tag;
247 :
248 : // If Tag is already mutable then return it.
249 : Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
250 4 : if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
251 : return Tag;
252 :
253 : // Otherwise, create another node.
254 4 : if (!NewFormat)
255 2 : return createTBAAStructTagNode(BaseType, AccessType, Offset);
256 :
257 : Metadata *SizeNode = Tag->getOperand(3);
258 : uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
259 2 : return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
260 : }
261 :
262 29 : MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
263 : Metadata *Vals[] = {
264 29 : createString("loop_header_weight"),
265 29 : createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
266 29 : };
267 29 : return MDNode::get(Context, Vals);
268 : }
|