<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/92609>92609</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AArch64][X86] Suboptimal code for combining overflow flags
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:AArch64,
backend:X86,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
This code performs a sequence of 3 adds and sets `w0` if none of the adds overflow.
The generated code calculates `!(add1_overflowed | add2_overflowed)`.
It would be more efficient to generate `(add1_didnt_overflow & add2_didnt_overflow)`
```rs
#[no_mangle]
pub fn checked_add3(a: u32, b: u32, c: u32) -> Option<u32> {
a.checked_add(b)?.checked_add(c)
}
```
generated AArch64 code:
```asm
checked_add3:
adds w8, w0, w1
cset w9, hs
adds w1, w8, w2
csinc w8, w9, wzr, lo
eor w0, w8, #0x1
ret
```
Optimal AArch64 code:
```asm
checked_add2:
adds w1, w0, w1
cset w0, lo
ret
checked_add3:
adds w8, w0, w1
cset w9, lo
adds w1, w8, w2
csel w0, w9, wzr, lo
ret
```
The generated code for x86 is similarly suboptimal:
```asm
checked_add3:
add edi, esi
setb cl
add edi, edx
setb al
or al, cl
xor al, 1
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVU2PozgT_jXmUurI2IHAgUO6M5FevYc97Bz21jJ2EbxtcNY2k5759SsbOl_bk5V2ogjQU1XPU19g4b0-jIgNKZ5JscvEFHrrmv8PKN70mLVWfW--9tqDtArhiK6zbvAgwONfE44SwXbAQSjlQYwKPAYPpKQnSkoKuoPRjskn9Dh72W_oOmNPK0J3hG6_9ggHHNGJgGpWkcLIyYiAiYmwnLBKKJW_foSiArJ5iXTsCiOsJiVdaP8X4GQno6BFGKxDwK7TUuMYINiz4Mw_kyutxnCmA8LKWeAWn0VmjeVa0vnv_AIwTorn0b4OYjwYJMVuxo9TC90Iskf5hupVKMWjNuFbmDgj7AXaq2d5fq7hifAv8NsxaDsS_hJB_gXI5nnmBbG64iSsamOSfH-HyojOCW52d6lf13MZxnbrZF-u01AIvy9X-GFGbgr6cIPll0YOAKcqFnWi6ZovPtJjSMY6wr3_SWiegmYCdusjvR7lhT7xnH64eDP21hWtS_clhxRAGKfv-a2fw_CgO3EKgzD_oTfs573JH_WGflbMJclfHMG5j9eTuFd7OAnp0Vz19dEIHrf2ky9BZx28VyVoD14P2ghnvoOfWjtP4ReWElDpmCJ6fWvzGNrUEfPPmLRGS5x6_yRO3AUtKxfx-ErfWd8X82z91zXMVMNVzWuRYZNv8qKu2LpiWd-UOatLKsUmlypHWlAmlairnLe8rIUsM90wyta0yDcsX1c0X61bwTpBeZmrgvK6JWuKg9BmZcy3YWXdIdPeT9jUrKR1ZkSLxqcTgrFWyDccFeHb5RUgLH6vbix_VOUZHbT3qJ7SxPQPkb5hjMWjxjVR7amdDp6sqdE--It-0MGkQ-lDpdiR4jkSFzv4_bwBlzWRdmj1qMfD-YCBzoiDzyZnmj6Eo49rwPaE7Q869FO7knYgbB8Vl9vT0dk_UQbC9ql8T9g-deDvAAAA__-uoPzB">