LLVM 24.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
17using namespace llvm;
18
19/// Returns the number of buckets to allocate to ensure that the DenseMap can
20/// accommodate \p NumEntries without need to grow().
21static inline unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
22 // Ensure that "NumEntries * 4 < NumBuckets * 3"
23 if (NumEntries == 0)
24 return 0;
25 // +1 is required because of the strict equality.
26 // For example if NumEntries is 48, we need to return 401.
27 return NextPowerOf2(NumEntries * 4 / 3 + 1);
28}
29
30static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) {
31 auto **Table = static_cast<StringMapEntryBase **>(safe_calloc(
32 NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned)));
33
34 // Allocate one extra bucket, set it to look filled so the iterators stop at
35 // end.
36 Table[NewNumBuckets] = (StringMapEntryBase *)2;
37 return Table;
38}
39
40static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
41 unsigned NumBuckets) {
42 return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
43}
44
46
47StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize)
48 : ItemSize(itemSize) {
49 // If a size is specified, initialize the table with that many buckets.
50 if (InitSize) {
51 // The table will grow when the number of entries reach 3/4 of the number of
52 // buckets. To guarantee that "InitSize" number of entries can be inserted
53 // in the table without growing, we allocate just what is needed here.
55 }
56}
57
58void StringMapImpl::init(unsigned InitSize) {
59 assert((InitSize & (InitSize - 1)) == 0 &&
60 "Init Size must be a power of 2 or zero!");
61
62 unsigned NewNumBuckets = InitSize ? InitSize : 16;
63 NumItems = 0;
64
65 TheTable = createTable(NewNumBuckets);
66
67 // Set the member only if TheTable was successfully allocated
68 NumBuckets = NewNumBuckets;
69}
70
71/// LookupBucketFor - Look up the bucket that the specified string should end
72/// up in. If it already exists as a key in the map, the Item pointer for the
73/// specified bucket will be non-null. Otherwise, it will be null. In either
74/// case, the FullHashValue field of the bucket will be set to the hash value
75/// of the string.
77 uint32_t FullHashValue) {
78#ifdef EXPENSIVE_CHECKS
79 assert(FullHashValue == hash(Name));
80#endif
81 // Hash table unallocated so far?
82 if (NumBuckets == 0)
83 init(16);
84 if constexpr (shouldReverseIterate())
85 FullHashValue = ~FullHashValue;
86 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
87 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
88
89 while (true) {
90 StringMapEntryBase *BucketItem = TheTable[BucketNo];
91 // If we found an empty bucket, this key isn't in the table yet, return it.
92 if (LLVM_LIKELY(!BucketItem)) {
93 HashTable[BucketNo] = FullHashValue;
94 return BucketNo;
95 }
96
97 if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
98 // If the full hash value matches, check deeply for a match. The common
99 // case here is that we are only looking at the buckets (for item info
100 // being non-null and for the full hash value) not at the items. This
101 // is important for cache locality.
102
103 // Do the comparison like this because Name isn't necessarily
104 // null-terminated!
105 char *ItemStr = (char *)BucketItem + ItemSize;
106 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
107 // We found a match!
108 return BucketNo;
109 }
110 }
111
112 // Okay, we didn't find the item. Probe to the next bucket.
113 BucketNo = (BucketNo + 1) & (NumBuckets - 1);
114 }
115}
116
117/// FindKey - Look up the bucket that contains the specified key. If it exists
118/// in the map, return the bucket number of the key. Otherwise return -1.
119/// This does not modify the map.
120int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
121 if (NumBuckets == 0)
122 return -1; // Really empty table?
123#ifdef EXPENSIVE_CHECKS
124 assert(FullHashValue == hash(Key));
125#endif
126 if constexpr (shouldReverseIterate())
127 FullHashValue = ~FullHashValue;
128 unsigned BucketNo = FullHashValue & (NumBuckets - 1);
129 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
130
131 while (true) {
132 StringMapEntryBase *BucketItem = TheTable[BucketNo];
133 // If we found an empty bucket, this key isn't in the table yet, return.
134 if (LLVM_LIKELY(!BucketItem))
135 return -1;
136
137 if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
138 // If the full hash value matches, check deeply for a match. The common
139 // case here is that we are only looking at the buckets (for item info
140 // being non-null and for the full hash value) not at the items. This
141 // is important for cache locality.
142
143 // Do the comparison like this because NameStart isn't necessarily
144 // null-terminated!
145 char *ItemStr = (char *)BucketItem + ItemSize;
146 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
147 // We found a match!
148 return BucketNo;
149 }
150 }
151
152 // Okay, we didn't find the item. Probe to the next bucket.
153 BucketNo = (BucketNo + 1) & (NumBuckets - 1);
154 }
155}
156
157/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
158/// delete it. This aborts if the value isn't in the table.
160 const char *VStr = (char *)V + ItemSize;
161 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
162 (void)V2;
163 assert(V == V2 && "Didn't find key?");
164}
165
166// Knuth TAOCP 6.4 Algorithm R: walk forward sliding each following entry
167// whose probe path crosses the hole.
168void StringMapImpl::removeBucket(unsigned Bucket) {
169 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
170 unsigned Mask = NumBuckets - 1;
171 unsigned I = Bucket, J = I;
172 while ((J = (J + 1) & Mask), TheTable[J]) {
173 unsigned Ideal = HashTable[J];
174 if (((I - Ideal) & Mask) < ((J - Ideal) & Mask)) {
175 TheTable[I] = TheTable[J];
176 HashTable[I] = HashTable[J];
177 I = J;
178 }
179 }
180 TheTable[I] = nullptr;
181 --NumItems;
182}
183
185 int Bucket = FindKey(Key);
186 if (Bucket == -1)
187 return nullptr;
188
189 StringMapEntryBase *Result = TheTable[Bucket];
190 removeBucket(Bucket);
191 return Result;
192}
193
194/// RehashTable - Grow the table, redistributing values into the buckets with
195/// the appropriate mod-of-hashtable-size.
196unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
197 unsigned NewSize;
198 // If the hash table is now more than 3/4 full, grow the table.
199 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
200 NewSize = NumBuckets * 2;
201 } else {
202 return BucketNo;
203 }
204
205 unsigned NewBucketNo = BucketNo;
206 auto **NewTableArray = createTable(NewSize);
207 unsigned *NewHashArray = getHashTable(NewTableArray, NewSize);
208 unsigned *HashTable = getHashTable(TheTable, NumBuckets);
209
210 // Rehash all the items into their new buckets. Luckily :) we already have
211 // the hash values available, so we don't have to rehash any strings.
212 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
213 StringMapEntryBase *Bucket = TheTable[I];
214 if (Bucket) {
215 // If the bucket is not available, probe for a spot.
216 unsigned FullHash = HashTable[I];
217 unsigned NewBucket = FullHash & (NewSize - 1);
218 while (NewTableArray[NewBucket])
219 NewBucket = (NewBucket + 1) & (NewSize - 1);
220
221 // Finally found a slot. Fill it in.
222 NewTableArray[NewBucket] = Bucket;
223 NewHashArray[NewBucket] = FullHash;
224 if (I == BucketNo)
225 NewBucketNo = NewBucket;
226 }
227 }
228
229 free(TheTable);
230
231 TheTable = NewTableArray;
232 NumBuckets = NewSize;
233 return NewBucketNo;
234}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:344
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:343
#define I(x, y, z)
Definition MD5.cpp:57
static StringMapEntryBase ** createTable(unsigned NewNumBuckets)
Definition StringMap.cpp:30
static unsigned * getHashTable(StringMapEntryBase **TheTable, unsigned NumBuckets)
Definition StringMap.cpp:40
static unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition StringMap.cpp:21
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 pointer at the given (live) bucket without destroying the entry, and close the hole ...
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:58
static LLVM_ABI uint32_t hash(StringRef Key)
Returns the hash value that will be used for the given string.
Definition StringMap.cpp:45
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:368