https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127285
Bug ID: 127285
Summary: GCC generates redundant `test + sete`
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: saicharan at astrome dot co
Target Milestone: ---
GCC performs redundant `test + sete` after moving a constant in the register
when compiled with -O3
Environment: Linux
Version: 16.2
## Minimal reproducible example
```cpp
#include <string_view>
bool test(const char (&a)[4], const char (&b)[4]) {
return std::string_view(a, 3) == std::string_view(b, 3);
}
```
Generated assembly
```
"test(char const (&) [4], char const (&) [4])":
movzx eax, WORD PTR [rsi]
cmp WORD PTR [rdi], ax
je .L5
.L2:
mov eax, 1
test eax, eax
sete al
ret
.L5:
movzx eax, BYTE PTR [rsi+2]
cmp BYTE PTR [rdi+2], al
jne .L2
xor eax, eax
test eax, eax
sete al
ret
```
Expected assembly
```
"test(char const (&) [4], char const (&) [4])":
movzx eax, WORD PTR [rsi]
cmp WORD PTR [rdi], ax
je .L5
.L2:
xor eax, eax
ret
.L5:
movzx eax, BYTE PTR [rsi+2]
cmp BYTE PTR [rdi+2], al
jne .L2
mov eax, 1
ret
```
Compiler explorer link: https://godbolt.org/z/98s8Tfcz5