Missed optimization - unnecessary PUSH/POP

Bug #1720872 reported by Dan Lewis on 2017-10-02
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU ARM Embedded Toolchain
Undecided
Unassigned

Bug Description

I found two examples of where gcc -O3 produces a PUSH / POP sequence that unnecessarily includes register R3. According to the ARM Procedure Call Standard, register R3 does not need to be preserved by functions.

An example of the C source code and the resulting ARM assembly can be found here:

     https://godbolt.org/g/UZFt9K

This is with version 5.4 of gcc and using the following command line options:

     -O3 -march=armv7-m -mtune=cortex-m4 -mthumb

Hi,

GCC often pushes r3 to ensure that the stack is 8byte aligned at function call boundary. If an odd number of register needs to be saved, GCC will also push r3 to make it even and thus keep the stack pointer 8byte aligned.

Let me know if this is what happens for you.

Best regards,

Thomas

Hi Thomas,

Yes, both examples that I found push an even number of registers on entry
to the function. However, I don't understand why this is necessary. My
understanding was that the stack in the 32-bit Cortex-M4 need only be
aligned to a mod four address. Is this in gcc because of some 64-bit cpu's?

Thanks!
Dan

On Wed, Oct 4, 2017 at 1:32 AM, Thomas Preud'homme <
<email address hidden>> wrote:

> Hi,
>
> GCC often pushes r3 to ensure that the stack is 8byte aligned at
> function call boundary. If an odd number of register needs to be saved,
> GCC will also push r3 to make it even and thus keep the stack pointer
> 8byte aligned.
>
> Let me know if this is what happens for you.
>
> Best regards,
>
> Thomas
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1720872
>
> Title:
> Missed optimization - unnecessary PUSH/POP
>
> Status in GNU ARM Embedded Toolchain:
> New
>
> Bug description:
> I found two examples of where gcc -O3 produces a PUSH / POP sequence
> that unnecessarily includes register R3. According to the ARM
> Procedure Call Standard, register R3 does not need to be preserved by
> functions.
>
> An example of the C source code and the resulting ARM assembly can be
> found here:
>
> https://godbolt.org/g/UZFt9K
>
> This is with version 5.4 of gcc and using the following command line
> options:
>
> -O3 -march=armv7-m -mtune=cortex-m4 -mthumb
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/gcc-arm-embedded/+bug/1720872/+subscriptions
>

Hi Dan,

This is mandated by the aarch32 ABI [1]. A function call across different source file could involve 2 compilers and these need to agree on what to expect (where are arguments, etc.). The ABI for aarch32 mandates 64bit alignment at (public) function call boundary.

[1] http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf

Best regards.

Dan Lewis (danielwlewis) wrote :

Thanks, Thomas! I learn something new everyday! :-)

Dan

On Wed, Oct 4, 2017 at 8:13 AM, Thomas Preud'homme <
<email address hidden>> wrote:

> Hi Dan,
>
> This is mandated by the aarch32 ABI [1]. A function call across
> different source file could involve 2 compilers and these need to agree
> on what to expect (where are arguments, etc.). The ABI for aarch32
> mandates 64bit alignment at (public) function call boundary.
>
> [1]
> http://infocenter.arm.com/help/topic/com.arm.doc.
> ihi0042f/IHI0042F_aapcs.pdf
>
> Best regards.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1720872
>
> Title:
> Missed optimization - unnecessary PUSH/POP
>
> Status in GNU ARM Embedded Toolchain:
> New
>
> Bug description:
> I found two examples of where gcc -O3 produces a PUSH / POP sequence
> that unnecessarily includes register R3. According to the ARM
> Procedure Call Standard, register R3 does not need to be preserved by
> functions.
>
> An example of the C source code and the resulting ARM assembly can be
> found here:
>
> https://godbolt.org/g/UZFt9K
>
> This is with version 5.4 of gcc and using the following command line
> options:
>
> -O3 -march=armv7-m -mtune=cortex-m4 -mthumb
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/gcc-arm-embedded/+bug/1720872/+subscriptions
>

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers