https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112403
Bug ID: 112403 Summary: `s+ (a?-1:1)` and `a?s-1:s+1` produce two different code generation Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Blocks: 94274 Target Milestone: --- Target: aarch64 Take: ``` int foo(int t1, int s) { int t; if (t1 >= 0) t=1; else t=-1; s+=t; return s; } int foo1(int t1, int s) { if (t1 >= 0) s++; else s--; return s; } ``` This current produces: ``` foo: cmp w0, 0 mov w2, 1 csneg w2, w2, w2, ge add w0, w2, w1 ret foo1: cmp w0, 0 sub w0, w1, #1 csinc w0, w0, w1, lt ret ``` But we should be able to even just produce: ``` asr w8, w0, #31 // a >= 0 ? -1 : 0 orr w8, w8, #0x1 // |1 (or a >= ? -1 : 1) add w0, w8, w1 // s+=that ``` Note on x86, the foo is optimal even: ``` sarl $31, %edi orl $1, %edi leal (%rdi,%rsi), %eax ``` I should note this blocks PR 94274 work. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94274 [Bug 94274] fold phi whose incoming args are defined from binary operations