Sort the #include lines for the include/... tree with the script.
[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/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/DataTypes.h"
22
23 namespace llvm {
24   class APFloat;
25   class APInt;
26   class BumpPtrAllocator;
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   /// InsertNode - Insert the specified node into the folding set, knowing that
170   /// it is not already in the folding set.
171   void InsertNode(Node *N) {
172     Node *Inserted = GetOrInsertNode(N);
173     (void)Inserted;
174     assert(Inserted == N && "Node already inserted!");
175   }
176
177   /// size - Returns the number of nodes in the folding set.
178   unsigned size() const { return NumNodes; }
179
180   /// empty - Returns true if there are no nodes in the folding set.
181   bool empty() const { return NumNodes == 0; }
182
183 private:
184
185   /// GrowHashTable - Double the size of the hash table and rehash everything.
186   ///
187   void GrowHashTable();
188
189 protected:
190
191   /// GetNodeProfile - Instantiations of the FoldingSet template implement
192   /// this function to gather data bits for the given node.
193   virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const = 0;
194   /// NodeEquals - Instantiations of the FoldingSet template implement
195   /// this function to compare the given node with the given ID.
196   virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
197                           FoldingSetNodeID &TempID) const=0;
198   /// ComputeNodeHash - Instantiations of the FoldingSet template implement
199   /// this function to compute a hash value for the given node.
200   virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const = 0;
201 };
202
203 //===----------------------------------------------------------------------===//
204
205 template<typename T> struct FoldingSetTrait;
206
207 /// DefaultFoldingSetTrait - This class provides default implementations
208 /// for FoldingSetTrait implementations.
209 ///
210 template<typename T> struct DefaultFoldingSetTrait {
211   static void Profile(const T &X, FoldingSetNodeID &ID) {
212     X.Profile(ID);
213   }
214   static void Profile(T &X, FoldingSetNodeID &ID) {
215     X.Profile(ID);
216   }
217
218   // Equals - Test if the profile for X would match ID, using TempID
219   // to compute a temporary ID if necessary. The default implementation
220   // just calls Profile and does a regular comparison. Implementations
221   // can override this to provide more efficient implementations.
222   static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
223                             FoldingSetNodeID &TempID);
224
225   // ComputeHash - Compute a hash value for X, using TempID to
226   // compute a temporary ID if necessary. The default implementation
227   // just calls Profile and does a regular hash computation.
228   // Implementations can override this to provide more efficient
229   // implementations.
230   static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
231 };
232
233 /// FoldingSetTrait - This trait class is used to define behavior of how
234 /// to "profile" (in the FoldingSet parlance) an object of a given type.
235 /// The default behavior is to invoke a 'Profile' method on an object, but
236 /// through template specialization the behavior can be tailored for specific
237 /// types.  Combined with the FoldingSetNodeWrapper class, one can add objects
238 /// to FoldingSets that were not originally designed to have that behavior.
239 template<typename T> struct FoldingSetTrait
240   : public DefaultFoldingSetTrait<T> {};
241
242 template<typename T, typename Ctx> struct ContextualFoldingSetTrait;
243
244 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
245 /// for ContextualFoldingSets.
246 template<typename T, typename Ctx>
247 struct DefaultContextualFoldingSetTrait {
248   static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
249     X.Profile(ID, Context);
250   }
251   static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
252                             FoldingSetNodeID &TempID, Ctx Context);
253   static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
254                                      Ctx Context);
255 };
256
257 /// ContextualFoldingSetTrait - Like FoldingSetTrait, but for
258 /// ContextualFoldingSets.
259 template<typename T, typename Ctx> struct ContextualFoldingSetTrait
260   : public DefaultContextualFoldingSetTrait<T, Ctx> {};
261
262 //===--------------------------------------------------------------------===//
263 /// FoldingSetNodeIDRef - This class describes a reference to an interned
264 /// FoldingSetNodeID, which can be a useful to store node id data rather
265 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
266 /// is often much larger than necessary, and the possibility of heap
267 /// allocation means it requires a non-trivial destructor call.
268 class FoldingSetNodeIDRef {
269   const unsigned *Data;
270   size_t Size;
271 public:
272   FoldingSetNodeIDRef() : Data(0), Size(0) {}
273   FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
274
275   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
276   /// used to lookup the node in the FoldingSetImpl.
277   unsigned ComputeHash() const;
278
279   bool operator==(FoldingSetNodeIDRef) const;
280
281   /// Used to compare the "ordering" of two nodes as defined by the
282   /// profiled bits and their ordering defined by memcmp().
283   bool operator<(FoldingSetNodeIDRef) const;
284
285   const unsigned *getData() const { return Data; }
286   size_t getSize() const { return Size; }
287 };
288
289 //===--------------------------------------------------------------------===//
290 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
291 /// a node.  When all the bits are gathered this class is used to produce a
292 /// hash value for the node.
293 ///
294 class FoldingSetNodeID {
295   /// Bits - Vector of all the data bits that make the node unique.
296   /// Use a SmallVector to avoid a heap allocation in the common case.
297   SmallVector<unsigned, 32> Bits;
298
299 public:
300   FoldingSetNodeID() {}
301
302   FoldingSetNodeID(FoldingSetNodeIDRef Ref)
303     : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
304
305   /// Add* - Add various data types to Bit data.
306   ///
307   void AddPointer(const void *Ptr);
308   void AddInteger(signed I);
309   void AddInteger(unsigned I);
310   void AddInteger(long I);
311   void AddInteger(unsigned long I);
312   void AddInteger(long long I);
313   void AddInteger(unsigned long long I);
314   void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
315   void AddString(StringRef String);
316   void AddNodeID(const FoldingSetNodeID &ID);
317
318   template <typename T>
319   inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
320
321   /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
322   /// object to be used to compute a new profile.
323   inline void clear() { Bits.clear(); }
324
325   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
326   /// to lookup the node in the FoldingSetImpl.
327   unsigned ComputeHash() const;
328
329   /// operator== - Used to compare two nodes to each other.
330   ///
331   bool operator==(const FoldingSetNodeID &RHS) const;
332   bool operator==(const FoldingSetNodeIDRef RHS) const;
333
334   /// Used to compare the "ordering" of two nodes as defined by the
335   /// profiled bits and their ordering defined by memcmp().
336   bool operator<(const FoldingSetNodeID &RHS) const;
337   bool operator<(const FoldingSetNodeIDRef RHS) const;
338
339   /// Intern - Copy this node's data to a memory region allocated from the
340   /// given allocator and return a FoldingSetNodeIDRef describing the
341   /// interned data.
342   FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
343 };
344
345 // Convenience type to hide the implementation of the folding set.
346 typedef FoldingSetImpl::Node FoldingSetNode;
347 template<class T> class FoldingSetIterator;
348 template<class T> class FoldingSetBucketIterator;
349
350 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
351 // require the definition of FoldingSetNodeID.
352 template<typename T>
353 inline bool
354 DefaultFoldingSetTrait<T>::Equals(T &X, const FoldingSetNodeID &ID,
355                                   unsigned IDHash, FoldingSetNodeID &TempID) {
356   FoldingSetTrait<T>::Profile(X, TempID);
357   return TempID == ID;
358 }
359 template<typename T>
360 inline unsigned
361 DefaultFoldingSetTrait<T>::ComputeHash(T &X, FoldingSetNodeID &TempID) {
362   FoldingSetTrait<T>::Profile(X, TempID);
363   return TempID.ComputeHash();
364 }
365 template<typename T, typename Ctx>
366 inline bool
367 DefaultContextualFoldingSetTrait<T, Ctx>::Equals(T &X,
368                                                  const FoldingSetNodeID &ID,
369                                                  unsigned IDHash,
370                                                  FoldingSetNodeID &TempID,
371                                                  Ctx Context) {
372   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
373   return TempID == ID;
374 }
375 template<typename T, typename Ctx>
376 inline unsigned
377 DefaultContextualFoldingSetTrait<T, Ctx>::ComputeHash(T &X,
378                                                       FoldingSetNodeID &TempID,
379                                                       Ctx Context) {
380   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
381   return TempID.ComputeHash();
382 }
383
384 //===----------------------------------------------------------------------===//
385 /// FoldingSet - This template class is used to instantiate a specialized
386 /// implementation of the folding set to the node class T.  T must be a
387 /// subclass of FoldingSetNode and implement a Profile function.
388 ///
389 template<class T> class FoldingSet : public FoldingSetImpl {
390 private:
391   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
392   /// way to convert nodes into a unique specifier.
393   virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const {
394     T *TN = static_cast<T *>(N);
395     FoldingSetTrait<T>::Profile(*TN, ID);
396   }
397   /// NodeEquals - Instantiations may optionally provide a way to compare a
398   /// node with a specified ID.
399   virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
400                           FoldingSetNodeID &TempID) const {
401     T *TN = static_cast<T *>(N);
402     return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
403   }
404   /// ComputeNodeHash - Instantiations may optionally provide a way to compute a
405   /// hash value directly from a node.
406   virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const {
407     T *TN = static_cast<T *>(N);
408     return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
409   }
410
411 public:
412   explicit FoldingSet(unsigned Log2InitSize = 6)
413   : FoldingSetImpl(Log2InitSize)
414   {}
415
416   typedef FoldingSetIterator<T> iterator;
417   iterator begin() { return iterator(Buckets); }
418   iterator end() { return iterator(Buckets+NumBuckets); }
419
420   typedef FoldingSetIterator<const T> const_iterator;
421   const_iterator begin() const { return const_iterator(Buckets); }
422   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
423
424   typedef FoldingSetBucketIterator<T> bucket_iterator;
425
426   bucket_iterator bucket_begin(unsigned hash) {
427     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
428   }
429
430   bucket_iterator bucket_end(unsigned hash) {
431     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
432   }
433
434   /// GetOrInsertNode - If there is an existing simple Node exactly
435   /// equal to the specified node, return it.  Otherwise, insert 'N' and
436   /// return it instead.
437   T *GetOrInsertNode(Node *N) {
438     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
439   }
440
441   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
442   /// return it.  If not, return the insertion token that will make insertion
443   /// faster.
444   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
445     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
446   }
447 };
448
449 //===----------------------------------------------------------------------===//
450 /// ContextualFoldingSet - This template class is a further refinement
451 /// of FoldingSet which provides a context argument when calling
452 /// Profile on its nodes.  Currently, that argument is fixed at
453 /// initialization time.
454 ///
455 /// T must be a subclass of FoldingSetNode and implement a Profile
456 /// function with signature
457 ///   void Profile(llvm::FoldingSetNodeID &, Ctx);
458 template <class T, class Ctx>
459 class ContextualFoldingSet : public FoldingSetImpl {
460   // Unfortunately, this can't derive from FoldingSet<T> because the
461   // construction vtable for FoldingSet<T> requires
462   // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
463   // requires a single-argument T::Profile().
464
465 private:
466   Ctx Context;
467
468   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
469   /// way to convert nodes into a unique specifier.
470   virtual void GetNodeProfile(FoldingSetImpl::Node *N,
471                               FoldingSetNodeID &ID) const {
472     T *TN = static_cast<T *>(N);
473     ContextualFoldingSetTrait<T, Ctx>::Profile(*TN, ID, Context);
474   }
475   virtual bool NodeEquals(FoldingSetImpl::Node *N,
476                           const FoldingSetNodeID &ID, unsigned IDHash,
477                           FoldingSetNodeID &TempID) const {
478     T *TN = static_cast<T *>(N);
479     return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
480                                                      Context);
481   }
482   virtual unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
483                                    FoldingSetNodeID &TempID) const {
484     T *TN = static_cast<T *>(N);
485     return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
486   }
487
488 public:
489   explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
490   : FoldingSetImpl(Log2InitSize), Context(Context)
491   {}
492
493   Ctx getContext() const { return Context; }
494
495
496   typedef FoldingSetIterator<T> iterator;
497   iterator begin() { return iterator(Buckets); }
498   iterator end() { return iterator(Buckets+NumBuckets); }
499
500   typedef FoldingSetIterator<const T> const_iterator;
501   const_iterator begin() const { return const_iterator(Buckets); }
502   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
503
504   typedef FoldingSetBucketIterator<T> bucket_iterator;
505
506   bucket_iterator bucket_begin(unsigned hash) {
507     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
508   }
509
510   bucket_iterator bucket_end(unsigned hash) {
511     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
512   }
513
514   /// GetOrInsertNode - If there is an existing simple Node exactly
515   /// equal to the specified node, return it.  Otherwise, insert 'N'
516   /// and return it instead.
517   T *GetOrInsertNode(Node *N) {
518     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
519   }
520
521   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it
522   /// exists, return it.  If not, return the insertion token that will
523   /// make insertion faster.
524   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
525     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
526   }
527 };
528
529 //===----------------------------------------------------------------------===//
530 /// FoldingSetVectorIterator - This implements an iterator for
531 /// FoldingSetVector. It is only necessary because FoldingSetIterator provides
532 /// a value_type of T, while the vector in FoldingSetVector exposes
533 /// a value_type of T*. Fortunately, FoldingSetIterator doesn't expose very
534 /// much besides operator* and operator->, so we just wrap the inner vector
535 /// iterator and perform the extra dereference.
536 template <class T, class VectorIteratorT>
537 class FoldingSetVectorIterator {
538   // Provide a typedef to workaround the lack of correct injected class name
539   // support in older GCCs.
540   typedef FoldingSetVectorIterator<T, VectorIteratorT> SelfT;
541
542   VectorIteratorT Iterator;
543
544 public:
545   FoldingSetVectorIterator(VectorIteratorT I) : Iterator(I) {}
546
547   bool operator==(const SelfT &RHS) const {
548     return Iterator == RHS.Iterator;
549   }
550   bool operator!=(const SelfT &RHS) const {
551     return Iterator != RHS.Iterator;
552   }
553
554   T &operator*() const { return **Iterator; }
555
556   T *operator->() const { return *Iterator; }
557
558   inline SelfT &operator++() {
559     ++Iterator;
560     return *this;
561   }
562   SelfT operator++(int) {
563     SelfT tmp = *this;
564     ++*this;
565     return tmp;
566   }
567 };
568
569 //===----------------------------------------------------------------------===//
570 /// FoldingSetVector - This template class combines a FoldingSet and a vector
571 /// to provide the interface of FoldingSet but with deterministic iteration
572 /// order based on the insertion order. T must be a subclass of FoldingSetNode
573 /// and implement a Profile function.
574 template <class T, class VectorT = SmallVector<T*, 8> >
575 class FoldingSetVector {
576   FoldingSet<T> Set;
577   VectorT Vector;
578
579 public:
580   explicit FoldingSetVector(unsigned Log2InitSize = 6)
581       : Set(Log2InitSize) {
582   }
583
584   typedef FoldingSetVectorIterator<T, typename VectorT::iterator> iterator;
585   iterator begin() { return Vector.begin(); }
586   iterator end()   { return Vector.end(); }
587
588   typedef FoldingSetVectorIterator<const T, typename VectorT::const_iterator>
589     const_iterator;
590   const_iterator begin() const { return Vector.begin(); }
591   const_iterator end()   const { return Vector.end(); }
592
593   /// clear - Remove all nodes from the folding set.
594   void clear() { Set.clear(); Vector.clear(); }
595
596   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
597   /// return it.  If not, return the insertion token that will make insertion
598   /// faster.
599   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
600     return Set.FindNodeOrInsertPos(ID, InsertPos);
601   }
602
603   /// GetOrInsertNode - If there is an existing simple Node exactly
604   /// equal to the specified node, return it.  Otherwise, insert 'N' and
605   /// return it instead.
606   T *GetOrInsertNode(T *N) {
607     T *Result = Set.GetOrInsertNode(N);
608     if (Result == N) Vector.push_back(N);
609     return Result;
610   }
611
612   /// InsertNode - Insert the specified node into the folding set, knowing that
613   /// it is not already in the folding set.  InsertPos must be obtained from
614   /// FindNodeOrInsertPos.
615   void InsertNode(T *N, void *InsertPos) {
616     Set.InsertNode(N, InsertPos);
617     Vector.push_back(N);
618   }
619
620   /// InsertNode - Insert the specified node into the folding set, knowing that
621   /// it is not already in the folding set.
622   void InsertNode(T *N) {
623     Set.InsertNode(N);
624     Vector.push_back(N);
625   }
626
627   /// size - Returns the number of nodes in the folding set.
628   unsigned size() const { return Set.size(); }
629
630   /// empty - Returns true if there are no nodes in the folding set.
631   bool empty() const { return Set.empty(); }
632 };
633
634 //===----------------------------------------------------------------------===//
635 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
636 /// folding sets, which knows how to walk the folding set hash table.
637 class FoldingSetIteratorImpl {
638 protected:
639   FoldingSetNode *NodePtr;
640   FoldingSetIteratorImpl(void **Bucket);
641   void advance();
642
643 public:
644   bool operator==(const FoldingSetIteratorImpl &RHS) const {
645     return NodePtr == RHS.NodePtr;
646   }
647   bool operator!=(const FoldingSetIteratorImpl &RHS) const {
648     return NodePtr != RHS.NodePtr;
649   }
650 };
651
652
653 template<class T>
654 class FoldingSetIterator : public FoldingSetIteratorImpl {
655 public:
656   explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
657
658   T &operator*() const {
659     return *static_cast<T*>(NodePtr);
660   }
661
662   T *operator->() const {
663     return static_cast<T*>(NodePtr);
664   }
665
666   inline FoldingSetIterator &operator++() {          // Preincrement
667     advance();
668     return *this;
669   }
670   FoldingSetIterator operator++(int) {        // Postincrement
671     FoldingSetIterator tmp = *this; ++*this; return tmp;
672   }
673 };
674
675 //===----------------------------------------------------------------------===//
676 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
677 /// shared by all folding sets, which knows how to walk a particular bucket
678 /// of a folding set hash table.
679
680 class FoldingSetBucketIteratorImpl {
681 protected:
682   void *Ptr;
683
684   explicit FoldingSetBucketIteratorImpl(void **Bucket);
685
686   FoldingSetBucketIteratorImpl(void **Bucket, bool)
687     : Ptr(Bucket) {}
688
689   void advance() {
690     void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
691     uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
692     Ptr = reinterpret_cast<void*>(x);
693   }
694
695 public:
696   bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
697     return Ptr == RHS.Ptr;
698   }
699   bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
700     return Ptr != RHS.Ptr;
701   }
702 };
703
704
705 template<class T>
706 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
707 public:
708   explicit FoldingSetBucketIterator(void **Bucket) :
709     FoldingSetBucketIteratorImpl(Bucket) {}
710
711   FoldingSetBucketIterator(void **Bucket, bool) :
712     FoldingSetBucketIteratorImpl(Bucket, true) {}
713
714   T &operator*() const { return *static_cast<T*>(Ptr); }
715   T *operator->() const { return static_cast<T*>(Ptr); }
716
717   inline FoldingSetBucketIterator &operator++() { // Preincrement
718     advance();
719     return *this;
720   }
721   FoldingSetBucketIterator operator++(int) {      // Postincrement
722     FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
723   }
724 };
725
726 //===----------------------------------------------------------------------===//
727 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
728 /// types in an enclosing object so that they can be inserted into FoldingSets.
729 template <typename T>
730 class FoldingSetNodeWrapper : public FoldingSetNode {
731   T data;
732 public:
733   explicit FoldingSetNodeWrapper(const T &x) : data(x) {}
734   virtual ~FoldingSetNodeWrapper() {}
735
736   template<typename A1>
737   explicit FoldingSetNodeWrapper(const A1 &a1)
738     : data(a1) {}
739
740   template <typename A1, typename A2>
741   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2)
742     : data(a1,a2) {}
743
744   template <typename A1, typename A2, typename A3>
745   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3)
746     : data(a1,a2,a3) {}
747
748   template <typename A1, typename A2, typename A3, typename A4>
749   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
750                                  const A4 &a4)
751     : data(a1,a2,a3,a4) {}
752
753   template <typename A1, typename A2, typename A3, typename A4, typename A5>
754   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
755                                  const A4 &a4, const A5 &a5)
756   : data(a1,a2,a3,a4,a5) {}
757
758
759   void Profile(FoldingSetNodeID &ID) { FoldingSetTrait<T>::Profile(data, ID); }
760
761   T &getValue() { return data; }
762   const T &getValue() const { return data; }
763
764   operator T&() { return data; }
765   operator const T&() const { return data; }
766 };
767
768 //===----------------------------------------------------------------------===//
769 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
770 /// a FoldingSetNodeID value rather than requiring the node to recompute it
771 /// each time it is needed. This trades space for speed (which can be
772 /// significant if the ID is long), and it also permits nodes to drop
773 /// information that would otherwise only be required for recomputing an ID.
774 class FastFoldingSetNode : public FoldingSetNode {
775   FoldingSetNodeID FastID;
776 protected:
777   explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
778 public:
779   void Profile(FoldingSetNodeID &ID) const { 
780     ID.AddNodeID(FastID); 
781   }
782 };
783
784 //===----------------------------------------------------------------------===//
785 // Partial specializations of FoldingSetTrait.
786
787 template<typename T> struct FoldingSetTrait<T*> {
788   static inline void Profile(T *X, FoldingSetNodeID &ID) {
789     ID.AddPointer(X);
790   }
791 };
792 } // End of namespace llvm.
793
794 #endif