public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ubizjak at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/109483] New: Unoptimal jump threading with assembler flag output
Date: Wed, 12 Apr 2023 08:54:33 +0000	[thread overview]
Message-ID: <bug-109483-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109483

            Bug ID: 109483
           Summary: Unoptimal jump threading with assembler flag output
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ubizjak at gmail dot com
  Target Milestone: ---

Following testcase (int3 mnemonic is for marker only):


--cut here--
_Bool foo (int cnt)
{
  if (cnt == -1)
    {
      _Bool success;
      asm volatile("int3" : "=@ccz" (success));

      if (!success)
        return 0;
    }

  asm volatile ("" ::: "memory");
  return 1;
}
--cut here--

compiles w/ -O2 on x86_64 to:

0000000000000000 <foo>:
   0:   83 ff ff                cmp    $0xffffffff,%edi
   3:   74 0b                   je     10 <foo+0x10>
   5:   b8 01 00 00 00          mov    $0x1,%eax
   a:   c3                      retq   
   b:   0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
  10:   cc                      int3   
  11:   0f 94 c0                sete   %al
  14:   74 ef                   je     5 <foo+0x5>
  16:   c3                      retq   

Please note setting of %al before conditional jump. The instruction could be
moved after the jump, where the register could be cleared using "xor %eax,
%eax", similar to what clang creates:

0000000000000000 <foo>:
   0:   83 ff ff                cmp    $0xffffffff,%edi
   3:   75 06                   jne    b <foo+0xb>
   5:   cc                      int3   
   6:   74 03                   je     b <foo+0xb>
   8:   31 c0                   xor    %eax,%eax
   a:   c3                      retq   
   b:   b0 01                   mov    $0x1,%al
   d:   c3                      retq   

Also note that for ZF=1 gcc sets %al to 1, jumps to *5 where the register is
again set to 1. This is not the case in the clang code.

             reply	other threads:[~2023-04-12  8:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-12  8:54 ubizjak at gmail dot com [this message]
2023-04-12  8:59 ` [Bug middle-end/109483] " pinskia at gcc dot gnu.org
2023-04-12 10:47 ` [Bug tree-optimization/109483] Unoptimal uncprop " rguenth at gcc dot gnu.org
2023-04-12 10:48 ` rguenth at gcc dot gnu.org
2023-04-12 14:40 ` ubizjak at gmail dot com
2023-04-12 19:43 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109483-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).