LLVM  9.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  std::stable_sort(OrderID.begin(), OrderID.end(),
73  return A < B;});
74  for (auto ID : OrderID)
76  }
77  return MDNode::get(Context, Ops);
78 }
79 
81  return MDNode::get(Context,
82  {createString("function_section_prefix"),
83  createString(Prefix)});
84 }
85 
87  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
88 
89  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
90  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
91 }
92 
94  // If the range is everything then it is useless.
95  if (Hi == Lo)
96  return nullptr;
97 
98  // Return the range [Lo, Hi).
99  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
100 }
101 
104  for (Function *F : Callees)
105  Ops.push_back(createConstant(F));
106  return MDNode::get(Context, Ops);
107 }
108 
111  bool VarArgArePassed) {
113 
114  Type *Int64 = Type::getInt64Ty(Context);
115  Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
116 
117  for (int ArgNo : Arguments)
118  Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
119 
120  Type *Int1 = Type::getInt1Ty(Context);
121  Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
122 
123  return MDNode::get(Context, Ops);
124 }
125 
127  MDNode *NewCB) {
128  if (!ExistingCallbacks)
129  return MDNode::get(Context, {NewCB});
130 
131  auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
132  uint64_t NewCBCalleeIdx =
133  cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
134  (void)NewCBCalleeIdx;
135 
137  unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
138  Ops.resize(NumExistingOps + 1);
139 
140  for (unsigned u = 0; u < NumExistingOps; u++) {
141  Ops[u] = ExistingCallbacks->getOperand(u);
142 
143  auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
144  uint64_t OldCBCalleeIdx =
145  cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
146  (void)OldCBCalleeIdx;
147  assert(NewCBCalleeIdx != OldCBCalleeIdx &&
148  "Cannot map a callback callee index twice!");
149  }
150 
151  Ops[NumExistingOps] = NewCB;
152  return MDNode::get(Context, Ops);
153 }
154 
156  // To ensure uniqueness the root node is self-referential.
157  auto Dummy = MDNode::getTemporary(Context, None);
158 
160  if (Extra)
161  Args.push_back(Extra);
162  if (!Name.empty())
163  Args.push_back(createString(Name));
164  MDNode *Root = MDNode::get(Context, Args);
165 
166  // At this point we have
167  // !0 = metadata !{} <- dummy
168  // !1 = metadata !{metadata !0} <- root
169  // Replace the dummy operand with the root node itself and delete the dummy.
170  Root->replaceOperandWith(0, Root);
171 
172  // We now have
173  // !1 = metadata !{metadata !1} <- self-referential root
174  return Root;
175 }
176 
178  return MDNode::get(Context, createString(Name));
179 }
180 
181 /// Return metadata for a non-root TBAA node with the given name,
182 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
184  bool isConstant) {
185  if (isConstant) {
186  Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
187  return MDNode::get(Context,
188  {createString(Name), Parent, createConstant(Flags)});
189  }
190  return MDNode::get(Context, {createString(Name), Parent});
191 }
192 
194  return MDNode::get(Context, createString(Name));
195 }
196 
198  return MDNode::get(Context, {createString(Name), Domain});
199 }
200 
201 /// Return metadata for a tbaa.struct node with the given
202 /// struct field descriptions.
204  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
205  Type *Int64 = Type::getInt64Ty(Context);
206  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
207  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
208  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
209  Vals[i * 3 + 2] = Fields[i].Type;
210  }
211  return MDNode::get(Context, Vals);
212 }
213 
214 /// Return metadata for a TBAA struct node in the type DAG
215 /// with the given name, a list of pairs (offset, field type in the type DAG).
217  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
218  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
219  Type *Int64 = Type::getInt64Ty(Context);
220  Ops[0] = createString(Name);
221  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
222  Ops[i * 2 + 1] = Fields[i].first;
223  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
224  }
225  return MDNode::get(Context, Ops);
226 }
227 
228 /// Return metadata for a TBAA scalar type node with the
229 /// given name, an offset and a parent in the TBAA type DAG.
231  uint64_t Offset) {
232  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
233  return MDNode::get(Context,
234  {createString(Name), Parent, createConstant(Off)});
235 }
236 
237 /// Return metadata for a TBAA tag node with the given
238 /// base type, access type and offset relative to the base type.
240  uint64_t Offset, bool IsConstant) {
241  IntegerType *Int64 = Type::getInt64Ty(Context);
242  ConstantInt *Off = ConstantInt::get(Int64, Offset);
243  if (IsConstant) {
244  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
245  createConstant(ConstantInt::get(Int64, 1))});
246  }
247  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
248 }
249 
251  Metadata *Id,
252  ArrayRef<TBAAStructField> Fields) {
253  SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
254  Type *Int64 = Type::getInt64Ty(Context);
255  Ops[0] = Parent;
256  Ops[1] = createConstant(ConstantInt::get(Int64, Size));
257  Ops[2] = Id;
258  for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
259  Ops[I * 3 + 3] = Fields[I].Type;
260  Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
261  Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
262  }
263  return MDNode::get(Context, Ops);
264 }
265 
267  uint64_t Offset, uint64_t Size,
268  bool IsImmutable) {
269  IntegerType *Int64 = Type::getInt64Ty(Context);
270  auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
271  auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
272  if (IsImmutable) {
273  auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
274  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
275  ImmutabilityFlagNode});
276  }
277  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
278 }
279 
281  MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));
282  MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));
283  Metadata *OffsetNode = Tag->getOperand(2);
284  uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();
285 
286  bool NewFormat = isa<MDNode>(AccessType->getOperand(0));
287 
288  // See if the tag is already mutable.
289  unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;
290  if (Tag->getNumOperands() <= ImmutabilityFlagOp)
291  return Tag;
292 
293  // If Tag is already mutable then return it.
294  Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
295  if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
296  return Tag;
297 
298  // Otherwise, create another node.
299  if (!NewFormat)
300  return createTBAAStructTagNode(BaseType, AccessType, Offset);
301 
302  Metadata *SizeNode = Tag->getOperand(3);
303  uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
304  return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
305 }
306 
308  Metadata *Vals[] = {
309  createString("loop_header_weight"),
311  };
312  return MDNode::get(Context, Vals);
313 }
uint64_t CallInst * C
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:86
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:155
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:492
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:1508
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:307
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:266
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:126
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:250
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:409
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1165
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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:109
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:193
Class to represent integer types.
Definition: DerivedTypes.h:39
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:203
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:230
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:841
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:631
MDNode * mergeCallbackEncodings(MDNode *ExistingCallbacks, MDNode *NewCB)
Merge the new callback encoding NewCB into ExistingCallbacks.
Definition: MDBuilder.cpp:126
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:694
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:102
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:216
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:197
#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:1309
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:239
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:183
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:80
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:177
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:280
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:344