GCC v10.3 use more stack size than GCC v6.3 for a function

Bug #1955060 reported by abousamra
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
New
Undecided
Unassigned

Bug Description

I have an application which I compile using arm-non-eabi-gcc v6.3, when I compile it with arm-non-eabi-gcc v10.3 I find some functions use more stack size than what GCC v6.3 uses, for example this function uses zero stack for GCC v6.3 but uses 8 bytes for GCC v10.3

int func(int *x, int y, int z, int a)
{
    switch (y) {
        case 1:
            *x = a;
            y = z;
            return y;
    }
    return 77;
}

int main(
    int aArgsCount, /**< [in] Number of arguments.*/
    char** aArgsPtr /**< [in] Arguments array.*/)
{
    int ret = 0;
    int x = 7, y = 8, z =10;

    func(&ret, x, y, z);

    return 0;
}

GCC v6.3 assembly:

   int func(int *x, int y, int z, int a)
{
   switch (y) {
   0: 2901 cmp r1, #1
   2: d102 bne.n a <func+0xa>
       case 1:
           *x = a;
   4: 6003 str r3, [r0, #0]
           y = z;
           return y;
   6: 0010 movs r0, r2
   }

   return 77;
}
   8: 4770 bx lr
   return 77;
   a: 204d movs r0, #77 ; 0x4d
   c: e7fc b.n 8 <func+0x8>

and GCC v10.3 assembly:

int func(int *x, int y, int z, int a)
{
   0: b510 push {r4, lr}
   2: 0004 movs r4, r0
           *x = a;
           y = z;
           return y;
   }

   return 77;
   4: 204d movs r0, #77 ; 0x4d
   switch (y) {
   6: 2901 cmp r1, #1
   8: d101 bne.n e <func+0xe>
           return y;
   a: 0010 movs r0, r2
           *x = a;
   c: 6023 str r3, [r4, #0]
}
   e: bd10 pop {r4, pc}

In GCCv10.3, it pushes {r4, lr} which is not the case in GCC v6.3

so why does this happen?, it costs the application more stack area over the old compiler, and how to avoid it to get less stack size
also, why it stacked the lr although it's a leaf function?

Notes:

1. This function is a dummy one just to reproduce the issue, so don't consider rewriting it.
2. How to build:

arm-none-eabi-gcc -c -std=c99 -fmessage-length=0 -fomit-frame-pointer -Wno-aggressive-loop-optimizations -fno-strict-aliasing -fshort-wchar -mfix-cortex-m3-ldrd -gdwarf-3 -gstrict-dwarf -mabi=aapcs -mthumb -mcpu=Cortex-M0 -DQSPI_IMAGE -g3 -Os -mthumb -ffunction-sections -fdata-sections -o "main.o" "main.c"

arm-none-eabi-objdump -S main.o

3. arm-none-eabi-gcc is downloaded as binaries not locally built.
4. Host os: Ubuntu 16.04 on virtualbox, linux kernel 5.11.0-41-generic.

Revision history for this message
abousamra (my-abousamra) wrote :

main.c attached

Revision history for this message
abousamra (my-abousamra) wrote :

This issue appeared between gcc 7 and gcc 8, please have a look on this disassembly over different gcc versions.
https://godbolt.org/z/4Y97o8PKK

Source: a comment from a question linked to this bug

Revision history for this message
abousamra (my-abousamra) wrote :

Any update?

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

Other bug subscribers

Related questions

Bug attachments

Remote bug watches

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