loongarch: fix mulsidi3_64bit instruction
authorXi Ruoyao <xry111@xry111.site>
Fri, 8 Jul 2022 13:09:25 +0000 (21:09 +0800)
committerXi Ruoyao <xry111@xry111.site>
Sun, 10 Jul 2022 03:39:45 +0000 (11:39 +0800)
(mult (sign_extend:DI rj:SI) (sign_extend:DI rk:SI)) should be
"mulw.d.w", not "mul.d".

gcc/ChangeLog:

* config/loongarch/loongarch.md (mulsidi3_64bit): Use mulw.d.w
instead of mul.d.

gcc/testsuite/ChangeLog:

* gcc.target/loongarch/mulw_d_w.c: New test.
* gcc.c-torture/execute/mul-sext.c: New test.

(cherry picked from commit 1fa42d62140b56589771eb3d46f89c810bfc8e0a)

gcc/config/loongarch/loongarch.md
gcc/testsuite/gcc.c-torture/execute/mul-sext.c [new file with mode: 0644]
gcc/testsuite/gcc.target/loongarch/mulw_d_w.c [new file with mode: 0644]

index d3c809e..8f8412f 100644 (file)
        (mult:DI (sign_extend:DI (match_operand:SI 1 "register_operand" "r"))
                 (sign_extend:DI (match_operand:SI 2 "register_operand" "r"))))]
   "TARGET_64BIT"
-  "mul.d\t%0,%1,%2"
+  "mulw.d.w\t%0,%1,%2"
   [(set_attr "type" "imul")
    (set_attr "mode" "DI")])
 
diff --git a/gcc/testsuite/gcc.c-torture/execute/mul-sext.c b/gcc/testsuite/gcc.c-torture/execute/mul-sext.c
new file mode 100644 (file)
index 0000000..8b68008
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+
+typedef __INT64_TYPE__ int64_t;
+typedef __INT32_TYPE__ int32_t;
+
+/* f() was misoptimized to a single "mul.d" instruction on LA64.  */
+__attribute__((noipa, noinline)) int64_t
+f(int64_t a, int64_t b)
+{
+  return (int64_t)(int32_t)a * (int64_t)(int32_t)b;
+}
+
+int
+main()
+{
+  int64_t a = 0x1145140000000001;
+  int64_t b = 0x1919810000000001;
+  if (f(a, b) != 1)
+    __builtin_abort();
+}
diff --git a/gcc/testsuite/gcc.target/loongarch/mulw_d_w.c b/gcc/testsuite/gcc.target/loongarch/mulw_d_w.c
new file mode 100644 (file)
index 0000000..4ab7df8
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mabi=lp64d" } */
+/* { dg-final { scan-assembler "mulw.d.w" } } */
+
+/* This should be optimized to mulw.d.w for LA64.  */
+__attribute__((noipa, noinline)) long
+f(long a, long b)
+{
+  return (long)(int)a * (long)(int)b;
+}