Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/ADT/SparseMultiSet.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/ADT/SparseMultiSet.h - Sparse multiset --------------*- 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
// This file defines the SparseMultiSet class, which adds multiset behavior to
10
// the SparseSet.
11
//
12
// A sparse multiset holds a small number of objects identified by integer keys
13
// from a moderately sized universe. The sparse multiset uses more memory than
14
// other containers in order to provide faster operations. Any key can map to
15
// multiple values. A SparseMultiSetNode class is provided, which serves as a
16
// convenient base class for the contents of a SparseMultiSet.
17
//
18
//===----------------------------------------------------------------------===//
19
20
#ifndef LLVM_ADT_SPARSEMULTISET_H
21
#define LLVM_ADT_SPARSEMULTISET_H
22
23
#include "llvm/ADT/STLExtras.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include "llvm/ADT/SparseSet.h"
26
#include <cassert>
27
#include <cstdint>
28
#include <cstdlib>
29
#include <iterator>
30
#include <limits>
31
#include <utility>
32
33
namespace llvm {
34
35
/// Fast multiset implementation for objects that can be identified by small
36
/// unsigned keys.
37
///
38
/// SparseMultiSet allocates memory proportional to the size of the key
39
/// universe, so it is not recommended for building composite data structures.
40
/// It is useful for algorithms that require a single set with fast operations.
41
///
42
/// Compared to DenseSet and DenseMap, SparseMultiSet provides constant-time
43
/// fast clear() as fast as a vector.  The find(), insert(), and erase()
44
/// operations are all constant time, and typically faster than a hash table.
45
/// The iteration order doesn't depend on numerical key values, it only depends
46
/// on the order of insert() and erase() operations.  Iteration order is the
47
/// insertion order. Iteration is only provided over elements of equivalent
48
/// keys, but iterators are bidirectional.
49
///
50
/// Compared to BitVector, SparseMultiSet<unsigned> uses 8x-40x more memory, but
51
/// offers constant-time clear() and size() operations as well as fast iteration
52
/// independent on the size of the universe.
53
///
54
/// SparseMultiSet contains a dense vector holding all the objects and a sparse
55
/// array holding indexes into the dense vector.  Most of the memory is used by
56
/// the sparse array which is the size of the key universe. The SparseT template
57
/// parameter provides a space/speed tradeoff for sets holding many elements.
58
///
59
/// When SparseT is uint32_t, find() only touches up to 3 cache lines, but the
60
/// sparse array uses 4 x Universe bytes.
61
///
62
/// When SparseT is uint8_t (the default), find() touches up to 3+[N/256] cache
63
/// lines, but the sparse array is 4x smaller.  N is the number of elements in
64
/// the set.
65
///
66
/// For sets that may grow to thousands of elements, SparseT should be set to
67
/// uint16_t or uint32_t.
68
///
69
/// Multiset behavior is provided by providing doubly linked lists for values
70
/// that are inlined in the dense vector. SparseMultiSet is a good choice when
71
/// one desires a growable number of entries per key, as it will retain the
72
/// SparseSet algorithmic properties despite being growable. Thus, it is often a
73
/// better choice than a SparseSet of growable containers or a vector of
74
/// vectors. SparseMultiSet also keeps iterators valid after erasure (provided
75
/// the iterators don't point to the element erased), allowing for more
76
/// intuitive and fast removal.
77
///
78
/// @tparam ValueT      The type of objects in the set.
79
/// @tparam KeyFunctorT A functor that computes an unsigned index from KeyT.
80
/// @tparam SparseT     An unsigned integer type. See above.
81
///
82
template<typename ValueT,
83
         typename KeyFunctorT = identity<unsigned>,
84
         typename SparseT = uint8_t>
85
class SparseMultiSet {
86
  static_assert(std::numeric_limits<SparseT>::is_integer &&
87
                !std::numeric_limits<SparseT>::is_signed,
88
                "SparseT must be an unsigned integer type");
89
90
  /// The actual data that's stored, as a doubly-linked list implemented via
91
  /// indices into the DenseVector.  The doubly linked list is implemented
92
  /// circular in Prev indices, and INVALID-terminated in Next indices. This
93
  /// provides efficient access to list tails. These nodes can also be
94
  /// tombstones, in which case they are actually nodes in a single-linked
95
  /// freelist of recyclable slots.
96
  struct SMSNode {
97
    static const unsigned INVALID = ~0U;
98
99
    ValueT Data;
100
    unsigned Prev;
101
    unsigned Next;
102
103
30.8M
    SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) {}
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::SMSNode(llvm::VReg2SUnit, unsigned int, unsigned int)
Line
Count
Source
103
4.88M
    SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) {}
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode::SMSNode(llvm::PhysRegSUOper, unsigned int, unsigned int)
Line
Count
Source
103
13.7M
    SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) {}
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::SMSNode(llvm::VReg2SUnitOperIdx, unsigned int, unsigned int)
Line
Count
Source
103
12.2M
    SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) {}
104
105
    /// List tails have invalid Nexts.
106
40.8M
    bool isTail() const {
107
40.8M
      return Next == INVALID;
108
40.8M
    }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::isTail() const
Line
Count
Source
106
6.95M
    bool isTail() const {
107
6.95M
      return Next == INVALID;
108
6.95M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode::isTail() const
Line
Count
Source
106
25.5M
    bool isTail() const {
107
25.5M
      return Next == INVALID;
108
25.5M
    }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::isTail() const
Line
Count
Source
106
8.33M
    bool isTail() const {
107
8.33M
      return Next == INVALID;
108
8.33M
    }
109
110
    /// Whether this node is a tombstone node, and thus is in our freelist.
111
    bool isTombstone() const {
112
      return Prev == INVALID;
113
    }
114
115
    /// Since the list is circular in Prev, all non-tombstone nodes have a valid
116
    /// Prev.
117
40.9M
    bool isValid() const { return Prev != INVALID; }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::isValid() const
Line
Count
Source
117
6.95M
    bool isValid() const { return Prev != INVALID; }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode::isValid() const
Line
Count
Source
117
26.4M
    bool isValid() const { return Prev != INVALID; }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode::isValid() const
Line
Count
Source
117
7.53M
    bool isValid() const { return Prev != INVALID; }
118
  };
119
120
  using KeyT = typename KeyFunctorT::argument_type;
121
  using DenseT = SmallVector<SMSNode, 8>;
122
  DenseT Dense;
123
  SparseT *Sparse = nullptr;
124
  unsigned Universe = 0;
125
  KeyFunctorT KeyIndexOf;
126
  SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
