While doing cleanups on this code I noticed that we do the duplicate
of comparisons at -O0. For C and C++ code this makes no difference as
the gimplifier never produces COND_EXPR. But it could make a difference
for other front-ends.
Oh and for -fno-tree-ter, duplicating the comparison is just a waste
as it is never used for expand.

I also decided to add a few testcases so this is checked in the future.
Even added one for the duplication itself.

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

        PR tree-optimization/116101

gcc/ChangeLog:

        * gimple-isel.cc (maybe_duplicate_comparison): Don't
        do anything for -O0 or -fno-tree-ter.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/dup_compare_cond-1.c: New test.
        * gcc.dg/tree-ssa/dup_compare_cond-2.c: New test.
        * gcc.dg/tree-ssa/dup_compare_cond-3.c: New test.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
 gcc/gimple-isel.cc                            |  5 +++++
 .../gcc.dg/tree-ssa/dup_compare_cond-1.c      | 19 +++++++++++++++++++
 .../gcc.dg/tree-ssa/dup_compare_cond-2.c      | 19 +++++++++++++++++++
 .../gcc.dg/tree-ssa/dup_compare_cond-3.c      | 19 +++++++++++++++++++
 4 files changed, 62 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-3.c

diff --git a/gcc/gimple-isel.cc b/gcc/gimple-isel.cc
index 99bfc937bd5..2817ab659af 100644
--- a/gcc/gimple-isel.cc
+++ b/gcc/gimple-isel.cc
@@ -407,6 +407,11 @@ maybe_duplicate_comparison (gassign *stmt, basic_block bb)
   tree lhs = gimple_assign_lhs (stmt);
   unsigned cnt = 0;
 
+  /* This is should not be used for -O0 nor it is not useful
+     when ter is turned off. */
+  if (!optimize || !flag_tree_ter)
+    return;
+
   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
     {
       if (is_gimple_debug (USE_STMT (use_p)))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-1.c
new file mode 100644
index 00000000000..0321a60b34f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-1.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-fgimple -O0 -fdump-tree-optimized " } */
+/* PR tree-optimization/116101 */
+
+int __GIMPLE() f(int a, int b, int c, int d, int e)
+{
+  _Bool t;
+  int ff;
+  int gg;
+  int res;
+  t = a == b;
+  ff = t ? a : e;
+  gg = t ? d : b;
+  res = ff+gg;
+  return res;
+}
+
+/* At -O0 we should not duplicate the comparison. */
+/* { dg-final { scan-tree-dump-times " == " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-2.c
new file mode 100644
index 00000000000..07e2175c612
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-2.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-fgimple -O2 -fdump-tree-optimized " } */
+/* PR middle-end/105715 */
+
+int __GIMPLE() f(int a, int b, int c, int d, int e)
+{
+  _Bool t;
+  int ff;
+  int gg;
+  int res;
+  t = a == b;
+  ff = t ? a : e;
+  gg = t ? d : b;
+  res = ff+gg;
+  return res;
+}
+
+/* At -O2 we should have duplicate the comparison. */
+/* { dg-final { scan-tree-dump-times " == " 2 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-3.c
new file mode 100644
index 00000000000..88bf19795e0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/dup_compare_cond-3.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-fgimple -O2 -fno-tree-ter -fdump-tree-optimized " } */
+/* PR tree-optimization/116101 */
+
+int __GIMPLE() f(int a, int b, int c, int d, int e)
+{
+  _Bool t;
+  int ff;
+  int gg;
+  int res;
+  t = a == b;
+  ff = t ? a : e;
+  gg = t ? d : b;
+  res = ff+gg;
+  return res;
+}
+
+/* With -fno-tree-ter it is not useful to duplicate the comparison. */
+/* { dg-final { scan-tree-dump-times " == " 1 "optimized" } } */
-- 
2.43.0

Reply via email to