LLVM  16.0.0git
StringMapEntry.h
Go to the documentation of this file.
1 //===- StringMapEntry.h - String Hash table map interface -------*- 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 /// \file
10 /// This file defines the StringMapEntry class - it is intended to be a low
11 /// dependency implementation detail of StringMap that is more suitable for
12 /// inclusion in public headers than StringMap.h itself is.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ADT_STRINGMAPENTRY_H
17 #define LLVM_ADT_STRINGMAPENTRY_H
18 
19 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/StringRef.h"
22 
23 namespace llvm {
24 
25 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
27  size_t keyLength;
28 
29 public:
30  explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
31 
32  size_t getKeyLength() const { return keyLength; }
33 
34 protected:
35  /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
36  /// could be reused elsewhere, maybe even taking an llvm::function_ref to
37  /// type-erase the allocator and put it in a source file.
38  template <typename AllocatorTy>
39  static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
40  StringRef Key, AllocatorTy &Allocator);
41 };
42 
43 // Define out-of-line to dissuade inlining.
44 template <typename AllocatorTy>
45 void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
46  StringRef Key,
47  AllocatorTy &Allocator) {
48  size_t KeyLength = Key.size();
49 
50  // Allocate a new item with space for the string at the end and a null
51  // terminator.
52  size_t AllocSize = EntrySize + KeyLength + 1;
53  void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
54  assert(Allocation && "Unhandled out-of-memory");
55 
56  // Copy the string information.
57  char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
58  if (KeyLength > 0)
59  ::memcpy(Buffer, Key.data(), KeyLength);
60  Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
61  return Allocation;
62 }
63 
64 /// StringMapEntryStorage - Holds the value in a StringMapEntry.
65 ///
66 /// Factored out into a separate base class to make it easier to specialize.
67 /// This is primarily intended to support StringSet, which doesn't need a value
68 /// stored at all.
69 template <typename ValueTy>
71 public:
72  ValueTy second;
73 
74  explicit StringMapEntryStorage(size_t keyLength)
75  : StringMapEntryBase(keyLength), second() {}
76  template <typename... InitTy>
77  StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
78  : StringMapEntryBase(keyLength),
79  second(std::forward<InitTy>(initVals)...) {}
81 
82  const ValueTy &getValue() const { return second; }
83  ValueTy &getValue() { return second; }
84 
85  void setValue(const ValueTy &V) { second = V; }
86 };
87 
88 template <>
89 class StringMapEntryStorage<std::nullopt_t> : public StringMapEntryBase {
90 public:
91  explicit StringMapEntryStorage(size_t keyLength, std::nullopt_t = None)
92  : StringMapEntryBase(keyLength) {}
94 
95  std::nullopt_t getValue() const { return None; }
96 };
97 
98 /// StringMapEntry - This is used to represent one value that is inserted into
99 /// a StringMap. It contains the Value itself and the key: the string length
100 /// and data.
101 template <typename ValueTy>
102 class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
103 public:
105 
106  using ValueType = ValueTy;
107 
108  StringRef getKey() const {
109  return StringRef(getKeyData(), this->getKeyLength());
110  }
111 
112  /// getKeyData - Return the start of the string data that is the key for this
113  /// value. The string data is always stored immediately after the
114  /// StringMapEntry object.
115  const char *getKeyData() const {
116  return reinterpret_cast<const char *>(this + 1);
117  }
118 
119  StringRef first() const {
120  return StringRef(getKeyData(), this->getKeyLength());
121  }
122 
123  /// Create a StringMapEntry for the specified key construct the value using
124  /// \p InitiVals.
125  template <typename AllocatorTy, typename... InitTy>
126  static StringMapEntry *Create(StringRef key, AllocatorTy &allocator,
127  InitTy &&...initVals) {
129  sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
130  StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
131  }
132 
133  /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
134  /// into a StringMapEntry, return the StringMapEntry itself.
135  static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
136  char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
137  return *reinterpret_cast<StringMapEntry *>(ptr);
138  }
139 
140  /// Destroy - Destroy this StringMapEntry, releasing memory back to the
141  /// specified allocator.
142  template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
143  // Free memory referenced by the item.
144  size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
145  this->~StringMapEntry();
146  allocator.Deallocate(static_cast<void *>(this), AllocSize,
147  alignof(StringMapEntry));
148  }
149 };
150 
151 } // end namespace llvm
152 
153 #endif // LLVM_ADT_STRINGMAPENTRY_H
llvm::StringMapEntry::Create
static StringMapEntry * Create(StringRef key, AllocatorTy &allocator, InitTy &&...initVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
Definition: StringMapEntry.h:126
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::StringMapEntryBase::StringMapEntryBase
StringMapEntryBase(size_t keyLength)
Definition: StringMapEntry.h:30
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:102
llvm::StringMapEntryBase::getKeyLength
size_t getKeyLength() const
Definition: StringMapEntry.h:32
StringRef.h
llvm::StringMapEntry::first
StringRef first() const
Definition: StringMapEntry.h:119
llvm::StringMapEntryStorage
StringMapEntryStorage - Holds the value in a StringMapEntry.
Definition: StringMapEntry.h:70
llvm::StringMapEntry::GetStringMapEntryFromKeyData
static StringMapEntry & GetStringMapEntryFromKeyData(const char *keyData)
GetStringMapEntryFromKeyData - Given key data that is known to be embedded into a StringMapEntry,...
Definition: StringMapEntry.h:135
SpecialSubKind::allocator
@ allocator
llvm::StringMapEntryBase
StringMapEntryBase - Shared base class of StringMapEntry instances.
Definition: StringMapEntry.h:26
llvm::StringMapEntryStorage::getValue
ValueTy & getValue()
Definition: StringMapEntry.h:83
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
STLFunctionalExtras.h
llvm::Comdat
Definition: Comdat.h:33
llvm::StringMapEntryStorage::second
ValueTy second
Definition: StringMapEntry.h:72
llvm::StringMapEntryBase::allocateWithKey
static void * allocateWithKey(size_t EntrySize, size_t EntryAlign, StringRef Key, AllocatorTy &Allocator)
Helper to tail-allocate Key.
Definition: StringMapEntry.h:45
llvm::StringMapEntry::getKeyData
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
Definition: StringMapEntry.h:115
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:53
llvm::StringMapEntryStorage::setValue
void setValue(const ValueTy &V)
Definition: StringMapEntry.h:85
llvm::StringMapEntryStorage::getValue
const ValueTy & getValue() const
Definition: StringMapEntry.h:82
llvm::StringMapEntry::getKey
StringRef getKey() const
Definition: StringMapEntry.h:108
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::StringMapEntry::Destroy
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMapEntry.h:142
llvm::StringMapEntryStorage< std::nullopt_t >::StringMapEntryStorage
StringMapEntryStorage(size_t keyLength, std::nullopt_t=None)
Definition: StringMapEntry.h:91
None.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::StringMapEntryStorage::StringMapEntryStorage
StringMapEntryStorage(size_t keyLength)
Definition: StringMapEntry.h:74
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
std
Definition: BitVector.h:851
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
llvm::StringMapEntryStorage::StringMapEntryStorage
StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
Definition: StringMapEntry.h:77
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:143
entry
print Instructions which execute on loop entry
Definition: MustExecute.cpp:346
llvm::StringMapEntryStorage< std::nullopt_t >::getValue
std::nullopt_t getValue() const
Definition: StringMapEntry.h:95