public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109362] New: codegen adds unnecessary extra add when reading atomic member
@ 2023-03-31 14:01 barry.revzin at gmail dot com
  2023-03-31 14:38 ` [Bug c++/109362] " barry.revzin at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: barry.revzin at gmail dot com @ 2023-03-31 14:01 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2023-04-01  7:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-31 14:01 [Bug c++/109362] New: codegen adds unnecessary extra add when reading atomic member barry.revzin at gmail dot com
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

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