public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105875] New: Toggling an atomic_bool is inefficient
@ 2022-06-07 15:10 josephcsible at gmail dot com
  2022-06-21  6:35 ` [Bug tree-optimization/105875] " pinskia at gcc dot gnu.org
  2023-05-17 23:32 ` [Bug c/105875] " pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: josephcsible at gmail dot com @ 2022-06-07 15:10 UTC (permalink / raw)
  To: gcc-bugs

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)".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/105875] Toggling an atomic_bool is inefficient
  2022-06-07 15:10 [Bug c/105875] New: Toggling an atomic_bool is inefficient josephcsible at gmail dot com
@ 2022-06-21  6:35 ` pinskia at gcc dot gnu.org
  2023-05-17 23:32 ` [Bug c/105875] " pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-21  6:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang/LLVM produces the same ....

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/105875] Toggling an atomic_bool is inefficient
  2022-06-07 15:10 [Bug c/105875] New: Toggling an atomic_bool is inefficient josephcsible at gmail dot com
  2022-06-21  6:35 ` [Bug tree-optimization/105875] " pinskia at gcc dot gnu.org
@ 2023-05-17 23:32 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-17 23:32 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|tree-optimization           |c
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-05-17

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It is the front-end that is producing the worse code:
  TARGET_EXPR <D.2818, 1>;
  TARGET_EXPR <D.2819, (void) (D.2819 =
VIEW_CONVERT_EXPR<atomic_bool>(__atomic_load_1 ((const volatile void *) &b,
5)))>;
  <D.2821>:;
  TARGET_EXPR <D.2820, ((int) TARGET_EXPR <D.2819, (void) (D.2819 =
VIEW_CONVERT_EXPR<atomic_bool>(__atomic_load_1 ((const volatile void *) &b,
5)))> ^ D.2818) != 0>;
  if (__atomic_compare_exchange_1 ((volatile void *) &b, (void *) &D.2819,
(int) VIEW_CONVERT_EXPR<unsigned char>(D.2820), 0, 5, 5))
    {
      goto <D.2822>;
    }
  goto <D.2821>;
  <D.2822>:;, D.2820;

vs:
  TARGET_EXPR <D.2827, (char) __atomic_xor_fetch_1 ((volatile void *) &c, (int)
(unsigned char) TARGET_EXPR <D.2826, 1>, 5)>;, D.2827;


So confirmed.

Using __atomic_xor_fetch_1 directly works.
That is:
  __atomic_xor_fetch_1 (&b, 1, 5);

Produces:
        lock xorb       $1, b(%rip)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-05-17 23:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-07 15:10 [Bug c/105875] New: Toggling an atomic_bool is inefficient josephcsible at gmail dot com
2022-06-21  6:35 ` [Bug tree-optimization/105875] " pinskia at gcc dot gnu.org
2023-05-17 23:32 ` [Bug c/105875] " pinskia at gcc dot gnu.org

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).