FoldingSet.cpp revision 0a3fecad0a4bf48a83eb7370a4d46880efdc6971
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file implements a hash set that can be used to remove duplication of 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// nodes in a graph. This code was originally created by Chris Lattner for use 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with SelectionDAGCSEMap, but was isolated to provide use across the llvm code 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// set. 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/FoldingSet.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/APFloat.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MathExtras.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cassert> 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace llvm; 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===// 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// FoldingSetNodeID Implementation 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Add* - Add various data types to Bit data. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddPointer(const void *Ptr) { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Note: this adds pointers to the hash using sizes and endianness that 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // depend on the host. It doesn't matter however, because hashing on 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // pointer values in inherently unstable. Nothing should depend on the 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ordering of nodes in the folding set. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) intptr_t PtrI = (intptr_t)Ptr; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(unsigned(PtrI)); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (sizeof(intptr_t) > sizeof(unsigned)) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(unsigned(uint64_t(PtrI) >> 32)); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddInteger(signed I) { 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(I); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddInteger(unsigned I) { 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(I); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddInteger(int64_t I) { 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddInteger((uint64_t)I); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddInteger(uint64_t I) { 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(unsigned(I)); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the integer is small, encode it just as 32-bits. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((uint64_t)(int)I != I) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(unsigned(I >> 32)); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddFloat(float F) { 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(FloatToBits(F)); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddDouble(double D) { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddInteger(DoubleToBits(D)); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FoldingSetNodeID::AddAPFloat(const APFloat& apf) { 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) APInt api = apf.convertToAPInt(); 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const uint64_t *p = api.getRawData(); 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (unsigned i=0; i<api.getNumWords(); i++) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddInteger(*p++); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void FoldingSetNodeID::AddString(const std::string &String) { 675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) unsigned Size = String.size(); 685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Bits.push_back(Size); 695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (!Size) return; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Units = Size / 4; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Pos = 0; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const unsigned *Base = (const unsigned *)String.data(); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the string is aligned do a bulk transfer. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!((intptr_t)Base & 3)) { 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.append(Base, Base + Units); 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Pos = (Units + 1) * 4; 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise do it the hard way. 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for ( Pos += 4; Pos <= Size; Pos += 4) { 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned V = ((unsigned char)String[Pos - 4] << 24) | 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ((unsigned char)String[Pos - 3] << 16) | 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ((unsigned char)String[Pos - 2] << 8) | 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (unsigned char)String[Pos - 1]; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(V); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // With the leftover bits. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned V = 0; 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pos will have overshot size by 4 - #bytes left over. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch (Pos - Size) { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru. 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru. 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: return; // Nothing left. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Bits.push_back(V); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// lookup the node in the FoldingSetImpl. 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned FoldingSetNodeID::ComputeHash() const { 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This is adapted from SuperFastHash by Paul Hsieh. 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Hash = Bits.size(); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (const unsigned *BP = &Bits[0], *E = BP+Bits.size(); BP != E; ++BP) { 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Data = *BP; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash += Data & 0xFFFF; 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Tmp = ((Data >> 16) << 11) ^ Hash; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash = (Hash << 16) ^ Tmp; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash += Hash >> 11; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Force "avalanching" of final 127 bits. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash ^= Hash << 3; 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash += Hash >> 5; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash ^= Hash << 4; 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash += Hash >> 17; 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash ^= Hash << 25; 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Hash += Hash >> 6; 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return Hash; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// operator== - Used to compare two nodes to each other. 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS)const{ 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Bits.size() != RHS.Bits.size()) return false; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return memcmp(&Bits[0], &RHS.Bits[0], Bits.size()*sizeof(Bits[0])) == 0; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Helper functions for FoldingSetImpl. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// GetNextPtr - In order to save space, each bucket is a 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// singly-linked-list. In order to make deletion more efficient, we make 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// the list circular, so we can delete a node without computing its hash. 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The problem with this is that the start of the hash buckets are not 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null: 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// use GetBucketPtr when this happens. 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) { 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The low bit is set if this is the pointer back to the bucket. 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1) 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr); 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// GetBucketPtr - Provides a casting of a bucket pointer for isNode 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// testing. 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void **GetBucketPtr(void *NextInBucketPtr) { 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr); 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert((Ptr & 1) && "Not a bucket pointer"); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return reinterpret_cast<void**>(Ptr & ~intptr_t(1)); 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// GetBucketFor - Hash the specified node ID and return the hash bucket for 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// the specified ID. 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void **GetBucketFor(const FoldingSetNodeID &ID, 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void **Buckets, unsigned NumBuckets) { 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // NumBuckets is always a power of 2. 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned BucketNum = ID.ComputeHash() & (NumBuckets-1); 165 return Buckets + BucketNum; 166} 167 168//===----------------------------------------------------------------------===// 169// FoldingSetImpl Implementation 170 171FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) : NumNodes(0) { 172 assert(5 < Log2InitSize && Log2InitSize < 32 && 173 "Initial hash table size out of range"); 174 NumBuckets = 1 << Log2InitSize; 175 Buckets = new void*[NumBuckets+1]; 176 memset(Buckets, 0, NumBuckets*sizeof(void*)); 177 178 // Set the very last bucket to be a non-null "pointer". 179 Buckets[NumBuckets] = reinterpret_cast<void*>(-2); 180} 181FoldingSetImpl::~FoldingSetImpl() { 182 delete [] Buckets; 183} 184 185/// GrowHashTable - Double the size of the hash table and rehash everything. 186/// 187void FoldingSetImpl::GrowHashTable() { 188 void **OldBuckets = Buckets; 189 unsigned OldNumBuckets = NumBuckets; 190 NumBuckets <<= 1; 191 192 // Reset the node count to zero: we're going to reinsert everything. 193 NumNodes = 0; 194 195 // Clear out new buckets. 196 Buckets = new void*[NumBuckets+1]; 197 memset(Buckets, 0, NumBuckets*sizeof(void*)); 198 199 // Set the very last bucket to be a non-null "pointer". 200 Buckets[NumBuckets] = reinterpret_cast<void*>(-1); 201 202 // Walk the old buckets, rehashing nodes into their new place. 203 for (unsigned i = 0; i != OldNumBuckets; ++i) { 204 void *Probe = OldBuckets[i]; 205 if (!Probe) continue; 206 while (Node *NodeInBucket = GetNextPtr(Probe)) { 207 // Figure out the next link, remove NodeInBucket from the old link. 208 Probe = NodeInBucket->getNextInBucket(); 209 NodeInBucket->SetNextInBucket(0); 210 211 // Insert the node into the new bucket, after recomputing the hash. 212 FoldingSetNodeID ID; 213 GetNodeProfile(ID, NodeInBucket); 214 InsertNode(NodeInBucket, GetBucketFor(ID, Buckets, NumBuckets)); 215 } 216 } 217 218 delete[] OldBuckets; 219} 220 221/// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, 222/// return it. If not, return the insertion token that will make insertion 223/// faster. 224FoldingSetImpl::Node *FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID, 225 void *&InsertPos) { 226 void **Bucket = GetBucketFor(ID, Buckets, NumBuckets); 227 void *Probe = *Bucket; 228 229 InsertPos = 0; 230 231 while (Node *NodeInBucket = GetNextPtr(Probe)) { 232 FoldingSetNodeID OtherID; 233 GetNodeProfile(OtherID, NodeInBucket); 234 if (OtherID == ID) 235 return NodeInBucket; 236 237 Probe = NodeInBucket->getNextInBucket(); 238 } 239 240 // Didn't find the node, return null with the bucket as the InsertPos. 241 InsertPos = Bucket; 242 return 0; 243} 244 245/// InsertNode - Insert the specified node into the folding set, knowing that it 246/// is not already in the map. InsertPos must be obtained from 247/// FindNodeOrInsertPos. 248void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) { 249 assert(N->getNextInBucket() == 0); 250 // Do we need to grow the hashtable? 251 if (NumNodes+1 > NumBuckets*2) { 252 GrowHashTable(); 253 FoldingSetNodeID ID; 254 GetNodeProfile(ID, N); 255 InsertPos = GetBucketFor(ID, Buckets, NumBuckets); 256 } 257 258 ++NumNodes; 259 260 /// The insert position is actually a bucket pointer. 261 void **Bucket = static_cast<void**>(InsertPos); 262 263 void *Next = *Bucket; 264 265 // If this is the first insertion into this bucket, its next pointer will be 266 // null. Pretend as if it pointed to itself, setting the low bit to indicate 267 // that it is a pointer to the bucket. 268 if (Next == 0) 269 Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1); 270 271 // Set the node's next pointer, and make the bucket point to the node. 272 N->SetNextInBucket(Next); 273 *Bucket = N; 274} 275 276/// RemoveNode - Remove a node from the folding set, returning true if one was 277/// removed or false if the node was not in the folding set. 278bool FoldingSetImpl::RemoveNode(Node *N) { 279 // Because each bucket is a circular list, we don't need to compute N's hash 280 // to remove it. 281 void *Ptr = N->getNextInBucket(); 282 if (Ptr == 0) return false; // Not in folding set. 283 284 --NumNodes; 285 N->SetNextInBucket(0); 286 287 // Remember what N originally pointed to, either a bucket or another node. 288 void *NodeNextPtr = Ptr; 289 290 // Chase around the list until we find the node (or bucket) which points to N. 291 while (true) { 292 if (Node *NodeInBucket = GetNextPtr(Ptr)) { 293 // Advance pointer. 294 Ptr = NodeInBucket->getNextInBucket(); 295 296 // We found a node that points to N, change it to point to N's next node, 297 // removing N from the list. 298 if (Ptr == N) { 299 NodeInBucket->SetNextInBucket(NodeNextPtr); 300 return true; 301 } 302 } else { 303 void **Bucket = GetBucketPtr(Ptr); 304 Ptr = *Bucket; 305 306 // If we found that the bucket points to N, update the bucket to point to 307 // whatever is next. 308 if (Ptr == N) { 309 *Bucket = NodeNextPtr; 310 return true; 311 } 312 } 313 } 314} 315 316/// GetOrInsertNode - If there is an existing simple Node exactly 317/// equal to the specified node, return it. Otherwise, insert 'N' and it 318/// instead. 319FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) { 320 FoldingSetNodeID ID; 321 GetNodeProfile(ID, N); 322 void *IP; 323 if (Node *E = FindNodeOrInsertPos(ID, IP)) 324 return E; 325 InsertNode(N, IP); 326 return N; 327} 328 329//===----------------------------------------------------------------------===// 330// FoldingSetIteratorImpl Implementation 331 332FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) { 333 // Skip to the first non-null non-self-cycle bucket. 334 while (*Bucket == 0 || GetNextPtr(*Bucket) == 0) 335 ++Bucket; 336 337 NodePtr = static_cast<FoldingSetNode*>(*Bucket); 338} 339 340void FoldingSetIteratorImpl::advance() { 341 // If there is another link within this bucket, go to it. 342 void *Probe = NodePtr->getNextInBucket(); 343 344 if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe)) 345 NodePtr = NextNodeInBucket; 346 else { 347 // Otherwise, this is the last link in this bucket. 348 void **Bucket = GetBucketPtr(Probe); 349 350 // Skip to the next non-null non-self-cycle bucket. 351 do { 352 ++Bucket; 353 } while (*Bucket == 0 || GetNextPtr(*Bucket) == 0); 354 355 NodePtr = static_cast<FoldingSetNode*>(*Bucket); 356 } 357} 358 359