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

            Bug ID: 128128
           Summary: [17 Regression] RISC-V: small struct return values are
                    stored to and reloaded from the stack at -O2 without
                    Zba/Zbb
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuki.tanaka at defaultcase dot de
  Target Milestone: ---

struct s { int x, y; };

struct s a(int x)
{
  struct s r = { x, 0 };
  return r;
}

-O2 -march=rv64gc -mabi=lp64d

Compiler Explorer (GCC 16.1 vs. 17):

GCC 16.1:
a:
        addi    sp,sp,-16
        slli    a0,a0,32
        srli    a0,a0,32
        addi    sp,sp,16
        jr      ra

GCC 17:
a:
        addi    sp,sp,-16
        sw      a0,8(sp)
        lwu     a0,8(sp)
        addi    sp,sp,16
        jr      ra

GCC 17 stores x to the stack and reloads it with lwu just to clear the upper 32
bits, where GCC 16.1 uses slli/srli. With -march=rv64gc_zba_zbb there are no
stack accesses.

Possibly related to r17-578-g900b5386f50 ("[RISC-V] Improve SI->DI zero/sign
extension patterns for RISC-V").

Reply via email to