ADT: Add SmallVectorImpl::truncate() to replace uses of set_size()
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Wed, 8 Dec 2021 00:20:18 +0000 (16:20 -0800)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Wed, 8 Dec 2021 21:57:48 +0000 (13:57 -0800)
commitca451d3fa40ebbd35c702ba698e9e5f29e8ed69b
treed51b6a792bb057aef2a3ff2f953636de08ea559c
parent1225c8a06103e029a4b2ebb782a175ab1e011cbe
ADT: Add SmallVectorImpl::truncate() to replace uses of set_size()

Add `SmallVectorImpl::truncate()`, a variant of `resize()` that cannot
increase the size.

- Compared to `resize()`, this has no code path for growing the
  allocation and can be better optimized.
- Compared to `set_size()`, this formally calls destructors, and does
  not skip any constructors.
- Compared to `pop_back_n()`, this takes the new desired size, which in
  many contexts is more intuitive than the number of elements to remove.

The immediate motivation is to pair this with `resize_for_overwrite()`
to remove uses of `set_size()`, which can then be made private.

Differential Revision: https://reviews.llvm.org/D115383
llvm/include/llvm/ADT/SmallVector.h
llvm/unittests/ADT/SmallVectorTest.cpp