diff options
author | Pan Li <pan2.li@intel.com> | 2024-08-03 07:02:57 +0000 |
---|---|---|
committer | Pan Li <pan2.li@intel.com> | 2024-08-26 09:36:44 +0800 |
commit | 17be00916e51835dcc47e30ed32fc892ee0c581d (patch) | |
tree | a24f0dff60992947f741f838285d48e05fd1f1ea /gcc/config/riscv | |
parent | 8f2f7aabcef8d801af002a26885a97ccf9889099 (diff) | |
download | gcc-17be00916e51835dcc47e30ed32fc892ee0c581d.zip gcc-17be00916e51835dcc47e30ed32fc892ee0c581d.tar.gz gcc-17be00916e51835dcc47e30ed32fc892ee0c581d.tar.bz2 |
RISC-V: Support IMM for operand 0 of ussub pattern
This patch would like to allow IMM for the operand 0 of ussub pattern.
Aka .SAT_SUB(1023, y) as the below example.
Form 1:
#define DEF_SAT_U_SUB_IMM_FMT_1(T, IMM) \
T __attribute__((noinline)) \
sat_u_sub_imm##IMM##_##T##_fmt_1 (T y) \
{ \
return (T)IMM >= y ? (T)IMM - y : 0; \
}
DEF_SAT_U_SUB_IMM_FMT_1(uint64_t, 1023)
Before this patch:
10 │ sat_u_sub_imm82_uint64_t_fmt_1:
11 │ li a5,82
12 │ bgtu a0,a5,.L3
13 │ sub a0,a5,a0
14 │ ret
15 │ .L3:
16 │ li a0,0
17 │ ret
After this patch:
10 │ sat_u_sub_imm82_uint64_t_fmt_1:
11 │ li a5,82
12 │ sltu a4,a5,a0
13 │ addi a4,a4,-1
14 │ sub a0,a5,a0
15 │ and a0,a4,a0
16 │ ret
The below test suites are passed for this patch:
1. The rv64gcv fully regression test.
gcc/ChangeLog:
* config/riscv/riscv.cc (riscv_gen_unsigned_xmode_reg): Add new
func impl to gen xmode rtx reg from operand rtx.
(riscv_expand_ussub): Gen xmode reg for operand 1.
* config/riscv/riscv.md: Allow const_int for operand 1.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/sat_arith.h: Add test helper macro.
* gcc.target/riscv/sat_u_sub_imm-1.c: New test.
* gcc.target/riscv/sat_u_sub_imm-1_1.c: New test.
* gcc.target/riscv/sat_u_sub_imm-1_2.c: New test.
* gcc.target/riscv/sat_u_sub_imm-2.c: New test.
* gcc.target/riscv/sat_u_sub_imm-2_1.c: New test.
* gcc.target/riscv/sat_u_sub_imm-2_2.c: New test.
* gcc.target/riscv/sat_u_sub_imm-3.c: New test.
* gcc.target/riscv/sat_u_sub_imm-3_1.c: New test.
* gcc.target/riscv/sat_u_sub_imm-3_2.c: New test.
* gcc.target/riscv/sat_u_sub_imm-4.c: New test.
* gcc.target/riscv/sat_u_sub_imm-run-1.c: New test.
* gcc.target/riscv/sat_u_sub_imm-run-2.c: New test.
* gcc.target/riscv/sat_u_sub_imm-run-3.c: New test.
* gcc.target/riscv/sat_u_sub_imm-run-4.c: New test.
Signed-off-by: Pan Li <pan2.li@intel.com>
Diffstat (limited to 'gcc/config/riscv')
-rw-r--r-- | gcc/config/riscv/riscv.cc | 46 | ||||
-rw-r--r-- | gcc/config/riscv/riscv.md | 2 |
2 files changed, 46 insertions, 2 deletions
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 8538d40..90a6e93 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -11907,6 +11907,50 @@ riscv_expand_usadd (rtx dest, rtx x, rtx y) emit_move_insn (dest, gen_lowpart (mode, xmode_dest)); } +/* Generate a REG rtx of Xmode from the given rtx and mode. + The rtx x can be REG (QI/HI/SI/DI) or const_int. + The machine_mode mode is the original mode from define pattern. + + If rtx is REG, the gen_lowpart of Xmode will be returned. + + If rtx is const_int, a new REG rtx will be created to hold the value of + const_int and then returned. + + According to the gccint doc, the constants generated for modes with fewer + bits than in HOST_WIDE_INT must be sign extended to full width. Thus there + will be two cases here, take QImode as example. + + For .SAT_SUB (127, y) in QImode, we have (const_int 127) and one simple + mov from const_int to the new REG rtx is good enough here. + + For .SAT_SUB (254, y) in QImode, we have (const_int -2) after define_expand. + Aka 0xfffffffffffffffe in Xmode of RV64 but we actually need 0xfe in Xmode + of RV64. So we need to cleanup the highest 56 bits of the new REG rtx moved + from the (const_int -2). + + Then the underlying expanding can perform the code generation based on + the REG rtx of Xmode, instead of taking care of these in expand func. */ + +static rtx +riscv_gen_unsigned_xmode_reg (rtx x, machine_mode mode) +{ + if (!CONST_INT_P (x)) + return gen_lowpart (Xmode, x); + + rtx xmode_x = gen_reg_rtx (Xmode); + + if (mode == Xmode) + emit_move_insn (xmode_x, x); + else + { + rtx reg_x = gen_reg_rtx (mode); + emit_move_insn (reg_x, x); + riscv_emit_unary (ZERO_EXTEND, xmode_x, reg_x); + } + + return xmode_x; +} + /* Implements the unsigned saturation sub standard name usadd for int mode. z = SAT_SUB(x, y). @@ -11920,7 +11964,7 @@ void riscv_expand_ussub (rtx dest, rtx x, rtx y) { machine_mode mode = GET_MODE (dest); - rtx xmode_x = gen_lowpart (Xmode, x); + rtx xmode_x = riscv_gen_unsigned_xmode_reg (x, mode); rtx xmode_y = gen_lowpart (Xmode, y); rtx xmode_lt = gen_reg_rtx (Xmode); rtx xmode_minus = gen_reg_rtx (Xmode); diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index a9cbcca..a94705a 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -4369,7 +4369,7 @@ (define_expand "ussub<mode>3" [(match_operand:ANYI 0 "register_operand") - (match_operand:ANYI 1 "register_operand") + (match_operand:ANYI 1 "reg_or_int_operand") (match_operand:ANYI 2 "register_operand")] "" { |