From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 116CE3858D28; Fri, 31 Mar 2023 14:01:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 116CE3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680271277; bh=IphLnAcZttXcpGyib1me0b48GRrCga2c7sWUeMy5ng0=; h=From:To:Subject:Date:From; b=VVSShb4q576Pk/YI+JElOSVyz8FfcF9ir1q6vyYAjZBT30Wn2FvJqt0udbha96VVs /B9hHhg86IG03OyergBMRAe5L1S/WoeCeA1b4lT2/9JUHfUTzNJB14VSBghoYHBz8f 18iZMirg0gxLdOuOWQ+nMMRgA1sElp+OGiTPKDVs= From: "barry.revzin at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: barry.revzin at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109362 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 struct S { long size; std::atomic read_ptr; auto peek() const -> const char* {=20=20=20=20 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: # =3D>This Inner Loop Header: Depth= =3D1 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 mo= re downstream optimization effects.=