https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107090

--- Comment #18 from Feng Xue <fxue at os dot amperecomputing.com> ---
LLVM adopted the same rule-based match, but this limits its capability of only
handling specific code pattern, any variant in input arguments or code would
make the matching failed. 

1. one uin64 argument comes from a constant or a uext of a uint32 value, the
resulted code sequence should be distorted after inlining expansion and
experession simplification.

mul128(a, 123u);   // uint64 a
mul128(b, c);      // uint64 b, uint32 c

2. list other code pattern of mult emulation with minor adjustment.

void multiply_uint64_to_uint128(uint64_t op0, uint64_t op1, uint64_t &res_lo,
uint64_t &res_hi)
{
  uint64_t op0_lo = op0 & 0x00000000FFFFFFFF;
  uint64_t op1_lo = op1 & 0x00000000FFFFFFFF;
  uint64_t op0_hi = op0 >> 32;
  uint64_t op1_hi = op1 >> 32;
  uint64_t mul_lo = op0_lo * op1_lo;
  uint64_t mul_mi_01 = op0_hi * op1_lo;
  uint64_t mul_mi_10 = op1_hi * op0_lo;
  uint64_t mul_hi = op0_hi * op1_hi;
  uint64_t sum_mi = mul_mi_01 + mul_mi_10;
  uint64_t sum_mi_carry = sum_mi < mul_mi_01;
  uint64_t sum_32 = (mul_lo >> 32) + (sum_mi & 0x00000000FFFFFFFF);

  res_hi = mul_hi + (sum_mi_carry << 32) + (sum_mi >> 32) + (sum_32 >> 32);
  res_lo = (sum_32 << 32) | (mul_lo & 0x00000000FFFFFFFF);
}

void Emulate64x64to128(
    const uint64_t x,
    const uint64_t y,
    uint64_t& res_lo,
    uint64_t& res_hi)
{
    const uint64_t x0 = (uint32_t)x, x1 = x >> 32;
    const uint64_t y0 = (uint32_t)y, y1 = y >> 32;
    const uint64_t p11 = x1 * y1;
    const uint64_t p01 = x0 * y1;
    const uint64_t p10 = x1 * y0;
    const uint64_t p00 = x0 * y0;

    const uint64_t middle = p10 + (p00 >> 32) + (uint32_t)p01;

    res_hi = p11 + (middle >> 32) + (p01 >> 32);
    res_lo = (middle << 32) | (uint32_t)p00;
}

std::tuple<uint64_t, uint64_t> overflow_multiply(const uint64_t a, const
uint64_t b) {
  const uint64_t a_upper_half = (a >> 32);
  const uint64_t a_lower_half = (a & ((1ULL << 32) - 1));
  const uint64_t b_upper_half = (b >> 32);
  const uint64_t b_lower_half = (b & ((1ULL << 32) - 1));
  const uint64_t p1 = (a_upper_half * b_upper_half);
  const uint64_t p2 = (a_upper_half * b_lower_half);
  const uint64_t p3 = (a_lower_half * b_upper_half);
  const uint64_t p4 = (a_lower_half * b_lower_half);
  const uint64_t p2_r32 = (p2 >> 32);
  const uint64_t p3_r32 = (p3 >> 32);
  const uint64_t p2_l32 = (p2 << 32);
  const uint64_t p3_l32 = (p3 << 32);
  const uint64_t mult_result = (p4 + p2_l32 + p3_l32);
  const uint64_t mult_carry = (
    p1 + p2_r32 + p3_r32 + 
    (mult_result < p4) +
    (p3_l32 > (UINT64_T_MAX - p2_l32))
  );
  return { mult_result, mult_carry };
}

Have not worked for this tracker, but summaried those thoughts if we hope a
more generized implementation, which have to resort to manual coding-based
matching in math-opt pass.

(In reply to Hongtao Liu from comment #16)
> Created attachment 63963 [details]
> A patch.
> 
> Add gimple pattern to match and simplify pure c implementation of 64-bit to
> 128-bit widen_mult.
> 
> A pattern match in the match.pd can optimized the instruction sequence to
> widen_mult before inline, and help inline to get a more accurate number of
> instructions size of the function.

Reply via email to