LLVM 24.0.0git
FoldingSet.h
Go to the documentation of this file.
1//===- llvm/ADT/FoldingSet.h - Uniquing Hash Set ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
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
13/// set.
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_FOLDINGSET_H
17#define LLVM_ADT_FOLDINGSET_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/Hashing.h"
23#include "llvm/ADT/iterator.h"
26#include "llvm/Support/xxhash.h"
27#include <cassert>
28#include <cstddef>
29#include <cstdint>
30#include <type_traits>
31#include <utility>
32
33namespace llvm {
34
35/// This folding set used for two purposes:
36/// 1. Given information about a node we want to create, look up the unique
37/// instance of the node in the set. If the node already exists, return
38/// it, otherwise return the bucket it should be inserted into.
39/// 2. Given a node that has already been created, remove it from the set.
40///
41/// This class is implemented as a single-link chained hash table, where the
42/// "buckets" are actually the nodes themselves (the next pointer is in the
43/// node). The last node points back to the bucket to simplify node removal.
44///
45/// Any node that is to be included in the folding set must be a subclass of
46/// FoldingSetNode. The node class must also define a Profile method used to
47/// establish the unique bits of data for the node. The Profile method is
48/// passed a FoldingSetNodeID object which is used to gather the bits. Just
49/// call one of the Add* functions defined in the FoldingSetBase::NodeID class.
50/// NOTE: That the folding set does not own the nodes and it is the
51/// responsibility of the user to dispose of the nodes.
52///
53/// Eg.
54/// class MyNode : public FoldingSetNode {
55/// private:
56/// std::string Name;
57/// unsigned Value;
58/// public:
59/// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
60/// ...
61/// void Profile(FoldingSetNodeID &ID) const {
62/// ID.AddString(Name);
63/// ID.AddInteger(Value);
64/// }
65/// ...
66/// };
67///
68/// To define the folding set itself use the FoldingSet template;
69///
70/// Eg.
71/// FoldingSet<MyNode> MyFoldingSet;
72///
73/// Four public methods are available to manipulate the folding set;
74///
75/// 1) If you have an existing node that you want add to the set but unsure
76/// that the node might already exist then call;
77///
78/// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
79///
80/// If The result is equal to the input then the node has been inserted.
81/// Otherwise, the result is the node existing in the folding set, and the
82/// input can be discarded (use the result instead.)
83///
84/// 2) If you are ready to construct a node but want to check if it already
85/// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
86/// check;
87///
88/// FoldingSetNodeID ID;
89/// ID.AddString(Name);
90/// ID.AddInteger(Value);
91/// void *InsertPoint;
92///
93/// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
94///
95/// If found then M will be non-NULL, else InsertPoint will point to where it
96/// should be inserted using InsertNode.
97///
98/// 3) If you get a NULL result from FindNodeOrInsertPos then you can insert a
99/// new node with InsertNode;
100///
101/// MyFoldingSet.InsertNode(M, InsertPoint);
102///
103/// 4) Finally, if you want to remove a node from the folding set call;
104///
105/// bool WasRemoved = MyFoldingSet.RemoveNode(M);
106///
107/// The result indicates whether the node existed in the folding set.
108
109class FoldingSetNodeID;
110class StringRef;
111
112//===----------------------------------------------------------------------===//
113
114/// This class provides default implementations for FoldingSetTrait
115/// implementations.
116template<typename T> struct DefaultFoldingSetTrait {
117 static void Profile(const T &X, FoldingSetNodeID &ID) {
118 X.Profile(ID);
119 }
120 static void Profile(T &X, FoldingSetNodeID &ID) {
121 X.Profile(ID);
122 }
123
124 // Equals - Test if the profile for X would match ID, using TempID
125 // to compute a temporary ID if necessary. The default implementation
126 // just calls Profile and does a regular comparison. Implementations
127 // can override this to provide more efficient implementations.
128 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
129 FoldingSetNodeID &TempID);
130
131 // ComputeHash - Compute a hash value for X, using TempID to
132 // compute a temporary ID if necessary. The default implementation
133 // just calls Profile and does a regular hash computation.
134 // Implementations can override this to provide more efficient
135 // implementations.
136 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
137};
138
139/// This trait class is used to define behavior of how to "profile" (in the
140/// FoldingSet parlance) an object of a given type.
141/// The default behavior is to invoke a 'Profile' method on an object, but
142/// through template specialization the behavior can be tailored for specific
143/// types. Combined with the FoldingSetNodeWrapper class, one can add objects
144/// to FoldingSets that were not originally designed to have that behavior.
145template <typename T, typename Enable = void>
147
148/// Like DefaultFoldingSetTrait, but for ContextualFoldingSets.
149template<typename T, typename Ctx>
151 static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
152 X.Profile(ID, Context);
153 }
154
155 static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
156 FoldingSetNodeID &TempID, Ctx Context);
157 static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
158 Ctx Context);
159};
160
161/// Like FoldingSetTrait, but for ContextualFoldingSets.
162template<typename T, typename Ctx> struct ContextualFoldingSetTrait
163 : public DefaultContextualFoldingSetTrait<T, Ctx> {};
164
165//===--------------------------------------------------------------------===//
166/// This class describes a reference to an interned FoldingSetNodeID, which can
167/// be a useful to store node id data rather than using plain FoldingSetNodeIDs,
168/// since the 32-element SmallVector is often much larger than necessary, and
169/// the possibility of heap allocation means it requires a non-trivial
170/// destructor call.
172 const unsigned *Data = nullptr;
173 size_t Size = 0;
174
175public:
177 FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
178
179 // Compute a strong hash value used to lookup the node in the FoldingSetBase.
180 // The hash value is not guaranteed to be deterministic across processes.
181 unsigned ComputeHash() const {
182 return static_cast<unsigned>(hash_combine_range(Data, Data + Size));
183 }
184
185 // Compute a deterministic hash value across processes that is suitable for
186 // on-disk serialization.
187 unsigned computeStableHash() const {
188 return static_cast<unsigned>(xxh3_64bits(
189 reinterpret_cast<const uint8_t *>(Data), sizeof(unsigned) * Size));
190 }
191
193
194 bool operator!=(FoldingSetNodeIDRef RHS) const { return !(*this == RHS); }
195
196 /// Used to compare the "ordering" of two nodes as defined by the
197 /// profiled bits and their ordering defined by memcmp().
199
200 const unsigned *getData() const { return Data; }
201 size_t getSize() const { return Size; }
202};
203
204//===--------------------------------------------------------------------===//
205/// This class is used to gather all the unique data bits of a node. When all
206/// the bits are gathered this class is used to produce a hash value for the
207/// node.
209 /// Vector of all the data bits that make the node unique.
210 /// Use a SmallVector to avoid a heap allocation in the common case.
212
213 template <typename T> void AddIntegerImpl(T I) {
214 static_assert(std::is_integral_v<T> && sizeof(T) <= sizeof(unsigned) * 2,
215 "T must be an integer type no wider than 64 bits");
216 Bits.push_back(static_cast<unsigned>(I));
217 if constexpr (sizeof(unsigned) < sizeof(T))
218 Bits.push_back(static_cast<unsigned long long>(I) >> 32);
219 }
220
221public:
222 FoldingSetNodeID() = default;
223
225 : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
226
227 /// Add* - Add various data types to Bit data.
228 void AddPointer(const void *Ptr) {
229 // Note: this adds pointers to the hash using sizes and endianness that
230 // depend on the host. It doesn't matter, however, because hashing on
231 // pointer values is inherently unstable. Nothing should depend on the
232 // ordering of nodes in the folding set.
233 static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long),
234 "unexpected pointer size");
235 AddInteger(reinterpret_cast<uintptr_t>(Ptr));
236 }
237 void AddInteger(signed I) { AddIntegerImpl(I); }
238 void AddInteger(unsigned I) { AddIntegerImpl(I); }
239 void AddInteger(long I) { AddIntegerImpl(I); }
240 void AddInteger(unsigned long I) { AddIntegerImpl(I); }
241 void AddInteger(long long I) { AddIntegerImpl(I); }
242 void AddInteger(unsigned long long I) { AddIntegerImpl(I); }
243 void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
245 LLVM_ABI void AddNodeID(const FoldingSetNodeID &ID);
246
247 template <typename T>
248 inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
249
250 /// Clear the accumulated profile, allowing this FoldingSetNodeID
251 /// object to be used to compute a new profile.
252 inline void clear() { Bits.clear(); }
253
254 // Compute a strong hash value for this FoldingSetNodeID, used to lookup the
255 // node in the FoldingSetBase. The hash value is not guaranteed to be
256 // deterministic across processes.
257 unsigned ComputeHash() const {
258 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
259 }
260
261 // Compute a deterministic hash value across processes that is suitable for
262 // on-disk serialization.
263 unsigned computeStableHash() const {
264 return FoldingSetNodeIDRef(Bits.data(), Bits.size()).computeStableHash();
265 }
266
267 /// operator== - Used to compare two nodes to each other.
268 LLVM_ABI bool operator==(const FoldingSetNodeID &RHS) const;
269 LLVM_ABI bool operator==(const FoldingSetNodeIDRef RHS) const;
270
271 bool operator!=(const FoldingSetNodeID &RHS) const { return !(*this == RHS); }
272 bool operator!=(const FoldingSetNodeIDRef RHS) const { return !(*this ==RHS);}
273
274 /// Used to compare the "ordering" of two nodes as defined by the
275 /// profiled bits and their ordering defined by memcmp().
276 LLVM_ABI bool operator<(const FoldingSetNodeID &RHS) const;
277 LLVM_ABI bool operator<(const FoldingSetNodeIDRef RHS) const;
278
279 /// Copy this node's data to a memory region allocated from the
280 /// given allocator and return a FoldingSetNodeIDRef describing the
281 /// interned data.
283};
284
285//===----------------------------------------------------------------------===//
286/// Implements the folding set functionality. The main structure is an array of
287/// buckets. Each bucket is indexed by the hash of the nodes it contains. The
288/// bucket itself points to the nodes contained in the bucket via a singly
289/// linked list. The last node in the list points back to the bucket to
290/// facilitate node removal.
291///
293protected:
294 /// Array of bucket chains.
295 void **Buckets;
296
297 /// Length of the Buckets array. Always a power of 2.
298 unsigned NumBuckets;
299
300 /// Number of nodes in the folding set. Growth occurs when NumNodes
301 /// is greater than twice the number of buckets.
302 unsigned NumNodes;
303
304 LLVM_ABI explicit FoldingSetBase(unsigned Log2InitSize = 6);
308
309public:
310 //===--------------------------------------------------------------------===//
311 /// This class is used to maintain the singly linked bucket list in
312 /// a folding set.
313 class Node {
314 private:
315 // NextInFoldingSetBucket - next link in the bucket list.
316 void *NextInFoldingSetBucket = nullptr;
317
318 public:
319 Node() = default;
320
321 // Accessors
322 void *getNextInBucket() const { return NextInFoldingSetBucket; }
323 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
324 };
325
326 /// Remove all nodes from the folding set.
327 LLVM_ABI void clear();
328
329 /// Returns the number of nodes in the folding set.
330 unsigned size() const { return NumNodes; }
331
332 /// Returns true if there are no nodes in the folding set.
333 bool empty() const { return NumNodes == 0; }
334
335 /// Returns the number of nodes permitted in the folding set
336 /// before a rebucket operation is performed.
337 unsigned capacity() {
338 // We allow a load factor of up to 2.0,
339 // so that means our capacity is NumBuckets * 2
340 return NumBuckets * 2;
341 }
342
343protected:
344 /// Functions provided by the derived class to compute folding properties.
345 /// This is effectively a vtable for FoldingSetBase, except that we don't
346 /// actually store a pointer to it in the object.
348 /// Instantiations of the FoldingSet template implement this function to
349 /// gather data bits for the given node.
350 void (*GetNodeProfile)(const FoldingSetBase *Self, Node *N,
351 FoldingSetNodeID &ID);
352
353 /// Instantiations of the FoldingSet template implement this function to
354 /// compare the given node with the given ID.
356 const FoldingSetNodeID &ID, unsigned IDHash,
357 FoldingSetNodeID &TempID);
358
359 /// Instantiations of the FoldingSet template implement this function to
360 /// compute a hash value for the given node.
362 FoldingSetNodeID &TempID);
363 };
364
365private:
366 /// Double the size of the hash table and rehash everything.
367 void GrowHashTable(const FoldingSetInfo &Info);
368
369 /// Resize the hash table and rehash everything. \p NewBucketCount must be a
370 /// power of two, and must be greater than the old bucket count.
371 void GrowBucketCount(unsigned NewBucketCount, const FoldingSetInfo &Info);
372
373protected:
374 // The below methods are protected to encourage subclasses to provide a more
375 // type-safe API.
376
377 /// Increase the number of buckets such that adding the \p EltCount th node
378 /// won't cause a rebucket operation. reserve is permitted to allocate more
379 /// space than requested by EltCount.
380 LLVM_ABI void reserve(unsigned EltCount, const FoldingSetInfo &Info);
381
382 /// Remove a node from the folding set, returning true if one
383 /// was removed or false if the node was not in the folding set.
384 LLVM_ABI bool RemoveNode(Node *N);
385
386 /// If there is an existing simple Node exactly equal to the node \p N,
387 /// return it. Otherwise, insert \p N and return it instead.
389
390 /// Look up the node specified by ID. If it exists, return it. If not,
391 /// return the insertion token that will make insertion faster.
393 void *&InsertPos,
394 const FoldingSetInfo &Info);
395
396 /// Insert the specified node into the folding set, knowing that
397 /// it is not already in the folding set. InsertPos must be obtained from
398 /// FindNodeOrInsertPos.
399 LLVM_ABI void InsertNode(Node *N, void *InsertPos,
400 const FoldingSetInfo &Info);
401};
402
403// Convenience type to hide the implementation of the folding set.
405template <class T> class FoldingSetIterator;
406
407// Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
408// require the definition of FoldingSetNodeID.
409template<typename T>
410inline bool
412 unsigned /*IDHash*/,
413 FoldingSetNodeID &TempID) {
415 return TempID == ID;
416}
417template<typename T>
418inline unsigned
423template<typename T, typename Ctx>
424inline bool
426 const FoldingSetNodeID &ID,
427 unsigned /*IDHash*/,
428 FoldingSetNodeID &TempID,
429 Ctx Context) {
431 return TempID == ID;
432}
433template<typename T, typename Ctx>
434inline unsigned
436 FoldingSetNodeID &TempID,
437 Ctx Context) {
439 return TempID.ComputeHash();
440}
441
442//===----------------------------------------------------------------------===//
443/// An implementation detail that lets us share code between FoldingSet and
444/// ContextualFoldingSet.
445template <class Derived, class T> class FoldingSetImpl : public FoldingSetBase {
446protected:
447 explicit FoldingSetImpl(unsigned Log2InitSize)
448 : FoldingSetBase(Log2InitSize) {}
449
452 ~FoldingSetImpl() = default;
453
454public:
456
459
461
464
465 /// Increase the number of buckets such that adding the \p EltCount th node
466 /// won't cause a rebucket operation. reserve is permitted to allocate more
467 /// space than requested by EltCount.
468 void reserve(unsigned EltCount) {
469 return FoldingSetBase::reserve(EltCount, Derived::getFoldingSetInfo());
470 }
471
472 /// Remove a node from the folding set, returning true if one
473 /// was removed or false if the node was not in the folding set.
474 bool RemoveNode(T *N) {
476 }
477
478 /// If there is an existing simple Node exactly equal to the specified node,
479 /// return it. Otherwise, insert 'N' and return it instead.
481 return static_cast<T *>(
482 FoldingSetBase::GetOrInsertNode(N, Derived::getFoldingSetInfo()));
483 }
484
485 /// Look up the node specified by ID. If it exists, return it. If not,
486 /// return the insertion token that will make insertion faster.
487 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
488 return static_cast<T *>(FoldingSetBase::FindNodeOrInsertPos(
489 ID, InsertPos, Derived::getFoldingSetInfo()));
490 }
491
492 /// Insert the specified node into the folding set, knowing that
493 /// it is not already in the folding set. InsertPos must be obtained from
494 /// FindNodeOrInsertPos.
495 void InsertNode(T *N, void *InsertPos) {
496 FoldingSetBase::InsertNode(N, InsertPos, Derived::getFoldingSetInfo());
497 }
498
499 /// Insert the specified node into the folding set, knowing that it is not
500 /// already in the folding set.
501 void InsertNode(T *N) {
502 T *Inserted = GetOrInsertNode(N);
503 (void)Inserted;
504 assert(Inserted == N && "Node already inserted!");
505 }
506};
507
508//===----------------------------------------------------------------------===//
509/// This template class is used to instantiate a specialized
510/// implementation of the folding set to the node class T. T must be a
511/// subclass of FoldingSetNode and implement a Profile function.
512///
513/// Note that this set type is movable and move-assignable. However, its
514/// moved-from state is not a valid state for anything other than
515/// move-assigning and destroying. This is primarily to enable movable APIs
516/// that incorporate these objects.
517template <class T>
518class FoldingSet : public FoldingSetImpl<FoldingSet<T>, T> {
519 using Super = FoldingSetImpl<FoldingSet, T>;
520 using Node = typename Super::Node;
521
522 /// Each instantiation of the FoldingSet needs to provide a
523 /// way to convert nodes into a unique specifier.
524 static void GetNodeProfile(const FoldingSetBase *, Node *N,
525 FoldingSetNodeID &ID) {
526 T *TN = static_cast<T *>(N);
528 }
529
530 /// Instantiations may optionally provide a way to compare a
531 /// node with a specified ID.
532 static bool NodeEquals(const FoldingSetBase *, Node *N,
533 const FoldingSetNodeID &ID, unsigned IDHash,
534 FoldingSetNodeID &TempID) {
535 T *TN = static_cast<T *>(N);
536 return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
537 }
538
539 /// Instantiations may optionally provide a way to compute a
540 /// hash value directly from a node.
541 static unsigned ComputeNodeHash(const FoldingSetBase *, Node *N,
542 FoldingSetNodeID &TempID) {
543 T *TN = static_cast<T *>(N);
544 return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
545 }
546
547 static const FoldingSetBase::FoldingSetInfo &getFoldingSetInfo() {
548 static constexpr FoldingSetBase::FoldingSetInfo Info = {
549 GetNodeProfile, NodeEquals, ComputeNodeHash};
550 return Info;
551 }
552 friend Super;
553
554public:
555 explicit FoldingSet(unsigned Log2InitSize = 6) : Super(Log2InitSize) {}
556 FoldingSet(FoldingSet &&Arg) = default;
558};
559
560//===----------------------------------------------------------------------===//
561/// This template class is a further refinement of FoldingSet which provides a
562/// context argument when calling Profile on its nodes. Currently, that
563/// argument is fixed at initialization time.
564///
565/// T must be a subclass of FoldingSetNode and implement a Profile
566/// function with signature
567/// void Profile(FoldingSetNodeID &, Ctx);
568template <class T, class Ctx>
570 : public FoldingSetImpl<ContextualFoldingSet<T, Ctx>, T> {
571 // Unfortunately, this can't derive from FoldingSet<T> because the
572 // construction of the vtable for FoldingSet<T> requires
573 // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
574 // requires a single-argument T::Profile().
575
577 using Node = typename Super::Node;
578
579 Ctx Context;
580
581 static const Ctx &getContext(const FoldingSetBase *Base) {
582 return static_cast<const ContextualFoldingSet*>(Base)->Context;
583 }
584
585 /// Each instantiatation of the FoldingSet needs to provide a way to convert
586 /// nodes into a unique specifier.
587 static void GetNodeProfile(const FoldingSetBase *Base, Node *N,
588 FoldingSetNodeID &ID) {
589 T *TN = static_cast<T *>(N);
591 }
592
593 static bool NodeEquals(const FoldingSetBase *Base, Node *N,
594 const FoldingSetNodeID &ID, unsigned IDHash,
595 FoldingSetNodeID &TempID) {
596 T *TN = static_cast<T *>(N);
597 return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
599 }
600
601 static unsigned ComputeNodeHash(const FoldingSetBase *Base, Node *N,
602 FoldingSetNodeID &TempID) {
603 T *TN = static_cast<T *>(N);
606 }
607
608 static const FoldingSetBase::FoldingSetInfo &getFoldingSetInfo() {
609 static constexpr FoldingSetBase::FoldingSetInfo Info = {
610 GetNodeProfile, NodeEquals, ComputeNodeHash};
611 return Info;
612 }
613 friend Super;
614
615public:
616 explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
617 : Super(Log2InitSize), Context(Context) {}
618
619 Ctx getContext() const { return Context; }
620};
621
622//===----------------------------------------------------------------------===//
623/// This template class combines a FoldingSet and a vector to provide the
624/// interface of FoldingSet but with deterministic iteration order based on the
625/// insertion order. T must be a subclass of FoldingSetNode and implement a
626/// Profile function.
627template <class T, class VectorT = SmallVector<T*, 8>>
629 FoldingSet<T> Set;
630 VectorT Vector;
631
632public:
633 explicit FoldingSetVector(unsigned Log2InitSize = 6) : Set(Log2InitSize) {}
634
636
637 iterator begin() { return Vector.begin(); }
638 iterator end() { return Vector.end(); }
639
641
642 const_iterator begin() const { return Vector.begin(); }
643 const_iterator end() const { return Vector.end(); }
644
645 /// Remove all nodes from the folding set.
646 void clear() { Set.clear(); Vector.clear(); }
647
648 /// Look up the node specified by ID. If it exists, return it. If not,
649 /// return the insertion token that will make insertion faster.
650 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
651 return Set.FindNodeOrInsertPos(ID, InsertPos);
652 }
653
654 /// If there is an existing simple Node exactly equal to the specified node,
655 /// return it. Otherwise, insert 'N' and return it instead.
657 T *Result = Set.GetOrInsertNode(N);
658 if (Result == N) Vector.push_back(N);
659 return Result;
660 }
661
662 /// Insert the specified node into the folding set, knowing that
663 /// it is not already in the folding set. InsertPos must be obtained from
664 /// FindNodeOrInsertPos.
665 void InsertNode(T *N, void *InsertPos) {
666 Set.InsertNode(N, InsertPos);
667 Vector.push_back(N);
668 }
669
670 /// Insert the specified node into the folding set, knowing that
671 /// it is not already in the folding set.
672 void InsertNode(T *N) {
673 Set.InsertNode(N);
674 Vector.push_back(N);
675 }
676
677 /// Returns the number of nodes in the folding set.
678 unsigned size() const { return Set.size(); }
679
680 /// Returns true if there are no nodes in the folding set.
681 bool empty() const { return Set.empty(); }
682};
683
684//===----------------------------------------------------------------------===//
685/// This is the common iterator support shared by all folding sets, which knows
686/// how to walk the folding set hash table.
688protected:
690
691 LLVM_ABI FoldingSetIteratorImpl(void **Bucket);
692
693 LLVM_ABI void advance();
694
695public:
697 return NodePtr == RHS.NodePtr;
698 }
700 return NodePtr != RHS.NodePtr;
701 }
702};
703
704template <class T> class FoldingSetIterator : public FoldingSetIteratorImpl {
705public:
706 explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
707
708 T &operator*() const {
709 return *static_cast<T*>(NodePtr);
710 }
711
712 T *operator->() const {
713 return static_cast<T*>(NodePtr);
714 }
715
716 inline FoldingSetIterator &operator++() { // Preincrement
717 advance();
718 return *this;
719 }
720 FoldingSetIterator operator++(int) { // Postincrement
721 FoldingSetIterator tmp = *this; ++*this; return tmp;
722 }
723};
724
725//===----------------------------------------------------------------------===//
726/// This template class is used to "wrap" arbitrary types in an enclosing object
727/// so that they can be inserted into FoldingSets.
728template <typename T>
730 T data;
731
732public:
733 template <typename... Ts>
734 explicit FoldingSetNodeWrapper(Ts &&... Args)
735 : data(std::forward<Ts>(Args)...) {}
736
738
739 T &getValue() { return data; }
740 const T &getValue() const { return data; }
741
742 operator T&() { return data; }
743 operator const T&() const { return data; }
744};
745
746//===----------------------------------------------------------------------===//
747/// This is a subclass of FoldingSetNode which stores a FoldingSetNodeID value
748/// rather than requiring the node to recompute it each time it is needed. This
749/// trades space for speed (which can be significant if the ID is long), and it
750/// also permits nodes to drop information that would otherwise only be required
751/// for recomputing an ID.
753 FoldingSetNodeID FastID;
754
755protected:
756 explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
757
758public:
759 void Profile(FoldingSetNodeID &ID) const { ID.AddNodeID(FastID); }
760};
761
762//===----------------------------------------------------------------------===//
763// Partial specializations of FoldingSetTrait.
764
765template<typename T> struct FoldingSetTrait<T*> {
766 static inline void Profile(T *X, FoldingSetNodeID &ID) {
767 ID.AddPointer(X);
768 }
769};
770template <typename T1, typename T2>
771struct FoldingSetTrait<std::pair<T1, T2>> {
772 static inline void Profile(const std::pair<T1, T2> &P,
773 FoldingSetNodeID &ID) {
774 ID.Add(P.first);
775 ID.Add(P.second);
776 }
777};
778
779template <typename T>
780struct FoldingSetTrait<T, std::enable_if_t<std::is_enum<T>::value>> {
781 static void Profile(const T &X, FoldingSetNodeID &ID) {
782 ID.AddInteger(llvm::to_underlying(X));
783 }
784};
785
786} // end namespace llvm
787
788#endif // LLVM_ADT_FOLDINGSET_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define P(N)
Basic Register Allocator
This file contains library features backported from future STL versions.
This file defines the SmallVector class.
Value * RHS
static unsigned getSize(unsigned Kind)
ContextualFoldingSet(Ctx Context, unsigned Log2InitSize=6)
Definition FoldingSet.h:616
FastFoldingSetNode(const FoldingSetNodeID &ID)
Definition FoldingSet.h:756
void Profile(FoldingSetNodeID &ID) const
Definition FoldingSet.h:759
This class is used to maintain the singly linked bucket list in a folding set.
Definition FoldingSet.h:313
void * getNextInBucket() const
Definition FoldingSet.h:322
void SetNextInBucket(void *N)
Definition FoldingSet.h:323
Implements the folding set functionality.
Definition FoldingSet.h:292
LLVM_ABI FoldingSetBase(unsigned Log2InitSize=6)
void ** Buckets
Array of bucket chains.
Definition FoldingSet.h:295
unsigned size() const
Returns the number of nodes in the folding set.
Definition FoldingSet.h:330
LLVM_ABI void reserve(unsigned EltCount, const FoldingSetInfo &Info)
Increase the number of buckets such that adding the EltCount th node won't cause a rebucket operation...
LLVM_ABI bool RemoveNode(Node *N)
Remove a node from the folding set, returning true if one was removed or false if the node was not in...
LLVM_ABI FoldingSetBase & operator=(FoldingSetBase &&RHS)
LLVM_ABI ~FoldingSetBase()
unsigned NumBuckets
Length of the Buckets array. Always a power of 2.
Definition FoldingSet.h:298
unsigned NumNodes
Number of nodes in the folding set.
Definition FoldingSet.h:302
unsigned capacity()
Returns the number of nodes permitted in the folding set before a rebucket operation is performed.
Definition FoldingSet.h:337
LLVM_ABI Node * GetOrInsertNode(Node *N, const FoldingSetInfo &Info)
If there is an existing simple Node exactly equal to the node N, return it.
bool empty() const
Returns true if there are no nodes in the folding set.
Definition FoldingSet.h:333
LLVM_ABI void InsertNode(Node *N, void *InsertPos, const FoldingSetInfo &Info)
Insert the specified node into the folding set, knowing that it is not already in the folding set.
LLVM_ABI void clear()
Remove all nodes from the folding set.
LLVM_ABI Node * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos, const FoldingSetInfo &Info)
Look up the node specified by ID.
void reserve(unsigned EltCount)
Increase the number of buckets such that adding the EltCount th node won't cause a rebucket operation...
Definition FoldingSet.h:468
FoldingSetIterator< T > iterator
Definition FoldingSet.h:455
const_iterator end() const
Definition FoldingSet.h:463
bool RemoveNode(T *N)
Remove a node from the folding set, returning true if one was removed or false if the node was not in...
Definition FoldingSet.h:474
~FoldingSetImpl()=default
FoldingSetImpl(FoldingSetImpl &&Arg)=default
void InsertNode(T *N)
Insert the specified node into the folding set, knowing that it is not already in the folding set.
Definition FoldingSet.h:501
void InsertNode(T *N, void *InsertPos)
Insert the specified node into the folding set, knowing that it is not already in the folding set.
Definition FoldingSet.h:495
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
Look up the node specified by ID.
Definition FoldingSet.h:487
const_iterator begin() const
Definition FoldingSet.h:462
FoldingSetIterator< const T > const_iterator
Definition FoldingSet.h:460
FoldingSetImpl & operator=(FoldingSetImpl &&RHS)=default
T * GetOrInsertNode(T *N)
If there is an existing simple Node exactly equal to the specified node, return it.
Definition FoldingSet.h:480
FoldingSetImpl(unsigned Log2InitSize)
Definition FoldingSet.h:447
LLVM_ABI FoldingSetIteratorImpl(void **Bucket)
bool operator==(const FoldingSetIteratorImpl &RHS) const
Definition FoldingSet.h:696
bool operator!=(const FoldingSetIteratorImpl &RHS) const
Definition FoldingSet.h:699
FoldingSetIterator(void **Bucket)
Definition FoldingSet.h:706
FoldingSetIterator operator++(int)
Definition FoldingSet.h:720
FoldingSetIterator & operator++()
Definition FoldingSet.h:716
This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node...
Definition FoldingSet.h:171
unsigned computeStableHash() const
Definition FoldingSet.h:187
LLVM_ABI bool operator==(FoldingSetNodeIDRef) const
FoldingSetNodeIDRef(const unsigned *D, size_t S)
Definition FoldingSet.h:177
LLVM_ABI bool operator<(FoldingSetNodeIDRef) const
Used to compare the "ordering" of two nodes as defined by the profiled bits and their ordering define...
bool operator!=(FoldingSetNodeIDRef RHS) const
Definition FoldingSet.h:194
unsigned ComputeHash() const
Definition FoldingSet.h:181
const unsigned * getData() const
Definition FoldingSet.h:200
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:208
LLVM_ABI FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const
Copy this node's data to a memory region allocated from the given allocator and return a FoldingSetNo...
void AddInteger(signed I)
Definition FoldingSet.h:237
void AddInteger(unsigned long I)
Definition FoldingSet.h:240
FoldingSetNodeID(FoldingSetNodeIDRef Ref)
Definition FoldingSet.h:224
unsigned computeStableHash() const
Definition FoldingSet.h:263
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition FoldingSet.h:228
bool operator!=(const FoldingSetNodeIDRef RHS) const
Definition FoldingSet.h:272
void clear()
Clear the accumulated profile, allowing this FoldingSetNodeID object to be used to compute a new prof...
Definition FoldingSet.h:252
void AddInteger(unsigned I)
Definition FoldingSet.h:238
void AddInteger(long I)
Definition FoldingSet.h:239
void AddBoolean(bool B)
Definition FoldingSet.h:243
LLVM_ABI bool operator==(const FoldingSetNodeID &RHS) const
operator== - Used to compare two nodes to each other.
bool operator!=(const FoldingSetNodeID &RHS) const
Definition FoldingSet.h:271
void AddInteger(unsigned long long I)
Definition FoldingSet.h:242
void AddInteger(long long I)
Definition FoldingSet.h:241
unsigned ComputeHash() const
Definition FoldingSet.h:257
LLVM_ABI bool operator<(const FoldingSetNodeID &RHS) const
Used to compare the "ordering" of two nodes as defined by the profiled bits and their ordering define...
LLVM_ABI void AddNodeID(const FoldingSetNodeID &ID)
void Add(const T &x)
Definition FoldingSet.h:248
LLVM_ABI void AddString(StringRef String)
Add* - Add various data types to Bit data.
FoldingSetNodeWrapper(Ts &&... Args)
Definition FoldingSet.h:734
const T & getValue() const
Definition FoldingSet.h:740
void Profile(FoldingSetNodeID &ID)
Definition FoldingSet.h:737
T * GetOrInsertNode(T *N)
If there is an existing simple Node exactly equal to the specified node, return it.
Definition FoldingSet.h:656
const_iterator end() const
Definition FoldingSet.h:643
void InsertNode(T *N)
Insert the specified node into the folding set, knowing that it is not already in the folding set.
Definition FoldingSet.h:672
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
Look up the node specified by ID.
Definition FoldingSet.h:650
unsigned size() const
Returns the number of nodes in the folding set.
Definition FoldingSet.h:678
pointee_iterator< typename VectorT::const_iterator > const_iterator
Definition FoldingSet.h:640
pointee_iterator< typename VectorT::iterator > iterator
Definition FoldingSet.h:635
void clear()
Remove all nodes from the folding set.
Definition FoldingSet.h:646
bool empty() const
Returns true if there are no nodes in the folding set.
Definition FoldingSet.h:681
FoldingSetVector(unsigned Log2InitSize=6)
Definition FoldingSet.h:633
void InsertNode(T *N, void *InsertPos)
Insert the specified node into the folding set, knowing that it is not already in the folding set.
Definition FoldingSet.h:665
const_iterator begin() const
Definition FoldingSet.h:642
This template class is used to instantiate a specialized implementation of the folding set to the nod...
Definition FoldingSet.h:518
FoldingSet(FoldingSet &&Arg)=default
FoldingSet(unsigned Log2InitSize=6)
Definition FoldingSet.h:555
FoldingSet & operator=(FoldingSet &&RHS)=default
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
uint64_t xxh3_64bits(ArrayRef< uint8_t > data)
Inline ArrayRef overloads of the xxhash entry points declared out-of-line in llvm/Support/xxhash....
Definition ArrayRef.h:558
FoldingSetBase::Node FoldingSetNode
Definition FoldingSet.h:404
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:390
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:285
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
#define N
Like FoldingSetTrait, but for ContextualFoldingSets.
Definition FoldingSet.h:163
Like DefaultFoldingSetTrait, but for ContextualFoldingSets.
Definition FoldingSet.h:150
static bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID, Ctx Context)
Definition FoldingSet.h:425
static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context)
Definition FoldingSet.h:151
static unsigned ComputeHash(T &X, FoldingSetNodeID &TempID, Ctx Context)
Definition FoldingSet.h:435
This class provides default implementations for FoldingSetTrait implementations.
Definition FoldingSet.h:116
static void Profile(const T &X, FoldingSetNodeID &ID)
Definition FoldingSet.h:117
static unsigned ComputeHash(T &X, FoldingSetNodeID &TempID)
Definition FoldingSet.h:419
static bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
Definition FoldingSet.h:411
static void Profile(T &X, FoldingSetNodeID &ID)
Definition FoldingSet.h:120
Functions provided by the derived class to compute folding properties.
Definition FoldingSet.h:347
unsigned(* ComputeNodeHash)(const FoldingSetBase *Self, Node *N, FoldingSetNodeID &TempID)
Instantiations of the FoldingSet template implement this function to compute a hash value for the giv...
Definition FoldingSet.h:361
bool(* NodeEquals)(const FoldingSetBase *Self, Node *N, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
Instantiations of the FoldingSet template implement this function to compare the given node with the ...
Definition FoldingSet.h:355
void(* GetNodeProfile)(const FoldingSetBase *Self, Node *N, FoldingSetNodeID &ID)
Instantiations of the FoldingSet template implement this function to gather data bits for the given n...
Definition FoldingSet.h:350
static void Profile(T *X, FoldingSetNodeID &ID)
Definition FoldingSet.h:766
static void Profile(const std::pair< T1, T2 > &P, FoldingSetNodeID &ID)
Definition FoldingSet.h:772
This trait class is used to define behavior of how to "profile" (in the FoldingSet parlance) an objec...
Definition FoldingSet.h:146
An iterator type that allows iterating over the pointees via some other iterator.
Definition iterator.h:329