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