From: Djordje Todorovic <[email protected]> The RISC-V psABI requires named 2 * XLEN scalar arguments and return values to use significance-based register-pair ordering: the lower-order XLEN bits are passed in the lower-numbered register, and the higher-order XLEN bits are passed in the higher-numbered register. This ordering is independent of endianness and applies to both RV32 and RV64. On big-endian targets, significance ordering differs from memory-layout ordering.
Keep anonymous variadic arguments and aggregates on the existing memory-layout ordering path, so va_arg spill layout remains correct on big-endian targets. Floating-point reals also remain on the memory-layout ordering path according to the psABI. The relevant big-endian calling-convention rules were introduced by the RISC-V ELF psABI change recently [1]. [1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/470 Co-authored-by: Eldar Osmanovic <[email protected]> Signed-off-by: Djordje Todorovic <[email protected]> Signed-off-by: Eldar Osmanovic <[email protected]> gcc/ChangeLog: * config/riscv/riscv.cc (riscv_2xlen_significance_scalar_p): New function. (riscv_pass_2xlen_scalar_gpr_pair): Likewise. (riscv_get_arg_info): Use significance-based ordering for named 2 * XLEN scalar arguments and return values. gcc/testsuite/ChangeLog: * gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate-return.c: New test. * gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate.c: New test. * gcc.target/riscv/abi/rv32be-abi-2xlen-float-incoming.c: New test. * gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-arg.c: New test. * gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-return.c: New test. * gcc.target/riscv/abi/rv64be-abi-2xlen-scalar-incoming.c: New test. * gcc.target/riscv/abi/rv64be-abi-2xlen-vararg.c: New test. --- Changes in v2: - Restrict the fix to named 2 * XLEN scalars passed or returned entirely in a GPR pair. - Drop the explicit register/stack PARALLEL representation. - Leave arguments split between the last argument GPR and the stack unchanged for a follow-up requiring middle-end support. Link to v1: https://patchwork.sourceware.org/project/gcc/patch/[email protected]/ gcc/config/riscv/riscv.cc | 55 ++++++++++++++ .../abi/rv32be-abi-2xlen-aggregate-return.c | 52 +++++++++++++ .../riscv/abi/rv32be-abi-2xlen-aggregate.c | 34 +++++++++ .../abi/rv32be-abi-2xlen-float-incoming.c | 70 +++++++++++++++++ .../riscv/abi/rv32be-abi-2xlen-scalar-arg.c | 54 +++++++++++++ .../abi/rv32be-abi-2xlen-scalar-return.c | 27 +++++++ .../abi/rv64be-abi-2xlen-scalar-incoming.c | 31 ++++++++ .../riscv/abi/rv64be-abi-2xlen-vararg.c | 76 +++++++++++++++++++ 8 files changed, 399 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate-return.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-float-incoming.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-arg.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-return.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-scalar-incoming.c create mode 100644 gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-vararg.c diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 3e0aa58d3f3..4d3ce264301 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -7240,6 +7240,54 @@ riscv_pass_fpr_pair (machine_mode mode, unsigned regno1, GEN_INT (offset2)))); } +/* Return true if MODE and TYPE describe a value that is exactly 2 * XLEN + bits wide and uses significance-based scalar ordering. Floating-point + reals, aggregates and complex values follow memory-layout ordering. */ + +static bool +riscv_2xlen_significance_scalar_p (machine_mode mode, const_tree type) +{ + if (!known_eq (GET_MODE_SIZE (mode), 2 * UNITS_PER_WORD) + || COMPLEX_MODE_P (mode) + || VECTOR_MODE_P (mode) + || mode == BLKmode) + return false; + + if (!type) + return SCALAR_INT_MODE_P (mode); + + if (AGGREGATE_TYPE_P (type) + || TREE_CODE (type) == COMPLEX_TYPE + || VECTOR_TYPE_P (type)) + return false; + + return (INTEGRAL_TYPE_P (type) + || FIXED_POINT_TYPE_P (type) + || POINTER_TYPE_P (type)); +} + +/* Pass or return a 2 * XLEN scalar in REGNO and REGNO + 1 using + significance-based ordering. On big-endian targets this differs from + memory-layout ordering: the lower-numbered register holds the low-order + XLEN bits. */ + +static rtx +riscv_pass_2xlen_scalar_gpr_pair (machine_mode mode, unsigned regno) +{ + HOST_WIDE_INT low_offset = BYTES_BIG_ENDIAN ? UNITS_PER_WORD : 0; + HOST_WIDE_INT high_offset = BYTES_BIG_ENDIAN ? 0 : UNITS_PER_WORD; + + return gen_rtx_PARALLEL + (mode, + gen_rtvec (2, + gen_rtx_EXPR_LIST (VOIDmode, + gen_rtx_REG (word_mode, regno), + GEN_INT (low_offset)), + gen_rtx_EXPR_LIST (VOIDmode, + gen_rtx_REG (word_mode, regno + 1), + GEN_INT (high_offset)))); +} + /* Return true if VLS mode MODE fits in general purpose registers per the psABI. The psABI allows aggregates up to 2 * XLEN bits to be passed in GPRs. */ @@ -7755,6 +7803,13 @@ riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum, = MIN (num_words, MAX_ARGS_IN_REGISTERS - info->gpr_offset); info->stack_p = (num_words - info->num_gprs) != 0; + if (BYTES_BIG_ENDIAN + && named + && info->num_gprs == 2 + && riscv_2xlen_significance_scalar_p (mode, type)) + return riscv_pass_2xlen_scalar_gpr_pair (mode, + gpr_base + info->gpr_offset); + if (info->num_gprs || return_p) return gen_rtx_REG (mode, gpr_base + info->gpr_offset); } diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate-return.c b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate-return.c new file mode 100644 index 00000000000..9d2cfce1a9b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate-return.c @@ -0,0 +1,52 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +typedef unsigned int u32; + +struct pair +{ + u32 high; + u32 low; +}; + +/* Aggregate returns use memory-layout ordering. On RV32BE the first field is + returned in a0 and the second field in a1. */ + +/* +** make_pair: +** ... +** li a0,1 +** li a1,2 +** ... +** jr ra +*/ +struct pair __attribute__ ((noipa)) +make_pair (void) +{ + struct pair x = { 1, 2 }; + return x; +} + +extern struct pair get_pair (void); +volatile u32 pair_sink[2]; + +/* The caller must interpret a0 as high and a1 as low. */ + +/* +** consume_pair_return: +** ... +** call get_pair(?:@plt)? +** ... +** sw a0,0\([a-z][0-9]+\) +** sw a1,4\([a-z][0-9]+\) +** ... +*/ +void __attribute__ ((noipa)) +consume_pair_return (void) +{ + struct pair x = get_pair (); + pair_sink[0] = x.high; + pair_sink[1] = x.low; +} diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate.c b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate.c new file mode 100644 index 00000000000..cc619459bb2 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-aggregate.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ + +typedef unsigned int u32; +struct pair +{ + u32 high; + u32 low; +}; + +extern void consume_pair (struct pair); + +void +call_pair (u32 high, u32 low) +{ + struct pair x = { high, low }; + consume_pair (x); +} + +volatile unsigned int sink_pair[2]; + +void +store_pair (struct pair x) +{ + sink_pair[0] = x.high; + sink_pair[1] = x.low; +} + +/* Aggregates keep memory-layout ordering: the lower-numbered register holds + the lower-addressed XLEN chunk. */ +/* { dg-final { scan-assembler-not {\mmv[ \t]+a0,a1} } } */ +/* { dg-final { scan-assembler {\msw[ \t]+a0,(sink_pair,|0\()} } } */ +/* { dg-final { scan-assembler {\msw[ \t]+a1,(sink_pair\+4|4\()} } } */ diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-float-incoming.c b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-float-incoming.c new file mode 100644 index 00000000000..17e202b96cc --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-float-incoming.c @@ -0,0 +1,70 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +typedef unsigned int u32; +typedef unsigned long long u64; + +union double_bits +{ + double value; + u64 bits; +}; + +/* Under the integer calling convention, floating-point reals are passed in + the same way as aggregates of the same size. On RV32BE, the + lower addressed high word is therefore in the lower-numbered GPR. */ + +/* +** double_pair_low: +** mv a0,a1 +** ret +*/ +u32 __attribute__ ((noipa)) +double_pair_low (double x) +{ + union double_bits b = { .value = x }; + return (u32) b.bits; +} + +/* +** double_pair_high: +** ret +*/ +u32 __attribute__ ((noipa)) +double_pair_high (double x) +{ + union double_bits b = { .value = x }; + return (u32) (b.bits >> 32); +} + +/* +** double_split_low: +** ... +** lw a0,[0-9]+\(sp\) +** ... +** jr ra +*/ +u32 __attribute__ ((noipa)) +double_split_low (u32 pad0, u32 pad1, u32 pad2, u32 pad3, + u32 pad4, u32 pad5, u32 pad6, double x) +{ + union double_bits b = { .value = x }; + return (u32) (b.bits); +} + +/* +** double_split_high: +** ... +** mv a0,a7 +** ... +** jr ra +*/ +u32 __attribute__ ((noipa)) +double_split_high (u32 pad0, u32 pad1, u32 pad2, u32 pad3, + u32 pad4, u32 pad5, u32 pad6, double x) +{ + union double_bits b = { .value = x }; + return (u32) (b.bits >> 32); +} diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-arg.c b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-arg.c new file mode 100644 index 00000000000..a47fab33636 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-arg.c @@ -0,0 +1,54 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ + +typedef unsigned int u32; +typedef unsigned long long u64; + +extern void named_scalar (u64); +extern void named_even_pair (u32, u32, u64); + +void +call_named (u32 hi, u32 lo) +{ + u64 x = ((u64) hi << 32) | lo; + named_scalar (x); +} + +void +call_named_even_pair (u32 hi, u32 lo) +{ + u64 x = ((u64) hi << 32) | lo; + named_even_pair (0, 1, x); +} + +volatile u32 sink_words[2]; +volatile u32 sink_even_pair[2]; + +void +consume_named (u64 x) +{ + sink_words[0] = (u32) x; + sink_words[1] = (u32) (x >> 32); +} + +void +consume_named_even_pair (u32 pad0, u32 pad1, + u64 x) +{ + sink_even_pair[0] = (u32) x; + sink_even_pair[1] = (u32) (x >> 32); +} + +/* Named 2 * XLEN scalars use significance ordering: a0 is the low word, + a1 is the high word, even for big-endian. */ +/* { dg-final { scan-assembler {\mmv[ \t]+a0,a1} } } */ +/* { dg-final { scan-assembler {\msw[ \t]+a0,(sink_words,|0\()} } } */ +/* { dg-final { scan-assembler {\msw[ \t]+a1,(sink_words\+4|4\()} } } */ +/* { dg-final { scan-assembler-not {\msw[ \t]+a1,(sink_words,|0\()} } } */ + +/* The same rule applies directly to even/odd register pairs used by P/Zilsd: + a2 is the low word and a3 is the high word. */ +/* { dg-final { scan-assembler {\msw[ \t]+a2,(sink_even_pair,|0\()} } } */ +/* { dg-final { scan-assembler {\msw[ \t]+a3,(sink_even_pair\+4|4\()} } } */ +/* { dg-final { scan-assembler-not {\msw[ \t]+a3,(sink_even_pair,|0\()} } } */ diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-return.c b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-return.c new file mode 100644 index 00000000000..403c348a3d1 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv32be-abi-2xlen-scalar-return.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ + +typedef unsigned int u32; +typedef unsigned long long u64; + +u64 +ret_one (void) +{ + return 1; +} + +extern u64 ret_scalar (void); + +u32 +use_ret (void) +{ + return (u32) ret_scalar (); +} + +/* Return values use significance ordering: a0 is the low word and a1 is the + high word. The caller therefore needs no move from a1 to use the low word. */ +/* { dg-final { scan-assembler {\mli[ \t]+a0,1} } } */ +/* { dg-final { scan-assembler {\mli[ \t]+a1,0} } } */ +/* { dg-final { scan-assembler-not {\mli[ \t]+a1,1} } } */ +/* { dg-final { scan-assembler-not {\mmv[ \t]+a0,a1} } } */ diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-scalar-incoming.c b/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-scalar-incoming.c new file mode 100644 index 00000000000..af6409f1bae --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-scalar-incoming.c @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +typedef unsigned long u64; +typedef unsigned __int128 u128; + +/* Verify that the 2 * XLEN rules are XLEN-generic rather than specific to + RV32. A named unsigned __int128 uses significance ordering in GPRs. */ + +/* +** pair_low: +** ret +*/ +u64 __attribute__ ((noipa)) +pair_low (u128 x) +{ + return (u64) x; +} + +/* +** pair_high: +** mv a0,a1 +** ret +*/ +u64 __attribute__ ((noipa)) +pair_high (u128 x) +{ + return (u64) (x >> 64); +} diff --git a/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-vararg.c b/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-vararg.c new file mode 100644 index 00000000000..b66bcf1094c --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/abi/rv64be-abi-2xlen-vararg.c @@ -0,0 +1,76 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64 -mbig-endian" } */ +/* { dg-skip-if "" { *-*-* } { "-flto" } } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +typedef unsigned long u64; +typedef unsigned __int128 u128; +typedef __builtin_va_list va_list; + +extern void take_var_u128 (u64, ...); + +/* A variadic u128 requires an aligned register pair. With a0 occupied by + the marker, a1 is skipped. Memory-layout ordering on RV64BE puts the high + 64 bits in a2 and the low 64 bits in a3. */ + +/* +** pass_var_u128: +** ... +** mv a2,a0 +** mv a3,a1 +** li a0,0 +** tail take_var_u128(?:@plt)? +*/ +void __attribute__ ((noipa)) +pass_var_u128 (u64 high, u64 low) +{ + u128 x = ((u128) high << 64) | low; + take_var_u128 (0, x); +} + +/* va_arg observes the same memory ordering. */ + +/* +** va_arg_u128_high: +** ... +** ld a0,0\([a-z][0-9]+\) +** ret +*/ +u64 __attribute__ ((noipa)) +va_arg_u128_high (va_list ap) +{ + u128 x = __builtin_va_arg (ap, u128); + return (u64) (x >> 64); +} + +/* +** va_arg_u128_low: +** ... +** ld a0,8\([a-z][0-9]+\) +** ret +*/ +u64 __attribute__ ((noipa)) +va_arg_u128_low (va_list ap) +{ + u128 x = __builtin_va_arg (ap, u128); + return (u64) x; +} + +/* Exercise va_start and the conversion from memory-layout ordering in the + register-save area to significance ordering for a named u128 return. */ + +u128 __attribute__ ((noipa)) +va_u128_from_registers (u64 marker, ...) +{ + va_list ap; + u128 x; + + __builtin_va_start (ap, marker); + x = __builtin_va_arg (ap, u128); + __builtin_va_end (ap); + return x; +} + +/* a1 is skipped for alignment; the value itself arrives in a2/a3. */ +/* { dg-final { scan-assembler {\msd[ \t]+a2,[0-9]+\(sp\)} } } */ +/* { dg-final { scan-assembler {\msd[ \t]+a3,[0-9]+\(sp\)} } } */ -- 2.43.0
