tree-optimization/113078 - conditional subtraction reduction vectorization

Message ID 20240110142647.2092B3858298@sourceware.org
State Committed
Commit cac9d2d2346bf06b29b34e12cf0a005c37eacdc9
Headers
Series tree-optimization/113078 - conditional subtraction reduction vectorization |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged

Commit Message

Richard Biener Jan. 10, 2024, 2:21 p.m. UTC
  When if-conversion was changed to use .COND_ADD/SUB for conditional
reduction it was forgotten to update reduction path handling to
canonicalize .COND_SUB to .COND_ADD for vectorizable_reduction
similar to what we do for MINUS_EXPR.  The following adds this
and testcases exercising this at runtime and looking for the
appropriate masked subtraction in the vectorized code on x86.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	PR tree-optimization/113078
	* tree-vect-loop.cc (check_reduction_path): Canonicalize
	.COND_SUB to .COND_ADD.

	* gcc.dg/vect/vect-reduc-cond-sub.c: New testcase.
	* gcc.target/i386/vect-pr113078.c: Likewise.
---
 .../gcc.dg/vect/vect-reduc-cond-sub.c         | 29 +++++++++++++++++++
 gcc/testsuite/gcc.target/i386/vect-pr113078.c | 16 ++++++++++
 gcc/tree-vect-loop.cc                         |  7 +++++
 3 files changed, 52 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c
 create mode 100644 gcc/testsuite/gcc.target/i386/vect-pr113078.c
  

Patch

diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c b/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c
new file mode 100644
index 00000000000..0213a0ab4fd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c
@@ -0,0 +1,29 @@ 
+/* { dg-require-effective-target vect_int } */
+
+#include "tree-vect.h"
+
+int __attribute__((noipa))
+foo (int n, int* p, int* pi)
+{
+  int sum = 0;
+  for (int i = 0; i != n; i++)
+    {
+      if (pi[i] > 0)
+        sum -= p[i];
+    }
+  return sum;
+}
+
+int p[16] __attribute__((aligned(__BIGGEST_ALIGNMENT__)))
+  = { 7, 3, 1, 4, 9, 10, 14, 7, -10, -55, 20, 9, 1, 2, 0, -17 };
+int pi[16] __attribute__((aligned(__BIGGEST_ALIGNMENT__)))
+  = { 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1 };
+int
+main()
+{
+  check_vect ();
+
+  if (foo (16, p, pi) != 57)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/vect-pr113078.c b/gcc/testsuite/gcc.target/i386/vect-pr113078.c
new file mode 100644
index 00000000000..e7666054324
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/vect-pr113078.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O3 -mavx512vl" } */
+
+int
+foo (int n, int* p, int* pi)
+{
+  int sum = 0;
+  for (int i = 0; i != n; i++)
+    {
+      if (pi[i] > 0)
+	sum -= p[i];
+    }
+  return sum;
+}
+
+/* { dg-final { scan-assembler-times "vpsub\[^\r\n\]*%k" 2 } } */
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index c5b2799be23..1bdad0fbe0f 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -4116,6 +4116,13 @@  pop:
 	  if (op.ops[1] == op.ops[opi])
 	    neg = ! neg;
 	}
+      else if (op.code == IFN_COND_SUB)
+	{
+	  op.code = IFN_COND_ADD;
+	  /* Track whether we negate the reduction value each iteration.  */
+	  if (op.ops[2] == op.ops[opi])
+	    neg = ! neg;
+	}
       if (CONVERT_EXPR_CODE_P (op.code)
 	  && tree_nop_conversion_p (op.type, TREE_TYPE (op.ops[0])))
 	;