Compiling this file with -O2 optimisation: ---------------- struct str { int a, b, c, d; };
void fct3 (struct str *); extern inline struct str fct (void) { struct str returned = { 1, 2, 3, 4 }; return returned; } void fct2 (void) { struct str tmp; tmp = fct (); fct3 (&tmp); } ---------------- with this compiler: gcc version 4.0.2 20050913 (prerelease) (Debian 4.0.1-7) creates this assembler having three copies of the structure in the stack, and one as constant in .rodata: $ gcc -O2 tmp.c -S -o tmp.s $ cat tmp.s .file "tmp.c" .section .rodata .align 4 .type C.0.1141, @object .size C.0.1141, 16 C.0.1141: .long 1 .long 2 .long 3 .long 4 .text .p2align 4,,15 .globl fct2 .type fct2, @function fct2: pushl %ebp movl %esp, %ebp pushl %edi pushl %esi subl $76, %esp leal -56(%ebp), %edi movl $C.0.1141, %esi cld movl $4, %ecx rep movsl leal -24(%ebp), %edi leal -56(%ebp), %esi movb $4, %cl rep movsl leal -40(%ebp), %edi leal -24(%ebp), %esi movb $4, %cl rep movsl leal -40(%ebp), %eax pushl %eax call fct3 addl $16, %esp leal -8(%ebp), %esp popl %esi popl %edi popl %ebp ret .size fct2, .-fct2 .ident "GCC: (GNU) 4.0.2 20050913 (prerelease) (Debian 4.0.1-7)" .section .note.GNU-stack,"",@progbits ---------------- If compiled with -Os, the "memcpy" function is called three times. $ gcc -Os tmp.c -S -o tmp.s $ cat tmp.s .file "tmp.c" .section .rodata .align 4 .type C.0.1141, @object .size C.0.1141, 16 C.0.1141: .long 1 .long 2 .long 3 .long 4 .text .globl fct2 .type fct2, @function fct2: pushl %ebp movl %esp, %ebp pushl %esi pushl %ebx subl $48, %esp leal -56(%ebp), %ebx pushl $16 pushl $C.0.1141 pushl %ebx call memcpy leal -24(%ebp), %esi pushl $16 pushl %ebx pushl %esi call memcpy leal -40(%ebp), %ebx pushl $16 pushl %esi pushl %ebx call memcpy addl $36, %esp pushl %ebx call fct3 popl %eax leal -8(%ebp), %esp popl %ebx popl %esi popl %ebp ret .size fct2, .-fct2 .ident "GCC: (GNU) 4.0.2 20050913 (prerelease) (Debian 4.0.1-7)" .section .note.GNU-stack,"",@progbits ---------------- That is not a regression, gcc-3.4* and gcc-2.95 do not produce very good assembler code neither for this source file. Etienne. -- Summary: function returning structure produce very long/slow assembly Product: gcc Version: 4.0.2 Status: UNCONFIRMED Severity: enhancement Priority: P2 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: etienne_lorrain at yahoo dot fr GCC build triplet: i486-linux-gnu GCC host triplet: i486-linux-gnu GCC target triplet: i486-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24177