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/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/DataTypes.h"
29 /// This folding set used for two purposes:
30 /// 1. Given information about a node we want to create, look up the unique
31 /// instance of the node in the set. If the node already exists, return
32 /// it, otherwise return the bucket it should be inserted into.
33 /// 2. Given a node that has already been created, remove it from the set.
35 /// This class is implemented as a single-link chained hash table, where the
36 /// "buckets" are actually the nodes themselves (the next pointer is in the
37 /// node). The last node points back to the bucket to simplify node removal.
39 /// Any node that is to be included in the folding set must be a subclass of
40 /// FoldingSetNode. The node class must also define a Profile method used to
41 /// establish the unique bits of data for the node. The Profile method is
42 /// passed a FoldingSetNodeID object which is used to gather the bits. Just
43 /// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
44 /// NOTE: That the folding set does not own the nodes and it is the
45 /// responsibility of the user to dispose of the nodes.
48 /// class MyNode : public FoldingSetNode {
53 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
55 /// void Profile(FoldingSetNodeID &ID) const {
56 /// ID.AddString(Name);
57 /// ID.AddInteger(Value);
62 /// To define the folding set itself use the FoldingSet template;
65 /// FoldingSet<MyNode> MyFoldingSet;
67 /// Four public methods are available to manipulate the folding set;
69 /// 1) If you have an existing node that you want add to the set but unsure
70 /// that the node might already exist then call;
72 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
74 /// If The result is equal to the input then the node has been inserted.
75 /// Otherwise, the result is the node existing in the folding set, and the
76 /// input can be discarded (use the result instead.)
78 /// 2) If you are ready to construct a node but want to check if it already
79 /// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
82 /// FoldingSetNodeID ID;
83 /// ID.AddString(Name);
84 /// ID.AddInteger(Value);
85 /// void *InsertPoint;
87 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
89 /// If found then M with be non-NULL, else InsertPoint will point to where it
90 /// should be inserted using InsertNode.
92 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
93 /// node with FindNodeOrInsertPos;
95 /// InsertNode(N, InsertPoint);
97 /// 4) Finally, if you want to remove a node from the folding set call;
99 /// bool WasRemoved = RemoveNode(N);
101 /// The result indicates whether the node existed in the folding set.
103 class FoldingSetNodeID;
105 //===----------------------------------------------------------------------===//
106 /// FoldingSetImpl - Implements the folding set functionality. The main
107 /// structure is an array of buckets. Each bucket is indexed by the hash of
108 /// the nodes it contains. The bucket itself points to the nodes contained
109 /// in the bucket via a singly linked list. The last node in the list points
110 /// back to the bucket to facilitate node removal.
112 class FoldingSetImpl {
113 virtual void anchor(); // Out of line virtual method.
116 /// Buckets - Array of bucket chains.
120 /// NumBuckets - Length of the Buckets array. Always a power of 2.
124 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
125 /// is greater than twice the number of buckets.
130 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
133 //===--------------------------------------------------------------------===//
134 /// Node - This class is used to maintain the singly linked bucket list in
139 // NextInFoldingSetBucket - next link in the bucket list.
140 void *NextInFoldingSetBucket;
144 Node() : NextInFoldingSetBucket(nullptr) {}
147 void *getNextInBucket() const { return NextInFoldingSetBucket; }
148 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
151 /// clear - Remove all nodes from the folding set.
154 /// RemoveNode - Remove a node from the folding set, returning true if one
155 /// was removed or false if the node was not in the folding set.
156 bool RemoveNode(Node *N);
158 /// GetOrInsertNode - If there is an existing simple Node exactly
159 /// equal to the specified node, return it. Otherwise, insert 'N' and return
161 Node *GetOrInsertNode(Node *N);
163 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
164 /// return it. If not, return the insertion token that will make insertion
166 Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
168 /// InsertNode - Insert the specified node into the folding set, knowing that
169 /// it is not already in the folding set. InsertPos must be obtained from
170 /// FindNodeOrInsertPos.
171 void InsertNode(Node *N, void *InsertPos);
173 /// InsertNode - Insert the specified node into the folding set, knowing that
174 /// it is not already in the folding set.
175 void InsertNode(Node *N) {
176 Node *Inserted = GetOrInsertNode(N);
178 assert(Inserted == N && "Node already inserted!");
181 /// size - Returns the number of nodes in the folding set.
182 unsigned size() const { return NumNodes; }
184 /// empty - Returns true if there are no nodes in the folding set.
185 bool empty() const { return NumNodes == 0; }
189 /// GrowHashTable - Double the size of the hash table and rehash everything.
191 void GrowHashTable();
195 /// GetNodeProfile - Instantiations of the FoldingSet template implement
196 /// this function to gather data bits for the given node.
197 virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const = 0;
198 /// NodeEquals - Instantiations of the FoldingSet template implement
199 /// this function to compare the given node with the given ID.
200 virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
201 FoldingSetNodeID &TempID) const=0;
202 /// ComputeNodeHash - Instantiations of the FoldingSet template implement
203 /// this function to compute a hash value for the given node.
204 virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const = 0;
207 //===----------------------------------------------------------------------===//
209 template<typename T> struct FoldingSetTrait;
211 /// DefaultFoldingSetTrait - This class provides default implementations
212 /// for FoldingSetTrait implementations.
214 template<typename T> struct DefaultFoldingSetTrait {
215 static void Profile(const T &X, FoldingSetNodeID &ID) {
218 static void Profile(T &X, FoldingSetNodeID &ID) {
222 // Equals - Test if the profile for X would match ID, using TempID
223 // to compute a temporary ID if necessary. The default implementation
224 // just calls Profile and does a regular comparison. Implementations
225 // can override this to provide more efficient implementations.
226 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
227 FoldingSetNodeID &TempID);
229 // ComputeHash - Compute a hash value for X, using TempID to
230 // compute a temporary ID if necessary. The default implementation
231 // just calls Profile and does a regular hash computation.
232 // Implementations can override this to provide more efficient
234 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
237 /// FoldingSetTrait - This trait class is used to define behavior of how
238 /// to "profile" (in the FoldingSet parlance) an object of a given type.
239 /// The default behavior is to invoke a 'Profile' method on an object, but
240 /// through template specialization the behavior can be tailored for specific
241 /// types. Combined with the FoldingSetNodeWrapper class, one can add objects
242 /// to FoldingSets that were not originally designed to have that behavior.
243 template<typename T> struct FoldingSetTrait
244 : public DefaultFoldingSetTrait<T> {};
246 template<typename T, typename Ctx> struct ContextualFoldingSetTrait;
248 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
249 /// for ContextualFoldingSets.
250 template<typename T, typename Ctx>
251 struct DefaultContextualFoldingSetTrait {
252 static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
253 X.Profile(ID, Context);
255 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
256 FoldingSetNodeID &TempID, Ctx Context);
257 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
261 /// ContextualFoldingSetTrait - Like FoldingSetTrait, but for
262 /// ContextualFoldingSets.
263 template<typename T, typename Ctx> struct ContextualFoldingSetTrait
264 : public DefaultContextualFoldingSetTrait<T, Ctx> {};
266 //===--------------------------------------------------------------------===//
267 /// FoldingSetNodeIDRef - This class describes a reference to an interned
268 /// FoldingSetNodeID, which can be a useful to store node id data rather
269 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
270 /// is often much larger than necessary, and the possibility of heap
271 /// allocation means it requires a non-trivial destructor call.
272 class FoldingSetNodeIDRef {
273 const unsigned *Data;
276 FoldingSetNodeIDRef() : Data(nullptr), Size(0) {}
277 FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
279 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
280 /// used to lookup the node in the FoldingSetImpl.
281 unsigned ComputeHash() const;
283 bool operator==(FoldingSetNodeIDRef) const;
285 bool operator!=(FoldingSetNodeIDRef RHS) const { return !(*this == RHS); }
287 /// Used to compare the "ordering" of two nodes as defined by the
288 /// profiled bits and their ordering defined by memcmp().
289 bool operator<(FoldingSetNodeIDRef) const;
291 const unsigned *getData() const { return Data; }
292 size_t getSize() const { return Size; }
295 //===--------------------------------------------------------------------===//
296 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
297 /// a node. When all the bits are gathered this class is used to produce a
298 /// hash value for the node.
300 class FoldingSetNodeID {
301 /// Bits - Vector of all the data bits that make the node unique.
302 /// Use a SmallVector to avoid a heap allocation in the common case.
303 SmallVector<unsigned, 32> Bits;
306 FoldingSetNodeID() {}
308 FoldingSetNodeID(FoldingSetNodeIDRef Ref)
309 : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
311 /// Add* - Add various data types to Bit data.
313 void AddPointer(const void *Ptr);
314 void AddInteger(signed I);
315 void AddInteger(unsigned I);
316 void AddInteger(long I);
317 void AddInteger(unsigned long I);
318 void AddInteger(long long I);
319 void AddInteger(unsigned long long I);
320 void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
321 void AddString(StringRef String);
322 void AddNodeID(const FoldingSetNodeID &ID);
324 template <typename T>
325 inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
327 /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
328 /// object to be used to compute a new profile.
329 inline void clear() { Bits.clear(); }
331 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
332 /// to lookup the node in the FoldingSetImpl.
333 unsigned ComputeHash() const;
335 /// operator== - Used to compare two nodes to each other.
337 bool operator==(const FoldingSetNodeID &RHS) const;
338 bool operator==(const FoldingSetNodeIDRef RHS) const;
340 bool operator!=(const FoldingSetNodeID &RHS) const { return !(*this == RHS); }
341 bool operator!=(const FoldingSetNodeIDRef RHS) const { return !(*this ==RHS);}
343 /// Used to compare the "ordering" of two nodes as defined by the
344 /// profiled bits and their ordering defined by memcmp().
345 bool operator<(const FoldingSetNodeID &RHS) const;
346 bool operator<(const FoldingSetNodeIDRef RHS) const;
348 /// Intern - Copy this node's data to a memory region allocated from the
349 /// given allocator and return a FoldingSetNodeIDRef describing the
351 FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
354 // Convenience type to hide the implementation of the folding set.
355 typedef FoldingSetImpl::Node FoldingSetNode;
356 template<class T> class FoldingSetIterator;
357 template<class T> class FoldingSetBucketIterator;
359 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
360 // require the definition of FoldingSetNodeID.
363 DefaultFoldingSetTrait<T>::Equals(T &X, const FoldingSetNodeID &ID,
365 FoldingSetNodeID &TempID) {
366 FoldingSetTrait<T>::Profile(X, TempID);
371 DefaultFoldingSetTrait<T>::ComputeHash(T &X, FoldingSetNodeID &TempID) {
372 FoldingSetTrait<T>::Profile(X, TempID);
373 return TempID.ComputeHash();
375 template<typename T, typename Ctx>
377 DefaultContextualFoldingSetTrait<T, Ctx>::Equals(T &X,
378 const FoldingSetNodeID &ID,
380 FoldingSetNodeID &TempID,
382 ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
385 template<typename T, typename Ctx>
387 DefaultContextualFoldingSetTrait<T, Ctx>::ComputeHash(T &X,
388 FoldingSetNodeID &TempID,
390 ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
391 return TempID.ComputeHash();
394 //===----------------------------------------------------------------------===//
395 /// FoldingSet - This template class is used to instantiate a specialized
396 /// implementation of the folding set to the node class T. T must be a
397 /// subclass of FoldingSetNode and implement a Profile function.
399 template <class T> class FoldingSet final : public FoldingSetImpl {
401 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
402 /// way to convert nodes into a unique specifier.
403 void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const override {
404 T *TN = static_cast<T *>(N);
405 FoldingSetTrait<T>::Profile(*TN, ID);
407 /// NodeEquals - Instantiations may optionally provide a way to compare a
408 /// node with a specified ID.
409 bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
410 FoldingSetNodeID &TempID) const override {
411 T *TN = static_cast<T *>(N);
412 return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
414 /// ComputeNodeHash - Instantiations may optionally provide a way to compute a
415 /// hash value directly from a node.
416 unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const override {
417 T *TN = static_cast<T *>(N);
418 return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
422 explicit FoldingSet(unsigned Log2InitSize = 6)
423 : FoldingSetImpl(Log2InitSize)
426 typedef FoldingSetIterator<T> iterator;
427 iterator begin() { return iterator(Buckets); }
428 iterator end() { return iterator(Buckets+NumBuckets); }
430 typedef FoldingSetIterator<const T> const_iterator;
431 const_iterator begin() const { return const_iterator(Buckets); }
432 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
434 typedef FoldingSetBucketIterator<T> bucket_iterator;
436 bucket_iterator bucket_begin(unsigned hash) {
437 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
440 bucket_iterator bucket_end(unsigned hash) {
441 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
444 /// GetOrInsertNode - If there is an existing simple Node exactly
445 /// equal to the specified node, return it. Otherwise, insert 'N' and
446 /// return it instead.
447 T *GetOrInsertNode(Node *N) {
448 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
451 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
452 /// return it. If not, return the insertion token that will make insertion
454 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
455 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
459 //===----------------------------------------------------------------------===//
460 /// ContextualFoldingSet - This template class is a further refinement
461 /// of FoldingSet which provides a context argument when calling
462 /// Profile on its nodes. Currently, that argument is fixed at
463 /// initialization time.
465 /// T must be a subclass of FoldingSetNode and implement a Profile
466 /// function with signature
467 /// void Profile(llvm::FoldingSetNodeID &, Ctx);
468 template <class T, class Ctx>
469 class ContextualFoldingSet final : public FoldingSetImpl {
470 // Unfortunately, this can't derive from FoldingSet<T> because the
471 // construction vtable for FoldingSet<T> requires
472 // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
473 // requires a single-argument T::Profile().
478 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
479 /// way to convert nodes into a unique specifier.
480 void GetNodeProfile(FoldingSetImpl::Node *N,
481 FoldingSetNodeID &ID) const override {
482 T *TN = static_cast<T *>(N);
483 ContextualFoldingSetTrait<T, Ctx>::Profile(*TN, ID, Context);
485 bool NodeEquals(FoldingSetImpl::Node *N, const FoldingSetNodeID &ID,
486 unsigned IDHash, FoldingSetNodeID &TempID) const override {
487 T *TN = static_cast<T *>(N);
488 return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
491 unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
492 FoldingSetNodeID &TempID) const override {
493 T *TN = static_cast<T *>(N);
494 return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
498 explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
499 : FoldingSetImpl(Log2InitSize), Context(Context)
502 Ctx getContext() const { return Context; }
505 typedef FoldingSetIterator<T> iterator;
506 iterator begin() { return iterator(Buckets); }
507 iterator end() { return iterator(Buckets+NumBuckets); }
509 typedef FoldingSetIterator<const T> const_iterator;
510 const_iterator begin() const { return const_iterator(Buckets); }
511 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
513 typedef FoldingSetBucketIterator<T> bucket_iterator;
515 bucket_iterator bucket_begin(unsigned hash) {
516 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
519 bucket_iterator bucket_end(unsigned hash) {
520 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
523 /// GetOrInsertNode - If there is an existing simple Node exactly
524 /// equal to the specified node, return it. Otherwise, insert 'N'
525 /// and return it instead.
526 T *GetOrInsertNode(Node *N) {
527 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
530 /// FindNodeOrInsertPos - Look up the node specified by ID. If it
531 /// exists, return it. If not, return the insertion token that will
532 /// make insertion faster.
533 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
534 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
538 //===----------------------------------------------------------------------===//
539 /// FoldingSetVector - This template class combines a FoldingSet and a vector
540 /// to provide the interface of FoldingSet but with deterministic iteration
541 /// order based on the insertion order. T must be a subclass of FoldingSetNode
542 /// and implement a Profile function.
543 template <class T, class VectorT = SmallVector<T*, 8> >
544 class FoldingSetVector {
549 explicit FoldingSetVector(unsigned Log2InitSize = 6)
550 : Set(Log2InitSize) {
553 typedef pointee_iterator<typename VectorT::iterator> iterator;
554 iterator begin() { return Vector.begin(); }
555 iterator end() { return Vector.end(); }
557 typedef pointee_iterator<typename VectorT::const_iterator> const_iterator;
558 const_iterator begin() const { return Vector.begin(); }
559 const_iterator end() const { return Vector.end(); }
561 /// clear - Remove all nodes from the folding set.
562 void clear() { Set.clear(); Vector.clear(); }
564 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
565 /// return it. If not, return the insertion token that will make insertion
567 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
568 return Set.FindNodeOrInsertPos(ID, InsertPos);
571 /// GetOrInsertNode - If there is an existing simple Node exactly
572 /// equal to the specified node, return it. Otherwise, insert 'N' and
573 /// return it instead.
574 T *GetOrInsertNode(T *N) {
575 T *Result = Set.GetOrInsertNode(N);
576 if (Result == N) Vector.push_back(N);
580 /// InsertNode - Insert the specified node into the folding set, knowing that
581 /// it is not already in the folding set. InsertPos must be obtained from
582 /// FindNodeOrInsertPos.
583 void InsertNode(T *N, void *InsertPos) {
584 Set.InsertNode(N, InsertPos);
588 /// InsertNode - Insert the specified node into the folding set, knowing that
589 /// it is not already in the folding set.
590 void InsertNode(T *N) {
595 /// size - Returns the number of nodes in the folding set.
596 unsigned size() const { return Set.size(); }
598 /// empty - Returns true if there are no nodes in the folding set.
599 bool empty() const { return Set.empty(); }
602 //===----------------------------------------------------------------------===//
603 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
604 /// folding sets, which knows how to walk the folding set hash table.
605 class FoldingSetIteratorImpl {
607 FoldingSetNode *NodePtr;
608 FoldingSetIteratorImpl(void **Bucket);
612 bool operator==(const FoldingSetIteratorImpl &RHS) const {
613 return NodePtr == RHS.NodePtr;
615 bool operator!=(const FoldingSetIteratorImpl &RHS) const {
616 return NodePtr != RHS.NodePtr;
622 class FoldingSetIterator : public FoldingSetIteratorImpl {
624 explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
626 T &operator*() const {
627 return *static_cast<T*>(NodePtr);
630 T *operator->() const {
631 return static_cast<T*>(NodePtr);
634 inline FoldingSetIterator &operator++() { // Preincrement
638 FoldingSetIterator operator++(int) { // Postincrement
639 FoldingSetIterator tmp = *this; ++*this; return tmp;
643 //===----------------------------------------------------------------------===//
644 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
645 /// shared by all folding sets, which knows how to walk a particular bucket
646 /// of a folding set hash table.
648 class FoldingSetBucketIteratorImpl {
652 explicit FoldingSetBucketIteratorImpl(void **Bucket);
654 FoldingSetBucketIteratorImpl(void **Bucket, bool)
658 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
659 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
660 Ptr = reinterpret_cast<void*>(x);
664 bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
665 return Ptr == RHS.Ptr;
667 bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
668 return Ptr != RHS.Ptr;
674 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
676 explicit FoldingSetBucketIterator(void **Bucket) :
677 FoldingSetBucketIteratorImpl(Bucket) {}
679 FoldingSetBucketIterator(void **Bucket, bool) :
680 FoldingSetBucketIteratorImpl(Bucket, true) {}
682 T &operator*() const { return *static_cast<T*>(Ptr); }
683 T *operator->() const { return static_cast<T*>(Ptr); }
685 inline FoldingSetBucketIterator &operator++() { // Preincrement
689 FoldingSetBucketIterator operator++(int) { // Postincrement
690 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
694 //===----------------------------------------------------------------------===//
695 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
696 /// types in an enclosing object so that they can be inserted into FoldingSets.
697 template <typename T>
698 class FoldingSetNodeWrapper : public FoldingSetNode {
701 template <typename... Ts>
702 explicit FoldingSetNodeWrapper(Ts &&... Args)
703 : data(std::forward<Ts>(Args)...) {}
705 void Profile(FoldingSetNodeID &ID) { FoldingSetTrait<T>::Profile(data, ID); }
707 T &getValue() { return data; }
708 const T &getValue() const { return data; }
710 operator T&() { return data; }
711 operator const T&() const { return data; }
714 //===----------------------------------------------------------------------===//
715 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
716 /// a FoldingSetNodeID value rather than requiring the node to recompute it
717 /// each time it is needed. This trades space for speed (which can be
718 /// significant if the ID is long), and it also permits nodes to drop
719 /// information that would otherwise only be required for recomputing an ID.
720 class FastFoldingSetNode : public FoldingSetNode {
721 FoldingSetNodeID FastID;
723 explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
725 void Profile(FoldingSetNodeID &ID) const {
726 ID.AddNodeID(FastID);
730 //===----------------------------------------------------------------------===//
731 // Partial specializations of FoldingSetTrait.
733 template<typename T> struct FoldingSetTrait<T*> {
734 static inline void Profile(T *X, FoldingSetNodeID &ID) {
738 template <typename T1, typename T2>
739 struct FoldingSetTrait<std::pair<T1, T2>> {
740 static inline void Profile(const std::pair<T1, T2> &P,
741 llvm::FoldingSetNodeID &ID) {
746 } // End of namespace llvm.