public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-8-branch)] tree-optimization/98117 - fix range set by vectorization on niter IVs
@ 2021-05-14 14:54 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2021-05-14 14:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:8633465e377d507fe86848781500de68bdd28f68

commit 8633465e377d507fe86848781500de68bdd28f68
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Dec 7 10:29:07 2020 +0100

    tree-optimization/98117 - fix range set by vectorization on niter IVs
    
    This avoids the degenerate case of a TYPE_MAX_VALUE latch iteration
    count value causing wrong range info for the vector IV.  There's
    still the case of VF == 1 where if we don't know whether we hit the
    above case we cannot emit a range.
    
    2020-12-07  Richard Biener  <rguenther@suse.de>
    
            PR tree-optimization/98117
            * tree-vect-loop-manip.c (vect_gen_vector_loop_niters):
            Properly handle degenerate niter when setting the vector
            loop IV range.
    
            * gcc.dg/torture/pr98117.c: New testcase.
    
    (cherry picked from commit 69894ce172412996c10c89838717980ede7c9003)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr98117.c | 19 +++++++++++++++++++
 gcc/tree-vect-loop-manip.c             | 28 ++++++++++++++++++++++------
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr98117.c b/gcc/testsuite/gcc.dg/torture/pr98117.c
new file mode 100644
index 00000000000..f2160257263
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr98117.c
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-additional-options "-fno-tree-scev-cprop" } */
+
+unsigned char c;
+void __attribute__((noipa))
+e()
+{
+  do
+    {
+    }
+  while (++c);
+}
+int main()
+{
+  e();
+  if (c != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-vect-loop-manip.c b/gcc/tree-vect-loop-manip.c
index e82c1fe027e..8e41f5faf83 100644
--- a/gcc/tree-vect-loop-manip.c
+++ b/gcc/tree-vect-loop-manip.c
@@ -1967,13 +1967,29 @@ vect_gen_vector_loop_niters (loop_vec_info loop_vinfo, tree niters,
       niters_vector = force_gimple_operand (niters_vector, &stmts, true, var);
       gsi_insert_seq_on_edge_immediate (pe, stmts);
       /* Peeling algorithm guarantees that vector loop bound is at least ONE,
-	 we set range information to make niters analyzer's life easier.  */
+	 we set range information to make niters analyzer's life easier.
+	 Note the number of latch iteration value can be TYPE_MAX_VALUE so
+	 we have to represent the vector niter TYPE_MAX_VALUE + 1 >> log_vf.  */
       if (stmts != NULL && log_vf)
-	set_range_info (niters_vector, VR_RANGE,
-			wi::to_wide (build_int_cst (type, 1)),
-			wi::to_wide (fold_build2 (RSHIFT_EXPR, type,
-						  TYPE_MAX_VALUE (type),
-						  log_vf)));
+	{
+	  if (niters_no_overflow)
+	    set_range_info (niters_vector, VR_RANGE,
+			    wi::one (TYPE_PRECISION (type)),
+			    wi::rshift (wi::max_value (TYPE_PRECISION (type),
+						       TYPE_SIGN (type)),
+					exact_log2 (const_vf),
+					TYPE_SIGN (type)));
+	  /* For VF == 1 the vector IV might also overflow so we cannot
+	     assert a minimum value of 1.  */
+	  else if (const_vf > 1)
+	    set_range_info (niters_vector, VR_RANGE,
+			    wi::one (TYPE_PRECISION (type)),
+			    wi::rshift (wi::max_value (TYPE_PRECISION (type),
+						       TYPE_SIGN (type))
+					- (const_vf - 1),
+					exact_log2 (const_vf), TYPE_SIGN (type))
+			    + 1);
+	}
     }
   *niters_vector_ptr = niters_vector;
   *step_vector_ptr = step_vector;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-14 14:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-14 14:54 [gcc(refs/vendors/redhat/heads/gcc-8-branch)] tree-optimization/98117 - fix range set by vectorization on niter IVs Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).