IR: Rewrite uniquing and creation of MDString

Stop using `Value::getName()` to get the string behind an `MDString`.
Switch to `StringMapEntry<MDString>` so that we can find the string by
its coallocation.

This is part of PR21532.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221960 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith
2014-11-14 01:17:09 +00:00
parent 7987683c39
commit ed7bdcaf03
4 changed files with 24 additions and 15 deletions

View File

@@ -57,14 +57,15 @@ public:
/// ///
/// TODO: Inherit from Metadata. /// TODO: Inherit from Metadata.
class MDString : public Value { class MDString : public Value {
friend class StringMapEntry<MDString>;
virtual void anchor(); virtual void anchor();
MDString(const MDString &) LLVM_DELETED_FUNCTION; MDString(const MDString &) LLVM_DELETED_FUNCTION;
explicit MDString(LLVMContext &C); explicit MDString(LLVMContext &C);
private:
/// \brief Shadow Value::getName() to prevent its use. /// \brief Shadow Value::getName() to prevent its use.
StringRef getName() const { return Value::getName(); } StringRef getName() const LLVM_DELETED_FUNCTION;
public: public:
static MDString *get(LLVMContext &Context, StringRef Str); static MDString *get(LLVMContext &Context, StringRef Str);
@@ -72,17 +73,17 @@ public:
return get(Context, Str ? StringRef(Str) : StringRef()); 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; typedef StringRef::iterator iterator;
/// \brief Pointer to the first byte of the string. /// \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. /// \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. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
static bool classof(const Value *V) { static bool classof(const Value *V) {

View File

@@ -135,7 +135,7 @@ LLVMContextImpl::~LLVMContextImpl() {
"Destroying all MDNodes didn't empty the Context's sets."); "Destroying all MDNodes didn't empty the Context's sets.");
// Destroy MDStrings. // Destroy MDStrings.
DeleteContainerSeconds(MDStringCache); MDStringCache.clear();
} }
// ConstantsContext anchors // ConstantsContext anchors

View File

@@ -261,7 +261,7 @@ public:
FoldingSet<AttributeSetImpl> AttrsLists; FoldingSet<AttributeSetImpl> AttrsLists;
FoldingSet<AttributeSetNode> AttrsSetNodes; FoldingSet<AttributeSetNode> AttrsSetNodes;
StringMap<Value*> MDStringCache; StringMap<MDString> MDStringCache;
FoldingSet<MDNode> MDNodeSet; FoldingSet<MDNode> MDNodeSet;

View File

@@ -37,13 +37,21 @@ MDString::MDString(LLVMContext &C)
: Value(Type::getMetadataTy(C), Value::MDStringVal) {} : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
MDString *MDString::get(LLVMContext &Context, StringRef Str) { MDString *MDString::get(LLVMContext &Context, StringRef Str) {
LLVMContextImpl *pImpl = Context.pImpl; auto &Store = Context.pImpl->MDStringCache;
StringMapEntry<Value*> &Entry = auto I = Store.find(Str);
pImpl->MDStringCache.GetOrCreateValue(Str); if (I != Store.end())
Value *&S = Entry.getValue(); return &I->second;
if (!S) S = new MDString(Context);
S->setValueName(&Entry); auto *Entry =
return cast<MDString>(S); StringMapEntry<MDString>::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<MDString>::GetStringMapEntryFromValue(*this).first();
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//