public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-3313] Refine fix for PR78185, improve LIM for code after inner loops
Date: Thu,  2 Sep 2021 09:39:38 +0000 (GMT)	[thread overview]
Message-ID: <20210902093938.DA94F3858018@sourceware.org> (raw)

https://gcc.gnu.org/g:483e400870601f650c80f867ec781cd5f83507d6

commit r12-3313-g483e400870601f650c80f867ec781cd5f83507d6
Author: Richard Biener <rguenther@suse.de>
Date:   Thu Sep 2 10:47:35 2021 +0200

    Refine fix for PR78185, improve LIM for code after inner loops
    
    This refines the fix for PR78185 after understanding that the code
    regarding to the comment 'In a loop that is always entered we may
    proceed anyway.  But record that we entered it and stop once we leave
    it.' was supposed to protect us from leaving possibly infinite inner
    loops.  The simpler fix of moving the misplaced stopping code
    can then be refined to continue processing when the exited inner
    loop is finite, improving invariant motion for cases like in the
    added testcase.
    
    2021-09-02  Richard Biener  <rguenther@suse.de>
    
            * tree-ssa-loop-im.c (fill_always_executed_in_1): Refine
            fix for PR78185 and continue processing when leaving
            finite inner loops.
    
            * gcc.dg/tree-ssa/ssa-lim-16.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-16.c | 19 +++++++++++++++++
 gcc/tree-ssa-loop-im.c                     | 33 +++++++++++++++++-------------
 2 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-16.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-16.c
new file mode 100644
index 00000000000..741582b745f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-16.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-lim2-details" } */
+
+volatile int flag, bar;
+double foo (double *valp)
+{
+  double sum = 0;
+  for (int i = 0; i < 256; ++i)
+    {
+      for (int j = 0; j < 256; ++j)
+        bar = flag;
+      if (flag)
+        sum += 1.;
+      sum += *valp; // we should move the load of *valp out of the loop
+    }
+  return sum;
+}
+
+/* { dg-final { scan-tree-dump-times "Moving statement" 1 "lim2" } } */
diff --git a/gcc/tree-ssa-loop-im.c b/gcc/tree-ssa-loop-im.c
index d9f75d5025e..01f3fc2f7f0 100644
--- a/gcc/tree-ssa-loop-im.c
+++ b/gcc/tree-ssa-loop-im.c
@@ -3044,23 +3044,27 @@ fill_always_executed_in_1 (class loop *loop, sbitmap contains_call)
 	  edge_iterator ei;
 	  bb = bbs[i];
 
+	  if (!flow_bb_inside_loop_p (inn_loop, bb))
+	    {
+	      /* When we are leaving a possibly infinite inner loop
+		 we have to stop processing.  */
+	      if (!finite_loop_p (inn_loop))
+		break;
+	      /* If the loop was finite we can continue with processing
+		 the loop we exited to.  */
+	      inn_loop = bb->loop_father;
+	    }
+
 	  if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
 	    last = bb;
 
 	  if (bitmap_bit_p (contains_call, bb->index))
 	    break;
 
+	  /* If LOOP exits from this BB stop processing.  */
 	  FOR_EACH_EDGE (e, ei, bb->succs)
-	    {
-	      /* If there is an exit from this BB.  */
-	      if (!flow_bb_inside_loop_p (loop, e->dest))
-		break;
-	      /* Or we enter a possibly non-finite loop.  */
-	      if (flow_loop_nested_p (bb->loop_father,
-				      e->dest->loop_father)
-		  && ! finite_loop_p (e->dest->loop_father))
-		break;
-	    }
+	    if (!flow_bb_inside_loop_p (loop, e->dest))
+	      break;
 	  if (e)
 	    break;
 
@@ -3069,22 +3073,23 @@ fill_always_executed_in_1 (class loop *loop, sbitmap contains_call)
 	  if (bb->flags & BB_IRREDUCIBLE_LOOP)
 	    break;
 
-	  if (!flow_bb_inside_loop_p (inn_loop, bb))
-	    break;
-
 	  if (bb->loop_father->header == bb)
 	    {
 	      if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
 		break;
 
 	      /* In a loop that is always entered we may proceed anyway.
-		 But record that we entered it and stop once we leave it.  */
+		 But record that we entered it and stop once we leave it
+		 since it might not be finite.  */
 	      inn_loop = bb->loop_father;
 	    }
 	}
 
       while (1)
 	{
+	  if (dump_enabled_p ())
+	    dump_printf (MSG_NOTE, "BB %d is always executed in loop %d\n",
+			 last->index, loop->num);
 	  SET_ALWAYS_EXECUTED_IN (last, loop);
 	  if (last == loop->header)
 	    break;


                 reply	other threads:[~2021-09-02  9:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210902093938.DA94F3858018@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).