f7b8a7723cff35edb09084deff99cb35d64ad5ea
[oota-llvm.git] / lib / Support / SmallPtrSet.cpp
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;
22
23 void SmallPtrSetImplBase::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);
33   assert(CurArray && "Failed to allocate memory?");
34   memset(CurArray, -1, CurArraySize*sizeof(void*));
35 }
36
37 bool SmallPtrSetImplBase::insert_imp(const void * Ptr) {
38   if (isSmall()) {
39     // Check to see if it is already in the set.
40     for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
41          APtr != E; ++APtr)
42       if (*APtr == Ptr)
43         return false;
44     
45     // Nope, there isn't.  If we stay small, just 'pushback' now.
46     if (NumElements < CurArraySize-1) {
47       SmallArray[NumElements++] = Ptr;
48       return true;
49     }
50     // Otherwise, hit the big set case, which will call grow.
51   }
52   
53   if (NumElements*4 >= CurArraySize*3) {
54     // If more than 3/4 of the array is full, grow.
55     Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
56   } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
57     // If fewer of 1/8 of the array is empty (meaning that many are filled with
58     // tombstones), rehash.
59     Grow(CurArraySize);
60   }
61   
62   // Okay, we know we have space.  Find a hash bucket.
63   const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
64   if (*Bucket == Ptr) return false; // Already inserted, good.
65   
66   // Otherwise, insert it!
67   if (*Bucket == getTombstoneMarker())
68     --NumTombstones;
69   *Bucket = Ptr;
70   ++NumElements;  // Track density.
71   return true;
72 }
73
74 bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
75   if (isSmall()) {
76     // Check to see if it is in the set.
77     for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
78          APtr != E; ++APtr)
79       if (*APtr == Ptr) {
80         // If it is in the set, replace this element.
81         *APtr = E[-1];
82         E[-1] = getEmptyMarker();
83         --NumElements;
84         return true;
85       }
86     
87     return false;
88   }
89   
90   // Okay, we know we have space.  Find a hash bucket.
91   void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
92   if (*Bucket != Ptr) return false;  // Not in the set?
93
94   // Set this as a tombstone.
95   *Bucket = getTombstoneMarker();
96   --NumElements;
97   ++NumTombstones;
98   return true;
99 }
100
101 const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
102   unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
103   unsigned ArraySize = CurArraySize;
104   unsigned ProbeAmt = 1;
105   const void *const *Array = CurArray;
106   const void *const *Tombstone = 0;
107   while (1) {
108     // Found Ptr's bucket?
109     if (Array[Bucket] == Ptr)
110       return Array+Bucket;
111     
112     // If we found an empty bucket, the pointer doesn't exist in the set.
113     // Return a tombstone if we've seen one so far, or the empty bucket if
114     // not.
115     if (Array[Bucket] == getEmptyMarker())
116       return Tombstone ? Tombstone : Array+Bucket;
117     
118     // If this is a tombstone, remember it.  If Ptr ends up not in the set, we
119     // prefer to return it than something that would require more probing.
120     if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
121       Tombstone = Array+Bucket;  // Remember the first tombstone found.
122     
123     // It's a hash collision or a tombstone. Reprobe.
124     Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
125   }
126 }
127
128 /// Grow - Allocate a larger backing store for the buckets and move it over.
129 ///
130 void SmallPtrSetImplBase::Grow(unsigned NewSize) {
131   // Allocate at twice as many buckets, but at least 128.
132   unsigned OldSize = CurArraySize;
133   
134   const void **OldBuckets = CurArray;
135   bool WasSmall = isSmall();
136   
137   // Install the new array.  Clear all the buckets to empty.
138   CurArray = (const void**)malloc(sizeof(void*) * NewSize);
139   assert(CurArray && "Failed to allocate memory?");
140   CurArraySize = NewSize;
141   memset(CurArray, -1, NewSize*sizeof(void*));
142   
143   // Copy over all the elements.
144   if (WasSmall) {
145     // Small sets store their elements in order.
146     for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
147          BucketPtr != E; ++BucketPtr) {
148       const void *Elt = *BucketPtr;
149       *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
150     }
151   } else {
152     // Copy over all valid entries.
153     for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
154          BucketPtr != E; ++BucketPtr) {
155       // Copy over the element if it is valid.
156       const void *Elt = *BucketPtr;
157       if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
158         *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
159     }
160     
161     free(OldBuckets);
162     NumTombstones = 0;
163   }
164 }
165
166 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
167                                  const SmallPtrSetImplBase& that) {
168   SmallArray = SmallStorage;
169
170   // If we're becoming small, prepare to insert into our stack space
171   if (that.isSmall()) {
172     CurArray = SmallArray;
173   // Otherwise, allocate new heap space (unless we were the same size)
174   } else {
175     CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
176     assert(CurArray && "Failed to allocate memory?");
177   }
178   
179   // Copy over the new array size
180   CurArraySize = that.CurArraySize;
181
182   // Copy over the contents from the other set
183   memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
184   
185   NumElements = that.NumElements;
186   NumTombstones = that.NumTombstones;
187 }
188
189 #if LLVM_HAS_RVALUE_REFERENCES
190 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
191                                          unsigned SmallSize,
192                                          SmallPtrSetImplBase &&that) {
193   SmallArray = SmallStorage;
194
195   // Copy over the basic members.
196   CurArraySize = that.CurArraySize;
197   NumElements = that.NumElements;
198   NumTombstones = that.NumTombstones;
199
200   // When small, just copy into our small buffer.
201   if (that.isSmall()) {
202     CurArray = SmallArray;
203     memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
204     return;
205   }
206
207   // Otherwise, we steal the large memory allocation and no copy is needed.
208   CurArray = that.CurArray;
209   that.CurArray = that.SmallArray;
210
211   // Make the "that" object small and empty.
212   that.CurArraySize = SmallSize;
213   assert(that.CurArray == that.SmallArray);
214   that.NumElements = 0;
215   that.NumTombstones = 0;
216 }
217 #endif
218
219 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
220 /// type, but may have a different small size.
221 void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
222   assert(&RHS != this && "Self-copy should be handled by the caller.");
223
224   if (isSmall() && RHS.isSmall())
225     assert(CurArraySize == RHS.CurArraySize &&
226            "Cannot assign sets with different small sizes");
227
228   // If we're becoming small, prepare to insert into our stack space
229   if (RHS.isSmall()) {
230     if (!isSmall())
231       free(CurArray);
232     CurArray = SmallArray;
233   // Otherwise, allocate new heap space (unless we were the same size)
234   } else if (CurArraySize != RHS.CurArraySize) {
235     if (isSmall())
236       CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
237     else {
238       const void **T = (const void**)realloc(CurArray,
239                                              sizeof(void*) * RHS.CurArraySize);
240       if (!T)
241         free(CurArray);
242       CurArray = T;
243     }
244     assert(CurArray && "Failed to allocate memory?");
245   }
246   
247   // Copy over the new array size
248   CurArraySize = RHS.CurArraySize;
249
250   // Copy over the contents from the other set
251   memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
252   
253   NumElements = RHS.NumElements;
254   NumTombstones = RHS.NumTombstones;
255 }
256
257 #if LLVM_HAS_RVALUE_REFERENCES
258 void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
259                                    SmallPtrSetImplBase &&RHS) {
260   assert(&RHS != this && "Self-move should be handled by the caller.");
261
262   if (!isSmall())
263     free(CurArray);
264
265   if (RHS.isSmall()) {
266     // Copy a small RHS rather than moving.
267     CurArray = SmallArray;
268     memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
269   } else {
270     CurArray = RHS.CurArray;
271     RHS.CurArray = RHS.SmallArray;
272   }
273
274   // Copy the rest of the trivial members.
275   CurArraySize = RHS.CurArraySize;
276   NumElements = RHS.NumElements;
277   NumTombstones = RHS.NumTombstones;
278
279   // Make the RHS small and empty.
280   RHS.CurArraySize = SmallSize;
281   assert(RHS.CurArray == RHS.SmallArray);
282   RHS.NumElements = 0;
283   RHS.NumTombstones = 0;
284 }
285 #endif
286
287 void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
288   if (this == &RHS) return;
289
290   // We can only avoid copying elements if neither set is small.
291   if (!this->isSmall() && !RHS.isSmall()) {
292     std::swap(this->CurArray, RHS.CurArray);
293     std::swap(this->CurArraySize, RHS.CurArraySize);
294     std::swap(this->NumElements, RHS.NumElements);
295     std::swap(this->NumTombstones, RHS.NumTombstones);
296     return;
297   }
298
299   // FIXME: From here on we assume that both sets have the same small size.
300
301   // If only RHS is small, copy the small elements into LHS and move the pointer
302   // from LHS to RHS.
303   if (!this->isSmall() && RHS.isSmall()) {
304     std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
305               this->SmallArray);
306     std::swap(this->NumElements, RHS.NumElements);
307     std::swap(this->CurArraySize, RHS.CurArraySize);
308     RHS.CurArray = this->CurArray;
309     RHS.NumTombstones = this->NumTombstones;
310     this->CurArray = this->SmallArray;
311     this->NumTombstones = 0;
312     return;
313   }
314
315   // If only LHS is small, copy the small elements into RHS and move the pointer
316   // from RHS to LHS.
317   if (this->isSmall() && !RHS.isSmall()) {
318     std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
319               RHS.SmallArray);
320     std::swap(RHS.NumElements, this->NumElements);
321     std::swap(RHS.CurArraySize, this->CurArraySize);
322     this->CurArray = RHS.CurArray;
323     this->NumTombstones = RHS.NumTombstones;
324     RHS.CurArray = RHS.SmallArray;
325     RHS.NumTombstones = 0;
326     return;
327   }
328
329   // Both a small, just swap the small elements.
330   assert(this->isSmall() && RHS.isSmall());
331   assert(this->CurArraySize == RHS.CurArraySize);
332   std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
333                    RHS.SmallArray);
334   std::swap(this->NumElements, RHS.NumElements);
335 }
336
337 SmallPtrSetImplBase::~SmallPtrSetImplBase() {
338   if (!isSmall())
339     free(CurArray);
340 }