Coverage Report

Created: 2025-12-31 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/keystone/llvm/lib/Support/StringMap.cpp
Line
Count
Source
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 <cassert>
18
using namespace llvm_ks;
19
20
0
StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
21
0
  ItemSize = itemSize;
22
  
23
  // If a size is specified, initialize the table with that many buckets.
24
0
  if (InitSize) {
25
0
    init(InitSize);
26
0
    return;
27
0
  }
28
  
29
  // Otherwise, initialize it with zero buckets to avoid the allocation.
30
0
  TheTable = nullptr;
31
0
  NumBuckets = 0;
32
0
  NumItems = 0;
33
0
  NumTombstones = 0;
34
0
}
35
36
657k
void StringMapImpl::init(unsigned InitSize) {
37
657k
  assert((InitSize & (InitSize-1)) == 0 &&
38
657k
         "Init Size must be a power of 2 or zero!");
39
657k
  NumBuckets = InitSize ? InitSize : 16;
40
657k
  NumItems = 0;
41
657k
  NumTombstones = 0;
42
  
43
657k
  TheTable = (StringMapEntryBase **)calloc(NumBuckets+1,
44
657k
                                           sizeof(StringMapEntryBase **) +
45
657k
                                           sizeof(unsigned));
46
47
  // Allocate one extra bucket, set it to look filled so the iterators stop at
48
  // end.
49
657k
  TheTable[NumBuckets] = (StringMapEntryBase*)2;
50
657k
}
51
52
53
/// 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.
58
32.6M
unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
59
32.6M
  unsigned HTSize = NumBuckets;
60
32.6M
  if (HTSize == 0) {  // Hash table unallocated so far?
61
657k
    init(16);
62
657k
    HTSize = NumBuckets;
63
657k
  }
64
32.6M
  unsigned FullHashValue = HashString(Name);
65
32.6M
  unsigned BucketNo = FullHashValue & (HTSize-1);
66
32.6M
  unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
67
68
32.6M
  unsigned ProbeAmt = 1;
69
32.6M
  int FirstTombstone = -1;
70
75.3M
  while (1) {
71
75.3M
    StringMapEntryBase *BucketItem = TheTable[BucketNo];
72
    // If we found an empty bucket, this key isn't in the table yet, return it.
73
75.3M
    if (LLVM_LIKELY(!BucketItem)) {
74
      // If we found a tombstone, we want to reuse the tombstone instead of an
75
      // empty bucket.  This reduces probing.
76
28.6M
      if (FirstTombstone != -1) {
77
0
        HashTable[FirstTombstone] = FullHashValue;
78
0
        return FirstTombstone;
79
0
      }
80
      
81
28.6M
      HashTable[BucketNo] = FullHashValue;
82
28.6M
      return BucketNo;
83
28.6M
    }
84
    
85
46.6M
    if (BucketItem == getTombstoneVal()) {
86
      // Skip over tombstones.  However, remember the first one we see.
87
0
      if (FirstTombstone == -1) FirstTombstone = BucketNo;
88
46.6M
    } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
89
      // 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
      
94
      // Do the comparison like this because Name isn't necessarily
95
      // null-terminated!
96
3.95M
      char *ItemStr = (char*)BucketItem+ItemSize;
97
3.95M
      if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
98
        // We found a match!
99
3.95M
        return BucketNo;
100
3.95M
      }
101
3.95M
    }
102
    
103
    // Okay, we didn't find the item.  Probe to the next bucket.
104
42.7M
    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
42.7M
    ++ProbeAmt;
109
42.7M
  }
110
32.6M
}
111
112
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.
116
28.7M
int StringMapImpl::FindKey(StringRef Key) const {
117
28.7M
  unsigned HTSize = NumBuckets;
118
28.7M
  if (HTSize == 0) return -1;  // Really empty table?
119
21.0M
  unsigned FullHashValue = HashString(Key);
120
21.0M
  unsigned BucketNo = FullHashValue & (HTSize-1);
121
21.0M
  unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
122
123
21.0M
  unsigned ProbeAmt = 1;
124
42.5M
  while (1) {
125
42.5M
    StringMapEntryBase *BucketItem = TheTable[BucketNo];
126
    // If we found an empty bucket, this key isn't in the table yet, return.
127
42.5M
    if (LLVM_LIKELY(!BucketItem))
128
16.5M
      return -1;
129
    
130
25.9M
    if (BucketItem == getTombstoneVal()) {
131
      // Ignore tombstones.
132
25.9M
    } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
133
      // 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
      
138
      // Do the comparison like this because NameStart isn't necessarily
139
      // null-terminated!
140
4.51M
      char *ItemStr = (char*)BucketItem+ItemSize;
141
4.51M
      if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
142
        // We found a match!
143
4.50M
        return BucketNo;
144
4.50M
      }
145
4.51M
    }
