public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code
@ 2020-12-11 10:18 denis.campredon at gmail dot com
  2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: denis.campredon at gmail dot com @ 2020-12-11 10:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98236
           Summary: x plus/minus y cmp 0 produces unoptimal code
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denis.campredon at gmail dot com
  Target Milestone: ---

Created attachment 49733
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49733&action=edit
input file

Compiling test.c on x86 with -O2 leads to unoptimal code generation.

f1 to f4 could be optimized to `add + set(g|ge|l|le)`
f5 to f8 could be optimized to `sud|cmp + set(g|ge|l|le)`


For f2, f4, f6 and f8 no pattern is recognized.


For f1, f3, f5 and f7, the optimizers are not aware that, at least on x86,
`add` and `sub` can set flags.

This can also be seen with the following function. gcc will produce `cmp + sub`
although only `sub` could be used

-----------
void foo();

int bar(int x, int y) {
    if (x - y)
        foo();
    return x - y;
}
-----------
produces
-----------
bar(int, int):
        push    rbp
        mov     ebp, esi
        push    rbx
        mov     ebx, edi
        sub     rsp, 8
        cmp     edi, esi
        je      .L2
        call    foo()
.L2:
        mov     eax, ebx
        add     rsp, 8
        sub     eax, ebp
        pop     rbx
        pop     rbp
        ret
-----------

expected

-----------
bar(int, int):
        push    rbx
        mov     ebx, edi
        sub     ebx, esi
        je      .L2
        call    foo()
.L2:
        mov     eax, ebx
        pop     rbx
        ret
-----------

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

end of thread, other threads:[~2023-06-13  5:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
2020-12-11 10:39 ` rguenth at gcc dot gnu.org
2020-12-11 10:40 ` [Bug middle-end/98236] " rguenth at gcc dot gnu.org
2021-08-01 18:14 ` pinskia at gcc dot gnu.org
2023-06-13  5:56 ` 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).