public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "barry.revzin at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/109362] New: codegen adds unnecessary extra add when reading atomic member
Date: Fri, 31 Mar 2023 14:01:16 +0000	[thread overview]
Message-ID: <bug-109362-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109362
           Summary: codegen adds unnecessary extra add when reading atomic
                    member
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

This program:

#include <atomic>

struct S {
    long size;
    std::atomic<char*> read_ptr;

    auto peek() const -> const char* {    
        return read_ptr.load(std::memory_order_acquire);
    }
};

auto with_atomic(S const& v) {
    while (true) {
        if (v.peek()) {
            return true;
        }
    }
}

emits (on gcc 12.2 -O3):

with_atomic(S const&):
        add     rdi, 8
.L2:
        mov     rax, QWORD PTR [rdi]
        test    rax, rax
        je      .L2
        mov     eax, 1
        ret

But that add is completely necessary, the mov could just be:

        mov     rax, QWORD PTR [rdi + 8]

which is what clang (16.0 -O3) generates:

with_atomic(S const&):                    # @with_atomic(S const&)
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        mov     rax, qword ptr [rdi + 8]
        test    rax, rax
        je      .LBB0_1
        mov     al, 1
        ret

It's not just an extra add, it's consuming an extra register - which has more
downstream optimization effects.

             reply	other threads:[~2023-03-31 14:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-31 14:01 barry.revzin at gmail dot com [this message]
2023-03-31 14:38 ` [Bug c++/109362] " barry.revzin at gmail dot com
2023-03-31 15:26 ` jakub at gcc dot gnu.org
2023-03-31 15:27 ` jakub at gcc dot gnu.org
2023-03-31 15:29 ` barry.revzin at gmail dot com
2023-04-01  7:01 ` [Bug tree-optimization/109362] " cvs-commit at gcc dot gnu.org

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-109362-4@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).