Coverage Report

Created: 2019-07-24 05:18

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