https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103150
Bug ID: 103150 Summary: Structure return is not optimized on 32-bit targets Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: sebastian.hu...@embedded-brains.de Target Milestone: --- Consider the following test case: struct timespec { long long tv_sec; long tv_nsec; }; void a(struct timespec *); void b(struct timespec *); static inline struct timespec c(void) { struct timespec t; a(&t); return t; } void f(void) { struct timespec ts = c(); b(&ts); } For the 64-bit targets (I checked x86_64, aarch64, riscv, powerpc) this is optimized to: aarch64-rtems6-gcc -O2 -S -o - getptr.c .arch armv8-a .file "getptr.c" .text .align 2 .p2align 4,,11 .global f .type f, %function f: stp x29, x30, [sp, -32]! mov x29, sp add x0, sp, 16 bl a add x0, sp, 16 bl b ldp x29, x30, [sp], 32 ret .size f, .-f .ident "GCC: (GNU) 10.3.1 20210409 (RTEMS 6, RSB 889cf95db0122bd1a6b21598569620c40ff2069d, Newlib eb03ac1)" The pointer used to get the data from a() is directly passed to the consumer b(). However, for 32-bit targets this test case seems to be not optimized and the data is copied on the stack frame. For example: arm-rtems6-gcc -O2 -S -o - getptr.c .cpu arm7tdmi .eabi_attribute 20, 1 .eabi_attribute 21, 1 .eabi_attribute 23, 3 .eabi_attribute 24, 1 .eabi_attribute 25, 1 .eabi_attribute 26, 2 .eabi_attribute 30, 2 .eabi_attribute 34, 0 .eabi_attribute 18, 4 .file "getptr.c" .text .align 2 .global f .arch armv4t .syntax unified .arm .fpu softvfp .type f, %function f: @ Function supports interworking. @ args = 0, pretend = 0, frame = 32 @ frame_needed = 0, uses_anonymous_args = 0 push {r4, lr} sub sp, sp, #32 add r4, sp, #16 mov r0, r4 bl a mov ip, sp ldm r4, {r0, r1, r2, r3} stm ip, {r0, r1, r2, r3} mov r0, ip bl b add sp, sp, #32 @ sp needed pop {r4, lr} bx lr .size f, .-f .ident "GCC: (GNU) 10.3.1 20210409 (RTEMS 6, RSB 889cf95db0122bd1a6b21598569620c40ff2069d, Newlib eb03ac1)" riscv-rtems6-gcc -O2 -S -o - getptr.c .file "getptr.c" .option nopic .text .align 1 .globl f .type f, @function f: addi sp,sp,-48 addi a0,sp,16 sw ra,44(sp) call a lw a5,16(sp) mv a0,sp sw a5,0(sp) lw a5,20(sp) sw a5,4(sp) lw a5,24(sp) sw a5,8(sp) lw a5,28(sp) sw a5,12(sp) call b lw ra,44(sp) addi sp,sp,48 jr ra .size f, .-f .ident "GCC: (GNU) 10.3.1 20210409 (RTEMS 6, RSB 889cf95db0122bd1a6b21598569620c40ff2069d, Newlib eb03ac1)"