Coverage Report

Created: 2026-02-07 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/keystone/llvm/lib/Support/SmallPtrSet.cpp
Line
Count
Source
1
//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
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 SmallPtrSet class.  See SmallPtrSet.h for an
11
// overview of the algorithm.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/ADT/SmallPtrSet.h"
16
#include "llvm/ADT/DenseMapInfo.h"
17
#include "llvm/Support/MathExtras.h"
18
#include <algorithm>
19
#include <cstdlib>
20
21
using namespace llvm_ks;
22
23
0
void SmallPtrSetImplBase::shrink_and_clear() {
24
0
  assert(!isSmall() && "Can't shrink a small set!");
25
0
  free(CurArray);
26
27
  // Reduce the number of buckets.
28
0
  CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
29
0
  NumElements = NumTombstones = 0;
30
31
  // Install the new array.  Clear all the buckets to empty.
32
0
  CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
33
0
  assert(CurArray && "Failed to allocate memory?");
34
0
  memset(CurArray, -1, CurArraySize*sizeof(void*));
35
0
}
36
37
std::pair<const void *const *, bool>
38
0
SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
39
0
  if (LLVM_UNLIKELY(NumElements * 4 >= CurArraySize * 3)) {
40
    // If more than 3/4 of the array is full, grow.
41
0
    Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
42
0
  } else if (LLVM_UNLIKELY(CurArraySize - (NumElements + NumTombstones) <
43
0
                           CurArraySize / 8)) {
44
    // If fewer of 1/8 of the array is empty (meaning that many are filled with
45
    // tombstones), rehash.
46
0
    Grow(CurArraySize);
47
0
  }
48
49
  // Okay, we know we have space.  Find a hash bucket.
50
0
  const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
51
0
  if (*Bucket == Ptr)
52
0
    return std::make_pair(Bucket, false); // Already inserted, good.
53
54
  // Otherwise, insert it!
55
0
  if (*Bucket == getTombstoneMarker())
56
0
    --NumTombstones;
57
0
  *Bucket = Ptr;
58
0
  ++NumElements;  // Track density.
59
0
  return std::make_pair(Bucket, true);
60
0
}
61
62
0
bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
63
0
  if (isSmall()) {
64
    // Check to see if it is in the set.
65
0
    for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
66
0
         APtr != E; ++APtr)
67
0
      if (*APtr == Ptr) {
68
        // If it is in the set, replace this element.
69
0
        *APtr = E[-1];
70
0
        E[-1] = getEmptyMarker();
71
0
        --NumElements;
72
0
        return true;
73
0
      }
74
75
0
    return false;
76
0
  }
77
78
  // Okay, we know we have space.  Find a hash bucket.
79
0
  void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
80
0
  if (*Bucket != Ptr) return false;  // Not in the set?
81
82
  // Set this as a tombstone.
83
0
  *Bucket = getTombstoneMarker();
84
0
  --NumElements;
85
0
  ++NumTombstones;
86
0
  return true;
87
0
}
88
89
0
const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
90
0
  unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
91
0
  unsigned ArraySize = CurArraySize;
92
0
  unsigned ProbeAmt = 1;
93
0
  const void *const *Array = CurArray;
94
0
  const void *const *Tombstone = nullptr;
95
0
  while (1) {
96
    // If we found an empty bucket, the pointer doesn't exist in the set.
97
    // Return a tombstone if we've seen one so far, or the empty bucket if
98
    // not.
99
0
    if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
100
0
      return Tombstone ? Tombstone : Array+Bucket;
101
102
    // Found Ptr's bucket?
103
0
    if (LLVM_LIKELY(Array[Bucket] == Ptr))
104
0
      return Array+Bucket;
105
106
    // If this is a tombstone, remember it.  If Ptr ends up not in the set, we
107
    // prefer to return it than something that would require more probing.
108
0
    if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
109
0
      Tombstone = Array+Bucket;  // Remember the first tombstone found.
110
111
    // It's a hash collision or a tombstone. Reprobe.
112
0
    Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
113
0
  }
114
0
}
115
116
/// Grow - Allocate a larger backing store for the buckets and move it over.
117
///
118
0
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
119
  // Allocate at twice as many buckets, but at least 128.
120
0
  unsigned OldSize = CurArraySize;
121
122
0
  const void **OldBuckets = CurArray;
123
0
  bool WasSmall = isSmall();
124
125
  // Install the new array.  Clear all the buckets to empty.
126
0
  CurArray = (const void**)malloc(sizeof(void*) * NewSize);
127
0
  assert(CurArray && "Failed to allocate memory?");
128
0
  CurArraySize = NewSize;
129
0
  memset(CurArray, -1, NewSize*sizeof(void*));
130
131
  // Copy over all the elements.
132
0
  if (WasSmall) {
133
    // Small sets store their elements in order.
134
0
    for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
135
0
         BucketPtr != E; ++BucketPtr) {
136
0
      const void *Elt = *BucketPtr;
137
0
      *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
138
0
    }
139
0
  } else {
140
    // Copy over all valid entries.
141
0
    for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
142
0
         BucketPtr != E; ++BucketPtr) {
143
      // Copy over the element if it is valid.
144
0
      const void *Elt = *BucketPtr;
145
0
      if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
146
0
        *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
147
0
    }
148
149
0
    free(OldBuckets);
150
0
    NumTombstones = 0;
151
0
  }
