The following makes sure to not confuse scalar passes after
vectorization for the scalar prologue/epilogue loops which for
early break vectorization with assumptions may not record all
assumptions of non-main exits in the versioning condition and
thus exit the loop through alternate exits.

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

        PR tree-optimization/128517
        * tree-vect-loop-manip.cc (vect_do_peeling): Clear
        LOOP_C_FINITE for scalar prologue and epilogue if
        doing early-break vectorization.

        * gcc.dg/vect/vect-early-break_150-pr128517.c: New testcase.
---
 .../vect/vect-early-break_150-pr128517.c      | 33 +++++++++++++++++++
 gcc/tree-vect-loop-manip.cc                   |  8 +++++
 2 files changed, 41 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/vect/vect-early-break_150-pr128517.c

diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break_150-pr128517.c 
b/gcc/testsuite/gcc.dg/vect/vect-early-break_150-pr128517.c
new file mode 100644
index 00000000000..01eff937a4a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break_150-pr128517.c
@@ -0,0 +1,33 @@
+/* { dg-add-options vect_early_break } */
+/* { dg-require-effective-target vect_early_break_hw } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-O3" } */
+/* { dg-additional-options "-mavx2" { target avx2_runtime } } */
+
+#include "tree-vect.h"
+
+unsigned calls;
+
+static unsigned rec(unsigned d, unsigned x)
+{
+  calls++;
+  if (d == 0)
+    return 0;
+  rec(d - 1, (x ? 2 : calls) % (2 * d + 1));
+  return calls % 10 / (calls / (x | 1) | 1);
+}
+
+int main ()
+{
+  check_vect ();
+
+  /* Odd D exercises the scalar loop reached when the niter assumption
+     check fails, even D the scalar epilogue of the vectorized loop.  */
+  for (volatile unsigned d = 7; d <= 8; d++)
+    {
+      calls = 0;
+      if (rec(d, 1) != (d & 1 ? 0 : 1) || calls != d + 1)
+       __builtin_abort ();
+    }
+  return 0;
+}
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 96d76332d2b..51c8b42deff 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -3571,6 +3571,10 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, 
tree nitersm1,
 
       gcc_assert (prolog);
       prolog->force_vectorize = false;
+      /* Assumptions are only versioned for the main exit, others might
+        still be taken, so do not mislead later passes.  */
+      if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
+       loop_constraint_clear (prolog, LOOP_C_FINITE);
 
       first_loop = prolog;
       reset_original_copy_tables ();
@@ -3681,6 +3685,10 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, 
tree nitersm1,
       gcc_assert (epilog);
       gcc_assert (new_epilog_e);
       epilog->force_vectorize = false;
+      /* Assumptions are only versioned for the main exit, others might
+        still be taken, so do not mislead later passes.  */
+      if (!vect_epilogues && LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
+       loop_constraint_clear (epilog, LOOP_C_FINITE);
       bb_before_epilog = loop_preheader_edge (epilog)->src;
 
       /* Scalar version loop may be preferred.  In this case, add guard

base-commit: a89bad222e954a9c1a4f29112c75174ad4e54213
-- 
2.51.0

Reply via email to