LLVM 22.0.0git
DenseMap.h
Go to the documentation of this file.
1//===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the DenseMap class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_DENSEMAP_H
15#define LLVM_ADT_DENSEMAP_H
16
17#include "llvm/ADT/ADL.h"
20#include "llvm/ADT/STLExtras.h"
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstring>
32#include <initializer_list>
33#include <iterator>
34#include <new>
35#include <type_traits>
36#include <utility>
37
38namespace llvm {
39
40namespace detail {
41
42// We extend a pair to allow users to override the bucket type with their own
43// implementation without requiring two members.
44template <typename KeyT, typename ValueT>
45struct DenseMapPair : std::pair<KeyT, ValueT> {
46 using std::pair<KeyT, ValueT>::pair;
47
48 KeyT &getFirst() { return std::pair<KeyT, ValueT>::first; }
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; }
52};
53
54} // end namespace detail
55
56template <typename KeyT, typename ValueT,
57 typename KeyInfoT = DenseMapInfo<KeyT>,
59 bool IsConst = false>
60class DenseMapIterator;
61
62template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
63 typename BucketT>
65 template <typename T>
66 using const_arg_type_t = typename const_pointer_or_const_ref<T>::type;
67
68public:
70 using key_type = KeyT;
71 using mapped_type = ValueT;
72 using value_type = BucketT;
73
77
78 [[nodiscard]] inline iterator begin() {
79 return iterator::makeBegin(buckets(), empty(), *this);
80 }
81 [[nodiscard]] inline iterator end() {
82 return iterator::makeEnd(buckets(), *this);
83 }
84 [[nodiscard]] inline const_iterator begin() const {
85 return const_iterator::makeBegin(buckets(), empty(), *this);
86 }
87 [[nodiscard]] inline const_iterator end() const {
88 return const_iterator::makeEnd(buckets(), *this);
89 }
90
91 // Return an iterator to iterate over keys in the map.
92 [[nodiscard]] inline auto keys() {
93 return map_range(*this, [](const BucketT &P) { return P.getFirst(); });
94 }
95
96 // Return an iterator to iterate over values in the map.
97 [[nodiscard]] inline auto values() {
98 return map_range(*this, [](const BucketT &P) { return P.getSecond(); });
99 }
100
101 [[nodiscard]] inline auto keys() const {
102 return map_range(*this, [](const BucketT &P) { return P.getFirst(); });
103 }
104
105 [[nodiscard]] inline auto values() const {
106 return map_range(*this, [](const BucketT &P) { return P.getSecond(); });
107 }
108
109 [[nodiscard]] bool empty() const { return getNumEntries() == 0; }
110 [[nodiscard]] unsigned size() const { return getNumEntries(); }
111
112 /// Grow the densemap so that it can contain at least \p NumEntries items
113 /// before resizing again.
114 void reserve(size_type NumEntries) {
115 auto NumBuckets = getMinBucketToReserveForEntries(NumEntries);
117 if (NumBuckets > getNumBuckets())
118 grow(NumBuckets);
119 }
120
121 void clear() {
123 if (getNumEntries() == 0 && getNumTombstones() == 0)
124 return;
125
126 // If the capacity of the array is huge, and the # elements used is small,
127 // shrink the array.
128 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
130 return;
131 }
132
133 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
134 if constexpr (std::is_trivially_destructible_v<ValueT>) {
135 // Use a simpler loop when values don't need destruction.
136 for (BucketT &B : buckets())
137 B.getFirst() = EmptyKey;
138 } else {
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();
145 --NumEntries;
146 }
147 B.getFirst() = EmptyKey;
148 }
149 }
150 assert(NumEntries == 0 && "Node count imbalance!");
151 (void)NumEntries;
152 }
153 setNumEntries(0);
154 setNumTombstones(0);
155 }
156
158 auto [Reallocate, NewNumBuckets] = derived().planShrinkAndClear();
159 destroyAll();
160 if (!Reallocate) {
161 initEmpty();
162 return;
163 }
164 derived().deallocateBuckets();
165 initWithExactBucketCount(NewNumBuckets);
166 }
167
168 /// Return true if the specified key is in the map, false otherwise.
169 [[nodiscard]] bool contains(const_arg_type_t<KeyT> Val) const {
170 return doFind(Val) != nullptr;
171 }
172
173 /// Return 1 if the specified key is in the map, 0 otherwise.
174 [[nodiscard]] size_type count(const_arg_type_t<KeyT> Val) const {
175 return contains(Val) ? 1 : 0;
176 }
177
178 [[nodiscard]] iterator find(const_arg_type_t<KeyT> Val) {
179 return find_as(Val);
180 }
181 [[nodiscard]] const_iterator find(const_arg_type_t<KeyT> Val) const {
182 return find_as(Val);
183 }
184
185 /// Alternate version of find() which allows a different, and possibly
186 /// less expensive, key type.
187 /// The DenseMapInfo is responsible for supplying methods
188 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
189 /// type used.
190 template <class LookupKeyT>
191 [[nodiscard]] iterator find_as(const LookupKeyT &Val) {
192 if (BucketT *Bucket = doFind(Val))
193 return makeIterator(Bucket);
194 return end();
195 }
196 template <class LookupKeyT>
197 [[nodiscard]] const_iterator find_as(const LookupKeyT &Val) const {
198 if (const BucketT *Bucket = doFind(Val))
199 return makeConstIterator(Bucket);
200 return end();
201 }
202
203 /// lookup - Return the entry for the specified key, or a default
204 /// constructed value if no such entry exists.
205 [[nodiscard]] ValueT lookup(const_arg_type_t<KeyT> Val) const {
206 if (const BucketT *Bucket = doFind(Val))
207 return Bucket->getSecond();
208 return ValueT();
209 }
210
211 // Return the entry with the specified key, or \p Default. This variant is
212 // useful, because `lookup` cannot be used with non-default-constructible
213 // values.
214 template <typename U = std::remove_cv_t<ValueT>>
215 [[nodiscard]] ValueT lookup_or(const_arg_type_t<KeyT> Val,
216 U &&Default) const {
217 if (const BucketT *Bucket = doFind(Val))
218 return Bucket->getSecond();
219 return Default;
220 }
221
222 /// at - Return the entry for the specified key, or abort if no such
223 /// entry exists.
224 [[nodiscard]] ValueT &at(const_arg_type_t<KeyT> Val) {
225 auto Iter = this->find(std::move(Val));
226 assert(Iter != this->end() && "DenseMap::at failed due to a missing key");
227 return Iter->second;
228 }
229
230 /// at - Return the entry for the specified key, or abort if no such
231 /// entry exists.
232 [[nodiscard]] const ValueT &at(const_arg_type_t<KeyT> Val) const {
233 auto Iter = this->find(std::move(Val));
234 assert(Iter != this->end() && "DenseMap::at failed due to a missing key");
235 return Iter->second;
236 }
237
238 // Inserts key,value pair into the map if the key isn't already in the map.
239 // If the key is already in the map, it returns false and doesn't update the
240 // value.
241 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
242 return try_emplace_impl(KV.first, KV.second);
243 }
244
245 // Inserts key,value pair into the map if the key isn't already in the map.
246 // If the key is already in the map, it returns false and doesn't update the
247 // value.
248 std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
249 return try_emplace_impl(std::move(KV.first), std::move(KV.second));
250 }
251
252 // Inserts key,value pair into the map if the key isn't already in the map.
253 // The value is constructed in-place if the key is not in the map, otherwise
254 // it is not moved.
255 template <typename... Ts>
256 std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
257 return try_emplace_impl(std::move(Key), std::forward<Ts>(Args)...);
258 }
259
260 // Inserts key,value pair into the map if the key isn't already in the map.
261 // The value is constructed in-place if the key is not in the map, otherwise
262 // it is not moved.
263 template <typename... Ts>
264 std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
265 return try_emplace_impl(Key, std::forward<Ts>(Args)...);
266 }
267
268 /// Alternate version of insert() which allows a different, and possibly
269 /// less expensive, key type.
270 /// The DenseMapInfo is responsible for supplying methods
271 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
272 /// type used.
273 template <typename LookupKeyT>
274 std::pair<iterator, bool> insert_as(std::pair<KeyT, ValueT> &&KV,
275 const LookupKeyT &Val) {
276 BucketT *TheBucket;
277 if (LookupBucketFor(Val, TheBucket))
278 return {makeIterator(TheBucket), false}; // Already in map.
279
280 // Otherwise, insert the new element.
281 TheBucket = findBucketForInsertion(Val, TheBucket);
282 TheBucket->getFirst() = std::move(KV.first);
283 ::new (&TheBucket->getSecond()) ValueT(std::move(KV.second));
284 return {makeIterator(TheBucket), true};
285 }
286
287 /// insert - Range insertion of pairs.
288 template <typename InputIt> void insert(InputIt I, InputIt E) {
289 for (; I != E; ++I)
290 insert(*I);
291 }
292
293 /// Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
294 template <typename Range> void insert_range(Range &&R) {
295 insert(adl_begin(R), adl_end(R));
296 }
297
298 template <typename V>
299 std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
300 auto Ret = try_emplace(Key, std::forward<V>(Val));
301 if (!Ret.second)
302 Ret.first->second = std::forward<V>(Val);
303 return Ret;
304 }
305
306 template <typename V>
307 std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
308 auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
309 if (!Ret.second)
310 Ret.first->second = std::forward<V>(Val);
311 return Ret;
312 }
313
314 template <typename... Ts>
315 std::pair<iterator, bool> emplace_or_assign(const KeyT &Key, Ts &&...Args) {
316 auto Ret = try_emplace(Key, std::forward<Ts>(Args)...);
317 if (!Ret.second)
318 Ret.first->second = ValueT(std::forward<Ts>(Args)...);
319 return Ret;
320 }
321
322 template <typename... Ts>
323 std::pair<iterator, bool> emplace_or_assign(KeyT &&Key, Ts &&...Args) {
324 auto Ret = try_emplace(std::move(Key), std::forward<Ts>(Args)...);
325 if (!Ret.second)
326 Ret.first->second = ValueT(std::forward<Ts>(Args)...);
327 return Ret;
328 }
329
330 bool erase(const KeyT &Val) {
331 BucketT *TheBucket = doFind(Val);
332 if (!TheBucket)
333 return false; // not in map.
334
335 TheBucket->getSecond().~ValueT();
336 TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
337 decrementNumEntries();
338 incrementNumTombstones();
339 return true;
340 }
342 BucketT *TheBucket = &*I;
343 TheBucket->getSecond().~ValueT();
344 TheBucket->getFirst() = KeyInfoT::getTombstoneKey();
345 decrementNumEntries();
346 incrementNumTombstones();
347 }
348
349 ValueT &operator[](const KeyT &Key) {
350 return lookupOrInsertIntoBucket(Key).first->second;
351 }
352
353 ValueT &operator[](KeyT &&Key) {
354 return lookupOrInsertIntoBucket(std::move(Key)).first->second;
355 }
356
357 /// isPointerIntoBucketsArray - Return true if the specified pointer points
358 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
359 /// value in the DenseMap).
360 [[nodiscard]] bool isPointerIntoBucketsArray(const void *Ptr) const {
361 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
362 }
363
364 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
365 /// array. In conjunction with the previous method, this can be used to
366 /// determine whether an insertion caused the DenseMap to reallocate.
367 [[nodiscard]] const void *getPointerIntoBucketsArray() const {
368 return getBuckets();
369 }
370
371 void swap(DerivedT &RHS) {
372 this->incrementEpoch();
373 RHS.incrementEpoch();
374 derived().swapImpl(RHS);
375 }
376
377protected:
378 DenseMapBase() = default;
379
381
382 void initWithExactBucketCount(unsigned NewNumBuckets) {
383 if (derived().allocateBuckets(NewNumBuckets)) {
384 initEmpty();
385 } else {
386 setNumEntries(0);
387 setNumTombstones(0);
388 }
389 }
390
391 void destroyAll() {
392 // No need to iterate through the buckets if both KeyT and ValueT are
393 // trivially destructible.
394 if constexpr (std::is_trivially_destructible_v<KeyT> &&
395 std::is_trivially_destructible_v<ValueT>)
396 return;
397
398 if (getNumBuckets() == 0) // Nothing to do.
399 return;
400
401 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
402 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
403 for (BucketT &B : buckets()) {
404 if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
405 !KeyInfoT::isEqual(B.getFirst(), TombstoneKey))
406 B.getSecond().~ValueT();
407 B.getFirst().~KeyT();
408 }
409 }
410
411 void initEmpty() {
412 static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
413 "Must pass the derived type to this template!");
414 setNumEntries(0);
415 setNumTombstones(0);
416
417 assert((getNumBuckets() & (getNumBuckets() - 1)) == 0 &&
418 "# initial buckets must be a power of two!");
419 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
420 for (BucketT &B : buckets())
421 ::new (&B.getFirst()) KeyT(EmptyKey);
422 }
423
424 /// Returns the number of buckets to allocate to ensure that the DenseMap can
425 /// accommodate \p NumEntries without need to grow().
426 unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
427 // Ensure that "NumEntries * 4 < NumBuckets * 3"
428 if (NumEntries == 0)
429 return 0;
430 // +1 is required because of the strict inequality.
431 // For example, if NumEntries is 48, we need to return 128.
432 return NextPowerOf2(NumEntries * 4 / 3 + 1);
433 }
434
435 // Move key/value from Other to *this.
436 // Other is left in a valid but empty state.
437 void moveFrom(DerivedT &Other) {
438 // Insert all the old elements.
439 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
440 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
441 for (BucketT &B : Other.buckets()) {
442 if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
443 !KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {
444 // Insert the key/value into the new table.
445 BucketT *DestBucket;
446 bool FoundVal = LookupBucketFor(B.getFirst(), DestBucket);
447 (void)FoundVal; // silence warning.
448 assert(!FoundVal && "Key already in new map?");
449 DestBucket->getFirst() = std::move(B.getFirst());
450 ::new (&DestBucket->getSecond()) ValueT(std::move(B.getSecond()));
451 incrementNumEntries();
452
453 // Free the value.
454 B.getSecond().~ValueT();
455 }
456 B.getFirst().~KeyT();
457 }
458 Other.derived().kill();
459 }
460
461 void copyFrom(const DerivedT &other) {
462 this->destroyAll();
463 derived().deallocateBuckets();
464 setNumEntries(0);
465 setNumTombstones(0);
466 if (!derived().allocateBuckets(other.getNumBuckets())) {
467 // The bucket list is empty. No work to do.
468 return;
469 }
470
471 assert(&other != this);
472 assert(getNumBuckets() == other.getNumBuckets());
473
474 setNumEntries(other.getNumEntries());
475 setNumTombstones(other.getNumTombstones());
476
477 BucketT *Buckets = getBuckets();
478 const BucketT *OtherBuckets = other.getBuckets();
479 const size_t NumBuckets = getNumBuckets();
480 if constexpr (std::is_trivially_copyable_v<KeyT> &&
481 std::is_trivially_copyable_v<ValueT>) {
482 memcpy(reinterpret_cast<void *>(Buckets), OtherBuckets,
483 NumBuckets * sizeof(BucketT));
484 } else {
485 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
486 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
487 for (size_t I = 0; I < NumBuckets; ++I) {
488 ::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
489 if (!KeyInfoT::isEqual(Buckets[I].getFirst(), EmptyKey) &&
490 !KeyInfoT::isEqual(Buckets[I].getFirst(), TombstoneKey))
491 ::new (&Buckets[I].getSecond()) ValueT(OtherBuckets[I].getSecond());
492 }
493 }
494 }
495
496private:
497 DerivedT &derived() { return *static_cast<DerivedT *>(this); }
498 const DerivedT &derived() const {
499 return *static_cast<const DerivedT *>(this);
500 }
501
502 template <typename KeyArgT, typename... Ts>
503 std::pair<BucketT *, bool> lookupOrInsertIntoBucket(KeyArgT &&Key,
504 Ts &&...Args) {
505 BucketT *TheBucket = nullptr;
506 if (LookupBucketFor(Key, TheBucket))
507 return {TheBucket, false}; // Already in the map.
508
509 // Otherwise, insert the new element.
510 TheBucket = findBucketForInsertion(Key, TheBucket);
511 TheBucket->getFirst() = std::forward<KeyArgT>(Key);
512 ::new (&TheBucket->getSecond()) ValueT(std::forward<Ts>(Args)...);
513 return {TheBucket, true};
514 }
515
516 template <typename KeyArgT, typename... Ts>
517 std::pair<iterator, bool> try_emplace_impl(KeyArgT &&Key, Ts &&...Args) {
518 auto [Bucket, Inserted] = lookupOrInsertIntoBucket(
519 std::forward<KeyArgT>(Key), std::forward<Ts>(Args)...);
520 return {makeIterator(Bucket), Inserted};
521 }
522
523 iterator makeIterator(BucketT *TheBucket) {
524 return iterator::makeIterator(TheBucket, buckets(), *this);
525 }
526
527 const_iterator makeConstIterator(const BucketT *TheBucket) const {
528 return const_iterator::makeIterator(TheBucket, buckets(), *this);
529 }
530
531 unsigned getNumEntries() const { return derived().getNumEntries(); }
532
533 void setNumEntries(unsigned Num) { derived().setNumEntries(Num); }
534
535 void incrementNumEntries() { setNumEntries(getNumEntries() + 1); }
536
537 void decrementNumEntries() { setNumEntries(getNumEntries() - 1); }
538
539 unsigned getNumTombstones() const { return derived().getNumTombstones(); }
540
541 void setNumTombstones(unsigned Num) { derived().setNumTombstones(Num); }
542
543 void incrementNumTombstones() { setNumTombstones(getNumTombstones() + 1); }
544
545 void decrementNumTombstones() { setNumTombstones(getNumTombstones() - 1); }
546
547 const BucketT *getBuckets() const { return derived().getBuckets(); }
548
549 BucketT *getBuckets() { return derived().getBuckets(); }
550
551 unsigned getNumBuckets() const { return derived().getNumBuckets(); }
552
553 BucketT *getBucketsEnd() { return getBuckets() + getNumBuckets(); }
554
555 const BucketT *getBucketsEnd() const {
556 return getBuckets() + getNumBuckets();
557 }
558
559 iterator_range<BucketT *> buckets() {
560 return llvm::make_range(getBuckets(), getBucketsEnd());
561 }
562
563 iterator_range<const BucketT *> buckets() const {
564 return llvm::make_range(getBuckets(), getBucketsEnd());
565 }
566
567 void grow(unsigned MinNumBuckets) {
568 unsigned NumBuckets = DerivedT::roundUpNumBuckets(MinNumBuckets);
569 DerivedT Tmp(NumBuckets, ExactBucketCount{});
570 Tmp.moveFrom(derived());
571 if (derived().maybeMoveFast(std::move(Tmp)))
572 return;
573 initWithExactBucketCount(NumBuckets);
574 moveFrom(Tmp);
575 }
576
577 template <typename LookupKeyT>
578 BucketT *findBucketForInsertion(const LookupKeyT &Lookup,
579 BucketT *TheBucket) {
581
582 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
583 // the buckets are empty (meaning that many are filled with tombstones),
584 // grow the table.
585 //
586 // The later case is tricky. For example, if we had one empty bucket with
587 // tons of tombstones, failing lookups (e.g. for insertion) would have to
588 // probe almost the entire table until it found the empty bucket. If the
589 // table completely filled with tombstones, no lookup would ever succeed,
590 // causing infinite loops in lookup.
591 unsigned NewNumEntries = getNumEntries() + 1;
592 unsigned NumBuckets = getNumBuckets();
593 if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) {
594 this->grow(NumBuckets * 2);
595 LookupBucketFor(Lookup, TheBucket);
596 } else if (LLVM_UNLIKELY(NumBuckets -
597 (NewNumEntries + getNumTombstones()) <=
598 NumBuckets / 8)) {
599 this->grow(NumBuckets);
600 LookupBucketFor(Lookup, TheBucket);
601 }
602 assert(TheBucket);
603
604 // Only update the state after we've grown our bucket space appropriately
605 // so that when growing buckets we have self-consistent entry count.
606 incrementNumEntries();
607
608 // If we are writing over a tombstone, remember this.
609 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
610 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
611 decrementNumTombstones();
612
613 return TheBucket;
614 }
615
616 template <typename LookupKeyT>
617 const BucketT *doFind(const LookupKeyT &Val) const {
618 const BucketT *BucketsPtr = getBuckets();
619 const unsigned NumBuckets = getNumBuckets();
620 if (NumBuckets == 0)
621 return nullptr;
622
623 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
624 unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
625 unsigned ProbeAmt = 1;
626 while (true) {
627 const BucketT *Bucket = BucketsPtr + BucketNo;
628 if (LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
629 return Bucket;
630 if (LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
631 return nullptr;
632
633 // Otherwise, it's a hash collision or a tombstone, continue quadratic
634 // probing.
635 BucketNo += ProbeAmt++;
636 BucketNo &= NumBuckets - 1;
637 }
638 }
639
640 template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
641 return const_cast<BucketT *>(
642 static_cast<const DenseMapBase *>(this)->doFind(Val));
643 }
644
645 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
646 /// FoundBucket. If the bucket contains the key and a value, this returns
647 /// true, otherwise it returns a bucket with an empty marker or tombstone and
648 /// returns false.
649 template <typename LookupKeyT>
650 bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) {
651 BucketT *BucketsPtr = getBuckets();
652 const unsigned NumBuckets = getNumBuckets();
653
654 if (NumBuckets == 0) {
655 FoundBucket = nullptr;
656 return false;
657 }
658
659 // FoundTombstone - Keep track of whether we find a tombstone while probing.
660 BucketT *FoundTombstone = nullptr;
661 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
662 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
663 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
664 !KeyInfoT::isEqual(Val, TombstoneKey) &&
665 "Empty/Tombstone value shouldn't be inserted into map!");
666
667 unsigned BucketNo = KeyInfoT::getHashValue(Val) & (NumBuckets - 1);
668 unsigned ProbeAmt = 1;
669 while (true) {
670 BucketT *ThisBucket = BucketsPtr + BucketNo;
671 // Found Val's bucket? If so, return it.
672 if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
673 FoundBucket = ThisBucket;
674 return true;
675 }
676
677 // If we found an empty bucket, the key doesn't exist in the set.
678 // Insert it and return the default value.
679 if (LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
680 // If we've already seen a tombstone while probing, fill it in instead
681 // of the empty bucket we eventually probed to.
682 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
683 return false;
684 }
685
686 // If this is a tombstone, remember it. If Val ends up not in the map, we
687 // prefer to return it than something that would require more probing.
688 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
689 !FoundTombstone)
690 FoundTombstone = ThisBucket; // Remember the first tombstone found.
691
692 // Otherwise, it's a hash collision or a tombstone, continue quadratic
693 // probing.
694 BucketNo += ProbeAmt++;
695 BucketNo &= (NumBuckets - 1);
696 }
697 }
698
699public:
700 /// Return the approximate size (in bytes) of the actual map.
701 /// This is just the raw memory used by DenseMap.
702 /// If entries are pointers to objects, the size of the referenced objects
703 /// are not included.
704 [[nodiscard]] size_t getMemorySize() const {
705 return getNumBuckets() * sizeof(BucketT);
706 }
707};
708
709/// Equality comparison for DenseMap.
710///
711/// Iterates over elements of LHS confirming that each (key, value) pair in LHS
712/// is also in RHS, and that no additional pairs are in RHS.
713/// Equivalent to N calls to RHS.find and N value comparisons. Amortized
714/// complexity is linear, worst case is O(N^2) (if every hash collides).
715template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
716 typename BucketT>
717[[nodiscard]] bool
720 if (LHS.size() != RHS.size())
721 return false;
722
723 for (auto &KV : LHS) {
724 auto I = RHS.find(KV.first);
725 if (I == RHS.end() || I->second != KV.second)
726 return false;
727 }
728
729 return true;
730}
731
732/// Inequality comparison for DenseMap.
733///
734/// Equivalent to !(LHS == RHS). See operator== for performance notes.
735template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
736 typename BucketT>
737[[nodiscard]] bool
742
743template <typename KeyT, typename ValueT,
744 typename KeyInfoT = DenseMapInfo<KeyT>,
746class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
747 KeyT, ValueT, KeyInfoT, BucketT> {
748 friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
749
750 // Lift some types from the dependent base class into this class for
751 // simplicity of referring to them.
753
754 BucketT *Buckets;
755 unsigned NumEntries;
756 unsigned NumTombstones;
757 unsigned NumBuckets;
758
759 explicit DenseMap(unsigned NumBuckets, typename BaseT::ExactBucketCount) {
760 this->initWithExactBucketCount(NumBuckets);
761 }
762
763public:
764 /// Create a DenseMap with an optional \p NumElementsToReserve to guarantee
765 /// that this number of elements can be inserted in the map without grow().
766 explicit DenseMap(unsigned NumElementsToReserve = 0)
767 : DenseMap(BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
768 typename BaseT::ExactBucketCount{}) {}
769
770 DenseMap(const DenseMap &other) : DenseMap() { this->copyFrom(other); }
771
772 DenseMap(DenseMap &&other) : DenseMap() { this->swap(other); }
773
774 template <typename InputIt>
775 DenseMap(const InputIt &I, const InputIt &E) : DenseMap(std::distance(I, E)) {
776 this->insert(I, E);
777 }
778
779 template <typename RangeT>
781 : DenseMap(adl_begin(Range), adl_end(Range)) {}
782
783 DenseMap(std::initializer_list<typename BaseT::value_type> Vals)
784 : DenseMap(Vals.begin(), Vals.end()) {}
785
787 this->destroyAll();
788 deallocateBuckets();
789 }
790
791 DenseMap &operator=(const DenseMap &other) {
792 if (&other != this)
793 this->copyFrom(other);
794 return *this;
795 }
796
797 DenseMap &operator=(DenseMap &&other) {
798 this->destroyAll();
799 deallocateBuckets();
801 this->swap(other);
802 return *this;
803 }
804
805private:
806 void swapImpl(DenseMap &RHS) {
807 std::swap(Buckets, RHS.Buckets);
808 std::swap(NumEntries, RHS.NumEntries);
809 std::swap(NumTombstones, RHS.NumTombstones);
810 std::swap(NumBuckets, RHS.NumBuckets);
811 }
812
813 unsigned getNumEntries() const { return NumEntries; }
814
815 void setNumEntries(unsigned Num) { NumEntries = Num; }
816
817 unsigned getNumTombstones() const { return NumTombstones; }
818
819 void setNumTombstones(unsigned Num) { NumTombstones = Num; }
820
821 BucketT *getBuckets() const { return Buckets; }
822
823 unsigned getNumBuckets() const { return NumBuckets; }
824
825 void deallocateBuckets() {
826 deallocate_buffer(Buckets, sizeof(BucketT) * NumBuckets, alignof(BucketT));
827 }
828
829 bool allocateBuckets(unsigned Num) {
830 NumBuckets = Num;
831 if (NumBuckets == 0) {
832 Buckets = nullptr;
833 return false;
834 }
835
836 Buckets = static_cast<BucketT *>(
837 allocate_buffer(sizeof(BucketT) * NumBuckets, alignof(BucketT)));
838 return true;
839 }
840
841 // Put the zombie instance in a known good state after a move.
842 void kill() {
843 deallocateBuckets();
844 Buckets = nullptr;
845 NumBuckets = 0;
846 }
847
848 static unsigned roundUpNumBuckets(unsigned MinNumBuckets) {
849 return std::max(64u,
850 static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
851 }
852
853 bool maybeMoveFast(DenseMap &&Other) {
854 swapImpl(Other);
855 return true;
856 }
857
858 // Plan how to shrink the bucket table. Return:
859 // - {false, 0} to reuse the existing bucket table
860 // - {true, N} to reallocate a bucket table with N entries
861 std::pair<bool, unsigned> planShrinkAndClear() const {
862 unsigned NewNumBuckets = 0;
863 if (NumEntries)
864 NewNumBuckets = std::max(64u, 1u << (Log2_32_Ceil(NumEntries) + 1));
865 if (NewNumBuckets == NumBuckets)
866 return {false, 0}; // Reuse.
867 return {true, NewNumBuckets}; // Reallocate.
868 }
869};
870
871template <typename KeyT, typename ValueT, unsigned InlineBuckets = 4,
872 typename KeyInfoT = DenseMapInfo<KeyT>,
874class SmallDenseMap
875 : public DenseMapBase<
876 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
877 ValueT, KeyInfoT, BucketT> {
878 friend class DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
879
880 // Lift some types from the dependent base class into this class for
881 // simplicity of referring to them.
883
884 static_assert(isPowerOf2_64(InlineBuckets),
885 "InlineBuckets must be a power of 2.");
886
887 unsigned Small : 1;
888 unsigned NumEntries : 31;
889 unsigned NumTombstones;
890
891 struct LargeRep {
892 BucketT *Buckets;
893 unsigned NumBuckets;
895 return llvm::make_range(Buckets, Buckets + NumBuckets);
896 }
897 };
898
899 /// A "union" of an inline bucket array and the struct representing
900 /// a large bucket. This union will be discriminated by the 'Small' bit.
901 AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
902
903 SmallDenseMap(unsigned NumBuckets, typename BaseT::ExactBucketCount) {
904 this->initWithExactBucketCount(NumBuckets);
905 }
906
907public:
908 explicit SmallDenseMap(unsigned NumElementsToReserve = 0)
909 : SmallDenseMap(
910 BaseT::getMinBucketToReserveForEntries(NumElementsToReserve),
911 typename BaseT::ExactBucketCount{}) {}
912
913 SmallDenseMap(const SmallDenseMap &other) : SmallDenseMap() {
914 this->copyFrom(other);
915 }
916
917 SmallDenseMap(SmallDenseMap &&other) : SmallDenseMap() { this->swap(other); }
918
919 template <typename InputIt>
920 SmallDenseMap(const InputIt &I, const InputIt &E)
921 : SmallDenseMap(std::distance(I, E)) {
922 this->insert(I, E);
923 }
924
925 template <typename RangeT>
927 : SmallDenseMap(adl_begin(Range), adl_end(Range)) {}
928
929 SmallDenseMap(std::initializer_list<typename BaseT::value_type> Vals)
930 : SmallDenseMap(Vals.begin(), Vals.end()) {}
931
933 this->destroyAll();
934 deallocateBuckets();
935 }
936
937 SmallDenseMap &operator=(const SmallDenseMap &other) {
938 if (&other != this)
939 this->copyFrom(other);
940 return *this;
941 }
942
943 SmallDenseMap &operator=(SmallDenseMap &&other) {
944 this->destroyAll();
945 deallocateBuckets();
947 this->swap(other);
948 return *this;
949 }
950
951private:
952 void swapImpl(SmallDenseMap &RHS) {
953 unsigned TmpNumEntries = RHS.NumEntries;
954 RHS.NumEntries = NumEntries;
955 NumEntries = TmpNumEntries;
956 std::swap(NumTombstones, RHS.NumTombstones);
957
958 const KeyT EmptyKey = KeyInfoT::getEmptyKey();
959 const KeyT TombstoneKey = KeyInfoT::getTombstoneKey();
960 if (Small && RHS.Small) {
961 // If we're swapping inline bucket arrays, we have to cope with some of
962 // the tricky bits of DenseMap's storage system: the buckets are not
963 // fully initialized. Thus we swap every key, but we may have
964 // a one-directional move of the value.
965 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
966 BucketT *LHSB = &getInlineBuckets()[i],
967 *RHSB = &RHS.getInlineBuckets()[i];
968 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
969 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
970 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
971 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
972 if (hasLHSValue && hasRHSValue) {
973 // Swap together if we can...
974 std::swap(*LHSB, *RHSB);
975 continue;
976 }
977 // Swap separately and handle any asymmetry.
978 std::swap(LHSB->getFirst(), RHSB->getFirst());
979 if (hasLHSValue) {
980 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
981 LHSB->getSecond().~ValueT();
982 } else if (hasRHSValue) {
983 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
984 RHSB->getSecond().~ValueT();
985 }
986 }
987 return;
988 }
989 if (!Small && !RHS.Small) {
990 std::swap(*getLargeRep(), *RHS.getLargeRep());
991 return;
992 }
993
994 SmallDenseMap &SmallSide = Small ? *this : RHS;
995 SmallDenseMap &LargeSide = Small ? RHS : *this;
996
997 // First stash the large side's rep and move the small side across.
998 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
999 LargeSide.getLargeRep()->~LargeRep();
1000 LargeSide.Small = true;
1001 // This is similar to the standard move-from-old-buckets, but the bucket
1002 // count hasn't actually rotated in this case. So we have to carefully
1003 // move construct the keys and values into their new locations, but there
1004 // is no need to re-hash things.
1005 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
1006 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
1007 *OldB = &SmallSide.getInlineBuckets()[i];
1008 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
1009 OldB->getFirst().~KeyT();
1010 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
1011 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
1012 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
1013 OldB->getSecond().~ValueT();
1014 }
1015 }
1016
1017 // The hard part of moving the small buckets across is done, just move
1018 // the TmpRep into its new home.
1019 SmallSide.Small = false;
1020 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
1021 }
1022
1023 unsigned getNumEntries() const { return NumEntries; }
1024
1025 void setNumEntries(unsigned Num) {
1026 // NumEntries is hardcoded to be 31 bits wide.
1027 assert(Num < (1U << 31) && "Cannot support more than 1<<31 entries");
1028 NumEntries = Num;
1029 }
1030
1031 unsigned getNumTombstones() const { return NumTombstones; }
1032
1033 void setNumTombstones(unsigned Num) { NumTombstones = Num; }
1034
1035 const BucketT *getInlineBuckets() const {
1036 assert(Small);
1037 // Note that this cast does not violate aliasing rules as we assert that
1038 // the memory's dynamic type is the small, inline bucket buffer, and the
1039 // 'storage' is a POD containing a char buffer.
1040 return reinterpret_cast<const BucketT *>(&storage);
1041 }
1042
1043 BucketT *getInlineBuckets() {
1044 return const_cast<BucketT *>(
1045 const_cast<const SmallDenseMap *>(this)->getInlineBuckets());
1046 }
1047
1048 const LargeRep *getLargeRep() const {
1049 assert(!Small);
1050 // Note, same rule about aliasing as with getInlineBuckets.
1051 return reinterpret_cast<const LargeRep *>(&storage);
1052 }
1053
1054 LargeRep *getLargeRep() {
1055 return const_cast<LargeRep *>(
1056 const_cast<const SmallDenseMap *>(this)->getLargeRep());
1057 }
1058
1059 const BucketT *getBuckets() const {
1060 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1061 }
1062
1063 BucketT *getBuckets() {
1064 return const_cast<BucketT *>(
1065 const_cast<const SmallDenseMap *>(this)->getBuckets());
1066 }
1067
1068 unsigned getNumBuckets() const {
1069 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1070 }
1071
1072 iterator_range<BucketT *> inlineBuckets() {
1073 BucketT *Begin = getInlineBuckets();
1074 return llvm::make_range(Begin, Begin + InlineBuckets);
1075 }
1076
1077 void deallocateBuckets() {
1078 // Fast path to deallocateBuckets in case getLargeRep()->NumBuckets == 0,
1079 // just like destroyAll. This path is used to destruct zombie instances
1080 // after moves.
1081 if (Small || getLargeRep()->NumBuckets == 0)
1082 return;
1083
1084 deallocate_buffer(getLargeRep()->Buckets,
1085 sizeof(BucketT) * getLargeRep()->NumBuckets,
1086 alignof(BucketT));
1087 getLargeRep()->~LargeRep();
1088 }
1089
1090 bool allocateBuckets(unsigned Num) {
1091 if (Num <= InlineBuckets) {
1092 Small = true;
1093 } else {
1094 Small = false;
1095 BucketT *NewBuckets = static_cast<BucketT *>(
1096 allocate_buffer(sizeof(BucketT) * Num, alignof(BucketT)));
1097 new (getLargeRep()) LargeRep{NewBuckets, Num};
1098 }
1099 return true;
1100 }
1101
1102 // Put the zombie instance in a known good state after a move.
1103 void kill() {
1104 deallocateBuckets();
1105 Small = false;
1106 new (getLargeRep()) LargeRep{nullptr, 0};
1107 }
1108
1109 static unsigned roundUpNumBuckets(unsigned MinNumBuckets) {
1110 if (MinNumBuckets <= InlineBuckets)
1111 return MinNumBuckets;
1112 return std::max(64u,
1113 static_cast<unsigned>(NextPowerOf2(MinNumBuckets - 1)));
1114 }
1115
1116 bool maybeMoveFast(SmallDenseMap &&Other) {
1117 if (Other.Small)
1118 return false;
1119
1120 Small = false;
1121 NumEntries = Other.NumEntries;
1122 NumTombstones = Other.NumTombstones;
1123 *getLargeRep() = std::move(*Other.getLargeRep());
1124 Other.getLargeRep()->NumBuckets = 0;
1125 return true;
1126 }
1127
1128 // Plan how to shrink the bucket table. Return:
1129 // - {false, 0} to reuse the existing bucket table
1130 // - {true, N} to reallocate a bucket table with N entries
1131 std::pair<bool, unsigned> planShrinkAndClear() const {
1132 unsigned NewNumBuckets = 0;
1133 if (!this->empty()) {
1134 NewNumBuckets = 1u << (Log2_32_Ceil(this->size()) + 1);
1135 if (NewNumBuckets > InlineBuckets)
1136 NewNumBuckets = std::max(64u, NewNumBuckets);
1137 }
1138 bool Reuse = Small ? NewNumBuckets <= InlineBuckets
1139 : NewNumBuckets == getLargeRep()->NumBuckets;
1140 if (Reuse)
1141 return {false, 0}; // Reuse.
1142 return {true, NewNumBuckets}; // Reallocate.
1143 }
1144};
1145
1146template <typename KeyT, typename ValueT, typename KeyInfoT, typename Bucket,
1147 bool IsConst>
1148class DenseMapIterator : DebugEpochBase::HandleBase {
1149 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
1150 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, false>;
1151
1152public:
1154 using value_type = std::conditional_t<IsConst, const Bucket, Bucket>;
1157 using iterator_category = std::forward_iterator_tag;
1158
1159private:
1160 using BucketItTy =
1161 std::conditional_t<shouldReverseIterate<KeyT>(),
1162 std::reverse_iterator<pointer>, pointer>;
1163
1164 BucketItTy Ptr = {};
1165 BucketItTy End = {};
1166
1167 DenseMapIterator(BucketItTy Pos, BucketItTy E, const DebugEpochBase &Epoch)
1168 : DebugEpochBase::HandleBase(&Epoch), Ptr(Pos), End(E) {
1169 assert(isHandleInSync() && "invalid construction!");
1170 }
1171
1172public:
1173 DenseMapIterator() = default;
1174
1175 static DenseMapIterator makeBegin(iterator_range<pointer> Buckets,
1176 bool IsEmpty, const DebugEpochBase &Epoch) {
1177 // When the map is empty, avoid the overhead of advancing/retreating past
1178 // empty buckets.
1179 if (IsEmpty)
1180 return makeEnd(Buckets, Epoch);
1181 auto R = maybeReverse(Buckets);
1182 DenseMapIterator Iter(R.begin(), R.end(), Epoch);
1183 Iter.AdvancePastEmptyBuckets();
1184 return Iter;
1185 }
1186
1187 static DenseMapIterator makeEnd(iterator_range<pointer> Buckets,
1188 const DebugEpochBase &Epoch) {
1189 auto R = maybeReverse(Buckets);
1190 return DenseMapIterator(R.end(), R.end(), Epoch);
1191 }
1192
1193 static DenseMapIterator makeIterator(pointer P,
1195 const DebugEpochBase &Epoch) {
1196 auto R = maybeReverse(Buckets);
1197 constexpr int Offset = shouldReverseIterate<KeyT>() ? 1 : 0;
1198 return DenseMapIterator(BucketItTy(P + Offset), R.end(), Epoch);
1199 }
1200
1201 // Converting ctor from non-const iterators to const iterators. SFINAE'd out
1202 // for const iterator destinations so it doesn't end up as a user defined copy
1203 // constructor.
1204 template <bool IsConstSrc,
1205 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1207 const DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc> &I)
1208 : DebugEpochBase::HandleBase(I), Ptr(I.Ptr), End(I.End) {}
1209
1210 [[nodiscard]] reference operator*() const {
1211 assert(isHandleInSync() && "invalid iterator access!");
1212 assert(Ptr != End && "dereferencing end() iterator");
1213 return *Ptr;
1214 }
1215 [[nodiscard]] pointer operator->() const { return &operator*(); }
1216
1217 [[nodiscard]] friend bool operator==(const DenseMapIterator &LHS,
1218 const DenseMapIterator &RHS) {
1219 assert((!LHS.getEpochAddress() || LHS.isHandleInSync()) &&
1220 "handle not in sync!");
1221 assert((!RHS.getEpochAddress() || RHS.isHandleInSync()) &&
1222 "handle not in sync!");
1223 assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
1224 "comparing incomparable iterators!");
1225 return LHS.Ptr == RHS.Ptr;
1226 }
1227
1228 [[nodiscard]] friend bool operator!=(const DenseMapIterator &LHS,
1229 const DenseMapIterator &RHS) {
1230 return !(LHS == RHS);
1231 }
1232
1233 inline DenseMapIterator &operator++() { // Preincrement
1234 assert(isHandleInSync() && "invalid iterator access!");
1235 assert(Ptr != End && "incrementing end() iterator");
1236 ++Ptr;
1237 AdvancePastEmptyBuckets();
1238 return *this;
1239 }
1240 DenseMapIterator operator++(int) { // Postincrement
1241 assert(isHandleInSync() && "invalid iterator access!");
1242 DenseMapIterator tmp = *this;
1243 ++*this;
1244 return tmp;
1245 }
1246
1247private:
1248 void AdvancePastEmptyBuckets() {
1249 assert(Ptr <= End);
1250 const KeyT Empty = KeyInfoT::getEmptyKey();
1251 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1252
1253 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1254 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1255 ++Ptr;
1256 }
1257
1258 static auto maybeReverse(iterator_range<pointer> Range) {
1259 if constexpr (shouldReverseIterate<KeyT>())
1260 return reverse(Range);
1261 else
1262 return Range;
1263 }
1264};
1265
1266template <typename KeyT, typename ValueT, typename KeyInfoT>
1267[[nodiscard]] inline size_t
1269 return X.getMemorySize();
1270}
1271
1272} // end namespace llvm
1273
1274#endif // LLVM_ADT_DENSEMAP_H
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)
Definition Compiler.h:336
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file defines DenseMapInfo traits for DenseMap.
This file defines the DebugEpochBase and DebugEpochBase::HandleBase classes.
#define I(x, y, z)
Definition MD5.cpp:57
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
if(PassOpts->AAPipeline)
This file contains some templates that are useful if you are working with the STL at all.
This file contains library features backported from future STL versions.
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
LocallyHashedType DenseMapInfo< LocallyHashedType >::Tombstone
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
Value * RHS
Value * LHS
ValueT & at(const_arg_type_t< KeyT > Val)
at - Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:224
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
void copyFrom(const DerivedT &other)
Definition DenseMap.h:461
unsigned size_type
Definition DenseMap.h:69
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:256
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition DenseMap.h:248
bool erase(const KeyT &Val)
Definition DenseMap.h:330
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
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,...
Definition DenseMap.h:274
void moveFrom(DerivedT &Other)
Definition DenseMap.h:437
DenseMapBase()=default
const_iterator find_as(const LookupKeyT &Val) const
Definition DenseMap.h:197
const_iterator end() const
Definition DenseMap.h:87
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition DenseMap.h:191
unsigned size() const
Definition DenseMap.h:110
const_iterator find(const_arg_type_t< KeyT > Val) const
Definition DenseMap.h:181
bool empty() const
Definition DenseMap.h:109
std::pair< iterator, bool > emplace_or_assign(const KeyT &Key, Ts &&...Args)
Definition DenseMap.h:315
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
Definition DenseMap.h:288
iterator begin()
Definition DenseMap.h:78
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:174
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
const ValueT & at(const_arg_type_t< KeyT > Val) const
at - Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:232
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition DenseMap.h:360
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:169
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition DenseMap.h:264
const_iterator begin() const
Definition DenseMap.h:84
std::pair< iterator, bool > emplace_or_assign(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:323
void insert_range(Range &&R)
Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
Definition DenseMap.h:294
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition DenseMap.h:367
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
Definition DenseMap.h:307
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
Definition DenseMap.h:215
unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition DenseMap.h:426
void swap(DerivedT &RHS)
Definition DenseMap.h:371
ValueT & operator[](const KeyT &Key)
Definition DenseMap.h:349
BucketT value_type
Definition DenseMap.h:72
auto keys() const
Definition DenseMap.h:101
void initWithExactBucketCount(unsigned NewNumBuckets)
Definition DenseMap.h:382
void shrink_and_clear()
Definition DenseMap.h:157
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
void erase(iterator I)
Definition DenseMap.h:341
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
Definition DenseMap.h:299
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
ValueT & operator[](KeyT &&Key)
Definition DenseMap.h:353
auto values() const
Definition DenseMap.h:105
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition DenseMap.h:704
std::conditional_t< IsConst, const BucketT, BucketT > value_type
Definition DenseMap.h:1154
static DenseMapIterator makeIterator(pointer P, iterator_range< pointer > Buckets, const DebugEpochBase &Epoch)
Definition DenseMap.h:1193
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1228
DenseMapIterator & operator++()
Definition DenseMap.h:1233
pointer operator->() const
Definition DenseMap.h:1215
reference operator*() const
Definition DenseMap.h:1210
static DenseMapIterator makeBegin(iterator_range< pointer > Buckets, bool IsEmpty, const DebugEpochBase &Epoch)
Definition DenseMap.h:1175
DenseMapIterator operator++(int)
Definition DenseMap.h:1240
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
Definition DenseMap.h:1206
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1217
static DenseMapIterator makeEnd(iterator_range< pointer > Buckets, const DebugEpochBase &Epoch)
Definition DenseMap.h:1187
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:783
DenseMap(unsigned NumElementsToReserve=0)
Create a DenseMap with an optional NumElementsToReserve to guarantee that this number of elements can...
Definition DenseMap.h:766
DenseMap & operator=(DenseMap &&other)
Definition DenseMap.h:797
DenseMap(llvm::from_range_t, const RangeT &Range)
Definition DenseMap.h:780
DenseMap(const DenseMap &other)
Definition DenseMap.h:770
DenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:775
DenseMap(DenseMap &&other)
Definition DenseMap.h:772
DenseMap & operator=(const DenseMap &other)
Definition DenseMap.h:791
SmallDenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:920
SmallDenseMap & operator=(SmallDenseMap &&other)
Definition DenseMap.h:943
SmallDenseMap & operator=(const SmallDenseMap &other)
Definition DenseMap.h:937
SmallDenseMap(unsigned NumElementsToReserve=0)
Definition DenseMap.h:908
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:929
SmallDenseMap(SmallDenseMap &&other)
Definition DenseMap.h:917
SmallDenseMap(const SmallDenseMap &other)
Definition DenseMap.h:913
SmallDenseMap(llvm::from_range_t, const RangeT &Range)
Definition DenseMap.h:926
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...
Definition ADL.h:123
constexpr double e
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.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
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...
Definition ADL.h:78
BitVector::size_type capacity_in_bytes(const BitVector &X)
Definition BitVector.h:844
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2114
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.)
Definition MathExtras.h:284
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...
Definition ADL.h:86
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
auto map_range(ContainerTy &&C, FuncTy F)
Definition STLExtras.h:364
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.
Definition MemAlloc.cpp:15
LLVM_ABI void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
Definition MemAlloc.cpp:27
constexpr bool shouldReverseIterate()
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
std::conditional_t< std::is_pointer_v< T >, typename add_const_past_pointer< T >::type, const T & > type
Definition type_traits.h:53
const ValueT & getSecond() const
Definition DenseMap.h:51
const KeyT & getFirst() const
Definition DenseMap.h:49