This patch has been merged in GCC-14 but it needs to be added into GCC-13 as 
well. 

Missing boolean_expression TARGET_ZMMUL in riscv_rtx_costs() cause different 
instructions when
multiplying an integer with a constant. ( 
https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1482 )

int foo(int *ib) {
    *ib = *ib * 33938;
    return 0;
}

rv64im:
        lw      a4,0(a1)
        li      a5,32768
        addiw   a5,a5,1170
        mulw    a5,a5,a4
        sw      a5,0(a1)
        ret

rv64i_zmmul:
        lw      a4,0(a1)
        slliw   a5,a4,5
        addw    a5,a5,a4
        slliw   a5,a5,3
        addw    a5,a5,a4
        slliw   a5,a5,3
        addw    a5,a5,a4
        slliw   a5,a5,3
        addw    a5,a5,a4
        slliw   a5,a5,1
        sw      a5,0(a1)
        ret

Fixed.

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_rtx_costs): Add TARGET_ZMMUL.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/zmmul-3.c: New test.

(cherry picked from commit 06bb125521dec5648b725ddee4345b00decfdc77)
---
 gcc/config/riscv/riscv.cc                | 2 +-
 gcc/testsuite/gcc.target/riscv/zmmul-3.c | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/zmmul-3.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index ca3eae2abf3..a3d32c05d3c 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -2643,7 +2643,7 @@ riscv_rtx_costs (rtx x, machine_mode mode, int 
outer_code, int opno ATTRIBUTE_UN
     case MULT:
       if (float_mode_p)
        *total = tune_param->fp_mul[mode == DFmode];
-      else if (!TARGET_MUL)
+      else if (!(TARGET_MUL || TARGET_ZMMUL))
        /* Estimate the cost of a library call.  */
        *total = COSTS_N_INSNS (speed ? 32 : 6);
       else if (GET_MODE_SIZE (mode).to_constant () > UNITS_PER_WORD)
diff --git a/gcc/testsuite/gcc.target/riscv/zmmul-3.c 
b/gcc/testsuite/gcc.target/riscv/zmmul-3.c
new file mode 100644
index 00000000000..f8377f86180
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zmmul-3.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64iafdc_zmmul -mabi=lp64d" } */
+int foo1(int a)
+{
+    return a * 999999;
+}
+
+/* { dg-final { scan-assembler-times "mulw\t" 1 } } */
-- 
2.43.0

Reply via email to