Debian Codegraph @

gcc/fortran/trans-openmp.cc

raw
1 /* OpenMP directive translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2005-2025 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "options.h"
26 #include "tree.h"
27 #include "gfortran.h"
28 #include "basic-block.h"
29 #include "tree-ssa.h"
30 #include "function.h"
31 #include "gimple.h"
32 #include "gimple-expr.h"
33 #include "trans.h"
34 #include "stringpool.h"
35 #include "fold-const.h"
36 #include "gimplify.h" /* For create_tmp_var_raw. */
37 #include "trans-stmt.h"
38 #include "trans-types.h"
39 #include "trans-array.h"
40 #include "trans-const.h"
41 #include "arith.h"
42 #include "constructor.h"
43 #include "gomp-constants.h"
44 #include "omp-general.h"
45 #include "omp-low.h"
46 #include "memmodel.h" /* For MEMMODEL_ enums. */
47 #include "dependency.h"
48 #include "gimple-iterator.h" /* For gsi_iterator_update. */
49 #include "gimplify-me.h" /* For force_gimple_operand. */
50
51 #undef GCC_DIAG_STYLE
52 #define GCC_DIAG_STYLE __gcc_tdiag__
53 #include "diagnostic-core.h"
54 #undef GCC_DIAG_STYLE
55 #define GCC_DIAG_STYLE __gcc_gfc__
56 #include "attribs.h"
57 #include "function.h"
58
59 int ompws_flags;
60
61 /* True if OpenMP should regard this DECL as being a scalar which has Fortran's
62 allocatable or pointer attribute. */
63
64 bool
65 gfc_omp_is_allocatable_or_ptr (const_tree decl)
66 {
67 return (DECL_P (decl)
68 && (GFC_DECL_GET_SCALAR_POINTER (decl)
69 || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)));
70 }
71
72 /* True if the argument is an optional argument; except that false is also
73 returned for arguments with the value attribute (nonpointers) and for
74 assumed-shape variables (decl is a local variable containing arg->data).
75 Note that for 'procedure(), optional' the value false is used as that's
76 always a pointer and no additional indirection is used.
77 Note that pvoid_type_node is for 'type(c_ptr), value' (and c_funloc). */
78
79 static bool
80 gfc_omp_is_optional_argument (const_tree decl)
81 {
82 /* Note: VAR_DECL can occur with BIND(C) and array descriptors. */
83 return ((TREE_CODE (decl) == PARM_DECL || VAR_P (decl))
84 && DECL_LANG_SPECIFIC (decl)
85 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
86 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))
87 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != FUNCTION_TYPE
88 && GFC_DECL_OPTIONAL_ARGUMENT (decl));
89 }
90
91 /* Check whether this DECL belongs to a Fortran optional argument.
92 With 'for_present_check' set to false, decls which are optional parameters
93 themselves are returned as tree - or a NULL_TREE otherwise. Those decls are
94 always pointers. With 'for_present_check' set to true, the decl for checking
95 whether an argument is present is returned; for arguments with value
96 attribute this is the hidden argument and of BOOLEAN_TYPE. If the decl is
97 unrelated to optional arguments, NULL_TREE is returned. */
98
99 tree
100 gfc_omp_check_optional_argument (tree decl, bool for_present_check)
101 {
102 if (!for_present_check)
103 return gfc_omp_is_optional_argument (decl) ? decl : NULL_TREE;
104
105 if (!DECL_LANG_SPECIFIC (decl))
106 return NULL_TREE;
107
108 tree orig_decl = decl;
109
110 /* For assumed-shape arrays, a local decl with arg->data is used. */
111 if (TREE_CODE (decl) != PARM_DECL
112 && (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
113 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl))))
114 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
115
116 /* Note: With BIND(C), array descriptors are converted to a VAR_DECL. */
117 if (decl == NULL_TREE
118 || (TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != VAR_DECL)
119 || !DECL_LANG_SPECIFIC (decl)
120 || !GFC_DECL_OPTIONAL_ARGUMENT (decl))
121 return NULL_TREE;
122
123 /* Scalars with VALUE attribute which are passed by value use a hidden
124 argument to denote the present status. They are passed as nonpointer type
125 with one exception: 'type(c_ptr), value' as 'void*'. */
126 /* Cf. trans-expr.cc's gfc_conv_expr_present. */
127 if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
128 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
129 {
130 char name[GFC_MAX_SYMBOL_LEN + 2];
131 tree tree_name;
132
133 name[0] = '.';
134 strcpy (&name[1], IDENTIFIER_POINTER (DECL_NAME (decl)));
135 tree_name = get_identifier (name);
136
137 /* Walk function argument list to find the hidden arg. */
138 decl = DECL_ARGUMENTS (DECL_CONTEXT (decl));
139 for ( ; decl != NULL_TREE; decl = TREE_CHAIN (decl))
140 if (DECL_NAME (decl) == tree_name
141 && DECL_ARTIFICIAL (decl))
142 break;
143
144 gcc_assert (decl);
145 return decl;
146 }
147
148 return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
149 orig_decl, null_pointer_node);
150 }
151
152
153 /* Returns tree with NULL if it is not an array descriptor and with the tree to
154 access the 'data' component otherwise. With type_only = true, it returns the
155 TREE_TYPE without creating a new tree. */
156
157 tree
158 gfc_omp_array_data (tree decl, bool type_only)
159 {
160 tree type = TREE_TYPE (decl);
161
162 if (POINTER_TYPE_P (type))
163 type = TREE_TYPE (type);
164
165 if (!GFC_DESCRIPTOR_TYPE_P (type))
166 return NULL_TREE;
167
168 if (type_only)
169 return GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
170
171 if (POINTER_TYPE_P (TREE_TYPE (decl)))
172 decl = build_fold_indirect_ref (decl);
173
174 decl = gfc_conv_descriptor_data_get (decl);
175 STRIP_NOPS (decl);
176 return decl;
177 }
178
179 /* Return the byte-size of the passed array descriptor. */
180
181 tree
182 gfc_omp_array_size (tree decl, gimple_seq *pre_p)
183 {
184 stmtblock_t block;
185 if (POINTER_TYPE_P (TREE_TYPE (decl)))
186 decl = build_fold_indirect_ref (decl);
187 tree type = TREE_TYPE (decl);
188 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
189 bool allocatable = (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
190 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
191 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT);
192 gfc_init_block (&block);
193 tree size = gfc_full_array_size (&block, decl,
194 GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)));
195 size = fold_convert (size_type_node, size);
196 tree elemsz = gfc_get_element_type (TREE_TYPE (decl));
197 if (TREE_CODE (elemsz) == ARRAY_TYPE && TYPE_STRING_FLAG (elemsz))
198 elemsz = gfc_conv_descriptor_elem_len (decl);
199 else
200 elemsz = TYPE_SIZE_UNIT (elemsz);
201 size = fold_build2 (MULT_EXPR, size_type_node, size, elemsz);
202 if (!allocatable)
203 gimplify_and_add (gfc_finish_block (&block), pre_p);
204 else
205 {
206 tree var = create_tmp_var (size_type_node);
207 gfc_add_expr_to_block (&block, build2 (MODIFY_EXPR, sizetype, var, size));
208 tree tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
209 gfc_conv_descriptor_data_get (decl),
210 null_pointer_node);
211 tmp = build3_loc (input_location, COND_EXPR, void_type_node, tmp,
212 gfc_finish_block (&block),
213 build2 (MODIFY_EXPR, sizetype, var, size_zero_node));
214 gimplify_and_add (tmp, pre_p);
215 size = var;
216 }
217 return size;
218 }
219
220
221 /* True if OpenMP should privatize what this DECL points to rather
222 than the DECL itself. */
223
224 bool
225 gfc_omp_privatize_by_reference (const_tree decl)
226 {
227 tree type = TREE_TYPE (decl);
228
229 if (TREE_CODE (type) == REFERENCE_TYPE
230 && (!DECL_ARTIFICIAL (decl) || TREE_CODE (decl) == PARM_DECL))
231 return true;
232
233 if (TREE_CODE (type) == POINTER_TYPE
234 && gfc_omp_is_optional_argument (decl))
235 return true;
236
237 if (TREE_CODE (type) == POINTER_TYPE)
238 {
239 while (TREE_CODE (decl) == COMPONENT_REF)
240 decl = TREE_OPERAND (decl, 1);
241
242 /* Array POINTER/ALLOCATABLE have aggregate types, all user variables
243 that have POINTER_TYPE type and aren't scalar pointers, scalar
244 allocatables, Cray pointees or C pointers are supposed to be
245 privatized by reference. */
246 if (GFC_DECL_GET_SCALAR_POINTER (decl)
247 || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
248 || GFC_DECL_CRAY_POINTEE (decl)
249 || GFC_DECL_ASSOCIATE_VAR_P (decl)
250 || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
251 return false;
252
253 if (!DECL_ARTIFICIAL (decl)
254 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
255 return true;
256
257 /* Some arrays are expanded as DECL_ARTIFICIAL pointers
258 by the frontend. */
259 if (DECL_LANG_SPECIFIC (decl)
260 && GFC_DECL_SAVED_DESCRIPTOR (decl))
261 return true;
262 }
263
264 return false;
265 }
266
267 /* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute
268 of DECL is predetermined. */
269
270 enum omp_clause_default_kind
271 gfc_omp_predetermined_sharing (tree decl)
272 {
273 /* Associate names preserve the association established during ASSOCIATE.
274 As they are implemented either as pointers to the selector or array
275 descriptor and shouldn't really change in the ASSOCIATE region,
276 this decl can be either shared or firstprivate. If it is a pointer,
277 use firstprivate, as it is cheaper that way, otherwise make it shared. */
278 if (GFC_DECL_ASSOCIATE_VAR_P (decl))
279 {
280 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
281 return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
282 else
283 return OMP_CLAUSE_DEFAULT_SHARED;
284 }
285
286 if (DECL_ARTIFICIAL (decl)
287 && ! GFC_DECL_RESULT (decl)
288 && ! (DECL_LANG_SPECIFIC (decl)
289 && GFC_DECL_SAVED_DESCRIPTOR (decl)))
290 return OMP_CLAUSE_DEFAULT_SHARED;
291
292 /* Cray pointees shouldn't be listed in any clauses and should be
293 gimplified to dereference of the corresponding Cray pointer.
294 Make them all private, so that they are emitted in the debug
295 information. */
296 if (GFC_DECL_CRAY_POINTEE (decl))
297 return OMP_CLAUSE_DEFAULT_PRIVATE;
298
299 /* Assumed-size arrays are predetermined shared. */
300 if (TREE_CODE (decl) == PARM_DECL
301 && GFC_ARRAY_TYPE_P (TREE_TYPE (decl))
302 && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (decl)) == GFC_ARRAY_UNKNOWN
303 && GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (decl),
304 GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)) - 1)
305 == NULL)
306 return OMP_CLAUSE_DEFAULT_SHARED;
307
308 /* Dummy procedures aren't considered variables by OpenMP, thus are
309 disallowed in OpenMP clauses. They are represented as PARM_DECLs
310 in the middle-end, so return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE here
311 to avoid complaining about their uses with default(none). */
312 if (TREE_CODE (decl) == PARM_DECL
313 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
314 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == FUNCTION_TYPE)
315 return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
316
317 /* COMMON and EQUIVALENCE decls are shared. They
318 are only referenced through DECL_VALUE_EXPR of the variables
319 contained in them. If those are privatized, they will not be
320 gimplified to the COMMON or EQUIVALENCE decls. */
321 if (GFC_DECL_COMMON_OR_EQUIV (decl) && ! DECL_HAS_VALUE_EXPR_P (decl))
322 return OMP_CLAUSE_DEFAULT_SHARED;
323
324 if (GFC_DECL_RESULT (decl) && ! DECL_HAS_VALUE_EXPR_P (decl))
325 return OMP_CLAUSE_DEFAULT_SHARED;
326
327 /* These are either array or derived parameters, or vtables.
328 In the former cases, the OpenMP standard doesn't consider them to be
329 variables at all (they can't be redefined), but they can nevertheless appear
330 in parallel/task regions and for default(none) purposes treat them as shared.
331 For vtables likely the same handling is desirable. */
332 if (VAR_P (decl) && TREE_READONLY (decl)
333 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
334 return OMP_CLAUSE_DEFAULT_SHARED;
335
336 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
337 }
338
339
340 /* OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED unless OpenMP mapping attribute
341 of DECL is predetermined. */
342
343 enum omp_clause_defaultmap_kind
344 gfc_omp_predetermined_mapping (tree decl)
345 {
346 if (DECL_ARTIFICIAL (decl)
347 && ! GFC_DECL_RESULT (decl)
348 && ! (DECL_LANG_SPECIFIC (decl)
349 && GFC_DECL_SAVED_DESCRIPTOR (decl)))
350 return OMP_CLAUSE_DEFAULTMAP_TO;
351
352 /* Dummy procedures aren't considered variables by OpenMP, thus are
353 disallowed in OpenMP clauses. They are represented as PARM_DECLs
354 in the middle-end, so return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE here
355 to avoid complaining about their uses with defaultmap(none). */
356 if (TREE_CODE (decl) == PARM_DECL
357 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
358 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == FUNCTION_TYPE)
359 return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
360
361 /* These are either array or derived parameters, or vtables. */
362 if (VAR_P (decl) && TREE_READONLY (decl)
363 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
364 return OMP_CLAUSE_DEFAULTMAP_TO;
365
366 return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
367 }
368
369
370 /* Return decl that should be used when reporting DEFAULT(NONE)
371 diagnostics. */
372
373 tree
374 gfc_omp_report_decl (tree decl)
375 {
376 if (DECL_ARTIFICIAL (decl)
377 && DECL_LANG_SPECIFIC (decl)
378 && GFC_DECL_SAVED_DESCRIPTOR (decl))
379 return GFC_DECL_SAVED_DESCRIPTOR (decl);
380
381 return decl;
382 }
383
384 /* Return true if TYPE has any allocatable components;
385 if ptr_ok, the decl itself is permitted to have the POINTER attribute.
386 if shallow_alloc_only, returns only true if any of the fields is an
387 allocatable; called with true by gfc_omp_replace_alloc_by_to_mapping. */
388
389 static bool
390 gfc_has_alloc_comps (tree type, tree decl, bool ptr_ok,
391 bool shallow_alloc_only=false)
392 {
393 tree field, ftype;
394
395 if (POINTER_TYPE_P (type))
396 {
397 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
398 || (ptr_ok && GFC_DECL_GET_SCALAR_POINTER (decl)))
399 type = TREE_TYPE (type);
400 else if (GFC_DECL_GET_SCALAR_POINTER (decl))
401 return false;
402 }
403
404 if (!ptr_ok
405 && GFC_DESCRIPTOR_TYPE_P (type)
406 && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
407 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT))
408 return false;
409
410 if (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type))
411 type = gfc_get_element_type (type);
412
413 if (TREE_CODE (type) != RECORD_TYPE)
414 return false;
415
416 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
417 {
418 ftype = TREE_TYPE (field);
419 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
420 return true;
421 if (GFC_DESCRIPTOR_TYPE_P (ftype)
422 && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
423 return true;
424 if (!shallow_alloc_only
425 && gfc_has_alloc_comps (ftype, field, false))
426 return true;
427 }
428 return false;
429 }
430
431 /* gfc_omp_replace_alloc_by_to_mapping is used with gfc_omp_deep_mapping... to
432 handle the following:
433
434 For map(alloc: dt), the array descriptors of allocatable components should
435 be mapped as 'to'; this could be done by (A) adding 'map(to: dt%alloc_comp)'
436 for each component (and avoiding to increment the reference count).
437 Or (B) by just mapping all of 'dt' as 'to'.
438
439 If 'dt' contains several allocatable components and not much other data,
440 (A) is more efficient. If 'dt' contains a large const-size array, (A) will
441 copy it to the device instead of only 'alloc'ating it.
442
443 IMPLEMENTATION CHOICE: We do (A). It avoids the ref-count issue and it is
444 expected that, for real-world code, derived types with allocatable
445 components only have few other components and either no const-size arrays.
446 This copying is done irrespectively whether the allocatables are allocated.
447
448 If users wanted to save memory, they have to use 'map(alloc:dt%comp)' as
449 also with 'map(alloc:dt)' all components get copied.
450
451 For the copy to the device, only allocatable arrays are relevant as their
452 the bounds are required; the pointer is set separately (GOMP_MAP_ATTACH)
453 and the only setting required for scalars. However, when later copying out
454 of the device, an unallocated allocatable must remain unallocated/NULL on
455 the host; to achieve this we also must have it set to NULL on the device
456 to avoid issues with uninitialized memory being copied back for the pointer
457 address. If we could set the pointer to NULL, gfc_has_alloc_comps's
458 shallow_alloc_only could be restricted to return true only for arrays.
459
460 We only need to return true if there are allocatable-array components. */
461
462 static bool
463 gfc_omp_replace_alloc_by_to_mapping (tree type, tree decl, bool ptr_ok)
464 {
465 return gfc_has_alloc_comps (type, decl, ptr_ok, true);
466 }
467
468
469 static bool
470 gfc_is_polymorphic_nonptr (tree type)
471 {
472 if (POINTER_TYPE_P (type))
473 type = TREE_TYPE (type);
474 return GFC_CLASS_TYPE_P (type);
475 }
476
477 /* Return true if TYPE is a class container for a POINTER entity. */
478
479 static bool
480 gfc_is_class_pointer_type (tree type)
481 {
482 tree name;
483 const char *s;
484
485 if (POINTER_TYPE_P (type))
486 type = TREE_TYPE (type);
487
488 if (!GFC_CLASS_TYPE_P (type))
489 return false;
490
491 name = TYPE_NAME (type);
492 if (name && TREE_CODE (name) == TYPE_DECL)
493 name = DECL_NAME (name);
494 if (!name)
495 return false;
496
497 s = IDENTIFIER_POINTER (name);
498 return startswith (s, "__class_") && s[strlen (s) - 1] == 'p';
499 }
500
501 /* Return true if TYPE is unlimited polymorphic but not with pointer attribute;
502 unlimited means also intrinsic types are handled and _len is used. */
503
504 static bool
505 gfc_is_unlimited_polymorphic_nonptr (tree type)
506 {
507 if (POINTER_TYPE_P (type))
508 type = TREE_TYPE (type);
509 if (!GFC_CLASS_TYPE_P (type))
510 return false;
511
512 tree field = TYPE_FIELDS (type); /* _data */
513 gcc_assert (field);
514 field = DECL_CHAIN (field); /* _vptr */
515 gcc_assert (field);
516 field = DECL_CHAIN (field);
517 if (!field)
518 return false;
519 gcc_assert (strcmp ("_len", IDENTIFIER_POINTER (DECL_NAME (field))) == 0);
520 return true;
521 }
522
523 /* Return true if the DECL is for an allocatable array or scalar. */
524
525 bool
526 gfc_omp_allocatable_p (tree decl)
527 {
528 if (!DECL_P (decl))
529 return false;
530
531 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl))
532 return true;
533
534 tree type = TREE_TYPE (decl);
535 if (gfc_omp_privatize_by_reference (decl))
536 type = TREE_TYPE (type);
537
538 if (GFC_DESCRIPTOR_TYPE_P (type)
539 && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
540 return true;
541
542 return false;
543 }
544
545
546 /* Return true if DECL in private clause needs
547 OMP_CLAUSE_PRIVATE_OUTER_REF on the private clause. */
548 bool
549 gfc_omp_private_outer_ref (tree decl)
550 {
551 tree type = TREE_TYPE (decl);
552
553 if (gfc_omp_privatize_by_reference (decl))
554 type = TREE_TYPE (type);
555
556 if (GFC_DESCRIPTOR_TYPE_P (type)
557 && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
558 return true;
559
560 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl))
561 return true;
562
563 if (gfc_has_alloc_comps (type, decl, false))
564 return true;
565
566 return false;
567 }
568
569 /* Callback for gfc_omp_unshare_expr. */
570
571 static tree
572 gfc_omp_unshare_expr_r (tree *tp, int *walk_subtrees, void *)
573 {
574 tree t = *tp;
575 enum tree_code code = TREE_CODE (t);
576
577 /* Stop at types, decls, constants like copy_tree_r. */
578 if (TREE_CODE_CLASS (code) == tcc_type
579 || TREE_CODE_CLASS (code) == tcc_declaration
580 || TREE_CODE_CLASS (code) == tcc_constant
581 || code == BLOCK)
582 *walk_subtrees = 0;
583 else if (handled_component_p (t)
584 || TREE_CODE (t) == MEM_REF)
585 {
586 *tp = unshare_expr (t);
587 *walk_subtrees = 0;
588 }
589
590 return NULL_TREE;
591 }
592
593 /* Unshare in expr anything that the FE which normally doesn't
594 care much about tree sharing (because during gimplification
595 everything is unshared) could cause problems with tree sharing
596 at omp-low.cc time. */
597
598 static tree
599 gfc_omp_unshare_expr (tree expr)
600 {
601 walk_tree (&expr, gfc_omp_unshare_expr_r, NULL, NULL);
602 return expr;
603 }
604
605 enum walk_alloc_comps
606 {
607 WALK_ALLOC_COMPS_DTOR,
608 WALK_ALLOC_COMPS_DEFAULT_CTOR,
609 WALK_ALLOC_COMPS_COPY_CTOR
610 };
611
612 /* Handle allocatable components in OpenMP clauses. */
613
614 static tree
615 gfc_walk_alloc_comps (tree decl, tree dest, tree var,
616 enum walk_alloc_comps kind)
617 {
618 stmtblock_t block, tmpblock;
619 tree type = TREE_TYPE (decl), then_b, tem, field;
620 gfc_init_block (&block);
621
622 if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
623 {
624 if (GFC_DESCRIPTOR_TYPE_P (type))
625 {
626 gfc_init_block (&tmpblock);
627 tem = gfc_full_array_size (&tmpblock, decl,
628 GFC_TYPE_ARRAY_RANK (type));
629 then_b = gfc_finish_block (&tmpblock);
630 gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (then_b));
631 tem = gfc_omp_unshare_expr (tem);
632 tem = fold_build2_loc (input_location, MINUS_EXPR,
633 gfc_array_index_type, tem,
634 gfc_index_one_node);
635 }
636 else
637 {
638 bool compute_nelts = false;
639 if (!TYPE_DOMAIN (type)
640 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE
641 || TYPE_MIN_VALUE (TYPE_DOMAIN (type)) == error_mark_node
642 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == error_mark_node)
643 compute_nelts = true;
644 else if (VAR_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
645 {
646 tree a = DECL_ATTRIBUTES (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
647 if (lookup_attribute ("omp dummy var", a))
648 compute_nelts = true;
649 }
650 if (compute_nelts)
651 {
652 tem = fold_build2 (EXACT_DIV_EXPR, sizetype,
653 TYPE_SIZE_UNIT (type),
654 TYPE_SIZE_UNIT (TREE_TYPE (type)));
655 tem = size_binop (MINUS_EXPR, tem, size_one_node);
656 }
657 else
658 tem = array_type_nelts_minus_one (type);
659 tem = fold_convert (gfc_array_index_type, tem);
660 }
661
662 tree nelems = gfc_evaluate_now (tem, &block);
663 tree index = gfc_create_var (gfc_array_index_type, "S");
664
665 gfc_init_block (&tmpblock);
666 tem = gfc_conv_array_data (decl);
667 tree declvar = build_fold_indirect_ref_loc (input_location, tem);
668 tree declvref = gfc_build_array_ref (declvar, index, NULL);
669 tree destvar, destvref = NULL_TREE;
670 if (dest)
671 {
672 tem = gfc_conv_array_data (dest);
673 destvar = build_fold_indirect_ref_loc (input_location, tem);
674 destvref = gfc_build_array_ref (destvar, index, NULL);
675 }
676 gfc_add_expr_to_block (&tmpblock,
677 gfc_walk_alloc_comps (declvref, destvref,
678 var, kind));
679
680 gfc_loopinfo loop;
681 gfc_init_loopinfo (&loop);
682 loop.dimen = 1;
683 loop.from[0] = gfc_index_zero_node;
684 loop.loopvar[0] = index;
685 loop.to[0] = nelems;
686 gfc_trans_scalarizing_loops (&loop, &tmpblock);
687 gfc_add_block_to_block (&block, &loop.pre);
688 return gfc_finish_block (&block);
689 }
690 else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (var))
691 {
692 decl = build_fold_indirect_ref_loc (input_location, decl);
693 if (dest)
694 dest = build_fold_indirect_ref_loc (input_location, dest);
695 type = TREE_TYPE (decl);
696 }
697
698 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
699 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
700 {
701 tree ftype = TREE_TYPE (field);
702 tree declf, destf = NULL_TREE;
703 bool has_alloc_comps = gfc_has_alloc_comps (ftype, field, false);
704 if ((!GFC_DESCRIPTOR_TYPE_P (ftype)
705 || GFC_TYPE_ARRAY_AKIND (ftype) != GFC_ARRAY_ALLOCATABLE)
706 && !GFC_DECL_GET_SCALAR_ALLOCATABLE (field)
707 && !has_alloc_comps)
708 continue;
709 declf = fold_build3_loc (input_location, COMPONENT_REF, ftype,
710 decl, field, NULL_TREE);
711 if (dest)
712 destf = fold_build3_loc (input_location, COMPONENT_REF, ftype,
713 dest, field, NULL_TREE);
714
715 tem = NULL_TREE;
716 switch (kind)
717 {
718 case WALK_ALLOC_COMPS_DTOR:
719 break;
720 case WALK_ALLOC_COMPS_DEFAULT_CTOR:
721 if (GFC_DESCRIPTOR_TYPE_P (ftype)
722 && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
723 {
724 gfc_add_modify (&block, unshare_expr (destf),
725 unshare_expr (declf));
726 tem = gfc_duplicate_allocatable_nocopy
727 (destf, declf, ftype,
728 GFC_TYPE_ARRAY_RANK (ftype));
729 }
730 else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
731 tem = gfc_duplicate_allocatable_nocopy (destf, declf, ftype, 0);
732 break;
733 case WALK_ALLOC_COMPS_COPY_CTOR:
734 if (GFC_DESCRIPTOR_TYPE_P (ftype)
735 && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
736 tem = gfc_duplicate_allocatable (destf, declf, ftype,
737 GFC_TYPE_ARRAY_RANK (ftype),
738 NULL_TREE);
739 else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
740 tem = gfc_duplicate_allocatable (destf, declf, ftype, 0,
741 NULL_TREE);
742 break;
743 }
744 if (tem)
745 gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
746 if (has_alloc_comps)
747 {
748 gfc_init_block (&tmpblock);
749 gfc_add_expr_to_block (&tmpblock,
750 gfc_walk_alloc_comps (declf, destf,
751 field, kind));
752 then_b = gfc_finish_block (&tmpblock);
753 if (GFC_DESCRIPTOR_TYPE_P (ftype)
754 && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
755 tem = gfc_conv_descriptor_data_get (unshare_expr (declf));
756 else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
757 tem = unshare_expr (declf);
758 else
759 tem = NULL_TREE;
760 if (tem)
761 {
762 tem = fold_convert (pvoid_type_node, tem);
763 tem = fold_build2_loc (input_location, NE_EXPR,
764 logical_type_node, tem,
765 null_pointer_node);
766 then_b = build3_loc (input_location, COND_EXPR, void_type_node,
767 tem, then_b,
768 build_empty_stmt (input_location));
769 }
770 gfc_add_expr_to_block (&block, then_b);
771 }
772 if (kind == WALK_ALLOC_COMPS_DTOR)
773 {
774 if (GFC_DESCRIPTOR_TYPE_P (ftype)
775 && GFC_TYPE_ARRAY_AKIND (ftype) == GFC_ARRAY_ALLOCATABLE)
776 {
777 tem = gfc_conv_descriptor_data_get (unshare_expr (declf));
778 tem = gfc_deallocate_with_status (tem, NULL_TREE, NULL_TREE,
779 NULL_TREE, NULL_TREE, true,
780 NULL,
781 GFC_CAF_COARRAY_NOCOARRAY);
782 gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
783 }
784 else if (GFC_DECL_GET_SCALAR_ALLOCATABLE (field))
785 {
786 tem = gfc_call_free (unshare_expr (declf));
787 gfc_add_expr_to_block (&block, gfc_omp_unshare_expr (tem));
788 }
789 }
790 }
791
792 return gfc_finish_block (&block);
793 }
794
795 /* Return code to initialize DECL with its default constructor, or
796 NULL if there's nothing to do. */
797
798 tree
799 gfc_omp_clause_default_ctor (tree clause, tree decl, tree outer)
800 {
801 tree type = TREE_TYPE (decl), size, ptr, cond, then_b, else_b;
802 stmtblock_t block, cond_block;
803
804 switch (OMP_CLAUSE_CODE (clause))
805 {
806 case OMP_CLAUSE__LOOPTEMP_:
807 case OMP_CLAUSE__REDUCTEMP_:
808 case OMP_CLAUSE__CONDTEMP_:
809 case OMP_CLAUSE__SCANTEMP_:
810 return NULL;
811 case OMP_CLAUSE_PRIVATE:
812 case OMP_CLAUSE_LASTPRIVATE:
813 case OMP_CLAUSE_LINEAR:
814 case OMP_CLAUSE_REDUCTION:
815 case OMP_CLAUSE_IN_REDUCTION:
816 case OMP_CLAUSE_TASK_REDUCTION:
817 break;
818 default:
819 gcc_unreachable ();
820 }
821
822 if ((! GFC_DESCRIPTOR_TYPE_P (type)
823 || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
824 && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
825 || !POINTER_TYPE_P (type)))
826 {
827 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
828 {
829 gcc_assert (outer);
830 gfc_start_block (&block);
831 tree tem = gfc_walk_alloc_comps (outer, decl,
832 OMP_CLAUSE_DECL (clause),
833 WALK_ALLOC_COMPS_DEFAULT_CTOR);
834 gfc_add_expr_to_block (&block, tem);
835 return gfc_finish_block (&block);
836 }
837 return NULL_TREE;
838 }
839
840 gcc_assert (outer != NULL_TREE
841 || (!GFC_DESCRIPTOR_TYPE_P (type)
842 && !gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause),
843 false)));
844
845 /* Allocatable arrays and scalars in PRIVATE clauses need to be set to
846 "not currently allocated" allocation status if outer
847 array is "not currently allocated", otherwise should be allocated. */
848 gfc_start_block (&block);
849
850 gfc_init_block (&cond_block);
851
852 if (GFC_DESCRIPTOR_TYPE_P (type))
853 {
854 gfc_add_modify (&cond_block, decl, outer);
855 tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
856 size = gfc_conv_descriptor_ubound_get (decl, rank);
857 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
858 size,
859 gfc_conv_descriptor_lbound_get (decl, rank));
860 size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
861 size, gfc_index_one_node);
862 if (GFC_TYPE_ARRAY_RANK (type) > 1)
863 size = fold_build2_loc (input_location, MULT_EXPR,
864 gfc_array_index_type, size,
865 gfc_conv_descriptor_stride_get (decl, rank));
866 tree esize = fold_convert (gfc_array_index_type,
867 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
868 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
869 size, esize);
870 size = unshare_expr (size);
871 size = gfc_evaluate_now (fold_convert (size_type_node, size),
872 &cond_block);
873 }
874 else
875 size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
876 ptr = gfc_create_var (pvoid_type_node, NULL);
877 gfc_allocate_using_malloc (&cond_block, ptr, size, NULL_TREE);
878 if (GFC_DESCRIPTOR_TYPE_P (type))
879 gfc_conv_descriptor_data_set (&cond_block, unshare_expr (decl), ptr);
880 else
881 gfc_add_modify (&cond_block, unshare_expr (decl),
882 fold_convert (TREE_TYPE (decl), ptr));
883 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
884 {
885 tree tem = gfc_walk_alloc_comps (outer, decl,
886 OMP_CLAUSE_DECL (clause),
887 WALK_ALLOC_COMPS_DEFAULT_CTOR);
888 gfc_add_expr_to_block (&cond_block, tem);
889 }
890 then_b = gfc_finish_block (&cond_block);
891
892 /* Reduction clause requires allocated ALLOCATABLE. */
893 if (OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_REDUCTION
894 && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_IN_REDUCTION
895 && OMP_CLAUSE_CODE (clause) != OMP_CLAUSE_TASK_REDUCTION)
896 {
897 gfc_init_block (&cond_block);
898 if (GFC_DESCRIPTOR_TYPE_P (type))
899 gfc_conv_descriptor_data_set (&cond_block, unshare_expr (decl),
900 null_pointer_node);
901 else
902 gfc_add_modify (&cond_block, unshare_expr (decl),
903 build_zero_cst (TREE_TYPE (decl)));
904 else_b = gfc_finish_block (&cond_block);
905
906 tree tem = fold_convert (pvoid_type_node,
907 GFC_DESCRIPTOR_TYPE_P (type)
908 ? gfc_conv_descriptor_data_get (outer) : outer);
909 tem = unshare_expr (tem);
910 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
911 tem, null_pointer_node);
912 gfc_add_expr_to_block (&block,
913 build3_loc (input_location, COND_EXPR,
914 void_type_node, cond, then_b,
915 else_b));
916 /* Avoid -W*uninitialized warnings. */
917 if (DECL_P (decl))
918 suppress_warning (decl, OPT_Wuninitialized);
919 }
920 else
921 gfc_add_expr_to_block (&block, then_b);
922
923 return gfc_finish_block (&block);
924 }
925
926 /* Build and return code for a copy constructor from SRC to DEST. */
927
928 tree
929 gfc_omp_clause_copy_ctor (tree clause, tree dest, tree src)
930 {
931 tree type = TREE_TYPE (dest), ptr, size, call;
932 tree decl_type = TREE_TYPE (OMP_CLAUSE_DECL (clause));
933 tree orig_decl = OMP_CLAUSE_DECL (clause);
934 tree cond, then_b, else_b;
935 stmtblock_t block, cond_block;
936
937 gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_FIRSTPRIVATE
938 || OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LINEAR);
939
940 if (DECL_ARTIFICIAL (orig_decl)
941 && DECL_LANG_SPECIFIC (orig_decl)
942 && GFC_DECL_SAVED_DESCRIPTOR (orig_decl))
943 {
944 orig_decl = GFC_DECL_SAVED_DESCRIPTOR (orig_decl);
945 decl_type = TREE_TYPE (orig_decl);
946 }
947
948 /* Privatize pointer association only; cf. gfc_omp_predetermined_sharing.
949 This includes scalar class pointers, whose tree type is still the class
950 record even though the Fortran entity has POINTER semantics. */
951 if (DECL_P (orig_decl)
952 && (GFC_DECL_ASSOCIATE_VAR_P (orig_decl)
953 || GFC_DECL_GET_SCALAR_POINTER (orig_decl)
954 || gfc_is_class_pointer_type (decl_type)))
955 return build2 (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
956
957 if (gfc_is_polymorphic_nonptr (decl_type))
958 {
959 if (POINTER_TYPE_P (decl_type))
960 decl_type = TREE_TYPE (decl_type);
961 decl_type = TREE_TYPE (TYPE_FIELDS (decl_type));
962 if (GFC_DESCRIPTOR_TYPE_P (decl_type) || GFC_ARRAY_TYPE_P (decl_type))
963 fatal_error (input_location,
964 "Sorry, polymorphic arrays not yet supported for "
965 "firstprivate");
966 tree src_len;
967 tree nelems = build_int_cst (size_type_node, 1); /* Scalar. */
968 tree src_data = gfc_class_data_get (unshare_expr (src));
969 tree dest_data = gfc_class_data_get (unshare_expr (dest));
970 bool unlimited = gfc_is_unlimited_polymorphic_nonptr (type);
971
972 gfc_start_block (&block);
973 gfc_add_modify (&block, gfc_class_vptr_get (dest),
974 gfc_class_vptr_get (src));
975 gfc_init_block (&cond_block);
976
977 if (unlimited)
978 {
979 src_len = gfc_class_len_get (src);
980 gfc_add_modify (&cond_block, gfc_class_len_get (unshare_expr (dest)), src_len);
981 }
982
983 /* Use: size = class._vtab._size * (class._len > 0 ? class._len : 1). */
984 size = fold_convert (size_type_node, gfc_class_vtab_size_get (src));
985 if (unlimited)
986 {
987 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
988 unshare_expr (src_len),
989 build_zero_cst (TREE_TYPE (src_len)));
990 cond = build3_loc (input_location, COND_EXPR, size_type_node, cond,
991 fold_convert (size_type_node,
992 unshare_expr (src_len)),
993 build_int_cst (size_type_node, 1));
994 size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
995 size, cond);
996 }
997
998 /* Malloc memory + call class->_vpt->_copy. */
999 call = builtin_decl_explicit (BUILT_IN_MALLOC);
1000 call = build_call_expr_loc (input_location, call, 1, size);
1001 gfc_add_modify (&cond_block, dest_data,
1002 fold_convert (TREE_TYPE (dest_data), call));
1003 gfc_add_expr_to_block (&cond_block,
1004 gfc_copy_class_to_class (src, dest, nelems,
1005 unlimited));
1006
1007 gcc_assert (TREE_CODE (dest_data) == COMPONENT_REF);
1008 if (!GFC_DECL_GET_SCALAR_ALLOCATABLE (TREE_OPERAND (dest_data, 1)))
1009 {
1010 gfc_add_block_to_block (&block, &cond_block);
1011 }
1012 else
1013 {
1014 /* Create: if (class._data != 0) <cond_block> else class._data = NULL; */
1015 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1016 src_data, null_pointer_node);
1017 gfc_add_expr_to_block (&block, build3_loc (input_location, COND_EXPR,
1018 void_type_node, cond,
1019 gfc_finish_block (&cond_block),
1020 fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
1021 unshare_expr (dest_data), null_pointer_node)));
1022 }
1023 return gfc_finish_block (&block);
1024 }
1025
1026 if ((! GFC_DESCRIPTOR_TYPE_P (type)
1027 || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
1028 && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
1029 || !POINTER_TYPE_P (type)))
1030 {
1031 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1032 {
1033 gfc_start_block (&block);
1034 gfc_add_modify (&block, dest, src);
1035 tree tem = gfc_walk_alloc_comps (src, dest, OMP_CLAUSE_DECL (clause),
1036 WALK_ALLOC_COMPS_COPY_CTOR);
1037 gfc_add_expr_to_block (&block, tem);
1038 return gfc_finish_block (&block);
1039 }
1040 else
1041 return build2_v (MODIFY_EXPR, dest, src);
1042 }
1043
1044 /* Allocatable arrays in FIRSTPRIVATE clauses need to be allocated
1045 and copied from SRC. */
1046 gfc_start_block (&block);
1047
1048 gfc_init_block (&cond_block);
1049
1050 gfc_add_modify (&cond_block, dest, fold_convert (TREE_TYPE (dest), src));
1051 if (GFC_DESCRIPTOR_TYPE_P (type))
1052 {
1053 tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
1054 size = gfc_conv_descriptor_ubound_get (dest, rank);
1055 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1056 size,
1057 gfc_conv_descriptor_lbound_get (dest, rank));
1058 size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1059 size, gfc_index_one_node);
1060 if (GFC_TYPE_ARRAY_RANK (type) > 1)
1061 size = fold_build2_loc (input_location, MULT_EXPR,
1062 gfc_array_index_type, size,
1063 gfc_conv_descriptor_stride_get (dest, rank));
1064 tree esize = fold_convert (gfc_array_index_type,
1065 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
1066 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1067 size, esize);
1068 size = unshare_expr (size);
1069 size = gfc_evaluate_now (fold_convert (size_type_node, size),
1070 &cond_block);
1071 }
1072 else
1073 size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
1074 ptr = gfc_create_var (pvoid_type_node, NULL);
1075 gfc_allocate_using_malloc (&cond_block, ptr, size, NULL_TREE);
1076 if (GFC_DESCRIPTOR_TYPE_P (type))
1077 gfc_conv_descriptor_data_set (&cond_block, unshare_expr (dest), ptr);
1078 else
1079 gfc_add_modify (&cond_block, unshare_expr (dest),
1080 fold_convert (TREE_TYPE (dest), ptr));
1081
1082 tree srcptr = GFC_DESCRIPTOR_TYPE_P (type)
1083 ? gfc_conv_descriptor_data_get (src) : src;
1084 srcptr = unshare_expr (srcptr);
1085 srcptr = fold_convert (pvoid_type_node, srcptr);
1086 call = build_call_expr_loc (input_location,
1087 builtin_decl_explicit (BUILT_IN_MEMCPY), 3, ptr,
1088 srcptr, size);
1089 gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
1090 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1091 {
1092 tree tem = gfc_walk_alloc_comps (src, dest,
1093 OMP_CLAUSE_DECL (clause),
1094 WALK_ALLOC_COMPS_COPY_CTOR);
1095 gfc_add_expr_to_block (&cond_block, tem);
1096 }
1097 then_b = gfc_finish_block (&cond_block);
1098
1099 gfc_init_block (&cond_block);
1100 if (GFC_DESCRIPTOR_TYPE_P (type))
1101 gfc_conv_descriptor_data_set (&cond_block, unshare_expr (dest),
1102 null_pointer_node);
1103 else
1104 gfc_add_modify (&cond_block, unshare_expr (dest),
1105 build_zero_cst (TREE_TYPE (dest)));
1106 else_b = gfc_finish_block (&cond_block);
1107
1108 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1109 unshare_expr (srcptr), null_pointer_node);
1110 gfc_add_expr_to_block (&block,
1111 build3_loc (input_location, COND_EXPR,
1112 void_type_node, cond, then_b, else_b));
1113 /* Avoid -W*uninitialized warnings. */
1114 if (DECL_P (dest))
1115 suppress_warning (dest, OPT_Wuninitialized);
1116
1117 return gfc_finish_block (&block);
1118 }
1119
1120 /* Similarly, except use an intrinsic or pointer assignment operator
1121 instead. */
1122
1123 tree
1124 gfc_omp_clause_assign_op (tree clause, tree dest, tree src)
1125 {
1126 tree type = TREE_TYPE (dest), ptr, size, call, nonalloc;
1127 tree cond, then_b, else_b;
1128 stmtblock_t block, cond_block, cond_block2, inner_block;
1129
1130 if ((! GFC_DESCRIPTOR_TYPE_P (type)
1131 || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
1132 && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
1133 || !POINTER_TYPE_P (type)))
1134 {
1135 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1136 {
1137 gfc_start_block (&block);
1138 /* First dealloc any allocatable components in DEST. */
1139 tree tem = gfc_walk_alloc_comps (dest, NULL_TREE,
1140 OMP_CLAUSE_DECL (clause),
1141 WALK_ALLOC_COMPS_DTOR);
1142 gfc_add_expr_to_block (&block, tem);
1143 /* Then copy over toplevel data. */
1144 gfc_add_modify (&block, dest, src);
1145 /* Finally allocate any allocatable components and copy. */
1146 tem = gfc_walk_alloc_comps (src, dest, OMP_CLAUSE_DECL (clause),
1147 WALK_ALLOC_COMPS_COPY_CTOR);
1148 gfc_add_expr_to_block (&block, tem);
1149 return gfc_finish_block (&block);
1150 }
1151 else
1152 return build2_v (MODIFY_EXPR, dest, src);
1153 }
1154
1155 gfc_start_block (&block);
1156
1157 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1158 {
1159 then_b = gfc_walk_alloc_comps (dest, NULL_TREE, OMP_CLAUSE_DECL (clause),
1160 WALK_ALLOC_COMPS_DTOR);
1161 tree tem = fold_convert (pvoid_type_node,
1162 GFC_DESCRIPTOR_TYPE_P (type)
1163 ? gfc_conv_descriptor_data_get (dest) : dest);
1164 tem = unshare_expr (tem);
1165 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1166 tem, null_pointer_node);
1167 tem = build3_loc (input_location, COND_EXPR, void_type_node, cond,
1168 then_b, build_empty_stmt (input_location));
1169 gfc_add_expr_to_block (&block, tem);
1170 }
1171
1172 gfc_init_block (&cond_block);
1173
1174 if (GFC_DESCRIPTOR_TYPE_P (type))
1175 {
1176 tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
1177 size = gfc_conv_descriptor_ubound_get (src, rank);
1178 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1179 size,
1180 gfc_conv_descriptor_lbound_get (src, rank));
1181 size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1182 size, gfc_index_one_node);
1183 if (GFC_TYPE_ARRAY_RANK (type) > 1)
1184 size = fold_build2_loc (input_location, MULT_EXPR,
1185 gfc_array_index_type, size,
1186 gfc_conv_descriptor_stride_get (src, rank));
1187 tree esize = fold_convert (gfc_array_index_type,
1188 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
1189 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1190 size, esize);
1191 size = unshare_expr (size);
1192 size = gfc_evaluate_now (fold_convert (size_type_node, size),
1193 &cond_block);
1194 }
1195 else
1196 size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
1197 ptr = gfc_create_var (pvoid_type_node, NULL);
1198
1199 tree destptr = GFC_DESCRIPTOR_TYPE_P (type)
1200 ? gfc_conv_descriptor_data_get (dest) : dest;
1201 destptr = unshare_expr (destptr);
1202 destptr = fold_convert (pvoid_type_node, destptr);
1203 gfc_add_modify (&cond_block, ptr, destptr);
1204
1205 nonalloc = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
1206 destptr, null_pointer_node);
1207 cond = nonalloc;
1208 if (GFC_DESCRIPTOR_TYPE_P (type))
1209 {
1210 int i;
1211 for (i = 0; i < GFC_TYPE_ARRAY_RANK (type); i++)
1212 {
1213 tree rank = gfc_rank_cst[i];
1214 tree tem = gfc_conv_descriptor_ubound_get (src, rank);
1215 tem = fold_build2_loc (input_location, MINUS_EXPR,
1216 gfc_array_index_type, tem,
1217 gfc_conv_descriptor_lbound_get (src, rank));
1218 tem = fold_build2_loc (input_location, PLUS_EXPR,
1219 gfc_array_index_type, tem,
1220 gfc_conv_descriptor_lbound_get (dest, rank));
1221 tem = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1222 tem, gfc_conv_descriptor_ubound_get (dest,
1223 rank));
1224 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
1225 logical_type_node, cond, tem);
1226 }
1227 }
1228
1229 gfc_init_block (&cond_block2);
1230
1231 if (GFC_DESCRIPTOR_TYPE_P (type))
1232 {
1233 gfc_init_block (&inner_block);
1234 gfc_allocate_using_malloc (&inner_block, ptr, size, NULL_TREE);
1235 then_b = gfc_finish_block (&inner_block);
1236
1237 gfc_init_block (&inner_block);
1238 gfc_add_modify (&inner_block, ptr,
1239 gfc_call_realloc (&inner_block, ptr, size));
1240 else_b = gfc_finish_block (&inner_block);
1241
1242 gfc_add_expr_to_block (&cond_block2,
1243 build3_loc (input_location, COND_EXPR,
1244 void_type_node,
1245 unshare_expr (nonalloc),
1246 then_b, else_b));
1247 gfc_add_modify (&cond_block2, dest, src);
1248 gfc_conv_descriptor_data_set (&cond_block2, unshare_expr (dest), ptr);
1249 }
1250 else
1251 {
1252 gfc_allocate_using_malloc (&cond_block2, ptr, size, NULL_TREE);
1253 gfc_add_modify (&cond_block2, unshare_expr (dest),
1254 fold_convert (type, ptr));
1255 }
1256 then_b = gfc_finish_block (&cond_block2);
1257 else_b = build_empty_stmt (input_location);
1258
1259 gfc_add_expr_to_block (&cond_block,
1260 build3_loc (input_location, COND_EXPR,
1261 void_type_node, unshare_expr (cond),
1262 then_b, else_b));
1263
1264 tree srcptr = GFC_DESCRIPTOR_TYPE_P (type)
1265 ? gfc_conv_descriptor_data_get (src) : src;
1266 srcptr = unshare_expr (srcptr);
1267 srcptr = fold_convert (pvoid_type_node, srcptr);
1268 call = build_call_expr_loc (input_location,
1269 builtin_decl_explicit (BUILT_IN_MEMCPY), 3, ptr,
1270 srcptr, size);
1271 gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
1272 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1273 {
1274 tree tem = gfc_walk_alloc_comps (src, dest,
1275 OMP_CLAUSE_DECL (clause),
1276 WALK_ALLOC_COMPS_COPY_CTOR);
1277 gfc_add_expr_to_block (&cond_block, tem);
1278 }
1279 then_b = gfc_finish_block (&cond_block);
1280
1281 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_COPYIN)
1282 {
1283 gfc_init_block (&cond_block);
1284 if (GFC_DESCRIPTOR_TYPE_P (type))
1285 {
1286 tree tmp = gfc_conv_descriptor_data_get (unshare_expr (dest));
1287 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
1288 NULL_TREE, NULL_TREE, true, NULL,
1289 GFC_CAF_COARRAY_NOCOARRAY);
1290 gfc_add_expr_to_block (&cond_block, tmp);
1291 }
1292 else
1293 {
1294 destptr = gfc_evaluate_now (destptr, &cond_block);
1295 gfc_add_expr_to_block (&cond_block, gfc_call_free (destptr));
1296 gfc_add_modify (&cond_block, unshare_expr (dest),
1297 build_zero_cst (TREE_TYPE (dest)));
1298 }
1299 else_b = gfc_finish_block (&cond_block);
1300
1301 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1302 unshare_expr (srcptr), null_pointer_node);
1303 gfc_add_expr_to_block (&block,
1304 build3_loc (input_location, COND_EXPR,
1305 void_type_node, cond,
1306 then_b, else_b));
1307 }
1308 else
1309 gfc_add_expr_to_block (&block, then_b);
1310
1311 return gfc_finish_block (&block);
1312 }
1313
1314 static void
1315 gfc_omp_linear_clause_add_loop (stmtblock_t *block, tree dest, tree src,
1316 tree add, tree nelems)
1317 {
1318 stmtblock_t tmpblock;
1319 tree desta, srca, index = gfc_create_var (gfc_array_index_type, "S");
1320 nelems = gfc_evaluate_now (nelems, block);
1321
1322 gfc_init_block (&tmpblock);
1323 if (TREE_CODE (TREE_TYPE (dest)) == ARRAY_TYPE)
1324 {
1325 desta = gfc_build_array_ref (dest, index, NULL);
1326 srca = gfc_build_array_ref (src, index, NULL);
1327 }
1328 else
1329 {
1330 gcc_assert (POINTER_TYPE_P (TREE_TYPE (dest)));
1331 tree idx = fold_build2 (MULT_EXPR, sizetype,
1332 fold_convert (sizetype, index),
1333 TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dest))));
1334 desta = build_fold_indirect_ref (fold_build2 (POINTER_PLUS_EXPR,
1335 TREE_TYPE (dest), dest,
1336 idx));
1337 srca = build_fold_indirect_ref (fold_build2 (POINTER_PLUS_EXPR,
1338 TREE_TYPE (src), src,
1339 idx));
1340 }
1341 gfc_add_modify (&tmpblock, desta,
1342 fold_build2 (PLUS_EXPR, TREE_TYPE (desta),
1343 srca, add));
1344
1345 gfc_loopinfo loop;
1346 gfc_init_loopinfo (&loop);
1347 loop.dimen = 1;
1348 loop.from[0] = gfc_index_zero_node;
1349 loop.loopvar[0] = index;
1350 loop.to[0] = nelems;
1351 gfc_trans_scalarizing_loops (&loop, &tmpblock);
1352 gfc_add_block_to_block (block, &loop.pre);
1353 }
1354
1355 /* Build and return code for a constructor of DEST that initializes
1356 it to SRC plus ADD (ADD is scalar integer). */
1357
1358 tree
1359 gfc_omp_clause_linear_ctor (tree clause, tree dest, tree src, tree add)
1360 {
1361 tree type = TREE_TYPE (dest), ptr, size, nelems = NULL_TREE;
1362 stmtblock_t block;
1363
1364 gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LINEAR);
1365
1366 gfc_start_block (&block);
1367 add = gfc_evaluate_now (add, &block);
1368
1369 if ((! GFC_DESCRIPTOR_TYPE_P (type)
1370 || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
1371 && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
1372 || !POINTER_TYPE_P (type)))
1373 {
1374 bool compute_nelts = false;
1375 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1376 if (!TYPE_DOMAIN (type)
1377 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE
1378 || TYPE_MIN_VALUE (TYPE_DOMAIN (type)) == error_mark_node
1379 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == error_mark_node)
1380 compute_nelts = true;
1381 else if (VAR_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1382 {
1383 tree a = DECL_ATTRIBUTES (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
1384 if (lookup_attribute ("omp dummy var", a))
1385 compute_nelts = true;
1386 }
1387 if (compute_nelts)
1388 {
1389 nelems = fold_build2 (EXACT_DIV_EXPR, sizetype,
1390 TYPE_SIZE_UNIT (type),
1391 TYPE_SIZE_UNIT (TREE_TYPE (type)));
1392 nelems = size_binop (MINUS_EXPR, nelems, size_one_node);
1393 }
1394 else
1395 nelems = array_type_nelts_minus_one (type);
1396 nelems = fold_convert (gfc_array_index_type, nelems);
1397
1398 gfc_omp_linear_clause_add_loop (&block, dest, src, add, nelems);
1399 return gfc_finish_block (&block);
1400 }
1401
1402 /* Allocatable arrays in LINEAR clauses need to be allocated
1403 and copied from SRC. */
1404 gfc_add_modify (&block, dest, src);
1405 if (GFC_DESCRIPTOR_TYPE_P (type))
1406 {
1407 tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
1408 size = gfc_conv_descriptor_ubound_get (dest, rank);
1409 size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
1410 size,
1411 gfc_conv_descriptor_lbound_get (dest, rank));
1412 size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1413 size, gfc_index_one_node);
1414 if (GFC_TYPE_ARRAY_RANK (type) > 1)
1415 size = fold_build2_loc (input_location, MULT_EXPR,
1416 gfc_array_index_type, size,
1417 gfc_conv_descriptor_stride_get (dest, rank));
1418 tree esize = fold_convert (gfc_array_index_type,
1419 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
1420 nelems = gfc_evaluate_now (unshare_expr (size), &block);
1421 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1422 nelems, unshare_expr (esize));
1423 size = gfc_evaluate_now (fold_convert (size_type_node, size),
1424 &block);
1425 nelems = fold_build2_loc (input_location, MINUS_EXPR,
1426 gfc_array_index_type, nelems,
1427 gfc_index_one_node);
1428 }
1429 else
1430 size = fold_convert (size_type_node, TYPE_SIZE_UNIT (TREE_TYPE (type)));
1431 ptr = gfc_create_var (pvoid_type_node, NULL);
1432 gfc_allocate_using_malloc (&block, ptr, size, NULL_TREE);
1433 if (GFC_DESCRIPTOR_TYPE_P (type))
1434 {
1435 gfc_conv_descriptor_data_set (&block, unshare_expr (dest), ptr);
1436 tree etype = gfc_get_element_type (type);
1437 ptr = fold_convert (build_pointer_type (etype), ptr);
1438 tree srcptr = gfc_conv_descriptor_data_get (unshare_expr (src));
1439 srcptr = fold_convert (build_pointer_type (etype), srcptr);
1440 gfc_omp_linear_clause_add_loop (&block, ptr, srcptr, add, nelems);
1441 }
1442 else
1443 {
1444 gfc_add_modify (&block, unshare_expr (dest),
1445 fold_convert (TREE_TYPE (dest), ptr));
1446 ptr = fold_convert (TREE_TYPE (dest), ptr);
1447 tree dstm = build_fold_indirect_ref (ptr);
1448 tree srcm = build_fold_indirect_ref (unshare_expr (src));
1449 gfc_add_modify (&block, dstm,
1450 fold_build2 (PLUS_EXPR, TREE_TYPE (add), srcm, add));
1451 }
1452 return gfc_finish_block (&block);
1453 }
1454
1455 /* Build and return code destructing DECL. Return NULL if nothing
1456 to be done. */
1457
1458 tree
1459 gfc_omp_clause_dtor (tree clause, tree decl)
1460 {
1461 tree type = TREE_TYPE (decl), tem;
1462 tree decl_type = TREE_TYPE (OMP_CLAUSE_DECL (clause));
1463 tree orig_decl = OMP_CLAUSE_DECL (clause);
1464
1465 if (DECL_ARTIFICIAL (orig_decl)
1466 && DECL_LANG_SPECIFIC (orig_decl)
1467 && GFC_DECL_SAVED_DESCRIPTOR (orig_decl))
1468 {
1469 orig_decl = GFC_DECL_SAVED_DESCRIPTOR (orig_decl);
1470 decl_type = TREE_TYPE (orig_decl);
1471 }
1472
1473 /* Only pointer association was privatized; cf. gfc_omp_clause_copy_ctor.
1474 Scalar class pointers must not finalize or free their targets here. */
1475 if (DECL_P (orig_decl)
1476 && (GFC_DECL_ASSOCIATE_VAR_P (orig_decl)
1477 || GFC_DECL_GET_SCALAR_POINTER (orig_decl)
1478 || gfc_is_class_pointer_type (decl_type)))
1479 return NULL_TREE;
1480 if (gfc_is_polymorphic_nonptr (decl_type))
1481 {
1482 if (POINTER_TYPE_P (decl_type))
1483 decl_type = TREE_TYPE (decl_type);
1484 decl_type = TREE_TYPE (TYPE_FIELDS (decl_type));
1485 if (GFC_DESCRIPTOR_TYPE_P (decl_type) || GFC_ARRAY_TYPE_P (decl_type))
1486 fatal_error (input_location,
1487 "Sorry, polymorphic arrays not yet supported for "
1488 "firstprivate");
1489 stmtblock_t block, cond_block;
1490 gfc_start_block (&block);
1491 gfc_init_block (&cond_block);
1492 tree final = gfc_class_vtab_final_get (decl);
1493 tree size = fold_convert (size_type_node, gfc_class_vtab_size_get (decl));
1494 gfc_se se;
1495 gfc_init_se (&se, NULL);
1496 symbol_attribute attr = {};
1497 tree data = gfc_class_data_get (decl);
1498 tree desc = gfc_conv_scalar_to_descriptor (&se, data, attr);
1499
1500 /* Call class->_vpt->_finalize + free. */
1501 tree call = build_fold_indirect_ref (final);
1502 call = build_call_expr_loc (input_location, call, 3,
1503 gfc_build_addr_expr (NULL, desc),
1504 size, boolean_false_node);
1505 gfc_add_block_to_block (&cond_block, &se.pre);
1506 gfc_add_expr_to_block (&cond_block, fold_convert (void_type_node, call));
1507 gfc_add_block_to_block (&cond_block, &se.post);
1508 /* Create: if (_vtab && _final) <cond_block> */
1509 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1510 gfc_class_vptr_get (decl),
1511 null_pointer_node);
1512 tree cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1513 final, null_pointer_node);
1514 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1515 boolean_type_node, cond, cond2);
1516 gfc_add_expr_to_block (&block, build3_loc (input_location, COND_EXPR,
1517 void_type_node, cond,
1518 gfc_finish_block (&cond_block), NULL_TREE));
1519 call = builtin_decl_explicit (BUILT_IN_FREE);
1520 call = build_call_expr_loc (input_location, call, 1, data);
1521 gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
1522 return gfc_finish_block (&block);
1523 }
1524
1525 if ((! GFC_DESCRIPTOR_TYPE_P (type)
1526 || GFC_TYPE_ARRAY_AKIND (type) != GFC_ARRAY_ALLOCATABLE)
1527 && (!GFC_DECL_GET_SCALAR_ALLOCATABLE (OMP_CLAUSE_DECL (clause))
1528 || !POINTER_TYPE_P (type)))
1529 {
1530 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1531 return gfc_walk_alloc_comps (decl, NULL_TREE,
1532 OMP_CLAUSE_DECL (clause),
1533 WALK_ALLOC_COMPS_DTOR);
1534 return NULL_TREE;
1535 }
1536
1537 if (GFC_DESCRIPTOR_TYPE_P (type))
1538 {
1539 /* Allocatable arrays in FIRSTPRIVATE/LASTPRIVATE etc. clauses need
1540 to be deallocated if they were allocated. */
1541 tem = gfc_conv_descriptor_data_get (decl);
1542 tem = gfc_deallocate_with_status (tem, NULL_TREE, NULL_TREE, NULL_TREE,
1543 NULL_TREE, true, NULL,
1544 GFC_CAF_COARRAY_NOCOARRAY);
1545 }
1546 else
1547 tem = gfc_call_free (decl);
1548 tem = gfc_omp_unshare_expr (tem);
1549
1550 if (gfc_has_alloc_comps (type, OMP_CLAUSE_DECL (clause), false))
1551 {
1552 stmtblock_t block;
1553 tree then_b;
1554
1555 gfc_init_block (&block);
1556 gfc_add_expr_to_block (&block,
1557 gfc_walk_alloc_comps (decl, NULL_TREE,
1558 OMP_CLAUSE_DECL (clause),
1559 WALK_ALLOC_COMPS_DTOR));
1560 gfc_add_expr_to_block (&block, tem);
1561 then_b = gfc_finish_block (&block);
1562
1563 tem = fold_convert (pvoid_type_node,
1564 GFC_DESCRIPTOR_TYPE_P (type)
1565 ? gfc_conv_descriptor_data_get (decl) : decl);
1566 tem = unshare_expr (tem);
1567 tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1568 tem, null_pointer_node);
1569 tem = build3_loc (input_location, COND_EXPR, void_type_node, cond,
1570 then_b, build_empty_stmt (input_location));
1571 }
1572 return tem;
1573 }
1574
1575 /* Build a conditional expression in BLOCK. If COND_VAL is not
1576 null, then the block THEN_B is executed, otherwise ELSE_VAL
1577 is assigned to VAL. */
1578
1579 static void
1580 gfc_build_cond_assign (stmtblock_t *block, tree val, tree cond_val,
1581 tree then_b, tree else_val)
1582 {
1583 stmtblock_t cond_block;
1584 tree else_b = NULL_TREE;
1585 tree val_ty = TREE_TYPE (val);
1586
1587 if (else_val)
1588 {
1589 gfc_init_block (&cond_block);
1590 gfc_add_modify (&cond_block, val, fold_convert (val_ty, else_val));
1591 else_b = gfc_finish_block (&cond_block);
1592 }
1593 gfc_add_expr_to_block (block,
1594 build3_loc (input_location, COND_EXPR, void_type_node,
1595 cond_val, then_b, else_b));
1596 }
1597
1598 /* Build a conditional expression in BLOCK, returning a temporary
1599 variable containing the result. If COND_VAL is not null, then
1600 THEN_VAL will be assigned to the variable, otherwise ELSE_VAL
1601 is assigned.
1602 */
1603
1604 static tree
1605 gfc_build_cond_assign_expr (stmtblock_t *block, tree cond_val,
1606 tree then_val, tree else_val)
1607 {
1608 tree val;
1609 tree val_ty = TREE_TYPE (then_val);
1610 stmtblock_t cond_block;
1611
1612 val = create_tmp_var (val_ty);
1613
1614 gfc_init_block (&cond_block);
1615 gfc_add_modify (&cond_block, val, then_val);
1616 tree then_b = gfc_finish_block (&cond_block);
1617
1618 gfc_build_cond_assign (block, val, cond_val, then_b, else_val);
1619
1620 return val;
1621 }
1622
1623 void
1624 gfc_omp_finish_clause (tree c, gimple_seq *pre_p, bool openacc)
1625 {
1626 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
1627 return;
1628
1629 tree decl = OMP_CLAUSE_DECL (c);
1630 location_t loc = OMP_CLAUSE_LOCATION (c);
1631
1632 /* Assumed-size arrays can't be mapped implicitly, they have to be
1633 mapped explicitly using array sections. */
1634 if (TREE_CODE (decl) == PARM_DECL
1635 && GFC_ARRAY_TYPE_P (TREE_TYPE (decl))
1636 && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (decl)) == GFC_ARRAY_UNKNOWN
1637 && GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (decl),
1638 GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)) - 1)
1639 == NULL)
1640 {
1641 error_at (OMP_CLAUSE_LOCATION (c),
1642 "implicit mapping of assumed size array %qD", decl);
1643 return;
1644 }
1645
1646 tree c2 = NULL_TREE, c3 = NULL_TREE, c4 = NULL_TREE;
1647 tree present = gfc_omp_check_optional_argument (decl, true);
1648 tree orig_decl = NULL_TREE;
1649 if (POINTER_TYPE_P (TREE_TYPE (decl)))
1650 {
1651 if (!gfc_omp_privatize_by_reference (decl)
1652 && !GFC_DECL_GET_SCALAR_POINTER (decl)
1653 && !GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
1654 && !GFC_DECL_CRAY_POINTEE (decl)
1655 && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
1656 return;
1657 orig_decl = decl;
1658
1659 c4 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
1660 OMP_CLAUSE_SET_MAP_KIND (c4, GOMP_MAP_POINTER);
1661 OMP_CLAUSE_DECL (c4) = decl;
1662 OMP_CLAUSE_SIZE (c4) = size_int (0);
1663 decl = build_fold_indirect_ref (decl);
1664 if (present
1665 && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
1666 || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
1667 {
1668 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
1669 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
1670 OMP_CLAUSE_DECL (c2) = unshare_expr (decl);
1671 OMP_CLAUSE_SIZE (c2) = size_int (0);
1672
1673 stmtblock_t block;
1674 gfc_start_block (&block);
1675 tree ptr = gfc_build_cond_assign_expr (&block, present,
1676 unshare_expr (decl),
1677 null_pointer_node);
1678 gimplify_and_add (gfc_finish_block (&block), pre_p);
1679 ptr = build_fold_indirect_ref (ptr);
1680 OMP_CLAUSE_DECL (c) = ptr;
1681 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (ptr));
1682 }
1683 else
1684 {
1685 OMP_CLAUSE_DECL (c) = decl;
1686 OMP_CLAUSE_SIZE (c) = NULL_TREE;
1687 }
1688 if (TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE
1689 && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
1690 || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
1691 {
1692 c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
1693 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
1694 OMP_CLAUSE_DECL (c3) = decl;
1695 OMP_CLAUSE_SIZE (c3) = size_int (0);
1696 decl = build_fold_indirect_ref (decl);
1697 OMP_CLAUSE_DECL (c) = unshare_expr (decl);
1698 }
1699 }
1700 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
1701 {
1702 stmtblock_t block;
1703 gfc_start_block (&block);
1704 tree type = TREE_TYPE (decl);
1705 tree ptr = gfc_conv_descriptor_data_get (decl);
1706
1707 /* OpenMP: automatically map pointer targets with the pointer;
1708 hence, always update the descriptor/pointer itself.
1709 NOTE: This also remaps the pointer for allocatable arrays with
1710 'target' attribute which also don't have the 'restrict' qualifier. */
1711 bool always_modifier = false;
1712
1713 if (!openacc
1714 && !(TYPE_QUALS (TREE_TYPE (ptr)) & TYPE_QUAL_RESTRICT))
1715 always_modifier = true;
1716
1717 if (present)
1718 ptr = gfc_build_cond_assign_expr (&block, present, ptr,
1719 null_pointer_node);
1720 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
1721 ptr = build_fold_indirect_ref (ptr);
1722 OMP_CLAUSE_DECL (c) = ptr;
1723 c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
1724 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_TO_PSET);
1725 if (present)
1726 {
1727 ptr = create_tmp_var (TREE_TYPE (TREE_OPERAND (decl, 0)));
1728 gfc_add_modify (&block, ptr, TREE_OPERAND (decl, 0));
1729
1730 OMP_CLAUSE_DECL (c2) = build_fold_indirect_ref (ptr);
1731 }
1732 else
1733 OMP_CLAUSE_DECL (c2) = decl;
1734 OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (type);
1735 c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
1736 OMP_CLAUSE_SET_MAP_KIND (c3, always_modifier ? GOMP_MAP_ALWAYS_POINTER
1737 : GOMP_MAP_POINTER);
1738 if (present)
1739 {
1740 ptr = gfc_conv_descriptor_data_get (unshare_expr (decl));
1741 ptr = gfc_build_addr_expr (NULL, ptr);
1742 ptr = gfc_build_cond_assign_expr (&block, present,
1743 ptr, null_pointer_node);
1744 ptr = build_fold_indirect_ref (ptr);
1745 OMP_CLAUSE_DECL (c3) = ptr;
1746 }
1747 else
1748 OMP_CLAUSE_DECL (c3) = gfc_conv_descriptor_data_get (decl);
1749 OMP_CLAUSE_SIZE (c3) = size_int (0);
1750 tree size = create_tmp_var (gfc_array_index_type);
1751 tree elemsz = TYPE_SIZE_UNIT (gfc_get_element_type (type));
1752 elemsz = fold_convert (gfc_array_index_type, elemsz);
1753
1754 if (orig_decl == NULL_TREE)
1755 orig_decl = decl;
1756 if (!openacc
1757 && gfc_has_alloc_comps (type, orig_decl, true))
1758 {
1759 /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
1760 force evaluate to ensure that it is not gimplified + is a decl. */
1761 gfc_allocate_lang_decl (size);
1762 GFC_DECL_SAVED_DESCRIPTOR (size) = orig_decl;
1763 }
1764 enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
1765 if (akind == GFC_ARRAY_ALLOCATABLE
1766 || akind == GFC_ARRAY_POINTER
1767 || akind == GFC_ARRAY_POINTER_CONT
1768 || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
1769 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
1770 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
1771 {
1772 stmtblock_t cond_block;
1773 tree tem, then_b, else_b, zero, cond;
1774
1775 int rank = ((akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
1776 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
1777 || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
1778 ? -1 : GFC_TYPE_ARRAY_RANK (type));
1779 gfc_init_block (&cond_block);
1780 tem = gfc_full_array_size (&cond_block, unshare_expr (decl), rank);
1781 gfc_add_modify (&cond_block, size, tem);
1782 gfc_add_modify (&cond_block, size,
1783 fold_build2 (MULT_EXPR, gfc_array_index_type,
1784 size, elemsz));
1785 then_b = gfc_finish_block (&cond_block);
1786 gfc_init_block (&cond_block);
1787 zero = build_int_cst (gfc_array_index_type, 0);
1788 gfc_add_modify (&cond_block, size, zero);
1789 else_b = gfc_finish_block (&cond_block);
1790 tem = gfc_conv_descriptor_data_get (unshare_expr (decl));
1791 tem = fold_convert (pvoid_type_node, tem);
1792 cond = fold_build2_loc (loc, NE_EXPR,
1793 boolean_type_node, tem, null_pointer_node);
1794 if (present)
1795 {
1796 cond = fold_build2_loc (loc, TRUTH_ANDIF_EXPR,
1797 boolean_type_node, present, cond);
1798 }
1799 gfc_add_expr_to_block (&block, build3_loc (loc, COND_EXPR,
1800 void_type_node, cond,
1801 then_b, else_b));
1802 }
1803 else if (present)
1804 {
1805 stmtblock_t cond_block;
1806 tree then_b;
1807
1808 int rank = ((akind == GFC_ARRAY_ASSUMED_RANK
1809 || akind == GFC_ARRAY_ASSUMED_RANK_CONT)
1810 ? -1 : GFC_TYPE_ARRAY_RANK (type));
1811 gfc_init_block (&cond_block);
1812 gfc_add_modify (&cond_block, size,
1813 gfc_full_array_size (&cond_block, unshare_expr (decl),
1814 rank));
1815 gfc_add_modify (&cond_block, size,
1816 fold_build2 (MULT_EXPR, gfc_array_index_type,
1817 size, elemsz));
1818 then_b = gfc_finish_block (&cond_block);
1819
1820 gfc_build_cond_assign (&block, size, present, then_b,
1821 build_int_cst (gfc_array_index_type, 0));
1822 }
1823 else
1824 {
1825 int rank = ((akind == GFC_ARRAY_ASSUMED_RANK
1826 || akind == GFC_ARRAY_ASSUMED_RANK_CONT)
1827 ? -1 : GFC_TYPE_ARRAY_RANK (type));
1828 gfc_add_modify (&block, size,
1829 gfc_full_array_size (&block, unshare_expr (decl),
1830 rank));
1831 gfc_add_modify (&block, size,
1832 fold_build2 (MULT_EXPR, gfc_array_index_type,
1833 size, elemsz));
1834 }
1835 OMP_CLAUSE_SIZE (c) = size;
1836 tree stmt = gfc_finish_block (&block);
1837 gimplify_and_add (stmt, pre_p);
1838 }
1839 else
1840 {
1841 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
1842 {
1843 if (DECL_P (decl))
1844 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
1845 else
1846 {
1847 tree type = TREE_TYPE (decl);
1848 tree size = TYPE_SIZE_UNIT (type);
1849 /* For variable-length character types, TYPE_SIZE_UNIT is a
1850 SAVE_EXPR. Gimplifying the SAVE_EXPR (here or elsewhere)
1851 resolves it in place, embedding a gimple temporary that
1852 later causes an ICE in remap_type during inlining because
1853 the temporary is not in scope (PR101760, PR102314).
1854 Compute the size from the array domain and element size
1855 to decouple completely from the type's SAVE_EXPRs. */
1856 if (size
1857 && TREE_CODE (type) == ARRAY_TYPE
1858 && TYPE_DOMAIN (type)
1859 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
1860 && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1861 {
1862 tree len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1863 tree lb = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
1864 tree eltsz = TYPE_SIZE_UNIT (TREE_TYPE (type));
1865 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len, lb);
1866 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len,
1867 build_one_cst (TREE_TYPE (len)));
1868 size = fold_build2 (MULT_EXPR, sizetype,
1869 fold_convert (sizetype, len),
1870 fold_convert (sizetype, eltsz));
1871 }
1872 OMP_CLAUSE_SIZE (c) = size;
1873 }
1874 }
1875
1876 tree type = TREE_TYPE (decl);
1877 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
1878 type = TREE_TYPE (type);
1879 if (!openacc
1880 && orig_decl != NULL_TREE
1881 && gfc_has_alloc_comps (type, orig_decl, true))
1882 {
1883 /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
1884 force evaluate to ensure that it is not gimplified + is a decl. */
1885 tree size = create_tmp_var (TREE_TYPE (OMP_CLAUSE_SIZE (c)));
1886 gfc_allocate_lang_decl (size);
1887 GFC_DECL_SAVED_DESCRIPTOR (size) = orig_decl;
1888 gimplify_assign (size, OMP_CLAUSE_SIZE (c), pre_p);
1889 OMP_CLAUSE_SIZE (c) = size;
1890 }
1891 }
1892 tree last = c;
1893 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
1894 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
1895 OMP_CLAUSE_SIZE (c) = size_int (0);
1896 if (c2)
1897 {
1898 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (last);
1899 OMP_CLAUSE_CHAIN (last) = c2;
1900 last = c2;
1901 }
1902 if (c3)
1903 {
1904 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (last);
1905 OMP_CLAUSE_CHAIN (last) = c3;
1906 last = c3;
1907 }
1908 if (c4)
1909 {
1910 OMP_CLAUSE_CHAIN (c4) = OMP_CLAUSE_CHAIN (last);
1911 OMP_CLAUSE_CHAIN (last) = c4;
1912 }
1913 }
1914
1915
1916 /* map(<flag>: data [len: <size>])
1917 map(attach: &data [bias: <bias>])
1918 offset += 2; offset_data += 2 */
1919 static void
1920 gfc_omp_deep_mapping_map (tree data, tree size, unsigned HOST_WIDE_INT tkind,
1921 location_t loc, tree data_array, tree sizes_array,
1922 tree kinds_array, tree offset_data, tree offset,
1923 gimple_seq *seq, const gimple *ctx)
1924 {
1925 tree one = build_int_cst (size_type_node, 1);
1926
1927 STRIP_NOPS (data);
1928 if (!POINTER_TYPE_P (TREE_TYPE (data)))
1929 {
1930 gcc_assert (TREE_CODE (data) == INDIRECT_REF);
1931 data = TREE_OPERAND (data, 0);
1932 }
1933
1934 /* data_array[offset_data] = data; */
1935 tree tmp = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (data_array)),
1936 unshare_expr (data_array), offset_data,
1937 NULL_TREE, NULL_TREE);
1938 gimplify_assign (tmp, data, seq);
1939
1940 /* offset_data++ */
1941 tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset_data, one);
1942 gimplify_assign (offset_data, tmp, seq);
1943
1944 /* data_array[offset_data] = &data; */
1945 tmp = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (data_array)),
1946 unshare_expr (data_array),
1947 offset_data, NULL_TREE, NULL_TREE);
1948 gimplify_assign (tmp, build_fold_addr_expr (data), seq);
1949
1950 /* offset_data++ */
1951 tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset_data, one);
1952 gimplify_assign (offset_data, tmp, seq);
1953
1954 /* sizes_array[offset] = size */
1955 tmp = build2_loc (loc, MULT_EXPR, size_type_node,
1956 TYPE_SIZE_UNIT (size_type_node), offset);
1957 tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (sizes_array),
1958 sizes_array, tmp);
1959 gimple_seq seq2 = NULL;
1960 tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
1961 gimple_seq_add_seq (seq, seq2);
1962 tmp = build_fold_indirect_ref_loc (loc, tmp);
1963 gimplify_assign (tmp, size, seq);
1964
1965 /* FIXME: tkind |= talign << talign_shift; */
1966 /* kinds_array[offset] = tkind. */
1967 tmp = build2_loc (loc, MULT_EXPR, size_type_node,
1968 TYPE_SIZE_UNIT (short_unsigned_type_node), offset);
1969 tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (kinds_array),
1970 kinds_array, tmp);
1971 seq2 = NULL;
1972 tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
1973 gimple_seq_add_seq (seq, seq2);
1974 tmp = build_fold_indirect_ref_loc (loc, tmp);
1975 gimplify_assign (tmp, build_int_cst (short_unsigned_type_node, tkind), seq);
1976
1977 /* offset++ */
1978 tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset, one);
1979 gimplify_assign (offset, tmp, seq);
1980
1981 /* sizes_array[offset] = bias (= 0). */
1982 tmp = build2_loc (loc, MULT_EXPR, size_type_node,
1983 TYPE_SIZE_UNIT (size_type_node), offset);
1984 tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (sizes_array),
1985 sizes_array, tmp);
1986 seq2 = NULL;
1987 tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
1988 gimple_seq_add_seq (seq, seq2);
1989 tmp = build_fold_indirect_ref_loc (loc, tmp);
1990 gimplify_assign (tmp, build_zero_cst (size_type_node), seq);
1991
1992 gcc_assert (gimple_code (ctx) == GIMPLE_OMP_TARGET);
1993 tkind = (gimple_omp_target_kind (ctx) == GF_OMP_TARGET_KIND_EXIT_DATA
1994 ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH);
1995
1996 /* kinds_array[offset] = tkind. */
1997 tmp = build2_loc (loc, MULT_EXPR, size_type_node,
1998 TYPE_SIZE_UNIT (short_unsigned_type_node), offset);
1999 tmp = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (kinds_array),
2000 kinds_array, tmp);
2001 seq2 = NULL;
2002 tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
2003 gimple_seq_add_seq (seq, seq2);
2004 tmp = build_fold_indirect_ref_loc (loc, tmp);
2005 gimplify_assign (tmp, build_int_cst (short_unsigned_type_node, tkind), seq);
2006
2007 /* offset++ */
2008 tmp = build2_loc (loc, PLUS_EXPR, size_type_node, offset, one);
2009 gimplify_assign (offset, tmp, seq);
2010 }
2011
2012 static void gfc_omp_deep_mapping_item (bool, bool, bool, location_t, tree,
2013 tree *, unsigned HOST_WIDE_INT, tree,
2014 tree, tree, tree, tree, tree,
2015 gimple_seq *, const gimple *, bool *);
2016
2017 /* Map allocatable components. */
2018 static void
2019 gfc_omp_deep_mapping_comps (bool is_cnt, location_t loc, tree decl,
2020 tree *token, unsigned HOST_WIDE_INT tkind,
2021 tree data_array, tree sizes_array, tree kinds_array,
2022 tree offset_data, tree offset, tree num,
2023 gimple_seq *seq, const gimple *ctx,
2024 bool *poly_warned)
2025 {
2026 tree type = TREE_TYPE (decl);
2027 if (TREE_CODE (type) != RECORD_TYPE)
2028 return;
2029 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2030 {
2031 type = TREE_TYPE (field);
2032 if (gfc_is_polymorphic_nonptr (type)
2033 || GFC_DECL_GET_SCALAR_ALLOCATABLE (field)
2034 || (GFC_DESCRIPTOR_TYPE_P (type)
2035 && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE))
2036 {
2037 tree tmp = fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
2038 decl, field, NULL_TREE);
2039 gfc_omp_deep_mapping_item (is_cnt, true, true, loc, tmp, token,
2040 tkind, data_array, sizes_array,
2041 kinds_array, offset_data, offset, num,
2042 seq, ctx, poly_warned);
2043 }
2044 else if (GFC_DECL_GET_SCALAR_POINTER (field)
2045 || GFC_DESCRIPTOR_TYPE_P (type))
2046 continue;
2047 else if (gfc_has_alloc_comps (TREE_TYPE (field), field, false))
2048 {
2049 tree tmp = fold_build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
2050 decl, field, NULL_TREE);
2051 if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2052 gfc_omp_deep_mapping_item (is_cnt, false, false, loc, tmp,
2053 token, tkind, data_array, sizes_array,
2054 kinds_array, offset_data, offset, num,
2055 seq, ctx, poly_warned);
2056 else
2057 gfc_omp_deep_mapping_comps (is_cnt, loc, tmp, token, tkind,
2058 data_array, sizes_array, kinds_array,
2059 offset_data, offset, num, seq, ctx,
2060 poly_warned);
2061 }
2062 }
2063 }
2064
2065 static void
2066 gfc_omp_gen_simple_loop (tree var, tree begin, tree end, enum tree_code cond,
2067 tree step, location_t loc, gimple_seq *seq1,
2068 gimple_seq *seq2)
2069 {
2070 tree tmp;
2071
2072 /* var = begin. */
2073 gimplify_assign (var, begin, seq1);
2074
2075 /* Loop: for (var = begin; var <cond> end; var += step). */
2076 tree label_loop = create_artificial_label (loc);
2077 tree label_cond = create_artificial_label (loc);
2078
2079 gimplify_and_add (fold_build1_loc (loc, GOTO_EXPR, void_type_node,
2080 label_cond), seq1);
2081 gimple_seq_add_stmt (seq1, gimple_build_label (label_loop));
2082
2083 /* Everything above is seq1; place loop body here. */
2084
2085 /* End of loop body -> put into seq2. */
2086 tmp = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (var), var, step);
2087 gimplify_assign (var, tmp, seq2);
2088 gimple_seq_add_stmt (seq2, gimple_build_label (label_cond));
2089 tmp = fold_build2_loc (loc, cond, boolean_type_node, var, end);
2090 tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
2091 build_empty_stmt (loc));
2092 gimplify_and_add (tmp, seq2);
2093 }
2094
2095 /* Return size variable with the size of an array. */
2096 static tree
2097 gfc_omp_get_array_size (location_t loc, tree desc, gimple_seq *seq)
2098 {
2099 tree tmp;
2100 gimple_seq seq1 = NULL, seq2 = NULL;
2101 tree size = build_decl (loc, VAR_DECL, create_tmp_var_name ("size"),
2102 size_type_node);
2103 tree extent = build_decl (loc, VAR_DECL, create_tmp_var_name ("extent"),
2104 gfc_array_index_type);
2105 tree idx = build_decl (loc, VAR_DECL, create_tmp_var_name ("idx"),
2106 signed_char_type_node);
2107
2108 tree begin = build_zero_cst (signed_char_type_node);
2109 tree end;
2110 if (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc)) == GFC_ARRAY_ASSUMED_SHAPE_CONT
2111 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (desc)) == GFC_ARRAY_ASSUMED_SHAPE)
2112 end = gfc_conv_descriptor_rank (desc);
2113 else
2114 end = build_int_cst (signed_char_type_node,
2115 GFC_TYPE_ARRAY_RANK (TREE_TYPE (desc)));
2116 tree step = build_int_cst (signed_char_type_node, 1);
2117
2118 /* size = 0
2119 for (idx = 0; idx < rank; idx++)
2120 extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1
2121 if (extent < 0) extent = 0
2122 size *= extent. */
2123 gimplify_assign (size, build_int_cst (size_type_node, 1), seq);
2124
2125 gfc_omp_gen_simple_loop (idx, begin, end, LT_EXPR, step, loc, &seq1, &seq2);
2126 gimple_seq_add_seq (seq, seq1);
2127
2128 tmp = fold_build2_loc (loc, MINUS_EXPR, gfc_array_index_type,
2129 gfc_conv_descriptor_ubound_get (desc, idx),
2130 gfc_conv_descriptor_lbound_get (desc, idx));
2131 tmp = fold_build2_loc (loc, PLUS_EXPR, gfc_array_index_type,
2132 tmp, gfc_index_one_node);
2133 gimplify_assign (extent, tmp, seq);
2134 tmp = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
2135 extent, gfc_index_zero_node);
2136 tmp = build3_v (COND_EXPR, tmp,
2137 fold_build2_loc (loc, MODIFY_EXPR,
2138 gfc_array_index_type,
2139 extent, gfc_index_zero_node),
2140 build_empty_stmt (loc));
2141 gimplify_and_add (tmp, seq);
2142 /* size *= extent. */
2143 gimplify_assign (size, fold_build2_loc (loc, MULT_EXPR, size_type_node, size,
2144 fold_convert (size_type_node,
2145 extent)), seq);
2146 gimple_seq_add_seq (seq, seq2);
2147 return size;
2148 }
2149
2150 /* Generate loop to access every array element; takes addr of first element
2151 (decl's data comp); returns loop code in seq1 + seq2
2152 and the pointer to the element as return value. */
2153 static tree
2154 gfc_omp_elmental_loop (location_t loc, tree decl, tree size, tree elem_len,
2155 gimple_seq *seq1, gimple_seq *seq2)
2156 {
2157 tree idx = build_decl (loc, VAR_DECL, create_tmp_var_name ("idx"),
2158 size_type_node);
2159 tree begin = build_zero_cst (size_type_node);
2160 tree end = size;
2161 tree step = build_int_cst (size_type_node, 1);
2162 tree ptr;
2163
2164 gfc_omp_gen_simple_loop (idx, begin, end, LT_EXPR, step, loc, seq1, seq2);
2165
2166 tree type = TREE_TYPE (decl);
2167 if (POINTER_TYPE_P (type))
2168 {
2169 type = TREE_TYPE (type);
2170 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2171 decl = fold_convert (build_pointer_type (TREE_TYPE (type)), decl);
2172 }
2173 else
2174 {
2175 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2176 decl = build_fold_addr_expr_loc (loc, decl);
2177 }
2178 decl = fold_convert (build_pointer_type (TREE_TYPE (type)), decl);
2179 tree tmp = build2_loc (loc, MULT_EXPR, size_type_node, idx,
2180 fold_convert (size_type_node, elem_len));
2181 ptr = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (decl), decl, tmp);
2182 gimple_seq seq3 = NULL;
2183 ptr = force_gimple_operand (ptr, &seq3, true, NULL_TREE);
2184 gimple_seq_add_seq (seq1, seq3);
2185
2186 return ptr;
2187 }
2188
2189
2190 /* If do_copy, copy data pointer and vptr (if applicable) as well.
2191 Otherwise, only handle allocatable components.
2192 do_copy == false can happen only with nonpolymorphic arguments
2193 to a copy clause.
2194 if (is_cnt) token ... offset is ignored and num is used, otherwise
2195 num is NULL_TREE and unused. */
2196
2197 static void
2198 gfc_omp_deep_mapping_item (bool is_cnt, bool do_copy, bool do_alloc_check,
2199 location_t loc, tree decl, tree *token,
2200 unsigned HOST_WIDE_INT tkind, tree data_array,
2201 tree sizes_array, tree kinds_array, tree offset_data,
2202 tree offset, tree num, gimple_seq *seq,
2203 const gimple *ctx, bool *poly_warned)
2204 {
2205 tree tmp;
2206 tree type = TREE_TYPE (decl);
2207 if (POINTER_TYPE_P (type))
2208 type = TREE_TYPE (type);
2209 tree end_label = NULL_TREE;
2210 tree size = NULL_TREE, elem_len = NULL_TREE;
2211
2212 bool poly = gfc_is_polymorphic_nonptr (type);
2213 if (poly && is_cnt && !*poly_warned)
2214 {
2215 if (gfc_is_unlimited_polymorphic_nonptr (type))
2216 error_at (loc,
2217 "Mapping of unlimited polymorphic list item %qD is "
2218 "unspecified behavior and unsupported", decl);
2219
2220 else
2221 warning_at (loc, OPT_Wopenmp,
2222 "Mapping of polymorphic list item %qD is "
2223 "unspecified behavior", decl);
2224 *poly_warned = true;
2225 }
2226 if (do_alloc_check)
2227 {
2228 tree then_label = create_artificial_label (loc);
2229 end_label = create_artificial_label (loc);
2230 tmp = decl;
2231 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE
2232 || (POINTER_TYPE_P (TREE_TYPE (tmp))
2233 && (POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
2234 || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (tmp))))))
2235 tmp = build_fold_indirect_ref_loc (loc, tmp);
2236 if (poly)
2237 tmp = gfc_class_data_get (tmp);
2238 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2239 tmp = gfc_conv_descriptor_data_get (tmp);
2240 gimple_seq seq2 = NULL;
2241 tmp = force_gimple_operand (tmp, &seq2, true, NULL_TREE);
2242 gimple_seq_add_seq (seq, seq2);
2243
2244 gimple_seq_add_stmt (seq,
2245 gimple_build_cond (NE_EXPR, tmp, null_pointer_node,
2246 then_label, end_label));
2247 gimple_seq_add_stmt (seq, gimple_build_label (then_label));
2248 }
2249 tree class_decl = decl;
2250 if (poly)
2251 {
2252 decl = gfc_class_data_get (decl);
2253 type = TREE_TYPE (decl);
2254 }
2255 if (POINTER_TYPE_P (TREE_TYPE (decl)))
2256 {
2257 decl = build_fold_indirect_ref (decl);
2258 type = TREE_TYPE (decl);
2259 }
2260
2261 if (is_cnt && do_copy)
2262 {
2263 tree tmp = fold_build2_loc (loc, PLUS_EXPR, size_type_node,
2264 num, build_int_cst (size_type_node, 1));
2265 gimplify_assign (num, tmp, seq);
2266 }
2267 else if (do_copy)
2268 {
2269 /* copy data pointer */
2270 tree bytesize;
2271 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
2272 {
2273 /* TODO: Optimization: Shouldn't this be an expr. const, except for
2274 deferred-length strings. (Cf. also below). */
2275 elem_len = (poly ? gfc_class_vtab_size_get (class_decl)
2276 : gfc_conv_descriptor_elem_len (decl));
2277 tmp = (POINTER_TYPE_P (TREE_TYPE (decl))
2278 ? build_fold_indirect_ref (decl) : decl);
2279 size = gfc_omp_get_array_size (loc, tmp, seq);
2280 bytesize = fold_build2_loc (loc, MULT_EXPR, size_type_node,
2281 fold_convert (size_type_node, size),
2282 fold_convert (size_type_node, elem_len));
2283 tmp = gfc_conv_descriptor_data_get (decl);
2284 }
2285 else if (poly)
2286 {
2287 tmp = decl;
2288 bytesize = fold_convert (size_type_node,
2289 gfc_class_vtab_size_get (class_decl));
2290 }
2291 else
2292 {
2293 tmp = decl;
2294 bytesize = TYPE_SIZE_UNIT (TREE_TYPE (decl));
2295 }
2296 unsigned HOST_WIDE_INT tkind2 = tkind;
2297 if (!is_cnt
2298 && (tkind == GOMP_MAP_ALLOC
2299 || (tkind == GOMP_MAP_FROM
2300 && (gimple_omp_target_kind (ctx)
2301 != GF_OMP_TARGET_KIND_EXIT_DATA)))
2302 && gfc_omp_replace_alloc_by_to_mapping (TREE_TYPE (decl), decl, true))
2303 tkind2 = tkind == GOMP_MAP_ALLOC ? GOMP_MAP_TO : GOMP_MAP_TOFROM;
2304
2305 gfc_omp_deep_mapping_map (tmp, bytesize, tkind2, loc, data_array,
2306 sizes_array, kinds_array, offset_data,
2307 offset, seq, ctx);
2308 }
2309
2310 tmp = decl;
2311 if (POINTER_TYPE_P (TREE_TYPE (decl)))
2312 while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
2313 tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
2314 if (poly || gfc_has_alloc_comps (type, tmp, true))
2315 {
2316 gimple_seq seq2 = NULL;
2317 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
2318 {
2319 if (elem_len == NULL_TREE)
2320 {
2321 elem_len = gfc_conv_descriptor_elem_len (decl);
2322 size = fold_convert (size_type_node,
2323 gfc_omp_get_array_size (loc, decl, seq));
2324 }
2325 decl = gfc_conv_descriptor_data_get (decl);
2326 decl = gfc_omp_elmental_loop (loc, decl, size, elem_len, seq, &seq2);
2327 decl = build_fold_indirect_ref_loc (loc, decl);
2328 }
2329 else if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2330 {
2331 type = TREE_TYPE (tmp);
2332 /* FIXME: PR95868 - for var%str of deferred length, elem_len == 0;
2333 len is stored as var%_str_length, but not in GFC_DECL_STRING_LEN
2334 nor in TYPE_SIZE_UNIT as expression. */
2335 elem_len = TYPE_SIZE_UNIT (TREE_TYPE (type));
2336 size = fold_convert (size_type_node, GFC_TYPE_ARRAY_SIZE (type));
2337 decl = gfc_omp_elmental_loop (loc, decl, size, elem_len, seq, &seq2);
2338 decl = build_fold_indirect_ref_loc (loc, decl);
2339 }
2340 else if (POINTER_TYPE_P (TREE_TYPE (decl)))
2341 decl = build_fold_indirect_ref (decl);
2342
2343 gfc_omp_deep_mapping_comps (is_cnt, loc, decl, token, tkind,
2344 data_array, sizes_array, kinds_array,
2345 offset_data, offset, num, seq, ctx,
2346 poly_warned);
2347 gimple_seq_add_seq (seq, seq2);
2348 }
2349 if (end_label)
2350 gimple_seq_add_stmt (seq, gimple_build_label (end_label));
2351 }
2352
2353
2354 /* Which map types to check/handle for deep mapping. */
2355 static bool
2356 gfc_omp_deep_map_kind_p (tree clause)
2357 {
2358 switch (OMP_CLAUSE_CODE (clause))
2359 {
2360 case OMP_CLAUSE_MAP:
2361 break;
2362 case OMP_CLAUSE_FIRSTPRIVATE:
2363 case OMP_CLAUSE_TO:
2364 case OMP_CLAUSE_FROM:
2365 return true;
2366 default:
2367 gcc_unreachable ();
2368 }
2369
2370 switch (OMP_CLAUSE_MAP_KIND (clause))
2371 {
2372 case GOMP_MAP_TO:
2373 case GOMP_MAP_FROM:
2374 case GOMP_MAP_TOFROM:
2375 case GOMP_MAP_ALWAYS_TO:
2376 case GOMP_MAP_ALWAYS_FROM:
2377 case GOMP_MAP_ALWAYS_TOFROM:
2378 case GOMP_MAP_ALWAYS_PRESENT_FROM:
2379 case GOMP_MAP_ALWAYS_PRESENT_TO:
2380 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
2381 case GOMP_MAP_FIRSTPRIVATE:
2382 case GOMP_MAP_ALLOC:
2383 return true;
2384 case GOMP_MAP_POINTER:
2385 case GOMP_MAP_TO_PSET:
2386 case GOMP_MAP_FORCE_PRESENT:
2387 case GOMP_MAP_DELETE:
2388 case GOMP_MAP_FORCE_DEVICEPTR:
2389 case GOMP_MAP_DEVICE_RESIDENT:
2390 case GOMP_MAP_LINK:
2391 case GOMP_MAP_IF_PRESENT:
2392 case GOMP_MAP_PRESENT_ALLOC:
2393 case GOMP_MAP_PRESENT_FROM:
2394 case GOMP_MAP_PRESENT_TO:
2395 case GOMP_MAP_PRESENT_TOFROM:
2396 case GOMP_MAP_FIRSTPRIVATE_INT:
2397 case GOMP_MAP_USE_DEVICE_PTR:
2398 case GOMP_MAP_ZERO_LEN_ARRAY_SECTION:
2399 case GOMP_MAP_FORCE_ALLOC:
2400 case GOMP_MAP_FORCE_TO:
2401 case GOMP_MAP_FORCE_FROM:
2402 case GOMP_MAP_FORCE_TOFROM:
2403 case GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT:
2404 case GOMP_MAP_STRUCT:
2405 case GOMP_MAP_STRUCT_UNORD:
2406 case GOMP_MAP_ALWAYS_POINTER:
2407 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
2408 case GOMP_MAP_DELETE_ZERO_LEN_ARRAY_SECTION:
2409 case GOMP_MAP_RELEASE:
2410 case GOMP_MAP_ATTACH:
2411 case GOMP_MAP_DETACH:
2412 case GOMP_MAP_FORCE_DETACH:
2413 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
2414 case GOMP_MAP_FIRSTPRIVATE_POINTER:
2415 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
2416 case GOMP_MAP_ATTACH_DETACH:
2417 break;
2418 default:
2419 gcc_unreachable ();
2420 }
2421 return false;
2422 }
2423
2424 /* Three OpenMP deep-mapping lang hooks: gfc_omp_deep_mapping{_p,_cnt,}. */
2425
2426 /* Common check for gfc_omp_deep_mapping_p and gfc_omp_deep_mapping_do. */
2427
2428 static tree
2429 gfc_omp_deep_mapping_int_p (const gimple *ctx, tree clause)
2430 {
2431 if (is_gimple_omp_oacc (ctx) || !gfc_omp_deep_map_kind_p (clause))
2432 return NULL_TREE;
2433 tree decl = OMP_CLAUSE_DECL (clause);
2434 if (OMP_CLAUSE_SIZE (clause) != NULL_TREE
2435 && DECL_P (OMP_CLAUSE_SIZE (clause))
2436 && DECL_LANG_SPECIFIC (OMP_CLAUSE_SIZE (clause))
2437 && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (clause)))
2438 /* Saved decl. */
2439 decl = GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (clause));
2440 else if (TREE_CODE (decl) == MEM_REF || TREE_CODE (decl) == INDIRECT_REF)
2441 /* The following can happen for, e.g., class(t) :: var(..) */
2442 decl = TREE_OPERAND (decl, 0);
2443 if (TREE_CODE (decl) == INDIRECT_REF)
2444 /* The following can happen for, e.g., class(t) :: var(..) */
2445 decl = TREE_OPERAND (decl, 0);
2446 if (DECL_P (decl)
2447 && DECL_LANG_SPECIFIC (decl)
2448 && GFC_DECL_SAVED_DESCRIPTOR (decl))
2449 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
2450 /* Handle map(to: var.desc) map([to/from/tofrom:] var.desc.data)
2451 to get proper map kind by skipping to the next item. */
2452 tree tmp = OMP_CLAUSE_CHAIN (clause);
2453 if (tmp != NULL_TREE
2454 && OMP_CLAUSE_CODE (tmp) == OMP_CLAUSE_CODE (clause)
2455 && OMP_CLAUSE_SIZE (tmp) != NULL_TREE
2456 && DECL_P (OMP_CLAUSE_SIZE (tmp))
2457 && DECL_LANG_SPECIFIC (OMP_CLAUSE_SIZE (tmp))
2458 && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_SIZE (tmp)) == decl)
2459 return NULL_TREE;
2460 if (DECL_P (decl)
2461 && DECL_LANG_SPECIFIC (decl)
2462 && GFC_DECL_SAVED_DESCRIPTOR (decl))
2463 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
2464 tree type = TREE_TYPE (decl);
2465 if (POINTER_TYPE_P (type))
2466 type = TREE_TYPE (type);
2467 if (POINTER_TYPE_P (type))
2468 type = TREE_TYPE (type);
2469 tmp = decl;
2470 while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
2471 tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
2472 if (!gfc_is_polymorphic_nonptr (type)
2473 && !gfc_has_alloc_comps (type, tmp, true))
2474 return NULL_TREE;
2475 return decl;
2476 }
2477
2478 /* Return true if there is deep mapping, even if the number of mapping is known
2479 at compile time. */
2480 bool
2481 gfc_omp_deep_mapping_p (const gimple *ctx, tree clause)
2482 {
2483 tree decl = gfc_omp_deep_mapping_int_p (ctx, clause);
2484 if (decl == NULL_TREE)
2485 return false;
2486 return true;
2487 }
2488
2489 /* Handle gfc_omp_deep_mapping{,_cnt} */
2490 static tree
2491 gfc_omp_deep_mapping_do (bool is_cnt, const gimple *ctx, tree clause,
2492 unsigned HOST_WIDE_INT tkind, tree data, tree sizes,
2493 tree kinds, tree offset_data, tree offset,
2494 gimple_seq *seq)
2495 {
2496 tree num = NULL_TREE;
2497 location_t loc = OMP_CLAUSE_LOCATION (clause);
2498 tree decl = gfc_omp_deep_mapping_int_p (ctx, clause);
2499 bool poly_warned = false;
2500 if (decl == NULL_TREE)
2501 return NULL_TREE;
2502 /* Handle: map(alloc:dt%cmp [len: ptr_size]) map(tofrom: D.0123...),
2503 where GFC_DECL_SAVED_DESCRIPTOR(D.0123) is the same (here: dt%cmp). */
2504 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
2505 && (OMP_CLAUSE_MAP_KIND (clause) == GOMP_MAP_ALLOC
2506 || OMP_CLAUSE_MAP_KIND (clause) == GOMP_MAP_PRESENT_ALLOC))
2507 {
2508 tree c = clause;
2509 while ((c = OMP_CLAUSE_CHAIN (c)) != NULL_TREE)
2510 {
2511 if (!gfc_omp_deep_map_kind_p (c))
2512 continue;
2513 tree d = gfc_omp_deep_mapping_int_p (ctx, c);
2514 if (d != NULL_TREE && operand_equal_p (decl, d, 0))
2515 return NULL_TREE;
2516 }
2517 }
2518 tree type = TREE_TYPE (decl);
2519 if (POINTER_TYPE_P (type))
2520 type = TREE_TYPE (type);
2521 if (POINTER_TYPE_P (type))
2522 type = TREE_TYPE (type);
2523 bool poly = gfc_is_polymorphic_nonptr (type);
2524
2525 if (is_cnt)
2526 {
2527 num = build_decl (loc, VAR_DECL,
2528 create_tmp_var_name ("n_deepmap"), size_type_node);
2529 tree tmp = fold_build2_loc (loc, MODIFY_EXPR, size_type_node, num,
2530 build_int_cst (size_type_node, 0));
2531 gimple_add_tmp_var (num);
2532 gimplify_and_add (tmp, seq);
2533 }
2534 else
2535 gcc_assert (short_unsigned_type_node == TREE_TYPE (TREE_TYPE (kinds)));
2536
2537 bool do_copy = poly;
2538 bool do_alloc_check = false;
2539 tree token = NULL_TREE;
2540 tree tmp = decl;
2541 if (poly)
2542 {
2543 tmp = TYPE_FIELDS (type);
2544 type = TREE_TYPE (tmp);
2545 }
2546 else
2547 while (TREE_CODE (tmp) == COMPONENT_REF || TREE_CODE (tmp) == ARRAY_REF)
2548 tmp = TREE_OPERAND (tmp, TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
2549 if (TREE_CODE (tmp) == MEM_REF)
2550 tmp = TREE_OPERAND (tmp, 0);
2551 if (TREE_CODE (tmp) == SSA_NAME)
2552 {
2553 gimple *def_stmt = SSA_NAME_DEF_STMT (tmp);
2554 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2555 {
2556 tmp = gimple_assign_rhs1 (def_stmt);
2557 if (poly)
2558 {
2559 tmp = TYPE_FIELDS (type);
2560 type = TREE_TYPE (tmp);
2561 }
2562 else
2563 while (TREE_CODE (tmp) == COMPONENT_REF
2564 || TREE_CODE (tmp) == ARRAY_REF)
2565 tmp = TREE_OPERAND (tmp,
2566 TREE_CODE (tmp) == COMPONENT_REF ? 1 : 0);
2567 }
2568 }
2569 /* If the clause argument is nonallocatable, skip is-allocate check. */
2570 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (tmp)
2571 || GFC_DECL_GET_SCALAR_POINTER (tmp)
2572 || (GFC_DESCRIPTOR_TYPE_P (type)
2573 && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
2574 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
2575 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)))
2576 do_alloc_check = true;
2577
2578 if (!is_cnt
2579 && OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
2580 && (tkind == GOMP_MAP_ALLOC
2581 || (tkind == GOMP_MAP_FROM
2582 && (gimple_omp_target_kind (ctx)
2583 != GF_OMP_TARGET_KIND_EXIT_DATA)))
2584 && (poly || gfc_omp_replace_alloc_by_to_mapping (type, tmp, true)))
2585 OMP_CLAUSE_SET_MAP_KIND (clause, tkind == GOMP_MAP_ALLOC ? GOMP_MAP_TO
2586 : GOMP_MAP_TOFROM);
2587
2588 /* TODO: For map(a(:)), we know it is present & allocated. */
2589
2590 tree present = (DECL_P (decl) ? gfc_omp_check_optional_argument (decl, true)
2591 : NULL_TREE);
2592 if (POINTER_TYPE_P (TREE_TYPE (decl))
2593 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2594 decl = build_fold_indirect_ref (decl);
2595 if (present)
2596 {
2597 tree then_label = create_artificial_label (loc);
2598 tree end_label = create_artificial_label (loc);
2599 gimple_seq seq2 = NULL;
2600 tmp = force_gimple_operand (present, &seq2, true, NULL_TREE);
2601 gimple_seq_add_seq (seq, seq2);
2602 gimple_seq_add_stmt (seq,
2603 gimple_build_cond_from_tree (present,
2604 then_label, end_label));
2605 gimple_seq_add_stmt (seq, gimple_build_label (then_label));
2606 gfc_omp_deep_mapping_item (is_cnt, do_copy, do_alloc_check, loc, decl,
2607 &token, tkind, data, sizes, kinds,
2608 offset_data, offset, num, seq, ctx,
2609 &poly_warned);
2610 gimple_seq_add_stmt (seq, gimple_build_label (end_label));
2611 }
2612 else
2613 gfc_omp_deep_mapping_item (is_cnt, do_copy, do_alloc_check, loc, decl,
2614 &token, tkind, data, sizes, kinds, offset_data,
2615 offset, num, seq, ctx, &poly_warned);
2616 /* Multiply by 2 as there are two mappings: data + pointer assign. */
2617 if (is_cnt)
2618 gimplify_assign (num,
2619 fold_build2_loc (loc, MULT_EXPR,
2620 size_type_node, num,
2621 build_int_cst (size_type_node, 2)), seq);
2622 return num;
2623 }
2624
2625 /* Return tree with a variable which contains the count of deep-mappyings
2626 (value depends, e.g., on allocation status) */
2627 tree
2628 gfc_omp_deep_mapping_cnt (const gimple *ctx, tree clause, gimple_seq *seq)
2629 {
2630 return gfc_omp_deep_mapping_do (true, ctx, clause, 0, NULL_TREE, NULL_TREE,
2631 NULL_TREE, NULL_TREE, NULL_TREE, seq);
2632 }
2633
2634 /* Does the actual deep mapping. */
2635 void
2636 gfc_omp_deep_mapping (const gimple *ctx, tree clause,
2637 unsigned HOST_WIDE_INT tkind, tree data,
2638 tree sizes, tree kinds, tree offset_data, tree offset,
2639 gimple_seq *seq)
2640 {
2641 (void) gfc_omp_deep_mapping_do (false, ctx, clause, tkind, data, sizes, kinds,
2642 offset_data, offset, seq);
2643 }
2644
2645 /* Return true if DECL is a scalar variable (for the purpose of
2646 implicit firstprivatization/mapping). Only if 'ptr_alloc_ok.'
2647 is true, allocatables and pointers are permitted. */
2648
2649 bool
2650 gfc_omp_scalar_p (tree decl, bool ptr_alloc_ok)
2651 {
2652 tree type = TREE_TYPE (decl);
2653 if (TREE_CODE (type) == REFERENCE_TYPE)
2654 type = TREE_TYPE (type);
2655 if (TREE_CODE (type) == POINTER_TYPE)
2656 {
2657 if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
2658 || GFC_DECL_GET_SCALAR_POINTER (decl))
2659 {
2660 if (!ptr_alloc_ok)
2661 return false;
2662 type = TREE_TYPE (type);
2663 }
2664 if (GFC_ARRAY_TYPE_P (type)
2665 || GFC_CLASS_TYPE_P (type))
2666 return false;
2667 }
2668 if ((TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE)
2669 && TYPE_STRING_FLAG (type))
2670 return false;
2671 if (INTEGRAL_TYPE_P (type)
2672 || SCALAR_FLOAT_TYPE_P (type)
2673 || COMPLEX_FLOAT_TYPE_P (type))
2674 return true;
2675 return false;
2676 }
2677
2678
2679 /* Return true if DECL is a scalar with target attribute but does not have the
2680 allocatable (or pointer) attribute (for the purpose of implicit mapping). */
2681
2682 bool
2683 gfc_omp_scalar_target_p (tree decl)
2684 {
2685 return (DECL_P (decl) && GFC_DECL_GET_SCALAR_TARGET (decl)
2686 && gfc_omp_scalar_p (decl, false));
2687 }
2688
2689
2690 /* Return true if DECL's DECL_VALUE_EXPR (if any) should be
2691 disregarded in OpenMP construct, because it is going to be
2692 remapped during OpenMP lowering. SHARED is true if DECL
2693 is going to be shared, false if it is going to be privatized. */
2694
2695 bool
2696 gfc_omp_disregard_value_expr (tree decl, bool shared)
2697 {
2698 if (GFC_DECL_COMMON_OR_EQUIV (decl)
2699 && DECL_HAS_VALUE_EXPR_P (decl))
2700 {
2701 tree value = DECL_VALUE_EXPR (decl);
2702
2703 if (TREE_CODE (value) == COMPONENT_REF
2704 && VAR_P (TREE_OPERAND (value, 0))
2705 && GFC_DECL_COMMON_OR_EQUIV (TREE_OPERAND (value, 0)))
2706 {
2707 /* If variable in COMMON or EQUIVALENCE is privatized, return
2708 true, as just that variable is supposed to be privatized,
2709 not the whole COMMON or whole EQUIVALENCE.
2710 For shared variables in COMMON or EQUIVALENCE, let them be
2711 gimplified to DECL_VALUE_EXPR, so that for multiple shared vars
2712 from the same COMMON or EQUIVALENCE just one sharing of the
2713 whole COMMON or EQUIVALENCE is enough. */
2714 return ! shared;
2715 }
2716 }
2717
2718 if (GFC_DECL_RESULT (decl) && DECL_HAS_VALUE_EXPR_P (decl))
2719 return ! shared;
2720
2721 return false;
2722 }
2723
2724 /* Return true if DECL that is shared iff SHARED is true should
2725 be put into OMP_CLAUSE_PRIVATE with OMP_CLAUSE_PRIVATE_DEBUG
2726 flag set. */
2727
2728 bool
2729 gfc_omp_private_debug_clause (tree decl, bool shared)
2730 {
2731 if (GFC_DECL_CRAY_POINTEE (decl))
2732 return true;
2733
2734 if (GFC_DECL_COMMON_OR_EQUIV (decl)
2735 && DECL_HAS_VALUE_EXPR_P (decl))
2736 {
2737 tree value = DECL_VALUE_EXPR (decl);
2738
2739 if (TREE_CODE (value) == COMPONENT_REF
2740 && VAR_P (TREE_OPERAND (value, 0))
2741 && GFC_DECL_COMMON_OR_EQUIV (TREE_OPERAND (value, 0)))
2742 return shared;
2743 }
2744
2745 return false;
2746 }
2747
2748 /* Register language specific type size variables as potentially OpenMP
2749 firstprivate variables. */
2750
2751 void
2752 gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
2753 {
2754 if (GFC_ARRAY_TYPE_P (type) || GFC_DESCRIPTOR_TYPE_P (type))
2755 {
2756 int r;
2757
2758 gcc_assert (TYPE_LANG_SPECIFIC (type) != NULL);
2759 for (r = 0; r < GFC_TYPE_ARRAY_RANK (type); r++)
2760 {
2761 omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_LBOUND (type, r));
2762 omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_UBOUND (type, r));
2763 omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_STRIDE (type, r));
2764 }
2765 omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_SIZE (type));
2766 omp_firstprivatize_variable (ctx, GFC_TYPE_ARRAY_OFFSET (type));
2767 }
2768 }
2769
2770
2771 static inline tree
2772 gfc_trans_add_clause (tree node, tree tail)
2773 {
2774 OMP_CLAUSE_CHAIN (node) = tail;
2775 return node;
2776 }
2777
2778 static tree
2779 gfc_trans_omp_variable (gfc_symbol *sym, bool declare_simd)
2780 {
2781 if (declare_simd)
2782 {
2783 int cnt = 0;
2784 gfc_symbol *proc_sym;
2785 gfc_formal_arglist *f;
2786
2787 gcc_assert (sym->attr.dummy);
2788 proc_sym = sym->ns->proc_name;
2789 if (proc_sym->attr.entry_master)
2790 ++cnt;
2791 if (gfc_return_by_reference (proc_sym))
2792 {
2793 ++cnt;
2794 if (proc_sym->ts.type == BT_CHARACTER)
2795 ++cnt;
2796 }
2797 for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
2798 if (f->sym == sym)
2799 break;
2800 else if (f->sym)
2801 ++cnt;
2802 gcc_assert (f);
2803 return build_int_cst (integer_type_node, cnt);
2804 }
2805
2806 tree t = gfc_get_symbol_decl (sym);
2807 tree parent_decl;
2808 int parent_flag;
2809 bool return_value;
2810 bool alternate_entry;
2811 bool entry_master;
2812
2813 return_value = sym->attr.function && sym->result == sym;
2814 alternate_entry = sym->attr.function && sym->attr.entry
2815 && sym->result == sym;
2816 entry_master = sym->attr.result
2817 && sym->ns->proc_name->attr.entry_master
2818 && !gfc_return_by_reference (sym->ns->proc_name);
2819 parent_decl = current_function_decl
2820 ? DECL_CONTEXT (current_function_decl) : NULL_TREE;
2821
2822 if ((t == parent_decl && return_value)
2823 || (sym->ns && sym->ns->proc_name
2824 && sym->ns->proc_name->backend_decl == parent_decl
2825 && (alternate_entry || entry_master)))
2826 parent_flag = 1;
2827 else
2828 parent_flag = 0;
2829
2830 /* Special case for assigning the return value of a function.
2831 Self recursive functions must have an explicit return value. */
2832 if (return_value && (t == current_function_decl || parent_flag))
2833 t = gfc_get_fake_result_decl (sym, parent_flag);
2834
2835 /* Similarly for alternate entry points. */
2836 else if (alternate_entry
2837 && (sym->ns->proc_name->backend_decl == current_function_decl
2838 || parent_flag))
2839 {
2840 gfc_entry_list *el = NULL;
2841
2842 for (el = sym->ns->entries; el; el = el->next)
2843 if (sym == el->sym)
2844 {
2845 t = gfc_get_fake_result_decl (sym, parent_flag);
2846 break;
2847 }
2848 }
2849
2850 else if (entry_master
2851 && (sym->ns->proc_name->backend_decl == current_function_decl
2852 || parent_flag))
2853 t = gfc_get_fake_result_decl (sym, parent_flag);
2854
2855 return t;
2856 }
2857
2858 static tree
2859 gfc_trans_omp_variable_list (enum omp_clause_code code,
2860 gfc_omp_namelist *namelist, tree list,
2861 bool declare_simd)
2862 {
2863 for (; namelist != NULL; namelist = namelist->next)
2864 if (namelist->sym->attr.referenced || declare_simd)
2865 {
2866 tree t = gfc_trans_omp_variable (namelist->sym, declare_simd);
2867 if (t != error_mark_node)
2868 {
2869 tree node;
2870 node = build_omp_clause (input_location, code);
2871 OMP_CLAUSE_DECL (node) = t;
2872 list = gfc_trans_add_clause (node, list);
2873
2874 if (code == OMP_CLAUSE_LASTPRIVATE
2875 && namelist->u.lastprivate_conditional)
2876 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (node) = 1;
2877 }
2878 }
2879 return list;
2880 }
2881
2882 struct omp_udr_find_orig_data
2883 {
2884 gfc_omp_udr *omp_udr;
2885 bool omp_orig_seen;
2886 };
2887
2888 static int
2889 omp_udr_find_orig (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
2890 void *data)
2891 {
2892 struct omp_udr_find_orig_data *cd = (struct omp_udr_find_orig_data *) data;
2893 if ((*e)->expr_type == EXPR_VARIABLE
2894 && (*e)->symtree->n.sym == cd->omp_udr->omp_orig)
2895 cd->omp_orig_seen = true;
2896
2897 return 0;
2898 }
2899
2900 static void
2901 gfc_trans_omp_array_reduction_or_udr (tree c, gfc_omp_namelist *n, locus where)
2902 {
2903 gfc_symbol *sym = n->sym;
2904 gfc_symtree *root1 = NULL, *root2 = NULL, *root3 = NULL, *root4 = NULL;
2905 gfc_symtree *symtree1, *symtree2, *symtree3, *symtree4 = NULL;
2906 gfc_symbol init_val_sym, outer_sym, intrinsic_sym;
2907 gfc_symbol omp_var_copy[4];
2908 gfc_expr *e1, *e2, *e3, *e4;
2909 gfc_ref *ref;
2910 tree decl, backend_decl, stmt, type, outer_decl;
2911 locus old_loc = gfc_current_locus;
2912 const char *iname;
2913 bool t;
2914 gfc_omp_udr *udr = n->u2.udr ? n->u2.udr->udr : NULL;
2915
2916 decl = OMP_CLAUSE_DECL (c);
2917 gfc_current_locus = where;
2918 type = TREE_TYPE (decl);
2919 outer_decl = create_tmp_var_raw (type);
2920 if (TREE_CODE (decl) == PARM_DECL
2921 && TREE_CODE (type) == REFERENCE_TYPE
2922 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))
2923 && GFC_TYPE_ARRAY_AKIND (TREE_TYPE (type)) == GFC_ARRAY_ALLOCATABLE)
2924 {
2925 decl = build_fold_indirect_ref (decl);
2926 type = TREE_TYPE (type);
2927 }
2928
2929 /* Create a fake symbol for init value. */
2930 memset (&init_val_sym, 0, sizeof (init_val_sym));
2931 init_val_sym.ns = sym->ns;
2932 init_val_sym.name = sym->name;
2933 init_val_sym.ts = sym->ts;
2934 init_val_sym.attr.referenced = 1;
2935 init_val_sym.declared_at = where;
2936 init_val_sym.attr.flavor = FL_VARIABLE;
2937 if (OMP_CLAUSE_REDUCTION_CODE (c) != ERROR_MARK)
2938 backend_decl = omp_reduction_init (c, gfc_sym_type (&init_val_sym));
2939 else if (udr->initializer_ns)
2940 backend_decl = NULL;
2941 else
2942 switch (sym->ts.type)
2943 {
2944 case BT_LOGICAL:
2945 case BT_INTEGER:
2946 case BT_REAL:
2947 case BT_COMPLEX:
2948 backend_decl = build_zero_cst (gfc_sym_type (&init_val_sym));
2949 break;
2950 default:
2951 backend_decl = NULL_TREE;
2952 break;
2953 }
2954 init_val_sym.backend_decl = backend_decl;
2955
2956 /* Create a fake symbol for the outer array reference. */
2957 outer_sym = *sym;
2958 if (sym->as)
2959 outer_sym.as = gfc_copy_array_spec (sym->as);
2960 outer_sym.attr.dummy = 0;
2961 outer_sym.attr.result = 0;
2962 outer_sym.attr.flavor = FL_VARIABLE;
2963 outer_sym.backend_decl = outer_decl;
2964 if (decl != OMP_CLAUSE_DECL (c))
2965 outer_sym.backend_decl = build_fold_indirect_ref (outer_decl);
2966
2967 /* Create fake symtrees for it. */
2968 symtree1 = gfc_new_symtree (&root1, sym->name);
2969 symtree1->n.sym = sym;
2970 gcc_assert (symtree1 == root1);
2971
2972 symtree2 = gfc_new_symtree (&root2, sym->name);
2973 symtree2->n.sym = &init_val_sym;
2974 gcc_assert (symtree2 == root2);
2975
2976 symtree3 = gfc_new_symtree (&root3, sym->name);
2977 symtree3->n.sym = &outer_sym;
2978 gcc_assert (symtree3 == root3);
2979
2980 memset (omp_var_copy, 0, sizeof omp_var_copy);
2981 if (udr)
2982 {
2983 omp_var_copy[0] = *udr->omp_out;
2984 omp_var_copy[1] = *udr->omp_in;
2985 *udr->omp_out = outer_sym;
2986 *udr->omp_in = *sym;
2987 if (udr->initializer_ns)
2988 {
2989 omp_var_copy[2] = *udr->omp_priv;
2990 omp_var_copy[3] = *udr->omp_orig;
2991 *udr->omp_priv = *sym;
2992 *udr->omp_orig = outer_sym;
2993 }
2994 }
2995
2996 /* Create expressions. */
2997 e1 = gfc_get_expr ();
2998 e1->expr_type = EXPR_VARIABLE;
2999 e1->where = where;
3000 e1->symtree = symtree1;
3001 e1->ts = sym->ts;
3002 if (sym->attr.dimension)
3003 {
3004 e1->ref = ref = gfc_get_ref ();
3005 ref->type = REF_ARRAY;
3006 ref->u.ar.where = where;
3007 ref->u.ar.as = sym->as;
3008 ref->u.ar.type = AR_FULL;
3009 ref->u.ar.dimen = 0;
3010 }
3011 t = gfc_resolve_expr (e1);
3012 gcc_assert (t);
3013
3014 e2 = NULL;
3015 if (backend_decl != NULL_TREE)
3016 {
3017 e2 = gfc_get_expr ();
3018 e2->expr_type = EXPR_VARIABLE;
3019 e2->where = where;
3020 e2->symtree = symtree2;
3021 e2->ts = sym->ts;
3022 t = gfc_resolve_expr (e2);
3023 gcc_assert (t);
3024 }
3025 else if (udr->initializer_ns == NULL)
3026 {
3027 gcc_assert (sym->ts.type == BT_DERIVED);
3028 e2 = gfc_default_initializer (&sym->ts);
3029 gcc_assert (e2);
3030 t = gfc_resolve_expr (e2);
3031 gcc_assert (t);
3032 }
3033 else if (n->u2.udr->initializer->op == EXEC_ASSIGN)
3034 {
3035 e2 = gfc_copy_expr (n->u2.udr->initializer->expr2);
3036 t = gfc_resolve_expr (e2);
3037 gcc_assert (t);
3038 }
3039 if (udr && udr->initializer_ns)
3040 {
3041 struct omp_udr_find_orig_data cd;
3042 cd.omp_udr = udr;
3043 cd.omp_orig_seen = false;
3044 gfc_code_walker (&n->u2.udr->initializer,
3045 gfc_dummy_code_callback, omp_udr_find_orig, &cd);
3046 if (cd.omp_orig_seen)
3047 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
3048 }
3049
3050 e3 = gfc_copy_expr (e1);
3051 e3->symtree = symtree3;
3052 t = gfc_resolve_expr (e3);
3053 gcc_assert (t);
3054
3055 iname = NULL;
3056 e4 = NULL;
3057 switch (OMP_CLAUSE_REDUCTION_CODE (c))
3058 {
3059 case PLUS_EXPR:
3060 case MINUS_EXPR:
3061 e4 = gfc_add (e3, e1);
3062 break;
3063 case MULT_EXPR:
3064 e4 = gfc_multiply (e3, e1);
3065 break;
3066 case TRUTH_ANDIF_EXPR:
3067 e4 = gfc_and (e3, e1);
3068 break;
3069 case TRUTH_ORIF_EXPR:
3070 e4 = gfc_or (e3, e1);
3071 break;
3072 case EQ_EXPR:
3073 e4 = gfc_eqv (e3, e1);
3074 break;
3075 case NE_EXPR:
3076 e4 = gfc_neqv (e3, e1);
3077 break;
3078 case MIN_EXPR:
3079 iname = "min";
3080 break;
3081 case MAX_EXPR:
3082 iname = "max";
3083 break;
3084 case BIT_AND_EXPR:
3085 iname = "iand";
3086 break;
3087 case BIT_IOR_EXPR:
3088 iname = "ior";
3089 break;
3090 case BIT_XOR_EXPR:
3091 iname = "ieor";
3092 break;
3093 case ERROR_MARK:
3094 if (n->u2.udr->combiner->op == EXEC_ASSIGN)
3095 {
3096 gfc_free_expr (e3);
3097 e3 = gfc_copy_expr (n->u2.udr->combiner->expr1);
3098 e4 = gfc_copy_expr (n->u2.udr->combiner->expr2);
3099 t = gfc_resolve_expr (e3);
3100 gcc_assert (t);
3101 t = gfc_resolve_expr (e4);
3102 gcc_assert (t);
3103 }
3104 break;
3105 default:
3106 gcc_unreachable ();
3107 }
3108 if (iname != NULL)
3109 {
3110 memset (&intrinsic_sym, 0, sizeof (intrinsic_sym));
3111 intrinsic_sym.ns = sym->ns;
3112 intrinsic_sym.name = iname;
3113 intrinsic_sym.ts = sym->ts;
3114 intrinsic_sym.attr.referenced = 1;
3115 intrinsic_sym.attr.intrinsic = 1;
3116 intrinsic_sym.attr.function = 1;
3117 intrinsic_sym.attr.implicit_type = 1;
3118 intrinsic_sym.result = &intrinsic_sym;
3119 intrinsic_sym.declared_at = where;
3120
3121 symtree4 = gfc_new_symtree (&root4, iname);
3122 symtree4->n.sym = &intrinsic_sym;
3123 gcc_assert (symtree4 == root4);
3124
3125 e4 = gfc_get_expr ();
3126 e4->expr_type = EXPR_FUNCTION;
3127 e4->where = where;
3128 e4->symtree = symtree4;
3129 e4->value.function.actual = gfc_get_actual_arglist ();
3130 e4->value.function.actual->expr = e3;
3131 e4->value.function.actual->next = gfc_get_actual_arglist ();
3132 e4->value.function.actual->next->expr = e1;
3133 }
3134 if (OMP_CLAUSE_REDUCTION_CODE (c) != ERROR_MARK)
3135 {
3136 /* e1 and e3 have been stored as arguments of e4, avoid sharing. */
3137 e1 = gfc_copy_expr (e1);
3138 e3 = gfc_copy_expr (e3);
3139 t = gfc_resolve_expr (e4);
3140 gcc_assert (t);
3141 }
3142
3143 /* Create the init statement list. */
3144 pushlevel ();
3145 if (e2)
3146 stmt = gfc_trans_assignment (e1, e2, false, false);
3147 else
3148 stmt = gfc_trans_call (n->u2.udr->initializer, false,
3149 NULL_TREE, NULL_TREE, false);
3150 if (TREE_CODE (stmt) != BIND_EXPR)
3151 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
3152 else
3153 poplevel (0, 0);
3154 OMP_CLAUSE_REDUCTION_INIT (c) = stmt;
3155
3156 /* Create the merge statement list. */
3157 pushlevel ();
3158 if (e4)
3159 stmt = gfc_trans_assignment (e3, e4, false, true);
3160 else
3161 stmt = gfc_trans_call (n->u2.udr->combiner, false,
3162 NULL_TREE, NULL_TREE, false);
3163 if (TREE_CODE (stmt) != BIND_EXPR)
3164 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
3165 else
3166 poplevel (0, 0);
3167 OMP_CLAUSE_REDUCTION_MERGE (c) = stmt;
3168
3169 /* And stick the placeholder VAR_DECL into the clause as well. */
3170 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = outer_decl;
3171
3172 gfc_current_locus = old_loc;
3173
3174 gfc_free_expr (e1);
3175 if (e2)
3176 gfc_free_expr (e2);
3177 gfc_free_expr (e3);
3178 if (e4)
3179 gfc_free_expr (e4);
3180 free (symtree1);
3181 free (symtree2);
3182 free (symtree3);
3183 free (symtree4);
3184 if (outer_sym.as)
3185 gfc_free_array_spec (outer_sym.as);
3186
3187 if (udr)
3188 {
3189 *udr->omp_out = omp_var_copy[0];
3190 *udr->omp_in = omp_var_copy[1];
3191 if (udr->initializer_ns)
3192 {
3193 *udr->omp_priv = omp_var_copy[2];
3194 *udr->omp_orig = omp_var_copy[3];
3195 }
3196 }
3197 }
3198
3199 static tree
3200 gfc_trans_omp_reduction_list (int kind, gfc_omp_namelist *namelist, tree list,
3201 locus where, bool mark_addressable)
3202 {
3203 omp_clause_code clause = OMP_CLAUSE_REDUCTION;
3204 switch (kind)
3205 {
3206 case OMP_LIST_REDUCTION:
3207 case OMP_LIST_REDUCTION_INSCAN:
3208 case OMP_LIST_REDUCTION_TASK:
3209 break;
3210 case OMP_LIST_IN_REDUCTION:
3211 clause = OMP_CLAUSE_IN_REDUCTION;
3212 break;
3213 case OMP_LIST_TASK_REDUCTION:
3214 clause = OMP_CLAUSE_TASK_REDUCTION;
3215 break;
3216 default:
3217 gcc_unreachable ();
3218 }
3219 for (; namelist != NULL; namelist = namelist->next)
3220 if (namelist->sym->attr.referenced)
3221 {
3222 tree t = gfc_trans_omp_variable (namelist->sym, false);
3223 if (t != error_mark_node)
3224 {
3225 tree node = build_omp_clause (gfc_get_location (&namelist->where),
3226 clause);
3227 OMP_CLAUSE_DECL (node) = t;
3228 if (mark_addressable)
3229 TREE_ADDRESSABLE (t) = 1;
3230 if (kind == OMP_LIST_REDUCTION_INSCAN)
3231 OMP_CLAUSE_REDUCTION_INSCAN (node) = 1;
3232 if (kind == OMP_LIST_REDUCTION_TASK)
3233 OMP_CLAUSE_REDUCTION_TASK (node) = 1;
3234 switch (namelist->u.reduction_op)
3235 {
3236 case OMP_REDUCTION_PLUS:
3237 OMP_CLAUSE_REDUCTION_CODE (node) = PLUS_EXPR;
3238 break;
3239 case OMP_REDUCTION_MINUS:
3240 OMP_CLAUSE_REDUCTION_CODE (node) = MINUS_EXPR;
3241 break;
3242 case OMP_REDUCTION_TIMES:
3243 OMP_CLAUSE_REDUCTION_CODE (node) = MULT_EXPR;
3244 break;
3245 case OMP_REDUCTION_AND:
3246 OMP_CLAUSE_REDUCTION_CODE (node) = TRUTH_ANDIF_EXPR;
3247 break;
3248 case OMP_REDUCTION_OR:
3249 OMP_CLAUSE_REDUCTION_CODE (node) = TRUTH_ORIF_EXPR;
3250 break;
3251 case OMP_REDUCTION_EQV:
3252 OMP_CLAUSE_REDUCTION_CODE (node) = EQ_EXPR;
3253 break;
3254 case OMP_REDUCTION_NEQV:
3255 OMP_CLAUSE_REDUCTION_CODE (node) = NE_EXPR;
3256 break;
3257 case OMP_REDUCTION_MAX:
3258 OMP_CLAUSE_REDUCTION_CODE (node) = MAX_EXPR;
3259 break;
3260 case OMP_REDUCTION_MIN:
3261 OMP_CLAUSE_REDUCTION_CODE (node) = MIN_EXPR;
3262 break;
3263 case OMP_REDUCTION_IAND:
3264 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_AND_EXPR;
3265 break;
3266 case OMP_REDUCTION_IOR:
3267 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_IOR_EXPR;
3268 break;
3269 case OMP_REDUCTION_IEOR:
3270 OMP_CLAUSE_REDUCTION_CODE (node) = BIT_XOR_EXPR;
3271 break;
3272 case OMP_REDUCTION_USER:
3273 OMP_CLAUSE_REDUCTION_CODE (node) = ERROR_MARK;
3274 break;
3275 default:
3276 gcc_unreachable ();
3277 }
3278 if (namelist->sym->attr.dimension
3279 || namelist->u.reduction_op == OMP_REDUCTION_USER
3280 || namelist->sym->attr.allocatable)
3281 gfc_trans_omp_array_reduction_or_udr (node, namelist, where);
3282 list = gfc_trans_add_clause (node, list);
3283 }
3284 }
3285 return list;
3286 }
3287
3288 static inline tree
3289 gfc_convert_expr_to_tree (stmtblock_t *block, gfc_expr *expr)
3290 {
3291 gfc_se se;
3292 tree result;
3293
3294 gfc_init_se (&se, NULL );
3295 gfc_conv_expr (&se, expr);
3296 gfc_add_block_to_block (block, &se.pre);
3297 result = gfc_evaluate_now (se.expr, block);
3298 gfc_add_block_to_block (block, &se.post);
3299
3300 return result;
3301 }
3302
3303 static vec<tree, va_heap, vl_embed> *doacross_steps;
3304
3305
3306 /* Translate an array section or array element. */
3307
3308 static void
3309 gfc_trans_omp_array_section (stmtblock_t *block, gfc_exec_op op,
3310 gfc_omp_namelist *n, tree decl, bool element,
3311 bool openmp, gomp_map_kind ptr_kind, tree &node,
3312 tree &node2, tree &node3, tree &node4)
3313 {
3314 gfc_se se;
3315 tree ptr, ptr2;
3316 tree elemsz = NULL_TREE;
3317
3318 gfc_init_se (&se, NULL);
3319 if (element)
3320 {
3321 gfc_conv_expr_reference (&se, n->expr);
3322 gfc_add_block_to_block (block, &se.pre);
3323 ptr = se.expr;
3324 }
3325 else
3326 {
3327 gfc_conv_expr_descriptor (&se, n->expr);
3328 ptr = gfc_conv_array_data (se.expr);
3329 }
3330 if (n->expr->ts.type == BT_CHARACTER && n->expr->ts.deferred)
3331 {
3332 gcc_assert (se.string_length);
3333 tree len = gfc_evaluate_now (se.string_length, block);
3334 elemsz = gfc_get_char_type (n->expr->ts.kind);
3335 elemsz = TYPE_SIZE_UNIT (elemsz);
3336 elemsz = fold_build2 (MULT_EXPR, size_type_node,
3337 fold_convert (size_type_node, len), elemsz);
3338 }
3339 if (element)
3340 {
3341 if (!elemsz)
3342 elemsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ptr)));
3343 OMP_CLAUSE_SIZE (node) = elemsz;
3344 }
3345 else
3346 {
3347 tree type = TREE_TYPE (se.expr);
3348 gfc_add_block_to_block (block, &se.pre);
3349 OMP_CLAUSE_SIZE (node) = gfc_full_array_size (block, se.expr,
3350 GFC_TYPE_ARRAY_RANK (type));
3351 if (!elemsz)
3352 elemsz = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3353 elemsz = fold_convert (gfc_array_index_type, elemsz);
3354 OMP_CLAUSE_SIZE (node) = fold_build2 (MULT_EXPR, gfc_array_index_type,
3355 OMP_CLAUSE_SIZE (node), elemsz);
3356 if (n->expr->ts.type == BT_DERIVED
3357 && n->expr->ts.u.derived->attr.alloc_comp)
3358 {
3359 /* Save array descriptor for use in gfc_omp_deep_mapping{,_p,_cnt};
3360 force evaluate to ensure that it is not gimplified + is a decl. */
3361 tree tmp = OMP_CLAUSE_SIZE (node);
3362 tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
3363 gfc_add_modify_loc (input_location, block, var, tmp);
3364 OMP_CLAUSE_SIZE (node) = var;
3365 gfc_allocate_lang_decl (var);
3366 GFC_DECL_SAVED_DESCRIPTOR (var) = se.expr;
3367 }
3368 }
3369 gcc_assert (se.post.head == NULL_TREE);
3370 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
3371 OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
3372 ptr = fold_convert (ptrdiff_type_node, ptr);
3373
3374 if (POINTER_TYPE_P (TREE_TYPE (decl))
3375 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))
3376 && ptr_kind == GOMP_MAP_POINTER
3377 && op != EXEC_OMP_TARGET_EXIT_DATA
3378 && OMP_CLAUSE_MAP_KIND (node) != GOMP_MAP_RELEASE
3379 && OMP_CLAUSE_MAP_KIND (node) != GOMP_MAP_DELETE)
3380
3381 {
3382 node4 = build_omp_clause (input_location,
3383 OMP_CLAUSE_MAP);
3384 OMP_CLAUSE_SET_MAP_KIND (node4, GOMP_MAP_POINTER);
3385 OMP_CLAUSE_DECL (node4) = decl;
3386 OMP_CLAUSE_SIZE (node4) = size_int (0);
3387 decl = build_fold_indirect_ref (decl);
3388 }
3389 else if (ptr_kind == GOMP_MAP_ALWAYS_POINTER
3390 && n->expr->ts.type == BT_CHARACTER
3391 && n->expr->ts.deferred)
3392 {
3393 gomp_map_kind map_kind;
3394 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE)
3395 map_kind = OMP_CLAUSE_MAP_KIND (node);
3396 else if (op == EXEC_OMP_TARGET_EXIT_DATA
3397 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_RELEASE)
3398 map_kind = GOMP_MAP_RELEASE;
3399 else
3400 map_kind = GOMP_MAP_TO;
3401 gcc_assert (se.string_length);
3402 node4 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
3403 OMP_CLAUSE_SET_MAP_KIND (node4, map_kind);
3404 OMP_CLAUSE_DECL (node4) = se.string_length;
3405 OMP_CLAUSE_SIZE (node4) = TYPE_SIZE_UNIT (gfc_charlen_type_node);
3406 }
3407 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
3408 {
3409 tree type = TREE_TYPE (decl);
3410 ptr2 = gfc_conv_descriptor_data_get (decl);
3411 node2 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
3412 OMP_CLAUSE_DECL (node2) = decl;
3413 OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
3414 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE
3415 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_RELEASE
3416 || op == EXEC_OMP_TARGET_EXIT_DATA
3417 || op == EXEC_OACC_EXIT_DATA)
3418 {
3419 gomp_map_kind map_kind
3420 = OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DELETE ? GOMP_MAP_DELETE
3421 : GOMP_MAP_RELEASE;
3422 OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
3423 OMP_CLAUSE_RELEASE_DESCRIPTOR (node2) = 1;
3424 }
3425 else
3426 OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_TO_PSET);
3427 node3 = build_omp_clause (input_location, OMP_CLAUSE_MAP);
3428 OMP_CLAUSE_SET_MAP_KIND (node3, ptr_kind);
3429 OMP_CLAUSE_DECL (node3) = gfc_conv_descriptor_data_get (decl);
3430 /* This purposely does not include GOMP_MAP_ALWAYS_POINTER. The extra
3431 cast prevents gimplify.cc from recognising it as being part of the
3432 struct - and adding an 'alloc: for the 'desc.data' pointer, which
3433 would break as the 'desc' (the descriptor) is also mapped
3434 (see node4 above). */
3435 if (ptr_kind == GOMP_MAP_ATTACH_DETACH && !openmp)
3436 STRIP_NOPS (OMP_CLAUSE_DECL (node3));
3437 }
3438 else
3439 {
3440 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3441 {
3442 tree offset;
3443 ptr2 = build_fold_addr_expr (decl);
3444 offset = fold_build2 (MINUS_EXPR, ptrdiff_type_node, ptr,
3445 fold_convert (ptrdiff_type_node, ptr2));
3446 offset = build2 (TRUNC_DIV_EXPR, ptrdiff_type_node,
3447 offset, fold_convert (ptrdiff_type_node, elemsz));
3448 offset = build4_loc (input_location, ARRAY_REF,
3449 TREE_TYPE (TREE_TYPE (decl)),
3450 decl, offset, NULL_TREE, NULL_TREE);
3451 OMP_CLAUSE_DECL (node) = offset;
3452
3453 if (ptr_kind == GOMP_MAP_ATTACH_DETACH && openmp)
3454 return;
3455 }
3456 else
3457 {
3458 gcc_assert (POINTER_TYPE_P (TREE_TYPE (decl)));
3459 ptr2 = decl;
3460 }
3461 node3 = build_omp_clause (input_location,
3462 OMP_CLAUSE_MAP);
3463 OMP_CLAUSE_SET_MAP_KIND (node3, ptr_kind);
3464 OMP_CLAUSE_DECL (node3) = decl;
3465 }
3466 ptr2 = fold_convert (ptrdiff_type_node, ptr2);
3467 OMP_CLAUSE_SIZE (node3) = fold_build2 (MINUS_EXPR, ptrdiff_type_node,
3468 ptr, ptr2);
3469 }
3470
3471 static tree
3472 handle_iterator (gfc_namespace *ns, stmtblock_t *iter_block, tree block)
3473 {
3474 tree list = NULL_TREE;
3475 for (gfc_symbol *sym = ns->omp_affinity_iterators; sym; sym = sym->tlink)
3476 {
3477 gfc_constructor *c;
3478 gfc_se se;
3479
3480 tree last = make_tree_vec (6);
3481 tree iter_var = gfc_get_symbol_decl (sym);
3482 tree type = TREE_TYPE (iter_var);
3483 TREE_VEC_ELT (last, 0) = iter_var;
3484 DECL_CHAIN (iter_var) = BLOCK_VARS (block);
3485 BLOCK_VARS (block) = iter_var;
3486
3487 /* begin */
3488 c = gfc_constructor_first (sym->value->value.constructor);
3489 gfc_init_se (&se, NULL);
3490 gfc_conv_expr (&se, c->expr);
3491 gfc_add_block_to_block (iter_block, &se.pre);
3492 gfc_add_block_to_block (iter_block, &se.post);
3493 TREE_VEC_ELT (last, 1) = fold_convert (type,
3494 gfc_evaluate_now (se.expr,
3495 iter_block));
3496 /* end */
3497 c = gfc_constructor_next (c);
3498 gfc_init_se (&se, NULL);
3499 gfc_conv_expr (&se, c->expr);
3500 gfc_add_block_to_block (iter_block, &se.pre);
3501 gfc_add_block_to_block (iter_block, &se.post);
3502 TREE_VEC_ELT (last, 2) = fold_convert (type,
3503 gfc_evaluate_now (se.expr,
3504 iter_block));
3505 /* step */
3506 c = gfc_constructor_next (c);
3507 tree step;
3508 if (c)
3509 {
3510 gfc_init_se (&se, NULL);
3511 gfc_conv_expr (&se, c->expr);
3512 gfc_add_block_to_block (iter_block, &se.pre);
3513 gfc_add_block_to_block (iter_block, &se.post);
3514 gfc_conv_expr (&se, c->expr);
3515 step = fold_convert (type,
3516 gfc_evaluate_now (se.expr,
3517 iter_block));
3518 }
3519 else
3520 step = build_int_cst (type, 1);
3521 TREE_VEC_ELT (last, 3) = step;
3522 /* orig_step */
3523 TREE_VEC_ELT (last, 4) = save_expr (step);
3524 TREE_CHAIN (last) = list;
3525 list = last;
3526 }
3527 return list;
3528 }
3529
3530 /* To alleviate quadratic behaviour in checking each entry of a
3531 gfc_omp_namelist against every other entry, we build a hashtable indexed by
3532 gfc_symbol pointer, which we can use in the usual case that a map
3533 expression has a symbol as its root term. Return a namelist based on the
3534 root symbol used by N, building a new table in SYM_ROOTED_NL using the
3535 gfc_omp_namelist N2 (all clauses) if we haven't done so already. */
3536
3537 static gfc_omp_namelist *
3538 get_symbol_rooted_namelist (hash_map<gfc_symbol *,
3539 gfc_omp_namelist *> *&sym_rooted_nl,
3540 gfc_omp_namelist *n,
3541 gfc_omp_namelist *n2, bool *sym_based)
3542 {
3543 /* Early-out if we have a NULL clause list (e.g. for OpenACC). */
3544 if (!n2)
3545 return NULL;
3546
3547 gfc_symbol *use_sym = NULL;
3548
3549 /* We're only interested in cases where we have an expression, e.g. a
3550 component access. */
3551 if (n->expr && n->expr->expr_type == EXPR_VARIABLE && n->expr->symtree)
3552 use_sym = n->expr->symtree->n.sym;
3553
3554 *sym_based = false;
3555
3556 if (!use_sym)
3557 return n2;
3558
3559 if (!sym_rooted_nl)
3560 {
3561 sym_rooted_nl = new hash_map<gfc_symbol *, gfc_omp_namelist *> ();
3562
3563 for (; n2 != NULL; n2 = n2->next)
3564 {
3565 if (!n2->expr
3566 || n2->expr->expr_type != EXPR_VARIABLE
3567 || !n2->expr->symtree)
3568 continue;
3569
3570 gfc_omp_namelist *nl_copy = gfc_get_omp_namelist ();
3571 memcpy (nl_copy, n2, sizeof *nl_copy);
3572 nl_copy->u2.duplicate_of = n2;
3573 nl_copy->next = NULL;
3574
3575 gfc_symbol *idx_sym = n2->expr->symtree->n.sym;
3576
3577 bool existed;
3578 gfc_omp_namelist *&entry
3579 = sym_rooted_nl->get_or_insert (idx_sym, &existed);
3580 if (existed)
3581 nl_copy->next = entry;
3582 entry = nl_copy;
3583 }
3584 }
3585
3586 gfc_omp_namelist **n2_sym = sym_rooted_nl->get (use_sym);
3587
3588 if (n2_sym)
3589 {
3590 *sym_based = true;
3591 return *n2_sym;
3592 }
3593
3594 return NULL;
3595 }
3596
3597 static tree
3598 gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
3599 locus where, bool declare_simd = false,
3600 bool openacc = false, gfc_exec_op op = EXEC_NOP)
3601 {
3602 tree omp_clauses = NULL_TREE, prev_clauses, chunk_size, c;
3603 tree iterator = NULL_TREE;
3604 tree tree_block = NULL_TREE;
3605 stmtblock_t iter_block;
3606 int list, ifc;
3607 enum omp_clause_code clause_code;
3608 gfc_omp_namelist *prev = NULL;
3609 gfc_se se;
3610
3611 if (clauses == NULL)
3612 return NULL_TREE;
3613
3614 hash_map<gfc_symbol *, gfc_omp_namelist *> *sym_rooted_nl = NULL;
3615
3616 for (list = 0; list < OMP_LIST_NUM; list++)
3617 {
3618 gfc_omp_namelist *n = clauses->lists[list];
3619
3620 if (n == NULL)
3621 continue;
3622 switch (list)
3623 {
3624 case OMP_LIST_REDUCTION:
3625 case OMP_LIST_REDUCTION_INSCAN:
3626 case OMP_LIST_REDUCTION_TASK:
3627 case OMP_LIST_IN_REDUCTION:
3628 case OMP_LIST_TASK_REDUCTION:
3629 /* An OpenACC async clause indicates the need to set reduction
3630 arguments addressable, to allow asynchronous copy-out. */
3631 omp_clauses = gfc_trans_omp_reduction_list (list, n, omp_clauses,
3632 where, clauses->async);
3633 break;
3634 case OMP_LIST_PRIVATE:
3635 clause_code = OMP_CLAUSE_PRIVATE;
3636 goto add_clause;
3637 case OMP_LIST_SHARED:
3638 clause_code = OMP_CLAUSE_SHARED;
3639 goto add_clause;
3640 case OMP_LIST_FIRSTPRIVATE:
3641 clause_code = OMP_CLAUSE_FIRSTPRIVATE;
3642 goto add_clause;
3643 case OMP_LIST_LASTPRIVATE:
3644 clause_code = OMP_CLAUSE_LASTPRIVATE;
3645 goto add_clause;
3646 case OMP_LIST_COPYIN:
3647 clause_code = OMP_CLAUSE_COPYIN;
3648 goto add_clause;
3649 case OMP_LIST_COPYPRIVATE:
3650 clause_code = OMP_CLAUSE_COPYPRIVATE;
3651 goto add_clause;
3652 case OMP_LIST_UNIFORM:
3653 clause_code = OMP_CLAUSE_UNIFORM;
3654 goto add_clause;
3655 case OMP_LIST_USE_DEVICE:
3656 case OMP_LIST_USE_DEVICE_PTR:
3657 clause_code = OMP_CLAUSE_USE_DEVICE_PTR;
3658 goto add_clause;
3659 case OMP_LIST_USE_DEVICE_ADDR:
3660 clause_code = OMP_CLAUSE_USE_DEVICE_ADDR;
3661 goto add_clause;
3662 case OMP_LIST_IS_DEVICE_PTR:
3663 clause_code = OMP_CLAUSE_IS_DEVICE_PTR;
3664 goto add_clause;
3665 case OMP_LIST_HAS_DEVICE_ADDR:
3666 clause_code = OMP_CLAUSE_HAS_DEVICE_ADDR;
3667 goto add_clause;
3668 case OMP_LIST_NONTEMPORAL:
3669 clause_code = OMP_CLAUSE_NONTEMPORAL;
3670 goto add_clause;
3671 case OMP_LIST_SCAN_IN:
3672 clause_code = OMP_CLAUSE_INCLUSIVE;
3673 goto add_clause;
3674 case OMP_LIST_SCAN_EX:
3675 clause_code = OMP_CLAUSE_EXCLUSIVE;
3676 goto add_clause;
3677 case OMP_LIST_USE:
3678 clause_code = OMP_CLAUSE_USE;
3679 goto add_clause;
3680 case OMP_LIST_INTEROP:
3681 clause_code = OMP_CLAUSE_INTEROP;
3682 goto add_clause;
3683
3684 add_clause:
3685 omp_clauses
3686 = gfc_trans_omp_variable_list (clause_code, n, omp_clauses,
3687 declare_simd);
3688 break;
3689
3690 case OMP_LIST_DESTROY:
3691 for (; n != NULL; n = n->next)
3692 if (n->sym->attr.referenced)
3693 {
3694 tree t = gfc_trans_omp_variable (n->sym, declare_simd);
3695 if (t != error_mark_node)
3696 {
3697 tree node
3698 = build_omp_clause (input_location, OMP_CLAUSE_DESTROY);
3699 OMP_CLAUSE_DECL (node) = t;
3700 TREE_ADDRESSABLE (OMP_CLAUSE_DECL (node)) = 1;
3701 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
3702 }
3703 }
3704 break;
3705
3706 case OMP_LIST_INIT:
3707 {
3708 tree pref_type = NULL_TREE;
3709 const char *last = NULL;
3710 for (; n != NULL; n = n->next)
3711 if (n->sym->attr.referenced)
3712 {
3713 tree t = gfc_trans_omp_variable (n->sym, false);
3714 if (t == error_mark_node)
3715 continue;
3716 tree node = build_omp_clause (input_location,
3717 OMP_CLAUSE_INIT);
3718 OMP_CLAUSE_DECL (node) = t;
3719 TREE_ADDRESSABLE (OMP_CLAUSE_DECL (node)) = 1;
3720 if (n->u.init.target)
3721 OMP_CLAUSE_INIT_TARGET (node) = 1;
3722 if (n->u.init.targetsync)
3723 OMP_CLAUSE_INIT_TARGETSYNC (node) = 1;
3724 if (last != n->u2.init_interop)
3725 {
3726 last = n->u2.init_interop;
3727 if (n->u2.init_interop == NULL)
3728 pref_type = NULL_TREE;
3729 else
3730 {
3731 pref_type = build_string (n->u.init.len,
3732 n->u2.init_interop);
3733 TREE_TYPE (pref_type)
3734 = build_array_type_nelts (unsigned_char_type_node,
3735 n->u.init.len);
3736 }
3737 }
3738 OMP_CLAUSE_INIT_PREFER_TYPE (node) = pref_type;
3739 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
3740 }
3741 break;
3742 }
3743
3744 case OMP_LIST_ALIGNED:
3745 for (; n != NULL; n = n->next)
3746 if (n->sym->attr.referenced || declare_simd)
3747 {
3748 tree t = gfc_trans_omp_variable (n->sym, declare_simd);
3749 if (t != error_mark_node)
3750 {
3751 tree node = build_omp_clause (input_location,
3752 OMP_CLAUSE_ALIGNED);
3753 OMP_CLAUSE_DECL (node) = t;
3754 if (n->expr)
3755 {
3756 tree alignment_var;
3757
3758 if (declare_simd)
3759 alignment_var = gfc_conv_constant_to_tree (n->expr);
3760 else
3761 {
3762 gfc_init_se (&se, NULL);
3763 gfc_conv_expr (&se, n->expr);
3764 gfc_add_block_to_block (block, &se.pre);
3765 alignment_var = gfc_evaluate_now (se.expr, block);
3766 gfc_add_block_to_block (block, &se.post);
3767 }
3768 OMP_CLAUSE_ALIGNED_ALIGNMENT (node) = alignment_var;
3769 }
3770 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
3771 }
3772 }
3773 break;
3774 case OMP_LIST_ALLOCATE:
3775 {
3776 tree allocator_ = NULL_TREE;
3777 gfc_expr *alloc_expr = NULL;
3778 for (; n != NULL; n = n->next)
3779 if (n->sym->attr.referenced)
3780 {
3781 tree t = gfc_trans_omp_variable (n->sym, false);
3782 if (t != error_mark_node)
3783 {
3784 tree node = build_omp_clause (input_location,
3785 OMP_CLAUSE_ALLOCATE);
3786 OMP_CLAUSE_DECL (node) = t;
3787 if (n->u2.allocator)
3788 {
3789 if (alloc_expr != n->u2.allocator)
3790 {
3791 gfc_init_se (&se, NULL);
3792 gfc_conv_expr (&se, n->u2.allocator);
3793 gfc_add_block_to_block (block, &se.pre);
3794 allocator_ = gfc_evaluate_now (se.expr, block);
3795 gfc_add_block_to_block (block, &se.post);
3796 }
3797 OMP_CLAUSE_ALLOCATE_ALLOCATOR (node) = allocator_;
3798 }
3799 alloc_expr = n->u2.allocator;
3800 if (n->u.align)
3801 {
3802 tree align_;
3803 gfc_init_se (&se, NULL);
3804 gfc_conv_expr (&se, n->u.align);
3805 gcc_assert (CONSTANT_CLASS_P (se.expr)
3806 && se.pre.head == NULL
3807 && se.post.head == NULL);
3808 align_ = se.expr;
3809 OMP_CLAUSE_ALLOCATE_ALIGN (node) = align_;
3810 }
3811 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
3812 }
3813 }
3814 else
3815 alloc_expr = n->u2.allocator;
3816 }
3817 break;
3818 case OMP_LIST_LINEAR:
3819 {
3820 gfc_expr *last_step_expr = NULL;
3821 tree last_step = NULL_TREE;
3822 bool last_step_parm = false;
3823
3824 for (; n != NULL; n = n->next)
3825 {
3826 if (n->expr)
3827 {
3828 last_step_expr = n->expr;
3829 last_step = NULL_TREE;
3830 last_step_parm = false;
3831 }
3832 if (n->sym->attr.referenced || declare_simd)
3833 {
3834 tree t = gfc_trans_omp_variable (n->sym, declare_simd);
3835 if (t != error_mark_node)
3836 {
3837 tree node = build_omp_clause (input_location,
3838 OMP_CLAUSE_LINEAR);
3839 OMP_CLAUSE_DECL (node) = t;
3840 omp_clause_linear_kind kind;
3841 switch (n->u.linear.op)
3842 {
3843 case OMP_LINEAR_DEFAULT:
3844 kind = OMP_CLAUSE_LINEAR_DEFAULT;
3845 break;
3846 case OMP_LINEAR_REF:
3847 kind = OMP_CLAUSE_LINEAR_REF;
3848 break;
3849 case OMP_LINEAR_VAL:
3850 kind = OMP_CLAUSE_LINEAR_VAL;
3851 break;
3852 case OMP_LINEAR_UVAL:
3853 kind = OMP_CLAUSE_LINEAR_UVAL;
3854 break;
3855 default:
3856 gcc_unreachable ();
3857 }
3858 OMP_CLAUSE_LINEAR_KIND (node) = kind;
3859 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (node)
3860 = n->u.linear.old_modifier;
3861 if (last_step_expr && last_step == NULL_TREE)
3862 {
3863 if (!declare_simd)
3864 {
3865 gfc_init_se (&se, NULL);
3866 gfc_conv_expr (&se, last_step_expr);
3867 gfc_add_block_to_block (block, &se.pre);
3868 last_step = gfc_evaluate_now (se.expr, block);
3869 gfc_add_block_to_block (block, &se.post);
3870 }
3871 else if (last_step_expr->expr_type == EXPR_VARIABLE)
3872 {
3873 gfc_symbol *s = last_step_expr->symtree->n.sym;
3874 last_step = gfc_trans_omp_variable (s, true);
3875 last_step_parm = true;
3876 }
3877 else
3878 last_step
3879 = gfc_conv_constant_to_tree (last_step_expr);
3880 }
3881 if (last_step_parm)
3882 {
3883 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (node) = 1;
3884 OMP_CLAUSE_LINEAR_STEP (node) = last_step;
3885 }
3886 else
3887 {
3888 if (kind == OMP_CLAUSE_LINEAR_REF)
3889 {
3890 tree type;
3891 if (n->sym->attr.flavor == FL_PROCEDURE)
3892 {
3893 type = gfc_get_function_type (n->sym);
3894 type = build_pointer_type (type);
3895 }
3896 else
3897 type = gfc_sym_type (n->sym);
3898 if (POINTER_TYPE_P (type))
3899 type = TREE_TYPE (type);
3900 /* Otherwise to be determined what exactly
3901 should be done. */
3902 tree t = fold_convert (sizetype, last_step);
3903 t = size_binop (MULT_EXPR, t,
3904 TYPE_SIZE_UNIT (type));
3905 OMP_CLAUSE_LINEAR_STEP (node) = t;
3906 }
3907 else
3908 {
3909 tree type
3910 = gfc_typenode_for_spec (&n->sym->ts);
3911 OMP_CLAUSE_LINEAR_STEP (node)
3912 = fold_convert (type, last_step);
3913 }
3914 }
3915 if (n->sym->attr.dimension || n->sym->attr.allocatable)
3916 OMP_CLAUSE_LINEAR_ARRAY (node) = 1;
3917 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
3918 }
3919 }
3920 }
3921 }
3922 break;
3923 case OMP_LIST_AFFINITY:
3924 case OMP_LIST_DEPEND:
3925 iterator = NULL_TREE;
3926 prev = NULL;
3927 prev_clauses = omp_clauses;
3928 for (; n != NULL; n = n->next)
3929 {
3930 if (iterator && prev->u2.ns != n->u2.ns)
3931 {
3932 BLOCK_SUBBLOCKS (tree_block) = gfc_finish_block (&iter_block);
3933 TREE_VEC_ELT (iterator, 5) = tree_block;
3934 for (tree c = omp_clauses; c != prev_clauses;
3935 c = OMP_CLAUSE_CHAIN (c))
3936 OMP_CLAUSE_DECL (c) = build_tree_list (iterator,
3937 OMP_CLAUSE_DECL (c));
3938 prev_clauses = omp_clauses;
3939 iterator = NULL_TREE;
3940 }
3941 if (n->u2.ns && (!prev || prev->u2.ns != n->u2.ns))
3942 {
3943 gfc_init_block (&iter_block);
3944 tree_block = make_node (BLOCK);
3945 TREE_USED (tree_block) = 1;
3946 BLOCK_VARS (tree_block) = NULL_TREE;
3947 iterator = handle_iterator (n->u2.ns, block,
3948 tree_block);
3949 }
3950 if (!iterator)
3951 gfc_init_block (&iter_block);
3952 prev = n;
3953 if (list == OMP_LIST_DEPEND
3954 && (n->u.depend_doacross_op == OMP_DOACROSS_SINK_FIRST
3955 || n->u.depend_doacross_op == OMP_DEPEND_SINK_FIRST))
3956 {
3957 tree vec = NULL_TREE;
3958 unsigned int i;
3959 bool is_depend
3960 = n->u.depend_doacross_op == OMP_DEPEND_SINK_FIRST;
3961 for (i = 0; ; i++)
3962 {
3963 tree addend = integer_zero_node, t;
3964 bool neg = false;
3965 if (n->sym && n->expr)
3966 {
3967 addend = gfc_conv_constant_to_tree (n->expr);
3968 if (TREE_CODE (addend) == INTEGER_CST
3969 && tree_int_cst_sgn (addend) == -1)
3970 {
3971 neg = true;
3972 addend = const_unop (NEGATE_EXPR,
3973 TREE_TYPE (addend), addend);
3974 }
3975 }
3976
3977 if (n->sym == NULL)
3978 t = null_pointer_node; /* "omp_cur_iteration - 1". */
3979 else
3980 t = gfc_trans_omp_variable (n->sym, false);
3981 if (t != error_mark_node)
3982 {
3983 if (i < vec_safe_length (doacross_steps)
3984 && !integer_zerop (addend)
3985 && (*doacross_steps)[i])
3986 {
3987 tree step = (*doacross_steps)[i];
3988 addend = fold_convert (TREE_TYPE (step), addend);
3989 addend = build2 (TRUNC_DIV_EXPR,
3990 TREE_TYPE (step), addend, step);
3991 }
3992 vec = tree_cons (addend, t, vec);
3993 if (neg)
3994 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
3995 }
3996 if (n->next == NULL
3997 || n->next->u.depend_doacross_op != OMP_DOACROSS_SINK)
3998 break;
3999 n = n->next;
4000 }
4001 if (vec == NULL_TREE)
4002 continue;
4003
4004 tree node = build_omp_clause (input_location,
4005 OMP_CLAUSE_DOACROSS);
4006 OMP_CLAUSE_DOACROSS_KIND (node) = OMP_CLAUSE_DOACROSS_SINK;
4007 OMP_CLAUSE_DOACROSS_DEPEND (node) = is_depend;
4008 OMP_CLAUSE_DECL (node) = nreverse (vec);
4009 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
4010 continue;
4011 }
4012
4013 if (n->sym && !n->sym->attr.referenced)
4014 continue;
4015
4016 tree node = build_omp_clause (input_location,
4017 list == OMP_LIST_DEPEND
4018 ? OMP_CLAUSE_DEPEND
4019 : OMP_CLAUSE_AFFINITY);
4020 if (n->sym == NULL) /* omp_all_memory */
4021 OMP_CLAUSE_DECL (node) = null_pointer_node;
4022 else if (n->expr == NULL || n->expr->ref->u.ar.type == AR_FULL)
4023 {
4024 tree decl = gfc_trans_omp_variable (n->sym, false);
4025 if (gfc_omp_privatize_by_reference (decl))
4026 decl = build_fold_indirect_ref (decl);
4027 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
4028 {
4029 decl = gfc_conv_descriptor_data_get (decl);
4030 gcc_assert (POINTER_TYPE_P (TREE_TYPE (decl)));
4031 decl = build_fold_indirect_ref (decl);
4032 }
4033 else if (n->sym->attr.allocatable || n->sym->attr.pointer)
4034 decl = build_fold_indirect_ref (decl);
4035 else if (DECL_P (decl))
4036 TREE_ADDRESSABLE (decl) = 1;
4037 OMP_CLAUSE_DECL (node) = decl;
4038 }
4039 else
4040 {
4041 tree ptr;
4042 gfc_init_se (&se, NULL);
4043 /* The first ref can be an element selection on the base
4044 object while the full expression still denotes an array,
4045 e.g. x(j)%a. Pick the lowering path from the overall
4046 expression rank, not from the first REF_ARRAY. */
4047 if (n->expr->rank == 0)
4048 {
4049 gfc_conv_expr_reference (&se, n->expr);
4050 ptr = se.expr;
4051 }
4052 else
4053 {
4054 gfc_conv_expr_descriptor (&se, n->expr);
4055 ptr = gfc_conv_array_data (se.expr);
4056 }
4057 gfc_add_block_to_block (&iter_block, &se.pre);
4058 gfc_add_block_to_block (&iter_block, &se.post);
4059 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
4060 OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
4061 }
4062 if (list == OMP_LIST_DEPEND)
4063 switch (n->u.depend_doacross_op)
4064 {
4065 case OMP_DEPEND_IN:
4066 OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_IN;
4067 break;
4068 case OMP_DEPEND_OUT:
4069 OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_OUT;
4070 break;
4071 case OMP_DEPEND_INOUT:
4072 OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_INOUT;
4073 break;
4074 case OMP_DEPEND_INOUTSET:
4075 OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_INOUTSET;
4076 break;
4077 case OMP_DEPEND_MUTEXINOUTSET:
4078 OMP_CLAUSE_DEPEND_KIND (node)
4079 = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
4080 break;
4081 case OMP_DEPEND_DEPOBJ:
4082 OMP_CLAUSE_DEPEND_KIND (node) = OMP_CLAUSE_DEPEND_DEPOBJ;
4083 break;
4084 default:
4085 gcc_unreachable ();
4086 }
4087 if (!iterator)
4088 gfc_add_block_to_block (block, &iter_block);
4089 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
4090 }
4091 if (iterator)
4092 {
4093 BLOCK_SUBBLOCKS (tree_block) = gfc_finish_block (&iter_block);
4094 TREE_VEC_ELT (iterator, 5) = tree_block;
4095 for (tree c = omp_clauses; c != prev_clauses;
4096 c = OMP_CLAUSE_CHAIN (c))
4097 OMP_CLAUSE_DECL (c) = build_tree_list (iterator,
4098 OMP_CLAUSE_DECL (c));
4099 }
4100 break;
4101 case OMP_LIST_MAP:
4102 for (; n != NULL; n = n->next)
4103 {
4104 if (!n->sym->attr.referenced)
4105 continue;
4106
4107 location_t map_loc = gfc_get_location (&n->where);
4108 bool always_modifier = false;
4109 tree node = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4110 tree node2 = NULL_TREE;
4111 tree node3 = NULL_TREE;
4112 tree node4 = NULL_TREE;
4113 tree node5 = NULL_TREE;
4114
4115 /* OpenMP: automatically map pointer targets with the pointer;
4116 hence, always update the descriptor/pointer itself. */
4117 if (!openacc
4118 && ((n->expr == NULL && n->sym->attr.pointer)
4119 || (n->expr && gfc_expr_attr (n->expr).pointer)))
4120 always_modifier = true;
4121
4122 if (n->u.map.readonly)
4123 OMP_CLAUSE_MAP_READONLY (node) = 1;
4124
4125 switch (n->u.map.op)
4126 {
4127 case OMP_MAP_ALLOC:
4128 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALLOC);
4129 break;
4130 case OMP_MAP_IF_PRESENT:
4131 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_IF_PRESENT);
4132 break;
4133 case OMP_MAP_ATTACH:
4134 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ATTACH);
4135 break;
4136 case OMP_MAP_TO:
4137 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_TO);
4138 break;
4139 case OMP_MAP_FROM:
4140 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FROM);
4141 break;
4142 case OMP_MAP_TOFROM:
4143 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_TOFROM);
4144 break;
4145 case OMP_MAP_ALWAYS_TO:
4146 always_modifier = true;
4147 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_TO);
4148 break;
4149 case OMP_MAP_ALWAYS_FROM:
4150 always_modifier = true;
4151 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_FROM);
4152 break;
4153 case OMP_MAP_ALWAYS_TOFROM:
4154 always_modifier = true;
4155 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_TOFROM);
4156 break;
4157 case OMP_MAP_PRESENT_ALLOC:
4158 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_ALLOC);
4159 break;
4160 case OMP_MAP_PRESENT_TO:
4161 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_TO);
4162 break;
4163 case OMP_MAP_PRESENT_FROM:
4164 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_FROM);
4165 break;
4166 case OMP_MAP_PRESENT_TOFROM:
4167 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_PRESENT_TOFROM);
4168 break;
4169 case OMP_MAP_ALWAYS_PRESENT_TO:
4170 always_modifier = true;
4171 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_TO);
4172 break;
4173 case OMP_MAP_ALWAYS_PRESENT_FROM:
4174 always_modifier = true;
4175 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_FROM);
4176 break;
4177 case OMP_MAP_ALWAYS_PRESENT_TOFROM:
4178 always_modifier = true;
4179 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_ALWAYS_PRESENT_TOFROM);
4180 break;
4181 case OMP_MAP_RELEASE:
4182 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_RELEASE);
4183 break;
4184 case OMP_MAP_DELETE:
4185 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DELETE);
4186 break;
4187 case OMP_MAP_DETACH:
4188 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DETACH);
4189 break;
4190 case OMP_MAP_FORCE_ALLOC:
4191 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_ALLOC);
4192 break;
4193 case OMP_MAP_FORCE_TO:
4194 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_TO);
4195 break;
4196 case OMP_MAP_FORCE_FROM:
4197 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_FROM);
4198 break;
4199 case OMP_MAP_FORCE_TOFROM:
4200 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_TOFROM);
4201 break;
4202 case OMP_MAP_FORCE_PRESENT:
4203 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_PRESENT);
4204 break;
4205 case OMP_MAP_FORCE_DEVICEPTR:
4206 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_DEVICEPTR);
4207 break;
4208 default:
4209 gcc_unreachable ();
4210 }
4211
4212 tree decl = gfc_trans_omp_variable (n->sym, false);
4213 if (DECL_P (decl))
4214 TREE_ADDRESSABLE (decl) = 1;
4215
4216 gfc_ref *lastref = NULL;
4217
4218 if (n->expr)
4219 for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
4220 if (ref->type == REF_COMPONENT || ref->type == REF_ARRAY)
4221 lastref = ref;
4222
4223 bool allocatable = false, pointer = false;
4224
4225 if (lastref && lastref->type == REF_COMPONENT)
4226 {
4227 gfc_component *c = lastref->u.c.component;
4228
4229 if (c->ts.type == BT_CLASS)
4230 {
4231 pointer = CLASS_DATA (c)->attr.class_pointer;
4232 allocatable = CLASS_DATA (c)->attr.allocatable;
4233 }
4234 else
4235 {
4236 pointer = c->attr.pointer;
4237 allocatable = c->attr.allocatable;
4238 }
4239 }
4240
4241 if (n->expr == NULL
4242 || (n->expr->ref->type == REF_ARRAY
4243 && n->expr->ref->u.ar.type == AR_FULL))
4244 {
4245 gomp_map_kind map_kind;
4246 tree type = TREE_TYPE (decl);
4247 if (n->sym->ts.type == BT_CHARACTER
4248 && n->sym->ts.deferred
4249 && n->sym->attr.omp_declare_target
4250 && (always_modifier || n->sym->attr.pointer)
4251 && op != EXEC_OMP_TARGET_EXIT_DATA
4252 && n->u.map.op != OMP_MAP_DELETE
4253 && n->u.map.op != OMP_MAP_RELEASE)
4254 {
4255 gcc_assert (n->sym->ts.u.cl->backend_decl);
4256 node5 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4257 OMP_CLAUSE_SET_MAP_KIND (node5, GOMP_MAP_ALWAYS_TO);
4258 OMP_CLAUSE_DECL (node5) = n->sym->ts.u.cl->backend_decl;
4259 OMP_CLAUSE_SIZE (node5)
4260 = TYPE_SIZE_UNIT (gfc_charlen_type_node);
4261 }
4262
4263 tree present = gfc_omp_check_optional_argument (decl, true);
4264 if (openacc && n->sym->ts.type == BT_CLASS)
4265 {
4266 if (n->sym->attr.optional)
4267 sorry_at (gfc_get_location (&n->where),
4268 "optional class parameter");
4269 tree ptr = gfc_class_data_get (decl);
4270 ptr = build_fold_indirect_ref (ptr);
4271 OMP_CLAUSE_DECL (node) = ptr;
4272 OMP_CLAUSE_SIZE (node) = gfc_class_vtab_size_get (decl);
4273 node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4274 OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_ATTACH_DETACH);
4275 OMP_CLAUSE_DECL (node2) = gfc_class_data_get (decl);
4276 OMP_CLAUSE_SIZE (node2) = size_int (0);
4277 goto finalize_map_clause;
4278 }
4279 else if (POINTER_TYPE_P (type)
4280 && (gfc_omp_privatize_by_reference (decl)
4281 || GFC_DECL_GET_SCALAR_POINTER (decl)
4282 || GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
4283 || GFC_DECL_CRAY_POINTEE (decl)
4284 || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))
4285 || (n->sym->ts.type == BT_DERIVED
4286 && (n->sym->ts.u.derived->ts.f90_type
4287 != BT_VOID))))
4288 {
4289 tree orig_decl = decl;
4290 bool bare_attach_detach
4291 = (openacc
4292 && (n->u.map.op == OMP_MAP_ATTACH
4293 || n->u.map.op == OMP_MAP_DETACH)
4294 && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
4295 && !(POINTER_TYPE_P (TREE_TYPE (decl))
4296 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE
4297 (TREE_TYPE (decl)))));
4298
4299 /* For nonallocatable, nonpointer arrays, a temporary
4300 variable is generated, but this one is only defined if
4301 the variable is present; hence, we now set it to NULL
4302 to avoid accessing undefined variables. We cannot use
4303 a temporary variable here as otherwise the replacement
4304 of the variables in omp-low.cc will not work. */
4305 if (present && GFC_ARRAY_TYPE_P (type))
4306 {
4307 tree tmp = fold_build2_loc (input_location,
4308 MODIFY_EXPR,
4309 void_type_node, decl,
4310 null_pointer_node);
4311 tree cond = fold_build1_loc (input_location,
4312 TRUTH_NOT_EXPR,
4313 boolean_type_node,
4314 present);
4315 gfc_add_expr_to_block (block,
4316 build3_loc (input_location,
4317 COND_EXPR,
4318 void_type_node,
4319 cond, tmp,
4320 NULL_TREE));
4321 }
4322 /* Bare OpenACC attach/detach on scalar pointer-like
4323 variables wants a single attach operation on the
4324 pointer itself, not a standalone pointer-mapping
4325 node. Component and descriptor cases have dedicated
4326 handling below; this covers the plain scalar path. */
4327 if (bare_attach_detach)
4328 {
4329 decl = build_fold_indirect_ref (decl);
4330 OMP_CLAUSE_DECL (node) = build_fold_addr_expr (decl);
4331 OMP_CLAUSE_SIZE (node) = size_zero_node;
4332 goto finalize_map_clause;
4333 }
4334 /* For descriptor types, the unmapping happens below. */
4335 if (op != EXEC_OMP_TARGET_EXIT_DATA
4336 || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
4337 {
4338 enum gomp_map_kind gmk = GOMP_MAP_POINTER;
4339 if (op == EXEC_OMP_TARGET_EXIT_DATA
4340 && n->u.map.op == OMP_MAP_DELETE)
4341 gmk = GOMP_MAP_DELETE;
4342 else if (op == EXEC_OMP_TARGET_EXIT_DATA)
4343 gmk = GOMP_MAP_RELEASE;
4344 tree size;
4345 if (gmk == GOMP_MAP_RELEASE || gmk == GOMP_MAP_DELETE)
4346 size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
4347 else
4348 size = size_int (0);
4349 node4 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4350 OMP_CLAUSE_SET_MAP_KIND (node4, gmk);
4351 OMP_CLAUSE_DECL (node4) = decl;
4352 OMP_CLAUSE_SIZE (node4) = size;
4353 }
4354 decl = build_fold_indirect_ref (decl);
4355 if ((TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE
4356 || gfc_omp_is_optional_argument (orig_decl))
4357 && (GFC_DECL_GET_SCALAR_POINTER (orig_decl)
4358 || GFC_DECL_GET_SCALAR_ALLOCATABLE (orig_decl)))
4359 {
4360 enum gomp_map_kind gmk;
4361 if (op == EXEC_OMP_TARGET_EXIT_DATA
4362 && n->u.map.op == OMP_MAP_DELETE)
4363 gmk = GOMP_MAP_DELETE;
4364 else if (op == EXEC_OMP_TARGET_EXIT_DATA)
4365 gmk = GOMP_MAP_RELEASE;
4366 else
4367 gmk = GOMP_MAP_POINTER;
4368 tree size;
4369 if (gmk == GOMP_MAP_RELEASE || gmk == GOMP_MAP_DELETE)
4370 size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
4371 else
4372 size = size_int (0);
4373 node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4374 OMP_CLAUSE_SET_MAP_KIND (node3, gmk);
4375 OMP_CLAUSE_DECL (node3) = decl;
4376 OMP_CLAUSE_SIZE (node3) = size;
4377 decl = build_fold_indirect_ref (decl);
4378 }
4379 }
4380 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
4381 {
4382 tree type = TREE_TYPE (decl);
4383 tree ptr = gfc_conv_descriptor_data_get (decl);
4384 if (present)
4385 ptr = gfc_build_cond_assign_expr (block, present, ptr,
4386 null_pointer_node);
4387 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
4388 ptr = build_fold_indirect_ref (ptr);
4389 OMP_CLAUSE_DECL (node) = ptr;
4390 node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4391 OMP_CLAUSE_DECL (node2) = decl;
4392 OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
4393 if (n->u.map.op == OMP_MAP_DELETE)
4394 map_kind = GOMP_MAP_DELETE;
4395 else if (op == EXEC_OMP_TARGET_EXIT_DATA
4396 || n->u.map.op == OMP_MAP_RELEASE)
4397 map_kind = GOMP_MAP_RELEASE;
4398 else
4399 map_kind = GOMP_MAP_TO_PSET;
4400 OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
4401
4402 if (op != EXEC_OMP_TARGET_EXIT_DATA
4403 && n->u.map.op != OMP_MAP_DELETE
4404 && n->u.map.op != OMP_MAP_RELEASE)
4405 {
4406 node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4407 if (present)
4408 {
4409 ptr = gfc_conv_descriptor_data_get (decl);
4410 ptr = gfc_build_addr_expr (NULL, ptr);
4411 ptr = gfc_build_cond_assign_expr (
4412 block, present, ptr, null_pointer_node);
4413 ptr = build_fold_indirect_ref (ptr);
4414 OMP_CLAUSE_DECL (node3) = ptr;
4415 }
4416 else
4417 OMP_CLAUSE_DECL (node3)
4418 = gfc_conv_descriptor_data_get (decl);
4419 OMP_CLAUSE_SIZE (node3) = size_int (0);
4420
4421 if (n->u.map.op == OMP_MAP_ATTACH)
4422 {
4423 /* Standalone attach clauses used with arrays with
4424 descriptors must copy the descriptor to the
4425 target, else they won't have anything to
4426 perform the attachment onto (see OpenACC 2.6,
4427 "2.6.3. Data Structures with Pointers"). */
4428 OMP_CLAUSE_SET_MAP_KIND (node3, GOMP_MAP_ATTACH);
4429 /* We don't want to map PTR at all in this case,
4430 so delete its node and shuffle the others
4431 down. */
4432 node = node2;
4433 node2 = node3;
4434 node3 = NULL;
4435 goto finalize_map_clause;
4436 }
4437 else if (n->u.map.op == OMP_MAP_DETACH)
4438 {
4439 OMP_CLAUSE_SET_MAP_KIND (node3, GOMP_MAP_DETACH);
4440 /* Similarly to above, we don't want to unmap PTR
4441 here. */
4442 node = node2;
4443 node2 = node3;
4444 node3 = NULL;
4445 goto finalize_map_clause;
4446 }
4447 else
4448 OMP_CLAUSE_SET_MAP_KIND (node3,
4449 always_modifier
4450 ? GOMP_MAP_ALWAYS_POINTER
4451 : GOMP_MAP_POINTER);
4452 }
4453
4454 /* We have to check for n->sym->attr.dimension because
4455 of scalar coarrays. */
4456 if ((n->sym->attr.pointer || n->sym->attr.allocatable)
4457 && n->sym->attr.dimension)
4458 {
4459 stmtblock_t cond_block;
4460 tree size
4461 = gfc_create_var (gfc_array_index_type, NULL);
4462 tree tem, then_b, else_b, zero, cond;
4463
4464 gfc_init_block (&cond_block);
4465 tem
4466 = gfc_full_array_size (&cond_block, decl,
4467 GFC_TYPE_ARRAY_RANK (type));
4468 tree elemsz;
4469 if (n->sym->ts.type == BT_CHARACTER
4470 && n->sym->ts.deferred)
4471 {
4472 tree len = n->sym->ts.u.cl->backend_decl;
4473 len = fold_convert (size_type_node, len);
4474 elemsz = gfc_get_char_type (n->sym->ts.kind);
4475 elemsz = TYPE_SIZE_UNIT (elemsz);
4476 elemsz = fold_build2 (MULT_EXPR, size_type_node,
4477 len, elemsz);
4478 }
4479 else
4480 elemsz
4481 = TYPE_SIZE_UNIT (gfc_get_element_type (type));
4482 elemsz = fold_convert (gfc_array_index_type, elemsz);
4483 tem = fold_build2 (MULT_EXPR, gfc_array_index_type,
4484 tem, elemsz);
4485 gfc_add_modify (&cond_block, size, tem);
4486 then_b = gfc_finish_block (&cond_block);
4487 gfc_init_block (&cond_block);
4488 zero = build_int_cst (gfc_array_index_type, 0);
4489 gfc_add_modify (&cond_block, size, zero);
4490 else_b = gfc_finish_block (&cond_block);
4491 tem = gfc_conv_descriptor_data_get (decl);
4492 tem = fold_convert (pvoid_type_node, tem);
4493 cond = fold_build2_loc (input_location, NE_EXPR,
4494 boolean_type_node,
4495 tem, null_pointer_node);
4496 if (present)
4497 cond = fold_build2_loc (input_location,
4498 TRUTH_ANDIF_EXPR,
4499 boolean_type_node,
4500 present, cond);
4501 gfc_add_expr_to_block (block,
4502 build3_loc (input_location,
4503 COND_EXPR,
4504 void_type_node,
4505 cond, then_b,
4506 else_b));
4507 OMP_CLAUSE_SIZE (node) = size;
4508 }
4509 else if (n->sym->attr.dimension)
4510 {
4511 stmtblock_t cond_block;
4512 gfc_init_block (&cond_block);
4513 tree size = gfc_full_array_size (&cond_block, decl,
4514 GFC_TYPE_ARRAY_RANK (type));
4515 tree elemsz
4516 = TYPE_SIZE_UNIT (gfc_get_element_type (type));
4517 elemsz = fold_convert (gfc_array_index_type, elemsz);
4518 size = fold_build2 (MULT_EXPR, gfc_array_index_type,
4519 size, elemsz);
4520 size = gfc_evaluate_now (size, &cond_block);
4521 if (present)
4522 {
4523 tree var = gfc_create_var (gfc_array_index_type,
4524 NULL);
4525 gfc_add_modify (&cond_block, var, size);
4526 tree cond_body = gfc_finish_block (&cond_block);
4527 tree cond = build3_loc (input_location, COND_EXPR,
4528 void_type_node, present,
4529 cond_body, NULL_TREE);
4530 gfc_add_expr_to_block (block, cond);
4531 OMP_CLAUSE_SIZE (node) = var;
4532 }
4533 else
4534 {
4535 gfc_add_block_to_block (block, &cond_block);
4536 OMP_CLAUSE_SIZE (node) = size;
4537 }
4538 }
4539 }
4540 else if (present
4541 && INDIRECT_REF_P (decl)
4542 && INDIRECT_REF_P (TREE_OPERAND (decl, 0)))
4543 {
4544 /* A single indirectref is handled by the middle end. */
4545 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
4546 tree tmp = TREE_OPERAND (decl, 0);
4547 tmp = gfc_build_cond_assign_expr (block, present, tmp,
4548 null_pointer_node);
4549 OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (tmp);
4550 }
4551 else
4552 OMP_CLAUSE_DECL (node) = decl;
4553
4554 if (!n->sym->attr.dimension
4555 && n->sym->ts.type == BT_CHARACTER
4556 && n->sym->ts.deferred)
4557 {
4558 if (!DECL_P (decl))
4559 {
4560 gcc_assert (TREE_CODE (decl) == INDIRECT_REF);
4561 decl = TREE_OPERAND (decl, 0);
4562 }
4563 tree cond = fold_build2_loc (input_location, NE_EXPR,
4564 boolean_type_node,
4565 decl, null_pointer_node);
4566 if (present)
4567 cond = fold_build2_loc (input_location,
4568 TRUTH_ANDIF_EXPR,
4569 boolean_type_node,
4570 present, cond);
4571 tree len = n->sym->ts.u.cl->backend_decl;
4572 len = fold_convert (size_type_node, len);
4573 tree size = gfc_get_char_type (n->sym->ts.kind);
4574 size = TYPE_SIZE_UNIT (size);
4575 size = fold_build2 (MULT_EXPR, size_type_node, len, size);
4576 size = build3_loc (input_location,
4577 COND_EXPR,
4578 size_type_node,
4579 cond, size,
4580 size_zero_node);
4581 size = gfc_evaluate_now (size, block);
4582 OMP_CLAUSE_SIZE (node) = size;
4583 }
4584 if ((TREE_CODE (decl) != PARM_DECL
4585 || DECL_ARTIFICIAL (OMP_CLAUSE_DECL (node)))
4586 && n->sym->ts.type == BT_DERIVED
4587 && n->sym->ts.u.derived->attr.alloc_comp)
4588 {
4589 /* Save array descriptor for use in
4590 gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
4591 to ensure that it is not gimplified + is a decl. */
4592 tree tmp = OMP_CLAUSE_SIZE (node);
4593 if (tmp == NULL_TREE)
4594 tmp = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
4595 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
4596 tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
4597 gfc_add_modify_loc (input_location, block, var, tmp);
4598 OMP_CLAUSE_SIZE (node) = var;
4599 gfc_allocate_lang_decl (var);
4600 if (TREE_CODE (decl) == INDIRECT_REF)
4601 decl = TREE_OPERAND (decl, 0);
4602 if (TREE_CODE (decl) == INDIRECT_REF)
4603 decl = TREE_OPERAND (decl, 0);
4604 if (DECL_LANG_SPECIFIC (decl)
4605 && GFC_DECL_SAVED_DESCRIPTOR (decl))
4606 GFC_DECL_SAVED_DESCRIPTOR (var)
4607 = GFC_DECL_SAVED_DESCRIPTOR (decl);
4608 else
4609 GFC_DECL_SAVED_DESCRIPTOR (var) = decl;
4610 }
4611 }
4612 else if (n->expr
4613 && n->expr->expr_type == EXPR_VARIABLE
4614 && n->expr->ref->type == REF_ARRAY
4615 && !n->expr->ref->next)
4616 {
4617 /* An array element or array section which is not part of a
4618 derived type, etc. */
4619 bool element = n->expr->ref->u.ar.type == AR_ELEMENT;
4620 tree type = TREE_TYPE (decl);
4621 gomp_map_kind k = GOMP_MAP_POINTER;
4622 if (!openacc
4623 && !GFC_DESCRIPTOR_TYPE_P (type)
4624 && !(POINTER_TYPE_P (type)
4625 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type))))
4626 k = GOMP_MAP_FIRSTPRIVATE_POINTER;
4627 gfc_trans_omp_array_section (block, op, n, decl, element,
4628 !openacc, k, node, node2,
4629 node3, node4);
4630 }
4631 else if (n->expr
4632 && n->expr->expr_type == EXPR_VARIABLE
4633 && (n->expr->ref->type == REF_COMPONENT
4634 || n->expr->ref->type == REF_ARRAY)
4635 && lastref
4636 && lastref->type == REF_COMPONENT
4637 && lastref->u.c.component->ts.type != BT_CLASS
4638 && lastref->u.c.component->ts.type != BT_DERIVED
4639 && !lastref->u.c.component->attr.dimension)
4640 {
4641 /* Derived type access with last component being a scalar. */
4642 gfc_init_se (&se, NULL);
4643
4644 gfc_conv_expr (&se, n->expr);
4645 gfc_add_block_to_block (block, &se.pre);
4646 /* For BT_CHARACTER a pointer is returned. */
4647 OMP_CLAUSE_DECL (node)
4648 = POINTER_TYPE_P (TREE_TYPE (se.expr))
4649 ? build_fold_indirect_ref (se.expr) : se.expr;
4650 gfc_add_block_to_block (block, &se.post);
4651 if (pointer || allocatable)
4652 {
4653 /* If it's a bare attach/detach clause, we just want
4654 to perform a single attach/detach operation, of the
4655 pointer itself, not of the pointed-to object. */
4656 if (openacc
4657 && (n->u.map.op == OMP_MAP_ATTACH
4658 || n->u.map.op == OMP_MAP_DETACH))
4659 {
4660 OMP_CLAUSE_DECL (node)
4661 = build_fold_addr_expr (OMP_CLAUSE_DECL (node));
4662 OMP_CLAUSE_SIZE (node) = size_zero_node;
4663 goto finalize_map_clause;
4664 }
4665
4666 node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4667 OMP_CLAUSE_SET_MAP_KIND (node2, GOMP_MAP_ATTACH_DETACH);
4668 OMP_CLAUSE_DECL (node2)
4669 = POINTER_TYPE_P (TREE_TYPE (se.expr))
4670 ? se.expr
4671 : gfc_build_addr_expr (NULL, se.expr);
4672 OMP_CLAUSE_SIZE (node2) = size_int (0);
4673 if (!openacc
4674 && n->expr->ts.type == BT_CHARACTER
4675 && n->expr->ts.deferred)
4676 {
4677 gcc_assert (se.string_length);
4678 tree tmp
4679 = gfc_get_char_type (n->expr->ts.kind);
4680 OMP_CLAUSE_SIZE (node)
4681 = fold_build2 (MULT_EXPR, size_type_node,
4682 fold_convert (size_type_node,
4683 se.string_length),
4684 TYPE_SIZE_UNIT (tmp));
4685 gomp_map_kind kind;
4686 if (n->u.map.op == OMP_MAP_DELETE)
4687 kind = GOMP_MAP_DELETE;
4688 else if (op == EXEC_OMP_TARGET_EXIT_DATA)
4689 kind = GOMP_MAP_RELEASE;
4690 else
4691 kind = GOMP_MAP_TO;
4692 node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4693 OMP_CLAUSE_SET_MAP_KIND (node3, kind);
4694 OMP_CLAUSE_DECL (node3) = se.string_length;
4695 OMP_CLAUSE_SIZE (node3)
4696 = TYPE_SIZE_UNIT (gfc_charlen_type_node);
4697 }
4698 if (!openacc
4699 && n->expr->ts.type == BT_DERIVED
4700 && n->expr->ts.u.derived->attr.alloc_comp)
4701 {
4702 /* Save array descriptor for use in
4703 gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
4704 to ensure that it is not gimplified + is a decl. */
4705 tree tmp = OMP_CLAUSE_SIZE (node);
4706 if (tmp == NULL_TREE)
4707 tmp = (DECL_P (se.expr)
4708 ? DECL_SIZE_UNIT (se.expr)
4709 : TYPE_SIZE_UNIT (TREE_TYPE (se.expr)));
4710 tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
4711 gfc_add_modify_loc (input_location, block, var, tmp);
4712 OMP_CLAUSE_SIZE (node) = var;
4713 gfc_allocate_lang_decl (var);
4714 if (TREE_CODE (se.expr) == INDIRECT_REF)
4715 se.expr = TREE_OPERAND (se.expr, 0);
4716 if (DECL_LANG_SPECIFIC (se.expr)
4717 && GFC_DECL_SAVED_DESCRIPTOR (se.expr))
4718 GFC_DECL_SAVED_DESCRIPTOR (var)
4719 = GFC_DECL_SAVED_DESCRIPTOR (se.expr);
4720 else
4721 GFC_DECL_SAVED_DESCRIPTOR (var) = se.expr;
4722 }
4723 }
4724 }
4725 else if (n->expr
4726 && n->expr->expr_type == EXPR_VARIABLE
4727 && (n->expr->ref->type == REF_COMPONENT
4728 || n->expr->ref->type == REF_ARRAY))
4729 {
4730 gfc_init_se (&se, NULL);
4731 se.expr = gfc_maybe_dereference_var (n->sym, decl);
4732
4733 for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
4734 {
4735 if (ref->type == REF_COMPONENT)
4736 {
4737 if (ref->u.c.sym->attr.extension)
4738 conv_parent_component_references (&se, ref);
4739
4740 gfc_conv_component_ref (&se, ref);
4741 }
4742 else if (ref->type == REF_ARRAY)
4743 {
4744 if (ref->u.ar.type == AR_ELEMENT && ref->next)
4745 gfc_conv_array_ref (&se, &ref->u.ar, n->expr,
4746 &n->expr->where);
4747 else
4748 gcc_assert (!ref->next);
4749 }
4750 else
4751 sorry_at (gfc_get_location (&n->where),
4752 "unhandled expression type");
4753 }
4754
4755 tree inner = se.expr;
4756
4757 /* Last component is a derived type or class pointer. */
4758 if (lastref->type == REF_COMPONENT
4759 && (lastref->u.c.component->ts.type == BT_DERIVED
4760 || lastref->u.c.component->ts.type == BT_CLASS))
4761 {
4762 if (pointer || allocatable)
4763 {
4764 /* If it's a bare attach/detach clause, we just want
4765 to perform a single attach/detach operation, of the
4766 pointer itself, not of the pointed-to object. */
4767 if (openacc
4768 && (n->u.map.op == OMP_MAP_ATTACH
4769 || n->u.map.op == OMP_MAP_DETACH))
4770 {
4771 OMP_CLAUSE_DECL (node)
4772 = build_fold_addr_expr (inner);
4773 OMP_CLAUSE_SIZE (node) = size_zero_node;
4774 goto finalize_map_clause;
4775 }
4776
4777 gfc_omp_namelist *n2
4778 = openacc ? NULL : clauses->lists[OMP_LIST_MAP];
4779
4780 bool sym_based;
4781 n2 = get_symbol_rooted_namelist (sym_rooted_nl, n,
4782 n2, &sym_based);
4783
4784 /* If the last reference is a pointer to a derived
4785 type ("foo%dt_ptr"), check if any subcomponents
4786 of the same derived type member are being mapped
4787 elsewhere in the clause list ("foo%dt_ptr%x",
4788 etc.). If we have such subcomponent mappings,
4789 we only create an ALLOC node for the pointer
4790 itself, and inhibit mapping the whole derived
4791 type. */
4792
4793 for (; n2 != NULL; n2 = n2->next)
4794 {
4795 if ((!sym_based && n == n2)
4796 || (sym_based && n == n2->u2.duplicate_of)
4797 || !n2->expr)
4798 continue;
4799
4800 if (!gfc_omp_expr_prefix_same (n->expr,
4801 n2->expr))
4802 continue;
4803
4804 gfc_ref *ref1 = n->expr->ref;
4805 gfc_ref *ref2 = n2->expr->ref;
4806
4807 while (ref1->next && ref2->next)
4808 {
4809 ref1 = ref1->next;
4810 ref2 = ref2->next;
4811 }
4812
4813 if (ref2->next)
4814 {
4815 inner = build_fold_addr_expr (inner);
4816 OMP_CLAUSE_SET_MAP_KIND (node,
4817 GOMP_MAP_ALLOC);
4818 OMP_CLAUSE_DECL (node) = inner;
4819 OMP_CLAUSE_SIZE (node)
4820 = TYPE_SIZE_UNIT (TREE_TYPE (inner));
4821 goto finalize_map_clause;
4822 }
4823 }
4824
4825 tree data, size;
4826
4827 if (lastref->u.c.component->ts.type == BT_CLASS)
4828 {
4829 data = gfc_class_data_get (inner);
4830 gcc_assert (POINTER_TYPE_P (TREE_TYPE (data)));
4831 data = build_fold_indirect_ref (data);
4832 size = gfc_class_vtab_size_get (inner);
4833 }
4834 else /* BT_DERIVED. */
4835 {
4836 data = inner;
4837 size = TYPE_SIZE_UNIT (TREE_TYPE (inner));
4838 }
4839
4840 OMP_CLAUSE_DECL (node) = data;
4841 OMP_CLAUSE_SIZE (node) = size;
4842 node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4843 OMP_CLAUSE_SET_MAP_KIND (node2,
4844 GOMP_MAP_ATTACH_DETACH);
4845 OMP_CLAUSE_DECL (node2) = build_fold_addr_expr (data);
4846 OMP_CLAUSE_SIZE (node2) = size_int (0);
4847 }
4848 else
4849 {
4850 OMP_CLAUSE_DECL (node) = inner;
4851 OMP_CLAUSE_SIZE (node)
4852 = TYPE_SIZE_UNIT (TREE_TYPE (inner));
4853 }
4854 if (!openacc
4855 && n->expr->ts.type == BT_DERIVED
4856 && n->expr->ts.u.derived->attr.alloc_comp)
4857 {
4858 /* Save array descriptor for use in
4859 gfc_omp_deep_mapping{,_p,_cnt}; force evaluate
4860 to ensure that it is not gimplified + is a decl. */
4861 tree tmp = OMP_CLAUSE_SIZE (node);
4862 tree var = gfc_create_var (TREE_TYPE (tmp), NULL);
4863 gfc_add_modify_loc (input_location, block, var, tmp);
4864 OMP_CLAUSE_SIZE (node) = var;
4865 gfc_allocate_lang_decl (var);
4866 if (TREE_CODE (inner) == INDIRECT_REF)
4867 inner = TREE_OPERAND (inner, 0);
4868 GFC_DECL_SAVED_DESCRIPTOR (var) = inner;
4869 }
4870 }
4871 else if (lastref->type == REF_ARRAY
4872 && lastref->u.ar.type == AR_FULL)
4873 {
4874 /* Bare attach and detach clauses don't want any
4875 additional nodes. */
4876 if ((n->u.map.op == OMP_MAP_ATTACH
4877 || n->u.map.op == OMP_MAP_DETACH)
4878 && (POINTER_TYPE_P (TREE_TYPE (inner))
4879 || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner))))
4880 {
4881 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner)))
4882 {
4883 tree ptr = gfc_conv_descriptor_data_get (inner);
4884 OMP_CLAUSE_DECL (node) = ptr;
4885 }
4886 else
4887 OMP_CLAUSE_DECL (node) = inner;
4888 OMP_CLAUSE_SIZE (node) = size_zero_node;
4889 goto finalize_map_clause;
4890 }
4891
4892 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (inner)))
4893 {
4894 gomp_map_kind map_kind;
4895 tree type = TREE_TYPE (inner);
4896 tree ptr = gfc_conv_descriptor_data_get (inner);
4897 ptr = build_fold_indirect_ref (ptr);
4898 OMP_CLAUSE_DECL (node) = ptr;
4899 int rank = GFC_TYPE_ARRAY_RANK (type);
4900 OMP_CLAUSE_SIZE (node)
4901 = gfc_full_array_size (block, inner, rank);
4902 tree elemsz
4903 = TYPE_SIZE_UNIT (gfc_get_element_type (type));
4904 map_kind = OMP_CLAUSE_MAP_KIND (node);
4905 if (GOMP_MAP_COPY_TO_P (map_kind)
4906 || map_kind == GOMP_MAP_ALLOC)
4907 map_kind = ((GOMP_MAP_ALWAYS_P (map_kind)
4908 || gfc_expr_attr (n->expr).pointer)
4909 ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO);
4910 else if (n->u.map.op == OMP_MAP_RELEASE
4911 || n->u.map.op == OMP_MAP_DELETE)
4912 ;
4913 else if (op == EXEC_OMP_TARGET_EXIT_DATA
4914 || op == EXEC_OACC_EXIT_DATA)
4915 map_kind = GOMP_MAP_RELEASE;
4916 else
4917 map_kind = GOMP_MAP_ALLOC;
4918 if (!openacc
4919 && n->expr->ts.type == BT_CHARACTER
4920 && n->expr->ts.deferred)
4921 {
4922 gcc_assert (se.string_length);
4923 tree len = fold_convert (size_type_node,
4924 se.string_length);
4925 elemsz = gfc_get_char_type (n->expr->ts.kind);
4926 elemsz = TYPE_SIZE_UNIT (elemsz);
4927 elemsz = fold_build2 (MULT_EXPR, size_type_node,
4928 len, elemsz);
4929 node4 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4930 OMP_CLAUSE_SET_MAP_KIND (node4, map_kind);
4931 OMP_CLAUSE_DECL (node4) = se.string_length;
4932 OMP_CLAUSE_SIZE (node4)
4933 = TYPE_SIZE_UNIT (gfc_charlen_type_node);
4934 }
4935 elemsz = fold_convert (gfc_array_index_type, elemsz);
4936 OMP_CLAUSE_SIZE (node)
4937 = fold_build2 (MULT_EXPR, gfc_array_index_type,
4938 OMP_CLAUSE_SIZE (node), elemsz);
4939 node2 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
4940 if (map_kind == GOMP_MAP_RELEASE
4941 || map_kind == GOMP_MAP_DELETE)
4942 {
4943 OMP_CLAUSE_SET_MAP_KIND (node2, map_kind);
4944 OMP_CLAUSE_RELEASE_DESCRIPTOR (node2) = 1;
4945 }
4946 else
4947 OMP_CLAUSE_SET_MAP_KIND (node2,
4948 GOMP_MAP_TO_PSET);
4949 OMP_CLAUSE_DECL (node2) = inner;
4950 OMP_CLAUSE_SIZE (node2) = TYPE_SIZE_UNIT (type);
4951 if (!openacc)
4952 {
4953 if (n->expr->ts.type == BT_DERIVED
4954 && n->expr->ts.u.derived->attr.alloc_comp)
4955 {
4956 /* Save array descriptor for use
4957 in gfc_omp_deep_mapping{,_p,_cnt}; force
4958 evaluate to ensure that it is
4959 not gimplified + is a decl. */
4960 tree tmp = OMP_CLAUSE_SIZE (node);
4961 tree var = gfc_create_var (TREE_TYPE (tmp),
4962 NULL);
4963 gfc_add_modify_loc (map_loc, block,
4964 var, tmp);
4965 OMP_CLAUSE_SIZE (node) = var;
4966 gfc_allocate_lang_decl (var);
4967 GFC_DECL_SAVED_DESCRIPTOR (var) = inner;
4968 }
4969
4970 gfc_omp_namelist *n2
4971 = clauses->lists[OMP_LIST_MAP];
4972
4973 /* If we don't have a mapping of a smaller part
4974 of the array -- or we can't prove that we do
4975 statically -- set this flag. If there is a
4976 mapping of a smaller part of the array after
4977 all, this will turn into a no-op at
4978 runtime. */
4979 OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (node) = 1;
4980
4981 bool sym_based;
4982 n2 = get_symbol_rooted_namelist (sym_rooted_nl,
4983 n, n2,
4984 &sym_based);
4985
4986 bool drop_mapping = false;
4987
4988 for (; n2 != NULL; n2 = n2->next)
4989 {
4990 if ((!sym_based && n == n2)
4991 || (sym_based && n == n2->u2.duplicate_of)
4992 || !n2->expr)
4993 continue;
4994
4995 if (!gfc_omp_expr_prefix_same (n->expr,
4996 n2->expr))
4997 continue;
4998
4999 gfc_ref *ref1 = n->expr->ref;
5000 gfc_ref *ref2 = n2->expr->ref;
5001
5002 /* We know ref1 and ref2 overlap. We're
5003 interested in whether ref2 describes a
5004 smaller part of the array than ref1, which
5005 we already know refers to the full
5006 array. */
5007
5008 while (ref1->next && ref2->next)
5009 {
5010 ref1 = ref1->next;
5011 ref2 = ref2->next;
5012 }
5013
5014 if (ref2->next
5015 || (ref2->type == REF_ARRAY
5016 && (ref2->u.ar.type == AR_ELEMENT
5017 || (ref2->u.ar.type
5018 == AR_SECTION))))
5019 {
5020 drop_mapping = true;
5021 break;
5022 }
5023 }
5024 if (drop_mapping)
5025 continue;
5026 }
5027 node3 = build_omp_clause (map_loc, OMP_CLAUSE_MAP);
5028 OMP_CLAUSE_SET_MAP_KIND (node3,
5029 GOMP_MAP_ATTACH_DETACH);
5030 OMP_CLAUSE_DECL (node3)
5031 = gfc_conv_descriptor_data_get (inner);
5032 /* Similar to gfc_trans_omp_array_section (details
5033 there), we add/keep the cast for OpenMP to prevent
5034 that an 'alloc:' gets added for node3 ('desc.data')
5035 as that is part of the whole descriptor (node3).
5036 TODO: Remove once the ME handles this properly. */
5037 if (!openacc)
5038 OMP_CLAUSE_DECL (node3)
5039 = fold_convert (TREE_TYPE (TREE_OPERAND(ptr, 0)),
5040 OMP_CLAUSE_DECL (node3));
5041 else
5042 STRIP_NOPS (OMP_CLAUSE_DECL (node3));
5043 OMP_CLAUSE_SIZE (node3) = size_int (0);
5044 }
5045 else
5046 OMP_CLAUSE_DECL (node) = inner;
5047 }
5048 else if (lastref->type == REF_ARRAY)
5049 {
5050 /* An array element or section. */
5051 bool element = lastref->u.ar.type == AR_ELEMENT;
5052 gomp_map_kind kind = GOMP_MAP_ATTACH_DETACH;
5053 gfc_trans_omp_array_section (block, op, n, inner, element,
5054 !openacc, kind, node, node2,
5055 node3, node4);
5056 }
5057 else
5058 gcc_unreachable ();
5059 }
5060 else
5061 sorry_at (gfc_get_location (&n->where), "unhandled expression");
5062
5063 finalize_map_clause:
5064
5065 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
5066 if (node2)
5067 omp_clauses = gfc_trans_add_clause (node2, omp_clauses);
5068 if (node3)
5069 omp_clauses = gfc_trans_add_clause (node3, omp_clauses);
5070 if (node4)
5071 omp_clauses = gfc_trans_add_clause (node4, omp_clauses);
5072 if (node5)
5073 omp_clauses = gfc_trans_add_clause (node5, omp_clauses);
5074 }
5075 break;
5076 case OMP_LIST_TO:
5077 case OMP_LIST_FROM:
5078 case OMP_LIST_CACHE:
5079 for (; n != NULL; n = n->next)
5080 {
5081 if (!n->sym->attr.referenced)
5082 continue;
5083
5084 switch (list)
5085 {
5086 case OMP_LIST_TO:
5087 clause_code = OMP_CLAUSE_TO;
5088 break;
5089 case OMP_LIST_FROM:
5090 clause_code = OMP_CLAUSE_FROM;
5091 break;
5092 case OMP_LIST_CACHE:
5093 clause_code = OMP_CLAUSE__CACHE_;
5094 break;
5095 default:
5096 gcc_unreachable ();
5097 }
5098 tree node = build_omp_clause (gfc_get_location (&n->where),
5099 clause_code);
5100 if (n->expr == NULL
5101 || (n->expr->ref->type == REF_ARRAY
5102 && n->expr->ref->u.ar.type == AR_FULL
5103 && n->expr->ref->next == NULL))
5104 {
5105 tree decl = gfc_trans_omp_variable (n->sym, false);
5106 if (gfc_omp_privatize_by_reference (decl))
5107 {
5108 if (gfc_omp_is_allocatable_or_ptr (decl))
5109 decl = build_fold_indirect_ref (decl);
5110 decl = build_fold_indirect_ref (decl);
5111 }
5112 else if (DECL_P (decl))
5113 TREE_ADDRESSABLE (decl) = 1;
5114 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
5115 {
5116 tree type = TREE_TYPE (decl);
5117 tree ptr = gfc_conv_descriptor_data_get (decl);
5118 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
5119 ptr = build_fold_indirect_ref (ptr);
5120 OMP_CLAUSE_DECL (node) = ptr;
5121 OMP_CLAUSE_SIZE (node)
5122 = gfc_full_array_size (block, decl,
5123 GFC_TYPE_ARRAY_RANK (type));
5124 tree elemsz
5125 = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5126 elemsz = fold_convert (gfc_array_index_type, elemsz);
5127 OMP_CLAUSE_SIZE (node)
5128 = fold_build2 (MULT_EXPR, gfc_array_index_type,
5129 OMP_CLAUSE_SIZE (node), elemsz);
5130 }
5131 else
5132 {
5133 OMP_CLAUSE_DECL (node) = decl;
5134 if (gfc_omp_is_allocatable_or_ptr (decl))
5135 OMP_CLAUSE_SIZE (node)
5136 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5137 }
5138 }
5139 else
5140 {
5141 tree ptr;
5142 gfc_init_se (&se, NULL);
5143 if (n->expr->rank == 0)
5144 {
5145 gfc_conv_expr_reference (&se, n->expr);
5146 ptr = se.expr;
5147 gfc_add_block_to_block (block, &se.pre);
5148 OMP_CLAUSE_SIZE (node)
5149 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ptr)));
5150 }
5151 else
5152 {
5153 gfc_conv_expr_descriptor (&se, n->expr);
5154 ptr = gfc_conv_array_data (se.expr);
5155 tree type = TREE_TYPE (se.expr);
5156 gfc_add_block_to_block (block, &se.pre);
5157 OMP_CLAUSE_SIZE (node)
5158 = gfc_full_array_size (block, se.expr,
5159 GFC_TYPE_ARRAY_RANK (type));
5160 tree elemsz
5161 = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5162 elemsz = fold_convert (gfc_array_index_type, elemsz);
5163 OMP_CLAUSE_SIZE (node)
5164 = fold_build2 (MULT_EXPR, gfc_array_index_type,
5165 OMP_CLAUSE_SIZE (node), elemsz);
5166 }
5167 gfc_add_block_to_block (block, &se.post);
5168 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
5169 OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (ptr);
5170 }
5171 if (n->u.present_modifier)
5172 OMP_CLAUSE_MOTION_PRESENT (node) = 1;
5173 if (list == OMP_LIST_CACHE && n->u.map.readonly)
5174 OMP_CLAUSE__CACHE__READONLY (node) = 1;
5175 omp_clauses = gfc_trans_add_clause (node, omp_clauses);
5176 }
5177 break;
5178 case OMP_LIST_USES_ALLOCATORS:
5179 /* Ignore pre-defined allocators as no special treatment is needed. */
5180 for (; n != NULL; n = n->next)
5181 if (n->sym->attr.flavor == FL_VARIABLE)
5182 break;
5183 if (n != NULL)
5184 sorry_at (input_location, "%<uses_allocators%> clause with traits "
5185 "and memory spaces");
5186 break;
5187 default:
5188 break;
5189 }
5190 }
5191
5192 /* Free hashmap if we built it. */
5193 if (sym_rooted_nl)
5194 {
5195 typedef hash_map<gfc_symbol *, gfc_omp_namelist *>::iterator hti;
5196 for (hti it = sym_rooted_nl->begin (); it != sym_rooted_nl->end (); ++it)
5197 {
5198 gfc_omp_namelist *&nl = (*it).second;
5199 while (nl)
5200 {
5201 gfc_omp_namelist *next = nl->next;
5202 free (nl);
5203 nl = next;
5204 }
5205 }
5206 delete sym_rooted_nl;
5207 }
5208
5209 if (clauses->if_expr)
5210 {
5211 tree if_var;
5212
5213 gfc_init_se (&se, NULL);
5214 gfc_conv_expr (&se, clauses->if_expr);
5215 gfc_add_block_to_block (block, &se.pre);
5216 if_var = gfc_evaluate_now (se.expr, block);
5217 gfc_add_block_to_block (block, &se.post);
5218
5219 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF);
5220 OMP_CLAUSE_IF_MODIFIER (c) = ERROR_MARK;
5221 OMP_CLAUSE_IF_EXPR (c) = if_var;
5222 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5223 }
5224
5225 for (ifc = 0; ifc < OMP_IF_LAST; ifc++)
5226 if (clauses->if_exprs[ifc])
5227 {
5228 tree if_var;
5229
5230 gfc_init_se (&se, NULL);
5231 gfc_conv_expr (&se, clauses->if_exprs[ifc]);
5232 gfc_add_block_to_block (block, &se.pre);
5233 if_var = gfc_evaluate_now (se.expr, block);
5234 gfc_add_block_to_block (block, &se.post);
5235
5236 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF);
5237 switch (ifc)
5238 {
5239 case OMP_IF_CANCEL:
5240 OMP_CLAUSE_IF_MODIFIER (c) = VOID_CST;
5241 break;
5242 case OMP_IF_PARALLEL:
5243 OMP_CLAUSE_IF_MODIFIER (c) = OMP_PARALLEL;
5244 break;
5245 case OMP_IF_SIMD:
5246 OMP_CLAUSE_IF_MODIFIER (c) = OMP_SIMD;
5247 break;
5248 case OMP_IF_TASK:
5249 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TASK;
5250 break;
5251 case OMP_IF_TASKLOOP:
5252 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TASKLOOP;
5253 break;
5254 case OMP_IF_TARGET:
5255 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET;
5256 break;
5257 case OMP_IF_TARGET_DATA:
5258 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_DATA;
5259 break;
5260 case OMP_IF_TARGET_UPDATE:
5261 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_UPDATE;
5262 break;
5263 case OMP_IF_TARGET_ENTER_DATA:
5264 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_ENTER_DATA;
5265 break;
5266 case OMP_IF_TARGET_EXIT_DATA:
5267 OMP_CLAUSE_IF_MODIFIER (c) = OMP_TARGET_EXIT_DATA;
5268 break;
5269 default:
5270 gcc_unreachable ();
5271 }
5272 OMP_CLAUSE_IF_EXPR (c) = if_var;
5273 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5274 }
5275
5276 if (clauses->self_expr)
5277 {
5278 tree self_var;
5279
5280 gfc_init_se (&se, NULL);
5281 gfc_conv_expr (&se, clauses->self_expr);
5282 gfc_add_block_to_block (block, &se.pre);
5283 self_var = gfc_evaluate_now (se.expr, block);
5284 gfc_add_block_to_block (block, &se.post);
5285
5286 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SELF);
5287 OMP_CLAUSE_SELF_EXPR (c) = self_var;
5288 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5289 }
5290
5291 if (clauses->final_expr)
5292 {
5293 tree final_var;
5294
5295 gfc_init_se (&se, NULL);
5296 gfc_conv_expr (&se, clauses->final_expr);
5297 gfc_add_block_to_block (block, &se.pre);
5298 final_var = gfc_evaluate_now (se.expr, block);
5299 gfc_add_block_to_block (block, &se.post);
5300
5301 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FINAL);
5302 OMP_CLAUSE_FINAL_EXPR (c) = final_var;
5303 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5304 }
5305
5306 if (clauses->novariants)
5307 {
5308 tree novariants_var;
5309
5310 gfc_init_se (&se, NULL);
5311 gfc_conv_expr (&se, clauses->novariants);
5312 gfc_add_block_to_block (block, &se.pre);
5313 novariants_var = gfc_evaluate_now (se.expr, block);
5314 gfc_add_block_to_block (block, &se.post);
5315
5316 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOVARIANTS);
5317 OMP_CLAUSE_NOVARIANTS_EXPR (c) = novariants_var;
5318 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5319 }
5320
5321 if (clauses->nocontext)
5322 {
5323 tree nocontext_var;
5324
5325 gfc_init_se (&se, NULL);
5326 gfc_conv_expr (&se, clauses->nocontext);
5327 gfc_add_block_to_block (block, &se.pre);
5328 nocontext_var = gfc_evaluate_now (se.expr, block);
5329 gfc_add_block_to_block (block, &se.post);
5330
5331 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOCONTEXT);
5332 OMP_CLAUSE_NOCONTEXT_EXPR (c) = nocontext_var;
5333 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5334 }
5335
5336 if (clauses->num_threads)
5337 {
5338 tree num_threads;
5339
5340 gfc_init_se (&se, NULL);
5341 gfc_conv_expr (&se, clauses->num_threads);
5342 gfc_add_block_to_block (block, &se.pre);
5343 num_threads = gfc_evaluate_now (se.expr, block);
5344 gfc_add_block_to_block (block, &se.post);
5345
5346 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_THREADS);
5347 OMP_CLAUSE_NUM_THREADS_EXPR (c) = num_threads;
5348 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5349 }
5350
5351 chunk_size = NULL_TREE;
5352 if (clauses->chunk_size)
5353 {
5354 gfc_init_se (&se, NULL);
5355 gfc_conv_expr (&se, clauses->chunk_size);
5356 gfc_add_block_to_block (block, &se.pre);
5357 chunk_size = gfc_evaluate_now (se.expr, block);
5358 gfc_add_block_to_block (block, &se.post);
5359 }
5360
5361 if (clauses->sched_kind != OMP_SCHED_NONE)
5362 {
5363 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SCHEDULE);
5364 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = chunk_size;
5365 switch (clauses->sched_kind)
5366 {
5367 case OMP_SCHED_STATIC:
5368 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
5369 break;
5370 case OMP_SCHED_DYNAMIC:
5371 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
5372 break;
5373 case OMP_SCHED_GUIDED:
5374 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
5375 break;
5376 case OMP_SCHED_RUNTIME:
5377 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
5378 break;
5379 case OMP_SCHED_AUTO:
5380 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
5381 break;
5382 default:
5383 gcc_unreachable ();
5384 }
5385 if (clauses->sched_monotonic)
5386 OMP_CLAUSE_SCHEDULE_KIND (c)
5387 = (omp_clause_schedule_kind) (OMP_CLAUSE_SCHEDULE_KIND (c)
5388 | OMP_CLAUSE_SCHEDULE_MONOTONIC);
5389 else if (clauses->sched_nonmonotonic)
5390 OMP_CLAUSE_SCHEDULE_KIND (c)
5391 = (omp_clause_schedule_kind) (OMP_CLAUSE_SCHEDULE_KIND (c)
5392 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
5393 if (clauses->sched_simd)
5394 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
5395 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5396 }
5397
5398 if (clauses->default_sharing != OMP_DEFAULT_UNKNOWN)
5399 {
5400 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEFAULT);
5401 switch (clauses->default_sharing)
5402 {
5403 case OMP_DEFAULT_NONE:
5404 OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_NONE;
5405 break;
5406 case OMP_DEFAULT_SHARED:
5407 OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_SHARED;
5408 break;
5409 case OMP_DEFAULT_PRIVATE:
5410 OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_PRIVATE;
5411 break;
5412 case OMP_DEFAULT_FIRSTPRIVATE:
5413 OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
5414 break;
5415 case OMP_DEFAULT_PRESENT:
5416 OMP_CLAUSE_DEFAULT_KIND (c) = OMP_CLAUSE_DEFAULT_PRESENT;
5417 break;
5418 default:
5419 gcc_unreachable ();
5420 }
5421 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5422 }
5423
5424 if (clauses->nowait)
5425 {
5426 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOWAIT);
5427 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5428 }
5429
5430 if (clauses->full)
5431 {
5432 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FULL);
5433 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5434 }
5435
5436 if (clauses->partial)
5437 {
5438 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PARTIAL);
5439 OMP_CLAUSE_PARTIAL_EXPR (c)
5440 = (clauses->partial > 0
5441 ? build_int_cst (integer_type_node, clauses->partial)
5442 : NULL_TREE);
5443 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5444 }
5445
5446 if (clauses->sizes_list)
5447 {
5448 tree list = NULL_TREE;
5449 for (gfc_expr_list *el = clauses->sizes_list; el; el = el->next)
5450 list = tree_cons (NULL_TREE, gfc_convert_expr_to_tree (block, el->expr),
5451 list);
5452
5453 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIZES);
5454 OMP_CLAUSE_SIZES_LIST (c) = nreverse (list);
5455 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5456 }
5457
5458 if (clauses->ordered)
5459 {
5460 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ORDERED);
5461 OMP_CLAUSE_ORDERED_EXPR (c)
5462 = clauses->orderedc ? build_int_cst (integer_type_node,
5463 clauses->orderedc) : NULL_TREE;
5464 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5465 }
5466
5467 if (clauses->order_concurrent)
5468 {
5469 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ORDER);
5470 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = clauses->order_unconstrained;
5471 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = clauses->order_reproducible;
5472 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5473 }
5474
5475 if (clauses->untied)
5476 {
5477 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_UNTIED);
5478 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5479 }
5480
5481 if (clauses->mergeable)
5482 {
5483 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_MERGEABLE);
5484 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5485 }
5486
5487 if (clauses->collapse)
5488 {
5489 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_COLLAPSE);
5490 OMP_CLAUSE_COLLAPSE_EXPR (c)
5491 = build_int_cst (integer_type_node, clauses->collapse);
5492 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5493 }
5494
5495 if (clauses->inbranch)
5496 {
5497 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_INBRANCH);
5498 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5499 }
5500
5501 if (clauses->notinbranch)
5502 {
5503 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOTINBRANCH);
5504 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5505 }
5506
5507 switch (clauses->cancel)
5508 {
5509 case OMP_CANCEL_UNKNOWN:
5510 break;
5511 case OMP_CANCEL_PARALLEL:
5512 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PARALLEL);
5513 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5514 break;
5515 case OMP_CANCEL_SECTIONS:
5516 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SECTIONS);
5517 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5518 break;
5519 case OMP_CANCEL_DO:
5520 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FOR);
5521 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5522 break;
5523 case OMP_CANCEL_TASKGROUP:
5524 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_TASKGROUP);
5525 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5526 break;
5527 }
5528
5529 if (clauses->proc_bind != OMP_PROC_BIND_UNKNOWN)
5530 {
5531 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PROC_BIND);
5532 switch (clauses->proc_bind)
5533 {
5534 case OMP_PROC_BIND_PRIMARY:
5535 OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_PRIMARY;
5536 break;
5537 case OMP_PROC_BIND_MASTER:
5538 OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_MASTER;
5539 break;
5540 case OMP_PROC_BIND_SPREAD:
5541 OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_SPREAD;
5542 break;
5543 case OMP_PROC_BIND_CLOSE:
5544 OMP_CLAUSE_PROC_BIND_KIND (c) = OMP_CLAUSE_PROC_BIND_CLOSE;
5545 break;
5546 default:
5547 gcc_unreachable ();
5548 }
5549 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5550 }
5551
5552 if (clauses->safelen_expr)
5553 {
5554 tree safelen_var;
5555
5556 gfc_init_se (&se, NULL);
5557 gfc_conv_expr (&se, clauses->safelen_expr);
5558 gfc_add_block_to_block (block, &se.pre);
5559 safelen_var = gfc_evaluate_now (se.expr, block);
5560 gfc_add_block_to_block (block, &se.post);
5561
5562 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SAFELEN);
5563 OMP_CLAUSE_SAFELEN_EXPR (c) = safelen_var;
5564 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5565 }
5566
5567 if (clauses->simdlen_expr)
5568 {
5569 if (declare_simd)
5570 {
5571 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIMDLEN);
5572 OMP_CLAUSE_SIMDLEN_EXPR (c)
5573 = gfc_conv_constant_to_tree (clauses->simdlen_expr);
5574 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5575 }
5576 else
5577 {
5578 tree simdlen_var;
5579
5580 gfc_init_se (&se, NULL);
5581 gfc_conv_expr (&se, clauses->simdlen_expr);
5582 gfc_add_block_to_block (block, &se.pre);
5583 simdlen_var = gfc_evaluate_now (se.expr, block);
5584 gfc_add_block_to_block (block, &se.post);
5585
5586 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIMDLEN);
5587 OMP_CLAUSE_SIMDLEN_EXPR (c) = simdlen_var;
5588 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5589 }
5590 }
5591
5592 if (clauses->num_teams_upper)
5593 {
5594 tree num_teams_lower = NULL_TREE, num_teams_upper;
5595
5596 gfc_init_se (&se, NULL);
5597 gfc_conv_expr (&se, clauses->num_teams_upper);
5598 gfc_add_block_to_block (block, &se.pre);
5599 num_teams_upper = gfc_evaluate_now (se.expr, block);
5600 gfc_add_block_to_block (block, &se.post);
5601
5602 if (clauses->num_teams_lower)
5603 {
5604 gfc_init_se (&se, NULL);
5605 gfc_conv_expr (&se, clauses->num_teams_lower);
5606 gfc_add_block_to_block (block, &se.pre);
5607 num_teams_lower = gfc_evaluate_now (se.expr, block);
5608 gfc_add_block_to_block (block, &se.post);
5609 }
5610 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_TEAMS);
5611 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
5612 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
5613 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5614 }
5615
5616 if (clauses->device)
5617 {
5618 tree device;
5619
5620 gfc_init_se (&se, NULL);
5621 gfc_conv_expr (&se, clauses->device);
5622 gfc_add_block_to_block (block, &se.pre);
5623 device = gfc_evaluate_now (se.expr, block);
5624 gfc_add_block_to_block (block, &se.post);
5625
5626 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEVICE);
5627 OMP_CLAUSE_DEVICE_ID (c) = device;
5628
5629 if (clauses->ancestor)
5630 OMP_CLAUSE_DEVICE_ANCESTOR (c) = 1;
5631
5632 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5633 }
5634
5635 if (clauses->thread_limit)
5636 {
5637 tree thread_limit;
5638
5639 gfc_init_se (&se, NULL);
5640 gfc_conv_expr (&se, clauses->thread_limit);
5641 gfc_add_block_to_block (block, &se.pre);
5642 thread_limit = gfc_evaluate_now (se.expr, block);
5643 gfc_add_block_to_block (block, &se.post);
5644
5645 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_THREAD_LIMIT);
5646 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
5647 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5648 }
5649
5650 chunk_size = NULL_TREE;
5651 if (clauses->dist_chunk_size)
5652 {
5653 gfc_init_se (&se, NULL);
5654 gfc_conv_expr (&se, clauses->dist_chunk_size);
5655 gfc_add_block_to_block (block, &se.pre);
5656 chunk_size = gfc_evaluate_now (se.expr, block);
5657 gfc_add_block_to_block (block, &se.post);
5658 }
5659
5660 if (clauses->dist_sched_kind != OMP_SCHED_NONE)
5661 {
5662 c = build_omp_clause (gfc_get_location (&where),
5663 OMP_CLAUSE_DIST_SCHEDULE);
5664 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = chunk_size;
5665 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5666 }
5667
5668 if (clauses->grainsize)
5669 {
5670 tree grainsize;
5671
5672 gfc_init_se (&se, NULL);
5673 gfc_conv_expr (&se, clauses->grainsize);
5674 gfc_add_block_to_block (block, &se.pre);
5675 grainsize = gfc_evaluate_now (se.expr, block);
5676 gfc_add_block_to_block (block, &se.post);
5677
5678 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_GRAINSIZE);
5679 OMP_CLAUSE_GRAINSIZE_EXPR (c) = grainsize;
5680 if (clauses->grainsize_strict)
5681 OMP_CLAUSE_GRAINSIZE_STRICT (c) = 1;
5682 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5683 }
5684
5685 if (clauses->num_tasks)
5686 {
5687 tree num_tasks;
5688
5689 gfc_init_se (&se, NULL);
5690 gfc_conv_expr (&se, clauses->num_tasks);
5691 gfc_add_block_to_block (block, &se.pre);
5692 num_tasks = gfc_evaluate_now (se.expr, block);
5693 gfc_add_block_to_block (block, &se.post);
5694
5695 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_TASKS);
5696 OMP_CLAUSE_NUM_TASKS_EXPR (c) = num_tasks;
5697 if (clauses->num_tasks_strict)
5698 OMP_CLAUSE_NUM_TASKS_STRICT (c) = 1;
5699 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5700 }
5701
5702 if (clauses->priority)
5703 {
5704 tree priority;
5705
5706 gfc_init_se (&se, NULL);
5707 gfc_conv_expr (&se, clauses->priority);
5708 gfc_add_block_to_block (block, &se.pre);
5709 priority = gfc_evaluate_now (se.expr, block);
5710 gfc_add_block_to_block (block, &se.post);
5711
5712 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_PRIORITY);
5713 OMP_CLAUSE_PRIORITY_EXPR (c) = priority;
5714 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5715 }
5716
5717 if (clauses->detach)
5718 {
5719 tree detach;
5720
5721 gfc_init_se (&se, NULL);
5722 gfc_conv_expr (&se, clauses->detach);
5723 gfc_add_block_to_block (block, &se.pre);
5724 detach = se.expr;
5725 gfc_add_block_to_block (block, &se.post);
5726
5727 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DETACH);
5728 TREE_ADDRESSABLE (detach) = 1;
5729 OMP_CLAUSE_DECL (c) = detach;
5730 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5731 }
5732
5733 if (clauses->filter)
5734 {
5735 tree filter;
5736
5737 gfc_init_se (&se, NULL);
5738 gfc_conv_expr (&se, clauses->filter);
5739 gfc_add_block_to_block (block, &se.pre);
5740 filter = gfc_evaluate_now (se.expr, block);
5741 gfc_add_block_to_block (block, &se.post);
5742
5743 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FILTER);
5744 OMP_CLAUSE_FILTER_EXPR (c) = filter;
5745 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5746 }
5747
5748 if (clauses->hint)
5749 {
5750 tree hint;
5751
5752 gfc_init_se (&se, NULL);
5753 gfc_conv_expr (&se, clauses->hint);
5754 gfc_add_block_to_block (block, &se.pre);
5755 hint = gfc_evaluate_now (se.expr, block);
5756 gfc_add_block_to_block (block, &se.post);
5757
5758 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_HINT);
5759 OMP_CLAUSE_HINT_EXPR (c) = hint;
5760 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5761 }
5762
5763 if (clauses->simd)
5764 {
5765 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SIMD);
5766 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5767 }
5768 if (clauses->threads)
5769 {
5770 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_THREADS);
5771 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5772 }
5773 if (clauses->nogroup)
5774 {
5775 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NOGROUP);
5776 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5777 }
5778
5779 for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; i++)
5780 {
5781 if (clauses->defaultmap[i] == OMP_DEFAULTMAP_UNSET)
5782 continue;
5783 enum omp_clause_defaultmap_kind behavior, category;
5784 switch ((gfc_omp_defaultmap_category) i)
5785 {
5786 case OMP_DEFAULTMAP_CAT_UNCATEGORIZED:
5787 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
5788 break;
5789 case OMP_DEFAULTMAP_CAT_ALL:
5790 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
5791 break;
5792 case OMP_DEFAULTMAP_CAT_SCALAR:
5793 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
5794 break;
5795 case OMP_DEFAULTMAP_CAT_AGGREGATE:
5796 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
5797 break;
5798 case OMP_DEFAULTMAP_CAT_ALLOCATABLE:
5799 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE;
5800 break;
5801 case OMP_DEFAULTMAP_CAT_POINTER:
5802 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
5803 break;
5804 default: gcc_unreachable ();
5805 }
5806 switch (clauses->defaultmap[i])
5807 {
5808 case OMP_DEFAULTMAP_ALLOC:
5809 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
5810 break;
5811 case OMP_DEFAULTMAP_TO: behavior = OMP_CLAUSE_DEFAULTMAP_TO; break;
5812 case OMP_DEFAULTMAP_FROM: behavior = OMP_CLAUSE_DEFAULTMAP_FROM; break;
5813 case OMP_DEFAULTMAP_TOFROM:
5814 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
5815 break;
5816 case OMP_DEFAULTMAP_FIRSTPRIVATE:
5817 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
5818 break;
5819 case OMP_DEFAULTMAP_PRESENT:
5820 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
5821 break;
5822 case OMP_DEFAULTMAP_NONE: behavior = OMP_CLAUSE_DEFAULTMAP_NONE; break;
5823 case OMP_DEFAULTMAP_DEFAULT:
5824 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
5825 break;
5826 default: gcc_unreachable ();
5827 }
5828 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DEFAULTMAP);
5829 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
5830 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5831 }
5832
5833 if (clauses->doacross_source)
5834 {
5835 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_DOACROSS);
5836 OMP_CLAUSE_DOACROSS_KIND (c) = OMP_CLAUSE_DOACROSS_SOURCE;
5837 OMP_CLAUSE_DOACROSS_DEPEND (c) = clauses->depend_source;
5838 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5839 }
5840
5841 if (clauses->async)
5842 {
5843 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ASYNC);
5844 if (clauses->async_expr)
5845 OMP_CLAUSE_ASYNC_EXPR (c)
5846 = gfc_convert_expr_to_tree (block, clauses->async_expr);
5847 else
5848 OMP_CLAUSE_ASYNC_EXPR (c) = NULL;
5849 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5850 }
5851 if (clauses->seq)
5852 {
5853 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_SEQ);
5854 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5855 }
5856 if (clauses->par_auto)
5857 {
5858 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_AUTO);
5859 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5860 }
5861 if (clauses->if_present)
5862 {
5863 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_IF_PRESENT);
5864 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5865 }
5866 if (clauses->finalize)
5867 {
5868 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_FINALIZE);
5869 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5870 }
5871 if (clauses->independent)
5872 {
5873 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_INDEPENDENT);
5874 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5875 }
5876 if (clauses->wait_list)
5877 {
5878 gfc_expr_list *el;
5879
5880 for (el = clauses->wait_list; el; el = el->next)
5881 {
5882 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_WAIT);
5883 OMP_CLAUSE_DECL (c) = gfc_convert_expr_to_tree (block, el->expr);
5884 OMP_CLAUSE_CHAIN (c) = omp_clauses;
5885 omp_clauses = c;
5886 }
5887 }
5888 if (clauses->num_gangs_expr)
5889 {
5890 tree num_gangs_var
5891 = gfc_convert_expr_to_tree (block, clauses->num_gangs_expr);
5892 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_GANGS);
5893 OMP_CLAUSE_NUM_GANGS_EXPR (c) = num_gangs_var;
5894 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5895 }
5896 if (clauses->num_workers_expr)
5897 {
5898 tree num_workers_var
5899 = gfc_convert_expr_to_tree (block, clauses->num_workers_expr);
5900 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_NUM_WORKERS);
5901 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = num_workers_var;
5902 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5903 }
5904 if (clauses->vector_length_expr)
5905 {
5906 tree vector_length_var
5907 = gfc_convert_expr_to_tree (block, clauses->vector_length_expr);
5908 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_VECTOR_LENGTH);
5909 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = vector_length_var;
5910 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5911 }
5912 if (clauses->tile_list)
5913 {
5914 tree list = NULL_TREE;
5915 for (gfc_expr_list *el = clauses->tile_list; el; el = el->next)
5916 list = tree_cons (NULL_TREE, gfc_convert_expr_to_tree (block, el->expr),
5917 list);
5918
5919 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_TILE);
5920 OMP_CLAUSE_TILE_LIST (c) = nreverse (list);
5921 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5922 }
5923 if (clauses->vector)
5924 {
5925 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_VECTOR);
5926 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5927
5928 if (clauses->vector_expr)
5929 {
5930 tree vector_var
5931 = gfc_convert_expr_to_tree (block, clauses->vector_expr);
5932 OMP_CLAUSE_VECTOR_EXPR (c) = vector_var;
5933
5934 /* TODO: We're not capturing location information for individual
5935 clauses. However, if we have an expression attached to the
5936 clause, that one provides better location information. */
5937 OMP_CLAUSE_LOCATION (c)
5938 = gfc_get_location (&clauses->vector_expr->where);
5939 }
5940 }
5941 if (clauses->worker)
5942 {
5943 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_WORKER);
5944 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5945
5946 if (clauses->worker_expr)
5947 {
5948 tree worker_var
5949 = gfc_convert_expr_to_tree (block, clauses->worker_expr);
5950 OMP_CLAUSE_WORKER_EXPR (c) = worker_var;
5951
5952 /* TODO: We're not capturing location information for individual
5953 clauses. However, if we have an expression attached to the
5954 clause, that one provides better location information. */
5955 OMP_CLAUSE_LOCATION (c)
5956 = gfc_get_location (&clauses->worker_expr->where);
5957 }
5958 }
5959 if (clauses->gang)
5960 {
5961 tree arg;
5962 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_GANG);
5963 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5964
5965 if (clauses->gang_num_expr)
5966 {
5967 arg = gfc_convert_expr_to_tree (block, clauses->gang_num_expr);
5968 OMP_CLAUSE_GANG_EXPR (c) = arg;
5969
5970 /* TODO: We're not capturing location information for individual
5971 clauses. However, if we have an expression attached to the
5972 clause, that one provides better location information. */
5973 OMP_CLAUSE_LOCATION (c)
5974 = gfc_get_location (&clauses->gang_num_expr->where);
5975 }
5976
5977 if (clauses->gang_static)
5978 {
5979 arg = clauses->gang_static_expr
5980 ? gfc_convert_expr_to_tree (block, clauses->gang_static_expr)
5981 : integer_minus_one_node;
5982 OMP_CLAUSE_GANG_STATIC_EXPR (c) = arg;
5983 }
5984 }
5985 if (clauses->bind != OMP_BIND_UNSET)
5986 {
5987 c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_BIND);
5988 omp_clauses = gfc_trans_add_clause (c, omp_clauses);
5989 switch (clauses->bind)
5990 {
5991 case OMP_BIND_TEAMS:
5992 OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_TEAMS;
5993 break;
5994 case OMP_BIND_PARALLEL:
5995 OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_PARALLEL;
5996 break;
5997 case OMP_BIND_THREAD:
5998 OMP_CLAUSE_BIND_KIND (c) = OMP_CLAUSE_BIND_THREAD;
5999 break;
6000 default:
6001 gcc_unreachable ();
6002 }
6003 }
6004 /* OpenACC 'nohost' clauses cannot appear here. */
6005 gcc_checking_assert (!clauses->nohost);
6006
6007 return nreverse (omp_clauses);
6008 }
6009
6010 /* Like gfc_trans_code, but force creation of a BIND_EXPR around it. */
6011
6012 static tree
6013 gfc_trans_omp_code (gfc_code *code, bool force_empty)
6014 {
6015 tree stmt;
6016
6017 pushlevel ();
6018 stmt = gfc_trans_code (code);
6019 if (TREE_CODE (stmt) != BIND_EXPR)
6020 {
6021 if (!IS_EMPTY_STMT (stmt) || force_empty)
6022 {
6023 tree block = poplevel (1, 0);
6024 stmt = build3_v (BIND_EXPR, NULL, stmt, block);
6025 }
6026 else
6027 poplevel (0, 0);
6028 }
6029 else
6030 poplevel (0, 0);
6031 return stmt;
6032 }
6033
6034 /* Translate OpenACC 'parallel', 'kernels', 'serial', 'data', 'host_data'
6035 construct. */
6036
6037 static tree
6038 gfc_trans_oacc_construct (gfc_code *code)
6039 {
6040 stmtblock_t block;
6041 tree stmt, oacc_clauses;
6042 enum tree_code construct_code;
6043
6044 switch (code->op)
6045 {
6046 case EXEC_OACC_PARALLEL:
6047 construct_code = OACC_PARALLEL;
6048 break;
6049 case EXEC_OACC_KERNELS:
6050 construct_code = OACC_KERNELS;
6051 break;
6052 case EXEC_OACC_SERIAL:
6053 construct_code = OACC_SERIAL;
6054 break;
6055 case EXEC_OACC_DATA:
6056 construct_code = OACC_DATA;
6057 break;
6058 case EXEC_OACC_HOST_DATA:
6059 construct_code = OACC_HOST_DATA;
6060 break;
6061 default:
6062 gcc_unreachable ();
6063 }
6064
6065 gfc_start_block (&block);
6066 oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
6067 code->loc, false, true);
6068 pushlevel ();
6069 stmt = gfc_trans_omp_code (code->block->next, true);
6070 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
6071 stmt = build2_loc (gfc_get_location (&code->loc), construct_code,
6072 void_type_node, stmt, oacc_clauses);
6073 gfc_add_expr_to_block (&block, stmt);
6074 return gfc_finish_block (&block);
6075 }
6076
6077 /* update, enter_data, exit_data, cache. */
6078 static tree
6079 gfc_trans_oacc_executable_directive (gfc_code *code)
6080 {
6081 stmtblock_t block;
6082 tree stmt, oacc_clauses;
6083 enum tree_code construct_code;
6084
6085 switch (code->op)
6086 {
6087 case EXEC_OACC_UPDATE:
6088 construct_code = OACC_UPDATE;
6089 break;
6090 case EXEC_OACC_ENTER_DATA:
6091 construct_code = OACC_ENTER_DATA;
6092 break;
6093 case EXEC_OACC_EXIT_DATA:
6094 construct_code = OACC_EXIT_DATA;
6095 break;
6096 case EXEC_OACC_CACHE:
6097 construct_code = OACC_CACHE;
6098 break;
6099 default:
6100 gcc_unreachable ();
6101 }
6102
6103 gfc_start_block (&block);
6104 oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
6105 code->loc, false, true, code->op);
6106 stmt = build1_loc (input_location, construct_code, void_type_node,
6107 oacc_clauses);
6108 gfc_add_expr_to_block (&block, stmt);
6109 return gfc_finish_block (&block);
6110 }
6111
6112 static tree
6113 gfc_trans_oacc_wait_directive (gfc_code *code)
6114 {
6115 stmtblock_t block;
6116 tree stmt, t;
6117 vec<tree, va_gc> *args;
6118 int nparms = 0;
6119 gfc_expr_list *el;
6120 gfc_omp_clauses *clauses = code->ext.omp_clauses;
6121 location_t loc = input_location;
6122
6123 for (el = clauses->wait_list; el; el = el->next)
6124 nparms++;
6125
6126 vec_alloc (args, nparms + 2);
6127 stmt = builtin_decl_explicit (BUILT_IN_GOACC_WAIT);
6128
6129 gfc_start_block (&block);
6130
6131 if (clauses->async_expr)
6132 t = gfc_convert_expr_to_tree (&block, clauses->async_expr);
6133 else
6134 t = build_int_cst (integer_type_node, -2);
6135
6136 args->quick_push (t);
6137 args->quick_push (build_int_cst (integer_type_node, nparms));
6138
6139 for (el = clauses->wait_list; el; el = el->next)
6140 args->quick_push (gfc_convert_expr_to_tree (&block, el->expr));
6141
6142 stmt = build_call_expr_loc_vec (loc, stmt, args);
6143 gfc_add_expr_to_block (&block, stmt);
6144
6145 vec_free (args);
6146
6147 return gfc_finish_block (&block);
6148 }
6149
6150 static tree gfc_trans_omp_sections (gfc_code *, gfc_omp_clauses *);
6151 static tree gfc_trans_omp_workshare (gfc_code *, gfc_omp_clauses *);
6152
6153 static tree
6154 gfc_trans_omp_allocators (gfc_code *code)
6155 {
6156 static bool warned = false;
6157 gfc_omp_namelist *omp_allocate
6158 = code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
6159 if (!flag_openmp_allocators && !warned)
6160 {
6161 omp_allocate = NULL;
6162 gfc_error ("%<!$OMP %s%> at %L requires %<-fopenmp-allocators%>",
6163 code->op == EXEC_OMP_ALLOCATE ? "ALLOCATE" : "ALLOCATORS",
6164 &code->loc);
6165 warning (0, "All files that might deallocate such a variable must be "
6166 "compiled with %<-fopenmp-allocators%>");
6167 inform (UNKNOWN_LOCATION,
6168 "This includes explicit DEALLOCATE, reallocation on intrinsic "
6169 "assignment, INTENT(OUT) for allocatable dummy arguments, and "
6170 "reallocation of allocatable components allocated with an "
6171 "OpenMP allocator");
6172 warned = true;
6173 }
6174 return gfc_trans_allocate (code->block->next, omp_allocate);
6175 }
6176
6177 static tree
6178 gfc_trans_omp_assume (gfc_code *code)
6179 {
6180 stmtblock_t block;
6181 gfc_init_block (&block);
6182 gfc_omp_assumptions *assume = code->ext.omp_clauses->assume;
6183 if (assume)
6184 for (gfc_expr_list *el = assume->holds; el; el = el->next)
6185 {
6186 location_t loc = gfc_get_location (&el->expr->where);
6187 gfc_se se;
6188 gfc_init_se (&se, NULL);
6189 gfc_conv_expr (&se, el->expr);
6190 tree t;
6191 if (se.pre.head == NULL_TREE && se.post.head == NULL_TREE)
6192 t = se.expr;
6193 else
6194 {
6195 tree var = create_tmp_var_raw (boolean_type_node);
6196 DECL_CONTEXT (var) = current_function_decl;
6197 stmtblock_t block2;
6198 gfc_init_block (&block2);
6199 gfc_add_block_to_block (&block2, &se.pre);
6200 gfc_add_modify_loc (loc, &block2, var,
6201 fold_convert_loc (loc, boolean_type_node,
6202 se.expr));
6203 gfc_add_block_to_block (&block2, &se.post);
6204 t = gfc_finish_block (&block2);
6205 t = build4 (TARGET_EXPR, boolean_type_node, var, t, NULL, NULL);
6206 }
6207 t = build_call_expr_internal_loc (loc, IFN_ASSUME,
6208 void_type_node, 1, t);
6209 gfc_add_expr_to_block (&block, t);
6210 }
6211 gfc_add_expr_to_block (&block, gfc_trans_omp_code (code->block->next, true));
6212 return gfc_finish_block (&block);
6213 }
6214
6215 static tree
6216 gfc_trans_omp_atomic (gfc_code *code)
6217 {
6218 gfc_code *atomic_code = code->block;
6219 gfc_se lse;
6220 gfc_se rse;
6221 gfc_se vse;
6222 gfc_expr *expr1, *expr2, *e, *capture_expr1 = NULL, *capture_expr2 = NULL;
6223 gfc_symbol *var;
6224 stmtblock_t block;
6225 tree lhsaddr, type, rhs, x, compare = NULL_TREE, comp_tgt = NULL_TREE;
6226 enum tree_code op = ERROR_MARK;
6227 enum tree_code aop = OMP_ATOMIC;
6228 bool var_on_left = false, else_branch = false;
6229 enum omp_memory_order mo, fail_mo;
6230 switch (atomic_code->ext.omp_clauses->memorder)
6231 {
6232 case OMP_MEMORDER_UNSET: mo = OMP_MEMORY_ORDER_UNSPECIFIED; break;
6233 case OMP_MEMORDER_ACQ_REL: mo = OMP_MEMORY_ORDER_ACQ_REL; break;
6234 case OMP_MEMORDER_ACQUIRE: mo = OMP_MEMORY_ORDER_ACQUIRE; break;
6235 case OMP_MEMORDER_RELAXED: mo = OMP_MEMORY_ORDER_RELAXED; break;
6236 case OMP_MEMORDER_RELEASE: mo = OMP_MEMORY_ORDER_RELEASE; break;
6237 case OMP_MEMORDER_SEQ_CST: mo = OMP_MEMORY_ORDER_SEQ_CST; break;
6238 default: gcc_unreachable ();
6239 }
6240 switch (atomic_code->ext.omp_clauses->fail)
6241 {
6242 case OMP_MEMORDER_UNSET: fail_mo = OMP_FAIL_MEMORY_ORDER_UNSPECIFIED; break;
6243 case OMP_MEMORDER_ACQUIRE: fail_mo = OMP_FAIL_MEMORY_ORDER_ACQUIRE; break;
6244 case OMP_MEMORDER_RELAXED: fail_mo = OMP_FAIL_MEMORY_ORDER_RELAXED; break;
6245 case OMP_MEMORDER_SEQ_CST: fail_mo = OMP_FAIL_MEMORY_ORDER_SEQ_CST; break;
6246 default: gcc_unreachable ();
6247 }
6248 mo = (omp_memory_order) (mo | fail_mo);
6249
6250 code = code->block->next;
6251 if (atomic_code->ext.omp_clauses->compare)
6252 {
6253 gfc_expr *comp_expr;
6254 if (code->op == EXEC_IF)
6255 {
6256 comp_expr = code->block->expr1;
6257 gcc_assert (code->block->next->op == EXEC_ASSIGN);
6258 expr1 = code->block->next->expr1;
6259 expr2 = code->block->next->expr2;
6260 if (code->block->block)
6261 {
6262 gcc_assert (atomic_code->ext.omp_clauses->capture
6263 && code->block->block->next->op == EXEC_ASSIGN);
6264 else_branch = true;
6265 aop = OMP_ATOMIC_CAPTURE_OLD;
6266 capture_expr1 = code->block->block->next->expr1;
6267 capture_expr2 = code->block->block->next->expr2;
6268 }
6269 else if (atomic_code->ext.omp_clauses->capture)
6270 {
6271 gcc_assert (code->next->op == EXEC_ASSIGN);
6272 aop = OMP_ATOMIC_CAPTURE_NEW;
6273 capture_expr1 = code->next->expr1;
6274 capture_expr2 = code->next->expr2;
6275 }
6276 }
6277 else
6278 {
6279 gcc_assert (atomic_code->ext.omp_clauses->capture
6280 && code->op == EXEC_ASSIGN
6281 && code->next->op == EXEC_IF);
6282 aop = OMP_ATOMIC_CAPTURE_OLD;
6283 capture_expr1 = code->expr1;
6284 capture_expr2 = code->expr2;
6285 expr1 = code->next->block->next->expr1;
6286 expr2 = code->next->block->next->expr2;
6287 comp_expr = code->next->block->expr1;
6288 }
6289 gfc_init_se (&lse, NULL);
6290 gfc_conv_expr (&lse, comp_expr->value.op.op2);
6291 gfc_add_block_to_block (&block, &lse.pre);
6292 compare = lse.expr;
6293 var = expr1->symtree->n.sym;
6294 }
6295 else
6296 {
6297 gcc_assert (code->op == EXEC_ASSIGN);
6298 expr1 = code->expr1;
6299 expr2 = code->expr2;
6300 if (atomic_code->ext.omp_clauses->capture
6301 && (expr2->expr_type == EXPR_VARIABLE
6302 || (expr2->expr_type == EXPR_FUNCTION
6303 && expr2->value.function.isym
6304 && expr2->value.function.isym->id == GFC_ISYM_CONVERSION
6305 && (expr2->value.function.actual->expr->expr_type
6306 == EXPR_VARIABLE))))
6307 {
6308 capture_expr1 = expr1;
6309 capture_expr2 = expr2;
6310 expr1 = code->next->expr1;
6311 expr2 = code->next->expr2;
6312 aop = OMP_ATOMIC_CAPTURE_OLD;
6313 }
6314 else if (atomic_code->ext.omp_clauses->capture)
6315 {
6316 aop = OMP_ATOMIC_CAPTURE_NEW;
6317 capture_expr1 = code->next->expr1;
6318 capture_expr2 = code->next->expr2;
6319 }
6320 var = expr1->symtree->n.sym;
6321 }
6322
6323 gfc_init_se (&lse, NULL);
6324 gfc_init_se (&rse, NULL);
6325 gfc_init_se (&vse, NULL);
6326 gfc_start_block (&block);
6327
6328 if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
6329 != GFC_OMP_ATOMIC_WRITE)
6330 && expr2->expr_type == EXPR_FUNCTION
6331 && expr2->value.function.isym
6332 && expr2->value.function.isym->id == GFC_ISYM_CONVERSION)
6333 expr2 = expr2->value.function.actual->expr;
6334
6335 if ((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
6336 == GFC_OMP_ATOMIC_READ)
6337 {
6338 gfc_conv_expr (&vse, expr1);
6339 gfc_add_block_to_block (&block, &vse.pre);
6340
6341 gfc_conv_expr (&lse, expr2);
6342 gfc_add_block_to_block (&block, &lse.pre);
6343 type = TREE_TYPE (lse.expr);
6344 lhsaddr = gfc_build_addr_expr (NULL, lse.expr);
6345
6346 x = build1 (OMP_ATOMIC_READ, type, lhsaddr);
6347 OMP_ATOMIC_MEMORY_ORDER (x) = mo;
6348 x = convert (TREE_TYPE (vse.expr), x);
6349 gfc_add_modify (&block, vse.expr, x);
6350
6351 gfc_add_block_to_block (&block, &lse.pre);
6352 gfc_add_block_to_block (&block, &rse.pre);
6353
6354 return gfc_finish_block (&block);
6355 }
6356
6357 if (capture_expr2
6358 && capture_expr2->expr_type == EXPR_FUNCTION
6359 && capture_expr2->value.function.isym
6360 && capture_expr2->value.function.isym->id == GFC_ISYM_CONVERSION)
6361 capture_expr2 = capture_expr2->value.function.actual->expr;
6362 gcc_assert (!capture_expr2 || capture_expr2->expr_type == EXPR_VARIABLE);
6363
6364 if (aop == OMP_ATOMIC_CAPTURE_OLD)
6365 {
6366 gfc_conv_expr (&vse, capture_expr1);
6367 gfc_add_block_to_block (&block, &vse.pre);
6368 gfc_conv_expr (&lse, capture_expr2);
6369 gfc_add_block_to_block (&block, &lse.pre);
6370 gfc_init_se (&lse, NULL);
6371 }
6372
6373 gfc_conv_expr (&lse, expr1);
6374 gfc_add_block_to_block (&block, &lse.pre);
6375 type = TREE_TYPE (lse.expr);
6376 lhsaddr = gfc_build_addr_expr (NULL, lse.expr);
6377
6378 if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
6379 == GFC_OMP_ATOMIC_WRITE)
6380 || (atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_SWAP)
6381 || compare)
6382 {
6383 gfc_conv_expr (&rse, expr2);
6384 gfc_add_block_to_block (&block, &rse.pre);
6385 }
6386 else if (expr2->expr_type == EXPR_OP)
6387 {
6388 gfc_expr *e;
6389 switch (expr2->value.op.op)
6390 {
6391 case INTRINSIC_PLUS:
6392 op = PLUS_EXPR;
6393 break;
6394 case INTRINSIC_TIMES:
6395 op = MULT_EXPR;
6396 break;
6397 case INTRINSIC_MINUS:
6398 op = MINUS_EXPR;
6399 break;
6400 case INTRINSIC_DIVIDE:
6401 if (expr2->ts.type == BT_INTEGER)
6402 op = TRUNC_DIV_EXPR;
6403 else
6404 op = RDIV_EXPR;
6405 break;
6406 case INTRINSIC_AND:
6407 op = TRUTH_ANDIF_EXPR;
6408 break;
6409 case INTRINSIC_OR:
6410 op = TRUTH_ORIF_EXPR;
6411 break;
6412 case INTRINSIC_EQV:
6413 op = EQ_EXPR;
6414 break;
6415 case INTRINSIC_NEQV:
6416 op = NE_EXPR;
6417 break;
6418 default:
6419 gcc_unreachable ();
6420 }
6421 e = expr2->value.op.op1;
6422 if (e->expr_type == EXPR_FUNCTION
6423 && e->value.function.isym
6424 && e->value.function.isym->id == GFC_ISYM_CONVERSION)
6425 e = e->value.function.actual->expr;
6426 if (e->expr_type == EXPR_VARIABLE
6427 && e->symtree != NULL
6428 && e->symtree->n.sym == var)
6429 {
6430 expr2 = expr2->value.op.op2;
6431 var_on_left = true;
6432 }
6433 else
6434 {
6435 e = expr2->value.op.op2;
6436 if (e->expr_type == EXPR_FUNCTION
6437 && e->value.function.isym
6438 && e->value.function.isym->id == GFC_ISYM_CONVERSION)
6439 e = e->value.function.actual->expr;
6440 gcc_assert (e->expr_type == EXPR_VARIABLE
6441 && e->symtree != NULL
6442 && e->symtree->n.sym == var);
6443 expr2 = expr2->value.op.op1;
6444 var_on_left = false;
6445 }
6446 gfc_conv_expr (&rse, expr2);
6447 gfc_add_block_to_block (&block, &rse.pre);
6448 }
6449 else
6450 {
6451 gcc_assert (expr2->expr_type == EXPR_FUNCTION);
6452 switch (expr2->value.function.isym->id)
6453 {
6454 case GFC_ISYM_MIN:
6455 op = MIN_EXPR;
6456 break;
6457 case GFC_ISYM_MAX:
6458 op = MAX_EXPR;
6459 break;
6460 case GFC_ISYM_IAND:
6461 op = BIT_AND_EXPR;
6462 break;
6463 case GFC_ISYM_IOR:
6464 op = BIT_IOR_EXPR;
6465 break;
6466 case GFC_ISYM_IEOR:
6467 op = BIT_XOR_EXPR;
6468 break;
6469 default:
6470 gcc_unreachable ();
6471 }
6472 e = expr2->value.function.actual->expr;
6473 if (e->expr_type == EXPR_FUNCTION
6474 && e->value.function.isym
6475 && e->value.function.isym->id == GFC_ISYM_CONVERSION)
6476 e = e->value.function.actual->expr;
6477 gcc_assert (e->expr_type == EXPR_VARIABLE
6478 && e->symtree != NULL
6479 && e->symtree->n.sym == var);
6480
6481 gfc_conv_expr (&rse, expr2->value.function.actual->next->expr);
6482 gfc_add_block_to_block (&block, &rse.pre);
6483 if (expr2->value.function.actual->next->next != NULL)
6484 {
6485 tree accum = gfc_create_var (TREE_TYPE (rse.expr), NULL);
6486 gfc_actual_arglist *arg;
6487
6488 gfc_add_modify (&block, accum, rse.expr);
6489 for (arg = expr2->value.function.actual->next->next; arg;
6490 arg = arg->next)
6491 {
6492 gfc_init_block (&rse.pre);
6493 gfc_conv_expr (&rse, arg->expr);
6494 gfc_add_block_to_block (&block, &rse.pre);
6495 x = fold_build2_loc (input_location, op, TREE_TYPE (accum),
6496 accum, rse.expr);
6497 gfc_add_modify (&block, accum, x);
6498 }
6499
6500 rse.expr = accum;
6501 }
6502
6503 expr2 = expr2->value.function.actual->next->expr;
6504 }
6505
6506 lhsaddr = save_expr (lhsaddr);
6507 if (TREE_CODE (lhsaddr) != SAVE_EXPR
6508 && (TREE_CODE (lhsaddr) != ADDR_EXPR
6509 || !VAR_P (TREE_OPERAND (lhsaddr, 0))))
6510 {
6511 /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
6512 it even after unsharing function body. */
6513 tree var = create_tmp_var_raw (TREE_TYPE (lhsaddr));
6514 DECL_CONTEXT (var) = current_function_decl;
6515 lhsaddr = build4 (TARGET_EXPR, TREE_TYPE (lhsaddr), var, lhsaddr,
6516 NULL_TREE, NULL_TREE);
6517 }
6518
6519 if (compare)
6520 {
6521 tree var = create_tmp_var_raw (TREE_TYPE (lhsaddr));
6522 DECL_CONTEXT (var) = current_function_decl;
6523 lhsaddr = build4 (TARGET_EXPR, TREE_TYPE (lhsaddr), var, lhsaddr, NULL,
6524 NULL);
6525 lse.expr = build_fold_indirect_ref_loc (input_location, lhsaddr);
6526 compare = convert (TREE_TYPE (lse.expr), compare);
6527 compare = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
6528 lse.expr, compare);
6529 }
6530
6531 if (expr2->expr_type == EXPR_VARIABLE || compare)
6532 rhs = rse.expr;
6533 else
6534 rhs = gfc_evaluate_now (rse.expr, &block);
6535
6536 if (((atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_MASK)
6537 == GFC_OMP_ATOMIC_WRITE)
6538 || (atomic_code->ext.omp_clauses->atomic_op & GFC_OMP_ATOMIC_SWAP)
6539 || compare)
6540 x = rhs;
6541 else
6542 {
6543 x = convert (TREE_TYPE (rhs),
6544 build_fold_indirect_ref_loc (input_location, lhsaddr));
6545 if (var_on_left)
6546 x = fold_build2_loc (input_location, op, TREE_TYPE (rhs), x, rhs);
6547 else
6548 x = fold_build2_loc (input_location, op, TREE_TYPE (rhs), rhs, x);
6549 }
6550
6551 if (TREE_CODE (TREE_TYPE (rhs)) == COMPLEX_TYPE
6552 && TREE_CODE (type) != COMPLEX_TYPE)
6553 x = fold_build1_loc (input_location, REALPART_EXPR,
6554 TREE_TYPE (TREE_TYPE (rhs)), x);
6555
6556 gfc_add_block_to_block (&block, &lse.pre);
6557 gfc_add_block_to_block (&block, &rse.pre);
6558
6559 if (aop == OMP_ATOMIC_CAPTURE_NEW)
6560 {
6561 gfc_conv_expr (&vse, capture_expr1);
6562 gfc_add_block_to_block (&block, &vse.pre);
6563 gfc_add_block_to_block (&block, &lse.pre);
6564 }
6565
6566 if (compare && else_branch)
6567 {
6568 tree var2 = create_tmp_var_raw (boolean_type_node);
6569 DECL_CONTEXT (var2) = current_function_decl;
6570 comp_tgt = build4 (TARGET_EXPR, boolean_type_node, var2,
6571 boolean_false_node, NULL, NULL);
6572 compare = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (var2),
6573 var2, compare);
6574 TREE_OPERAND (compare, 0) = comp_tgt;
6575 compare = omit_one_operand_loc (input_location, boolean_type_node,
6576 compare, comp_tgt);
6577 }
6578
6579 if (compare)
6580 x = build3_loc (input_location, COND_EXPR, type, compare,
6581 convert (type, x), lse.expr);
6582
6583 if (aop == OMP_ATOMIC)
6584 {
6585 x = build2_v (OMP_ATOMIC, lhsaddr, convert (type, x));
6586 OMP_ATOMIC_MEMORY_ORDER (x) = mo;
6587 OMP_ATOMIC_WEAK (x) = atomic_code->ext.omp_clauses->weak;
6588 gfc_add_expr_to_block (&block, x);
6589 }
6590 else
6591 {
6592 x = build2 (aop, type, lhsaddr, convert (type, x));
6593 OMP_ATOMIC_MEMORY_ORDER (x) = mo;
6594 OMP_ATOMIC_WEAK (x) = atomic_code->ext.omp_clauses->weak;
6595 if (compare && else_branch)
6596 {
6597 tree vtmp = create_tmp_var_raw (TREE_TYPE (x));
6598 DECL_CONTEXT (vtmp) = current_function_decl;
6599 x = fold_build2_loc (input_location, MODIFY_EXPR,
6600 TREE_TYPE (vtmp), vtmp, x);
6601 vtmp = build4 (TARGET_EXPR, TREE_TYPE (vtmp), vtmp,
6602 build_zero_cst (TREE_TYPE (vtmp)), NULL, NULL);
6603 TREE_OPERAND (x, 0) = vtmp;
6604 tree x2 = convert (TREE_TYPE (vse.expr), vtmp);
6605 x2 = fold_build2_loc (input_location, MODIFY_EXPR,
6606 TREE_TYPE (vse.expr), vse.expr, x2);
6607 x2 = build3_loc (input_location, COND_EXPR, void_type_node, comp_tgt,
6608 void_node, x2);
6609 x = omit_one_operand_loc (input_location, TREE_TYPE (x2), x2, x);
6610 gfc_add_expr_to_block (&block, x);
6611 }
6612 else
6613 {
6614 x = convert (TREE_TYPE (vse.expr), x);
6615 gfc_add_modify (&block, vse.expr, x);
6616 }
6617 }
6618
6619 return gfc_finish_block (&block);
6620 }
6621
6622 static tree
6623 gfc_trans_omp_barrier (void)
6624 {
6625 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6626 return build_call_expr_loc (input_location, decl, 0);
6627 }
6628
6629 static tree
6630 gfc_trans_omp_cancel (gfc_code *code)
6631 {
6632 int mask = 0;
6633 tree ifc = boolean_true_node;
6634 stmtblock_t block;
6635 switch (code->ext.omp_clauses->cancel)
6636 {
6637 case OMP_CANCEL_PARALLEL: mask = 1; break;
6638 case OMP_CANCEL_DO: mask = 2; break;
6639 case OMP_CANCEL_SECTIONS: mask = 4; break;
6640 case OMP_CANCEL_TASKGROUP: mask = 8; break;
6641 default: gcc_unreachable ();
6642 }
6643 gfc_start_block (&block);
6644 if (code->ext.omp_clauses->if_expr
6645 || code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL])
6646 {
6647 gfc_se se;
6648 tree if_var;
6649
6650 gcc_assert ((code->ext.omp_clauses->if_expr == NULL)
6651 ^ (code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL] == NULL));
6652 gfc_init_se (&se, NULL);
6653 gfc_conv_expr (&se, code->ext.omp_clauses->if_expr != NULL
6654 ? code->ext.omp_clauses->if_expr
6655 : code->ext.omp_clauses->if_exprs[OMP_IF_CANCEL]);
6656 gfc_add_block_to_block (&block, &se.pre);
6657 if_var = gfc_evaluate_now (se.expr, &block);
6658 gfc_add_block_to_block (&block, &se.post);
6659 tree type = TREE_TYPE (if_var);
6660 ifc = fold_build2_loc (input_location, NE_EXPR,
6661 boolean_type_node, if_var,
6662 build_zero_cst (type));
6663 }
6664 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6665 tree c_bool_type = TREE_TYPE (TREE_TYPE (decl));
6666 ifc = fold_convert (c_bool_type, ifc);
6667 gfc_add_expr_to_block (&block,
6668 build_call_expr_loc (input_location, decl, 2,
6669 build_int_cst (integer_type_node,
6670 mask), ifc));
6671 return gfc_finish_block (&block);
6672 }
6673
6674 static tree
6675 gfc_trans_omp_cancellation_point (gfc_code *code)
6676 {
6677 int mask = 0;
6678 switch (code->ext.omp_clauses->cancel)
6679 {
6680 case OMP_CANCEL_PARALLEL: mask = 1; break;
6681 case OMP_CANCEL_DO: mask = 2; break;
6682 case OMP_CANCEL_SECTIONS: mask = 4; break;
6683 case OMP_CANCEL_TASKGROUP: mask = 8; break;
6684 default: gcc_unreachable ();
6685 }
6686 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6687 return build_call_expr_loc (input_location, decl, 1,
6688 build_int_cst (integer_type_node, mask));
6689 }
6690
6691 static tree
6692 gfc_trans_omp_critical (gfc_code *code)
6693 {
6694 stmtblock_t block;
6695 tree stmt, name = NULL_TREE;
6696 if (code->ext.omp_clauses->critical_name != NULL)
6697 name = get_identifier (code->ext.omp_clauses->critical_name);
6698 gfc_start_block (&block);
6699 stmt = make_node (OMP_CRITICAL);
6700 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
6701 TREE_TYPE (stmt) = void_type_node;
6702 OMP_CRITICAL_BODY (stmt) = gfc_trans_code (code->block->next);
6703 OMP_CRITICAL_NAME (stmt) = name;
6704 OMP_CRITICAL_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
6705 code->ext.omp_clauses,
6706 code->loc);
6707 gfc_add_expr_to_block (&block, stmt);
6708 return gfc_finish_block (&block);
6709 }
6710
6711 typedef struct dovar_init_d {
6712 gfc_symbol *sym;
6713 tree var;
6714 tree init;
6715 bool non_unit_iter;
6716 } dovar_init;
6717
6718 static bool
6719 gfc_nonrect_loop_expr (stmtblock_t *pblock, gfc_se *sep, int loop_n,
6720 gfc_code *code, gfc_expr *expr, vec<dovar_init> *inits,
6721 int simple, gfc_expr *curr_loop_var)
6722 {
6723 int i;
6724 for (i = 0; i < loop_n; i++)
6725 {
6726 gcc_assert (code->ext.iterator->var->expr_type == EXPR_VARIABLE);
6727 if (gfc_find_sym_in_expr (code->ext.iterator->var->symtree->n.sym, expr))
6728 break;
6729 code = code->block->next;
6730 }
6731 if (i >= loop_n)
6732 return false;
6733
6734 /* Canonical format: TREE_VEC with [var, multiplier, offset]. */
6735 gfc_symbol *var = code->ext.iterator->var->symtree->n.sym;
6736
6737 tree tree_var = NULL_TREE;
6738 tree a1 = integer_one_node;
6739 tree a2 = integer_zero_node;
6740
6741 if (!simple)
6742 {
6743 /* FIXME: Handle non-const iter steps, cf. PR fortran/110735. */
6744 sorry_at (gfc_get_location (&curr_loop_var->where),
6745 "non-rectangular loop nest with non-constant step for %qs",
6746 curr_loop_var->symtree->n.sym->name);
6747 return false;
6748 }
6749
6750 dovar_init *di;
6751 unsigned ix;
6752 FOR_EACH_VEC_ELT (*inits, ix, di)
6753 if (di->sym == var)
6754 {
6755 if (!di->non_unit_iter)
6756 {
6757 tree_var = di->init;
6758 gcc_assert (DECL_P (tree_var));
6759 break;
6760 }
6761 else
6762 {
6763 /* FIXME: Handle non-const iter steps, cf. PR fortran/110735. */
6764 sorry_at (gfc_get_location (&code->loc),
6765 "non-rectangular loop nest with non-constant step "
6766 "for %qs", var->name);
6767 inform (gfc_get_location (&expr->where), "Used here");
6768 return false;
6769 }
6770 }
6771 if (tree_var == NULL_TREE)
6772 tree_var = var->backend_decl;
6773
6774 if (expr->expr_type == EXPR_VARIABLE)
6775 gcc_assert (expr->symtree->n.sym == var);
6776 else if (expr->expr_type != EXPR_OP
6777 || (expr->value.op.op != INTRINSIC_TIMES
6778 && expr->value.op.op != INTRINSIC_PLUS
6779 && expr->value.op.op != INTRINSIC_MINUS))
6780 gcc_unreachable ();
6781 else
6782 {
6783 gfc_se se;
6784 gfc_expr *et = NULL, *eo = NULL, *e = expr;
6785 if (expr->value.op.op != INTRINSIC_TIMES)
6786 {
6787 if (gfc_find_sym_in_expr (var, expr->value.op.op1))
6788 {
6789 e = expr->value.op.op1;
6790 eo = expr->value.op.op2;
6791 }
6792 else
6793 {
6794 eo = expr->value.op.op1;
6795 e = expr->value.op.op2;
6796 }
6797 }
6798 if (e->value.op.op == INTRINSIC_TIMES)
6799 {
6800 if (e->value.op.op1->expr_type == EXPR_VARIABLE
6801 && e->value.op.op1->symtree->n.sym == var)
6802 et = e->value.op.op2;
6803 else
6804 {
6805 et = e->value.op.op1;
6806 gcc_assert (e->value.op.op2->expr_type == EXPR_VARIABLE
6807 && e->value.op.op2->symtree->n.sym == var);
6808 }
6809 }
6810 else
6811 gcc_assert (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == var);
6812 if (et != NULL)
6813 {
6814 gfc_init_se (&se, NULL);
6815 gfc_conv_expr_val (&se, et);
6816 gfc_add_block_to_block (pblock, &se.pre);
6817 a1 = se.expr;
6818 }
6819 if (eo != NULL)
6820 {
6821 gfc_init_se (&se, NULL);
6822 gfc_conv_expr_val (&se, eo);
6823 gfc_add_block_to_block (pblock, &se.pre);
6824 a2 = se.expr;
6825 if (expr->value.op.op == INTRINSIC_MINUS && expr->value.op.op2 == eo)
6826 /* outer-var - a2. */
6827 a2 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a2), a2);
6828 else if (expr->value.op.op == INTRINSIC_MINUS)
6829 /* a2 - outer-var. */
6830 a1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a1), a1);
6831 }
6832 a1 = DECL_P (a1) ? a1 : gfc_evaluate_now (a1, pblock);
6833 a2 = DECL_P (a2) ? a2 : gfc_evaluate_now (a2, pblock);
6834 }
6835
6836 gfc_init_se (sep, NULL);
6837 sep->expr = make_tree_vec (3);
6838 TREE_VEC_ELT (sep->expr, 0) = tree_var;
6839 TREE_VEC_ELT (sep->expr, 1) = fold_convert (TREE_TYPE (tree_var), a1);
6840 TREE_VEC_ELT (sep->expr, 2) = fold_convert (TREE_TYPE (tree_var), a2);
6841
6842 return true;
6843 }
6844
6845 int
6846 gfc_expr_list_len (gfc_expr_list *list)
6847 {
6848 unsigned len = 0;
6849 for (; list; list = list->next)
6850 len++;
6851
6852 return len;
6853 }
6854
6855 static tree
6856 gfc_trans_omp_do (gfc_code *code, gfc_exec_op op, stmtblock_t *pblock,
6857 gfc_omp_clauses *do_clauses, tree par_clauses)
6858 {
6859 gfc_se se;
6860 tree dovar, stmt, from, to, step, type, init, cond, incr, orig_decls;
6861 tree local_dovar = NULL_TREE, cycle_label, tmp, omp_clauses;
6862 stmtblock_t block;
6863 stmtblock_t body;
6864 gfc_omp_clauses *clauses = code->ext.omp_clauses;
6865 int i, collapse = clauses->collapse;
6866 vec<dovar_init> inits = vNULL;
6867 dovar_init *di;
6868 unsigned ix;
6869 vec<tree, va_heap, vl_embed> *saved_doacross_steps = doacross_steps;
6870 gfc_expr_list *oacc_tile
6871 = do_clauses ? do_clauses->tile_list : clauses->tile_list;
6872 gfc_expr_list *sizes
6873 = do_clauses ? do_clauses->sizes_list : clauses->sizes_list;
6874 gfc_code *orig_code = code;
6875
6876 /* Both collapsed and tiled loops are lowered the same way. In
6877 OpenACC, those clauses are not compatible, so prioritize the tile
6878 clause, if present. */
6879 if (oacc_tile)
6880 collapse = gfc_expr_list_len (oacc_tile);
6881 else if (sizes)
6882 collapse = gfc_expr_list_len (sizes);
6883
6884 doacross_steps = NULL;
6885 if (clauses->orderedc)
6886 collapse = clauses->orderedc;
6887 if (collapse <= 0)
6888 collapse = 1;
6889
6890 code = code->block->next;
6891
6892 init = make_tree_vec (collapse);
6893 cond = make_tree_vec (collapse);
6894 incr = make_tree_vec (collapse);
6895 orig_decls = clauses->ordered ? make_tree_vec (collapse) : NULL_TREE;
6896
6897 if (pblock == NULL)
6898 {
6899 gfc_start_block (&block);
6900 pblock = &block;
6901 }
6902
6903 /* simd schedule modifier is only useful for composite do simd and other
6904 constructs including that, where gfc_trans_omp_do is only called
6905 on the simd construct and DO's clauses are translated elsewhere. */
6906 do_clauses->sched_simd = false;
6907
6908 omp_clauses = gfc_trans_omp_clauses (pblock, do_clauses, code->loc);
6909
6910 for (i = 0; i < collapse; i++)
6911 {
6912 int simple = 0;
6913 int dovar_found = 0;
6914 tree dovar_decl;
6915
6916 if (code->op == EXEC_OMP_TILE || code->op == EXEC_OMP_UNROLL)
6917 {
6918 TREE_VEC_ELT (init, i) = NULL_TREE;
6919 TREE_VEC_ELT (cond, i) = NULL_TREE;
6920 TREE_VEC_ELT (incr, i) = NULL_TREE;
6921 TREE_VEC_ELT (incr, i) = NULL_TREE;
6922 if (orig_decls)
6923 TREE_VEC_ELT (orig_decls, i) = NULL_TREE;
6924 continue;
6925 }
6926 gcc_assert (code->op == EXEC_DO);
6927 if (clauses)
6928 {
6929 gfc_omp_namelist *n = NULL;
6930 if (op == EXEC_OMP_SIMD && collapse == 1)
6931 for (n = clauses->lists[OMP_LIST_LINEAR];
6932 n != NULL; n = n->next)
6933 if (code->ext.iterator->var->symtree->n.sym == n->sym)
6934 {
6935 dovar_found = 3;
6936 break;
6937 }
6938 if (n == NULL && op != EXEC_OMP_DISTRIBUTE)
6939 for (n = clauses->lists[OMP_LIST_LASTPRIVATE];
6940 n != NULL; n = n->next)
6941 if (code->ext.iterator->var->symtree->n.sym == n->sym)
6942 {
6943 dovar_found = 2;
6944 break;
6945 }
6946 if (n == NULL)
6947 for (n = clauses->lists[OMP_LIST_PRIVATE]; n != NULL; n = n->next)
6948 if (code->ext.iterator->var->symtree->n.sym == n->sym)
6949 {
6950 dovar_found = 1;
6951 break;
6952 }
6953 }
6954
6955 /* Evaluate all the expressions in the iterator. */
6956 gfc_init_se (&se, NULL);
6957 gfc_conv_expr_lhs (&se, code->ext.iterator->var);
6958 gfc_add_block_to_block (pblock, &se.pre);
6959 local_dovar = dovar_decl = dovar = se.expr;
6960 type = TREE_TYPE (dovar);
6961 gcc_assert (TREE_CODE (type) == INTEGER_TYPE);
6962
6963 gfc_init_se (&se, NULL);
6964 gfc_conv_expr_val (&se, code->ext.iterator->step);
6965 gfc_add_block_to_block (pblock, &se.pre);
6966 step = gfc_evaluate_now (se.expr, pblock);
6967
6968 if (TREE_CODE (step) == INTEGER_CST)
6969 simple = tree_int_cst_sgn (step);
6970
6971 gfc_init_se (&se, NULL);
6972 if (!clauses->non_rectangular
6973 || !gfc_nonrect_loop_expr (pblock, &se, i, orig_code->block->next,
6974 code->ext.iterator->start, &inits, simple,
6975 code->ext.iterator->var))
6976 {
6977 gfc_conv_expr_val (&se, code->ext.iterator->start);
6978 gfc_add_block_to_block (pblock, &se.pre);
6979 if (!DECL_P (se.expr))
6980 se.expr = gfc_evaluate_now (se.expr, pblock);
6981 }
6982 from = se.expr;
6983
6984 gfc_init_se (&se, NULL);
6985 if (!clauses->non_rectangular
6986 || !gfc_nonrect_loop_expr (pblock, &se, i, orig_code->block->next,
6987 code->ext.iterator->end, &inits, simple,
6988 code->ext.iterator->var))
6989 {
6990 gfc_conv_expr_val (&se, code->ext.iterator->end);
6991 gfc_add_block_to_block (pblock, &se.pre);
6992 if (!DECL_P (se.expr))
6993 se.expr = gfc_evaluate_now (se.expr, pblock);
6994 }
6995 to = se.expr;
6996
6997 if (!DECL_P (dovar))
6998 dovar_decl
6999 = gfc_trans_omp_variable (code->ext.iterator->var->symtree->n.sym,
7000 false);
7001 if (simple && !DECL_P (dovar))
7002 {
7003 const char *name = code->ext.iterator->var->symtree->n.sym->name;
7004 local_dovar = gfc_create_var (type, name);
7005 dovar_init e = {code->ext.iterator->var->symtree->n.sym,
7006 dovar, local_dovar, false};
7007 inits.safe_push (e);
7008 }
7009 /* Loop body. */
7010 if (simple)
7011 {
7012 TREE_VEC_ELT (init, i) = build2_v (MODIFY_EXPR, local_dovar, from);
7013 /* The condition should not be folded. */
7014 TREE_VEC_ELT (cond, i) = build2_loc (input_location, simple > 0
7015 ? LE_EXPR : GE_EXPR,
7016 logical_type_node, local_dovar,
7017 to);
7018 TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location, PLUS_EXPR,
7019 type, local_dovar, step);
7020 TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location,
7021 MODIFY_EXPR,
7022 type, local_dovar,
7023 TREE_VEC_ELT (incr, i));
7024 if (orig_decls && !clauses->orderedc)
7025 orig_decls = NULL;
7026 else if (orig_decls)
7027 TREE_VEC_ELT (orig_decls, i) = dovar_decl;
7028 }
7029 else
7030 {
7031 /* STEP is not 1 or -1. Use:
7032 for (count = 0; count < (to + step - from) / step; count++)
7033 {
7034 dovar = from + count * step;
7035 body;
7036 cycle_label:;
7037 } */
7038 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, step, from);
7039 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, to, tmp);
7040 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, type, tmp,
7041 step);
7042 tmp = gfc_evaluate_now (tmp, pblock);
7043 local_dovar = gfc_create_var (type, "count");
7044 TREE_VEC_ELT (init, i) = build2_v (MODIFY_EXPR, local_dovar,
7045 build_int_cst (type, 0));
7046 /* The condition should not be folded. */
7047 TREE_VEC_ELT (cond, i) = build2_loc (input_location, LT_EXPR,
7048 logical_type_node,
7049 local_dovar, tmp);
7050 TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location, PLUS_EXPR,
7051 type, local_dovar,
7052 build_int_cst (type, 1));
7053 TREE_VEC_ELT (incr, i) = fold_build2_loc (input_location,
7054 MODIFY_EXPR, type,
7055 local_dovar,
7056 TREE_VEC_ELT (incr, i));
7057
7058 /* Initialize DOVAR. */
7059 tmp = fold_build2_loc (input_location, MULT_EXPR, type, local_dovar,
7060 step);
7061 tmp = fold_build2_loc (input_location, PLUS_EXPR, type, from, tmp);
7062 dovar_init e = {code->ext.iterator->var->symtree->n.sym,
7063 dovar, tmp, true};
7064 inits.safe_push (e);
7065 if (clauses->orderedc)
7066 {
7067 if (doacross_steps == NULL)
7068 vec_safe_grow_cleared (doacross_steps, clauses->orderedc, true);
7069 (*doacross_steps)[i] = step;
7070 }
7071 if (orig_decls)
7072 TREE_VEC_ELT (orig_decls, i) = dovar_decl;
7073 }
7074
7075 if (dovar_found == 3
7076 && op == EXEC_OMP_SIMD
7077 && collapse == 1
7078 && local_dovar != dovar)
7079 {
7080 for (tmp = omp_clauses; tmp; tmp = OMP_CLAUSE_CHAIN (tmp))
7081 if (OMP_CLAUSE_CODE (tmp) == OMP_CLAUSE_LINEAR
7082 && OMP_CLAUSE_DECL (tmp) == dovar)
7083 {
7084 OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
7085 break;
7086 }
7087 }
7088 if (!dovar_found && op == EXEC_OMP_SIMD)
7089 {
7090 if (collapse == 1)
7091 {
7092 tmp = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7093 OMP_CLAUSE_LINEAR_STEP (tmp) = step;
7094 OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
7095 OMP_CLAUSE_DECL (tmp) = dovar_decl;
7096 omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
7097 if (local_dovar != dovar)
7098 dovar_found = 3;
7099 }
7100 }
7101 else if (!dovar_found && local_dovar != dovar)
7102 {
7103 tmp = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
7104 OMP_CLAUSE_DECL (tmp) = dovar_decl;
7105 omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
7106 }
7107 if (dovar_found > 1)
7108 {
7109 tree c = NULL;
7110
7111 tmp = NULL;
7112 if (local_dovar != dovar)
7113 {
7114 /* If dovar is lastprivate, but different counter is used,
7115 dovar += step needs to be added to
7116 OMP_CLAUSE_LASTPRIVATE_STMT, otherwise the copied dovar
7117 will have the value on entry of the last loop, rather
7118 than value after iterator increment. */
7119 if (clauses->orderedc)
7120 {
7121 if (clauses->collapse <= 1 || i >= clauses->collapse)
7122 tmp = local_dovar;
7123 else
7124 tmp = fold_build2_loc (input_location, PLUS_EXPR,
7125 type, local_dovar,
7126 build_one_cst (type));
7127 tmp = fold_build2_loc (input_location, MULT_EXPR, type,
7128 tmp, step);
7129 tmp = fold_build2_loc (input_location, PLUS_EXPR, type,
7130 from, tmp);
7131 }
7132 else
7133 tmp = fold_build2_loc (input_location, PLUS_EXPR, type,
7134 dovar, step);
7135 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type,
7136 dovar, tmp);
7137 for (c = omp_clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7138 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7139 && OMP_CLAUSE_DECL (c) == dovar_decl)
7140 {
7141 OMP_CLAUSE_LASTPRIVATE_STMT (c) = tmp;
7142 break;
7143 }
7144 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7145 && OMP_CLAUSE_DECL (c) == dovar_decl)
7146 {
7147 OMP_CLAUSE_LINEAR_STMT (c) = tmp;
7148 break;
7149 }
7150 }
7151 if (c == NULL && op == EXEC_OMP_DO && par_clauses != NULL)
7152 {
7153 for (c = par_clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7154 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7155 && OMP_CLAUSE_DECL (c) == dovar_decl)
7156 {
7157 tree l = build_omp_clause (input_location,
7158 OMP_CLAUSE_LASTPRIVATE);
7159 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
7160 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (l) = 1;
7161 OMP_CLAUSE_DECL (l) = dovar_decl;
7162 OMP_CLAUSE_CHAIN (l) = omp_clauses;
7163 OMP_CLAUSE_LASTPRIVATE_STMT (l) = tmp;
7164 omp_clauses = l;
7165 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_SHARED);
7166 break;
7167 }
7168 }
7169 gcc_assert (local_dovar == dovar || c != NULL);
7170 }
7171 if (local_dovar != dovar)
7172 {
7173 if (op != EXEC_OMP_SIMD || dovar_found == 1)
7174 tmp = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
7175 else if (collapse == 1)
7176 {
7177 tmp = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7178 OMP_CLAUSE_LINEAR_STEP (tmp) = build_int_cst (type, 1);
7179 OMP_CLAUSE_LINEAR_NO_COPYIN (tmp) = 1;
7180 OMP_CLAUSE_LINEAR_NO_COPYOUT (tmp) = 1;
7181 }
7182 else
7183 tmp = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
7184 OMP_CLAUSE_DECL (tmp) = local_dovar;
7185 omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
7186 }
7187
7188 if (i + 1 < collapse)
7189 code = code->block->next;
7190 }
7191
7192 if (pblock != &block)
7193 {
7194 pushlevel ();
7195 gfc_start_block (&block);
7196 }
7197
7198 gfc_start_block (&body);
7199
7200 FOR_EACH_VEC_ELT (inits, ix, di)
7201 gfc_add_modify (&body, di->var, di->init);
7202 inits.release ();
7203
7204 /* Cycle statement is implemented with a goto. Exit statement must not be
7205 present for this loop. */
7206 cycle_label = gfc_build_label_decl (NULL_TREE);
7207
7208 /* Put these labels where they can be found later. */
7209
7210 code->cycle_label = cycle_label;
7211 code->exit_label = NULL_TREE;
7212
7213 /* Main loop body. */
7214 if (clauses->lists[OMP_LIST_REDUCTION_INSCAN])
7215 {
7216 gfc_code *code1, *scan, *code2, *tmpcode;
7217 code1 = tmpcode = code->block->next;
7218 if (tmpcode && tmpcode->op != EXEC_OMP_SCAN)
7219 while (tmpcode && tmpcode->next && tmpcode->next->op != EXEC_OMP_SCAN)
7220 tmpcode = tmpcode->next;
7221 scan = tmpcode->op == EXEC_OMP_SCAN ? tmpcode : tmpcode->next;
7222 if (code1 != scan)
7223 tmpcode->next = NULL;
7224 code2 = scan->next;
7225 gcc_assert (scan->op == EXEC_OMP_SCAN);
7226 location_t loc = gfc_get_location (&scan->loc);
7227
7228 tmp = code1 != scan ? gfc_trans_code (code1) : build_empty_stmt (loc);
7229 tmp = build2 (OMP_SCAN, void_type_node, tmp, NULL_TREE);
7230 SET_EXPR_LOCATION (tmp, loc);
7231 gfc_add_expr_to_block (&body, tmp);
7232 input_location = loc;
7233 tree c = gfc_trans_omp_clauses (&body, scan->ext.omp_clauses, scan->loc);
7234 tmp = code2 ? gfc_trans_code (code2) : build_empty_stmt (loc);
7235 tmp = build2 (OMP_SCAN, void_type_node, tmp, c);
7236 SET_EXPR_LOCATION (tmp, loc);
7237 if (code1 != scan)
7238 tmpcode->next = scan;
7239 }
7240 else if (code->op == EXEC_OMP_TILE || code->op == EXEC_OMP_UNROLL)
7241 tmp = gfc_trans_omp_code (code, true);
7242 else
7243 tmp = gfc_trans_omp_code (code->block->next, true);
7244 gfc_add_expr_to_block (&body, tmp);
7245
7246 /* Label for cycle statements (if needed). */
7247 if (TREE_USED (cycle_label))
7248 {
7249 tmp = build1_v (LABEL_EXPR, cycle_label);
7250 gfc_add_expr_to_block (&body, tmp);
7251 }
7252
7253 /* End of loop body. */
7254 switch (op)
7255 {
7256 case EXEC_OMP_SIMD: stmt = make_node (OMP_SIMD); break;
7257 case EXEC_OMP_DO: stmt = make_node (OMP_FOR); break;
7258 case EXEC_OMP_DISTRIBUTE: stmt = make_node (OMP_DISTRIBUTE); break;
7259 case EXEC_OMP_LOOP: stmt = make_node (OMP_LOOP); break;
7260 case EXEC_OMP_TASKLOOP: stmt = make_node (OMP_TASKLOOP); break;
7261 case EXEC_OACC_LOOP: stmt = make_node (OACC_LOOP); break;
7262 case EXEC_OMP_TILE: stmt = make_node (OMP_TILE); break;
7263 case EXEC_OMP_UNROLL: stmt = make_node (OMP_UNROLL); break;
7264 default: gcc_unreachable ();
7265 }
7266
7267 SET_EXPR_LOCATION (stmt, gfc_get_location (&orig_code->loc));
7268 TREE_TYPE (stmt) = void_type_node;
7269 OMP_FOR_BODY (stmt) = gfc_finish_block (&body);
7270 OMP_FOR_CLAUSES (stmt) = omp_clauses;
7271 OMP_FOR_INIT (stmt) = init;
7272 OMP_FOR_COND (stmt) = cond;
7273 OMP_FOR_INCR (stmt) = incr;
7274 if (orig_decls)
7275 OMP_FOR_ORIG_DECLS (stmt) = orig_decls;
7276 OMP_FOR_NON_RECTANGULAR (stmt) = clauses->non_rectangular;
7277 gfc_add_expr_to_block (&block, stmt);
7278
7279 vec_free (doacross_steps);
7280 doacross_steps = saved_doacross_steps;
7281
7282 return gfc_finish_block (&block);
7283 }
7284
7285 /* Translate combined OpenACC 'parallel loop', 'kernels loop', 'serial loop'
7286 construct. */
7287
7288 static tree
7289 gfc_trans_oacc_combined_directive (gfc_code *code)
7290 {
7291 stmtblock_t block, *pblock = NULL;
7292 gfc_omp_clauses construct_clauses, loop_clauses;
7293 tree stmt, oacc_clauses = NULL_TREE;
7294 enum tree_code construct_code;
7295 location_t loc = input_location;
7296
7297 switch (code->op)
7298 {
7299 case EXEC_OACC_PARALLEL_LOOP:
7300 construct_code = OACC_PARALLEL;
7301 break;
7302 case EXEC_OACC_KERNELS_LOOP:
7303 construct_code = OACC_KERNELS;
7304 break;
7305 case EXEC_OACC_SERIAL_LOOP:
7306 construct_code = OACC_SERIAL;
7307 break;
7308 default:
7309 gcc_unreachable ();
7310 }
7311
7312 gfc_start_block (&block);
7313
7314 memset (&loop_clauses, 0, sizeof (loop_clauses));
7315 if (code->ext.omp_clauses != NULL)
7316 {
7317 memcpy (&construct_clauses, code->ext.omp_clauses,
7318 sizeof (construct_clauses));
7319 loop_clauses.collapse = construct_clauses.collapse;
7320 loop_clauses.gang = construct_clauses.gang;
7321 loop_clauses.gang_static = construct_clauses.gang_static;
7322 loop_clauses.gang_num_expr = construct_clauses.gang_num_expr;
7323 loop_clauses.gang_static_expr = construct_clauses.gang_static_expr;
7324 loop_clauses.vector = construct_clauses.vector;
7325 loop_clauses.vector_expr = construct_clauses.vector_expr;
7326 loop_clauses.worker = construct_clauses.worker;
7327 loop_clauses.worker_expr = construct_clauses.worker_expr;
7328 loop_clauses.seq = construct_clauses.seq;
7329 loop_clauses.par_auto = construct_clauses.par_auto;
7330 loop_clauses.independent = construct_clauses.independent;
7331 loop_clauses.tile_list = construct_clauses.tile_list;
7332 loop_clauses.lists[OMP_LIST_PRIVATE]
7333 = construct_clauses.lists[OMP_LIST_PRIVATE];
7334 loop_clauses.lists[OMP_LIST_REDUCTION]
7335 = construct_clauses.lists[OMP_LIST_REDUCTION];
7336 construct_clauses.gang = false;
7337 construct_clauses.gang_static = false;
7338 construct_clauses.gang_num_expr = NULL;
7339 construct_clauses.gang_static_expr = NULL;
7340 construct_clauses.vector = false;
7341 construct_clauses.vector_expr = NULL;
7342 construct_clauses.worker = false;
7343 construct_clauses.worker_expr = NULL;
7344 construct_clauses.seq = false;
7345 construct_clauses.par_auto = false;
7346 construct_clauses.independent = false;
7347 construct_clauses.independent = false;
7348 construct_clauses.tile_list = NULL;
7349 construct_clauses.lists[OMP_LIST_PRIVATE] = NULL;
7350 if (construct_code == OACC_KERNELS)
7351 construct_clauses.lists[OMP_LIST_REDUCTION] = NULL;
7352 oacc_clauses = gfc_trans_omp_clauses (&block, &construct_clauses,
7353 code->loc, false, true);
7354 }
7355 if (!loop_clauses.seq)
7356 pblock = &block;
7357 else
7358 pushlevel ();
7359 stmt = gfc_trans_omp_do (code, EXEC_OACC_LOOP, pblock, &loop_clauses, NULL);
7360 protected_set_expr_location (stmt, loc);
7361 if (TREE_CODE (stmt) != BIND_EXPR)
7362 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
7363 else
7364 poplevel (0, 0);
7365 stmt = build2_loc (loc, construct_code, void_type_node, stmt, oacc_clauses);
7366 gfc_add_expr_to_block (&block, stmt);
7367 return gfc_finish_block (&block);
7368 }
7369
7370 static tree
7371 gfc_trans_omp_depobj (gfc_code *code)
7372 {
7373 stmtblock_t block;
7374 gfc_se se;
7375 gfc_init_se (&se, NULL);
7376 gfc_init_block (&block);
7377 gfc_conv_expr (&se, code->ext.omp_clauses->depobj);
7378 gcc_assert (se.pre.head == NULL && se.post.head == NULL);
7379 tree depobj = se.expr;
7380 location_t loc = EXPR_LOCATION (depobj);
7381 if (!POINTER_TYPE_P (TREE_TYPE (depobj)))
7382 depobj = gfc_build_addr_expr (NULL, depobj);
7383 depobj = fold_convert (build_pointer_type_for_mode (ptr_type_node,
7384 TYPE_MODE (ptr_type_node),
7385 true), depobj);
7386 gfc_omp_namelist *n = code->ext.omp_clauses->lists[OMP_LIST_DEPEND];
7387 if (n)
7388 {
7389 tree var;
7390 if (!n->sym) /* omp_all_memory. */
7391 var = null_pointer_node;
7392 else if (n->expr && n->expr->ref->u.ar.type != AR_FULL)
7393 {
7394 gfc_init_se (&se, NULL);
7395 if (n->expr->rank == 0)
7396 {
7397 gfc_conv_expr_reference (&se, n->expr);
7398 var = se.expr;
7399 }
7400 else
7401 {
7402 gfc_conv_expr_descriptor (&se, n->expr);
7403 var = gfc_conv_array_data (se.expr);
7404 }
7405 gfc_add_block_to_block (&block, &se.pre);
7406 gfc_add_block_to_block (&block, &se.post);
7407 gcc_assert (POINTER_TYPE_P (TREE_TYPE (var)));
7408 }
7409 else
7410 {
7411 var = gfc_get_symbol_decl (n->sym);
7412 if (POINTER_TYPE_P (TREE_TYPE (var))
7413 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (var))))
7414 var = build_fold_indirect_ref (var);
7415 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (var)))
7416 {
7417 var = gfc_conv_descriptor_data_get (var);
7418 gcc_assert (POINTER_TYPE_P (TREE_TYPE (var)));
7419 }
7420 else if ((n->sym->attr.allocatable || n->sym->attr.pointer)
7421 && n->sym->attr.dummy)
7422 var = build_fold_indirect_ref (var);
7423 else if (!POINTER_TYPE_P (TREE_TYPE (var))
7424 || (n->sym->ts.f90_type == BT_VOID
7425 && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (var)))
7426 && !GFC_ARRAY_TYPE_P (TREE_TYPE (TREE_TYPE (var)))))
7427 {
7428 TREE_ADDRESSABLE (var) = 1;
7429 var = gfc_build_addr_expr (NULL, var);
7430 }
7431 }
7432 depobj = save_expr (depobj);
7433 tree r = build_fold_indirect_ref_loc (loc, depobj);
7434 gfc_add_expr_to_block (&block,
7435 build2 (MODIFY_EXPR, void_type_node, r, var));
7436 }
7437
7438 /* Only one may be set. */
7439 gcc_assert (((int)(n != NULL) + (int)(code->ext.omp_clauses->destroy)
7440 + (int)(code->ext.omp_clauses->depobj_update != OMP_DEPEND_UNSET))
7441 == 1);
7442 int k = -1; /* omp_clauses->destroy */
7443 if (!code->ext.omp_clauses->destroy)
7444 switch (code->ext.omp_clauses->depobj_update != OMP_DEPEND_UNSET
7445 ? code->ext.omp_clauses->depobj_update : n->u.depend_doacross_op)
7446 {
7447 case OMP_DEPEND_IN: k = GOMP_DEPEND_IN; break;
7448 case OMP_DEPEND_OUT: k = GOMP_DEPEND_OUT; break;
7449 case OMP_DEPEND_INOUT: k = GOMP_DEPEND_INOUT; break;
7450 case OMP_DEPEND_INOUTSET: k = GOMP_DEPEND_INOUTSET; break;
7451 case OMP_DEPEND_MUTEXINOUTSET: k = GOMP_DEPEND_MUTEXINOUTSET; break;
7452 default: gcc_unreachable ();
7453 }
7454 tree t = build_int_cst (ptr_type_node, k);
7455 depobj = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (depobj), depobj,
7456 TYPE_SIZE_UNIT (ptr_type_node));
7457 depobj = build_fold_indirect_ref_loc (loc, depobj);
7458 gfc_add_expr_to_block (&block, build2 (MODIFY_EXPR, void_type_node, depobj, t));
7459
7460 return gfc_finish_block (&block);
7461 }
7462
7463 /* Callback for walk_tree to find an OMP dispatch call and wrap it into an
7464 * IFN_GOMP_DISPATCH. */
7465
7466 static tree
7467 replace_omp_dispatch_call (tree *tp, int *, void *decls_p)
7468 {
7469 tree t = *tp;
7470 tree decls = (tree) decls_p;
7471 tree orig_fn_decl = TREE_PURPOSE (decls);
7472 tree dup_fn_decl = TREE_VALUE (decls);
7473 if (TREE_CODE (t) == CALL_EXPR)
7474 {
7475 if (CALL_EXPR_FN (t) == dup_fn_decl)
7476 CALL_EXPR_FN (t) = orig_fn_decl;
7477 else if (TREE_CODE (CALL_EXPR_FN (t)) == ADDR_EXPR
7478 && TREE_OPERAND (CALL_EXPR_FN (t), 0) == dup_fn_decl)
7479 TREE_OPERAND (CALL_EXPR_FN (t), 0) = dup_fn_decl;
7480 else
7481 return NULL_TREE;
7482 *tp = build_call_expr_internal_loc (input_location, IFN_GOMP_DISPATCH,
7483 TREE_TYPE (t), 1, t);
7484 return *tp;
7485 }
7486
7487 return NULL_TREE;
7488 }
7489
7490 static tree
7491 gfc_trans_omp_dispatch (gfc_code *code)
7492 {
7493 stmtblock_t block;
7494 gfc_code *next = code->block->next;
7495 // assume ill-formed "function dispatch structured
7496 // block" have already been rejected by resolve_omp_dispatch
7497 gcc_assert (next->op == EXEC_CALL || next->op == EXEC_ASSIGN);
7498
7499 // Make duplicate decl for dispatch function call to make it easy to spot
7500 // after translation
7501 gfc_symbol *orig_fn_sym;
7502 gfc_expr *call_expr = next->op == EXEC_CALL ? next->expr1 : next->expr2;
7503 if (call_expr != NULL) // function
7504 {
7505 if (call_expr->value.function.isym != NULL) // dig into convert intrinsics
7506 call_expr = call_expr->value.function.actual->expr;
7507 gcc_assert (call_expr->expr_type == EXPR_FUNCTION);
7508 orig_fn_sym = call_expr->value.function.esym
7509 ? call_expr->value.function.esym
7510 : call_expr->symtree->n.sym;
7511 }
7512 else // subroutine
7513 {
7514 orig_fn_sym = next->resolved_sym;
7515 }
7516 if (!orig_fn_sym->backend_decl)
7517 gfc_get_symbol_decl (orig_fn_sym);
7518 gfc_symbol dup_fn_sym = *orig_fn_sym;
7519 dup_fn_sym.backend_decl = copy_node (orig_fn_sym->backend_decl);
7520 if (call_expr != NULL)
7521 call_expr->value.function.esym = &dup_fn_sym;
7522 else
7523 next->resolved_sym = &dup_fn_sym;
7524
7525 tree body = gfc_trans_code (next);
7526
7527 // Walk the tree to find the duplicate decl, wrap IFN call and replace
7528 // dup decl with original
7529 tree fn_decls
7530 = build_tree_list (orig_fn_sym->backend_decl, dup_fn_sym.backend_decl);
7531 tree dispatch_call
7532 = walk_tree (&body, replace_omp_dispatch_call, fn_decls, NULL);
7533 gcc_assert (dispatch_call != NULL_TREE);
7534
7535 gfc_start_block (&block);
7536 tree omp_clauses
7537 = gfc_trans_omp_clauses (&block, code->ext.omp_clauses, code->loc);
7538
7539 // Extract depend clauses and create taskwait
7540 tree depend_clauses = NULL_TREE;
7541 tree *depend_clauses_ptr = &depend_clauses;
7542 for (tree c = omp_clauses; c; c = OMP_CLAUSE_CHAIN (c))
7543 {
7544 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7545 {
7546 *depend_clauses_ptr = c;
7547 depend_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
7548 }
7549 }
7550 if (depend_clauses != NULL_TREE)
7551 {
7552 tree stmt = make_node (OMP_TASK);
7553 TREE_TYPE (stmt) = void_node;
7554 OMP_TASK_CLAUSES (stmt) = depend_clauses;
7555 OMP_TASK_BODY (stmt) = NULL_TREE;
7556 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
7557 gfc_add_expr_to_block (&block, stmt);
7558 }
7559
7560 tree stmt = make_node (OMP_DISPATCH);
7561 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
7562 TREE_TYPE (stmt) = void_type_node;
7563 OMP_DISPATCH_BODY (stmt) = body;
7564 OMP_DISPATCH_CLAUSES (stmt) = omp_clauses;
7565
7566 gfc_add_expr_to_block (&block, stmt);
7567 return gfc_finish_block (&block);
7568 }
7569
7570 static tree
7571 gfc_trans_omp_error (gfc_code *code)
7572 {
7573 stmtblock_t block;
7574 gfc_se se;
7575 tree len, message;
7576 bool fatal = code->ext.omp_clauses->severity == OMP_SEVERITY_FATAL;
7577 tree fndecl = builtin_decl_explicit (fatal ? BUILT_IN_GOMP_ERROR
7578 : BUILT_IN_GOMP_WARNING);
7579 gfc_start_block (&block);
7580 gfc_init_se (&se, NULL );
7581 if (!code->ext.omp_clauses->message)
7582 {
7583 message = null_pointer_node;
7584 len = build_int_cst (size_type_node, 0);
7585 }
7586 else
7587 {
7588 gfc_conv_expr (&se, code->ext.omp_clauses->message);
7589 message = se.expr;
7590 if (!POINTER_TYPE_P (TREE_TYPE (message)))
7591 /* To ensure an ARRAY_TYPE is not passed as such. */
7592 message = gfc_build_addr_expr (NULL, message);
7593 len = se.string_length;
7594 }
7595 gfc_add_block_to_block (&block, &se.pre);
7596 gfc_add_expr_to_block (&block, build_call_expr_loc (input_location, fndecl,
7597 2, message, len));
7598 gfc_add_block_to_block (&block, &se.post);
7599 return gfc_finish_block (&block);
7600 }
7601
7602 static tree
7603 gfc_trans_omp_flush (gfc_code *code)
7604 {
7605 tree call;
7606 if (!code->ext.omp_clauses
7607 || code->ext.omp_clauses->memorder == OMP_MEMORDER_UNSET
7608 || code->ext.omp_clauses->memorder == OMP_MEMORDER_SEQ_CST)
7609 {
7610 call = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
7611 call = build_call_expr_loc (input_location, call, 0);
7612 }
7613 else
7614 {
7615 enum memmodel mo = MEMMODEL_LAST;
7616 switch (code->ext.omp_clauses->memorder)
7617 {
7618 case OMP_MEMORDER_ACQ_REL: mo = MEMMODEL_ACQ_REL; break;
7619 case OMP_MEMORDER_RELEASE: mo = MEMMODEL_RELEASE; break;
7620 case OMP_MEMORDER_ACQUIRE: mo = MEMMODEL_ACQUIRE; break;
7621 default: gcc_unreachable (); break;
7622 }
7623 call = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
7624 call = build_call_expr_loc (input_location, call, 1,
7625 build_int_cst (integer_type_node, mo));
7626 }
7627 return call;
7628 }
7629
7630 static tree
7631 gfc_trans_omp_master (gfc_code *code)
7632 {
7633 tree stmt = gfc_trans_code (code->block->next);
7634 if (IS_EMPTY_STMT (stmt))
7635 return stmt;
7636 return build1_v (OMP_MASTER, stmt);
7637 }
7638
7639 static tree
7640 gfc_trans_omp_masked (gfc_code *code, gfc_omp_clauses *clauses)
7641 {
7642 stmtblock_t block;
7643 tree body = gfc_trans_code (code->block->next);
7644 if (IS_EMPTY_STMT (body))
7645 return body;
7646 if (!clauses)
7647 clauses = code->ext.omp_clauses;
7648 gfc_start_block (&block);
7649 tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
7650 tree stmt = make_node (OMP_MASKED);
7651 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
7652 TREE_TYPE (stmt) = void_type_node;
7653 OMP_MASKED_BODY (stmt) = body;
7654 OMP_MASKED_CLAUSES (stmt) = omp_clauses;
7655 gfc_add_expr_to_block (&block, stmt);
7656 return gfc_finish_block (&block);
7657 }
7658
7659
7660 static tree
7661 gfc_trans_omp_ordered (gfc_code *code)
7662 {
7663 if (!flag_openmp)
7664 {
7665 if (!code->ext.omp_clauses->simd)
7666 return gfc_trans_code (code->block ? code->block->next : NULL);
7667 code->ext.omp_clauses->threads = 0;
7668 }
7669 tree omp_clauses = gfc_trans_omp_clauses (NULL, code->ext.omp_clauses,
7670 code->loc);
7671 return build2_loc (input_location, OMP_ORDERED, void_type_node,
7672 code->block ? gfc_trans_code (code->block->next)
7673 : NULL_TREE, omp_clauses);
7674 }
7675
7676 static tree
7677 gfc_trans_omp_parallel (gfc_code *code)
7678 {
7679 stmtblock_t block;
7680 tree stmt, omp_clauses;
7681
7682 gfc_start_block (&block);
7683 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
7684 code->loc);
7685 pushlevel ();
7686 stmt = gfc_trans_omp_code (code->block->next, true);
7687 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
7688 stmt = build2_loc (input_location, OMP_PARALLEL, void_type_node, stmt,
7689 omp_clauses);
7690 gfc_add_expr_to_block (&block, stmt);
7691 return gfc_finish_block (&block);
7692 }
7693
7694 enum
7695 {
7696 GFC_OMP_SPLIT_SIMD,
7697 GFC_OMP_SPLIT_DO,
7698 GFC_OMP_SPLIT_PARALLEL,
7699 GFC_OMP_SPLIT_DISTRIBUTE,
7700 GFC_OMP_SPLIT_TEAMS,
7701 GFC_OMP_SPLIT_TARGET,
7702 GFC_OMP_SPLIT_TASKLOOP,
7703 GFC_OMP_SPLIT_MASKED,
7704 GFC_OMP_SPLIT_NUM
7705 };
7706
7707 enum
7708 {
7709 GFC_OMP_MASK_SIMD = (1 << GFC_OMP_SPLIT_SIMD),
7710 GFC_OMP_MASK_DO = (1 << GFC_OMP_SPLIT_DO),
7711 GFC_OMP_MASK_PARALLEL = (1 << GFC_OMP_SPLIT_PARALLEL),
7712 GFC_OMP_MASK_DISTRIBUTE = (1 << GFC_OMP_SPLIT_DISTRIBUTE),
7713 GFC_OMP_MASK_TEAMS = (1 << GFC_OMP_SPLIT_TEAMS),
7714 GFC_OMP_MASK_TARGET = (1 << GFC_OMP_SPLIT_TARGET),
7715 GFC_OMP_MASK_TASKLOOP = (1 << GFC_OMP_SPLIT_TASKLOOP),
7716 GFC_OMP_MASK_MASKED = (1 << GFC_OMP_SPLIT_MASKED)
7717 };
7718
7719 /* If a var is in lastprivate/firstprivate/reduction but not in a
7720 data mapping/sharing clause, add it to 'map(tofrom:' if is_target
7721 and to 'shared' otherwise. */
7722 static void
7723 gfc_add_clause_implicitly (gfc_omp_clauses *clauses_out,
7724 gfc_omp_clauses *clauses_in,
7725 bool is_target, bool is_parallel_do)
7726 {
7727 int clauselist_to_add = is_target ? OMP_LIST_MAP : OMP_LIST_SHARED;
7728 gfc_omp_namelist *tail = NULL;
7729 for (int i = 0; i < 5; ++i)
7730 {
7731 gfc_omp_namelist *n;
7732 switch (i)
7733 {
7734 case 0: n = clauses_in->lists[OMP_LIST_FIRSTPRIVATE]; break;
7735 case 1: n = clauses_in->lists[OMP_LIST_LASTPRIVATE]; break;
7736 case 2: n = clauses_in->lists[OMP_LIST_REDUCTION]; break;
7737 case 3: n = clauses_in->lists[OMP_LIST_REDUCTION_INSCAN]; break;
7738 case 4: n = clauses_in->lists[OMP_LIST_REDUCTION_TASK]; break;
7739 default: gcc_unreachable ();
7740 }
7741 for (; n != NULL; n = n->next)
7742 {
7743 gfc_omp_namelist *n2, **n_firstp = NULL, **n_lastp = NULL;
7744 for (int j = 0; j < 6; ++j)
7745 {
7746 gfc_omp_namelist **n2ref = NULL, *prev2 = NULL;
7747 switch (j)
7748 {
7749 case 0:
7750 n2ref = &clauses_out->lists[clauselist_to_add];
7751 break;
7752 case 1:
7753 n2ref = &clauses_out->lists[OMP_LIST_FIRSTPRIVATE];
7754 break;
7755 case 2:
7756 if (is_target)
7757 n2ref = &clauses_in->lists[OMP_LIST_LASTPRIVATE];
7758 else
7759 n2ref = &clauses_out->lists[OMP_LIST_LASTPRIVATE];
7760 break;
7761 case 3: n2ref = &clauses_out->lists[OMP_LIST_REDUCTION]; break;
7762 case 4:
7763 n2ref = &clauses_out->lists[OMP_LIST_REDUCTION_INSCAN];
7764 break;
7765 case 5:
7766 n2ref = &clauses_out->lists[OMP_LIST_REDUCTION_TASK];
7767 break;
7768 default: gcc_unreachable ();
7769 }
7770 for (n2 = *n2ref; n2 != NULL; prev2 = n2, n2 = n2->next)
7771 if (n2->sym == n->sym)
7772 break;
7773 if (n2)
7774 {
7775 if (j == 0 /* clauselist_to_add */)
7776 break; /* Already present. */
7777 if (j == 1 /* OMP_LIST_FIRSTPRIVATE */)
7778 {
7779 n_firstp = prev2 ? &prev2->next : n2ref;
7780 continue;
7781 }
7782 if (j == 2 /* OMP_LIST_LASTPRIVATE */)
7783 {
7784 n_lastp = prev2 ? &prev2->next : n2ref;
7785 continue;
7786 }
7787 break;
7788 }
7789 }
7790 if (n_firstp && n_lastp)
7791 {
7792 /* For parallel do, GCC puts firstprivate/lastprivate
7793 on the parallel. */
7794 if (is_parallel_do)
7795 continue;
7796 *n_firstp = (*n_firstp)->next;
7797 if (!is_target)
7798 *n_lastp = (*n_lastp)->next;
7799 }
7800 else if (is_target && n_lastp)
7801 ;
7802 else if (n2 || n_firstp || n_lastp)
7803 continue;
7804 if (clauses_out->lists[clauselist_to_add]
7805 && (clauses_out->lists[clauselist_to_add]
7806 == clauses_in->lists[clauselist_to_add]))
7807 {
7808 gfc_omp_namelist *p = NULL;
7809 for (n2 = clauses_in->lists[clauselist_to_add]; n2; n2 = n2->next)
7810 {
7811 if (p)
7812 {
7813 p->next = gfc_get_omp_namelist ();
7814 p = p->next;
7815 }
7816 else
7817 {
7818 p = gfc_get_omp_namelist ();
7819 clauses_out->lists[clauselist_to_add] = p;
7820 }
7821 *p = *n2;
7822 }
7823 }
7824 if (!tail)
7825 {
7826 tail = clauses_out->lists[clauselist_to_add];
7827 for (; tail && tail->next; tail = tail->next)
7828 ;
7829 }
7830 n2 = gfc_get_omp_namelist ();
7831 n2->where = n->where;
7832 n2->sym = n->sym;
7833 if (is_target)
7834 n2->u.map.op = OMP_MAP_TOFROM;
7835 if (tail)
7836 {
7837 tail->next = n2;
7838 tail = n2;
7839 }
7840 else
7841 clauses_out->lists[clauselist_to_add] = n2;
7842 }
7843 }
7844 }
7845
7846 /* Kind of opposite to above, add firstprivate to CLAUSES_OUT if it is mapped
7847 in CLAUSES_IN's FIRSTPRIVATE list but not its MAP list. */
7848
7849 static void
7850 gfc_add_firstprivate_if_unmapped (gfc_omp_clauses *clauses_out,
7851 gfc_omp_clauses *clauses_in)
7852 {
7853 gfc_omp_namelist *n = clauses_in->lists[OMP_LIST_FIRSTPRIVATE];
7854 gfc_omp_namelist **tail = NULL;
7855
7856 for (; n != NULL; n = n->next)
7857 {
7858 gfc_omp_namelist *n2 = clauses_out->lists[OMP_LIST_MAP];
7859 for (; n2 != NULL; n2 = n2->next)
7860 if (n->sym == n2->sym)
7861 break;
7862 if (n2 == NULL)
7863 {
7864 gfc_omp_namelist *dup = gfc_get_omp_namelist ();
7865 *dup = *n;
7866 dup->next = NULL;
7867 if (!tail)
7868 {
7869 tail = &clauses_out->lists[OMP_LIST_FIRSTPRIVATE];
7870 while (*tail && (*tail)->next)
7871 tail = &(*tail)->next;
7872 }
7873 *tail = dup;
7874 tail = &(*tail)->next;
7875 }
7876 }
7877 }
7878
7879 static void
7880 gfc_free_split_omp_clauses (gfc_code *code, gfc_omp_clauses *clausesa)
7881 {
7882 for (int i = 0; i < GFC_OMP_SPLIT_NUM; ++i)
7883 for (int j = 0; j < OMP_LIST_NUM; ++j)
7884 if (clausesa[i].lists[j] && clausesa[i].lists[j] != code->ext.omp_clauses->lists[j])
7885 for (gfc_omp_namelist *n = clausesa[i].lists[j]; n;)
7886 {
7887 gfc_omp_namelist *p = n;
7888 n = n->next;
7889 free (p);
7890 }
7891 }
7892
7893 static void
7894 gfc_split_omp_clauses (gfc_code *code,
7895 gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM])
7896 {
7897 int mask = 0, innermost = 0;
7898 bool is_loop = false;
7899 memset (clausesa, 0, GFC_OMP_SPLIT_NUM * sizeof (gfc_omp_clauses));
7900 switch (code->op)
7901 {
7902 case EXEC_OMP_DISTRIBUTE:
7903 innermost = GFC_OMP_SPLIT_DISTRIBUTE;
7904 break;
7905 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
7906 mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
7907 innermost = GFC_OMP_SPLIT_DO;
7908 break;
7909 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
7910 mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_PARALLEL
7911 | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
7912 innermost = GFC_OMP_SPLIT_SIMD;
7913 break;
7914 case EXEC_OMP_DISTRIBUTE_SIMD:
7915 mask = GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
7916 innermost = GFC_OMP_SPLIT_SIMD;
7917 break;
7918 case EXEC_OMP_DO:
7919 case EXEC_OMP_LOOP:
7920 innermost = GFC_OMP_SPLIT_DO;
7921 break;
7922 case EXEC_OMP_DO_SIMD:
7923 mask = GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
7924 innermost = GFC_OMP_SPLIT_SIMD;
7925 break;
7926 case EXEC_OMP_PARALLEL:
7927 innermost = GFC_OMP_SPLIT_PARALLEL;
7928 break;
7929 case EXEC_OMP_PARALLEL_DO:
7930 case EXEC_OMP_PARALLEL_LOOP:
7931 mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
7932 innermost = GFC_OMP_SPLIT_DO;
7933 break;
7934 case EXEC_OMP_PARALLEL_DO_SIMD:
7935 mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
7936 innermost = GFC_OMP_SPLIT_SIMD;
7937 break;
7938 case EXEC_OMP_PARALLEL_MASKED:
7939 mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED;
7940 innermost = GFC_OMP_SPLIT_MASKED;
7941 break;
7942 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
7943 mask = (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED
7944 | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD);
7945 innermost = GFC_OMP_SPLIT_TASKLOOP;
7946 break;
7947 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
7948 mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
7949 innermost = GFC_OMP_SPLIT_TASKLOOP;
7950 break;
7951 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
7952 mask = (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_MASKED
7953 | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD);
7954 innermost = GFC_OMP_SPLIT_SIMD;
7955 break;
7956 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
7957 mask = GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
7958 innermost = GFC_OMP_SPLIT_SIMD;
7959 break;
7960 case EXEC_OMP_SIMD:
7961 innermost = GFC_OMP_SPLIT_SIMD;
7962 break;
7963 case EXEC_OMP_TARGET:
7964 innermost = GFC_OMP_SPLIT_TARGET;
7965 break;
7966 case EXEC_OMP_TARGET_PARALLEL:
7967 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL;
7968 innermost = GFC_OMP_SPLIT_PARALLEL;
7969 break;
7970 case EXEC_OMP_TARGET_PARALLEL_DO:
7971 case EXEC_OMP_TARGET_PARALLEL_LOOP:
7972 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
7973 innermost = GFC_OMP_SPLIT_DO;
7974 break;
7975 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
7976 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO
7977 | GFC_OMP_MASK_SIMD;
7978 innermost = GFC_OMP_SPLIT_SIMD;
7979 break;
7980 case EXEC_OMP_TARGET_SIMD:
7981 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_SIMD;
7982 innermost = GFC_OMP_SPLIT_SIMD;
7983 break;
7984 case EXEC_OMP_TARGET_TEAMS:
7985 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS;
7986 innermost = GFC_OMP_SPLIT_TEAMS;
7987 break;
7988 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
7989 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS
7990 | GFC_OMP_MASK_DISTRIBUTE;
7991 innermost = GFC_OMP_SPLIT_DISTRIBUTE;
7992 break;
7993 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
7994 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
7995 | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
7996 innermost = GFC_OMP_SPLIT_DO;
7997 break;
7998 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
7999 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
8000 | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
8001 innermost = GFC_OMP_SPLIT_SIMD;
8002 break;
8003 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
8004 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS
8005 | GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
8006 innermost = GFC_OMP_SPLIT_SIMD;
8007 break;
8008 case EXEC_OMP_TARGET_TEAMS_LOOP:
8009 mask = GFC_OMP_MASK_TARGET | GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DO;
8010 innermost = GFC_OMP_SPLIT_DO;
8011 break;
8012 case EXEC_OMP_MASKED_TASKLOOP:
8013 mask = GFC_OMP_MASK_MASKED | GFC_OMP_MASK_TASKLOOP;
8014 innermost = GFC_OMP_SPLIT_TASKLOOP;
8015 break;
8016 case EXEC_OMP_MASTER_TASKLOOP:
8017 case EXEC_OMP_TASKLOOP:
8018 innermost = GFC_OMP_SPLIT_TASKLOOP;
8019 break;
8020 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
8021 mask = GFC_OMP_MASK_MASKED | GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
8022 innermost = GFC_OMP_SPLIT_SIMD;
8023 break;
8024 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
8025 case EXEC_OMP_TASKLOOP_SIMD:
8026 mask = GFC_OMP_MASK_TASKLOOP | GFC_OMP_MASK_SIMD;
8027 innermost = GFC_OMP_SPLIT_SIMD;
8028 break;
8029 case EXEC_OMP_TEAMS:
8030 innermost = GFC_OMP_SPLIT_TEAMS;
8031 break;
8032 case EXEC_OMP_TEAMS_DISTRIBUTE:
8033 mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE;
8034 innermost = GFC_OMP_SPLIT_DISTRIBUTE;
8035 break;
8036 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
8037 mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
8038 | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO;
8039 innermost = GFC_OMP_SPLIT_DO;
8040 break;
8041 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
8042 mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE
8043 | GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO | GFC_OMP_MASK_SIMD;
8044 innermost = GFC_OMP_SPLIT_SIMD;
8045 break;
8046 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
8047 mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DISTRIBUTE | GFC_OMP_MASK_SIMD;
8048 innermost = GFC_OMP_SPLIT_SIMD;
8049 break;
8050 case EXEC_OMP_TEAMS_LOOP:
8051 mask = GFC_OMP_MASK_TEAMS | GFC_OMP_MASK_DO;
8052 innermost = GFC_OMP_SPLIT_DO;
8053 break;
8054 default:
8055 gcc_unreachable ();
8056 }
8057 if (mask == 0)
8058 {
8059 clausesa[innermost] = *code->ext.omp_clauses;
8060 return;
8061 }
8062 /* Loops are similar to DO but still a bit different. */
8063 switch (code->op)
8064 {
8065 case EXEC_OMP_LOOP:
8066 case EXEC_OMP_PARALLEL_LOOP:
8067 case EXEC_OMP_TEAMS_LOOP:
8068 case EXEC_OMP_TARGET_PARALLEL_LOOP:
8069 case EXEC_OMP_TARGET_TEAMS_LOOP:
8070 is_loop = true;
8071 default:
8072 break;
8073 }
8074 if (code->ext.omp_clauses != NULL)
8075 {
8076 if (mask & GFC_OMP_MASK_TARGET)
8077 {
8078 /* First the clauses that are unique to some constructs. */
8079 clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_MAP]
8080 = code->ext.omp_clauses->lists[OMP_LIST_MAP];
8081 clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_IS_DEVICE_PTR]
8082 = code->ext.omp_clauses->lists[OMP_LIST_IS_DEVICE_PTR];
8083 clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_HAS_DEVICE_ADDR]
8084 = code->ext.omp_clauses->lists[OMP_LIST_HAS_DEVICE_ADDR];
8085 clausesa[GFC_OMP_SPLIT_TARGET].device
8086 = code->ext.omp_clauses->device;
8087 clausesa[GFC_OMP_SPLIT_TARGET].thread_limit
8088 = code->ext.omp_clauses->thread_limit;
8089 clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_USES_ALLOCATORS]
8090 = code->ext.omp_clauses->lists[OMP_LIST_USES_ALLOCATORS];
8091 for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; i++)
8092 clausesa[GFC_OMP_SPLIT_TARGET].defaultmap[i]
8093 = code->ext.omp_clauses->defaultmap[i];
8094 clausesa[GFC_OMP_SPLIT_TARGET].if_exprs[OMP_IF_TARGET]
8095 = code->ext.omp_clauses->if_exprs[OMP_IF_TARGET];
8096 /* And this is copied to all. */
8097 clausesa[GFC_OMP_SPLIT_TARGET].if_expr
8098 = code->ext.omp_clauses->if_expr;
8099 clausesa[GFC_OMP_SPLIT_TARGET].nowait
8100 = code->ext.omp_clauses->nowait;
8101 }
8102 if (mask & GFC_OMP_MASK_TEAMS)
8103 {
8104 /* First the clauses that are unique to some constructs. */
8105 clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_lower
8106 = code->ext.omp_clauses->num_teams_lower;
8107 clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_upper
8108 = code->ext.omp_clauses->num_teams_upper;
8109 clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit
8110 = code->ext.omp_clauses->thread_limit;
8111 /* Shared and default clauses are allowed on parallel, teams
8112 and taskloop. */
8113 clausesa[GFC_OMP_SPLIT_TEAMS].lists[OMP_LIST_SHARED]
8114 = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
8115 clausesa[GFC_OMP_SPLIT_TEAMS].default_sharing
8116 = code->ext.omp_clauses->default_sharing;
8117 }
8118 if (mask & GFC_OMP_MASK_DISTRIBUTE)
8119 {
8120 /* First the clauses that are unique to some constructs. */
8121 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].dist_sched_kind
8122 = code->ext.omp_clauses->dist_sched_kind;
8123 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].dist_chunk_size
8124 = code->ext.omp_clauses->dist_chunk_size;
8125 /* Duplicate collapse. */
8126 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].collapse
8127 = code->ext.omp_clauses->collapse;
8128 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_concurrent
8129 = code->ext.omp_clauses->order_concurrent;
8130 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_unconstrained
8131 = code->ext.omp_clauses->order_unconstrained;
8132 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_reproducible
8133 = code->ext.omp_clauses->order_reproducible;
8134 }
8135 if (mask & GFC_OMP_MASK_PARALLEL)
8136 {
8137 /* First the clauses that are unique to some constructs. */
8138 clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_COPYIN]
8139 = code->ext.omp_clauses->lists[OMP_LIST_COPYIN];
8140 clausesa[GFC_OMP_SPLIT_PARALLEL].num_threads
8141 = code->ext.omp_clauses->num_threads;
8142 clausesa[GFC_OMP_SPLIT_PARALLEL].proc_bind
8143 = code->ext.omp_clauses->proc_bind;
8144 /* Shared and default clauses are allowed on parallel, teams
8145 and taskloop. */
8146 clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_SHARED]
8147 = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
8148 clausesa[GFC_OMP_SPLIT_PARALLEL].default_sharing
8149 = code->ext.omp_clauses->default_sharing;
8150 clausesa[GFC_OMP_SPLIT_PARALLEL].if_exprs[OMP_IF_PARALLEL]
8151 = code->ext.omp_clauses->if_exprs[OMP_IF_PARALLEL];
8152 /* And this is copied to all. */
8153 clausesa[GFC_OMP_SPLIT_PARALLEL].if_expr
8154 = code->ext.omp_clauses->if_expr;
8155 }
8156 if (mask & GFC_OMP_MASK_MASKED)
8157 clausesa[GFC_OMP_SPLIT_MASKED].filter = code->ext.omp_clauses->filter;
8158 if ((mask & GFC_OMP_MASK_DO) && !is_loop)
8159 {
8160 /* First the clauses that are unique to some constructs. */
8161 clausesa[GFC_OMP_SPLIT_DO].ordered
8162 = code->ext.omp_clauses->ordered;
8163 clausesa[GFC_OMP_SPLIT_DO].orderedc
8164 = code->ext.omp_clauses->orderedc;
8165 clausesa[GFC_OMP_SPLIT_DO].sched_kind
8166 = code->ext.omp_clauses->sched_kind;
8167 if (innermost == GFC_OMP_SPLIT_SIMD)
8168 clausesa[GFC_OMP_SPLIT_DO].sched_simd
8169 = code->ext.omp_clauses->sched_simd;
8170 clausesa[GFC_OMP_SPLIT_DO].sched_monotonic
8171 = code->ext.omp_clauses->sched_monotonic;
8172 clausesa[GFC_OMP_SPLIT_DO].sched_nonmonotonic
8173 = code->ext.omp_clauses->sched_nonmonotonic;
8174 clausesa[GFC_OMP_SPLIT_DO].chunk_size
8175 = code->ext.omp_clauses->chunk_size;
8176 clausesa[GFC_OMP_SPLIT_DO].nowait
8177 = code->ext.omp_clauses->nowait;
8178 }
8179 if (mask & GFC_OMP_MASK_DO)
8180 {
8181 clausesa[GFC_OMP_SPLIT_DO].bind
8182 = code->ext.omp_clauses->bind;
8183 /* Duplicate collapse. */
8184 clausesa[GFC_OMP_SPLIT_DO].collapse
8185 = code->ext.omp_clauses->collapse;
8186 clausesa[GFC_OMP_SPLIT_DO].order_concurrent
8187 = code->ext.omp_clauses->order_concurrent;
8188 clausesa[GFC_OMP_SPLIT_DO].order_unconstrained
8189 = code->ext.omp_clauses->order_unconstrained;
8190 clausesa[GFC_OMP_SPLIT_DO].order_reproducible
8191 = code->ext.omp_clauses->order_reproducible;
8192 }
8193 if (mask & GFC_OMP_MASK_SIMD)
8194 {
8195 clausesa[GFC_OMP_SPLIT_SIMD].safelen_expr
8196 = code->ext.omp_clauses->safelen_expr;
8197 clausesa[GFC_OMP_SPLIT_SIMD].simdlen_expr
8198 = code->ext.omp_clauses->simdlen_expr;
8199 clausesa[GFC_OMP_SPLIT_SIMD].lists[OMP_LIST_ALIGNED]
8200 = code->ext.omp_clauses->lists[OMP_LIST_ALIGNED];
8201 /* Duplicate collapse. */
8202 clausesa[GFC_OMP_SPLIT_SIMD].collapse
8203 = code->ext.omp_clauses->collapse;
8204 clausesa[GFC_OMP_SPLIT_SIMD].if_exprs[OMP_IF_SIMD]
8205 = code->ext.omp_clauses->if_exprs[OMP_IF_SIMD];
8206 clausesa[GFC_OMP_SPLIT_SIMD].order_concurrent
8207 = code->ext.omp_clauses->order_concurrent;
8208 clausesa[GFC_OMP_SPLIT_SIMD].order_unconstrained
8209 = code->ext.omp_clauses->order_unconstrained;
8210 clausesa[GFC_OMP_SPLIT_SIMD].order_reproducible
8211 = code->ext.omp_clauses->order_reproducible;
8212 /* And this is copied to all. */
8213 clausesa[GFC_OMP_SPLIT_SIMD].if_expr
8214 = code->ext.omp_clauses->if_expr;
8215 }
8216 if (mask & GFC_OMP_MASK_TASKLOOP)
8217 {
8218 /* First the clauses that are unique to some constructs. */
8219 clausesa[GFC_OMP_SPLIT_TASKLOOP].nogroup
8220 = code->ext.omp_clauses->nogroup;
8221 clausesa[GFC_OMP_SPLIT_TASKLOOP].grainsize
8222 = code->ext.omp_clauses->grainsize;
8223 clausesa[GFC_OMP_SPLIT_TASKLOOP].grainsize_strict
8224 = code->ext.omp_clauses->grainsize_strict;
8225 clausesa[GFC_OMP_SPLIT_TASKLOOP].num_tasks
8226 = code->ext.omp_clauses->num_tasks;
8227 clausesa[GFC_OMP_SPLIT_TASKLOOP].num_tasks_strict
8228 = code->ext.omp_clauses->num_tasks_strict;
8229 clausesa[GFC_OMP_SPLIT_TASKLOOP].priority
8230 = code->ext.omp_clauses->priority;
8231 clausesa[GFC_OMP_SPLIT_TASKLOOP].final_expr
8232 = code->ext.omp_clauses->final_expr;
8233 clausesa[GFC_OMP_SPLIT_TASKLOOP].untied
8234 = code->ext.omp_clauses->untied;
8235 clausesa[GFC_OMP_SPLIT_TASKLOOP].mergeable
8236 = code->ext.omp_clauses->mergeable;
8237 clausesa[GFC_OMP_SPLIT_TASKLOOP].if_exprs[OMP_IF_TASKLOOP]
8238 = code->ext.omp_clauses->if_exprs[OMP_IF_TASKLOOP];
8239 /* And this is copied to all. */
8240 clausesa[GFC_OMP_SPLIT_TASKLOOP].if_expr
8241 = code->ext.omp_clauses->if_expr;
8242 /* Shared and default clauses are allowed on parallel, teams
8243 and taskloop. */
8244 clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_SHARED]
8245 = code->ext.omp_clauses->lists[OMP_LIST_SHARED];
8246 clausesa[GFC_OMP_SPLIT_TASKLOOP].default_sharing
8247 = code->ext.omp_clauses->default_sharing;
8248 /* Duplicate collapse. */
8249 clausesa[GFC_OMP_SPLIT_TASKLOOP].collapse
8250 = code->ext.omp_clauses->collapse;
8251 }
8252 /* Private clause is supported on all constructs but master/masked,
8253 it is enough to put it on the innermost one except for master/masked. For
8254 !$ omp parallel do put it on parallel though,
8255 as that's what we did for OpenMP 3.1. */
8256 clausesa[((innermost == GFC_OMP_SPLIT_DO && !is_loop)
8257 || code->op == EXEC_OMP_PARALLEL_MASTER
8258 || code->op == EXEC_OMP_PARALLEL_MASKED)
8259 ? (int) GFC_OMP_SPLIT_PARALLEL
8260 : innermost].lists[OMP_LIST_PRIVATE]
8261 = code->ext.omp_clauses->lists[OMP_LIST_PRIVATE];
8262 /* Firstprivate clause is supported on all constructs but
8263 simd and masked/master. Put it on the outermost of those and duplicate
8264 on parallel and teams. */
8265 if (mask & GFC_OMP_MASK_TARGET)
8266 gfc_add_firstprivate_if_unmapped (&clausesa[GFC_OMP_SPLIT_TARGET],
8267 code->ext.omp_clauses);
8268 if (mask & GFC_OMP_MASK_TEAMS)
8269 clausesa[GFC_OMP_SPLIT_TEAMS].lists[OMP_LIST_FIRSTPRIVATE]
8270 = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
8271 else if (mask & GFC_OMP_MASK_DISTRIBUTE)
8272 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].lists[OMP_LIST_FIRSTPRIVATE]
8273 = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
8274 if (mask & GFC_OMP_MASK_TASKLOOP)
8275 clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_FIRSTPRIVATE]
8276 = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
8277 if ((mask & GFC_OMP_MASK_PARALLEL)
8278 && !(mask & GFC_OMP_MASK_TASKLOOP))
8279 clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_FIRSTPRIVATE]
8280 = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
8281 else if ((mask & GFC_OMP_MASK_DO) && !is_loop)
8282 clausesa[GFC_OMP_SPLIT_DO].lists[OMP_LIST_FIRSTPRIVATE]
8283 = code->ext.omp_clauses->lists[OMP_LIST_FIRSTPRIVATE];
8284 /* Lastprivate is allowed on distribute, do, simd, taskloop and loop.
8285 In parallel do{, simd} we actually want to put it on
8286 parallel rather than do. */
8287 if (mask & GFC_OMP_MASK_DISTRIBUTE)
8288 clausesa[GFC_OMP_SPLIT_DISTRIBUTE].lists[OMP_LIST_LASTPRIVATE]
8289 = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
8290 if (mask & GFC_OMP_MASK_TASKLOOP)
8291 clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_LASTPRIVATE]
8292 = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
8293 if ((mask & GFC_OMP_MASK_PARALLEL) && !is_loop
8294 && !(mask & GFC_OMP_MASK_TASKLOOP))
8295 clausesa[GFC_OMP_SPLIT_PARALLEL].lists[OMP_LIST_LASTPRIVATE]
8296 = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
8297 else if (mask & GFC_OMP_MASK_DO)
8298 clausesa[GFC_OMP_SPLIT_DO].lists[OMP_LIST_LASTPRIVATE]
8299 = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
8300 if (mask & GFC_OMP_MASK_SIMD)
8301 clausesa[GFC_OMP_SPLIT_SIMD].lists[OMP_LIST_LASTPRIVATE]
8302 = code->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE];
8303 /* Reduction is allowed on simd, do, parallel, teams, taskloop, and loop.
8304 Duplicate it on all of them, but
8305 - omit on do if parallel is present;
8306 - omit on task and parallel if loop is present;
8307 additionally, inscan applies to do/simd only. */
8308 for (int i = OMP_LIST_REDUCTION; i <= OMP_LIST_REDUCTION_TASK; i++)
8309 {
8310 if (mask & GFC_OMP_MASK_TASKLOOP
8311 && i != OMP_LIST_REDUCTION_INSCAN)
8312 clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[i]
8313 = code->ext.omp_clauses->lists[i];
8314 if (mask & GFC_OMP_MASK_TEAMS
8315 && i != OMP_LIST_REDUCTION_INSCAN
8316 && !is_loop)
8317 clausesa[GFC_OMP_SPLIT_TEAMS].lists[i]
8318 = code->ext.omp_clauses->lists[i];
8319 if (mask & GFC_OMP_MASK_PARALLEL
8320 && i != OMP_LIST_REDUCTION_INSCAN
8321 && !(mask & GFC_OMP_MASK_TASKLOOP)
8322 && !is_loop)
8323 clausesa[GFC_OMP_SPLIT_PARALLEL].lists[i]
8324 = code->ext.omp_clauses->lists[i];
8325 else if (mask & GFC_OMP_MASK_DO)
8326 clausesa[GFC_OMP_SPLIT_DO].lists[i]
8327 = code->ext.omp_clauses->lists[i];
8328 if (mask & GFC_OMP_MASK_SIMD)
8329 clausesa[GFC_OMP_SPLIT_SIMD].lists[i]
8330 = code->ext.omp_clauses->lists[i];
8331 }
8332 if (mask & GFC_OMP_MASK_TARGET)
8333 clausesa[GFC_OMP_SPLIT_TARGET].lists[OMP_LIST_IN_REDUCTION]
8334 = code->ext.omp_clauses->lists[OMP_LIST_IN_REDUCTION];
8335 if (mask & GFC_OMP_MASK_TASKLOOP)
8336 clausesa[GFC_OMP_SPLIT_TASKLOOP].lists[OMP_LIST_IN_REDUCTION]
8337 = code->ext.omp_clauses->lists[OMP_LIST_IN_REDUCTION];
8338 /* Linear clause is supported on do and simd,
8339 put it on the innermost one. */
8340 clausesa[innermost].lists[OMP_LIST_LINEAR]
8341 = code->ext.omp_clauses->lists[OMP_LIST_LINEAR];
8342 }
8343 /* Propagate firstprivate/lastprivate/reduction vars to
8344 shared (parallel, teams) and map-tofrom (target). */
8345 if (mask & GFC_OMP_MASK_TARGET)
8346 gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_TARGET],
8347 code->ext.omp_clauses, true, false);
8348 if ((mask & GFC_OMP_MASK_PARALLEL) && innermost != GFC_OMP_MASK_PARALLEL)
8349 gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_PARALLEL],
8350 code->ext.omp_clauses, false,
8351 mask & GFC_OMP_MASK_DO);
8352 if (mask & GFC_OMP_MASK_TEAMS && innermost != GFC_OMP_MASK_TEAMS)
8353 gfc_add_clause_implicitly (&clausesa[GFC_OMP_SPLIT_TEAMS],
8354 code->ext.omp_clauses, false, false);
8355 if (((mask & (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO))
8356 == (GFC_OMP_MASK_PARALLEL | GFC_OMP_MASK_DO))
8357 && !is_loop)
8358 clausesa[GFC_OMP_SPLIT_DO].nowait = true;
8359
8360 /* Distribute allocate clause to do, parallel, distribute, teams, target
8361 and taskloop. The code below iterates over variables in the
8362 allocate list and checks if that available is also in any
8363 privatization clause on those construct. If yes, then we add it
8364 to the list of 'allocate'ed variables for that construct. If a
8365 variable is found in none of them then we issue an error. */
8366
8367 if (code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE])
8368 {
8369 gfc_omp_namelist *alloc_nl, *priv_nl;
8370 gfc_omp_namelist *tails[GFC_OMP_SPLIT_NUM];
8371 for (alloc_nl = code->ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
8372 alloc_nl; alloc_nl = alloc_nl->next)
8373 {
8374 bool found = false;
8375 for (int i = GFC_OMP_SPLIT_DO; i <= GFC_OMP_SPLIT_TASKLOOP; i++)
8376 {
8377 gfc_omp_namelist *p;
8378 int list;
8379 for (list = 0; list < OMP_LIST_NUM; list++)
8380 {
8381 switch (list)
8382 {
8383 case OMP_LIST_PRIVATE:
8384 case OMP_LIST_FIRSTPRIVATE:
8385 case OMP_LIST_LASTPRIVATE:
8386 case OMP_LIST_REDUCTION:
8387 case OMP_LIST_REDUCTION_INSCAN:
8388 case OMP_LIST_REDUCTION_TASK:
8389 case OMP_LIST_IN_REDUCTION:
8390 case OMP_LIST_TASK_REDUCTION:
8391 case OMP_LIST_LINEAR:
8392 for (priv_nl = clausesa[i].lists[list]; priv_nl;
8393 priv_nl = priv_nl->next)
8394 if (alloc_nl->sym == priv_nl->sym)
8395 {
8396 found = true;
8397 p = gfc_get_omp_namelist ();
8398 p->sym = alloc_nl->sym;
8399 p->expr = alloc_nl->expr;
8400 p->u.align = alloc_nl->u.align;
8401 p->u2.allocator = alloc_nl->u2.allocator;
8402 p->where = alloc_nl->where;
8403 if (clausesa[i].lists[OMP_LIST_ALLOCATE] == NULL)
8404 {
8405 clausesa[i].lists[OMP_LIST_ALLOCATE] = p;
8406 tails[i] = p;
8407 }
8408 else
8409 {
8410 tails[i]->next = p;
8411 tails[i] = tails[i]->next;
8412 }
8413 }
8414 break;
8415 default:
8416 break;
8417 }
8418 }
8419 }
8420 if (!found)
8421 gfc_error ("%qs specified in %<allocate%> clause at %L but not "
8422 "in an explicit privatization clause",
8423 alloc_nl->sym->name, &alloc_nl->where);
8424 }
8425 }
8426 }
8427
8428 static tree
8429 gfc_trans_omp_do_simd (gfc_code *code, stmtblock_t *pblock,
8430 gfc_omp_clauses *clausesa, tree omp_clauses)
8431 {
8432 stmtblock_t block;
8433 gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
8434 tree stmt, body, omp_do_clauses = NULL_TREE;
8435 bool free_clausesa = false;
8436
8437 if (pblock == NULL)
8438 gfc_start_block (&block);
8439 else
8440 gfc_init_block (&block);
8441
8442 if (clausesa == NULL)
8443 {
8444 clausesa = clausesa_buf;
8445 gfc_split_omp_clauses (code, clausesa);
8446 free_clausesa = true;
8447 }
8448 if (flag_openmp)
8449 omp_do_clauses
8450 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_DO], code->loc);
8451 body = gfc_trans_omp_do (code, EXEC_OMP_SIMD, pblock ? pblock : &block,
8452 &clausesa[GFC_OMP_SPLIT_SIMD], omp_clauses);
8453 if (pblock == NULL)
8454 {
8455 if (TREE_CODE (body) != BIND_EXPR)
8456 body = build3_v (BIND_EXPR, NULL, body, poplevel (1, 0));
8457 else
8458 poplevel (0, 0);
8459 }
8460 else if (TREE_CODE (body) != BIND_EXPR)
8461 body = build3_v (BIND_EXPR, NULL, body, NULL_TREE);
8462 if (flag_openmp)
8463 {
8464 stmt = make_node (OMP_FOR);
8465 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
8466 TREE_TYPE (stmt) = void_type_node;
8467 OMP_FOR_BODY (stmt) = body;
8468 OMP_FOR_CLAUSES (stmt) = omp_do_clauses;
8469 }
8470 else
8471 stmt = body;
8472 gfc_add_expr_to_block (&block, stmt);
8473 if (free_clausesa)
8474 gfc_free_split_omp_clauses (code, clausesa);
8475 return gfc_finish_block (&block);
8476 }
8477
8478 static tree
8479 gfc_trans_omp_parallel_do (gfc_code *code, bool is_loop, stmtblock_t *pblock,
8480 gfc_omp_clauses *clausesa)
8481 {
8482 stmtblock_t block, *new_pblock = pblock;
8483 gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
8484 tree stmt, omp_clauses = NULL_TREE;
8485 bool free_clausesa = false;
8486
8487 if (pblock == NULL)
8488 gfc_start_block (&block);
8489 else
8490 gfc_init_block (&block);
8491
8492 if (clausesa == NULL)
8493 {
8494 clausesa = clausesa_buf;
8495 gfc_split_omp_clauses (code, clausesa);
8496 free_clausesa = true;
8497 }
8498 omp_clauses
8499 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_PARALLEL],
8500 code->loc);
8501 if (pblock == NULL)
8502 {
8503 if (!clausesa[GFC_OMP_SPLIT_DO].ordered
8504 && clausesa[GFC_OMP_SPLIT_DO].sched_kind != OMP_SCHED_STATIC)
8505 new_pblock = &block;
8506 else
8507 pushlevel ();
8508 }
8509 stmt = gfc_trans_omp_do (code, is_loop ? EXEC_OMP_LOOP : EXEC_OMP_DO,
8510 new_pblock, &clausesa[GFC_OMP_SPLIT_DO],
8511 omp_clauses);
8512 if (pblock == NULL)
8513 {
8514 if (TREE_CODE (stmt) != BIND_EXPR)
8515 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8516 else
8517 poplevel (0, 0);
8518 }
8519 else if (TREE_CODE (stmt) != BIND_EXPR)
8520 stmt = build3_v (BIND_EXPR, NULL, stmt, NULL_TREE);
8521 stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
8522 void_type_node, stmt, omp_clauses);
8523 OMP_PARALLEL_COMBINED (stmt) = 1;
8524 gfc_add_expr_to_block (&block, stmt);
8525 if (free_clausesa)
8526 gfc_free_split_omp_clauses (code, clausesa);
8527 return gfc_finish_block (&block);
8528 }
8529
8530 static tree
8531 gfc_trans_omp_parallel_do_simd (gfc_code *code, stmtblock_t *pblock,
8532 gfc_omp_clauses *clausesa)
8533 {
8534 stmtblock_t block;
8535 gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
8536 tree stmt, omp_clauses = NULL_TREE;
8537 bool free_clausesa = false;
8538
8539 if (pblock == NULL)
8540 gfc_start_block (&block);
8541 else
8542 gfc_init_block (&block);
8543
8544 if (clausesa == NULL)
8545 {
8546 clausesa = clausesa_buf;
8547 gfc_split_omp_clauses (code, clausesa);
8548 free_clausesa = true;
8549 }
8550 if (flag_openmp)
8551 omp_clauses
8552 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_PARALLEL],
8553 code->loc);
8554 if (pblock == NULL)
8555 pushlevel ();
8556 stmt = gfc_trans_omp_do_simd (code, pblock, clausesa, omp_clauses);
8557 if (pblock == NULL)
8558 {
8559 if (TREE_CODE (stmt) != BIND_EXPR)
8560 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8561 else
8562 poplevel (0, 0);
8563 }
8564 else if (TREE_CODE (stmt) != BIND_EXPR)
8565 stmt = build3_v (BIND_EXPR, NULL, stmt, NULL_TREE);
8566 if (flag_openmp)
8567 {
8568 stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
8569 void_type_node, stmt, omp_clauses);
8570 OMP_PARALLEL_COMBINED (stmt) = 1;
8571 }
8572 gfc_add_expr_to_block (&block, stmt);
8573 if (free_clausesa)
8574 gfc_free_split_omp_clauses (code, clausesa);
8575 return gfc_finish_block (&block);
8576 }
8577
8578 static tree
8579 gfc_trans_omp_parallel_sections (gfc_code *code)
8580 {
8581 stmtblock_t block;
8582 gfc_omp_clauses section_clauses;
8583 tree stmt, omp_clauses;
8584
8585 memset (&section_clauses, 0, sizeof (section_clauses));
8586 section_clauses.nowait = true;
8587
8588 gfc_start_block (&block);
8589 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
8590 code->loc);
8591 pushlevel ();
8592 stmt = gfc_trans_omp_sections (code, &section_clauses);
8593 if (TREE_CODE (stmt) != BIND_EXPR)
8594 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8595 else
8596 poplevel (0, 0);
8597 stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
8598 void_type_node, stmt, omp_clauses);
8599 OMP_PARALLEL_COMBINED (stmt) = 1;
8600 gfc_add_expr_to_block (&block, stmt);
8601 return gfc_finish_block (&block);
8602 }
8603
8604 static tree
8605 gfc_trans_omp_parallel_workshare (gfc_code *code)
8606 {
8607 stmtblock_t block;
8608 gfc_omp_clauses workshare_clauses;
8609 tree stmt, omp_clauses;
8610
8611 memset (&workshare_clauses, 0, sizeof (workshare_clauses));
8612 workshare_clauses.nowait = true;
8613
8614 gfc_start_block (&block);
8615 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
8616 code->loc);
8617 pushlevel ();
8618 stmt = gfc_trans_omp_workshare (code, &workshare_clauses);
8619 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8620 stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
8621 void_type_node, stmt, omp_clauses);
8622 OMP_PARALLEL_COMBINED (stmt) = 1;
8623 gfc_add_expr_to_block (&block, stmt);
8624 return gfc_finish_block (&block);
8625 }
8626
8627 static tree
8628 gfc_trans_omp_scope (gfc_code *code)
8629 {
8630 stmtblock_t block;
8631 tree body = gfc_trans_code (code->block->next);
8632 if (IS_EMPTY_STMT (body))
8633 return body;
8634 gfc_start_block (&block);
8635 tree omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
8636 code->loc);
8637 tree stmt = make_node (OMP_SCOPE);
8638 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
8639 TREE_TYPE (stmt) = void_type_node;
8640 OMP_SCOPE_BODY (stmt) = body;
8641 OMP_SCOPE_CLAUSES (stmt) = omp_clauses;
8642 gfc_add_expr_to_block (&block, stmt);
8643 return gfc_finish_block (&block);
8644 }
8645
8646 static tree
8647 gfc_trans_omp_sections (gfc_code *code, gfc_omp_clauses *clauses)
8648 {
8649 stmtblock_t block, body;
8650 tree omp_clauses, stmt;
8651 bool has_lastprivate = clauses->lists[OMP_LIST_LASTPRIVATE] != NULL;
8652 location_t loc = gfc_get_location (&code->loc);
8653
8654 gfc_start_block (&block);
8655
8656 omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
8657
8658 gfc_init_block (&body);
8659 for (code = code->block; code; code = code->block)
8660 {
8661 /* Last section is special because of lastprivate, so even if it
8662 is empty, chain it in. */
8663 stmt = gfc_trans_omp_code (code->next,
8664 has_lastprivate && code->block == NULL);
8665 if (! IS_EMPTY_STMT (stmt))
8666 {
8667 stmt = build1_v (OMP_SECTION, stmt);
8668 gfc_add_expr_to_block (&body, stmt);
8669 }
8670 }
8671 stmt = gfc_finish_block (&body);
8672
8673 stmt = build2_loc (loc, OMP_SECTIONS, void_type_node, stmt, omp_clauses);
8674 gfc_add_expr_to_block (&block, stmt);
8675
8676 return gfc_finish_block (&block);
8677 }
8678
8679 static tree
8680 gfc_trans_omp_single (gfc_code *code, gfc_omp_clauses *clauses)
8681 {
8682 stmtblock_t block;
8683 gfc_start_block (&block);
8684 tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
8685 tree stmt = gfc_trans_omp_code (code->block->next, true);
8686 stmt = build2_loc (gfc_get_location (&code->loc), OMP_SINGLE, void_type_node,
8687 stmt, omp_clauses);
8688 gfc_add_expr_to_block (&block, stmt);
8689 return gfc_finish_block (&block);
8690 }
8691
8692 static tree
8693 gfc_trans_omp_task (gfc_code *code)
8694 {
8695 stmtblock_t block;
8696 tree stmt, omp_clauses;
8697
8698 gfc_start_block (&block);
8699 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
8700 code->loc);
8701 pushlevel ();
8702 stmt = gfc_trans_omp_code (code->block->next, true);
8703 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8704 stmt = build2_loc (gfc_get_location (&code->loc), OMP_TASK, void_type_node,
8705 stmt, omp_clauses);
8706 gfc_add_expr_to_block (&block, stmt);
8707 return gfc_finish_block (&block);
8708 }
8709
8710 static tree
8711 gfc_trans_omp_taskgroup (gfc_code *code)
8712 {
8713 stmtblock_t block;
8714 gfc_start_block (&block);
8715 tree body = gfc_trans_code (code->block->next);
8716 tree stmt = make_node (OMP_TASKGROUP);
8717 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
8718 TREE_TYPE (stmt) = void_type_node;
8719 OMP_TASKGROUP_BODY (stmt) = body;
8720 OMP_TASKGROUP_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
8721 code->ext.omp_clauses,
8722 code->loc);
8723 gfc_add_expr_to_block (&block, stmt);
8724 return gfc_finish_block (&block);
8725 }
8726
8727 static tree
8728 gfc_trans_omp_taskwait (gfc_code *code)
8729 {
8730 if (!code->ext.omp_clauses)
8731 {
8732 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8733 return build_call_expr_loc (input_location, decl, 0);
8734 }
8735 stmtblock_t block;
8736 gfc_start_block (&block);
8737 tree stmt = make_node (OMP_TASK);
8738 SET_EXPR_LOCATION (stmt, gfc_get_location (&code->loc));
8739 TREE_TYPE (stmt) = void_type_node;
8740 OMP_TASK_BODY (stmt) = NULL_TREE;
8741 OMP_TASK_CLAUSES (stmt) = gfc_trans_omp_clauses (&block,
8742 code->ext.omp_clauses,
8743 code->loc);
8744 gfc_add_expr_to_block (&block, stmt);
8745 return gfc_finish_block (&block);
8746 }
8747
8748 static tree
8749 gfc_trans_omp_taskyield (void)
8750 {
8751 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8752 return build_call_expr_loc (input_location, decl, 0);
8753 }
8754
8755 static tree
8756 gfc_trans_omp_distribute (gfc_code *code, gfc_omp_clauses *clausesa)
8757 {
8758 stmtblock_t block;
8759 gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
8760 tree stmt, omp_clauses = NULL_TREE;
8761 bool free_clausesa = false;
8762
8763 gfc_start_block (&block);
8764 if (clausesa == NULL)
8765 {
8766 clausesa = clausesa_buf;
8767 gfc_split_omp_clauses (code, clausesa);
8768 free_clausesa = true;
8769 }
8770 if (flag_openmp)
8771 omp_clauses
8772 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_DISTRIBUTE],
8773 code->loc);
8774 switch (code->op)
8775 {
8776 case EXEC_OMP_DISTRIBUTE:
8777 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
8778 case EXEC_OMP_TEAMS_DISTRIBUTE:
8779 /* This is handled in gfc_trans_omp_do. */
8780 gcc_unreachable ();
8781 break;
8782 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
8783 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
8784 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
8785 stmt = gfc_trans_omp_parallel_do (code, false, &block, clausesa);
8786 if (TREE_CODE (stmt) != BIND_EXPR)
8787 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8788 else
8789 poplevel (0, 0);
8790 break;
8791 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
8792 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
8793 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
8794 stmt = gfc_trans_omp_parallel_do_simd (code, &block, clausesa);
8795 if (TREE_CODE (stmt) != BIND_EXPR)
8796 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8797 else
8798 poplevel (0, 0);
8799 break;
8800 case EXEC_OMP_DISTRIBUTE_SIMD:
8801 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
8802 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
8803 stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
8804 &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
8805 if (TREE_CODE (stmt) != BIND_EXPR)
8806 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8807 else
8808 poplevel (0, 0);
8809 break;
8810 default:
8811 gcc_unreachable ();
8812 }
8813 if (flag_openmp)
8814 {
8815 tree distribute = make_node (OMP_DISTRIBUTE);
8816 SET_EXPR_LOCATION (distribute, gfc_get_location (&code->loc));
8817 TREE_TYPE (distribute) = void_type_node;
8818 OMP_FOR_BODY (distribute) = stmt;
8819 OMP_FOR_CLAUSES (distribute) = omp_clauses;
8820 stmt = distribute;
8821 }
8822 gfc_add_expr_to_block (&block, stmt);
8823 if (free_clausesa)
8824 gfc_free_split_omp_clauses (code, clausesa);
8825 return gfc_finish_block (&block);
8826 }
8827
8828 static tree
8829 gfc_trans_omp_teams (gfc_code *code, gfc_omp_clauses *clausesa,
8830 tree omp_clauses)
8831 {
8832 stmtblock_t block;
8833 gfc_omp_clauses clausesa_buf[GFC_OMP_SPLIT_NUM];
8834 tree stmt;
8835 bool combined = true, free_clausesa = false;
8836
8837 gfc_start_block (&block);
8838 if (clausesa == NULL)
8839 {
8840 clausesa = clausesa_buf;
8841 gfc_split_omp_clauses (code, clausesa);
8842 free_clausesa = true;
8843 }
8844 if (flag_openmp)
8845 {
8846 omp_clauses
8847 = chainon (omp_clauses,
8848 gfc_trans_omp_clauses (&block,
8849 &clausesa[GFC_OMP_SPLIT_TEAMS],
8850 code->loc));
8851 pushlevel ();
8852 }
8853 switch (code->op)
8854 {
8855 case EXEC_OMP_TARGET_TEAMS:
8856 case EXEC_OMP_TEAMS:
8857 stmt = gfc_trans_omp_code (code->block->next, true);
8858 combined = false;
8859 break;
8860 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
8861 case EXEC_OMP_TEAMS_DISTRIBUTE:
8862 stmt = gfc_trans_omp_do (code, EXEC_OMP_DISTRIBUTE, NULL,
8863 &clausesa[GFC_OMP_SPLIT_DISTRIBUTE],
8864 NULL);
8865 break;
8866 case EXEC_OMP_TARGET_TEAMS_LOOP:
8867 case EXEC_OMP_TEAMS_LOOP:
8868 stmt = gfc_trans_omp_do (code, EXEC_OMP_LOOP, NULL,
8869 &clausesa[GFC_OMP_SPLIT_DO],
8870 NULL);
8871 break;
8872 default:
8873 stmt = gfc_trans_omp_distribute (code, clausesa);
8874 break;
8875 }
8876 if (flag_openmp)
8877 {
8878 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8879 stmt = build2_loc (gfc_get_location (&code->loc), OMP_TEAMS,
8880 void_type_node, stmt, omp_clauses);
8881 if (combined)
8882 OMP_TEAMS_COMBINED (stmt) = 1;
8883 }
8884 gfc_add_expr_to_block (&block, stmt);
8885 if (free_clausesa)
8886 gfc_free_split_omp_clauses (code, clausesa);
8887 return gfc_finish_block (&block);
8888 }
8889
8890 static tree
8891 gfc_trans_omp_target (gfc_code *code)
8892 {
8893 stmtblock_t block;
8894 gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
8895 tree stmt, omp_clauses = NULL_TREE;
8896
8897 gfc_start_block (&block);
8898 gfc_split_omp_clauses (code, clausesa);
8899 if (flag_openmp)
8900 omp_clauses
8901 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_TARGET],
8902 code->loc);
8903 switch (code->op)
8904 {
8905 case EXEC_OMP_TARGET:
8906 pushlevel ();
8907 stmt = gfc_trans_omp_code (code->block->next, true);
8908 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8909 break;
8910 case EXEC_OMP_TARGET_PARALLEL:
8911 {
8912 stmtblock_t iblock;
8913
8914 pushlevel ();
8915 gfc_start_block (&iblock);
8916 tree inner_clauses
8917 = gfc_trans_omp_clauses (&iblock, &clausesa[GFC_OMP_SPLIT_PARALLEL],
8918 code->loc);
8919 stmt = gfc_trans_omp_code (code->block->next, true);
8920 stmt = build2_loc (input_location, OMP_PARALLEL, void_type_node, stmt,
8921 inner_clauses);
8922 gfc_add_expr_to_block (&iblock, stmt);
8923 stmt = gfc_finish_block (&iblock);
8924 if (TREE_CODE (stmt) != BIND_EXPR)
8925 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8926 else
8927 poplevel (0, 0);
8928 }
8929 break;
8930 case EXEC_OMP_TARGET_PARALLEL_DO:
8931 case EXEC_OMP_TARGET_PARALLEL_LOOP:
8932 stmt = gfc_trans_omp_parallel_do (code,
8933 (code->op
8934 == EXEC_OMP_TARGET_PARALLEL_LOOP),
8935 &block, clausesa);
8936 if (TREE_CODE (stmt) != BIND_EXPR)
8937 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8938 else
8939 poplevel (0, 0);
8940 break;
8941 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
8942 stmt = gfc_trans_omp_parallel_do_simd (code, &block, clausesa);
8943 if (TREE_CODE (stmt) != BIND_EXPR)
8944 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8945 else
8946 poplevel (0, 0);
8947 break;
8948 case EXEC_OMP_TARGET_SIMD:
8949 stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
8950 &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
8951 if (TREE_CODE (stmt) != BIND_EXPR)
8952 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8953 else
8954 poplevel (0, 0);
8955 break;
8956 default:
8957 if (flag_openmp
8958 && (clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_upper
8959 || clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit))
8960 {
8961 gfc_omp_clauses clausesb;
8962 tree teams_clauses;
8963 /* For combined !$omp target teams, the num_teams and
8964 thread_limit clauses are evaluated before entering the
8965 target construct. */
8966 memset (&clausesb, '\0', sizeof (clausesb));
8967 clausesb.num_teams_lower
8968 = clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_lower;
8969 clausesb.num_teams_upper
8970 = clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_upper;
8971 clausesb.thread_limit = clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit;
8972 clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_lower = NULL;
8973 clausesa[GFC_OMP_SPLIT_TEAMS].num_teams_upper = NULL;
8974 clausesa[GFC_OMP_SPLIT_TEAMS].thread_limit = NULL;
8975 teams_clauses
8976 = gfc_trans_omp_clauses (&block, &clausesb, code->loc);
8977 pushlevel ();
8978 stmt = gfc_trans_omp_teams (code, clausesa, teams_clauses);
8979 }
8980 else
8981 {
8982 pushlevel ();
8983 stmt = gfc_trans_omp_teams (code, clausesa, NULL_TREE);
8984 }
8985 if (TREE_CODE (stmt) != BIND_EXPR)
8986 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
8987 else
8988 poplevel (0, 0);
8989 break;
8990 }
8991 if (flag_openmp)
8992 {
8993 stmt = build2_loc (gfc_get_location (&code->loc), OMP_TARGET,
8994 void_type_node, stmt, omp_clauses);
8995 if (code->op != EXEC_OMP_TARGET)
8996 OMP_TARGET_COMBINED (stmt) = 1;
8997 cfun->has_omp_target = true;
8998 }
8999 gfc_add_expr_to_block (&block, stmt);
9000 gfc_free_split_omp_clauses (code, clausesa);
9001 return gfc_finish_block (&block);
9002 }
9003
9004 static tree
9005 gfc_trans_omp_taskloop (gfc_code *code, gfc_exec_op op)
9006 {
9007 stmtblock_t block;
9008 gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
9009 tree stmt, omp_clauses = NULL_TREE;
9010
9011 gfc_start_block (&block);
9012 gfc_split_omp_clauses (code, clausesa);
9013 if (flag_openmp)
9014 omp_clauses
9015 = gfc_trans_omp_clauses (&block, &clausesa[GFC_OMP_SPLIT_TASKLOOP],
9016 code->loc);
9017 switch (op)
9018 {
9019 case EXEC_OMP_TASKLOOP:
9020 /* This is handled in gfc_trans_omp_do. */
9021 gcc_unreachable ();
9022 break;
9023 case EXEC_OMP_TASKLOOP_SIMD:
9024 stmt = gfc_trans_omp_do (code, EXEC_OMP_SIMD, &block,
9025 &clausesa[GFC_OMP_SPLIT_SIMD], NULL_TREE);
9026 if (TREE_CODE (stmt) != BIND_EXPR)
9027 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
9028 else
9029 poplevel (0, 0);
9030 break;
9031 default:
9032 gcc_unreachable ();
9033 }
9034 if (flag_openmp)
9035 {
9036 tree taskloop = make_node (OMP_TASKLOOP);
9037 SET_EXPR_LOCATION (taskloop, gfc_get_location (&code->loc));
9038 TREE_TYPE (taskloop) = void_type_node;
9039 OMP_FOR_BODY (taskloop) = stmt;
9040 OMP_FOR_CLAUSES (taskloop) = omp_clauses;
9041 stmt = taskloop;
9042 }
9043 gfc_add_expr_to_block (&block, stmt);
9044 gfc_free_split_omp_clauses (code, clausesa);
9045 return gfc_finish_block (&block);
9046 }
9047
9048 static tree
9049 gfc_trans_omp_master_masked_taskloop (gfc_code *code, gfc_exec_op op)
9050 {
9051 gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
9052 stmtblock_t block;
9053 tree stmt;
9054
9055 if (op != EXEC_OMP_MASTER_TASKLOOP_SIMD
9056 && code->op != EXEC_OMP_MASTER_TASKLOOP)
9057 gfc_split_omp_clauses (code, clausesa);
9058
9059 pushlevel ();
9060 if (op == EXEC_OMP_MASKED_TASKLOOP_SIMD
9061 || op == EXEC_OMP_MASTER_TASKLOOP_SIMD)
9062 stmt = gfc_trans_omp_taskloop (code, EXEC_OMP_TASKLOOP_SIMD);
9063 else
9064 {
9065 gcc_assert (op == EXEC_OMP_MASKED_TASKLOOP
9066 || op == EXEC_OMP_MASTER_TASKLOOP);
9067 stmt = gfc_trans_omp_do (code, EXEC_OMP_TASKLOOP, NULL,
9068 code->op != EXEC_OMP_MASTER_TASKLOOP
9069 ? &clausesa[GFC_OMP_SPLIT_TASKLOOP]
9070 : code->ext.omp_clauses, NULL);
9071 }
9072 if (TREE_CODE (stmt) != BIND_EXPR)
9073 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
9074 else
9075 poplevel (0, 0);
9076 gfc_start_block (&block);
9077 if (op == EXEC_OMP_MASKED_TASKLOOP || op == EXEC_OMP_MASKED_TASKLOOP_SIMD)
9078 {
9079 tree clauses = gfc_trans_omp_clauses (&block,
9080 &clausesa[GFC_OMP_SPLIT_MASKED],
9081 code->loc);
9082 tree msk = make_node (OMP_MASKED);
9083 SET_EXPR_LOCATION (msk, gfc_get_location (&code->loc));
9084 TREE_TYPE (msk) = void_type_node;
9085 OMP_MASKED_BODY (msk) = stmt;
9086 OMP_MASKED_CLAUSES (msk) = clauses;
9087 OMP_MASKED_COMBINED (msk) = 1;
9088 gfc_add_expr_to_block (&block, msk);
9089 }
9090 else
9091 {
9092 gcc_assert (op == EXEC_OMP_MASTER_TASKLOOP
9093 || op == EXEC_OMP_MASTER_TASKLOOP_SIMD);
9094 stmt = build1_v (OMP_MASTER, stmt);
9095 gfc_add_expr_to_block (&block, stmt);
9096 }
9097 if (op != EXEC_OMP_MASTER_TASKLOOP_SIMD
9098 && code->op != EXEC_OMP_MASTER_TASKLOOP)
9099 gfc_free_split_omp_clauses (code, clausesa);
9100 return gfc_finish_block (&block);
9101 }
9102
9103 static tree
9104 gfc_trans_omp_parallel_master_masked (gfc_code *code)
9105 {
9106 stmtblock_t block;
9107 tree stmt, omp_clauses;
9108 gfc_omp_clauses clausesa[GFC_OMP_SPLIT_NUM];
9109 bool parallel_combined = false;
9110
9111 if (code->op != EXEC_OMP_PARALLEL_MASTER)
9112 gfc_split_omp_clauses (code, clausesa);
9113
9114 gfc_start_block (&block);
9115 omp_clauses = gfc_trans_omp_clauses (&block,
9116 code->op == EXEC_OMP_PARALLEL_MASTER
9117 ? code->ext.omp_clauses
9118 : &clausesa[GFC_OMP_SPLIT_PARALLEL],
9119 code->loc);
9120 pushlevel ();
9121 if (code->op == EXEC_OMP_PARALLEL_MASTER)
9122 stmt = gfc_trans_omp_master (code);
9123 else if (code->op == EXEC_OMP_PARALLEL_MASKED)
9124 stmt = gfc_trans_omp_masked (code, &clausesa[GFC_OMP_SPLIT_MASKED]);
9125 else
9126 {
9127 gfc_exec_op op;
9128 switch (code->op)
9129 {
9130 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
9131 op = EXEC_OMP_MASKED_TASKLOOP;
9132 break;
9133 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
9134 op = EXEC_OMP_MASKED_TASKLOOP_SIMD;
9135 break;
9136 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
9137 op = EXEC_OMP_MASTER_TASKLOOP;
9138 break;
9139 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
9140 op = EXEC_OMP_MASTER_TASKLOOP_SIMD;
9141 break;
9142 default:
9143 gcc_unreachable ();
9144 }
9145 stmt = gfc_trans_omp_master_masked_taskloop (code, op);
9146 parallel_combined = true;
9147 }
9148 if (TREE_CODE (stmt) != BIND_EXPR)
9149 stmt = build3_v (BIND_EXPR, NULL, stmt, poplevel (1, 0));
9150 else
9151 poplevel (0, 0);
9152 stmt = build2_loc (gfc_get_location (&code->loc), OMP_PARALLEL,
9153 void_type_node, stmt, omp_clauses);
9154 /* masked does have just filter clause, but during gimplification
9155 isn't represented by a gimplification omp context, so for
9156 !$omp parallel masked don't set OMP_PARALLEL_COMBINED,
9157 so that
9158 !$omp parallel masked
9159 !$omp taskloop simd lastprivate (x)
9160 isn't confused with
9161 !$omp parallel masked taskloop simd lastprivate (x) */
9162 if (parallel_combined)
9163 OMP_PARALLEL_COMBINED (stmt) = 1;
9164 gfc_add_expr_to_block (&block, stmt);
9165 if (code->op != EXEC_OMP_PARALLEL_MASTER)
9166 gfc_free_split_omp_clauses (code, clausesa);
9167 return gfc_finish_block (&block);
9168 }
9169
9170 static tree
9171 gfc_trans_omp_target_data (gfc_code *code)
9172 {
9173 stmtblock_t block;
9174 tree stmt, omp_clauses;
9175
9176 gfc_start_block (&block);
9177 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
9178 code->loc);
9179 stmt = gfc_trans_omp_code (code->block->next, true);
9180 stmt = build2_loc (gfc_get_location (&code->loc), OMP_TARGET_DATA,
9181 void_type_node, stmt, omp_clauses);
9182 gfc_add_expr_to_block (&block, stmt);
9183 return gfc_finish_block (&block);
9184 }
9185
9186 static tree
9187 gfc_trans_omp_target_enter_data (gfc_code *code)
9188 {
9189 stmtblock_t block;
9190 tree stmt, omp_clauses;
9191
9192 gfc_start_block (&block);
9193 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
9194 code->loc);
9195 stmt = build1_loc (input_location, OMP_TARGET_ENTER_DATA, void_type_node,
9196 omp_clauses);
9197 gfc_add_expr_to_block (&block, stmt);
9198 return gfc_finish_block (&block);
9199 }
9200
9201 static tree
9202 gfc_trans_omp_target_exit_data (gfc_code *code)
9203 {
9204 stmtblock_t block;
9205 tree stmt, omp_clauses;
9206
9207 gfc_start_block (&block);
9208 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
9209 code->loc, false, false, code->op);
9210 stmt = build1_loc (input_location, OMP_TARGET_EXIT_DATA, void_type_node,
9211 omp_clauses);
9212 gfc_add_expr_to_block (&block, stmt);
9213 return gfc_finish_block (&block);
9214 }
9215
9216 static tree
9217 gfc_trans_omp_target_update (gfc_code *code)
9218 {
9219 stmtblock_t block;
9220 tree stmt, omp_clauses;
9221
9222 gfc_start_block (&block);
9223 omp_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
9224 code->loc);
9225 stmt = build1_loc (input_location, OMP_TARGET_UPDATE, void_type_node,
9226 omp_clauses);
9227 gfc_add_expr_to_block (&block, stmt);
9228 return gfc_finish_block (&block);
9229 }
9230
9231 static tree
9232 gfc_trans_openmp_interop (gfc_code *code, gfc_omp_clauses *clauses)
9233 {
9234 stmtblock_t block;
9235 gfc_start_block (&block);
9236 tree omp_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc);
9237 tree stmt = build1_loc (input_location, OMP_INTEROP, void_type_node,
9238 omp_clauses);
9239 gfc_add_expr_to_block (&block, stmt);
9240 return gfc_finish_block (&block);
9241 }
9242
9243 static tree
9244 gfc_trans_omp_workshare (gfc_code *code, gfc_omp_clauses *clauses)
9245 {
9246 tree res, tmp, stmt;
9247 stmtblock_t block, *pblock = NULL;
9248 stmtblock_t singleblock;
9249 int saved_ompws_flags;
9250 bool singleblock_in_progress = false;
9251 /* True if previous gfc_code in workshare construct is not workshared. */
9252 bool prev_singleunit;
9253 location_t loc = gfc_get_location (&code->loc);
9254
9255 code = code->block->next;
9256
9257 pushlevel ();
9258
9259 gfc_start_block (&block);
9260 pblock = &block;
9261
9262 ompws_flags = OMPWS_WORKSHARE_FLAG;
9263 prev_singleunit = false;
9264
9265 /* Translate statements one by one to trees until we reach
9266 the end of the workshare construct. Adjacent gfc_codes that
9267 are a single unit of work are clustered and encapsulated in a
9268 single OMP_SINGLE construct. */
9269 for (; code; code = code->next)
9270 {
9271 if (code->here != 0)
9272 {
9273 res = gfc_trans_label_here (code);
9274 gfc_add_expr_to_block (pblock, res);
9275 }
9276
9277 /* No dependence analysis, use for clauses with wait.
9278 If this is the last gfc_code, use default omp_clauses. */
9279 if (code->next == NULL && clauses->nowait)
9280 ompws_flags |= OMPWS_NOWAIT;
9281
9282 /* By default, every gfc_code is a single unit of work. */
9283 ompws_flags |= OMPWS_CURR_SINGLEUNIT;
9284 ompws_flags &= ~(OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY);
9285
9286 switch (code->op)
9287 {
9288 case EXEC_NOP:
9289 res = NULL_TREE;
9290 break;
9291
9292 case EXEC_ASSIGN:
9293 res = gfc_trans_assign (code);
9294 break;
9295
9296 case EXEC_POINTER_ASSIGN:
9297 res = gfc_trans_pointer_assign (code);
9298 break;
9299
9300 case EXEC_INIT_ASSIGN:
9301 res = gfc_trans_init_assign (code);
9302 break;
9303
9304 case EXEC_FORALL:
9305 res = gfc_trans_forall (code);
9306 break;
9307
9308 case EXEC_WHERE:
9309 res = gfc_trans_where (code);
9310 break;
9311
9312 case EXEC_OMP_ATOMIC:
9313 res = gfc_trans_omp_directive (code);
9314 break;
9315
9316 case EXEC_OMP_PARALLEL:
9317 case EXEC_OMP_PARALLEL_DO:
9318 case EXEC_OMP_PARALLEL_MASTER:
9319 case EXEC_OMP_PARALLEL_SECTIONS:
9320 case EXEC_OMP_PARALLEL_WORKSHARE:
9321 case EXEC_OMP_CRITICAL:
9322 saved_ompws_flags = ompws_flags;
9323 ompws_flags = 0;
9324 res = gfc_trans_omp_directive (code);
9325 ompws_flags = saved_ompws_flags;
9326 break;
9327
9328 case EXEC_BLOCK:
9329 res = gfc_trans_block_construct (code);
9330 break;
9331
9332 default:
9333 gfc_internal_error ("gfc_trans_omp_workshare(): Bad statement code");
9334 }
9335
9336 input_location = gfc_get_location (&code->loc);
9337
9338 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
9339 {
9340 if (prev_singleunit)
9341 {
9342 if (ompws_flags & OMPWS_CURR_SINGLEUNIT)
9343 /* Add current gfc_code to single block. */
9344 gfc_add_expr_to_block (&singleblock, res);
9345 else
9346 {
9347 /* Finish single block and add it to pblock. */
9348 tmp = gfc_finish_block (&singleblock);
9349 tmp = build2_loc (loc, OMP_SINGLE,
9350 void_type_node, tmp, NULL_TREE);
9351 gfc_add_expr_to_block (pblock, tmp);
9352 /* Add current gfc_code to pblock. */
9353 gfc_add_expr_to_block (pblock, res);
9354 singleblock_in_progress = false;
9355 }
9356 }
9357 else
9358 {
9359 if (ompws_flags & OMPWS_CURR_SINGLEUNIT)
9360 {
9361 /* Start single block. */
9362 gfc_init_block (&singleblock);
9363 gfc_add_expr_to_block (&singleblock, res);
9364 singleblock_in_progress = true;
9365 loc = gfc_get_location (&code->loc);
9366 }
9367 else
9368 /* Add the new statement to the block. */
9369 gfc_add_expr_to_block (pblock, res);
9370 }
9371 prev_singleunit = (ompws_flags & OMPWS_CURR_SINGLEUNIT) != 0;
9372 }
9373 }
9374
9375 /* Finish remaining SINGLE block, if we were in the middle of one. */
9376 if (singleblock_in_progress)
9377 {
9378 /* Finish single block and add it to pblock. */
9379 tmp = gfc_finish_block (&singleblock);
9380 tmp = build2_loc (loc, OMP_SINGLE, void_type_node, tmp,
9381 clauses->nowait
9382 ? build_omp_clause (input_location, OMP_CLAUSE_NOWAIT)
9383 : NULL_TREE);
9384 gfc_add_expr_to_block (pblock, tmp);
9385 }
9386
9387 stmt = gfc_finish_block (pblock);
9388 if (TREE_CODE (stmt) != BIND_EXPR)
9389 {
9390 if (!IS_EMPTY_STMT (stmt))
9391 {
9392 tree bindblock = poplevel (1, 0);
9393 stmt = build3_v (BIND_EXPR, NULL, stmt, bindblock);
9394 }
9395 else
9396 poplevel (0, 0);
9397 }
9398 else
9399 poplevel (0, 0);
9400
9401 if (IS_EMPTY_STMT (stmt) && !clauses->nowait)
9402 stmt = gfc_trans_omp_barrier ();
9403
9404 ompws_flags = 0;
9405 return stmt;
9406 }
9407
9408 tree
9409 gfc_trans_oacc_declare (gfc_code *code)
9410 {
9411 stmtblock_t block;
9412 tree stmt, oacc_clauses;
9413 enum tree_code construct_code;
9414
9415 construct_code = OACC_DATA;
9416
9417 gfc_start_block (&block);
9418
9419 oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.oacc_declare->clauses,
9420 code->loc, false, true);
9421 stmt = gfc_trans_omp_code (code->block->next, true);
9422 stmt = build2_loc (input_location, construct_code, void_type_node, stmt,
9423 oacc_clauses);
9424 gfc_add_expr_to_block (&block, stmt);
9425
9426 return gfc_finish_block (&block);
9427 }
9428
9429 tree
9430 gfc_trans_oacc_directive (gfc_code *code)
9431 {
9432 switch (code->op)
9433 {
9434 case EXEC_OACC_PARALLEL_LOOP:
9435 case EXEC_OACC_KERNELS_LOOP:
9436 case EXEC_OACC_SERIAL_LOOP:
9437 return gfc_trans_oacc_combined_directive (code);
9438 case EXEC_OACC_PARALLEL:
9439 case EXEC_OACC_KERNELS:
9440 case EXEC_OACC_SERIAL:
9441 case EXEC_OACC_DATA:
9442 case EXEC_OACC_HOST_DATA:
9443 return gfc_trans_oacc_construct (code);
9444 case EXEC_OACC_LOOP:
9445 return gfc_trans_omp_do (code, code->op, NULL, code->ext.omp_clauses,
9446 NULL);
9447 case EXEC_OACC_UPDATE:
9448 case EXEC_OACC_CACHE:
9449 case EXEC_OACC_ENTER_DATA:
9450 case EXEC_OACC_EXIT_DATA:
9451 return gfc_trans_oacc_executable_directive (code);
9452 case EXEC_OACC_WAIT:
9453 return gfc_trans_oacc_wait_directive (code);
9454 case EXEC_OACC_ATOMIC:
9455 return gfc_trans_omp_atomic (code);
9456 case EXEC_OACC_DECLARE:
9457 return gfc_trans_oacc_declare (code);
9458 default:
9459 gcc_unreachable ();
9460 }
9461 }
9462
9463 tree
9464 gfc_trans_omp_directive (gfc_code *code)
9465 {
9466 switch (code->op)
9467 {
9468 case EXEC_OMP_ALLOCATE:
9469 case EXEC_OMP_ALLOCATORS:
9470 return gfc_trans_omp_allocators (code);
9471 case EXEC_OMP_ASSUME:
9472 return gfc_trans_omp_assume (code);
9473 case EXEC_OMP_ATOMIC:
9474 return gfc_trans_omp_atomic (code);
9475 case EXEC_OMP_BARRIER:
9476 return gfc_trans_omp_barrier ();
9477 case EXEC_OMP_CANCEL:
9478 return gfc_trans_omp_cancel (code);
9479 case EXEC_OMP_CANCELLATION_POINT:
9480 return gfc_trans_omp_cancellation_point (code);
9481 case EXEC_OMP_CRITICAL:
9482 return gfc_trans_omp_critical (code);
9483 case EXEC_OMP_DEPOBJ:
9484 return gfc_trans_omp_depobj (code);
9485 case EXEC_OMP_DISTRIBUTE:
9486 case EXEC_OMP_DO:
9487 case EXEC_OMP_LOOP:
9488 case EXEC_OMP_SIMD:
9489 case EXEC_OMP_TASKLOOP:
9490 case EXEC_OMP_TILE:
9491 case EXEC_OMP_UNROLL:
9492 return gfc_trans_omp_do (code, code->op, NULL, code->ext.omp_clauses,
9493 NULL);
9494 case EXEC_OMP_DISPATCH:
9495 return gfc_trans_omp_dispatch (code);
9496 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9497 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9498 case EXEC_OMP_DISTRIBUTE_SIMD:
9499 return gfc_trans_omp_distribute (code, NULL);
9500 case EXEC_OMP_DO_SIMD:
9501 return gfc_trans_omp_do_simd (code, NULL, NULL, NULL_TREE);
9502 case EXEC_OMP_ERROR:
9503 return gfc_trans_omp_error (code);
9504 case EXEC_OMP_FLUSH:
9505 return gfc_trans_omp_flush (code);
9506 case EXEC_OMP_MASKED:
9507 return gfc_trans_omp_masked (code, NULL);
9508 case EXEC_OMP_MASTER:
9509 return gfc_trans_omp_master (code);
9510 case EXEC_OMP_MASKED_TASKLOOP:
9511 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
9512 case EXEC_OMP_MASTER_TASKLOOP:
9513 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
9514 return gfc_trans_omp_master_masked_taskloop (code, code->op);
9515 case EXEC_OMP_METADIRECTIVE:
9516 return gfc_trans_omp_metadirective (code);
9517 case EXEC_OMP_ORDERED:
9518 return gfc_trans_omp_ordered (code);
9519 case EXEC_OMP_PARALLEL:
9520 return gfc_trans_omp_parallel (code);
9521 case EXEC_OMP_PARALLEL_DO:
9522 return gfc_trans_omp_parallel_do (code, false, NULL, NULL);
9523 case EXEC_OMP_PARALLEL_LOOP:
9524 return gfc_trans_omp_parallel_do (code, true, NULL, NULL);
9525 case EXEC_OMP_PARALLEL_DO_SIMD:
9526 return gfc_trans_omp_parallel_do_simd (code, NULL, NULL);
9527 case EXEC_OMP_PARALLEL_MASKED:
9528 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
9529 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
9530 case EXEC_OMP_PARALLEL_MASTER:
9531 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
9532 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
9533 return gfc_trans_omp_parallel_master_masked (code);
9534 case EXEC_OMP_PARALLEL_SECTIONS:
9535 return gfc_trans_omp_parallel_sections (code);
9536 case EXEC_OMP_PARALLEL_WORKSHARE:
9537 return gfc_trans_omp_parallel_workshare (code);
9538 case EXEC_OMP_SCOPE:
9539 return gfc_trans_omp_scope (code);
9540 case EXEC_OMP_SECTIONS:
9541 return gfc_trans_omp_sections (code, code->ext.omp_clauses);
9542 case EXEC_OMP_SINGLE:
9543 return gfc_trans_omp_single (code, code->ext.omp_clauses);
9544 case EXEC_OMP_TARGET:
9545 case EXEC_OMP_TARGET_PARALLEL:
9546 case EXEC_OMP_TARGET_PARALLEL_DO:
9547 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
9548 case EXEC_OMP_TARGET_PARALLEL_LOOP:
9549 case EXEC_OMP_TARGET_SIMD:
9550 case EXEC_OMP_TARGET_TEAMS:
9551 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9552 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9553 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9554 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9555 case EXEC_OMP_TARGET_TEAMS_LOOP:
9556 return gfc_trans_omp_target (code);
9557 case EXEC_OMP_TARGET_DATA:
9558 return gfc_trans_omp_target_data (code);
9559 case EXEC_OMP_TARGET_ENTER_DATA:
9560 return gfc_trans_omp_target_enter_data (code);
9561 case EXEC_OMP_TARGET_EXIT_DATA:
9562 return gfc_trans_omp_target_exit_data (code);
9563 case EXEC_OMP_TARGET_UPDATE:
9564 return gfc_trans_omp_target_update (code);
9565 case EXEC_OMP_TASK:
9566 return gfc_trans_omp_task (code);
9567 case EXEC_OMP_TASKGROUP:
9568 return gfc_trans_omp_taskgroup (code);
9569 case EXEC_OMP_TASKLOOP_SIMD:
9570 return gfc_trans_omp_taskloop (code, code->op);
9571 case EXEC_OMP_TASKWAIT:
9572 return gfc_trans_omp_taskwait (code);
9573 case EXEC_OMP_TASKYIELD:
9574 return gfc_trans_omp_taskyield ();
9575 case EXEC_OMP_TEAMS:
9576 case EXEC_OMP_TEAMS_DISTRIBUTE:
9577 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9578 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9579 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9580 case EXEC_OMP_TEAMS_LOOP:
9581 return gfc_trans_omp_teams (code, NULL, NULL_TREE);
9582 case EXEC_OMP_WORKSHARE:
9583 return gfc_trans_omp_workshare (code, code->ext.omp_clauses);
9584 case EXEC_OMP_INTEROP:
9585 return gfc_trans_openmp_interop (code, code->ext.omp_clauses);
9586 default:
9587 gcc_unreachable ();
9588 }
9589 }
9590
9591 void
9592 gfc_trans_omp_declare_simd (gfc_namespace *ns)
9593 {
9594 if (ns->entries)
9595 return;
9596
9597 gfc_omp_declare_simd *ods;
9598 for (ods = ns->omp_declare_simd; ods; ods = ods->next)
9599 {
9600 tree c = gfc_trans_omp_clauses (NULL, ods->clauses, ods->where, true);
9601 tree fndecl = ns->proc_name->backend_decl;
9602 if (c != NULL_TREE)
9603 c = tree_cons (NULL_TREE, c, NULL_TREE);
9604 c = build_tree_list (get_identifier ("omp declare simd"), c);
9605 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
9606 DECL_ATTRIBUTES (fndecl) = c;
9607 }
9608 }
9609
9610 /* Translate the context selector list GFC_SELECTORS, using WHERE as the
9611 locus for error messages. */
9612
9613 static tree
9614 gfc_trans_omp_set_selector (gfc_omp_set_selector *gfc_selectors, locus where)
9615 {
9616 tree set_selectors = NULL_TREE;
9617 gfc_omp_set_selector *oss;
9618
9619 for (oss = gfc_selectors; oss; oss = oss->next)
9620 {
9621 tree selectors = NULL_TREE;
9622 gfc_omp_selector *os;
9623 enum omp_tss_code set = oss->code;
9624 gcc_assert (set != OMP_TRAIT_SET_INVALID);
9625
9626 for (os = oss->trait_selectors; os; os = os->next)
9627 {
9628 tree scoreval = NULL_TREE;
9629 tree properties = NULL_TREE;
9630 gfc_omp_trait_property *otp;
9631 enum omp_ts_code sel = os->code;
9632
9633 /* Per the spec, "Implementations can ignore specified
9634 selectors that are not those described in this section";
9635 however, we must record such selectors because they
9636 cause match failures. */
9637 if (sel == OMP_TRAIT_INVALID)
9638 {
9639 selectors = make_trait_selector (sel, NULL_TREE, NULL_TREE,
9640 selectors);
9641 continue;
9642 }
9643
9644 for (otp = os->properties; otp; otp = otp->next)
9645 {
9646 switch (otp->property_kind)
9647 {
9648 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
9649 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
9650 {
9651 tree expr = NULL_TREE;
9652 gfc_se se;
9653 gfc_init_se (&se, NULL);
9654 gfc_conv_expr (&se, otp->expr);
9655 expr = se.expr;
9656 properties = make_trait_property (NULL_TREE, expr,
9657 properties);
9658 }
9659 break;
9660 case OMP_TRAIT_PROPERTY_ID:
9661 properties
9662 = make_trait_property (get_identifier (otp->name),
9663 NULL_TREE, properties);
9664 break;
9665 case OMP_TRAIT_PROPERTY_NAME_LIST:
9666 {
9667 tree prop = OMP_TP_NAMELIST_NODE;
9668 tree value = NULL_TREE;
9669 if (otp->is_name)
9670 value = get_identifier (otp->name);
9671 else
9672 value = gfc_conv_constant_to_tree (otp->expr);
9673
9674 properties = make_trait_property (prop, value,
9675 properties);
9676 }
9677 break;
9678 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
9679 properties = gfc_trans_omp_clauses (NULL, otp->clauses,
9680 where, true);
9681 break;
9682 default:
9683 gcc_unreachable ();
9684 }
9685 }
9686
9687 if (os->score)
9688 {
9689 gfc_se se;
9690 gfc_init_se (&se, NULL);
9691 gfc_conv_expr (&se, os->score);
9692 scoreval = se.expr;
9693 }
9694
9695 selectors = make_trait_selector (sel, scoreval,
9696 properties, selectors);
9697 }
9698 set_selectors = make_trait_set_selector (set, selectors, set_selectors);
9699 }
9700 return set_selectors;
9701 }
9702
9703 /* If 'ns' points to a formal namespace in an interface, ns->parent == NULL;
9704 hence, parent_ns is used instead. */
9705
9706 void
9707 gfc_trans_omp_declare_variant (gfc_namespace *ns, gfc_namespace *parent_ns)
9708 {
9709 tree base_fn_decl = ns->proc_name->backend_decl;
9710 gfc_namespace *search_ns = ns;
9711 gfc_omp_declare_variant *next;
9712
9713 for (gfc_omp_declare_variant *odv = search_ns->omp_declare_variant;
9714 search_ns; odv = next)
9715 {
9716 /* Look in the parent namespace if there are no more directives in the
9717 current namespace. */
9718 if (!odv)
9719 {
9720 if (!search_ns->parent && search_ns == ns)
9721 search_ns = parent_ns;
9722 else
9723 search_ns = search_ns->parent;
9724 if (search_ns)
9725 next = search_ns->omp_declare_variant;
9726 continue;
9727 }
9728
9729 next = odv->next;
9730
9731 if (odv->error_p)
9732 continue;
9733
9734 /* Check directive the first time it is encountered. */
9735 bool error_found = true;
9736
9737 if (odv->checked_p)
9738 error_found = false;
9739 if (odv->base_proc_symtree == NULL)
9740 {
9741 if (!search_ns->proc_name->attr.function
9742 && !search_ns->proc_name->attr.subroutine)
9743 gfc_error ("The base name for %<declare variant%> must be "
9744 "specified at %L", &odv->where);
9745 else
9746 error_found = false;
9747 }
9748 else
9749 {
9750 if (!search_ns->contained
9751 && !odv->base_proc_symtree->n.sym->attr.use_assoc
9752 && strcmp (odv->base_proc_symtree->name,
9753 ns->proc_name->name))
9754 gfc_error ("The base name at %L does not match the name of the "
9755 "current procedure", &odv->where);
9756 else if (odv->base_proc_symtree->n.sym->attr.entry)
9757 gfc_error ("The base name at %L must not be an entry name",
9758 &odv->where);
9759 else if (odv->base_proc_symtree->n.sym->attr.generic)
9760 gfc_error ("The base name at %L must not be a generic name",
9761 &odv->where);
9762 else if (odv->base_proc_symtree->n.sym->attr.proc_pointer)
9763 gfc_error ("The base name at %L must not be a procedure pointer",
9764 &odv->where);
9765 else if (odv->base_proc_symtree->n.sym->attr.implicit_type)
9766 gfc_error ("The base procedure at %L must have an explicit "
9767 "interface", &odv->where);
9768 else
9769 error_found = false;
9770 }
9771
9772 odv->checked_p = true;
9773 if (error_found)
9774 {
9775 odv->error_p = true;
9776 continue;
9777 }
9778
9779 /* Ignore directives that do not apply to the current procedure. */
9780 if ((odv->base_proc_symtree == NULL && search_ns != ns)
9781 || (odv->base_proc_symtree != NULL
9782 && !ns->proc_name->attr.use_assoc
9783 && strcmp (odv->base_proc_symtree->name, ns->proc_name->name))
9784 || (odv->base_proc_symtree != NULL
9785 && ns->proc_name->attr.use_assoc
9786 && strcmp (odv->base_proc_symtree->n.sym->name,
9787 ns->proc_name->name)))
9788 continue;
9789
9790 tree set_selectors = gfc_trans_omp_set_selector (odv->set_selectors,
9791 odv->where);
9792 const char *variant_proc_name = odv->variant_proc_symtree->name;
9793 gfc_symbol *variant_proc_sym = odv->variant_proc_symtree->n.sym;
9794 if (variant_proc_sym == NULL || variant_proc_sym->attr.implicit_type)
9795 {
9796 gfc_symtree *proc_st;
9797 gfc_find_sym_tree (variant_proc_name, gfc_current_ns, 1, &proc_st);
9798 variant_proc_sym = proc_st ? proc_st->n.sym : NULL;
9799 }
9800 if (variant_proc_sym == NULL)
9801 {
9802 gfc_error ("Cannot find symbol %qs at %L", variant_proc_name,
9803 &odv->where);
9804 continue;
9805 }
9806 set_selectors = omp_check_context_selector
9807 (gfc_get_location (&odv->where), set_selectors,
9808 OMP_CTX_DECLARE_VARIANT);
9809 if (set_selectors != error_mark_node)
9810 {
9811 if (!variant_proc_sym->attr.implicit_type
9812 && !variant_proc_sym->attr.subroutine
9813 && !variant_proc_sym->attr.function)
9814 {
9815 gfc_error ("variant %qs at %L is not a function or subroutine",
9816 variant_proc_name, &odv->where);
9817 variant_proc_sym = NULL;
9818 }
9819 else if (omp_get_context_selector (set_selectors,
9820 OMP_TRAIT_SET_CONSTRUCT,
9821 OMP_TRAIT_CONSTRUCT_SIMD)
9822 == NULL_TREE)
9823 {
9824 char err[256];
9825 gfc_formal_arglist *last_arg = NULL, *extra_arg = NULL;
9826 int nappend_args = 0;
9827 if (odv->append_args_list)
9828 {
9829 gfc_formal_arglist *arg;
9830 int nargs = 0;
9831 for (arg = gfc_sym_get_dummy_args (ns->proc_name);
9832 arg; arg = arg->next)
9833 nargs++;
9834
9835 last_arg = gfc_sym_get_dummy_args (variant_proc_sym);
9836 for (int i = 1 ; i < nargs && last_arg; i++)
9837 last_arg = last_arg->next;
9838 if (nargs == 0)
9839 {
9840 extra_arg = last_arg;
9841 last_arg = NULL;
9842 variant_proc_sym->formal = NULL;
9843 }
9844 else if (last_arg)
9845 {
9846 extra_arg = last_arg->next;
9847 last_arg->next = NULL;
9848 }
9849 for (gfc_omp_namelist *n = odv->append_args_list; n != NULL;
9850 n = n->next)
9851 nappend_args++;
9852 }
9853 if (!gfc_compare_interfaces (ns->proc_name, variant_proc_sym,
9854 variant_proc_sym->name, 0, 1,
9855 err, sizeof (err), NULL, NULL))
9856 {
9857 gfc_error ("variant %qs and base %qs at %L have "
9858 "incompatible types: %s",
9859 variant_proc_name, ns->proc_name->name,
9860 &odv->where, err);
9861 if (nappend_args)
9862 inform (gfc_get_location (&odv->append_args_list->where),
9863 "%<append_args%> clause implies that %qs has %d "
9864 "dummy arguments of integer type with "
9865 "%<omp_interop_kind%> kind", variant_proc_name,
9866 nappend_args);
9867 variant_proc_sym = NULL;
9868 }
9869 if (last_arg)
9870 last_arg->next = extra_arg;
9871 else if (extra_arg)
9872 variant_proc_sym->formal = extra_arg;
9873 locus *loc = (odv->append_args_list
9874 ? &odv->append_args_list->where : &odv->where);
9875 int nextra_arg = 0;
9876 for (; extra_arg; extra_arg = extra_arg->next)
9877 {
9878 nextra_arg++;
9879 if (!variant_proc_sym)
9880 continue;
9881 if (extra_arg->sym->ts.type != BT_INTEGER
9882 || extra_arg->sym->ts.kind != gfc_index_integer_kind
9883 || extra_arg->sym->attr.dimension
9884 || extra_arg->sym->attr.codimension
9885 || extra_arg->sym->attr.pointer
9886 || extra_arg->sym->attr.allocatable
9887 || extra_arg->sym->attr.proc_pointer)
9888 {
9889 gfc_error ("%qs at %L must be a nonpointer, "
9890 "nonallocatable scalar integer dummy argument "
9891 "of %<omp_interop_kind%> kind as it utilized "
9892 "with the %<append_args%> clause at %L",
9893 extra_arg->sym->name,
9894 &extra_arg->sym->declared_at, loc);
9895 variant_proc_sym = NULL;
9896 }
9897 if (extra_arg->sym->attr.optional)
9898 {
9899 gfc_error ("%qs at %L with OPTIONAL attribute "
9900 "not support when utilized with the "
9901 "%<append_args%> clause at %L",
9902 extra_arg->sym->name,
9903 &extra_arg->sym->declared_at, loc);
9904 variant_proc_sym = NULL;
9905 }
9906 }
9907 if (variant_proc_sym && nappend_args != nextra_arg)
9908 {
9909 gfc_error ("%qs at %L has %d but requires %d "
9910 "%<omp_interop_kind%> kind dummy arguments as it "
9911 "is utilized with the %<append_args%> clause at "
9912 "%L", variant_proc_sym->name,
9913 &variant_proc_sym->declared_at, nextra_arg,
9914 nappend_args, loc);
9915 variant_proc_sym = NULL;
9916 }
9917 }
9918 if ((odv->adjust_args_list != NULL || odv->append_args_list != NULL)
9919 && omp_get_context_selector (set_selectors,
9920 OMP_TRAIT_SET_CONSTRUCT,
9921 OMP_TRAIT_CONSTRUCT_DISPATCH)
9922 == NULL_TREE)
9923 {
9924 gfc_error ("the %qs clause can only be specified if "
9925 "the %<dispatch%> selector of the construct "
9926 "selector set appears in the %<match%> clause at %L",
9927 odv->adjust_args_list ? "adjust_args" : "append_args",
9928 &odv->where);
9929 variant_proc_sym = NULL;
9930 }
9931 if (variant_proc_sym != NULL)
9932 {
9933 gfc_set_sym_referenced (variant_proc_sym);
9934 tree construct
9935 = omp_get_context_selector_list (set_selectors,
9936 OMP_TRAIT_SET_CONSTRUCT);
9937 omp_mark_declare_variant (gfc_get_location (&odv->where),
9938 gfc_get_symbol_decl (variant_proc_sym),
9939 construct);
9940 if (omp_context_selector_matches (set_selectors,
9941 NULL_TREE, false))
9942 {
9943 tree need_device_ptr_list = NULL_TREE;
9944 tree need_device_addr_list = NULL_TREE;
9945 tree append_args_tree = NULL_TREE;
9946 tree id = get_identifier ("omp declare variant base");
9947 tree variant = gfc_get_symbol_decl (variant_proc_sym);
9948 DECL_ATTRIBUTES (base_fn_decl)
9949 = tree_cons (id, build_tree_list (variant, set_selectors),
9950 DECL_ATTRIBUTES (base_fn_decl));
9951 int arg_idx_offset = 0;
9952 if (gfc_return_by_reference (ns->proc_name))
9953 {
9954 arg_idx_offset++;
9955 if (ns->proc_name->ts.type == BT_CHARACTER)
9956 arg_idx_offset++;
9957 }
9958 int nargs = 0;
9959 for (gfc_formal_arglist *arg
9960 = gfc_sym_get_dummy_args (ns->proc_name);
9961 arg; arg = arg->next)
9962 nargs++;
9963 if (odv->append_args_list)
9964 {
9965 int append_arg_no = arg_idx_offset + nargs;
9966 tree last_arg = NULL_TREE;
9967 for (gfc_omp_namelist *n = odv->append_args_list;
9968 n != NULL; n = n->next)
9969 {
9970 tree pref = NULL_TREE;
9971 if (n->u.init.len)
9972 {
9973 pref = build_string (n->u.init.len,
9974 n->u2.init_interop);
9975 TREE_TYPE (pref) = build_array_type_nelts (
9976 unsigned_char_type_node,
9977 n->u.init.len);
9978 }
9979 /* Save location, (target + target sync) and
9980 prefer_type list in a tree list. */
9981 tree t = build_tree_list (n->u.init.target
9982 ? boolean_true_node
9983 : boolean_false_node,
9984 n->u.init.targetsync
9985 ? boolean_true_node
9986 : boolean_false_node);
9987 t = build1_loc (gfc_get_location (&n->where),
9988 NOP_EXPR, void_type_node, t);
9989 t = build_tree_list (t, pref);
9990 if (append_args_tree)
9991 {
9992 TREE_CHAIN (last_arg) = t;
9993 last_arg = t;
9994 }
9995 else
9996 append_args_tree = last_arg = t;
9997 }
9998 /* Store as 'purpose' = arg number to be used for inserting
9999 and 'value' = list of interop items. */
10000 append_args_tree = build_tree_list (
10001 build_int_cst (integer_type_node,
10002 append_arg_no),
10003 append_args_tree);
10004 }
10005 vec<gfc_symbol *> adjust_args_list = vNULL;
10006 for (gfc_omp_namelist *arg_list = odv->adjust_args_list;
10007 arg_list != NULL; arg_list = arg_list->next)
10008 {
10009 int from, to;
10010 if (arg_list->expr == NULL || arg_list->sym)
10011 from = ((arg_list->u.adj_args.omp_num_args_minus
10012 || arg_list->u.adj_args.omp_num_args_plus)
10013 ? nargs : 1);
10014 else
10015 {
10016 if (arg_list->u.adj_args.omp_num_args_plus)
10017 mpz_add_ui (arg_list->expr->value.integer,
10018 arg_list->expr->value.integer, nargs);
10019 if (arg_list->u.adj_args.omp_num_args_minus)
10020 mpz_ui_sub (arg_list->expr->value.integer, nargs,
10021 arg_list->expr->value.integer);
10022 if (mpz_sgn (arg_list->expr->value.integer) <= 0)
10023 {
10024 gfc_warning (OPT_Wopenmp,
10025 "Expected positive argument index "
10026 "at %L", &arg_list->where);
10027 from = 1;
10028 }
10029 else
10030 from
10031 = (mpz_fits_sint_p (arg_list->expr->value.integer)
10032 ? mpz_get_si (arg_list->expr->value.integer)
10033 : INT_MAX);
10034 if (from > nargs)
10035 gfc_warning (OPT_Wopenmp,
10036 "Argument index at %L exceeds number "
10037 "of arguments %d", &arg_list->where,
10038 nargs);
10039 }
10040 locus loc = arg_list->where;
10041 if (!arg_list->u.adj_args.range_start)
10042 to = from;
10043 else
10044 {
10045 loc = gfc_get_location_range (&arg_list->where, 0,
10046 &arg_list->where, 0,
10047 &arg_list->next->where);
10048 if (arg_list->next->expr == NULL)
10049 to = nargs;
10050 else
10051 {
10052 if (arg_list->next->u.adj_args.omp_num_args_plus)
10053 mpz_add_ui (arg_list->next->expr->value.integer,
10054 arg_list->next->expr->value.integer,
10055 nargs);
10056 if (arg_list->next->u.adj_args.omp_num_args_minus)
10057 mpz_ui_sub (arg_list->next->expr->value.integer,
10058 nargs,
10059 arg_list->next->expr->value.integer);
10060 if (mpz_sgn (arg_list->next->expr->value.integer)
10061 <= 0)
10062 {
10063 gfc_warning (OPT_Wopenmp,
10064 "Expected positive argument "
10065 "index at %L", &loc);
10066 to = 0;
10067 }
10068 else
10069 to = mpz_get_si (
10070 arg_list->next->expr->value.integer);
10071 }
10072 if (from > to && to != 0)
10073 gfc_warning (OPT_Wopenmp,
10074 "Upper argument index smaller than "
10075 "lower one at %L", &loc);
10076 if (to > nargs)
10077 to = nargs;
10078 arg_list = arg_list->next;
10079 }
10080 if (from > nargs)
10081 continue;
10082 /* Change to zero based index. */
10083 from--; to--;
10084 gfc_formal_arglist *arg = ns->proc_name->formal;
10085 if (!arg_list->sym && to >= from)
10086 for (int idx = 0; idx < from; idx++)
10087 arg = arg->next;
10088 for (int idx = from; idx <= to; idx++)
10089 {
10090 if (idx > from)
10091 arg = arg->next;
10092 if (arg_list->sym)
10093 {
10094 for (arg = ns->proc_name->formal, idx = 0;
10095 arg != NULL; arg = arg->next, idx++)
10096 if (arg->sym == arg_list->sym)
10097 break;
10098 if (!arg || !arg_list->sym->attr.dummy)
10099 {
10100 gfc_error ("List item %qs at %L, declared at "
10101 "%L, is not a dummy argument",
10102 arg_list->sym->name, &loc,
10103 &arg_list->sym->declared_at);
10104 continue;
10105 }
10106 }
10107 if (arg_list->u.adj_args.need_ptr
10108 && (arg->sym->ts.f90_type != BT_VOID
10109 || !arg->sym->ts.u.derived->ts.is_iso_c
10110 || (arg->sym->ts.u.derived->intmod_sym_id
10111 != ISOCBINDING_PTR)
10112 || arg->sym->attr.dimension))
10113 {
10114 gfc_error ("Argument %qs at %L to list item in "
10115 "%<need_device_ptr%> at %L must be a "
10116 "scalar of TYPE(C_PTR)",
10117 arg->sym->name,
10118 &arg->sym->declared_at, &loc);
10119 if (!arg->sym->attr.value)
10120 inform (gfc_get_location (&loc),
10121 "Consider using %<need_device_addr%> "
10122 "instead");
10123 continue;
10124 }
10125 if (arg_list->u.adj_args.need_addr
10126 && arg->sym->attr.value)
10127 {
10128 gfc_error ("Argument %qs at %L to list item in "
10129 "%<need_device_addr%> at %L must not "
10130 "have the VALUE attribute",
10131 arg->sym->name,
10132 &arg->sym->declared_at, &loc);
10133 continue;
10134 }
10135 if (adjust_args_list.contains (arg->sym))
10136 {
10137 gfc_error ("%qs at %L is specified more than "
10138 "once", arg->sym->name, &loc);
10139 continue;
10140 }
10141 adjust_args_list.safe_push (arg->sym);
10142
10143 if (arg_list->u.adj_args.need_addr)
10144 {
10145 /* TODO: Has to to support OPTIONAL and array
10146 descriptors; should check for CLASS, coarrays?
10147 Reject "abc" and 123 as actual arguments (in
10148 gimplify.cc or in the FE? Reject noncontiguous
10149 actuals? Cf. also PR C++/118859.
10150 Also check array-valued type(c_ptr). */
10151 static bool warned = false;
10152 if (!warned)
10153 sorry_at (gfc_get_location (&loc),
10154 "%<need_device_addr%> not yet "
10155 "supported");
10156 warned = true;
10157 continue;
10158 }
10159 if (arg_list->u.adj_args.need_ptr
10160 || arg_list->u.adj_args.need_addr)
10161 {
10162 // Store 0-based argument index,
10163 // as in gimplify_call_expr
10164 tree t
10165 = build_tree_list (
10166 NULL_TREE,
10167 build_int_cst (integer_type_node,
10168 idx + arg_idx_offset));
10169 if (arg_list->u.adj_args.need_ptr)
10170 need_device_ptr_list
10171 = chainon (need_device_ptr_list, t);
10172 else
10173 need_device_addr_list
10174 = chainon (need_device_addr_list, t);
10175 }
10176 }
10177 }
10178 tree t = NULL_TREE;
10179 if (need_device_ptr_list
10180 || need_device_addr_list
10181 || append_args_tree)
10182 {
10183 t = build_tree_list (need_device_ptr_list,
10184 need_device_addr_list),
10185 TREE_CHAIN (t) = append_args_tree;
10186 DECL_ATTRIBUTES (variant) = tree_cons (
10187 get_identifier ("omp declare variant variant args"), t,
10188 DECL_ATTRIBUTES (variant));
10189 }
10190 }
10191 }
10192 }
10193 }
10194 }
10195
10196 /* Add ptr for tracking as being allocated by GOMP_alloc. */
10197
10198 tree
10199 gfc_omp_call_add_alloc (tree ptr)
10200 {
10201 static tree fn = NULL_TREE;
10202 if (fn == NULL_TREE)
10203 {
10204 fn = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10205 tree att = build_tree_list (NULL_TREE, build_string (4, ". R "));
10206 att = tree_cons (get_identifier ("fn spec"), att, TYPE_ATTRIBUTES (fn));
10207 fn = build_type_attribute_variant (fn, att);
10208 fn = build_fn_decl ("GOMP_add_alloc", fn);
10209 }
10210 return build_call_expr_loc (input_location, fn, 1, ptr);
10211 }
10212
10213 /* Generated function returns true when it was tracked via GOMP_add_alloc and
10214 removes it from the tracking. As called just before GOMP_free or omp_realloc
10215 the pointer is or might become invalid, thus, it is always removed. */
10216
10217 tree
10218 gfc_omp_call_is_alloc (tree ptr)
10219 {
10220 static tree fn = NULL_TREE;
10221 if (fn == NULL_TREE)
10222 {
10223 fn = build_function_type_list (boolean_type_node, ptr_type_node,
10224 NULL_TREE);
10225 tree att = build_tree_list (NULL_TREE, build_string (4, ". R "));
10226 att = tree_cons (get_identifier ("fn spec"), att, TYPE_ATTRIBUTES (fn));
10227 fn = build_type_attribute_variant (fn, att);
10228 fn = build_fn_decl ("GOMP_is_alloc", fn);
10229 }
10230 return build_call_expr_loc (input_location, fn, 1, ptr);
10231 }
10232
10233 tree
10234 gfc_trans_omp_metadirective (gfc_code *code)
10235 {
10236 gfc_omp_variant *variant = code->ext.omp_variants;
10237
10238 tree metadirective_tree = make_node (OMP_METADIRECTIVE);
10239 SET_EXPR_LOCATION (metadirective_tree, gfc_get_location (&code->loc));
10240 TREE_TYPE (metadirective_tree) = void_type_node;
10241 OMP_METADIRECTIVE_VARIANTS (metadirective_tree) = NULL_TREE;
10242
10243 tree tree_body = NULL_TREE;
10244
10245 while (variant)
10246 {
10247 tree ctx = gfc_trans_omp_set_selector (variant->selectors,
10248 variant->where);
10249 ctx = omp_check_context_selector (gfc_get_location (&variant->where),
10250 ctx, OMP_CTX_METADIRECTIVE);
10251 if (ctx == error_mark_node)
10252 return error_mark_node;
10253
10254 /* If the selector doesn't match, drop the whole variant. */
10255 if (!omp_context_selector_matches (ctx, NULL_TREE, false))
10256 {
10257 variant = variant->next;
10258 continue;
10259 }
10260
10261 gfc_code *next_code = variant->code->next;
10262 if (next_code && tree_body == NULL_TREE)
10263 tree_body = gfc_trans_code (next_code);
10264
10265 if (next_code)
10266 variant->code->next = NULL;
10267 tree directive = gfc_trans_code (variant->code);
10268 if (next_code)
10269 variant->code->next = next_code;
10270
10271 tree body = next_code ? tree_body : NULL_TREE;
10272 tree omp_variant = make_omp_metadirective_variant (ctx, directive, body);
10273 OMP_METADIRECTIVE_VARIANTS (metadirective_tree)
10274 = chainon (OMP_METADIRECTIVE_VARIANTS (metadirective_tree),
10275 omp_variant);
10276 variant = variant->next;
10277 }
10278
10279 /* TODO: Resolve the metadirective here if possible. */
10280
10281 return metadirective_tree;
10282 }