LLVM 23.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); }
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,
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;
406template<class T> class FoldingSetBucketIterator;
407
408// Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
409// require the definition of FoldingSetNodeID.
410template<typename T>
411inline bool
413 unsigned /*IDHash*/,
414 FoldingSetNodeID &TempID) {
416 return TempID == ID;
417}
418template<typename T>
419inline unsigned
424template<typename T, typename Ctx>
425inline bool
427 const FoldingSetNodeID &ID,
428 unsigned /*IDHash*/,
429 FoldingSetNodeID &TempID,
430 Ctx Context) {
432 return TempID == ID;
433}
434template<typename T, typename Ctx>
435inline unsigned
437 FoldingSetNodeID &TempID,
438 Ctx Context) {
440 return TempID.ComputeHash();
441}
442
443//===----------------------------------------------------------------------===//
444/// An implementation detail that lets us share code between FoldingSet and
445/// ContextualFoldingSet.
446template <class Derived, class T> class FoldingSetImpl : public FoldingSetBase {
447protected:
448 explicit FoldingSetImpl(unsigned Log2InitSize)
449 : FoldingSetBase(Log2InitSize) {}
450
453 ~FoldingSetImpl() = default;
454
455public:
457
460
462
465
467
469 return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
470 }
471
473 return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
474 }
475
476 /// Increase the number of buckets such that adding the \p EltCount th node
477 /// won't cause a rebucket operation. reserve is permitted to allocate more
478 /// space than requested by EltCount.
479 void reserve(unsigned EltCount) {
480 return FoldingSetBase::reserve(EltCount, Derived::getFoldingSetInfo());
481 }
482
483 /// Remove a node from the folding set, returning true if one
484 /// was removed or false if the node was not in the folding set.
485 bool RemoveNode(T *N) {
487 }
488
489 /// If there is an existing simple Node exactly equal to the specified node,
490 /// return it. Otherwise, insert 'N' and return it instead.
492 return static_cast<T *>(
493 FoldingSetBase::GetOrInsertNode(N, Derived::getFoldingSetInfo()));
494 }
495
496 /// Look up the node specified by ID. If it exists, return it. If not,
497 /// return the insertion token that will make insertion faster.
498 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
499 return static_cast<T *>(FoldingSetBase::FindNodeOrInsertPos(
500 ID, InsertPos, Derived::getFoldingSetInfo()));
501 }
502
503 /// Insert the specified node into the folding set, knowing that
504 /// it is not already in the folding set. InsertPos must be obtained from
505 /// FindNodeOrInsertPos.
506 void InsertNode(T *N, void *InsertPos) {
507 FoldingSetBase::InsertNode(N, InsertPos, Derived::getFoldingSetInfo());
508 }
509
510 /// Insert the specified node into the folding set, knowing that it is not
511 /// already in the folding set.
512 void InsertNode(T *N) {
513 T *Inserted = GetOrInsertNode(N);
514 (void)Inserted;
515 assert(Inserted == N && "Node already inserted!");
516 }
517};
518
519//===----------------------------------------------------------------------===//
520/// This template class is used to instantiate a specialized
521/// implementation of the folding set to the node class T. T must be a
522/// subclass of FoldingSetNode and implement a Profile function.
523///
524/// Note that this set type is movable and move-assignable. However, its
525/// moved-from state is not a valid state for anything other than
526/// move-assigning and destroying. This is primarily to enable movable APIs
527/// that incorporate these objects.
528template <class T>
529class FoldingSet : public FoldingSetImpl<FoldingSet<T>, T> {
530 using Super = FoldingSetImpl<FoldingSet, T>;
531 using Node = typename Super::Node;
532
533 /// Each instantiation of the FoldingSet needs to provide a
534 /// way to convert nodes into a unique specifier.
535 static void GetNodeProfile(const FoldingSetBase *, Node *N,
537 T *TN = static_cast<T *>(N);
539 }
540
541 /// Instantiations may optionally provide a way to compare a
542 /// node with a specified ID.
543 static bool NodeEquals(const FoldingSetBase *, Node *N,
544 const FoldingSetNodeID &ID, unsigned IDHash,
545 FoldingSetNodeID &TempID) {
546 T *TN = static_cast<T *>(N);
547 return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
548 }
549
550 /// Instantiations may optionally provide a way to compute a
551 /// hash value directly from a node.
552 static unsigned ComputeNodeHash(const FoldingSetBase *, Node *N,
553 FoldingSetNodeID &TempID) {
554 T *TN = static_cast<T *>(N);
555 return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
556 }
557
558 static const FoldingSetBase::FoldingSetInfo &getFoldingSetInfo() {
559 static constexpr FoldingSetBase::FoldingSetInfo Info = {
560 GetNodeProfile, NodeEquals, ComputeNodeHash};
561 return Info;
562 }
563 friend Super;
564
565public:
566 explicit FoldingSet(unsigned Log2InitSize = 6) : Super(Log2InitSize) {}
567 FoldingSet(FoldingSet &&Arg) = default;
569};
570
571//===----------------------------------------------------------------------===//
572/// This template class is a further refinement of FoldingSet which provides a
573/// context argument when calling Profile on its nodes. Currently, that
574/// argument is fixed at initialization time.
575///
576/// T must be a subclass of FoldingSetNode and implement a Profile
577/// function with signature
578/// void Profile(FoldingSetNodeID &, Ctx);
579template <class T, class Ctx>
581 : public FoldingSetImpl<ContextualFoldingSet<T, Ctx>, T> {
582 // Unfortunately, this can't derive from FoldingSet<T> because the
583 // construction of the vtable for FoldingSet<T> requires
584 // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
585 // requires a single-argument T::Profile().
586
588 using Node = typename Super::Node;
589
590 Ctx Context;
591
592 static const Ctx &getContext(const FoldingSetBase *Base) {
593 return static_cast<const ContextualFoldingSet*>(Base)->Context;
594 }
595
596 /// Each instantiatation of the FoldingSet needs to provide a way to convert
597 /// nodes into a unique specifier.
598 static void GetNodeProfile(const FoldingSetBase *Base, Node *N,
600 T *TN = static_cast<T *>(N);
602 }
603
604 static bool NodeEquals(const FoldingSetBase *Base, Node *N,
605 const FoldingSetNodeID &ID, unsigned IDHash,
606 FoldingSetNodeID &TempID) {
607 T *TN = static_cast<T *>(N);
608 return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
610 }
611
612 static unsigned ComputeNodeHash(const FoldingSetBase *Base, Node *N,
613 FoldingSetNodeID &TempID) {
614 T *TN = static_cast<T *>(N);
617 }
618
619 static const FoldingSetBase::FoldingSetInfo &getFoldingSetInfo() {
620 static constexpr FoldingSetBase::FoldingSetInfo Info = {
621 GetNodeProfile, NodeEquals, ComputeNodeHash};
622 return Info;
623 }
624 friend Super;
625
626public:
627 explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
628 : Super(Log2InitSize), Context(Context) {}
629
630 Ctx getContext() const { return Context; }
631};
632
633//===----------------------------------------------------------------------===//
634/// This template class combines a FoldingSet and a vector to provide the
635/// interface of FoldingSet but with deterministic iteration order based on the
636/// insertion order. T must be a subclass of FoldingSetNode and implement a
637/// Profile function.
638template <class T, class VectorT = SmallVector<T*, 8>>
640 FoldingSet<T> Set;
641 VectorT Vector;
642
643public:
644 explicit FoldingSetVector(unsigned Log2InitSize = 6) : Set(Log2InitSize) {}
645
647
648 iterator begin() { return Vector.begin(); }
649 iterator end() { return Vector.end(); }
650
652
653 const_iterator begin() const { return Vector.begin(); }
654 const_iterator end() const { return Vector.end(); }
655
656 /// Remove all nodes from the folding set.
657 void clear() { Set.clear(); Vector.clear(); }
658
659 /// Look up the node specified by ID. If it exists, return it. If not,
660 /// return the insertion token that will make insertion faster.
661 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
662 return Set.FindNodeOrInsertPos(ID, InsertPos);
663 }
664
665 /// If there is an existing simple Node exactly equal to the specified node,
666 /// return it. Otherwise, insert 'N' and return it instead.
668 T *Result = Set.GetOrInsertNode(N);
669 if (Result == N) Vector.push_back(N);
670 return Result;
671 }
672
673 /// Insert the specified node into the folding set, knowing that
674 /// it is not already in the folding set. InsertPos must be obtained from
675 /// FindNodeOrInsertPos.
676 void InsertNode(T *N, void *InsertPos) {
677 Set.InsertNode(N, InsertPos);
678 Vector.push_back(N);
679 }
680
681 /// Insert the specified node into the folding set, knowing that
682 /// it is not already in the folding set.
683 void InsertNode(T *N) {
684 Set.InsertNode(N);
685 Vector.push_back(N);
686 }
687
688 /// Returns the number of nodes in the folding set.
689 unsigned size() const { return Set.size(); }
690
691 /// Returns true if there are no nodes in the folding set.
692 bool empty() const { return Set.empty(); }
693};
694
695//===----------------------------------------------------------------------===//
696/// This is the common iterator support shared by all folding sets, which knows
697/// how to walk the folding set hash table.
699protected:
701
702 LLVM_ABI FoldingSetIteratorImpl(void **Bucket);
703
704 LLVM_ABI void advance();
705
706public:
708 return NodePtr == RHS.NodePtr;
709 }
711 return NodePtr != RHS.NodePtr;
712 }
713};
714
715template <class T> class FoldingSetIterator : public FoldingSetIteratorImpl {
716public:
717 explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
718
719 T &operator*() const {
720 return *static_cast<T*>(NodePtr);
721 }
722
723 T *operator->() const {
724 return static_cast<T*>(NodePtr);
725 }
726
727 inline FoldingSetIterator &operator++() { // Preincrement
728 advance();
729 return *this;
730 }
731 FoldingSetIterator operator++(int) { // Postincrement
732 FoldingSetIterator tmp = *this; ++*this; return tmp;
733 }
734};
735
736//===----------------------------------------------------------------------===//
737/// This is the common bucket iterator support shared by all folding sets, which
738/// knows how to walk a particular bucket of a folding set hash table.
740protected:
741 void *Ptr;
742
743 LLVM_ABI explicit FoldingSetBucketIteratorImpl(void **Bucket);
744
745 FoldingSetBucketIteratorImpl(void **Bucket, bool) : Ptr(Bucket) {}
746
747 void advance() {
748 void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
749 uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
750 Ptr = reinterpret_cast<void*>(x);
751 }
752
753public:
755 return Ptr == RHS.Ptr;
756 }
758 return Ptr != RHS.Ptr;
759 }
760};
761
762template <class T>
764public:
765 explicit FoldingSetBucketIterator(void **Bucket) :
767
768 FoldingSetBucketIterator(void **Bucket, bool) :
770
771 T &operator*() const { return *static_cast<T*>(Ptr); }
772 T *operator->() const { return static_cast<T*>(Ptr); }
773
774 inline FoldingSetBucketIterator &operator++() { // Preincrement
775 advance();
776 return *this;
777 }
778 FoldingSetBucketIterator operator++(int) { // Postincrement
779 FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
780 }
781};
782
783//===----------------------------------------------------------------------===//
784/// This template class is used to "wrap" arbitrary types in an enclosing object
785/// so that they can be inserted into FoldingSets.
786template <typename T>
788 T data;
789
790public:
791 template <typename... Ts>
792 explicit FoldingSetNodeWrapper(Ts &&... Args)
793 : data(std::forward<Ts>(Args)...) {}
794
796
797 T &getValue() { return data; }
798 const T &getValue() const { return data; }
799
800 operator T&() { return data; }
801 operator const T&() const { return data; }
802};
803
804//===----------------------------------------------------------------------===//
805/// This is a subclass of FoldingSetNode which stores a FoldingSetNodeID value
806/// rather than requiring the node to recompute it each time it is needed. This
807/// trades space for speed (which can be significant if the ID is long), and it
808/// also permits nodes to drop information that would otherwise only be required
809/// for recomputing an ID.
811 FoldingSetNodeID FastID;
812
813protected:
814 explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
815
816public:
817 void Profile(FoldingSetNodeID &ID) const { ID.AddNodeID(FastID); }
818};
819
820//===----------------------------------------------------------------------===//
821// Partial specializations of FoldingSetTrait.
822
823template<typename T> struct FoldingSetTrait<T*> {
824 static inline void Profile(T *X, FoldingSetNodeID &ID) {
825 ID.AddPointer(X);
826 }
827};
828template <typename T1, typename T2>
829struct FoldingSetTrait<std::pair<T1, T2>> {
830 static inline void Profile(const std::pair<T1, T2> &P,
832 ID.Add(P.first);
833 ID.Add(P.second);
834 }
835};
836
837template <typename T>
838struct FoldingSetTrait<T, std::enable_if_t<std::is_enum<T>::value>> {
839 static void Profile(const T &X, FoldingSetNodeID &ID) {
840 ID.AddInteger(llvm::to_underlying(X));
841 }
842};
843
844} // end namespace llvm
845
846#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:853
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:213
#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:627
FastFoldingSetNode(const FoldingSetNodeID &ID)
Definition FoldingSet.h:814
void Profile(FoldingSetNodeID &ID) const
Definition FoldingSet.h:817
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.
LLVM_ABI FoldingSetBucketIteratorImpl(void **Bucket)
FoldingSetBucketIteratorImpl(void **Bucket, bool)
Definition FoldingSet.h:745
bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const
Definition FoldingSet.h:757
bool operator==(const FoldingSetBucketIteratorImpl &RHS) const
Definition FoldingSet.h:754
FoldingSetBucketIterator(void **Bucket, bool)
Definition FoldingSet.h:768
FoldingSetBucketIterator(void **Bucket)
Definition FoldingSet.h:765
FoldingSetBucketIterator operator++(int)
Definition FoldingSet.h:778
FoldingSetBucketIterator & operator++()
Definition FoldingSet.h:774
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:479
FoldingSetIterator< T > iterator
Definition FoldingSet.h:456
const_iterator end() const
Definition FoldingSet.h:464
bucket_iterator bucket_begin(unsigned hash)
Definition FoldingSet.h:468
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:485
~FoldingSetImpl()=default
FoldingSetImpl(FoldingSetImpl &&Arg)=default
FoldingSetBucketIterator< T > bucket_iterator
Definition FoldingSet.h:466
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:512
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:506
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
Look up the node specified by ID.
Definition FoldingSet.h:498
const_iterator begin() const
Definition FoldingSet.h:463
FoldingSetIterator< const T > const_iterator
Definition FoldingSet.h:461
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:491
FoldingSetImpl(unsigned Log2InitSize)
Definition FoldingSet.h:448
bucket_iterator bucket_end(unsigned hash)
Definition FoldingSet.h:472
LLVM_ABI FoldingSetIteratorImpl(void **Bucket)
bool operator==(const FoldingSetIteratorImpl &RHS) const
Definition FoldingSet.h:707
bool operator!=(const FoldingSetIteratorImpl &RHS) const
Definition FoldingSet.h:710
FoldingSetIterator(void **Bucket)
Definition FoldingSet.h:717
FoldingSetIterator operator++(int)
Definition FoldingSet.h:731
FoldingSetIterator & operator++()
Definition FoldingSet.h:727
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:792
const T & getValue() const
Definition FoldingSet.h:798
void Profile(FoldingSetNodeID &ID)
Definition FoldingSet.h:795
T * GetOrInsertNode(T *N)
If there is an existing simple Node exactly equal to the specified node, return it.
Definition FoldingSet.h:667
const_iterator end() const
Definition FoldingSet.h:654
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:683
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
Look up the node specified by ID.
Definition FoldingSet.h:661
unsigned size() const
Returns the number of nodes in the folding set.
Definition FoldingSet.h:689
pointee_iterator< typename VectorT::const_iterator > const_iterator
Definition FoldingSet.h:651
pointee_iterator< typename VectorT::iterator > iterator
Definition FoldingSet.h:646
void clear()
Remove all nodes from the folding set.
Definition FoldingSet.h:657
bool empty() const
Returns true if there are no nodes in the folding set.
Definition FoldingSet.h:692
FoldingSetVector(unsigned Log2InitSize=6)
Definition FoldingSet.h:644
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:676
const_iterator begin() const
Definition FoldingSet.h:653
This template class is used to instantiate a specialized implementation of the folding set to the nod...
Definition FoldingSet.h:529
FoldingSet(FoldingSet &&Arg)=default
FoldingSet(unsigned Log2InitSize=6)
Definition FoldingSet.h:566
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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:383
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:305
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
#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:426
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:436
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:420
static bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
Definition FoldingSet.h:412
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:824
static void Profile(const std::pair< T1, T2 > &P, FoldingSetNodeID &ID)
Definition FoldingSet.h:830
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