xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/SmallVector.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the SmallVector class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
14349cc55cSDimitry Andric #include "llvm/ADT/Twine.h"
1504eeddc0SDimitry Andric #include "llvm/Support/MemAlloc.h"
165ffd83dbSDimitry Andric #include <cstdint>
17e8d8bef9SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
18e8d8bef9SDimitry Andric #include <stdexcept>
19e8d8bef9SDimitry Andric #endif
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric // Check that no bytes are wasted and everything is well-aligned.
230b57cec5SDimitry Andric namespace {
24349cc55cSDimitry Andric // These structures may cause binary compat warnings on AIX. Suppress the
25349cc55cSDimitry Andric // warning since we are only using these types for the static assertions below.
26349cc55cSDimitry Andric #if defined(_AIX)
27349cc55cSDimitry Andric #pragma GCC diagnostic push
28349cc55cSDimitry Andric #pragma GCC diagnostic ignored "-Waix-compat"
29349cc55cSDimitry Andric #endif
300b57cec5SDimitry Andric struct Struct16B {
310b57cec5SDimitry Andric   alignas(16) void *X;
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric struct Struct32B {
340b57cec5SDimitry Andric   alignas(32) void *X;
350b57cec5SDimitry Andric };
36349cc55cSDimitry Andric #if defined(_AIX)
37349cc55cSDimitry Andric #pragma GCC diagnostic pop
38349cc55cSDimitry Andric #endif
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric static_assert(sizeof(SmallVector<void *, 0>) ==
410b57cec5SDimitry Andric                   sizeof(unsigned) * 2 + sizeof(void *),
420b57cec5SDimitry Andric               "wasted space in SmallVector size 0");
430b57cec5SDimitry Andric static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
440b57cec5SDimitry Andric               "wrong alignment for 16-byte aligned T");
450b57cec5SDimitry Andric static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
460b57cec5SDimitry Andric               "wrong alignment for 32-byte aligned T");
470b57cec5SDimitry Andric static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
480b57cec5SDimitry Andric               "missing padding for 16-byte aligned T");
490b57cec5SDimitry Andric static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
500b57cec5SDimitry Andric               "missing padding for 32-byte aligned T");
510b57cec5SDimitry Andric static_assert(sizeof(SmallVector<void *, 1>) ==
520b57cec5SDimitry Andric                   sizeof(unsigned) * 2 + sizeof(void *) * 2,
530b57cec5SDimitry Andric               "wasted space in SmallVector size 1");
540b57cec5SDimitry Andric 
555ffd83dbSDimitry Andric static_assert(sizeof(SmallVector<char, 0>) ==
565ffd83dbSDimitry Andric                   sizeof(void *) * 2 + sizeof(void *),
575ffd83dbSDimitry Andric               "1 byte elements have word-sized type for size and capacity");
585ffd83dbSDimitry Andric 
59e8d8bef9SDimitry Andric /// Report that MinSize doesn't fit into this vector's size type. Throws
60e8d8bef9SDimitry Andric /// std::length_error or calls report_fatal_error.
61349cc55cSDimitry Andric [[noreturn]] static void report_size_overflow(size_t MinSize, size_t MaxSize);
report_size_overflow(size_t MinSize,size_t MaxSize)62e8d8bef9SDimitry Andric static void report_size_overflow(size_t MinSize, size_t MaxSize) {
63e8d8bef9SDimitry Andric   std::string Reason = "SmallVector unable to grow. Requested capacity (" +
64e8d8bef9SDimitry Andric                        std::to_string(MinSize) +
65e8d8bef9SDimitry Andric                        ") is larger than maximum value for size type (" +
66e8d8bef9SDimitry Andric                        std::to_string(MaxSize) + ")";
67e8d8bef9SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
68e8d8bef9SDimitry Andric   throw std::length_error(Reason);
69e8d8bef9SDimitry Andric #else
70349cc55cSDimitry Andric   report_fatal_error(Twine(Reason));
71e8d8bef9SDimitry Andric #endif
72e8d8bef9SDimitry Andric }
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric /// Report that this vector is already at maximum capacity. Throws
75e8d8bef9SDimitry Andric /// std::length_error or calls report_fatal_error.
76349cc55cSDimitry Andric [[noreturn]] static void report_at_maximum_capacity(size_t MaxSize);
report_at_maximum_capacity(size_t MaxSize)77e8d8bef9SDimitry Andric static void report_at_maximum_capacity(size_t MaxSize) {
78e8d8bef9SDimitry Andric   std::string Reason =
79e8d8bef9SDimitry Andric       "SmallVector capacity unable to grow. Already at maximum size " +
80e8d8bef9SDimitry Andric       std::to_string(MaxSize);
81e8d8bef9SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
82e8d8bef9SDimitry Andric   throw std::length_error(Reason);
83e8d8bef9SDimitry Andric #else
84349cc55cSDimitry Andric   report_fatal_error(Twine(Reason));
85e8d8bef9SDimitry Andric #endif
86e8d8bef9SDimitry Andric }
87e8d8bef9SDimitry Andric 
885ffd83dbSDimitry Andric // Note: Moving this function into the header may cause performance regression.
895ffd83dbSDimitry Andric template <class Size_T>
getNewCapacity(size_t MinSize,size_t TSize,size_t OldCapacity)90e8d8bef9SDimitry Andric static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
91e8d8bef9SDimitry Andric   constexpr size_t MaxSize = std::numeric_limits<Size_T>::max();
92e8d8bef9SDimitry Andric 
935ffd83dbSDimitry Andric   // Ensure we can fit the new capacity.
945ffd83dbSDimitry Andric   // This is only going to be applicable when the capacity is 32 bit.
95e8d8bef9SDimitry Andric   if (MinSize > MaxSize)
96e8d8bef9SDimitry Andric     report_size_overflow(MinSize, MaxSize);
970b57cec5SDimitry Andric 
985ffd83dbSDimitry Andric   // Ensure we can meet the guarantee of space for at least one more element.
995ffd83dbSDimitry Andric   // The above check alone will not catch the case where grow is called with a
100e8d8bef9SDimitry Andric   // default MinSize of 0, but the current capacity cannot be increased.
1015ffd83dbSDimitry Andric   // This is only going to be applicable when the capacity is 32 bit.
102e8d8bef9SDimitry Andric   if (OldCapacity == MaxSize)
103e8d8bef9SDimitry Andric     report_at_maximum_capacity(MaxSize);
1045ffd83dbSDimitry Andric 
1055ffd83dbSDimitry Andric   // In theory 2*capacity can overflow if the capacity is 64 bit, but the
1065ffd83dbSDimitry Andric   // original capacity would never be large enough for this to be a problem.
107e8d8bef9SDimitry Andric   size_t NewCapacity = 2 * OldCapacity + 1; // Always grow.
108bdd1243dSDimitry Andric   return std::clamp(NewCapacity, MinSize, MaxSize);
109bdd1243dSDimitry Andric }
110bdd1243dSDimitry Andric 
111bdd1243dSDimitry Andric template <class Size_T>
replaceAllocation(void * NewElts,size_t TSize,size_t NewCapacity,size_t VSize)112bdd1243dSDimitry Andric void *SmallVectorBase<Size_T>::replaceAllocation(void *NewElts, size_t TSize,
113bdd1243dSDimitry Andric                                                  size_t NewCapacity,
114bdd1243dSDimitry Andric                                                  size_t VSize) {
115bdd1243dSDimitry Andric   void *NewEltsReplace = llvm::safe_malloc(NewCapacity * TSize);
116bdd1243dSDimitry Andric   if (VSize)
117bdd1243dSDimitry Andric     memcpy(NewEltsReplace, NewElts, VSize * TSize);
118bdd1243dSDimitry Andric   free(NewElts);
119bdd1243dSDimitry Andric   return NewEltsReplace;
120e8d8bef9SDimitry Andric }
1210b57cec5SDimitry Andric 
122e8d8bef9SDimitry Andric // Note: Moving this function into the header may cause performance regression.
123e8d8bef9SDimitry Andric template <class Size_T>
mallocForGrow(void * FirstEl,size_t MinSize,size_t TSize,size_t & NewCapacity)124bdd1243dSDimitry Andric void *SmallVectorBase<Size_T>::mallocForGrow(void *FirstEl, size_t MinSize,
125bdd1243dSDimitry Andric                                              size_t TSize,
126e8d8bef9SDimitry Andric                                              size_t &NewCapacity) {
127e8d8bef9SDimitry Andric   NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
128bdd1243dSDimitry Andric   // Even if capacity is not 0 now, if the vector was originally created with
129bdd1243dSDimitry Andric   // capacity 0, it's possible for the malloc to return FirstEl.
130bdd1243dSDimitry Andric   void *NewElts = llvm::safe_malloc(NewCapacity * TSize);
131bdd1243dSDimitry Andric   if (NewElts == FirstEl)
132bdd1243dSDimitry Andric     NewElts = replaceAllocation(NewElts, TSize, NewCapacity);
133bdd1243dSDimitry Andric   return NewElts;
134e8d8bef9SDimitry Andric }
135e8d8bef9SDimitry Andric 
136e8d8bef9SDimitry Andric // Note: Moving this function into the header may cause performance regression.
137e8d8bef9SDimitry Andric template <class Size_T>
grow_pod(void * FirstEl,size_t MinSize,size_t TSize)138e8d8bef9SDimitry Andric void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize,
139e8d8bef9SDimitry Andric                                        size_t TSize) {
140e8d8bef9SDimitry Andric   size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
1410b57cec5SDimitry Andric   void *NewElts;
1420b57cec5SDimitry Andric   if (BeginX == FirstEl) {
143bdd1243dSDimitry Andric     NewElts = llvm::safe_malloc(NewCapacity * TSize);
144bdd1243dSDimitry Andric     if (NewElts == FirstEl)
145bdd1243dSDimitry Andric       NewElts = replaceAllocation(NewElts, TSize, NewCapacity);
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric     // Copy the elements over.  No need to run dtors on PODs.
1480b57cec5SDimitry Andric     memcpy(NewElts, this->BeginX, size() * TSize);
1490b57cec5SDimitry Andric   } else {
1500b57cec5SDimitry Andric     // If this wasn't grown from the inline copy, grow the allocated space.
151bdd1243dSDimitry Andric     NewElts = llvm::safe_realloc(this->BeginX, NewCapacity * TSize);
152bdd1243dSDimitry Andric     if (NewElts == FirstEl)
153bdd1243dSDimitry Andric       NewElts = replaceAllocation(NewElts, TSize, NewCapacity, size());
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric 
156*7a6dacacSDimitry Andric   this->set_allocation_range(NewElts, NewCapacity);
1570b57cec5SDimitry Andric }
1585ffd83dbSDimitry Andric 
1595ffd83dbSDimitry Andric template class llvm::SmallVectorBase<uint32_t>;
1605ffd83dbSDimitry Andric 
1615ffd83dbSDimitry Andric // Disable the uint64_t instantiation for 32-bit builds.
162e8d8bef9SDimitry Andric // Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
1635ffd83dbSDimitry Andric // This instantiation will never be used in 32-bit builds, and will cause
1645ffd83dbSDimitry Andric // warnings when sizeof(Size_T) > sizeof(size_t).
1655ffd83dbSDimitry Andric #if SIZE_MAX > UINT32_MAX
1665ffd83dbSDimitry Andric template class llvm::SmallVectorBase<uint64_t>;
1675ffd83dbSDimitry Andric 
1685ffd83dbSDimitry Andric // Assertions to ensure this #if stays in sync with SmallVectorSizeType.
1695ffd83dbSDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t),
1705ffd83dbSDimitry Andric               "Expected SmallVectorBase<uint64_t> variant to be in use.");
1715ffd83dbSDimitry Andric #else
1725ffd83dbSDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t),
1735ffd83dbSDimitry Andric               "Expected SmallVectorBase<uint32_t> variant to be in use.");
1745ffd83dbSDimitry Andric #endif
175