LLVM  8.0.1
MDBuilder.cpp
Go to the documentation of this file.
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 
22  return MDString::get(Context, Str);
23 }
24 
26  return ConstantAsMetadata::get(C);
27 }
28 
29 MDNode *MDBuilder::createFPMath(float Accuracy) {
30  if (Accuracy == 0.0)
31  return nullptr;
32  assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
33  auto *Op =
34  createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
35  return MDNode::get(Context, Op);
36 }
37 
39  uint32_t FalseWeight) {
40  return createBranchWeights({TrueWeight, FalseWeight});
41 }
42 
44  assert(Weights.size() >= 1 && "Need at least one branch weights!");
45 
46  SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
47  Vals[0] = createString("branch_weights");
48 
49  Type *Int32Ty = Type::getInt32Ty(Context);
50  for (unsigned i = 0, e = Weights.size(); i != e; ++i)
51  Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
52 
53  return MDNode::get(Context, Vals);
54 }
55 
57  return MDNode::get(Context, None);
58 }
59 
61  uint64_t Count, bool Synthetic,
62  const DenseSet<GlobalValue::GUID> *Imports) {
63  Type *Int64Ty = Type::getInt64Ty(Context);
65  if (Synthetic)
66  Ops.push_back(createString("synthetic_function_entry_count"));
67  else
68  Ops.push_back(createString("function_entry_count"));
69  Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
70  if (Imports) {
71  SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
72  std::stable_sort(OrderID.begin(), OrderID.end(),
74  return A < B;});
75  for (auto ID : OrderID)
77  }
78  return MDNode::get(Context, Ops);
79 }
80 
82  return MDNode::get(Context,
83  {createString("function_section_prefix"),
84  createString(Prefix)});
85 }
86 
88  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
89 
90  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
91  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
92 }
93 
95  // If the range is everything then it is useless.
96  if (Hi == Lo)
97  return nullptr;
98 
99  // Return the range [Lo, Hi).
100  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
101 }
102 
105  for (Function *F : Callees)
106  Ops.push_back(createConstant(F));
107  return MDNode::get(Context, Ops);
108 }
109 
111  // To ensure uniqueness the root node is self-referential.
112  auto Dummy = MDNode::getTemporary(Context, None);
113 
115  if (Extra)
116  Args.push_back(Extra);
117  if (!Name.empty())
118  Args.push_back(createString(Name));
119  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  Root->replaceOperandWith(0, Root);
126 
127  // We now have
128  // !1 = metadata !{metadata !1} <- self-referential root
129  return Root;
130 }
131 
133  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'.
139  bool isConstant) {
140  if (isConstant) {
141  Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
142  return MDNode::get(Context,
143  {createString(Name), Parent, createConstant(Flags)});
144  }
145  return MDNode::get(Context, {createString(Name), Parent});
146 }
147 
149  return MDNode::get(Context, createString(Name));
150 }
151 
153  return MDNode::get(Context, {createString(Name), Domain});
154 }
155 
156 /// Return metadata for a tbaa.struct node with the given
157 /// struct field descriptions.
159  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
160  Type *Int64 = Type::getInt64Ty(Context);
161  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
162  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
163  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
164  Vals[i * 3 + 2] = Fields[i].Type;
165  }
166  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).
172  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
173  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
174  Type *Int64 = Type::getInt64Ty(Context);
175  Ops[0] = createString(Name);
176  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
177  Ops[i * 2 + 1] = Fields[i].first;
178  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
179  }
180  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.
186  uint64_t Offset) {
187  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
188  return MDNode::get(Context,
189  {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.
195  uint64_t Offset, bool IsConstant) {
196  IntegerType *Int64 = Type::getInt64Ty(Context);
197  ConstantInt *Off = ConstantInt::get(Int64, Offset);
198  if (IsConstant) {
199  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
200  createConstant(ConstantInt::get(Int64, 1))});
201  }
202  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
203 }
204 
206  Metadata *Id,
207  ArrayRef<TBAAStructField> Fields) {
208  SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);
209  Type *Int64 = Type::getInt64Ty(Context);
210  Ops[0] = Parent;
211  Ops[1] = createConstant(ConstantInt::get(Int64, Size));
212  Ops[2] = Id;
213  for (unsigned I = 0, E = Fields.size(); I != E; ++I) {
214  Ops[I * 3 + 3] = Fields[I].Type;
215  Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));
216  Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));
217  }
218  return MDNode::get(Context, Ops);
219 }
220 
222  uint64_t Offset, uint64_t Size,
223  bool IsImmutable) {
224  IntegerType *Int64 = Type::getInt64Ty(Context);
225  auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));
226  auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));
227  if (IsImmutable) {
228  auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));
229  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,
230  ImmutabilityFlagNode});
231  }
232  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});
233 }
234 
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  if (Tag->getNumOperands() <= ImmutabilityFlagOp)
246  return Tag;
247 
248  // If Tag is already mutable then return it.
249  Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);
250  if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())
251  return Tag;
252 
253  // Otherwise, create another node.
254  if (!NewFormat)
255  return createTBAAStructTagNode(BaseType, AccessType, Offset);
256 
257  Metadata *SizeNode = Tag->getOperand(3);
258  uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
259  return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
260 }
261 
263  Metadata *Vals[] = {
264  createString("loop_header_weight"),
266  };
267  return MDNode::get(Context, Vals);
268 }
uint64_t CallInst * C
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:87
static bool isConstant(const MachineInstr &MI)
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:110
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:56
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:493
This class represents lattice values for constants.
Definition: AllocatorList.h:24
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:25
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:859
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:454
Implements a dense probed hash-table based set.
Definition: DenseSet.h:250
This file contains the declarations for metadata subclasses.
unsigned second
Metadata node.
Definition: Metadata.h:864
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1069
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:29
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1509
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:262
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:60
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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:221
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:164
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1178
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:205
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:410
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
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:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:148
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:158
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:38
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:185
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:240
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
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:622
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:685
Class for arbitrary precision integers.
Definition: APInt.h:70
MDNode * createCallees(ArrayRef< Function *> Callees)
Return metadata indicating the possible callees of indirect calls.
Definition: MDBuilder.cpp:103
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:171
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:21
MDNode * createAliasScope(StringRef Name, MDNode *Domain)
Return metadata appropriate for an alias scope node with the given name.
Definition: MDBuilder.cpp:152
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:194
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:138
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:132
A single uniqued string.
Definition: Metadata.h:604
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1075
Root of the metadata hierarchy.
Definition: Metadata.h:58
MDNode * createMutableTBAAAccessTag(MDNode *Tag)
Return mutable version of the given mutable or immutable TBAA access tag.
Definition: MDBuilder.cpp:235
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IntegerType * Int32Ty