public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions
@ 2022-05-05 14:18 redbeard0531 at gmail dot com
  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
  0 siblings, 2 replies; 3+ messages in thread
From: redbeard0531 at gmail dot com @ 2022-05-05 14:18 UTC (permalink / raw)
  To: gcc-bugs

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?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug target/105496] Comparison optimizations result in unnecessary cmp instructions
  2022-05-05 14:18 [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions redbeard0531 at gmail dot com
@ 2022-05-06  6:06 ` rguenth at gcc dot gnu.org
  2023-11-22  0:31 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-06  6:06 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64-*-*
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-05-06
            Version|unknown                     |12.0

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  We canonicalize x < 11 to x <= 10 and on GIMPLE we don't have CC
flags and thus we cannot combine the two compares (we could maybe invent
switch (x CC_EXPR 11) { case LT: .. }).  So _maybe_ compare elimination on RTL
is needed to do the job here.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug target/105496] Comparison optimizations result in unnecessary cmp instructions
  2022-05-05 14:18 [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions redbeard0531 at gmail dot com
  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
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-22  0:31 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |goon.pri.low at gmail dot com

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 112664 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-22  0:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-05 14:18 [Bug target/105496] New: Comparison optimizations result in unnecessary cmp instructions redbeard0531 at gmail dot com
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

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