https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128957
Bug ID: 128957
Summary: [14/15/16/17 Regression] [mips64] big-endian 4-byte
vector argument is zero-extended
(assign_parm_setup_block uses a logical shift and a
lowpart subreg), 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: mips64-linux-gnuabi64
Fails with trunk (gcc 17.0.0 20261123, bfd6e895d9ec) and with 6.5.0 through
16.2.0. 4.9.4 and 5.5.0 emit an arithmetic shift and work. Trunk was tested
with a mips64el-linux-gnuabi64 cross build using -EB.
Target: mips64-linux-gnuabi64 (big-endian n64 and n32).
typedef short v2hi __attribute__((vector_size (4)));
__attribute__((noipa)) int eq (v2hi v, int k) { int i; __builtin_memcpy
(&i, &v, 4); return i == k; }
__attribute__((noipa)) int lt (v2hi v, int k) { return (int) v < k; }
int main (void)
{
v2hi v = { -1, 2 };
int k;
__builtin_memcpy (&k, &v, 4);
if (!eq (v, k)) return 1;
if (lt (v, k)) return 2;
if (!lt (v, k + 1)) return 3;
return 0;
}
mips64-linux-gnuabi64-gcc -O1 -static t.c && qemu-mips64 ./a.out
Observed: exit 1 at -O1, -O2, -O3 and -Os, and with -mabi=n32. Expected: exit
0. -O0 works. GCC 6 and 7 ignore noipa; with __attribute__((noinline, noclone))
instead they also exit 1.
Godbolt (16.2.0 and 5.5.0): https://godbolt.org/z/9Y7sGMnxG
On big-endian n64 the vector is passed in the most significant half of $4. eq
extracts it with a logical shift:
eq: dsrl $4,$4,32
xor $2,$4,$5
jr $31
sltu $2,$2,1
For v = {-1, 2} this gives 0x00000000ffff0002, while k is 0xffffffffffff0002.
Cause: assign_parm_setup_block (function.cc), in the BLOCK_REG_PADDING branch
for a parameter that lives in a pseudo, does
x = expand_shift (RSHIFT_EXPR, word_mode, x, by, NULL_RTX, 1);
x = force_reg (word_mode, x);
x = gen_lowpart_SUBREG (GET_MODE (mem), x);
On MIPS64, TRULY_NOOP_TRUNCATION (SI, DI) is false, so this SImode subreg of a
logically shifted DImode value is not valid. The branch just above already uses
convert_to_mode in this case.
Possible fix: after the shift, when the integer mode of GET_MODE (mem) is
narrower than word_mode and !TRULY_NOOP_TRUNCATION_MODES_P (imode, word_mode),
use convert_to_mode (imode, x, 1) and take the lowpart of that instead of the
lowpart of the shifted word. With that change on trunk, eq uses dsra, and the
testcase passes at -O0 through -Os for -EB n64 and at -O0 through -O2 for -EB
n32 under qemu. gcc.c-torture/execute compiles to identical -O2 assembly with
-EB and with -EL.
The return-value side of the same truncation problem (copy_blkmode_to_reg in
expr.cc) is reported separately.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.