LLVM  4.0.0
FoldingSet.h
Go to the documentation of this file.
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/iterator.h"
21 #include "llvm/Support/Allocator.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <cstdint>
25 #include <utility>
26 
27 namespace llvm {
28 
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.
34 ///
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.
38 ///
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.
46 ///
47 /// Eg.
48 /// class MyNode : public FoldingSetNode {
49 /// private:
50 /// std::string Name;
51 /// unsigned Value;
52 /// public:
53 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
54 /// ...
55 /// void Profile(FoldingSetNodeID &ID) const {
56 /// ID.AddString(Name);
57 /// ID.AddInteger(Value);
58 /// }
59 /// ...
60 /// };
61 ///
62 /// To define the folding set itself use the FoldingSet template;
63 ///
64 /// Eg.
65 /// FoldingSet<MyNode> MyFoldingSet;
66 ///
67 /// Four public methods are available to manipulate the folding set;
68 ///
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;
71 ///
72 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
73 ///
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.)
77 ///
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
80 /// check;
81 ///
82 /// FoldingSetNodeID ID;
83 /// ID.AddString(Name);
84 /// ID.AddInteger(Value);
85 /// void *InsertPoint;
86 ///
87 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
88 ///
89 /// If found then M with be non-NULL, else InsertPoint will point to where it
90 /// should be inserted using InsertNode.
91 ///
92 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
93 /// node with FindNodeOrInsertPos;
94 ///
95 /// InsertNode(N, InsertPoint);
96 ///
97 /// 4) Finally, if you want to remove a node from the folding set call;
98 ///
99 /// bool WasRemoved = RemoveNode(N);
100 ///
101 /// The result indicates whether the node existed in the folding set.
102 
103 class FoldingSetNodeID;
104 class StringRef;
105 
106 //===----------------------------------------------------------------------===//
107 /// FoldingSetImpl - Implements the folding set functionality. The main
108 /// structure is an array of buckets. Each bucket is indexed by the hash of
109 /// the nodes it contains. The bucket itself points to the nodes contained
110 /// in the bucket via a singly linked list. The last node in the list points
111 /// back to the bucket to facilitate node removal.
112 ///
114  virtual void anchor(); // Out of line virtual method.
115 
116 protected:
117  /// Buckets - Array of bucket chains.
118  ///
119  void **Buckets;
120 
121  /// NumBuckets - Length of the Buckets array. Always a power of 2.
122  ///
123  unsigned NumBuckets;
124 
125  /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
126  /// is greater than twice the number of buckets.
127  unsigned NumNodes;
128 
129  explicit FoldingSetImpl(unsigned Log2InitSize = 6);
132  ~FoldingSetImpl();
133 
134 public:
135  //===--------------------------------------------------------------------===//
136  /// Node - This class is used to maintain the singly linked bucket list in
137  /// a folding set.
138  ///
139  class Node {
140  private:
141  // NextInFoldingSetBucket - next link in the bucket list.
142  void *NextInFoldingSetBucket;
143 
144  public:
145  Node() : NextInFoldingSetBucket(nullptr) {}
146 
147  // Accessors
148  void *getNextInBucket() const { return NextInFoldingSetBucket; }
149  void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
150  };
151 
152  /// clear - Remove all nodes from the folding set.
153  void clear();
154 
155  /// RemoveNode - Remove a node from the folding set, returning true if one
156  /// was removed or false if the node was not in the folding set.
157  bool RemoveNode(Node *N);
158 
159  /// GetOrInsertNode - If there is an existing simple Node exactly
160  /// equal to the specified node, return it. Otherwise, insert 'N' and return
161  /// it instead.
162  Node *GetOrInsertNode(Node *N);
163 
164  /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
165  /// return it. If not, return the insertion token that will make insertion
166  /// faster.
167  Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
168 
169  /// InsertNode - Insert the specified node into the folding set, knowing that
170  /// it is not already in the folding set. InsertPos must be obtained from
171  /// FindNodeOrInsertPos.
172  void InsertNode(Node *N, void *InsertPos);
173 
174  /// InsertNode - Insert the specified node into the folding set, knowing that
175  /// it is not already in the folding set.
176  void InsertNode(Node *N) {
177  Node *Inserted = GetOrInsertNode(N);
178  (void)Inserted;
179  assert(Inserted == N && "Node already inserted!");
180  }
181 
182  /// size - Returns the number of nodes in the folding set.
183  unsigned size() const { return NumNodes; }
184 
185  /// empty - Returns true if there are no nodes in the folding set.
186  bool empty() const { return NumNodes == 0; }
187 
188  /// reserve - Increase the number of buckets such that adding the
189  /// EltCount-th node won't cause a rebucket operation. reserve is permitted
190  /// to allocate more space than requested by EltCount.
191  void reserve(unsigned EltCount);
192 
193  /// capacity - Returns the number of nodes permitted in the folding set
194  /// before a rebucket operation is performed.
195  unsigned capacity() {
196  // We allow a load factor of up to 2.0,
197  // so that means our capacity is NumBuckets * 2
198  return NumBuckets * 2;
199  }
200 
201 private:
202  /// GrowHashTable - Double the size of the hash table and rehash everything.
203  void GrowHashTable();
204 
205  /// GrowBucketCount - resize the hash table and rehash everything.
206  /// NewBucketCount must be a power of two, and must be greater than the old
207  /// bucket count.
208  void GrowBucketCount(unsigned NewBucketCount);
209 
210 protected:
211  /// GetNodeProfile - Instantiations of the FoldingSet template implement
212  /// this function to gather data bits for the given node.
213  virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const = 0;
214 
215  /// NodeEquals - Instantiations of the FoldingSet template implement
216  /// this function to compare the given node with the given ID.
217  virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
218  FoldingSetNodeID &TempID) const=0;
219 
220  /// ComputeNodeHash - Instantiations of the FoldingSet template implement
221  /// this function to compute a hash value for the given node.
222  virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const = 0;
223 };
224 
225 //===----------------------------------------------------------------------===//
226 
227 /// DefaultFoldingSetTrait - This class provides default implementations
228 /// for FoldingSetTrait implementations.
229 ///
230 template<typename T> struct DefaultFoldingSetTrait {
231  static void Profile(const T &X, FoldingSetNodeID &ID) {
232  X.Profile(ID);
233  }
234  static void Profile(T &X, FoldingSetNodeID &ID) {
235  X.Profile(ID);
236  }
237 
238  // Equals - Test if the profile for X would match ID, using TempID
239  // to compute a temporary ID if necessary. The default implementation
240  // just calls Profile and does a regular comparison. Implementations
241  // can override this to provide more efficient implementations.
242  static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
243  FoldingSetNodeID &TempID);
244 
245  // ComputeHash - Compute a hash value for X, using TempID to
246  // compute a temporary ID if necessary. The default implementation
247  // just calls Profile and does a regular hash computation.
248  // Implementations can override this to provide more efficient
249  // implementations.
250  static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
251 };
252 
253 /// FoldingSetTrait - This trait class is used to define behavior of how
254 /// to "profile" (in the FoldingSet parlance) an object of a given type.
255 /// The default behavior is to invoke a 'Profile' method on an object, but
256 /// through template specialization the behavior can be tailored for specific
257 /// types. Combined with the FoldingSetNodeWrapper class, one can add objects
258 /// to FoldingSets that were not originally designed to have that behavior.
259 template<typename T> struct FoldingSetTrait
260  : public DefaultFoldingSetTrait<T> {};
261 
262 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
263 /// for ContextualFoldingSets.
264 template<typename T, typename Ctx>
266  static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
267  X.Profile(ID, Context);
268  }
269 
270  static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
271  FoldingSetNodeID &TempID, Ctx Context);
272  static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
273  Ctx Context);
274 };
275 
276 /// ContextualFoldingSetTrait - Like FoldingSetTrait, but for
277 /// ContextualFoldingSets.
278 template<typename T, typename Ctx> struct ContextualFoldingSetTrait
279  : public DefaultContextualFoldingSetTrait<T, Ctx> {};
280 
281 //===--------------------------------------------------------------------===//
282 /// FoldingSetNodeIDRef - This class describes a reference to an interned
283 /// FoldingSetNodeID, which can be a useful to store node id data rather
284 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
285 /// is often much larger than necessary, and the possibility of heap
286 /// allocation means it requires a non-trivial destructor call.
288  const unsigned *Data = nullptr;
289  size_t Size = 0;
290 
291 public:
292  FoldingSetNodeIDRef() = default;
293  FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
294 
295  /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
296  /// used to lookup the node in the FoldingSetImpl.
297  unsigned ComputeHash() const;
298 
299  bool operator==(FoldingSetNodeIDRef) const;
300 
301  bool operator!=(FoldingSetNodeIDRef RHS) const { return !(*this == RHS); }
302 
303  /// Used to compare the "ordering" of two nodes as defined by the
304  /// profiled bits and their ordering defined by memcmp().
305  bool operator<(FoldingSetNodeIDRef) const;
306 
307  const unsigned *getData() const { return Data; }
308  size_t getSize() const { return Size; }
309 };
310 
311 //===--------------------------------------------------------------------===//
312 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
313 /// a node. When all the bits are gathered this class is used to produce a
314 /// hash value for the node.
315 ///
317  /// Bits - Vector of all the data bits that make the node unique.
318  /// Use a SmallVector to avoid a heap allocation in the common case.
320 
321 public:
322  FoldingSetNodeID() = default;
323 
325  : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
326 
327  /// Add* - Add various data types to Bit data.
328  ///
329  void AddPointer(const void *Ptr);
330  void AddInteger(signed I);
331  void AddInteger(unsigned I);
332  void AddInteger(long I);
333  void AddInteger(unsigned long I);
334  void AddInteger(long long I);
335  void AddInteger(unsigned long long I);
336  void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
337  void AddString(StringRef String);
338  void AddNodeID(const FoldingSetNodeID &ID);
339 
340  template <typename T>
341  inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
342 
343  /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
344  /// object to be used to compute a new profile.
345  inline void clear() { Bits.clear(); }
346 
347  /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
348  /// to lookup the node in the FoldingSetImpl.
349  unsigned ComputeHash() const;
350 
351  /// operator== - Used to compare two nodes to each other.
352  ///
353  bool operator==(const FoldingSetNodeID &RHS) const;
354  bool operator==(const FoldingSetNodeIDRef RHS) const;
355 
356  bool operator!=(const FoldingSetNodeID &RHS) const { return !(*this == RHS); }
357  bool operator!=(const FoldingSetNodeIDRef RHS) const { return !(*this ==RHS);}
358 
359  /// Used to compare the "ordering" of two nodes as defined by the
360  /// profiled bits and their ordering defined by memcmp().
361  bool operator<(const FoldingSetNodeID &RHS) const;
362  bool operator<(const FoldingSetNodeIDRef RHS) const;
363 
364  /// Intern - Copy this node's data to a memory region allocated from the
365  /// given allocator and return a FoldingSetNodeIDRef describing the
366  /// interned data.
368 };
369 
370 // Convenience type to hide the implementation of the folding set.
372 template<class T> class FoldingSetIterator;
373 template<class T> class FoldingSetBucketIterator;
374 
375 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
376 // require the definition of FoldingSetNodeID.
377 template<typename T>
378 inline bool
380  unsigned /*IDHash*/,
381  FoldingSetNodeID &TempID) {
382  FoldingSetTrait<T>::Profile(X, TempID);
383  return TempID == ID;
384 }
385 template<typename T>
386 inline unsigned
388  FoldingSetTrait<T>::Profile(X, TempID);
389  return TempID.ComputeHash();
390 }
391 template<typename T, typename Ctx>
392 inline bool
394  const FoldingSetNodeID &ID,
395  unsigned /*IDHash*/,
396  FoldingSetNodeID &TempID,
397  Ctx Context) {
398  ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
399  return TempID == ID;
400 }
401 template<typename T, typename Ctx>
402 inline unsigned
404  FoldingSetNodeID &TempID,
405  Ctx Context) {
406  ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
407  return TempID.ComputeHash();
408 }
409 
410 //===----------------------------------------------------------------------===//
411 /// FoldingSet - This template class is used to instantiate a specialized
412 /// implementation of the folding set to the node class T. T must be a
413 /// subclass of FoldingSetNode and implement a Profile function.
414 ///
415 /// Note that this set type is movable and move-assignable. However, its
416 /// moved-from state is not a valid state for anything other than
417 /// move-assigning and destroying. This is primarily to enable movable APIs
418 /// that incorporate these objects.
419 template <class T> class FoldingSet final : public FoldingSetImpl {
420 private:
421  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
422  /// way to convert nodes into a unique specifier.
423  void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const override {
424  T *TN = static_cast<T *>(N);
426  }
427 
428  /// NodeEquals - Instantiations may optionally provide a way to compare a
429  /// node with a specified ID.
430  bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
431  FoldingSetNodeID &TempID) const override {
432  T *TN = static_cast<T *>(N);
433  return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
434  }
435 
436  /// ComputeNodeHash - Instantiations may optionally provide a way to compute a
437  /// hash value directly from a node.
438  unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const override {
439  T *TN = static_cast<T *>(N);
440  return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
441  }
442 
443 public:
444  explicit FoldingSet(unsigned Log2InitSize = 6)
445  : FoldingSetImpl(Log2InitSize) {}
446 
447  FoldingSet(FoldingSet &&Arg) : FoldingSetImpl(std::move(Arg)) {}
449  (void)FoldingSetImpl::operator=(std::move(RHS));
450  return *this;
451  }
452 
456 
460 
462 
463  bucket_iterator bucket_begin(unsigned hash) {
464  return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
465  }
466 
467  bucket_iterator bucket_end(unsigned hash) {
468  return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
469  }
470 
471  /// GetOrInsertNode - If there is an existing simple Node exactly
472  /// equal to the specified node, return it. Otherwise, insert 'N' and
473  /// return it instead.
475  return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
476  }
477 
478  /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
479  /// return it. If not, return the insertion token that will make insertion
480  /// faster.
481  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
482  return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
483  }
484 };
485 
486 //===----------------------------------------------------------------------===//
487 /// ContextualFoldingSet - This template class is a further refinement
488 /// of FoldingSet which provides a context argument when calling
489 /// Profile on its nodes. Currently, that argument is fixed at
490 /// initialization time.
491 ///
492 /// T must be a subclass of FoldingSetNode and implement a Profile
493 /// function with signature
494 /// void Profile(FoldingSetNodeID &, Ctx);
495 template <class T, class Ctx>
496 class ContextualFoldingSet final : public FoldingSetImpl {
497  // Unfortunately, this can't derive from FoldingSet<T> because the
498  // construction vtable for FoldingSet<T> requires
499  // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
500  // requires a single-argument T::Profile().
501 
502 private:
503  Ctx Context;
504 
505  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
506  /// way to convert nodes into a unique specifier.
507  void GetNodeProfile(FoldingSetImpl::Node *N,
508  FoldingSetNodeID &ID) const override {
509  T *TN = static_cast<T *>(N);
511  }
512 
513  bool NodeEquals(FoldingSetImpl::Node *N, const FoldingSetNodeID &ID,
514  unsigned IDHash, FoldingSetNodeID &TempID) const override {
515  T *TN = static_cast<T *>(N);
516  return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
517  Context);
518  }
519 
520  unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
521  FoldingSetNodeID &TempID) const override {
522  T *TN = static_cast<T *>(N);
523  return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
524  }
525 
526 public:
527  explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
528  : FoldingSetImpl(Log2InitSize), Context(Context)
529  {}
530 
531  Ctx getContext() const { return Context; }
532 
536 
540 
542 
543  bucket_iterator bucket_begin(unsigned hash) {
544  return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
545  }
546 
547  bucket_iterator bucket_end(unsigned hash) {
548  return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
549  }
550 
551  /// GetOrInsertNode - If there is an existing simple Node exactly
552  /// equal to the specified node, return it. Otherwise, insert 'N'
553  /// and return it instead.
555  return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
556  }
557 
558  /// FindNodeOrInsertPos - Look up the node specified by ID. If it
559  /// exists, return it. If not, return the insertion token that will
560  /// make insertion faster.
561  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
562  return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
563  }
564 };
565 
566 //===----------------------------------------------------------------------===//
567 /// FoldingSetVector - This template class combines a FoldingSet and a vector
568 /// to provide the interface of FoldingSet but with deterministic iteration
569 /// order based on the insertion order. T must be a subclass of FoldingSetNode
570 /// and implement a Profile function.
571 template <class T, class VectorT = SmallVector<T*, 8>>
573  FoldingSet<T> Set;
574  VectorT Vector;
575 
576 public:
577  explicit FoldingSetVector(unsigned Log2InitSize = 6)
578  : Set(Log2InitSize) {
579  }
580 
582  iterator begin() { return Vector.begin(); }
583  iterator end() { return Vector.end(); }
584 
586  const_iterator begin() const { return Vector.begin(); }
587  const_iterator end() const { return Vector.end(); }
588 
589  /// clear - Remove all nodes from the folding set.
590  void clear() { Set.clear(); Vector.clear(); }
591 
592  /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
593  /// return it. If not, return the insertion token that will make insertion
594  /// faster.
595  T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
596  return Set.FindNodeOrInsertPos(ID, InsertPos);
597  }
598 
599  /// GetOrInsertNode - If there is an existing simple Node exactly
600  /// equal to the specified node, return it. Otherwise, insert 'N' and
601  /// return it instead.
603  T *Result = Set.GetOrInsertNode(N);
604  if (Result == N) Vector.push_back(N);
605  return Result;
606  }
607 
608  /// InsertNode - Insert the specified node into the folding set, knowing that
609  /// it is not already in the folding set. InsertPos must be obtained from
610  /// FindNodeOrInsertPos.
611  void InsertNode(T *N, void *InsertPos) {
612  Set.InsertNode(N, InsertPos);
613  Vector.push_back(N);
614  }
615 
616  /// InsertNode - Insert the specified node into the folding set, knowing that
617  /// it is not already in the folding set.
618  void InsertNode(T *N) {
619  Set.InsertNode(N);
620  Vector.push_back(N);
621  }
622 
623  /// size - Returns the number of nodes in the folding set.
624  unsigned size() const { return Set.size(); }
625 
626  /// empty - Returns true if there are no nodes in the folding set.
627  bool empty() const { return Set.empty(); }
628 };
629 
630 //===----------------------------------------------------------------------===//
631 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
632 /// folding sets, which knows how to walk the folding set hash table.
634 protected:
636 
637  FoldingSetIteratorImpl(void **Bucket);
638 
639  void advance();
640 
641 public:
642  bool operator==(const FoldingSetIteratorImpl &RHS) const {
643  return NodePtr == RHS.NodePtr;
644  }
645  bool operator!=(const FoldingSetIteratorImpl &RHS) const {
646  return NodePtr != RHS.NodePtr;
647  }
648 };
649 
650 template <class T> class FoldingSetIterator : public FoldingSetIteratorImpl {
651 public:
652  explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
653 
654  T &operator*() const {
655  return *static_cast<T*>(NodePtr);
656  }
657 
658  T *operator->() const {
659  return static_cast<T*>(NodePtr);
660  }
661 
662  inline FoldingSetIterator &operator++() { // Preincrement
663  advance();
664  return *this;
665  }
666  FoldingSetIterator operator++(int) { // Postincrement
667  FoldingSetIterator tmp = *this; ++*this; return tmp;
668  }
669 };
670 
671 //===----------------------------------------------------------------------===//
672 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
673 /// shared by all folding sets, which knows how to walk a particular bucket
674 /// of a folding set hash table.
675 
677 protected:
678  void *Ptr;
679 
680  explicit FoldingSetBucketIteratorImpl(void **Bucket);
681 
682  FoldingSetBucketIteratorImpl(void **Bucket, bool)
683  : Ptr(Bucket) {}
684 
685  void advance() {
686  void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
687  uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
688  Ptr = reinterpret_cast<void*>(x);
689  }
690 
691 public:
692  bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
693  return Ptr == RHS.Ptr;
694  }
695  bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
696  return Ptr != RHS.Ptr;
697  }
698 };
699 
700 template <class T>
701 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
702 public:
703  explicit FoldingSetBucketIterator(void **Bucket) :
705 
706  FoldingSetBucketIterator(void **Bucket, bool) :
708 
709  T &operator*() const { return *static_cast<T*>(Ptr); }
710  T *operator->() const { return static_cast<T*>(Ptr); }
711 
712  inline FoldingSetBucketIterator &operator++() { // Preincrement
713  advance();
714  return *this;
715  }
716  FoldingSetBucketIterator operator++(int) { // Postincrement
717  FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
718  }
719 };
720 
721 //===----------------------------------------------------------------------===//
722 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
723 /// types in an enclosing object so that they can be inserted into FoldingSets.
724 template <typename T>
726  T data;
727 
728 public:
729  template <typename... Ts>
730  explicit FoldingSetNodeWrapper(Ts &&... Args)
731  : data(std::forward<Ts>(Args)...) {}
732 
734 
735  T &getValue() { return data; }
736  const T &getValue() const { return data; }
737 
738  operator T&() { return data; }
739  operator const T&() const { return data; }
740 };
741 
742 //===----------------------------------------------------------------------===//
743 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
744 /// a FoldingSetNodeID value rather than requiring the node to recompute it
745 /// each time it is needed. This trades space for speed (which can be
746 /// significant if the ID is long), and it also permits nodes to drop
747 /// information that would otherwise only be required for recomputing an ID.
749  FoldingSetNodeID FastID;
750 
751 protected:
752  explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
753 
754 public:
755  void Profile(FoldingSetNodeID &ID) const { ID.AddNodeID(FastID); }
756 };
757 
758 //===----------------------------------------------------------------------===//
759 // Partial specializations of FoldingSetTrait.
760 
761 template<typename T> struct FoldingSetTrait<T*> {
762  static inline void Profile(T *X, FoldingSetNodeID &ID) {
763  ID.AddPointer(X);
764  }
765 };
766 template <typename T1, typename T2>
767 struct FoldingSetTrait<std::pair<T1, T2>> {
768  static inline void Profile(const std::pair<T1, T2> &P,
769  FoldingSetNodeID &ID) {
770  ID.Add(P.first);
771  ID.Add(P.second);
772  }
773 };
774 
775 } // end namespace llvm
776 
777 #endif // LLVM_ADT_FOLDINGSET_H
Node * GetOrInsertNode(Node *N)
GetOrInsertNode - If there is an existing simple Node exactly equal to the specified node...
Definition: FoldingSet.cpp:415
FoldingSetVector - This template class combines a FoldingSet and a vector to provide the interface of...
Definition: FoldingSet.h:572
FoldingSetIterator< T > iterator
Definition: FoldingSet.h:453
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
FoldingSetNodeIDRef(const unsigned *D, size_t S)
Definition: FoldingSet.h:293
LLVMContext & Context
FoldingSetBucketIterator< T > bucket_iterator
Definition: FoldingSet.h:461
ContextualFoldingSet(Ctx Context, unsigned Log2InitSize=6)
Definition: FoldingSet.h:527
FastFoldingSetNode - This is a subclass of FoldingSetNode which stores a FoldingSetNodeID value rathe...
Definition: FoldingSet.h:748
FoldingSetIterator< const T > const_iterator
Definition: FoldingSet.h:537
iterator end()
Definition: FoldingSet.h:455
bucket_iterator bucket_begin(unsigned hash)
Definition: FoldingSet.h:463
FastFoldingSetNode(const FoldingSetNodeID &ID)
Definition: FoldingSet.h:752
void ** Buckets
Buckets - Array of bucket chains.
Definition: FoldingSet.h:119
void InsertNode(Node *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.cpp:345
FoldingSetImpl & operator=(FoldingSetImpl &&RHS)
Definition: FoldingSet.cpp:243
static void Profile(const T &X, FoldingSetNodeID &ID)
Definition: FoldingSet.h:231
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
FoldingSet(unsigned Log2InitSize=6)
Definition: FoldingSet.h:444
void InsertNode(Node *N)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.h:176
pointee_iterator< typename VectorT::const_iterator > const_iterator
Definition: FoldingSet.h:585
unsigned ComputeHash() const
ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, used to lookup the node in th...
Definition: FoldingSet.cpp:30
static void Profile(T *X, FoldingSetNodeID &ID)
Definition: FoldingSet.h:762
ContextualFoldingSetTrait - Like FoldingSetTrait, but for ContextualFoldingSets.
Definition: FoldingSet.h:278
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
static unsigned ComputeHash(T &X, FoldingSetNodeID &TempID)
Definition: FoldingSet.h:387
virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const =0
GetNodeProfile - Instantiations of the FoldingSet template implement this function to gather data bit...
T * GetOrInsertNode(Node *N)
GetOrInsertNode - If there is an existing simple Node exactly equal to the specified node...
Definition: FoldingSet.h:474
size_t getSize() const
Definition: FoldingSet.h:308
void AddInteger(signed I)
Definition: FoldingSet.cpp:61
FoldingSetIteratorImpl - This is the common iterator support shared by all folding sets...
Definition: FoldingSet.h:633
const_iterator end() const
Definition: FoldingSet.h:459
bool operator==(const FoldingSetNodeID &RHS) const
operator== - Used to compare two nodes to each other.
Definition: FoldingSet.cpp:152
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
const_iterator end() const
Definition: FoldingSet.h:539
const_iterator begin() const
Definition: FoldingSet.h:586
void Profile(FoldingSetNodeID &ID)
Definition: FoldingSet.h:733
void clear()
clear - Remove all nodes from the folding set.
Definition: FoldingSet.h:590
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:316
#define P(N)
virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const =0
ComputeNodeHash - Instantiations of the FoldingSet template implement this function to compute a hash...
FoldingSetTrait - This trait class is used to define behavior of how to "profile" (in the FoldingSet ...
Definition: FoldingSet.h:259
void AddNodeID(const FoldingSetNodeID &ID)
Definition: FoldingSet.cpp:140
const T & getValue() const
Definition: FoldingSet.h:736
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
void * getNextInBucket() const
Definition: FoldingSet.h:148
void Profile(FoldingSetNodeID &ID) const
Definition: FoldingSet.h:755
static unsigned ComputeHash(T &X, FoldingSetNodeID &TempID, Ctx Context)
Definition: FoldingSet.h:403
FoldingSetNode * NodePtr
Definition: FoldingSet.h:635
bool operator<(FoldingSetNodeIDRef) const
Used to compare the "ordering" of two nodes as defined by the profiled bits and their ordering define...
Definition: FoldingSet.cpp:41
void AddBoolean(bool B)
Definition: FoldingSet.h:336
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:481
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:595
FoldingSetBucketIterator operator++(int)
Definition: FoldingSet.h:716
FoldingSetNodeWrapper(Ts &&...Args)
Definition: FoldingSet.h:730
bool empty() const
empty - Returns true if there are no nodes in the folding set.
Definition: FoldingSet.h:186
Greedy Register Allocator
Node * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.cpp:320
FoldingSetImpl - Implements the folding set functionality.
Definition: FoldingSet.h:113
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:561
FoldingSetIterator operator++(int)
Definition: FoldingSet.h:666
unsigned size() const
size - Returns the number of nodes in the folding set.
Definition: FoldingSet.h:624
unsigned NumNodes
NumNodes - Number of nodes in the folding set.
Definition: FoldingSet.h:127
FoldingSetBucketIterator & operator++()
Definition: FoldingSet.h:712
bucket_iterator bucket_end(unsigned hash)
Definition: FoldingSet.h:467
void InsertNode(T *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.h:611
FoldingSetImpl(unsigned Log2InitSize=6)
Definition: FoldingSet.cpp:228
FoldingSetIterator< T > iterator
Definition: FoldingSet.h:533
FoldingSetVector(unsigned Log2InitSize=6)
Definition: FoldingSet.h:577
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:419
bool RemoveNode(Node *N)
RemoveNode - Remove a node from the folding set, returning true if one was removed or false if the no...
Definition: FoldingSet.cpp:374
static bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
Definition: FoldingSet.h:379
bool operator!=(const FoldingSetNodeID &RHS) const
Definition: FoldingSet.h:356
bool empty() const
empty - Returns true if there are no nodes in the folding set.
Definition: FoldingSet.h:627
void Add(const T &x)
Definition: FoldingSet.h:341
FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary types in an enclosing object ...
Definition: FoldingSet.h:725
bool operator<(const FoldingSetNodeID &RHS) const
Used to compare the "ordering" of two nodes as defined by the profiled bits and their ordering define...
Definition: FoldingSet.cpp:164
void InsertNode(T *N)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.h:618
iterator begin()
Definition: FoldingSet.h:454
unsigned size() const
size - Returns the number of nodes in the folding set.
Definition: FoldingSet.h:183
FoldingSetNodeID(FoldingSetNodeIDRef Ref)
Definition: FoldingSet.h:324
void SetNextInBucket(void *N)
Definition: FoldingSet.h:149
void clear()
clear - Clear the accumulated profile, allowing this FoldingSetNodeID object to be used to compute a ...
Definition: FoldingSet.h:345
FoldingSetBucketIterator(void **Bucket)
Definition: FoldingSet.h:703
bool operator!=(const FoldingSetNodeIDRef RHS) const
Definition: FoldingSet.h:357
FoldingSetBucketIterator(void **Bucket, bool)
Definition: FoldingSet.h:706
FoldingSet & operator=(FoldingSet &&RHS)
Definition: FoldingSet.h:448
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:273
FoldingSetImpl::Node FoldingSetNode
Definition: FoldingSet.h:371
ContextualFoldingSet - This template class is a further refinement of FoldingSet which provides a con...
Definition: FoldingSet.h:496
T & operator*() const
Definition: FoldingSet.h:654
T * GetOrInsertNode(Node *N)
GetOrInsertNode - If there is an existing simple Node exactly equal to the specified node...
Definition: FoldingSet.h:554
FoldingSetIterator & operator++()
Definition: FoldingSet.h:662
unsigned capacity()
capacity - Returns the number of nodes permitted in the folding set before a rebucket operation is pe...
Definition: FoldingSet.h:195
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:139
unsigned NumBuckets
NumBuckets - Length of the Buckets array.
Definition: FoldingSet.h:123
static void Profile(T &X, FoldingSetNodeID &ID)
Definition: FoldingSet.h:234
const unsigned * getData() const
Definition: FoldingSet.h:307
T * GetOrInsertNode(T *N)
GetOrInsertNode - If there is an existing simple Node exactly equal to the specified node...
Definition: FoldingSet.h:602
Basic Alias true
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node id data rather than using plain FoldingSetNodeIDs, since the 32-element SmallVector is often much larger than necessary, and the possibility of heap allocation means it requires a non-trivial destructor call.
Definition: FoldingSet.h:287
FoldingSetIteratorImpl(void **Bucket)
Definition: FoldingSet.cpp:428
unsigned ComputeHash() const
ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to lookup the node in the F...
Definition: FoldingSet.cpp:146
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
const_iterator end() const
Definition: FoldingSet.h:587
bucket_iterator bucket_end(unsigned hash)
Definition: FoldingSet.h:547
void AddString(StringRef String)
Definition: FoldingSet.cpp:87
bool operator==(const FoldingSetIteratorImpl &RHS) const
Definition: FoldingSet.h:642
pointee_iterator< typename VectorT::iterator > iterator
Definition: FoldingSet.h:581
DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but for ContextualFoldingSets.
Definition: FoldingSet.h:265
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const_iterator begin() const
Definition: FoldingSet.h:538
FoldingSetBucketIteratorImpl(void **Bucket)
Definition: FoldingSet.cpp:460
T * operator->() const
Definition: FoldingSet.h:658
DefaultFoldingSetTrait - This class provides default implementations for FoldingSetTrait implementati...
Definition: FoldingSet.h:230
FoldingSetIterator(void **Bucket)
Definition: FoldingSet.h:652
bool operator!=(FoldingSetNodeIDRef RHS) const
Definition: FoldingSet.h:301
void reserve(unsigned EltCount)
reserve - Increase the number of buckets such that adding the EltCount-th node won't cause a rebucket...
Definition: FoldingSet.cpp:307
FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const
Intern - Copy this node's data to a memory region allocated from the given allocator and return a Fol...
Definition: FoldingSet.cpp:176
bucket_iterator bucket_begin(unsigned hash)
Definition: FoldingSet.h:543
virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID) const =0
NodeEquals - Instantiations of the FoldingSet template implement this function to compare the given n...
static void Profile(const std::pair< T1, T2 > &P, FoldingSetNodeID &ID)
Definition: FoldingSet.h:768
const_iterator begin() const
Definition: FoldingSet.h:458
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
int * Ptr
bool operator==(const FoldingSetBucketIteratorImpl &RHS) const
Definition: FoldingSet.h:692
void clear()
clear - Remove all nodes from the folding set.
Definition: FoldingSet.cpp:258
FoldingSet(FoldingSet &&Arg)
Definition: FoldingSet.h:447
FoldingSetBucketIteratorImpl(void **Bucket, bool)
Definition: FoldingSet.h:682
bool operator!=(const FoldingSetIteratorImpl &RHS) const
Definition: FoldingSet.h:645
FoldingSetBucketIteratorImpl - This is the common bucket iterator support shared by all folding sets...
Definition: FoldingSet.h:676
FoldingSetBucketIterator< T > bucket_iterator
Definition: FoldingSet.h:541
bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const
Definition: FoldingSet.h:695
bool operator==(FoldingSetNodeIDRef) const
Definition: FoldingSet.cpp:34
FoldingSetIterator< const T > const_iterator
Definition: FoldingSet.h:457
static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context)
Definition: FoldingSet.h:266
static bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID, Ctx Context)
Definition: FoldingSet.h:393