[committed] tree-optimization/128394 - fix insertion of early-break niter def

Message ID 262s3oo3-r088-96p0-n86p-n8n8038p25o1@fhfr.qr
State New
Headers
Series [committed] tree-optimization/128394 - fix insertion of early-break niter def |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply

Commit Message

Richard Biener Nov. 3, 2026, 12:06 p.m. UTC
  As we're inserting on a gsi_after_labels iterator we have to use
gsi_insert_before.

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

	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.
---
 .../vect/vect-early-break_149-pr128394.c      | 23 +++++++++++++
 gcc/tree-vect-loop-manip.cc                   | 34 +++++++++----------
 2 files changed, 40 insertions(+), 17 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/vect-early-break_149-pr128394.c
  

Patch

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 00000000000..d1b93541d13
--- /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 6b6f11a25a4..886f27b5fa1 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;