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/Support/DataTypes.h"
20 #include "llvm/ADT/SmallVector.h"
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.
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.
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.
45 /// class MyNode : public FoldingSetNode {
50 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
52 /// void Profile(FoldingSetNodeID &ID) {
53 /// ID.AddString(Name);
54 /// ID.AddInteger(Value);
59 /// To define the folding set itself use the FoldingSet template;
62 /// FoldingSet<MyNode> MyFoldingSet;
64 /// Four public methods are available to manipulate the folding set;
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;
69 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
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.)
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
79 /// FoldingSetNodeID ID;
80 /// ID.AddString(Name);
81 /// ID.AddInteger(Value);
82 /// void *InsertPoint;
84 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
86 /// If found then M with be non-NULL, else InsertPoint will point to where it
87 /// should be inserted using InsertNode.
89 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
90 /// node with FindNodeOrInsertPos;
92 /// InsertNode(N, InsertPoint);
94 /// 4) Finally, if you want to remove a node from the folding set call;
96 /// bool WasRemoved = RemoveNode(N);
98 /// The result indicates whether the node existed in the folding set.
100 class FoldingSetNodeID;
102 //===----------------------------------------------------------------------===//
103 /// FoldingSetImpl - Implements the folding set functionality. The main
104 /// structure is an array of buckets. Each bucket is indexed by the hash of
105 /// the nodes it contains. The bucket itself points to the nodes contained
106 /// in the bucket via a singly linked list. The last node in the list points
107 /// back to the bucket to facilitate node removal.
109 class FoldingSetImpl {
111 /// Buckets - Array of bucket chains.
115 /// NumBuckets - Length of the Buckets array. Always a power of 2.
119 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
120 /// is greater than twice the number of buckets.
124 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
125 virtual ~FoldingSetImpl();
127 //===--------------------------------------------------------------------===//
128 /// Node - This class is used to maintain the singly linked bucket list in
133 // NextInFoldingSetBucket - next link in the bucket list.
134 void *NextInFoldingSetBucket;
138 Node() : NextInFoldingSetBucket(0) {}
141 void *getNextInBucket() const { return NextInFoldingSetBucket; }
142 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
145 /// RemoveNode - Remove a node from the folding set, returning true if one
146 /// was removed or false if the node was not in the folding set.
147 bool RemoveNode(Node *N);
149 /// GetOrInsertNode - If there is an existing simple Node exactly
150 /// equal to the specified node, return it. Otherwise, insert 'N' and return
152 Node *GetOrInsertNode(Node *N);
154 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
155 /// return it. If not, return the insertion token that will make insertion
157 Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
159 /// InsertNode - Insert the specified node into the folding set, knowing that
160 /// it is not already in the folding set. InsertPos must be obtained from
161 /// FindNodeOrInsertPos.
162 void InsertNode(Node *N, void *InsertPos);
164 /// size - Returns the number of nodes in the folding set.
165 unsigned size() const { return NumNodes; }
169 /// GrowHashTable - Double the size of the hash table and rehash everything.
171 void GrowHashTable();
175 /// GetNodeProfile - Instantiations of the FoldingSet template implement
176 /// this function to gather data bits for the given node.
177 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const = 0;
180 //===--------------------------------------------------------------------===//
181 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
182 /// a node. When all the bits are gathered this class is used to produce a
183 /// hash value for the node.
185 class FoldingSetNodeID {
186 /// Bits - Vector of all the data bits that make the node unique.
187 /// Use a SmallVector to avoid a heap allocation in the common case.
188 SmallVector<unsigned, 32> Bits;
191 FoldingSetNodeID() {}
193 /// getRawData - Return the ith entry in the Bits data.
195 unsigned getRawData(unsigned i) const {
199 /// Add* - Add various data types to Bit data.
201 void AddPointer(const void *Ptr);
202 void AddInteger(signed I);
203 void AddInteger(unsigned I);
204 void AddInteger(int64_t I);
205 void AddInteger(uint64_t I);
206 void AddFloat(float F);
207 void AddDouble(double D);
208 void AddAPFloat(const APFloat& apf);
209 void AddString(const std::string &String);
211 /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
212 /// object to be used to compute a new profile.
213 inline void clear() { Bits.clear(); }
215 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
216 /// to lookup the node in the FoldingSetImpl.
217 unsigned ComputeHash() const;
219 /// operator== - Used to compare two nodes to each other.
221 bool operator==(const FoldingSetNodeID &RHS) const;
224 // Convenience type to hide the implementation of the folding set.
225 typedef FoldingSetImpl::Node FoldingSetNode;
226 template<class T> class FoldingSetIterator;
227 template<class T> class FoldingSetBucketIterator;
229 //===----------------------------------------------------------------------===//
230 /// FoldingSetTrait - This trait class is used to define behavior of how
231 /// to "profile" (in the FoldingSet parlance) an object of a given type.
232 /// The default behavior is to invoke a 'Profile' method on an object, but
233 /// through template specialization the behavior can be tailored for specific
234 /// types. Combined with the FoldingSetNodeWrapper classs, one can add objects
235 /// to FoldingSets that were not originally designed to have that behavior.
237 template<typename T> struct FoldingSetTrait {
238 static inline void Profile(const T& X, FoldingSetNodeID& ID) { X.Profile(ID);}
239 static inline void Profile(T& X, FoldingSetNodeID& ID) { X.Profile(ID); }
242 //===----------------------------------------------------------------------===//
243 /// FoldingSet - This template class is used to instantiate a specialized
244 /// implementation of the folding set to the node class T. T must be a
245 /// subclass of FoldingSetNode and implement a Profile function.
247 template<class T> class FoldingSet : public FoldingSetImpl {
249 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
250 /// way to convert nodes into a unique specifier.
251 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const {
252 T *TN = static_cast<T *>(N);
253 FoldingSetTrait<T>::Profile(*TN,ID);
257 explicit FoldingSet(unsigned Log2InitSize = 6)
258 : FoldingSetImpl(Log2InitSize)
261 typedef FoldingSetIterator<T> iterator;
262 iterator begin() { return iterator(Buckets); }
263 iterator end() { return iterator(Buckets+NumBuckets); }
265 typedef FoldingSetIterator<const T> const_iterator;
266 const_iterator begin() const { return const_iterator(Buckets); }
267 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
269 typedef FoldingSetBucketIterator<T> bucket_iterator;
271 bucket_iterator bucket_begin(unsigned hash) {
272 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
275 bucket_iterator bucket_end(unsigned hash) {
276 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
279 /// GetOrInsertNode - If there is an existing simple Node exactly
280 /// equal to the specified node, return it. Otherwise, insert 'N' and
281 /// return it instead.
282 T *GetOrInsertNode(Node *N) {
283 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
286 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
287 /// return it. If not, return the insertion token that will make insertion
289 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
290 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
294 //===----------------------------------------------------------------------===//
295 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
296 /// folding sets, which knows how to walk the folding set hash table.
297 class FoldingSetIteratorImpl {
299 FoldingSetNode *NodePtr;
300 FoldingSetIteratorImpl(void **Bucket);
304 bool operator==(const FoldingSetIteratorImpl &RHS) const {
305 return NodePtr == RHS.NodePtr;
307 bool operator!=(const FoldingSetIteratorImpl &RHS) const {
308 return NodePtr != RHS.NodePtr;
314 class FoldingSetIterator : public FoldingSetIteratorImpl {
316 FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
318 T &operator*() const {
319 return *static_cast<T*>(NodePtr);
322 T *operator->() const {
323 return static_cast<T*>(NodePtr);
326 inline FoldingSetIterator& operator++() { // Preincrement
330 FoldingSetIterator operator++(int) { // Postincrement
331 FoldingSetIterator tmp = *this; ++*this; return tmp;
335 //===----------------------------------------------------------------------===//
336 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
337 /// shared by all folding sets, which knows how to walk a particular bucket
338 /// of a folding set hash table.
340 class FoldingSetBucketIteratorImpl {
344 FoldingSetBucketIteratorImpl(void **Bucket);
346 FoldingSetBucketIteratorImpl(void **Bucket, bool)
347 : Ptr(reinterpret_cast<void*>(Bucket)) {}
350 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
351 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
352 Ptr = reinterpret_cast<void*>(x);
356 bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
357 return Ptr == RHS.Ptr;
359 bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
360 return Ptr != RHS.Ptr;
366 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
368 FoldingSetBucketIterator(void **Bucket) :
369 FoldingSetBucketIteratorImpl(Bucket) {}
371 FoldingSetBucketIterator(void **Bucket, bool) :
372 FoldingSetBucketIteratorImpl(Bucket, true) {}
374 T& operator*() const { return *static_cast<T*>(Ptr); }
375 T* operator->() const { return static_cast<T*>(Ptr); }
377 inline FoldingSetBucketIterator& operator++() { // Preincrement
381 FoldingSetBucketIterator operator++(int) { // Postincrement
382 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
386 //===----------------------------------------------------------------------===//
387 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
388 /// types in an enclosing object so that they can be inserted into FoldingSets.
389 template <typename T>
390 class FoldingSetNodeWrapper : public FoldingSetNode {
393 FoldingSetNodeWrapper(const T& x) : data(x) {}
394 virtual ~FoldingSetNodeWrapper() {}
396 template<typename A1>
397 explicit FoldingSetNodeWrapper(const A1& a1)
400 template <typename A1, typename A2>
401 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2)
404 template <typename A1, typename A2, typename A3>
405 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3)
408 template <typename A1, typename A2, typename A3, typename A4>
409 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
411 : data(a1,a2,a3,a4) {}
413 template <typename A1, typename A2, typename A3, typename A4, typename A5>
414 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
415 const A4& a4, const A5& a5)
416 : data(a1,a2,a3,a4,a5) {}
419 void Profile(FoldingSetNodeID& ID) { FoldingSetTrait<T>::Profile(data, ID); }
421 T& getValue() { return data; }
422 const T& getValue() const { return data; }
424 operator T&() { return data; }
425 operator const T&() const { return data; }
428 } // End of namespace llvm.