<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87010>87010</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang fails to eliminate redundant bounds check from std::vector::push_back
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
efriedma-quic
</td>
</tr>
</table>
<pre>
Consider the following:
```
#include <vector>
void f1(std::vector<int>&x) { x.push_back(1);}
```
Compile with `-O2 -stdlib=libc++`, and looks at the resulting assembly. (https://godbolt.org/z/d3GzvEGY4)
There are two bounds checks on the size of the vector: the first in the implementation of std::vector itself, which checks that the length of the vector isn't too large, and calls __throw_length_error if it fails. Then, there's a check inside the allocator that the allocation isn't too large, and calls __throw_bad_array_new_length if it fails. But the second check shouldn't be necessary: if the vector isn't too large, the allocation also isn't too large, since "too large" is defined exactly the same way for both checks.
(Briefly discussed in #64132, but I'm breaking it out into its own issue for clarity.)
CC @fhahn @hiraditya @philnik777
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVM1u4zgTfBr60oghkYolH3SInfHgO32XuezJ4E_L7DVFekkqjvP0C0rx7MQYYBcQZJLuZlWXqlumRCeP2LPnHXt-Xckp2xB7HCKhGeXTXxPplQrm1u-DT2QwQrYIQ3AuXMmfmHhh1Sur7u9N9fksWy7IazcZBCb2b6hziEx8W_58C2RgqBnvUjblHvFyj9iTzyWOb94Z3wJrd_C-vkzJHpXUZ8a7mvEtEzvWvv4ed37vw3ghh3ClbIFtqqf_c3hK2ThSTLw6UprxXXk2FeN7kN6AC-GcQOa5yIhpcpn8CWRKOCp3WwMw3tmcL6kQ5gfGD6dgVHB5HeKJ8cMH4wcjvn-8ffv-R1NI_sLnh8WIICNCvgZQYfImgbaozwmCnyETfSCEYV7fxXhZFKeYMtASRuPF4Yg-y0zBl4QHCYFyQjeUsq6WtL3DZPtZm0N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