llvm.org GIT mirror llvm / 309b3af
Extend function-local metadata to be usable as attachments. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111895 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
5 changed file(s) with 128 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
11231123 return TokError("expected metadata after comma");
11241124
11251125 std::string Name = Lex.getStrVal();
1126 unsigned MDK = M->getMDKindID(Name.c_str());
11261127 Lex.Lex();
11271128
11281129 MDNode *Node;
11291130 unsigned NodeID;
11301131 SMLoc Loc = Lex.getLoc();
1131 if (ParseToken(lltok::exclaim, "expected '!' here") ||
1132 ParseMDNodeID(Node, NodeID))
1132
1133 if (ParseToken(lltok::exclaim, "expected '!' here"))
11331134 return true;
11341135
1135 unsigned MDK = M->getMDKindID(Name.c_str());
1136 if (Node) {
1137 // If we got the node, add it to the instruction.
1138 Inst->setMetadata(MDK, Node);
1136 if (Lex.getKind() == lltok::lbrace) {
1137 ValID ID;
1138 if (ParseMetadataListValue(ID, PFS))
1139 return true;
1140 assert(ID.Kind == ValID::t_MDNode);
1141 Inst->setMetadata(MDK, ID.MDNodeVal);
11391142 } else {
1140 MDRef R = { Loc, MDK, NodeID };
1141 // Otherwise, remember that this should be resolved later.
1142 ForwardRefInstMetadata[Inst].push_back(R);
1143 if (ParseMDNodeID(Node, NodeID))
1144 return true;
1145 if (Node) {
1146 // If we got the node, add it to the instruction.
1147 Inst->setMetadata(MDK, Node);
1148 } else {
1149 MDRef R = { Loc, MDK, NodeID };
1150 // Otherwise, remember that this should be resolved later.
1151 ForwardRefInstMetadata[Inst].push_back(R);
1152 }
11431153 }
11441154
11451155 // If this is the end of the list, we're done.
25042514 return false;
25052515 }
25062516
2517 bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2518 assert(Lex.getKind() == lltok::lbrace);
2519 Lex.Lex();
2520
2521 SmallVector Elts;
2522 if (ParseMDNodeVector(Elts, PFS) ||
2523 ParseToken(lltok::rbrace, "expected end of metadata node"))
2524 return true;
2525
2526 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
2527 ID.Kind = ValID::t_MDNode;
2528 return false;
2529 }
2530
25072531 /// ParseMetadataValue
25082532 /// ::= !42
25092533 /// ::= !{...}
25142538
25152539 // MDNode:
25162540 // !{ ... }
2517 if (EatIfPresent(lltok::lbrace)) {
2518 SmallVector Elts;
2519 if (ParseMDNodeVector(Elts, PFS) ||
2520 ParseToken(lltok::rbrace, "expected end of metadata node"))
2521 return true;
2522
2523 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
2524 ID.Kind = ValID::t_MDNode;
2525 return false;
2526 }
2541 if (Lex.getKind() == lltok::lbrace)
2542 return ParseMetadataListValue(ID, PFS);
25272543
25282544 // Standalone metadata reference
25292545 // !42
306306 bool ParseGlobalValue(const Type *Ty, Constant *&V);
307307 bool ParseGlobalTypeAndValue(Constant *&V);
308308 bool ParseGlobalValueVector(SmallVectorImpl &Elts);
309 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
309310 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
310311 bool ParseMDNodeVector(SmallVectorImpl &, PerFunctionState *PFS);
311312 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
219219 EnumerateMetadata(MD->getOperand(i));
220220 }
221221
222 /// EnumerateMDNodeOperands - Enumerate all non-function-local values
223 /// and types referenced by the given MDNode.
224 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
225 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
226 if (Value *V = N->getOperand(i)) {
227 if (isa(V) || isa(V))
228 EnumerateMetadata(V);
229 else if (!isa(V) && !isa(V))
230 EnumerateValue(V);
231 } else
232 EnumerateType(Type::getVoidTy(N->getContext()));
233 }
234 }
235
222236 void ValueEnumerator::EnumerateMetadata(const Value *MD) {
223237 assert((isa(MD) || isa(MD)) && "Invalid metadata kind");
238
239 // Enumerate the type of this value.
240 EnumerateType(MD->getType());
241
242 const MDNode *N = dyn_cast(MD);
243
244 // In the module-level pass, skip function-local nodes themselves, but
245 // do walk their operands.
246 if (N && N->isFunctionLocal() && N->getFunction()) {
247 EnumerateMDNodeOperands(N);
248 return;
249 }
250
224251 // Check to see if it's already in!
225252 unsigned &MDValueID = MDValueMap[MD];
226253 if (MDValueID) {
228255 MDValues[MDValueID-1].second++;
229256 return;
230257 }
231
232 // Enumerate the type of this value.
233 EnumerateType(MD->getType());
234
235 if (const MDNode *N = dyn_cast(MD)) {
236 MDValues.push_back(std::make_pair(MD, 1U));
237 MDValueMap[MD] = MDValues.size();
238 MDValueID = MDValues.size();
239 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
240 if (Value *V = N->getOperand(i))
241 EnumerateValue(V);
242 else
243 EnumerateType(Type::getVoidTy(MD->getContext()));
244 }
245 if (N->isFunctionLocal() && N->getFunction())
246 FunctionLocalMDs.push_back(N);
247 return;
248 }
249
250 // Add the value.
251 assert(isa(MD) && "Unknown metadata kind");
252258 MDValues.push_back(std::make_pair(MD, 1U));
253259 MDValueID = MDValues.size();
260
261 // Enumerate all non-function-local operands.
262 if (N)
263 EnumerateMDNodeOperands(N);
264 }
265
266 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
267 /// information reachable from the given MDNode.
268 void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
269 assert(N->isFunctionLocal() && N->getFunction() &&
270 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
271
272 // Enumerate the type of this value.
273 EnumerateType(N->getType());
274
275 // Check to see if it's already in!
276 unsigned &MDValueID = MDValueMap[N];
277 if (MDValueID) {
278 // Increment use count.
279 MDValues[MDValueID-1].second++;
280 return;
281 }
282 MDValues.push_back(std::make_pair(N, 1U));
283 MDValueID = MDValues.size();
284
285 // To incoroporate function-local information visit all function-local
286 // MDNodes and all function-local values they reference.
287 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
288 if (Value *V = N->getOperand(i)) {
289 if (MDNode *O = dyn_cast(V))
290 if (O->isFunctionLocal() && O->getFunction())
291 EnumerateFunctionLocalMetadata(O);
292 else if (isa(V) || isa(V))
293 EnumerateValue(V);
294 }
295
296 // Also, collect all function-local MDNodes for easy access.
297 FunctionLocalMDs.push_back(N);
254298 }
255299
256300 void ValueEnumerator::EnumerateValue(const Value *V) {
257301 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
258 if (isa(V) || isa(V))
259 return EnumerateMetadata(V);
302 assert(!isa(V) && !isa(V) &&
303 "EnumerateValue doesn't handle Metadata!");
260304
261305 // Check to see if it's already in!
262306 unsigned &ValueID = ValueMap[V];
369413 void ValueEnumerator::incorporateFunction(const Function &F) {
370414 InstructionCount = 0;
371415 NumModuleValues = Values.size();
416 NumModuleMDValues = MDValues.size();
372417
373418 // Adding function arguments to the value table.
374419 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
411456 // Enumerate metadata after the instructions they might refer to.
412457 FnLocalMDVector.push_back(MD);
413458 }
459
460 SmallVector, 8> MDs;
461 I->getAllMetadataOtherThanDebugLoc(MDs);
462 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
463 MDNode *N = MDs[i].second;
464 if (N->isFunctionLocal() && N->getFunction())
465 FnLocalMDVector.push_back(N);
466 }
467
414468 if (!I->getType()->isVoidTy())
415469 EnumerateValue(I);
416470 }
418472
419473 // Add all of the function-local metadata.
420474 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
421 EnumerateOperandType(FnLocalMDVector[i]);
475 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
422476 }
423477
424478 void ValueEnumerator::purgeFunction() {
425479 /// Remove purged values from the ValueMap.
426480 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
427481 ValueMap.erase(Values[i].first);
482 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
483 MDValueMap.erase(MDValues[i].first);
428484 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
429485 ValueMap.erase(BasicBlocks[i]);
430486
431487 Values.resize(NumModuleValues);
488 MDValues.resize(NumModuleMDValues);
432489 BasicBlocks.clear();
433490 }
434491
7171 /// When a function is incorporated, this is the size of the Values list
7272 /// before incorporation.
7373 unsigned NumModuleValues;
74
75 /// When a function is incorporated, this is the size of the MDValues list
76 /// before incorporation.
77 unsigned NumModuleMDValues;
78
7479 unsigned FirstFuncConstantID;
7580 unsigned FirstInstID;
7681
131136 private:
132137 void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
133138
139 void EnumerateMDNodeOperands(const MDNode *N);
134140 void EnumerateMetadata(const Value *MD);
141 void EnumerateFunctionLocalMetadata(const MDNode *N);
135142 void EnumerateNamedMDNode(const NamedMDNode *NMD);
136143 void EnumerateValue(const Value *V);
137144 void EnumerateType(const Type *T);
0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis
11 ; PR7105
22
3 define void @foo() {
3 define void @foo(i32 %x) {
44 call void @llvm.zonk(metadata !1, i64 0, metadata !1)
5 ret void
5 store i32 0, i32* null, !whatever !0, !whatever_else !{}, !more !{metadata !"hello"}
6 store i32 0, i32* null, !whatever !{i32 %x, metadata !"hello", metadata !1, metadata !{}, metadata !2}
7 ret void, !whatever !{i32 %x}
68 }
79
810 declare void @llvm.zonk(metadata, i64, metadata) nounwind readnone