14#ifndef LLVM_ADT_DENSEMAP_H
15#define LLVM_ADT_DENSEMAP_H
32#include <initializer_list>
44template <
typename KeyT,
typename ValueT>
46 using std::pair<
KeyT, ValueT>::pair;
49 const KeyT &
getFirst()
const {
return std::pair<KeyT, ValueT>::first; }
50 ValueT &
getSecond() {
return std::pair<KeyT, ValueT>::second; }
51 const ValueT &
getSecond()
const {
return std::pair<KeyT, ValueT>::second; }
56template <
typename KeyT,
typename ValueT,
57 typename KeyInfoT = DenseMapInfo<KeyT>,
60class DenseMapIterator;
62template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
92 [[nodiscard]]
inline auto keys() {
93 return map_range(*
this, [](
const BucketT &
P) {
return P.getFirst(); });
98 return map_range(*
this, [](
const BucketT &
P) {
return P.getSecond(); });
101 [[nodiscard]]
inline auto keys()
const {
102 return map_range(*
this, [](
const BucketT &
P) {
return P.getFirst(); });
105 [[nodiscard]]
inline auto values()
const {
106 return map_range(*
this, [](
const BucketT &
P) {
return P.getSecond(); });
109 [[nodiscard]]
bool empty()
const {
return getNumEntries() == 0; }
110 [[nodiscard]]
unsigned size()
const {
return getNumEntries(); }
117 if (NumBuckets > getNumBuckets())
123 if (getNumEntries() == 0 && getNumTombstones() == 0)
128 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
133 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
134 if constexpr (std::is_trivially_destructible_v<ValueT>) {
136 for (BucketT &
B : buckets())
137 B.getFirst() = EmptyKey;
139 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
140 unsigned NumEntries = getNumEntries();
141 for (BucketT &
B : buckets()) {
142 if (!KeyInfoT::isEqual(
B.getFirst(), EmptyKey)) {
143 if (!KeyInfoT::isEqual(
B.getFirst(), TombstoneKey)) {
144 B.getSecond().~ValueT();
147 B.getFirst() = EmptyKey;
150 assert(NumEntries == 0 &&
"Node count imbalance!");
158 auto [Reallocate, NewNumBuckets] = derived().planShrinkAndClear();
164 derived().deallocateBuckets();
169 [[nodiscard]]
bool contains(const_arg_type_t<KeyT> Val)
const {
170 return doFind(Val) !=
nullptr;
190 template <
class LookupKeyT>
192 if (BucketT *Bucket = doFind(Val))
193 return makeIterator(Bucket);
196 template <
class LookupKeyT>
198 if (
const BucketT *Bucket = doFind(Val))
199 return makeConstIterator(Bucket);
205 [[nodiscard]] ValueT
lookup(const_arg_type_t<KeyT> Val)
const {
206 if (
const BucketT *Bucket = doFind(Val))
207 return Bucket->getSecond();
214 template <
typename U = std::remove_cv_t<ValueT>>
215 [[nodiscard]] ValueT
lookup_or(const_arg_type_t<KeyT> Val,
217 if (
const BucketT *Bucket = doFind(Val))
218 return Bucket->getSecond();
223 [[nodiscard]] ValueT &
at(const_arg_type_t<KeyT> Val) {
224 auto Iter = this->
find(std::move(Val));
225 assert(Iter != this->
end() &&
"DenseMap::at failed due to a missing key");
230 [[nodiscard]]
const ValueT &
at(const_arg_type_t<KeyT> Val)
const {
231 auto Iter = this->
find(std::move(Val));
232 assert(Iter != this->
end() &&
"DenseMap::at failed due to a missing key");
239 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
240 return try_emplace_impl(KV.first, KV.second);
246 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
247 return try_emplace_impl(std::move(KV.first), std::move(KV.second));
253 template <
typename... Ts>
255 return try_emplace_impl(std::move(
Key), std::forward<Ts>(Args)...);
261 template <
typename... Ts>
263 return try_emplace_impl(
Key, std::forward<Ts>(Args)...);
271 template <
typename LookupKeyT>
272 std::pair<iterator, bool>
insert_as(std::pair<KeyT, ValueT> &&KV,
273 const LookupKeyT &Val) {
275 if (LookupBucketFor(Val, TheBucket))
276 return {makeIterator(TheBucket),
false};
279 TheBucket = findBucketForInsertion(Val, TheBucket);
280 TheBucket->getFirst() = std::move(KV.first);
281 ::new (&TheBucket->getSecond()) ValueT(std::move(KV.second));
282 return {makeIterator(TheBucket),
true};
286 template <
typename InputIt>
void insert(InputIt
I, InputIt
E) {
296 template <
typename V>
300 Ret.first->second = std::forward<V>(Val);
304 template <
typename V>
308 Ret.first->second = std::forward<V>(Val);
312 template <
typename... Ts>
316 Ret.first->second = ValueT(std::forward<Ts>(Args)...);
320 template <
typename... Ts>
322 auto Ret =
try_emplace(std::move(
Key), std::forward<Ts>(Args)...);
324 Ret.first->second = ValueT(std::forward<Ts>(Args)...);
329 BucketT *TheBucket = doFind(Val);
333 TheBucket->getSecond().~ValueT();
334 TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
335 decrementNumEntries();
336 incrementNumTombstones();
340 BucketT *TheBucket = &*
I;
341 TheBucket->getSecond().~ValueT();
342 TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
343 decrementNumEntries();
344 incrementNumTombstones();
348 return lookupOrInsertIntoBucket(
Key).first->second;
352 return lookupOrInsertIntoBucket(std::move(
Key)).first->second;
358 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
370 RHS.incrementEpoch();
371 derived().swapImpl(
RHS);
380 if (derived().allocateBuckets(NewNumBuckets)) {
391 if constexpr (std::is_trivially_destructible_v<KeyT> &&
392 std::is_trivially_destructible_v<ValueT>)
395 if (getNumBuckets() == 0)
398 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
399 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
400 for (BucketT &
B : buckets()) {
401 if (!KeyInfoT::isEqual(
B.getFirst(), EmptyKey) &&
402 !KeyInfoT::isEqual(
B.getFirst(), TombstoneKey))
403 B.getSecond().~ValueT();
404 B.getFirst().~KeyT();
409 static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
410 "Must pass the derived type to this template!");
414 assert((getNumBuckets() & (getNumBuckets() - 1)) == 0 &&
415 "# initial buckets must be a power of two!");
416 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
417 for (BucketT &
B : buckets())
418 ::new (&
B.getFirst())
KeyT(EmptyKey);
436 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
437 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
438 for (BucketT &
B :
Other.buckets()) {
439 if (!KeyInfoT::isEqual(
B.getFirst(), EmptyKey) &&
440 !KeyInfoT::isEqual(
B.getFirst(), TombstoneKey)) {
443 bool FoundVal = LookupBucketFor(
B.getFirst(), DestBucket);
445 assert(!FoundVal &&
"Key already in new map?");
446 DestBucket->getFirst() = std::move(
B.getFirst());
447 ::new (&DestBucket->getSecond()) ValueT(std::move(
B.getSecond()));
448 incrementNumEntries();
451 B.getSecond().~ValueT();
453 B.getFirst().~KeyT();
455 Other.derived().kill();
460 derived().deallocateBuckets();
463 if (!derived().allocateBuckets(other.getNumBuckets())) {
469 assert(getNumBuckets() == other.getNumBuckets());
471 setNumEntries(other.getNumEntries());
472 setNumTombstones(other.getNumTombstones());
474 BucketT *Buckets = getBuckets();
475 const BucketT *OtherBuckets = other.getBuckets();
476 const size_t NumBuckets = getNumBuckets();
477 if constexpr (std::is_trivially_copyable_v<KeyT> &&
478 std::is_trivially_copyable_v<ValueT>) {
479 memcpy(
reinterpret_cast<void *
>(Buckets), OtherBuckets,
480 NumBuckets *
sizeof(BucketT));
482 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
483 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
484 for (
size_t I = 0;
I < NumBuckets; ++
I) {
485 ::new (&Buckets[
I].getFirst())
KeyT(OtherBuckets[
I].getFirst());
486 if (!KeyInfoT::isEqual(Buckets[
I].getFirst(), EmptyKey) &&
487 !KeyInfoT::isEqual(Buckets[
I].getFirst(), TombstoneKey))
488 ::new (&Buckets[
I].getSecond()) ValueT(OtherBuckets[
I].getSecond());
494 DerivedT &derived() {
return *
static_cast<DerivedT *
>(
this); }
495 const DerivedT &derived()
const {
496 return *
static_cast<const DerivedT *
>(
this);
499 template <
typename KeyArgT,
typename... Ts>
500 std::pair<BucketT *, bool> lookupOrInsertIntoBucket(KeyArgT &&
Key,
502 BucketT *TheBucket =
nullptr;
503 if (LookupBucketFor(
Key, TheBucket))
504 return {TheBucket,
false};
507 TheBucket = findBucketForInsertion(
Key, TheBucket);
508 TheBucket->getFirst() = std::forward<KeyArgT>(
Key);
509 ::new (&TheBucket->getSecond()) ValueT(std::forward<Ts>(Args)...);
510 return {TheBucket,
true};
513 template <
typename KeyArgT,
typename... Ts>
514 std::pair<iterator, bool> try_emplace_impl(KeyArgT &&
Key, Ts &&...Args) {
515 auto [Bucket,
Inserted] = lookupOrInsertIntoBucket(
516 std::forward<KeyArgT>(
Key), std::forward<Ts>(Args)...);
517 return {makeIterator(Bucket),
Inserted};
520 iterator makeIterator(BucketT *TheBucket) {
524 const_iterator makeConstIterator(
const BucketT *TheBucket)
const {
528 unsigned getNumEntries()
const {
return derived().getNumEntries(); }
530 void setNumEntries(
unsigned Num) { derived().setNumEntries(Num); }
532 void incrementNumEntries() { setNumEntries(getNumEntries() + 1); }
534 void decrementNumEntries() { setNumEntries(getNumEntries() - 1); }
536 unsigned getNumTombstones()
const {
return derived().getNumTombstones(); }
538 void setNumTombstones(
unsigned Num) { derived().setNumTombstones(Num); }
540 void incrementNumTombstones() { setNumTombstones(getNumTombstones() + 1); }
542 void decrementNumTombstones() { setNumTombstones(getNumTombstones() - 1); }
544 const BucketT *getBuckets()
const {
return derived().getBuckets(); }
546 BucketT *getBuckets() {
return derived().getBuckets(); }
548 unsigned getNumBuckets()
const {
return derived().getNumBuckets(); }
550 BucketT *getBucketsEnd() {
return getBuckets() + getNumBuckets(); }
552 const BucketT *getBucketsEnd()
const {
553 return getBuckets() + getNumBuckets();
564 void grow(
unsigned MinNumBuckets) {
565 unsigned NumBuckets = DerivedT::roundUpNumBuckets(MinNumBuckets);
567 Tmp.moveFrom(derived());
568 if (derived().maybeMoveFast(std::move(Tmp)))
574 template <
typename LookupKeyT>
575 BucketT *findBucketForInsertion(
const LookupKeyT &
Lookup,
576 BucketT *TheBucket) {
588 unsigned NewNumEntries = getNumEntries() + 1;
589 unsigned NumBuckets = getNumBuckets();
591 this->grow(NumBuckets * 2);
592 LookupBucketFor(
Lookup, TheBucket);
594 (NewNumEntries + getNumTombstones()) <=
596 this->grow(NumBuckets);
597 LookupBucketFor(
Lookup, TheBucket);
603 incrementNumEntries();
606 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
607 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
608 decrementNumTombstones();
613 template <
typename LookupKeyT>
614 const BucketT *doFind(
const LookupKeyT &Val)
const {
615 const BucketT *BucketsPtr = getBuckets();
616 const unsigned NumBuckets = getNumBuckets();
620 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
621 unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
622 unsigned ProbeAmt = 1;
624 const BucketT *Bucket = BucketsPtr + BucketNo;
625 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
627 if (
LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
632 BucketNo += ProbeAmt++;
633 BucketNo &= NumBuckets - 1;
637 template <
typename LookupKeyT> BucketT *doFind(
const LookupKeyT &Val) {
638 return const_cast<BucketT *
>(
645 template <
typename LookupKeyT>
646 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
647 BucketT *BucketsPtr = getBuckets();
648 const unsigned NumBuckets = getNumBuckets();
650 if (NumBuckets == 0) {
651 FoundBucket =
nullptr;
656 BucketT *FoundTombstone =
nullptr;
657 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
658 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
659 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
660 !KeyInfoT::isEqual(Val, TombstoneKey) &&
661 "Empty/Tombstone value shouldn't be inserted into map!");
663 unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
664 unsigned ProbeAmt = 1;
666 BucketT *ThisBucket = BucketsPtr + BucketNo;
668 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
669 FoundBucket = ThisBucket;
675 if (
LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
678 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
684 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
686 FoundTombstone = ThisBucket;
690 BucketNo += ProbeAmt++;
691 BucketNo &= (NumBuckets - 1);
701 return getNumBuckets() *
sizeof(BucketT);
711template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
716 if (
LHS.size() !=
RHS.size())
719 for (
auto &KV :
LHS) {
720 auto I =
RHS.find(KV.first);
721 if (
I ==
RHS.end() ||
I->second != KV.second)
731template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
739template <
typename KeyT,
typename ValueT,
740 typename KeyInfoT = DenseMapInfo<KeyT>,
742class DenseMap :
public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
743 KeyT, ValueT, KeyInfoT, BucketT> {
750 BucketT *Buckets =
nullptr;
751 unsigned NumEntries = 0;
752 unsigned NumTombstones = 0;
753 unsigned NumBuckets = 0;
755 explicit DenseMap(
unsigned NumBuckets,
typename BaseT::ExactBucketCount) {
762 explicit DenseMap(
unsigned NumElementsToReserve = 0)
770 template <
typename InputIt>
775 template <
typename RangeT>
779 DenseMap(std::initializer_list<typename BaseT::value_type> Vals)
780 : DenseMap(Vals.
begin(), Vals.
end()) {}
809 unsigned getNumEntries()
const {
return NumEntries; }
811 void setNumEntries(
unsigned Num) { NumEntries = Num; }
813 unsigned getNumTombstones()
const {
return NumTombstones; }
815 void setNumTombstones(
unsigned Num) { NumTombstones = Num; }
817 BucketT *getBuckets()
const {
return Buckets; }
819 unsigned getNumBuckets()
const {
return NumBuckets; }
821 void deallocateBuckets() {
825 bool allocateBuckets(
unsigned Num) {
827 if (NumBuckets == 0) {
832 Buckets =
static_cast<BucketT *
>(
844 static unsigned roundUpNumBuckets(
unsigned MinNumBuckets) {
846 static_cast<unsigned>(
NextPowerOf2(MinNumBuckets - 1)));
849 bool maybeMoveFast(DenseMap &&
Other) {
857 std::pair<bool, unsigned> planShrinkAndClear()
const {
858 unsigned NewNumBuckets = 0;
860 NewNumBuckets = std::max(64u, 1u << (
Log2_32_Ceil(NumEntries) + 1));
861 if (NewNumBuckets == NumBuckets)
863 return {
true, NewNumBuckets};
867template <
typename KeyT,
typename ValueT,
unsigned InlineBuckets = 4,
868 typename KeyInfoT = DenseMapInfo<KeyT>,
872 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
873 ValueT, KeyInfoT, BucketT> {
881 "InlineBuckets must be a power of 2.");
884 unsigned NumEntries : 31;
885 unsigned NumTombstones;
897 AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
899 SmallDenseMap(
unsigned NumBuckets,
typename BaseT::ExactBucketCount) {
915 template <
typename InputIt>
917 : SmallDenseMap(
std::distance(
I,
E)) {
921 template <
typename RangeT>
926 : SmallDenseMap(Vals.
begin(), Vals.
end()) {}
949 unsigned TmpNumEntries =
RHS.NumEntries;
950 RHS.NumEntries = NumEntries;
951 NumEntries = TmpNumEntries;
954 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
955 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
956 if (Small &&
RHS.Small) {
961 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
962 BucketT *LHSB = &getInlineBuckets()[i],
963 *RHSB = &
RHS.getInlineBuckets()[i];
964 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
965 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
966 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
967 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
968 if (hasLHSValue && hasRHSValue) {
974 std::swap(LHSB->getFirst(), RHSB->getFirst());
976 ::new (&RHSB->getSecond()) ValueT(
std::
move(LHSB->getSecond()));
977 LHSB->getSecond().~ValueT();
979 ::new (&LHSB->getSecond()) ValueT(
std::
move(RHSB->getSecond()));
980 RHSB->getSecond().~ValueT();
985 if (!Small && !
RHS.Small) {
990 SmallDenseMap &SmallSide = Small ? *this :
RHS;
991 SmallDenseMap &LargeSide = Small ?
RHS : *
this;
994 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
995 LargeSide.getLargeRep()->~LargeRep();
996 LargeSide.Small =
true;
1001 for (
unsigned i = 0, e = InlineBuckets; i !=
e; ++i) {
1002 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
1003 *OldB = &SmallSide.getInlineBuckets()[i];
1004 ::new (&NewB->getFirst()) KeyT(std::
move(OldB->getFirst()));
1005 OldB->getFirst().~KeyT();
1006 if (!KeyInfoT::
isEqual(NewB->getFirst(), EmptyKey) &&
1007 !KeyInfoT::
isEqual(NewB->getFirst(), TombstoneKey)) {
1008 ::new (&NewB->getSecond()) ValueT(std::
move(OldB->getSecond()));
1009 OldB->getSecond().~ValueT();
1015 SmallSide.Small = false;
1016 new (SmallSide.getLargeRep()) LargeRep(std::
move(TmpRep));
1019 unsigned getNumEntries()
const {
return NumEntries; }
1021 void setNumEntries(
unsigned Num) {
1023 assert(Num < (1U << 31) &&
"Cannot support more than 1<<31 entries");
1027 unsigned getNumTombstones()
const {
return NumTombstones; }
1029 void setNumTombstones(
unsigned Num) { NumTombstones = Num; }
1031 const BucketT *getInlineBuckets()
const {
1036#if defined(__clang__) && \
1037 (defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_HWADDRESS__))
1041 __asm__
volatile(
"" :::
"memory");
1043 return reinterpret_cast<const BucketT *
>(&storage);
1046 BucketT *getInlineBuckets() {
1047 return const_cast<BucketT *
>(
1048 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
1051 const LargeRep *getLargeRep()
const {
1054#if defined(__clang__) && \
1055 (defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_HWADDRESS__))
1056 __asm__
volatile(
"" :::
"memory");
1058 return reinterpret_cast<const LargeRep *
>(&storage);
1061 LargeRep *getLargeRep() {
1062 return const_cast<LargeRep *
>(
1063 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
1066 const BucketT *getBuckets()
const {
1067 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1070 BucketT *getBuckets() {
1071 return const_cast<BucketT *
>(
1072 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
1075 unsigned getNumBuckets()
const {
1076 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1080 BucketT *Begin = getInlineBuckets();
1084 void deallocateBuckets() {
1088 if (Small || getLargeRep()->NumBuckets == 0)
1092 sizeof(BucketT) * getLargeRep()->NumBuckets,
1094 getLargeRep()->~LargeRep();
1097 bool allocateBuckets(
unsigned Num) {
1098 if (Num <= InlineBuckets) {
1102 BucketT *NewBuckets =
static_cast<BucketT *
>(
1104 new (getLargeRep()) LargeRep{NewBuckets, Num};
1111 deallocateBuckets();
1113 new (getLargeRep()) LargeRep{
nullptr, 0};
1116 static unsigned roundUpNumBuckets(
unsigned MinNumBuckets) {
1117 if (MinNumBuckets <= InlineBuckets)
1118 return MinNumBuckets;
1119 return std::max(64u,
1120 static_cast<unsigned>(
NextPowerOf2(MinNumBuckets - 1)));
1123 bool maybeMoveFast(SmallDenseMap &&
Other) {
1128 NumEntries =
Other.NumEntries;
1129 NumTombstones =
Other.NumTombstones;
1130 *getLargeRep() = std::move(*
Other.getLargeRep());
1131 Other.getLargeRep()->NumBuckets = 0;
1138 std::pair<bool, unsigned> planShrinkAndClear()
const {
1139 unsigned NewNumBuckets = 0;
1140 if (!this->
empty()) {
1142 if (NewNumBuckets > InlineBuckets)
1143 NewNumBuckets = std::max(64u, NewNumBuckets);
1145 bool Reuse = Small ? NewNumBuckets <= InlineBuckets
1146 : NewNumBuckets == getLargeRep()->NumBuckets;
1149 return {
true, NewNumBuckets};
1153template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
1156 friend class DenseMapIterator<
KeyT, ValueT, KeyInfoT, Bucket,
true>;
1157 friend class DenseMapIterator<
KeyT, ValueT, KeyInfoT, Bucket,
false>;
1161 using value_type = std::conditional_t<IsConst, const Bucket, Bucket>;
1168 std::conditional_t<shouldReverseIterate<KeyT>(),
1169 std::reverse_iterator<pointer>,
pointer>;
1171 BucketItTy Ptr = {};
1172 BucketItTy End = {};
1174 DenseMapIterator(BucketItTy Pos, BucketItTy
E,
const DebugEpochBase &Epoch)
1175 : DebugEpochBase::HandleBase(&Epoch), Ptr(Pos), End(
E) {
1176 assert(isHandleInSync() &&
"invalid construction!");
1187 return makeEnd(Buckets, Epoch);
1188 auto R = maybeReverse(Buckets);
1189 DenseMapIterator Iter(R.begin(), R.end(), Epoch);
1190 Iter.AdvancePastEmptyBuckets();
1196 auto R = maybeReverse(Buckets);
1197 return DenseMapIterator(R.end(), R.end(), Epoch);
1203 auto R = maybeReverse(Buckets);
1205 return DenseMapIterator(BucketItTy(
P +
Offset), R.end(), Epoch);
1211 template <
bool IsConstSrc,
1212 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1214 const DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc> &
I)
1219 assert(Ptr != End &&
"dereferencing end() iterator");
1225 const DenseMapIterator &
RHS) {
1226 assert((!
LHS.getEpochAddress() ||
LHS.isHandleInSync()) &&
1227 "handle not in sync!");
1228 assert((!
RHS.getEpochAddress() ||
RHS.isHandleInSync()) &&
1229 "handle not in sync!");
1230 assert(
LHS.getEpochAddress() ==
RHS.getEpochAddress() &&
1231 "comparing incomparable iterators!");
1232 return LHS.Ptr ==
RHS.Ptr;
1236 const DenseMapIterator &
RHS) {
1242 assert(Ptr != End &&
"incrementing end() iterator");
1244 AdvancePastEmptyBuckets();
1249 DenseMapIterator tmp = *
this;
1255 void AdvancePastEmptyBuckets() {
1257 const KeyT Empty = KeyInfoT::getEmptyKey();
1260 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(),
Empty) ||
1261 KeyInfoT::isEqual(Ptr->getFirst(),
Tombstone)))
1265 static auto maybeReverse(iterator_range<pointer>
Range) {
1266 if constexpr (shouldReverseIterate<KeyT>())
1267 return reverse(
Range);
1273template <
typename KeyT,
typename ValueT,
typename KeyInfoT>
1274[[nodiscard]]
inline size_t
1276 return X.getMemorySize();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_LIKELY(EXPR)
This file defines DenseMapInfo traits for DenseMap.
This file defines the DebugEpochBase and DebugEpochBase::HandleBase classes.
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains library features backported from future STL versions.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
LocallyHashedType DenseMapInfo< LocallyHashedType >::Tombstone
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
bool isHandleInSync() const
ValueT & at(const_arg_type_t< KeyT > Val)
Return the entry for the specified key, or abort if no such entry exists.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
iterator find(const_arg_type_t< KeyT > Val)
void copyFrom(const DerivedT &other)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
bool erase(const KeyT &Val)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
std::pair< iterator, bool > insert_as(std::pair< KeyT, ValueT > &&KV, const LookupKeyT &Val)
Alternate version of insert() which allows a different, and possibly less expensive,...
void moveFrom(DerivedT &Other)
const_iterator find_as(const LookupKeyT &Val) const
const_iterator end() const
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
const_iterator find(const_arg_type_t< KeyT > Val) const
std::pair< iterator, bool > emplace_or_assign(const KeyT &Key, Ts &&...Args)
void insert(InputIt I, InputIt E)
Range insertion of pairs.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
const ValueT & at(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or abort if no such entry exists.
bool isPointerIntoBucketsArray(const void *Ptr) const
Return true if the specified pointer points somewhere into the DenseMap's array of buckets (i....
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
const_iterator begin() const
std::pair< iterator, bool > emplace_or_assign(KeyT &&Key, Ts &&...Args)
void insert_range(Range &&R)
Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
ValueT & operator[](const KeyT &Key)
void initWithExactBucketCount(unsigned NewNumBuckets)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
ValueT & operator[](KeyT &&Key)
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
std::conditional_t< IsConst, const BucketT, BucketT > value_type
static DenseMapIterator makeIterator(pointer P, iterator_range< pointer > Buckets, const DebugEpochBase &Epoch)
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
DenseMapIterator & operator++()
pointer operator->() const
reference operator*() const
DenseMapIterator()=default
static DenseMapIterator makeBegin(iterator_range< pointer > Buckets, bool IsEmpty, const DebugEpochBase &Epoch)
DenseMapIterator operator++(int)
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
ptrdiff_t difference_type
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
static DenseMapIterator makeEnd(iterator_range< pointer > Buckets, const DebugEpochBase &Epoch)
std::forward_iterator_tag iterator_category
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
DenseMap(unsigned NumElementsToReserve=0)
Create a DenseMap with an optional NumElementsToReserve to guarantee that this number of elements can...
DenseMap & operator=(DenseMap &&other)
DenseMap(llvm::from_range_t, const RangeT &Range)
DenseMap(const DenseMap &other)
DenseMap(const InputIt &I, const InputIt &E)
DenseMap(DenseMap &&other)
DenseMap & operator=(const DenseMap &other)
SmallDenseMap(const InputIt &I, const InputIt &E)
SmallDenseMap & operator=(SmallDenseMap &&other)
SmallDenseMap & operator=(const SmallDenseMap &other)
SmallDenseMap(unsigned NumElementsToReserve=0)
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
SmallDenseMap(SmallDenseMap &&other)
SmallDenseMap(const SmallDenseMap &other)
SmallDenseMap(llvm::from_range_t, const RangeT &Range)
A range adaptor for a pair of iterators.
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
This is an optimization pass for GlobalISel generic memory operations.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
BitVector::size_type capacity_in_bytes(const BitVector &X)
bool operator!=(uint64_t V1, const APInt &V2)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
LLVM_ABI LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * allocate_buffer(size_t Size, size_t Alignment)
Allocate a buffer of memory with the given size and alignment.
LLVM_ABI void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
constexpr bool shouldReverseIterate()
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
@ Default
The result value is uniform if and only if all operands are uniform.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
std::conditional_t< std::is_pointer_v< T >, typename add_const_past_pointer< T >::type, const T & > type
const ValueT & getSecond() const
const KeyT & getFirst() const