Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/IR/Metadata.cpp @ 95:afa8332a0e37
LLVM 3.8
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2015 17:48:58 +0900 (2015-10-13) |
parents | 60c9769439b8 |
children | 7d135dc70f03 |
comparison
equal
deleted
inserted
replaced
84:f3e34b893a5f | 95:afa8332a0e37 |
---|---|
254 auto &Context = V->getContext(); | 254 auto &Context = V->getContext(); |
255 auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; | 255 auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; |
256 if (!Entry) { | 256 if (!Entry) { |
257 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && | 257 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && |
258 "Expected constant or function-local value"); | 258 "Expected constant or function-local value"); |
259 assert(!V->NameAndIsUsedByMD.getInt() && | 259 assert(!V->IsUsedByMD && |
260 "Expected this to be the only metadata use"); | 260 "Expected this to be the only metadata use"); |
261 V->NameAndIsUsedByMD.setInt(true); | 261 V->IsUsedByMD = true; |
262 if (auto *C = dyn_cast<Constant>(V)) | 262 if (auto *C = dyn_cast<Constant>(V)) |
263 Entry = new ConstantAsMetadata(C); | 263 Entry = new ConstantAsMetadata(C); |
264 else | 264 else |
265 Entry = new LocalAsMetadata(V); | 265 Entry = new LocalAsMetadata(V); |
266 } | 266 } |
300 | 300 |
301 LLVMContext &Context = From->getType()->getContext(); | 301 LLVMContext &Context = From->getType()->getContext(); |
302 auto &Store = Context.pImpl->ValuesAsMetadata; | 302 auto &Store = Context.pImpl->ValuesAsMetadata; |
303 auto I = Store.find(From); | 303 auto I = Store.find(From); |
304 if (I == Store.end()) { | 304 if (I == Store.end()) { |
305 assert(!From->NameAndIsUsedByMD.getInt() && | 305 assert(!From->IsUsedByMD && |
306 "Expected From not to be used by metadata"); | 306 "Expected From not to be used by metadata"); |
307 return; | 307 return; |
308 } | 308 } |
309 | 309 |
310 // Remove old entry from the map. | 310 // Remove old entry from the map. |
311 assert(From->NameAndIsUsedByMD.getInt() && | 311 assert(From->IsUsedByMD && |
312 "Expected From to be used by metadata"); | 312 "Expected From to be used by metadata"); |
313 From->NameAndIsUsedByMD.setInt(false); | 313 From->IsUsedByMD = false; |
314 ValueAsMetadata *MD = I->second; | 314 ValueAsMetadata *MD = I->second; |
315 assert(MD && "Expected valid metadata"); | 315 assert(MD && "Expected valid metadata"); |
316 assert(MD->getValue() == From && "Expected valid mapping"); | 316 assert(MD->getValue() == From && "Expected valid mapping"); |
317 Store.erase(I); | 317 Store.erase(I); |
318 | 318 |
344 delete MD; | 344 delete MD; |
345 return; | 345 return; |
346 } | 346 } |
347 | 347 |
348 // Update MD in place (and update the map entry). | 348 // Update MD in place (and update the map entry). |
349 assert(!To->NameAndIsUsedByMD.getInt() && | 349 assert(!To->IsUsedByMD && |
350 "Expected this to be the only metadata use"); | 350 "Expected this to be the only metadata use"); |
351 To->NameAndIsUsedByMD.setInt(true); | 351 To->IsUsedByMD = true; |
352 MD->V = To; | 352 MD->V = To; |
353 Entry = MD; | 353 Entry = MD; |
354 } | 354 } |
355 | 355 |
356 //===----------------------------------------------------------------------===// | 356 //===----------------------------------------------------------------------===// |
379 | 379 |
380 //===----------------------------------------------------------------------===// | 380 //===----------------------------------------------------------------------===// |
381 // MDNode implementation. | 381 // MDNode implementation. |
382 // | 382 // |
383 | 383 |
384 // Assert that the MDNode types will not be unaligned by the objects | |
385 // prepended to them. | |
386 #define HANDLE_MDNODE_LEAF(CLASS) \ | |
387 static_assert( \ | |
388 llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment, \ | |
389 "Alignment is insufficient after objects prepended to " #CLASS); | |
390 #include "llvm/IR/Metadata.def" | |
391 | |
384 void *MDNode::operator new(size_t Size, unsigned NumOps) { | 392 void *MDNode::operator new(size_t Size, unsigned NumOps) { |
385 void *Ptr = ::operator new(Size + NumOps * sizeof(MDOperand)); | 393 size_t OpSize = NumOps * sizeof(MDOperand); |
394 // uint64_t is the most aligned type we need support (ensured by static_assert | |
395 // above) | |
396 OpSize = RoundUpToAlignment(OpSize, llvm::alignOf<uint64_t>()); | |
397 void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize; | |
386 MDOperand *O = static_cast<MDOperand *>(Ptr); | 398 MDOperand *O = static_cast<MDOperand *>(Ptr); |
387 for (MDOperand *E = O + NumOps; O != E; ++O) | 399 for (MDOperand *E = O - NumOps; O != E; --O) |
388 (void)new (O) MDOperand; | 400 (void)new (O - 1) MDOperand; |
389 return O; | 401 return Ptr; |
390 } | 402 } |
391 | 403 |
392 void MDNode::operator delete(void *Mem) { | 404 void MDNode::operator delete(void *Mem) { |
393 MDNode *N = static_cast<MDNode *>(Mem); | 405 MDNode *N = static_cast<MDNode *>(Mem); |
406 size_t OpSize = N->NumOperands * sizeof(MDOperand); | |
407 OpSize = RoundUpToAlignment(OpSize, llvm::alignOf<uint64_t>()); | |
408 | |
394 MDOperand *O = static_cast<MDOperand *>(Mem); | 409 MDOperand *O = static_cast<MDOperand *>(Mem); |
395 for (MDOperand *E = O - N->NumOperands; O != E; --O) | 410 for (MDOperand *E = O - N->NumOperands; O != E; --O) |
396 (O - 1)->~MDOperand(); | 411 (O - 1)->~MDOperand(); |
397 ::operator delete(O); | 412 ::operator delete(reinterpret_cast<char *>(Mem) - OpSize); |
398 } | 413 } |
399 | 414 |
400 MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, | 415 MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, |
401 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2) | 416 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2) |
402 : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), | 417 : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), |
444 | 459 |
445 void MDNode::makeUniqued() { | 460 void MDNode::makeUniqued() { |
446 assert(isTemporary() && "Expected this to be temporary"); | 461 assert(isTemporary() && "Expected this to be temporary"); |
447 assert(!isResolved() && "Expected this to be unresolved"); | 462 assert(!isResolved() && "Expected this to be unresolved"); |
448 | 463 |
464 // Enable uniquing callbacks. | |
465 for (auto &Op : mutable_operands()) | |
466 Op.reset(Op.get(), this); | |
467 | |
449 // Make this 'uniqued'. | 468 // Make this 'uniqued'. |
450 Storage = Uniqued; | 469 Storage = Uniqued; |
451 if (!countUnresolvedOperands()) | 470 if (!countUnresolvedOperands()) |
452 resolve(); | 471 resolve(); |
453 | 472 |
524 return true; | 543 return true; |
525 return false; | 544 return false; |
526 } | 545 } |
527 | 546 |
528 MDNode *MDNode::replaceWithPermanentImpl() { | 547 MDNode *MDNode::replaceWithPermanentImpl() { |
548 switch (getMetadataID()) { | |
549 default: | |
550 // If this type isn't uniquable, replace with a distinct node. | |
551 return replaceWithDistinctImpl(); | |
552 | |
553 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ | |
554 case CLASS##Kind: \ | |
555 break; | |
556 #include "llvm/IR/Metadata.def" | |
557 } | |
558 | |
559 // Even if this type is uniquable, self-references have to be distinct. | |
529 if (hasSelfReference(this)) | 560 if (hasSelfReference(this)) |
530 return replaceWithDistinctImpl(); | 561 return replaceWithDistinctImpl(); |
531 return replaceWithUniquedImpl(); | 562 return replaceWithUniquedImpl(); |
532 } | 563 } |
533 | 564 |
650 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node"); | 681 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node"); |
651 | 682 |
652 // Try to insert into uniquing store. | 683 // Try to insert into uniquing store. |
653 switch (getMetadataID()) { | 684 switch (getMetadataID()) { |
654 default: | 685 default: |
655 llvm_unreachable("Invalid subclass of MDNode"); | 686 llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); |
656 #define HANDLE_MDNODE_LEAF(CLASS) \ | 687 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
657 case CLASS##Kind: { \ | 688 case CLASS##Kind: { \ |
658 CLASS *SubclassThis = cast<CLASS>(this); \ | 689 CLASS *SubclassThis = cast<CLASS>(this); \ |
659 std::integral_constant<bool, HasCachedHash<CLASS>::value> \ | 690 std::integral_constant<bool, HasCachedHash<CLASS>::value> \ |
660 ShouldRecalculateHash; \ | 691 ShouldRecalculateHash; \ |
661 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ | 692 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ |
666 } | 697 } |
667 | 698 |
668 void MDNode::eraseFromStore() { | 699 void MDNode::eraseFromStore() { |
669 switch (getMetadataID()) { | 700 switch (getMetadataID()) { |
670 default: | 701 default: |
671 llvm_unreachable("Invalid subclass of MDNode"); | 702 llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); |
672 #define HANDLE_MDNODE_LEAF(CLASS) \ | 703 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
673 case CLASS##Kind: \ | 704 case CLASS##Kind: \ |
674 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \ | 705 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \ |
675 break; | 706 break; |
676 #include "llvm/IR/Metadata.def" | 707 #include "llvm/IR/Metadata.def" |
677 } | 708 } |
969 } | 1000 } |
970 | 1001 |
971 //===----------------------------------------------------------------------===// | 1002 //===----------------------------------------------------------------------===// |
972 // Instruction Metadata method implementations. | 1003 // Instruction Metadata method implementations. |
973 // | 1004 // |
1005 void MDAttachmentMap::set(unsigned ID, MDNode &MD) { | |
1006 for (auto &I : Attachments) | |
1007 if (I.first == ID) { | |
1008 I.second.reset(&MD); | |
1009 return; | |
1010 } | |
1011 Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID), | |
1012 std::make_tuple(&MD)); | |
1013 } | |
1014 | |
1015 void MDAttachmentMap::erase(unsigned ID) { | |
1016 if (empty()) | |
1017 return; | |
1018 | |
1019 // Common case is one/last value. | |
1020 if (Attachments.back().first == ID) { | |
1021 Attachments.pop_back(); | |
1022 return; | |
1023 } | |
1024 | |
1025 for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E; | |
1026 ++I) | |
1027 if (I->first == ID) { | |
1028 *I = std::move(Attachments.back()); | |
1029 Attachments.pop_back(); | |
1030 return; | |
1031 } | |
1032 } | |
1033 | |
1034 MDNode *MDAttachmentMap::lookup(unsigned ID) const { | |
1035 for (const auto &I : Attachments) | |
1036 if (I.first == ID) | |
1037 return I.second; | |
1038 return nullptr; | |
1039 } | |
1040 | |
1041 void MDAttachmentMap::getAll( | |
1042 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { | |
1043 Result.append(Attachments.begin(), Attachments.end()); | |
1044 | |
1045 // Sort the resulting array so it is stable. | |
1046 if (Result.size() > 1) | |
1047 array_pod_sort(Result.begin(), Result.end()); | |
1048 } | |
974 | 1049 |
975 void Instruction::setMetadata(StringRef Kind, MDNode *Node) { | 1050 void Instruction::setMetadata(StringRef Kind, MDNode *Node) { |
976 if (!Node && !hasMetadata()) | 1051 if (!Node && !hasMetadata()) |
977 return; | 1052 return; |
978 setMetadata(getContext().getMDKindID(Kind), Node); | 1053 setMetadata(getContext().getMDKindID(Kind), Node); |
980 | 1055 |
981 MDNode *Instruction::getMetadataImpl(StringRef Kind) const { | 1056 MDNode *Instruction::getMetadataImpl(StringRef Kind) const { |
982 return getMetadataImpl(getContext().getMDKindID(Kind)); | 1057 return getMetadataImpl(getContext().getMDKindID(Kind)); |
983 } | 1058 } |
984 | 1059 |
985 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { | 1060 void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) { |
986 SmallSet<unsigned, 5> KnownSet; | 1061 SmallSet<unsigned, 5> KnownSet; |
987 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); | 1062 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); |
988 | 1063 |
989 // Drop debug if needed | |
990 if (KnownSet.erase(LLVMContext::MD_dbg)) | |
991 DbgLoc = DebugLoc(); | |
992 | |
993 if (!hasMetadataHashEntry()) | 1064 if (!hasMetadataHashEntry()) |
994 return; // Nothing to remove! | 1065 return; // Nothing to remove! |
995 | 1066 |
996 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore = | 1067 auto &InstructionMetadata = getContext().pImpl->InstructionMetadata; |
997 getContext().pImpl->MetadataStore; | |
998 | 1068 |
999 if (KnownSet.empty()) { | 1069 if (KnownSet.empty()) { |
1000 // Just drop our entry at the store. | 1070 // Just drop our entry at the store. |
1001 MetadataStore.erase(this); | 1071 InstructionMetadata.erase(this); |
1002 setHasMetadataHashEntry(false); | 1072 setHasMetadataHashEntry(false); |
1003 return; | 1073 return; |
1004 } | 1074 } |
1005 | 1075 |
1006 LLVMContextImpl::MDMapTy &Info = MetadataStore[this]; | 1076 auto &Info = InstructionMetadata[this]; |
1007 unsigned I; | 1077 Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) { |
1008 unsigned E; | 1078 return !KnownSet.count(I.first); |
1009 // Walk the array and drop any metadata we don't know. | 1079 }); |
1010 for (I = 0, E = Info.size(); I != E;) { | 1080 |
1011 if (KnownSet.count(Info[I].first)) { | 1081 if (Info.empty()) { |
1012 ++I; | |
1013 continue; | |
1014 } | |
1015 | |
1016 Info[I] = std::move(Info.back()); | |
1017 Info.pop_back(); | |
1018 --E; | |
1019 } | |
1020 assert(E == Info.size()); | |
1021 | |
1022 if (E == 0) { | |
1023 // Drop our entry at the store. | 1082 // Drop our entry at the store. |
1024 MetadataStore.erase(this); | 1083 InstructionMetadata.erase(this); |
1025 setHasMetadataHashEntry(false); | 1084 setHasMetadataHashEntry(false); |
1026 } | 1085 } |
1027 } | 1086 } |
1028 | 1087 |
1029 /// setMetadata - Set the metadata of of the specified kind to the specified | 1088 /// setMetadata - Set the metadata of the specified kind to the specified |
1030 /// node. This updates/replaces metadata if already present, or removes it if | 1089 /// node. This updates/replaces metadata if already present, or removes it if |
1031 /// Node is null. | 1090 /// Node is null. |
1032 void Instruction::setMetadata(unsigned KindID, MDNode *Node) { | 1091 void Instruction::setMetadata(unsigned KindID, MDNode *Node) { |
1033 if (!Node && !hasMetadata()) | 1092 if (!Node && !hasMetadata()) |
1034 return; | 1093 return; |
1035 | 1094 |
1036 // Handle 'dbg' as a special case since it is not stored in the hash table. | 1095 // Handle 'dbg' as a special case since it is not stored in the hash table. |
1037 if (KindID == LLVMContext::MD_dbg) { | 1096 if (KindID == LLVMContext::MD_dbg) { |
1038 DbgLoc = DebugLoc::getFromDILocation(Node); | 1097 DbgLoc = DebugLoc(Node); |
1039 return; | 1098 return; |
1040 } | 1099 } |
1041 | 1100 |
1042 // Handle the case when we're adding/updating metadata on an instruction. | 1101 // Handle the case when we're adding/updating metadata on an instruction. |
1043 if (Node) { | 1102 if (Node) { |
1044 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; | 1103 auto &Info = getContext().pImpl->InstructionMetadata[this]; |
1045 assert(!Info.empty() == hasMetadataHashEntry() && | 1104 assert(!Info.empty() == hasMetadataHashEntry() && |
1046 "HasMetadata bit is wonked"); | 1105 "HasMetadata bit is wonked"); |
1047 if (Info.empty()) { | 1106 if (Info.empty()) |
1048 setHasMetadataHashEntry(true); | 1107 setHasMetadataHashEntry(true); |
1049 } else { | 1108 Info.set(KindID, *Node); |
1050 // Handle replacement of an existing value. | |
1051 for (auto &P : Info) | |
1052 if (P.first == KindID) { | |
1053 P.second.reset(Node); | |
1054 return; | |
1055 } | |
1056 } | |
1057 | |
1058 // No replacement, just add it to the list. | |
1059 Info.emplace_back(std::piecewise_construct, std::make_tuple(KindID), | |
1060 std::make_tuple(Node)); | |
1061 return; | 1109 return; |
1062 } | 1110 } |
1063 | 1111 |
1064 // Otherwise, we're removing metadata from an instruction. | 1112 // Otherwise, we're removing metadata from an instruction. |
1065 assert((hasMetadataHashEntry() == | 1113 assert((hasMetadataHashEntry() == |
1066 (getContext().pImpl->MetadataStore.count(this) > 0)) && | 1114 (getContext().pImpl->InstructionMetadata.count(this) > 0)) && |
1067 "HasMetadata bit out of date!"); | 1115 "HasMetadata bit out of date!"); |
1068 if (!hasMetadataHashEntry()) | 1116 if (!hasMetadataHashEntry()) |
1069 return; // Nothing to remove! | 1117 return; // Nothing to remove! |
1070 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; | 1118 auto &Info = getContext().pImpl->InstructionMetadata[this]; |
1071 | |
1072 // Common case is removing the only entry. | |
1073 if (Info.size() == 1 && Info[0].first == KindID) { | |
1074 getContext().pImpl->MetadataStore.erase(this); | |
1075 setHasMetadataHashEntry(false); | |
1076 return; | |
1077 } | |
1078 | 1119 |
1079 // Handle removal of an existing value. | 1120 // Handle removal of an existing value. |
1080 for (unsigned i = 0, e = Info.size(); i != e; ++i) | 1121 Info.erase(KindID); |
1081 if (Info[i].first == KindID) { | 1122 |
1082 Info[i] = std::move(Info.back()); | 1123 if (!Info.empty()) |
1083 Info.pop_back(); | 1124 return; |
1084 assert(!Info.empty() && "Removing last entry should be handled above"); | 1125 |
1085 return; | 1126 getContext().pImpl->InstructionMetadata.erase(this); |
1086 } | 1127 setHasMetadataHashEntry(false); |
1087 // Otherwise, removing an entry that doesn't exist on the instruction. | |
1088 } | 1128 } |
1089 | 1129 |
1090 void Instruction::setAAMetadata(const AAMDNodes &N) { | 1130 void Instruction::setAAMetadata(const AAMDNodes &N) { |
1091 setMetadata(LLVMContext::MD_tbaa, N.TBAA); | 1131 setMetadata(LLVMContext::MD_tbaa, N.TBAA); |
1092 setMetadata(LLVMContext::MD_alias_scope, N.Scope); | 1132 setMetadata(LLVMContext::MD_alias_scope, N.Scope); |
1096 MDNode *Instruction::getMetadataImpl(unsigned KindID) const { | 1136 MDNode *Instruction::getMetadataImpl(unsigned KindID) const { |
1097 // Handle 'dbg' as a special case since it is not stored in the hash table. | 1137 // Handle 'dbg' as a special case since it is not stored in the hash table. |
1098 if (KindID == LLVMContext::MD_dbg) | 1138 if (KindID == LLVMContext::MD_dbg) |
1099 return DbgLoc.getAsMDNode(); | 1139 return DbgLoc.getAsMDNode(); |
1100 | 1140 |
1101 if (!hasMetadataHashEntry()) return nullptr; | 1141 if (!hasMetadataHashEntry()) |
1102 | 1142 return nullptr; |
1103 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; | 1143 auto &Info = getContext().pImpl->InstructionMetadata[this]; |
1104 assert(!Info.empty() && "bit out of sync with hash table"); | 1144 assert(!Info.empty() && "bit out of sync with hash table"); |
1105 | 1145 |
1106 for (const auto &I : Info) | 1146 return Info.lookup(KindID); |
1107 if (I.first == KindID) | |
1108 return I.second; | |
1109 return nullptr; | |
1110 } | 1147 } |
1111 | 1148 |
1112 void Instruction::getAllMetadataImpl( | 1149 void Instruction::getAllMetadataImpl( |
1113 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { | 1150 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
1114 Result.clear(); | 1151 Result.clear(); |
1115 | 1152 |
1116 // Handle 'dbg' as a special case since it is not stored in the hash table. | 1153 // Handle 'dbg' as a special case since it is not stored in the hash table. |
1117 if (!DbgLoc.isUnknown()) { | 1154 if (DbgLoc) { |
1118 Result.push_back( | 1155 Result.push_back( |
1119 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); | 1156 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); |
1120 if (!hasMetadataHashEntry()) return; | 1157 if (!hasMetadataHashEntry()) return; |
1121 } | 1158 } |
1122 | 1159 |
1123 assert(hasMetadataHashEntry() && | 1160 assert(hasMetadataHashEntry() && |
1124 getContext().pImpl->MetadataStore.count(this) && | 1161 getContext().pImpl->InstructionMetadata.count(this) && |
1125 "Shouldn't have called this"); | 1162 "Shouldn't have called this"); |
1126 const LLVMContextImpl::MDMapTy &Info = | 1163 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; |
1127 getContext().pImpl->MetadataStore.find(this)->second; | |
1128 assert(!Info.empty() && "Shouldn't have called this"); | 1164 assert(!Info.empty() && "Shouldn't have called this"); |
1129 | 1165 Info.getAll(Result); |
1130 Result.reserve(Result.size() + Info.size()); | |
1131 for (auto &I : Info) | |
1132 Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); | |
1133 | |
1134 // Sort the resulting array so it is stable. | |
1135 if (Result.size() > 1) | |
1136 array_pod_sort(Result.begin(), Result.end()); | |
1137 } | 1166 } |
1138 | 1167 |
1139 void Instruction::getAllMetadataOtherThanDebugLocImpl( | 1168 void Instruction::getAllMetadataOtherThanDebugLocImpl( |
1140 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { | 1169 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { |
1141 Result.clear(); | 1170 Result.clear(); |
1142 assert(hasMetadataHashEntry() && | 1171 assert(hasMetadataHashEntry() && |
1143 getContext().pImpl->MetadataStore.count(this) && | 1172 getContext().pImpl->InstructionMetadata.count(this) && |
1144 "Shouldn't have called this"); | 1173 "Shouldn't have called this"); |
1145 const LLVMContextImpl::MDMapTy &Info = | 1174 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; |
1146 getContext().pImpl->MetadataStore.find(this)->second; | |
1147 assert(!Info.empty() && "Shouldn't have called this"); | 1175 assert(!Info.empty() && "Shouldn't have called this"); |
1148 Result.reserve(Result.size() + Info.size()); | 1176 Info.getAll(Result); |
1149 for (auto &I : Info) | |
1150 Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); | |
1151 | |
1152 // Sort the resulting array so it is stable. | |
1153 if (Result.size() > 1) | |
1154 array_pod_sort(Result.begin(), Result.end()); | |
1155 } | 1177 } |
1156 | 1178 |
1157 /// clearMetadataHashEntries - Clear all hashtable-based metadata from | 1179 /// clearMetadataHashEntries - Clear all hashtable-based metadata from |
1158 /// this instruction. | 1180 /// this instruction. |
1159 void Instruction::clearMetadataHashEntries() { | 1181 void Instruction::clearMetadataHashEntries() { |
1160 assert(hasMetadataHashEntry() && "Caller should check"); | 1182 assert(hasMetadataHashEntry() && "Caller should check"); |
1161 getContext().pImpl->MetadataStore.erase(this); | 1183 getContext().pImpl->InstructionMetadata.erase(this); |
1162 setHasMetadataHashEntry(false); | 1184 setHasMetadataHashEntry(false); |
1163 } | 1185 } |
1186 | |
1187 MDNode *Function::getMetadata(unsigned KindID) const { | |
1188 if (!hasMetadata()) | |
1189 return nullptr; | |
1190 return getContext().pImpl->FunctionMetadata[this].lookup(KindID); | |
1191 } | |
1192 | |
1193 MDNode *Function::getMetadata(StringRef Kind) const { | |
1194 if (!hasMetadata()) | |
1195 return nullptr; | |
1196 return getMetadata(getContext().getMDKindID(Kind)); | |
1197 } | |
1198 | |
1199 void Function::setMetadata(unsigned KindID, MDNode *MD) { | |
1200 if (MD) { | |
1201 if (!hasMetadata()) | |
1202 setHasMetadataHashEntry(true); | |
1203 | |
1204 getContext().pImpl->FunctionMetadata[this].set(KindID, *MD); | |
1205 return; | |
1206 } | |
1207 | |
1208 // Nothing to unset. | |
1209 if (!hasMetadata()) | |
1210 return; | |
1211 | |
1212 auto &Store = getContext().pImpl->FunctionMetadata[this]; | |
1213 Store.erase(KindID); | |
1214 if (Store.empty()) | |
1215 clearMetadata(); | |
1216 } | |
1217 | |
1218 void Function::setMetadata(StringRef Kind, MDNode *MD) { | |
1219 if (!MD && !hasMetadata()) | |
1220 return; | |
1221 setMetadata(getContext().getMDKindID(Kind), MD); | |
1222 } | |
1223 | |
1224 void Function::getAllMetadata( | |
1225 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { | |
1226 MDs.clear(); | |
1227 | |
1228 if (!hasMetadata()) | |
1229 return; | |
1230 | |
1231 getContext().pImpl->FunctionMetadata[this].getAll(MDs); | |
1232 } | |
1233 | |
1234 void Function::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { | |
1235 if (!hasMetadata()) | |
1236 return; | |
1237 if (KnownIDs.empty()) { | |
1238 clearMetadata(); | |
1239 return; | |
1240 } | |
1241 | |
1242 SmallSet<unsigned, 5> KnownSet; | |
1243 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); | |
1244 | |
1245 auto &Store = getContext().pImpl->FunctionMetadata[this]; | |
1246 assert(!Store.empty()); | |
1247 | |
1248 Store.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) { | |
1249 return !KnownSet.count(I.first); | |
1250 }); | |
1251 | |
1252 if (Store.empty()) | |
1253 clearMetadata(); | |
1254 } | |
1255 | |
1256 void Function::clearMetadata() { | |
1257 if (!hasMetadata()) | |
1258 return; | |
1259 getContext().pImpl->FunctionMetadata.erase(this); | |
1260 setHasMetadataHashEntry(false); | |
1261 } | |
1262 | |
1263 void Function::setSubprogram(DISubprogram *SP) { | |
1264 setMetadata(LLVMContext::MD_dbg, SP); | |
1265 } | |
1266 | |
1267 DISubprogram *Function::getSubprogram() const { | |
1268 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg)); | |
1269 } |