/home/arjun/llvm-project/llvm/lib/Support/StringMap.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 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" | 
| 16 |  | #include "llvm/Support/MathExtras.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 | 0 | static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { | 
| 23 | 0 |   // Ensure that "NumEntries * 4 < NumBuckets * 3" | 
| 24 | 0 |   if (NumEntries == 0) | 
| 25 | 0 |     return 0; | 
| 26 | 0 |   // +1 is required because of the strict equality. | 
| 27 | 0 |   // For example if NumEntries is 48, we need to return 401. | 
| 28 | 0 |   return NextPowerOf2(NumEntries * 4 / 3 + 1); | 
| 29 | 0 | } | 
| 30 |  |  | 
| 31 | 0 | StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) { | 
| 32 | 0 |   ItemSize = itemSize; | 
| 33 | 0 | 
 | 
| 34 | 0 |   // If a size is specified, initialize the table with that many buckets. | 
| 35 | 0 |   if (InitSize) { | 
| 36 | 0 |     // The table will grow when the number of entries reach 3/4 of the number of | 
| 37 | 0 |     // buckets. To guarantee that "InitSize" number of entries can be inserted | 
| 38 | 0 |     // in the table without growing, we allocate just what is needed here. | 
| 39 | 0 |     init(getMinBucketToReserveForEntries(InitSize)); | 
| 40 | 0 |     return; | 
| 41 | 0 |   } | 
| 42 | 0 |  | 
| 43 | 0 |   // Otherwise, initialize it with zero buckets to avoid the allocation. | 
| 44 | 0 |   TheTable = nullptr; | 
| 45 | 0 |   NumBuckets = 0; | 
| 46 | 0 |   NumItems = 0; | 
| 47 | 0 |   NumTombstones = 0; | 
| 48 | 0 | } | 
| 49 |  |  | 
| 50 | 4 | void StringMapImpl::init(unsigned InitSize) { | 
| 51 | 4 |   assert((InitSize & (InitSize - 1)) == 0 && | 
| 52 | 4 |          "Init Size must be a power of 2 or zero!"); | 
| 53 | 4 |  | 
| 54 | 4 |   unsigned NewNumBuckets = InitSize ? InitSize : 16; | 
| 55 | 4 |   NumItems = 0; | 
| 56 | 4 |   NumTombstones = 0; | 
| 57 | 4 |  | 
| 58 | 4 |   TheTable = static_cast<StringMapEntryBase **>(safe_calloc( | 
| 59 | 4 |       NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned))); | 
| 60 | 4 |  | 
| 61 | 4 |   // Set the member only if TheTable was successfully allocated | 
| 62 | 4 |   NumBuckets = NewNumBuckets; | 
| 63 | 4 |  | 
| 64 | 4 |   // Allocate one extra bucket, set it to look filled so the iterators stop at | 
| 65 | 4 |   // end. | 
| 66 | 4 |   TheTable[NumBuckets] = (StringMapEntryBase *)2; | 
| 67 | 4 | } | 
| 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. | 
| 74 | 40 | unsigned StringMapImpl::LookupBucketFor(StringRef Name) { | 
| 75 | 40 |   unsigned HTSize = NumBuckets; | 
| 76 | 40 |   if (HTSize == 0) { // Hash table unallocated so far? | 
| 77 | 4 |     init(16); | 
| 78 | 4 |     HTSize = NumBuckets; | 
| 79 | 4 |   } | 
| 80 | 40 |   unsigned FullHashValue = djbHash(Name, 0); | 
| 81 | 40 |   unsigned BucketNo = FullHashValue & (HTSize - 1); | 
| 82 | 40 |   unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
| 83 | 40 |  | 
| 84 | 40 |   unsigned ProbeAmt = 1; | 
| 85 | 40 |   int FirstTombstone = -1; | 
| 86 | 66 |   while (true) { | 
| 87 | 66 |     StringMapEntryBase *BucketItem = TheTable[BucketNo]; | 
| 88 | 66 |     // If we found an empty bucket, this key isn't in the table yet, return it. | 
| 89 | 66 |     if (LLVM_LIKELY(!BucketItem)) { | 
| 90 | 40 |       // If we found a tombstone, we want to reuse the tombstone instead of an | 
| 91 | 40 |       // empty bucket.  This reduces probing. | 
| 92 | 40 |       if (FirstTombstone != -1) { | 
| 93 | 0 |         HashTable[FirstTombstone] = FullHashValue; | 
| 94 | 0 |         return FirstTombstone; | 
| 95 | 0 |       } | 
| 96 | 40 |  | 
| 97 | 40 |       HashTable[BucketNo] = FullHashValue; | 
| 98 | 40 |       return BucketNo; | 
| 99 | 40 |     } | 
| 100 | 26 |  | 
| 101 | 26 |     if (BucketItem == getTombstoneVal()) { | 
| 102 | 0 |       // Skip over tombstones.  However, remember the first one we see. | 
| 103 | 0 |       if (FirstTombstone == -1) | 
| 104 | 0 |         FirstTombstone = BucketNo; | 
| 105 | 26 |     } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { | 
| 106 | 0 |       // If the full hash value matches, check deeply for a match.  The common | 
| 107 | 0 |       // case here is that we are only looking at the buckets (for item info | 
| 108 | 0 |       // being non-null and for the full hash value) not at the items.  This | 
| 109 | 0 |       // is important for cache locality. | 
| 110 | 0 | 
 | 
| 111 | 0 |       // Do the comparison like this because Name isn't necessarily | 
| 112 | 0 |       // null-terminated! | 
| 113 | 0 |       char *ItemStr = (char *)BucketItem + ItemSize; | 
| 114 | 0 |       if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) { | 
| 115 | 0 |         // We found a match! | 
| 116 | 0 |         return BucketNo; | 
| 117 | 0 |       } | 
| 118 | 26 |     } | 
| 119 | 26 |  | 
| 120 | 26 |     // Okay, we didn't find the item.  Probe to the next bucket. | 
| 121 | 26 |     BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1); | 
| 122 | 26 |  | 
| 123 | 26 |     // Use quadratic probing, it has fewer clumping artifacts than linear | 
| 124 | 26 |     // probing and has good cache behavior in the common case. | 
| 125 | 26 |     ++ProbeAmt; | 
| 126 | 26 |   } | 
| 127 | 40 | } | 
| 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. | 
| 132 | 4 | int StringMapImpl::FindKey(StringRef Key) const { | 
| 133 | 4 |   unsigned HTSize = NumBuckets; | 
| 134 | 4 |   if (HTSize == 0) | 
| 135 | 0 |     return -1; // Really empty table? | 
| 136 | 4 |   unsigned FullHashValue = djbHash(Key, 0); | 
| 137 | 4 |   unsigned BucketNo = FullHashValue & (HTSize - 1); | 
| 138 | 4 |   unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
| 139 | 4 |  | 
| 140 | 4 |   unsigned ProbeAmt = 1; | 
| 141 | 4 |   while (true) { | 
| 142 | 4 |     StringMapEntryBase *BucketItem = TheTable[BucketNo]; | 
| 143 | 4 |     // If we found an empty bucket, this key isn't in the table yet, return. | 
| 144 | 4 |     if (LLVM_LIKELY(!BucketItem)) | 
| 145 | 4 |       return -1; | 
| 146 | 0 |  | 
| 147 | 0 |     if (BucketItem == getTombstoneVal()) { | 
| 148 | 0 |       // Ignore tombstones. | 
| 149 | 0 |     } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { | 
| 150 | 0 |       // If the full hash value matches, check deeply for a match.  The common | 
| 151 | 0 |       // case here is that we are only looking at the buckets (for item info | 
| 152 | 0 |       // being non-null and for the full hash value) not at the items.  This | 
| 153 | 0 |       // is important for cache locality. | 
| 154 | 0 | 
 | 
| 155 | 0 |       // Do the comparison like this because NameStart isn't necessarily | 
| 156 | 0 |       // null-terminated! | 
| 157 | 0 |       char *ItemStr = (char *)BucketItem + ItemSize; | 
| 158 | 0 |       if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) { | 
| 159 | 0 |         // We found a match! | 
| 160 | 0 |         return BucketNo; | 
| 161 | 0 |       } | 
| 162 | 0 |     } | 
| 163 | 0 |  | 
| 164 | 0 |     // Okay, we didn't find the item.  Probe to the next bucket. | 
| 165 | 0 |     BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1); | 
| 166 | 0 | 
 | 