127
128
  /// We have a built-in recycler for reusing tombstone slots. This recycler
129
  /// puts a singly-linked free list into tombstone slots, allowing us quick
130
  /// erasure, iterator preservation, and dense size.
131
  unsigned FreelistIdx = SMSNode::INVALID;
132
  unsigned NumFree = 0;
133
134
145M
  unsigned sparseIndex(const ValueT &Val) const {
135
145M
    assert(ValIndexOf(Val) < Universe &&
136
145M
           "Invalid key in set. Did object mutate?");
137
145M
    return ValIndexOf(Val);
138
145M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::sparseIndex(llvm::VReg2SUnit const&) const
Line
Count
Source
134
23.3M
  unsigned sparseIndex(const ValueT &Val) const {
135
23.3M
    assert(ValIndexOf(Val) < Universe &&
136
23.3M
           "Invalid key in set. Did object mutate?");
137
23.3M
    return ValIndexOf(Val);
138
23.3M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::sparseIndex(llvm::PhysRegSUOper const&) const
Line
Count
Source
134
94.1M
  unsigned sparseIndex(const ValueT &Val) const {
135
94.1M
    assert(ValIndexOf(Val) < Universe &&
136
94.1M
           "Invalid key in set. Did object mutate?");
137
94.1M
    return ValIndexOf(Val);
138
94.1M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::sparseIndex(llvm::VReg2SUnitOperIdx const&) const
Line
Count
Source
134
28.1M
  unsigned sparseIndex(const ValueT &Val) const {
135
28.1M
    assert(ValIndexOf(Val) < Universe &&
136
28.1M
           "Invalid key in set. Did object mutate?");
137
28.1M
    return ValIndexOf(Val);
138
28.1M
  }
139
114M
  unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::sparseIndex(llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&) const
Line
Count
Source
139
18.4M
  unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::sparseIndex(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode const&) const
Line
Count
Source
139
80.3M
  unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::sparseIndex(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&) const
Line
Count
Source
139
15.8M
  unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); }
140
141
  /// Whether the given entry is the head of the list. List heads's previous
142
  /// pointers are to the tail of the list, allowing for efficient access to the
143
  /// list tail. D must be a valid entry node.
144
40.8M
  bool isHead(const SMSNode &D) const {
145
40.8M
    assert(D.isValid() && "Invalid node for head");
146
40.8M
    return Dense[D.Prev].isTail();
147
40.8M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::isHead(llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&) const
Line
Count
Source
144
6.95M
  bool isHead(const SMSNode &D) const {
145
6.95M
    assert(D.isValid() && "Invalid node for head");
146
6.95M
    return Dense[D.Prev].isTail();
147
6.95M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::isHead(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode const&) const
Line
Count
Source
144
25.5M
  bool isHead(const SMSNode &D) const {
145
25.5M
    assert(D.isValid() && "Invalid node for head");
146
25.5M
    return Dense[D.Prev].isTail();
147
25.5M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::isHead(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&) const
Line
Count
Source
144
8.32M
  bool isHead(const SMSNode &D) const {
145
8.32M
    assert(D.isValid() && "Invalid node for head");
146
8.32M
    return Dense[D.Prev].isTail();
147
8.32M
  }
148
149
  /// Whether the given entry is a singleton entry, i.e. the only entry with
150
  /// that key.
151
11.4M
  bool isSingleton(const SMSNode &N) const {
152
11.4M
    assert(N.isValid() && "Invalid node for singleton");
153
11.4M
    // Is N its own predecessor?
154
11.4M
    return &Dense[N.Prev] == &N;
155
11.4M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::isSingleton(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode const&) const
Line
Count
Source
151
5.39M
  bool isSingleton(const SMSNode &N) const {
152
5.39M
    assert(N.isValid() && "Invalid node for singleton");
153
5.39M
    // Is N its own predecessor?
154
5.39M
    return &Dense[N.Prev] == &N;
155
5.39M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::isSingleton(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&) const
Line
Count
Source
151
6.02M
  bool isSingleton(const SMSNode &N) const {
152
6.02M
    assert(N.isValid() && "Invalid node for singleton");
153
6.02M
    // Is N its own predecessor?
154
6.02M
    return &Dense[N.Prev] == &N;
155
6.02M
  }
156
157
  /// Add in the given SMSNode. Uses a free entry in our freelist if
158
  /// available. Returns the index of the added node.
159
30.8M
  unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) {
160
30.8M
    if (NumFree == 0) {
161
25.1M
      Dense.push_back(SMSNode(V, Prev, Next));
162
25.1M
      return Dense.size() - 1;
163
25.1M
    }
164
5.68M
165
5.68M
    // Peel off a free slot
166
5.68M
    unsigned Idx = FreelistIdx;
167
5.68M
    unsigned NextFree = Dense[Idx].Next;
168
5.68M
    assert(Dense[Idx].isTombstone() && "Non-tombstone free?");
169
5.68M
170
5.68M
    Dense[Idx] = SMSNode(V, Prev, Next);
171
5.68M
    FreelistIdx = NextFree;
172
5.68M
    --NumFree;
173
5.68M
    return Idx;
174
5.68M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::addValue(llvm::VReg2SUnit const&, unsigned int, unsigned int)
Line
Count
Source
159
4.88M
  unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) {
160
4.88M
    if (NumFree == 0) {
161
4.88M
      Dense.push_back(SMSNode(V, Prev, Next));
162
4.88M
      return Dense.size() - 1;
163
4.88M
    }
164
0
165
0
    // Peel off a free slot
166
0
    unsigned Idx = FreelistIdx;
167
0
    unsigned NextFree = Dense[Idx].Next;
168
0
    assert(Dense[Idx].isTombstone() && "Non-tombstone free?");
169
0
170
0
    Dense[Idx] = SMSNode(V, Prev, Next);
171
0
    FreelistIdx = NextFree;
172
0
    --NumFree;
173
0
    return Idx;
174
0
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::addValue(llvm::PhysRegSUOper const&, unsigned int, unsigned int)
Line
Count
Source
159
13.7M
  unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) {
160
13.7M
    if (NumFree == 0) {
161
12.1M
      Dense.push_back(SMSNode(V, Prev, Next));
162
12.1M
      return Dense.size() - 1;
163
12.1M
    }
164
1.67M
165
1.67M
    // Peel off a free slot
166
1.67M
    unsigned Idx = FreelistIdx;
167
1.67M
    unsigned NextFree = Dense[Idx].Next;
168
1.67M
    assert(Dense[Idx].isTombstone() && "Non-tombstone free?");
169
1.67M
170
1.67M
    Dense[Idx] = SMSNode(V, Prev, Next);
171
1.67M
    FreelistIdx = NextFree;
172
1.67M
    --NumFree;
173
1.67M
    return Idx;
174
1.67M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::addValue(llvm::VReg2SUnitOperIdx const&, unsigned int, unsigned int)
Line
Count
Source
159
12.2M
  unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) {
160
12.2M
    if (NumFree == 0) {
161
8.19M
      Dense.push_back(SMSNode(V, Prev, Next));
162
8.19M
      return Dense.size() - 1;
163
8.19M
    }
164
4.01M
165
4.01M
    // Peel off a free slot
166
4.01M
    unsigned Idx = FreelistIdx;
167
4.01M
    unsigned NextFree = Dense[Idx].Next;
168
4.01M
    assert(Dense[Idx].isTombstone() && "Non-tombstone free?");
169
4.01M
170
4.01M
    Dense[Idx] = SMSNode(V, Prev, Next);
171
4.01M
    FreelistIdx = NextFree;
172
4.01M
    --NumFree;
173
4.01M
    return Idx;
174
4.01M
  }
175
176
  /// Make the current index a new tombstone. Pushes it onto the freelist.
177
11.4M
  void makeTombstone(unsigned Idx) {
178
11.4M
    Dense[Idx].Prev = SMSNode::INVALID;
179
11.4M
    Dense[Idx].Next = FreelistIdx;
180
11.4M
    FreelistIdx = Idx;
181
11.4M
    ++NumFree;
182
11.4M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::makeTombstone(unsigned int)
Line
Count
Source
177
5.39M
  void makeTombstone(unsigned Idx) {
178
5.39M
    Dense[Idx].Prev = SMSNode::INVALID;
179
5.39M
    Dense[Idx].Next = FreelistIdx;
180
5.39M
    FreelistIdx = Idx;
181
5.39M
    ++NumFree;
182
5.39M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::makeTombstone(unsigned int)
Line
Count
Source
177
6.02M
  void makeTombstone(unsigned Idx) {
178
6.02M
    Dense[Idx].Prev = SMSNode::INVALID;
179
6.02M
    Dense[Idx].Next = FreelistIdx;
180
6.02M
    FreelistIdx = Idx;
181
6.02M
    ++NumFree;
182
6.02M
  }
183
184
public:
185
  using value_type = ValueT;
186
  using reference = ValueT &;
187
  using const_reference = const ValueT &;
188
  using pointer = ValueT *;
189
  using const_pointer = const ValueT *;
190
  using size_type = unsigned;
191
192
2.42M
  SparseMultiSet() = default;
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::SparseMultiSet()
Line
Count
Source
192
928k
  SparseMultiSet() = default;
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SparseMultiSet()
Line
Count
Source
192
999k
  SparseMultiSet() = default;
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SparseMultiSet()
Line
Count
Source
192
499k
  SparseMultiSet() = default;
193
  SparseMultiSet(const SparseMultiSet &) = delete;
194
  SparseMultiSet &operator=(const SparseMultiSet &) = delete;
195
2.42M
  ~SparseMultiSet() { free(Sparse); }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::~SparseMultiSet()
Line
Count
Source
195
499k
  ~SparseMultiSet() { free(Sparse); }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::~SparseMultiSet()
Line
Count
Source
195
928k
  ~SparseMultiSet() { free(Sparse); }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::~SparseMultiSet()
Line
Count
Source
195
999k
  ~SparseMultiSet() { free(Sparse); }
196
197
  /// Set the universe size which determines the largest key the set can hold.
198
  /// The universe must be sized before any elements can be added.
199
  ///
200
  /// @param U Universe size. All object keys must be less than U.
201
  ///
202
12.2M
  void setUniverse(unsigned U) {
203
12.2M
    // It's not hard to resize the universe on a non-empty set, but it doesn't
204
12.2M
    // seem like a likely use case, so we can add that code when we need it.
205
12.2M
    assert(empty() && "Can only resize universe on an empty map");
206
12.2M
    // Hysteresis prevents needless reallocations.
207
12.2M
    if (
U >= Universe/412.2M
&& U <= Universe)
208
10.4M
      return;
209
1.83M
    free(Sparse);
210
1.83M
    // The Sparse array doesn't actually need to be initialized, so malloc
211
1.83M
    // would be enough here, but that will cause tools like valgrind to
212
1.83M
    // complain about branching on uninitialized data.
213
1.83M
    Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
214
1.83M
    Universe = U;
215
1.83M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::setUniverse(unsigned int)
Line
Count
Source
202
3.19M
  void setUniverse(unsigned U) {
203
3.19M
    // It's not hard to resize the universe on a non-empty set, but it doesn't
204
3.19M
    // seem like a likely use case, so we can add that code when we need it.
205
3.19M
    assert(empty() && "Can only resize universe on an empty map");
206
3.19M
    // Hysteresis prevents needless reallocations.
207
3.19M
    if (U >= Universe/4 && U <= Universe)
208
2.68M
      return;
209
507k
    free(Sparse);
210
507k
    // The Sparse array doesn't actually need to be initialized, so malloc
211
507k
    // would be enough here, but that will cause tools like valgrind to
212
507k
    // complain about branching on uninitialized data.
213
507k
    Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
214
507k
    Universe = U;
215
507k
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::setUniverse(unsigned int)
Line
Count
Source
202
6.06M
  void setUniverse(unsigned U) {
203
6.06M
    // It's not hard to resize the universe on a non-empty set, but it doesn't
204
6.06M
    // seem like a likely use case, so we can add that code when we need it.
205
6.06M
    assert(empty() && "Can only resize universe on an empty map");
206
6.06M
    // Hysteresis prevents needless reallocations.
207
6.06M
    if (
U >= Universe/46.06M
&& U <= Universe)
208
5.13M
      return;
209
928k
    free(Sparse);
210
928k
    // The Sparse array doesn't actually need to be initialized, so malloc
211
928k
    // would be enough here, but that will cause tools like valgrind to
212
928k
    // complain about branching on uninitialized data.
213
928k
    Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
214
928k
    Universe = U;
215
928k
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::setUniverse(unsigned int)
Line
Count
Source
202
3.03M
  void setUniverse(unsigned U) {
203
3.03M
    // It's not hard to resize the universe on a non-empty set, but it doesn't
204
3.03M
    // seem like a likely use case, so we can add that code when we need it.
205
3.03M
    assert(empty() && "Can only resize universe on an empty map");
206
3.03M
    // Hysteresis prevents needless reallocations.
207
3.03M
    if (U >= Universe/4 && 
U <= Universe3.03M
)
208
2.63M
      return;
209
400k
    free(Sparse);
210
400k
    // The Sparse array doesn't actually need to be initialized, so malloc
211
400k
    // would be enough here, but that will cause tools like valgrind to
212
400k
    // complain about branching on uninitialized data.
213
400k
    Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
214
400k
    Universe = U;
215
400k
  }
216
217
  /// Our iterators are iterators over the collection of objects that share a
218
  /// key.
219
  template<typename SMSPtrTy>
220
  class iterator_base : public std::iterator<std::bidirectional_iterator_tag,
221
                                             ValueT> {
222
    friend class SparseMultiSet;
223
224
    SMSPtrTy SMS;
225
    unsigned Idx;
226
    unsigned SparseIdx;
227
228
    iterator_base(SMSPtrTy P, unsigned I, unsigned SI)
229
456M
      : SMS(P), Idx(I), SparseIdx(SI) {}
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::iterator_base(llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*, unsigned int, unsigned int)
Line
Count
Source
229
79.2M
      : SMS(P), Idx(I), SparseIdx(SI) {}
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::iterator_base(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*, unsigned int, unsigned int)
Line
Count
Source
229
175M
      : SMS(P), Idx(I), SparseIdx(SI) {}
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*>::iterator_base(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*, unsigned int, unsigned int)
Line
Count
Source
229
144M
      : SMS(P), Idx(I), SparseIdx(SI) {}
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::iterator_base(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*, unsigned int, unsigned int)
Line
Count
Source
229
57.0M
      : SMS(P), Idx(I), SparseIdx(SI) {}
230
231
    /// Whether our iterator has fallen outside our dense vector.
232
1.10k
    bool isEnd() const {
233
1.10k
      if (Idx == SMSNode::INVALID)
234
1.10k
        return true;
235
0
236
0
      assert(Idx < SMS->Dense.size() && "Out of range, non-INVALID Idx?");
237
0
      return false;
238
0
    }
239
240
    /// Whether our iterator is properly keyed, i.e. the SparseIdx is valid
241
    bool isKeyed() const { return SparseIdx < SMS->Universe; }
242
243
4.77M
    unsigned Prev() const { return SMS->Dense[Idx].Prev; }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::Prev() const
Line
Count
Source
243
1.82M
    unsigned Prev() const { return SMS->Dense[Idx].Prev; }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::Prev() const
Line
Count
Source
243
733k
    unsigned Prev() const { return SMS->Dense[Idx].Prev; }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::Prev() const
Line
Count
Source
243
2.22M
    unsigned Prev() const { return SMS->Dense[Idx].Prev; }
244
37.1M
    unsigned Next() const { return SMS->Dense[Idx].Next; }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::Next() const
Line
Count
Source
244
28.0M
    unsigned Next() const { return SMS->Dense[Idx].Next; }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::Next() const
Line
Count
Source
244
8.89M
    unsigned Next() const { return SMS->Dense[Idx].Next; }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::Next() const
Line
Count
Source
244
160k
    unsigned Next() const { return SMS->Dense[Idx].Next; }
245
246
3.55k
    void setPrev(unsigned P) { SMS->Dense[Idx].Prev = P; }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::setPrev(unsigned int)
Line
Count
Source
246
164
    void setPrev(unsigned P) { SMS->Dense[Idx].Prev = P; }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::setPrev(unsigned int)
Line
Count
Source
246
3.39k
    void setPrev(unsigned P) { SMS->Dense[Idx].Prev = P; }
247
    void setNext(unsigned N) { SMS->Dense[Idx].Next = N; }
248
249
  public:
250
    using super = std::iterator<std::bidirectional_iterator_tag, ValueT>;
251
    using value_type = typename super::value_type;
252
    using difference_type = typename super::difference_type;
253
    using pointer = typename super::pointer;
254
    using reference = typename super::reference;
255
256
61.2M
    reference operator*() const {
257
61.2M
      assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx &&
258
61.2M
             "Dereferencing iterator of invalid key or index");
259
61.2M
260
61.2M
      return SMS->Dense[Idx].Data;
261
61.2M
    }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator*() const
Line
Count
Source
256
28.1M
    reference operator*() const {
257
28.1M
      assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx &&
258
28.1M
             "Dereferencing iterator of invalid key or index");
259
28.1M
260
28.1M
      return SMS->Dense[Idx].Data;
261
28.1M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::operator*() const
Line
Count
Source
256
14.6M
    reference operator*() const {
257
14.6M
      assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx &&
258
14.6M
             "Dereferencing iterator of invalid key or index");
259
14.6M
260
14.6M
      return SMS->Dense[Idx].Data;
261
14.6M
    }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator*() const
Line
Count
Source
256
18.5M
    reference operator*() const {
257
18.5M
      assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx &&
258
18.5M
             "Dereferencing iterator of invalid key or index");
259
18.5M
260
18.5M
      return SMS->Dense[Idx].Data;
261
18.5M
    }
262
54.5M
    pointer operator->() const { return &operator*(); }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator->() const
Line
Count
Source
262
21.4M
    pointer operator->() const { return &operator*(); }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::operator->() const
Line
Count
Source
262
14.6M
    pointer operator->() const { return &operator*(); }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator->() const
Line
Count
Source
262
18.5M
    pointer operator->() const { return &operator*(); }
263
264
    /// Comparison operators
265
203M
    bool operator==(const iterator_base &RHS) const {
266
203M
      // end compares equal
267
203M
      if (SMS == RHS.SMS && 
Idx == RHS.Idx203M
) {
268
137M
        assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
269
137M
               "Same dense entry, but different keys?");
270
137M
        return true;
271
137M
      }
272
65.5M
273
65.5M
      return false;
274
65.5M
    }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator==(llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*> const&) const
Line
Count
Source
265
56.2M
    bool operator==(const iterator_base &RHS) const {
266
56.2M
      // end compares equal
267
56.2M
      if (SMS == RHS.SMS && 
Idx == RHS.Idx56.2M
) {
268
26.3M
        assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
269
26.3M
               "Same dense entry, but different keys?");
270
26.3M
        return true;
271
26.3M
      }
272
29.9M
273
29.9M
      return false;
274
29.9M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*>::operator==(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*> const&) const
Line
Count
Source
265
72.4M
    bool operator==(const iterator_base &RHS) const {
266
72.4M
      // end compares equal
267
72.4M
      if (SMS == RHS.SMS && 
Idx == RHS.Idx72.4M
) {
268
60.2M
        assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
269
60.2M
               "Same dense entry, but different keys?");
270
60.2M
        return true;
271
60.2M
      }
272
12.1M
273
12.1M
      return false;
274
12.1M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::operator==(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*> const&) const
Line
Count
Source
265
48.8M
    bool operator==(const iterator_base &RHS) const {
266
48.8M
      // end compares equal
267
48.8M
      if (SMS == RHS.SMS && 
Idx == RHS.Idx48.8M
) {
268
33.8M
        assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
269
33.8M
               "Same dense entry, but different keys?");
270
33.8M
        return true;
271
33.8M
      }
272
15.0M
273
15.0M
      return false;
274
15.0M
    }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator==(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*> const&) const
Line
Count
Source
265
25.5M
    bool operator==(const iterator_base &RHS) const {
266
25.5M
      // end compares equal
267
25.5M
      if (SMS == RHS.SMS && Idx == RHS.Idx) {
268
17.1M
        assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
269
17.1M
               "Same dense entry, but different keys?");
270
17.1M
        return true;
271
17.1M
      }
272
8.40M
273
8.40M
      return false;
274
8.40M
    }
275
276
168M
    bool operator!=(const iterator_base &RHS) const {
277
168M
      return !operator==(RHS);
278
168M
    }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator!=(llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*> const&) const
Line
Count
Source
276
47.9M
    bool operator!=(const iterator_base &RHS) const {
277
47.9M
      return !operator==(RHS);
278
47.9M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*>::operator!=(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short> const*> const&) const
Line
Count
Source
276
72.4M
    bool operator!=(const iterator_base &RHS) const {
277
72.4M
      return !operator==(RHS);
278
72.4M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::operator!=(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*> const&) const
Line
Count
Source
276
35.0M
    bool operator!=(const iterator_base &RHS) const {
277
35.0M
      return !operator==(RHS);
278
35.0M
    }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator!=(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*> const&) const
Line
Count
Source
276
13.3M
    bool operator!=(const iterator_base &RHS) const {
277
13.3M
      return !operator==(RHS);
278
13.3M
    }
279
280
    /// Increment and decrement operators
281
1.10k
    iterator_base &operator--() { // predecrement - Back up
282
1.10k
      assert(isKeyed() && "Decrementing an invalid iterator");
283
1.10k
      assert((isEnd() || !SMS->isHead(SMS->Dense[Idx])) &&
284
1.10k
             "Decrementing head of list");
285
1.10k
286
1.10k
      // If we're at the end, then issue a new find()
287
1.10k
      if (isEnd())
288
1.10k
        Idx = SMS->findIndex(SparseIdx).Prev();
289
0
      else
290
0
        Idx = Prev();
291
1.10k
292
1.10k
      return *this;
293
1.10k
    }
294
37.1M
    iterator_base &operator++() { // preincrement - Advance
295
37.1M
      assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator");
296
37.1M
      Idx = Next();
297
37.1M
      return *this;
298
37.1M
    }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator++()
Line
Count
Source
294
28.0M
    iterator_base &operator++() { // preincrement - Advance
295
28.0M
      assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator");
296
28.0M
      Idx = Next();
297
28.0M
      return *this;
298
28.0M
    }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>::operator++()
Line
Count
Source
294
8.89M
    iterator_base &operator++() { // preincrement - Advance
295
8.89M
      assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator");
296
8.89M
      Idx = Next();
297
8.89M
      return *this;
298
8.89M
    }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>::operator++()
Line
Count
Source
294
160k
    iterator_base &operator++() { // preincrement - Advance
295
160k
      assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator");
296
160k
      Idx = Next();
297
160k
      return *this;
298
160k
    }
299
    iterator_base operator--(int) { // postdecrement
300
      iterator_base I(*this);
301
      --*this;
302
      return I;
303
    }
304
    iterator_base operator++(int) { // postincrement
305
      iterator_base I(*this);
306
      ++*this;
307
      return I;
308
    }
309
  };
310
311
  using iterator = iterator_base<SparseMultiSet *>;
312
  using const_iterator = iterator_base<const SparseMultiSet *>;
313
314
  // Convenience types
315
  using RangePair = std::pair<iterator, iterator>;
316
317
  /// Returns an iterator past this container. Note that such an iterator cannot
318
  /// be decremented, but will compare equal to other end iterators.
319
229M
  iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::end()
Line
Count
Source
319
67.4M
  iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::end()
Line
Count
Source
319
130M
  iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::end()
Line
Count
Source
319
31.6M
  iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); }
320
72.4M
  const_iterator end() const {
321
72.4M
    return const_iterator(this, SMSNode::INVALID, SMSNode::INVALID);
322
72.4M
  }
323
324
  /// Returns true if the set is empty.
325
  ///
326
  /// This is not the same as BitVector::empty().
327
  ///
328
  bool empty() const { return size() == 0; }
329
330
  /// Returns the number of elements in the set.
331
  ///
332
  /// This is not the same as BitVector::size() which returns the size of the
333
  /// universe.
334
  ///
335
  size_type size() const {
336
    assert(NumFree <= Dense.size() && "Out-of-bounds free entries");
337
    return Dense.size() - NumFree;
338
  }
339
340
  /// Clears the set.  This is a very fast constant time operation.
341
  ///
342
12.2M
  void clear() {
343
12.2M
    // Sparse does not need to be cleared, see find().
344
12.2M
    Dense.clear();
345
12.2M
    NumFree = 0;
346
12.2M
    FreelistIdx = SMSNode::INVALID;
347
12.2M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::clear()
Line
Count
Source
342
3.19M
  void clear() {
343
3.19M
    // Sparse does not need to be cleared, see find().
344
3.19M
    Dense.clear();
345
3.19M
    NumFree = 0;
346
3.19M
    FreelistIdx = SMSNode::INVALID;
347
3.19M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::clear()
Line
Count
Source
342
6.06M
  void clear() {
343
6.06M
    // Sparse does not need to be cleared, see find().
344
6.06M
    Dense.clear();
345
6.06M
    NumFree = 0;
346
6.06M
    FreelistIdx = SMSNode::INVALID;
347
6.06M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::clear()
Line
Count
Source
342
3.03M
  void clear() {
343
3.03M
    // Sparse does not need to be cleared, see find().
344
3.03M
    Dense.clear();
345
3.03M
    NumFree = 0;
346
3.03M
    FreelistIdx = SMSNode::INVALID;
347
3.03M
  }
348
349
  /// Find an element by its index.
350
  ///
351
  /// @param   Idx A valid index to find.
352
  /// @returns An iterator to the element identified by key, or end().
353
  ///
354
151M
  iterator findIndex(unsigned Idx) {
355
151M
    assert(Idx < Universe && "Key out of range");
356
151M
    const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
357
225M
    for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; 
i += Stride73.9M
) {
358
113M
      const unsigned FoundIdx = sparseIndex(Dense[i]);
359
113M
      // Check that we're pointing at the correct entry and that it is the head
360
113M
      // of a valid list.
361
113M
      if (Idx == FoundIdx && 
Dense[i].isValid()40.9M
&&
isHead(Dense[i])39.4M
)
362
39.4M
        return iterator(this, i, Idx);
363
73.9M
      // Stride is 0 when SparseT >= unsigned.  We don't need to loop.
364
73.9M
      if (!Stride)
365
0
        break;
366
73.9M
    }
367
151M
    
return end()111M
;
368
151M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::findIndex(unsigned int)
Line
Count
Source
354
24.7M
  iterator findIndex(unsigned Idx) {
355
24.7M
    assert(Idx < Universe && "Key out of range");
356
24.7M
    const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
357
36.2M
    for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; 
i += Stride11.4M
) {
358
18.4M
      const unsigned FoundIdx = sparseIndex(Dense[i]);
359
18.4M
      // Check that we're pointing at the correct entry and that it is the head
360
18.4M
      // of a valid list.
361
18.4M
      if (Idx == FoundIdx && 
Dense[i].isValid()6.95M
&&
isHead(Dense[i])6.95M
)
362
6.95M
        return iterator(this, i, Idx);
363
11.4M
      // Stride is 0 when SparseT >= unsigned.  We don't need to loop.
364
11.4M
      if (!Stride)
365
0
        break;
366
11.4M
    }
367
24.7M
    
return end()17.7M
;
368
24.7M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::findIndex(unsigned int)
Line
Count
Source
354
106M
  iterator findIndex(unsigned Idx) {
355
106M
    assert(Idx < Universe && "Key out of range");
356
106M
    const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
357
161M
    for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; 
i += Stride54.8M
) {
358
80.1M
      const unsigned FoundIdx = sparseIndex(Dense[i]);
359
80.1M
      // Check that we're pointing at the correct entry and that it is the head
360
80.1M
      // of a valid list.
361
80.1M
      if (Idx == FoundIdx && 
Dense[i].isValid()26.4M
&&
isHead(Dense[i])25.3M
)
362
25.3M
        return iterator(this, i, Idx);
363
54.8M
      // Stride is 0 when SparseT >= unsigned.  We don't need to loop.
364
54.8M
      if (!Stride)
365
0
        break;
366
54.8M
    }
367
106M
    
return end()81.6M
;
368
106M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::findIndex(unsigned int)
Line
Count
Source
354
19.4M
  iterator findIndex(unsigned Idx) {
355
19.4M
    assert(Idx < Universe && "Key out of range");
356
19.4M
    const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
357
26.9M
    for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; 
i += Stride7.56M
) {
358
14.7M
      const unsigned FoundIdx = sparseIndex(Dense[i]);
359
14.7M
      // Check that we're pointing at the correct entry and that it is the head
360
14.7M
      // of a valid list.
361
14.7M
      if (Idx == FoundIdx && 
Dense[i].isValid()7.53M
&&
isHead(Dense[i])7.18M
)
362
7.18M
        return iterator(this, i, Idx);
363
7.56M
      // Stride is 0 when SparseT >= unsigned.  We don't need to loop.
364
7.56M
      if (!Stride)
365
0
        break;
366
7.56M
    }
367
19.4M
    
return end()12.2M
;
368
19.4M
  }
369
370
  /// Find an element by its key.
371
  ///
372
  /// @param   Key A valid key to find.
373
  /// @returns An iterator to the element identified by key, or end().
374
  ///
375
47.8M
  iterator find(const KeyT &Key) {
376
47.8M
    return findIndex(KeyIndexOf(Key));
377
47.8M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::find(unsigned int const&)
Line
Count
Source
375
19.8M
  iterator find(const KeyT &Key) {
376
19.8M
    return findIndex(KeyIndexOf(Key));
377
19.8M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::find(unsigned int const&)
Line
Count
Source
375
20.7M
  iterator find(const KeyT &Key) {
376
20.7M
    return findIndex(KeyIndexOf(Key));
377
20.7M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::find(unsigned int const&)
Line
Count
Source
375
7.19M
  iterator find(const KeyT &Key) {
376
7.19M
    return findIndex(KeyIndexOf(Key));
377
7.19M
  }
378
379
72.4M
  const_iterator find(const KeyT &Key) const {
380
72.4M
    iterator I = const_cast<SparseMultiSet*>(this)->findIndex(KeyIndexOf(Key));
381
72.4M
    return const_iterator(I.SMS, I.Idx, KeyIndexOf(Key));
382
72.4M
  }
383
384
  /// Returns the number of elements identified by Key. This will be linear in
385
  /// the number of elements of that key.
386
  size_type count(const KeyT &Key) const {
387
    unsigned Ret = 0;
388
    for (const_iterator It = find(Key); It != end(); ++It)
389
      ++Ret;
390
391
    return Ret;
392
  }
393
394
  /// Returns true if this set contains an element identified by Key.
395
72.4M
  bool contains(const KeyT &Key) const {
396
72.4M
    return find(Key) != end();
397
72.4M
  }
398
399
  /// Return the head and tail of the subset's list, otherwise returns end().
400
  iterator getHead(const KeyT &Key) { return find(Key); }
401
  iterator getTail(const KeyT &Key) {
402
    iterator I = find(Key);
403
    if (I != end())
404
      I = iterator(this, I.Prev(), KeyIndexOf(Key));
405
    return I;
406
  }
407
408
  /// The bounds of the range of items sharing Key K. First member is the head
409
  /// of the list, and the second member is a decrementable end iterator for
410
  /// that key.
411
1.40k
  RangePair equal_range(const KeyT &K) {
412
1.40k
    iterator B = find(K);
413
1.40k
    iterator E = iterator(this, SMSNode::INVALID, B.SparseIdx);
414
1.40k
    return make_pair(B, E);
415
1.40k
  }
416
417
  /// Insert a new element at the tail of the subset list. Returns an iterator
418
  /// to the newly added entry.
419
30.8M
  iterator insert(const ValueT &Val) {
420
30.8M
    unsigned Idx = sparseIndex(Val);
421
30.8M
    iterator I = findIndex(Idx);
422
30.8M
423
30.8M
    unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID);
424
30.8M
425
30.8M
    if (I == end()) {
426
26.1M
      // Make a singleton list
427
26.1M
      Sparse[Idx] = NodeIdx;
428
26.1M
      Dense[NodeIdx].Prev = NodeIdx;
429
26.1M
      return iterator(this, NodeIdx, Idx);
430
26.1M
    }
431
4.77M
432
4.77M
    // Stick it at the end.
433
4.77M
    unsigned HeadIdx = I.Idx;
434
4.77M
    unsigned TailIdx = I.Prev();
435
4.77M
    Dense[TailIdx].Next = NodeIdx;
436
4.77M
    Dense[HeadIdx].Prev = NodeIdx;
437
4.77M
    Dense[NodeIdx].Prev = TailIdx;
438
4.77M
439
4.77M
    return iterator(this, NodeIdx, Idx);
440
4.77M
  }
llvm::SparseMultiSet<llvm::VReg2SUnit, llvm::VirtReg2IndexFunctor, unsigned char>::insert(llvm::VReg2SUnit const&)
Line
Count
Source
419
4.88M
  iterator insert(const ValueT &Val) {
420
4.88M
    unsigned Idx = sparseIndex(Val);
421
4.88M
    iterator I = findIndex(Idx);
422
4.88M
423
4.88M
    unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID);
424
4.88M
425
4.88M
    if (I == end()) {
426
3.06M
      // Make a singleton list
427
3.06M
      Sparse[Idx] = NodeIdx;
428
3.06M
      Dense[NodeIdx].Prev = NodeIdx;
429
3.06M
      return iterator(this, NodeIdx, Idx);
430
3.06M
    }
431
1.82M
432
1.82M
    // Stick it at the end.
433
1.82M
    unsigned HeadIdx = I.Idx;
434
1.82M
    unsigned TailIdx = I.Prev();
435
1.82M
    Dense[TailIdx].Next = NodeIdx;
436
1.82M
    Dense[HeadIdx].Prev = NodeIdx;
437
1.82M
    Dense[NodeIdx].Prev = TailIdx;
438
1.82M
439
1.82M
    return iterator(this, NodeIdx, Idx);
440
1.82M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::insert(llvm::PhysRegSUOper const&)
Line
Count
Source
419
13.7M
  iterator insert(const ValueT &Val) {
420
13.7M
    unsigned Idx = sparseIndex(Val);
421
13.7M
    iterator I = findIndex(Idx);
422
13.7M
423
13.7M
    unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID);
424
13.7M
425
13.7M
    if (I == end()) {
426
13.0M
      // Make a singleton list
427
13.0M
      Sparse[Idx] = NodeIdx;
428
13.0M
      Dense[NodeIdx].Prev = NodeIdx;
429
13.0M
      return iterator(this, NodeIdx, Idx);
430
13.0M
    }
431
732k
432
732k
    // Stick it at the end.
433
732k
    unsigned HeadIdx = I.Idx;
434
732k
    unsigned TailIdx = I.Prev();
435
732k
    Dense[TailIdx].Next = NodeIdx;
436
732k
    Dense[HeadIdx].Prev = NodeIdx;
437
732k
    Dense[NodeIdx].Prev = TailIdx;
438
732k
439
732k
    return iterator(this, NodeIdx, Idx);
440
732k
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::insert(llvm::VReg2SUnitOperIdx const&)
Line
Count
Source
419
12.2M
  iterator insert(const ValueT &Val) {
420
12.2M
    unsigned Idx = sparseIndex(Val);
421
12.2M
    iterator I = findIndex(Idx);
422
12.2M
423
12.2M
    unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID);
424
12.2M
425
12.2M
    if (I == end()) {
426
9.98M
      // Make a singleton list
427
9.98M
      Sparse[Idx] = NodeIdx;
428
9.98M
      Dense[NodeIdx].Prev = NodeIdx;
429
9.98M
      return iterator(this, NodeIdx, Idx);
430
9.98M
    }
431
2.22M
432
2.22M
    // Stick it at the end.
433
2.22M
    unsigned HeadIdx = I.Idx;
434
2.22M
    unsigned TailIdx = I.Prev();
435
2.22M
    Dense[TailIdx].Next = NodeIdx;
436
2.22M
    Dense[HeadIdx].Prev = NodeIdx;
437
2.22M
    Dense[NodeIdx].Prev = TailIdx;
438
2.22M
439
2.22M
    return iterator(this, NodeIdx, Idx);
440
2.22M
  }
441
442
  /// Erases an existing element identified by a valid iterator.
443
  ///
444
  /// This invalidates iterators pointing at the same entry, but erase() returns
445
  /// an iterator pointing to the next element in the subset's list. This makes
446
  /// it possible to erase selected elements while iterating over the subset:
447
  ///
448
  ///   tie(I, E) = Set.equal_range(Key);
449
  ///   while (I != E)
450
  ///     if (test(*I))
451
  ///       I = Set.erase(I);
452
  ///     else
453
  ///       ++I;
454
  ///
455
  /// Note that if the last element in the subset list is erased, this will
456
  /// return an end iterator which can be decremented to get the new tail (if it
457
  /// exists):
458
  ///
459
  ///  tie(B, I) = Set.equal_range(Key);
460
  ///  for (bool isBegin = B == I; !isBegin; /* empty */) {
461
  ///    isBegin = (--I) == B;
462
  ///    if (test(I))
463
  ///      break;
464
  ///    I = erase(I);
465
  ///  }
466
11.4M
  iterator erase(iterator I) {
467
11.4M
    assert(I.isKeyed() && !I.isEnd() && !Dense[I.Idx].isTombstone() &&
468
11.4M
           "erasing invalid/end/tombstone iterator");
469
11.4M
470
11.4M
    // First, unlink the node from its list. Then swap the node out with the
471
11.4M
    // dense vector's last entry
472
11.4M
    iterator NextI = unlink(Dense[I.Idx]);
473
11.4M
474
11.4M
    // Put in a tombstone.
475
11.4M
    makeTombstone(I.Idx);
476
11.4M
477
11.4M
    return NextI;
478
11.4M
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::erase(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::iterator_base<llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>*>)
Line
Count
Source
466
5.39M
  iterator erase(iterator I) {
467
5.39M
    assert(I.isKeyed() && !I.isEnd() && !Dense[I.Idx].isTombstone() &&
468
5.39M
           "erasing invalid/end/tombstone iterator");
469
5.39M
470
5.39M
    // First, unlink the node from its list. Then swap the node out with the
471
5.39M
    // dense vector's last entry
472
5.39M
    iterator NextI = unlink(Dense[I.Idx]);
473
5.39M
474
5.39M
    // Put in a tombstone.
475
5.39M
    makeTombstone(I.Idx);
476
5.39M
477
5.39M
    return NextI;
478
5.39M
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::erase(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::iterator_base<llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>*>)
Line
Count
Source
466
6.02M
  iterator erase(iterator I) {
467
6.02M
    assert(I.isKeyed() && !I.isEnd() && !Dense[I.Idx].isTombstone() &&
468
6.02M
           "erasing invalid/end/tombstone iterator");
469
6.02M
470
6.02M
    // First, unlink the node from its list. Then swap the node out with the
471
6.02M
    // dense vector's last entry
472
6.02M
    iterator NextI = unlink(Dense[I.Idx]);
473
6.02M
474
6.02M
    // Put in a tombstone.
475
6.02M
    makeTombstone(I.Idx);
476
6.02M
477
6.02M
    return NextI;
478
6.02M
  }
479
480
  /// Erase all elements with the given key. This invalidates all
481
  /// iterators of that key.
482
13.5M
  void eraseAll(const KeyT &K) {
483
18.9M
    for (iterator I = find(K); I != end(); /* empty */)
484
5.39M
      I = erase(I);
485
13.5M
  }
486
487
private:
488
  /// Unlink the node from its list. Returns the next node in the list.
489
11.4M
  iterator unlink(const SMSNode &N) {
490
11.4M
    if (isSingleton(N)) {
491
10.0M
      // Singleton is already unlinked
492
10.0M
      assert(N.Next == SMSNode::INVALID && "Singleton has next?");
493
10.0M
      return iterator(this, SMSNode::INVALID, ValIndexOf(N.Data));
494
10.0M
    }
495
1.35M
496
1.35M
    if (isHead(N)) {
497
1.35M
      // If we're the head, then update the sparse array and our next.
498
1.35M
      Sparse[sparseIndex(N)] = N.Next;
499
1.35M
      Dense[N.Next].Prev = N.Prev;
500
1.35M
      return iterator(this, N.Next, ValIndexOf(N.Data));
501
1.35M
    }
502
5.18k
503
5.18k
    if (N.isTail()) {
504
3.55k
      // If we're the tail, then update our head and our previous.
505
3.55k
      findIndex(sparseIndex(N)).setPrev(N.Prev);
506
3.55k
      Dense[N.Prev].Next = N.Next;
507
3.55k
508
3.55k
      // Give back an end iterator that can be decremented
509
3.55k
      iterator I(this, N.Prev, ValIndexOf(N.Data));
510
3.55k
      return ++I;
511
3.55k
    }
512
1.63k
513
1.63k
    // Otherwise, just drop us
514
1.63k
    Dense[N.Next].Prev = N.Prev;
515
1.63k
    Dense[N.Prev].Next = N.Next;
516
1.63k
    return iterator(this, N.Next, ValIndexOf(N.Data));
517
1.63k
  }
llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::unlink(llvm::SparseMultiSet<llvm::PhysRegSUOper, llvm::identity<unsigned int>, unsigned short>::SMSNode const&)
Line
Count
Source
489
5.39M
  iterator unlink(const SMSNode &N) {
490
5.39M
    if (isSingleton(N)) {
491
5.17M
      // Singleton is already unlinked
492
5.17M
      assert(N.Next == SMSNode::INVALID && "Singleton has next?");
493
5.17M
      return iterator(this, SMSNode::INVALID, ValIndexOf(N.Data));
494
5.17M
    }
495
214k
496
214k
    if (isHead(N)) {
497
214k
      // If we're the head, then update the sparse array and our next.
498
214k
      Sparse[sparseIndex(N)] = N.Next;
499
214k
      Dense[N.Next].Prev = N.Prev;
500
214k
      return iterator(this, N.Next, ValIndexOf(N.Data));
501
214k
    }
502
164
503
164
    if (N.isTail()) {
504
164
      // If we're the tail, then update our head and our previous.
505
164
      findIndex(sparseIndex(N)).setPrev(N.Prev);
506
164
      Dense[N.Prev].Next = N.Next;
507
164
508
164
      // Give back an end iterator that can be decremented
509
164
      iterator I(this, N.Prev, ValIndexOf(N.Data));
510
164
      return ++I;
511
164
    }
512
0
513
0
    // Otherwise, just drop us
514
0
    Dense[N.Next].Prev = N.Prev;
515
0
    Dense[N.Prev].Next = N.Next;
516
0
    return iterator(this, N.Next, ValIndexOf(N.Data));
517
0
  }
llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::unlink(llvm::SparseMultiSet<llvm::VReg2SUnitOperIdx, llvm::VirtReg2IndexFunctor, unsigned char>::SMSNode const&)
Line
Count
Source
489
6.02M
  iterator unlink(const SMSNode &N) {
490
6.02M
    if (isSingleton(N)) {
491
4.87M
      // Singleton is already unlinked
492
4.87M
      assert(N.Next == SMSNode::INVALID && "Singleton has next?");
493
4.87M
      return iterator(this, SMSNode::INVALID, ValIndexOf(N.Data));
494
4.87M
    }
495
1.14M
496
1.14M
    if (isHead(N)) {
497
1.13M
      // If we're the head, then update the sparse array and our next.
498
1.13M
      Sparse[sparseIndex(N)] = N.Next;
499
1.13M
      Dense[N.Next].Prev = N.Prev;
500
1.13M
      return iterator(this, N.Next, ValIndexOf(N.Data));
501
1.13M
    }
502
5.02k
503
5.02k
    if (N.isTail()) {
504
3.39k
      // If we're the tail, then update our head and our previous.
505
3.39k
      findIndex(sparseIndex(N)).setPrev(N.Prev);
506
3.39k
      Dense[N.Prev].Next = N.Next;
507
3.39k
508
3.39k
      // Give back an end iterator that can be decremented
509
3.39k
      iterator I(this, N.Prev, ValIndexOf(N.Data));
510
3.39k
      return ++I;
511
3.39k
    }
512
1.63k
513
1.63k
    // Otherwise, just drop us
514
1.63k
    Dense[N.Next].Prev = N.Prev;
515
1.63k
    Dense[N.Prev].Next = N.Next;
516
1.63k
    return iterator(this, N.Next, ValIndexOf(N.Data));
517
1.63k
  }
518
};
519
520
} // end namespace llvm
521
522
#endif // LLVM_ADT_SPARSEMULTISET_H