llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Missed Optimization: Consecutive divisions can be merged into one if intermediate multiplication doesn't overflow

Open MaxGraey opened this issue 1 year ago • 2 comments

I'm wondering whether computeOverflowForUnsignedMul fails to handle this scenario, or if InstCombine is simply missing the optimization?

https://godbolt.org/z/rc18KhfPo

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

define noundef i8 @src(i8 noundef %a, i8 noundef %b, i8 noundef %c) {
start:
  ;; assume b < sqrt(max)
  %b_less_than_sqrt_max = icmp ult i8 %b, 15
  tail call void @llvm.assume(i1 %b_less_than_sqrt_max)
  ;; assume c < sqrt(max)
  %c_less_than_sqrt_max = icmp ult i8 %c, 15
  tail call void @llvm.assume(i1 %c_less_than_sqrt_max)

  ;; perform a / b / c
  %div_ab  = udiv i8 %a, %b
  %res = udiv i8 %div_ab, %c
  ret i8 %res
}
define noundef i8 @tgt(i8 noundef %a, i8 noundef %b, i8 noundef %c) {
start:
  ;; perform a / (b * c)
  %mul_cb = mul nuw i8 %c, %b
  %res = udiv i8 %a, %mul_cb
  ret i8 %res
}

MaxGraey avatar Mar 25 '25 10:03 MaxGraey

I'm working on this.

Bagodiya avatar Aug 06 '26 15:08 Bagodiya

nice!

MaxGraey avatar Aug 09 '26 15:08 MaxGraey