LCOV - code coverage report
Current view: top level - lib/Support - SmallPtrSet.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 137 143 95.8 %
Date: 2017-09-14 15:23:50 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "llvm/Support/ErrorHandling.h"
      19             : #include <algorithm>
      20             : #include <cassert>
      21             : #include <cstdlib>
      22             : 
      23             : using namespace llvm;
      24             : 
      25       41379 : void SmallPtrSetImplBase::shrink_and_clear() {
      26             :   assert(!isSmall() && "Can't shrink a small set!");
      27       41379 :   free(CurArray);
      28             : 
      29             :   // Reduce the number of buckets.
      30       82758 :   unsigned Size = size();
      31       54700 :   CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
      32       41379 :   NumNonEmpty = NumTombstones = 0;
      33             : 
      34             :   // Install the new array.  Clear all the buckets to empty.
      35       41379 :   CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
      36       41379 :   if (CurArray == nullptr)
      37           0 :     report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
      38             : 
      39       41379 :   memset(CurArray, -1, CurArraySize*sizeof(void*));
      40       41379 : }
      41             : 
      42             : std::pair<const void *const *, bool>
      43   870041969 : SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
      44  1740083938 :   if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
      45             :     // If more than 3/4 of the array is full, grow.
      46     2167348 :     Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
      47   867874621 :   } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
      48             :     // If fewer of 1/8 of the array is empty (meaning that many are filled with
      49             :     // tombstones), rehash.
      50        3596 :     Grow(CurArraySize);
      51             :   }
      52             : 
      53             :   // Okay, we know we have space.  Find a hash bucket.
      54   870041969 :   const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
      55   870041969 :   if (*Bucket == Ptr)
      56  1504712742 :     return std::make_pair(Bucket, false); // Already inserted, good.
      57             : 
      58             :   // Otherwise, insert it!
      59   368471055 :   if (*Bucket == getTombstoneMarker())
      60     1490999 :     --NumTombstones;
      61             :   else
      62   366980056 :     ++NumNonEmpty; // Track density.
      63   368471055 :   *Bucket = Ptr;
      64  1105413165 :   return std::make_pair(Bucket, true);
      65             : }
      66             : 
      67  1469186831 : const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
      68  1469186831 :   unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
      69  1469186831 :   unsigned ArraySize = CurArraySize;
      70  1469186831 :   unsigned ProbeAmt = 1;
      71  1469186831 :   const void *const *Array = CurArray;
      72  1469186831 :   const void *const *Tombstone = nullptr;
      73             :   while (true) {
      74             :     // If we found an empty bucket, the pointer doesn't exist in the set.
      75             :     // Return a tombstone if we've seen one so far, or the empty bucket if
      76             :     // not.
      77  4071948647 :     if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
      78   931663805 :       return Tombstone ? Tombstone : Array+Bucket;
      79             : 
      80             :     // Found Ptr's bucket?
      81  1838903934 :     if (LLVM_LIKELY(Array[Bucket] == Ptr))
      82             :       return Array+Bucket;
      83             : 
      84             :     // If this is a tombstone, remember it.  If Ptr ends up not in the set, we
      85             :     // prefer to return it than something that would require more probing.
      86  1301380908 :     if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
      87             :       Tombstone = Array+Bucket;  // Remember the first tombstone found.
      88             : 
      89             :     // It's a hash collision or a tombstone. Reprobe.
      90  1301380908 :     Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
      91             :   }
      92             : }
      93             : 
      94             : /// Grow - Allocate a larger backing store for the buckets and move it over.
      95             : ///
      96     2170944 : void SmallPtrSetImplBase::Grow(unsigned NewSize) {
      97     2170944 :   const void **OldBuckets = CurArray;
      98     2170944 :   const void **OldEnd = EndPointer();
      99     2170944 :   bool WasSmall = isSmall();
     100             : 
     101             :   // Install the new array.  Clear all the buckets to empty.
     102     2170944 :   const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
     103     2170944 :   if (NewBuckets == nullptr)
     104           0 :     report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
     105             : 
     106             :   // Reset member only if memory was allocated successfully
     107     2170944 :   CurArray = NewBuckets;
     108     2170944 :   CurArraySize = NewSize;
     109     2170944 :   memset(CurArray, -1, NewSize*sizeof(void*));
     110             : 
     111             :   // Copy over all valid entries.
     112   688280761 :   for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
     113             :     // Copy over the element if it is valid.
     114   686109817 :     const void *Elt = *BucketPtr;
     115   686109817 :     if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
     116   520907442 :       *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
     117             :   }
     118             : 
     119     2170944 :   if (!WasSmall)
     120      588873 :     free(OldBuckets);
     121     2170944 :   NumNonEmpty -= NumTombstones;
     122     2170944 :   NumTombstones = 0;
     123     2170944 : }
     124             : 
     125    16757319 : SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
     126    16757319 :                                          const SmallPtrSetImplBase &that) {
     127    16757319 :   SmallArray = SmallStorage;
     128             : 
     129             :   // If we're becoming small, prepare to insert into our stack space
     130    16757319 :   if (that.isSmall()) {
     131    16692277 :     CurArray = SmallArray;
     132             :   // Otherwise, allocate new heap space (unless we were the same size)
     133             :   } else {
     134       65042 :     CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
     135       65042 :     if (CurArray == nullptr)
     136           0 :       report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
     137             :   }
     138             : 
     139             :   // Copy over the that array.
     140    16757319 :   CopyHelper(that);
     141    16757315 : }
     142             : 
     143    24683514 : SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
     144             :                                          unsigned SmallSize,
     145    24683514 :                                          SmallPtrSetImplBase &&that) {
     146    24683514 :   SmallArray = SmallStorage;
     147    24683514 :   MoveHelper(SmallSize, std::move(that));
     148    24683515 : }
     149             : 
     150      212050 : void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
     151             :   assert(&RHS != this && "Self-copy should be handled by the caller.");
     152             : 
     153      212050 :   if (isSmall() && RHS.isSmall())
     154             :     assert(CurArraySize == RHS.CurArraySize &&
     155             :            "Cannot assign sets with different small sizes");
     156             : 
     157             :   // If we're becoming small, prepare to insert into our stack space
     158      212050 :   if (RHS.isSmall()) {
     159      212034 :     if (!isSmall())
     160           2 :       free(CurArray);
     161      212034 :     CurArray = SmallArray;
     162             :   // Otherwise, allocate new heap space (unless we were the same size)
     163          16 :   } else if (CurArraySize != RHS.CurArraySize) {
     164          16 :     if (isSmall())
     165          14 :       CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
     166             :     else {
     167           2 :       const void **T = (const void**)realloc(CurArray,
     168           4 :                                              sizeof(void*) * RHS.CurArraySize);
     169           2 :       if (!T)
     170           0 :         free(CurArray);
     171           2 :       CurArray = T;
     172             :     }
     173          16 :     if (CurArray == nullptr)
     174           0 :       report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
     175             :   }
     176             : 
     177      212050 :   CopyHelper(RHS);
     178      212050 : }
     179             : 
     180    16969367 : void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
     181             :   // Copy over the new array size
     182    16969367 :   CurArraySize = RHS.CurArraySize;
     183             : 
     184             :   // Copy over the contents from the other set
     185    50908101 :   std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
     186             : 
     187    16969367 :   NumNonEmpty = RHS.NumNonEmpty;
     188    16969367 :   NumTombstones = RHS.NumTombstones;
     189    16969367 : }
     190             : 
     191        5773 : void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
     192             :                                    SmallPtrSetImplBase &&RHS) {
     193        5773 :   if (!isSmall())
     194           0 :     free(CurArray);
     195        5773 :   MoveHelper(SmallSize, std::move(RHS));
     196        5773 : }
     197             : 
     198    24689288 : void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
     199             :                                      SmallPtrSetImplBase &&RHS) {
     200             :   assert(&RHS != this && "Self-move should be handled by the caller.");
     201             : 
     202    24689288 :   if (RHS.isSmall()) {
     203             :     // Copy a small RHS rather than moving.
     204    24585454 :     CurArray = SmallArray;
     205    24585454 :     std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
     206             :   } else {
     207      103834 :     CurArray = RHS.CurArray;
     208      103834 :     RHS.CurArray = RHS.SmallArray;
     209             :   }
     210             : 
     211             :   // Copy the rest of the trivial members.
     212    24689288 :   CurArraySize = RHS.CurArraySize;
     213    24689288 :   NumNonEmpty = RHS.NumNonEmpty;
     214    24689288 :   NumTombstones = RHS.NumTombstones;
     215             : 
     216             :   // Make the RHS small and empty.
     217    24689288 :   RHS.CurArraySize = SmallSize;
     218             :   assert(RHS.CurArray == RHS.SmallArray);
     219    24689288 :   RHS.NumNonEmpty = 0;
     220    24689288 :   RHS.NumTombstones = 0;
     221    24689288 : }
     222             : 
     223    11677756 : void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
     224    11677756 :   if (this == &RHS) return;
     225             : 
     226             :   // We can only avoid copying elements if neither set is small.
     227    11677756 :   if (!this->isSmall() && !RHS.isSmall()) {
     228           2 :     std::swap(this->CurArray, RHS.CurArray);
     229           2 :     std::swap(this->CurArraySize, RHS.CurArraySize);
     230           2 :     std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
     231           1 :     std::swap(this->NumTombstones, RHS.NumTombstones);
     232             :     return;
     233             :   }
     234             : 
     235             :   // FIXME: From here on we assume that both sets have the same small size.
     236             : 
     237             :   // If only RHS is small, copy the small elements into LHS and move the pointer
     238             :   // from LHS to RHS.
     239    11677755 :   if (!this->isSmall() && RHS.isSmall()) {
     240             :     assert(RHS.CurArray == RHS.SmallArray);
     241        1492 :     std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
     242        1492 :     std::swap(RHS.CurArraySize, this->CurArraySize);
     243        1492 :     std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
     244        1492 :     std::swap(this->NumTombstones, RHS.NumTombstones);
     245         746 :     RHS.CurArray = this->CurArray;
     246         746 :     this->CurArray = this->SmallArray;
     247         746 :     return;
     248             :   }
     249             : 
     250             :   // If only LHS is small, copy the small elements into RHS and move the pointer
     251             :   // from RHS to LHS.
     252    23354018 :   if (this->isSmall() && !RHS.isSmall()) {
     253             :     assert(this->CurArray == this->SmallArray);
     254        1226 :     std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
     255             :               RHS.SmallArray);
     256        1226 :     std::swap(RHS.CurArraySize, this->CurArraySize);
     257        1226 :     std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
     258        1226 :     std::swap(RHS.NumTombstones, this->NumTombstones);
     259         613 :     this->CurArray = RHS.CurArray;
     260         613 :     RHS.CurArray = RHS.SmallArray;
     261         613 :     return;
     262             :   }
     263             : 
     264             :   // Both a small, just swap the small elements.
     265             :   assert(this->isSmall() && RHS.isSmall());
     266    23352792 :   unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
     267    23352792 :   std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
     268             :                    RHS.SmallArray);
     269    11676396 :   if (this->NumNonEmpty > MinNonEmpty) {
     270       97028 :     std::copy(this->SmallArray + MinNonEmpty,
     271       48514 :               this->SmallArray + this->NumNonEmpty,
     272       48514 :               RHS.SmallArray + MinNonEmpty);
     273             :   } else {
     274    11627882 :     std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
     275    11627882 :               this->SmallArray + MinNonEmpty);
     276             :   }
     277             :   assert(this->CurArraySize == RHS.CurArraySize);
     278    23352792 :   std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
     279    11676396 :   std::swap(this->NumTombstones, RHS.NumTombstones);
     280             : }

Generated by: LCOV version 1.13