aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-10-27 09:22:07 +0200
committerJakub Jelinek <jakub@redhat.com>2021-10-27 09:22:07 +0200
commit2084b5f42a4432da8b0625f9c669bf690ec46468 (patch)
treeb600a0ba8491e143aec0a29d2b0a761a02673917 /gcc
parent6b0f35299bd1468ebc13b900a73b7cac6181a2aa (diff)
downloadgcc-2084b5f42a4432da8b0625f9c669bf690ec46468.zip
gcc-2084b5f42a4432da8b0625f9c669bf690ec46468.tar.gz
gcc-2084b5f42a4432da8b0625f9c669bf690ec46468.tar.bz2
openmp: Allow non-rectangular loops with pointer iterators
This patch handles pointer iterators for non-rectangular loops. They are more limited than integral iterators of non-rectangular loops, in particular only var-outer, var-outer + a2, a2 + var-outer or var-outer - a2 can appear in lb or ub where a2 is some integral loop invariant expression, so no e.g. multiplication etc. 2021-10-27 Jakub Jelinek <jakub@redhat.com> gcc/ * omp-expand.c (expand_omp_for_init_counts): Handle non-rectangular iterators with pointer types. (expand_omp_for_init_vars, extract_omp_for_update_vars): Likewise. gcc/c-family/ * c-omp.c (c_omp_check_loop_iv_r): Don't clear 3rd bit for POINTER_PLUS_EXPR. (c_omp_check_nonrect_loop_iv): Handle POINTER_PLUS_EXPR. (c_omp_check_loop_iv): Set kind even if the iterator is non-integral. gcc/testsuite/ * c-c++-common/gomp/loop-8.c: New test. * c-c++-common/gomp/loop-9.c: New test. libgomp/ * testsuite/libgomp.c/loop-26.c: New test. * testsuite/libgomp.c/loop-27.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c-family/c-omp.c18
-rw-r--r--gcc/omp-expand.c166
-rw-r--r--gcc/testsuite/c-c++-common/gomp/loop-8.c10
-rw-r--r--gcc/testsuite/c-c++-common/gomp/loop-9.c38
4 files changed, 186 insertions, 46 deletions
diff --git a/gcc/c-family/c-omp.c b/gcc/c-family/c-omp.c
index 32db226..fad0606 100644
--- a/gcc/c-family/c-omp.c
+++ b/gcc/c-family/c-omp.c
@@ -1358,6 +1358,7 @@ c_omp_check_loop_iv_r (tree *tp, int *walk_subtrees, void *data)
&& TREE_CODE (*tp) != PLUS_EXPR
&& TREE_CODE (*tp) != MINUS_EXPR
&& TREE_CODE (*tp) != MULT_EXPR
+ && TREE_CODE (*tp) != POINTER_PLUS_EXPR
&& !CONVERT_EXPR_P (*tp))
{
*walk_subtrees = 0;
@@ -1477,6 +1478,18 @@ c_omp_check_nonrect_loop_iv (tree *tp, struct c_omp_check_loop_iv_data *d,
}
a2 = integer_zero_node;
break;
+ case POINTER_PLUS_EXPR:
+ a1 = TREE_OPERAND (t, 0);
+ a2 = TREE_OPERAND (t, 1);
+ while (CONVERT_EXPR_P (a1))
+ a1 = TREE_OPERAND (a1, 0);
+ if (DECL_P (a1) && c_omp_is_loop_iterator (a1, d) >= 0)
+ {
+ a2 = TREE_OPERAND (t, 1);
+ t = a1;
+ break;
+ }
+ break;
default:
break;
}
@@ -1599,10 +1612,7 @@ c_omp_check_loop_iv (tree stmt, tree declv, walk_tree_lh lh)
data.fail = true;
}
/* Handle non-rectangular loop nests. */
- if (TREE_CODE (stmt) != OACC_LOOP
- && (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC
- || INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 1))))
- && i > 0)
+ if (TREE_CODE (stmt) != OACC_LOOP && i > 0)
kind = 4;
data.kind = kind;
data.idx = i;
diff --git a/gcc/omp-expand.c b/gcc/omp-expand.c
index 9715ef5..70957a6 100644
--- a/gcc/omp-expand.c
+++ b/gcc/omp-expand.c
@@ -1975,6 +1975,7 @@ expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
break;
if (i == fd->last_nonrect
&& fd->loops[i].outer == fd->last_nonrect - fd->first_nonrect
+ && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
&& !TYPE_UNSIGNED (TREE_TYPE (fd->loops[i].v)))
{
int o = fd->first_nonrect;
@@ -2250,15 +2251,22 @@ expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
gsi2 = gsi_after_labels (cur_bb);
tree n1, n2;
t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
- if (fd->loops[i].m1)
+ if (fd->loops[i].m1 == NULL_TREE)
+ n1 = t;
+ else if (POINTER_TYPE_P (itype))
+ {
+ gcc_assert (integer_onep (fd->loops[i].m1));
+ t = fold_convert (sizetype,
+ unshare_expr (fd->loops[i].n1));
+ n1 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
+ }
+ else
{
n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
n1 = fold_build2 (MULT_EXPR, itype,
vs[i - fd->loops[i].outer], n1);
n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
}
- else
- n1 = t;
n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
true, GSI_SAME_STMT);
if (i < fd->last_nonrect)
@@ -2267,17 +2275,26 @@ expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
expand_omp_build_assign (&gsi2, vs[i], n1);
}
t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
- if (fd->loops[i].m2)
+ if (fd->loops[i].m2 == NULL_TREE)
+ n2 = t;
+ else if (POINTER_TYPE_P (itype))
+ {
+ gcc_assert (integer_onep (fd->loops[i].m2));
+ t = fold_convert (sizetype,
+ unshare_expr (fd->loops[i].n2));
+ n2 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
+ }
+ else
{
n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
n2 = fold_build2 (MULT_EXPR, itype,
vs[i - fd->loops[i].outer], n2);
n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
}
- else
- n2 = t;
n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
true, GSI_SAME_STMT);
+ if (POINTER_TYPE_P (itype))
+ itype = signed_type_for (itype);
if (i == fd->last_nonrect)
{
gcond *cond_stmt
@@ -2295,8 +2312,10 @@ expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
? -1 : 1));
t = fold_build2 (PLUS_EXPR, itype,
fold_convert (itype, fd->loops[i].step), t);
- t = fold_build2 (PLUS_EXPR, itype, t, n2);
- t = fold_build2 (MINUS_EXPR, itype, t, n1);
+ t = fold_build2 (PLUS_EXPR, itype, t,
+ fold_convert (itype, n2));
+ t = fold_build2 (MINUS_EXPR, itype, t,
+ fold_convert (itype, n1));
tree step = fold_convert (itype, fd->loops[i].step);
if (TYPE_UNSIGNED (itype)
&& fd->loops[i].cond_code == GT_EXPR)
@@ -2323,7 +2342,11 @@ expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
gsi2 = gsi_after_labels (e->dest);
tree step = fold_convert (itype,
unshare_expr (fd->loops[i].step));
- t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
+ if (POINTER_TYPE_P (TREE_TYPE (vs[i])))
+ t = fold_build_pointer_plus (vs[i],
+ fold_convert (sizetype, step));
+ else
+ t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
true, GSI_SAME_STMT);
expand_omp_build_assign (&gsi2, vs[i], t);
@@ -2761,17 +2784,22 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
&& !fd->loops[j].non_rect_referenced);
gsi2 = gsi_after_labels (cur_bb);
t = fold_convert (itype, unshare_expr (fd->loops[j].n1));
- if (fd->loops[j].m1)
+ if (fd->loops[j].m1 == NULL_TREE)
+ n1 = rect_p ? build_zero_cst (type) : t;
+ else if (POINTER_TYPE_P (itype))
+ {
+ gcc_assert (integer_onep (fd->loops[j].m1));
+ t = fold_convert (sizetype,
+ unshare_expr (fd->loops[j].n1));
+ n1 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
+ }
+ else
{
n1 = fold_convert (itype, unshare_expr (fd->loops[j].m1));
n1 = fold_build2 (MULT_EXPR, itype,
vs[j - fd->loops[j].outer], n1);
n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
}
- else if (rect_p)
- n1 = build_zero_cst (type);
- else
- n1 = t;
n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
true, GSI_SAME_STMT);
if (j < fd->last_nonrect)
@@ -2780,19 +2808,26 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
expand_omp_build_assign (&gsi2, vs[j], n1);
}
t = fold_convert (itype, unshare_expr (fd->loops[j].n2));
- if (fd->loops[j].m2)
+ if (fd->loops[j].m2 == NULL_TREE)
+ n2 = rect_p ? counts[j] : t;
+ else if (POINTER_TYPE_P (itype))
+ {
+ gcc_assert (integer_onep (fd->loops[j].m2));
+ t = fold_convert (sizetype,
+ unshare_expr (fd->loops[j].n2));
+ n2 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
+ }
+ else
{
n2 = fold_convert (itype, unshare_expr (fd->loops[j].m2));
n2 = fold_build2 (MULT_EXPR, itype,
vs[j - fd->loops[j].outer], n2);
n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
}
- else if (rect_p)
- n2 = counts[j];
- else
- n2 = t;
n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
true, GSI_SAME_STMT);
+ if (POINTER_TYPE_P (itype))
+ itype = signed_type_for (itype);
if (j == fd->last_nonrect)
{
gcond *cond_stmt
@@ -2810,8 +2845,10 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
? -1 : 1));
t = fold_build2 (PLUS_EXPR, itype,
fold_convert (itype, fd->loops[j].step), t);
- t = fold_build2 (PLUS_EXPR, itype, t, n2);
- t = fold_build2 (MINUS_EXPR, itype, t, n1);
+ t = fold_build2 (PLUS_EXPR, itype, t,
+ fold_convert (itype, n2));
+ t = fold_build2 (MINUS_EXPR, itype, t,
+ fold_convert (itype, n1));
tree step = fold_convert (itype, fd->loops[j].step);
if (TYPE_UNSIGNED (itype)
&& fd->loops[j].cond_code == GT_EXPR)
@@ -2853,7 +2890,11 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
{
tree step
= fold_convert (itype, unshare_expr (fd->loops[j].step));
- t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
+ if (POINTER_TYPE_P (vtype))
+ t = fold_build_pointer_plus (vs[j], fold_convert (sizetype,
+ step));
+ else
+ t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
}
t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
true, GSI_SAME_STMT);
@@ -2899,7 +2940,10 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
}
for (int j = fd->last_nonrect; j >= fd->first_nonrect; j--)
{
- tree itype = TREE_TYPE (fd->loops[j].v);
+ tree vtype = TREE_TYPE (fd->loops[j].v);
+ tree itype = vtype;
+ if (POINTER_TYPE_P (itype))
+ itype = signed_type_for (itype);
bool rect_p = (fd->loops[j].m1 == NULL_TREE
&& fd->loops[j].m2 == NULL_TREE
&& !fd->loops[j].non_rect_referenced);
@@ -2910,7 +2954,11 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
tree t2
= fold_convert (itype, unshare_expr (fd->loops[j].step));
t = fold_build2 (MULT_EXPR, itype, t, t2);
- t = fold_build2 (PLUS_EXPR, itype, n1, t);
+ if (POINTER_TYPE_P (vtype))
+ t = fold_build_pointer_plus (n1,
+ fold_convert (sizetype, t));
+ else
+ t = fold_build2 (PLUS_EXPR, itype, n1, t);
}
else if (rect_p)
{
@@ -2918,7 +2966,8 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
t = fold_build2 (MULT_EXPR, itype, t,
fold_convert (itype, fd->loops[j].step));
if (POINTER_TYPE_P (vtype))
- t = fold_build_pointer_plus (fd->loops[j].n1, t);
+ t = fold_build_pointer_plus (fd->loops[j].n1,
+ fold_convert (sizetype, t));
else
t = fold_build2 (PLUS_EXPR, itype, fd->loops[j].n1, t);
}
@@ -2978,12 +3027,23 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
{
tree itype = TREE_TYPE (fd->loops[i].v);
- tree t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
- t = fold_build2 (MULT_EXPR, itype,
- fd->loops[i - fd->loops[i].outer].v, t);
- t = fold_build2 (PLUS_EXPR, itype, t,
- fold_convert (itype,
- unshare_expr (fd->loops[i].n2)));
+ tree t;
+ if (POINTER_TYPE_P (itype))
+ {
+ gcc_assert (integer_onep (fd->loops[i].m2));
+ t = fold_convert (sizetype, unshare_expr (fd->loops[i].n2));
+ t = fold_build_pointer_plus (fd->loops[i - fd->loops[i].outer].v,
+ t);
+ }
+ else
+ {
+ t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
+ t = fold_build2 (MULT_EXPR, itype,
+ fd->loops[i - fd->loops[i].outer].v, t);
+ t = fold_build2 (PLUS_EXPR, itype, t,
+ fold_convert (itype,
+ unshare_expr (fd->loops[i].n2)));
+ }
nonrect_bounds[i] = create_tmp_reg (itype, ".bound");
t = force_gimple_operand_gsi (gsi, t, false,
NULL_TREE, false,
@@ -3065,10 +3125,16 @@ extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
t = l->n1;
if (l->m1)
{
- tree t2
- = fold_build2 (MULT_EXPR, TREE_TYPE (t),
- fd->loops[i + 1 - l->outer].v, l->m1);
- t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
+ if (POINTER_TYPE_P (TREE_TYPE (l->v)))
+ t = fold_build_pointer_plus (fd->loops[i + 1 - l->outer].v,
+ fold_convert (sizetype, t));
+ else
+ {
+ tree t2
+ = fold_build2 (MULT_EXPR, TREE_TYPE (t),
+ fd->loops[i + 1 - l->outer].v, l->m1);
+ t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
+ }
}
t = force_gimple_operand_gsi (&gsi, t,
DECL_P (l->v)
@@ -3116,9 +3182,17 @@ extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
}
if (l->m1)
{
- t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
- fd->loops[i].v);
- t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v), t, l->n1);
+ if (POINTER_TYPE_P (TREE_TYPE (l->v)))
+ t = fold_build_pointer_plus (fd->loops[i].v,
+ fold_convert (sizetype,
+ l->n1));
+ else
+ {
+ t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
+ fd->loops[i].v);
+ t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v),
+ t, l->n1);
+ }
n1 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
false,
GSI_CONTINUE_LINKING);
@@ -3132,10 +3206,18 @@ extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
GSI_CONTINUE_LINKING);
if (l->m2)
{
- t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
- fd->loops[i].v);
- t = fold_build2 (PLUS_EXPR, TREE_TYPE (nonrect_bounds[j]),
- t, unshare_expr (l->n2));
+ if (POINTER_TYPE_P (TREE_TYPE (l->v)))
+ t = fold_build_pointer_plus (fd->loops[i].v,
+ fold_convert (sizetype,
+ l->n2));
+ else
+ {
+ t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
+ fd->loops[i].v);
+ t = fold_build2 (PLUS_EXPR,
+ TREE_TYPE (nonrect_bounds[j]),
+ t, unshare_expr (l->n2));
+ }
n2 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
false,
GSI_CONTINUE_LINKING);
diff --git a/gcc/testsuite/c-c++-common/gomp/loop-8.c b/gcc/testsuite/c-c++-common/gomp/loop-8.c
new file mode 100644
index 0000000..d66bbcd
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/loop-8.c
@@ -0,0 +1,10 @@
+void
+foo (void)
+{
+ int a[1024];
+ int *p, *q;
+ #pragma omp parallel for collapse(2)
+ for (p = &a[0]; p < &a[512]; p++)
+ for (q = p + 64; q < p + 128; q++)
+ ;
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/loop-9.c b/gcc/testsuite/c-c++-common/gomp/loop-9.c
new file mode 100644
index 0000000..a64ad98
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/loop-9.c
@@ -0,0 +1,38 @@
+int *qux (int *);
+
+void
+foo (void)
+{
+ int a[1024];
+ int *p;
+ short *q;
+ __PTRDIFF_TYPE__ r;
+ #pragma omp parallel for collapse(2)
+ for (p = &a[0]; p < &a[512]; p++)
+ for (q = (short *) p + 64; q < (short *) p + 128; q++) /* { dg-error "outer iteration variable 'p' used in initializer expression has type other than 'short int ?\\\*'" } */
+ ;
+ #pragma omp parallel for collapse(2)
+ for (p = &a[0]; p < &a[512]; p++)
+ for (r = &a[32] - p; r < 32; r++) /* { dg-error "initializer expression refers to iteration variable 'p'" } */
+ ;
+ #pragma omp parallel for collapse(2)
+ for (r = 0; r < 64; r++)
+ for (p = &a[0] + r; p < &a[32] + 3 * r; p++) /* { dg-error "initializer expression refers to iteration variable 'r'" } */
+ ;
+}
+
+void
+bar (void)
+{
+ int a[1024];
+ int *p, *q, *r;
+ #pragma omp parallel for collapse(2)
+ for (p = &a[0]; p < &a[512]; p++)
+ for (q = p + (&a[16] - qux (p)); q < &a[32]; q++) /* { dg-error "initializer expression refers to iteration variable 'p'" } */
+ ;
+ #pragma omp parallel for collapse(3)
+ for (p = &a[0]; p < &a[512]; p++)
+ for (q = &a[0]; q < &a[512]; q++)
+ for (r = p; r < q + 32; r++) /* { dg-error "two different outer iteration variables 'p' and 'q' used in a single loop" } */
+ ;
+}