Hi!

expand_omp_for_init_counts was using for the case where collapse(2)
inner loop has init expression dependent on non-constant multiple of
the outer iterator and the condition upper bound expression doesn't
depend on the outer iterator fold_unary (NEGATE_EXPR, ...).  This
will just return NULL if it can't be folded, we need fold_build1
instead.

Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk.

2023-01-19  Jakub Jelinek  <ja...@redhat.com>

        PR middle-end/108459
        * omp-expand.cc (expand_omp_for_init_counts): Use fold_build1 rather
        than fold_unary for NEGATE_EXPR.

        * testsuite/libgomp.c/pr108459.c: New test.

--- gcc/omp-expand.cc.jj        2023-01-02 09:32:49.399894958 +0100
+++ gcc/omp-expand.cc   2023-01-19 12:01:05.103410564 +0100
@@ -2003,8 +2003,8 @@ expand_omp_for_init_counts (struct omp_f
            t = fold_build2 (MINUS_EXPR, itype, unshare_expr (fd->loops[i].m2),
                             unshare_expr (fd->loops[i].m1));
          else if (fd->loops[i].m1)
-           t = fold_unary (NEGATE_EXPR, itype,
-                           unshare_expr (fd->loops[i].m1));
+           t = fold_build1 (NEGATE_EXPR, itype,
+                            unshare_expr (fd->loops[i].m1));
          else
            t = unshare_expr (fd->loops[i].m2);
          tree m2minusm1
--- libgomp/testsuite/libgomp.c/pr108459.c.jj   2023-01-19 12:22:07.191038771 
+0100
+++ libgomp/testsuite/libgomp.c/pr108459.c      2023-01-19 12:21:17.973755215 
+0100
@@ -0,0 +1,41 @@
+/* PR middle-end/108459 */
+
+char a[17][17];
+
+__attribute__((noipa)) void
+foo (int x, int y)
+{
+  #pragma omp for collapse(2)
+  for (int i = 1; i <= 16; i++)
+    for (int j = i * x + y; j <= 16; j++)
+      a[i][j] = 1;
+}
+
+int
+main ()
+{
+  #pragma omp parallel
+  foo (1, 1);
+  for (int i = 0; i <= 16; i++)
+    for (int j = 0; j <= 16; j++)
+      if (i >= 1 && j >= i + 1)
+       {
+         if (a[i][j] != 1)
+           __builtin_abort ();
+         a[i][j] = 0;
+       }
+      else if (a[i][j])
+       __builtin_abort ();
+  #pragma omp parallel
+  foo (2, -2);
+  for (int i = 0; i <= 16; i++)
+    for (int j = 0; j <= 16; j++)
+      if (i >= 1 && j >= 2 * i - 2)
+       {
+         if (a[i][j] != 1)
+           __builtin_abort ();
+       }
+      else if (a[i][j])
+       __builtin_abort ();
+  return 0;
+}

        Jakub

Reply via email to