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  <ja...@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

Reply via email to