| Issue |
239543
|
| Summary |
[InstCombine] Missed folding of always-false range checks on non-wrapping multiplication
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
134ARG
|
FFmpeg's `mlp_encode_frame`, in `ffmpeg/mlpenc.bc` from llvm-opt-benchmark, retains redundant range checks in its O3 output.
The pattern skeleton is as follows:
```llvm
%scaled = mul nuw nsw i64 %iv, 26456
; ...
%offset = add nsw i64 %scaled, -26457
%diff.check = icmp ult i64 %offset, 31
; ...
%short = icmp ult i8 %count, 8
%fallback = select i1 %short, i1 true, i1 %diff.check
br i1 %fallback, label %scalar, label %vector
```
The comparison tests whether `%scaled` is in `[26457, 26488)`, which contains no multiple of 26456. With `nuw`, the multiplication cannot wrap into this interval, so `%diff.check` is always false on defined executions and the dispatch only needs `%short`. There are two such checks in this function. FFmpeg's `test_fill_picture`, in `ffmpeg/vsrc_testsrc.bc`, has the same pattern with a multiplier of 13 and the interval `[1, 8)`.
This looks like a missing fold in `foldICmpAddConstant()`. `foldICmpMulConstant()` already folds bounds through non-wrapping multiplication, but here an add separates the multiplication from the comparison. It could check whether the inferred range contains any multiple of the constant multiplier. If no such multiple exists, the comparison must be false.
The following standalone reproducer is still not folded to a constant by `-O3` on (current opt truck: https://godbolt.org/z/ecKocs956):
```llvm
declare { i32, i1 } @llvm.smul.with.overflow.i32(i32, i32)
define i1 @test(i32 noundef %a) {
entry:
%square = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %a, i32 %a)
%overflow = extractvalue { i32, i1 } %square, 1
br i1 %overflow, label %then, label %else
then:
%mul = mul nuw nsw i32 %a, 10000
%add = add nsw i32 %mul, -5000
%cond = icmp ult i32 %add, 4999
ret i1 %cond
else:
ret i1 false
}
```
It can return `false` directly (alive2: https://alive2.llvm.org/ce/z/tqZBuo): the true path tests whether a non-wrapping multiple of 10000 lies in `[5000, 9999)`, and the other path already returns false.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs