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