]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
forwprop: Check type conversion in pack/unpack [PR123117].
authorRobin Dapp <rdapp@qti.qualcomm.com>
Mon, 15 Dec 2025 12:01:40 +0000 (13:01 +0100)
committerRobin Dapp <rdapp@oss.qualcomm.com>
Fri, 19 Dec 2025 18:41:53 +0000 (19:41 +0100)
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.

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/testsuite/gcc.target/loongarch/vector/lsx/pr123117.c [new file with mode: 0644]
gcc/tree-ssa-forwprop.cc

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 (file)
index 0000000..c9683e9
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-options "-mlsx -O1" } */
+
+#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 ();
+}
index 2200fc04918d2bddd041ea0cd03ec5e5becfb1bc..17bfa2a99dc442529b876b6e3af4c907da248d8e 100644 (file)
@@ -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])))),