[4/4] VN: Lookup `val != 0` if we got back val when looking up the predicate for GIMPLE_COND [PR117414]

Message ID 20241102150921.3319089-4-quic_apinski@quicinc.com
State Committed
Commit 684e5ae90b64c3481f8a5cb7b9517daf79c78ab4
Headers
Series [1/4] VN: Factor out inserting predicates for conditional |

Commit Message

Andrew Pinski Nov. 2, 2024, 3:09 p.m. UTC
  Sometimes we get back a full ssa name when looking up the comparison of the GIMPLE_COND
rather than a predicate. We then want to lookup the `val != 0` for the predicate.

Note this might happen with other boolean assignments and COND_EXPR but I am not sure
if it is as important; I have not found a testcase yet.

Bootstrapped and tested on x86_64-linux-gnu.

	PR tree-optimization/117414

gcc/ChangeLog:

	* tree-ssa-sccvn.cc (process_bb): Lookup
	`val != 0` if got back a ssa name when looking the comparison.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/fre-predicated-4.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 .../gcc.dg/tree-ssa/fre-predicated-4.c        | 38 +++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                         | 10 +++++
 2 files changed, 48 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
  

Comments

Richard Biener Nov. 6, 2024, 12:57 p.m. UTC | #1
On Sat, Nov 2, 2024 at 4:10 PM Andrew Pinski <quic_apinski@quicinc.com> wrote:
>
> Sometimes we get back a full ssa name when looking up the comparison of the GIMPLE_COND
> rather than a predicate. We then want to lookup the `val != 0` for the predicate.
>
> Note this might happen with other boolean assignments and COND_EXPR but I am not sure
> if it is as important; I have not found a testcase yet.
>
> Bootstrapped and tested on x86_64-linux-gnu.

OK.

Thanks,
Richard.

>         PR tree-optimization/117414
>
> gcc/ChangeLog:
>
>         * tree-ssa-sccvn.cc (process_bb): Lookup
>         `val != 0` if got back a ssa name when looking the comparison.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/fre-predicated-4.c: New test.
>
> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> ---
>  .../gcc.dg/tree-ssa/fre-predicated-4.c        | 38 +++++++++++++++++++
>  gcc/tree-ssa-sccvn.cc                         | 10 +++++
>  2 files changed, 48 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
> new file mode 100644
> index 00000000000..fe9d2e2fb58
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
> @@ -0,0 +1,38 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +/* PR tree-optimization/117414 */
> +
> +/* Fre1 should figure out that `*aaa != 0`
> +   For f0 and f1. */
> +
> +
> +void foo();
> +int f0(int *aaa, int j, int t)
> +{
> +  int b = *aaa;
> +  if (b == 0 || t == 1)
> +    return 0;
> +  for(int i = 0; i < j; i++)
> +  {
> +    if (!*aaa) foo();
> +  }
> +  return 0;
> +}
> +
> +int f1(int *aaa, int j, int t)
> +{
> +  int b = *aaa;
> +  if (b == 0)
> +    return 0;
> +  if (t == 1)
> +    return 0;
> +  for(int i = 0; i < j; i++)
> +  {
> +    if (!*aaa) foo();
> +  }
> +  return 0;
> +}
> +
> +/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */
> +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
> diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
> index 190b7d24f1a..c8d40697560 100644
> --- a/gcc/tree-ssa-sccvn.cc
> +++ b/gcc/tree-ssa-sccvn.cc
> @@ -8149,6 +8149,16 @@ process_bb (rpo_elim &avail, basic_block bb,
>                 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
>                                                 boolean_type_node, ops,
>                                                 &vnresult);
> +               /* Got back a ssa name, then try looking up `val != 0`
> +                  as it might have been recorded that way.  */
> +               if (val && TREE_CODE (val) == SSA_NAME)
> +                 {
> +                   ops[0] = val;
> +                   ops[1] = build_zero_cst (TREE_TYPE (val));
> +                   val = vn_nary_op_lookup_pieces (2, NE_EXPR,
> +                                                   boolean_type_node, ops,
> +                                                   &vnresult);
> +                 }
>                 /* Did we get a predicated value?  */
>                 if (! val && vnresult && vnresult->predicated_values)
>                   {
> --
> 2.43.0
>
  

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
new file mode 100644
index 00000000000..fe9d2e2fb58
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
@@ -0,0 +1,38 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* PR tree-optimization/117414 */
+
+/* Fre1 should figure out that `*aaa != 0`
+   For f0 and f1. */
+
+
+void foo();
+int f0(int *aaa, int j, int t)
+{
+  int b = *aaa;
+  if (b == 0 || t == 1)
+    return 0;
+  for(int i = 0; i < j; i++)
+  {
+    if (!*aaa) foo();
+  }
+  return 0;
+}
+
+int f1(int *aaa, int j, int t)
+{
+  int b = *aaa;
+  if (b == 0)
+    return 0;
+  if (t == 1)
+    return 0;
+  for(int i = 0; i < j; i++)
+  {
+    if (!*aaa) foo();
+  }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 190b7d24f1a..c8d40697560 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -8149,6 +8149,16 @@  process_bb (rpo_elim &avail, basic_block bb,
 		val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
 						boolean_type_node, ops,
 						&vnresult);
+		/* Got back a ssa name, then try looking up `val != 0`
+		   as it might have been recorded that way.  */
+		if (val && TREE_CODE (val) == SSA_NAME)
+		  {
+		    ops[0] = val;
+		    ops[1] = build_zero_cst (TREE_TYPE (val));
+		    val = vn_nary_op_lookup_pieces (2, NE_EXPR,
+						    boolean_type_node, ops,
+						    &vnresult);
+		  }
 		/* Did we get a predicated value?  */
 		if (! val && vnresult && vnresult->predicated_values)
 		  {