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

            Bug ID: 81774
           Summary: Volatile - order of reads in generated code
           Product: gcc
           Version: new-ra
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pj at hugeone dot co.uk
  Target Milestone: ---

I do not know if it and bug but it look strange to me.

Lets consider a trivial example:

int x3(int x)
{
        volatile int y = x;
        return y * y * y * y;
}

the code generated for x86-64 (7.1.0 - tested on many versions since 4.4.7)
(tested on ARM gcc up to 6.3 & AVR as well)

        mov     DWORD PTR [rsp-4], edi
        mov     eax, DWORD PTR [rsp-4]
        mov     esi, DWORD PTR [rsp-4]
        mov     ecx, DWORD PTR [rsp-4]
        mov     edx, DWORD PTR [rsp-4]
        imul    eax, esi
        imul    eax, ecx
        imul    eax, edx

IMO it reading the variable should be between the muls:
        mov     DWORD PTR [rsp-4], edi
        mov     eax, DWORD PTR [rsp-4]
        mov     esi, DWORD PTR [rsp-4]
        imul    eax, esi
        mov     ecx, DWORD PTR [rsp-4]
        imul    eax, ecx
        mov     edx, DWORD PTR [rsp-4]
        imul    eax, edx

as the stored value can change during the multiplications

Reply via email to