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