Bug 42017 - gcc compiling C for ARM has stopped using r14 in leaf functions?
Summary: gcc compiling C for ARM has stopped using r14 in leaf functions?
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.4.1
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-12 12:11 UTC by Nick Pelling
Modified: 2019-05-17 22:01 UTC (History)
1 user (show)

See Also:
Host:
Target: ARM
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-11-16 09:58:48


Attachments
Test C file for which gcc -O3 should (but currently doesn't) use r14 (189 bytes, text/plain)
2009-11-14 16:45 UTC, Nick Pelling
Details
This is the assembler file generated by gcc 4.4.1 for the C test file (534 bytes, text/plain)
2009-11-14 16:48 UTC, Nick Pelling
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Pelling 2009-11-12 12:11:16 UTC
In gcc4.2.1 targeting ARM processors, pure leaf functions were able to make use of r14 / lr (the link register). However, in gcc4.3.2 and gcc4.4.1 (and presumably since then, since I can't find it mentioned in any gcc bug reports), this now uses the stack instead rather than this spare register (boo, hiss).

Here's some C test code: when compiled with the -O3 option, this uses lr on gcc4.2.1 but [sp] on gcc4.3.2 and gcc4.4.1.

int foo(int r0) {
int r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14;

r1 = 0;

for(r2=0;r2<r0;r2++)
for(r3=0;r3<r0;r3++)
for(r4=0;r4<r0;r4++)
for(r5=0;r5<r0;r5++)
for(r6=0;r6<r0;r6++)
for(r7=0;r7<r0;r7++)
for(r8=0;r8<r0;r8++)
for(r9=0;r9<r0;r9++)
for(r10=0;r10<r0;r10++)
for(r11=0;r11<r0;r11++)
for(r12=0;r12<r0;r12++)
for(r13=0;r13<r0;r13++)
for(r14=0;r14<r0;r14++)
  r1++;

return r1;
}

PS: my guess is that this might have got broken when Thumb2 and ARMv7 support was added in (round about gcc4.3)...
Comment 1 Ramana Radhakrishnan 2009-11-13 17:32:39 UTC
(In reply to comment #0)
> In gcc4.2.1 targeting ARM processors, pure leaf functions were able to make use
> of r14 / lr (the link register). However, in gcc4.3.2 and gcc4.4.1 (and
> presumably since then, since I can't find it mentioned in any gcc bug reports),
> this now uses the stack instead rather than this spare register (boo, hiss).

Can you post specifically what output you got with 4.2.1, what you get with 4.3 and what you get with 4.4 ? 

Presumably you are targetting Thumb1 but you need to make that explicit.




> 
> Here's some C test code: when compiled with the -O3 option, this uses lr on
> gcc4.2.1 but [sp] on gcc4.3.2 and gcc4.4.1.
> 
> int foo(int r0) {
> int r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14;
> 
> r1 = 0;
> 
> for(r2=0;r2<r0;r2++)
> for(r3=0;r3<r0;r3++)
> for(r4=0;r4<r0;r4++)
> for(r5=0;r5<r0;r5++)
> for(r6=0;r6<r0;r6++)
> for(r7=0;r7<r0;r7++)
> for(r8=0;r8<r0;r8++)
> for(r9=0;r9<r0;r9++)
> for(r10=0;r10<r0;r10++)
> for(r11=0;r11<r0;r11++)
> for(r12=0;r12<r0;r12++)
> for(r13=0;r13<r0;r13++)
> for(r14=0;r14<r0;r14++)
>   r1++;
> 
> return r1;
> }
> 
> PS: my guess is that this might have got broken when Thumb2 and ARMv7 support
> was added in (round about gcc4.3)...
> 

Comment 2 Nick Pelling 2009-11-14 16:45:16 UTC
Created attachment 19017 [details]
Test C file for which gcc -O3 should (but currently doesn't) use r14
Comment 3 Nick Pelling 2009-11-14 16:48:06 UTC
Created attachment 19018 [details]
This is the assembler file generated by gcc 4.4.1 for the C test file

Note the presence of "[sp, #0]" in the listing at the points where it really ought to be using the lr register (AKA r14).
Comment 4 Nick Pelling 2009-11-14 16:50:43 UTC
Actually, I am targeting ARM (not Thumb1).

I don't currently have easy access to 4.2.1 and 4.3.2, but I am assured that the former does indeed use r14 and the latter does indeed not use r14.
Comment 5 Ramana Radhakrishnan 2009-11-16 09:58:48 UTC
Confirmed. LR could have been used instead of using the stack here. 
Comment 6 Wilco 2019-05-17 22:01:53 UTC
This has been fixed since at least GCC5.4: https://www.godbolt.org/z/6IAGfh