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

            Bug ID: 113269
           Summary: X86_64 generates extra mov (and xchg) when passing
                    struct with constant value to function
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denis.campredon at gmail dot com
  Target Milestone: ---

On today's trunk, compiled with g++

---------------
typedef struct s{
    char *key, *value;
}s;

s *search(s);

s *find(char *word)
{
    return search((s){.key = word});
}

s *find2(char *word)
{
    s a = {.key = word, .value = 0};
    return search(a);
}
----------------

compiled with -O2 produces a useless mov instruction in each function :
----------------------
find(char*):
        xor     edx, edx
        mov     rsi, rdx
        jmp     search(s)
find2(char*):
        xor     edx, edx
        mov     rsi, rdx
        jmp     search(s)
----------------------

compiled with -Oz, a useless mov and two useless xchg instructions are
produced.
-----------------------
find(char*):
        xchg    rdi, rax
        xor     edx, edx
        xchg    rax, rdi
        mov     rsi, rdx
        jmp     search(s)
find2(char*):
        xchg    rdi, rax
        xor     edx, edx
        xchg    rax, rdi
        mov     rsi, rdx
        jmp     search(s)
------------------------

If the code is compiled with gcc, the mov instruction is not generated for
find2.

Reply via email to