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

            Bug ID: 114660
           Summary: Exponentiating by squaring not performed for x * y * y
                    * y * y
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

For the following code:

int mul(int x, int y) {
    return x * y * y * y * y;
}


with -O2 GCC produces the frollowing assembly:

mul(int, int):
  mov eax, edi
  imul eax, esi
  imul eax, esi
  imul eax, esi
  imul eax, esi
  ret


However, a more optimal code could be generated with less multiplications:

mul(int, int):
        mov     eax, edi
        imul    esi, esi
        imul    eax, esi
        imul    eax, esi
        ret

Godbolt playground: https://godbolt.org/z/6dP11jPfx

Reply via email to