public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] tree-optimization/101278 - handle self-use in DSE analysis
Date: Thu, 1 Jul 2021 12:31:00 +0200 (CEST)	[thread overview]
Message-ID: <o5295o6p-47nr-5o64-p09n-pro441o24q41@fhfr.qr> (raw)

DSE store classification short-cuts the to-be classified stmt itself
from chaining but fails to first check whether the store uses itself
which can be the case when it is a call with the LHS also passed by
value as argument.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2021-07-01  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/101278
	* tree-ssa-dse.c (dse_classify_store): First check for
	uses, then ignore stmt for chaining purposes.

	* gcc.dg/torture/pr101278.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr101278.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-dse.c                      | 10 +++++-----
 2 files changed, 26 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr101278.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr101278.c b/gcc/testsuite/gcc.dg/torture/pr101278.c
new file mode 100644
index 00000000000..1d25658f37d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr101278.c
@@ -0,0 +1,21 @@
+/* { dg-do run } */
+
+struct X { int counter; };
+
+struct X __attribute__((noipa)) foo (struct X x)
+{
+  x.counter++;
+  if (x.counter == 5)
+    __builtin_exit (0);
+  return x;
+}
+
+int
+main ()
+{
+  struct X x;
+  x.counter = 0;
+  for (int i = 0; i < 10; ++i)
+    x = foo (x);
+  __builtin_abort ();
+}
diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c
index c3939a6417f..98daa8ab24c 100644
--- a/gcc/tree-ssa-dse.c
+++ b/gcc/tree-ssa-dse.c
@@ -813,15 +813,11 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
 	      break;
 	    }
 
-	  /* We have visited ourselves already so ignore STMT for the
-	     purpose of chaining.  */
-	  if (use_stmt == stmt)
-	    ;
 	  /* In simple cases we can look through PHI nodes, but we
 	     have to be careful with loops and with memory references
 	     containing operands that are also operands of PHI nodes.
 	     See gcc.c-torture/execute/20051110-*.c.  */
-	  else if (gimple_code (use_stmt) == GIMPLE_PHI)
+	  if (gimple_code (use_stmt) == GIMPLE_PHI)
 	    {
 	      /* If we already visited this PHI ignore it for further
 		 processing.  */
@@ -861,6 +857,10 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
 	      fail = true;
 	      break;
 	    }
+	  /* We have visited ourselves already so ignore STMT for the
+	     purpose of chaining.  */
+	  else if (use_stmt == stmt)
+	    ;
 	  /* If this is a store, remember it as we possibly need to walk the
 	     defs uses.  */
 	  else if (gimple_vdef (use_stmt))
-- 
2.26.2

                 reply	other threads:[~2021-07-01 10:31 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=o5295o6p-47nr-5o64-p09n-pro441o24q41@fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@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).