Issue 134318
Summary Missed Optimization: Consecutive divisions can be merged into one if intermediate multiplication with sext/zext extention
Labels new issue
Assignees
Reporter MaxGraey
    Similar to [#132908](https://github.com/llvm/llvm-project/issues/132908#issue-2945981308) we can replace consecutive divisions with one division and one multiplication even if last two operands are overflowing. To do this, we need to apply a `sext` or `zext` for all operands followed by a lowering to the previous width:

```rust
fn src(x: i8, y: i8, z: i8) -> i8 {
   x / y / z
}
```
```rust
fn dst(x: i8, y: i8, z: i8) -> i8 {
   let x = x as i16;
   let y = y as i16;
   let z = z as i16;
   (x / (y * z)) as i8
}
```

Alive2 proof: https://alive2.llvm.org/ce/z/fR5_nU

```llvm
define i8 @src(i8 %x, i8 %y, i8 %z) {
start:
  %_4 = sdiv i8 %x, %y
  %_0 = sdiv i8 %_4, %z
  ret i8 %_0
}
```
```llvm
define i8 @tgt(i8 %x, i8 %y, i8 %z) {
start:
  %_5 = sext i8 %x to i16
  %_7 = sext i8 %y to i16
  %_8 = sext i8 %z to i16
 %_6 = mul nsw i16 %_8, %_7
  %_4 = sdiv i16 %_5, %_6
  %_0 = trunc i16 %_4 to i8
  ret i8 %_0
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to