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 
20 MDString *MDBuilder::createString(StringRef Str) {
21   return MDString::get(Context, Str);
22 }
23 
24 ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
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 
37 MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
38                                        uint32_t FalseWeight) {
39   return createBranchWeights({TrueWeight, FalseWeight});
40 }
41 
42 MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
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 
55 MDNode *MDBuilder::createUnpredictable() {
56   return MDNode::get(Context, None);
57 }
58 
59 MDNode *MDBuilder::createFunctionEntryCount(
60     uint64_t Count, const DenseSet<GlobalValue::GUID> *Imports) {
61   Type *Int64Ty = Type::getInt64Ty(Context);
62   SmallVector<Metadata *, 8> Ops;
63   Ops.push_back(createString("function_entry_count"));
64   Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
65   if (Imports) {
66     SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
67     std::stable_sort(OrderID.begin(), OrderID.end(),
68       [] (GlobalValue::GUID A, GlobalValue::GUID B) {
69         return A < B;});
70     for (auto ID : OrderID)
71       Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
72   }
73   return MDNode::get(Context, Ops);
74 }
75 
76 MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
77   return MDNode::get(Context,
78                      {createString("function_section_prefix"),
79                       createString(Prefix)});
80 }
81 
82 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
83   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
84 
85   Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
86   return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));
87 }
88 
89 MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {
90   // If the range is everything then it is useless.
91   if (Hi == Lo)
92     return nullptr;
93 
94   // Return the range [Lo, Hi).
95   return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});
96 }
97 
98 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
99   // To ensure uniqueness the root node is self-referential.
100   auto Dummy = MDNode::getTemporary(Context, None);
101 
102   SmallVector<Metadata *, 3> Args(1, Dummy.get());
103   if (Extra)
104     Args.push_back(Extra);
105   if (!Name.empty())
106     Args.push_back(createString(Name));
107   MDNode *Root = MDNode::get(Context, Args);
108 
109   // At this point we have
110   //   !0 = metadata !{}            <- dummy
111   //   !1 = metadata !{metadata !0} <- root
112   // Replace the dummy operand with the root node itself and delete the dummy.
113   Root->replaceOperandWith(0, Root);
114 
115   // We now have
116   //   !1 = metadata !{metadata !1} <- self-referential root
117   return Root;
118 }
119 
120 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
121   return MDNode::get(Context, createString(Name));
122 }
123 
124 /// \brief Return metadata for a non-root TBAA node with the given name,
125 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
126 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
127                                   bool isConstant) {
128   if (isConstant) {
129     Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
130     return MDNode::get(Context,
131                        {createString(Name), Parent, createConstant(Flags)});
132   }
133   return MDNode::get(Context, {createString(Name), Parent});
134 }
135 
136 MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
137   return MDNode::get(Context, createString(Name));
138 }
139 
140 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
141   return MDNode::get(Context, {createString(Name), Domain});
142 }
143 
144 /// \brief Return metadata for a tbaa.struct node with the given
145 /// struct field descriptions.
146 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
147   SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
148   Type *Int64 = Type::getInt64Ty(Context);
149   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
150     Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
151     Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
152     Vals[i * 3 + 2] = Fields[i].TBAA;
153   }
154   return MDNode::get(Context, Vals);
155 }
156 
157 /// \brief Return metadata for a TBAA struct node in the type DAG
158 /// with the given name, a list of pairs (offset, field type in the type DAG).
159 MDNode *MDBuilder::createTBAAStructTypeNode(
160     StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
161   SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
162   Type *Int64 = Type::getInt64Ty(Context);
163   Ops[0] = createString(Name);
164   for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
165     Ops[i * 2 + 1] = Fields[i].first;
166     Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
167   }
168   return MDNode::get(Context, Ops);
169 }
170 
171 /// \brief Return metadata for a TBAA scalar type node with the
172 /// given name, an offset and a parent in the TBAA type DAG.
173 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
174                                             uint64_t Offset) {
175   ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
176   return MDNode::get(Context,
177                      {createString(Name), Parent, createConstant(Off)});
178 }
179 
180 /// \brief Return metadata for a TBAA tag node with the given
181 /// base type, access type and offset relative to the base type.
182 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
183                                            uint64_t Offset, bool IsConstant) {
184   IntegerType *Int64 = Type::getInt64Ty(Context);
185   ConstantInt *Off = ConstantInt::get(Int64, Offset);
186   if (IsConstant) {
187     return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),
188                                  createConstant(ConstantInt::get(Int64, 1))});
189   }
190   return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});
191 }
192