Missed folding of a masked-bit test implied by an unsigned range check
For the following IR:
define i32 @src(i32 %len) {
entry:
%masked = and i32 %len, 16
%c0 = icmp ult i32 %len, 13
br i1 %c0, label %check, label %exit0
exit0:
ret i32 0
check:
%c1 = icmp eq i32 %masked, 0
br i1 %c1, label %exit1, label %exit2
exit2:
ret i32 2
exit1:
ret i32 1
}
opt -O3 preserves the bit test and keep the two branches as selects (https://godbolt.org/z/fMoxf7bzb):
define range(i32 0, 3) i32 @src(i32 %len) {
entry:
%masked = and i32 %len, 16
%c0 = icmp ult i32 %len, 13
%c1 = icmp eq i32 %masked, 0
%inner = select i1 %c1, i32 1, i32 2
%result = select i1 %c0, i32 %inner, i32 0
ret i32 %result
}
The %c1 comparison is redundant. When %len < 13, %len & 16 == 0 always holds. Therefore, the expected minimal IR should be like (alive2 https://alive2.llvm.org/ce/z/nBciPT):
define range(i32 0, 2) i32 @tgt(i32 %len) {
entry:
%c0 = icmp ult i32 %len, 13
%result = zext i1 %c0 to i32
ret i32 %result
}
LLVM has successful range implication testcases in llvm/test/Transforms/JumpThreading/basic.ll,but it can not handle the bitmask <-> range. JumpThreading queries LazyValueInfo::getPredicateOnEdge() who does not seem to propagate the range info of %len to the bitmask.
InstCombine could handle this case, but is blocked by a phase ordering problem with SimplifyCFG in O2/O3 (check the other three opt outputs in https://godbolt.org/z/fMoxf7bzb).
@134ARG Hey, are you working on this? If not, I would be interested in taking this on
@benscharl havent yet. You could take that, thanks!