https://gcc.gnu.org/g:6f3b6a451771cd54c98768e7db3c5d58aab2b6aa

commit r15-3683-g6f3b6a451771cd54c98768e7db3c5d58aab2b6aa
Author: Jennifer Schmitz <jschm...@nvidia.com>
Date:   Thu Sep 5 08:10:02 2024 -0700

    match.pd: Check trunc_mod vector obtap before folding.
    
    In the pattern X - (X / Y) * Y to X % Y, this patch guards the
    simplification for vector types by a check for:
    1) Support of the mod optab for vectors OR
    2) Application before vector lowering for non-VL vectors.
    This is to prevent reverting vectorization of modulo to div/mult/sub
    if the target does not support vector mod optab.
    
    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/116569
            * match.pd: Guard simplification to trunc_mod with check for
            mod optab support.
    
    gcc/testsuite/
            PR tree-optimization/116569
            * gcc.dg/torture/pr116569.c: New test.

Diff:
---
 gcc/match.pd                            |  7 ++++++-
 gcc/testsuite/gcc.dg/torture/pr116569.c | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5566c0e4c41c..4aa610e22708 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -894,7 +894,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* X - (X / Y) * Y is the same as X % Y.  */
 (simplify
  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
- (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
+ (if (INTEGRAL_TYPE_P (type)
+      || (VECTOR_INTEGER_TYPE_P (type)
+         && ((optimize_vectors_before_lowering_p ()
+              && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
+             || target_supports_op_p (type, TRUNC_MOD_EXPR,
+                                      optab_vector))))
   (convert (trunc_mod @0 @1))))
 
 /* x * (1 + y / x) - y -> x - y % x */
diff --git a/gcc/testsuite/gcc.dg/torture/pr116569.c 
b/gcc/testsuite/gcc.dg/torture/pr116569.c
new file mode 100644
index 000000000000..b74c749721bf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr116569.c
@@ -0,0 +1,18 @@
+/* { dg-additional-options "-mcpu=neoverse-v2" { target aarch64*-*-* } } */
+int a;
+short b, c, e;
+long d, f;
+long g (long h)
+{
+  if (h)
+    return h;
+  return d;
+}
+void i (int h[][0][0][0])
+{
+  for (short j; j; j += 3)
+    {
+      a = g(h[1][2] ? 0 : h[1][1][1][1]);
+      b = e ?: f % c;
+    }
+}

Reply via email to