<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/109455>109455</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Opportunity to reuse overflow flag in `min/max(a, add/sub(a, b))`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            backend:X86,
            missed-optimization,
            llvm:SelectionDAG
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Kmeakin
      </td>
    </tr>
</table>

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


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVM1u8zYQfBrqQtiQVj-mDjrIdpRDDz20QJtTQIlrmwklqiTlOHn6grL8-xn5QhhLYGe4Ox6Ky62V2w6xIOmSpOuAD26nTfFHi_xddkGtxWfxdOgNWit1Z6mS70hJFnJKYElrSuInyimJqzGWdMqTLKQN76jBwSJ1O6R6j2aj9AfdKL6lG6PbqzKe7jTley0F5dRioztBG9323EiruzkJ1yQsp5guVwSW_iAeeNsrtCRdE2A753pL4pJARaDaalFr5ebabAlUXwSqf9KXt_9S8_w3gfxcqlRyj7Q3Wm8eluEeh7lS-3Yq1eBUb-OSF24uxY4xC48_btspEy9pWZpmlyXHhDXN69DywysXwvcZk3RaXIhx_2AEVvQjGmN4y2na_sgJR5TdoRbVFXriwIru5C3ToLuW7rbugS4uhL0SFE6yftrSTsxLr5NBv3ftwLLZj0xTyMcd-cE3JenSWDl-WkZIf6sP7UMhPdsfusP1nl-qedr9f_jGr2m1en8tCa18fM-_NLkSUX-Df-9nIIpY5HHOAyyiBWQszSKWBLuCQbRIFjlECeMgEoZJU2cNQ4bIGpbwQBYQQhLmEEYsyiCZZ3mWZIso5ps0hniTkSTElkt1fhKBtHbAIgrzJE0DxWtUdhwnADVv3rHz5pweAACB1Q3yL8vO2VZai2Kmeydb-cWd1N0Z8-1IXP6FChsPrMtnj6XrwBQem9XD1pIkVNI6exHnpFNY_Nn32rihk-7TD5rjWLodSbLzA6mVHYGq9aYz7o33dwuVHepTovYPHnKShcFgVHE3dKTbDfW80S2BalR83Ga90W_YOALV6JYlUE2G7Qv4PwAA__-KBoc_">