]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect/ifcvt: Don't factor out VEC_PERM_EXPR with constant masks [PR123382]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sat, 3 Jan 2026 19:32:02 +0000 (11:32 -0800)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Thu, 8 Jan 2026 00:47:10 +0000 (16:47 -0800)
VEC_PERM_EXPR is another special case expression where constants can mean
something different from non-constant.
So if we have:
```
  if (_5 != 0) goto <bb 4>; else goto <bb 5>;
  <bb 4>
  t_15 = VEC_PERM_EXPR <t_12, t_12, { 3, 3, 2, 3 }>;
  goto <bb 6>; [100.00%]
  <bb 5>
  t_14 = VEC_PERM_EXPR <t_12, t_12, { 0, 0, 2, 3 }>;
  <bb 6>
  # t_7 = PHI <t_15(4), t_14(5)>
```
We can't factor out the VEC_PERM_EXPR here since the type
of the vector constant can be different from the type of
the other operands. This is unlike the operand is not a
constant, the mask has to be an integral type which is
similar to the other operands.

Changes since v1:
 * v2: Expand comment on why we should reject this.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/123382

gcc/ChangeLog:

* tree-if-conv.cc: Reject VEC_PERM_EXPR for factoring
if it is the mask and they are constant.

gcc/testsuite/ChangeLog:

* gcc.dg/torture/pr123382-1.c: New test.
* gcc.dg/torture/pr123382-2.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/testsuite/gcc.dg/torture/pr123382-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/pr123382-2.c [new file with mode: 0644]
gcc/tree-if-conv.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr123382-1.c b/gcc/testsuite/gcc.dg/torture/pr123382-1.c
new file mode 100644 (file)
index 0000000..9890f91
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* PR tree-optimization/123382 */
+
+#define BS_VEC(type, num) type __attribute__((vector_size(num * sizeof(type))))
+
+typedef BS_VEC(short, 4) v4s;
+void f(int l, v4s *a, bool *b)
+{
+  for(int i =0;i < l; i++)
+  {
+    v4s t = a[i];
+    if (b[i])
+      t = __builtin_shufflevector(t, t, 3,3,2,3);
+    else
+      t = __builtin_shufflevector(t, t, 0,0,2,3);
+    a[i] = t;
+  }
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr123382-2.c b/gcc/testsuite/gcc.dg/torture/pr123382-2.c
new file mode 100644 (file)
index 0000000..6d57b51
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* PR tree-optimization/123382 */
+
+#define BS_VEC(type, num) type __attribute__((vector_size(num * sizeof(type))))
+
+typedef BS_VEC(short, 4) v4s;
+void f(int l, v4s *a, bool *b)
+{
+  for(int i =0;i < l; i++)
+  {
+    v4s t = a[i];
+    if (b[i])
+      t = __builtin_shufflevector(t, t, 3,3,2,3);
+    else
+      t = __builtin_shuffle(t, t, t);
+    a[i] = t;
+  }
+}
index 6f258a390761b52e1a79ae991327d3ceae33e1e8..c8f7b8453d85e87f529838ebb8d9fea29b853192 100644 (file)
@@ -2284,6 +2284,19 @@ again:
       && opnum != 0)
     return;
 
+  /* It is not profitability to factor out vec_perm with
+     constant masks (operand 2).  The target might not support it
+     and that might be invalid to do as such. Also with constants
+     masks, the number of elements of the mask type does not need
+     to match tne number of elements of other operands and can be
+     arbitrary integral vector type so factoring that out can't work.
+     Note in the case where one mask is a constant and the other is not,
+     the next check for compatiable types will reject the case the
+     constant mask has the incompatible type.  */
+  if (arg1_op.code == VEC_PERM_EXPR && opnum == 2
+      && TREE_CODE (new_arg0) == VECTOR_CST
+      && TREE_CODE (new_arg1) == VECTOR_CST)
+    return;
 
   if (!types_compatible_p (TREE_TYPE (new_arg0), TREE_TYPE (new_arg1)))
     return;