This is just a small optimization for the case where the real and imag
parts are the same when lowering complex addition/subtraction. We only
need to do the addition once when the real and imag parts are the same (on
both sides of the operator). This gets done later on by FRE/PRE/DOM but
having it done soon allows the cabs lowering to remove the sqrt and
just change it to a multiply by a constant.

Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

        * tree-complex.cc (expand_complex_addition): If both
        operands have the same real and imag parts, only
        add the addition once.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/complex-8.c: New test.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
 gcc/testsuite/gcc.dg/tree-ssa/complex-8.c | 12 ++++++++++++
 gcc/tree-complex.cc                       |  7 ++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/complex-8.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c 
b/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c
new file mode 100644
index 00000000000..a9636ff9e9a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/complex-8.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-cplxlower1-raw" } */
+
+_Complex double f(double a, double c)
+{
+  _Complex double d = __builtin_complex (a, a);
+  d+=__builtin_complex(c, c);
+  return d;
+}
+
+/* There should only be one plus as (a+c) is still (a+c) */
+/* { dg-final { scan-tree-dump-times "plus_expr, " 1 "cplxlower1" } } */
diff --git a/gcc/tree-complex.cc b/gcc/tree-complex.cc
index 8a879acffca..dfebec18ec3 100644
--- a/gcc/tree-complex.cc
+++ b/gcc/tree-complex.cc
@@ -984,7 +984,12 @@ expand_complex_addition (gimple_stmt_iterator *gsi, tree 
inner_type,
     case PAIR (VARYING, VARYING):
     general:
       rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
-      ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
+      /* (a+ai) + (b+bi) -> (a+b)+(a+b)i
+         small optimization to remove one new statement. */
+      if (operand_equal_p (ar, ai) && operand_equal_p (br, bi))
+       ri = rr;
+      else
+       ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
       break;
 
     default:
-- 
2.43.0

Reply via email to