Issue 109455
Summary Opportunity to reuse overflow flag in `min/max(a, add/sub(a, b))`
Labels backend:AArch64, backend:X86, missed-optimization, llvm:SelectionDAG
Assignees
Reporter Kmeakin
    Expressions like `a + b > a ? a : a + b` can reuse the overflow flag from `a + b` to avoid a second comparison.

[C++ examples](https://godbolt.org/z/W5Yjq5rGT)
[Alive proofs](https://alive2.llvm.org/ce/z/ft4Yar)

```asm
; AArch64
src_umax_add:
        add     w8, w1, w0
        cmp     w0, w8
        csel    w0, w0, w8, hi
        ret

tgt_umax_add:
 adds    w8, w0, w1
        csel    w0, w0, w8, hs
 ret
```

```asm
; x86-64
src_umax_add:
        lea     eax, [rsi + rdi]
        cmp     edi, eax
        cmova   eax, edi
 ret

tgt_umax_add:
        mov     eax, esi
        add     eax, edi
        cmovb   eax, edi
        ret
```


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to