146
    
147
    // Okay, we didn't find the item.  Probe to the next bucket.
148
21.4M
    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
21.4M
    ++ProbeAmt;
153
21.4M
  }
154
21.0M
}
155
156
/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
157
/// delete it.  This aborts if the value isn't in the table.
158
0
void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
159
0
  const char *VStr = (char*)V + ItemSize;
160
0
  StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
161
0
  (void)V2;
162
0
  assert(V == V2 && "Didn't find key?");
163
0
}
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.
167
0
StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
168
0
  int Bucket = FindKey(Key);
169
0
  if (Bucket == -1) return nullptr;
170
  
171
0
  StringMapEntryBase *Result = TheTable[Bucket];
172
0
  TheTable[Bucket] = getTombstoneVal();
173
0
  --NumItems;
174
0
  ++NumTombstones;
175
0
  assert(NumItems + NumTombstones <= NumBuckets);
176
177
0
  return Result;
178
0
}
179
180
181
182
/// RehashTable - Grow the table, redistributing values into the buckets with
183
/// the appropriate mod-of-hashtable-size.
184
28.6M
unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
185
28.6M
  unsigned NewSize;
186
28.6M
  unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
187
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
28.6M
  if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
192
965k
    NewSize = NumBuckets*2;
193
27.6M
  } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
194
27.6M
                           NumBuckets / 8)) {
195
0
    NewSize = NumBuckets;
196
27.6M
  } else {
197
27.6M
    return BucketNo;
198
27.6M
  }
199
200
965k
  unsigned NewBucketNo = BucketNo;
201
  // Allocate one extra bucket which will always be non-empty.  This allows the
202
  // iterators to stop at end.
203
965k
  StringMapEntryBase **NewTableArray =
204
965k
    (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) +
205
965k
                                             sizeof(unsigned));
206
965k
  unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
207
965k
  NewTableArray[NewSize] = (StringMapEntryBase*)2;
208
209
  // Rehash all the items into their new buckets.  Luckily :) we already have
210
  // the hash values available, so we don't have to rehash any strings.
211
49.1M
  for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
212
48.2M
    StringMapEntryBase *Bucket = TheTable[I];
213
48.2M
    if (Bucket && Bucket != getTombstoneVal()) {
214
      // Fast case, bucket available.
215
37.1M
      unsigned FullHash = HashTable[I];
216
37.1M
      unsigned NewBucket = FullHash & (NewSize-1);
217
37.1M
      if (!NewTableArray[NewBucket]) {
218
29.0M
        NewTableArray[FullHash & (NewSize-1)] = Bucket;
219
29.0M
        NewHashArray[FullHash & (NewSize-1)] = FullHash;
220
29.0M
        if (I == BucketNo)
221
824k
          NewBucketNo = NewBucket;
222
29.0M
        continue;
223
29.0M
      }
224
      
225
      // Otherwise probe for a spot.
226
8.08M
      unsigned ProbeSize = 1;
227
11.7M
      do {
228
11.7M
        NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
229
11.7M
      } while (NewTableArray[NewBucket]);
230
      
231
      // Finally found a slot.  Fill it in.
232
8.08M
      NewTableArray[NewBucket] = Bucket;
233
8.08M
      NewHashArray[NewBucket] = FullHash;
234
8.08M
      if (I == BucketNo)
235
141k
        NewBucketNo = NewBucket;
236
8.08M
    }
237
48.2M
  }
238
  
239
965k
  free(TheTable);
240
  
241
965k
  TheTable = NewTableArray;
242
965k
  NumBuckets = NewSize;
243
965k
  NumTombstones = 0;
244
965k
  return NewBucketNo;
245
28.6M
}