Register R0 is not used sometimes

Bug #1790343 reported by Harjit Singh
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Confirmed
Undecided
Unassigned

Bug Description

Please refer to https://godbolt.org/z/HP31lK 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.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) wrote :

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.

Revision history for this message
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.

Revision history for this message
Harjit Singh (zeetahhs) wrote :

Reduced the example to the bare minimum. This shows R4 being pushed and popped needlessly: https://godbolt.org/z/Y5jzx2

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) wrote :

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  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.