public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "zero at smallinteger dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/106420] New: Missed optimization for comparisons
Date: Sat, 23 Jul 2022 09:25:36 +0000	[thread overview]
Message-ID: <bug-106420-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 106420
           Summary: Missed optimization for comparisons
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zero at smallinteger dot com
  Target Milestone: ---

Created attachment 53339
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53339&action=edit
Sample code

When comparing different variables to the same constants, in some cases the
compiler could first combine the variables and then do a single compare.  In
the sample given, two variables are compared against 7.  In the slow path, GCC
produces the following with -O2.

        cmp     edi, 7
        setg    al
        cmp     esi, 7
        setg    dl
        or      eax, edx
        movzx   eax, al
        ret

In the fast path, GCC produces this instead.

        or      edi, esi
        xor     eax, eax
        cmp     edi, 7
        setg    al
        ret

Although the expression a > 7 || b > 7 is the same as (a | b) > 7, the latter
is better because it results in fewer instructions.  A quick experiment shows
the latter also runs quite faster.

Verified with Godbolt for GCC trunk.  Clang, ICC, and MSVC latest versions also
miss this opportunity as per Godbolt.

             reply	other threads:[~2022-07-23  9:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-23  9:25 zero at smallinteger dot com [this message]
2022-07-23  9:38 ` [Bug target/106420] " zero at smallinteger dot com
2022-07-25  2:12 ` [Bug tree-optimization/106420] " crazylht at gmail dot com
2022-07-25  2:23 ` pinskia at gcc dot gnu.org
2022-07-25  2:35 ` zero at smallinteger dot com

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-106420-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).