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