LLVM 23.0.0git
SmallVector.h
Go to the documentation of this file.
1//===- llvm/ADT/SmallVector.h - 'Normally small' 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/// \file
10/// This file defines the SmallVector class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_SMALLVECTOR_H
15#define LLVM_ADT_SMALLVECTOR_H
16
17#include "llvm/ADT/ADL.h"
20#include <algorithm>
21#include <cassert>
22#include <cstddef>
23#include <cstdint>
24#include <cstdlib>
25#include <cstring>
26#include <functional>
27#include <initializer_list>
28#include <iterator>
29#include <limits>
30#include <memory>
31#include <new>
32#include <type_traits>
33#include <utility>
34
35namespace llvm {
36
37template <typename T> class ArrayRef;
38
39template <typename IteratorT> class iterator_range;
40
41template <class Iterator, class Tag>
42using HasIteratorTag = std::is_convertible<
43 typename std::iterator_traits<Iterator>::iterator_category, Tag>;
44
45template <class Iterator>
47 std::enable_if_t<HasIteratorTag<Iterator, std::input_iterator_tag>::value>;
48
49/// This is all the stuff common to all SmallVectors.
50///
51/// The template parameter specifies the type which should be used to hold the
52/// Size and Capacity of the SmallVector, so it can be adjusted.
53/// Using 32 bit size is desirable to shrink the size of the SmallVector.
54/// Using 64 bit size is desirable for cases like SmallVector<char>, where a
55/// 32 bit size would limit the vector to ~4GB. SmallVectors are used for
56/// buffering bitcode output - which can exceed 4GB.
57template <class Size_T> class SmallVectorBase {
58protected:
59 void *BeginX;
60 Size_T Size = 0, Capacity;
61
62 /// The maximum value of the Size_T used.
63 static constexpr size_t SizeTypeMax() {
64 return std::numeric_limits<Size_T>::max();
65 }
66
67 SmallVectorBase() = delete;
68 SmallVectorBase(void *FirstEl, size_t TotalCapacity)
69 : BeginX(FirstEl), Capacity(static_cast<Size_T>(TotalCapacity)) {}
70
71 /// This is a helper for \a grow() that's out of line to reduce code
72 /// duplication. This function will report a fatal error if it can't grow at
73 /// least to \p MinSize.
74 LLVM_ABI void *mallocForGrow(void *FirstEl, size_t MinSize, size_t TSize,
75 size_t &NewCapacity);
76
77 /// This is an implementation of the grow() method which only works
78 /// on POD-like data types and is out of line to reduce code duplication.
79 /// This function will report a fatal error if it cannot increase capacity.
80 LLVM_ABI void grow_pod(void *FirstEl, size_t MinSize, size_t TSize);
81
82public:
83 size_t size() const { return Size; }
84 size_t capacity() const { return Capacity; }
85
86 [[nodiscard]] bool empty() const { return !Size; }
87
88protected:
89 /// Set the array size to \p N, which the current array must have enough
90 /// capacity for.
91 ///
92 /// This does not construct or destroy any elements in the vector.
93 void set_size(size_t N) {
94 assert(N <= capacity()); // implies no overflow in assignment
95 Size = static_cast<Size_T>(N);
96 }
97
98 /// Set the array data pointer to \p Begin and capacity to \p N.
99 ///
100 /// This does not construct or destroy any elements in the vector.
101 // This does not clean up any existing allocation.
102 void set_allocation_range(void *Begin, size_t N) {
103 assert(N <= SizeTypeMax());
104 BeginX = Begin;
105 Capacity = static_cast<Size_T>(N);
106 }
107};
108
109template <class T>
111 std::conditional_t<sizeof(T) < 4 && sizeof(void *) >= 8, uint64_t,
112 uint32_t>;
113
114/// Figure out the offset of the first element.
115template <class T, typename = void> struct SmallVectorAlignmentAndSize {
118 alignas(T) char FirstEl[sizeof(T)];
119};
120
121/// This is the part of SmallVectorTemplateBase which does not depend on whether
122/// the type T is a POD. The extra dummy template argument is used by ArrayRef
123/// to avoid unnecessarily requiring T to be complete.
124template <typename T, typename = void>
126 : public SmallVectorBase<SmallVectorSizeType<T>> {
128
129protected:
130 /// Find the address of the first element. For this pointer math to be valid
131 /// with small-size of 0 for T with lots of alignment, it's important that
132 /// SmallVectorStorage is properly-aligned even for small-size of 0.
133 void *getFirstEl() const {
134 return const_cast<void *>(reinterpret_cast<const void *>(
135 reinterpret_cast<const char *>(this) +
137 }
138 // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
139
140 SmallVectorTemplateCommon(size_t SizeArg) : Base(getFirstEl(), SizeArg) {}
141
142 void grow_pod(size_t MinSize, size_t TSize) {
143 Base::grow_pod(getFirstEl(), MinSize, TSize);
144 }
145
146 /// Return true if this is a smallvector which has not had dynamic
147 /// memory allocated for it.
148 bool isSmall() const { return this->BeginX == getFirstEl(); }
149
150 /// Put this vector in a state of being small.
152 this->BeginX = getFirstEl();
153 this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
154 }
155
156 /// Return true if V is an internal reference to the given range.
157 bool isReferenceToRange(const void *V, const void *First, const void *Last) const {
158 // Use std::less to avoid UB.
159 std::less<> LessThan;
160 return !LessThan(V, First) && LessThan(V, Last);
161 }
162
163 /// Return true if V is an internal reference to this vector.
164 bool isReferenceToStorage(const void *V) const {
165 return isReferenceToRange(V, this->begin(), this->end());
166 }
167
168 /// Return true if First and Last form a valid (possibly empty) range in this
169 /// vector's storage.
170 bool isRangeInStorage(const void *First, const void *Last) const {
171 // Use std::less to avoid UB.
172 std::less<> LessThan;
173 return !LessThan(First, this->begin()) && !LessThan(Last, First) &&
174 !LessThan(this->end(), Last);
175 }
176
177 /// Return true unless Elt will be invalidated by resizing the vector to
178 /// NewSize.
179 bool isSafeToReferenceAfterResize(const void *Elt, size_t NewSize) {
180 // Past the end.
182 return true;
183
184 // Return false if Elt will be destroyed by shrinking.
185 if (NewSize <= this->size())
186 return Elt < this->begin() + NewSize;
187
188 // Return false if we need to grow.
189 return NewSize <= this->capacity();
190 }
191
192 /// Check whether Elt will be invalidated by resizing the vector to NewSize.
193 void assertSafeToReferenceAfterResize(const void *Elt, size_t NewSize) {
194 assert(isSafeToReferenceAfterResize(Elt, NewSize) &&
195 "Attempting to reference an element of the vector in an operation "
196 "that invalidates it");
197 }
198
199 /// Check whether Elt will be invalidated by increasing the size of the
200 /// vector by N.
201 void assertSafeToAdd(const void *Elt, size_t N = 1) {
202 this->assertSafeToReferenceAfterResize(Elt, this->size() + N);
203 }
204
205 /// Check whether any part of the range will be invalidated by clearing.
206 template <class ItTy>
208 if constexpr (std::is_pointer_v<ItTy> &&
209 std::is_same_v<
210 std::remove_const_t<std::remove_pointer_t<ItTy>>,
211 std::remove_const_t<T>>) {
212 if (From == To)
213 return;
215 this->assertSafeToReferenceAfterResize(To - 1, 0);
216 }
217 (void)From;
218 (void)To;
219 }
220
221 /// Check whether any part of the range will be invalidated by growing.
222 template <class ItTy> void assertSafeToAddRange(ItTy From, ItTy To) {
223 if constexpr (std::is_pointer_v<ItTy> &&
224 std::is_same_v<std::remove_cv_t<std::remove_pointer_t<ItTy>>,
225 T>) {
226 if (From == To)
227 return;
228 this->assertSafeToAdd(From, To - From);
229 this->assertSafeToAdd(To - 1, To - From);
230 }
231 (void)From;
232 (void)To;
233 }
234
235 /// Reserve enough space to add one element, and return the updated element
236 /// pointer in case it was a reference to the storage.
237 template <class U>
238 static const T *reserveForParamAndGetAddressImpl(U *This, const T &Elt,
239 size_t N) {
240 size_t NewSize = This->size() + N;
241 if (LLVM_LIKELY(NewSize <= This->capacity()))
242 return &Elt;
243
244 bool ReferencesStorage = false;
245 int64_t Index = -1;
246 if (!U::TakesParamByValue) {
247 if (LLVM_UNLIKELY(This->isReferenceToStorage(&Elt))) {
248 ReferencesStorage = true;
249 Index = &Elt - This->begin();
250 }
251 }
252 This->grow(NewSize);
253 return ReferencesStorage ? This->begin() + Index : &Elt;
254 }
255
256public:
259 using value_type = T;
260 using iterator = T *;
261 using const_iterator = const T *;
262
263 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
264 using reverse_iterator = std::reverse_iterator<iterator>;
265
266 using reference = T &;
267 using const_reference = const T &;
268 using pointer = T *;
269 using const_pointer = const T *;
270
271 using Base::capacity;
272 using Base::empty;
273 using Base::size;
274
275 // forward iterator creation methods.
276 iterator begin() { return (iterator)this->BeginX; }
277 const_iterator begin() const { return (const_iterator)this->BeginX; }
278 iterator end() { return begin() + size(); }
279 const_iterator end() const { return begin() + size(); }
280
281 // reverse iterator creation methods.
286
287 size_type size_in_bytes() const { return size() * sizeof(T); }
289 return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T));
290 }
291
292 size_t capacity_in_bytes() const { return capacity() * sizeof(T); }
293
294 /// Return a pointer to the vector's buffer, even if empty().
295 pointer data() { return pointer(begin()); }
296 /// Return a pointer to the vector's buffer, even if empty().
297 const_pointer data() const { return const_pointer(begin()); }
298
300 assert(idx < size());
301 return begin()[idx];
302 }
304 assert(idx < size());
305 return begin()[idx];
306 }
307
309 assert(!empty());
310 return begin()[0];
311 }
313 assert(!empty());
314 return begin()[0];
315 }
316
318 assert(!empty());
319 return end()[-1];
320 }
322 assert(!empty());
323 return end()[-1];
324 }
325};
326
327/// SmallVectorTemplateBase<TriviallyCopyable = false> - This is where we put
328/// method implementations that are designed to work with non-trivial T's.
329///
330/// We approximate is_trivially_copyable with trivial move/copy construction and
331/// trivial destruction. While the standard doesn't specify that you're allowed
332/// copy these types with memcpy, there is no way for the type to observe this.
333/// This catches the important case of std::pair<POD, POD>, which is not
334/// trivially assignable.
335template <typename T, bool = (std::is_trivially_copy_constructible<T>::value) &&
336 (std::is_trivially_move_constructible<T>::value) &&
337 std::is_trivially_destructible<T>::value>
339 friend class SmallVectorTemplateCommon<T>;
340
341protected:
342 static constexpr bool TakesParamByValue = false;
343 using ValueParamT = const T &;
344
346 : SmallVectorTemplateCommon<T>(SizeArg) {}
347
348 static void destroy_range(T *S, T *E) {
349 while (S != E) {
350 --E;
351 E->~T();
352 }
353 }
354
355 /// Move the range [I, E) into the uninitialized memory starting with "Dest",
356 /// constructing elements as needed.
357 template<typename It1, typename It2>
358 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
359 std::uninitialized_move(I, E, Dest);
360 }
361
362 /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
363 /// constructing elements as needed.
364 template<typename It1, typename It2>
365 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
366 std::uninitialized_copy(I, E, Dest);
367 }
368
369 /// Grow the allocated memory (without initializing new elements), doubling
370 /// the size of the allocated memory. Guarantees space for at least one more
371 /// element, or MinSize more elements if specified.
372 void grow(size_t MinSize = 0);
373
374 /// Create a new allocation big enough for \p MinSize and pass back its size
375 /// in \p NewCapacity. This is the first section of \a grow().
376 T *mallocForGrow(size_t MinSize, size_t &NewCapacity);
377
378 /// Move existing elements over to the new allocation \p NewElts, the middle
379 /// section of \a grow().
380 void moveElementsForGrow(T *NewElts);
381
382 /// Transfer ownership of the allocation, finishing up \a grow().
383 void takeAllocationForGrow(T *NewElts, size_t NewCapacity);
384
385 /// Reserve enough space to add one element, and return the updated element
386 /// pointer in case it was a reference to the storage.
387 const T *reserveForParamAndGetAddress(const T &Elt, size_t N = 1) {
388 return this->reserveForParamAndGetAddressImpl(this, Elt, N);
389 }
390
391 /// Reserve enough space to add one element, and return the updated element
392 /// pointer in case it was a reference to the storage.
393 T *reserveForParamAndGetAddress(T &Elt, size_t N = 1) {
394 return const_cast<T *>(
395 this->reserveForParamAndGetAddressImpl(this, Elt, N));
396 }
397
398 static T &&forward_value_param(T &&V) { return std::move(V); }
399 static const T &forward_value_param(const T &V) { return V; }
400
401 void growAndAssign(size_t NumElts, const T &Elt) {
402 // Grow manually in case Elt is an internal reference.
403 size_t NewCapacity;
404 T *NewElts = mallocForGrow(NumElts, NewCapacity);
405 std::uninitialized_fill_n(NewElts, NumElts, Elt);
406 this->destroy_range(this->begin(), this->end());
407 takeAllocationForGrow(NewElts, NewCapacity);
408 this->set_size(NumElts);
409 }
410
411 template <typename... ArgTypes> T &growAndEmplaceBack(ArgTypes &&... Args) {
412 // Grow manually in case one of Args is an internal reference.
413 size_t NewCapacity;
414 T *NewElts = mallocForGrow(0, NewCapacity);
415 ::new ((void *)(NewElts + this->size())) T(std::forward<ArgTypes>(Args)...);
416 moveElementsForGrow(NewElts);
417 takeAllocationForGrow(NewElts, NewCapacity);
418 this->set_size(this->size() + 1);
419 return this->back();
420 }
421
422public:
423 void push_back(const T &Elt) {
424 const T *EltPtr = reserveForParamAndGetAddress(Elt);
425 ::new ((void *)this->end()) T(*EltPtr);
426 this->set_size(this->size() + 1);
427 }
428
429 void push_back(T &&Elt) {
430 T *EltPtr = reserveForParamAndGetAddress(Elt);
431 ::new ((void *)this->end()) T(::std::move(*EltPtr));
432 this->set_size(this->size() + 1);
433 }
434
435 void pop_back() {
436 this->set_size(this->size() - 1);
437 this->end()->~T();
438 }
439};
440
441// Define this out-of-line to dissuade the C++ compiler from inlining it.
442template <typename T, bool TriviallyCopyable>
444 size_t NewCapacity;
445 T *NewElts = mallocForGrow(MinSize, NewCapacity);
446 moveElementsForGrow(NewElts);
447 takeAllocationForGrow(NewElts, NewCapacity);
448}
449
450template <typename T, bool TriviallyCopyable>
452 size_t MinSize, size_t &NewCapacity) {
453 return static_cast<T *>(
455 this->getFirstEl(), MinSize, sizeof(T), NewCapacity));
456}
457
458// Define this out-of-line to dissuade the C++ compiler from inlining it.
459template <typename T, bool TriviallyCopyable>
461 T *NewElts) {
462 // Move the elements over.
463 this->uninitialized_move(this->begin(), this->end(), NewElts);
464
465 // Destroy the original elements.
466 destroy_range(this->begin(), this->end());
467}
468
469// Define this out-of-line to dissuade the C++ compiler from inlining it.
470template <typename T, bool TriviallyCopyable>
472 T *NewElts, size_t NewCapacity) {
473 // If this wasn't grown from the inline copy, deallocate the old space.
474 if (!this->isSmall())
475 free(this->begin());
476
477 this->set_allocation_range(NewElts, NewCapacity);
478}
479
480/// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
481/// method implementations that are designed to work with trivially copyable
482/// T's. This allows using memcpy in place of copy/move construction and
483/// skipping destruction.
484template <typename T>
486 friend class SmallVectorTemplateCommon<T>;
487
488protected:
489 /// True if it's cheap enough to take parameters by value. Doing so avoids
490 /// overhead related to mitigations for reference invalidation.
491 static constexpr bool TakesParamByValue = sizeof(T) <= 2 * sizeof(void *);
492
493 /// Either const T& or T, depending on whether it's cheap enough to take
494 /// parameters by value.
495 using ValueParamT = std::conditional_t<TakesParamByValue, T, const T &>;
496
498 : SmallVectorTemplateCommon<T>(SizeArg) {}
499
500 // No need to do a destroy loop for POD's.
501 static void destroy_range(T *, T *) {}
502
503 /// Move the range [I, E) onto the uninitialized memory
504 /// starting with "Dest", constructing elements into it as needed.
505 template <typename It1, typename It2>
506 static void uninitialized_move(It1 I, It1 E, It2 Dest) {
507 // Just do a copy.
508 uninitialized_copy(I, E, Dest);
509 }
510
511 /// Copy the range [I, E) onto the uninitialized memory
512 /// starting with "Dest", constructing elements into it as needed.
513 template <typename It1, typename It2>
514 static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
515 if constexpr (std::is_pointer_v<It1> && std::is_pointer_v<It2> &&
516 std::is_same_v<
517 std::remove_const_t<std::remove_pointer_t<It1>>,
518 std::remove_pointer_t<It2>>) {
519 // Use memcpy for PODs iterated by pointers (which includes SmallVector
520 // iterators): std::uninitialized_copy optimizes to memmove, but we can
521 // use memcpy here. Note that I and E are iterators and thus might be
522 // invalid for memcpy if they are equal.
523 if (I != E)
524 std::memcpy(reinterpret_cast<void *>(Dest), I, (E - I) * sizeof(T));
525 } else {
526 // Arbitrary iterator types; just use the basic implementation.
527 std::uninitialized_copy(I, E, Dest);
528 }
529 }
530
531 /// Double the size of the allocated memory, guaranteeing space for at
532 /// least one more element or MinSize if specified.
533 void grow(size_t MinSize = 0) { this->grow_pod(MinSize, sizeof(T)); }
534
535 /// Reserve enough space to add one element, and return the updated element
536 /// pointer in case it was a reference to the storage.
537 const T *reserveForParamAndGetAddress(const T &Elt, size_t N = 1) {
538 return this->reserveForParamAndGetAddressImpl(this, Elt, N);
539 }
540
541 /// Reserve enough space to add one element, and return the updated element
542 /// pointer in case it was a reference to the storage.
543 T *reserveForParamAndGetAddress(T &Elt, size_t N = 1) {
544 return const_cast<T *>(
545 this->reserveForParamAndGetAddressImpl(this, Elt, N));
546 }
547
548 /// Copy \p V or return a reference, depending on \a ValueParamT.
550
551 void growAndAssign(size_t NumElts, T Elt) {
552 // Elt has been copied in case it's an internal reference, side-stepping
553 // reference invalidation problems without losing the realloc optimization.
554 this->set_size(0);
555 this->grow(NumElts);
556 std::uninitialized_fill_n(this->begin(), NumElts, Elt);
557 this->set_size(NumElts);
558 }
559
560 template <typename... ArgTypes> T &growAndEmplaceBack(ArgTypes &&... Args) {
561 // Use push_back with a copy in case Args has an internal reference,
562 // side-stepping reference invalidation problems without losing the realloc
563 // optimization.
564 push_back(T(std::forward<ArgTypes>(Args)...));
565 return this->back();
566 }
567
568 // Out-of-line slow path so the inline push_back needs no callee-saved
569 // registers or stack frame on its hot path.
571 // Copy in case Elt is an internal reference invalidated by grow.
572 T Tmp = Elt;
573 this->grow(this->size() + 1);
574 std::memcpy(reinterpret_cast<void *>(this->end()), &Tmp, sizeof(T));
575 this->set_size(this->size() + 1);
576 }
577
578public:
580 if (LLVM_UNLIKELY(this->size() >= this->capacity()))
581 return growAndPushBack(Elt);
582 std::memcpy(reinterpret_cast<void *>(this->end()), &Elt, sizeof(T));
583 this->set_size(this->size() + 1);
584 }
585
586 void pop_back() { this->set_size(this->size() - 1); }
587};
588
589/// This class consists of common code factored out of the SmallVector class to
590/// reduce code duplication based on the SmallVector 'N' template parameter.
591template <typename T>
593 using SuperClass = SmallVectorTemplateBase<T>;
594
595public:
600
601protected:
604
605 // Default ctor - Initialize to empty.
606 explicit SmallVectorImpl(unsigned N)
608
610 this->destroy_range(this->begin(), this->end());
611 if (!this->isSmall())
612 free(this->begin());
613 this->BeginX = RHS.BeginX;
614 this->Size = RHS.Size;
615 this->Capacity = RHS.Capacity;
616 RHS.resetToSmall();
617 }
618
620 // Subclass has already destructed this vector's elements.
621 // If this wasn't grown from the inline copy, deallocate the old space.
622 if (!this->isSmall())
623 free(this->begin());
624 }
625
626public:
628
629 void clear() {
630 this->destroy_range(this->begin(), this->end());
631 this->Size = 0;
632 }
633
634private:
635 // Make set_size() private to avoid misuse in subclasses.
637
638 template <bool ForOverwrite> void resizeImpl(size_type N) {
639 if (N == this->size())
640 return;
641
642 if (N < this->size()) {
643 this->truncate(N);
644 return;
645 }
646
647 this->reserve(N);
648 for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
649 if (ForOverwrite)
650 new (&*I) T;
651 else
652 new (&*I) T();
653 this->set_size(N);
654 }
655
656public:
657 void resize(size_type N) { resizeImpl<false>(N); }
658
659 /// Like resize, but \ref T is POD, the new values won't be initialized.
660 void resize_for_overwrite(size_type N) { resizeImpl<true>(N); }
661
662 /// Like resize, but requires that \p N is less than \a size().
664 assert(this->size() >= N && "Cannot increase size with truncate");
665 this->destroy_range(this->begin() + N, this->end());
666 this->set_size(N);
667 }
668
670 if (N == this->size())
671 return;
672
673 if (N < this->size()) {
674 this->truncate(N);
675 return;
676 }
677
678 // N > this->size(). Defer to append.
679 this->append(N - this->size(), NV);
680 }
681
683 if (this->capacity() < N)
684 this->grow(N);
685 }
686
687 void pop_back_n(size_type NumItems) {
688 assert(this->size() >= NumItems);
689 truncate(this->size() - NumItems);
690 }
691
692 [[nodiscard]] T pop_back_val() {
693 T Result = ::std::move(this->back());
694 this->pop_back();
695 return Result;
696 }
697
699
700 /// Add the specified range to the end of the SmallVector.
701 template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>>
702 void append(ItTy in_start, ItTy in_end) {
704 this->assertSafeToAddRange(in_start, in_end);
705 size_type NumInputs = std::distance(in_start, in_end);
706 this->reserve(this->size() + NumInputs);
707 this->uninitialized_copy(in_start, in_end, this->end());
708 this->set_size(this->size() + NumInputs);
709 } else {
710 // Input iterator, we can't know ahead how many elements we'll add.
711 for (; in_start != in_end; ++in_start)
712 this->emplace_back(*in_start);
713 }
714 }
715
716 /// Append \p NumInputs copies of \p Elt to the end.
717 void append(size_type NumInputs, ValueParamT Elt) {
718 const T *EltPtr = this->reserveForParamAndGetAddress(Elt, NumInputs);
719 std::uninitialized_fill_n(this->end(), NumInputs, *EltPtr);
720 this->set_size(this->size() + NumInputs);
721 }
722
723 void append(std::initializer_list<T> IL) {
724 append(IL.begin(), IL.end());
725 }
726
727 void append(const SmallVectorImpl &RHS) { append(RHS.begin(), RHS.end()); }
728
729 void assign(size_type NumElts, ValueParamT Elt) {
730 // Note that Elt could be an internal reference.
731 if (NumElts > this->capacity()) {
732 this->growAndAssign(NumElts, Elt);
733 return;
734 }
735
736 // Assign over existing elements.
737 std::fill_n(this->begin(), std::min(NumElts, this->size()), Elt);
738 if (NumElts > this->size())
739 std::uninitialized_fill_n(this->end(), NumElts - this->size(), Elt);
740 else if (NumElts < this->size())
741 this->destroy_range(this->begin() + NumElts, this->end());
742 this->set_size(NumElts);
743 }
744
745 // FIXME: Consider assigning over existing elements, rather than clearing &
746 // re-initializing them - for all assign(...) variants.
747
748 template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>>
749 void assign(ItTy in_start, ItTy in_end) {
750 this->assertSafeToReferenceAfterClear(in_start, in_end);
751 clear();
752 append(in_start, in_end);
753 }
754
755 void assign(std::initializer_list<T> IL) {
756 clear();
757 append(IL);
758 }
759
760 void assign(const SmallVectorImpl &RHS) { assign(RHS.begin(), RHS.end()); }
761
762 template <typename U,
763 typename = std::enable_if_t<std::is_convertible_v<U, T>>>
765 assign(AR.begin(), AR.end());
766 }
767
769 // Just cast away constness because this is a non-const member function.
770 iterator I = const_cast<iterator>(CI);
771
772 assert(this->isReferenceToStorage(CI) && "Iterator to erase is out of bounds.");
773
774 iterator N = I;
775 // Shift all elts down one.
776 std::move(I+1, this->end(), I);
777 // Drop the last elt.
778 this->pop_back();
779 return(N);
780 }
781
783 // Just cast away constness because this is a non-const member function.
784 iterator S = const_cast<iterator>(CS);
785 iterator E = const_cast<iterator>(CE);
786
787 assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds.");
788
789 iterator N = S;
790 // Shift all elts down.
791 iterator I = std::move(E, this->end(), S);
792 // Drop the last elts.
793 this->destroy_range(I, this->end());
794 this->set_size(I - this->begin());
795 return(N);
796 }
797
798private:
799 template <class ArgType> iterator insert_one_impl(iterator I, ArgType &&Elt) {
800 // Callers ensure that ArgType is derived from T.
801 static_assert(
802 std::is_same<std::remove_const_t<std::remove_reference_t<ArgType>>,
803 T>::value,
804 "ArgType must be derived from T!");
805
806 if (I == this->end()) { // Important special case for empty vector.
807 this->push_back(::std::forward<ArgType>(Elt));
808 return this->end()-1;
809 }
810
811 assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.");
812
813 // Grow if necessary.
814 size_t Index = I - this->begin();
815 std::remove_reference_t<ArgType> *EltPtr =
817 I = this->begin() + Index;
818
819 ::new ((void*) this->end()) T(::std::move(this->back()));
820 // Push everything else over.
821 std::move_backward(I, this->end()-1, this->end());
822 this->set_size(this->size() + 1);
823
824 // If we just moved the element we're inserting, be sure to update
825 // the reference (never happens if TakesParamByValue).
826 static_assert(!TakesParamByValue || std::is_same<ArgType, T>::value,
827 "ArgType must be 'T' when taking by value!");
828 if (!TakesParamByValue && this->isReferenceToRange(EltPtr, I, this->end()))
829 ++EltPtr;
830
831 *I = ::std::forward<ArgType>(*EltPtr);
832 return I;
833 }
834
835public:
837 return insert_one_impl(I, this->forward_value_param(std::move(Elt)));
838 }
839
840 iterator insert(iterator I, const T &Elt) {
841 return insert_one_impl(I, this->forward_value_param(Elt));
842 }
843
845 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
846 size_t InsertElt = I - this->begin();
847
848 if (I == this->end()) { // Important special case for empty vector.
849 append(NumToInsert, Elt);
850 return this->begin()+InsertElt;
851 }
852
853 assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.");
854
855 // Ensure there is enough space, and get the (maybe updated) address of
856 // Elt.
857 const T *EltPtr = this->reserveForParamAndGetAddress(Elt, NumToInsert);
858
859 // Uninvalidate the iterator.
860 I = this->begin()+InsertElt;
861
862 // If there are more elements between the insertion point and the end of the
863 // range than there are being inserted, we can use a simple approach to
864 // insertion. Since we already reserved space, we know that this won't
865 // reallocate the vector.
866 if (size_t(this->end()-I) >= NumToInsert) {
867 T *OldEnd = this->end();
868 append(std::move_iterator<iterator>(this->end() - NumToInsert),
869 std::move_iterator<iterator>(this->end()));
870
871 // Copy the existing elements that get replaced.
872 std::move_backward(I, OldEnd-NumToInsert, OldEnd);
873
874 // If we just moved the element we're inserting, be sure to update
875 // the reference (never happens if TakesParamByValue).
876 if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
877 EltPtr += NumToInsert;
878
879 std::fill_n(I, NumToInsert, *EltPtr);
880 return I;
881 }
882
883 // Otherwise, we're inserting more elements than exist already, and we're
884 // not inserting at the end.
885
886 // Move over the elements that we're about to overwrite.
887 T *OldEnd = this->end();
888 this->set_size(this->size() + NumToInsert);
889 size_t NumOverwritten = OldEnd-I;
890 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
891
892 // If we just moved the element we're inserting, be sure to update
893 // the reference (never happens if TakesParamByValue).
894 if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
895 EltPtr += NumToInsert;
896
897 // Replace the overwritten part.
898 std::fill_n(I, NumOverwritten, *EltPtr);
899
900 // Insert the non-overwritten middle part.
901 std::uninitialized_fill_n(OldEnd, NumToInsert - NumOverwritten, *EltPtr);
902 return I;
903 }
904
905 template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>>
907 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
908 size_t InsertElt = I - this->begin();
909
910 if (I == this->end()) { // Important special case for empty vector.
911 append(From, To);
912 return this->begin()+InsertElt;
913 }
914
916 // For input iterators, we don't know the number of elements to insert.
917 size_t OldSize = this->size();
918 append(From, To);
919 I = this->begin() + InsertElt; // Uninvalidate the iterator.
920 std::rotate(I, this->begin() + OldSize, this->end());
921 return I;
922 }
923
924 assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.");
925
926 // Check that the reserve that follows doesn't invalidate the iterators.
927 this->assertSafeToAddRange(From, To);
928
929 size_t NumToInsert = std::distance(From, To);
930
931 // Ensure there is enough space.
932 reserve(this->size() + NumToInsert);
933
934 // Uninvalidate the iterator.
935 I = this->begin()+InsertElt;
936
937 // If there are more elements between the insertion point and the end of the
938 // range than there are being inserted, we can use a simple approach to
939 // insertion. Since we already reserved space, we know that this won't
940 // reallocate the vector.
941 if (size_t(this->end()-I) >= NumToInsert) {
942 T *OldEnd = this->end();
943 append(std::move_iterator<iterator>(this->end() - NumToInsert),
944 std::move_iterator<iterator>(this->end()));
945
946 // Copy the existing elements that get replaced.
947 std::move_backward(I, OldEnd-NumToInsert, OldEnd);
948
949 std::copy(From, To, I);
950 return I;
951 }
952
953 // Otherwise, we're inserting more elements than exist already, and we're
954 // not inserting at the end.
955
956 // Move over the elements that we're about to overwrite.
957 T *OldEnd = this->end();
958 this->set_size(this->size() + NumToInsert);
959 size_t NumOverwritten = OldEnd-I;
960 this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
961
962 // Replace the overwritten part.
963 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
964 *J = *From;
965 ++J; ++From;
966 }
967
968 // Insert the non-overwritten middle part.
969 this->uninitialized_copy(From, To, OldEnd);
970 return I;
971 }
972
973 void insert(iterator I, std::initializer_list<T> IL) {
974 insert(I, IL.begin(), IL.end());
975 }
976
977 template <typename... ArgTypes> reference emplace_back(ArgTypes &&... Args) {
978 if (LLVM_UNLIKELY(this->size() >= this->capacity()))
979 return this->growAndEmplaceBack(std::forward<ArgTypes>(Args)...);
980
981 ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
982 this->set_size(this->size() + 1);
983 return this->back();
984 }
985
987
989
990 bool operator==(const SmallVectorImpl &RHS) const {
991 if (this->size() != RHS.size()) return false;
992 return std::equal(this->begin(), this->end(), RHS.begin());
993 }
994 bool operator!=(const SmallVectorImpl &RHS) const {
995 return !(*this == RHS);
996 }
997
998 bool operator<(const SmallVectorImpl &RHS) const {
999 return std::lexicographical_compare(this->begin(), this->end(),
1000 RHS.begin(), RHS.end());
1001 }
1002 bool operator>(const SmallVectorImpl &RHS) const { return RHS < *this; }
1003 bool operator<=(const SmallVectorImpl &RHS) const { return !(*this > RHS); }
1004 bool operator>=(const SmallVectorImpl &RHS) const { return !(*this < RHS); }
1005};
1006
1007template <typename T>
1009 if (this == &RHS) return;
1010
1011 // We can only avoid copying elements if neither vector is small.
1012 if (!this->isSmall() && !RHS.isSmall()) {
1013 std::swap(this->BeginX, RHS.BeginX);
1014 std::swap(this->Size, RHS.Size);
1015 std::swap(this->Capacity, RHS.Capacity);
1016 return;
1017 }
1018 this->reserve(RHS.size());
1019 RHS.reserve(this->size());
1020
1021 // Swap the shared elements.
1022 size_t NumShared = this->size();
1023 if (NumShared > RHS.size()) NumShared = RHS.size();
1024 for (size_type i = 0; i != NumShared; ++i)
1025 std::swap((*this)[i], RHS[i]);
1026
1027 // Copy over the extra elts.
1028 if (this->size() > RHS.size()) {
1029 size_t EltDiff = this->size() - RHS.size();
1030 this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
1031 RHS.set_size(RHS.size() + EltDiff);
1032 this->destroy_range(this->begin()+NumShared, this->end());
1033 this->set_size(NumShared);
1034 } else if (RHS.size() > this->size()) {
1035 size_t EltDiff = RHS.size() - this->size();
1036 this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
1037 this->set_size(this->size() + EltDiff);
1038 this->destroy_range(RHS.begin()+NumShared, RHS.end());
1039 RHS.set_size(NumShared);
1040 }
1041}
1042
1043template <typename T>
1046 // Avoid self-assignment.
1047 if (this == &RHS) return *this;
1048
1049 // If we already have sufficient space, assign the common elements, then
1050 // destroy any excess.
1051 size_t RHSSize = RHS.size();
1052 size_t CurSize = this->size();
1053 if (CurSize >= RHSSize) {
1054 // Assign common elements.
1055 iterator NewEnd;
1056 if (RHSSize)
1057 NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
1058 else
1059 NewEnd = this->begin();
1060
1061 // Destroy excess elements.
1062 this->destroy_range(NewEnd, this->end());
1063
1064 // Trim.
1065 this->set_size(RHSSize);
1066 return *this;
1067 }
1068
1069 // If we have to grow to have enough elements, destroy the current elements.
1070 // This allows us to avoid copying them during the grow.
1071 // FIXME: don't do this if they're efficiently moveable.
1072 if (this->capacity() < RHSSize) {
1073 // Destroy current elements.
1074 this->clear();
1075 CurSize = 0;
1076 this->grow(RHSSize);
1077 } else if (CurSize) {
1078 // Otherwise, use assignment for the already-constructed elements.
1079 std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
1080 }
1081
1082 // Copy construct the new elements in place.
1083 this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
1084 this->begin()+CurSize);
1085
1086 // Set end.
1087 this->set_size(RHSSize);
1088 return *this;
1089}
1090
1091template <typename T>
1093 // Avoid self-assignment.
1094 if (this == &RHS) return *this;
1095
1096 // If the RHS isn't small, clear this vector and then steal its buffer.
1097 if (!RHS.isSmall()) {
1098 this->assignRemote(std::move(RHS));
1099 return *this;
1100 }
1101
1102 // If we already have sufficient space, assign the common elements, then
1103 // destroy any excess.
1104 size_t RHSSize = RHS.size();
1105 size_t CurSize = this->size();
1106 if (CurSize >= RHSSize) {
1107 // Assign common elements.
1108 iterator NewEnd = this->begin();
1109 if (RHSSize)
1110 NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
1111
1112 // Destroy excess elements and trim the bounds.
1113 this->destroy_range(NewEnd, this->end());
1114 this->set_size(RHSSize);
1115
1116 // Clear the RHS.
1117 RHS.clear();
1118
1119 return *this;
1120 }
1121
1122 // If we have to grow to have enough elements, destroy the current elements.
1123 // This allows us to avoid copying them during the grow.
1124 // FIXME: this may not actually make any sense if we can efficiently move
1125 // elements.
1126 if (this->capacity() < RHSSize) {
1127 // Destroy current elements.
1128 this->clear();
1129 CurSize = 0;
1130 this->grow(RHSSize);
1131 } else if (CurSize) {
1132 // Otherwise, use assignment for the already-constructed elements.
1133 std::move(RHS.begin(), RHS.begin()+CurSize, this->begin());
1134 }
1135
1136 // Move-construct the new elements in place.
1137 this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
1138 this->begin()+CurSize);
1139
1140 // Set end.
1141 this->set_size(RHSSize);
1142
1143 RHS.clear();
1144 return *this;
1145}
1146
1147/// Storage for the SmallVector elements. This is specialized for the N=0 case
1148/// to avoid allocating unnecessary storage.
1149template <typename T, unsigned N>
1151 alignas(T) char InlineElts[N * sizeof(T)];
1152};
1153
1154/// We need the storage to be properly aligned even for small-size of 0 so that
1155/// the pointer math in \a SmallVectorTemplateCommon::getFirstEl() is
1156/// well-defined.
1157template <typename T> struct alignas(T) SmallVectorStorage<T, 0> {};
1158
1159/// Forward declaration of SmallVector so that
1160/// calculateSmallVectorDefaultInlinedElements can reference
1161/// `sizeof(SmallVector<T, 0>)`.
1162template <typename T, unsigned N> class LLVM_GSL_OWNER SmallVector;
1163
1164/// Helper class for calculating the default number of inline elements for
1165/// `SmallVector<T>`.
1166///
1167/// This should be migrated to a constexpr function when our minimum
1168/// compiler support is enough for multi-statement constexpr functions.
1170 // Parameter controlling the default number of inlined elements
1171 // for `SmallVector<T>`.
1172 //
1173 // The default number of inlined elements ensures that
1174 // 1. There is at least one inlined element.
1175 // 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless
1176 // it contradicts 1.
1177 static constexpr size_t kPreferredSmallVectorSizeof = 64;
1178
1179 // static_assert that sizeof(T) is not "too big".
1180 //
1181 // Because our policy guarantees at least one inlined element, it is possible
1182 // for an arbitrarily large inlined element to allocate an arbitrarily large
1183 // amount of inline storage. We generally consider it an antipattern for a
1184 // SmallVector to allocate an excessive amount of inline storage, so we want
1185 // to call attention to these cases and make sure that users are making an
1186 // intentional decision if they request a lot of inline storage.
1187 //
1188 // We want this assertion to trigger in pathological cases, but otherwise
1189 // not be too easy to hit. To accomplish that, the cutoff is actually somewhat
1190 // larger than kPreferredSmallVectorSizeof (otherwise,
1191 // `SmallVector<SmallVector<T>>` would be one easy way to trip it, and that
1192 // pattern seems useful in practice).
1193 //
1194 // One wrinkle is that this assertion is in theory non-portable, since
1195 // sizeof(T) is in general platform-dependent. However, we don't expect this
1196 // to be much of an issue, because most LLVM development happens on 64-bit
1197 // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for
1198 // 32-bit hosts, dodging the issue. The reverse situation, where development
1199 // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a
1200 // 64-bit host, is expected to be very rare.
1201 static_assert(
1202 sizeof(T) <= 256,
1203 "You are trying to use a default number of inlined elements for "
1204 "`SmallVector<T>` but `sizeof(T)` is really big! Please use an "
1205 "explicit number of inlined elements with `SmallVector<T, N>` to make "
1206 "sure you really want that much inline storage.");
1207
1208 // Discount the size of the header itself when calculating the maximum inline
1209 // bytes.
1210 static constexpr size_t PreferredInlineBytes =
1212 static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
1213 static constexpr size_t value =
1215};
1216
1217/// This is a 'vector' (really, a variable-sized array), optimized
1218/// for the case when the array is small. It contains some number of elements
1219/// in-place, which allows it to avoid heap allocation when the actual number of
1220/// elements is below that threshold. This allows normal "small" cases to be
1221/// fast without losing generality for large inputs.
1222///
1223/// \note
1224/// In the absence of a well-motivated choice for the number of inlined
1225/// elements \p N, it is recommended to use \c SmallVector<T> (that is,
1226/// omitting the \p N). This will choose a default number of inlined elements
1227/// reasonable for allocation on the stack (for example, trying to keep \c
1228/// sizeof(SmallVector<T>) around 64 bytes).
1229///
1230/// \warning This does not attempt to be exception safe.
1231///
1232/// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
1233template <typename T,
1236 SmallVectorStorage<T, N> {
1237public:
1239
1241 // Destroy the constructed elements in the vector.
1242 this->destroy_range(this->begin(), this->end());
1243 }
1244
1245 explicit SmallVector(size_t SizeArg) : SmallVectorImpl<T>(N) {
1246 this->resize(SizeArg);
1247 }
1248
1249 SmallVector(size_t SizeArg, const T &Value) : SmallVectorImpl<T>(N) {
1250 this->assign(SizeArg, Value);
1251 }
1252
1253 template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>>
1255 this->append(S, E);
1256 }
1257
1258 template <typename RangeTy>
1260 : SmallVectorImpl<T>(N) {
1261 this->append(R.begin(), R.end());
1262 }
1263
1264 SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
1265 this->append(IL);
1266 }
1267
1268 template <typename U,
1269 typename = std::enable_if_t<std::is_convertible_v<U, T>>>
1271 this->append(A.begin(), A.end());
1272 }
1273
1275 if (!RHS.empty())
1277 }
1278
1281 return *this;
1282 }
1283
1285 if (!RHS.empty())
1286 SmallVectorImpl<T>::operator=(::std::move(RHS));
1287 }
1288
1290 if (!RHS.empty())
1291 SmallVectorImpl<T>::operator=(::std::move(RHS));
1292 }
1293
1295 if (N) {
1296 SmallVectorImpl<T>::operator=(::std::move(RHS));
1297 return *this;
1298 }
1299 // SmallVectorImpl<T>::operator= does not leverage N==0. Optimize the
1300 // case.
1301 if (this == &RHS)
1302 return *this;
1303 if (RHS.empty()) {
1304 this->destroy_range(this->begin(), this->end());
1305 this->Size = 0;
1306 } else {
1307 this->assignRemote(std::move(RHS));
1308 }
1309 return *this;
1310 }
1311
1313 SmallVectorImpl<T>::operator=(::std::move(RHS));
1314 return *this;
1315 }
1316
1317 SmallVector &operator=(std::initializer_list<T> IL) {
1318 this->assign(IL);
1319 return *this;
1320 }
1321};
1322
1323template <typename T, unsigned N>
1325 return X.capacity_in_bytes();
1326}
1327
1328template <typename RangeType>
1330 std::remove_const_t<detail::ValueOfRange<RangeType>>;
1331
1332/// Given a range of type R, iterate the entire range and return a
1333/// SmallVector with elements of the vector. This is useful, for example,
1334/// when you want to iterate a range and then sort the results.
1335template <unsigned Size, typename R>
1340template <typename R>
1345
1346template <typename Out, unsigned Size, typename R>
1350
1351template <typename Out, typename R> SmallVector<Out> to_vector_of(R &&Range) {
1353}
1354
1355// Explicit instantiations
1356extern template class llvm::SmallVectorBase<uint32_t>;
1357#if SIZE_MAX > UINT32_MAX
1358extern template class llvm::SmallVectorBase<uint64_t>;
1359#endif
1360
1361// Provide DenseMapInfo for SmallVector of a type which has info.
1362template <typename T, unsigned N> struct DenseMapInfo<llvm::SmallVector<T, N>> {
1363 static unsigned getHashValue(const SmallVector<T, N> &V) {
1364 return static_cast<unsigned>(hash_combine_range(V));
1365 }
1366
1367 static bool isEqual(const SmallVector<T, N> &LHS,
1368 const SmallVector<T, N> &RHS) {
1369 return LHS == RHS;
1370 }
1371};
1372
1373} // end namespace llvm
1374
1375namespace std {
1376
1377 /// Implement std::swap in terms of SmallVector swap.
1378 template<typename T>
1379 inline void
1383
1384 /// Implement std::swap in terms of SmallVector swap.
1385 template<typename T, unsigned N>
1386 inline void
1390
1391} // end namespace std
1392
1393#endif // LLVM_ADT_SMALLVECTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define X(NUM, ENUM, NAME)
Definition ELF.h:854
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:338
#define LLVM_GSL_OWNER
LLVM_GSL_OWNER - Apply this to owning classes like SmallVector to enable lifetime warnings.
Definition Compiler.h:423
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_ATTRIBUTE_NOINLINE
LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, mark a method "not for inl...
Definition Compiler.h:348
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:337
This file defines DenseMapInfo traits for DenseMap.
#define offsetof(TYPE, MEMBER)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
if(PassOpts->AAPipeline)
Value * RHS
Value * LHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
iterator begin() const
Definition ArrayRef.h:129
This is all the stuff common to all SmallVectors.
Definition SmallVector.h:57
LLVM_ABI void grow_pod(void *FirstEl, size_t MinSize, size_t TSize)
This is an implementation of the grow() method which only works on POD-like data types and is out of ...
LLVM_ABI void * mallocForGrow(void *FirstEl, size_t MinSize, size_t TSize, size_t &NewCapacity)
This is a helper for grow() that's out of line to reduce code duplication.
void set_allocation_range(void *Begin, size_t N)
Set the array data pointer to Begin and capacity to N.
SmallVectorBase(void *FirstEl, size_t TotalCapacity)
Definition SmallVector.h:68
void set_size(size_t N)
Set the array size to N, which the current array must have enough capacity for.
Definition SmallVector.h:93
size_t capacity() const
Definition SmallVector.h:84
size_t size() const
Definition SmallVector.h:83
static constexpr size_t SizeTypeMax()
The maximum value of the Size_T used.
Definition SmallVector.h:63
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
void append(const SmallVectorImpl &RHS)
void pop_back_n(size_type NumItems)
void assign(const SmallVectorImpl &RHS)
SmallVectorImpl(const SmallVectorImpl &)=delete
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
bool operator==(const SmallVectorImpl &RHS) const
void reserve(size_type N)
typename SuperClass::reference reference
iterator insert(iterator I, size_type NumToInsert, ValueParamT Elt)
iterator erase(const_iterator CI)
iterator insert(iterator I, ItTy From, ItTy To)
typename SuperClass::const_iterator const_iterator
void assignRemote(SmallVectorImpl &&RHS)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N, ValueParamT NV)
iterator insert(iterator I, MemoryLocation &&Elt)
bool operator>(const SmallVectorImpl &RHS) const
bool operator<(const SmallVectorImpl &RHS) const
iterator erase(const_iterator CS, const_iterator CE)
bool operator!=(const SmallVectorImpl &RHS) const
void truncate(size_type N)
Like resize, but requires that N is less than size().
void assign(ItTy in_start, ItTy in_end)
void assign(std::initializer_list< T > IL)
void assign(ArrayRef< U > AR)
SmallVectorImpl(unsigned N)
void swap(SmallVectorImpl &RHS)
typename SuperClass::iterator iterator
bool operator<=(const SmallVectorImpl &RHS) const
typename SuperClass::size_type size_type
void append(std::initializer_list< T > IL)
void append(size_type NumInputs, ValueParamT Elt)
Append NumInputs copies of Elt to the end.
void resize(size_type N)
SmallVectorImpl & operator=(const SmallVectorImpl &RHS)
void insert(iterator I, std::initializer_list< T > IL)
SmallVectorImpl & operator=(SmallVectorImpl &&RHS)
typename SuperClass::ValueParamT ValueParamT
bool operator>=(const SmallVectorImpl &RHS) const
iterator insert(iterator I, const T &Elt)
static void uninitialized_copy(It1 I, It1 E, It2 Dest)
Copy the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements into ...
static void uninitialized_move(It1 I, It1 E, It2 Dest)
Move the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements into ...
std::conditional_t< TakesParamByValue, T, const T & > ValueParamT
Either const T& or T, depending on whether it's cheap enough to take parameters by value.
const T * reserveForParamAndGetAddress(const T &Elt, size_t N=1)
Reserve enough space to add one element, and return the updated element pointer in case it was a refe...
T * reserveForParamAndGetAddress(T &Elt, size_t N=1)
Reserve enough space to add one element, and return the updated element pointer in case it was a refe...
static ValueParamT forward_value_param(ValueParamT V)
Copy V or return a reference, depending on ValueParamT.
T & growAndEmplaceBack(ArgTypes &&... Args)
static constexpr bool TakesParamByValue
True if it's cheap enough to take parameters by value.
void growAndAssign(size_t NumElts, T Elt)
void grow(size_t MinSize=0)
Double the size of the allocated memory, guaranteeing space for at least one more element or MinSize ...
LLVM_ATTRIBUTE_NOINLINE void growAndPushBack(ValueParamT Elt)
void moveElementsForGrow(T *NewElts)
Move existing elements over to the new allocation NewElts, the middle section of grow().
static void uninitialized_copy(It1 I, It1 E, It2 Dest)
Copy the range [I, E) onto the uninitialized memory starting with "Dest", constructing elements as ne...
static T && forward_value_param(T &&V)
static void destroy_range(T *S, T *E)
T * mallocForGrow(size_t MinSize, size_t &NewCapacity)
Create a new allocation big enough for MinSize and pass back its size in NewCapacity.
static constexpr bool TakesParamByValue
T * reserveForParamAndGetAddress(T &Elt, size_t N=1)
Reserve enough space to add one element, and return the updated element pointer in case it was a refe...
void takeAllocationForGrow(T *NewElts, size_t NewCapacity)
Transfer ownership of the allocation, finishing up grow().
void growAndAssign(size_t NumElts, const T &Elt)
static const T & forward_value_param(const T &V)
static void uninitialized_move(It1 I, It1 E, It2 Dest)
Move the range [I, E) into the uninitialized memory starting with "Dest", constructing elements as ne...
void grow(size_t MinSize=0)
Grow the allocated memory (without initializing new elements), doubling the size of the allocated mem...
const T * reserveForParamAndGetAddress(const T &Elt, size_t N=1)
Reserve enough space to add one element, and return the updated element pointer in case it was a refe...
SmallVectorTemplateBase(size_t SizeArg)
T & growAndEmplaceBack(ArgTypes &&... Args)
void push_back(const T &Elt)
bool isSmall() const
Return true if this is a smallvector which has not had dynamic memory allocated for it.
const_iterator end() const
static const T * reserveForParamAndGetAddressImpl(U *This, const T &Elt, size_t N)
Reserve enough space to add one element, and return the updated element pointer in case it was a refe...
SmallVectorTemplateCommon(size_t SizeArg)
const_reference operator[](size_type idx) const
void resetToSmall()
Put this vector in a state of being small.
bool isSafeToReferenceAfterResize(const void *Elt, size_t NewSize)
Return true unless Elt will be invalidated by resizing the vector to NewSize.
std::reverse_iterator< const_iterator > const_reverse_iterator
pointer data()
Return a pointer to the vector's buffer, even if empty().
bool isReferenceToRange(const void *V, const void *First, const void *Last) const
Return true if V is an internal reference to the given range.
void grow_pod(size_t MinSize, size_t TSize)
const_reverse_iterator rbegin() const
const_iterator begin() const
reference operator[](size_type idx)
size_type size_in_bytes() const
bool isReferenceToStorage(const void *V) const
Return true if V is an internal reference to this vector.
const_reference back() const
bool isRangeInStorage(const void *First, const void *Last) const
Return true if First and Last form a valid (possibly empty) range in this vector's storage.
const_reference front() const
void assertSafeToAdd(const void *Elt, size_t N=1)
Check whether Elt will be invalidated by increasing the size of the vector by N.
void assertSafeToReferenceAfterResize(const void *Elt, size_t NewSize)
Check whether Elt will be invalidated by resizing the vector to NewSize.
void assertSafeToAddRange(ItTy From, ItTy To)
Check whether any part of the range will be invalidated by growing.
std::reverse_iterator< iterator > reverse_iterator
void assertSafeToReferenceAfterClear(ItTy From, ItTy To)
Check whether any part of the range will be invalidated by clearing.
const_reverse_iterator rend() const
const_pointer data() const
Return a pointer to the vector's buffer, even if empty().
void * getFirstEl() const
Find the address of the first element.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
SmallVector(std::initializer_list< T > IL)
SmallVector(SmallVectorImpl< T > &&RHS)
SmallVector(ArrayRef< U > A)
SmallVector & operator=(const SmallVector &RHS)
SmallVector(const iterator_range< RangeTy > &R)
SmallVector & operator=(std::initializer_list< T > IL)
SmallVector(ItTy S, ItTy E)
SmallVector(size_t SizeArg)
SmallVector(SmallVector &&RHS)
SmallVector & operator=(SmallVector &&RHS)
SmallVector(size_t SizeArg, const T &Value)
SmallVector(const SmallVector &RHS)
SmallVector & operator=(SmallVectorImpl< T > &&RHS)
LLVM Value Representation.
Definition Value.h:75
A range adaptor for a pair of iterators.
This is an optimization pass for GlobalISel generic memory operations.
std::remove_const_t< detail::ValueOfRange< RangeType > > ValueTypeFromRangeType
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition ADL.h:78
BitVector::size_type capacity_in_bytes(const BitVector &X)
Definition BitVector.h:845
std::enable_if_t< HasIteratorTag< Iterator, std::input_iterator_tag >::value > EnableIfConvertibleToInputIterator
Definition SmallVector.h:46
std::is_convertible< typename std::iterator_traits< Iterator >::iterator_category, Tag > HasIteratorTag
Definition SmallVector.h:42
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition ADL.h:86
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
std::conditional_t< sizeof(T)< 4 &&sizeof(void *) >=8, uint64_t, uint32_t > SmallVectorSizeType
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
SmallVector< Out, Size > to_vector_of(R &&Range)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:285
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
#define N
Helper class for calculating the default number of inline elements for SmallVector<T>.
static bool isEqual(const SmallVector< T, N > &LHS, const SmallVector< T, N > &RHS)
static unsigned getHashValue(const SmallVector< T, N > &V)
An information struct used to provide DenseMap with the various necessary components for a given valu...
Figure out the offset of the first element.
char Base[sizeof(SmallVectorBase< SmallVectorSizeType< T > >)]
Storage for the SmallVector elements.
char InlineElts[N *sizeof(T)]