aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <pinskia@gmail.com>2023-10-15 19:15:38 +0000
committerAndrew Pinski <pinskia@gmail.com>2023-10-24 11:18:12 +0000
commit0fc13e8c0e39c51e82deb93f324d9d86ad8d7460 (patch)
tree721e75dd8d73907e3da3fcffe07571304d783644
parent452c4f32373feb6b2c1c1d91b5ec6fe7e7ce0000 (diff)
downloadgcc-0fc13e8c0e39c51e82deb93f324d9d86ad8d7460.zip
gcc-0fc13e8c0e39c51e82deb93f324d9d86ad8d7460.tar.gz
gcc-0fc13e8c0e39c51e82deb93f324d9d86ad8d7460.tar.bz2
Improve factor_out_conditional_operation for conversions and constants
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.
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c43
-rw-r--r--gcc/tree-ssa-phiopt.cc16
2 files changed, 56 insertions, 3 deletions
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 0000000..6b6006a
--- /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 312a6f9..bb55a4f 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;