Hi,

When using pack or unpack in the simplification of a vector constructor
we must make sure that the original BIT_FIELD_REF was no sign-changing
nop conversion.  If it was we cannot safely pack/unpack as that would
skip sign or zero extensions.  This patch adds useless_type_conversion_p
to both paths.

Bootstrapped and regtested on x86 and power10.  On power10 I surprisingly
saw one algol68 failure with the patch but that can only be unrelated and due 
to the build environment.  Re-testing in progress.

Regtested on riscv64 and aarch64, and loongarch64 via qemu.

I tried coming up with an x86 test case but failed.  Suggestions welcome.

Regards
 Robin

        PR tree-optimization/123117

gcc/ChangeLog:

        * tree-ssa-forwprop.cc (simplify_vector_constructor):
        Check if we had a nop conversion and don't use pack/unpack in
        that case.

gcc/testsuite/ChangeLog:

        * gcc.target/loongarch/vector/lsx/pr123117.c: New test.
---
 .../gcc.target/loongarch/vector/lsx/pr123117.c  | 17 +++++++++++++++++
 gcc/tree-ssa-forwprop.cc                        | 12 ++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/vector/lsx/pr123117.c

diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lsx/pr123117.c 
b/gcc/testsuite/gcc.target/loongarch/vector/lsx/pr123117.c
new file mode 100644
index 00000000000..e16a7204ef4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/vector/lsx/pr123117.c
@@ -0,0 +1,17 @@
+/* { dg-do run } */
+/* { dg-options "-mlsx -DARCH_LOONGARCH64 -O1 -static" } */
+
+#define BS_VEC(type, num) type __attribute__((vector_size(num * sizeof(type))))
+#define T BS_VEC(unsigned, 4)
+
+int main ()
+{
+  BS_VEC(long int, 2) BS_VAR_0[1];
+  BS_VEC(int, 4) tt = (BS_VEC(int, 4)){0x9e47d3d2, 0, 0, 0};
+  asm("":"+f"(tt));
+  BS_VEC(unsigned int, 2) SHUF = __builtin_shufflevector((T)tt, (T)tt, 0, 1);
+  BS_VAR_0[0] = __builtin_convertvector(SHUF, BS_VEC(long int, 2));
+
+  if (BS_VAR_0[0][0] != 0x000000009e47d3d2)
+    __builtin_abort ();
+}
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index 2200fc04918..17bfa2a99dc 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -3848,6 +3848,7 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
 
   orig[0] = NULL;
   orig[1] = NULL;
+  tree orig_elem_type[2] = {};
   conv_code = ERROR_MARK;
   bool maybe_ident = true;
   bool maybe_blend[2] = { true, true };
@@ -3902,6 +3903,11 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          if (j < 2)
            {
              orig[j] = ref;
+             /* Track what element type was actually extracted (which may
+                differ in signedness from the vector's element type due to
+                tree_nop_conversion_p).  */
+             if (!orig_elem_type[j])
+               orig_elem_type[j] = TREE_TYPE (op1);
              if (elem != i || j != 0)
                maybe_ident = false;
              if (elem != i)
@@ -4011,6 +4017,9 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          if (CONVERT_EXPR_CODE_P (conv_code)
              && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
                  == TYPE_PRECISION (TREE_TYPE (type)))
+             && orig_elem_type[0]
+             && useless_type_conversion_p (orig_elem_type[0],
+                                           TREE_TYPE (type))
              && mode_for_vector (as_a <scalar_mode>
                                  (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
                                  nelts * 2).exists ()
@@ -4050,6 +4059,9 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          else if (CONVERT_EXPR_CODE_P (conv_code)
                   && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
                       == 2 * TYPE_PRECISION (TREE_TYPE (type)))
+                  && orig_elem_type[0]
+                  && useless_type_conversion_p (orig_elem_type[0],
+                                                TREE_TYPE (type))
                   && mode_for_vector (as_a <scalar_mode>
                                         (TYPE_MODE
                                           (TREE_TYPE (TREE_TYPE (orig[0])))),
-- 
2.51.1

Reply via email to