Issue 239896
Summary [AArch64] `-1` unnecessarily materialized for `csel`
Labels backend:AArch64, missed-optimization
Assignees
Reporter Kmeakin
    https://godbolt.org/z/9vc8ensP7

This issue arises when compiling Rust's `saturating_add` for unsigned types:

```rs
pub fn saturating_mul(self, rhs: Self) -> Self {
    match self.checked_mul(rhs) {
        Some(x) => x,
        None => Self::MAX,
 }
}

pub fn checked_mul(self, rhs: Self) -> Option<Self> {
    let (a, b) = self.overflowing_mul(rhs);
    if intrinsics::unlikely(b) { None } else { Some(a) }
}
```

which after inlining becomes
```rs
pub fn saturating_mul(self, rhs: Self) -> Self {
    let (a, b) = self.overflowing_mul(rhs);
    if intrinsics::unlikely(b) { Self::MAX } else { a }
}
```

This produces
```asm
src32:
        umull   x9, w0, w1
 mov     w8, #-1
        tst     x9, #0xffffffff00000000
        csel w0, w9, w8, eq
        ret
```

if the `unlikely` is removed, the unnecessary `mov` disappears:
```asm
tgt32:
        umull   x8, w0, w1
 tst     x8, #0xffffffff00000000
        csinv   w0, w8, wzr, eq
 ret
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to