diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h index baa91f3a5f533..c784859081221 100644 --- a/llvm/include/llvm/ADT/DenseMap.h +++ b/llvm/include/llvm/ADT/DenseMap.h @@ -418,9 +418,16 @@ class DenseMapBase : public DebugEpochBase { } } - template - void copyFrom( - const DenseMapBase &other) { + void copyFrom(const DerivedT &other) { + this->destroyAll(); + derived().deallocateBuckets(); + setNumEntries(0); + setNumTombstones(0); + if (!derived().allocateBuckets(other.getNumBuckets())) { + // The bucket list is empty. No work to do. + return; + } + assert(&other != this); assert(getNumBuckets() == other.getNumBuckets()); @@ -725,7 +732,7 @@ class DenseMap : public DenseMapBase, DenseMap(const DenseMap &other) : BaseT() { init(0); - copyFrom(other); + this->copyFrom(other); } DenseMap(DenseMap &&other) : BaseT() { @@ -761,7 +768,7 @@ class DenseMap : public DenseMapBase, DenseMap &operator=(const DenseMap &other) { if (&other != this) - copyFrom(other); + this->copyFrom(other); return *this; } @@ -831,17 +838,6 @@ class DenseMap : public DenseMapBase, } } - void copyFrom(const DenseMap &other) { - this->destroyAll(); - deallocateBuckets(); - if (allocateBuckets(other.NumBuckets)) { - this->BaseT::copyFrom(other); - } else { - NumEntries = 0; - NumTombstones = 0; - } - } - void grow(unsigned AtLeast) { unsigned OldNumBuckets = NumBuckets; BucketT *OldBuckets = Buckets; @@ -902,7 +898,7 @@ class SmallDenseMap SmallDenseMap(const SmallDenseMap &other) : BaseT() { init(0); - copyFrom(other); + this->copyFrom(other); } SmallDenseMap(SmallDenseMap &&other) : BaseT() { @@ -1001,7 +997,7 @@ class SmallDenseMap SmallDenseMap &operator=(const SmallDenseMap &other) { if (&other != this) - copyFrom(other); + this->copyFrom(other); return *this; } @@ -1099,7 +1095,7 @@ class SmallDenseMap getLargeRep()->~LargeRep(); } - void allocateBuckets(unsigned Num) { + bool allocateBuckets(unsigned Num) { if (Num <= InlineBuckets) { Small = true; } else { @@ -1108,6 +1104,7 @@ class SmallDenseMap allocate_buffer(sizeof(BucketT) * Num, alignof(BucketT))); new (getLargeRep()) LargeRep{NewBuckets, Num}; } + return true; } void init(unsigned InitNumEntries) { @@ -1116,13 +1113,6 @@ class SmallDenseMap this->BaseT::initEmpty(); } - void copyFrom(const SmallDenseMap &other) { - this->destroyAll(); - deallocateBuckets(); - allocateBuckets(other.getNumBuckets()); - this->BaseT::copyFrom(other); - } - void grow(unsigned AtLeast) { if (AtLeast > InlineBuckets) AtLeast = std::max(64, NextPowerOf2(AtLeast - 1));