[gcc r17-2530] match.pd: Make the (trunc)abs(extend x) guard vector-safe [PR126291]

Philipp Tomsich via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:6d5d980f76c3057cc90b1343a679e7eea3ad2a50

commit r17-2530-g6d5d980f76c3057cc90b1343a679e7eea3ad2a50
Author: Philipp Tomsich <[email protected]>
Date:   Thu Jul 16 06:45:03 2026 +0200

    match.pd: Make the (trunc)abs(extend x) guard vector-safe [PR126291]
    
    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.

Diff:
---
 gcc/match.pd                                |  4 +++-
 gcc/testsuite/gcc.target/aarch64/pr126291.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index d750438382e9..b3738fdde1da 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -9541,7 +9541,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;
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.