xref: /freebsd/contrib/llvm-project/llvm/lib/IR/MDBuilder.cpp (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MDBuilder class, which is used as a convenient way to
10 // create LLVM metadata with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/MDBuilder.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Metadata.h"
18 using namespace llvm;
19 
20 MDString *MDBuilder::createString(StringRef Str) {
21   return MDString::get(Context, Str);
22 }
23 
24 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
25   return ConstantAsMetadata::get(C);
26 }
27 
28 MDNode *MDBuilder::createFPMath(float Accuracy) {
29   if (Accuracy == 0.0)
30     return nullptr;
31   assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
32   auto *Op =
33       createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
34   return MDNode::get(Context, Op);
35 }
36 
37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38                                        uint32_t FalseWeight) {
39   return createBranchWeights({TrueWeight, FalseWeight});
40 }
41 
42 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
43   assert(Weights.size() >= 1 && "Need at least one branch weights!");
44 
45   SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
46   Vals[0] = createString("branch_weights");
47 
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]));
51 
52   return MDNode::get(Context, Vals);
53 }
54 
55 MDNode *MDBuilder::createUnpredictable() {
56   return MDNode::get(Context, None);
57 }
58 
59 MDNode *MDBuilder::createFunctionEntryCount(
60     uint64_t Count, bool Synthetic,
61     const DenseSet<GlobalValue::GUID> *Imports) {
62   Type *Int64Ty = Type::getInt64Ty(Context);
63   SmallVector<Metadata *, 8> Ops;
64   if (Synthetic)
65     Ops.push_back(createString("synthetic_function_entry_count"));
66   else
67     Ops.push_back(createString("function_entry_count"));
68   Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
69   if (Imports) {
70     SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
71     llvm::sort(OrderID);
72     for (auto ID : OrderID)
73       Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
74   }
75   return MDNode::get(Context, Ops);
76 }
77 
78 MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
79   return MDNode::get(Context,
80                      {createString("function_section_prefix"),
81                       createString(Prefix)});
82 }
83 
84 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
85   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
86 
87   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
88   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
89 }
90 
91 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
92   // If the range is everything then it is useless.
93   if (Hi == Lo)
94     return nullptr;
95 
96   // Return the range [Lo, Hi).
97   return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
98 }
99 
100 MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
101   SmallVector<Metadata *, 4> Ops;
102   for (Function *F : Callees)
103     Ops.push_back(createConstant(F));
104   return MDNode::get(Context, Ops);
105 }
106 
107 MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
108                                           ArrayRef<int> Arguments,
109                                           bool VarArgArePassed) {
110   SmallVector<Metadata *, 4> Ops;
111 
112   Type *Int64 = Type::getInt64Ty(Context);
113   Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
114 
115   for (int ArgNo : Arguments)
116     Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
117 
118   Type *Int1 = Type::getInt1Ty(Context);
119   Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
120 
121   return MDNode::get(Context, Ops);
122 }
123 
124 MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
125                                           MDNode *NewCB) {
126   if (!ExistingCallbacks)
127     return MDNode::get(Context, {NewCB});
128 
129   auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
130   uint64_t NewCBCalleeIdx =
131       cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
132   (void)NewCBCalleeIdx;
133 
134   SmallVector<Metadata *, 4> Ops;
135   unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
136   Ops.resize(NumExistingOps + 1);
137 
138   for (unsigned u = 0; u < NumExistingOps; u++) {
139     Ops[u] = ExistingCallbacks->getOperand(u);
140 
141     auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
142     uint64_t OldCBCalleeIdx =
143       cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
144     (void)OldCBCalleeIdx;
145     assert(NewCBCalleeIdx != OldCBCalleeIdx &&
146            "Cannot map a callback callee index twice!");
147   }
148 
149   Ops[NumExistingOps] = NewCB;
150   return MDNode::get(Context, Ops);
151 }
152 
153 MDNode *MDBuilder::createRTTIPointerPrologue(Constant *PrologueSig,
154                                              Constant *RTTI) {
155   SmallVector<Metadata *, 4> Ops;
156   Ops.push_back(createConstant(PrologueSig));
157   Ops.push_back(createConstant(RTTI));
158   return MDNode::get(Context, Ops);
159 }
160 
161 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
162   SmallVector<Metadata *, 3> Args(1, nullptr);
163   if (Extra)
164     Args.push_back(Extra);
165   if (!Name.empty())
166     Args.push_back(createString(Name));
167   MDNode *Root = MDNode::getDistinct(Context, Args);
168 
169   // At this point we have
170   //   !0 = distinct !{null} <- root
171   // Replace the reserved operand with the root node itself.
172   Root->replaceOperandWith(0, Root);
173 
174   // We now have
175   //   !0 = distinct !{!0} <- root
176   return Root;
177 }
178 
179 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
180   return MDNode::get(Context, createString(Name));
181 }
182 
183 /// Return metadata for a non-root TBAA node with the given name,
184 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
185 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
186                                   bool isConstant) {
187   if (isConstant) {
188     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
189     return MDNode::get(Context,
190                        {createString(Name), Parent, createConstant(Flags)});
191   }
192   return MDNode::get(Context, {createString(Name), Parent});
193 }
194 
195 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
196   return MDNode::get(Context, createString(Name));
197 }
198 
199 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
200   return MDNode::get(Context, {createString(Name), Domain});
201 }
202 
203 /// Return metadata for a tbaa.struct node with the given
204 /// struct field descriptions.
205 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
206   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
207   Type *Int64 = Type::getInt64Ty(Context);
208   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
209     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
210     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
211     Vals[i * 3 + 2] = Fields[i].Type;
212   }
213   return MDNode::get(Context, Vals);
214 }
215 
216 /// Return metadata for a TBAA struct node in the type DAG
217 /// with the given name, a list of pairs (offset, field type in the type DAG).
218 MDNode *MDBuilder::createTBAAStructTypeNode(
219     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
220   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
221   Type *Int64 = Type::getInt64Ty(Context);
222   Ops[0] = createString(Name);
223   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
224     Ops[i * 2 + 1] = Fields[i].first;
225     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
226   }
227   return MDNode::get(Context, Ops);
228 }
229 
230 /// Return metadata for a TBAA scalar type node with the
231 /// given name, an offset and a parent in the TBAA type DAG.
232 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
233                                             uint64_t Offset) {
234   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
235   return MDNode::get(Context,
236                      {createString(Name), Parent, createConstant(Off)});
237 }
238 
239 /// Return metadata for a TBAA tag node with the given
240 /// base type, access type and offset relative to the base type.
241 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
242                                            uint64_t Offset, bool IsConstant) {
243   IntegerType *Int64 = Type::getInt64Ty(Context);
244   ConstantInt *Off = ConstantInt::get(Int64, Offset);
245   if (IsConstant) {
246     return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
247                                  createConstant(ConstantInt::get(Int64, 1))});
248   }
249   return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
250 }
251 
252 MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,
253                                       Metadata *Id,
254                                       ArrayRef<TBAAStructField> Fields) {
255   SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
256   Type *Int64 = Type::getInt64Ty(Context);
257   Ops[0] = Parent;
258   Ops[1] = createConstant(ConstantInt::get(Int64, Size));
259   Ops[2] = Id;
260   for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
261     Ops[I * 3 + 3] = Fields[I].Type;
262     Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
263     Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
264   }
265   return MDNode::get(Context, Ops);
266 }
267 
268 MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,
269                                        uint64_t Offset, uint64_t Size,
270                                        bool IsImmutable) {
271   IntegerType *Int64 = Type::getInt64Ty(Context);
272   auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
273   auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
274   if (IsImmutable) {
275     auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
276     return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
277                                  ImmutabilityFlagNode});
278   }
279   return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
280 }
281 
282 MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
283   MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
284   MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
285   Metadata *OffsetNode = Tag->getOperand(2);
286   uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
287 
288   bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
289 
290   // See if the tag is already mutable.
291   unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
292   if (Tag->getNumOperands() <= ImmutabilityFlagOp)
293     return Tag;
294 
295   // If Tag is already mutable then return it.
296   Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
297   if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
298     return Tag;
299 
300   // Otherwise, create another node.
301   if (!NewFormat)
302     return createTBAAStructTagNode(BaseType, AccessType, Offset);
303 
304   Metadata *SizeNode = Tag->getOperand(3);
305   uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
306   return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
307 }
308 
309 MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
310   Metadata *Vals[] = {
311     createString("loop_header_weight"),
312     createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
313   };
314   return MDNode::get(Context, Vals);
315 }
316 
317 MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
318                                          Function *F) {
319   auto *Int64Ty = Type::getInt64Ty(Context);
320   SmallVector<Metadata *, 3> Ops(3);
321   Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
322   Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
323   Ops[2] = createString(F->getName());
324   return MDNode::get(Context, Ops);
325 }
326