]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match: Restrict "min|max(a+|-c,b+|-c)" folding to integral types [PR125214]
authorPengxuan Zheng <pengxuan.zheng@oss.qualcomm.com>
Thu, 7 May 2026 16:18:02 +0000 (09:18 -0700)
committerPengxuan Zheng <pengxuan.zheng@oss.qualcomm.com>
Thu, 7 May 2026 17:51:13 +0000 (10:51 -0700)
The original patch adding the "min|max(a+|-c,b+|-c) -> min|max(a,b)+|-c" pattern
missed integral type checks and caused some powerpc tests to fail.

PR tree-optimization/125214

gcc/ChangeLog:

* match.pd (min|max(a+|-c,b+|-c)): Add integral type check.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/pr125214.c: New test.

Signed-off-by: Pengxuan Zheng <pengxuan.zheng@oss.qualcomm.com>
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr125214.c [new file with mode: 0644]

index 1a7d820779f4b747baf1bc5fe4627fa140464e74..04faffb760d1ed3c239cf239a52b367796085aa8 100644 (file)
@@ -5010,7 +5010,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for minmax (min max)
  (simplify
   (minmax:c (plus:cs @0 @2) (plus:s @1 @2))
-   (if (TYPE_OVERFLOW_UNDEFINED (type)
+   (if (INTEGRAL_TYPE_P (type)
+       && TYPE_OVERFLOW_UNDEFINED (type)
        && !TYPE_OVERFLOW_SANITIZED (type))
     (plus (minmax @0 @1) @2))))
 
@@ -5018,7 +5019,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for minmax (min max)
  (simplify
   (minmax:c (minus:s @0 @2) (minus:s @1 @2))
-   (if (TYPE_OVERFLOW_UNDEFINED (type)
+   (if (INTEGRAL_TYPE_P (type)
+       && TYPE_OVERFLOW_UNDEFINED (type)
        && !TYPE_OVERFLOW_SANITIZED (type))
     (minus (minmax @0 @1) @2))))
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125214.c b/gcc/testsuite/gcc.dg/tree-ssa/pr125214.c
new file mode 100644 (file)
index 0000000..8101f70
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -fdump-tree-forwprop1-raw" } */
+
+/* "min|max(a+|-c,b+|-c) -> min|max(a,b)+|-c" folding should NOT happen for
+   non-integral types.  */
+
+double
+f (double a, double b, double c)
+{
+  return __builtin_fmax (a + c, b + c);
+}
+
+/* { dg-final { scan-tree-dump-times "plus_expr" 2 "forwprop1" } } */