diff options
Diffstat (limited to 'lib/IR/Metadata.cpp')
-rw-r--r-- | lib/IR/Metadata.cpp | 83 |
1 files changed, 47 insertions, 36 deletions
diff --git a/lib/IR/Metadata.cpp b/lib/IR/Metadata.cpp index a148ab65fc83..83a22d95bd81 100644 --- a/lib/IR/Metadata.cpp +++ b/lib/IR/Metadata.cpp @@ -237,7 +237,7 @@ void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { // Copy out uses since UseMap will get touched below. using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>; SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); - std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + llvm::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { return L.second.second < R.second.second; }); for (const auto &Pair : Uses) { @@ -290,7 +290,7 @@ void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { // Copy out uses since UseMap could get touched below. using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>; SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); - std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + llvm::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { return L.second.second < R.second.second; }); UseMap.clear(); @@ -329,12 +329,20 @@ bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) { return dyn_cast<ValueAsMetadata>(&MD); } -static Function *getLocalFunction(Value *V) { +static DISubprogram *getLocalFunctionMetadata(Value *V) { assert(V && "Expected value"); - if (auto *A = dyn_cast<Argument>(V)) - return A->getParent(); - if (BasicBlock *BB = cast<Instruction>(V)->getParent()) - return BB->getParent(); + if (auto *A = dyn_cast<Argument>(V)) { + if (auto *Fn = A->getParent()) + return Fn->getSubprogram(); + return nullptr; + } + + if (BasicBlock *BB = cast<Instruction>(V)->getParent()) { + if (auto *Fn = BB->getParent()) + return Fn->getSubprogram(); + return nullptr; + } + return nullptr; } @@ -410,9 +418,9 @@ void ValueAsMetadata::handleRAUW(Value *From, Value *To) { delete MD; return; } - if (getLocalFunction(From) && getLocalFunction(To) && - getLocalFunction(From) != getLocalFunction(To)) { - // Function changed. + if (getLocalFunctionMetadata(From) && getLocalFunctionMetadata(To) && + getLocalFunctionMetadata(From) != getLocalFunctionMetadata(To)) { + // DISubprogram changed. MD->replaceAllUsesWith(nullptr); delete MD; return; @@ -1102,14 +1110,14 @@ void MDAttachmentMap::set(unsigned ID, MDNode &MD) { std::make_tuple(&MD)); } -void MDAttachmentMap::erase(unsigned ID) { +bool MDAttachmentMap::erase(unsigned ID) { if (empty()) - return; + return false; // Common case is one/last value. if (Attachments.back().first == ID) { Attachments.pop_back(); - return; + return true; } for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E; @@ -1117,8 +1125,10 @@ void MDAttachmentMap::erase(unsigned ID) { if (I->first == ID) { *I = std::move(Attachments.back()); Attachments.pop_back(); - return; + return true; } + + return false; } MDNode *MDAttachmentMap::lookup(unsigned ID) const { @@ -1141,29 +1151,31 @@ void MDGlobalAttachmentMap::insert(unsigned ID, MDNode &MD) { Attachments.push_back({ID, TrackingMDNodeRef(&MD)}); } +MDNode *MDGlobalAttachmentMap::lookup(unsigned ID) const { + for (const auto &A : Attachments) + if (A.MDKind == ID) + return A.Node; + return nullptr; +} + void MDGlobalAttachmentMap::get(unsigned ID, - SmallVectorImpl<MDNode *> &Result) { - for (auto A : Attachments) + SmallVectorImpl<MDNode *> &Result) const { + for (const auto &A : Attachments) if (A.MDKind == ID) Result.push_back(A.Node); } -void MDGlobalAttachmentMap::erase(unsigned ID) { - auto Follower = Attachments.begin(); - for (auto Leader = Attachments.begin(), E = Attachments.end(); Leader != E; - ++Leader) { - if (Leader->MDKind != ID) { - if (Follower != Leader) - *Follower = std::move(*Leader); - ++Follower; - } - } - Attachments.resize(Follower - Attachments.begin()); +bool MDGlobalAttachmentMap::erase(unsigned ID) { + auto I = std::remove_if(Attachments.begin(), Attachments.end(), + [ID](const Attachment &A) { return A.MDKind == ID; }); + bool Changed = I != Attachments.end(); + Attachments.erase(I, Attachments.end()); + return Changed; } void MDGlobalAttachmentMap::getAll( SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { - for (auto &A : Attachments) + for (const auto &A : Attachments) Result.emplace_back(A.MDKind, A.Node); // Sort the resulting array so it is stable with respect to metadata IDs. We @@ -1390,15 +1402,16 @@ void GlobalObject::addMetadata(StringRef Kind, MDNode &MD) { addMetadata(getContext().getMDKindID(Kind), MD); } -void GlobalObject::eraseMetadata(unsigned KindID) { +bool GlobalObject::eraseMetadata(unsigned KindID) { // Nothing to unset. if (!hasMetadata()) - return; + return false; auto &Store = getContext().pImpl->GlobalObjectMetadata[this]; - Store.erase(KindID); + bool Changed = Store.erase(KindID); if (Store.empty()) clearMetadata(); + return Changed; } void GlobalObject::getAllMetadata( @@ -1429,11 +1442,9 @@ void GlobalObject::setMetadata(StringRef Kind, MDNode *N) { } MDNode *GlobalObject::getMetadata(unsigned KindID) const { - SmallVector<MDNode *, 1> MDs; - getMetadata(KindID, MDs); - if (MDs.empty()) - return nullptr; - return MDs[0]; + if (hasMetadata()) + return getContext().pImpl->GlobalObjectMetadata[this].lookup(KindID); + return nullptr; } MDNode *GlobalObject::getMetadata(StringRef Kind) const { |