https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105875
Bug ID: 105875
Summary: Toggling an atomic_bool is inefficient
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: josephcsible at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
Consider this C code:
#include <stdatomic.h>
atomic_bool b;
atomic_char c;
_Bool b2;
void f1(void) {
b ^= 1;
}
void f2(void) {
c ^= 1;
}
void f3(void) {
b2 ^= 1;
}
At -O3, those functions compile into this:
f1:
movzbl b(%rip), %eax
.L5:
movb %al, -1(%rsp)
xorl $1, %eax
movl %eax, %edx
movzbl -1(%rsp), %eax
lock cmpxchgb %dl, b(%rip)
jne .L5
ret
f2:
lock xorb $1, c(%rip)
ret
f3:
xorb $1, b2(%rip)
ret
The code generated for f1 is inefficient. It should have just done a "lock xorb
$1, b(%rip)".