The loongarch_expand_vector_extract helper contained some logic to
eliminate the extension but it seems not working in many cases, as
expand pass is too early (we don't even know if we are extracting into
GR or FR).  Rewrite the vector extract machine description, add an insn
to describe it.

Also incorperate Zhao Zhou's vector width reduction.

gcc/

        * config/loongarch/lsx.md (indeximm): Moving to simd.md.
        (vec_extract<mode><unitmode>): Remove.
        (lsx_vpickve2gr_<lsxfmt><u>): Remove.
        (lsx_vpickve2gr_du): Remove.
        (lsx_vpickve2gr_<lsxfmt_f>): Remove.
        * config/loongarch/lasx.md (lasx_xvpickve2gr_d<u>): Remove.
        (vec_extract<mode><unitmode>): Remove.
        (lasx_xvpickve2gr_w<u>): Remove.
        * config/loongarch/simd.md (indeximm): Moved from lsx.md,
        generalized for LASX.
        (pickve_idx): New define_mode_attr.
        (*vec_extract_ext): New define_insn.
        (*vec_extract<mode><unitmode>): New define_insn.
        (vec_extract<mode><unitmode>): New define_expand.
        (<simd_isa>_<x>vpickve2gr_<simdfmt><u><_f>): New define_expand.
        * config/loongarch/loongarch-builtins.cc: Adjust
        vpickve2gr_{b,h}{,u} return type.
        * config/loongarch/loongarch-ftypes.def: Likewise.
        * config/loongarch/loongarch-protos.h
        (loongarch_expand_vector_extract): Remove.
        * config/loongarch/loongarch.cc
        (loongarch_expand_vector_extract): Remove.

gcc/testsuite/

        * gcc.target/loongarch/fixuns-lsx.c (dg-final): Check against
        redundant sign extension.
        * gcc.target/loongarch/vector/lasx/lasx-builtin.c: Adjust test.
        * gcc.target/loongarch/vector/lasx/vect-extract-256-128.c:
        Adjust test.
        * gcc.target/loongarch/vector/lsx/lsx-builtin.c: Adjust test.
        * gcc.target/loongarch/vector/lasx/lasx-xvpickve2gr-1.c:
        New test.
        * gcc.target/loongarch/vector/lsx/lsx-vpickve2gr-1.c: New test.

Co-authored-by: Zhao Zhou <[email protected]>
---
 gcc/config/loongarch/lasx.md                  |  34 ----
 gcc/config/loongarch/loongarch-builtins.cc    |   8 +-
 gcc/config/loongarch/loongarch-ftypes.def     |  10 +-
 gcc/config/loongarch/loongarch-protos.h       |   1 -
 gcc/config/loongarch/loongarch.cc             |  74 ---------
 gcc/config/loongarch/lsx.md                   |  91 -----------
 gcc/config/loongarch/simd.md                  | 149 ++++++++++++++++++
 .../gcc.target/loongarch/fixuns-lsx.c         |   1 +
 .../loongarch/vector/lasx/lasx-builtin.c      |  10 +-
 .../vector/lasx/lasx-xvpickve2gr-1.c          |  50 ++++++
 .../vector/lasx/vect-extract-256-128.c        |  24 +--
 .../loongarch/vector/lsx/lsx-builtin.c        |   4 +-
 .../loongarch/vector/lsx/lsx-vpickve2gr-1.c   |  34 ++++
 13 files changed, 262 insertions(+), 228 deletions(-)
 create mode 100644 
gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-xvpickve2gr-1.c
 create mode 100644 
gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-vpickve2gr-1.c

diff --git a/gcc/config/loongarch/lasx.md b/gcc/config/loongarch/lasx.md
index 84b763f7b75..58872171170 100644
--- a/gcc/config/loongarch/lasx.md
+++ b/gcc/config/loongarch/lasx.md
@@ -565,17 +565,6 @@ (define_insn "lasx_vecinit_merge_<LASX:mode>"
   [(set_attr "type" "simd_splat")
    (set_attr "mode" "<MODE>")])
 
