Jim Laskey | 0e5af19 | 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 | 4ee451d | 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 | 0e5af19 | 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 | 79dcd43 | 2012-11-16 18:58:23 +0000 | [diff] [blame] | 11 | // nodes in a graph. |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | abe24cf | 2012-03-01 23:18:44 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Hashing.h" |
Dan Gohman | c93b4cf | 2010-03-18 16:16:38 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Host.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | 39c6d3a | 2006-11-03 01:38:14 +0000 | [diff] [blame] | 21 | #include <cassert> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 22 | #include <cstring> |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 26 | // FoldingSetNodeIDRef Implementation |
| 27 | |
| 28 | /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, |
| 29 | /// used to lookup the node in the FoldingSetImpl. |
| 30 | unsigned FoldingSetNodeIDRef::ComputeHash() const { |
Chandler Carruth | abe24cf | 2012-03-01 23:18:44 +0000 | [diff] [blame] | 31 | return static_cast<unsigned>(hash_combine_range(Data, Data+Size)); |
Dan Gohman | 3063410 | 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 | 0d651e0 | 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 | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0a3feca | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 48 | // FoldingSetNodeID Implementation |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 49 | |
Duncan Sands | ed5edea | 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 |
| 54 | // depend on the host. It doesn't matter however, because hashing on |
| 55 | // pointer values in inherently unstable. Nothing should depend on the |
| 56 | // ordering of nodes in the folding set. |
| 57 | Bits.append(reinterpret_cast<unsigned *>(&Ptr), |
| 58 | reinterpret_cast<unsigned *>(&Ptr+1)); |
| 59 | } |
| 60 | void FoldingSetNodeID::AddInteger(signed I) { |
| 61 | Bits.push_back(I); |
| 62 | } |
| 63 | void FoldingSetNodeID::AddInteger(unsigned I) { |
| 64 | Bits.push_back(I); |
| 65 | } |
| 66 | void FoldingSetNodeID::AddInteger(long I) { |
| 67 | AddInteger((unsigned long)I); |
| 68 | } |
| 69 | void 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 | } |
| 78 | void FoldingSetNodeID::AddInteger(long long I) { |
| 79 | AddInteger((unsigned long long)I); |
| 80 | } |
| 81 | void 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 Dunbar | 27dba67 | 2009-09-22 03:34:53 +0000 | [diff] [blame] | 87 | void FoldingSetNodeID::AddString(StringRef String) { |
| 88 | unsigned Size = String.size(); |
Owen Anderson | 72e61b8 | 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 | 27dba67 | 2009-09-22 03:34:53 +0000 | [diff] [blame] | 94 | const unsigned *Base = (const unsigned*) String.data(); |
Owen Anderson | 72e61b8 | 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 | c81c7fe | 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. |
Rafael Espindola | 21a01d1 | 2013-04-15 14:44:24 +0000 | [diff] [blame^] | 104 | if (sys::IsBigEndianHost) { |
Dale Johannesen | c81c7fe | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 105 | for (Pos += 4; Pos <= Size; Pos += 4) { |
| 106 | unsigned V = ((unsigned char)String[Pos - 4] << 24) | |
| 107 | ((unsigned char)String[Pos - 3] << 16) | |
| 108 | ((unsigned char)String[Pos - 2] << 8) | |
| 109 | (unsigned char)String[Pos - 1]; |
| 110 | Bits.push_back(V); |
| 111 | } |
| 112 | } else { |
Rafael Espindola | 21a01d1 | 2013-04-15 14:44:24 +0000 | [diff] [blame^] | 113 | assert(sys::IsLittleEndianHost && "Unexpected host endianness"); |
Dale Johannesen | c81c7fe | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 114 | 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 Anderson | 72e61b8 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | // With the leftover bits. |
| 125 | unsigned V = 0; |
Dale Johannesen | c81c7fe | 2010-11-19 00:48:58 +0000 | [diff] [blame] | 126 | // Pos will have overshot size by 4 - #bytes left over. |
| 127 | // No need to take endianness into account here - this is always executed. |
Owen Anderson | 72e61b8 | 2008-07-01 23:49:59 +0000 | [diff] [blame] | 128 | switch (Pos - Size) { |
| 129 | case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru. |
| 130 | case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru. |
| 131 | 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 Sands | ed5edea | 2012-03-08 09:32:21 +0000 | [diff] [blame] | 138 | // AddNodeID - Adds the Bit data of another ID to *this. |
| 139 | void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) { |
| 140 | Bits.append(ID.Bits.begin(), ID.Bits.end()); |
| 141 | } |
| 142 | |
| 143 | /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 144 | /// lookup the node in the FoldingSetImpl. |
Ted Kremenek | 0a3feca | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 145 | unsigned FoldingSetNodeID::ComputeHash() const { |
Dan Gohman | 365c53e | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 146 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash(); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /// operator== - Used to compare two nodes to each other. |
| 150 | /// |
Nick Lewycky | 08d785b | 2012-12-25 06:13:25 +0000 | [diff] [blame] | 151 | bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const { |
Dan Gohman | 365c53e | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 152 | return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size()); |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /// operator== - Used to compare two nodes to each other. |
| 156 | /// |
| 157 | bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const { |
Dan Gohman | 365c53e | 2010-08-24 23:16:53 +0000 | [diff] [blame] | 158 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS; |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | 0d651e0 | 2012-09-08 04:25:29 +0000 | [diff] [blame] | 161 | /// Used to compare the "ordering" of two nodes as defined by the |
| 162 | /// profiled bits and their ordering defined by memcmp(). |
Nick Lewycky | 08d785b | 2012-12-25 06:13:25 +0000 | [diff] [blame] | 163 | bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const { |
Ted Kremenek | 0d651e0 | 2012-09-08 04:25:29 +0000 | [diff] [blame] | 164 | return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size()); |
| 165 | } |
| 166 | |
| 167 | bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const { |
| 168 | return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS; |
| 169 | } |
| 170 | |
Dan Gohman | c93b4cf | 2010-03-18 16:16:38 +0000 | [diff] [blame] | 171 | /// 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. |
| 174 | FoldingSetNodeIDRef |
| 175 | FoldingSetNodeID::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 Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 180 | |
| 181 | //===----------------------------------------------------------------------===// |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 182 | /// Helper functions for FoldingSetImpl. |
| 183 | |
| 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 Lattner | 3cab071 | 2007-01-30 23:16:22 +0000 | [diff] [blame] | 188 | /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null: |
| 189 | /// use GetBucketPtr when this happens. |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 190 | static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) { |
| 191 | // The low bit is set if this is the pointer back to the bucket. |
| 192 | if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1) |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 193 | return 0; |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 194 | |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 195 | return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr); |
| 196 | } |
| 197 | |
Ted Kremenek | 26e3c44 | 2008-02-04 21:11:17 +0000 | [diff] [blame] | 198 | |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 199 | /// testing. |
| 200 | static void **GetBucketPtr(void *NextInBucketPtr) { |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 201 | intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr); |
Chris Lattner | 116c321 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 202 | assert((Ptr & 1) && "Not a bucket pointer"); |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 203 | return reinterpret_cast<void**>(Ptr & ~intptr_t(1)); |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /// GetBucketFor - Hash the specified node ID and return the hash bucket for |
| 207 | /// the specified ID. |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 208 | static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) { |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 209 | // NumBuckets is always a power of 2. |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 210 | unsigned BucketNum = Hash & (NumBuckets-1); |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 211 | return Buckets + BucketNum; |
| 212 | } |
| 213 | |
Benjamin Kramer | 6118efa | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 214 | /// AllocateBuckets - Allocated initialized bucket memory. |
| 215 | static void **AllocateBuckets(unsigned NumBuckets) { |
| 216 | void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*))); |
| 217 | // Set the very last bucket to be a non-null "pointer". |
| 218 | Buckets[NumBuckets] = reinterpret_cast<void*>(-1); |
| 219 | return Buckets; |
| 220 | } |
| 221 | |
Jim Laskey | 18529f3 | 2006-10-27 18:05:12 +0000 | [diff] [blame] | 222 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 223 | // FoldingSetImpl Implementation |
| 224 | |
Dan Gohman | 535de1a | 2008-08-23 00:42:16 +0000 | [diff] [blame] | 225 | FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) { |
Jim Laskey | 1f67a99 | 2006-11-02 14:21:26 +0000 | [diff] [blame] | 226 | assert(5 < Log2InitSize && Log2InitSize < 32 && |
| 227 | "Initial hash table size out of range"); |
| 228 | NumBuckets = 1 << Log2InitSize; |
Benjamin Kramer | 6118efa | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 229 | Buckets = AllocateBuckets(NumBuckets); |
| 230 | NumNodes = 0; |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 231 | } |
| 232 | FoldingSetImpl::~FoldingSetImpl() { |
Benjamin Kramer | 6118efa | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 233 | free(Buckets); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 234 | } |
Dan Gohman | 535de1a | 2008-08-23 00:42:16 +0000 | [diff] [blame] | 235 | void FoldingSetImpl::clear() { |
| 236 | // Set all but the last bucket to null pointers. |
| 237 | memset(Buckets, 0, NumBuckets*sizeof(void*)); |
| 238 | |
| 239 | // Set the very last bucket to be a non-null "pointer". |
| 240 | Buckets[NumBuckets] = reinterpret_cast<void*>(-1); |
| 241 | |
| 242 | // Reset the node count to zero. |
| 243 | NumNodes = 0; |
| 244 | } |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 245 | |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 246 | /// GrowHashTable - Double the size of the hash table and rehash everything. |
| 247 | /// |
| 248 | void FoldingSetImpl::GrowHashTable() { |
| 249 | void **OldBuckets = Buckets; |
| 250 | unsigned OldNumBuckets = NumBuckets; |
| 251 | NumBuckets <<= 1; |
| 252 | |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 253 | // Clear out new buckets. |
Benjamin Kramer | 6118efa | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 254 | Buckets = AllocateBuckets(NumBuckets); |
| 255 | NumNodes = 0; |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 256 | |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 257 | // Walk the old buckets, rehashing nodes into their new place. |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 258 | FoldingSetNodeID TempID; |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 259 | for (unsigned i = 0; i != OldNumBuckets; ++i) { |
| 260 | void *Probe = OldBuckets[i]; |
| 261 | if (!Probe) continue; |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 262 | while (Node *NodeInBucket = GetNextPtr(Probe)) { |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 263 | // Figure out the next link, remove NodeInBucket from the old link. |
| 264 | Probe = NodeInBucket->getNextInBucket(); |
| 265 | NodeInBucket->SetNextInBucket(0); |
| 266 | |
| 267 | // Insert the node into the new bucket, after recomputing the hash. |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 268 | InsertNode(NodeInBucket, |
| 269 | GetBucketFor(ComputeNodeHash(NodeInBucket, TempID), |
| 270 | Buckets, NumBuckets)); |
| 271 | TempID.clear(); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Benjamin Kramer | 6118efa | 2010-06-19 17:00:31 +0000 | [diff] [blame] | 275 | free(OldBuckets); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, |
| 279 | /// return it. If not, return the insertion token that will make insertion |
| 280 | /// faster. |
Ted Kremenek | 27a8e0d | 2008-02-04 17:14:20 +0000 | [diff] [blame] | 281 | FoldingSetImpl::Node |
| 282 | *FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID, |
| 283 | void *&InsertPos) { |
Benjamin Kramer | f7c3e5f | 2012-04-11 14:06:47 +0000 | [diff] [blame] | 284 | unsigned IDHash = ID.ComputeHash(); |
| 285 | void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 286 | void *Probe = *Bucket; |
| 287 | |
| 288 | InsertPos = 0; |
| 289 | |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 290 | FoldingSetNodeID TempID; |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 291 | while (Node *NodeInBucket = GetNextPtr(Probe)) { |
Benjamin Kramer | f7c3e5f | 2012-04-11 14:06:47 +0000 | [diff] [blame] | 292 | if (NodeEquals(NodeInBucket, ID, IDHash, TempID)) |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 293 | return NodeInBucket; |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 294 | TempID.clear(); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 295 | |
| 296 | Probe = NodeInBucket->getNextInBucket(); |
| 297 | } |
| 298 | |
| 299 | // Didn't find the node, return null with the bucket as the InsertPos. |
| 300 | InsertPos = Bucket; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /// InsertNode - Insert the specified node into the folding set, knowing that it |
| 305 | /// is not already in the map. InsertPos must be obtained from |
| 306 | /// FindNodeOrInsertPos. |
| 307 | void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) { |
Chris Lattner | 0de4439 | 2007-02-01 05:33:21 +0000 | [diff] [blame] | 308 | assert(N->getNextInBucket() == 0); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 309 | // Do we need to grow the hashtable? |
Chris Lattner | b85210f | 2007-01-31 06:04:41 +0000 | [diff] [blame] | 310 | if (NumNodes+1 > NumBuckets*2) { |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 311 | GrowHashTable(); |
Dan Gohman | 3063410 | 2010-08-16 15:30:39 +0000 | [diff] [blame] | 312 | FoldingSetNodeID TempID; |
| 313 | InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | b85210f | 2007-01-31 06:04:41 +0000 | [diff] [blame] | 315 | |
| 316 | ++NumNodes; |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 317 | |
| 318 | /// The insert position is actually a bucket pointer. |
| 319 | void **Bucket = static_cast<void**>(InsertPos); |
| 320 | |
| 321 | void *Next = *Bucket; |
| 322 | |
| 323 | // If this is the first insertion into this bucket, its next pointer will be |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 324 | // null. Pretend as if it pointed to itself, setting the low bit to indicate |
| 325 | // that it is a pointer to the bucket. |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 326 | if (Next == 0) |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 327 | Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 328 | |
Chris Lattner | b85210f | 2007-01-31 06:04:41 +0000 | [diff] [blame] | 329 | // Set the node's next pointer, and make the bucket point to the node. |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 330 | N->SetNextInBucket(Next); |
| 331 | *Bucket = N; |
| 332 | } |
| 333 | |
| 334 | /// RemoveNode - Remove a node from the folding set, returning true if one was |
| 335 | /// removed or false if the node was not in the folding set. |
| 336 | bool FoldingSetImpl::RemoveNode(Node *N) { |
| 337 | // Because each bucket is a circular list, we don't need to compute N's hash |
Chris Lattner | 0de4439 | 2007-02-01 05:33:21 +0000 | [diff] [blame] | 338 | // to remove it. |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 339 | void *Ptr = N->getNextInBucket(); |
| 340 | if (Ptr == 0) return false; // Not in folding set. |
| 341 | |
| 342 | --NumNodes; |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 343 | N->SetNextInBucket(0); |
Chris Lattner | 0de4439 | 2007-02-01 05:33:21 +0000 | [diff] [blame] | 344 | |
| 345 | // Remember what N originally pointed to, either a bucket or another node. |
| 346 | void *NodeNextPtr = Ptr; |
| 347 | |
| 348 | // Chase around the list until we find the node (or bucket) which points to N. |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 349 | while (true) { |
Chris Lattner | 9a7288b | 2007-10-03 20:45:43 +0000 | [diff] [blame] | 350 | if (Node *NodeInBucket = GetNextPtr(Ptr)) { |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 351 | // Advance pointer. |
| 352 | Ptr = NodeInBucket->getNextInBucket(); |
| 353 | |
| 354 | // We found a node that points to N, change it to point to N's next node, |
| 355 | // removing N from the list. |
| 356 | if (Ptr == N) { |
| 357 | NodeInBucket->SetNextInBucket(NodeNextPtr); |
| 358 | return true; |
| 359 | } |
| 360 | } else { |
| 361 | void **Bucket = GetBucketPtr(Ptr); |
| 362 | Ptr = *Bucket; |
| 363 | |
| 364 | // If we found that the bucket points to N, update the bucket to point to |
| 365 | // whatever is next. |
| 366 | if (Ptr == N) { |
| 367 | *Bucket = NodeNextPtr; |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// GetOrInsertNode - If there is an existing simple Node exactly |
| 375 | /// equal to the specified node, return it. Otherwise, insert 'N' and it |
| 376 | /// instead. |
| 377 | FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) { |
Ted Kremenek | 0a3feca | 2008-01-19 04:22:50 +0000 | [diff] [blame] | 378 | FoldingSetNodeID ID; |
Dan Gohman | 6616f7e | 2010-08-16 14:53:42 +0000 | [diff] [blame] | 379 | GetNodeProfile(N, ID); |
Jim Laskey | 0e5af19 | 2006-10-27 16:16:16 +0000 | [diff] [blame] | 380 | void *IP; |
| 381 | if (Node *E = FindNodeOrInsertPos(ID, IP)) |
| 382 | return E; |
| 383 | InsertNode(N, IP); |
| 384 | return N; |
| 385 | } |
Chris Lattner | 116c321 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 386 | |
| 387 | //===----------------------------------------------------------------------===// |
| 388 | // FoldingSetIteratorImpl Implementation |
| 389 | |
| 390 | FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) { |
| 391 | // Skip to the first non-null non-self-cycle bucket. |
Ted Kremenek | e3e0957 | 2008-02-15 21:12:46 +0000 | [diff] [blame] | 392 | while (*Bucket != reinterpret_cast<void*>(-1) && |
| 393 | (*Bucket == 0 || GetNextPtr(*Bucket) == 0)) |
Chris Lattner | 116c321 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 394 | ++Bucket; |
| 395 | |
| 396 | NodePtr = static_cast<FoldingSetNode*>(*Bucket); |
| 397 | } |
| 398 | |
| 399 | void FoldingSetIteratorImpl::advance() { |
| 400 | // If there is another link within this bucket, go to it. |
| 401 | void *Probe = NodePtr->getNextInBucket(); |
| 402 | |
| 403 | if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe)) |
| 404 | NodePtr = NextNodeInBucket; |
| 405 | else { |
| 406 | // Otherwise, this is the last link in this bucket. |
| 407 | void **Bucket = GetBucketPtr(Probe); |
| 408 | |
| 409 | // Skip to the next non-null non-self-cycle bucket. |
| 410 | do { |
| 411 | ++Bucket; |
Ted Kremenek | e3e0957 | 2008-02-15 21:12:46 +0000 | [diff] [blame] | 412 | } while (*Bucket != reinterpret_cast<void*>(-1) && |
| 413 | (*Bucket == 0 || GetNextPtr(*Bucket) == 0)); |
Chris Lattner | 116c321 | 2007-10-03 21:12:09 +0000 | [diff] [blame] | 414 | |
| 415 | NodePtr = static_cast<FoldingSetNode*>(*Bucket); |
| 416 | } |
| 417 | } |
| 418 | |
Ted Kremenek | 26e3c44 | 2008-02-04 21:11:17 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
| 420 | // FoldingSetBucketIteratorImpl Implementation |
| 421 | |
| 422 | FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) { |
| 423 | Ptr = (*Bucket == 0 || GetNextPtr(*Bucket) == 0) ? (void*) Bucket : *Bucket; |
| 424 | } |