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