LLVM  14.0.0git
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::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 
79  return MDNode::get(Context,
80  {createString("function_section_prefix"),
82 }
83 
85  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
86 
87  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
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  SmallVector<Metadata *, 3> Args(1, nullptr);
155  if (Extra)
156  Args.push_back(Extra);
157  if (!Name.empty())
158  Args.push_back(createString(Name));
159  MDNode *Root = MDNode::getDistinct(Context, Args);
160 
161  // At this point we have
162  // !0 = distinct !{null} <- root
163  // Replace the reserved operand with the root node itself.
164  Root->replaceOperandWith(0, Root);
165 
166  // We now have
167  // !0 = distinct !{!0} <- root
168  return Root;
169 }
170 
172  return MDNode::get(Context, createString(Name));
173 }
174 
175 /// Return metadata for a non-root TBAA node with the given name,
176 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
178  bool isConstant) {
179  if (isConstant) {
180  Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
181  return MDNode::get(Context,
182  {createString(Name), Parent, createConstant(Flags)});
183  }
184  return MDNode::get(Context, {createString(Name), Parent});
185 }
186 
188  return MDNode::get(Context, createString(Name));
189 }
190 
192  return MDNode::get(Context, {createString(Name), Domain});
193 }
194 
195 /// Return metadata for a tbaa.struct node with the given
196 /// struct field descriptions.
198  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
199  Type *Int64 = Type::getInt64Ty(Context);
200  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
201  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
202  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
203  Vals[i * 3 + 2] = Fields[i].Type;
204  }
205  return MDNode::get(Context, Vals);
206 }
207 
208 /// Return metadata for a TBAA struct node in the type DAG
209 /// with the given name, a list of pairs (offset, field type in the type DAG).
211  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
212  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
213  Type *Int64 = Type::getInt64Ty(Context);
214  Ops[0] = createString(Name);
215  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
216  Ops[i * 2 + 1] = Fields[i].first;
217  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
218  }
219  return MDNode::get(Context, Ops);
220 }
221 
222 /// Return metadata for a TBAA scalar type node with the
223 /// given name, an offset and a parent in the TBAA type DAG.
225  uint64_t Offset) {
227  return MDNode::get(Context,
228  {createString(Name), Parent, createConstant(Off)});
229 }
230 
231 /// Return metadata for a TBAA tag node with the given
232 /// base type, access type and offset relative to the base type.
234  uint64_t Offset, bool IsConstant) {
235  IntegerType *Int64 = Type::getInt64Ty(Context);
236  ConstantInt *Off = ConstantInt::get(Int64, Offset);
237  if (IsConstant) {
238  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
239  createConstant(ConstantInt::get(Int64, 1))});
240  }
241  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
242 }
243 
245  Metadata *Id,
246  ArrayRef<TBAAStructField> Fields) {
247  SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
248  Type *Int64 = Type::getInt64Ty(Context);
249  Ops[0] = Parent;
250  Ops[1] = createConstant(ConstantInt::get(Int64, Size));
251  Ops[2] = Id;
252  for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
253  Ops[I * 3 + 3] = Fields[I].Type;
254  Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
255  Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
256  }
257  return MDNode::get(Context, Ops);
258 }
259 
261  uint64_t Offset, uint64_t Size,
262  bool IsImmutable) {
263  IntegerType *Int64 = Type::getInt64Ty(Context);
264  auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
265  auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
266  if (IsImmutable) {
267  auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
268  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
269  ImmutabilityFlagNode});
270  }
271  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
272 }
273 
275  MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
276  MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
277  Metadata *OffsetNode = Tag->getOperand(2);
278  uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
279 
280  bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
281 
282  // See if the tag is already mutable.
283  unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
284  if (Tag->getNumOperands() <= ImmutabilityFlagOp)
285  return Tag;
286 
287  // If Tag is already mutable then return it.
288  Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
289  if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
290  return Tag;
291 
292  // Otherwise, create another node.
293  if (!NewFormat)
294  return createTBAAStructTagNode(BaseType, AccessType, Offset);
295 
296  Metadata *SizeNode = Tag->getOperand(3);
297  uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
298  return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
299 }
300 
302  Metadata *Vals[] = {
303  createString("loop_header_weight"),
305  };
306  return MDNode::get(Context, Vals);
307 }
308 
309 MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,
310  Function *F) {
311  auto *Int64Ty = Type::getInt64Ty(Context);
313  Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));
314  Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));
315  Ops[2] = createString(F->getName());
316  return MDNode::get(Context, Ops);
317 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MDBuilder::createConstant
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
llvm::MDBuilder::createMutableTBAAAccessTag
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:274
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:200
llvm::MDBuilder::createTBAAStructTypeNode
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:210
llvm::MDBuilder::createFPMath
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
Metadata.h
llvm::MDBuilder::createAliasScopeDomain
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:187
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::Function
Definition: Function.h:61
llvm::MDBuilder::createPseudoProbeDesc
MDNode * createPseudoProbeDesc(uint64_t GUID, uint64_t Hash, Function *F)
Return metadata containing the pseudo probe descriptor for a function.
Definition: MDBuilder.cpp:309
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::MipsISD::Lo
@ Lo
Definition: MipsISelLowering.h:79
llvm::MDBuilder::createAliasScope
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:191
llvm::MDBuilder::createUnpredictable
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:419
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
BaseType
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:203
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1198
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MipsISD::Hi
@ Hi
Definition: MipsISelLowering.h:75
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1108
llvm::detail::DenseSetImpl::end
iterator end()
Definition: DenseSet.h:174
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
Constants.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::MDBuilder::createBranchWeights
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
llvm::MDBuilder::createTBAANode
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,...
Definition: MDBuilder.cpp:177
Domain
Domain
Definition: CorrelatedValuePropagation.cpp:660
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
MDBuilder.h
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:606
llvm::ConstantInt::get
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:899
llvm::MDBuilder::createTBAAStructTagNode
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:233
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::None
const NoneType None
Definition: None.h:23
llvm::MDBuilder::mergeCallbackEncodings
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:124
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1102
llvm::MDBuilder::createString
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
llvm::DenseSet< GlobalValue::GUID >
BaseType
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
Definition: SafepointIRVerifier.cpp:316
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
isConstant
static bool isConstant(const MachineInstr &MI)
Definition: AMDGPUInstructionSelector.cpp:2311
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:473
llvm::detail::DenseSetImpl::begin
iterator begin()
Definition: DenseSet.h:173
llvm::MDBuilder::createTBAAAccessTag
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,...
Definition: MDBuilder.cpp:260
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDBuilder::createCallbackEncoding
MDNode * createCallbackEncoding(unsigned CalleeArgNo, ArrayRef< int > Arguments, bool VarArgsArePassed)
Return metadata describing a callback (see llvm::AbstractCallSite).
Definition: MDBuilder.cpp:107
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::ArrayRef< uint32_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::MDNode::getDistinct
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1206
llvm::MDBuilder::createFunctionSectionPrefix
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:78
llvm::MDBuilder::createAnonymousAARoot
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:153
llvm::ConstantAsMetadata
Definition: Metadata.h:412
llvm::MDBuilder::createTBAAStructNode
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:197
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:204
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:314
llvm::ConstantFP::get
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:946
Function.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1488
Arguments
AMDGPU Lower Kernel Arguments
Definition: AMDGPULowerKernelArguments.cpp:244
llvm::MDBuilder::createTBAAScalarTypeNode
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:224
llvm::MDNode::replaceOperandWith
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:877
llvm::MDBuilder::createTBAARoot
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:171
llvm::MDBuilder::createCallees
MDNode * createCallees(ArrayRef< Function * > Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:100
llvm::MDBuilder::createRange
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:84
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:275
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::MDBuilder::createTBAATypeNode
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,...
Definition: MDBuilder.cpp:244
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:221
llvm::MDString
A single uniqued string.
Definition: Metadata.h:611
llvm::MDBuilder::createFunctionEntryCount
MDNode * createFunctionEntryCount(uint64_t Count, bool Synthetic, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, a boolean \Synthetic indicating whether th...
Definition: MDBuilder.cpp:59
llvm::Type::getFloatTy
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:190
llvm::MDBuilder::createIrrLoopHeaderWeight
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:301
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38