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

[InstCombine] `x * 3 / 2` => `x + x / 2` when overflow is impossible

Open Kmeakin opened this issue 4 months ago • 5 comments

Compiler explorer Alive proof

InstCombine performs this rewrite for unsigned integers, but not for signed integers:

#include <stdint.h>

extern "C" {
auto src1(uint32_t x) -> uint32_t {
    uint32_t product;
    if (__builtin_mul_overflow(x, 3, &product)) __builtin_unreachable();
    return product / 2;
}
auto tgt1(uint32_t x) -> uint32_t { return x + (x / 2); }

auto src2(int32_t x) -> int32_t {
    int32_t product;
    if (__builtin_mul_overflow(x, 3, &product)) __builtin_unreachable();
    return product / 2;
}
auto tgt2(int32_t x) -> int32_t { return x + (x / 2); }
}
define dso_local range(i32 0, -2147483648) i32 @src1(i32 noundef %x) local_unnamed_addr {
entry:
  %0 = lshr i32 %x, 1
  %div1 = add nuw i32 %0, %x
  ret i32 %div1
}

define dso_local noundef i32 @tgt1(i32 noundef %x) local_unnamed_addr {
entry:
  %div2 = lshr i32 %x, 1
  %add = add i32 %div2, %x
  ret i32 %add
}

define dso_local range(i32 -1073741824, 1073741824) i32 @src2(i32 noundef %x) local_unnamed_addr {
entry:
  %0 = mul nsw i32 %x, 3
  %div = sdiv i32 %0, 2
  ret i32 %div
}

define dso_local i32 @tgt2(i32 noundef %x) local_unnamed_addr {
entry:
  %div = sdiv i32 %x, 2
  %add = add nsw i32 %div, %x
  ret i32 %add
}

On AArch64 and RISC-V this saves one instruction:

src1:
        add     w0, w0, w0, lsr #1
        ret

tgt1:
        add     w0, w0, w0, lsr #1
        ret

src2:
        add     w8, w0, w0, lsl #1
        add     w8, w8, w8, lsr #31
        asr     w0, w8, #1
        ret

tgt2:
        add     w8, w0, w0, lsr #31
        add     w0, w0, w8, asr #1
        ret
src1:
        srliw   a1, a0, 1
        addw    a0, a0, a1
        ret

tgt1:
        srliw   a1, a0, 1
        addw    a0, a0, a1
        ret

src2:
        slli    a1, a0, 1
        add     a0, a0, a1
        srliw   a1, a0, 31
        add     a0, a0, a1
        sraiw   a0, a0, 1
        ret

tgt2:
        srliw   a1, a0, 31
        add     a1, a1, a0
        sraiw   a1, a1, 1
        addw    a0, a0, a1
        ret

Kmeakin avatar Jul 16 '26 00:07 Kmeakin

Hi, I've started working on this issue. Could you please assign this issue to me?

iamaayushrivastava avatar Jul 16 '26 10:07 iamaayushrivastava

Hi, I've started working on this issue. Could you please assign this issue to me?

Just prepare pull request and mention it on this page.

EugeneZelenko avatar Jul 16 '26 15:07 EugeneZelenko

I will be honest, I was thinking that maybe this transform being in instcombine was a bad idea to start with, though it was easier to add there than say dagcombine.

AZero13 avatar Jul 16 '26 18:07 AZero13

This should optimize as well:

#define C 7

unsigned
src (unsigned short b)
{
  unsigned t = b;
  return (t * (C - 1)) / C;
}

unsigned
tgt (unsigned short b)
{
  unsigned t = b;
  return t - (t / C);
}

BreadTom avatar Jul 19 '26 11:07 BreadTom

This should optimize as well:

#define C 7

unsigned src (unsigned short b) { unsigned t = b; return (t * (C - 1)) / C; }

unsigned tgt (unsigned short b) { unsigned t = b; return t - (t / C); }

https://godbolt.org/z/6EW1K3xKo

src:
  addl %edi, %edi
  leal (%rdi,%rdi,2), %eax
  imulq $613566757, %rax, %rax
  shrq $32, %rax
  retq

tgt:
  movl %edi, %eax
  imull $9363, %edi, %ecx
  shrl $16, %ecx
  movl %edi, %edx
  subl %ecx, %edx
  movzwl %dx, %edx
  shrl %edx
  addl %ecx, %edx
  shrl $2, %edx
  subl %edx, %eax
  retq

Andarwinux avatar Jul 19 '26 13:07 Andarwinux