| 167 | 0 |     // Use quadratic probing, it has fewer clumping artifacts than linear | 
| 168 | 0 |     // probing and has good cache behavior in the common case. | 
| 169 | 0 |     ++ProbeAmt; | 
| 170 | 0 |   } | 
| 171 | 4 | } | 
| 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. | 
| 175 | 0 | void StringMapImpl::RemoveKey(StringMapEntryBase *V) { | 
| 176 | 0 |   const char *VStr = (char *)V + ItemSize; | 
| 177 | 0 |   StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength())); | 
| 178 | 0 |   (void)V2; | 
| 179 | 0 |   assert(V == V2 && "Didn't find key?"); | 
| 180 | 0 | } | 
| 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. | 
| 184 | 0 | StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) { | 
| 185 | 0 |   int Bucket = FindKey(Key); | 
| 186 | 0 |   if (Bucket == -1) | 
| 187 | 0 |     return nullptr; | 
| 188 | 0 |  | 
| 189 | 0 |   StringMapEntryBase *Result = TheTable[Bucket]; | 
| 190 | 0 |   TheTable[Bucket] = getTombstoneVal(); | 
| 191 | 0 |   --NumItems; | 
| 192 | 0 |   ++NumTombstones; | 
| 193 | 0 |   assert(NumItems + NumTombstones <= NumBuckets); | 
| 194 | 0 | 
 | 
| 195 | 0 |   return Result; | 
| 196 | 0 | } | 
| 197 |  |  | 
| 198 |  | /// RehashTable - Grow the table, redistributing values into the buckets with | 
| 199 |  | /// the appropriate mod-of-hashtable-size. | 
| 200 | 40 | unsigned StringMapImpl::RehashTable(unsigned BucketNo) { | 
| 201 | 40 |   unsigned NewSize; | 
| 202 | 40 |   unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); | 
| 203 | 40 |  | 
| 204 | 40 |   // If the hash table is now more than 3/4 full, or if fewer than 1/8 of | 
| 205 | 40 |   // the buckets are empty (meaning that many are filled with tombstones), | 
| 206 | 40 |   // grow/rehash the table. | 
| 207 | 40 |   if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) { | 
| 208 | 2 |     NewSize = NumBuckets * 2; | 
| 209 | 38 |   } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <= | 
| 210 | 38 |                            NumBuckets / 8)) { | 
| 211 | 0 |     NewSize = NumBuckets; | 
| 212 | 38 |   } else { | 
| 213 | 38 |     return BucketNo; | 
| 214 | 38 |   } | 
| 215 | 2 |  | 
| 216 | 2 |   unsigned NewBucketNo = BucketNo; | 
| 217 | 2 |   // Allocate one extra bucket which will always be non-empty.  This allows the | 
| 218 | 2 |   // iterators to stop at end. | 
| 219 | 2 |   auto NewTableArray = static_cast<StringMapEntryBase **>(safe_calloc( | 
| 220 | 2 |       NewSize + 1, sizeof(StringMapEntryBase *) + sizeof(unsigned))); | 
| 221 | 2 |  | 
| 222 | 2 |   unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1); | 
| 223 | 2 |   NewTableArray[NewSize] = (StringMapEntryBase *)2; | 
| 224 | 2 |  | 
| 225 | 2 |   // Rehash all the items into their new buckets.  Luckily :) we already have | 
| 226 | 2 |   // the hash values available, so we don't have to rehash any strings. | 
| 227 | 34 |   for (unsigned I = 0, E = NumBuckets; I != E; ++I) { | 
| 228 | 32 |     StringMapEntryBase *Bucket = TheTable[I]; | 
| 229 | 32 |     if (Bucket && Bucket != getTombstoneVal()) { | 
| 230 | 26 |       // Fast case, bucket available. | 
| 231 | 26 |       unsigned FullHash = HashTable[I]; | 
| 232 | 26 |       unsigned NewBucket = FullHash & (NewSize - 1); | 
| 233 | 26 |       if (!NewTableArray[NewBucket]) { | 
| 234 | 20 |         NewTableArray[FullHash & (NewSize - 1)] = Bucket; | 
| 235 | 20 |         NewHashArray[FullHash & (NewSize - 1)] = FullHash; | 
| 236 | 20 |         if (I == BucketNo) | 
| 237 | 2 |           NewBucketNo = NewBucket; | 
| 238 | 20 |         continue; | 
| 239 | 20 |       } | 
| 240 | 6 |  | 
| 241 | 6 |       // Otherwise probe for a spot. | 
| 242 | 6 |       unsigned ProbeSize = 1; | 
| 243 | 10 |       do { | 
| 244 | 10 |         NewBucket = (NewBucket + ProbeSize++) & (NewSize - 1); | 
| 245 | 10 |       } while (NewTableArray[NewBucket]); | 
| 246 | 6 |  | 
| 247 | 6 |       // Finally found a slot.  Fill it in. | 
| 248 | 6 |       NewTableArray[NewBucket] = Bucket; | 
| 249 | 6 |       NewHashArray[NewBucket] = FullHash; | 
| 250 | 6 |       if (I == BucketNo) | 
| 251 | 0 |         NewBucketNo = NewBucket; | 
| 252 | 6 |     } | 
| 253 | 32 |   } | 
| 254 | 2 |  | 
| 255 | 2 |   free(TheTable); | 
| 256 | 2 |  | 
| 257 | 2 |   TheTable = NewTableArray; | 
| 258 | 2 |   NumBuckets = NewSize; | 
| 259 | 2 |   NumTombstones = 0; | 
| 260 | 2 |   return NewBucketNo; | 
| 261 | 2 | } |