Hi,

This patch aims to address the issue 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126690

regtested and bootstrapped on x86_64-linux-gnu. Ok for trunk ?

gcc/ChangeLog:
        PR tree-optimization/126690.
        * match.pd: Add patterns to fold CPROJ when nested inside CPROJ or CABS.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/pr126690.c: New test.
---
 gcc/match.pd                             | 10 +++++
 gcc/testsuite/gcc.dg/tree-ssa/pr126690.c | 52 ++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126690.c

diff --git a/gcc/match.pd b/gcc/match.pd
index c7841cab459..a7bf8092d25 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -10186,6 +10186,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))

+/* cproj(cproj(x)) -> cproj(x).  */
+(simplify
+ (CPROJ (CPROJ @0))
+ (CPROJ @0))
+
+/* cabs(cproj(x)) -> cabs(x).  */
+(simplify
+ (CABS (CPROJ @0))
+ (CABS @0))
+
 (for pows (POW)
      sqrts (SQRT)
      cbrts (CBRT)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126690.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr126690.c
new file mode 100644
index 00000000000..f4125fef6bc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126690.c
@@ -0,0 +1,52 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target c99_runtime } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+__attribute__((noinline))
+_Complex float
+cproj_idempotentf (_Complex float a)
+{
+  return __builtin_cprojf (__builtin_cprojf (a));
+}
+
+__attribute__((noinline))
+_Complex double
+cproj_idempotent (_Complex double a)
+{
+  return __builtin_cproj (__builtin_cproj (a));
+}
+
+__attribute__((noinline))
+_Complex long double
+cproj_idempotentl (_Complex long double a)
+{
+  return __builtin_cprojl (__builtin_cprojl (a));
+}
+
+__attribute__((noinline))
+float
+cabs_absorbs_cprojf (_Complex float a)
+{
+  return __builtin_cabsf (__builtin_cprojf (a));
+}
+
+__attribute__((noinline))
+double
+cabs_absorbs_cproj (_Complex double a)
+{
+  return __builtin_cabs (__builtin_cproj (a));
+}
+
+__attribute__((noinline))
+long double
+cabs_absorbs_cprojl (_Complex long double a)
+{
+  return __builtin_cabsl (__builtin_cprojl (a));
+}
+
+/* { dg-final { scan-tree-dump-not {__builtin_cprojf \(__builtin_cprojf} 
"optimized" } } */
+/* { dg-final { scan-tree-dump-not {__builtin_cproj \(__builtin_cproj} 
"optimized" } } */
+/* { dg-final { scan-tree-dump-not {__builtin_cprojl \(__builtin_cprojl} 
"optimized" } } */
+/* { dg-final { scan-tree-dump-not {__builtin_cabsf \(__builtin_cprojf} 
"optimized" } } */
+/* { dg-final { scan-tree-dump-not {__builtin_cabs \(__builtin_cproj} 
"optimized" } } */
+/* { dg-final { scan-tree-dump-not {__builtin_cabsl \(__builtin_cprojl} 
"optimized" } } */
--
2.34.1

Reply via email to