Bug 109019 - Failure to optimize b + c -1
Summary: Failure to optimize b + c -1
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2023-03-03 23:49 UTC by Thomas Koenig
Modified: 2023-03-04 00:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2023-03-03 23:49:51 UTC
Looks like a general RTL issue, I see this on POWER, RV64 and ARM64 (the
latter two on godbolt).


[tkoenig@gcc135 ~]$ cat c.c
long foo (long b, long c)
{
  return b + c - 1;
}
[tkoenig@gcc135 ~]$ gcc -O3 -S c.c
[tkoenig@gcc135 ~]$ cat c.s
        .file   "c.c"
        .machine power8
        .abiversion 2
        .section        ".text"
        .align 2
        .p2align 4,,15
        .globl foo
        .type   foo, @function
foo:
.LFB0:
        .cfi_startproc
        add 3,3,4
        addi 3,3,-1
        blr
        .long 0
        .byte 0,0,0,0,0,0,0,0
        .cfi_endproc
.LFE0:
        .size   foo,.-foo
        .ident  "GCC: (GNU) 13.0.1 20230215 (experimental)"
        .section        .note.GNU-stack,"",@progbits

This should be

        addi    3,4,-1
        ret
Comment 1 Jakub Jelinek 2023-03-03 23:57:24 UTC
How could it?
        addi    3,4,-1
        blr
would be return c - 1; rather than return b + c - 1;
Comment 2 Thomas Koenig 2023-03-04 00:01:22 UTC
Urgh,too late in the evening, I guess. Sorry for the noise.