LLVM  10.0.0svn
MDBuilder.cpp
Go to the documentation of this file.
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 
21  return MDString::get(Context, Str);
22 }
23 
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 
38  uint32_t FalseWeight) {
39  return createBranchWeights({TrueWeight, FalseWeight});
40 }
41 
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 
56  return MDNode::get(Context, None);
57 }
58 
60  uint64_t Count, bool Synthetic,
61  const DenseSet<GlobalValue::GUID> *Imports) {
62  Type *Int64Ty = Type::getInt64Ty(Context);
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::stable_sort(OrderID);
72  for (auto ID : OrderID)
74  }
75  return MDNode::get(Context, Ops);
76 }
77 
79  return MDNode::get(Context,
80  {createString("function_section_prefix"),
81  createString(Prefix)});
82 }
83 
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 
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 
102  for (Function *F : Callees)
103  Ops.push_back(createConstant(F));
104  return MDNode::get(Context, Ops);
105 }
106 
109  bool VarArgArePassed) {
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 
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 
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 
154  // To ensure uniqueness the root node is self-referential.
155  auto Dummy = MDNode::getTemporary(Context, None);
156 
158  if (Extra)
159  Args.push_back(Extra);
160  if (!Name.empty())
161  Args.push_back(createString(Name));
162  MDNode *Root = MDNode::get(Context, Args);
163 
164  // At this point we have
165  // !0 = metadata !{} <- dummy
166  // !1 = metadata !{metadata !0} <- root
167  // Replace the dummy operand with the root node itself and delete the dummy.
168  Root->replaceOperandWith(0, Root);
169 
170  // We now have
171  // !1 = metadata !{metadata !1} <- self-referential root
172  return Root;
173 }
174 
176  return MDNode::get(Context, createString(Name));
177 }
178 
179 /// Return metadata for a non-root TBAA node with the given name,
180 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
182  bool isConstant) {
183  if (isConstant) {
184  Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
185  return MDNode::get(Context,
186  {createString(Name), Parent, createConstant(Flags)});
187  }
188  return MDNode::get(Context, {createString(Name), Parent});
189 }
190 
192  return MDNode::get(Context, createString(Name));
193 }
194 
196  return MDNode::get(Context, {createString(Name), Domain});
197 }
198 
199 /// Return metadata for a tbaa.struct node with the given
200 /// struct field descriptions.
202  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
203  Type *Int64 = Type::getInt64Ty(Context);
204  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
205  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
206  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
207  Vals[i * 3 + 2] = Fields[i].Type;
208  }
209  return MDNode::get(Context, Vals);
210 }
211 
212 /// Return metadata for a TBAA struct node in the type DAG
213 /// with the given name, a list of pairs (offset, field type in the type DAG).
215  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
216  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
217  Type *Int64 = Type::getInt64Ty(Context);
218  Ops[0] = createString(Name);
219  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
220  Ops[i * 2 + 1] = Fields[i].first;
221  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
222  }
223  return MDNode::get(Context, Ops);
224 }
225 
226 /// Return metadata for a TBAA scalar type node with the
227 /// given name, an offset and a parent in the TBAA type DAG.
229  uint64_t Offset) {
230  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
231  return MDNode::get(Context,
232  {createString(Name), Parent, createConstant(Off)});
233 }
234 
235 /// Return metadata for a TBAA tag node with the given
236 /// base type, access type and offset relative to the base type.
238  uint64_t Offset, bool IsConstant) {
239  IntegerType *Int64 = Type::getInt64Ty(Context);
240  ConstantInt *Off = ConstantInt::get(Int64, Offset);
241  if (IsConstant) {
242  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
243  createConstant(ConstantInt::get(Int64, 1))});
244  }
245  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
246 }
247 
249  Metadata *Id,
250  ArrayRef<TBAAStructField> Fields) {
251  SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
252  Type *Int64 = Type::getInt64Ty(Context);
253  Ops[0] = Parent;
254  Ops[1] = createConstant(ConstantInt::get(Int64, Size));
255  Ops[2] = Id;
256  for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
257  Ops[I * 3 + 3] = Fields[I].Type;
258  Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
259  Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
260  }
261  return MDNode::get(Context, Ops);
262 }
263 
265  uint64_t Offset, uint64_t Size,
266  bool IsImmutable) {
267  IntegerType *Int64 = Type::getInt64Ty(Context);
268  auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
269  auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
270  if (IsImmutable) {
271  auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
272  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
273  ImmutabilityFlagNode});
274  }
275  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
276 }
277 
279  MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
280  MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
281  Metadata *OffsetNode = Tag->getOperand(2);
282  uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
283 
284  bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
285 
286  // See if the tag is already mutable.
287  unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
288  if (Tag->getNumOperands() <= ImmutabilityFlagOp)
289  return Tag;
290 
291  // If Tag is already mutable then return it.
292  Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
293  if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
294  return Tag;
295 
296  // Otherwise, create another node.
297  if (!NewFormat)
298  return createTBAAStructTagNode(BaseType, AccessType, Offset);
299 
300  Metadata *SizeNode = Tag->getOperand(3);
301  uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
302  return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
303 }
304 
306  Metadata *Vals[] = {
307  createString("loop_header_weight"),
309  };
310  return MDNode::get(Context, Vals);
311 }
312 
313 MDNode *MDBuilder::createMisExpect(uint64_t Index, uint64_t LikleyWeight,
314  uint64_t UnlikleyWeight) {
315  auto *IntType = Type::getInt64Ty(Context);
316  Metadata *Vals[] = {
317  createString("misexpect"),
318  createConstant(ConstantInt::get(IntType, Index)),
319  createConstant(ConstantInt::get(IntType, LikleyWeight)),
320  createConstant(ConstantInt::get(IntType, UnlikleyWeight)),
321  };
322  return MDNode::get(Context, Vals);
323 }
uint64_t CallInst * C
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:84
static bool isConstant(const MachineInstr &MI)
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:172
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:153
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:858
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:453
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
This file contains the declarations for metadata subclasses.
unsigned second
Metadata node.
Definition: Metadata.h:863
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1068
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:176
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1517
MDNode * createMisExpect(uint64_t Index, uint64_t LikelyWeight, uint64_t UnlikelyWeight)
return metadata containing expected value
Definition: MDBuilder.cpp:313
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:305
MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean indicating whether the counts w...
Definition: MDBuilder.cpp:59
MDNode * createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, uint64_t Size, bool IsImmutable=false)
Return metadata for a TBAA access tag with the given base type, final access type, offset of the access relative to the base type, size of the access and flag indicating whether the accessed object can be considered immutable for the purposes of the TBAA analysis.
Definition: MDBuilder.cpp:264
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:163
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1177
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
MDNode * createTBAATypeNode(MDNode *Parent, uint64_t Size, Metadata *Id, ArrayRef< TBAAStructField > Fields=ArrayRef< TBAAStructField >())
Return metadata for a TBAA type node in the TBAA type DAG with the given parent type, size in bytes, type identifier and a list of fields.
Definition: MDBuilder.cpp:248
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:409
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1165
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:107
This file contains the declarations for the subclasses of Constant, which represent the different fla...
AMDGPU Lower Kernel Arguments
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:191
Class to represent integer types.
Definition: DerivedTypes.h:40
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:201
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
MDNode * createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset=0)
Return metadata for a TBAA scalar type node with the given name, an offset and a parent in the TBAA t...
Definition: MDBuilder.cpp:228
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:239
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:653
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:124
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:716
Class for arbitrary precision integers.
Definition: APInt.h:69
MDNode * createCallees(ArrayRef< Function *> Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:100
MDNode * createTBAAStructTypeNode(StringRef Name, ArrayRef< std::pair< MDNode *, uint64_t >> Fields)
Return metadata for a TBAA struct node in the type DAG with the given name, a list of pairs (offset...
Definition: MDBuilder.cpp:214
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:175
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:195
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void stable_sort(R &&Range)
Definition: STLExtras.h:1289
MDNode * createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset, bool IsConstant=false)
Return metadata for a TBAA tag node with the given base type, access type and offset relative to the ...
Definition: MDBuilder.cpp:237
MDNode * createTBAANode(StringRef Name, MDNode *Parent, bool isConstant=false)
Return metadata for a non-root TBAA node with the given name, parent in the TBAA tree, and value for &#39;pointsToConstantMemory&#39;.
Definition: MDBuilder.cpp:181
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:78
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:175
A single uniqued string.
Definition: Metadata.h:603
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1074
Root of the metadata hierarchy.
Definition: Metadata.h:57
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:278
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:344