blob: ce6f196e1060f7d857d5466034997293e66244c4 [file] [log] [blame]
Jim Laskey43bc1842006-10-27 16:16:16 +00001//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jim Laskey43bc1842006-10-27 16:16:16 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a hash set that can be used to remove duplication of
Chris Lattner54129452012-11-16 18:58:23 +000010// nodes in a graph.
Jim Laskey43bc1842006-10-27 16:16:16 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/FoldingSet.h"
Chandler Carruth93cffd22012-03-01 23:18:44 +000015#include "llvm/ADT/Hashing.h"
Dan Gohman6556c892010-03-18 16:16:38 +000016#include "llvm/Support/Allocator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/MathExtras.h"
Rafael Espindolaf16dbf62006-11-03 01:38:14 +000020#include <cassert>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <cstring>
Jim Laskey43bc1842006-10-27 16:16:16 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
Dan Gohman9c9ce532010-08-16 15:30:39 +000025// FoldingSetNodeIDRef Implementation
26
27/// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
George Burgess IV1f990e52017-06-12 20:52:53 +000028/// used to lookup the node in the FoldingSetBase.
Dan Gohman9c9ce532010-08-16 15:30:39 +000029unsigned FoldingSetNodeIDRef::ComputeHash() const {
Chandler Carruth93cffd22012-03-01 23:18:44 +000030 return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
Dan Gohman9c9ce532010-08-16 15:30:39 +000031}
32
33bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
34 if (Size != RHS.Size) return false;
35 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
36}
37
Ted Kremenek0b3e8052012-09-08 04:25:29 +000038/// Used to compare the "ordering" of two nodes as defined by the
39/// profiled bits and their ordering defined by memcmp().
40bool FoldingSetNodeIDRef::operator<(FoldingSetNodeIDRef RHS) const {
41 if (Size != RHS.Size)
42 return Size < RHS.Size;
43 return memcmp(Data, RHS.Data, Size*sizeof(*Data)) < 0;
44}
45
Dan Gohman9c9ce532010-08-16 15:30:39 +000046//===----------------------------------------------------------------------===//
Ted Kremenekc0259632008-01-19 04:22:50 +000047// FoldingSetNodeID Implementation
Jim Laskey43bc1842006-10-27 16:16:16 +000048
Duncan Sandsd8e918b2012-03-08 09:32:21 +000049/// Add* - Add various data types to Bit data.
50///
51void FoldingSetNodeID::AddPointer(const void *Ptr) {
52 // Note: this adds pointers to the hash using sizes and endianness that
Sanjay Patel16ab5e62015-04-06 16:21:12 +000053 // depend on the host. It doesn't matter, however, because hashing on
54 // pointer values is inherently unstable. Nothing should depend on the
Duncan Sandsd8e918b2012-03-08 09:32:21 +000055 // ordering of nodes in the folding set.
Richard Smithe3a9a672016-10-16 17:49:09 +000056 static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long),
57 "unexpected pointer size");
58 AddInteger(reinterpret_cast<uintptr_t>(Ptr));
Duncan Sandsd8e918b2012-03-08 09:32:21 +000059}
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));
Richard Smithe3a9a672016-10-16 17:49:09 +000083 AddInteger(unsigned(I >> 32));
Duncan Sandsd8e918b2012-03-08 09:32:21 +000084}
85
Daniel Dunbar78faee02009-09-22 03:34:53 +000086void FoldingSetNodeID::AddString(StringRef String) {
87 unsigned Size = String.size();
Owen Anderson31936d62008-07-01 23:49:59 +000088 Bits.push_back(Size);
89 if (!Size) return;
90
91 unsigned Units = Size / 4;
92 unsigned Pos = 0;
Daniel Dunbar78faee02009-09-22 03:34:53 +000093 const unsigned *Base = (const unsigned*) String.data();
Fangrui Songf78650a2018-07-30 19:41:25 +000094
Owen Anderson31936d62008-07-01 23:49:59 +000095 // If the string is aligned do a bulk transfer.
96 if (!((intptr_t)Base & 3)) {
97 Bits.append(Base, Base + Units);
98 Pos = (Units + 1) * 4;
99 } else {
100 // Otherwise do it the hard way.
Dale Johannesen461e7042010-11-19 00:48:58 +0000101 // To be compatible with above bulk transfer, we need to take endianness
102 // into account.
Gabor Horvathfee04342015-03-16 09:53:42 +0000103 static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost,
104 "Unexpected host endianness");
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000105 if (sys::IsBigEndianHost) {
Dale Johannesen461e7042010-11-19 00:48:58 +0000106 for (Pos += 4; Pos <= Size; Pos += 4) {
107 unsigned V = ((unsigned char)String[Pos - 4] << 24) |
108 ((unsigned char)String[Pos - 3] << 16) |
109 ((unsigned char)String[Pos - 2] << 8) |
110 (unsigned char)String[Pos - 1];
111 Bits.push_back(V);
112 }
Gabor Horvathfee04342015-03-16 09:53:42 +0000113 } else { // Little-endian host
Dale Johannesen461e7042010-11-19 00:48:58 +0000114 for (Pos += 4; Pos <= Size; Pos += 4) {
115 unsigned V = ((unsigned char)String[Pos - 1] << 24) |
116 ((unsigned char)String[Pos - 2] << 16) |
117 ((unsigned char)String[Pos - 3] << 8) |
118 (unsigned char)String[Pos - 4];
119 Bits.push_back(V);
120 }
Owen Anderson31936d62008-07-01 23:49:59 +0000121 }
122 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000123
Owen Anderson31936d62008-07-01 23:49:59 +0000124 // With the leftover bits.
125 unsigned V = 0;
Dale Johannesen461e7042010-11-19 00:48:58 +0000126 // Pos will have overshot size by 4 - #bytes left over.
127 // No need to take endianness into account here - this is always executed.
Owen Anderson31936d62008-07-01 23:49:59 +0000128 switch (Pos - Size) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000129 case 1: V = (V << 8) | (unsigned char)String[Size - 3]; LLVM_FALLTHROUGH;
130 case 2: V = (V << 8) | (unsigned char)String[Size - 2]; LLVM_FALLTHROUGH;
Owen Anderson31936d62008-07-01 23:49:59 +0000131 case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
132 default: return; // Nothing left.
133 }
134
135 Bits.push_back(V);
136}
137
Duncan Sandsd8e918b2012-03-08 09:32:21 +0000138// AddNodeID - Adds the Bit data of another ID to *this.
139void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) {
140 Bits.append(ID.Bits.begin(), ID.Bits.end());
141}
142
Fangrui Songf78650a2018-07-30 19:41:25 +0000143/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
George Burgess IV1f990e52017-06-12 20:52:53 +0000144/// lookup the node in the FoldingSetBase.
Ted Kremenekc0259632008-01-19 04:22:50 +0000145unsigned FoldingSetNodeID::ComputeHash() const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000146 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
Jim Laskey43bc1842006-10-27 16:16:16 +0000147}
148
149/// operator== - Used to compare two nodes to each other.
150///
Nick Lewyckyd1925172012-12-25 06:13:25 +0000151bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000152 return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
Dan Gohman9c9ce532010-08-16 15:30:39 +0000153}
154
155/// operator== - Used to compare two nodes to each other.
156///
157bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
Dan Gohman3d9ed282010-08-24 23:16:53 +0000158 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
Jim Laskey43bc1842006-10-27 16:16:16 +0000159}
160
Ted Kremenek0b3e8052012-09-08 04:25:29 +0000161/// Used to compare the "ordering" of two nodes as defined by the
162/// profiled bits and their ordering defined by memcmp().
Nick Lewyckyd1925172012-12-25 06:13:25 +0000163bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const {
Ted Kremenek0b3e8052012-09-08 04:25:29 +0000164 return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
165}
166
167bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const {
168 return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS;
169}
170
Dan Gohman6556c892010-03-18 16:16:38 +0000171/// Intern - Copy this node's data to a memory region allocated from the
172/// given allocator and return a FoldingSetNodeIDRef describing the
173/// interned data.
174FoldingSetNodeIDRef
175FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
176 unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
177 std::uninitialized_copy(Bits.begin(), Bits.end(), New);
178 return FoldingSetNodeIDRef(New, Bits.size());
179}
Jim Laskey43bc1842006-10-27 16:16:16 +0000180
181//===----------------------------------------------------------------------===//
George Burgess IV1f990e52017-06-12 20:52:53 +0000182/// Helper functions for FoldingSetBase.
Jim Laskey6ca4a342006-10-27 18:05:12 +0000183
184/// GetNextPtr - In order to save space, each bucket is a
185/// singly-linked-list. In order to make deletion more efficient, we make
186/// the list circular, so we can delete a node without computing its hash.
187/// The problem with this is that the start of the hash buckets are not
Chris Lattnera94523d2007-01-30 23:16:22 +0000188/// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
189/// use GetBucketPtr when this happens.
George Burgess IV1f990e52017-06-12 20:52:53 +0000190static FoldingSetBase::Node *GetNextPtr(void *NextInBucketPtr) {
Chris Lattner8c41ed62007-10-03 20:45:43 +0000191 // The low bit is set if this is the pointer back to the bucket.
192 if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
Craig Topperc10719f2014-04-07 04:17:22 +0000193 return nullptr;
Fangrui Songf78650a2018-07-30 19:41:25 +0000194
George Burgess IV1f990e52017-06-12 20:52:53 +0000195 return static_cast<FoldingSetBase::Node*>(NextInBucketPtr);
Jim Laskey6ca4a342006-10-27 18:05:12 +0000196}
197
Ted Kremeneke2887862008-02-04 21:11:17 +0000198
Jim Laskey6ca4a342006-10-27 18:05:12 +0000199/// testing.
200static void **GetBucketPtr(void *NextInBucketPtr) {
Chris Lattner8c41ed62007-10-03 20:45:43 +0000201 intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
Chris Lattner99f6ab72007-10-03 21:12:09 +0000202 assert((Ptr & 1) && "Not a bucket pointer");
Chris Lattner8c41ed62007-10-03 20:45:43 +0000203 return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
Jim Laskey6ca4a342006-10-27 18:05:12 +0000204}
205
206/// GetBucketFor - Hash the specified node ID and return the hash bucket for
207/// the specified ID.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000208static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
Jim Laskey6ca4a342006-10-27 18:05:12 +0000209 // NumBuckets is always a power of 2.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000210 unsigned BucketNum = Hash & (NumBuckets-1);
Jim Laskey6ca4a342006-10-27 18:05:12 +0000211 return Buckets + BucketNum;
212}
213
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000214/// AllocateBuckets - Allocated initialized bucket memory.
215static void **AllocateBuckets(unsigned NumBuckets) {
Serge Pavlov15681ad2018-06-09 05:19:45 +0000216 void **Buckets = static_cast<void**>(safe_calloc(NumBuckets + 1,
217 sizeof(void*)));
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000218 // 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//===----------------------------------------------------------------------===//
George Burgess IV1f990e52017-06-12 20:52:53 +0000224// FoldingSetBase Implementation
Jim Laskey43bc1842006-10-27 16:16:16 +0000225
George Burgess IV1f990e52017-06-12 20:52:53 +0000226void FoldingSetBase::anchor() {}
Benjamin Kramer66f486f2015-03-22 18:22:33 +0000227
George Burgess IV1f990e52017-06-12 20:52:53 +0000228FoldingSetBase::FoldingSetBase(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
George Burgess IV1f990e52017-06-12 20:52:53 +0000236FoldingSetBase::FoldingSetBase(FoldingSetBase &&Arg)
Chandler Carruthb596ba22015-08-16 23:17:27 +0000237 : Buckets(Arg.Buckets), NumBuckets(Arg.NumBuckets), NumNodes(Arg.NumNodes) {
238 Arg.Buckets = nullptr;
239 Arg.NumBuckets = 0;
240 Arg.NumNodes = 0;
241}
242
George Burgess IV1f990e52017-06-12 20:52:53 +0000243FoldingSetBase &FoldingSetBase::operator=(FoldingSetBase &&RHS) {
Chandler Carruthb596ba22015-08-16 23:17:27 +0000244 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
George Burgess IV1f990e52017-06-12 20:52:53 +0000254FoldingSetBase::~FoldingSetBase() {
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
George Burgess IV1f990e52017-06-12 20:52:53 +0000258void FoldingSetBase::clear() {
Dan Gohman0e44e0d2008-08-23 00:42:16 +0000259 // 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
George Burgess IV1f990e52017-06-12 20:52:53 +0000269void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount) {
Ben Craig60adb922016-06-03 13:54:48 +0000270 assert((NewBucketCount > NumBuckets) && "Can't shrink a folding set with GrowBucketCount");
271 assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!");
Jim Laskey43bc1842006-10-27 16:16:16 +0000272 void **OldBuckets = Buckets;
273 unsigned OldNumBuckets = NumBuckets;
Fangrui Songf78650a2018-07-30 19:41:25 +0000274
Jim Laskey43bc1842006-10-27 16:16:16 +0000275 // Clear out new buckets.
Matthias Braunc20b3382017-07-20 01:30:39 +0000276 Buckets = AllocateBuckets(NewBucketCount);
Adrian Prantl86497ad2018-09-14 16:12:14 +0000277 // Set NumBuckets only if allocation of new buckets was successful.
Fangrui Songf78650a2018-07-30 19:41:25 +0000278 NumBuckets = NewBucketCount;
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000279 NumNodes = 0;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000280
Jim Laskey43bc1842006-10-27 16:16:16 +0000281 // Walk the old buckets, rehashing nodes into their new place.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000282 FoldingSetNodeID TempID;
Jim Laskey43bc1842006-10-27 16:16:16 +0000283 for (unsigned i = 0; i != OldNumBuckets; ++i) {
284 void *Probe = OldBuckets[i];
285 if (!Probe) continue;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000286 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000287 // Figure out the next link, remove NodeInBucket from the old link.
288 Probe = NodeInBucket->getNextInBucket();
Craig Topperc10719f2014-04-07 04:17:22 +0000289 NodeInBucket->SetNextInBucket(nullptr);
Jim Laskey43bc1842006-10-27 16:16:16 +0000290
291 // Insert the node into the new bucket, after recomputing the hash.
Dan Gohman9c9ce532010-08-16 15:30:39 +0000292 InsertNode(NodeInBucket,
293 GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
294 Buckets, NumBuckets));
295 TempID.clear();
Jim Laskey43bc1842006-10-27 16:16:16 +0000296 }
297 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000298
Benjamin Kramerbf5c3d42010-06-19 17:00:31 +0000299 free(OldBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000300}
301
Ben Craig60adb922016-06-03 13:54:48 +0000302/// GrowHashTable - Double the size of the hash table and rehash everything.
303///
George Burgess IV1f990e52017-06-12 20:52:53 +0000304void FoldingSetBase::GrowHashTable() {
Ben Craig60adb922016-06-03 13:54:48 +0000305 GrowBucketCount(NumBuckets * 2);
306}
307
George Burgess IV1f990e52017-06-12 20:52:53 +0000308void FoldingSetBase::reserve(unsigned EltCount) {
Ben Craig60adb922016-06-03 13:54:48 +0000309 // This will give us somewhere between EltCount / 2 and
310 // EltCount buckets. This puts us in the load factor
311 // range of 1.0 - 2.0.
312 if(EltCount < capacity())
313 return;
314 GrowBucketCount(PowerOf2Floor(EltCount));
315}
316
Jim Laskey43bc1842006-10-27 16:16:16 +0000317/// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
318/// return it. If not, return the insertion token that will make insertion
319/// faster.
George Burgess IV1f990e52017-06-12 20:52:53 +0000320FoldingSetBase::Node *
321FoldingSetBase::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
322 void *&InsertPos) {
Benjamin Kramer63057a52012-04-11 14:06:47 +0000323 unsigned IDHash = ID.ComputeHash();
324 void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000325 void *Probe = *Bucket;
Fangrui Songf78650a2018-07-30 19:41:25 +0000326
Craig Topperc10719f2014-04-07 04:17:22 +0000327 InsertPos = nullptr;
Fangrui Songf78650a2018-07-30 19:41:25 +0000328
Dan Gohman9c9ce532010-08-16 15:30:39 +0000329 FoldingSetNodeID TempID;
Chris Lattner8c41ed62007-10-03 20:45:43 +0000330 while (Node *NodeInBucket = GetNextPtr(Probe)) {
Benjamin Kramer63057a52012-04-11 14:06:47 +0000331 if (NodeEquals(NodeInBucket, ID, IDHash, TempID))
Jim Laskey43bc1842006-10-27 16:16:16 +0000332 return NodeInBucket;
Dan Gohman9c9ce532010-08-16 15:30:39 +0000333 TempID.clear();
Jim Laskey43bc1842006-10-27 16:16:16 +0000334
335 Probe = NodeInBucket->getNextInBucket();
336 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000337
Jim Laskey43bc1842006-10-27 16:16:16 +0000338 // Didn't find the node, return null with the bucket as the InsertPos.
339 InsertPos = Bucket;
Craig Topperc10719f2014-04-07 04:17:22 +0000340 return nullptr;
Jim Laskey43bc1842006-10-27 16:16:16 +0000341}
342
343/// InsertNode - Insert the specified node into the folding set, knowing that it
Fangrui Songf78650a2018-07-30 19:41:25 +0000344/// is not already in the map. InsertPos must be obtained from
Jim Laskey43bc1842006-10-27 16:16:16 +0000345/// FindNodeOrInsertPos.
George Burgess IV1f990e52017-06-12 20:52:53 +0000346void FoldingSetBase::InsertNode(Node *N, void *InsertPos) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000347 assert(!N->getNextInBucket());
Jim Laskey43bc1842006-10-27 16:16:16 +0000348 // Do we need to grow the hashtable?
Ben Craig60adb922016-06-03 13:54:48 +0000349 if (NumNodes+1 > capacity()) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000350 GrowHashTable();
Dan Gohman9c9ce532010-08-16 15:30:39 +0000351 FoldingSetNodeID TempID;
352 InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
Jim Laskey43bc1842006-10-27 16:16:16 +0000353 }
Chris Lattner0dbb1372007-01-31 06:04:41 +0000354
355 ++NumNodes;
Fangrui Songf78650a2018-07-30 19:41:25 +0000356
Jim Laskey43bc1842006-10-27 16:16:16 +0000357 /// The insert position is actually a bucket pointer.
358 void **Bucket = static_cast<void**>(InsertPos);
Fangrui Songf78650a2018-07-30 19:41:25 +0000359
Jim Laskey43bc1842006-10-27 16:16:16 +0000360 void *Next = *Bucket;
Fangrui Songf78650a2018-07-30 19:41:25 +0000361
Jim Laskey43bc1842006-10-27 16:16:16 +0000362 // If this is the first insertion into this bucket, its next pointer will be
Chris Lattner8c41ed62007-10-03 20:45:43 +0000363 // null. Pretend as if it pointed to itself, setting the low bit to indicate
364 // that it is a pointer to the bucket.
Craig Topper8d399f82014-04-09 04:20:00 +0000365 if (!Next)
Chris Lattner8c41ed62007-10-03 20:45:43 +0000366 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
Jim Laskey43bc1842006-10-27 16:16:16 +0000367
Chris Lattner0dbb1372007-01-31 06:04:41 +0000368 // Set the node's next pointer, and make the bucket point to the node.
Jim Laskey43bc1842006-10-27 16:16:16 +0000369 N->SetNextInBucket(Next);
370 *Bucket = N;
371}
372
373/// RemoveNode - Remove a node from the folding set, returning true if one was
374/// removed or false if the node was not in the folding set.
George Burgess IV1f990e52017-06-12 20:52:53 +0000375bool FoldingSetBase::RemoveNode(Node *N) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000376 // Because each bucket is a circular list, we don't need to compute N's hash
Chris Lattner4f5cdec2007-02-01 05:33:21 +0000377 // to remove it.
Jim Laskey43bc1842006-10-27 16:16:16 +0000378 void *Ptr = N->getNextInBucket();
Craig Topper8d399f82014-04-09 04:20:00 +0000379 if (!Ptr) return false; // Not in folding set.
Jim Laskey43bc1842006-10-27 16:16:16 +0000380
381 --NumNodes;
Craig Topperc10719f2014-04-07 04:17:22 +0000382 N->SetNextInBucket(nullptr);
Chris Lattner4f5cdec2007-02-01 05:33:21 +0000383
384 // Remember what N originally pointed to, either a bucket or another node.
385 void *NodeNextPtr = Ptr;
Fangrui Songf78650a2018-07-30 19:41:25 +0000386
Chris Lattner4f5cdec2007-02-01 05:33:21 +0000387 // Chase around the list until we find the node (or bucket) which points to N.
Jim Laskey43bc1842006-10-27 16:16:16 +0000388 while (true) {
Chris Lattner8c41ed62007-10-03 20:45:43 +0000389 if (Node *NodeInBucket = GetNextPtr(Ptr)) {
Jim Laskey43bc1842006-10-27 16:16:16 +0000390 // Advance pointer.
391 Ptr = NodeInBucket->getNextInBucket();
Fangrui Songf78650a2018-07-30 19:41:25 +0000392
Jim Laskey43bc1842006-10-27 16:16:16 +0000393 // We found a node that points to N, change it to point to N's next node,
394 // removing N from the list.
395 if (Ptr == N) {
396 NodeInBucket->SetNextInBucket(NodeNextPtr);
397 return true;
398 }
399 } else {
400 void **Bucket = GetBucketPtr(Ptr);
401 Ptr = *Bucket;
Fangrui Songf78650a2018-07-30 19:41:25 +0000402
Jim Laskey43bc1842006-10-27 16:16:16 +0000403 // If we found that the bucket points to N, update the bucket to point to
404 // whatever is next.
405 if (Ptr == N) {
406 *Bucket = NodeNextPtr;
407 return true;
408 }
409 }
410 }
411}
412
413/// GetOrInsertNode - If there is an existing simple Node exactly
414/// equal to the specified node, return it. Otherwise, insert 'N' and it
415/// instead.
George Burgess IV1f990e52017-06-12 20:52:53 +0000416FoldingSetBase::Node *FoldingSetBase::GetOrInsertNode(FoldingSetBase::Node *N) {
Ted Kremenekc0259632008-01-19 04:22:50 +0000417 FoldingSetNodeID ID;
Dan Gohman27c98e62010-08-16 14:53:42 +0000418 GetNodeProfile(N, ID);
Jim Laskey43bc1842006-10-27 16:16:16 +0000419 void *IP;
420 if (Node *E = FindNodeOrInsertPos(ID, IP))
421 return E;
422 InsertNode(N, IP);
423 return N;
424}
Chris Lattner99f6ab72007-10-03 21:12:09 +0000425
426//===----------------------------------------------------------------------===//
427// FoldingSetIteratorImpl Implementation
428
429FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
430 // Skip to the first non-null non-self-cycle bucket.
Ted Kremenekd66c7912008-02-15 21:12:46 +0000431 while (*Bucket != reinterpret_cast<void*>(-1) &&
Craig Topper8d399f82014-04-09 04:20:00 +0000432 (!*Bucket || !GetNextPtr(*Bucket)))
Chris Lattner99f6ab72007-10-03 21:12:09 +0000433 ++Bucket;
Fangrui Songf78650a2018-07-30 19:41:25 +0000434
Chris Lattner99f6ab72007-10-03 21:12:09 +0000435 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
436}
437
438void FoldingSetIteratorImpl::advance() {
439 // If there is another link within this bucket, go to it.
440 void *Probe = NodePtr->getNextInBucket();
441
442 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
443 NodePtr = NextNodeInBucket;
444 else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000445 // Otherwise, this is the last link in this bucket.
Chris Lattner99f6ab72007-10-03 21:12:09 +0000446 void **Bucket = GetBucketPtr(Probe);
447
448 // Skip to the next non-null non-self-cycle bucket.
449 do {
450 ++Bucket;
Ted Kremenekd66c7912008-02-15 21:12:46 +0000451 } while (*Bucket != reinterpret_cast<void*>(-1) &&
Craig Topper8d399f82014-04-09 04:20:00 +0000452 (!*Bucket || !GetNextPtr(*Bucket)));
Fangrui Songf78650a2018-07-30 19:41:25 +0000453
Chris Lattner99f6ab72007-10-03 21:12:09 +0000454 NodePtr = static_cast<FoldingSetNode*>(*Bucket);
455 }
456}
457
Ted Kremeneke2887862008-02-04 21:11:17 +0000458//===----------------------------------------------------------------------===//
459// FoldingSetBucketIteratorImpl Implementation
460
461FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
Craig Topper8d399f82014-04-09 04:20:00 +0000462 Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket;
Ted Kremeneke2887862008-02-04 21:11:17 +0000463}