public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-8-branch)] reassoc: Fix -fcompare-debug bug in reassociate_bb [PR94329]
@ 2020-09-17 17:21 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-09-17 17:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:21ec7732be0ee84dd279bc8ee669e5d34c171576

commit 21ec7732be0ee84dd279bc8ee669e5d34c171576
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Mar 28 10:21:52 2020 +0100

    reassoc: Fix -fcompare-debug bug in reassociate_bb [PR94329]
    
    The following testcase FAILs with -fcompare-debug, because reassociate_bb
    mishandles the case when the last stmt in a bb has zero uses.  In that case
    reassoc_remove_stmt (like gsi_remove) moves the iterator to the next stmt,
    i.e. gsi_end_p is true, which means the code sets the iterator back to
    gsi_last_bb.  The problem is that the for loop does gsi_prev on that before
    handling the next statement, which means the former penultimate stmt, now
    last one, is not processed by reassociate_bb.
    Now, with -g, if there is at least one debug stmt at the end of the bb,
    reassoc_remove_stmt moves the iterator to that following debug stmt and we
    just do gsi_prev and continue with the former penultimate non-debug stmt,
    now last non-debug stmt.
    
    The following patch fixes that by not doing the gsi_prev in this case; there
    are too many continue; cases, so I didn't want to copy over the gsi_prev to
    all of them, so this patch uses a bool for that instead.  The second
    gsi_end_p check isn't needed anymore, because when we don't do the
    undesirable gsi_prev after gsi = gsi_last_bb, the loop !gsi_end_p (gsi)
    condition will catch the removal of the very last stmt from a bb.
    
    2020-03-28  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/94329
            * tree-ssa-reassoc.c (reassociate_bb): When calling reassoc_remove_stmt
            on the last stmt in a bb, make sure gsi_prev isn't done immediately
            after gsi_last_bb.
    
            * gfortran.dg/pr94329.f90: New test.
    
    (cherry picked from commit aa9c08ef97f4df1ebb1fc8d72f2e7f9f8c1045c2)

Diff:
---
 gcc/testsuite/gfortran.dg/pr94329.f90 | 12 ++++++++++++
 gcc/tree-ssa-reassoc.c                | 14 +++++++-------
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/gcc/testsuite/gfortran.dg/pr94329.f90 b/gcc/testsuite/gfortran.dg/pr94329.f90
new file mode 100644
index 00000000000..9efcf4b6524
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr94329.f90
@@ -0,0 +1,12 @@
+! PR tree-optimization/94329
+! { dg-do compile }
+! { dg-options "-O1 -fno-tree-loop-optimize -fwrapv -fcompare-debug" }
+
+subroutine pr94329 (s, t)
+  real :: s, t(:,:)
+  do i = 1,3
+    do j = 1,3
+      s = t(i,j)
+    end do
+  end do
+end
diff --git a/gcc/tree-ssa-reassoc.c b/gcc/tree-ssa-reassoc.c
index 9ad3581f85d..e2a08c6ad24 100644
--- a/gcc/tree-ssa-reassoc.c
+++ b/gcc/tree-ssa-reassoc.c
@@ -5829,8 +5829,11 @@ reassociate_bb (basic_block bb)
   if (stmt && !gimple_visited_p (stmt))
     cfg_cleanup_needed |= maybe_optimize_range_tests (stmt);
 
-  for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
+  bool do_prev = false;
+  for (gsi = gsi_last_bb (bb);
+       !gsi_end_p (gsi); do_prev ? gsi_prev (&gsi) : (void) 0)
     {
+      do_prev = true;
       stmt = gsi_stmt (gsi);
 
       if (is_gimple_assign (stmt)
@@ -5856,15 +5859,12 @@ reassociate_bb (basic_block bb)
 		  release_defs (stmt);
 		  /* We might end up removing the last stmt above which
 		     places the iterator to the end of the sequence.
-		     Reset it to the last stmt in this case which might
-		     be the end of the sequence as well if we removed
-		     the last statement of the sequence.  In which case
-		     we need to bail out.  */
+		     Reset it to the last stmt in this case and make sure
+		     we don't do gsi_prev in that case.  */
 		  if (gsi_end_p (gsi))
 		    {
 		      gsi = gsi_last_bb (bb);
-		      if (gsi_end_p (gsi))
-			break;
+		      do_prev = false;
 		    }
 		}
 	      continue;


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

only message in thread, other threads:[~2020-09-17 17:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 17:21 [gcc(refs/vendors/redhat/heads/gcc-8-branch)] reassoc: Fix -fcompare-debug bug in reassociate_bb [PR94329] 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).