]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[RISC-V][PR target/119830] Fix RISC-V codegen on 32bit hosts
authorAndrew Pinski <quic_apinski@quicinc.com>
Sun, 22 Jun 2025 18:35:19 +0000 (12:35 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Thu, 14 Aug 2025 13:01:59 +0000 (07:01 -0600)
So this is Andrew's patch from the PR.  We weren't clean for a 32bit host in
some of the arithmetic for constant synthesis.

I confirmed the bug on a 32bit linux host, then confirmed that Andrew's patch
from the PR fixes the problem, then ran Andrew's patch through my tester
successfully.

Naturally I'll wait for pre-commit testing, but I'm not expecting problems.

PR target/119830
gcc/
* config/riscv/riscv.cc (riscv_build_integer_1): Make arithmetic in bclr case
clean for 32 bit hosts.

gcc/testsuite/
* gcc.target/riscv/pr119830.c: New test.

(cherry picked from commit 07c02ff39e121a496c46d3a997a25e2f46ce227e)

gcc/config/riscv/riscv.cc
gcc/testsuite/gcc.target/riscv/pr119830.c [new file with mode: 0644]

index bd0046ad2e8ffc4f0c85153db02f739c207b1d7b..031584a437a9d4bfce77e561128d4edd0ba4f684 100644 (file)
@@ -1007,16 +1007,16 @@ riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
          /* Now iterate over the bits we want to clear until the cost is
             too high or we're done.  */
          nval = value ^ HOST_WIDE_INT_C (-1);
-         nval &= HOST_WIDE_INT_C (~0x7fffffff);
+         nval &= ~HOST_WIDE_INT_C (0x7fffffff);
          while (nval && alt_cost < cost)
            {
              HOST_WIDE_INT bit = ctz_hwi (nval);
              alt_codes[alt_cost].code = AND;
-             alt_codes[alt_cost].value = ~(1UL << bit);
+             alt_codes[alt_cost].value = ~(HOST_WIDE_INT_UC (1) << bit);
              alt_codes[alt_cost].use_uw = false;
              alt_codes[alt_cost].save_temporary = false;
              alt_cost++;
-             nval &= ~(1UL << bit);
+             nval &= ~(HOST_WIDE_INT_UC (1) << bit);
            }
 
          if (nval == 0 && alt_cost <= cost)
diff --git a/gcc/testsuite/gcc.target/riscv/pr119830.c b/gcc/testsuite/gcc.target/riscv/pr119830.c
new file mode 100644 (file)
index 0000000..8c7cf3b
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbb_zbs -mabi=lp64d" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zbb_zbs -mabi=ilp32" { target { rv32 } } } */
+
+#include <stdint.h>
+void test(int32_t N, int16_t* A, int16_t val) {
+    int32_t i, j;
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            A[i * N + j] += val;
+        }
+    }
+}