LLVM  6.0.0svn
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, const DenseSet<GlobalValue::GUID> *Imports) {
62  Type *Int64Ty = Type::getInt64Ty(Context);
64  Ops.push_back(createString("function_entry_count"));
65  Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
66  if (Imports) {
67  SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
68  std::stable_sort(OrderID.begin(), OrderID.end(),
70  return A < B;});
71  for (auto ID : OrderID)
73  }
74  return MDNode::get(Context, Ops);
75 }
76 
78  return MDNode::get(Context,
79  {createString("function_section_prefix"),
80  createString(Prefix)});
81 }
82 
84  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
85 
86  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
87  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
88 }
89 
91  // If the range is everything then it is useless.
92  if (Hi == Lo)
93  return nullptr;
94 
95  // Return the range [Lo, Hi).
96  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
97 }
98 
101  for (Function *F : Callees)
102  Ops.push_back(createConstant(F));
103  return MDNode::get(Context, Ops);
104 }
105 
107  // To ensure uniqueness the root node is self-referential.
108  auto Dummy = MDNode::getTemporary(Context, None);
109 
111  if (Extra)
112  Args.push_back(Extra);
113  if (!Name.empty())
114  Args.push_back(createString(Name));
115  MDNode *Root = MDNode::get(Context, Args);
116 
117  // At this point we have
118  // !0 = metadata !{} <- dummy
119  // !1 = metadata !{metadata !0} <- root
120  // Replace the dummy operand with the root node itself and delete the dummy.
121  Root->replaceOperandWith(0, Root);
122 
123  // We now have
124  // !1 = metadata !{metadata !1} <- self-referential root
125  return Root;
126 }
127 
129  return MDNode::get(Context, createString(Name));
130 }
131 
132 /// \brief Return metadata for a non-root TBAA node with the given name,
133 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
135  bool isConstant) {
136  if (isConstant) {
137  Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
138  return MDNode::get(Context,
139  {createString(Name), Parent, createConstant(Flags)});
140  }
141  return MDNode::get(Context, {createString(Name), Parent});
142 }
143 
145  return MDNode::get(Context, createString(Name));
146 }
147 
149  return MDNode::get(Context, {createString(Name), Domain});
150 }
151 
152 /// \brief Return metadata for a tbaa.struct node with the given
153 /// struct field descriptions.
155  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
156  Type *Int64 = Type::getInt64Ty(Context);
157  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
158  Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
159  Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
160  Vals[i * 3 + 2] = Fields[i].TBAA;
161  }
162  return MDNode::get(Context, Vals);
163 }
164 
165 /// \brief Return metadata for a TBAA struct node in the type DAG
166 /// with the given name, a list of pairs (offset, field type in the type DAG).
168  StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
169  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
170  Type *Int64 = Type::getInt64Ty(Context);
171  Ops[0] = createString(Name);
172  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
173  Ops[i * 2 + 1] = Fields[i].first;
174  Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
175  }
176  return MDNode::get(Context, Ops);
177 }
178 
179 /// \brief Return metadata for a TBAA scalar type node with the
180 /// given name, an offset and a parent in the TBAA type DAG.
182  uint64_t Offset) {
183  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
184  return MDNode::get(Context,
185  {createString(Name), Parent, createConstant(Off)});
186 }
187 
188 /// \brief Return metadata for a TBAA tag node with the given
189 /// base type, access type and offset relative to the base type.
191  uint64_t Offset, bool IsConstant) {
192  IntegerType *Int64 = Type::getInt64Ty(Context);
193  ConstantInt *Off = ConstantInt::get(Int64, Offset);
194  if (IsConstant) {
195  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
196  createConstant(ConstantInt::get(Int64, 1))});
197  }
198  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
199 }
200 
203  Vals[0] = createString("loop_header_weight");
204  Vals[1] = createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight));
205  return MDNode::get(Context, Vals);
206 }
uint64_t CallInst * C
MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition: MDBuilder.cpp:83
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:106
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:483
Compute iterated dominance frontiers using a linear time algorithm.
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:851
MDNode * createFunctionEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports)
Return metadata containing the entry Count for a function, and the GUIDs stored in Imports that need ...
Definition: MDBuilder.cpp:60
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:446
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
This file contains the declarations for metadata subclasses.
unsigned second
Metadata node.
Definition: Metadata.h:862
F(f)
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:1488
MDNode * createIrrLoopHeaderWeight(uint64_t Weight)
Return metadata containing an irreducible loop header weight.
Definition: MDBuilder.cpp:201
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:164
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1176
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:408
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
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
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:144
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:154
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:181
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:864
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:560
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
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:99
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:167
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:148
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:190
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:134
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
MDNode * createFunctionSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a function.
Definition: MDBuilder.cpp:77
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:128
A single uniqued string.
Definition: Metadata.h:602
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
IntegerType * Int32Ty