Mercurial > hg > CbC > CbC_llvm
comparison lib/Bitcode/Writer/ValueEnumerator.cpp @ 120:1172e4bd9c6f
update 4.0.0
author | mir3636 |
---|---|
date | Fri, 25 Nov 2016 19:14:25 +0900 |
parents | 7d135dc70f03 |
children | 803732b1fca8 |
comparison
equal
deleted
inserted
replaced
101:34baf5011add | 120:1172e4bd9c6f |
---|---|
84 if (!isa<GlobalValue>(G.getInitializer())) | 84 if (!isa<GlobalValue>(G.getInitializer())) |
85 orderValue(G.getInitializer(), OM); | 85 orderValue(G.getInitializer(), OM); |
86 for (const GlobalAlias &A : M.aliases()) | 86 for (const GlobalAlias &A : M.aliases()) |
87 if (!isa<GlobalValue>(A.getAliasee())) | 87 if (!isa<GlobalValue>(A.getAliasee())) |
88 orderValue(A.getAliasee(), OM); | 88 orderValue(A.getAliasee(), OM); |
89 for (const GlobalIFunc &I : M.ifuncs()) | |
90 if (!isa<GlobalValue>(I.getResolver())) | |
91 orderValue(I.getResolver(), OM); | |
89 for (const Function &F : M) { | 92 for (const Function &F : M) { |
90 for (const Use &U : F.operands()) | 93 for (const Use &U : F.operands()) |
91 if (!isa<GlobalValue>(U.get())) | 94 if (!isa<GlobalValue>(U.get())) |
92 orderValue(U.get(), OM); | 95 orderValue(U.get(), OM); |
93 } | 96 } |
103 // uses in their initializers. | 106 // uses in their initializers. |
104 for (const Function &F : M) | 107 for (const Function &F : M) |
105 orderValue(&F, OM); | 108 orderValue(&F, OM); |
106 for (const GlobalAlias &A : M.aliases()) | 109 for (const GlobalAlias &A : M.aliases()) |
107 orderValue(&A, OM); | 110 orderValue(&A, OM); |
111 for (const GlobalIFunc &I : M.ifuncs()) | |
112 orderValue(&I, OM); | |
108 for (const GlobalVariable &G : M.globals()) | 113 for (const GlobalVariable &G : M.globals()) |
109 orderValue(&G, OM); | 114 orderValue(&G, OM); |
110 OM.LastGlobalValueID = OM.size(); | 115 OM.LastGlobalValueID = OM.size(); |
111 | 116 |
112 for (const Function &F : M) { | 117 for (const Function &F : M) { |
259 predictValueUseListOrder(&G, nullptr, OM, Stack); | 264 predictValueUseListOrder(&G, nullptr, OM, Stack); |
260 for (const Function &F : M) | 265 for (const Function &F : M) |
261 predictValueUseListOrder(&F, nullptr, OM, Stack); | 266 predictValueUseListOrder(&F, nullptr, OM, Stack); |
262 for (const GlobalAlias &A : M.aliases()) | 267 for (const GlobalAlias &A : M.aliases()) |
263 predictValueUseListOrder(&A, nullptr, OM, Stack); | 268 predictValueUseListOrder(&A, nullptr, OM, Stack); |
269 for (const GlobalIFunc &I : M.ifuncs()) | |
270 predictValueUseListOrder(&I, nullptr, OM, Stack); | |
264 for (const GlobalVariable &G : M.globals()) | 271 for (const GlobalVariable &G : M.globals()) |
265 if (G.hasInitializer()) | 272 if (G.hasInitializer()) |
266 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); | 273 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); |
267 for (const GlobalAlias &A : M.aliases()) | 274 for (const GlobalAlias &A : M.aliases()) |
268 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); | 275 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); |
276 for (const GlobalIFunc &I : M.ifuncs()) | |
277 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack); | |
269 for (const Function &F : M) { | 278 for (const Function &F : M) { |
270 for (const Use &U : F.operands()) | 279 for (const Use &U : F.operands()) |
271 predictValueUseListOrder(U.get(), nullptr, OM, Stack); | 280 predictValueUseListOrder(U.get(), nullptr, OM, Stack); |
272 } | 281 } |
273 | 282 |
278 return V.first->getType()->isIntOrIntVectorTy(); | 287 return V.first->getType()->isIntOrIntVectorTy(); |
279 } | 288 } |
280 | 289 |
281 ValueEnumerator::ValueEnumerator(const Module &M, | 290 ValueEnumerator::ValueEnumerator(const Module &M, |
282 bool ShouldPreserveUseListOrder) | 291 bool ShouldPreserveUseListOrder) |
283 : HasMDString(false), HasDILocation(false), HasGenericDINode(false), | 292 : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { |
284 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { | |
285 if (ShouldPreserveUseListOrder) | 293 if (ShouldPreserveUseListOrder) |
286 UseListOrders = predictUseListOrder(M); | 294 UseListOrders = predictUseListOrder(M); |
287 | 295 |
288 // Enumerate the global variables. | 296 // Enumerate the global variables. |
289 for (const GlobalVariable &GV : M.globals()) | 297 for (const GlobalVariable &GV : M.globals()) |
297 | 305 |
298 // Enumerate the aliases. | 306 // Enumerate the aliases. |
299 for (const GlobalAlias &GA : M.aliases()) | 307 for (const GlobalAlias &GA : M.aliases()) |
300 EnumerateValue(&GA); | 308 EnumerateValue(&GA); |
301 | 309 |
310 // Enumerate the ifuncs. | |
311 for (const GlobalIFunc &GIF : M.ifuncs()) | |
312 EnumerateValue(&GIF); | |
313 | |
302 // Remember what is the cutoff between globalvalue's and other constants. | 314 // Remember what is the cutoff between globalvalue's and other constants. |
303 unsigned FirstConstant = Values.size(); | 315 unsigned FirstConstant = Values.size(); |
304 | 316 |
305 // Enumerate the global variable initializers. | 317 // Enumerate the global variable initializers. |
306 for (const GlobalVariable &GV : M.globals()) | 318 for (const GlobalVariable &GV : M.globals()) |
308 EnumerateValue(GV.getInitializer()); | 320 EnumerateValue(GV.getInitializer()); |
309 | 321 |
310 // Enumerate the aliasees. | 322 // Enumerate the aliasees. |
311 for (const GlobalAlias &GA : M.aliases()) | 323 for (const GlobalAlias &GA : M.aliases()) |
312 EnumerateValue(GA.getAliasee()); | 324 EnumerateValue(GA.getAliasee()); |
325 | |
326 // Enumerate the ifunc resolvers. | |
327 for (const GlobalIFunc &GIF : M.ifuncs()) | |
328 EnumerateValue(GIF.getResolver()); | |
313 | 329 |
314 // Enumerate any optional Function data. | 330 // Enumerate any optional Function data. |
315 for (const Function &F : M) | 331 for (const Function &F : M) |
316 for (const Use &U : F.operands()) | 332 for (const Use &U : F.operands()) |
317 EnumerateValue(U.get()); | 333 EnumerateValue(U.get()); |
326 // pool so that the module symbol table can refer to them... | 342 // pool so that the module symbol table can refer to them... |
327 EnumerateValueSymbolTable(M.getValueSymbolTable()); | 343 EnumerateValueSymbolTable(M.getValueSymbolTable()); |
328 EnumerateNamedMetadata(M); | 344 EnumerateNamedMetadata(M); |
329 | 345 |
330 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; | 346 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; |
347 for (const GlobalVariable &GV : M.globals()) { | |
348 MDs.clear(); | |
349 GV.getAllMetadata(MDs); | |
350 for (const auto &I : MDs) | |
351 // FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer | |
352 // to write metadata to the global variable's own metadata block | |
353 // (PR28134). | |
354 EnumerateMetadata(nullptr, I.second); | |
355 } | |
331 | 356 |
332 // Enumerate types used by function bodies and argument lists. | 357 // Enumerate types used by function bodies and argument lists. |
333 for (const Function &F : M) { | 358 for (const Function &F : M) { |
334 for (const Argument &A : F.args()) | 359 for (const Argument &A : F.args()) |
335 EnumerateType(A.getType()); | 360 EnumerateType(A.getType()); |
336 | 361 |
337 // Enumerate metadata attached to this function. | 362 // Enumerate metadata attached to this function. |
363 MDs.clear(); | |
338 F.getAllMetadata(MDs); | 364 F.getAllMetadata(MDs); |
339 for (const auto &I : MDs) | 365 for (const auto &I : MDs) |
340 EnumerateMetadata(I.second); | 366 EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second); |
341 | 367 |
342 for (const BasicBlock &BB : F) | 368 for (const BasicBlock &BB : F) |
343 for (const Instruction &I : BB) { | 369 for (const Instruction &I : BB) { |
344 for (const Use &Op : I.operands()) { | 370 for (const Use &Op : I.operands()) { |
345 auto *MD = dyn_cast<MetadataAsValue>(&Op); | 371 auto *MD = dyn_cast<MetadataAsValue>(&Op); |
350 | 376 |
351 // Local metadata is enumerated during function-incorporation. | 377 // Local metadata is enumerated during function-incorporation. |
352 if (isa<LocalAsMetadata>(MD->getMetadata())) | 378 if (isa<LocalAsMetadata>(MD->getMetadata())) |
353 continue; | 379 continue; |
354 | 380 |
355 EnumerateMetadata(MD->getMetadata()); | 381 EnumerateMetadata(&F, MD->getMetadata()); |
356 } | 382 } |
357 EnumerateType(I.getType()); | 383 EnumerateType(I.getType()); |
358 if (const CallInst *CI = dyn_cast<CallInst>(&I)) | 384 if (const CallInst *CI = dyn_cast<CallInst>(&I)) |
359 EnumerateAttributes(CI->getAttributes()); | 385 EnumerateAttributes(CI->getAttributes()); |
360 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) | 386 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) |
362 | 388 |
363 // Enumerate metadata attached with this instruction. | 389 // Enumerate metadata attached with this instruction. |
364 MDs.clear(); | 390 MDs.clear(); |
365 I.getAllMetadataOtherThanDebugLoc(MDs); | 391 I.getAllMetadataOtherThanDebugLoc(MDs); |
366 for (unsigned i = 0, e = MDs.size(); i != e; ++i) | 392 for (unsigned i = 0, e = MDs.size(); i != e; ++i) |
367 EnumerateMetadata(MDs[i].second); | 393 EnumerateMetadata(&F, MDs[i].second); |
368 | 394 |
369 // Don't enumerate the location directly -- it has a special record | 395 // Don't enumerate the location directly -- it has a special record |
370 // type -- but enumerate its operands. | 396 // type -- but enumerate its operands. |
371 if (DILocation *L = I.getDebugLoc()) | 397 if (DILocation *L = I.getDebugLoc()) |
372 EnumerateMDNodeOperands(L); | 398 for (const Metadata *Op : L->operands()) |
399 EnumerateMetadata(&F, Op); | |
373 } | 400 } |
374 } | 401 } |
375 | 402 |
376 // Optimize constant ordering. | 403 // Optimize constant ordering. |
377 OptimizeConstants(FirstConstant, Values.size()); | 404 OptimizeConstants(FirstConstant, Values.size()); |
405 | |
406 // Organize metadata ordering. | |
407 organizeMetadata(); | |
378 } | 408 } |
379 | 409 |
380 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { | 410 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const { |
381 InstructionMapType::const_iterator I = InstructionMap.find(Inst); | 411 InstructionMapType::const_iterator I = InstructionMap.find(Inst); |
382 assert(I != InstructionMap.end() && "Instruction is not mapped!"); | 412 assert(I != InstructionMap.end() && "Instruction is not mapped!"); |
400 ValueMapType::const_iterator I = ValueMap.find(V); | 430 ValueMapType::const_iterator I = ValueMap.find(V); |
401 assert(I != ValueMap.end() && "Value not in slotcalculator!"); | 431 assert(I != ValueMap.end() && "Value not in slotcalculator!"); |
402 return I->second-1; | 432 return I->second-1; |
403 } | 433 } |
404 | 434 |
405 void ValueEnumerator::dump() const { | 435 LLVM_DUMP_METHOD void ValueEnumerator::dump() const { |
406 print(dbgs(), ValueMap, "Default"); | 436 print(dbgs(), ValueMap, "Default"); |
407 dbgs() << '\n'; | 437 dbgs() << '\n'; |
408 print(dbgs(), MetadataMap, "MetaData"); | 438 print(dbgs(), MetadataMap, "MetaData"); |
409 dbgs() << '\n'; | 439 dbgs() << '\n'; |
410 } | 440 } |
443 | 473 |
444 OS << "Map Name: " << Name << "\n"; | 474 OS << "Map Name: " << Name << "\n"; |
445 OS << "Size: " << Map.size() << "\n"; | 475 OS << "Size: " << Map.size() << "\n"; |
446 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) { | 476 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) { |
447 const Metadata *MD = I->first; | 477 const Metadata *MD = I->first; |
448 OS << "Metadata: slot = " << I->second << "\n"; | 478 OS << "Metadata: slot = " << I->second.ID << "\n"; |
479 OS << "Metadata: function = " << I->second.F << "\n"; | |
449 MD->print(OS); | 480 MD->print(OS); |
481 OS << "\n"; | |
450 } | 482 } |
451 } | 483 } |
452 | 484 |
453 /// OptimizeConstants - Reorder constant pool for denser encoding. | 485 /// OptimizeConstants - Reorder constant pool for denser encoding. |
454 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { | 486 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { |
470 }); | 502 }); |
471 | 503 |
472 // Ensure that integer and vector of integer constants are at the start of the | 504 // Ensure that integer and vector of integer constants are at the start of the |
473 // constant pool. This is important so that GEP structure indices come before | 505 // constant pool. This is important so that GEP structure indices come before |
474 // gep constant exprs. | 506 // gep constant exprs. |
475 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd, | 507 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd, |
476 isIntOrIntVectorValue); | 508 isIntOrIntVectorValue); |
477 | 509 |
478 // Rebuild the modified portion of ValueMap. | 510 // Rebuild the modified portion of ValueMap. |
479 for (; CstStart != CstEnd; ++CstStart) | 511 for (; CstStart != CstEnd; ++CstStart) |
480 ValueMap[Values[CstStart].first] = CstStart+1; | 512 ValueMap[Values[CstStart].first] = CstStart+1; |
481 } | 513 } |
496 EnumerateNamedMDNode(&I); | 528 EnumerateNamedMDNode(&I); |
497 } | 529 } |
498 | 530 |
499 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { | 531 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { |
500 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) | 532 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) |
501 EnumerateMetadata(MD->getOperand(i)); | 533 EnumerateMetadata(nullptr, MD->getOperand(i)); |
502 } | 534 } |
503 | 535 |
504 /// EnumerateMDNodeOperands - Enumerate all non-function-local values | 536 unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const { |
505 /// and types referenced by the given MDNode. | 537 return F ? getValueID(F) + 1 : 0; |
506 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { | 538 } |
507 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { | 539 |
508 Metadata *MD = N->getOperand(i); | 540 void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) { |
509 if (!MD) | 541 EnumerateMetadata(getMetadataFunctionID(F), MD); |
542 } | |
543 | |
544 void ValueEnumerator::EnumerateFunctionLocalMetadata( | |
545 const Function &F, const LocalAsMetadata *Local) { | |
546 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local); | |
547 } | |
548 | |
549 void ValueEnumerator::dropFunctionFromMetadata( | |
550 MetadataMapType::value_type &FirstMD) { | |
551 SmallVector<const MDNode *, 64> Worklist; | |
552 auto push = [this, &Worklist](MetadataMapType::value_type &MD) { | |
553 auto &Entry = MD.second; | |
554 | |
555 // Nothing to do if this metadata isn't tagged. | |
556 if (!Entry.F) | |
557 return; | |
558 | |
559 // Drop the function tag. | |
560 Entry.F = 0; | |
561 | |
562 // If this is has an ID and is an MDNode, then its operands have entries as | |
563 // well. We need to drop the function from them too. | |
564 if (Entry.ID) | |
565 if (auto *N = dyn_cast<MDNode>(MD.first)) | |
566 Worklist.push_back(N); | |
567 }; | |
568 push(FirstMD); | |
569 while (!Worklist.empty()) | |
570 for (const Metadata *Op : Worklist.pop_back_val()->operands()) { | |
571 if (!Op) | |
572 continue; | |
573 auto MD = MetadataMap.find(Op); | |
574 if (MD != MetadataMap.end()) | |
575 push(*MD); | |
576 } | |
577 } | |
578 | |
579 void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) { | |
580 // It's vital for reader efficiency that uniqued subgraphs are done in | |
581 // post-order; it's expensive when their operands have forward references. | |
582 // If a distinct node is referenced from a uniqued node, it'll be delayed | |
583 // until the uniqued subgraph has been completely traversed. | |
584 SmallVector<const MDNode *, 32> DelayedDistinctNodes; | |
585 | |
586 // Start by enumerating MD, and then work through its transitive operands in | |
587 // post-order. This requires a depth-first search. | |
588 SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist; | |
589 if (const MDNode *N = enumerateMetadataImpl(F, MD)) | |
590 Worklist.push_back(std::make_pair(N, N->op_begin())); | |
591 | |
592 while (!Worklist.empty()) { | |
593 const MDNode *N = Worklist.back().first; | |
594 | |
595 // Enumerate operands until we hit a new node. We need to traverse these | |
596 // nodes' operands before visiting the rest of N's operands. | |
597 MDNode::op_iterator I = std::find_if( | |
598 Worklist.back().second, N->op_end(), | |
599 [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); }); | |
600 if (I != N->op_end()) { | |
601 auto *Op = cast<MDNode>(*I); | |
602 Worklist.back().second = ++I; | |
603 | |
604 // Delay traversing Op if it's a distinct node and N is uniqued. | |
605 if (Op->isDistinct() && !N->isDistinct()) | |
606 DelayedDistinctNodes.push_back(Op); | |
607 else | |
608 Worklist.push_back(std::make_pair(Op, Op->op_begin())); | |
510 continue; | 609 continue; |
511 assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local"); | 610 } |
512 EnumerateMetadata(MD); | 611 |
513 } | 612 // All the operands have been visited. Now assign an ID. |
514 } | 613 Worklist.pop_back(); |
515 | 614 MDs.push_back(N); |
516 void ValueEnumerator::EnumerateMetadata(const Metadata *MD) { | 615 MetadataMap[N].ID = MDs.size(); |
616 | |
617 // Flush out any delayed distinct nodes; these are all the distinct nodes | |
618 // that are leaves in last uniqued subgraph. | |
619 if (Worklist.empty() || Worklist.back().first->isDistinct()) { | |
620 for (const MDNode *N : DelayedDistinctNodes) | |
621 Worklist.push_back(std::make_pair(N, N->op_begin())); | |
622 DelayedDistinctNodes.clear(); | |
623 } | |
624 } | |
625 } | |
626 | |
627 const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F, const Metadata *MD) { | |
628 if (!MD) | |
629 return nullptr; | |
630 | |
517 assert( | 631 assert( |
518 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) && | 632 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) && |
519 "Invalid metadata kind"); | 633 "Invalid metadata kind"); |
520 | 634 |
521 // Insert a dummy ID to block the co-recursive call to | 635 auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F))); |
522 // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph. | 636 MDIndex &Entry = Insertion.first->second; |
523 // | 637 if (!Insertion.second) { |
524 // Return early if there's already an ID. | 638 // Already mapped. If F doesn't match the function tag, drop it. |
525 if (!MetadataMap.insert(std::make_pair(MD, 0)).second) | 639 if (Entry.hasDifferentFunction(F)) |
526 return; | 640 dropFunctionFromMetadata(*Insertion.first); |
527 | 641 return nullptr; |
528 // Visit operands first to minimize RAUW. | 642 } |
643 | |
644 // Don't assign IDs to metadata nodes. | |
529 if (auto *N = dyn_cast<MDNode>(MD)) | 645 if (auto *N = dyn_cast<MDNode>(MD)) |
530 EnumerateMDNodeOperands(N); | 646 return N; |
531 else if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) | 647 |
648 // Save the metadata. | |
649 MDs.push_back(MD); | |
650 Entry.ID = MDs.size(); | |
651 | |
652 // Enumerate the constant, if any. | |
653 if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) | |
532 EnumerateValue(C->getValue()); | 654 EnumerateValue(C->getValue()); |
533 | 655 |
534 HasMDString |= isa<MDString>(MD); | 656 return nullptr; |
535 HasDILocation |= isa<DILocation>(MD); | |
536 HasGenericDINode |= isa<GenericDINode>(MD); | |
537 | |
538 // Replace the dummy ID inserted above with the correct one. MetadataMap may | |
539 // have changed by inserting operands, so we need a fresh lookup here. | |
540 MDs.push_back(MD); | |
541 MetadataMap[MD] = MDs.size(); | |
542 } | 657 } |
543 | 658 |
544 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata | 659 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata |
545 /// information reachable from the metadata. | 660 /// information reachable from the metadata. |
546 void ValueEnumerator::EnumerateFunctionLocalMetadata( | 661 void ValueEnumerator::EnumerateFunctionLocalMetadata( |
547 const LocalAsMetadata *Local) { | 662 unsigned F, const LocalAsMetadata *Local) { |
663 assert(F && "Expected a function"); | |
664 | |
548 // Check to see if it's already in! | 665 // Check to see if it's already in! |
549 unsigned &MetadataID = MetadataMap[Local]; | 666 MDIndex &Index = MetadataMap[Local]; |
550 if (MetadataID) | 667 if (Index.ID) { |
551 return; | 668 assert(Index.F == F && "Expected the same function"); |
669 return; | |
670 } | |
552 | 671 |
553 MDs.push_back(Local); | 672 MDs.push_back(Local); |
554 MetadataID = MDs.size(); | 673 Index.F = F; |
674 Index.ID = MDs.size(); | |
555 | 675 |
556 EnumerateValue(Local->getValue()); | 676 EnumerateValue(Local->getValue()); |
557 | 677 } |
558 // Also, collect all function-local metadata for easy access. | 678 |
559 FunctionLocalMDs.push_back(Local); | 679 static unsigned getMetadataTypeOrder(const Metadata *MD) { |
680 // Strings are emitted in bulk and must come first. | |
681 if (isa<MDString>(MD)) | |
682 return 0; | |
683 | |
684 // ConstantAsMetadata doesn't reference anything. We may as well shuffle it | |
685 // to the front since we can detect it. | |
686 auto *N = dyn_cast<MDNode>(MD); | |
687 if (!N) | |
688 return 1; | |
689 | |
690 // The reader is fast forward references for distinct node operands, but slow | |
691 // when uniqued operands are unresolved. | |
692 return N->isDistinct() ? 2 : 3; | |
693 } | |
694 | |
695 void ValueEnumerator::organizeMetadata() { | |
696 assert(MetadataMap.size() == MDs.size() && | |
697 "Metadata map and vector out of sync"); | |
698 | |
699 if (MDs.empty()) | |
700 return; | |
701 | |
702 // Copy out the index information from MetadataMap in order to choose a new | |
703 // order. | |
704 SmallVector<MDIndex, 64> Order; | |
705 Order.reserve(MetadataMap.size()); | |
706 for (const Metadata *MD : MDs) | |
707 Order.push_back(MetadataMap.lookup(MD)); | |
708 | |
709 // Partition: | |
710 // - by function, then | |
711 // - by isa<MDString> | |
712 // and then sort by the original/current ID. Since the IDs are guaranteed to | |
713 // be unique, the result of std::sort will be deterministic. There's no need | |
714 // for std::stable_sort. | |
715 std::sort(Order.begin(), Order.end(), [this](MDIndex LHS, MDIndex RHS) { | |
716 return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) < | |
717 std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID); | |
718 }); | |
719 | |
720 // Rebuild MDs, index the metadata ranges for each function in FunctionMDs, | |
721 // and fix up MetadataMap. | |
722 std::vector<const Metadata *> OldMDs = std::move(MDs); | |
723 MDs.reserve(OldMDs.size()); | |
724 for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) { | |
725 auto *MD = Order[I].get(OldMDs); | |
726 MDs.push_back(MD); | |
727 MetadataMap[MD].ID = I + 1; | |
728 if (isa<MDString>(MD)) | |
729 ++NumMDStrings; | |
730 } | |
731 | |
732 // Return early if there's nothing for the functions. | |
733 if (MDs.size() == Order.size()) | |
734 return; | |
735 | |
736 // Build the function metadata ranges. | |
737 MDRange R; | |
738 FunctionMDs.reserve(OldMDs.size()); | |
739 unsigned PrevF = 0; | |
740 for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E; | |
741 ++I) { | |
742 unsigned F = Order[I].F; | |
743 if (!PrevF) { | |
744 PrevF = F; | |
745 } else if (PrevF != F) { | |
746 R.Last = FunctionMDs.size(); | |
747 std::swap(R, FunctionMDInfo[PrevF]); | |
748 R.First = FunctionMDs.size(); | |
749 | |
750 ID = MDs.size(); | |
751 PrevF = F; | |
752 } | |
753 | |
754 auto *MD = Order[I].get(OldMDs); | |
755 FunctionMDs.push_back(MD); | |
756 MetadataMap[MD].ID = ++ID; | |
757 if (isa<MDString>(MD)) | |
758 ++R.NumStrings; | |
759 } | |
760 R.Last = FunctionMDs.size(); | |
761 FunctionMDInfo[PrevF] = R; | |
762 } | |
763 | |
764 void ValueEnumerator::incorporateFunctionMetadata(const Function &F) { | |
765 NumModuleMDs = MDs.size(); | |
766 | |
767 auto R = FunctionMDInfo.lookup(getValueID(&F) + 1); | |
768 NumMDStrings = R.NumStrings; | |
769 MDs.insert(MDs.end(), FunctionMDs.begin() + R.First, | |
770 FunctionMDs.begin() + R.Last); | |
560 } | 771 } |
561 | 772 |
562 void ValueEnumerator::EnumerateValue(const Value *V) { | 773 void ValueEnumerator::EnumerateValue(const Value *V) { |
563 assert(!V->getType()->isVoidTy() && "Can't insert void values!"); | 774 assert(!V->getType()->isVoidTy() && "Can't insert void values!"); |
564 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!"); | 775 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!"); |
648 // Enumerate the types for the specified value. If the value is a constant, | 859 // Enumerate the types for the specified value. If the value is a constant, |
649 // walk through it, enumerating the types of the constant. | 860 // walk through it, enumerating the types of the constant. |
650 void ValueEnumerator::EnumerateOperandType(const Value *V) { | 861 void ValueEnumerator::EnumerateOperandType(const Value *V) { |
651 EnumerateType(V->getType()); | 862 EnumerateType(V->getType()); |
652 | 863 |
653 if (auto *MD = dyn_cast<MetadataAsValue>(V)) { | 864 assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand"); |
654 assert(!isa<LocalAsMetadata>(MD->getMetadata()) && | |
655 "Function-local metadata should be left for later"); | |
656 | |
657 EnumerateMetadata(MD->getMetadata()); | |
658 return; | |
659 } | |
660 | 865 |
661 const Constant *C = dyn_cast<Constant>(V); | 866 const Constant *C = dyn_cast<Constant>(V); |
662 if (!C) | 867 if (!C) |
663 return; | 868 return; |
664 | 869 |
702 } | 907 } |
703 | 908 |
704 void ValueEnumerator::incorporateFunction(const Function &F) { | 909 void ValueEnumerator::incorporateFunction(const Function &F) { |
705 InstructionCount = 0; | 910 InstructionCount = 0; |
706 NumModuleValues = Values.size(); | 911 NumModuleValues = Values.size(); |
707 NumModuleMDs = MDs.size(); | 912 |
913 // Add global metadata to the function block. This doesn't include | |
914 // LocalAsMetadata. | |
915 incorporateFunctionMetadata(F); | |
708 | 916 |
709 // Adding function arguments to the value table. | 917 // Adding function arguments to the value table. |
710 for (const auto &I : F.args()) | 918 for (const auto &I : F.args()) |
711 EnumerateValue(&I); | 919 EnumerateValue(&I); |
712 | 920 |
747 EnumerateValue(&I); | 955 EnumerateValue(&I); |
748 } | 956 } |
749 } | 957 } |
750 | 958 |
751 // Add all of the function-local metadata. | 959 // Add all of the function-local metadata. |
752 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) | 960 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) { |
753 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]); | 961 // At this point, every local values have been incorporated, we shouldn't |
962 // have a metadata operand that references a value that hasn't been seen. | |
963 assert(ValueMap.count(FnLocalMDVector[i]->getValue()) && | |
964 "Missing value for metadata operand"); | |
965 EnumerateFunctionLocalMetadata(F, FnLocalMDVector[i]); | |
966 } | |
754 } | 967 } |
755 | 968 |
756 void ValueEnumerator::purgeFunction() { | 969 void ValueEnumerator::purgeFunction() { |
757 /// Remove purged values from the ValueMap. | 970 /// Remove purged values from the ValueMap. |
758 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) | 971 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) |
763 ValueMap.erase(BasicBlocks[i]); | 976 ValueMap.erase(BasicBlocks[i]); |
764 | 977 |
765 Values.resize(NumModuleValues); | 978 Values.resize(NumModuleValues); |
766 MDs.resize(NumModuleMDs); | 979 MDs.resize(NumModuleMDs); |
767 BasicBlocks.clear(); | 980 BasicBlocks.clear(); |
768 FunctionLocalMDs.clear(); | 981 NumMDStrings = 0; |
769 } | 982 } |
770 | 983 |
771 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, | 984 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, |
772 DenseMap<const BasicBlock*, unsigned> &IDMap) { | 985 DenseMap<const BasicBlock*, unsigned> &IDMap) { |
773 unsigned Counter = 0; | 986 unsigned Counter = 0; |