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"
27 /// This folding set used for two purposes:
28 /// 1. Given information about a node we want to create, look up the unique
29 /// instance of the node in the set. If the node already exists, return
30 /// it, otherwise return the bucket it should be inserted into.
31 /// 2. Given a node that has already been created, remove it from the set.
33 /// This class is implemented as a single-link chained hash table, where the
34 /// "buckets" are actually the nodes themselves (the next pointer is in the
35 /// node). The last node points back to the bucket to simplified node removal.
37 /// Any node that is to be included in the folding set must be a subclass of
38 /// FoldingSetNode. The node class must also define a Profile method used to
39 /// establish the unique bits of data for the node. The Profile method is
40 /// passed a FoldingSetNodeID object which is used to gather the bits. Just
41 /// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
42 /// NOTE: That the folding set does not own the nodes and it is the
43 /// responsibility of the user to dispose of the nodes.
46 /// class MyNode : public FoldingSetNode {
51 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
53 /// void Profile(FoldingSetNodeID &ID) {
54 /// ID.AddString(Name);
55 /// ID.AddInteger(Value);
60 /// To define the folding set itself use the FoldingSet template;
63 /// FoldingSet<MyNode> MyFoldingSet;
65 /// Four public methods are available to manipulate the folding set;
67 /// 1) If you have an existing node that you want add to the set but unsure
68 /// that the node might already exist then call;
70 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
72 /// If The result is equal to the input then the node has been inserted.
73 /// Otherwise, the result is the node existing in the folding set, and the
74 /// input can be discarded (use the result instead.)
76 /// 2) If you are ready to construct a node but want to check if it already
77 /// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
80 /// FoldingSetNodeID ID;
81 /// ID.AddString(Name);
82 /// ID.AddInteger(Value);
83 /// void *InsertPoint;
85 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
87 /// If found then M with be non-NULL, else InsertPoint will point to where it
88 /// should be inserted using InsertNode.
90 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
91 /// node with FindNodeOrInsertPos;
93 /// InsertNode(N, InsertPoint);
95 /// 4) Finally, if you want to remove a node from the folding set call;
97 /// bool WasRemoved = RemoveNode(N);
99 /// The result indicates whether the node existed in the folding set.
101 class FoldingSetNodeID;
103 //===----------------------------------------------------------------------===//
104 /// FoldingSetImpl - Implements the folding set functionality. The main
105 /// structure is an array of buckets. Each bucket is indexed by the hash of
106 /// the nodes it contains. The bucket itself points to the nodes contained
107 /// in the bucket via a singly linked list. The last node in the list points
108 /// back to the bucket to facilitate node removal.
110 class FoldingSetImpl {
112 /// Buckets - Array of bucket chains.
116 /// NumBuckets - Length of the Buckets array. Always a power of 2.
120 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
121 /// is greater than twice the number of buckets.
125 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
126 virtual ~FoldingSetImpl();
128 //===--------------------------------------------------------------------===//
129 /// Node - This class is used to maintain the singly linked bucket list in
134 // NextInFoldingSetBucket - next link in the bucket list.
135 void *NextInFoldingSetBucket;
139 Node() : NextInFoldingSetBucket(0) {}
142 void *getNextInBucket() const { return NextInFoldingSetBucket; }
143 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
146 /// RemoveNode - Remove a node from the folding set, returning true if one
147 /// was removed or false if the node was not in the folding set.
148 bool RemoveNode(Node *N);
150 /// GetOrInsertNode - If there is an existing simple Node exactly
151 /// equal to the specified node, return it. Otherwise, insert 'N' and return
153 Node *GetOrInsertNode(Node *N);
155 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
156 /// return it. If not, return the insertion token that will make insertion
158 Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
160 /// InsertNode - Insert the specified node into the folding set, knowing that
161 /// it is not already in the folding set. InsertPos must be obtained from
162 /// FindNodeOrInsertPos.
163 void InsertNode(Node *N, void *InsertPos);
165 /// size - Returns the number of nodes in the folding set.
166 unsigned size() const { return NumNodes; }
170 /// GrowHashTable - Double the size of the hash table and rehash everything.
172 void GrowHashTable();
176 /// GetNodeProfile - Instantiations of the FoldingSet template implement
177 /// this function to gather data bits for the given node.
178 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const = 0;
181 //===--------------------------------------------------------------------===//
182 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
183 /// a node. When all the bits are gathered this class is used to produce a
184 /// hash value for the node.
186 class FoldingSetNodeID {
187 /// Bits - Vector of all the data bits that make the node unique.
188 /// Use a SmallVector to avoid a heap allocation in the common case.
189 SmallVector<unsigned, 32> Bits;
192 FoldingSetNodeID() {}
194 /// getRawData - Return the ith entry in the Bits data.
196 unsigned getRawData(unsigned i) const {
200 /// Add* - Add various data types to Bit data.
202 void AddPointer(const void *Ptr);
203 void AddInteger(signed I);
204 void AddInteger(unsigned I);
205 void AddInteger(int64_t I);
206 void AddInteger(uint64_t I);
207 void AddFloat(float F);
208 void AddDouble(double D);
209 void AddAPFloat(const APFloat& apf);
210 void AddAPInt(const APInt& api);
211 void AddString(const std::string &String);
213 /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
214 /// object to be used to compute a new profile.
215 inline void clear() { Bits.clear(); }
217 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
218 /// to lookup the node in the FoldingSetImpl.
219 unsigned ComputeHash() const;
221 /// operator== - Used to compare two nodes to each other.
223 bool operator==(const FoldingSetNodeID &RHS) const;
226 // Convenience type to hide the implementation of the folding set.
227 typedef FoldingSetImpl::Node FoldingSetNode;
228 template<class T> class FoldingSetIterator;
229 template<class T> class FoldingSetBucketIterator;
231 //===----------------------------------------------------------------------===//
232 /// FoldingSetTrait - This trait class is used to define behavior of how
233 /// to "profile" (in the FoldingSet parlance) an object of a given type.
234 /// The default behavior is to invoke a 'Profile' method on an object, but
235 /// through template specialization the behavior can be tailored for specific
236 /// types. Combined with the FoldingSetNodeWrapper classs, one can add objects
237 /// to FoldingSets that were not originally designed to have that behavior.
239 template<typename T> struct FoldingSetTrait {
240 static inline void Profile(const T& X, FoldingSetNodeID& ID) { X.Profile(ID);}
241 static inline void Profile(T& X, FoldingSetNodeID& ID) { X.Profile(ID); }
244 //===----------------------------------------------------------------------===//
245 /// FoldingSet - This template class is used to instantiate a specialized
246 /// implementation of the folding set to the node class T. T must be a
247 /// subclass of FoldingSetNode and implement a Profile function.
249 template<class T> class FoldingSet : public FoldingSetImpl {
251 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
252 /// way to convert nodes into a unique specifier.
253 virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const {
254 T *TN = static_cast<T *>(N);
255 FoldingSetTrait<T>::Profile(*TN,ID);
259 explicit FoldingSet(unsigned Log2InitSize = 6)
260 : FoldingSetImpl(Log2InitSize)
263 typedef FoldingSetIterator<T> iterator;
264 iterator begin() { return iterator(Buckets); }
265 iterator end() { return iterator(Buckets+NumBuckets); }
267 typedef FoldingSetIterator<const T> const_iterator;
268 const_iterator begin() const { return const_iterator(Buckets); }
269 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
271 typedef FoldingSetBucketIterator<T> bucket_iterator;
273 bucket_iterator bucket_begin(unsigned hash) {
274 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
277 bucket_iterator bucket_end(unsigned hash) {
278 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
281 /// GetOrInsertNode - If there is an existing simple Node exactly
282 /// equal to the specified node, return it. Otherwise, insert 'N' and
283 /// return it instead.
284 T *GetOrInsertNode(Node *N) {
285 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
288 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
289 /// return it. If not, return the insertion token that will make insertion
291 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
292 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
296 //===----------------------------------------------------------------------===//
297 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
298 /// folding sets, which knows how to walk the folding set hash table.
299 class FoldingSetIteratorImpl {
301 FoldingSetNode *NodePtr;
302 FoldingSetIteratorImpl(void **Bucket);
306 bool operator==(const FoldingSetIteratorImpl &RHS) const {
307 return NodePtr == RHS.NodePtr;
309 bool operator!=(const FoldingSetIteratorImpl &RHS) const {
310 return NodePtr != RHS.NodePtr;
316 class FoldingSetIterator : public FoldingSetIteratorImpl {
318 FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
320 T &operator*() const {
321 return *static_cast<T*>(NodePtr);
324 T *operator->() const {
325 return static_cast<T*>(NodePtr);
328 inline FoldingSetIterator& operator++() { // Preincrement
332 FoldingSetIterator operator++(int) { // Postincrement
333 FoldingSetIterator tmp = *this; ++*this; return tmp;
337 //===----------------------------------------------------------------------===//
338 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
339 /// shared by all folding sets, which knows how to walk a particular bucket
340 /// of a folding set hash table.
342 class FoldingSetBucketIteratorImpl {
346 FoldingSetBucketIteratorImpl(void **Bucket);
348 FoldingSetBucketIteratorImpl(void **Bucket, bool)
349 : Ptr(reinterpret_cast<void*>(Bucket)) {}
352 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
353 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
354 Ptr = reinterpret_cast<void*>(x);
358 bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
359 return Ptr == RHS.Ptr;
361 bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
362 return Ptr != RHS.Ptr;
368 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
370 FoldingSetBucketIterator(void **Bucket) :
371 FoldingSetBucketIteratorImpl(Bucket) {}
373 FoldingSetBucketIterator(void **Bucket, bool) :
374 FoldingSetBucketIteratorImpl(Bucket, true) {}
376 T& operator*() const { return *static_cast<T*>(Ptr); }
377 T* operator->() const { return static_cast<T*>(Ptr); }
379 inline FoldingSetBucketIterator& operator++() { // Preincrement
383 FoldingSetBucketIterator operator++(int) { // Postincrement
384 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
388 //===----------------------------------------------------------------------===//
389 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
390 /// types in an enclosing object so that they can be inserted into FoldingSets.
391 template <typename T>
392 class FoldingSetNodeWrapper : public FoldingSetNode {
395 FoldingSetNodeWrapper(const T& x) : data(x) {}
396 virtual ~FoldingSetNodeWrapper() {}
398 template<typename A1>
399 explicit FoldingSetNodeWrapper(const A1& a1)
402 template <typename A1, typename A2>
403 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2)
406 template <typename A1, typename A2, typename A3>
407 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3)
410 template <typename A1, typename A2, typename A3, typename A4>
411 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
413 : data(a1,a2,a3,a4) {}
415 template <typename A1, typename A2, typename A3, typename A4, typename A5>
416 explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
417 const A4& a4, const A5& a5)
418 : data(a1,a2,a3,a4,a5) {}
421 void Profile(FoldingSetNodeID& ID) { FoldingSetTrait<T>::Profile(data, ID); }
423 T& getValue() { return data; }
424 const T& getValue() const { return data; }
426 operator T&() { return data; }
427 operator const T&() const { return data; }
430 } // End of namespace llvm.