Improve factor_out_conditional_operation for conversions and constants

Message ID 20231016000138.2235395-1-pinskia@gmail.com
State Accepted
Headers
Series Improve factor_out_conditional_operation for conversions and constants |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski Oct. 16, 2023, 12:01 a.m. UTC
  In the case of a NOP conversion (precisions of the 2 types are equal),
factoring out the conversion can be done even if int_fits_type_p returns
false and even when the conversion is defined by a statement inside the
conditional. Since it is a NOP conversion there is no zero/sign extending
happening which is why it is ok to be done here.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	PR tree-optimization/104376
	PR tree-optimization/101541
	* tree-ssa-phiopt.cc (factor_out_conditional_operation):
	Allow nop conversions even if it is defined by a statement
	inside the conditional.

gcc/testsuite/ChangeLog:

	PR tree-optimization/101541
	* gcc.dg/tree-ssa/phi-opt-38.c: New test.
---
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c | 44 ++++++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                     |  8 +++-
 2 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
  

Comments

Richard Biener Oct. 16, 2023, 9:53 a.m. UTC | #1
On Mon, Oct 16, 2023 at 2:02 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> In the case of a NOP conversion (precisions of the 2 types are equal),
> factoring out the conversion can be done even if int_fits_type_p returns
> false and even when the conversion is defined by a statement inside the
> conditional. Since it is a NOP conversion there is no zero/sign extending
> happening which is why it is ok to be done here.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> gcc/ChangeLog:
>
>         PR tree-optimization/104376
>         PR tree-optimization/101541
>         * tree-ssa-phiopt.cc (factor_out_conditional_operation):
>         Allow nop conversions even if it is defined by a statement
>         inside the conditional.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/101541
>         * gcc.dg/tree-ssa/phi-opt-38.c: New test.
> ---
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c | 44 ++++++++++++++++++++++
>  gcc/tree-ssa-phiopt.cc                     |  8 +++-
>  2 files changed, 50 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
> new file mode 100644
> index 00000000000..ca04d1619e6
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
> @@ -0,0 +1,44 @@
> +/* { dg-options "-O2 -fdump-tree-phiopt" } */
> +
> +unsigned f0(int A)
> +{
> +//     A == 0? A : -A    same as -A
> +  if (A == 0)  return A;
> +  return -A;
> +}
> +
> +unsigned f1(int A)
> +{
> +//     A != 0? A : -A    same as A
> +  if (A != 0)  return A;
> +  return -A;
> +}
> +unsigned f2(int A)
> +{
> +//     A >= 0? A : -A    same as abs (A)
> +  if (A >= 0)  return A;
> +  return -A;
> +}
> +unsigned f3(int A)
> +{
> +//     A > 0?  A : -A    same as abs (A)
> +  if (A > 0)  return A;
> +  return -A;
> +}
> +unsigned f4(int A)
> +{
> +//     A <= 0? A : -A    same as -abs (A)
> +  if (A <= 0)  return A;
> +  return -A;
> +}
> +unsigned f5(int A)
> +{
> +//     A < 0?  A : -A    same as -abs (A)
> +  if (A < 0)  return A;
> +  return -A;
> +}
> +
> +/* f4 and f5 are not allowed to be optimized in early phi-opt. */
> +/* { dg-final { scan-tree-dump-times "if" 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */
> +
> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
> index 312a6f9082b..0ab8fad5898 100644
> --- a/gcc/tree-ssa-phiopt.cc
> +++ b/gcc/tree-ssa-phiopt.cc
> @@ -310,7 +310,9 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
>         return NULL;
>        /* If arg1 is an INTEGER_CST, fold it to new type.  */
>        if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
> -         && int_fits_type_p (arg1, TREE_TYPE (new_arg0)))
> +         && (int_fits_type_p (arg1, TREE_TYPE (new_arg0))
> +             || TYPE_PRECISION (TREE_TYPE (new_arg0))
> +                 == TYPE_PRECISION (TREE_TYPE (arg1))))

can you add parens for auto-indent?

>         {
>           if (gimple_assign_cast_p (arg0_def_stmt))
>             {
> @@ -323,7 +325,9 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
>                  its basic block, because then it is possible this
>                  could enable further optimizations (minmax replacement
>                  etc.).  See PR71016.  */

Doesn't the comment also apply for equal precision?

> -             if (new_arg0 != gimple_cond_lhs (cond_stmt)
> +             if (TYPE_PRECISION (TREE_TYPE (new_arg0))
> +                   != TYPE_PRECISION (TREE_TYPE (arg1))
> +                 && new_arg0 != gimple_cond_lhs (cond_stmt)
>                   && new_arg0 != gimple_cond_rhs (cond_stmt)
>                   && gimple_bb (arg0_def_stmt) == e0->src)
>                 {

When we later fold_convert () I think you want to drop TREE_OVERFLOW
which we eventually add.

Otherwise OK I think.

Richard.

> --
> 2.34.1
>
  

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
new file mode 100644
index 00000000000..ca04d1619e6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c
@@ -0,0 +1,44 @@ 
+/* { dg-options "-O2 -fdump-tree-phiopt" } */
+
+unsigned f0(int A)
+{
+//     A == 0? A : -A    same as -A
+  if (A == 0)  return A;
+  return -A;
+}
+
+unsigned f1(int A)
+{
+//     A != 0? A : -A    same as A
+  if (A != 0)  return A;
+  return -A;
+}
+unsigned f2(int A)
+{
+//     A >= 0? A : -A    same as abs (A)
+  if (A >= 0)  return A;
+  return -A;
+}
+unsigned f3(int A)
+{
+//     A > 0?  A : -A    same as abs (A)
+  if (A > 0)  return A;
+  return -A;
+}
+unsigned f4(int A)
+{
+//     A <= 0? A : -A    same as -abs (A)
+  if (A <= 0)  return A;
+  return -A;
+}
+unsigned f5(int A)
+{
+//     A < 0?  A : -A    same as -abs (A)
+  if (A < 0)  return A;
+  return -A;
+}
+
+/* f4 and f5 are not allowed to be optimized in early phi-opt. */
+/* { dg-final { scan-tree-dump-times "if" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */
+
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 312a6f9082b..0ab8fad5898 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -310,7 +310,9 @@  factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
 	return NULL;
       /* If arg1 is an INTEGER_CST, fold it to new type.  */
       if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0))
-	  && int_fits_type_p (arg1, TREE_TYPE (new_arg0)))
+	  && (int_fits_type_p (arg1, TREE_TYPE (new_arg0))
+	      || TYPE_PRECISION (TREE_TYPE (new_arg0))
+		  == TYPE_PRECISION (TREE_TYPE (arg1))))
 	{
 	  if (gimple_assign_cast_p (arg0_def_stmt))
 	    {
@@ -323,7 +325,9 @@  factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
 		 its basic block, because then it is possible this
 		 could enable further optimizations (minmax replacement
 		 etc.).  See PR71016.  */
-	      if (new_arg0 != gimple_cond_lhs (cond_stmt)
+	      if (TYPE_PRECISION (TREE_TYPE (new_arg0))
+		    != TYPE_PRECISION (TREE_TYPE (arg1))
+	          && new_arg0 != gimple_cond_lhs (cond_stmt)
 		  && new_arg0 != gimple_cond_rhs (cond_stmt)
 		  && gimple_bb (arg0_def_stmt) == e0->src)
 		{