FoldingSet.h revision 7ed47a13356daed2a34cd2209a31f92552e3bdd8
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/Support/DataTypes.h"
20#include "llvm/ADT/SmallVector.h"
21#include <string>
22
23namespace llvm {
24  class APFloat;
25
26/// This folding set used for two purposes:
27///   1. Given information about a node we want to create, look up the unique
28///      instance of the node in the set.  If the node already exists, return
29///      it, otherwise return the bucket it should be inserted into.
30///   2. Given a node that has already been created, remove it from the set.
31///
32/// This class is implemented as a single-link chained hash table, where the
33/// "buckets" are actually the nodes themselves (the next pointer is in the
34/// node).  The last node points back to the bucket to simplified node removal.
35///
36/// Any node that is to be included in the folding set must be a subclass of
37/// FoldingSetNode.  The node class must also define a Profile method used to
38/// establish the unique bits of data for the node.  The Profile method is
39/// passed a FoldingSetNodeID object which is used to gather the bits.  Just
40/// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
41/// NOTE: That the folding set does not own the nodes and it is the
42/// responsibility of the user to dispose of the nodes.
43///
44/// Eg.
45///    class MyNode : public FoldingSetNode {
46///    private:
47///      std::string Name;
48///      unsigned Value;
49///    public:
50///      MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
51///       ...
52///      void Profile(FoldingSetNodeID &ID) {
53///        ID.AddString(Name);
54///        ID.AddInteger(Value);
55///       }
56///       ...
57///     };
58///
59/// To define the folding set itself use the FoldingSet template;
60///
61/// Eg.
62///    FoldingSet<MyNode> MyFoldingSet;
63///
64/// Four public methods are available to manipulate the folding set;
65///
66/// 1) If you have an existing node that you want add to the set but unsure
67/// that the node might already exist then call;
68///
69///    MyNode *M = MyFoldingSet.GetOrInsertNode(N);
70///
71/// If The result is equal to the input then the node has been inserted.
72/// Otherwise, the result is the node existing in the folding set, and the
73/// input can be discarded (use the result instead.)
74///
75/// 2) If you are ready to construct a node but want to check if it already
76/// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
77/// check;
78///
79///   FoldingSetNodeID ID;
80///   ID.AddString(Name);
81///   ID.AddInteger(Value);
82///   void *InsertPoint;
83///
84///    MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
85///
86/// If found then M with be non-NULL, else InsertPoint will point to where it
87/// should be inserted using InsertNode.
88///
89/// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
90/// node with FindNodeOrInsertPos;
91///
92///    InsertNode(N, InsertPoint);
93///
94/// 4) Finally, if you want to remove a node from the folding set call;
95///
96///    bool WasRemoved = RemoveNode(N);
97///
98/// The result indicates whether the node existed in the folding set.
99
100
101//===----------------------------------------------------------------------===//
102/// FoldingSetImpl - Implements the folding set functionality.  The main
103/// structure is an array of buckets.  Each bucket is indexed by the hash of
104/// the nodes it contains.  The bucket itself points to the nodes contained
105/// in the bucket via a singly linked list.  The last node in the list points
106/// back to the bucket to facilitate node removal.
107///
108class FoldingSetImpl {
109protected:
110  /// Buckets - Array of bucket chains.
111  ///
112  void **Buckets;
113
114  /// NumBuckets - Length of the Buckets array.  Always a power of 2.
115  ///
116  unsigned NumBuckets;
117
118  /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
119  /// is greater than twice the number of buckets.
120  unsigned NumNodes;
121
122public:
123  explicit FoldingSetImpl(unsigned Log2InitSize = 6);
124  virtual ~FoldingSetImpl();
125
126  // Forward declaration.
127  class Node;
128
129  //===--------------------------------------------------------------------===//
130  /// NodeID - This class is used to gather all the unique data bits of a
131  /// node.  When all the bits are gathered this class is used to produce a
132  /// hash value for the node.
133  ///
134  class NodeID {
135    /// Bits - Vector of all the data bits that make the node unique.
136    /// Use a SmallVector to avoid a heap allocation in the common case.
137    SmallVector<unsigned, 32> Bits;
138
139  public:
140    NodeID() {}
141
142    /// getRawData - Return the ith entry in the Bits data.
143    ///
144    unsigned getRawData(unsigned i) const {
145      return Bits[i];
146    }
147
148    /// Add* - Add various data types to Bit data.
149    ///
150    void AddPointer(const void *Ptr);
151    void AddInteger(signed I);
152    void AddInteger(unsigned I);
153    void AddInteger(int64_t I);
154    void AddInteger(uint64_t I);
155    void AddFloat(float F);
156    void AddDouble(double D);
157    void AddAPFloat(const APFloat& apf);
158    void AddString(const std::string &String);
159
160    /// ComputeHash - Compute a strong hash value for this NodeID, used to
161    /// lookup the node in the FoldingSetImpl.
162    unsigned ComputeHash() const;
163
164    /// operator== - Used to compare two nodes to each other.
165    ///
166    bool operator==(const NodeID &RHS) const;
167  };
168
169  //===--------------------------------------------------------------------===//
170  /// Node - This class is used to maintain the singly linked bucket list in
171  /// a folding set.
172  ///
173  class Node {
174  private:
175    // NextInFoldingSetBucket - next link in the bucket list.
176    void *NextInFoldingSetBucket;
177
178  public:
179
180    Node() : NextInFoldingSetBucket(0) {}
181
182    // Accessors
183    void *getNextInBucket() const { return NextInFoldingSetBucket; }
184    void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
185  };
186
187  /// RemoveNode - Remove a node from the folding set, returning true if one
188  /// was removed or false if the node was not in the folding set.
189  bool RemoveNode(Node *N);
190
191  /// GetOrInsertNode - If there is an existing simple Node exactly
192  /// equal to the specified node, return it.  Otherwise, insert 'N' and return
193  /// it instead.
194  Node *GetOrInsertNode(Node *N);
195
196  /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
197  /// return it.  If not, return the insertion token that will make insertion
198  /// faster.
199  Node *FindNodeOrInsertPos(const NodeID &ID, void *&InsertPos);
200
201  /// InsertNode - Insert the specified node into the folding set, knowing that
202  /// it is not already in the folding set.  InsertPos must be obtained from
203  /// FindNodeOrInsertPos.
204  void InsertNode(Node *N, void *InsertPos);
205
206  /// size - Returns the number of nodes in the folding set.
207  unsigned size() const { return NumNodes; }
208
209private:
210
211  /// GrowHashTable - Double the size of the hash table and rehash everything.
212  ///
213  void GrowHashTable();
214
215protected:
216
217  /// GetNodeProfile - Instantiations of the FoldingSet template implement
218  /// this function to gather data bits for the given node.
219  virtual void GetNodeProfile(NodeID &ID, Node *N) const = 0;
220};
221
222// Convenience types to hide the implementation of the folding set.
223typedef FoldingSetImpl::Node FoldingSetNode;
224typedef FoldingSetImpl::NodeID FoldingSetNodeID;
225
226template<class T> class FoldingSetIterator;
227
228//===----------------------------------------------------------------------===//
229/// FoldingSet - This template class is used to instantiate a specialized
230/// implementation of the folding set to the node class T.  T must be a
231/// subclass of FoldingSetNode and implement a Profile function.
232///
233template<class T> class FoldingSet : public FoldingSetImpl {
234private:
235  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
236  /// way to convert nodes into a unique specifier.
237  virtual void GetNodeProfile(NodeID &ID, Node *N) const {
238    T *TN = static_cast<T *>(N);
239    TN->Profile(ID);
240  }
241
242public:
243  explicit FoldingSet(unsigned Log2InitSize = 6)
244  : FoldingSetImpl(Log2InitSize)
245  {}
246
247  typedef FoldingSetIterator<T> iterator;
248  iterator begin() { return iterator(Buckets); }
249  iterator end() { return iterator(Buckets+NumBuckets); }
250
251  typedef FoldingSetIterator<const T> const_iterator;
252  const_iterator begin() const { return const_iterator(Buckets); }
253  const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
254
255  /// GetOrInsertNode - If there is an existing simple Node exactly
256  /// equal to the specified node, return it.  Otherwise, insert 'N' and
257  /// return it instead.
258  T *GetOrInsertNode(Node *N) {
259    return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
260  }
261
262  /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
263  /// return it.  If not, return the insertion token that will make insertion
264  /// faster.
265  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
266    return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
267  }
268};
269
270//===----------------------------------------------------------------------===//
271/// FoldingSetIteratorImpl - This is the common iterator support shared by all
272/// folding sets, which knows how to walk the folding set hash table.
273class FoldingSetIteratorImpl {
274protected:
275  FoldingSetNode *NodePtr;
276  FoldingSetIteratorImpl(void **Bucket);
277  void advance();
278
279public:
280  bool operator==(const FoldingSetIteratorImpl &RHS) const {
281    return NodePtr == RHS.NodePtr;
282  }
283  bool operator!=(const FoldingSetIteratorImpl &RHS) const {
284    return NodePtr != RHS.NodePtr;
285  }
286};
287
288
289template<class T>
290class FoldingSetIterator : public FoldingSetIteratorImpl {
291public:
292  FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
293
294  T &operator*() const {
295    return *static_cast<T*>(NodePtr);
296  }
297
298  T *operator->() const {
299    return static_cast<T*>(NodePtr);
300  }
301
302  inline FoldingSetIterator& operator++() {          // Preincrement
303    advance();
304    return *this;
305  }
306  FoldingSetIterator operator++(int) {        // Postincrement
307    FoldingSetIterator tmp = *this; ++*this; return tmp;
308  }
309};
310
311} // End of namespace llvm.
312
313
314#endif
315
316