https://gcc.gnu.org/g:85ff4494247018528bae0275970c7acb0f6df443

commit r17-6570-g85ff4494247018528bae0275970c7acb0f6df443
Author: Guo Jie <[email protected]>
Date:   Sun Nov 8 14:30:15 2026 +0800

    LoongArch: Fix sign-extension mode in heterogeneous widen add/sub/mul
    
    The heterogeneous even/odd widening templates combine a zero-extended
    operand with a sign-extended operand and produce a <WVEC_HALF> result.
    The zero-extended operand correctly uses <WVEC_HALF>, but the
    sign-extended operand in simd_<optab>w_evod_<mode>_hetero incorrectly
    uses <WVEC>, i.e. the mode of the un-widened source vector.
    
    For a 64-bit element input such as V2DI, the malformed RTL is:
    
      (plus:V1TI
        (zero_extend:V1TI (vec_select:V2DI ...))
        (sign_extend:V2TI (vec_select:V2DI ...)))
    
    or the corresponding minus or mult expression.  The sign extension must
    produce V1TI, not V2TI.  The malformed RTL can assert in CSE while
    folding constant operands, or later in combine's simplify_subreg.
    
    gcc/ChangeLog:
    
            * config/loongarch/simd.md
            (simd_<optab>w_evod_<mode>_hetero): Use <WVEC_HALF> instead of
            <WVEC> for the result of the sign extension.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/loongarch/vector/lasx/hetero-widen-const.c: New test.
            * gcc.target/loongarch/vector/lsx/hetero-widen-const.c: New test.

Diff:
---
 gcc/config/loongarch/simd.md                                |  2 +-
 .../gcc.target/loongarch/vector/lasx/hetero-widen-const.c   | 13 +++++++++++++
 .../gcc.target/loongarch/vector/lsx/hetero-widen-const.c    | 13 +++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/gcc/config/loongarch/simd.md b/gcc/config/loongarch/simd.md
index 09e746c56e7d..83c93ad8b54b 100644
--- a/gcc/config/loongarch/simd.md
+++ b/gcc/config/loongarch/simd.md
@@ -743,7 +743,7 @@
            (vec_select:<VEC_HALF>
              (match_operand:IVEC 1 "register_operand" "f")
              (match_operand:IVEC 3 "vect_par_cnst_even_or_odd_half")))
-         (sign_extend:<WVEC>
+         (sign_extend:<WVEC_HALF>
            (vec_select:<VEC_HALF>
              (match_operand:IVEC 2 "register_operand" "f")
              (match_dup 3)))))]
diff --git 
a/gcc/testsuite/gcc.target/loongarch/vector/lasx/hetero-widen-const.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lasx/hetero-widen-const.c
new file mode 100644
index 000000000000..15621f1886e7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lasx/hetero-widen-const.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mlasx" } */
+
+#include <lasxintrin.h>
+
+v4i64
+test_xvaddwod_q_du_d (void)
+{
+  v4u64 a = { 0, 0, 0, 0 };
+  v4i64 b = { -1, -1, -1, -1 };
+
+  return __builtin_lasx_xvaddwod_q_du_d (a, b);
+}
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lsx/hetero-widen-const.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lsx/hetero-widen-const.c
new file mode 100644
index 000000000000..be9712a31525
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lsx/hetero-widen-const.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mlsx" } */
+
+#include <lsxintrin.h>
+
+v2i64
+test_vaddwod_q_du_d (void)
+{
+  v2u64 a = { 0, 0 };
+  v2i64 b = { -1, -1 };
+
+  return __builtin_lsx_vaddwod_q_du_d (a, b);
+}

Reply via email to