http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50081
Bug #: 50081 Summary: Wrong code (wrong order) generated with -O2 or -Os while function return a struct Classification: Unclassified Product: gcc Version: 4.4.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: lingyouz...@arimacomm-hz.cn The code like: typedef struct { struct umdb_list_head_st *list_head; struct umdb_list_node_st *current; } umdb_util_it_t; typedef umdb_util_it_t umdb_dbms_hl_oper_list_iterator_t; umdb_dbms_hl_oper_list_iterator_t umdb_dbms_oper_list_find_element (umdb_dbms_hl_oper_list_handle_t *list, MSF_UINT8 src_mod_id, MSF_UINT16 transaction_id) { umdb_dbms_hl_oper_list_iterator_t it; umdb_dbms_oper_list_elem_t *elem; it = umdb_dbms_oper_list_get_iterator (list); while (!umdb_dbms_oper_list_equals_end (&it)) { elem = (umdb_dbms_oper_list_elem_t*)it.current; if (elem->ml_transaction_id == transaction_id && elem->src_mod_id == src_mod_id) { break; } umdb_dbms_oper_list_iterator_next (&it); } return it; } The asm code like: 00000000 <umdb_dbms_oper_list_find_element>: 0: b5f0 push {r4, r5, r6, r7, lr} 2: b087 sub sp, #28 4: ac02 add r4, sp, #8 6: 1c05 adds r5, r0, #0 8: ae04 add r6, sp, #16 a: 1c20 adds r0, r4, #0 c: 1c17 adds r7, r2, #0 e: 9301 str r3, [sp, #4] 10: f7ff fffe bl 0 <umdb_dbms_oper_list_find_element> 14: cc03 ldmia r4!, {r0, r1} 16: c603 stmia r6!, {r0, r1} 18: e00a b.n 30 <umdb_dbms_oper_list_find_element+0x30> 1a: 6863 ldr r3, [r4, #4] 1c: 9801 ldr r0, [sp, #4] 1e: 8a1a ldrh r2, [r3, #16] 20: 4282 cmp r2, r0 22: d102 bne.n 2a <umdb_dbms_oper_list_find_element+0x2a> 24: 7a1b ldrb r3, [r3, #8] 26: 42bb cmp r3, r7 28: d008 beq.n 3c <umdb_dbms_oper_list_find_element+0x3c> 2a: a804 add r0, sp, #16 2c: f7ff fffe bl 0 <umdb_dbms_oper_list_find_element> 30: ac04 add r4, sp, #16 32: 1c20 adds r0, r4, #0 34: f7ff fffe bl 0 <umdb_dbms_oper_list_find_element> 38: 2800 cmp r0, #0 3a: d0ee beq.n 1a <umdb_dbms_oper_list_find_element+0x1a> 3c: 1c2a adds r2, r5, #0 3e: ab04 add r3, sp, #16 40: b007 add sp, #28 42: cb03 ldmia r3!, {r0, r1} 44: c203 stmia r2!, {r0, r1} 46: 1c28 adds r0, r5, #0 48: bdf0 pop {r4, r5, r6, r7, pc} 4a: 46c0 nop ; (mov r8, r8) It show the return struct is not safe here, it still use the stack after sp+0x28. 40: b007 add sp, #28 42: cb03 ldmia r3!, {r0, r1} 44: c203 stmia r2!, {r0, r1}