[PATCHv2] Improve factor_out_conditional_operation for conversions and constants
Checks
Commit Message
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; we were trying to prevent
an extra sign/zero extend from being moved away from definition which no-op
conversions are not.
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-39.c: New test.
---
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c | 43 ++++++++++++++++++++++
gcc/tree-ssa-phiopt.cc | 16 ++++++--
2 files changed, 56 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
Comments
On Tue, Oct 24, 2023 at 8:45 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; we were trying to prevent
> an extra sign/zero extend from being moved away from definition which no-op
> conversions are not.
>
> Bootstrapped and tested on x86_64-linux-gnu with no regressions.
OK.
> 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-39.c: New test.
> ---
> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c | 43 ++++++++++++++++++++++
> gcc/tree-ssa-phiopt.cc | 16 ++++++--
> 2 files changed, 56 insertions(+), 3 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
> new file mode 100644
> index 00000000000..6b6006a96db
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c
> @@ -0,0 +1,43 @@
> +/* { 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..bb55a4fba33 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))
> {
> @@ -322,8 +324,12 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
> if arg0_def_stmt is the only non-debug stmt in
> 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)
> + etc.). See PR71016.
> + Note no-op conversions don't have this issue as
> + it will not generate any zero/sign extend in that case. */
> + 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)
> {
> @@ -354,6 +360,10 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
> return NULL;
> }
> new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
> +
> + /* Drop the overlow that fold_convert might add. */
> + if (TREE_OVERFLOW (new_arg1))
> + new_arg1 = drop_tree_overflow (new_arg1);
> }
> else
> return NULL;
> --
> 2.34.1
>
new file mode 100644
@@ -0,0 +1,43 @@
+/* { 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" } } */
@@ -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))
{
@@ -322,8 +324,12 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
if arg0_def_stmt is the only non-debug stmt in
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)
+ etc.). See PR71016.
+ Note no-op conversions don't have this issue as
+ it will not generate any zero/sign extend in that case. */
+ 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)
{
@@ -354,6 +360,10 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi,
return NULL;
}
new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1);
+
+ /* Drop the overlow that fold_convert might add. */
+ if (TREE_OVERFLOW (new_arg1))
+ new_arg1 = drop_tree_overflow (new_arg1);
}
else
return NULL;