FoldingSet.h revision dbac071050010effb95febae0ddf72102551323d
1//===-- llvm/ADT/FoldingSet.h - Uniquing Hash Set ---------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a hash set that can be used to remove duplication of nodes
11// in a graph.  This code was originally created by Chris Lattner for use with
12// SelectionDAGCSEMap, but was isolated to provide use across the llvm code set.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_FOLDINGSET_H
17#define LLVM_ADT_FOLDINGSET_H
18
19#include "llvm/System/DataTypes.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22
23namespace llvm {
24  class APFloat;
25  class APInt;
26  class BumpPtrAllocator;
27
28/// This folding set used for two purposes:
29///   1. Given information about a node we want to create, look up the unique
30///      instance of the node in the set.  If the node already exists, return
31///      it, otherwise return the bucket it should be inserted into.
32///   2. Given a node that has already been created, remove it from the set.
33///
34/// This class is implemented as a single-link chained hash table, where the
35/// "buckets" are actually the nodes themselves (the next pointer is in the
36/// node).  The last node points back to the bucket to simplify node removal.
37///
38/// Any node that is to be included in the folding set must be a subclass of
39/// FoldingSetNode.  The node class must also define a Profile method used to
40/// establish the unique bits of data for the node.  The Profile method is
41/// passed a FoldingSetNodeID object which is used to gather the bits.  Just
42/// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
43/// NOTE: That the folding set does not own the nodes and it is the
44/// responsibility of the user to dispose of the nodes.
45///
46/// Eg.
47///    class MyNode : public FoldingSetNode {
48///    private:
49///      std::string Name;
50///      unsigned Value;
51///    public:
52///      MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
53///       ...
54///      void Profile(FoldingSetNodeID &ID) const {
55///        ID.AddString(Name);
56///        ID.AddInteger(Value);
57///       }
58///       ...
59///     };
60///
61/// To define the folding set itself use the FoldingSet template;
62///
63/// Eg.
64///    FoldingSet<MyNode> MyFoldingSet;
65///
66/// Four public methods are available to manipulate the folding set;
67///
68/// 1) If you have an existing node that you want add to the set but unsure
69/// that the node might already exist then call;
70///
71///    MyNode *M = MyFoldingSet.GetOrInsertNode(N);
72///
73/// If The result is equal to the input then the node has been inserted.
74/// Otherwise, the result is the node existing in the folding set, and the
75/// input can be discarded (use the result instead.)
76///
77/// 2) If you are ready to construct a node but want to check if it already
78/// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
79/// check;
80///
81///   FoldingSetNodeID ID;
82///   ID.AddString(Name);
83///   ID.AddInteger(Value);
84///   void *InsertPoint;
85///
86///    MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
87///
88/// If found then M with be non-NULL, else InsertPoint will point to where it
89/// should be inserted using InsertNode.
90///
91/// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
92/// node with FindNodeOrInsertPos;
93///
94///    InsertNode(N, InsertPoint);
95///
96/// 4) Finally, if you want to remove a node from the folding set call;
97///
98///    bool WasRemoved = RemoveNode(N);
99///
100/// The result indicates whether the node existed in the folding set.
101
102class FoldingSetNodeID;
103
104//===----------------------------------------------------------------------===//
105/// FoldingSetImpl - Implements the folding set functionality.  The main
106/// structure is an array of buckets.  Each bucket is indexed by the hash of
107/// the nodes it contains.  The bucket itself points to the nodes contained
108/// in the bucket via a singly linked list.  The last node in the list points
109/// back to the bucket to facilitate node removal.
110///
111class FoldingSetImpl {
112protected:
113  /// Buckets - Array of bucket chains.
114  ///
115  void **Buckets;
116
117  /// NumBuckets - Length of the Buckets array.  Always a power of 2.
118  ///
119  unsigned NumBuckets;
120
121  /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
122  /// is greater than twice the number of buckets.
123  unsigned NumNodes;
124
125public:
126  explicit FoldingSetImpl(unsigned Log2InitSize = 6);
127  virtual ~FoldingSetImpl();
128
129  //===--------------------------------------------------------------------===//
130  /// Node - This class is used to maintain the singly linked bucket list in
131  /// a folding set.
132  ///
133  class Node {
134  private:
135    // NextInFoldingSetBucket - next link in the bucket list.
136    void *NextInFoldingSetBucket;
137
138  public:
139
140    Node() : NextInFoldingSetBucket(0) {}
141
142    // Accessors
143    void *getNextInBucket() const { return NextInFoldingSetBucket; }
144    void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
145  };
146
147  /// clear - Remove all nodes from the folding set.
148  void clear();
149
150  /// RemoveNode - Remove a node from the folding set, returning true if one
151  /// was removed or false if the node was not in the folding set.
152  bool RemoveNode(Node *N);
153
154  /// GetOrInsertNode - If there is an existing simple Node exactly
155  /// equal to the specified node, return it.  Otherwise, insert 'N' and return
156  /// it instead.
157  Node *GetOrInsertNode(Node *N);
158
159  /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
160  /// return it.  If not, return the insertion token that will make insertion
161  /// faster.
162  Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
163
164  /// InsertNode - Insert the specified node into the folding set, knowing that
165  /// it is not already in the folding set.  InsertPos must be obtained from
166  /// FindNodeOrInsertPos.
167  void InsertNode(Node *N, void *InsertPos);
168
169  /// InsertNode - Insert the specified node into the folding set, knowing that
170  /// it is not already in the folding set.
171  void InsertNode(Node *N) {
172    Node *Inserted = GetOrInsertNode(N);
173    (void)Inserted;
174    assert(Inserted == N && "Node already inserted!");
175  }
176
177  /// size - Returns the number of nodes in the folding set.
178  unsigned size() const { return NumNodes; }
179
180  /// empty - Returns true if there are no nodes in the folding set.
181  bool empty() const { return NumNodes == 0; }
182
183private:
184
185  /// GrowHashTable - Double the size of the hash table and rehash everything.
186  ///
187  void GrowHashTable();
188
189protected:
190
191  /// GetNodeProfile - Instantiations of the FoldingSet template implement
192  /// this function to gather data bits for the given node.
193  virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const = 0;
194};
195
196//===----------------------------------------------------------------------===//
197/// FoldingSetTrait - This trait class is used to define behavior of how
198///  to "profile" (in the FoldingSet parlance) an object of a given type.
199///  The default behavior is to invoke a 'Profile' method on an object, but
200///  through template specialization the behavior can be tailored for specific
201///  types.  Combined with the FoldingSetNodeWrapper class, one can add objects
202///  to FoldingSets that were not originally designed to have that behavior.
203///
204template<typename T> struct FoldingSetTrait {
205  static inline void Profile(const T& X, FoldingSetNodeID& ID) { X.Profile(ID);}
206  static inline void Profile(T& X, FoldingSetNodeID& ID) { X.Profile(ID); }
207  template <typename Ctx>
208  static inline void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
209    X.Profile(ID, Context);
210  }
211};
212
213//===--------------------------------------------------------------------===//
214/// FoldingSetNodeIDRef - This class describes a reference to an interned
215/// FoldingSetNodeID, which can be a useful to store node id data rather
216/// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
217/// is often much larger than necessary, and the possibility of heap
218/// allocation means it requires a non-trivial destructor call.
219class FoldingSetNodeIDRef {
220  unsigned* Data;
221  size_t Size;
222public:
223  FoldingSetNodeIDRef() : Data(0), Size(0) {}
224  FoldingSetNodeIDRef(unsigned *D, size_t S) : Data(D), Size(S) {}
225
226  unsigned *getData() const { return Data; }
227  size_t getSize() const { return Size; }
228};
229
230//===--------------------------------------------------------------------===//
231/// FoldingSetNodeID - This class is used to gather all the unique data bits of
232/// a node.  When all the bits are gathered this class is used to produce a
233/// hash value for the node.
234///
235class FoldingSetNodeID {
236  /// Bits - Vector of all the data bits that make the node unique.
237  /// Use a SmallVector to avoid a heap allocation in the common case.
238  SmallVector<unsigned, 32> Bits;
239
240public:
241  FoldingSetNodeID() {}
242
243  FoldingSetNodeID(FoldingSetNodeIDRef Ref)
244    : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
245
246  /// Add* - Add various data types to Bit data.
247  ///
248  void AddPointer(const void *Ptr);
249  void AddInteger(signed I);
250  void AddInteger(unsigned I);
251  void AddInteger(long I);
252  void AddInteger(unsigned long I);
253  void AddInteger(long long I);
254  void AddInteger(unsigned long long I);
255  void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
256  void AddString(StringRef String);
257
258  template <typename T>
259  inline void Add(const T& x) { FoldingSetTrait<T>::Profile(x, *this); }
260
261  /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
262  ///  object to be used to compute a new profile.
263  inline void clear() { Bits.clear(); }
264
265  /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
266  ///  to lookup the node in the FoldingSetImpl.
267  unsigned ComputeHash() const;
268
269  /// operator== - Used to compare two nodes to each other.
270  ///
271  bool operator==(const FoldingSetNodeID &RHS) const;
272
273  /// Intern - Copy this node's data to a memory region allocated from the
274  /// given allocator and return a FoldingSetNodeIDRef describing the
275  /// interned data.
276  FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
277};
278
279// Convenience type to hide the implementation of the folding set.
280typedef FoldingSetImpl::Node FoldingSetNode;
281template<class T> class FoldingSetIterator;
282template<class T> class FoldingSetBucketIterator;
283
284//===----------------------------------------------------------------------===//
285/// FoldingSet - This template class is used to instantiate a specialized
286/// implementation of the folding set to the node class T.  T must be a
287/// subclass of FoldingSetNode and implement a Profile function.
288///
289template<class T> class FoldingSet : public FoldingSetImpl {
290private:
291  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
292  /// way to convert nodes into a unique specifier.
293  virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const {
294    T *TN = static_cast<T *>(N);
295    FoldingSetTrait<T>::Profile(*TN,ID);
296  }
297
298public:
299  explicit FoldingSet(unsigned Log2InitSize = 6)
300  : FoldingSetImpl(Log2InitSize)
301  {}
302
303  typedef FoldingSetIterator<T> iterator;
304  iterator begin() { return iterator(Buckets); }
305  iterator end() { return iterator(Buckets+NumBuckets); }
306
307  typedef FoldingSetIterator<const T> const_iterator;
308  const_iterator begin() const { return const_iterator(Buckets); }
309  const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
310
311  typedef FoldingSetBucketIterator<T> bucket_iterator;
312
313  bucket_iterator bucket_begin(unsigned hash) {
314    return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
315  }
316
317  bucket_iterator bucket_end(unsigned hash) {
318    return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
319  }
320
321  /// GetOrInsertNode - If there is an existing simple Node exactly
322  /// equal to the specified node, return it.  Otherwise, insert 'N' and
323  /// return it instead.
324  T *GetOrInsertNode(Node *N) {
325    return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
326  }
327
328  /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
329  /// return it.  If not, return the insertion token that will make insertion
330  /// faster.
331  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
332    return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
333  }
334};
335
336//===----------------------------------------------------------------------===//
337/// ContextualFoldingSet - This template class is a further refinement
338/// of FoldingSet which provides a context argument when calling
339/// Profile on its nodes.  Currently, that argument is fixed at
340/// initialization time.
341///
342/// T must be a subclass of FoldingSetNode and implement a Profile
343/// function with signature
344///   void Profile(llvm::FoldingSetNodeID &, Ctx);
345template <class T, class Ctx>
346class ContextualFoldingSet : public FoldingSetImpl {
347  // Unfortunately, this can't derive from FoldingSet<T> because the
348  // construction vtable for FoldingSet<T> requires
349  // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
350  // requires a single-argument T::Profile().
351
352private:
353  Ctx Context;
354
355  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
356  /// way to convert nodes into a unique specifier.
357  virtual void GetNodeProfile(FoldingSetNodeID &ID,
358                              FoldingSetImpl::Node *N) const {
359    T *TN = static_cast<T *>(N);
360
361    // We must use explicit template arguments in case Ctx is a
362    // reference type.
363    FoldingSetTrait<T>::template Profile<Ctx>(*TN, ID, Context);
364  }
365
366public:
367  explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
368  : FoldingSetImpl(Log2InitSize), Context(Context)
369  {}
370
371  Ctx getContext() const { return Context; }
372
373
374  typedef FoldingSetIterator<T> iterator;
375  iterator begin() { return iterator(Buckets); }
376  iterator end() { return iterator(Buckets+NumBuckets); }
377
378  typedef FoldingSetIterator<const T> const_iterator;
379  const_iterator begin() const { return const_iterator(Buckets); }
380  const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
381
382  typedef FoldingSetBucketIterator<T> bucket_iterator;
383
384  bucket_iterator bucket_begin(unsigned hash) {
385    return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
386  }
387
388  bucket_iterator bucket_end(unsigned hash) {
389    return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
390  }
391
392  /// GetOrInsertNode - If there is an existing simple Node exactly
393  /// equal to the specified node, return it.  Otherwise, insert 'N'
394  /// and return it instead.
395  T *GetOrInsertNode(Node *N) {
396    return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
397  }
398
399  /// FindNodeOrInsertPos - Look up the node specified by ID.  If it
400  /// exists, return it.  If not, return the insertion token that will
401  /// make insertion faster.
402  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
403    return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
404  }
405};
406
407//===----------------------------------------------------------------------===//
408/// FoldingSetIteratorImpl - This is the common iterator support shared by all
409/// folding sets, which knows how to walk the folding set hash table.
410class FoldingSetIteratorImpl {
411protected:
412  FoldingSetNode *NodePtr;
413  FoldingSetIteratorImpl(void **Bucket);
414  void advance();
415
416public:
417  bool operator==(const FoldingSetIteratorImpl &RHS) const {
418    return NodePtr == RHS.NodePtr;
419  }
420  bool operator!=(const FoldingSetIteratorImpl &RHS) const {
421    return NodePtr != RHS.NodePtr;
422  }
423};
424
425
426template<class T>
427class FoldingSetIterator : public FoldingSetIteratorImpl {
428public:
429  explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
430
431  T &operator*() const {
432    return *static_cast<T*>(NodePtr);
433  }
434
435  T *operator->() const {
436    return static_cast<T*>(NodePtr);
437  }
438
439  inline FoldingSetIterator& operator++() {          // Preincrement
440    advance();
441    return *this;
442  }
443  FoldingSetIterator operator++(int) {        // Postincrement
444    FoldingSetIterator tmp = *this; ++*this; return tmp;
445  }
446};
447
448//===----------------------------------------------------------------------===//
449/// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
450///  shared by all folding sets, which knows how to walk a particular bucket
451///  of a folding set hash table.
452
453class FoldingSetBucketIteratorImpl {
454protected:
455  void *Ptr;
456
457  explicit FoldingSetBucketIteratorImpl(void **Bucket);
458
459  FoldingSetBucketIteratorImpl(void **Bucket, bool)
460    : Ptr(Bucket) {}
461
462  void advance() {
463    void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
464    uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
465    Ptr = reinterpret_cast<void*>(x);
466  }
467
468public:
469  bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
470    return Ptr == RHS.Ptr;
471  }
472  bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
473    return Ptr != RHS.Ptr;
474  }
475};
476
477
478template<class T>
479class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
480public:
481  explicit FoldingSetBucketIterator(void **Bucket) :
482    FoldingSetBucketIteratorImpl(Bucket) {}
483
484  FoldingSetBucketIterator(void **Bucket, bool) :
485    FoldingSetBucketIteratorImpl(Bucket, true) {}
486
487  T& operator*() const { return *static_cast<T*>(Ptr); }
488  T* operator->() const { return static_cast<T*>(Ptr); }
489
490  inline FoldingSetBucketIterator& operator++() { // Preincrement
491    advance();
492    return *this;
493  }
494  FoldingSetBucketIterator operator++(int) {      // Postincrement
495    FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
496  }
497};
498
499//===----------------------------------------------------------------------===//
500/// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
501/// types in an enclosing object so that they can be inserted into FoldingSets.
502template <typename T>
503class FoldingSetNodeWrapper : public FoldingSetNode {
504  T data;
505public:
506  explicit FoldingSetNodeWrapper(const T& x) : data(x) {}
507  virtual ~FoldingSetNodeWrapper() {}
508
509  template<typename A1>
510  explicit FoldingSetNodeWrapper(const A1& a1)
511    : data(a1) {}
512
513  template <typename A1, typename A2>
514  explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2)
515    : data(a1,a2) {}
516
517  template <typename A1, typename A2, typename A3>
518  explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3)
519    : data(a1,a2,a3) {}
520
521  template <typename A1, typename A2, typename A3, typename A4>
522  explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
523                                 const A4& a4)
524    : data(a1,a2,a3,a4) {}
525
526  template <typename A1, typename A2, typename A3, typename A4, typename A5>
527  explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
528                                 const A4& a4, const A5& a5)
529  : data(a1,a2,a3,a4,a5) {}
530
531
532  void Profile(FoldingSetNodeID& ID) { FoldingSetTrait<T>::Profile(data, ID); }
533
534  T& getValue() { return data; }
535  const T& getValue() const { return data; }
536
537  operator T&() { return data; }
538  operator const T&() const { return data; }
539};
540
541//===----------------------------------------------------------------------===//
542/// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
543/// a FoldingSetNodeID value rather than requiring the node to recompute it
544/// each time it is needed. This trades space for speed (which can be
545/// significant if the ID is long), and it also permits nodes to drop
546/// information that would otherwise only be required for recomputing an ID.
547class FastFoldingSetNode : public FoldingSetNode {
548  FoldingSetNodeID FastID;
549protected:
550  explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
551public:
552  void Profile(FoldingSetNodeID& ID) { ID = FastID; }
553};
554
555//===----------------------------------------------------------------------===//
556// Partial specializations of FoldingSetTrait.
557
558template<typename T> struct FoldingSetTrait<T*> {
559  static inline void Profile(const T* X, FoldingSetNodeID& ID) {
560    ID.AddPointer(X);
561  }
562  static inline void Profile(T* X, FoldingSetNodeID& ID) {
563    ID.AddPointer(X);
564  }
565};
566
567template<typename T> struct FoldingSetTrait<const T*> {
568  static inline void Profile(const T* X, FoldingSetNodeID& ID) {
569    ID.AddPointer(X);
570  }
571};
572
573} // End of namespace llvm.
574
575#endif
576