diff --git a/llvm/include/llvm/CodeGen/DwarfStringPoolEntry.h b/llvm/include/llvm/CodeGen/DwarfStringPoolEntry.h --- a/llvm/include/llvm/CodeGen/DwarfStringPoolEntry.h +++ b/llvm/include/llvm/CodeGen/DwarfStringPoolEntry.h @@ -9,7 +9,7 @@ #ifndef LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H #define LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H -#include "llvm/ADT/PointerIntPair.h" +#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/StringMap.h" namespace llvm { @@ -20,49 +20,76 @@ struct DwarfStringPoolEntry { static constexpr unsigned NotIndexed = -1; - MCSymbol *Symbol; - uint64_t Offset; - unsigned Index; + MCSymbol *Symbol = nullptr; + uint64_t Offset = 0; + unsigned Index = 0; bool isIndexed() const { return Index != NotIndexed; } }; /// String pool entry reference. class DwarfStringPoolEntryRef { - PointerIntPair *, 1, bool> - MapEntryAndIndexed; + /// Full DwarfStringPoolEntry stored in the StringMapEntry. + using StringEntryPtr = const StringMapEntry *; - const StringMapEntry *getMapEntry() const { - return MapEntryAndIndexed.getPointer(); - } + /// Pointer to DwarfStringPoolEntry stored in the StringMapEntry. + using ShortStringEntryPtr = const StringMapEntry *; + + /// Pointer to the string pool Entry. + PointerUnion MapEntry = nullptr; public: DwarfStringPoolEntryRef() = default; - DwarfStringPoolEntryRef(const StringMapEntry &Entry, - bool Indexed) - : MapEntryAndIndexed(&Entry, Indexed) {} - explicit operator bool() const { return getMapEntry(); } + // ASSUMPTION: \p Entry mustn`t be reallocated. + DwarfStringPoolEntryRef(const StringMapEntry &Entry) + : MapEntry(&Entry) {} + + // ASSUMPTION: \p Entry mustn`t be reallocated. + DwarfStringPoolEntryRef(const StringMapEntry &Entry) + : MapEntry(&Entry) {} + + explicit operator bool() const { return !MapEntry.isNull(); } + + /// \returns symbol for the dwarf string. MCSymbol *getSymbol() const { - assert(getMapEntry()->second.Symbol && "No symbol available!"); - return getMapEntry()->second.Symbol; + assert(getEntry().Symbol && "No symbol available!"); + return getEntry().Symbol; } - uint64_t getOffset() const { return getMapEntry()->second.Offset; } - bool isIndexed() const { return MapEntryAndIndexed.getInt(); } + + /// \returns offset for the dwarf string. + uint64_t getOffset() const { return getEntry().Offset; } + + /// \returns index for the dwarf string. unsigned getIndex() const { - assert(isIndexed()); - assert(getMapEntry()->getValue().isIndexed()); - return getMapEntry()->second.Index; + assert(getEntry().isIndexed() && "Index is not set!"); + return getEntry().Index; + } + + /// \returns string. + StringRef getString() const { + if (MapEntry.is()) + return MapEntry.get()->first(); + else + return MapEntry.get()->first(); + } + + /// \returns the entire string pool entry for convenience. + const DwarfStringPoolEntry &getEntry() const { + if (MapEntry.is()) + return MapEntry.get()->second; + else { + assert(MapEntry.get()->second != nullptr); + return *MapEntry.get()->second; + } } - StringRef getString() const { return getMapEntry()->first(); } - /// Return the entire string pool entry for convenience. - DwarfStringPoolEntry getEntry() const { return getMapEntry()->getValue(); } bool operator==(const DwarfStringPoolEntryRef &X) const { - return getMapEntry() == X.getMapEntry(); + return MapEntry.getOpaqueValue() == X.MapEntry.getOpaqueValue(); } + bool operator!=(const DwarfStringPoolEntryRef &X) const { - return getMapEntry() != X.getMapEntry(); + return MapEntry.getOpaqueValue() != X.MapEntry.getOpaqueValue(); } }; diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp @@ -39,7 +39,7 @@ DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, StringRef Str) { auto &MapEntry = getEntryImpl(Asm, Str); - return EntryRef(MapEntry, false); + return EntryRef(MapEntry); } DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, @@ -47,7 +47,7 @@ auto &MapEntry = getEntryImpl(Asm, Str); if (!MapEntry.getValue().isIndexed()) MapEntry.getValue().Index = NumIndexedStrings++; - return EntryRef(MapEntry, true); + return EntryRef(MapEntry); } void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm, diff --git a/llvm/lib/CodeGen/NonRelocatableStringpool.cpp b/llvm/lib/CodeGen/NonRelocatableStringpool.cpp --- a/llvm/lib/CodeGen/NonRelocatableStringpool.cpp +++ b/llvm/lib/CodeGen/NonRelocatableStringpool.cpp @@ -25,7 +25,7 @@ Entry.Symbol = nullptr; CurrentEndOffset += S.size() + 1; } - return DwarfStringPoolEntryRef(*I.first, true); + return DwarfStringPoolEntryRef(*I.first); } StringRef NonRelocatableStringpool::internString(StringRef S) { @@ -44,7 +44,7 @@ Result.reserve(Strings.size()); for (const auto &E : Strings) if (E.getValue().isIndexed()) - Result.emplace_back(E, true); + Result.emplace_back(E); llvm::sort(Result, [](const DwarfStringPoolEntryRef A, const DwarfStringPoolEntryRef B) { return A.getIndex() < B.getIndex(); diff --git a/llvm/unittests/CodeGen/DIEHashTest.cpp b/llvm/unittests/CodeGen/DIEHashTest.cpp --- a/llvm/unittests/CodeGen/DIEHashTest.cpp +++ b/llvm/unittests/CodeGen/DIEHashTest.cpp @@ -41,8 +41,8 @@ public: DIEString getString(StringRef S) { DwarfStringPoolEntry Entry = {nullptr, 1, 1}; - return DIEString(DwarfStringPoolEntryRef( - *Pool.insert(std::make_pair(S, Entry)).first, Entry.isIndexed())); + return DIEString( + DwarfStringPoolEntryRef(*Pool.insert(std::make_pair(S, Entry)).first)); } AsmPrinter *getAsmPrinter() {