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

            Bug ID: 128956
           Summary: [mips64] small BLKmode struct returned in $2 is not
                    sign-extended (copy_blkmode_to_reg narrows with
                    gen_lowpart), wrong code at -O1
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---
            Target: mips64el-linux-gnuabi64

Fails with a mips64el-linux-gnuabi64 cross trunk build (gcc 17.0.0 20261123,
bfd6e895d9ec) and with every tested release from 4.9.4 to 16.2.0, so it is not
a regression.
Target: mips64el-linux-gnuabi64 (n64) with -march=mips64r2, which is the Debian
default.

    /* struct { short a, b; } is BLKmode on mips64 (STRICT_ALIGNMENT,
       alignment 2 < SImode alignment) and is returned in $2.  */
    struct H { short a, b; };

    __attribute__((noipa)) struct H
    get (struct H *p)
    {
      return *p;
    }

    __attribute__((noipa)) long
    as_long (struct H *p)
    {
      struct H g = get (p);
      int v;
      __builtin_memcpy (&v, &g, sizeof v);
      return v;
    }

    int
    main (void)
    {
      struct H h = { 1, -2 };
      int w;
      __builtin_memcpy (&w, &h, sizeof w);
      if (as_long (&h) != w)
        __builtin_abort ();
      return 0;
    }

    mips64el-linux-gnuabi64-gcc -O1 -march=mips64r2 -static t.c &&
qemu-mips64el ./a.out

Observed: abort at -O1, -O2, -O3 and -Os. Expected: exit 0. -O0 works. GCC 7
and older ignore noipa; with __attribute__((noinline, noclone)) instead, 6.5.0
and 7.5.0 abort the same way, and 4.9.4 and 5.5.0 emit the same code for get.

Godbolt (16.2.0): https://godbolt.org/z/nhbr6nP9f

Trunk code for get at -O1:

    daddiu  $sp,$sp,-16
    move    $2,$0
    lhu     $3,0($4)
    dins    $2,$3,0,16
    lhu     $3,2($4)
    dins    $2,$3,16,16
    jr      $31
    daddiu  $sp,$sp,16

$2 ends up as 0x00000000fffe0001. The caller treats the SImode value in $2 as
sign-extended and returns it as a long, giving 4294836225 instead of -131071.

Cause: copy_blkmode_to_reg (expr.cc) builds the value in a word_mode pseudo
with store_bit_field and then narrows it with

      if (mode != dst_mode)
        dst = gen_lowpart (mode, dst);

On MIPS64, TRULY_NOOP_TRUNCATION (SI, DI) is false, so a lowpart of a DImode
value that is not sign-extended is not a valid SImode value. Arguments are fine
because assign_parm_setup_block uses convert_to_mode in that case.

Possible fix: in copy_blkmode_to_reg, use convert_to_mode (mode, dst, 1)
instead of gen_lowpart when mode is a scalar integer mode. With that change on
trunk, get gets an extra "sll $2,$2,0" and the testcase passes at -O0 through
-Os under qemu-mips64el. At -O2 -march=mips64r2, gcc.c-torture/execute changes
the assembly of 5 files (20040709-1/2/3, 20170401-1, pr45070), and all 5 still
pass under qemu-mips64el; the other files compile to identical assembly.

Related: PR118608 (a different missing sign extension, in store_field).

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to