LLVM 20.0.0git
StringMap.cpp
Go to the documentation of this file.
1//===--- StringMap.cpp - String Hash table map implementation -------------===//
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 implements the StringMap class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/StringMap.h"
16#include "llvm/Support/xxhash.h"
17
18using namespace llvm;
19
20/// Returns the number of buckets to allocate to ensure that the DenseMap can
21/// accommodate \p NumEntries without need to grow().
22static inline unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
23 // Ensure that "NumEntries * 4 < NumBuckets * 3"
24 if (NumEntries == 0)
25 return 0;
26 // +1 is required because of the strict equality.
27 // For example if NumEntries is 48, we need to return 401.
28 return NextPowerOf2(NumEntries * 4 / 3 + 1);
29}
30
31static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) {
32 auto **Table = static_cast<StringMapEntryBase **>(safe_calloc(
33 NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned)));
34
35 // Allocate one extra bucket, set it to look filled so the iterators stop at
36 // end.
37 Table[NewNumBuckets] = (StringMapEntryBase *)2;
38 return Table;
39}
40
41static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
42 unsigned NumBuckets) {
43 return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
44}
45
47
48StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
49 ItemSize = itemSize;
50
51 // If a size is specified, initialize the table with that many buckets.
52 if (InitSize) {
53 // The table will grow when the number of entries reach 3/4 of the number of
54 // buckets. To guarantee that "InitSize" number of entries can be inserted
55 // in the table without growing, we allocate just what is needed here.
57 return;
58 }
59
60 // Otherwise, initialize it with zero buckets to avoid the allocation.
61 TheTable = nullptr;
62 NumBuckets = 0;
63 NumItems = 0;
64 NumTombstones = 0;
65}
66
67void StringMapImpl::init(unsigned InitSize) {
68 assert((InitSize & (InitSize - 1)) == 0 &&
69 "Init Size must be a power of 2 or zero!");
70
71 unsigned NewNumBuckets = InitSize ? InitSize : 16;
72 NumItems = 0;
73 NumTombstones = 0;
74
75 TheTable = createTable(NewNumBuckets);
76
77 // Set the member only if TheTable was successfully allocated
78 NumBuckets = NewNumBuckets;
79}
80
81/// LookupBucketFor - Look up the bucket that the specified string should end
82/// up in. If it already exists as a key in the map, the Item pointer for the
83/// specified bucket will be non-null. Otherwise, it will be null. In either
84/// case, the FullHashValue field of the bucket will be set to the hash value
85/// of the string.
87 uint32_t FullHashValue) {
88#ifdef EXPENSIVE_CHECKS
89 assert(FullHashValue == hash(Name));
90#endif
91 // Hash table unallocated so far?
92 if (NumBuckets == 0)
93 init(16);
95 FullHashValue = ~FullHashValue;
96 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
97 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
98
99 unsigned ProbeAmt = 1;
100 int FirstTombstone = -1;
101 while (true) {
102 StringMapEntryBase *BucketItem = TheTable[BucketNo];
103 // If we found an empty bucket, this key isn't in the table yet, return it.
104 if (LLVM_LIKELY(!BucketItem)) {
105 // If we found a tombstone, we want to reuse the tombstone instead of an
106 // empty bucket. This reduces probing.
107 if (FirstTombstone != -1) {
108 HashTable[FirstTombstone] = FullHashValue;
109 return FirstTombstone;
110 }
111
112 HashTable[BucketNo] = FullHashValue;
113 return BucketNo;
114 }
115
116 if (BucketItem == getTombstoneVal()) {
117 // Skip over tombstones. However, remember the first one we see.
118 if (FirstTombstone == -1)
119 FirstTombstone = BucketNo;
120 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
121 // If the full hash value matches, check deeply for a match. The common
122 // case here is that we are only looking at the buckets (for item info
123 // being non-null and for the full hash value) not at the items. This
124 // is important for cache locality.
125
126 // Do the comparison like this because Name isn't necessarily
127 // null-terminated!
128 char *ItemStr = (char *)BucketItem + ItemSize;
129 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
130 // We found a match!
131 return BucketNo;
132 }
133 }
134
135 // Okay, we didn't find the item. Probe to the next bucket.
136 BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
137
138 // Use quadratic probing, it has fewer clumping artifacts than linear
139 // probing and has good cache behavior in the common case.
140 ++ProbeAmt;
141 }
142}
143
144/// FindKey - Look up the bucket that contains the specified key. If it exists
145/// in the map, return the bucket number of the key. Otherwise return -1.
146/// This does not modify the map.
147int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
148 if (NumBuckets == 0)
149 return -1; // Really empty table?
150#ifdef EXPENSIVE_CHECKS
151 assert(FullHashValue == hash(Key));
152#endif
154 FullHashValue = ~FullHashValue;
155 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
156 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
157
158 unsigned ProbeAmt = 1;
159 while (true) {
160 StringMapEntryBase *BucketItem = TheTable[BucketNo];
161 // If we found an empty bucket, this key isn't in the table yet, return.
162 if (LLVM_LIKELY(!BucketItem))
163 return -1;
164
165 if (BucketItem == getTombstoneVal()) {
166 // Ignore tombstones.
167 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
168 // If the full hash value matches, check deeply for a match. The common
169 // case here is that we are only looking at the buckets (for item info
170 // being non-null and for the full hash value) not at the items. This
171 // is important for cache locality.
172
173 // Do the comparison like this because NameStart isn't necessarily
174 // null-terminated!
175 char *ItemStr = (char *)BucketItem + ItemSize;
176 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
177 // We found a match!
178 return BucketNo;
179 }
180 }
181
182 // Okay, we didn't find the item. Probe to the next bucket.
183 BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
184
185 // Use quadratic probing, it has fewer clumping artifacts than linear
186 // probing and has good cache behavior in the common case.
187 ++ProbeAmt;
188 }
189}
190
191/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
192/// delete it. This aborts if the value isn't in the table.
194 const char *VStr = (char *)V + ItemSize;
195 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
196 (void)V2;
197 assert(V == V2 && "Didn't find key?");
198}
199
200/// RemoveKey - Remove the StringMapEntry for the specified key from the
201/// table, returning it. If the key is not in the table, this returns null.
203 int Bucket = FindKey(Key);
204 if (Bucket == -1)
205 return nullptr;
206
207 StringMapEntryBase *Result = TheTable[Bucket];
208 TheTable[Bucket] = getTombstoneVal();
209 --NumItems;
212
213 return Result;
214}
215
216/// RehashTable - Grow the table, redistributing values into the buckets with
217/// the appropriate mod-of-hashtable-size.
218unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
219 unsigned NewSize;
220 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
221 // the buckets are empty (meaning that many are filled with tombstones),
222 // grow/rehash the table.
223 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
224 NewSize = NumBuckets * 2;
226 NumBuckets / 8)) {
227 NewSize = NumBuckets;
228 } else {
229 return BucketNo;
230 }
231
232 unsigned NewBucketNo = BucketNo;
233 auto **NewTableArray = createTable(NewSize);
234 unsigned *NewHashArray = getHashTable(NewTableArray, NewSize);
235 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
236
237 // Rehash all the items into their new buckets. Luckily :) we already have
238 // the hash values available, so we don't have to rehash any strings.
239 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
240 StringMapEntryBase *Bucket = TheTable[I];
241 if (Bucket && Bucket != getTombstoneVal()) {
242 // If the bucket is not available, probe for a spot.
243 unsigned FullHash = HashTable[I];
244 unsigned NewBucket = FullHash & (NewSize - 1);
245 if (NewTableArray[NewBucket]) {
246 unsigned ProbeSize = 1;
247 do {
248 NewBucket = (NewBucket + ProbeSize++) & (NewSize - 1);
249 } while (NewTableArray[NewBucket]);
250 }
251
252 // Finally found a slot. Fill it in.
253 NewTableArray[NewBucket] = Bucket;
254 NewHashArray[NewBucket] = FullHash;
255 if (I == BucketNo)
256 NewBucketNo = NewBucket;
257 }
258 }
259
260 free(TheTable);
261
262 TheTable = NewTableArray;
263 NumBuckets = NewSize;
264 NumTombstones = 0;
265 return NewBucketNo;
266}
This file defines the StringMap class.
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:237
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:236
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringMapEntryBase ** createTable(unsigned NewNumBuckets)
Definition: StringMap.cpp:31
static unsigned * getHashTable(StringMapEntryBase **TheTable, unsigned NumBuckets)
Definition: StringMap.cpp:41
static unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition: StringMap.cpp:22
StringMapEntryBase - Shared base class of StringMapEntry instances.
size_t getKeyLength() const
static StringMapEntryBase * getTombstoneVal()
Definition: StringMap.h:96
unsigned ItemSize
Definition: StringMap.h:41
unsigned LookupBucketFor(StringRef Key)
LookupBucketFor - Look up the bucket that the specified string should end up in.
Definition: StringMap.h:64
unsigned RehashTable(unsigned BucketNo=0)
RehashTable - Grow the table, redistributing values into the buckets with the appropriate mod-of-hash...
Definition: StringMap.cpp:218
unsigned NumItems
Definition: StringMap.h:39
void RemoveKey(StringMapEntryBase *V)
RemoveKey - Remove the specified StringMapEntry from the table, but do not delete it.
Definition: StringMap.cpp:193
StringMapEntryBase ** TheTable
Definition: StringMap.h:37
StringMapImpl(unsigned itemSize)
Definition: StringMap.h:44
void init(unsigned Size)
Allocate the table with the specified number of buckets and otherwise setup the map as empty.
Definition: StringMap.cpp:67
static uint32_t hash(StringRef Key)
Returns the hash value that will be used for the given string.
Definition: StringMap.cpp:46
unsigned NumBuckets
Definition: StringMap.h:38
unsigned NumTombstones
Definition: StringMap.h:40
int FindKey(StringRef Key) const
FindKey - Look up the bucket that contains the specified key.
Definition: StringMap.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint64_t xxh3_64bits(ArrayRef< uint8_t > data)
Definition: xxhash.cpp:553
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_calloc(size_t Count, size_t Sz)
Definition: MemAlloc.h:38
bool shouldReverseIterate()
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:382