Coverage Report

Created: 2020-06-26 05:44

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