Change allowsUnalignedMemoryAccesses to take type argument since some targets
[llvm/avr.git] / lib / Support / SmallPtrSet.cpp
blob68938fa5a5716849ce499d1b56c7ef0d4fc4da04
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Support/MathExtras.h"
17 #include <cstdlib>
19 using namespace llvm;
21 void SmallPtrSetImpl::shrink_and_clear() {
22 assert(!isSmall() && "Can't shrink a small set!");
23 free(CurArray);
25 // Reduce the number of buckets.
26 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
27 NumElements = NumTombstones = 0;
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*));
34 // The end pointer, always valid, is set to a valid element to help the
35 // iterator.
36 CurArray[CurArraySize] = 0;
39 bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
40 if (isSmall()) {
41 // Check to see if it is already in the set.
42 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
43 APtr != E; ++APtr)
44 if (*APtr == Ptr)
45 return false;
47 // Nope, there isn't. If we stay small, just 'pushback' now.
48 if (NumElements < CurArraySize-1) {
49 SmallArray[NumElements++] = Ptr;
50 return true;
52 // Otherwise, hit the big set case, which will call grow.
55 // If more than 3/4 of the array is full, grow.
56 if (NumElements*4 >= CurArraySize*3 ||
57 CurArraySize-(NumElements+NumTombstones) < CurArraySize/8)
58 Grow();
60 // Okay, we know we have space. Find a hash bucket.
61 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
62 if (*Bucket == Ptr) return false; // Already inserted, good.
64 // Otherwise, insert it!
65 if (*Bucket == getTombstoneMarker())
66 --NumTombstones;
67 *Bucket = Ptr;
68 ++NumElements; // Track density.
69 return true;
72 bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
73 if (isSmall()) {
74 // Check to see if it is in the set.
75 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
76 APtr != E; ++APtr)
77 if (*APtr == Ptr) {
78 // If it is in the set, replace this element.
79 *APtr = E[-1];
80 E[-1] = getEmptyMarker();
81 --NumElements;
82 return true;
85 return false;
88 // Okay, we know we have space. Find a hash bucket.
89 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
90 if (*Bucket != Ptr) return false; // Not in the set?
92 // Set this as a tombstone.
93 *Bucket = getTombstoneMarker();
94 --NumElements;
95 ++NumTombstones;
96 return true;
99 const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
100 unsigned Bucket = Hash(Ptr);
101 unsigned ArraySize = CurArraySize;
102 unsigned ProbeAmt = 1;
103 const void *const *Array = CurArray;
104 const void *const *Tombstone = 0;
105 while (1) {
106 // Found Ptr's bucket?
107 if (Array[Bucket] == Ptr)
108 return Array+Bucket;
110 // If we found an empty bucket, the pointer doesn't exist in the set.
111 // Return a tombstone if we've seen one so far, or the empty bucket if
112 // not.
113 if (Array[Bucket] == getEmptyMarker())
114 return Tombstone ? Tombstone : Array+Bucket;
116 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
117 // prefer to return it than something that would require more probing.
118 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
119 Tombstone = Array+Bucket; // Remember the first tombstone found.
121 // It's a hash collision or a tombstone. Reprobe.
122 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
126 /// Grow - Allocate a larger backing store for the buckets and move it over.
128 void SmallPtrSetImpl::Grow() {
129 // Allocate at twice as many buckets, but at least 128.
130 unsigned OldSize = CurArraySize;
131 unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
133 const void **OldBuckets = CurArray;
134 bool WasSmall = isSmall();
136 // Install the new array. Clear all the buckets to empty.
137 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
138 assert(CurArray && "Failed to allocate memory?");
139 CurArraySize = NewSize;
140 memset(CurArray, -1, NewSize*sizeof(void*));
142 // The end pointer, always valid, is set to a valid element to help the
143 // iterator.
144 CurArray[NewSize] = 0;
146 // Copy over all the elements.
147 if (WasSmall) {
148 // Small sets store their elements in order.
149 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
150 BucketPtr != E; ++BucketPtr) {
151 const void *Elt = *BucketPtr;
152 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
154 } else {
155 // Copy over all valid entries.
156 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
157 BucketPtr != E; ++BucketPtr) {
158 // Copy over the element if it is valid.
159 const void *Elt = *BucketPtr;
160 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
161 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
164 free(OldBuckets);
165 NumTombstones = 0;
169 SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
170 // If we're becoming small, prepare to insert into our stack space
171 if (that.isSmall()) {
172 CurArray = &SmallArray[0];
173 // Otherwise, allocate new heap space (unless we were the same size)
174 } else {
175 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
176 assert(CurArray && "Failed to allocate memory?");
179 // Copy over the new array size
180 CurArraySize = that.CurArraySize;
182 // Copy over the contents from the other set
183 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
185 NumElements = that.NumElements;
186 NumTombstones = that.NumTombstones;
189 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
190 /// type, but may have a different small size.
191 void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
192 if (isSmall() && RHS.isSmall())
193 assert(CurArraySize == RHS.CurArraySize &&
194 "Cannot assign sets with different small sizes");
196 // If we're becoming small, prepare to insert into our stack space
197 if (RHS.isSmall()) {
198 if (!isSmall())
199 free(CurArray);
200 CurArray = &SmallArray[0];
201 // Otherwise, allocate new heap space (unless we were the same size)
202 } else if (CurArraySize != RHS.CurArraySize) {
203 if (isSmall())
204 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
205 else
206 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
207 assert(CurArray && "Failed to allocate memory?");
210 // Copy over the new array size
211 CurArraySize = RHS.CurArraySize;
213 // Copy over the contents from the other set
214 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
216 NumElements = RHS.NumElements;
217 NumTombstones = RHS.NumTombstones;
220 SmallPtrSetImpl::~SmallPtrSetImpl() {
221 if (!isSmall())
222 free(CurArray);