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

[X86] mul.fix i4/i8 regresses from mulb -> imull sequence after i8 -> i32 promote

Open 222rohan opened this issue 1 week ago • 2 comments

Needs to be built on top of #241128.

For context, we add legality checks for MUL_LOHI. Though its i8/i16 forms are (in theory) legal in x86, we promote them to i32 as - a. it is faster, b. ISel for it was removed.

For mul.fix (TargetLowering::expandFixedPointMul(?)) it looks like it used to generate i8 forms before promotion. Now it expands it.

repro:

; llc -mtriple=i686
define i4 @f(i4 %x, i4 %y) {
  %r = call i4 @llvm.umul.fix.i4(i4 %x, i4 %y, i32 2)
  ret i4 %r
}

if #241128 lands:

  ; i4 -> promote to i8 -> promote to i32
  andb $15, %al
  andb $15, %cl
  movzbl %cl, %ecx
  movzbl %al, %eax
  imull %ecx, %eax
  shrb $2, %al

should be:

  andb $15, %al
  andb $15, %cl
  mulb %cl
  shrb $2, %al

cc @RKSimon

222rohan avatar Nov 26 '26 05:11 222rohan

@llvm/issue-subscribers-backend-x86

Author: Rohan Shenoy (222rohan)

Needs to be built on top of #241128.

For context, we add legality checks for MUL_LOHI. Though its i8/i16 forms are (in theory) legal in x86, we promote them to i32 as - a. it is faster, b. ISel for it was removed.

For mul.fix (TargetLowering::expandFixedPointMul(?)) it looks like it used to generate i8 forms before promotion. Now it expands it.

repro:

; llc -mtriple=i686
define i4 @<!-- -->f(i4 %x, i4 %y) {
  %r = call i4 @<!-- -->llvm.umul.fix.i4(i4 %x, i4 %y, i32 2)
  ret i4 %r
}

if #241128 lands:

  ; i4 -&gt; promote to i8 -&gt; promote to i32
  andb $15, %al
  andb $15, %cl
  movzbl %cl, %ecx
  movzbl %al, %eax
  imull %ecx, %eax
  shrb $2, %al

should be:

  andb $15, %al
  andb $15, %cl
  mulb %cl
  shrb $2, %al

cc @RKSimon

this would be ok as an alternative

  andl $15, %eax
  andl $15, %ecx
  imull %ecx
  shrb $2, %al

RKSimon avatar Nov 27 '26 11:11 RKSimon