LLVM 24.0.0git
TinyPtrVector.h
Go to the documentation of this file.
1//===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- 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#ifndef LLVM_ADT_TINYPTRVECTOR_H
10#define LLVM_ADT_TINYPTRVECTOR_H
11
12#include "llvm/ADT/ArrayRef.h"
16#include <cassert>
17#include <cstddef>
18#include <iterator>
19
20namespace llvm {
21
22/// TinyPtrVector - This class is specialized for cases where there are
23/// normally 0 or 1 element in a vector, but is general enough to go beyond that
24/// when required.
25///
26template <typename EltTy> class LLVM_ATTRIBUTE_WARN_UNUSED TinyPtrVector {
27public:
29 using value_type = typename VecTy::value_type;
30 // A TinyPtrVector has three different possible representations internally:
31 // - A null VecTy *. This is an empty vector.
32 // - An EltTy. This is a one-element vector.
33 // - A non-null VecTy *. This is a pointer to a SmallVector (which may have
34 // any number of elements, including zero or one).
35 //
36 // EltTy must be the first pointer type so we can expose begin()/end()
37 // for a one-element vector using getAddrOfPtr1().
39
40private:
41 static constexpr VecTy *EmptyVal = nullptr;
42
43 bool isEmptyForm() const { return isa<VecTy *>(Val) && !cast<VecTy *>(Val); }
44 bool isSingleElementForm() const { return isa<EltTy>(Val); }
45 bool isMultiElementForm() const { return dyn_cast<VecTy *>(Val); }
46
47 PtrUnion Val;
48
49public:
50 // FIXME: Add a constructor to PtrUnion that allows marking this constexpr.
51 TinyPtrVector() : Val(EmptyVal) {}
52
54 if (VecTy *V = dyn_cast<VecTy *>(Val))
55 delete V;
56 }
57
58 TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
59 if (VecTy *V = dyn_cast<VecTy *>(Val))
60 Val = new VecTy(*V);
61 }
62
64 if (this == &RHS)
65 return *this;
66 if (RHS.empty()) {
67 this->clear();
68 return *this;
69 }
70
71 // Try to squeeze into the single slot. If it won't fit, allocate a copied
72 // vector.
73 if (!isMultiElementForm()) {
74 if (RHS.size() == 1)
75 Val = RHS.front();
76 else
77 Val = new VecTy(*cast<VecTy *>(RHS.Val));
78 return *this;
79 }
80
81 // If we have a full vector allocated, try to re-use it.
82 if (RHS.isSingleElementForm()) {
83 cast<VecTy *>(Val)->clear();
84 cast<VecTy *>(Val)->push_back(RHS.front());
85 } else {
86 *cast<VecTy *>(Val) = *cast<VecTy *>(RHS.Val);
87 }
88 return *this;
89 }
90
91 TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) { RHS.Val = EmptyVal; }
92
94 if (this == &RHS)
95 return *this;
96 if (RHS.empty()) {
97 this->clear();
98 return *this;
99 }
100
101 // If this vector has been allocated on the heap, re-use it if cheap. If it
102 // would require more copying, just delete it and we'll steal the other
103 // side.
104 if (VecTy *V = dyn_cast<VecTy *>(Val)) {
105 if (isa<EltTy>(RHS.Val)) {
106 V->clear();
107 V->push_back(RHS.front());
108 RHS.Val = EmptyVal;
109 return *this;
110 }
111 delete V;
112 }
113
114 Val = RHS.Val;
115 RHS.Val = EmptyVal;
116 return *this;
117 }
118
119 TinyPtrVector(std::initializer_list<EltTy> IL)
120 : Val(IL.size() == 0 ? PtrUnion(EmptyVal)
121 : IL.size() == 1 ? PtrUnion(*IL.begin())
122 : PtrUnion(new VecTy(IL.begin(), IL.end()))) {}
123
124 /// Constructor from an ArrayRef.
125 ///
126 /// This also is a constructor for individual array elements due to the single
127 /// element constructor for ArrayRef.
129 : Val(Elts.empty() ? PtrUnion(EmptyVal)
130 : Elts.size() == 1
131 ? PtrUnion(Elts[0])
132 : PtrUnion(new VecTy(Elts.begin(), Elts.end()))) {}
133
134 TinyPtrVector(size_t Count, EltTy Value)
135 : Val(Count == 0 ? PtrUnion(EmptyVal)
136 : Count == 1 ? PtrUnion(Value)
137 : PtrUnion(new VecTy(Count, Value))) {}
138
139 bool empty() const {
140 // This vector can be empty if it contains no element, or if it
141 // contains a pointer to an empty vector.
142 if (isSingleElementForm())
143 return false;
144 if (isEmptyForm())
145 return true;
146 return cast<VecTy *>(Val)->empty();
147 }
148
149 unsigned size() const {
150 if (isSingleElementForm())
151 return 1;
152 if (isEmptyForm())
153 return 0;
154 return cast<VecTy *>(Val)->size();
155 }
156
157 using iterator = EltTy *;
158 using const_iterator = const EltTy *;
159 using reverse_iterator = std::reverse_iterator<iterator>;
160 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
161
163 if (isSingleElementForm())
164 return Val.getAddrOfPtr1();
165
166 if (isEmptyForm()) {
167 // Any valid pointer with correct alignment works here. Using the same
168 // value as the one-element case slightly reduces the complexity of the
169 // generated code.
170 return reinterpret_cast<EltTy *>(this);
171 }
172
173 return cast<VecTy *>(Val)->begin();
174 }
175
177 if (isSingleElementForm())
178 return begin() + 1;
179
180 if (isEmptyForm())
181 return begin();
182
183 return cast<VecTy *>(Val)->end();
184 }
185
187 return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
188 }
189
191 return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
192 }
193
196
200
204
205 EltTy *data() { return begin(); }
206 const EltTy *data() const { return begin(); }
207
208 EltTy operator[](unsigned i) const {
209 assert(!empty() && "vector empty");
210 if (isSingleElementForm()) {
211 assert(i == 0 && "tinyvector index out of range");
212 return cast<EltTy>(Val);
213 }
214
215 assert(i < cast<VecTy *>(Val)->size() && "tinyvector index out of range");
216 return (*cast<VecTy *>(Val))[i];
217 }
218
219 EltTy front() const {
220 assert(!empty() && "vector empty");
221 if (isSingleElementForm())
222 return cast<EltTy>(Val);
223 return cast<VecTy *>(Val)->front();
224 }
225
226 EltTy back() const {
227 assert(!empty() && "vector empty");
228 if (isSingleElementForm())
229 return cast<EltTy>(Val);
230 return cast<VecTy *>(Val)->back();
231 }
232
233 void push_back(EltTy NewVal) {
234 // If we have nothing, add something.
235 if (isEmptyForm()) {
236 Val = NewVal;
237 return;
238 }
239
240 // If we have a single value, convert to a vector.
241 if (isSingleElementForm()) {
242 EltTy V = cast<EltTy>(Val);
243 Val = new VecTy({V, NewVal});
244 return;
245 }
246
247 // Add the new value, we know we have a vector.
248 cast<VecTy *>(Val)->push_back(NewVal);
249 }
250
251 void pop_back() {
252 // If we have a single value, convert to empty.
253 assert(!empty() && "vector empty");
254 if (isSingleElementForm())
255 Val = EmptyVal;
256 else
257 cast<VecTy *>(Val)->pop_back();
258 }
259
260 void clear() {
261 // If we have a single value, convert to empty.
262 if (isSingleElementForm()) {
263 Val = EmptyVal;
264 } else if (cast<VecTy *>(Val)) {
265 // If we have a vector form, just clear it.
266 cast<VecTy *>(Val)->clear();
267 }
268 }
269
271 assert(I >= begin() && "Iterator to erase is out of bounds.");
272 assert(I < end() && "Erasing at past-the-end iterator.");
273
274 // If we have a single value, convert to empty.
275 if (isSingleElementForm()) {
276 if (I == begin())
277 Val = EmptyVal;
278 } else {
279 // multiple items in a vector; just do the erase, there is no
280 // benefit to collapsing back to a pointer
281 return cast<VecTy *>(Val)->erase(I);
282 }
283 return end();
284 }
285
287 assert(S >= begin() && "Range to erase is out of bounds.");
288 assert(S <= E && "Trying to erase invalid range.");
289 assert(E <= end() && "Trying to erase past the end.");
290
291 if (isSingleElementForm()) {
292 if (S == begin() && S != E)
293 Val = EmptyVal;
294 } else if (isMultiElementForm()) {
295 return cast<VecTy *>(Val)->erase(S, E);
296 }
297 return end();
298 }
299
300 iterator insert(iterator I, const EltTy &Elt) {
301 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
302 assert(I <= this->end() && "Inserting past the end of the vector.");
303 if (I == end()) {
304 push_back(Elt);
305 return std::prev(end());
306 }
307 if (isSingleElementForm()) {
308 EltTy V = cast<EltTy>(Val);
309 assert(I == begin());
310 Val = Elt;
311 push_back(V);
312 return begin();
313 }
314
315 return cast<VecTy *>(Val)->insert(I, Elt);
316 }
317
318 template<typename ItTy>
320 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
321 assert(I <= this->end() && "Inserting past the end of the vector.");
322 if (From == To)
323 return I;
324
325 ptrdiff_t Offset = I - begin();
326 if (isEmptyForm()) {
327 if (std::next(From) == To) {
328 Val = *From;
329 return begin();
330 }
331
332 Val = new VecTy();
333 } else if (isSingleElementForm()) {
334 EltTy V = cast<EltTy>(Val);
335 Val = new VecTy();
336 cast<VecTy *>(Val)->push_back(V);
337 }
338 return cast<VecTy *>(Val)->insert(begin() + Offset, From, To);
339 }
340};
341
342} // end namespace llvm
343
344#endif // LLVM_ADT_TINYPTRVECTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file defines the SmallVector class.
Value * RHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A discriminated union of two or more pointer types, with the discriminator in the low bits of the poi...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
reverse_iterator rbegin()
const EltTy * const_iterator
TinyPtrVector(std::initializer_list< EltTy > IL)
void push_back(EltTy NewVal)
TinyPtrVector & operator=(TinyPtrVector &&RHS)
reverse_iterator rend()
std::reverse_iterator< const_iterator > const_reverse_iterator
const_reverse_iterator rbegin() const
TinyPtrVector(ArrayRef< EltTy > Elts)
Constructor from an ArrayRef.
std::reverse_iterator< iterator > reverse_iterator
EltTy front() const
const_iterator begin() const
iterator erase(iterator I)
TinyPtrVector & operator=(const TinyPtrVector &RHS)
TinyPtrVector(const TinyPtrVector &RHS)
const_reverse_iterator rend() const
const EltTy * data() const
const_iterator end() const
PointerUnion< EltTy, VecTy * > PtrUnion
iterator insert(iterator I, const EltTy &Elt)
iterator erase(iterator S, iterator E)
SmallVector< EltTy, 4 > VecTy
typename VecTy::value_type value_type
TinyPtrVector(TinyPtrVector &&RHS)
iterator insert(iterator I, ItTy From, ItTy To)
EltTy operator[](unsigned i) const
TinyPtrVector(size_t Count, EltTy Value)
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559