public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [tree-optimization/61607] Look through SSA_NAME_VALUE chains
Date: Mon, 30 Jun 2014 06:56:00 -0000	[thread overview]
Message-ID: <53B10A08.4000306@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 980 bytes --]


SSA_NAME_VALUE is, in effect, a chain of values.  ie, it's possible for 
SSA_NAME_VALUE of any given SSA_NAME to refer to another SSA_NAME.  In 
many cases it is advantageous to look deeper into those chains, 
particularly when simplifying conditionals for jump threading.

The problem with simply following the chains, is they can have loops. 
This can occur when we're threading across a loop backedge.

I did some fairly simple experiments which showed that chains of 0, or 1 
element are by far the most common.  chains of 2 elements are rare, but 
do occur in practice (such as pr61607).  Chains of > 2 elements 
consistently have loops.

So this patch just looks up to two elements deep in the chain and avoids 
the complication of explicitly looking for loops.  This allows us to 
pick up the obvious equivalency in pr61607 and gets the jumps outside 
the loop fully threaded.

Bootstrapped and regression tested on x86_64-unknown-linux-gnu. 
Installed on the trunk.






[-- Attachment #2: P --]
[-- Type: text/plain, Size: 3657 bytes --]

commit 3e2754da946eb64d7a1d30548c9b6119cda3a014
Author: Jeff Law <law@redhat.com>
Date:   Sun Jun 29 23:35:50 2014 -0600

    	tree-optimization/61607
    	* tree-ssa-threadedge.c (simplify_control_stmt_condition): Look
    	deeper into the SSA_NAME_VALUE chain.
    
    	tree-optimization/61607
    	* gcc.dg/tree-ssa/pr61607.c: New test.

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 4cc167a..6dfe1d3 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2014-06-30  Jeff Law  <law@redhat.com>
+
+	tree-optimization/61607
+	* tree-ssa-threadedge.c (simplify_control_stmt_condition): Look
+	deeper into the SSA_NAME_VALUE chain.
+
 2014-06-30  Zhenqiang Chen  <zhenqiang.chen@linaro.org>
 
 	* loop-invariant.c (get_inv_cost): Handle register class.
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 5a9d73a..1df9d4e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2014-06-30  Jeff Law  <law@redhat.com>
+
+	tree-optimization/61607
+	* gcc.dg/tree-ssa/pr61607.c: New test.
+
 2014-06-30  Zhenqiang Chen  <zhenqiang.chen@linaro.org>
 
 	* ira-loop-pressure.c: New test.
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr61607.c b/gcc/testsuite/gcc.dg/tree-ssa/pr61607.c
new file mode 100644
index 0000000..ec00f51
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr61607.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fno-tree-fre -fdump-tree-dom1" } */
+
+void foo(int *);
+void f2(int dst[3], int R)
+{
+  int i, inter[2];
+  _Bool inter0p = 0;
+  _Bool inter1p = 0;
+  for (i = 1; i < R; i++)
+    {
+      inter0p = 1;
+      inter1p = 1;
+    }
+  if (inter0p)
+    inter[0] = 1;
+  if (inter1p)
+    inter[1] = 1;
+  foo(inter);
+}
+
+
+/* There should be precisely two conditionals.  One for the loop condition
+   and one for the test after the loop.  Previously we failed to eliminate
+   the second conditional after the loop.  */
+/* { dg-final { scan-tree-dump-times "if" 2 "dom1"} } */
+ 
+/* { dg-final { cleanup-tree-dump "dom1" } } */
+
diff --git a/gcc/tree-ssa-threadedge.c b/gcc/tree-ssa-threadedge.c
index a76a7ce..9807b42 100644
--- a/gcc/tree-ssa-threadedge.c
+++ b/gcc/tree-ssa-threadedge.c
@@ -542,16 +542,26 @@ simplify_control_stmt_condition (edge e,
       /* Get the current value of both operands.  */
       if (TREE_CODE (op0) == SSA_NAME)
 	{
-          tree tmp = SSA_NAME_VALUE (op0);
-	  if (tmp)
-	    op0 = tmp;
+	  for (int i = 0; i < 2; i++)
+	    {
+	      if (TREE_CODE (op0) == SSA_NAME
+		  && SSA_NAME_VALUE (op0))
+		op0 = SSA_NAME_VALUE (op0);
+	      else
+		break;
+	    }
 	}
 
       if (TREE_CODE (op1) == SSA_NAME)
 	{
-	  tree tmp = SSA_NAME_VALUE (op1);
-	  if (tmp)
-	    op1 = tmp;
+	  for (int i = 0; i < 2; i++)
+	    {
+	      if (TREE_CODE (op1) == SSA_NAME
+		  && SSA_NAME_VALUE (op1))
+		op1 = SSA_NAME_VALUE (op1);
+	      else
+		break;
+	    }
 	}
 
       if (handle_dominating_asserts)
@@ -625,10 +635,17 @@ simplify_control_stmt_condition (edge e,
 	 It is possible to get loops in the SSA_NAME_VALUE chains
 	 (consider threading the backedge of a loop where we have
 	 a loop invariant SSA_NAME used in the condition.  */
-      if (cached_lhs
-	  && TREE_CODE (cached_lhs) == SSA_NAME
-	  && SSA_NAME_VALUE (cached_lhs))
-	cached_lhs = SSA_NAME_VALUE (cached_lhs);
+      if (cached_lhs)
+	{
+	  for (int i = 0; i < 2; i++)
+	    {
+	      if (TREE_CODE (cached_lhs) == SSA_NAME
+		  && SSA_NAME_VALUE (cached_lhs))
+		cached_lhs = SSA_NAME_VALUE (cached_lhs);
+	      else
+		break;
+	    }
+	}
 
       /* If we're dominated by a suitable ASSERT_EXPR, then
 	 update CACHED_LHS appropriately.  */

             reply	other threads:[~2014-06-30  6:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-30  6:56 Jeff Law [this message]
2014-07-07  9:02 ` Richard Biener
2014-07-18  5:06   ` Jeff Law

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=53B10A08.4000306@redhat.com \
    --to=law@redhat.com \
    --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).