From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C5CF5385828D; Wed, 18 Jan 2023 18:49:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5CF5385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674067782; bh=BM5R5Vcc6wx9i3jj7Osz3kVJ7Nv844VsLyvTDUHQju4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=o1zKLOKKVZAazQDbfa+PiXTkSpHf5ZWfCTXZdApS2EbBPNDeL8e9sQrKJwDO9e5uM PcOQ3qDaXNFIJfke53hPz4QQWpFI7haawpULWa43ZV+K5XG2kALzmfwJ70pMqV0U5J llD4As+pOQ4E26XtUrEGJY48buYbnhfkuM1vnayc= From: "hjl.tools at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/102566] [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic Date: Wed, 18 Jan 2023 18:49:40 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: hjl.tools at gmail dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102566 --- Comment #36 from H.J. Lu --- (1 << (x)) works, but (((unsigned int) 1) << (x)) doesn't work: [hjl@gnu-skx-1 gcc]$ cat bar.c void bar (void); #define MASK1(x) (1 << (x)) void f1 (unsigned int *a, unsigned int bit) { if ((__atomic_fetch_xor (a, MASK1 (bit), __ATOMIC_RELAXED) & MASK1 (bit))) bar (); } #define MASK2(x) (((unsigned int) 1) << (x)) void f2 (unsigned int *a, unsigned int bit) { if ((__atomic_fetch_xor (a, MASK2 (bit), __ATOMIC_RELAXED) & MASK2 (bit))) bar (); } [hjl@gnu-skx-1 gcc]$ ./xgcc -B./ -S -O2 bar.c [hjl@gnu-skx-1 gcc]$ cat bar.s .file "bar.c" .text .p2align 4 .globl f1 .type f1, @function f1: .LFB0: .cfi_startproc lock btcl %esi, (%rdi) jc .L4 ret .p2align 4,,10 .p2align 3 .L4: jmp bar .cfi_endproc .LFE0: .size f1, .-f1 .p2align 4 .globl f2 .type f2, @function f2: .LFB1: .cfi_startproc movl %esi, %ecx movl $1, %edx movl (%rdi), %eax sall %cl, %edx .L6: movl %eax, %r8d movl %eax, %esi xorl %edx, %r8d lock cmpxchgl %r8d, (%rdi) jne .L6 btl %ecx, %esi jc .L10 ret .p2align 4,,10 .p2align 3 .L10: jmp bar .cfi_endproc .LFE1: .size f2, .-f2 .ident "GCC: (GNU) 13.0.1 20230118 (experimental)" .section .note.GNU-stack,"",@progbits [hjl@gnu-skx-1 gcc]$=