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