blob: 9ac1f867fdd4576e76571d036365504976e48500 [file] [log] [blame]
Chris Lattnerbb28a812007-02-08 19:20:57 +00001//===--- StringMap.cpp - String Hash table map implementation -------------===//
Chris Lattner23d7b362006-10-29 23:42:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner23d7b362006-10-29 23:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbb28a812007-02-08 19:20:57 +000010// This file implements the StringMap class.
Chris Lattner23d7b362006-10-29 23:42:03 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbb28a812007-02-08 19:20:57 +000014#include "llvm/ADT/StringMap.h"
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +000015#include "llvm/ADT/StringExtras.h"
Benjamin Kramere160c532012-08-29 22:57:04 +000016#include "llvm/Support/Compiler.h"
Chris Lattner23d7b362006-10-29 23:42:03 +000017#include <cassert>
18using namespace llvm;
19
Chris Lattnerbb28a812007-02-08 19:20:57 +000020StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
Chris Lattner794a0142007-04-04 00:29:37 +000021 ItemSize = itemSize;
22
23 // If a size is specified, initialize the table with that many buckets.
24 if (InitSize) {
25 init(InitSize);
26 return;
27 }
28
29 // Otherwise, initialize it with zero buckets to avoid the allocation.
30 TheTable = 0;
31 NumBuckets = 0;
32 NumItems = 0;
33 NumTombstones = 0;
34}
35
36void StringMapImpl::init(unsigned InitSize) {
Chris Lattner23d7b362006-10-29 23:42:03 +000037 assert((InitSize & (InitSize-1)) == 0 &&
38 "Init Size must be a power of 2 or zero!");
Chris Lattneref4c9162007-04-03 22:15:38 +000039 NumBuckets = InitSize ? InitSize : 16;
Chris Lattner23d7b362006-10-29 23:42:03 +000040 NumItems = 0;
Chris Lattner44dcd012007-02-11 20:58:00 +000041 NumTombstones = 0;
Chris Lattner23d7b362006-10-29 23:42:03 +000042
Benjamin Kramerc894b022011-12-27 20:35:07 +000043 TheTable = (StringMapEntryBase **)calloc(NumBuckets+1,
44 sizeof(StringMapEntryBase **) +
45 sizeof(unsigned));
46
Chris Lattnera86559e2007-02-11 08:20:35 +000047 // Allocate one extra bucket, set it to look filled so the iterators stop at
48 // end.
Benjamin Kramerc894b022011-12-27 20:35:07 +000049 TheTable[NumBuckets] = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +000050}
51
52
Chris Lattner23d7b362006-10-29 23:42:03 +000053/// LookupBucketFor - Look up the bucket that the specified string should end
54/// up in. If it already exists as a key in the map, the Item pointer for the
55/// specified bucket will be non-null. Otherwise, it will be null. In either
56/// case, the FullHashValue field of the bucket will be set to the hash value
57/// of the string.
Daniel Dunbar2928c832009-11-06 10:58:06 +000058unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
Chris Lattner23d7b362006-10-29 23:42:03 +000059 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +000060 if (HTSize == 0) { // Hash table unallocated so far?
61 init(16);
62 HTSize = NumBuckets;
63 }
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +000064 unsigned FullHashValue = HashString(Name);
Chris Lattner23d7b362006-10-29 23:42:03 +000065 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +000066 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
67
Chris Lattner23d7b362006-10-29 23:42:03 +000068 unsigned ProbeAmt = 1;
Chris Lattner44dcd012007-02-11 20:58:00 +000069 int FirstTombstone = -1;
Chris Lattner23d7b362006-10-29 23:42:03 +000070 while (1) {
Benjamin Kramerc894b022011-12-27 20:35:07 +000071 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattner23d7b362006-10-29 23:42:03 +000072 // If we found an empty bucket, this key isn't in the table yet, return it.
Benjamin Kramere160c532012-08-29 22:57:04 +000073 if (LLVM_LIKELY(BucketItem == 0)) {
Chris Lattner44dcd012007-02-11 20:58:00 +000074 // If we found a tombstone, we want to reuse the tombstone instead of an
75 // empty bucket. This reduces probing.
76 if (FirstTombstone != -1) {
Benjamin Kramerc894b022011-12-27 20:35:07 +000077 HashTable[FirstTombstone] = FullHashValue;
Chris Lattner44dcd012007-02-11 20:58:00 +000078 return FirstTombstone;
79 }
80
Benjamin Kramerc894b022011-12-27 20:35:07 +000081 HashTable[BucketNo] = FullHashValue;
Chris Lattner23d7b362006-10-29 23:42:03 +000082 return BucketNo;
83 }
84
Chris Lattner44dcd012007-02-11 20:58:00 +000085 if (BucketItem == getTombstoneVal()) {
86 // Skip over tombstones. However, remember the first one we see.
87 if (FirstTombstone == -1) FirstTombstone = BucketNo;
Benjamin Kramere160c532012-08-29 22:57:04 +000088 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
Chris Lattner44dcd012007-02-11 20:58:00 +000089 // If the full hash value matches, check deeply for a match. The common
90 // case here is that we are only looking at the buckets (for item info
91 // being non-null and for the full hash value) not at the items. This
92 // is important for cache locality.
93
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000094 // Do the comparison like this because Name isn't necessarily
Chris Lattner23d7b362006-10-29 23:42:03 +000095 // null-terminated!
96 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000097 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattner23d7b362006-10-29 23:42:03 +000098 // We found a match!
99 return BucketNo;
100 }
101 }
102
103 // Okay, we didn't find the item. Probe to the next bucket.
104 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
105
106 // Use quadratic probing, it has fewer clumping artifacts than linear
107 // probing and has good cache behavior in the common case.
108 ++ProbeAmt;
109 }
110}
111
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000112
113/// FindKey - Look up the bucket that contains the specified key. If it exists
114/// in the map, return the bucket number of the key. Otherwise return -1.
115/// This does not modify the map.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000116int StringMapImpl::FindKey(StringRef Key) const {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000117 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +0000118 if (HTSize == 0) return -1; // Really empty table?
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +0000119 unsigned FullHashValue = HashString(Key);
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000120 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000121 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
122
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000123 unsigned ProbeAmt = 1;
124 while (1) {
Benjamin Kramerc894b022011-12-27 20:35:07 +0000125 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000126 // If we found an empty bucket, this key isn't in the table yet, return.
Benjamin Kramere160c532012-08-29 22:57:04 +0000127 if (LLVM_LIKELY(BucketItem == 0))
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000128 return -1;
129
Chris Lattner44dcd012007-02-11 20:58:00 +0000130 if (BucketItem == getTombstoneVal()) {
131 // Ignore tombstones.
Benjamin Kramere160c532012-08-29 22:57:04 +0000132 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
Chris Lattner44dcd012007-02-11 20:58:00 +0000133 // If the full hash value matches, check deeply for a match. The common
134 // case here is that we are only looking at the buckets (for item info
135 // being non-null and for the full hash value) not at the items. This
136 // is important for cache locality.
137
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000138 // Do the comparison like this because NameStart isn't necessarily
139 // null-terminated!
140 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000141 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000142 // We found a match!
143 return BucketNo;
144 }
145 }
146
147 // Okay, we didn't find the item. Probe to the next bucket.
148 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
149
150 // Use quadratic probing, it has fewer clumping artifacts than linear
151 // probing and has good cache behavior in the common case.
152 ++ProbeAmt;
153 }
154}
155
Chris Lattner44dcd012007-02-11 20:58:00 +0000156/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
157/// delete it. This aborts if the value isn't in the table.
158void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
159 const char *VStr = (char*)V + ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000160 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000161 (void)V2;
Chris Lattner44dcd012007-02-11 20:58:00 +0000162 assert(V == V2 && "Didn't find key?");
163}
164
165/// RemoveKey - Remove the StringMapEntry for the specified key from the
166/// table, returning it. If the key is not in the table, this returns null.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000167StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000168 int Bucket = FindKey(Key);
Chris Lattner44dcd012007-02-11 20:58:00 +0000169 if (Bucket == -1) return 0;
170
Benjamin Kramerc894b022011-12-27 20:35:07 +0000171 StringMapEntryBase *Result = TheTable[Bucket];
172 TheTable[Bucket] = getTombstoneVal();
Chris Lattner44dcd012007-02-11 20:58:00 +0000173 --NumItems;
174 ++NumTombstones;
Jakob Stoklund Olesen03ef4492011-03-30 18:32:51 +0000175 assert(NumItems + NumTombstones <= NumBuckets);
176
Chris Lattner44dcd012007-02-11 20:58:00 +0000177 return Result;
178}
179
180
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000181
Chris Lattner23d7b362006-10-29 23:42:03 +0000182/// RehashTable - Grow the table, redistributing values into the buckets with
183/// the appropriate mod-of-hashtable-size.
Chris Lattnerbb28a812007-02-08 19:20:57 +0000184void StringMapImpl::RehashTable() {
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000185 unsigned NewSize;
Benjamin Kramerc894b022011-12-27 20:35:07 +0000186 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000187
188 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
189 // the buckets are empty (meaning that many are filled with tombstones),
190 // grow/rehash the table.
191 if (NumItems*4 > NumBuckets*3) {
192 NewSize = NumBuckets*2;
Chandler Carruth2a791162012-06-19 17:40:35 +0000193 } else if (NumBuckets-(NumItems+NumTombstones) <= NumBuckets/8) {
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000194 NewSize = NumBuckets;
195 } else {
196 return;
197 }
198
Chris Lattnera86559e2007-02-11 08:20:35 +0000199 // Allocate one extra bucket which will always be non-empty. This allows the
200 // iterators to stop at end.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000201 StringMapEntryBase **NewTableArray =
202 (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) +
203 sizeof(unsigned));
204 unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
205 NewTableArray[NewSize] = (StringMapEntryBase*)2;
206
Chris Lattner23d7b362006-10-29 23:42:03 +0000207 // Rehash all the items into their new buckets. Luckily :) we already have
208 // the hash values available, so we don't have to rehash any strings.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000209 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
210 StringMapEntryBase *Bucket = TheTable[I];
211 if (Bucket && Bucket != getTombstoneVal()) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000212 // Fast case, bucket available.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000213 unsigned FullHash = HashTable[I];
Chris Lattner23d7b362006-10-29 23:42:03 +0000214 unsigned NewBucket = FullHash & (NewSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000215 if (NewTableArray[NewBucket] == 0) {
216 NewTableArray[FullHash & (NewSize-1)] = Bucket;
217 NewHashArray[FullHash & (NewSize-1)] = FullHash;
Chris Lattner23d7b362006-10-29 23:42:03 +0000218 continue;
219 }
220
Chris Lattner44dcd012007-02-11 20:58:00 +0000221 // Otherwise probe for a spot.
Chris Lattner23d7b362006-10-29 23:42:03 +0000222 unsigned ProbeSize = 1;
223 do {
224 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000225 } while (NewTableArray[NewBucket]);
Chris Lattner23d7b362006-10-29 23:42:03 +0000226
227 // Finally found a slot. Fill it in.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000228 NewTableArray[NewBucket] = Bucket;
229 NewHashArray[NewBucket] = FullHash;
Chris Lattner23d7b362006-10-29 23:42:03 +0000230 }
231 }
232
Chris Lattner12ba8062007-04-04 17:24:28 +0000233 free(TheTable);
Chris Lattner23d7b362006-10-29 23:42:03 +0000234
235 TheTable = NewTableArray;
236 NumBuckets = NewSize;
Jakob Stoklund Olesen03ef4492011-03-30 18:32:51 +0000237 NumTombstones = 0;
Chris Lattner23d7b362006-10-29 23:42:03 +0000238}