public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [match.pd] PR83750 - CSE erf/erfc pair
@ 2021-10-18 10:05 Prathamesh Kulkarni
  2021-10-18 10:48 ` Richard Biener
  0 siblings, 1 reply; 16+ messages in thread
From: Prathamesh Kulkarni @ 2021-10-18 10:05 UTC (permalink / raw)
  To: gcc Patches, Richard Biener

[-- Attachment #1: Type: text/plain, Size: 1029 bytes --]

Hi Richard,
As suggested in PR, I have attached WIP patch that adds two patterns
to match.pd:
erfc(x) --> 1 - erf(x) if canonicalize_math_p() and,
1 - erf(x) --> erfc(x) if !canonicalize_math_p().

This works to remove call to erfc for the following test:
double f(double x)
{
  double g(double, double);

  double t1 = __builtin_erf (x);
  double t2 = __builtin_erfc (x);
  return g(t1, t2);
}

with .optimized dump shows:
  t1_2 = __builtin_erf (x_1(D));
  t2_3 = 1.0e+0 - t1_2;

However, for the following test:
double f(double x)
{
  double g(double, double);

  double t1 = __builtin_erfc (x);
  return t1;
}

It canonicalizes erfc(x) to 1 - erf(x), but does not transform 1 -
erf(x) to erfc(x) again
post canonicalization.
-fdump-tree-folding shows that 1 - erf(x) --> erfc(x) gets applied,
but then it tries to
resimplify erfc(x), which fails post canonicalization. So we end up
with erfc(x) transformed to
1 - erf(x) in .optimized dump, which I suppose isn't ideal.
Could you suggest how to proceed ?

Thanks,
Prathamesh

[-- Attachment #2: pr83750-1.txt --]
[-- Type: text/plain, Size: 705 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index a9791ceb74a..217e46ff12c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6147,6 +6147,20 @@ 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 @0))
+  (if (!canonicalize_math_p ())
+   (ERFC @0))))
+
 (match double_value_p
  @0
  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-10-25  9:48 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-18 10:05 [match.pd] PR83750 - CSE erf/erfc pair Prathamesh Kulkarni
2021-10-18 10:48 ` Richard Biener
2021-10-18 11:30   ` Prathamesh Kulkarni
2021-10-18 11:40     ` Richard Biener
2021-10-18 11:45       ` Prathamesh Kulkarni
2021-10-18 11:53         ` Richard Biener
2021-10-19  7:01           ` Prathamesh Kulkarni
2021-10-19  7:32             ` Richard Biener
2021-10-19 11:09               ` Prathamesh Kulkarni
2021-10-19 11:25                 ` Richard Biener
2021-10-20 11:59                   ` Prathamesh Kulkarni
2021-10-20 12:51                     ` Richard Biener
2021-10-22  7:47                       ` Prathamesh Kulkarni
2021-10-22  9:26                         ` Richard Biener
2021-10-22 10:51                           ` Prathamesh Kulkarni
2021-10-25  9:48                             ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).