Add another required #include for freestanding .h files.
[oota-llvm.git] / include / llvm / ADT / FoldingSet.h
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 "llvm/ADT/StringRef.h"
22 #include <iterator>
23
24 namespace llvm {
25   class APFloat;
26   class APInt;
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
102 class 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 ///
111 class FoldingSetImpl {
112 protected:
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
125 public:
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   /// size - Returns the number of nodes in the folding set.
170   unsigned size() const { return NumNodes; }
171
172   /// empty - Returns true if there are no nodes in the folding set.
173   bool empty() const { return NumNodes == 0; }
174
175 private:
176
177   /// GrowHashTable - Double the size of the hash table and rehash everything.
178   ///
179   void GrowHashTable();
180
181 protected:
182
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;
186 };
187
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.
195 ///
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); }
199 };
200
201 //===--------------------------------------------------------------------===//
202 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
203 /// a node.  When all the bits are gathered this class is used to produce a
204 /// hash value for the node.
205 ///
206 class FoldingSetNodeID {
207   /// Bits - Vector of all the data bits that make the node unique.
208   /// Use a SmallVector to avoid a heap allocation in the common case.
209   SmallVector<unsigned, 32> Bits;
210
211 public:
212   FoldingSetNodeID() {}
213
214   /// getRawData - Return the ith entry in the Bits data.
215   ///
216   unsigned getRawData(unsigned i) const {
217     return Bits[i];
218   }
219
220   /// Add* - Add various data types to Bit data.
221   ///
222   void AddPointer(const void *Ptr);
223   void AddInteger(signed I);
224   void AddInteger(unsigned I);
225   void AddInteger(long I);
226   void AddInteger(unsigned long I);
227   void AddInteger(long long I);
228   void AddInteger(unsigned long long I);
229   void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
230   void AddString(StringRef String);
231
232   template <typename T>
233   inline void Add(const T& x) { FoldingSetTrait<T>::Profile(x, *this); }
234
235   /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
236   ///  object to be used to compute a new profile.
237   inline void clear() { Bits.clear(); }
238
239   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
240   ///  to lookup the node in the FoldingSetImpl.
241   unsigned ComputeHash() const;
242
243   /// operator== - Used to compare two nodes to each other.
244   ///
245   bool operator==(const FoldingSetNodeID &RHS) const;
246 };
247
248 // Convenience type to hide the implementation of the folding set.
249 typedef FoldingSetImpl::Node FoldingSetNode;
250 template<class T> class FoldingSetIterator;
251 template<class T> class FoldingSetBucketIterator;
252
253 //===----------------------------------------------------------------------===//
254 /// FoldingSet - This template class is used to instantiate a specialized
255 /// implementation of the folding set to the node class T.  T must be a
256 /// subclass of FoldingSetNode and implement a Profile function.
257 ///
258 template<class T> class FoldingSet : public FoldingSetImpl {
259 private:
260   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
261   /// way to convert nodes into a unique specifier.
262   virtual void GetNodeProfile(FoldingSetNodeID &ID, Node *N) const {
263     T *TN = static_cast<T *>(N);
264     FoldingSetTrait<T>::Profile(*TN,ID);
265   }
266
267 public:
268   explicit FoldingSet(unsigned Log2InitSize = 6)
269   : FoldingSetImpl(Log2InitSize)
270   {}
271
272   typedef FoldingSetIterator<T> iterator;
273   iterator begin() { return iterator(Buckets); }
274   iterator end() { return iterator(Buckets+NumBuckets); }
275
276   typedef FoldingSetIterator<const T> const_iterator;
277   const_iterator begin() const { return const_iterator(Buckets); }
278   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
279
280   typedef FoldingSetBucketIterator<T> bucket_iterator;
281
282   bucket_iterator bucket_begin(unsigned hash) {
283     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
284   }
285
286   bucket_iterator bucket_end(unsigned hash) {
287     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
288   }
289
290   /// GetOrInsertNode - If there is an existing simple Node exactly
291   /// equal to the specified node, return it.  Otherwise, insert 'N' and
292   /// return it instead.
293   T *GetOrInsertNode(Node *N) {
294     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
295   }
296
297   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
298   /// return it.  If not, return the insertion token that will make insertion
299   /// faster.
300   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
301     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
302   }
303 };
304
305 //===----------------------------------------------------------------------===//
306 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
307 /// folding sets, which knows how to walk the folding set hash table.
308 class FoldingSetIteratorImpl {
309 protected:
310   FoldingSetNode *NodePtr;
311   FoldingSetIteratorImpl(void **Bucket);
312   void advance();
313
314 public:
315   bool operator==(const FoldingSetIteratorImpl &RHS) const {
316     return NodePtr == RHS.NodePtr;
317   }
318   bool operator!=(const FoldingSetIteratorImpl &RHS) const {
319     return NodePtr != RHS.NodePtr;
320   }
321 };
322
323
324 template<class T>
325 class FoldingSetIterator : public FoldingSetIteratorImpl {
326 public:
327   explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
328
329   T &operator*() const {
330     return *static_cast<T*>(NodePtr);
331   }
332
333   T *operator->() const {
334     return static_cast<T*>(NodePtr);
335   }
336
337   inline FoldingSetIterator& operator++() {          // Preincrement
338     advance();
339     return *this;
340   }
341   FoldingSetIterator operator++(int) {        // Postincrement
342     FoldingSetIterator tmp = *this; ++*this; return tmp;
343   }
344 };
345
346 //===----------------------------------------------------------------------===//
347 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
348 ///  shared by all folding sets, which knows how to walk a particular bucket
349 ///  of a folding set hash table.
350
351 class FoldingSetBucketIteratorImpl {
352 protected:
353   void *Ptr;
354
355   explicit FoldingSetBucketIteratorImpl(void **Bucket);
356
357   FoldingSetBucketIteratorImpl(void **Bucket, bool)
358     : Ptr(Bucket) {}
359
360   void advance() {
361     void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
362     uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
363     Ptr = reinterpret_cast<void*>(x);
364   }
365
366 public:
367   bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
368     return Ptr == RHS.Ptr;
369   }
370   bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
371     return Ptr != RHS.Ptr;
372   }
373 };
374
375
376 template<class T>
377 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
378 public:
379   explicit FoldingSetBucketIterator(void **Bucket) :
380     FoldingSetBucketIteratorImpl(Bucket) {}
381
382   FoldingSetBucketIterator(void **Bucket, bool) :
383     FoldingSetBucketIteratorImpl(Bucket, true) {}
384
385   T& operator*() const { return *static_cast<T*>(Ptr); }
386   T* operator->() const { return static_cast<T*>(Ptr); }
387
388   inline FoldingSetBucketIterator& operator++() { // Preincrement
389     advance();
390     return *this;
391   }
392   FoldingSetBucketIterator operator++(int) {      // Postincrement
393     FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
394   }
395 };
396
397 //===----------------------------------------------------------------------===//
398 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
399 /// types in an enclosing object so that they can be inserted into FoldingSets.
400 template <typename T>
401 class FoldingSetNodeWrapper : public FoldingSetNode {
402   T data;
403 public:
404   explicit FoldingSetNodeWrapper(const T& x) : data(x) {}
405   virtual ~FoldingSetNodeWrapper() {}
406
407   template<typename A1>
408   explicit FoldingSetNodeWrapper(const A1& a1)
409     : data(a1) {}
410
411   template <typename A1, typename A2>
412   explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2)
413     : data(a1,a2) {}
414
415   template <typename A1, typename A2, typename A3>
416   explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3)
417     : data(a1,a2,a3) {}
418
419   template <typename A1, typename A2, typename A3, typename A4>
420   explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
421                                  const A4& a4)
422     : data(a1,a2,a3,a4) {}
423
424   template <typename A1, typename A2, typename A3, typename A4, typename A5>
425   explicit FoldingSetNodeWrapper(const A1& a1, const A2& a2, const A3& a3,
426                                  const A4& a4, const A5& a5)
427   : data(a1,a2,a3,a4,a5) {}
428
429
430   void Profile(FoldingSetNodeID& ID) { FoldingSetTrait<T>::Profile(data, ID); }
431
432   T& getValue() { return data; }
433   const T& getValue() const { return data; }
434
435   operator T&() { return data; }
436   operator const T&() const { return data; }
437 };
438
439 //===----------------------------------------------------------------------===//
440 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
441 /// a FoldingSetNodeID value rather than requiring the node to recompute it
442 /// each time it is needed. This trades space for speed (which can be
443 /// significant if the ID is long), and it also permits nodes to drop
444 /// information that would otherwise only be required for recomputing an ID.
445 class FastFoldingSetNode : public FoldingSetNode {
446   FoldingSetNodeID FastID;
447 protected:
448   explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
449 public:
450   void Profile(FoldingSetNodeID& ID) { ID = FastID; }
451 };
452
453 //===----------------------------------------------------------------------===//
454 // Partial specializations of FoldingSetTrait.
455
456 template<typename T> struct FoldingSetTrait<T*> {
457   static inline void Profile(const T* X, FoldingSetNodeID& ID) {
458     ID.AddPointer(X);
459   }
460   static inline void Profile(T* X, FoldingSetNodeID& ID) {
461     ID.AddPointer(X);
462   }
463 };
464
465 template<typename T> struct FoldingSetTrait<const T*> {
466   static inline void Profile(const T* X, FoldingSetNodeID& ID) {
467     ID.AddPointer(X);
468   }
469 };
470
471 } // End of namespace llvm.
472
473 #endif