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 via email to