152
0
}
153
154
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
155
0
                                         const SmallPtrSetImplBase &that) {
156
0
  SmallArray = SmallStorage;
157
158
  // If we're becoming small, prepare to insert into our stack space
159
0
  if (that.isSmall()) {
160
0
    CurArray = SmallArray;
161
  // Otherwise, allocate new heap space (unless we were the same size)
162
0
  } else {
163
0
    CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
164
0
    assert(CurArray && "Failed to allocate memory?");
165
0
  }
166
167
  // Copy over the that array.
168
0
  CopyHelper(that);
169
0
}
170
171
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
172
                                         unsigned SmallSize,
173
0
                                         SmallPtrSetImplBase &&that) {
174
0
  SmallArray = SmallStorage;
175
0
  MoveHelper(SmallSize, std::move(that));
176
0
}
177
178
0
void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
179
0
  assert(&RHS != this && "Self-copy should be handled by the caller.");
180
181
0
  if (isSmall() && RHS.isSmall())
182
0
    assert(CurArraySize == RHS.CurArraySize &&
183
0
           "Cannot assign sets with different small sizes");
184
185
  // If we're becoming small, prepare to insert into our stack space
186
0
  if (RHS.isSmall()) {
187
0
    if (!isSmall())
188
0
      free(CurArray);
189
0
    CurArray = SmallArray;
190
  // Otherwise, allocate new heap space (unless we were the same size)
191
0
  } else if (CurArraySize != RHS.CurArraySize) {
192
0
    if (isSmall())
193
0
      CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
194
0
    else {
195
0
      const void **T = (const void**)realloc(CurArray,
196
0
                                             sizeof(void*) * RHS.CurArraySize);
197
0
      if (!T)
198
0
        free(CurArray);
199
0
      CurArray = T;
200
0
    }
201
0
    assert(CurArray && "Failed to allocate memory?");
202
0
  }
203
204
0
  CopyHelper(RHS);
205
0
}
206
207
0
void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
208
  // Copy over the new array size
209
0
  CurArraySize = RHS.CurArraySize;
210
211
  // Copy over the contents from the other set
212
0
  memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
213
214
0
  NumElements = RHS.NumElements;
215
0
  NumTombstones = RHS.NumTombstones;
216
0
}
217
218
void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
219
0
                                   SmallPtrSetImplBase &&RHS) {
220
0
  if (!isSmall())
221
0
    free(CurArray);
222
0
  MoveHelper(SmallSize, std::move(RHS));
223
0
}
224
225
void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
226
0
                                     SmallPtrSetImplBase &&RHS) {
227
0
  assert(&RHS != this && "Self-move should be handled by the caller.");
228
229
0
  if (RHS.isSmall()) {
230
    // Copy a small RHS rather than moving.
231
0
    CurArray = SmallArray;
232
0
    memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
233
0
  } else {
234
0
    CurArray = RHS.CurArray;
235
0
    RHS.CurArray = RHS.SmallArray;
236
0
  }
237
238
  // Copy the rest of the trivial members.
239
0
  CurArraySize = RHS.CurArraySize;
240
0
  NumElements = RHS.NumElements;
241
0
  NumTombstones = RHS.NumTombstones;
242
243
  // Make the RHS small and empty.
244
0
  RHS.CurArraySize = SmallSize;
245
0
  assert(RHS.CurArray == RHS.SmallArray);
246
0
  RHS.NumElements = 0;
247
0
  RHS.NumTombstones = 0;
248
0
}
249
250
0
void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
251
0
  if (this == &RHS) return;
252
253
  // We can only avoid copying elements if neither set is small.
254
0
  if (!this->isSmall() && !RHS.isSmall()) {
255
0
    std::swap(this->CurArray, RHS.CurArray);
256
0
    std::swap(this->CurArraySize, RHS.CurArraySize);
257
0
    std::swap(this->NumElements, RHS.NumElements);
258
0
    std::swap(this->NumTombstones, RHS.NumTombstones);
259
0
    return;
260
0
  }
261
262
  // FIXME: From here on we assume that both sets have the same small size.
263
264
  // If only RHS is small, copy the small elements into LHS and move the pointer
265
  // from LHS to RHS.
266
0
  if (!this->isSmall() && RHS.isSmall()) {
267
0
    std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
268
0
              this->SmallArray);
269
0
    std::swap(this->NumElements, RHS.NumElements);
270
0
    std::swap(this->CurArraySize, RHS.CurArraySize);
271
0
    RHS.CurArray = this->CurArray;
272
0
    RHS.NumTombstones = this->NumTombstones;
273
0
    this->CurArray = this->SmallArray;
274
0
    this->NumTombstones = 0;
275
0
    return;
276
0
  }
277
278
  // If only LHS is small, copy the small elements into RHS and move the pointer
279
  // from RHS to LHS.
280
0
  if (this->isSmall() && !RHS.isSmall()) {
281
0
    std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
282
0
              RHS.SmallArray);
283
0
    std::swap(RHS.NumElements, this->NumElements);
284
0
    std::swap(RHS.CurArraySize, this->CurArraySize);
285
0
    this->CurArray = RHS.CurArray;
286
0
    this->NumTombstones = RHS.NumTombstones;
287
0
    RHS.CurArray = RHS.SmallArray;
288
0
    RHS.NumTombstones = 0;
289
0
    return;
290
0
  }
291
292
  // Both a small, just swap the small elements.
293
0
  assert(this->isSmall() && RHS.isSmall());
294
0
  assert(this->CurArraySize == RHS.CurArraySize);
295
0
  std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
296
0
                   RHS.SmallArray);
297
0
  std::swap(this->NumElements, RHS.NumElements);
298
0
}