Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 1 | //===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a hash set that can be used to remove duplication of |
Chris Lattner | 5412945 | 2012-11-16 18:58:23 +0000 | [diff] [blame] | 11 | // nodes in a graph. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 93cffd2 | 2012-03-01 23:18:44 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Hashing.h" |
Dan Gohman | 6556c89 | 2010-03-18 16:16:38 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Host.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | f16dbf6 | 2006-11-03 01:38:14 +0000 | [diff] [blame] | 21 | #include <cassert> |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 22 | #include <cstring> |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 26 | // FoldingSetNodeIDRef Implementation |
| 27 | |
| 28 | /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 29 | /// used to lookup the node in the FoldingSetBase. |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 30 | unsigned FoldingSetNodeIDRef::ComputeHash() const { |
Chandler Carruth | 93cffd2 | 2012-03-01 23:18:44 +0000 | [diff] [blame] | 31 | return static_cast<unsigned>(hash_combine_range(Data, Data+Size)); |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | bool 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 Kremenek | 0b3e805 | 2012-09-08 04:25:29 +0000 | [diff] [blame] | 39 | /// Used to compare the "ordering" of two nodes as defined by the |
| 40 | /// profiled bits and their ordering defined by memcmp(). |
| 41 | bool 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 Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c025963 | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 48 | // FoldingSetNodeID Implementation |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 49 | |
Duncan Sands | d8e918b | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 50 | /// Add* - Add various data types to Bit data. |
| 51 | /// |
| 52 | void FoldingSetNodeID::AddPointer(const void *Ptr) { |
| 53 | // Note: this adds pointers to the hash using sizes and endianness that |
Sanjay Patel | 16ab5e6 | 2015-04-06 16:21:12 +0000 | [diff] [blame] | 54 | // depend on the host. It doesn't matter, however, because hashing on |
| 55 | // pointer values is inherently unstable. Nothing should depend on the |
Duncan Sands | d8e918b | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 56 | // ordering of nodes in the folding set. |
Richard Smith | e3a9a67 | 2016-10-16 17:49:09 +0000 | [diff] [blame] | 57 | static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long), |
| 58 | "unexpected pointer size"); |
| 59 | AddInteger(reinterpret_cast<uintptr_t>(Ptr)); |
Duncan Sands | d8e918b | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 60 | } |
| 61 | void FoldingSetNodeID::AddInteger(signed I) { |
| 62 | Bits.push_back(I); |
| 63 | } |
| 64 | void FoldingSetNodeID::AddInteger(unsigned I) { |
| 65 | Bits.push_back(I); |
| 66 | } |
| 67 | void FoldingSetNodeID::AddInteger(long I) { |
| 68 | AddInteger((unsigned long)I); |
| 69 | } |
| 70 | void FoldingSetNodeID::AddInteger(unsigned long I) { |
| 71 | if (sizeof(long) == sizeof(int)) |
| 72 | AddInteger(unsigned(I)); |
| 73 | else if (sizeof(long) == sizeof(long long)) { |
| 74 | AddInteger((unsigned long long)I); |
| 75 | } else { |
| 76 | llvm_unreachable("unexpected sizeof(long)"); |
| 77 | } |
| 78 | } |
| 79 | void FoldingSetNodeID::AddInteger(long long I) { |
| 80 | AddInteger((unsigned long long)I); |
| 81 | } |
| 82 | void FoldingSetNodeID::AddInteger(unsigned long long I) { |
| 83 | AddInteger(unsigned(I)); |
Richard Smith | e3a9a67 | 2016-10-16 17:49:09 +0000 | [diff] [blame] | 84 | AddInteger(unsigned(I >> 32)); |
Duncan Sands | d8e918b | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Daniel Dunbar | 78faee0 | 2009-09-22 03:34:53 +0000 | [diff] [blame] | 87 | void FoldingSetNodeID::AddString(StringRef String) { |
| 88 | unsigned Size = String.size(); |
Owen Anderson | 31936d6 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 89 | Bits.push_back(Size); |
| 90 | if (!Size) return; |
| 91 | |
| 92 | unsigned Units = Size / 4; |
| 93 | unsigned Pos = 0; |
Daniel Dunbar | 78faee0 | 2009-09-22 03:34:53 +0000 | [diff] [blame] | 94 | const unsigned *Base = (const unsigned*) String.data(); |
Owen Anderson | 31936d6 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 95 | |
| 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 Johannesen | 461e704 | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 102 | // To be compatible with above bulk transfer, we need to take endianness |
| 103 | // into account. |
Gabor Horvath | fee0434 | 2015-03-16 09:53:42 +0000 | [diff] [blame] | 104 | static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost, |
| 105 | "Unexpected host endianness"); |
Rafael Espindola | 41cb64f | 2013-04-15 14:44:24 +0000 | [diff] [blame] | 106 | if (sys::IsBigEndianHost) { |
Dale Johannesen | 461e704 | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 107 | 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 Horvath | fee0434 | 2015-03-16 09:53:42 +0000 | [diff] [blame] | 114 | } else { // Little-endian host |
Dale Johannesen | 461e704 | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 115 | 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 Anderson | 31936d6 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | // With the leftover bits. |
| 126 | unsigned V = 0; |
Dale Johannesen | 461e704 | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 127 | // Pos will have overshot size by 4 - #bytes left over. |
| 128 | // No need to take endianness into account here - this is always executed. |
Owen Anderson | 31936d6 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 129 | switch (Pos - Size) { |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 130 | case 1: V = (V << 8) | (unsigned char)String[Size - 3]; LLVM_FALLTHROUGH; |
| 131 | case 2: V = (V << 8) | (unsigned char)String[Size - 2]; LLVM_FALLTHROUGH; |
Owen Anderson | 31936d6 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 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 Sands | d8e918b | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 139 | // AddNodeID - Adds the Bit data of another ID to *this. |
| 140 | void 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 |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 145 | /// lookup the node in the FoldingSetBase. |
Ted Kremenek | c025963 | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 146 | unsigned FoldingSetNodeID::ComputeHash() const { |
Dan Gohman | 3d9ed28 | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 147 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash(); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /// operator== - Used to compare two nodes to each other. |
| 151 | /// |
Nick Lewycky | d192517 | 2012-12-25 06:13:25 +0000 | [diff] [blame] | 152 | bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const { |
Dan Gohman | 3d9ed28 | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 153 | return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size()); |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /// operator== - Used to compare two nodes to each other. |
| 157 | /// |
| 158 | bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const { |
Dan Gohman | 3d9ed28 | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 159 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Ted Kremenek | 0b3e805 | 2012-09-08 04:25:29 +0000 | [diff] [blame] | 162 | /// Used to compare the "ordering" of two nodes as defined by the |
| 163 | /// profiled bits and their ordering defined by memcmp(). |
Nick Lewycky | d192517 | 2012-12-25 06:13:25 +0000 | [diff] [blame] | 164 | bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const { |
Ted Kremenek | 0b3e805 | 2012-09-08 04:25:29 +0000 | [diff] [blame] | 165 | return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size()); |
| 166 | } |
| 167 | |
| 168 | bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const { |
| 169 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS; |
| 170 | } |
| 171 | |
Dan Gohman | 6556c89 | 2010-03-18 16:16:38 +0000 | [diff] [blame] | 172 | /// 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. |
| 175 | FoldingSetNodeIDRef |
| 176 | FoldingSetNodeID::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 Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 181 | |
| 182 | //===----------------------------------------------------------------------===// |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 183 | /// Helper functions for FoldingSetBase. |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 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 Lattner | a94523d | 2007-01-30 23:16:22 +0000 | [diff] [blame] | 189 | /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null: |
| 190 | /// use GetBucketPtr when this happens. |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 191 | static FoldingSetBase::Node *GetNextPtr(void *NextInBucketPtr) { |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 192 | // The low bit is set if this is the pointer back to the bucket. |
| 193 | if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 194 | return nullptr; |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 195 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 196 | return static_cast<FoldingSetBase::Node*>(NextInBucketPtr); |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Ted Kremenek | e288786 | 2008-02-04 21:11:17 +0000 | [diff] [blame] | 199 | |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 200 | /// testing. |
| 201 | static void **GetBucketPtr(void *NextInBucketPtr) { |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 202 | intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr); |
Chris Lattner | 99f6ab7 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 203 | assert((Ptr & 1) && "Not a bucket pointer"); |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 204 | return reinterpret_cast<void**>(Ptr & ~intptr_t(1)); |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /// GetBucketFor - Hash the specified node ID and return the hash bucket for |
| 208 | /// the specified ID. |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 209 | static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) { |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 210 | // NumBuckets is always a power of 2. |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 211 | unsigned BucketNum = Hash & (NumBuckets-1); |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 212 | return Buckets + BucketNum; |
| 213 | } |
| 214 | |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 215 | /// AllocateBuckets - Allocated initialized bucket memory. |
| 216 | static void **AllocateBuckets(unsigned NumBuckets) { |
| 217 | void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*))); |
Matthias Braun | c20b338 | 2017-07-20 01:30:39 +0000 | [diff] [blame] | 218 | |
| 219 | if (Buckets == nullptr) |
| 220 | report_bad_alloc_error("Allocation of Buckets failed."); |
| 221 | |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 222 | // Set the very last bucket to be a non-null "pointer". |
| 223 | Buckets[NumBuckets] = reinterpret_cast<void*>(-1); |
| 224 | return Buckets; |
| 225 | } |
| 226 | |
Jim Laskey | 6ca4a34 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 227 | //===----------------------------------------------------------------------===// |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 228 | // FoldingSetBase Implementation |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 229 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 230 | void FoldingSetBase::anchor() {} |
Benjamin Kramer | 66f486f | 2015-03-22 18:22:33 +0000 | [diff] [blame] | 231 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 232 | FoldingSetBase::FoldingSetBase(unsigned Log2InitSize) { |
Jim Laskey | eb0fd25 | 2006-11-02 14:21:26 +0000 | [diff] [blame] | 233 | assert(5 < Log2InitSize && Log2InitSize < 32 && |
| 234 | "Initial hash table size out of range"); |
| 235 | NumBuckets = 1 << Log2InitSize; |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 236 | Buckets = AllocateBuckets(NumBuckets); |
| 237 | NumNodes = 0; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 238 | } |
Chandler Carruth | b596ba2 | 2015-08-16 23:17:27 +0000 | [diff] [blame] | 239 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 240 | FoldingSetBase::FoldingSetBase(FoldingSetBase &&Arg) |
Chandler Carruth | b596ba2 | 2015-08-16 23:17:27 +0000 | [diff] [blame] | 241 | : Buckets(Arg.Buckets), NumBuckets(Arg.NumBuckets), NumNodes(Arg.NumNodes) { |
| 242 | Arg.Buckets = nullptr; |
| 243 | Arg.NumBuckets = 0; |
| 244 | Arg.NumNodes = 0; |
| 245 | } |
| 246 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 247 | FoldingSetBase &FoldingSetBase::operator=(FoldingSetBase &&RHS) { |
Chandler Carruth | b596ba2 | 2015-08-16 23:17:27 +0000 | [diff] [blame] | 248 | free(Buckets); // This may be null if the set is in a moved-from state. |
| 249 | Buckets = RHS.Buckets; |
| 250 | NumBuckets = RHS.NumBuckets; |
| 251 | NumNodes = RHS.NumNodes; |
| 252 | RHS.Buckets = nullptr; |
| 253 | RHS.NumBuckets = 0; |
| 254 | RHS.NumNodes = 0; |
| 255 | return *this; |
| 256 | } |
| 257 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 258 | FoldingSetBase::~FoldingSetBase() { |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 259 | free(Buckets); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 260 | } |
Chandler Carruth | b596ba2 | 2015-08-16 23:17:27 +0000 | [diff] [blame] | 261 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 262 | void FoldingSetBase::clear() { |
Dan Gohman | 0e44e0d | 2008-08-23 00:42:16 +0000 | [diff] [blame] | 263 | // Set all but the last bucket to null pointers. |
| 264 | memset(Buckets, 0, NumBuckets*sizeof(void*)); |
| 265 | |
| 266 | // Set the very last bucket to be a non-null "pointer". |
| 267 | Buckets[NumBuckets] = reinterpret_cast<void*>(-1); |
| 268 | |
| 269 | // Reset the node count to zero. |
| 270 | NumNodes = 0; |
| 271 | } |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 272 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 273 | void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount) { |
Ben Craig | 60adb92 | 2016-06-03 13:54:48 +0000 | [diff] [blame] | 274 | assert((NewBucketCount > NumBuckets) && "Can't shrink a folding set with GrowBucketCount"); |
| 275 | assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!"); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 276 | void **OldBuckets = Buckets; |
| 277 | unsigned OldNumBuckets = NumBuckets; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 278 | |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 279 | // Clear out new buckets. |
Matthias Braun | c20b338 | 2017-07-20 01:30:39 +0000 | [diff] [blame] | 280 | Buckets = AllocateBuckets(NewBucketCount); |
| 281 | // Set NumBuckets only if allocation of new buckets was succesful |
| 282 | NumBuckets = NewBucketCount; |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 283 | NumNodes = 0; |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 284 | |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 285 | // Walk the old buckets, rehashing nodes into their new place. |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 286 | FoldingSetNodeID TempID; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 287 | for (unsigned i = 0; i != OldNumBuckets; ++i) { |
| 288 | void *Probe = OldBuckets[i]; |
| 289 | if (!Probe) continue; |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 290 | while (Node *NodeInBucket = GetNextPtr(Probe)) { |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 291 | // Figure out the next link, remove NodeInBucket from the old link. |
| 292 | Probe = NodeInBucket->getNextInBucket(); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 293 | NodeInBucket->SetNextInBucket(nullptr); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 294 | |
| 295 | // Insert the node into the new bucket, after recomputing the hash. |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 296 | InsertNode(NodeInBucket, |
| 297 | GetBucketFor(ComputeNodeHash(NodeInBucket, TempID), |
| 298 | Buckets, NumBuckets)); |
| 299 | TempID.clear(); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Benjamin Kramer | bf5c3d4 | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 303 | free(OldBuckets); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Ben Craig | 60adb92 | 2016-06-03 13:54:48 +0000 | [diff] [blame] | 306 | /// GrowHashTable - Double the size of the hash table and rehash everything. |
| 307 | /// |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 308 | void FoldingSetBase::GrowHashTable() { |
Ben Craig | 60adb92 | 2016-06-03 13:54:48 +0000 | [diff] [blame] | 309 | GrowBucketCount(NumBuckets * 2); |
| 310 | } |
| 311 | |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 312 | void FoldingSetBase::reserve(unsigned EltCount) { |
Ben Craig | 60adb92 | 2016-06-03 13:54:48 +0000 | [diff] [blame] | 313 | // This will give us somewhere between EltCount / 2 and |
| 314 | // EltCount buckets. This puts us in the load factor |
| 315 | // range of 1.0 - 2.0. |
| 316 | if(EltCount < capacity()) |
| 317 | return; |
| 318 | GrowBucketCount(PowerOf2Floor(EltCount)); |
| 319 | } |
| 320 | |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 321 | /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, |
| 322 | /// return it. If not, return the insertion token that will make insertion |
| 323 | /// faster. |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 324 | FoldingSetBase::Node * |
| 325 | FoldingSetBase::FindNodeOrInsertPos(const FoldingSetNodeID &ID, |
| 326 | void *&InsertPos) { |
Benjamin Kramer | 63057a5 | 2012-04-11 14:06:47 +0000 | [diff] [blame] | 327 | unsigned IDHash = ID.ComputeHash(); |
| 328 | void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 329 | void *Probe = *Bucket; |
| 330 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 331 | InsertPos = nullptr; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 332 | |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 333 | FoldingSetNodeID TempID; |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 334 | while (Node *NodeInBucket = GetNextPtr(Probe)) { |
Benjamin Kramer | 63057a5 | 2012-04-11 14:06:47 +0000 | [diff] [blame] | 335 | if (NodeEquals(NodeInBucket, ID, IDHash, TempID)) |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 336 | return NodeInBucket; |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 337 | TempID.clear(); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 338 | |
| 339 | Probe = NodeInBucket->getNextInBucket(); |
| 340 | } |
| 341 | |
| 342 | // Didn't find the node, return null with the bucket as the InsertPos. |
| 343 | InsertPos = Bucket; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 344 | return nullptr; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /// InsertNode - Insert the specified node into the folding set, knowing that it |
| 348 | /// is not already in the map. InsertPos must be obtained from |
| 349 | /// FindNodeOrInsertPos. |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 350 | void FoldingSetBase::InsertNode(Node *N, void *InsertPos) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 351 | assert(!N->getNextInBucket()); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 352 | // Do we need to grow the hashtable? |
Ben Craig | 60adb92 | 2016-06-03 13:54:48 +0000 | [diff] [blame] | 353 | if (NumNodes+1 > capacity()) { |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 354 | GrowHashTable(); |
Dan Gohman | 9c9ce53 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 355 | FoldingSetNodeID TempID; |
| 356 | InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 357 | } |
Chris Lattner | 0dbb137 | 2007-01-31 06:04:41 +0000 | [diff] [blame] | 358 | |
| 359 | ++NumNodes; |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 360 | |
| 361 | /// The insert position is actually a bucket pointer. |
| 362 | void **Bucket = static_cast<void**>(InsertPos); |
| 363 | |
| 364 | void *Next = *Bucket; |
| 365 | |
| 366 | // If this is the first insertion into this bucket, its next pointer will be |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 367 | // null. Pretend as if it pointed to itself, setting the low bit to indicate |
| 368 | // that it is a pointer to the bucket. |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 369 | if (!Next) |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 370 | Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 0dbb137 | 2007-01-31 06:04:41 +0000 | [diff] [blame] | 372 | // Set the node's next pointer, and make the bucket point to the node. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 373 | N->SetNextInBucket(Next); |
| 374 | *Bucket = N; |
| 375 | } |
| 376 | |
| 377 | /// RemoveNode - Remove a node from the folding set, returning true if one was |
| 378 | /// removed or false if the node was not in the folding set. |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 379 | bool FoldingSetBase::RemoveNode(Node *N) { |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 380 | // Because each bucket is a circular list, we don't need to compute N's hash |
Chris Lattner | 4f5cdec | 2007-02-01 05:33:21 +0000 | [diff] [blame] | 381 | // to remove it. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 382 | void *Ptr = N->getNextInBucket(); |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 383 | if (!Ptr) return false; // Not in folding set. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 384 | |
| 385 | --NumNodes; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 386 | N->SetNextInBucket(nullptr); |
Chris Lattner | 4f5cdec | 2007-02-01 05:33:21 +0000 | [diff] [blame] | 387 | |
| 388 | // Remember what N originally pointed to, either a bucket or another node. |
| 389 | void *NodeNextPtr = Ptr; |
| 390 | |
| 391 | // Chase around the list until we find the node (or bucket) which points to N. |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 392 | while (true) { |
Chris Lattner | 8c41ed6 | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 393 | if (Node *NodeInBucket = GetNextPtr(Ptr)) { |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 394 | // Advance pointer. |
| 395 | Ptr = NodeInBucket->getNextInBucket(); |
| 396 | |
| 397 | // We found a node that points to N, change it to point to N's next node, |
| 398 | // removing N from the list. |
| 399 | if (Ptr == N) { |
| 400 | NodeInBucket->SetNextInBucket(NodeNextPtr); |
| 401 | return true; |
| 402 | } |
| 403 | } else { |
| 404 | void **Bucket = GetBucketPtr(Ptr); |
| 405 | Ptr = *Bucket; |
| 406 | |
| 407 | // If we found that the bucket points to N, update the bucket to point to |
| 408 | // whatever is next. |
| 409 | if (Ptr == N) { |
| 410 | *Bucket = NodeNextPtr; |
| 411 | return true; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /// GetOrInsertNode - If there is an existing simple Node exactly |
| 418 | /// equal to the specified node, return it. Otherwise, insert 'N' and it |
| 419 | /// instead. |
George Burgess IV | 1f990e5 | 2017-06-12 20:52:53 +0000 | [diff] [blame] | 420 | FoldingSetBase::Node *FoldingSetBase::GetOrInsertNode(FoldingSetBase::Node *N) { |
Ted Kremenek | c025963 | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 421 | FoldingSetNodeID ID; |
Dan Gohman | 27c98e6 | 2010-08-16 14:53:42 +0000 | [diff] [blame] | 422 | GetNodeProfile(N, ID); |
Jim Laskey | 43bc184 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 423 | void *IP; |
| 424 | if (Node *E = FindNodeOrInsertPos(ID, IP)) |
| 425 | return E; |
| 426 | InsertNode(N, IP); |
| 427 | return N; |
| 428 | } |
Chris Lattner | 99f6ab7 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 429 | |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | // FoldingSetIteratorImpl Implementation |
| 432 | |
| 433 | FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) { |
| 434 | // Skip to the first non-null non-self-cycle bucket. |
Ted Kremenek | d66c791 | 2008-02-15 21:12:46 +0000 | [diff] [blame] | 435 | while (*Bucket != reinterpret_cast<void*>(-1) && |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 436 | (!*Bucket || !GetNextPtr(*Bucket))) |
Chris Lattner | 99f6ab7 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 437 | ++Bucket; |
| 438 | |
| 439 | NodePtr = static_cast<FoldingSetNode*>(*Bucket); |
| 440 | } |
| 441 | |
| 442 | void FoldingSetIteratorImpl::advance() { |
| 443 | // If there is another link within this bucket, go to it. |
| 444 | void *Probe = NodePtr->getNextInBucket(); |
| 445 | |
| 446 | if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe)) |
| 447 | NodePtr = NextNodeInBucket; |
| 448 | else { |
| 449 | // Otherwise, this is the last link in this bucket. |
| 450 | void **Bucket = GetBucketPtr(Probe); |
| 451 | |
| 452 | // Skip to the next non-null non-self-cycle bucket. |
| 453 | do { |
| 454 | ++Bucket; |
Ted Kremenek | d66c791 | 2008-02-15 21:12:46 +0000 | [diff] [blame] | 455 | } while (*Bucket != reinterpret_cast<void*>(-1) && |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 456 | (!*Bucket || !GetNextPtr(*Bucket))); |
Chris Lattner | 99f6ab7 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 457 | |
| 458 | NodePtr = static_cast<FoldingSetNode*>(*Bucket); |
| 459 | } |
| 460 | } |
| 461 | |
Ted Kremenek | e288786 | 2008-02-04 21:11:17 +0000 | [diff] [blame] | 462 | //===----------------------------------------------------------------------===// |
| 463 | // FoldingSetBucketIteratorImpl Implementation |
| 464 | |
| 465 | FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) { |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 466 | Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket; |
Ted Kremenek | e288786 | 2008-02-04 21:11:17 +0000 | [diff] [blame] | 467 | } |