1 //===- llvm/CodeGen/DwarfStringPoolEntry.h - String pool entry --*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H 10 #define LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H 11 12 #include "llvm/ADT/PointerUnion.h" 13 #include "llvm/ADT/StringMap.h" 14 15 namespace llvm { 16 17 class MCSymbol; 18 19 /// Data for a string pool entry. 20 struct DwarfStringPoolEntry { 21 static constexpr unsigned NotIndexed = -1; 22 23 MCSymbol *Symbol = nullptr; 24 uint64_t Offset = 0; 25 unsigned Index = 0; 26 27 bool isIndexed() const { return Index != NotIndexed; } 28 }; 29 30 /// DwarfStringPoolEntryRef: Dwarf string pool entry reference. 31 /// 32 /// Dwarf string pool entry keeps string value and its data. 33 /// There are two variants how data are represented: 34 /// 35 /// 1. By value - StringMapEntry<DwarfStringPoolEntry>. 36 /// 2. By pointer - StringMapEntry<DwarfStringPoolEntry *>. 37 /// 38 /// The "By pointer" variant allows for reducing memory usage for the case 39 /// when string pool entry does not have data: it keeps the null pointer 40 /// and so no need to waste space for the full DwarfStringPoolEntry. 41 /// It is recommended to use "By pointer" variant if not all entries 42 /// of dwarf string pool have corresponding DwarfStringPoolEntry. 43 44 class DwarfStringPoolEntryRef { 45 /// Pointer type for "By value" string entry. 46 using ByValStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry> *; 47 48 /// Pointer type for "By pointer" string entry. 49 using ByPtrStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry *> *; 50 51 /// Pointer to the dwarf string pool Entry. 52 PointerUnion<ByValStringEntryPtr, ByPtrStringEntryPtr> MapEntry = nullptr; 53 54 public: 55 DwarfStringPoolEntryRef() = default; 56 57 /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry, 58 /// thus specified entry mustn`t be reallocated. 59 DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry> &Entry) 60 : MapEntry(&Entry) {} 61 62 /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry, 63 /// thus specified entry mustn`t be reallocated. 64 DwarfStringPoolEntryRef(const StringMapEntry<DwarfStringPoolEntry *> &Entry) 65 : MapEntry(&Entry) { 66 assert(MapEntry.get<ByPtrStringEntryPtr>()->second != nullptr); 67 } 68 69 explicit operator bool() const { return !MapEntry.isNull(); } 70 71 /// \returns symbol for the dwarf string. 72 MCSymbol *getSymbol() const { 73 assert(getEntry().Symbol && "No symbol available!"); 74 return getEntry().Symbol; 75 } 76 77 /// \returns offset for the dwarf string. 78 uint64_t getOffset() const { return getEntry().Offset; } 79 80 /// \returns index for the dwarf string. 81 unsigned getIndex() const { 82 assert(getEntry().isIndexed() && "Index is not set!"); 83 return getEntry().Index; 84 } 85 86 /// \returns string. 87 StringRef getString() const { 88 if (MapEntry.is<ByValStringEntryPtr>()) 89 return MapEntry.get<ByValStringEntryPtr>()->first(); 90 91 return MapEntry.get<ByPtrStringEntryPtr>()->first(); 92 } 93 94 /// \returns the entire string pool entry for convenience. 95 const DwarfStringPoolEntry &getEntry() const { 96 if (MapEntry.is<ByValStringEntryPtr>()) 97 return MapEntry.get<ByValStringEntryPtr>()->second; 98 99 return *MapEntry.get<ByPtrStringEntryPtr>()->second; 100 } 101 102 bool operator==(const DwarfStringPoolEntryRef &X) const { 103 return MapEntry.getOpaqueValue() == X.MapEntry.getOpaqueValue(); 104 } 105 106 bool operator!=(const DwarfStringPoolEntryRef &X) const { 107 return MapEntry.getOpaqueValue() != X.MapEntry.getOpaqueValue(); 108 } 109 }; 110 111 } // end namespace llvm 112 113 #endif 114