Debian Codegraph @

gcc/omp-low.cc

raw
1 /* Lowering pass for OMP directives. Converts OMP directives into explicit
2 calls to the runtime library (libgomp), data marshalling to implement data
3 sharing and copying clauses, offloading to accelerators, and more.
4
5 Contributed by Diego Novillo <[email protected]>
6
7 Copyright (C) 2005-2025 Free Software Foundation, Inc.
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "tree-pass.h"
33 #include "ssa.h"
34 #include "cgraph.h"
35 #include "pretty-print.h"
36 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "internal-fn.h"
40 #include "gimple-iterator.h"
41 #include "gimple-fold.h"
42 #include "gimplify.h"
43 #include "gimplify-me.h"
44 #include "gimple-walk.h"
45 #include "tree-iterator.h"
46 #include "tree-inline.h"
47 #include "langhooks.h"
48 #include "tree-dfa.h"
49 #include "tree-ssa.h"
50 #include "splay-tree.h"
51 #include "omp-general.h"
52 #include "omp-low.h"
53 #include "gimple-low.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
56 #include "tree-nested.h"
57 #include "context.h"
58 #include "gomp-constants.h"
59 #include "gimple-pretty-print.h"
60 #include "stringpool.h"
61 #include "attribs.h"
62 #include "omp-offload.h"
63
64 /* Lowering of OMP parallel and workshare constructs proceeds in two
65 phases. The first phase scans the function looking for OMP statements
66 and then for variables that must be replaced to satisfy data sharing
67 clauses. The second phase expands code for the constructs, as well as
68 re-gimplifying things when variables have been replaced with complex
69 expressions.
70
71 Final code generation is done by pass_expand_omp. The flowgraph is
72 scanned for regions which are then moved to a new
73 function, to be invoked by the thread library, or offloaded. */
74
75 /* Context structure. Used to store information about each parallel
76 directive in the code. */
77
78 struct omp_context
79 {
80 /* This field must be at the beginning, as we do "inheritance": Some
81 callback functions for tree-inline.cc (e.g., omp_copy_decl)
82 receive a copy_body_data pointer that is up-casted to an
83 omp_context pointer. */
84 copy_body_data cb;
85
86 /* The tree of contexts corresponding to the encountered constructs. */
87 struct omp_context *outer;
88 gimple *stmt;
89
90 /* Map variables to fields in a structure that allows communication
91 between sending and receiving threads. */
92 splay_tree field_map;
93 tree record_type;
94 tree sender_decl;
95 tree receiver_decl;
96
97 /* These are used just by task contexts, if task firstprivate fn is
98 needed. srecord_type is used to communicate from the thread
99 that encountered the task construct to task firstprivate fn,
100 record_type is allocated by GOMP_task, initialized by task firstprivate
101 fn and passed to the task body fn. */
102 splay_tree sfield_map;
103 tree srecord_type;
104
105 /* A chain of variables to add to the top-level block surrounding the
106 construct. In the case of a parallel, this is in the child function. */
107 tree block_vars;
108
109 /* Label to which GOMP_cancel{,llation_point} and explicit and implicit
110 barriers should jump to during omplower pass. */
111 tree cancel_label;
112
113 /* The sibling GIMPLE_OMP_FOR simd with _simt_ clause or NULL
114 otherwise. */
115 gimple *simt_stmt;
116
117 /* For task reductions registered in this context, a vector containing
118 the length of the private copies block (if constant, otherwise NULL)
119 and then offsets (if constant, otherwise NULL) for each entry. */
120 vec<tree> task_reductions;
121
122 /* A hash map from the reduction clauses to the registered array
123 elts. */
124 hash_map<tree, unsigned> *task_reduction_map;
125
126 /* And a hash map from the lastprivate(conditional:) variables to their
127 corresponding tracking loop iteration variables. */
128 hash_map<tree, tree> *lastprivate_conditional_map;
129
130 /* And a hash map from the allocate variables to their corresponding
131 allocators. */
132 hash_map<tree, tree> *allocate_map;
133
134 /* A tree_list of the reduction clauses in this context. This is
135 only used for checking the consistency of OpenACC reduction
136 clauses in scan_omp_for and is not guaranteed to contain a valid
137 value outside of this function. */
138 tree local_reduction_clauses;
139
140 /* A tree_list of the reduction clauses in outer contexts. This is
141 only used for checking the consistency of OpenACC reduction
142 clauses in scan_omp_for and is not guaranteed to contain a valid
143 value outside of this function. */
144 tree outer_reduction_clauses;
145
146 /* Nesting depth of this context. Used to beautify error messages re
147 invalid gotos. The outermost ctx is depth 1, with depth 0 being
148 reserved for the main body of the function. */
149 int depth;
150
151 /* True if this parallel directive is nested within another. */
152 bool is_nested;
153
154 /* True if this construct can be cancelled. */
155 bool cancellable;
156
157 /* True if lower_omp_1 should look up lastprivate conditional in parent
158 context. */
159 bool combined_into_simd_safelen1;
160
161 /* True if there is nested scan context with inclusive clause. */
162 bool scan_inclusive;
163
164 /* True if there is nested scan context with exclusive clause. */
165 bool scan_exclusive;
166
167 /* True in the second simd loop of for simd with inscan reductions. */
168 bool for_simd_scan_phase;
169
170 /* True if there is order(concurrent) clause on the construct. */
171 bool order_concurrent;
172
173 /* True if there is bind clause on the construct (i.e. a loop construct). */
174 bool loop_p;
175
176 /* Only used for omp target contexts. True if a teams construct is
177 strictly nested in it. */
178 bool teams_nested_p;
179
180 /* Only used for omp target contexts. True if an OpenMP construct other
181 than teams is strictly nested in it. */
182 bool nonteams_nested_p;
183
184 /* Candidates for adjusting OpenACC privatization level. */
185 vec<tree> oacc_privatization_candidates;
186 };
187
188 static splay_tree all_contexts;
189 static int taskreg_nesting_level;
190 static int target_nesting_level;
191 static bitmap make_addressable_vars;
192 static bitmap global_nonaddressable_vars;
193 static vec<omp_context *> taskreg_contexts;
194 static vec<gomp_task *> task_cpyfns;
195
196 static void scan_omp (gimple_seq *, omp_context *);
197 static tree scan_omp_1_op (tree *, int *, void *);
198 static bool omp_maybe_offloaded_ctx (omp_context *ctx);
199
200 #define WALK_SUBSTMTS \
201 case GIMPLE_BIND: \
202 case GIMPLE_TRY: \
203 case GIMPLE_CATCH: \
204 case GIMPLE_EH_FILTER: \
205 case GIMPLE_ASSUME: \
206 case GIMPLE_TRANSACTION: \
207 /* The sub-statements for these should be walked. */ \
208 *handled_ops_p = false; \
209 break;
210
211 /* Return whether CTX represents an OpenACC 'parallel' or 'serial' construct.
212 (This doesn't include OpenACC 'kernels' decomposed parts.) */
213
214 static bool
215 is_oacc_parallel_or_serial (omp_context *ctx)
216 {
217 enum gimple_code outer_type = gimple_code (ctx->stmt);
218 return ((outer_type == GIMPLE_OMP_TARGET)
219 && ((gimple_omp_target_kind (ctx->stmt)
220 == GF_OMP_TARGET_KIND_OACC_PARALLEL)
221 || (gimple_omp_target_kind (ctx->stmt)
222 == GF_OMP_TARGET_KIND_OACC_SERIAL)));
223 }
224
225 /* Return whether CTX represents an OpenACC 'kernels' construct.
226 (This doesn't include OpenACC 'kernels' decomposed parts.) */
227
228 static bool
229 is_oacc_kernels (omp_context *ctx)
230 {
231 enum gimple_code outer_type = gimple_code (ctx->stmt);
232 return ((outer_type == GIMPLE_OMP_TARGET)
233 && (gimple_omp_target_kind (ctx->stmt)
234 == GF_OMP_TARGET_KIND_OACC_KERNELS));
235 }
236
237 /* Return whether CTX represents an OpenACC 'kernels' decomposed part. */
238
239 static bool
240 is_oacc_kernels_decomposed_part (omp_context *ctx)
241 {
242 enum gimple_code outer_type = gimple_code (ctx->stmt);
243 return ((outer_type == GIMPLE_OMP_TARGET)
244 && ((gimple_omp_target_kind (ctx->stmt)
245 == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED)
246 || (gimple_omp_target_kind (ctx->stmt)
247 == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE)
248 || (gimple_omp_target_kind (ctx->stmt)
249 == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS)));
250 }
251
252 /* Return true if STMT corresponds to an OpenMP target region. */
253 static bool
254 is_omp_target (gimple *stmt)
255 {
256 if (gimple_code (stmt) == GIMPLE_OMP_TARGET)
257 {
258 int kind = gimple_omp_target_kind (stmt);
259 return (kind == GF_OMP_TARGET_KIND_REGION
260 || kind == GF_OMP_TARGET_KIND_DATA
261 || kind == GF_OMP_TARGET_KIND_ENTER_DATA
262 || kind == GF_OMP_TARGET_KIND_EXIT_DATA);
263 }
264 return false;
265 }
266
267 /* If DECL is the artificial dummy VAR_DECL created for non-static
268 data member privatization, return the underlying "this" parameter,
269 otherwise return NULL. */
270
271 tree
272 omp_member_access_dummy_var (tree decl)
273 {
274 if (!VAR_P (decl)
275 || !DECL_ARTIFICIAL (decl)
276 || !DECL_IGNORED_P (decl)
277 || !DECL_HAS_VALUE_EXPR_P (decl)
278 || !lang_hooks.decls.omp_disregard_value_expr (decl, false))
279 return NULL_TREE;
280
281 tree v = DECL_VALUE_EXPR (decl);
282 if (TREE_CODE (v) != COMPONENT_REF)
283 return NULL_TREE;
284
285 while (1)
286 switch (TREE_CODE (v))
287 {
288 case COMPONENT_REF:
289 case MEM_REF:
290 case INDIRECT_REF:
291 CASE_CONVERT:
292 case POINTER_PLUS_EXPR:
293 v = TREE_OPERAND (v, 0);
294 continue;
295 case PARM_DECL:
296 if (DECL_CONTEXT (v) == current_function_decl
297 && DECL_ARTIFICIAL (v)
298 && TREE_CODE (TREE_TYPE (v)) == POINTER_TYPE)
299 return v;
300 return NULL_TREE;
301 default:
302 return NULL_TREE;
303 }
304 }
305
306 /* Helper for unshare_and_remap, called through walk_tree. */
307
308 static tree
309 unshare_and_remap_1 (tree *tp, int *walk_subtrees, void *data)
310 {
311 tree *pair = (tree *) data;
312 if (*tp == pair[0])
313 {
314 *tp = unshare_expr (pair[1]);
315 *walk_subtrees = 0;
316 }
317 else if (IS_TYPE_OR_DECL_P (*tp))
318 *walk_subtrees = 0;
319 return NULL_TREE;
320 }
321
322 /* Return unshare_expr (X) with all occurrences of FROM
323 replaced with TO. */
324
325 static tree
326 unshare_and_remap (tree x, tree from, tree to)
327 {
328 tree pair[2] = { from, to };
329 x = unshare_expr (x);
330 walk_tree (&x, unshare_and_remap_1, pair, NULL);
331 return x;
332 }
333
334 /* Convenience function for calling scan_omp_1_op on tree operands. */
335
336 static inline tree
337 scan_omp_op (tree *tp, omp_context *ctx)
338 {
339 struct walk_stmt_info wi;
340
341 memset (&wi, 0, sizeof (wi));
342 wi.info = ctx;
343 wi.want_locations = true;
344
345 return walk_tree (tp, scan_omp_1_op, &wi, NULL);
346 }
347
348 static void lower_omp (gimple_seq *, omp_context *);
349 static tree lookup_decl_in_outer_ctx (tree, omp_context *);
350 static tree maybe_lookup_decl_in_outer_ctx (tree, omp_context *);
351
352 /* Return true if CTX is for an omp parallel. */
353
354 static inline bool
355 is_parallel_ctx (omp_context *ctx)
356 {
357 return gimple_code (ctx->stmt) == GIMPLE_OMP_PARALLEL;
358 }
359
360
361 /* Return true if CTX is for an omp task. */
362
363 static inline bool
364 is_task_ctx (omp_context *ctx)
365 {
366 return gimple_code (ctx->stmt) == GIMPLE_OMP_TASK;
367 }
368
369
370 /* Return true if CTX is for an omp taskloop. */
371
372 static inline bool
373 is_taskloop_ctx (omp_context *ctx)
374 {
375 return gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
376 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_TASKLOOP;
377 }
378
379
380 /* Return true if CTX is for a host omp teams. */
381
382 static inline bool
383 is_host_teams_ctx (omp_context *ctx)
384 {
385 return gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
386 && gimple_omp_teams_host (as_a <gomp_teams *> (ctx->stmt));
387 }
388
389 /* Return true if CTX is for an omp parallel or omp task or host omp teams
390 (the last one is strictly not a task region in OpenMP speak, but we
391 need to treat it similarly). */
392
393 static inline bool
394 is_taskreg_ctx (omp_context *ctx)
395 {
396 return is_parallel_ctx (ctx) || is_task_ctx (ctx) || is_host_teams_ctx (ctx);
397 }
398
399 /* Return true if EXPR is variable sized. */
400
401 static inline bool
402 is_variable_sized (const_tree expr)
403 {
404 return !TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
405 }
406
407 /* Lookup variables. The "maybe" form
408 allows for the variable form to not have been entered, otherwise we
409 assert that the variable must have been entered. */
410
411 static inline tree
412 lookup_decl (tree var, omp_context *ctx)
413 {
414 tree *n = ctx->cb.decl_map->get (var);
415 return *n;
416 }
417
418 static inline tree
419 maybe_lookup_decl (const_tree var, omp_context *ctx)
420 {
421 tree *n = ctx->cb.decl_map->get (const_cast<tree> (var));
422 return n ? *n : NULL_TREE;
423 }
424
425 static inline tree
426 lookup_field (tree var, omp_context *ctx)
427 {
428 splay_tree_node n;
429 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) var);
430 return (tree) n->value;
431 }
432
433 static inline tree
434 lookup_sfield (splay_tree_key key, omp_context *ctx)
435 {
436 splay_tree_node n;
437 n = splay_tree_lookup (ctx->sfield_map
438 ? ctx->sfield_map : ctx->field_map, key);
439 return (tree) n->value;
440 }
441
442 static inline tree
443 lookup_sfield (tree var, omp_context *ctx)
444 {
445 return lookup_sfield ((splay_tree_key) var, ctx);
446 }
447
448 static inline tree
449 maybe_lookup_field (splay_tree_key key, omp_context *ctx)
450 {
451 splay_tree_node n;
452 n = splay_tree_lookup (ctx->field_map, key);
453 return n ? (tree) n->value : NULL_TREE;
454 }
455
456 static inline tree
457 maybe_lookup_field (tree var, omp_context *ctx)
458 {
459 return maybe_lookup_field ((splay_tree_key) var, ctx);
460 }
461
462 /* Return true if DECL should be copied by pointer. SHARED_CTX is
463 the parallel context if DECL is to be shared. */
464
465 static bool
466 use_pointer_for_field (tree decl, omp_context *shared_ctx)
467 {
468 if (AGGREGATE_TYPE_P (TREE_TYPE (decl))
469 || TYPE_ATOMIC (TREE_TYPE (decl))
470 || POLY_INT_CST_P (DECL_SIZE (decl)))
471 return true;
472
473 /* We can only use copy-in/copy-out semantics for shared variables
474 when we know the value is not accessible from an outer scope. */
475 if (shared_ctx)
476 {
477 gcc_assert (!is_gimple_omp_oacc (shared_ctx->stmt));
478
479 /* ??? Trivially accessible from anywhere. But why would we even
480 be passing an address in this case? Should we simply assert
481 this to be false, or should we have a cleanup pass that removes
482 these from the list of mappings? */
483 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, shared_ctx)))
484 return true;
485
486 /* For variables with DECL_HAS_VALUE_EXPR_P set, we cannot tell
487 without analyzing the expression whether or not its location
488 is accessible to anyone else. In the case of nested parallel
489 regions it certainly may be. */
490 if (TREE_CODE (decl) != RESULT_DECL && DECL_HAS_VALUE_EXPR_P (decl))
491 return true;
492
493 /* Do not use copy-in/copy-out for variables that have their
494 address taken. */
495 if (is_global_var (decl))
496 {
497 /* For file scope vars, track whether we've seen them as
498 non-addressable initially and in that case, keep the same
499 answer for the duration of the pass, even when they are made
500 addressable later on e.g. through reduction expansion. Global
501 variables which weren't addressable before the pass will not
502 have their privatized copies address taken. See PR91216. */
503 if (!TREE_ADDRESSABLE (decl))
504 {
505 if (!global_nonaddressable_vars)
506 global_nonaddressable_vars = BITMAP_ALLOC (NULL);
507 bitmap_set_bit (global_nonaddressable_vars, DECL_UID (decl));
508 }
509 else if (!global_nonaddressable_vars
510 || !bitmap_bit_p (global_nonaddressable_vars,
511 DECL_UID (decl)))
512 return true;
513 }
514 else if (TREE_ADDRESSABLE (decl))
515 return true;
516
517 /* lower_send_shared_vars only uses copy-in, but not copy-out
518 for these. */
519 if (TREE_READONLY (decl)
520 || ((TREE_CODE (decl) == RESULT_DECL
521 || TREE_CODE (decl) == PARM_DECL)
522 && DECL_BY_REFERENCE (decl)))
523 return false;
524
525 /* Disallow copy-in/out in nested parallel if
526 decl is shared in outer parallel, otherwise
527 each thread could store the shared variable
528 in its own copy-in location, making the
529 variable no longer really shared. */
530 if (shared_ctx->is_nested)
531 {
532 omp_context *up;
533
534 for (up = shared_ctx->outer; up; up = up->outer)
535 if ((is_taskreg_ctx (up)
536 || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
537 && is_gimple_omp_offloaded (up->stmt)))
538 && maybe_lookup_decl (decl, up))
539 break;
540
541 if (up)
542 {
543 tree c;
544
545 if (gimple_code (up->stmt) == GIMPLE_OMP_TARGET)
546 {
547 for (c = gimple_omp_target_clauses (up->stmt);
548 c; c = OMP_CLAUSE_CHAIN (c))
549 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
550 && OMP_CLAUSE_DECL (c) == decl)
551 break;
552 }
553 else
554 for (c = gimple_omp_taskreg_clauses (up->stmt);
555 c; c = OMP_CLAUSE_CHAIN (c))
556 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
557 && OMP_CLAUSE_DECL (c) == decl)
558 break;
559
560 if (c)
561 goto maybe_mark_addressable_and_ret;
562 }
563 }
564
565 /* For tasks avoid using copy-in/out. As tasks can be
566 deferred or executed in different thread, when GOMP_task
567 returns, the task hasn't necessarily terminated. */
568 if (is_task_ctx (shared_ctx))
569 {
570 tree outer;
571 maybe_mark_addressable_and_ret:
572 outer = maybe_lookup_decl_in_outer_ctx (decl, shared_ctx);
573 if (is_gimple_reg (outer) && !omp_member_access_dummy_var (outer))
574 {
575 /* Taking address of OUTER in lower_send_shared_vars
576 might need regimplification of everything that uses the
577 variable. */
578 if (!make_addressable_vars)
579 make_addressable_vars = BITMAP_ALLOC (NULL);
580 bitmap_set_bit (make_addressable_vars, DECL_UID (outer));
581 TREE_ADDRESSABLE (outer) = 1;
582 }
583 return true;
584 }
585 }
586
587 return false;
588 }
589
590 /* Construct a new automatic decl similar to VAR. */
591
592 static tree
593 omp_copy_decl_2 (tree var, tree name, tree type, omp_context *ctx)
594 {
595 tree copy = copy_var_decl (var, name, type);
596
597 DECL_CONTEXT (copy) = current_function_decl;
598
599 if (ctx)
600 {
601 DECL_CHAIN (copy) = ctx->block_vars;
602 ctx->block_vars = copy;
603 }
604 else
605 record_vars (copy);
606
607 /* If VAR is listed in make_addressable_vars, it wasn't
608 originally addressable, but was only later made so.
609 We don't need to take address of privatizations
610 from that var. */
611 if (TREE_ADDRESSABLE (var)
612 && ((make_addressable_vars
613 && bitmap_bit_p (make_addressable_vars, DECL_UID (var)))
614 || (global_nonaddressable_vars
615 && bitmap_bit_p (global_nonaddressable_vars, DECL_UID (var)))))
616 TREE_ADDRESSABLE (copy) = 0;
617
618 return copy;
619 }
620
621 static tree
622 omp_copy_decl_1 (tree var, omp_context *ctx)
623 {
624 return omp_copy_decl_2 (var, DECL_NAME (var), TREE_TYPE (var), ctx);
625 }
626
627 /* Build tree nodes to access the field for VAR on the receiver side. */
628
629 static tree
630 build_receiver_ref (tree var, bool by_ref, omp_context *ctx)
631 {
632 tree x, field = lookup_field (var, ctx);
633
634 /* If the receiver record type was remapped in the child function,
635 remap the field into the new record type. */
636 x = maybe_lookup_field (field, ctx);
637 if (x != NULL)
638 field = x;
639
640 x = build_simple_mem_ref (ctx->receiver_decl);
641 TREE_THIS_NOTRAP (x) = 1;
642 x = omp_build_component_ref (x, field);
643 if (by_ref)
644 {
645 x = build_simple_mem_ref (x);
646 TREE_THIS_NOTRAP (x) = 1;
647 }
648
649 return x;
650 }
651
652 /* Build tree nodes to access VAR in the scope outer to CTX. In the case
653 of a parallel, this is a component reference; for workshare constructs
654 this is some variable. */
655
656 static tree
657 build_outer_var_ref (tree var, omp_context *ctx,
658 enum omp_clause_code code = OMP_CLAUSE_ERROR)
659 {
660 tree x;
661 omp_context *outer = ctx->outer;
662 for (; outer; outer = outer->outer)
663 {
664 if (gimple_code (outer->stmt) == GIMPLE_OMP_TASKGROUP)
665 continue;
666 if (gimple_code (outer->stmt) == GIMPLE_OMP_SCOPE
667 && !maybe_lookup_decl (var, outer))
668 continue;
669 break;
670 }
671
672 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx)))
673 x = var;
674 else if (is_variable_sized (var))
675 {
676 x = TREE_OPERAND (DECL_VALUE_EXPR (var), 0);
677 x = build_outer_var_ref (x, ctx, code);
678 x = build_simple_mem_ref (x);
679 }
680 else if (is_taskreg_ctx (ctx))
681 {
682 bool by_ref = use_pointer_for_field (var, NULL);
683 x = build_receiver_ref (var, by_ref, ctx);
684 }
685 else if ((gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
686 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
687 || ctx->loop_p
688 || code == OMP_CLAUSE_ALLOCATE
689 || (code == OMP_CLAUSE_PRIVATE
690 && (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
691 || gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS
692 || gimple_code (ctx->stmt) == GIMPLE_OMP_SINGLE)))
693 {
694 /* #pragma omp simd isn't a worksharing construct, and can reference
695 even private vars in its linear etc. clauses.
696 Similarly for OMP_CLAUSE_PRIVATE with outer ref, that can refer
697 to private vars in all worksharing constructs. */
698 x = NULL_TREE;
699 if (outer && is_taskreg_ctx (outer))
700 x = lookup_decl (var, outer);
701 else if (outer)
702 x = maybe_lookup_decl_in_outer_ctx (var, ctx);
703 if (x == NULL_TREE)
704 x = var;
705 }
706 else if (code == OMP_CLAUSE_LASTPRIVATE && is_taskloop_ctx (ctx))
707 {
708 gcc_assert (outer);
709 splay_tree_node n
710 = splay_tree_lookup (outer->field_map,
711 (splay_tree_key) &DECL_UID (var));
712 if (n == NULL)
713 {
714 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, outer)))
715 x = var;
716 else
717 x = lookup_decl (var, outer);
718 }
719 else
720 {
721 tree field = (tree) n->value;
722 /* If the receiver record type was remapped in the child function,
723 remap the field into the new record type. */
724 x = maybe_lookup_field (field, outer);
725 if (x != NULL)
726 field = x;
727
728 x = build_simple_mem_ref (outer->receiver_decl);
729 x = omp_build_component_ref (x, field);
730 if (use_pointer_for_field (var, outer))
731 x = build_simple_mem_ref (x);
732 }
733 }
734 else if (outer)
735 x = lookup_decl (var, outer);
736 else if (omp_privatize_by_reference (var))
737 /* This can happen with orphaned constructs. If var is reference, it is
738 possible it is shared and as such valid. */
739 x = var;
740 else if (omp_member_access_dummy_var (var))
741 x = var;
742 else
743 gcc_unreachable ();
744
745 if (x == var)
746 {
747 tree t = omp_member_access_dummy_var (var);
748 if (t)
749 {
750 x = DECL_VALUE_EXPR (var);
751 tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
752 if (o != t)
753 x = unshare_and_remap (x, t, o);
754 else
755 x = unshare_expr (x);
756 }
757 }
758
759 if (omp_privatize_by_reference (var))
760 x = build_simple_mem_ref (x);
761
762 return x;
763 }
764
765 /* Build tree nodes to access the field for VAR on the sender side. */
766
767 static tree
768 build_sender_ref (splay_tree_key key, omp_context *ctx)
769 {
770 tree field = lookup_sfield (key, ctx);
771 tree tmp = ctx->sender_decl;
772 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
773 tmp = build_fold_indirect_ref (tmp);
774 return omp_build_component_ref (tmp, field);
775 }
776
777 static tree
778 build_sender_ref (tree var, omp_context *ctx)
779 {
780 return build_sender_ref ((splay_tree_key) var, ctx);
781 }
782
783 /* Add a new field for VAR inside the structure CTX->SENDER_DECL. If
784 BASE_POINTERS_RESTRICT, declare the field with restrict. */
785
786 static void
787 install_var_field (tree var, bool by_ref, int mask, omp_context *ctx)
788 {
789 tree field, type, sfield = NULL_TREE;
790 splay_tree_key key = (splay_tree_key) var;
791
792 if ((mask & 16) != 0)
793 {
794 key = (splay_tree_key) &DECL_NAME (var);
795 gcc_checking_assert (key != (splay_tree_key) var);
796 }
797 if ((mask & 8) != 0)
798 {
799 key = (splay_tree_key) &DECL_UID (var);
800 gcc_checking_assert (key != (splay_tree_key) var);
801 }
802 gcc_assert ((mask & 1) == 0
803 || !splay_tree_lookup (ctx->field_map, key));
804 gcc_assert ((mask & 2) == 0 || !ctx->sfield_map
805 || !splay_tree_lookup (ctx->sfield_map, key));
806 gcc_assert ((mask & 3) == 3
807 || !is_gimple_omp_oacc (ctx->stmt));
808
809 type = TREE_TYPE (var);
810 if ((mask & 16) != 0)
811 type = lang_hooks.decls.omp_array_data (var, true);
812
813 /* Prevent redeclaring the var in the split-off function with a restrict
814 pointer type. Note that we only clear type itself, restrict qualifiers in
815 the pointed-to type will be ignored by points-to analysis. */
816 if (POINTER_TYPE_P (type)
817 && TYPE_RESTRICT (type))
818 type = build_qualified_type (type, TYPE_QUALS (type) & ~TYPE_QUAL_RESTRICT);
819
820 if (mask & 4)
821 {
822 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
823 type = build_pointer_type (build_pointer_type (type));
824 }
825 else if (by_ref)
826 type = build_pointer_type (type);
827 else if ((mask & (32 | 3)) == 1
828 && omp_privatize_by_reference (var))
829 type = TREE_TYPE (type);
830
831 field = build_decl (DECL_SOURCE_LOCATION (var),
832 FIELD_DECL, DECL_NAME (var), type);
833
834 /* Remember what variable this field was created for. This does have a
835 side effect of making dwarf2out ignore this member, so for helpful
836 debugging we clear it later in delete_omp_context. */
837 DECL_ABSTRACT_ORIGIN (field) = var;
838 if ((mask & 16) == 0 && type == TREE_TYPE (var))
839 {
840 SET_DECL_ALIGN (field, DECL_ALIGN (var));
841 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (var);
842 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (var);
843 }
844 else
845 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
846
847 if ((mask & 3) == 3)
848 {
849 insert_field_into_struct (ctx->record_type, field);
850 if (ctx->srecord_type)
851 {
852 sfield = build_decl (DECL_SOURCE_LOCATION (var),
853 FIELD_DECL, DECL_NAME (var), type);
854 DECL_ABSTRACT_ORIGIN (sfield) = var;
855 SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
856 DECL_USER_ALIGN (sfield) = DECL_USER_ALIGN (field);
857 TREE_THIS_VOLATILE (sfield) = TREE_THIS_VOLATILE (field);
858 insert_field_into_struct (ctx->srecord_type, sfield);
859 }
860 }
861 else
862 {
863 if (ctx->srecord_type == NULL_TREE)
864 {
865 tree t;
866
867 ctx->srecord_type = lang_hooks.types.make_type (RECORD_TYPE);
868 ctx->sfield_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
869 for (t = TYPE_FIELDS (ctx->record_type); t ; t = TREE_CHAIN (t))
870 {
871 sfield = build_decl (DECL_SOURCE_LOCATION (t),
872 FIELD_DECL, DECL_NAME (t), TREE_TYPE (t));
873 DECL_ABSTRACT_ORIGIN (sfield) = DECL_ABSTRACT_ORIGIN (t);
874 insert_field_into_struct (ctx->srecord_type, sfield);
875 splay_tree_insert (ctx->sfield_map,
876 (splay_tree_key) DECL_ABSTRACT_ORIGIN (t),
877 (splay_tree_value) sfield);
878 }
879 }
880 sfield = field;
881 insert_field_into_struct ((mask & 1) ? ctx->record_type
882 : ctx->srecord_type, field);
883 }
884
885 if (mask & 1)
886 splay_tree_insert (ctx->field_map, key, (splay_tree_value) field);
887 if ((mask & 2) && ctx->sfield_map)
888 splay_tree_insert (ctx->sfield_map, key, (splay_tree_value) sfield);
889 }
890
891 static tree
892 install_var_local (tree var, omp_context *ctx)
893 {
894 tree new_var = omp_copy_decl_1 (var, ctx);
895 insert_decl_map (&ctx->cb, var, new_var);
896 return new_var;
897 }
898
899 /* Adjust the replacement for DECL in CTX for the new context. This means
900 copying the DECL_VALUE_EXPR, and fixing up the type. */
901
902 static void
903 fixup_remapped_decl (tree decl, omp_context *ctx, bool private_debug)
904 {
905 tree new_decl, size;
906
907 new_decl = lookup_decl (decl, ctx);
908
909 TREE_TYPE (new_decl) = remap_type (TREE_TYPE (decl), &ctx->cb);
910
911 if ((!TREE_CONSTANT (DECL_SIZE (new_decl)) || private_debug)
912 && DECL_HAS_VALUE_EXPR_P (decl))
913 {
914 tree ve = DECL_VALUE_EXPR (decl);
915 walk_tree (&ve, copy_tree_body_r, &ctx->cb, NULL);
916 SET_DECL_VALUE_EXPR (new_decl, ve);
917 DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
918 }
919
920 if (!TREE_CONSTANT (DECL_SIZE (new_decl)))
921 {
922 size = remap_decl (DECL_SIZE (decl), &ctx->cb);
923 if (size == error_mark_node)
924 size = TYPE_SIZE (TREE_TYPE (new_decl));
925 DECL_SIZE (new_decl) = size;
926
927 size = remap_decl (DECL_SIZE_UNIT (decl), &ctx->cb);
928 if (size == error_mark_node)
929 size = TYPE_SIZE_UNIT (TREE_TYPE (new_decl));
930 DECL_SIZE_UNIT (new_decl) = size;
931 }
932 }
933
934 /* The callback for remap_decl. Search all containing contexts for a
935 mapping of the variable; this avoids having to duplicate the splay
936 tree ahead of time. We know a mapping doesn't already exist in the
937 given context. Create new mappings to implement default semantics. */
938
939 static tree
940 omp_copy_decl (tree var, copy_body_data *cb)
941 {
942 omp_context *ctx = (omp_context *) cb;
943 tree new_var;
944
945 if (TREE_CODE (var) == LABEL_DECL)
946 {
947 if (FORCED_LABEL (var) || DECL_NONLOCAL (var))
948 return var;
949 new_var = create_artificial_label (DECL_SOURCE_LOCATION (var));
950 DECL_CONTEXT (new_var) = current_function_decl;
951 insert_decl_map (&ctx->cb, var, new_var);
952 return new_var;
953 }
954
955 while (!is_taskreg_ctx (ctx))
956 {
957 ctx = ctx->outer;
958 if (ctx == NULL)
959 return var;
960 new_var = maybe_lookup_decl (var, ctx);
961 if (new_var)
962 return new_var;
963 }
964
965 if (is_global_var (var) || decl_function_context (var) != ctx->cb.src_fn)
966 return var;
967
968 return error_mark_node;
969 }
970
971 /* Create a new context, with OUTER_CTX being the surrounding context. */
972
973 static omp_context *
974 new_omp_context (gimple *stmt, omp_context *outer_ctx)
975 {
976 omp_context *ctx = XCNEW (omp_context);
977
978 splay_tree_insert (all_contexts, (splay_tree_key) stmt,
979 (splay_tree_value) ctx);
980 ctx->stmt = stmt;
981
982 if (outer_ctx)
983 {
984 ctx->outer = outer_ctx;
985 ctx->cb = outer_ctx->cb;
986 ctx->cb.block = NULL;
987 ctx->depth = outer_ctx->depth + 1;
988 }
989 else
990 {
991 ctx->cb.src_fn = current_function_decl;
992 ctx->cb.dst_fn = current_function_decl;
993 ctx->cb.src_node = cgraph_node::get (current_function_decl);
994 gcc_checking_assert (ctx->cb.src_node);
995 ctx->cb.dst_node = ctx->cb.src_node;
996 ctx->cb.src_cfun = cfun;
997 ctx->cb.copy_decl = omp_copy_decl;
998 ctx->cb.eh_lp_nr = 0;
999 ctx->cb.transform_call_graph_edges = CB_CGE_MOVE;
1000 ctx->cb.adjust_array_error_bounds = true;
1001 ctx->cb.dont_remap_vla_if_no_change = true;
1002 ctx->depth = 1;
1003 }
1004
1005 ctx->cb.decl_map = new hash_map<tree, tree>;
1006
1007 return ctx;
1008 }
1009
1010 static gimple_seq maybe_catch_exception (gimple_seq);
1011
1012 /* Finalize task copyfn. */
1013
1014 static void
1015 finalize_task_copyfn (gomp_task *task_stmt)
1016 {
1017 struct function *child_cfun;
1018 tree child_fn;
1019 gimple_seq seq = NULL, new_seq;
1020 gbind *bind;
1021
1022 child_fn = gimple_omp_task_copy_fn (task_stmt);
1023 if (child_fn == NULL_TREE)
1024 return;
1025
1026 child_cfun = DECL_STRUCT_FUNCTION (child_fn);
1027 DECL_STRUCT_FUNCTION (child_fn)->curr_properties = cfun->curr_properties;
1028
1029 push_cfun (child_cfun);
1030 bind = gimplify_body (child_fn, false);
1031 gimple_seq_add_stmt (&seq, bind);
1032 new_seq = maybe_catch_exception (seq);
1033 if (new_seq != seq)
1034 {
1035 bind = gimple_build_bind (NULL, new_seq, NULL);
1036 seq = NULL;
1037 gimple_seq_add_stmt (&seq, bind);
1038 }
1039 gimple_set_body (child_fn, seq);
1040 pop_cfun ();
1041
1042 /* Inform the callgraph about the new function. */
1043 cgraph_node *node = cgraph_node::get_create (child_fn);
1044 node->parallelized_function = 1;
1045 cgraph_node::add_new_function (child_fn, false);
1046 }
1047
1048 /* Destroy a omp_context data structures. Called through the splay tree
1049 value delete callback. */
1050
1051 static void
1052 delete_omp_context (splay_tree_value value)
1053 {
1054 omp_context *ctx = (omp_context *) value;
1055
1056 delete ctx->cb.decl_map;
1057
1058 if (ctx->field_map)
1059 splay_tree_delete (ctx->field_map);
1060 if (ctx->sfield_map)
1061 splay_tree_delete (ctx->sfield_map);
1062
1063 /* We hijacked DECL_ABSTRACT_ORIGIN earlier. We need to clear it before
1064 it produces corrupt debug information. */
1065 if (ctx->record_type)
1066 {
1067 tree t;
1068 for (t = TYPE_FIELDS (ctx->record_type); t ; t = DECL_CHAIN (t))
1069 DECL_ABSTRACT_ORIGIN (t) = NULL;
1070 }
1071 if (ctx->srecord_type)
1072 {
1073 tree t;
1074 for (t = TYPE_FIELDS (ctx->srecord_type); t ; t = DECL_CHAIN (t))
1075 DECL_ABSTRACT_ORIGIN (t) = NULL;
1076 }
1077
1078 if (ctx->task_reduction_map)
1079 {
1080 ctx->task_reductions.release ();
1081 delete ctx->task_reduction_map;
1082 }
1083
1084 delete ctx->lastprivate_conditional_map;
1085 delete ctx->allocate_map;
1086
1087 XDELETE (ctx);
1088 }
1089
1090 /* Fix up RECEIVER_DECL with a type that has been remapped to the child
1091 context. */
1092
1093 static void
1094 fixup_child_record_type (omp_context *ctx)
1095 {
1096 tree f, type = ctx->record_type;
1097
1098 if (!ctx->receiver_decl)
1099 return;
1100 /* ??? It isn't sufficient to just call remap_type here, because
1101 variably_modified_type_p doesn't work the way we expect for
1102 record types. Testing each field for whether it needs remapping
1103 and creating a new record by hand works, however. */
1104 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
1105 if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
1106 break;
1107 if (f)
1108 {
1109 tree name, new_fields = NULL;
1110
1111 type = lang_hooks.types.make_type (RECORD_TYPE);
1112 name = DECL_NAME (TYPE_NAME (ctx->record_type));
1113 name = build_decl (DECL_SOURCE_LOCATION (ctx->receiver_decl),
1114 TYPE_DECL, name, type);
1115 TYPE_NAME (type) = name;
1116
1117 for (f = TYPE_FIELDS (ctx->record_type); f ; f = DECL_CHAIN (f))
1118 {
1119 tree new_f = copy_node (f);
1120 DECL_CONTEXT (new_f) = type;
1121 TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &ctx->cb);
1122 DECL_CHAIN (new_f) = new_fields;
1123 walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &ctx->cb, NULL);
1124 walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r,
1125 &ctx->cb, NULL);
1126 walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
1127 &ctx->cb, NULL);
1128 new_fields = new_f;
1129
1130 /* Arrange to be able to look up the receiver field
1131 given the sender field. */
1132 splay_tree_insert (ctx->field_map, (splay_tree_key) f,
1133 (splay_tree_value) new_f);
1134 }
1135 TYPE_FIELDS (type) = nreverse (new_fields);
1136 layout_type (type);
1137 }
1138
1139 /* In a target region we never modify any of the pointers in *.omp_data_i,
1140 so attempt to help the optimizers. */
1141 if (is_gimple_omp_offloaded (ctx->stmt))
1142 type = build_qualified_type (type, TYPE_QUAL_CONST);
1143
1144 TREE_TYPE (ctx->receiver_decl)
1145 = build_qualified_type (flexible_array_type_p (type)
1146 ? build_pointer_type (type)
1147 : build_reference_type (type), TYPE_QUAL_RESTRICT);
1148 }
1149
1150 /* Instantiate decls as necessary in CTX to satisfy the data sharing
1151 specified by CLAUSES. */
1152
1153 static void
1154 scan_sharing_clauses (tree clauses, omp_context *ctx)
1155 {
1156 tree c, decl;
1157 bool scan_array_reductions = false;
1158 bool flex_array_ptr = false;
1159
1160 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1161 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
1162 && (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
1163 /* omp_default_mem_alloc is 1 */
1164 || !integer_onep (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
1165 || OMP_CLAUSE_ALLOCATE_ALIGN (c) != NULL_TREE))
1166 {
1167 /* The allocate clauses that appear on a target construct or on
1168 constructs in a target region must specify an allocator expression
1169 unless a requires directive with the dynamic_allocators clause
1170 is present in the same compilation unit. */
1171 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
1172 && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
1173 && omp_maybe_offloaded_ctx (ctx))
1174 error_at (OMP_CLAUSE_LOCATION (c), "%<allocate%> clause must"
1175 " specify an allocator here");
1176 if (ctx->allocate_map == NULL)
1177 ctx->allocate_map = new hash_map<tree, tree>;
1178 tree val = integer_zero_node;
1179 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
1180 val = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
1181 if (OMP_CLAUSE_ALLOCATE_ALIGN (c))
1182 val = build_tree_list (val, OMP_CLAUSE_ALLOCATE_ALIGN (c));
1183 ctx->allocate_map->put (OMP_CLAUSE_DECL (c), val);
1184 }
1185
1186 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1187 {
1188 bool by_ref;
1189
1190 switch (OMP_CLAUSE_CODE (c))
1191 {
1192 case OMP_CLAUSE_PRIVATE:
1193 decl = OMP_CLAUSE_DECL (c);
1194 if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
1195 goto do_private;
1196 else if (!is_variable_sized (decl))
1197 install_var_local (decl, ctx);
1198 break;
1199
1200 case OMP_CLAUSE_SHARED:
1201 decl = OMP_CLAUSE_DECL (c);
1202 if (ctx->allocate_map && ctx->allocate_map->get (decl))
1203 ctx->allocate_map->remove (decl);
1204 /* Ignore shared directives in teams construct inside of
1205 target construct. */
1206 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
1207 && !is_host_teams_ctx (ctx))
1208 {
1209 /* Global variables don't need to be copied,
1210 the receiver side will use them directly. */
1211 tree odecl = maybe_lookup_decl_in_outer_ctx (decl, ctx);
1212 if (is_global_var (odecl))
1213 break;
1214 insert_decl_map (&ctx->cb, decl, odecl);
1215 break;
1216 }
1217 gcc_assert (is_taskreg_ctx (ctx));
1218 gcc_assert (!COMPLETE_TYPE_P (TREE_TYPE (decl))
1219 || !is_variable_sized (decl));
1220 /* Global variables don't need to be copied,
1221 the receiver side will use them directly. */
1222 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1223 break;
1224 if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
1225 {
1226 use_pointer_for_field (decl, ctx);
1227 break;
1228 }
1229 by_ref = use_pointer_for_field (decl, NULL);
1230 if ((! TREE_READONLY (decl) && !OMP_CLAUSE_SHARED_READONLY (c))
1231 || TREE_ADDRESSABLE (decl)
1232 || by_ref
1233 || omp_privatize_by_reference (decl))
1234 {
1235 by_ref = use_pointer_for_field (decl, ctx);
1236 install_var_field (decl, by_ref, 3, ctx);
1237 install_var_local (decl, ctx);
1238 break;
1239 }
1240 /* We don't need to copy const scalar vars back. */
1241 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_FIRSTPRIVATE);
1242 goto do_private;
1243
1244 case OMP_CLAUSE_REDUCTION:
1245 /* Collect 'reduction' clauses on OpenACC compute construct. */
1246 if (is_gimple_omp_oacc (ctx->stmt)
1247 && is_gimple_omp_offloaded (ctx->stmt))
1248 {
1249 /* No 'reduction' clauses on OpenACC 'kernels'. */
1250 gcc_checking_assert (!is_oacc_kernels (ctx));
1251 /* Likewise, on OpenACC 'kernels' decomposed parts. */
1252 gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
1253
1254 ctx->local_reduction_clauses
1255 = tree_cons (NULL, c, ctx->local_reduction_clauses);
1256 }
1257 /* FALLTHRU */
1258
1259 case OMP_CLAUSE_IN_REDUCTION:
1260 decl = OMP_CLAUSE_DECL (c);
1261 if (ctx->allocate_map
1262 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1263 && (OMP_CLAUSE_REDUCTION_INSCAN (c)
1264 || OMP_CLAUSE_REDUCTION_TASK (c)))
1265 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
1266 || is_task_ctx (ctx)))
1267 {
1268 /* For now. */
1269 if (ctx->allocate_map->get (decl))
1270 ctx->allocate_map->remove (decl);
1271 }
1272 if (TREE_CODE (decl) == MEM_REF)
1273 {
1274 tree t = TREE_OPERAND (decl, 0);
1275 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
1276 t = TREE_OPERAND (t, 0);
1277 if (INDIRECT_REF_P (t)
1278 || TREE_CODE (t) == ADDR_EXPR)
1279 t = TREE_OPERAND (t, 0);
1280 if (is_omp_target (ctx->stmt))
1281 {
1282 if (is_variable_sized (t))
1283 {
1284 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
1285 t = DECL_VALUE_EXPR (t);
1286 gcc_assert (INDIRECT_REF_P (t));
1287 t = TREE_OPERAND (t, 0);
1288 gcc_assert (DECL_P (t));
1289 }
1290 tree at = t;
1291 if (ctx->outer)
1292 scan_omp_op (&at, ctx->outer);
1293 tree nt = omp_copy_decl_1 (at, ctx->outer);
1294 splay_tree_insert (ctx->field_map,
1295 (splay_tree_key) &DECL_CONTEXT (t),
1296 (splay_tree_value) nt);
1297 if (at != t)
1298 splay_tree_insert (ctx->field_map,
1299 (splay_tree_key) &DECL_CONTEXT (at),
1300 (splay_tree_value) nt);
1301 break;
1302 }
1303 install_var_local (t, ctx);
1304 if (is_taskreg_ctx (ctx)
1305 && (!is_global_var (maybe_lookup_decl_in_outer_ctx (t, ctx))
1306 || (is_task_ctx (ctx)
1307 && (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1308 || (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
1309 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
1310 == POINTER_TYPE)))))
1311 && !is_variable_sized (t)
1312 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
1313 || (!OMP_CLAUSE_REDUCTION_TASK (c)
1314 && !is_task_ctx (ctx))))
1315 {
1316 by_ref = use_pointer_for_field (t, NULL);
1317 if (is_task_ctx (ctx)
1318 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
1319 && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == POINTER_TYPE)
1320 {
1321 install_var_field (t, false, 1, ctx);
1322 install_var_field (t, by_ref, 2, ctx);
1323 }
1324 else
1325 install_var_field (t, by_ref, 3, ctx);
1326 }
1327 break;
1328 }
1329 if (is_omp_target (ctx->stmt))
1330 {
1331 tree at = decl;
1332 if (ctx->outer)
1333 scan_omp_op (&at, ctx->outer);
1334 tree nt = omp_copy_decl_1 (at, ctx->outer);
1335 splay_tree_insert (ctx->field_map,
1336 (splay_tree_key) &DECL_CONTEXT (decl),
1337 (splay_tree_value) nt);
1338 if (at != decl)
1339 splay_tree_insert (ctx->field_map,
1340 (splay_tree_key) &DECL_CONTEXT (at),
1341 (splay_tree_value) nt);
1342 break;
1343 }
1344 if (is_task_ctx (ctx)
1345 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1346 && OMP_CLAUSE_REDUCTION_TASK (c)
1347 && is_parallel_ctx (ctx)))
1348 {
1349 /* Global variables don't need to be copied,
1350 the receiver side will use them directly. */
1351 if (!is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1352 {
1353 by_ref = use_pointer_for_field (decl, ctx);
1354 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
1355 install_var_field (decl, by_ref, 3, ctx);
1356 }
1357 install_var_local (decl, ctx);
1358 break;
1359 }
1360 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1361 && OMP_CLAUSE_REDUCTION_TASK (c))
1362 {
1363 install_var_local (decl, ctx);
1364 break;
1365 }
1366 goto do_private;
1367
1368 case OMP_CLAUSE_LASTPRIVATE:
1369 /* Let the corresponding firstprivate clause create
1370 the variable. */
1371 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1372 break;
1373 /* FALLTHRU */
1374
1375 case OMP_CLAUSE_FIRSTPRIVATE:
1376 case OMP_CLAUSE_LINEAR:
1377 decl = OMP_CLAUSE_DECL (c);
1378 do_private:
1379 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1380 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
1381 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1382 && is_gimple_omp_offloaded (ctx->stmt))
1383 {
1384 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1385 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
1386 && lang_hooks.decls.omp_array_data (decl, true)))
1387 {
1388 by_ref = !omp_privatize_by_reference (decl);
1389 install_var_field (decl, by_ref, 3, ctx);
1390 }
1391 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1392 {
1393 if (INDIRECT_REF_P (decl))
1394 decl = TREE_OPERAND (decl, 0);
1395 install_var_field (decl, true, 3, ctx);
1396 }
1397 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1398 install_var_field (decl, true, 3, ctx);
1399 else
1400 install_var_field (decl, false, 3, ctx);
1401 }
1402 if (is_variable_sized (decl))
1403 {
1404 if (is_task_ctx (ctx))
1405 {
1406 if (ctx->allocate_map
1407 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
1408 {
1409 /* For now. */
1410 if (ctx->allocate_map->get (decl))
1411 ctx->allocate_map->remove (decl);
1412 }
1413 install_var_field (decl, false, 1, ctx);
1414 }
1415 break;
1416 }
1417 else if (is_taskreg_ctx (ctx))
1418 {
1419 bool global
1420 = is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx));
1421 by_ref = use_pointer_for_field (decl, NULL);
1422
1423 if (is_task_ctx (ctx)
1424 && (global || by_ref || omp_privatize_by_reference (decl)))
1425 {
1426 if (ctx->allocate_map
1427 && ctx->allocate_map->get (decl))
1428 install_var_field (decl, by_ref, 32 | 1, ctx);
1429 else
1430 install_var_field (decl, false, 1, ctx);
1431 if (!global)
1432 install_var_field (decl, by_ref, 2, ctx);
1433 }
1434 else if (!global)
1435 install_var_field (decl, by_ref, 3, ctx);
1436 }
1437 install_var_local (decl, ctx);
1438 /* For descr arrays on target: firstprivatize data + attach ptr. */
1439 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1440 && is_gimple_omp_offloaded (ctx->stmt)
1441 && !is_gimple_omp_oacc (ctx->stmt)
1442 && lang_hooks.decls.omp_array_data (decl, true))
1443 {
1444 install_var_field (decl, false, 16 | 3, ctx);
1445 install_var_field (decl, true, 8 | 3, ctx);
1446 }
1447 break;
1448
1449 case OMP_CLAUSE_USE_DEVICE_PTR:
1450 case OMP_CLAUSE_USE_DEVICE_ADDR:
1451 decl = OMP_CLAUSE_DECL (c);
1452
1453 /* Fortran array descriptors. */
1454 if (lang_hooks.decls.omp_array_data (decl, true))
1455 install_var_field (decl, false, 19, ctx);
1456 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
1457 && !omp_privatize_by_reference (decl)
1458 && !omp_is_allocatable_or_ptr (decl))
1459 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1460 install_var_field (decl, true, 11, ctx);
1461 else
1462 install_var_field (decl, false, 11, ctx);
1463 if (DECL_SIZE (decl)
1464 && !poly_int_tree_p (DECL_SIZE (decl)))
1465 {
1466 tree decl2 = DECL_VALUE_EXPR (decl);
1467 gcc_assert (INDIRECT_REF_P (decl2));
1468 decl2 = TREE_OPERAND (decl2, 0);
1469 gcc_assert (DECL_P (decl2));
1470 install_var_local (decl2, ctx);
1471 }
1472 install_var_local (decl, ctx);
1473 break;
1474
1475 case OMP_CLAUSE_HAS_DEVICE_ADDR:
1476 decl = OMP_CLAUSE_DECL (c);
1477 while (INDIRECT_REF_P (decl)
1478 || TREE_CODE (decl) == ARRAY_REF)
1479 decl = TREE_OPERAND (decl, 0);
1480 goto do_private;
1481
1482 case OMP_CLAUSE_IS_DEVICE_PTR:
1483 decl = OMP_CLAUSE_DECL (c);
1484 goto do_private;
1485
1486 case OMP_CLAUSE__LOOPTEMP_:
1487 case OMP_CLAUSE__REDUCTEMP_:
1488 gcc_assert (is_taskreg_ctx (ctx));
1489 decl = OMP_CLAUSE_DECL (c);
1490 install_var_field (decl, false, 3, ctx);
1491 install_var_local (decl, ctx);
1492 break;
1493
1494 case OMP_CLAUSE_COPYPRIVATE:
1495 case OMP_CLAUSE_COPYIN:
1496 decl = OMP_CLAUSE_DECL (c);
1497 by_ref = use_pointer_for_field (decl, NULL);
1498 install_var_field (decl, by_ref, 3, ctx);
1499 break;
1500
1501 case OMP_CLAUSE_FINAL:
1502 case OMP_CLAUSE_IF:
1503 case OMP_CLAUSE_SELF:
1504 case OMP_CLAUSE_NUM_THREADS:
1505 case OMP_CLAUSE_NUM_TEAMS:
1506 case OMP_CLAUSE_THREAD_LIMIT:
1507 case OMP_CLAUSE_DEVICE:
1508 case OMP_CLAUSE_SCHEDULE:
1509 case OMP_CLAUSE_DIST_SCHEDULE:
1510 case OMP_CLAUSE_DEPEND:
1511 case OMP_CLAUSE_PRIORITY:
1512 case OMP_CLAUSE_GRAINSIZE:
1513 case OMP_CLAUSE_NUM_TASKS:
1514 case OMP_CLAUSE_NUM_GANGS:
1515 case OMP_CLAUSE_NUM_WORKERS:
1516 case OMP_CLAUSE_VECTOR_LENGTH:
1517 case OMP_CLAUSE_DETACH:
1518 case OMP_CLAUSE_FILTER:
1519 if (ctx->outer)
1520 scan_omp_op (&OMP_CLAUSE_OPERAND (c, 0), ctx->outer);
1521 break;
1522
1523 case OMP_CLAUSE_TO:
1524 case OMP_CLAUSE_FROM:
1525 case OMP_CLAUSE_MAP:
1526 if (ctx->outer)
1527 scan_omp_op (&OMP_CLAUSE_SIZE (c), ctx->outer);
1528 decl = OMP_CLAUSE_DECL (c);
1529 /* If requested, make 'decl' addressable. */
1530 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1531 && OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c))
1532 {
1533 gcc_checking_assert (DECL_P (decl));
1534
1535 bool decl_addressable = TREE_ADDRESSABLE (decl);
1536 if (!decl_addressable)
1537 {
1538 if (!make_addressable_vars)
1539 make_addressable_vars = BITMAP_ALLOC (NULL);
1540 bitmap_set_bit (make_addressable_vars, DECL_UID (decl));
1541 TREE_ADDRESSABLE (decl) = 1;
1542 }
1543
1544 if (dump_enabled_p ())
1545 {
1546 location_t loc = OMP_CLAUSE_LOCATION (c);
1547 const dump_user_location_t d_u_loc
1548 = dump_user_location_t::from_location_t (loc);
1549 /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
1550 #if __GNUC__ >= 10
1551 # pragma GCC diagnostic push
1552 # pragma GCC diagnostic ignored "-Wformat"
1553 #endif
1554 if (!decl_addressable)
1555 dump_printf_loc (MSG_NOTE, d_u_loc,
1556 "variable %<%T%>"
1557 " made addressable\n",
1558 decl);
1559 else
1560 dump_printf_loc (MSG_NOTE, d_u_loc,
1561 "variable %<%T%>"
1562 " already made addressable\n",
1563 decl);
1564 #if __GNUC__ >= 10
1565 # pragma GCC diagnostic pop
1566 #endif
1567 }
1568
1569 /* Done. */
1570 OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c) = 0;
1571 }
1572 /* Global variables with "omp declare target" attribute
1573 don't need to be copied, the receiver side will use them
1574 directly. However, global variables with "omp declare target link"
1575 attribute need to be copied. Or when ALWAYS modifier is used. */
1576 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1577 && DECL_P (decl)
1578 && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
1579 && (OMP_CLAUSE_MAP_KIND (c)
1580 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
1581 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
1582 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)
1583 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1584 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TO
1585 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_FROM
1586 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TOFROM
1587 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TO
1588 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_FROM
1589 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TOFROM
1590 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
1591 && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
1592 && varpool_node::get_create (decl)->offloadable
1593 && !lookup_attribute ("omp declare target link",
1594 DECL_ATTRIBUTES (decl)))
1595 break;
1596 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1597 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
1598 {
1599 /* Ignore GOMP_MAP_POINTER kind for arrays in regions that are
1600 not offloaded; there is nothing to map for those. */
1601 if (!is_gimple_omp_offloaded (ctx->stmt)
1602 && !POINTER_TYPE_P (TREE_TYPE (decl))
1603 && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
1604 break;
1605 }
1606 if (!flex_array_ptr)
1607 flex_array_ptr = lang_hooks.decls.omp_deep_mapping_p (ctx->stmt, c);
1608 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1609 && DECL_P (decl)
1610 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
1611 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
1612 && is_omp_target (ctx->stmt))
1613 {
1614 /* If this is an offloaded region, an attach operation should
1615 only exist when the pointer variable is mapped in a prior
1616 clause. An exception is if we have a reference (to pointer):
1617 in that case we should have mapped "*decl" in a previous
1618 mapping instead of "decl". Skip the assertion in that case.
1619 If we had an error, we may not have attempted to sort clauses
1620 properly, so avoid the test. */
1621 if (TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
1622 && is_gimple_omp_offloaded (ctx->stmt)
1623 && !seen_error ())
1624 gcc_assert
1625 (maybe_lookup_decl (decl, ctx)
1626 || (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
1627 && lookup_attribute ("omp declare target",
1628 DECL_ATTRIBUTES (decl))));
1629
1630 /* By itself, attach/detach is generated as part of pointer
1631 variable mapping and should not create new variables in the
1632 offloaded region, however sender refs for it must be created
1633 for its address to be passed to the runtime. */
1634 tree field
1635 = build_decl (OMP_CLAUSE_LOCATION (c),
1636 FIELD_DECL, NULL_TREE, ptr_type_node);
1637 SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
1638 insert_field_into_struct (ctx->record_type, field);
1639 /* To not clash with a map of the pointer variable itself,
1640 attach/detach maps have their field looked up by the *clause*
1641 tree expression, not the decl. */
1642 gcc_assert (!splay_tree_lookup (ctx->field_map,
1643 (splay_tree_key) c));
1644 splay_tree_insert (ctx->field_map, (splay_tree_key) c,
1645 (splay_tree_value) field);
1646 break;
1647 }
1648 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1649 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
1650 || (OMP_CLAUSE_MAP_KIND (c)
1651 == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
1652 {
1653 if (TREE_CODE (decl) == COMPONENT_REF
1654 || (INDIRECT_REF_P (decl)
1655 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
1656 && (((TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
1657 == REFERENCE_TYPE)
1658 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
1659 == POINTER_TYPE)))))
1660 break;
1661 if (DECL_SIZE (decl)
1662 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1663 {
1664 tree decl2 = DECL_VALUE_EXPR (decl);
1665 gcc_assert (INDIRECT_REF_P (decl2));
1666 decl2 = TREE_OPERAND (decl2, 0);
1667 gcc_assert (DECL_P (decl2));
1668 install_var_local (decl2, ctx);
1669 }
1670 install_var_local (decl, ctx);
1671 break;
1672 }
1673 if (DECL_P (decl))
1674 {
1675 if (DECL_SIZE (decl)
1676 && !poly_int_tree_p (DECL_SIZE (decl)))
1677 {
1678 tree decl2 = DECL_VALUE_EXPR (decl);
1679 gcc_assert (INDIRECT_REF_P (decl2));
1680 decl2 = TREE_OPERAND (decl2, 0);
1681 gcc_assert (DECL_P (decl2));
1682 install_var_field (decl2, true, 3, ctx);
1683 install_var_local (decl2, ctx);
1684 install_var_local (decl, ctx);
1685 }
1686 else
1687 {
1688 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1689 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
1690 && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
1691 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1692 install_var_field (decl, true, 7, ctx);
1693 else
1694 install_var_field (decl, true, 3, ctx);
1695 if (is_gimple_omp_offloaded (ctx->stmt)
1696 && !(is_gimple_omp_oacc (ctx->stmt)
1697 && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
1698 install_var_local (decl, ctx);
1699 }
1700 }
1701 else
1702 {
1703 tree base = get_base_address (decl);
1704 tree nc = OMP_CLAUSE_CHAIN (c);
1705 if (DECL_P (base)
1706 && nc != NULL_TREE
1707 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
1708 && OMP_CLAUSE_DECL (nc) == base
1709 && OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
1710 && integer_zerop (OMP_CLAUSE_SIZE (nc)))
1711 {
1712 OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c) = 1;
1713 OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (nc) = 1;
1714 }
1715 else
1716 {
1717 if (ctx->outer)
1718 {
1719 scan_omp_op (&OMP_CLAUSE_DECL (c), ctx->outer);
1720 decl = OMP_CLAUSE_DECL (c);
1721 }
1722 gcc_assert (!splay_tree_lookup (ctx->field_map,
1723 (splay_tree_key) decl));
1724 tree field
1725 = build_decl (OMP_CLAUSE_LOCATION (c),
1726 FIELD_DECL, NULL_TREE, ptr_type_node);
1727 SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
1728 insert_field_into_struct (ctx->record_type, field);
1729 splay_tree_insert (ctx->field_map, (splay_tree_key) decl,
1730 (splay_tree_value) field);
1731 }
1732 }
1733 break;
1734
1735 case OMP_CLAUSE_ORDER:
1736 ctx->order_concurrent = true;
1737 break;
1738
1739 case OMP_CLAUSE_BIND:
1740 ctx->loop_p = true;
1741 break;
1742
1743 case OMP_CLAUSE_NOWAIT:
1744 case OMP_CLAUSE_ORDERED:
1745 case OMP_CLAUSE_COLLAPSE:
1746 case OMP_CLAUSE_UNTIED:
1747 case OMP_CLAUSE_MERGEABLE:
1748 case OMP_CLAUSE_PROC_BIND:
1749 case OMP_CLAUSE_SAFELEN:
1750 case OMP_CLAUSE_SIMDLEN:
1751 case OMP_CLAUSE_THREADS:
1752 case OMP_CLAUSE_SIMD:
1753 case OMP_CLAUSE_NOGROUP:
1754 case OMP_CLAUSE_DEFAULTMAP:
1755 case OMP_CLAUSE_ASYNC:
1756 case OMP_CLAUSE_WAIT:
1757 case OMP_CLAUSE_GANG:
1758 case OMP_CLAUSE_WORKER:
1759 case OMP_CLAUSE_VECTOR:
1760 case OMP_CLAUSE_INDEPENDENT:
1761 case OMP_CLAUSE_AUTO:
1762 case OMP_CLAUSE_SEQ:
1763 case OMP_CLAUSE_TILE:
1764 case OMP_CLAUSE__SIMT_:
1765 case OMP_CLAUSE_DEFAULT:
1766 case OMP_CLAUSE_NONTEMPORAL:
1767 case OMP_CLAUSE_IF_PRESENT:
1768 case OMP_CLAUSE_FINALIZE:
1769 case OMP_CLAUSE_TASK_REDUCTION:
1770 case OMP_CLAUSE_ALLOCATE:
1771 break;
1772
1773 case OMP_CLAUSE_ALIGNED:
1774 decl = OMP_CLAUSE_DECL (c);
1775 if (is_global_var (decl)
1776 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1777 install_var_local (decl, ctx);
1778 break;
1779
1780 case OMP_CLAUSE__CONDTEMP_:
1781 decl = OMP_CLAUSE_DECL (c);
1782 if (is_parallel_ctx (ctx))
1783 {
1784 install_var_field (decl, false, 3, ctx);
1785 install_var_local (decl, ctx);
1786 }
1787 else if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
1788 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
1789 && !OMP_CLAUSE__CONDTEMP__ITER (c))
1790 install_var_local (decl, ctx);
1791 break;
1792
1793 case OMP_CLAUSE_INIT:
1794 case OMP_CLAUSE_USE:
1795 case OMP_CLAUSE_DESTROY:
1796 break;
1797
1798 case OMP_CLAUSE__CACHE_:
1799 case OMP_CLAUSE_NOHOST:
1800 default:
1801 gcc_unreachable ();
1802 }
1803 }
1804
1805 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1806 {
1807 switch (OMP_CLAUSE_CODE (c))
1808 {
1809 case OMP_CLAUSE_LASTPRIVATE:
1810 /* Let the corresponding firstprivate clause create
1811 the variable. */
1812 if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
1813 scan_array_reductions = true;
1814 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1815 break;
1816 /* FALLTHRU */
1817
1818 case OMP_CLAUSE_FIRSTPRIVATE:
1819 case OMP_CLAUSE_PRIVATE:
1820 case OMP_CLAUSE_LINEAR:
1821 case OMP_CLAUSE_HAS_DEVICE_ADDR:
1822 case OMP_CLAUSE_IS_DEVICE_PTR:
1823 decl = OMP_CLAUSE_DECL (c);
1824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1825 {
1826 while (INDIRECT_REF_P (decl)
1827 || TREE_CODE (decl) == ARRAY_REF)
1828 decl = TREE_OPERAND (decl, 0);
1829 }
1830
1831 if (is_variable_sized (decl))
1832 {
1833 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1834 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
1835 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1836 && is_gimple_omp_offloaded (ctx->stmt))
1837 {
1838 tree decl2 = DECL_VALUE_EXPR (decl);
1839 gcc_assert (INDIRECT_REF_P (decl2));
1840 decl2 = TREE_OPERAND (decl2, 0);
1841 gcc_assert (DECL_P (decl2));
1842 install_var_local (decl2, ctx);
1843 fixup_remapped_decl (decl2, ctx, false);
1844 }
1845 install_var_local (decl, ctx);
1846 }
1847 fixup_remapped_decl (decl, ctx,
1848 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
1849 && OMP_CLAUSE_PRIVATE_DEBUG (c));
1850 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
1851 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
1852 scan_array_reductions = true;
1853 break;
1854
1855 case OMP_CLAUSE_REDUCTION:
1856 case OMP_CLAUSE_IN_REDUCTION:
1857 decl = OMP_CLAUSE_DECL (c);
1858 if (TREE_CODE (decl) != MEM_REF && !is_omp_target (ctx->stmt))
1859 {
1860 if (is_variable_sized (decl))
1861 install_var_local (decl, ctx);
1862 fixup_remapped_decl (decl, ctx, false);
1863 }
1864 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1865 scan_array_reductions = true;
1866 break;
1867
1868 case OMP_CLAUSE_TASK_REDUCTION:
1869 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1870 scan_array_reductions = true;
1871 break;
1872
1873 case OMP_CLAUSE_SHARED:
1874 /* Ignore shared directives in teams construct inside of
1875 target construct. */
1876 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
1877 && !is_host_teams_ctx (ctx))
1878 break;
1879 decl = OMP_CLAUSE_DECL (c);
1880 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1881 break;
1882 if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
1883 {
1884 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl,
1885 ctx->outer)))
1886 break;
1887 bool by_ref = use_pointer_for_field (decl, ctx);
1888 install_var_field (decl, by_ref, 11, ctx);
1889 break;
1890 }
1891 fixup_remapped_decl (decl, ctx, false);
1892 break;
1893
1894 case OMP_CLAUSE_MAP:
1895 if (!is_gimple_omp_offloaded (ctx->stmt))
1896 break;
1897 decl = OMP_CLAUSE_DECL (c);
1898 if (DECL_P (decl)
1899 && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
1900 && (OMP_CLAUSE_MAP_KIND (c)
1901 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
1902 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1903 && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
1904 && varpool_node::get_create (decl)->offloadable)
1905 break;
1906 if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
1907 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
1908 && is_omp_target (ctx->stmt)
1909 && !is_gimple_omp_offloaded (ctx->stmt))
1910 break;
1911 if (DECL_P (decl))
1912 {
1913 if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
1914 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
1915 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1916 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
1917 {
1918 tree new_decl = lookup_decl (decl, ctx);
1919 TREE_TYPE (new_decl)
1920 = remap_type (TREE_TYPE (decl), &ctx->cb);
1921 }
1922 else if (DECL_SIZE (decl)
1923 && !poly_int_tree_p (DECL_SIZE (decl)))
1924 {
1925 tree decl2 = DECL_VALUE_EXPR (decl);
1926 gcc_assert (INDIRECT_REF_P (decl2));
1927 decl2 = TREE_OPERAND (decl2, 0);
1928 gcc_assert (DECL_P (decl2));
1929 fixup_remapped_decl (decl2, ctx, false);
1930 fixup_remapped_decl (decl, ctx, true);
1931 }
1932 else
1933 fixup_remapped_decl (decl, ctx, false);
1934 }
1935 break;
1936
1937 case OMP_CLAUSE_COPYPRIVATE:
1938 case OMP_CLAUSE_COPYIN:
1939 case OMP_CLAUSE_DEFAULT:
1940 case OMP_CLAUSE_IF:
1941 case OMP_CLAUSE_SELF:
1942 case OMP_CLAUSE_NUM_THREADS:
1943 case OMP_CLAUSE_NUM_TEAMS:
1944 case OMP_CLAUSE_THREAD_LIMIT:
1945 case OMP_CLAUSE_DEVICE:
1946 case OMP_CLAUSE_SCHEDULE:
1947 case OMP_CLAUSE_DIST_SCHEDULE:
1948 case OMP_CLAUSE_NOWAIT:
1949 case OMP_CLAUSE_ORDERED:
1950 case OMP_CLAUSE_COLLAPSE:
1951 case OMP_CLAUSE_UNTIED:
1952 case OMP_CLAUSE_FINAL:
1953 case OMP_CLAUSE_MERGEABLE:
1954 case OMP_CLAUSE_PROC_BIND:
1955 case OMP_CLAUSE_SAFELEN:
1956 case OMP_CLAUSE_SIMDLEN:
1957 case OMP_CLAUSE_ALIGNED:
1958 case OMP_CLAUSE_DEPEND:
1959 case OMP_CLAUSE_DETACH:
1960 case OMP_CLAUSE_ALLOCATE:
1961 case OMP_CLAUSE__LOOPTEMP_:
1962 case OMP_CLAUSE__REDUCTEMP_:
1963 case OMP_CLAUSE_TO:
1964 case OMP_CLAUSE_FROM:
1965 case OMP_CLAUSE_PRIORITY:
1966 case OMP_CLAUSE_GRAINSIZE:
1967 case OMP_CLAUSE_NUM_TASKS:
1968 case OMP_CLAUSE_THREADS:
1969 case OMP_CLAUSE_SIMD:
1970 case OMP_CLAUSE_NOGROUP:
1971 case OMP_CLAUSE_DEFAULTMAP:
1972 case OMP_CLAUSE_ORDER:
1973 case OMP_CLAUSE_BIND:
1974 case OMP_CLAUSE_USE_DEVICE_PTR:
1975 case OMP_CLAUSE_USE_DEVICE_ADDR:
1976 case OMP_CLAUSE_NONTEMPORAL:
1977 case OMP_CLAUSE_ASYNC:
1978 case OMP_CLAUSE_WAIT:
1979 case OMP_CLAUSE_NUM_GANGS:
1980 case OMP_CLAUSE_NUM_WORKERS:
1981 case OMP_CLAUSE_VECTOR_LENGTH:
1982 case OMP_CLAUSE_GANG:
1983 case OMP_CLAUSE_WORKER:
1984 case OMP_CLAUSE_VECTOR:
1985 case OMP_CLAUSE_INDEPENDENT:
1986 case OMP_CLAUSE_AUTO:
1987 case OMP_CLAUSE_SEQ:
1988 case OMP_CLAUSE_TILE:
1989 case OMP_CLAUSE__SIMT_:
1990 case OMP_CLAUSE_IF_PRESENT:
1991 case OMP_CLAUSE_FINALIZE:
1992 case OMP_CLAUSE_FILTER:
1993 case OMP_CLAUSE__CONDTEMP_:
1994 case OMP_CLAUSE_INIT:
1995 case OMP_CLAUSE_USE:
1996 case OMP_CLAUSE_DESTROY:
1997 break;
1998
1999 case OMP_CLAUSE__CACHE_:
2000 case OMP_CLAUSE_NOHOST:
2001 default:
2002 gcc_unreachable ();
2003 }
2004 }
2005
2006 gcc_checking_assert (!scan_array_reductions
2007 || !is_gimple_omp_oacc (ctx->stmt));
2008 if (scan_array_reductions)
2009 {
2010 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
2011 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
2012 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
2013 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
2014 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
2015 {
2016 omp_context *rctx = ctx;
2017 if (is_omp_target (ctx->stmt))
2018 rctx = ctx->outer;
2019 scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c), rctx);
2020 scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), rctx);
2021 }
2022 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
2023 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
2024 scan_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
2025 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
2026 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
2027 scan_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
2028 }
2029 if (flex_array_ptr)
2030 {
2031 tree field = build_range_type (size_type_node,
2032 build_int_cstu (size_type_node, 0),
2033 NULL_TREE);
2034 field = build_array_type (ptr_type_node, field);
2035 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, field);
2036 SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
2037 DECL_CONTEXT (field) = ctx->record_type;
2038 DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
2039 TYPE_FIELDS (ctx->record_type) = field;
2040 }
2041 }
2042
2043 /* Create a new name for omp child function. Returns an identifier. */
2044
2045 static tree
2046 create_omp_child_function_name (bool task_copy)
2047 {
2048 return clone_function_name_numbered (current_function_decl,
2049 task_copy ? "_omp_cpyfn" : "_omp_fn");
2050 }
2051
2052 /* Return true if CTX may belong to offloaded code: either if current function
2053 is offloaded, or any enclosing context corresponds to a target region. */
2054
2055 static bool
2056 omp_maybe_offloaded_ctx (omp_context *ctx)
2057 {
2058 if (cgraph_node::get (current_function_decl)->offloadable)
2059 return true;
2060 for (; ctx; ctx = ctx->outer)
2061 if (is_gimple_omp_offloaded (ctx->stmt))
2062 return true;
2063 return false;
2064 }
2065
2066 /* Build a decl for the omp child function. It'll not contain a body
2067 yet, just the bare decl. */
2068
2069 static void
2070 create_omp_child_function (omp_context *ctx, bool task_copy)
2071 {
2072 tree decl, type, name, t;
2073
2074 name = create_omp_child_function_name (task_copy);
2075 if (task_copy)
2076 type = build_function_type_list (void_type_node, ptr_type_node,
2077 ptr_type_node, NULL_TREE);
2078 else
2079 type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
2080
2081 decl = build_decl (gimple_location (ctx->stmt), FUNCTION_DECL, name, type);
2082
2083 gcc_checking_assert (!is_gimple_omp_oacc (ctx->stmt)
2084 || !task_copy);
2085 if (!task_copy)
2086 ctx->cb.dst_fn = decl;
2087 else
2088 gimple_omp_task_set_copy_fn (ctx->stmt, decl);
2089
2090 TREE_STATIC (decl) = 1;
2091 TREE_USED (decl) = 1;
2092 DECL_ARTIFICIAL (decl) = 1;
2093 DECL_IGNORED_P (decl) = 0;
2094 TREE_PUBLIC (decl) = 0;
2095 DECL_UNINLINABLE (decl) = 1;
2096 DECL_EXTERNAL (decl) = 0;
2097 DECL_CONTEXT (decl) = NULL_TREE;
2098 DECL_INITIAL (decl) = make_node (BLOCK);
2099 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
2100 DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (current_function_decl);
2101 /* Remove omp declare simd attribute from the new attributes. */
2102 if (tree a = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (decl)))
2103 {
2104 while (tree a2 = lookup_attribute ("omp declare simd", TREE_CHAIN (a)))
2105 a = a2;
2106 a = TREE_CHAIN (a);
2107 for (tree *p = &DECL_ATTRIBUTES (decl); *p != a;)
2108 if (is_attribute_p ("omp declare simd", get_attribute_name (*p)))
2109 *p = TREE_CHAIN (*p);
2110 else
2111 {
2112 tree chain = TREE_CHAIN (*p);
2113 *p = copy_node (*p);
2114 p = &TREE_CHAIN (*p);
2115 *p = chain;
2116 }
2117 }
2118 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
2119 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
2120 DECL_FUNCTION_SPECIFIC_TARGET (decl)
2121 = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
2122 DECL_FUNCTION_VERSIONED (decl)
2123 = DECL_FUNCTION_VERSIONED (current_function_decl);
2124
2125 if (omp_maybe_offloaded_ctx (ctx))
2126 {
2127 cgraph_node::get_create (decl)->offloadable = 1;
2128 if (ENABLE_OFFLOADING)
2129 g->have_offload = true;
2130 }
2131
2132 if (cgraph_node::get_create (decl)->offloadable)
2133 {
2134 const char *target_attr = (is_gimple_omp_offloaded (ctx->stmt)
2135 ? "omp target entrypoint"
2136 : "omp declare target");
2137 if (lookup_attribute ("omp declare target",
2138 DECL_ATTRIBUTES (current_function_decl)))
2139 {
2140 if (is_gimple_omp_offloaded (ctx->stmt))
2141 DECL_ATTRIBUTES (decl)
2142 = remove_attribute ("omp declare target",
2143 copy_list (DECL_ATTRIBUTES (decl)));
2144 else
2145 target_attr = NULL;
2146 }
2147 if (target_attr
2148 && is_gimple_omp_offloaded (ctx->stmt)
2149 && lookup_attribute ("noclone", DECL_ATTRIBUTES (decl)) == NULL_TREE)
2150 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("noclone"),
2151 NULL_TREE, DECL_ATTRIBUTES (decl));
2152 if (target_attr)
2153 DECL_ATTRIBUTES (decl)
2154 = tree_cons (get_identifier (target_attr),
2155 NULL_TREE, DECL_ATTRIBUTES (decl));
2156 }
2157
2158 t = build_decl (DECL_SOURCE_LOCATION (decl),
2159 RESULT_DECL, NULL_TREE, void_type_node);
2160 DECL_ARTIFICIAL (t) = 1;
2161 DECL_IGNORED_P (t) = 1;
2162 DECL_CONTEXT (t) = decl;
2163 DECL_RESULT (decl) = t;
2164
2165 tree data_name = get_identifier (".omp_data_i");
2166 t = build_decl (DECL_SOURCE_LOCATION (decl), PARM_DECL, data_name,
2167 ptr_type_node);
2168 DECL_ARTIFICIAL (t) = 1;
2169 DECL_NAMELESS (t) = 1;
2170 DECL_ARG_TYPE (t) = ptr_type_node;
2171 DECL_CONTEXT (t) = current_function_decl;
2172 TREE_USED (t) = 1;
2173 TREE_READONLY (t) = 1;
2174 DECL_ARGUMENTS (decl) = t;
2175 if (!task_copy)
2176 ctx->receiver_decl = t;
2177 else
2178 {
2179 t = build_decl (DECL_SOURCE_LOCATION (decl),
2180 PARM_DECL, get_identifier (".omp_data_o"),
2181 ptr_type_node);
2182 DECL_ARTIFICIAL (t) = 1;
2183 DECL_NAMELESS (t) = 1;
2184 DECL_ARG_TYPE (t) = ptr_type_node;
2185 DECL_CONTEXT (t) = current_function_decl;
2186 TREE_USED (t) = 1;
2187 TREE_ADDRESSABLE (t) = 1;
2188 DECL_CHAIN (t) = DECL_ARGUMENTS (decl);
2189 DECL_ARGUMENTS (decl) = t;
2190 }
2191
2192 /* Allocate memory for the function structure. The call to
2193 allocate_struct_function clobbers CFUN, so we need to restore
2194 it afterward. */
2195 push_struct_function (decl);
2196 cfun->function_end_locus = gimple_location (ctx->stmt);
2197 init_tree_ssa (cfun);
2198 pop_cfun ();
2199 }
2200
2201 /* Callback for walk_gimple_seq. Check if combined parallel
2202 contains gimple_omp_for_combined_into_p OMP_FOR. */
2203
2204 tree
2205 omp_find_combined_for (gimple_stmt_iterator *gsi_p,
2206 bool *handled_ops_p,
2207 struct walk_stmt_info *wi)
2208 {
2209 gimple *stmt = gsi_stmt (*gsi_p);
2210
2211 *handled_ops_p = true;
2212 switch (gimple_code (stmt))
2213 {
2214 WALK_SUBSTMTS;
2215
2216 case GIMPLE_OMP_FOR:
2217 if (gimple_omp_for_combined_into_p (stmt)
2218 && gimple_omp_for_kind (stmt)
2219 == *(const enum gf_mask *) (wi->info))
2220 {
2221 wi->info = stmt;
2222 return integer_zero_node;
2223 }
2224 break;
2225 default:
2226 break;
2227 }
2228 return NULL;
2229 }
2230
2231 /* Add _LOOPTEMP_/_REDUCTEMP_ clauses on OpenMP parallel or task. */
2232
2233 static void
2234 add_taskreg_looptemp_clauses (enum gf_mask msk, gimple *stmt,
2235 omp_context *outer_ctx)
2236 {
2237 struct walk_stmt_info wi;
2238
2239 memset (&wi, 0, sizeof (wi));
2240 wi.val_only = true;
2241 wi.info = (void *) &msk;
2242 walk_gimple_seq (gimple_omp_body (stmt), omp_find_combined_for, NULL, &wi);
2243 if (wi.info != (void *) &msk)
2244 {
2245 gomp_for *for_stmt = as_a <gomp_for *> ((gimple *) wi.info);
2246 struct omp_for_data fd;
2247 omp_extract_for_data (for_stmt, &fd, NULL);
2248 /* We need two temporaries with fd.loop.v type (istart/iend)
2249 and then (fd.collapse - 1) temporaries with the same
2250 type for count2 ... countN-1 vars if not constant. */
2251 size_t count = 2, i;
2252 tree type = fd.iter_type;
2253 if (fd.collapse > 1
2254 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
2255 {
2256 count += fd.collapse - 1;
2257 /* If there are lastprivate clauses on the inner
2258 GIMPLE_OMP_FOR, add one more temporaries for the total number
2259 of iterations (product of count1 ... countN-1). */
2260 if (omp_find_clause (gimple_omp_for_clauses (for_stmt),
2261 OMP_CLAUSE_LASTPRIVATE)
2262 || (msk == GF_OMP_FOR_KIND_FOR
2263 && omp_find_clause (gimple_omp_parallel_clauses (stmt),
2264 OMP_CLAUSE_LASTPRIVATE)))
2265 {
2266 tree temp = create_tmp_var (type);
2267 tree c = build_omp_clause (UNKNOWN_LOCATION,
2268 OMP_CLAUSE__LOOPTEMP_);
2269 insert_decl_map (&outer_ctx->cb, temp, temp);
2270 OMP_CLAUSE_DECL (c) = temp;
2271 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2272 gimple_omp_taskreg_set_clauses (stmt, c);
2273 }
2274 if (fd.non_rect
2275 && fd.last_nonrect == fd.first_nonrect + 1)
2276 if (tree v = gimple_omp_for_index (for_stmt, fd.last_nonrect))
2277 if (!TYPE_UNSIGNED (TREE_TYPE (v)))
2278 {
2279 v = gimple_omp_for_index (for_stmt, fd.first_nonrect);
2280 tree type2 = TREE_TYPE (v);
2281 count++;
2282 for (i = 0; i < 3; i++)
2283 {
2284 tree temp = create_tmp_var (type2);
2285 tree c = build_omp_clause (UNKNOWN_LOCATION,
2286 OMP_CLAUSE__LOOPTEMP_);
2287 insert_decl_map (&outer_ctx->cb, temp, temp);
2288 OMP_CLAUSE_DECL (c) = temp;
2289 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2290 gimple_omp_taskreg_set_clauses (stmt, c);
2291 }
2292 }
2293 }
2294 for (i = 0; i < count; i++)
2295 {
2296 tree temp = create_tmp_var (type);
2297 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
2298 insert_decl_map (&outer_ctx->cb, temp, temp);
2299 OMP_CLAUSE_DECL (c) = temp;
2300 OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2301 gimple_omp_taskreg_set_clauses (stmt, c);
2302 }
2303 }
2304 if (msk == GF_OMP_FOR_KIND_TASKLOOP
2305 && omp_find_clause (gimple_omp_task_clauses (stmt),
2306 OMP_CLAUSE_REDUCTION))
2307 {
2308 tree type = build_pointer_type (pointer_sized_int_node);
2309 tree temp = create_tmp_var (type);
2310 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
2311 insert_decl_map (&outer_ctx->cb, temp, temp);
2312 OMP_CLAUSE_DECL (c) = temp;
2313 OMP_CLAUSE_CHAIN (c) = gimple_omp_task_clauses (stmt);
2314 gimple_omp_task_set_clauses (stmt, c);
2315 }
2316 }
2317
2318 /* Scan an OpenMP parallel directive. */
2319
2320 static void
2321 scan_omp_parallel (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
2322 {
2323 omp_context *ctx;
2324 tree name;
2325 gomp_parallel *stmt = as_a <gomp_parallel *> (gsi_stmt (*gsi));
2326
2327 /* Ignore parallel directives with empty bodies, unless there
2328 are copyin clauses. */
2329 if (optimize > 0
2330 && empty_body_p (gimple_omp_body (stmt))
2331 && omp_find_clause (gimple_omp_parallel_clauses (stmt),
2332 OMP_CLAUSE_COPYIN) == NULL)
2333 {
2334 gsi_replace (gsi, gimple_build_nop (), false);
2335 return;
2336 }
2337
2338 if (gimple_omp_parallel_combined_p (stmt))
2339 add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_FOR, stmt, outer_ctx);
2340 for (tree c = omp_find_clause (gimple_omp_parallel_clauses (stmt),
2341 OMP_CLAUSE_REDUCTION);
2342 c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_REDUCTION))
2343 if (OMP_CLAUSE_REDUCTION_TASK (c))
2344 {
2345 tree type = build_pointer_type (pointer_sized_int_node);
2346 tree temp = create_tmp_var (type);
2347 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
2348 if (outer_ctx)
2349 insert_decl_map (&outer_ctx->cb, temp, temp);
2350 OMP_CLAUSE_DECL (c) = temp;
2351 OMP_CLAUSE_CHAIN (c) = gimple_omp_parallel_clauses (stmt);
2352 gimple_omp_parallel_set_clauses (stmt, c);
2353 break;
2354 }
2355 else if (OMP_CLAUSE_CHAIN (c) == NULL_TREE)
2356 break;
2357
2358 ctx = new_omp_context (stmt, outer_ctx);
2359 taskreg_contexts.safe_push (ctx);
2360 if (taskreg_nesting_level > 1)
2361 ctx->is_nested = true;
2362 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
2363 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
2364 name = create_tmp_var_name (".omp_data_s");
2365 name = build_decl (gimple_location (stmt),
2366 TYPE_DECL, name, ctx->record_type);
2367 DECL_ARTIFICIAL (name) = 1;
2368 DECL_NAMELESS (name) = 1;
2369 TYPE_NAME (ctx->record_type) = name;
2370 TYPE_ARTIFICIAL (ctx->record_type) = 1;
2371 create_omp_child_function (ctx, false);
2372 gimple_omp_parallel_set_child_fn (stmt, ctx->cb.dst_fn);
2373
2374 scan_sharing_clauses (gimple_omp_parallel_clauses (stmt), ctx);
2375 scan_omp (gimple_omp_body_ptr (stmt), ctx);
2376
2377 if (TYPE_FIELDS (ctx->record_type) == NULL)
2378 ctx->record_type = ctx->receiver_decl = NULL;
2379 }
2380
2381 /* Scan an OpenMP task directive. */
2382
2383 static void
2384 scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
2385 {
2386 omp_context *ctx;
2387 tree name, t;
2388 gomp_task *stmt = as_a <gomp_task *> (gsi_stmt (*gsi));
2389
2390 /* Ignore task directives with empty bodies, unless they have depend
2391 clause. */
2392 if (optimize > 0
2393 && gimple_omp_body (stmt)
2394 && empty_body_p (gimple_omp_body (stmt))
2395 && !omp_find_clause (gimple_omp_task_clauses (stmt), OMP_CLAUSE_DEPEND))
2396 {
2397 gsi_replace (gsi, gimple_build_nop (), false);
2398 return;
2399 }
2400
2401 if (gimple_omp_task_taskloop_p (stmt))
2402 add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_TASKLOOP, stmt, outer_ctx);
2403
2404 ctx = new_omp_context (stmt, outer_ctx);
2405
2406 if (gimple_omp_task_taskwait_p (stmt))
2407 {
2408 scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
2409 return;
2410 }
2411
2412 taskreg_contexts.safe_push (ctx);
2413 if (taskreg_nesting_level > 1)
2414 ctx->is_nested = true;
2415 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
2416 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
2417 name = create_tmp_var_name (".omp_data_s");
2418 name = build_decl (gimple_location (stmt),
2419 TYPE_DECL, name, ctx->record_type);
2420 DECL_ARTIFICIAL (name) = 1;
2421 DECL_NAMELESS (name) = 1;
2422 TYPE_NAME (ctx->record_type) = name;
2423 TYPE_ARTIFICIAL (ctx->record_type) = 1;
2424 create_omp_child_function (ctx, false);
2425 gimple_omp_task_set_child_fn (stmt, ctx->cb.dst_fn);
2426
2427 scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
2428
2429 if (ctx->srecord_type)
2430 {
2431 name = create_tmp_var_name (".omp_data_a");
2432 name = build_decl (gimple_location (stmt),
2433 TYPE_DECL, name, ctx->srecord_type);
2434 DECL_ARTIFICIAL (name) = 1;
2435 DECL_NAMELESS (name) = 1;
2436 TYPE_NAME (ctx->srecord_type) = name;
2437 TYPE_ARTIFICIAL (ctx->srecord_type) = 1;
2438 create_omp_child_function (ctx, true);
2439 }
2440
2441 scan_omp (gimple_omp_body_ptr (stmt), ctx);
2442
2443 if (TYPE_FIELDS (ctx->record_type) == NULL)
2444 {
2445 ctx->record_type = ctx->receiver_decl = NULL;
2446 t = build_int_cst (long_integer_type_node, 0);
2447 gimple_omp_task_set_arg_size (stmt, t);
2448 t = build_int_cst (long_integer_type_node, 1);
2449 gimple_omp_task_set_arg_align (stmt, t);
2450 }
2451 }
2452
2453 /* Helper function for finish_taskreg_scan, called through walk_tree.
2454 If maybe_lookup_decl_in_outer_context returns non-NULL for some
2455 tree, replace it in the expression. */
2456
2457 static tree
2458 finish_taskreg_remap (tree *tp, int *walk_subtrees, void *data)
2459 {
2460 if (VAR_P (*tp))
2461 {
2462 omp_context *ctx = (omp_context *) data;
2463 tree t = maybe_lookup_decl_in_outer_ctx (*tp, ctx);
2464 if (t != *tp)
2465 {
2466 if (DECL_HAS_VALUE_EXPR_P (t))
2467 t = unshare_expr (DECL_VALUE_EXPR (t));
2468 *tp = t;
2469 }
2470 *walk_subtrees = 0;
2471 }
2472 else if (IS_TYPE_OR_DECL_P (*tp))
2473 *walk_subtrees = 0;
2474 return NULL_TREE;
2475 }
2476
2477 /* If any decls have been made addressable during scan_omp,
2478 adjust their fields if needed, and layout record types
2479 of parallel/task constructs. */
2480
2481 static void
2482 finish_taskreg_scan (omp_context *ctx)
2483 {
2484 if (ctx->record_type == NULL_TREE)
2485 return;
2486
2487 /* If any make_addressable_vars were needed, verify all
2488 OMP_CLAUSE_SHARED clauses on GIMPLE_OMP_{PARALLEL,TASK,TEAMS}
2489 statements if use_pointer_for_field hasn't changed
2490 because of that. If it did, update field types now. */
2491 if (make_addressable_vars)
2492 {
2493 tree c;
2494
2495 for (c = gimple_omp_taskreg_clauses (ctx->stmt);
2496 c; c = OMP_CLAUSE_CHAIN (c))
2497 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
2498 && !OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
2499 {
2500 tree decl = OMP_CLAUSE_DECL (c);
2501
2502 /* Global variables don't need to be copied,
2503 the receiver side will use them directly. */
2504 if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
2505 continue;
2506 if (!bitmap_bit_p (make_addressable_vars, DECL_UID (decl))
2507 || !use_pointer_for_field (decl, ctx))
2508 continue;
2509 tree field = lookup_field (decl, ctx);
2510 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE
2511 && TREE_TYPE (TREE_TYPE (field)) == TREE_TYPE (decl))
2512 continue;
2513 TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
2514 TREE_THIS_VOLATILE (field) = 0;
2515 DECL_USER_ALIGN (field) = 0;
2516 SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
2517 if (TYPE_ALIGN (ctx->record_type) < DECL_ALIGN (field))
2518 SET_TYPE_ALIGN (ctx->record_type, DECL_ALIGN (field));
2519 if (ctx->srecord_type)
2520 {
2521 tree sfield = lookup_sfield (decl, ctx);
2522 TREE_TYPE (sfield) = TREE_TYPE (field);
2523 TREE_THIS_VOLATILE (sfield) = 0;
2524 DECL_USER_ALIGN (sfield) = 0;
2525 SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
2526 if (TYPE_ALIGN (ctx->srecord_type) < DECL_ALIGN (sfield))
2527 SET_TYPE_ALIGN (ctx->srecord_type, DECL_ALIGN (sfield));
2528 }
2529 }
2530 }
2531
2532 if (gimple_code (ctx->stmt) == GIMPLE_OMP_PARALLEL)
2533 {
2534 tree clauses = gimple_omp_parallel_clauses (ctx->stmt);
2535 tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
2536 if (c)
2537 {
2538 /* Move the _reductemp_ clause first. GOMP_parallel_reductions
2539 expects to find it at the start of data. */
2540 tree f = lookup_field (OMP_CLAUSE_DECL (c), ctx);
2541 tree *p = &TYPE_FIELDS (ctx->record_type);
2542 while (*p)
2543 if (*p == f)
2544 {
2545 *p = DECL_CHAIN (*p);
2546 break;
2547 }
2548 else
2549 p = &DECL_CHAIN (*p);
2550 DECL_CHAIN (f) = TYPE_FIELDS (ctx->record_type);
2551 TYPE_FIELDS (ctx->record_type) = f;
2552 }
2553 layout_type (ctx->record_type);
2554 fixup_child_record_type (ctx);
2555 }
2556 else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
2557 {
2558 layout_type (ctx->record_type);
2559 fixup_child_record_type (ctx);
2560 }
2561 else
2562 {
2563 location_t loc = gimple_location (ctx->stmt);
2564 tree *p, vla_fields = NULL_TREE, *q = &vla_fields;
2565 tree detach_clause
2566 = omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
2567 OMP_CLAUSE_DETACH);
2568 /* Move VLA fields to the end. */
2569 p = &TYPE_FIELDS (ctx->record_type);
2570 while (*p)
2571 if (!TYPE_SIZE_UNIT (TREE_TYPE (*p))
2572 || ! TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (*p))))
2573 {
2574 *q = *p;
2575 *p = TREE_CHAIN (*p);
2576 TREE_CHAIN (*q) = NULL_TREE;
2577 q = &TREE_CHAIN (*q);
2578 }
2579 else
2580 p = &DECL_CHAIN (*p);
2581 *p = vla_fields;
2582 if (gimple_omp_task_taskloop_p (ctx->stmt))
2583 {
2584 /* Move fields corresponding to first and second _looptemp_
2585 clause first. There are filled by GOMP_taskloop
2586 and thus need to be in specific positions. */
2587 tree clauses = gimple_omp_task_clauses (ctx->stmt);
2588 tree c1 = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
2589 tree c2 = omp_find_clause (OMP_CLAUSE_CHAIN (c1),
2590 OMP_CLAUSE__LOOPTEMP_);
2591 tree c3 = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
2592 tree f1 = lookup_field (OMP_CLAUSE_DECL (c1), ctx);
2593 tree f2 = lookup_field (OMP_CLAUSE_DECL (c2), ctx);
2594 tree f3 = c3 ? lookup_field (OMP_CLAUSE_DECL (c3), ctx) : NULL_TREE;
2595 p = &TYPE_FIELDS (ctx->record_type);
2596 while (*p)
2597 if (*p == f1 || *p == f2 || *p == f3)
2598 *p = DECL_CHAIN (*p);
2599 else
2600 p = &DECL_CHAIN (*p);
2601 DECL_CHAIN (f1) = f2;
2602 if (c3)
2603 {
2604 DECL_CHAIN (f2) = f3;
2605 DECL_CHAIN (f3) = TYPE_FIELDS (ctx->record_type);
2606 }
2607 else
2608 DECL_CHAIN (f2) = TYPE_FIELDS (ctx->record_type);
2609 TYPE_FIELDS (ctx->record_type) = f1;
2610 if (ctx->srecord_type)
2611 {
2612 f1 = lookup_sfield (OMP_CLAUSE_DECL (c1), ctx);
2613 f2 = lookup_sfield (OMP_CLAUSE_DECL (c2), ctx);
2614 if (c3)
2615 f3 = lookup_sfield (OMP_CLAUSE_DECL (c3), ctx);
2616 p = &TYPE_FIELDS (ctx->srecord_type);
2617 while (*p)
2618 if (*p == f1 || *p == f2 || *p == f3)
2619 *p = DECL_CHAIN (*p);
2620 else
2621 p = &DECL_CHAIN (*p);
2622 DECL_CHAIN (f1) = f2;
2623 DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
2624 if (c3)
2625 {
2626 DECL_CHAIN (f2) = f3;
2627 DECL_CHAIN (f3) = TYPE_FIELDS (ctx->srecord_type);
2628 }
2629 else
2630 DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
2631 TYPE_FIELDS (ctx->srecord_type) = f1;
2632 }
2633 }
2634 if (detach_clause)
2635 {
2636 tree c, field;
2637
2638 /* Look for a firstprivate clause with the detach event handle. */
2639 for (c = gimple_omp_taskreg_clauses (ctx->stmt);
2640 c; c = OMP_CLAUSE_CHAIN (c))
2641 {
2642 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
2643 continue;
2644 if (maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c), ctx)
2645 == OMP_CLAUSE_DECL (detach_clause))
2646 break;
2647 }
2648
2649 gcc_assert (c);
2650 field = lookup_field (OMP_CLAUSE_DECL (c), ctx);
2651
2652 /* Move field corresponding to the detach clause first.
2653 This is filled by GOMP_task and needs to be in a
2654 specific position. */
2655 p = &TYPE_FIELDS (ctx->record_type);
2656 while (*p)
2657 if (*p == field)
2658 *p = DECL_CHAIN (*p);
2659 else
2660 p = &DECL_CHAIN (*p);
2661 DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
2662 TYPE_FIELDS (ctx->record_type) = field;
2663 if (ctx->srecord_type)
2664 {
2665 field = lookup_sfield (OMP_CLAUSE_DECL (c), ctx);
2666 p = &TYPE_FIELDS (ctx->srecord_type);
2667 while (*p)
2668 if (*p == field)
2669 *p = DECL_CHAIN (*p);
2670 else
2671 p = &DECL_CHAIN (*p);
2672 DECL_CHAIN (field) = TYPE_FIELDS (ctx->srecord_type);
2673 TYPE_FIELDS (ctx->srecord_type) = field;
2674 }
2675 }
2676 layout_type (ctx->record_type);
2677 fixup_child_record_type (ctx);
2678 if (ctx->srecord_type)
2679 layout_type (ctx->srecord_type);
2680 tree t = fold_convert_loc (loc, long_integer_type_node,
2681 TYPE_SIZE_UNIT (ctx->record_type));
2682 if (TREE_CODE (t) != INTEGER_CST)
2683 {
2684 t = unshare_expr (t);
2685 walk_tree (&t, finish_taskreg_remap, ctx, NULL);
2686 }
2687 gimple_omp_task_set_arg_size (ctx->stmt, t);
2688 t = build_int_cst (long_integer_type_node,
2689 TYPE_ALIGN_UNIT (ctx->record_type));
2690 gimple_omp_task_set_arg_align (ctx->stmt, t);
2691 }
2692 }
2693
2694 /* Find the enclosing offload context. */
2695
2696 static omp_context *
2697 enclosing_target_ctx (omp_context *ctx)
2698 {
2699 for (; ctx; ctx = ctx->outer)
2700 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET)
2701 break;
2702
2703 return ctx;
2704 }
2705
2706 /* Return whether CTX's parent compute construct is an OpenACC 'kernels'
2707 construct.
2708 (This doesn't include OpenACC 'kernels' decomposed parts.) */
2709
2710 static bool
2711 ctx_in_oacc_kernels_region (omp_context *ctx)
2712 {
2713 for (;ctx != NULL; ctx = ctx->outer)
2714 {
2715 gimple *stmt = ctx->stmt;
2716 if (gimple_code (stmt) == GIMPLE_OMP_TARGET
2717 && gimple_omp_target_kind (stmt) == GF_OMP_TARGET_KIND_OACC_KERNELS)
2718 return true;
2719 }
2720
2721 return false;
2722 }
2723
2724 /* Check the parallelism clauses inside a OpenACC 'kernels' region.
2725 (This doesn't include OpenACC 'kernels' decomposed parts.)
2726 Until kernels handling moves to use the same loop indirection
2727 scheme as parallel, we need to do this checking early. */
2728
2729 static unsigned
2730 check_oacc_kernel_gwv (gomp_for *stmt, omp_context *ctx)
2731 {
2732 bool checking = true;
2733 unsigned outer_mask = 0;
2734 unsigned this_mask = 0;
2735 bool has_seq = false, has_auto = false;
2736
2737 if (ctx->outer)
2738 outer_mask = check_oacc_kernel_gwv (NULL, ctx->outer);
2739 if (!stmt)
2740 {
2741 checking = false;
2742 if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR)
2743 return outer_mask;
2744 stmt = as_a <gomp_for *> (ctx->stmt);
2745 }
2746
2747 for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
2748 {
2749 switch (OMP_CLAUSE_CODE (c))
2750 {
2751 case OMP_CLAUSE_GANG:
2752 this_mask |= GOMP_DIM_MASK (GOMP_DIM_GANG);
2753 break;
2754 case OMP_CLAUSE_WORKER:
2755 this_mask |= GOMP_DIM_MASK (GOMP_DIM_WORKER);
2756 break;
2757 case OMP_CLAUSE_VECTOR:
2758 this_mask |= GOMP_DIM_MASK (GOMP_DIM_VECTOR);
2759 break;
2760 case OMP_CLAUSE_SEQ:
2761 has_seq = true;
2762 break;
2763 case OMP_CLAUSE_AUTO:
2764 has_auto = true;
2765 break;
2766 default:
2767 break;
2768 }
2769 }
2770
2771 if (checking)
2772 {
2773 if (has_seq && (this_mask || has_auto))
2774 error_at (gimple_location (stmt), "%<seq%> overrides other"
2775 " OpenACC loop specifiers");
2776 else if (has_auto && this_mask)
2777 error_at (gimple_location (stmt), "%<auto%> conflicts with other"
2778 " OpenACC loop specifiers");
2779
2780 if (this_mask & outer_mask)
2781 error_at (gimple_location (stmt), "inner loop uses same"
2782 " OpenACC parallelism as containing loop");
2783 }
2784
2785 return outer_mask | this_mask;
2786 }
2787
2788 /* Scan a GIMPLE_OMP_FOR. */
2789
2790 static omp_context *
2791 scan_omp_for (gomp_for *stmt, omp_context *outer_ctx)
2792 {
2793 omp_context *ctx;
2794 size_t i;
2795 tree clauses = gimple_omp_for_clauses (stmt);
2796
2797 ctx = new_omp_context (stmt, outer_ctx);
2798
2799 if (is_gimple_omp_oacc (stmt))
2800 {
2801 omp_context *tgt = enclosing_target_ctx (outer_ctx);
2802
2803 if (!(tgt && is_oacc_kernels (tgt)))
2804 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
2805 {
2806 tree c_op0;
2807 switch (OMP_CLAUSE_CODE (c))
2808 {
2809 case OMP_CLAUSE_GANG:
2810 c_op0 = OMP_CLAUSE_GANG_EXPR (c);
2811 break;
2812
2813 case OMP_CLAUSE_WORKER:
2814 c_op0 = OMP_CLAUSE_WORKER_EXPR (c);
2815 break;
2816
2817 case OMP_CLAUSE_VECTOR:
2818 c_op0 = OMP_CLAUSE_VECTOR_EXPR (c);
2819 break;
2820
2821 default:
2822 continue;
2823 }
2824
2825 if (c_op0)
2826 {
2827 /* By construction, this is impossible for OpenACC 'kernels'
2828 decomposed parts. */
2829 gcc_assert (!(tgt && is_oacc_kernels_decomposed_part (tgt)));
2830
2831 error_at (OMP_CLAUSE_LOCATION (c),
2832 "argument not permitted on %qs clause",
2833 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
2834 if (tgt)
2835 inform (gimple_location (tgt->stmt),
2836 "enclosing parent compute construct");
2837 else if (oacc_get_fn_attrib (current_function_decl))
2838 inform (DECL_SOURCE_LOCATION (current_function_decl),
2839 "enclosing routine");
2840 else
2841 gcc_unreachable ();
2842 }
2843 }
2844
2845 if (tgt && is_oacc_kernels (tgt))
2846 check_oacc_kernel_gwv (stmt, ctx);
2847
2848 /* Collect all variables named in reductions on this loop. Ensure
2849 that, if this loop has a reduction on some variable v, and there is
2850 a reduction on v somewhere in an outer context, then there is a
2851 reduction on v on all intervening loops as well. */
2852 tree local_reduction_clauses = NULL;
2853 for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
2854 {
2855 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
2856 local_reduction_clauses
2857 = tree_cons (NULL, c, local_reduction_clauses);
2858 }
2859 if (ctx->outer_reduction_clauses == NULL && ctx->outer != NULL)
2860 ctx->outer_reduction_clauses
2861 = chainon (unshare_expr (ctx->outer->local_reduction_clauses),
2862 ctx->outer->outer_reduction_clauses);
2863 tree outer_reduction_clauses = ctx->outer_reduction_clauses;
2864 tree local_iter = local_reduction_clauses;
2865 for (; local_iter; local_iter = TREE_CHAIN (local_iter))
2866 {
2867 tree local_clause = TREE_VALUE (local_iter);
2868 tree local_var = OMP_CLAUSE_DECL (local_clause);
2869 tree_code local_op = OMP_CLAUSE_REDUCTION_CODE (local_clause);
2870 bool have_outer_reduction = false;
2871 tree ctx_iter = outer_reduction_clauses;
2872 for (; ctx_iter; ctx_iter = TREE_CHAIN (ctx_iter))
2873 {
2874 tree outer_clause = TREE_VALUE (ctx_iter);
2875 tree outer_var = OMP_CLAUSE_DECL (outer_clause);
2876 tree_code outer_op = OMP_CLAUSE_REDUCTION_CODE (outer_clause);
2877 if (outer_var == local_var && outer_op != local_op)
2878 {
2879 if (warning_at (OMP_CLAUSE_LOCATION (local_clause),
2880 OPT_Wopenmp, "conflicting reduction "
2881 "operations for %qE",
2882 local_var))
2883 inform (OMP_CLAUSE_LOCATION (outer_clause),
2884 "location of the previous reduction for %qE",
2885 outer_var);
2886 }
2887 if (outer_var == local_var)
2888 {
2889 have_outer_reduction = true;
2890 break;
2891 }
2892 }
2893 if (have_outer_reduction)
2894 {
2895 /* There is a reduction on outer_var both on this loop and on
2896 some enclosing loop. Walk up the context tree until such a
2897 loop with a reduction on outer_var is found, and complain
2898 about all intervening loops that do not have such a
2899 reduction. */
2900 struct omp_context *curr_loop = ctx->outer;
2901 bool found = false;
2902 while (curr_loop != NULL)
2903 {
2904 tree curr_iter = curr_loop->local_reduction_clauses;
2905 for (; curr_iter; curr_iter = TREE_CHAIN (curr_iter))
2906 {
2907 tree curr_clause = TREE_VALUE (curr_iter);
2908 tree curr_var = OMP_CLAUSE_DECL (curr_clause);
2909 if (curr_var == local_var)
2910 {
2911 found = true;
2912 break;
2913 }
2914 }
2915 if (!found)
2916 warning_at (gimple_location (curr_loop->stmt), OPT_Wopenmp,
2917 "nested loop in reduction needs "
2918 "reduction clause for %qE",
2919 local_var);
2920 else
2921 break;
2922 curr_loop = curr_loop->outer;
2923 }
2924 }
2925 }
2926 ctx->local_reduction_clauses = local_reduction_clauses;
2927 ctx->outer_reduction_clauses
2928 = chainon (unshare_expr (ctx->local_reduction_clauses),
2929 ctx->outer_reduction_clauses);
2930
2931 if (tgt && is_oacc_kernels (tgt))
2932 {
2933 /* Strip out reductions, as they are not handled yet. */
2934 tree *prev_ptr = &clauses;
2935
2936 while (tree probe = *prev_ptr)
2937 {
2938 tree *next_ptr = &OMP_CLAUSE_CHAIN (probe);
2939
2940 if (OMP_CLAUSE_CODE (probe) == OMP_CLAUSE_REDUCTION)
2941 *prev_ptr = *next_ptr;
2942 else
2943 prev_ptr = next_ptr;
2944 }
2945
2946 gimple_omp_for_set_clauses (stmt, clauses);
2947 }
2948 }
2949
2950 scan_sharing_clauses (clauses, ctx);
2951
2952 scan_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
2953 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2954 {
2955 scan_omp_op (gimple_omp_for_index_ptr (stmt, i), ctx);
2956 scan_omp_op (gimple_omp_for_initial_ptr (stmt, i), ctx);
2957 scan_omp_op (gimple_omp_for_final_ptr (stmt, i), ctx);
2958 scan_omp_op (gimple_omp_for_incr_ptr (stmt, i), ctx);
2959 }
2960 scan_omp (gimple_omp_body_ptr (stmt), ctx);
2961 return ctx;
2962 }
2963
2964 /* Duplicate #pragma omp simd, one for SIMT, another one for SIMD. */
2965
2966 static void
2967 scan_omp_simd (gimple_stmt_iterator *gsi, gomp_for *stmt,
2968 omp_context *outer_ctx)
2969 {
2970 gbind *bind = gimple_build_bind (NULL, NULL, NULL);
2971 gsi_replace (gsi, bind, false);
2972 gimple_seq seq = NULL;
2973 gimple *g = gimple_build_call_internal (IFN_GOMP_USE_SIMT, 0);
2974 tree cond = create_tmp_var_raw (integer_type_node);
2975 DECL_CONTEXT (cond) = current_function_decl;
2976 DECL_SEEN_IN_BIND_EXPR_P (cond) = 1;
2977 gimple_bind_set_vars (bind, cond);
2978 gimple_call_set_lhs (g, cond);
2979 gimple_seq_add_stmt (&seq, g);
2980 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
2981 tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
2982 tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
2983 g = gimple_build_cond (NE_EXPR, cond, integer_zero_node, lab1, lab2);
2984 gimple_seq_add_stmt (&seq, g);
2985 g = gimple_build_label (lab1);
2986 gimple_seq_add_stmt (&seq, g);
2987 gimple_seq new_seq = copy_gimple_seq_and_replace_locals (stmt);
2988 gomp_for *new_stmt = as_a <gomp_for *> (new_seq);
2989 tree clause = build_omp_clause (gimple_location (stmt), OMP_CLAUSE__SIMT_);
2990 OMP_CLAUSE_CHAIN (clause) = gimple_omp_for_clauses (new_stmt);
2991 gimple_omp_for_set_clauses (new_stmt, clause);
2992 gimple_seq_add_stmt (&seq, new_stmt);
2993 g = gimple_build_goto (lab3);
2994 gimple_seq_add_stmt (&seq, g);
2995 g = gimple_build_label (lab2);
2996 gimple_seq_add_stmt (&seq, g);
2997 gimple_seq_add_stmt (&seq, stmt);
2998 g = gimple_build_label (lab3);
2999 gimple_seq_add_stmt (&seq, g);
3000 gimple_bind_set_body (bind, seq);
3001 update_stmt (bind);
3002 scan_omp_for (new_stmt, outer_ctx);
3003 scan_omp_for (stmt, outer_ctx)->simt_stmt = new_stmt;
3004 }
3005
3006 static tree omp_find_scan (gimple_stmt_iterator *, bool *,
3007 struct walk_stmt_info *);
3008 static omp_context *maybe_lookup_ctx (gimple *);
3009
3010 /* Duplicate #pragma omp simd, one for the scan input phase loop and one
3011 for scan phase loop. */
3012
3013 static void
3014 scan_omp_simd_scan (gimple_stmt_iterator *gsi, gomp_for *stmt,
3015 omp_context *outer_ctx)
3016 {
3017 /* The only change between inclusive and exclusive scan will be
3018 within the first simd loop, so just use inclusive in the
3019 worksharing loop. */
3020 outer_ctx->scan_inclusive = true;
3021 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_INCLUSIVE);
3022 OMP_CLAUSE_DECL (c) = integer_zero_node;
3023
3024 gomp_scan *input_stmt = gimple_build_omp_scan (NULL, NULL_TREE);
3025 gomp_scan *scan_stmt = gimple_build_omp_scan (NULL, c);
3026 gsi_replace (gsi, input_stmt, false);
3027 gimple_seq input_body = NULL;
3028 gimple_seq_add_stmt (&input_body, stmt);
3029 gsi_insert_after (gsi, scan_stmt, GSI_NEW_STMT);
3030
3031 gimple_stmt_iterator input1_gsi = gsi_none ();
3032 struct walk_stmt_info wi;
3033 memset (&wi, 0, sizeof (wi));
3034 wi.val_only = true;
3035 wi.info = (void *) &input1_gsi;
3036 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), omp_find_scan, NULL, &wi);
3037 gcc_assert (!gsi_end_p (input1_gsi));
3038
3039 gimple *input_stmt1 = gsi_stmt (input1_gsi);
3040 gsi_next (&input1_gsi);
3041 gimple *scan_stmt1 = gsi_stmt (input1_gsi);
3042 gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
3043 c = gimple_omp_scan_clauses (as_a <gomp_scan *> (scan_stmt1));
3044 if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
3045 std::swap (input_stmt1, scan_stmt1);
3046
3047 gimple_seq input_body1 = gimple_omp_body (input_stmt1);
3048 gimple_omp_set_body (input_stmt1, NULL);
3049
3050 gimple_seq scan_body = copy_gimple_seq_and_replace_locals (stmt);
3051 gomp_for *new_stmt = as_a <gomp_for *> (scan_body);
3052
3053 gimple_omp_set_body (input_stmt1, input_body1);
3054 gimple_omp_set_body (scan_stmt1, NULL);
3055
3056 gimple_stmt_iterator input2_gsi = gsi_none ();
3057 memset (&wi, 0, sizeof (wi));
3058 wi.val_only = true;
3059 wi.info = (void *) &input2_gsi;
3060 walk_gimple_seq_mod (gimple_omp_body_ptr (new_stmt), omp_find_scan,
3061 NULL, &wi);
3062 gcc_assert (!gsi_end_p (input2_gsi));
3063
3064 gimple *input_stmt2 = gsi_stmt (input2_gsi);
3065 gsi_next (&input2_gsi);
3066 gimple *scan_stmt2 = gsi_stmt (input2_gsi);
3067 gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
3068 if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
3069 std::swap (input_stmt2, scan_stmt2);
3070
3071 gimple_omp_set_body (input_stmt2, NULL);
3072
3073 gimple_omp_set_body (input_stmt, input_body);
3074 gimple_omp_set_body (scan_stmt, scan_body);
3075
3076 omp_context *ctx = new_omp_context (input_stmt, outer_ctx);
3077 scan_omp (gimple_omp_body_ptr (input_stmt), ctx);
3078
3079 ctx = new_omp_context (scan_stmt, outer_ctx);
3080 scan_omp (gimple_omp_body_ptr (scan_stmt), ctx);
3081
3082 maybe_lookup_ctx (new_stmt)->for_simd_scan_phase = true;
3083 }
3084
3085 /* Scan an OpenMP sections directive. */
3086
3087 static void
3088 scan_omp_sections (gomp_sections *stmt, omp_context *outer_ctx)
3089 {
3090 omp_context *ctx;
3091
3092 ctx = new_omp_context (stmt, outer_ctx);
3093 scan_sharing_clauses (gimple_omp_sections_clauses (stmt), ctx);
3094 scan_omp (gimple_omp_body_ptr (stmt), ctx);
3095 }
3096
3097 /* Scan an OpenMP single directive. */
3098
3099 static void
3100 scan_omp_single (gomp_single *stmt, omp_context *outer_ctx)
3101 {
3102 omp_context *ctx;
3103 tree name;
3104
3105 ctx = new_omp_context (stmt, outer_ctx);
3106 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3107 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3108 name = create_tmp_var_name (".omp_copy_s");
3109 name = build_decl (gimple_location (stmt),
3110 TYPE_DECL, name, ctx->record_type);
3111 TYPE_NAME (ctx->record_type) = name;
3112
3113 scan_sharing_clauses (gimple_omp_single_clauses (stmt), ctx);
3114 scan_omp (gimple_omp_body_ptr (stmt), ctx);
3115
3116 if (TYPE_FIELDS (ctx->record_type) == NULL)
3117 ctx->record_type = NULL;
3118 else
3119 layout_type (ctx->record_type);
3120 }
3121
3122 /* Scan a GIMPLE_OMP_TARGET. */
3123
3124 static void
3125 scan_omp_target (gomp_target *stmt, omp_context *outer_ctx)
3126 {
3127 omp_context *ctx;
3128 tree name;
3129 bool offloaded = is_gimple_omp_offloaded (stmt);
3130 tree clauses = gimple_omp_target_clauses (stmt);
3131
3132 ctx = new_omp_context (stmt, outer_ctx);
3133 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3134 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3135 name = create_tmp_var_name (".omp_data_t");
3136 name = build_decl (gimple_location (stmt),
3137 TYPE_DECL, name, ctx->record_type);
3138 DECL_ARTIFICIAL (name) = 1;
3139 DECL_NAMELESS (name) = 1;
3140 TYPE_NAME (ctx->record_type) = name;
3141 TYPE_ARTIFICIAL (ctx->record_type) = 1;
3142
3143 if (offloaded)
3144 {
3145 create_omp_child_function (ctx, false);
3146 gimple_omp_target_set_child_fn (stmt, ctx->cb.dst_fn);
3147 }
3148
3149 scan_sharing_clauses (clauses, ctx);
3150 scan_omp (gimple_omp_body_ptr (stmt), ctx);
3151
3152 if (TYPE_FIELDS (ctx->record_type) == NULL)
3153 ctx->record_type = ctx->receiver_decl = NULL;
3154 else
3155 {
3156 TYPE_FIELDS (ctx->record_type)
3157 = nreverse (TYPE_FIELDS (ctx->record_type));
3158 if (flag_checking)
3159 {
3160 unsigned int align = DECL_ALIGN (TYPE_FIELDS (ctx->record_type));
3161 for (tree field = TYPE_FIELDS (ctx->record_type);
3162 field;
3163 field = DECL_CHAIN (field))
3164 gcc_assert (DECL_ALIGN (field) == align);
3165 }
3166 layout_type (ctx->record_type);
3167 if (offloaded)
3168 fixup_child_record_type (ctx);
3169 }
3170
3171 if (ctx->teams_nested_p && ctx->nonteams_nested_p)
3172 {
3173 error_at (gimple_location (stmt),
3174 "%<target%> construct with nested %<teams%> construct "
3175 "contains directives outside of the %<teams%> construct");
3176 gimple_omp_set_body (stmt, gimple_build_bind (NULL, NULL, NULL));
3177 }
3178 }
3179
3180 /* Scan an OpenMP teams directive. */
3181
3182 static void
3183 scan_omp_teams (gomp_teams *stmt, omp_context *outer_ctx)
3184 {
3185 omp_context *ctx = new_omp_context (stmt, outer_ctx);
3186
3187 if (!gimple_omp_teams_host (stmt))
3188 {
3189 scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
3190 scan_omp (gimple_omp_body_ptr (stmt), ctx);
3191 return;
3192 }
3193 taskreg_contexts.safe_push (ctx);
3194 gcc_assert (taskreg_nesting_level == 1);
3195 ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3196 ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3197 tree name = create_tmp_var_name (".omp_data_s");
3198 name = build_decl (gimple_location (stmt),
3199 TYPE_DECL, name, ctx->record_type);
3200 DECL_ARTIFICIAL (name) = 1;
3201 DECL_NAMELESS (name) = 1;
3202 TYPE_NAME (ctx->record_type) = name;
3203 TYPE_ARTIFICIAL (ctx->record_type) = 1;
3204 create_omp_child_function (ctx, false);
3205 gimple_omp_teams_set_child_fn (stmt, ctx->cb.dst_fn);
3206
3207 scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
3208 scan_omp (gimple_omp_body_ptr (stmt), ctx);
3209
3210 if (TYPE_FIELDS (ctx->record_type) == NULL)
3211 ctx->record_type = ctx->receiver_decl = NULL;
3212 }
3213
3214 /* Check nesting restrictions. */
3215 static bool
3216 check_omp_nesting_restrictions (gimple *stmt, omp_context *ctx)
3217 {
3218 tree c;
3219
3220 /* No nesting of non-OpenACC STMT (that is, an OpenMP one, or a GOMP builtin)
3221 inside an OpenACC CTX. */
3222 if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3223 || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE)
3224 /* ..., except for the atomic codes that OpenACC shares with OpenMP. */
3225 ;
3226 else if (!(is_gimple_omp (stmt)
3227 && is_gimple_omp_oacc (stmt)))
3228 {
3229 if (oacc_get_fn_attrib (cfun->decl) != NULL)
3230 {
3231 error_at (gimple_location (stmt),
3232 "non-OpenACC construct inside of OpenACC routine");
3233 return false;
3234 }
3235 else
3236 for (omp_context *octx = ctx; octx != NULL; octx = octx->outer)
3237 if (is_gimple_omp (octx->stmt)
3238 && is_gimple_omp_oacc (octx->stmt))
3239 {
3240 error_at (gimple_location (stmt),
3241 "non-OpenACC construct inside of OpenACC region");
3242 return false;
3243 }
3244 }
3245
3246 if (ctx != NULL)
3247 {
3248 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
3249 && gimple_omp_target_kind (ctx->stmt) == GF_OMP_TARGET_KIND_REGION)
3250 {
3251 c = omp_find_clause (gimple_omp_target_clauses (ctx->stmt),
3252 OMP_CLAUSE_DEVICE);
3253 if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
3254 {
3255 error_at (gimple_location (stmt),
3256 "OpenMP constructs are not allowed in target region "
3257 "with %<ancestor%>");
3258 return false;
3259 }
3260
3261 if (gimple_code (stmt) == GIMPLE_OMP_TEAMS && !ctx->teams_nested_p)
3262 ctx->teams_nested_p = true;
3263 else
3264 ctx->nonteams_nested_p = true;
3265 }
3266 if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN
3267 && ctx->outer
3268 && gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
3269 ctx = ctx->outer;
3270 if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
3271 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
3272 && !ctx->loop_p)
3273 {
3274 c = NULL_TREE;
3275 if (ctx->order_concurrent
3276 && (gimple_code (stmt) == GIMPLE_OMP_ORDERED
3277 || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3278 || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE))
3279 {
3280 error_at (gimple_location (stmt),
3281 "OpenMP constructs other than %<parallel%>, %<loop%>"
3282 " or %<simd%> may not be nested inside a region with"
3283 " the %<order(concurrent)%> clause");
3284 return false;
3285 }
3286 if (gimple_code (stmt) == GIMPLE_OMP_ORDERED)
3287 {
3288 c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3289 if (omp_find_clause (c, OMP_CLAUSE_SIMD))
3290 {
3291 if (omp_find_clause (c, OMP_CLAUSE_THREADS)
3292 && (ctx->outer == NULL
3293 || !gimple_omp_for_combined_into_p (ctx->stmt)
3294 || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR
3295 || (gimple_omp_for_kind (ctx->outer->stmt)
3296 != GF_OMP_FOR_KIND_FOR)
3297 || !gimple_omp_for_combined_p (ctx->outer->stmt)))
3298 {
3299 error_at (gimple_location (stmt),
3300 "%<ordered simd threads%> must be closely "
3301 "nested inside of %<%s simd%> region",
3302 lang_GNU_Fortran () ? "do" : "for");
3303 return false;
3304 }
3305 return true;
3306 }
3307 }
3308 else if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3309 || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE
3310 || gimple_code (stmt) == GIMPLE_OMP_SCAN)
3311 return true;
3312 else if (gimple_code (stmt) == GIMPLE_OMP_FOR
3313 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
3314 return true;
3315 error_at (gimple_location (stmt),
3316 "OpenMP constructs other than "
3317 "%<ordered simd%>, %<simd%>, %<loop%> or %<atomic%> may "
3318 "not be nested inside %<simd%> region");
3319 return false;
3320 }
3321 else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
3322 {
3323 if ((gimple_code (stmt) != GIMPLE_OMP_FOR
3324 || (gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_DISTRIBUTE
3325 && omp_find_clause (gimple_omp_for_clauses (stmt),
3326 OMP_CLAUSE_BIND) == NULL_TREE))
3327 && gimple_code (stmt) != GIMPLE_OMP_PARALLEL)
3328 {
3329 error_at (gimple_location (stmt),
3330 "only %<distribute%>, %<parallel%> or %<loop%> "
3331 "regions are allowed to be strictly nested inside "
3332 "%<teams%> region");
3333 return false;
3334 }
3335 }
3336 else if (ctx->order_concurrent
3337 && gimple_code (stmt) != GIMPLE_OMP_PARALLEL
3338 && (gimple_code (stmt) != GIMPLE_OMP_FOR
3339 || gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_SIMD)
3340 && gimple_code (stmt) != GIMPLE_OMP_SCAN)
3341 {
3342 if (ctx->loop_p)
3343 error_at (gimple_location (stmt),
3344 "OpenMP constructs other than %<parallel%>, %<loop%> or "
3345 "%<simd%> may not be nested inside a %<loop%> region");
3346 else
3347 error_at (gimple_location (stmt),
3348 "OpenMP constructs other than %<parallel%>, %<loop%> or "
3349 "%<simd%> may not be nested inside a region with "
3350 "the %<order(concurrent)%> clause");
3351 return false;
3352 }
3353 }
3354 switch (gimple_code (stmt))
3355 {
3356 case GIMPLE_OMP_FOR:
3357 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD)
3358 return true;
3359 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
3360 {
3361 if (ctx != NULL && gimple_code (ctx->stmt) != GIMPLE_OMP_TEAMS)
3362 {
3363 error_at (gimple_location (stmt),
3364 "%<distribute%> region must be strictly nested "
3365 "inside %<teams%> construct");
3366 return false;
3367 }
3368 return true;
3369 }
3370 /* We split taskloop into task and nested taskloop in it. */
3371 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_TASKLOOP)
3372 return true;
3373 /* For now, hope this will change and loop bind(parallel) will not
3374 be allowed in lots of contexts. */
3375 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
3376 && omp_find_clause (gimple_omp_for_clauses (stmt), OMP_CLAUSE_BIND))
3377 return true;
3378 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
3379 {
3380 bool ok = false;
3381
3382 if (ctx)
3383 switch (gimple_code (ctx->stmt))
3384 {
3385 case GIMPLE_OMP_FOR:
3386 ok = (gimple_omp_for_kind (ctx->stmt)
3387 == GF_OMP_FOR_KIND_OACC_LOOP);
3388 break;
3389
3390 case GIMPLE_OMP_TARGET:
3391 switch (gimple_omp_target_kind (ctx->stmt))
3392 {
3393 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
3394 case GF_OMP_TARGET_KIND_OACC_KERNELS:
3395 case GF_OMP_TARGET_KIND_OACC_SERIAL:
3396 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
3397 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
3398 ok = true;
3399 break;
3400
3401 default:
3402 break;
3403 }
3404
3405 default:
3406 break;
3407 }
3408 else if (oacc_get_fn_attrib (current_function_decl))
3409 ok = true;
3410 if (!ok)
3411 {
3412 error_at (gimple_location (stmt),
3413 "OpenACC loop directive must be associated with"
3414 " an OpenACC compute region");
3415 return false;
3416 }
3417 }
3418 /* FALLTHRU */
3419 case GIMPLE_CALL:
3420 if (is_gimple_call (stmt)
3421 && (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3422 == BUILT_IN_GOMP_CANCEL
3423 || DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3424 == BUILT_IN_GOMP_CANCELLATION_POINT))
3425 {
3426 const char *bad = NULL;
3427 const char *kind = NULL;
3428 const char *construct
3429 = (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3430 == BUILT_IN_GOMP_CANCEL)
3431 ? "cancel"
3432 : "cancellation point";
3433 if (ctx == NULL)
3434 {
3435 error_at (gimple_location (stmt), "orphaned %qs construct",
3436 construct);
3437 return false;
3438 }
3439 switch (tree_fits_shwi_p (gimple_call_arg (stmt, 0))
3440 ? tree_to_shwi (gimple_call_arg (stmt, 0))
3441 : 0)
3442 {
3443 case 1:
3444 if (gimple_code (ctx->stmt) != GIMPLE_OMP_PARALLEL)
3445 bad = "parallel";
3446 else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3447 == BUILT_IN_GOMP_CANCEL
3448 && !integer_zerop (gimple_call_arg (stmt, 1)))
3449 ctx->cancellable = true;
3450 kind = "parallel";
3451 break;
3452 case 2:
3453 if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
3454 || gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR)
3455 bad = "for";
3456 else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3457 == BUILT_IN_GOMP_CANCEL
3458 && !integer_zerop (gimple_call_arg (stmt, 1)))
3459 {
3460 ctx->cancellable = true;
3461 if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3462 OMP_CLAUSE_NOWAIT))
3463 warning_at (gimple_location (stmt), OPT_Wopenmp,
3464 "%<cancel for%> inside "
3465 "%<nowait%> for construct");
3466 if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3467 OMP_CLAUSE_ORDERED))
3468 warning_at (gimple_location (stmt), OPT_Wopenmp,
3469 "%<cancel for%> inside "
3470 "%<ordered%> for construct");
3471 }
3472 kind = "for";
3473 break;
3474 case 4:
3475 if (gimple_code (ctx->stmt) != GIMPLE_OMP_SECTIONS
3476 && gimple_code (ctx->stmt) != GIMPLE_OMP_SECTION)
3477 bad = "sections";
3478 else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3479 == BUILT_IN_GOMP_CANCEL
3480 && !integer_zerop (gimple_call_arg (stmt, 1)))
3481 {
3482 if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
3483 {
3484 ctx->cancellable = true;
3485 if (omp_find_clause (gimple_omp_sections_clauses
3486 (ctx->stmt),
3487 OMP_CLAUSE_NOWAIT))
3488 warning_at (gimple_location (stmt), OPT_Wopenmp,
3489 "%<cancel sections%> inside "
3490 "%<nowait%> sections construct");
3491 }
3492 else
3493 {
3494 gcc_assert (ctx->outer
3495 && gimple_code (ctx->outer->stmt)
3496 == GIMPLE_OMP_SECTIONS);
3497 ctx->outer->cancellable = true;
3498 if (omp_find_clause (gimple_omp_sections_clauses
3499 (ctx->outer->stmt),
3500 OMP_CLAUSE_NOWAIT))
3501 warning_at (gimple_location (stmt), OPT_Wopenmp,
3502 "%<cancel sections%> inside "
3503 "%<nowait%> sections construct");
3504 }
3505 }
3506 kind = "sections";
3507 break;
3508 case 8:
3509 if (!is_task_ctx (ctx)
3510 && (!is_taskloop_ctx (ctx)
3511 || ctx->outer == NULL
3512 || !is_task_ctx (ctx->outer)))
3513 bad = "task";
3514 else
3515 {
3516 for (omp_context *octx = ctx->outer;
3517 octx; octx = octx->outer)
3518 {
3519 switch (gimple_code (octx->stmt))
3520 {
3521 case GIMPLE_OMP_TASKGROUP:
3522 break;
3523 case GIMPLE_OMP_TARGET:
3524 if (gimple_omp_target_kind (octx->stmt)
3525 != GF_OMP_TARGET_KIND_REGION)
3526 continue;
3527 /* FALLTHRU */
3528 case GIMPLE_OMP_PARALLEL:
3529 case GIMPLE_OMP_TEAMS:
3530 error_at (gimple_location (stmt),
3531 "%<%s taskgroup%> construct not closely "
3532 "nested inside of %<taskgroup%> region",
3533 construct);
3534 return false;
3535 case GIMPLE_OMP_TASK:
3536 if (gimple_omp_task_taskloop_p (octx->stmt)
3537 && octx->outer
3538 && is_taskloop_ctx (octx->outer))
3539 {
3540 tree clauses
3541 = gimple_omp_for_clauses (octx->outer->stmt);
3542 if (!omp_find_clause (clauses, OMP_CLAUSE_NOGROUP))
3543 break;
3544 }
3545 continue;
3546 default:
3547 continue;
3548 }
3549 break;
3550 }
3551 ctx->cancellable = true;
3552 }
3553 kind = "taskgroup";
3554 break;
3555 default:
3556 error_at (gimple_location (stmt), "invalid arguments");
3557 return false;
3558 }
3559 if (bad)
3560 {
3561 error_at (gimple_location (stmt),
3562 "%<%s %s%> construct not closely nested inside of %qs",
3563 construct, kind, bad);
3564 return false;
3565 }
3566 }
3567 /* FALLTHRU */
3568 case GIMPLE_OMP_SECTIONS:
3569 case GIMPLE_OMP_SINGLE:
3570 for (; ctx != NULL; ctx = ctx->outer)
3571 switch (gimple_code (ctx->stmt))
3572 {
3573 case GIMPLE_OMP_FOR:
3574 if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3575 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3576 break;
3577 /* FALLTHRU */
3578 case GIMPLE_OMP_SECTIONS:
3579 case GIMPLE_OMP_SINGLE:
3580 case GIMPLE_OMP_ORDERED:
3581 case GIMPLE_OMP_MASTER:
3582 case GIMPLE_OMP_MASKED:
3583 case GIMPLE_OMP_TASK:
3584 case GIMPLE_OMP_CRITICAL:
3585 if (is_gimple_call (stmt))
3586 {
3587 if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3588 != BUILT_IN_GOMP_BARRIER)
3589 return true;
3590 error_at (gimple_location (stmt),
3591 "barrier region may not be closely nested inside "
3592 "of work-sharing, %<loop%>, %<critical%>, "
3593 "%<ordered%>, %<master%>, %<masked%>, explicit "
3594 "%<task%> or %<taskloop%> region");
3595 return false;
3596 }
3597 error_at (gimple_location (stmt),
3598 "work-sharing region may not be closely nested inside "
3599 "of work-sharing, %<loop%>, %<critical%>, %<ordered%>, "
3600 "%<master%>, %<masked%>, explicit %<task%> or "
3601 "%<taskloop%> region");
3602 return false;
3603 case GIMPLE_OMP_PARALLEL:
3604 case GIMPLE_OMP_TEAMS:
3605 return true;
3606 case GIMPLE_OMP_TARGET:
3607 if (gimple_omp_target_kind (ctx->stmt)
3608 == GF_OMP_TARGET_KIND_REGION)
3609 return true;
3610 break;
3611 default:
3612 break;
3613 }
3614 break;
3615 case GIMPLE_OMP_MASTER:
3616 case GIMPLE_OMP_MASKED:
3617 for (; ctx != NULL; ctx = ctx->outer)
3618 switch (gimple_code (ctx->stmt))
3619 {
3620 case GIMPLE_OMP_FOR:
3621 if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3622 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3623 break;
3624 /* FALLTHRU */
3625 case GIMPLE_OMP_SECTIONS:
3626 case GIMPLE_OMP_SINGLE:
3627 case GIMPLE_OMP_TASK:
3628 error_at (gimple_location (stmt),
3629 "%qs region may not be closely nested inside "
3630 "of work-sharing, %<loop%>, explicit %<task%> or "
3631 "%<taskloop%> region",
3632 gimple_code (stmt) == GIMPLE_OMP_MASTER
3633 ? "master" : "masked");
3634 return false;
3635 case GIMPLE_OMP_PARALLEL:
3636 case GIMPLE_OMP_TEAMS:
3637 return true;
3638 case GIMPLE_OMP_TARGET:
3639 if (gimple_omp_target_kind (ctx->stmt)
3640 == GF_OMP_TARGET_KIND_REGION)
3641 return true;
3642 break;
3643 default:
3644 break;
3645 }
3646 break;
3647 case GIMPLE_OMP_SCOPE:
3648 for (; ctx != NULL; ctx = ctx->outer)
3649 switch (gimple_code (ctx->stmt))
3650 {
3651 case GIMPLE_OMP_FOR:
3652 if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3653 && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3654 break;
3655 /* FALLTHRU */
3656 case GIMPLE_OMP_SECTIONS:
3657 case GIMPLE_OMP_SINGLE:
3658 case GIMPLE_OMP_TASK:
3659 case GIMPLE_OMP_CRITICAL:
3660 case GIMPLE_OMP_ORDERED:
3661 case GIMPLE_OMP_MASTER:
3662 case GIMPLE_OMP_MASKED:
3663 error_at (gimple_location (stmt),
3664 "%<scope%> region may not be closely nested inside "
3665 "of work-sharing, %<loop%>, explicit %<task%>, "
3666 "%<taskloop%>, %<critical%>, %<ordered%>, %<master%>, "
3667 "or %<masked%> region");
3668 return false;
3669 case GIMPLE_OMP_PARALLEL:
3670 case GIMPLE_OMP_TEAMS:
3671 return true;
3672 case GIMPLE_OMP_TARGET:
3673 if (gimple_omp_target_kind (ctx->stmt)
3674 == GF_OMP_TARGET_KIND_REGION)
3675 return true;
3676 break;
3677 default:
3678 break;
3679 }
3680 break;
3681 case GIMPLE_OMP_TASK:
3682 for (c = gimple_omp_task_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
3683 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
3684 {
3685 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
3686 error_at (OMP_CLAUSE_LOCATION (c),
3687 "%<%s(%s)%> is only allowed in %<omp ordered%>",
3688 OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross",
3689 kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
3690 return false;
3691 }
3692 break;
3693 case GIMPLE_OMP_ORDERED:
3694 for (c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3695 c; c = OMP_CLAUSE_CHAIN (c))
3696 {
3697 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS)
3698 {
3699 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
3700 {
3701 error_at (OMP_CLAUSE_LOCATION (c),
3702 "invalid depend kind in omp %<ordered%> %<depend%>");
3703 return false;
3704 }
3705 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREADS
3706 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMD);
3707 continue;
3708 }
3709
3710 tree oclause;
3711 /* Look for containing ordered(N) loop. */
3712 if (ctx == NULL
3713 || gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
3714 || (oclause
3715 = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3716 OMP_CLAUSE_ORDERED)) == NULL_TREE)
3717 {
3718 error_at (OMP_CLAUSE_LOCATION (c),
3719 "%<ordered%> construct with %<depend%> clause "
3720 "must be closely nested inside an %<ordered%> loop");
3721 return false;
3722 }
3723 }
3724 c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3725 if (omp_find_clause (c, OMP_CLAUSE_SIMD))
3726 {
3727 /* ordered simd must be closely nested inside of simd region,
3728 and simd region must not encounter constructs other than
3729 ordered simd, therefore ordered simd may be either orphaned,
3730 or ctx->stmt must be simd. The latter case is handled already
3731 earlier. */
3732 if (ctx != NULL)
3733 {
3734 error_at (gimple_location (stmt),
3735 "%<ordered%> %<simd%> must be closely nested inside "
3736 "%<simd%> region");
3737 return false;
3738 }
3739 }
3740 for (; ctx != NULL; ctx = ctx->outer)
3741 switch (gimple_code (ctx->stmt))
3742 {
3743 case GIMPLE_OMP_CRITICAL:
3744 case GIMPLE_OMP_TASK:
3745 case GIMPLE_OMP_ORDERED:
3746 ordered_in_taskloop:
3747 error_at (gimple_location (stmt),
3748 "%<ordered%> region may not be closely nested inside "
3749 "of %<critical%>, %<ordered%>, explicit %<task%> or "
3750 "%<taskloop%> region");
3751 return false;
3752 case GIMPLE_OMP_FOR:
3753 if (gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_TASKLOOP)
3754 goto ordered_in_taskloop;
3755 tree o;
3756 o = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3757 OMP_CLAUSE_ORDERED);
3758 if (o == NULL)
3759 {
3760 error_at (gimple_location (stmt),
3761 "%<ordered%> region must be closely nested inside "
3762 "a loop region with an %<ordered%> clause");
3763 return false;
3764 }
3765 if (!gimple_omp_ordered_standalone_p (stmt))
3766 {
3767 if (OMP_CLAUSE_ORDERED_DOACROSS (o))
3768 {
3769 error_at (gimple_location (stmt),
3770 "%<ordered%> construct without %<doacross%> or "
3771 "%<depend%> clauses must not have the same "
3772 "binding region as %<ordered%> construct with "
3773 "those clauses");
3774 return false;
3775 }
3776 else if (OMP_CLAUSE_ORDERED_EXPR (o))
3777 {
3778 tree co
3779 = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3780 OMP_CLAUSE_COLLAPSE);
3781 HOST_WIDE_INT
3782 o_n = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (o));
3783 HOST_WIDE_INT c_n = 1;
3784 if (co)
3785 c_n = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (co));
3786 if (o_n != c_n)
3787 {
3788 error_at (gimple_location (stmt),
3789 "%<ordered%> construct without %<doacross%> "
3790 "or %<depend%> clauses binds to loop where "
3791 "%<collapse%> argument %wd is different from "
3792 "%<ordered%> argument %wd", c_n, o_n);
3793 return false;
3794 }
3795 }
3796 }
3797 return true;
3798 case GIMPLE_OMP_TARGET:
3799 if (gimple_omp_target_kind (ctx->stmt)
3800 != GF_OMP_TARGET_KIND_REGION)
3801 break;
3802 /* FALLTHRU */
3803 case GIMPLE_OMP_PARALLEL:
3804 case GIMPLE_OMP_TEAMS:
3805 error_at (gimple_location (stmt),
3806 "%<ordered%> region must be closely nested inside "
3807 "a loop region with an %<ordered%> clause");
3808 return false;
3809 default:
3810 break;
3811 }
3812 break;
3813 case GIMPLE_OMP_CRITICAL:
3814 {
3815 tree this_stmt_name
3816 = gimple_omp_critical_name (as_a <gomp_critical *> (stmt));
3817 for (; ctx != NULL; ctx = ctx->outer)
3818 if (gomp_critical *other_crit
3819 = dyn_cast <gomp_critical *> (ctx->stmt))
3820 if (this_stmt_name == gimple_omp_critical_name (other_crit))
3821 {
3822 error_at (gimple_location (stmt),
3823 "%<critical%> region may not be nested inside "
3824 "a %<critical%> region with the same name");
3825 return false;
3826 }
3827 }
3828 break;
3829 case GIMPLE_OMP_TEAMS:
3830 if (ctx == NULL)
3831 break;
3832 else if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET
3833 || (gimple_omp_target_kind (ctx->stmt)
3834 != GF_OMP_TARGET_KIND_REGION))
3835 {
3836 /* Teams construct can appear either strictly nested inside of
3837 target construct with no intervening stmts, or can be encountered
3838 only by initial task (so must not appear inside any OpenMP
3839 construct. */
3840 error_at (gimple_location (stmt),
3841 "%<teams%> construct must be closely nested inside of "
3842 "%<target%> construct or not nested in any OpenMP "
3843 "construct");
3844 return false;
3845 }
3846 break;
3847 case GIMPLE_OMP_TARGET:
3848 for (c = gimple_omp_target_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
3849 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
3850 {
3851 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
3852 error_at (OMP_CLAUSE_LOCATION (c),
3853 "%<depend(%s)%> is only allowed in %<omp ordered%>",
3854 kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
3855 return false;
3856 }
3857 if (is_gimple_omp_offloaded (stmt)
3858 && oacc_get_fn_attrib (cfun->decl) != NULL)
3859 {
3860 error_at (gimple_location (stmt),
3861 "OpenACC region inside of OpenACC routine, nested "
3862 "parallelism not supported yet");
3863 return false;
3864 }
3865 for (; ctx != NULL; ctx = ctx->outer)
3866 {
3867 if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET)
3868 {
3869 if (is_gimple_omp (stmt)
3870 && is_gimple_omp_oacc (stmt)
3871 && is_gimple_omp (ctx->stmt))
3872 {
3873 error_at (gimple_location (stmt),
3874 "OpenACC construct inside of non-OpenACC region");
3875 return false;
3876 }
3877 continue;
3878 }
3879
3880 const char *stmt_name, *ctx_stmt_name;
3881 switch (gimple_omp_target_kind (stmt))
3882 {
3883 case GF_OMP_TARGET_KIND_REGION: stmt_name = "target"; break;
3884 case GF_OMP_TARGET_KIND_DATA: stmt_name = "target data"; break;
3885 case GF_OMP_TARGET_KIND_UPDATE: stmt_name = "target update"; break;
3886 case GF_OMP_TARGET_KIND_ENTER_DATA:
3887 stmt_name = "target enter data"; break;
3888 case GF_OMP_TARGET_KIND_EXIT_DATA:
3889 stmt_name = "target exit data"; break;
3890 case GF_OMP_TARGET_KIND_OACC_PARALLEL: stmt_name = "parallel"; break;
3891 case GF_OMP_TARGET_KIND_OACC_KERNELS: stmt_name = "kernels"; break;
3892 case GF_OMP_TARGET_KIND_OACC_SERIAL: stmt_name = "serial"; break;
3893 case GF_OMP_TARGET_KIND_OACC_DATA: stmt_name = "data"; break;
3894 case GF_OMP_TARGET_KIND_OACC_UPDATE: stmt_name = "update"; break;
3895 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
3896 stmt_name = "enter data"; break;
3897 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
3898 stmt_name = "exit data"; break;
3899 case GF_OMP_TARGET_KIND_OACC_DECLARE: stmt_name = "declare"; break;
3900 case GF_OMP_TARGET_KIND_OACC_HOST_DATA: stmt_name = "host_data";
3901 break;
3902 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
3903 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
3904 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
3905 /* OpenACC 'kernels' decomposed parts. */
3906 stmt_name = "kernels"; break;
3907 default: gcc_unreachable ();
3908 }
3909 switch (gimple_omp_target_kind (ctx->stmt))
3910 {
3911 case GF_OMP_TARGET_KIND_REGION: ctx_stmt_name = "target"; break;
3912 case GF_OMP_TARGET_KIND_DATA: ctx_stmt_name = "target data"; break;
3913 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
3914 ctx_stmt_name = "parallel"; break;
3915 case GF_OMP_TARGET_KIND_OACC_KERNELS:
3916 ctx_stmt_name = "kernels"; break;
3917 case GF_OMP_TARGET_KIND_OACC_SERIAL:
3918 ctx_stmt_name = "serial"; break;
3919 case GF_OMP_TARGET_KIND_OACC_DATA: ctx_stmt_name = "data"; break;
3920 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
3921 ctx_stmt_name = "host_data"; break;
3922 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
3923 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
3924 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
3925 /* OpenACC 'kernels' decomposed parts. */
3926 ctx_stmt_name = "kernels"; break;
3927 default: gcc_unreachable ();
3928 }
3929
3930 /* OpenACC/OpenMP mismatch? */
3931 if (is_gimple_omp_oacc (stmt)
3932 != is_gimple_omp_oacc (ctx->stmt))
3933 {
3934 error_at (gimple_location (stmt),
3935 "%s %qs construct inside of %s %qs region",
3936 (is_gimple_omp_oacc (stmt)
3937 ? "OpenACC" : "OpenMP"), stmt_name,
3938 (is_gimple_omp_oacc (ctx->stmt)
3939 ? "OpenACC" : "OpenMP"), ctx_stmt_name);
3940 return false;
3941 }
3942 if (is_gimple_omp_offloaded (ctx->stmt))
3943 {
3944 /* No GIMPLE_OMP_TARGET inside offloaded OpenACC CTX. */
3945 if (is_gimple_omp_oacc (ctx->stmt))
3946 {
3947 error_at (gimple_location (stmt),
3948 "%qs construct inside of %qs region",
3949 stmt_name, ctx_stmt_name);
3950 return false;
3951 }
3952 else
3953 {
3954 if ((gimple_omp_target_kind (ctx->stmt)
3955 == GF_OMP_TARGET_KIND_REGION)
3956 && (gimple_omp_target_kind (stmt)
3957 == GF_OMP_TARGET_KIND_REGION))
3958 {
3959 c = omp_find_clause (gimple_omp_target_clauses (stmt),
3960 OMP_CLAUSE_DEVICE);
3961 if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
3962 break;
3963 }
3964 warning_at (gimple_location (stmt), OPT_Wopenmp,
3965 "%qs construct inside of %qs region",
3966 stmt_name, ctx_stmt_name);
3967 }
3968 }
3969 }
3970 break;
3971 default:
3972 break;
3973 }
3974 return true;
3975 }
3976
3977
3978 /* Helper function scan_omp.
3979
3980 Callback for walk_tree or operators in walk_gimple_stmt used to
3981 scan for OMP directives in TP. */
3982
3983 static tree
3984 scan_omp_1_op (tree *tp, int *walk_subtrees, void *data)
3985 {
3986 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
3987 omp_context *ctx = (omp_context *) wi->info;
3988 tree t = *tp;
3989 tree tmp;
3990
3991 switch (TREE_CODE (t))
3992 {
3993 case VAR_DECL:
3994 case PARM_DECL:
3995 case LABEL_DECL:
3996 case RESULT_DECL:
3997 if (ctx)
3998 {
3999 tmp = NULL_TREE;
4000 if (TREE_CODE (t) == VAR_DECL
4001 && (tmp = lookup_attribute ("omp allocate var",
4002 DECL_ATTRIBUTES (t))) != NULL_TREE)
4003 t = TREE_VALUE (TREE_VALUE (tmp));
4004 tree repl = remap_decl (t, &ctx->cb);
4005 gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
4006 if (tmp != NULL_TREE && t != repl)
4007 *tp = build_fold_addr_expr (repl);
4008 else if (tmp == NULL_TREE)
4009 *tp = repl;
4010 }
4011 break;
4012
4013 case INDIRECT_REF:
4014 case MEM_REF:
4015 if (ctx
4016 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
4017 && ((tmp = lookup_attribute ("omp allocate var",
4018 DECL_ATTRIBUTES (TREE_OPERAND (t, 0))))
4019 != NULL_TREE))
4020 {
4021 tmp = TREE_VALUE (TREE_VALUE (tmp));
4022 tree repl = remap_decl (tmp, &ctx->cb);
4023 gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
4024 if (tmp != repl)
4025 *tp = repl;
4026 break;
4027 }
4028 gcc_fallthrough ();
4029
4030 default:
4031 if (ctx && TYPE_P (t))
4032 *tp = remap_type (t, &ctx->cb);
4033 else if (!DECL_P (t))
4034 {
4035 *walk_subtrees = 1;
4036 if (ctx)
4037 {
4038 tree tem = remap_type (TREE_TYPE (t), &ctx->cb);
4039 if (tem != TREE_TYPE (t))
4040 {
4041 if (TREE_CODE (t) == INTEGER_CST)
4042 *tp = wide_int_to_tree (tem, wi::to_wide (t));
4043 else
4044 TREE_TYPE (t) = tem;
4045 }
4046 }
4047 }
4048 break;
4049 }
4050
4051 return NULL_TREE;
4052 }
4053
4054 /* Return true if FNDECL is a setjmp or a longjmp. */
4055
4056 static bool
4057 setjmp_or_longjmp_p (const_tree fndecl)
4058 {
4059 if (fndecl_built_in_p (fndecl, BUILT_IN_SETJMP, BUILT_IN_LONGJMP))
4060 return true;
4061
4062 tree declname = DECL_NAME (fndecl);
4063 if (!declname
4064 || (DECL_CONTEXT (fndecl) != NULL_TREE
4065 && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
4066 || !TREE_PUBLIC (fndecl))
4067 return false;
4068
4069 const char *name = IDENTIFIER_POINTER (declname);
4070 return !strcmp (name, "setjmp") || !strcmp (name, "longjmp");
4071 }
4072
4073 /* Helper function for scan_omp.
4074
4075 Callback for walk_gimple_stmt used to scan for OMP directives in
4076 the current statement in GSI. */
4077
4078 static tree
4079 scan_omp_1_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
4080 struct walk_stmt_info *wi)
4081 {
4082 gimple *stmt = gsi_stmt (*gsi);
4083 omp_context *ctx = (omp_context *) wi->info;
4084
4085 if (gimple_has_location (stmt))
4086 input_location = gimple_location (stmt);
4087
4088 /* Check the nesting restrictions. */
4089 bool remove = false;
4090 if (is_gimple_omp (stmt))
4091 remove = !check_omp_nesting_restrictions (stmt, ctx);
4092 else if (is_gimple_call (stmt))
4093 {
4094 tree fndecl = gimple_call_fndecl (stmt);
4095 if (fndecl)
4096 {
4097 if (ctx
4098 && gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
4099 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
4100 && setjmp_or_longjmp_p (fndecl)
4101 && !ctx->loop_p)
4102 {
4103 remove = true;
4104 error_at (gimple_location (stmt),
4105 "setjmp/longjmp inside %<simd%> construct");
4106 }
4107 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4108 switch (DECL_FUNCTION_CODE (fndecl))
4109 {
4110 case BUILT_IN_GOMP_BARRIER:
4111 case BUILT_IN_GOMP_CANCEL:
4112 case BUILT_IN_GOMP_CANCELLATION_POINT:
4113 case BUILT_IN_GOMP_TASKYIELD:
4114 case BUILT_IN_GOMP_TASKWAIT:
4115 case BUILT_IN_GOMP_TASKGROUP_START:
4116 case BUILT_IN_GOMP_TASKGROUP_END:
4117 remove = !check_omp_nesting_restrictions (stmt, ctx);
4118 break;
4119 default:
4120 break;
4121 }
4122 else if (ctx)
4123 {
4124 omp_context *octx = ctx;
4125 if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN && ctx->outer)
4126 octx = ctx->outer;
4127 if (octx->order_concurrent && omp_runtime_api_call (fndecl))
4128 {
4129 remove = true;
4130 error_at (gimple_location (stmt),
4131 "OpenMP runtime API call %qD in a region with "
4132 "%<order(concurrent)%> clause", fndecl);
4133 }
4134 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
4135 && omp_runtime_api_call (fndecl)
4136 && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
4137 != strlen ("omp_get_num_teams"))
4138 || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4139 "omp_get_num_teams") != 0)
4140 && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
4141 != strlen ("omp_get_team_num"))
4142 || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4143 "omp_get_team_num") != 0))
4144 {
4145 remove = true;
4146 error_at (gimple_location (stmt),
4147 "OpenMP runtime API call %qD strictly nested in a "
4148 "%<teams%> region", fndecl);
4149 }
4150 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
4151 && (gimple_omp_target_kind (ctx->stmt)
4152 == GF_OMP_TARGET_KIND_REGION)
4153 && omp_runtime_api_call (fndecl))
4154 {
4155 tree tgt_clauses = gimple_omp_target_clauses (ctx->stmt);
4156 tree c = omp_find_clause (tgt_clauses, OMP_CLAUSE_DEVICE);
4157 if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
4158 error_at (gimple_location (stmt),
4159 "OpenMP runtime API call %qD in a region with "
4160 "%<device(ancestor)%> clause", fndecl);
4161 }
4162 }
4163 }
4164 }
4165 if (remove)
4166 {
4167 stmt = gimple_build_nop ();
4168 gsi_replace (gsi, stmt, false);
4169 }
4170
4171 *handled_ops_p = true;
4172
4173 switch (gimple_code (stmt))
4174 {
4175 case GIMPLE_OMP_PARALLEL:
4176 taskreg_nesting_level++;
4177 scan_omp_parallel (gsi, ctx);
4178 taskreg_nesting_level--;
4179 break;
4180
4181 case GIMPLE_OMP_TASK:
4182 taskreg_nesting_level++;
4183 scan_omp_task (gsi, ctx);
4184 taskreg_nesting_level--;
4185 break;
4186
4187 case GIMPLE_OMP_FOR:
4188 if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
4189 == GF_OMP_FOR_KIND_SIMD)
4190 && gimple_omp_for_combined_into_p (stmt)
4191 && gimple_code (ctx->stmt) != GIMPLE_OMP_SCAN)
4192 {
4193 tree clauses = gimple_omp_for_clauses (as_a <gomp_for *> (stmt));
4194 tree c = omp_find_clause (clauses, OMP_CLAUSE_REDUCTION);
4195 if (c && OMP_CLAUSE_REDUCTION_INSCAN (c) && !seen_error ())
4196 {
4197 scan_omp_simd_scan (gsi, as_a <gomp_for *> (stmt), ctx);
4198 break;
4199 }
4200 }
4201 if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
4202 == GF_OMP_FOR_KIND_SIMD)
4203 && omp_maybe_offloaded_ctx (ctx)
4204 && omp_max_simt_vf ()
4205 && gimple_omp_for_collapse (stmt) == 1)
4206 scan_omp_simd (gsi, as_a <gomp_for *> (stmt), ctx);
4207 else
4208 scan_omp_for (as_a <gomp_for *> (stmt), ctx);
4209 break;
4210
4211 case GIMPLE_OMP_SCOPE:
4212 ctx = new_omp_context (stmt, ctx);
4213 scan_sharing_clauses (gimple_omp_scope_clauses (stmt), ctx);
4214 scan_omp (gimple_omp_body_ptr (stmt), ctx);
4215 break;
4216
4217 case GIMPLE_OMP_DISPATCH:
4218 ctx = new_omp_context (stmt, ctx);
4219 scan_omp (gimple_omp_body_ptr (stmt), ctx);
4220 break;
4221
4222 case GIMPLE_OMP_INTEROP:
4223 ctx = new_omp_context (stmt, ctx);
4224 break;
4225
4226 case GIMPLE_OMP_SECTIONS:
4227 scan_omp_sections (as_a <gomp_sections *> (stmt), ctx);
4228 break;
4229
4230 case GIMPLE_OMP_SINGLE:
4231 scan_omp_single (as_a <gomp_single *> (stmt), ctx);
4232 break;
4233
4234 case GIMPLE_OMP_SCAN:
4235 if (tree clauses = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)))
4236 {
4237 if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_INCLUSIVE)
4238 ctx->scan_inclusive = true;
4239 else if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_EXCLUSIVE)
4240 ctx->scan_exclusive = true;
4241 }
4242 /* FALLTHRU */
4243 case GIMPLE_OMP_SECTION:
4244 case GIMPLE_OMP_STRUCTURED_BLOCK:
4245 case GIMPLE_OMP_MASTER:
4246 case GIMPLE_OMP_ORDERED:
4247 case GIMPLE_OMP_CRITICAL:
4248 ctx = new_omp_context (stmt, ctx);
4249 scan_omp (gimple_omp_body_ptr (stmt), ctx);
4250 break;
4251
4252 case GIMPLE_OMP_MASKED:
4253 ctx = new_omp_context (stmt, ctx);
4254 scan_sharing_clauses (gimple_omp_masked_clauses (stmt), ctx);
4255 scan_omp (gimple_omp_body_ptr (stmt), ctx);
4256 break;
4257
4258 case GIMPLE_OMP_TASKGROUP:
4259 ctx = new_omp_context (stmt, ctx);
4260 scan_sharing_clauses (gimple_omp_taskgroup_clauses (stmt), ctx);
4261 scan_omp (gimple_omp_body_ptr (stmt), ctx);
4262 break;
4263
4264 case GIMPLE_OMP_TARGET:
4265 if (is_gimple_omp_offloaded (stmt))
4266 {
4267 taskreg_nesting_level++;
4268 scan_omp_target (as_a <gomp_target *> (stmt), ctx);
4269 taskreg_nesting_level--;
4270 }
4271 else
4272 scan_omp_target (as_a <gomp_target *> (stmt), ctx);
4273 break;
4274
4275 case GIMPLE_OMP_TEAMS:
4276 if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
4277 {
4278 taskreg_nesting_level++;
4279 scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
4280 taskreg_nesting_level--;
4281 }
4282 else
4283 scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
4284 break;
4285
4286 case GIMPLE_BIND:
4287 {
4288 tree var;
4289
4290 *handled_ops_p = false;
4291 if (ctx)
4292 for (var = gimple_bind_vars (as_a <gbind *> (stmt));
4293 var ;
4294 var = DECL_CHAIN (var))
4295 insert_decl_map (&ctx->cb, var, var);
4296 }
4297 break;
4298 default:
4299 *handled_ops_p = false;
4300 break;
4301 }
4302
4303 return NULL_TREE;
4304 }
4305
4306
4307 /* Scan all the statements starting at the current statement. CTX
4308 contains context information about the OMP directives and
4309 clauses found during the scan. */
4310
4311 static void
4312 scan_omp (gimple_seq *body_p, omp_context *ctx)
4313 {
4314 location_t saved_location;
4315 struct walk_stmt_info wi;
4316
4317 memset (&wi, 0, sizeof (wi));
4318 wi.info = ctx;
4319 wi.want_locations = true;
4320
4321 saved_location = input_location;
4322 walk_gimple_seq_mod (body_p, scan_omp_1_stmt, scan_omp_1_op, &wi);
4323 input_location = saved_location;
4324 }
4325
4326 /* Re-gimplification and code generation routines. */
4327
4328 /* Remove omp_member_access_dummy_var variables from gimple_bind_vars
4329 of BIND if in a method. */
4330
4331 static void
4332 maybe_remove_omp_member_access_dummy_vars (gbind *bind)
4333 {
4334 if (DECL_ARGUMENTS (current_function_decl)
4335 && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
4336 && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
4337 == POINTER_TYPE))
4338 {
4339 tree vars = gimple_bind_vars (bind);
4340 for (tree *pvar = &vars; *pvar; )
4341 if (omp_member_access_dummy_var (*pvar))
4342 *pvar = DECL_CHAIN (*pvar);
4343 else
4344 pvar = &DECL_CHAIN (*pvar);
4345 gimple_bind_set_vars (bind, vars);
4346 }
4347 }
4348
4349 /* Remove omp_member_access_dummy_var variables from BLOCK_VARS of
4350 block and its subblocks. */
4351
4352 static void
4353 remove_member_access_dummy_vars (tree block)
4354 {
4355 for (tree *pvar = &BLOCK_VARS (block); *pvar; )
4356 if (omp_member_access_dummy_var (*pvar))
4357 *pvar = DECL_CHAIN (*pvar);
4358 else
4359 pvar = &DECL_CHAIN (*pvar);
4360
4361 for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
4362 remove_member_access_dummy_vars (block);
4363 }
4364
4365 /* If a context was created for STMT when it was scanned, return it. */
4366
4367 static omp_context *
4368 maybe_lookup_ctx (gimple *stmt)
4369 {
4370 splay_tree_node n;
4371 n = splay_tree_lookup (all_contexts, (splay_tree_key) stmt);
4372 return n ? (omp_context *) n->value : NULL;
4373 }
4374
4375
4376 /* Find the mapping for DECL in CTX or the immediately enclosing
4377 context that has a mapping for DECL.
4378
4379 If CTX is a nested parallel directive, we may have to use the decl
4380 mappings created in CTX's parent context. Suppose that we have the
4381 following parallel nesting (variable UIDs showed for clarity):
4382
4383 iD.1562 = 0;
4384 #omp parallel shared(iD.1562) -> outer parallel
4385 iD.1562 = iD.1562 + 1;
4386
4387 #omp parallel shared (iD.1562) -> inner parallel
4388 iD.1562 = iD.1562 - 1;
4389
4390 Each parallel structure will create a distinct .omp_data_s structure
4391 for copying iD.1562 in/out of the directive:
4392
4393 outer parallel .omp_data_s.1.i -> iD.1562
4394 inner parallel .omp_data_s.2.i -> iD.1562
4395
4396 A shared variable mapping will produce a copy-out operation before
4397 the parallel directive and a copy-in operation after it. So, in
4398 this case we would have:
4399
4400 iD.1562 = 0;
4401 .omp_data_o.1.i = iD.1562;
4402 #omp parallel shared(iD.1562) -> outer parallel
4403 .omp_data_i.1 = &.omp_data_o.1
4404 .omp_data_i.1->i = .omp_data_i.1->i + 1;
4405
4406 .omp_data_o.2.i = iD.1562; -> **
4407 #omp parallel shared(iD.1562) -> inner parallel
4408 .omp_data_i.2 = &.omp_data_o.2
4409 .omp_data_i.2->i = .omp_data_i.2->i - 1;
4410
4411
4412 ** This is a problem. The symbol iD.1562 cannot be referenced
4413 inside the body of the outer parallel region. But since we are
4414 emitting this copy operation while expanding the inner parallel
4415 directive, we need to access the CTX structure of the outer
4416 parallel directive to get the correct mapping:
4417
4418 .omp_data_o.2.i = .omp_data_i.1->i
4419
4420 Since there may be other workshare or parallel directives enclosing
4421 the parallel directive, it may be necessary to walk up the context
4422 parent chain. This is not a problem in general because nested
4423 parallelism happens only rarely. */
4424
4425 static tree
4426 lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
4427 {
4428 tree t;
4429 omp_context *up;
4430
4431 for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
4432 t = maybe_lookup_decl (decl, up);
4433
4434 gcc_assert (!ctx->is_nested || t || is_global_var (decl));
4435
4436 return t ? t : decl;
4437 }
4438
4439
4440 /* Similar to lookup_decl_in_outer_ctx, but return DECL if not found
4441 in outer contexts. */
4442
4443 static tree
4444 maybe_lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
4445 {
4446 tree t = NULL;
4447 omp_context *up;
4448
4449 for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
4450 t = maybe_lookup_decl (decl, up);
4451
4452 return t ? t : decl;
4453 }
4454
4455
4456 /* Construct the initialization value for reduction operation OP. */
4457
4458 tree
4459 omp_reduction_init_op (location_t loc, enum tree_code op, tree type)
4460 {
4461 switch (op)
4462 {
4463 case PLUS_EXPR:
4464 case MINUS_EXPR:
4465 case BIT_IOR_EXPR:
4466 case BIT_XOR_EXPR:
4467 case TRUTH_OR_EXPR:
4468 case TRUTH_ORIF_EXPR:
4469 case TRUTH_XOR_EXPR:
4470 case NE_EXPR:
4471 return build_zero_cst (type);
4472
4473 case MULT_EXPR:
4474 case TRUTH_AND_EXPR:
4475 case TRUTH_ANDIF_EXPR:
4476 case EQ_EXPR:
4477 return fold_convert_loc (loc, type, integer_one_node);
4478
4479 case BIT_AND_EXPR:
4480 return fold_convert_loc (loc, type, integer_minus_one_node);
4481
4482 case MAX_EXPR:
4483 if (SCALAR_FLOAT_TYPE_P (type))
4484 {
4485 REAL_VALUE_TYPE min;
4486 if (HONOR_INFINITIES (type))
4487 real_arithmetic (&min, NEGATE_EXPR, &dconstinf, NULL);
4488 else
4489 real_maxval (&min, 1, TYPE_MODE (type));
4490 return build_real (type, min);
4491 }
4492 else if (POINTER_TYPE_P (type))
4493 {
4494 wide_int min
4495 = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4496 return wide_int_to_tree (type, min);
4497 }
4498 else
4499 {
4500 gcc_assert (INTEGRAL_TYPE_P (type));
4501 return TYPE_MIN_VALUE (type);
4502 }
4503
4504 case MIN_EXPR:
4505 if (SCALAR_FLOAT_TYPE_P (type))
4506 {
4507 REAL_VALUE_TYPE max;
4508 if (HONOR_INFINITIES (type))
4509 max = dconstinf;
4510 else
4511 real_maxval (&max, 0, TYPE_MODE (type));
4512 return build_real (type, max);
4513 }
4514 else if (POINTER_TYPE_P (type))
4515 {
4516 wide_int max
4517 = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4518 return wide_int_to_tree (type, max);
4519 }
4520 else
4521 {
4522 gcc_assert (INTEGRAL_TYPE_P (type));
4523 return TYPE_MAX_VALUE (type);
4524 }
4525
4526 default:
4527 gcc_unreachable ();
4528 }
4529 }
4530
4531 /* Construct the initialization value for reduction CLAUSE. */
4532
4533 tree
4534 omp_reduction_init (tree clause, tree type)
4535 {
4536 return omp_reduction_init_op (OMP_CLAUSE_LOCATION (clause),
4537 OMP_CLAUSE_REDUCTION_CODE (clause), type);
4538 }
4539
4540 /* Return alignment to be assumed for var in CLAUSE, which should be
4541 OMP_CLAUSE_ALIGNED. */
4542
4543 static tree
4544 omp_clause_aligned_alignment (tree clause)
4545 {
4546 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
4547 return OMP_CLAUSE_ALIGNED_ALIGNMENT (clause);
4548
4549 /* Otherwise return implementation defined alignment. */
4550 unsigned int al = 1;
4551 opt_scalar_mode mode_iter;
4552 auto_vector_modes modes;
4553 targetm.vectorize.autovectorize_vector_modes (&modes, true);
4554 static enum mode_class classes[]
4555 = { MODE_INT, MODE_VECTOR_INT, MODE_FLOAT, MODE_VECTOR_FLOAT };
4556 for (int i = 0; i < 4; i += 2)
4557 /* The for loop above dictates that we only walk through scalar classes. */
4558 FOR_EACH_MODE_IN_CLASS (mode_iter, classes[i])
4559 {
4560 scalar_mode mode = mode_iter.require ();
4561 machine_mode vmode = targetm.vectorize.preferred_simd_mode (mode);
4562 if (GET_MODE_CLASS (vmode) != classes[i + 1])
4563 continue;
4564 machine_mode alt_vmode;
4565 for (unsigned int j = 0; j < modes.length (); ++j)
4566 if (related_vector_mode (modes[j], mode).exists (&alt_vmode)
4567 && known_ge (GET_MODE_SIZE (alt_vmode), GET_MODE_SIZE (vmode)))
4568 vmode = alt_vmode;
4569
4570 tree type = lang_hooks.types.type_for_mode (mode, 1);
4571 if (type == NULL_TREE || TYPE_MODE (type) != mode)
4572 continue;
4573 type = build_vector_type_for_mode (type, vmode);
4574 if (TYPE_MODE (type) != vmode)
4575 continue;
4576 if (TYPE_ALIGN_UNIT (type) > al)
4577 al = TYPE_ALIGN_UNIT (type);
4578 }
4579 return build_int_cst (integer_type_node, al);
4580 }
4581
4582
4583 /* This structure is part of the interface between lower_rec_simd_input_clauses
4584 and lower_rec_input_clauses. */
4585
4586 class omplow_simd_context {
4587 public:
4588 omplow_simd_context () { memset (this, 0, sizeof (*this)); }
4589 tree idx;
4590 tree lane;
4591 tree lastlane;
4592 vec<tree, va_heap> simt_eargs;
4593 gimple_seq simt_dlist;
4594 poly_uint64 max_vf;
4595 bool is_simt;
4596 };
4597
4598 /* Helper function of lower_rec_input_clauses, used for #pragma omp simd
4599 privatization. */
4600
4601 static bool
4602 lower_rec_simd_input_clauses (tree new_var, omp_context *ctx,
4603 omplow_simd_context *sctx, tree &ivar,
4604 tree &lvar, tree *rvar = NULL,
4605 tree *rvar2 = NULL)
4606 {
4607 if (known_eq (sctx->max_vf, 0U))
4608 {
4609 sctx->max_vf = (sctx->is_simt ? omp_max_simt_vf ()
4610 : omp_max_vf (omp_maybe_offloaded_ctx (ctx)));
4611 if (maybe_gt (sctx->max_vf, 1U))
4612 {
4613 tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
4614 OMP_CLAUSE_SAFELEN);
4615 if (c)
4616 {
4617 poly_uint64 safe_len;
4618 if (!poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
4619 || maybe_lt (safe_len, 1U))
4620 sctx->max_vf = 1;
4621 else
4622 sctx->max_vf = lower_bound (sctx->max_vf, safe_len);
4623 }
4624 }
4625 if (sctx->is_simt && !known_eq (sctx->max_vf, 1U))
4626 {
4627 for (tree c = gimple_omp_for_clauses (ctx->stmt); c;
4628 c = OMP_CLAUSE_CHAIN (c))
4629 {
4630 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4631 continue;
4632
4633 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4634 {
4635 /* UDR reductions are not supported yet for SIMT, disable
4636 SIMT. */
4637 sctx->max_vf = 1;
4638 break;
4639 }
4640
4641 if (truth_value_p (OMP_CLAUSE_REDUCTION_CODE (c))
4642 && !INTEGRAL_TYPE_P (TREE_TYPE (new_var)))
4643 {
4644 /* Doing boolean operations on non-integral types is
4645 for conformance only, it's not worth supporting this
4646 for SIMT. */
4647 sctx->max_vf = 1;
4648 break;
4649 }
4650 }
4651 }
4652 if (maybe_gt (sctx->max_vf, 1U))
4653 {
4654 sctx->idx = create_tmp_var (unsigned_type_node);
4655 sctx->lane = create_tmp_var (unsigned_type_node);
4656 }
4657 }
4658 if (known_eq (sctx->max_vf, 1U))
4659 return false;
4660
4661 if (sctx->is_simt)
4662 {
4663 if (is_gimple_reg (new_var))
4664 {
4665 ivar = lvar = new_var;
4666 return true;
4667 }
4668 tree type = TREE_TYPE (new_var), ptype = build_pointer_type (type);
4669 ivar = lvar = create_tmp_var (type);
4670 TREE_ADDRESSABLE (ivar) = 1;
4671 DECL_ATTRIBUTES (ivar) = tree_cons (get_identifier ("omp simt private"),
4672 NULL, DECL_ATTRIBUTES (ivar));
4673 sctx->simt_eargs.safe_push (build1 (ADDR_EXPR, ptype, ivar));
4674 tree clobber = build_clobber (type);
4675 gimple *g = gimple_build_assign (ivar, clobber);
4676 gimple_seq_add_stmt (&sctx->simt_dlist, g);
4677 }
4678 else
4679 {
4680 tree atype = build_array_type_nelts (TREE_TYPE (new_var), sctx->max_vf);
4681 tree avar = create_tmp_var_raw (atype);
4682 if (TREE_ADDRESSABLE (new_var))
4683 TREE_ADDRESSABLE (avar) = 1;
4684 DECL_ATTRIBUTES (avar)
4685 = tree_cons (get_identifier ("omp simd array"), NULL,
4686 DECL_ATTRIBUTES (avar));
4687 gimple_add_tmp_var (avar);
4688 tree iavar = avar;
4689 if (rvar && !ctx->for_simd_scan_phase)
4690 {
4691 /* For inscan reductions, create another array temporary,
4692 which will hold the reduced value. */
4693 iavar = create_tmp_var_raw (atype);
4694 if (TREE_ADDRESSABLE (new_var))
4695 TREE_ADDRESSABLE (iavar) = 1;
4696 DECL_ATTRIBUTES (iavar)
4697 = tree_cons (get_identifier ("omp simd array"), NULL,
4698 tree_cons (get_identifier ("omp simd inscan"), NULL,
4699 DECL_ATTRIBUTES (iavar)));
4700 gimple_add_tmp_var (iavar);
4701 ctx->cb.decl_map->put (avar, iavar);
4702 if (sctx->lastlane == NULL_TREE)
4703 sctx->lastlane = create_tmp_var (unsigned_type_node);
4704 *rvar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar,
4705 sctx->lastlane, NULL_TREE, NULL_TREE);
4706 TREE_THIS_NOTRAP (*rvar) = 1;
4707
4708 if (ctx->scan_exclusive)
4709 {
4710 /* And for exclusive scan yet another one, which will
4711 hold the value during the scan phase. */
4712 tree savar = create_tmp_var_raw (atype);
4713 if (TREE_ADDRESSABLE (new_var))
4714 TREE_ADDRESSABLE (savar) = 1;
4715 DECL_ATTRIBUTES (savar)
4716 = tree_cons (get_identifier ("omp simd array"), NULL,
4717 tree_cons (get_identifier ("omp simd inscan "
4718 "exclusive"), NULL,
4719 DECL_ATTRIBUTES (savar)));
4720 gimple_add_tmp_var (savar);
4721 ctx->cb.decl_map->put (iavar, savar);
4722 *rvar2 = build4 (ARRAY_REF, TREE_TYPE (new_var), savar,
4723 sctx->idx, NULL_TREE, NULL_TREE);
4724 TREE_THIS_NOTRAP (*rvar2) = 1;
4725 }
4726 }
4727 ivar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar, sctx->idx,
4728 NULL_TREE, NULL_TREE);
4729 lvar = build4 (ARRAY_REF, TREE_TYPE (new_var), avar, sctx->lane,
4730 NULL_TREE, NULL_TREE);
4731 TREE_THIS_NOTRAP (ivar) = 1;
4732 TREE_THIS_NOTRAP (lvar) = 1;
4733 }
4734 if (DECL_P (new_var))
4735 {
4736 SET_DECL_VALUE_EXPR (new_var, lvar);
4737 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
4738 }
4739 return true;
4740 }
4741
4742 /* Helper function of lower_rec_input_clauses. For a reference
4743 in simd reduction, add an underlying variable it will reference. */
4744
4745 static void
4746 handle_simd_reference (location_t loc, tree new_vard, gimple_seq *ilist)
4747 {
4748 tree z = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_vard)));
4749 if (TREE_CONSTANT (z))
4750 {
4751 z = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_vard)),
4752 get_name (new_vard));
4753 gimple_add_tmp_var (z);
4754 TREE_ADDRESSABLE (z) = 1;
4755 z = build_fold_addr_expr_loc (loc, z);
4756 gimplify_assign (new_vard, z, ilist);
4757 }
4758 }
4759
4760 /* Helper function for lower_rec_input_clauses. Emit into ilist sequence
4761 code to emit (type) (tskred_temp[idx]). */
4762
4763 static tree
4764 task_reduction_read (gimple_seq *ilist, tree tskred_temp, tree type,
4765 unsigned idx)
4766 {
4767 unsigned HOST_WIDE_INT sz
4768 = tree_to_uhwi (TYPE_SIZE_UNIT (pointer_sized_int_node));
4769 tree r = build2 (MEM_REF, pointer_sized_int_node,
4770 tskred_temp, build_int_cst (TREE_TYPE (tskred_temp),
4771 idx * sz));
4772 tree v = create_tmp_var (pointer_sized_int_node);
4773 gimple *g = gimple_build_assign (v, r);
4774 gimple_seq_add_stmt (ilist, g);
4775 if (!useless_type_conversion_p (type, pointer_sized_int_node))
4776 {
4777 v = create_tmp_var (type);
4778 g = gimple_build_assign (v, NOP_EXPR, gimple_assign_lhs (g));
4779 gimple_seq_add_stmt (ilist, g);
4780 }
4781 return v;
4782 }
4783
4784 /* Lower early initialization of privatized variable NEW_VAR
4785 if it needs an allocator (has allocate clause). */
4786
4787 static bool
4788 lower_private_allocate (tree var, tree new_var, tree &allocator,
4789 tree &allocate_ptr, gimple_seq *ilist,
4790 omp_context *ctx, bool is_ref, tree size)
4791 {
4792 if (allocator)
4793 return false;
4794 gcc_assert (allocate_ptr == NULL_TREE);
4795 if (ctx->allocate_map
4796 && (DECL_P (new_var) || (TYPE_P (new_var) && size)))
4797 if (tree *allocatorp = ctx->allocate_map->get (var))
4798 allocator = *allocatorp;
4799 if (allocator == NULL_TREE)
4800 return false;
4801 if (!is_ref && omp_privatize_by_reference (var))
4802 {
4803 allocator = NULL_TREE;
4804 return false;
4805 }
4806
4807 unsigned HOST_WIDE_INT ialign = 0;
4808 if (TREE_CODE (allocator) == TREE_LIST)
4809 {
4810 ialign = tree_to_uhwi (TREE_VALUE (allocator));
4811 allocator = TREE_PURPOSE (allocator);
4812 }
4813 if (TREE_CODE (allocator) != INTEGER_CST)
4814 allocator = build_outer_var_ref (allocator, ctx, OMP_CLAUSE_ALLOCATE);
4815 allocator = fold_convert (pointer_sized_int_node, allocator);
4816 if (TREE_CODE (allocator) != INTEGER_CST)
4817 {
4818 tree var = create_tmp_var (TREE_TYPE (allocator));
4819 gimplify_assign (var, allocator, ilist);
4820 allocator = var;
4821 }
4822
4823 tree ptr_type, align, sz = size;
4824 if (TYPE_P (new_var))
4825 {
4826 ptr_type = build_pointer_type (new_var);
4827 ialign = MAX (ialign, TYPE_ALIGN_UNIT (new_var));
4828 }
4829 else if (is_ref)
4830 {
4831 ptr_type = build_pointer_type (TREE_TYPE (TREE_TYPE (new_var)));
4832 ialign = MAX (ialign, TYPE_ALIGN_UNIT (TREE_TYPE (ptr_type)));
4833 }
4834 else
4835 {
4836 ptr_type = build_pointer_type (TREE_TYPE (new_var));
4837 ialign = MAX (ialign, DECL_ALIGN_UNIT (new_var));
4838 if (sz == NULL_TREE)
4839 sz = fold_convert (size_type_node, DECL_SIZE_UNIT (new_var));
4840 }
4841 align = build_int_cst (size_type_node, ialign);
4842 if (TREE_CODE (sz) != INTEGER_CST)
4843 {
4844 tree szvar = create_tmp_var (size_type_node);
4845 gimplify_assign (szvar, sz, ilist);
4846 sz = szvar;
4847 }
4848 allocate_ptr = create_tmp_var (ptr_type);
4849 tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
4850 gimple *g = gimple_build_call (a, 3, align, sz, allocator);
4851 gimple_call_set_lhs (g, allocate_ptr);
4852 gimple_seq_add_stmt (ilist, g);
4853 if (!is_ref)
4854 {
4855 tree x = build_simple_mem_ref (allocate_ptr);
4856 TREE_THIS_NOTRAP (x) = 1;
4857 SET_DECL_VALUE_EXPR (new_var, x);
4858 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
4859 }
4860 return true;
4861 }
4862
4863 /* Generate code to implement the input clauses, FIRSTPRIVATE and COPYIN,
4864 from the receiver (aka child) side and initializers for REFERENCE_TYPE
4865 private variables. Initialization statements go in ILIST, while calls
4866 to destructors go in DLIST. */
4867
4868 static void
4869 lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist,
4870 omp_context *ctx, struct omp_for_data *fd)
4871 {
4872 tree c, copyin_seq, x, ptr;
4873 bool copyin_by_ref = false;
4874 bool lastprivate_firstprivate = false;
4875 bool reduction_omp_orig_ref = false;
4876 int pass;
4877 bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
4878 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
4879 omplow_simd_context sctx = omplow_simd_context ();
4880 tree simt_lane = NULL_TREE, simtrec = NULL_TREE;
4881 tree ivar = NULL_TREE, lvar = NULL_TREE, uid = NULL_TREE;
4882 gimple_seq llist[4] = { };
4883 tree nonconst_simd_if = NULL_TREE;
4884
4885 copyin_seq = NULL;
4886 sctx.is_simt = is_simd && omp_find_clause (clauses, OMP_CLAUSE__SIMT_);
4887
4888 /* Set max_vf=1 (which will later enforce safelen=1) in simd loops
4889 with data sharing clauses referencing variable sized vars. That
4890 is unnecessarily hard to support and very unlikely to result in
4891 vectorized code anyway. */
4892 if (is_simd)
4893 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
4894 switch (OMP_CLAUSE_CODE (c))
4895 {
4896 case OMP_CLAUSE_LINEAR:
4897 if (OMP_CLAUSE_LINEAR_ARRAY (c))
4898 sctx.max_vf = 1;
4899 /* FALLTHRU */
4900 case OMP_CLAUSE_PRIVATE:
4901 case OMP_CLAUSE_FIRSTPRIVATE:
4902 case OMP_CLAUSE_LASTPRIVATE:
4903 if (is_variable_sized (OMP_CLAUSE_DECL (c)))
4904 sctx.max_vf = 1;
4905 else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
4906 {
4907 tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4908 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
4909 sctx.max_vf = 1;
4910 }
4911 break;
4912 case OMP_CLAUSE_REDUCTION:
4913 case OMP_CLAUSE_IN_REDUCTION:
4914 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
4915 || is_variable_sized (OMP_CLAUSE_DECL (c)))
4916 sctx.max_vf = 1;
4917 else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
4918 {
4919 tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4920 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
4921 sctx.max_vf = 1;
4922 }
4923 break;
4924 case OMP_CLAUSE_IF:
4925 if (integer_zerop (OMP_CLAUSE_IF_EXPR (c)))
4926 sctx.max_vf = 1;
4927 else if (TREE_CODE (OMP_CLAUSE_IF_EXPR (c)) != INTEGER_CST)
4928 nonconst_simd_if = OMP_CLAUSE_IF_EXPR (c);
4929 break;
4930 case OMP_CLAUSE_SIMDLEN:
4931 if (integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (c)))
4932 sctx.max_vf = 1;
4933 break;
4934 case OMP_CLAUSE__CONDTEMP_:
4935 /* FIXME: lastprivate(conditional:) not handled for SIMT yet. */
4936 if (sctx.is_simt)
4937 sctx.max_vf = 1;
4938 break;
4939 default:
4940 continue;
4941 }
4942
4943 /* Add a placeholder for simduid. */
4944 if (sctx.is_simt && maybe_ne (sctx.max_vf, 1U))
4945 sctx.simt_eargs.safe_push (NULL_TREE);
4946
4947 unsigned task_reduction_cnt = 0;
4948 unsigned task_reduction_cntorig = 0;
4949 unsigned task_reduction_cnt_full = 0;
4950 unsigned task_reduction_cntorig_full = 0;
4951 unsigned task_reduction_other_cnt = 0;
4952 tree tskred_atype = NULL_TREE, tskred_avar = NULL_TREE;
4953 tree tskred_base = NULL_TREE, tskred_temp = NULL_TREE;
4954 /* Do all the fixed sized types in the first pass, and the variable sized
4955 types in the second pass. This makes sure that the scalar arguments to
4956 the variable sized types are processed before we use them in the
4957 variable sized operations. For task reductions we use 4 passes, in the
4958 first two we ignore them, in the third one gather arguments for
4959 GOMP_task_reduction_remap call and in the last pass actually handle
4960 the task reductions. */
4961 for (pass = 0; pass < ((task_reduction_cnt || task_reduction_other_cnt)
4962 ? 4 : 2); ++pass)
4963 {
4964 if (pass == 2 && task_reduction_cnt)
4965 {
4966 tskred_atype
4967 = build_array_type_nelts (ptr_type_node, task_reduction_cnt
4968 + task_reduction_cntorig);
4969 tskred_avar = create_tmp_var_raw (tskred_atype);
4970 gimple_add_tmp_var (tskred_avar);
4971 TREE_ADDRESSABLE (tskred_avar) = 1;
4972 task_reduction_cnt_full = task_reduction_cnt;
4973 task_reduction_cntorig_full = task_reduction_cntorig;
4974 }
4975 else if (pass == 3 && task_reduction_cnt)
4976 {
4977 x = builtin_decl_explicit (BUILT_IN_GOMP_TASK_REDUCTION_REMAP);
4978 gimple *g
4979 = gimple_build_call (x, 3, size_int (task_reduction_cnt),
4980 size_int (task_reduction_cntorig),
4981 build_fold_addr_expr (tskred_avar));
4982 gimple_seq_add_stmt (ilist, g);
4983 }
4984 if (pass == 3 && task_reduction_other_cnt)
4985 {
4986 /* For reduction clauses, build
4987 tskred_base = (void *) tskred_temp[2]
4988 + omp_get_thread_num () * tskred_temp[1]
4989 or if tskred_temp[1] is known to be constant, that constant
4990 directly. This is the start of the private reduction copy block
4991 for the current thread. */
4992 tree v = create_tmp_var (integer_type_node);
4993 x = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
4994 gimple *g = gimple_build_call (x, 0);
4995 gimple_call_set_lhs (g, v);
4996 gimple_seq_add_stmt (ilist, g);
4997 c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
4998 tskred_temp = OMP_CLAUSE_DECL (c);
4999 if (is_taskreg_ctx (ctx))
5000 tskred_temp = lookup_decl (tskred_temp, ctx);
5001 tree v2 = create_tmp_var (sizetype);
5002 g = gimple_build_assign (v2, NOP_EXPR, v);
5003 gimple_seq_add_stmt (ilist, g);
5004 if (ctx->task_reductions[0])
5005 v = fold_convert (sizetype, ctx->task_reductions[0]);
5006 else
5007 v = task_reduction_read (ilist, tskred_temp, sizetype, 1);
5008 tree v3 = create_tmp_var (sizetype);
5009 g = gimple_build_assign (v3, MULT_EXPR, v2, v);
5010 gimple_seq_add_stmt (ilist, g);
5011 v = task_reduction_read (ilist, tskred_temp, ptr_type_node, 2);
5012 tskred_base = create_tmp_var (ptr_type_node);
5013 g = gimple_build_assign (tskred_base, POINTER_PLUS_EXPR, v, v3);
5014 gimple_seq_add_stmt (ilist, g);
5015 }
5016 task_reduction_cnt = 0;
5017 task_reduction_cntorig = 0;
5018 task_reduction_other_cnt = 0;
5019 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
5020 {
5021 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5022 tree var, new_var;
5023 bool by_ref;
5024 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
5025 bool task_reduction_p = false;
5026 bool task_reduction_needs_orig_p = false;
5027 tree cond = NULL_TREE;
5028 tree allocator, allocate_ptr;
5029
5030 switch (c_kind)
5031 {
5032 case OMP_CLAUSE_PRIVATE:
5033 if (OMP_CLAUSE_PRIVATE_DEBUG (c))
5034 continue;
5035 break;
5036 case OMP_CLAUSE_SHARED:
5037 /* Ignore shared directives in teams construct inside
5038 of target construct. */
5039 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
5040 && !is_host_teams_ctx (ctx))
5041 continue;
5042 if (maybe_lookup_decl (OMP_CLAUSE_DECL (c), ctx) == NULL)
5043 {
5044 gcc_assert (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c)
5045 || is_global_var (OMP_CLAUSE_DECL (c)));
5046 continue;
5047 }
5048 case OMP_CLAUSE_FIRSTPRIVATE:
5049 case OMP_CLAUSE_COPYIN:
5050 break;
5051 case OMP_CLAUSE_LINEAR:
5052 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
5053 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
5054 lastprivate_firstprivate = true;
5055 break;
5056 case OMP_CLAUSE_REDUCTION:
5057 case OMP_CLAUSE_IN_REDUCTION:
5058 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5059 || is_task_ctx (ctx)
5060 || OMP_CLAUSE_REDUCTION_TASK (c))
5061 {
5062 task_reduction_p = true;
5063 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5064 {
5065 task_reduction_other_cnt++;
5066 if (pass == 2)
5067 continue;
5068 }
5069 else
5070 task_reduction_cnt++;
5071 if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5072 {
5073 var = OMP_CLAUSE_DECL (c);
5074 /* If var is a global variable that isn't privatized
5075 in outer contexts, we don't need to look up the
5076 original address, it is always the address of the
5077 global variable itself. */
5078 if (!DECL_P (var)
5079 || omp_privatize_by_reference (var)
5080 || !is_global_var
5081 (maybe_lookup_decl_in_outer_ctx (var, ctx)))
5082 {
5083 task_reduction_needs_orig_p = true;
5084 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5085 task_reduction_cntorig++;
5086 }
5087 }
5088 }
5089 else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5090 reduction_omp_orig_ref = true;
5091 break;
5092 case OMP_CLAUSE__REDUCTEMP_:
5093 if (!is_taskreg_ctx (ctx))
5094 continue;
5095 /* FALLTHRU */
5096 case OMP_CLAUSE__LOOPTEMP_:
5097 /* Handle _looptemp_/_reductemp_ clauses only on
5098 parallel/task. */
5099 if (fd)
5100 continue;
5101 break;
5102 case OMP_CLAUSE_LASTPRIVATE:
5103 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
5104 {
5105 lastprivate_firstprivate = true;
5106 if (pass != 0 || is_taskloop_ctx (ctx))
5107 continue;
5108 }
5109 /* Even without corresponding firstprivate, if
5110 decl is Fortran allocatable, it needs outer var
5111 reference. */
5112 else if (pass == 0
5113 && lang_hooks.decls.omp_private_outer_ref
5114 (OMP_CLAUSE_DECL (c)))
5115 lastprivate_firstprivate = true;
5116 break;
5117 case OMP_CLAUSE_ALIGNED:
5118 if (pass != 1)
5119 continue;
5120 var = OMP_CLAUSE_DECL (c);
5121 if (TREE_CODE (TREE_TYPE (var)) == POINTER_TYPE
5122 && !is_global_var (var))
5123 {
5124 new_var = maybe_lookup_decl (var, ctx);
5125 if (new_var == NULL_TREE)
5126 new_var = maybe_lookup_decl_in_outer_ctx (var, ctx);
5127 x = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
5128 tree alarg = omp_clause_aligned_alignment (c);
5129 alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
5130 x = build_call_expr_loc (clause_loc, x, 2, new_var, alarg);
5131 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5132 x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
5133 gimplify_and_add (x, ilist);
5134 }
5135 else if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
5136 && is_global_var (var))
5137 {
5138 tree ptype = build_pointer_type (TREE_TYPE (var)), t, t2;
5139 new_var = lookup_decl (var, ctx);
5140 t = maybe_lookup_decl_in_outer_ctx (var, ctx);
5141 t = build_fold_addr_expr_loc (clause_loc, t);
5142 t2 = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
5143 tree alarg = omp_clause_aligned_alignment (c);
5144 alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
5145 t = build_call_expr_loc (clause_loc, t2, 2, t, alarg);
5146 t = fold_convert_loc (clause_loc, ptype, t);
5147 x = create_tmp_var (ptype);
5148 t = build2 (MODIFY_EXPR, ptype, x, t);
5149 gimplify_and_add (t, ilist);
5150 t = build_simple_mem_ref_loc (clause_loc, x);
5151 SET_DECL_VALUE_EXPR (new_var, t);
5152 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5153 }
5154 continue;
5155 case OMP_CLAUSE__CONDTEMP_:
5156 if (is_parallel_ctx (ctx)
5157 || (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c)))
5158 break;
5159 continue;
5160 default:
5161 continue;
5162 }
5163
5164 if (task_reduction_p != (pass >= 2))
5165 continue;
5166
5167 allocator = NULL_TREE;
5168 allocate_ptr = NULL_TREE;
5169 new_var = var = OMP_CLAUSE_DECL (c);
5170 if ((c_kind == OMP_CLAUSE_REDUCTION
5171 || c_kind == OMP_CLAUSE_IN_REDUCTION)
5172 && TREE_CODE (var) == MEM_REF)
5173 {
5174 var = TREE_OPERAND (var, 0);
5175 if (TREE_CODE (var) == POINTER_PLUS_EXPR)
5176 var = TREE_OPERAND (var, 0);
5177 if (TREE_CODE (var) == INDIRECT_REF
5178 || TREE_CODE (var) == ADDR_EXPR)
5179 var = TREE_OPERAND (var, 0);
5180 if (is_variable_sized (var))
5181 {
5182 gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
5183 var = DECL_VALUE_EXPR (var);
5184 gcc_assert (TREE_CODE (var) == INDIRECT_REF);
5185 var = TREE_OPERAND (var, 0);
5186 gcc_assert (DECL_P (var));
5187 }
5188 new_var = var;
5189 }
5190 if (c_kind == OMP_CLAUSE_IN_REDUCTION && is_omp_target (ctx->stmt))
5191 {
5192 splay_tree_key key = (splay_tree_key) &DECL_CONTEXT (var);
5193 new_var = (tree) splay_tree_lookup (ctx->field_map, key)->value;
5194 }
5195 else if (c_kind != OMP_CLAUSE_COPYIN)
5196 new_var = lookup_decl (var, ctx);
5197
5198 if (c_kind == OMP_CLAUSE_SHARED || c_kind == OMP_CLAUSE_COPYIN)
5199 {
5200 if (pass != 0)
5201 continue;
5202 }
5203 /* C/C++ array section reductions. */
5204 else if ((c_kind == OMP_CLAUSE_REDUCTION
5205 || c_kind == OMP_CLAUSE_IN_REDUCTION)
5206 && var != OMP_CLAUSE_DECL (c))
5207 {
5208 if (pass == 0)
5209 continue;
5210
5211 tree bias = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5212 tree orig_var = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
5213
5214 if (TREE_CODE (orig_var) == POINTER_PLUS_EXPR)
5215 {
5216 tree b = TREE_OPERAND (orig_var, 1);
5217 if (is_omp_target (ctx->stmt))
5218 b = NULL_TREE;
5219 else
5220 b = maybe_lookup_decl (b, ctx);
5221 if (b == NULL)
5222 {
5223 b = TREE_OPERAND (orig_var, 1);
5224 b = maybe_lookup_decl_in_outer_ctx (b, ctx);
5225 }
5226 if (integer_zerop (bias))
5227 bias = b;
5228 else
5229 {
5230 bias = fold_convert_loc (clause_loc,
5231 TREE_TYPE (b), bias);
5232 bias = fold_build2_loc (clause_loc, PLUS_EXPR,
5233 TREE_TYPE (b), b, bias);
5234 }
5235 orig_var = TREE_OPERAND (orig_var, 0);
5236 }
5237 if (pass == 2)
5238 {
5239 tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
5240 if (is_global_var (out)
5241 && TREE_CODE (TREE_TYPE (out)) != POINTER_TYPE
5242 && (TREE_CODE (TREE_TYPE (out)) != REFERENCE_TYPE
5243 || (TREE_CODE (TREE_TYPE (TREE_TYPE (out)))
5244 != POINTER_TYPE)))
5245 x = var;
5246 else if (is_omp_target (ctx->stmt))
5247 x = out;
5248 else
5249 {
5250 bool by_ref = use_pointer_for_field (var, NULL);
5251 x = build_receiver_ref (var, by_ref, ctx);
5252 if (TREE_CODE (TREE_TYPE (var)) == REFERENCE_TYPE
5253 && (TREE_CODE (TREE_TYPE (TREE_TYPE (var)))
5254 == POINTER_TYPE))
5255 x = build_fold_addr_expr (x);
5256 }
5257 if (TREE_CODE (orig_var) == INDIRECT_REF)
5258 x = build_simple_mem_ref (x);
5259 else if (TREE_CODE (orig_var) == ADDR_EXPR)
5260 {
5261 if (var == TREE_OPERAND (orig_var, 0))
5262 x = build_fold_addr_expr (x);
5263 }
5264 bias = fold_convert (sizetype, bias);
5265 x = fold_convert (ptr_type_node, x);
5266 x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
5267 TREE_TYPE (x), x, bias);
5268 unsigned cnt = task_reduction_cnt - 1;
5269 if (!task_reduction_needs_orig_p)
5270 cnt += (task_reduction_cntorig_full
5271 - task_reduction_cntorig);
5272 else
5273 cnt = task_reduction_cntorig - 1;
5274 tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5275 size_int (cnt), NULL_TREE, NULL_TREE);
5276 gimplify_assign (r, x, ilist);
5277 continue;
5278 }
5279
5280 if (TREE_CODE (orig_var) == INDIRECT_REF
5281 || TREE_CODE (orig_var) == ADDR_EXPR)
5282 orig_var = TREE_OPERAND (orig_var, 0);
5283 tree d = OMP_CLAUSE_DECL (c);
5284 tree type = TREE_TYPE (d);
5285 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5286 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
5287 tree sz = v;
5288 const char *name = get_name (orig_var);
5289 if (pass != 3 && !TREE_CONSTANT (v))
5290 {
5291 tree t;
5292 if (is_omp_target (ctx->stmt))
5293 t = NULL_TREE;
5294 else
5295 t = maybe_lookup_decl (v, ctx);
5296 if (t)
5297 v = t;
5298 else
5299 v = maybe_lookup_decl_in_outer_ctx (v, ctx);
5300 gimplify_expr (&v, ilist, NULL, is_gimple_val, fb_rvalue);
5301 t = fold_build2_loc (clause_loc, PLUS_EXPR,
5302 TREE_TYPE (v), v,
5303 build_int_cst (TREE_TYPE (v), 1));
5304 sz = fold_build2_loc (clause_loc, MULT_EXPR,
5305 TREE_TYPE (v), t,
5306 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5307 }
5308 if (pass == 3)
5309 {
5310 tree xv = create_tmp_var (ptr_type_node);
5311 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5312 {
5313 unsigned cnt = task_reduction_cnt - 1;
5314 if (!task_reduction_needs_orig_p)
5315 cnt += (task_reduction_cntorig_full
5316 - task_reduction_cntorig);
5317 else
5318 cnt = task_reduction_cntorig - 1;
5319 x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5320 size_int (cnt), NULL_TREE, NULL_TREE);
5321
5322 gimple *g = gimple_build_assign (xv, x);
5323 gimple_seq_add_stmt (ilist, g);
5324 }
5325 else
5326 {
5327 unsigned int idx = *ctx->task_reduction_map->get (c);
5328 tree off;
5329 if (ctx->task_reductions[1 + idx])
5330 off = fold_convert (sizetype,
5331 ctx->task_reductions[1 + idx]);
5332 else
5333 off = task_reduction_read (ilist, tskred_temp, sizetype,
5334 7 + 3 * idx + 1);
5335 gimple *g = gimple_build_assign (xv, POINTER_PLUS_EXPR,
5336 tskred_base, off);
5337 gimple_seq_add_stmt (ilist, g);
5338 }
5339 x = fold_convert (build_pointer_type (boolean_type_node),
5340 xv);
5341 if (TREE_CONSTANT (v))
5342 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x,
5343 TYPE_SIZE_UNIT (type));
5344 else
5345 {
5346 tree t;
5347 if (is_omp_target (ctx->stmt))
5348 t = NULL_TREE;
5349 else
5350 t = maybe_lookup_decl (v, ctx);
5351 if (t)
5352 v = t;
5353 else
5354 v = maybe_lookup_decl_in_outer_ctx (v, ctx);
5355 gimplify_expr (&v, ilist, NULL, is_gimple_val,
5356 fb_rvalue);
5357 t = fold_build2_loc (clause_loc, PLUS_EXPR,
5358 TREE_TYPE (v), v,
5359 build_int_cst (TREE_TYPE (v), 1));
5360 t = fold_build2_loc (clause_loc, MULT_EXPR,
5361 TREE_TYPE (v), t,
5362 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5363 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x, t);
5364 }
5365 cond = create_tmp_var (TREE_TYPE (x));
5366 gimplify_assign (cond, x, ilist);
5367 x = xv;
5368 }
5369 else if (lower_private_allocate (var, type, allocator,
5370 allocate_ptr, ilist, ctx,
5371 true,
5372 TREE_CONSTANT (v)
5373 ? TYPE_SIZE_UNIT (type)
5374 : sz))
5375 x = allocate_ptr;
5376 else if (TREE_CONSTANT (v))
5377 {
5378 x = create_tmp_var_raw (type, name);
5379 gimple_add_tmp_var (x);
5380 TREE_ADDRESSABLE (x) = 1;
5381 x = build_fold_addr_expr_loc (clause_loc, x);
5382 }
5383 else
5384 {
5385 tree atmp
5386 = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5387 tree al = size_int (TYPE_ALIGN (TREE_TYPE (type)));
5388 x = build_call_expr_loc (clause_loc, atmp, 2, sz, al);
5389 }
5390
5391 tree ptype = build_pointer_type (TREE_TYPE (type));
5392 x = fold_convert_loc (clause_loc, ptype, x);
5393 tree y = create_tmp_var (ptype, name);
5394 gimplify_assign (y, x, ilist);
5395 x = y;
5396 tree yb = y;
5397
5398 if (!integer_zerop (bias))
5399 {
5400 bias = fold_convert_loc (clause_loc, pointer_sized_int_node,
5401 bias);
5402 yb = fold_convert_loc (clause_loc, pointer_sized_int_node,
5403 x);
5404 yb = fold_build2_loc (clause_loc, MINUS_EXPR,
5405 pointer_sized_int_node, yb, bias);
5406 x = fold_convert_loc (clause_loc, TREE_TYPE (x), yb);
5407 yb = create_tmp_var (ptype, name);
5408 gimplify_assign (yb, x, ilist);
5409 x = yb;
5410 }
5411
5412 d = TREE_OPERAND (d, 0);
5413 if (TREE_CODE (d) == POINTER_PLUS_EXPR)
5414 d = TREE_OPERAND (d, 0);
5415 if (TREE_CODE (d) == ADDR_EXPR)
5416 {
5417 if (orig_var != var)
5418 {
5419 gcc_assert (is_variable_sized (orig_var));
5420 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var),
5421 x);
5422 gimplify_assign (new_var, x, ilist);
5423 tree new_orig_var = lookup_decl (orig_var, ctx);
5424 tree t = build_fold_indirect_ref (new_var);
5425 DECL_IGNORED_P (new_var) = 0;
5426 TREE_THIS_NOTRAP (t) = 1;
5427 SET_DECL_VALUE_EXPR (new_orig_var, t);
5428 DECL_HAS_VALUE_EXPR_P (new_orig_var) = 1;
5429 }
5430 else
5431 {
5432 x = build2 (MEM_REF, TREE_TYPE (new_var), x,
5433 build_int_cst (ptype, 0));
5434 SET_DECL_VALUE_EXPR (new_var, x);
5435 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5436 }
5437 }
5438 else
5439 {
5440 gcc_assert (orig_var == var);
5441 if (TREE_CODE (d) == INDIRECT_REF)
5442 {
5443 x = create_tmp_var (ptype, name);
5444 TREE_ADDRESSABLE (x) = 1;
5445 gimplify_assign (x, yb, ilist);
5446 x = build_fold_addr_expr_loc (clause_loc, x);
5447 }
5448 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5449 gimplify_assign (new_var, x, ilist);
5450 }
5451 /* GOMP_taskgroup_reduction_register memsets the whole
5452 array to zero. If the initializer is zero, we don't
5453 need to initialize it again, just mark it as ever
5454 used unconditionally, i.e. cond = true. */
5455 if (cond
5456 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
5457 && initializer_zerop (omp_reduction_init (c,
5458 TREE_TYPE (type))))
5459 {
5460 gimple *g = gimple_build_assign (build_simple_mem_ref (cond),
5461 boolean_true_node);
5462 gimple_seq_add_stmt (ilist, g);
5463 continue;
5464 }
5465 tree end = create_artificial_label (UNKNOWN_LOCATION);
5466 if (cond)
5467 {
5468 gimple *g;
5469 if (!is_parallel_ctx (ctx))
5470 {
5471 tree condv = create_tmp_var (boolean_type_node);
5472 g = gimple_build_assign (condv,
5473 build_simple_mem_ref (cond));
5474 gimple_seq_add_stmt (ilist, g);
5475 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
5476 g = gimple_build_cond (NE_EXPR, condv,
5477 boolean_false_node, end, lab1);
5478 gimple_seq_add_stmt (ilist, g);
5479 gimple_seq_add_stmt (ilist, gimple_build_label (lab1));
5480 }
5481 g = gimple_build_assign (build_simple_mem_ref (cond),
5482 boolean_true_node);
5483 gimple_seq_add_stmt (ilist, g);
5484 }
5485
5486 tree y1 = create_tmp_var (ptype);
5487 gimplify_assign (y1, y, ilist);
5488 tree i2 = NULL_TREE, y2 = NULL_TREE;
5489 tree body2 = NULL_TREE, end2 = NULL_TREE;
5490 tree y3 = NULL_TREE, y4 = NULL_TREE;
5491 if (task_reduction_needs_orig_p)
5492 {
5493 y3 = create_tmp_var (ptype);
5494 tree ref;
5495 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5496 ref = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5497 size_int (task_reduction_cnt_full
5498 + task_reduction_cntorig - 1),
5499 NULL_TREE, NULL_TREE);
5500 else
5501 {
5502 unsigned int idx = *ctx->task_reduction_map->get (c);
5503 ref = task_reduction_read (ilist, tskred_temp, ptype,
5504 7 + 3 * idx);
5505 }
5506 gimplify_assign (y3, ref, ilist);
5507 }
5508 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) || is_simd)
5509 {
5510 if (pass != 3)
5511 {
5512 y2 = create_tmp_var (ptype);
5513 gimplify_assign (y2, y, ilist);
5514 }
5515 if (is_simd || OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5516 {
5517 tree ref = build_outer_var_ref (var, ctx);
5518 /* For ref build_outer_var_ref already performs this. */
5519 if (TREE_CODE (d) == INDIRECT_REF)
5520 gcc_assert (omp_privatize_by_reference (var));
5521 else if (TREE_CODE (d) == ADDR_EXPR)
5522 ref = build_fold_addr_expr (ref);
5523 else if (omp_privatize_by_reference (var))
5524 ref = build_fold_addr_expr (ref);
5525 ref = fold_convert_loc (clause_loc, ptype, ref);
5526 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
5527 && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5528 {
5529 y3 = create_tmp_var (ptype);
5530 gimplify_assign (y3, unshare_expr (ref), ilist);
5531 }
5532 if (is_simd)
5533 {
5534 y4 = create_tmp_var (ptype);
5535 gimplify_assign (y4, ref, dlist);
5536 }
5537 }
5538 }
5539 tree i = create_tmp_var (TREE_TYPE (v));
5540 gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), ilist);
5541 tree body = create_artificial_label (UNKNOWN_LOCATION);
5542 gimple_seq_add_stmt (ilist, gimple_build_label (body));
5543 if (y2)
5544 {
5545 i2 = create_tmp_var (TREE_TYPE (v));
5546 gimplify_assign (i2, build_int_cst (TREE_TYPE (v), 0), dlist);
5547 body2 = create_artificial_label (UNKNOWN_LOCATION);
5548 end2 = create_artificial_label (UNKNOWN_LOCATION);
5549 gimple_seq_add_stmt (dlist, gimple_build_label (body2));
5550 }
5551 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5552 {
5553 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5554 tree decl_placeholder
5555 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
5556 SET_DECL_VALUE_EXPR (decl_placeholder,
5557 build_simple_mem_ref (y1));
5558 DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
5559 SET_DECL_VALUE_EXPR (placeholder,
5560 y3 ? build_simple_mem_ref (y3)
5561 : error_mark_node);
5562 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
5563 x = lang_hooks.decls.omp_clause_default_ctor
5564 (c, build_simple_mem_ref (y1),
5565 y3 ? build_simple_mem_ref (y3) : NULL_TREE);
5566 if (x)
5567 gimplify_and_add (x, ilist);
5568 if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
5569 {
5570 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
5571 lower_omp (&tseq, ctx);
5572 gimple_seq_add_seq (ilist, tseq);
5573 }
5574 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
5575 if (is_simd)
5576 {
5577 SET_DECL_VALUE_EXPR (decl_placeholder,
5578 build_simple_mem_ref (y2));
5579 SET_DECL_VALUE_EXPR (placeholder,
5580 build_simple_mem_ref (y4));
5581 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
5582 lower_omp (&tseq, ctx);
5583 gimple_seq_add_seq (dlist, tseq);
5584 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
5585 }
5586 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
5587 DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 0;
5588 if (y2)
5589 {
5590 x = lang_hooks.decls.omp_clause_dtor
5591 (c, build_simple_mem_ref (y2));
5592 if (x)
5593 gimplify_and_add (x, dlist);
5594 }
5595 }
5596 else
5597 {
5598 x = omp_reduction_init (c, TREE_TYPE (type));
5599 enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
5600
5601 /* reduction(-:var) sums up the partial results, so it
5602 acts identically to reduction(+:var). */
5603 if (code == MINUS_EXPR)
5604 code = PLUS_EXPR;
5605
5606 gimplify_assign (build_simple_mem_ref (y1), x, ilist);
5607 if (is_simd)
5608 {
5609 x = build2 (code, TREE_TYPE (type),
5610 build_simple_mem_ref (y4),
5611 build_simple_mem_ref (y2));
5612 gimplify_assign (build_simple_mem_ref (y4), x, dlist);
5613 }
5614 }
5615 gimple *g
5616 = gimple_build_assign (y1, POINTER_PLUS_EXPR, y1,
5617 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5618 gimple_seq_add_stmt (ilist, g);
5619 if (y3)
5620 {
5621 g = gimple_build_assign (y3, POINTER_PLUS_EXPR, y3,
5622 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5623 gimple_seq_add_stmt (ilist, g);
5624 }
5625 g = gimple_build_assign (i, PLUS_EXPR, i,
5626 build_int_cst (TREE_TYPE (i), 1));
5627 gimple_seq_add_stmt (ilist, g);
5628 g = gimple_build_cond (LE_EXPR, i, v, body, end);
5629 gimple_seq_add_stmt (ilist, g);
5630 gimple_seq_add_stmt (ilist, gimple_build_label (end));
5631 if (y2)
5632 {
5633 g = gimple_build_assign (y2, POINTER_PLUS_EXPR, y2,
5634 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5635 gimple_seq_add_stmt (dlist, g);
5636 if (y4)
5637 {
5638 g = gimple_build_assign
5639 (y4, POINTER_PLUS_EXPR, y4,
5640 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5641 gimple_seq_add_stmt (dlist, g);
5642 }
5643 g = gimple_build_assign (i2, PLUS_EXPR, i2,
5644 build_int_cst (TREE_TYPE (i2), 1));
5645 gimple_seq_add_stmt (dlist, g);
5646 g = gimple_build_cond (LE_EXPR, i2, v, body2, end2);
5647 gimple_seq_add_stmt (dlist, g);
5648 gimple_seq_add_stmt (dlist, gimple_build_label (end2));
5649 }
5650 if (allocator)
5651 {
5652 tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
5653 g = gimple_build_call (f, 2, allocate_ptr, allocator);
5654 gimple_seq_add_stmt (dlist, g);
5655 }
5656 continue;
5657 }
5658 else if (pass == 2)
5659 {
5660 tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
5661 if (is_global_var (out))
5662 x = var;
5663 else if (is_omp_target (ctx->stmt))
5664 x = out;
5665 else
5666 {
5667 bool by_ref = use_pointer_for_field (var, ctx);
5668 x = build_receiver_ref (var, by_ref, ctx);
5669 }
5670 if (!omp_privatize_by_reference (var))
5671 x = build_fold_addr_expr (x);
5672 x = fold_convert (ptr_type_node, x);
5673 unsigned cnt = task_reduction_cnt - 1;
5674 if (!task_reduction_needs_orig_p)
5675 cnt += task_reduction_cntorig_full - task_reduction_cntorig;
5676 else
5677 cnt = task_reduction_cntorig - 1;
5678 tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5679 size_int (cnt), NULL_TREE, NULL_TREE);
5680 gimplify_assign (r, x, ilist);
5681 continue;
5682 }
5683 else if (pass == 3)
5684 {
5685 tree type = TREE_TYPE (new_var);
5686 if (!omp_privatize_by_reference (var))
5687 type = build_pointer_type (type);
5688 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5689 {
5690 unsigned cnt = task_reduction_cnt - 1;
5691 if (!task_reduction_needs_orig_p)
5692 cnt += (task_reduction_cntorig_full
5693 - task_reduction_cntorig);
5694 else
5695 cnt = task_reduction_cntorig - 1;
5696 x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5697 size_int (cnt), NULL_TREE, NULL_TREE);
5698 }
5699 else
5700 {
5701 unsigned int idx = *ctx->task_reduction_map->get (c);
5702 tree off;
5703 if (ctx->task_reductions[1 + idx])
5704 off = fold_convert (sizetype,
5705 ctx->task_reductions[1 + idx]);
5706 else
5707 off = task_reduction_read (ilist, tskred_temp, sizetype,
5708 7 + 3 * idx + 1);
5709 x = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node,
5710 tskred_base, off);
5711 }
5712 x = fold_convert (type, x);
5713 tree t;
5714 if (omp_privatize_by_reference (var))
5715 {
5716 gimplify_assign (new_var, x, ilist);
5717 t = new_var;
5718 new_var = build_simple_mem_ref (new_var);
5719 }
5720 else
5721 {
5722 t = create_tmp_var (type);
5723 gimplify_assign (t, x, ilist);
5724 SET_DECL_VALUE_EXPR (new_var, build_simple_mem_ref (t));
5725 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5726 }
5727 t = fold_convert (build_pointer_type (boolean_type_node), t);
5728 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
5729 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5730 cond = create_tmp_var (TREE_TYPE (t));
5731 gimplify_assign (cond, t, ilist);
5732 }
5733 else if (is_variable_sized (var))
5734 {
5735 /* For variable sized types, we need to allocate the
5736 actual storage here. Call alloca and store the
5737 result in the pointer decl that we created elsewhere. */
5738 if (pass == 0)
5739 continue;
5740
5741 if (c_kind != OMP_CLAUSE_FIRSTPRIVATE || !is_task_ctx (ctx))
5742 {
5743 tree tmp;
5744
5745 ptr = DECL_VALUE_EXPR (new_var);
5746 gcc_assert (TREE_CODE (ptr) == INDIRECT_REF);
5747 ptr = TREE_OPERAND (ptr, 0);
5748 gcc_assert (DECL_P (ptr));
5749 x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
5750
5751 if (lower_private_allocate (var, new_var, allocator,
5752 allocate_ptr, ilist, ctx,
5753 false, x))
5754 tmp = allocate_ptr;
5755 else
5756 {
5757 /* void *tmp = __builtin_alloca */
5758 tree atmp
5759 = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5760 gcall *stmt
5761 = gimple_build_call (atmp, 2, x,
5762 size_int (DECL_ALIGN (var)));
5763 cfun->calls_alloca = 1;
5764 tmp = create_tmp_var_raw (ptr_type_node);
5765 gimple_add_tmp_var (tmp);
5766 gimple_call_set_lhs (stmt, tmp);
5767
5768 gimple_seq_add_stmt (ilist, stmt);
5769 }
5770
5771 x = fold_convert_loc (clause_loc, TREE_TYPE (ptr), tmp);
5772 gimplify_assign (ptr, x, ilist);
5773 }
5774 }
5775 else if (omp_privatize_by_reference (var)
5776 && (c_kind != OMP_CLAUSE_FIRSTPRIVATE
5777 || !OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c)))
5778 {
5779 /* For references that are being privatized for Fortran,
5780 allocate new backing storage for the new pointer
5781 variable. This allows us to avoid changing all the
5782 code that expects a pointer to something that expects
5783 a direct variable. */
5784 if (pass == 0)
5785 continue;
5786
5787 x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
5788 if (c_kind == OMP_CLAUSE_FIRSTPRIVATE && is_task_ctx (ctx))
5789 {
5790 x = build_receiver_ref (var, false, ctx);
5791 if (ctx->allocate_map)
5792 if (tree *allocatep = ctx->allocate_map->get (var))
5793 {
5794 allocator = *allocatep;
5795 if (TREE_CODE (allocator) == TREE_LIST)
5796 allocator = TREE_PURPOSE (allocator);
5797 if (TREE_CODE (allocator) != INTEGER_CST)
5798 allocator = build_outer_var_ref (allocator, ctx);
5799 allocator = fold_convert (pointer_sized_int_node,
5800 allocator);
5801 allocate_ptr = unshare_expr (x);
5802 }
5803 if (allocator == NULL_TREE)
5804 x = build_fold_addr_expr_loc (clause_loc, x);
5805 }
5806 else if (lower_private_allocate (var, new_var, allocator,
5807 allocate_ptr,
5808 ilist, ctx, true, x))
5809 x = allocate_ptr;
5810 else if (TREE_CONSTANT (x))
5811 {
5812 /* For reduction in SIMD loop, defer adding the
5813 initialization of the reference, because if we decide
5814 to use SIMD array for it, the initilization could cause
5815 expansion ICE. Ditto for other privatization clauses. */
5816 if (is_simd)
5817 x = NULL_TREE;
5818 else
5819 {
5820 x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
5821 get_name (var));
5822 gimple_add_tmp_var (x);
5823 TREE_ADDRESSABLE (x) = 1;
5824 x = build_fold_addr_expr_loc (clause_loc, x);
5825 }
5826 }
5827 else
5828 {
5829 tree atmp
5830 = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5831 tree rtype = TREE_TYPE (TREE_TYPE (new_var));
5832 tree al = size_int (TYPE_ALIGN (rtype));
5833 x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
5834 }
5835
5836 if (x)
5837 {
5838 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5839 gimplify_assign (new_var, x, ilist);
5840 }
5841
5842 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
5843 }
5844 else if ((c_kind == OMP_CLAUSE_REDUCTION
5845 || c_kind == OMP_CLAUSE_IN_REDUCTION)
5846 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5847 {
5848 if (pass == 0)
5849 continue;
5850 }
5851 else if (pass != 0)
5852 continue;
5853
5854 switch (OMP_CLAUSE_CODE (c))
5855 {
5856 case OMP_CLAUSE_SHARED:
5857 /* Ignore shared directives in teams construct inside
5858 target construct. */
5859 if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
5860 && !is_host_teams_ctx (ctx))
5861 continue;
5862 /* Shared global vars are just accessed directly. */
5863 if (is_global_var (new_var))
5864 break;
5865 /* For taskloop firstprivate/lastprivate, represented
5866 as firstprivate and shared clause on the task, new_var
5867 is the firstprivate var. */
5868 if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
5869 break;
5870 /* Set up the DECL_VALUE_EXPR for shared variables now. This
5871 needs to be delayed until after fixup_child_record_type so
5872 that we get the correct type during the dereference. */
5873 by_ref = use_pointer_for_field (var, ctx);
5874 x = build_receiver_ref (var, by_ref, ctx);
5875 SET_DECL_VALUE_EXPR (new_var, x);
5876 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5877
5878 /* ??? If VAR is not passed by reference, and the variable
5879 hasn't been initialized yet, then we'll get a warning for
5880 the store into the omp_data_s structure. Ideally, we'd be
5881 able to notice this and not store anything at all, but
5882 we're generating code too early. Suppress the warning. */
5883 if (!by_ref)
5884 suppress_warning (var, OPT_Wuninitialized);
5885 break;
5886
5887 case OMP_CLAUSE__CONDTEMP_:
5888 if (is_parallel_ctx (ctx))
5889 {
5890 x = build_receiver_ref (var, false, ctx);
5891 SET_DECL_VALUE_EXPR (new_var, x);
5892 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5893 }
5894 else if (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c))
5895 {
5896 x = build_zero_cst (TREE_TYPE (var));
5897 goto do_private;
5898 }
5899 break;
5900
5901 case OMP_CLAUSE_LASTPRIVATE:
5902 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
5903 break;
5904 /* FALLTHRU */
5905
5906 case OMP_CLAUSE_PRIVATE:
5907 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE)
5908 x = build_outer_var_ref (var, ctx);
5909 else if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
5910 {
5911 if (is_task_ctx (ctx))
5912 x = build_receiver_ref (var, false, ctx);
5913 else
5914 x = build_outer_var_ref (var, ctx, OMP_CLAUSE_PRIVATE);
5915 }
5916 else
5917 x = NULL;
5918 do_private:
5919 tree nx;
5920 bool copy_ctor;
5921 copy_ctor = false;
5922 lower_private_allocate (var, new_var, allocator, allocate_ptr,
5923 ilist, ctx, false, NULL_TREE);
5924 nx = unshare_expr (new_var);
5925 if (is_simd
5926 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5927 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
5928 copy_ctor = true;
5929 if (copy_ctor)
5930 nx = lang_hooks.decls.omp_clause_copy_ctor (c, nx, x);
5931 else
5932 nx = lang_hooks.decls.omp_clause_default_ctor (c, nx, x);
5933 if (is_simd)
5934 {
5935 tree y = lang_hooks.decls.omp_clause_dtor (c, new_var);
5936 if ((TREE_ADDRESSABLE (new_var) || nx || y
5937 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5938 && (gimple_omp_for_collapse (ctx->stmt) != 1
5939 || (gimple_omp_for_index (ctx->stmt, 0)
5940 != new_var)))
5941 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_
5942 || omp_privatize_by_reference (var))
5943 && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
5944 ivar, lvar))
5945 {
5946 if (omp_privatize_by_reference (var))
5947 {
5948 gcc_assert (TREE_CODE (new_var) == MEM_REF);
5949 tree new_vard = TREE_OPERAND (new_var, 0);
5950 gcc_assert (DECL_P (new_vard));
5951 SET_DECL_VALUE_EXPR (new_vard,
5952 build_fold_addr_expr (lvar));
5953 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
5954 }
5955
5956 if (nx)
5957 {
5958 tree iv = unshare_expr (ivar);
5959 if (copy_ctor)
5960 x = lang_hooks.decls.omp_clause_copy_ctor (c, iv,
5961 x);
5962 else
5963 x = lang_hooks.decls.omp_clause_default_ctor (c,
5964 iv,
5965 x);
5966 }
5967 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_)
5968 {
5969 x = build2 (MODIFY_EXPR, TREE_TYPE (ivar),
5970 unshare_expr (ivar), x);
5971 nx = x;
5972 }
5973 if (nx && x)
5974 gimplify_and_add (x, &llist[0]);
5975 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5976 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
5977 {
5978 tree v = new_var;
5979 if (!DECL_P (v))
5980 {
5981 gcc_assert (TREE_CODE (v) == MEM_REF);
5982 v = TREE_OPERAND (v, 0);
5983 gcc_assert (DECL_P (v));
5984 }
5985 v = *ctx->lastprivate_conditional_map->get (v);
5986 tree t = create_tmp_var (TREE_TYPE (v));
5987 tree z = build_zero_cst (TREE_TYPE (v));
5988 tree orig_v
5989 = build_outer_var_ref (var, ctx,
5990 OMP_CLAUSE_LASTPRIVATE);
5991 gimple_seq_add_stmt (dlist,
5992 gimple_build_assign (t, z));
5993 gcc_assert (DECL_HAS_VALUE_EXPR_P (v));
5994 tree civar = DECL_VALUE_EXPR (v);
5995 gcc_assert (TREE_CODE (civar) == ARRAY_REF);
5996 civar = unshare_expr (civar);
5997 TREE_OPERAND (civar, 1) = sctx.idx;
5998 x = build2 (MODIFY_EXPR, TREE_TYPE (t), t,
5999 unshare_expr (civar));
6000 x = build2 (COMPOUND_EXPR, TREE_TYPE (orig_v), x,
6001 build2 (MODIFY_EXPR, TREE_TYPE (orig_v),
6002 orig_v, unshare_expr (ivar)));
6003 tree cond = build2 (LT_EXPR, boolean_type_node, t,
6004 civar);
6005 x = build3 (COND_EXPR, void_type_node, cond, x,
6006 void_node);
6007 gimple_seq tseq = NULL;
6008 gimplify_and_add (x, &tseq);
6009 if (ctx->outer)
6010 lower_omp (&tseq, ctx->outer);
6011 gimple_seq_add_seq (&llist[1], tseq);
6012 }
6013 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6014 && ctx->for_simd_scan_phase)
6015 {
6016 x = unshare_expr (ivar);
6017 tree orig_v
6018 = build_outer_var_ref (var, ctx,
6019 OMP_CLAUSE_LASTPRIVATE);
6020 x = lang_hooks.decls.omp_clause_assign_op (c, x,
6021 orig_v);
6022 gimplify_and_add (x, &llist[0]);
6023 }
6024 if (y)
6025 {
6026 y = lang_hooks.decls.omp_clause_dtor (c, ivar);
6027 if (y)
6028 gimplify_and_add (y, &llist[1]);
6029 }
6030 break;
6031 }
6032 if (omp_privatize_by_reference (var))
6033 {
6034 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6035 tree new_vard = TREE_OPERAND (new_var, 0);
6036 gcc_assert (DECL_P (new_vard));
6037 tree type = TREE_TYPE (TREE_TYPE (new_vard));
6038 x = TYPE_SIZE_UNIT (type);
6039 if (TREE_CONSTANT (x))
6040 {
6041 x = create_tmp_var_raw (type, get_name (var));
6042 gimple_add_tmp_var (x);
6043 TREE_ADDRESSABLE (x) = 1;
6044 x = build_fold_addr_expr_loc (clause_loc, x);
6045 x = fold_convert_loc (clause_loc,
6046 TREE_TYPE (new_vard), x);
6047 gimplify_assign (new_vard, x, ilist);
6048 }
6049 }
6050 }
6051 if (nx)
6052 gimplify_and_add (nx, ilist);
6053 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6054 && is_simd
6055 && ctx->for_simd_scan_phase)
6056 {
6057 tree orig_v = build_outer_var_ref (var, ctx,
6058 OMP_CLAUSE_LASTPRIVATE);
6059 x = lang_hooks.decls.omp_clause_assign_op (c, new_var,
6060 orig_v);
6061 gimplify_and_add (x, ilist);
6062 }
6063 /* FALLTHRU */
6064
6065 do_dtor:
6066 x = lang_hooks.decls.omp_clause_dtor (c, new_var);
6067 if (x)
6068 gimplify_and_add (x, dlist);
6069 if (allocator)
6070 {
6071 if (!is_gimple_val (allocator))
6072 {
6073 tree avar = create_tmp_var (TREE_TYPE (allocator));
6074 gimplify_assign (avar, allocator, dlist);
6075 allocator = avar;
6076 }
6077 if (!is_gimple_val (allocate_ptr))
6078 {
6079 tree apvar = create_tmp_var (TREE_TYPE (allocate_ptr));
6080 gimplify_assign (apvar, allocate_ptr, dlist);
6081 allocate_ptr = apvar;
6082 }
6083 tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
6084 gimple *g
6085 = gimple_build_call (f, 2, allocate_ptr, allocator);
6086 gimple_seq_add_stmt (dlist, g);
6087 }
6088 break;
6089
6090 case OMP_CLAUSE_LINEAR:
6091 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6092 goto do_firstprivate;
6093 if (OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6094 x = NULL;
6095 else
6096 x = build_outer_var_ref (var, ctx);
6097 goto do_private;
6098
6099 case OMP_CLAUSE_FIRSTPRIVATE:
6100 if (is_task_ctx (ctx))
6101 {
6102 if ((omp_privatize_by_reference (var)
6103 && !OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c))
6104 || is_variable_sized (var))
6105 goto do_dtor;
6106 else if (is_global_var (maybe_lookup_decl_in_outer_ctx (var,
6107 ctx))
6108 || use_pointer_for_field (var, NULL))
6109 {
6110 x = build_receiver_ref (var, false, ctx);
6111 if (ctx->allocate_map)
6112 if (tree *allocatep = ctx->allocate_map->get (var))
6113 {
6114 allocator = *allocatep;
6115 if (TREE_CODE (allocator) == TREE_LIST)
6116 allocator = TREE_PURPOSE (allocator);
6117 if (TREE_CODE (allocator) != INTEGER_CST)
6118 allocator = build_outer_var_ref (allocator, ctx);
6119 allocator = fold_convert (pointer_sized_int_node,
6120 allocator);
6121 allocate_ptr = unshare_expr (x);
6122 x = build_simple_mem_ref (x);
6123 TREE_THIS_NOTRAP (x) = 1;
6124 }
6125 SET_DECL_VALUE_EXPR (new_var, x);
6126 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
6127 goto do_dtor;
6128 }
6129 }
6130 if (OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c)
6131 && omp_privatize_by_reference (var))
6132 {
6133 x = build_outer_var_ref (var, ctx);
6134 gcc_assert (TREE_CODE (x) == MEM_REF
6135 && integer_zerop (TREE_OPERAND (x, 1)));
6136 x = TREE_OPERAND (x, 0);
6137 x = lang_hooks.decls.omp_clause_copy_ctor
6138 (c, unshare_expr (new_var), x);
6139 gimplify_and_add (x, ilist);
6140 goto do_dtor;
6141 }
6142 do_firstprivate:
6143 lower_private_allocate (var, new_var, allocator, allocate_ptr,
6144 ilist, ctx, false, NULL_TREE);
6145 x = build_outer_var_ref (var, ctx);
6146 if (is_simd)
6147 {
6148 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6149 && gimple_omp_for_combined_into_p (ctx->stmt))
6150 {
6151 tree t = OMP_CLAUSE_LINEAR_STEP (c);
6152 if (DECL_P (t))
6153 t = build_outer_var_ref (t, ctx);
6154 tree stept = TREE_TYPE (t);
6155 tree ct = omp_find_clause (clauses,
6156 OMP_CLAUSE__LOOPTEMP_);
6157 gcc_assert (ct);
6158 tree l = OMP_CLAUSE_DECL (ct);
6159 tree n1 = fd->loop.n1;
6160 tree step = fd->loop.step;
6161 tree itype = TREE_TYPE (l);
6162 if (POINTER_TYPE_P (itype))
6163 itype = signed_type_for (itype);
6164 l = fold_build2 (MINUS_EXPR, itype, l, n1);
6165 if (TYPE_UNSIGNED (itype)
6166 && fd->loop.cond_code == GT_EXPR)
6167 l = fold_build2 (TRUNC_DIV_EXPR, itype,
6168 fold_build1 (NEGATE_EXPR, itype, l),
6169 fold_build1 (NEGATE_EXPR,
6170 itype, step));
6171 else
6172 l = fold_build2 (TRUNC_DIV_EXPR, itype, l, step);
6173 t = fold_build2 (MULT_EXPR, stept,
6174 fold_convert (stept, l), t);
6175
6176 if (OMP_CLAUSE_LINEAR_ARRAY (c))
6177 {
6178 if (omp_privatize_by_reference (var))
6179 {
6180 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6181 tree new_vard = TREE_OPERAND (new_var, 0);
6182 gcc_assert (DECL_P (new_vard));
6183 tree type = TREE_TYPE (TREE_TYPE (new_vard));
6184 nx = TYPE_SIZE_UNIT (type);
6185 if (TREE_CONSTANT (nx))
6186 {
6187 nx = create_tmp_var_raw (type,
6188 get_name (var));
6189 gimple_add_tmp_var (nx);
6190 TREE_ADDRESSABLE (nx) = 1;
6191 nx = build_fold_addr_expr_loc (clause_loc,
6192 nx);
6193 nx = fold_convert_loc (clause_loc,
6194 TREE_TYPE (new_vard),
6195 nx);
6196 gimplify_assign (new_vard, nx, ilist);
6197 }
6198 }
6199
6200 x = lang_hooks.decls.omp_clause_linear_ctor
6201 (c, new_var, x, t);
6202 gimplify_and_add (x, ilist);
6203 goto do_dtor;
6204 }
6205
6206 if (POINTER_TYPE_P (TREE_TYPE (x)))
6207 x = fold_build_pointer_plus (x, t);
6208 else
6209 x = fold_build2 (PLUS_EXPR, TREE_TYPE (x), x,
6210 fold_convert (TREE_TYPE (x), t));
6211 }
6212
6213 if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6214 || TREE_ADDRESSABLE (new_var)
6215 || omp_privatize_by_reference (var))
6216 && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6217 ivar, lvar))
6218 {
6219 if (omp_privatize_by_reference (var))
6220 {
6221 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6222 tree new_vard = TREE_OPERAND (new_var, 0);
6223 gcc_assert (DECL_P (new_vard));
6224 SET_DECL_VALUE_EXPR (new_vard,
6225 build_fold_addr_expr (lvar));
6226 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6227 }
6228 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
6229 {
6230 tree iv = create_tmp_var (TREE_TYPE (new_var));
6231 x = lang_hooks.decls.omp_clause_copy_ctor (c, iv, x);
6232 gimplify_and_add (x, ilist);
6233 gimple_stmt_iterator gsi
6234 = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
6235 gassign *g
6236 = gimple_build_assign (unshare_expr (lvar), iv);
6237 gsi_insert_before_without_update (&gsi, g,
6238 GSI_SAME_STMT);
6239 tree t = OMP_CLAUSE_LINEAR_STEP (c);
6240 enum tree_code code = PLUS_EXPR;
6241 if (POINTER_TYPE_P (TREE_TYPE (new_var)))
6242 code = POINTER_PLUS_EXPR;
6243 g = gimple_build_assign (iv, code, iv, t);
6244 gsi_insert_before_without_update (&gsi, g,
6245 GSI_SAME_STMT);
6246 break;
6247 }
6248 x = lang_hooks.decls.omp_clause_copy_ctor
6249 (c, unshare_expr (ivar), x);
6250 gimplify_and_add (x, &llist[0]);
6251 x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6252 if (x)
6253 gimplify_and_add (x, &llist[1]);
6254 break;
6255 }
6256 if (omp_privatize_by_reference (var))
6257 {
6258 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6259 tree new_vard = TREE_OPERAND (new_var, 0);
6260 gcc_assert (DECL_P (new_vard));
6261 tree type = TREE_TYPE (TREE_TYPE (new_vard));
6262 nx = TYPE_SIZE_UNIT (type);
6263 if (TREE_CONSTANT (nx))
6264 {
6265 nx = create_tmp_var_raw (type, get_name (var));
6266 gimple_add_tmp_var (nx);
6267 TREE_ADDRESSABLE (nx) = 1;
6268 nx = build_fold_addr_expr_loc (clause_loc, nx);
6269 nx = fold_convert_loc (clause_loc,
6270 TREE_TYPE (new_vard), nx);
6271 gimplify_assign (new_vard, nx, ilist);
6272 }
6273 }
6274 }
6275 x = lang_hooks.decls.omp_clause_copy_ctor
6276 (c, unshare_expr (new_var), x);
6277 gimplify_and_add (x, ilist);
6278 goto do_dtor;
6279
6280 case OMP_CLAUSE__LOOPTEMP_:
6281 case OMP_CLAUSE__REDUCTEMP_:
6282 gcc_assert (is_taskreg_ctx (ctx));
6283 x = build_outer_var_ref (var, ctx);
6284 x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
6285 gimplify_and_add (x, ilist);
6286 break;
6287
6288 case OMP_CLAUSE_COPYIN:
6289 by_ref = use_pointer_for_field (var, NULL);
6290 x = build_receiver_ref (var, by_ref, ctx);
6291 x = lang_hooks.decls.omp_clause_assign_op (c, new_var, x);
6292 append_to_statement_list (x, &copyin_seq);
6293 copyin_by_ref |= by_ref;
6294 break;
6295
6296 case OMP_CLAUSE_REDUCTION:
6297 case OMP_CLAUSE_IN_REDUCTION:
6298 /* OpenACC reductions are initialized using the
6299 GOACC_REDUCTION internal function. */
6300 if (is_gimple_omp_oacc (ctx->stmt))
6301 break;
6302 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6303 {
6304 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6305 gimple *tseq;
6306 tree ptype = TREE_TYPE (placeholder);
6307 if (cond)
6308 {
6309 x = error_mark_node;
6310 if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)
6311 && !task_reduction_needs_orig_p)
6312 x = var;
6313 else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
6314 {
6315 tree pptype = build_pointer_type (ptype);
6316 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
6317 x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
6318 size_int (task_reduction_cnt_full
6319 + task_reduction_cntorig - 1),
6320 NULL_TREE, NULL_TREE);
6321 else
6322 {
6323 unsigned int idx
6324 = *ctx->task_reduction_map->get (c);
6325 x = task_reduction_read (ilist, tskred_temp,
6326 pptype, 7 + 3 * idx);
6327 }
6328 x = fold_convert (pptype, x);
6329 x = build_simple_mem_ref (x);
6330 }
6331 }
6332 else
6333 {
6334 lower_private_allocate (var, new_var, allocator,
6335 allocate_ptr, ilist, ctx, false,
6336 NULL_TREE);
6337 x = build_outer_var_ref (var, ctx);
6338
6339 if (omp_privatize_by_reference (var)
6340 && !useless_type_conversion_p (ptype, TREE_TYPE (x)))
6341 x = build_fold_addr_expr_loc (clause_loc, x);
6342 }
6343 SET_DECL_VALUE_EXPR (placeholder, x);
6344 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
6345 tree new_vard = new_var;
6346 if (omp_privatize_by_reference (var))
6347 {
6348 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6349 new_vard = TREE_OPERAND (new_var, 0);
6350 gcc_assert (DECL_P (new_vard));
6351 }
6352 tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
6353 if (is_simd
6354 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6355 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6356 rvarp = &rvar;
6357 if (is_simd
6358 && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6359 ivar, lvar, rvarp,
6360 &rvar2))
6361 {
6362 if (new_vard == new_var)
6363 {
6364 gcc_assert (DECL_VALUE_EXPR (new_var) == lvar);
6365 SET_DECL_VALUE_EXPR (new_var, ivar);
6366 }
6367 else
6368 {
6369 SET_DECL_VALUE_EXPR (new_vard,
6370 build_fold_addr_expr (ivar));
6371 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6372 }
6373 x = lang_hooks.decls.omp_clause_default_ctor
6374 (c, unshare_expr (ivar),
6375 build_outer_var_ref (var, ctx));
6376 if (rvarp && ctx->for_simd_scan_phase)
6377 {
6378 if (x)
6379 gimplify_and_add (x, &llist[0]);
6380 x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6381 if (x)
6382 gimplify_and_add (x, &llist[1]);
6383 break;
6384 }
6385 else if (rvarp)
6386 {
6387 if (x)
6388 {
6389 gimplify_and_add (x, &llist[0]);
6390
6391 tree ivar2 = unshare_expr (lvar);
6392 TREE_OPERAND (ivar2, 1) = sctx.idx;
6393 x = lang_hooks.decls.omp_clause_default_ctor
6394 (c, ivar2, build_outer_var_ref (var, ctx));
6395 gimplify_and_add (x, &llist[0]);
6396
6397 if (rvar2)
6398 {
6399 x = lang_hooks.decls.omp_clause_default_ctor
6400 (c, unshare_expr (rvar2),
6401 build_outer_var_ref (var, ctx));
6402 gimplify_and_add (x, &llist[0]);
6403 }
6404
6405 /* For types that need construction, add another
6406 private var which will be default constructed
6407 and optionally initialized with
6408 OMP_CLAUSE_REDUCTION_GIMPLE_INIT, as in the
6409 loop we want to assign this value instead of
6410 constructing and destructing it in each
6411 iteration. */
6412 tree nv = create_tmp_var_raw (TREE_TYPE (ivar));
6413 gimple_add_tmp_var (nv);
6414 ctx->cb.decl_map->put (TREE_OPERAND (rvar2
6415 ? rvar2
6416 : ivar, 0),
6417 nv);
6418 x = lang_hooks.decls.omp_clause_default_ctor
6419 (c, nv, build_outer_var_ref (var, ctx));
6420 gimplify_and_add (x, ilist);
6421
6422 if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6423 {
6424 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6425 x = DECL_VALUE_EXPR (new_vard);
6426 tree vexpr = nv;
6427 if (new_vard != new_var)
6428 vexpr = build_fold_addr_expr (nv);
6429 SET_DECL_VALUE_EXPR (new_vard, vexpr);
6430 lower_omp (&tseq, ctx);
6431 SET_DECL_VALUE_EXPR (new_vard, x);
6432 gimple_seq_add_seq (ilist, tseq);
6433 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6434 }
6435
6436 x = lang_hooks.decls.omp_clause_dtor (c, nv);
6437 if (x)
6438 gimplify_and_add (x, dlist);
6439 }
6440
6441 tree ref = build_outer_var_ref (var, ctx);
6442 x = unshare_expr (ivar);
6443 x = lang_hooks.decls.omp_clause_assign_op (c, x,
6444 ref);
6445 gimplify_and_add (x, &llist[0]);
6446
6447 ref = build_outer_var_ref (var, ctx);
6448 x = lang_hooks.decls.omp_clause_assign_op (c, ref,
6449 rvar);
6450 gimplify_and_add (x, &llist[3]);
6451
6452 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6453 if (new_vard == new_var)
6454 SET_DECL_VALUE_EXPR (new_var, lvar);
6455 else
6456 SET_DECL_VALUE_EXPR (new_vard,
6457 build_fold_addr_expr (lvar));
6458
6459 x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6460 if (x)
6461 gimplify_and_add (x, &llist[1]);
6462
6463 tree ivar2 = unshare_expr (lvar);
6464 TREE_OPERAND (ivar2, 1) = sctx.idx;
6465 x = lang_hooks.decls.omp_clause_dtor (c, ivar2);
6466 if (x)
6467 gimplify_and_add (x, &llist[1]);
6468
6469 if (rvar2)
6470 {
6471 x = lang_hooks.decls.omp_clause_dtor (c, rvar2);
6472 if (x)
6473 gimplify_and_add (x, &llist[1]);
6474 }
6475 break;
6476 }
6477 if (x)
6478 gimplify_and_add (x, &llist[0]);
6479 if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6480 {
6481 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6482 lower_omp (&tseq, ctx);
6483 gimple_seq_add_seq (&llist[0], tseq);
6484 }
6485 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6486 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
6487 lower_omp (&tseq, ctx);
6488 gimple_seq_add_seq (&llist[1], tseq);
6489 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6490 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6491 if (new_vard == new_var)
6492 SET_DECL_VALUE_EXPR (new_var, lvar);
6493 else
6494 SET_DECL_VALUE_EXPR (new_vard,
6495 build_fold_addr_expr (lvar));
6496 x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6497 if (x)
6498 gimplify_and_add (x, &llist[1]);
6499 break;
6500 }
6501 /* If this is a reference to constant size reduction var
6502 with placeholder, we haven't emitted the initializer
6503 for it because it is undesirable if SIMD arrays are used.
6504 But if they aren't used, we need to emit the deferred
6505 initialization now. */
6506 else if (omp_privatize_by_reference (var) && is_simd)
6507 handle_simd_reference (clause_loc, new_vard, ilist);
6508
6509 tree lab2 = NULL_TREE;
6510 if (cond)
6511 {
6512 gimple *g;
6513 if (!is_parallel_ctx (ctx))
6514 {
6515 tree condv = create_tmp_var (boolean_type_node);
6516 tree m = build_simple_mem_ref (cond);
6517 g = gimple_build_assign (condv, m);
6518 gimple_seq_add_stmt (ilist, g);
6519 tree lab1
6520 = create_artificial_label (UNKNOWN_LOCATION);
6521 lab2 = create_artificial_label (UNKNOWN_LOCATION);
6522 g = gimple_build_cond (NE_EXPR, condv,
6523 boolean_false_node,
6524 lab2, lab1);
6525 gimple_seq_add_stmt (ilist, g);
6526 gimple_seq_add_stmt (ilist,
6527 gimple_build_label (lab1));
6528 }
6529 g = gimple_build_assign (build_simple_mem_ref (cond),
6530 boolean_true_node);
6531 gimple_seq_add_stmt (ilist, g);
6532 }
6533 x = lang_hooks.decls.omp_clause_default_ctor
6534 (c, unshare_expr (new_var),
6535 cond ? NULL_TREE
6536 : build_outer_var_ref (var, ctx));
6537 if (x)
6538 gimplify_and_add (x, ilist);
6539
6540 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6541 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6542 {
6543 if (ctx->for_simd_scan_phase)
6544 goto do_dtor;
6545 if (x || (!is_simd
6546 && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)))
6547 {
6548 tree nv = create_tmp_var_raw (TREE_TYPE (new_var));
6549 gimple_add_tmp_var (nv);
6550 ctx->cb.decl_map->put (new_vard, nv);
6551 x = lang_hooks.decls.omp_clause_default_ctor
6552 (c, nv, build_outer_var_ref (var, ctx));
6553 if (x)
6554 gimplify_and_add (x, ilist);
6555 if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6556 {
6557 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6558 tree vexpr = nv;
6559 if (new_vard != new_var)
6560 vexpr = build_fold_addr_expr (nv);
6561 SET_DECL_VALUE_EXPR (new_vard, vexpr);
6562 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6563 lower_omp (&tseq, ctx);
6564 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
6565 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
6566 gimple_seq_add_seq (ilist, tseq);
6567 }
6568 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6569 if (is_simd && ctx->scan_exclusive)
6570 {
6571 tree nv2
6572 = create_tmp_var_raw (TREE_TYPE (new_var));
6573 gimple_add_tmp_var (nv2);
6574 ctx->cb.decl_map->put (nv, nv2);
6575 x = lang_hooks.decls.omp_clause_default_ctor
6576 (c, nv2, build_outer_var_ref (var, ctx));
6577 gimplify_and_add (x, ilist);
6578 x = lang_hooks.decls.omp_clause_dtor (c, nv2);
6579 if (x)
6580 gimplify_and_add (x, dlist);
6581 }
6582 x = lang_hooks.decls.omp_clause_dtor (c, nv);
6583 if (x)
6584 gimplify_and_add (x, dlist);
6585 }
6586 else if (is_simd
6587 && ctx->scan_exclusive
6588 && TREE_ADDRESSABLE (TREE_TYPE (new_var)))
6589 {
6590 tree nv2 = create_tmp_var_raw (TREE_TYPE (new_var));
6591 gimple_add_tmp_var (nv2);
6592 ctx->cb.decl_map->put (new_vard, nv2);
6593 x = lang_hooks.decls.omp_clause_dtor (c, nv2);
6594 if (x)
6595 gimplify_and_add (x, dlist);
6596 }
6597 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6598 goto do_dtor;
6599 }
6600
6601 if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6602 {
6603 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6604 if (c_kind == OMP_CLAUSE_IN_REDUCTION
6605 && is_omp_target (ctx->stmt))
6606 {
6607 tree d = maybe_lookup_decl_in_outer_ctx (var, ctx);
6608 tree oldv = NULL_TREE;
6609 gcc_assert (d);
6610 if (DECL_HAS_VALUE_EXPR_P (d))
6611 oldv = DECL_VALUE_EXPR (d);
6612 SET_DECL_VALUE_EXPR (d, new_vard);
6613 DECL_HAS_VALUE_EXPR_P (d) = 1;
6614 lower_omp (&tseq, ctx);
6615 if (oldv)
6616 SET_DECL_VALUE_EXPR (d, oldv);
6617 else
6618 {
6619 SET_DECL_VALUE_EXPR (d, NULL_TREE);
6620 DECL_HAS_VALUE_EXPR_P (d) = 0;
6621 }
6622 }
6623 else
6624 lower_omp (&tseq, ctx);
6625 gimple_seq_add_seq (ilist, tseq);
6626 }
6627 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6628 if (is_simd)
6629 {
6630 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
6631 lower_omp (&tseq, ctx);
6632 gimple_seq_add_seq (dlist, tseq);
6633 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6634 }
6635 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6636 if (cond)
6637 {
6638 if (lab2)
6639 gimple_seq_add_stmt (ilist, gimple_build_label (lab2));
6640 break;
6641 }
6642 goto do_dtor;
6643 }
6644 else
6645 {
6646 x = omp_reduction_init (c, TREE_TYPE (new_var));
6647 gcc_assert (TREE_CODE (TREE_TYPE (new_var)) != ARRAY_TYPE);
6648 enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
6649
6650 if (cond)
6651 {
6652 gimple *g;
6653 tree lab2 = NULL_TREE;
6654 /* GOMP_taskgroup_reduction_register memsets the whole
6655 array to zero. If the initializer is zero, we don't
6656 need to initialize it again, just mark it as ever
6657 used unconditionally, i.e. cond = true. */
6658 if (initializer_zerop (x))
6659 {
6660 g = gimple_build_assign (build_simple_mem_ref (cond),
6661 boolean_true_node);
6662 gimple_seq_add_stmt (ilist, g);
6663 break;
6664 }
6665
6666 /* Otherwise, emit
6667 if (!cond) { cond = true; new_var = x; } */
6668 if (!is_parallel_ctx (ctx))
6669 {
6670 tree condv = create_tmp_var (boolean_type_node);
6671 tree m = build_simple_mem_ref (cond);
6672 g = gimple_build_assign (condv, m);
6673 gimple_seq_add_stmt (ilist, g);
6674 tree lab1
6675 = create_artificial_label (UNKNOWN_LOCATION);
6676 lab2 = create_artificial_label (UNKNOWN_LOCATION);
6677 g = gimple_build_cond (NE_EXPR, condv,
6678 boolean_false_node,
6679 lab2, lab1);
6680 gimple_seq_add_stmt (ilist, g);
6681 gimple_seq_add_stmt (ilist,
6682 gimple_build_label (lab1));
6683 }
6684 g = gimple_build_assign (build_simple_mem_ref (cond),
6685 boolean_true_node);
6686 gimple_seq_add_stmt (ilist, g);
6687 gimplify_assign (new_var, x, ilist);
6688 if (lab2)
6689 gimple_seq_add_stmt (ilist, gimple_build_label (lab2));
6690 break;
6691 }
6692
6693 /* reduction(-:var) sums up the partial results, so it
6694 acts identically to reduction(+:var). */
6695 if (code == MINUS_EXPR)
6696 code = PLUS_EXPR;
6697
6698 bool is_truth_op
6699 = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
6700 tree new_vard = new_var;
6701 if (is_simd && omp_privatize_by_reference (var))
6702 {
6703 gcc_assert (TREE_CODE (new_var) == MEM_REF);
6704 new_vard = TREE_OPERAND (new_var, 0);
6705 gcc_assert (DECL_P (new_vard));
6706 }
6707 tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
6708 if (is_simd
6709 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6710 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6711 rvarp = &rvar;
6712 if (is_simd
6713 && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6714 ivar, lvar, rvarp,
6715 &rvar2))
6716 {
6717 if (new_vard != new_var)
6718 {
6719 SET_DECL_VALUE_EXPR (new_vard,
6720 build_fold_addr_expr (lvar));
6721 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6722 }
6723
6724 tree ref = build_outer_var_ref (var, ctx);
6725
6726 if (rvarp)
6727 {
6728 if (ctx->for_simd_scan_phase)
6729 break;
6730 gimplify_assign (ivar, ref, &llist[0]);
6731 ref = build_outer_var_ref (var, ctx);
6732 gimplify_assign (ref, rvar, &llist[3]);
6733 break;
6734 }
6735
6736 gimplify_assign (unshare_expr (ivar), x, &llist[0]);
6737
6738 if (sctx.is_simt)
6739 {
6740 if (!simt_lane)
6741 simt_lane = create_tmp_var (unsigned_type_node);
6742 x = build_call_expr_internal_loc
6743 (UNKNOWN_LOCATION, IFN_GOMP_SIMT_XCHG_BFLY,
6744 TREE_TYPE (ivar), 2, ivar, simt_lane);
6745 /* Make sure x is evaluated unconditionally. */
6746 tree bfly_var = create_tmp_var (TREE_TYPE (ivar));
6747 gimplify_assign (bfly_var, x, &llist[2]);
6748 x = build2 (code, TREE_TYPE (ivar), ivar, bfly_var);
6749 gimplify_assign (ivar, x, &llist[2]);
6750 }
6751 tree ivar2 = ivar;
6752 tree ref2 = ref;
6753 if (is_truth_op)
6754 {
6755 tree zero = build_zero_cst (TREE_TYPE (ivar));
6756 ivar2 = fold_build2_loc (clause_loc, NE_EXPR,
6757 boolean_type_node, ivar,
6758 zero);
6759 ref2 = fold_build2_loc (clause_loc, NE_EXPR,
6760 boolean_type_node, ref,
6761 zero);
6762 }
6763 x = build2 (code, TREE_TYPE (ref), ref2, ivar2);
6764 if (is_truth_op)
6765 x = fold_convert (TREE_TYPE (ref), x);
6766 ref = build_outer_var_ref (var, ctx);
6767 gimplify_assign (ref, x, &llist[1]);
6768
6769 }
6770 else
6771 {
6772 lower_private_allocate (var, new_var, allocator,
6773 allocate_ptr, ilist, ctx,
6774 false, NULL_TREE);
6775 if (omp_privatize_by_reference (var) && is_simd)
6776 handle_simd_reference (clause_loc, new_vard, ilist);
6777 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6778 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6779 break;
6780 gimplify_assign (new_var, x, ilist);
6781 if (is_simd)
6782 {
6783 tree ref = build_outer_var_ref (var, ctx);
6784 tree new_var2 = new_var;
6785 tree ref2 = ref;
6786 if (is_truth_op)
6787 {
6788 tree zero = build_zero_cst (TREE_TYPE (new_var));
6789 new_var2
6790 = fold_build2_loc (clause_loc, NE_EXPR,
6791 boolean_type_node, new_var,
6792 zero);
6793 ref2 = fold_build2_loc (clause_loc, NE_EXPR,
6794 boolean_type_node, ref,
6795 zero);
6796 }
6797 x = build2 (code, TREE_TYPE (ref2), ref2, new_var2);
6798 if (is_truth_op)
6799 x = fold_convert (TREE_TYPE (new_var), x);
6800 ref = build_outer_var_ref (var, ctx);
6801 gimplify_assign (ref, x, dlist);
6802 }
6803 if (allocator)
6804 goto do_dtor;
6805 }
6806 }
6807 break;
6808
6809 default:
6810 gcc_unreachable ();
6811 }
6812 }
6813 }
6814 if (tskred_avar)
6815 {
6816 tree clobber = build_clobber (TREE_TYPE (tskred_avar));
6817 gimple_seq_add_stmt (ilist, gimple_build_assign (tskred_avar, clobber));
6818 }
6819
6820 if (known_eq (sctx.max_vf, 1U))
6821 {
6822 sctx.is_simt = false;
6823 if (ctx->lastprivate_conditional_map)
6824 {
6825 if (gimple_omp_for_combined_into_p (ctx->stmt))
6826 {
6827 /* Signal to lower_omp_1 that it should use parent context. */
6828 ctx->combined_into_simd_safelen1 = true;
6829 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6831 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
6832 {
6833 tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
6834 omp_context *outer = ctx->outer;
6835 if (gimple_code (outer->stmt) == GIMPLE_OMP_SCAN)
6836 outer = outer->outer;
6837 tree *v = ctx->lastprivate_conditional_map->get (o);
6838 tree po = lookup_decl (OMP_CLAUSE_DECL (c), outer);
6839 tree *pv = outer->lastprivate_conditional_map->get (po);
6840 *v = *pv;
6841 }
6842 }
6843 else
6844 {
6845 /* When not vectorized, treat lastprivate(conditional:) like
6846 normal lastprivate, as there will be just one simd lane
6847 writing the privatized variable. */
6848 delete ctx->lastprivate_conditional_map;
6849 ctx->lastprivate_conditional_map = NULL;
6850 }
6851 }
6852 }
6853
6854 if (nonconst_simd_if)
6855 {
6856 if (sctx.lane == NULL_TREE)
6857 {
6858 sctx.idx = create_tmp_var (unsigned_type_node);
6859 sctx.lane = create_tmp_var (unsigned_type_node);
6860 }
6861 /* FIXME: For now. */
6862 sctx.is_simt = false;
6863 }
6864
6865 if (sctx.lane || sctx.is_simt)
6866 {
6867 uid = create_tmp_var (ptr_type_node, "simduid");
6868 /* Don't want uninit warnings on simduid, it is always uninitialized,
6869 but we use it not for the value, but for the DECL_UID only. */
6870 suppress_warning (uid, OPT_Wuninitialized);
6871 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SIMDUID_);
6872 OMP_CLAUSE__SIMDUID__DECL (c) = uid;
6873 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
6874 gimple_omp_for_set_clauses (ctx->stmt, c);
6875 }
6876 /* Emit calls denoting privatized variables and initializing a pointer to
6877 structure that holds private variables as fields after ompdevlow pass. */
6878 if (sctx.is_simt)
6879 {
6880 sctx.simt_eargs[0] = uid;
6881 gimple *g
6882 = gimple_build_call_internal_vec (IFN_GOMP_SIMT_ENTER, sctx.simt_eargs);
6883 gimple_call_set_lhs (g, uid);
6884 gimple_seq_add_stmt (ilist, g);
6885 sctx.simt_eargs.release ();
6886
6887 simtrec = create_tmp_var (ptr_type_node, ".omp_simt");
6888 g = gimple_build_call_internal (IFN_GOMP_SIMT_ENTER_ALLOC, 1, uid);
6889 gimple_call_set_lhs (g, simtrec);
6890 gimple_seq_add_stmt (ilist, g);
6891 }
6892 if (sctx.lane)
6893 {
6894 gimple *g = gimple_build_call_internal (IFN_GOMP_SIMD_LANE,
6895 2 + (nonconst_simd_if != NULL),
6896 uid, integer_zero_node,
6897 nonconst_simd_if);
6898 gimple_call_set_lhs (g, sctx.lane);
6899 gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
6900 gsi_insert_before_without_update (&gsi, g, GSI_SAME_STMT);
6901 g = gimple_build_assign (sctx.lane, INTEGER_CST,
6902 build_int_cst (unsigned_type_node, 0));
6903 gimple_seq_add_stmt (ilist, g);
6904 if (sctx.lastlane)
6905 {
6906 g = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
6907 2, uid, sctx.lane);
6908 gimple_call_set_lhs (g, sctx.lastlane);
6909 gimple_seq_add_stmt (dlist, g);
6910 gimple_seq_add_seq (dlist, llist[3]);
6911 }
6912 /* Emit reductions across SIMT lanes in log_2(simt_vf) steps. */
6913 if (llist[2])
6914 {
6915 tree simt_vf = create_tmp_var (unsigned_type_node);
6916 g = gimple_build_call_internal (IFN_GOMP_SIMT_VF, 0);
6917 gimple_call_set_lhs (g, simt_vf);
6918 gimple_seq_add_stmt (dlist, g);
6919
6920 tree t = build_int_cst (unsigned_type_node, 1);
6921 g = gimple_build_assign (simt_lane, INTEGER_CST, t);
6922 gimple_seq_add_stmt (dlist, g);
6923
6924 t = build_int_cst (unsigned_type_node, 0);
6925 g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
6926 gimple_seq_add_stmt (dlist, g);
6927
6928 tree body = create_artificial_label (UNKNOWN_LOCATION);
6929 tree header = create_artificial_label (UNKNOWN_LOCATION);
6930 tree end = create_artificial_label (UNKNOWN_LOCATION);
6931 gimple_seq_add_stmt (dlist, gimple_build_goto (header));
6932 gimple_seq_add_stmt (dlist, gimple_build_label (body));
6933
6934 gimple_seq_add_seq (dlist, llist[2]);
6935
6936 g = gimple_build_assign (simt_lane, LSHIFT_EXPR, simt_lane, integer_one_node);
6937 gimple_seq_add_stmt (dlist, g);
6938
6939 gimple_seq_add_stmt (dlist, gimple_build_label (header));
6940 g = gimple_build_cond (LT_EXPR, simt_lane, simt_vf, body, end);
6941 gimple_seq_add_stmt (dlist, g);
6942
6943 gimple_seq_add_stmt (dlist, gimple_build_label (end));
6944 }
6945 for (int i = 0; i < 2; i++)
6946 if (llist[i])
6947 {
6948 tree vf = create_tmp_var (unsigned_type_node);
6949 g = gimple_build_call_internal (IFN_GOMP_SIMD_VF, 1, uid);
6950 gimple_call_set_lhs (g, vf);
6951 gimple_seq *seq = i == 0 ? ilist : dlist;
6952 gimple_seq_add_stmt (seq, g);
6953 tree t = build_int_cst (unsigned_type_node, 0);
6954 g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
6955 gimple_seq_add_stmt (seq, g);
6956 tree body = create_artificial_label (UNKNOWN_LOCATION);
6957 tree header = create_artificial_label (UNKNOWN_LOCATION);
6958 tree end = create_artificial_label (UNKNOWN_LOCATION);
6959 gimple_seq_add_stmt (seq, gimple_build_goto (header));
6960 gimple_seq_add_stmt (seq, gimple_build_label (body));
6961 gimple_seq_add_seq (seq, llist[i]);
6962 t = build_int_cst (unsigned_type_node, 1);
6963 g = gimple_build_assign (sctx.idx, PLUS_EXPR, sctx.idx, t);
6964 gimple_seq_add_stmt (seq, g);
6965 gimple_seq_add_stmt (seq, gimple_build_label (header));
6966 g = gimple_build_cond (LT_EXPR, sctx.idx, vf, body, end);
6967 gimple_seq_add_stmt (seq, g);
6968 gimple_seq_add_stmt (seq, gimple_build_label (end));
6969 }
6970 }
6971 if (sctx.is_simt)
6972 {
6973 gimple_seq_add_seq (dlist, sctx.simt_dlist);
6974 gimple *g
6975 = gimple_build_call_internal (IFN_GOMP_SIMT_EXIT, 1, simtrec);
6976 gimple_seq_add_stmt (dlist, g);
6977 }
6978
6979 /* The copyin sequence is not to be executed by the main thread, since
6980 that would result in self-copies. Perhaps not visible to scalars,
6981 but it certainly is to C++ operator=. */
6982 if (copyin_seq)
6983 {
6984 x = build_call_expr (builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM),
6985 0);
6986 x = build2 (NE_EXPR, boolean_type_node, x,
6987 build_int_cst (TREE_TYPE (x), 0));
6988 x = build3 (COND_EXPR, void_type_node, x, copyin_seq, NULL);
6989 gimplify_and_add (x, ilist);
6990 }
6991
6992 /* If any copyin variable is passed by reference, we must ensure the
6993 master thread doesn't modify it before it is copied over in all
6994 threads. Similarly for variables in both firstprivate and
6995 lastprivate clauses we need to ensure the lastprivate copying
6996 happens after firstprivate copying in all threads. And similarly
6997 for UDRs if initializer expression refers to omp_orig. */
6998 if (copyin_by_ref || lastprivate_firstprivate
6999 || (reduction_omp_orig_ref
7000 && !ctx->scan_inclusive
7001 && !ctx->scan_exclusive))
7002 {
7003 /* Don't add any barrier for #pragma omp simd or
7004 #pragma omp distribute. */
7005 if (!is_task_ctx (ctx)
7006 && (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
7007 || gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_FOR))
7008 gimple_seq_add_stmt (ilist, omp_build_barrier (NULL_TREE));
7009 }
7010
7011 /* If max_vf is non-zero, then we can use only a vectorization factor
7012 up to the max_vf we chose. So stick it into the safelen clause. */
7013 if (maybe_ne (sctx.max_vf, 0U))
7014 {
7015 tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
7016 OMP_CLAUSE_SAFELEN);
7017 poly_uint64 safe_len;
7018 if (c == NULL_TREE
7019 || (poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
7020 && maybe_gt (safe_len, sctx.max_vf)))
7021 {
7022 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
7023 OMP_CLAUSE_SAFELEN_EXPR (c) = build_int_cst (integer_type_node,
7024 sctx.max_vf);
7025 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
7026 gimple_omp_for_set_clauses (ctx->stmt, c);
7027 }
7028 }
7029 }
7030
7031 /* Create temporary variables for lastprivate(conditional:) implementation
7032 in context CTX with CLAUSES. */
7033
7034 static void
7035 lower_lastprivate_conditional_clauses (tree *clauses, omp_context *ctx)
7036 {
7037 tree iter_type = NULL_TREE;
7038 tree cond_ptr = NULL_TREE;
7039 tree iter_var = NULL_TREE;
7040 bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7041 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
7042 tree next = *clauses;
7043 for (tree c = *clauses; c; c = OMP_CLAUSE_CHAIN (c))
7044 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7045 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
7046 {
7047 if (is_simd)
7048 {
7049 tree cc = omp_find_clause (next, OMP_CLAUSE__CONDTEMP_);
7050 gcc_assert (cc);
7051 if (iter_type == NULL_TREE)
7052 {
7053 iter_type = TREE_TYPE (OMP_CLAUSE_DECL (cc));
7054 iter_var = create_tmp_var_raw (iter_type);
7055 DECL_CONTEXT (iter_var) = current_function_decl;
7056 DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
7057 DECL_CHAIN (iter_var) = ctx->block_vars;
7058 ctx->block_vars = iter_var;
7059 tree c3
7060 = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
7061 OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
7062 OMP_CLAUSE_DECL (c3) = iter_var;
7063 OMP_CLAUSE_CHAIN (c3) = *clauses;
7064 *clauses = c3;
7065 ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
7066 }
7067 next = OMP_CLAUSE_CHAIN (cc);
7068 tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7069 tree v = lookup_decl (OMP_CLAUSE_DECL (cc), ctx);
7070 ctx->lastprivate_conditional_map->put (o, v);
7071 continue;
7072 }
7073 if (iter_type == NULL)
7074 {
7075 if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR)
7076 {
7077 struct omp_for_data fd;
7078 omp_extract_for_data (as_a <gomp_for *> (ctx->stmt), &fd,
7079 NULL);
7080 iter_type = unsigned_type_for (fd.iter_type);
7081 }
7082 else if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
7083 iter_type = unsigned_type_node;
7084 tree c2 = omp_find_clause (*clauses, OMP_CLAUSE__CONDTEMP_);
7085 if (c2)
7086 {
7087 cond_ptr
7088 = lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c2), ctx);
7089 OMP_CLAUSE_DECL (c2) = cond_ptr;
7090 }
7091 else
7092 {
7093 cond_ptr = create_tmp_var_raw (build_pointer_type (iter_type));
7094 DECL_CONTEXT (cond_ptr) = current_function_decl;
7095 DECL_SEEN_IN_BIND_EXPR_P (cond_ptr) = 1;
7096 DECL_CHAIN (cond_ptr) = ctx->block_vars;
7097 ctx->block_vars = cond_ptr;
7098 c2 = build_omp_clause (UNKNOWN_LOCATION,
7099 OMP_CLAUSE__CONDTEMP_);
7100 OMP_CLAUSE_DECL (c2) = cond_ptr;
7101 OMP_CLAUSE_CHAIN (c2) = *clauses;
7102 *clauses = c2;
7103 }
7104 iter_var = create_tmp_var_raw (iter_type);
7105 DECL_CONTEXT (iter_var) = current_function_decl;
7106 DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
7107 DECL_CHAIN (iter_var) = ctx->block_vars;
7108 ctx->block_vars = iter_var;
7109 tree c3
7110 = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
7111 OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
7112 OMP_CLAUSE_DECL (c3) = iter_var;
7113 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
7114 OMP_CLAUSE_CHAIN (c2) = c3;
7115 ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
7116 }
7117 tree v = create_tmp_var_raw (iter_type);
7118 DECL_CONTEXT (v) = current_function_decl;
7119 DECL_SEEN_IN_BIND_EXPR_P (v) = 1;
7120 DECL_CHAIN (v) = ctx->block_vars;
7121 ctx->block_vars = v;
7122 tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7123 ctx->lastprivate_conditional_map->put (o, v);
7124 }
7125 }
7126
7127
7128 /* Generate code to implement the LASTPRIVATE clauses. This is used for
7129 both parallel and workshare constructs. PREDICATE may be NULL if it's
7130 always true. BODY_P is the sequence to insert early initialization
7131 if needed, STMT_LIST is where the non-conditional lastprivate handling
7132 goes into and CSTMT_LIST is a sequence that needs to be run in a critical
7133 section. */
7134
7135 static void
7136 lower_lastprivate_clauses (tree clauses, tree predicate, gimple_seq *body_p,
7137 gimple_seq *stmt_list, gimple_seq *cstmt_list,
7138 omp_context *ctx)
7139 {
7140 tree x, c, label = NULL, orig_clauses = clauses;
7141 bool par_clauses = false;
7142 tree simduid = NULL, lastlane = NULL, simtcond = NULL, simtlast = NULL;
7143 unsigned HOST_WIDE_INT conditional_off = 0;
7144 gimple_seq post_stmt_list = NULL;
7145
7146 /* Early exit if there are no lastprivate or linear clauses. */
7147 for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
7148 if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LASTPRIVATE
7149 || (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LINEAR
7150 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (clauses)))
7151 break;
7152 if (clauses == NULL)
7153 {
7154 /* If this was a workshare clause, see if it had been combined
7155 with its parallel. In that case, look for the clauses on the
7156 parallel statement itself. */
7157 if (is_parallel_ctx (ctx))
7158 return;
7159
7160 ctx = ctx->outer;
7161 if (ctx == NULL || !is_parallel_ctx (ctx))
7162 return;
7163
7164 clauses = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
7165 OMP_CLAUSE_LASTPRIVATE);
7166 if (clauses == NULL)
7167 return;
7168 par_clauses = true;
7169 }
7170
7171 bool maybe_simt = false;
7172 if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7173 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
7174 {
7175 maybe_simt = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMT_);
7176 simduid = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMDUID_);
7177 if (simduid)
7178 simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
7179 }
7180
7181 if (predicate)
7182 {
7183 gcond *stmt;
7184 tree label_true, arm1, arm2;
7185 enum tree_code pred_code = TREE_CODE (predicate);
7186
7187 label = create_artificial_label (UNKNOWN_LOCATION);
7188 label_true = create_artificial_label (UNKNOWN_LOCATION);
7189 if (TREE_CODE_CLASS (pred_code) == tcc_comparison)
7190 {
7191 arm1 = TREE_OPERAND (predicate, 0);
7192 arm2 = TREE_OPERAND (predicate, 1);
7193 gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
7194 gimplify_expr (&arm2, stmt_list, NULL, is_gimple_val, fb_rvalue);
7195 }
7196 else
7197 {
7198 arm1 = predicate;
7199 gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
7200 arm2 = boolean_false_node;
7201 pred_code = NE_EXPR;
7202 }
7203 if (maybe_simt)
7204 {
7205 c = build2 (pred_code, boolean_type_node, arm1, arm2);
7206 c = fold_convert (integer_type_node, c);
7207 simtcond = create_tmp_var (integer_type_node);
7208 gimplify_assign (simtcond, c, stmt_list);
7209 gcall *g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY,
7210 1, simtcond);
7211 c = create_tmp_var (integer_type_node);
7212 gimple_call_set_lhs (g, c);
7213 gimple_seq_add_stmt (stmt_list, g);
7214 stmt = gimple_build_cond (NE_EXPR, c, integer_zero_node,
7215 label_true, label);
7216 }
7217 else
7218 stmt = gimple_build_cond (pred_code, arm1, arm2, label_true, label);
7219 gimple_seq_add_stmt (stmt_list, stmt);
7220 gimple_seq_add_stmt (stmt_list, gimple_build_label (label_true));
7221 }
7222
7223 tree cond_ptr = NULL_TREE;
7224 for (c = clauses; c ;)
7225 {
7226 tree var, new_var;
7227 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7228 gimple_seq *this_stmt_list = stmt_list;
7229 tree lab2 = NULL_TREE;
7230
7231 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7232 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
7233 && ctx->lastprivate_conditional_map
7234 && !ctx->combined_into_simd_safelen1)
7235 {
7236 gcc_assert (body_p);
7237 if (simduid)
7238 goto next;
7239 if (cond_ptr == NULL_TREE)
7240 {
7241 cond_ptr = omp_find_clause (orig_clauses, OMP_CLAUSE__CONDTEMP_);
7242 cond_ptr = OMP_CLAUSE_DECL (cond_ptr);
7243 }
7244 tree type = TREE_TYPE (TREE_TYPE (cond_ptr));
7245 tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7246 tree v = *ctx->lastprivate_conditional_map->get (o);
7247 gimplify_assign (v, build_zero_cst (type), body_p);
7248 this_stmt_list = cstmt_list;
7249 tree mem;
7250 if (POINTER_TYPE_P (TREE_TYPE (cond_ptr)))
7251 {
7252 mem = build2 (MEM_REF, type, cond_ptr,
7253 build_int_cst (TREE_TYPE (cond_ptr),
7254 conditional_off));
7255 conditional_off += tree_to_uhwi (TYPE_SIZE_UNIT (type));
7256 }
7257 else
7258 mem = build4 (ARRAY_REF, type, cond_ptr,
7259 size_int (conditional_off++), NULL_TREE, NULL_TREE);
7260 tree mem2 = copy_node (mem);
7261 gimple_seq seq = NULL;
7262 mem = force_gimple_operand (mem, &seq, true, NULL_TREE);
7263 gimple_seq_add_seq (this_stmt_list, seq);
7264 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
7265 lab2 = create_artificial_label (UNKNOWN_LOCATION);
7266 gimple *g = gimple_build_cond (GT_EXPR, v, mem, lab1, lab2);
7267 gimple_seq_add_stmt (this_stmt_list, g);
7268 gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab1));
7269 gimplify_assign (mem2, v, this_stmt_list);
7270 }
7271 else if (predicate
7272 && ctx->combined_into_simd_safelen1
7273 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7274 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
7275 && ctx->lastprivate_conditional_map)
7276 this_stmt_list = &post_stmt_list;
7277
7278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7279 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7280 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7281 {
7282 var = OMP_CLAUSE_DECL (c);
7283 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7284 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7285 && is_taskloop_ctx (ctx))
7286 {
7287 gcc_checking_assert (ctx->outer && is_task_ctx (ctx->outer));
7288 new_var = lookup_decl (var, ctx->outer);
7289 }
7290 else
7291 {
7292 new_var = lookup_decl (var, ctx);
7293 /* Avoid uninitialized warnings for lastprivate and
7294 for linear iterators. */
7295 if (predicate
7296 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7297 || OMP_CLAUSE_LINEAR_NO_COPYIN (c)))
7298 suppress_warning (new_var, OPT_Wuninitialized);
7299 }
7300
7301 if (!maybe_simt && simduid && DECL_HAS_VALUE_EXPR_P (new_var))
7302 {
7303 tree val = DECL_VALUE_EXPR (new_var);
7304 if (TREE_CODE (val) == ARRAY_REF
7305 && VAR_P (TREE_OPERAND (val, 0))
7306 && lookup_attribute ("omp simd array",
7307 DECL_ATTRIBUTES (TREE_OPERAND (val,
7308 0))))
7309 {
7310 if (lastlane == NULL)
7311 {
7312 lastlane = create_tmp_var (unsigned_type_node);
7313 gcall *g
7314 = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
7315 2, simduid,
7316 TREE_OPERAND (val, 1));
7317 gimple_call_set_lhs (g, lastlane);
7318 gimple_seq_add_stmt (this_stmt_list, g);
7319 }
7320 new_var = build4 (ARRAY_REF, TREE_TYPE (val),
7321 TREE_OPERAND (val, 0), lastlane,
7322 NULL_TREE, NULL_TREE);
7323 TREE_THIS_NOTRAP (new_var) = 1;
7324 }
7325 }
7326 else if (maybe_simt)
7327 {
7328 tree val = (DECL_HAS_VALUE_EXPR_P (new_var)
7329 ? DECL_VALUE_EXPR (new_var)
7330 : new_var);
7331 if (simtlast == NULL)
7332 {
7333 simtlast = create_tmp_var (unsigned_type_node);
7334 gcall *g = gimple_build_call_internal
7335 (IFN_GOMP_SIMT_LAST_LANE, 1, simtcond);
7336 gimple_call_set_lhs (g, simtlast);
7337 gimple_seq_add_stmt (this_stmt_list, g);
7338 }
7339 x = build_call_expr_internal_loc
7340 (UNKNOWN_LOCATION, IFN_GOMP_SIMT_XCHG_IDX,
7341 TREE_TYPE (val), 2, val, simtlast);
7342 new_var = unshare_expr (new_var);
7343 gimplify_assign (new_var, x, this_stmt_list);
7344 new_var = unshare_expr (new_var);
7345 }
7346
7347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7348 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
7349 {
7350 lower_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
7351 gimple_seq_add_seq (this_stmt_list,
7352 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7353 OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) = NULL;
7354 }
7355 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7356 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
7357 {
7358 lower_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
7359 gimple_seq_add_seq (this_stmt_list,
7360 OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
7361 OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) = NULL;
7362 }
7363
7364 x = NULL_TREE;
7365 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7366 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
7367 && is_taskloop_ctx (ctx))
7368 {
7369 tree ovar = maybe_lookup_decl_in_outer_ctx (var,
7370 ctx->outer->outer);
7371 if (is_global_var (ovar))
7372 x = ovar;
7373 }
7374 if (!x)
7375 x = build_outer_var_ref (var, ctx, OMP_CLAUSE_LASTPRIVATE);
7376 if (omp_privatize_by_reference (var))
7377 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7378 x = lang_hooks.decls.omp_clause_assign_op (c, x, new_var);
7379 gimplify_and_add (x, this_stmt_list);
7380
7381 if (lab2)
7382 gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab2));
7383 }
7384
7385 next:
7386 c = OMP_CLAUSE_CHAIN (c);
7387 if (c == NULL && !par_clauses)
7388 {
7389 /* If this was a workshare clause, see if it had been combined
7390 with its parallel. In that case, continue looking for the
7391 clauses also on the parallel statement itself. */
7392 if (is_parallel_ctx (ctx))
7393 break;
7394
7395 ctx = ctx->outer;
7396 if (ctx == NULL || !is_parallel_ctx (ctx))
7397 break;
7398
7399 c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
7400 OMP_CLAUSE_LASTPRIVATE);
7401 par_clauses = true;
7402 }
7403 }
7404
7405 if (label)
7406 gimple_seq_add_stmt (stmt_list, gimple_build_label (label));
7407 gimple_seq_add_seq (stmt_list, post_stmt_list);
7408 }
7409
7410 /* Lower the OpenACC reductions of CLAUSES for compute axis LEVEL
7411 (which might be a placeholder). INNER is true if this is an inner
7412 axis of a multi-axis loop. FORK and JOIN are (optional) fork and
7413 join markers. Generate the before-loop forking sequence in
7414 FORK_SEQ and the after-loop joining sequence to JOIN_SEQ. The
7415 general form of these sequences is
7416
7417 GOACC_REDUCTION_SETUP
7418 GOACC_FORK
7419 GOACC_REDUCTION_INIT
7420 ...
7421 GOACC_REDUCTION_FINI
7422 GOACC_JOIN
7423 GOACC_REDUCTION_TEARDOWN. */
7424
7425 static void
7426 lower_oacc_reductions (location_t loc, tree clauses, tree level, bool inner,
7427 gcall *fork, gcall *private_marker, gcall *join,
7428 gimple_seq *fork_seq, gimple_seq *join_seq,
7429 omp_context *ctx)
7430 {
7431 gimple_seq before_fork = NULL;
7432 gimple_seq after_fork = NULL;
7433 gimple_seq before_join = NULL;
7434 gimple_seq after_join = NULL;
7435 tree init_code = NULL_TREE, fini_code = NULL_TREE,
7436 setup_code = NULL_TREE, teardown_code = NULL_TREE;
7437 unsigned offset = 0;
7438
7439 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
7440 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7441 {
7442 /* No 'reduction' clauses on OpenACC 'kernels'. */
7443 gcc_checking_assert (!is_oacc_kernels (ctx));
7444 /* Likewise, on OpenACC 'kernels' decomposed parts. */
7445 gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
7446
7447 tree orig = OMP_CLAUSE_DECL (c);
7448 tree var = maybe_lookup_decl (orig, ctx);
7449 tree ref_to_res = NULL_TREE;
7450 tree incoming, outgoing, v1, v2, v3;
7451 bool is_private = false;
7452
7453 enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
7454 if (rcode == MINUS_EXPR)
7455 rcode = PLUS_EXPR;
7456 else if (rcode == TRUTH_ANDIF_EXPR)
7457 rcode = BIT_AND_EXPR;
7458 else if (rcode == TRUTH_ORIF_EXPR)
7459 rcode = BIT_IOR_EXPR;
7460 tree op = build_int_cst (unsigned_type_node, rcode);
7461
7462 if (!var)
7463 var = orig;
7464
7465 incoming = outgoing = var;
7466
7467 if (!inner)
7468 {
7469 /* See if an outer construct also reduces this variable. */
7470 omp_context *outer = ctx;
7471
7472 while (omp_context *probe = outer->outer)
7473 {
7474 enum gimple_code type = gimple_code (probe->stmt);
7475 tree cls;
7476
7477 switch (type)
7478 {
7479 case GIMPLE_OMP_FOR:
7480 cls = gimple_omp_for_clauses (probe->stmt);
7481 break;
7482
7483 case GIMPLE_OMP_TARGET:
7484 /* No 'reduction' clauses inside OpenACC 'kernels'
7485 regions. */
7486 gcc_checking_assert (!is_oacc_kernels (probe));
7487
7488 if (!is_gimple_omp_offloaded (probe->stmt))
7489 goto do_lookup;
7490
7491 cls = gimple_omp_target_clauses (probe->stmt);
7492 break;
7493
7494 default:
7495 goto do_lookup;
7496 }
7497
7498 outer = probe;
7499 for (; cls; cls = OMP_CLAUSE_CHAIN (cls))
7500 if (OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_REDUCTION
7501 && orig == OMP_CLAUSE_DECL (cls))
7502 {
7503 incoming = outgoing = lookup_decl (orig, probe);
7504 goto has_outer_reduction;
7505 }
7506 else if ((OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_FIRSTPRIVATE
7507 || OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_PRIVATE)
7508 && orig == OMP_CLAUSE_DECL (cls))
7509 {
7510 is_private = true;
7511 goto do_lookup;
7512 }
7513 }
7514
7515 do_lookup:
7516 /* This is the outermost construct with this reduction,
7517 see if there's a mapping for it. */
7518 if (gimple_code (outer->stmt) == GIMPLE_OMP_TARGET
7519 && maybe_lookup_field (orig, outer) && !is_private)
7520 {
7521 ref_to_res = build_receiver_ref (orig, false, outer);
7522 if (omp_privatize_by_reference (orig))
7523 ref_to_res = build_simple_mem_ref (ref_to_res);
7524
7525 tree type = TREE_TYPE (var);
7526 if (POINTER_TYPE_P (type))
7527 type = TREE_TYPE (type);
7528
7529 outgoing = var;
7530 incoming = omp_reduction_init_op (loc, rcode, type);
7531 }
7532 else
7533 {
7534 /* Try to look at enclosing contexts for reduction var,
7535 use original if no mapping found. */
7536 tree t = NULL_TREE;
7537 omp_context *c = ctx->outer;
7538 while (c && !t)
7539 {
7540 t = maybe_lookup_decl (orig, c);
7541 c = c->outer;
7542 }
7543 incoming = outgoing = (t ? t : orig);
7544 }
7545
7546 has_outer_reduction:;
7547 }
7548
7549 if (!ref_to_res)
7550 ref_to_res = integer_zero_node;
7551
7552 if (omp_privatize_by_reference (orig))
7553 {
7554 tree type = TREE_TYPE (var);
7555 const char *id = IDENTIFIER_POINTER (DECL_NAME (var));
7556
7557 if (!inner)
7558 {
7559 tree x = create_tmp_var (TREE_TYPE (type), id);
7560 gimplify_assign (var, build_fold_addr_expr (x), fork_seq);
7561 }
7562
7563 v1 = create_tmp_var (type, id);
7564 v2 = create_tmp_var (type, id);
7565 v3 = create_tmp_var (type, id);
7566
7567 gimplify_assign (v1, var, fork_seq);
7568 gimplify_assign (v2, var, fork_seq);
7569 gimplify_assign (v3, var, fork_seq);
7570
7571 var = build_simple_mem_ref (var);
7572 v1 = build_simple_mem_ref (v1);
7573 v2 = build_simple_mem_ref (v2);
7574 v3 = build_simple_mem_ref (v3);
7575 outgoing = build_simple_mem_ref (outgoing);
7576
7577 if (!TREE_CONSTANT (incoming))
7578 incoming = build_simple_mem_ref (incoming);
7579 }
7580 else
7581 /* Note that 'var' might be a mem ref. */
7582 v1 = v2 = v3 = var;
7583
7584 /* Determine position in reduction buffer, which may be used
7585 by target. The parser has ensured that this is not a
7586 variable-sized type. */
7587 fixed_size_mode mode
7588 = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (var)));
7589 unsigned align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
7590 offset = (offset + align - 1) & ~(align - 1);
7591 tree off = build_int_cst (sizetype, offset);
7592 offset += GET_MODE_SIZE (mode);
7593
7594 if (!init_code)
7595 {
7596 init_code = build_int_cst (integer_type_node,
7597 IFN_GOACC_REDUCTION_INIT);
7598 fini_code = build_int_cst (integer_type_node,
7599 IFN_GOACC_REDUCTION_FINI);
7600 setup_code = build_int_cst (integer_type_node,
7601 IFN_GOACC_REDUCTION_SETUP);
7602 teardown_code = build_int_cst (integer_type_node,
7603 IFN_GOACC_REDUCTION_TEARDOWN);
7604 }
7605
7606 tree setup_call
7607 = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7608 TREE_TYPE (var), 6, setup_code,
7609 unshare_expr (ref_to_res),
7610 unshare_expr (incoming),
7611 level, op, off);
7612 tree init_call
7613 = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7614 TREE_TYPE (var), 6, init_code,
7615 unshare_expr (ref_to_res),
7616 unshare_expr (v1), level, op, off);
7617 tree fini_call
7618 = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7619 TREE_TYPE (var), 6, fini_code,
7620 unshare_expr (ref_to_res),
7621 unshare_expr (v2), level, op, off);
7622 tree teardown_call
7623 = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7624 TREE_TYPE (var), 6, teardown_code,
7625 ref_to_res, unshare_expr (v3),
7626 level, op, off);
7627
7628 gimplify_assign (unshare_expr (v1), setup_call, &before_fork);
7629 gimplify_assign (unshare_expr (v2), init_call, &after_fork);
7630 gimplify_assign (unshare_expr (v3), fini_call, &before_join);
7631 gimplify_assign (unshare_expr (outgoing), teardown_call, &after_join);
7632 }
7633
7634 /* Now stitch things together. */
7635 gimple_seq_add_seq (fork_seq, before_fork);
7636 if (private_marker)
7637 gimple_seq_add_stmt (fork_seq, private_marker);
7638 if (fork)
7639 gimple_seq_add_stmt (fork_seq, fork);
7640 gimple_seq_add_seq (fork_seq, after_fork);
7641
7642 gimple_seq_add_seq (join_seq, before_join);
7643 if (join)
7644 gimple_seq_add_stmt (join_seq, join);
7645 gimple_seq_add_seq (join_seq, after_join);
7646 }
7647
7648 /* Generate code to implement the REDUCTION clauses, append it
7649 to STMT_SEQP. CLIST if non-NULL is a pointer to a sequence
7650 that should be emitted also inside of the critical section,
7651 in that case clear *CLIST afterwards, otherwise leave it as is
7652 and let the caller emit it itself. */
7653
7654 static void
7655 lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
7656 gimple_seq *clist, omp_context *ctx)
7657 {
7658 gimple_seq sub_seq = NULL;
7659 gimple *stmt;
7660 tree x, c;
7661 int count = 0;
7662
7663 /* OpenACC loop reductions are handled elsewhere. */
7664 if (is_gimple_omp_oacc (ctx->stmt))
7665 return;
7666
7667 /* SIMD reductions are handled in lower_rec_input_clauses. */
7668 if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7669 && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
7670 return;
7671
7672 /* inscan reductions are handled elsewhere. */
7673 if (ctx->scan_inclusive || ctx->scan_exclusive)
7674 return;
7675
7676 /* First see if there is exactly one reduction clause. Use OMP_ATOMIC
7677 update in that case, otherwise use a lock. */
7678 for (c = clauses; c && count < 2; c = OMP_CLAUSE_CHAIN (c))
7679 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7680 && !OMP_CLAUSE_REDUCTION_TASK (c))
7681 {
7682 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
7683 || TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
7684 {
7685 /* Never use OMP_ATOMIC for array reductions or UDRs. */
7686 count = -1;
7687 break;
7688 }
7689 count++;
7690 }
7691
7692 if (count == 0)
7693 return;
7694
7695 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7696 {
7697 tree var, ref, new_var, orig_var;
7698 enum tree_code code;
7699 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7700
7701 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
7702 || OMP_CLAUSE_REDUCTION_TASK (c))
7703 continue;
7704
7705 enum omp_clause_code ccode = OMP_CLAUSE_REDUCTION;
7706 orig_var = var = OMP_CLAUSE_DECL (c);
7707 if (TREE_CODE (var) == MEM_REF)
7708 {
7709 var = TREE_OPERAND (var, 0);
7710 if (TREE_CODE (var) == POINTER_PLUS_EXPR)
7711 var = TREE_OPERAND (var, 0);
7712 if (TREE_CODE (var) == ADDR_EXPR)
7713 var = TREE_OPERAND (var, 0);
7714 else
7715 {
7716 /* If this is a pointer or referenced based array
7717 section, the var could be private in the outer
7718 context e.g. on orphaned loop construct. Pretend this
7719 is private variable's outer reference. */
7720 ccode = OMP_CLAUSE_PRIVATE;
7721 if (INDIRECT_REF_P (var))
7722 var = TREE_OPERAND (var, 0);
7723 }
7724 orig_var = var;
7725 if (is_variable_sized (var))
7726 {
7727 gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
7728 var = DECL_VALUE_EXPR (var);
7729 gcc_assert (INDIRECT_REF_P (var));
7730 var = TREE_OPERAND (var, 0);
7731 gcc_assert (DECL_P (var));
7732 }
7733 }
7734 new_var = lookup_decl (var, ctx);
7735 if (var == OMP_CLAUSE_DECL (c)
7736 && omp_privatize_by_reference (var))
7737 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7738 ref = build_outer_var_ref (var, ctx, ccode);
7739 code = OMP_CLAUSE_REDUCTION_CODE (c);
7740
7741 /* reduction(-:var) sums up the partial results, so it acts
7742 identically to reduction(+:var). */
7743 if (code == MINUS_EXPR)
7744 code = PLUS_EXPR;
7745
7746 bool is_truth_op = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
7747 if (count == 1)
7748 {
7749 tree addr = build_fold_addr_expr_loc (clause_loc, ref);
7750
7751 addr = save_expr (addr);
7752 ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (addr)), addr);
7753 tree new_var2 = new_var;
7754 tree ref2 = ref;
7755 if (is_truth_op)
7756 {
7757 tree zero = build_zero_cst (TREE_TYPE (new_var));
7758 new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
7759 boolean_type_node, new_var, zero);
7760 ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
7761 ref, zero);
7762 }
7763 x = fold_build2_loc (clause_loc, code, TREE_TYPE (new_var2), ref2,
7764 new_var2);
7765 if (is_truth_op)
7766 x = fold_convert (TREE_TYPE (new_var), x);
7767 x = build2 (OMP_ATOMIC, void_type_node, addr, x);
7768 OMP_ATOMIC_MEMORY_ORDER (x) = OMP_MEMORY_ORDER_RELAXED;
7769 gimplify_and_add (x, stmt_seqp);
7770 return;
7771 }
7772 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
7773 {
7774 tree d = OMP_CLAUSE_DECL (c);
7775 tree type = TREE_TYPE (d);
7776 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7777 tree i = create_tmp_var (TREE_TYPE (v));
7778 tree ptype = build_pointer_type (TREE_TYPE (type));
7779 tree bias = TREE_OPERAND (d, 1);
7780 d = TREE_OPERAND (d, 0);
7781 if (TREE_CODE (d) == POINTER_PLUS_EXPR)
7782 {
7783 tree b = TREE_OPERAND (d, 1);
7784 b = maybe_lookup_decl (b, ctx);
7785 if (b == NULL)
7786 {
7787 b = TREE_OPERAND (d, 1);
7788 b = maybe_lookup_decl_in_outer_ctx (b, ctx);
7789 }
7790 if (integer_zerop (bias))
7791 bias = b;
7792 else
7793 {
7794 bias = fold_convert_loc (clause_loc, TREE_TYPE (b), bias);
7795 bias = fold_build2_loc (clause_loc, PLUS_EXPR,
7796 TREE_TYPE (b), b, bias);
7797 }
7798 d = TREE_OPERAND (d, 0);
7799 }
7800 /* For ref build_outer_var_ref already performs this, so
7801 only new_var needs a dereference. */
7802 if (INDIRECT_REF_P (d))
7803 {
7804 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7805 gcc_assert (omp_privatize_by_reference (var)
7806 && var == orig_var);
7807 }
7808 else if (TREE_CODE (d) == ADDR_EXPR)
7809 {
7810 if (orig_var == var)
7811 {
7812 new_var = build_fold_addr_expr (new_var);
7813 ref = build_fold_addr_expr (ref);
7814 }
7815 }
7816 else
7817 {
7818 gcc_assert (orig_var == var);
7819 if (omp_privatize_by_reference (var))
7820 ref = build_fold_addr_expr (ref);
7821 }
7822 if (DECL_P (v))
7823 {
7824 tree t = maybe_lookup_decl (v, ctx);
7825 if (t)
7826 v = t;
7827 else
7828 v = maybe_lookup_decl_in_outer_ctx (v, ctx);
7829 gimplify_expr (&v, stmt_seqp, NULL, is_gimple_val, fb_rvalue);
7830 }
7831 if (!integer_zerop (bias))
7832 {
7833 bias = fold_convert_loc (clause_loc, sizetype, bias);
7834 new_var = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
7835 TREE_TYPE (new_var), new_var,
7836 unshare_expr (bias));
7837 ref = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
7838 TREE_TYPE (ref), ref, bias);
7839 }
7840 new_var = fold_convert_loc (clause_loc, ptype, new_var);
7841 ref = fold_convert_loc (clause_loc, ptype, ref);
7842 tree m = create_tmp_var (ptype);
7843 gimplify_assign (m, new_var, stmt_seqp);
7844 new_var = m;
7845 m = create_tmp_var (ptype);
7846 gimplify_assign (m, ref, stmt_seqp);
7847 ref = m;
7848 gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), stmt_seqp);
7849 tree body = create_artificial_label (UNKNOWN_LOCATION);
7850 tree end = create_artificial_label (UNKNOWN_LOCATION);
7851 gimple_seq_add_stmt (&sub_seq, gimple_build_label (body));
7852 tree priv = build_simple_mem_ref_loc (clause_loc, new_var);
7853 tree out = build_simple_mem_ref_loc (clause_loc, ref);
7854 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7855 {
7856 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
7857 tree decl_placeholder
7858 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
7859 SET_DECL_VALUE_EXPR (placeholder, out);
7860 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
7861 SET_DECL_VALUE_EXPR (decl_placeholder, priv);
7862 DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
7863 lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
7864 gimple_seq_add_seq (&sub_seq,
7865 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7866 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7867 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
7868 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
7869 }
7870 else
7871 {
7872 tree out2 = out;
7873 tree priv2 = priv;
7874 if (is_truth_op)
7875 {
7876 tree zero = build_zero_cst (TREE_TYPE (out));
7877 out2 = fold_build2_loc (clause_loc, NE_EXPR,
7878 boolean_type_node, out, zero);
7879 priv2 = fold_build2_loc (clause_loc, NE_EXPR,
7880 boolean_type_node, priv, zero);
7881 }
7882 x = build2 (code, TREE_TYPE (out2), out2, priv2);
7883 if (is_truth_op)
7884 x = fold_convert (TREE_TYPE (out), x);
7885 out = unshare_expr (out);
7886 gimplify_assign (out, x, &sub_seq);
7887 }
7888 gimple *g = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
7889 TYPE_SIZE_UNIT (TREE_TYPE (type)));
7890 gimple_seq_add_stmt (&sub_seq, g);
7891 g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
7892 TYPE_SIZE_UNIT (TREE_TYPE (type)));
7893 gimple_seq_add_stmt (&sub_seq, g);
7894 g = gimple_build_assign (i, PLUS_EXPR, i,
7895 build_int_cst (TREE_TYPE (i), 1));
7896 gimple_seq_add_stmt (&sub_seq, g);
7897 g = gimple_build_cond (LE_EXPR, i, v, body, end);
7898 gimple_seq_add_stmt (&sub_seq, g);
7899 gimple_seq_add_stmt (&sub_seq, gimple_build_label (end));
7900 }
7901 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7902 {
7903 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
7904
7905 if (omp_privatize_by_reference (var)
7906 && !useless_type_conversion_p (TREE_TYPE (placeholder),
7907 TREE_TYPE (ref)))
7908 ref = build_fold_addr_expr_loc (clause_loc, ref);
7909 SET_DECL_VALUE_EXPR (placeholder, ref);
7910 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
7911 lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
7912 gimple_seq_add_seq (&sub_seq, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7913 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7914 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
7915 }
7916 else
7917 {
7918 tree new_var2 = new_var;
7919 tree ref2 = ref;
7920 if (is_truth_op)
7921 {
7922 tree zero = build_zero_cst (TREE_TYPE (new_var));
7923 new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
7924 boolean_type_node, new_var, zero);
7925 ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
7926 ref, zero);
7927 }
7928 x = build2 (code, TREE_TYPE (ref), ref2, new_var2);
7929 if (is_truth_op)
7930 x = fold_convert (TREE_TYPE (new_var), x);
7931 ref = build_outer_var_ref (var, ctx);
7932 gimplify_assign (ref, x, &sub_seq);
7933 }
7934 }
7935
7936 stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START),
7937 0);
7938 gimple_seq_add_stmt (stmt_seqp, stmt);
7939
7940 gimple_seq_add_seq (stmt_seqp, sub_seq);
7941
7942 if (clist)
7943 {
7944 gimple_seq_add_seq (stmt_seqp, *clist);
7945 *clist = NULL;
7946 }
7947
7948 stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END),
7949 0);
7950 gimple_seq_add_stmt (stmt_seqp, stmt);
7951 }
7952
7953
7954 /* Generate code to implement the COPYPRIVATE clauses. */
7955
7956 static void
7957 lower_copyprivate_clauses (tree clauses, gimple_seq *slist, gimple_seq *rlist,
7958 omp_context *ctx)
7959 {
7960 tree c;
7961
7962 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7963 {
7964 tree var, new_var, ref, x;
7965 bool by_ref;
7966 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7967
7968 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYPRIVATE)
7969 continue;
7970
7971 var = OMP_CLAUSE_DECL (c);
7972 by_ref = use_pointer_for_field (var, NULL);
7973
7974 ref = build_sender_ref (var, ctx);
7975 x = new_var = lookup_decl_in_outer_ctx (var, ctx);
7976 if (by_ref)
7977 {
7978 x = build_fold_addr_expr_loc (clause_loc, new_var);
7979 x = fold_convert_loc (clause_loc, TREE_TYPE (ref), x);
7980 }
7981 gimplify_assign (ref, x, slist);
7982
7983 ref = build_receiver_ref (var, false, ctx);
7984 if (by_ref)
7985 {
7986 ref = fold_convert_loc (clause_loc,
7987 build_pointer_type (TREE_TYPE (new_var)),
7988 ref);
7989 ref = build_fold_indirect_ref_loc (clause_loc, ref);
7990 }
7991 if (omp_privatize_by_reference (var))
7992 {
7993 ref = fold_convert_loc (clause_loc, TREE_TYPE (new_var), ref);
7994 ref = build_simple_mem_ref_loc (clause_loc, ref);
7995 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7996 }
7997 x = lang_hooks.decls.omp_clause_assign_op (c, new_var, ref);
7998 gimplify_and_add (x, rlist);
7999 }
8000 }
8001
8002
8003 /* Generate code to implement the clauses, FIRSTPRIVATE, COPYIN, LASTPRIVATE,
8004 and REDUCTION from the sender (aka parent) side. */
8005
8006 static void
8007 lower_send_clauses (tree clauses, gimple_seq *ilist, gimple_seq *olist,
8008 omp_context *ctx)
8009 {
8010 tree c, t;
8011 int ignored_looptemp = 0;
8012 bool is_taskloop = false;
8013
8014 /* For taskloop, ignore first two _looptemp_ clauses, those are initialized
8015 by GOMP_taskloop. */
8016 if (is_task_ctx (ctx) && gimple_omp_task_taskloop_p (ctx->stmt))
8017 {
8018 ignored_looptemp = 2;
8019 is_taskloop = true;
8020 }
8021
8022 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8023 {
8024 tree val, ref, x, var;
8025 bool by_ref, do_in = false, do_out = false;
8026 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
8027
8028 switch (OMP_CLAUSE_CODE (c))
8029 {
8030 case OMP_CLAUSE_PRIVATE:
8031 if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
8032 break;
8033 continue;
8034 case OMP_CLAUSE_FIRSTPRIVATE:
8035 case OMP_CLAUSE_COPYIN:
8036 case OMP_CLAUSE_LASTPRIVATE:
8037 case OMP_CLAUSE_IN_REDUCTION:
8038 case OMP_CLAUSE__REDUCTEMP_:
8039 break;
8040 case OMP_CLAUSE_REDUCTION:
8041 if (is_task_ctx (ctx) || OMP_CLAUSE_REDUCTION_TASK (c))
8042 continue;
8043 break;
8044 case OMP_CLAUSE_SHARED:
8045 if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
8046 break;
8047 continue;
8048 case OMP_CLAUSE__LOOPTEMP_:
8049 if (ignored_looptemp)
8050 {
8051 ignored_looptemp--;
8052 continue;
8053 }
8054 break;
8055 default:
8056 continue;
8057 }
8058
8059 val = OMP_CLAUSE_DECL (c);
8060 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8061 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
8062 && TREE_CODE (val) == MEM_REF)
8063 {
8064 val = TREE_OPERAND (val, 0);
8065 if (TREE_CODE (val) == POINTER_PLUS_EXPR)
8066 val = TREE_OPERAND (val, 0);
8067 if (INDIRECT_REF_P (val)
8068 || TREE_CODE (val) == ADDR_EXPR)
8069 val = TREE_OPERAND (val, 0);
8070 if (is_variable_sized (val))
8071 continue;
8072 }
8073
8074 /* For OMP_CLAUSE_SHARED_FIRSTPRIVATE, look beyond the
8075 outer taskloop region. */
8076 omp_context *ctx_for_o = ctx;
8077 if (is_taskloop
8078 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8079 && OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
8080 ctx_for_o = ctx->outer;
8081
8082 var = lookup_decl_in_outer_ctx (val, ctx_for_o);
8083
8084 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYIN
8085 && is_global_var (var)
8086 && (val == OMP_CLAUSE_DECL (c)
8087 || !is_task_ctx (ctx)
8088 || (TREE_CODE (TREE_TYPE (val)) != POINTER_TYPE
8089 && (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE
8090 || (TREE_CODE (TREE_TYPE (TREE_TYPE (val)))
8091 != POINTER_TYPE)))))
8092 continue;
8093
8094 t = omp_member_access_dummy_var (var);
8095 if (t)
8096 {
8097 var = DECL_VALUE_EXPR (var);
8098 tree o = maybe_lookup_decl_in_outer_ctx (t, ctx_for_o);
8099 if (o != t)
8100 var = unshare_and_remap (var, t, o);
8101 else
8102 var = unshare_expr (var);
8103 }
8104
8105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED)
8106 {
8107 /* Handle taskloop firstprivate/lastprivate, where the
8108 lastprivate on GIMPLE_OMP_TASK is represented as
8109 OMP_CLAUSE_SHARED_FIRSTPRIVATE. */
8110 tree f = lookup_sfield ((splay_tree_key) &DECL_UID (val), ctx);
8111 x = omp_build_component_ref (ctx->sender_decl, f);
8112 if (use_pointer_for_field (val, ctx))
8113 var = build_fold_addr_expr (var);
8114 gimplify_assign (x, var, ilist);
8115 DECL_ABSTRACT_ORIGIN (f) = NULL;
8116 continue;
8117 }
8118
8119 if (((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8120 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION)
8121 || val == OMP_CLAUSE_DECL (c))
8122 && is_variable_sized (val))
8123 continue;
8124 by_ref = use_pointer_for_field (val, NULL);
8125
8126 switch (OMP_CLAUSE_CODE (c))
8127 {
8128 case OMP_CLAUSE_FIRSTPRIVATE:
8129 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
8130 && !by_ref
8131 && is_task_ctx (ctx))
8132 suppress_warning (var);
8133 do_in = true;
8134 break;
8135
8136 case OMP_CLAUSE_PRIVATE:
8137 case OMP_CLAUSE_COPYIN:
8138 case OMP_CLAUSE__LOOPTEMP_:
8139 case OMP_CLAUSE__REDUCTEMP_:
8140 do_in = true;
8141 break;
8142
8143 case OMP_CLAUSE_LASTPRIVATE:
8144 if (by_ref || omp_privatize_by_reference (val))
8145 {
8146 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8147 continue;
8148 do_in = true;
8149 }
8150 else
8151 {
8152 do_out = true;
8153 if (lang_hooks.decls.omp_private_outer_ref (val))
8154 do_in = true;
8155 }
8156 break;
8157
8158 case OMP_CLAUSE_REDUCTION:
8159 case OMP_CLAUSE_IN_REDUCTION:
8160 do_in = true;
8161 if (val == OMP_CLAUSE_DECL (c))
8162 {
8163 if (is_task_ctx (ctx))
8164 by_ref = use_pointer_for_field (val, ctx);
8165 else
8166 do_out = !(by_ref || omp_privatize_by_reference (val));
8167 }
8168 else
8169 by_ref = TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE;
8170 break;
8171
8172 default:
8173 gcc_unreachable ();
8174 }
8175
8176 if (do_in)
8177 {
8178 ref = build_sender_ref (val, ctx);
8179 x = by_ref ? build_fold_addr_expr_loc (clause_loc, var) : var;
8180 gimplify_assign (ref, x, ilist);
8181 if (is_task_ctx (ctx))
8182 DECL_ABSTRACT_ORIGIN (TREE_OPERAND (ref, 1)) = NULL;
8183 }
8184
8185 if (do_out)
8186 {
8187 ref = build_sender_ref (val, ctx);
8188 gimplify_assign (var, ref, olist);
8189 }
8190 }
8191 }
8192
8193 /* Generate code to implement SHARED from the sender (aka parent)
8194 side. This is trickier, since GIMPLE_OMP_PARALLEL_CLAUSES doesn't
8195 list things that got automatically shared. */
8196
8197 static void
8198 lower_send_shared_vars (gimple_seq *ilist, gimple_seq *olist, omp_context *ctx)
8199 {
8200 tree var, ovar, nvar, t, f, x, record_type;
8201
8202 if (ctx->record_type == NULL)
8203 return;
8204
8205 record_type = ctx->srecord_type ? ctx->srecord_type : ctx->record_type;
8206 for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
8207 {
8208 ovar = DECL_ABSTRACT_ORIGIN (f);
8209 if (!ovar || TREE_CODE (ovar) == FIELD_DECL)
8210 continue;
8211
8212 nvar = maybe_lookup_decl (ovar, ctx);
8213 if (!nvar
8214 || !DECL_HAS_VALUE_EXPR_P (nvar)
8215 || (ctx->allocate_map
8216 && ctx->allocate_map->get (ovar)))
8217 continue;
8218
8219 /* If CTX is a nested parallel directive. Find the immediately
8220 enclosing parallel or workshare construct that contains a
8221 mapping for OVAR. */
8222 var = lookup_decl_in_outer_ctx (ovar, ctx);
8223
8224 t = omp_member_access_dummy_var (var);
8225 if (t)
8226 {
8227 var = DECL_VALUE_EXPR (var);
8228 tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
8229 if (o != t)
8230 var = unshare_and_remap (var, t, o);
8231 else
8232 var = unshare_expr (var);
8233 }
8234
8235 if (use_pointer_for_field (ovar, ctx))
8236 {
8237 x = build_sender_ref (ovar, ctx);
8238 if (TREE_CODE (TREE_TYPE (f)) == ARRAY_TYPE
8239 && TREE_TYPE (f) == TREE_TYPE (ovar))
8240 {
8241 gcc_assert (is_parallel_ctx (ctx)
8242 && DECL_ARTIFICIAL (ovar));
8243 /* _condtemp_ clause. */
8244 var = build_constructor (TREE_TYPE (x), NULL);
8245 }
8246 else
8247 var = build_fold_addr_expr (var);
8248 gimplify_assign (x, var, ilist);
8249 }
8250 else
8251 {
8252 x = build_sender_ref (ovar, ctx);
8253 gimplify_assign (x, var, ilist);
8254
8255 if (!TREE_READONLY (var)
8256 /* We don't need to receive a new reference to a result
8257 or parm decl. In fact we may not store to it as we will
8258 invalidate any pending RSO and generate wrong gimple
8259 during inlining. */
8260 && !((TREE_CODE (var) == RESULT_DECL
8261 || TREE_CODE (var) == PARM_DECL)
8262 && DECL_BY_REFERENCE (var)))
8263 {
8264 x = build_sender_ref (ovar, ctx);
8265 gimplify_assign (var, x, olist);
8266 }
8267 }
8268 }
8269 }
8270
8271 /* Emit an OpenACC head marker call, encapulating the partitioning and
8272 other information that must be processed by the target compiler.
8273 Return the maximum number of dimensions the associated loop might
8274 be partitioned over. */
8275
8276 static unsigned
8277 lower_oacc_head_mark (location_t loc, tree ddvar, tree clauses,
8278 gimple_seq *seq, omp_context *ctx)
8279 {
8280 unsigned levels = 0;
8281 unsigned tag = 0;
8282 tree gang_static = NULL_TREE;
8283 auto_vec<tree, 5> args;
8284
8285 args.quick_push (build_int_cst
8286 (integer_type_node, IFN_UNIQUE_OACC_HEAD_MARK));
8287 args.quick_push (ddvar);
8288 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
8289 {
8290 switch (OMP_CLAUSE_CODE (c))
8291 {
8292 case OMP_CLAUSE_GANG:
8293 tag |= OLF_DIM_GANG;
8294 gang_static = OMP_CLAUSE_GANG_STATIC_EXPR (c);
8295 /* static:* is represented by -1, and we can ignore it, as
8296 scheduling is always static. */
8297 if (gang_static && integer_minus_onep (gang_static))
8298 gang_static = NULL_TREE;
8299 levels++;
8300 break;
8301
8302 case OMP_CLAUSE_WORKER:
8303 tag |= OLF_DIM_WORKER;
8304 levels++;
8305 break;
8306
8307 case OMP_CLAUSE_VECTOR:
8308 tag |= OLF_DIM_VECTOR;
8309 levels++;
8310 break;
8311
8312 case OMP_CLAUSE_SEQ:
8313 tag |= OLF_SEQ;
8314 break;
8315
8316 case OMP_CLAUSE_AUTO:
8317 tag |= OLF_AUTO;
8318 break;
8319
8320 case OMP_CLAUSE_INDEPENDENT:
8321 tag |= OLF_INDEPENDENT;
8322 break;
8323
8324 case OMP_CLAUSE_TILE:
8325 tag |= OLF_TILE;
8326 break;
8327
8328 case OMP_CLAUSE_REDUCTION:
8329 tag |= OLF_REDUCTION;
8330 break;
8331
8332 default:
8333 continue;
8334 }
8335 }
8336
8337 if (gang_static)
8338 {
8339 if (DECL_P (gang_static))
8340 gang_static = build_outer_var_ref (gang_static, ctx);
8341 tag |= OLF_GANG_STATIC;
8342 }
8343
8344 omp_context *tgt = enclosing_target_ctx (ctx);
8345 if (!tgt || is_oacc_parallel_or_serial (tgt))
8346 ;
8347 else if (is_oacc_kernels (tgt))
8348 /* Not using this loops handling inside OpenACC 'kernels' regions. */
8349 gcc_unreachable ();
8350 else if (is_oacc_kernels_decomposed_part (tgt))
8351 ;
8352 else
8353 gcc_unreachable ();
8354
8355 /* In a parallel region, loops are implicitly INDEPENDENT. */
8356 if (!tgt || is_oacc_parallel_or_serial (tgt))
8357 tag |= OLF_INDEPENDENT;
8358
8359 /* Loops inside OpenACC 'kernels' decomposed parts' regions are expected to
8360 have an explicit 'seq' or 'independent' clause, and no 'auto' clause. */
8361 if (tgt && is_oacc_kernels_decomposed_part (tgt))
8362 {
8363 gcc_assert (tag & (OLF_SEQ | OLF_INDEPENDENT));
8364 gcc_assert (!(tag & OLF_AUTO));
8365 }
8366
8367 if (tag & OLF_TILE)
8368 /* Tiling could use all 3 levels. */
8369 levels = 3;
8370 else
8371 {
8372 /* A loop lacking SEQ, GANG, WORKER and/or VECTOR could be AUTO.
8373 Ensure at least one level, or 2 for possible auto
8374 partitioning */
8375 bool maybe_auto = !(tag & (((GOMP_DIM_MASK (GOMP_DIM_MAX) - 1)
8376 << OLF_DIM_BASE) | OLF_SEQ));
8377
8378 if (levels < 1u + maybe_auto)
8379 levels = 1u + maybe_auto;
8380 }
8381
8382 args.quick_push (build_int_cst (integer_type_node, levels));
8383 args.quick_push (build_int_cst (integer_type_node, tag));
8384 if (gang_static)
8385 args.quick_push (gang_static);
8386
8387 gcall *call = gimple_build_call_internal_vec (IFN_UNIQUE, args);
8388 gimple_set_location (call, loc);
8389 gimple_set_lhs (call, ddvar);
8390 gimple_seq_add_stmt (seq, call);
8391
8392 return levels;
8393 }
8394
8395 /* Emit an OpenACC lopp head or tail marker to SEQ. LEVEL is the
8396 partitioning level of the enclosed region. */
8397
8398 static void
8399 lower_oacc_loop_marker (location_t loc, tree ddvar, bool head,
8400 tree tofollow, gimple_seq *seq)
8401 {
8402 int marker_kind = (head ? IFN_UNIQUE_OACC_HEAD_MARK
8403 : IFN_UNIQUE_OACC_TAIL_MARK);
8404 tree marker = build_int_cst (integer_type_node, marker_kind);
8405 int nargs = 2 + (tofollow != NULL_TREE);
8406 gcall *call = gimple_build_call_internal (IFN_UNIQUE, nargs,
8407 marker, ddvar, tofollow);
8408 gimple_set_location (call, loc);
8409 gimple_set_lhs (call, ddvar);
8410 gimple_seq_add_stmt (seq, call);
8411 }
8412
8413 /* Generate the before and after OpenACC loop sequences. CLAUSES are
8414 the loop clauses, from which we extract reductions. Initialize
8415 HEAD and TAIL. */
8416
8417 static void
8418 lower_oacc_head_tail (location_t loc, tree clauses, gcall *private_marker,
8419 gimple_seq *head, gimple_seq *tail, omp_context *ctx)
8420 {
8421 bool inner = false;
8422 tree ddvar = create_tmp_var (integer_type_node, ".data_dep");
8423 gimple_seq_add_stmt (head, gimple_build_assign (ddvar, integer_zero_node));
8424
8425 unsigned count = lower_oacc_head_mark (loc, ddvar, clauses, head, ctx);
8426
8427 if (private_marker)
8428 {
8429 gimple_set_location (private_marker, loc);
8430 gimple_call_set_lhs (private_marker, ddvar);
8431 gimple_call_set_arg (private_marker, 1, ddvar);
8432 }
8433
8434 tree fork_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_FORK);
8435 tree join_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_JOIN);
8436
8437 gcc_assert (count);
8438 for (unsigned done = 1; count; count--, done++)
8439 {
8440 gimple_seq fork_seq = NULL;
8441 gimple_seq join_seq = NULL;
8442
8443 tree place = build_int_cst (integer_type_node, -1);
8444 gcall *fork = gimple_build_call_internal (IFN_UNIQUE, 3,
8445 fork_kind, ddvar, place);
8446 gimple_set_location (fork, loc);
8447 gimple_set_lhs (fork, ddvar);
8448
8449 gcall *join = gimple_build_call_internal (IFN_UNIQUE, 3,
8450 join_kind, ddvar, place);
8451 gimple_set_location (join, loc);
8452 gimple_set_lhs (join, ddvar);
8453
8454 /* Mark the beginning of this level sequence. */
8455 if (inner)
8456 lower_oacc_loop_marker (loc, ddvar, true,
8457 build_int_cst (integer_type_node, count),
8458 &fork_seq);
8459 lower_oacc_loop_marker (loc, ddvar, false,
8460 build_int_cst (integer_type_node, done),
8461 &join_seq);
8462
8463 lower_oacc_reductions (loc, clauses, place, inner,
8464 fork, (count == 1) ? private_marker : NULL,
8465 join, &fork_seq, &join_seq, ctx);
8466
8467 /* Append this level to head. */
8468 gimple_seq_add_seq (head, fork_seq);
8469 /* Prepend it to tail. */
8470 gimple_seq_add_seq (&join_seq, *tail);
8471 *tail = join_seq;
8472
8473 inner = true;
8474 }
8475
8476 /* Mark the end of the sequence. */
8477 lower_oacc_loop_marker (loc, ddvar, true, NULL_TREE, head);
8478 lower_oacc_loop_marker (loc, ddvar, false, NULL_TREE, tail);
8479 }
8480
8481 /* If exceptions are enabled, wrap the statements in BODY in a MUST_NOT_THROW
8482 catch handler and return it. This prevents programs from violating the
8483 structured block semantics with throws. */
8484
8485 static gimple_seq
8486 maybe_catch_exception (gimple_seq body)
8487 {
8488 gimple *g;
8489 tree decl;
8490
8491 if (!flag_exceptions)
8492 return body;
8493
8494 if (lang_hooks.eh_protect_cleanup_actions != NULL)
8495 decl = lang_hooks.eh_protect_cleanup_actions ();
8496 else
8497 decl = builtin_decl_explicit (BUILT_IN_TRAP);
8498
8499 g = gimple_build_eh_must_not_throw (decl);
8500 g = gimple_build_try (body, gimple_seq_alloc_with_stmt (g),
8501 GIMPLE_TRY_CATCH);
8502
8503 return gimple_seq_alloc_with_stmt (g);
8504 }
8505
8506
8507 /* Routines to lower OMP directives into OMP-GIMPLE. */
8508
8509 /* If ctx is a worksharing context inside of a cancellable parallel
8510 region and it isn't nowait, add lhs to its GIMPLE_OMP_RETURN
8511 and conditional branch to parallel's cancel_label to handle
8512 cancellation in the implicit barrier. */
8513
8514 static void
8515 maybe_add_implicit_barrier_cancel (omp_context *ctx, gimple *omp_return,
8516 gimple_seq *body)
8517 {
8518 gcc_assert (gimple_code (omp_return) == GIMPLE_OMP_RETURN);
8519 if (gimple_omp_return_nowait_p (omp_return))
8520 return;
8521 for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
8522 if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
8523 && outer->cancellable)
8524 {
8525 tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8526 tree c_bool_type = TREE_TYPE (TREE_TYPE (fndecl));
8527 tree lhs = create_tmp_var (c_bool_type);
8528 gimple_omp_return_set_lhs (omp_return, lhs);
8529 tree fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
8530 gimple *g = gimple_build_cond (NE_EXPR, lhs,
8531 fold_convert (c_bool_type,
8532 boolean_false_node),
8533 outer->cancel_label, fallthru_label);
8534 gimple_seq_add_stmt (body, g);
8535 gimple_seq_add_stmt (body, gimple_build_label (fallthru_label));
8536 }
8537 else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
8538 && gimple_code (outer->stmt) != GIMPLE_OMP_SCOPE)
8539 return;
8540 }
8541
8542 /* Find the first task_reduction or reduction clause or return NULL
8543 if there are none. */
8544
8545 static inline tree
8546 omp_task_reductions_find_first (tree clauses, enum tree_code code,
8547 enum omp_clause_code ccode)
8548 {
8549 while (1)
8550 {
8551 clauses = omp_find_clause (clauses, ccode);
8552 if (clauses == NULL_TREE)
8553 return NULL_TREE;
8554 if (ccode != OMP_CLAUSE_REDUCTION
8555 || code == OMP_TASKLOOP
8556 || OMP_CLAUSE_REDUCTION_TASK (clauses))
8557 return clauses;
8558 clauses = OMP_CLAUSE_CHAIN (clauses);
8559 }
8560 }
8561
8562 static void lower_omp_task_reductions (omp_context *, enum tree_code, tree,
8563 gimple_seq *, gimple_seq *);
8564
8565 /* Lower the OpenMP sections directive in the current statement in GSI_P.
8566 CTX is the enclosing OMP context for the current statement. */
8567
8568 static void
8569 lower_omp_sections (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8570 {
8571 tree block, control;
8572 gimple_stmt_iterator tgsi;
8573 gomp_sections *stmt;
8574 gimple *t;
8575 gbind *new_stmt, *bind;
8576 gimple_seq ilist, dlist, olist, tred_dlist = NULL, clist = NULL, new_body;
8577
8578 stmt = as_a <gomp_sections *> (gsi_stmt (*gsi_p));
8579
8580 push_gimplify_context ();
8581
8582 dlist = NULL;
8583 ilist = NULL;
8584
8585 tree rclauses
8586 = omp_task_reductions_find_first (gimple_omp_sections_clauses (stmt),
8587 OMP_SECTIONS, OMP_CLAUSE_REDUCTION);
8588 tree rtmp = NULL_TREE;
8589 if (rclauses)
8590 {
8591 tree type = build_pointer_type (pointer_sized_int_node);
8592 tree temp = create_tmp_var (type);
8593 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
8594 OMP_CLAUSE_DECL (c) = temp;
8595 OMP_CLAUSE_CHAIN (c) = gimple_omp_sections_clauses (stmt);
8596 gimple_omp_sections_set_clauses (stmt, c);
8597 lower_omp_task_reductions (ctx, OMP_SECTIONS,
8598 gimple_omp_sections_clauses (stmt),
8599 &ilist, &tred_dlist);
8600 rclauses = c;
8601 rtmp = make_ssa_name (type);
8602 gimple_seq_add_stmt (&ilist, gimple_build_assign (rtmp, temp));
8603 }
8604
8605 tree *clauses_ptr = gimple_omp_sections_clauses_ptr (stmt);
8606 lower_lastprivate_conditional_clauses (clauses_ptr, ctx);
8607
8608 lower_rec_input_clauses (gimple_omp_sections_clauses (stmt),
8609 &ilist, &dlist, ctx, NULL);
8610
8611 control = create_tmp_var (unsigned_type_node, ".section");
8612 gimple_omp_sections_set_control (stmt, control);
8613
8614 new_body = gimple_omp_body (stmt);
8615 gimple_omp_set_body (stmt, NULL);
8616 tgsi = gsi_start (new_body);
8617 for (; !gsi_end_p (tgsi); gsi_next (&tgsi))
8618 {
8619 omp_context *sctx;
8620 gimple *sec_start;
8621
8622 sec_start = gsi_stmt (tgsi);
8623 sctx = maybe_lookup_ctx (sec_start);
8624 gcc_assert (sctx);
8625
8626 lower_omp (gimple_omp_body_ptr (sec_start), sctx);
8627 gsi_insert_seq_after (&tgsi, gimple_omp_body (sec_start),
8628 GSI_CONTINUE_LINKING);
8629 gimple_omp_set_body (sec_start, NULL);
8630
8631 if (gsi_one_before_end_p (tgsi))
8632 {
8633 gimple_seq l = NULL;
8634 lower_lastprivate_clauses (gimple_omp_sections_clauses (stmt), NULL,
8635 &ilist, &l, &clist, ctx);
8636 gsi_insert_seq_after (&tgsi, l, GSI_CONTINUE_LINKING);
8637 gimple_omp_section_set_last (sec_start);
8638 }
8639
8640 gsi_insert_after (&tgsi, gimple_build_omp_return (false),
8641 GSI_CONTINUE_LINKING);
8642 }
8643
8644 block = make_node (BLOCK);
8645 bind = gimple_build_bind (NULL, new_body, block);
8646
8647 olist = NULL;
8648 lower_reduction_clauses (gimple_omp_sections_clauses (stmt), &olist,
8649 &clist, ctx);
8650 if (clist)
8651 {
8652 tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
8653 gcall *g = gimple_build_call (fndecl, 0);
8654 gimple_seq_add_stmt (&olist, g);
8655 gimple_seq_add_seq (&olist, clist);
8656 fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
8657 g = gimple_build_call (fndecl, 0);
8658 gimple_seq_add_stmt (&olist, g);
8659 }
8660
8661 block = make_node (BLOCK);
8662 new_stmt = gimple_build_bind (NULL, NULL, block);
8663 gsi_replace (gsi_p, new_stmt, true);
8664
8665 pop_gimplify_context (new_stmt);
8666 gimple_bind_append_vars (new_stmt, ctx->block_vars);
8667 BLOCK_VARS (block) = gimple_bind_vars (bind);
8668 if (BLOCK_VARS (block))
8669 TREE_USED (block) = 1;
8670
8671 new_body = NULL;
8672 gimple_seq_add_seq (&new_body, ilist);
8673 gimple_seq_add_stmt (&new_body, stmt);
8674 gimple_seq_add_stmt (&new_body, gimple_build_omp_sections_switch ());
8675 gimple_seq_add_stmt (&new_body, bind);
8676
8677 t = gimple_build_omp_continue (control, control);
8678 gimple_seq_add_stmt (&new_body, t);
8679
8680 gimple_seq_add_seq (&new_body, olist);
8681 if (ctx->cancellable)
8682 gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
8683 gimple_seq_add_seq (&new_body, dlist);
8684
8685 new_body = maybe_catch_exception (new_body);
8686
8687 bool nowait = omp_find_clause (gimple_omp_sections_clauses (stmt),
8688 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8689 t = gimple_build_omp_return (nowait);
8690 gimple_seq_add_stmt (&new_body, t);
8691 gimple_seq_add_seq (&new_body, tred_dlist);
8692 maybe_add_implicit_barrier_cancel (ctx, t, &new_body);
8693
8694 if (rclauses)
8695 OMP_CLAUSE_DECL (rclauses) = rtmp;
8696
8697 gimple_bind_set_body (new_stmt, new_body);
8698 }
8699
8700
8701 /* A subroutine of lower_omp_single. Expand the simple form of
8702 a GIMPLE_OMP_SINGLE, without a copyprivate clause:
8703
8704 if (GOMP_single_start ())
8705 BODY;
8706 [ GOMP_barrier (); ] -> unless 'nowait' is present.
8707
8708 FIXME. It may be better to delay expanding the logic of this until
8709 pass_expand_omp. The expanded logic may make the job more difficult
8710 to a synchronization analysis pass. */
8711
8712 static void
8713 lower_omp_single_simple (gomp_single *single_stmt, gimple_seq *pre_p)
8714 {
8715 location_t loc = gimple_location (single_stmt);
8716 tree tlabel = create_artificial_label (loc);
8717 tree flabel = create_artificial_label (loc);
8718 gimple *call, *cond;
8719 tree lhs, decl;
8720
8721 decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_START);
8722 lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (decl)));
8723 call = gimple_build_call (decl, 0);
8724 gimple_call_set_lhs (call, lhs);
8725 gimple_seq_add_stmt (pre_p, call);
8726
8727 cond = gimple_build_cond (EQ_EXPR, lhs,
8728 fold_convert_loc (loc, TREE_TYPE (lhs),
8729 boolean_true_node),
8730 tlabel, flabel);
8731 gimple_seq_add_stmt (pre_p, cond);
8732 gimple_seq_add_stmt (pre_p, gimple_build_label (tlabel));
8733 gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
8734 gimple_seq_add_stmt (pre_p, gimple_build_label (flabel));
8735 }
8736
8737
8738 /* A subroutine of lower_omp_single. Expand the simple form of
8739 a GIMPLE_OMP_SINGLE, with a copyprivate clause:
8740
8741 #pragma omp single copyprivate (a, b, c)
8742
8743 Create a new structure to hold copies of 'a', 'b' and 'c' and emit:
8744
8745 {
8746 if ((copyout_p = GOMP_single_copy_start ()) == NULL)
8747 {
8748 BODY;
8749 copyout.a = a;
8750 copyout.b = b;
8751 copyout.c = c;
8752 GOMP_single_copy_end (&copyout);
8753 }
8754 else
8755 {
8756 a = copyout_p->a;
8757 b = copyout_p->b;
8758 c = copyout_p->c;
8759 }
8760 GOMP_barrier ();
8761 }
8762
8763 FIXME. It may be better to delay expanding the logic of this until
8764 pass_expand_omp. The expanded logic may make the job more difficult
8765 to a synchronization analysis pass. */
8766
8767 static void
8768 lower_omp_single_copy (gomp_single *single_stmt, gimple_seq *pre_p,
8769 omp_context *ctx)
8770 {
8771 tree ptr_type, t, l0, l1, l2, bfn_decl;
8772 gimple_seq copyin_seq;
8773 location_t loc = gimple_location (single_stmt);
8774
8775 ctx->sender_decl = create_tmp_var (ctx->record_type, ".omp_copy_o");
8776
8777 ptr_type = build_pointer_type (ctx->record_type);
8778 ctx->receiver_decl = create_tmp_var (ptr_type, ".omp_copy_i");
8779
8780 l0 = create_artificial_label (loc);
8781 l1 = create_artificial_label (loc);
8782 l2 = create_artificial_label (loc);
8783
8784 bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_START);
8785 t = build_call_expr_loc (loc, bfn_decl, 0);
8786 t = fold_convert_loc (loc, ptr_type, t);
8787 gimplify_assign (ctx->receiver_decl, t, pre_p);
8788
8789 t = build2 (EQ_EXPR, boolean_type_node, ctx->receiver_decl,
8790 build_int_cst (ptr_type, 0));
8791 t = build3 (COND_EXPR, void_type_node, t,
8792 build_and_jump (&l0), build_and_jump (&l1));
8793 gimplify_and_add (t, pre_p);
8794
8795 gimple_seq_add_stmt (pre_p, gimple_build_label (l0));
8796
8797 gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
8798
8799 copyin_seq = NULL;
8800 lower_copyprivate_clauses (gimple_omp_single_clauses (single_stmt), pre_p,
8801 &copyin_seq, ctx);
8802
8803 t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
8804 bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_END);
8805 t = build_call_expr_loc (loc, bfn_decl, 1, t);
8806 gimplify_and_add (t, pre_p);
8807
8808 t = build_and_jump (&l2);
8809 gimplify_and_add (t, pre_p);
8810
8811 gimple_seq_add_stmt (pre_p, gimple_build_label (l1));
8812
8813 gimple_seq_add_seq (pre_p, copyin_seq);
8814
8815 gimple_seq_add_stmt (pre_p, gimple_build_label (l2));
8816 }
8817
8818
8819 /* Expand code for an OpenMP single directive. */
8820
8821 static void
8822 lower_omp_single (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8823 {
8824 tree block;
8825 gomp_single *single_stmt = as_a <gomp_single *> (gsi_stmt (*gsi_p));
8826 gbind *bind;
8827 gimple_seq bind_body, bind_body_tail = NULL, dlist;
8828
8829 push_gimplify_context ();
8830
8831 block = make_node (BLOCK);
8832 bind = gimple_build_bind (NULL, NULL, block);
8833 gsi_replace (gsi_p, bind, true);
8834 bind_body = NULL;
8835 dlist = NULL;
8836 lower_rec_input_clauses (gimple_omp_single_clauses (single_stmt),
8837 &bind_body, &dlist, ctx, NULL);
8838 lower_omp (gimple_omp_body_ptr (single_stmt), ctx);
8839
8840 gimple_seq_add_stmt (&bind_body, single_stmt);
8841
8842 if (ctx->record_type)
8843 lower_omp_single_copy (single_stmt, &bind_body, ctx);
8844 else
8845 lower_omp_single_simple (single_stmt, &bind_body);
8846
8847 gimple_omp_set_body (single_stmt, NULL);
8848
8849 gimple_seq_add_seq (&bind_body, dlist);
8850
8851 bind_body = maybe_catch_exception (bind_body);
8852
8853 bool nowait = omp_find_clause (gimple_omp_single_clauses (single_stmt),
8854 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8855 gimple *g = gimple_build_omp_return (nowait);
8856 gimple_seq_add_stmt (&bind_body_tail, g);
8857 maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
8858 if (ctx->record_type)
8859 {
8860 gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
8861 tree clobber = build_clobber (ctx->record_type);
8862 gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
8863 clobber), GSI_SAME_STMT);
8864 }
8865 gimple_seq_add_seq (&bind_body, bind_body_tail);
8866 gimple_bind_set_body (bind, bind_body);
8867
8868 pop_gimplify_context (bind);
8869
8870 gimple_bind_append_vars (bind, ctx->block_vars);
8871 BLOCK_VARS (block) = ctx->block_vars;
8872 if (BLOCK_VARS (block))
8873 TREE_USED (block) = 1;
8874 }
8875
8876
8877 /* Lower code for an OMP scope directive. */
8878
8879 static void
8880 lower_omp_scope (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8881 {
8882 tree block;
8883 gimple *scope_stmt = gsi_stmt (*gsi_p);
8884 gbind *bind;
8885 gimple_seq bind_body, bind_body_tail = NULL, dlist;
8886 gimple_seq tred_dlist = NULL;
8887
8888 push_gimplify_context ();
8889
8890 block = make_node (BLOCK);
8891 bind = gimple_build_bind (NULL, NULL, block);
8892 gsi_replace (gsi_p, bind, true);
8893 bind_body = NULL;
8894 dlist = NULL;
8895
8896 tree rclauses
8897 = omp_task_reductions_find_first (gimple_omp_scope_clauses (scope_stmt),
8898 OMP_SCOPE, OMP_CLAUSE_REDUCTION);
8899 if (rclauses)
8900 {
8901 tree type = build_pointer_type (pointer_sized_int_node);
8902 tree temp = create_tmp_var (type);
8903 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
8904 OMP_CLAUSE_DECL (c) = temp;
8905 OMP_CLAUSE_CHAIN (c) = gimple_omp_scope_clauses (scope_stmt);
8906 gimple_omp_scope_set_clauses (scope_stmt, c);
8907 lower_omp_task_reductions (ctx, OMP_SCOPE,
8908 gimple_omp_scope_clauses (scope_stmt),
8909 &bind_body, &tred_dlist);
8910 rclauses = c;
8911 tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_SCOPE_START);
8912 gimple *stmt = gimple_build_call (fndecl, 1, temp);
8913 gimple_seq_add_stmt (&bind_body, stmt);
8914 }
8915
8916 lower_rec_input_clauses (gimple_omp_scope_clauses (scope_stmt),
8917 &bind_body, &dlist, ctx, NULL);
8918 lower_omp (gimple_omp_body_ptr (scope_stmt), ctx);
8919
8920 gimple_seq_add_stmt (&bind_body, scope_stmt);
8921
8922 gimple_seq_add_seq (&bind_body, gimple_omp_body (scope_stmt));
8923
8924 gimple_omp_set_body (scope_stmt, NULL);
8925
8926 gimple_seq clist = NULL;
8927 lower_reduction_clauses (gimple_omp_scope_clauses (scope_stmt),
8928 &bind_body, &clist, ctx);
8929 if (clist)
8930 {
8931 tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
8932 gcall *g = gimple_build_call (fndecl, 0);
8933 gimple_seq_add_stmt (&bind_body, g);
8934 gimple_seq_add_seq (&bind_body, clist);
8935 fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
8936 g = gimple_build_call (fndecl, 0);
8937 gimple_seq_add_stmt (&bind_body, g);
8938 }
8939
8940 gimple_seq_add_seq (&bind_body, dlist);
8941
8942 bind_body = maybe_catch_exception (bind_body);
8943
8944 bool nowait = omp_find_clause (gimple_omp_scope_clauses (scope_stmt),
8945 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8946 gimple *g = gimple_build_omp_return (nowait);
8947 gimple_seq_add_stmt (&bind_body_tail, g);
8948 gimple_seq_add_seq (&bind_body_tail, tred_dlist);
8949 maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
8950 if (ctx->record_type)
8951 {
8952 gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
8953 tree clobber = build_clobber (ctx->record_type);
8954 gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
8955 clobber), GSI_SAME_STMT);
8956 }
8957 gimple_seq_add_seq (&bind_body, bind_body_tail);
8958
8959 gimple_bind_set_body (bind, bind_body);
8960
8961 pop_gimplify_context (bind);
8962
8963 gimple_bind_append_vars (bind, ctx->block_vars);
8964 BLOCK_VARS (block) = ctx->block_vars;
8965 if (BLOCK_VARS (block))
8966 TREE_USED (block) = 1;
8967 }
8968
8969 /* Lower code for an OMP dispatch directive. */
8970
8971 static void
8972 lower_omp_dispatch (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8973 {
8974 tree block;
8975 gimple *stmt = gsi_stmt (*gsi_p);
8976 gbind *bind;
8977
8978 push_gimplify_context ();
8979
8980 block = make_node (BLOCK);
8981 bind = gimple_build_bind (NULL, NULL, block);
8982 gsi_replace (gsi_p, bind, true);
8983
8984 lower_omp (gimple_omp_body_ptr (stmt), ctx);
8985 gimple_bind_set_body (bind, maybe_catch_exception (gimple_omp_body (stmt)));
8986
8987 pop_gimplify_context (bind);
8988
8989 gimple_bind_append_vars (bind, ctx->block_vars);
8990 BLOCK_VARS (block) = ctx->block_vars;
8991 }
8992
8993 /* Expand code for an OpenMP master or masked directive. */
8994
8995 static void
8996 lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8997 {
8998 tree block, lab = NULL, x, bfn_decl;
8999 gimple *stmt = gsi_stmt (*gsi_p);
9000 gbind *bind;
9001 location_t loc = gimple_location (stmt);
9002 gimple_seq tseq;
9003 tree filter = integer_zero_node;
9004
9005 push_gimplify_context ();
9006
9007 if (gimple_code (stmt) == GIMPLE_OMP_MASKED)
9008 {
9009 filter = omp_find_clause (gimple_omp_masked_clauses (stmt),
9010 OMP_CLAUSE_FILTER);
9011 if (filter)
9012 filter = fold_convert (integer_type_node,
9013 OMP_CLAUSE_FILTER_EXPR (filter));
9014 else
9015 filter = integer_zero_node;
9016 }
9017 block = make_node (BLOCK);
9018 bind = gimple_build_bind (NULL, NULL, block);
9019 gsi_replace (gsi_p, bind, true);
9020 gimple_bind_add_stmt (bind, stmt);
9021
9022 bfn_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
9023 x = build_call_expr_loc (loc, bfn_decl, 0);
9024 x = build2 (EQ_EXPR, boolean_type_node, x, filter);
9025 x = build3 (COND_EXPR, void_type_node, x, NULL, build_and_jump (&lab));
9026 tseq = NULL;
9027 gimplify_and_add (x, &tseq);
9028 gimple_bind_add_seq (bind, tseq);
9029
9030 lower_omp (gimple_omp_body_ptr (stmt), ctx);
9031 gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
9032 gimple_bind_add_seq (bind, gimple_omp_body (stmt));
9033 gimple_omp_set_body (stmt, NULL);
9034
9035 gimple_bind_add_stmt (bind, gimple_build_label (lab));
9036
9037 gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
9038
9039 pop_gimplify_context (bind);
9040
9041 gimple_bind_append_vars (bind, ctx->block_vars);
9042 BLOCK_VARS (block) = ctx->block_vars;
9043 }
9044
9045 /* Helper function for lower_omp_task_reductions. For a specific PASS
9046 find out the current clause it should be processed, or return false
9047 if all have been processed already. */
9048
9049 static inline bool
9050 omp_task_reduction_iterate (int pass, enum tree_code code,
9051 enum omp_clause_code ccode, tree *c, tree *decl,
9052 tree *type, tree *next)
9053 {
9054 for (; *c; *c = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode))
9055 {
9056 if (ccode == OMP_CLAUSE_REDUCTION
9057 && code != OMP_TASKLOOP
9058 && !OMP_CLAUSE_REDUCTION_TASK (*c))
9059 continue;
9060 *decl = OMP_CLAUSE_DECL (*c);
9061 *type = TREE_TYPE (*decl);
9062 if (TREE_CODE (*decl) == MEM_REF)
9063 {
9064 if (pass != 1)
9065 continue;
9066 }
9067 else
9068 {
9069 if (omp_privatize_by_reference (*decl))
9070 *type = TREE_TYPE (*type);
9071 if (pass != (!TREE_CONSTANT (TYPE_SIZE_UNIT (*type))))
9072 continue;
9073 }
9074 *next = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode);
9075 return true;
9076 }
9077 *decl = NULL_TREE;
9078 *type = NULL_TREE;
9079 *next = NULL_TREE;
9080 return false;
9081 }
9082
9083 /* Lower task_reduction and reduction clauses (the latter unless CODE is
9084 OMP_TASKGROUP only with task modifier). Register mapping of those in
9085 START sequence and reducing them and unregister them in the END sequence. */
9086
9087 static void
9088 lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
9089 gimple_seq *start, gimple_seq *end)
9090 {
9091 enum omp_clause_code ccode
9092 = (code == OMP_TASKGROUP
9093 ? OMP_CLAUSE_TASK_REDUCTION : OMP_CLAUSE_REDUCTION);
9094 tree cancellable = NULL_TREE;
9095 clauses = omp_task_reductions_find_first (clauses, code, ccode);
9096 if (clauses == NULL_TREE)
9097 return;
9098 if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
9099 {
9100 for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
9101 if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
9102 && outer->cancellable)
9103 {
9104 cancellable = error_mark_node;
9105 break;
9106 }
9107 else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
9108 && gimple_code (outer->stmt) != GIMPLE_OMP_SCOPE)
9109 break;
9110 }
9111 tree record_type = lang_hooks.types.make_type (RECORD_TYPE);
9112 tree *last = &TYPE_FIELDS (record_type);
9113 unsigned cnt = 0;
9114 if (cancellable)
9115 {
9116 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
9117 ptr_type_node);
9118 tree ifield = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
9119 integer_type_node);
9120 *last = field;
9121 DECL_CHAIN (field) = ifield;
9122 last = &DECL_CHAIN (ifield);
9123 DECL_CONTEXT (field) = record_type;
9124 if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
9125 SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
9126 DECL_CONTEXT (ifield) = record_type;
9127 if (TYPE_ALIGN (record_type) < DECL_ALIGN (ifield))
9128 SET_TYPE_ALIGN (record_type, DECL_ALIGN (ifield));
9129 }
9130 for (int pass = 0; pass < 2; pass++)
9131 {
9132 tree decl, type, next;
9133 for (tree c = clauses;
9134 omp_task_reduction_iterate (pass, code, ccode,
9135 &c, &decl, &type, &next); c = next)
9136 {
9137 ++cnt;
9138 tree new_type = type;
9139 if (ctx->outer)
9140 new_type = remap_type (type, &ctx->outer->cb);
9141 tree field
9142 = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL,
9143 DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE,
9144 new_type);
9145 if (DECL_P (decl) && type == TREE_TYPE (decl))
9146 {
9147 SET_DECL_ALIGN (field, DECL_ALIGN (decl));
9148 DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
9149 TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
9150 }
9151 else
9152 SET_DECL_ALIGN (field, TYPE_ALIGN (type));
9153 DECL_CONTEXT (field) = record_type;
9154 if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
9155 SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
9156 *last = field;
9157 last = &DECL_CHAIN (field);
9158 tree bfield
9159 = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL, NULL_TREE,
9160 boolean_type_node);
9161 DECL_CONTEXT (bfield) = record_type;
9162 if (TYPE_ALIGN (record_type) < DECL_ALIGN (bfield))
9163 SET_TYPE_ALIGN (record_type, DECL_ALIGN (bfield));
9164 *last = bfield;
9165 last = &DECL_CHAIN (bfield);
9166 }
9167 }
9168 *last = NULL_TREE;
9169 layout_type (record_type);
9170
9171 /* Build up an array which registers with the runtime all the reductions
9172 and deregisters them at the end. Format documented in libgomp/task.c. */
9173 tree atype = build_array_type_nelts (pointer_sized_int_node, 7 + cnt * 3);
9174 tree avar = create_tmp_var_raw (atype);
9175 gimple_add_tmp_var (avar);
9176 TREE_ADDRESSABLE (avar) = 1;
9177 tree r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_zero_node,
9178 NULL_TREE, NULL_TREE);
9179 tree t = build_int_cst (pointer_sized_int_node, cnt);
9180 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9181 gimple_seq seq = NULL;
9182 tree sz = fold_convert (pointer_sized_int_node,
9183 TYPE_SIZE_UNIT (record_type));
9184 int cachesz = 64;
9185 sz = fold_build2 (PLUS_EXPR, pointer_sized_int_node, sz,
9186 build_int_cst (pointer_sized_int_node, cachesz - 1));
9187 sz = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, sz,
9188 build_int_cst (pointer_sized_int_node, ~(cachesz - 1)));
9189 ctx->task_reductions.create (1 + cnt);
9190 ctx->task_reduction_map = new hash_map<tree, unsigned>;
9191 ctx->task_reductions.quick_push (TREE_CODE (sz) == INTEGER_CST
9192 ? sz : NULL_TREE);
9193 sz = force_gimple_operand (sz, &seq, true, NULL_TREE);
9194 gimple_seq_add_seq (start, seq);
9195 r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_one_node,
9196 NULL_TREE, NULL_TREE);
9197 gimple_seq_add_stmt (start, gimple_build_assign (r, sz));
9198 r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
9199 NULL_TREE, NULL_TREE);
9200 t = build_int_cst (pointer_sized_int_node,
9201 MAX (TYPE_ALIGN_UNIT (record_type), (unsigned) cachesz));
9202 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9203 r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (3),
9204 NULL_TREE, NULL_TREE);
9205 t = build_int_cst (pointer_sized_int_node, -1);
9206 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9207 r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (4),
9208 NULL_TREE, NULL_TREE);
9209 t = build_int_cst (pointer_sized_int_node, 0);
9210 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9211
9212 /* In end, build a loop that iterates from 0 to < omp_get_num_threads ()
9213 and for each task reduction checks a bool right after the private variable
9214 within that thread's chunk; if the bool is clear, it hasn't been
9215 initialized and thus isn't going to be reduced nor destructed, otherwise
9216 reduce and destruct it. */
9217 tree idx = create_tmp_var (size_type_node);
9218 gimple_seq_add_stmt (end, gimple_build_assign (idx, size_zero_node));
9219 tree num_thr_sz = create_tmp_var (size_type_node);
9220 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
9221 tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
9222 tree lab3 = NULL_TREE, lab7 = NULL_TREE;
9223 gimple *g;
9224 if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
9225 {
9226 /* For worksharing constructs or scope, only perform it in the master
9227 thread, with the exception of cancelled implicit barriers - then only
9228 handle the current thread. */
9229 tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
9230 t = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
9231 tree thr_num = create_tmp_var (integer_type_node);
9232 g = gimple_build_call (t, 0);
9233 gimple_call_set_lhs (g, thr_num);
9234 gimple_seq_add_stmt (end, g);
9235 if (cancellable)
9236 {
9237 tree c;
9238 tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9239 tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
9240 lab3 = create_artificial_label (UNKNOWN_LOCATION);
9241 if (code == OMP_FOR)
9242 c = gimple_omp_for_clauses (ctx->stmt);
9243 else if (code == OMP_SECTIONS)
9244 c = gimple_omp_sections_clauses (ctx->stmt);
9245 else /* if (code == OMP_SCOPE) */
9246 c = gimple_omp_scope_clauses (ctx->stmt);
9247 c = OMP_CLAUSE_DECL (omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_));
9248 cancellable = c;
9249 g = gimple_build_cond (NE_EXPR, c, build_zero_cst (TREE_TYPE (c)),
9250 lab5, lab6);
9251 gimple_seq_add_stmt (end, g);
9252 gimple_seq_add_stmt (end, gimple_build_label (lab5));
9253 g = gimple_build_assign (idx, NOP_EXPR, thr_num);
9254 gimple_seq_add_stmt (end, g);
9255 g = gimple_build_assign (num_thr_sz, PLUS_EXPR, idx,
9256 build_one_cst (TREE_TYPE (idx)));
9257 gimple_seq_add_stmt (end, g);
9258 gimple_seq_add_stmt (end, gimple_build_goto (lab3));
9259 gimple_seq_add_stmt (end, gimple_build_label (lab6));
9260 }
9261 g = gimple_build_cond (NE_EXPR, thr_num, integer_zero_node, lab2, lab4);
9262 gimple_seq_add_stmt (end, g);
9263 gimple_seq_add_stmt (end, gimple_build_label (lab4));
9264 }
9265 if (code != OMP_PARALLEL)
9266 {
9267 t = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
9268 tree num_thr = create_tmp_var (integer_type_node);
9269 g = gimple_build_call (t, 0);
9270 gimple_call_set_lhs (g, num_thr);
9271 gimple_seq_add_stmt (end, g);
9272 g = gimple_build_assign (num_thr_sz, NOP_EXPR, num_thr);
9273 gimple_seq_add_stmt (end, g);
9274 if (cancellable)
9275 gimple_seq_add_stmt (end, gimple_build_label (lab3));
9276 }
9277 else
9278 {
9279 tree c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
9280 OMP_CLAUSE__REDUCTEMP_);
9281 t = fold_convert (pointer_sized_int_node, OMP_CLAUSE_DECL (c));
9282 t = fold_convert (size_type_node, t);
9283 gimplify_assign (num_thr_sz, t, end);
9284 }
9285 t = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
9286 NULL_TREE, NULL_TREE);
9287 tree data = create_tmp_var (pointer_sized_int_node);
9288 gimple_seq_add_stmt (end, gimple_build_assign (data, t));
9289 if (code == OMP_TASKLOOP)
9290 {
9291 lab7 = create_artificial_label (UNKNOWN_LOCATION);
9292 g = gimple_build_cond (NE_EXPR, data,
9293 build_zero_cst (pointer_sized_int_node),
9294 lab1, lab7);
9295 gimple_seq_add_stmt (end, g);
9296 }
9297 gimple_seq_add_stmt (end, gimple_build_label (lab1));
9298 tree ptr;
9299 if (TREE_CODE (TYPE_SIZE_UNIT (record_type)) == INTEGER_CST)
9300 ptr = create_tmp_var (build_pointer_type (record_type));
9301 else
9302 ptr = create_tmp_var (ptr_type_node);
9303 gimple_seq_add_stmt (end, gimple_build_assign (ptr, NOP_EXPR, data));
9304
9305 tree field = TYPE_FIELDS (record_type);
9306 cnt = 0;
9307 if (cancellable)
9308 field = DECL_CHAIN (DECL_CHAIN (field));
9309 for (int pass = 0; pass < 2; pass++)
9310 {
9311 tree decl, type, next;
9312 for (tree c = clauses;
9313 omp_task_reduction_iterate (pass, code, ccode,
9314 &c, &decl, &type, &next); c = next)
9315 {
9316 tree var = decl, ref;
9317 if (TREE_CODE (decl) == MEM_REF)
9318 {
9319 var = TREE_OPERAND (var, 0);
9320 if (TREE_CODE (var) == POINTER_PLUS_EXPR)
9321 var = TREE_OPERAND (var, 0);
9322 tree v = var;
9323 if (TREE_CODE (var) == ADDR_EXPR)
9324 var = TREE_OPERAND (var, 0);
9325 else if (INDIRECT_REF_P (var))
9326 var = TREE_OPERAND (var, 0);
9327 tree orig_var = var;
9328 if (is_variable_sized (var))
9329 {
9330 gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
9331 var = DECL_VALUE_EXPR (var);
9332 gcc_assert (INDIRECT_REF_P (var));
9333 var = TREE_OPERAND (var, 0);
9334 gcc_assert (DECL_P (var));
9335 }
9336 t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
9337 if (orig_var != var)
9338 gcc_assert (TREE_CODE (v) == ADDR_EXPR);
9339 else if (TREE_CODE (v) == ADDR_EXPR)
9340 t = build_fold_addr_expr (t);
9341 else if (INDIRECT_REF_P (v))
9342 t = build_fold_indirect_ref (t);
9343 if (TREE_CODE (TREE_OPERAND (decl, 0)) == POINTER_PLUS_EXPR)
9344 {
9345 tree b = TREE_OPERAND (TREE_OPERAND (decl, 0), 1);
9346 b = maybe_lookup_decl_in_outer_ctx (b, ctx);
9347 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, b);
9348 }
9349 if (!integer_zerop (TREE_OPERAND (decl, 1)))
9350 t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
9351 fold_convert (size_type_node,
9352 TREE_OPERAND (decl, 1)));
9353 }
9354 else
9355 {
9356 t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
9357 if (!omp_privatize_by_reference (decl))
9358 t = build_fold_addr_expr (t);
9359 }
9360 t = fold_convert (pointer_sized_int_node, t);
9361 seq = NULL;
9362 t = force_gimple_operand (t, &seq, true, NULL_TREE);
9363 gimple_seq_add_seq (start, seq);
9364 r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9365 size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
9366 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9367 t = unshare_expr (byte_position (field));
9368 t = fold_convert (pointer_sized_int_node, t);
9369 ctx->task_reduction_map->put (c, cnt);
9370 ctx->task_reductions.quick_push (TREE_CODE (t) == INTEGER_CST
9371 ? t : NULL_TREE);
9372 seq = NULL;
9373 t = force_gimple_operand (t, &seq, true, NULL_TREE);
9374 gimple_seq_add_seq (start, seq);
9375 r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9376 size_int (7 + cnt * 3 + 1), NULL_TREE, NULL_TREE);
9377 gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9378
9379 tree bfield = DECL_CHAIN (field);
9380 tree cond;
9381 if (code == OMP_PARALLEL
9382 || code == OMP_FOR
9383 || code == OMP_SECTIONS
9384 || code == OMP_SCOPE)
9385 /* In parallel, worksharing or scope all threads unconditionally
9386 initialize all their task reduction private variables. */
9387 cond = boolean_true_node;
9388 else if (TREE_TYPE (ptr) == ptr_type_node)
9389 {
9390 cond = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
9391 unshare_expr (byte_position (bfield)));
9392 seq = NULL;
9393 cond = force_gimple_operand (cond, &seq, true, NULL_TREE);
9394 gimple_seq_add_seq (end, seq);
9395 tree pbool = build_pointer_type (TREE_TYPE (bfield));
9396 cond = build2 (MEM_REF, TREE_TYPE (bfield), cond,
9397 build_int_cst (pbool, 0));
9398 }
9399 else
9400 cond = build3 (COMPONENT_REF, TREE_TYPE (bfield),
9401 build_simple_mem_ref (ptr), bfield, NULL_TREE);
9402 tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
9403 tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
9404 tree condv = create_tmp_var (boolean_type_node);
9405 gimple_seq_add_stmt (end, gimple_build_assign (condv, cond));
9406 g = gimple_build_cond (NE_EXPR, condv, boolean_false_node,
9407 lab3, lab4);
9408 gimple_seq_add_stmt (end, g);
9409 gimple_seq_add_stmt (end, gimple_build_label (lab3));
9410 if (cancellable && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE)
9411 {
9412 /* If this reduction doesn't need destruction and parallel
9413 has been cancelled, there is nothing to do for this
9414 reduction, so jump around the merge operation. */
9415 tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9416 g = gimple_build_cond (NE_EXPR, cancellable,
9417 build_zero_cst (TREE_TYPE (cancellable)),
9418 lab4, lab5);
9419 gimple_seq_add_stmt (end, g);
9420 gimple_seq_add_stmt (end, gimple_build_label (lab5));
9421 }
9422
9423 tree new_var;
9424 if (TREE_TYPE (ptr) == ptr_type_node)
9425 {
9426 new_var = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
9427 unshare_expr (byte_position (field)));
9428 seq = NULL;
9429 new_var = force_gimple_operand (new_var, &seq, true, NULL_TREE);
9430 gimple_seq_add_seq (end, seq);
9431 tree pbool = build_pointer_type (TREE_TYPE (field));
9432 new_var = build2 (MEM_REF, TREE_TYPE (field), new_var,
9433 build_int_cst (pbool, 0));
9434 }
9435 else
9436 new_var = build3 (COMPONENT_REF, TREE_TYPE (field),
9437 build_simple_mem_ref (ptr), field, NULL_TREE);
9438
9439 enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
9440 if (TREE_CODE (decl) != MEM_REF
9441 && omp_privatize_by_reference (decl))
9442 ref = build_simple_mem_ref (ref);
9443 /* reduction(-:var) sums up the partial results, so it acts
9444 identically to reduction(+:var). */
9445 if (rcode == MINUS_EXPR)
9446 rcode = PLUS_EXPR;
9447 if (TREE_CODE (decl) == MEM_REF)
9448 {
9449 tree type = TREE_TYPE (new_var);
9450 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9451 tree i = create_tmp_var (TREE_TYPE (v));
9452 tree ptype = build_pointer_type (TREE_TYPE (type));
9453 if (DECL_P (v))
9454 {
9455 v = maybe_lookup_decl_in_outer_ctx (v, ctx);
9456 tree vv = create_tmp_var (TREE_TYPE (v));
9457 gimplify_assign (vv, v, start);
9458 v = vv;
9459 }
9460 ref = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9461 size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
9462 new_var = build_fold_addr_expr (new_var);
9463 new_var = fold_convert (ptype, new_var);
9464 ref = fold_convert (ptype, ref);
9465 tree m = create_tmp_var (ptype);
9466 gimplify_assign (m, new_var, end);
9467 new_var = m;
9468 m = create_tmp_var (ptype);
9469 gimplify_assign (m, ref, end);
9470 ref = m;
9471 gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), end);
9472 tree body = create_artificial_label (UNKNOWN_LOCATION);
9473 tree endl = create_artificial_label (UNKNOWN_LOCATION);
9474 gimple_seq_add_stmt (end, gimple_build_label (body));
9475 tree priv = build_simple_mem_ref (new_var);
9476 tree out = build_simple_mem_ref (ref);
9477 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
9478 {
9479 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
9480 tree decl_placeholder
9481 = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
9482 tree lab6 = NULL_TREE;
9483 if (cancellable)
9484 {
9485 /* If this reduction needs destruction and parallel
9486 has been cancelled, jump around the merge operation
9487 to the destruction. */
9488 tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9489 lab6 = create_artificial_label (UNKNOWN_LOCATION);
9490 tree zero = build_zero_cst (TREE_TYPE (cancellable));
9491 g = gimple_build_cond (NE_EXPR, cancellable, zero,
9492 lab6, lab5);
9493 gimple_seq_add_stmt (end, g);
9494 gimple_seq_add_stmt (end, gimple_build_label (lab5));
9495 }
9496 SET_DECL_VALUE_EXPR (placeholder, out);
9497 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
9498 SET_DECL_VALUE_EXPR (decl_placeholder, priv);
9499 DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
9500 lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
9501 gimple_seq_add_seq (end,
9502 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
9503 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
9504 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9505 {
9506 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
9507 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
9508 }
9509 if (cancellable)
9510 gimple_seq_add_stmt (end, gimple_build_label (lab6));
9511 tree x = lang_hooks.decls.omp_clause_dtor (c, priv);
9512 if (x)
9513 {
9514 gimple_seq tseq = NULL;
9515 gimplify_stmt (&x, &tseq);
9516 gimple_seq_add_seq (end, tseq);
9517 }
9518 }
9519 else
9520 {
9521 tree x = build2 (rcode, TREE_TYPE (out), out, priv);
9522 out = unshare_expr (out);
9523 gimplify_assign (out, x, end);
9524 }
9525 gimple *g
9526 = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
9527 TYPE_SIZE_UNIT (TREE_TYPE (type)));
9528 gimple_seq_add_stmt (end, g);
9529 g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
9530 TYPE_SIZE_UNIT (TREE_TYPE (type)));
9531 gimple_seq_add_stmt (end, g);
9532 g = gimple_build_assign (i, PLUS_EXPR, i,
9533 build_int_cst (TREE_TYPE (i), 1));
9534 gimple_seq_add_stmt (end, g);
9535 g = gimple_build_cond (LE_EXPR, i, v, body, endl);
9536 gimple_seq_add_stmt (end, g);
9537 gimple_seq_add_stmt (end, gimple_build_label (endl));
9538 }
9539 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
9540 {
9541 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
9542 tree oldv = NULL_TREE;
9543 tree lab6 = NULL_TREE;
9544 if (cancellable)
9545 {
9546 /* If this reduction needs destruction and parallel
9547 has been cancelled, jump around the merge operation
9548 to the destruction. */
9549 tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9550 lab6 = create_artificial_label (UNKNOWN_LOCATION);
9551 tree zero = build_zero_cst (TREE_TYPE (cancellable));
9552 g = gimple_build_cond (NE_EXPR, cancellable, zero,
9553 lab6, lab5);
9554 gimple_seq_add_stmt (end, g);
9555 gimple_seq_add_stmt (end, gimple_build_label (lab5));
9556 }
9557 if (omp_privatize_by_reference (decl)
9558 && !useless_type_conversion_p (TREE_TYPE (placeholder),
9559 TREE_TYPE (ref)))
9560 ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
9561 ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
9562 tree refv = create_tmp_var (TREE_TYPE (ref));
9563 gimplify_assign (refv, ref, end);
9564 ref = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), refv);
9565 SET_DECL_VALUE_EXPR (placeholder, ref);
9566 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
9567 tree d = maybe_lookup_decl (decl, ctx);
9568 gcc_assert (d);
9569 if (DECL_HAS_VALUE_EXPR_P (d))
9570 oldv = DECL_VALUE_EXPR (d);
9571 if (omp_privatize_by_reference (var))
9572 {
9573 tree v = fold_convert (TREE_TYPE (d),
9574 build_fold_addr_expr (new_var));
9575 SET_DECL_VALUE_EXPR (d, v);
9576 }
9577 else
9578 SET_DECL_VALUE_EXPR (d, new_var);
9579 DECL_HAS_VALUE_EXPR_P (d) = 1;
9580 lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
9581 if (oldv)
9582 SET_DECL_VALUE_EXPR (d, oldv);
9583 else
9584 {
9585 SET_DECL_VALUE_EXPR (d, NULL_TREE);
9586 DECL_HAS_VALUE_EXPR_P (d) = 0;
9587 }
9588 gimple_seq_add_seq (end, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
9589 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
9590 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9591 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
9592 if (cancellable)
9593 gimple_seq_add_stmt (end, gimple_build_label (lab6));
9594 tree x = lang_hooks.decls.omp_clause_dtor (c, new_var);
9595 if (x)
9596 {
9597 gimple_seq tseq = NULL;
9598 gimplify_stmt (&x, &tseq);
9599 gimple_seq_add_seq (end, tseq);
9600 }
9601 }
9602 else
9603 {
9604 tree x = build2 (rcode, TREE_TYPE (ref), ref, new_var);
9605 ref = unshare_expr (ref);
9606 gimplify_assign (ref, x, end);
9607 }
9608 gimple_seq_add_stmt (end, gimple_build_label (lab4));
9609 ++cnt;
9610 field = DECL_CHAIN (bfield);
9611 }
9612 }
9613
9614 if (code == OMP_TASKGROUP)
9615 {
9616 t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_REGISTER);
9617 g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
9618 gimple_seq_add_stmt (start, g);
9619 }
9620 else
9621 {
9622 tree c;
9623 if (code == OMP_FOR)
9624 c = gimple_omp_for_clauses (ctx->stmt);
9625 else if (code == OMP_SECTIONS)
9626 c = gimple_omp_sections_clauses (ctx->stmt);
9627 else if (code == OMP_SCOPE)
9628 c = gimple_omp_scope_clauses (ctx->stmt);
9629 else
9630 c = gimple_omp_taskreg_clauses (ctx->stmt);
9631 c = omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_);
9632 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)),
9633 build_fold_addr_expr (avar));
9634 gimplify_assign (OMP_CLAUSE_DECL (c), t, start);
9635 }
9636
9637 gimple_seq_add_stmt (end, gimple_build_assign (data, PLUS_EXPR, data, sz));
9638 gimple_seq_add_stmt (end, gimple_build_assign (idx, PLUS_EXPR, idx,
9639 size_one_node));
9640 g = gimple_build_cond (NE_EXPR, idx, num_thr_sz, lab1, lab2);
9641 gimple_seq_add_stmt (end, g);
9642 gimple_seq_add_stmt (end, gimple_build_label (lab2));
9643 if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
9644 {
9645 enum built_in_function bfn
9646 = BUILT_IN_GOMP_WORKSHARE_TASK_REDUCTION_UNREGISTER;
9647 t = builtin_decl_explicit (bfn);
9648 tree c_bool_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t)));
9649 tree arg;
9650 if (cancellable)
9651 {
9652 arg = create_tmp_var (c_bool_type);
9653 gimple_seq_add_stmt (end, gimple_build_assign (arg, NOP_EXPR,
9654 cancellable));
9655 }
9656 else
9657 arg = build_int_cst (c_bool_type, 0);
9658 g = gimple_build_call (t, 1, arg);
9659 }
9660 else
9661 {
9662 t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_UNREGISTER);
9663 g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
9664 }
9665 gimple_seq_add_stmt (end, g);
9666 if (lab7)
9667 gimple_seq_add_stmt (end, gimple_build_label (lab7));
9668 t = build_constructor (atype, NULL);
9669 TREE_THIS_VOLATILE (t) = 1;
9670 gimple_seq_add_stmt (end, gimple_build_assign (avar, t));
9671 }
9672
9673 /* Expand code for an OpenMP taskgroup directive. */
9674
9675 static void
9676 lower_omp_taskgroup (gimple_stmt_iterator *gsi_p, omp_context *ctx)
9677 {
9678 gimple *stmt = gsi_stmt (*gsi_p);
9679 gcall *x;
9680 gbind *bind;
9681 gimple_seq dseq = NULL;
9682 tree block = make_node (BLOCK);
9683
9684 bind = gimple_build_bind (NULL, NULL, block);
9685 gsi_replace (gsi_p, bind, true);
9686 gimple_bind_add_stmt (bind, stmt);
9687
9688 push_gimplify_context ();
9689
9690 x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_START),
9691 0);
9692 gimple_bind_add_stmt (bind, x);
9693
9694 lower_omp_task_reductions (ctx, OMP_TASKGROUP,
9695 gimple_omp_taskgroup_clauses (stmt),
9696 gimple_bind_body_ptr (bind), &dseq);
9697
9698 lower_omp (gimple_omp_body_ptr (stmt), ctx);
9699 gimple_bind_add_seq (bind, gimple_omp_body (stmt));
9700 gimple_omp_set_body (stmt, NULL);
9701
9702 gimple_bind_add_seq (bind, dseq);
9703
9704 pop_gimplify_context (bind);
9705
9706 gimple_bind_append_vars (bind, ctx->block_vars);
9707 BLOCK_VARS (block) = ctx->block_vars;
9708 }
9709
9710
9711 /* Fold the OMP_ORDERED_CLAUSES for the OMP_ORDERED in STMT if possible. */
9712
9713 static void
9714 lower_omp_ordered_clauses (gimple_stmt_iterator *gsi_p, gomp_ordered *ord_stmt,
9715 omp_context *ctx)
9716 {
9717 struct omp_for_data fd;
9718 if (!ctx->outer || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR)
9719 return;
9720
9721 unsigned int len = gimple_omp_for_collapse (ctx->outer->stmt);
9722 struct omp_for_data_loop *loops = XALLOCAVEC (struct omp_for_data_loop, len);
9723 omp_extract_for_data (as_a <gomp_for *> (ctx->outer->stmt), &fd, loops);
9724 if (!fd.ordered)
9725 return;
9726
9727 tree *list_p = gimple_omp_ordered_clauses_ptr (ord_stmt);
9728 tree c = gimple_omp_ordered_clauses (ord_stmt);
9729 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
9730 && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
9731 {
9732 /* Merge depend clauses from multiple adjacent
9733 #pragma omp ordered depend(sink:...) constructs
9734 into one #pragma omp ordered depend(sink:...), so that
9735 we can optimize them together. */
9736 gimple_stmt_iterator gsi = *gsi_p;
9737 gsi_next (&gsi);
9738 while (!gsi_end_p (gsi))
9739 {
9740 gimple *stmt = gsi_stmt (gsi);
9741 if (is_gimple_debug (stmt)
9742 || gimple_code (stmt) == GIMPLE_NOP)
9743 {
9744 gsi_next (&gsi);
9745 continue;
9746 }
9747 if (gimple_code (stmt) != GIMPLE_OMP_ORDERED)
9748 break;
9749 gomp_ordered *ord_stmt2 = as_a <gomp_ordered *> (stmt);
9750 c = gimple_omp_ordered_clauses (ord_stmt2);
9751 if (c == NULL_TREE
9752 || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS
9753 || OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
9754 break;
9755 while (*list_p)
9756 list_p = &OMP_CLAUSE_CHAIN (*list_p);
9757 *list_p = c;
9758 gsi_remove (&gsi, true);
9759 }
9760 }
9761
9762 /* Canonicalize sink dependence clauses into one folded clause if
9763 possible.
9764
9765 The basic algorithm is to create a sink vector whose first
9766 element is the GCD of all the first elements, and whose remaining
9767 elements are the minimum of the subsequent columns.
9768
9769 We ignore dependence vectors whose first element is zero because
9770 such dependencies are known to be executed by the same thread.
9771
9772 We take into account the direction of the loop, so a minimum
9773 becomes a maximum if the loop is iterating forwards. We also
9774 ignore sink clauses where the loop direction is unknown, or where
9775 the offsets are clearly invalid because they are not a multiple
9776 of the loop increment.
9777
9778 For example:
9779
9780 #pragma omp for ordered(2)
9781 for (i=0; i < N; ++i)
9782 for (j=0; j < M; ++j)
9783 {
9784 #pragma omp ordered \
9785 depend(sink:i-8,j-2) \
9786 depend(sink:i,j-1) \ // Completely ignored because i+0.
9787 depend(sink:i-4,j-3) \
9788 depend(sink:i-6,j-4)
9789 #pragma omp ordered depend(source)
9790 }
9791
9792 Folded clause is:
9793
9794 depend(sink:-gcd(8,4,6),-min(2,3,4))
9795 -or-
9796 depend(sink:-2,-2)
9797 */
9798
9799 /* FIXME: Computing GCD's where the first element is zero is
9800 non-trivial in the presence of collapsed loops. Do this later. */
9801 if (fd.collapse > 1)
9802 return;
9803
9804 wide_int *folded_deps = XALLOCAVEC (wide_int, 2 * len - 1);
9805
9806 /* wide_int is not a POD so it must be default-constructed. */
9807 for (unsigned i = 0; i != 2 * len - 1; ++i)
9808 new (static_cast<void*>(folded_deps + i)) wide_int ();
9809
9810 tree folded_dep = NULL_TREE;
9811 /* TRUE if the first dimension's offset is negative. */
9812 bool neg_offset_p = false;
9813
9814 list_p = gimple_omp_ordered_clauses_ptr (ord_stmt);
9815 unsigned int i;
9816 while ((c = *list_p) != NULL)
9817 {
9818 bool remove = false;
9819
9820 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS);
9821 if (OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
9822 goto next_ordered_clause;
9823
9824 tree vec;
9825 for (vec = OMP_CLAUSE_DECL (c), i = 0;
9826 vec && TREE_CODE (vec) == TREE_LIST;
9827 vec = TREE_CHAIN (vec), ++i)
9828 {
9829 gcc_assert (i < len);
9830
9831 /* omp_extract_for_data has canonicalized the condition. */
9832 gcc_assert (fd.loops[i].cond_code == LT_EXPR
9833 || fd.loops[i].cond_code == GT_EXPR);
9834 bool forward = fd.loops[i].cond_code == LT_EXPR;
9835 bool maybe_lexically_later = true;
9836
9837 /* While the committee makes up its mind, bail if we have any
9838 non-constant steps. */
9839 if (TREE_CODE (fd.loops[i].step) != INTEGER_CST)
9840 goto lower_omp_ordered_ret;
9841
9842 tree itype = TREE_TYPE (TREE_VALUE (vec));
9843 if (POINTER_TYPE_P (itype))
9844 itype = sizetype;
9845 wide_int offset = wide_int::from (wi::to_wide (TREE_PURPOSE (vec)),
9846 TYPE_PRECISION (itype),
9847 TYPE_SIGN (itype));
9848
9849 /* Ignore invalid offsets that are not multiples of the step. */
9850 if (!wi::multiple_of_p (wi::abs (offset),
9851 wi::abs (wi::to_wide (fd.loops[i].step)),
9852 UNSIGNED))
9853 {
9854 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
9855 "ignoring %<sink%> clause with offset that is not "
9856 "a multiple of the loop step");
9857 remove = true;
9858 goto next_ordered_clause;
9859 }
9860
9861 /* Calculate the first dimension. The first dimension of
9862 the folded dependency vector is the GCD of the first
9863 elements, while ignoring any first elements whose offset
9864 is 0. */
9865 if (i == 0)
9866 {
9867 /* Ignore dependence vectors whose first dimension is 0. */
9868 if (offset == 0)
9869 {
9870 remove = true;
9871 goto next_ordered_clause;
9872 }
9873 else
9874 {
9875 if (!TYPE_UNSIGNED (itype) && (forward ^ wi::neg_p (offset)))
9876 {
9877 error_at (OMP_CLAUSE_LOCATION (c),
9878 "first offset must be in opposite direction "
9879 "of loop iterations");
9880 goto lower_omp_ordered_ret;
9881 }
9882 if (forward)
9883 offset = -offset;
9884 neg_offset_p = forward;
9885 /* Initialize the first time around. */
9886 if (folded_dep == NULL_TREE)
9887 {
9888 folded_dep = c;
9889 folded_deps[0] = offset;
9890 }
9891 else
9892 folded_deps[0] = wi::gcd (folded_deps[0],
9893 offset, UNSIGNED);
9894 }
9895 }
9896 /* Calculate minimum for the remaining dimensions. */
9897 else
9898 {
9899 folded_deps[len + i - 1] = offset;
9900 if (folded_dep == c)
9901 folded_deps[i] = offset;
9902 else if (maybe_lexically_later
9903 && !wi::eq_p (folded_deps[i], offset))
9904 {
9905 if (forward ^ wi::gts_p (folded_deps[i], offset))
9906 {
9907 unsigned int j;
9908 folded_dep = c;
9909 for (j = 1; j <= i; j++)
9910 folded_deps[j] = folded_deps[len + j - 1];
9911 }
9912 else
9913 maybe_lexically_later = false;
9914 }
9915 }
9916 }
9917 gcc_assert (i == len);
9918
9919 remove = true;
9920
9921 next_ordered_clause:
9922 if (remove)
9923 *list_p = OMP_CLAUSE_CHAIN (c);
9924 else
9925 list_p = &OMP_CLAUSE_CHAIN (c);
9926 }
9927
9928 if (folded_dep)
9929 {
9930 if (neg_offset_p)
9931 folded_deps[0] = -folded_deps[0];
9932
9933 tree itype = TREE_TYPE (TREE_VALUE (OMP_CLAUSE_DECL (folded_dep)));
9934 if (POINTER_TYPE_P (itype))
9935 itype = sizetype;
9936
9937 TREE_PURPOSE (OMP_CLAUSE_DECL (folded_dep))
9938 = wide_int_to_tree (itype, folded_deps[0]);
9939 OMP_CLAUSE_CHAIN (folded_dep) = gimple_omp_ordered_clauses (ord_stmt);
9940 *gimple_omp_ordered_clauses_ptr (ord_stmt) = folded_dep;
9941 }
9942
9943 lower_omp_ordered_ret:
9944
9945 /* Ordered without clauses is #pragma omp threads, while we want
9946 a nop instead if we remove all clauses. */
9947 if (gimple_omp_ordered_clauses (ord_stmt) == NULL_TREE)
9948 gsi_replace (gsi_p, gimple_build_nop (), true);
9949 }
9950
9951
9952 /* Expand code for an OpenMP ordered directive. */
9953
9954 static void
9955 lower_omp_ordered (gimple_stmt_iterator *gsi_p, omp_context *ctx)
9956 {
9957 tree block;
9958 gimple *stmt = gsi_stmt (*gsi_p), *g;
9959 gomp_ordered *ord_stmt = as_a <gomp_ordered *> (stmt);
9960 gcall *x;
9961 gbind *bind;
9962 bool simd = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
9963 OMP_CLAUSE_SIMD);
9964 /* FIXME: this should check presence of OMP_CLAUSE__SIMT_ on the enclosing
9965 loop. */
9966 bool maybe_simt
9967 = simd && omp_maybe_offloaded_ctx (ctx) && omp_max_simt_vf () > 1;
9968 bool threads = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
9969 OMP_CLAUSE_THREADS);
9970
9971 if (gimple_omp_ordered_standalone_p (ord_stmt))
9972 {
9973 /* FIXME: This is needs to be moved to the expansion to verify various
9974 conditions only testable on cfg with dominators computed, and also
9975 all the depend clauses to be merged still might need to be available
9976 for the runtime checks. */
9977 if (0)
9978 lower_omp_ordered_clauses (gsi_p, ord_stmt, ctx);
9979 return;
9980 }
9981
9982 push_gimplify_context ();
9983
9984 block = make_node (BLOCK);
9985 bind = gimple_build_bind (NULL, NULL, block);
9986 gsi_replace (gsi_p, bind, true);
9987 gimple_bind_add_stmt (bind, stmt);
9988
9989 if (simd)
9990 {
9991 x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_START, 1,
9992 build_int_cst (NULL_TREE, threads));
9993 cfun->has_simduid_loops = true;
9994 }
9995 else
9996 x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_START),
9997 0);
9998 gimple_bind_add_stmt (bind, x);
9999
10000 tree counter = NULL_TREE, test = NULL_TREE, body = NULL_TREE;
10001 if (maybe_simt)
10002 {
10003 counter = create_tmp_var (integer_type_node);
10004 g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
10005 gimple_call_set_lhs (g, counter);
10006 gimple_bind_add_stmt (bind, g);
10007
10008 body = create_artificial_label (UNKNOWN_LOCATION);
10009 test = create_artificial_label (UNKNOWN_LOCATION);
10010 gimple_bind_add_stmt (bind, gimple_build_label (body));
10011
10012 tree simt_pred = create_tmp_var (integer_type_node);
10013 g = gimple_build_call_internal (IFN_GOMP_SIMT_ORDERED_PRED, 1, counter);
10014 gimple_call_set_lhs (g, simt_pred);
10015 gimple_bind_add_stmt (bind, g);
10016
10017 tree t = create_artificial_label (UNKNOWN_LOCATION);
10018 g = gimple_build_cond (EQ_EXPR, simt_pred, integer_zero_node, t, test);
10019 gimple_bind_add_stmt (bind, g);
10020
10021 gimple_bind_add_stmt (bind, gimple_build_label (t));
10022 }
10023 lower_omp (gimple_omp_body_ptr (stmt), ctx);
10024 gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
10025 gimple_bind_add_seq (bind, gimple_omp_body (stmt));
10026 gimple_omp_set_body (stmt, NULL);
10027
10028 if (maybe_simt)
10029 {
10030 gimple_bind_add_stmt (bind, gimple_build_label (test));
10031 g = gimple_build_assign (counter, MINUS_EXPR, counter, integer_one_node);
10032 gimple_bind_add_stmt (bind, g);
10033
10034 tree c = build2 (GE_EXPR, boolean_type_node, counter, integer_zero_node);
10035 tree nonneg = create_tmp_var (integer_type_node);
10036 gimple_seq tseq = NULL;
10037 gimplify_assign (nonneg, fold_convert (integer_type_node, c), &tseq);
10038 gimple_bind_add_seq (bind, tseq);
10039
10040 g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY, 1, nonneg);
10041 gimple_call_set_lhs (g, nonneg);
10042 gimple_bind_add_stmt (bind, g);
10043
10044 tree end = create_artificial_label (UNKNOWN_LOCATION);
10045 g = gimple_build_cond (NE_EXPR, nonneg, integer_zero_node, body, end);
10046 gimple_bind_add_stmt (bind, g);
10047
10048 gimple_bind_add_stmt (bind, gimple_build_label (end));
10049 }
10050 if (simd)
10051 x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_END, 1,
10052 build_int_cst (NULL_TREE, threads));
10053 else
10054 x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_END),
10055 0);
10056 gimple_bind_add_stmt (bind, x);
10057
10058 gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
10059
10060 pop_gimplify_context (bind);
10061
10062 gimple_bind_append_vars (bind, ctx->block_vars);
10063 BLOCK_VARS (block) = gimple_bind_vars (bind);
10064 }
10065
10066
10067 /* Expand code for an OpenMP scan directive and the structured block
10068 before the scan directive. */
10069
10070 static void
10071 lower_omp_scan (gimple_stmt_iterator *gsi_p, omp_context *ctx)
10072 {
10073 gimple *stmt = gsi_stmt (*gsi_p);
10074 bool has_clauses
10075 = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)) != NULL;
10076 tree lane = NULL_TREE;
10077 gimple_seq before = NULL;
10078 omp_context *octx = ctx->outer;
10079 gcc_assert (octx);
10080 if (octx->scan_exclusive && !has_clauses)
10081 {
10082 gimple_stmt_iterator gsi2 = *gsi_p;
10083 gsi_next (&gsi2);
10084 gimple *stmt2 = gsi_stmt (gsi2);
10085 /* For exclusive scan, swap GIMPLE_OMP_SCAN without clauses
10086 with following GIMPLE_OMP_SCAN with clauses, so that input_phase,
10087 the one with exclusive clause(s), comes first. */
10088 if (stmt2
10089 && gimple_code (stmt2) == GIMPLE_OMP_SCAN
10090 && gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt2)) != NULL)
10091 {
10092 gsi_remove (gsi_p, false);
10093 gsi_insert_after (gsi_p, stmt, GSI_SAME_STMT);
10094 ctx = maybe_lookup_ctx (stmt2);
10095 gcc_assert (ctx);
10096 lower_omp_scan (gsi_p, ctx);
10097 return;
10098 }
10099 }
10100
10101 bool input_phase = has_clauses ^ octx->scan_inclusive;
10102 bool is_simd = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
10103 && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_SIMD);
10104 bool is_for = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
10105 && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_FOR
10106 && !gimple_omp_for_combined_p (octx->stmt));
10107 bool is_for_simd = is_simd && gimple_omp_for_combined_into_p (octx->stmt);
10108 if (is_for_simd && octx->for_simd_scan_phase)
10109 is_simd = false;
10110 if (is_simd)
10111 if (tree c = omp_find_clause (gimple_omp_for_clauses (octx->stmt),
10112 OMP_CLAUSE__SIMDUID_))
10113 {
10114 tree uid = OMP_CLAUSE__SIMDUID__DECL (c);
10115 lane = create_tmp_var (unsigned_type_node);
10116 tree t = build_int_cst (integer_type_node,
10117 input_phase ? 1
10118 : octx->scan_inclusive ? 2 : 3);
10119 gimple *g
10120 = gimple_build_call_internal (IFN_GOMP_SIMD_LANE, 2, uid, t);
10121 gimple_call_set_lhs (g, lane);
10122 gimple_seq_add_stmt (&before, g);
10123 }
10124
10125 if (is_simd || is_for)
10126 {
10127 for (tree c = gimple_omp_for_clauses (octx->stmt);
10128 c; c = OMP_CLAUSE_CHAIN (c))
10129 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10130 && OMP_CLAUSE_REDUCTION_INSCAN (c))
10131 {
10132 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
10133 tree var = OMP_CLAUSE_DECL (c);
10134 tree new_var = lookup_decl (var, octx);
10135 tree val = new_var;
10136 tree var2 = NULL_TREE;
10137 tree var3 = NULL_TREE;
10138 tree var4 = NULL_TREE;
10139 tree lane0 = NULL_TREE;
10140 tree new_vard = new_var;
10141 if (omp_privatize_by_reference (var))
10142 {
10143 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
10144 val = new_var;
10145 }
10146 if (DECL_HAS_VALUE_EXPR_P (new_vard))
10147 {
10148 val = DECL_VALUE_EXPR (new_vard);
10149 if (new_vard != new_var)
10150 {
10151 gcc_assert (TREE_CODE (val) == ADDR_EXPR);
10152 val = TREE_OPERAND (val, 0);
10153 }
10154 if (TREE_CODE (val) == ARRAY_REF
10155 && VAR_P (TREE_OPERAND (val, 0)))
10156 {
10157 tree v = TREE_OPERAND (val, 0);
10158 if (lookup_attribute ("omp simd array",
10159 DECL_ATTRIBUTES (v)))
10160 {
10161 val = unshare_expr (val);
10162 lane0 = TREE_OPERAND (val, 1);
10163 TREE_OPERAND (val, 1) = lane;
10164 var2 = lookup_decl (v, octx);
10165 if (octx->scan_exclusive)
10166 var4 = lookup_decl (var2, octx);
10167 if (input_phase
10168 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10169 var3 = maybe_lookup_decl (var4 ? var4 : var2, octx);
10170 if (!input_phase)
10171 {
10172 var2 = build4 (ARRAY_REF, TREE_TYPE (val),
10173 var2, lane, NULL_TREE, NULL_TREE);
10174 TREE_THIS_NOTRAP (var2) = 1;
10175 if (octx->scan_exclusive)
10176 {
10177 var4 = build4 (ARRAY_REF, TREE_TYPE (val),
10178 var4, lane, NULL_TREE,
10179 NULL_TREE);
10180 TREE_THIS_NOTRAP (var4) = 1;
10181 }
10182 }
10183 else
10184 var2 = val;
10185 }
10186 }
10187 gcc_assert (var2);
10188 }
10189 else
10190 {
10191 var2 = build_outer_var_ref (var, octx);
10192 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10193 {
10194 var3 = maybe_lookup_decl (new_vard, octx);
10195 if (var3 == new_vard || var3 == NULL_TREE)
10196 var3 = NULL_TREE;
10197 else if (is_simd && octx->scan_exclusive && !input_phase)
10198 {
10199 var4 = maybe_lookup_decl (var3, octx);
10200 if (var4 == var3 || var4 == NULL_TREE)
10201 {
10202 if (TREE_ADDRESSABLE (TREE_TYPE (new_var)))
10203 {
10204 var4 = var3;
10205 var3 = NULL_TREE;
10206 }
10207 else
10208 var4 = NULL_TREE;
10209 }
10210 }
10211 }
10212 if (is_simd
10213 && octx->scan_exclusive
10214 && !input_phase
10215 && var4 == NULL_TREE)
10216 var4 = create_tmp_var (TREE_TYPE (val));
10217 }
10218 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10219 {
10220 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
10221 if (input_phase)
10222 {
10223 if (var3)
10224 {
10225 /* If we've added a separate identity element
10226 variable, copy it over into val. */
10227 tree x = lang_hooks.decls.omp_clause_assign_op (c, val,
10228 var3);
10229 gimplify_and_add (x, &before);
10230 }
10231 else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
10232 {
10233 /* Otherwise, assign to it the identity element. */
10234 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
10235 if (is_for)
10236 tseq = copy_gimple_seq_and_replace_locals (tseq);
10237 tree ref = build_outer_var_ref (var, octx);
10238 tree x = (DECL_HAS_VALUE_EXPR_P (new_vard)
10239 ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
10240 if (x)
10241 {
10242 if (new_vard != new_var)
10243 val = build_fold_addr_expr_loc (clause_loc, val);
10244 SET_DECL_VALUE_EXPR (new_vard, val);
10245 }
10246 SET_DECL_VALUE_EXPR (placeholder, ref);
10247 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
10248 lower_omp (&tseq, octx);
10249 if (x)
10250 SET_DECL_VALUE_EXPR (new_vard, x);
10251 SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
10252 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
10253 gimple_seq_add_seq (&before, tseq);
10254 if (is_simd)
10255 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
10256 }
10257 }
10258 else if (is_simd)
10259 {
10260 tree x;
10261 if (octx->scan_exclusive)
10262 {
10263 tree v4 = unshare_expr (var4);
10264 tree v2 = unshare_expr (var2);
10265 x = lang_hooks.decls.omp_clause_assign_op (c, v4, v2);
10266 gimplify_and_add (x, &before);
10267 }
10268 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
10269 x = (DECL_HAS_VALUE_EXPR_P (new_vard)
10270 ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
10271 tree vexpr = val;
10272 if (x && new_vard != new_var)
10273 vexpr = build_fold_addr_expr_loc (clause_loc, val);
10274 if (x)
10275 SET_DECL_VALUE_EXPR (new_vard, vexpr);
10276 SET_DECL_VALUE_EXPR (placeholder, var2);
10277 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
10278 lower_omp (&tseq, octx);
10279 gimple_seq_add_seq (&before, tseq);
10280 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
10281 if (x)
10282 SET_DECL_VALUE_EXPR (new_vard, x);
10283 SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
10284 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
10285 if (octx->scan_inclusive)
10286 {
10287 x = lang_hooks.decls.omp_clause_assign_op (c, val,
10288 var2);
10289 gimplify_and_add (x, &before);
10290 }
10291 else if (lane0 == NULL_TREE)
10292 {
10293 x = lang_hooks.decls.omp_clause_assign_op (c, val,
10294 var4);
10295 gimplify_and_add (x, &before);
10296 }
10297 }
10298 }
10299 else
10300 {
10301 if (input_phase)
10302 {
10303 /* input phase. Set val to initializer before
10304 the body. */
10305 tree x = omp_reduction_init (c, TREE_TYPE (new_var));
10306 gimplify_assign (val, x, &before);
10307 }
10308 else if (is_simd)
10309 {
10310 /* scan phase. */
10311 enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
10312 if (code == MINUS_EXPR)
10313 code = PLUS_EXPR;
10314
10315 tree x = build2 (code, TREE_TYPE (var2),
10316 unshare_expr (var2), unshare_expr (val));
10317 if (octx->scan_inclusive)
10318 {
10319 gimplify_assign (unshare_expr (var2), x, &before);
10320 gimplify_assign (val, var2, &before);
10321 }
10322 else
10323 {
10324 gimplify_assign (unshare_expr (var4),
10325 unshare_expr (var2), &before);
10326 gimplify_assign (var2, x, &before);
10327 if (lane0 == NULL_TREE)
10328 gimplify_assign (val, var4, &before);
10329 }
10330 }
10331 }
10332 if (octx->scan_exclusive && !input_phase && lane0)
10333 {
10334 tree vexpr = unshare_expr (var4);
10335 TREE_OPERAND (vexpr, 1) = lane0;
10336 if (new_vard != new_var)
10337 vexpr = build_fold_addr_expr_loc (clause_loc, vexpr);
10338 SET_DECL_VALUE_EXPR (new_vard, vexpr);
10339 }
10340 }
10341 }
10342 if (is_simd && !is_for_simd)
10343 {
10344 gsi_insert_seq_after (gsi_p, gimple_omp_body (stmt), GSI_SAME_STMT);
10345 gsi_insert_seq_after (gsi_p, before, GSI_SAME_STMT);
10346 gsi_replace (gsi_p, gimple_build_nop (), true);
10347 return;
10348 }
10349 lower_omp (gimple_omp_body_ptr (stmt), octx);
10350 if (before)
10351 {
10352 gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (stmt));
10353 gsi_insert_seq_before (&gsi, before, GSI_SAME_STMT);
10354 }
10355 }
10356
10357
10358 /* Gimplify a GIMPLE_OMP_CRITICAL statement. This is a relatively simple
10359 substitution of a couple of function calls. But in the NAMED case,
10360 requires that languages coordinate a symbol name. It is therefore
10361 best put here in common code. */
10362
10363 static GTY(()) hash_map<tree, tree> *critical_name_mutexes;
10364
10365 static void
10366 lower_omp_critical (gimple_stmt_iterator *gsi_p, omp_context *ctx)
10367 {
10368 tree block;
10369 tree name, lock, unlock;
10370 gomp_critical *stmt = as_a <gomp_critical *> (gsi_stmt (*gsi_p));
10371 gbind *bind;
10372 location_t loc = gimple_location (stmt);
10373 gimple_seq tbody;
10374
10375 name = gimple_omp_critical_name (stmt);
10376 if (name)
10377 {
10378 tree decl;
10379
10380 if (!critical_name_mutexes)
10381 critical_name_mutexes = hash_map<tree, tree>::create_ggc (10);
10382
10383 tree *n = critical_name_mutexes->get (name);
10384 if (n == NULL)
10385 {
10386 char *new_str;
10387
10388 decl = create_tmp_var_raw (ptr_type_node);
10389
10390 new_str = ACONCAT ((".gomp_critical_user_",
10391 IDENTIFIER_POINTER (name), NULL));
10392 DECL_NAME (decl) = get_identifier (new_str);
10393 TREE_PUBLIC (decl) = 1;
10394 TREE_STATIC (decl) = 1;
10395 DECL_COMMON (decl) = 1;
10396 DECL_ARTIFICIAL (decl) = 1;
10397 DECL_IGNORED_P (decl) = 1;
10398
10399 varpool_node::finalize_decl (decl);
10400
10401 critical_name_mutexes->put (name, decl);
10402 }
10403 else
10404 decl = *n;
10405
10406 /* If '#pragma omp critical' is inside offloaded region or
10407 inside function marked as offloadable, the symbol must be
10408 marked as offloadable too. */
10409 omp_context *octx;
10410 if (cgraph_node::get (current_function_decl)->offloadable)
10411 varpool_node::get_create (decl)->offloadable = 1;
10412 else
10413 for (octx = ctx->outer; octx; octx = octx->outer)
10414 if (is_gimple_omp_offloaded (octx->stmt))
10415 {
10416 varpool_node::get_create (decl)->offloadable = 1;
10417 break;
10418 }
10419
10420 lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_START);
10421 lock = build_call_expr_loc (loc, lock, 1,
10422 build_fold_addr_expr_loc (loc, decl));
10423
10424 unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_END);
10425 unlock = build_call_expr_loc (loc, unlock, 1,
10426 build_fold_addr_expr_loc (loc, decl));
10427 }
10428 else
10429 {
10430 lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_START);
10431 lock = build_call_expr_loc (loc, lock, 0);
10432
10433 unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_END);
10434 unlock = build_call_expr_loc (loc, unlock, 0);
10435 }
10436
10437 push_gimplify_context ();
10438
10439 block = make_node (BLOCK);
10440 bind = gimple_build_bind (NULL, NULL, block);
10441 gsi_replace (gsi_p, bind, true);
10442 gimple_bind_add_stmt (bind, stmt);
10443
10444 tbody = gimple_bind_body (bind);
10445 gimplify_and_add (lock, &tbody);
10446 gimple_bind_set_body (bind, tbody);
10447
10448 lower_omp (gimple_omp_body_ptr (stmt), ctx);
10449 gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
10450 gimple_bind_add_seq (bind, gimple_omp_body (stmt));
10451 gimple_omp_set_body (stmt, NULL);
10452
10453 tbody = gimple_bind_body (bind);
10454 gimplify_and_add (unlock, &tbody);
10455 gimple_bind_set_body (bind, tbody);
10456
10457 gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
10458
10459 pop_gimplify_context (bind);
10460 gimple_bind_append_vars (bind, ctx->block_vars);
10461 BLOCK_VARS (block) = gimple_bind_vars (bind);
10462 }
10463
10464 /* A subroutine of lower_omp_for. Generate code to emit the predicate
10465 for a lastprivate clause. Given a loop control predicate of (V
10466 cond N2), we gate the clause on (!(V cond N2)). The lowered form
10467 is appended to *DLIST, iterator initialization is appended to
10468 *BODY_P. *CLIST is for lastprivate(conditional:) code that needs
10469 to be emitted in a critical section. */
10470
10471 static void
10472 lower_omp_for_lastprivate (struct omp_for_data *fd, gimple_seq *body_p,
10473 gimple_seq *dlist, gimple_seq *clist,
10474 struct omp_context *ctx)
10475 {
10476 tree clauses, cond, vinit;
10477 enum tree_code cond_code;
10478 gimple_seq stmts;
10479
10480 cond_code = fd->loop.cond_code;
10481 cond_code = cond_code == LT_EXPR ? GE_EXPR : LE_EXPR;
10482
10483 /* When possible, use a strict equality expression. This can let VRP
10484 type optimizations deduce the value and remove a copy. */
10485 if (tree_fits_shwi_p (fd->loop.step))
10486 {
10487 HOST_WIDE_INT step = tree_to_shwi (fd->loop.step);
10488 if (step == 1 || step == -1)
10489 cond_code = EQ_EXPR;
10490 }
10491
10492 tree n2 = fd->loop.n2;
10493 if (fd->collapse > 1
10494 && TREE_CODE (n2) != INTEGER_CST
10495 && gimple_omp_for_combined_into_p (fd->for_stmt))
10496 {
10497 struct omp_context *taskreg_ctx = NULL;
10498 if (gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
10499 {
10500 gomp_for *gfor = as_a <gomp_for *> (ctx->outer->stmt);
10501 if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_FOR
10502 || gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_DISTRIBUTE)
10503 {
10504 if (gimple_omp_for_combined_into_p (gfor))
10505 {
10506 gcc_assert (ctx->outer->outer
10507 && is_parallel_ctx (ctx->outer->outer));
10508 taskreg_ctx = ctx->outer->outer;
10509 }
10510 else
10511 {
10512 struct omp_for_data outer_fd;
10513 omp_extract_for_data (gfor, &outer_fd, NULL);
10514 n2 = fold_convert (TREE_TYPE (n2), outer_fd.loop.n2);
10515 }
10516 }
10517 else if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_TASKLOOP)
10518 taskreg_ctx = ctx->outer->outer;
10519 }
10520 else if (is_taskreg_ctx (ctx->outer))
10521 taskreg_ctx = ctx->outer;
10522 if (taskreg_ctx)
10523 {
10524 int i;
10525 tree taskreg_clauses
10526 = gimple_omp_taskreg_clauses (taskreg_ctx->stmt);
10527 tree innerc = omp_find_clause (taskreg_clauses,
10528 OMP_CLAUSE__LOOPTEMP_);
10529 gcc_assert (innerc);
10530 int count = fd->collapse;
10531 if (fd->non_rect
10532 && fd->last_nonrect == fd->first_nonrect + 1)
10533 if (tree v = gimple_omp_for_index (fd->for_stmt, fd->last_nonrect))
10534 if (!TYPE_UNSIGNED (TREE_TYPE (v)))
10535 count += 4;
10536 for (i = 0; i < count; i++)
10537 {
10538 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
10539 OMP_CLAUSE__LOOPTEMP_);
10540 gcc_assert (innerc);
10541 }
10542 innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
10543 OMP_CLAUSE__LOOPTEMP_);
10544 if (innerc)
10545 n2 = fold_convert (TREE_TYPE (n2),
10546 lookup_decl (OMP_CLAUSE_DECL (innerc),
10547 taskreg_ctx));
10548 }
10549 }
10550 cond = build2 (cond_code, boolean_type_node, fd->loop.v, n2);
10551
10552 clauses = gimple_omp_for_clauses (fd->for_stmt);
10553 stmts = NULL;
10554 lower_lastprivate_clauses (clauses, cond, body_p, &stmts, clist, ctx);
10555 if (!gimple_seq_empty_p (stmts))
10556 {
10557 gimple_seq_add_seq (&stmts, *dlist);
10558 *dlist = stmts;
10559
10560 /* Optimize: v = 0; is usually cheaper than v = some_other_constant. */
10561 vinit = fd->loop.n1;
10562 if (cond_code == EQ_EXPR
10563 && tree_fits_shwi_p (fd->loop.n2)
10564 && ! integer_zerop (fd->loop.n2))
10565 vinit = build_int_cst (TREE_TYPE (fd->loop.v), 0);
10566 else
10567 vinit = unshare_expr (vinit);
10568
10569 /* Initialize the iterator variable, so that threads that don't execute
10570 any iterations don't execute the lastprivate clauses by accident. */
10571 gimplify_assign (fd->loop.v, vinit, body_p);
10572 }
10573 }
10574
10575 /* OpenACC privatization.
10576
10577 Or, in other words, *sharing* at the respective OpenACC level of
10578 parallelism.
10579
10580 From a correctness perspective, a non-addressable variable can't be accessed
10581 outside the current thread, so it can go in a (faster than shared memory)
10582 register -- though that register may need to be broadcast in some
10583 circumstances. A variable can only meaningfully be "shared" across workers
10584 or vector lanes if its address is taken, e.g. by a call to an atomic
10585 builtin.
10586
10587 From an optimisation perspective, the answer might be fuzzier: maybe
10588 sometimes, using shared memory directly would be faster than
10589 broadcasting. */
10590
10591 static void
10592 oacc_privatization_begin_diagnose_var (const dump_flags_t l_dump_flags,
10593 const location_t loc, const tree c,
10594 const tree decl)
10595 {
10596 const dump_user_location_t d_u_loc
10597 = dump_user_location_t::from_location_t (loc);
10598 /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
10599 #if __GNUC__ >= 10
10600 # pragma GCC diagnostic push
10601 # pragma GCC diagnostic ignored "-Wformat"
10602 #endif
10603 dump_printf_loc (l_dump_flags, d_u_loc,
10604 "variable %<%T%> ", decl);
10605 #if __GNUC__ >= 10
10606 # pragma GCC diagnostic pop
10607 #endif
10608 if (c)
10609 dump_printf (l_dump_flags,
10610 "in %qs clause ",
10611 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10612 else
10613 dump_printf (l_dump_flags,
10614 "declared in block ");
10615 }
10616
10617 static bool
10618 oacc_privatization_candidate_p (const location_t loc, const tree c,
10619 const tree decl)
10620 {
10621 dump_flags_t l_dump_flags = get_openacc_privatization_dump_flags ();
10622
10623 /* There is some differentiation depending on block vs. clause. */
10624 bool block = !c;
10625
10626 bool res = true;
10627
10628 if (res && !VAR_P (decl))
10629 {
10630 /* A PARM_DECL (appearing in a 'private' clause) is expected to have been
10631 privatized into a new VAR_DECL. */
10632 gcc_checking_assert (TREE_CODE (decl) != PARM_DECL);
10633
10634 res = false;
10635
10636 if (dump_enabled_p ())
10637 {
10638 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10639 dump_printf (l_dump_flags,
10640 "potentially has improper OpenACC privatization level: %qs\n",
10641 get_tree_code_name (TREE_CODE (decl)));
10642 }
10643 }
10644
10645 if (res && block && TREE_STATIC (decl))
10646 {
10647 res = false;
10648
10649 if (dump_enabled_p ())
10650 {
10651 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10652 dump_printf (l_dump_flags,
10653 "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
10654 "static");
10655 }
10656 }
10657
10658 if (res && block && DECL_EXTERNAL (decl))
10659 {
10660 res = false;
10661
10662 if (dump_enabled_p ())
10663 {
10664 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10665 dump_printf (l_dump_flags,
10666 "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
10667 "external");
10668 }
10669 }
10670
10671 if (res && !TREE_ADDRESSABLE (decl))
10672 {
10673 res = false;
10674
10675 if (dump_enabled_p ())
10676 {
10677 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10678 dump_printf (l_dump_flags,
10679 "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
10680 "not addressable");
10681 }
10682 }
10683
10684 /* If an artificial variable has been added to a bind, e.g.
10685 a compiler-generated temporary structure used by the Fortran front-end, do
10686 not consider it as a privatization candidate. Note that variables on
10687 the stack are private per-thread by default: making them "gang-private"
10688 for OpenACC actually means to share a single instance of a variable
10689 amongst all workers and threads spawned within each gang.
10690 At present, no compiler-generated artificial variables require such
10691 sharing semantics, so this is safe. */
10692
10693 if (res && block && DECL_ARTIFICIAL (decl))
10694 {
10695 res = false;
10696
10697 if (dump_enabled_p ())
10698 {
10699 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10700 dump_printf (l_dump_flags,
10701 "isn%'t candidate for adjusting OpenACC privatization "
10702 "level: %s\n", "artificial");
10703 }
10704 }
10705
10706 if (res)
10707 {
10708 if (dump_enabled_p ())
10709 {
10710 oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10711 dump_printf (l_dump_flags,
10712 "is candidate for adjusting OpenACC privatization level\n");
10713 }
10714 }
10715
10716 if (dump_file && (dump_flags & TDF_DETAILS))
10717 {
10718 print_generic_decl (dump_file, decl, dump_flags);
10719 fprintf (dump_file, "\n");
10720 }
10721
10722 return res;
10723 }
10724
10725 /* Scan CLAUSES for candidates for adjusting OpenACC privatization level in
10726 CTX. */
10727
10728 static void
10729 oacc_privatization_scan_clause_chain (omp_context *ctx, tree clauses)
10730 {
10731 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
10732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE)
10733 {
10734 tree decl = OMP_CLAUSE_DECL (c);
10735
10736 tree new_decl = lookup_decl (decl, ctx);
10737
10738 if (!oacc_privatization_candidate_p (OMP_CLAUSE_LOCATION (c), c,
10739 new_decl))
10740 continue;
10741
10742 gcc_checking_assert
10743 (!ctx->oacc_privatization_candidates.contains (new_decl));
10744 ctx->oacc_privatization_candidates.safe_push (new_decl);
10745 }
10746 }
10747
10748 /* Scan DECLS for candidates for adjusting OpenACC privatization level in
10749 CTX. */
10750
10751 static void
10752 oacc_privatization_scan_decl_chain (omp_context *ctx, tree decls)
10753 {
10754 for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
10755 {
10756 tree new_decl = lookup_decl (decl, ctx);
10757 gcc_checking_assert (new_decl == decl);
10758
10759 if (!oacc_privatization_candidate_p (gimple_location (ctx->stmt), NULL,
10760 new_decl))
10761 continue;
10762
10763 gcc_checking_assert
10764 (!ctx->oacc_privatization_candidates.contains (new_decl));
10765 ctx->oacc_privatization_candidates.safe_push (new_decl);
10766 }
10767 }
10768
10769 /* Callback for walk_gimple_seq. Find #pragma omp scan statement. */
10770
10771 static tree
10772 omp_find_scan (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
10773 struct walk_stmt_info *wi)
10774 {
10775 gimple *stmt = gsi_stmt (*gsi_p);
10776
10777 *handled_ops_p = true;
10778 switch (gimple_code (stmt))
10779 {
10780 WALK_SUBSTMTS;
10781
10782 case GIMPLE_OMP_FOR:
10783 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD
10784 && gimple_omp_for_combined_into_p (stmt))
10785 *handled_ops_p = false;
10786 break;
10787
10788 case GIMPLE_OMP_SCAN:
10789 *(gimple_stmt_iterator *) (wi->info) = *gsi_p;
10790 return integer_zero_node;
10791 default:
10792 break;
10793 }
10794 return NULL;
10795 }
10796
10797 /* Helper function for lower_omp_for, add transformations for a worksharing
10798 loop with scan directives inside of it.
10799 For worksharing loop not combined with simd, transform:
10800 #pragma omp for reduction(inscan,+:r) private(i)
10801 for (i = 0; i < n; i = i + 1)
10802 {
10803 {
10804 update (r);
10805 }
10806 #pragma omp scan inclusive(r)
10807 {
10808 use (r);
10809 }
10810 }
10811
10812 into two worksharing loops + code to merge results:
10813
10814 num_threads = omp_get_num_threads ();
10815 thread_num = omp_get_thread_num ();
10816 if (thread_num == 0) goto <D.2099>; else goto <D.2100>;
10817 <D.2099>:
10818 var2 = r;
10819 goto <D.2101>;
10820 <D.2100>:
10821 // For UDRs this is UDR init, or if ctors are needed, copy from
10822 // var3 that has been constructed to contain the neutral element.
10823 var2 = 0;
10824 <D.2101>:
10825 ivar = 0;
10826 // The _scantemp_ clauses will arrange for rpriva to be initialized to
10827 // a shared array with num_threads elements and rprivb to a local array
10828 // number of elements equal to the number of (contiguous) iterations the
10829 // current thread will perform. controlb and controlp variables are
10830 // temporaries to handle deallocation of rprivb at the end of second
10831 // GOMP_FOR.
10832 #pragma omp for _scantemp_(rpriva) _scantemp_(rprivb) _scantemp_(controlb) \
10833 _scantemp_(controlp) reduction(inscan,+:r) private(i) nowait
10834 for (i = 0; i < n; i = i + 1)
10835 {
10836 {
10837 // For UDRs this is UDR init or copy from var3.
10838 r = 0;
10839 // This is the input phase from user code.
10840 update (r);
10841 }
10842 {
10843 // For UDRs this is UDR merge.
10844 var2 = var2 + r;
10845 // Rather than handing it over to the user, save to local thread's
10846 // array.
10847 rprivb[ivar] = var2;
10848 // For exclusive scan, the above two statements are swapped.
10849 ivar = ivar + 1;
10850 }
10851 }
10852 // And remember the final value from this thread's into the shared
10853 // rpriva array.
10854 rpriva[(sizetype) thread_num] = var2;
10855 // If more than one thread, compute using Work-Efficient prefix sum
10856 // the inclusive parallel scan of the rpriva array.
10857 if (num_threads > 1) goto <D.2102>; else goto <D.2103>;
10858 <D.2102>:
10859 GOMP_barrier ();
10860 down = 0;
10861 k = 1;
10862 num_threadsu = (unsigned int) num_threads;
10863 thread_numup1 = (unsigned int) thread_num + 1;
10864 <D.2108>:
10865 twok = k << 1;
10866 if (twok > num_threadsu) goto <D.2110>; else goto <D.2111>;
10867 <D.2110>:
10868 down = 4294967295;
10869 k = k >> 1;
10870 if (k == num_threadsu) goto <D.2112>; else goto <D.2111>;
10871 <D.2112>:
10872 k = k >> 1;
10873 <D.2111>:
10874 twok = k << 1;
10875 cplx = .MUL_OVERFLOW (thread_nump1, twok);
10876 mul = REALPART_EXPR <cplx>;
10877 ovf = IMAGPART_EXPR <cplx>;
10878 if (ovf == 0) goto <D.2116>; else goto <D.2117>;
10879 <D.2116>:
10880 andv = k & down;
10881 andvm1 = andv + 4294967295;
10882 l = mul + andvm1;
10883 if (l < num_threadsu) goto <D.2120>; else goto <D.2117>;
10884 <D.2120>:
10885 // For UDRs this is UDR merge, performed using var2 variable as temporary,
10886 // i.e. var2 = rpriva[l - k]; UDR merge (var2, rpriva[l]); rpriva[l] = var2;
10887 rpriva[l] = rpriva[l - k] + rpriva[l];
10888 <D.2117>:
10889 if (down == 0) goto <D.2121>; else goto <D.2122>;
10890 <D.2121>:
10891 k = k << 1;
10892 goto <D.2123>;
10893 <D.2122>:
10894 k = k >> 1;
10895 <D.2123>:
10896 GOMP_barrier ();
10897 if (k != 0) goto <D.2108>; else goto <D.2103>;
10898 <D.2103>:
10899 if (thread_num == 0) goto <D.2124>; else goto <D.2125>;
10900 <D.2124>:
10901 // For UDRs this is UDR init or copy from var3.
10902 var2 = 0;
10903 goto <D.2126>;
10904 <D.2125>:
10905 var2 = rpriva[thread_num - 1];
10906 <D.2126>:
10907 ivar = 0;
10908 #pragma omp for _scantemp_(controlb) _scantemp_(controlp) \
10909 reduction(inscan,+:r) private(i)
10910 for (i = 0; i < n; i = i + 1)
10911 {
10912 {
10913 // For UDRs, this is r = var2; UDR merge (r, rprivb[ivar]);
10914 r = var2 + rprivb[ivar];
10915 }
10916 {
10917 // This is the scan phase from user code.
10918 use (r);
10919 // Plus a bump of the iterator.
10920 ivar = ivar + 1;
10921 }
10922 } */
10923
10924 static void
10925 lower_omp_for_scan (gimple_seq *body_p, gimple_seq *dlist, gomp_for *stmt,
10926 struct omp_for_data *fd, omp_context *ctx)
10927 {
10928 bool is_for_simd = gimple_omp_for_combined_p (stmt);
10929 gcc_assert (ctx->scan_inclusive || ctx->scan_exclusive);
10930
10931 gimple_seq body = gimple_omp_body (stmt);
10932 gimple_stmt_iterator input1_gsi = gsi_none ();
10933 struct walk_stmt_info wi;
10934 memset (&wi, 0, sizeof (wi));
10935 wi.val_only = true;
10936 wi.info = (void *) &input1_gsi;
10937 walk_gimple_seq_mod (&body, omp_find_scan, NULL, &wi);
10938 gcc_assert (!gsi_end_p (input1_gsi));
10939
10940 gimple *input_stmt1 = gsi_stmt (input1_gsi);
10941 gimple_stmt_iterator gsi = input1_gsi;
10942 gsi_next (&gsi);
10943 gimple_stmt_iterator scan1_gsi = gsi;
10944 gimple *scan_stmt1 = gsi_stmt (gsi);
10945 gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
10946
10947 gimple_seq input_body = gimple_omp_body (input_stmt1);
10948 gimple_seq scan_body = gimple_omp_body (scan_stmt1);
10949 gimple_omp_set_body (input_stmt1, NULL);
10950 gimple_omp_set_body (scan_stmt1, NULL);
10951 gimple_omp_set_body (stmt, NULL);
10952
10953 gomp_for *new_stmt = as_a <gomp_for *> (gimple_copy (stmt));
10954 gimple_seq new_body = copy_gimple_seq_and_replace_locals (body);
10955 gimple_omp_set_body (stmt, body);
10956 gimple_omp_set_body (input_stmt1, input_body);
10957
10958 gimple_stmt_iterator input2_gsi = gsi_none ();
10959 memset (&wi, 0, sizeof (wi));
10960 wi.val_only = true;
10961 wi.info = (void *) &input2_gsi;
10962 walk_gimple_seq_mod (&new_body, omp_find_scan, NULL, &wi);
10963 gcc_assert (!gsi_end_p (input2_gsi));
10964
10965 gimple *input_stmt2 = gsi_stmt (input2_gsi);
10966 gsi = input2_gsi;
10967 gsi_next (&gsi);
10968 gimple_stmt_iterator scan2_gsi = gsi;
10969 gimple *scan_stmt2 = gsi_stmt (gsi);
10970 gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
10971 gimple_omp_set_body (scan_stmt2, scan_body);
10972
10973 gimple_stmt_iterator input3_gsi = gsi_none ();
10974 gimple_stmt_iterator scan3_gsi = gsi_none ();
10975 gimple_stmt_iterator input4_gsi = gsi_none ();
10976 gimple_stmt_iterator scan4_gsi = gsi_none ();
10977 gimple *input_stmt3 = NULL, *scan_stmt3 = NULL;
10978 gimple *input_stmt4 = NULL, *scan_stmt4 = NULL;
10979 omp_context *input_simd_ctx = NULL, *scan_simd_ctx = NULL;
10980 if (is_for_simd)
10981 {
10982 memset (&wi, 0, sizeof (wi));
10983 wi.val_only = true;
10984 wi.info = (void *) &input3_gsi;
10985 walk_gimple_seq_mod (&input_body, omp_find_scan, NULL, &wi);
10986 gcc_assert (!gsi_end_p (input3_gsi));
10987
10988 input_stmt3 = gsi_stmt (input3_gsi);
10989 gsi = input3_gsi;
10990 gsi_next (&gsi);
10991 scan3_gsi = gsi;
10992 scan_stmt3 = gsi_stmt (gsi);
10993 gcc_assert (scan_stmt3 && gimple_code (scan_stmt3) == GIMPLE_OMP_SCAN);
10994
10995 memset (&wi, 0, sizeof (wi));
10996 wi.val_only = true;
10997 wi.info = (void *) &input4_gsi;
10998 walk_gimple_seq_mod (&scan_body, omp_find_scan, NULL, &wi);
10999 gcc_assert (!gsi_end_p (input4_gsi));
11000
11001 input_stmt4 = gsi_stmt (input4_gsi);
11002 gsi = input4_gsi;
11003 gsi_next (&gsi);
11004 scan4_gsi = gsi;
11005 scan_stmt4 = gsi_stmt (gsi);
11006 gcc_assert (scan_stmt4 && gimple_code (scan_stmt4) == GIMPLE_OMP_SCAN);
11007
11008 input_simd_ctx = maybe_lookup_ctx (input_stmt3)->outer;
11009 scan_simd_ctx = maybe_lookup_ctx (input_stmt4)->outer;
11010 }
11011
11012 tree num_threads = create_tmp_var (integer_type_node);
11013 tree thread_num = create_tmp_var (integer_type_node);
11014 tree nthreads_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
11015 tree threadnum_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
11016 gimple *g = gimple_build_call (nthreads_decl, 0);
11017 gimple_call_set_lhs (g, num_threads);
11018 gimple_seq_add_stmt (body_p, g);
11019 g = gimple_build_call (threadnum_decl, 0);
11020 gimple_call_set_lhs (g, thread_num);
11021 gimple_seq_add_stmt (body_p, g);
11022
11023 tree ivar = create_tmp_var (sizetype);
11024 tree new_clauses1 = NULL_TREE, new_clauses2 = NULL_TREE;
11025 tree *cp1 = &new_clauses1, *cp2 = &new_clauses2;
11026 tree k = create_tmp_var (unsigned_type_node);
11027 tree l = create_tmp_var (unsigned_type_node);
11028
11029 gimple_seq clist = NULL, mdlist = NULL;
11030 gimple_seq thr01_list = NULL, thrn1_list = NULL;
11031 gimple_seq thr02_list = NULL, thrn2_list = NULL;
11032 gimple_seq scan1_list = NULL, input2_list = NULL;
11033 gimple_seq last_list = NULL, reduc_list = NULL;
11034 for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
11035 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11036 && OMP_CLAUSE_REDUCTION_INSCAN (c))
11037 {
11038 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
11039 tree var = OMP_CLAUSE_DECL (c);
11040 tree new_var = lookup_decl (var, ctx);
11041 tree var3 = NULL_TREE;
11042 tree new_vard = new_var;
11043 if (omp_privatize_by_reference (var))
11044 new_var = build_simple_mem_ref_loc (clause_loc, new_var);
11045 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11046 {
11047 var3 = maybe_lookup_decl (new_vard, ctx);
11048 if (var3 == new_vard)
11049 var3 = NULL_TREE;
11050 }
11051
11052 tree ptype = build_pointer_type (TREE_TYPE (new_var));
11053 tree rpriva = create_tmp_var (ptype);
11054 tree nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
11055 OMP_CLAUSE_DECL (nc) = rpriva;
11056 *cp1 = nc;
11057 cp1 = &OMP_CLAUSE_CHAIN (nc);
11058
11059 tree rprivb = create_tmp_var (ptype);
11060 nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
11061 OMP_CLAUSE_DECL (nc) = rprivb;
11062 OMP_CLAUSE__SCANTEMP__ALLOC (nc) = 1;
11063 *cp1 = nc;
11064 cp1 = &OMP_CLAUSE_CHAIN (nc);
11065
11066 tree var2 = create_tmp_var_raw (TREE_TYPE (new_var));
11067 if (new_vard != new_var)
11068 TREE_ADDRESSABLE (var2) = 1;
11069 gimple_add_tmp_var (var2);
11070
11071 tree x = fold_convert_loc (clause_loc, sizetype, thread_num);
11072 x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11073 TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11074 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11075 tree rpriva_ref = build_simple_mem_ref_loc (clause_loc, x);
11076
11077 x = fold_build2_loc (clause_loc, PLUS_EXPR, integer_type_node,
11078 thread_num, integer_minus_one_node);
11079 x = fold_convert_loc (clause_loc, sizetype, x);
11080 x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11081 TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11082 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11083 tree rprivam1_ref = build_simple_mem_ref_loc (clause_loc, x);
11084
11085 x = fold_convert_loc (clause_loc, sizetype, l);
11086 x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11087 TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11088 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11089 tree rprival_ref = build_simple_mem_ref_loc (clause_loc, x);
11090
11091 x = fold_build2_loc (clause_loc, MINUS_EXPR, unsigned_type_node, l, k);
11092 x = fold_convert_loc (clause_loc, sizetype, x);
11093 x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11094 TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11095 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11096 tree rprivalmk_ref = build_simple_mem_ref_loc (clause_loc, x);
11097
11098 x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, ivar,
11099 TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11100 x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rprivb), rprivb, x);
11101 tree rprivb_ref = build_simple_mem_ref_loc (clause_loc, x);
11102
11103 tree var4 = is_for_simd ? new_var : var2;
11104 tree var5 = NULL_TREE, var6 = NULL_TREE;
11105 if (is_for_simd)
11106 {
11107 var5 = lookup_decl (var, input_simd_ctx);
11108 var6 = lookup_decl (var, scan_simd_ctx);
11109 if (new_vard != new_var)
11110 {
11111 var5 = build_simple_mem_ref_loc (clause_loc, var5);
11112 var6 = build_simple_mem_ref_loc (clause_loc, var6);
11113 }
11114 }
11115 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11116 {
11117 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11118 tree val = var2;
11119
11120 x = lang_hooks.decls.omp_clause_default_ctor
11121 (c, var2, build_outer_var_ref (var, ctx));
11122 if (x)
11123 gimplify_and_add (x, &clist);
11124
11125 x = build_outer_var_ref (var, ctx);
11126 x = lang_hooks.decls.omp_clause_assign_op (c, unshare_expr (var4),
11127 x);
11128 gimplify_and_add (x, &thr01_list);
11129
11130 tree y = (DECL_HAS_VALUE_EXPR_P (new_vard)
11131 ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
11132 if (var3)
11133 {
11134 x = unshare_expr (var4);
11135 x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
11136 gimplify_and_add (x, &thrn1_list);
11137 x = unshare_expr (var4);
11138 x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
11139 gimplify_and_add (x, &thr02_list);
11140 }
11141 else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
11142 {
11143 /* Otherwise, assign to it the identity element. */
11144 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
11145 tseq = copy_gimple_seq_and_replace_locals (tseq);
11146 if (!is_for_simd)
11147 {
11148 if (new_vard != new_var)
11149 val = build_fold_addr_expr_loc (clause_loc, val);
11150 SET_DECL_VALUE_EXPR (new_vard, val);
11151 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11152 }
11153 SET_DECL_VALUE_EXPR (placeholder, error_mark_node);
11154 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11155 lower_omp (&tseq, ctx);
11156 gimple_seq_add_seq (&thrn1_list, tseq);
11157 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
11158 lower_omp (&tseq, ctx);
11159 gimple_seq_add_seq (&thr02_list, tseq);
11160 SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
11161 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11162 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
11163 if (y)
11164 SET_DECL_VALUE_EXPR (new_vard, y);
11165 else
11166 {
11167 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11168 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11169 }
11170 }
11171
11172 x = unshare_expr (var4);
11173 x = lang_hooks.decls.omp_clause_assign_op (c, x, rprivam1_ref);
11174 gimplify_and_add (x, &thrn2_list);
11175
11176 if (is_for_simd)
11177 {
11178 x = unshare_expr (rprivb_ref);
11179 x = lang_hooks.decls.omp_clause_assign_op (c, x, var5);
11180 gimplify_and_add (x, &scan1_list);
11181 }
11182 else
11183 {
11184 if (ctx->scan_exclusive)
11185 {
11186 x = unshare_expr (rprivb_ref);
11187 x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
11188 gimplify_and_add (x, &scan1_list);
11189 }
11190
11191 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11192 tseq = copy_gimple_seq_and_replace_locals (tseq);
11193 SET_DECL_VALUE_EXPR (placeholder, var2);
11194 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11195 lower_omp (&tseq, ctx);
11196 gimple_seq_add_seq (&scan1_list, tseq);
11197
11198 if (ctx->scan_inclusive)
11199 {
11200 x = unshare_expr (rprivb_ref);
11201 x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
11202 gimplify_and_add (x, &scan1_list);
11203 }
11204 }
11205
11206 x = unshare_expr (rpriva_ref);
11207 x = lang_hooks.decls.omp_clause_assign_op (c, x,
11208 unshare_expr (var4));
11209 gimplify_and_add (x, &mdlist);
11210
11211 x = unshare_expr (is_for_simd ? var6 : new_var);
11212 x = lang_hooks.decls.omp_clause_assign_op (c, x, var4);
11213 gimplify_and_add (x, &input2_list);
11214
11215 val = rprivb_ref;
11216 if (new_vard != new_var)
11217 val = build_fold_addr_expr_loc (clause_loc, val);
11218
11219 gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11220 tseq = copy_gimple_seq_and_replace_locals (tseq);
11221 SET_DECL_VALUE_EXPR (new_vard, val);
11222 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11223 if (is_for_simd)
11224 {
11225 SET_DECL_VALUE_EXPR (placeholder, var6);
11226 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11227 }
11228 else
11229 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11230 lower_omp (&tseq, ctx);
11231 if (y)
11232 SET_DECL_VALUE_EXPR (new_vard, y);
11233 else
11234 {
11235 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11236 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11237 }
11238 if (!is_for_simd)
11239 {
11240 SET_DECL_VALUE_EXPR (placeholder, new_var);
11241 DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11242 lower_omp (&tseq, ctx);
11243 }
11244 gimple_seq_add_seq (&input2_list, tseq);
11245
11246 x = build_outer_var_ref (var, ctx);
11247 x = lang_hooks.decls.omp_clause_assign_op (c, x, rpriva_ref);
11248 gimplify_and_add (x, &last_list);
11249
11250 x = lang_hooks.decls.omp_clause_assign_op (c, var2, rprivalmk_ref);
11251 gimplify_and_add (x, &reduc_list);
11252 tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11253 tseq = copy_gimple_seq_and_replace_locals (tseq);
11254 val = rprival_ref;
11255 if (new_vard != new_var)
11256 val = build_fold_addr_expr_loc (clause_loc, val);
11257 SET_DECL_VALUE_EXPR (new_vard, val);
11258 DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11259 SET_DECL_VALUE_EXPR (placeholder, var2);
11260 lower_omp (&tseq, ctx);
11261 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
11262 SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
11263 DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11264 if (y)
11265 SET_DECL_VALUE_EXPR (new_vard, y);
11266 else
11267 {
11268 DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11269 SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11270 }
11271 gimple_seq_add_seq (&reduc_list, tseq);
11272 x = lang_hooks.decls.omp_clause_assign_op (c, rprival_ref, var2);
11273 gimplify_and_add (x, &reduc_list);
11274
11275 x = lang_hooks.decls.omp_clause_dtor (c, var2);
11276 if (x)
11277 gimplify_and_add (x, dlist);
11278 }
11279 else
11280 {
11281 x = build_outer_var_ref (var, ctx);
11282 gimplify_assign (unshare_expr (var4), x, &thr01_list);
11283
11284 x = omp_reduction_init (c, TREE_TYPE (new_var));
11285 gimplify_assign (unshare_expr (var4), unshare_expr (x),
11286 &thrn1_list);
11287 gimplify_assign (unshare_expr (var4), x, &thr02_list);
11288
11289 gimplify_assign (unshare_expr (var4), rprivam1_ref, &thrn2_list);
11290
11291 enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
11292 if (code == MINUS_EXPR)
11293 code = PLUS_EXPR;
11294
11295 if (is_for_simd)
11296 gimplify_assign (unshare_expr (rprivb_ref), var5, &scan1_list);
11297 else
11298 {
11299 if (ctx->scan_exclusive)
11300 gimplify_assign (unshare_expr (rprivb_ref), var2,
11301 &scan1_list);
11302 x = build2 (code, TREE_TYPE (new_var), var2, new_var);
11303 gimplify_assign (var2, x, &scan1_list);
11304 if (ctx->scan_inclusive)
11305 gimplify_assign (unshare_expr (rprivb_ref), var2,
11306 &scan1_list);
11307 }
11308
11309 gimplify_assign (unshare_expr (rpriva_ref), unshare_expr (var4),
11310 &mdlist);
11311
11312 x = build2 (code, TREE_TYPE (new_var), var4, rprivb_ref);
11313 gimplify_assign (is_for_simd ? var6 : new_var, x, &input2_list);
11314
11315 gimplify_assign (build_outer_var_ref (var, ctx), rpriva_ref,
11316 &last_list);
11317
11318 x = build2 (code, TREE_TYPE (new_var), rprivalmk_ref,
11319 unshare_expr (rprival_ref));
11320 gimplify_assign (rprival_ref, x, &reduc_list);
11321 }
11322 }
11323
11324 g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
11325 gimple_seq_add_stmt (&scan1_list, g);
11326 g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
11327 gimple_seq_add_stmt (gimple_omp_body_ptr (is_for_simd
11328 ? scan_stmt4 : scan_stmt2), g);
11329
11330 tree controlb = create_tmp_var (boolean_type_node);
11331 tree controlp = create_tmp_var (ptr_type_node);
11332 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11333 OMP_CLAUSE_DECL (nc) = controlb;
11334 OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11335 *cp1 = nc;
11336 cp1 = &OMP_CLAUSE_CHAIN (nc);
11337 nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11338 OMP_CLAUSE_DECL (nc) = controlp;
11339 OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11340 *cp1 = nc;
11341 cp1 = &OMP_CLAUSE_CHAIN (nc);
11342 nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11343 OMP_CLAUSE_DECL (nc) = controlb;
11344 OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11345 *cp2 = nc;
11346 cp2 = &OMP_CLAUSE_CHAIN (nc);
11347 nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11348 OMP_CLAUSE_DECL (nc) = controlp;
11349 OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11350 *cp2 = nc;
11351 cp2 = &OMP_CLAUSE_CHAIN (nc);
11352
11353 *cp1 = gimple_omp_for_clauses (stmt);
11354 gimple_omp_for_set_clauses (stmt, new_clauses1);
11355 *cp2 = gimple_omp_for_clauses (new_stmt);
11356 gimple_omp_for_set_clauses (new_stmt, new_clauses2);
11357
11358 if (is_for_simd)
11359 {
11360 gimple_seq_add_seq (gimple_omp_body_ptr (scan_stmt3), scan1_list);
11361 gimple_seq_add_seq (gimple_omp_body_ptr (input_stmt4), input2_list);
11362
11363 gsi_insert_seq_after (&input3_gsi, gimple_omp_body (input_stmt3),
11364 GSI_SAME_STMT);
11365 gsi_remove (&input3_gsi, true);
11366 gsi_insert_seq_after (&scan3_gsi, gimple_omp_body (scan_stmt3),
11367 GSI_SAME_STMT);
11368 gsi_remove (&scan3_gsi, true);
11369 gsi_insert_seq_after (&input4_gsi, gimple_omp_body (input_stmt4),
11370 GSI_SAME_STMT);
11371 gsi_remove (&input4_gsi, true);
11372 gsi_insert_seq_after (&scan4_gsi, gimple_omp_body (scan_stmt4),
11373 GSI_SAME_STMT);
11374 gsi_remove (&scan4_gsi, true);
11375 }
11376 else
11377 {
11378 gimple_omp_set_body (scan_stmt1, scan1_list);
11379 gimple_omp_set_body (input_stmt2, input2_list);
11380 }
11381
11382 gsi_insert_seq_after (&input1_gsi, gimple_omp_body (input_stmt1),
11383 GSI_SAME_STMT);
11384 gsi_remove (&input1_gsi, true);
11385 gsi_insert_seq_after (&scan1_gsi, gimple_omp_body (scan_stmt1),
11386 GSI_SAME_STMT);
11387 gsi_remove (&scan1_gsi, true);
11388 gsi_insert_seq_after (&input2_gsi, gimple_omp_body (input_stmt2),
11389 GSI_SAME_STMT);
11390 gsi_remove (&input2_gsi, true);
11391 gsi_insert_seq_after (&scan2_gsi, gimple_omp_body (scan_stmt2),
11392 GSI_SAME_STMT);
11393 gsi_remove (&scan2_gsi, true);
11394
11395 gimple_seq_add_seq (body_p, clist);
11396
11397 tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
11398 tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
11399 tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
11400 g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
11401 gimple_seq_add_stmt (body_p, g);
11402 g = gimple_build_label (lab1);
11403 gimple_seq_add_stmt (body_p, g);
11404 gimple_seq_add_seq (body_p, thr01_list);
11405 g = gimple_build_goto (lab3);
11406 gimple_seq_add_stmt (body_p, g);
11407 g = gimple_build_label (lab2);
11408 gimple_seq_add_stmt (body_p, g);
11409 gimple_seq_add_seq (body_p, thrn1_list);
11410 g = gimple_build_label (lab3);
11411 gimple_seq_add_stmt (body_p, g);
11412
11413 g = gimple_build_assign (ivar, size_zero_node);
11414 gimple_seq_add_stmt (body_p, g);
11415
11416 gimple_seq_add_stmt (body_p, stmt);
11417 gimple_seq_add_seq (body_p, body);
11418 gimple_seq_add_stmt (body_p, gimple_build_omp_continue (fd->loop.v,
11419 fd->loop.v));
11420
11421 g = gimple_build_omp_return (true);
11422 gimple_seq_add_stmt (body_p, g);
11423 gimple_seq_add_seq (body_p, mdlist);
11424
11425 lab1 = create_artificial_label (UNKNOWN_LOCATION);
11426 lab2 = create_artificial_label (UNKNOWN_LOCATION);
11427 g = gimple_build_cond (GT_EXPR, num_threads, integer_one_node, lab1, lab2);
11428 gimple_seq_add_stmt (body_p, g);
11429 g = gimple_build_label (lab1);
11430 gimple_seq_add_stmt (body_p, g);
11431
11432 g = omp_build_barrier (NULL);
11433 gimple_seq_add_stmt (body_p, g);
11434
11435 tree down = create_tmp_var (unsigned_type_node);
11436 g = gimple_build_assign (down, build_zero_cst (unsigned_type_node));
11437 gimple_seq_add_stmt (body_p, g);
11438
11439 g = gimple_build_assign (k, build_one_cst (unsigned_type_node));
11440 gimple_seq_add_stmt (body_p, g);
11441
11442 tree num_threadsu = create_tmp_var (unsigned_type_node);
11443 g = gimple_build_assign (num_threadsu, NOP_EXPR, num_threads);
11444 gimple_seq_add_stmt (body_p, g);
11445
11446 tree thread_numu = create_tmp_var (unsigned_type_node);
11447 g = gimple_build_assign (thread_numu, NOP_EXPR, thread_num);
11448 gimple_seq_add_stmt (body_p, g);
11449
11450 tree thread_nump1 = create_tmp_var (unsigned_type_node);
11451 g = gimple_build_assign (thread_nump1, PLUS_EXPR, thread_numu,
11452 build_int_cst (unsigned_type_node, 1));
11453 gimple_seq_add_stmt (body_p, g);
11454
11455 lab3 = create_artificial_label (UNKNOWN_LOCATION);
11456 g = gimple_build_label (lab3);
11457 gimple_seq_add_stmt (body_p, g);
11458
11459 tree twok = create_tmp_var (unsigned_type_node);
11460 g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
11461 gimple_seq_add_stmt (body_p, g);
11462
11463 tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
11464 tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
11465 tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
11466 g = gimple_build_cond (GT_EXPR, twok, num_threadsu, lab4, lab5);
11467 gimple_seq_add_stmt (body_p, g);
11468 g = gimple_build_label (lab4);
11469 gimple_seq_add_stmt (body_p, g);
11470 g = gimple_build_assign (down, build_all_ones_cst (unsigned_type_node));
11471 gimple_seq_add_stmt (body_p, g);
11472 g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11473 gimple_seq_add_stmt (body_p, g);
11474
11475 g = gimple_build_cond (EQ_EXPR, k, num_threadsu, lab6, lab5);
11476 gimple_seq_add_stmt (body_p, g);
11477 g = gimple_build_label (lab6);
11478 gimple_seq_add_stmt (body_p, g);
11479
11480 g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11481 gimple_seq_add_stmt (body_p, g);
11482
11483 g = gimple_build_label (lab5);
11484 gimple_seq_add_stmt (body_p, g);
11485
11486 g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
11487 gimple_seq_add_stmt (body_p, g);
11488
11489 tree cplx = create_tmp_var (build_complex_type (unsigned_type_node, false));
11490 g = gimple_build_call_internal (IFN_MUL_OVERFLOW, 2, thread_nump1, twok);
11491 gimple_call_set_lhs (g, cplx);
11492 gimple_seq_add_stmt (body_p, g);
11493 tree mul = create_tmp_var (unsigned_type_node);
11494 g = gimple_build_assign (mul, REALPART_EXPR,
11495 build1 (REALPART_EXPR, unsigned_type_node, cplx));
11496 gimple_seq_add_stmt (body_p, g);
11497 tree ovf = create_tmp_var (unsigned_type_node);
11498 g = gimple_build_assign (ovf, IMAGPART_EXPR,
11499 build1 (IMAGPART_EXPR, unsigned_type_node, cplx));
11500 gimple_seq_add_stmt (body_p, g);
11501
11502 tree lab7 = create_artificial_label (UNKNOWN_LOCATION);
11503 tree lab8 = create_artificial_label (UNKNOWN_LOCATION);
11504 g = gimple_build_cond (EQ_EXPR, ovf, build_zero_cst (unsigned_type_node),
11505 lab7, lab8);
11506 gimple_seq_add_stmt (body_p, g);
11507 g = gimple_build_label (lab7);
11508 gimple_seq_add_stmt (body_p, g);
11509
11510 tree andv = create_tmp_var (unsigned_type_node);
11511 g = gimple_build_assign (andv, BIT_AND_EXPR, k, down);
11512 gimple_seq_add_stmt (body_p, g);
11513 tree andvm1 = create_tmp_var (unsigned_type_node);
11514 g = gimple_build_assign (andvm1, PLUS_EXPR, andv,
11515 build_minus_one_cst (unsigned_type_node));
11516 gimple_seq_add_stmt (body_p, g);
11517
11518 g = gimple_build_assign (l, PLUS_EXPR, mul, andvm1);
11519 gimple_seq_add_stmt (body_p, g);
11520
11521 tree lab9 = create_artificial_label (UNKNOWN_LOCATION);
11522 g = gimple_build_cond (LT_EXPR, l, num_threadsu, lab9, lab8);
11523 gimple_seq_add_stmt (body_p, g);
11524 g = gimple_build_label (lab9);
11525 gimple_seq_add_stmt (body_p, g);
11526 gimple_seq_add_seq (body_p, reduc_list);
11527 g = gimple_build_label (lab8);
11528 gimple_seq_add_stmt (body_p, g);
11529
11530 tree lab10 = create_artificial_label (UNKNOWN_LOCATION);
11531 tree lab11 = create_artificial_label (UNKNOWN_LOCATION);
11532 tree lab12 = create_artificial_label (UNKNOWN_LOCATION);
11533 g = gimple_build_cond (EQ_EXPR, down, build_zero_cst (unsigned_type_node),
11534 lab10, lab11);
11535 gimple_seq_add_stmt (body_p, g);
11536 g = gimple_build_label (lab10);
11537 gimple_seq_add_stmt (body_p, g);
11538 g = gimple_build_assign (k, LSHIFT_EXPR, k, integer_one_node);
11539 gimple_seq_add_stmt (body_p, g);
11540 g = gimple_build_goto (lab12);
11541 gimple_seq_add_stmt (body_p, g);
11542 g = gimple_build_label (lab11);
11543 gimple_seq_add_stmt (body_p, g);
11544 g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11545 gimple_seq_add_stmt (body_p, g);
11546 g = gimple_build_label (lab12);
11547 gimple_seq_add_stmt (body_p, g);
11548
11549 g = omp_build_barrier (NULL);
11550 gimple_seq_add_stmt (body_p, g);
11551
11552 g = gimple_build_cond (NE_EXPR, k, build_zero_cst (unsigned_type_node),
11553 lab3, lab2);
11554 gimple_seq_add_stmt (body_p, g);
11555
11556 g = gimple_build_label (lab2);
11557 gimple_seq_add_stmt (body_p, g);
11558
11559 lab1 = create_artificial_label (UNKNOWN_LOCATION);
11560 lab2 = create_artificial_label (UNKNOWN_LOCATION);
11561 lab3 = create_artificial_label (UNKNOWN_LOCATION);
11562 g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
11563 gimple_seq_add_stmt (body_p, g);
11564 g = gimple_build_label (lab1);
11565 gimple_seq_add_stmt (body_p, g);
11566 gimple_seq_add_seq (body_p, thr02_list);
11567 g = gimple_build_goto (lab3);
11568 gimple_seq_add_stmt (body_p, g);
11569 g = gimple_build_label (lab2);
11570 gimple_seq_add_stmt (body_p, g);
11571 gimple_seq_add_seq (body_p, thrn2_list);
11572 g = gimple_build_label (lab3);
11573 gimple_seq_add_stmt (body_p, g);
11574
11575 g = gimple_build_assign (ivar, size_zero_node);
11576 gimple_seq_add_stmt (body_p, g);
11577 gimple_seq_add_stmt (body_p, new_stmt);
11578 gimple_seq_add_seq (body_p, new_body);
11579
11580 gimple_seq new_dlist = NULL;
11581 lab1 = create_artificial_label (UNKNOWN_LOCATION);
11582 lab2 = create_artificial_label (UNKNOWN_LOCATION);
11583 tree num_threadsm1 = create_tmp_var (integer_type_node);
11584 g = gimple_build_assign (num_threadsm1, PLUS_EXPR, num_threads,
11585 integer_minus_one_node);
11586 gimple_seq_add_stmt (&new_dlist, g);
11587 g = gimple_build_cond (EQ_EXPR, thread_num, num_threadsm1, lab1, lab2);
11588 gimple_seq_add_stmt (&new_dlist, g);
11589 g = gimple_build_label (lab1);
11590 gimple_seq_add_stmt (&new_dlist, g);
11591 gimple_seq_add_seq (&new_dlist, last_list);
11592 g = gimple_build_label (lab2);
11593 gimple_seq_add_stmt (&new_dlist, g);
11594 gimple_seq_add_seq (&new_dlist, *dlist);
11595 *dlist = new_dlist;
11596 }
11597
11598 /* Build an internal UNIQUE function with type IFN_UNIQUE_OACC_PRIVATE listing
11599 the addresses of variables to be made private at the surrounding
11600 parallelism level. Such functions appear in the gimple code stream in two
11601 forms, e.g. for a partitioned loop:
11602
11603 .data_dep.6 = .UNIQUE (OACC_HEAD_MARK, .data_dep.6, 1, 68);
11604 .data_dep.6 = .UNIQUE (OACC_PRIVATE, .data_dep.6, -1, &w);
11605 .data_dep.6 = .UNIQUE (OACC_FORK, .data_dep.6, -1);
11606 .data_dep.6 = .UNIQUE (OACC_HEAD_MARK, .data_dep.6);
11607
11608 or alternatively, OACC_PRIVATE can appear at the top level of a parallel,
11609 not as part of a HEAD_MARK sequence:
11610
11611 .UNIQUE (OACC_PRIVATE, 0, 0, &w);
11612
11613 For such stand-alone appearances, the 3rd argument is always 0, denoting
11614 gang partitioning. */
11615
11616 static gcall *
11617 lower_oacc_private_marker (omp_context *ctx)
11618 {
11619 if (ctx->oacc_privatization_candidates.length () == 0)
11620 return NULL;
11621
11622 auto_vec<tree, 5> args;
11623
11624 args.quick_push (build_int_cst (integer_type_node, IFN_UNIQUE_OACC_PRIVATE));
11625 args.quick_push (integer_zero_node);
11626 args.quick_push (integer_minus_one_node);
11627
11628 int i;
11629 tree decl;
11630 FOR_EACH_VEC_ELT (ctx->oacc_privatization_candidates, i, decl)
11631 {
11632 gcc_checking_assert (TREE_ADDRESSABLE (decl));
11633 tree addr = build_fold_addr_expr (decl);
11634 args.safe_push (addr);
11635 }
11636
11637 return gimple_build_call_internal_vec (IFN_UNIQUE, args);
11638 }
11639
11640 /* Lower code for an OMP loop directive. */
11641
11642 static void
11643 lower_omp_for (gimple_stmt_iterator *gsi_p, omp_context *ctx)
11644 {
11645 tree *rhs_p, block;
11646 struct omp_for_data fd, *fdp = NULL;
11647 gomp_for *stmt = as_a <gomp_for *> (gsi_stmt (*gsi_p));
11648 gbind *new_stmt;
11649 gimple_seq omp_for_body, body, dlist, tred_ilist = NULL, tred_dlist = NULL;
11650 gimple_seq cnt_list = NULL, clist = NULL;
11651 gimple_seq oacc_head = NULL, oacc_tail = NULL;
11652 size_t i;
11653
11654 push_gimplify_context ();
11655
11656 if (is_gimple_omp_oacc (ctx->stmt))
11657 oacc_privatization_scan_clause_chain (ctx, gimple_omp_for_clauses (stmt));
11658
11659 lower_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
11660
11661 block = make_node (BLOCK);
11662 new_stmt = gimple_build_bind (NULL, NULL, block);
11663 /* Replace at gsi right away, so that 'stmt' is no member
11664 of a sequence anymore as we're going to add to a different
11665 one below. */
11666 gsi_replace (gsi_p, new_stmt, true);
11667
11668 /* Move declaration of temporaries in the loop body before we make
11669 it go away. */
11670 omp_for_body = gimple_omp_body (stmt);
11671 if (!gimple_seq_empty_p (omp_for_body)
11672 && gimple_code (gimple_seq_first_stmt (omp_for_body)) == GIMPLE_BIND)
11673 {
11674 gbind *inner_bind
11675 = as_a <gbind *> (gimple_seq_first_stmt (omp_for_body));
11676 tree vars = gimple_bind_vars (inner_bind);
11677 if (is_gimple_omp_oacc (ctx->stmt))
11678 oacc_privatization_scan_decl_chain (ctx, vars);
11679 gimple_bind_append_vars (new_stmt, vars);
11680 /* bind_vars/BLOCK_VARS are being moved to new_stmt/block, don't
11681 keep them on the inner_bind and it's block. */
11682 gimple_bind_set_vars (inner_bind, NULL_TREE);
11683 if (gimple_bind_block (inner_bind))
11684 BLOCK_VARS (gimple_bind_block (inner_bind)) = NULL_TREE;
11685 }
11686
11687 if (gimple_omp_for_combined_into_p (stmt))
11688 {
11689 omp_extract_for_data (stmt, &fd, NULL);
11690 fdp = &fd;
11691
11692 /* We need two temporaries with fd.loop.v type (istart/iend)
11693 and then (fd.collapse - 1) temporaries with the same
11694 type for count2 ... countN-1 vars if not constant. */
11695 size_t count = 2;
11696 tree type = fd.iter_type;
11697 if (fd.collapse > 1
11698 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
11699 count += fd.collapse - 1;
11700 size_t count2 = 0;
11701 tree type2 = NULL_TREE;
11702 bool taskreg_for
11703 = (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
11704 || gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_TASKLOOP);
11705 tree outerc = NULL, *pc = gimple_omp_for_clauses_ptr (stmt);
11706 tree simtc = NULL;
11707 tree clauses = *pc;
11708 if (fd.collapse > 1
11709 && fd.non_rect
11710 && fd.last_nonrect == fd.first_nonrect + 1
11711 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
11712 if (tree v = gimple_omp_for_index (stmt, fd.last_nonrect))
11713 if (!TYPE_UNSIGNED (TREE_TYPE (v)))
11714 {
11715 v = gimple_omp_for_index (stmt, fd.first_nonrect);
11716 type2 = TREE_TYPE (v);
11717 count++;
11718 count2 = 3;
11719 }
11720 if (taskreg_for)
11721 outerc
11722 = omp_find_clause (gimple_omp_taskreg_clauses (ctx->outer->stmt),
11723 OMP_CLAUSE__LOOPTEMP_);
11724 if (ctx->simt_stmt)
11725 simtc = omp_find_clause (gimple_omp_for_clauses (ctx->simt_stmt),
11726 OMP_CLAUSE__LOOPTEMP_);
11727 for (i = 0; i < count + count2; i++)
11728 {
11729 tree temp;
11730 if (taskreg_for)
11731 {
11732 gcc_assert (outerc);
11733 temp = lookup_decl (OMP_CLAUSE_DECL (outerc), ctx->outer);
11734 outerc = omp_find_clause (OMP_CLAUSE_CHAIN (outerc),
11735 OMP_CLAUSE__LOOPTEMP_);
11736 }
11737 else
11738 {
11739 /* If there are 2 adjacent SIMD stmts, one with _simt_
11740 clause, another without, make sure they have the same
11741 decls in _looptemp_ clauses, because the outer stmt
11742 they are combined into will look up just one inner_stmt. */
11743 if (ctx->simt_stmt)
11744 temp = OMP_CLAUSE_DECL (simtc);
11745 else
11746 temp = create_tmp_var (i >= count ? type2 : type);
11747 insert_decl_map (&ctx->outer->cb, temp, temp);
11748 }
11749 *pc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
11750 OMP_CLAUSE_DECL (*pc) = temp;
11751 pc = &OMP_CLAUSE_CHAIN (*pc);
11752 if (ctx->simt_stmt)
11753 simtc = omp_find_clause (OMP_CLAUSE_CHAIN (simtc),
11754 OMP_CLAUSE__LOOPTEMP_);
11755 }
11756 *pc = clauses;
11757 }
11758
11759 /* The pre-body and input clauses go before the lowered GIMPLE_OMP_FOR. */
11760 dlist = NULL;
11761 body = NULL;
11762 tree rclauses
11763 = omp_task_reductions_find_first (gimple_omp_for_clauses (stmt), OMP_FOR,
11764 OMP_CLAUSE_REDUCTION);
11765 tree rtmp = NULL_TREE;
11766 if (rclauses)
11767 {
11768 tree type = build_pointer_type (pointer_sized_int_node);
11769 tree temp = create_tmp_var (type);
11770 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
11771 OMP_CLAUSE_DECL (c) = temp;
11772 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (stmt);
11773 gimple_omp_for_set_clauses (stmt, c);
11774 lower_omp_task_reductions (ctx, OMP_FOR,
11775 gimple_omp_for_clauses (stmt),
11776 &tred_ilist, &tred_dlist);
11777 rclauses = c;
11778 rtmp = make_ssa_name (type);
11779 gimple_seq_add_stmt (&body, gimple_build_assign (rtmp, temp));
11780 }
11781
11782 lower_lastprivate_conditional_clauses (gimple_omp_for_clauses_ptr (stmt),
11783 ctx);
11784
11785 lower_rec_input_clauses (gimple_omp_for_clauses (stmt), &body, &dlist, ctx,
11786 fdp);
11787 gimple_seq_add_seq (rclauses ? &tred_ilist : &body,
11788 gimple_omp_for_pre_body (stmt));
11789
11790 lower_omp (gimple_omp_body_ptr (stmt), ctx);
11791
11792 gcall *private_marker = NULL;
11793 if (is_gimple_omp_oacc (ctx->stmt)
11794 && !gimple_seq_empty_p (omp_for_body))
11795 private_marker = lower_oacc_private_marker (ctx);
11796
11797 /* Lower the header expressions. At this point, we can assume that
11798 the header is of the form:
11799
11800 #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3)
11801
11802 We just need to make sure that VAL1, VAL2 and VAL3 are lowered
11803 using the .omp_data_s mapping, if needed. */
11804 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
11805 {
11806 rhs_p = gimple_omp_for_initial_ptr (stmt, i);
11807 if (TREE_CODE (*rhs_p) == TREE_VEC)
11808 {
11809 if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
11810 TREE_VEC_ELT (*rhs_p, 1)
11811 = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
11812 if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
11813 TREE_VEC_ELT (*rhs_p, 2)
11814 = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
11815 }
11816 else if (!is_gimple_min_invariant (*rhs_p))
11817 *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11818 else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
11819 recompute_tree_invariant_for_addr_expr (*rhs_p);
11820
11821 rhs_p = gimple_omp_for_final_ptr (stmt, i);
11822 if (TREE_CODE (*rhs_p) == TREE_VEC)
11823 {
11824 if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
11825 TREE_VEC_ELT (*rhs_p, 1)
11826 = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
11827 if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
11828 TREE_VEC_ELT (*rhs_p, 2)
11829 = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
11830 }
11831 else if (!is_gimple_min_invariant (*rhs_p))
11832 *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11833 else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
11834 recompute_tree_invariant_for_addr_expr (*rhs_p);
11835
11836 rhs_p = &TREE_OPERAND (gimple_omp_for_incr (stmt, i), 1);
11837 if (!is_gimple_min_invariant (*rhs_p))
11838 *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11839 }
11840 if (rclauses)
11841 gimple_seq_add_seq (&tred_ilist, cnt_list);
11842 else
11843 gimple_seq_add_seq (&body, cnt_list);
11844
11845 /* Once lowered, extract the bounds and clauses. */
11846 omp_extract_for_data (stmt, &fd, NULL);
11847
11848 if (is_gimple_omp_oacc (ctx->stmt)
11849 && !ctx_in_oacc_kernels_region (ctx))
11850 lower_oacc_head_tail (gimple_location (stmt),
11851 gimple_omp_for_clauses (stmt), private_marker,
11852 &oacc_head, &oacc_tail, ctx);
11853
11854 /* Add OpenACC partitioning and reduction markers just before the loop. */
11855 if (oacc_head)
11856 gimple_seq_add_seq (&body, oacc_head);
11857
11858 lower_omp_for_lastprivate (&fd, &body, &dlist, &clist, ctx);
11859
11860 if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
11861 for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
11862 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
11863 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
11864 {
11865 OMP_CLAUSE_DECL (c) = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
11866 if (DECL_P (OMP_CLAUSE_LINEAR_STEP (c)))
11867 OMP_CLAUSE_LINEAR_STEP (c)
11868 = maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_LINEAR_STEP (c),
11869 ctx);
11870 }
11871
11872 if ((ctx->scan_inclusive || ctx->scan_exclusive)
11873 && gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
11874 lower_omp_for_scan (&body, &dlist, stmt, &fd, ctx);
11875 else
11876 {
11877 gimple_seq_add_stmt (&body, stmt);
11878 gimple_seq_add_seq (&body, gimple_omp_body (stmt));
11879 }
11880
11881 gimple_seq_add_stmt (&body, gimple_build_omp_continue (fd.loop.v,
11882 fd.loop.v));
11883
11884 /* After the loop, add exit clauses. */
11885 lower_reduction_clauses (gimple_omp_for_clauses (stmt), &body, &clist, ctx);
11886
11887 if (clist)
11888 {
11889 tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
11890 gcall *g = gimple_build_call (fndecl, 0);
11891 gimple_seq_add_stmt (&body, g);
11892 gimple_seq_add_seq (&body, clist);
11893 fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
11894 g = gimple_build_call (fndecl, 0);
11895 gimple_seq_add_stmt (&body, g);
11896 }
11897
11898 if (ctx->cancellable)
11899 gimple_seq_add_stmt (&body, gimple_build_label (ctx->cancel_label));
11900
11901 gimple_seq_add_seq (&body, dlist);
11902
11903 if (rclauses)
11904 {
11905 gimple_seq_add_seq (&tred_ilist, body);
11906 body = tred_ilist;
11907 }
11908
11909 body = maybe_catch_exception (body);
11910
11911 /* Region exit marker goes at the end of the loop body. */
11912 gimple *g = gimple_build_omp_return (fd.have_nowait);
11913 gimple_seq_add_stmt (&body, g);
11914
11915 gimple_seq_add_seq (&body, tred_dlist);
11916
11917 maybe_add_implicit_barrier_cancel (ctx, g, &body);
11918
11919 if (rclauses)
11920 OMP_CLAUSE_DECL (rclauses) = rtmp;
11921
11922 /* Add OpenACC joining and reduction markers just after the loop. */
11923 if (oacc_tail)
11924 gimple_seq_add_seq (&body, oacc_tail);
11925
11926 pop_gimplify_context (new_stmt);
11927
11928 gimple_bind_append_vars (new_stmt, ctx->block_vars);
11929 maybe_remove_omp_member_access_dummy_vars (new_stmt);
11930 BLOCK_VARS (block) = gimple_bind_vars (new_stmt);
11931 if (BLOCK_VARS (block))
11932 TREE_USED (block) = 1;
11933
11934 gimple_bind_set_body (new_stmt, body);
11935 gimple_omp_set_body (stmt, NULL);
11936 gimple_omp_for_set_pre_body (stmt, NULL);
11937 }
11938
11939 /* Callback for walk_stmts. Check if the current statement only contains
11940 GIMPLE_OMP_FOR or GIMPLE_OMP_SECTIONS. */
11941
11942 static tree
11943 check_combined_parallel (gimple_stmt_iterator *gsi_p,
11944 bool *handled_ops_p,
11945 struct walk_stmt_info *wi)
11946 {
11947 int *info = (int *) wi->info;
11948 gimple *stmt = gsi_stmt (*gsi_p);
11949
11950 *handled_ops_p = true;
11951 switch (gimple_code (stmt))
11952 {
11953 WALK_SUBSTMTS;
11954
11955 case GIMPLE_DEBUG:
11956 break;
11957 case GIMPLE_OMP_FOR:
11958 case GIMPLE_OMP_SECTIONS:
11959 *info = *info == 0 ? 1 : -1;
11960 break;
11961 default:
11962 *info = -1;
11963 break;
11964 }
11965 return NULL;
11966 }
11967
11968 struct omp_taskcopy_context
11969 {
11970 /* This field must be at the beginning, as we do "inheritance": Some
11971 callback functions for tree-inline.cc (e.g., omp_copy_decl)
11972 receive a copy_body_data pointer that is up-casted to an
11973 omp_context pointer. */
11974 copy_body_data cb;
11975 omp_context *ctx;
11976 };
11977
11978 static tree
11979 task_copyfn_copy_decl (tree var, copy_body_data *cb)
11980 {
11981 struct omp_taskcopy_context *tcctx = (struct omp_taskcopy_context *) cb;
11982
11983 if (splay_tree_lookup (tcctx->ctx->sfield_map, (splay_tree_key) var))
11984 return create_tmp_var (TREE_TYPE (var));
11985
11986 return var;
11987 }
11988
11989 static tree
11990 task_copyfn_remap_type (struct omp_taskcopy_context *tcctx, tree orig_type)
11991 {
11992 tree name, new_fields = NULL, type, f;
11993
11994 type = lang_hooks.types.make_type (RECORD_TYPE);
11995 name = DECL_NAME (TYPE_NAME (orig_type));
11996 name = build_decl (gimple_location (tcctx->ctx->stmt),
11997 TYPE_DECL, name, type);
11998 TYPE_NAME (type) = name;
11999
12000 for (f = TYPE_FIELDS (orig_type); f ; f = TREE_CHAIN (f))
12001 {
12002 tree new_f = copy_node (f);
12003 DECL_CONTEXT (new_f) = type;
12004 TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &tcctx->cb);
12005 TREE_CHAIN (new_f) = new_fields;
12006 walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &tcctx->cb, NULL);
12007 walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r, &tcctx->cb, NULL);
12008 walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
12009 &tcctx->cb, NULL);
12010 new_fields = new_f;
12011 tcctx->cb.decl_map->put (f, new_f);
12012 }
12013 TYPE_FIELDS (type) = nreverse (new_fields);
12014 layout_type (type);
12015 return type;
12016 }
12017
12018 /* Create task copyfn. */
12019
12020 static void
12021 create_task_copyfn (gomp_task *task_stmt, omp_context *ctx)
12022 {
12023 struct function *child_cfun;
12024 tree child_fn, t, c, src, dst, f, sf, arg, sarg, decl;
12025 tree record_type, srecord_type, bind, list;
12026 bool record_needs_remap = false, srecord_needs_remap = false;
12027 splay_tree_node n;
12028 struct omp_taskcopy_context tcctx;
12029 location_t loc = gimple_location (task_stmt);
12030 size_t looptempno = 0;
12031
12032 child_fn = gimple_omp_task_copy_fn (task_stmt);
12033 task_cpyfns.safe_push (task_stmt);
12034 child_cfun = DECL_STRUCT_FUNCTION (child_fn);
12035 gcc_assert (child_cfun->cfg == NULL);
12036 DECL_SAVED_TREE (child_fn) = alloc_stmt_list ();
12037
12038 /* Reset DECL_CONTEXT on function arguments. */
12039 for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
12040 DECL_CONTEXT (t) = child_fn;
12041
12042 /* Populate the function. */
12043 push_gimplify_context ();
12044 push_cfun (child_cfun);
12045
12046 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
12047 TREE_SIDE_EFFECTS (bind) = 1;
12048 list = NULL;
12049 DECL_SAVED_TREE (child_fn) = bind;
12050 DECL_SOURCE_LOCATION (child_fn) = gimple_location (task_stmt);
12051
12052 /* Remap src and dst argument types if needed. */
12053 record_type = ctx->record_type;
12054 srecord_type = ctx->srecord_type;
12055 for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
12056 if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
12057 {
12058 record_needs_remap = true;
12059 break;
12060 }
12061 for (f = TYPE_FIELDS (srecord_type); f ; f = DECL_CHAIN (f))
12062 if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
12063 {
12064 srecord_needs_remap = true;
12065 break;
12066 }
12067
12068 if (record_needs_remap || srecord_needs_remap)
12069 {
12070 memset (&tcctx, '\0', sizeof (tcctx));
12071 tcctx.cb.src_fn = ctx->cb.src_fn;
12072 tcctx.cb.dst_fn = child_fn;
12073 tcctx.cb.src_node = cgraph_node::get (tcctx.cb.src_fn);
12074 gcc_checking_assert (tcctx.cb.src_node);
12075 tcctx.cb.dst_node = tcctx.cb.src_node;
12076 tcctx.cb.src_cfun = ctx->cb.src_cfun;
12077 tcctx.cb.copy_decl = task_copyfn_copy_decl;
12078 tcctx.cb.eh_lp_nr = 0;
12079 tcctx.cb.transform_call_graph_edges = CB_CGE_MOVE;
12080 tcctx.cb.decl_map = new hash_map<tree, tree>;
12081 tcctx.ctx = ctx;
12082
12083 if (record_needs_remap)
12084 record_type = task_copyfn_remap_type (&tcctx, record_type);
12085 if (srecord_needs_remap)
12086 srecord_type = task_copyfn_remap_type (&tcctx, srecord_type);
12087 }
12088 else
12089 tcctx.cb.decl_map = NULL;
12090
12091 arg = DECL_ARGUMENTS (child_fn);
12092 TREE_TYPE (arg) = build_pointer_type (record_type);
12093 sarg = DECL_CHAIN (arg);
12094 TREE_TYPE (sarg) = build_pointer_type (srecord_type);
12095
12096 /* First pass: initialize temporaries used in record_type and srecord_type
12097 sizes and field offsets. */
12098 if (tcctx.cb.decl_map)
12099 for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12100 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12101 {
12102 tree *p;
12103
12104 decl = OMP_CLAUSE_DECL (c);
12105 p = tcctx.cb.decl_map->get (decl);
12106 if (p == NULL)
12107 continue;
12108 n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12109 sf = (tree) n->value;
12110 sf = *tcctx.cb.decl_map->get (sf);
12111 src = build_simple_mem_ref_loc (loc, sarg);
12112 src = omp_build_component_ref (src, sf);
12113 t = build2 (MODIFY_EXPR, TREE_TYPE (*p), *p, src);
12114 append_to_statement_list (t, &list);
12115 }
12116
12117 /* Second pass: copy shared var pointers and copy construct non-VLA
12118 firstprivate vars. */
12119 for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12120 switch (OMP_CLAUSE_CODE (c))
12121 {
12122 splay_tree_key key;
12123 case OMP_CLAUSE_SHARED:
12124 decl = OMP_CLAUSE_DECL (c);
12125 key = (splay_tree_key) decl;
12126 if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
12127 key = (splay_tree_key) &DECL_UID (decl);
12128 n = splay_tree_lookup (ctx->field_map, key);
12129 if (n == NULL)
12130 break;
12131 f = (tree) n->value;
12132 if (tcctx.cb.decl_map)
12133 f = *tcctx.cb.decl_map->get (f);
12134 n = splay_tree_lookup (ctx->sfield_map, key);
12135 sf = (tree) n->value;
12136 if (tcctx.cb.decl_map)
12137 sf = *tcctx.cb.decl_map->get (sf);
12138 src = build_simple_mem_ref_loc (loc, sarg);
12139 src = omp_build_component_ref (src, sf);
12140 dst = build_simple_mem_ref_loc (loc, arg);
12141 dst = omp_build_component_ref (dst, f);
12142 t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12143 append_to_statement_list (t, &list);
12144 break;
12145 case OMP_CLAUSE_REDUCTION:
12146 case OMP_CLAUSE_IN_REDUCTION:
12147 decl = OMP_CLAUSE_DECL (c);
12148 if (TREE_CODE (decl) == MEM_REF)
12149 {
12150 decl = TREE_OPERAND (decl, 0);
12151 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
12152 decl = TREE_OPERAND (decl, 0);
12153 if (TREE_CODE (decl) == INDIRECT_REF
12154 || TREE_CODE (decl) == ADDR_EXPR)
12155 decl = TREE_OPERAND (decl, 0);
12156 }
12157 key = (splay_tree_key) decl;
12158 n = splay_tree_lookup (ctx->field_map, key);
12159 if (n == NULL)
12160 break;
12161 f = (tree) n->value;
12162 if (tcctx.cb.decl_map)
12163 f = *tcctx.cb.decl_map->get (f);
12164 n = splay_tree_lookup (ctx->sfield_map, key);
12165 sf = (tree) n->value;
12166 if (tcctx.cb.decl_map)
12167 sf = *tcctx.cb.decl_map->get (sf);
12168 src = build_simple_mem_ref_loc (loc, sarg);
12169 src = omp_build_component_ref (src, sf);
12170 if (decl != OMP_CLAUSE_DECL (c)
12171 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
12172 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
12173 src = build_simple_mem_ref_loc (loc, src);
12174 dst = build_simple_mem_ref_loc (loc, arg);
12175 dst = omp_build_component_ref (dst, f);
12176 t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12177 append_to_statement_list (t, &list);
12178 break;
12179 case OMP_CLAUSE__LOOPTEMP_:
12180 /* Fields for first two _looptemp_ clauses are initialized by
12181 GOMP_taskloop*, the rest are handled like firstprivate. */
12182 if (looptempno < 2)
12183 {
12184 looptempno++;
12185 break;
12186 }
12187 /* FALLTHRU */
12188 case OMP_CLAUSE__REDUCTEMP_:
12189 case OMP_CLAUSE_FIRSTPRIVATE:
12190 decl = OMP_CLAUSE_DECL (c);
12191 if (is_variable_sized (decl))
12192 break;
12193 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12194 if (n == NULL)
12195 break;
12196 f = (tree) n->value;
12197 if (tcctx.cb.decl_map)
12198 f = *tcctx.cb.decl_map->get (f);
12199 n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12200 if (n != NULL)
12201 {
12202 sf = (tree) n->value;
12203 if (tcctx.cb.decl_map)
12204 sf = *tcctx.cb.decl_map->get (sf);
12205 src = build_simple_mem_ref_loc (loc, sarg);
12206 src = omp_build_component_ref (src, sf);
12207 if (use_pointer_for_field (decl, NULL)
12208 || omp_privatize_by_reference (decl))
12209 src = build_simple_mem_ref_loc (loc, src);
12210 }
12211 else
12212 src = decl;
12213 dst = build_simple_mem_ref_loc (loc, arg);
12214 dst = omp_build_component_ref (dst, f);
12215 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
12216 t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12217 else
12218 {
12219 if (ctx->allocate_map)
12220 if (tree *allocatorp = ctx->allocate_map->get (decl))
12221 {
12222 tree allocator = *allocatorp;
12223 HOST_WIDE_INT ialign = 0;
12224 if (TREE_CODE (allocator) == TREE_LIST)
12225 {
12226 ialign = tree_to_uhwi (TREE_VALUE (allocator));
12227 allocator = TREE_PURPOSE (allocator);
12228 }
12229 if (TREE_CODE (allocator) != INTEGER_CST)
12230 {
12231 n = splay_tree_lookup (ctx->sfield_map,
12232 (splay_tree_key) allocator);
12233 allocator = (tree) n->value;
12234 if (tcctx.cb.decl_map)
12235 allocator = *tcctx.cb.decl_map->get (allocator);
12236 tree a = build_simple_mem_ref_loc (loc, sarg);
12237 allocator = omp_build_component_ref (a, allocator);
12238 }
12239 allocator = fold_convert (pointer_sized_int_node, allocator);
12240 tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
12241 tree align = build_int_cst (size_type_node,
12242 MAX (ialign,
12243 DECL_ALIGN_UNIT (decl)));
12244 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dst)));
12245 tree ptr = build_call_expr_loc (loc, a, 3, align, sz,
12246 allocator);
12247 ptr = fold_convert (TREE_TYPE (dst), ptr);
12248 t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, ptr);
12249 append_to_statement_list (t, &list);
12250 dst = build_simple_mem_ref_loc (loc, dst);
12251 }
12252 t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
12253 }
12254 append_to_statement_list (t, &list);
12255 break;
12256 case OMP_CLAUSE_PRIVATE:
12257 if (! OMP_CLAUSE_PRIVATE_OUTER_REF (c))
12258 break;
12259 decl = OMP_CLAUSE_DECL (c);
12260 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12261 f = (tree) n->value;
12262 if (tcctx.cb.decl_map)
12263 f = *tcctx.cb.decl_map->get (f);
12264 n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12265 if (n != NULL)
12266 {
12267 sf = (tree) n->value;
12268 if (tcctx.cb.decl_map)
12269 sf = *tcctx.cb.decl_map->get (sf);
12270 src = build_simple_mem_ref_loc (loc, sarg);
12271 src = omp_build_component_ref (src, sf);
12272 if (use_pointer_for_field (decl, NULL))
12273 src = build_simple_mem_ref_loc (loc, src);
12274 }
12275 else
12276 src = decl;
12277 dst = build_simple_mem_ref_loc (loc, arg);
12278 dst = omp_build_component_ref (dst, f);
12279 t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12280 append_to_statement_list (t, &list);
12281 break;
12282 default:
12283 break;
12284 }
12285
12286 /* Last pass: handle VLA firstprivates. */
12287 if (tcctx.cb.decl_map)
12288 for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12290 {
12291 tree ind, ptr, df;
12292
12293 decl = OMP_CLAUSE_DECL (c);
12294 if (!is_variable_sized (decl))
12295 continue;
12296 n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12297 if (n == NULL)
12298 continue;
12299 f = (tree) n->value;
12300 f = *tcctx.cb.decl_map->get (f);
12301 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
12302 ind = DECL_VALUE_EXPR (decl);
12303 gcc_assert (TREE_CODE (ind) == INDIRECT_REF);
12304 gcc_assert (DECL_P (TREE_OPERAND (ind, 0)));
12305 n = splay_tree_lookup (ctx->sfield_map,
12306 (splay_tree_key) TREE_OPERAND (ind, 0));
12307 sf = (tree) n->value;
12308 sf = *tcctx.cb.decl_map->get (sf);
12309 src = build_simple_mem_ref_loc (loc, sarg);
12310 src = omp_build_component_ref (src, sf);
12311 src = build_simple_mem_ref_loc (loc, src);
12312 dst = build_simple_mem_ref_loc (loc, arg);
12313 dst = omp_build_component_ref (dst, f);
12314 t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
12315 append_to_statement_list (t, &list);
12316 n = splay_tree_lookup (ctx->field_map,
12317 (splay_tree_key) TREE_OPERAND (ind, 0));
12318 df = (tree) n->value;
12319 df = *tcctx.cb.decl_map->get (df);
12320 ptr = build_simple_mem_ref_loc (loc, arg);
12321 ptr = omp_build_component_ref (ptr, df);
12322 t = build2 (MODIFY_EXPR, TREE_TYPE (ptr), ptr,
12323 build_fold_addr_expr_loc (loc, dst));
12324 append_to_statement_list (t, &list);
12325 }
12326
12327 t = build1 (RETURN_EXPR, void_type_node, NULL);
12328 append_to_statement_list (t, &list);
12329
12330 if (tcctx.cb.decl_map)
12331 delete tcctx.cb.decl_map;
12332 pop_gimplify_context (NULL);
12333 BIND_EXPR_BODY (bind) = list;
12334 pop_cfun ();
12335 }
12336
12337 static void
12338 lower_depend_clauses (tree *pclauses, gimple_seq *iseq, gimple_seq *oseq)
12339 {
12340 tree c, clauses;
12341 gimple *g;
12342 size_t cnt[5] = { 0, 0, 0, 0, 0 }, idx = 2, i;
12343
12344 clauses = omp_find_clause (*pclauses, OMP_CLAUSE_DEPEND);
12345 gcc_assert (clauses);
12346 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12348 switch (OMP_CLAUSE_DEPEND_KIND (c))
12349 {
12350 case OMP_CLAUSE_DEPEND_LAST:
12351 /* Lowering already done at gimplification. */
12352 return;
12353 case OMP_CLAUSE_DEPEND_IN:
12354 cnt[2]++;
12355 break;
12356 case OMP_CLAUSE_DEPEND_OUT:
12357 case OMP_CLAUSE_DEPEND_INOUT:
12358 cnt[0]++;
12359 break;
12360 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
12361 cnt[1]++;
12362 break;
12363 case OMP_CLAUSE_DEPEND_DEPOBJ:
12364 cnt[3]++;
12365 break;
12366 case OMP_CLAUSE_DEPEND_INOUTSET:
12367 cnt[4]++;
12368 break;
12369 default:
12370 gcc_unreachable ();
12371 }
12372 if (cnt[1] || cnt[3] || cnt[4])
12373 idx = 5;
12374 size_t total = cnt[0] + cnt[1] + cnt[2] + cnt[3] + cnt[4];
12375 size_t inoutidx = total + idx;
12376 tree type = build_array_type_nelts (ptr_type_node, total + idx + 2 * cnt[4]);
12377 tree array = create_tmp_var (type);
12378 TREE_ADDRESSABLE (array) = 1;
12379 tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
12380 NULL_TREE);
12381 if (idx == 5)
12382 {
12383 g = gimple_build_assign (r, build_int_cst (ptr_type_node, 0));
12384 gimple_seq_add_stmt (iseq, g);
12385 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
12386 NULL_TREE);
12387 }
12388 g = gimple_build_assign (r, build_int_cst (ptr_type_node, total));
12389 gimple_seq_add_stmt (iseq, g);
12390 for (i = 0; i < (idx == 5 ? 3 : 1); i++)
12391 {
12392 r = build4 (ARRAY_REF, ptr_type_node, array,
12393 size_int (i + 1 + (idx == 5)), NULL_TREE, NULL_TREE);
12394 g = gimple_build_assign (r, build_int_cst (ptr_type_node, cnt[i]));
12395 gimple_seq_add_stmt (iseq, g);
12396 }
12397 for (i = 0; i < 5; i++)
12398 {
12399 if (cnt[i] == 0)
12400 continue;
12401 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12402 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12403 continue;
12404 else
12405 {
12406 switch (OMP_CLAUSE_DEPEND_KIND (c))
12407 {
12408 case OMP_CLAUSE_DEPEND_IN:
12409 if (i != 2)
12410 continue;
12411 break;
12412 case OMP_CLAUSE_DEPEND_OUT:
12413 case OMP_CLAUSE_DEPEND_INOUT:
12414 if (i != 0)
12415 continue;
12416 break;
12417 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
12418 if (i != 1)
12419 continue;
12420 break;
12421 case OMP_CLAUSE_DEPEND_DEPOBJ:
12422 if (i != 3)
12423 continue;
12424 break;
12425 case OMP_CLAUSE_DEPEND_INOUTSET:
12426 if (i != 4)
12427 continue;
12428 break;
12429 default:
12430 gcc_unreachable ();
12431 }
12432 tree t = OMP_CLAUSE_DECL (c);
12433 if (i == 4)
12434 {
12435 t = build4 (ARRAY_REF, ptr_type_node, array,
12436 size_int (inoutidx), NULL_TREE, NULL_TREE);
12437 t = build_fold_addr_expr (t);
12438 inoutidx += 2;
12439 }
12440 t = fold_convert (ptr_type_node, t);
12441 gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
12442 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12443 NULL_TREE, NULL_TREE);
12444 g = gimple_build_assign (r, t);
12445 gimple_seq_add_stmt (iseq, g);
12446 }
12447 }
12448 if (cnt[4])
12449 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12450 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12451 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_INOUTSET)
12452 {
12453 tree t = OMP_CLAUSE_DECL (c);
12454 t = fold_convert (ptr_type_node, t);
12455 gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
12456 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12457 NULL_TREE, NULL_TREE);
12458 g = gimple_build_assign (r, t);
12459 gimple_seq_add_stmt (iseq, g);
12460 t = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
12461 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12462 NULL_TREE, NULL_TREE);
12463 g = gimple_build_assign (r, t);
12464 gimple_seq_add_stmt (iseq, g);
12465 }
12466
12467 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
12468 OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
12469 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
12470 OMP_CLAUSE_CHAIN (c) = *pclauses;
12471 *pclauses = c;
12472 tree clobber = build_clobber (type);
12473 g = gimple_build_assign (array, clobber);
12474 gimple_seq_add_stmt (oseq, g);
12475 }
12476
12477 /* Lower the OpenMP parallel or task directive in the current statement
12478 in GSI_P. CTX holds context information for the directive. */
12479
12480 static void
12481 lower_omp_taskreg (gimple_stmt_iterator *gsi_p, omp_context *ctx)
12482 {
12483 tree clauses;
12484 tree child_fn, t;
12485 gimple *stmt = gsi_stmt (*gsi_p);
12486 gbind *par_bind, *bind, *dep_bind = NULL;
12487 gimple_seq par_body;
12488 location_t loc = gimple_location (stmt);
12489
12490 clauses = gimple_omp_taskreg_clauses (stmt);
12491 if (gimple_code (stmt) == GIMPLE_OMP_TASK
12492 && gimple_omp_task_taskwait_p (stmt))
12493 {
12494 par_bind = NULL;
12495 par_body = NULL;
12496 }
12497 else
12498 {
12499 par_bind
12500 = as_a <gbind *> (gimple_seq_first_stmt (gimple_omp_body (stmt)));
12501 par_body = gimple_bind_body (par_bind);
12502 }
12503 child_fn = ctx->cb.dst_fn;
12504 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
12505 && !gimple_omp_parallel_combined_p (stmt))
12506 {
12507 struct walk_stmt_info wi;
12508 int ws_num = 0;
12509
12510 memset (&wi, 0, sizeof (wi));
12511 wi.info = &ws_num;
12512 wi.val_only = true;
12513 walk_gimple_seq (par_body, check_combined_parallel, NULL, &wi);
12514 if (ws_num == 1)
12515 gimple_omp_parallel_set_combined_p (stmt, true);
12516 }
12517 gimple_seq dep_ilist = NULL;
12518 gimple_seq dep_olist = NULL;
12519 if (gimple_code (stmt) == GIMPLE_OMP_TASK
12520 && omp_find_clause (clauses, OMP_CLAUSE_DEPEND))
12521 {
12522 push_gimplify_context ();
12523 dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12524 lower_depend_clauses (gimple_omp_task_clauses_ptr (stmt),
12525 &dep_ilist, &dep_olist);
12526 }
12527
12528 if (gimple_code (stmt) == GIMPLE_OMP_TASK
12529 && gimple_omp_task_taskwait_p (stmt))
12530 {
12531 if (dep_bind)
12532 {
12533 gsi_replace (gsi_p, dep_bind, true);
12534 gimple_bind_add_seq (dep_bind, dep_ilist);
12535 gimple_bind_add_stmt (dep_bind, stmt);
12536 gimple_bind_add_seq (dep_bind, dep_olist);
12537 pop_gimplify_context (dep_bind);
12538 }
12539 return;
12540 }
12541
12542 if (ctx->srecord_type)
12543 create_task_copyfn (as_a <gomp_task *> (stmt), ctx);
12544
12545 gimple_seq tskred_ilist = NULL;
12546 gimple_seq tskred_olist = NULL;
12547 if ((is_task_ctx (ctx)
12548 && gimple_omp_task_taskloop_p (ctx->stmt)
12549 && omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
12550 OMP_CLAUSE_REDUCTION))
12551 || (is_parallel_ctx (ctx)
12552 && omp_find_clause (gimple_omp_parallel_clauses (stmt),
12553 OMP_CLAUSE__REDUCTEMP_)))
12554 {
12555 if (dep_bind == NULL)
12556 {
12557 push_gimplify_context ();
12558 dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12559 }
12560 lower_omp_task_reductions (ctx, is_task_ctx (ctx) ? OMP_TASKLOOP
12561 : OMP_PARALLEL,
12562 gimple_omp_taskreg_clauses (ctx->stmt),
12563 &tskred_ilist, &tskred_olist);
12564 }
12565
12566 push_gimplify_context ();
12567
12568 gimple_seq par_olist = NULL;
12569 gimple_seq par_ilist = NULL;
12570 gimple_seq par_rlist = NULL;
12571 lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx, NULL);
12572 lower_omp (&par_body, ctx);
12573 if (gimple_code (stmt) != GIMPLE_OMP_TASK)
12574 lower_reduction_clauses (clauses, &par_rlist, NULL, ctx);
12575
12576 /* Declare all the variables created by mapping and the variables
12577 declared in the scope of the parallel body. */
12578 record_vars_into (ctx->block_vars, child_fn);
12579 maybe_remove_omp_member_access_dummy_vars (par_bind);
12580 record_vars_into (gimple_bind_vars (par_bind), child_fn);
12581
12582 if (ctx->record_type)
12583 {
12584 ctx->sender_decl
12585 = create_tmp_var (ctx->srecord_type ? ctx->srecord_type
12586 : ctx->record_type, ".omp_data_o");
12587 DECL_NAMELESS (ctx->sender_decl) = 1;
12588 TREE_ADDRESSABLE (ctx->sender_decl) = 1;
12589 gimple_omp_taskreg_set_data_arg (stmt, ctx->sender_decl);
12590 }
12591
12592 gimple_seq olist = NULL;
12593 gimple_seq ilist = NULL;
12594 lower_send_clauses (clauses, &ilist, &olist, ctx);
12595 lower_send_shared_vars (&ilist, &olist, ctx);
12596
12597 if (ctx->record_type)
12598 {
12599 tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
12600 gimple_seq_add_stmt (&olist, gimple_build_assign (ctx->sender_decl,
12601 clobber));
12602 }
12603
12604 /* Once all the expansions are done, sequence all the different
12605 fragments inside gimple_omp_body. */
12606
12607 gimple_seq new_body = NULL;
12608
12609 if (ctx->record_type)
12610 {
12611 t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
12612 /* fixup_child_record_type might have changed receiver_decl's type. */
12613 t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
12614 gimple_seq_add_stmt (&new_body,
12615 gimple_build_assign (ctx->receiver_decl, t));
12616 }
12617
12618 gimple_seq_add_seq (&new_body, par_ilist);
12619 gimple_seq_add_seq (&new_body, par_body);
12620 gimple_seq_add_seq (&new_body, par_rlist);
12621 if (ctx->cancellable)
12622 gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
12623 gimple_seq_add_seq (&new_body, par_olist);
12624 new_body = maybe_catch_exception (new_body);
12625 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
12626 gimple_seq_add_stmt (&new_body,
12627 gimple_build_omp_continue (integer_zero_node,
12628 integer_zero_node));
12629 gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
12630 gimple_omp_set_body (stmt, new_body);
12631
12632 if (dep_bind && gimple_bind_block (par_bind) == NULL_TREE)
12633 bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12634 else
12635 bind = gimple_build_bind (NULL, NULL, gimple_bind_block (par_bind));
12636 gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
12637 gimple_bind_add_seq (bind, ilist);
12638 gimple_bind_add_stmt (bind, stmt);
12639 gimple_bind_add_seq (bind, olist);
12640
12641 pop_gimplify_context (NULL);
12642
12643 if (dep_bind)
12644 {
12645 gimple_bind_add_seq (dep_bind, dep_ilist);
12646 gimple_bind_add_seq (dep_bind, tskred_ilist);
12647 gimple_bind_add_stmt (dep_bind, bind);
12648 gimple_bind_add_seq (dep_bind, tskred_olist);
12649 gimple_bind_add_seq (dep_bind, dep_olist);
12650 pop_gimplify_context (dep_bind);
12651 }
12652 }
12653
12654 /* Lower the GIMPLE_OMP_TARGET in the current statement
12655 in GSI_P. CTX holds context information for the directive. */
12656
12657 static void
12658 lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
12659 {
12660 tree clauses;
12661 tree child_fn, t, c;
12662 gomp_target *stmt = as_a <gomp_target *> (gsi_stmt (*gsi_p));
12663 gbind *tgt_bind, *bind, *dep_bind = NULL;
12664 gimple_seq tgt_body, olist, ilist, fplist, new_body;
12665 location_t loc = gimple_location (stmt);
12666 bool offloaded, data_region;
12667 unsigned int map_cnt = 0;
12668 tree in_reduction_clauses = NULL_TREE;
12669
12670 tree deep_map_cnt = NULL_TREE;
12671 tree deep_map_data = NULL_TREE;
12672 tree deep_map_offset_data = NULL_TREE;
12673 tree deep_map_offset = NULL_TREE;
12674
12675 offloaded = is_gimple_omp_offloaded (stmt);
12676 switch (gimple_omp_target_kind (stmt))
12677 {
12678 case GF_OMP_TARGET_KIND_REGION:
12679 tree *p, *q;
12680 q = &in_reduction_clauses;
12681 for (p = gimple_omp_target_clauses_ptr (stmt); *p; )
12682 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_IN_REDUCTION)
12683 {
12684 *q = *p;
12685 q = &OMP_CLAUSE_CHAIN (*q);
12686 *p = OMP_CLAUSE_CHAIN (*p);
12687 }
12688 else
12689 p = &OMP_CLAUSE_CHAIN (*p);
12690 *q = NULL_TREE;
12691 *p = in_reduction_clauses;
12692 /* FALLTHRU */
12693 case GF_OMP_TARGET_KIND_UPDATE:
12694 case GF_OMP_TARGET_KIND_ENTER_DATA:
12695 case GF_OMP_TARGET_KIND_EXIT_DATA:
12696 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
12697 case GF_OMP_TARGET_KIND_OACC_KERNELS:
12698 case GF_OMP_TARGET_KIND_OACC_SERIAL:
12699 case GF_OMP_TARGET_KIND_OACC_UPDATE:
12700 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
12701 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
12702 case GF_OMP_TARGET_KIND_OACC_DECLARE:
12703 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
12704 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
12705 data_region = false;
12706 break;
12707 case GF_OMP_TARGET_KIND_DATA:
12708 case GF_OMP_TARGET_KIND_OACC_DATA:
12709 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
12710 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
12711 data_region = true;
12712 break;
12713 default:
12714 gcc_unreachable ();
12715 }
12716
12717 /* Ensure that requires map is written via output_offload_tables, even if only
12718 'target (enter/exit) data' is used in the translation unit. */
12719 if (ENABLE_OFFLOADING && (omp_requires_mask & OMP_REQUIRES_TARGET_USED))
12720 g->have_offload = true;
12721
12722 clauses = gimple_omp_target_clauses (stmt);
12723
12724 gimple_seq dep_ilist = NULL;
12725 gimple_seq dep_olist = NULL;
12726 bool has_depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND) != NULL_TREE;
12727 if (has_depend || in_reduction_clauses)
12728 {
12729 push_gimplify_context ();
12730 dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12731 if (has_depend)
12732 lower_depend_clauses (gimple_omp_target_clauses_ptr (stmt),
12733 &dep_ilist, &dep_olist);
12734 if (in_reduction_clauses)
12735 lower_rec_input_clauses (in_reduction_clauses, &dep_ilist, &dep_olist,
12736 ctx, NULL);
12737 }
12738
12739 tgt_bind = NULL;
12740 tgt_body = NULL;
12741 if (offloaded)
12742 {
12743 tgt_bind = gimple_seq_first_stmt_as_a_bind (gimple_omp_body (stmt));
12744 tgt_body = gimple_bind_body (tgt_bind);
12745 }
12746 else if (data_region)
12747 tgt_body = gimple_omp_body (stmt);
12748 child_fn = ctx->cb.dst_fn;
12749
12750 push_gimplify_context ();
12751 fplist = NULL;
12752
12753 ilist = NULL;
12754 olist = NULL;
12755 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12756 switch (OMP_CLAUSE_CODE (c))
12757 {
12758 tree var, x;
12759
12760 default:
12761 break;
12762 case OMP_CLAUSE_MAP:
12763 #if CHECKING_P
12764 /* First check what we're prepared to handle in the following. */
12765 switch (OMP_CLAUSE_MAP_KIND (c))
12766 {
12767 case GOMP_MAP_ALLOC:
12768 case GOMP_MAP_TO:
12769 case GOMP_MAP_FROM:
12770 case GOMP_MAP_TOFROM:
12771 case GOMP_MAP_POINTER:
12772 case GOMP_MAP_TO_PSET:
12773 case GOMP_MAP_DELETE:
12774 case GOMP_MAP_RELEASE:
12775 case GOMP_MAP_ALWAYS_TO:
12776 case GOMP_MAP_ALWAYS_FROM:
12777 case GOMP_MAP_ALWAYS_TOFROM:
12778 case GOMP_MAP_FORCE_PRESENT:
12779 case GOMP_MAP_ALWAYS_PRESENT_FROM:
12780 case GOMP_MAP_ALWAYS_PRESENT_TO:
12781 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
12782
12783 case GOMP_MAP_FIRSTPRIVATE_POINTER:
12784 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
12785 case GOMP_MAP_STRUCT:
12786 case GOMP_MAP_STRUCT_UNORD:
12787 case GOMP_MAP_ALWAYS_POINTER:
12788 case GOMP_MAP_ATTACH:
12789 case GOMP_MAP_DETACH:
12790 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
12791 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
12792 break;
12793 case GOMP_MAP_IF_PRESENT:
12794 case GOMP_MAP_FORCE_ALLOC:
12795 case GOMP_MAP_FORCE_TO:
12796 case GOMP_MAP_FORCE_FROM:
12797 case GOMP_MAP_FORCE_TOFROM:
12798 case GOMP_MAP_FORCE_DEVICEPTR:
12799 case GOMP_MAP_DEVICE_RESIDENT:
12800 case GOMP_MAP_LINK:
12801 case GOMP_MAP_FORCE_DETACH:
12802 gcc_assert (is_gimple_omp_oacc (stmt));
12803 break;
12804 default:
12805 gcc_unreachable ();
12806 }
12807 #endif
12808 /* FALLTHRU */
12809 case OMP_CLAUSE_TO:
12810 case OMP_CLAUSE_FROM:
12811 oacc_firstprivate:
12812 var = OMP_CLAUSE_DECL (c);
12813 {
12814 tree extra = lang_hooks.decls.omp_deep_mapping_cnt (stmt, c, &ilist);
12815 if (extra != NULL_TREE && deep_map_cnt != NULL_TREE)
12816 deep_map_cnt = fold_build2_loc (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12817 size_type_node, deep_map_cnt,
12818 extra);
12819 else if (extra != NULL_TREE)
12820 deep_map_cnt = extra;
12821 }
12822
12823 if (!DECL_P (var))
12824 {
12825 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12826 || (!OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
12827 && (OMP_CLAUSE_MAP_KIND (c)
12828 != GOMP_MAP_FIRSTPRIVATE_POINTER)))
12829 map_cnt++;
12830 continue;
12831 }
12832
12833 if (DECL_SIZE (var)
12834 && !poly_int_tree_p (DECL_SIZE (var)))
12835 {
12836 tree var2 = DECL_VALUE_EXPR (var);
12837 gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
12838 var2 = TREE_OPERAND (var2, 0);
12839 gcc_assert (DECL_P (var2));
12840 var = var2;
12841 }
12842
12843 if (offloaded
12844 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12845 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
12846 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
12847 {
12848 if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
12849 {
12850 if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx))
12851 && varpool_node::get_create (var)->offloadable)
12852 continue;
12853
12854 tree type = build_pointer_type (TREE_TYPE (var));
12855 tree new_var = lookup_decl (var, ctx);
12856 x = create_tmp_var_raw (type, get_name (new_var));
12857 gimple_add_tmp_var (x);
12858 x = build_simple_mem_ref (x);
12859 SET_DECL_VALUE_EXPR (new_var, x);
12860 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12861 }
12862 continue;
12863 }
12864
12865 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12866 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
12867 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
12868 && is_omp_target (stmt))
12869 {
12870 gcc_assert (maybe_lookup_field (c, ctx));
12871 map_cnt++;
12872 continue;
12873 }
12874
12875 if (!maybe_lookup_field (var, ctx))
12876 continue;
12877
12878 /* Don't remap compute constructs' reduction variables, because the
12879 intermediate result must be local to each gang. */
12880 if (offloaded && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12881 && is_gimple_omp_oacc (ctx->stmt)
12882 && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
12883 {
12884 x = build_receiver_ref (var, true, ctx);
12885 tree new_var = lookup_decl (var, ctx);
12886
12887 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12888 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12889 && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
12890 && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
12891 x = build_simple_mem_ref (x);
12892 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12893 {
12894 gcc_assert (is_gimple_omp_oacc (ctx->stmt));
12895 if (omp_privatize_by_reference (new_var)
12896 && (TREE_CODE (TREE_TYPE (new_var)) != POINTER_TYPE
12897 || DECL_BY_REFERENCE (var)))
12898 {
12899 /* Create a local object to hold the instance
12900 value. */
12901 tree type = TREE_TYPE (TREE_TYPE (new_var));
12902 const char *id = IDENTIFIER_POINTER (DECL_NAME (new_var));
12903 tree inst = create_tmp_var (type, id);
12904 gimplify_assign (inst, fold_indirect_ref (x), &fplist);
12905 x = build_fold_addr_expr (inst);
12906 }
12907 gimplify_assign (new_var, x, &fplist);
12908 }
12909 else if (DECL_P (new_var))
12910 {
12911 SET_DECL_VALUE_EXPR (new_var, x);
12912 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12913 }
12914 else
12915 gcc_unreachable ();
12916 }
12917 map_cnt++;
12918 break;
12919
12920 case OMP_CLAUSE_FIRSTPRIVATE:
12921 omp_firstprivate_recv:
12922 gcc_checking_assert (offloaded);
12923 if (is_gimple_omp_oacc (ctx->stmt))
12924 {
12925 /* No 'firstprivate' clauses on OpenACC 'kernels'. */
12926 gcc_checking_assert (!is_oacc_kernels (ctx));
12927 /* Likewise, on OpenACC 'kernels' decomposed parts. */
12928 gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
12929
12930 goto oacc_firstprivate;
12931 }
12932 map_cnt++;
12933 var = OMP_CLAUSE_DECL (c);
12934 if (!omp_privatize_by_reference (var)
12935 && !is_gimple_reg_type (TREE_TYPE (var)))
12936 {
12937 tree new_var = lookup_decl (var, ctx);
12938 if (is_variable_sized (var))
12939 {
12940 tree pvar = DECL_VALUE_EXPR (var);
12941 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
12942 pvar = TREE_OPERAND (pvar, 0);
12943 gcc_assert (DECL_P (pvar));
12944 tree new_pvar = lookup_decl (pvar, ctx);
12945 x = build_fold_indirect_ref (new_pvar);
12946 TREE_THIS_NOTRAP (x) = 1;
12947 }
12948 else
12949 x = build_receiver_ref (var, true, ctx);
12950 SET_DECL_VALUE_EXPR (new_var, x);
12951 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12952 }
12953 /* Fortran array descriptors: firstprivate of data + attach. */
12954 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
12955 && lang_hooks.decls.omp_array_data (var, true))
12956 map_cnt += 2;
12957 break;
12958
12959 case OMP_CLAUSE_PRIVATE:
12960 gcc_checking_assert (offloaded);
12961 if (is_gimple_omp_oacc (ctx->stmt))
12962 {
12963 /* No 'private' clauses on OpenACC 'kernels'. */
12964 gcc_checking_assert (!is_oacc_kernels (ctx));
12965 /* Likewise, on OpenACC 'kernels' decomposed parts. */
12966 gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
12967
12968 break;
12969 }
12970 var = OMP_CLAUSE_DECL (c);
12971 if (is_variable_sized (var))
12972 {
12973 tree new_var = lookup_decl (var, ctx);
12974 tree pvar = DECL_VALUE_EXPR (var);
12975 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
12976 pvar = TREE_OPERAND (pvar, 0);
12977 gcc_assert (DECL_P (pvar));
12978 tree new_pvar = lookup_decl (pvar, ctx);
12979 x = build_fold_indirect_ref (new_pvar);
12980 TREE_THIS_NOTRAP (x) = 1;
12981 SET_DECL_VALUE_EXPR (new_var, x);
12982 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12983 }
12984 break;
12985
12986 case OMP_CLAUSE_USE_DEVICE_PTR:
12987 case OMP_CLAUSE_USE_DEVICE_ADDR:
12988 case OMP_CLAUSE_HAS_DEVICE_ADDR:
12989 case OMP_CLAUSE_IS_DEVICE_PTR:
12990 var = OMP_CLAUSE_DECL (c);
12991 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
12992 {
12993 while (TREE_CODE (var) == INDIRECT_REF
12994 || TREE_CODE (var) == ARRAY_REF)
12995 var = TREE_OPERAND (var, 0);
12996 if (lang_hooks.decls.omp_array_data (var, true))
12997 goto omp_firstprivate_recv;
12998 }
12999 map_cnt++;
13000 if (is_variable_sized (var))
13001 {
13002 tree new_var = lookup_decl (var, ctx);
13003 tree pvar = DECL_VALUE_EXPR (var);
13004 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
13005 pvar = TREE_OPERAND (pvar, 0);
13006 gcc_assert (DECL_P (pvar));
13007 tree new_pvar = lookup_decl (pvar, ctx);
13008 x = build_fold_indirect_ref (new_pvar);
13009 TREE_THIS_NOTRAP (x) = 1;
13010 SET_DECL_VALUE_EXPR (new_var, x);
13011 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13012 }
13013 else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
13014 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13015 && !omp_privatize_by_reference (var)
13016 && !omp_is_allocatable_or_ptr (var)
13017 && !lang_hooks.decls.omp_array_data (var, true))
13018 || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
13019 {
13020 tree new_var = lookup_decl (var, ctx);
13021 tree type = build_pointer_type (TREE_TYPE (var));
13022 x = create_tmp_var_raw (type, get_name (new_var));
13023 gimple_add_tmp_var (x);
13024 x = build_simple_mem_ref (x);
13025 SET_DECL_VALUE_EXPR (new_var, x);
13026 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13027 }
13028 else
13029 {
13030 tree new_var = lookup_decl (var, ctx);
13031 x = create_tmp_var_raw (TREE_TYPE (new_var), get_name (new_var));
13032 gimple_add_tmp_var (x);
13033 SET_DECL_VALUE_EXPR (new_var, x);
13034 DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13035 }
13036 break;
13037 }
13038
13039 if (offloaded)
13040 {
13041 target_nesting_level++;
13042 lower_omp (&tgt_body, ctx);
13043 target_nesting_level--;
13044 }
13045 else if (data_region)
13046 lower_omp (&tgt_body, ctx);
13047
13048 if (offloaded)
13049 {
13050 /* Declare all the variables created by mapping and the variables
13051 declared in the scope of the target body. */
13052 record_vars_into (ctx->block_vars, child_fn);
13053 maybe_remove_omp_member_access_dummy_vars (tgt_bind);
13054 record_vars_into (gimple_bind_vars (tgt_bind), child_fn);
13055 }
13056
13057 if (ctx->record_type)
13058 {
13059 if (deep_map_cnt && TREE_CODE (deep_map_cnt) == INTEGER_CST)
13060 /* map_cnt = map_cnt + tree_to_hwi (deep_map_cnt); */
13061 /* deep_map_cnt = NULL_TREE; */
13062 gcc_unreachable ();
13063 else if (deep_map_cnt)
13064 {
13065 gcc_assert (flexible_array_type_p (ctx->record_type));
13066 tree n = create_tmp_var_raw (size_type_node, "nn_map");
13067 gimple_add_tmp_var (n);
13068 gimplify_assign (n, deep_map_cnt, &ilist);
13069 deep_map_cnt = n;
13070 }
13071 ctx->sender_decl
13072 = create_tmp_var (deep_map_cnt ? build_pointer_type (ctx->record_type)
13073 : ctx->record_type, ".omp_data_arr");
13074 DECL_NAMELESS (ctx->sender_decl) = 1;
13075 TREE_ADDRESSABLE (ctx->sender_decl) = 1;
13076 t = make_tree_vec (deep_map_cnt ? 4 : 3);
13077 TREE_VEC_ELT (t, 0) = ctx->sender_decl;
13078 TREE_VEC_ELT (t, 1)
13079 = create_tmp_var (deep_map_cnt
13080 ? build_pointer_type (size_type_node)
13081 : build_array_type_nelts (size_type_node, map_cnt),
13082 ".omp_data_sizes");
13083 DECL_NAMELESS (TREE_VEC_ELT (t, 1)) = 1;
13084 TREE_ADDRESSABLE (TREE_VEC_ELT (t, 1)) = 1;
13085 TREE_STATIC (TREE_VEC_ELT (t, 1)) = 1;
13086 tree tkind_type = short_unsigned_type_node;
13087 int talign_shift = 8;
13088 TREE_VEC_ELT (t, 2)
13089 = create_tmp_var (deep_map_cnt
13090 ? build_pointer_type (tkind_type)
13091 : build_array_type_nelts (tkind_type, map_cnt),
13092 ".omp_data_kinds");
13093 DECL_NAMELESS (TREE_VEC_ELT (t, 2)) = 1;
13094 TREE_ADDRESSABLE (TREE_VEC_ELT (t, 2)) = 1;
13095 TREE_STATIC (TREE_VEC_ELT (t, 2)) = 1;
13096 gimple_omp_target_set_data_arg (stmt, t);
13097
13098 if (deep_map_cnt)
13099 {
13100 tree tmp, size;
13101 size = create_tmp_var (size_type_node, NULL);
13102 DECL_NAMELESS (size) = 1;
13103 gimplify_assign (size,
13104 fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
13105 size_type_node, deep_map_cnt,
13106 build_int_cst (size_type_node,
13107 map_cnt)), &ilist);
13108 TREE_VEC_ELT (t, 3) = size;
13109
13110 tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
13111 size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13112 size_type_node, deep_map_cnt,
13113 TYPE_SIZE_UNIT (ptr_type_node));
13114 size = fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
13115 size_type_node, size,
13116 TYPE_SIZE_UNIT (ctx->record_type));
13117 tmp = build_call_expr_loc (input_location, call, 1, size);
13118 gimplify_assign (ctx->sender_decl, tmp, &ilist);
13119
13120 size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13121 size_type_node, TREE_VEC_ELT (t, 3),
13122 TYPE_SIZE_UNIT (size_type_node));
13123 tmp = build_call_expr_loc (input_location, call, 1, size);
13124 gimplify_assign (TREE_VEC_ELT (t, 1), tmp, &ilist);
13125
13126 size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13127 size_type_node, TREE_VEC_ELT (t, 3),
13128 TYPE_SIZE_UNIT (tkind_type));
13129 tmp = build_call_expr_loc (input_location, call, 1, size);
13130 gimplify_assign (TREE_VEC_ELT (t, 2), tmp, &ilist);
13131 tree field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (ctx->sender_decl)));
13132 for ( ; DECL_CHAIN (field) != NULL_TREE; field = DECL_CHAIN (field))
13133 ;
13134 gcc_assert (TREE_CODE (TREE_TYPE (field)));
13135 tmp = build_fold_indirect_ref (ctx->sender_decl);
13136 deep_map_data = omp_build_component_ref (tmp, field);
13137 deep_map_offset_data = create_tmp_var_raw (size_type_node,
13138 "map_offset_data");
13139 deep_map_offset = create_tmp_var_raw (size_type_node, "map_offset");
13140 gimple_add_tmp_var (deep_map_offset_data);
13141 gimple_add_tmp_var (deep_map_offset);
13142 gimplify_assign (deep_map_offset_data, build_int_cst (size_type_node,
13143 0), &ilist);
13144 gimplify_assign (deep_map_offset, build_int_cst (size_type_node,
13145 map_cnt), &ilist);
13146 }
13147
13148 vec<constructor_elt, va_gc> *vsize;
13149 vec<constructor_elt, va_gc> *vkind;
13150 vec_alloc (vsize, map_cnt);
13151 vec_alloc (vkind, map_cnt);
13152 unsigned int map_idx = 0;
13153
13154 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13155 switch (OMP_CLAUSE_CODE (c))
13156 {
13157 tree ovar, nc, s, purpose, var, x, type;
13158 unsigned int talign;
13159
13160 default:
13161 break;
13162
13163 case OMP_CLAUSE_MAP:
13164 case OMP_CLAUSE_TO:
13165 case OMP_CLAUSE_FROM:
13166 oacc_firstprivate_map:
13167 nc = c;
13168 ovar = OMP_CLAUSE_DECL (c);
13169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13170 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
13171 || (OMP_CLAUSE_MAP_KIND (c)
13172 == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
13173 break;
13174 if (deep_map_cnt)
13175 {
13176 unsigned HOST_WIDE_INT tkind2;
13177 switch (OMP_CLAUSE_CODE (c))
13178 {
13179 case OMP_CLAUSE_MAP: tkind2 = OMP_CLAUSE_MAP_KIND (c); break;
13180 case OMP_CLAUSE_FIRSTPRIVATE: tkind2 = GOMP_MAP_TO; break;
13181 case OMP_CLAUSE_TO: tkind2 = GOMP_MAP_TO; break;
13182 case OMP_CLAUSE_FROM: tkind2 = GOMP_MAP_FROM; break;
13183 default: gcc_unreachable ();
13184 }
13185 lang_hooks.decls.omp_deep_mapping (stmt, c, tkind2,
13186 deep_map_data,
13187 TREE_VEC_ELT (t, 1),
13188 TREE_VEC_ELT (t, 2),
13189 deep_map_offset_data,
13190 deep_map_offset, &ilist);
13191 }
13192 if (!DECL_P (ovar))
13193 {
13194 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13195 && OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
13196 {
13197 nc = OMP_CLAUSE_CHAIN (c);
13198 gcc_checking_assert (OMP_CLAUSE_DECL (nc)
13199 == get_base_address (ovar));
13200 ovar = OMP_CLAUSE_DECL (nc);
13201 }
13202 else
13203 {
13204 tree x = build_sender_ref (ovar, ctx);
13205 tree v = ovar;
13206 if (in_reduction_clauses
13207 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13208 && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13209 {
13210 v = unshare_expr (v);
13211 tree *p = &v;
13212 while (handled_component_p (*p)
13213 || TREE_CODE (*p) == INDIRECT_REF
13214 || TREE_CODE (*p) == ADDR_EXPR
13215 || TREE_CODE (*p) == MEM_REF
13216 || TREE_CODE (*p) == NON_LVALUE_EXPR)
13217 p = &TREE_OPERAND (*p, 0);
13218 tree d = *p;
13219 if (is_variable_sized (d))
13220 {
13221 gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
13222 d = DECL_VALUE_EXPR (d);
13223 gcc_assert (TREE_CODE (d) == INDIRECT_REF);
13224 d = TREE_OPERAND (d, 0);
13225 gcc_assert (DECL_P (d));
13226 }
13227 splay_tree_key key
13228 = (splay_tree_key) &DECL_CONTEXT (d);
13229 tree nd = (tree) splay_tree_lookup (ctx->field_map,
13230 key)->value;
13231 if (d == *p)
13232 *p = nd;
13233 else
13234 *p = build_fold_indirect_ref (nd);
13235 }
13236 v = build_fold_addr_expr_with_type (v, ptr_type_node);
13237 gimplify_assign (x, v, &ilist);
13238 nc = NULL_TREE;
13239 }
13240 }
13241 else
13242 {
13243 if (DECL_SIZE (ovar)
13244 && !poly_int_tree_p (DECL_SIZE (ovar)))
13245 {
13246 tree ovar2 = DECL_VALUE_EXPR (ovar);
13247 gcc_assert (TREE_CODE (ovar2) == INDIRECT_REF);
13248 ovar2 = TREE_OPERAND (ovar2, 0);
13249 gcc_assert (DECL_P (ovar2));
13250 ovar = ovar2;
13251 }
13252 if (!maybe_lookup_field (ovar, ctx)
13253 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13254 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13255 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)))
13256 continue;
13257 }
13258
13259 talign = TYPE_ALIGN_UNIT (TREE_TYPE (ovar));
13260 if (DECL_P (ovar) && DECL_ALIGN_UNIT (ovar) > talign)
13261 talign = DECL_ALIGN_UNIT (ovar);
13262
13263 var = NULL_TREE;
13264 if (nc)
13265 {
13266 if (in_reduction_clauses
13267 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13268 && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13269 {
13270 tree d = ovar;
13271 if (is_variable_sized (d))
13272 {
13273 gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
13274 d = DECL_VALUE_EXPR (d);
13275 gcc_assert (TREE_CODE (d) == INDIRECT_REF);
13276 d = TREE_OPERAND (d, 0);
13277 gcc_assert (DECL_P (d));
13278 }
13279 splay_tree_key key
13280 = (splay_tree_key) &DECL_CONTEXT (d);
13281 tree nd = (tree) splay_tree_lookup (ctx->field_map,
13282 key)->value;
13283 if (d == ovar)
13284 var = nd;
13285 else
13286 var = build_fold_indirect_ref (nd);
13287 }
13288 else
13289 var = lookup_decl_in_outer_ctx (ovar, ctx);
13290 }
13291 if (nc
13292 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13293 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13294 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13295 && is_omp_target (stmt))
13296 {
13297 x = build_sender_ref (c, ctx);
13298 gimplify_assign (x, build_fold_addr_expr (var), &ilist);
13299 }
13300 else if (nc)
13301 {
13302 x = build_sender_ref (ovar, ctx);
13303
13304 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13305 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13306 && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
13307 && TREE_CODE (TREE_TYPE (ovar)) == ARRAY_TYPE)
13308 {
13309 gcc_assert (offloaded);
13310 tree avar
13311 = create_tmp_var (TREE_TYPE (TREE_TYPE (x)));
13312 mark_addressable (avar);
13313 gimplify_assign (avar, build_fold_addr_expr (var), &ilist);
13314 talign = DECL_ALIGN_UNIT (avar);
13315 avar = build_fold_addr_expr (avar);
13316 gimplify_assign (x, avar, &ilist);
13317 }
13318 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
13319 {
13320 gcc_assert (is_gimple_omp_oacc (ctx->stmt));
13321 if (!omp_privatize_by_reference (var))
13322 {
13323 if (is_gimple_reg (var)
13324 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13325 suppress_warning (var);
13326 var = build_fold_addr_expr (var);
13327 }
13328 else
13329 talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13330 gimplify_assign (x, var, &ilist);
13331 }
13332 else if (is_gimple_reg (var))
13333 {
13334 gcc_assert (offloaded);
13335 tree avar = create_tmp_var (TREE_TYPE (var));
13336 mark_addressable (avar);
13337 enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (c);
13338 if (GOMP_MAP_COPY_TO_P (map_kind)
13339 || map_kind == GOMP_MAP_POINTER
13340 || map_kind == GOMP_MAP_TO_PSET
13341 || map_kind == GOMP_MAP_FORCE_DEVICEPTR)
13342 {
13343 /* If we need to initialize a temporary
13344 with VAR because it is not addressable, and
13345 the variable hasn't been initialized yet, then
13346 we'll get a warning for the store to avar.
13347 Don't warn in that case, the mapping might
13348 be implicit. */
13349 suppress_warning (var, OPT_Wuninitialized);
13350 gimplify_assign (avar, var, &ilist);
13351 }
13352 avar = build_fold_addr_expr (avar);
13353 gimplify_assign (x, avar, &ilist);
13354 if ((GOMP_MAP_COPY_FROM_P (map_kind)
13355 || map_kind == GOMP_MAP_FORCE_DEVICEPTR)
13356 && !TYPE_READONLY (TREE_TYPE (var)))
13357 {
13358 x = unshare_expr (x);
13359 x = build_simple_mem_ref (x);
13360 gimplify_assign (var, x, &olist);
13361 }
13362 }
13363 else
13364 {
13365 /* While MAP is handled explicitly by the FE,
13366 for 'target update', only the identified is passed. */
13367 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM
13368 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO)
13369 && (omp_is_allocatable_or_ptr (var)
13370 && omp_check_optional_argument (var, false)))
13371 var = build_fold_indirect_ref (var);
13372 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FROM
13373 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TO)
13374 || (!omp_is_allocatable_or_ptr (var)
13375 && !omp_check_optional_argument (var, false)))
13376 var = build_fold_addr_expr (var);
13377 gimplify_assign (x, var, &ilist);
13378 }
13379 }
13380 s = NULL_TREE;
13381 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
13382 {
13383 gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
13384 s = TREE_TYPE (ovar);
13385 if (TREE_CODE (s) == REFERENCE_TYPE
13386 || omp_check_optional_argument (ovar, false))
13387 s = TREE_TYPE (s);
13388 s = TYPE_SIZE_UNIT (s);
13389 }
13390 else
13391 s = OMP_CLAUSE_SIZE (c);
13392 if (s == NULL_TREE)
13393 s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
13394 s = fold_convert (size_type_node, s);
13395 purpose = size_int (map_idx++);
13396 CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13397 if (TREE_CODE (s) != INTEGER_CST)
13398 TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13399
13400 unsigned HOST_WIDE_INT tkind, tkind_zero;
13401 switch (OMP_CLAUSE_CODE (c))
13402 {
13403 case OMP_CLAUSE_MAP:
13404 tkind = OMP_CLAUSE_MAP_KIND (c);
13405 tkind_zero = tkind;
13406 if (OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c))
13407 switch (tkind)
13408 {
13409 case GOMP_MAP_ALLOC:
13410 case GOMP_MAP_IF_PRESENT:
13411 case GOMP_MAP_TO:
13412 case GOMP_MAP_FROM:
13413 case GOMP_MAP_TOFROM:
13414 case GOMP_MAP_ALWAYS_TO:
13415 case GOMP_MAP_ALWAYS_FROM:
13416 case GOMP_MAP_ALWAYS_TOFROM:
13417 case GOMP_MAP_ALWAYS_PRESENT_TO:
13418 case GOMP_MAP_ALWAYS_PRESENT_FROM:
13419 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
13420 case GOMP_MAP_RELEASE:
13421 case GOMP_MAP_FORCE_TO:
13422 case GOMP_MAP_FORCE_FROM:
13423 case GOMP_MAP_FORCE_TOFROM:
13424 case GOMP_MAP_FORCE_PRESENT:
13425 tkind_zero = GOMP_MAP_ZERO_LEN_ARRAY_SECTION;
13426 break;
13427 case GOMP_MAP_DELETE:
13428 tkind_zero = GOMP_MAP_DELETE_ZERO_LEN_ARRAY_SECTION;
13429 default:
13430 break;
13431 }
13432 if (tkind_zero != tkind)
13433 {
13434 if (integer_zerop (s))
13435 tkind = tkind_zero;
13436 else if (integer_nonzerop (s))
13437 tkind_zero = tkind;
13438 }
13439 if (tkind_zero == tkind
13440 && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (c)
13441 && (((tkind & GOMP_MAP_FLAG_SPECIAL_BITS)
13442 & ~GOMP_MAP_IMPLICIT)
13443 == 0))
13444 {
13445 /* If this is an implicit map, and the GOMP_MAP_IMPLICIT
13446 bits are not interfered by other special bit encodings,
13447 then turn the GOMP_IMPLICIT_BIT flag on for the runtime
13448 to see. */
13449 tkind |= GOMP_MAP_IMPLICIT;
13450 tkind_zero = tkind;
13451 }
13452 break;
13453 case OMP_CLAUSE_FIRSTPRIVATE:
13454 gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
13455 tkind = GOMP_MAP_TO;
13456 tkind_zero = tkind;
13457 break;
13458 case OMP_CLAUSE_TO:
13459 tkind
13460 = (OMP_CLAUSE_MOTION_PRESENT (c)
13461 ? GOMP_MAP_ALWAYS_PRESENT_TO : GOMP_MAP_TO);
13462 tkind_zero = tkind;
13463 break;
13464 case OMP_CLAUSE_FROM:
13465 tkind
13466 = (OMP_CLAUSE_MOTION_PRESENT (c)
13467 ? GOMP_MAP_ALWAYS_PRESENT_FROM : GOMP_MAP_FROM);
13468 tkind_zero = tkind;
13469 break;
13470 default:
13471 gcc_unreachable ();
13472 }
13473 gcc_checking_assert (tkind
13474 < (HOST_WIDE_INT_C (1U) << talign_shift));
13475 gcc_checking_assert (tkind_zero
13476 < (HOST_WIDE_INT_C (1U) << talign_shift));
13477 talign = ceil_log2 (talign);
13478 tkind |= talign << talign_shift;
13479 tkind_zero |= talign << talign_shift;
13480 gcc_checking_assert (tkind
13481 <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13482 gcc_checking_assert (tkind_zero
13483 <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13484 if (tkind == tkind_zero)
13485 x = build_int_cstu (tkind_type, tkind);
13486 else
13487 {
13488 TREE_STATIC (TREE_VEC_ELT (t, 2)) = 0;
13489 x = build3 (COND_EXPR, tkind_type,
13490 fold_build2 (EQ_EXPR, boolean_type_node,
13491 unshare_expr (s), size_zero_node),
13492 build_int_cstu (tkind_type, tkind_zero),
13493 build_int_cstu (tkind_type, tkind));
13494 }
13495 CONSTRUCTOR_APPEND_ELT (vkind, purpose, x);
13496 if (nc && nc != c)
13497 c = nc;
13498 break;
13499
13500 case OMP_CLAUSE_FIRSTPRIVATE:
13501 omp_has_device_addr_descr:
13502 if (is_gimple_omp_oacc (ctx->stmt))
13503 goto oacc_firstprivate_map;
13504 ovar = OMP_CLAUSE_DECL (c);
13505 if (omp_privatize_by_reference (ovar))
13506 talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13507 else
13508 talign = DECL_ALIGN_UNIT (ovar);
13509 var = lookup_decl_in_outer_ctx (ovar, ctx);
13510 x = build_sender_ref (ovar, ctx);
13511 tkind = GOMP_MAP_FIRSTPRIVATE;
13512 type = TREE_TYPE (ovar);
13513 if (omp_privatize_by_reference (ovar))
13514 type = TREE_TYPE (type);
13515 if ((INTEGRAL_TYPE_P (type)
13516 && TYPE_PRECISION (type) <= POINTER_SIZE)
13517 || TREE_CODE (type) == POINTER_TYPE)
13518 {
13519 tkind = GOMP_MAP_FIRSTPRIVATE_INT;
13520 tree t = var;
13521 if (omp_privatize_by_reference (var))
13522 t = build_simple_mem_ref (var);
13523 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13524 suppress_warning (var);
13525 if (TREE_CODE (type) != POINTER_TYPE)
13526 t = fold_convert (pointer_sized_int_node, t);
13527 t = fold_convert (TREE_TYPE (x), t);
13528 gimplify_assign (x, t, &ilist);
13529 }
13530 else if (omp_privatize_by_reference (var))
13531 gimplify_assign (x, var, &ilist);
13532 else if (is_gimple_reg (var))
13533 {
13534 tree avar = create_tmp_var (TREE_TYPE (var));
13535 mark_addressable (avar);
13536 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13537 suppress_warning (var);
13538 gimplify_assign (avar, var, &ilist);
13539 avar = build_fold_addr_expr (avar);
13540 gimplify_assign (x, avar, &ilist);
13541 }
13542 else
13543 {
13544 var = build_fold_addr_expr (var);
13545 gimplify_assign (x, var, &ilist);
13546 }
13547 if (tkind == GOMP_MAP_FIRSTPRIVATE_INT)
13548 s = size_int (0);
13549 else if (omp_privatize_by_reference (ovar))
13550 s = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13551 else
13552 s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
13553 s = fold_convert (size_type_node, s);
13554 purpose = size_int (map_idx++);
13555 CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13556 if (TREE_CODE (s) != INTEGER_CST)
13557 TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13558
13559 gcc_checking_assert (tkind
13560 < (HOST_WIDE_INT_C (1U) << talign_shift));
13561 talign = ceil_log2 (talign);
13562 tkind |= talign << talign_shift;
13563 gcc_checking_assert (tkind
13564 <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13565 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13566 build_int_cstu (tkind_type, tkind));
13567 /* Fortran array descriptors: firstprivate of data + attach. */
13568 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
13569 && lang_hooks.decls.omp_array_data (ovar, true))
13570 {
13571 tree not_null_lb, null_lb, after_lb;
13572 tree var1, var2, size1, size2;
13573 tree present = omp_check_optional_argument (ovar, true);
13574 if (present)
13575 {
13576 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
13577 not_null_lb = create_artificial_label (clause_loc);
13578 null_lb = create_artificial_label (clause_loc);
13579 after_lb = create_artificial_label (clause_loc);
13580 gimple_seq seq = NULL;
13581 present = force_gimple_operand (present, &seq, true,
13582 NULL_TREE);
13583 gimple_seq_add_seq (&ilist, seq);
13584 gimple_seq_add_stmt (&ilist,
13585 gimple_build_cond_from_tree (present,
13586 not_null_lb, null_lb));
13587 gimple_seq_add_stmt (&ilist,
13588 gimple_build_label (not_null_lb));
13589 }
13590 var1 = lang_hooks.decls.omp_array_data (var, false);
13591 size1 = lang_hooks.decls.omp_array_size (var, &ilist);
13592 var2 = build_fold_addr_expr (x);
13593 if (!POINTER_TYPE_P (TREE_TYPE (var)))
13594 var = build_fold_addr_expr (var);
13595 size2 = fold_build2 (POINTER_DIFF_EXPR, ssizetype,
13596 build_fold_addr_expr (var1), var);
13597 size2 = fold_convert (sizetype, size2);
13598 if (present)
13599 {
13600 tree tmp = create_tmp_var (TREE_TYPE (var1));
13601 gimplify_assign (tmp, var1, &ilist);
13602 var1 = tmp;
13603 tmp = create_tmp_var (TREE_TYPE (var2));
13604 gimplify_assign (tmp, var2, &ilist);
13605 var2 = tmp;
13606 tmp = create_tmp_var (TREE_TYPE (size1));
13607 gimplify_assign (tmp, size1, &ilist);
13608 size1 = tmp;
13609 tmp = create_tmp_var (TREE_TYPE (size2));
13610 gimplify_assign (tmp, size2, &ilist);
13611 size2 = tmp;
13612 gimple_seq_add_stmt (&ilist, gimple_build_goto (after_lb));
13613 gimple_seq_add_stmt (&ilist, gimple_build_label (null_lb));
13614 gimplify_assign (var1, null_pointer_node, &ilist);
13615 gimplify_assign (var2, null_pointer_node, &ilist);
13616 gimplify_assign (size1, size_zero_node, &ilist);
13617 gimplify_assign (size2, size_zero_node, &ilist);
13618 gimple_seq_add_stmt (&ilist, gimple_build_label (after_lb));
13619 }
13620 x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
13621 gimplify_assign (x, var1, &ilist);
13622 tkind = GOMP_MAP_FIRSTPRIVATE;
13623 talign = DECL_ALIGN_UNIT (ovar);
13624 talign = ceil_log2 (talign);
13625 tkind |= talign << talign_shift;
13626 gcc_checking_assert (tkind
13627 <= tree_to_uhwi (
13628 TYPE_MAX_VALUE (tkind_type)));
13629 purpose = size_int (map_idx++);
13630 CONSTRUCTOR_APPEND_ELT (vsize, purpose, size1);
13631 if (TREE_CODE (size1) != INTEGER_CST)
13632 TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13633 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13634 build_int_cstu (tkind_type, tkind));
13635 x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
13636 gimplify_assign (x, var2, &ilist);
13637 tkind = GOMP_MAP_ATTACH;
13638 purpose = size_int (map_idx++);
13639 CONSTRUCTOR_APPEND_ELT (vsize, purpose, size2);
13640 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13641 build_int_cstu (tkind_type, tkind));
13642 }
13643 break;
13644
13645 case OMP_CLAUSE_USE_DEVICE_PTR:
13646 case OMP_CLAUSE_USE_DEVICE_ADDR:
13647 case OMP_CLAUSE_HAS_DEVICE_ADDR:
13648 case OMP_CLAUSE_IS_DEVICE_PTR:
13649 ovar = OMP_CLAUSE_DECL (c);
13650 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13651 {
13652 if (lang_hooks.decls.omp_array_data (ovar, true))
13653 goto omp_has_device_addr_descr;
13654 while (TREE_CODE (ovar) == INDIRECT_REF
13655 || TREE_CODE (ovar) == ARRAY_REF)
13656 ovar = TREE_OPERAND (ovar, 0);
13657 }
13658 var = lookup_decl_in_outer_ctx (ovar, ctx);
13659
13660 if (lang_hooks.decls.omp_array_data (ovar, true))
13661 {
13662 tkind = ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13663 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13664 ? GOMP_MAP_USE_DEVICE_PTR : GOMP_MAP_FIRSTPRIVATE_INT);
13665 x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
13666 }
13667 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13668 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13669 {
13670 tkind = GOMP_MAP_USE_DEVICE_PTR;
13671 x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
13672 }
13673 else
13674 {
13675 tkind = GOMP_MAP_FIRSTPRIVATE_INT;
13676 x = build_sender_ref (ovar, ctx);
13677 }
13678
13679 if (is_gimple_omp_oacc (ctx->stmt))
13680 {
13681 gcc_assert (tkind == GOMP_MAP_USE_DEVICE_PTR);
13682
13683 if (OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c))
13684 tkind = GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT;
13685 }
13686
13687 type = TREE_TYPE (ovar);
13688 if (lang_hooks.decls.omp_array_data (ovar, true))
13689 var = lang_hooks.decls.omp_array_data (var, false);
13690 else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
13691 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13692 && !omp_privatize_by_reference (ovar)
13693 && !omp_is_allocatable_or_ptr (ovar))
13694 || TREE_CODE (type) == ARRAY_TYPE)
13695 var = build_fold_addr_expr (var);
13696 else
13697 {
13698 if (omp_privatize_by_reference (ovar)
13699 || omp_check_optional_argument (ovar, false)
13700 || omp_is_allocatable_or_ptr (ovar))
13701 {
13702 type = TREE_TYPE (type);
13703 if (POINTER_TYPE_P (type)
13704 && TREE_CODE (type) != ARRAY_TYPE
13705 && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
13706 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
13707 && !omp_is_allocatable_or_ptr (ovar))
13708 || (omp_privatize_by_reference (ovar)
13709 && omp_is_allocatable_or_ptr (ovar))))
13710 var = build_simple_mem_ref (var);
13711 var = fold_convert (TREE_TYPE (x), var);
13712 }
13713 }
13714 tree present;
13715 present = omp_check_optional_argument (ovar, true);
13716 if (present)
13717 {
13718 tree null_label = create_artificial_label (UNKNOWN_LOCATION);
13719 tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
13720 tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
13721 tree new_x = unshare_expr (x);
13722 gimplify_expr (&present, &ilist, NULL, is_gimple_val,
13723 fb_rvalue);
13724 gcond *cond = gimple_build_cond_from_tree (present,
13725 notnull_label,
13726 null_label);
13727 gimple_seq_add_stmt (&ilist, cond);
13728 gimple_seq_add_stmt (&ilist, gimple_build_label (null_label));
13729 gimplify_assign (new_x, null_pointer_node, &ilist);
13730 gimple_seq_add_stmt (&ilist, gimple_build_goto (opt_arg_label));
13731 gimple_seq_add_stmt (&ilist,
13732 gimple_build_label (notnull_label));
13733 gimplify_assign (x, var, &ilist);
13734 gimple_seq_add_stmt (&ilist,
13735 gimple_build_label (opt_arg_label));
13736 }
13737 else
13738 gimplify_assign (x, var, &ilist);
13739 s = size_int (0);
13740 purpose = size_int (map_idx++);
13741 CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13742 gcc_checking_assert (tkind
13743 < (HOST_WIDE_INT_C (1U) << talign_shift));
13744 gcc_checking_assert (tkind
13745 <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13746 CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13747 build_int_cstu (tkind_type, tkind));
13748 break;
13749 }
13750
13751 gcc_assert (map_idx == map_cnt);
13752
13753 if (!deep_map_cnt)
13754 {
13755 DECL_INITIAL (TREE_VEC_ELT (t, 1))
13756 = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 1)), vsize);
13757 DECL_INITIAL (TREE_VEC_ELT (t, 2))
13758 = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 2)), vkind);
13759 }
13760 for (int i = 1; i <= 2; i++)
13761 if (deep_map_cnt || !TREE_STATIC (TREE_VEC_ELT (t, i)))
13762 {
13763 tree tmp = TREE_VEC_ELT (t, i);
13764 if (deep_map_cnt)
13765 {
13766 const char *prefix = (i == 1 ? ".omp_data_sizes0"
13767 : ".omp_data_kinds0");
13768 tree type = (i == 1) ? size_type_node : tkind_type;
13769 type = build_array_type_nelts (type, map_cnt);
13770 tree var = create_tmp_var (type, prefix);
13771 DECL_NAMELESS (var) = 1;
13772 TREE_ADDRESSABLE (var) = 1;
13773 TREE_STATIC (var) = TREE_STATIC (tmp);
13774 DECL_INITIAL (var) = build_constructor (type, i == 1
13775 ? vsize : vkind);
13776 tmp = var;
13777 TREE_STATIC (TREE_VEC_ELT (t, i)) = 0;
13778 }
13779
13780 gimple_seq initlist = NULL;
13781 force_gimple_operand (build1 (DECL_EXPR, void_type_node, tmp),
13782 &initlist, true, NULL_TREE);
13783 gimple_seq_add_seq (&ilist, initlist);
13784
13785 if (deep_map_cnt)
13786 {
13787 tree tmp2;
13788 tree call = builtin_decl_explicit (BUILT_IN_MEMCPY);
13789 tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13790 call = build_call_expr_loc (input_location, call, 3,
13791 TREE_VEC_ELT (t, i),
13792 build_fold_addr_expr (tmp), tmp2);
13793 gimplify_and_add (call, &ilist);
13794 }
13795
13796 if (!TREE_STATIC (tmp))
13797 {
13798 tree clobber = build_clobber (TREE_TYPE (tmp));
13799 gimple_seq_add_stmt (&olist,
13800 gimple_build_assign (tmp, clobber));
13801 }
13802 if (deep_map_cnt)
13803 {
13804 tmp = TREE_VEC_ELT (t, i);
13805 tree call = builtin_decl_explicit (BUILT_IN_FREE);
13806 call = build_call_expr_loc (input_location, call, 1, tmp);
13807 gimplify_and_add (call, &olist);
13808 tree clobber = build_clobber (TREE_TYPE (tmp));
13809 gimple_seq_add_stmt (&olist,
13810 gimple_build_assign (tmp, clobber));
13811 }
13812 }
13813 else if (omp_maybe_offloaded_ctx (ctx->outer))
13814 {
13815 tree id = get_identifier ("omp declare target");
13816 tree decl = TREE_VEC_ELT (t, i);
13817 DECL_ATTRIBUTES (decl)
13818 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13819 varpool_node *node = varpool_node::get (decl);
13820 if (node)
13821 {
13822 node->offloadable = 1;
13823 if (ENABLE_OFFLOADING)
13824 {
13825 g->have_offload = true;
13826 vec_safe_push (offload_vars, t);
13827 }
13828 }
13829 }
13830
13831 if (deep_map_cnt)
13832 {
13833 tree call = builtin_decl_explicit (BUILT_IN_FREE);
13834 call = build_call_expr_loc (input_location, call, 1,
13835 TREE_VEC_ELT (t, 0));
13836 gimplify_and_add (call, &olist);
13837
13838 gimplify_expr (&TREE_VEC_ELT (t, 1), &ilist, NULL, is_gimple_val,
13839 fb_rvalue);
13840 }
13841
13842 tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
13843 gimple_seq_add_stmt (&olist, gimple_build_assign (ctx->sender_decl,
13844 clobber));
13845 }
13846
13847 /* Once all the expansions are done, sequence all the different
13848 fragments inside gimple_omp_body. */
13849
13850 new_body = NULL;
13851
13852 if (offloaded
13853 && ctx->record_type)
13854 {
13855 t = ctx->sender_decl;
13856 if (!deep_map_cnt)
13857 t = build_fold_addr_expr_loc (loc, t);
13858 /* fixup_child_record_type might have changed receiver_decl's type. */
13859 t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
13860 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctx->sender_decl)))
13861 gimplify_assign (ctx->receiver_decl, t, &new_body);
13862 else
13863 gimple_seq_add_stmt (&new_body,
13864 gimple_build_assign (ctx->receiver_decl, t));
13865 }
13866 gimple_seq_add_seq (&new_body, fplist);
13867
13868 if (offloaded || data_region)
13869 {
13870 tree prev = NULL_TREE;
13871 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13872 switch (OMP_CLAUSE_CODE (c))
13873 {
13874 tree var, x;
13875 default:
13876 break;
13877 case OMP_CLAUSE_FIRSTPRIVATE:
13878 omp_firstprivatize_data_region:
13879 if (is_gimple_omp_oacc (ctx->stmt))
13880 break;
13881 var = OMP_CLAUSE_DECL (c);
13882 if (omp_privatize_by_reference (var)
13883 || is_gimple_reg_type (TREE_TYPE (var)))
13884 {
13885 tree new_var = lookup_decl (var, ctx);
13886 tree type;
13887 type = TREE_TYPE (var);
13888 if (omp_privatize_by_reference (var))
13889 type = TREE_TYPE (type);
13890 if ((INTEGRAL_TYPE_P (type)
13891 && TYPE_PRECISION (type) <= POINTER_SIZE)
13892 || TREE_CODE (type) == POINTER_TYPE)
13893 {
13894 x = build_receiver_ref (var, false, ctx);
13895 if (TREE_CODE (type) != POINTER_TYPE)
13896 x = fold_convert (pointer_sized_int_node, x);
13897 x = fold_convert (type, x);
13898 gimplify_expr (&x, &new_body, NULL, is_gimple_val,
13899 fb_rvalue);
13900 if (omp_privatize_by_reference (var))
13901 {
13902 tree v = create_tmp_var_raw (type, get_name (var));
13903 gimple_add_tmp_var (v);
13904 TREE_ADDRESSABLE (v) = 1;
13905 gimple_seq_add_stmt (&new_body,
13906 gimple_build_assign (v, x));
13907 x = build_fold_addr_expr (v);
13908 }
13909 gimple_seq_add_stmt (&new_body,
13910 gimple_build_assign (new_var, x));
13911 }
13912 else
13913 {
13914 bool by_ref = !omp_privatize_by_reference (var);
13915 x = build_receiver_ref (var, by_ref, ctx);
13916 gimplify_expr (&x, &new_body, NULL, is_gimple_val,
13917 fb_rvalue);
13918 gimple_seq_add_stmt (&new_body,
13919 gimple_build_assign (new_var, x));
13920 }
13921 }
13922 else if (is_variable_sized (var))
13923 {
13924 tree pvar = DECL_VALUE_EXPR (var);
13925 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
13926 pvar = TREE_OPERAND (pvar, 0);
13927 gcc_assert (DECL_P (pvar));
13928 tree new_var = lookup_decl (pvar, ctx);
13929 x = build_receiver_ref (var, false, ctx);
13930 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
13931 gimple_seq_add_stmt (&new_body,
13932 gimple_build_assign (new_var, x));
13933 }
13934 break;
13935 case OMP_CLAUSE_PRIVATE:
13936 if (is_gimple_omp_oacc (ctx->stmt))
13937 break;
13938 var = OMP_CLAUSE_DECL (c);
13939 if (omp_privatize_by_reference (var))
13940 {
13941 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
13942 tree new_var = lookup_decl (var, ctx);
13943 x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
13944 if (TREE_CONSTANT (x))
13945 {
13946 x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
13947 get_name (var));
13948 gimple_add_tmp_var (x);
13949 TREE_ADDRESSABLE (x) = 1;
13950 x = build_fold_addr_expr_loc (clause_loc, x);
13951 }
13952 else
13953 break;
13954
13955 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
13956 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
13957 gimple_seq_add_stmt (&new_body,
13958 gimple_build_assign (new_var, x));
13959 }
13960 break;
13961 case OMP_CLAUSE_USE_DEVICE_PTR:
13962 case OMP_CLAUSE_USE_DEVICE_ADDR:
13963 case OMP_CLAUSE_HAS_DEVICE_ADDR:
13964 case OMP_CLAUSE_IS_DEVICE_PTR:
13965 tree new_var;
13966 gimple_seq assign_body;
13967 bool is_array_data;
13968 bool do_optional_check;
13969 assign_body = NULL;
13970 do_optional_check = false;
13971 var = OMP_CLAUSE_DECL (c);
13972 is_array_data = lang_hooks.decls.omp_array_data (var, true) != NULL;
13973 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR && is_array_data)
13974 goto omp_firstprivatize_data_region;
13975
13976 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13977 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13978 x = build_sender_ref (is_array_data
13979 ? (splay_tree_key) &DECL_NAME (var)
13980 : (splay_tree_key) &DECL_UID (var), ctx);
13981 else
13982 {
13983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13984 {
13985 while (TREE_CODE (var) == INDIRECT_REF
13986 || TREE_CODE (var) == ARRAY_REF)
13987 var = TREE_OPERAND (var, 0);
13988 }
13989 x = build_receiver_ref (var, false, ctx);
13990 }
13991
13992 if (is_array_data)
13993 {
13994 bool is_ref = omp_privatize_by_reference (var);
13995 do_optional_check = true;
13996 /* First, we copy the descriptor data from the host; then
13997 we update its data to point to the target address. */
13998 new_var = lookup_decl (var, ctx);
13999 new_var = DECL_VALUE_EXPR (new_var);
14000 tree v = new_var;
14001 tree v2 = var;
14002 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14003 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR)
14004 v2 = maybe_lookup_decl_in_outer_ctx (var, ctx);
14005
14006 if (is_ref)
14007 {
14008 v2 = build_fold_indirect_ref (v2);
14009 v = create_tmp_var_raw (TREE_TYPE (v2), get_name (var));
14010 gimple_add_tmp_var (v);
14011 TREE_ADDRESSABLE (v) = 1;
14012 gimplify_assign (v, v2, &assign_body);
14013 tree rhs = build_fold_addr_expr (v);
14014 gimple_seq_add_stmt (&assign_body,
14015 gimple_build_assign (new_var, rhs));
14016 }
14017 else
14018 gimplify_assign (new_var, v2, &assign_body);
14019
14020 v2 = lang_hooks.decls.omp_array_data (unshare_expr (v), false);
14021 gcc_assert (v2);
14022 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14023 gimple_seq_add_stmt (&assign_body,
14024 gimple_build_assign (v2, x));
14025 }
14026 else if (is_variable_sized (var))
14027 {
14028 tree pvar = DECL_VALUE_EXPR (var);
14029 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
14030 pvar = TREE_OPERAND (pvar, 0);
14031 gcc_assert (DECL_P (pvar));
14032 new_var = lookup_decl (pvar, ctx);
14033 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14034 gimple_seq_add_stmt (&assign_body,
14035 gimple_build_assign (new_var, x));
14036 }
14037 else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
14038 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14039 && !omp_privatize_by_reference (var)
14040 && !omp_is_allocatable_or_ptr (var))
14041 || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
14042 {
14043 new_var = lookup_decl (var, ctx);
14044 new_var = DECL_VALUE_EXPR (new_var);
14045 gcc_assert (TREE_CODE (new_var) == MEM_REF);
14046 new_var = TREE_OPERAND (new_var, 0);
14047 gcc_assert (DECL_P (new_var));
14048 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14049 gimple_seq_add_stmt (&assign_body,
14050 gimple_build_assign (new_var, x));
14051 }
14052 else
14053 {
14054 tree type = TREE_TYPE (var);
14055 new_var = lookup_decl (var, ctx);
14056 if (omp_privatize_by_reference (var))
14057 {
14058 type = TREE_TYPE (type);
14059 if (POINTER_TYPE_P (type)
14060 && TREE_CODE (type) != ARRAY_TYPE
14061 && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
14062 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
14063 || (omp_privatize_by_reference (var)
14064 && omp_is_allocatable_or_ptr (var))))
14065 {
14066 tree v = create_tmp_var_raw (type, get_name (var));
14067 gimple_add_tmp_var (v);
14068 TREE_ADDRESSABLE (v) = 1;
14069 x = fold_convert (type, x);
14070 gimplify_expr (&x, &assign_body, NULL, is_gimple_val,
14071 fb_rvalue);
14072 gimple_seq_add_stmt (&assign_body,
14073 gimple_build_assign (v, x));
14074 x = build_fold_addr_expr (v);
14075 do_optional_check = true;
14076 }
14077 }
14078 new_var = DECL_VALUE_EXPR (new_var);
14079 x = fold_convert (TREE_TYPE (new_var), x);
14080 gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14081 gimple_seq_add_stmt (&assign_body,
14082 gimple_build_assign (new_var, x));
14083 }
14084 tree present;
14085 present = ((do_optional_check
14086 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
14087 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR)
14088 ? omp_check_optional_argument (OMP_CLAUSE_DECL (c), true)
14089 : NULL_TREE);
14090 if (present)
14091 {
14092 tree null_label = create_artificial_label (UNKNOWN_LOCATION);
14093 tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
14094 tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
14095 glabel *null_glabel = gimple_build_label (null_label);
14096 glabel *notnull_glabel = gimple_build_label (notnull_label);
14097 ggoto *opt_arg_ggoto = gimple_build_goto (opt_arg_label);
14098 gimplify_expr (&x, &new_body, NULL, is_gimple_val,
14099 fb_rvalue);
14100 gimplify_expr (&present, &new_body, NULL, is_gimple_val,
14101 fb_rvalue);
14102 gcond *cond = gimple_build_cond_from_tree (present,
14103 notnull_label,
14104 null_label);
14105 gimple_seq_add_stmt (&new_body, cond);
14106 gimple_seq_add_stmt (&new_body, null_glabel);
14107 gimplify_assign (new_var, null_pointer_node, &new_body);
14108 gimple_seq_add_stmt (&new_body, opt_arg_ggoto);
14109 gimple_seq_add_stmt (&new_body, notnull_glabel);
14110 gimple_seq_add_seq (&new_body, assign_body);
14111 gimple_seq_add_stmt (&new_body,
14112 gimple_build_label (opt_arg_label));
14113 }
14114 else
14115 gimple_seq_add_seq (&new_body, assign_body);
14116 break;
14117 }
14118 /* Handle GOMP_MAP_FIRSTPRIVATE_{POINTER,REFERENCE} in second pass,
14119 so that firstprivate vars holding OMP_CLAUSE_SIZE if needed
14120 are already handled. Similarly OMP_CLAUSE_PRIVATE for VLAs
14121 or references to VLAs. */
14122 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14123 switch (OMP_CLAUSE_CODE (c))
14124 {
14125 tree var;
14126 default:
14127 break;
14128 case OMP_CLAUSE_MAP:
14129 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14130 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
14131 {
14132 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14133 poly_int64 offset = 0;
14134 gcc_assert (prev);
14135 var = OMP_CLAUSE_DECL (c);
14136 if (DECL_P (var)
14137 && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
14138 && is_global_var (maybe_lookup_decl_in_outer_ctx (var,
14139 ctx))
14140 && varpool_node::get_create (var)->offloadable)
14141 break;
14142 if (TREE_CODE (var) == INDIRECT_REF
14143 && TREE_CODE (TREE_OPERAND (var, 0)) == COMPONENT_REF)
14144 var = TREE_OPERAND (var, 0);
14145 if (TREE_CODE (var) == COMPONENT_REF)
14146 {
14147 var = get_addr_base_and_unit_offset (var, &offset);
14148 gcc_assert (var != NULL_TREE && DECL_P (var));
14149 }
14150 else if (DECL_SIZE (var)
14151 && TREE_CODE (DECL_SIZE (var)) != INTEGER_CST)
14152 {
14153 tree var2 = DECL_VALUE_EXPR (var);
14154 gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
14155 var2 = TREE_OPERAND (var2, 0);
14156 gcc_assert (DECL_P (var2));
14157 var = var2;
14158 }
14159 tree new_var = lookup_decl (var, ctx), x;
14160 tree type = TREE_TYPE (new_var);
14161 bool is_ref;
14162 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == INDIRECT_REF
14163 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14164 == COMPONENT_REF))
14165 {
14166 type = TREE_TYPE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0));
14167 is_ref = true;
14168 new_var = build2 (MEM_REF, type,
14169 build_fold_addr_expr (new_var),
14170 build_int_cst (build_pointer_type (type),
14171 offset));
14172 }
14173 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14174 {
14175 type = TREE_TYPE (OMP_CLAUSE_DECL (c));
14176 is_ref = TREE_CODE (type) == REFERENCE_TYPE;
14177 new_var = build2 (MEM_REF, type,
14178 build_fold_addr_expr (new_var),
14179 build_int_cst (build_pointer_type (type),
14180 offset));
14181 }
14182 else
14183 is_ref = omp_privatize_by_reference (var);
14184 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
14185 is_ref = false;
14186 bool ref_to_array = false;
14187 bool ref_to_ptr = false;
14188 if (is_ref)
14189 {
14190 type = TREE_TYPE (type);
14191 if (TREE_CODE (type) == ARRAY_TYPE)
14192 {
14193 type = build_pointer_type (type);
14194 ref_to_array = true;
14195 }
14196 }
14197 else if (TREE_CODE (type) == ARRAY_TYPE)
14198 {
14199 tree decl2 = DECL_VALUE_EXPR (new_var);
14200 gcc_assert (TREE_CODE (decl2) == MEM_REF);
14201 decl2 = TREE_OPERAND (decl2, 0);
14202 gcc_assert (DECL_P (decl2));
14203 new_var = decl2;
14204 type = TREE_TYPE (new_var);
14205 }
14206 else if (TREE_CODE (type) == REFERENCE_TYPE
14207 && TREE_CODE (TREE_TYPE (type)) == POINTER_TYPE)
14208 {
14209 type = TREE_TYPE (type);
14210 ref_to_ptr = true;
14211 }
14212 x = build_receiver_ref (OMP_CLAUSE_DECL (prev), false, ctx);
14213 x = fold_convert_loc (clause_loc, type, x);
14214 if (!integer_zerop (OMP_CLAUSE_SIZE (c)))
14215 {
14216 tree bias = OMP_CLAUSE_SIZE (c);
14217 if (DECL_P (bias))
14218 bias = lookup_decl (bias, ctx);
14219 bias = fold_convert_loc (clause_loc, sizetype, bias);
14220 bias = fold_build1_loc (clause_loc, NEGATE_EXPR, sizetype,
14221 bias);
14222 x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
14223 TREE_TYPE (x), x, bias);
14224 }
14225 if (ref_to_array)
14226 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
14227 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14228 if ((is_ref && !ref_to_array)
14229 || ref_to_ptr)
14230 {
14231 tree t = create_tmp_var_raw (type, get_name (var));
14232 gimple_add_tmp_var (t);
14233 TREE_ADDRESSABLE (t) = 1;
14234 gimple_seq_add_stmt (&new_body,
14235 gimple_build_assign (t, x));
14236 x = build_fold_addr_expr_loc (clause_loc, t);
14237 }
14238 gimple_seq_add_stmt (&new_body,
14239 gimple_build_assign (new_var, x));
14240 prev = NULL_TREE;
14241 }
14242 else if (OMP_CLAUSE_CHAIN (c)
14243 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c))
14244 == OMP_CLAUSE_MAP
14245 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
14246 == GOMP_MAP_FIRSTPRIVATE_POINTER
14247 || (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
14248 == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
14249 prev = c;
14250 break;
14251 case OMP_CLAUSE_PRIVATE:
14252 var = OMP_CLAUSE_DECL (c);
14253 if (is_variable_sized (var))
14254 {
14255 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14256 tree new_var = lookup_decl (var, ctx);
14257 tree pvar = DECL_VALUE_EXPR (var);
14258 gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
14259 pvar = TREE_OPERAND (pvar, 0);
14260 gcc_assert (DECL_P (pvar));
14261 tree new_pvar = lookup_decl (pvar, ctx);
14262 tree atmp = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
14263 tree al = size_int (DECL_ALIGN (var));
14264 tree x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
14265 x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
14266 x = fold_convert_loc (clause_loc, TREE_TYPE (new_pvar), x);
14267 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14268 gimple_seq_add_stmt (&new_body,
14269 gimple_build_assign (new_pvar, x));
14270 }
14271 else if (omp_privatize_by_reference (var)
14272 && !is_gimple_omp_oacc (ctx->stmt))
14273 {
14274 location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14275 tree new_var = lookup_decl (var, ctx);
14276 tree x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
14277 if (TREE_CONSTANT (x))
14278 break;
14279 else
14280 {
14281 tree atmp
14282 = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
14283 tree rtype = TREE_TYPE (TREE_TYPE (new_var));
14284 tree al = size_int (TYPE_ALIGN (rtype));
14285 x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
14286 }
14287
14288 x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
14289 gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14290 gimple_seq_add_stmt (&new_body,
14291 gimple_build_assign (new_var, x));
14292 }
14293 break;
14294 }
14295
14296 gimple_seq fork_seq = NULL;
14297 gimple_seq join_seq = NULL;
14298
14299 if (offloaded && is_gimple_omp_oacc (ctx->stmt))
14300 {
14301 /* If there are reductions on the offloaded region itself, treat
14302 them as a dummy GANG loop. */
14303 tree level = build_int_cst (integer_type_node, GOMP_DIM_GANG);
14304
14305 gcall *private_marker = lower_oacc_private_marker (ctx);
14306
14307 if (private_marker)
14308 gimple_call_set_arg (private_marker, 2, level);
14309
14310 lower_oacc_reductions (gimple_location (ctx->stmt), clauses, level,
14311 false, NULL, private_marker, NULL, &fork_seq,
14312 &join_seq, ctx);
14313 }
14314
14315 gimple_seq_add_seq (&new_body, fork_seq);
14316 gimple_seq_add_seq (&new_body, tgt_body);
14317 gimple_seq_add_seq (&new_body, join_seq);
14318
14319 if (offloaded)
14320 {
14321 new_body = maybe_catch_exception (new_body);
14322 gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
14323 }
14324 gimple_omp_set_body (stmt, new_body);
14325 }
14326
14327 bind = gimple_build_bind (NULL, NULL,
14328 tgt_bind ? gimple_bind_block (tgt_bind)
14329 : NULL_TREE);
14330 gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
14331 gimple_bind_add_seq (bind, ilist);
14332 gimple_bind_add_stmt (bind, stmt);
14333 gimple_bind_add_seq (bind, olist);
14334
14335 pop_gimplify_context (NULL);
14336
14337 if (dep_bind)
14338 {
14339 gimple_bind_add_seq (dep_bind, dep_ilist);
14340 gimple_bind_add_stmt (dep_bind, bind);
14341 gimple_bind_add_seq (dep_bind, dep_olist);
14342 pop_gimplify_context (dep_bind);
14343 }
14344 }
14345
14346 /* Generate code to implement the action-clauses (destroy, init, use) of an
14347 OpenMP interop construct. */
14348
14349 static void
14350 lower_omp_interop_action_clauses (gimple_seq *seq, vec<tree> &objs,
14351 vec<tree> *interop_types = NULL,
14352 vec<tree> *prefer_types = NULL)
14353 {
14354 if (objs.length () == 0)
14355 return;
14356
14357 enum omp_clause_code action = OMP_CLAUSE_CODE (objs[0]);
14358 if (action == OMP_CLAUSE_INIT)
14359 gcc_checking_assert (objs.length () == interop_types->length ()
14360 && objs.length () == prefer_types->length ());
14361 else
14362 gcc_assert (prefer_types == NULL && interop_types == NULL);
14363
14364 tree ret_objs = NULL_TREE, ret_interop_types = NULL_TREE,
14365 ret_prefer_types = NULL_TREE;
14366
14367 /* Build an array of interop objects. */
14368
14369 tree type_obj_pref = build_array_type_nelts (ptr_type_node, objs.length ());
14370 ret_objs = create_tmp_var (type_obj_pref, "interopobjs");
14371
14372 bool have_pref_type = false;
14373 if (action == OMP_CLAUSE_INIT)
14374 {
14375 for (tree pref_type : prefer_types)
14376 if (pref_type != NULL_TREE)
14377 {
14378 have_pref_type = true;
14379 break;
14380 }
14381 tree type_tgtsync
14382 = build_array_type_nelts (integer_type_node, objs.length ());
14383 ret_interop_types = create_tmp_var (type_tgtsync, "tgt_tgtsync");
14384 if (have_pref_type)
14385 ret_prefer_types = create_tmp_var (type_obj_pref, "pref_type");
14386 else
14387 {
14388 ret_prefer_types = null_pointer_node;
14389 prefer_types->truncate (0);
14390 }
14391 }
14392
14393 for (size_t i = 0; !objs.is_empty (); i++)
14394 {
14395 tree offset = build_int_cst (integer_type_node, i);
14396 tree init = build4 (ARRAY_REF, ptr_type_node, ret_objs, offset, NULL_TREE,
14397 NULL_TREE);
14398 tree obj = OMP_CLAUSE_DECL (objs.pop ());
14399 if (TREE_CODE (TREE_TYPE (obj)) == REFERENCE_TYPE)
14400 obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
14401 if (action != OMP_CLAUSE_USE
14402 && TREE_CODE (TREE_TYPE (obj)) != POINTER_TYPE)
14403 /* For modifying actions, we need a pointer. */
14404 obj = build_fold_addr_expr (obj);
14405 else if (action == OMP_CLAUSE_USE
14406 && TREE_CODE (TREE_TYPE (obj)) == POINTER_TYPE)
14407 /* For use action, we need the value. */
14408 obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
14409 init = build2 (MODIFY_EXPR, ptr_type_node, init,
14410 fold_convert (ptr_type_node, obj));
14411 gimplify_and_add (init, seq);
14412
14413 if (action == OMP_CLAUSE_INIT)
14414 {
14415 init = build4 (ARRAY_REF, integer_type_node, ret_interop_types,
14416 offset, NULL_TREE, NULL_TREE);
14417 init = build2 (MODIFY_EXPR, integer_type_node, init,
14418 interop_types->pop ());
14419 gimplify_and_add (init, seq);
14420
14421 if (have_pref_type)
14422 {
14423 tree prefer_type = prefer_types->pop ();
14424 tree pref = (prefer_type == NULL_TREE
14425 ? null_pointer_node
14426 : build_fold_addr_expr (prefer_type));
14427 init = build4 (ARRAY_REF, ptr_type_node, ret_prefer_types, offset,
14428 NULL_TREE, NULL_TREE);
14429 init = build2 (MODIFY_EXPR, ptr_type_node, init, pref);
14430 gimplify_and_add (init, seq);
14431 }
14432 }
14433 }
14434 if (action == OMP_CLAUSE_INIT)
14435 {
14436 if (have_pref_type)
14437 ret_prefer_types = build_fold_addr_expr (ret_prefer_types);
14438 ret_interop_types = build_fold_addr_expr (ret_interop_types);
14439 }
14440 ret_objs = build_fold_addr_expr (ret_objs);
14441
14442 gcc_assert (objs.is_empty ()
14443 && (!interop_types || interop_types->is_empty ())
14444 && (!prefer_types || prefer_types->is_empty ()));
14445
14446 objs.safe_push (ret_objs);
14447 if (action == OMP_CLAUSE_INIT)
14448 {
14449 interop_types->safe_push (ret_interop_types);
14450 prefer_types->safe_push (ret_prefer_types);
14451 }
14452 }
14453
14454 /* Lower code for an OpenMP interop directive. */
14455
14456 static void
14457 lower_omp_interop (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14458 {
14459 push_gimplify_context ();
14460
14461 tree block = make_node (BLOCK);
14462 gbind *bind = gimple_build_bind (NULL, NULL, block);
14463 gimple_seq bind_body = NULL;
14464
14465 /* Emit call to GOMP_interop:
14466 void
14467 GOMP_interop (int device_num, int n_init, omp_interop_t **init,
14468 const void *target_targetsync, const void *prefer_type,
14469 int n_use, omp_interop_t *use, int n_destroy,
14470 omp_interop_t **destroy, unsigned int flags,
14471 void **depend) */
14472
14473 tree flags = NULL_TREE;
14474 tree depend = null_pointer_node;
14475 tree device_num = NULL_TREE;
14476
14477 auto_vec<tree> init_objs, use_objs, destroy_objs, prefer_type,
14478 target_targetsync;
14479 gimple_seq dep_ilist = NULL, dep_olist = NULL;
14480 tree clauses = gimple_omp_interop_clauses (gsi_stmt (*gsi_p));
14481 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14482 {
14483 switch (OMP_CLAUSE_CODE (c))
14484 {
14485 case OMP_CLAUSE_INIT:
14486 {
14487 init_objs.safe_push (c);
14488 int target_targetsync_bits = 0;
14489 if (OMP_CLAUSE_INIT_TARGET (c))
14490 target_targetsync_bits |= GOMP_INTEROP_TARGET;
14491 if (OMP_CLAUSE_INIT_TARGETSYNC (c))
14492 target_targetsync_bits |= GOMP_INTEROP_TARGETSYNC;
14493 tree t = build_int_cst (integer_type_node, target_targetsync_bits);
14494 target_targetsync.safe_push (t);
14495 prefer_type.safe_push (OMP_CLAUSE_INIT_PREFER_TYPE (c));
14496 }
14497 break;
14498 case OMP_CLAUSE_USE:
14499 use_objs.safe_push (c);
14500 break;
14501 case OMP_CLAUSE_DESTROY:
14502 destroy_objs.safe_push (c);
14503 break;
14504 case OMP_CLAUSE_NOWAIT:
14505 flags = build_int_cst (integer_type_node, GOMP_INTEROP_FLAG_NOWAIT);
14506 break;
14507 case OMP_CLAUSE_DEPEND:
14508 {
14509 tree *cp = gimple_omp_interop_clauses_ptr (gsi_stmt (*gsi_p));
14510 lower_depend_clauses (cp, &dep_ilist, &dep_olist);
14511 depend = OMP_CLAUSE_DECL (*cp);
14512 }
14513 break;
14514 case OMP_CLAUSE_DEVICE:
14515 device_num = OMP_CLAUSE_DEVICE_ID (c);
14516 break;
14517 default:
14518 gcc_unreachable ();
14519 }
14520 }
14521
14522 if (flags == NULL_TREE)
14523 flags = build_int_cst (integer_type_node, 0);
14524
14525 if (device_num == NULL_TREE)
14526 device_num = build_int_cst (integer_type_node, GOMP_DEVICE_DEFAULT_OMP_61);
14527
14528 tree n_init = build_int_cst (integer_type_node, init_objs.length ());
14529 tree n_use = build_int_cst (integer_type_node, use_objs.length ());
14530 tree n_destroy = build_int_cst (integer_type_node, destroy_objs.length ());
14531
14532 lower_omp_interop_action_clauses (&bind_body, init_objs, &target_targetsync,
14533 &prefer_type);
14534 lower_omp_interop_action_clauses (&bind_body, use_objs);
14535 lower_omp_interop_action_clauses (&bind_body, destroy_objs);
14536
14537 gimple_seq_add_seq (&bind_body, dep_ilist);
14538 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
14539 tree init_arg = init_objs.length () ? init_objs.pop () : null_pointer_node;
14540 tree target_targetsync_arg = target_targetsync.length ()
14541 ? target_targetsync.pop ()
14542 : null_pointer_node;
14543 tree prefer_type_arg
14544 = prefer_type.length () ? prefer_type.pop () : null_pointer_node;
14545 tree use_arg = use_objs.length () ? use_objs.pop () : null_pointer_node;
14546 tree destroy_arg
14547 = destroy_objs.length () ? destroy_objs.pop () : null_pointer_node;
14548 gcall *call
14549 = gimple_build_call (fn, 11, device_num, n_init, init_arg,
14550 target_targetsync_arg, prefer_type_arg, n_use, use_arg,
14551 n_destroy, destroy_arg, flags, depend);
14552 gimple_seq_add_stmt (&bind_body, call);
14553 gimple_seq_add_seq (&bind_body, dep_olist);
14554
14555 gsi_replace (gsi_p, bind, true);
14556 gimple_bind_set_body (bind, bind_body);
14557 pop_gimplify_context (bind);
14558 gimple_bind_append_vars (bind, ctx->block_vars);
14559 BLOCK_VARS (block) = ctx->block_vars;
14560 }
14561
14562 /* Expand code for an OpenMP teams directive. */
14563
14564 static void
14565 lower_omp_teams (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14566 {
14567 gomp_teams *teams_stmt = as_a <gomp_teams *> (gsi_stmt (*gsi_p));
14568 push_gimplify_context ();
14569
14570 tree block = make_node (BLOCK);
14571 gbind *bind = gimple_build_bind (NULL, NULL, block);
14572 gsi_replace (gsi_p, bind, true);
14573 gimple_seq bind_body = NULL;
14574 gimple_seq dlist = NULL;
14575 gimple_seq olist = NULL;
14576
14577 tree num_teams = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
14578 OMP_CLAUSE_NUM_TEAMS);
14579 tree num_teams_lower = NULL_TREE;
14580 if (num_teams == NULL_TREE)
14581 num_teams = build_int_cst (unsigned_type_node, 0);
14582 else
14583 {
14584 num_teams_lower = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (num_teams);
14585 if (num_teams_lower)
14586 {
14587 num_teams_lower = fold_convert (unsigned_type_node, num_teams_lower);
14588 gimplify_expr (&num_teams_lower, &bind_body, NULL, is_gimple_val,
14589 fb_rvalue);
14590 }
14591 num_teams = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
14592 num_teams = fold_convert (unsigned_type_node, num_teams);
14593 gimplify_expr (&num_teams, &bind_body, NULL, is_gimple_val, fb_rvalue);
14594 }
14595 if (num_teams_lower == NULL_TREE)
14596 num_teams_lower = num_teams;
14597 tree thread_limit = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
14598 OMP_CLAUSE_THREAD_LIMIT);
14599 if (thread_limit == NULL_TREE)
14600 thread_limit = build_int_cst (unsigned_type_node, 0);
14601 else
14602 {
14603 thread_limit = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
14604 thread_limit = fold_convert (unsigned_type_node, thread_limit);
14605 gimplify_expr (&thread_limit, &bind_body, NULL, is_gimple_val,
14606 fb_rvalue);
14607 }
14608 location_t loc = gimple_location (teams_stmt);
14609 tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TEAMS4);
14610 tree rettype = TREE_TYPE (TREE_TYPE (decl));
14611 tree first = create_tmp_var (rettype);
14612 gimple_seq_add_stmt (&bind_body,
14613 gimple_build_assign (first, build_one_cst (rettype)));
14614 tree llabel = create_artificial_label (loc);
14615 gimple_seq_add_stmt (&bind_body, gimple_build_label (llabel));
14616 gimple *call
14617 = gimple_build_call (decl, 4, num_teams_lower, num_teams, thread_limit,
14618 first);
14619 gimple_set_location (call, loc);
14620 tree temp = create_tmp_var (rettype);
14621 gimple_call_set_lhs (call, temp);
14622 gimple_seq_add_stmt (&bind_body, call);
14623
14624 tree tlabel = create_artificial_label (loc);
14625 tree flabel = create_artificial_label (loc);
14626 gimple *cond = gimple_build_cond (NE_EXPR, temp, build_zero_cst (rettype),
14627 tlabel, flabel);
14628 gimple_seq_add_stmt (&bind_body, cond);
14629 gimple_seq_add_stmt (&bind_body, gimple_build_label (tlabel));
14630 gimple_seq_add_stmt (&bind_body,
14631 gimple_build_assign (first, build_zero_cst (rettype)));
14632
14633 lower_rec_input_clauses (gimple_omp_teams_clauses (teams_stmt),
14634 &bind_body, &dlist, ctx, NULL);
14635 lower_omp (gimple_omp_body_ptr (teams_stmt), ctx);
14636 lower_reduction_clauses (gimple_omp_teams_clauses (teams_stmt), &olist,
14637 NULL, ctx);
14638 gimple_seq_add_stmt (&bind_body, teams_stmt);
14639
14640 gimple_seq_add_seq (&bind_body, gimple_omp_body (teams_stmt));
14641 gimple_omp_set_body (teams_stmt, NULL);
14642 gimple_seq_add_seq (&bind_body, olist);
14643 gimple_seq_add_seq (&bind_body, dlist);
14644 gimple_seq_add_stmt (&bind_body, gimple_build_omp_return (true));
14645 gimple_seq_add_stmt (&bind_body, gimple_build_goto (llabel));
14646 gimple_seq_add_stmt (&bind_body, gimple_build_label (flabel));
14647 gimple_bind_set_body (bind, bind_body);
14648
14649 pop_gimplify_context (bind);
14650
14651 gimple_bind_append_vars (bind, ctx->block_vars);
14652 BLOCK_VARS (block) = ctx->block_vars;
14653 if (BLOCK_VARS (block))
14654 TREE_USED (block) = 1;
14655 }
14656
14657 /* Callback for lower_omp_1. Return non-NULL if *tp needs to be
14658 regimplified. If DATA is non-NULL, lower_omp_1 is outside
14659 of OMP context, but with make_addressable_vars set. */
14660
14661 static tree
14662 lower_omp_regimplify_p (tree *tp, int *walk_subtrees,
14663 void *data)
14664 {
14665 tree t = *tp;
14666
14667 /* Any variable with DECL_VALUE_EXPR needs to be regimplified. */
14668 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
14669 && data == NULL
14670 && DECL_HAS_VALUE_EXPR_P (t))
14671 return t;
14672
14673 if (make_addressable_vars
14674 && DECL_P (t)
14675 && bitmap_bit_p (make_addressable_vars, DECL_UID (t)))
14676 return t;
14677
14678 /* If a global variable has been privatized, TREE_CONSTANT on
14679 ADDR_EXPR might be wrong. */
14680 if (data == NULL && TREE_CODE (t) == ADDR_EXPR)
14681 recompute_tree_invariant_for_addr_expr (t);
14682
14683 *walk_subtrees = !IS_TYPE_OR_DECL_P (t);
14684 return NULL_TREE;
14685 }
14686
14687 /* Data to be communicated between lower_omp_regimplify_operands and
14688 lower_omp_regimplify_operands_p. */
14689
14690 struct lower_omp_regimplify_operands_data
14691 {
14692 omp_context *ctx;
14693 vec<tree> *decls;
14694 };
14695
14696 /* Helper function for lower_omp_regimplify_operands. Find
14697 omp_member_access_dummy_var vars and adjust temporarily their
14698 DECL_VALUE_EXPRs if needed. */
14699
14700 static tree
14701 lower_omp_regimplify_operands_p (tree *tp, int *walk_subtrees,
14702 void *data)
14703 {
14704 tree t = omp_member_access_dummy_var (*tp);
14705 if (t)
14706 {
14707 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
14708 lower_omp_regimplify_operands_data *ldata
14709 = (lower_omp_regimplify_operands_data *) wi->info;
14710 tree o = maybe_lookup_decl (t, ldata->ctx);
14711 if (o == NULL_TREE)
14712 o = maybe_lookup_decl_in_outer_ctx (t, ldata->ctx);
14713 if (o != t)
14714 {
14715 ldata->decls->safe_push (DECL_VALUE_EXPR (*tp));
14716 ldata->decls->safe_push (*tp);
14717 tree v = unshare_and_remap (DECL_VALUE_EXPR (*tp), t, o);
14718 SET_DECL_VALUE_EXPR (*tp, v);
14719 }
14720 }
14721 *walk_subtrees = !IS_TYPE_OR_DECL_P (*tp);
14722 return NULL_TREE;
14723 }
14724
14725 /* Wrapper around gimple_regimplify_operands that adjusts DECL_VALUE_EXPRs
14726 of omp_member_access_dummy_var vars during regimplification. */
14727
14728 static void
14729 lower_omp_regimplify_operands (omp_context *ctx, gimple *stmt,
14730 gimple_stmt_iterator *gsi_p)
14731 {
14732 auto_vec<tree, 10> decls;
14733 if (ctx)
14734 {
14735 struct walk_stmt_info wi;
14736 memset (&wi, '\0', sizeof (wi));
14737 struct lower_omp_regimplify_operands_data data;
14738 data.ctx = ctx;
14739 data.decls = &decls;
14740 wi.info = &data;
14741 walk_gimple_op (stmt, lower_omp_regimplify_operands_p, &wi);
14742 }
14743 gimple_regimplify_operands (stmt, gsi_p);
14744 while (!decls.is_empty ())
14745 {
14746 tree t = decls.pop ();
14747 tree v = decls.pop ();
14748 SET_DECL_VALUE_EXPR (t, v);
14749 }
14750 }
14751
14752 static void
14753 lower_omp_1 (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14754 {
14755 gimple *stmt = gsi_stmt (*gsi_p);
14756 struct walk_stmt_info wi;
14757 gcall *call_stmt;
14758
14759 if (gimple_has_location (stmt))
14760 input_location = gimple_location (stmt);
14761
14762 if (make_addressable_vars)
14763 memset (&wi, '\0', sizeof (wi));
14764
14765 /* If we have issued syntax errors, avoid doing any heavy lifting.
14766 Just replace the OMP directives with a NOP to avoid
14767 confusing RTL expansion. */
14768 if (seen_error () && is_gimple_omp (stmt))
14769 {
14770 gsi_replace (gsi_p, gimple_build_nop (), true);
14771 return;
14772 }
14773
14774 switch (gimple_code (stmt))
14775 {
14776 case GIMPLE_COND:
14777 {
14778 gcond *cond_stmt = as_a <gcond *> (stmt);
14779 if ((ctx || make_addressable_vars)
14780 && (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
14781 lower_omp_regimplify_p,
14782 ctx ? NULL : &wi, NULL)
14783 || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
14784 lower_omp_regimplify_p,
14785 ctx ? NULL : &wi, NULL)))
14786 lower_omp_regimplify_operands (ctx, cond_stmt, gsi_p);
14787 }
14788 break;
14789 case GIMPLE_CATCH:
14790 lower_omp (gimple_catch_handler_ptr (as_a <gcatch *> (stmt)), ctx);
14791 break;
14792 case GIMPLE_EH_FILTER:
14793 lower_omp (gimple_eh_filter_failure_ptr (stmt), ctx);
14794 break;
14795 case GIMPLE_TRY:
14796 lower_omp (gimple_try_eval_ptr (stmt), ctx);
14797 lower_omp (gimple_try_cleanup_ptr (stmt), ctx);
14798 break;
14799 case GIMPLE_ASSUME:
14800 lower_omp (gimple_assume_body_ptr (stmt), ctx);
14801 break;
14802 case GIMPLE_TRANSACTION:
14803 lower_omp (gimple_transaction_body_ptr (as_a <gtransaction *> (stmt)),
14804 ctx);
14805 break;
14806 case GIMPLE_BIND:
14807 if (ctx && is_gimple_omp_oacc (ctx->stmt))
14808 {
14809 tree vars = gimple_bind_vars (as_a <gbind *> (stmt));
14810 oacc_privatization_scan_decl_chain (ctx, vars);
14811 }
14812 lower_omp (gimple_bind_body_ptr (as_a <gbind *> (stmt)), ctx);
14813 maybe_remove_omp_member_access_dummy_vars (as_a <gbind *> (stmt));
14814 break;
14815 case GIMPLE_OMP_PARALLEL:
14816 case GIMPLE_OMP_TASK:
14817 ctx = maybe_lookup_ctx (stmt);
14818 gcc_assert (ctx);
14819 if (ctx->cancellable)
14820 ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14821 lower_omp_taskreg (gsi_p, ctx);
14822 break;
14823 case GIMPLE_OMP_FOR:
14824 ctx = maybe_lookup_ctx (stmt);
14825 gcc_assert (ctx);
14826 if (ctx->cancellable)
14827 ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14828 lower_omp_for (gsi_p, ctx);
14829 break;
14830 case GIMPLE_OMP_SECTIONS:
14831 ctx = maybe_lookup_ctx (stmt);
14832 gcc_assert (ctx);
14833 if (ctx->cancellable)
14834 ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14835 lower_omp_sections (gsi_p, ctx);
14836 break;
14837 case GIMPLE_OMP_SCOPE:
14838 ctx = maybe_lookup_ctx (stmt);
14839 gcc_assert (ctx);
14840 lower_omp_scope (gsi_p, ctx);
14841 break;
14842 case GIMPLE_OMP_DISPATCH:
14843 ctx = maybe_lookup_ctx (stmt);
14844 gcc_assert (ctx);
14845 lower_omp_dispatch (gsi_p, ctx);
14846 break;
14847 case GIMPLE_OMP_INTEROP:
14848 ctx = maybe_lookup_ctx (stmt);
14849 gcc_assert (ctx);
14850 lower_omp_interop (gsi_p, ctx);
14851 break;
14852 case GIMPLE_OMP_SINGLE:
14853 ctx = maybe_lookup_ctx (stmt);
14854 gcc_assert (ctx);
14855 lower_omp_single (gsi_p, ctx);
14856 break;
14857 case GIMPLE_OMP_STRUCTURED_BLOCK:
14858 /* We have already done error checking at this point, so these nodes
14859 can be completely removed and replaced with their body. */
14860 ctx = maybe_lookup_ctx (stmt);
14861 gcc_assert (ctx);
14862 lower_omp (gimple_omp_body_ptr (stmt), ctx);
14863 gsi_replace_with_seq (gsi_p, gimple_omp_body (stmt), true);
14864 break;
14865 case GIMPLE_OMP_MASTER:
14866 case GIMPLE_OMP_MASKED:
14867 ctx = maybe_lookup_ctx (stmt);
14868 gcc_assert (ctx);
14869 lower_omp_master (gsi_p, ctx);
14870 break;
14871 case GIMPLE_OMP_TASKGROUP:
14872 ctx = maybe_lookup_ctx (stmt);
14873 gcc_assert (ctx);
14874 lower_omp_taskgroup (gsi_p, ctx);
14875 break;
14876 case GIMPLE_OMP_ORDERED:
14877 ctx = maybe_lookup_ctx (stmt);
14878 gcc_assert (ctx);
14879 lower_omp_ordered (gsi_p, ctx);
14880 break;
14881 case GIMPLE_OMP_SCAN:
14882 ctx = maybe_lookup_ctx (stmt);
14883 gcc_assert (ctx);
14884 lower_omp_scan (gsi_p, ctx);
14885 break;
14886 case GIMPLE_OMP_CRITICAL:
14887 ctx = maybe_lookup_ctx (stmt);
14888 gcc_assert (ctx);
14889 lower_omp_critical (gsi_p, ctx);
14890 break;
14891 case GIMPLE_OMP_ATOMIC_LOAD:
14892 if ((ctx || make_addressable_vars)
14893 && walk_tree (gimple_omp_atomic_load_rhs_ptr (
14894 as_a <gomp_atomic_load *> (stmt)),
14895 lower_omp_regimplify_p, ctx ? NULL : &wi, NULL))
14896 lower_omp_regimplify_operands (ctx, stmt, gsi_p);
14897 break;
14898 case GIMPLE_OMP_TARGET:
14899 ctx = maybe_lookup_ctx (stmt);
14900 gcc_assert (ctx);
14901 lower_omp_target (gsi_p, ctx);
14902 break;
14903 case GIMPLE_OMP_TEAMS:
14904 ctx = maybe_lookup_ctx (stmt);
14905 gcc_assert (ctx);
14906 if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
14907 lower_omp_taskreg (gsi_p, ctx);
14908 else
14909 lower_omp_teams (gsi_p, ctx);
14910 break;
14911 case GIMPLE_CALL:
14912 tree fndecl;
14913 call_stmt = as_a <gcall *> (stmt);
14914 fndecl = gimple_call_fndecl (call_stmt);
14915 if (fndecl
14916 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
14917 switch (DECL_FUNCTION_CODE (fndecl))
14918 {
14919 case BUILT_IN_GOMP_BARRIER:
14920 if (ctx == NULL)
14921 break;
14922 /* FALLTHRU */
14923 case BUILT_IN_GOMP_CANCEL:
14924 case BUILT_IN_GOMP_CANCELLATION_POINT:
14925 omp_context *cctx;
14926 cctx = ctx;
14927 if (gimple_code (cctx->stmt) == GIMPLE_OMP_SECTION)
14928 cctx = cctx->outer;
14929 gcc_assert (gimple_call_lhs (call_stmt) == NULL_TREE);
14930 if (!cctx->cancellable)
14931 {
14932 if (DECL_FUNCTION_CODE (fndecl)
14933 == BUILT_IN_GOMP_CANCELLATION_POINT)
14934 {
14935 stmt = gimple_build_nop ();
14936 gsi_replace (gsi_p, stmt, false);
14937 }
14938 break;
14939 }
14940 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_GOMP_BARRIER)
14941 {
14942 fndecl = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER_CANCEL);
14943 gimple_call_set_fndecl (call_stmt, fndecl);
14944 gimple_call_set_fntype (call_stmt, TREE_TYPE (fndecl));
14945 }
14946 tree lhs;
14947 lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (fndecl)));
14948 gimple_call_set_lhs (call_stmt, lhs);
14949 tree fallthru_label;
14950 fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
14951 gimple *g;
14952 g = gimple_build_label (fallthru_label);
14953 gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
14954 g = gimple_build_cond (NE_EXPR, lhs,
14955 fold_convert (TREE_TYPE (lhs),
14956 boolean_false_node),
14957 cctx->cancel_label, fallthru_label);
14958 gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
14959 break;
14960 default:
14961 break;
14962 }
14963 goto regimplify;
14964
14965 case GIMPLE_ASSIGN:
14966 for (omp_context *up = ctx; up; up = up->outer)
14967 {
14968 if (gimple_code (up->stmt) == GIMPLE_OMP_ORDERED
14969 || gimple_code (up->stmt) == GIMPLE_OMP_CRITICAL
14970 || gimple_code (up->stmt) == GIMPLE_OMP_TASKGROUP
14971 || gimple_code (up->stmt) == GIMPLE_OMP_SCOPE
14972 || gimple_code (up->stmt) == GIMPLE_OMP_SECTION
14973 || gimple_code (up->stmt) == GIMPLE_OMP_SCAN
14974 || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
14975 && (gimple_omp_target_kind (up->stmt)
14976 == GF_OMP_TARGET_KIND_DATA)))
14977 continue;
14978 else if (!up->lastprivate_conditional_map)
14979 break;
14980 tree lhs = get_base_address (gimple_assign_lhs (stmt));
14981 if (TREE_CODE (lhs) == MEM_REF
14982 && DECL_P (TREE_OPERAND (lhs, 0))
14983 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs,
14984 0))) == REFERENCE_TYPE)
14985 lhs = TREE_OPERAND (lhs, 0);
14986 if (DECL_P (lhs))
14987 if (tree *v = up->lastprivate_conditional_map->get (lhs))
14988 {
14989 tree clauses;
14990 if (up->combined_into_simd_safelen1)
14991 {
14992 up = up->outer;
14993 if (gimple_code (up->stmt) == GIMPLE_OMP_SCAN)
14994 up = up->outer;
14995 }
14996 if (gimple_code (up->stmt) == GIMPLE_OMP_FOR)
14997 clauses = gimple_omp_for_clauses (up->stmt);
14998 else
14999 clauses = gimple_omp_sections_clauses (up->stmt);
15000 tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
15001 if (!OMP_CLAUSE__CONDTEMP__ITER (c))
15002 c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
15003 OMP_CLAUSE__CONDTEMP_);
15004 gcc_assert (OMP_CLAUSE__CONDTEMP__ITER (c));
15005 gimple *g = gimple_build_assign (*v, OMP_CLAUSE_DECL (c));
15006 gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
15007 }
15008 }
15009 /* FALLTHRU */
15010
15011 default:
15012 regimplify:
15013 if ((ctx || make_addressable_vars)
15014 && walk_gimple_op (stmt, lower_omp_regimplify_p,
15015 ctx ? NULL : &wi))
15016 {
15017 /* Just remove clobbers, this should happen only if we have
15018 "privatized" local addressable variables in SIMD regions,
15019 the clobber isn't needed in that case and gimplifying address
15020 of the ARRAY_REF into a pointer and creating MEM_REF based
15021 clobber would create worse code than we get with the clobber
15022 dropped. */
15023 if (gimple_clobber_p (stmt))
15024 {
15025 gsi_replace (gsi_p, gimple_build_nop (), true);
15026 break;
15027 }
15028 lower_omp_regimplify_operands (ctx, stmt, gsi_p);
15029 }
15030 break;
15031 }
15032 }
15033
15034 static void
15035 lower_omp (gimple_seq *body, omp_context *ctx)
15036 {
15037 location_t saved_location = input_location;
15038 gimple_stmt_iterator gsi;
15039 for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
15040 lower_omp_1 (&gsi, ctx);
15041 /* During gimplification, we haven't folded statments inside offloading
15042 or taskreg regions (gimplify.cc:maybe_fold_stmt); do that now. */
15043 if (target_nesting_level || taskreg_nesting_level)
15044 for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
15045 fold_stmt (&gsi);
15046 input_location = saved_location;
15047 }
15048
15049 /* Main entry point. */
15050
15051 static unsigned int
15052 execute_lower_omp (void)
15053 {
15054 gimple_seq body;
15055 int i;
15056 omp_context *ctx;
15057
15058 /* This pass always runs, to provide PROP_gimple_lomp.
15059 But often, there is nothing to do. */
15060 if (flag_openacc == 0 && flag_openmp == 0
15061 && flag_openmp_simd == 0)
15062 return 0;
15063
15064 all_contexts = splay_tree_new (splay_tree_compare_pointers, 0,
15065 delete_omp_context);
15066
15067 body = gimple_body (current_function_decl);
15068
15069 scan_omp (&body, NULL);
15070 gcc_assert (taskreg_nesting_level == 0);
15071 FOR_EACH_VEC_ELT (taskreg_contexts, i, ctx)
15072 finish_taskreg_scan (ctx);
15073 taskreg_contexts.release ();
15074
15075 if (all_contexts->root)
15076 {
15077 if (make_addressable_vars)
15078 push_gimplify_context ();
15079 lower_omp (&body, NULL);
15080 if (make_addressable_vars)
15081 pop_gimplify_context (NULL);
15082 }
15083
15084 if (all_contexts)
15085 {
15086 splay_tree_delete (all_contexts);
15087 all_contexts = NULL;
15088 }
15089 BITMAP_FREE (make_addressable_vars);
15090 BITMAP_FREE (global_nonaddressable_vars);
15091
15092 /* If current function is a method, remove artificial dummy VAR_DECL created
15093 for non-static data member privatization, they aren't needed for
15094 debuginfo nor anything else, have been already replaced everywhere in the
15095 IL and cause problems with LTO. */
15096 if (DECL_ARGUMENTS (current_function_decl)
15097 && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
15098 && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
15099 == POINTER_TYPE))
15100 remove_member_access_dummy_vars (DECL_INITIAL (current_function_decl));
15101
15102 for (auto task_stmt : task_cpyfns)
15103 finalize_task_copyfn (task_stmt);
15104 task_cpyfns.release ();
15105 return 0;
15106 }
15107
15108 namespace {
15109
15110 const pass_data pass_data_lower_omp =
15111 {
15112 GIMPLE_PASS, /* type */
15113 "omplower", /* name */
15114 OPTGROUP_OMP, /* optinfo_flags */
15115 TV_NONE, /* tv_id */
15116 PROP_gimple_any, /* properties_required */
15117 PROP_gimple_lomp | PROP_gimple_lomp_dev, /* properties_provided */
15118 0, /* properties_destroyed */
15119 0, /* todo_flags_start */
15120 0, /* todo_flags_finish */
15121 };
15122
15123 class pass_lower_omp : public gimple_opt_pass
15124 {
15125 public:
15126 pass_lower_omp (gcc::context *ctxt)
15127 : gimple_opt_pass (pass_data_lower_omp, ctxt)
15128 {}
15129
15130 /* opt_pass methods: */
15131 unsigned int execute (function *) final override
15132 {
15133 return execute_lower_omp ();
15134 }
15135
15136 }; // class pass_lower_omp
15137
15138 } // anon namespace
15139
15140 gimple_opt_pass *
15141 make_pass_lower_omp (gcc::context *ctxt)
15142 {
15143 return new pass_lower_omp (ctxt);
15144 }
15145
15146 /* The following is a utility to diagnose structured block violations.
15147 It is not part of the "omplower" pass, as that's invoked too late. It
15148 should be invoked by the respective front ends after gimplification. */
15149
15150 static splay_tree all_labels;
15151
15152 /* Check for mismatched contexts and generate an error if needed. Return
15153 true if an error is detected. */
15154
15155 static bool
15156 diagnose_sb_0 (gimple_stmt_iterator *gsi_p,
15157 gimple *branch_ctx, gimple *label_ctx)
15158 {
15159 gcc_checking_assert (!branch_ctx || is_gimple_omp (branch_ctx));
15160 gcc_checking_assert (!label_ctx || is_gimple_omp (label_ctx));
15161
15162 if (label_ctx == branch_ctx)
15163 return false;
15164
15165 const char* kind = NULL;
15166
15167 if (flag_openacc)
15168 {
15169 if ((branch_ctx && is_gimple_omp_oacc (branch_ctx))
15170 || (label_ctx && is_gimple_omp_oacc (label_ctx)))
15171 {
15172 gcc_checking_assert (kind == NULL);
15173 kind = "OpenACC";
15174 }
15175 }
15176 if (kind == NULL)
15177 {
15178 gcc_checking_assert (flag_openmp || flag_openmp_simd);
15179 kind = "OpenMP";
15180 }
15181
15182 /* Previously we kept track of the label's entire context in diagnose_sb_[12]
15183 so we could traverse it and issue a correct "exit" or "enter" error
15184 message upon a structured block violation.
15185
15186 We built the context by building a list with tree_cons'ing, but there is
15187 no easy counterpart in gimple tuples. It seems like far too much work
15188 for issuing exit/enter error messages. If someone really misses the
15189 distinct error message... patches welcome. */
15190
15191 #if 0
15192 /* Try to avoid confusing the user by producing and error message
15193 with correct "exit" or "enter" verbiage. We prefer "exit"
15194 unless we can show that LABEL_CTX is nested within BRANCH_CTX. */
15195 if (branch_ctx == NULL)
15196 exit_p = false;
15197 else
15198 {
15199 while (label_ctx)
15200 {
15201 if (TREE_VALUE (label_ctx) == branch_ctx)
15202 {
15203 exit_p = false;
15204 break;
15205 }
15206 label_ctx = TREE_CHAIN (label_ctx);
15207 }
15208 }
15209
15210 if (exit_p)
15211 error ("invalid exit from %s structured block", kind);
15212 else
15213 error ("invalid entry to %s structured block", kind);
15214 #endif
15215
15216 /* If it's obvious we have an invalid entry, be specific about the error. */
15217 if (branch_ctx == NULL)
15218 error ("invalid entry to %s structured block", kind);
15219 else
15220 {
15221 /* Otherwise, be vague and lazy, but efficient. */
15222 error ("invalid branch to/from %s structured block", kind);
15223 }
15224
15225 gsi_replace (gsi_p, gimple_build_nop (), false);
15226 return true;
15227 }
15228
15229 /* Pass 1: Create a minimal tree of structured blocks, and record
15230 where each label is found. */
15231
15232 static tree
15233 diagnose_sb_1 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
15234 struct walk_stmt_info *wi)
15235 {
15236 gimple *context = (gimple *) wi->info;
15237 gimple *inner_context;
15238 gimple *stmt = gsi_stmt (*gsi_p);
15239
15240 *handled_ops_p = true;
15241
15242 switch (gimple_code (stmt))
15243 {
15244 WALK_SUBSTMTS;
15245
15246 case GIMPLE_OMP_PARALLEL:
15247 case GIMPLE_OMP_TASK:
15248 case GIMPLE_OMP_SCOPE:
15249 case GIMPLE_OMP_SECTIONS:
15250 case GIMPLE_OMP_SINGLE:
15251 case GIMPLE_OMP_SECTION:
15252 case GIMPLE_OMP_STRUCTURED_BLOCK:
15253 case GIMPLE_OMP_MASTER:
15254 case GIMPLE_OMP_MASKED:
15255 case GIMPLE_OMP_ORDERED:
15256 case GIMPLE_OMP_SCAN:
15257 case GIMPLE_OMP_CRITICAL:
15258 case GIMPLE_OMP_TARGET:
15259 case GIMPLE_OMP_TEAMS:
15260 case GIMPLE_OMP_TASKGROUP:
15261 /* The minimal context here is just the current OMP construct. */
15262 inner_context = stmt;
15263 wi->info = inner_context;
15264 walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
15265 wi->info = context;
15266 break;
15267
15268 case GIMPLE_OMP_FOR:
15269 inner_context = stmt;
15270 wi->info = inner_context;
15271 /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
15272 walk them. */
15273 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
15274 diagnose_sb_1, NULL, wi);
15275 walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
15276 wi->info = context;
15277 break;
15278
15279 case GIMPLE_LABEL:
15280 splay_tree_insert (all_labels,
15281 (splay_tree_key) gimple_label_label (
15282 as_a <glabel *> (stmt)),
15283 (splay_tree_value) context);
15284 break;
15285
15286 default:
15287 break;
15288 }
15289
15290 return NULL_TREE;
15291 }
15292
15293 /* Pass 2: Check each branch and see if its context differs from that of
15294 the destination label's context. */
15295
15296 static tree
15297 diagnose_sb_2 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
15298 struct walk_stmt_info *wi)
15299 {
15300 gimple *context = (gimple *) wi->info;
15301 splay_tree_node n;
15302 gimple *stmt = gsi_stmt (*gsi_p);
15303
15304 *handled_ops_p = true;
15305
15306 switch (gimple_code (stmt))
15307 {
15308 WALK_SUBSTMTS;
15309
15310 case GIMPLE_OMP_PARALLEL:
15311 case GIMPLE_OMP_TASK:
15312 case GIMPLE_OMP_SCOPE:
15313 case GIMPLE_OMP_SECTIONS:
15314 case GIMPLE_OMP_SINGLE:
15315 case GIMPLE_OMP_SECTION:
15316 case GIMPLE_OMP_STRUCTURED_BLOCK:
15317 case GIMPLE_OMP_MASTER:
15318 case GIMPLE_OMP_MASKED:
15319 case GIMPLE_OMP_ORDERED:
15320 case GIMPLE_OMP_SCAN:
15321 case GIMPLE_OMP_CRITICAL:
15322 case GIMPLE_OMP_TARGET:
15323 case GIMPLE_OMP_TEAMS:
15324 case GIMPLE_OMP_TASKGROUP:
15325 wi->info = stmt;
15326 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
15327 wi->info = context;
15328 break;
15329
15330 case GIMPLE_OMP_FOR:
15331 wi->info = stmt;
15332 /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
15333 walk them. */
15334 walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt),
15335 diagnose_sb_2, NULL, wi);
15336 walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
15337 wi->info = context;
15338 break;
15339
15340 case GIMPLE_COND:
15341 {
15342 gcond *cond_stmt = as_a <gcond *> (stmt);
15343 tree lab = gimple_cond_true_label (cond_stmt);
15344 if (lab)
15345 {
15346 n = splay_tree_lookup (all_labels,
15347 (splay_tree_key) lab);
15348 diagnose_sb_0 (gsi_p, context,
15349 n ? (gimple *) n->value : NULL);
15350 }
15351 lab = gimple_cond_false_label (cond_stmt);
15352 if (lab)
15353 {
15354 n = splay_tree_lookup (all_labels,
15355 (splay_tree_key) lab);
15356 diagnose_sb_0 (gsi_p, context,
15357 n ? (gimple *) n->value : NULL);
15358 }
15359 }
15360 break;
15361
15362 case GIMPLE_GOTO:
15363 {
15364 tree lab = gimple_goto_dest (stmt);
15365 if (TREE_CODE (lab) != LABEL_DECL)
15366 break;
15367
15368 n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
15369 diagnose_sb_0 (gsi_p, context, n ? (gimple *) n->value : NULL);
15370 }
15371 break;
15372
15373 case GIMPLE_SWITCH:
15374 {
15375 gswitch *switch_stmt = as_a <gswitch *> (stmt);
15376 unsigned int i;
15377 for (i = 0; i < gimple_switch_num_labels (switch_stmt); ++i)
15378 {
15379 tree lab = CASE_LABEL (gimple_switch_label (switch_stmt, i));
15380 n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
15381 if (n && diagnose_sb_0 (gsi_p, context, (gimple *) n->value))
15382 break;
15383 }
15384 }
15385 break;
15386
15387 case GIMPLE_ASM:
15388 {
15389 gasm *asm_stmt = as_a <gasm *> (stmt);
15390 for (unsigned i = 0; i < gimple_asm_nlabels (asm_stmt); ++i)
15391 {
15392 tree lab = TREE_VALUE (gimple_asm_label_op (asm_stmt, i));
15393 n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
15394 if (n && diagnose_sb_0 (gsi_p, context, (gimple *) n->value))
15395 break;
15396 }
15397 }
15398 break;
15399
15400 case GIMPLE_RETURN:
15401 diagnose_sb_0 (gsi_p, context, NULL);
15402 break;
15403
15404 default:
15405 break;
15406 }
15407
15408 return NULL_TREE;
15409 }
15410
15411 static unsigned int
15412 diagnose_omp_structured_block_errors (void)
15413 {
15414 struct walk_stmt_info wi;
15415 gimple_seq body = gimple_body (current_function_decl);
15416
15417 all_labels = splay_tree_new (splay_tree_compare_pointers, 0, 0);
15418
15419 memset (&wi, 0, sizeof (wi));
15420 walk_gimple_seq (body, diagnose_sb_1, NULL, &wi);
15421
15422 memset (&wi, 0, sizeof (wi));
15423 wi.want_locations = true;
15424 walk_gimple_seq_mod (&body, diagnose_sb_2, NULL, &wi);
15425
15426 gimple_set_body (current_function_decl, body);
15427
15428 splay_tree_delete (all_labels);
15429 all_labels = NULL;
15430
15431 return 0;
15432 }
15433
15434 namespace {
15435
15436 const pass_data pass_data_diagnose_omp_blocks =
15437 {
15438 GIMPLE_PASS, /* type */
15439 "*diagnose_omp_blocks", /* name */
15440 OPTGROUP_OMP, /* optinfo_flags */
15441 TV_NONE, /* tv_id */
15442 PROP_gimple_any, /* properties_required */
15443 0, /* properties_provided */
15444 0, /* properties_destroyed */
15445 0, /* todo_flags_start */
15446 0, /* todo_flags_finish */
15447 };
15448
15449 class pass_diagnose_omp_blocks : public gimple_opt_pass
15450 {
15451 public:
15452 pass_diagnose_omp_blocks (gcc::context *ctxt)
15453 : gimple_opt_pass (pass_data_diagnose_omp_blocks, ctxt)
15454 {}
15455
15456 /* opt_pass methods: */
15457 bool gate (function *) final override
15458 {
15459 return flag_openacc || flag_openmp || flag_openmp_simd;
15460 }
15461 unsigned int execute (function *) final override
15462 {
15463 return diagnose_omp_structured_block_errors ();
15464 }
15465
15466 }; // class pass_diagnose_omp_blocks
15467
15468 } // anon namespace
15469
15470 gimple_opt_pass *
15471 make_pass_diagnose_omp_blocks (gcc::context *ctxt)
15472 {
15473 return new pass_diagnose_omp_blocks (ctxt);
15474 }
15475
15476
15477 #include "gt-omp-low.h"