llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstCombine] Missed fold of (x + 1) * y to y << x when x is known to be in {0, 1}

Open 134ARG opened this issue 4 months ago • 9 comments

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.

134ARG avatar Jul 22 '26 06:07 134ARG

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

ParkHanbum avatar Jul 22 '26 09:07 ParkHanbum

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
}

134ARG avatar Jul 22 '26 10:07 134ARG

The code I proposed and your final version produce the same result after running through opt. Please check.

ParkHanbum avatar Jul 22 '26 10:07 ParkHanbum

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 avatar Jul 23 '26 02:07 134ARG

@134ARG Hi! Are you working on a PR for this? Thanks.

user1342234 avatar Jul 24 '26 23:07 user1342234

@user1342234 havent yet. Are you interested?

134ARG avatar Jul 26 '26 06:07 134ARG

@user1342234 havent yet. Are you interested?

Yes I am interesting in taking this on!

user1342234 avatar Jul 27 '26 15:07 user1342234

@134ARG Hello, do you have a real world example which uses this pattern?

user1342234 avatar Aug 10 '26 21:08 user1342234

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.

134ARG avatar Aug 12 '26 02:08 134ARG