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

    <tr>
        <th>Summary</th>
        <td>
            [x86] Missed optimization with addc : uses cmp to get if an addition would overflow before actually computing it 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Hello there, I've found the following missed optimization wile compiling.
I need to perform  an unsigned addition repeatedly, and count how many times it wrapped around
normally this can be done with 
```asm 
add <dy>, <acc> 
addc <acc>, 0  // counter+=1 if add overflowed
```
but instead clang compiles to 
```asm 
cmp <dy>, <acc> 
addc <acc>, 0  // counter+=1 if add overflowed
add <dy> , <acc> 
```
Unless I use something like 
```c
        y+=__builtin_uadd_overflow(dy,acc,&acc);
```
[link to a godbolt example showing the issue](https://godbolt.org/z/hErscdox7)

I hope the x86 compiler team can fix it :) 
Thanks! 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VE2PmzAQ_TXmMmoEZgPhwGE_1T301p4jGw_grrERNptkf33HwCpVtD02Igpjj-e9efMc6dSl_o7GOAg9Tsj4I7wyXr4jtG62Kq7SG-2ftO1g0N6jAjcGPegPEbSzcNIGoXHDqA2l7Fj6xNL7V7BIicHBiFPrpgFAWJit152ldaGUXg5POKIIqMwlIgsCbAg2QO9OMAh7AQJCDzrAaRLjGI9OkdeKYqmwMIayeu2hIQSJoJxFIhV6WJNYka6P8MO2RPDA8kd1YflzxKV30TQUXPeb62LMSAEYf6Fn5YcT4w8sf8pAt7EZcO_UJomE6gZ0DeUcQFsfUFCDRpCSq2DUGkn0T57NMP5nnn8rAV9B3DTyyxJnD68wewTvBiTlqRuj3_D2QLPGsH0uK5PjUc7aBG2PM2EfP_kwflDRAhGbPzJeLC8Vyx--pMH2D2S2tyiegM4p6UwAPIthJC_6fjVrtC75dUa2f6L6fQijZ_n9qs92aOemjqIP-vbPk2-UO5cRd0XZrNy7EZdq50PxObgJaJjD4rlWn6NBl9LVpsLPXtg3z3gGCdZZUWS8LPdpmqg6V1VeiSToYLCmPqgm8YMfX94sMvE24_uoOHmcHEFNdxiWidrrTTq52VznSxeBbh2CaMK83JBIew5RFqKazJOpb_QgrFnuKI0CY94_f76Nk_uNTaBw0ZJ6etmXeVYkfS2b9G4v07ySB4kiF7Jo7jIp01S1WZO1bWKERONjl4xzi6dtHJxTx4muecp5euB5Vu4PPN3xMiuzvK0OlaqyNs_YXYqD0GYXecRBJVO9UJJz52nTaB_8dVP45c9lETXWF3Po3VQLK3y4dJa8mizw9UL_D6VgihE">