Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 1 | //===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 24757de | 2007-01-27 07:18:32 +0000 | [diff] [blame] | 10 | // This file implements the SmallPtrSet class. See SmallPtrSet.h for an |
| 11 | // overview of the algorithm. |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4bb87cb | 2012-04-18 10:37:32 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMapInfo.h" |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 2945a32 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Reid Spencer | 845b31d | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | 42e4bdf | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 23 | void SmallPtrSetImpl::shrink_and_clear() { |
| 24 | assert(!isSmall() && "Can't shrink a small set!"); |
| 25 | free(CurArray); |
| 26 | |
| 27 | // Reduce the number of buckets. |
| 28 | CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32; |
| 29 | NumElements = NumTombstones = 0; |
| 30 | |
| 31 | // Install the new array. Clear all the buckets to empty. |
| 32 | CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1)); |
| 33 | assert(CurArray && "Failed to allocate memory?"); |
| 34 | memset(CurArray, -1, CurArraySize*sizeof(void*)); |
| 35 | |
| 36 | // The end pointer, always valid, is set to a valid element to help the |
| 37 | // iterator. |
| 38 | CurArray[CurArraySize] = 0; |
| 39 | } |
| 40 | |
Chris Lattner | 373a733 | 2007-11-06 22:12:43 +0000 | [diff] [blame] | 41 | bool SmallPtrSetImpl::insert_imp(const void * Ptr) { |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 42 | if (isSmall()) { |
| 43 | // Check to see if it is already in the set. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 44 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 45 | APtr != E; ++APtr) |
| 46 | if (*APtr == Ptr) |
| 47 | return false; |
| 48 | |
| 49 | // Nope, there isn't. If we stay small, just 'pushback' now. |
| 50 | if (NumElements < CurArraySize-1) { |
| 51 | SmallArray[NumElements++] = Ptr; |
| 52 | return true; |
| 53 | } |
| 54 | // Otherwise, hit the big set case, which will call grow. |
| 55 | } |
| 56 | |
Jakob Stoklund Olesen | e10fff6 | 2011-03-30 18:32:48 +0000 | [diff] [blame] | 57 | if (NumElements*4 >= CurArraySize*3) { |
| 58 | // If more than 3/4 of the array is full, grow. |
| 59 | Grow(CurArraySize < 64 ? 128 : CurArraySize*2); |
| 60 | } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) { |
| 61 | // If fewer of 1/8 of the array is empty (meaning that many are filled with |
| 62 | // tombstones), rehash. |
| 63 | Grow(CurArraySize); |
| 64 | } |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 65 | |
| 66 | // Okay, we know we have space. Find a hash bucket. |
Dan Gohman | 430b8a2 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 67 | const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr)); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 68 | if (*Bucket == Ptr) return false; // Already inserted, good. |
| 69 | |
| 70 | // Otherwise, insert it! |
Chris Lattner | e237cf9 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 71 | if (*Bucket == getTombstoneMarker()) |
| 72 | --NumTombstones; |
Dan Gohman | 430b8a2 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 73 | *Bucket = Ptr; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 74 | ++NumElements; // Track density. |
| 75 | return true; |
| 76 | } |
| 77 | |
Chris Lattner | 373a733 | 2007-11-06 22:12:43 +0000 | [diff] [blame] | 78 | bool SmallPtrSetImpl::erase_imp(const void * Ptr) { |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 79 | if (isSmall()) { |
| 80 | // Check to see if it is in the set. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 81 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 82 | APtr != E; ++APtr) |
| 83 | if (*APtr == Ptr) { |
Chris Lattner | 61766ca | 2007-06-21 23:23:32 +0000 | [diff] [blame] | 84 | // If it is in the set, replace this element. |
| 85 | *APtr = E[-1]; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 86 | E[-1] = getEmptyMarker(); |
| 87 | --NumElements; |
Chris Lattner | 7ef856d | 2007-02-05 23:10:31 +0000 | [diff] [blame] | 88 | return true; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // Okay, we know we have space. Find a hash bucket. |
| 95 | void **Bucket = const_cast<void**>(FindBucketFor(Ptr)); |
| 96 | if (*Bucket != Ptr) return false; // Not in the set? |
| 97 | |
| 98 | // Set this as a tombstone. |
| 99 | *Bucket = getTombstoneMarker(); |
| 100 | --NumElements; |
Chris Lattner | e237cf9 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 101 | ++NumTombstones; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 105 | const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const { |
Benjamin Kramer | 4bb87cb | 2012-04-18 10:37:32 +0000 | [diff] [blame] | 106 | unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 107 | unsigned ArraySize = CurArraySize; |
| 108 | unsigned ProbeAmt = 1; |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 109 | const void *const *Array = CurArray; |
| 110 | const void *const *Tombstone = 0; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 111 | while (1) { |
| 112 | // Found Ptr's bucket? |
| 113 | if (Array[Bucket] == Ptr) |
| 114 | return Array+Bucket; |
| 115 | |
| 116 | // If we found an empty bucket, the pointer doesn't exist in the set. |
| 117 | // Return a tombstone if we've seen one so far, or the empty bucket if |
| 118 | // not. |
| 119 | if (Array[Bucket] == getEmptyMarker()) |
| 120 | return Tombstone ? Tombstone : Array+Bucket; |
| 121 | |
| 122 | // If this is a tombstone, remember it. If Ptr ends up not in the set, we |
| 123 | // prefer to return it than something that would require more probing. |
| 124 | if (Array[Bucket] == getTombstoneMarker() && !Tombstone) |
| 125 | Tombstone = Array+Bucket; // Remember the first tombstone found. |
| 126 | |
| 127 | // It's a hash collision or a tombstone. Reprobe. |
| 128 | Bucket = (Bucket + ProbeAmt++) & (ArraySize-1); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Grow - Allocate a larger backing store for the buckets and move it over. |
| 133 | /// |
Jakob Stoklund Olesen | e10fff6 | 2011-03-30 18:32:48 +0000 | [diff] [blame] | 134 | void SmallPtrSetImpl::Grow(unsigned NewSize) { |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 135 | // Allocate at twice as many buckets, but at least 128. |
| 136 | unsigned OldSize = CurArraySize; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 137 | |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 138 | const void **OldBuckets = CurArray; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 139 | bool WasSmall = isSmall(); |
| 140 | |
| 141 | // Install the new array. Clear all the buckets to empty. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 142 | CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 143 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 144 | CurArraySize = NewSize; |
| 145 | memset(CurArray, -1, NewSize*sizeof(void*)); |
| 146 | |
| 147 | // The end pointer, always valid, is set to a valid element to help the |
| 148 | // iterator. |
| 149 | CurArray[NewSize] = 0; |
| 150 | |
| 151 | // Copy over all the elements. |
| 152 | if (WasSmall) { |
| 153 | // Small sets store their elements in order. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 154 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 155 | BucketPtr != E; ++BucketPtr) { |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 156 | const void *Elt = *BucketPtr; |
| 157 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 158 | } |
| 159 | } else { |
| 160 | // Copy over all valid entries. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 161 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 162 | BucketPtr != E; ++BucketPtr) { |
| 163 | // Copy over the element if it is valid. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 164 | const void *Elt = *BucketPtr; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 165 | if (Elt != getTombstoneMarker() && Elt != getEmptyMarker()) |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 166 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 169 | free(OldBuckets); |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 170 | NumTombstones = 0; |
| 171 | } |
| 172 | } |
| 173 | |
Duncan Sands | 2a8bf42 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 174 | SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, |
| 175 | const SmallPtrSetImpl& that) { |
| 176 | SmallArray = SmallStorage; |
| 177 | |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 178 | // If we're becoming small, prepare to insert into our stack space |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 179 | if (that.isSmall()) { |
Duncan Sands | 2a8bf42 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 180 | CurArray = SmallArray; |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 181 | // Otherwise, allocate new heap space (unless we were the same size) |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 182 | } else { |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 183 | CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 184 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 185 | } |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 186 | |
| 187 | // Copy over the new array size |
| 188 | CurArraySize = that.CurArraySize; |
| 189 | |
| 190 | // Copy over the contents from the other set |
| 191 | memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1)); |
| 192 | |
| 193 | NumElements = that.NumElements; |
| 194 | NumTombstones = that.NumTombstones; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 196 | |
| 197 | /// CopyFrom - implement operator= from a smallptrset that has the same pointer |
| 198 | /// type, but may have a different small size. |
| 199 | void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) { |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 200 | if (isSmall() && RHS.isSmall()) |
| 201 | assert(CurArraySize == RHS.CurArraySize && |
| 202 | "Cannot assign sets with different small sizes"); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 203 | |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 204 | // If we're becoming small, prepare to insert into our stack space |
Owen Anderson | 71a1e57 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 205 | if (RHS.isSmall()) { |
| 206 | if (!isSmall()) |
| 207 | free(CurArray); |
Duncan Sands | 2a8bf42 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 208 | CurArray = SmallArray; |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 209 | // Otherwise, allocate new heap space (unless we were the same size) |
Owen Anderson | 71a1e57 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 210 | } else if (CurArraySize != RHS.CurArraySize) { |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 211 | if (isSmall()) |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 212 | CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1)); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 213 | else |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 214 | CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 215 | assert(CurArray && "Failed to allocate memory?"); |
| 216 | } |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 217 | |
| 218 | // Copy over the new array size |
| 219 | CurArraySize = RHS.CurArraySize; |
| 220 | |
| 221 | // Copy over the contents from the other set |
| 222 | memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1)); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 223 | |
| 224 | NumElements = RHS.NumElements; |
| 225 | NumTombstones = RHS.NumTombstones; |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 226 | } |
Reid Spencer | 845b31d | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 227 | |
Benjamin Kramer | 2945a32 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 228 | void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) { |
| 229 | if (this == &RHS) return; |
| 230 | |
| 231 | // We can only avoid copying elements if neither set is small. |
| 232 | if (!this->isSmall() && !RHS.isSmall()) { |
| 233 | std::swap(this->CurArray, RHS.CurArray); |
| 234 | std::swap(this->CurArraySize, RHS.CurArraySize); |
| 235 | std::swap(this->NumElements, RHS.NumElements); |
| 236 | std::swap(this->NumTombstones, RHS.NumTombstones); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // FIXME: From here on we assume that both sets have the same small size. |
| 241 | |
| 242 | // If only RHS is small, copy the small elements into LHS and move the pointer |
| 243 | // from LHS to RHS. |
| 244 | if (!this->isSmall() && RHS.isSmall()) { |
Benjamin Kramer | f03e62a | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 245 | std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize, |
| 246 | this->SmallArray); |
Benjamin Kramer | 2945a32 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 247 | std::swap(this->NumElements, RHS.NumElements); |
| 248 | std::swap(this->CurArraySize, RHS.CurArraySize); |
| 249 | RHS.CurArray = this->CurArray; |
| 250 | RHS.NumTombstones = this->NumTombstones; |
| 251 | this->CurArray = this->SmallArray; |
| 252 | this->NumTombstones = 0; |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // If only LHS is small, copy the small elements into RHS and move the pointer |
| 257 | // from RHS to LHS. |
| 258 | if (this->isSmall() && !RHS.isSmall()) { |
Benjamin Kramer | f03e62a | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 259 | std::copy(this->SmallArray, this->SmallArray+this->CurArraySize, |
| 260 | RHS.SmallArray); |
Benjamin Kramer | 2945a32 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 261 | std::swap(RHS.NumElements, this->NumElements); |
| 262 | std::swap(RHS.CurArraySize, this->CurArraySize); |
| 263 | this->CurArray = RHS.CurArray; |
| 264 | this->NumTombstones = RHS.NumTombstones; |
| 265 | RHS.CurArray = RHS.SmallArray; |
| 266 | RHS.NumTombstones = 0; |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // Both a small, just swap the small elements. |
| 271 | assert(this->isSmall() && RHS.isSmall()); |
| 272 | assert(this->CurArraySize == RHS.CurArraySize); |
Benjamin Kramer | f03e62a | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 273 | std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize, |
Benjamin Kramer | 24e0e7c | 2012-03-07 22:33:21 +0000 | [diff] [blame] | 274 | RHS.SmallArray); |
Benjamin Kramer | 2945a32 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 275 | std::swap(this->NumElements, RHS.NumElements); |
| 276 | } |
| 277 | |
Reid Spencer | 845b31d | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 278 | SmallPtrSetImpl::~SmallPtrSetImpl() { |
| 279 | if (!isSmall()) |
| 280 | free(CurArray); |
| 281 | } |