LLVM 23.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 // If a size is specified, initialize the table with that many buckets.
51 if (InitSize) {
52 // The table will grow when the number of entries reach 3/4 of the number of
53 // buckets. To guarantee that "InitSize" number of entries can be inserted
54 // in the table without growing, we allocate just what is needed here.
56 }
57}
58
59void StringMapImpl::init(unsigned InitSize) {
60 assert((InitSize & (InitSize - 1)) == 0 &&
61 "Init Size must be a power of 2 or zero!");
62
63 unsigned NewNumBuckets = InitSize ? InitSize : 16;
64 NumItems = 0;
65
66 TheTable = createTable(NewNumBuckets);
67
68 // Set the member only if TheTable was successfully allocated
69 NumBuckets = NewNumBuckets;
70}
71
72/// LookupBucketFor - Look up the bucket that the specified string should end
73/// up in. If it already exists as a key in the map, the Item pointer for the
74/// specified bucket will be non-null. Otherwise, it will be null. In either
75/// case, the FullHashValue field of the bucket will be set to the hash value
76/// of the string.
78 uint32_t FullHashValue) {
79#ifdef EXPENSIVE_CHECKS
80 assert(FullHashValue == hash(Name));
81#endif
82 // Hash table unallocated so far?
83 if (NumBuckets == 0)
84 init(16);
85 if constexpr (shouldReverseIterate())
86 FullHashValue = ~FullHashValue;
87 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
88 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
89
90 while (true) {
91 StringMapEntryBase *BucketItem = TheTable[BucketNo];
92 // If we found an empty bucket, this key isn't in the table yet, return it.
93 if (LLVM_LIKELY(!BucketItem)) {
94 HashTable[BucketNo] = FullHashValue;
95 return BucketNo;
96 }
97
98 if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
99 // If the full hash value matches, check deeply for a match. The common
100 // case here is that we are only looking at the buckets (for item info
101 // being non-null and for the full hash value) not at the items. This
102 // is important for cache locality.
103
104 // Do the comparison like this because Name isn't necessarily
105 // null-terminated!
106 char *ItemStr = (char *)BucketItem + ItemSize;
107 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
108 // We found a match!
109 return BucketNo;
110 }
111 }
112
113 // Okay, we didn't find the item. Probe to the next bucket.
114 BucketNo = (BucketNo + 1) & (NumBuckets - 1);
115 }
116}
117
118/// FindKey - Look up the bucket that contains the specified key. If it exists
119/// in the map, return the bucket number of the key. Otherwise return -1.
120/// This does not modify the map.
121int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
122 if (NumBuckets == 0)
123 return -1; // Really empty table?
124#ifdef EXPENSIVE_CHECKS
125 assert(FullHashValue == hash(Key));
126#endif
127 if constexpr (shouldReverseIterate())
128 FullHashValue = ~FullHashValue;
129 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
130 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
131
132 while (true) {
133 StringMapEntryBase *BucketItem = TheTable[BucketNo];
134 // If we found an empty bucket, this key isn't in the table yet, return.
135 if (LLVM_LIKELY(!BucketItem))
136 return -1;
137
138 if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
139 // If the full hash value matches, check deeply for a match. The common
140 // case here is that we are only looking at the buckets (for item info
141 // being non-null and for the full hash value) not at the items. This
142 // is important for cache locality.
143
144 // Do the comparison like this because NameStart isn't necessarily
145 // null-terminated!
146 char *ItemStr = (char *)BucketItem + ItemSize;
147 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
148 // We found a match!
149 return BucketNo;
150 }
151 }
152
153 // Okay, we didn't find the item. Probe to the next bucket.
154 BucketNo = (BucketNo + 1) & (NumBuckets - 1);
155 }
156}
157
158/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
159/// delete it. This aborts if the value isn't in the table.
161 const char *VStr = (char *)V + ItemSize;
162 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
163 (void)V2;
164 assert(V == V2 && "Didn't find key?");
165}
166
167// Remove the StringMapEntry for the specified key from the table. Knuth
168// TAOCP 6.4 Algorithm R: walk forward sliding each following entry whose probe
169// path crosses the hole.
170void StringMapImpl::removeBucket(unsigned Bucket) {
171 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
172 unsigned Mask = NumBuckets - 1;
173 unsigned I = Bucket, J = I;
174 while ((J = (J + 1) & Mask), TheTable[J]) {
175 unsigned Ideal = HashTable[J];
176 if (((I - Ideal) & Mask) < ((J - Ideal) & Mask)) {
177 TheTable[I] = TheTable[J];
178 HashTable[I] = HashTable[J];
179 I = J;
180 }
181 }
182 TheTable[I] = nullptr;
183 --NumItems;
184}
185
187 int Bucket = FindKey(Key);
188 if (Bucket == -1)
189 return nullptr;
190
191 StringMapEntryBase *Result = TheTable[Bucket];
192 removeBucket(Bucket);
193 return Result;
194}
195
196/// RehashTable - Grow the table, redistributing values into the buckets with
197/// the appropriate mod-of-hashtable-size.
198unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
199 unsigned NewSize;
200 // If the hash table is now more than 3/4 full, grow the table.
201 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
202 NewSize = NumBuckets * 2;
203 } else {
204 return BucketNo;
205 }
206
207 unsigned NewBucketNo = BucketNo;
208 auto **NewTableArray = createTable(NewSize);
209 unsigned *NewHashArray = getHashTable(NewTableArray, NewSize);
210 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
211
212 // Rehash all the items into their new buckets. Luckily :) we already have
213 // the hash values available, so we don't have to rehash any strings.
214 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
215 StringMapEntryBase *Bucket = TheTable[I];
216 if (Bucket) {
217 // If the bucket is not available, probe for a spot.
218 unsigned FullHash = HashTable[I];
219 unsigned NewBucket = FullHash & (NewSize - 1);
220 while (NewTableArray[NewBucket])
221 NewBucket = (NewBucket + 1) & (NewSize - 1);
222
223 // Finally found a slot. Fill it in.
224 NewTableArray[NewBucket] = Bucket;
225 NewHashArray[NewBucket] = FullHash;
226 if (I == BucketNo)
227 NewBucketNo = NewBucket;
228 }
229 }
230
231 free(TheTable);
232
233 TheTable = NewTableArray;
234 NumBuckets = NewSize;
235 return NewBucketNo;
236}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
#define I(x, y, z)
Definition MD5.cpp:57
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
unsigned LookupBucketFor(StringRef Key)
LookupBucketFor - Look up the bucket that the specified string should end up in.
Definition StringMap.h:63
LLVM_ABI unsigned RehashTable(unsigned BucketNo=0)
RehashTable - Grow the table, redistributing values into the buckets with the appropriate mod-of-hash...
LLVM_ABI void RemoveKey(StringMapEntryBase *V)
RemoveKey - Remove the specified StringMapEntry from the table, but do not delete it.
StringMapEntryBase ** TheTable
Definition StringMap.h:39
LLVM_ABI void removeBucket(unsigned Bucket)
Remove the entry at the given (live) bucket, whose value the caller has already destroyed,...
StringMapImpl(unsigned itemSize)
Definition StringMap.h:45
LLVM_ABI void init(unsigned Size)
Allocate the table with the specified number of buckets and otherwise setup the map as empty.
Definition StringMap.cpp:59
static LLVM_ABI 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:40
int FindKey(StringRef Key) const
FindKey - Look up the bucket that contains the specified key.
Definition StringMap.h:73
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
uint64_t xxh3_64bits(ArrayRef< uint8_t > data)
Inline ArrayRef overloads of the xxhash entry points declared out-of-line in llvm/Support/xxhash....
Definition ArrayRef.h:558
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_calloc(size_t Count, size_t Sz)
Definition MemAlloc.h:38
constexpr bool shouldReverseIterate()
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
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:373