public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112657] New: missed optimization: cmove not used with multiple returns
@ 2023-11-21 21:42 goon.pri.low at gmail dot com
  2023-11-21 21:53 ` [Bug target/112657] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: goon.pri.low at gmail dot com @ 2023-11-21 21:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112657
           Summary: missed optimization: cmove not used with multiple
                    returns
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

This function

int unopt(int c) {
    if (c == 14)
        return -9;
    else
        return c;
}

unopt:
        mov     eax, edi
        cmp     edi, 14
        je      .L4
        ret
.L4:
        mov     eax, -9
        ret

Should probably be optimized to:

int opt(int c) {
    if (c == 14)
        c = -9;

    return c;
}

opt:
        cmp     edi, 14
        mov     eax, -9
        cmovne  eax, edi
        ret

This seems to only really happen when negative numbers are used,

int positive(int c) {
    if (c == 14)
        return 9;
    else
        return c;
}

positive:
        mov     eax, edi
        cmp     edi, 14
        mov     edx, 9
        cmove   eax, edx
        ret

Though use of positive values still isn't completely optimized (possibly same
as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97968)

Also it seems like if the order is reversed:

int reverse(int c) {
    if (c != 14)
        c = 9120;

    return c;
}

reverse:
        cmp     edi, 14
        mov     edx, 14
        mov     eax, 9120
        cmove   eax, edx
        ret

We could use %edi in the cmove and eliminate the 2nd instruction.

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

end of thread, other threads:[~2024-05-21  9:18 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-21 21:42 [Bug tree-optimization/112657] New: missed optimization: cmove not used with multiple returns goon.pri.low at gmail dot com
2023-11-21 21:53 ` [Bug target/112657] " pinskia at gcc dot gnu.org
2023-11-21 21:53 ` pinskia at gcc dot gnu.org
2023-11-21 21:59 ` [Bug rtl-optimization/112657] [13/14 Regression] " pinskia at gcc dot gnu.org
2023-11-22  9:16 ` ubizjak at gmail dot com
2023-11-22  9:28 ` ubizjak at gmail dot com
2023-11-22  9:38 ` ubizjak at gmail dot com
2023-11-22  9:46 ` ubizjak at gmail dot com
2023-11-22 10:48 ` rguenth at gcc dot gnu.org
2023-11-22 13:35 ` hubicka at gcc dot gnu.org
2024-03-07 21:01 ` law at gcc dot gnu.org
2024-05-21  9:18 ` [Bug rtl-optimization/112657] [13/14/15 " jakub 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).