blob: 29b59522088745e5e0254cad0446dd72aab37fbd [file] [log] [blame]
Jim Laskey0e5af192006-10-27 16:16:16 +00001//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey0e5af192006-10-27 16:16:16 +00007//
8//===----------------------------------------------------------------------===//
9//
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
13// set.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/ADT/FoldingSet.h"
Dan Gohmanc93b4cf2010-03-18 16:16:38 +000018#include "llvm/Support/Allocator.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000019#include "llvm/Support/ErrorHandling.h"
Bill Wendling160db5d2006-10-27 18:47:29 +000020#include "llvm/Support/MathExtras.h"
Rafael Espindola39c6d3a2006-11-03 01:38:14 +000021#include <cassert>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000022#include <cstring>
Jim Laskey0e5af192006-10-27 16:16:16 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
Dan Gohman30634102010-08-16 15:30:39 +000026// FoldingSetNodeIDRef Implementation
27
28/// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
29/// used to lookup the node in the FoldingSetImpl.
30unsigned FoldingSetNodeIDRef::ComputeHash() const {
31 // This is adapted from SuperFastHash by Paul Hsieh.
32 unsigned Hash = static_cast<unsigned>(Size);
33 for (const unsigned *BP = Data, *E = BP+Size; BP != E; ++BP) {
34 unsigned Data = *BP;
35 Hash += Data & 0xFFFF;
36 unsigned Tmp = ((Data >> 16) << 11) ^ Hash;
37 Hash = (Hash << 16) ^ Tmp;
38 Hash += Hash >> 11;
39 }
40
41 // Force "avalanching" of final 127 bits.
42 Hash ^= Hash << 3;
43 Hash += Hash >> 5;
44 Hash ^= Hash << 4;
45 Hash += Hash >> 17;
46 Hash ^= Hash << 25;
47 Hash += Hash >> 6;
48 return Hash;
49}
50
51bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
52 if (Size != RHS.Size) return false;
53 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
54}
55
56//===----------------------------------------------------------------------===//
Ted Kremenek0a3feca2008-01-19 04:22:50 +000057// FoldingSetNodeID Implementation
Jim Laskey0e5af192006-10-27 16:16:16 +000058
59/// Add* - Add various data types to Bit data.
60///
Ted Kremenek0a3feca2008-01-19 04:22:50 +000061void FoldingSetNodeID::AddPointer(const void *Ptr) {
Jim Laskey0e5af192006-10-27 16:16:16 +000062 // Note: this adds pointers to the hash using sizes and endianness that
63 // depend on the host. It doesn't matter however, because hashing on
64 // pointer values in inherently unstable. Nothing should depend on the
65 // ordering of nodes in the folding set.
66 intptr_t PtrI = (intptr_t)Ptr;
67 Bits.push_back(unsigned(PtrI));
68 if (sizeof(intptr_t) > sizeof(unsigned))
69 Bits.push_back(unsigned(uint64_t(PtrI) >> 32));
70}
Ted Kremenek0a3feca2008-01-19 04:22:50 +000071void FoldingSetNodeID::AddInteger(signed I) {
Jim Laskey0e5af192006-10-27 16:16:16 +000072 Bits.push_back(I);
73}
Ted Kremenek0a3feca2008-01-19 04:22:50 +000074void FoldingSetNodeID::AddInteger(unsigned I) {
Jim Laskey0e5af192006-10-27 16:16:16 +000075 Bits.push_back(I);
76}
Dan Gohman20cd13f2008-11-03 19:40:18 +000077void FoldingSetNodeID::AddInteger(long I) {
78 AddInteger((unsigned long)I);
Dan Gohmanf82e1e62007-09-14 20:48:42 +000079}
Dan Gohman20cd13f2008-11-03 19:40:18 +000080void FoldingSetNodeID::AddInteger(unsigned long I) {
81 if (sizeof(long) == sizeof(int))
82 AddInteger(unsigned(I));
83 else if (sizeof(long) == sizeof(long long)) {
84 AddInteger((unsigned long long)I);
85 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +000086 llvm_unreachable("unexpected sizeof(long)");
Dan Gohman20cd13f2008-11-03 19:40:18 +000087 }
88}
89void FoldingSetNodeID::AddInteger(long long I) {
90 AddInteger((unsigned long long)I);
91}
92void FoldingSetNodeID::AddInteger(unsigned long long I) {
93 AddInteger(unsigned(I));
Chris Lattnere4116f82007-02-04 01:48:10 +000094 if ((uint64_t)(int)I != I)
95 Bits.push_back(unsigned(I >> 32));
Jim Laskey0e5af192006-10-27 16:16:16 +000096}
Owen Anderson72e61b82008-07-01 23:49:59 +000097
Daniel Dunbar27dba672009-09-22 03:34:53 +000098void FoldingSetNodeID::AddString(StringRef String) {
99 unsigned Size = String.size();
Owen Anderson72e61b82008-07-01 23:49:59 +0000100 Bits.push_back(Size);
101 if (!Size) return;
102
103 unsigned Units = Size / 4;
104 unsigned Pos = 0;
Daniel Dunbar27dba672009-09-22 03:34:53 +0000105 const unsigned *Base = (const unsigned*) String.data();
Owen Anderson72e61b82008-07-01 23:49:59 +0000106
107 // If the string is aligned do a bulk transfer.
108 if (!((intptr_t)Base & 3)) {
109 Bits.append(Base, Base + Units);
110 Pos = (Units + 1) * 4;
111 } else {
112 // Otherwise do it the hard way.
Nick Lewyckyf9968312009-02-07 04:57:08 +0000113 for (Pos += 4; Pos <= Size; Pos += 4) {
Owen Anderson72e61b82008-07-01 23:49:59 +0000114 unsigned V = ((unsigned char)String[Pos - 4] << 24) |
115 ((unsigned char)String[Pos - 3] << 16) |
116 ((unsigned char)String[Pos - 2] << 8) |
117 (unsigned char)String[Pos - 1];
118 Bits.push_back(V);
119 }
120 }
121
122 // With the leftover bits.
123 unsigned V = 0;
124 // Pos will have overshot size by 4 - #bytes left over.
125 switch (Pos - Size) {
126 case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
127 case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
128 case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
129 default: return; // Nothing left.
130 }
131
132 Bits.push_back(V);
133}
134
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000135/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
Jim Laskey0e5af192006-10-27 16:16:16 +0000136/// lookup the node in the FoldingSetImpl.
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000137unsigned FoldingSetNodeID::ComputeHash() const {
Dan Gohman365c53e2010-08-24 23:16:53 +0000138 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
Jim Laskey0e5af192006-10-27 16:16:16 +0000139}
140
141/// operator== - Used to compare two nodes to each other.
142///
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000143bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS)const{
Dan Gohman365c53e2010-08-24 23:16:53 +0000144 return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
Dan Gohman30634102010-08-16 15:30:39 +0000145}
146
147/// operator== - Used to compare two nodes to each other.
148///
149bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
Dan Gohman365c53e2010-08-24 23:16:53 +0000150 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
Jim Laskey0e5af192006-10-27 16:16:16 +0000151}
152
Dan Gohmanc93b4cf2010-03-18 16:16:38 +0000153/// Intern - Copy this node's data to a memory region allocated from the
154/// given allocator and return a FoldingSetNodeIDRef describing the
155/// interned data.
156FoldingSetNodeIDRef
157FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
158 unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
159 std::uninitialized_copy(Bits.begin(), Bits.end(), New);
160 return FoldingSetNodeIDRef(New, Bits.size());
161}
Jim Laskey0e5af192006-10-27 16:16:16 +0000162
163//===----------------------------------------------------------------------===//
Jim Laskey18529f32006-10-27 18:05:12 +0000164/// Helper functions for FoldingSetImpl.
165
166/// GetNextPtr - In order to save space, each bucket is a
167/// singly-linked-list. In order to make deletion more efficient, we make
168/// the list circular, so we can delete a node without computing its hash.
169/// The problem with this is that the start of the hash buckets are not
Chris Lattner3cab0712007-01-30 23:16:22 +0000170/// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
171/// use GetBucketPtr when this happens.
Chris Lattner9a7288b2007-10-03 20:45:43 +0000172static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
173 // The low bit is set if this is the pointer back to the bucket.
174 if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
Jim Laskey18529f32006-10-27 18:05:12 +0000175 return 0;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000176
Jim Laskey18529f32006-10-27 18:05:12 +0000177 return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
178}
179
Ted Kremenek26e3c442008-02-04 21:11:17 +0000180
Jim Laskey18529f32006-10-27 18:05:12 +0000181/// testing.
182static void **GetBucketPtr(void *NextInBucketPtr) {
Chris Lattner9a7288b2007-10-03 20:45:43 +0000183 intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
Chris Lattner116c3212007-10-03 21:12:09 +0000184 assert((Ptr & 1) && "Not a bucket pointer");
Chris Lattner9a7288b2007-10-03 20:45:43 +0000185 return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
Jim Laskey18529f32006-10-27 18:05:12 +0000186}
187
188/// GetBucketFor - Hash the specified node ID and return the hash bucket for
189/// the specified ID.
Dan Gohman30634102010-08-16 15:30:39 +0000190static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
Jim Laskey18529f32006-10-27 18:05:12 +0000191 // NumBuckets is always a power of 2.
Dan Gohman30634102010-08-16 15:30:39 +0000192 unsigned BucketNum = Hash & (NumBuckets-1);
Jim Laskey18529f32006-10-27 18:05:12 +0000193 return Buckets + BucketNum;
194}
195
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000196/// AllocateBuckets - Allocated initialized bucket memory.
197static void **AllocateBuckets(unsigned NumBuckets) {
198 void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
199 // Set the very last bucket to be a non-null "pointer".
200 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
201 return Buckets;
202}
203
Jim Laskey18529f32006-10-27 18:05:12 +0000204//===----------------------------------------------------------------------===//
Jim Laskey0e5af192006-10-27 16:16:16 +0000205// FoldingSetImpl Implementation
206
Dan Gohman535de1a2008-08-23 00:42:16 +0000207FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
Jim Laskey1f67a992006-11-02 14:21:26 +0000208 assert(5 < Log2InitSize && Log2InitSize < 32 &&
209 "Initial hash table size out of range");
210 NumBuckets = 1 << Log2InitSize;
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000211 Buckets = AllocateBuckets(NumBuckets);
212 NumNodes = 0;
Jim Laskey0e5af192006-10-27 16:16:16 +0000213}
214FoldingSetImpl::~FoldingSetImpl() {
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000215 free(Buckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000216}
Dan Gohman535de1a2008-08-23 00:42:16 +0000217void FoldingSetImpl::clear() {
218 // Set all but the last bucket to null pointers.
219 memset(Buckets, 0, NumBuckets*sizeof(void*));
220
221 // Set the very last bucket to be a non-null "pointer".
222 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
223
224 // Reset the node count to zero.
225 NumNodes = 0;
226}
Jim Laskey0e5af192006-10-27 16:16:16 +0000227
Jim Laskey0e5af192006-10-27 16:16:16 +0000228/// GrowHashTable - Double the size of the hash table and rehash everything.
229///
230void FoldingSetImpl::GrowHashTable() {
231 void **OldBuckets = Buckets;
232 unsigned OldNumBuckets = NumBuckets;
233 NumBuckets <<= 1;
234
Jim Laskey0e5af192006-10-27 16:16:16 +0000235 // Clear out new buckets.
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000236 Buckets = AllocateBuckets(NumBuckets);
237 NumNodes = 0;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000238
Jim Laskey0e5af192006-10-27 16:16:16 +0000239 // Walk the old buckets, rehashing nodes into their new place.
Dan Gohman30634102010-08-16 15:30:39 +0000240 FoldingSetNodeID TempID;
Jim Laskey0e5af192006-10-27 16:16:16 +0000241 for (unsigned i = 0; i != OldNumBuckets; ++i) {
242 void *Probe = OldBuckets[i];
243 if (!Probe) continue;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000244 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000245 // Figure out the next link, remove NodeInBucket from the old link.
246 Probe = NodeInBucket->getNextInBucket();
247 NodeInBucket->SetNextInBucket(0);
248
249 // Insert the node into the new bucket, after recomputing the hash.
Dan Gohman30634102010-08-16 15:30:39 +0000250 InsertNode(NodeInBucket,
251 GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
252 Buckets, NumBuckets));
253 TempID.clear();
Jim Laskey0e5af192006-10-27 16:16:16 +0000254 }
255 }
256
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000257 free(OldBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000258}
259
260/// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
261/// return it. If not, return the insertion token that will make insertion
262/// faster.
Ted Kremenek27a8e0d2008-02-04 17:14:20 +0000263FoldingSetImpl::Node
264*FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
265 void *&InsertPos) {
266
Dan Gohman30634102010-08-16 15:30:39 +0000267 void **Bucket = GetBucketFor(ID.ComputeHash(), Buckets, NumBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000268 void *Probe = *Bucket;
269
270 InsertPos = 0;
271
Dan Gohman30634102010-08-16 15:30:39 +0000272 FoldingSetNodeID TempID;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000273 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Dan Gohman30634102010-08-16 15:30:39 +0000274 if (NodeEquals(NodeInBucket, ID, TempID))
Jim Laskey0e5af192006-10-27 16:16:16 +0000275 return NodeInBucket;
Dan Gohman30634102010-08-16 15:30:39 +0000276 TempID.clear();
Jim Laskey0e5af192006-10-27 16:16:16 +0000277
278 Probe = NodeInBucket->getNextInBucket();
279 }
280
281 // Didn't find the node, return null with the bucket as the InsertPos.
282 InsertPos = Bucket;
283 return 0;
284}
285
286/// InsertNode - Insert the specified node into the folding set, knowing that it
287/// is not already in the map. InsertPos must be obtained from
288/// FindNodeOrInsertPos.
289void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
Chris Lattner0de44392007-02-01 05:33:21 +0000290 assert(N->getNextInBucket() == 0);
Jim Laskey0e5af192006-10-27 16:16:16 +0000291 // Do we need to grow the hashtable?
Chris Lattnerb85210f2007-01-31 06:04:41 +0000292 if (NumNodes+1 > NumBuckets*2) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000293 GrowHashTable();
Dan Gohman30634102010-08-16 15:30:39 +0000294 FoldingSetNodeID TempID;
295 InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000296 }
Chris Lattnerb85210f2007-01-31 06:04:41 +0000297
298 ++NumNodes;
Jim Laskey0e5af192006-10-27 16:16:16 +0000299
300 /// The insert position is actually a bucket pointer.
301 void **Bucket = static_cast<void**>(InsertPos);
302
303 void *Next = *Bucket;
304
305 // If this is the first insertion into this bucket, its next pointer will be
Chris Lattner9a7288b2007-10-03 20:45:43 +0000306 // null. Pretend as if it pointed to itself, setting the low bit to indicate
307 // that it is a pointer to the bucket.
Jim Laskey0e5af192006-10-27 16:16:16 +0000308 if (Next == 0)
Chris Lattner9a7288b2007-10-03 20:45:43 +0000309 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
Jim Laskey0e5af192006-10-27 16:16:16 +0000310
Chris Lattnerb85210f2007-01-31 06:04:41 +0000311 // Set the node's next pointer, and make the bucket point to the node.
Jim Laskey0e5af192006-10-27 16:16:16 +0000312 N->SetNextInBucket(Next);
313 *Bucket = N;
314}
315
316/// RemoveNode - Remove a node from the folding set, returning true if one was
317/// removed or false if the node was not in the folding set.
318bool FoldingSetImpl::RemoveNode(Node *N) {
319 // Because each bucket is a circular list, we don't need to compute N's hash
Chris Lattner0de44392007-02-01 05:33:21 +0000320 // to remove it.
Jim Laskey0e5af192006-10-27 16:16:16 +0000321 void *Ptr = N->getNextInBucket();
322 if (Ptr == 0) return false; // Not in folding set.
323
324 --NumNodes;
Jim Laskey0e5af192006-10-27 16:16:16 +0000325 N->SetNextInBucket(0);
Chris Lattner0de44392007-02-01 05:33:21 +0000326
327 // Remember what N originally pointed to, either a bucket or another node.
328 void *NodeNextPtr = Ptr;
329
330 // Chase around the list until we find the node (or bucket) which points to N.
Jim Laskey0e5af192006-10-27 16:16:16 +0000331 while (true) {
Chris Lattner9a7288b2007-10-03 20:45:43 +0000332 if (Node *NodeInBucket = GetNextPtr(Ptr)) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000333 // Advance pointer.
334 Ptr = NodeInBucket->getNextInBucket();
335
336 // We found a node that points to N, change it to point to N's next node,
337 // removing N from the list.
338 if (Ptr == N) {
339 NodeInBucket->SetNextInBucket(NodeNextPtr);
340 return true;
341 }
342 } else {
343 void **Bucket = GetBucketPtr(Ptr);
344 Ptr = *Bucket;
345
346 // If we found that the bucket points to N, update the bucket to point to
347 // whatever is next.
348 if (Ptr == N) {
349 *Bucket = NodeNextPtr;
350 return true;
351 }
352 }
353 }
354}
355
356/// GetOrInsertNode - If there is an existing simple Node exactly
357/// equal to the specified node, return it. Otherwise, insert 'N' and it
358/// instead.
359FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000360 FoldingSetNodeID ID;
Dan Gohman6616f7e2010-08-16 14:53:42 +0000361 GetNodeProfile(N, ID);
Jim Laskey0e5af192006-10-27 16:16:16 +0000362 void *IP;
363 if (Node *E = FindNodeOrInsertPos(ID, IP))
364 return E;
365 InsertNode(N, IP);
366 return N;
367}
Chris Lattner116c3212007-10-03 21:12:09 +0000368
369//===----------------------------------------------------------------------===//
370// FoldingSetIteratorImpl Implementation
371
372FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
373 // Skip to the first non-null non-self-cycle bucket.
Ted Kremeneke3e09572008-02-15 21:12:46 +0000374 while (*Bucket != reinterpret_cast<void*>(-1) &&
375 (*Bucket == 0 || GetNextPtr(*Bucket) == 0))
Chris Lattner116c3212007-10-03 21:12:09 +0000376 ++Bucket;
377
378 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
379}
380
381void FoldingSetIteratorImpl::advance() {
382 // If there is another link within this bucket, go to it.
383 void *Probe = NodePtr->getNextInBucket();
384
385 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
386 NodePtr = NextNodeInBucket;
387 else {
388 // Otherwise, this is the last link in this bucket.
389 void **Bucket = GetBucketPtr(Probe);
390
391 // Skip to the next non-null non-self-cycle bucket.
392 do {
393 ++Bucket;
Ted Kremeneke3e09572008-02-15 21:12:46 +0000394 } while (*Bucket != reinterpret_cast<void*>(-1) &&
395 (*Bucket == 0 || GetNextPtr(*Bucket) == 0));
Chris Lattner116c3212007-10-03 21:12:09 +0000396
397 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
398 }
399}
400
Ted Kremenek26e3c442008-02-04 21:11:17 +0000401//===----------------------------------------------------------------------===//
402// FoldingSetBucketIteratorImpl Implementation
403
404FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
405 Ptr = (*Bucket == 0 || GetNextPtr(*Bucket) == 0) ? (void*) Bucket : *Bucket;
406}