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