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

            Bug ID: 123181
           Summary: Don't optimize to push 7; pop rcx on
                    x86_64-pc-windows-*
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: blubban at gmail dot com
  Target Milestone: ---

https://learn.microsoft.com/en-us/cpp/build/stack-usage?view=msvc-170

> The stack will always be maintained 16-byte aligned, except within the
> prolog (for example, after the return address is pushed), and except
> where indicated in Function Types for a certain class of frame functions.

> If a frame function does not call another function then it is not required
> to align the stack (referenced in Section Stack Allocation).

Therefore, mov ecx,7 -> push 7; pop rcx is (typically) not a legal optimization
on x86_64-pc-windows-* targets.


void line();
void square(int);
void cube();
void tesseract()
{
    line();
    square(7);
    cube();
}


-Oz, with a mingw gcc build

Expected:

tesseract:
        sub     rsp, 40
        call    line
        mov     ecx, 7
        call    square
        nop
        add     rsp, 40
        jmp     cube


Actual:

tesseract:
        sub     rsp, 40
        call    line
        push    7
        pop     rcx
        call    square
        nop
        add     rsp, 40
        jmp     cube

https://godbolt.org/z/c1zK94ae1

(not sure what's up with the nop either, but both gcc and clang do that, so
I'll assume there's a good reason. Even if not, it's missed-optimization at
worst, not worth caring about.)

Reply via email to