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