From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 66BF73858D35; Wed, 15 May 2024 01:02:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 66BF73858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1715734921; bh=BE+AYDDZolZi+TYxqpBBHfyO6gHdHaruhR0G/nejKvY=; h=From:To:Subject:Date:From; b=hhktwdKuL0JA8HkEKhsWZlGZoJ6GHD8yv4NCgDNAntqLbo2bipbGMH7VtSSDSWVAu qmIm5tVAMXadWPMy9iui2y2aeh3CX4t3u+PHupZp62rWw20sBhxSziJsMF9N1sXoS5 oM/2LXNcN8hbLmtDqhzJveM2KkxMeSaeIDrPRkIo= From: "arthur.j.odwyer at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/115097] New: Strange suboptimal codegen specifically at -O2 when copying struct type Date: Wed, 15 May 2024 01:02:00 +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: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: arthur.j.odwyer 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=3D115097 Bug ID: 115097 Summary: Strange suboptimal codegen specifically at -O2 when copying struct type Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: arthur.j.odwyer at gmail dot com Target Milestone: --- // https://godbolt.org/z/G7qG4vvWb (C++ version) // https://godbolt.org/z/fT793cznT (C version) struct A { int a; short b; }; A test1(A& a) { return a; } A test2(A&& a) { return a; } A test3(const A& a) { return a; } A test4(const A&& a) { return a; } At -O1, they all have the same perfect codegen: test2(A&&): mov rax, QWORD PTR [rdi] ret At -O2, all-but-one of them have weird suboptimal (but correct) codegen: test2(A&&): movzx edx, WORD PTR [rdi+4] mov eax, DWORD PTR [rdi] sal rdx, 32 or rax, rdx ret What's really weird is that it really is "all but one of them." The lexical= ly first function will have good codegen, and then the subsequent ones will ha= ve the suboptimal codegen. You can comment out the good one and watch GCC pick another one to make good. You can reorder the definitions and see GCC's decision of which one to optimize will change. This behavior dates all the way back to GCC 5. In GCC 4.9.4, -O2 didn't have this weird behavior; all four functions would just be optimal all the time. The same symptom reproduces on x86-64, ARM64, and RISC-V (32 *and* 64). The RISC-V result seems to indicate it's not specifically limited to 64-bit. It also reproduces in C, with pointers instead of references. It seems to have something to do with the signature of the enclosing functi= on, which is super weird: https://godbolt.org/z/KPac7bvTM=