public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: Punt on optimizing sqrt with incorrect arg type [PR105150]
@ 2022-04-05  4:39 Jakub Jelinek
  2022-04-05  8:50 ` Richard Biener
  0 siblings, 1 reply; 14+ messages in thread
From: Jakub Jelinek @ 2022-04-05  4:39 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

In the following testcase sqrt is declared as unprototyped function
and so it depends on what type has the argument passed to it.
If an argument of incorrect type is passed, the sqrt comparison
simplification can create invalid GIMPLE.

The patch fixes it by punting if there is a mismatch of types.
As I didn't want to reindent 133 lines, the first hunk contains an
ugly hack with if (false).  If you prefer reindentation, I can do that
too.

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

2022-04-05  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/105150
	* match.pd (sqrt (x) cmp real_cst, sqrt (x) cmp sqrt (y)): Punt
	if sqrt operand has incompatible types.

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

--- gcc/match.pd.jj	2022-03-18 18:32:36.000000000 +0100
+++ gcc/match.pd	2022-04-04 19:49:28.621934784 +0200
@@ -4927,6 +4927,10 @@ (define_operator_list SYNC_FETCH_AND_AND
    (simplify
     (cmp (sq @0) REAL_CST@1)
     (switch
+     (if (!types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+      /* Punt if there is a type mismatch.  */
+      (if (false)
+       @1))
      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
       (switch
        /* sqrt(x) < y is always false, if y is negative.  */
@@ -5062,7 +5066,7 @@ (define_operator_list SYNC_FETCH_AND_AND
    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
    (simplify
     (cmp (sq @0) (sq @1))
-      (if (! HONOR_NANS (@0))
+      (if (! HONOR_NANS (@0) && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
 	(cmp @0 @1))))))
 
 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
--- gcc/testsuite/gcc.dg/pr105150.c.jj	2022-04-04 19:53:03.597935060 +0200
+++ gcc/testsuite/gcc.dg/pr105150.c	2022-04-04 19:54:55.734370333 +0200
@@ -0,0 +1,8 @@
+/* PR tree-optimization/105150 */
+/* { dg-options "-w -Ofast" } */
+
+#define A(name) __typeof (__builtin_##name (0)) name (); \
+  float name##1 () { return !name (1); } \
+  double name##2 () { return name (1.0L); }
+#define B(name) A(name) A(name##l)
+B (sqrt)

	Jakub


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

end of thread, other threads:[~2022-04-06 10:30 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-05  4:39 [PATCH] match.pd: Punt on optimizing sqrt with incorrect arg type [PR105150] Jakub Jelinek
2022-04-05  8:50 ` Richard Biener
2022-04-05  9:15   ` Jakub Jelinek
2022-04-05  9:28     ` Richard Biener
2022-04-05 14:21       ` [PATCH] gimple.cc: Adjust gimple_call_builtin_p and gimple_call_combined_fn [PR105150] Jakub Jelinek
2022-04-06  6:13         ` Richard Biener
2022-04-06  7:10           ` Jakub Jelinek
2022-04-06  7:40             ` Richard Biener
2022-04-06  8:41               ` Richard Sandiford
2022-04-06  9:02                 ` Jakub Jelinek
2022-04-06  9:52                   ` Richard Biener
2022-04-06 10:08                     ` Jakub Jelinek
2022-04-06 10:25                       ` Richard Biener
2022-04-06 10:30                       ` Richard Sandiford

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).