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

    <tr>
        <th>Summary</th>
        <td>
            Chaining `uadd.with.overflow` should optimize to  `or disjoint`
        </td>
    </tr>

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

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

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

<pre>
    The following code is useful for doing bigint addition, where you need to add the the digits from both numbers but also add a carry <https://cpp.godbolt.org/z/c7PcWqbz7>
```cpp
bool uadd_with_carry(unsigned a, unsigned b, bool c, unsigned* out) {
 unsigned sum1;
    bool c1 = __builtin_uadd_overflow(a, b, &sum1);
 unsigned sum2;
    bool c2 = __builtin_uadd_overflow(sum1, (unsigned)c, &sum2);
    
    *out = sum2;
    return c1 | c2;
}
```

That `c1 | c2` stays an `or i1`, but it would be legal for it to be `or disjoint i1`: <https://alive2.llvm.org/ce/z/BMwvqN>

(This is a C++ translation of Rust's <https://doc.rust-lang.org/std/primitive.u32.html#method.carrying_add>, cc https://github.com/rust-lang/rust/issues/85532)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyElF2P6jYQhn_NcDPayJmQhFzkIrDlrlVVrdRL5Ngm8ZETc_wB2v31lUMKu2elVgL5YzzvOx4ezL3Xw6xUC-UeytcNj2G0rvXChjCJadNb-d6-jQrP1hh70_OAwkqF2mP06hwNnq1DaVOg14OeA3IpddB2BjrgbVRO4buNOCslMdgUxTCq5Sv1oIPHs7MT9jaMOMepV85jHwNy4--nOQru3DtCcRhDuHgoOqAj0FFcLtlgZW9NyKwbgI4fabf-U_z9s_-oofgNWAcVu3_E5QKs6601GLmUp5sO42lRBtrFeWmDRJ6qfqz6tFpSxOd9oA5tDEANQr0H1j0zfJxyKJY9xDU1Ryhe8XTqozZBz6fF3l6VOxt7A9otnosVULUIULNqfNalX3Xpf3TvUkl196y8EU8jehoh4r8DdTaGRfqrqVMhunm5Tn1AsUagfv3c5TRn3dvIA6aeP85WDH3g7x75nALWoc7T8XTzGFAHvNloJPYKjRr4HSsdEjG9WjOk9j9sIuyeWnTfkeBGXxVlxlynFQmhVi72v9-uP_9YoWAd0O5t1D6BzPEAtAfaY3B89oYnetGe8a_oA1Dtv_tIKzIXfXgxfB5WJx8k0PHi9KSDvqosFpSNYTJAxaTCaGW20Kbn4cSlTIXQAYXAr8qDDmPsM2EnoOPDYp0DHbX3UXmg464si-UHZN1GtoVsioZvVJvXBTVlXTVsM7ayLpszVX1Z8qIqec_qkrO6ZqUot1Q3fKNbYrTN84Kxhu1yltXVuabdVpS5kOetzGHL1MS1ebR0sxTQ5vkur2hjeK-MXx4PolndcIkCUXpLXJuSXvo4eNgyo33wT5mgg1HtYeR6Tm8HVCzhm6V_ZfZgOFEzLlzYS9CT_lAJiF9wgIptojPtf_Qxua7Dy8XZH0p8aeV6mWtL_wQAAP__0Y6UrA">