//===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements a hash set that can be used to remove duplication of // nodes in a graph. // //===----------------------------------------------------------------------===// #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/SwapByteOrder.h" #include <cassert> #include <cstring> usingnamespacellvm; //===----------------------------------------------------------------------===// // FoldingSetNodeIDRef Implementation bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const { … } /// Used to compare the "ordering" of two nodes as defined by the /// profiled bits and their ordering defined by memcmp(). bool FoldingSetNodeIDRef::operator<(FoldingSetNodeIDRef RHS) const { … } //===----------------------------------------------------------------------===// // FoldingSetNodeID Implementation /// Add* - Add various data types to Bit data. /// void FoldingSetNodeID::AddString(StringRef String) { … } // AddNodeID - Adds the Bit data of another ID to *this. void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) { … } /// operator== - Used to compare two nodes to each other. /// bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const { … } /// operator== - Used to compare two nodes to each other. /// bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const { … } /// Used to compare the "ordering" of two nodes as defined by the /// profiled bits and their ordering defined by memcmp(). bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const { … } bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const { … } /// Intern - Copy this node's data to a memory region allocated from the /// given allocator and return a FoldingSetNodeIDRef describing the /// interned data. FoldingSetNodeIDRef FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const { … } //===----------------------------------------------------------------------===// /// Helper functions for FoldingSetBase. /// GetNextPtr - In order to save space, each bucket is a /// singly-linked-list. In order to make deletion more efficient, we make /// the list circular, so we can delete a node without computing its hash. /// The problem with this is that the start of the hash buckets are not /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null: /// use GetBucketPtr when this happens. static FoldingSetBase::Node *GetNextPtr(void *NextInBucketPtr) { … } /// testing. static void **GetBucketPtr(void *NextInBucketPtr) { … } /// GetBucketFor - Hash the specified node ID and return the hash bucket for /// the specified ID. static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) { … } /// AllocateBuckets - Allocated initialized bucket memory. static void **AllocateBuckets(unsigned NumBuckets) { … } //===----------------------------------------------------------------------===// // FoldingSetBase Implementation FoldingSetBase::FoldingSetBase(unsigned Log2InitSize) { … } FoldingSetBase::FoldingSetBase(FoldingSetBase &&Arg) : … { … } FoldingSetBase &FoldingSetBase::operator=(FoldingSetBase &&RHS) { … } FoldingSetBase::~FoldingSetBase() { … } void FoldingSetBase::clear() { … } void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount, const FoldingSetInfo &Info) { … } /// GrowHashTable - Double the size of the hash table and rehash everything. /// void FoldingSetBase::GrowHashTable(const FoldingSetInfo &Info) { … } void FoldingSetBase::reserve(unsigned EltCount, const FoldingSetInfo &Info) { … } /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, /// return it. If not, return the insertion token that will make insertion /// faster. FoldingSetBase::Node *FoldingSetBase::FindNodeOrInsertPos( const FoldingSetNodeID &ID, void *&InsertPos, const FoldingSetInfo &Info) { … } /// InsertNode - Insert the specified node into the folding set, knowing that it /// is not already in the map. InsertPos must be obtained from /// FindNodeOrInsertPos. void FoldingSetBase::InsertNode(Node *N, void *InsertPos, const FoldingSetInfo &Info) { … } /// RemoveNode - Remove a node from the folding set, returning true if one was /// removed or false if the node was not in the folding set. bool FoldingSetBase::RemoveNode(Node *N) { … } /// GetOrInsertNode - If there is an existing simple Node exactly /// equal to the specified node, return it. Otherwise, insert 'N' and it /// instead. FoldingSetBase::Node * FoldingSetBase::GetOrInsertNode(FoldingSetBase::Node *N, const FoldingSetInfo &Info) { … } //===----------------------------------------------------------------------===// // FoldingSetIteratorImpl Implementation FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) { … } void FoldingSetIteratorImpl::advance() { … } //===----------------------------------------------------------------------===// // FoldingSetBucketIteratorImpl Implementation FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) { … }