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

Reply via email to