]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: Correct the cost of mulh.{w[u]/d[u]}
authorGuo Jie <guojie@loongson.cn>
Sat, 1 Nov 2025 07:33:06 +0000 (15:33 +0800)
committerLulu Cheng <chenglulu@loongson.cn>
Mon, 3 Nov 2025 08:09:01 +0000 (16:09 +0800)
gcc/ChangeLog:

* config/loongarch/loongarch.cc (loongarch_rtx_costs):
Correct the cost of mulh.{w[u]|d[u]}.

gcc/testsuite/ChangeLog:

* gcc.target/loongarch/mulh_wu.c: New test.

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

index c725d028d8c3275a8677bdd146a8950c52126a57..735753ad7e2d70a3879653ff5d66243368c87bea 100644 (file)
@@ -3978,9 +3978,34 @@ loongarch_rtx_costs (rtx x, machine_mode mode, int outer_code,
                                      speed);
       return true;
 
+    case LSHIFTRT:
+      /* Correct the cost of mulh.{w[u]/d[u]}.  */
+      if (outer_code == TRUNCATE && CONST_INT_P (XEXP (x, 1))
+         && INTVAL (XEXP (x, 1)) == (GET_MODE_BITSIZE (mode) / 2)
+         && GET_CODE (XEXP (x, 0)) == MULT
+         && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
+              && GET_CODE (XEXP (XEXP (x, 0), 1)) == ZERO_EXTEND)
+             || (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
+                 && GET_CODE (XEXP (XEXP (x, 0), 1)) == SIGN_EXTEND))
+         && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == REG
+         && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == REG)
+       {
+         if (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SImode
+             && GET_MODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == SImode)
+           {
+             *total = loongarch_cost->int_mult_si;
+             return true;
+           }
+         if (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == DImode
+             && GET_MODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == DImode)
+           {
+             *total = loongarch_cost->int_mult_di;
+             return true;
+           }
+       }
+      /* Fall through.  */
     case ASHIFT:
     case ASHIFTRT:
-    case LSHIFTRT:
     case ROTATE:
     case ROTATERT:
       if (CONSTANT_P (XEXP (x, 1)))
diff --git a/gcc/testsuite/gcc.target/loongarch/mulh_wu.c b/gcc/testsuite/gcc.target/loongarch/mulh_wu.c
new file mode 100644 (file)
index 0000000..53fc518
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile { target { loongarch64*-*-* } } } */
+/* { dg-options "-O3 -mabi=lp64d" } */
+/* { dg-final { scan-assembler "\tmulh.wu" } } */
+/* { dg-final { scan-assembler-not "\tlu32i.d" } } */
+
+unsigned int
+test (unsigned int *a)
+{
+  return *a / 60;
+}