Debian Codegraph @

gcc/omp-general.cc

raw
1 /* General types and functions that are useful for processing of OpenMP,
2 OpenACC and similar directives at various stages of compilation.
3
4 Copyright (C) 2005-2025 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "langhooks.h"
33 #include "omp-general.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "gimplify.h"
37 #include "cgraph.h"
38 #include "alloc-pool.h"
39 #include "symbol-summary.h"
40 #include "tree-pass.h"
41 #include "omp-device-properties.h"
42 #include "tree-iterator.h"
43 #include "data-streamer.h"
44 #include "streamer-hooks.h"
45 #include "opts.h"
46 #include "tree-pretty-print.h"
47
48 enum omp_requires omp_requires_mask;
49
50 /* Find an OMP clause of type KIND within CLAUSES. */
51 tree
52 omp_find_clause (tree clauses, enum omp_clause_code kind)
53 {
54 for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
55 if (OMP_CLAUSE_CODE (clauses) == kind)
56 return clauses;
57
58 return NULL_TREE;
59 }
60
61 /* True if OpenMP should regard this DECL as being a scalar which has Fortran's
62 allocatable or pointer attribute. */
63 bool
64 omp_is_allocatable_or_ptr (tree decl)
65 {
66 return lang_hooks.decls.omp_is_allocatable_or_ptr (decl);
67 }
68
69 /* Check whether this DECL belongs to a Fortran optional argument.
70 With 'for_present_check' set to false, decls which are optional parameters
71 themselve are returned as tree - or a NULL_TREE otherwise. Those decls are
72 always pointers. With 'for_present_check' set to true, the decl for checking
73 whether an argument is present is returned; for arguments with value
74 attribute this is the hidden argument and of BOOLEAN_TYPE. If the decl is
75 unrelated to optional arguments, NULL_TREE is returned. */
76
77 tree
78 omp_check_optional_argument (tree decl, bool for_present_check)
79 {
80 return lang_hooks.decls.omp_check_optional_argument (decl, for_present_check);
81 }
82
83 /* Return true if TYPE is an OpenMP mappable type. */
84
85 bool
86 omp_mappable_type (tree type)
87 {
88 /* Mappable type has to be complete. */
89 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
90 return false;
91 return true;
92 }
93
94 /* True if OpenMP should privatize what this DECL points to rather
95 than the DECL itself. */
96
97 bool
98 omp_privatize_by_reference (tree decl)
99 {
100 return lang_hooks.decls.omp_privatize_by_reference (decl);
101 }
102
103 /* Adjust *COND_CODE and *N2 so that the former is either LT_EXPR or GT_EXPR,
104 given that V is the loop index variable and STEP is loop step. */
105
106 void
107 omp_adjust_for_condition (location_t loc, enum tree_code *cond_code, tree *n2,
108 tree v, tree step)
109 {
110 switch (*cond_code)
111 {
112 case LT_EXPR:
113 case GT_EXPR:
114 break;
115
116 case NE_EXPR:
117 gcc_assert (TREE_CODE (step) == INTEGER_CST);
118 if (TREE_CODE (TREE_TYPE (v)) == INTEGER_TYPE
119 || TREE_CODE (TREE_TYPE (v)) == BITINT_TYPE)
120 {
121 if (integer_onep (step))
122 *cond_code = LT_EXPR;
123 else
124 {
125 gcc_assert (integer_minus_onep (step));
126 *cond_code = GT_EXPR;
127 }
128 }
129 else
130 {
131 tree unit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (v)));
132 gcc_assert (TREE_CODE (unit) == INTEGER_CST);
133 if (tree_int_cst_equal (unit, step))
134 *cond_code = LT_EXPR;
135 else
136 {
137 gcc_assert (wi::neg (wi::to_widest (unit))
138 == wi::to_widest (step));
139 *cond_code = GT_EXPR;
140 }
141 }
142
143 break;
144
145 case LE_EXPR:
146 if (POINTER_TYPE_P (TREE_TYPE (*n2)))
147 {
148 tree unit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (v)));
149 gcc_assert (TREE_CODE (unit) == INTEGER_CST);
150 *n2 = fold_build_pointer_plus_loc (loc, *n2, unit);
151 }
152 else
153 *n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (*n2), *n2,
154 build_int_cst (TREE_TYPE (*n2), 1));
155 *cond_code = LT_EXPR;
156 break;
157 case GE_EXPR:
158 if (POINTER_TYPE_P (TREE_TYPE (*n2)))
159 {
160 tree unit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (v)));
161 gcc_assert (TREE_CODE (unit) == INTEGER_CST);
162 unit = convert_to_ptrofftype_loc (loc, unit);
163 unit = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (unit),
164 unit);
165 *n2 = fold_build_pointer_plus_loc (loc, *n2, unit);
166 }
167 else
168 *n2 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (*n2), *n2,
169 build_int_cst (TREE_TYPE (*n2), 1));
170 *cond_code = GT_EXPR;
171 break;
172 default:
173 gcc_unreachable ();
174 }
175 }
176
177 /* Return the looping step from INCR, extracted from the step of a gimple omp
178 for statement. */
179
180 tree
181 omp_get_for_step_from_incr (location_t loc, tree incr)
182 {
183 tree step;
184 switch (TREE_CODE (incr))
185 {
186 case PLUS_EXPR:
187 step = TREE_OPERAND (incr, 1);
188 break;
189 case POINTER_PLUS_EXPR:
190 step = fold_convert (ssizetype, TREE_OPERAND (incr, 1));
191 break;
192 case MINUS_EXPR:
193 step = TREE_OPERAND (incr, 1);
194 step = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (step), step);
195 break;
196 default:
197 gcc_unreachable ();
198 }
199 return step;
200 }
201
202 /* Extract the header elements of parallel loop FOR_STMT and store
203 them into *FD. */
204
205 void
206 omp_extract_for_data (gomp_for *for_stmt, struct omp_for_data *fd,
207 struct omp_for_data_loop *loops)
208 {
209 tree t, var, *collapse_iter, *collapse_count;
210 tree count = NULL_TREE, iter_type = long_integer_type_node;
211 struct omp_for_data_loop *loop;
212 int i;
213 struct omp_for_data_loop dummy_loop;
214 location_t loc = gimple_location (for_stmt);
215 bool simd = gimple_omp_for_kind (for_stmt) == GF_OMP_FOR_KIND_SIMD;
216 bool distribute = gimple_omp_for_kind (for_stmt)
217 == GF_OMP_FOR_KIND_DISTRIBUTE;
218 bool taskloop = gimple_omp_for_kind (for_stmt)
219 == GF_OMP_FOR_KIND_TASKLOOP;
220 bool order_reproducible = false;
221 tree iterv, countv;
222
223 fd->for_stmt = for_stmt;
224 fd->pre = NULL;
225 fd->have_nowait = distribute || simd;
226 fd->have_ordered = false;
227 fd->have_reductemp = false;
228 fd->have_pointer_condtemp = false;
229 fd->have_scantemp = false;
230 fd->have_nonctrl_scantemp = false;
231 fd->non_rect = false;
232 fd->lastprivate_conditional = 0;
233 fd->tiling = NULL_TREE;
234 fd->collapse = 1;
235 fd->ordered = 0;
236 fd->first_nonrect = -1;
237 fd->last_nonrect = -1;
238 fd->sched_kind = OMP_CLAUSE_SCHEDULE_STATIC;
239 fd->sched_modifiers = 0;
240 fd->chunk_size = NULL_TREE;
241 fd->simd_schedule = false;
242 fd->first_inner_iterations = NULL_TREE;
243 fd->factor = NULL_TREE;
244 fd->adjn1 = NULL_TREE;
245 collapse_iter = NULL;
246 collapse_count = NULL;
247
248 for (t = gimple_omp_for_clauses (for_stmt); t ; t = OMP_CLAUSE_CHAIN (t))
249 switch (OMP_CLAUSE_CODE (t))
250 {
251 case OMP_CLAUSE_NOWAIT:
252 fd->have_nowait = true;
253 break;
254 case OMP_CLAUSE_ORDERED:
255 fd->have_ordered = true;
256 if (OMP_CLAUSE_ORDERED_DOACROSS (t))
257 {
258 if (OMP_CLAUSE_ORDERED_EXPR (t))
259 fd->ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (t));
260 else
261 fd->ordered = -1;
262 }
263 break;
264 case OMP_CLAUSE_SCHEDULE:
265 gcc_assert (!distribute && !taskloop);
266 fd->sched_kind
267 = (enum omp_clause_schedule_kind)
268 (OMP_CLAUSE_SCHEDULE_KIND (t) & OMP_CLAUSE_SCHEDULE_MASK);
269 fd->sched_modifiers = (OMP_CLAUSE_SCHEDULE_KIND (t)
270 & ~OMP_CLAUSE_SCHEDULE_MASK);
271 fd->chunk_size = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (t);
272 fd->simd_schedule = OMP_CLAUSE_SCHEDULE_SIMD (t);
273 break;
274 case OMP_CLAUSE_DIST_SCHEDULE:
275 gcc_assert (distribute);
276 fd->chunk_size = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (t);
277 break;
278 case OMP_CLAUSE_COLLAPSE:
279 fd->collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (t));
280 if (fd->collapse > 1)
281 {
282 collapse_iter = &OMP_CLAUSE_COLLAPSE_ITERVAR (t);
283 collapse_count = &OMP_CLAUSE_COLLAPSE_COUNT (t);
284 }
285 break;
286 case OMP_CLAUSE_TILE:
287 fd->tiling = OMP_CLAUSE_TILE_LIST (t);
288 fd->collapse = list_length (fd->tiling);
289 gcc_assert (fd->collapse);
290 collapse_iter = &OMP_CLAUSE_TILE_ITERVAR (t);
291 collapse_count = &OMP_CLAUSE_TILE_COUNT (t);
292 break;
293 case OMP_CLAUSE__REDUCTEMP_:
294 fd->have_reductemp = true;
295 break;
296 case OMP_CLAUSE_LASTPRIVATE:
297 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (t))
298 fd->lastprivate_conditional++;
299 break;
300 case OMP_CLAUSE__CONDTEMP_:
301 if (POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (t))))
302 fd->have_pointer_condtemp = true;
303 break;
304 case OMP_CLAUSE__SCANTEMP_:
305 fd->have_scantemp = true;
306 if (!OMP_CLAUSE__SCANTEMP__ALLOC (t)
307 && !OMP_CLAUSE__SCANTEMP__CONTROL (t))
308 fd->have_nonctrl_scantemp = true;
309 break;
310 case OMP_CLAUSE_ORDER:
311 /* FIXME: For OpenMP 5.2 this should change to
312 if (OMP_CLAUSE_ORDER_REPRODUCIBLE (t))
313 (with the exception of loop construct but that lowers to
314 no schedule/dist_schedule clauses currently). */
315 if (!OMP_CLAUSE_ORDER_UNCONSTRAINED (t))
316 order_reproducible = true;
317 default:
318 break;
319 }
320
321 if (fd->ordered == -1)
322 fd->ordered = fd->collapse;
323
324 /* For order(reproducible:concurrent) schedule ({dynamic,guided,runtime})
325 we have either the option to expensively remember at runtime how we've
326 distributed work from first loop and reuse that in following loops with
327 the same number of iterations and schedule, or just force static schedule.
328 OpenMP API calls etc. aren't allowed in order(concurrent) bodies so
329 users can't observe it easily anyway. */
330 if (order_reproducible)
331 fd->sched_kind = OMP_CLAUSE_SCHEDULE_STATIC;
332 if (fd->collapse > 1 || fd->tiling)
333 fd->loops = loops;
334 else
335 fd->loops = &fd->loop;
336
337 if (fd->ordered && fd->collapse == 1 && loops != NULL)
338 {
339 fd->loops = loops;
340 iterv = NULL_TREE;
341 countv = NULL_TREE;
342 collapse_iter = &iterv;
343 collapse_count = &countv;
344 }
345
346 /* FIXME: for now map schedule(auto) to schedule(static).
347 There should be analysis to determine whether all iterations
348 are approximately the same amount of work (then schedule(static)
349 is best) or if it varies (then schedule(dynamic,N) is better). */
350 if (fd->sched_kind == OMP_CLAUSE_SCHEDULE_AUTO)
351 {
352 fd->sched_kind = OMP_CLAUSE_SCHEDULE_STATIC;
353 gcc_assert (fd->chunk_size == NULL);
354 }
355 gcc_assert ((fd->collapse == 1 && !fd->tiling) || collapse_iter != NULL);
356 if (taskloop)
357 fd->sched_kind = OMP_CLAUSE_SCHEDULE_RUNTIME;
358 if (fd->sched_kind == OMP_CLAUSE_SCHEDULE_RUNTIME)
359 gcc_assert (fd->chunk_size == NULL);
360 else if (fd->chunk_size == NULL)
361 {
362 /* We only need to compute a default chunk size for ordered
363 static loops and dynamic loops. */
364 if (fd->sched_kind != OMP_CLAUSE_SCHEDULE_STATIC
365 || fd->have_ordered)
366 fd->chunk_size = (fd->sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
367 ? integer_zero_node : integer_one_node;
368 }
369
370 int cnt = fd->ordered ? fd->ordered : fd->collapse;
371 int single_nonrect = -1;
372 tree single_nonrect_count = NULL_TREE;
373 enum tree_code single_nonrect_cond_code = ERROR_MARK;
374 for (i = 1; i < cnt; i++)
375 {
376 tree n1 = gimple_omp_for_initial (for_stmt, i);
377 tree n2 = gimple_omp_for_final (for_stmt, i);
378 if (TREE_CODE (n1) == TREE_VEC)
379 {
380 if (fd->non_rect)
381 {
382 single_nonrect = -1;
383 break;
384 }
385 for (int j = i - 1; j >= 0; j--)
386 if (TREE_VEC_ELT (n1, 0) == gimple_omp_for_index (for_stmt, j))
387 {
388 single_nonrect = j;
389 break;
390 }
391 fd->non_rect = true;
392 }
393 else if (TREE_CODE (n2) == TREE_VEC)
394 {
395 if (fd->non_rect)
396 {
397 single_nonrect = -1;
398 break;
399 }
400 for (int j = i - 1; j >= 0; j--)
401 if (TREE_VEC_ELT (n2, 0) == gimple_omp_for_index (for_stmt, j))
402 {
403 single_nonrect = j;
404 break;
405 }
406 fd->non_rect = true;
407 }
408 }
409 for (i = 0; i < cnt; i++)
410 {
411 if (i == 0
412 && fd->collapse == 1
413 && !fd->tiling
414 && (fd->ordered == 0 || loops == NULL))
415 loop = &fd->loop;
416 else if (loops != NULL)
417 loop = loops + i;
418 else
419 loop = &dummy_loop;
420
421 loop->v = gimple_omp_for_index (for_stmt, i);
422 gcc_assert (SSA_VAR_P (loop->v));
423 gcc_assert (TREE_CODE (TREE_TYPE (loop->v)) == INTEGER_TYPE
424 || TREE_CODE (TREE_TYPE (loop->v)) == BITINT_TYPE
425 || TREE_CODE (TREE_TYPE (loop->v)) == POINTER_TYPE);
426 var = TREE_CODE (loop->v) == SSA_NAME ? SSA_NAME_VAR (loop->v) : loop->v;
427 loop->n1 = gimple_omp_for_initial (for_stmt, i);
428 loop->m1 = NULL_TREE;
429 loop->m2 = NULL_TREE;
430 loop->outer = 0;
431 loop->non_rect_referenced = false;
432 if (TREE_CODE (loop->n1) == TREE_VEC)
433 {
434 for (int j = i - 1; j >= 0; j--)
435 if (TREE_VEC_ELT (loop->n1, 0) == gimple_omp_for_index (for_stmt, j))
436 {
437 loop->outer = i - j;
438 if (loops != NULL)
439 loops[j].non_rect_referenced = true;
440 if (fd->first_nonrect == -1 || fd->first_nonrect > j)
441 fd->first_nonrect = j;
442 break;
443 }
444 gcc_assert (loop->outer);
445 loop->m1 = TREE_VEC_ELT (loop->n1, 1);
446 loop->n1 = TREE_VEC_ELT (loop->n1, 2);
447 fd->non_rect = true;
448 fd->last_nonrect = i;
449 }
450
451 loop->cond_code = gimple_omp_for_cond (for_stmt, i);
452 loop->n2 = gimple_omp_for_final (for_stmt, i);
453 gcc_assert (loop->cond_code != NE_EXPR
454 || (gimple_omp_for_kind (for_stmt)
455 != GF_OMP_FOR_KIND_OACC_LOOP));
456 if (TREE_CODE (loop->n2) == TREE_VEC)
457 {
458 if (loop->outer)
459 gcc_assert (TREE_VEC_ELT (loop->n2, 0)
460 == gimple_omp_for_index (for_stmt, i - loop->outer));
461 else
462 for (int j = i - 1; j >= 0; j--)
463 if (TREE_VEC_ELT (loop->n2, 0) == gimple_omp_for_index (for_stmt, j))
464 {
465 loop->outer = i - j;
466 if (loops != NULL)
467 loops[j].non_rect_referenced = true;
468 if (fd->first_nonrect == -1 || fd->first_nonrect > j)
469 fd->first_nonrect = j;
470 break;
471 }
472 gcc_assert (loop->outer);
473 loop->m2 = TREE_VEC_ELT (loop->n2, 1);
474 loop->n2 = TREE_VEC_ELT (loop->n2, 2);
475 fd->non_rect = true;
476 fd->last_nonrect = i;
477 }
478
479 t = gimple_omp_for_incr (for_stmt, i);
480 gcc_assert (TREE_OPERAND (t, 0) == var);
481 loop->step = omp_get_for_step_from_incr (loc, t);
482
483 omp_adjust_for_condition (loc, &loop->cond_code, &loop->n2, loop->v,
484 loop->step);
485
486 if (simd
487 || (fd->sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
488 && !fd->have_ordered))
489 {
490 if (fd->collapse == 1 && !fd->tiling)
491 iter_type = TREE_TYPE (loop->v);
492 else if (i == 0
493 || TYPE_PRECISION (iter_type)
494 < TYPE_PRECISION (TREE_TYPE (loop->v)))
495 {
496 if (TREE_CODE (iter_type) == BITINT_TYPE
497 || TREE_CODE (TREE_TYPE (loop->v)) == BITINT_TYPE)
498 iter_type
499 = build_bitint_type (TYPE_PRECISION (TREE_TYPE (loop->v)),
500 1);
501 else
502 iter_type
503 = build_nonstandard_integer_type
504 (TYPE_PRECISION (TREE_TYPE (loop->v)), 1);
505 }
506 }
507 else if (iter_type != long_long_unsigned_type_node)
508 {
509 if (POINTER_TYPE_P (TREE_TYPE (loop->v)))
510 iter_type = long_long_unsigned_type_node;
511 else if (TYPE_UNSIGNED (TREE_TYPE (loop->v))
512 && TYPE_PRECISION (TREE_TYPE (loop->v))
513 >= TYPE_PRECISION (iter_type))
514 {
515 tree n;
516
517 if (loop->cond_code == LT_EXPR)
518 n = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (loop->v),
519 loop->n2, loop->step);
520 else
521 n = loop->n1;
522 if (loop->m1
523 || loop->m2
524 || TREE_CODE (n) != INTEGER_CST
525 || tree_int_cst_lt (TYPE_MAX_VALUE (iter_type), n))
526 iter_type = long_long_unsigned_type_node;
527 }
528 else if (TYPE_PRECISION (TREE_TYPE (loop->v))
529 > TYPE_PRECISION (iter_type))
530 {
531 tree n1, n2;
532
533 if (loop->cond_code == LT_EXPR)
534 {
535 n1 = loop->n1;
536 n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (loop->v),
537 loop->n2, loop->step);
538 }
539 else
540 {
541 n1 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (loop->v),
542 loop->n2, loop->step);
543 n2 = loop->n1;
544 }
545 if (loop->m1
546 || loop->m2
547 || TREE_CODE (n1) != INTEGER_CST
548 || TREE_CODE (n2) != INTEGER_CST
549 || !tree_int_cst_lt (TYPE_MIN_VALUE (iter_type), n1)
550 || !tree_int_cst_lt (n2, TYPE_MAX_VALUE (iter_type)))
551 iter_type = long_long_unsigned_type_node;
552 }
553 }
554
555 if (i >= fd->collapse)
556 continue;
557
558 if (collapse_count && *collapse_count == NULL)
559 {
560 if (count && integer_zerop (count))
561 continue;
562 tree n1first = NULL_TREE, n2first = NULL_TREE;
563 tree n1last = NULL_TREE, n2last = NULL_TREE;
564 tree ostep = NULL_TREE;
565 if (loop->m1 || loop->m2)
566 {
567 if (count == NULL_TREE)
568 continue;
569 if (single_nonrect == -1
570 || (loop->m1 && TREE_CODE (loop->m1) != INTEGER_CST)
571 || (loop->m2 && TREE_CODE (loop->m2) != INTEGER_CST)
572 || TREE_CODE (loop->n1) != INTEGER_CST
573 || TREE_CODE (loop->n2) != INTEGER_CST
574 || TREE_CODE (loop->step) != INTEGER_CST)
575 {
576 count = NULL_TREE;
577 continue;
578 }
579 tree var = gimple_omp_for_initial (for_stmt, single_nonrect);
580 tree itype = TREE_TYPE (var);
581 tree first = gimple_omp_for_initial (for_stmt, single_nonrect);
582 t = gimple_omp_for_incr (for_stmt, single_nonrect);
583 ostep = omp_get_for_step_from_incr (loc, t);
584 t = fold_binary (MINUS_EXPR, long_long_unsigned_type_node,
585 single_nonrect_count,
586 build_one_cst (long_long_unsigned_type_node));
587 t = fold_convert (itype, t);
588 first = fold_convert (itype, first);
589 ostep = fold_convert (itype, ostep);
590 tree last = fold_binary (PLUS_EXPR, itype, first,
591 fold_binary (MULT_EXPR, itype, t,
592 ostep));
593 if (TREE_CODE (first) != INTEGER_CST
594 || TREE_CODE (last) != INTEGER_CST)
595 {
596 count = NULL_TREE;
597 continue;
598 }
599 if (loop->m1)
600 {
601 tree m1 = fold_convert (itype, loop->m1);
602 tree n1 = fold_convert (itype, loop->n1);
603 n1first = fold_binary (PLUS_EXPR, itype,
604 fold_binary (MULT_EXPR, itype,
605 first, m1), n1);
606 n1last = fold_binary (PLUS_EXPR, itype,
607 fold_binary (MULT_EXPR, itype,
608 last, m1), n1);
609 }
610 else
611 n1first = n1last = loop->n1;
612 if (loop->m2)
613 {
614 tree n2 = fold_convert (itype, loop->n2);
615 tree m2 = fold_convert (itype, loop->m2);
616 n2first = fold_binary (PLUS_EXPR, itype,
617 fold_binary (MULT_EXPR, itype,
618 first, m2), n2);
619 n2last = fold_binary (PLUS_EXPR, itype,
620 fold_binary (MULT_EXPR, itype,
621 last, m2), n2);
622 }
623 else
624 n2first = n2last = loop->n2;
625 n1first = fold_convert (TREE_TYPE (loop->v), n1first);
626 n2first = fold_convert (TREE_TYPE (loop->v), n2first);
627 n1last = fold_convert (TREE_TYPE (loop->v), n1last);
628 n2last = fold_convert (TREE_TYPE (loop->v), n2last);
629 t = fold_binary (loop->cond_code, boolean_type_node,
630 n1first, n2first);
631 tree t2 = fold_binary (loop->cond_code, boolean_type_node,
632 n1last, n2last);
633 if (t && t2 && integer_nonzerop (t) && integer_nonzerop (t2))
634 /* All outer loop iterators have at least one inner loop
635 iteration. Try to compute the count at compile time. */
636 t = NULL_TREE;
637 else if (t && t2 && integer_zerop (t) && integer_zerop (t2))
638 /* No iterations of the inner loop. count will be set to
639 zero cst below. */;
640 else if (TYPE_UNSIGNED (itype)
641 || t == NULL_TREE
642 || t2 == NULL_TREE
643 || TREE_CODE (t) != INTEGER_CST
644 || TREE_CODE (t2) != INTEGER_CST)
645 {
646 /* Punt (for now). */
647 count = NULL_TREE;
648 continue;
649 }
650 else
651 {
652 /* Some iterations of the outer loop have zero iterations
653 of the inner loop, while others have at least one.
654 In this case, we need to adjust one of those outer
655 loop bounds. If ADJ_FIRST, we need to adjust outer n1
656 (first), otherwise outer n2 (last). */
657 bool adj_first = integer_zerop (t);
658 tree n1 = fold_convert (itype, loop->n1);
659 tree n2 = fold_convert (itype, loop->n2);
660 tree m1 = loop->m1 ? fold_convert (itype, loop->m1)
661 : build_zero_cst (itype);
662 tree m2 = loop->m2 ? fold_convert (itype, loop->m2)
663 : build_zero_cst (itype);
664 t = fold_binary (MINUS_EXPR, itype, n1, n2);
665 t2 = fold_binary (MINUS_EXPR, itype, m2, m1);
666 t = fold_binary (TRUNC_DIV_EXPR, itype, t, t2);
667 t2 = fold_binary (MINUS_EXPR, itype, t, first);
668 t2 = fold_binary (TRUNC_MOD_EXPR, itype, t2, ostep);
669 t = fold_binary (MINUS_EXPR, itype, t, t2);
670 tree n1cur
671 = fold_binary (PLUS_EXPR, itype, n1,
672 fold_binary (MULT_EXPR, itype, m1, t));
673 tree n2cur
674 = fold_binary (PLUS_EXPR, itype, n2,
675 fold_binary (MULT_EXPR, itype, m2, t));
676 t2 = fold_binary (loop->cond_code, boolean_type_node,
677 n1cur, n2cur);
678 tree t3 = fold_binary (MULT_EXPR, itype, m1, ostep);
679 tree t4 = fold_binary (MULT_EXPR, itype, m2, ostep);
680 tree diff;
681 if (adj_first)
682 {
683 tree new_first;
684 if (integer_nonzerop (t2))
685 {
686 new_first = t;
687 n1first = n1cur;
688 n2first = n2cur;
689 if (flag_checking)
690 {
691 t3 = fold_binary (MINUS_EXPR, itype, n1cur, t3);
692 t4 = fold_binary (MINUS_EXPR, itype, n2cur, t4);
693 t3 = fold_binary (loop->cond_code,
694 boolean_type_node, t3, t4);
695 gcc_assert (integer_zerop (t3));
696 }
697 }
698 else
699 {
700 t3 = fold_binary (PLUS_EXPR, itype, n1cur, t3);
701 t4 = fold_binary (PLUS_EXPR, itype, n2cur, t4);
702 new_first = fold_binary (PLUS_EXPR, itype, t, ostep);
703 n1first = t3;
704 n2first = t4;
705 if (flag_checking)
706 {
707 t3 = fold_binary (loop->cond_code,
708 boolean_type_node, t3, t4);
709 gcc_assert (integer_nonzerop (t3));
710 }
711 }
712 diff = fold_binary (MINUS_EXPR, itype, new_first, first);
713 first = new_first;
714 fd->adjn1 = first;
715 }
716 else
717 {
718 tree new_last;
719 if (integer_zerop (t2))
720 {
721 t3 = fold_binary (MINUS_EXPR, itype, n1cur, t3);
722 t4 = fold_binary (MINUS_EXPR, itype, n2cur, t4);
723 new_last = fold_binary (MINUS_EXPR, itype, t, ostep);
724 n1last = t3;
725 n2last = t4;
726 if (flag_checking)
727 {
728 t3 = fold_binary (loop->cond_code,
729 boolean_type_node, t3, t4);
730 gcc_assert (integer_nonzerop (t3));
731 }
732 }
733 else
734 {
735 new_last = t;
736 n1last = n1cur;
737 n2last = n2cur;
738 if (flag_checking)
739 {
740 t3 = fold_binary (PLUS_EXPR, itype, n1cur, t3);
741 t4 = fold_binary (PLUS_EXPR, itype, n2cur, t4);
742 t3 = fold_binary (loop->cond_code,
743 boolean_type_node, t3, t4);
744 gcc_assert (integer_zerop (t3));
745 }
746 }
747 diff = fold_binary (MINUS_EXPR, itype, last, new_last);
748 }
749 if (TYPE_UNSIGNED (itype)
750 && single_nonrect_cond_code == GT_EXPR)
751 diff = fold_binary (TRUNC_DIV_EXPR, itype,
752 fold_unary (NEGATE_EXPR, itype, diff),
753 fold_unary (NEGATE_EXPR, itype,
754 ostep));
755 else
756 diff = fold_binary (TRUNC_DIV_EXPR, itype, diff, ostep);
757 diff = fold_convert (long_long_unsigned_type_node, diff);
758 single_nonrect_count
759 = fold_binary (MINUS_EXPR, long_long_unsigned_type_node,
760 single_nonrect_count, diff);
761 t = NULL_TREE;
762 }
763 }
764 else
765 t = fold_binary (loop->cond_code, boolean_type_node,
766 fold_convert (TREE_TYPE (loop->v), loop->n1),
767 fold_convert (TREE_TYPE (loop->v), loop->n2));
768 if (t && integer_zerop (t))
769 count = build_zero_cst (long_long_unsigned_type_node);
770 else if ((i == 0 || count != NULL_TREE)
771 && (TREE_CODE (TREE_TYPE (loop->v)) == INTEGER_TYPE
772 || TREE_CODE (TREE_TYPE (loop->v)) == BITINT_TYPE)
773 && TREE_CONSTANT (loop->n1)
774 && TREE_CONSTANT (loop->n2)
775 && TREE_CODE (loop->step) == INTEGER_CST)
776 {
777 tree itype = TREE_TYPE (loop->v);
778
779 if (POINTER_TYPE_P (itype))
780 itype = signed_type_for (itype);
781 t = build_int_cst (itype, (loop->cond_code == LT_EXPR ? -1 : 1));
782 t = fold_build2 (PLUS_EXPR, itype,
783 fold_convert (itype, loop->step), t);
784 tree n1 = loop->n1;
785 tree n2 = loop->n2;
786 if (loop->m1 || loop->m2)
787 {
788 gcc_assert (single_nonrect != -1);
789 n1 = n1first;
790 n2 = n2first;
791 }
792 t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, n2));
793 t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
794 tree step = fold_convert_loc (loc, itype, loop->step);
795 if (TYPE_UNSIGNED (itype) && loop->cond_code == GT_EXPR)
796 t = fold_build2 (TRUNC_DIV_EXPR, itype,
797 fold_build1 (NEGATE_EXPR, itype, t),
798 fold_build1 (NEGATE_EXPR, itype, step));
799 else
800 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
801 tree llutype = long_long_unsigned_type_node;
802 t = fold_convert (llutype, t);
803 if (loop->m1 || loop->m2)
804 {
805 /* t is number of iterations of inner loop at either first
806 or last value of the outer iterator (the one with fewer
807 iterations).
808 Compute t2 = ((m2 - m1) * ostep) / step
809 and niters = outer_count * t
810 + t2 * ((outer_count - 1) * outer_count / 2)
811 */
812 tree m1 = loop->m1 ? loop->m1 : integer_zero_node;
813 tree m2 = loop->m2 ? loop->m2 : integer_zero_node;
814 m1 = fold_convert (itype, m1);
815 m2 = fold_convert (itype, m2);
816 tree t2 = fold_build2 (MINUS_EXPR, itype, m2, m1);
817 t2 = fold_build2 (MULT_EXPR, itype, t2, ostep);
818 if (TYPE_UNSIGNED (itype) && loop->cond_code == GT_EXPR)
819 t2 = fold_build2 (TRUNC_DIV_EXPR, itype,
820 fold_build1 (NEGATE_EXPR, itype, t2),
821 fold_build1 (NEGATE_EXPR, itype, step));
822 else
823 t2 = fold_build2 (TRUNC_DIV_EXPR, itype, t2, step);
824 t2 = fold_convert (llutype, t2);
825 fd->first_inner_iterations = t;
826 fd->factor = t2;
827 t = fold_build2 (MULT_EXPR, llutype, t,
828 single_nonrect_count);
829 tree t3 = fold_build2 (MINUS_EXPR, llutype,
830 single_nonrect_count,
831 build_one_cst (llutype));
832 t3 = fold_build2 (MULT_EXPR, llutype, t3,
833 single_nonrect_count);
834 t3 = fold_build2 (TRUNC_DIV_EXPR, llutype, t3,
835 build_int_cst (llutype, 2));
836 t2 = fold_build2 (MULT_EXPR, llutype, t2, t3);
837 t = fold_build2 (PLUS_EXPR, llutype, t, t2);
838 }
839 if (i == single_nonrect)
840 {
841 if (integer_zerop (t) || TREE_CODE (t) != INTEGER_CST)
842 count = t;
843 else
844 {
845 single_nonrect_count = t;
846 single_nonrect_cond_code = loop->cond_code;
847 if (count == NULL_TREE)
848 count = build_one_cst (llutype);
849 }
850 }
851 else if (count != NULL_TREE)
852 count = fold_build2 (MULT_EXPR, llutype, count, t);
853 else
854 count = t;
855 if (TREE_CODE (count) != INTEGER_CST)
856 count = NULL_TREE;
857 }
858 else if (count && !integer_zerop (count))
859 count = NULL_TREE;
860 }
861 }
862
863 if (count
864 && !simd
865 && (fd->sched_kind != OMP_CLAUSE_SCHEDULE_STATIC
866 || fd->have_ordered))
867 {
868 if (!tree_int_cst_lt (count, TYPE_MAX_VALUE (long_integer_type_node)))
869 iter_type = long_long_unsigned_type_node;
870 else
871 iter_type = long_integer_type_node;
872 }
873 else if (collapse_iter && *collapse_iter != NULL)
874 iter_type = TREE_TYPE (*collapse_iter);
875 fd->iter_type = iter_type;
876 if (collapse_iter && *collapse_iter == NULL)
877 *collapse_iter = create_tmp_var (iter_type, ".iter");
878 if (collapse_count && *collapse_count == NULL)
879 {
880 if (count)
881 {
882 *collapse_count = fold_convert_loc (loc, iter_type, count);
883 if (fd->first_inner_iterations && fd->factor)
884 {
885 t = make_tree_vec (4);
886 TREE_VEC_ELT (t, 0) = *collapse_count;
887 TREE_VEC_ELT (t, 1) = fd->first_inner_iterations;
888 TREE_VEC_ELT (t, 2) = fd->factor;
889 TREE_VEC_ELT (t, 3) = fd->adjn1;
890 *collapse_count = t;
891 }
892 }
893 else
894 *collapse_count = create_tmp_var (iter_type, ".count");
895 }
896
897 if (fd->collapse > 1 || fd->tiling || (fd->ordered && loops))
898 {
899 fd->loop.v = *collapse_iter;
900 fd->loop.n1 = build_int_cst (TREE_TYPE (fd->loop.v), 0);
901 fd->loop.n2 = *collapse_count;
902 if (TREE_CODE (fd->loop.n2) == TREE_VEC)
903 {
904 gcc_assert (fd->non_rect);
905 fd->first_inner_iterations = TREE_VEC_ELT (fd->loop.n2, 1);
906 fd->factor = TREE_VEC_ELT (fd->loop.n2, 2);
907 fd->adjn1 = TREE_VEC_ELT (fd->loop.n2, 3);
908 fd->loop.n2 = TREE_VEC_ELT (fd->loop.n2, 0);
909 }
910 fd->loop.step = build_int_cst (TREE_TYPE (fd->loop.v), 1);
911 fd->loop.m1 = NULL_TREE;
912 fd->loop.m2 = NULL_TREE;
913 fd->loop.outer = 0;
914 fd->loop.cond_code = LT_EXPR;
915 }
916 else if (loops)
917 loops[0] = fd->loop;
918 }
919
920 /* Build a call to GOMP_barrier. */
921
922 gimple *
923 omp_build_barrier (tree lhs)
924 {
925 tree fndecl = builtin_decl_explicit (lhs ? BUILT_IN_GOMP_BARRIER_CANCEL
926 : BUILT_IN_GOMP_BARRIER);
927 gcall *g = gimple_build_call (fndecl, 0);
928 if (lhs)
929 gimple_call_set_lhs (g, lhs);
930 return g;
931 }
932
933 /* Find OMP_FOR resp. OMP_SIMD with non-NULL OMP_FOR_INIT. Also, fill in pdata
934 array, pdata[0] non-NULL if there is anything non-trivial in between,
935 pdata[1] is address of OMP_PARALLEL in between if any, pdata[2] is address
936 of OMP_FOR in between if any and pdata[3] is address of the inner
937 OMP_FOR/OMP_SIMD. */
938
939 tree
940 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
941 {
942 tree **pdata = (tree **) data;
943 *walk_subtrees = 0;
944 switch (TREE_CODE (*tp))
945 {
946 case OMP_FOR:
947 if (OMP_FOR_INIT (*tp) != NULL_TREE)
948 {
949 pdata[3] = tp;
950 return *tp;
951 }
952 pdata[2] = tp;
953 *walk_subtrees = 1;
954 break;
955 case OMP_SIMD:
956 if (OMP_FOR_INIT (*tp) != NULL_TREE)
957 {
958 pdata[3] = tp;
959 return *tp;
960 }
961 break;
962 case BIND_EXPR:
963 if (BIND_EXPR_VARS (*tp)
964 || (BIND_EXPR_BLOCK (*tp)
965 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
966 pdata[0] = tp;
967 *walk_subtrees = 1;
968 break;
969 case STATEMENT_LIST:
970 if (!tsi_one_before_end_p (tsi_start (*tp)))
971 pdata[0] = tp;
972 *walk_subtrees = 1;
973 break;
974 case TRY_FINALLY_EXPR:
975 case CLEANUP_POINT_EXPR:
976 pdata[0] = tp;
977 *walk_subtrees = 1;
978 break;
979 case OMP_PARALLEL:
980 pdata[1] = tp;
981 *walk_subtrees = 1;
982 break;
983 default:
984 break;
985 }
986 return NULL_TREE;
987 }
988
989 /* Return maximum possible vectorization factor for the target, or for
990 the OpenMP offload target if one exists. */
991
992 poly_uint64
993 omp_max_vf (bool offload)
994 {
995 if (!optimize
996 || optimize_debug
997 || !flag_tree_loop_optimize
998 || (!flag_tree_loop_vectorize
999 && OPTION_SET_P (flag_tree_loop_vectorize)))
1000 return 1;
1001
1002 if (ENABLE_OFFLOADING && offload)
1003 {
1004 for (const char *c = getenv ("OFFLOAD_TARGET_NAMES"); c;)
1005 {
1006 if (startswith (c, "amdgcn"))
1007 return ordered_max (poly_uint64 (64), omp_max_vf (false));
1008 else if ((c = strchr (c, ':')))
1009 c++;
1010 }
1011 /* Otherwise, fall through to host VF. */
1012 }
1013
1014 auto_vector_modes modes;
1015 targetm.vectorize.autovectorize_vector_modes (&modes, true);
1016 if (!modes.is_empty ())
1017 {
1018 poly_uint64 vf = 0;
1019 for (unsigned int i = 0; i < modes.length (); ++i)
1020 /* The returned modes use the smallest element size (and thus
1021 the largest nunits) for the vectorization approach that they
1022 represent. */
1023 vf = ordered_max (vf, GET_MODE_NUNITS (modes[i]));
1024 return vf;
1025 }
1026
1027 machine_mode vqimode = targetm.vectorize.preferred_simd_mode (QImode);
1028 if (GET_MODE_CLASS (vqimode) == MODE_VECTOR_INT)
1029 return GET_MODE_NUNITS (vqimode);
1030
1031 return 1;
1032 }
1033
1034 /* Return maximum SIMT width if offloading may target SIMT hardware. */
1035
1036 int
1037 omp_max_simt_vf (void)
1038 {
1039 if (!optimize)
1040 return 0;
1041 if (ENABLE_OFFLOADING)
1042 for (const char *c = getenv ("OFFLOAD_TARGET_NAMES"); c;)
1043 {
1044 if (startswith (c, "nvptx"))
1045 return 32;
1046 else if ((c = strchr (c, ':')))
1047 c++;
1048 }
1049 return 0;
1050 }
1051
1052 /* Return true if PROP is possibly present in one of the offloading target's
1053 OpenMP contexts. The format of PROPS string is always offloading target's
1054 name terminated by '\0', followed by properties for that offloading
1055 target separated by '\0' and terminated by another '\0'. The strings
1056 are created from omp-device-properties installed files of all configured
1057 offloading targets. */
1058
1059 static bool
1060 omp_offload_device_kind_arch_isa (const char *props, const char *prop)
1061 {
1062 const char *names = getenv ("OFFLOAD_TARGET_NAMES");
1063 if (names == NULL || *names == '\0')
1064 return false;
1065 while (*props != '\0')
1066 {
1067 size_t name_len = strlen (props);
1068 bool matches = false;
1069 for (const char *c = names; c; )
1070 {
1071 if (strncmp (props, c, name_len) == 0
1072 && (c[name_len] == '\0'
1073 || c[name_len] == ':'
1074 || c[name_len] == '='))
1075 {
1076 matches = true;
1077 break;
1078 }
1079 else if ((c = strchr (c, ':')))
1080 c++;
1081 }
1082 props = props + name_len + 1;
1083 while (*props != '\0')
1084 {
1085 if (matches && strcmp (props, prop) == 0)
1086 return true;
1087 props = strchr (props, '\0') + 1;
1088 }
1089 props++;
1090 }
1091 return false;
1092 }
1093
1094 /* Return true if the current code location is or might be offloaded.
1095 Return true in declare target functions, or when nested in a target
1096 region or when unsure, return false otherwise. */
1097
1098 static bool
1099 omp_maybe_offloaded (tree construct_context)
1100 {
1101 /* No offload targets available? */
1102 if (!ENABLE_OFFLOADING)
1103 return false;
1104 const char *names = getenv ("OFFLOAD_TARGET_NAMES");
1105 if (names == NULL || *names == '\0')
1106 return false;
1107
1108 /* Parsing is too early to tell. */
1109 if (symtab->state == PARSING)
1110 /* Maybe. */
1111 return true;
1112
1113 /* Late resolution of offloaded code happens in the offload compiler,
1114 where it's treated as native code instead. So return false here. */
1115 if (cfun && cfun->after_inlining)
1116 return false;
1117
1118 /* Check if the function is marked for offloading (either explicitly
1119 or via omp_discover_implicit_declare_target). */
1120 if (current_function_decl
1121 && lookup_attribute ("omp declare target",
1122 DECL_ATTRIBUTES (current_function_decl)))
1123 return true;
1124
1125 /* Check for nesting inside a target directive. */
1126 for (tree ts = construct_context; ts; ts = TREE_CHAIN (ts))
1127 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_TARGET)
1128 return true;
1129
1130 return false;
1131 }
1132
1133 /* Lookup tables for context selectors. */
1134 const char *omp_tss_map[] =
1135 {
1136 "construct",
1137 "device",
1138 "target_device",
1139 "implementation",
1140 "user",
1141 NULL
1142 };
1143
1144 /* Arrays of property candidates must be null-terminated. */
1145 static const char *const kind_properties[] =
1146 { "host", "nohost", "cpu", "gpu", "fpga", "any", NULL };
1147 static const char *const vendor_properties[] =
1148 { "amd", "arm", "bsc", "cray", "fujitsu", "gnu", "hpe", "ibm", "intel",
1149 "llvm", "nec", "nvidia", "pgi", "ti", "unknown", NULL };
1150 static const char *const extension_properties[] =
1151 { NULL };
1152 static const char *const atomic_default_mem_order_properties[] =
1153 { "seq_cst", "relaxed", "acq_rel", "acquire", "release", NULL };
1154
1155 struct omp_ts_info omp_ts_map[] =
1156 {
1157 { "kind",
1158 (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
1159 OMP_TRAIT_PROPERTY_NAME_LIST, false,
1160 kind_properties
1161 },
1162 { "isa",
1163 (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
1164 OMP_TRAIT_PROPERTY_NAME_LIST, false,
1165 NULL
1166 },
1167 { "arch",
1168 (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
1169 OMP_TRAIT_PROPERTY_NAME_LIST, false,
1170 NULL
1171 },
1172 { "device_num",
1173 (1 << OMP_TRAIT_SET_TARGET_DEVICE),
1174 OMP_TRAIT_PROPERTY_DEV_NUM_EXPR, false,
1175 NULL
1176 },
1177 { "vendor",
1178 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1179 OMP_TRAIT_PROPERTY_NAME_LIST, true,
1180 vendor_properties,
1181 },
1182 { "extension",
1183 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1184 OMP_TRAIT_PROPERTY_NAME_LIST, true,
1185 extension_properties,
1186 },
1187 { "atomic_default_mem_order",
1188 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1189 OMP_TRAIT_PROPERTY_ID, true,
1190 atomic_default_mem_order_properties,
1191 },
1192 { "requires",
1193 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1194 OMP_TRAIT_PROPERTY_CLAUSE_LIST, true,
1195 NULL
1196 },
1197 { "unified_address",
1198 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1199 OMP_TRAIT_PROPERTY_NONE, true,
1200 NULL
1201 },
1202 { "unified_shared_memory",
1203 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1204 OMP_TRAIT_PROPERTY_NONE, true,
1205 NULL
1206 },
1207 { "self_maps",
1208 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1209 OMP_TRAIT_PROPERTY_NONE, true,
1210 NULL
1211 },
1212 { "dynamic_allocators",
1213 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1214 OMP_TRAIT_PROPERTY_NONE, true,
1215 NULL
1216 },
1217 { "reverse_offload",
1218 (1 << OMP_TRAIT_SET_IMPLEMENTATION),
1219 OMP_TRAIT_PROPERTY_NONE, true,
1220 NULL
1221 },
1222 { "condition",
1223 (1 << OMP_TRAIT_SET_USER),
1224 OMP_TRAIT_PROPERTY_BOOL_EXPR, true,
1225 NULL
1226 },
1227 { "target",
1228 (1 << OMP_TRAIT_SET_CONSTRUCT),
1229 OMP_TRAIT_PROPERTY_NONE, false,
1230 NULL
1231 },
1232 { "teams",
1233 (1 << OMP_TRAIT_SET_CONSTRUCT),
1234 OMP_TRAIT_PROPERTY_NONE, false,
1235 NULL
1236 },
1237 { "parallel",
1238 (1 << OMP_TRAIT_SET_CONSTRUCT),
1239 OMP_TRAIT_PROPERTY_NONE, false,
1240 NULL
1241 },
1242 { "for",
1243 (1 << OMP_TRAIT_SET_CONSTRUCT),
1244 OMP_TRAIT_PROPERTY_NONE, false,
1245 NULL
1246 },
1247 { "simd",
1248 (1 << OMP_TRAIT_SET_CONSTRUCT),
1249 OMP_TRAIT_PROPERTY_CLAUSE_LIST, false,
1250 NULL
1251 },
1252 { "dispatch",
1253 (1 << OMP_TRAIT_SET_CONSTRUCT),
1254 OMP_TRAIT_PROPERTY_NONE, false,
1255 NULL
1256 },
1257 { NULL, 0, OMP_TRAIT_PROPERTY_NONE, false, NULL } /* OMP_TRAIT_LAST */
1258 };
1259
1260 /* Return a name from PROP, a property in selectors accepting
1261 name lists. */
1262
1263 const char *
1264 omp_context_name_list_prop (tree prop)
1265 {
1266 gcc_assert (OMP_TP_NAME (prop) == OMP_TP_NAMELIST_NODE);
1267 tree val = OMP_TP_VALUE (prop);
1268 switch (TREE_CODE (val))
1269 {
1270 case IDENTIFIER_NODE:
1271 return IDENTIFIER_POINTER (val);
1272 case STRING_CST:
1273 #ifdef ACCEL_COMPILER
1274 return TREE_STRING_POINTER (val);
1275 #else
1276 {
1277 const char *ret = TREE_STRING_POINTER (val);
1278 if ((size_t) TREE_STRING_LENGTH (val)
1279 == strlen (ret) + (lang_GNU_Fortran () ? 0 : 1))
1280 return ret;
1281 return NULL;
1282 }
1283 #endif
1284 default:
1285 return NULL;
1286 }
1287 }
1288
1289
1290 /* Helper function called via walk_tree, to determine if *TP is a
1291 PARM_DECL. */
1292 static tree
1293 expr_uses_parm_decl (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
1294 void *data ATTRIBUTE_UNUSED)
1295 {
1296 if (TREE_CODE (*tp) == PARM_DECL)
1297 return *tp;
1298 return NULL_TREE;
1299 }
1300
1301 /* Diagnose errors in an OpenMP context selector, return CTX if
1302 it is correct or error_mark_node otherwise. */
1303
1304 tree
1305 omp_check_context_selector (location_t loc, tree ctx,
1306 enum omp_ctx_directive directive)
1307 {
1308 bool tss_seen[OMP_TRAIT_SET_LAST], ts_seen[OMP_TRAIT_LAST];
1309
1310 memset (tss_seen, 0, sizeof (tss_seen));
1311 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
1312 {
1313 enum omp_tss_code tss_code = OMP_TSS_CODE (tss);
1314 bool saw_any_prop = false;
1315 bool saw_other_prop = false;
1316
1317 /* Each trait-set-selector-name can only be specified once. */
1318 if (tss_seen[tss_code])
1319 {
1320 error_at (loc, "selector set %qs specified more than once",
1321 OMP_TSS_NAME (tss));
1322 return error_mark_node;
1323 }
1324 else
1325 tss_seen[tss_code] = true;
1326
1327 memset (ts_seen, 0, sizeof (ts_seen));
1328 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
1329 {
1330 enum omp_ts_code ts_code = OMP_TS_CODE (ts);
1331
1332 /* Ignore unknown traits. */
1333 if (ts_code == OMP_TRAIT_INVALID)
1334 continue;
1335
1336 /* Each trait-selector-name can only be specified once. */
1337 if (ts_seen[ts_code])
1338 {
1339 error_at (loc,
1340 "selector %qs specified more than once in set %qs",
1341 OMP_TS_NAME (ts),
1342 OMP_TSS_NAME (tss));
1343 return error_mark_node;
1344 }
1345 else
1346 ts_seen[ts_code] = true;
1347
1348 /* If trait-property "any" is specified in the "kind"
1349 trait-selector of the "device" selector set or the
1350 "target_device" selector sets, no other trait-property
1351 may be specified in the same selector set. */
1352 if (ts_code == OMP_TRAIT_DEVICE_KIND)
1353 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1354 {
1355 const char *prop = omp_context_name_list_prop (p);
1356 if (!prop)
1357 continue;
1358 else if (strcmp (prop, "any") == 0)
1359 saw_any_prop = true;
1360 else
1361 saw_other_prop = true;
1362 }
1363 /* It seems slightly suspicious that the spec's language covers
1364 the device_num selector too, but
1365 target_device={device_num(whatever),kind(any)}
1366 is probably not terribly useful anyway. */
1367 else if (ts_code == OMP_TRAIT_DEVICE_ARCH
1368 || ts_code == OMP_TRAIT_DEVICE_ISA
1369 || ts_code == OMP_TRAIT_DEVICE_NUM)
1370 saw_other_prop = true;
1371
1372 /* Each trait-property can only be specified once in a trait-selector
1373 other than the construct selector set. FIXME: only handles
1374 name-list properties, not clause-list properties, since the
1375 "requires" selector is not implemented yet (PR 113067). */
1376 if (tss_code != OMP_TRAIT_SET_CONSTRUCT)
1377 for (tree p1 = OMP_TS_PROPERTIES (ts); p1; p1 = TREE_CHAIN (p1))
1378 {
1379 if (OMP_TP_NAME (p1) != OMP_TP_NAMELIST_NODE)
1380 break;
1381 const char *n1 = omp_context_name_list_prop (p1);
1382 if (!n1)
1383 continue;
1384 for (tree p2 = TREE_CHAIN (p1); p2; p2 = TREE_CHAIN (p2))
1385 {
1386 const char *n2 = omp_context_name_list_prop (p2);
1387 if (!n2)
1388 continue;
1389 if (!strcmp (n1, n2))
1390 {
1391 error_at (loc,
1392 "trait-property %qs specified more "
1393 "than once in %qs selector",
1394 n1, OMP_TS_NAME (ts));
1395 return error_mark_node;
1396 }
1397 }
1398 }
1399
1400 /* This restriction is documented in the spec in the section
1401 for the metadirective "when" clause (7.4.1 in the 5.2 spec). */
1402 if (directive == OMP_CTX_METADIRECTIVE
1403 && ts_code == OMP_TRAIT_CONSTRUCT_SIMD
1404 && OMP_TS_PROPERTIES (ts))
1405 {
1406 error_at (loc,
1407 "properties must not be specified for the %<simd%> "
1408 "selector in a %<metadirective%> context-selector");
1409 return error_mark_node;
1410 }
1411
1412 /* "simd" is not allowed at all in "begin declare variant"
1413 selectors. */
1414 if (directive == OMP_CTX_BEGIN_DECLARE_VARIANT
1415 && ts_code == OMP_TRAIT_CONSTRUCT_SIMD)
1416 {
1417 error_at (loc,
1418 "the %<simd%> selector is not permitted in a "
1419 "%<begin declare variant%> context selector");
1420 return error_mark_node;
1421 }
1422
1423 /* Reject expressions that reference parameter variables in
1424 "declare variant", as this is not yet implemented. FIXME;
1425 see PR middle-end/113904. */
1426 if (directive != OMP_CTX_METADIRECTIVE
1427 && (ts_code == OMP_TRAIT_DEVICE_NUM
1428 || ts_code == OMP_TRAIT_USER_CONDITION))
1429 {
1430 tree exp = OMP_TS_PROPERTIES (ts);
1431 if (walk_tree (&exp, expr_uses_parm_decl, NULL, NULL))
1432 {
1433 sorry_at (loc,
1434 "reference to function parameter in "
1435 "%<declare variant%> dynamic selector expression");
1436 return error_mark_node;
1437 }
1438 }
1439
1440 /* Check for unknown properties. */
1441 if (omp_ts_map[ts_code].valid_properties == NULL)
1442 continue;
1443 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1444 for (unsigned j = 0; ; j++)
1445 {
1446 const char *candidate
1447 = omp_ts_map[ts_code].valid_properties[j];
1448 if (candidate == NULL)
1449 {
1450 /* We've reached the end of the candidate array. */
1451 if (ts_code == OMP_TRAIT_IMPLEMENTATION_ADMO)
1452 /* FIXME: not sure why this is an error vs warnings
1453 for the others, + incorrect/unknown wording? */
1454 {
1455 error_at (loc,
1456 "incorrect property %qs of %qs selector",
1457 IDENTIFIER_POINTER (OMP_TP_NAME (p)),
1458 "atomic_default_mem_order");
1459 return error_mark_node;
1460 }
1461 if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE
1462 && (TREE_CODE (OMP_TP_VALUE (p)) == STRING_CST))
1463 warning_at (loc, OPT_Wopenmp,
1464 "unknown property %qE of %qs selector",
1465 OMP_TP_VALUE (p),
1466 OMP_TS_NAME (ts));
1467 else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
1468 warning_at (loc, OPT_Wopenmp,
1469 "unknown property %qs of %qs selector",
1470 omp_context_name_list_prop (p),
1471 OMP_TS_NAME (ts));
1472 else if (OMP_TP_NAME (p))
1473 warning_at (loc, OPT_Wopenmp,
1474 "unknown property %qs of %qs selector",
1475 IDENTIFIER_POINTER (OMP_TP_NAME (p)),
1476 OMP_TS_NAME (ts));
1477 break;
1478 }
1479 else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
1480 /* Property-list traits. */
1481 {
1482 const char *str = omp_context_name_list_prop (p);
1483 if (str && !strcmp (str, candidate))
1484 break;
1485 }
1486 else if (!strcmp (IDENTIFIER_POINTER (OMP_TP_NAME (p)),
1487 candidate))
1488 /* Identifier traits. */
1489 break;
1490 }
1491 }
1492
1493 if (saw_any_prop && saw_other_prop)
1494 {
1495 error_at (loc,
1496 "no other trait-property may be specified "
1497 "in the same selector set with %<kind(\"any\")%>");
1498 return error_mark_node;
1499 }
1500 }
1501 return ctx;
1502 }
1503
1504 /* Forward declarations. */
1505 static int omp_context_selector_set_compare (enum omp_tss_code, tree, tree);
1506 static int omp_construct_simd_compare (tree, tree, bool);
1507
1508 /* Register VARIANT as variant of some base function marked with
1509 #pragma omp declare variant. CONSTRUCT is corresponding list of
1510 trait-selectors for the construct selector set. This is stashed as the
1511 value of the "omp declare variant variant" attribute on VARIANT. */
1512 void
1513 omp_mark_declare_variant (location_t loc, tree variant, tree construct)
1514 {
1515 /* Ignore this variant if it contains unknown construct selectors.
1516 It will never match, and the front ends have already issued a warning
1517 about it. */
1518 for (tree c = construct; c; c = TREE_CHAIN (c))
1519 if (OMP_TS_CODE (c) == OMP_TRAIT_INVALID)
1520 return;
1521
1522 tree attr = lookup_attribute ("omp declare variant variant",
1523 DECL_ATTRIBUTES (variant));
1524 if (attr == NULL_TREE)
1525 {
1526 attr = tree_cons (get_identifier ("omp declare variant variant"),
1527 unshare_expr (construct),
1528 DECL_ATTRIBUTES (variant));
1529 DECL_ATTRIBUTES (variant) = attr;
1530 return;
1531 }
1532 if ((TREE_VALUE (attr) != NULL_TREE) != (construct != NULL_TREE)
1533 || (construct != NULL_TREE
1534 && omp_context_selector_set_compare (OMP_TRAIT_SET_CONSTRUCT,
1535 TREE_VALUE (attr),
1536 construct)))
1537 error_at (loc, "%qD used as a variant with incompatible %<construct%> "
1538 "selector sets", variant);
1539 }
1540
1541
1542 /* Constructors for context selectors. */
1543
1544 tree
1545 make_trait_set_selector (enum omp_tss_code code, tree selectors, tree chain)
1546 {
1547 return tree_cons (build_int_cst (integer_type_node, code),
1548 selectors, chain);
1549 }
1550
1551 tree
1552 make_trait_selector (enum omp_ts_code code, tree score, tree properties,
1553 tree chain)
1554 {
1555 if (score == NULL_TREE)
1556 return tree_cons (build_int_cst (integer_type_node, code),
1557 properties, chain);
1558 else
1559 return tree_cons (build_int_cst (integer_type_node, code),
1560 tree_cons (OMP_TS_SCORE_NODE, score, properties),
1561 chain);
1562 }
1563
1564 tree
1565 make_trait_property (tree name, tree value, tree chain)
1566 {
1567 return tree_cons (name, value, chain);
1568 }
1569
1570 /* Constructor for metadirective variants. */
1571 tree
1572 make_omp_metadirective_variant (tree selector, tree directive, tree body)
1573 {
1574 return build_tree_list (selector, build_tree_list (directive, body));
1575 }
1576
1577 /* If the construct selector traits SELECTOR_TRAITS match the corresponding
1578 OpenMP context traits CONTEXT_TRAITS, return true and set *SCORE to the
1579 corresponding score if it is non-null. */
1580 static bool
1581 omp_construct_traits_match (tree selector_traits, tree context_traits,
1582 score_wide_int *score)
1583 {
1584 int slength = list_length (selector_traits);
1585 int clength = list_length (context_traits);
1586
1587 /* Trivial failure: the selector has more traits than the OpenMP context. */
1588 if (slength > clength)
1589 return false;
1590
1591 /* There's only one trait in the selector and it doesn't have any properties
1592 to match. */
1593 if (slength == 1 && !OMP_TS_PROPERTIES (selector_traits))
1594 {
1595 int p = 0, i = 1;
1596 enum omp_ts_code code = OMP_TS_CODE (selector_traits);
1597 for (tree t = context_traits; t; t = TREE_CHAIN (t), i++)
1598 if (OMP_TS_CODE (t) == code)
1599 p = i;
1600 if (p != 0)
1601 {
1602 if (score)
1603 *score = wi::shifted_mask <score_wide_int> (p - 1, 1, false);
1604 return true;
1605 }
1606 else
1607 return false;
1608 }
1609
1610 /* Now handle the more general cases.
1611 Both lists of traits are ordered from outside in, corresponding to
1612 the c1, ..., cN numbering for the OpenMP context specified in
1613 in section 7.1 of the OpenMP 5.2 spec. Section 7.3 of the spec says
1614 "if the traits that correspond to the construct selector set appear
1615 multiple times in the OpenMP context, the highest valued subset of
1616 context traits that contains all trait selectors in the same order
1617 are used". This means that we want to start the search for a match
1618 from the end of the list, rather than the beginning. To facilitate
1619 that, transfer the lists to temporary arrays to allow random access
1620 to the elements (their order remains outside in). */
1621 int i, j;
1622 tree s, c;
1623
1624 tree *sarray = (tree *) alloca (slength * sizeof (tree));
1625 for (s = selector_traits, i = 0; s; s = TREE_CHAIN (s), i++)
1626 sarray[i] = s;
1627
1628 tree *carray = (tree *) alloca (clength * sizeof (tree));
1629 for (c = context_traits, j = 0; c; c = TREE_CHAIN (c), j++)
1630 carray[j] = c;
1631
1632 /* The variable "i" indexes the selector, "j" indexes the OpenMP context.
1633 Find the "j" corresponding to each sarray[i]. Note that the spec uses
1634 "p" as the 1-based position, but "j" is zero-based, e.g. equal to
1635 p - 1. */
1636 score_wide_int result = 0;
1637 j = clength - 1;
1638 for (i = slength - 1; i >= 0; i--)
1639 {
1640 enum omp_ts_code code = OMP_TS_CODE (sarray[i]);
1641 tree props = OMP_TS_PROPERTIES (sarray[i]);
1642 for (; j >= 0; j--)
1643 {
1644 if (OMP_TS_CODE (carray[j]) != code)
1645 continue;
1646 if (code == OMP_TRAIT_CONSTRUCT_SIMD
1647 && props
1648 && omp_construct_simd_compare (props,
1649 OMP_TS_PROPERTIES (carray[j]),
1650 true) > 0)
1651 continue;
1652 break;
1653 }
1654 /* If j >= 0, we have a match for this trait at position j. */
1655 if (j < 0)
1656 return false;
1657 result += wi::shifted_mask <score_wide_int> (j, 1, false);
1658 j--;
1659 }
1660 if (score)
1661 *score = result;
1662 return true;
1663 }
1664
1665 /* Return 1 if context selector CTX matches the current OpenMP context, 0
1666 if it does not and -1 if it is unknown and need to be determined later.
1667 Some properties can be checked right away during parsing, others need
1668 to wait until the whole TU is parsed, others need to wait until
1669 IPA, others until vectorization.
1670
1671 CONSTRUCT_CONTEXT is a list of construct traits from the OpenMP context,
1672 which must be collected by omp_get_construct_context during
1673 gimplification. It is ignored (and may be null) if this function is
1674 called during parsing. Otherwise COMPLETE_P should indicate whether
1675 CONSTRUCT_CONTEXT is known to be complete and not missing constructs
1676 filled in later during compilation.
1677
1678 Dynamic properties (which are evaluated at run-time) should always
1679 return 1. */
1680
1681 int
1682 omp_context_selector_matches (tree ctx,
1683 tree construct_context,
1684 bool complete_p)
1685 {
1686 int ret = 1;
1687 bool maybe_offloaded = omp_maybe_offloaded (construct_context);
1688
1689 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
1690 {
1691 enum omp_tss_code set = OMP_TSS_CODE (tss);
1692 tree selectors = OMP_TSS_TRAIT_SELECTORS (tss);
1693
1694 /* Immediately reject the match if there are any ignored
1695 selectors present. */
1696 for (tree ts = selectors; ts; ts = TREE_CHAIN (ts))
1697 if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
1698 return 0;
1699
1700 if (set == OMP_TRAIT_SET_CONSTRUCT)
1701 {
1702 /* We cannot resolve the construct selector during parsing because
1703 the OpenMP context (and CONSTRUCT_CONTEXT) isn't available
1704 until gimplification. */
1705 if (symtab->state == PARSING)
1706 {
1707 ret = -1;
1708 continue;
1709 }
1710
1711 gcc_assert (selectors);
1712
1713 /* During gimplification, CONSTRUCT_CONTEXT is partial, and doesn't
1714 include a construct for "declare simd" that may be added
1715 when there is not an enclosing "target" construct. We might
1716 be able to find a positive match against the partial context
1717 (although we cannot yet score it accurately), but if we can't,
1718 treat it as unknown instead of no match. */
1719 if (!omp_construct_traits_match (selectors, construct_context, NULL))
1720 {
1721 /* If we've got a complete context, it's definitely a failed
1722 match. */
1723 if (complete_p)
1724 return 0;
1725
1726 /* If the selector doesn't include simd, then we don't have
1727 to worry about whether "declare simd" would cause it to
1728 match; so this is also a definite failure. */
1729 bool have_simd = false;
1730 for (tree ts = selectors; ts; ts = TREE_CHAIN (ts))
1731 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD)
1732 {
1733 have_simd = true;
1734 break;
1735 }
1736 if (!have_simd)
1737 return 0;
1738 else
1739 ret = -1;
1740 }
1741 continue;
1742 }
1743 else if (set == OMP_TRAIT_SET_TARGET_DEVICE)
1744 /* The target_device set is dynamic, so treat it as always
1745 resolvable. However, the current implementation doesn't
1746 support it in a target region, so diagnose that as an error.
1747 FIXME: maybe make this a warning and return 0 instead? */
1748 {
1749 for (tree ts = construct_context; ts; ts = TREE_CHAIN (ts))
1750 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_TARGET)
1751 sorry ("%<target_device%> selector set inside of %<target%> "
1752 "directive");
1753 continue;
1754 }
1755
1756 for (tree ts = selectors; ts; ts = TREE_CHAIN (ts))
1757 {
1758 enum omp_ts_code sel = OMP_TS_CODE (ts);
1759 switch (sel)
1760 {
1761 case OMP_TRAIT_IMPLEMENTATION_VENDOR:
1762 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1763 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1764 {
1765 const char *prop = omp_context_name_list_prop (p);
1766 if (prop == NULL)
1767 return 0;
1768 if (!strcmp (prop, "gnu"))
1769 continue;
1770 return 0;
1771 }
1772 break;
1773 case OMP_TRAIT_IMPLEMENTATION_EXTENSION:
1774 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1775 /* We don't support any extensions right now. */
1776 return 0;
1777 break;
1778 case OMP_TRAIT_IMPLEMENTATION_ADMO:
1779 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1780 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1781 break;
1782
1783 {
1784 enum omp_memory_order omo
1785 = ((enum omp_memory_order)
1786 (omp_requires_mask
1787 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER));
1788 if (omo == OMP_MEMORY_ORDER_UNSPECIFIED)
1789 {
1790 /* We don't know yet, until end of TU. */
1791 if (symtab->state == PARSING)
1792 {
1793 ret = -1;
1794 break;
1795 }
1796 else
1797 omo = OMP_MEMORY_ORDER_RELAXED;
1798 }
1799 tree p = OMP_TS_PROPERTIES (ts);
1800 const char *prop = IDENTIFIER_POINTER (OMP_TP_NAME (p));
1801 if (!strcmp (prop, "relaxed")
1802 && omo != OMP_MEMORY_ORDER_RELAXED)
1803 return 0;
1804 else if (!strcmp (prop, "seq_cst")
1805 && omo != OMP_MEMORY_ORDER_SEQ_CST)
1806 return 0;
1807 else if (!strcmp (prop, "acq_rel")
1808 && omo != OMP_MEMORY_ORDER_ACQ_REL)
1809 return 0;
1810 else if (!strcmp (prop, "acquire")
1811 && omo != OMP_MEMORY_ORDER_ACQUIRE)
1812 return 0;
1813 else if (!strcmp (prop, "release")
1814 && omo != OMP_MEMORY_ORDER_RELEASE)
1815 return 0;
1816 }
1817 break;
1818 case OMP_TRAIT_DEVICE_ARCH:
1819 gcc_assert (set == OMP_TRAIT_SET_DEVICE);
1820 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1821 {
1822 const char *arch = omp_context_name_list_prop (p);
1823 if (arch == NULL)
1824 return 0;
1825 int r = 0;
1826 if (targetm.omp.device_kind_arch_isa != NULL)
1827 r = targetm.omp.device_kind_arch_isa (omp_device_arch,
1828 arch);
1829 if (r == 0 || (r == -1 && symtab->state != PARSING))
1830 {
1831 /* If we are or might be in a target region or
1832 declare target function, need to take into account
1833 also offloading values.
1834 Note that maybe_offloaded is always false in late
1835 resolution; that's handled as native code (the
1836 above case) in the offload compiler instead. */
1837 if (!maybe_offloaded)
1838 return 0;
1839 if (ENABLE_OFFLOADING)
1840 {
1841 const char *arches = omp_offload_device_arch;
1842 if (omp_offload_device_kind_arch_isa (arches, arch))
1843 {
1844 ret = -1;
1845 continue;
1846 }
1847 }
1848 return 0;
1849 }
1850 else if (r == -1)
1851 ret = -1;
1852 /* If arch matches on the host, it still might not match
1853 in the offloading region. */
1854 else if (maybe_offloaded)
1855 ret = -1;
1856 }
1857 break;
1858 case OMP_TRAIT_IMPLEMENTATION_UNIFIED_ADDRESS:
1859 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1860 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1861 break;
1862
1863 if ((omp_requires_mask & OMP_REQUIRES_UNIFIED_ADDRESS) == 0)
1864 {
1865 if (symtab->state == PARSING)
1866 ret = -1;
1867 else
1868 return 0;
1869 }
1870 break;
1871 case OMP_TRAIT_IMPLEMENTATION_UNIFIED_SHARED_MEMORY:
1872 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1873 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1874 break;
1875
1876 if ((omp_requires_mask
1877 & OMP_REQUIRES_UNIFIED_SHARED_MEMORY) == 0)
1878 {
1879 if (symtab->state == PARSING)
1880 ret = -1;
1881 else
1882 return 0;
1883 }
1884 break;
1885 case OMP_TRAIT_IMPLEMENTATION_SELF_MAPS:
1886 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1887 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1888 break;
1889
1890 if ((omp_requires_mask & OMP_REQUIRES_SELF_MAPS) == 0)
1891 {
1892 if (symtab->state == PARSING)
1893 ret = -1;
1894 else
1895 return 0;
1896 }
1897 break;
1898 case OMP_TRAIT_IMPLEMENTATION_DYNAMIC_ALLOCATORS:
1899 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1900 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1901 break;
1902
1903 if ((omp_requires_mask
1904 & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
1905 {
1906 if (symtab->state == PARSING)
1907 ret = -1;
1908 else
1909 return 0;
1910 }
1911 break;
1912 case OMP_TRAIT_IMPLEMENTATION_REVERSE_OFFLOAD:
1913 gcc_assert (set == OMP_TRAIT_SET_IMPLEMENTATION);
1914 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
1915 break;
1916
1917 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
1918 {
1919 if (symtab->state == PARSING)
1920 ret = -1;
1921 else
1922 return 0;
1923 }
1924 break;
1925 case OMP_TRAIT_DEVICE_KIND:
1926 gcc_assert (set == OMP_TRAIT_SET_DEVICE);
1927 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1928 {
1929 const char *prop = omp_context_name_list_prop (p);
1930 if (prop == NULL)
1931 return 0;
1932 if (!strcmp (prop, "any"))
1933 continue;
1934 if (!strcmp (prop, "host"))
1935 {
1936 #ifdef ACCEL_COMPILER
1937 return 0;
1938 #else
1939 if (maybe_offloaded)
1940 ret = -1;
1941 continue;
1942 #endif
1943 }
1944 if (!strcmp (prop, "nohost"))
1945 {
1946 #ifndef ACCEL_COMPILER
1947 if (maybe_offloaded)
1948 ret = -1;
1949 else
1950 return 0;
1951 #endif
1952 continue;
1953 }
1954
1955 int r = 0;
1956 if (targetm.omp.device_kind_arch_isa != NULL)
1957 r = targetm.omp.device_kind_arch_isa (omp_device_kind,
1958 prop);
1959 else
1960 #ifndef ACCEL_COMPILER
1961 r = strcmp (prop, "cpu") == 0;
1962 #else
1963 gcc_unreachable ();
1964 #endif
1965 if (r == 0 || (r == -1 && symtab->state != PARSING))
1966 {
1967 /* If we are or might be in a target region or
1968 declare target function, need to take into account
1969 also offloading values.
1970 Note that maybe_offloaded is always false in late
1971 resolution; that's handled as native code (the
1972 above case) in the offload compiler instead. */
1973 if (!maybe_offloaded)
1974 return 0;
1975 if (ENABLE_OFFLOADING)
1976 {
1977 const char *kinds = omp_offload_device_kind;
1978 if (omp_offload_device_kind_arch_isa (kinds, prop))
1979 {
1980 ret = -1;
1981 continue;
1982 }
1983 }
1984 return 0;
1985 }
1986 else if (r == -1)
1987 ret = -1;
1988 /* If kind matches on the host, it still might not match
1989 in the offloading region. */
1990 else if (maybe_offloaded)
1991 ret = -1;
1992 }
1993 break;
1994 case OMP_TRAIT_DEVICE_ISA:
1995 gcc_assert (set == OMP_TRAIT_SET_DEVICE);
1996 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
1997 {
1998 const char *isa = omp_context_name_list_prop (p);
1999 if (isa == NULL)
2000 return 0;
2001 int r = 0;
2002 if (targetm.omp.device_kind_arch_isa != NULL)
2003 r = targetm.omp.device_kind_arch_isa (omp_device_isa,
2004 isa);
2005 if (r == 0 || (r == -1 && symtab->state != PARSING))
2006 {
2007 /* If isa is valid on the target, but not in the
2008 current function and current function has
2009 #pragma omp declare simd on it, some simd clones
2010 might have the isa added later on. */
2011 if (r == -1
2012 && targetm.simd_clone.compute_vecsize_and_simdlen
2013 && (cfun == NULL || !cfun->after_inlining))
2014 {
2015 tree attrs
2016 = DECL_ATTRIBUTES (current_function_decl);
2017 if (lookup_attribute ("omp declare simd", attrs))
2018 {
2019 ret = -1;
2020 continue;
2021 }
2022 }
2023 /* If we are or might be in a target region or
2024 declare target function, need to take into account
2025 also offloading values.
2026 Note that maybe_offloaded is always false in late
2027 resolution; that's handled as native code (the
2028 above case) in the offload compiler instead. */
2029 if (!maybe_offloaded)
2030 return 0;
2031 if (ENABLE_OFFLOADING)
2032 {
2033 const char *isas = omp_offload_device_isa;
2034 if (omp_offload_device_kind_arch_isa (isas, isa))
2035 {
2036 ret = -1;
2037 continue;
2038 }
2039 }
2040 return 0;
2041 }
2042 else if (r == -1)
2043 ret = -1;
2044 /* If isa matches on the host, it still might not match
2045 in the offloading region. */
2046 else if (maybe_offloaded)
2047 ret = -1;
2048 }
2049 break;
2050 case OMP_TRAIT_USER_CONDITION:
2051 gcc_assert (set == OMP_TRAIT_SET_USER);
2052 for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
2053 if (OMP_TP_NAME (p) == NULL_TREE)
2054 {
2055 /* If the expression is not a constant, the selector
2056 is dynamic. */
2057 if (!tree_fits_shwi_p (OMP_TP_VALUE (p)))
2058 break;
2059
2060 if (integer_zerop (OMP_TP_VALUE (p)))
2061 return 0;
2062 if (integer_nonzerop (OMP_TP_VALUE (p)))
2063 break;
2064 ret = -1;
2065 }
2066 break;
2067 default:
2068 break;
2069 }
2070 }
2071 }
2072 return ret;
2073 }
2074
2075 /* Helper function for resolve_omp_target_device_matches, also used
2076 directly when we know in advance that the device is the host to avoid
2077 the overhead of late resolution. SEL is the selector code and
2078 PROPERTIES are the properties to match. The return value is a
2079 boolean. */
2080 static bool
2081 omp_target_device_matches_on_host (enum omp_ts_code selector,
2082 tree properties)
2083 {
2084 bool result = 1;
2085
2086 if (dump_file)
2087 fprintf (dump_file, "omp_target_device_matches_on_host:\n");
2088
2089 switch (selector)
2090 {
2091 case OMP_TRAIT_DEVICE_KIND:
2092 for (tree p = properties; p && result; p = TREE_CHAIN (p))
2093 {
2094 const char *prop = omp_context_name_list_prop (p);
2095
2096 if (prop == NULL)
2097 result = 0;
2098 else if (!strcmp (prop, "any"))
2099 ;
2100 else if (!strcmp (prop, "host"))
2101 {
2102 #ifdef ACCEL_COMPILER
2103 result = 0;
2104 #else
2105 ;
2106 #endif
2107 }
2108 else if (!strcmp (prop, "nohost"))
2109 {
2110 #ifdef ACCEL_COMPILER
2111 ;
2112 #else
2113 result = 0;
2114 #endif
2115 }
2116 else if (targetm.omp.device_kind_arch_isa != NULL)
2117 result = targetm.omp.device_kind_arch_isa (omp_device_kind, prop);
2118 else
2119 #ifndef ACCEL_COMPILER
2120 result = strcmp (prop, "cpu") == 0;
2121 #else
2122 gcc_unreachable ();
2123 #endif
2124 if (dump_file)
2125 fprintf (dump_file, "Matching device kind %s = %s\n",
2126 prop, (result ? "true" : "false"));
2127 }
2128 break;
2129 case OMP_TRAIT_DEVICE_ARCH:
2130 if (targetm.omp.device_kind_arch_isa != NULL)
2131 for (tree p = properties; p && result; p = TREE_CHAIN (p))
2132 {
2133 const char *prop = omp_context_name_list_prop (p);
2134 if (prop == NULL)
2135 result = 0;
2136 else
2137 result = targetm.omp.device_kind_arch_isa (omp_device_arch,
2138 prop);
2139 if (dump_file)
2140 fprintf (dump_file, "Matching device arch %s = %s\n",
2141 prop, (result ? "true" : "false"));
2142 }
2143 else
2144 {
2145 result = 0;
2146 if (dump_file)
2147 fprintf (dump_file, "Cannot match device arch on target\n");
2148 }
2149 break;
2150 case OMP_TRAIT_DEVICE_ISA:
2151 if (targetm.omp.device_kind_arch_isa != NULL)
2152 for (tree p = properties; p && result; p = TREE_CHAIN (p))
2153 {
2154 const char *prop = omp_context_name_list_prop (p);
2155 if (prop == NULL)
2156 result = 0;
2157 else
2158 result = targetm.omp.device_kind_arch_isa (omp_device_isa,
2159 prop);
2160 if (dump_file)
2161 fprintf (dump_file, "Matching device isa %s = %s\n",
2162 prop, (result ? "true" : "false"));
2163 }
2164 else
2165 {
2166 result = 0;
2167 if (dump_file)
2168 fprintf (dump_file, "Cannot match device isa on target\n");
2169 }
2170 break;
2171 default:
2172 gcc_unreachable ();
2173 }
2174 return result;
2175 }
2176
2177 /* Called for late resolution of the OMP_TARGET_DEVICE_MATCHES tree node to
2178 a constant in omp-offload.cc. This is used in code that is wrapped in a
2179 #pragma omp target construct to execute on the specified device, and
2180 can be reduced to a compile-time constant in the offload compiler.
2181 NODE is an OMP_TARGET_DEVICE_MATCHES tree node and the result is an
2182 INTEGER_CST. */
2183 tree
2184 resolve_omp_target_device_matches (tree node)
2185 {
2186 tree sel = OMP_TARGET_DEVICE_MATCHES_SELECTOR (node);
2187 enum omp_ts_code selector = (enum omp_ts_code) tree_to_shwi (sel);
2188 tree properties = OMP_TARGET_DEVICE_MATCHES_PROPERTIES (node);
2189 if (omp_target_device_matches_on_host (selector, properties))
2190 return integer_one_node;
2191 else
2192 return integer_zero_node;
2193 }
2194
2195 /* Compare construct={simd} CLAUSES1 with CLAUSES2, return 0/-1/1/2 as
2196 in omp_context_selector_set_compare. If MATCH_P is true, additionally
2197 apply the special matching rules for the "simdlen" and "aligned" clauses
2198 used to determine whether the selector CLAUSES1 is part of matches
2199 the OpenMP context containing CLAUSES2. */
2200
2201 static int
2202 omp_construct_simd_compare (tree clauses1, tree clauses2, bool match_p)
2203 {
2204 if (clauses1 == NULL_TREE)
2205 return clauses2 == NULL_TREE ? 0 : -1;
2206 if (clauses2 == NULL_TREE)
2207 return 1;
2208
2209 int r = 0;
2210 struct declare_variant_simd_data {
2211 bool inbranch, notinbranch;
2212 tree simdlen;
2213 auto_vec<tree,16> data_sharing;
2214 auto_vec<tree,16> aligned;
2215 declare_variant_simd_data ()
2216 : inbranch(false), notinbranch(false), simdlen(NULL_TREE) {}
2217 } data[2];
2218 unsigned int i;
2219 tree e0, e1;
2220 for (i = 0; i < 2; i++)
2221 for (tree c = i ? clauses2 : clauses1; c; c = OMP_CLAUSE_CHAIN (c))
2222 {
2223 vec<tree> *v;
2224 switch (OMP_CLAUSE_CODE (c))
2225 {
2226 case OMP_CLAUSE_INBRANCH:
2227 data[i].inbranch = true;
2228 continue;
2229 case OMP_CLAUSE_NOTINBRANCH:
2230 data[i].notinbranch = true;
2231 continue;
2232 case OMP_CLAUSE_SIMDLEN:
2233 data[i].simdlen = OMP_CLAUSE_SIMDLEN_EXPR (c);
2234 continue;
2235 case OMP_CLAUSE_UNIFORM:
2236 case OMP_CLAUSE_LINEAR:
2237 v = &data[i].data_sharing;
2238 break;
2239 case OMP_CLAUSE_ALIGNED:
2240 v = &data[i].aligned;
2241 break;
2242 default:
2243 gcc_unreachable ();
2244 }
2245 unsigned HOST_WIDE_INT argno = tree_to_uhwi (OMP_CLAUSE_DECL (c));
2246 if (argno >= v->length ())
2247 v->safe_grow_cleared (argno + 1, true);
2248 (*v)[argno] = c;
2249 }
2250 /* Here, r is used as a bitmask, 2 is set if CLAUSES1 has something
2251 CLAUSES2 doesn't, 1 is set if CLAUSES2 has something CLAUSES1
2252 doesn't. Thus, r == 3 implies return value 2, r == 1 implies
2253 -1, r == 2 implies 1 and r == 0 implies 0. */
2254 if (data[0].inbranch != data[1].inbranch)
2255 r |= data[0].inbranch ? 2 : 1;
2256 if (data[0].notinbranch != data[1].notinbranch)
2257 r |= data[0].notinbranch ? 2 : 1;
2258 e0 = data[0].simdlen;
2259 e1 = data[1].simdlen;
2260 if (!simple_cst_equal (e0, e1))
2261 {
2262 if (e0 && e1)
2263 {
2264 if (match_p && tree_fits_uhwi_p (e0) && tree_fits_uhwi_p (e1))
2265 {
2266 /* The two simdlen clauses match if m is a multiple of n. */
2267 unsigned HOST_WIDE_INT n = tree_to_uhwi (e0);
2268 unsigned HOST_WIDE_INT m = tree_to_uhwi (e1);
2269 if (m % n != 0)
2270 return 2;
2271 }
2272 else
2273 return 2;
2274 }
2275 r |= data[0].simdlen ? 2 : 1;
2276 }
2277 if (data[0].data_sharing.length () < data[1].data_sharing.length ()
2278 || data[0].aligned.length () < data[1].aligned.length ())
2279 r |= 1;
2280 tree c1, c2;
2281 FOR_EACH_VEC_ELT (data[0].data_sharing, i, c1)
2282 {
2283 c2 = (i < data[1].data_sharing.length ()
2284 ? data[1].data_sharing[i] : NULL_TREE);
2285 if ((c1 == NULL_TREE) != (c2 == NULL_TREE))
2286 {
2287 r |= c1 != NULL_TREE ? 2 : 1;
2288 continue;
2289 }
2290 if (c1 == NULL_TREE)
2291 continue;
2292 if (OMP_CLAUSE_CODE (c1) != OMP_CLAUSE_CODE (c2))
2293 return 2;
2294 if (OMP_CLAUSE_CODE (c1) != OMP_CLAUSE_LINEAR)
2295 continue;
2296 if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c1)
2297 != OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c2))
2298 return 2;
2299 if (OMP_CLAUSE_LINEAR_KIND (c1) != OMP_CLAUSE_LINEAR_KIND (c2))
2300 return 2;
2301 if (!simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (c1),
2302 OMP_CLAUSE_LINEAR_STEP (c2)))
2303 return 2;
2304 }
2305 FOR_EACH_VEC_ELT (data[0].aligned, i, c1)
2306 {
2307 c2 = i < data[1].aligned.length () ? data[1].aligned[i] : NULL_TREE;
2308 if ((c1 == NULL_TREE) != (c2 == NULL_TREE))
2309 {
2310 r |= c1 != NULL_TREE ? 2 : 1;
2311 continue;
2312 }
2313 if (c1 == NULL_TREE)
2314 continue;
2315 e0 = OMP_CLAUSE_ALIGNED_ALIGNMENT (c1);
2316 e1 = OMP_CLAUSE_ALIGNED_ALIGNMENT (c2);
2317 if (!simple_cst_equal (e0, e1))
2318 {
2319 if (e0 && e1
2320 && match_p && tree_fits_uhwi_p (e0) && tree_fits_uhwi_p (e1))
2321 {
2322 /* The two aligned clauses match if n is a multiple of m. */
2323 unsigned HOST_WIDE_INT n = tree_to_uhwi (e0);
2324 unsigned HOST_WIDE_INT m = tree_to_uhwi (e1);
2325 if (n % m != 0)
2326 return 2;
2327 }
2328 else
2329 return 2;
2330 }
2331 }
2332 switch (r)
2333 {
2334 case 0: return 0;
2335 case 1: return -1;
2336 case 2: return 1;
2337 case 3: return 2;
2338 default: gcc_unreachable ();
2339 }
2340 }
2341
2342 /* Compare properties of selectors SEL from SET other than construct.
2343 CTX1 and CTX2 are the lists of properties to compare.
2344 Return 0/-1/1/2 as in omp_context_selector_set_compare.
2345 Unlike set names or selector names, properties can have duplicates. */
2346
2347 static int
2348 omp_context_selector_props_compare (enum omp_tss_code set,
2349 enum omp_ts_code sel,
2350 tree ctx1, tree ctx2)
2351 {
2352 int ret = 0;
2353 for (int pass = 0; pass < 2; pass++)
2354 for (tree p1 = pass ? ctx2 : ctx1; p1; p1 = TREE_CHAIN (p1))
2355 {
2356 tree p2;
2357 for (p2 = pass ? ctx1 : ctx2; p2; p2 = TREE_CHAIN (p2))
2358 if (OMP_TP_NAME (p1) == OMP_TP_NAME (p2))
2359 {
2360 if (OMP_TP_NAME (p1) == NULL_TREE)
2361 {
2362 if (set == OMP_TRAIT_SET_USER
2363 && sel == OMP_TRAIT_USER_CONDITION)
2364 {
2365 /* Recognize constants that have equal truth values,
2366 otherwise assume all expressions are unique. */
2367 tree v1 = OMP_TP_VALUE (p1);
2368 tree v2 = OMP_TP_VALUE (p2);
2369 if (TREE_CODE (v1) != INTEGER_CST
2370 || TREE_CODE (v2) != INTEGER_CST
2371 || integer_zerop (v1) != integer_zerop (v2))
2372 return 2;
2373 break;
2374 }
2375 if (set == OMP_TRAIT_SET_TARGET_DEVICE
2376 && sel == OMP_TRAIT_DEVICE_NUM)
2377 {
2378 /* Recognize constants that have equal values,
2379 otherwise assume all expressions are unique. */
2380 tree v1 = OMP_TP_VALUE (p1);
2381 tree v2 = OMP_TP_VALUE (p2);
2382 if (TREE_CODE (v1) != INTEGER_CST
2383 || TREE_CODE (v2) != INTEGER_CST
2384 || tree_int_cst_compare (v1, v2) != 0)
2385 return 2;
2386 break;
2387 }
2388 if (simple_cst_equal (OMP_TP_VALUE (p1), OMP_TP_VALUE (p2)))
2389 break;
2390 }
2391 else if (OMP_TP_NAME (p1) == OMP_TP_NAMELIST_NODE)
2392 {
2393 /* Handle string constant vs identifier comparison for
2394 name-list properties. */
2395 const char *n1 = omp_context_name_list_prop (p1);
2396 const char *n2 = omp_context_name_list_prop (p2);
2397 if (n1 && n2 && !strcmp (n1, n2))
2398 break;
2399 }
2400 else
2401 break;
2402 }
2403 if (p2 == NULL_TREE)
2404 {
2405 int r = pass ? -1 : 1;
2406 if (ret && ret != r)
2407 return 2;
2408 else if (pass)
2409 return r;
2410 else
2411 {
2412 ret = r;
2413 break;
2414 }
2415 }
2416 }
2417 return ret;
2418 }
2419
2420 /* Compare single context selector sets CTX1 and CTX2 with SET name.
2421 CTX1 and CTX2 are lists of trait-selectors.
2422 Return 0 if CTX1 is equal to CTX2,
2423 -1 if CTX1 is a strict subset of CTX2,
2424 1 if CTX2 is a strict subset of CTX1, or
2425 2 if neither context is a subset of another one. */
2426
2427 static int
2428 omp_context_selector_set_compare (enum omp_tss_code set, tree ctx1, tree ctx2)
2429 {
2430
2431 /* If either list includes an ignored selector trait, neither can
2432 be a subset of the other. */
2433 for (tree ts = ctx1; ts; ts = TREE_CHAIN (ts))
2434 if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
2435 return 2;
2436 for (tree ts = ctx2; ts; ts = TREE_CHAIN (ts))
2437 if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
2438 return 2;
2439
2440 bool swapped = false;
2441 int ret = 0;
2442 int len1 = list_length (ctx1);
2443 int len2 = list_length (ctx2);
2444 int cnt = 0;
2445 if (len1 < len2)
2446 {
2447 swapped = true;
2448 std::swap (ctx1, ctx2);
2449 std::swap (len1, len2);
2450 }
2451
2452 if (set == OMP_TRAIT_SET_CONSTRUCT)
2453 {
2454 tree ts1;
2455 tree ts2 = ctx2;
2456 /* Handle construct set specially. In this case the order
2457 of the selector matters too. */
2458 for (ts1 = ctx1; ts1; ts1 = TREE_CHAIN (ts1))
2459 if (OMP_TS_CODE (ts1) == OMP_TS_CODE (ts2))
2460 {
2461 int r = 0;
2462 if (OMP_TS_CODE (ts1) == OMP_TRAIT_CONSTRUCT_SIMD)
2463 r = omp_construct_simd_compare (OMP_TS_PROPERTIES (ts1),
2464 OMP_TS_PROPERTIES (ts2),
2465 false);
2466 if (r == 2 || (ret && r && (ret < 0) != (r < 0)))
2467 return 2;
2468 if (ret == 0)
2469 ret = r;
2470 ts2 = TREE_CHAIN (ts2);
2471 if (ts2 == NULL_TREE)
2472 {
2473 ts1 = TREE_CHAIN (ts1);
2474 break;
2475 }
2476 }
2477 else if (ret < 0)
2478 return 2;
2479 else
2480 ret = 1;
2481 if (ts2 != NULL_TREE)
2482 return 2;
2483 if (ts1 != NULL_TREE)
2484 {
2485 if (ret < 0)
2486 return 2;
2487 ret = 1;
2488 }
2489 if (ret == 0)
2490 return 0;
2491 return swapped ? -ret : ret;
2492 }
2493 for (tree ts1 = ctx1; ts1; ts1 = TREE_CHAIN (ts1))
2494 {
2495 enum omp_ts_code sel = OMP_TS_CODE (ts1);
2496 tree ts2;
2497 for (ts2 = ctx2; ts2; ts2 = TREE_CHAIN (ts2))
2498 if (sel == OMP_TS_CODE (ts2))
2499 {
2500 tree score1 = OMP_TS_SCORE (ts1);
2501 tree score2 = OMP_TS_SCORE (ts2);
2502 if ((score1 && score2 && !simple_cst_equal (score1, score2))
2503 || (score1 && !score2)
2504 || (!score1 && score2))
2505 return 2;
2506
2507 int r = omp_context_selector_props_compare (set, OMP_TS_CODE (ts1),
2508 OMP_TS_PROPERTIES (ts1),
2509 OMP_TS_PROPERTIES (ts2));
2510 if (r == 2 || (ret && r && (ret < 0) != (r < 0)))
2511 return 2;
2512 if (ret == 0)
2513 ret = r;
2514 cnt++;
2515 break;
2516 }
2517 if (ts2 == NULL_TREE)
2518 {
2519 if (ret == -1)
2520 return 2;
2521 ret = 1;
2522 }
2523 }
2524 if (cnt < len2)
2525 return 2;
2526 if (ret == 0)
2527 return 0;
2528 return swapped ? -ret : ret;
2529 }
2530
2531 /* Compare whole context selector specification CTX1 and CTX2.
2532 Return 0 if CTX1 is equal to CTX2,
2533 -1 if CTX1 is a strict subset of CTX2,
2534 1 if CTX2 is a strict subset of CTX1, or
2535 2 if neither context is a subset of another one. */
2536
2537 static int
2538 omp_context_selector_compare (tree ctx1, tree ctx2)
2539 {
2540 bool swapped = false;
2541 int ret = 0;
2542 int len1 = list_length (ctx1);
2543 int len2 = list_length (ctx2);
2544 int cnt = 0;
2545 if (len1 < len2)
2546 {
2547 swapped = true;
2548 std::swap (ctx1, ctx2);
2549 std::swap (len1, len2);
2550 }
2551 for (tree tss1 = ctx1; tss1; tss1 = TREE_CHAIN (tss1))
2552 {
2553 enum omp_tss_code set = OMP_TSS_CODE (tss1);
2554 tree tss2;
2555 for (tss2 = ctx2; tss2; tss2 = TREE_CHAIN (tss2))
2556 if (set == OMP_TSS_CODE (tss2))
2557 {
2558 int r
2559 = omp_context_selector_set_compare
2560 (set, OMP_TSS_TRAIT_SELECTORS (tss1),
2561 OMP_TSS_TRAIT_SELECTORS (tss2));
2562 if (r == 2 || (ret && r && (ret < 0) != (r < 0)))
2563 return 2;
2564 if (ret == 0)
2565 ret = r;
2566 cnt++;
2567 break;
2568 }
2569 if (tss2 == NULL_TREE)
2570 {
2571 if (ret == -1)
2572 return 2;
2573 ret = 1;
2574 }
2575 }
2576 if (cnt < len2)
2577 return 2;
2578 if (ret == 0)
2579 return 0;
2580 return swapped ? -ret : ret;
2581 }
2582
2583 /* From context selector CTX, return trait-selector with name SEL in
2584 trait-selector-set with name SET if any, or NULL_TREE if not found. */
2585 tree
2586 omp_get_context_selector (tree ctx, enum omp_tss_code set,
2587 enum omp_ts_code sel)
2588 {
2589 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
2590 if (OMP_TSS_CODE (tss) == set)
2591 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
2592 if (OMP_TS_CODE (ts) == sel)
2593 return ts;
2594 return NULL_TREE;
2595 }
2596
2597 /* Similar, but returns the whole trait-selector list for SET in CTX. */
2598 tree
2599 omp_get_context_selector_list (tree ctx, enum omp_tss_code set)
2600 {
2601 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
2602 if (OMP_TSS_CODE (tss) == set)
2603 return OMP_TSS_TRAIT_SELECTORS (tss);
2604 return NULL_TREE;
2605 }
2606
2607 /* Map string S onto a trait selector set code. */
2608 enum omp_tss_code
2609 omp_lookup_tss_code (const char * s)
2610 {
2611 for (int i = 0; i < OMP_TRAIT_SET_LAST; i++)
2612 if (strcmp (s, omp_tss_map[i]) == 0)
2613 return (enum omp_tss_code) i;
2614 return OMP_TRAIT_SET_INVALID;
2615 }
2616
2617 /* Map string S onto a trait selector code for set SET. */
2618 enum omp_ts_code
2619 omp_lookup_ts_code (enum omp_tss_code set, const char *s)
2620 {
2621 unsigned int mask = 1 << set;
2622 for (int i = 0; i < OMP_TRAIT_LAST; i++)
2623 if ((mask & omp_ts_map[i].tss_mask) != 0
2624 && strcmp (s, omp_ts_map[i].name) == 0)
2625 return (enum omp_ts_code) i;
2626 return OMP_TRAIT_INVALID;
2627 }
2628
2629
2630 /* Return true if the selector CTX is dynamic. */
2631 static bool
2632 omp_selector_is_dynamic (tree ctx)
2633 {
2634 tree user_sel = omp_get_context_selector (ctx, OMP_TRAIT_SET_USER,
2635 OMP_TRAIT_USER_CONDITION);
2636 if (user_sel)
2637 {
2638 tree expr = OMP_TP_VALUE (OMP_TS_PROPERTIES (user_sel));
2639
2640 /* The user condition is not dynamic if it is constant. */
2641 if (!tree_fits_shwi_p (expr))
2642 return true;
2643 }
2644
2645 tree target_device_ss
2646 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_TARGET_DEVICE);
2647 if (target_device_ss)
2648 return true;
2649
2650 return false;
2651 }
2652
2653 /* Helper function for omp_dynamic_cond: return a boolean tree expression
2654 that tests whether *DEVICE_NUM is a "conforming device number other
2655 than omp_invalid_device". This may modify *DEVICE_NUM (i.e, to be
2656 a save_expr). *IS_HOST is set to true if the device can be statically
2657 determined to be the host. */
2658
2659 static tree
2660 omp_device_num_check (tree *device_num, bool *is_host)
2661 {
2662 /* First check for some constant values we can treat specially. */
2663 if (tree_fits_shwi_p (*device_num))
2664 {
2665 HOST_WIDE_INT num = tree_to_shwi (*device_num);
2666 if (num < -1)
2667 return integer_zero_node;
2668 /* Initial device? */
2669 if (num == -1)
2670 {
2671 *is_host = true;
2672 return integer_one_node;
2673 }
2674 /* There is always at least one device (the host + offload devices). */
2675 if (num == 0)
2676 return integer_one_node;
2677 /* If there is no offloading, there is exactly one device. */
2678 if (!ENABLE_OFFLOADING && num > 0)
2679 return integer_zero_node;
2680 }
2681
2682 /* Also test for direct calls to OpenMP routines that return valid
2683 device numbers. */
2684 if (TREE_CODE (*device_num) == CALL_EXPR)
2685 {
2686 tree fndecl = get_callee_fndecl (*device_num);
2687 if (fndecl && omp_runtime_api_call (fndecl))
2688 {
2689 const char *fnname = IDENTIFIER_POINTER (DECL_NAME (fndecl));
2690 if (strcmp (fnname, "omp_get_default_device") == 0
2691 || strcmp (fnname, "omp_get_device_num") == 0)
2692 return integer_one_node;
2693 if (strcmp (fnname, "omp_get_num_devices") == 0
2694 || strcmp (fnname, "omp_get_initial_device") == 0)
2695 {
2696 *is_host = true;
2697 return integer_one_node;
2698 }
2699 }
2700 }
2701
2702 /* Otherwise, test that -1 <= *device_num <= omp_get_num_devices (). */
2703 *device_num = save_expr (*device_num);
2704 tree lotest = build2 (GE_EXPR, integer_type_node, *device_num,
2705 integer_minus_one_node);
2706 tree fndecl = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
2707 tree hitest = build2 (LE_EXPR, integer_type_node, *device_num,
2708 build_call_expr (fndecl, 0));
2709 return build2 (TRUTH_ANDIF_EXPR, integer_type_node, lotest, hitest);
2710 }
2711
2712 /* Return a tree expression representing the dynamic part of the context
2713 selector CTX. SUPERCONTEXT is the surrounding BLOCK, in case we need
2714 to introduce a new BLOCK in the result. */
2715 tree
2716 omp_dynamic_cond (tree ctx, tree supercontext)
2717 {
2718 tree user_cond = NULL_TREE, target_device_cond = NULL_TREE;
2719
2720 /* Build the "user" part of the dynamic selector. This is a test
2721 predicate taken directly for the "condition" trait in this set. */
2722 tree user_sel = omp_get_context_selector (ctx, OMP_TRAIT_SET_USER,
2723 OMP_TRAIT_USER_CONDITION);
2724 if (user_sel)
2725 {
2726 tree expr = OMP_TP_VALUE (OMP_TS_PROPERTIES (user_sel));
2727
2728 /* The user condition is not dynamic if it is constant. */
2729 if (!tree_fits_shwi_p (expr))
2730 user_cond = unshare_expr (expr);
2731 }
2732
2733 /* Build the "target_device" part of the dynamic selector. In the
2734 most general case this requires building a bit of code that runs
2735 on the specified device_num using the same mechanism as
2736 "#pragma omp target" that uses the OMP_TARGET_DEVICE_MATCHES magic
2737 cookie to represent the kind/arch/isa tests which are and'ed together.
2738 These cookies can be resolved into a constant truth value by the
2739 offload compiler; see resolve_omp_target_device_matches, above.
2740
2741 In some cases, we can (in)validate the device number in advance.
2742 If it is not valid, the whole selector fails to match. If it is
2743 valid and refers to the host (e.g., constant -1), then we can
2744 resolve the match to a constant truth value now instead of having
2745 to create a OMP_TARGET_DEVICE_MATCHES. */
2746
2747 tree target_device_ss
2748 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_TARGET_DEVICE);
2749 if (target_device_ss)
2750 {
2751 tree device_num = NULL_TREE;
2752 tree kind = NULL_TREE;
2753 tree arch = NULL_TREE;
2754 tree isa = NULL_TREE;
2755 tree device_ok = NULL_TREE;
2756 bool is_host = !ENABLE_OFFLOADING;
2757
2758 tree device_num_sel
2759 = omp_get_context_selector (ctx, OMP_TRAIT_SET_TARGET_DEVICE,
2760 OMP_TRAIT_DEVICE_NUM);
2761 if (device_num_sel)
2762 {
2763 device_num = OMP_TP_VALUE (OMP_TS_PROPERTIES (device_num_sel));
2764 device_ok = omp_device_num_check (&device_num, &is_host);
2765 /* If an invalid constant device number was specified, the
2766 whole selector fails to match, and there's no point in
2767 continuing to generate code that would never be executed. */
2768 if (device_ok == integer_zero_node)
2769 {
2770 target_device_cond = integer_zero_node;
2771 goto wrapup;
2772 }
2773 }
2774
2775 tree kind_sel
2776 = omp_get_context_selector (ctx, OMP_TRAIT_SET_TARGET_DEVICE,
2777 OMP_TRAIT_DEVICE_KIND);
2778 /* "any" is equivalent to omitting this trait selector. */
2779 if (kind_sel
2780 && strcmp (omp_context_name_list_prop (OMP_TS_PROPERTIES (kind_sel)),
2781 "any"))
2782 {
2783 tree props = OMP_TS_PROPERTIES (kind_sel);
2784 if (!is_host)
2785 kind = build2 (OMP_TARGET_DEVICE_MATCHES, integer_type_node,
2786 build_int_cst (integer_type_node,
2787 (int) OMP_TRAIT_DEVICE_KIND),
2788 props);
2789 else if (!omp_target_device_matches_on_host (OMP_TRAIT_DEVICE_KIND,
2790 props))
2791 {
2792 /* The whole selector fails to match. */
2793 target_device_cond = integer_zero_node;
2794 goto wrapup;
2795 }
2796 /* else it is statically resolved to true and is a no-op. */
2797 }
2798 tree arch_sel
2799 = omp_get_context_selector (ctx, OMP_TRAIT_SET_TARGET_DEVICE,
2800 OMP_TRAIT_DEVICE_ARCH);
2801 if (arch_sel)
2802 {
2803 tree props = OMP_TS_PROPERTIES (arch_sel);
2804 if (!is_host)
2805 arch = build2 (OMP_TARGET_DEVICE_MATCHES, integer_type_node,
2806 build_int_cst (integer_type_node,
2807 (int) OMP_TRAIT_DEVICE_ARCH),
2808 props);
2809 else if (!omp_target_device_matches_on_host (OMP_TRAIT_DEVICE_ARCH,
2810 props))
2811 {
2812 /* The whole selector fails to match. */
2813 target_device_cond = integer_zero_node;
2814 goto wrapup;
2815 }
2816 /* else it is statically resolved to true and is a no-op. */
2817 }
2818
2819 tree isa_sel
2820 = omp_get_context_selector (ctx, OMP_TRAIT_SET_TARGET_DEVICE,
2821 OMP_TRAIT_DEVICE_ISA);
2822 if (isa_sel)
2823 {
2824 tree props = OMP_TS_PROPERTIES (isa_sel);
2825 if (!is_host)
2826 isa = build2 (OMP_TARGET_DEVICE_MATCHES, integer_type_node,
2827 build_int_cst (integer_type_node,
2828 (int) OMP_TRAIT_DEVICE_ISA),
2829 props);
2830 else if (!omp_target_device_matches_on_host (OMP_TRAIT_DEVICE_ISA,
2831 props))
2832 {
2833 /* The whole selector fails to match. */
2834 target_device_cond = integer_zero_node;
2835 goto wrapup;
2836 }
2837 /* else it is statically resolved to true and is a no-op. */
2838 }
2839
2840 /* AND the three possible tests together. */
2841 tree test_expr = kind ? kind : NULL_TREE;
2842 if (arch && test_expr)
2843 test_expr = build2 (TRUTH_ANDIF_EXPR, integer_type_node,
2844 arch, test_expr);
2845 else if (arch)
2846 test_expr = arch;
2847 if (isa && test_expr)
2848 test_expr = build2 (TRUTH_ANDIF_EXPR, integer_type_node,
2849 isa, test_expr);
2850 else if (isa)
2851 test_expr = isa;
2852
2853 if (!test_expr)
2854 /* This could happen if the selector includes only kind="any",
2855 or is_host is true and it could be statically determined to
2856 be true. The selector always matches, but we still have to
2857 evaluate the device_num expression. */
2858 {
2859 if (device_num)
2860 target_device_cond = build2 (COMPOUND_EXPR, integer_type_node,
2861 device_num, integer_one_node);
2862 else
2863 target_device_cond = integer_one_node;
2864 }
2865 else
2866 {
2867 /* Arrange to evaluate test_expr in the offload compiler for
2868 device device_num. */
2869 tree stmt = make_node (OMP_TARGET);
2870 TREE_TYPE (stmt) = void_type_node;
2871 tree result_var = create_tmp_var (integer_type_node, "td_match");
2872 tree map = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
2873 OMP_CLAUSE_DECL (map) = result_var;
2874 OMP_CLAUSE_SET_MAP_KIND (map, GOMP_MAP_FROM);
2875 OMP_TARGET_CLAUSES (stmt) = map;
2876 if (device_num)
2877 {
2878 tree clause = build_omp_clause (UNKNOWN_LOCATION,
2879 OMP_CLAUSE_DEVICE);
2880 OMP_CLAUSE_CHAIN (clause) = NULL_TREE;
2881 OMP_CLAUSE_DEVICE_ID (clause) = device_num;
2882 OMP_CLAUSE_DEVICE_ANCESTOR (clause) = false;
2883 OMP_CLAUSE_CHAIN (map) = clause;
2884 }
2885
2886 tree block = make_node (BLOCK);
2887 BLOCK_SUPERCONTEXT (block) = supercontext;
2888
2889 tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
2890 build2 (MODIFY_EXPR, integer_type_node,
2891 result_var, test_expr),
2892 block);
2893 TREE_SIDE_EFFECTS (bind) = 1;
2894 OMP_TARGET_BODY (stmt) = bind;
2895 target_device_cond = build2 (COMPOUND_EXPR, integer_type_node,
2896 stmt, result_var);
2897
2898 /* If necessary, "and" target_device_cond with the test to
2899 make sure the device number is valid. */
2900 if (device_ok && device_ok != integer_one_node)
2901 target_device_cond = build2 (TRUTH_ANDIF_EXPR, integer_type_node,
2902 device_ok, target_device_cond);
2903
2904 /* Set the bit to trigger resolution of OMP_TARGET_DEVICE_MATCHES
2905 in the ompdevlow pass. */
2906 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
2907 cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
2908 }
2909 }
2910
2911 wrapup:
2912 if (user_cond && target_device_cond)
2913 return build2 (TRUTH_ANDIF_EXPR, integer_type_node,
2914 user_cond, target_device_cond);
2915 else if (user_cond)
2916 return user_cond;
2917 else if (target_device_cond)
2918 return target_device_cond;
2919 else
2920 return NULL_TREE;
2921 }
2922
2923
2924 /* Given an omp_variant VARIANT, compute VARIANT->score and
2925 VARIANT->scorable.
2926 CONSTRUCT_CONTEXT is the OpenMP construct context; if this is null or
2927 COMPLETE_P is false (e.g., during parsing or gimplification) then it
2928 may not be possible to compute the score accurately and the scorable
2929 flag is set to false.
2930
2931 Cited text in the comments is from section 7.2 of the OpenMP 5.2
2932 specification. */
2933
2934 static void
2935 omp_context_compute_score (struct omp_variant *variant,
2936 tree construct_context, bool complete_p)
2937 {
2938 int l = list_length (construct_context);
2939 tree ctx = variant->selector;
2940 variant->scorable = true;
2941
2942 /* "the final score is the sum of the values of all specified selectors
2943 plus 1". */
2944 variant->score = 1;
2945 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
2946 {
2947 if (OMP_TSS_CODE (tss) == OMP_TRAIT_SET_CONSTRUCT)
2948 {
2949 /* "Each trait selector for which the corresponding trait appears
2950 in the context trait set in the OpenMP context..." */
2951 score_wide_int tss_score = 0;
2952 omp_construct_traits_match (OMP_TSS_TRAIT_SELECTORS (tss),
2953 construct_context, &tss_score);
2954 variant->score += tss_score;
2955 if (!complete_p)
2956 variant->scorable = false;
2957 }
2958 else if (OMP_TSS_CODE (tss) == OMP_TRAIT_SET_DEVICE
2959 || OMP_TSS_CODE (tss) == OMP_TRAIT_SET_TARGET_DEVICE)
2960 {
2961 /* "The kind, arch, and isa selectors, if specified, are given
2962 the values 2**l, 2**(l+1), and 2**(l+2), respectively..."
2963 FIXME: the spec isn't clear what should happen if there are
2964 both "device" and "target_device" selector sets specified.
2965 This implementation adds up the bits rather than ORs them. */
2966 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
2967 ts = TREE_CHAIN (ts))
2968 {
2969 enum omp_ts_code code = OMP_TS_CODE (ts);
2970 if (code == OMP_TRAIT_DEVICE_KIND)
2971 variant->score
2972 += wi::shifted_mask <score_wide_int> (l, 1, false);
2973 else if (code == OMP_TRAIT_DEVICE_ARCH)
2974 variant->score
2975 += wi::shifted_mask <score_wide_int> (l + 1, 1, false);
2976 else if (code == OMP_TRAIT_DEVICE_ISA)
2977 variant->score
2978 += wi::shifted_mask <score_wide_int> (l + 2, 1, false);
2979 }
2980 if (!complete_p)
2981 variant->scorable = false;
2982 }
2983 else
2984 {
2985 /* "Trait selectors for which a trait-score is specified..."
2986 Note that there are no implementation-defined selectors, and
2987 "other selectors are given a value of zero". */
2988 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
2989 ts = TREE_CHAIN (ts))
2990 {
2991 tree s = OMP_TS_SCORE (ts);
2992 if (s && TREE_CODE (s) == INTEGER_CST)
2993 variant->score
2994 += score_wide_int::from (wi::to_wide (s),
2995 TYPE_SIGN (TREE_TYPE (s)));
2996 }
2997 }
2998 }
2999 }
3000
3001 /* CONSTRUCT_CONTEXT contains "the directive names, each being a trait,
3002 of all enclosing constructs at that point in the program up to a target
3003 construct", per section 7.1 of the 5.2 specification. The traits are
3004 collected during gimplification and are listed outermost first.
3005
3006 This function attempts to apply the "if the point in the program is not
3007 enclosed by a target construct, the following rules are applied in order"
3008 requirements that follow in the same paragraph. This may not be possible,
3009 depending on the compilation phase; in particular, "declare simd" clones
3010 are not known until late resolution.
3011
3012 The augmented context is returned, and *COMPLETEP is set to true if
3013 the context is known to be complete, false otherwise. */
3014 static tree
3015 omp_complete_construct_context (tree construct_context, bool *completep)
3016 {
3017 /* The point in the program is enclosed by a target construct. */
3018 if (construct_context
3019 && OMP_TS_CODE (construct_context) == OMP_TRAIT_CONSTRUCT_TARGET)
3020 *completep = true;
3021
3022 /* At parse time we have none of the information we need to collect
3023 the missing pieces. */
3024 else if (symtab->state == PARSING)
3025 *completep = false;
3026
3027 else
3028 {
3029 tree attributes = DECL_ATTRIBUTES (current_function_decl);
3030
3031 /* Add simd trait when in a simd clone. This information is only
3032 available during late resolution in the omp_device_lower pass,
3033 however we can also rule out cases where we know earlier that
3034 cfun is not a candidate for cloning. */
3035 if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
3036 {
3037 cgraph_node *node = cgraph_node::get (cfun->decl);
3038 if (node->simdclone)
3039 construct_context = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
3040 NULL_TREE, NULL_TREE,
3041 construct_context);
3042 *completep = true;
3043 }
3044 else if (lookup_attribute ("omp declare simd", attributes))
3045 *completep = false;
3046 else
3047 *completep = true;
3048
3049 /* Add construct selector set within a "declare variant" function. */
3050 tree variant_attr
3051 = lookup_attribute ("omp declare variant variant", attributes);
3052 if (variant_attr)
3053 {
3054 tree temp = NULL_TREE;
3055 for (tree t = TREE_VALUE (variant_attr); t; t = TREE_CHAIN (t))
3056 temp = chainon (temp, copy_node (t));
3057 construct_context = chainon (temp, construct_context);
3058 }
3059
3060 /* Add target trait when in a target variant. */
3061 if (lookup_attribute ("omp declare target", attributes))
3062 construct_context = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
3063 NULL_TREE, NULL_TREE,
3064 construct_context);
3065 }
3066 return construct_context;
3067 }
3068
3069 /* Comparison function for sorting routines, to sort OpenMP metadirective
3070 variants by decreasing score. */
3071
3072 static int
3073 sort_variant (const void * a, const void *b, void *)
3074 {
3075 score_wide_int score1
3076 = ((const struct omp_variant *) a)->score;
3077 score_wide_int score2
3078 = ((const struct omp_variant *) b)->score;
3079
3080 if (score1 > score2)
3081 return -1;
3082 else if (score1 < score2)
3083 return 1;
3084 else
3085 return 0;
3086 }
3087
3088 /* Return a vector of dynamic replacement candidates for the directive
3089 candidates in ALL_VARIANTS. Return an empty vector if the candidates
3090 cannot be resolved. */
3091
3092 vec<struct omp_variant>
3093 omp_get_dynamic_candidates (vec <struct omp_variant> &all_variants,
3094 tree construct_context)
3095 {
3096 auto_vec <struct omp_variant> variants;
3097 struct omp_variant default_variant;
3098 bool default_found = false;
3099 bool complete_p;
3100
3101 construct_context
3102 = omp_complete_construct_context (construct_context, &complete_p);
3103
3104 if (dump_file)
3105 {
3106 fprintf (dump_file, "\nIn omp_get_dynamic_candidates:\n");
3107 if (symtab->state == PARSING)
3108 fprintf (dump_file, "invoked during parsing\n");
3109 else if (cfun && (cfun->curr_properties & PROP_gimple_any) == 0)
3110 fprintf (dump_file, "invoked during gimplification\n");
3111 else if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
3112 fprintf (dump_file, "invoked during late resolution\n");
3113 else
3114 fprintf (dump_file, "confused about invocation context?!?\n");
3115 fprintf (dump_file, "construct_context has %d traits (%s)\n",
3116 (construct_context ? list_length (construct_context) : 0),
3117 (complete_p ? "complete" : "incomplete"));
3118 }
3119
3120 for (unsigned int i = 0; i < all_variants.length (); i++)
3121 {
3122 struct omp_variant variant = all_variants[i];
3123
3124 if (variant.selector == NULL_TREE)
3125 {
3126 gcc_assert (!default_found);
3127 default_found = true;
3128 default_variant = variant;
3129 default_variant.score = 0;
3130 default_variant.scorable = true;
3131 default_variant.matchable = true;
3132 default_variant.dynamic_selector = false;
3133 if (dump_file)
3134 fprintf (dump_file,
3135 "Considering default selector as candidate\n");
3136 continue;
3137 }
3138
3139 variant.matchable = true;
3140 variant.scorable = true;
3141
3142 if (dump_file)
3143 {
3144 fprintf (dump_file, "Considering selector ");
3145 print_omp_context_selector (dump_file, variant.selector, TDF_NONE);
3146 fprintf (dump_file, " as candidate - ");
3147 }
3148
3149 switch (omp_context_selector_matches (variant.selector,
3150 construct_context, complete_p))
3151 {
3152 case -1:
3153 if (dump_file)
3154 fprintf (dump_file, "unmatchable\n");
3155 /* At parse time, just give up if we can't determine whether
3156 things match. */
3157 if (symtab->state == PARSING)
3158 {
3159 variants.truncate (0);
3160 return variants.copy ();
3161 }
3162 /* Otherwise we must be invoked from the gimplifier. */
3163 gcc_assert (cfun && (cfun->curr_properties & PROP_gimple_any) == 0);
3164 variant.matchable = false;
3165 /* FALLTHRU */
3166 case 1:
3167 omp_context_compute_score (&variant, construct_context, complete_p);
3168 variant.dynamic_selector
3169 = omp_selector_is_dynamic (variant.selector);
3170 variants.safe_push (variant);
3171 if (dump_file && variant.matchable)
3172 {
3173 if (variant.dynamic_selector)
3174 fprintf (dump_file, "matched, dynamic");
3175 else
3176 fprintf (dump_file, "matched, non-dynamic");
3177 }
3178 break;
3179 case 0:
3180 if (dump_file)
3181 fprintf (dump_file, "no match");
3182 break;
3183 }
3184
3185 if (dump_file)
3186 fprintf (dump_file, "\n");
3187 }
3188
3189 /* There must be one default variant. */
3190 gcc_assert (default_found);
3191
3192 /* If there are no matching selectors, return the default. */
3193 if (variants.length () == 0)
3194 {
3195 variants.safe_push (default_variant);
3196 return variants.copy ();
3197 }
3198
3199 /* If there is only one matching selector, use it. */
3200 if (variants.length () == 1)
3201 {
3202 if (variants[0].matchable)
3203 {
3204 if (variants[0].dynamic_selector)
3205 variants.safe_push (default_variant);
3206 return variants.copy ();
3207 }
3208 else
3209 {
3210 /* We don't know whether the one non-default selector will
3211 actually match. */
3212 variants.truncate (0);
3213 return variants.copy ();
3214 }
3215 }
3216
3217 /* A context selector that is a strict subset of another context selector
3218 has a score of zero. This only applies if the selector that is a
3219 superset definitely matches, though. */
3220 for (unsigned int i = 0; i < variants.length (); i++)
3221 for (unsigned int j = i + 1; j < variants.length (); j++)
3222 {
3223 int r = omp_context_selector_compare (variants[i].selector,
3224 variants[j].selector);
3225 if (r == -1 && variants[j].matchable)
3226 {
3227 /* variant i is a strict subset of variant j. */
3228 variants[i].score = 0;
3229 variants[i].scorable = true;
3230 break;
3231 }
3232 else if (r == 1 && variants[i].matchable)
3233 /* variant j is a strict subset of variant i. */
3234 {
3235 variants[j].score = 0;
3236 variants[j].scorable = true;
3237 }
3238 }
3239
3240 /* Sort the variants by decreasing score, preserving the original order
3241 in case of a tie. */
3242 variants.stablesort (sort_variant, NULL);
3243
3244 /* Add the default as a final choice. */
3245 variants.safe_push (default_variant);
3246
3247 if (dump_file)
3248 {
3249 fprintf (dump_file, "Sorted variants are:\n");
3250 for (unsigned i = 0; i < variants.length (); i++)
3251 {
3252 HOST_WIDE_INT score = variants[i].score.to_shwi ();
3253 fprintf (dump_file, "score %d matchable %d scorable %d ",
3254 (int)score, (int)(variants[i].matchable),
3255 (int)(variants[i].scorable));
3256 if (variants[i].selector)
3257 {
3258 fprintf (dump_file, "selector ");
3259 print_omp_context_selector (dump_file, variants[i].selector,
3260 TDF_NONE);
3261 fprintf (dump_file, "\n");
3262 }
3263 else
3264 fprintf (dump_file, "default selector\n");
3265 }
3266 }
3267
3268 /* Build the dynamic candidate list. */
3269 for (unsigned i = 0; i < variants.length (); i++)
3270 {
3271 /* If we encounter a candidate that wasn't definitely matched,
3272 give up now. */
3273 if (!variants[i].matchable)
3274 {
3275 variants.truncate (0);
3276 break;
3277 }
3278
3279 /* In general, we can't proceed if we can't accurately score any
3280 of the selectors, since the sorting may be incorrect. But, since
3281 the actual score will never be lower than the guessed value, we
3282 can use the first variant if it is not scorable but either the next
3283 one is a subset of the first, is scorable, or we can make a
3284 direct comparison of the high-order isa/arch/kind bits. */
3285 if (!variants[i].scorable)
3286 {
3287 bool ok = true;
3288 if (i != 0)
3289 ok = false;
3290 else if (variants[i+1].scorable)
3291 /* ok */
3292 ;
3293 else if (variants[i+1].score > 0)
3294 {
3295 /* To keep comparisons simple, reject selectors that contain
3296 sets other than device, target_device, or construct. */
3297 for (tree tss = variants[i].selector;
3298 tss && ok; tss = TREE_CHAIN (tss))
3299 {
3300 enum omp_tss_code code = OMP_TSS_CODE (tss);
3301 if (code != OMP_TRAIT_SET_DEVICE
3302 && code != OMP_TRAIT_SET_TARGET_DEVICE
3303 && code != OMP_TRAIT_SET_CONSTRUCT)
3304 ok = false;
3305 }
3306 for (tree tss = variants[i+1].selector;
3307 tss && ok; tss = TREE_CHAIN (tss))
3308 {
3309 enum omp_tss_code code = OMP_TSS_CODE (tss);
3310 if (code != OMP_TRAIT_SET_DEVICE
3311 && code != OMP_TRAIT_SET_TARGET_DEVICE
3312 && code != OMP_TRAIT_SET_CONSTRUCT)
3313 ok = false;
3314 }
3315 /* Ignore the construct bits of the score. If the isa/arch/kind
3316 bits are strictly ordered, we're good to go. Since
3317 "the final score is the sum of the values of all specified
3318 selectors plus 1", subtract that 1 from both scores before
3319 getting rid of the low bits. */
3320 if (ok)
3321 {
3322 size_t l = list_length (construct_context);
3323 gcc_assert (variants[i].score > 0
3324 && variants[i+1].score > 0);
3325 if ((variants[i].score - 1) >> l
3326 <= (variants[i+1].score - 1) >> l)
3327 ok = false;
3328 }
3329 }
3330
3331 if (!ok)
3332 {
3333 variants.truncate (0);
3334 break;
3335 }
3336 }
3337
3338 if (dump_file)
3339 {
3340 fprintf (dump_file, "Adding directive variant with ");
3341
3342 if (variants[i].selector)
3343 {
3344 fprintf (dump_file, "selector ");
3345 print_omp_context_selector (dump_file, variants[i].selector,
3346 TDF_NONE);
3347 }
3348 else
3349 fprintf (dump_file, "default selector");
3350
3351 fprintf (dump_file, " as candidate.\n");
3352 }
3353
3354 /* The last of the candidates is ended by a static selector. */
3355 if (!variants[i].dynamic_selector)
3356 {
3357 variants.truncate (i + 1);
3358 break;
3359 }
3360 }
3361
3362 return variants.copy ();
3363 }
3364
3365 /* Two attempts are made to resolve calls to "declare variant" functions:
3366 early resolution in the gimplifier, and late resolution in the
3367 omp_device_lower pass. If early resolution is not possible, the
3368 original function call is gimplified into the same form as metadirective
3369 and goes through the same late resolution code as metadirective. */
3370
3371 /* Collect "declare variant" candidates for BASE. CONSTRUCT_CONTEXT
3372 is the un-augmented context, or NULL_TREE if that information is not
3373 available yet. */
3374 vec<struct omp_variant>
3375 omp_declare_variant_candidates (tree base, tree construct_context)
3376 {
3377 auto_vec <struct omp_variant> candidates;
3378 bool complete_p;
3379 tree augmented_context
3380 = omp_complete_construct_context (construct_context, &complete_p);
3381
3382 /* The variants are stored on (possible multiple) "omp declare variant base"
3383 attributes on the base function. */
3384 for (tree attr = DECL_ATTRIBUTES (base); attr; attr = TREE_CHAIN (attr))
3385 {
3386 attr = lookup_attribute ("omp declare variant base", attr);
3387 if (attr == NULL_TREE)
3388 break;
3389
3390 tree fndecl = TREE_PURPOSE (TREE_VALUE (attr));
3391 tree selector = TREE_VALUE (TREE_VALUE (attr));
3392
3393 if (TREE_CODE (fndecl) != FUNCTION_DECL)
3394 continue;
3395
3396 /* Ignore this variant if its selector is known not to match. */
3397 if (!omp_context_selector_matches (selector, augmented_context,
3398 complete_p))
3399 continue;
3400
3401 struct omp_variant candidate;
3402 candidate.selector = selector;
3403 candidate.dynamic_selector = false;
3404 candidate.alternative = fndecl;
3405 candidate.body = NULL_TREE;
3406 candidates.safe_push (candidate);
3407 }
3408
3409 /* Add a default that is the base function. */
3410 struct omp_variant v;
3411 v.selector = NULL_TREE;
3412 v.dynamic_selector = false;
3413 v.alternative = base;
3414 v.body = NULL_TREE;
3415 candidates.safe_push (v);
3416 return candidates.copy ();
3417 }
3418
3419 /* Collect metadirective candidates for METADIRECTIVE. CONSTRUCT_CONTEXT
3420 is the un-augmented context, or NULL_TREE if that information is not
3421 available yet. */
3422 vec<struct omp_variant>
3423 omp_metadirective_candidates (tree metadirective, tree construct_context)
3424 {
3425 auto_vec <struct omp_variant> candidates;
3426 tree variant = OMP_METADIRECTIVE_VARIANTS (metadirective);
3427 bool complete_p;
3428 tree augmented_context
3429 = omp_complete_construct_context (construct_context, &complete_p);
3430
3431 gcc_assert (variant);
3432 for (; variant; variant = TREE_CHAIN (variant))
3433 {
3434 tree selector = OMP_METADIRECTIVE_VARIANT_SELECTOR (variant);
3435
3436 /* Ignore this variant if its selector is known not to match. */
3437 if (!omp_context_selector_matches (selector, augmented_context,
3438 complete_p))
3439 continue;
3440
3441 struct omp_variant candidate;
3442 candidate.selector = selector;
3443 candidate.dynamic_selector = false;
3444 candidate.alternative = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (variant);
3445 candidate.body = OMP_METADIRECTIVE_VARIANT_BODY (variant);
3446 candidates.safe_push (candidate);
3447 }
3448 return candidates.copy ();
3449 }
3450
3451 /* Return a vector of dynamic replacement candidates for the metadirective
3452 statement in METADIRECTIVE. Return an empty vector if the metadirective
3453 cannot be resolved. This function is intended to be called from the
3454 front ends, prior to gimplification. */
3455
3456 vec<struct omp_variant>
3457 omp_early_resolve_metadirective (tree metadirective)
3458 {
3459 vec <struct omp_variant> candidates
3460 = omp_metadirective_candidates (metadirective, NULL_TREE);
3461 return omp_get_dynamic_candidates (candidates, NULL_TREE);
3462 }
3463
3464 /* Return a vector of dynamic replacement candidates for the variant construct
3465 with SELECTORS and CONSTRUCT_CONTEXT. This version is called during late
3466 resolution in the ompdevlow pass. */
3467
3468 vec<struct omp_variant>
3469 omp_resolve_variant_construct (tree construct_context, tree selectors)
3470 {
3471 auto_vec <struct omp_variant> variants;
3472
3473 for (int i = 0; i < TREE_VEC_LENGTH (selectors); i++)
3474 {
3475 struct omp_variant variant;
3476
3477 variant.selector = TREE_VEC_ELT (selectors, i);
3478 variant.dynamic_selector = false;
3479 variant.alternative = build_int_cst (integer_type_node, i + 1);
3480 variant.body = NULL_TREE;
3481
3482 variants.safe_push (variant);
3483 }
3484
3485 return omp_get_dynamic_candidates (variants, construct_context);
3486 }
3487
3488 /* Encode an oacc launch argument. This matches the GOMP_LAUNCH_PACK
3489 macro on gomp-constants.h. We do not check for overflow. */
3490
3491 tree
3492 oacc_launch_pack (unsigned code, tree device, unsigned op)
3493 {
3494 tree res;
3495
3496 res = build_int_cst (unsigned_type_node, GOMP_LAUNCH_PACK (code, 0, op));
3497 if (device)
3498 {
3499 device = fold_build2 (LSHIFT_EXPR, unsigned_type_node,
3500 device, build_int_cst (unsigned_type_node,
3501 GOMP_LAUNCH_DEVICE_SHIFT));
3502 res = fold_build2 (BIT_IOR_EXPR, unsigned_type_node, res, device);
3503 }
3504 return res;
3505 }
3506
3507 /* Openacc compute grid dimension clauses are converted to an attribute
3508 attached to the function. This permits the target-side code to (a) massage
3509 the dimensions, (b) emit that data and (c) optimize. Non-constant
3510 dimensions are pushed onto ARGS.
3511
3512 The attribute value is a TREE_LIST. A set of dimensions is
3513 represented as a list of INTEGER_CST. Those that are runtime
3514 exprs are represented as an INTEGER_CST of zero.
3515
3516 TODO: Normally the attribute will just contain a single such list. If
3517 however it contains a list of lists, this will represent the use of
3518 device_type. Each member of the outer list is an assoc list of
3519 dimensions, keyed by the device type. The first entry will be the
3520 default. Well, that's the plan. */
3521
3522 /* Replace any existing oacc fn attribute in ATTRIBS with updated
3523 dimensions. */
3524
3525 tree
3526 oacc_replace_fn_attrib_attr (tree attribs, tree dims)
3527 {
3528 tree ident = get_identifier (OACC_FN_ATTRIB);
3529
3530 /* If we happen to be present as the first attrib, drop it. */
3531 if (attribs && TREE_PURPOSE (attribs) == ident)
3532 attribs = TREE_CHAIN (attribs);
3533 return tree_cons (ident, dims, attribs);
3534 }
3535
3536 /* Replace any existing oacc fn attribute on FN with updated
3537 dimensions. */
3538
3539 void
3540 oacc_replace_fn_attrib (tree fn, tree dims)
3541 {
3542 DECL_ATTRIBUTES (fn)
3543 = oacc_replace_fn_attrib_attr (DECL_ATTRIBUTES (fn), dims);
3544 }
3545
3546 /* Scan CLAUSES for launch dimensions and attach them to the oacc
3547 function attribute. Push any that are non-constant onto the ARGS
3548 list, along with an appropriate GOMP_LAUNCH_DIM tag. */
3549
3550 void
3551 oacc_set_fn_attrib (tree fn, tree clauses, vec<tree> *args)
3552 {
3553 /* Must match GOMP_DIM ordering. */
3554 static const omp_clause_code ids[]
3555 = { OMP_CLAUSE_NUM_GANGS, OMP_CLAUSE_NUM_WORKERS,
3556 OMP_CLAUSE_VECTOR_LENGTH };
3557 unsigned ix;
3558 tree dims[GOMP_DIM_MAX];
3559
3560 tree attr = NULL_TREE;
3561 unsigned non_const = 0;
3562
3563 for (ix = GOMP_DIM_MAX; ix--;)
3564 {
3565 tree clause = omp_find_clause (clauses, ids[ix]);
3566 tree dim = NULL_TREE;
3567
3568 if (clause)
3569 dim = OMP_CLAUSE_EXPR (clause, ids[ix]);
3570 dims[ix] = dim;
3571 if (dim && TREE_CODE (dim) != INTEGER_CST)
3572 {
3573 dim = integer_zero_node;
3574 non_const |= GOMP_DIM_MASK (ix);
3575 }
3576 attr = tree_cons (NULL_TREE, dim, attr);
3577 }
3578
3579 oacc_replace_fn_attrib (fn, attr);
3580
3581 if (non_const)
3582 {
3583 /* Push a dynamic argument set. */
3584 args->safe_push (oacc_launch_pack (GOMP_LAUNCH_DIM,
3585 NULL_TREE, non_const));
3586 for (unsigned ix = 0; ix != GOMP_DIM_MAX; ix++)
3587 if (non_const & GOMP_DIM_MASK (ix))
3588 args->safe_push (dims[ix]);
3589 }
3590 }
3591
3592 /* Verify OpenACC routine clauses.
3593
3594 Returns 0 if FNDECL should be marked with an OpenACC 'routine' directive, 1
3595 if it has already been marked in compatible way, and -1 if incompatible.
3596 Upon returning, the chain of clauses will contain exactly one clause
3597 specifying the level of parallelism. */
3598
3599 int
3600 oacc_verify_routine_clauses (tree fndecl, tree *clauses, location_t loc,
3601 const char *routine_str)
3602 {
3603 tree c_level = NULL_TREE;
3604 tree c_nohost = NULL_TREE;
3605 tree c_p = NULL_TREE;
3606 for (tree c = *clauses; c; c_p = c, c = OMP_CLAUSE_CHAIN (c))
3607 switch (OMP_CLAUSE_CODE (c))
3608 {
3609 case OMP_CLAUSE_GANG:
3610 case OMP_CLAUSE_WORKER:
3611 case OMP_CLAUSE_VECTOR:
3612 case OMP_CLAUSE_SEQ:
3613 if (c_level == NULL_TREE)
3614 c_level = c;
3615 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_CODE (c_level))
3616 {
3617 /* This has already been diagnosed in the front ends. */
3618 /* Drop the duplicate clause. */
3619 gcc_checking_assert (c_p != NULL_TREE);
3620 OMP_CLAUSE_CHAIN (c_p) = OMP_CLAUSE_CHAIN (c);
3621 c = c_p;
3622 }
3623 else
3624 {
3625 error_at (OMP_CLAUSE_LOCATION (c),
3626 "%qs specifies a conflicting level of parallelism",
3627 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
3628 inform (OMP_CLAUSE_LOCATION (c_level),
3629 "... to the previous %qs clause here",
3630 omp_clause_code_name[OMP_CLAUSE_CODE (c_level)]);
3631 /* Drop the conflicting clause. */
3632 gcc_checking_assert (c_p != NULL_TREE);
3633 OMP_CLAUSE_CHAIN (c_p) = OMP_CLAUSE_CHAIN (c);
3634 c = c_p;
3635 }
3636 break;
3637 case OMP_CLAUSE_NOHOST:
3638 /* Don't worry about duplicate clauses here. */
3639 c_nohost = c;
3640 break;
3641 default:
3642 gcc_unreachable ();
3643 }
3644 if (c_level == NULL_TREE)
3645 {
3646 /* Default to an implicit 'seq' clause. */
3647 c_level = build_omp_clause (loc, OMP_CLAUSE_SEQ);
3648 OMP_CLAUSE_CHAIN (c_level) = *clauses;
3649 *clauses = c_level;
3650 }
3651 /* In *clauses, we now have exactly one clause specifying the level of
3652 parallelism. */
3653
3654 tree attr
3655 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl));
3656 if (attr != NULL_TREE)
3657 {
3658 /* Diagnose if "#pragma omp declare target" has also been applied. */
3659 if (TREE_VALUE (attr) == NULL_TREE)
3660 {
3661 /* See <https://gcc.gnu.org/PR93465>; the semantics of combining
3662 OpenACC and OpenMP 'target' are not clear. */
3663 error_at (loc,
3664 "cannot apply %qs to %qD, which has also been"
3665 " marked with an OpenMP 'declare target' directive",
3666 routine_str, fndecl);
3667 /* Incompatible. */
3668 return -1;
3669 }
3670
3671 /* If a "#pragma acc routine" has already been applied, just verify
3672 this one for compatibility. */
3673 /* Collect previous directive's clauses. */
3674 tree c_level_p = NULL_TREE;
3675 tree c_nohost_p = NULL_TREE;
3676 for (tree c = TREE_VALUE (attr); c; c = OMP_CLAUSE_CHAIN (c))
3677 switch (OMP_CLAUSE_CODE (c))
3678 {
3679 case OMP_CLAUSE_GANG:
3680 case OMP_CLAUSE_WORKER:
3681 case OMP_CLAUSE_VECTOR:
3682 case OMP_CLAUSE_SEQ:
3683 gcc_checking_assert (c_level_p == NULL_TREE);
3684 c_level_p = c;
3685 break;
3686 case OMP_CLAUSE_NOHOST:
3687 gcc_checking_assert (c_nohost_p == NULL_TREE);
3688 c_nohost_p = c;
3689 break;
3690 default:
3691 gcc_unreachable ();
3692 }
3693 gcc_checking_assert (c_level_p != NULL_TREE);
3694 /* ..., and compare to current directive's, which we've already collected
3695 above. */
3696 tree c_diag;
3697 tree c_diag_p;
3698 /* Matching level of parallelism? */
3699 if (OMP_CLAUSE_CODE (c_level) != OMP_CLAUSE_CODE (c_level_p))
3700 {
3701 c_diag = c_level;
3702 c_diag_p = c_level_p;
3703 goto incompatible;
3704 }
3705 /* Matching 'nohost' clauses? */
3706 if ((c_nohost == NULL_TREE) != (c_nohost_p == NULL_TREE))
3707 {
3708 c_diag = c_nohost;
3709 c_diag_p = c_nohost_p;
3710 goto incompatible;
3711 }
3712 /* Compatible. */
3713 return 1;
3714
3715 incompatible:
3716 if (c_diag != NULL_TREE)
3717 error_at (OMP_CLAUSE_LOCATION (c_diag),
3718 "incompatible %qs clause when applying"
3719 " %qs to %qD, which has already been"
3720 " marked with an OpenACC 'routine' directive",
3721 omp_clause_code_name[OMP_CLAUSE_CODE (c_diag)],
3722 routine_str, fndecl);
3723 else if (c_diag_p != NULL_TREE)
3724 error_at (loc,
3725 "missing %qs clause when applying"
3726 " %qs to %qD, which has already been"
3727 " marked with an OpenACC 'routine' directive",
3728 omp_clause_code_name[OMP_CLAUSE_CODE (c_diag_p)],
3729 routine_str, fndecl);
3730 else
3731 gcc_unreachable ();
3732 if (c_diag_p != NULL_TREE)
3733 inform (OMP_CLAUSE_LOCATION (c_diag_p),
3734 "... with %qs clause here",
3735 omp_clause_code_name[OMP_CLAUSE_CODE (c_diag_p)]);
3736 else
3737 {
3738 /* In the front ends, we don't preserve location information for the
3739 OpenACC routine directive itself. However, that of c_level_p
3740 should be close. */
3741 location_t loc_routine = OMP_CLAUSE_LOCATION (c_level_p);
3742 inform (loc_routine, "... without %qs clause near to here",
3743 omp_clause_code_name[OMP_CLAUSE_CODE (c_diag)]);
3744 }
3745 /* Incompatible. */
3746 return -1;
3747 }
3748
3749 return 0;
3750 }
3751
3752 /* Process the OpenACC 'routine' directive clauses to generate an attribute
3753 for the level of parallelism. All dimensions have a size of zero
3754 (dynamic). TREE_PURPOSE is set to indicate whether that dimension
3755 can have a loop partitioned on it. non-zero indicates
3756 yes, zero indicates no. By construction once a non-zero has been
3757 reached, further inner dimensions must also be non-zero. We set
3758 TREE_VALUE to zero for the dimensions that may be partitioned and
3759 1 for the other ones -- if a loop is (erroneously) spawned at
3760 an outer level, we don't want to try and partition it. */
3761
3762 tree
3763 oacc_build_routine_dims (tree clauses)
3764 {
3765 /* Must match GOMP_DIM ordering. */
3766 static const omp_clause_code ids[]
3767 = {OMP_CLAUSE_GANG, OMP_CLAUSE_WORKER, OMP_CLAUSE_VECTOR, OMP_CLAUSE_SEQ};
3768 int ix;
3769 int level = -1;
3770
3771 for (; clauses; clauses = OMP_CLAUSE_CHAIN (clauses))
3772 for (ix = GOMP_DIM_MAX + 1; ix--;)
3773 if (OMP_CLAUSE_CODE (clauses) == ids[ix])
3774 {
3775 level = ix;
3776 break;
3777 }
3778 gcc_checking_assert (level >= 0);
3779
3780 tree dims = NULL_TREE;
3781
3782 for (ix = GOMP_DIM_MAX; ix--;)
3783 dims = tree_cons (build_int_cst (boolean_type_node, ix >= level),
3784 build_int_cst (integer_type_node, ix < level), dims);
3785
3786 return dims;
3787 }
3788
3789 /* Retrieve the oacc function attrib and return it. Non-oacc
3790 functions will return NULL. */
3791
3792 tree
3793 oacc_get_fn_attrib (tree fn)
3794 {
3795 return lookup_attribute (OACC_FN_ATTRIB, DECL_ATTRIBUTES (fn));
3796 }
3797
3798 /* Return true if FN is an OpenMP or OpenACC offloading function. */
3799
3800 bool
3801 offloading_function_p (tree fn)
3802 {
3803 tree attrs = DECL_ATTRIBUTES (fn);
3804 return (lookup_attribute ("omp declare target", attrs)
3805 || lookup_attribute ("omp target entrypoint", attrs));
3806 }
3807
3808 /* Extract an oacc execution dimension from FN. FN must be an
3809 offloaded function or routine that has already had its execution
3810 dimensions lowered to the target-specific values. */
3811
3812 int
3813 oacc_get_fn_dim_size (tree fn, int axis)
3814 {
3815 tree attrs = oacc_get_fn_attrib (fn);
3816
3817 gcc_assert (axis < GOMP_DIM_MAX);
3818
3819 tree dims = TREE_VALUE (attrs);
3820 while (axis--)
3821 dims = TREE_CHAIN (dims);
3822
3823 int size = TREE_INT_CST_LOW (TREE_VALUE (dims));
3824
3825 return size;
3826 }
3827
3828 /* Extract the dimension axis from an IFN_GOACC_DIM_POS or
3829 IFN_GOACC_DIM_SIZE call. */
3830
3831 int
3832 oacc_get_ifn_dim_arg (const gimple *stmt)
3833 {
3834 gcc_checking_assert (gimple_call_internal_fn (stmt) == IFN_GOACC_DIM_SIZE
3835 || gimple_call_internal_fn (stmt) == IFN_GOACC_DIM_POS);
3836 tree arg = gimple_call_arg (stmt, 0);
3837 HOST_WIDE_INT axis = TREE_INT_CST_LOW (arg);
3838
3839 gcc_checking_assert (axis >= 0 && axis < GOMP_DIM_MAX);
3840 return (int) axis;
3841 }
3842
3843 /* Build COMPONENT_REF and set TREE_THIS_VOLATILE and TREE_READONLY on it
3844 as appropriate. */
3845
3846 tree
3847 omp_build_component_ref (tree obj, tree field)
3848 {
3849 tree ret = build3 (COMPONENT_REF, TREE_TYPE (field), obj, field, NULL);
3850 if (TREE_THIS_VOLATILE (field))
3851 TREE_THIS_VOLATILE (ret) |= 1;
3852 if (TREE_READONLY (field))
3853 TREE_READONLY (ret) |= 1;
3854 return ret;
3855 }
3856
3857 /* Return true if NAME is the name of an omp_* runtime API call. */
3858 bool
3859 omp_runtime_api_procname (const char *name)
3860 {
3861 if (!startswith (name, "omp_"))
3862 return false;
3863
3864 static const char *omp_runtime_apis[] =
3865 {
3866 /* This array has 3 sections. First omp_* calls that don't
3867 have any suffixes. */
3868 "aligned_alloc",
3869 "aligned_calloc",
3870 "alloc",
3871 "calloc",
3872 "free",
3873 "get_interop_int",
3874 "get_interop_ptr",
3875 "get_mapped_ptr",
3876 "get_num_interop_properties",
3877 "realloc",
3878 "target_alloc",
3879 "target_associate_ptr",
3880 "target_disassociate_ptr",
3881 "target_free",
3882 "target_is_accessible",
3883 "target_is_present",
3884 "target_memcpy",
3885 "target_memcpy_async",
3886 "target_memcpy_rect",
3887 "target_memcpy_rect_async",
3888 NULL,
3889 /* Now omp_* calls that are available as omp_* and omp_*_; however, the
3890 DECL_NAME is always omp_* without tailing underscore. */
3891 "capture_affinity",
3892 "destroy_allocator",
3893 "destroy_lock",
3894 "destroy_nest_lock",
3895 "display_affinity",
3896 "fulfill_event",
3897 "get_active_level",
3898 "get_affinity_format",
3899 "get_cancellation",
3900 "get_default_allocator",
3901 "get_default_device",
3902 "get_device_from_uid",
3903 "get_device_num",
3904 "get_dynamic",
3905 "get_initial_device",
3906 "get_interop_name",
3907 "get_interop_rc_desc",
3908 "get_interop_str",
3909 "get_interop_type_desc",
3910 "get_level",
3911 "get_max_active_levels",
3912 "get_max_task_priority",
3913 "get_max_teams",
3914 "get_max_threads",
3915 "get_nested",
3916 "get_num_devices",
3917 "get_num_places",
3918 "get_num_procs",
3919 "get_num_teams",
3920 "get_num_threads",
3921 "get_partition_num_places",
3922 "get_place_num",
3923 "get_proc_bind",
3924 "get_supported_active_levels",
3925 "get_team_num",
3926 "get_teams_thread_limit",
3927 "get_thread_limit",
3928 "get_thread_num",
3929 "get_wtick",
3930 "get_wtime",
3931 "in_explicit_task",
3932 "in_final",
3933 "in_parallel",
3934 "init_lock",
3935 "init_nest_lock",
3936 "is_initial_device",
3937 "pause_resource",
3938 "pause_resource_all",
3939 "set_affinity_format",
3940 "set_default_allocator",
3941 "set_lock",
3942 "set_nest_lock",
3943 "test_lock",
3944 "test_nest_lock",
3945 "unset_lock",
3946 "unset_nest_lock",
3947 NULL,
3948 /* And finally calls available as omp_*, omp_*_ and omp_*_8_; however,
3949 as DECL_NAME only omp_* and omp_*_8 appear. */
3950 "display_env",
3951 "get_ancestor_thread_num",
3952 "get_uid_from_device",
3953 "get_partition_place_nums",
3954 "get_place_num_procs",
3955 "get_place_proc_ids",
3956 "get_schedule",
3957 "get_team_size",
3958 "init_allocator",
3959 "set_default_device",
3960 "set_dynamic",
3961 "set_max_active_levels",
3962 "set_nested",
3963 "set_num_teams",
3964 "set_num_threads",
3965 "set_schedule",
3966 "set_teams_thread_limit"
3967 };
3968
3969 int mode = 0;
3970 for (unsigned i = 0; i < ARRAY_SIZE (omp_runtime_apis); i++)
3971 {
3972 if (omp_runtime_apis[i] == NULL)
3973 {
3974 mode++;
3975 continue;
3976 }
3977 size_t len = strlen (omp_runtime_apis[i]);
3978 if (strncmp (name + 4, omp_runtime_apis[i], len) == 0
3979 && (name[4 + len] == '\0'
3980 || (mode > 1 && strcmp (name + 4 + len, "_8") == 0)))
3981 return true;
3982 }
3983 return false;
3984 }
3985
3986 /* Return true if FNDECL is an omp_* runtime API call. */
3987
3988 bool
3989 omp_runtime_api_call (const_tree fndecl)
3990 {
3991 tree declname = DECL_NAME (fndecl);
3992 if (!declname
3993 || (DECL_CONTEXT (fndecl) != NULL_TREE
3994 && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
3995 || !TREE_PUBLIC (fndecl))
3996 return false;
3997 return omp_runtime_api_procname (IDENTIFIER_POINTER (declname));
3998 }
3999
4000 /* See "Additional Definitions for the OpenMP API Specification" document;
4001 associated IDs are 1, 2, ... */
4002 static const char* omp_interop_fr_str[] = {"cuda", "cuda_driver", "opencl",
4003 "sycl", "hip", "level_zero", "hsa"};
4004
4005 /* Returns the foreign-runtime ID if found or 0 otherwise. */
4006
4007 char
4008 omp_get_fr_id_from_name (const char *str)
4009 {
4010 static_assert (GOMP_INTEROP_IFR_LAST == ARRAY_SIZE (omp_interop_fr_str), "");
4011
4012 for (unsigned i = 0; i < ARRAY_SIZE (omp_interop_fr_str); ++i)
4013 if (!strcmp (str, omp_interop_fr_str[i]))
4014 return i + 1;
4015 return GOMP_INTEROP_IFR_UNKNOWN;
4016 }
4017
4018 /* Returns the string value to a foreign-runtime integer value or NULL if value
4019 is not known. */
4020
4021 const char *
4022 omp_get_name_from_fr_id (int fr_id)
4023 {
4024 if (fr_id < 1 || fr_id > (int) ARRAY_SIZE (omp_interop_fr_str))
4025 return "<unknown>";
4026 return omp_interop_fr_str[fr_id-1];
4027 }
4028
4029 namespace omp_addr_tokenizer {
4030
4031 /* We scan an expression by recursive descent, and build a vector of
4032 "omp_addr_token *" pointers representing a "parsed" version of the
4033 expression. The grammar we use is something like this:
4034
4035 expr0::
4036 expr [section-access]
4037
4038 expr::
4039 structured-expr access-method
4040 | array-base access-method
4041
4042 structured-expr::
4043 structure-base component-selector
4044
4045 arbitrary-expr::
4046 (anything else)
4047
4048 structure-base::
4049 DECL access-method
4050 | structured-expr access-method
4051 | arbitrary-expr access-method
4052
4053 array-base::
4054 DECL
4055 | arbitrary-expr
4056
4057 access-method::
4058 DIRECT
4059 | REF
4060 | POINTER
4061 | REF_TO_POINTER
4062 | POINTER_OFFSET
4063 | REF_TO_POINTER_OFFSET
4064 | INDEXED_ARRAY
4065 | INDEXED_REF_TO_ARRAY
4066 | index-expr
4067
4068 index-expr::
4069 INDEX_EXPR access-method
4070
4071 component-selector::
4072 component-selector COMPONENT_REF
4073 | component-selector ARRAY_REF
4074 | COMPONENT_REF
4075
4076 This tokenized form is then used both in parsing, for OpenMP clause
4077 expansion (for C and C++) and in gimplify.cc for sibling-list handling
4078 (for C, C++ and Fortran). */
4079
4080 omp_addr_token::omp_addr_token (token_type t, tree e)
4081 : type(t), expr(e)
4082 {
4083 }
4084
4085 omp_addr_token::omp_addr_token (access_method_kinds k, tree e)
4086 : type(ACCESS_METHOD), expr(e)
4087 {
4088 u.access_kind = k;
4089 }
4090
4091 omp_addr_token::omp_addr_token (token_type t, structure_base_kinds k, tree e)
4092 : type(t), expr(e)
4093 {
4094 u.structure_base_kind = k;
4095 }
4096
4097 static bool
4098 omp_parse_component_selector (tree *expr0)
4099 {
4100 tree expr = *expr0;
4101 tree last_component = NULL_TREE;
4102
4103 while (TREE_CODE (expr) == COMPONENT_REF
4104 || TREE_CODE (expr) == ARRAY_REF)
4105 {
4106 if (TREE_CODE (expr) == COMPONENT_REF)
4107 last_component = expr;
4108
4109 expr = TREE_OPERAND (expr, 0);
4110
4111 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
4112 break;
4113 }
4114
4115 if (!last_component)
4116 return false;
4117
4118 *expr0 = last_component;
4119 return true;
4120 }
4121
4122 /* This handles references that have had convert_from_reference called on
4123 them, and also those that haven't. */
4124
4125 static bool
4126 omp_parse_ref (tree *expr0)
4127 {
4128 tree expr = *expr0;
4129
4130 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
4131 return true;
4132 else if ((TREE_CODE (expr) == INDIRECT_REF
4133 || (TREE_CODE (expr) == MEM_REF
4134 && integer_zerop (TREE_OPERAND (expr, 1))))
4135 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
4136 {
4137 *expr0 = TREE_OPERAND (expr, 0);
4138 return true;
4139 }
4140
4141 return false;
4142 }
4143
4144 static bool
4145 omp_parse_pointer (tree *expr0, bool *has_offset)
4146 {
4147 tree expr = *expr0;
4148
4149 *has_offset = false;
4150
4151 if ((TREE_CODE (expr) == INDIRECT_REF
4152 || (TREE_CODE (expr) == MEM_REF
4153 && integer_zerop (TREE_OPERAND (expr, 1))))
4154 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == POINTER_TYPE)
4155 {
4156 expr = TREE_OPERAND (expr, 0);
4157
4158 /* The Fortran FE sometimes emits a no-op cast here. */
4159 STRIP_NOPS (expr);
4160
4161 while (1)
4162 {
4163 if (TREE_CODE (expr) == COMPOUND_EXPR)
4164 {
4165 expr = TREE_OPERAND (expr, 1);
4166 STRIP_NOPS (expr);
4167 }
4168 else if (TREE_CODE (expr) == SAVE_EXPR)
4169 expr = TREE_OPERAND (expr, 0);
4170 else if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
4171 {
4172 *has_offset = true;
4173 expr = TREE_OPERAND (expr, 0);
4174 }
4175 else
4176 break;
4177 }
4178
4179 STRIP_NOPS (expr);
4180
4181 *expr0 = expr;
4182 return true;
4183 }
4184
4185 return false;
4186 }
4187
4188 static void
4189 omp_parse_access_method (tree *expr0, enum access_method_kinds *kind)
4190 {
4191 tree expr = *expr0;
4192 bool has_offset;
4193
4194 if (omp_parse_ref (&expr))
4195 *kind = ACCESS_REF;
4196 else if (omp_parse_pointer (&expr, &has_offset))
4197 {
4198 if (omp_parse_ref (&expr))
4199 *kind = has_offset ? ACCESS_REF_TO_POINTER_OFFSET
4200 : ACCESS_REF_TO_POINTER;
4201 else
4202 *kind = has_offset ? ACCESS_POINTER_OFFSET : ACCESS_POINTER;
4203 }
4204 else if (TREE_CODE (expr) == ARRAY_REF)
4205 {
4206 while (TREE_CODE (expr) == ARRAY_REF)
4207 expr = TREE_OPERAND (expr, 0);
4208 if (omp_parse_ref (&expr))
4209 *kind = ACCESS_INDEXED_REF_TO_ARRAY;
4210 else
4211 *kind = ACCESS_INDEXED_ARRAY;
4212 }
4213 else
4214 *kind = ACCESS_DIRECT;
4215
4216 STRIP_NOPS (expr);
4217
4218 *expr0 = expr;
4219 }
4220
4221 static void
4222 omp_parse_access_methods (vec<omp_addr_token *> &addr_tokens, tree *expr0)
4223 {
4224 tree expr = *expr0;
4225 enum access_method_kinds kind;
4226 tree am_expr;
4227
4228 omp_parse_access_method (&expr, &kind);
4229 am_expr = expr;
4230
4231 if (TREE_CODE (expr) == INDIRECT_REF
4232 || TREE_CODE (expr) == MEM_REF
4233 || TREE_CODE (expr) == ARRAY_REF)
4234 omp_parse_access_methods (addr_tokens, &expr);
4235
4236 addr_tokens.safe_push (new omp_addr_token (kind, am_expr));
4237
4238 *expr0 = expr;
4239 }
4240
4241 static bool omp_parse_structured_expr (vec<omp_addr_token *> &, tree *);
4242
4243 static bool
4244 omp_parse_structure_base (vec<omp_addr_token *> &addr_tokens,
4245 tree *expr0, structure_base_kinds *kind,
4246 vec<omp_addr_token *> &base_access_tokens,
4247 bool allow_structured = true)
4248 {
4249 tree expr = *expr0;
4250
4251 if (allow_structured)
4252 omp_parse_access_methods (base_access_tokens, &expr);
4253
4254 if (DECL_P (expr))
4255 {
4256 *kind = BASE_DECL;
4257 return true;
4258 }
4259
4260 if (allow_structured && omp_parse_structured_expr (addr_tokens, &expr))
4261 {
4262 *kind = BASE_COMPONENT_EXPR;
4263 *expr0 = expr;
4264 return true;
4265 }
4266
4267 *kind = BASE_ARBITRARY_EXPR;
4268 *expr0 = expr;
4269 return true;
4270 }
4271
4272 static bool
4273 omp_parse_structured_expr (vec<omp_addr_token *> &addr_tokens, tree *expr0)
4274 {
4275 tree expr = *expr0;
4276 tree base_component = NULL_TREE;
4277 structure_base_kinds struct_base_kind;
4278 auto_vec<omp_addr_token *> base_access_tokens;
4279
4280 if (omp_parse_component_selector (&expr))
4281 base_component = expr;
4282 else
4283 return false;
4284
4285 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
4286 expr = TREE_OPERAND (expr, 0);
4287
4288 tree structure_base = expr;
4289
4290 if (!omp_parse_structure_base (addr_tokens, &expr, &struct_base_kind,
4291 base_access_tokens))
4292 return false;
4293
4294 addr_tokens.safe_push (new omp_addr_token (STRUCTURE_BASE, struct_base_kind,
4295 structure_base));
4296 addr_tokens.safe_splice (base_access_tokens);
4297 addr_tokens.safe_push (new omp_addr_token (COMPONENT_SELECTOR,
4298 base_component));
4299
4300 *expr0 = expr;
4301
4302 return true;
4303 }
4304
4305 static bool
4306 omp_parse_array_expr (vec<omp_addr_token *> &addr_tokens, tree *expr0)
4307 {
4308 tree expr = *expr0;
4309 structure_base_kinds s_kind;
4310 auto_vec<omp_addr_token *> base_access_tokens;
4311
4312 if (!omp_parse_structure_base (addr_tokens, &expr, &s_kind,
4313 base_access_tokens, false))
4314 return false;
4315
4316 addr_tokens.safe_push (new omp_addr_token (ARRAY_BASE, s_kind, expr));
4317 addr_tokens.safe_splice (base_access_tokens);
4318
4319 *expr0 = expr;
4320 return true;
4321 }
4322
4323 /* Return TRUE if the ACCESS_METHOD token at index 'i' has a further
4324 ACCESS_METHOD chained after it (e.g., if we're processing an expression
4325 containing multiple pointer indirections). */
4326
4327 bool
4328 omp_access_chain_p (vec<omp_addr_token *> &addr_tokens, unsigned i)
4329 {
4330 gcc_assert (addr_tokens[i]->type == ACCESS_METHOD);
4331 return (i + 1 < addr_tokens.length ()
4332 && addr_tokens[i + 1]->type == ACCESS_METHOD);
4333 }
4334
4335 /* Return the address of the object accessed by the ACCESS_METHOD token
4336 at 'i': either of the next access method's expr, or of EXPR if we're at
4337 the end of the list of tokens. */
4338
4339 tree
4340 omp_accessed_addr (vec<omp_addr_token *> &addr_tokens, unsigned i, tree expr)
4341 {
4342 if (i + 1 < addr_tokens.length ())
4343 return build_fold_addr_expr (addr_tokens[i + 1]->expr);
4344 else
4345 return build_fold_addr_expr (expr);
4346 }
4347
4348 } /* namespace omp_addr_tokenizer. */
4349
4350 bool
4351 omp_parse_expr (vec<omp_addr_token *> &addr_tokens, tree expr)
4352 {
4353 using namespace omp_addr_tokenizer;
4354 auto_vec<omp_addr_token *> expr_access_tokens;
4355
4356 omp_parse_access_methods (expr_access_tokens, &expr);
4357
4358 if (omp_parse_structured_expr (addr_tokens, &expr))
4359 ;
4360 else if (omp_parse_array_expr (addr_tokens, &expr))
4361 ;
4362 else
4363 return false;
4364
4365 addr_tokens.safe_splice (expr_access_tokens);
4366
4367 return true;
4368 }
4369
4370 DEBUG_FUNCTION void
4371 debug_omp_tokenized_addr (vec<omp_addr_token *> &addr_tokens,
4372 bool with_exprs)
4373 {
4374 using namespace omp_addr_tokenizer;
4375 const char *sep = with_exprs ? " " : "";
4376
4377 for (auto e : addr_tokens)
4378 {
4379 const char *pfx = "";
4380
4381 fputs (sep, stderr);
4382
4383 switch (e->type)
4384 {
4385 case COMPONENT_SELECTOR:
4386 fputs ("component_selector", stderr);
4387 break;
4388 case ACCESS_METHOD:
4389 switch (e->u.access_kind)
4390 {
4391 case ACCESS_DIRECT:
4392 fputs ("access_direct", stderr);
4393 break;
4394 case ACCESS_REF:
4395 fputs ("access_ref", stderr);
4396 break;
4397 case ACCESS_POINTER:
4398 fputs ("access_pointer", stderr);
4399 break;
4400 case ACCESS_POINTER_OFFSET:
4401 fputs ("access_pointer_offset", stderr);
4402 break;
4403 case ACCESS_REF_TO_POINTER:
4404 fputs ("access_ref_to_pointer", stderr);
4405 break;
4406 case ACCESS_REF_TO_POINTER_OFFSET:
4407 fputs ("access_ref_to_pointer_offset", stderr);
4408 break;
4409 case ACCESS_INDEXED_ARRAY:
4410 fputs ("access_indexed_array", stderr);
4411 break;
4412 case ACCESS_INDEXED_REF_TO_ARRAY:
4413 fputs ("access_indexed_ref_to_array", stderr);
4414 break;
4415 }
4416 break;
4417 case ARRAY_BASE:
4418 case STRUCTURE_BASE:
4419 pfx = e->type == ARRAY_BASE ? "array_" : "struct_";
4420 switch (e->u.structure_base_kind)
4421 {
4422 case BASE_DECL:
4423 fprintf (stderr, "%sbase_decl", pfx);
4424 break;
4425 case BASE_COMPONENT_EXPR:
4426 fputs ("base_component_expr", stderr);
4427 break;
4428 case BASE_ARBITRARY_EXPR:
4429 fprintf (stderr, "%sbase_arbitrary_expr", pfx);
4430 break;
4431 }
4432 break;
4433 }
4434 if (with_exprs)
4435 {
4436 fputs (" [", stderr);
4437 print_generic_expr (stderr, e->expr);
4438 fputc (']', stderr);
4439 sep = ",\n ";
4440 }
4441 else
4442 sep = " ";
4443 }
4444
4445 fputs ("\n", stderr);
4446 }
4447
4448 /* Return number of iterations of loop I in FOR_STMT. If PSTEP is non-NULL,
4449 *PSTEP will be the loop step. */
4450
4451 tree
4452 omp_loop_number_of_iterations (tree for_stmt, int i, tree *pstep)
4453 {
4454 tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
4455 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
4456 tree decl = TREE_OPERAND (t, 0);
4457 tree n1 = TREE_OPERAND (t, 1);
4458 tree type = TREE_TYPE (decl);
4459 tree cond = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
4460 gcc_assert (COMPARISON_CLASS_P (cond));
4461 gcc_assert (TREE_OPERAND (cond, 0) == decl);
4462 tree_code cond_code = TREE_CODE (cond);
4463 tree n2 = TREE_OPERAND (cond, 1);
4464 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
4465 tree step = NULL_TREE;
4466 switch (TREE_CODE (t))
4467 {
4468 case PREINCREMENT_EXPR:
4469 case POSTINCREMENT_EXPR:
4470 gcc_assert (!POINTER_TYPE_P (type));
4471 gcc_assert (TREE_OPERAND (t, 0) == decl);
4472 step = build_int_cst (type, 1);
4473 break;
4474 case PREDECREMENT_EXPR:
4475 case POSTDECREMENT_EXPR:
4476 gcc_assert (!POINTER_TYPE_P (type));
4477 gcc_assert (TREE_OPERAND (t, 0) == decl);
4478 step = build_int_cst (type, -1);
4479 break;
4480 case MODIFY_EXPR:
4481 gcc_assert (TREE_OPERAND (t, 0) == decl);
4482 t = TREE_OPERAND (t, 1);
4483 switch (TREE_CODE (t))
4484 {
4485 case PLUS_EXPR:
4486 if (TREE_OPERAND (t, 1) == decl)
4487 {
4488 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
4489 TREE_OPERAND (t, 0) = decl;
4490 }
4491 /* FALLTHRU */
4492 case POINTER_PLUS_EXPR:
4493 case MINUS_EXPR:
4494 step = omp_get_for_step_from_incr (EXPR_LOCATION (t), t);
4495 break;
4496 default:
4497 gcc_unreachable ();
4498 }
4499 break;
4500 default:
4501 gcc_unreachable ();
4502 }
4503 omp_adjust_for_condition (EXPR_LOCATION (for_stmt), &cond_code, &n2,
4504 decl, step);
4505 if (pstep)
4506 *pstep = step;
4507 if (INTEGRAL_TYPE_P (type)
4508 && TYPE_PRECISION (type) < TYPE_PRECISION (long_long_integer_type_node))
4509 {
4510 n1 = fold_convert (long_long_integer_type_node, n1);
4511 n2 = fold_convert (long_long_integer_type_node, n2);
4512 step = fold_convert (long_long_integer_type_node, step);
4513 }
4514 if (cond_code == LT_EXPR
4515 || POINTER_TYPE_P (type)
4516 || !TYPE_UNSIGNED (TREE_TYPE (n1)))
4517 {
4518 if (POINTER_TYPE_P (type))
4519 t = fold_build2 (POINTER_DIFF_EXPR, ssizetype, n2, n1);
4520 else
4521 t = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n2, n1);
4522 t = fold_build2 (CEIL_DIV_EXPR, TREE_TYPE (t), t, step);
4523 }
4524 else
4525 {
4526 t = fold_build2 (MINUS_EXPR, type, n1, n2);
4527 t = fold_build2 (CEIL_DIV_EXPR, type, t,
4528 fold_build1 (NEGATE_EXPR, type, step));
4529 }
4530 return t;
4531 }
4532
4533 /* Tile transformation:
4534 Original loop:
4535
4536 #pragma omp tile sizes(16, 32)
4537 for (i = 0; i < k; ++i)
4538 for (j = 0; j < 128; j += 2)
4539 {
4540 baz (i, j);
4541 }
4542
4543 Transformed loop:
4544 #pragma omp tile sizes(16, 32)
4545 for (i.0 = 0; i.0 < k; i.0 += 16)
4546 for (j.0 = 0; j.0 < 128; j.0 += 64)
4547 {
4548 i = i.0;
4549 i.1 = MIN_EXPR <i.0 + 16, k>;
4550 goto <D.2783>;
4551 <D.2782>:;
4552 j = j.0;
4553 j.1 = j.0 + 32;
4554 goto <D.2786>;
4555 <D.2785>:;
4556 {
4557 baz (i, j);
4558 }
4559 j += 2;
4560 <D.2786>:;
4561 if (j < j.1) goto <D.2785>; else goto <D.2787>;
4562 <D.2787>:;
4563 ++i;
4564 <D.2783>:;
4565 if (i < i.1) goto <D.2782>; else goto <D.2784>;
4566 <D.2784>:;
4567 }
4568
4569 where the grid loops have canonical form, but the inner
4570 loops don't and so are immediately lowered. */
4571
4572 static void
4573 omp_apply_tile (tree for_stmt, tree sizes, int size)
4574 {
4575 tree pre_body = NULL_TREE, post_body = NULL_TREE;
4576 tree orig_sizes = sizes;
4577 if (OMP_FOR_NON_RECTANGULAR (for_stmt))
4578 {
4579 error_at (EXPR_LOCATION (for_stmt), "non-rectangular %<tile%>");
4580 return;
4581 }
4582 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
4583 {
4584 if (orig_sizes)
4585 {
4586 size = tree_to_uhwi (TREE_VALUE (sizes));
4587 sizes = TREE_CHAIN (sizes);
4588 }
4589 if (size == 1)
4590 continue;
4591 if (OMP_FOR_ORIG_DECLS (for_stmt) == NULL_TREE)
4592 {
4593 OMP_FOR_ORIG_DECLS (for_stmt)
4594 = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)));
4595 for (int j = 0; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
4596 {
4597 gcc_assert (TREE_CODE (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j))
4598 == MODIFY_EXPR);
4599 TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), j)
4600 = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j), 0);
4601 }
4602 }
4603 tree step;
4604 tree iters = omp_loop_number_of_iterations (for_stmt, i, &step);
4605 tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
4606 tree decl = TREE_OPERAND (t, 0);
4607 tree type = TREE_TYPE (decl);
4608 tree griddecl = create_tmp_var_raw (type);
4609 DECL_CONTEXT (griddecl) = current_function_decl;
4610 t = build1 (DECL_EXPR, void_type_node, griddecl);
4611 append_to_statement_list (t, &OMP_FOR_PRE_BODY (for_stmt));
4612 TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i), 0) = griddecl;
4613 TREE_PRIVATE (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i)) = 1;
4614 tree cond = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
4615 TREE_OPERAND (cond, 0) = griddecl;
4616 tree ub = save_expr (TREE_OPERAND (cond, 1));
4617 TREE_OPERAND (cond, 1) = ub;
4618 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
4619 if (TREE_CODE (cond) == NE_EXPR)
4620 {
4621 tree_code cond_code = TREE_CODE (cond);
4622 omp_adjust_for_condition (EXPR_LOCATION (for_stmt), &cond_code,
4623 &ub, griddecl, step);
4624 TREE_SET_CODE (cond, cond_code);
4625 }
4626 step = save_expr (step);
4627 tree gridstep = fold_build2 (MULT_EXPR, TREE_TYPE (step),
4628 step, build_int_cst (TREE_TYPE (step),
4629 size));
4630 if (POINTER_TYPE_P (type))
4631 t = build2 (POINTER_PLUS_EXPR, type, griddecl,
4632 fold_convert (sizetype, gridstep));
4633 else
4634 t = build2 (PLUS_EXPR, type, griddecl, gridstep);
4635 t = build2 (MODIFY_EXPR, type, griddecl, t);
4636 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
4637 t = build2 (MODIFY_EXPR, type, decl, griddecl);
4638 append_to_statement_list (t, &pre_body);
4639 if (POINTER_TYPE_P (type))
4640 t = build2 (POINTER_PLUS_EXPR, type, griddecl,
4641 fold_convert (sizetype, gridstep));
4642 else
4643 t = build2 (PLUS_EXPR, type, griddecl, gridstep);
4644 bool minmax_needed = true;
4645 if (TREE_CODE (iters) == INTEGER_CST)
4646 {
4647 wide_int witers = wi::to_wide (iters);
4648 wide_int wsize = wide_int::from (size, witers.get_precision (),
4649 TYPE_SIGN (TREE_TYPE (iters)));
4650 if (wi::multiple_of_p (witers, wsize, TYPE_SIGN (TREE_TYPE (iters))))
4651 minmax_needed = false;
4652 }
4653 if (minmax_needed)
4654 switch (TREE_CODE (cond))
4655 {
4656 case LE_EXPR:
4657 if (POINTER_TYPE_P (type))
4658 t = build2 (MIN_EXPR, type, t,
4659 build2 (POINTER_PLUS_EXPR, type, ub, size_int (1)));
4660 else
4661 t = build2 (MIN_EXPR, type, t,
4662 build2 (PLUS_EXPR, type, ub, build_one_cst (type)));
4663 break;
4664 case LT_EXPR:
4665 t = build2 (MIN_EXPR, type, t, ub);
4666 break;
4667 case GE_EXPR:
4668 if (POINTER_TYPE_P (type))
4669 t = build2 (MAX_EXPR, type, t,
4670 build2 (POINTER_PLUS_EXPR, type, ub, size_int (-1)));
4671 else
4672 t = build2 (MAX_EXPR, type, t,
4673 build2 (PLUS_EXPR, type, ub,
4674 build_minus_one_cst (type)));
4675 break;
4676 case GT_EXPR:
4677 t = build2 (MAX_EXPR, type, t, ub);
4678 break;
4679 default:
4680 gcc_unreachable ();
4681 }
4682 tree end = create_tmp_var_raw (type);
4683 DECL_CONTEXT (end) = current_function_decl;
4684 end = build4 (TARGET_EXPR, type, end, t, NULL_TREE, NULL_TREE);
4685 TREE_SIDE_EFFECTS (end) = 1;
4686 append_to_statement_list (end, &pre_body);
4687 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
4688 tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
4689 t = build1 (GOTO_EXPR, void_type_node, lab2);
4690 append_to_statement_list (t, &pre_body);
4691 t = build1 (LABEL_EXPR, void_type_node, lab1);
4692 append_to_statement_list (t, &pre_body);
4693 tree this_post_body = NULL_TREE;
4694 if (POINTER_TYPE_P (type))
4695 t = build2 (POINTER_PLUS_EXPR, type, decl,
4696 fold_convert (sizetype, step));
4697 else
4698 t = build2 (PLUS_EXPR, type, decl, step);
4699 t = build2 (MODIFY_EXPR, type, decl, t);
4700 append_to_statement_list (t, &this_post_body);
4701 t = build1 (LABEL_EXPR, void_type_node, lab2);
4702 append_to_statement_list (t, &this_post_body);
4703 t = build2 ((TREE_CODE (cond) == LT_EXPR || TREE_CODE (cond) == LE_EXPR)
4704 ? LT_EXPR : GT_EXPR, boolean_type_node, decl, end);
4705 if (orig_sizes == NULL_TREE)
4706 {
4707 gcc_assert (i == 0);
4708 t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
4709 build_int_cst (integer_type_node,
4710 annot_expr_unroll_kind),
4711 build_int_cst (integer_type_node, size));
4712 }
4713 t = build3 (COND_EXPR, void_type_node, t,
4714 build1 (GOTO_EXPR, void_type_node, lab1), NULL_TREE);
4715 append_to_statement_list (t, &this_post_body);
4716 append_to_statement_list (post_body, &this_post_body);
4717 post_body = this_post_body;
4718 }
4719 if (pre_body || post_body)
4720 {
4721 append_to_statement_list (OMP_FOR_BODY (for_stmt), &pre_body);
4722 append_to_statement_list (post_body, &pre_body);
4723 OMP_FOR_BODY (for_stmt) = pre_body;
4724 }
4725 }
4726
4727 /* Callback for walk_tree to find nested loop transforming construct. */
4728
4729 static tree
4730 find_nested_loop_xform (tree *tp, int *walk_subtrees, void *data)
4731 {
4732 tree **pdata = (tree **) data;
4733 *walk_subtrees = 0;
4734 switch (TREE_CODE (*tp))
4735 {
4736 case OMP_TILE:
4737 case OMP_UNROLL:
4738 pdata[1] = tp;
4739 return *tp;
4740 case BIND_EXPR:
4741 if (BIND_EXPR_VARS (*tp)
4742 || (BIND_EXPR_BLOCK (*tp)
4743 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
4744 pdata[0] = tp;
4745 *walk_subtrees = 1;
4746 break;
4747 case STATEMENT_LIST:
4748 if (!tsi_one_before_end_p (tsi_start (*tp)))
4749 pdata[0] = tp;
4750 *walk_subtrees = 1;
4751 break;
4752 case TRY_FINALLY_EXPR:
4753 case CLEANUP_POINT_EXPR:
4754 pdata[0] = tp;
4755 *walk_subtrees = 1;
4756 break;
4757 default:
4758 break;
4759 }
4760 return NULL;
4761 }
4762
4763 /* Main entry point for performing OpenMP loop transformations. */
4764
4765 void
4766 omp_maybe_apply_loop_xforms (tree *expr_p, tree for_clauses)
4767 {
4768 tree for_stmt = *expr_p;
4769
4770 switch (TREE_CODE (for_stmt))
4771 {
4772 case OMP_TILE:
4773 case OMP_UNROLL:
4774 if (OMP_LOOPXFORM_LOWERED (for_stmt))
4775 return;
4776 break;
4777 default:
4778 break;
4779 }
4780
4781 tree *inner_expr_p = expr_p;
4782 tree inner_for_stmt = for_stmt;
4783 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
4784 {
4785 /* If some loop nest needs one or more loops in canonical form
4786 from nested loop transforming constructs, first perform the
4787 loop transformation on the nested construct and then move over
4788 the corresponding loops in canonical form from the inner construct
4789 to the outer one. */
4790 if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
4791 {
4792 if (inner_for_stmt == for_stmt
4793 && omp_find_clause (for_clauses ? for_clauses
4794 : OMP_FOR_CLAUSES (for_stmt),
4795 OMP_CLAUSE_ORDERED))
4796 {
4797 error_at (EXPR_LOCATION (for_stmt),
4798 "%<ordered%> clause used with generated loops");
4799 *expr_p = void_node;
4800 return;
4801 }
4802 tree *data[2] = { NULL, NULL };
4803 walk_tree (&OMP_FOR_BODY (inner_for_stmt),
4804 find_nested_loop_xform, &data, NULL);
4805 gcc_assert (data[1]);
4806 if (data[0])
4807 {
4808 /* If there is a BIND_EXPR declaring some vars, or statement
4809 list with more than one stmt etc., move the intervening
4810 code around the outermost loop. */
4811 tree t = *inner_expr_p;
4812 *inner_expr_p = OMP_FOR_BODY (inner_for_stmt);
4813 OMP_FOR_BODY (inner_for_stmt) = *data[1];
4814 *data[1] = t;
4815 inner_expr_p = data[1];
4816 data[1] = &OMP_FOR_BODY (inner_for_stmt);
4817 }
4818 inner_for_stmt = *data[1];
4819
4820 omp_maybe_apply_loop_xforms (data[1], NULL_TREE);
4821 if (*data[1] != inner_for_stmt)
4822 {
4823 tree *data2[2] = { NULL, NULL };
4824 walk_tree (data[1], find_nested_loop_xform, &data2, NULL);
4825 gcc_assert (data2[1]
4826 && *data2[1] == inner_for_stmt
4827 && data2[0]);
4828 tree t = *inner_expr_p;
4829 *inner_expr_p = *data[1];
4830 *data[1] = *data2[1];
4831 *data2[1] = t;
4832 inner_expr_p = data2[1];
4833 }
4834 tree clauses = OMP_FOR_CLAUSES (inner_for_stmt);
4835 gcc_checking_assert (TREE_CODE (inner_for_stmt) != OMP_UNROLL
4836 || omp_find_clause (clauses,
4837 OMP_CLAUSE_PARTIAL));
4838 append_to_statement_list (OMP_FOR_PRE_BODY (inner_for_stmt),
4839 &OMP_FOR_PRE_BODY (for_stmt));
4840 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
4841 if (OMP_FOR_ORIG_DECLS (for_stmt) == NULL_TREE
4842 && OMP_FOR_ORIG_DECLS (inner_for_stmt) != NULL_TREE)
4843 {
4844 OMP_FOR_ORIG_DECLS (for_stmt)
4845 = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)));
4846 for (int j = 0; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
4847 j++)
4848 {
4849 if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j) == NULL_TREE)
4850 continue;
4851 gcc_assert (TREE_CODE (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt),
4852 j)) == MODIFY_EXPR);
4853 TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), j)
4854 = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j),
4855 0);
4856 }
4857 }
4858 for (int j = 0; j < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt));
4859 ++j)
4860 {
4861 if (i + j == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
4862 break;
4863 if (OMP_FOR_ORIG_DECLS (for_stmt))
4864 {
4865 if (OMP_FOR_ORIG_DECLS (inner_for_stmt))
4866 {
4867 TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i + j)
4868 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
4869 j);
4870 TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), j)
4871 = NULL_TREE;
4872 }
4873 else
4874 {
4875 tree t = TREE_VEC_ELT (OMP_FOR_INIT (inner_for_stmt), j);
4876 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
4877 TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i + j)
4878 = TREE_OPERAND (t, 0);
4879 }
4880 }
4881 TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i + j)
4882 = TREE_VEC_ELT (OMP_FOR_INIT (inner_for_stmt), j);
4883 TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i + j)
4884 = TREE_VEC_ELT (OMP_FOR_COND (inner_for_stmt), j);
4885 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i + j)
4886 = TREE_VEC_ELT (OMP_FOR_INCR (inner_for_stmt), j);
4887 TREE_VEC_ELT (OMP_FOR_INIT (inner_for_stmt), j) = NULL_TREE;
4888 TREE_VEC_ELT (OMP_FOR_COND (inner_for_stmt), j) = NULL_TREE;
4889 TREE_VEC_ELT (OMP_FOR_INCR (inner_for_stmt), j) = NULL_TREE;
4890 }
4891 }
4892 }
4893
4894 switch (TREE_CODE (for_stmt))
4895 {
4896 case OMP_TILE:
4897 tree sizes;
4898 sizes = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_SIZES);
4899 omp_apply_tile (for_stmt, OMP_CLAUSE_SIZES_LIST (sizes), 0);
4900 OMP_LOOPXFORM_LOWERED (for_stmt) = 1;
4901 break;
4902 case OMP_UNROLL:
4903 tree partial;
4904 partial = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
4905 OMP_CLAUSE_PARTIAL);
4906 if (partial)
4907 omp_apply_tile (for_stmt, NULL_TREE,
4908 OMP_CLAUSE_PARTIAL_EXPR (partial)
4909 ? tree_to_shwi (OMP_CLAUSE_PARTIAL_EXPR (partial))
4910 : 8);
4911 else if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
4912 {
4913 tree iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
4914 if (TREE_CODE (iters) != INTEGER_CST)
4915 error_at (EXPR_LOCATION (for_stmt),
4916 "non-constant iteration count of %<unroll full%> loop");
4917 }
4918 OMP_LOOPXFORM_LOWERED (for_stmt) = 1;
4919 break;
4920 default:
4921 break;
4922 }
4923 }
4924