1 //===- llvm/ADT/SmallPtrSet.h - 'Normally small' pointer set ----*- C++ -*-===// 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 defines the SmallPtrSet class. See the doxygen comment for 11 // SmallPtrSetImplBase for more details on the algorithm used. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_ADT_SMALLPTRSET_H 16 #define LLVM_ADT_SMALLPTRSET_H 17 18 #include "llvm/Support/Compiler.h" 19 #include "llvm/Support/DataTypes.h" 20 #include "llvm/Support/PointerLikeTypeTraits.h" 21 #include <cassert> 22 #include <cstddef> 23 #include <cstring> 24 #include <cstdlib> 25 #include <iterator> 26 #include <utility> 27 28 namespace llvm { 29 30 class SmallPtrSetIteratorImpl; 31 32 /// SmallPtrSetImplBase - This is the common code shared among all the 33 /// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one 34 /// for small and one for large sets. 35 /// 36 /// Small sets use an array of pointers allocated in the SmallPtrSet object, 37 /// which is treated as a simple array of pointers. When a pointer is added to 38 /// the set, the array is scanned to see if the element already exists, if not 39 /// the element is 'pushed back' onto the array. If we run out of space in the 40 /// array, we grow into the 'large set' case. SmallSet should be used when the 41 /// sets are often small. In this case, no memory allocation is used, and only 42 /// light-weight and cache-efficient scanning is used. 43 /// 44 /// Large sets use a classic exponentially-probed hash table. Empty buckets are 45 /// represented with an illegal pointer value (-1) to allow null pointers to be 46 /// inserted. Tombstones are represented with another illegal pointer value 47 /// (-2), to allow deletion. The hash table is resized when the table is 3/4 or 48 /// more. When this happens, the table is doubled in size. 49 /// 50 class SmallPtrSetImplBase { 51 friend class SmallPtrSetIteratorImpl; 52 53 protected: 54 /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'. 55 const void **SmallArray; 56 /// CurArray - This is the current set of buckets. If equal to SmallArray, 57 /// then the set is in 'small mode'. 58 const void **CurArray; 59 /// CurArraySize - The allocated size of CurArray, always a power of two. 60 unsigned CurArraySize; 61 62 /// Number of elements in CurArray that contain a value or are a tombstone. 63 /// If small, all these elements are at the beginning of CurArray and the rest 64 /// is uninitialized. 65 unsigned NumNonEmpty; 66 /// Number of tombstones in CurArray. 67 unsigned NumTombstones; 68 69 // Helpers to copy and move construct a SmallPtrSet. 70 SmallPtrSetImplBase(const void **SmallStorage, 71 const SmallPtrSetImplBase &that); 72 SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize, 73 SmallPtrSetImplBase &&that); 74 explicit SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize) 75 : SmallArray(SmallStorage), CurArray(SmallStorage), 76 CurArraySize(SmallSize), NumNonEmpty(0), NumTombstones(0) { 77 assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 && 78 "Initial size must be a power of two!"); 79 } 80 ~SmallPtrSetImplBase() { 81 if (!isSmall()) 82 free(CurArray); 83 } 84 85 public: 86 typedef unsigned size_type; 87 bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return size() == 0; } 88 size_type size() const { return NumNonEmpty - NumTombstones; } 89 90 void clear() { 91 // If the capacity of the array is huge, and the # elements used is small, 92 // shrink the array. 93 if (!isSmall()) { 94 if (size() * 4 < CurArraySize && CurArraySize > 32) 95 return shrink_and_clear(); 96 // Fill the array with empty markers. 97 memset(CurArray, -1, CurArraySize * sizeof(void *)); 98 } 99 100 NumNonEmpty = 0; 101 NumTombstones = 0; 102 } 103 104 protected: 105 static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); } 106 static void *getEmptyMarker() { 107 // Note that -1 is chosen to make clear() efficiently implementable with 108 // memset and because it's not a valid pointer value. 109 return reinterpret_cast<void*>(-1); 110 } 111 112 const void **EndPointer() const { 113 return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize; 114 } 115 116 /// insert_imp - This returns true if the pointer was new to the set, false if 117 /// it was already in the set. This is hidden from the client so that the 118 /// derived class can check that the right type of pointer is passed in. 119 std::pair<const void *const *, bool> insert_imp(const void *Ptr) { 120 if (isSmall()) { 121 // Check to see if it is already in the set. 122 const void **LastTombstone = nullptr; 123 for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty; 124 APtr != E; ++APtr) { 125 const void *Value = *APtr; 126 if (Value == Ptr) 127 return std::make_pair(APtr, false); 128 if (Value == getTombstoneMarker()) 129 LastTombstone = APtr; 130 } 131 132 // Did we find any tombstone marker? 133 if (LastTombstone != nullptr) { 134 *LastTombstone = Ptr; 135 --NumTombstones; 136 return std::make_pair(LastTombstone, true); 137 } 138 139 // Nope, there isn't. If we stay small, just 'pushback' now. 140 if (NumNonEmpty < CurArraySize) { 141 SmallArray[NumNonEmpty++] = Ptr; 142 return std::make_pair(SmallArray + (NumNonEmpty - 1), true); 143 } 144 // Otherwise, hit the big set case, which will call grow. 145 } 146 return insert_imp_big(Ptr); 147 } 148 149 /// erase_imp - If the set contains the specified pointer, remove it and 150 /// return true, otherwise return false. This is hidden from the client so 151 /// that the derived class can check that the right type of pointer is passed 152 /// in. 153 bool erase_imp(const void * Ptr); 154 155 bool count_imp(const void * Ptr) const { 156 if (isSmall()) { 157 // Linear search for the item. 158 for (const void *const *APtr = SmallArray, 159 *const *E = SmallArray + NumNonEmpty; APtr != E; ++APtr) 160 if (*APtr == Ptr) 161 return true; 162 return false; 163 } 164 165 // Big set case. 166 return *FindBucketFor(Ptr) == Ptr; 167 } 168 169 private: 170 bool isSmall() const { return CurArray == SmallArray; } 171 172 std::pair<const void *const *, bool> insert_imp_big(const void *Ptr); 173 174 const void * const *FindBucketFor(const void *Ptr) const; 175 void shrink_and_clear(); 176 177 /// Grow - Allocate a larger backing store for the buckets and move it over. 178 void Grow(unsigned NewSize); 179 180 void operator=(const SmallPtrSetImplBase &RHS) = delete; 181 182 protected: 183 /// swap - Swaps the elements of two sets. 184 /// Note: This method assumes that both sets have the same small size. 185 void swap(SmallPtrSetImplBase &RHS); 186 187 void CopyFrom(const SmallPtrSetImplBase &RHS); 188 void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS); 189 190 private: 191 /// Code shared by MoveFrom() and move constructor. 192 void MoveHelper(unsigned SmallSize, SmallPtrSetImplBase &&RHS); 193 /// Code shared by CopyFrom() and copy constructor. 194 void CopyHelper(const SmallPtrSetImplBase &RHS); 195 }; 196 197 /// SmallPtrSetIteratorImpl - This is the common base class shared between all 198 /// instances of SmallPtrSetIterator. 199 class SmallPtrSetIteratorImpl { 200 protected: 201 const void *const *Bucket; 202 const void *const *End; 203 204 public: 205 explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E) 206 : Bucket(BP), End(E) { 207 AdvanceIfNotValid(); 208 } 209 210 bool operator==(const SmallPtrSetIteratorImpl &RHS) const { 211 return Bucket == RHS.Bucket; 212 } 213 bool operator!=(const SmallPtrSetIteratorImpl &RHS) const { 214 return Bucket != RHS.Bucket; 215 } 216 217 protected: 218 /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket 219 /// that is. This is guaranteed to stop because the end() bucket is marked 220 /// valid. 221 void AdvanceIfNotValid() { 222 assert(Bucket <= End); 223 while (Bucket != End && 224 (*Bucket == SmallPtrSetImplBase::getEmptyMarker() || 225 *Bucket == SmallPtrSetImplBase::getTombstoneMarker())) 226 ++Bucket; 227 } 228 }; 229 230 /// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet. 231 template<typename PtrTy> 232 class SmallPtrSetIterator : public SmallPtrSetIteratorImpl { 233 typedef PointerLikeTypeTraits<PtrTy> PtrTraits; 234 235 public: 236 typedef PtrTy value_type; 237 typedef PtrTy reference; 238 typedef PtrTy pointer; 239 typedef std::ptrdiff_t difference_type; 240 typedef std::forward_iterator_tag iterator_category; 241 242 explicit SmallPtrSetIterator(const void *const *BP, const void *const *E) 243 : SmallPtrSetIteratorImpl(BP, E) {} 244 245 // Most methods provided by baseclass. 246 247 const PtrTy operator*() const { 248 assert(Bucket < End); 249 return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket)); 250 } 251 252 inline SmallPtrSetIterator& operator++() { // Preincrement 253 ++Bucket; 254 AdvanceIfNotValid(); 255 return *this; 256 } 257 258 SmallPtrSetIterator operator++(int) { // Postincrement 259 SmallPtrSetIterator tmp = *this; ++*this; return tmp; 260 } 261 }; 262 263 /// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next 264 /// power of two (which means N itself if N is already a power of two). 265 template<unsigned N> 266 struct RoundUpToPowerOfTwo; 267 268 /// RoundUpToPowerOfTwoH - If N is not a power of two, increase it. This is a 269 /// helper template used to implement RoundUpToPowerOfTwo. 270 template<unsigned N, bool isPowerTwo> 271 struct RoundUpToPowerOfTwoH { 272 enum { Val = N }; 273 }; 274 template<unsigned N> 275 struct RoundUpToPowerOfTwoH<N, false> { 276 enum { 277 // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets 278 // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111. 279 Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val 280 }; 281 }; 282 283 template<unsigned N> 284 struct RoundUpToPowerOfTwo { 285 enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val }; 286 }; 287 288 /// \brief A templated base class for \c SmallPtrSet which provides the 289 /// typesafe interface that is common across all small sizes. 290 /// 291 /// This is particularly useful for passing around between interface boundaries 292 /// to avoid encoding a particular small size in the interface boundary. 293 template <typename PtrType> 294 class SmallPtrSetImpl : public SmallPtrSetImplBase { 295 typedef PointerLikeTypeTraits<PtrType> PtrTraits; 296 297 SmallPtrSetImpl(const SmallPtrSetImpl &) = delete; 298 299 protected: 300 // Constructors that forward to the base. 301 SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that) 302 : SmallPtrSetImplBase(SmallStorage, that) {} 303 SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize, 304 SmallPtrSetImpl &&that) 305 : SmallPtrSetImplBase(SmallStorage, SmallSize, std::move(that)) {} 306 explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) 307 : SmallPtrSetImplBase(SmallStorage, SmallSize) {} 308 309 public: 310 typedef SmallPtrSetIterator<PtrType> iterator; 311 typedef SmallPtrSetIterator<PtrType> const_iterator; 312 313 /// Inserts Ptr if and only if there is no element in the container equal to 314 /// Ptr. The bool component of the returned pair is true if and only if the 315 /// insertion takes place, and the iterator component of the pair points to 316 /// the element equal to Ptr. 317 std::pair<iterator, bool> insert(PtrType Ptr) { 318 auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr)); 319 return std::make_pair(iterator(p.first, EndPointer()), p.second); 320 } 321 322 /// erase - If the set contains the specified pointer, remove it and return 323 /// true, otherwise return false. 324 bool erase(PtrType Ptr) { 325 return erase_imp(PtrTraits::getAsVoidPointer(Ptr)); 326 } 327 328 /// count - Return 1 if the specified pointer is in the set, 0 otherwise. 329 size_type count(PtrType Ptr) const { 330 return count_imp(PtrTraits::getAsVoidPointer(Ptr)) ? 1 : 0; 331 } 332 333 template <typename IterT> 334 void insert(IterT I, IterT E) { 335 for (; I != E; ++I) 336 insert(*I); 337 } 338 339 inline iterator begin() const { 340 return iterator(CurArray, EndPointer()); 341 } 342 inline iterator end() const { 343 const void *const *End = EndPointer(); 344 return iterator(End, End); 345 } 346 }; 347 348 /// SmallPtrSet - This class implements a set which is optimized for holding 349 /// SmallSize or less elements. This internally rounds up SmallSize to the next 350 /// power of two if it is not already a power of two. See the comments above 351 /// SmallPtrSetImplBase for details of the algorithm. 352 template<class PtrType, unsigned SmallSize> 353 class SmallPtrSet : public SmallPtrSetImpl<PtrType> { 354 // In small mode SmallPtrSet uses linear search for the elements, so it is 355 // not a good idea to choose this value too high. You may consider using a 356 // DenseSet<> instead if you expect many elements in the set. 357 static_assert(SmallSize <= 32, "SmallSize should be small"); 358 359 typedef SmallPtrSetImpl<PtrType> BaseT; 360 361 // Make sure that SmallSize is a power of two, round up if not. 362 enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val }; 363 /// SmallStorage - Fixed size storage used in 'small mode'. 364 const void *SmallStorage[SmallSizePowTwo]; 365 366 public: 367 SmallPtrSet() : BaseT(SmallStorage, SmallSizePowTwo) {} 368 SmallPtrSet(const SmallPtrSet &that) : BaseT(SmallStorage, that) {} 369 SmallPtrSet(SmallPtrSet &&that) 370 : BaseT(SmallStorage, SmallSizePowTwo, std::move(that)) {} 371 372 template<typename It> 373 SmallPtrSet(It I, It E) : BaseT(SmallStorage, SmallSizePowTwo) { 374 this->insert(I, E); 375 } 376 377 SmallPtrSet<PtrType, SmallSize> & 378 operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) { 379 if (&RHS != this) 380 this->CopyFrom(RHS); 381 return *this; 382 } 383 384 SmallPtrSet<PtrType, SmallSize>& 385 operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) { 386 if (&RHS != this) 387 this->MoveFrom(SmallSizePowTwo, std::move(RHS)); 388 return *this; 389 } 390 391 /// swap - Swaps the elements of two sets. 392 void swap(SmallPtrSet<PtrType, SmallSize> &RHS) { 393 SmallPtrSetImplBase::swap(RHS); 394 } 395 }; 396 } 397 398 namespace std { 399 /// Implement std::swap in terms of SmallPtrSet swap. 400 template<class T, unsigned N> 401 inline void swap(llvm::SmallPtrSet<T, N> &LHS, llvm::SmallPtrSet<T, N> &RHS) { 402 LHS.swap(RHS); 403 } 404 } 405 406 #endif 407