| Issue |
239497
|
| Summary |
[ConstraintElimination] Missing relational proofs across signed and unsigned systems
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
134ARG
|
In FFmpeg's `filter_channels_float` and `filter_channels_double`, from `ffmpeg/af_aap.bc` in llvm-opt-benchmark, LLVM retains redundant loop-exit checks. The relevant part of `filter_channels_float`'s O3 output is shown below, with SSA and block names shortened and unrelated blocks and paths omitted:
```llvm
%positive = icmp sgt i32 %n, 0
br i1 %positive, label %process, label %skip
; ... intervening control flow and loops ...
loop.preheader:
%extended = zext nneg i32 %n to i64
; ...
loop.latch:
%next = add nuw nsw i64 %iv, 1
%equal = icmp eq i64 %next, %extended
br i1 %equal, label %loop.exit, label %loop.body
loop.exit:
br i1 %positive, label %continue, label %fallback ; redundant check
```
On the loop-exit edge, `%extended == %next` holds. The `nuw` increment is at least one, so this implies `%n >u 0`. Together with the nonnegativity required by `zext nneg`, this proves `%n >s 0`, making the final branch unconditional. CE can establish the unsigned bound but does not use it to answer the signed query.
This appears to be a query-time gap in `checkCondition()`. CE already transfers some facts between its signed and unsigned systems, but a failed relational query does not retry the other system using same-sign facts from the accumulated constraints. Signed and unsigned ordering agree when both operands have the same sign. This is related to #205282, but concerns relational comparisons rather than equality predicates.
Here is a standalone case that remains unfolded after `-O3` on current opttrunk (godbolt: https://godbolt.org/z/j7fMaE9vE):
```llvm
define i1 @test(i32 noundef %x, i32 noundef %y) {
entry:
%unsigned = icmp ult i32 %x, %y
%x_positive = icmp slt i32 0, %x
%xy = and i1 %unsigned, %x_positive
%y_positive = icmp slt i32 0, %y
%guard = and i1 %xy, %y_positive
br i1 %guard, label %if.then, label %if.else
if.then:
%signed = icmp slt i32 %x, %y
ret i1 %signed
if.else:
ret i1 true
}
```
This can fold to `ret i1 true` (alive2: https://alive2.llvm.org/ce/z/FrVSGS): when the guard holds, both inputs are positive, so `%x <u %y` implies `%x <s %y`. The other path already returns true. Like the FFmpeg case, an unsigned bound can answer the remaining signed query once the operands' nonnegativity is taken into account.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs