blob: 997ce0b74cd24c559c6a6a05c7e5a6f44dc6d752 [file] [log] [blame]
Chris Lattnerc95dc982007-01-27 07:10:46 +00001//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc95dc982007-01-27 07:10:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner24757de2007-01-27 07:18:32 +000010// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11// overview of the algorithm.
Chris Lattnerc95dc982007-01-27 07:10:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner91f01582007-07-09 16:54:03 +000016#include "llvm/Support/MathExtras.h"
Reid Spencer845b31d2007-07-17 02:16:12 +000017#include <cstdlib>
18
Chris Lattnerc95dc982007-01-27 07:10:46 +000019using namespace llvm;
20
Chris Lattner42e4bdf2007-08-05 07:32:14 +000021void SmallPtrSetImpl::shrink_and_clear() {
22 assert(!isSmall() && "Can't shrink a small set!");
23 free(CurArray);
24
25 // Reduce the number of buckets.
26 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
27 NumElements = NumTombstones = 0;
28
29 // Install the new array. Clear all the buckets to empty.
30 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
31 assert(CurArray && "Failed to allocate memory?");
32 memset(CurArray, -1, CurArraySize*sizeof(void*));
33
34 // The end pointer, always valid, is set to a valid element to help the
35 // iterator.
36 CurArray[CurArraySize] = 0;
37}
38
Chris Lattner373a7332007-11-06 22:12:43 +000039bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
Chris Lattnerc95dc982007-01-27 07:10:46 +000040 if (isSmall()) {
41 // Check to see if it is already in the set.
Owen Andersone992a562007-07-27 18:07:02 +000042 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +000043 APtr != E; ++APtr)
44 if (*APtr == Ptr)
45 return false;
46
47 // Nope, there isn't. If we stay small, just 'pushback' now.
48 if (NumElements < CurArraySize-1) {
49 SmallArray[NumElements++] = Ptr;
50 return true;
51 }
52 // Otherwise, hit the big set case, which will call grow.
53 }
54
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +000055 if (NumElements*4 >= CurArraySize*3) {
56 // If more than 3/4 of the array is full, grow.
57 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
58 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
59 // If fewer of 1/8 of the array is empty (meaning that many are filled with
60 // tombstones), rehash.
61 Grow(CurArraySize);
62 }
Chris Lattnerc95dc982007-01-27 07:10:46 +000063
64 // Okay, we know we have space. Find a hash bucket.
Dan Gohman430b8a22008-08-05 14:45:15 +000065 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
Chris Lattnerc95dc982007-01-27 07:10:46 +000066 if (*Bucket == Ptr) return false; // Already inserted, good.
67
68 // Otherwise, insert it!
Chris Lattnere237cf92007-02-07 01:11:25 +000069 if (*Bucket == getTombstoneMarker())
70 --NumTombstones;
Dan Gohman430b8a22008-08-05 14:45:15 +000071 *Bucket = Ptr;
Chris Lattnerc95dc982007-01-27 07:10:46 +000072 ++NumElements; // Track density.
73 return true;
74}
75
Chris Lattner373a7332007-11-06 22:12:43 +000076bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
Chris Lattner0b930852007-01-27 07:59:10 +000077 if (isSmall()) {
78 // Check to see if it is in the set.
Owen Andersone992a562007-07-27 18:07:02 +000079 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner0b930852007-01-27 07:59:10 +000080 APtr != E; ++APtr)
81 if (*APtr == Ptr) {
Chris Lattner61766ca2007-06-21 23:23:32 +000082 // If it is in the set, replace this element.
83 *APtr = E[-1];
Chris Lattner0b930852007-01-27 07:59:10 +000084 E[-1] = getEmptyMarker();
85 --NumElements;
Chris Lattner7ef856d2007-02-05 23:10:31 +000086 return true;
Chris Lattner0b930852007-01-27 07:59:10 +000087 }
88
89 return false;
90 }
91
92 // Okay, we know we have space. Find a hash bucket.
93 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
94 if (*Bucket != Ptr) return false; // Not in the set?
95
96 // Set this as a tombstone.
97 *Bucket = getTombstoneMarker();
98 --NumElements;
Chris Lattnere237cf92007-02-07 01:11:25 +000099 ++NumTombstones;
Chris Lattner0b930852007-01-27 07:59:10 +0000100 return true;
101}
102
Owen Andersone992a562007-07-27 18:07:02 +0000103const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000104 unsigned Bucket = Hash(Ptr);
105 unsigned ArraySize = CurArraySize;
106 unsigned ProbeAmt = 1;
Owen Andersone992a562007-07-27 18:07:02 +0000107 const void *const *Array = CurArray;
108 const void *const *Tombstone = 0;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000109 while (1) {
110 // Found Ptr's bucket?
111 if (Array[Bucket] == Ptr)
112 return Array+Bucket;
113
114 // If we found an empty bucket, the pointer doesn't exist in the set.
115 // Return a tombstone if we've seen one so far, or the empty bucket if
116 // not.
117 if (Array[Bucket] == getEmptyMarker())
118 return Tombstone ? Tombstone : Array+Bucket;
119
120 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
121 // prefer to return it than something that would require more probing.
122 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
123 Tombstone = Array+Bucket; // Remember the first tombstone found.
124
125 // It's a hash collision or a tombstone. Reprobe.
126 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
127 }
128}
129
130/// Grow - Allocate a larger backing store for the buckets and move it over.
131///
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +0000132void SmallPtrSetImpl::Grow(unsigned NewSize) {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000133 // Allocate at twice as many buckets, but at least 128.
134 unsigned OldSize = CurArraySize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000135
Owen Andersone992a562007-07-27 18:07:02 +0000136 const void **OldBuckets = CurArray;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000137 bool WasSmall = isSmall();
138
139 // Install the new array. Clear all the buckets to empty.
Owen Andersone992a562007-07-27 18:07:02 +0000140 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000141 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000142 CurArraySize = NewSize;
143 memset(CurArray, -1, NewSize*sizeof(void*));
144
145 // The end pointer, always valid, is set to a valid element to help the
146 // iterator.
147 CurArray[NewSize] = 0;
148
149 // Copy over all the elements.
150 if (WasSmall) {
151 // Small sets store their elements in order.
Owen Andersone992a562007-07-27 18:07:02 +0000152 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000153 BucketPtr != E; ++BucketPtr) {
Owen Andersone992a562007-07-27 18:07:02 +0000154 const void *Elt = *BucketPtr;
155 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000156 }
157 } else {
158 // Copy over all valid entries.
Owen Andersone992a562007-07-27 18:07:02 +0000159 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000160 BucketPtr != E; ++BucketPtr) {
161 // Copy over the element if it is valid.
Owen Andersone992a562007-07-27 18:07:02 +0000162 const void *Elt = *BucketPtr;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000163 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Andersone992a562007-07-27 18:07:02 +0000164 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000165 }
166
Owen Anderson1629a1f2007-07-16 21:27:44 +0000167 free(OldBuckets);
Jeff Cohenac58a162007-04-14 21:50:21 +0000168 NumTombstones = 0;
169 }
170}
171
Duncan Sands2a8bf422010-06-30 15:02:37 +0000172SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
173 const SmallPtrSetImpl& that) {
174 SmallArray = SmallStorage;
175
Owen Andersonbf31b852007-07-24 21:31:23 +0000176 // If we're becoming small, prepare to insert into our stack space
Jeff Cohenac58a162007-04-14 21:50:21 +0000177 if (that.isSmall()) {
Duncan Sands2a8bf422010-06-30 15:02:37 +0000178 CurArray = SmallArray;
Owen Andersonbf31b852007-07-24 21:31:23 +0000179 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohenac58a162007-04-14 21:50:21 +0000180 } else {
Owen Andersone992a562007-07-27 18:07:02 +0000181 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000182 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000183 }
Owen Andersonbf31b852007-07-24 21:31:23 +0000184
185 // Copy over the new array size
186 CurArraySize = that.CurArraySize;
187
188 // Copy over the contents from the other set
189 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
190
191 NumElements = that.NumElements;
192 NumTombstones = that.NumTombstones;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000193}
Chris Lattner91f01582007-07-09 16:54:03 +0000194
195/// CopyFrom - implement operator= from a smallptrset that has the same pointer
196/// type, but may have a different small size.
197void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
Owen Anderson69b5d122007-07-09 22:27:20 +0000198 if (isSmall() && RHS.isSmall())
199 assert(CurArraySize == RHS.CurArraySize &&
200 "Cannot assign sets with different small sizes");
Owen Andersonb54b3152007-07-18 19:54:15 +0000201
Owen Anderson69b5d122007-07-09 22:27:20 +0000202 // If we're becoming small, prepare to insert into our stack space
Owen Anderson71a1e572007-07-19 06:45:33 +0000203 if (RHS.isSmall()) {
204 if (!isSmall())
205 free(CurArray);
Duncan Sands2a8bf422010-06-30 15:02:37 +0000206 CurArray = SmallArray;
Owen Anderson69b5d122007-07-09 22:27:20 +0000207 // Otherwise, allocate new heap space (unless we were the same size)
Owen Anderson71a1e572007-07-19 06:45:33 +0000208 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersonb54b3152007-07-18 19:54:15 +0000209 if (isSmall())
Owen Andersone992a562007-07-27 18:07:02 +0000210 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000211 else
Owen Andersone992a562007-07-27 18:07:02 +0000212 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000213 assert(CurArray && "Failed to allocate memory?");
214 }
Owen Anderson69b5d122007-07-09 22:27:20 +0000215
216 // Copy over the new array size
217 CurArraySize = RHS.CurArraySize;
218
219 // Copy over the contents from the other set
220 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000221
222 NumElements = RHS.NumElements;
223 NumTombstones = RHS.NumTombstones;
Chris Lattner91f01582007-07-09 16:54:03 +0000224}
Reid Spencer845b31d2007-07-17 02:16:12 +0000225
226SmallPtrSetImpl::~SmallPtrSetImpl() {
227 if (!isSmall())
228 free(CurArray);
229}