public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "securesneakers at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-110184-4-9qXIHx4P7E@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110184-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110184

Ivan Bodrov <securesneakers at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |securesneakers at gmail dot com

--- Comment #2 from Ivan Bodrov <securesneakers at gmail dot com> ---
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 = ~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 PURPOSE.

    $ uname -s -m
    Linux x86_64
    $ gcc -O2 -c fragile-fetch-and.c 
    $ objdump -d fragile-fetch-and.o

    fragile-fetch-and.o:     file format elf64-x86-64


    Disassembly of section .text:

    0000000000000000 <func_bad>:
       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 <func_bad+0x3>
      14:   48 83 fa 01             cmp    $0x1,%rdx
      18:   77 06                   ja     20 <func_bad+0x20>
      1a:   c3                      ret
      1b:   0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
      20:   e9 00 00 00 00          jmp    25 <func_bad+0x25>
      25:   66 66 2e 0f 1f 84 00    data16 cs nopw 0x0(%rax,%rax,1)
      2c:   00 00 00 00 

    0000000000000030 <func_good>:
      30:   f0 48 83 27 fe          lock andq $0xfffffffffffffffe,(%rdi)
      35:   75 09                   jne    40 <func_good+0x10>
      37:   c3                      ret
      38:   0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
      3f:   00 
      40:   e9 00 00 00 00          jmp    45 <func_good+0x15>

  parent reply	other threads:[~2023-11-19 17:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 22:55 [Bug target/110184] New: [i386] " thiago at kde dot org
2023-06-08 23:12 ` [Bug target/110184] " pinskia at gcc dot gnu.org
2023-06-08 23:16 ` [Bug target/110184] [x86] " pinskia at gcc dot gnu.org
2023-11-19 17:58 ` securesneakers at gmail dot com [this message]
2023-11-19 17:59 ` securesneakers at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110184-4-9qXIHx4P7E@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).