-(define_insn "lasx_xvpickve2gr_d<u>"
-  [(set (match_operand:DI 0 "register_operand" "=r")
-       (any_extend:DI
-         (vec_select:DI
-           (match_operand:V4DI 1 "register_operand" "f")
-           (parallel [(match_operand 2 "const_0_to_3_operand" "")]))))]
-  "ISA_HAS_LASX"
-  "xvpickve2gr.d<u>\t%0,%u1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "V4DI")])
-
 (define_expand "vec_set<mode>"
   [(match_operand:ILASX_DW 0 "register_operand")
    (match_operand:<UNITMODE> 1 "reg_or_0_operand")
@@ -627,17 +616,6 @@ (define_expand "vec_set<mode>"
   DONE;
 })
 
-(define_expand "vec_extract<mode><unitmode>"
-  [(match_operand:<UNITMODE> 0 "register_operand")
-   (match_operand:LASX 1 "register_operand")
-   (match_operand 2 "const_<indeximm256>_operand")]
-  "ISA_HAS_LASX"
-{
-  loongarch_expand_vector_extract (operands[0], operands[1],
-      INTVAL (operands[2]));
-  DONE;
-})
-
 (define_insn_and_split "vec_extract<mode>_0"
   [(set (match_operand:<UNITMODE> 0 "register_operand" "=f")
         (vec_select:<UNITMODE>
@@ -3085,18 +3063,6 @@ (define_insn "lasx_xvreplve_<lasxfmt_f>"
   [(set_attr "type" "simd_splat")
    (set_attr "mode" "<MODE>")])
 
-(define_insn "lasx_xvpickve2gr_w<u>"
-  [(set (match_operand:SI 0 "register_operand" "=r")
-       (any_extend:SI
-         (vec_select:SI
-           (match_operand:V8SI 1 "register_operand" "f")
-           (parallel [(match_operand 2 "const_0_to_7_operand" "")]))))]
-  "ISA_HAS_LASX"
-  "xvpickve2gr.w<u>\t%0,%u1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "V8SI")])
-
-
 (define_insn "lasx_xvmskgez_b"
   [(set (match_operand:V32QI 0 "register_operand" "=f")
        (unspec:V32QI [(match_operand:V32QI 1 "register_operand" "f")]
diff --git a/gcc/config/loongarch/loongarch-builtins.cc 
b/gcc/config/loongarch/loongarch-builtins.cc
index 3774f1b81d7..d4924e9cfb5 100644
--- a/gcc/config/loongarch/loongarch-builtins.cc
+++ b/gcc/config/loongarch/loongarch-builtins.cc
@@ -1262,12 +1262,12 @@ static const struct loongarch_builtin_description 
loongarch_builtins[] = {
   LSX_BUILTIN (vclz_h, LARCH_V8HI_FTYPE_V8HI),
   LSX_BUILTIN (vclz_w, LARCH_V4SI_FTYPE_V4SI),
   LSX_BUILTIN (vclz_d, LARCH_V2DI_FTYPE_V2DI),
-  LSX_BUILTIN (vpickve2gr_b, LARCH_SI_FTYPE_V16QI_UQI),
-  LSX_BUILTIN (vpickve2gr_h, LARCH_SI_FTYPE_V8HI_UQI),
+  LSX_BUILTIN (vpickve2gr_b, LARCH_QI_FTYPE_V16QI_UQI),
+  LSX_BUILTIN (vpickve2gr_h, LARCH_HI_FTYPE_V8HI_UQI),
   LSX_BUILTIN (vpickve2gr_w, LARCH_SI_FTYPE_V4SI_UQI),
   LSX_BUILTIN (vpickve2gr_d, LARCH_DI_FTYPE_V2DI_UQI),
-  LSX_BUILTIN (vpickve2gr_bu, LARCH_USI_FTYPE_V16QI_UQI),
-  LSX_BUILTIN (vpickve2gr_hu, LARCH_USI_FTYPE_V8HI_UQI),
+  LSX_BUILTIN (vpickve2gr_bu, LARCH_UQI_FTYPE_V16QI_UQI),
+  LSX_BUILTIN (vpickve2gr_hu, LARCH_UHI_FTYPE_V8HI_UQI),
   LSX_BUILTIN (vpickve2gr_wu, LARCH_USI_FTYPE_V4SI_UQI),
   LSX_BUILTIN (vpickve2gr_du, LARCH_UDI_FTYPE_V2DI_UQI),
   LSX_BUILTIN (vinsgr2vr_b, LARCH_V16QI_FTYPE_V16QI_SI_UQI),
diff --git a/gcc/config/loongarch/loongarch-ftypes.def 
b/gcc/config/loongarch/loongarch-ftypes.def
index b33f5079683..700ccff9ca8 100644
--- a/gcc/config/loongarch/loongarch-ftypes.def
+++ b/gcc/config/loongarch/loongarch-ftypes.def
@@ -125,15 +125,15 @@ DEF_LARCH_FTYPE (1, (SI, UV4SI))
 DEF_LARCH_FTYPE (1, (SI, UV8SI))
 DEF_LARCH_FTYPE (1, (SI, UV8HI))
 DEF_LARCH_FTYPE (1, (SI, UV16HI))
-DEF_LARCH_FTYPE (2, (SI, V16QI, UQI))
-DEF_LARCH_FTYPE (2, (SI, V32QI, UQI))
+DEF_LARCH_FTYPE (2, (QI, V16QI, UQI))
+DEF_LARCH_FTYPE (2, (QI, V32QI, UQI))
 DEF_LARCH_FTYPE (1, (SI, V2HI))
 DEF_LARCH_FTYPE (2, (SI, V2HI, V2HI))
 DEF_LARCH_FTYPE (1, (SI, V4QI))
 DEF_LARCH_FTYPE (2, (SI, V4QI, V4QI))
 DEF_LARCH_FTYPE (2, (SI, V4SI, UQI))
 DEF_LARCH_FTYPE (2, (SI, V8SI, UQI))
-DEF_LARCH_FTYPE (2, (SI, V8HI, UQI))
+DEF_LARCH_FTYPE (2, (HI, V8HI, UQI))
 DEF_LARCH_FTYPE (1, (SI, VOID))
 
 DEF_LARCH_FTYPE (2, (UDI, UDI, UDI))
@@ -144,9 +144,9 @@ DEF_LARCH_FTYPE (2, (UDI, V2DI, UQI))
 DEF_LARCH_FTYPE (2, (USI, V16HI, UQI))
 DEF_LARCH_FTYPE (2, (UDI, V4DI, UQI))
 
-DEF_LARCH_FTYPE (2, (USI, V16QI, UQI))
+DEF_LARCH_FTYPE (2, (UQI, V16QI, UQI))
 DEF_LARCH_FTYPE (2, (USI, V4SI, UQI))
-DEF_LARCH_FTYPE (2, (USI, V8HI, UQI))
+DEF_LARCH_FTYPE (2, (UHI, V8HI, UQI))
 DEF_LARCH_FTYPE (1, (USI, VOID))
 
 DEF_LARCH_FTYPE (2, (UV16QI, UV16QI, UQI))
diff --git a/gcc/config/loongarch/loongarch-protos.h 
b/gcc/config/loongarch/loongarch-protos.h
index 52aec1fd1b7..44bfee53364 100644
--- a/gcc/config/loongarch/loongarch-protos.h
+++ b/gcc/config/loongarch/loongarch-protos.h
@@ -180,7 +180,6 @@ extern void loongarch_expand_vector_group_init (rtx, rtx);
 extern void loongarch_expand_vector_init (rtx, rtx);
 extern void loongarch_expand_vec_unpack (rtx op[2], bool);
 extern void loongarch_expand_vec_perm_1 (rtx[]);
-extern void loongarch_expand_vector_extract (rtx, rtx, int);
 extern void loongarch_expand_vector_reduc (rtx (*)(rtx, rtx, rtx), rtx, rtx);
 
 extern int loongarch_ldst_scaled_shift (machine_mode);
diff --git a/gcc/config/loongarch/loongarch.cc 
b/gcc/config/loongarch/loongarch.cc
index 7789b0b964c..f16366124aa 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -10092,80 +10092,6 @@ loongarch_sched_reassociation_width (tree_code opc, 
machine_mode mode)
   return loongarch_cpu_sched_reassociation_width (&la_target, opc, mode);
 }
 
-/* Implement extract a scalar element from vector register */
-
-void
-loongarch_expand_vector_extract (rtx target, rtx vec, int elt)
-{
-  machine_mode mode = GET_MODE (vec);
-  machine_mode inner_mode = GET_MODE_INNER (mode);
-  rtx tmp;
-
-  switch (mode)
-    {
-    case E_V8HImode:
-    case E_V16QImode:
-      break;
-
-    case E_V32QImode:
-      if (ISA_HAS_LASX)
-       {
-         if (elt >= 16)
-           {
-             tmp = gen_reg_rtx (V32QImode);
-             emit_insn (gen_lasx_xvpermi_d_v32qi (tmp, vec, GEN_INT (0xe)));
-             loongarch_expand_vector_extract (target,
-                                              gen_lowpart (V16QImode, tmp),
-                                              elt & 15);
-           }
-         else
-           loongarch_expand_vector_extract (target,
-                                            gen_lowpart (V16QImode, vec),
-                                            elt & 15);
-         return;
-       }
-      break;
-
-    case E_V16HImode:
-      if (ISA_HAS_LASX)
-       {
-         if (elt >= 8)
-           {
-             tmp = gen_reg_rtx (V16HImode);
-             emit_insn (gen_lasx_xvpermi_d_v16hi (tmp, vec, GEN_INT (0xe)));
-             loongarch_expand_vector_extract (target,
-                                              gen_lowpart (V8HImode, tmp),
-                                              elt & 7);
-           }
-         else
-           loongarch_expand_vector_extract (target,
-                                            gen_lowpart (V8HImode, vec),
-                                            elt & 7);
-         return;
-       }
-      break;
-
-    default:
-      break;
-    }
-
-  tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
-  tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
-
-  /* Let the rtl optimizers know about the zero extension performed.  */
-  if (inner_mode == QImode || inner_mode == HImode)
-    {
-      tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
-      target = gen_lowpart (SImode, target);
-    }
-  if (inner_mode == SImode || inner_mode == DImode)
-    {
-      tmp = gen_rtx_SIGN_EXTEND (inner_mode, tmp);
-    }
-
-  emit_insn (gen_rtx_SET (target, tmp));
-}
-
 /* Generate code to copy vector bits i / 2 ... i - 1 from vector SRC
    to bits 0 ... i / 2 - 1 of vector DEST, which has the same mode.
    The upper bits of DEST are undefined, though they shouldn't cause
diff --git a/gcc/config/loongarch/lsx.md b/gcc/config/loongarch/lsx.md
index 015cebedac8..c30cd47cf87 100644
--- a/gcc/config/loongarch/lsx.md
+++ b/gcc/config/loongarch/lsx.md
@@ -250,16 +250,6 @@ (define_mode_attr ilsxfmt_u
   [(V2DF "lu")
    (V4SF "wu")])
 
-;; This is used to form an immediate operand constraint using
-;; "const_<indeximm>_operand".
-(define_mode_attr indeximm
-  [(V2DF "0_or_1")
-   (V4SF "0_to_3")
-   (V2DI "0_or_1")
-   (V4SI "0_to_3")
-   (V8HI "uimm3")
-   (V16QI "uimm4")])
-
 ;; This attribute represents bitmask needed for vec_merge using
 ;; "const_<bitmask>_operand".
 (define_mode_attr bitmask
@@ -316,45 +306,6 @@ (define_expand "vec_unpacku_lo_<mode>"
   DONE;
 })
 
-(define_expand "vec_extract<mode><unitmode>"
-  [(match_operand:<UNITMODE> 0 "register_operand")
-   (match_operand:ILSX 1 "register_operand")
-   (match_operand 2 "const_<indeximm>_operand")]
-  "ISA_HAS_LSX"
-{
-  if (<UNITMODE>mode == QImode || <UNITMODE>mode == HImode)
-    {
-      rtx dest1 = gen_reg_rtx (SImode);
-      emit_insn (gen_lsx_vpickve2gr_<lsxfmt> (dest1, operands[1], 
operands[2]));
-      emit_move_insn (operands[0],
-                     gen_lowpart (<UNITMODE>mode, dest1));
-    }
-  else
-    emit_insn (gen_lsx_vpickve2gr_<lsxfmt> (operands[0], operands[1], 
operands[2]));
-  DONE;
-})
-
-(define_expand "vec_extract<mode><unitmode>"
-  [(match_operand:<UNITMODE> 0 "register_operand")
-   (match_operand:FLSX 1 "register_operand")
-   (match_operand 2 "const_<indeximm>_operand")]
-  "ISA_HAS_LSX"
-{
-  rtx temp;
-  HOST_WIDE_INT val = INTVAL (operands[2]);
-
-  if (val == 0)
-    temp = operands[1];
-  else
-    {
-      rtx n = GEN_INT (val * GET_MODE_SIZE (<UNITMODE>mode));
-      temp = gen_reg_rtx (<MODE>mode);
-      emit_insn (gen_lsx_vbsrl_<lsxfmt_f> (temp, operands[1], n));
-    }
-  emit_insn (gen_lsx_vec_extract_<lsxfmt_f> (operands[0], temp));
-  DONE;
-})
-
 (define_insn_and_split "lsx_vec_extract_<lsxfmt_f>"
   [(set (match_operand:<UNITMODE> 0 "register_operand" "=f")
        (vec_select:<UNITMODE>
@@ -447,48 +398,6 @@ (define_insn "lsx_vextrins_<lsxfmt_f>_scalar"
   [(set_attr "type" "simd_insert")
    (set_attr "mode" "<MODE>")])
 
-(define_insn "lsx_vpickve2gr_<lsxfmt><u>"
-  [(set (match_operand:<VRES> 0 "register_operand" "=r")
-       (any_extend:<VRES>
-         (vec_select:<UNITMODE>
-           (match_operand:ILSX_HB 1 "register_operand" "f")
-           (parallel [(match_operand 2 "const_<indeximm>_operand" "")]))))]
-  "ISA_HAS_LSX"
-  "vpickve2gr.<lsxfmt><u>\t%0,%w1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "<MODE>")])
-
-(define_insn "lsx_vpickve2gr_<lsxfmt_f><u>"
-  [(set (match_operand:<UNITMODE> 0 "register_operand" "=r")
-       (any_extend:<UNITMODE>
-         (vec_select:<UNITMODE>
-           (match_operand:LSX_W 1 "register_operand" "f")
-           (parallel [(match_operand 2 "const_<indeximm>_operand" "")]))))]
-  "ISA_HAS_LSX"
-  "vpickve2gr.<lsxfmt><u>\t%0,%w1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "<MODE>")])
-
-(define_insn "lsx_vpickve2gr_du"
-  [(set (match_operand:DI 0 "register_operand" "=r")
-       (vec_select:DI
-         (match_operand:V2DI 1 "register_operand" "f")
-         (parallel [(match_operand 2 "const_0_or_1_operand" "")])))]
-  "ISA_HAS_LSX"
-  "vpickve2gr.du\t%0,%w1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "V2DI")])
-
-(define_insn "lsx_vpickve2gr_<lsxfmt_f>"
-  [(set (match_operand:<UNITMODE> 0 "register_operand" "=r")
-       (vec_select:<UNITMODE>
-         (match_operand:LSX_D 1 "register_operand" "f")
-         (parallel [(match_operand 2 "const_<indeximm>_operand" "")])))]
-  "ISA_HAS_LSX"
-  "vpickve2gr.<lsxfmt>\t%0,%w1,%2"
-  [(set_attr "type" "simd_copy")
-   (set_attr "mode" "<MODE>")])
-
 (define_expand "neg<mode>2"
   [(set (match_operand:ILSX 0 "register_operand")
        (neg:ILSX (match_operand:ILSX 1 "register_operand")))]
diff --git a/gcc/config/loongarch/simd.md b/gcc/config/loongarch/simd.md
index 557b8a70bf4..41554543fb9 100644
--- a/gcc/config/loongarch/simd.md
+++ b/gcc/config/loongarch/simd.md
@@ -195,6 +195,34 @@ (define_mode_attr bitimm [(V16QI "uimm3") (V32QI "uimm3")
                          (V4SI  "uimm5") (V8SI "uimm5")
                          (V2DI  "uimm6") (V4DI "uimm6")])
 
+(define_mode_attr indeximm
+  [(V2DF "0_or_1")
+   (V4SF "0_to_3")
+   (V2DI "0_or_1")
+   (V4SI "0_to_3")
+   (V8HI "uimm3")
+   (V16QI "uimm4")
+   (V4DF "0_to_3")
+   (V8SF "0_to_7")
+   (V4DI "0_to_3")
+   (V8SI "0_to_7")
+   (V16HI "uimm4")
+   (V32QI "uimm5")])
+
+(define_mode_attr pickve_idx
+  [(V2DF "0_or_1")
+   (V4SF "0_to_3")
+   (V2DI "0_or_1")
+   (V4SI "0_to_3")
+   (V8HI "uimm3")
+   (V16QI "uimm4")
+   (V4DF "0_to_3")
+   (V8SF "0_to_7")
+   (V4DI "0_to_3")
+   (V8SI "0_to_7")
+   (V16HI "uimm3")
+   (V32QI "uimm4")])
+
 (define_int_attr hi_lo [(0 "lo") (1 "hi")])
 
 ;; cnv_mode
@@ -1316,6 +1344,127 @@ (define_expand "<simd_isa>_<x>vshuf_<simdfmt><_f>"
     DONE;
   })
 
+(define_insn "*vec_extract_ext"
+  [(set (match_operand:DI 0 "register_operand" "=r")
+       (any_extend:DI
+         (vec_select:<UNITMODE>
+           (match_operand:IVEC 1 "register_operand" "f")
+           (parallel [(match_operand 2 "const_<pickve_idx>_operand")]))))]
+  "<UNITMODE>mode != DImode"
+{
+  if ('<su>' == 's' && <UNITMODE>mode == SImode)
+    {
+      if (operands[2] == const0_rtx)
+       return "movfr2gr.s\t%0,%1";
+      if (operands[2] == const1_rtx)
+       return "movfrh2gr.s\t%0,%1";
+    }
+
+  if (GET_MODE_SIZE (<UNITMODE>mode) * UINTVAL (operands[2]) < 16)
+    return "vpickve2gr.<simdfmt><u>\t%0,%w1,%2";
+
+  return "xvpickve2gr.<simdfmt><u>\t%0,%u1,%2";
+}
+  [(set_attr "type" "simd_copy")
+   (set_attr "mode" "<MODE>")])
+
+(define_insn_and_split "*vec_extract<mode><unitmode>"
+  [(set (match_operand:<UNITMODE> 0 "register_operand" "=r,f")
+       (vec_select:<UNITMODE>
+         (match_operand:ALLVEC 1 "register_operand" "f,f")
+         (parallel [(match_operand 2 "const_<pickve_idx>_operand")])))]
+  ""
+{
+  int unit_size = GET_MODE_SIZE (<UNITMODE>mode);
+  if (operands[2] == const0_rtx)
+    {
+      const char *tmpl[] = {
+       "movfr2gr.s\t%0,%1",
+       "movfr2gr.d\t%0,%1",
+       "fmov.s\t%0,%1",
+       "fmov.d\t%0,%1"
+      };
+
+      return tmpl[which_alternative << 1 | (unit_size == 8)];
+    }
+
+  if (INTVAL (operands[2]) * unit_size == 4 && !which_alternative)
+    return "movfrh2gr.s\t%0,%1";
+
+  int idx = INTVAL (operands[2]);
+  if (unit_size * idx < 16)
+    {
+      if (which_alternative)
+       {
+         operands[2] = GEN_INT (idx * unit_size);
+         return "vbsrl.v\t%w0,%w1,%2";
+       }
+
+      return "vpickve2gr.<simdfmt_as_i>\t%0,%w1,%2";
+    }
+
+  gcc_checking_assert (unit_size >= 4);
+  /* Actually HI and QI are not allowed in FPR (VR) by hard_regno_mode_ok
+     so which_alternative == 1 should not happen yet.  Keep the path
+     to be future-proof.  */
+  return (which_alternative ? "xvpickve.<simdfmt_as_i>\t%u0,%u1,%2"
+                           : "xvpickve2gr.<simdfmt_as_i>\t%0,%u1,%2");
+}
+  "reload_completed
+   && REGNO (operands[0]) == REGNO (operands[1])
+   && operands[2] == const0_rtx"
+  [(const_int 0)]
+{
+  emit_note (NOTE_INSN_DELETED);
+  DONE;
+}
+  [(set_attr "type" "simd_copy,simd_shift")
+   (set_attr "mode" "<MODE>")])
+
+(define_expand "vec_extract<mode><unitmode>"
+  [(set (match_operand:<UNITMODE> 0 "register_operand")
+       (vec_select:<UNITMODE>
+         (match_operand:ALLVEC 1 "register_operand")
+         (parallel [(match_operand 2 "const_<indeximm>_operand")])))]
+  ""
+{
+  if (!const_<pickve_idx>_operand (operands[2], VOIDmode))
+    {
+      rtx t = gen_reg_rtx (<MODE>mode);
+      emit_insn (gen_lasx_xvpermi_d (<MODE>mode, t, operands[1],
+                                    GEN_INT (0xe)));
+      operands[1] = t;
+
+      rtx mask = GEN_INT (GET_MODE_NUNITS (<MODE>mode) / 2 - 1);
+      operands[2] = simplify_const_binary_operation (AND, QImode,
+                                                    operands[2], mask);
+    }
+})
+
+(define_expand "<simd_isa>_<x>vpickve2gr_<simdfmt><u><_f>"
+  [(set (match_dup 3)
+       (any_extend:DI
+         (vec_select:<UNITMODE>
+           (match_operand:IVEC 1 "register_operand")
+           (parallel [(match_operand 2 "const_<pickve_idx>_operand")]))))
+   (set (match_operand:<UNITMODE> 0 "register_operand") (match_dup 4))]
+  ""
+{
+  if (<UNITMODE>mode == DImode)
+    {
+      emit_insn (gen_vec_extract<mode><unitmode> (operands[0], operands[1],
+                                                 operands[2]));
+      DONE;
+    }
+
+  operands[3] = gen_reg_rtx (DImode);
+  operands[4] = gen_lowpart (<UNITMODE>mode, operands[3]);
+  SUBREG_PROMOTED_VAR_P (operands[4]) = 1;
+  SUBREG_PROMOTED_SET (operands[4],
+                      ('<su>' == 's' ? SRP_SIGNED : SRP_UNSIGNED));
+})
+
+
 ; The LoongArch SX Instructions.
 (include "lsx.md")
 
diff --git a/gcc/testsuite/gcc.target/loongarch/fixuns-lsx.c 
b/gcc/testsuite/gcc.target/loongarch/fixuns-lsx.c
index da9b7d8379e..6f0c33f0e8e 100644
--- a/gcc/testsuite/gcc.target/loongarch/fixuns-lsx.c
+++ b/gcc/testsuite/gcc.target/loongarch/fixuns-lsx.c
@@ -1,6 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-O2 -march=loongarch64 -mabi=lp64d -mlsx" } */
 /* { dg-final { scan-assembler-times "vftintrz.\[wl\]u" 4 } } */
+/* { dg-final { scan-assembler-not "slli.w" } } */
 
 unsigned int
 t1 (float x)
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-builtin.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-builtin.c
index b9753f5aecb..9ff5089d1a3 100644
--- a/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-builtin.c
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-builtin.c
@@ -515,7 +515,7 @@
 /* { dg-final { scan-assembler-times 
"lasx_xvpickve2gr_w:.*xvpickve2gr\\.w.*lasx_xvpickve2gr_w" 1 } } */
 /* { dg-final { scan-assembler-times 
"lasx_xvpickve2gr_wu:.*xvpickve2gr\\.wu.*lasx_xvpickve2gr_wu" 1 } } */
 /* { dg-final { scan-assembler-times 
"lasx_xvpickve2gr_d:.*xvpickve2gr\\.d.*lasx_xvpickve2gr_d" 1 } } */
-/* { dg-final { scan-assembler-times 
"lasx_xvpickve2gr_du:.*xvpickve2gr\\.du.*lasx_xvpickve2gr_du" 1 } } */
+/* { dg-final { scan-assembler-times 
"lasx_xvpickve2gr_du:.*xvpickve2gr\\.d.*lasx_xvpickve2gr_du" 1 } } */
 /* { dg-final { scan-assembler-times 
"lasx_xvaddwev_q_d:.*xvaddwev\\.q\\.d.*lasx_xvaddwev_q_d" 1 } } */
 /* { dg-final { scan-assembler-times 
"lasx_xvaddwev_d_w:.*xvaddwev\\.d\\.w.*lasx_xvaddwev_d_w" 1 } } */
 /* { dg-final { scan-assembler-times 
"lasx_xvaddwev_w_h:.*xvaddwev\\.w\\.h.*lasx_xvaddwev_w_h" 1 } } */
@@ -3336,22 +3336,22 @@ __lasx_xvldrepl_d (void *_1)
 int
 __lasx_xvpickve2gr_w (v8i32 _1)
 {
-  return __builtin_lasx_xvpickve2gr_w (_1, 1);
+  return __builtin_lasx_xvpickve2gr_w (_1, 4);
 }
 unsigned int
 __lasx_xvpickve2gr_wu (v8i32 _1)
 {
-  return __builtin_lasx_xvpickve2gr_wu (_1, 1);
+  return __builtin_lasx_xvpickve2gr_wu (_1, 4);
 }
 long
 __lasx_xvpickve2gr_d (v4i64 _1)
 {
-  return __builtin_lasx_xvpickve2gr_d (_1, 1);
+  return __builtin_lasx_xvpickve2gr_d (_1, 2);
 }
 unsigned long int
 __lasx_xvpickve2gr_du (v4i64 _1)
 {
-  return __builtin_lasx_xvpickve2gr_du (_1, 1);
+  return __builtin_lasx_xvpickve2gr_du (_1, 2);
 }
 v4i64
 __lasx_xvaddwev_q_d (v4i64 _1, v4i64 _2)
diff --git 
a/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-xvpickve2gr-1.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-xvpickve2gr-1.c
new file mode 100644
index 00000000000..bd1fa409c2b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lasx/lasx-xvpickve2gr-1.c
@@ -0,0 +1,50 @@
+
+/* { dg-do compile } */
+/* { dg-options "-mlasx -O3" } */
+#include <lasxintrin.h>
+extern int *x_si0;
+extern int *x_si1;
+extern long long *x_di0;
+extern long long *x_di1;
+/* { dg-final { scan-assembler-times "i32_0:.*\tmovfr2gr\\.s.*i32_0" 1 } } */
+int
+lasx_pickve2gr_i32_0 ()
+{
+  v8i32 a = { x_si0[0], x_si0[1], x_si0[2], x_si0[3],
+              x_si0[4], x_si0[5], x_si0[6], x_si0[7] };
+  v8i32 b = { x_si1[0], x_si1[1], x_si1[2], x_si1[3],
+              x_si1[4], x_si1[5], x_si1[6], x_si1[7] };
+  v8i32 res = a + b;
+  return res[0];
+}
+/* { dg-final { scan-assembler-times "i32_1:.*\tmovfrh2gr\\.s.*i32_1" 1 } } */
+int
+lasx_pickve2gr_i32_1 ()
+{
+  v8i32 a = { x_si0[0], x_si0[1], x_si0[2], x_si0[3],
+              x_si0[4], x_si0[5], x_si0[6], x_si0[7] };
+  v8i32 b = { x_si1[0], x_si1[1], x_si1[2], x_si1[3],
+              x_si1[4], x_si1[5], x_si1[6], x_si1[7] };
+  v8i32 res = a + b;
+  return res[1];
+}
+/* { dg-final { scan-assembler-times "i32_2:.*\tvpickve2gr\\.w.*i32_2" 1 } } */
+int
+lasx_pickve2gr_i32_2 ()
+{
+  v8i32 a = { x_si0[0], x_si0[1], x_si0[2], x_si0[3],
+              x_si0[4], x_si0[5], x_si0[6], x_si0[7] };
+  v8i32 b = { x_si1[0], x_si1[1], x_si1[2], x_si1[3],
+              x_si1[4], x_si1[5], x_si1[6], x_si1[7] };
+  v8i32 res = a + b;
+  return res[2];
+}
+/* { dg-final { scan-assembler-times "i64_0:.*\tmovfr2gr\\.d.*i64_0" 1 } } */
+long long
+lasx_pickve2gr_i64_0 ()
+{
+  v4i64 a = { x_di0[0], x_di0[1], x_di0[2], x_di0[3] };
+  v4i64 b = { x_di1[0], x_di1[1], x_di1[2], x_di1[3] };
+  v4i64 res = a + b;
+  return res[0];
+}
diff --git 
a/gcc/testsuite/gcc.target/loongarch/vector/lasx/vect-extract-256-128.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lasx/vect-extract-256-128.c
index bdf6e160ace..b3faf6f3b73 100644
--- a/gcc/testsuite/gcc.target/loongarch/vector/lasx/vect-extract-256-128.c
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lasx/vect-extract-256-128.c
@@ -7,8 +7,8 @@
 /*
 **foo1_lo:
 **     xvld    (\$xr[0-9]+),\$r4,0
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */
 __m128
@@ -21,8 +21,8 @@ foo1_lo (__m256 x)
 **foo1_hi:
 **     xvld    (\$xr[0-9]+),\$r4,0
 **     xvpermi.d       (\$xr[0-9]+),(\$xr[0-9]+),0xe
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */
 __m128
@@ -34,8 +34,8 @@ foo1_hi (__m256 x)
 /*
 **foo2_lo:
 **     xvld    (\$xr[0-9]+),\$r4,0
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */
 __m128d
@@ -48,8 +48,8 @@ foo2_lo (__m256d x)
 **foo2_hi:
 **     xvld    (\$xr[0-9]+),\$r4,0
 **     xvpermi.d       (\$xr[0-9]+),(\$xr[0-9]+),0xe
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */    
 __m128d
@@ -61,8 +61,8 @@ foo2_hi (__m256d x)
 /*
 **foo3_lo:
 **     xvld    (\$xr[0-9]+),\$r4,0
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */
 __m128i
@@ -75,8 +75,8 @@ foo3_lo (__m256i x)
 **foo3_hi:
 **     xvld    (\$xr[0-9]+),\$r4,0
 **     xvpermi.d       (\$xr[0-9]+),(\$xr[0-9]+),0xe
-**     vpickve2gr.du   \$r4,(\$vr[0-9]+),0
-**     vpickve2gr.du   \$r5,(\$vr[0-9]+),1
+**     movfr2gr.d      \$r4,(\$f[0-9]+)
+**     vpickve2gr.d    \$r5,(\$vr[0-9]+),1
 **     jr      \$r1
 */
 __m128i
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-builtin.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-builtin.c
index 7ad8f45d840..2db2f5a4033 100644
--- a/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-builtin.c
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-builtin.c
@@ -316,7 +316,7 @@
 /* { dg-final { scan-assembler-times 
"lsx_vpickve2gr_bu:.*vpickve2gr\\.bu.*lsx_vpickve2gr_bu" 1 } } */
 /* { dg-final { scan-assembler-times 
"lsx_vpickve2gr_hu:.*vpickve2gr\\.hu.*lsx_vpickve2gr_hu" 1 } } */
 /* { dg-final { scan-assembler-times 
"lsx_vpickve2gr_wu:.*vpickve2gr\\.wu.*lsx_vpickve2gr_wu" 1 } } */
-/* { dg-final { scan-assembler-times 
"lsx_vpickve2gr_du:.*vpickve2gr\\.du.*lsx_vpickve2gr_du" 1 } } */
+/* { dg-final { scan-assembler-times 
"lsx_vpickve2gr_du:.*vpickve2gr\\.d.*lsx_vpickve2gr_du" 1 } } */
 /* { dg-final { scan-assembler-times 
"lsx_vinsgr2vr_b:.*vinsgr2vr\\.b.*lsx_vinsgr2vr_b" 1 } } */
 /* { dg-final { scan-assembler-times 
"lsx_vinsgr2vr_h:.*vinsgr2vr\\.h.*lsx_vinsgr2vr_h" 1 } } */
 /* { dg-final { scan-assembler-times 
"lsx_vinsgr2vr_w:.*vinsgr2vr\\.w.*lsx_vinsgr2vr_w" 1 } } */
@@ -2299,7 +2299,7 @@ __lsx_vpickve2gr_h (v8i16 _1)
 int
 __lsx_vpickve2gr_w (v4i32 _1)
 {
-  return __builtin_lsx_vpickve2gr_w (_1, 1);
+  return __builtin_lsx_vpickve2gr_w (_1, 2);
 }
 long
 __lsx_vpickve2gr_d (v2i64 _1)
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-vpickve2gr-1.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-vpickve2gr-1.c
new file mode 100644
index 00000000000..83651e0022e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lsx/lsx-vpickve2gr-1.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+/* { dg-options "-mlsx -O3" } */
+#include <lsxintrin.h>
+extern int *x_si0;
+extern int *x_si1;
+extern long long *x_di0;
+extern long long *x_di1;
+/* { dg-final { scan-assembler-times "i32_0:.*\tmovfr2gr\\.s.*i32_0" 1 } } */
+int
+lsx_pickve2gr_i32_0 ()
+{
+  v4i32 a = { x_si0[0], x_si0[1], x_si0[2], x_si0[3] };
+  v4i32 b = { x_si1[0], x_si1[1], x_si1[2], x_si1[3] };
+  v4i32 res = a + b;
+  return res[0];
+}
+/* { dg-final { scan-assembler-times "i32_1:.*\tmovfrh2gr\\.s.*i32_1" 1 } } */
+int
+lsx_pickve2gr_i32_1 ()
+{
+  v4i32 a = { x_si0[0], x_si0[1], x_si0[2], x_si0[3] };
+  v4i32 b = { x_si1[0], x_si1[1], x_si1[2], x_si1[3] };
+  v4i32 res = a + b;
+  return res[1];
+}
+/* { dg-final { scan-assembler-times "i64:.*\tmovfr2gr\\.d.*i64" 1 } } */
+long long
+lsx_pickve2gr_i64 ()
+{
+  v2i64 a = { x_di0[0], x_di0[1] };
+  v2i64 b = { x_di1[0], x_di1[1] };
+  v2i64 res = a + b;
+  return res[0];
+}
-- 
2.56.1

Reply via email to