registers pushed on stack when not needed
Hi all,
I'm compiling (g++) the following code
foo_t * bar_t::zoo(mydata_t *data){
foo_t * foo;
if(!subroutine())
foo=foo1;
else
foo=foo2;
return foo->yasubrouti
}
with these options: -c -W -Wall -Os -g -mthumb -march=armv7-m -mcpu=cortex-m3 -nostartfiles -fno-rtti -fno-exceptions -ffreestanding -ffunction-sections -fdata-sections --specs=nano.specs -Os -g
the assembly outcome is this:
0: b538 push {r3, r4, r5, lr}
2: 4604 mov r4, r0
4: f101 0008 add.w r0, r1, #8
8: 460d mov r5, r1
a: f7ff fffe bl 0 <endpoint_
e: 68a2 ldr r2, [r4, #8]
10: 6863 ldr r3, [r4, #4]
12: 4629 mov r1, r5
14: 2800 cmp r0, #0
16: bf08 it eq
18: 4613 moveq r3, r2
1a: 681a ldr r2, [r3, #0]
1c: 4618 mov r0, r3
1e: 6812 ldr r2, [r2, #0]
20: 4790 blx r2
22: bd38 pop {r3, r4, r5, pc}
When invoking "foo->yasubroutine" the subroutine arguments are in R0 and R1. I was wondering why the last instruction (pop {r3, r4, r5, pc}) cannot be done BEFORE "BLX R2" invocation.
Assuming the called subroutine will anyway save R3-R5 (just like this one is doing), couldn't the code be instead something like this?
22: bd38 pop {r3, r4, r5, pc}
20: 4790 bx r2
The result would be saving space in the task stack. Is there any optimization to do this?
Regards,
R
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask InuY4sha for more information if necessary.