1 //===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a hash set that can be used to remove duplication of
11 // nodes in a graph. This code was originally created by Chris Lattner for use
12 // with SelectionDAGCSEMap, but was isolated to provide use across the llvm code
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/Hashing.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/Host.h"
27 //===----------------------------------------------------------------------===//
28 // FoldingSetNodeIDRef Implementation
30 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
31 /// used to lookup the node in the FoldingSetImpl.
32 unsigned FoldingSetNodeIDRef::ComputeHash() const {
33 return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
36 bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
37 if (Size != RHS.Size) return false;
38 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
41 //===----------------------------------------------------------------------===//
42 // FoldingSetNodeID Implementation
44 /// Add* - Add various data types to Bit data.
46 void FoldingSetNodeID::AddPointer(const void *Ptr) {
47 // Note: this adds pointers to the hash using sizes and endianness that
48 // depend on the host. It doesn't matter however, because hashing on
49 // pointer values in inherently unstable. Nothing should depend on the
50 // ordering of nodes in the folding set.
51 Bits.append(reinterpret_cast<unsigned *>(&Ptr),
52 reinterpret_cast<unsigned *>(&Ptr+1));
54 void FoldingSetNodeID::AddInteger(signed I) {
57 void FoldingSetNodeID::AddInteger(unsigned I) {
60 void FoldingSetNodeID::AddInteger(long I) {
61 AddInteger((unsigned long)I);
63 void FoldingSetNodeID::AddInteger(unsigned long I) {
64 if (sizeof(long) == sizeof(int))
65 AddInteger(unsigned(I));
66 else if (sizeof(long) == sizeof(long long)) {
67 AddInteger((unsigned long long)I);
69 llvm_unreachable("unexpected sizeof(long)");
72 void FoldingSetNodeID::AddInteger(long long I) {
73 AddInteger((unsigned long long)I);
75 void FoldingSetNodeID::AddInteger(unsigned long long I) {
76 AddInteger(unsigned(I));
77 if ((uint64_t)(unsigned)I != I)
78 Bits.push_back(unsigned(I >> 32));
81 void FoldingSetNodeID::AddString(StringRef String) {
82 unsigned Size = String.size();
86 unsigned Units = Size / 4;
88 const unsigned *Base = (const unsigned*) String.data();
90 // If the string is aligned do a bulk transfer.
91 if (!((intptr_t)Base & 3)) {
92 Bits.append(Base, Base + Units);
93 Pos = (Units + 1) * 4;
95 // Otherwise do it the hard way.
96 // To be compatible with above bulk transfer, we need to take endianness
98 if (sys::isBigEndianHost()) {
99 for (Pos += 4; Pos <= Size; Pos += 4) {
100 unsigned V = ((unsigned char)String[Pos - 4] << 24) |
101 ((unsigned char)String[Pos - 3] << 16) |
102 ((unsigned char)String[Pos - 2] << 8) |
103 (unsigned char)String[Pos - 1];
107 assert(sys::isLittleEndianHost() && "Unexpected host endianness");
108 for (Pos += 4; Pos <= Size; Pos += 4) {
109 unsigned V = ((unsigned char)String[Pos - 1] << 24) |
110 ((unsigned char)String[Pos - 2] << 16) |
111 ((unsigned char)String[Pos - 3] << 8) |
112 (unsigned char)String[Pos - 4];
118 // With the leftover bits.
120 // Pos will have overshot size by 4 - #bytes left over.
121 // No need to take endianness into account here - this is always executed.
122 switch (Pos - Size) {
123 case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
124 case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
125 case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
126 default: return; // Nothing left.
132 // AddNodeID - Adds the Bit data of another ID to *this.
133 void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) {
134 Bits.append(ID.Bits.begin(), ID.Bits.end());
137 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
138 /// lookup the node in the FoldingSetImpl.
139 unsigned FoldingSetNodeID::ComputeHash() const {
140 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
143 /// operator== - Used to compare two nodes to each other.
145 bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS)const{
146 return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
149 /// operator== - Used to compare two nodes to each other.
151 bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
152 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
155 /// Intern - Copy this node's data to a memory region allocated from the
156 /// given allocator and return a FoldingSetNodeIDRef describing the
159 FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
160 unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
161 std::uninitialized_copy(Bits.begin(), Bits.end(), New);
162 return FoldingSetNodeIDRef(New, Bits.size());
165 //===----------------------------------------------------------------------===//
166 /// Helper functions for FoldingSetImpl.
168 /// GetNextPtr - In order to save space, each bucket is a
169 /// singly-linked-list. In order to make deletion more efficient, we make
170 /// the list circular, so we can delete a node without computing its hash.
171 /// The problem with this is that the start of the hash buckets are not
172 /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
173 /// use GetBucketPtr when this happens.
174 static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
175 // The low bit is set if this is the pointer back to the bucket.
176 if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
179 return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
184 static void **GetBucketPtr(void *NextInBucketPtr) {
185 intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
186 assert((Ptr & 1) && "Not a bucket pointer");
187 return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
190 /// GetBucketFor - Hash the specified node ID and return the hash bucket for
191 /// the specified ID.
192 static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
193 // NumBuckets is always a power of 2.
194 unsigned BucketNum = Hash & (NumBuckets-1);
195 return Buckets + BucketNum;
198 /// AllocateBuckets - Allocated initialized bucket memory.
199 static void **AllocateBuckets(unsigned NumBuckets) {
200 void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
201 // Set the very last bucket to be a non-null "pointer".
202 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
206 //===----------------------------------------------------------------------===//
207 // FoldingSetImpl Implementation
209 FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
210 assert(5 < Log2InitSize && Log2InitSize < 32 &&
211 "Initial hash table size out of range");
212 NumBuckets = 1 << Log2InitSize;
213 Buckets = AllocateBuckets(NumBuckets);
216 FoldingSetImpl::~FoldingSetImpl() {
219 void FoldingSetImpl::clear() {
220 // Set all but the last bucket to null pointers.
221 memset(Buckets, 0, NumBuckets*sizeof(void*));
223 // Set the very last bucket to be a non-null "pointer".
224 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
226 // Reset the node count to zero.
230 /// GrowHashTable - Double the size of the hash table and rehash everything.
232 void FoldingSetImpl::GrowHashTable() {
233 void **OldBuckets = Buckets;
234 unsigned OldNumBuckets = NumBuckets;
237 // Clear out new buckets.
238 Buckets = AllocateBuckets(NumBuckets);
241 // Walk the old buckets, rehashing nodes into their new place.
242 FoldingSetNodeID TempID;
243 for (unsigned i = 0; i != OldNumBuckets; ++i) {
244 void *Probe = OldBuckets[i];
245 if (!Probe) continue;
246 while (Node *NodeInBucket = GetNextPtr(Probe)) {
247 // Figure out the next link, remove NodeInBucket from the old link.
248 Probe = NodeInBucket->getNextInBucket();
249 NodeInBucket->SetNextInBucket(0);
251 // Insert the node into the new bucket, after recomputing the hash.
252 InsertNode(NodeInBucket,
253 GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
254 Buckets, NumBuckets));
262 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
263 /// return it. If not, return the insertion token that will make insertion
266 *FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
269 void **Bucket = GetBucketFor(ID.ComputeHash(), Buckets, NumBuckets);
270 void *Probe = *Bucket;
274 FoldingSetNodeID TempID;
275 while (Node *NodeInBucket = GetNextPtr(Probe)) {
276 if (NodeEquals(NodeInBucket, ID, TempID))
280 Probe = NodeInBucket->getNextInBucket();
283 // Didn't find the node, return null with the bucket as the InsertPos.
288 /// InsertNode - Insert the specified node into the folding set, knowing that it
289 /// is not already in the map. InsertPos must be obtained from
290 /// FindNodeOrInsertPos.
291 void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
292 assert(N->getNextInBucket() == 0);
293 // Do we need to grow the hashtable?
294 if (NumNodes+1 > NumBuckets*2) {
296 FoldingSetNodeID TempID;
297 InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
302 /// The insert position is actually a bucket pointer.
303 void **Bucket = static_cast<void**>(InsertPos);
305 void *Next = *Bucket;
307 // If this is the first insertion into this bucket, its next pointer will be
308 // null. Pretend as if it pointed to itself, setting the low bit to indicate
309 // that it is a pointer to the bucket.
311 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
313 // Set the node's next pointer, and make the bucket point to the node.
314 N->SetNextInBucket(Next);
318 /// RemoveNode - Remove a node from the folding set, returning true if one was
319 /// removed or false if the node was not in the folding set.
320 bool FoldingSetImpl::RemoveNode(Node *N) {
321 // Because each bucket is a circular list, we don't need to compute N's hash
323 void *Ptr = N->getNextInBucket();
324 if (Ptr == 0) return false; // Not in folding set.
327 N->SetNextInBucket(0);
329 // Remember what N originally pointed to, either a bucket or another node.
330 void *NodeNextPtr = Ptr;
332 // Chase around the list until we find the node (or bucket) which points to N.
334 if (Node *NodeInBucket = GetNextPtr(Ptr)) {
336 Ptr = NodeInBucket->getNextInBucket();
338 // We found a node that points to N, change it to point to N's next node,
339 // removing N from the list.
341 NodeInBucket->SetNextInBucket(NodeNextPtr);
345 void **Bucket = GetBucketPtr(Ptr);
348 // If we found that the bucket points to N, update the bucket to point to
351 *Bucket = NodeNextPtr;
358 /// GetOrInsertNode - If there is an existing simple Node exactly
359 /// equal to the specified node, return it. Otherwise, insert 'N' and it
361 FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
363 GetNodeProfile(N, ID);
365 if (Node *E = FindNodeOrInsertPos(ID, IP))
371 //===----------------------------------------------------------------------===//
372 // FoldingSetIteratorImpl Implementation
374 FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
375 // Skip to the first non-null non-self-cycle bucket.
376 while (*Bucket != reinterpret_cast<void*>(-1) &&
377 (*Bucket == 0 || GetNextPtr(*Bucket) == 0))
380 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
383 void FoldingSetIteratorImpl::advance() {
384 // If there is another link within this bucket, go to it.
385 void *Probe = NodePtr->getNextInBucket();
387 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
388 NodePtr = NextNodeInBucket;
390 // Otherwise, this is the last link in this bucket.
391 void **Bucket = GetBucketPtr(Probe);
393 // Skip to the next non-null non-self-cycle bucket.
396 } while (*Bucket != reinterpret_cast<void*>(-1) &&
397 (*Bucket == 0 || GetNextPtr(*Bucket) == 0));
399 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
403 //===----------------------------------------------------------------------===//
404 // FoldingSetBucketIteratorImpl Implementation
406 FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
407 Ptr = (*Bucket == 0 || GetNextPtr(*Bucket) == 0) ? (void*) Bucket : *Bucket;