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"
14#include "llvm/Support/MathExtras.h"
15#include "llvm/Support/ReverseIteration.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(A: NumEntries * 4 / 3 + 1);
29}
30
31static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) {
32 auto **Table = static_cast<StringMapEntryBase **>(safe_calloc(
33 Count: NewNumBuckets + 1, Sz: 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
46uint32_t StringMapImpl::hash(StringRef Key) { return xxh3_64bits(data: Key); }
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.
56 init(Size: getMinBucketToReserveForEntries(NumEntries: InitSize));
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.
86unsigned StringMapImpl::LookupBucketFor(StringRef Name,
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(InitSize: 16);
94 if (shouldReverseIterate())
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
153 if (shouldReverseIterate())
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.
193void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
194 const char *VStr = (char *)V + ItemSize;
195 StringMapEntryBase *V2 = RemoveKey(Key: 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.
202StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
203 int Bucket = FindKey(Key);
204 if (Bucket == -1)
205 return nullptr;
206
207 StringMapEntryBase *Result = TheTable[Bucket];
208 TheTable[Bucket] = getTombstoneVal();
209 --NumItems;
210 ++NumTombstones;
211 assert(NumItems + NumTombstones <= NumBuckets);
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;
225 } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
226 NumBuckets / 8)) {
227 NewSize = NumBuckets;
228 } else {
229 return BucketNo;
230 }
231
232 unsigned NewBucketNo = BucketNo;
233 auto **NewTableArray = createTable(NewNumBuckets: NewSize);
234 unsigned *NewHashArray = getHashTable(TheTable: NewTableArray, NumBuckets: 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(ptr: TheTable);
261
262 TheTable = NewTableArray;
263 NumBuckets = NewSize;
264 NumTombstones = 0;
265 return NewBucketNo;
266}
267