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

            Bug ID: 116023
           Summary: Failure to optimize (x+x)*(y+y) to (x*y)*4 when
                    intermediate result is cast to larger type
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

int32_t mulx32(int32_t x, int32_t y)
{
    uint64_t r1 = (uint64_t)(x + x) * (uint64_t)(y + y);
    return r1;
}

This can be optimized to `return (x * y) * 4`. When compiling with `-O3`, this
transformation is done by LLVM, but not by GCC.

This seems related to the 64-bit casts and the use of a temporary variable, as
either changing the code to `return (uint64_t)((uint64_t)(x + x) * (uint64_t)(y
+ y))` or replacing all occurrences of `uint64_t` with `uint32_t` makes it so
the function is appropriately optimized.

Reply via email to