https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125907
Bug ID: 125907
Summary: Missed Optimization (x86 bit-test `bt`)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: agatha at geometrian dot com
Target Milestone: ---
Example (with "gcc -O3")
unsigned test( unsigned a, unsigned b, unsigned c )
{
return (a&(1<<b)) ? c : 0;
}
Produces:
"test(unsigned int, unsigned int, unsigned int)":
mov ecx, esi
mov eax, 1
sal eax, cl
and eax, edi
cmovne eax, edx
ret
This is more or less a literal implementation of the above code. However, it
can be optimized significantly with bit-test (`bt`). For example, "clang -O3"
does (MSVC does similarly):
test(unsigned int, unsigned int, unsigned int):
xor eax, eax
bt edi, esi
cmovb eax, edx
ret
The exact GCC version is:
g++
(Compiler-Explorer-Build-gcc-3ddf16f3e94b36346a36dca17e17fdd5388da911-binutils-2.44)
17.0.0 20260619 (experimental)