llvm-project
llvm-project copied to clipboard
Missed Optimization: Consecutive divisions can be merged into one if intermediate multiplication doesn't overflow
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
}
I'm working on this.
nice!