LLVM  3.7.0
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/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  uint32_t Weights[] = {TrueWeight, FalseWeight};
40  return createBranchWeights(Weights);
41 }
42 
44  assert(Weights.size() >= 2 && "Need at least two 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 
58  Vals[0] = createString("function_entry_count");
59 
60  Type *Int64Ty = Type::getInt64Ty(Context);
61  Vals[1] = createConstant(ConstantInt::get(Int64Ty, Count));
62 
63  return MDNode::get(Context, Vals);
64 }
65 
67  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
68 
69  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
70  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
71 }
72 
74  // If the range is everything then it is useless.
75  if (Hi == Lo)
76  return nullptr;
77 
78  // Return the range [Lo, Hi).
79  Metadata *Range[2] = {createConstant(Lo), createConstant(Hi)};
80  return MDNode::get(Context, Range);
81 }
82 
84  // To ensure uniqueness the root node is self-referential.
85  auto Dummy = MDNode::getTemporary(Context, None);
86 
87  SmallVector<Metadata *, 3> Args(1, Dummy.get());
88  if (Extra)
89  Args.push_back(Extra);
90  if (!Name.empty())
91  Args.push_back(createString(Name));
92  MDNode *Root = MDNode::get(Context, Args);
93 
94  // At this point we have
95  // !0 = metadata !{} <- dummy
96  // !1 = metadata !{metadata !0} <- root
97  // Replace the dummy operand with the root node itself and delete the dummy.
98  Root->replaceOperandWith(0, Root);
99 
100  // We now have
101  // !1 = metadata !{metadata !1} <- self-referential root
102  return Root;
103 }
104 
106  return MDNode::get(Context, createString(Name));
107 }
108 
109 /// \brief Return metadata for a non-root TBAA node with the given name,
110 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
112  bool isConstant) {
113  if (isConstant) {
115  Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
116  return MDNode::get(Context, Ops);
117  } else {
118  Metadata *Ops[2] = {createString(Name), Parent};
119  return MDNode::get(Context, Ops);
120  }
121 }
122 
124  return MDNode::get(Context, createString(Name));
125 }
126 
128  Metadata *Ops[2] = {createString(Name), Domain};
129  return MDNode::get(Context, Ops);
130 }
131 
132 /// \brief Return metadata for a tbaa.struct node with the given
133 /// struct field descriptions.
135  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
136  Type *Int64 = Type::getInt64Ty(Context);
137  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
138  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
139  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
140  Vals[i * 3 + 2] = Fields[i].TBAA;
141  }
142  return MDNode::get(Context, Vals);
143 }
144 
145 /// \brief Return metadata for a TBAA struct node in the type DAG
146 /// with the given name, a list of pairs (offset, field type in the type DAG).
148  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
149  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
150  Type *Int64 = Type::getInt64Ty(Context);
151  Ops[0] = createString(Name);
152  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
153  Ops[i * 2 + 1] = Fields[i].first;
154  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
155  }
156  return MDNode::get(Context, Ops);
157 }
158 
159 /// \brief Return metadata for a TBAA scalar type node with the
160 /// given name, an offset and a parent in the TBAA type DAG.
162  uint64_t Offset) {
163  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
164  Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
165  return MDNode::get(Context, Ops);
166 }
167 
168 /// \brief Return metadata for a TBAA tag node with the given
169 /// base type, access type and offset relative to the base type.
171  uint64_t Offset, bool IsConstant) {
172  Type *Int64 = Type::getInt64Ty(Context);
173  if (IsConstant) {
174  Metadata *Ops[4] = {BaseType, AccessType,
175  createConstant(ConstantInt::get(Int64, Offset)),
176  createConstant(ConstantInt::get(Int64, 1))};
177  return MDNode::get(Context, Ops);
178  } else {
179  Metadata *Ops[3] = {BaseType, AccessType,
180  createConstant(ConstantInt::get(Int64, Offset))};
181  return MDNode::get(Context, Ops);
182  }
183 }
void push_back(const T &Elt)
Definition: SmallVector.h:222
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:66
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:83
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:743
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:360
This file contains the declarations for metadata subclasses.
Metadata node.
Definition: Metadata.h:740
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:228
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:318
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1273
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:123
MDNode * createFunctionEntryCount(uint64_t Count)
Return metadata containing the entry count for a function.
Definition: MDBuilder.cpp:56
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:134
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:161
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
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:582
static Constant * get(Type *Ty, double V)
get() - This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in the specified type.
Definition: Constants.cpp:652
Class for arbitrary precision integers.
Definition: APInt.h:73
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1039
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1030
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:147
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
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:127
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:170
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 'pointsToConstantMemory'.
Definition: MDBuilder.cpp:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:105
A single uniqued string.
Definition: Metadata.h:508
Root of the metadata hierarchy.
Definition: Metadata.h:45
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110