blob: c1f707ce50a5cee8431b36725a0d34529b2e71b8 [file] [log] [blame]
Chris Lattner751a4202007-02-08 19:20:57 +00001//===--- StringMap.cpp - String Hash table map implementation -------------===//
Chris Lattner149e6662006-10-29 23:42:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner149e6662006-10-29 23:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner751a4202007-02-08 19:20:57 +000010// This file implements the StringMap class.
Chris Lattner149e6662006-10-29 23:42:03 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner751a4202007-02-08 19:20:57 +000014#include "llvm/ADT/StringMap.h"
Daniel Dunbar77668002009-10-17 18:21:06 +000015#include "llvm/ADT/StringExtras.h"
Benjamin Kramerffa24e02012-08-29 22:57:04 +000016#include "llvm/Support/Compiler.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000017#include "llvm/Support/DJB.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000018#include "llvm/Support/MathExtras.h"
Chris Lattner149e6662006-10-29 23:42:03 +000019#include <cassert>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000020
Chris Lattner149e6662006-10-29 23:42:03 +000021using namespace llvm;
22
Mehdi Aminibe8a57f2016-03-25 05:57:57 +000023/// Returns the number of buckets to allocate to ensure that the DenseMap can
24/// accommodate \p NumEntries without need to grow().
25static 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
Chris Lattner751a4202007-02-08 19:20:57 +000034StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
Chris Lattner23763462007-04-04 00:29:37 +000035 ItemSize = itemSize;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000036
Chris Lattner23763462007-04-04 00:29:37 +000037 // If a size is specified, initialize the table with that many buckets.
38 if (InitSize) {
Mehdi Aminibe8a57f2016-03-25 05:57:57 +000039 // 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.
42 init(getMinBucketToReserveForEntries(InitSize));
Chris Lattner23763462007-04-04 00:29:37 +000043 return;
44 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000045
Chris Lattner23763462007-04-04 00:29:37 +000046 // Otherwise, initialize it with zero buckets to avoid the allocation.
Craig Topperc10719f2014-04-07 04:17:22 +000047 TheTable = nullptr;
Chris Lattner23763462007-04-04 00:29:37 +000048 NumBuckets = 0;
49 NumItems = 0;
50 NumTombstones = 0;
51}
52
53void StringMapImpl::init(unsigned InitSize) {
Chris Lattner149e6662006-10-29 23:42:03 +000054 assert((InitSize & (InitSize-1)) == 0 &&
55 "Init Size must be a power of 2 or zero!");
Matthias Braunc20b3382017-07-20 01:30:39 +000056
57 unsigned NewNumBuckets = InitSize ? InitSize : 16;
Chris Lattner149e6662006-10-29 23:42:03 +000058 NumItems = 0;
Chris Lattner77baa562007-02-11 20:58:00 +000059 NumTombstones = 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000060
Serge Pavlov76d8cce2018-02-20 05:41:26 +000061 TheTable = static_cast<StringMapEntryBase **>(
Serge Pavlov15681ad2018-06-09 05:19:45 +000062 safe_calloc(NewNumBuckets+1,
Serge Pavlov76d8cce2018-02-20 05:41:26 +000063 sizeof(StringMapEntryBase **) + sizeof(unsigned)));
Matthias Braunc20b3382017-07-20 01:30:39 +000064
65 // Set the member only if TheTable was successfully allocated
66 NumBuckets = NewNumBuckets;
67
Chris Lattnere15605c2007-02-11 08:20:35 +000068 // Allocate one extra bucket, set it to look filled so the iterators stop at
69 // end.
Benjamin Kramer46236ee2011-12-27 20:35:07 +000070 TheTable[NumBuckets] = (StringMapEntryBase*)2;
Chris Lattner149e6662006-10-29 23:42:03 +000071}
72
Chris Lattner149e6662006-10-29 23:42:03 +000073/// 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.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000078unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
Chris Lattner149e6662006-10-29 23:42:03 +000079 unsigned HTSize = NumBuckets;
Chris Lattner23763462007-04-04 00:29:37 +000080 if (HTSize == 0) { // Hash table unallocated so far?
81 init(16);
82 HTSize = NumBuckets;
83 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000084 unsigned FullHashValue = djbHash(Name, 0);
Chris Lattner149e6662006-10-29 23:42:03 +000085 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramer46236ee2011-12-27 20:35:07 +000086 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
87
Chris Lattner149e6662006-10-29 23:42:03 +000088 unsigned ProbeAmt = 1;
Chris Lattner77baa562007-02-11 20:58:00 +000089 int FirstTombstone = -1;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000090 while (true) {
Benjamin Kramer46236ee2011-12-27 20:35:07 +000091 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattner149e6662006-10-29 23:42:03 +000092 // If we found an empty bucket, this key isn't in the table yet, return it.
Craig Topper8d399f82014-04-09 04:20:00 +000093 if (LLVM_LIKELY(!BucketItem)) {
Chris Lattner77baa562007-02-11 20:58:00 +000094 // If we found a tombstone, we want to reuse the tombstone instead of an
95 // empty bucket. This reduces probing.
96 if (FirstTombstone != -1) {
Benjamin Kramer46236ee2011-12-27 20:35:07 +000097 HashTable[FirstTombstone] = FullHashValue;
Chris Lattner77baa562007-02-11 20:58:00 +000098 return FirstTombstone;
99 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000100
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000101 HashTable[BucketNo] = FullHashValue;
Chris Lattner149e6662006-10-29 23:42:03 +0000102 return BucketNo;
103 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000104
Chris Lattner77baa562007-02-11 20:58:00 +0000105 if (BucketItem == getTombstoneVal()) {
106 // Skip over tombstones. However, remember the first one we see.
107 if (FirstTombstone == -1) FirstTombstone = BucketNo;
Benjamin Kramerffa24e02012-08-29 22:57:04 +0000108 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
Chris Lattner77baa562007-02-11 20:58:00 +0000109 // 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.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000113
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000114 // Do the comparison like this because Name isn't necessarily
Chris Lattner149e6662006-10-29 23:42:03 +0000115 // null-terminated!
116 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000117 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattner149e6662006-10-29 23:42:03 +0000118 // We found a match!
119 return BucketNo;
120 }
121 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000122
Chris Lattner149e6662006-10-29 23:42:03 +0000123 // Okay, we didn't find the item. Probe to the next bucket.
124 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000125
Chris Lattner149e6662006-10-29 23:42:03 +0000126 // 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
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000132/// 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.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000135int StringMapImpl::FindKey(StringRef Key) const {
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000136 unsigned HTSize = NumBuckets;
Chris Lattner23763462007-04-04 00:29:37 +0000137 if (HTSize == 0) return -1; // Really empty table?
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000138 unsigned FullHashValue = djbHash(Key, 0);
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000139 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000140 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
141
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000142 unsigned ProbeAmt = 1;
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000143 while (true) {
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000144 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000145 // If we found an empty bucket, this key isn't in the table yet, return.
Craig Topper8d399f82014-04-09 04:20:00 +0000146 if (LLVM_LIKELY(!BucketItem))
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000147 return -1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000148
Chris Lattner77baa562007-02-11 20:58:00 +0000149 if (BucketItem == getTombstoneVal()) {
150 // Ignore tombstones.
Benjamin Kramerffa24e02012-08-29 22:57:04 +0000151 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
Chris Lattner77baa562007-02-11 20:58:00 +0000152 // 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.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000156
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000157 // Do the comparison like this because NameStart isn't necessarily
158 // null-terminated!
159 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000160 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000161 // We found a match!
162 return BucketNo;
163 }
164 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000165
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000166 // Okay, we didn't find the item. Probe to the next bucket.
167 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000168
Chris Lattnerdb08c1b2007-02-11 19:49:41 +0000169 // 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
Chris Lattner77baa562007-02-11 20:58:00 +0000175/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
176/// delete it. This aborts if the value isn't in the table.
177void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
178 const char *VStr = (char*)V + ItemSize;
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000179 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000180 (void)V2;
Chris Lattner77baa562007-02-11 20:58:00 +0000181 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.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000186StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000187 int Bucket = FindKey(Key);
Craig Topperc10719f2014-04-07 04:17:22 +0000188 if (Bucket == -1) return nullptr;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000189
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000190 StringMapEntryBase *Result = TheTable[Bucket];
191 TheTable[Bucket] = getTombstoneVal();
Chris Lattner77baa562007-02-11 20:58:00 +0000192 --NumItems;
193 ++NumTombstones;
Jakob Stoklund Olesen846f9502011-03-30 18:32:51 +0000194 assert(NumItems + NumTombstones <= NumBuckets);
195
Chris Lattner77baa562007-02-11 20:58:00 +0000196 return Result;
197}
198
Chris Lattner149e6662006-10-29 23:42:03 +0000199/// RehashTable - Grow the table, redistributing values into the buckets with
200/// the appropriate mod-of-hashtable-size.
David Blaikie16a9eab2014-06-23 18:28:53 +0000201unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
Jakob Stoklund Olesenf587f442011-03-30 18:32:44 +0000202 unsigned NewSize;
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000203 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
Jakob Stoklund Olesenf587f442011-03-30 18:32:44 +0000204
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.
Benjamin Kramer654a85e2015-02-23 16:41:36 +0000208 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
Jakob Stoklund Olesenf587f442011-03-30 18:32:44 +0000209 NewSize = NumBuckets*2;
Benjamin Kramer654a85e2015-02-23 16:41:36 +0000210 } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
211 NumBuckets / 8)) {
Jakob Stoklund Olesenf587f442011-03-30 18:32:44 +0000212 NewSize = NumBuckets;
213 } else {
David Blaikie16a9eab2014-06-23 18:28:53 +0000214 return BucketNo;
Jakob Stoklund Olesenf587f442011-03-30 18:32:44 +0000215 }
216
David Blaikie16a9eab2014-06-23 18:28:53 +0000217 unsigned NewBucketNo = BucketNo;
Chris Lattnere15605c2007-02-11 08:20:35 +0000218 // Allocate one extra bucket which will always be non-empty. This allows the
219 // iterators to stop at end.
Serge Pavlov76d8cce2018-02-20 05:41:26 +0000220 auto NewTableArray = static_cast<StringMapEntryBase **>(
Serge Pavlov15681ad2018-06-09 05:19:45 +0000221 safe_calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
Matthias Braunc20b3382017-07-20 01:30:39 +0000222
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000223 unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
224 NewTableArray[NewSize] = (StringMapEntryBase*)2;
225
Chris Lattner149e6662006-10-29 23:42:03 +0000226 // 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.
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000228 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
229 StringMapEntryBase *Bucket = TheTable[I];
230 if (Bucket && Bucket != getTombstoneVal()) {
Chris Lattner149e6662006-10-29 23:42:03 +0000231 // Fast case, bucket available.
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000232 unsigned FullHash = HashTable[I];
Chris Lattner149e6662006-10-29 23:42:03 +0000233 unsigned NewBucket = FullHash & (NewSize-1);
Craig Topper8d399f82014-04-09 04:20:00 +0000234 if (!NewTableArray[NewBucket]) {
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000235 NewTableArray[FullHash & (NewSize-1)] = Bucket;
236 NewHashArray[FullHash & (NewSize-1)] = FullHash;
David Blaikie16a9eab2014-06-23 18:28:53 +0000237 if (I == BucketNo)
238 NewBucketNo = NewBucket;
Chris Lattner149e6662006-10-29 23:42:03 +0000239 continue;
240 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000241
Chris Lattner77baa562007-02-11 20:58:00 +0000242 // Otherwise probe for a spot.
Chris Lattner149e6662006-10-29 23:42:03 +0000243 unsigned ProbeSize = 1;
244 do {
245 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000246 } while (NewTableArray[NewBucket]);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000247
Chris Lattner149e6662006-10-29 23:42:03 +0000248 // Finally found a slot. Fill it in.
Benjamin Kramer46236ee2011-12-27 20:35:07 +0000249 NewTableArray[NewBucket] = Bucket;
250 NewHashArray[NewBucket] = FullHash;
David Blaikie16a9eab2014-06-23 18:28:53 +0000251 if (I == BucketNo)
252 NewBucketNo = NewBucket;
Chris Lattner149e6662006-10-29 23:42:03 +0000253 }
254 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000255
Chris Lattnerc770a022007-04-04 17:24:28 +0000256 free(TheTable);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000257
Chris Lattner149e6662006-10-29 23:42:03 +0000258 TheTable = NewTableArray;
259 NumBuckets = NewSize;
Jakob Stoklund Olesen846f9502011-03-30 18:32:51 +0000260 NumTombstones = 0;
David Blaikie16a9eab2014-06-23 18:28:53 +0000261 return NewBucketNo;
Chris Lattner149e6662006-10-29 23:42:03 +0000262}