Coverage Report

Created: 2026-09-14 06:12

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
880k
void StringMapImpl::init(unsigned InitSize) {
37
880k
  assert((InitSize & (InitSize-1)) == 0 &&
38
880k
         "Init Size must be a power of 2 or zero!");
39
880k
  NumBuckets = InitSize ? InitSize : 16;
40
880k
  NumItems = 0;
41
880k
  NumTombstones = 0;
42
  
43
880k
  TheTable = (StringMapEntryBase **)calloc(NumBuckets+1,
44
880k
                                           sizeof(StringMapEntryBase **) +
45
880k
                                           sizeof(unsigned));
46
47
  // Allocate one extra bucket, set it to look filled so the iterators stop at
48
  // end.
49
880k
  TheTable[NumBuckets] = (StringMapEntryBase*)2;
50
880k
}
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
42.6M
unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
59
42.6M
  unsigned HTSize = NumBuckets;
60
42.6M
  if (HTSize == 0) {  // Hash table unallocated so far?
61
880k
    init(16);
62
880k
    HTSize = NumBuckets;
63
880k
  }
64
42.6M
  unsigned FullHashValue = HashString(Name);
65
42.6M
  unsigned BucketNo = FullHashValue & (HTSize-1);
66
42.6M
  unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
67
68
42.6M
  unsigned ProbeAmt = 1;
69
42.6M
  int FirstTombstone = -1;
70
101M
  while (1) {
71
101M
    StringMapEntryBase *BucketItem = TheTable[BucketNo];
72
    // If we found an empty bucket, this key isn't in the table yet, return it.
73
101M
    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
38.4M
      if (FirstTombstone != -1) {
77
0
        HashTable[FirstTombstone] = FullHashValue;
78
0
        return FirstTombstone;
79
0
      }
80
      
81
38.4M
      HashTable[BucketNo] = FullHashValue;
82
38.4M
      return BucketNo;
83
38.4M
    }
84
    
85
62.8M
    if (BucketItem == getTombstoneVal()) {
86
      // Skip over tombstones.  However, remember the first one we see.
87
0
      if (FirstTombstone == -1) FirstTombstone = BucketNo;
88
62.8M
    } 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
4.17M
      char *ItemStr = (char*)BucketItem+ItemSize;
97
4.17M
      if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
98
        // We found a match!
99
4.16M
        return BucketNo;
100
4.16M
      }
101
4.17M
    }
102
    
103
    // Okay, we didn't find the item.  Probe to the next bucket.
104
58.6M
    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
58.6M
    ++ProbeAmt;
109
58.6M
  }
110
42.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
36.7M
int StringMapImpl::FindKey(StringRef Key) const {
117
36.7M
  unsigned HTSize = NumBuckets;
118
36.7M
  if (HTSize == 0) return -1;  // Really empty table?
119
28.6M
  unsigned FullHashValue = HashString(Key);
120
28.6M
  unsigned BucketNo = FullHashValue & (HTSize-1);
121
28.6M
  unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
122
123
28.6M
  unsigned ProbeAmt = 1;
124
54.9M
  while (1) {
125
54.9M
    StringMapEntryBase *BucketItem = TheTable[BucketNo];
126
    // If we found an empty bucket, this key isn't in the table yet, return.
127
54.9M
    if (LLVM_LIKELY(!BucketItem))
128
21.6M
      return -1;
129
    
130
33.3M
    if (BucketItem == getTombstoneVal()) {
131
      // Ignore tombstones.
132
33.3M
    } 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
6.98M
      char *ItemStr = (char*)BucketItem+ItemSize;
141
6.98M
      if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
142
        // We found a match!
143
6.98M
        return BucketNo;
144
6.98M
      }
145
6.98M
    }
146
    
147
    // Okay, we didn't find the item.  Probe to the next bucket.
148
26.3M
    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
26.3M
    ++ProbeAmt;
153
26.3M
  }
154
28.6M
}
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
38.4M
unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
185
38.4M
  unsigned NewSize;
186
38.4M
  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
38.4M
  if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
192
1.29M
    NewSize = NumBuckets*2;
193
37.1M
  } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
194
37.1M
                           NumBuckets / 8)) {
195
0
    NewSize = NumBuckets;
196
37.1M
  } else {
197
37.1M
    return BucketNo;
198
37.1M
  }
199
200
1.29M
  unsigned NewBucketNo = BucketNo;
201
  // Allocate one extra bucket which will always be non-empty.  This allows the
202
  // iterators to stop at end.
203
1.29M
  StringMapEntryBase **NewTableArray =
204
1.29M
    (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) +
205
1.29M
                                             sizeof(unsigned));
206
1.29M
  unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
207
1.29M
  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
66.1M
  for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
212
64.8M
    StringMapEntryBase *Bucket = TheTable[I];
213
64.8M
    if (Bucket && Bucket != getTombstoneVal()) {
214
      // Fast case, bucket available.
215
49.9M
      unsigned FullHash = HashTable[I];
216
49.9M
      unsigned NewBucket = FullHash & (NewSize-1);
217
49.9M
      if (!NewTableArray[NewBucket]) {
218
39.0M
        NewTableArray[FullHash & (NewSize-1)] = Bucket;
219
39.0M
        NewHashArray[FullHash & (NewSize-1)] = FullHash;
220
39.0M
        if (I == BucketNo)
221
1.10M
          NewBucketNo = NewBucket;
222
39.0M
        continue;
223
39.0M
      }
224
      
225
      // Otherwise probe for a spot.
226
10.9M
      unsigned ProbeSize = 1;
227
16.1M
      do {
228
16.1M
        NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
229
16.1M
      } while (NewTableArray[NewBucket]);
230
      
231
      // Finally found a slot.  Fill it in.
232
10.9M
      NewTableArray[NewBucket] = Bucket;
233
10.9M
      NewHashArray[NewBucket] = FullHash;
234
10.9M
      if (I == BucketNo)
235
189k
        NewBucketNo = NewBucket;
236
10.9M
    }
237
64.8M
  }
238
  
239
1.29M
  free(TheTable);
240
  
241
1.29M
  TheTable = NewTableArray;
242
1.29M
  NumBuckets = NewSize;
243
1.29M
  NumTombstones = 0;
244
1.29M
  return NewBucketNo;
245
38.4M
}