From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3E3FE3858283; Sun, 19 Nov 2023 17:58:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E3FE3858283 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700416707; bh=UChTX+B+HmIhWftbQ9Msx0cdZf3At8KGkhvAWiEczIo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=DC1E8TkyCDCrvZFLTHt+oMbVqzPGltypkUmW6mY9iSi6ue1p+XSRfkp/t+W28EL/t VV3lA1ufxbblkYxceaaO22b/1uvCtvjJZsrkrM5eAp1riu+V3hB8xxbLOz5hExJQAv rvhiP2Wg+p+5Rrm5p6aRHK/g9S+iwup4J3T4czec= From: "securesneakers at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/110184] [x86] Missed optimisation: atomic operations should use PF, ZF and SF Date: Sun, 19 Nov 2023 17:58:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 13.1.1 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: securesneakers at gmail dot com X-Bugzilla-Status: UNCONFIRMED 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: cc 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=3D110184 Ivan Bodrov changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |securesneakers at gmail do= t com --- Comment #2 from Ivan Bodrov --- This seem to have been implemented, at least for __atomic_fetch_and, but the optimization is very fragile and fails when "lock and" value and mask used during checking come from separate literals: $ cat fragile-fetch-and.c void slowpath(unsigned long *p); void func_bad(unsigned long *p) { if (__atomic_fetch_and(p, ~1UL, __ATOMIC_RELAXED) & ~1UL) slowpath(p); } void func_good(unsigned long *p) { unsigned long mask =3D ~1UL; if (__atomic_fetch_and(p, mask, __ATOMIC_RELAXED) & mask) slowpath(p); } Compiling this we can see that even though functions are the same, the first one wasn't optimized: $ gcc --version gcc (GCC) 13.2.1 20230801 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is= NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURP= OSE. $ uname -s -m Linux x86_64 $ gcc -O2 -c fragile-fetch-and.c=20 $ objdump -d fragile-fetch-and.o fragile-fetch-and.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 : 0: 48 8b 07 mov (%rdi),%rax 3: 48 89 c1 mov %rax,%rcx 6: 48 89 c2 mov %rax,%rdx 9: 48 83 e1 fe and $0xfffffffffffffffe,%rcx d: f0 48 0f b1 0f lock cmpxchg %rcx,(%rdi) 12: 75 ef jne 3 14: 48 83 fa 01 cmp $0x1,%rdx 18: 77 06 ja 20 1a: c3 ret 1b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 20: e9 00 00 00 00 jmp 25 25: 66 66 2e 0f 1f 84 00 data16 cs nopw 0x0(%rax,%rax,1) 2c: 00 00 00 00=20 0000000000000030 : 30: f0 48 83 27 fe lock andq $0xfffffffffffffffe,(%rdi) 35: 75 09 jne 40 37: c3 ret 38: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1) 3f: 00=20 40: e9 00 00 00 00 jmp 45 =