blob: bb0ec2defef9f1c761974484cf8cae2b09301281 [file] [log] [blame]
Jim Laskey43bc1842006-10-27 16:16:16 +00001//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Laskey43bc1842006-10-27 16:16:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a hash set that can be used to remove duplication of
Chris Lattner54129452012-11-16 18:58:23 +000011// nodes in a graph.
Jim Laskey43bc1842006-10-27 16:16:16 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/FoldingSet.h"
Chandler Carruth93cffd22012-03-01 23:18:44 +000016#include "llvm/ADT/Hashing.h"
Dan Gohman6556c892010-03-18 16:16:38 +000017#include "llvm/Support/Allocator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/MathExtras.h"
Rafael Espindolaf16dbf62006-11-03 01:38:14 +000021#include <cassert>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000022#include <cstring>
Jim Laskey43bc1842006-10-27 16:16:16 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
Dan Gohman9c9ce532010-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 {
Chandler Carruth93cffd22012-03-01 23:18:44 +000031 return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
Dan Gohman9c9ce532010-08-16 15:30:39 +000032}
33
34bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
35 if (Size != RHS.Size) return false;
36 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
37}
38
Ted Kremenek0b3e8052012-09-08 04:25:29 +000039/// Used to compare the "ordering" of two nodes as defined by the
40/// profiled bits and their ordering defined by memcmp().
41bool FoldingSetNodeIDRef::operator<(FoldingSetNodeIDRef RHS) const {
42 if (Size != RHS.Size)
43 return Size < RHS.Size;
44 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) < 0;
45}
46
Dan Gohman9c9ce532010-08-16 15:30:39 +000047//===----------------------------------------------------------------------===//
Ted Kremenekc0259632008-01-19 04:22:50 +000048// FoldingSetNodeID Implementation
Jim Laskey43bc1842006-10-27 16:16:16 +000049
Duncan Sandsd8e918b2012-03-08 09:32:21 +000050/// Add* - Add various data types to Bit data.
51///
52void FoldingSetNodeID::AddPointer(const void *Ptr) {
53 // Note: this adds pointers to the hash using sizes and endianness that
Sanjay Patel16ab5e62015-04-06 16:21:12 +000054 // depend on the host. It doesn't matter, however, because hashing on
55 // pointer values is inherently unstable. Nothing should depend on the
Duncan Sandsd8e918b2012-03-08 09:32:21 +000056 // ordering of nodes in the folding set.
57 Bits.append(reinterpret_cast<unsigned *>(&Ptr),
58 reinterpret_cast<unsigned *>(&Ptr+1));
59}
60void FoldingSetNodeID::AddInteger(signed I) {
61 Bits.push_back(I);
62}
63void FoldingSetNodeID::AddInteger(unsigned I) {
64 Bits.push_back(I);
65}
66void FoldingSetNodeID::AddInteger(long I) {
67 AddInteger((unsigned long)I);
68}
69void FoldingSetNodeID::AddInteger(unsigned long I) {
70 if (sizeof(long) == sizeof(int))
71 AddInteger(unsigned(I));
72 else if (sizeof(long) == sizeof(long long)) {
73 AddInteger((unsigned long long)I);
74 } else {
75 llvm_unreachable("unexpected sizeof(long)");
76 }
77}
78void FoldingSetNodeID::AddInteger(long long I) {
79 AddInteger((unsigned long long)I);
80}
81void FoldingSetNodeID::AddInteger(unsigned long long I) {
82 AddInteger(unsigned(I));
83 if ((uint64_t)(unsigned)I != I)
84 Bits.push_back(unsigned(I >> 32));
85}
86
Daniel Dunbar78faee02009-09-22 03:34:53 +000087void FoldingSetNodeID::AddString(StringRef String) {
88 unsigned Size = String.size();
Owen Anderson31936d62008-07-01 23:49:59 +000089 Bits.push_back(Size);
90 if (!Size) return;
91
92 unsigned Units = Size / 4;
93 unsigned Pos = 0;
Daniel Dunbar78faee02009-09-22 03:34:53 +000094 const unsigned *Base = (const unsigned*) String.data();
Owen Anderson31936d62008-07-01 23:49:59 +000095
96 // If the string is aligned do a bulk transfer.
97 if (!((intptr_t)Base & 3)) {
98 Bits.append(Base, Base + Units);
99 Pos = (Units + 1) * 4;
100 } else {
101 // Otherwise do it the hard way.
Dale Johannesen461e7042010-11-19 00:48:58 +0000102 // To be compatible with above bulk transfer, we need to take endianness
103 // into account.
Gabor Horvathfee04342015-03-16 09:53:42 +0000104 static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost,
105 "Unexpected host endianness");
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000106 if (sys::IsBigEndianHost) {
Dale Johannesen461e7042010-11-19 00:48:58 +0000107 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 }
Gabor Horvathfee04342015-03-16 09:53:42 +0000114 } else { // Little-endian host
Dale Johannesen461e7042010-11-19 00:48:58 +0000115 for (Pos += 4; Pos <= Size; Pos += 4) {
116 unsigned V = ((unsigned char)String[Pos - 1] << 24) |
117 ((unsigned char)String[Pos - 2] << 16) |
118 ((unsigned char)String[Pos - 3] << 8) |
119 (unsigned char)String[Pos - 4];
120 Bits.push_back(V);
121 }
Owen Anderson31936d62008-07-01 23:49:59 +0000122 }
123 }
124
125 // With the leftover bits.
126 unsigned V = 0;
Dale Johannesen461e7042010-11-19 00:48:58 +0000127 // Pos will have overshot size by 4 - #bytes left over.
128 // No need to take endianness into account here - this is always executed.
Owen Anderson31936d62008-07-01 23:49:59 +0000129 switch (Pos - Size) {
130 case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
131 case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
132 case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
133 default: return; // Nothing left.
134 }
135
136 Bits.push_back(V);
137}
138
Duncan Sandsd8e918b2012-03-08 09:32:21 +0000139// AddNodeID - Adds the Bit data of another ID to *this.
140void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) {
141 Bits.append(ID.Bits.begin(), ID.Bits.end());
142}
143
144/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
Jim Laskey43bc1842006-10-27 16:16:16 +0000145/// lookup the node in the FoldingSetImpl.
Ted Kremenekc0259632008-01-19 04:22:50 +0000146unsigned FoldingSetNodeID::ComputeHash() const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000147 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
Jim Laskey43bc1842006-10-27 16:16:16 +0000148}
149
150/// operator== - Used to compare two nodes to each other.
151///
Nick Lewyckyd1925172012-12-25 06:13:25 +0000152bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000153 return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
Dan Gohman9c9ce532010-08-16 15:30:39 +0000154}
155
156/// operator== - Used to compare two nodes to each other.
157///
158bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000159 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
Jim Laskey43bc1842006-10-27 16:16:16 +0000160}
161
Ted Kremenek0b3e8052012-09-08 04:25:29 +0000162/// Used to compare the "ordering" of two nodes as defined by the
163/// profiled bits and their ordering defined by memcmp().
Nick Lewyckyd1925172012-12-25 06:13:25 +0000164bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const {
Ted Kremenek0b3e8052012-09-08 04:25:29 +0000165 return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
166}
167
168bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const {
169 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS;
170}
171
Dan Gohman6556c892010-03-18 16:16:38 +0000172/// 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.
175FoldingSetNodeIDRef
176FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
177 unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
178 std::uninitialized_copy(Bits.begin(), Bits.end(), New);
179 return FoldingSetNodeIDRef(New, Bits.size());
180}
Jim Laskey43bc1842006-10-27 16:16:16 +0000181
182//===----------------------------------------------------------------------===//
Jim Laskey6ca4a342006-10-27 18:05:12 +0000183/// Helper functions for FoldingSetImpl.
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
Chris Lattnera94523d2007-01-30 23:16:22 +0000189/// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
190/// use GetBucketPtr when this happens.
Chris Lattner8c41ed62007-10-03 20:45:43 +0000191static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
192 // The low bit is set if this is the pointer back to the bucket.
193 if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
Craig Topperc10719f2014-04-07 04:17:22 +0000194 return nullptr;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000195
Jim Laskey6ca4a342006-10-27 18:05:12 +0000196 return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
197}
198
Ted Kremeneke2887862008-02-04 21:11:17 +0000199
Jim Laskey6ca4a342006-10-27 18:05:12 +0000200/// testing.
201static void **GetBucketPtr(void *NextInBucketPtr) {
Chris Lattner8c41ed62007-10-03 20:45:43 +0000202 intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
Chris Lattner99f6ab72007-10-03 21:12:09 +0000203 assert((Ptr & 1) && "Not a bucket pointer");
Chris Lattner8c41ed62007-10-03 20:45:43 +0000204 return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
Jim Laskey6ca4a342006-10-27 18:05:12 +0000205}
206
207/// GetBucketFor - Hash the specified node ID and return the hash bucket for
208/// the specified ID.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000209static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
Jim Laskey6ca4a342006-10-27 18:05:12 +0000210 // NumBuckets is always a power of 2.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000211 unsigned BucketNum = Hash & (NumBuckets-1);
Jim Laskey6ca4a342006-10-27 18:05:12 +0000212 return Buckets + BucketNum;
213}
214
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000215/// AllocateBuckets - Allocated initialized bucket memory.
216static void **AllocateBuckets(unsigned NumBuckets) {
217 void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
218 // Set the very last bucket to be a non-null "pointer".
219 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
220 return Buckets;
221}
222
Jim Laskey6ca4a342006-10-27 18:05:12 +0000223//===----------------------------------------------------------------------===//
Jim Laskey43bc1842006-10-27 16:16:16 +0000224// FoldingSetImpl Implementation
225
Benjamin Kramer66f486f2015-03-22 18:22:33 +0000226void FoldingSetImpl::anchor() {}
227
Dan Gohman0e44e0d2008-08-23 00:42:16 +0000228FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
Jim Laskeyeb0fd252006-11-02 14:21:26 +0000229 assert(5 < Log2InitSize && Log2InitSize < 32 &&
230 "Initial hash table size out of range");
231 NumBuckets = 1 << Log2InitSize;
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000232 Buckets = AllocateBuckets(NumBuckets);
233 NumNodes = 0;
Jim Laskey43bc1842006-10-27 16:16:16 +0000234}
Chandler Carruthb596ba22015-08-16 23:17:27 +0000235
236FoldingSetImpl::FoldingSetImpl(FoldingSetImpl &&Arg)
237 : Buckets(Arg.Buckets), NumBuckets(Arg.NumBuckets), NumNodes(Arg.NumNodes) {
238 Arg.Buckets = nullptr;
239 Arg.NumBuckets = 0;
240 Arg.NumNodes = 0;
241}
242
243FoldingSetImpl &FoldingSetImpl::operator=(FoldingSetImpl &&RHS) {
244 free(Buckets); // This may be null if the set is in a moved-from state.
245 Buckets = RHS.Buckets;
246 NumBuckets = RHS.NumBuckets;
247 NumNodes = RHS.NumNodes;
248 RHS.Buckets = nullptr;
249 RHS.NumBuckets = 0;
250 RHS.NumNodes = 0;
251 return *this;
252}
253
Jim Laskey43bc1842006-10-27 16:16:16 +0000254FoldingSetImpl::~FoldingSetImpl() {
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000255 free(Buckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000256}
Chandler Carruthb596ba22015-08-16 23:17:27 +0000257
Dan Gohman0e44e0d2008-08-23 00:42:16 +0000258void FoldingSetImpl::clear() {
259 // Set all but the last bucket to null pointers.
260 memset(Buckets, 0, NumBuckets*sizeof(void*));
261
262 // Set the very last bucket to be a non-null "pointer".
263 Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
264
265 // Reset the node count to zero.
266 NumNodes = 0;
267}
Jim Laskey43bc1842006-10-27 16:16:16 +0000268
Jim Laskey43bc1842006-10-27 16:16:16 +0000269/// GrowHashTable - Double the size of the hash table and rehash everything.
270///
271void FoldingSetImpl::GrowHashTable() {
272 void **OldBuckets = Buckets;
273 unsigned OldNumBuckets = NumBuckets;
274 NumBuckets <<= 1;
275
Jim Laskey43bc1842006-10-27 16:16:16 +0000276 // Clear out new buckets.
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000277 Buckets = AllocateBuckets(NumBuckets);
278 NumNodes = 0;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000279
Jim Laskey43bc1842006-10-27 16:16:16 +0000280 // Walk the old buckets, rehashing nodes into their new place.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000281 FoldingSetNodeID TempID;
Jim Laskey43bc1842006-10-27 16:16:16 +0000282 for (unsigned i = 0; i != OldNumBuckets; ++i) {
283 void *Probe = OldBuckets[i];
284 if (!Probe) continue;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000285 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000286 // Figure out the next link, remove NodeInBucket from the old link.
287 Probe = NodeInBucket->getNextInBucket();
Craig Topperc10719f2014-04-07 04:17:22 +0000288 NodeInBucket->SetNextInBucket(nullptr);
Jim Laskey43bc1842006-10-27 16:16:16 +0000289
290 // Insert the node into the new bucket, after recomputing the hash.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000291 InsertNode(NodeInBucket,
292 GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
293 Buckets, NumBuckets));
294 TempID.clear();
Jim Laskey43bc1842006-10-27 16:16:16 +0000295 }
296 }
297
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000298 free(OldBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000299}
300
301/// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
302/// return it. If not, return the insertion token that will make insertion
303/// faster.
Ted Kremenek726933a2008-02-04 17:14:20 +0000304FoldingSetImpl::Node
305*FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
306 void *&InsertPos) {
Benjamin Kramer63057a52012-04-11 14:06:47 +0000307 unsigned IDHash = ID.ComputeHash();
308 void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000309 void *Probe = *Bucket;
310
Craig Topperc10719f2014-04-07 04:17:22 +0000311 InsertPos = nullptr;
Jim Laskey43bc1842006-10-27 16:16:16 +0000312
Dan Gohman9c9ce532010-08-16 15:30:39 +0000313 FoldingSetNodeID TempID;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000314 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Benjamin Kramer63057a52012-04-11 14:06:47 +0000315 if (NodeEquals(NodeInBucket, ID, IDHash, TempID))
Jim Laskey43bc1842006-10-27 16:16:16 +0000316 return NodeInBucket;
Dan Gohman9c9ce532010-08-16 15:30:39 +0000317 TempID.clear();
Jim Laskey43bc1842006-10-27 16:16:16 +0000318
319 Probe = NodeInBucket->getNextInBucket();
320 }
321
322 // Didn't find the node, return null with the bucket as the InsertPos.
323 InsertPos = Bucket;
Craig Topperc10719f2014-04-07 04:17:22 +0000324 return nullptr;
Jim Laskey43bc1842006-10-27 16:16:16 +0000325}
326
327/// InsertNode - Insert the specified node into the folding set, knowing that it
328/// is not already in the map. InsertPos must be obtained from
329/// FindNodeOrInsertPos.
330void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000331 assert(!N->getNextInBucket());
Jim Laskey43bc1842006-10-27 16:16:16 +0000332 // Do we need to grow the hashtable?
Chris Lattner0dbb1372007-01-31 06:04:41 +0000333 if (NumNodes+1 > NumBuckets*2) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000334 GrowHashTable();
Dan Gohman9c9ce532010-08-16 15:30:39 +0000335 FoldingSetNodeID TempID;
336 InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000337 }
Chris Lattner0dbb1372007-01-31 06:04:41 +0000338
339 ++NumNodes;
Jim Laskey43bc1842006-10-27 16:16:16 +0000340
341 /// The insert position is actually a bucket pointer.
342 void **Bucket = static_cast<void**>(InsertPos);
343
344 void *Next = *Bucket;
345
346 // If this is the first insertion into this bucket, its next pointer will be
Chris Lattner8c41ed62007-10-03 20:45:43 +0000347 // null. Pretend as if it pointed to itself, setting the low bit to indicate
348 // that it is a pointer to the bucket.
Craig Topper8d399f82014-04-09 04:20:00 +0000349 if (!Next)
Chris Lattner8c41ed62007-10-03 20:45:43 +0000350 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
Jim Laskey43bc1842006-10-27 16:16:16 +0000351
Chris Lattner0dbb1372007-01-31 06:04:41 +0000352 // Set the node's next pointer, and make the bucket point to the node.
Jim Laskey43bc1842006-10-27 16:16:16 +0000353 N->SetNextInBucket(Next);
354 *Bucket = N;
355}
356
357/// RemoveNode - Remove a node from the folding set, returning true if one was
358/// removed or false if the node was not in the folding set.
359bool FoldingSetImpl::RemoveNode(Node *N) {
360 // Because each bucket is a circular list, we don't need to compute N's hash
Chris Lattner4f5cdec2007-02-01 05:33:21 +0000361 // to remove it.
Jim Laskey43bc1842006-10-27 16:16:16 +0000362 void *Ptr = N->getNextInBucket();
Craig Topper8d399f82014-04-09 04:20:00 +0000363 if (!Ptr) return false; // Not in folding set.
Jim Laskey43bc1842006-10-27 16:16:16 +0000364
365 --NumNodes;
Craig Topperc10719f2014-04-07 04:17:22 +0000366 N->SetNextInBucket(nullptr);
Chris Lattner4f5cdec2007-02-01 05:33:21 +0000367
368 // Remember what N originally pointed to, either a bucket or another node.
369 void *NodeNextPtr = Ptr;
370
371 // Chase around the list until we find the node (or bucket) which points to N.
Jim Laskey43bc1842006-10-27 16:16:16 +0000372 while (true) {
Chris Lattner8c41ed62007-10-03 20:45:43 +0000373 if (Node *NodeInBucket = GetNextPtr(Ptr)) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000374 // Advance pointer.
375 Ptr = NodeInBucket->getNextInBucket();
376
377 // We found a node that points to N, change it to point to N's next node,
378 // removing N from the list.
379 if (Ptr == N) {
380 NodeInBucket->SetNextInBucket(NodeNextPtr);
381 return true;
382 }
383 } else {
384 void **Bucket = GetBucketPtr(Ptr);
385 Ptr = *Bucket;
386
387 // If we found that the bucket points to N, update the bucket to point to
388 // whatever is next.
389 if (Ptr == N) {
390 *Bucket = NodeNextPtr;
391 return true;
392 }
393 }
394 }
395}
396
397/// GetOrInsertNode - If there is an existing simple Node exactly
398/// equal to the specified node, return it. Otherwise, insert 'N' and it
399/// instead.
400FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
Ted Kremenekc0259632008-01-19 04:22:50 +0000401 FoldingSetNodeID ID;
Dan Gohman27c98e62010-08-16 14:53:42 +0000402 GetNodeProfile(N, ID);
Jim Laskey43bc1842006-10-27 16:16:16 +0000403 void *IP;
404 if (Node *E = FindNodeOrInsertPos(ID, IP))
405 return E;
406 InsertNode(N, IP);
407 return N;
408}
Chris Lattner99f6ab72007-10-03 21:12:09 +0000409
410//===----------------------------------------------------------------------===//
411// FoldingSetIteratorImpl Implementation
412
413FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
414 // Skip to the first non-null non-self-cycle bucket.
Ted Kremenekd66c7912008-02-15 21:12:46 +0000415 while (*Bucket != reinterpret_cast<void*>(-1) &&
Craig Topper8d399f82014-04-09 04:20:00 +0000416 (!*Bucket || !GetNextPtr(*Bucket)))
Chris Lattner99f6ab72007-10-03 21:12:09 +0000417 ++Bucket;
418
419 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
420}
421
422void FoldingSetIteratorImpl::advance() {
423 // If there is another link within this bucket, go to it.
424 void *Probe = NodePtr->getNextInBucket();
425
426 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
427 NodePtr = NextNodeInBucket;
428 else {
429 // Otherwise, this is the last link in this bucket.
430 void **Bucket = GetBucketPtr(Probe);
431
432 // Skip to the next non-null non-self-cycle bucket.
433 do {
434 ++Bucket;
Ted Kremenekd66c7912008-02-15 21:12:46 +0000435 } while (*Bucket != reinterpret_cast<void*>(-1) &&
Craig Topper8d399f82014-04-09 04:20:00 +0000436 (!*Bucket || !GetNextPtr(*Bucket)));
Chris Lattner99f6ab72007-10-03 21:12:09 +0000437
438 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
439 }
440}
441
Ted Kremeneke2887862008-02-04 21:11:17 +0000442//===----------------------------------------------------------------------===//
443// FoldingSetBucketIteratorImpl Implementation
444
445FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
Craig Topper8d399f82014-04-09 04:20:00 +0000446 Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket;
Ted Kremeneke2887862008-02-04 21:11:17 +0000447}