Issue 86874
Summary Missed peephole opt: `sub nuw 15, x` => `xor x, 15`
Labels llvm:instcombine, missed-optimization
Assignees
Reporter Kmeakin
    This transformation is beneficial because materializing the `15` argument to `sub` requires an extra instruction, but the `15` argument to `xor` can fit directly into the `xor` instruction

Rust example (https://godbolt.org/z/h7aTc5sca):
```rust
#![feature(unchecked_math)]

#[no_mangle]
pub fn src(x: u8) -> u8 {
    unsafe { 15_u8.unchecked_sub(x) }
}

#[no_mangle]
pub fn tgt(x: u8) -> u8 {
    x ^ 15
}
```

alive proof: https://alive2.llvm.org/ce/z/YKCPCA
```llvm
define noundef i8 @src(i8 noundef %x) unnamed_addr #0 {
  %_0 = sub nuw i8 15, %x
  ret i8 %_0
}

define noundef i8 @tgt(i8 noundef %x) unnamed_addr #0 {
 %_0 = xor i8 %x, 15
  ret i8 %_0
}

```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to