public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redbeard0531 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions
Date: Thu, 05 May 2022 14:18:53 +0000	[thread overview]
Message-ID: <bug-105496-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105496
           Summary: Comparison optimizations result in unnecessary cmp
                    instructions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/1zdYsaqEj

Consider these equivalent functions:

int test1(int x) {
    if (x <= 10)
        return 123;
    if (x == 11)
        return 456;
    return 789;
}

int test2(int x) {
    if (x < 11)
        return 123;
    if (x == 11)
        return 456;
    return 789;
}

In test2 it is very clear that you can do a single cmp of x with 11 then use
different flag bits to choose your case. In test1 it is less clear, but because
x<=10 and x<11 are equivalent, you could always transform one to the other.
Clang seems to do this correctly and transforms test1 into test2 and only emits
a single cmp instruction in each. For some reason, not only does gcc miss this
optimization, it seems to go the other direction and transform test2 into
test1, emitting 2 cmp instructions for both!

test1(int):
        mov     eax, 123
        cmp     edi, 10
        jle     .L1
        cmp     edi, 11
        mov     eax, 456
        mov     edx, 789
        cmovne  eax, edx
.L1:
        ret
test2(int):
        mov     eax, 123
        cmp     edi, 10
        jle     .L6
        cmp     edi, 11
        mov     eax, 456
        mov     edx, 789
        cmovne  eax, edx
.L6:
        ret

Observed with at least -O2 and -O3. I initially observed this for code where
each if generated an actual branch rather than a cmov, but when I reduced the
example, the cmov was generated.

I'm not sure if this should be a middle-end or target specific optimization,
since ideally it would be smart on all targets that use comparison flags, even
if there are some targets that don't. Is there ever a down side to trying to
make two adjacent comparisons compare the same number?

             reply	other threads:[~2022-05-05 14:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-05 14:18 redbeard0531 at gmail dot com [this message]
2022-05-06  6:06 ` [Bug target/105496] " rguenth at gcc dot gnu.org
2023-11-22  0:31 ` 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-105496-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).