From: Philipp Tomsich <[email protected]>

The (convert (abs (convert@1 @0))) -> (abs @0) rule added by
r17-2276-ge2c4fc6b1cff evaluates TYPE_PRECISION on the outer and inner types
after only types_match, so a vectorized abs-of-narrowed chain like

  vect_1 = (vector(2) int) vect_0;
  vect_2 = ABS_EXPR <vect_1>;
  vect_3 = (vector(2) signed long) vect_2;

trips the vector_type tree check when gimple_simplify visits the
outer conversion (ICE during fre). Use element_precision
so the guard is evaluable for vectors, and require target support
for ABS on the narrow vector type before enabling the transform
there.

Bootstrapped and regtested on aarch64-unknown-linux-gnu, x86_64-pc-linux-gnu
and riscv64-unknown-linux-gnu.

        PR tree-optimization/126291

gcc/ChangeLog:

        * match.pd ((trunc)abs (extend x) -> abs (x)): Use
        element_precision. Require target ABS support for the
        vector case.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/pr126291.c: New test.
---
 gcc/match.pd                                |  4 +++-
 gcc/testsuite/gcc.target/aarch64/pr126291.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/pr126291.c

diff --git a/gcc/match.pd b/gcc/match.pd
index cb8c68bd9150..9306659ecb44 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -9531,7 +9531,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (optimize
       && !HONOR_SNANS (@1)
       && types_match (type, TREE_TYPE (@0))
-      && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@1)))
+      && element_precision (type) < element_precision (TREE_TYPE (@1))
+      && (!VECTOR_TYPE_P (type)
+         || target_supports_op_p (type, ABS_EXPR, optab_default)))
    (abs @0)))
 
 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
diff --git a/gcc/testsuite/gcc.target/aarch64/pr126291.c 
b/gcc/testsuite/gcc.target/aarch64/pr126291.c
new file mode 100644
index 000000000000..f65fc4a0025f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr126291.c
@@ -0,0 +1,20 @@
+/* PR tree-optimization/126291 */
+/* { dg-do compile } */
+/* { dg-options "-O3 -funsafe-math-optimizations -mcpu=ampere1a" } */
+
+double
+f (const double *p, double x, double y, long n)
+{
+  double best = 1e30;
+  for (long i = 0; i < n; i++)
+    {
+      int dx = p[2 * i] - x;
+      int dy = p[2 * i + 1] - y;
+      double ax = __builtin_abs (dx);
+      double ay = __builtin_abs (dy);
+      double d = ax * ax + ay * ay;
+      if (d < best)
+       best = d;
+    }
+  return best;
+}
-- 
2.55.0

Reply via email to