[PATCH 2/2] vect: Change IV increment position to standard_iv_increment_position

Alfie Richards alfie.richards@arm.com
Thu Oct 8 16:02:43 GMT 2026


Change both vect_set_loop_controls_directly and
vect_update_ivs_after_vectorizer_for_early_breaks to use
standard_iv_increment_position.

This moves the IV increment to the end of the loop which allows these
loops to later have loop variant increments.

gcc/ChangeLog:

	* tree-vect-loop-manip.cc (vect_set_loop_controls_directly):
	Use standard_iv_increment_position instead of
	vect_iv_increment_position.
	(vect_set_loop_condition_partial_vectors_avx512): Likewise
	(vect_set_loop_condition_normal): Likewise.
	* tree-vect-loop.cc
	(vect_update_ivs_after_vectorizer_for_early_breaks):
	Likewise.

(already approved https://gcc.gnu.org/pipermail/gcc-patches/2026-October/733863.html)
---
 gcc/tree-vect-loop-manip.cc | 17 +++++++----------
 gcc/tree-vect-loop.cc       |  3 +--
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index f8e259c19bd..3421159a042 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -619,8 +619,8 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
   tree index_before_incr, index_after_incr;
   gimple_stmt_iterator incr_gsi;
   bool insert_after;
-  edge exit_e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
-  vect_iv_increment_position (exit_e, &incr_gsi, &insert_after);
+  standard_iv_increment_position (loop, &incr_gsi, &insert_after);
+
   if (LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo))
     {
       /* Create an IV that counts down from niters_total and whose step
@@ -690,7 +690,6 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
 
   tree zero_index = build_int_cst (compare_type, 0);
   tree test_index, test_limit, first_limit;
-  gimple_stmt_iterator *test_gsi;
   if (might_wrap_p)
     {
       /* In principle the loop should stop iterating once the incremented
@@ -729,7 +728,6 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
 				 nitems_total, adjust);
       test_limit = gimple_build (preheader_seq, MINUS_EXPR, compare_type,
 				 test_limit, adjust);
-      test_gsi = &incr_gsi;
 
       /* Get a safe limit for the first iteration.  */
       if (nitems_skip)
@@ -760,7 +758,6 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
       if (nitems_skip)
 	test_limit = gimple_build (preheader_seq, PLUS_EXPR, compare_type,
 				   test_limit, nitems_skip);
-      test_gsi = &loop_cond_gsi;
 
       first_limit = test_limit;
     }
@@ -769,7 +766,7 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
      a demotion).  */
   gimple_seq test_seq = NULL;
   test_index = gimple_convert (&test_seq, compare_type, test_index);
-  gsi_insert_seq_before (test_gsi, test_seq, GSI_SAME_STMT);
+  gsi_insert_seq_after (&incr_gsi, test_seq, GSI_NEW_STMT);
 
   /* Provide a definition of each control in the group.  */
   tree next_ctrl = NULL_TREE;
@@ -878,14 +875,14 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
 	  gimple_seq stmts = NULL;
 	  next_ctrl = vect_gen_while (&stmts, ctrl_type, test_index,
 				      this_test_limit, "next_mask");
-	  gsi_insert_seq_before (test_gsi, stmts, GSI_SAME_STMT);
+	  gsi_insert_seq_after (&incr_gsi, stmts, GSI_SAME_STMT);
 	}
       else
 	{
 	  next_ctrl = make_temp_ssa_name (compare_type, NULL, "next_len");
 	  gimple_seq seq = vect_gen_len (next_ctrl, test_index, this_test_limit,
 					 length_limit);
-	  gsi_insert_seq_before (test_gsi, seq, GSI_SAME_STMT);
+	  gsi_insert_seq_after (&incr_gsi, seq, GSI_SAME_STMT);
 	}
 
       vect_set_loop_control (loop, ctrl, init_ctrl, next_ctrl);
@@ -1194,7 +1191,7 @@ vect_set_loop_condition_partial_vectors_avx512 (class loop *loop,
   gimple_stmt_iterator incr_gsi;
   tree index_before_incr, index_after_incr;
   bool insert_after;
-  vect_iv_increment_position (exit_edge, &incr_gsi, &insert_after);
+  standard_iv_increment_position (loop, &incr_gsi, &insert_after);
 
   /* The iteration step is the vectorization factor.  */
   tree iv_step = gimple_convert (&preheader_seq, iv_type,
@@ -1505,7 +1502,7 @@ vect_set_loop_condition_normal (loop_vec_info loop_vinfo, edge exit_edge,
 	}
     }
 
-  vect_iv_increment_position (exit_edge, &incr_gsi, &insert_after);
+  standard_iv_increment_position (loop, &incr_gsi, &insert_after);
   create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
 	     &incr_gsi, insert_after,
 	     &indx_before_incr, &indx_after_incr,
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 287902089c6..45aacdb2a13 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -11272,8 +11272,7 @@ vect_update_ivs_after_vectorizer_for_early_breaks (loop_vec_info loop_vinfo)
   /* Write the adjustments at the end of the iv increment.  */
   bool insert_after;
   gimple_stmt_iterator incr_gsi;
-  vect_iv_increment_position (LOOP_VINFO_MAIN_EXIT (loop_vinfo), &incr_gsi,
-			      &insert_after);
+  standard_iv_increment_position (loop, &incr_gsi, &insert_after);
 
   if (insert_after)
     gsi_insert_seq_after (&incr_gsi, stmts, GSI_NEW_STMT);
-- 
2.55.0



More information about the Gcc-patches mailing list