https://gcc.gnu.org/g:6a36a6dbe1379325f067fd90b237b1b78e211526

commit r17-5882-g6a36a6dbe1379325f067fd90b237b1b78e211526
Author: Richard Biener <[email protected]>
Date:   Tue Nov 3 10:11:09 2026 +0100

    tree-optimization/128394 - fix insertion of early-break niter def
    
    As we're inserting on a gsi_after_labels iterator we have to use
    gsi_insert_before.
    
            PR tree-optimization/128394
            * tree-vect-loop-manip.cc (vect_do_peeling): Use gsi_insert_before
            to insert after labels.
    
            * gcc.dg/vect/vect-early-break_149-pr128394.c: New testcase.

Diff:
---
 .../gcc.dg/vect/vect-early-break_149-pr128394.c    | 23 +++++++++++++++
 gcc/tree-vect-loop-manip.cc                        | 34 +++++++++++-----------
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break_149-pr128394.c 
b/gcc/testsuite/gcc.dg/vect/vect-early-break_149-pr128394.c
new file mode 100644
index 000000000000..d1b93541d13f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break_149-pr128394.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target vect_early_break } */
+/* { dg-add-options vect_early_break } */
+
+char a;
+
+void
+b (void)
+{
+  &&c;
+  void *e = &&d;
+  goto *e;
+f:
+  a = a + 1;
+  goto g;
+h:
+  goto f;
+g:
+  if (4U + a)
+  d:
+  c:
+    goto h;
+}
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 6b6f11a25a4d..886f27b5fa14 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -3862,27 +3862,27 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, 
tree nitersm1,
          gcall *tmp_call = gimple_build_call_internal (IFN_VARYING, 0);
          gimple_call_set_lhs (tmp_call, tmp_niters_vf);
          auto header_gsi = gsi_after_labels (loop->header);
-         gsi_insert_after (&header_gsi, tmp_call, GSI_SAME_STMT);
+         gsi_insert_before (&header_gsi, tmp_call, GSI_SAME_STMT);
 
          if (!(LOOP_VINFO_NITERS_UNCOUNTED_P (loop_vinfo)
                && get_loop_exit_edges (loop).length () == 1)
              && LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo))
-         {
-           basic_block exit_bb = NULL;
-           edge update_e = NULL;
-
-           /* Identify the early exit merge block.  I wish we had stored
-              this.  */
-           for (auto e : get_loop_exit_edges (loop))
-             if (e != LOOP_VINFO_MAIN_EXIT (loop_vinfo))
-               {
-                 exit_bb = e->dest;
-                 update_e = single_succ_edge (exit_bb);
-                 break;
-               }
-           vect_update_ivs_after_vectorizer (loop_vinfo, tmp_niters_vf,
-                                             update_e, true);
-         }
+           {
+             basic_block exit_bb = NULL;
+             edge update_e = NULL;
+
+             /* Identify the early exit merge block.  I wish we had stored
+                this.  */
+             for (auto e : get_loop_exit_edges (loop))
+               if (e != LOOP_VINFO_MAIN_EXIT (loop_vinfo))
+                 {
+                   exit_bb = e->dest;
+                   update_e = single_succ_edge (exit_bb);
+                   break;
+                 }
+             vect_update_ivs_after_vectorizer (loop_vinfo, tmp_niters_vf,
+                                               update_e, true);
+           }
          if (uncounted_p)
            vector_iters_vf = tmp_niters_vf;

Reply via email to