13 #ifndef LLVM_ADT_IMMUTABLESET_H 14 #define LLVM_ADT_IMMUTABLESET_H 40 template <
typename ImutInfo >
80 else if (ImutInfo::isLess(K,CurrentKey))
120 ImutInfo::KeyOfValue(V)))
124 if (!ImutInfo::isDataEqual(ImutInfo::DataOfValue(
getValue()),
125 ImutInfo::DataOfValue(V)))
145 while (LItr != LEnd && RItr != REnd) {
146 if (&*LItr == &*RItr) {
159 return LItr == LEnd && RItr == REnd;
174 template <
typename Callback>
175 void foreach(Callback&
C) {
198 &&
"Height calculation wrong");
200 assert((HL > HR ? HL-HR : HR-HL) <= 2
201 &&
"Balancing invariant violated");
205 ImutInfo::KeyOfValue(
getValue()))) &&
206 "Value in left child is not less that current value");
209 ImutInfo::isLess(ImutInfo::KeyOfValue(
getValue()),
211 "Current value is not less that value of right child");
227 unsigned height : 28;
229 bool IsDigestCached : 1;
230 bool IsCanonicalized : 1;
245 : factory(f), left(l), right(r), height(height), IsMutable(
true),
246 IsDigestCached(
false), IsCanonicalized(
false), value(v)
249 if (right) right->
retain();
258 bool isMutable()
const {
return IsMutable; }
262 bool hasCachedDigest()
const {
return IsDigestCached; }
277 void markImmutable() {
278 assert(isMutable() &&
"Mutable flag already removed.");
283 void markedCachedDigest() {
284 assert(!hasCachedDigest() &&
"NoCachedDigest flag already removed.");
285 IsDigestCached =
true;
290 void setHeight(
unsigned h) {
291 assert(isMutable() &&
"Only a mutable tree can have its height changed.");
300 digest += L->computeDigest();
304 ImutInfo::Profile(ID,V);
308 digest += R->computeDigest();
316 if (hasCachedDigest())
321 markedCachedDigest();
343 if (IsCanonicalized) {
356 factory->freeNodes.push_back(
this);
364 template <
typename ImutInfo >
375 std::vector<TreeTy*> createdNodes;
376 std::vector<TreeTy*> freeNodes;
378 bool ownsAllocator()
const {
379 return (Allocator & 0x1) == 0;
395 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
398 if (ownsAllocator())
delete &getAllocator();
401 TreeTy*
add(TreeTy*
T, value_type_ref V) {
402 T = add_internal(V,T);
408 TreeTy*
remove(TreeTy*
T, key_type_ref V) {
409 T = remove_internal(V,T);
429 value_type_ref
getValue(TreeTy* T)
const {
return T->value; }
437 return (hl > hr ? hl : hr) + 1;
444 for ( ; I!=
E ; ++
I, ++TI) {
464 if (!freeNodes.empty()) {
465 T = freeNodes.back();
466 freeNodes.pop_back();
472 new (
T) TreeTy(
this, L, R, V, incrementHeight(L,R));
473 createdNodes.push_back(T);
477 TreeTy*
createNode(TreeTy* newLeft, TreeTy* oldTree, TreeTy* newRight) {
478 return createNode(newLeft,
getValue(oldTree), newRight);
482 for (
unsigned i = 0, n = createdNodes.size(); i < n; ++i) {
483 TreeTy *
N = createdNodes[i];
484 if (N->isMutable() && N->refCount == 0)
487 createdNodes.clear();
497 assert(!isEmpty(L) &&
"Left tree cannot be empty to have a height >= 2");
503 return createNode(LL, L, createNode(LR,V,R));
505 assert(!isEmpty(LR) &&
"LR cannot be empty because it has a height >= 1");
510 return createNode(createNode(LL,L,LRL), LR, createNode(LRR,V,R));
514 assert(!isEmpty(R) &&
"Right tree cannot be empty to have a height >= 2");
520 return createNode(createNode(L,V,RL), R, RR);
522 assert(!isEmpty(RL) &&
"RL cannot be empty because it has a height >= 1");
527 return createNode(createNode(L,V,RLL), RL, createNode(RLR,R,RR));
530 return createNode(L,V,R);
538 return createNode(T, V, T);
541 key_type_ref K = ImutInfo::KeyOfValue(V);
542 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
546 else if (ImutInfo::isLess(K,KCurrent))
562 key_type_ref KCurrent = ImutInfo::KeyOfValue(
getValue(T));
566 }
else if (ImutInfo::isLess(K,KCurrent)) {
567 return balanceTree(remove_internal(K,
getLeft(T)),
581 TreeTy* newRight = removeMinBinding(R,OldNode);
582 return balanceTree(L,
getValue(OldNode), newRight);
591 return balanceTree(removeMinBinding(
getLeft(T), Noderemoved),
598 if (!T || !T->isMutable())
610 if (TNew->IsCanonicalized)
615 unsigned digest = TNew->computeDigest();
616 TreeTy *&
entry = Cache[maskCacheIndex(digest)];
620 for (TreeTy *T = entry ; T !=
nullptr; T = T->next) {
623 if (!compareTreeWithSection(TNew, TI,
TE))
628 if (TNew->refCount == 0)
638 TNew->IsCanonicalized =
true;
647 template <
typename ImutInfo>
649 :
public std::iterator<std::bidirectional_iterator_tag,
650 ImutAVLTree<ImutInfo>> {
654 enum VisitFlag { VisitedNone=0x0, VisitedLeft=0x1, VisitedRight=0x3,
661 if (Root) stack.
push_back(reinterpret_cast<uintptr_t>(Root));
666 return *
reinterpret_cast<TreeTy *
>(stack.
back() & ~Flags);
672 return stack.
back() & Flags;
678 return stack.
size() == 1 && getVisitState() == VisitedNone;
686 switch (getVisitState()) {
688 stack.
back() |= VisitedLeft;
691 stack.
back() |= VisitedRight;
699 return stack == x.stack;
703 return !(*
this == x);
708 TreeTy* Current =
reinterpret_cast<TreeTy*
>(stack.
back() & ~Flags);
710 switch (getVisitState()) {
712 if (TreeTy* L = Current->getLeft())
713 stack.
push_back(reinterpret_cast<uintptr_t>(L));
715 stack.
back() |= VisitedLeft;
718 if (TreeTy* R = Current->getRight())
719 stack.
push_back(reinterpret_cast<uintptr_t>(R));
721 stack.
back() |= VisitedRight;
734 TreeTy* Current =
reinterpret_cast<TreeTy*
>(stack.
back() & ~Flags);
736 switch (getVisitState()) {
741 stack.
back() &= ~Flags;
742 if (TreeTy* L = Current->getLeft())
743 stack.
push_back(reinterpret_cast<uintptr_t>(L) | VisitedRight);
746 stack.
back() &= ~Flags;
747 stack.
back() |= VisitedLeft;
748 if (TreeTy* R = Current->getRight())
749 stack.
push_back(reinterpret_cast<uintptr_t>(R) | VisitedRight);
758 template <
typename ImutInfo>
760 :
public std::iterator<std::bidirectional_iterator_tag,
761 ImutAVLTree<ImutInfo>> {
764 InternalIteratorTy InternalItr;
777 return InternalItr == x.InternalItr;
781 return !(*
this == x);
789 while (!InternalItr.atEnd() &&
790 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
797 while (!InternalItr.atBeginning() &&
798 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft);
804 InternalItr.skipToParent();
806 while (!InternalItr.atEnd() &&
807 InternalItr.getVisitState() != InternalIteratorTy::VisitedLeft)
814 template <
typename T>
817 ImutAVLValueIterator<T>, typename T::TreeTy::iterator,
818 typename std::iterator_traits<
819 typename T::TreeTy::iterator>::iterator_category,
820 const typename T::value_type> {
825 typename ImutAVLValueIterator::reference
operator*()
const {
826 return this->
I->getValue();
837 template <
typename T>
848 template <
typename T>
858 #define PROFILE_INTEGER_INFO(X)\ 859 template<> struct ImutProfileInfo<X> : ImutProfileInteger<X> {}; 872 #undef PROFILE_INTEGER_INFO 887 template <
typename T>
907 template <
typename T>
920 return std::equal_to<key_type>()(LHS,RHS);
924 return std::less<key_type>()(LHS,RHS);
933 template <
typename T>
956 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT>>
972 if (Root) { Root->
retain(); }
976 if (Root) { Root->
retain(); }
984 if (Root != X.Root) {
985 if (X.Root) { X.Root->
retain(); }
994 const bool Canonicalize;
998 : Canonicalize(canonicalize) {}
1001 : F(Alloc), Canonicalize(canonicalize) {}
1004 void operator=(
const Factory& RHS) =
delete;
1019 TreeTy *NewT = F.
add(Old.Root, V);
1031 TreeTy *NewT = F.
remove(Old.Root, V);
1046 return Root ? Root->
contains(V) :
false;
1050 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1054 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
1058 if (Root) { Root->
retain(); }
1073 template <
typename Callback>
1074 void foreach(Callback&
C) {
if (Root) Root->
foreach(C); }
1076 template <
typename Callback>
1077 void foreach() {
if (Root) { Callback
C; Root->
foreach(C); } }
1108 template <
typename ValT,
typename ValInfo = ImutContainerInfo<ValT>>
1128 if (Root) { Root->
retain(); }
1133 Factory(X.Factory) {
1134 if (Root) { Root->
retain(); }
1138 if (Root) { Root->
release(); }
1142 if (Root != X.Root) {
1143 if (X.Root) { X.Root->
retain(); }
1144 if (Root) { Root->
release(); }
1146 Factory = X.Factory;
1165 return Root ? Root->
contains(V) :
false;
1170 Factory->getCanonicalTree(Root) : Root);
1178 return Root && RHS.Root ? Root->
isEqual(*RHS.Root) : Root == RHS.Root;
1182 return Root && RHS.Root ? Root->
isNotEqual(*RHS.Root) : Root != RHS.Root;
1222 #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 AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
ImmutableSetRef(const ImmutableSetRef &X)
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
TreeTy * getRootWithoutRetain() const
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
static bool isDataEqual(data_type_ref, data_type_ref)
Generic profile trait for pointer types.
This class represents lattice values for constants.
TreeTy * balanceTree(TreeTy *L, value_type_ref V, TreeTy *R)
balanceTree - Used by add_internal and remove_internal to balance a newly created tree...
typename ImutInfo::value_type value_type
static data_type_ref DataOfValue(value_type_ref)
void Profile(FoldingSetNodeID &ID) const
TreeTy * add(TreeTy *T, value_type_ref V)
LLVM_NODISCARD 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...
iterator begin() const
begin - Returns an iterator that iterates over the nodes of the tree in an inorder traversal...
void push_back(const T &Elt)
typename ImutInfo::key_type_ref key_type_ref
ImutAVLValueIterator::reference operator*() const
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--()
typename TreeTy::Factory FactoryTy
bool operator==(const ImutAVLTreeGenericIterator &x) const
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
TreeTy * createNode(TreeTy *newLeft, TreeTy *oldTree, TreeTy *newRight)
ImutAVLTreeGenericIterator(const TreeTy *Root)
TreeTy & operator*() const
static void Profile(const T &X, FoldingSetNodeID &ID)
ImmutableSet & operator=(const ImmutableSet &X)
bool isNotEqual(const ImutAVLTree &RHS) const
isNotEqual - Compares two trees for structural inequality.
unsigned ComputeHash() const
ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to lookup the node in the F...
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
TreeTy::Factory * getTreeFactory() const
ImutAVLTree * getMaxElement()
getMaxElement - Find the subtree associated with the highest ranged key value.
TreeTy * getRight(TreeTy *T) const
static ImmutableSetRef getEmptySet(FactoryTy *F)
TreeTy * createNode(TreeTy *L, value_type_ref V, TreeTy *R)
ImutAVLTreeInOrderIterator & operator++()
bool isEqual(const ImutAVLTree &RHS) const
isEqual - Compares two trees for structural equality and returns true if they are equal...
TreeTy * operator->() const
Generic profile template.
typename ImutProfileInfo< T * >::value_type_ref value_type_ref
void AddInteger(signed I)
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
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)
void Profile(FoldingSetNodeID &ID) const
#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)
unsigned validateTree() const
validateTree - A utility method that checks that the balancing and ordering invariants of the tree ar...
ImmutableSetRef(TreeTy *R, FactoryTy *F)
Constructs a set from a pointer to a tree root.
bool operator!=(const ImmutableSet &RHS) const
ImutAVLValueIterator(typename T::TreeTy *Tree)
TreeTy * operator->() const
value_type_ref key_type_ref
Profile traits for integers.
void foreach(Callback &C)
foreach - A member template the accepts invokes operator() on a functor object (specifed by Callback)...
bool isElementEqual(const ImutAVLTree *RHS) const
static void Profile(FoldingSetNodeID &ID, const ImmutableSetRef &S)
bool operator!=(const ImmutableSetRef &RHS) const
ImutAVLTreeInOrderIterator()
bool operator!=(const ImutAVLTreeGenericIterator &x) const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
iterator end() const
end - Returns an iterator for the tree that denotes the end of an inorder traversal.
Factory(bool canonicalize=true)
bool isEmpty() const
isEmpty - Return true if the set contains no elements.
TreeTy * getLeft(TreeTy *T) const
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 isEmpty(TreeTy *T) 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.
bool operator==(const ImmutableSetRef &RHS) const
CRTP base class for adapting an iterator to a different type.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
ImutAVLTree * find(key_type_ref K)
find - Finds the subtree associated with the specified key value.
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)
ImutAVLTreeInOrderIterator(const TreeTy *Root)
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
typename ImutInfo::value_type_ref value_type_ref
void validateTree() const
bool contains(value_type_ref V) const
Returns true if the set contains the specified value.
value_type_ref getValue(TreeTy *T) const
bool operator!=(const ImutAVLTreeInOrderIterator &x) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool contains(key_type_ref K)
contains - Returns true if this tree contains a subtree (node) that has an data element that matches ...
TreeTy * getRootWithoutRetain() const
bool operator==(const ImutAVLTreeInOrderIterator &x) const
const value_type & getValue() const
getValue - Returns the data value associated with the tree node.
unsigned getHeight() const
Factory(BumpPtrAllocator &Alloc, bool canonicalize=true)
uintptr_t getVisitState() const
typename ValInfo::value_type_ref value_type_ref
typename ValInfo::value_type value_type
typename ImutProfileInfo< T * >::value_type value_type
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
unsigned getHeight(TreeTy *T) const
ImmutableSetRef & operator=(const ImmutableSetRef &X)
static key_type_ref KeyOfValue(value_type_ref D)
TreeTy * getCanonicalTree(TreeTy *TNew)
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
static bool isDataEqual(data_type_ref, data_type_ref)
TreeTy * combineTrees(TreeTy *L, TreeTy *R)
TreeTy * removeMinBinding(TreeTy *T, TreeTy *&Noderemoved)
ImutAVLFactory(BumpPtrAllocator &Alloc)
unsigned size() const
size - Returns the number of nodes in the tree, which includes both leaves and non-leaf nodes...
ImutAVLTreeInOrderIterator< ImutInfo > iterator
ImutAVLTreeGenericIterator & operator++()
void validateTree() const
static key_type_ref KeyOfValue(value_type_ref D)
ImmutableSet(TreeTy *R)
Constructs a set from a pointer to a tree root.
LLVM_NODISCARD bool empty() const
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
bool isSingleton() const
isSingleton - Return true if the set contains exactly one element.
static bool isLess(key_type_ref LHS, key_type_ref RHS)
ImmutableSet(const ImmutableSet &X)
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
static bool isLess(key_type_ref LHS, key_type_ref RHS)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ImutAVLTree * getRight() const
Return a pointer to the right subtree.
static void Profile(FoldingSetNodeID &ID, value_type_ref X)
ImmutableSet< ValT > asImmutableSet(bool canonicalize=true) const
unsigned getHeight() const
typename ValInfo::value_type_ref value_type_ref
print Instructions which execute on loop entry
bool isElementEqual(value_type_ref V) const
unsigned incrementHeight(TreeTy *L, TreeTy *R) const
TreeTy & operator*() const
ImutContainerInfo - Generic definition of comparison operations for elements of immutable containers ...
static unsigned maskCacheIndex(unsigned I)
bool operator==(const ImmutableSet &RHS) const
typename ValInfo::value_type value_type
ImutAVLTree * getLeft() const
Return a pointer to the left subtree.