Debian Codegraph @

gcc/c-family/c-omp.cc

raw
1 /* This file contains routines to construct OpenACC and OpenMP constructs,
2 called from parsing in the C and C++ front ends.
3
4 Copyright (C) 2005-2025 Free Software Foundation, Inc.
5 Contributed by Richard Henderson <[email protected]>,
6 Diego Novillo <[email protected]>.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "options.h"
28 #include "c-common.h"
29 #include "gimple-expr.h"
30 #include "c-pragma.h"
31 #include "stringpool.h"
32 #include "omp-general.h"
33 #include "gomp-constants.h"
34 #include "memmodel.h"
35 #include "attribs.h"
36 #include "gimplify.h"
37 #include "langhooks.h"
38 #include "bitmap.h"
39 #include "tree-iterator.h"
40
41
42 /* Complete a #pragma oacc wait construct. LOC is the location of
43 the #pragma. */
44
45 tree
46 c_finish_oacc_wait (location_t loc, tree parms, tree clauses)
47 {
48 const int nparms = list_length (parms);
49 tree stmt, t;
50 vec<tree, va_gc> *args;
51
52 vec_alloc (args, nparms + 2);
53 stmt = builtin_decl_explicit (BUILT_IN_GOACC_WAIT);
54
55 if (omp_find_clause (clauses, OMP_CLAUSE_ASYNC))
56 t = OMP_CLAUSE_ASYNC_EXPR (clauses);
57 else
58 t = build_int_cst (integer_type_node, GOMP_ASYNC_SYNC);
59
60 args->quick_push (t);
61 args->quick_push (build_int_cst (integer_type_node, nparms));
62
63 for (t = parms; t; t = TREE_CHAIN (t))
64 {
65 if (TREE_CODE (OMP_CLAUSE_WAIT_EXPR (t)) == INTEGER_CST)
66 args->quick_push (build_int_cst (integer_type_node,
67 TREE_INT_CST_LOW (OMP_CLAUSE_WAIT_EXPR (t))));
68 else
69 args->quick_push (OMP_CLAUSE_WAIT_EXPR (t));
70 }
71
72 stmt = build_call_expr_loc_vec (loc, stmt, args);
73
74 vec_free (args);
75
76 return stmt;
77 }
78
79 /* Complete a #pragma omp master construct. STMT is the structured-block
80 that follows the pragma. LOC is the location of the #pragma. */
81
82 tree
83 c_finish_omp_master (location_t loc, tree stmt)
84 {
85 tree t = add_stmt (build1 (OMP_MASTER, void_type_node, stmt));
86 SET_EXPR_LOCATION (t, loc);
87 return t;
88 }
89
90 /* Complete a #pragma omp masked construct. BODY is the structured-block
91 that follows the pragma. LOC is the location of the #pragma. */
92
93 tree
94 c_finish_omp_masked (location_t loc, tree body, tree clauses)
95 {
96 tree stmt = make_node (OMP_MASKED);
97 TREE_TYPE (stmt) = void_type_node;
98 OMP_MASKED_BODY (stmt) = body;
99 OMP_MASKED_CLAUSES (stmt) = clauses;
100 SET_EXPR_LOCATION (stmt, loc);
101 return add_stmt (stmt);
102 }
103
104 /* Complete a #pragma omp taskgroup construct. BODY is the structured-block
105 that follows the pragma. LOC is the location of the #pragma. */
106
107 tree
108 c_finish_omp_taskgroup (location_t loc, tree body, tree clauses)
109 {
110 tree stmt = make_node (OMP_TASKGROUP);
111 TREE_TYPE (stmt) = void_type_node;
112 OMP_TASKGROUP_BODY (stmt) = body;
113 OMP_TASKGROUP_CLAUSES (stmt) = clauses;
114 SET_EXPR_LOCATION (stmt, loc);
115 return add_stmt (stmt);
116 }
117
118 /* Complete a #pragma omp critical construct. BODY is the structured-block
119 that follows the pragma, NAME is the identifier in the pragma, or null
120 if it was omitted. LOC is the location of the #pragma. */
121
122 tree
123 c_finish_omp_critical (location_t loc, tree body, tree name, tree clauses)
124 {
125 gcc_assert (!clauses || OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT);
126 if (name == NULL_TREE
127 && clauses != NULL_TREE
128 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (clauses)))
129 {
130 error_at (OMP_CLAUSE_LOCATION (clauses),
131 "%<#pragma omp critical%> with %<hint%> clause requires "
132 "a name, except when %<omp_sync_hint_none%> is used");
133 return error_mark_node;
134 }
135
136 tree stmt = make_node (OMP_CRITICAL);
137 TREE_TYPE (stmt) = void_type_node;
138 OMP_CRITICAL_BODY (stmt) = body;
139 OMP_CRITICAL_NAME (stmt) = name;
140 OMP_CRITICAL_CLAUSES (stmt) = clauses;
141 SET_EXPR_LOCATION (stmt, loc);
142 return add_stmt (stmt);
143 }
144
145 /* Complete a #pragma omp ordered construct. STMT is the structured-block
146 that follows the pragma. LOC is the location of the #pragma. */
147
148 tree
149 c_finish_omp_ordered (location_t loc, tree clauses, tree stmt)
150 {
151 tree t = make_node (OMP_ORDERED);
152 TREE_TYPE (t) = void_type_node;
153 OMP_ORDERED_BODY (t) = stmt;
154 if (!flag_openmp /* flag_openmp_simd */
155 && (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_SIMD
156 || OMP_CLAUSE_CHAIN (clauses)))
157 clauses = build_omp_clause (loc, OMP_CLAUSE_SIMD);
158 OMP_ORDERED_CLAUSES (t) = clauses;
159 SET_EXPR_LOCATION (t, loc);
160 return add_stmt (t);
161 }
162
163
164 /* Complete a #pragma omp barrier construct. LOC is the location of
165 the #pragma. */
166
167 void
168 c_finish_omp_barrier (location_t loc)
169 {
170 tree x;
171
172 x = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
173 x = build_call_expr_loc (loc, x, 0);
174 add_stmt (x);
175 }
176
177
178 /* Complete a #pragma omp taskwait construct. LOC is the location of the
179 pragma. */
180
181 void
182 c_finish_omp_taskwait (location_t loc)
183 {
184 tree x;
185
186 x = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
187 x = build_call_expr_loc (loc, x, 0);
188 add_stmt (x);
189 }
190
191
192 /* Complete a #pragma omp taskyield construct. LOC is the location of the
193 pragma. */
194
195 void
196 c_finish_omp_taskyield (location_t loc)
197 {
198 tree x;
199
200 x = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
201 x = build_call_expr_loc (loc, x, 0);
202 add_stmt (x);
203 }
204
205
206 /* Complete a #pragma omp atomic construct. For CODE OMP_ATOMIC
207 the expression to be implemented atomically is LHS opcode= RHS.
208 For OMP_ATOMIC_READ V = LHS, for OMP_ATOMIC_CAPTURE_{NEW,OLD} LHS
209 opcode= RHS with the new or old content of LHS returned.
210 LOC is the location of the atomic statement. The value returned
211 is either error_mark_node (if the construct was erroneous) or an
212 OMP_ATOMIC* node which should be added to the current statement
213 tree with add_stmt. If TEST is set, avoid calling save_expr
214 or create_tmp_var*. */
215
216 tree
217 c_finish_omp_atomic (location_t loc, enum tree_code code,
218 enum tree_code opcode, tree lhs, tree rhs,
219 tree v, tree lhs1, tree rhs1, tree r, bool swapped,
220 enum omp_memory_order memory_order, bool weak,
221 bool test)
222 {
223 tree x, type, addr, pre = NULL_TREE, rtmp = NULL_TREE, vtmp = NULL_TREE;
224 HOST_WIDE_INT bitpos = 0, bitsize = 0;
225 enum tree_code orig_opcode = opcode;
226
227 if (lhs == error_mark_node || rhs == error_mark_node
228 || v == error_mark_node || lhs1 == error_mark_node
229 || rhs1 == error_mark_node || r == error_mark_node)
230 return error_mark_node;
231
232 /* ??? According to one reading of the OpenMP spec, complex type are
233 supported, but there are no atomic stores for any architecture.
234 But at least icc 9.0 doesn't support complex types here either.
235 And lets not even talk about vector types... */
236 type = TREE_TYPE (lhs);
237 if (!INTEGRAL_TYPE_P (type)
238 && !POINTER_TYPE_P (type)
239 && !SCALAR_FLOAT_TYPE_P (type))
240 {
241 error_at (loc, "invalid expression type for %<#pragma omp atomic%>");
242 return error_mark_node;
243 }
244 if (TYPE_ATOMIC (type))
245 {
246 error_at (loc, "%<_Atomic%> expression in %<#pragma omp atomic%>");
247 return error_mark_node;
248 }
249 if (r && r != void_list_node && !INTEGRAL_TYPE_P (TREE_TYPE (r)))
250 {
251 error_at (loc, "%<#pragma omp atomic compare capture%> with non-integral "
252 "comparison result");
253 return error_mark_node;
254 }
255
256 if (opcode == RDIV_EXPR)
257 opcode = TRUNC_DIV_EXPR;
258
259 /* ??? Validate that rhs does not overlap lhs. */
260 tree blhs = NULL;
261 if (TREE_CODE (lhs) == COMPONENT_REF
262 && TREE_CODE (TREE_OPERAND (lhs, 1)) == FIELD_DECL
263 && DECL_C_BIT_FIELD (TREE_OPERAND (lhs, 1))
264 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (lhs, 1)))
265 {
266 tree field = TREE_OPERAND (lhs, 1);
267 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
268 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
269 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
270 bitpos = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
271 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
272 else
273 bitpos = 0;
274 bitpos += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
275 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
276 gcc_assert (tree_fits_shwi_p (DECL_SIZE (field)));
277 bitsize = tree_to_shwi (DECL_SIZE (field));
278 blhs = lhs;
279 type = TREE_TYPE (repr);
280 lhs = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (lhs, 0),
281 repr, TREE_OPERAND (lhs, 2));
282 }
283
284 /* Take and save the address of the lhs. From then on we'll reference it
285 via indirection. */
286 addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
287 if (addr == error_mark_node)
288 return error_mark_node;
289 if (!test)
290 addr = save_expr (addr);
291 if (!test
292 && TREE_CODE (addr) != SAVE_EXPR
293 && (TREE_CODE (addr) != ADDR_EXPR
294 || !VAR_P (TREE_OPERAND (addr, 0))))
295 {
296 /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
297 it even after unsharing function body. */
298 tree var = create_tmp_var_raw (TREE_TYPE (addr));
299 DECL_CONTEXT (var) = current_function_decl;
300 addr = build4 (TARGET_EXPR, TREE_TYPE (addr), var, addr, NULL, NULL);
301 }
302 tree orig_lhs = lhs;
303 lhs = build_indirect_ref (loc, addr, RO_NULL);
304 tree new_lhs = lhs;
305
306 if (code == OMP_ATOMIC_READ)
307 {
308 x = build1 (OMP_ATOMIC_READ, type, addr);
309 SET_EXPR_LOCATION (x, loc);
310 OMP_ATOMIC_MEMORY_ORDER (x) = memory_order;
311 gcc_assert (!weak);
312 if (blhs)
313 x = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), x,
314 bitsize_int (bitsize), bitsize_int (bitpos));
315 return build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
316 loc, x, NULL_TREE);
317 }
318
319 /* There are lots of warnings, errors, and conversions that need to happen
320 in the course of interpreting a statement. Use the normal mechanisms
321 to do this, and then take it apart again. */
322 if (blhs)
323 {
324 lhs = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), lhs,
325 bitsize_int (bitsize), bitsize_int (bitpos));
326 if (opcode == COND_EXPR)
327 {
328 bool save = in_late_binary_op;
329 in_late_binary_op = true;
330 std::swap (rhs, rhs1);
331 rhs1 = build_binary_op (loc, EQ_EXPR, lhs, rhs1, true);
332 in_late_binary_op = save;
333 }
334 else if (swapped)
335 rhs = build_binary_op (loc, opcode, rhs, lhs, true);
336 else if (opcode != NOP_EXPR)
337 rhs = build_binary_op (loc, opcode, lhs, rhs, true);
338 opcode = NOP_EXPR;
339 }
340 else if (opcode == COND_EXPR)
341 {
342 bool save = in_late_binary_op;
343 in_late_binary_op = true;
344 std::swap (rhs, rhs1);
345 rhs1 = build_binary_op (loc, EQ_EXPR, lhs, rhs1, true);
346 in_late_binary_op = save;
347 opcode = NOP_EXPR;
348 }
349 else if (swapped)
350 {
351 rhs = build_binary_op (loc, opcode, rhs, lhs, true);
352 opcode = NOP_EXPR;
353 }
354 bool save = in_late_binary_op;
355 in_late_binary_op = true;
356 if ((opcode == MIN_EXPR || opcode == MAX_EXPR)
357 && build_binary_op (loc, LT_EXPR, blhs ? blhs : lhs, rhs,
358 true) == error_mark_node)
359 x = error_mark_node;
360 else
361 x = build_modify_expr (loc, blhs ? blhs : lhs, NULL_TREE, opcode,
362 loc, rhs, NULL_TREE);
363 in_late_binary_op = save;
364 if (x == error_mark_node)
365 return error_mark_node;
366 if (TREE_CODE (x) == COMPOUND_EXPR)
367 {
368 pre = TREE_OPERAND (x, 0);
369 gcc_assert (TREE_CODE (pre) == SAVE_EXPR || tree_invariant_p (pre));
370 x = TREE_OPERAND (x, 1);
371 }
372 gcc_assert (TREE_CODE (x) == MODIFY_EXPR);
373 rhs = TREE_OPERAND (x, 1);
374
375 if (blhs)
376 rhs = build3_loc (loc, BIT_INSERT_EXPR, type, new_lhs,
377 rhs, bitsize_int (bitpos));
378 if (orig_opcode == COND_EXPR)
379 {
380 if (error_operand_p (rhs1))
381 return error_mark_node;
382 gcc_assert (TREE_CODE (rhs1) == EQ_EXPR);
383 tree cmptype = TREE_TYPE (TREE_OPERAND (rhs1, 0));
384 if (SCALAR_FLOAT_TYPE_P (cmptype) && !test)
385 {
386 bool clear_padding = false;
387 HOST_WIDE_INT non_padding_start = 0;
388 HOST_WIDE_INT non_padding_end = 0;
389 if (BITS_PER_UNIT == 8
390 && CHAR_BIT == 8
391 && clear_padding_type_may_have_padding_p (cmptype))
392 {
393 HOST_WIDE_INT sz = int_size_in_bytes (cmptype), i;
394 gcc_assert (sz > 0);
395 unsigned char *buf = XALLOCAVEC (unsigned char, sz);
396 memset (buf, ~0, sz);
397 clear_type_padding_in_mask (cmptype, buf);
398 for (i = 0; i < sz; i++)
399 if (buf[i] != (unsigned char) ~0)
400 {
401 clear_padding = true;
402 break;
403 }
404 if (clear_padding && buf[i] == 0)
405 {
406 /* Try to optimize. In the common case where
407 non-padding bits are all continuous and start
408 and end at a byte boundary, we can just adjust
409 the memcmp call arguments and don't need to
410 emit __builtin_clear_padding calls. */
411 if (i == 0)
412 {
413 for (i = 0; i < sz; i++)
414 if (buf[i] != 0)
415 break;
416 if (i < sz && buf[i] == (unsigned char) ~0)
417 {
418 non_padding_start = i;
419 for (; i < sz; i++)
420 if (buf[i] != (unsigned char) ~0)
421 break;
422 }
423 else
424 i = 0;
425 }
426 if (i != 0)
427 {
428 non_padding_end = i;
429 for (; i < sz; i++)
430 if (buf[i] != 0)
431 {
432 non_padding_start = 0;
433 non_padding_end = 0;
434 break;
435 }
436 }
437 }
438 }
439 tree inttype = NULL_TREE;
440 if (!clear_padding && tree_fits_uhwi_p (TYPE_SIZE (cmptype)))
441 {
442 HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (cmptype));
443 inttype = c_common_type_for_size (prec, 1);
444 if (inttype
445 && (!tree_int_cst_equal (TYPE_SIZE (cmptype),
446 TYPE_SIZE (inttype))
447 || TYPE_PRECISION (inttype) != prec))
448 inttype = NULL_TREE;
449 }
450 if (inttype)
451 {
452 TREE_OPERAND (rhs1, 0)
453 = build1_loc (loc, VIEW_CONVERT_EXPR, inttype,
454 TREE_OPERAND (rhs1, 0));
455 TREE_OPERAND (rhs1, 1)
456 = build1_loc (loc, VIEW_CONVERT_EXPR, inttype,
457 TREE_OPERAND (rhs1, 1));
458 }
459 else
460 {
461 tree pcmptype = build_pointer_type (cmptype);
462 tree tmp1 = create_tmp_var_raw (cmptype);
463 TREE_ADDRESSABLE (tmp1) = 1;
464 DECL_CONTEXT (tmp1) = current_function_decl;
465 tmp1 = build4 (TARGET_EXPR, cmptype, tmp1,
466 TREE_OPERAND (rhs1, 0), NULL, NULL);
467 tmp1 = build1 (ADDR_EXPR, pcmptype, tmp1);
468 tree tmp2 = create_tmp_var_raw (cmptype);
469 TREE_ADDRESSABLE (tmp2) = 1;
470 DECL_CONTEXT (tmp2) = current_function_decl;
471 tmp2 = build4 (TARGET_EXPR, cmptype, tmp2,
472 TREE_OPERAND (rhs1, 1), NULL, NULL);
473 tmp2 = build1 (ADDR_EXPR, pcmptype, tmp2);
474 if (non_padding_start)
475 {
476 tmp1 = build2 (POINTER_PLUS_EXPR, pcmptype, tmp1,
477 size_int (non_padding_start));
478 tmp2 = build2 (POINTER_PLUS_EXPR, pcmptype, tmp2,
479 size_int (non_padding_start));
480 }
481 tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
482 rhs1 = build_call_expr_loc (loc, fndecl, 3, tmp1, tmp2,
483 non_padding_end
484 ? size_int (non_padding_end
485 - non_padding_start)
486 : TYPE_SIZE_UNIT (cmptype));
487 rhs1 = build2 (EQ_EXPR, boolean_type_node, rhs1,
488 integer_zero_node);
489 if (clear_padding && non_padding_end == 0)
490 {
491 fndecl = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
492 tree cp1 = build_call_expr_loc (loc, fndecl, 1, tmp1);
493 tree cp2 = build_call_expr_loc (loc, fndecl, 1, tmp2);
494 rhs1 = omit_two_operands_loc (loc, boolean_type_node,
495 rhs1, cp2, cp1);
496 }
497 }
498 }
499 if (r && test)
500 rtmp = rhs1;
501 else if (r)
502 {
503 tree var = create_tmp_var_raw (boolean_type_node);
504 DECL_CONTEXT (var) = current_function_decl;
505 rtmp = build4 (TARGET_EXPR, boolean_type_node, var,
506 boolean_false_node, NULL, NULL);
507 save = in_late_binary_op;
508 in_late_binary_op = true;
509 x = build_modify_expr (loc, var, NULL_TREE, NOP_EXPR,
510 loc, rhs1, NULL_TREE);
511 in_late_binary_op = save;
512 if (x == error_mark_node)
513 return error_mark_node;
514 gcc_assert (TREE_CODE (x) == MODIFY_EXPR
515 && TREE_OPERAND (x, 0) == var);
516 TREE_OPERAND (x, 0) = rtmp;
517 rhs1 = omit_one_operand_loc (loc, boolean_type_node, x, rtmp);
518 }
519 rhs = build3_loc (loc, COND_EXPR, type, rhs1, rhs, new_lhs);
520 rhs1 = NULL_TREE;
521 }
522
523 /* Punt the actual generation of atomic operations to common code. */
524 if (code == OMP_ATOMIC)
525 type = void_type_node;
526 x = build2 (code, type, addr, rhs);
527 SET_EXPR_LOCATION (x, loc);
528 OMP_ATOMIC_MEMORY_ORDER (x) = memory_order;
529 OMP_ATOMIC_WEAK (x) = weak;
530
531 /* Generally it is hard to prove lhs1 and lhs are the same memory
532 location, just diagnose different variables. */
533 if (rhs1
534 && VAR_P (rhs1)
535 && VAR_P (orig_lhs)
536 && rhs1 != orig_lhs
537 && !test)
538 {
539 if (code == OMP_ATOMIC)
540 error_at (loc, "%<#pragma omp atomic update%> uses two different "
541 "variables for memory");
542 else
543 error_at (loc, "%<#pragma omp atomic capture%> uses two different "
544 "variables for memory");
545 return error_mark_node;
546 }
547
548 if (lhs1
549 && lhs1 != orig_lhs
550 && TREE_CODE (lhs1) == COMPONENT_REF
551 && TREE_CODE (TREE_OPERAND (lhs1, 1)) == FIELD_DECL
552 && DECL_C_BIT_FIELD (TREE_OPERAND (lhs1, 1))
553 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (lhs1, 1)))
554 {
555 tree field = TREE_OPERAND (lhs1, 1);
556 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
557 lhs1 = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (lhs1, 0),
558 repr, TREE_OPERAND (lhs1, 2));
559 }
560 if (rhs1
561 && rhs1 != orig_lhs
562 && TREE_CODE (rhs1) == COMPONENT_REF
563 && TREE_CODE (TREE_OPERAND (rhs1, 1)) == FIELD_DECL
564 && DECL_C_BIT_FIELD (TREE_OPERAND (rhs1, 1))
565 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (rhs1, 1)))
566 {
567 tree field = TREE_OPERAND (rhs1, 1);
568 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
569 rhs1 = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (rhs1, 0),
570 repr, TREE_OPERAND (rhs1, 2));
571 }
572
573 if (code != OMP_ATOMIC)
574 {
575 /* Generally it is hard to prove lhs1 and lhs are the same memory
576 location, just diagnose different variables. */
577 if (lhs1 && VAR_P (lhs1) && VAR_P (orig_lhs))
578 {
579 if (lhs1 != orig_lhs && !test)
580 {
581 error_at (loc, "%<#pragma omp atomic capture%> uses two "
582 "different variables for memory");
583 return error_mark_node;
584 }
585 }
586 if (blhs)
587 x = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), x,
588 bitsize_int (bitsize), bitsize_int (bitpos));
589 if (r && !test)
590 {
591 vtmp = create_tmp_var_raw (TREE_TYPE (x));
592 DECL_CONTEXT (vtmp) = current_function_decl;
593 }
594 else
595 vtmp = v;
596 x = build_modify_expr (loc, vtmp, NULL_TREE, NOP_EXPR,
597 loc, x, NULL_TREE);
598 if (x == error_mark_node)
599 return error_mark_node;
600 type = TREE_TYPE (x);
601 if (r && !test)
602 {
603 vtmp = build4 (TARGET_EXPR, TREE_TYPE (vtmp), vtmp,
604 build_zero_cst (TREE_TYPE (vtmp)), NULL, NULL);
605 gcc_assert (TREE_CODE (x) == MODIFY_EXPR
606 && TREE_OPERAND (x, 0) == TARGET_EXPR_SLOT (vtmp));
607 TREE_OPERAND (x, 0) = vtmp;
608 }
609 if (rhs1 && rhs1 != orig_lhs)
610 {
611 tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, false);
612 if (rhs1addr == error_mark_node)
613 return error_mark_node;
614 x = omit_one_operand_loc (loc, type, x, rhs1addr);
615 }
616 if (lhs1 && lhs1 != orig_lhs)
617 {
618 tree lhs1addr = build_unary_op (loc, ADDR_EXPR, lhs1, false);
619 if (lhs1addr == error_mark_node)
620 return error_mark_node;
621 if (code == OMP_ATOMIC_CAPTURE_OLD)
622 x = omit_one_operand_loc (loc, type, x, lhs1addr);
623 else
624 {
625 if (!test)
626 x = save_expr (x);
627 x = omit_two_operands_loc (loc, type, x, x, lhs1addr);
628 }
629 }
630 }
631 else if (rhs1 && rhs1 != orig_lhs)
632 {
633 tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, false);
634 if (rhs1addr == error_mark_node)
635 return error_mark_node;
636 x = omit_one_operand_loc (loc, type, x, rhs1addr);
637 }
638
639 if (pre)
640 x = omit_one_operand_loc (loc, type, x, pre);
641 if (r && r != void_list_node)
642 {
643 in_late_binary_op = true;
644 tree x2 = build_modify_expr (loc, r, NULL_TREE, NOP_EXPR,
645 loc, rtmp, NULL_TREE);
646 in_late_binary_op = save;
647 if (x2 == error_mark_node)
648 return error_mark_node;
649 x = omit_one_operand_loc (loc, TREE_TYPE (x2), x2, x);
650 }
651 if (v && vtmp != v)
652 {
653 in_late_binary_op = true;
654 tree x2 = build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
655 loc, vtmp, NULL_TREE);
656 in_late_binary_op = save;
657 if (x2 == error_mark_node)
658 return error_mark_node;
659 x2 = build3_loc (loc, COND_EXPR, void_type_node, rtmp,
660 void_node, x2);
661 x = omit_one_operand_loc (loc, TREE_TYPE (x2), x2, x);
662 }
663 return x;
664 }
665
666
667 /* Return true if TYPE is the implementation's omp_interop_t. */
668
669 bool
670 c_omp_interop_t_p (tree type)
671 {
672 if (type == error_mark_node)
673 return false;
674 type = TYPE_MAIN_VARIANT (type);
675 return (TREE_CODE (type) == ENUMERAL_TYPE
676 && TYPE_NAME (type)
677 && ((TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
678 ? DECL_NAME (TYPE_NAME (type)) : TYPE_NAME (type))
679 == get_identifier ("omp_interop_t"))
680 && TYPE_FILE_SCOPE_P (type)
681 && COMPLETE_TYPE_P (type)
682 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
683 && !compare_tree_int (TYPE_SIZE (type),
684 tree_to_uhwi (TYPE_SIZE (ptr_type_node))));
685 }
686
687 /* Return true if TYPE is the implementation's omp_depend_t. */
688
689 bool
690 c_omp_depend_t_p (tree type)
691 {
692 type = TYPE_MAIN_VARIANT (type);
693 return (TREE_CODE (type) == RECORD_TYPE
694 && TYPE_NAME (type)
695 && ((TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
696 ? DECL_NAME (TYPE_NAME (type)) : TYPE_NAME (type))
697 == get_identifier ("omp_depend_t"))
698 && TYPE_FILE_SCOPE_P (type)
699 && COMPLETE_TYPE_P (type)
700 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
701 && !compare_tree_int (TYPE_SIZE (type),
702 2 * tree_to_uhwi (TYPE_SIZE (ptr_type_node))));
703 }
704
705
706 /* Complete a #pragma omp depobj construct. LOC is the location of the
707 #pragma. */
708
709 void
710 c_finish_omp_depobj (location_t loc, tree depobj,
711 enum omp_clause_depend_kind kind, tree clause)
712 {
713 tree t = NULL_TREE;
714 if (!error_operand_p (depobj))
715 {
716 if (!c_omp_depend_t_p (TREE_TYPE (depobj)))
717 {
718 error_at (EXPR_LOC_OR_LOC (depobj, loc),
719 "type of %<depobj%> expression is not %<omp_depend_t%>");
720 depobj = error_mark_node;
721 }
722 else if (TYPE_READONLY (TREE_TYPE (depobj)))
723 {
724 error_at (EXPR_LOC_OR_LOC (depobj, loc),
725 "%<const%> qualified %<depobj%> expression");
726 depobj = error_mark_node;
727 }
728 }
729 else
730 depobj = error_mark_node;
731
732 if (clause == error_mark_node)
733 return;
734
735 if (clause)
736 {
737 gcc_assert (TREE_CODE (clause) == OMP_CLAUSE);
738 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_DOACROSS)
739 {
740 error_at (OMP_CLAUSE_LOCATION (clause),
741 "%<depend(%s)%> is only allowed in %<omp ordered%>",
742 OMP_CLAUSE_DOACROSS_KIND (clause)
743 == OMP_CLAUSE_DOACROSS_SOURCE
744 ? "source" : "sink");
745 return;
746 }
747 gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_DEPEND);
748 if (OMP_CLAUSE_CHAIN (clause))
749 error_at (OMP_CLAUSE_LOCATION (clause),
750 "more than one locator in %<depend%> clause on %<depobj%> "
751 "construct");
752 switch (OMP_CLAUSE_DEPEND_KIND (clause))
753 {
754 case OMP_CLAUSE_DEPEND_DEPOBJ:
755 error_at (OMP_CLAUSE_LOCATION (clause),
756 "%<depobj%> dependence type specified in %<depend%> "
757 "clause on %<depobj%> construct");
758 return;
759 case OMP_CLAUSE_DEPEND_IN:
760 case OMP_CLAUSE_DEPEND_OUT:
761 case OMP_CLAUSE_DEPEND_INOUT:
762 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
763 case OMP_CLAUSE_DEPEND_INOUTSET:
764 kind = OMP_CLAUSE_DEPEND_KIND (clause);
765 t = OMP_CLAUSE_DECL (clause);
766 gcc_assert (t);
767 if (TREE_CODE (t) == TREE_LIST
768 && TREE_PURPOSE (t)
769 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
770 {
771 error_at (OMP_CLAUSE_LOCATION (clause),
772 "%<iterator%> modifier may not be specified on "
773 "%<depobj%> construct");
774 return;
775 }
776 if (TREE_CODE (t) == COMPOUND_EXPR)
777 {
778 tree t1 = build_fold_addr_expr (TREE_OPERAND (t, 1));
779 t = build2 (COMPOUND_EXPR, TREE_TYPE (t1), TREE_OPERAND (t, 0),
780 t1);
781 }
782 else if (t != null_pointer_node)
783 t = build_fold_addr_expr (t);
784 break;
785 default:
786 gcc_unreachable ();
787 }
788 }
789 else
790 gcc_assert (kind != OMP_CLAUSE_DEPEND_INVALID);
791
792 if (depobj == error_mark_node)
793 return;
794
795 depobj = build_fold_addr_expr_loc (EXPR_LOC_OR_LOC (depobj, loc), depobj);
796 tree dtype
797 = build_pointer_type_for_mode (ptr_type_node, TYPE_MODE (ptr_type_node),
798 true);
799 depobj = fold_convert (dtype, depobj);
800 tree r;
801 if (clause)
802 {
803 depobj = save_expr (depobj);
804 r = build_indirect_ref (loc, depobj, RO_UNARY_STAR);
805 add_stmt (build2 (MODIFY_EXPR, void_type_node, r, t));
806 }
807 int k;
808 switch (kind)
809 {
810 case OMP_CLAUSE_DEPEND_IN:
811 k = GOMP_DEPEND_IN;
812 break;
813 case OMP_CLAUSE_DEPEND_OUT:
814 k = GOMP_DEPEND_OUT;
815 break;
816 case OMP_CLAUSE_DEPEND_INOUT:
817 k = GOMP_DEPEND_INOUT;
818 break;
819 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
820 k = GOMP_DEPEND_MUTEXINOUTSET;
821 break;
822 case OMP_CLAUSE_DEPEND_INOUTSET:
823 k = GOMP_DEPEND_INOUTSET;
824 break;
825 case OMP_CLAUSE_DEPEND_LAST:
826 k = -1;
827 break;
828 default:
829 gcc_unreachable ();
830 }
831 t = build_int_cst (ptr_type_node, k);
832 depobj = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (depobj), depobj,
833 TYPE_SIZE_UNIT (ptr_type_node));
834 r = build_indirect_ref (loc, depobj, RO_UNARY_STAR);
835 add_stmt (build2 (MODIFY_EXPR, void_type_node, r, t));
836 }
837
838
839 /* Complete a #pragma omp flush construct. We don't do anything with
840 the variable list that the syntax allows. LOC is the location of
841 the #pragma. */
842
843 void
844 c_finish_omp_flush (location_t loc, int mo)
845 {
846 tree x;
847
848 if (mo == MEMMODEL_LAST || mo == MEMMODEL_SEQ_CST)
849 {
850 x = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
851 x = build_call_expr_loc (loc, x, 0);
852 }
853 else
854 {
855 x = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
856 x = build_call_expr_loc (loc, x, 1,
857 build_int_cst (integer_type_node, mo));
858 }
859 add_stmt (x);
860 }
861
862
863 /* Check and canonicalize OMP_FOR increment expression.
864 Helper function for c_finish_omp_for. */
865
866 static tree
867 check_omp_for_incr_expr (location_t loc, tree exp, tree decl)
868 {
869 tree t;
870
871 if (!INTEGRAL_TYPE_P (TREE_TYPE (exp))
872 || TYPE_PRECISION (TREE_TYPE (exp)) < TYPE_PRECISION (TREE_TYPE (decl)))
873 return error_mark_node;
874
875 if (exp == decl)
876 return build_int_cst (TREE_TYPE (exp), 0);
877
878 switch (TREE_CODE (exp))
879 {
880 CASE_CONVERT:
881 t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
882 if (t != error_mark_node)
883 return fold_convert_loc (loc, TREE_TYPE (exp), t);
884 break;
885 case MINUS_EXPR:
886 t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
887 if (t != error_mark_node)
888 return fold_build2_loc (loc, MINUS_EXPR,
889 TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
890 break;
891 case PLUS_EXPR:
892 t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
893 if (t != error_mark_node)
894 return fold_build2_loc (loc, PLUS_EXPR,
895 TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
896 t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 1), decl);
897 if (t != error_mark_node)
898 return fold_build2_loc (loc, PLUS_EXPR,
899 TREE_TYPE (exp), TREE_OPERAND (exp, 0), t);
900 break;
901 case COMPOUND_EXPR:
902 {
903 /* cp_build_modify_expr forces preevaluation of the RHS to make
904 sure that it is evaluated before the lvalue-rvalue conversion
905 is applied to the LHS. Reconstruct the original expression. */
906 tree op0 = TREE_OPERAND (exp, 0);
907 if (TREE_CODE (op0) == TARGET_EXPR
908 && !VOID_TYPE_P (TREE_TYPE (op0)))
909 {
910 tree op1 = TREE_OPERAND (exp, 1);
911 tree temp = TARGET_EXPR_SLOT (op0);
912 if (BINARY_CLASS_P (op1)
913 && TREE_OPERAND (op1, 1) == temp)
914 {
915 op1 = copy_node (op1);
916 TREE_OPERAND (op1, 1) = TARGET_EXPR_INITIAL (op0);
917 return check_omp_for_incr_expr (loc, op1, decl);
918 }
919 }
920 break;
921 }
922 default:
923 break;
924 }
925
926 return error_mark_node;
927 }
928
929 /* If the OMP_FOR increment expression in INCR is of pointer type,
930 canonicalize it into an expression handled by gimplify_omp_for()
931 and return it. DECL is the iteration variable. */
932
933 static tree
934 c_omp_for_incr_canonicalize_ptr (location_t loc, tree decl, tree incr)
935 {
936 if (POINTER_TYPE_P (TREE_TYPE (decl))
937 && TREE_OPERAND (incr, 1))
938 {
939 tree t = fold_convert_loc (loc,
940 sizetype, TREE_OPERAND (incr, 1));
941
942 if (TREE_CODE (incr) == POSTDECREMENT_EXPR
943 || TREE_CODE (incr) == PREDECREMENT_EXPR)
944 t = fold_build1_loc (loc, NEGATE_EXPR, sizetype, t);
945 t = fold_build_pointer_plus (decl, t);
946 incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
947 }
948 return incr;
949 }
950
951 /* Validate and generate OMP_FOR.
952 DECLV is a vector of iteration variables, for each collapsed loop.
953
954 ORIG_DECLV, if non-NULL, is a vector with the original iteration
955 variables (prior to any transformations, by say, C++ iterators).
956
957 INITV, CONDV and INCRV are vectors containing initialization
958 expressions, controlling predicates and increment expressions.
959 BODY is the body of the loop and PRE_BODY statements that go before
960 the loop. */
961
962 tree
963 c_finish_omp_for (location_t locus, enum tree_code code, tree declv,
964 tree orig_declv, tree initv, tree condv, tree incrv,
965 tree body, tree pre_body, bool final_p)
966 {
967 location_t elocus;
968 bool fail = false;
969 int i;
970
971 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
972 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
973 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
974 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
975 {
976 tree decl = TREE_VEC_ELT (declv, i);
977 tree init = TREE_VEC_ELT (initv, i);
978 tree cond = TREE_VEC_ELT (condv, i);
979 tree incr = TREE_VEC_ELT (incrv, i);
980
981 if (init == NULL_TREE)
982 {
983 gcc_assert (decl == NULL_TREE
984 && cond == NULL_TREE
985 && incr == NULL_TREE);
986 for (i++; i < TREE_VEC_LENGTH (declv); i++)
987 gcc_assert (TREE_VEC_ELT (declv, i) == NULL_TREE
988 && TREE_VEC_ELT (initv, i) == NULL_TREE
989 && TREE_VEC_ELT (condv, i) == NULL_TREE
990 && TREE_VEC_ELT (incrv, i) == NULL_TREE);
991 break;
992 }
993
994 elocus = locus;
995 if (EXPR_HAS_LOCATION (init))
996 elocus = EXPR_LOCATION (init);
997
998 /* Validate the iteration variable. */
999 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
1000 && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
1001 {
1002 error_at (elocus, "invalid type for iteration variable %qE", decl);
1003 fail = true;
1004 }
1005 else if (TYPE_ATOMIC (TREE_TYPE (decl)))
1006 {
1007 error_at (elocus, "%<_Atomic%> iteration variable %qE", decl);
1008 fail = true;
1009 /* _Atomic iterator confuses stuff too much, so we risk ICE
1010 trying to diagnose it further. */
1011 continue;
1012 }
1013
1014 /* In the case of "for (int i = 0...)", init will be a decl. It should
1015 have a DECL_INITIAL that we can turn into an assignment. */
1016 if (init == decl)
1017 {
1018 elocus = DECL_SOURCE_LOCATION (decl);
1019
1020 init = DECL_INITIAL (decl);
1021 if (init == NULL)
1022 {
1023 error_at (elocus, "%qE is not initialized", decl);
1024 init = integer_zero_node;
1025 fail = true;
1026 }
1027 DECL_INITIAL (decl) = NULL_TREE;
1028
1029 init = build_modify_expr (elocus, decl, NULL_TREE, NOP_EXPR,
1030 /* FIXME diagnostics: This should
1031 be the location of the INIT. */
1032 elocus,
1033 init,
1034 NULL_TREE);
1035 }
1036 if (init != error_mark_node)
1037 {
1038 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
1039 gcc_assert (TREE_OPERAND (init, 0) == decl);
1040 }
1041
1042 if (cond == NULL_TREE)
1043 {
1044 error_at (elocus, "missing controlling predicate");
1045 fail = true;
1046 }
1047 else
1048 {
1049 bool cond_ok = false;
1050
1051 /* E.g. C sizeof (vla) could add COMPOUND_EXPRs with
1052 evaluation of the vla VAR_DECL. We need to readd
1053 them to the non-decl operand. See PR45784. */
1054 while (TREE_CODE (cond) == COMPOUND_EXPR)
1055 cond = TREE_OPERAND (cond, 1);
1056
1057 if (EXPR_HAS_LOCATION (cond))
1058 elocus = EXPR_LOCATION (cond);
1059
1060 if (TREE_CODE (cond) == LT_EXPR
1061 || TREE_CODE (cond) == LE_EXPR
1062 || TREE_CODE (cond) == GT_EXPR
1063 || TREE_CODE (cond) == GE_EXPR
1064 || TREE_CODE (cond) == NE_EXPR
1065 || TREE_CODE (cond) == EQ_EXPR)
1066 {
1067 tree op0 = TREE_OPERAND (cond, 0);
1068 tree op1 = TREE_OPERAND (cond, 1);
1069
1070 /* 2.5.1. The comparison in the condition is computed in
1071 the type of DECL, otherwise the behavior is undefined.
1072
1073 For example:
1074 long n; int i;
1075 i < n;
1076
1077 according to ISO will be evaluated as:
1078 (long)i < n;
1079
1080 We want to force:
1081 i < (int)n; */
1082 if (TREE_CODE (op0) == NOP_EXPR
1083 && decl == TREE_OPERAND (op0, 0))
1084 {
1085 TREE_OPERAND (cond, 0) = TREE_OPERAND (op0, 0);
1086 TREE_OPERAND (cond, 1)
1087 = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
1088 TREE_OPERAND (cond, 1));
1089 }
1090 else if (TREE_CODE (op1) == NOP_EXPR
1091 && decl == TREE_OPERAND (op1, 0))
1092 {
1093 TREE_OPERAND (cond, 1) = TREE_OPERAND (op1, 0);
1094 TREE_OPERAND (cond, 0)
1095 = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
1096 TREE_OPERAND (cond, 0));
1097 }
1098
1099 if (decl == TREE_OPERAND (cond, 0))
1100 cond_ok = true;
1101 else if (decl == TREE_OPERAND (cond, 1))
1102 {
1103 TREE_SET_CODE (cond,
1104 swap_tree_comparison (TREE_CODE (cond)));
1105 TREE_OPERAND (cond, 1) = TREE_OPERAND (cond, 0);
1106 TREE_OPERAND (cond, 0) = decl;
1107 cond_ok = true;
1108 }
1109
1110 if (TREE_CODE (cond) == NE_EXPR
1111 || TREE_CODE (cond) == EQ_EXPR)
1112 {
1113 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl)))
1114 {
1115 if (code == OACC_LOOP || TREE_CODE (cond) == EQ_EXPR)
1116 cond_ok = false;
1117 }
1118 else if (operand_equal_p (TREE_OPERAND (cond, 1),
1119 TYPE_MIN_VALUE (TREE_TYPE (decl)),
1120 0))
1121 TREE_SET_CODE (cond, TREE_CODE (cond) == NE_EXPR
1122 ? GT_EXPR : LE_EXPR);
1123 else if (operand_equal_p (TREE_OPERAND (cond, 1),
1124 TYPE_MAX_VALUE (TREE_TYPE (decl)),
1125 0))
1126 TREE_SET_CODE (cond, TREE_CODE (cond) == NE_EXPR
1127 ? LT_EXPR : GE_EXPR);
1128 else if (code == OACC_LOOP || TREE_CODE (cond) == EQ_EXPR)
1129 cond_ok = false;
1130 }
1131
1132 if (cond_ok && TREE_VEC_ELT (condv, i) != cond)
1133 {
1134 tree ce = NULL_TREE, *pce = &ce;
1135 tree type = TREE_TYPE (TREE_OPERAND (cond, 1));
1136 for (tree c = TREE_VEC_ELT (condv, i); c != cond;
1137 c = TREE_OPERAND (c, 1))
1138 {
1139 *pce = build2 (COMPOUND_EXPR, type, TREE_OPERAND (c, 0),
1140 TREE_OPERAND (cond, 1));
1141 pce = &TREE_OPERAND (*pce, 1);
1142 }
1143 TREE_OPERAND (cond, 1) = ce;
1144 TREE_VEC_ELT (condv, i) = cond;
1145 }
1146 }
1147
1148 if (!cond_ok)
1149 {
1150 error_at (elocus, "invalid controlling predicate");
1151 fail = true;
1152 }
1153 }
1154
1155 if (incr == NULL_TREE)
1156 {
1157 error_at (elocus, "missing increment expression");
1158 fail = true;
1159 }
1160 else
1161 {
1162 bool incr_ok = false;
1163
1164 if (EXPR_HAS_LOCATION (incr))
1165 elocus = EXPR_LOCATION (incr);
1166
1167 /* Check all the valid increment expressions: v++, v--, ++v, --v,
1168 v = v + incr, v = incr + v and v = v - incr. */
1169 switch (TREE_CODE (incr))
1170 {
1171 case POSTINCREMENT_EXPR:
1172 case PREINCREMENT_EXPR:
1173 case POSTDECREMENT_EXPR:
1174 case PREDECREMENT_EXPR:
1175 if (TREE_OPERAND (incr, 0) != decl)
1176 break;
1177
1178 incr_ok = true;
1179 if (!fail
1180 && TREE_CODE (cond) == NE_EXPR
1181 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
1182 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
1183 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
1184 != INTEGER_CST))
1185 {
1186 /* For pointer to VLA, transform != into < or >
1187 depending on whether incr is increment or decrement. */
1188 if (TREE_CODE (incr) == PREINCREMENT_EXPR
1189 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
1190 TREE_SET_CODE (cond, LT_EXPR);
1191 else
1192 TREE_SET_CODE (cond, GT_EXPR);
1193 }
1194 incr = c_omp_for_incr_canonicalize_ptr (elocus, decl, incr);
1195 break;
1196
1197 case COMPOUND_EXPR:
1198 if ((TREE_CODE (TREE_OPERAND (incr, 0)) != SAVE_EXPR
1199 && TREE_CODE (TREE_OPERAND (incr, 0)) != TARGET_EXPR)
1200 || TREE_CODE (TREE_OPERAND (incr, 1)) != MODIFY_EXPR)
1201 break;
1202 incr = TREE_OPERAND (incr, 1);
1203 /* FALLTHRU */
1204 case MODIFY_EXPR:
1205 if (TREE_OPERAND (incr, 0) != decl)
1206 break;
1207 if (TREE_OPERAND (incr, 1) == decl)
1208 break;
1209 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
1210 && (TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl
1211 || TREE_OPERAND (TREE_OPERAND (incr, 1), 1) == decl))
1212 incr_ok = true;
1213 else if ((TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR
1214 || (TREE_CODE (TREE_OPERAND (incr, 1))
1215 == POINTER_PLUS_EXPR))
1216 && TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl)
1217 incr_ok = true;
1218 else
1219 {
1220 tree t = check_omp_for_incr_expr (elocus,
1221 TREE_OPERAND (incr, 1),
1222 decl);
1223 if (t != error_mark_node)
1224 {
1225 incr_ok = true;
1226 t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
1227 incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
1228 }
1229 }
1230 if (!fail
1231 && incr_ok
1232 && TREE_CODE (cond) == NE_EXPR)
1233 {
1234 tree i = TREE_OPERAND (incr, 1);
1235 i = TREE_OPERAND (i, TREE_OPERAND (i, 0) == decl);
1236 i = c_fully_fold (i, false, NULL);
1237 if (!final_p
1238 && TREE_CODE (i) != INTEGER_CST)
1239 ;
1240 else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
1241 {
1242 tree unit
1243 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
1244 if (unit)
1245 {
1246 enum tree_code ccode = GT_EXPR;
1247 unit = c_fully_fold (unit, false, NULL);
1248 i = fold_convert (TREE_TYPE (unit), i);
1249 if (operand_equal_p (unit, i, 0))
1250 ccode = LT_EXPR;
1251 if (ccode == GT_EXPR)
1252 {
1253 i = fold_unary (NEGATE_EXPR, TREE_TYPE (i), i);
1254 if (i == NULL_TREE
1255 || !operand_equal_p (unit, i, 0))
1256 {
1257 error_at (elocus,
1258 "increment is not constant 1 or "
1259 "-1 for %<!=%> condition");
1260 fail = true;
1261 }
1262 }
1263 if (TREE_CODE (unit) != INTEGER_CST)
1264 /* For pointer to VLA, transform != into < or >
1265 depending on whether the pointer is
1266 incremented or decremented in each
1267 iteration. */
1268 TREE_SET_CODE (cond, ccode);
1269 }
1270 }
1271 else
1272 {
1273 if (!integer_onep (i) && !integer_minus_onep (i))
1274 {
1275 error_at (elocus,
1276 "increment is not constant 1 or -1 for"
1277 " %<!=%> condition");
1278 fail = true;
1279 }
1280 }
1281 }
1282 break;
1283
1284 default:
1285 break;
1286 }
1287 if (!incr_ok)
1288 {
1289 error_at (elocus, "invalid increment expression");
1290 fail = true;
1291 }
1292 }
1293
1294 TREE_VEC_ELT (initv, i) = init;
1295 TREE_VEC_ELT (incrv, i) = incr;
1296 }
1297
1298 if (fail)
1299 return NULL;
1300 else
1301 {
1302 tree t = make_node (code);
1303
1304 TREE_TYPE (t) = void_type_node;
1305 OMP_FOR_INIT (t) = initv;
1306 OMP_FOR_COND (t) = condv;
1307 OMP_FOR_INCR (t) = incrv;
1308 OMP_FOR_BODY (t) = body;
1309 OMP_FOR_PRE_BODY (t) = pre_body;
1310 OMP_FOR_ORIG_DECLS (t) = orig_declv;
1311
1312 SET_EXPR_LOCATION (t, locus);
1313 return t;
1314 }
1315 }
1316
1317 /* Type for passing data in between c_omp_check_loop_iv and
1318 c_omp_check_loop_iv_r. */
1319
1320 struct c_omp_check_loop_iv_data
1321 {
1322 tree declv;
1323 bool fail;
1324 bool maybe_nonrect;
1325 location_t stmt_loc;
1326 location_t expr_loc;
1327 int kind;
1328 int idx;
1329 walk_tree_lh lh;
1330 hash_set<tree> *ppset;
1331 };
1332
1333 /* Return -1 if DECL is not a loop iterator in loop nest D, otherwise
1334 return the index of the loop in which it is an iterator.
1335 Return TREE_VEC_LENGTH (d->declv) if it is a C++ range for iterator. */
1336
1337 static int
1338 c_omp_is_loop_iterator (tree decl, struct c_omp_check_loop_iv_data *d)
1339 {
1340 for (int i = 0; i < TREE_VEC_LENGTH (d->declv); i++)
1341 if (TREE_VEC_ELT (d->declv, i) == NULL_TREE)
1342 continue;
1343 else if (decl == TREE_VEC_ELT (d->declv, i)
1344 || (TREE_CODE (TREE_VEC_ELT (d->declv, i)) == TREE_LIST
1345 && decl == TREE_PURPOSE (TREE_VEC_ELT (d->declv, i))))
1346 return i;
1347 else if (TREE_CODE (TREE_VEC_ELT (d->declv, i)) == TREE_LIST
1348 && TREE_CHAIN (TREE_VEC_ELT (d->declv, i))
1349 && (TREE_CODE (TREE_CHAIN (TREE_VEC_ELT (d->declv, i)))
1350 == TREE_VEC))
1351 for (int j = 2;
1352 j < TREE_VEC_LENGTH (TREE_CHAIN (TREE_VEC_ELT (d->declv, i))); j++)
1353 if (decl == TREE_VEC_ELT (TREE_CHAIN (TREE_VEC_ELT (d->declv, i)), j))
1354 return TREE_VEC_LENGTH (d->declv);
1355 return -1;
1356 }
1357
1358 /* Helper function called via walk_tree, to diagnose uses
1359 of associated loop IVs inside of lb, b and incr expressions
1360 of OpenMP loops. */
1361
1362 static tree
1363 c_omp_check_loop_iv_r (tree *tp, int *walk_subtrees, void *data)
1364 {
1365 struct c_omp_check_loop_iv_data *d
1366 = (struct c_omp_check_loop_iv_data *) data;
1367 if (DECL_P (*tp))
1368 {
1369 int idx = c_omp_is_loop_iterator (*tp, d);
1370 if (idx == -1)
1371 return NULL_TREE;
1372
1373 if ((d->kind & 4) && idx < d->idx)
1374 {
1375 d->maybe_nonrect = true;
1376 return NULL_TREE;
1377 }
1378
1379 if (d->ppset->add (*tp))
1380 return NULL_TREE;
1381
1382 location_t loc = d->expr_loc;
1383 if (loc == UNKNOWN_LOCATION)
1384 loc = d->stmt_loc;
1385
1386 switch (d->kind & 3)
1387 {
1388 case 0:
1389 error_at (loc, "initializer expression refers to "
1390 "iteration variable %qD", *tp);
1391 break;
1392 case 1:
1393 error_at (loc, "condition expression refers to "
1394 "iteration variable %qD", *tp);
1395 break;
1396 case 2:
1397 error_at (loc, "increment expression refers to "
1398 "iteration variable %qD", *tp);
1399 break;
1400 }
1401 d->fail = true;
1402 }
1403 else if ((d->kind & 4)
1404 && TREE_CODE (*tp) != TREE_VEC
1405 && TREE_CODE (*tp) != PLUS_EXPR
1406 && TREE_CODE (*tp) != MINUS_EXPR
1407 && TREE_CODE (*tp) != MULT_EXPR
1408 && TREE_CODE (*tp) != POINTER_PLUS_EXPR
1409 && !CONVERT_EXPR_P (*tp))
1410 {
1411 *walk_subtrees = 0;
1412 d->kind &= 3;
1413 walk_tree_1 (tp, c_omp_check_loop_iv_r, data, NULL, d->lh);
1414 d->kind |= 4;
1415 return NULL_TREE;
1416 }
1417 else if (d->ppset->add (*tp))
1418 *walk_subtrees = 0;
1419 /* Don't walk dtors added by C++ wrap_cleanups_r. */
1420 else if (TREE_CODE (*tp) == TRY_CATCH_EXPR
1421 && TRY_CATCH_IS_CLEANUP (*tp))
1422 {
1423 *walk_subtrees = 0;
1424 return walk_tree_1 (&TREE_OPERAND (*tp, 0), c_omp_check_loop_iv_r, data,
1425 NULL, d->lh);
1426 }
1427
1428 return NULL_TREE;
1429 }
1430
1431 /* Check the allowed expressions for non-rectangular loop nest lb and b
1432 expressions. Return the outer var decl referenced in the expression. */
1433
1434 static tree
1435 c_omp_check_nonrect_loop_iv (tree *tp, struct c_omp_check_loop_iv_data *d,
1436 walk_tree_lh lh)
1437 {
1438 d->maybe_nonrect = false;
1439 if (d->fail)
1440 return NULL_TREE;
1441
1442 hash_set<tree> pset;
1443 hash_set<tree> *ppset = d->ppset;
1444 d->ppset = &pset;
1445
1446 tree t = *tp;
1447 if (TREE_CODE (t) == TREE_VEC
1448 && TREE_VEC_LENGTH (t) == 3
1449 && DECL_P (TREE_VEC_ELT (t, 0))
1450 && c_omp_is_loop_iterator (TREE_VEC_ELT (t, 0), d) >= 0)
1451 {
1452 d->kind &= 3;
1453 walk_tree_1 (&TREE_VEC_ELT (t, 1), c_omp_check_loop_iv_r, d, NULL, lh);
1454 walk_tree_1 (&TREE_VEC_ELT (t, 1), c_omp_check_loop_iv_r, d, NULL, lh);
1455 d->ppset = ppset;
1456 return d->fail ? NULL_TREE : TREE_VEC_ELT (t, 0);
1457 }
1458
1459 while (CONVERT_EXPR_P (t))
1460 t = TREE_OPERAND (t, 0);
1461
1462 tree a1 = t, a2 = integer_zero_node;
1463 bool neg_a1 = false, neg_a2 = false;
1464 switch (TREE_CODE (t))
1465 {
1466 case PLUS_EXPR:
1467 case MINUS_EXPR:
1468 a1 = TREE_OPERAND (t, 0);
1469 a2 = TREE_OPERAND (t, 1);
1470 while (CONVERT_EXPR_P (a1))
1471 a1 = TREE_OPERAND (a1, 0);
1472 while (CONVERT_EXPR_P (a2))
1473 a2 = TREE_OPERAND (a2, 0);
1474 if (DECL_P (a1) && c_omp_is_loop_iterator (a1, d) >= 0)
1475 {
1476 a2 = TREE_OPERAND (t, 1);
1477 if (TREE_CODE (t) == MINUS_EXPR)
1478 neg_a2 = true;
1479 t = a1;
1480 break;
1481 }
1482 if (DECL_P (a2) && c_omp_is_loop_iterator (a2, d) >= 0)
1483 {
1484 a1 = TREE_OPERAND (t, 0);
1485 if (TREE_CODE (t) == MINUS_EXPR)
1486 neg_a1 = true;
1487 t = a2;
1488 a2 = a1;
1489 break;
1490 }
1491 if (TREE_CODE (a1) == MULT_EXPR && TREE_CODE (a2) == MULT_EXPR)
1492 {
1493 tree o1 = TREE_OPERAND (a1, 0);
1494 tree o2 = TREE_OPERAND (a1, 1);
1495 while (CONVERT_EXPR_P (o1))
1496 o1 = TREE_OPERAND (o1, 0);
1497 while (CONVERT_EXPR_P (o2))
1498 o2 = TREE_OPERAND (o2, 0);
1499 if ((DECL_P (o1) && c_omp_is_loop_iterator (o1, d) >= 0)
1500 || (DECL_P (o2) && c_omp_is_loop_iterator (o2, d) >= 0))
1501 {
1502 a2 = TREE_OPERAND (t, 1);
1503 if (TREE_CODE (t) == MINUS_EXPR)
1504 neg_a2 = true;
1505 t = a1;
1506 break;
1507 }
1508 }
1509 if (TREE_CODE (a2) == MULT_EXPR)
1510 {
1511 a1 = TREE_OPERAND (t, 0);
1512 if (TREE_CODE (t) == MINUS_EXPR)
1513 neg_a1 = true;
1514 t = a2;
1515 a2 = a1;
1516 break;
1517 }
1518 if (TREE_CODE (a1) == MULT_EXPR)
1519 {
1520 a2 = TREE_OPERAND (t, 1);
1521 if (TREE_CODE (t) == MINUS_EXPR)
1522 neg_a2 = true;
1523 t = a1;
1524 break;
1525 }
1526 a2 = integer_zero_node;
1527 break;
1528 case POINTER_PLUS_EXPR:
1529 a1 = TREE_OPERAND (t, 0);
1530 a2 = TREE_OPERAND (t, 1);
1531 while (CONVERT_EXPR_P (a1))
1532 a1 = TREE_OPERAND (a1, 0);
1533 if (DECL_P (a1) && c_omp_is_loop_iterator (a1, d) >= 0)
1534 {
1535 a2 = TREE_OPERAND (t, 1);
1536 t = a1;
1537 break;
1538 }
1539 break;
1540 default:
1541 break;
1542 }
1543
1544 a1 = integer_one_node;
1545 if (TREE_CODE (t) == MULT_EXPR)
1546 {
1547 tree o1 = TREE_OPERAND (t, 0);
1548 tree o2 = TREE_OPERAND (t, 1);
1549 while (CONVERT_EXPR_P (o1))
1550 o1 = TREE_OPERAND (o1, 0);
1551 while (CONVERT_EXPR_P (o2))
1552 o2 = TREE_OPERAND (o2, 0);
1553 if (DECL_P (o1) && c_omp_is_loop_iterator (o1, d) >= 0)
1554 {
1555 a1 = TREE_OPERAND (t, 1);
1556 t = o1;
1557 }
1558 else if (DECL_P (o2) && c_omp_is_loop_iterator (o2, d) >= 0)
1559 {
1560 a1 = TREE_OPERAND (t, 0);
1561 t = o2;
1562 }
1563 }
1564
1565 d->kind &= 3;
1566 tree ret = NULL_TREE;
1567 if (DECL_P (t) && c_omp_is_loop_iterator (t, d) >= 0)
1568 {
1569 location_t loc = d->expr_loc;
1570 if (loc == UNKNOWN_LOCATION)
1571 loc = d->stmt_loc;
1572 if (!lang_hooks.types_compatible_p (TREE_TYPE (*tp), TREE_TYPE (t)))
1573 {
1574 if (d->kind == 0)
1575 error_at (loc, "outer iteration variable %qD used in initializer"
1576 " expression has type other than %qT",
1577 t, TREE_TYPE (*tp));
1578 else
1579 error_at (loc, "outer iteration variable %qD used in condition"
1580 " expression has type other than %qT",
1581 t, TREE_TYPE (*tp));
1582 d->fail = true;
1583 }
1584 else if (!INTEGRAL_TYPE_P (TREE_TYPE (a1)))
1585 {
1586 error_at (loc, "outer iteration variable %qD multiplier expression"
1587 " %qE is not integral", t, a1);
1588 d->fail = true;
1589 }
1590 else if (!INTEGRAL_TYPE_P (TREE_TYPE (a2)))
1591 {
1592 error_at (loc, "outer iteration variable %qD addend expression"
1593 " %qE is not integral", t, a2);
1594 d->fail = true;
1595 }
1596 else
1597 {
1598 walk_tree_1 (&a1, c_omp_check_loop_iv_r, d, NULL, lh);
1599 walk_tree_1 (&a2, c_omp_check_loop_iv_r, d, NULL, lh);
1600 }
1601 if (!d->fail)
1602 {
1603 a1 = fold_convert (TREE_TYPE (*tp), a1);
1604 a2 = fold_convert (TREE_TYPE (*tp), a2);
1605 if (neg_a1)
1606 a1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a1), a1);
1607 if (neg_a2)
1608 a2 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a2), a2);
1609 ret = t;
1610 *tp = make_tree_vec (3);
1611 TREE_VEC_ELT (*tp, 0) = t;
1612 TREE_VEC_ELT (*tp, 1) = a1;
1613 TREE_VEC_ELT (*tp, 2) = a2;
1614 }
1615 }
1616 else
1617 walk_tree_1 (&t, c_omp_check_loop_iv_r, d, NULL, lh);
1618
1619 d->ppset = ppset;
1620 return ret;
1621 }
1622
1623 /* Callback for walk_tree to find nested loop transforming construct. */
1624
1625 static tree
1626 c_find_nested_loop_xform_r (tree *tp, int *walk_subtrees, void *)
1627 {
1628 *walk_subtrees = 0;
1629 switch (TREE_CODE (*tp))
1630 {
1631 case OMP_TILE:
1632 case OMP_UNROLL:
1633 return *tp;
1634 case BIND_EXPR:
1635 *walk_subtrees = 1;
1636 break;
1637 case STATEMENT_LIST:
1638 *walk_subtrees = 1;
1639 break;
1640 case TRY_FINALLY_EXPR:
1641 case CLEANUP_POINT_EXPR:
1642 *walk_subtrees = 1;
1643 break;
1644 default:
1645 break;
1646 }
1647 return NULL;
1648 }
1649
1650 /* Find Jth loop among generated loops of STMT. */
1651
1652 int
1653 c_omp_find_generated_loop (tree &stmt, int j, walk_tree_lh lh)
1654 {
1655 stmt = walk_tree_1 (&stmt, c_find_nested_loop_xform_r,
1656 NULL, NULL, lh);
1657 gcc_assert (stmt);
1658 switch (TREE_CODE (stmt))
1659 {
1660 case OMP_UNROLL:
1661 gcc_assert (omp_find_clause (OMP_FOR_CLAUSES (stmt),
1662 OMP_CLAUSE_PARTIAL));
1663 /* FALLTHRU */
1664 case OMP_TILE:
1665 int k;
1666 k = 0;
1667 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (stmt)); ++i)
1668 if (i == j)
1669 {
1670 if (TREE_VEC_ELT (OMP_FOR_INIT (stmt), i) == NULL_TREE)
1671 {
1672 stmt = OMP_FOR_BODY (stmt);
1673 return c_omp_find_generated_loop (stmt, k, lh);
1674 }
1675 else
1676 return i;
1677 }
1678 else if (TREE_VEC_ELT (OMP_FOR_INIT (stmt), i) == NULL_TREE)
1679 ++k;
1680 gcc_unreachable ();
1681 default:
1682 gcc_unreachable ();
1683 }
1684 }
1685
1686 /* Diagnose invalid references to loop iterators in lb, b and incr
1687 expressions. */
1688
1689 bool
1690 c_omp_check_loop_iv (tree stmt, tree declv, walk_tree_lh lh)
1691 {
1692 hash_set<tree> pset;
1693 struct c_omp_check_loop_iv_data data;
1694 int i, k = 0;
1695
1696 data.declv = declv;
1697 data.fail = false;
1698 data.maybe_nonrect = false;
1699 data.stmt_loc = EXPR_LOCATION (stmt);
1700 data.lh = lh;
1701 data.ppset = &pset;
1702 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (stmt)); i++)
1703 {
1704 tree this_stmt = stmt;
1705 int j = i;
1706 tree init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), i);
1707 if (init == NULL_TREE)
1708 {
1709 if (k == 0)
1710 data.declv = copy_node (declv);
1711 this_stmt = OMP_FOR_BODY (stmt);
1712 j = c_omp_find_generated_loop (this_stmt, k++, lh);
1713 init = TREE_VEC_ELT (OMP_FOR_INIT (this_stmt), j);
1714 TREE_VEC_ELT (data.declv, i) = TREE_OPERAND (init, 0);
1715 }
1716 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
1717 tree decl = TREE_OPERAND (init, 0);
1718 tree cond = TREE_VEC_ELT (OMP_FOR_COND (this_stmt), j);
1719 gcc_assert (COMPARISON_CLASS_P (cond));
1720 gcc_assert (TREE_OPERAND (cond, 0) == decl);
1721 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (this_stmt), j);
1722 data.expr_loc = EXPR_LOCATION (TREE_OPERAND (init, 1));
1723 tree vec_outer1 = NULL_TREE, vec_outer2 = NULL_TREE;
1724 int kind = 0;
1725 if (i > 0
1726 && (unsigned) c_omp_is_loop_iterator (decl, &data) < (unsigned) i)
1727 {
1728 location_t loc = data.expr_loc;
1729 if (loc == UNKNOWN_LOCATION)
1730 loc = data.stmt_loc;
1731 error_at (loc, "the same loop iteration variables %qD used in "
1732 "multiple associated loops", decl);
1733 data.fail = true;
1734 }
1735 /* Handle non-rectangular loop nests. */
1736 if (TREE_CODE (stmt) != OACC_LOOP && i > 0)
1737 kind = 4;
1738 data.kind = kind;
1739 data.idx = i;
1740 walk_tree_1 (&TREE_OPERAND (init, 1),
1741 c_omp_check_loop_iv_r, &data, NULL, lh);
1742 if (data.maybe_nonrect)
1743 vec_outer1 = c_omp_check_nonrect_loop_iv (&TREE_OPERAND (init, 1),
1744 &data, lh);
1745 /* Don't warn for C++ random access iterators here, the
1746 expression then involves the subtraction and always refers
1747 to the original value. The C++ FE needs to warn on those
1748 earlier. */
1749 if (decl == TREE_VEC_ELT (data.declv, i)
1750 || (TREE_CODE (TREE_VEC_ELT (data.declv, i)) == TREE_LIST
1751 && decl == TREE_PURPOSE (TREE_VEC_ELT (data.declv, i))))
1752 {
1753 data.expr_loc = EXPR_LOCATION (cond);
1754 data.kind = kind | 1;
1755 walk_tree_1 (&TREE_OPERAND (cond, 1),
1756 c_omp_check_loop_iv_r, &data, NULL, lh);
1757 if (data.maybe_nonrect)
1758 vec_outer2 = c_omp_check_nonrect_loop_iv (&TREE_OPERAND (cond, 1),
1759 &data, lh);
1760 }
1761 if (vec_outer1 && vec_outer2 && vec_outer1 != vec_outer2)
1762 {
1763 location_t loc = data.expr_loc;
1764 if (loc == UNKNOWN_LOCATION)
1765 loc = data.stmt_loc;
1766 error_at (loc, "two different outer iteration variables %qD and %qD"
1767 " used in a single loop", vec_outer1, vec_outer2);
1768 data.fail = true;
1769 }
1770 else if ((vec_outer1 || vec_outer2) && this_stmt != stmt)
1771 {
1772 location_t loc = data.expr_loc;
1773 if (loc == UNKNOWN_LOCATION)
1774 loc = data.stmt_loc;
1775 sorry_at (loc, "non-rectangular loops from generated loops "
1776 "unsupported");
1777 data.fail = true;
1778 }
1779 if (vec_outer1 || vec_outer2)
1780 OMP_FOR_NON_RECTANGULAR (stmt) = 1;
1781 if (TREE_CODE (incr) == MODIFY_EXPR)
1782 {
1783 gcc_assert (TREE_OPERAND (incr, 0) == decl);
1784 incr = TREE_OPERAND (incr, 1);
1785 data.kind = 2;
1786 if (TREE_CODE (incr) == PLUS_EXPR
1787 && TREE_OPERAND (incr, 1) == decl)
1788 {
1789 data.expr_loc = EXPR_LOCATION (TREE_OPERAND (incr, 0));
1790 walk_tree_1 (&TREE_OPERAND (incr, 0),
1791 c_omp_check_loop_iv_r, &data, NULL, lh);
1792 }
1793 else
1794 {
1795 data.expr_loc = EXPR_LOCATION (TREE_OPERAND (incr, 1));
1796 walk_tree_1 (&TREE_OPERAND (incr, 1),
1797 c_omp_check_loop_iv_r, &data, NULL, lh);
1798 }
1799 }
1800 }
1801 return !data.fail;
1802 }
1803
1804 /* Similar, but allows to check the init or cond expressions individually. */
1805
1806 bool
1807 c_omp_check_loop_iv_exprs (location_t stmt_loc, enum tree_code code,
1808 tree declv, int i, tree decl, tree init, tree cond,
1809 walk_tree_lh lh)
1810 {
1811 hash_set<tree> pset;
1812 struct c_omp_check_loop_iv_data data;
1813 int kind = (code != OACC_LOOP && i > 0) ? 4 : 0;
1814
1815 data.declv = declv;
1816 data.fail = false;
1817 data.maybe_nonrect = false;
1818 data.stmt_loc = stmt_loc;
1819 data.lh = lh;
1820 data.ppset = &pset;
1821 data.idx = i;
1822 if (i > 0
1823 && (unsigned) c_omp_is_loop_iterator (decl, &data) < (unsigned) i)
1824 {
1825 error_at (stmt_loc, "the same loop iteration variables %qD used in "
1826 "multiple associated loops", decl);
1827 data.fail = true;
1828 }
1829 if (init)
1830 {
1831 data.expr_loc = EXPR_LOCATION (init);
1832 data.kind = kind;
1833 walk_tree_1 (&init,
1834 c_omp_check_loop_iv_r, &data, NULL, lh);
1835 }
1836 if (cond)
1837 {
1838 gcc_assert (COMPARISON_CLASS_P (cond));
1839 data.expr_loc = EXPR_LOCATION (init);
1840 data.kind = kind | 1;
1841 if (TREE_OPERAND (cond, 0) == decl)
1842 walk_tree_1 (&TREE_OPERAND (cond, 1),
1843 c_omp_check_loop_iv_r, &data, NULL, lh);
1844 else
1845 walk_tree_1 (&TREE_OPERAND (cond, 0),
1846 c_omp_check_loop_iv_r, &data, NULL, lh);
1847 }
1848 return !data.fail;
1849 }
1850
1851
1852 /* Helper function for c_omp_check_loop_binding_exprs: look for a binding
1853 of DECL in BODY. Only traverse things that might be containers for
1854 intervening code in an OMP loop. Returns the BIND_EXPR or DECL_EXPR
1855 if found, otherwise null. */
1856
1857 static tree
1858 find_binding_in_body (tree decl, tree body)
1859 {
1860 if (!body)
1861 return NULL_TREE;
1862
1863 switch (TREE_CODE (body))
1864 {
1865 case BIND_EXPR:
1866 for (tree b = BIND_EXPR_VARS (body); b; b = DECL_CHAIN (b))
1867 if (b == decl)
1868 return body;
1869 return find_binding_in_body (decl, BIND_EXPR_BODY (body));
1870
1871 case DECL_EXPR:
1872 if (DECL_EXPR_DECL (body) == decl)
1873 return body;
1874 return NULL_TREE;
1875
1876 case STATEMENT_LIST:
1877 for (tree_stmt_iterator si = tsi_start (body); !tsi_end_p (si);
1878 tsi_next (&si))
1879 {
1880 tree b = find_binding_in_body (decl, tsi_stmt (si));
1881 if (b)
1882 return b;
1883 }
1884 return NULL_TREE;
1885
1886 case OMP_STRUCTURED_BLOCK:
1887 return find_binding_in_body (decl, OMP_BODY (body));
1888
1889 default:
1890 return NULL_TREE;
1891 }
1892 }
1893
1894 /* Traversal function for check_loop_binding_expr, to diagnose
1895 errors when a binding made in intervening code is referenced outside
1896 of the loop. Returns non-null if such a reference is found. DATA points
1897 to the tree containing the loop body. */
1898
1899 static tree
1900 check_loop_binding_expr_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
1901 void *data)
1902 {
1903 tree body = *(tree *)data;
1904
1905 if (DECL_P (*tp) && find_binding_in_body (*tp, body))
1906 return *tp;
1907 return NULL_TREE;
1908 }
1909
1910 /* Helper macro used below. */
1911
1912 #define LOCATION_OR(loc1, loc2) \
1913 ((loc1) != UNKNOWN_LOCATION ? (loc1) : (loc2))
1914
1915 enum check_loop_binding_expr_ctx {
1916 CHECK_LOOP_BINDING_EXPR_CTX_LOOP_VAR,
1917 CHECK_LOOP_BINDING_EXPR_CTX_IN_INIT,
1918 CHECK_LOOP_BINDING_EXPR_CTX_END_TEST,
1919 CHECK_LOOP_BINDING_EXPR_CTX_INCR
1920 };
1921
1922 /* Check a single expression EXPR for references to variables bound in
1923 intervening code in BODY. Return true if ok, otherwise give an error
1924 referencing CONTEXT and return false. Use LOC for the error message
1925 if EXPR doesn't have one. */
1926 static bool
1927 check_loop_binding_expr (tree expr, tree body, location_t loc,
1928 check_loop_binding_expr_ctx ctx)
1929 {
1930 tree bad = walk_tree (&expr, check_loop_binding_expr_r, (void *)&body, NULL);
1931
1932 if (bad)
1933 {
1934 location_t eloc = EXPR_LOCATION (expr);
1935 eloc = LOCATION_OR (eloc, loc);
1936 switch (ctx)
1937 {
1938 case CHECK_LOOP_BINDING_EXPR_CTX_LOOP_VAR:
1939 error_at (eloc, "variable %qD used as loop variable is bound "
1940 "in intervening code", bad);
1941 break;
1942 case CHECK_LOOP_BINDING_EXPR_CTX_IN_INIT:
1943 error_at (eloc, "variable %qD used in initializer is bound "
1944 "in intervening code", bad);
1945 break;
1946 case CHECK_LOOP_BINDING_EXPR_CTX_END_TEST:
1947 error_at (eloc, "variable %qD used in end test is bound "
1948 "in intervening code", bad);
1949 break;
1950 case CHECK_LOOP_BINDING_EXPR_CTX_INCR:
1951 error_at (eloc, "variable %qD used in increment expression is bound "
1952 "in intervening code", bad);
1953 break;
1954 }
1955 return false;
1956 }
1957 return true;
1958 }
1959
1960 /* STMT is an OMP_FOR construct. Check all of the iteration variable,
1961 initializer, end condition, and increment for bindings inside the
1962 loop body. If ORIG_INITS is provided, check those elements too.
1963 Return true if OK, false otherwise. */
1964 bool
1965 c_omp_check_loop_binding_exprs (tree stmt, vec<tree> *orig_inits)
1966 {
1967 bool ok = true;
1968 location_t loc = EXPR_LOCATION (stmt);
1969 tree body = OMP_FOR_BODY (stmt);
1970 int orig_init_length = orig_inits ? orig_inits->length () : 0;
1971
1972 for (int i = 1; i < TREE_VEC_LENGTH (OMP_FOR_INIT (stmt)); i++)
1973 {
1974 tree init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), i);
1975 if (init == NULL_TREE)
1976 {
1977 sorry_at (loc, "imperfectly nested loop using generated loops");
1978 ok = false;
1979 continue;
1980 }
1981 tree cond = TREE_VEC_ELT (OMP_FOR_COND (stmt), i);
1982 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (stmt), i);
1983 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
1984 tree decl = TREE_OPERAND (init, 0);
1985 tree orig_init = i < orig_init_length ? (*orig_inits)[i] : NULL_TREE;
1986 tree e;
1987 location_t eloc;
1988
1989 e = TREE_OPERAND (init, 1);
1990 eloc = LOCATION_OR (EXPR_LOCATION (init), loc);
1991 if (!check_loop_binding_expr (decl, body, eloc,
1992 CHECK_LOOP_BINDING_EXPR_CTX_LOOP_VAR))
1993 ok = false;
1994 if (!check_loop_binding_expr (e, body, eloc,
1995 CHECK_LOOP_BINDING_EXPR_CTX_IN_INIT))
1996 ok = false;
1997 if (orig_init
1998 && !check_loop_binding_expr (orig_init, body, eloc,
1999 CHECK_LOOP_BINDING_EXPR_CTX_IN_INIT))
2000 ok = false;
2001
2002 /* INCR and/or COND may be null if this is a template with a
2003 class iterator. */
2004 if (cond)
2005 {
2006 eloc = LOCATION_OR (EXPR_LOCATION (cond), loc);
2007 if (COMPARISON_CLASS_P (cond) && TREE_OPERAND (cond, 0) == decl)
2008 e = TREE_OPERAND (cond, 1);
2009 else if (COMPARISON_CLASS_P (cond) && TREE_OPERAND (cond, 1) == decl)
2010 e = TREE_OPERAND (cond, 0);
2011 else
2012 e = cond;
2013 if (!check_loop_binding_expr (e, body, eloc,
2014 CHECK_LOOP_BINDING_EXPR_CTX_END_TEST))
2015 ok = false;
2016 }
2017
2018 if (incr)
2019 {
2020 eloc = LOCATION_OR (EXPR_LOCATION (incr), loc);
2021 /* INCR should be either a MODIFY_EXPR or pre/post
2022 increment/decrement. We don't have to check the latter
2023 since there are no operands besides the iteration variable. */
2024 if (TREE_CODE (incr) == MODIFY_EXPR
2025 && !check_loop_binding_expr (TREE_OPERAND (incr, 1), body, eloc,
2026 CHECK_LOOP_BINDING_EXPR_CTX_INCR))
2027 ok = false;
2028 }
2029 }
2030
2031 return ok;
2032 }
2033
2034 /* This function splits clauses for OpenACC combined loop
2035 constructs. OpenACC combined loop constructs are:
2036 #pragma acc kernels loop
2037 #pragma acc parallel loop */
2038
2039 tree
2040 c_oacc_split_loop_clauses (tree clauses, tree *not_loop_clauses,
2041 bool is_parallel)
2042 {
2043 tree next, loop_clauses, nc;
2044
2045 loop_clauses = *not_loop_clauses = NULL_TREE;
2046 for (; clauses ; clauses = next)
2047 {
2048 next = OMP_CLAUSE_CHAIN (clauses);
2049
2050 switch (OMP_CLAUSE_CODE (clauses))
2051 {
2052 /* Loop clauses. */
2053 case OMP_CLAUSE_COLLAPSE:
2054 case OMP_CLAUSE_TILE:
2055 case OMP_CLAUSE_GANG:
2056 case OMP_CLAUSE_WORKER:
2057 case OMP_CLAUSE_VECTOR:
2058 case OMP_CLAUSE_AUTO:
2059 case OMP_CLAUSE_SEQ:
2060 case OMP_CLAUSE_INDEPENDENT:
2061 case OMP_CLAUSE_PRIVATE:
2062 OMP_CLAUSE_CHAIN (clauses) = loop_clauses;
2063 loop_clauses = clauses;
2064 break;
2065
2066 /* Reductions must be duplicated on both constructs. */
2067 case OMP_CLAUSE_REDUCTION:
2068 if (is_parallel)
2069 {
2070 nc = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2071 OMP_CLAUSE_REDUCTION);
2072 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (clauses);
2073 OMP_CLAUSE_REDUCTION_CODE (nc)
2074 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2075 OMP_CLAUSE_CHAIN (nc) = *not_loop_clauses;
2076 *not_loop_clauses = nc;
2077 }
2078
2079 OMP_CLAUSE_CHAIN (clauses) = loop_clauses;
2080 loop_clauses = clauses;
2081 break;
2082
2083 /* Parallel/kernels clauses. */
2084 default:
2085 OMP_CLAUSE_CHAIN (clauses) = *not_loop_clauses;
2086 *not_loop_clauses = clauses;
2087 break;
2088 }
2089 }
2090
2091 return loop_clauses;
2092 }
2093
2094 /* This function attempts to split or duplicate clauses for OpenMP
2095 combined/composite constructs. Right now there are 30 different
2096 constructs. CODE is the innermost construct in the combined construct,
2097 and MASK allows to determine which constructs are combined together,
2098 as every construct has at least one clause that no other construct
2099 has (except for OMP_SECTIONS, but that can be only combined with parallel,
2100 and OMP_MASTER, which doesn't have any clauses at all).
2101 OpenMP combined/composite constructs are:
2102 #pragma omp distribute parallel for
2103 #pragma omp distribute parallel for simd
2104 #pragma omp distribute simd
2105 #pragma omp for simd
2106 #pragma omp masked taskloop
2107 #pragma omp masked taskloop simd
2108 #pragma omp master taskloop
2109 #pragma omp master taskloop simd
2110 #pragma omp parallel for
2111 #pragma omp parallel for simd
2112 #pragma omp parallel loop
2113 #pragma omp parallel masked
2114 #pragma omp parallel masked taskloop
2115 #pragma omp parallel masked taskloop simd
2116 #pragma omp parallel master
2117 #pragma omp parallel master taskloop
2118 #pragma omp parallel master taskloop simd
2119 #pragma omp parallel sections
2120 #pragma omp target parallel
2121 #pragma omp target parallel for
2122 #pragma omp target parallel for simd
2123 #pragma omp target parallel loop
2124 #pragma omp target teams
2125 #pragma omp target teams distribute
2126 #pragma omp target teams distribute parallel for
2127 #pragma omp target teams distribute parallel for simd
2128 #pragma omp target teams distribute simd
2129 #pragma omp target teams loop
2130 #pragma omp target simd
2131 #pragma omp taskloop simd
2132 #pragma omp teams distribute
2133 #pragma omp teams distribute parallel for
2134 #pragma omp teams distribute parallel for simd
2135 #pragma omp teams distribute simd
2136 #pragma omp teams loop */
2137
2138 void
2139 c_omp_split_clauses (location_t loc, enum tree_code code,
2140 omp_clause_mask mask, tree clauses, tree *cclauses)
2141 {
2142 tree next, c;
2143 enum c_omp_clause_split s;
2144 int i;
2145 bool has_dup_allocate = false;
2146
2147 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
2148 cclauses[i] = NULL;
2149 /* Add implicit nowait clause on
2150 #pragma omp parallel {for,for simd,sections}. */
2151 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
2152 switch (code)
2153 {
2154 case OMP_FOR:
2155 case OMP_SIMD:
2156 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2157 cclauses[C_OMP_CLAUSE_SPLIT_FOR]
2158 = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
2159 break;
2160 case OMP_SECTIONS:
2161 cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS]
2162 = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
2163 break;
2164 default:
2165 break;
2166 }
2167
2168 for (; clauses ; clauses = next)
2169 {
2170 next = OMP_CLAUSE_CHAIN (clauses);
2171
2172 switch (OMP_CLAUSE_CODE (clauses))
2173 {
2174 /* First the clauses that are unique to some constructs. */
2175 case OMP_CLAUSE_DEVICE:
2176 case OMP_CLAUSE_MAP:
2177 case OMP_CLAUSE_IS_DEVICE_PTR:
2178 case OMP_CLAUSE_HAS_DEVICE_ADDR:
2179 case OMP_CLAUSE_DEFAULTMAP:
2180 case OMP_CLAUSE_DEPEND:
2181 s = C_OMP_CLAUSE_SPLIT_TARGET;
2182 break;
2183 case OMP_CLAUSE_DOACROSS:
2184 /* This can happen with invalid depend(source) or
2185 depend(sink:vec) on target combined with other constructs. */
2186 gcc_assert (OMP_CLAUSE_DOACROSS_DEPEND (clauses));
2187 s = C_OMP_CLAUSE_SPLIT_TARGET;
2188 break;
2189 case OMP_CLAUSE_NUM_TEAMS:
2190 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2191 break;
2192 case OMP_CLAUSE_DIST_SCHEDULE:
2193 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2194 break;
2195 case OMP_CLAUSE_COPYIN:
2196 case OMP_CLAUSE_NUM_THREADS:
2197 case OMP_CLAUSE_PROC_BIND:
2198 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2199 break;
2200 case OMP_CLAUSE_ORDERED:
2201 s = C_OMP_CLAUSE_SPLIT_FOR;
2202 break;
2203 case OMP_CLAUSE_SCHEDULE:
2204 s = C_OMP_CLAUSE_SPLIT_FOR;
2205 if (code != OMP_SIMD)
2206 OMP_CLAUSE_SCHEDULE_SIMD (clauses) = 0;
2207 break;
2208 case OMP_CLAUSE_SAFELEN:
2209 case OMP_CLAUSE_SIMDLEN:
2210 case OMP_CLAUSE_ALIGNED:
2211 case OMP_CLAUSE_NONTEMPORAL:
2212 s = C_OMP_CLAUSE_SPLIT_SIMD;
2213 break;
2214 case OMP_CLAUSE_GRAINSIZE:
2215 case OMP_CLAUSE_NUM_TASKS:
2216 case OMP_CLAUSE_FINAL:
2217 case OMP_CLAUSE_UNTIED:
2218 case OMP_CLAUSE_MERGEABLE:
2219 case OMP_CLAUSE_NOGROUP:
2220 case OMP_CLAUSE_PRIORITY:
2221 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2222 break;
2223 case OMP_CLAUSE_BIND:
2224 s = C_OMP_CLAUSE_SPLIT_LOOP;
2225 break;
2226 case OMP_CLAUSE_FILTER:
2227 s = C_OMP_CLAUSE_SPLIT_MASKED;
2228 break;
2229 /* Duplicate this to all of taskloop, distribute, for, simd and
2230 loop. */
2231 case OMP_CLAUSE_COLLAPSE:
2232 if (code == OMP_SIMD)
2233 {
2234 if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)
2235 | (OMP_CLAUSE_MASK_1
2236 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)
2237 | (OMP_CLAUSE_MASK_1
2238 << PRAGMA_OMP_CLAUSE_NOGROUP))) != 0)
2239 {
2240 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2241 OMP_CLAUSE_COLLAPSE);
2242 OMP_CLAUSE_COLLAPSE_EXPR (c)
2243 = OMP_CLAUSE_COLLAPSE_EXPR (clauses);
2244 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
2245 cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
2246 }
2247 else
2248 {
2249 /* This must be #pragma omp target simd */
2250 s = C_OMP_CLAUSE_SPLIT_SIMD;
2251 break;
2252 }
2253 }
2254 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2255 {
2256 if ((mask & (OMP_CLAUSE_MASK_1
2257 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
2258 {
2259 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2260 OMP_CLAUSE_COLLAPSE);
2261 OMP_CLAUSE_COLLAPSE_EXPR (c)
2262 = OMP_CLAUSE_COLLAPSE_EXPR (clauses);
2263 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
2264 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = c;
2265 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2266 }
2267 else
2268 s = C_OMP_CLAUSE_SPLIT_FOR;
2269 }
2270 else if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
2271 != 0)
2272 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2273 else if (code == OMP_LOOP)
2274 s = C_OMP_CLAUSE_SPLIT_LOOP;
2275 else
2276 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2277 break;
2278 /* Private clause is supported on all constructs but master/masked,
2279 it is enough to put it on the innermost one other than
2280 master/masked. For #pragma omp {for,sections} put it on parallel
2281 though, as that's what we did for OpenMP 3.1. */
2282 case OMP_CLAUSE_PRIVATE:
2283 switch (code)
2284 {
2285 case OMP_SIMD: s = C_OMP_CLAUSE_SPLIT_SIMD; break;
2286 case OMP_FOR: case OMP_SECTIONS:
2287 case OMP_PARALLEL: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
2288 case OMP_DISTRIBUTE: s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE; break;
2289 case OMP_TEAMS: s = C_OMP_CLAUSE_SPLIT_TEAMS; break;
2290 case OMP_MASTER: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
2291 case OMP_MASKED: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
2292 case OMP_TASKLOOP: s = C_OMP_CLAUSE_SPLIT_TASKLOOP; break;
2293 case OMP_LOOP: s = C_OMP_CLAUSE_SPLIT_LOOP; break;
2294 default: gcc_unreachable ();
2295 }
2296 break;
2297 /* Firstprivate clause is supported on all constructs but
2298 simd, master, masked and loop. Put it on the outermost of those
2299 and duplicate on teams and parallel. */
2300 case OMP_CLAUSE_FIRSTPRIVATE:
2301 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
2302 != 0)
2303 {
2304 if (code == OMP_SIMD
2305 && (mask & ((OMP_CLAUSE_MASK_1
2306 << PRAGMA_OMP_CLAUSE_NUM_THREADS)
2307 | (OMP_CLAUSE_MASK_1
2308 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))) == 0)
2309 {
2310 /* This must be #pragma omp target simd. */
2311 s = C_OMP_CLAUSE_SPLIT_TARGET;
2312 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clauses) = 1;
2313 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (clauses) = 1;
2314 break;
2315 }
2316 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2317 OMP_CLAUSE_FIRSTPRIVATE);
2318 /* firstprivate should not be applied to target if it is
2319 also lastprivate or on the combined/composite construct,
2320 or if it is mentioned in map clause. OMP_CLAUSE_DECLs
2321 may need to go through FE handling though (instantiation,
2322 C++ non-static data members, array section lowering), so
2323 add the clause with OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT and
2324 let *finish_omp_clauses and the gimplifier handle it
2325 right. */
2326 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 1;
2327 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2328 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
2329 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
2330 }
2331 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
2332 != 0)
2333 {
2334 if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)
2335 | (OMP_CLAUSE_MASK_1
2336 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE))) != 0)
2337 {
2338 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2339 OMP_CLAUSE_FIRSTPRIVATE);
2340 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2341 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
2342 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] = c;
2343 if ((mask & (OMP_CLAUSE_MASK_1
2344 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) != 0)
2345 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2346 else
2347 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2348 }
2349 else if ((mask & (OMP_CLAUSE_MASK_1
2350 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
2351 /* This must be
2352 #pragma omp parallel mas{ked,ter} taskloop{, simd}. */
2353 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2354 else
2355 /* This must be
2356 #pragma omp parallel{, for{, simd}, sections,loop}
2357 or
2358 #pragma omp target parallel. */
2359 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2360 }
2361 else if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
2362 != 0)
2363 {
2364 /* This must be one of
2365 #pragma omp {,target }teams {distribute,loop}
2366 #pragma omp target teams
2367 #pragma omp {,target }teams distribute simd. */
2368 gcc_assert (code == OMP_DISTRIBUTE
2369 || code == OMP_LOOP
2370 || code == OMP_TEAMS
2371 || code == OMP_SIMD);
2372 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2373 }
2374 else if ((mask & (OMP_CLAUSE_MASK_1
2375 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
2376 {
2377 /* This must be #pragma omp distribute simd. */
2378 gcc_assert (code == OMP_SIMD);
2379 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2380 }
2381 else if ((mask & (OMP_CLAUSE_MASK_1
2382 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
2383 {
2384 /* This must be
2385 #pragma omp {,{,parallel }mas{ked,ter} }taskloop simd
2386 or
2387 #pragma omp {,parallel }mas{ked,ter} taskloop. */
2388 gcc_assert (code == OMP_SIMD || code == OMP_TASKLOOP);
2389 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2390 }
2391 else
2392 {
2393 /* This must be #pragma omp for simd. */
2394 gcc_assert (code == OMP_SIMD);
2395 s = C_OMP_CLAUSE_SPLIT_FOR;
2396 }
2397 break;
2398 /* Lastprivate is allowed on distribute, for, sections, taskloop, loop
2399 and simd. In parallel {for{, simd},sections} we actually want to
2400 put it on parallel rather than for or sections. */
2401 case OMP_CLAUSE_LASTPRIVATE:
2402 if (code == OMP_DISTRIBUTE)
2403 {
2404 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2405 break;
2406 }
2407 if ((mask & (OMP_CLAUSE_MASK_1
2408 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
2409 {
2410 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2411 OMP_CLAUSE_LASTPRIVATE);
2412 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2413 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
2414 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
2415 = OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
2416 cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] = c;
2417 }
2418 if (code == OMP_FOR || code == OMP_SECTIONS)
2419 {
2420 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
2421 != 0)
2422 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2423 else
2424 s = C_OMP_CLAUSE_SPLIT_FOR;
2425 break;
2426 }
2427 if (code == OMP_TASKLOOP)
2428 {
2429 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2430 break;
2431 }
2432 if (code == OMP_LOOP)
2433 {
2434 s = C_OMP_CLAUSE_SPLIT_LOOP;
2435 break;
2436 }
2437 gcc_assert (code == OMP_SIMD);
2438 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2439 {
2440 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2441 OMP_CLAUSE_LASTPRIVATE);
2442 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2443 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
2444 = OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
2445 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
2446 != 0)
2447 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2448 else
2449 s = C_OMP_CLAUSE_SPLIT_FOR;
2450 OMP_CLAUSE_CHAIN (c) = cclauses[s];
2451 cclauses[s] = c;
2452 }
2453 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
2454 {
2455 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2456 OMP_CLAUSE_LASTPRIVATE);
2457 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2458 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
2459 = OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
2460 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
2461 cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
2462 }
2463 s = C_OMP_CLAUSE_SPLIT_SIMD;
2464 break;
2465 /* Shared and default clauses are allowed on parallel, teams and
2466 taskloop. */
2467 case OMP_CLAUSE_SHARED:
2468 case OMP_CLAUSE_DEFAULT:
2469 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
2470 != 0)
2471 {
2472 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
2473 != 0)
2474 {
2475 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2476 OMP_CLAUSE_CODE (clauses));
2477 if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_SHARED)
2478 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2479 else
2480 OMP_CLAUSE_DEFAULT_KIND (c)
2481 = OMP_CLAUSE_DEFAULT_KIND (clauses);
2482 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
2483 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] = c;
2484 }
2485 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2486 break;
2487 }
2488 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
2489 != 0)
2490 {
2491 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
2492 == 0)
2493 {
2494 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2495 break;
2496 }
2497 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2498 OMP_CLAUSE_CODE (clauses));
2499 if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_SHARED)
2500 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2501 else
2502 OMP_CLAUSE_DEFAULT_KIND (c)
2503 = OMP_CLAUSE_DEFAULT_KIND (clauses);
2504 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
2505 cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
2506 }
2507 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2508 break;
2509 /* order clauses are allowed on distribute, for, simd and loop. */
2510 case OMP_CLAUSE_ORDER:
2511 if ((mask & (OMP_CLAUSE_MASK_1
2512 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
2513 {
2514 if (code == OMP_DISTRIBUTE)
2515 {
2516 s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
2517 break;
2518 }
2519 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2520 OMP_CLAUSE_ORDER);
2521 OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
2522 = OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
2523 OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
2524 = OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
2525 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
2526 cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] = c;
2527 }
2528 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2529 {
2530 if (code == OMP_SIMD)
2531 {
2532 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2533 OMP_CLAUSE_ORDER);
2534 OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
2535 = OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
2536 OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
2537 = OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
2538 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
2539 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = c;
2540 s = C_OMP_CLAUSE_SPLIT_SIMD;
2541 }
2542 else
2543 s = C_OMP_CLAUSE_SPLIT_FOR;
2544 }
2545 else if (code == OMP_LOOP)
2546 s = C_OMP_CLAUSE_SPLIT_LOOP;
2547 else
2548 s = C_OMP_CLAUSE_SPLIT_SIMD;
2549 break;
2550 /* Reduction is allowed on simd, for, parallel, sections, taskloop,
2551 teams and loop. Duplicate it on all of them, but omit on for or
2552 sections if parallel is present (unless inscan, in that case
2553 omit on parallel). If taskloop or loop is combined with
2554 parallel, omit it on parallel. */
2555 case OMP_CLAUSE_REDUCTION:
2556 if (OMP_CLAUSE_REDUCTION_TASK (clauses))
2557 {
2558 if (code == OMP_SIMD || code == OMP_LOOP)
2559 {
2560 error_at (OMP_CLAUSE_LOCATION (clauses),
2561 "invalid %<task%> reduction modifier on construct "
2562 "combined with %<simd%> or %<loop%>");
2563 OMP_CLAUSE_REDUCTION_TASK (clauses) = 0;
2564 }
2565 else if (code != OMP_SECTIONS
2566 && (mask & (OMP_CLAUSE_MASK_1
2567 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0
2568 && (mask & (OMP_CLAUSE_MASK_1
2569 << PRAGMA_OMP_CLAUSE_SCHEDULE)) == 0)
2570 {
2571 error_at (OMP_CLAUSE_LOCATION (clauses),
2572 "invalid %<task%> reduction modifier on construct "
2573 "not combined with %<parallel%>, %<for%> or "
2574 "%<sections%>");
2575 OMP_CLAUSE_REDUCTION_TASK (clauses) = 0;
2576 }
2577 }
2578 if (OMP_CLAUSE_REDUCTION_INSCAN (clauses)
2579 && ((mask & ((OMP_CLAUSE_MASK_1
2580 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)
2581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)))
2582 != 0))
2583 {
2584 error_at (OMP_CLAUSE_LOCATION (clauses),
2585 "%<inscan%> %<reduction%> clause on construct other "
2586 "than %<for%>, %<simd%>, %<for simd%>, "
2587 "%<parallel for%>, %<parallel for simd%>");
2588 OMP_CLAUSE_REDUCTION_INSCAN (clauses) = 0;
2589 }
2590 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
2591 {
2592 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2593 OMP_CLAUSE_MAP);
2594 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2595 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
2596 OMP_CLAUSE_MAP_IMPLICIT (c) = 1;
2597 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
2598 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
2599 }
2600 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2601 {
2602 if (code == OMP_SIMD)
2603 {
2604 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2605 OMP_CLAUSE_REDUCTION);
2606 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2607 OMP_CLAUSE_REDUCTION_CODE (c)
2608 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2609 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
2610 = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
2611 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
2612 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
2613 OMP_CLAUSE_REDUCTION_INSCAN (c)
2614 = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
2615 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
2616 cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
2617 }
2618 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
2619 != 0)
2620 {
2621 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2622 OMP_CLAUSE_REDUCTION);
2623 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2624 OMP_CLAUSE_REDUCTION_CODE (c)
2625 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2626 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
2627 = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
2628 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
2629 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
2630 OMP_CLAUSE_REDUCTION_INSCAN (c)
2631 = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
2632 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
2633 cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
2634 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2635 }
2636 else if ((mask & (OMP_CLAUSE_MASK_1
2637 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0
2638 && !OMP_CLAUSE_REDUCTION_INSCAN (clauses))
2639 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2640 else
2641 s = C_OMP_CLAUSE_SPLIT_FOR;
2642 }
2643 else if (code == OMP_SECTIONS
2644 || code == OMP_PARALLEL
2645 || code == OMP_MASTER
2646 || code == OMP_MASKED)
2647 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2648 else if (code == OMP_TASKLOOP)
2649 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2650 else if (code == OMP_LOOP)
2651 s = C_OMP_CLAUSE_SPLIT_LOOP;
2652 else if (code == OMP_SIMD)
2653 {
2654 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
2655 != 0)
2656 {
2657 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2658 OMP_CLAUSE_REDUCTION);
2659 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2660 OMP_CLAUSE_REDUCTION_CODE (c)
2661 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2662 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
2663 = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
2664 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
2665 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
2666 OMP_CLAUSE_REDUCTION_INSCAN (c)
2667 = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
2668 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
2669 cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
2670 }
2671 else if ((mask & (OMP_CLAUSE_MASK_1
2672 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) != 0)
2673 {
2674 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2675 OMP_CLAUSE_REDUCTION);
2676 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2677 OMP_CLAUSE_REDUCTION_CODE (c)
2678 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2679 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
2680 = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
2681 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
2682 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
2683 OMP_CLAUSE_REDUCTION_INSCAN (c)
2684 = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
2685 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
2686 cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
2687 }
2688 s = C_OMP_CLAUSE_SPLIT_SIMD;
2689 }
2690 else
2691 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2692 break;
2693 case OMP_CLAUSE_IN_REDUCTION:
2694 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
2695 {
2696 /* When on target, map(always, tofrom: item) is added as
2697 well. For non-combined target it is added in the FEs. */
2698 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2699 OMP_CLAUSE_MAP);
2700 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2701 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_TOFROM);
2702 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
2703 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
2704 s = C_OMP_CLAUSE_SPLIT_TARGET;
2705 break;
2706 }
2707 /* in_reduction on taskloop simd becomes reduction on the simd
2708 and keeps being in_reduction on taskloop. */
2709 if (code == OMP_SIMD)
2710 {
2711 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2712 OMP_CLAUSE_REDUCTION);
2713 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
2714 OMP_CLAUSE_REDUCTION_CODE (c)
2715 = OMP_CLAUSE_REDUCTION_CODE (clauses);
2716 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
2717 = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
2718 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
2719 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
2720 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
2721 cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
2722 }
2723 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2724 break;
2725 case OMP_CLAUSE_IF:
2726 if (OMP_CLAUSE_IF_MODIFIER (clauses) != ERROR_MARK)
2727 {
2728 s = C_OMP_CLAUSE_SPLIT_COUNT;
2729 switch (OMP_CLAUSE_IF_MODIFIER (clauses))
2730 {
2731 case OMP_PARALLEL:
2732 if ((mask & (OMP_CLAUSE_MASK_1
2733 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
2734 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2735 break;
2736 case OMP_SIMD:
2737 if (code == OMP_SIMD)
2738 s = C_OMP_CLAUSE_SPLIT_SIMD;
2739 break;
2740 case OMP_TASKLOOP:
2741 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
2742 != 0)
2743 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2744 break;
2745 case OMP_TARGET:
2746 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
2747 != 0)
2748 s = C_OMP_CLAUSE_SPLIT_TARGET;
2749 break;
2750 default:
2751 break;
2752 }
2753 if (s != C_OMP_CLAUSE_SPLIT_COUNT)
2754 break;
2755 /* Error-recovery here, invalid if-modifier specified, add the
2756 clause to just one construct. */
2757 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
2758 s = C_OMP_CLAUSE_SPLIT_TARGET;
2759 else if ((mask & (OMP_CLAUSE_MASK_1
2760 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
2761 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2762 else if ((mask & (OMP_CLAUSE_MASK_1
2763 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
2764 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2765 else if (code == OMP_SIMD)
2766 s = C_OMP_CLAUSE_SPLIT_SIMD;
2767 else
2768 gcc_unreachable ();
2769 break;
2770 }
2771 /* Otherwise, duplicate if clause to all constructs. */
2772 if (code == OMP_SIMD)
2773 {
2774 if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)
2775 | (OMP_CLAUSE_MASK_1
2776 << PRAGMA_OMP_CLAUSE_NUM_THREADS)
2777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)))
2778 != 0)
2779 {
2780 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2781 OMP_CLAUSE_IF);
2782 OMP_CLAUSE_IF_MODIFIER (c)
2783 = OMP_CLAUSE_IF_MODIFIER (clauses);
2784 OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
2785 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
2786 cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
2787 }
2788 else
2789 {
2790 s = C_OMP_CLAUSE_SPLIT_SIMD;
2791 break;
2792 }
2793 }
2794 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
2795 != 0)
2796 {
2797 if ((mask & (OMP_CLAUSE_MASK_1
2798 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
2799 {
2800 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2801 OMP_CLAUSE_IF);
2802 OMP_CLAUSE_IF_MODIFIER (c)
2803 = OMP_CLAUSE_IF_MODIFIER (clauses);
2804 OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
2805 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
2806 cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
2807 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2808 }
2809 else
2810 s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
2811 }
2812 else if ((mask & (OMP_CLAUSE_MASK_1
2813 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
2814 {
2815 if ((mask & (OMP_CLAUSE_MASK_1
2816 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
2817 {
2818 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2819 OMP_CLAUSE_IF);
2820 OMP_CLAUSE_IF_MODIFIER (c)
2821 = OMP_CLAUSE_IF_MODIFIER (clauses);
2822 OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
2823 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
2824 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
2825 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2826 }
2827 else
2828 s = C_OMP_CLAUSE_SPLIT_PARALLEL;
2829 }
2830 else
2831 s = C_OMP_CLAUSE_SPLIT_TARGET;
2832 break;
2833 case OMP_CLAUSE_LINEAR:
2834 /* Linear clause is allowed on simd and for. Put it on the
2835 innermost construct. */
2836 if (code == OMP_SIMD)
2837 s = C_OMP_CLAUSE_SPLIT_SIMD;
2838 else
2839 s = C_OMP_CLAUSE_SPLIT_FOR;
2840 break;
2841 case OMP_CLAUSE_NOWAIT:
2842 /* Nowait clause is allowed on target, for and sections, but
2843 is not allowed on parallel for or parallel sections. Therefore,
2844 put it on target construct if present, because that can only
2845 be combined with parallel for{, simd} and not with for{, simd},
2846 otherwise to the worksharing construct. */
2847 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
2848 != 0)
2849 s = C_OMP_CLAUSE_SPLIT_TARGET;
2850 else
2851 s = C_OMP_CLAUSE_SPLIT_FOR;
2852 break;
2853 /* thread_limit is allowed on target and teams. Distribute it
2854 to all. */
2855 case OMP_CLAUSE_THREAD_LIMIT:
2856 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
2857 != 0)
2858 {
2859 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
2860 != 0)
2861 {
2862 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2863 OMP_CLAUSE_THREAD_LIMIT);
2864 OMP_CLAUSE_THREAD_LIMIT_EXPR (c)
2865 = OMP_CLAUSE_THREAD_LIMIT_EXPR (clauses);
2866 OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
2867 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
2868 }
2869 else
2870 {
2871 s = C_OMP_CLAUSE_SPLIT_TARGET;
2872 break;
2873 }
2874 }
2875 s = C_OMP_CLAUSE_SPLIT_TEAMS;
2876 break;
2877 /* Allocate clause is allowed on target, teams, distribute, parallel,
2878 for, sections and taskloop. Distribute it to all. */
2879 case OMP_CLAUSE_ALLOCATE:
2880 s = C_OMP_CLAUSE_SPLIT_COUNT;
2881 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
2882 {
2883 switch (i)
2884 {
2885 case C_OMP_CLAUSE_SPLIT_TARGET:
2886 if ((mask & (OMP_CLAUSE_MASK_1
2887 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
2888 continue;
2889 break;
2890 case C_OMP_CLAUSE_SPLIT_TEAMS:
2891 if ((mask & (OMP_CLAUSE_MASK_1
2892 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) == 0)
2893 continue;
2894 break;
2895 case C_OMP_CLAUSE_SPLIT_DISTRIBUTE:
2896 if ((mask & (OMP_CLAUSE_MASK_1
2897 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
2898 continue;
2899 break;
2900 case C_OMP_CLAUSE_SPLIT_PARALLEL:
2901 if ((mask & (OMP_CLAUSE_MASK_1
2902 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0)
2903 continue;
2904 break;
2905 case C_OMP_CLAUSE_SPLIT_FOR:
2906 STATIC_ASSERT (C_OMP_CLAUSE_SPLIT_SECTIONS
2907 == C_OMP_CLAUSE_SPLIT_FOR
2908 && (C_OMP_CLAUSE_SPLIT_TASKLOOP
2909 == C_OMP_CLAUSE_SPLIT_FOR)
2910 && (C_OMP_CLAUSE_SPLIT_LOOP
2911 == C_OMP_CLAUSE_SPLIT_FOR));
2912 if (code == OMP_SECTIONS)
2913 break;
2914 if ((mask & (OMP_CLAUSE_MASK_1
2915 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
2916 break;
2917 if ((mask & (OMP_CLAUSE_MASK_1
2918 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
2919 break;
2920 continue;
2921 case C_OMP_CLAUSE_SPLIT_SIMD:
2922 continue;
2923 default:
2924 gcc_unreachable ();
2925 }
2926 if (s != C_OMP_CLAUSE_SPLIT_COUNT)
2927 {
2928 c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
2929 OMP_CLAUSE_ALLOCATE);
2930 OMP_CLAUSE_DECL (c)
2931 = OMP_CLAUSE_DECL (clauses);
2932 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
2933 = OMP_CLAUSE_ALLOCATE_ALLOCATOR (clauses);
2934 OMP_CLAUSE_ALLOCATE_ALIGN (c)
2935 = OMP_CLAUSE_ALLOCATE_ALIGN (clauses);
2936 OMP_CLAUSE_CHAIN (c) = cclauses[s];
2937 cclauses[s] = c;
2938 has_dup_allocate = true;
2939 }
2940 s = (enum c_omp_clause_split) i;
2941 }
2942 gcc_assert (s != C_OMP_CLAUSE_SPLIT_COUNT);
2943 break;
2944 default:
2945 gcc_unreachable ();
2946 }
2947 OMP_CLAUSE_CHAIN (clauses) = cclauses[s];
2948 cclauses[s] = clauses;
2949 }
2950
2951 if (has_dup_allocate)
2952 {
2953 bool need_prune = false;
2954 bitmap_obstack_initialize (NULL);
2955 for (i = 0; i < C_OMP_CLAUSE_SPLIT_SIMD - (code == OMP_LOOP); i++)
2956 if (cclauses[i])
2957 {
2958 bitmap_head allocate_head;
2959 bitmap_initialize (&allocate_head, &bitmap_default_obstack);
2960 for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
2961 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
2962 && DECL_P (OMP_CLAUSE_DECL (c)))
2963 bitmap_set_bit (&allocate_head,
2964 DECL_UID (OMP_CLAUSE_DECL (c)));
2965 for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
2966 switch (OMP_CLAUSE_CODE (c))
2967 {
2968 case OMP_CLAUSE_REDUCTION:
2969 case OMP_CLAUSE_IN_REDUCTION:
2970 case OMP_CLAUSE_TASK_REDUCTION:
2971 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
2972 {
2973 tree t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
2974 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2975 t = TREE_OPERAND (t, 0);
2976 if (TREE_CODE (t) == ADDR_EXPR
2977 || INDIRECT_REF_P (t))
2978 t = TREE_OPERAND (t, 0);
2979 if (DECL_P (t))
2980 bitmap_clear_bit (&allocate_head, DECL_UID (t));
2981 break;
2982 }
2983 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST)
2984 {
2985 /* TODO: This can go away once we transition all uses of
2986 TREE_LIST for representing OMP array sections to
2987 OMP_ARRAY_SECTION. */
2988 tree t;
2989 for (t = OMP_CLAUSE_DECL (c);
2990 TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
2991 ;
2992 if (DECL_P (t))
2993 bitmap_clear_bit (&allocate_head, DECL_UID (t));
2994 break;
2995 }
2996 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == OMP_ARRAY_SECTION)
2997 {
2998 tree t;
2999 for (t = OMP_CLAUSE_DECL (c);
3000 TREE_CODE (t) == OMP_ARRAY_SECTION;
3001 t = TREE_OPERAND (t, 0))
3002 ;
3003 if (DECL_P (t))
3004 bitmap_clear_bit (&allocate_head, DECL_UID (t));
3005 break;
3006 }
3007 /* FALLTHRU */
3008 case OMP_CLAUSE_PRIVATE:
3009 case OMP_CLAUSE_FIRSTPRIVATE:
3010 case OMP_CLAUSE_LASTPRIVATE:
3011 case OMP_CLAUSE_LINEAR:
3012 if (DECL_P (OMP_CLAUSE_DECL (c)))
3013 bitmap_clear_bit (&allocate_head,
3014 DECL_UID (OMP_CLAUSE_DECL (c)));
3015 break;
3016 default:
3017 break;
3018 }
3019 for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
3020 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
3021 && DECL_P (OMP_CLAUSE_DECL (c))
3022 && bitmap_bit_p (&allocate_head,
3023 DECL_UID (OMP_CLAUSE_DECL (c))))
3024 {
3025 /* Mark allocate clauses which don't have corresponding
3026 explicit data sharing clause. */
3027 OMP_CLAUSE_ALLOCATE_COMBINED (c) = 1;
3028 need_prune = true;
3029 }
3030 }
3031 bitmap_obstack_release (NULL);
3032 if (need_prune)
3033 {
3034 /* At least one allocate clause has been marked. Walk all the
3035 duplicated allocate clauses in sync. If it is marked in all
3036 constituent constructs, diagnose it as invalid and remove
3037 them. Otherwise, remove all marked inner clauses inside
3038 a construct that doesn't have them marked. Keep the outer
3039 marked ones, because some clause duplication is done only
3040 during gimplification. */
3041 tree *p[C_OMP_CLAUSE_SPLIT_COUNT];
3042 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
3043 if (cclauses[i] == NULL_TREE
3044 || i == C_OMP_CLAUSE_SPLIT_SIMD
3045 || (i == C_OMP_CLAUSE_SPLIT_LOOP && code == OMP_LOOP))
3046 p[i] = NULL;
3047 else
3048 p[i] = &cclauses[i];
3049 do
3050 {
3051 int j = -1;
3052 tree seen = NULL_TREE;
3053 for (i = C_OMP_CLAUSE_SPLIT_COUNT - 1; i >= 0; i--)
3054 if (p[i])
3055 {
3056 while (*p[i]
3057 && OMP_CLAUSE_CODE (*p[i]) != OMP_CLAUSE_ALLOCATE)
3058 p[i] = &OMP_CLAUSE_CHAIN (*p[i]);
3059 if (*p[i] == NULL_TREE)
3060 {
3061 i = C_OMP_CLAUSE_SPLIT_COUNT;
3062 break;
3063 }
3064 if (!OMP_CLAUSE_ALLOCATE_COMBINED (*p[i]) && j == -1)
3065 j = i;
3066 seen = *p[i];
3067 }
3068 if (i == C_OMP_CLAUSE_SPLIT_COUNT)
3069 break;
3070 if (j == -1)
3071 error_at (OMP_CLAUSE_LOCATION (seen),
3072 "%qD specified in %<allocate%> clause but not in "
3073 "an explicit privatization clause",
3074 OMP_CLAUSE_DECL (seen));
3075 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
3076 if (p[i])
3077 {
3078 if (i > j)
3079 /* Remove. */
3080 *p[i] = OMP_CLAUSE_CHAIN (*p[i]);
3081 else
3082 /* Keep. */
3083 p[i] = &OMP_CLAUSE_CHAIN (*p[i]);
3084 }
3085 }
3086 while (1);
3087 }
3088 }
3089
3090 if (!flag_checking)
3091 return;
3092
3093 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
3094 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_TARGET] == NULL_TREE);
3095 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) == 0)
3096 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] == NULL_TREE);
3097 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0
3098 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)) == 0)
3099 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] == NULL_TREE);
3100 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0)
3101 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] == NULL_TREE);
3102 if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)
3103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))) == 0
3104 && code != OMP_SECTIONS
3105 && code != OMP_LOOP)
3106 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_FOR] == NULL_TREE);
3107 if (code != OMP_SIMD)
3108 gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_SIMD] == NULL_TREE);
3109 }
3110
3111
3112 /* qsort callback to compare #pragma omp declare simd clauses. */
3113
3114 static int
3115 c_omp_declare_simd_clause_cmp (const void *p, const void *q)
3116 {
3117 tree a = *(const tree *) p;
3118 tree b = *(const tree *) q;
3119 if (OMP_CLAUSE_CODE (a) != OMP_CLAUSE_CODE (b))
3120 {
3121 if (OMP_CLAUSE_CODE (a) > OMP_CLAUSE_CODE (b))
3122 return -1;
3123 return 1;
3124 }
3125 if (OMP_CLAUSE_CODE (a) != OMP_CLAUSE_SIMDLEN
3126 && OMP_CLAUSE_CODE (a) != OMP_CLAUSE_INBRANCH
3127 && OMP_CLAUSE_CODE (a) != OMP_CLAUSE_NOTINBRANCH)
3128 {
3129 int c = tree_to_shwi (OMP_CLAUSE_DECL (a));
3130 int d = tree_to_shwi (OMP_CLAUSE_DECL (b));
3131 if (c < d)
3132 return 1;
3133 if (c > d)
3134 return -1;
3135 }
3136 return 0;
3137 }
3138
3139 /* Change PARM_DECLs in OMP_CLAUSE_DECL of #pragma omp declare simd
3140 CLAUSES on FNDECL into argument indexes and sort them. */
3141
3142 tree
3143 c_omp_declare_simd_clauses_to_numbers (tree parms, tree clauses)
3144 {
3145 tree c;
3146 vec<tree> clvec = vNULL;
3147
3148 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
3149 {
3150 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SIMDLEN
3151 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_INBRANCH
3152 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_NOTINBRANCH)
3153 {
3154 tree decl = OMP_CLAUSE_DECL (c);
3155 tree arg;
3156 int idx;
3157 for (arg = parms, idx = 0; arg;
3158 arg = TREE_CHAIN (arg), idx++)
3159 if (arg == decl)
3160 break;
3161 if (arg == NULL_TREE)
3162 {
3163 error_at (OMP_CLAUSE_LOCATION (c),
3164 "%qD is not a function argument", decl);
3165 continue;
3166 }
3167 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, idx);
3168 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
3169 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c))
3170 {
3171 decl = OMP_CLAUSE_LINEAR_STEP (c);
3172 for (arg = parms, idx = 0; arg;
3173 arg = TREE_CHAIN (arg), idx++)
3174 if (arg == decl)
3175 break;
3176 if (arg == NULL_TREE)
3177 {
3178 error_at (OMP_CLAUSE_LOCATION (c),
3179 "%qD is not a function argument", decl);
3180 continue;
3181 }
3182 OMP_CLAUSE_LINEAR_STEP (c)
3183 = build_int_cst (integer_type_node, idx);
3184 }
3185 }
3186 clvec.safe_push (c);
3187 }
3188 if (!clvec.is_empty ())
3189 {
3190 unsigned int len = clvec.length (), i;
3191 clvec.qsort (c_omp_declare_simd_clause_cmp);
3192 clauses = clvec[0];
3193 for (i = 0; i < len; i++)
3194 OMP_CLAUSE_CHAIN (clvec[i]) = (i < len - 1) ? clvec[i + 1] : NULL_TREE;
3195 }
3196 else
3197 clauses = NULL_TREE;
3198 clvec.release ();
3199 return clauses;
3200 }
3201
3202 /* Change argument indexes in CLAUSES of FNDECL back to PARM_DECLs. */
3203
3204 void
3205 c_omp_declare_simd_clauses_to_decls (tree fndecl, tree clauses)
3206 {
3207 tree c;
3208
3209 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
3210 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SIMDLEN
3211 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_INBRANCH
3212 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_NOTINBRANCH)
3213 {
3214 int idx = tree_to_shwi (OMP_CLAUSE_DECL (c)), i;
3215 tree arg;
3216 for (arg = DECL_ARGUMENTS (fndecl), i = 0; arg;
3217 arg = TREE_CHAIN (arg), i++)
3218 if (i == idx)
3219 break;
3220 gcc_assert (arg);
3221 OMP_CLAUSE_DECL (c) = arg;
3222 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
3223 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c))
3224 {
3225 idx = tree_to_shwi (OMP_CLAUSE_LINEAR_STEP (c));
3226 for (arg = DECL_ARGUMENTS (fndecl), i = 0; arg;
3227 arg = TREE_CHAIN (arg), i++)
3228 if (i == idx)
3229 break;
3230 gcc_assert (arg);
3231 OMP_CLAUSE_LINEAR_STEP (c) = arg;
3232 }
3233 }
3234 }
3235
3236 /* Return true for __func__ and similar function-local predefined
3237 variables (which are in OpenMP predetermined shared, allowed in
3238 shared/firstprivate clauses). */
3239
3240 bool
3241 c_omp_predefined_variable (tree decl)
3242 {
3243 if (VAR_P (decl)
3244 && DECL_ARTIFICIAL (decl)
3245 && TREE_STATIC (decl)
3246 && DECL_NAME (decl))
3247 {
3248 if (TREE_READONLY (decl)
3249 && (DECL_NAME (decl) == ridpointers[RID_C99_FUNCTION_NAME]
3250 || DECL_NAME (decl) == ridpointers[RID_FUNCTION_NAME]
3251 || DECL_NAME (decl) == ridpointers[RID_PRETTY_FUNCTION_NAME]))
3252 return true;
3253 /* For UBSan handle the same also ubsan_create_data created
3254 variables. There is no magic flag for those, but user variables
3255 shouldn't be DECL_ARTIFICIAL or have TYPE_ARTIFICIAL type with
3256 such names. */
3257 if ((flag_sanitize & (SANITIZE_UNDEFINED
3258 | SANITIZE_UNDEFINED_NONDEFAULT)) != 0
3259 && DECL_IGNORED_P (decl)
3260 && !TREE_READONLY (decl)
3261 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
3262 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
3263 && TYPE_ARTIFICIAL (TREE_TYPE (decl))
3264 && TYPE_NAME (TREE_TYPE (decl))
3265 && TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) == TYPE_DECL
3266 && DECL_NAME (TYPE_NAME (TREE_TYPE (decl)))
3267 && (TREE_CODE (DECL_NAME (TYPE_NAME (TREE_TYPE (decl))))
3268 == IDENTIFIER_NODE))
3269 {
3270 tree id1 = DECL_NAME (decl);
3271 tree id2 = DECL_NAME (TYPE_NAME (TREE_TYPE (decl)));
3272 if (IDENTIFIER_LENGTH (id1) >= sizeof ("ubsan_data") - 1
3273 && IDENTIFIER_LENGTH (id2) >= sizeof ("__ubsan__data")
3274 && !memcmp (IDENTIFIER_POINTER (id2), "__ubsan_",
3275 sizeof ("__ubsan_") - 1)
3276 && !memcmp (IDENTIFIER_POINTER (id2) + IDENTIFIER_LENGTH (id2)
3277 - sizeof ("_data") + 1, "_data",
3278 sizeof ("_data") - 1)
3279 && strstr (IDENTIFIER_POINTER (id1), "ubsan_data"))
3280 return true;
3281 }
3282 }
3283 return false;
3284 }
3285
3286 /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute of DECL
3287 is predetermined. */
3288
3289 enum omp_clause_default_kind
3290 c_omp_predetermined_sharing (tree decl)
3291 {
3292 /* Predetermine artificial variables holding integral values, those
3293 are usually result of gimplify_one_sizepos or SAVE_EXPR
3294 gimplification. */
3295 if (VAR_P (decl)
3296 && DECL_ARTIFICIAL (decl)
3297 && INTEGRAL_TYPE_P (TREE_TYPE (decl)))
3298 return OMP_CLAUSE_DEFAULT_SHARED;
3299
3300 if (c_omp_predefined_variable (decl))
3301 return OMP_CLAUSE_DEFAULT_SHARED;
3302
3303 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
3304 }
3305
3306 /* OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED unless OpenMP mapping attribute
3307 of DECL is predetermined. */
3308
3309 enum omp_clause_defaultmap_kind
3310 c_omp_predetermined_mapping (tree decl)
3311 {
3312 /* Predetermine artificial variables holding integral values, those
3313 are usually result of gimplify_one_sizepos or SAVE_EXPR
3314 gimplification. */
3315 if (VAR_P (decl)
3316 && DECL_ARTIFICIAL (decl)
3317 && INTEGRAL_TYPE_P (TREE_TYPE (decl)))
3318 return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
3319
3320 if (c_omp_predefined_variable (decl))
3321 return OMP_CLAUSE_DEFAULTMAP_TO;
3322
3323 return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
3324 }
3325
3326
3327 /* Used to merge map clause information in c_omp_adjust_map_clauses. */
3328 struct map_clause
3329 {
3330 tree clause;
3331 bool firstprivate_ptr_p;
3332 bool decl_mapped;
3333 bool omp_declare_target;
3334 map_clause (void) : clause (NULL_TREE), firstprivate_ptr_p (false),
3335 decl_mapped (false), omp_declare_target (false) { }
3336 };
3337
3338 /* Adjust map clauses after normal clause parsing, mainly to mark specific
3339 base-pointer map cases addressable that may be turned into attach/detach
3340 operations during gimplification. */
3341 void
3342 c_omp_adjust_map_clauses (tree clauses, bool is_target)
3343 {
3344 if (!is_target)
3345 {
3346 /* If this is not a target construct, just turn firstprivate pointers
3347 into attach/detach, the runtime will check and do the rest. */
3348
3349 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
3350 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3351 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
3352 && DECL_P (OMP_CLAUSE_DECL (c))
3353 && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
3354 {
3355 tree ptr = OMP_CLAUSE_DECL (c);
3356 c_common_mark_addressable_vec (ptr);
3357 }
3358 return;
3359 }
3360
3361 hash_map<tree, map_clause> maps;
3362
3363 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
3364 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3365 && DECL_P (OMP_CLAUSE_DECL (c)))
3366 {
3367 /* If this is for a target construct, the firstprivate pointer
3368 is marked addressable if either is true:
3369 (1) the base-pointer is mapped in this same construct, or
3370 (2) the base-pointer is a variable place on the device by
3371 "declare target" directives.
3372
3373 Here we iterate through all map clauses collecting these cases,
3374 and merge them with a hash_map to process below. */
3375
3376 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
3377 && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
3378 {
3379 tree ptr = OMP_CLAUSE_DECL (c);
3380 map_clause &mc = maps.get_or_insert (ptr);
3381 if (mc.clause == NULL_TREE)
3382 mc.clause = c;
3383 mc.firstprivate_ptr_p = true;
3384
3385 if (is_global_var (ptr)
3386 && lookup_attribute ("omp declare target",
3387 DECL_ATTRIBUTES (ptr)))
3388 mc.omp_declare_target = true;
3389 }
3390 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALLOC
3391 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO
3392 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FROM
3393 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TOFROM
3394 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
3395 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_FROM
3396 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
3397 {
3398 map_clause &mc = maps.get_or_insert (OMP_CLAUSE_DECL (c));
3399 mc.decl_mapped = true;
3400 }
3401 }
3402
3403 for (hash_map<tree, map_clause>::iterator i = maps.begin ();
3404 i != maps.end (); ++i)
3405 {
3406 map_clause &mc = (*i).second;
3407
3408 if (mc.firstprivate_ptr_p
3409 && (mc.decl_mapped || mc.omp_declare_target))
3410 c_common_mark_addressable_vec (OMP_CLAUSE_DECL (mc.clause));
3411 }
3412 }
3413
3414 /* Maybe strip off an indirection from a "converted" reference, then find the
3415 origin of a pointer (i.e. without any offset). */
3416
3417 tree
3418 c_omp_address_inspector::unconverted_ref_origin ()
3419 {
3420 tree t = orig;
3421
3422 /* We may have a reference-typed component access at the outermost level
3423 that has had convert_from_reference called on it. Get the un-dereferenced
3424 reference itself. */
3425 t = maybe_unconvert_ref (t);
3426
3427 /* Find base pointer for POINTER_PLUS_EXPR, etc. */
3428 t = get_origin (t);
3429
3430 return t;
3431 }
3432
3433 /* Return TRUE if the address is a component access. */
3434
3435 bool
3436 c_omp_address_inspector::component_access_p ()
3437 {
3438 tree t = maybe_unconvert_ref (orig);
3439
3440 t = get_origin (t);
3441
3442 return TREE_CODE (t) == COMPONENT_REF;
3443 }
3444
3445 /* Perform various checks on the address, as described by clause CLAUSE (we
3446 only use its code and location here). */
3447
3448 bool
3449 c_omp_address_inspector::check_clause (tree clause)
3450 {
3451 tree t = unconverted_ref_origin ();
3452
3453 if (TREE_CODE (t) != COMPONENT_REF)
3454 return true;
3455
3456 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
3457 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
3458 {
3459 error_at (OMP_CLAUSE_LOCATION (clause),
3460 "bit-field %qE in %qs clause",
3461 t, omp_clause_code_name[OMP_CLAUSE_CODE (clause)]);
3462 return false;
3463 }
3464 else if (!processing_template_decl_p ()
3465 && !omp_mappable_type (TREE_TYPE (t)))
3466 {
3467 error_at (OMP_CLAUSE_LOCATION (clause),
3468 "%qE does not have a mappable type in %qs clause",
3469 t, omp_clause_code_name[OMP_CLAUSE_CODE (clause)]);
3470 emit_unmappable_type_notes (TREE_TYPE (t));
3471 return false;
3472 }
3473 else if (TREE_TYPE (t) && TYPE_ATOMIC (TREE_TYPE (t)))
3474 {
3475 error_at (OMP_CLAUSE_LOCATION (clause),
3476 "%<_Atomic%> %qE in %qs clause", t,
3477 omp_clause_code_name[OMP_CLAUSE_CODE (clause)]);
3478 return false;
3479 }
3480
3481 return true;
3482 }
3483
3484 /* Find the "root term" for the address. This is the innermost decl, etc.
3485 of the access. */
3486
3487 tree
3488 c_omp_address_inspector::get_root_term (bool checking)
3489 {
3490 if (root_term && !checking)
3491 return root_term;
3492
3493 tree t = unconverted_ref_origin ();
3494
3495 while (TREE_CODE (t) == COMPONENT_REF)
3496 {
3497 if (checking
3498 && TREE_TYPE (TREE_OPERAND (t, 0))
3499 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
3500 {
3501 error_at (loc, "%qE is a member of a union", t);
3502 return error_mark_node;
3503 }
3504 t = TREE_OPERAND (t, 0);
3505 while (TREE_CODE (t) == MEM_REF
3506 || TREE_CODE (t) == INDIRECT_REF
3507 || TREE_CODE (t) == ARRAY_REF)
3508 {
3509 if (TREE_CODE (t) == MEM_REF
3510 || TREE_CODE (t) == INDIRECT_REF)
3511 indirections = true;
3512 t = TREE_OPERAND (t, 0);
3513 STRIP_NOPS (t);
3514 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
3515 t = TREE_OPERAND (t, 0);
3516 }
3517 }
3518
3519 root_term = t;
3520
3521 return t;
3522 }
3523
3524 /* Return TRUE if the address is supported in mapping clauses. At present,
3525 this means that the innermost expression is a DECL_P, but could be extended
3526 to other types of expression in the future. */
3527
3528 bool
3529 c_omp_address_inspector::map_supported_p ()
3530 {
3531 /* If we've already decided if the mapped address is supported, return
3532 that. */
3533 if (map_supported != -1)
3534 return map_supported;
3535
3536 tree t = unconverted_ref_origin ();
3537
3538 STRIP_NOPS (t);
3539
3540 while (TREE_CODE (t) == INDIRECT_REF
3541 || TREE_CODE (t) == MEM_REF
3542 || TREE_CODE (t) == ARRAY_REF
3543 || TREE_CODE (t) == COMPONENT_REF
3544 || TREE_CODE (t) == COMPOUND_EXPR
3545 || TREE_CODE (t) == SAVE_EXPR
3546 || TREE_CODE (t) == POINTER_PLUS_EXPR
3547 || TREE_CODE (t) == NON_LVALUE_EXPR
3548 || TREE_CODE (t) == OMP_ARRAY_SECTION
3549 || TREE_CODE (t) == NOP_EXPR)
3550 if (TREE_CODE (t) == COMPOUND_EXPR)
3551 t = TREE_OPERAND (t, 1);
3552 else
3553 t = TREE_OPERAND (t, 0);
3554
3555 STRIP_NOPS (t);
3556
3557 map_supported = DECL_P (t);
3558
3559 return map_supported;
3560 }
3561
3562 /* Get the origin of an address T, stripping off offsets and some other
3563 bits. */
3564
3565 tree
3566 c_omp_address_inspector::get_origin (tree t)
3567 {
3568 while (1)
3569 {
3570 if (TREE_CODE (t) == COMPOUND_EXPR)
3571 {
3572 t = TREE_OPERAND (t, 1);
3573 STRIP_NOPS (t);
3574 }
3575 else if (TREE_CODE (t) == POINTER_PLUS_EXPR
3576 || TREE_CODE (t) == SAVE_EXPR)
3577 t = TREE_OPERAND (t, 0);
3578 else if (!processing_template_decl_p ()
3579 && TREE_CODE (t) == INDIRECT_REF
3580 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == REFERENCE_TYPE)
3581 t = TREE_OPERAND (t, 0);
3582 else
3583 break;
3584 }
3585 STRIP_NOPS (t);
3586 return t;
3587 }
3588
3589 /* For an address T that might be a reference that has had
3590 "convert_from_reference" called on it, return the actual reference without
3591 any indirection. */
3592
3593 tree
3594 c_omp_address_inspector::maybe_unconvert_ref (tree t)
3595 {
3596 /* Be careful not to dereference the type if we're processing a
3597 template decl, else it might be NULL. */
3598 if (!processing_template_decl_p ()
3599 && TREE_CODE (t) == INDIRECT_REF
3600 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == REFERENCE_TYPE)
3601 return TREE_OPERAND (t, 0);
3602
3603 return t;
3604 }
3605
3606 /* Return TRUE if CLAUSE might describe a zero-length array section. */
3607
3608 bool
3609 c_omp_address_inspector::maybe_zero_length_array_section (tree clause)
3610 {
3611 switch (OMP_CLAUSE_MAP_KIND (clause))
3612 {
3613 case GOMP_MAP_ALLOC:
3614 case GOMP_MAP_IF_PRESENT:
3615 case GOMP_MAP_TO:
3616 case GOMP_MAP_FROM:
3617 case GOMP_MAP_TOFROM:
3618 case GOMP_MAP_ALWAYS_TO:
3619 case GOMP_MAP_ALWAYS_FROM:
3620 case GOMP_MAP_ALWAYS_TOFROM:
3621 case GOMP_MAP_PRESENT_ALLOC:
3622 case GOMP_MAP_PRESENT_TO:
3623 case GOMP_MAP_PRESENT_FROM:
3624 case GOMP_MAP_PRESENT_TOFROM:
3625 case GOMP_MAP_ALWAYS_PRESENT_TO:
3626 case GOMP_MAP_ALWAYS_PRESENT_FROM:
3627 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
3628 case GOMP_MAP_RELEASE:
3629 case GOMP_MAP_DELETE:
3630 case GOMP_MAP_FORCE_TO:
3631 case GOMP_MAP_FORCE_FROM:
3632 case GOMP_MAP_FORCE_TOFROM:
3633 case GOMP_MAP_FORCE_PRESENT:
3634 return true;
3635 default:
3636 return false;
3637 }
3638 }
3639
3640 /* Expand a chained access. We only expect to see a quite limited range of
3641 expression types here, because e.g. you can't have an array of
3642 references. */
3643
3644 static tree
3645 omp_expand_access_chain (tree c, tree expr, vec<omp_addr_token *> &addr_tokens,
3646 unsigned *idx, c_omp_region_type ort)
3647 {
3648 using namespace omp_addr_tokenizer;
3649 location_t loc = OMP_CLAUSE_LOCATION (c);
3650 unsigned i = *idx;
3651 tree c2 = NULL_TREE;
3652 gomp_map_kind kind;
3653
3654 if ((ort & C_ORT_EXIT_DATA) != 0
3655 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM
3656 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
3657 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FROM
3658 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE
3659 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
3660 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_FROM
3661 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_FROM
3662 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PRESENT_FROM
3663 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_FROM)))
3664 kind = GOMP_MAP_DETACH;
3665 else
3666 kind = GOMP_MAP_ATTACH;
3667
3668 switch (addr_tokens[i]->u.access_kind)
3669 {
3670 case ACCESS_POINTER:
3671 case ACCESS_POINTER_OFFSET:
3672 {
3673 tree virtual_origin
3674 = fold_convert_loc (loc, ptrdiff_type_node, addr_tokens[i]->expr);
3675 tree data_addr = omp_accessed_addr (addr_tokens, i, expr);
3676 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3677 OMP_CLAUSE_SET_MAP_KIND (c2, kind);
3678 OMP_CLAUSE_DECL (c2) = addr_tokens[i]->expr;
3679 OMP_CLAUSE_SIZE (c2)
3680 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
3681 fold_convert_loc (loc, ptrdiff_type_node,
3682 data_addr),
3683 virtual_origin);
3684 }
3685 break;
3686
3687 case ACCESS_INDEXED_ARRAY:
3688 break;
3689
3690 default:
3691 return error_mark_node;
3692 }
3693
3694 if (c2)
3695 {
3696 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
3697 OMP_CLAUSE_CHAIN (c) = c2;
3698 c = c2;
3699 }
3700
3701 *idx = ++i;
3702
3703 if (i < addr_tokens.length ()
3704 && addr_tokens[i]->type == ACCESS_METHOD)
3705 return omp_expand_access_chain (c, expr, addr_tokens, idx, ort);
3706
3707 return c;
3708 }
3709
3710 /* Translate "array_base_decl access_method" to OMP mapping clauses. */
3711
3712 tree
3713 c_omp_address_inspector::expand_array_base (tree c,
3714 vec<omp_addr_token *> &addr_tokens,
3715 tree expr, unsigned *idx,
3716 c_omp_region_type ort)
3717 {
3718 using namespace omp_addr_tokenizer;
3719 location_t loc = OMP_CLAUSE_LOCATION (c);
3720 int i = *idx;
3721 tree decl = addr_tokens[i + 1]->expr;
3722 bool decl_p = DECL_P (decl);
3723 bool declare_target_p = (decl_p
3724 && is_global_var (decl)
3725 && lookup_attribute ("omp declare target",
3726 DECL_ATTRIBUTES (decl)));
3727 bool map_p = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP;
3728 bool implicit_p = map_p && OMP_CLAUSE_MAP_IMPLICIT (c);
3729 bool chain_p = omp_access_chain_p (addr_tokens, i + 1);
3730 tree c2 = NULL_TREE, c3 = NULL_TREE;
3731 unsigned consume_tokens = 2;
3732 bool target_p = (ort & C_ORT_TARGET) != 0;
3733 bool openmp_p = (ort & C_ORT_OMP) != 0;
3734
3735 gcc_assert (i == 0);
3736
3737 if (!openmp_p
3738 && map_p
3739 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
3740 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
3741 {
3742 i += 2;
3743 *idx = i;
3744 return c;
3745 }
3746
3747 switch (addr_tokens[i + 1]->u.access_kind)
3748 {
3749 case ACCESS_DIRECT:
3750 if (decl_p && !target_p)
3751 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3752 break;
3753
3754 case ACCESS_REF:
3755 {
3756 /* Copy the referenced object. Note that we do this even for !MAP_P
3757 clauses. */
3758 tree obj = convert_from_reference (addr_tokens[i + 1]->expr);
3759 if (TREE_CODE (TREE_TYPE (obj)) == ARRAY_TYPE)
3760 /* We have a ref to array: add a [0] element as the ME expects. */
3761 OMP_CLAUSE_DECL (c) = build_array_ref (loc, obj, integer_zero_node);
3762 else
3763 OMP_CLAUSE_DECL (c) = obj;
3764 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (obj));
3765
3766 if (!map_p)
3767 {
3768 if (decl_p)
3769 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3770 break;
3771 }
3772
3773 if (!target_p)
3774 break;
3775
3776 /* If we have a reference to a pointer, avoid using
3777 FIRSTPRIVATE_REFERENCE here in case the pointer is modified in the
3778 offload region (we can only do that if the pointer does not point
3779 to a mapped block). We could avoid doing this if we don't have a
3780 FROM mapping... */
3781 bool ref_to_ptr = TREE_CODE (TREE_TYPE (obj)) == POINTER_TYPE;
3782
3783 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3784 if (!ref_to_ptr
3785 && !declare_target_p
3786 && decl_p)
3787 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
3788 else
3789 {
3790 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
3791 if (decl_p)
3792 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3793 }
3794 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3795 OMP_CLAUSE_SIZE (c2) = size_zero_node;
3796
3797 if (ref_to_ptr)
3798 {
3799 c3 = c2;
3800 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3801 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALLOC);
3802 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3803 OMP_CLAUSE_SIZE (c2)
3804 = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (c2)));
3805 }
3806 }
3807 break;
3808
3809 case ACCESS_INDEXED_REF_TO_ARRAY:
3810 {
3811 if (!map_p)
3812 {
3813 if (decl_p)
3814 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3815 break;
3816 }
3817
3818 if (!target_p)
3819 break;
3820
3821 tree virtual_origin
3822 = convert_from_reference (addr_tokens[i + 1]->expr);
3823 virtual_origin = build_fold_addr_expr (virtual_origin);
3824 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3825 virtual_origin);
3826 tree data_addr = omp_accessed_addr (addr_tokens, i + 1, expr);
3827 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3828 if (decl_p && target_p && !declare_target_p)
3829 {
3830 /* It appears that omp-low.cc mishandles cases where we have a
3831 [reference to an] array of pointers such as:
3832
3833 int *arr[N]; (or "int *(&arr)[N] = ...")
3834 #pragma omp target map(arr[a][b:c])
3835 { ... }
3836
3837 in such cases chain_p will be true. For now, fall back to
3838 GOMP_MAP_POINTER. */
3839 enum gomp_map_kind k = chain_p ? GOMP_MAP_POINTER
3840 : GOMP_MAP_FIRSTPRIVATE_REFERENCE;
3841 OMP_CLAUSE_SET_MAP_KIND (c2, k);
3842 }
3843 else
3844 {
3845 if (decl_p)
3846 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3847 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
3848 }
3849 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3850 OMP_CLAUSE_SIZE (c2)
3851 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
3852 fold_convert_loc (loc, ptrdiff_type_node,
3853 data_addr),
3854 virtual_origin);
3855 }
3856 break;
3857
3858 case ACCESS_INDEXED_ARRAY:
3859 {
3860 if (!map_p)
3861 {
3862 if (decl_p)
3863 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3864 break;
3865 }
3866
3867 /* The code handling "firstprivatize_array_bases" in gimplify.cc is
3868 relevant here. What do we need to create for arrays at this
3869 stage? (This condition doesn't feel quite right. FIXME?) */
3870 if (!target_p
3871 && (TREE_CODE (TREE_TYPE (addr_tokens[i + 1]->expr))
3872 == ARRAY_TYPE))
3873 break;
3874
3875 tree virtual_origin
3876 = build_fold_addr_expr (addr_tokens[i + 1]->expr);
3877 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3878 virtual_origin);
3879 tree data_addr = omp_accessed_addr (addr_tokens, i + 1, expr);
3880 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3881 if (decl_p && target_p)
3882 {
3883 /* See comment for ACCESS_INDEXED_REF_TO_ARRAY above. */
3884 enum gomp_map_kind k = chain_p ? GOMP_MAP_POINTER
3885 : GOMP_MAP_FIRSTPRIVATE_POINTER;
3886 OMP_CLAUSE_SET_MAP_KIND (c2, k);
3887 }
3888 else
3889 {
3890 if (decl_p)
3891 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3892 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
3893 }
3894 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3895 OMP_CLAUSE_SIZE (c2)
3896 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
3897 fold_convert_loc (loc, ptrdiff_type_node,
3898 data_addr),
3899 virtual_origin);
3900 }
3901 break;
3902
3903 case ACCESS_POINTER:
3904 case ACCESS_POINTER_OFFSET:
3905 {
3906 if (!map_p)
3907 {
3908 if (decl_p)
3909 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3910 break;
3911 }
3912
3913 unsigned last_access = i + 1;
3914 tree virtual_origin;
3915
3916 if (chain_p
3917 && addr_tokens[i + 2]->type == ACCESS_METHOD
3918 && addr_tokens[i + 2]->u.access_kind == ACCESS_INDEXED_ARRAY)
3919 {
3920 /* !!! This seems wrong for ACCESS_POINTER_OFFSET. */
3921 consume_tokens = 3;
3922 chain_p = omp_access_chain_p (addr_tokens, i + 2);
3923 last_access = i + 2;
3924 virtual_origin
3925 = build_array_ref (loc, addr_tokens[last_access]->expr,
3926 integer_zero_node);
3927 virtual_origin = build_fold_addr_expr (virtual_origin);
3928 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3929 virtual_origin);
3930 }
3931 else
3932 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3933 addr_tokens[last_access]->expr);
3934 tree data_addr = omp_accessed_addr (addr_tokens, last_access, expr);
3935 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3936 /* For OpenACC, use FIRSTPRIVATE_POINTER for decls even on non-compute
3937 regions (e.g. "acc data" constructs). It'll be removed anyway in
3938 gimplify.cc, but doing it this way maintains diagnostic
3939 behaviour. */
3940 if (decl_p && (target_p || !openmp_p) && !chain_p && !declare_target_p)
3941 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
3942 else
3943 {
3944 if (decl_p)
3945 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3946 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
3947 }
3948 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3949 OMP_CLAUSE_SIZE (c2)
3950 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
3951 fold_convert_loc (loc, ptrdiff_type_node,
3952 data_addr),
3953 virtual_origin);
3954 }
3955 break;
3956
3957 case ACCESS_REF_TO_POINTER:
3958 case ACCESS_REF_TO_POINTER_OFFSET:
3959 {
3960 if (!map_p)
3961 {
3962 if (decl_p)
3963 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
3964 break;
3965 }
3966
3967 unsigned last_access = i + 1;
3968 tree virtual_origin;
3969
3970 if (chain_p
3971 && addr_tokens[i + 2]->type == ACCESS_METHOD
3972 && addr_tokens[i + 2]->u.access_kind == ACCESS_INDEXED_ARRAY)
3973 {
3974 /* !!! This seems wrong for ACCESS_POINTER_OFFSET. */
3975 consume_tokens = 3;
3976 chain_p = omp_access_chain_p (addr_tokens, i + 2);
3977 last_access = i + 2;
3978 virtual_origin
3979 = build_array_ref (loc, addr_tokens[last_access]->expr,
3980 integer_zero_node);
3981 virtual_origin = build_fold_addr_expr (virtual_origin);
3982 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3983 virtual_origin);
3984 }
3985 else
3986 {
3987 virtual_origin
3988 = convert_from_reference (addr_tokens[last_access]->expr);
3989 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
3990 virtual_origin);
3991 }
3992
3993 tree data_addr = omp_accessed_addr (addr_tokens, last_access, expr);
3994 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
3995 if (decl_p && target_p && !chain_p && !declare_target_p)
3996 {
3997 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
3998 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
3999 }
4000 else
4001 {
4002 if (decl_p)
4003 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
4004 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
4005 OMP_CLAUSE_DECL (c2)
4006 = convert_from_reference (addr_tokens[i + 1]->expr);
4007 }
4008 OMP_CLAUSE_SIZE (c2)
4009 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
4010 fold_convert_loc (loc, ptrdiff_type_node,
4011 data_addr),
4012 virtual_origin);
4013 }
4014 break;
4015
4016 default:
4017 *idx = i + consume_tokens;
4018 return error_mark_node;
4019 }
4020
4021 if (c3)
4022 {
4023 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c);
4024 OMP_CLAUSE_CHAIN (c2) = c3;
4025 OMP_CLAUSE_CHAIN (c) = c2;
4026 if (implicit_p)
4027 {
4028 OMP_CLAUSE_MAP_IMPLICIT (c2) = 1;
4029 OMP_CLAUSE_MAP_IMPLICIT (c3) = 1;
4030 }
4031 c = c3;
4032 }
4033 else if (c2)
4034 {
4035 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4036 OMP_CLAUSE_CHAIN (c) = c2;
4037 if (implicit_p)
4038 OMP_CLAUSE_MAP_IMPLICIT (c2) = 1;
4039 c = c2;
4040 }
4041
4042 i += consume_tokens;
4043 *idx = i;
4044
4045 if (chain_p && map_p)
4046 return omp_expand_access_chain (c, expr, addr_tokens, idx, ort);
4047
4048 return c;
4049 }
4050
4051 /* Translate "component_selector access_method" to OMP mapping clauses. */
4052
4053 tree
4054 c_omp_address_inspector::expand_component_selector (tree c,
4055 vec<omp_addr_token *>
4056 &addr_tokens,
4057 tree expr, unsigned *idx,
4058 c_omp_region_type ort)
4059 {
4060 using namespace omp_addr_tokenizer;
4061 location_t loc = OMP_CLAUSE_LOCATION (c);
4062 unsigned i = *idx;
4063 tree c2 = NULL_TREE, c3 = NULL_TREE;
4064 bool chain_p = omp_access_chain_p (addr_tokens, i + 1);
4065 bool map_p = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP;
4066
4067 switch (addr_tokens[i + 1]->u.access_kind)
4068 {
4069 case ACCESS_DIRECT:
4070 case ACCESS_INDEXED_ARRAY:
4071 break;
4072
4073 case ACCESS_REF:
4074 {
4075 /* Copy the referenced object. Note that we also do this for !MAP_P
4076 clauses. */
4077 tree obj = convert_from_reference (addr_tokens[i + 1]->expr);
4078 OMP_CLAUSE_DECL (c) = obj;
4079 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (obj));
4080
4081 if (!map_p)
4082 break;
4083
4084 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
4085 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
4086 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
4087 OMP_CLAUSE_SIZE (c2) = size_zero_node;
4088 }
4089 break;
4090
4091 case ACCESS_INDEXED_REF_TO_ARRAY:
4092 {
4093 if (!map_p)
4094 break;
4095
4096 tree virtual_origin
4097 = convert_from_reference (addr_tokens[i + 1]->expr);
4098 virtual_origin = build_fold_addr_expr (virtual_origin);
4099 virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
4100 virtual_origin);
4101 tree data_addr = omp_accessed_addr (addr_tokens, i + 1, expr);
4102
4103 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
4104 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
4105 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
4106 OMP_CLAUSE_SIZE (c2)
4107 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
4108 fold_convert_loc (loc, ptrdiff_type_node,
4109 data_addr),
4110 virtual_origin);
4111 }
4112 break;
4113
4114 case ACCESS_POINTER:
4115 case ACCESS_POINTER_OFFSET:
4116 {
4117 if (!map_p)
4118 break;
4119
4120 tree virtual_origin
4121 = fold_convert_loc (loc, ptrdiff_type_node,
4122 addr_tokens[i + 1]->expr);
4123 tree data_addr = omp_accessed_addr (addr_tokens, i + 1, expr);
4124
4125 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
4126 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
4127 OMP_CLAUSE_DECL (c2) = addr_tokens[i + 1]->expr;
4128 OMP_CLAUSE_SIZE (c2)
4129 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
4130 fold_convert_loc (loc, ptrdiff_type_node,
4131 data_addr),
4132 virtual_origin);
4133 }
4134 break;
4135
4136 case ACCESS_REF_TO_POINTER:
4137 case ACCESS_REF_TO_POINTER_OFFSET:
4138 {
4139 if (!map_p)
4140 break;
4141
4142 tree ptr = convert_from_reference (addr_tokens[i + 1]->expr);
4143 tree virtual_origin = fold_convert_loc (loc, ptrdiff_type_node,
4144 ptr);
4145 tree data_addr = omp_accessed_addr (addr_tokens, i + 1, expr);
4146
4147 /* Attach the pointer... */
4148 c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
4149 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
4150 OMP_CLAUSE_DECL (c2) = ptr;
4151 OMP_CLAUSE_SIZE (c2)
4152 = fold_build2_loc (loc, MINUS_EXPR, ptrdiff_type_node,
4153 fold_convert_loc (loc, ptrdiff_type_node,
4154 data_addr),
4155 virtual_origin);
4156
4157 /* ...and also the reference. */
4158 c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
4159 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH_DETACH);
4160 OMP_CLAUSE_DECL (c3) = addr_tokens[i + 1]->expr;
4161 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4162 }
4163 break;
4164
4165 default:
4166 *idx = i + 2;
4167 return error_mark_node;
4168 }
4169
4170 if (c3)
4171 {
4172 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c);
4173 OMP_CLAUSE_CHAIN (c2) = c3;
4174 OMP_CLAUSE_CHAIN (c) = c2;
4175 c = c3;
4176 }
4177 else if (c2)
4178 {
4179 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4180 OMP_CLAUSE_CHAIN (c) = c2;
4181 c = c2;
4182 }
4183
4184 i += 2;
4185 *idx = i;
4186
4187 if (chain_p && map_p)
4188 return omp_expand_access_chain (c, expr, addr_tokens, idx, ort);
4189
4190 return c;
4191 }
4192
4193 /* Expand a map clause into a group of mapping clauses, creating nodes to
4194 attach/detach pointers and so forth as necessary. */
4195
4196 tree
4197 c_omp_address_inspector::expand_map_clause (tree c, tree expr,
4198 vec<omp_addr_token *> &addr_tokens,
4199 c_omp_region_type ort)
4200 {
4201 using namespace omp_addr_tokenizer;
4202 unsigned i, length = addr_tokens.length ();
4203
4204 for (i = 0; i < length;)
4205 {
4206 int remaining = length - i;
4207
4208 if (remaining >= 2
4209 && addr_tokens[i]->type == ARRAY_BASE
4210 && addr_tokens[i]->u.structure_base_kind == BASE_DECL
4211 && addr_tokens[i + 1]->type == ACCESS_METHOD)
4212 {
4213 c = expand_array_base (c, addr_tokens, expr, &i, ort);
4214 if (c == error_mark_node)
4215 return error_mark_node;
4216 }
4217 else if (remaining >= 2
4218 && addr_tokens[i]->type == ARRAY_BASE
4219 && addr_tokens[i]->u.structure_base_kind == BASE_ARBITRARY_EXPR
4220 && addr_tokens[i + 1]->type == ACCESS_METHOD)
4221 {
4222 c = expand_array_base (c, addr_tokens, expr, &i, ort);
4223 if (c == error_mark_node)
4224 return error_mark_node;
4225 }
4226 else if (remaining >= 2
4227 && addr_tokens[i]->type == STRUCTURE_BASE
4228 && addr_tokens[i]->u.structure_base_kind == BASE_DECL
4229 && addr_tokens[i + 1]->type == ACCESS_METHOD)
4230 {
4231 if (addr_tokens[i + 1]->u.access_kind == ACCESS_DIRECT)
4232 c_common_mark_addressable_vec (addr_tokens[i + 1]->expr);
4233 i += 2;
4234 while (addr_tokens[i]->type == ACCESS_METHOD)
4235 i++;
4236 }
4237 else if (remaining >= 2
4238 && addr_tokens[i]->type == STRUCTURE_BASE
4239 && addr_tokens[i]->u.structure_base_kind == BASE_ARBITRARY_EXPR
4240 && addr_tokens[i + 1]->type == ACCESS_METHOD)
4241 {
4242 switch (addr_tokens[i + 1]->u.access_kind)
4243 {
4244 case ACCESS_DIRECT:
4245 case ACCESS_POINTER:
4246 i += 2;
4247 while (addr_tokens[i]->type == ACCESS_METHOD)
4248 i++;
4249 break;
4250 default:
4251 return error_mark_node;
4252 }
4253 }
4254 else if (remaining >= 2
4255 && addr_tokens[i]->type == COMPONENT_SELECTOR
4256 && addr_tokens[i + 1]->type == ACCESS_METHOD)
4257 {
4258 c = expand_component_selector (c, addr_tokens, expr, &i, ort);
4259 /* We used 'expr', so these must have been the last tokens. */
4260 gcc_assert (i == length);
4261 if (c == error_mark_node)
4262 return error_mark_node;
4263 }
4264 else if (remaining >= 3
4265 && addr_tokens[i]->type == COMPONENT_SELECTOR
4266 && addr_tokens[i + 1]->type == STRUCTURE_BASE
4267 && (addr_tokens[i + 1]->u.structure_base_kind
4268 == BASE_COMPONENT_EXPR)
4269 && addr_tokens[i + 2]->type == ACCESS_METHOD)
4270 {
4271 i += 3;
4272 while (addr_tokens[i]->type == ACCESS_METHOD)
4273 i++;
4274 }
4275 else
4276 break;
4277 }
4278
4279 if (i == length)
4280 return c;
4281
4282 return error_mark_node;
4283 }
4284
4285 const struct c_omp_directive c_omp_directives[] = {
4286 /* Keep this alphabetically sorted by the first word. Non-null second/third
4287 if any should precede null ones. */
4288 { "allocate", nullptr, nullptr, PRAGMA_OMP_ALLOCATE,
4289 C_OMP_DIR_DECLARATIVE, false },
4290 { "assume", nullptr, nullptr, PRAGMA_OMP_ASSUME,
4291 C_OMP_DIR_INFORMATIONAL, false },
4292 { "assumes", nullptr, nullptr, PRAGMA_OMP_ASSUMES,
4293 C_OMP_DIR_INFORMATIONAL, false },
4294 { "atomic", nullptr, nullptr, PRAGMA_OMP_ATOMIC,
4295 C_OMP_DIR_CONSTRUCT, false },
4296 { "barrier", nullptr, nullptr, PRAGMA_OMP_BARRIER,
4297 C_OMP_DIR_STANDALONE, false },
4298 { "begin", "assumes", nullptr, PRAGMA_OMP_BEGIN,
4299 C_OMP_DIR_INFORMATIONAL, false },
4300 { "begin", "declare", "target", PRAGMA_OMP_BEGIN,
4301 C_OMP_DIR_DECLARATIVE, false },
4302 /* { "begin", "declare", "variant", PRAGMA_OMP_BEGIN,
4303 C_OMP_DIR_DECLARATIVE, false }, */
4304 /* { "begin", "metadirective", nullptr, PRAGMA_OMP_BEGIN,
4305 C_OMP_DIR_META, false }, */
4306 { "cancel", nullptr, nullptr, PRAGMA_OMP_CANCEL,
4307 C_OMP_DIR_STANDALONE, false },
4308 { "cancellation", "point", nullptr, PRAGMA_OMP_CANCELLATION_POINT,
4309 C_OMP_DIR_STANDALONE, false },
4310 { "critical", nullptr, nullptr, PRAGMA_OMP_CRITICAL,
4311 C_OMP_DIR_CONSTRUCT, false },
4312 /* { "declare", "mapper", nullptr, PRAGMA_OMP_DECLARE,
4313 C_OMP_DIR_DECLARATIVE, false }, */
4314 { "declare", "reduction", nullptr, PRAGMA_OMP_DECLARE,
4315 C_OMP_DIR_DECLARATIVE, true },
4316 { "declare", "simd", nullptr, PRAGMA_OMP_DECLARE,
4317 C_OMP_DIR_DECLARATIVE, true },
4318 { "declare", "target", nullptr, PRAGMA_OMP_DECLARE,
4319 C_OMP_DIR_DECLARATIVE, false },
4320 { "declare", "variant", nullptr, PRAGMA_OMP_DECLARE,
4321 C_OMP_DIR_DECLARATIVE, false },
4322 { "depobj", nullptr, nullptr, PRAGMA_OMP_DEPOBJ,
4323 C_OMP_DIR_STANDALONE, false },
4324 { "dispatch", nullptr, nullptr, PRAGMA_OMP_DISPATCH,
4325 C_OMP_DIR_DECLARATIVE, false },
4326 { "distribute", nullptr, nullptr, PRAGMA_OMP_DISTRIBUTE,
4327 C_OMP_DIR_CONSTRUCT, true },
4328 { "end", "assumes", nullptr, PRAGMA_OMP_END,
4329 C_OMP_DIR_INFORMATIONAL, false },
4330 { "end", "declare", "target", PRAGMA_OMP_END,
4331 C_OMP_DIR_DECLARATIVE, false },
4332 /* { "end", "declare", "variant", PRAGMA_OMP_END,
4333 C_OMP_DIR_DECLARATIVE, false }, */
4334 /* { "end", "metadirective", nullptr, PRAGMA_OMP_END,
4335 C_OMP_DIR_META, false }, */
4336 /* error with at(execution) is C_OMP_DIR_STANDALONE. */
4337 { "error", nullptr, nullptr, PRAGMA_OMP_ERROR,
4338 C_OMP_DIR_UTILITY, false },
4339 { "flush", nullptr, nullptr, PRAGMA_OMP_FLUSH,
4340 C_OMP_DIR_STANDALONE, false },
4341 { "for", nullptr, nullptr, PRAGMA_OMP_FOR,
4342 C_OMP_DIR_CONSTRUCT, true },
4343 /* { "groupprivate", nullptr, nullptr, PRAGMA_OMP_GROUPPRIVATE,
4344 C_OMP_DIR_DECLARATIVE, false }, */
4345 { "interop", nullptr, nullptr, PRAGMA_OMP_INTEROP,
4346 C_OMP_DIR_STANDALONE, false },
4347 { "loop", nullptr, nullptr, PRAGMA_OMP_LOOP,
4348 C_OMP_DIR_CONSTRUCT, true },
4349 { "masked", nullptr, nullptr, PRAGMA_OMP_MASKED,
4350 C_OMP_DIR_CONSTRUCT, true },
4351 { "master", nullptr, nullptr, PRAGMA_OMP_MASTER,
4352 C_OMP_DIR_CONSTRUCT, true },
4353 { "metadirective", nullptr, nullptr, PRAGMA_OMP_METADIRECTIVE,
4354 C_OMP_DIR_META, false },
4355 { "nothing", nullptr, nullptr, PRAGMA_OMP_NOTHING,
4356 C_OMP_DIR_UTILITY, false },
4357 /* ordered with depend clause is C_OMP_DIR_STANDALONE. */
4358 { "ordered", nullptr, nullptr, PRAGMA_OMP_ORDERED,
4359 C_OMP_DIR_CONSTRUCT, true },
4360 { "parallel", nullptr, nullptr, PRAGMA_OMP_PARALLEL,
4361 C_OMP_DIR_CONSTRUCT, true },
4362 { "requires", nullptr, nullptr, PRAGMA_OMP_REQUIRES,
4363 C_OMP_DIR_INFORMATIONAL, false },
4364 { "scan", nullptr, nullptr, PRAGMA_OMP_SCAN,
4365 C_OMP_DIR_CONSTRUCT, true },
4366 { "scope", nullptr, nullptr, PRAGMA_OMP_SCOPE,
4367 C_OMP_DIR_CONSTRUCT, false },
4368 { "section", nullptr, nullptr, PRAGMA_OMP_SECTION,
4369 C_OMP_DIR_CONSTRUCT, false },
4370 { "sections", nullptr, nullptr, PRAGMA_OMP_SECTIONS,
4371 C_OMP_DIR_CONSTRUCT, false },
4372 { "simd", nullptr, nullptr, PRAGMA_OMP_SIMD,
4373 C_OMP_DIR_CONSTRUCT, true },
4374 { "single", nullptr, nullptr, PRAGMA_OMP_SINGLE,
4375 C_OMP_DIR_CONSTRUCT, false },
4376 { "target", "data", nullptr, PRAGMA_OMP_TARGET,
4377 C_OMP_DIR_CONSTRUCT, false },
4378 { "target", "enter", "data", PRAGMA_OMP_TARGET,
4379 C_OMP_DIR_STANDALONE, false },
4380 { "target", "exit", "data", PRAGMA_OMP_TARGET,
4381 C_OMP_DIR_STANDALONE, false },
4382 { "target", "update", nullptr, PRAGMA_OMP_TARGET,
4383 C_OMP_DIR_STANDALONE, false },
4384 { "target", nullptr, nullptr, PRAGMA_OMP_TARGET,
4385 C_OMP_DIR_CONSTRUCT, true },
4386 { "task", nullptr, nullptr, PRAGMA_OMP_TASK,
4387 C_OMP_DIR_CONSTRUCT, false },
4388 { "taskgroup", nullptr, nullptr, PRAGMA_OMP_TASKGROUP,
4389 C_OMP_DIR_CONSTRUCT, false },
4390 { "taskloop", nullptr, nullptr, PRAGMA_OMP_TASKLOOP,
4391 C_OMP_DIR_CONSTRUCT, true },
4392 { "taskwait", nullptr, nullptr, PRAGMA_OMP_TASKWAIT,
4393 C_OMP_DIR_STANDALONE, false },
4394 { "taskyield", nullptr, nullptr, PRAGMA_OMP_TASKYIELD,
4395 C_OMP_DIR_STANDALONE, false },
4396 { "tile", nullptr, nullptr, PRAGMA_OMP_TILE,
4397 C_OMP_DIR_CONSTRUCT, false },
4398 { "teams", nullptr, nullptr, PRAGMA_OMP_TEAMS,
4399 C_OMP_DIR_CONSTRUCT, true },
4400 { "threadprivate", nullptr, nullptr, PRAGMA_OMP_THREADPRIVATE,
4401 C_OMP_DIR_DECLARATIVE, false },
4402 { "unroll", nullptr, nullptr, PRAGMA_OMP_UNROLL,
4403 C_OMP_DIR_CONSTRUCT, false },
4404 };
4405
4406 /* Find (non-combined/composite) OpenMP directive (if any) which starts
4407 with FIRST keyword and for multi-word directives has SECOND and
4408 THIRD keyword after it. */
4409
4410 const struct c_omp_directive *
4411 c_omp_categorize_directive (const char *first, const char *second,
4412 const char *third)
4413 {
4414 const size_t n_omp_directives = ARRAY_SIZE (c_omp_directives);
4415 for (size_t i = 0; i < n_omp_directives; i++)
4416 {
4417 if ((unsigned char) c_omp_directives[i].first[0]
4418 < (unsigned char) first[0])
4419 continue;
4420 if ((unsigned char) c_omp_directives[i].first[0]
4421 > (unsigned char) first[0])
4422 break;
4423 if (strcmp (c_omp_directives[i].first, first))
4424 continue;
4425 if (!c_omp_directives[i].second)
4426 return &c_omp_directives[i];
4427 if (!second || strcmp (c_omp_directives[i].second, second))
4428 continue;
4429 if (!c_omp_directives[i].third)
4430 return &c_omp_directives[i];
4431 if (!third || strcmp (c_omp_directives[i].third, third))
4432 continue;
4433 return &c_omp_directives[i];
4434 }
4435 return NULL;
4436 }
4437
4438 /* Auxilliary helper function for c_omp_expand_variant_construct. */
4439
4440 static tree
4441 c_omp_expand_variant_construct_r (vec<struct omp_variant> &candidates,
4442 hash_map<tree, tree> &body_labels,
4443 unsigned index)
4444 {
4445 struct omp_variant &candidate = candidates[index];
4446 tree if_block = push_stmt_list ();
4447 if (candidate.alternative != NULL_TREE)
4448 add_stmt (candidate.alternative);
4449 if (candidate.body != NULL_TREE)
4450 {
4451 tree *label = body_labels.get (candidate.body);
4452 if (label != NULL)
4453 add_stmt (build1 (GOTO_EXPR, void_type_node, *label));
4454 else
4455 {
4456 tree body_label = create_artificial_label (UNKNOWN_LOCATION);
4457 add_stmt (build1 (LABEL_EXPR, void_type_node, body_label));
4458 add_stmt (candidate.body);
4459 body_labels.put (candidate.body, body_label);
4460 }
4461 }
4462 if_block = pop_stmt_list (if_block);
4463
4464 if (index == candidates.length () - 1)
4465 return if_block;
4466
4467 tree cond = omp_dynamic_cond (candidate.selector, NULL_TREE);
4468 gcc_assert (cond != NULL_TREE);
4469
4470 tree else_block = c_omp_expand_variant_construct_r (candidates, body_labels,
4471 index + 1);
4472 tree ret = push_stmt_list ();
4473 tree stmt = build3 (COND_EXPR, void_type_node, cond, if_block, else_block);
4474 add_stmt (stmt);
4475 ret = pop_stmt_list (ret);
4476
4477 return ret;
4478 }
4479
4480 /* Resolve the vector of metadirective variant CANDIDATES to a parse tree
4481 structure. */
4482
4483 tree
4484 c_omp_expand_variant_construct (vec<struct omp_variant> &candidates)
4485 {
4486 hash_map<tree, tree> body_labels;
4487 return c_omp_expand_variant_construct_r (candidates, body_labels, 0);
4488 }