14 #ifndef LLVM_ADT_IMMUTABLESET_H
15 #define LLVM_ADT_IMMUTABLESET_H
41 template <
typename ImutInfo >
81 else if (ImutInfo::isLess(K,CurrentKey))
121 ImutInfo::KeyOfValue(V)))
125 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(
getValue()),
126 ImutInfo::DataOfValue(V)))
146 while (LItr != LEnd && RItr != REnd) {
147 if (&*LItr == &*RItr) {
160 return LItr == LEnd && RItr == REnd;
175 template <
typename Callback>
176 void foreach(Callback&
C) {
199 &&
"Height calculation wrong");
201 assert((HL > HR ? HL-HR : HR-HL) <= 2
202 &&
"Balancing invariant violated");
206 ImutInfo::KeyOfValue(
getValue()))) &&
207 "Value in left child is not less that current value");
211 ImutInfo::isLess(ImutInfo::KeyOfValue(
getValue()),
213 "Current value is not less that value of right child");
229 unsigned height : 28;
231 bool IsDigestCached : 1;
232 bool IsCanonicalized : 1;
247 : factory(f), left(l), right(r), height(height), IsMutable(
true),
248 IsDigestCached(
false), IsCanonicalized(
false), value(v)
251 if (right) right->
retain();
260 bool isMutable()
const {
return IsMutable; }
264 bool hasCachedDigest()
const {
return IsDigestCached; }
279 void markImmutable() {
280 assert(isMutable() &&
"Mutable flag already removed.");
285 void markedCachedDigest() {
286 assert(!hasCachedDigest() &&
"NoCachedDigest flag already removed.");
287 IsDigestCached =
true;
292 void setHeight(
unsigned h) {
293 assert(isMutable() &&
"Only a mutable tree can have its height changed.");
297 static uint32_t computeDigest(ImutAVLTree *
L, ImutAVLTree *R,
302 digest += L->computeDigest();
306 ImutInfo::Profile(ID,V);
307 digest += ID.ComputeHash();
310 digest += R->computeDigest();
318 if (hasCachedDigest())
323 markedCachedDigest();
345 if (IsCanonicalized) {
358 factory->freeNodes.push_back(
this);
366 template <
typename ImutInfo >
367 class ImutAVLFactory {
377 std::vector<TreeTy*> createdNodes;
378 std::vector<TreeTy*> freeNodes;
380 bool ownsAllocator()
const {
397 :
Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
400 if (ownsAllocator())
delete &getAllocator();
439 return (hl > hr ? hl : hr) + 1;
446 for ( ; I!=
E ; ++
I, ++TI) {
466 if (!freeNodes.empty()) {
467 T = freeNodes.back();
468 freeNodes.pop_back();
475 createdNodes.push_back(T);
484 for (
unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
486 if (N->isMutable() && N->refCount == 0)
489 createdNodes.clear();
499 assert(!
isEmpty(L) &&
"Left tree cannot be empty to have a height >= 2");
507 assert(!
isEmpty(LR) &&
"LR cannot be empty because it has a height >= 1");
516 assert(!
isEmpty(R) &&
"Right tree cannot be empty to have a height >= 2");
524 assert(!
isEmpty(RL) &&
"RL cannot be empty because it has a height >= 1");
543 key_type_ref K = ImutInfo::KeyOfValue(V);
544 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
548 else if (ImutInfo::isLess(K,KCurrent))
564 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
568 }
else if (ImutInfo::isLess(K,KCurrent)) {
600 if (!T || !T->isMutable())
612 if (TNew->IsCanonicalized)
617 unsigned digest = TNew->computeDigest();
622 for (
TreeTy *
T = entry ;
T !=
nullptr;
T =
T->next) {
630 if (TNew->refCount == 0)
640 TNew->IsCanonicalized =
true;
649 template <
typename ImutInfo>
650 class ImutAVLTreeGenericIterator
651 :
public std::iterator<std::bidirectional_iterator_tag,
652 ImutAVLTree<ImutInfo>> {
653 SmallVector<uintptr_t,20> stack;
663 if (Root) stack.
push_back(reinterpret_cast<uintptr_t>(Root));
701 return stack == x.stack;
705 return !(*
this == x);
715 stack.
push_back(reinterpret_cast<uintptr_t>(L));
721 stack.
push_back(reinterpret_cast<uintptr_t>(R));
760 template <
typename ImutInfo>
761 class ImutAVLTreeInOrderIterator
762 :
public std::iterator<std::bidirectional_iterator_tag,
763 ImutAVLTree<ImutInfo>> {
764 using InternalIteratorTy = ImutAVLTreeGenericIterator<ImutInfo>;
766 InternalIteratorTy InternalItr;
779 return InternalItr == x.InternalItr;
783 return !(*
this == x);
791 while (!InternalItr.atEnd() &&
792 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
799 while (!InternalItr.atBeginning() &&
800 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
806 InternalItr.skipToParent();
808 while (!InternalItr.atEnd() &&
809 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
816 template <
typename T>
819 ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
820 typename std::iterator_traits<
821 typename T::TreeTy::iterator>::iterator_category,
822 const typename T::value_type> {
827 typename ImutAVLValueIterator::reference
operator*()
const {
828 return this->
I->getValue();
839 template <
typename T>
850 template <
typename T>
860 #define PROFILE_INTEGER_INFO(X)\
861 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {};
874 #undef PROFILE_INTEGER_INFO
889 template <
typename T>
909 template <
typename T>
922 return std::equal_to<key_type>()(LHS,RHS);
926 return std::less<key_type>()(LHS,RHS);
935 template <
typename T>
958 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT>>
974 if (Root) { Root->
retain(); }
978 if (Root) { Root->
retain(); }
986 if (Root != X.Root) {
987 if (X.Root) { X.Root->
retain(); }
996 const bool Canonicalize;
1000 : Canonicalize(canonicalize) {}
1003 : F(Alloc), Canonicalize(canonicalize) {}
1048 return Root ? Root->
contains(V) :
false;
1052 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1056 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
1060 if (Root) { Root->
retain(); }
1075 template <
typename Callback>
1076 void foreach(Callback& C) {
if (Root) Root->
foreach(C); }
1078 template <
typename Callback>
1079 void foreach() {
if (Root) { Callback
C; Root->
foreach(C); } }
1110 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT>>
1130 if (Root) { Root->
retain(); }
1135 Factory(X.Factory) {
1136 if (Root) { Root->
retain(); }
1140 if (Root) { Root->
release(); }
1144 if (Root != X.Root) {
1145 if (X.Root) { X.Root->
retain(); }
1146 if (Root) { Root->
release(); }
1148 Factory = X.Factory;
1167 return Root ? Root->
contains(V) :
false;
1172 Factory->getCanonicalTree(Root) : Root);
1180 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1184 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
1224 #endif // LLVM_ADT_IMMUTABLESET_H
Generic iterator that wraps a T::TreeTy::iterator and exposes iterator::getValue() on dereference...
unsigned getHeight() const
getHeight - Returns the height of the tree.
void validateTree() const
ImmutableSet add(ImmutableSet Old, value_type_ref V)
add - Creates a new immutable set that contains all of the values of the original set with the additi...
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
ImmutableSetRef(const ImmutableSetRef &X)
void push_back(const T &Elt)
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
ImutAVLValueIterator< ImmutableSet > iterator
ImutAVLValueIterator()=default
unsigned validateTree() const
validateTree - A utility method that checks that the balancing and ordering invariants of the tree ar...
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static bool isDataEqual(data_type_ref, data_type_ref)
Generic profile trait for pointer types.
TreeTy * balanceTree(TreeTy *L, value_type_ref V, TreeTy *R)
balanceTree - Used by add_internal and remove_internal to balance a newly created tree...
ImutAVLTree * getLeft() const
Return a pointer to the left subtree.
typename ImutInfo::value_type value_type
static data_type_ref DataOfValue(value_type_ref)
bool operator==(const ImmutableSetRef &RHS) const
TreeTy * add(TreeTy *T, value_type_ref V)
typename ImutInfo::key_type_ref key_type_ref
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
BumpPtrAllocator & getAllocator()
TreeTy * remove_internal(key_type_ref K, TreeTy *T)
remove_internal - Creates a new tree that includes all the data from the original tree except the spe...
ImutAVLTreeInOrderIterator & operator--()
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
typename TreeTy::Factory FactoryTy
void Profile(FoldingSetNodeID &ID) const
TreeTy * getRootWithoutRetain() const
TreeTy & operator*() const
TreeTy * createNode(TreeTy *newLeft, TreeTy *oldTree, TreeTy *newRight)
ImutAVLTreeGenericIterator(const TreeTy *Root)
bool isNotEqual(const ImutAVLTree &RHS) const
isNotEqual - Compares two trees for structural inequality.
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
static void Profile(const T &X, FoldingSetNodeID &ID)
ImmutableSet & operator=(const ImmutableSet &X)
TreeTy::Factory * getTreeFactory() const
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
bool isElementEqual(const ImutAVLTree *RHS) const
ImutAVLTree * getMaxElement()
getMaxElement - Find the subtree associated with the highest ranged key value.
static ImmutableSetRef getEmptySet(FactoryTy *F)
TreeTy * createNode(TreeTy *L, value_type_ref V, TreeTy *R)
ImutAVLTreeInOrderIterator & operator++()
Generic profile template.
void AddInteger(signed I)
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
TreeTy * operator->() const
static void Profile(FoldingSetNodeID &ID, const ImmutableSet &S)
ImutAVLFactory< ImutInfo > Factory
static bool compareTreeWithSection(TreeTy *T, typename TreeTy::iterator &TI, typename TreeTy::iterator &TE)
static data_type_ref DataOfValue(value_type_ref)
ImmutableSetRef add(value_type_ref V)
unsigned getHeight() const
LLVM_NODISCARD bool empty() const
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
#define PROFILE_INTEGER_INFO(X)
ImmutableSet getEmptySet()
getEmptySet - Returns an immutable set that contains no elements.
static bool isEqual(const Function &Caller, const Function &Callee)
iterator begin() const
begin - Returns an iterator that iterates over the nodes of the tree in an inorder traversal...
typename ImutProfileInfo< T >::value_type value_type
ImmutableSetRef(TreeTy *R, FactoryTy *F)
Constructs a set from a pointer to a tree root.
ImutAVLValueIterator(typename T::TreeTy *Tree)
Profile traits for integers.
ImutAVLValueIterator< ImmutableSetRef > iterator
void foreach(Callback &C)
foreach - A member template the accepts invokes operator() on a functor object (specifed by Callback)...
static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S)
ImutAVLTreeInOrderIterator()
bool operator!=(const ImutAVLTreeInOrderIterator &x) const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Function Alias Analysis false
Factory(bool canonicalize=true)
TreeTy * add_internal(value_type_ref V, TreeTy *T)
add_internal - Creates a new tree that includes the specified data and the data from the original tre...
bool operator!=(const ImmutableSetRef &RHS) const
TreeTy * getEmptyTree() const
TreeTy * remove(TreeTy *T, key_type_ref V)
ImutAVLTreeGenericIterator & operator--()
Allocate memory in an ever growing pool, as if by bump-pointer.
CRTP base class for adapting an iterator to a different type.
TreeTy * operator->() const
bool operator==(const ImutAVLTreeInOrderIterator &x) const
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
ImutAVLTree * find(key_type_ref K)
find - Finds the subtree associated with the specified key value.
ImutAVLTree * getRight() const
Return a pointer to the right subtree.
void markImmutable(TreeTy *T)
markImmutable - Clears the mutable bits of a root and all of its descendants.
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
bool isElementEqual(value_type_ref V) const
ImutAVLTreeInOrderIterator(const TreeTy *Root)
unsigned size() const
size - Returns the number of nodes in the tree, which includes both leaves and non-leaf nodes...
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
typename ImutInfo::value_type_ref value_type_ref
void operator=(const Factory &RHS)=delete
TreeTy * getRight(TreeTy *T) const
value_type_ref getValue(TreeTy *T) const
bool contains(key_type_ref K)
contains - Returns true if this tree contains a subtree (node) that has an data element that matches ...
bool operator!=(const ImmutableSet &RHS) const
TreeTy & operator*() const
TreeTy * getRootWithoutRetain() const
unsigned getHeight() const
Factory(BumpPtrAllocator &Alloc, bool canonicalize=true)
bool operator==(const ImutAVLTreeGenericIterator &x) const
typename ValInfo::value_type_ref value_type_ref
iterator end() const
end - Returns an iterator for the tree that denotes the end of an inorder traversal.
typename ValInfo::value_type value_type
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
ImmutableSetRef & operator=(const ImmutableSetRef &X)
static key_type_ref KeyOfValue(value_type_ref D)
void validateTree() const
unsigned incrementHeight(TreeTy *L, TreeTy *R) const
TreeTy * getCanonicalTree(TreeTy *TNew)
static bool isDataEqual(data_type_ref, data_type_ref)
TreeTy * combineTrees(TreeTy *L, TreeTy *R)
TreeTy * removeMinBinding(TreeTy *T, TreeTy *&Noderemoved)
ImutAVLFactory(BumpPtrAllocator &Alloc)
ImutAVLTreeGenericIterator()=default
llvm_unreachable("Unknown RegBank!")
bool operator!=(const ImutAVLTreeGenericIterator &x) const
ImutAVLTreeInOrderIterator< ImutInfo > iterator
ImutAVLTreeGenericIterator & operator++()
static key_type_ref KeyOfValue(value_type_ref D)
ImmutableSet(TreeTy *R)
Constructs a set from a pointer to a tree root.
uintptr_t getVisitState() const
const value_type & getValue() const
getValue - Returns the data value associated with the tree node.
typename ImutProfileInfo< T >::value_type_ref value_type_ref
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static bool isLess(key_type_ref LHS, key_type_ref RHS)
ImmutableSet(const ImmutableSet &X)
bool isEmpty(TreeTy *T) const
void Profile(FoldingSetNodeID &ID) const
static bool isLess(key_type_ref LHS, key_type_ref RHS)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator==(const ImmutableSet &RHS) const
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
ImutAVLValueIterator::reference operator*() const
typename ValInfo::value_type_ref value_type_ref
bool isEqual(const ImutAVLTree &RHS) const
isEqual - Compares two trees for structural equality and returns true if they are equal...
TreeTy * getLeft(TreeTy *T) const
ImmutableSet< ValT > asImmutableSet(bool canonicalize=true) const
unsigned getHeight(TreeTy *T) const
ImutContainerInfo - Generic definition of comparison operations for elements of immutable containers ...
static unsigned maskCacheIndex(unsigned I)
typename ValInfo::value_type value_type