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"
26 /// This folding set used for two purposes:
27 /// 1. Given information about a node we want to create, look up the unique
28 /// instance of the node in the set. If the node already exists, return
29 /// it, otherwise return the bucket it should be inserted into.
30 /// 2. Given a node that has already been created, remove it from the set.
32 /// This class is implemented as a single-link chained hash table, where the
33 /// "buckets" are actually the nodes themselves (the next pointer is in the
34 /// node). The last node points back to the bucket to simplify node removal.
36 /// Any node that is to be included in the folding set must be a subclass of
37 /// FoldingSetNode. The node class must also define a Profile method used to
38 /// establish the unique bits of data for the node. The Profile method is
39 /// passed a FoldingSetNodeID object which is used to gather the bits. Just
40 /// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
41 /// NOTE: That the folding set does not own the nodes and it is the
42 /// responsibility of the user to dispose of the nodes.
45 /// class MyNode : public FoldingSetNode {
50 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
52 /// void Profile(FoldingSetNodeID &ID) const {
53 /// ID.AddString(Name);
54 /// ID.AddInteger(Value);
59 /// To define the folding set itself use the FoldingSet template;
62 /// FoldingSet<MyNode> MyFoldingSet;
64 /// Four public methods are available to manipulate the folding set;
66 /// 1) If you have an existing node that you want add to the set but unsure
67 /// that the node might already exist then call;
69 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
71 /// If The result is equal to the input then the node has been inserted.
72 /// Otherwise, the result is the node existing in the folding set, and the
73 /// input can be discarded (use the result instead.)
75 /// 2) If you are ready to construct a node but want to check if it already
76 /// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
79 /// FoldingSetNodeID ID;
80 /// ID.AddString(Name);
81 /// ID.AddInteger(Value);
82 /// void *InsertPoint;
84 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
86 /// If found then M with be non-NULL, else InsertPoint will point to where it
87 /// should be inserted using InsertNode.
89 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
90 /// node with FindNodeOrInsertPos;
92 /// InsertNode(N, InsertPoint);
94 /// 4) Finally, if you want to remove a node from the folding set call;
96 /// bool WasRemoved = RemoveNode(N);
98 /// The result indicates whether the node existed in the folding set.
100 class FoldingSetNodeID;
102 //===----------------------------------------------------------------------===//
103 /// FoldingSetImpl - Implements the folding set functionality. The main
104 /// structure is an array of buckets. Each bucket is indexed by the hash of
105 /// the nodes it contains. The bucket itself points to the nodes contained
106 /// in the bucket via a singly linked list. The last node in the list points
107 /// back to the bucket to facilitate node removal.
109 class FoldingSetImpl {
110 virtual void anchor(); // Out of line virtual method.
113 /// Buckets - Array of bucket chains.
117 /// NumBuckets - Length of the Buckets array. Always a power of 2.
121 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
122 /// is greater than twice the number of buckets.
125 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
126 FoldingSetImpl(FoldingSetImpl &&Arg);
127 FoldingSetImpl &operator=(FoldingSetImpl &&RHS);
131 //===--------------------------------------------------------------------===//
132 /// Node - This class is used to maintain the singly linked bucket list in
137 // NextInFoldingSetBucket - next link in the bucket list.
138 void *NextInFoldingSetBucket;
141 Node() : NextInFoldingSetBucket(nullptr) {}
144 void *getNextInBucket() const { return NextInFoldingSetBucket; }
145 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
148 /// clear - Remove all nodes from the folding set.
151 /// RemoveNode - Remove a node from the folding set, returning true if one
152 /// was removed or false if the node was not in the folding set.
153 bool RemoveNode(Node *N);
155 /// GetOrInsertNode - If there is an existing simple Node exactly
156 /// equal to the specified node, return it. Otherwise, insert 'N' and return
158 Node *GetOrInsertNode(Node *N);
160 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
161 /// return it. If not, return the insertion token that will make insertion
163 Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
165 /// InsertNode - Insert the specified node into the folding set, knowing that
166 /// it is not already in the folding set. InsertPos must be obtained from
167 /// FindNodeOrInsertPos.
168 void InsertNode(Node *N, void *InsertPos);
170 /// InsertNode - Insert the specified node into the folding set, knowing that
171 /// it is not already in the folding set.
172 void InsertNode(Node *N) {
173 Node *Inserted = GetOrInsertNode(N);
175 assert(Inserted == N && "Node already inserted!");
178 /// size - Returns the number of nodes in the folding set.
179 unsigned size() const { return NumNodes; }
181 /// empty - Returns true if there are no nodes in the folding set.
182 bool empty() const { return NumNodes == 0; }
185 /// GrowHashTable - Double the size of the hash table and rehash everything.
187 void GrowHashTable();
190 /// GetNodeProfile - Instantiations of the FoldingSet template implement
191 /// this function to gather data bits for the given node.
192 virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const = 0;
193 /// NodeEquals - Instantiations of the FoldingSet template implement
194 /// this function to compare the given node with the given ID.
195 virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
196 FoldingSetNodeID &TempID) const=0;
197 /// ComputeNodeHash - Instantiations of the FoldingSet template implement
198 /// this function to compute a hash value for the given node.
199 virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const = 0;
202 //===----------------------------------------------------------------------===//
204 template<typename T> struct FoldingSetTrait;
206 /// DefaultFoldingSetTrait - This class provides default implementations
207 /// for FoldingSetTrait implementations.
209 template<typename T> struct DefaultFoldingSetTrait {
210 static void Profile(const T &X, FoldingSetNodeID &ID) {
213 static void Profile(T &X, FoldingSetNodeID &ID) {
217 // Equals - Test if the profile for X would match ID, using TempID
218 // to compute a temporary ID if necessary. The default implementation
219 // just calls Profile and does a regular comparison. Implementations
220 // can override this to provide more efficient implementations.
221 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
222 FoldingSetNodeID &TempID);
224 // ComputeHash - Compute a hash value for X, using TempID to
225 // compute a temporary ID if necessary. The default implementation
226 // just calls Profile and does a regular hash computation.
227 // Implementations can override this to provide more efficient
229 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
232 /// FoldingSetTrait - This trait class is used to define behavior of how
233 /// to "profile" (in the FoldingSet parlance) an object of a given type.
234 /// The default behavior is to invoke a 'Profile' method on an object, but
235 /// through template specialization the behavior can be tailored for specific
236 /// types. Combined with the FoldingSetNodeWrapper class, one can add objects
237 /// to FoldingSets that were not originally designed to have that behavior.
238 template<typename T> struct FoldingSetTrait
239 : public DefaultFoldingSetTrait<T> {};
241 template<typename T, typename Ctx> struct ContextualFoldingSetTrait;
243 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
244 /// for ContextualFoldingSets.
245 template<typename T, typename Ctx>
246 struct DefaultContextualFoldingSetTrait {
247 static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
248 X.Profile(ID, Context);
250 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
251 FoldingSetNodeID &TempID, Ctx Context);
252 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
256 /// ContextualFoldingSetTrait - Like FoldingSetTrait, but for
257 /// ContextualFoldingSets.
258 template<typename T, typename Ctx> struct ContextualFoldingSetTrait
259 : public DefaultContextualFoldingSetTrait<T, Ctx> {};
261 //===--------------------------------------------------------------------===//
262 /// FoldingSetNodeIDRef - This class describes a reference to an interned
263 /// FoldingSetNodeID, which can be a useful to store node id data rather
264 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
265 /// is often much larger than necessary, and the possibility of heap
266 /// allocation means it requires a non-trivial destructor call.
267 class FoldingSetNodeIDRef {
268 const unsigned *Data;
272 FoldingSetNodeIDRef() : Data(nullptr), Size(0) {}
273 FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
275 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
276 /// used to lookup the node in the FoldingSetImpl.
277 unsigned ComputeHash() const;
279 bool operator==(FoldingSetNodeIDRef) const;
281 bool operator!=(FoldingSetNodeIDRef RHS) const { return !(*this == RHS); }
283 /// Used to compare the "ordering" of two nodes as defined by the
284 /// profiled bits and their ordering defined by memcmp().
285 bool operator<(FoldingSetNodeIDRef) const;
287 const unsigned *getData() const { return Data; }
288 size_t getSize() const { return Size; }
291 //===--------------------------------------------------------------------===//
292 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
293 /// a node. When all the bits are gathered this class is used to produce a
294 /// hash value for the node.
296 class FoldingSetNodeID {
297 /// Bits - Vector of all the data bits that make the node unique.
298 /// Use a SmallVector to avoid a heap allocation in the common case.
299 SmallVector<unsigned, 32> Bits;
302 FoldingSetNodeID() {}
304 FoldingSetNodeID(FoldingSetNodeIDRef Ref)
305 : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
307 /// Add* - Add various data types to Bit data.
309 void AddPointer(const void *Ptr);
310 void AddInteger(signed I);
311 void AddInteger(unsigned I);
312 void AddInteger(long I);
313 void AddInteger(unsigned long I);
314 void AddInteger(long long I);
315 void AddInteger(unsigned long long I);
316 void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
317 void AddString(StringRef String);
318 void AddNodeID(const FoldingSetNodeID &ID);
320 template <typename T>
321 inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
323 /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
324 /// object to be used to compute a new profile.
325 inline void clear() { Bits.clear(); }
327 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
328 /// to lookup the node in the FoldingSetImpl.
329 unsigned ComputeHash() const;
331 /// operator== - Used to compare two nodes to each other.
333 bool operator==(const FoldingSetNodeID &RHS) const;
334 bool operator==(const FoldingSetNodeIDRef RHS) const;
336 bool operator!=(const FoldingSetNodeID &RHS) const { return !(*this == RHS); }
337 bool operator!=(const FoldingSetNodeIDRef RHS) const { return !(*this ==RHS);}
339 /// Used to compare the "ordering" of two nodes as defined by the
340 /// profiled bits and their ordering defined by memcmp().
341 bool operator<(const FoldingSetNodeID &RHS) const;
342 bool operator<(const FoldingSetNodeIDRef RHS) const;
344 /// Intern - Copy this node's data to a memory region allocated from the
345 /// given allocator and return a FoldingSetNodeIDRef describing the
347 FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
350 // Convenience type to hide the implementation of the folding set.
351 typedef FoldingSetImpl::Node FoldingSetNode;
352 template<class T> class FoldingSetIterator;
353 template<class T> class FoldingSetBucketIterator;
355 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
356 // require the definition of FoldingSetNodeID.
359 DefaultFoldingSetTrait<T>::Equals(T &X, const FoldingSetNodeID &ID,
361 FoldingSetNodeID &TempID) {
362 FoldingSetTrait<T>::Profile(X, TempID);
367 DefaultFoldingSetTrait<T>::ComputeHash(T &X, FoldingSetNodeID &TempID) {
368 FoldingSetTrait<T>::Profile(X, TempID);
369 return TempID.ComputeHash();
371 template<typename T, typename Ctx>
373 DefaultContextualFoldingSetTrait<T, Ctx>::Equals(T &X,
374 const FoldingSetNodeID &ID,
376 FoldingSetNodeID &TempID,
378 ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
381 template<typename T, typename Ctx>
383 DefaultContextualFoldingSetTrait<T, Ctx>::ComputeHash(T &X,
384 FoldingSetNodeID &TempID,
386 ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
387 return TempID.ComputeHash();
390 //===----------------------------------------------------------------------===//
391 /// FoldingSet - This template class is used to instantiate a specialized
392 /// implementation of the folding set to the node class T. T must be a
393 /// subclass of FoldingSetNode and implement a Profile function.
395 /// Note that this set type is movable and move-assignable. However, its
396 /// moved-from state is not a valid state for anything other than
397 /// move-assigning and destroying. This is primarily to enable movable APIs
398 /// that incorporate these objects.
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) {}
425 FoldingSet(FoldingSet &&Arg) : FoldingSetImpl(std::move(Arg)) {}
426 FoldingSet &operator=(FoldingSet &&RHS) {
427 (void)FoldingSetImpl::operator=(std::move(RHS));
431 typedef FoldingSetIterator<T> iterator;
432 iterator begin() { return iterator(Buckets); }
433 iterator end() { return iterator(Buckets+NumBuckets); }
435 typedef FoldingSetIterator<const T> const_iterator;
436 const_iterator begin() const { return const_iterator(Buckets); }
437 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
439 typedef FoldingSetBucketIterator<T> bucket_iterator;
441 bucket_iterator bucket_begin(unsigned hash) {
442 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
445 bucket_iterator bucket_end(unsigned hash) {
446 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
449 /// GetOrInsertNode - If there is an existing simple Node exactly
450 /// equal to the specified node, return it. Otherwise, insert 'N' and
451 /// return it instead.
452 T *GetOrInsertNode(Node *N) {
453 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
456 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
457 /// return it. If not, return the insertion token that will make insertion
459 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
460 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
464 //===----------------------------------------------------------------------===//
465 /// ContextualFoldingSet - This template class is a further refinement
466 /// of FoldingSet which provides a context argument when calling
467 /// Profile on its nodes. Currently, that argument is fixed at
468 /// initialization time.
470 /// T must be a subclass of FoldingSetNode and implement a Profile
471 /// function with signature
472 /// void Profile(llvm::FoldingSetNodeID &, Ctx);
473 template <class T, class Ctx>
474 class ContextualFoldingSet final : public FoldingSetImpl {
475 // Unfortunately, this can't derive from FoldingSet<T> because the
476 // construction vtable for FoldingSet<T> requires
477 // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
478 // requires a single-argument T::Profile().
483 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
484 /// way to convert nodes into a unique specifier.
485 void GetNodeProfile(FoldingSetImpl::Node *N,
486 FoldingSetNodeID &ID) const override {
487 T *TN = static_cast<T *>(N);
488 ContextualFoldingSetTrait<T, Ctx>::Profile(*TN, ID, Context);
490 bool NodeEquals(FoldingSetImpl::Node *N, const FoldingSetNodeID &ID,
491 unsigned IDHash, FoldingSetNodeID &TempID) const override {
492 T *TN = static_cast<T *>(N);
493 return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
496 unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
497 FoldingSetNodeID &TempID) const override {
498 T *TN = static_cast<T *>(N);
499 return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
503 explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
504 : FoldingSetImpl(Log2InitSize), Context(Context)
507 Ctx getContext() const { return Context; }
509 typedef FoldingSetIterator<T> iterator;
510 iterator begin() { return iterator(Buckets); }
511 iterator end() { return iterator(Buckets+NumBuckets); }
513 typedef FoldingSetIterator<const T> const_iterator;
514 const_iterator begin() const { return const_iterator(Buckets); }
515 const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
517 typedef FoldingSetBucketIterator<T> bucket_iterator;
519 bucket_iterator bucket_begin(unsigned hash) {
520 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
523 bucket_iterator bucket_end(unsigned hash) {
524 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
527 /// GetOrInsertNode - If there is an existing simple Node exactly
528 /// equal to the specified node, return it. Otherwise, insert 'N'
529 /// and return it instead.
530 T *GetOrInsertNode(Node *N) {
531 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
534 /// FindNodeOrInsertPos - Look up the node specified by ID. If it
535 /// exists, return it. If not, return the insertion token that will
536 /// make insertion faster.
537 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
538 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
542 //===----------------------------------------------------------------------===//
543 /// FoldingSetVector - This template class combines a FoldingSet and a vector
544 /// to provide the interface of FoldingSet but with deterministic iteration
545 /// order based on the insertion order. T must be a subclass of FoldingSetNode
546 /// and implement a Profile function.
547 template <class T, class VectorT = SmallVector<T*, 8> >
548 class FoldingSetVector {
553 explicit FoldingSetVector(unsigned Log2InitSize = 6)
554 : Set(Log2InitSize) {
557 typedef pointee_iterator<typename VectorT::iterator> iterator;
558 iterator begin() { return Vector.begin(); }
559 iterator end() { return Vector.end(); }
561 typedef pointee_iterator<typename VectorT::const_iterator> const_iterator;
562 const_iterator begin() const { return Vector.begin(); }
563 const_iterator end() const { return Vector.end(); }
565 /// clear - Remove all nodes from the folding set.
566 void clear() { Set.clear(); Vector.clear(); }
568 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
569 /// return it. If not, return the insertion token that will make insertion
571 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
572 return Set.FindNodeOrInsertPos(ID, InsertPos);
575 /// GetOrInsertNode - If there is an existing simple Node exactly
576 /// equal to the specified node, return it. Otherwise, insert 'N' and
577 /// return it instead.
578 T *GetOrInsertNode(T *N) {
579 T *Result = Set.GetOrInsertNode(N);
580 if (Result == N) Vector.push_back(N);
584 /// InsertNode - Insert the specified node into the folding set, knowing that
585 /// it is not already in the folding set. InsertPos must be obtained from
586 /// FindNodeOrInsertPos.
587 void InsertNode(T *N, void *InsertPos) {
588 Set.InsertNode(N, InsertPos);
592 /// InsertNode - Insert the specified node into the folding set, knowing that
593 /// it is not already in the folding set.
594 void InsertNode(T *N) {
599 /// size - Returns the number of nodes in the folding set.
600 unsigned size() const { return Set.size(); }
602 /// empty - Returns true if there are no nodes in the folding set.
603 bool empty() const { return Set.empty(); }
606 //===----------------------------------------------------------------------===//
607 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
608 /// folding sets, which knows how to walk the folding set hash table.
609 class FoldingSetIteratorImpl {
611 FoldingSetNode *NodePtr;
612 FoldingSetIteratorImpl(void **Bucket);
616 bool operator==(const FoldingSetIteratorImpl &RHS) const {
617 return NodePtr == RHS.NodePtr;
619 bool operator!=(const FoldingSetIteratorImpl &RHS) const {
620 return NodePtr != RHS.NodePtr;
624 template <class T> class FoldingSetIterator : public FoldingSetIteratorImpl {
626 explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
628 T &operator*() const {
629 return *static_cast<T*>(NodePtr);
632 T *operator->() const {
633 return static_cast<T*>(NodePtr);
636 inline FoldingSetIterator &operator++() { // Preincrement
640 FoldingSetIterator operator++(int) { // Postincrement
641 FoldingSetIterator tmp = *this; ++*this; return tmp;
645 //===----------------------------------------------------------------------===//
646 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
647 /// shared by all folding sets, which knows how to walk a particular bucket
648 /// of a folding set hash table.
650 class FoldingSetBucketIteratorImpl {
654 explicit FoldingSetBucketIteratorImpl(void **Bucket);
656 FoldingSetBucketIteratorImpl(void **Bucket, bool)
660 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
661 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
662 Ptr = reinterpret_cast<void*>(x);
666 bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
667 return Ptr == RHS.Ptr;
669 bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
670 return Ptr != RHS.Ptr;
675 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
677 explicit FoldingSetBucketIterator(void **Bucket) :
678 FoldingSetBucketIteratorImpl(Bucket) {}
680 FoldingSetBucketIterator(void **Bucket, bool) :
681 FoldingSetBucketIteratorImpl(Bucket, true) {}
683 T &operator*() const { return *static_cast<T*>(Ptr); }
684 T *operator->() const { return static_cast<T*>(Ptr); }
686 inline FoldingSetBucketIterator &operator++() { // Preincrement
690 FoldingSetBucketIterator operator++(int) { // Postincrement
691 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
695 //===----------------------------------------------------------------------===//
696 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
697 /// types in an enclosing object so that they can be inserted into FoldingSets.
698 template <typename T>
699 class FoldingSetNodeWrapper : public FoldingSetNode {
703 template <typename... Ts>
704 explicit FoldingSetNodeWrapper(Ts &&... Args)
705 : data(std::forward<Ts>(Args)...) {}
707 void Profile(FoldingSetNodeID &ID) { FoldingSetTrait<T>::Profile(data, ID); }
709 T &getValue() { return data; }
710 const T &getValue() const { return data; }
712 operator T&() { return data; }
713 operator const T&() const { return data; }
716 //===----------------------------------------------------------------------===//
717 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
718 /// a FoldingSetNodeID value rather than requiring the node to recompute it
719 /// each time it is needed. This trades space for speed (which can be
720 /// significant if the ID is long), and it also permits nodes to drop
721 /// information that would otherwise only be required for recomputing an ID.
722 class FastFoldingSetNode : public FoldingSetNode {
723 FoldingSetNodeID FastID;
726 explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
729 void Profile(FoldingSetNodeID &ID) const { ID.AddNodeID(FastID); }
732 //===----------------------------------------------------------------------===//
733 // Partial specializations of FoldingSetTrait.
735 template<typename T> struct FoldingSetTrait<T*> {
736 static inline void Profile(T *X, FoldingSetNodeID &ID) {
740 template <typename T1, typename T2>
741 struct FoldingSetTrait<std::pair<T1, T2>> {
742 static inline void Profile(const std::pair<T1, T2> &P,
743 llvm::FoldingSetNodeID &ID) {
748 } // End of namespace llvm.