Missed optimization - unnecessary PUSH/POP

Bug #1720872 reported by Dan Lewis

This bug report was converted into a question: question #681994: Missed optimization - unnecessary PUSH/POP.

8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Invalid
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

Tags: optimization
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) 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

Revision history for this message
Dan Lewis (danielwlewis) wrote : Re: [Bug 1720872] Re: Missed optimization - unnecessary PUSH/POP

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
>

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) 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.

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

Dan Lewis (danielwlewis)
Changed in gcc-arm-embedded:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

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