blob: d182d48035463d4ecaaae91e1cdaa63ce407fc93 [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"
Chandler Carruthabe24cf2012-03-01 23:18:44 +000018#include "llvm/ADT/Hashing.h"
Dan Gohmanc93b4cf2010-03-18 16:16:38 +000019#include "llvm/Support/Allocator.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Bill Wendling160db5d2006-10-27 18:47:29 +000021#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000022#include "llvm/Support/Host.h"
Rafael Espindola39c6d3a2006-11-03 01:38:14 +000023#include <cassert>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000024#include <cstring>
Jim Laskey0e5af192006-10-27 16:16:16 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
Dan Gohman30634102010-08-16 15:30:39 +000028// FoldingSetNodeIDRef Implementation
29
30/// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
31/// used to lookup the node in the FoldingSetImpl.
32unsigned FoldingSetNodeIDRef::ComputeHash() const {
Chandler Carruthabe24cf2012-03-01 23:18:44 +000033 return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
Dan Gohman30634102010-08-16 15:30:39 +000034}
35
36bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
37 if (Size != RHS.Size) return false;
38 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
39}
40
41//===----------------------------------------------------------------------===//
Ted Kremenek0a3feca2008-01-19 04:22:50 +000042// FoldingSetNodeID Implementation
Jim Laskey0e5af192006-10-27 16:16:16 +000043
Daniel Dunbar27dba672009-09-22 03:34:53 +000044void FoldingSetNodeID::AddString(StringRef String) {
45 unsigned Size = String.size();
Owen Anderson72e61b82008-07-01 23:49:59 +000046 Bits.push_back(Size);
47 if (!Size) return;
48
49 unsigned Units = Size / 4;
50 unsigned Pos = 0;
Daniel Dunbar27dba672009-09-22 03:34:53 +000051 const unsigned *Base = (const unsigned*) String.data();
Owen Anderson72e61b82008-07-01 23:49:59 +000052
53 // If the string is aligned do a bulk transfer.
54 if (!((intptr_t)Base & 3)) {
55 Bits.append(Base, Base + Units);
56 Pos = (Units + 1) * 4;
57 } else {
58 // Otherwise do it the hard way.
Dale Johannesenc81c7fe2010-11-19 00:48:58 +000059 // To be compatible with above bulk transfer, we need to take endianness
60 // into account.
61 if (sys::isBigEndianHost()) {
62 for (Pos += 4; Pos <= Size; Pos += 4) {
63 unsigned V = ((unsigned char)String[Pos - 4] << 24) |
64 ((unsigned char)String[Pos - 3] << 16) |
65 ((unsigned char)String[Pos - 2] << 8) |
66 (unsigned char)String[Pos - 1];
67 Bits.push_back(V);
68 }
69 } else {
70 assert(sys::isLittleEndianHost() && "Unexpected host endianness");
71 for (Pos += 4; Pos <= Size; Pos += 4) {
72 unsigned V = ((unsigned char)String[Pos - 1] << 24) |
73 ((unsigned char)String[Pos - 2] << 16) |
74 ((unsigned char)String[Pos - 3] << 8) |
75 (unsigned char)String[Pos - 4];
76 Bits.push_back(V);
77 }
Owen Anderson72e61b82008-07-01 23:49:59 +000078 }
79 }
80
81 // With the leftover bits.
82 unsigned V = 0;
Dale Johannesenc81c7fe2010-11-19 00:48:58 +000083 // Pos will have overshot size by 4 - #bytes left over.
84 // No need to take endianness into account here - this is always executed.
Owen Anderson72e61b82008-07-01 23:49:59 +000085 switch (Pos - Size) {
86 case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
87 case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
88 case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
89 default: return; // Nothing left.
90 }
91
92 Bits.push_back(V);
93}
94
Daniel Dunbar9eddc1c2012-03-08 07:42:18 +000095/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
Jim Laskey0e5af192006-10-27 16:16:16 +000096/// lookup the node in the FoldingSetImpl.
Ted Kremenek0a3feca2008-01-19 04:22:50 +000097unsigned FoldingSetNodeID::ComputeHash() const {
Dan Gohman365c53e2010-08-24 23:16:53 +000098 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
Jim Laskey0e5af192006-10-27 16:16:16 +000099}
100
101/// operator== - Used to compare two nodes to each other.
102///
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000103bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS)const{
Dan Gohman365c53e2010-08-24 23:16:53 +0000104 return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
Dan Gohman30634102010-08-16 15:30:39 +0000105}
106
107/// operator== - Used to compare two nodes to each other.
108///
109bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
Dan Gohman365c53e2010-08-24 23:16:53 +0000110 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
Jim Laskey0e5af192006-10-27 16:16:16 +0000111}
112
Dan Gohmanc93b4cf2010-03-18 16:16:38 +0000113/// Intern - Copy this node's data to a memory region allocated from the
114/// given allocator and return a FoldingSetNodeIDRef describing the
115/// interned data.
116FoldingSetNodeIDRef
117FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
118 unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
119 std::uninitialized_copy(Bits.begin(), Bits.end(), New);
120 return FoldingSetNodeIDRef(New, Bits.size());
121}
Jim Laskey0e5af192006-10-27 16:16:16 +0000122
123//===----------------------------------------------------------------------===//
Jim Laskey18529f32006-10-27 18:05:12 +0000124/// Helper functions for FoldingSetImpl.
125
126/// GetNextPtr - In order to save space, each bucket is a
127/// singly-linked-list. In order to make deletion more efficient, we make
128/// the list circular, so we can delete a node without computing its hash.
129/// The problem with this is that the start of the hash buckets are not
Chris Lattner3cab0712007-01-30 23:16:22 +0000130/// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
131/// use GetBucketPtr when this happens.
Chris Lattner9a7288b2007-10-03 20:45:43 +0000132static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
133 // The low bit is set if this is the pointer back to the bucket.
134 if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
Jim Laskey18529f32006-10-27 18:05:12 +0000135 return 0;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000136
Jim Laskey18529f32006-10-27 18:05:12 +0000137 return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
138}
139
Ted Kremenek26e3c442008-02-04 21:11:17 +0000140
Jim Laskey18529f32006-10-27 18:05:12 +0000141/// testing.
142static void **GetBucketPtr(void *NextInBucketPtr) {
Chris Lattner9a7288b2007-10-03 20:45:43 +0000143 intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
Chris Lattner116c3212007-10-03 21:12:09 +0000144 assert((Ptr & 1) && "Not a bucket pointer");
Chris Lattner9a7288b2007-10-03 20:45:43 +0000145 return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
Jim Laskey18529f32006-10-27 18:05:12 +0000146}
147
148/// GetBucketFor - Hash the specified node ID and return the hash bucket for
149/// the specified ID.
Dan Gohman30634102010-08-16 15:30:39 +0000150static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
Jim Laskey18529f32006-10-27 18:05:12 +0000151 // NumBuckets is always a power of 2.
Dan Gohman30634102010-08-16 15:30:39 +0000152 unsigned BucketNum = Hash & (NumBuckets-1);
Jim Laskey18529f32006-10-27 18:05:12 +0000153 return Buckets + BucketNum;
154}
155
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000156/// AllocateBuckets - Allocated initialized bucket memory.
157static void **AllocateBuckets(unsigned NumBuckets) {
158 void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
159 // Set the very last bucket to be a non-null "pointer".
160 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
161 return Buckets;
162}
163
Jim Laskey18529f32006-10-27 18:05:12 +0000164//===----------------------------------------------------------------------===//
Jim Laskey0e5af192006-10-27 16:16:16 +0000165// FoldingSetImpl Implementation
166
Dan Gohman535de1a2008-08-23 00:42:16 +0000167FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
Jim Laskey1f67a992006-11-02 14:21:26 +0000168 assert(5 < Log2InitSize && Log2InitSize < 32 &&
169 "Initial hash table size out of range");
170 NumBuckets = 1 << Log2InitSize;
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000171 Buckets = AllocateBuckets(NumBuckets);
172 NumNodes = 0;
Jim Laskey0e5af192006-10-27 16:16:16 +0000173}
174FoldingSetImpl::~FoldingSetImpl() {
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000175 free(Buckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000176}
Dan Gohman535de1a2008-08-23 00:42:16 +0000177void FoldingSetImpl::clear() {
178 // Set all but the last bucket to null pointers.
179 memset(Buckets, 0, NumBuckets*sizeof(void*));
180
181 // Set the very last bucket to be a non-null "pointer".
182 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
183
184 // Reset the node count to zero.
185 NumNodes = 0;
186}
Jim Laskey0e5af192006-10-27 16:16:16 +0000187
Jim Laskey0e5af192006-10-27 16:16:16 +0000188/// GrowHashTable - Double the size of the hash table and rehash everything.
189///
190void FoldingSetImpl::GrowHashTable() {
191 void **OldBuckets = Buckets;
192 unsigned OldNumBuckets = NumBuckets;
193 NumBuckets <<= 1;
194
Jim Laskey0e5af192006-10-27 16:16:16 +0000195 // Clear out new buckets.
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000196 Buckets = AllocateBuckets(NumBuckets);
197 NumNodes = 0;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000198
Jim Laskey0e5af192006-10-27 16:16:16 +0000199 // Walk the old buckets, rehashing nodes into their new place.
Dan Gohman30634102010-08-16 15:30:39 +0000200 FoldingSetNodeID TempID;
Jim Laskey0e5af192006-10-27 16:16:16 +0000201 for (unsigned i = 0; i != OldNumBuckets; ++i) {
202 void *Probe = OldBuckets[i];
203 if (!Probe) continue;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000204 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000205 // Figure out the next link, remove NodeInBucket from the old link.
206 Probe = NodeInBucket->getNextInBucket();
207 NodeInBucket->SetNextInBucket(0);
208
209 // Insert the node into the new bucket, after recomputing the hash.
Dan Gohman30634102010-08-16 15:30:39 +0000210 InsertNode(NodeInBucket,
211 GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
212 Buckets, NumBuckets));
213 TempID.clear();
Jim Laskey0e5af192006-10-27 16:16:16 +0000214 }
215 }
216
Benjamin Kramer6118efa2010-06-19 17:00:31 +0000217 free(OldBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000218}
219
220/// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
221/// return it. If not, return the insertion token that will make insertion
222/// faster.
Ted Kremenek27a8e0d2008-02-04 17:14:20 +0000223FoldingSetImpl::Node
224*FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
225 void *&InsertPos) {
226
Dan Gohman30634102010-08-16 15:30:39 +0000227 void **Bucket = GetBucketFor(ID.ComputeHash(), Buckets, NumBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000228 void *Probe = *Bucket;
229
230 InsertPos = 0;
231
Dan Gohman30634102010-08-16 15:30:39 +0000232 FoldingSetNodeID TempID;
Chris Lattner9a7288b2007-10-03 20:45:43 +0000233 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Dan Gohman30634102010-08-16 15:30:39 +0000234 if (NodeEquals(NodeInBucket, ID, TempID))
Jim Laskey0e5af192006-10-27 16:16:16 +0000235 return NodeInBucket;
Dan Gohman30634102010-08-16 15:30:39 +0000236 TempID.clear();
Jim Laskey0e5af192006-10-27 16:16:16 +0000237
238 Probe = NodeInBucket->getNextInBucket();
239 }
240
241 // Didn't find the node, return null with the bucket as the InsertPos.
242 InsertPos = Bucket;
243 return 0;
244}
245
246/// InsertNode - Insert the specified node into the folding set, knowing that it
247/// is not already in the map. InsertPos must be obtained from
248/// FindNodeOrInsertPos.
249void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
Chris Lattner0de44392007-02-01 05:33:21 +0000250 assert(N->getNextInBucket() == 0);
Jim Laskey0e5af192006-10-27 16:16:16 +0000251 // Do we need to grow the hashtable?
Chris Lattnerb85210f2007-01-31 06:04:41 +0000252 if (NumNodes+1 > NumBuckets*2) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000253 GrowHashTable();
Dan Gohman30634102010-08-16 15:30:39 +0000254 FoldingSetNodeID TempID;
255 InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
Jim Laskey0e5af192006-10-27 16:16:16 +0000256 }
Chris Lattnerb85210f2007-01-31 06:04:41 +0000257
258 ++NumNodes;
Jim Laskey0e5af192006-10-27 16:16:16 +0000259
260 /// The insert position is actually a bucket pointer.
261 void **Bucket = static_cast<void**>(InsertPos);
262
263 void *Next = *Bucket;
264
265 // If this is the first insertion into this bucket, its next pointer will be
Chris Lattner9a7288b2007-10-03 20:45:43 +0000266 // null. Pretend as if it pointed to itself, setting the low bit to indicate
267 // that it is a pointer to the bucket.
Jim Laskey0e5af192006-10-27 16:16:16 +0000268 if (Next == 0)
Chris Lattner9a7288b2007-10-03 20:45:43 +0000269 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
Jim Laskey0e5af192006-10-27 16:16:16 +0000270
Chris Lattnerb85210f2007-01-31 06:04:41 +0000271 // Set the node's next pointer, and make the bucket point to the node.
Jim Laskey0e5af192006-10-27 16:16:16 +0000272 N->SetNextInBucket(Next);
273 *Bucket = N;
274}
275
276/// RemoveNode - Remove a node from the folding set, returning true if one was
277/// removed or false if the node was not in the folding set.
278bool FoldingSetImpl::RemoveNode(Node *N) {
279 // Because each bucket is a circular list, we don't need to compute N's hash
Chris Lattner0de44392007-02-01 05:33:21 +0000280 // to remove it.
Jim Laskey0e5af192006-10-27 16:16:16 +0000281 void *Ptr = N->getNextInBucket();
282 if (Ptr == 0) return false; // Not in folding set.
283
284 --NumNodes;
Jim Laskey0e5af192006-10-27 16:16:16 +0000285 N->SetNextInBucket(0);
Chris Lattner0de44392007-02-01 05:33:21 +0000286
287 // Remember what N originally pointed to, either a bucket or another node.
288 void *NodeNextPtr = Ptr;
289
290 // Chase around the list until we find the node (or bucket) which points to N.
Jim Laskey0e5af192006-10-27 16:16:16 +0000291 while (true) {
Chris Lattner9a7288b2007-10-03 20:45:43 +0000292 if (Node *NodeInBucket = GetNextPtr(Ptr)) {
Jim Laskey0e5af192006-10-27 16:16:16 +0000293 // Advance pointer.
294 Ptr = NodeInBucket->getNextInBucket();
295
296 // We found a node that points to N, change it to point to N's next node,
297 // removing N from the list.
298 if (Ptr == N) {
299 NodeInBucket->SetNextInBucket(NodeNextPtr);
300 return true;
301 }
302 } else {
303 void **Bucket = GetBucketPtr(Ptr);
304 Ptr = *Bucket;
305
306 // If we found that the bucket points to N, update the bucket to point to
307 // whatever is next.
308 if (Ptr == N) {
309 *Bucket = NodeNextPtr;
310 return true;
311 }
312 }
313 }
314}
315
316/// GetOrInsertNode - If there is an existing simple Node exactly
317/// equal to the specified node, return it. Otherwise, insert 'N' and it
318/// instead.
319FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
Ted Kremenek0a3feca2008-01-19 04:22:50 +0000320 FoldingSetNodeID ID;
Dan Gohman6616f7e2010-08-16 14:53:42 +0000321 GetNodeProfile(N, ID);
Jim Laskey0e5af192006-10-27 16:16:16 +0000322 void *IP;
323 if (Node *E = FindNodeOrInsertPos(ID, IP))
324 return E;
325 InsertNode(N, IP);
326 return N;
327}
Chris Lattner116c3212007-10-03 21:12:09 +0000328
329//===----------------------------------------------------------------------===//
330// FoldingSetIteratorImpl Implementation
331
332FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
333 // Skip to the first non-null non-self-cycle bucket.
Ted Kremeneke3e09572008-02-15 21:12:46 +0000334 while (*Bucket != reinterpret_cast<void*>(-1) &&
335 (*Bucket == 0 || GetNextPtr(*Bucket) == 0))
Chris Lattner116c3212007-10-03 21:12:09 +0000336 ++Bucket;
337
338 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
339}
340
341void FoldingSetIteratorImpl::advance() {
342 // If there is another link within this bucket, go to it.
343 void *Probe = NodePtr->getNextInBucket();
344
345 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
346 NodePtr = NextNodeInBucket;
347 else {
348 // Otherwise, this is the last link in this bucket.
349 void **Bucket = GetBucketPtr(Probe);
350
351 // Skip to the next non-null non-self-cycle bucket.
352 do {
353 ++Bucket;
Ted Kremeneke3e09572008-02-15 21:12:46 +0000354 } while (*Bucket != reinterpret_cast<void*>(-1) &&
355 (*Bucket == 0 || GetNextPtr(*Bucket) == 0));
Chris Lattner116c3212007-10-03 21:12:09 +0000356
357 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
358 }
359}
360
Ted Kremenek26e3c442008-02-04 21:11:17 +0000361//===----------------------------------------------------------------------===//
362// FoldingSetBucketIteratorImpl Implementation
363
364FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
365 Ptr = (*Bucket == 0 || GetNextPtr(*Bucket) == 0) ? (void*) Bucket : *Bucket;
366}