public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/110533] New: [x86-64] naked with -O0 and register-passed struct/int128 clobbers parameters/callee-saved regs
@ 2023-07-03 12:35 engelke at in dot tum.de
  2023-07-03 20:17 ` [Bug target/110533] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: engelke at in dot tum.de @ 2023-07-03 12:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110533
           Summary: [x86-64] naked with -O0 and register-passed
                    struct/int128 clobbers parameters/callee-saved regs
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: engelke at in dot tum.de
  Target Milestone: ---

Compiling a naked function with a parameter that is split over multiple
registers generates several mov operations with -O0, clobbering other
parameters and callee-saved registers. This does not happen with -O1. This
happens since the introduction of naked in GCC 8, at least up to GCC 13.

Example:

    __attribute__((naked))
    void fn(__int128 a) {
        asm("ret");
    }

Compiles to; note that rbx (callee-saved) is clobbered:

    fn:
    .LFB0:
            .cfi_startproc
            movq    %rdi, %rdx
            movq    %rsi, %rax
            movq    %rcx, %rbx
            movq    %rdx, %rcx
            movq    %rax, %rbx
    #APP
    # 3 "<stdin>" 1
            ret
    # 0 "" 2
    #NO_APP
            nop
            ud2
            .cfi_endproc

With two parameters:

    __attribute__((naked))
    void fn(__int128 a, __int128 b) {
        asm("ret");
    }

Compiles to; note that rbx and the second parameter are clobbered:

    fn:
    .LFB0:
            .cfi_startproc
            movq    %rdi, %rdx
            movq    %rsi, %rax
            movq    %rcx, %rbx
            movq    %rdx, %rcx
            movq    %rax, %rbx
    #APP
    # 3 "<stdin>" 1
            ret
    # 0 "" 2
    #NO_APP
            nop
            ud2
            .cfi_endproc

With a slight modification everything works as expected:

    __attribute__((naked))
    void fn(int x, int y, __int128 a) {
        asm("ret");
    }

Compiles to:

    fn:
    .LFB0:
            .cfi_startproc
    #APP
    # 3 "<stdin>" 1
            ret
    # 0 "" 2
    #NO_APP
            nop
            ud2
            .cfi_endproc

(Above examples generated with gcc 12.2.1, but many other versions are affected
as well.)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-22 20:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-03 12:35 [Bug target/110533] New: [x86-64] naked with -O0 and register-passed struct/int128 clobbers parameters/callee-saved regs engelke at in dot tum.de
2023-07-03 20:17 ` [Bug target/110533] " pinskia at gcc dot gnu.org
2023-07-04  8:47 ` ubizjak at gmail dot com
2023-07-06 12:27 ` roger at nextmovesoftware dot com
2023-07-07 19:41 ` cvs-commit at gcc dot gnu.org
2023-07-22 20:55 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).