<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65863>65863</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Simplify add-with-overflow pattern (uaddo)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue,
llvm:instcombine,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
chfast
</td>
</tr>
</table>
<pre>
For unsigned integers to check if an addition overflowed (get carry bit) it is enough to compare the sum with one of its arguments `carry = sum < a`. However, it is not obvious that it is enough to only check one of the arguments and we may encounter a pattern where both arguments are checked.
```llvm
define i1 @src(i64 %a, i64 %b){
%s = add i64 %a, %b
%cond_a = icmp ult i64 %s, %a
%cond_b = icmp ult i64 %s, %b
%cond = or i1 %cond_a, %cond_b
ret i1 %cond
}
define i1 @tgt(i64 %a, i64 %b){
%s = add i64 %a, %b
%cond_a = icmp ult i64 %s, %a
ret i1 %cond_a
}
```
Playground: https://godbolt.org/z/TW8enc6zo
Proof: https://alive2.llvm.org/ce/z/iPAE7t
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzElE2P4zYMhn-NfCESyPJH4oMP2UmDHhdogR4XkkXbam0xkOgEmV9f-CM72Rmg1wJBbEgPX76kZOoYXecRa1F8E8U50RP3FOqmb3XkxJB91BcKMPkFs-A8Y4chAhM0PTb_gGtBe9DWOnbkgW4Y2oHuaEGoY4cMjQ7hAcaxUBU4BhcBPU1dv2jQeNUBgXuEOI1wd9wDeQRqwXEEHbppRM8RRClXJZGdF1Rkb6BFKffwO93xhkGot03fEwOZm6MpAveav6QlPzw2_1uy2cBHMu0t3BFG_QD0DU2eMYCGq2bG4OHeY0AwxP1rTMBVEu1eyLOQp-2_lOtvGG7jumSxdR7BpSByGUMj1NGVOQhV6KWI9d0IVYnDtzUE5pW4FK-thVd8QT-ghrz9oRfSNeMVpoGfeNxw_Rk3_4l_Vl9gCov_Z74NXdWefEB-gbZ2HM6v3fmlFdzx_9aKX73-0J_dPk_x1fz3QT-6QJO3IjtBz3yNIjsJdRHq0pE1NPCeQifU5V2oy59_HdE35TttwYGo_RqnB3dDtZ8vyxbb4Cbgvp9-O3Bi68xWWaUTrNOyylVVVEWa9LVUB9kcD1Why9yYg8za1B4Kk7ZpfmhlZhJXK6kyWclKpXlRpPs2t2WJ0qDUmW0wFbnEUbvhZ_bExThhXRbHMksGbXCIy6RQyuMdlk2hlFBvQqnlemcn5yM3NBrnP7ZGFyPaHV3Zje5dz4Ni3ivOSajnsJ2ZuihyObjI8SM5Ox6w_sON18G1j_mwd_N82D1nzM_vUajjpK0loapkCkP96SQc95PZNzQKdVlcro_dNdDf2PDc2bmSKNRlqfTfAAAA__-JjIgo">