LLVM  4.0.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  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  Type *Int64Ty = Type::getInt64Ty(Context);
61  return MDNode::get(Context,
62  {createString("function_entry_count"),
63  createConstant(ConstantInt::get(Int64Ty, Count))});
64 }
65 
67  return MDNode::get(Context,
68  {createString("function_section_prefix"),
69  createString(Prefix)});
70 }
71 
73  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
74 
75  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
76  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
77 }
78 
80  // If the range is everything then it is useless.
81  if (Hi == Lo)
82  return nullptr;
83 
84  // Return the range [Lo, Hi).
85  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
86 }
87 
89  // To ensure uniqueness the root node is self-referential.
90  auto Dummy = MDNode::getTemporary(Context, None);
91 
93  if (Extra)
94  Args.push_back(Extra);
95  if (!Name.empty())
96  Args.push_back(createString(Name));
97  MDNode *Root = MDNode::get(Context, Args);
98 
99  // At this point we have
100  // !0 = metadata !{} <- dummy
101  // !1 = metadata !{metadata !0} <- root
102  // Replace the dummy operand with the root node itself and delete the dummy.
103  Root->replaceOperandWith(0, Root);
104 
105  // We now have
106  // !1 = metadata !{metadata !1} <- self-referential root
107  return Root;
108 }
109 
111  return MDNode::get(Context, createString(Name));
112 }
113 
114 /// \brief Return metadata for a non-root TBAA node with the given name,
115 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
117  bool isConstant) {
118  if (isConstant) {
120  return MDNode::get(Context,
121  {createString(Name), Parent, createConstant(Flags)});
122  }
123  return MDNode::get(Context, {createString(Name), Parent});
124 }
125 
127  return MDNode::get(Context, createString(Name));
128 }
129 
131  return MDNode::get(Context, {createString(Name), Domain});
132 }
133 
134 /// \brief Return metadata for a tbaa.struct node with the given
135 /// struct field descriptions.
137  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
138  Type *Int64 = Type::getInt64Ty(Context);
139  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
140  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
141  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
142  Vals[i * 3 + 2] = Fields[i].TBAA;
143  }
144  return MDNode::get(Context, Vals);
145 }
146 
147 /// \brief Return metadata for a TBAA struct node in the type DAG
148 /// with the given name, a list of pairs (offset, field type in the type DAG).
150  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
151  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
152  Type *Int64 = Type::getInt64Ty(Context);
153  Ops[0] = createString(Name);
154  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
155  Ops[i * 2 + 1] = Fields[i].first;
156  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
157  }
158  return MDNode::get(Context, Ops);
159 }
160 
161 /// \brief Return metadata for a TBAA scalar type node with the
162 /// given name, an offset and a parent in the TBAA type DAG.
164  uint64_t Offset) {
165  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
166  return MDNode::get(Context,
167  {createString(Name), Parent, createConstant(Off)});
168 }
169 
170 /// \brief Return metadata for a TBAA tag node with the given
171 /// base type, access type and offset relative to the base type.
173  uint64_t Offset, bool IsConstant) {
174  IntegerType *Int64 = Type::getInt64Ty(Context);
175  ConstantInt *Off = ConstantInt::get(Int64, Offset);
176  if (IsConstant) {
177  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
178  createConstant(ConstantInt::get(Int64, 1))});
179  }
180  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
181 }
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:72
MDNode * createAnonymousAARoot(StringRef Name=StringRef(), MDNode *Extra=nullptr)
Return metadata appropriate for a AA root node (scope or TBAA).
Definition: MDBuilder.cpp:88
MDNode * createUnpredictable()
Return metadata specifying that a branch or switch is unpredictable.
Definition: MDBuilder.cpp:55
size_t i
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:819
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:414
This file contains the declarations for metadata subclasses.
Metadata node.
Definition: Metadata.h:830
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:170
MDNode * createFPMath(float Accuracy)
Return metadata with the given settings.
Definition: MDBuilder.cpp:28
struct fuzzer::@269 Flags
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:157
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:392
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
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:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint32_t Offset
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1255
MDNode * createAliasScopeDomain(StringRef Name)
Return metadata appropriate for an alias scope domain node with the given name.
Definition: MDBuilder.cpp:126
Class to represent integer types.
Definition: DerivedTypes.h:39
MDNode * createFunctionEntryCount(uint64_t Count)
Return metadata containing the entry count for a function.
Definition: MDBuilder.cpp:59
MDNode * createTBAAStructNode(ArrayRef< TBAAStructField > Fields)
Return metadata for a tbaa.struct node with the given struct field descriptions.
Definition: MDBuilder.cpp:136
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:163
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:234
uint64_t * Vals
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
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:558
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:623
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Class for arbitrary precision integers.
Definition: APInt.h:77
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1144
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1132
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:149
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:169
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:130
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:172
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:116
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:66
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
MDNode * createTBAARoot(StringRef Name)
Return metadata appropriate for a TBAA root node with the given name.
Definition: MDBuilder.cpp:110
A single uniqued string.
Definition: Metadata.h:586
IntegerType * Int32Ty