From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5FABF388A432; Fri, 12 Feb 2021 01:17:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5FABF388A432 From: "magiblot at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99078] New: Optimizer moves struct initialization into loop Date: Fri, 12 Feb 2021 01:17:52 +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: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: magiblot at hotmail 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Feb 2021 01:17:52 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99078 Bug ID: 99078 Summary: Optimizer moves struct initialization into loop Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: magiblot at hotmail dot com Target Milestone: --- Consider the following piece of code (https://godbolt.org/z/WhTcbd): > struct S > { > char c[24]; > }; >=20 > void copy(S *dest, unsigned count) > { > S s {}; > for (int i =3D 0; i < 7; ++i) > s.c[i] =3D i; > for (int i =3D 8; i < 15; ++i) > s.c[i] =3D i; > for (int i =3D 16; i < 23; ++i) > s.c[i] =3D i; > while (count--) > *dest++ =3D s; > } The generated assembly with -O2 looks like this: > copy(S*, unsigned int): > mov QWORD PTR [rsp-24], 0 > pxor xmm0, xmm0 > movups XMMWORD PTR [rsp-40], xmm0 > test esi, esi > je .L1 > mov esi, esi > lea rax, [rsi+rsi*2] > lea rdx, [rdi+rax*8] > .L3: > mov eax, 1541 > mov ecx, 3340 > mov esi, 5396 > mov DWORD PTR [rsp-39], 67305985 > mov WORD PTR [rsp-35], ax > add rdi, 24 > mov DWORD PTR [rsp-32], 185207048 > mov WORD PTR [rsp-28], cx > mov BYTE PTR [rsp-26], 14 > movdqu xmm1, XMMWORD PTR [rsp-40] > mov DWORD PTR [rsp-24], 319951120 > mov WORD PTR [rsp-20], si > mov BYTE PTR [rsp-18], 22 > mov rax, QWORD PTR [rsp-24] > movups XMMWORD PTR [rdi-24], xmm1 > mov QWORD PTR [rdi-8], rax > cmp rdi, rdx > jne .L3 > .L1: > ret It can be seen that the struct initialization has been moved into the loop, which is a severe pessimization. The issue cannot be reproduced if the struct is initialized this way: > S s; > memset(&s, 0, sizeof(s)); But the following still reproduces the issue: > S s {}; > memset(&s, 0, sizeof(s)); Replacing the assignment inside the loop with memcpy does not affect the result. According to Godbolt, the generated assembly has not changed since GCC 7.2.= GCC 7.1 does not use vector registers but still initializes the struct inside t= he loop. GCC 6.4 and earlier do not use vector registers either but do initial= ize the struct outside the loop, as expected. EXPECTED RESULT Ideally, the loop body would be optimized into something like this: > movdqu xmm1, XMMWORD PTR [rsp-40] > mov rax, QWORD PTR [rsp-24] > .L3: > add rdi, 24 > movups XMMWORD PTR [rdi-24], xmm1 > mov QWORD PTR [rdi-8], rax > cmp rdi, rdx > jne .L3 > .L1: > ret Thank you.=