Register R0 is not used sometimes

Bug #1790343 reported by Harjit Singh on 2018-09-02
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain

Bug Description

Please refer to for code and compiled output.

The second column - the ARM version - is the one with the issue.

The function test() is compiled to "_Z4testv:" and it doesn't use R0. Instead, it uses R4 which results in R4 being saved and restored.

Using option -O3 doesn't change this.

Interestingly, main() which has essentially the same code does use R0 as expected.

Thanks for looking into this.

Test is missing a return statement. I think main is compiled more efficiently because it can see that the value in the ptr field is the same as the value being returned so it's happy to put ptr into r0. For test the return is missing so the compiler might be assuming that r0 should not be reused.

Best regards.

Harjit Singh (zeetahhs) wrote :

You are correct that adding a return does make test() use R0.

Changing the return value from 0, results in the new value being loaded into R0.

The x86 version don't do the equivalent of extra R4 register push/pop.

Harjit Singh (zeetahhs) wrote :

Reduced the example to the bare minimum. This shows R4 being pushed and popped needlessly:

I can reproduce with trunk GCC as well using the same flags (-std=c++17 -O3 -mcpu=cortex-m4).

Changed in gcc-arm-embedded:
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers