llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.02k stars 11.96k forks source link

Opportunity to reuse overflow flag in `min/max(a, add/sub(a, b))` #109455

Open Kmeakin opened 1 month ago

Kmeakin commented 1 month ago

Expressions like a + b > a ? a : a + b can reuse the overflow flag from a + b to avoid a second comparison.

C++ examples Alive proofs

; 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
; 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
llvmbot commented 1 month ago

@llvm/issue-subscribers-backend-aarch64

Author: Karl Meakin (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 ```
llvmbot commented 1 month ago

@llvm/issue-subscribers-backend-x86

Author: Karl Meakin (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 ```