From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4C3753945C08; Thu, 8 Apr 2021 14:41:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C3753945C08 From: "andysem at mail dot ru" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/99971] New: GCC generates partially vectorized and scalar code at once Date: Thu, 08 Apr 2021 14:41:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: andysem at mail dot ru 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: Thu, 08 Apr 2021 14:41:49 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99971 Bug ID: 99971 Summary: GCC generates partially vectorized and scalar code at once Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: andysem at mail dot ru Target Milestone: --- Consider the following code sample: struct A { unsigned int a, b, c, d; A& operator+=3D (A const& that) { a +=3D that.a; b +=3D that.b; c +=3D that.c; d +=3D that.d; return *this; } A& operator-=3D (A const& that) { a -=3D that.a; b -=3D that.b; c -=3D that.c; d -=3D that.d; return *this; } }; void test(A& x, A const& y1, A const& y2) { x +=3D y1; x -=3D y2; } The code, when compiled with options "-O3 -march=3Dnehalem", generates: test(A&, A const&, A const&): pushq %rbp movdqu (%rdi), %xmm1 pushq %rbx movl 4(%rsi), %r8d movdqu (%rsi), %xmm0 movl (%rsi), %r9d paddd %xmm1, %xmm0 movl 8(%rsi), %ecx movl 12(%rsi), %eax movl %r8d, %esi movl (%rdi), %ebp movl 4(%rdi), %ebx movl 8(%rdi), %r11d movl 12(%rdi), %r10d movups %xmm0, (%rdi) subl (%rdx), %r9d subl 4(%rdx), %esi subl 8(%rdx), %ecx subl 12(%rdx), %eax addl %ebp, %r9d addl %ebx, %esi movl %r9d, (%rdi) popq %rbx addl %r11d, %ecx popq %rbp movl %esi, 4(%rdi) addl %r10d, %eax movl %ecx, 8(%rdi) movl %eax, 12(%rdi) ret https://gcc.godbolt.org/z/Mzchj8bxG Here you can see that the compiler has partially vectorized the test functi= on - it converted "x +=3D y1" to paddd, as expected, but failed to vectorize "x = -=3D y2". But at the same time the compiler also generated scalar code, including for the already vectorized "x +=3D y1" line, basically duplicating it. Note that when either "x +=3D y1" or "x -=3D y2" is commented, the compiler= is able to vectorize the line that is left. It is also able to vectorize both lines when the +=3D and -=3D operators are applied to different objects instead o= f x. This is reproducible since gcc 8 up to and including 10.2. gcc 7 doesn't vectorize this code. With the current trunk on godbolt the generated code is different: test(A&, A const&, A const&): movdqu (%rsi), %xmm0 movdqu (%rdi), %xmm1 paddd %xmm1, %xmm0 movups %xmm0, (%rdi) movd %xmm0, %eax subl (%rdx), %eax movl %eax, (%rdi) pextrd $1, %xmm0, %eax subl 4(%rdx), %eax movl %eax, 4(%rdi) pextrd $2, %xmm0, %eax subl 8(%rdx), %eax movl %eax, 8(%rdi) pextrd $3, %xmm0, %eax subl 12(%rdx), %eax movl %eax, 12(%rdi) ret Here the compiler is able to vectorize "x +=3D y1" but not "x -=3D y2". At = least, it removed the duplicate scalar version of "x +=3D y1". Given that the compiler is able to vectorize each line in isolation, I would expect it to be able to vectorize them combined. Generating duplicate versi= ons of code is certainly not expected.=