FoldingSet.cpp revision 47ce6b4752ae245c3443bfbbc6268b902a03a4b2
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This file was developed by James M. Laskey and is distributed under
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// the University of Illinois Open Source License. See LICENSE.TXT for details.
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===//
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This file implements a hash set that can be used to remove duplication of
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// nodes in a graph.  This code was originally created by Chris Lattner for use
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// with SelectionDAGCSEMap, but was isolated to provide use across the llvm code
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// set.
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===//
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/FoldingSet.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/Support/MathExtras.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using namespace llvm;
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===//
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// FoldingSetImpl::NodeID Implementation
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// Add* - Add various data types to Bit data.
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)///
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void FoldingSetImpl::NodeID::AddPointer(const void *Ptr) {
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Note: this adds pointers to the hash using sizes and endianness that
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // depend on the host.  It doesn't matter however, because hashing on
29  // pointer values in inherently unstable.  Nothing  should depend on the
30  // ordering of nodes in the folding set.
31  intptr_t PtrI = (intptr_t)Ptr;
32  Bits.push_back(unsigned(PtrI));
33  if (sizeof(intptr_t) > sizeof(unsigned))
34    Bits.push_back(unsigned(uint64_t(PtrI) >> 32));
35}
36void FoldingSetImpl::NodeID::AddInteger(signed I) {
37  Bits.push_back(I);
38}
39void FoldingSetImpl::NodeID::AddInteger(unsigned I) {
40  Bits.push_back(I);
41}
42void FoldingSetImpl::NodeID::AddInteger(uint64_t I) {
43  Bits.push_back(unsigned(I));
44  Bits.push_back(unsigned(I >> 32));
45}
46void FoldingSetImpl::NodeID::AddFloat(float F) {
47  Bits.push_back(FloatToBits(F));
48}
49void FoldingSetImpl::NodeID::AddDouble(double D) {
50 AddInteger(DoubleToBits(D));
51}
52void FoldingSetImpl::NodeID::AddString(const std::string &String) {
53  unsigned Size = String.size();
54  unsigned Units = Size / 4;
55  unsigned Pos = 0;
56  const unsigned *Base = (const unsigned *)String.data();
57
58  // If the string is aligned do a bulk transfer.
59#if 0  // FIXME - Add insert to SmallVector (tested with vector)
60  if (!((intptr_t)Base & 3)) {
61    Bits.insert(Bits.end(), Base, Base + Units);
62    Pos = Units * sizeof(unsigned);
63  } else {
64#else
65  {
66#endif
67    // Otherwise do it the hard way.
68    for ( Pos += 4; Pos < Size; Pos += 4) {
69      unsigned V = ((unsigned char)String[Pos - 4] << 24) |
70                   ((unsigned char)String[Pos - 3] << 16) |
71                   ((unsigned char)String[Pos - 2] << 8) |
72                    (unsigned char)String[Pos - 1];
73      Bits.push_back(V);
74    }
75  }
76
77  // With the leftover bits.
78  unsigned V = 0;
79  // Pos will have overshot size by 4 - #bytes left over.
80  switch (Pos - Size) {
81  case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
82  case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
83  case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
84  case 0: return; // Nothing left.
85  }
86
87  Bits.push_back(V);
88}
89
90/// ComputeHash - Compute a strong hash value for this NodeID, used to
91/// lookup the node in the FoldingSetImpl.
92unsigned FoldingSetImpl::NodeID::ComputeHash() const {
93  // This is adapted from SuperFastHash by Paul Hsieh.
94  unsigned Hash = Bits.size();
95  for (const unsigned *BP = &Bits[0], *E = BP+Bits.size(); BP != E; ++BP) {
96    unsigned Data = *BP;
97    Hash         += Data & 0xFFFF;
98    unsigned Tmp  = ((Data >> 16) << 11) ^ Hash;
99    Hash          = (Hash << 16) ^ Tmp;
100    Hash         += Hash >> 11;
101  }
102
103  // Force "avalanching" of final 127 bits.
104  Hash ^= Hash << 3;
105  Hash += Hash >> 5;
106  Hash ^= Hash << 4;
107  Hash += Hash >> 17;
108  Hash ^= Hash << 25;
109  Hash += Hash >> 6;
110  return Hash;
111}
112
113/// operator== - Used to compare two nodes to each other.
114///
115bool FoldingSetImpl::NodeID::operator==(const FoldingSetImpl::NodeID &RHS)const{
116  if (Bits.size() != RHS.Bits.size()) return false;
117  return memcmp(&Bits[0], &RHS.Bits[0], Bits.size()*sizeof(Bits[0])) == 0;
118}
119
120
121//===----------------------------------------------------------------------===//
122/// Helper functions for FoldingSetImpl.
123
124/// GetNextPtr - In order to save space, each bucket is a
125/// singly-linked-list. In order to make deletion more efficient, we make
126/// the list circular, so we can delete a node without computing its hash.
127/// The problem with this is that the start of the hash buckets are not
128/// Nodes.  If NextInBucketPtr is a bucket pointer, this method returns null
129/// : use GetBucketPtr when this happens.
130static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr,
131                                        void **Buckets, unsigned NumBuckets) {
132  if (NextInBucketPtr >= Buckets && NextInBucketPtr < Buckets + NumBuckets)
133    return 0;
134  return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
135}
136
137/// GetBucketPtr - Provides a casting of a bucket pointer for isNode
138/// testing.
139static void **GetBucketPtr(void *NextInBucketPtr) {
140  return static_cast<void**>(NextInBucketPtr);
141}
142
143/// GetBucketFor - Hash the specified node ID and return the hash bucket for
144/// the specified ID.
145static void **GetBucketFor(const FoldingSetImpl::NodeID &ID,
146                           void **Buckets, unsigned NumBuckets) {
147  // NumBuckets is always a power of 2.
148  unsigned BucketNum = ID.ComputeHash() & (NumBuckets-1);
149  return Buckets + BucketNum;
150}
151
152//===----------------------------------------------------------------------===//
153// FoldingSetImpl Implementation
154
155FoldingSetImpl::FoldingSetImpl() : NumNodes(0) {
156  NumBuckets = 64;
157  Buckets = new void*[NumBuckets];
158  memset(Buckets, 0, NumBuckets*sizeof(void*));
159}
160FoldingSetImpl::~FoldingSetImpl() {
161  delete [] Buckets;
162}
163
164/// GrowHashTable - Double the size of the hash table and rehash everything.
165///
166void FoldingSetImpl::GrowHashTable() {
167  void **OldBuckets = Buckets;
168  unsigned OldNumBuckets = NumBuckets;
169  NumBuckets <<= 1;
170
171  // Reset the node count to zero: we're going to reinsert everything.
172  NumNodes = 0;
173
174  // Clear out new buckets.
175  Buckets = new void*[NumBuckets];
176  memset(Buckets, 0, NumBuckets*sizeof(void*));
177
178  // Walk the old buckets, rehashing nodes into their new place.
179  for (unsigned i = 0; i != OldNumBuckets; ++i) {
180    void *Probe = OldBuckets[i];
181    if (!Probe) continue;
182    while (Node *NodeInBucket = GetNextPtr(Probe, OldBuckets, OldNumBuckets)){
183      // Figure out the next link, remove NodeInBucket from the old link.
184      Probe = NodeInBucket->getNextInBucket();
185      NodeInBucket->SetNextInBucket(0);
186
187      // Insert the node into the new bucket, after recomputing the hash.
188      NodeID ID;
189      GetNodeProfile(ID, NodeInBucket);
190      InsertNode(NodeInBucket, GetBucketFor(ID, Buckets, NumBuckets));
191    }
192  }
193
194  delete[] OldBuckets;
195}
196
197/// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
198/// return it.  If not, return the insertion token that will make insertion
199/// faster.
200FoldingSetImpl::Node *FoldingSetImpl::FindNodeOrInsertPos(const NodeID &ID,
201                                                          void *&InsertPos) {
202  void **Bucket = GetBucketFor(ID, Buckets, NumBuckets);
203  void *Probe = *Bucket;
204
205  InsertPos = 0;
206
207  while (Node *NodeInBucket = GetNextPtr(Probe, Buckets, NumBuckets)) {
208    NodeID OtherID;
209    GetNodeProfile(OtherID, NodeInBucket);
210    if (OtherID == ID)
211      return NodeInBucket;
212
213    Probe = NodeInBucket->getNextInBucket();
214  }
215
216  // Didn't find the node, return null with the bucket as the InsertPos.
217  InsertPos = Bucket;
218  return 0;
219}
220
221/// InsertNode - Insert the specified node into the folding set, knowing that it
222/// is not already in the map.  InsertPos must be obtained from
223/// FindNodeOrInsertPos.
224void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
225  ++NumNodes;
226  // Do we need to grow the hashtable?
227  if (NumNodes > NumBuckets*2) {
228    GrowHashTable();
229    NodeID ID;
230    GetNodeProfile(ID, N);
231    InsertPos = GetBucketFor(ID, Buckets, NumBuckets);
232  }
233
234  /// The insert position is actually a bucket pointer.
235  void **Bucket = static_cast<void**>(InsertPos);
236
237  void *Next = *Bucket;
238
239  // If this is the first insertion into this bucket, its next pointer will be
240  // null.  Pretend as if it pointed to itself.
241  if (Next == 0)
242    Next = Bucket;
243
244  // Set the nodes next pointer, and make the bucket point to the node.
245  N->SetNextInBucket(Next);
246  *Bucket = N;
247}
248
249/// RemoveNode - Remove a node from the folding set, returning true if one was
250/// removed or false if the node was not in the folding set.
251bool FoldingSetImpl::RemoveNode(Node *N) {
252  // Because each bucket is a circular list, we don't need to compute N's hash
253  // to remove it.  Chase around the list until we find the node (or bucket)
254  // which points to N.
255  void *Ptr = N->getNextInBucket();
256  if (Ptr == 0) return false;  // Not in folding set.
257
258  --NumNodes;
259
260  void *NodeNextPtr = Ptr;
261  N->SetNextInBucket(0);
262  while (true) {
263    if (Node *NodeInBucket = GetNextPtr(Ptr, Buckets, NumBuckets)) {
264      // Advance pointer.
265      Ptr = NodeInBucket->getNextInBucket();
266
267      // We found a node that points to N, change it to point to N's next node,
268      // removing N from the list.
269      if (Ptr == N) {
270        NodeInBucket->SetNextInBucket(NodeNextPtr);
271        return true;
272      }
273    } else {
274      void **Bucket = GetBucketPtr(Ptr);
275      Ptr = *Bucket;
276
277      // If we found that the bucket points to N, update the bucket to point to
278      // whatever is next.
279      if (Ptr == N) {
280        *Bucket = NodeNextPtr;
281        return true;
282      }
283    }
284  }
285}
286
287/// GetOrInsertNode - If there is an existing simple Node exactly
288/// equal to the specified node, return it.  Otherwise, insert 'N' and it
289/// instead.
290FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
291  NodeID ID;
292  GetNodeProfile(ID, N);
293  void *IP;
294  if (Node *E = FindNodeOrInsertPos(ID, IP))
295    return E;
296  InsertNode(N, IP);
297  return N;
298}
299