<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64395>64395</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Dead bounds check code leftover on a dead vector copy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hiraditya
</td>
</tr>
</table>
<pre>
This is a follow up from: https://github.com/llvm/llvm-project/issues/61987
Where a dead vector copy was optimized away from the following code:
```cpp
#include<vector>
using T = int;
T vec_copy(const std::vector<T> &v1) {
auto v(v1);
return 10;
}
```
There is still leftover dead code. e.g., bonds check and unnecessary throw statements. I think removing the bounds check will result in DCE of throw statement anyways.
Reference: https://godbolt.org/z/xrdTdMsMr
$ clang++ -O3 -stdlib=libc++ -fno-exceptions -std=c++20
```asm
vec_copy(std::__1::vector<int, std::__1::allocator<int> > const&): # @vec_copy(std::__1::vector<int, std::__1::allocator<int> > const&)
sub rsp, 24
xorps xmm0, xmm0
movaps xmmword ptr [rsp], xmm0
mov qword ptr [rsp + 16], 0
mov rax, qword ptr [rdi + 8]
sub rax, qword ptr [rdi] <------------------------------------- dead loads and check
je .LBB0_2
js .LBB0_3
.LBB0_2:
mov eax, 10
add rsp, 24
ret
.LBB0_3:
mov rdi, rsp
call std::__1::vector<int, std::__1::allocator<int> >::__throw_length_error[abi:v180000]() const
std::__1::vector<int, std::__1::allocator<int> >::__throw_length_error[abi:v180000]() const: # @std::__1::vector<int, std::__1::allocator<int> >::__throw_length_error[abi:v180000]() const
push rax
lea rdi, [rip + .L.str]
call std::__1::__throw_length_error[abi:v180000](char const*)
std::__1::__throw_length_error[abi:v180000](char const*): # @std::__1::__throw_length_error[abi:v180000](char const*)
push rax
mov rsi, rdi
lea rdi, [rip + .L.str.1]
xor eax, eax
call std::__1::__libcpp_verbose_abort(char const*, ...)@PLT
.L.str:
.asciz "vector"
.L.str.1:
.asciz "length_error was thrown in -fno-exceptions mode with message \"%s\""
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVlmP4ygQ_jXkpdSWDTkf8pCjI63Uo12tIu1jhKESM4ONF3CO_vUrcE5Pena0syNN1DIt6uCj6quiuHNqVyFOyWBOBsseb3xh7LRQlkvlT7yXG3margvlQDngsDVamwM0NWytKQmbQeF97QibEboidLVTvmjyRJiS0JXW-8vyUlvzGYUndKWca9ARuhpmk_GIpEuSzv4q0CJwkMgl7FF4Y0GY-gQH7sDUXpXqHSXwAz_Fg8EXeMaiqh0IIzFAiL7O32Ha_om6Pu9Qpiqhm6C6aM8g7PXepnHB2RoIW4KqPGHzdn8dIG0CHkLHwlTOg_MyHMhmF0eLNWGvQOhwnxE6ATI62wIA8MYb2BM6jrKr2yCy6BtbQZZed8lo2bnBPcR1DJRy4LzSGjRuvdmjbQMXwpAAJruE0AXkppIORIHiC_BKQlNVKNA5bk_gC2sO4Dz3WGLlXQK_gS9U9QUslmYfwhAinJvm5uMQDrToGu1BVbBcvILZdj0Br04HfnLJQy7i90_cosVK4BPWGJkb7RNjd4Su3gldHa1cy0_uk33wQ_sgNK92hM4JncPL7wxenJda5YQttcrFRbCtzAseBdZemcpFJcKWZzFNnxKFu7LduUv2Nc2bTdbJdyAIXcDXGlxrI_hNKdCCvULkDaHDyIAZEMqA9NOff9aVauHnmjyu1tXBIe2fxUdjawcAx7JMgyCuD5al2fOgcizLg7ESam-BDObB0WD5oQn83VGGkJ5seLZ5YhDR8WOQPtpKFW3HwbS1ul7muToZLAEIW7x8z68tIG24dLFYIuUf0X3Gdk3e5vN0QztCd95n7f5F6dKUulfEFnPWiQCX8ml-LPp7v-xDv-HedBHtH-SCa_2EP_-VYRelWP0bjdXOFxu01lgymPNcBdfZOE3TNGZ6HFpiS8qI6lcAcqvBXwHNQ7LqxhUXYrcCjfw-vYHgqq2l5C1x3t6K4tv5_n5oouD20kdm1z7yv3r8VgZ-FOhDKG9x7FaLa6tFqkf5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