LCOV - code coverage report
Current view: top level - lib/Support - FoldingSet.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 162 178 91.0 %
Date: 2018-10-20 13:21:21 Functions: 30 34 88.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Support/FoldingSet.cpp - Uniquing Hash 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 implements a hash set that can be used to remove duplication of
      11             : // nodes in a graph.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/ADT/FoldingSet.h"
      16             : #include "llvm/ADT/Hashing.h"
      17             : #include "llvm/Support/Allocator.h"
      18             : #include "llvm/Support/ErrorHandling.h"
      19             : #include "llvm/Support/Host.h"
      20             : #include "llvm/Support/MathExtras.h"
      21             : #include <cassert>
      22             : #include <cstring>
      23             : using namespace llvm;
      24             : 
      25             : //===----------------------------------------------------------------------===//
      26             : // FoldingSetNodeIDRef Implementation
      27             : 
      28             : /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
      29             : /// used to lookup the node in the FoldingSetBase.
      30  1334048764 : unsigned FoldingSetNodeIDRef::ComputeHash() const {
      31  1334048764 :   return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
      32             : }
      33             : 
      34  1287946753 : bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
      35  1287946753 :   if (Size != RHS.Size) return false;
      36  1101014582 :   return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
      37             : }
      38             : 
      39             : /// Used to compare the "ordering" of two nodes as defined by the
      40             : /// profiled bits and their ordering defined by memcmp().
      41         276 : bool FoldingSetNodeIDRef::operator<(FoldingSetNodeIDRef RHS) const {
      42         276 :   if (Size != RHS.Size)
      43         151 :     return Size < RHS.Size;
      44         125 :   return memcmp(Data, RHS.Data, Size*sizeof(*Data)) < 0;
      45             : }
      46             : 
      47             : //===----------------------------------------------------------------------===//
      48             : // FoldingSetNodeID Implementation
      49             : 
      50             : /// Add* - Add various data types to Bit data.
      51             : ///
      52  4946548633 : void FoldingSetNodeID::AddPointer(const void *Ptr) {
      53             :   // Note: this adds pointers to the hash using sizes and endianness that
      54             :   // depend on the host. It doesn't matter, however, because hashing on
      55             :   // pointer values is inherently unstable. Nothing should depend on the
      56             :   // ordering of nodes in the folding set.
      57             :   static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long),
      58             :                 "unexpected pointer size");
      59  4946548633 :   AddInteger(reinterpret_cast<uintptr_t>(Ptr));
      60  4946548717 : }
      61  1060375629 : void FoldingSetNodeID::AddInteger(signed I) {
      62  1060375629 :   Bits.push_back(I);
      63  1060375629 : }
      64 12989978484 : void FoldingSetNodeID::AddInteger(unsigned I) {
      65 12989978484 :   Bits.push_back(I);
      66 12989979010 : }
      67    66629681 : void FoldingSetNodeID::AddInteger(long I) {
      68    66629681 :   AddInteger((unsigned long)I);
      69    66629682 : }
      70  5240782148 : void FoldingSetNodeID::AddInteger(unsigned long I) {
      71             :   if (sizeof(long) == sizeof(int))
      72             :     AddInteger(unsigned(I));
      73             :   else if (sizeof(long) == sizeof(long long)) {
      74  5240782148 :     AddInteger((unsigned long long)I);
      75             :   } else {
      76             :     llvm_unreachable("unexpected sizeof(long)");
      77             :   }
      78  5240782559 : }
      79           0 : void FoldingSetNodeID::AddInteger(long long I) {
      80           0 :   AddInteger((unsigned long long)I);
      81           0 : }
      82  5240787713 : void FoldingSetNodeID::AddInteger(unsigned long long I) {
      83  5240787713 :   AddInteger(unsigned(I));
      84  5240787715 :   AddInteger(unsigned(I >> 32));
      85  5240787743 : }
      86             : 
      87   295750186 : void FoldingSetNodeID::AddString(StringRef String) {
      88   295750186 :   unsigned Size =  String.size();
      89   295750186 :   Bits.push_back(Size);
      90   316130025 :   if (!Size) return;
      91             : 
      92   295737447 :   unsigned Units = Size / 4;
      93             :   unsigned Pos = 0;
      94             :   const unsigned *Base = (const unsigned*) String.data();
      95             : 
      96             :   // If the string is aligned do a bulk transfer.
      97   295737447 :   if (!((intptr_t)Base & 3)) {
      98   294896102 :     Bits.append(Base, Base + Units);
      99   294896102 :     Pos = (Units + 1) * 4;
     100             :   } else {
     101             :     // Otherwise do it the hard way.
     102             :     // To be compatible with above bulk transfer, we need to take endianness
     103             :     // into account.
     104             :     static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost,
     105             :                   "Unexpected host endianness");
     106             :     if (sys::IsBigEndianHost) {
     107             :       for (Pos += 4; Pos <= Size; Pos += 4) {
     108             :         unsigned V = ((unsigned char)String[Pos - 4] << 24) |
     109             :                      ((unsigned char)String[Pos - 3] << 16) |
     110             :                      ((unsigned char)String[Pos - 2] << 8) |
     111             :                       (unsigned char)String[Pos - 1];
     112             :         Bits.push_back(V);
     113             :       }
     114             :     } else {  // Little-endian host
     115     3378200 :       for (Pos += 4; Pos <= Size; Pos += 4) {
     116     2536855 :         unsigned V = ((unsigned char)String[Pos - 1] << 24) |
     117     2536855 :                      ((unsigned char)String[Pos - 2] << 16) |
     118     2536855 :                      ((unsigned char)String[Pos - 3] << 8) |
     119     2536855 :                       (unsigned char)String[Pos - 4];
     120     2536855 :         Bits.push_back(V);
     121             :       }
     122             :     }
     123             :   }
     124             : 
     125             :   // With the leftover bits.
     126   295737447 :   unsigned V = 0;
     127             :   // Pos will have overshot size by 4 - #bytes left over.
     128             :   // No need to take endianness into account here - this is always executed.
     129   295737447 :   switch (Pos - Size) {
     130   104539814 :   case 1: V = (V << 8) | (unsigned char)String[Size - 3]; LLVM_FALLTHROUGH;
     131   270539874 :   case 2: V = (V << 8) | (unsigned char)String[Size - 2]; LLVM_FALLTHROUGH;
     132   275357608 :   case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
     133             :   default: return; // Nothing left.
     134             :   }
     135             : 
     136   275357608 :   Bits.push_back(V);
     137             : }
     138             : 
     139             : // AddNodeID - Adds the Bit data of another ID to *this.
     140    11649675 : void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) {
     141    23299350 :   Bits.append(ID.Bits.begin(), ID.Bits.end());
     142    11649675 : }
     143             : 
     144             : /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
     145             : /// lookup the node in the FoldingSetBase.
     146  1333432226 : unsigned FoldingSetNodeID::ComputeHash() const {
     147  4000296678 :   return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
     148             : }
     149             : 
     150             : /// operator== - Used to compare two nodes to each other.
     151             : ///
     152  1254730450 : bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const {
     153  3764191350 :   return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
     154             : }
     155             : 
     156             : /// operator== - Used to compare two nodes to each other.
     157             : ///
     158  1287946753 : bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
     159  3863840259 :   return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
     160             : }
     161             : 
     162             : /// Used to compare the "ordering" of two nodes as defined by the
     163             : /// profiled bits and their ordering defined by memcmp().
     164         276 : bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const {
     165         828 :   return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
     166             : }
     167             : 
     168         276 : bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const {
     169         828 :   return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS;
     170             : }
     171             : 
     172             : /// Intern - Copy this node's data to a memory region allocated from the
     173             : /// given allocator and return a FoldingSetNodeIDRef describing the
     174             : /// interned data.
     175             : FoldingSetNodeIDRef
     176     1507833 : FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
     177     1507833 :   unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
     178             :   std::uninitialized_copy(Bits.begin(), Bits.end(), New);
     179     1507833 :   return FoldingSetNodeIDRef(New, Bits.size());
     180             : }
     181             : 
     182             : //===----------------------------------------------------------------------===//
     183             : /// Helper functions for FoldingSetBase.
     184             : 
     185             : /// GetNextPtr - In order to save space, each bucket is a
     186             : /// singly-linked-list. In order to make deletion more efficient, we make
     187             : /// the list circular, so we can delete a node without computing its hash.
     188             : /// The problem with this is that the start of the hash buckets are not
     189             : /// Nodes.  If NextInBucketPtr is a bucket pointer, this method returns null:
     190             : /// use GetBucketPtr when this happens.
     191             : static FoldingSetBase::Node *GetNextPtr(void *NextInBucketPtr) {
     192             :   // The low bit is set if this is the pointer back to the bucket.
     193  2163731078 :   if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
     194             :     return nullptr;
     195             : 
     196             :   return static_cast<FoldingSetBase::Node*>(NextInBucketPtr);
     197             : }
     198             : 
     199             : 
     200             : /// testing.
     201             : static void **GetBucketPtr(void *NextInBucketPtr) {
     202             :   intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
     203             :   assert((Ptr & 1) && "Not a bucket pointer");
     204     1089351 :   return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
     205             : }
     206             : 
     207             : /// GetBucketFor - Hash the specified node ID and return the hash bucket for
     208             : /// the specified ID.
     209             : static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
     210             :   // NumBuckets is always a power of 2.
     211  1312908575 :   unsigned BucketNum = Hash & (NumBuckets-1);
     212      335499 :   return Buckets + BucketNum;
     213             : }
     214             : 
     215             : /// AllocateBuckets - Allocated initialized bucket memory.
     216             : static void **AllocateBuckets(unsigned NumBuckets) {
     217    11472062 :   void **Buckets = static_cast<void**>(safe_calloc(NumBuckets + 1,
     218             :                                                    sizeof(void*)));
     219             :   // Set the very last bucket to be a non-null "pointer".
     220    11472064 :   Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
     221             :   return Buckets;
     222             : }
     223             : 
     224             : //===----------------------------------------------------------------------===//
     225             : // FoldingSetBase Implementation
     226             : 
     227           0 : void FoldingSetBase::anchor() {}
     228             : 
     229    11126810 : FoldingSetBase::FoldingSetBase(unsigned Log2InitSize) {
     230             :   assert(5 < Log2InitSize && Log2InitSize < 32 &&
     231             :          "Initial hash table size out of range");
     232    11126810 :   NumBuckets = 1 << Log2InitSize;
     233    11126812 :   Buckets = AllocateBuckets(NumBuckets);
     234    11126812 :   NumNodes = 0;
     235    11126812 : }
     236             : 
     237        3652 : FoldingSetBase::FoldingSetBase(FoldingSetBase &&Arg)
     238        3652 :     : Buckets(Arg.Buckets), NumBuckets(Arg.NumBuckets), NumNodes(Arg.NumNodes) {
     239        3652 :   Arg.Buckets = nullptr;
     240        3652 :   Arg.NumBuckets = 0;
     241        3652 :   Arg.NumNodes = 0;
     242        3652 : }
     243             : 
     244           0 : FoldingSetBase &FoldingSetBase::operator=(FoldingSetBase &&RHS) {
     245           0 :   free(Buckets); // This may be null if the set is in a moved-from state.
     246           0 :   Buckets = RHS.Buckets;
     247           0 :   NumBuckets = RHS.NumBuckets;
     248           0 :   NumNodes = RHS.NumNodes;
     249           0 :   RHS.Buckets = nullptr;
     250           0 :   RHS.NumBuckets = 0;
     251           0 :   RHS.NumNodes = 0;
     252           0 :   return *this;
     253             : }
     254             : 
     255     6722894 : FoldingSetBase::~FoldingSetBase() {
     256     3361447 :   free(Buckets);
     257     3361447 : }
     258             : 
     259     1270699 : void FoldingSetBase::clear() {
     260             :   // Set all but the last bucket to null pointers.
     261     1270699 :   memset(Buckets, 0, NumBuckets*sizeof(void*));
     262             : 
     263             :   // Set the very last bucket to be a non-null "pointer".
     264     1270699 :   Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
     265             : 
     266             :   // Reset the node count to zero.
     267     1270699 :   NumNodes = 0;
     268     1270699 : }
     269             : 
     270      345252 : void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount) {
     271             :   assert((NewBucketCount > NumBuckets) && "Can't shrink a folding set with GrowBucketCount");
     272             :   assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!");
     273      345252 :   void **OldBuckets = Buckets;
     274      345252 :   unsigned OldNumBuckets = NumBuckets;
     275             : 
     276             :   // Clear out new buckets.
     277      345252 :   Buckets = AllocateBuckets(NewBucketCount);
     278             :   // Set NumBuckets only if allocation of new buckets was successful.
     279      345252 :   NumBuckets = NewBucketCount;
     280      345252 :   NumNodes = 0;
     281             : 
     282             :   // Walk the old buckets, rehashing nodes into their new place.
     283             :   FoldingSetNodeID TempID;
     284   150409188 :   for (unsigned i = 0; i != OldNumBuckets; ++i) {
     285   150063936 :     void *Probe = OldBuckets[i];
     286   150063936 :     if (!Probe) continue;
     287   298889342 :     while (Node *NodeInBucket = GetNextPtr(Probe)) {
     288             :       // Figure out the next link, remove NodeInBucket from the old link.
     289   298889342 :       Probe = NodeInBucket->getNextInBucket();
     290             :       NodeInBucket->SetNextInBucket(nullptr);
     291             : 
     292             :       // Insert the node into the new bucket, after recomputing the hash.
     293   298889342 :       InsertNode(NodeInBucket,
     294   298889342 :                  GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
     295             :                               Buckets, NumBuckets));
     296             :       TempID.clear();
     297             :     }
     298             :   }
     299             : 
     300      345252 :   free(OldBuckets);
     301      345252 : }
     302             : 
     303             : /// GrowHashTable - Double the size of the hash table and rehash everything.
     304             : ///
     305      335499 : void FoldingSetBase::GrowHashTable() {
     306      335499 :   GrowBucketCount(NumBuckets * 2);
     307      335499 : }
     308             : 
     309        9761 : void FoldingSetBase::reserve(unsigned EltCount) {
     310             :   // This will give us somewhere between EltCount / 2 and
     311             :   // EltCount buckets.  This puts us in the load factor
     312             :   // range of 1.0 - 2.0.
     313       19522 :   if(EltCount < capacity())
     314             :     return;
     315       19506 :   GrowBucketCount(PowerOf2Floor(EltCount));
     316             : }
     317             : 
     318             : /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
     319             : /// return it.  If not, return the insertion token that will make insertion
     320             : /// faster.
     321             : FoldingSetBase::Node *
     322  1013683738 : FoldingSetBase::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
     323             :                                     void *&InsertPos) {
     324  1013683738 :   unsigned IDHash = ID.ComputeHash();
     325  1013683734 :   void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets);
     326  1013683734 :   void *Probe = *Bucket;
     327             : 
     328  1013683734 :   InsertPos = nullptr;
     329             : 
     330             :   FoldingSetNodeID TempID;
     331  1427647413 :   while (Node *NodeInBucket = GetNextPtr(Probe)) {
     332  1288747572 :     if (NodeEquals(NodeInBucket, ID, IDHash, TempID))
     333   626590313 :       return NodeInBucket;
     334             :     TempID.clear();
     335             : 
     336   662157258 :     Probe = NodeInBucket->getNextInBucket();
     337   662157258 :   }
     338             : 
     339             :   // Didn't find the node, return null with the bucket as the InsertPos.
     340   387093420 :   InsertPos = Bucket;
     341   387093420 :   return nullptr;
     342             : }
     343             : 
     344             : /// InsertNode - Insert the specified node into the folding set, knowing that it
     345             : /// is not already in the map.  InsertPos must be obtained from
     346             : /// FindNodeOrInsertPos.
     347   605411302 : void FoldingSetBase::InsertNode(Node *N, void *InsertPos) {
     348             :   assert(!N->getNextInBucket());
     349             :   // Do we need to grow the hashtable?
     350  1210822604 :   if (NumNodes+1 > capacity()) {
     351      335499 :     GrowHashTable();
     352             :     FoldingSetNodeID TempID;
     353      335499 :     InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
     354             :   }
     355             : 
     356   605411302 :   ++NumNodes;
     357             : 
     358             :   /// The insert position is actually a bucket pointer.
     359             :   void **Bucket = static_cast<void**>(InsertPos);
     360             : 
     361   605411302 :   void *Next = *Bucket;
     362             : 
     363             :   // If this is the first insertion into this bucket, its next pointer will be
     364             :   // null.  Pretend as if it pointed to itself, setting the low bit to indicate
     365             :   // that it is a pointer to the bucket.
     366   605411302 :   if (!Next)
     367   305131132 :     Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
     368             : 
     369             :   // Set the node's next pointer, and make the bucket point to the node.
     370             :   N->SetNextInBucket(Next);
     371   605411302 :   *Bucket = N;
     372   605411302 : }
     373             : 
     374             : /// RemoveNode - Remove a node from the folding set, returning true if one was
     375             : /// removed or false if the node was not in the folding set.
     376    44306201 : bool FoldingSetBase::RemoveNode(Node *N) {
     377             :   // Because each bucket is a circular list, we don't need to compute N's hash
     378             :   // to remove it.
     379    44306201 :   void *Ptr = N->getNextInBucket();
     380    44306201 :   if (!Ptr) return false;  // Not in folding set.
     381             : 
     382    38756108 :   --NumNodes;
     383             :   N->SetNextInBucket(nullptr);
     384             : 
     385             :   // Remember what N originally pointed to, either a bucket or another node.
     386             :   void *NodeNextPtr = Ptr;
     387             : 
     388             :   // Chase around the list until we find the node (or bucket) which points to N.
     389             :   while (true) {
     390    19873097 :     if (Node *NodeInBucket = GetNextPtr(Ptr)) {
     391             :       // Advance pointer.
     392    19873097 :       Ptr = NodeInBucket->getNextInBucket();
     393             : 
     394             :       // We found a node that points to N, change it to point to N's next node,
     395             :       // removing N from the list.
     396    19873097 :       if (Ptr == N) {
     397             :         NodeInBucket->SetNextInBucket(NodeNextPtr);
     398     7197156 :         return true;
     399             :       }
     400             :     } else {
     401             :       void **Bucket = GetBucketPtr(Ptr);
     402    38756108 :       Ptr = *Bucket;
     403             : 
     404             :       // If we found that the bucket points to N, update the bucket to point to
     405             :       // whatever is next.
     406    38756108 :       if (Ptr == N) {
     407    31558952 :         *Bucket = NodeNextPtr;
     408    31558952 :         return true;
     409             :       }
     410             :     }
     411             :   }
     412             : }
     413             : 
     414             : /// GetOrInsertNode - If there is an existing simple Node exactly
     415             : /// equal to the specified node, return it.  Otherwise, insert 'N' and it
     416             : /// instead.
     417    10937652 : FoldingSetBase::Node *FoldingSetBase::GetOrInsertNode(FoldingSetBase::Node *N) {
     418             :   FoldingSetNodeID ID;
     419    10937652 :   GetNodeProfile(N, ID);
     420             :   void *IP;
     421    10937652 :   if (Node *E = FindNodeOrInsertPos(ID, IP))
     422             :     return E;
     423    10861012 :   InsertNode(N, IP);
     424    10861012 :   return N;
     425             : }
     426             : 
     427             : //===----------------------------------------------------------------------===//
     428             : // FoldingSetIteratorImpl Implementation
     429             : 
     430      556399 : FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
     431             :   // Skip to the first non-null non-self-cycle bucket.
     432    13526706 :   while (*Bucket != reinterpret_cast<void*>(-1) &&
     433             :          (!*Bucket || !GetNextPtr(*Bucket)))
     434    12970307 :     ++Bucket;
     435             : 
     436      556399 :   NodePtr = static_cast<FoldingSetNode*>(*Bucket);
     437      556399 : }
     438             : 
     439     1434331 : void FoldingSetIteratorImpl::advance() {
     440             :   // If there is another link within this bucket, go to it.
     441     1434331 :   void *Probe = NodePtr->getNextInBucket();
     442             : 
     443      344981 :   if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
     444      344980 :     NodePtr = NextNodeInBucket;
     445             :   else {
     446             :     // Otherwise, this is the last link in this bucket.
     447             :     void **Bucket = GetBucketPtr(Probe);
     448             : 
     449             :     // Skip to the next non-null non-self-cycle bucket.
     450             :     do {
     451    48195295 :       ++Bucket;
     452    48195295 :     } while (*Bucket != reinterpret_cast<void*>(-1) &&
     453             :              (!*Bucket || !GetNextPtr(*Bucket)));
     454             : 
     455     1089351 :     NodePtr = static_cast<FoldingSetNode*>(*Bucket);
     456             :   }
     457     1434331 : }
     458             : 
     459             : //===----------------------------------------------------------------------===//
     460             : // FoldingSetBucketIteratorImpl Implementation
     461             : 
     462           0 : FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
     463           0 :   Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket;
     464           0 : }

Generated by: LCOV version 1.13