diff mbox

[tree-optimization/61607] Look through SSA_NAME_VALUE chains

Message ID 53B10A08.4000306@redhat.com
State New
Headers show

Commit Message

Jeff Law June 30, 2014, 6:56 a.m. UTC
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.
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.

Comments

Richard Biener July 7, 2014, 9:02 a.m. UTC | #1
On Mon, Jun 30, 2014 at 8:56 AM, Jeff Law <law@redhat.com> wrote:
>
> 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.

Hmm, but it shouldn't .... as we walk in dominator order and should
always do set_ssa_name_value (name, ssa_name_value (value))
there shouldn't be any chains.

But maybe I'm missing sth about the lattice used by DOM here ...
(at least none of the callers in DOM care to valueize 'value' before
setting it as value for an SSA name).

Richard.

>  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.
>
>
>
>
>
>
> 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.  */
>
Jeff Law July 17, 2014, 8:37 a.m. UTC | #2
On 07/07/14 03:02, Richard Biener wrote:
> On Mon, Jun 30, 2014 at 8:56 AM, Jeff Law <law@redhat.com> wrote:
>>
>> 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.
>
> Hmm, but it shouldn't .... as we walk in dominator order and should
> always do set_ssa_name_value (name, ssa_name_value (value))
> there shouldn't be any chains.
They're certainly there and always have been.  You can get chains in 
cases where we derive implicit sets, for example as well as due to loops 
in the CFG.


>
> But maybe I'm missing sth about the lattice used by DOM here ...
> (at least none of the callers in DOM care to valueize 'value' before
> setting it as value for an SSA name).
It's not really a lattice in the way you'd see in VRP.  It's just an 
equality relationship with an unwinding stack to allow us to restore 
state from earlier points in the domwalk.



jeff
diff mbox

Patch

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.  */