[InstCombine] Missed fold of (x + 1) * y to y << x when x is known to be in {0, 1}
For the below C code:
uint16_t f(uint16_t x, uint16_t y) {
uint16_t bounded = x < 2 ? x : 0;
return (uint16_t)((bounded + 1) * y);
}
LLVM produces the following suboptimal IR under -O3 (https://godbolt.org/z/M7q1r6Kbx):
define i16 @f(i16 noundef %0, i16 noundef %1) {
%3 = add nuw nsw i16 %0, 1
%4 = icmp ugt i16 %0, 1
%5 = select i1 %4, i16 1, i16 %3
%6 = mul i16 %5, %1
ret i16 %6
}
which could be further optimized as (https://alive2.llvm.org/ce/z/cSYLr-):
define i16 @f(i16 noundef %x, i16 noundef %y) {
%in.range = icmp ult i16 %x, 2
%bounded = select i1 %in.range, i16 %x, i16 0
%result = shl i16 %y, %bounded
ret i16 %result
}
The problem is LLVM already has success testcases for such a fold in llvm/test/Transforms/InstCombine/mul.ll, like the one below:
define i16 @mul_add_one(i16 range(i16 0, 2) %x, i16 %y) {
; CHECK-LABEL: @mul_add_one(
; CHECK-NEXT: [[RET:%.*]] = shl i16 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret i16 [[RET]]
;
%add = add i16 %x, 1
%ret = mul i16 %add, %y
ret i16 %ret
}
It appears that the existing fold is not applied when the same {0, 1} range is established through a select rather than a range attribute.
I think this look like more benefit.
define i16 @src(i16 noundef %0, i16 noundef %1) {
%3 = add nuw nsw i16 %0, 1
%4 = icmp ugt i16 %0, 1
%5 = select i1 %4, i16 1, i16 %3
%6 = mul i16 %5, %1
ret i16 %6
}
define i16 @tgt(i16 noundef %0, i16 noundef %1) {
%is_one = icmp eq i16 %0, 1
%5 = select i1 %is_one, i16 2, i16 1
%6 = mul i16 %5, %1
ret i16 %6
}
https://alive2.llvm.org/ce/z/FQgPLQ
I think this look like more benefit.
Brilliant! Based on that I think there might be a even better case (https://alive2.llvm.org/ce/z/fbGYh3):
define i16 @tgt(i16 noundef %x, i16 noundef %y) {
%is_one = icmp eq i16 %x, 1
%twice = shl i16 %y, 1
%result = select i1 %is_one, i16 %twice, i16 %y
ret i16 %result
}
The code I proposed and your final version produce the same result after running through opt. Please check.
define i16 @f1_f2_after_opt(i16 noundef %x, i16 noundef %y) {
%is_one = icmp eq i16 %x, 1
%twice = zext i1 %is_one to i16
%result = shl i16 %y, %twice
ret i16 %result
}
Yup. The one opt produced after all this is best so far at the IR level.
@134ARG Hi! Are you working on a PR for this? Thanks.
@user1342234 havent yet. Are you interested?
@134ARG Hello, do you have a real world example which uses this pattern?
I do not currently have an exact real-world occurrence. The testcase was derived from test2 in llvm/test/CodeGen/NVPTX/combine-mad.ll, which contains (s < 1 ? 1 : n + 1) * m. A bounded variant that correlates the condition with n gives (n < 2 ? n + 1 : 1) * m.
The issue is intended to show a representation-sensitive gap around the {0, 1} value set fact. The existing fold handles (x + 1) * y when this fact is expressed directly as a range, while the equivalent fact established by the correlated ternary does not produce the same optimization result. The select is one concrete representation of the fact; the underlying issue is that the optimization result depends on its representation.