From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 105C13858C2C; Thu, 24 Aug 2023 20:05:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 105C13858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692907510; bh=gLvo7YV8QGI5KyxsZW7/kzQ1VoYQ+U+aQgTwmcDcb1w=; h=From:To:Subject:Date:From; b=DMTqcw5Yv/DMRivFcuy7Sj37TTK2W1kD+LhoAX7CZlLXTKa1n69g8/Ye2rwYjOSaf VZ8Oj0x6EmkzW7PtqWMllIL+iVJT8IrMwlGUB1t80s8WEP5hGYUZRye0cy1ie3AchQ iBN6D6ikyrwfHomJCb3eV9gOWKkwR3YVwZT+wyg8= From: "eggert at cs dot ucla.edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/111143] New: [missed optimization] unlikely code slows down diffutils x86-64 ASCII processing Date: Thu, 24 Aug 2023 20:05:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 13.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: eggert at cs dot ucla.edu 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 attachments.created 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=3D111143 Bug ID: 111143 Summary: [missed optimization] unlikely code slows down diffutils x86-64 ASCII processing Product: gcc Version: 13.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: eggert at cs dot ucla.edu Target Milestone: --- Created attachment 55788 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D55788&action=3Dedit source code illustrating the performance problem This bug report may be related to bug 110823 (also found for diffutils) but= the symptoms differ somewhat so I am reporting it separately. I observed it with GCC 13.1.1 20230614 (Red Hat 13.1.1-4) on x86-64. While tuning GNU diffutils I noticed that its loops to process mostly-ASCII text were not compiled well by GCC on x86-64. For a stripped-down example of the problem, compile the attached program with: gcc -O2 -S code-mcel.c The result is in the attached file code-mcel.s. Its loop kernel assuming AS= CII text (starting on line 44) looks like this: .L6: movsbq (%rbx), %rax testb %al, %al js .L4 addq %rax, %r12 movl $1, %eax .L5: addq %rax, %rbx cmpq %r13, %rbx jb .L6 The "movl $1, %eax" immediately followed by "addq %rax, %rbx" is poorly scheduled; the resulting dependency makes the code run quite a bit slower t= han it should. Replacing it with "addq $1, %rbx" and readjusting the surrounding code accordingly, as is done in the attached file code-mcel-opt.s, causes t= he benchmark to run 38% faster on my laptop's Intel i5-1335U. It seems that code that GCC knows is unlikely (because of __builtin_expect)= is causing the kernel, which GCC knows is likely, to be poorly optimized.=