diff options
Diffstat (limited to 'llvm/lib/IR')
-rw-r--r-- | llvm/lib/IR/DebugInfo.cpp | 9 | ||||
-rw-r--r-- | llvm/lib/IR/LLVMContextImpl.h | 10 | ||||
-rw-r--r-- | llvm/lib/IR/Metadata.cpp | 55 |
3 files changed, 38 insertions, 36 deletions
diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 993020e7b4c..b741cc9236f 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -313,7 +313,7 @@ static MDNode *stripDebugLocFromLoopID(MDNode *N) { bool llvm::stripDebugInfo(Function &F) { bool Changed = false; - if (F.getMetadata(LLVMContext::MD_dbg)) { + if (F.hasMetadata(LLVMContext::MD_dbg)) { Changed = true; F.setSubprogram(nullptr); } @@ -369,12 +369,7 @@ bool llvm::StripDebugInfo(Module &M) { Changed |= stripDebugInfo(F); for (auto &GV : M.globals()) { - SmallVector<MDNode *, 1> MDs; - GV.getMetadata(LLVMContext::MD_dbg, MDs); - if (!MDs.empty()) { - GV.eraseMetadata(LLVMContext::MD_dbg); - Changed = true; - } + Changed |= GV.eraseMetadata(LLVMContext::MD_dbg); } if (GVMaterializer *Materializer = M.getMaterializer()) diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h index 7ceeca6e347..d5046d64418 100644 --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -1164,7 +1164,7 @@ public: /// Remove an attachment. /// /// Remove the attachment at \c ID, if any. - void erase(unsigned ID); + bool erase(unsigned ID); /// Copy out all the attachments. /// @@ -1197,10 +1197,14 @@ public: /// Appends all attachments with the given ID to \c Result in insertion order. /// If the global has no attachments with the given ID, or if ID is invalid, /// leaves Result unchanged. - void get(unsigned ID, SmallVectorImpl<MDNode *> &Result); + void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const; + + /// Returns the first attachment with the given ID or nullptr if no such + /// attachment exists. + MDNode *lookup(unsigned ID) const; void insert(unsigned ID, MDNode &MD); - void erase(unsigned ID); + bool erase(unsigned ID); /// Appends all attachments for the global to \c Result, sorting by attachment /// ID. Attachments with the same ID appear in insertion order. This function diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 76c43345266..83a22d95bd8 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -1110,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; @@ -1125,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 { @@ -1149,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 @@ -1398,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( @@ -1437,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 { |