https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100377

            Bug ID: 100377
           Summary: needless stack adjustment when passing struct in
                    register
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luc.vanoostenryck at gmail dot com
  Target Milestone: ---

Created attachment 50726
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50726&action=edit
testcases

When compiling with optimization for example -O2), the following code:

    struct sb {
        signed char a;
        char b;
        short y[3];
    };
    struct ub {
        unsigned char a;
        char b;
        short y[3];
    };
    int fsb(struct sb s) { return s.a; }
    int fub(struct ub s) { return s.a; }

produces the following assembly code on arm64:
    fsb:
        sub     sp, sp, #16
        sxtb    w0, w0
        add     sp, sp, 16
        ret
    fub:
        sub     sp, sp, #16
        and     w0, w0, 255
        add     sp, sp, 16
        ret

the following on mips64:
    fsb:
        daddiu  $sp,$sp,-16
        dsll    $2,$4,56
        dsra    $2,$2,56
        j       $31
        daddiu  $sp,$sp,16

    fub:
        daddiu  $sp,$sp,-16
        andi    $2,$4,0xff
        j       $31
        daddiu  $sp,$sp,16

the following on riscv64:
    fsb:
        addi    sp,sp,-16
        slli    a0,a0,24
        srai    a0,a0,24
        addi    sp,sp,16
        jr      ra
    fub:
        addi    sp,sp,-16
        andi    a0,a0,0xff
        addi    sp,sp,16
        jr      ra

OTOH, things seems OK on ppc64:
    fsb:
        extsb 3,3
        blr
    fub:
        rlwinm 3,3,0,0xff
        blr

and x86_64:
    fsb:
        movsx   eax, dil
        ret
    fub:
        movzx   eax, dil
        ret


Similar problems happen on 32-bit platforms too.
For example on arm32, the following code:
    struct ub32 {
        unsigned char a;
        char b;
        short y[1];
    };
    int fub32(struct ub32 s) { return s.a; }

produces:
    fub32:
        sub     sp, sp, #8
        uxtb    r0, r0
        add     sp, sp, #8
        bx      lr


All these seem to happen on all versions.
See https://gcc.godbolt.org/z/x9zc1EnYn

Note: similar PRs exist but reported for x86_64 only

Reply via email to