From f5a21bad608f625896d8d2ac05afde89b86bcd10 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 14 Nov 2014 01:17:09 +0000 Subject: [PATCH] IR: Rewrite uniquing and creation of MDString Stop using `Value::getName()` to get the string behind an `MDString`. Switch to `StringMapEntry` so that we can find the string by its coallocation. This is part of PR21532. llvm-svn: 221960 --- include/llvm/IR/Metadata.h | 13 +++++++------ lib/IR/LLVMContextImpl.cpp | 2 +- lib/IR/LLVMContextImpl.h | 2 +- lib/IR/Metadata.cpp | 22 +++++++++++++++------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/llvm/IR/Metadata.h b/include/llvm/IR/Metadata.h index 36b1e063cb9..6cad37e9c0b 100644 --- a/include/llvm/IR/Metadata.h +++ b/include/llvm/IR/Metadata.h @@ -57,14 +57,15 @@ public: /// /// TODO: Inherit from Metadata. class MDString : public Value { + friend class StringMapEntry; + virtual void anchor(); MDString(const MDString &) LLVM_DELETED_FUNCTION; explicit MDString(LLVMContext &C); -private: /// \brief Shadow Value::getName() to prevent its use. - StringRef getName() const { return Value::getName(); } + StringRef getName() const LLVM_DELETED_FUNCTION; public: static MDString *get(LLVMContext &Context, StringRef Str); @@ -72,17 +73,17 @@ public: return get(Context, Str ? StringRef(Str) : StringRef()); } - StringRef getString() const { return getName(); } + StringRef getString() const; - unsigned getLength() const { return (unsigned)getName().size(); } + unsigned getLength() const { return (unsigned)getString().size(); } typedef StringRef::iterator iterator; /// \brief Pointer to the first byte of the string. - iterator begin() const { return getName().begin(); } + iterator begin() const { return getString().begin(); } /// \brief Pointer to one byte past the end of the string. - iterator end() const { return getName().end(); } + iterator end() const { return getString().end(); } /// \brief Methods for support type inquiry through isa, cast, and dyn_cast. static bool classof(const Value *V) { diff --git a/lib/IR/LLVMContextImpl.cpp b/lib/IR/LLVMContextImpl.cpp index 09bf6b123a9..df3449d12c8 100644 --- a/lib/IR/LLVMContextImpl.cpp +++ b/lib/IR/LLVMContextImpl.cpp @@ -135,7 +135,7 @@ LLVMContextImpl::~LLVMContextImpl() { "Destroying all MDNodes didn't empty the Context's sets."); // Destroy MDStrings. - DeleteContainerSeconds(MDStringCache); + MDStringCache.clear(); } // ConstantsContext anchors diff --git a/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h index bd0097c31ba..3190c22fceb 100644 --- a/lib/IR/LLVMContextImpl.h +++ b/lib/IR/LLVMContextImpl.h @@ -261,7 +261,7 @@ public: FoldingSet AttrsLists; FoldingSet AttrsSetNodes; - StringMap MDStringCache; + StringMap MDStringCache; FoldingSet MDNodeSet; diff --git a/lib/IR/Metadata.cpp b/lib/IR/Metadata.cpp index 8d616cfb0d6..bc9682976b8 100644 --- a/lib/IR/Metadata.cpp +++ b/lib/IR/Metadata.cpp @@ -37,13 +37,21 @@ MDString::MDString(LLVMContext &C) : Value(Type::getMetadataTy(C), Value::MDStringVal) {} MDString *MDString::get(LLVMContext &Context, StringRef Str) { - LLVMContextImpl *pImpl = Context.pImpl; - StringMapEntry &Entry = - pImpl->MDStringCache.GetOrCreateValue(Str); - Value *&S = Entry.getValue(); - if (!S) S = new MDString(Context); - S->setValueName(&Entry); - return cast(S); + auto &Store = Context.pImpl->MDStringCache; + auto I = Store.find(Str); + if (I != Store.end()) + return &I->second; + + auto *Entry = + StringMapEntry::Create(Str, Store.getAllocator(), Context); + bool WasInserted = Store.insert(Entry); + (void)WasInserted; + assert(WasInserted && "Expected entry to be inserted"); + return &Entry->second; +} + +StringRef MDString::getString() const { + return StringMapEntry::GetStringMapEntryFromValue(*this).first(); } //===----------------------------------------------------------------------===//