diff --git a/gcc/match.pd b/gcc/match.pd index a9791ceb74a..827670fb59b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6147,6 +6147,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (floors tree_expr_nonnegative_p@0) (truncs @0)))) +/* Simplify, + erfc(x) -> 1 - erf(x) if canonicalize_math_p(). + 1 - erf(x) -> erfc(x) if !canonicalize_math_p(). */ + +(if (flag_unsafe_math_optimizations) + (simplify + (ERFC @0) + (if (canonicalize_math_p ()) + (minus { build_one_cst (TREE_TYPE (@0)); } (ERF @0)))) + (simplify + (minus real_onep (ERF@1 @0)) + (if (!canonicalize_math_p () && single_use (@1)) + (ERFC @0)))) + /* (x + 1) - erf(x) -> x + (1 - erf(x)). */ + (simplify + (minus (plus @0 real_onep) (ERF @0)) + (plus @0 (minus { build_one_cst (TREE_TYPE (@0)); } (ERF @0)))) + (match double_value_p @0 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c new file mode 100644 index 00000000000..a1d456c0119 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized -funsafe-math-optimizations -fno-math-errno" } */ + +double f(double x) +{ + double g (double, double); + double t1 = __builtin_erf (x); + double t2 = __builtin_erfc (x); + return g (t1, t2); +} + +/* { dg-final { scan-tree-dump-not "__builtin_erfc" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c new file mode 100644 index 00000000000..a55825bca47 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-2.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized -funsafe-math-optimizations -fno-math-errno" } */ + +double f(double x) +{ + return __builtin_erfc (x); +} + +/* { dg-final { scan-tree-dump-not "__builtin_erf \\(" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83750-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-3.c new file mode 100644 index 00000000000..3305cdf125c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83750-3.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized -funsafe-math-optimizations -fno-math-errno" } */ + +double f1(double x) +{ + return x + __builtin_erfc (x); +} + +double f2(double x) +{ + return __builtin_erfc (x) + x; +} + +/* { dg-final { scan-tree-dump-not "__builtin_erf \\(" "optimized"} } */