]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Fix incorrect code gen for scalar signed SAT_TRUNC [PR117688]
authorPan Li <pan2.li@intel.com>
Thu, 23 Jan 2025 06:28:39 +0000 (14:28 +0800)
committerPan Li <pan2.li@intel.com>
Wed, 29 Jan 2025 09:36:52 +0000 (17:36 +0800)
This patch would like to fix the wroing code generation for the scalar
signed SAT_TRUNC.  The input can be QI/HI/SI/DI while the alu like sub
can only work on Xmode.  Unfortunately we don't have sub/add for
non-Xmode like QImode in scalar, thus we need to sign extend to Xmode
to ensure we have the correct value before ALU like add.  The gen_lowpart
will generate something like lbu which has all zero for highest bits.

For example, when 0xff7f(-129 for HImode) trunc to QImode, we actually
want compare -129 to -128, but if there is no sign extend like lbu, we will
compare 0xff7f to 0xffffffffffffff80(assum Xmode is DImode).  Thus, we have
to sign extend 0xff(Qmode) to 0xffffffffffffff7f(assume Xmode is DImode)
before compare in Xmode.

The below test suites are passed for this patch.
* The rv64gcv fully regression test.

PR target/117688

gcc/ChangeLog:

* config/riscv/riscv.cc (riscv_expand_sstrunc): Leverage the helper
riscv_extend_to_xmode_reg with SIGN_EXTEND.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/pr117688.h: Add test helper macros.
* gcc.target/riscv/pr117688-trunc-run-1-s16-to-s8.c: New test.
* gcc.target/riscv/pr117688-trunc-run-1-s32-to-s16.c: New test.
* gcc.target/riscv/pr117688-trunc-run-1-s32-to-s8.c: New test.
* gcc.target/riscv/pr117688-trunc-run-1-s64-to-s16.c: New test.
* gcc.target/riscv/pr117688-trunc-run-1-s64-to-s32.c: New test.
* gcc.target/riscv/pr117688-trunc-run-1-s64-to-s8.c: New test.

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/config/riscv/riscv.cc
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s16-to-s8.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s16.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s8.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s16.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s32.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s8.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/pr117688.h

index 3b042e2817b85ac65cff6d90d3b405092d3280ac..439cc12f93d5dee816fc663098cf9253f1d67bec 100644 (file)
@@ -13031,7 +13031,7 @@ riscv_expand_sstrunc (rtx dest, rtx src)
   rtx xmode_narrow_min = gen_reg_rtx (Xmode);
   rtx xmode_lt = gen_reg_rtx (Xmode);
   rtx xmode_gt = gen_reg_rtx (Xmode);
-  rtx xmode_src = gen_lowpart (Xmode, src);
+  rtx xmode_src = riscv_extend_to_xmode_reg (src, GET_MODE (src), SIGN_EXTEND);
   rtx xmode_dest = gen_reg_rtx (Xmode);
   rtx xmode_mask = gen_reg_rtx (Xmode);
   rtx xmode_sat = gen_reg_rtx (Xmode);
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s16-to-s8.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s16-to-s8.c
new file mode 100644 (file)
index 0000000..df84615
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int16_t, int8_t, INT8_MIN, INT8_MAX)
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s16.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s16.c
new file mode 100644 (file)
index 0000000..1b0f860
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int32_t, int16_t, INT16_MIN, INT16_MAX)
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s8.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s32-to-s8.c
new file mode 100644 (file)
index 0000000..e412a29
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int32_t, int8_t, INT8_MIN, INT8_MAX)
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s16.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s16.c
new file mode 100644 (file)
index 0000000..234d33b
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int64_t, int16_t, INT16_MIN, INT16_MAX)
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s32.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s32.c
new file mode 100644 (file)
index 0000000..5f9e7f5
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int64_t, int32_t, INT32_MIN, INT32_MAX)
diff --git a/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s8.c b/gcc/testsuite/gcc.target/riscv/pr117688-trunc-run-1-s64-to-s8.c
new file mode 100644 (file)
index 0000000..aae6be9
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-additional-options "-std=c99" } */
+
+#include "pr117688.h"
+
+DEFINE_SIGNED_SAT_TRUNC_RUN(int64_t, int8_t, INT8_MIN, INT8_MAX)
index 3b734ce62ed22e83a50cd0b89074dc7ce99274c2..7621021016a2eec7951e4564acacd2ba2c388e57 100644 (file)
     return 0;                                               \
   }
 
+#define DEFINE_SIGNED_SAT_TRUNC_RUN(WT, NT, NT_MIN, NT_MAX) \
+  WT x;                                                     \
+  NT result;                                                \
+                                                            \
+  __attribute__ ((noipa)) void                              \
+  foo ()                                                    \
+  {                                                         \
+    NT trunc = (NT)x;                                       \
+    result = (WT)NT_MIN <= x && x <= (WT)NT_MAX             \
+       ? trunc                                              \
+       : x < 0 ? NT_MIN : NT_MAX;                           \
+  }                                                         \
+                                                            \
+  int main ()                                               \
+  {                                                         \
+    x = (WT)NT_MIN - 1 ;                                    \
+    foo();                                                  \
+    if (result != (NT)NT_MIN)                               \
+      __builtin_abort ();                                   \
+    return 0;                                               \
+  }
+
 #endif