1 //===-- llvm/ADT/FoldingSet.h - Uniquing Hash Set ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_FOLDINGSET_H
17 #define LLVM_ADT_FOLDINGSET_H
19 #include "llvm/System/DataTypes.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
26 class BumpPtrAllocator;
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.
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.
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.
47 /// class MyNode : public FoldingSetNode {
52 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
54 /// void Profile(FoldingSetNodeID &ID) const {
55 /// ID.AddString(Name);
56 /// ID.AddInteger(Value);
61 /// To define the folding set itself use the FoldingSet template;
64 /// FoldingSet<MyNode> MyFoldingSet;
66 /// Four public methods are available to manipulate the folding set;
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;
71 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
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.)
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
81 /// FoldingSetNodeID ID;
82 /// ID.AddString(Name);
83 /// ID.AddInteger(Value);
84 /// void *InsertPoint;
86 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
88 /// If found then M with be non-NULL, else InsertPoint will point to where it
89 /// should be inserted using InsertNode.
91 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
92 /// node with FindNodeOrInsertPos;
94 /// InsertNode(N, InsertPoint);
96 /// 4) Finally, if you want to remove a node from the folding set call;
98 /// bool WasRemoved = RemoveNode(N);
100 /// The result indicates whether the node existed in the folding set.
102 class FoldingSetNodeID;
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.
111 class FoldingSetImpl {
113 /// Buckets - Array of bucket chains.
117 /// NumBuckets - Length of the Buckets array. Always a power of 2.
121 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
122 /// is greater than twice the number of buckets.
126 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
127 virtual ~FoldingSetImpl();
129 //===--------------------------------------------------------------------===//
130 /// Node - This class is used to maintain the singly linked bucket list in
135 // NextInFoldingSetBucket - next link in the bucket list.
136 void *NextInFoldingSetBucket;
140 Node() : NextInFoldingSetBucket(0) {}
143 void *getNextInBucket() const { return NextInFoldingSetBucket; }
144 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
147 /// clear - Remove all nodes from the folding set.
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);
154 /// GetOrInsertNode - If there is an existing simple Node exactly
155 /// equal to the specified node, return it. Otherwise, insert 'N' and return
157 Node *GetOrInsertNode(Node *N);
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
162 Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
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);
169 /// size - Returns the number of nodes in the folding set.
170 unsigned size() const { return NumNodes; }
172 /// empty - Returns true if there are no nodes in the folding set.
173 bool empty() const { return NumNodes == 0; }
177 /// GrowHashTable - Double the size of the hash table and rehash everything.
179 void GrowHashTable();
183 /// GetNodeProfile - Instantiations of the FoldingSet template implement
184 /// this function to gather data bits for the given node.
185 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const = 0;
188 //===----------------------------------------------------------------------===//
189 /// FoldingSetTrait - This trait class is used to define behavior of how
190 /// to "profile" (in the FoldingSet parlance) an object of a given type.
191 /// The default behavior is to invoke a 'Profile' method on an object, but
192 /// through template specialization the behavior can be tailored for specific
193 /// types. Combined with the FoldingSetNodeWrapper classs, one can add objects
194 /// to FoldingSets that were not originally designed to have that behavior.
196 template<typename T> struct FoldingSetTrait {
197 static inline void Profile(const T& X, FoldingSetNodeID& ID) { X.Profile(ID);}
198 static inline void Profile(T& X, FoldingSetNodeID& ID) { X.Profile(ID); }
201 //===--------------------------------------------------------------------===//
202 /// FoldingSetNodeIDRef - This class describes a reference to an interned
203 /// FoldingSetNodeID, which can be a useful to store node id data rather
204 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
205 /// is often much larger than necessary, and the possibility of heap
206 /// allocation means it requires a non-trivial destructor call.
207 class FoldingSetNodeIDRef {
211 FoldingSetNodeIDRef() : Data(0), Size(0) {}
212 FoldingSetNodeIDRef(unsigned *D, size_t S) : Data(D), Size(S) {}
214 unsigned *getData() const { return Data; }
215 size_t getSize() const { return Size; }
218 //===--------------------------------------------------------------------===//
219 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
220 /// a node. When all the bits are gathered this class is used to produce a
221 /// hash value for the node.
223 class FoldingSetNodeID {
224 /// Bits - Vector of all the data bits that make the node unique.
225 /// Use a SmallVector to avoid a heap allocation in the common case.
226 SmallVector<unsigned, 32> Bits;
229 FoldingSetNodeID() {}
231 FoldingSetNodeID(FoldingSetNodeIDRef Ref)
232 : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
234 /// Add* - Add various data types to Bit data.
236 void AddPointer(const void *Ptr);
237 void AddInteger(signed I);
238 void AddInteger(unsigned I);
239 void AddInteger(long I);
240 void AddInteger(unsigned long I);
241 void AddInteger(long long I);
242 void AddInteger(unsigned long long I);
243 void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
244 void AddString(StringRef String);
246 template <typename T>
247 inline void Add(const T& x) { FoldingSetTrait<T>::Profile(x, *this); }
249 /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
250 /// object to be used to compute a new profile.
251 inline void clear() { Bits.clear(); }
253 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
254 /// to lookup the node in the FoldingSetImpl.
255 unsigned ComputeHash() const;
257 /// operator== - Used to compare two nodes to each other.
259 bool operator==(const FoldingSetNodeID &RHS) const;
261 /// Intern - Copy this node's data to a memory region allocated from the
262 /// given allocator and return a FoldingSetNodeIDRef describing the
264 FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
267 // Convenience type to hide the implementation of the folding set.
268 typedef FoldingSetImpl::Node FoldingSetNode;
269 template<class T> class FoldingSetIterator;
270 template<class T> class FoldingSetBucketIterator;
272 //===----------------------------------------------------------------------===//
273 /// FoldingSet - This template class is used to instantiate a specialized
274 /// implementation of the folding set to the node class T. T must be a
275 /// subclass of FoldingSetNode and implement a Profile function.
277 template<class T> class FoldingSet : public FoldingSetImpl {
279 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
280 /// way to convert nodes into a unique specifier.
281 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const {
282 T *TN = static_cast<T *>(N);
283 FoldingSetTrait<T>::Profile(*TN,ID);
287 explicit FoldingSet(unsigned Log2InitSize = 6)
288 : FoldingSetImpl(Log2InitSize)
291 typedef FoldingSetIterator<T> iterator;
292 iterator begin() { return iterator(Buckets); }
293 iterator end() { return iterator(Buckets+NumBuckets); }
295 typedef FoldingSetIterator<const T> const_iterator;
296 const_iterator begin() const { return const_iterator(Buckets); }
297 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
299 typedef FoldingSetBucketIterator<T> bucket_iterator;
301 bucket_iterator bucket_begin(unsigned hash) {
302 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
305 bucket_iterator bucket_end(unsigned hash) {
306 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
309 /// GetOrInsertNode - If there is an existing simple Node exactly
310 /// equal to the specified node, return it. Otherwise, insert 'N' and
311 /// return it instead.
312 T *GetOrInsertNode(Node *N) {
313 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
316 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
317 /// return it. If not, return the insertion token that will make insertion
319 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
320 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
324 //===----------------------------------------------------------------------===//
325 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
326 /// folding sets, which knows how to walk the folding set hash table.
327 class FoldingSetIteratorImpl {
329 FoldingSetNode *NodePtr;
330 FoldingSetIteratorImpl(void **Bucket);
334 bool operator==(const FoldingSetIteratorImpl &RHS) const {
335 return NodePtr == RHS.NodePtr;
337 bool operator!=(const FoldingSetIteratorImpl &RHS) const {
338 return NodePtr != RHS.NodePtr;
344 class FoldingSetIterator : public FoldingSetIteratorImpl {
346 explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
348 T &operator*() const {
349 return *static_cast<T*>(NodePtr);
352 T *operator->() const {
353 return static_cast<T*>(NodePtr);
356 inline FoldingSetIterator& operator++() { // Preincrement
360 FoldingSetIterator operator++(int) { // Postincrement
361 FoldingSetIterator tmp = *this; ++*this; return tmp;
365 //===----------------------------------------------------------------------===//
366 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
367 /// shared by all folding sets, which knows how to walk a particular bucket
368 /// of a folding set hash table.
370 class FoldingSetBucketIteratorImpl {
374 explicit FoldingSetBucketIteratorImpl(void **Bucket);
376 FoldingSetBucketIteratorImpl(void **Bucket, bool)
380 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
381 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
382 Ptr = reinterpret_cast<void*>(x);
386 bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
387 return Ptr == RHS.Ptr;
389 bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
390 return Ptr != RHS.Ptr;
396 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
398 explicit FoldingSetBucketIterator(void **Bucket) :
399 FoldingSetBucketIteratorImpl(Bucket) {}
401 FoldingSetBucketIterator(void **Bucket, bool) :
402 FoldingSetBucketIteratorImpl(Bucket, true) {}
404 T& operator*() const { return *static_cast<T*>(Ptr); }
405 T* operator->() const { return static_cast<T*>(Ptr); }
407 inline FoldingSetBucketIterator& operator++() { // Preincrement
411 FoldingSetBucketIterator operator++(int) { // Postincrement
412 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
416 //===----------------------------------------------------------------------===//
417 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
418 /// types in an enclosing object so that they can be inserted into FoldingSets.
419 template <typename T>
420 class FoldingSetNodeWrapper : public FoldingSetNode {
423 explicit FoldingSetNodeWrapper(const T& x) : data(x) {}
424 virtual ~FoldingSetNodeWrapper() {}
426 template<typename A1>
427 explicit FoldingSetNodeWrapper(const A1& a1)
430 template <typename A1, typename A2>
431 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2)
434 template <typename A1, typename A2, typename A3>
435 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3)
438 template <typename A1, typename A2, typename A3, typename A4>
439 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
441 : data(a1,a2,a3,a4) {}
443 template <typename A1, typename A2, typename A3, typename A4, typename A5>
444 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
445 const A4& a4, const A5& a5)
446 : data(a1,a2,a3,a4,a5) {}
449 void Profile(FoldingSetNodeID& ID) { FoldingSetTrait<T>::Profile(data, ID); }
451 T& getValue() { return data; }
452 const T& getValue() const { return data; }
454 operator T&() { return data; }
455 operator const T&() const { return data; }
458 //===----------------------------------------------------------------------===//
459 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
460 /// a FoldingSetNodeID value rather than requiring the node to recompute it
461 /// each time it is needed. This trades space for speed (which can be
462 /// significant if the ID is long), and it also permits nodes to drop
463 /// information that would otherwise only be required for recomputing an ID.
464 class FastFoldingSetNode : public FoldingSetNode {
465 FoldingSetNodeID FastID;
467 explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
469 void Profile(FoldingSetNodeID& ID) { ID = FastID; }
472 //===----------------------------------------------------------------------===//
473 // Partial specializations of FoldingSetTrait.
475 template<typename T> struct FoldingSetTrait<T*> {
476 static inline void Profile(const T* X, FoldingSetNodeID& ID) {
479 static inline void Profile(T* X, FoldingSetNodeID& ID) {
484 template<typename T> struct FoldingSetTrait<const T*> {
485 static inline void Profile(const T* X, FoldingSetNodeID& ID) {
490 } // End of namespace llvm.