11e7804dbSRoman Divacky //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
21e7804dbSRoman Divacky //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61e7804dbSRoman Divacky //
71e7804dbSRoman Divacky //===----------------------------------------------------------------------===//
81e7804dbSRoman Divacky //
91e7804dbSRoman Divacky // This file implements the SmallVector class.
101e7804dbSRoman Divacky //
111e7804dbSRoman Divacky //===----------------------------------------------------------------------===//
121e7804dbSRoman Divacky
131e7804dbSRoman Divacky #include "llvm/ADT/SmallVector.h"
14c0981da4SDimitry Andric #include "llvm/ADT/Twine.h"
156f8fc217SDimitry Andric #include "llvm/Support/MemAlloc.h"
16cfca06d7SDimitry Andric #include <cstdint>
17b60736ecSDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
18b60736ecSDimitry Andric #include <stdexcept>
19b60736ecSDimitry Andric #endif
201e7804dbSRoman Divacky using namespace llvm;
211e7804dbSRoman Divacky
22eb11fae6SDimitry Andric // Check that no bytes are wasted and everything is well-aligned.
23eb11fae6SDimitry Andric namespace {
24c0981da4SDimitry Andric // These structures may cause binary compat warnings on AIX. Suppress the
25c0981da4SDimitry Andric // warning since we are only using these types for the static assertions below.
26c0981da4SDimitry Andric #if defined(_AIX)
27c0981da4SDimitry Andric #pragma GCC diagnostic push
28c0981da4SDimitry Andric #pragma GCC diagnostic ignored "-Waix-compat"
29c0981da4SDimitry Andric #endif
30eb11fae6SDimitry Andric struct Struct16B {
31eb11fae6SDimitry Andric alignas(16) void *X;
32eb11fae6SDimitry Andric };
33eb11fae6SDimitry Andric struct Struct32B {
34eb11fae6SDimitry Andric alignas(32) void *X;
35eb11fae6SDimitry Andric };
36c0981da4SDimitry Andric #if defined(_AIX)
37c0981da4SDimitry Andric #pragma GCC diagnostic pop
38c0981da4SDimitry Andric #endif
39eb11fae6SDimitry Andric }
40eb11fae6SDimitry Andric static_assert(sizeof(SmallVector<void *, 0>) ==
41eb11fae6SDimitry Andric sizeof(unsigned) * 2 + sizeof(void *),
42eb11fae6SDimitry Andric "wasted space in SmallVector size 0");
43eb11fae6SDimitry Andric static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
44eb11fae6SDimitry Andric "wrong alignment for 16-byte aligned T");
45eb11fae6SDimitry Andric static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
46eb11fae6SDimitry Andric "wrong alignment for 32-byte aligned T");
47eb11fae6SDimitry Andric static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
48eb11fae6SDimitry Andric "missing padding for 16-byte aligned T");
49eb11fae6SDimitry Andric static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
50eb11fae6SDimitry Andric "missing padding for 32-byte aligned T");
51eb11fae6SDimitry Andric static_assert(sizeof(SmallVector<void *, 1>) ==
52eb11fae6SDimitry Andric sizeof(unsigned) * 2 + sizeof(void *) * 2,
53eb11fae6SDimitry Andric "wasted space in SmallVector size 1");
54eb11fae6SDimitry Andric
55cfca06d7SDimitry Andric static_assert(sizeof(SmallVector<char, 0>) ==
56cfca06d7SDimitry Andric sizeof(void *) * 2 + sizeof(void *),
57cfca06d7SDimitry Andric "1 byte elements have word-sized type for size and capacity");
58cfca06d7SDimitry Andric
59b60736ecSDimitry Andric /// Report that MinSize doesn't fit into this vector's size type. Throws
60b60736ecSDimitry Andric /// std::length_error or calls report_fatal_error.
61c0981da4SDimitry Andric [[noreturn]] static void report_size_overflow(size_t MinSize, size_t MaxSize);
report_size_overflow(size_t MinSize,size_t MaxSize)62b60736ecSDimitry Andric static void report_size_overflow(size_t MinSize, size_t MaxSize) {
63b60736ecSDimitry Andric std::string Reason = "SmallVector unable to grow. Requested capacity (" +
64b60736ecSDimitry Andric std::to_string(MinSize) +
65b60736ecSDimitry Andric ") is larger than maximum value for size type (" +
66b60736ecSDimitry Andric std::to_string(MaxSize) + ")";
67b60736ecSDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
68b60736ecSDimitry Andric throw std::length_error(Reason);
69b60736ecSDimitry Andric #else
70c0981da4SDimitry Andric report_fatal_error(Twine(Reason));
71b60736ecSDimitry Andric #endif
72b60736ecSDimitry Andric }
73b60736ecSDimitry Andric
74b60736ecSDimitry Andric /// Report that this vector is already at maximum capacity. Throws
75b60736ecSDimitry Andric /// std::length_error or calls report_fatal_error.
76c0981da4SDimitry Andric [[noreturn]] static void report_at_maximum_capacity(size_t MaxSize);
report_at_maximum_capacity(size_t MaxSize)77b60736ecSDimitry Andric static void report_at_maximum_capacity(size_t MaxSize) {
78b60736ecSDimitry Andric std::string Reason =
79b60736ecSDimitry Andric "SmallVector capacity unable to grow. Already at maximum size " +
80b60736ecSDimitry Andric std::to_string(MaxSize);
81b60736ecSDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
82b60736ecSDimitry Andric throw std::length_error(Reason);
83b60736ecSDimitry Andric #else
84c0981da4SDimitry Andric report_fatal_error(Twine(Reason));
85b60736ecSDimitry Andric #endif
86b60736ecSDimitry Andric }
87b60736ecSDimitry Andric
88cfca06d7SDimitry Andric // Note: Moving this function into the header may cause performance regression.
89cfca06d7SDimitry Andric template <class Size_T>
getNewCapacity(size_t MinSize,size_t TSize,size_t OldCapacity)90b60736ecSDimitry Andric static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
91b60736ecSDimitry Andric constexpr size_t MaxSize = std::numeric_limits<Size_T>::max();
92b60736ecSDimitry Andric
93cfca06d7SDimitry Andric // Ensure we can fit the new capacity.
94cfca06d7SDimitry Andric // This is only going to be applicable when the capacity is 32 bit.
95b60736ecSDimitry Andric if (MinSize > MaxSize)
96b60736ecSDimitry Andric report_size_overflow(MinSize, MaxSize);
97eb11fae6SDimitry Andric
98cfca06d7SDimitry Andric // Ensure we can meet the guarantee of space for at least one more element.
99cfca06d7SDimitry Andric // The above check alone will not catch the case where grow is called with a
100b60736ecSDimitry Andric // default MinSize of 0, but the current capacity cannot be increased.
101cfca06d7SDimitry Andric // This is only going to be applicable when the capacity is 32 bit.
102b60736ecSDimitry Andric if (OldCapacity == MaxSize)
103b60736ecSDimitry Andric report_at_maximum_capacity(MaxSize);
104cfca06d7SDimitry Andric
105cfca06d7SDimitry Andric // In theory 2*capacity can overflow if the capacity is 64 bit, but the
106cfca06d7SDimitry Andric // original capacity would never be large enough for this to be a problem.
107b60736ecSDimitry Andric size_t NewCapacity = 2 * OldCapacity + 1; // Always grow.
108e3b55780SDimitry Andric return std::clamp(NewCapacity, MinSize, MaxSize);
109e3b55780SDimitry Andric }
110e3b55780SDimitry Andric
111e3b55780SDimitry Andric template <class Size_T>
replaceAllocation(void * NewElts,size_t TSize,size_t NewCapacity,size_t VSize)112e3b55780SDimitry Andric void *SmallVectorBase<Size_T>::replaceAllocation(void *NewElts, size_t TSize,
113e3b55780SDimitry Andric size_t NewCapacity,
114e3b55780SDimitry Andric size_t VSize) {
115e3b55780SDimitry Andric void *NewEltsReplace = llvm::safe_malloc(NewCapacity * TSize);
116e3b55780SDimitry Andric if (VSize)
117e3b55780SDimitry Andric memcpy(NewEltsReplace, NewElts, VSize * TSize);
118e3b55780SDimitry Andric free(NewElts);
119e3b55780SDimitry Andric return NewEltsReplace;
120b60736ecSDimitry Andric }
12166e41e3cSRoman Divacky
122b60736ecSDimitry Andric // Note: Moving this function into the header may cause performance regression.
123b60736ecSDimitry Andric template <class Size_T>
mallocForGrow(void * FirstEl,size_t MinSize,size_t TSize,size_t & NewCapacity)124e3b55780SDimitry Andric void *SmallVectorBase<Size_T>::mallocForGrow(void *FirstEl, size_t MinSize,
125e3b55780SDimitry Andric size_t TSize,
126b60736ecSDimitry Andric size_t &NewCapacity) {
127b60736ecSDimitry Andric NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
128e3b55780SDimitry Andric // Even if capacity is not 0 now, if the vector was originally created with
129e3b55780SDimitry Andric // capacity 0, it's possible for the malloc to return FirstEl.
130e3b55780SDimitry Andric void *NewElts = llvm::safe_malloc(NewCapacity * TSize);
131e3b55780SDimitry Andric if (NewElts == FirstEl)
132e3b55780SDimitry Andric NewElts = replaceAllocation(NewElts, TSize, NewCapacity);
133e3b55780SDimitry Andric return NewElts;
134b60736ecSDimitry Andric }
135b60736ecSDimitry Andric
136b60736ecSDimitry Andric // Note: Moving this function into the header may cause performance regression.
137b60736ecSDimitry Andric template <class Size_T>
grow_pod(void * FirstEl,size_t MinSize,size_t TSize)138b60736ecSDimitry Andric void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize,
139b60736ecSDimitry Andric size_t TSize) {
140b60736ecSDimitry Andric size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
14166e41e3cSRoman Divacky void *NewElts;
142522600a2SDimitry Andric if (BeginX == FirstEl) {
143e3b55780SDimitry Andric NewElts = llvm::safe_malloc(NewCapacity * TSize);
144e3b55780SDimitry Andric if (NewElts == FirstEl)
145e3b55780SDimitry Andric NewElts = replaceAllocation(NewElts, TSize, NewCapacity);
1461e7804dbSRoman Divacky
1471e7804dbSRoman Divacky // Copy the elements over. No need to run dtors on PODs.
148eb11fae6SDimitry Andric memcpy(NewElts, this->BeginX, size() * TSize);
14966e41e3cSRoman Divacky } else {
15066e41e3cSRoman Divacky // If this wasn't grown from the inline copy, grow the allocated space.
151e3b55780SDimitry Andric NewElts = llvm::safe_realloc(this->BeginX, NewCapacity * TSize);
152e3b55780SDimitry Andric if (NewElts == FirstEl)
153e3b55780SDimitry Andric NewElts = replaceAllocation(NewElts, TSize, NewCapacity, size());
15466e41e3cSRoman Divacky }
1551e7804dbSRoman Divacky
1564df029ccSDimitry Andric this->set_allocation_range(NewElts, NewCapacity);
1571e7804dbSRoman Divacky }
158cfca06d7SDimitry Andric
159cfca06d7SDimitry Andric template class llvm::SmallVectorBase<uint32_t>;
160cfca06d7SDimitry Andric
161cfca06d7SDimitry Andric // Disable the uint64_t instantiation for 32-bit builds.
162b60736ecSDimitry Andric // Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
163cfca06d7SDimitry Andric // This instantiation will never be used in 32-bit builds, and will cause
164cfca06d7SDimitry Andric // warnings when sizeof(Size_T) > sizeof(size_t).
165cfca06d7SDimitry Andric #if SIZE_MAX > UINT32_MAX
166cfca06d7SDimitry Andric template class llvm::SmallVectorBase<uint64_t>;
167cfca06d7SDimitry Andric
168cfca06d7SDimitry Andric // Assertions to ensure this #if stays in sync with SmallVectorSizeType.
169cfca06d7SDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t),
170cfca06d7SDimitry Andric "Expected SmallVectorBase<uint64_t> variant to be in use.");
171cfca06d7SDimitry Andric #else
172cfca06d7SDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t),
173cfca06d7SDimitry Andric "Expected SmallVectorBase<uint32_t> variant to be in use.");
174cfca06d7SDimitry Andric #endif
175