comparison lib/IR/MDBuilder.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===// 1 //===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file defines the MDBuilder class, which is used as a convenient way to 9 // This file defines the MDBuilder class, which is used as a convenient way to
11 // create LLVM metadata with a consistent and simplified interface. 10 // create LLVM metadata with a consistent and simplified interface.
67 else 66 else
68 Ops.push_back(createString("function_entry_count")); 67 Ops.push_back(createString("function_entry_count"));
69 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count))); 68 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
70 if (Imports) { 69 if (Imports) {
71 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end()); 70 SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());
72 std::stable_sort(OrderID.begin(), OrderID.end(), 71 llvm::stable_sort(OrderID);
73 [] (GlobalValue::GUID A, GlobalValue::GUID B) {
74 return A < B;});
75 for (auto ID : OrderID) 72 for (auto ID : OrderID)
76 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID))); 73 Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));
77 } 74 }
78 return MDNode::get(Context, Ops); 75 return MDNode::get(Context, Ops);
79 } 76 }
102 99
103 MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) { 100 MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {
104 SmallVector<Metadata *, 4> Ops; 101 SmallVector<Metadata *, 4> Ops;
105 for (Function *F : Callees) 102 for (Function *F : Callees)
106 Ops.push_back(createConstant(F)); 103 Ops.push_back(createConstant(F));
104 return MDNode::get(Context, Ops);
105 }
106
107 MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,
108 ArrayRef<int> Arguments,
109 bool VarArgArePassed) {
110 SmallVector<Metadata *, 4> Ops;
111
112 Type *Int64 = Type::getInt64Ty(Context);
113 Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));
114
115 for (int ArgNo : Arguments)
116 Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));
117
118 Type *Int1 = Type::getInt1Ty(Context);
119 Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));
120
121 return MDNode::get(Context, Ops);
122 }
123
124 MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
125 MDNode *NewCB) {
126 if (!ExistingCallbacks)
127 return MDNode::get(Context, {NewCB});
128
129 auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));
130 uint64_t NewCBCalleeIdx =
131 cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();
132 (void)NewCBCalleeIdx;
133
134 SmallVector<Metadata *, 4> Ops;
135 unsigned NumExistingOps = ExistingCallbacks->getNumOperands();
136 Ops.resize(NumExistingOps + 1);
137
138 for (unsigned u = 0; u < NumExistingOps; u++) {
139 Ops[u] = ExistingCallbacks->getOperand(u);
140
141 auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
142 uint64_t OldCBCalleeIdx =
143 cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
144 (void)OldCBCalleeIdx;
145 assert(NewCBCalleeIdx != OldCBCalleeIdx &&
146 "Cannot map a callback callee index twice!");
147 }
148
149 Ops[NumExistingOps] = NewCB;
107 return MDNode::get(Context, Ops); 150 return MDNode::get(Context, Ops);
108 } 151 }
109 152
110 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { 153 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
111 // To ensure uniqueness the root node is self-referential. 154 // To ensure uniqueness the root node is self-referential.
131 174
132 MDNode *MDBuilder::createTBAARoot(StringRef Name) { 175 MDNode *MDBuilder::createTBAARoot(StringRef Name) {
133 return MDNode::get(Context, createString(Name)); 176 return MDNode::get(Context, createString(Name));
134 } 177 }
135 178
136 /// \brief Return metadata for a non-root TBAA node with the given name, 179 /// Return metadata for a non-root TBAA node with the given name,
137 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'. 180 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
138 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, 181 MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
139 bool isConstant) { 182 bool isConstant) {
140 if (isConstant) { 183 if (isConstant) {
141 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); 184 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
151 194
152 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { 195 MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
153 return MDNode::get(Context, {createString(Name), Domain}); 196 return MDNode::get(Context, {createString(Name), Domain});
154 } 197 }
155 198
156 /// \brief Return metadata for a tbaa.struct node with the given 199 /// Return metadata for a tbaa.struct node with the given
157 /// struct field descriptions. 200 /// struct field descriptions.
158 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { 201 MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
159 SmallVector<Metadata *, 4> Vals(Fields.size() * 3); 202 SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
160 Type *Int64 = Type::getInt64Ty(Context); 203 Type *Int64 = Type::getInt64Ty(Context);
161 for (unsigned i = 0, e = Fields.size(); i != e; ++i) { 204 for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
164 Vals[i * 3 + 2] = Fields[i].Type; 207 Vals[i * 3 + 2] = Fields[i].Type;
165 } 208 }
166 return MDNode::get(Context, Vals); 209 return MDNode::get(Context, Vals);
167 } 210 }
168 211
169 /// \brief Return metadata for a TBAA struct node in the type DAG 212 /// 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). 213 /// with the given name, a list of pairs (offset, field type in the type DAG).
171 MDNode *MDBuilder::createTBAAStructTypeNode( 214 MDNode *MDBuilder::createTBAAStructTypeNode(
172 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { 215 StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
173 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1); 216 SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
174 Type *Int64 = Type::getInt64Ty(Context); 217 Type *Int64 = Type::getInt64Ty(Context);
178 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); 221 Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
179 } 222 }
180 return MDNode::get(Context, Ops); 223 return MDNode::get(Context, Ops);
181 } 224 }
182 225
183 /// \brief Return metadata for a TBAA scalar type node with the 226 /// Return metadata for a TBAA scalar type node with the
184 /// given name, an offset and a parent in the TBAA type DAG. 227 /// given name, an offset and a parent in the TBAA type DAG.
185 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, 228 MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
186 uint64_t Offset) { 229 uint64_t Offset) {
187 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); 230 ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
188 return MDNode::get(Context, 231 return MDNode::get(Context,
189 {createString(Name), Parent, createConstant(Off)}); 232 {createString(Name), Parent, createConstant(Off)});
190 } 233 }
191 234
192 /// \brief Return metadata for a TBAA tag node with the given 235 /// Return metadata for a TBAA tag node with the given
193 /// base type, access type and offset relative to the base type. 236 /// base type, access type and offset relative to the base type.
194 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, 237 MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
195 uint64_t Offset, bool IsConstant) { 238 uint64_t Offset, bool IsConstant) {
196 IntegerType *Int64 = Type::getInt64Ty(Context); 239 IntegerType *Int64 = Type::getInt64Ty(Context);
197 ConstantInt *Off = ConstantInt::get(Int64, Offset); 240 ConstantInt *Off = ConstantInt::get(Int64, Offset);
258 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue(); 301 uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();
259 return createTBAAAccessTag(BaseType, AccessType, Offset, Size); 302 return createTBAAAccessTag(BaseType, AccessType, Offset, Size);
260 } 303 }
261 304
262 MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) { 305 MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
263 SmallVector<Metadata *, 2> Vals(2); 306 Metadata *Vals[] = {
264 Vals[0] = createString("loop_header_weight"); 307 createString("loop_header_weight"),
265 Vals[1] = createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)); 308 createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
309 };
266 return MDNode::get(Context, Vals); 310 return MDNode::get(Context, Vals);
267 } 311 }