[gcc r16-9244] vect: use .VARYING for loop iteraion mask [PR125597]

Tamar Christina via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:5d8844db0f1af8f6492fac1ca863e1a9f80528f1

commit r16-9244-g5d8844db0f1af8f6492fac1ca863e1a9f80528f1
Author: Tamar Christina <[email protected]>
Date:   Fri Jun 12 11:56:33 2026 +0100

    vect: use .VARYING for loop iteraion mask [PR125597]
    
    This replaces the gimple_build_nop () used for making loop control mask variable
    with an IFN_VARYING and then actually adding the statement to the IL.
    
    The bug happens because the mask def statement itself is not in the IL but
    computations that use it are.  As such dataflow analysis fails as it tries to
    analyze the use/def chain of these expressions.
    
    When the value if known we replace the IFN_VARYING with the final statement.
    
    gcc/ChangeLog:
    
            PR tree-optimization/125597
            (vect_do_peeling): Create IFN_VARYING for masks.
            * tree-vect-loop-manip.cc (vect_set_loop_condition): Replace the
            IFN_VARYING.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/125597
            * gcc.target/aarch64/pr125597.c: New test.
    
    (cherry picked from commit 7c1666c8c462cb1bbc6c0b2f6bc02ad968d769ad)

Diff:
---
 gcc/testsuite/gcc.target/aarch64/pr125597.c | 11 +++++++++++
 gcc/tree-vect-loop-manip.cc                 | 21 ++++++++++++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.target/aarch64/pr125597.c b/gcc/testsuite/gcc.target/aarch64/pr125597.c
new file mode 100644
index 000000000000..895ecfa0f9ba
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr125597.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -mcpu=c1-ultra -mautovec-preference=sve-only" } */
+
+_BitInt(256) b;
+
+void
+foo()
+{
+  for (;;)
+    b ^= 0x100000000020000000000000000000000000000000000wb;
+}
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index a5a3bb39e0ee..f7a0e8500435 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -1402,6 +1402,19 @@ vect_set_loop_condition (class loop *loop, edge loop_e, loop_vec_info loop_vinfo
   gcond *orig_cond = get_loop_exit_condition (loop_e);
   gimple_stmt_iterator loop_cond_gsi = gsi_for_stmt (orig_cond);
 
+  /* Check to see whether we will be replacing final_IV below.  Because of the
+     various replacement strategies (assign vs PHI) just remove it now and
+     leave the SSA name to be rebuild below.  */
+  if (final_iv && TREE_CODE (final_iv) == SSA_NAME)
+    {
+      gimple *def = SSA_NAME_DEF_STMT (final_iv);
+      if (gimple_call_internal_p (def, IFN_VARYING))
+	{
+	  gimple_stmt_iterator gsi = gsi_for_stmt (def);
+	  gsi_remove (&gsi, true);
+	}
+    }
+
   if (loop_vinfo && LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo))
     {
       if (LOOP_VINFO_PARTIAL_VECTORS_STYLE (loop_vinfo) == vect_partial_vectors_avx512)
@@ -3680,7 +3693,13 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
 		 until then.  */
 	      niters_vector_mult_vf
 		= make_ssa_name (TREE_TYPE (*niters_vector));
-	      SSA_NAME_DEF_STMT (niters_vector_mult_vf) = gimple_build_nop ();
+	      edge exit_e = LOOP_VINFO_MAIN_EXIT (loop_vinfo);
+	      gimple_stmt_iterator loop_cond_gsi
+		= gsi_after_labels (exit_e->dest);
+
+	      gcall *tmp = gimple_build_call_internal (IFN_VARYING, 0);
+	      gimple_call_set_lhs (tmp, niters_vector_mult_vf);
+	      gsi_insert_before (&loop_cond_gsi, tmp, GSI_SAME_STMT);
 	      *niters_vector_mult_vf_var = niters_vector_mult_vf;
 	    }
 	  else
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.