From ac9d1467e281d97ae2892ddc61823c4bf90f06c2 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Wed, 17 Feb 2016 02:18:58 +0000 Subject: [PATCH] Revert "Query the StringMap only once when creating MDString (NFC)" This reverts commit r261030 and r261036. (The revision was marked "approved" on phabricator, but some concerns were raised on the mailing list. Thanks D. Blaikie for notifying me.) From: Mehdi Amini llvm-svn: 261055 --- llvm/include/llvm/IR/Metadata.h | 4 +--- llvm/lib/IR/Metadata.cpp | 17 +++++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h index a514b6006d52..df8ce354bb7f 100644 --- a/llvm/include/llvm/IR/Metadata.h +++ b/llvm/include/llvm/IR/Metadata.h @@ -592,11 +592,9 @@ class MDString : public Metadata { StringMapEntry *Entry; MDString() : Metadata(MDStringKind, Uniqued), Entry(nullptr) {} + MDString(MDString &&) : Metadata(MDStringKind, Uniqued) {} public: - // This is a "default" move ctor, MSVC does not support "= default" here - MDString(MDString &&R) - : Metadata(MDStringKind, Uniqued), Entry(std::move(R.Entry)) {} static MDString *get(LLVMContext &Context, StringRef Str); static MDString *get(LLVMContext &Context, const char *Str) { return get(Context, Str ? StringRef(Str) : StringRef()); diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index ddb9cc4e17ff..cc0c568c8613 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -397,12 +397,17 @@ void ValueAsMetadata::handleRAUW(Value *From, Value *To) { MDString *MDString::get(LLVMContext &Context, StringRef Str) { auto &Store = Context.pImpl->MDStringCache; - auto I = Store.insert(std::make_pair(Str, MDString())); - auto &MapEntry = I.first->getValue(); - if (!I.second) - return &MapEntry; - MapEntry.Entry = &*I.first; - return &MapEntry; + auto I = Store.find(Str); + if (I != Store.end()) + return &I->second; + + auto *Entry = + StringMapEntry::Create(Str, Store.getAllocator(), MDString()); + bool WasInserted = Store.insert(Entry); + (void)WasInserted; + assert(WasInserted && "Expected entry to be inserted"); + Entry->second.Entry = Entry; + return &Entry->second; } StringRef MDString::getString() const {