https://gcc.gnu.org/g:a2e06b7f081a3d2e50e3afa8d3f1676a05099707

commit r15-4236-ga2e06b7f081a3d2e50e3afa8d3f1676a05099707
Author: Jennifer Schmitz <jschm...@nvidia.com>
Date:   Thu Oct 3 04:46:51 2024 -0700

    match.pd: Check trunc_mod vector obtap before folding.
    
    This patch guards the simplification x / y * y == x -> x % y == 0 in
    match.pd by a check for:
    1) Non-vector mode of x OR
    2) Lack of support for vector division OR
    3) Support of vector modulo
    
    The patch was bootstrapped and tested with no regression on
    aarch64-linux-gnu and x86_64-linux-gnu.
    OK for mainline?
    
    Signed-off-by: Jennifer Schmitz <jschm...@nvidia.com>
    
    gcc/
            PR tree-optimization/116831
            * match.pd: Guard simplification to trunc_mod with check for
            mod optab support.
    
    gcc/testsuite/
            PR tree-optimization/116831
            * gcc.dg/torture/pr116831.c: New test.

Diff:
---
 gcc/match.pd                            |  9 +++++++--
 gcc/testsuite/gcc.dg/torture/pr116831.c | 10 ++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 755ed13e77d1..8a7569ce3871 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5415,8 +5415,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* x / y * y == x -> x % y == 0.  */
 (simplify
   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
-  (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
-    (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
+  (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
+       && (!VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (@0)))
+          || !target_supports_op_p (TREE_TYPE (@0), TRUNC_DIV_EXPR,
+                                    optab_vector)
+          || target_supports_op_p (TREE_TYPE (@0), TRUNC_MOD_EXPR,
+                                   optab_vector)))
+   (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
 
 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
 (for op (plus minus)
diff --git a/gcc/testsuite/gcc.dg/torture/pr116831.c 
b/gcc/testsuite/gcc.dg/torture/pr116831.c
new file mode 100644
index 000000000000..92b2a130e69f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr116831.c
@@ -0,0 +1,10 @@
+/* { dg-additional-options "-mcpu=neoverse-v2" { target aarch64*-*-* } } */
+
+long a;
+int b, c;
+void d (int e[][5], short f[][5][5][5]) 
+{
+  for (short g; g; g += 4)
+    a = c ?: e[6][0] % b ? 0 : f[0][0][0][g];
+}
+

Reply via email to