xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/DwarfStringPoolEntry.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 /// DwarfStringPoolEntry with string keeping externally.
31 struct DwarfStringPoolEntryWithExtString : public DwarfStringPoolEntry {
32   StringRef String;
33 };
34 
35 /// DwarfStringPoolEntryRef: Dwarf string pool entry reference.
36 ///
37 /// Dwarf string pool entry keeps string value and its data.
38 /// There are two variants how data are represented:
39 ///
40 ///   1. String data in pool  - StringMapEntry<DwarfStringPoolEntry>.
41 ///   2. External string data - DwarfStringPoolEntryWithExtString.
42 ///
43 /// The external data variant allows reducing memory usage for the case
44 /// when string pool entry does not have data: string entry does not
45 /// keep any data and so no need to waste space for the full
46 /// DwarfStringPoolEntry. It is recommended to use external variant if not all
47 /// entries of dwarf string pool have corresponding DwarfStringPoolEntry.
48 
49 class DwarfStringPoolEntryRef {
50   /// Pointer type for "By value" string entry.
51   using ByValStringEntryPtr = const StringMapEntry<DwarfStringPoolEntry> *;
52 
53   /// Pointer type for external string entry.
54   using ExtStringEntryPtr = const DwarfStringPoolEntryWithExtString *;
55 
56   /// Pointer to the dwarf string pool Entry.
57   PointerUnion<ByValStringEntryPtr, ExtStringEntryPtr> MapEntry = nullptr;
58 
59 public:
60   DwarfStringPoolEntryRef() = default;
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 
67   /// ASSUMPTION: DwarfStringPoolEntryRef keeps pointer to \p Entry,
68   /// thus specified entry mustn`t be reallocated.
69   DwarfStringPoolEntryRef(const DwarfStringPoolEntryWithExtString &Entry)
70       : MapEntry(&Entry) {}
71 
72   explicit operator bool() const { return !MapEntry.isNull(); }
73 
74   /// \returns symbol for the dwarf string.
75   MCSymbol *getSymbol() const {
76     assert(getEntry().Symbol && "No symbol available!");
77     return getEntry().Symbol;
78   }
79 
80   /// \returns offset for the dwarf string.
81   uint64_t getOffset() const { return getEntry().Offset; }
82 
83   /// \returns index for the dwarf string.
84   unsigned getIndex() const {
85     assert(getEntry().isIndexed() && "Index is not set!");
86     return getEntry().Index;
87   }
88 
89   /// \returns string.
90   StringRef getString() const {
91     if (isa<ByValStringEntryPtr>(MapEntry))
92       return cast<ByValStringEntryPtr>(MapEntry)->first();
93 
94     return cast<ExtStringEntryPtr>(MapEntry)->String;
95   }
96 
97   /// \returns the entire string pool entry for convenience.
98   const DwarfStringPoolEntry &getEntry() const {
99     if (isa<ByValStringEntryPtr>(MapEntry))
100       return cast<ByValStringEntryPtr>(MapEntry)->second;
101 
102     return *cast<ExtStringEntryPtr>(MapEntry);
103   }
104 
105   bool operator==(const DwarfStringPoolEntryRef &X) const {
106     return MapEntry.getOpaqueValue() == X.MapEntry.getOpaqueValue();
107   }
108 
109   bool operator!=(const DwarfStringPoolEntryRef &X) const {
110     return MapEntry.getOpaqueValue() != X.MapEntry.getOpaqueValue();
111   }
112 };
113 
114 } // end namespace llvm
115 
116 #endif
117