public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: Fix up sqrt (sqrt (x)) simplification [PR109301]
@ 2023-03-28  8:25 Jakub Jelinek
  2023-03-28  8:57 ` Richard Biener
  0 siblings, 1 reply; 6+ messages in thread
From: Jakub Jelinek @ 2023-03-28  8:25 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

The following testcase ICEs since the sincos and vect pass order has
been swapped.  It is not valid to replace vector sqrt (sqrt (x)) with
pow (x, 0.25) because build_real on vector type is invalid (could be
handled by using build_uniform_cst and adjusting type passed to
build_real) but more importantly because nothing checks if we can
actually do vector pow.
While we have pow_optab, apparently no target defines it, so it doesn't
seem to be worth bothering with for now and the patch just punts on
non-scalar sqrts.
I think the other simplifications next to it are fine, as they mostly
use CBRT which doesn't even have internal function (so is a builtin
only and therefore always scalar), or have already pow in the IL (which
doesn't have optab and shouldn't be thus vector either).
It is true that with <bits/math-vector.h> we do vectorize some calls to
pow or cbrt (but don't handle others strangely), but those aren't using
internal functions but simd clones and so match.pd doesn't know anything
about those (at least for now).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2023-03-28  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/109301
	* match.pd (sqrt (sqrt (x)) -> pow (x, 0.25)): Only simplify for
	scalar floating point types.

	* gcc.dg/pr109301.c: New test.

--- gcc/match.pd.jj	2023-03-27 10:25:40.171676370 +0200
+++ gcc/match.pd	2023-03-27 21:03:38.816171522 +0200
@@ -6820,7 +6820,8 @@ (define_operator_list SYNC_FETCH_AND_AND
   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
   (simplify
    (sqrts (sqrts @0))
-   (pows @0 { build_real (type, dconst_quarter ()); }))
+   (if (SCALAR_FLOAT_TYPE_P (type))
+    (pows @0 { build_real (type, dconst_quarter ()); })))
   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
   (simplify
    (sqrts (cbrts @0))
--- gcc/testsuite/gcc.dg/pr109301.c.jj	2023-03-27 21:06:22.635806234 +0200
+++ gcc/testsuite/gcc.dg/pr109301.c	2023-03-27 21:06:05.413054906 +0200
@@ -0,0 +1,13 @@
+/* PR tree-optimization/109301 */
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffast-math" } */
+
+double x[256];
+
+void
+foo (void)
+{
+  for (int i = 0; i < 256; ++i)
+    for (int j = 0; j < 8; ++j)
+      x[i] = __builtin_pow (x[i], 0.5);
+}

	Jakub


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

end of thread, other threads:[~2023-03-28 10:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-28  8:25 [PATCH] match.pd: Fix up sqrt (sqrt (x)) simplification [PR109301] Jakub Jelinek
2023-03-28  8:57 ` Richard Biener
2023-03-28  9:15   ` Jakub Jelinek
2023-03-28  9:23     ` Richard Biener
2023-03-28 10:07       ` [PATCH] tree-ssa-math-opts: Move PROP_gimple_opt_math from sincos pass to powcabs [PR109301] Jakub Jelinek
2023-03-28 10:53         ` 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).