Branch data Line data Source code
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 <dnovillo@redhat.com>
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 : 17778 : is_oacc_parallel_or_serial (omp_context *ctx)
216 : : {
217 : 17778 : enum gimple_code outer_type = gimple_code (ctx->stmt);
218 : 17778 : return ((outer_type == GIMPLE_OMP_TARGET)
219 : 17778 : && ((gimple_omp_target_kind (ctx->stmt)
220 : : == GF_OMP_TARGET_KIND_OACC_PARALLEL)
221 : 3360 : || (gimple_omp_target_kind (ctx->stmt)
222 : 17778 : == 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 : 49942 : is_oacc_kernels (omp_context *ctx)
230 : : {
231 : 49942 : enum gimple_code outer_type = gimple_code (ctx->stmt);
232 : 49942 : return ((outer_type == GIMPLE_OMP_TARGET)
233 : 49942 : && (gimple_omp_target_kind (ctx->stmt)
234 : 49942 : == GF_OMP_TARGET_KIND_OACC_KERNELS));
235 : : }
236 : :
237 : : /* Return whether CTX represents an OpenACC 'kernels' decomposed part. */
238 : :
239 : : static bool
240 : 21645 : is_oacc_kernels_decomposed_part (omp_context *ctx)
241 : : {
242 : 21645 : enum gimple_code outer_type = gimple_code (ctx->stmt);
243 : 21645 : return ((outer_type == GIMPLE_OMP_TARGET)
244 : 21645 : && ((gimple_omp_target_kind (ctx->stmt)
245 : : == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED)
246 : 14534 : || (gimple_omp_target_kind (ctx->stmt)
247 : : == GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE)
248 : 14534 : || (gimple_omp_target_kind (ctx->stmt)
249 : 21645 : == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS)));
250 : : }
251 : :
252 : : /* Return true if STMT corresponds to an OpenMP target region. */
253 : : static bool
254 : 42516 : is_omp_target (gimple *stmt)
255 : : {
256 : 42516 : if (gimple_code (stmt) == GIMPLE_OMP_TARGET)
257 : : {
258 : 6186 : int kind = gimple_omp_target_kind (stmt);
259 : 6186 : return (kind == GF_OMP_TARGET_KIND_REGION
260 : 6186 : || kind == GF_OMP_TARGET_KIND_DATA
261 : 6186 : || kind == GF_OMP_TARGET_KIND_ENTER_DATA
262 : 10506 : || 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 : 481788 : omp_member_access_dummy_var (tree decl)
273 : : {
274 : 481788 : if (!VAR_P (decl)
275 : 307726 : || !DECL_ARTIFICIAL (decl)
276 : 151231 : || !DECL_IGNORED_P (decl)
277 : 146069 : || !DECL_HAS_VALUE_EXPR_P (decl)
278 : 493905 : || !lang_hooks.decls.omp_disregard_value_expr (decl, false))
279 : 479549 : return NULL_TREE;
280 : :
281 : 2239 : tree v = DECL_VALUE_EXPR (decl);
282 : 2239 : if (TREE_CODE (v) != COMPONENT_REF)
283 : : return NULL_TREE;
284 : :
285 : 15969 : while (1)
286 : 9089 : switch (TREE_CODE (v))
287 : : {
288 : 6880 : case COMPONENT_REF:
289 : 6880 : case MEM_REF:
290 : 6880 : case INDIRECT_REF:
291 : 6880 : CASE_CONVERT:
292 : 6880 : case POINTER_PLUS_EXPR:
293 : 6880 : v = TREE_OPERAND (v, 0);
294 : 6880 : continue;
295 : 2190 : case PARM_DECL:
296 : 2190 : if (DECL_CONTEXT (v) == current_function_decl
297 : 2190 : && DECL_ARTIFICIAL (v)
298 : 4380 : && 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 : 1462 : unshare_and_remap_1 (tree *tp, int *walk_subtrees, void *data)
310 : : {
311 : 1462 : tree *pair = (tree *) data;
312 : 1462 : if (*tp == pair[0])
313 : : {
314 : 218 : *tp = unshare_expr (pair[1]);
315 : 218 : *walk_subtrees = 0;
316 : : }
317 : 1244 : else if (IS_TYPE_OR_DECL_P (*tp))
318 : 229 : *walk_subtrees = 0;
319 : 1462 : return NULL_TREE;
320 : : }
321 : :
322 : : /* Return unshare_expr (X) with all occurrences of FROM
323 : : replaced with TO. */
324 : :
325 : : static tree
326 : 141 : unshare_and_remap (tree x, tree from, tree to)
327 : : {
328 : 141 : tree pair[2] = { from, to };
329 : 141 : x = unshare_expr (x);
330 : 141 : walk_tree (&x, unshare_and_remap_1, pair, NULL);
331 : 141 : return x;
332 : : }
333 : :
334 : : /* Convenience function for calling scan_omp_1_op on tree operands. */
335 : :
336 : : static inline tree
337 : 338585 : scan_omp_op (tree *tp, omp_context *ctx)
338 : : {
339 : 338585 : struct walk_stmt_info wi;
340 : :
341 : 338585 : memset (&wi, 0, sizeof (wi));
342 : 338585 : wi.info = ctx;
343 : 338585 : wi.want_locations = true;
344 : :
345 : 338585 : 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 : 774679 : is_parallel_ctx (omp_context *ctx)
356 : : {
357 : 61195 : 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 : 236192 : is_task_ctx (omp_context *ctx)
365 : : {
366 : 38963 : 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 : 8173 : is_taskloop_ctx (omp_context *ctx)
374 : : {
375 : 8173 : return gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
376 : 8173 : && 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 : 519610 : is_host_teams_ctx (omp_context *ctx)
384 : : {
385 : 519610 : return gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
386 : 519610 : && 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 : 662026 : is_taskreg_ctx (omp_context *ctx)
395 : : {
396 : 662026 : 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 : 877660 : is_variable_sized (const_tree expr)
403 : : {
404 : 877660 : 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 : 699191 : lookup_decl (tree var, omp_context *ctx)
413 : : {
414 : 1398283 : tree *n = ctx->cb.decl_map->get (var);
415 : 699191 : return *n;
416 : : }
417 : :
418 : : static inline tree
419 : 718849 : maybe_lookup_decl (const_tree var, omp_context *ctx)
420 : : {
421 : 718849 : tree *n = ctx->cb.decl_map->get (const_cast<tree> (var));
422 : 718849 : return n ? *n : NULL_TREE;
423 : : }
424 : :
425 : : static inline tree
426 : 125166 : lookup_field (tree var, omp_context *ctx)
427 : : {
428 : 125166 : splay_tree_node n;
429 : 250332 : n = splay_tree_lookup (ctx->field_map, (splay_tree_key) var);
430 : 125166 : return (tree) n->value;
431 : : }
432 : :
433 : : static inline tree
434 : 163637 : lookup_sfield (splay_tree_key key, omp_context *ctx)
435 : : {
436 : 163637 : splay_tree_node n;
437 : 163637 : n = splay_tree_lookup (ctx->sfield_map
438 : : ? ctx->sfield_map : ctx->field_map, key);
439 : 163637 : return (tree) n->value;
440 : : }
441 : :
442 : : static inline tree
443 : 967 : lookup_sfield (tree var, omp_context *ctx)
444 : : {
445 : 967 : return lookup_sfield ((splay_tree_key) var, ctx);
446 : : }
447 : :
448 : : static inline tree
449 : 215830 : maybe_lookup_field (splay_tree_key key, omp_context *ctx)
450 : : {
451 : 215830 : splay_tree_node n;
452 : 215830 : n = splay_tree_lookup (ctx->field_map, key);
453 : 215830 : return n ? (tree) n->value : NULL_TREE;
454 : : }
455 : :
456 : : static inline tree
457 : 215830 : maybe_lookup_field (tree var, omp_context *ctx)
458 : : {
459 : 2859 : 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 : 232705 : use_pointer_for_field (tree decl, omp_context *shared_ctx)
467 : : {
468 : 454036 : if (AGGREGATE_TYPE_P (TREE_TYPE (decl))
469 : 216359 : || TYPE_ATOMIC (TREE_TYPE (decl))
470 : 449064 : || 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 : 216329 : if (shared_ctx)
476 : : {
477 : 29502 : 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 : 29502 : 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 : 29502 : 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 : 28600 : 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 : 1265 : if (!TREE_ADDRESSABLE (decl))
504 : : {
505 : 545 : if (!global_nonaddressable_vars)
506 : 80 : global_nonaddressable_vars = BITMAP_ALLOC (NULL);
507 : 545 : bitmap_set_bit (global_nonaddressable_vars, DECL_UID (decl));
508 : : }
509 : 720 : else if (!global_nonaddressable_vars
510 : 950 : || !bitmap_bit_p (global_nonaddressable_vars,
511 : 230 : DECL_UID (decl)))
512 : 698 : return true;
513 : : }
514 : 27335 : 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 : 18431 : if (TREE_READONLY (decl)
520 : 18431 : || ((TREE_CODE (decl) == RESULT_DECL
521 : 11749 : || TREE_CODE (decl) == PARM_DECL)
522 : 664 : && 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 : 11517 : if (shared_ctx->is_nested)
531 : : {
532 : 2482 : omp_context *up;
533 : :
534 : 5987 : for (up = shared_ctx->outer; up; up = up->outer)
535 : 5714 : if ((is_taskreg_ctx (up)
536 : 3658 : || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
537 : 426 : && is_gimple_omp_offloaded (up->stmt)))
538 : 6140 : && maybe_lookup_decl (decl, up))
539 : : break;
540 : :
541 : 2482 : if (up)
542 : : {
543 : 2209 : tree c;
544 : :
545 : 2209 : if (gimple_code (up->stmt) == GIMPLE_OMP_TARGET)
546 : : {
547 : 324 : for (c = gimple_omp_target_clauses (up->stmt);
548 : 2106 : c; c = OMP_CLAUSE_CHAIN (c))
549 : 1992 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
550 : 1992 : && OMP_CLAUSE_DECL (c) == decl)
551 : : break;
552 : : }
553 : : else
554 : 1885 : for (c = gimple_omp_taskreg_clauses (up->stmt);
555 : 6906 : c; c = OMP_CLAUSE_CHAIN (c))
556 : 6451 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
557 : 6451 : && OMP_CLAUSE_DECL (c) == decl)
558 : : break;
559 : :
560 : 2209 : if (c)
561 : 1640 : 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 : 9877 : if (is_task_ctx (shared_ctx))
569 : : {
570 : 2190 : tree outer;
571 : 550 : maybe_mark_addressable_and_ret:
572 : 2190 : outer = maybe_lookup_decl_in_outer_ctx (decl, shared_ctx);
573 : 2190 : 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 : 981 : if (!make_addressable_vars)
579 : 488 : make_addressable_vars = BITMAP_ALLOC (NULL);
580 : 981 : bitmap_set_bit (make_addressable_vars, DECL_UID (outer));
581 : 981 : TREE_ADDRESSABLE (outer) = 1;
582 : : }
583 : 2190 : return true;
584 : : }
585 : : }
586 : :
587 : : return false;
588 : : }
589 : :
590 : : /* Construct a new automatic decl similar to VAR. */
591 : :
592 : : static tree
593 : 227261 : omp_copy_decl_2 (tree var, tree name, tree type, omp_context *ctx)
594 : : {
595 : 227261 : tree copy = copy_var_decl (var, name, type);
596 : :
597 : 227261 : DECL_CONTEXT (copy) = current_function_decl;
598 : :
599 : 227261 : if (ctx)
600 : : {
601 : 226980 : DECL_CHAIN (copy) = ctx->block_vars;
602 : 226980 : ctx->block_vars = copy;
603 : : }
604 : : else
605 : 281 : 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 : 227261 : if (TREE_ADDRESSABLE (var)
612 : 227261 : && ((make_addressable_vars
613 : 3568 : && bitmap_bit_p (make_addressable_vars, DECL_UID (var)))
614 : 37760 : || (global_nonaddressable_vars
615 : 513 : && bitmap_bit_p (global_nonaddressable_vars, DECL_UID (var)))))
616 : 2135 : TREE_ADDRESSABLE (copy) = 0;
617 : :
618 : 227261 : return copy;
619 : : }
620 : :
621 : : static tree
622 : 227261 : omp_copy_decl_1 (tree var, omp_context *ctx)
623 : : {
624 : 227261 : 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 : 120689 : build_receiver_ref (tree var, bool by_ref, omp_context *ctx)
631 : : {
632 : 120689 : 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 : 120689 : x = maybe_lookup_field (field, ctx);
637 : 120689 : if (x != NULL)
638 : 10842 : field = x;
639 : :
640 : 120689 : x = build_simple_mem_ref (ctx->receiver_decl);
641 : 120689 : TREE_THIS_NOTRAP (x) = 1;
642 : 120689 : x = omp_build_component_ref (x, field);
643 : 120689 : if (by_ref)
644 : : {
645 : 34568 : x = build_simple_mem_ref (x);
646 : 34568 : TREE_THIS_NOTRAP (x) = 1;
647 : : }
648 : :
649 : 120689 : 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 : 114407 : build_outer_var_ref (tree var, omp_context *ctx,
658 : : enum omp_clause_code code = OMP_CLAUSE_ERROR)
659 : : {
660 : 114407 : tree x;
661 : 114407 : omp_context *outer = ctx->outer;
662 : 114796 : for (; outer; outer = outer->outer)
663 : : {
664 : 82564 : if (gimple_code (outer->stmt) == GIMPLE_OMP_TASKGROUP)
665 : 373 : continue;
666 : 82207 : if (gimple_code (outer->stmt) == GIMPLE_OMP_SCOPE
667 : 82191 : && !maybe_lookup_decl (var, outer))
668 : 16 : continue;
669 : : break;
670 : : }
671 : :
672 : 114407 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx)))
673 : : x = var;
674 : 106476 : else if (is_variable_sized (var))
675 : : {
676 : 47 : x = TREE_OPERAND (DECL_VALUE_EXPR (var), 0);
677 : 47 : x = build_outer_var_ref (x, ctx, code);
678 : 47 : x = build_simple_mem_ref (x);
679 : : }
680 : 106429 : else if (is_taskreg_ctx (ctx))
681 : : {
682 : 63733 : bool by_ref = use_pointer_for_field (var, NULL);
683 : 63733 : x = build_receiver_ref (var, by_ref, ctx);
684 : : }
685 : 42696 : else if ((gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
686 : 41376 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
687 : 14122 : || ctx->loop_p
688 : 13357 : || code == OMP_CLAUSE_ALLOCATE
689 : 55801 : || (code == OMP_CLAUSE_PRIVATE
690 : 39 : && (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 : 29610 : x = NULL_TREE;
699 : 29610 : if (outer && is_taskreg_ctx (outer))
700 : 1002 : x = lookup_decl (var, outer);
701 : 28608 : else if (outer)
702 : 22332 : x = maybe_lookup_decl_in_outer_ctx (var, ctx);
703 : 23334 : if (x == NULL_TREE)
704 : : x = var;
705 : : }
706 : 13086 : else if (code == OMP_CLAUSE_LASTPRIVATE && is_taskloop_ctx (ctx))
707 : : {
708 : 686 : gcc_assert (outer);
709 : 686 : splay_tree_node n
710 : 1372 : = splay_tree_lookup (outer->field_map,
711 : 686 : (splay_tree_key) &DECL_UID (var));
712 : 686 : if (n == NULL)
713 : : {
714 : 610 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, outer)))
715 : : x = var;
716 : : else
717 : 384 : x = lookup_decl (var, outer);
718 : : }
719 : : else
720 : : {
721 : 76 : 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 : 76 : x = maybe_lookup_field (field, outer);
725 : 76 : if (x != NULL)
726 : 0 : field = x;
727 : :
728 : 76 : x = build_simple_mem_ref (outer->receiver_decl);
729 : 76 : x = omp_build_component_ref (x, field);
730 : 76 : if (use_pointer_for_field (var, outer))
731 : 56 : x = build_simple_mem_ref (x);
732 : : }
733 : : }
734 : 12400 : else if (outer)
735 : 12208 : x = lookup_decl (var, outer);
736 : 192 : 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 : 94 : else if (omp_member_access_dummy_var (var))
741 : : x = var;
742 : : else
743 : 0 : gcc_unreachable ();
744 : :
745 : 114407 : if (x == var)
746 : : {
747 : 16199 : tree t = omp_member_access_dummy_var (var);
748 : 16199 : if (t)
749 : : {
750 : 220 : x = DECL_VALUE_EXPR (var);
751 : 220 : tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
752 : 220 : if (o != t)
753 : 54 : x = unshare_and_remap (x, t, o);
754 : : else
755 : 166 : x = unshare_expr (x);
756 : : }
757 : : }
758 : :
759 : 114407 : if (omp_privatize_by_reference (var))
760 : 3355 : x = build_simple_mem_ref (x);
761 : :
762 : 114407 : return x;
763 : : }
764 : :
765 : : /* Build tree nodes to access the field for VAR on the sender side. */
766 : :
767 : : static tree
768 : 162594 : build_sender_ref (splay_tree_key key, omp_context *ctx)
769 : : {
770 : 162594 : tree field = lookup_sfield (key, ctx);
771 : 162594 : tree tmp = ctx->sender_decl;
772 : 162594 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
773 : 0 : tmp = build_fold_indirect_ref (tmp);
774 : 162594 : return omp_build_component_ref (tmp, field);
775 : : }
776 : :
777 : : static tree
778 : 158450 : build_sender_ref (tree var, omp_context *ctx)
779 : : {
780 : 0 : 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 : 128729 : install_var_field (tree var, bool by_ref, int mask, omp_context *ctx)
788 : : {
789 : 128729 : tree field, type, sfield = NULL_TREE;
790 : 128729 : splay_tree_key key = (splay_tree_key) var;
791 : :
792 : 128729 : if ((mask & 16) != 0)
793 : : {
794 : 619 : key = (splay_tree_key) &DECL_NAME (var);
795 : 619 : gcc_checking_assert (key != (splay_tree_key) var);
796 : : }
797 : 128729 : if ((mask & 8) != 0)
798 : : {
799 : 1611 : key = (splay_tree_key) &DECL_UID (var);
800 : 1611 : gcc_checking_assert (key != (splay_tree_key) var);
801 : : }
802 : 128729 : gcc_assert ((mask & 1) == 0
803 : : || !splay_tree_lookup (ctx->field_map, key));
804 : 128729 : gcc_assert ((mask & 2) == 0 || !ctx->sfield_map
805 : : || !splay_tree_lookup (ctx->sfield_map, key));
806 : 128729 : gcc_assert ((mask & 3) == 3
807 : : || !is_gimple_omp_oacc (ctx->stmt));
808 : :
809 : 128729 : type = TREE_TYPE (var);
810 : 128729 : if ((mask & 16) != 0)
811 : 619 : 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 : 128729 : if (POINTER_TYPE_P (type)
817 : 128729 : && TYPE_RESTRICT (type))
818 : 6880 : type = build_qualified_type (type, TYPE_QUALS (type) & ~TYPE_QUAL_RESTRICT);
819 : :
820 : 128729 : if (mask & 4)
821 : : {
822 : 425 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
823 : 425 : type = build_pointer_type (build_pointer_type (type));
824 : : }
825 : 128304 : else if (by_ref)
826 : 61519 : type = build_pointer_type (type);
827 : 66785 : else if ((mask & (32 | 3)) == 1
828 : 66785 : && omp_privatize_by_reference (var))
829 : 205 : type = TREE_TYPE (type);
830 : :
831 : 128729 : field = build_decl (DECL_SOURCE_LOCATION (var),
832 : 128729 : 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 : 128729 : DECL_ABSTRACT_ORIGIN (field) = var;
838 : 128729 : if ((mask & 16) == 0 && type == TREE_TYPE (var))
839 : : {
840 : 62300 : SET_DECL_ALIGN (field, DECL_ALIGN (var));
841 : 62300 : DECL_USER_ALIGN (field) = DECL_USER_ALIGN (var);
842 : 62300 : TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (var);
843 : : }
844 : : else
845 : 66429 : SET_DECL_ALIGN (field, TYPE_ALIGN (type));
846 : :
847 : 128729 : if ((mask & 3) == 3)
848 : : {
849 : 127581 : insert_field_into_struct (ctx->record_type, field);
850 : 127581 : if (ctx->srecord_type)
851 : : {
852 : 944 : sfield = build_decl (DECL_SOURCE_LOCATION (var),
853 : 944 : FIELD_DECL, DECL_NAME (var), type);
854 : 944 : DECL_ABSTRACT_ORIGIN (sfield) = var;
855 : 944 : SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
856 : 944 : DECL_USER_ALIGN (sfield) = DECL_USER_ALIGN (field);
857 : 944 : TREE_THIS_VOLATILE (sfield) = TREE_THIS_VOLATILE (field);
858 : 944 : insert_field_into_struct (ctx->srecord_type, sfield);
859 : : }
860 : : }
861 : : else
862 : : {
863 : 1148 : if (ctx->srecord_type == NULL_TREE)
864 : : {
865 : 564 : tree t;
866 : :
867 : 564 : ctx->srecord_type = lang_hooks.types.make_type (RECORD_TYPE);
868 : 564 : ctx->sfield_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
869 : 2035 : for (t = TYPE_FIELDS (ctx->record_type); t ; t = TREE_CHAIN (t))
870 : : {
871 : 1471 : sfield = build_decl (DECL_SOURCE_LOCATION (t),
872 : 1471 : FIELD_DECL, DECL_NAME (t), TREE_TYPE (t));
873 : 1471 : DECL_ABSTRACT_ORIGIN (sfield) = DECL_ABSTRACT_ORIGIN (t);
874 : 1471 : insert_field_into_struct (ctx->srecord_type, sfield);
875 : 2942 : splay_tree_insert (ctx->sfield_map,
876 : 1471 : (splay_tree_key) DECL_ABSTRACT_ORIGIN (t),
877 : : (splay_tree_value) sfield);
878 : : }
879 : : }
880 : 1148 : sfield = field;
881 : 1148 : insert_field_into_struct ((mask & 1) ? ctx->record_type
882 : : : ctx->srecord_type, field);
883 : : }
884 : :
885 : 128729 : if (mask & 1)
886 : 128294 : splay_tree_insert (ctx->field_map, key, (splay_tree_value) field);
887 : 128729 : if ((mask & 2) && ctx->sfield_map)
888 : 1379 : splay_tree_insert (ctx->sfield_map, key, (splay_tree_value) sfield);
889 : 128729 : }
890 : :
891 : : static tree
892 : 226830 : install_var_local (tree var, omp_context *ctx)
893 : : {
894 : 226830 : tree new_var = omp_copy_decl_1 (var, ctx);
895 : 226830 : insert_decl_map (&ctx->cb, var, new_var);
896 : 226830 : 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 : 202023 : fixup_remapped_decl (tree decl, omp_context *ctx, bool private_debug)
904 : : {
905 : 202023 : tree new_decl, size;
906 : :
907 : 202023 : new_decl = lookup_decl (decl, ctx);
908 : :
909 : 202023 : TREE_TYPE (new_decl) = remap_type (TREE_TYPE (decl), &ctx->cb);
910 : :
911 : 402929 : if ((!TREE_CONSTANT (DECL_SIZE (new_decl)) || private_debug)
912 : 202212 : && DECL_HAS_VALUE_EXPR_P (decl))
913 : : {
914 : 1306 : tree ve = DECL_VALUE_EXPR (decl);
915 : 1306 : walk_tree (&ve, copy_tree_body_r, &ctx->cb, NULL);
916 : 1306 : SET_DECL_VALUE_EXPR (new_decl, ve);
917 : 1306 : DECL_HAS_VALUE_EXPR_P (new_decl) = 1;
918 : : }
919 : :
920 : 202023 : if (!TREE_CONSTANT (DECL_SIZE (new_decl)))
921 : : {
922 : 1117 : size = remap_decl (DECL_SIZE (decl), &ctx->cb);
923 : 1117 : if (size == error_mark_node)
924 : 304 : size = TYPE_SIZE (TREE_TYPE (new_decl));
925 : 1117 : DECL_SIZE (new_decl) = size;
926 : :
927 : 1117 : size = remap_decl (DECL_SIZE_UNIT (decl), &ctx->cb);
928 : 1117 : if (size == error_mark_node)
929 : 291 : size = TYPE_SIZE_UNIT (TREE_TYPE (new_decl));
930 : 1117 : DECL_SIZE_UNIT (new_decl) = size;
931 : : }
932 : 202023 : }
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 : 453585 : omp_copy_decl (tree var, copy_body_data *cb)
941 : : {
942 : 453585 : omp_context *ctx = (omp_context *) cb;
943 : 453585 : tree new_var;
944 : :
945 : 453585 : if (TREE_CODE (var) == LABEL_DECL)
946 : : {
947 : 179507 : if (FORCED_LABEL (var) || DECL_NONLOCAL (var))
948 : : return var;
949 : 179501 : new_var = create_artificial_label (DECL_SOURCE_LOCATION (var));
950 : 179501 : DECL_CONTEXT (new_var) = current_function_decl;
951 : 179501 : insert_decl_map (&ctx->cb, var, new_var);
952 : 179501 : return new_var;
953 : : }
954 : :
955 : 355886 : while (!is_taskreg_ctx (ctx))
956 : : {
957 : 336745 : ctx = ctx->outer;
958 : 336745 : if (ctx == NULL)
959 : : return var;
960 : 279112 : new_var = maybe_lookup_decl (var, ctx);
961 : 279112 : if (new_var)
962 : : return new_var;
963 : : }
964 : :
965 : 19141 : if (is_global_var (var) || decl_function_context (var) != ctx->cb.src_fn)
966 : 13373 : return var;
967 : :
968 : 5768 : return error_mark_node;
969 : : }
970 : :
971 : : /* Create a new context, with OUTER_CTX being the surrounding context. */
972 : :
973 : : static omp_context *
974 : 135518 : new_omp_context (gimple *stmt, omp_context *outer_ctx)
975 : : {
976 : 135518 : omp_context *ctx = XCNEW (omp_context);
977 : :
978 : 135518 : splay_tree_insert (all_contexts, (splay_tree_key) stmt,
979 : : (splay_tree_value) ctx);
980 : 135518 : ctx->stmt = stmt;
981 : :
982 : 135518 : if (outer_ctx)
983 : : {
984 : 74375 : ctx->outer = outer_ctx;
985 : 74375 : ctx->cb = outer_ctx->cb;
986 : 74375 : ctx->cb.block = NULL;
987 : 74375 : ctx->depth = outer_ctx->depth + 1;
988 : : }
989 : : else
990 : : {
991 : 61143 : ctx->cb.src_fn = current_function_decl;
992 : 61143 : ctx->cb.dst_fn = current_function_decl;
993 : 61143 : ctx->cb.src_node = cgraph_node::get (current_function_decl);
994 : 61143 : gcc_checking_assert (ctx->cb.src_node);
995 : 61143 : ctx->cb.dst_node = ctx->cb.src_node;
996 : 61143 : ctx->cb.src_cfun = cfun;
997 : 61143 : ctx->cb.copy_decl = omp_copy_decl;
998 : 61143 : ctx->cb.eh_lp_nr = 0;
999 : 61143 : ctx->cb.transform_call_graph_edges = CB_CGE_MOVE;
1000 : 61143 : ctx->cb.adjust_array_error_bounds = true;
1001 : 61143 : ctx->cb.dont_remap_vla_if_no_change = true;
1002 : 61143 : ctx->depth = 1;
1003 : : }
1004 : :
1005 : 135518 : ctx->cb.decl_map = new hash_map<tree, tree>;
1006 : :
1007 : 135518 : return ctx;
1008 : : }
1009 : :
1010 : : static gimple_seq maybe_catch_exception (gimple_seq);
1011 : :
1012 : : /* Finalize task copyfn. */
1013 : :
1014 : : static void
1015 : 518 : finalize_task_copyfn (gomp_task *task_stmt)
1016 : : {
1017 : 518 : struct function *child_cfun;
1018 : 518 : tree child_fn;
1019 : 518 : gimple_seq seq = NULL, new_seq;
1020 : 518 : gbind *bind;
1021 : :
1022 : 518 : child_fn = gimple_omp_task_copy_fn (task_stmt);
1023 : 518 : if (child_fn == NULL_TREE)
1024 : 0 : return;
1025 : :
1026 : 518 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
1027 : 518 : DECL_STRUCT_FUNCTION (child_fn)->curr_properties = cfun->curr_properties;
1028 : :
1029 : 518 : push_cfun (child_cfun);
1030 : 518 : bind = gimplify_body (child_fn, false);
1031 : 518 : gimple_seq_add_stmt (&seq, bind);
1032 : 518 : new_seq = maybe_catch_exception (seq);
1033 : 518 : if (new_seq != seq)
1034 : : {
1035 : 347 : bind = gimple_build_bind (NULL, new_seq, NULL);
1036 : 347 : seq = NULL;
1037 : 347 : gimple_seq_add_stmt (&seq, bind);
1038 : : }
1039 : 518 : gimple_set_body (child_fn, seq);
1040 : 518 : pop_cfun ();
1041 : :
1042 : : /* Inform the callgraph about the new function. */
1043 : 518 : cgraph_node *node = cgraph_node::get_create (child_fn);
1044 : 518 : node->parallelized_function = 1;
1045 : 518 : 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 : 135512 : delete_omp_context (splay_tree_value value)
1053 : : {
1054 : 135512 : omp_context *ctx = (omp_context *) value;
1055 : :
1056 : 271024 : delete ctx->cb.decl_map;
1057 : :
1058 : 135512 : if (ctx->field_map)
1059 : 67200 : splay_tree_delete (ctx->field_map);
1060 : 135512 : if (ctx->sfield_map)
1061 : 564 : 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 : 135512 : if (ctx->record_type)
1066 : : {
1067 : 51612 : tree t;
1068 : 214669 : for (t = TYPE_FIELDS (ctx->record_type); t ; t = DECL_CHAIN (t))
1069 : 163057 : DECL_ABSTRACT_ORIGIN (t) = NULL;
1070 : : }
1071 : 135512 : if (ctx->srecord_type)
1072 : : {
1073 : 564 : tree t;
1074 : 3414 : for (t = TYPE_FIELDS (ctx->srecord_type); t ; t = DECL_CHAIN (t))
1075 : 2850 : DECL_ABSTRACT_ORIGIN (t) = NULL;
1076 : : }
1077 : :
1078 : 135512 : if (ctx->task_reduction_map)
1079 : : {
1080 : 1160 : ctx->task_reductions.release ();
1081 : 2320 : delete ctx->task_reduction_map;
1082 : : }
1083 : :
1084 : 135785 : delete ctx->lastprivate_conditional_map;
1085 : 136926 : delete ctx->allocate_map;
1086 : :
1087 : 135512 : XDELETE (ctx);
1088 : 135512 : }
1089 : :
1090 : : /* Fix up RECEIVER_DECL with a type that has been remapped to the child
1091 : : context. */
1092 : :
1093 : : static void
1094 : 35547 : fixup_child_record_type (omp_context *ctx)
1095 : : {
1096 : 35547 : tree f, type = ctx->record_type;
1097 : :
1098 : 35547 : 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 : 157233 : for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
1105 : 122893 : if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
1106 : : break;
1107 : 35547 : if (f)
1108 : : {
1109 : 1207 : tree name, new_fields = NULL;
1110 : :
1111 : 1207 : type = lang_hooks.types.make_type (RECORD_TYPE);
1112 : 1207 : name = DECL_NAME (TYPE_NAME (ctx->record_type));
1113 : 1207 : name = build_decl (DECL_SOURCE_LOCATION (ctx->receiver_decl),
1114 : : TYPE_DECL, name, type);
1115 : 1207 : TYPE_NAME (type) = name;
1116 : :
1117 : 12966 : for (f = TYPE_FIELDS (ctx->record_type); f ; f = DECL_CHAIN (f))
1118 : : {
1119 : 11759 : tree new_f = copy_node (f);
1120 : 11759 : DECL_CONTEXT (new_f) = type;
1121 : 11759 : TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &ctx->cb);
1122 : 11759 : DECL_CHAIN (new_f) = new_fields;
1123 : 11759 : walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &ctx->cb, NULL);
1124 : 11759 : walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r,
1125 : : &ctx->cb, NULL);
1126 : 11759 : walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
1127 : : &ctx->cb, NULL);
1128 : 11759 : new_fields = new_f;
1129 : :
1130 : : /* Arrange to be able to look up the receiver field
1131 : : given the sender field. */
1132 : 11759 : splay_tree_insert (ctx->field_map, (splay_tree_key) f,
1133 : : (splay_tree_value) new_f);
1134 : : }
1135 : 1207 : TYPE_FIELDS (type) = nreverse (new_fields);
1136 : 1207 : 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 : 35547 : if (is_gimple_omp_offloaded (ctx->stmt))
1142 : 16329 : type = build_qualified_type (type, TYPE_QUAL_CONST);
1143 : :
1144 : 35547 : TREE_TYPE (ctx->receiver_decl)
1145 : 106641 : = build_qualified_type (flexible_array_type_p (type)
1146 : 0 : ? build_pointer_type (type)
1147 : 35547 : : 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 : 127802 : scan_sharing_clauses (tree clauses, omp_context *ctx)
1155 : : {
1156 : 127802 : tree c, decl;
1157 : 127802 : bool scan_array_reductions = false;
1158 : 127802 : bool flex_array_ptr = false;
1159 : :
1160 : 538582 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1161 : 410780 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
1162 : 410780 : && (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
1163 : : /* omp_default_mem_alloc is 1 */
1164 : 1170 : || !integer_onep (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
1165 : 568 : || 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 : 1854 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
1172 : 1212 : && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
1173 : 3054 : && omp_maybe_offloaded_ctx (ctx))
1174 : 10 : error_at (OMP_CLAUSE_LOCATION (c), "%<allocate%> clause must"
1175 : : " specify an allocator here");
1176 : 1854 : if (ctx->allocate_map == NULL)
1177 : 1414 : ctx->allocate_map = new hash_map<tree, tree>;
1178 : 1854 : tree val = integer_zero_node;
1179 : 1854 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
1180 : 642 : val = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
1181 : 1854 : if (OMP_CLAUSE_ALLOCATE_ALIGN (c))
1182 : 195 : val = build_tree_list (val, OMP_CLAUSE_ALLOCATE_ALIGN (c));
1183 : 1854 : ctx->allocate_map->put (OMP_CLAUSE_DECL (c), val);
1184 : : }
1185 : :
1186 : 538582 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1187 : : {
1188 : 410780 : bool by_ref;
1189 : :
1190 : 410780 : switch (OMP_CLAUSE_CODE (c))
1191 : : {
1192 : 73065 : case OMP_CLAUSE_PRIVATE:
1193 : 73065 : decl = OMP_CLAUSE_DECL (c);
1194 : 73065 : if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
1195 : 163 : goto do_private;
1196 : 72902 : else if (!is_variable_sized (decl))
1197 : 72553 : install_var_local (decl, ctx);
1198 : : break;
1199 : :
1200 : 46660 : case OMP_CLAUSE_SHARED:
1201 : 46660 : decl = OMP_CLAUSE_DECL (c);
1202 : 46660 : if (ctx->allocate_map && ctx->allocate_map->get (decl))
1203 : 37 : ctx->allocate_map->remove (decl);
1204 : : /* Ignore shared directives in teams construct inside of
1205 : : target construct. */
1206 : 46660 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
1207 : 46660 : && !is_host_teams_ctx (ctx))
1208 : : {
1209 : : /* Global variables don't need to be copied,
1210 : : the receiver side will use them directly. */
1211 : 11803 : tree odecl = maybe_lookup_decl_in_outer_ctx (decl, ctx);
1212 : 11803 : if (is_global_var (odecl))
1213 : : break;
1214 : 8074 : insert_decl_map (&ctx->cb, decl, odecl);
1215 : 8074 : break;
1216 : : }
1217 : 34857 : gcc_assert (is_taskreg_ctx (ctx));
1218 : 34857 : 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 : 34857 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1223 : : break;
1224 : 28824 : if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
1225 : : {
1226 : 83 : use_pointer_for_field (decl, ctx);
1227 : 83 : break;
1228 : : }
1229 : 28741 : by_ref = use_pointer_for_field (decl, NULL);
1230 : 55004 : if ((! TREE_READONLY (decl) && !OMP_CLAUSE_SHARED_READONLY (c))
1231 : 19238 : || TREE_ADDRESSABLE (decl)
1232 : 18881 : || by_ref
1233 : 47622 : || omp_privatize_by_reference (decl))
1234 : : {
1235 : 11984 : by_ref = use_pointer_for_field (decl, ctx);
1236 : 11984 : install_var_field (decl, by_ref, 3, ctx);
1237 : 11984 : install_var_local (decl, ctx);
1238 : 11984 : break;
1239 : : }
1240 : : /* We don't need to copy const scalar vars back. */
1241 : 16757 : OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_FIRSTPRIVATE);
1242 : 16757 : goto do_private;
1243 : :
1244 : 14544 : case OMP_CLAUSE_REDUCTION:
1245 : : /* Collect 'reduction' clauses on OpenACC compute construct. */
1246 : 14544 : if (is_gimple_omp_oacc (ctx->stmt)
1247 : 14544 : && is_gimple_omp_offloaded (ctx->stmt))
1248 : : {
1249 : : /* No 'reduction' clauses on OpenACC 'kernels'. */
1250 : 788 : gcc_checking_assert (!is_oacc_kernels (ctx));
1251 : : /* Likewise, on OpenACC 'kernels' decomposed parts. */
1252 : 788 : gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
1253 : :
1254 : 788 : ctx->local_reduction_clauses
1255 : 788 : = tree_cons (NULL, c, ctx->local_reduction_clauses);
1256 : : }
1257 : : /* FALLTHRU */
1258 : :
1259 : 16614 : case OMP_CLAUSE_IN_REDUCTION:
1260 : 16614 : decl = OMP_CLAUSE_DECL (c);
1261 : 16614 : if (ctx->allocate_map
1262 : 16614 : && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1263 : 473 : && (OMP_CLAUSE_REDUCTION_INSCAN (c)
1264 : 473 : || OMP_CLAUSE_REDUCTION_TASK (c)))
1265 : 481 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
1266 : 473 : || is_task_ctx (ctx)))
1267 : : {
1268 : : /* For now. */
1269 : 17 : if (ctx->allocate_map->get (decl))
1270 : 17 : ctx->allocate_map->remove (decl);
1271 : : }
1272 : 16614 : if (TREE_CODE (decl) == MEM_REF)
1273 : : {
1274 : 2441 : tree t = TREE_OPERAND (decl, 0);
1275 : 2441 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
1276 : 436 : t = TREE_OPERAND (t, 0);
1277 : 2441 : if (INDIRECT_REF_P (t)
1278 : 2279 : || TREE_CODE (t) == ADDR_EXPR)
1279 : 1289 : t = TREE_OPERAND (t, 0);
1280 : 2441 : if (is_omp_target (ctx->stmt))
1281 : : {
1282 : 96 : if (is_variable_sized (t))
1283 : : {
1284 : 32 : gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
1285 : 32 : t = DECL_VALUE_EXPR (t);
1286 : 32 : gcc_assert (INDIRECT_REF_P (t));
1287 : 32 : t = TREE_OPERAND (t, 0);
1288 : 32 : gcc_assert (DECL_P (t));
1289 : : }
1290 : 96 : tree at = t;
1291 : 96 : if (ctx->outer)
1292 : 96 : scan_omp_op (&at, ctx->outer);
1293 : 96 : tree nt = omp_copy_decl_1 (at, ctx->outer);
1294 : 192 : splay_tree_insert (ctx->field_map,
1295 : 96 : (splay_tree_key) &DECL_CONTEXT (t),
1296 : : (splay_tree_value) nt);
1297 : 96 : if (at != t)
1298 : 96 : splay_tree_insert (ctx->field_map,
1299 : 48 : (splay_tree_key) &DECL_CONTEXT (at),
1300 : : (splay_tree_value) nt);
1301 : 96 : break;
1302 : : }
1303 : 2345 : install_var_local (t, ctx);
1304 : 2345 : if (is_taskreg_ctx (ctx)
1305 : 1897 : && (!is_global_var (maybe_lookup_decl_in_outer_ctx (t, ctx))
1306 : 387 : || (is_task_ctx (ctx)
1307 : 310 : && (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1308 : 254 : || (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
1309 : 90 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
1310 : : == POINTER_TYPE)))))
1311 : 1586 : && !is_variable_sized (t)
1312 : 3872 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
1313 : 692 : || (!OMP_CLAUSE_REDUCTION_TASK (c)
1314 : 653 : && !is_task_ctx (ctx))))
1315 : : {
1316 : 1410 : by_ref = use_pointer_for_field (t, NULL);
1317 : 1410 : if (is_task_ctx (ctx)
1318 : 835 : && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
1319 : 1687 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == POINTER_TYPE)
1320 : : {
1321 : 72 : install_var_field (t, false, 1, ctx);
1322 : 72 : install_var_field (t, by_ref, 2, ctx);
1323 : : }
1324 : : else
1325 : 1338 : install_var_field (t, by_ref, 3, ctx);
1326 : : }
1327 : : break;
1328 : : }
1329 : 14173 : if (is_omp_target (ctx->stmt))
1330 : : {
1331 : 335 : tree at = decl;
1332 : 335 : if (ctx->outer)
1333 : 54 : scan_omp_op (&at, ctx->outer);
1334 : 335 : tree nt = omp_copy_decl_1 (at, ctx->outer);
1335 : 670 : splay_tree_insert (ctx->field_map,
1336 : 335 : (splay_tree_key) &DECL_CONTEXT (decl),
1337 : : (splay_tree_value) nt);
1338 : 335 : if (at != decl)
1339 : 24 : splay_tree_insert (ctx->field_map,
1340 : 12 : (splay_tree_key) &DECL_CONTEXT (at),
1341 : : (splay_tree_value) nt);
1342 : 335 : break;
1343 : : }
1344 : 13838 : if (is_task_ctx (ctx)
1345 : 26614 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1346 : 12776 : && OMP_CLAUSE_REDUCTION_TASK (c)
1347 : 372 : && is_parallel_ctx (ctx)))
1348 : : {
1349 : : /* Global variables don't need to be copied,
1350 : : the receiver side will use them directly. */
1351 : 1141 : if (!is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1352 : : {
1353 : 461 : by_ref = use_pointer_for_field (decl, ctx);
1354 : 461 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
1355 : 304 : install_var_field (decl, by_ref, 3, ctx);
1356 : : }
1357 : 1141 : install_var_local (decl, ctx);
1358 : 1141 : break;
1359 : : }
1360 : 12697 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
1361 : 12697 : && OMP_CLAUSE_REDUCTION_TASK (c))
1362 : : {
1363 : 293 : install_var_local (decl, ctx);
1364 : 293 : break;
1365 : : }
1366 : 12404 : goto do_private;
1367 : :
1368 : 22727 : case OMP_CLAUSE_LASTPRIVATE:
1369 : : /* Let the corresponding firstprivate clause create
1370 : : the variable. */
1371 : 22727 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1372 : : break;
1373 : : /* FALLTHRU */
1374 : :
1375 : 58750 : case OMP_CLAUSE_FIRSTPRIVATE:
1376 : 58750 : case OMP_CLAUSE_LINEAR:
1377 : 58750 : decl = OMP_CLAUSE_DECL (c);
1378 : 88429 : do_private:
1379 : 88429 : 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 : 45087 : && is_gimple_omp_offloaded (ctx->stmt))
1383 : : {
1384 : 15628 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1385 : 15628 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
1386 : 225 : && lang_hooks.decls.omp_array_data (decl, true)))
1387 : : {
1388 : 15286 : by_ref = !omp_privatize_by_reference (decl);
1389 : 15286 : install_var_field (decl, by_ref, 3, ctx);
1390 : : }
1391 : 342 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1392 : : {
1393 : 212 : if (INDIRECT_REF_P (decl))
1394 : 0 : decl = TREE_OPERAND (decl, 0);
1395 : 212 : install_var_field (decl, true, 3, ctx);
1396 : : }
1397 : 130 : else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1398 : 8 : install_var_field (decl, true, 3, ctx);
1399 : : else
1400 : 122 : install_var_field (decl, false, 3, ctx);
1401 : : }
1402 : 88429 : if (is_variable_sized (decl))
1403 : : {
1404 : 58 : if (is_task_ctx (ctx))
1405 : : {
1406 : 14 : if (ctx->allocate_map
1407 : 14 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
1408 : : {
1409 : : /* For now. */
1410 : 0 : if (ctx->allocate_map->get (decl))
1411 : 0 : ctx->allocate_map->remove (decl);
1412 : : }
1413 : 14 : install_var_field (decl, false, 1, ctx);
1414 : : }
1415 : : break;
1416 : : }
1417 : 88371 : else if (is_taskreg_ctx (ctx))
1418 : : {
1419 : 38273 : bool global
1420 : 38273 : = is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx));
1421 : 38273 : by_ref = use_pointer_for_field (decl, NULL);
1422 : :
1423 : 38273 : if (is_task_ctx (ctx)
1424 : 38273 : && (global || by_ref || omp_privatize_by_reference (decl)))
1425 : : {
1426 : 627 : if (ctx->allocate_map
1427 : 627 : && ctx->allocate_map->get (decl))
1428 : 59 : install_var_field (decl, by_ref, 32 | 1, ctx);
1429 : : else
1430 : 568 : install_var_field (decl, false, 1, ctx);
1431 : 627 : if (!global)
1432 : 363 : install_var_field (decl, by_ref, 2, ctx);
1433 : : }
1434 : 37646 : else if (!global)
1435 : 36379 : install_var_field (decl, by_ref, 3, ctx);
1436 : : }
1437 : 88371 : install_var_local (decl, ctx);
1438 : : /* For descr arrays on target: firstprivatize data + attach ptr. */
1439 : 88371 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
1440 : 44687 : && is_gimple_omp_offloaded (ctx->stmt)
1441 : 15269 : && !is_gimple_omp_oacc (ctx->stmt)
1442 : 99398 : && lang_hooks.decls.omp_array_data (decl, true))
1443 : : {
1444 : 18 : install_var_field (decl, false, 16 | 3, ctx);
1445 : 18 : install_var_field (decl, true, 8 | 3, ctx);
1446 : : }
1447 : : break;
1448 : :
1449 : 2118 : case OMP_CLAUSE_USE_DEVICE_PTR:
1450 : 2118 : case OMP_CLAUSE_USE_DEVICE_ADDR:
1451 : 2118 : decl = OMP_CLAUSE_DECL (c);
1452 : :
1453 : : /* Fortran array descriptors. */
1454 : 2118 : if (lang_hooks.decls.omp_array_data (decl, true))
1455 : 601 : install_var_field (decl, false, 19, ctx);
1456 : 1517 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
1457 : 1312 : && !omp_privatize_by_reference (decl)
1458 : 513 : && !omp_is_allocatable_or_ptr (decl))
1459 : 2454 : || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1460 : 407 : install_var_field (decl, true, 11, ctx);
1461 : : else
1462 : 1110 : install_var_field (decl, false, 11, ctx);
1463 : 2118 : if (DECL_SIZE (decl)
1464 : 2118 : && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1465 : : {
1466 : 6 : tree decl2 = DECL_VALUE_EXPR (decl);
1467 : 6 : gcc_assert (INDIRECT_REF_P (decl2));
1468 : 6 : decl2 = TREE_OPERAND (decl2, 0);
1469 : 6 : gcc_assert (DECL_P (decl2));
1470 : 6 : install_var_local (decl2, ctx);
1471 : : }
1472 : 2118 : install_var_local (decl, ctx);
1473 : 2118 : break;
1474 : :
1475 : 225 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
1476 : 225 : decl = OMP_CLAUSE_DECL (c);
1477 : 225 : while (INDIRECT_REF_P (decl)
1478 : 242 : || TREE_CODE (decl) == ARRAY_REF)
1479 : 17 : decl = TREE_OPERAND (decl, 0);
1480 : 225 : goto do_private;
1481 : :
1482 : 130 : case OMP_CLAUSE_IS_DEVICE_PTR:
1483 : 130 : decl = OMP_CLAUSE_DECL (c);
1484 : 130 : goto do_private;
1485 : :
1486 : 20112 : case OMP_CLAUSE__LOOPTEMP_:
1487 : 20112 : case OMP_CLAUSE__REDUCTEMP_:
1488 : 20112 : gcc_assert (is_taskreg_ctx (ctx));
1489 : 20112 : decl = OMP_CLAUSE_DECL (c);
1490 : 20112 : install_var_field (decl, false, 3, ctx);
1491 : 20112 : install_var_local (decl, ctx);
1492 : 20112 : break;
1493 : :
1494 : 892 : case OMP_CLAUSE_COPYPRIVATE:
1495 : 892 : case OMP_CLAUSE_COPYIN:
1496 : 892 : decl = OMP_CLAUSE_DECL (c);
1497 : 892 : by_ref = use_pointer_for_field (decl, NULL);
1498 : 892 : install_var_field (decl, by_ref, 3, ctx);
1499 : 892 : break;
1500 : :
1501 : 52155 : case OMP_CLAUSE_FINAL:
1502 : 52155 : case OMP_CLAUSE_IF:
1503 : 52155 : case OMP_CLAUSE_SELF:
1504 : 52155 : case OMP_CLAUSE_NUM_THREADS:
1505 : 52155 : case OMP_CLAUSE_NUM_TEAMS:
1506 : 52155 : case OMP_CLAUSE_THREAD_LIMIT:
1507 : 52155 : case OMP_CLAUSE_DEVICE:
1508 : 52155 : case OMP_CLAUSE_SCHEDULE:
1509 : 52155 : case OMP_CLAUSE_DIST_SCHEDULE:
1510 : 52155 : case OMP_CLAUSE_DEPEND:
1511 : 52155 : case OMP_CLAUSE_PRIORITY:
1512 : 52155 : case OMP_CLAUSE_GRAINSIZE:
1513 : 52155 : case OMP_CLAUSE_NUM_TASKS:
1514 : 52155 : case OMP_CLAUSE_NUM_GANGS:
1515 : 52155 : case OMP_CLAUSE_NUM_WORKERS:
1516 : 52155 : case OMP_CLAUSE_VECTOR_LENGTH:
1517 : 52155 : case OMP_CLAUSE_DETACH:
1518 : 52155 : case OMP_CLAUSE_FILTER:
1519 : 52155 : if (ctx->outer)
1520 : 17486 : scan_omp_op (&OMP_CLAUSE_OPERAND (c, 0), ctx->outer);
1521 : : break;
1522 : :
1523 : 82943 : case OMP_CLAUSE_TO:
1524 : 82943 : case OMP_CLAUSE_FROM:
1525 : 82943 : case OMP_CLAUSE_MAP:
1526 : 82943 : if (ctx->outer)
1527 : 15397 : scan_omp_op (&OMP_CLAUSE_SIZE (c), ctx->outer);
1528 : 82943 : decl = OMP_CLAUSE_DECL (c);
1529 : : /* If requested, make 'decl' addressable. */
1530 : 82943 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1531 : 82943 : && OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c))
1532 : : {
1533 : 460 : gcc_checking_assert (DECL_P (decl));
1534 : :
1535 : 460 : bool decl_addressable = TREE_ADDRESSABLE (decl);
1536 : 460 : if (!decl_addressable)
1537 : : {
1538 : 241 : if (!make_addressable_vars)
1539 : 143 : make_addressable_vars = BITMAP_ALLOC (NULL);
1540 : 241 : bitmap_set_bit (make_addressable_vars, DECL_UID (decl));
1541 : 241 : TREE_ADDRESSABLE (decl) = 1;
1542 : : }
1543 : :
1544 : 460 : if (dump_enabled_p ())
1545 : : {
1546 : 448 : location_t loc = OMP_CLAUSE_LOCATION (c);
1547 : 448 : const dump_user_location_t d_u_loc
1548 : 448 : = dump_user_location_t::from_location_t (loc);
1549 : : /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
1550 : : #if __GNUC__ >= 10
1551 : 448 : # pragma GCC diagnostic push
1552 : 448 : # pragma GCC diagnostic ignored "-Wformat"
1553 : : #endif
1554 : 448 : if (!decl_addressable)
1555 : 229 : dump_printf_loc (MSG_NOTE, d_u_loc,
1556 : : "variable %<%T%>"
1557 : : " made addressable\n",
1558 : : decl);
1559 : : else
1560 : 219 : dump_printf_loc (MSG_NOTE, d_u_loc,
1561 : : "variable %<%T%>"
1562 : : " already made addressable\n",
1563 : : decl);
1564 : : #if __GNUC__ >= 10
1565 : 448 : # pragma GCC diagnostic pop
1566 : : #endif
1567 : : }
1568 : :
1569 : : /* Done. */
1570 : 460 : 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 : 82943 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1577 : 75320 : && DECL_P (decl)
1578 : 42245 : && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
1579 : 38544 : && (OMP_CLAUSE_MAP_KIND (c)
1580 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
1581 : 38145 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
1582 : 37931 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)
1583 : 4368 : || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1584 : 39009 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TO
1585 : 38898 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_FROM
1586 : 38879 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_TOFROM
1587 : 38393 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TO
1588 : 38367 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_FROM
1589 : 38345 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_PRESENT_TOFROM
1590 : 38335 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
1591 : 34406 : && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
1592 : 7284 : && varpool_node::get_create (decl)->offloadable
1593 : 88177 : && !lookup_attribute ("omp declare target link",
1594 : 5234 : DECL_ATTRIBUTES (decl)))
1595 : : break;
1596 : 77733 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1597 : 77733 : && 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 : 10296 : if (!is_gimple_omp_offloaded (ctx->stmt)
1602 : 4926 : && !POINTER_TYPE_P (TREE_TYPE (decl))
1603 : 10560 : && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
1604 : : break;
1605 : : }
1606 : 77469 : if (!flex_array_ptr)
1607 : 77469 : flex_array_ptr = lang_hooks.decls.omp_deep_mapping_p (ctx->stmt, c);
1608 : 77469 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1609 : 69846 : && DECL_P (decl)
1610 : 36771 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
1611 : 36557 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
1612 : 77737 : && 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 : 232 : if (TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
1622 : 200 : && is_gimple_omp_offloaded (ctx->stmt)
1623 : 296 : && !seen_error ())
1624 : 56 : 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 : 232 : tree field
1635 : 232 : = build_decl (OMP_CLAUSE_LOCATION (c),
1636 : : FIELD_DECL, NULL_TREE, ptr_type_node);
1637 : 232 : SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
1638 : 232 : 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 : 232 : gcc_assert (!splay_tree_lookup (ctx->field_map,
1643 : : (splay_tree_key) c));
1644 : 232 : splay_tree_insert (ctx->field_map, (splay_tree_key) c,
1645 : : (splay_tree_value) field);
1646 : 232 : break;
1647 : : }
1648 : 77237 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1649 : 77237 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
1650 : 65915 : || (OMP_CLAUSE_MAP_KIND (c)
1651 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
1652 : : {
1653 : 4098 : if (TREE_CODE (decl) == COMPONENT_REF
1654 : 4098 : || (INDIRECT_REF_P (decl)
1655 : 0 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
1656 : 0 : && (((TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
1657 : : == REFERENCE_TYPE)
1658 : 0 : || (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
1659 : : == POINTER_TYPE)))))
1660 : : break;
1661 : 4098 : if (DECL_SIZE (decl)
1662 : 4098 : && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1663 : : {
1664 : 710 : tree decl2 = DECL_VALUE_EXPR (decl);
1665 : 710 : gcc_assert (INDIRECT_REF_P (decl2));
1666 : 710 : decl2 = TREE_OPERAND (decl2, 0);
1667 : 710 : gcc_assert (DECL_P (decl2));
1668 : 710 : install_var_local (decl2, ctx);
1669 : : }
1670 : 4098 : install_var_local (decl, ctx);
1671 : 4098 : break;
1672 : : }
1673 : 73139 : if (DECL_P (decl))
1674 : : {
1675 : 38596 : if (DECL_SIZE (decl)
1676 : 38596 : && !poly_int_tree_p (DECL_SIZE (decl)))
1677 : : {
1678 : 0 : tree decl2 = DECL_VALUE_EXPR (decl);
1679 : 0 : gcc_assert (INDIRECT_REF_P (decl2));
1680 : 0 : decl2 = TREE_OPERAND (decl2, 0);
1681 : 0 : gcc_assert (DECL_P (decl2));
1682 : 0 : install_var_field (decl2, true, 3, ctx);
1683 : 0 : install_var_local (decl2, ctx);
1684 : 0 : install_var_local (decl, ctx);
1685 : : }
1686 : : else
1687 : : {
1688 : 38596 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
1689 : 32441 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
1690 : 5070 : && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
1691 : 43666 : && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1692 : 425 : install_var_field (decl, true, 7, ctx);
1693 : : else
1694 : 38171 : install_var_field (decl, true, 3, ctx);
1695 : 38596 : if (is_gimple_omp_offloaded (ctx->stmt)
1696 : 38596 : && !(is_gimple_omp_oacc (ctx->stmt)
1697 : 13870 : && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
1698 : 22356 : install_var_local (decl, ctx);
1699 : : }
1700 : : }
1701 : : else
1702 : : {
1703 : 34543 : tree base = get_base_address (decl);
1704 : 34543 : tree nc = OMP_CLAUSE_CHAIN (c);
1705 : 34543 : if (DECL_P (base)
1706 : 7961 : && nc != NULL_TREE
1707 : 5748 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
1708 : 4668 : && OMP_CLAUSE_DECL (nc) == base
1709 : 1114 : && OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
1710 : 35211 : && integer_zerop (OMP_CLAUSE_SIZE (nc)))
1711 : : {
1712 : 0 : OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c) = 1;
1713 : 0 : OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (nc) = 1;
1714 : : }
1715 : : else
1716 : : {
1717 : 34543 : if (ctx->outer)
1718 : : {
1719 : 7108 : scan_omp_op (&OMP_CLAUSE_DECL (c), ctx->outer);
1720 : 7108 : decl = OMP_CLAUSE_DECL (c);
1721 : : }
1722 : 34543 : gcc_assert (!splay_tree_lookup (ctx->field_map,
1723 : : (splay_tree_key) decl));
1724 : 34543 : tree field
1725 : 34543 : = build_decl (OMP_CLAUSE_LOCATION (c),
1726 : : FIELD_DECL, NULL_TREE, ptr_type_node);
1727 : 34543 : SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
1728 : 34543 : insert_field_into_struct (ctx->record_type, field);
1729 : 34543 : splay_tree_insert (ctx->field_map, (splay_tree_key) decl,
1730 : : (splay_tree_value) field);
1731 : : }
1732 : : }
1733 : : break;
1734 : :
1735 : 3966 : case OMP_CLAUSE_ORDER:
1736 : 3966 : ctx->order_concurrent = true;
1737 : 3966 : break;
1738 : :
1739 : 1900 : case OMP_CLAUSE_BIND:
1740 : 1900 : ctx->loop_p = true;
1741 : 1900 : 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 : 162 : case OMP_CLAUSE_ALIGNED:
1774 : 162 : decl = OMP_CLAUSE_DECL (c);
1775 : 162 : if (is_global_var (decl)
1776 : 162 : && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1777 : 83 : install_var_local (decl, ctx);
1778 : : break;
1779 : :
1780 : 361 : case OMP_CLAUSE__CONDTEMP_:
1781 : 361 : decl = OMP_CLAUSE_DECL (c);
1782 : 361 : if (is_parallel_ctx (ctx))
1783 : : {
1784 : 118 : install_var_field (decl, false, 3, ctx);
1785 : 118 : install_var_local (decl, ctx);
1786 : : }
1787 : 243 : else if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
1788 : 243 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
1789 : 368 : && !OMP_CLAUSE__CONDTEMP__ITER (c))
1790 : 125 : 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 : 0 : case OMP_CLAUSE__CACHE_:
1799 : 0 : case OMP_CLAUSE_NOHOST:
1800 : 0 : default:
1801 : 0 : gcc_unreachable ();
1802 : : }
1803 : : }
1804 : :
1805 : 538582 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
1806 : : {
1807 : 410780 : switch (OMP_CLAUSE_CODE (c))
1808 : : {
1809 : 22727 : case OMP_CLAUSE_LASTPRIVATE:
1810 : : /* Let the corresponding firstprivate clause create
1811 : : the variable. */
1812 : 22727 : if (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
1813 : 7234 : scan_array_reductions = true;
1814 : 22727 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
1815 : : break;
1816 : : /* FALLTHRU */
1817 : :
1818 : 148927 : case OMP_CLAUSE_FIRSTPRIVATE:
1819 : 148927 : case OMP_CLAUSE_PRIVATE:
1820 : 148927 : case OMP_CLAUSE_LINEAR:
1821 : 148927 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
1822 : 148927 : case OMP_CLAUSE_IS_DEVICE_PTR:
1823 : 148927 : decl = OMP_CLAUSE_DECL (c);
1824 : 148927 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
1825 : : {
1826 : 242 : while (INDIRECT_REF_P (decl)
1827 : 242 : || TREE_CODE (decl) == ARRAY_REF)
1828 : 17 : decl = TREE_OPERAND (decl, 0);
1829 : : }
1830 : :
1831 : 148927 : if (is_variable_sized (decl))
1832 : : {
1833 : 407 : 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 : 51 : && is_gimple_omp_offloaded (ctx->stmt))
1837 : : {
1838 : 10 : tree decl2 = DECL_VALUE_EXPR (decl);
1839 : 10 : gcc_assert (INDIRECT_REF_P (decl2));
1840 : 10 : decl2 = TREE_OPERAND (decl2, 0);
1841 : 10 : gcc_assert (DECL_P (decl2));
1842 : 10 : install_var_local (decl2, ctx);
1843 : 10 : fixup_remapped_decl (decl2, ctx, false);
1844 : : }
1845 : 407 : install_var_local (decl, ctx);
1846 : : }
1847 : 297854 : fixup_remapped_decl (decl, ctx,
1848 : 148927 : OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
1849 : 148927 : && OMP_CLAUSE_PRIVATE_DEBUG (c));
1850 : 148927 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
1851 : 148927 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
1852 : : scan_array_reductions = true;
1853 : : break;
1854 : :
1855 : 16614 : case OMP_CLAUSE_REDUCTION:
1856 : 16614 : case OMP_CLAUSE_IN_REDUCTION:
1857 : 16614 : decl = OMP_CLAUSE_DECL (c);
1858 : 16614 : if (TREE_CODE (decl) != MEM_REF && !is_omp_target (ctx->stmt))
1859 : : {
1860 : 13838 : if (is_variable_sized (decl))
1861 : 0 : install_var_local (decl, ctx);
1862 : 13838 : fixup_remapped_decl (decl, ctx, false);
1863 : : }
1864 : 16614 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1865 : 2832 : scan_array_reductions = true;
1866 : : break;
1867 : :
1868 : 530 : case OMP_CLAUSE_TASK_REDUCTION:
1869 : 530 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
1870 : 2832 : scan_array_reductions = true;
1871 : : break;
1872 : :
1873 : 29903 : case OMP_CLAUSE_SHARED:
1874 : : /* Ignore shared directives in teams construct inside of
1875 : : target construct. */
1876 : 29903 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
1877 : 29903 : && !is_host_teams_ctx (ctx))
1878 : : break;
1879 : 18100 : decl = OMP_CLAUSE_DECL (c);
1880 : 18100 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
1881 : : break;
1882 : 12067 : if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
1883 : : {
1884 : 83 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl,
1885 : : ctx->outer)))
1886 : : break;
1887 : 76 : bool by_ref = use_pointer_for_field (decl, ctx);
1888 : 76 : install_var_field (decl, by_ref, 11, ctx);
1889 : 76 : break;
1890 : : }
1891 : 11984 : fixup_remapped_decl (decl, ctx, false);
1892 : 11984 : break;
1893 : :
1894 : 75320 : case OMP_CLAUSE_MAP:
1895 : 75320 : if (!is_gimple_omp_offloaded (ctx->stmt))
1896 : : break;
1897 : 51010 : decl = OMP_CLAUSE_DECL (c);
1898 : 51010 : if (DECL_P (decl)
1899 : 32241 : && ((OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
1900 : 28708 : && (OMP_CLAUSE_MAP_KIND (c)
1901 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
1902 : 3932 : || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1903 : 29441 : && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
1904 : 58225 : && varpool_node::get_create (decl)->offloadable)
1905 : : break;
1906 : 45347 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
1907 : 43363 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
1908 : 1984 : && is_omp_target (ctx->stmt)
1909 : 47121 : && !is_gimple_omp_offloaded (ctx->stmt))
1910 : : break;
1911 : 45347 : if (DECL_P (decl))
1912 : : {
1913 : 26578 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
1914 : 23618 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
1915 : 6491 : && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
1916 : 28133 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
1917 : : {
1918 : 24 : tree new_decl = lookup_decl (decl, ctx);
1919 : 24 : TREE_TYPE (new_decl)
1920 : 48 : = remap_type (TREE_TYPE (decl), &ctx->cb);
1921 : : }
1922 : 26554 : else if (DECL_SIZE (decl)
1923 : 26554 : && !poly_int_tree_p (DECL_SIZE (decl)))
1924 : : {
1925 : 710 : tree decl2 = DECL_VALUE_EXPR (decl);
1926 : 710 : gcc_assert (INDIRECT_REF_P (decl2));
1927 : 710 : decl2 = TREE_OPERAND (decl2, 0);
1928 : 710 : gcc_assert (DECL_P (decl2));
1929 : 710 : fixup_remapped_decl (decl2, ctx, false);
1930 : 710 : fixup_remapped_decl (decl, ctx, true);
1931 : : }
1932 : : else
1933 : 25844 : 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 : 0 : case OMP_CLAUSE__CACHE_:
2000 : 0 : case OMP_CLAUSE_NOHOST:
2001 : 0 : default:
2002 : 0 : gcc_unreachable ();
2003 : : }
2004 : : }
2005 : :
2006 : 127802 : gcc_checking_assert (!scan_array_reductions
2007 : : || !is_gimple_omp_oacc (ctx->stmt));
2008 : : if (scan_array_reductions)
2009 : : {
2010 : 34551 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
2011 : 29376 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
2012 : 27000 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
2013 : 26376 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
2014 : 30080 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
2015 : : {
2016 : 2178 : omp_context *rctx = ctx;
2017 : 2178 : if (is_omp_target (ctx->stmt))
2018 : 60 : rctx = ctx->outer;
2019 : 2178 : scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c), rctx);
2020 : 2178 : scan_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), rctx);
2021 : : }
2022 : 27198 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
2023 : 27198 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
2024 : 7234 : scan_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
2025 : 19964 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
2026 : 19964 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
2027 : 654 : scan_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
2028 : : }
2029 : 127802 : if (flex_array_ptr)
2030 : : {
2031 : 0 : tree field = build_range_type (size_type_node,
2032 : 0 : build_int_cstu (size_type_node, 0),
2033 : : NULL_TREE);
2034 : 0 : field = build_array_type (ptr_type_node, field);
2035 : 0 : field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, field);
2036 : 0 : SET_DECL_ALIGN (field, TYPE_ALIGN (ptr_type_node));
2037 : 0 : DECL_CONTEXT (field) = ctx->record_type;
2038 : 0 : DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
2039 : 0 : TYPE_FIELDS (ctx->record_type) = field;
2040 : : }
2041 : 127802 : }
2042 : :
2043 : : /* Create a new name for omp child function. Returns an identifier. */
2044 : :
2045 : : static tree
2046 : 50060 : create_omp_child_function_name (bool task_copy)
2047 : : {
2048 : 99556 : return clone_function_name_numbered (current_function_decl,
2049 : 50060 : 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 : 119142 : omp_maybe_offloaded_ctx (omp_context *ctx)
2057 : : {
2058 : 119142 : if (cgraph_node::get (current_function_decl)->offloadable)
2059 : : return true;
2060 : 200081 : for (; ctx; ctx = ctx->outer)
2061 : 119151 : 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 : 50060 : create_omp_child_function (omp_context *ctx, bool task_copy)
2071 : : {
2072 : 50060 : tree decl, type, name, t;
2073 : :
2074 : 50060 : name = create_omp_child_function_name (task_copy);
2075 : 50060 : if (task_copy)
2076 : 564 : type = build_function_type_list (void_type_node, ptr_type_node,
2077 : : ptr_type_node, NULL_TREE);
2078 : : else
2079 : 49496 : type = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
2080 : :
2081 : 50060 : decl = build_decl (gimple_location (ctx->stmt), FUNCTION_DECL, name, type);
2082 : :
2083 : 50060 : gcc_checking_assert (!is_gimple_omp_oacc (ctx->stmt)
2084 : : || !task_copy);
2085 : 38684 : if (!task_copy)
2086 : 49496 : ctx->cb.dst_fn = decl;
2087 : : else
2088 : 564 : gimple_omp_task_set_copy_fn (ctx->stmt, decl);
2089 : :
2090 : 50060 : TREE_STATIC (decl) = 1;
2091 : 50060 : TREE_USED (decl) = 1;
2092 : 50060 : DECL_ARTIFICIAL (decl) = 1;
2093 : 50060 : DECL_IGNORED_P (decl) = 0;
2094 : 50060 : TREE_PUBLIC (decl) = 0;
2095 : 50060 : DECL_UNINLINABLE (decl) = 1;
2096 : 50060 : DECL_EXTERNAL (decl) = 0;
2097 : 50060 : DECL_CONTEXT (decl) = NULL_TREE;
2098 : 50060 : DECL_INITIAL (decl) = make_node (BLOCK);
2099 : 50060 : BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
2100 : 50060 : DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (current_function_decl);
2101 : : /* Remove omp declare simd attribute from the new attributes. */
2102 : 50060 : if (tree a = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (decl)))
2103 : : {
2104 : 9 : while (tree a2 = lookup_attribute ("omp declare simd", TREE_CHAIN (a)))
2105 : : a = a2;
2106 : 9 : a = TREE_CHAIN (a);
2107 : 22 : for (tree *p = &DECL_ATTRIBUTES (decl); *p != a;)
2108 : 13 : if (is_attribute_p ("omp declare simd", get_attribute_name (*p)))
2109 : 9 : *p = TREE_CHAIN (*p);
2110 : : else
2111 : : {
2112 : 4 : tree chain = TREE_CHAIN (*p);
2113 : 4 : *p = copy_node (*p);
2114 : 4 : p = &TREE_CHAIN (*p);
2115 : 4 : *p = chain;
2116 : : }
2117 : : }
2118 : 100120 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
2119 : 50060 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
2120 : 100120 : DECL_FUNCTION_SPECIFIC_TARGET (decl)
2121 : 50060 : = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
2122 : 100120 : DECL_FUNCTION_VERSIONED (decl)
2123 : 50060 : = DECL_FUNCTION_VERSIONED (current_function_decl);
2124 : :
2125 : 50060 : if (omp_maybe_offloaded_ctx (ctx))
2126 : : {
2127 : 29477 : cgraph_node::get_create (decl)->offloadable = 1;
2128 : 29477 : if (ENABLE_OFFLOADING)
2129 : : g->have_offload = true;
2130 : : }
2131 : :
2132 : 50060 : if (cgraph_node::get_create (decl)->offloadable)
2133 : : {
2134 : 29477 : const char *target_attr = (is_gimple_omp_offloaded (ctx->stmt)
2135 : 29477 : ? "omp target entrypoint"
2136 : 29477 : : "omp declare target");
2137 : 29477 : if (lookup_attribute ("omp declare target",
2138 : 29477 : DECL_ATTRIBUTES (current_function_decl)))
2139 : : {
2140 : 3024 : if (is_gimple_omp_offloaded (ctx->stmt))
2141 : 1618 : DECL_ATTRIBUTES (decl)
2142 : 1618 : = remove_attribute ("omp declare target",
2143 : 1618 : copy_list (DECL_ATTRIBUTES (decl)));
2144 : : else
2145 : : target_attr = NULL;
2146 : : }
2147 : 1618 : if (target_attr
2148 : 28071 : && is_gimple_omp_offloaded (ctx->stmt)
2149 : 23314 : && lookup_attribute ("noclone", DECL_ATTRIBUTES (decl)) == NULL_TREE)
2150 : 17809 : DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("noclone"),
2151 : 17809 : NULL_TREE, DECL_ATTRIBUTES (decl));
2152 : 29477 : if (target_attr)
2153 : 28071 : DECL_ATTRIBUTES (decl)
2154 : 56142 : = tree_cons (get_identifier (target_attr),
2155 : 28071 : NULL_TREE, DECL_ATTRIBUTES (decl));
2156 : : }
2157 : :
2158 : 50060 : t = build_decl (DECL_SOURCE_LOCATION (decl),
2159 : : RESULT_DECL, NULL_TREE, void_type_node);
2160 : 50060 : DECL_ARTIFICIAL (t) = 1;
2161 : 50060 : DECL_IGNORED_P (t) = 1;
2162 : 50060 : DECL_CONTEXT (t) = decl;
2163 : 50060 : DECL_RESULT (decl) = t;
2164 : :
2165 : 50060 : tree data_name = get_identifier (".omp_data_i");
2166 : 50060 : t = build_decl (DECL_SOURCE_LOCATION (decl), PARM_DECL, data_name,
2167 : : ptr_type_node);
2168 : 50060 : DECL_ARTIFICIAL (t) = 1;
2169 : 50060 : DECL_NAMELESS (t) = 1;
2170 : 50060 : DECL_ARG_TYPE (t) = ptr_type_node;
2171 : 50060 : DECL_CONTEXT (t) = current_function_decl;
2172 : 50060 : TREE_USED (t) = 1;
2173 : 50060 : TREE_READONLY (t) = 1;
2174 : 50060 : DECL_ARGUMENTS (decl) = t;
2175 : 50060 : if (!task_copy)
2176 : 49496 : ctx->receiver_decl = t;
2177 : : else
2178 : : {
2179 : 564 : t = build_decl (DECL_SOURCE_LOCATION (decl),
2180 : : PARM_DECL, get_identifier (".omp_data_o"),
2181 : : ptr_type_node);
2182 : 564 : DECL_ARTIFICIAL (t) = 1;
2183 : 564 : DECL_NAMELESS (t) = 1;
2184 : 564 : DECL_ARG_TYPE (t) = ptr_type_node;
2185 : 564 : DECL_CONTEXT (t) = current_function_decl;
2186 : 564 : TREE_USED (t) = 1;
2187 : 564 : TREE_ADDRESSABLE (t) = 1;
2188 : 564 : DECL_CHAIN (t) = DECL_ARGUMENTS (decl);
2189 : 564 : 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 : 50060 : push_struct_function (decl);
2196 : 50060 : cfun->function_end_locus = gimple_location (ctx->stmt);
2197 : 50060 : init_tree_ssa (cfun);
2198 : 50060 : pop_cfun ();
2199 : 50060 : }
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 : 35252 : omp_find_combined_for (gimple_stmt_iterator *gsi_p,
2206 : : bool *handled_ops_p,
2207 : : struct walk_stmt_info *wi)
2208 : : {
2209 : 35252 : gimple *stmt = gsi_stmt (*gsi_p);
2210 : :
2211 : 35252 : *handled_ops_p = true;
2212 : 35252 : switch (gimple_code (stmt))
2213 : : {
2214 : 19674 : WALK_SUBSTMTS;
2215 : :
2216 : 13514 : case GIMPLE_OMP_FOR:
2217 : 13514 : if (gimple_omp_for_combined_into_p (stmt)
2218 : 13514 : && gimple_omp_for_kind (stmt)
2219 : 7968 : == *(const enum gf_mask *) (wi->info))
2220 : : {
2221 : 7968 : wi->info = stmt;
2222 : 7968 : 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 : 14099 : add_taskreg_looptemp_clauses (enum gf_mask msk, gimple *stmt,
2235 : : omp_context *outer_ctx)
2236 : : {
2237 : 14099 : struct walk_stmt_info wi;
2238 : :
2239 : 14099 : memset (&wi, 0, sizeof (wi));
2240 : 14099 : wi.val_only = true;
2241 : 14099 : wi.info = (void *) &msk;
2242 : 14099 : walk_gimple_seq (gimple_omp_body (stmt), omp_find_combined_for, NULL, &wi);
2243 : 14099 : if (wi.info != (void *) &msk)
2244 : : {
2245 : 7968 : gomp_for *for_stmt = as_a <gomp_for *> ((gimple *) wi.info);
2246 : 7968 : struct omp_for_data fd;
2247 : 7968 : 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 : 7968 : size_t count = 2, i;
2252 : 7968 : tree type = fd.iter_type;
2253 : 7968 : if (fd.collapse > 1
2254 : 2635 : && TREE_CODE (fd.loop.n2) != INTEGER_CST)
2255 : : {
2256 : 1460 : 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 : 1460 : if (omp_find_clause (gimple_omp_for_clauses (for_stmt),
2261 : : OMP_CLAUSE_LASTPRIVATE)
2262 : 1460 : || (msk == GF_OMP_FOR_KIND_FOR
2263 : 1329 : && omp_find_clause (gimple_omp_parallel_clauses (stmt),
2264 : : OMP_CLAUSE_LASTPRIVATE)))
2265 : : {
2266 : 722 : tree temp = create_tmp_var (type);
2267 : 722 : tree c = build_omp_clause (UNKNOWN_LOCATION,
2268 : : OMP_CLAUSE__LOOPTEMP_);
2269 : 722 : insert_decl_map (&outer_ctx->cb, temp, temp);
2270 : 722 : OMP_CLAUSE_DECL (c) = temp;
2271 : 722 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2272 : 722 : gimple_omp_taskreg_set_clauses (stmt, c);
2273 : : }
2274 : 1460 : if (fd.non_rect
2275 : 24 : && fd.last_nonrect == fd.first_nonrect + 1)
2276 : 12 : if (tree v = gimple_omp_for_index (for_stmt, fd.last_nonrect))
2277 : 12 : if (!TYPE_UNSIGNED (TREE_TYPE (v)))
2278 : : {
2279 : 12 : v = gimple_omp_for_index (for_stmt, fd.first_nonrect);
2280 : 12 : tree type2 = TREE_TYPE (v);
2281 : 12 : count++;
2282 : 48 : for (i = 0; i < 3; i++)
2283 : : {
2284 : 36 : tree temp = create_tmp_var (type2);
2285 : 36 : tree c = build_omp_clause (UNKNOWN_LOCATION,
2286 : : OMP_CLAUSE__LOOPTEMP_);
2287 : 36 : insert_decl_map (&outer_ctx->cb, temp, temp);
2288 : 36 : OMP_CLAUSE_DECL (c) = temp;
2289 : 36 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2290 : 36 : gimple_omp_taskreg_set_clauses (stmt, c);
2291 : : }
2292 : : }
2293 : : }
2294 : 26743 : for (i = 0; i < count; i++)
2295 : : {
2296 : 18775 : tree temp = create_tmp_var (type);
2297 : 18775 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
2298 : 18775 : insert_decl_map (&outer_ctx->cb, temp, temp);
2299 : 18775 : OMP_CLAUSE_DECL (c) = temp;
2300 : 18775 : OMP_CLAUSE_CHAIN (c) = gimple_omp_taskreg_clauses (stmt);
2301 : 18775 : gimple_omp_taskreg_set_clauses (stmt, c);
2302 : : }
2303 : : }
2304 : 14099 : if (msk == GF_OMP_FOR_KIND_TASKLOOP
2305 : 15679 : && omp_find_clause (gimple_omp_task_clauses (stmt),
2306 : : OMP_CLAUSE_REDUCTION))
2307 : : {
2308 : 513 : tree type = build_pointer_type (pointer_sized_int_node);
2309 : 513 : tree temp = create_tmp_var (type);
2310 : 513 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
2311 : 513 : insert_decl_map (&outer_ctx->cb, temp, temp);
2312 : 513 : OMP_CLAUSE_DECL (c) = temp;
2313 : 513 : OMP_CLAUSE_CHAIN (c) = gimple_omp_task_clauses (stmt);
2314 : 513 : gimple_omp_task_set_clauses (stmt, c);
2315 : : }
2316 : 14099 : }
2317 : :
2318 : : /* Scan an OpenMP parallel directive. */
2319 : :
2320 : : static void
2321 : 18127 : scan_omp_parallel (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
2322 : : {
2323 : 18127 : omp_context *ctx;
2324 : 18127 : tree name;
2325 : 18127 : 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 : 18127 : if (optimize > 0
2330 : 12425 : && empty_body_p (gimple_omp_body (stmt))
2331 : 18172 : && omp_find_clause (gimple_omp_parallel_clauses (stmt),
2332 : : OMP_CLAUSE_COPYIN) == NULL)
2333 : : {
2334 : 39 : gsi_replace (gsi, gimple_build_nop (), false);
2335 : 39 : return;
2336 : : }
2337 : :
2338 : 18088 : if (gimple_omp_parallel_combined_p (stmt))
2339 : 12519 : add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_FOR, stmt, outer_ctx);
2340 : 18088 : for (tree c = omp_find_clause (gimple_omp_parallel_clauses (stmt),
2341 : : OMP_CLAUSE_REDUCTION);
2342 : 20931 : c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_REDUCTION))
2343 : 3938 : if (OMP_CLAUSE_REDUCTION_TASK (c))
2344 : : {
2345 : 66 : tree type = build_pointer_type (pointer_sized_int_node);
2346 : 66 : tree temp = create_tmp_var (type);
2347 : 66 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
2348 : 66 : if (outer_ctx)
2349 : 17 : insert_decl_map (&outer_ctx->cb, temp, temp);
2350 : 66 : OMP_CLAUSE_DECL (c) = temp;
2351 : 66 : OMP_CLAUSE_CHAIN (c) = gimple_omp_parallel_clauses (stmt);
2352 : 66 : gimple_omp_parallel_set_clauses (stmt, c);
2353 : 66 : break;
2354 : : }
2355 : 3872 : else if (OMP_CLAUSE_CHAIN (c) == NULL_TREE)
2356 : : break;
2357 : :
2358 : 18088 : ctx = new_omp_context (stmt, outer_ctx);
2359 : 18088 : taskreg_contexts.safe_push (ctx);
2360 : 18088 : if (taskreg_nesting_level > 1)
2361 : 6186 : ctx->is_nested = true;
2362 : 18088 : ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
2363 : 18088 : ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
2364 : 18088 : name = create_tmp_var_name (".omp_data_s");
2365 : 18088 : name = build_decl (gimple_location (stmt),
2366 : : TYPE_DECL, name, ctx->record_type);
2367 : 18088 : DECL_ARTIFICIAL (name) = 1;
2368 : 18088 : DECL_NAMELESS (name) = 1;
2369 : 18088 : TYPE_NAME (ctx->record_type) = name;
2370 : 18088 : TYPE_ARTIFICIAL (ctx->record_type) = 1;
2371 : 18088 : create_omp_child_function (ctx, false);
2372 : 18088 : gimple_omp_parallel_set_child_fn (stmt, ctx->cb.dst_fn);
2373 : :
2374 : 18088 : scan_sharing_clauses (gimple_omp_parallel_clauses (stmt), ctx);
2375 : 18088 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
2376 : :
2377 : 18088 : if (TYPE_FIELDS (ctx->record_type) == NULL)
2378 : 3538 : ctx->record_type = ctx->receiver_decl = NULL;
2379 : : }
2380 : :
2381 : : /* Scan an OpenMP task directive. */
2382 : :
2383 : : static void
2384 : 5543 : scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
2385 : : {
2386 : 5543 : omp_context *ctx;
2387 : 5543 : tree name, t;
2388 : 5543 : 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 : 5543 : if (optimize > 0
2393 : 2592 : && gimple_omp_body (stmt)
2394 : 2553 : && empty_body_p (gimple_omp_body (stmt))
2395 : 5595 : && !omp_find_clause (gimple_omp_task_clauses (stmt), OMP_CLAUSE_DEPEND))
2396 : : {
2397 : 19 : gsi_replace (gsi, gimple_build_nop (), false);
2398 : 125 : return;
2399 : : }
2400 : :
2401 : 5524 : if (gimple_omp_task_taskloop_p (stmt))
2402 : 1580 : add_taskreg_looptemp_clauses (GF_OMP_FOR_KIND_TASKLOOP, stmt, outer_ctx);
2403 : :
2404 : 5524 : ctx = new_omp_context (stmt, outer_ctx);
2405 : :
2406 : 5524 : if (gimple_omp_task_taskwait_p (stmt))
2407 : : {
2408 : 87 : scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
2409 : 87 : return;
2410 : : }
2411 : :
2412 : 5437 : taskreg_contexts.safe_push (ctx);
2413 : 5437 : if (taskreg_nesting_level > 1)
2414 : 2319 : ctx->is_nested = true;
2415 : 5437 : ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
2416 : 5437 : ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
2417 : 5437 : name = create_tmp_var_name (".omp_data_s");
2418 : 5437 : name = build_decl (gimple_location (stmt),
2419 : : TYPE_DECL, name, ctx->record_type);
2420 : 5437 : DECL_ARTIFICIAL (name) = 1;
2421 : 5437 : DECL_NAMELESS (name) = 1;
2422 : 5437 : TYPE_NAME (ctx->record_type) = name;
2423 : 5437 : TYPE_ARTIFICIAL (ctx->record_type) = 1;
2424 : 5437 : create_omp_child_function (ctx, false);
2425 : 5437 : gimple_omp_task_set_child_fn (stmt, ctx->cb.dst_fn);
2426 : :
2427 : 5437 : scan_sharing_clauses (gimple_omp_task_clauses (stmt), ctx);
2428 : :
2429 : 5437 : if (ctx->srecord_type)
2430 : : {
2431 : 564 : name = create_tmp_var_name (".omp_data_a");
2432 : 564 : name = build_decl (gimple_location (stmt),
2433 : : TYPE_DECL, name, ctx->srecord_type);
2434 : 564 : DECL_ARTIFICIAL (name) = 1;
2435 : 564 : DECL_NAMELESS (name) = 1;
2436 : 564 : TYPE_NAME (ctx->srecord_type) = name;
2437 : 564 : TYPE_ARTIFICIAL (ctx->srecord_type) = 1;
2438 : 564 : create_omp_child_function (ctx, true);
2439 : : }
2440 : :
2441 : 5437 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
2442 : :
2443 : 5437 : if (TYPE_FIELDS (ctx->record_type) == NULL)
2444 : : {
2445 : 1982 : ctx->record_type = ctx->receiver_decl = NULL;
2446 : 1982 : t = build_int_cst (long_integer_type_node, 0);
2447 : 1982 : gimple_omp_task_set_arg_size (stmt, t);
2448 : 1982 : t = build_int_cst (long_integer_type_node, 1);
2449 : 1982 : 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 : 276 : finish_taskreg_remap (tree *tp, int *walk_subtrees, void *data)
2459 : : {
2460 : 276 : if (VAR_P (*tp))
2461 : : {
2462 : 96 : omp_context *ctx = (omp_context *) data;
2463 : 96 : tree t = maybe_lookup_decl_in_outer_ctx (*tp, ctx);
2464 : 96 : if (t != *tp)
2465 : : {
2466 : 9 : if (DECL_HAS_VALUE_EXPR_P (t))
2467 : 0 : t = unshare_expr (DECL_VALUE_EXPR (t));
2468 : 9 : *tp = t;
2469 : : }
2470 : 96 : *walk_subtrees = 0;
2471 : : }
2472 : 180 : else if (IS_TYPE_OR_DECL_P (*tp))
2473 : 0 : *walk_subtrees = 0;
2474 : 276 : 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 : 26182 : finish_taskreg_scan (omp_context *ctx)
2483 : : {
2484 : 26182 : 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 : 19218 : if (make_addressable_vars)
2492 : : {
2493 : 2206 : tree c;
2494 : :
2495 : 12331 : for (c = gimple_omp_taskreg_clauses (ctx->stmt);
2496 : 12331 : c; c = OMP_CLAUSE_CHAIN (c))
2497 : 10125 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
2498 : 10125 : && !OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
2499 : : {
2500 : 2614 : 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 : 2614 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx)))
2505 : 281 : continue;
2506 : 2333 : if (!bitmap_bit_p (make_addressable_vars, DECL_UID (decl))
2507 : 2333 : || !use_pointer_for_field (decl, ctx))
2508 : 1784 : continue;
2509 : 549 : tree field = lookup_field (decl, ctx);
2510 : 549 : if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE
2511 : 549 : && TREE_TYPE (TREE_TYPE (field)) == TREE_TYPE (decl))
2512 : 495 : continue;
2513 : 54 : TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
2514 : 54 : TREE_THIS_VOLATILE (field) = 0;
2515 : 54 : DECL_USER_ALIGN (field) = 0;
2516 : 54 : SET_DECL_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
2517 : 54 : if (TYPE_ALIGN (ctx->record_type) < DECL_ALIGN (field))
2518 : 16 : SET_TYPE_ALIGN (ctx->record_type, DECL_ALIGN (field));
2519 : 54 : if (ctx->srecord_type)
2520 : : {
2521 : 0 : tree sfield = lookup_sfield (decl, ctx);
2522 : 0 : TREE_TYPE (sfield) = TREE_TYPE (field);
2523 : 0 : TREE_THIS_VOLATILE (sfield) = 0;
2524 : 0 : DECL_USER_ALIGN (sfield) = 0;
2525 : 0 : SET_DECL_ALIGN (sfield, DECL_ALIGN (field));
2526 : 0 : if (TYPE_ALIGN (ctx->srecord_type) < DECL_ALIGN (sfield))
2527 : 0 : SET_TYPE_ALIGN (ctx->srecord_type, DECL_ALIGN (sfield));
2528 : : }
2529 : : }
2530 : : }
2531 : :
2532 : 19218 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_PARALLEL)
2533 : : {
2534 : 14550 : tree clauses = gimple_omp_parallel_clauses (ctx->stmt);
2535 : 14550 : tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
2536 : 14550 : if (c)
2537 : : {
2538 : : /* Move the _reductemp_ clause first. GOMP_parallel_reductions
2539 : : expects to find it at the start of data. */
2540 : 66 : tree f = lookup_field (OMP_CLAUSE_DECL (c), ctx);
2541 : 66 : tree *p = &TYPE_FIELDS (ctx->record_type);
2542 : 152 : while (*p)
2543 : 152 : if (*p == f)
2544 : : {
2545 : 66 : *p = DECL_CHAIN (*p);
2546 : 66 : break;
2547 : : }
2548 : : else
2549 : 86 : p = &DECL_CHAIN (*p);
2550 : 66 : DECL_CHAIN (f) = TYPE_FIELDS (ctx->record_type);
2551 : 66 : TYPE_FIELDS (ctx->record_type) = f;
2552 : : }
2553 : 14550 : layout_type (ctx->record_type);
2554 : 14550 : fixup_child_record_type (ctx);
2555 : : }
2556 : 4668 : else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
2557 : : {
2558 : 1213 : layout_type (ctx->record_type);
2559 : 1213 : fixup_child_record_type (ctx);
2560 : : }
2561 : : else
2562 : : {
2563 : 3455 : location_t loc = gimple_location (ctx->stmt);
2564 : 3455 : tree *p, vla_fields = NULL_TREE, *q = &vla_fields;
2565 : 3455 : tree detach_clause
2566 : 3455 : = omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
2567 : : OMP_CLAUSE_DETACH);
2568 : : /* Move VLA fields to the end. */
2569 : 3455 : p = &TYPE_FIELDS (ctx->record_type);
2570 : 15487 : while (*p)
2571 : 12032 : if (!TYPE_SIZE_UNIT (TREE_TYPE (*p))
2572 : 12032 : || ! TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (*p))))
2573 : : {
2574 : 96 : *q = *p;
2575 : 96 : *p = TREE_CHAIN (*p);
2576 : 96 : TREE_CHAIN (*q) = NULL_TREE;
2577 : 96 : q = &TREE_CHAIN (*q);
2578 : : }
2579 : : else
2580 : 11936 : p = &DECL_CHAIN (*p);
2581 : 3455 : *p = vla_fields;
2582 : 3455 : 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 : 1580 : tree clauses = gimple_omp_task_clauses (ctx->stmt);
2588 : 1580 : tree c1 = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
2589 : 1580 : tree c2 = omp_find_clause (OMP_CLAUSE_CHAIN (c1),
2590 : : OMP_CLAUSE__LOOPTEMP_);
2591 : 1580 : tree c3 = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
2592 : 1580 : tree f1 = lookup_field (OMP_CLAUSE_DECL (c1), ctx);
2593 : 1580 : tree f2 = lookup_field (OMP_CLAUSE_DECL (c2), ctx);
2594 : 2093 : tree f3 = c3 ? lookup_field (OMP_CLAUSE_DECL (c3), ctx) : NULL_TREE;
2595 : 1580 : p = &TYPE_FIELDS (ctx->record_type);
2596 : 8997 : while (*p)
2597 : 7417 : if (*p == f1 || *p == f2 || *p == f3)
2598 : 3673 : *p = DECL_CHAIN (*p);
2599 : : else
2600 : 3744 : p = &DECL_CHAIN (*p);
2601 : 1580 : DECL_CHAIN (f1) = f2;
2602 : 1580 : if (c3)
2603 : : {
2604 : 513 : DECL_CHAIN (f2) = f3;
2605 : 513 : DECL_CHAIN (f3) = TYPE_FIELDS (ctx->record_type);
2606 : : }
2607 : : else
2608 : 1067 : DECL_CHAIN (f2) = TYPE_FIELDS (ctx->record_type);
2609 : 1580 : TYPE_FIELDS (ctx->record_type) = f1;
2610 : 1580 : if (ctx->srecord_type)
2611 : : {
2612 : 389 : f1 = lookup_sfield (OMP_CLAUSE_DECL (c1), ctx);
2613 : 389 : f2 = lookup_sfield (OMP_CLAUSE_DECL (c2), ctx);
2614 : 389 : if (c3)
2615 : 186 : f3 = lookup_sfield (OMP_CLAUSE_DECL (c3), ctx);
2616 : 389 : p = &TYPE_FIELDS (ctx->srecord_type);
2617 : 2097 : while (*p)
2618 : 1708 : if (*p == f1 || *p == f2 || *p == f3)
2619 : 964 : *p = DECL_CHAIN (*p);
2620 : : else
2621 : 744 : p = &DECL_CHAIN (*p);
2622 : 389 : DECL_CHAIN (f1) = f2;
2623 : 389 : DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
2624 : 389 : if (c3)
2625 : : {
2626 : 186 : DECL_CHAIN (f2) = f3;
2627 : 186 : DECL_CHAIN (f3) = TYPE_FIELDS (ctx->srecord_type);
2628 : : }
2629 : : else
2630 : 203 : DECL_CHAIN (f2) = TYPE_FIELDS (ctx->srecord_type);
2631 : 389 : TYPE_FIELDS (ctx->srecord_type) = f1;
2632 : : }
2633 : : }
2634 : 3455 : if (detach_clause)
2635 : : {
2636 : 189 : tree c, field;
2637 : :
2638 : : /* Look for a firstprivate clause with the detach event handle. */
2639 : 540 : for (c = gimple_omp_taskreg_clauses (ctx->stmt);
2640 : 540 : c; c = OMP_CLAUSE_CHAIN (c))
2641 : : {
2642 : 540 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
2643 : 347 : continue;
2644 : 193 : if (maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c), ctx)
2645 : 193 : == OMP_CLAUSE_DECL (detach_clause))
2646 : : break;
2647 : : }
2648 : :
2649 : 189 : gcc_assert (c);
2650 : 189 : 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 : 189 : p = &TYPE_FIELDS (ctx->record_type);
2656 : 518 : while (*p)
2657 : 329 : if (*p == field)
2658 : 189 : *p = DECL_CHAIN (*p);
2659 : : else
2660 : 140 : p = &DECL_CHAIN (*p);
2661 : 189 : DECL_CHAIN (field) = TYPE_FIELDS (ctx->record_type);
2662 : 189 : TYPE_FIELDS (ctx->record_type) = field;
2663 : 189 : if (ctx->srecord_type)
2664 : : {
2665 : 3 : field = lookup_sfield (OMP_CLAUSE_DECL (c), ctx);
2666 : 3 : p = &TYPE_FIELDS (ctx->srecord_type);
2667 : 9 : while (*p)
2668 : 6 : if (*p == field)
2669 : 3 : *p = DECL_CHAIN (*p);
2670 : : else
2671 : 3 : p = &DECL_CHAIN (*p);
2672 : 3 : DECL_CHAIN (field) = TYPE_FIELDS (ctx->srecord_type);
2673 : 3 : TYPE_FIELDS (ctx->srecord_type) = field;
2674 : : }
2675 : : }
2676 : 3455 : layout_type (ctx->record_type);
2677 : 3455 : fixup_child_record_type (ctx);
2678 : 3455 : if (ctx->srecord_type)
2679 : 564 : layout_type (ctx->srecord_type);
2680 : 3455 : tree t = fold_convert_loc (loc, long_integer_type_node,
2681 : 3455 : TYPE_SIZE_UNIT (ctx->record_type));
2682 : 3455 : if (TREE_CODE (t) != INTEGER_CST)
2683 : : {
2684 : 21 : t = unshare_expr (t);
2685 : 21 : walk_tree (&t, finish_taskreg_remap, ctx, NULL);
2686 : : }
2687 : 3455 : gimple_omp_task_set_arg_size (ctx->stmt, t);
2688 : 3455 : t = build_int_cst (long_integer_type_node,
2689 : 3455 : TYPE_ALIGN_UNIT (ctx->record_type));
2690 : 3455 : gimple_omp_task_set_arg_align (ctx->stmt, t);
2691 : : }
2692 : : }
2693 : :
2694 : : /* Find the enclosing offload context. */
2695 : :
2696 : : static omp_context *
2697 : 9669 : enclosing_target_ctx (omp_context *ctx)
2698 : : {
2699 : 43389 : for (; ctx; ctx = ctx->outer)
2700 : 41773 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET)
2701 : : break;
2702 : :
2703 : 22131 : 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 : 11341 : ctx_in_oacc_kernels_region (omp_context *ctx)
2712 : : {
2713 : 39189 : for (;ctx != NULL; ctx = ctx->outer)
2714 : : {
2715 : 29520 : gimple *stmt = ctx->stmt;
2716 : 29520 : if (gimple_code (stmt) == GIMPLE_OMP_TARGET
2717 : 29520 : && 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 : 7194 : check_oacc_kernel_gwv (gomp_for *stmt, omp_context *ctx)
2731 : : {
2732 : 7194 : bool checking = true;
2733 : 7194 : unsigned outer_mask = 0;
2734 : 7194 : unsigned this_mask = 0;
2735 : 7194 : bool has_seq = false, has_auto = false;
2736 : :
2737 : 7194 : if (ctx->outer)
2738 : 4850 : outer_mask = check_oacc_kernel_gwv (NULL, ctx->outer);
2739 : 7194 : if (!stmt)
2740 : : {
2741 : 4850 : checking = false;
2742 : 4850 : if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR)
2743 : : return outer_mask;
2744 : 1730 : stmt = as_a <gomp_for *> (ctx->stmt);
2745 : : }
2746 : :
2747 : 11352 : for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
2748 : : {
2749 : 7278 : switch (OMP_CLAUSE_CODE (c))
2750 : : {
2751 : 644 : case OMP_CLAUSE_GANG:
2752 : 644 : this_mask |= GOMP_DIM_MASK (GOMP_DIM_GANG);
2753 : 644 : break;
2754 : 439 : case OMP_CLAUSE_WORKER:
2755 : 439 : this_mask |= GOMP_DIM_MASK (GOMP_DIM_WORKER);
2756 : 439 : break;
2757 : 342 : case OMP_CLAUSE_VECTOR:
2758 : 342 : this_mask |= GOMP_DIM_MASK (GOMP_DIM_VECTOR);
2759 : 342 : break;
2760 : 63 : case OMP_CLAUSE_SEQ:
2761 : 63 : has_seq = true;
2762 : 63 : break;
2763 : 94 : case OMP_CLAUSE_AUTO:
2764 : 94 : has_auto = true;
2765 : 94 : break;
2766 : : default:
2767 : : break;
2768 : : }
2769 : : }
2770 : :
2771 : 4074 : if (checking)
2772 : : {
2773 : 2344 : if (has_seq && (this_mask || has_auto))
2774 : 36 : error_at (gimple_location (stmt), "%<seq%> overrides other"
2775 : : " OpenACC loop specifiers");
2776 : 2308 : else if (has_auto && this_mask)
2777 : 30 : error_at (gimple_location (stmt), "%<auto%> conflicts with other"
2778 : : " OpenACC loop specifiers");
2779 : :
2780 : 2344 : if (this_mask & outer_mask)
2781 : 15 : error_at (gimple_location (stmt), "inner loop uses same"
2782 : : " OpenACC parallelism as containing loop");
2783 : : }
2784 : :
2785 : 4074 : return outer_mask | this_mask;
2786 : : }
2787 : :
2788 : : /* Scan a GIMPLE_OMP_FOR. */
2789 : :
2790 : : static omp_context *
2791 : 52646 : scan_omp_for (gomp_for *stmt, omp_context *outer_ctx)
2792 : : {
2793 : 52646 : omp_context *ctx;
2794 : 52646 : size_t i;
2795 : 52646 : tree clauses = gimple_omp_for_clauses (stmt);
2796 : :
2797 : 52646 : ctx = new_omp_context (stmt, outer_ctx);
2798 : :
2799 : 52646 : if (is_gimple_omp_oacc (stmt))
2800 : : {
2801 : 12462 : omp_context *tgt = enclosing_target_ctx (outer_ctx);
2802 : :
2803 : 12462 : if (!(tgt && is_oacc_kernels (tgt)))
2804 : 32271 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
2805 : : {
2806 : 22153 : tree c_op0;
2807 : 22153 : switch (OMP_CLAUSE_CODE (c))
2808 : : {
2809 : 1952 : case OMP_CLAUSE_GANG:
2810 : 1952 : c_op0 = OMP_CLAUSE_GANG_EXPR (c);
2811 : 1952 : break;
2812 : :
2813 : 1426 : case OMP_CLAUSE_WORKER:
2814 : 1426 : c_op0 = OMP_CLAUSE_WORKER_EXPR (c);
2815 : 1426 : break;
2816 : :
2817 : 1627 : case OMP_CLAUSE_VECTOR:
2818 : 1627 : c_op0 = OMP_CLAUSE_VECTOR_EXPR (c);
2819 : 1627 : break;
2820 : :
2821 : 17148 : default:
2822 : 17148 : continue;
2823 : : }
2824 : :
2825 : 5005 : if (c_op0)
2826 : : {
2827 : : /* By construction, this is impossible for OpenACC 'kernels'
2828 : : decomposed parts. */
2829 : 210 : gcc_assert (!(tgt && is_oacc_kernels_decomposed_part (tgt)));
2830 : :
2831 : 210 : error_at (OMP_CLAUSE_LOCATION (c),
2832 : : "argument not permitted on %qs clause",
2833 : 210 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
2834 : 210 : if (tgt)
2835 : 180 : inform (gimple_location (tgt->stmt),
2836 : : "enclosing parent compute construct");
2837 : 30 : else if (oacc_get_fn_attrib (current_function_decl))
2838 : 30 : inform (DECL_SOURCE_LOCATION (current_function_decl),
2839 : : "enclosing routine");
2840 : : else
2841 : 0 : gcc_unreachable ();
2842 : : }
2843 : : }
2844 : :
2845 : 12462 : if (tgt && is_oacc_kernels (tgt))
2846 : 2344 : 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 : 12462 : tree local_reduction_clauses = NULL;
2853 : 39416 : for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
2854 : : {
2855 : 26954 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
2856 : 4711 : local_reduction_clauses
2857 : 4711 : = tree_cons (NULL, c, local_reduction_clauses);
2858 : : }
2859 : 12462 : if (ctx->outer_reduction_clauses == NULL && ctx->outer != NULL)
2860 : 11935 : ctx->outer_reduction_clauses
2861 : 11935 : = chainon (unshare_expr (ctx->outer->local_reduction_clauses),
2862 : : ctx->outer->outer_reduction_clauses);
2863 : 12462 : tree outer_reduction_clauses = ctx->outer_reduction_clauses;
2864 : 12462 : tree local_iter = local_reduction_clauses;
2865 : 17173 : for (; local_iter; local_iter = TREE_CHAIN (local_iter))
2866 : : {
2867 : 4711 : tree local_clause = TREE_VALUE (local_iter);
2868 : 4711 : tree local_var = OMP_CLAUSE_DECL (local_clause);
2869 : 4711 : tree_code local_op = OMP_CLAUSE_REDUCTION_CODE (local_clause);
2870 : 4711 : bool have_outer_reduction = false;
2871 : 4711 : tree ctx_iter = outer_reduction_clauses;
2872 : 5825 : for (; ctx_iter; ctx_iter = TREE_CHAIN (ctx_iter))
2873 : : {
2874 : 3323 : tree outer_clause = TREE_VALUE (ctx_iter);
2875 : 3323 : tree outer_var = OMP_CLAUSE_DECL (outer_clause);
2876 : 3323 : tree_code outer_op = OMP_CLAUSE_REDUCTION_CODE (outer_clause);
2877 : 3323 : if (outer_var == local_var && outer_op != local_op)
2878 : : {
2879 : 535 : if (warning_at (OMP_CLAUSE_LOCATION (local_clause),
2880 : : OPT_Wopenmp, "conflicting reduction "
2881 : : "operations for %qE",
2882 : : local_var))
2883 : 535 : inform (OMP_CLAUSE_LOCATION (outer_clause),
2884 : : "location of the previous reduction for %qE",
2885 : : outer_var);
2886 : : }
2887 : 3323 : if (outer_var == local_var)
2888 : : {
2889 : : have_outer_reduction = true;
2890 : : break;
2891 : : }
2892 : : }
2893 : 4711 : 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 : 2209 : struct omp_context *curr_loop = ctx->outer;
2901 : 2209 : bool found = false;
2902 : 2824 : while (curr_loop != NULL)
2903 : : {
2904 : 2824 : tree curr_iter = curr_loop->local_reduction_clauses;
2905 : 3458 : for (; curr_iter; curr_iter = TREE_CHAIN (curr_iter))
2906 : : {
2907 : 2843 : tree curr_clause = TREE_VALUE (curr_iter);
2908 : 2843 : tree curr_var = OMP_CLAUSE_DECL (curr_clause);
2909 : 2843 : if (curr_var == local_var)
2910 : : {
2911 : : found = true;
2912 : : break;
2913 : : }
2914 : : }
2915 : 2824 : if (!found)
2916 : 615 : 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 : 615 : curr_loop = curr_loop->outer;
2923 : : }
2924 : : }
2925 : : }
2926 : 12462 : ctx->local_reduction_clauses = local_reduction_clauses;
2927 : 12462 : ctx->outer_reduction_clauses
2928 : 12462 : = chainon (unshare_expr (ctx->local_reduction_clauses),
2929 : : ctx->outer_reduction_clauses);
2930 : :
2931 : 12462 : if (tgt && is_oacc_kernels (tgt))
2932 : : {
2933 : : /* Strip out reductions, as they are not handled yet. */
2934 : : tree *prev_ptr = &clauses;
2935 : :
2936 : 7145 : while (tree probe = *prev_ptr)
2937 : : {
2938 : 4801 : tree *next_ptr = &OMP_CLAUSE_CHAIN (probe);
2939 : :
2940 : 4801 : if (OMP_CLAUSE_CODE (probe) == OMP_CLAUSE_REDUCTION)
2941 : 718 : *prev_ptr = *next_ptr;
2942 : : else
2943 : : prev_ptr = next_ptr;
2944 : : }
2945 : :
2946 : 2344 : gimple_omp_for_set_clauses (stmt, clauses);
2947 : : }
2948 : : }
2949 : :
2950 : 52646 : scan_sharing_clauses (clauses, ctx);
2951 : :
2952 : 52646 : scan_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
2953 : 179903 : for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2954 : : {
2955 : 74611 : scan_omp_op (gimple_omp_for_index_ptr (stmt, i), ctx);
2956 : 74611 : scan_omp_op (gimple_omp_for_initial_ptr (stmt, i), ctx);
2957 : 74611 : scan_omp_op (gimple_omp_for_final_ptr (stmt, i), ctx);
2958 : 74611 : scan_omp_op (gimple_omp_for_incr_ptr (stmt, i), ctx);
2959 : : }
2960 : 52646 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
2961 : 52646 : return ctx;
2962 : : }
2963 : :
2964 : : /* Duplicate #pragma omp simd, one for SIMT, another one for SIMD. */
2965 : :
2966 : : static void
2967 : 0 : scan_omp_simd (gimple_stmt_iterator *gsi, gomp_for *stmt,
2968 : : omp_context *outer_ctx)
2969 : : {
2970 : 0 : gbind *bind = gimple_build_bind (NULL, NULL, NULL);
2971 : 0 : gsi_replace (gsi, bind, false);
2972 : 0 : gimple_seq seq = NULL;
2973 : 0 : gimple *g = gimple_build_call_internal (IFN_GOMP_USE_SIMT, 0);
2974 : 0 : tree cond = create_tmp_var_raw (integer_type_node);
2975 : 0 : DECL_CONTEXT (cond) = current_function_decl;
2976 : 0 : DECL_SEEN_IN_BIND_EXPR_P (cond) = 1;
2977 : 0 : gimple_bind_set_vars (bind, cond);
2978 : 0 : gimple_call_set_lhs (g, cond);
2979 : 0 : gimple_seq_add_stmt (&seq, g);
2980 : 0 : tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
2981 : 0 : tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
2982 : 0 : tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
2983 : 0 : g = gimple_build_cond (NE_EXPR, cond, integer_zero_node, lab1, lab2);
2984 : 0 : gimple_seq_add_stmt (&seq, g);
2985 : 0 : g = gimple_build_label (lab1);
2986 : 0 : gimple_seq_add_stmt (&seq, g);
2987 : 0 : gimple_seq new_seq = copy_gimple_seq_and_replace_locals (stmt);
2988 : 0 : gomp_for *new_stmt = as_a <gomp_for *> (new_seq);
2989 : 0 : tree clause = build_omp_clause (gimple_location (stmt), OMP_CLAUSE__SIMT_);
2990 : 0 : OMP_CLAUSE_CHAIN (clause) = gimple_omp_for_clauses (new_stmt);
2991 : 0 : gimple_omp_for_set_clauses (new_stmt, clause);
2992 : 0 : gimple_seq_add_stmt (&seq, new_stmt);
2993 : 0 : g = gimple_build_goto (lab3);
2994 : 0 : gimple_seq_add_stmt (&seq, g);
2995 : 0 : g = gimple_build_label (lab2);
2996 : 0 : gimple_seq_add_stmt (&seq, g);
2997 : 0 : gimple_seq_add_stmt (&seq, stmt);
2998 : 0 : g = gimple_build_label (lab3);
2999 : 0 : gimple_seq_add_stmt (&seq, g);
3000 : 0 : gimple_bind_set_body (bind, seq);
3001 : 0 : update_stmt (bind);
3002 : 0 : scan_omp_for (new_stmt, outer_ctx);
3003 : 0 : scan_omp_for (stmt, outer_ctx)->simt_stmt = new_stmt;
3004 : 0 : }
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 : 83 : 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 : 83 : outer_ctx->scan_inclusive = true;
3021 : 83 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_INCLUSIVE);
3022 : 83 : OMP_CLAUSE_DECL (c) = integer_zero_node;
3023 : :
3024 : 83 : gomp_scan *input_stmt = gimple_build_omp_scan (NULL, NULL_TREE);
3025 : 83 : gomp_scan *scan_stmt = gimple_build_omp_scan (NULL, c);
3026 : 83 : gsi_replace (gsi, input_stmt, false);
3027 : 83 : gimple_seq input_body = NULL;
3028 : 83 : gimple_seq_add_stmt (&input_body, stmt);
3029 : 83 : gsi_insert_after (gsi, scan_stmt, GSI_NEW_STMT);
3030 : :
3031 : 83 : gimple_stmt_iterator input1_gsi = gsi_none ();
3032 : 83 : struct walk_stmt_info wi;
3033 : 83 : memset (&wi, 0, sizeof (wi));
3034 : 83 : wi.val_only = true;
3035 : 83 : wi.info = (void *) &input1_gsi;
3036 : 83 : walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), omp_find_scan, NULL, &wi);
3037 : 83 : gcc_assert (!gsi_end_p (input1_gsi));
3038 : :
3039 : 83 : gimple *input_stmt1 = gsi_stmt (input1_gsi);
3040 : 83 : gsi_next (&input1_gsi);
3041 : 83 : gimple *scan_stmt1 = gsi_stmt (input1_gsi);
3042 : 83 : gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
3043 : 83 : c = gimple_omp_scan_clauses (as_a <gomp_scan *> (scan_stmt1));
3044 : 166 : if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
3045 : : std::swap (input_stmt1, scan_stmt1);
3046 : :
3047 : 83 : gimple_seq input_body1 = gimple_omp_body (input_stmt1);
3048 : 83 : gimple_omp_set_body (input_stmt1, NULL);
3049 : :
3050 : 83 : gimple_seq scan_body = copy_gimple_seq_and_replace_locals (stmt);
3051 : 83 : gomp_for *new_stmt = as_a <gomp_for *> (scan_body);
3052 : :
3053 : 83 : gimple_omp_set_body (input_stmt1, input_body1);
3054 : 83 : gimple_omp_set_body (scan_stmt1, NULL);
3055 : :
3056 : 83 : gimple_stmt_iterator input2_gsi = gsi_none ();
3057 : 83 : memset (&wi, 0, sizeof (wi));
3058 : 83 : wi.val_only = true;
3059 : 83 : wi.info = (void *) &input2_gsi;
3060 : 83 : walk_gimple_seq_mod (gimple_omp_body_ptr (new_stmt), omp_find_scan,
3061 : : NULL, &wi);
3062 : 83 : gcc_assert (!gsi_end_p (input2_gsi));
3063 : :
3064 : 83 : gimple *input_stmt2 = gsi_stmt (input2_gsi);
3065 : 83 : gsi_next (&input2_gsi);
3066 : 83 : gimple *scan_stmt2 = gsi_stmt (input2_gsi);
3067 : 83 : gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
3068 : 166 : if (c && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_EXCLUSIVE)
3069 : : std::swap (input_stmt2, scan_stmt2);
3070 : :
3071 : 83 : gimple_omp_set_body (input_stmt2, NULL);
3072 : :
3073 : 83 : gimple_omp_set_body (input_stmt, input_body);
3074 : 83 : gimple_omp_set_body (scan_stmt, scan_body);
3075 : :
3076 : 83 : omp_context *ctx = new_omp_context (input_stmt, outer_ctx);
3077 : 83 : scan_omp (gimple_omp_body_ptr (input_stmt), ctx);
3078 : :
3079 : 83 : ctx = new_omp_context (scan_stmt, outer_ctx);
3080 : 83 : scan_omp (gimple_omp_body_ptr (scan_stmt), ctx);
3081 : :
3082 : 83 : maybe_lookup_ctx (new_stmt)->for_simd_scan_phase = true;
3083 : 83 : }
3084 : :
3085 : : /* Scan an OpenMP sections directive. */
3086 : :
3087 : : static void
3088 : 581 : scan_omp_sections (gomp_sections *stmt, omp_context *outer_ctx)
3089 : : {
3090 : 581 : omp_context *ctx;
3091 : :
3092 : 581 : ctx = new_omp_context (stmt, outer_ctx);
3093 : 581 : scan_sharing_clauses (gimple_omp_sections_clauses (stmt), ctx);
3094 : 581 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
3095 : 581 : }
3096 : :
3097 : : /* Scan an OpenMP single directive. */
3098 : :
3099 : : static void
3100 : 1237 : scan_omp_single (gomp_single *stmt, omp_context *outer_ctx)
3101 : : {
3102 : 1237 : omp_context *ctx;
3103 : 1237 : tree name;
3104 : :
3105 : 1237 : ctx = new_omp_context (stmt, outer_ctx);
3106 : 1237 : ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3107 : 1237 : ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3108 : 1237 : name = create_tmp_var_name (".omp_copy_s");
3109 : 1237 : name = build_decl (gimple_location (stmt),
3110 : : TYPE_DECL, name, ctx->record_type);
3111 : 1237 : TYPE_NAME (ctx->record_type) = name;
3112 : :
3113 : 1237 : scan_sharing_clauses (gimple_omp_single_clauses (stmt), ctx);
3114 : 1237 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
3115 : :
3116 : 1237 : if (TYPE_FIELDS (ctx->record_type) == NULL)
3117 : 1090 : ctx->record_type = NULL;
3118 : : else
3119 : 147 : layout_type (ctx->record_type);
3120 : 1237 : }
3121 : :
3122 : : /* Scan a GIMPLE_OMP_TARGET. */
3123 : :
3124 : : static void
3125 : 39787 : scan_omp_target (gomp_target *stmt, omp_context *outer_ctx)
3126 : : {
3127 : 39787 : omp_context *ctx;
3128 : 39787 : tree name;
3129 : 39787 : bool offloaded = is_gimple_omp_offloaded (stmt);
3130 : 39787 : tree clauses = gimple_omp_target_clauses (stmt);
3131 : :
3132 : 39787 : ctx = new_omp_context (stmt, outer_ctx);
3133 : 39787 : ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3134 : 39787 : ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3135 : 39787 : name = create_tmp_var_name (".omp_data_t");
3136 : 39787 : name = build_decl (gimple_location (stmt),
3137 : : TYPE_DECL, name, ctx->record_type);
3138 : 39787 : DECL_ARTIFICIAL (name) = 1;
3139 : 39787 : DECL_NAMELESS (name) = 1;
3140 : 39787 : TYPE_NAME (ctx->record_type) = name;
3141 : 39787 : TYPE_ARTIFICIAL (ctx->record_type) = 1;
3142 : :
3143 : 39787 : if (offloaded)
3144 : : {
3145 : 23314 : create_omp_child_function (ctx, false);
3146 : 23314 : gimple_omp_target_set_child_fn (stmt, ctx->cb.dst_fn);
3147 : : }
3148 : :
3149 : 39787 : scan_sharing_clauses (clauses, ctx);
3150 : 39787 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
3151 : :
3152 : 39787 : if (TYPE_FIELDS (ctx->record_type) == NULL)
3153 : 7534 : ctx->record_type = ctx->receiver_decl = NULL;
3154 : : else
3155 : : {
3156 : 32253 : TYPE_FIELDS (ctx->record_type)
3157 : 32253 : = nreverse (TYPE_FIELDS (ctx->record_type));
3158 : 32253 : if (flag_checking)
3159 : : {
3160 : 32253 : unsigned int align = DECL_ALIGN (TYPE_FIELDS (ctx->record_type));
3161 : 32253 : for (tree field = TYPE_FIELDS (ctx->record_type);
3162 : 123406 : field;
3163 : 91153 : field = DECL_CHAIN (field))
3164 : 91153 : gcc_assert (DECL_ALIGN (field) == align);
3165 : : }
3166 : 32253 : layout_type (ctx->record_type);
3167 : 32253 : if (offloaded)
3168 : 16329 : fixup_child_record_type (ctx);
3169 : : }
3170 : :
3171 : 39787 : if (ctx->teams_nested_p && ctx->nonteams_nested_p)
3172 : : {
3173 : 4 : error_at (gimple_location (stmt),
3174 : : "%<target%> construct with nested %<teams%> construct "
3175 : : "contains directives outside of the %<teams%> construct");
3176 : 4 : gimple_omp_set_body (stmt, gimple_build_bind (NULL, NULL, NULL));
3177 : : }
3178 : 39787 : }
3179 : :
3180 : : /* Scan an OpenMP teams directive. */
3181 : :
3182 : : static void
3183 : 8722 : scan_omp_teams (gomp_teams *stmt, omp_context *outer_ctx)
3184 : : {
3185 : 8722 : omp_context *ctx = new_omp_context (stmt, outer_ctx);
3186 : :
3187 : 8722 : if (!gimple_omp_teams_host (stmt))
3188 : : {
3189 : 6065 : scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
3190 : 6065 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
3191 : 6065 : return;
3192 : : }
3193 : 2657 : taskreg_contexts.safe_push (ctx);
3194 : 2657 : gcc_assert (taskreg_nesting_level == 1);
3195 : 2657 : ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
3196 : 2657 : ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
3197 : 2657 : tree name = create_tmp_var_name (".omp_data_s");
3198 : 2657 : name = build_decl (gimple_location (stmt),
3199 : : TYPE_DECL, name, ctx->record_type);
3200 : 2657 : DECL_ARTIFICIAL (name) = 1;
3201 : 2657 : DECL_NAMELESS (name) = 1;
3202 : 2657 : TYPE_NAME (ctx->record_type) = name;
3203 : 2657 : TYPE_ARTIFICIAL (ctx->record_type) = 1;
3204 : 2657 : create_omp_child_function (ctx, false);
3205 : 2657 : gimple_omp_teams_set_child_fn (stmt, ctx->cb.dst_fn);
3206 : :
3207 : 2657 : scan_sharing_clauses (gimple_omp_teams_clauses (stmt), ctx);
3208 : 2657 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
3209 : :
3210 : 2657 : if (TYPE_FIELDS (ctx->record_type) == NULL)
3211 : 1444 : ctx->record_type = ctx->receiver_decl = NULL;
3212 : : }
3213 : :
3214 : : /* Check nesting restrictions. */
3215 : : static bool
3216 : 154848 : check_omp_nesting_restrictions (gimple *stmt, omp_context *ctx)
3217 : : {
3218 : 154848 : 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 : 154848 : if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3223 : 154848 : || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE)
3224 : : /* ..., except for the atomic codes that OpenACC shares with OpenMP. */
3225 : : ;
3226 : 277745 : else if (!(is_gimple_omp (stmt)
3227 : 136757 : && is_gimple_omp_oacc (stmt)))
3228 : : {
3229 : 110732 : if (oacc_get_fn_attrib (cfun->decl) != NULL)
3230 : : {
3231 : 48 : error_at (gimple_location (stmt),
3232 : : "non-OpenACC construct inside of OpenACC routine");
3233 : 48 : return false;
3234 : : }
3235 : : else
3236 : 233862 : for (omp_context *octx = ctx; octx != NULL; octx = octx->outer)
3237 : 246532 : if (is_gimple_omp (octx->stmt)
3238 : 123354 : && is_gimple_omp_oacc (octx->stmt))
3239 : : {
3240 : 176 : error_at (gimple_location (stmt),
3241 : : "non-OpenACC construct inside of OpenACC region");
3242 : 176 : return false;
3243 : : }
3244 : : }
3245 : :
3246 : 154624 : if (ctx != NULL)
3247 : : {
3248 : 84100 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
3249 : 84100 : && gimple_omp_target_kind (ctx->stmt) == GF_OMP_TARGET_KIND_REGION)
3250 : : {
3251 : 8250 : c = omp_find_clause (gimple_omp_target_clauses (ctx->stmt),
3252 : : OMP_CLAUSE_DEVICE);
3253 : 8250 : if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
3254 : : {
3255 : 9 : error_at (gimple_location (stmt),
3256 : : "OpenMP constructs are not allowed in target region "
3257 : : "with %<ancestor%>");
3258 : 9 : return false;
3259 : : }
3260 : :
3261 : 8241 : if (gimple_code (stmt) == GIMPLE_OMP_TEAMS && !ctx->teams_nested_p)
3262 : 6061 : ctx->teams_nested_p = true;
3263 : : else
3264 : 2180 : ctx->nonteams_nested_p = true;
3265 : : }
3266 : 84091 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN
3267 : 172 : && ctx->outer
3268 : 84263 : && gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
3269 : : ctx = ctx->outer;
3270 : 84091 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
3271 : 28317 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
3272 : 85888 : && !ctx->loop_p)
3273 : : {
3274 : 1581 : c = NULL_TREE;
3275 : 1581 : if (ctx->order_concurrent
3276 : 1581 : && (gimple_code (stmt) == GIMPLE_OMP_ORDERED
3277 : 280 : || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3278 : 190 : || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE))
3279 : : {
3280 : 210 : 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 : 210 : return false;
3285 : : }
3286 : 1371 : if (gimple_code (stmt) == GIMPLE_OMP_ORDERED)
3287 : : {
3288 : 133 : c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3289 : 133 : if (omp_find_clause (c, OMP_CLAUSE_SIMD))
3290 : : {
3291 : 133 : if (omp_find_clause (c, OMP_CLAUSE_THREADS)
3292 : 133 : && (ctx->outer == NULL
3293 : 29 : || !gimple_omp_for_combined_into_p (ctx->stmt)
3294 : 25 : || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR
3295 : 25 : || (gimple_omp_for_kind (ctx->outer->stmt)
3296 : : != GF_OMP_FOR_KIND_FOR)
3297 : 25 : || !gimple_omp_for_combined_p (ctx->outer->stmt)))
3298 : : {
3299 : 8 : error_at (gimple_location (stmt),
3300 : : "%<ordered simd threads%> must be closely "
3301 : : "nested inside of %<%s simd%> region",
3302 : 8 : lang_GNU_Fortran () ? "do" : "for");
3303 : 8 : return false;
3304 : : }
3305 : : return true;
3306 : : }
3307 : : }
3308 : 1238 : else if (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD
3309 : 1196 : || gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE
3310 : 2392 : || gimple_code (stmt) == GIMPLE_OMP_SCAN)
3311 : : return true;
3312 : 116 : else if (gimple_code (stmt) == GIMPLE_OMP_FOR
3313 : 116 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
3314 : : return true;
3315 : 60 : 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 : 60 : return false;
3320 : : }
3321 : 82510 : else if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS)
3322 : : {
3323 : 5828 : if ((gimple_code (stmt) != GIMPLE_OMP_FOR
3324 : 5524 : || (gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_DISTRIBUTE
3325 : 25 : && omp_find_clause (gimple_omp_for_clauses (stmt),
3326 : : OMP_CLAUSE_BIND) == NULL_TREE))
3327 : 5844 : && gimple_code (stmt) != GIMPLE_OMP_PARALLEL)
3328 : : {
3329 : 132 : error_at (gimple_location (stmt),
3330 : : "only %<distribute%>, %<parallel%> or %<loop%> "
3331 : : "regions are allowed to be strictly nested inside "
3332 : : "%<teams%> region");
3333 : 132 : return false;
3334 : : }
3335 : : }
3336 : 76682 : else if (ctx->order_concurrent
3337 : 1932 : && gimple_code (stmt) != GIMPLE_OMP_PARALLEL
3338 : 1462 : && (gimple_code (stmt) != GIMPLE_OMP_FOR
3339 : 1192 : || gimple_omp_for_kind (stmt) != GF_OMP_FOR_KIND_SIMD)
3340 : 76967 : && gimple_code (stmt) != GIMPLE_OMP_SCAN)
3341 : : {
3342 : 285 : if (ctx->loop_p)
3343 : 135 : 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 : 150 : 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 : 285 : return false;
3352 : : }
3353 : : }
3354 : 152617 : switch (gimple_code (stmt))
3355 : : {
3356 : 52765 : case GIMPLE_OMP_FOR:
3357 : 52765 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD)
3358 : : return true;
3359 : 42004 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
3360 : : {
3361 : 8212 : if (ctx != NULL && gimple_code (ctx->stmt) != GIMPLE_OMP_TEAMS)
3362 : : {
3363 : 0 : error_at (gimple_location (stmt),
3364 : : "%<distribute%> region must be strictly nested "
3365 : : "inside %<teams%> construct");
3366 : 0 : return false;
3367 : : }
3368 : : return true;
3369 : : }
3370 : : /* We split taskloop into task and nested taskloop in it. */
3371 : 33792 : 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 : 30632 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
3376 : 48737 : && omp_find_clause (gimple_omp_for_clauses (stmt), OMP_CLAUSE_BIND))
3377 : : return true;
3378 : 30002 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
3379 : : {
3380 : 12527 : bool ok = false;
3381 : :
3382 : 12527 : if (ctx)
3383 : 11979 : switch (gimple_code (ctx->stmt))
3384 : : {
3385 : 4051 : case GIMPLE_OMP_FOR:
3386 : 4051 : ok = (gimple_omp_for_kind (ctx->stmt)
3387 : : == GF_OMP_FOR_KIND_OACC_LOOP);
3388 : 4051 : break;
3389 : :
3390 : 7900 : case GIMPLE_OMP_TARGET:
3391 : 7900 : 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 : 548 : else if (oacc_get_fn_attrib (current_function_decl))
3409 : : ok = true;
3410 : 4051 : if (!ok)
3411 : : {
3412 : 65 : error_at (gimple_location (stmt),
3413 : : "OpenACC loop directive must be associated with"
3414 : : " an OpenACC compute region");
3415 : 65 : return false;
3416 : : }
3417 : : }
3418 : : /* FALLTHRU */
3419 : 34108 : case GIMPLE_CALL:
3420 : 34108 : if (is_gimple_call (stmt)
3421 : 34108 : && (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3422 : : == BUILT_IN_GOMP_CANCEL
3423 : 3067 : || DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3424 : : == BUILT_IN_GOMP_CANCELLATION_POINT))
3425 : : {
3426 : 1907 : const char *bad = NULL;
3427 : 1907 : const char *kind = NULL;
3428 : 1907 : const char *construct
3429 : 1907 : = (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3430 : : == BUILT_IN_GOMP_CANCEL)
3431 : 1907 : ? "cancel"
3432 : 1907 : : "cancellation point";
3433 : 1907 : if (ctx == NULL)
3434 : : {
3435 : 41 : error_at (gimple_location (stmt), "orphaned %qs construct",
3436 : : construct);
3437 : 41 : return false;
3438 : : }
3439 : 1866 : switch (tree_fits_shwi_p (gimple_call_arg (stmt, 0))
3440 : 1866 : ? tree_to_shwi (gimple_call_arg (stmt, 0))
3441 : : : 0)
3442 : : {
3443 : 528 : case 1:
3444 : 528 : if (gimple_code (ctx->stmt) != GIMPLE_OMP_PARALLEL)
3445 : : bad = "parallel";
3446 : 208 : else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3447 : : == BUILT_IN_GOMP_CANCEL
3448 : 208 : && !integer_zerop (gimple_call_arg (stmt, 1)))
3449 : 164 : ctx->cancellable = true;
3450 : 164 : kind = "parallel";
3451 : 164 : break;
3452 : 427 : case 2:
3453 : 427 : if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
3454 : 427 : || gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR)
3455 : : bad = "for";
3456 : 117 : else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3457 : : == BUILT_IN_GOMP_CANCEL
3458 : 117 : && !integer_zerop (gimple_call_arg (stmt, 1)))
3459 : : {
3460 : 106 : ctx->cancellable = true;
3461 : 106 : if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3462 : : OMP_CLAUSE_NOWAIT))
3463 : 5 : warning_at (gimple_location (stmt), OPT_Wopenmp,
3464 : : "%<cancel for%> inside "
3465 : : "%<nowait%> for construct");
3466 : 106 : if (omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3467 : : OMP_CLAUSE_ORDERED))
3468 : 5 : warning_at (gimple_location (stmt), OPT_Wopenmp,
3469 : : "%<cancel for%> inside "
3470 : : "%<ordered%> for construct");
3471 : : }
3472 : 5 : kind = "for";
3473 : 5 : break;
3474 : 383 : case 4:
3475 : 383 : if (gimple_code (ctx->stmt) != GIMPLE_OMP_SECTIONS
3476 : 383 : && gimple_code (ctx->stmt) != GIMPLE_OMP_SECTION)
3477 : : bad = "sections";
3478 : 93 : else if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3479 : : == BUILT_IN_GOMP_CANCEL
3480 : 93 : && !integer_zerop (gimple_call_arg (stmt, 1)))
3481 : : {
3482 : 73 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
3483 : : {
3484 : 0 : ctx->cancellable = true;
3485 : 0 : if (omp_find_clause (gimple_omp_sections_clauses
3486 : 0 : (ctx->stmt),
3487 : : OMP_CLAUSE_NOWAIT))
3488 : 0 : warning_at (gimple_location (stmt), OPT_Wopenmp,
3489 : : "%<cancel sections%> inside "
3490 : : "%<nowait%> sections construct");
3491 : : }
3492 : : else
3493 : : {
3494 : 73 : gcc_assert (ctx->outer
3495 : : && gimple_code (ctx->outer->stmt)
3496 : : == GIMPLE_OMP_SECTIONS);
3497 : 73 : ctx->outer->cancellable = true;
3498 : 73 : if (omp_find_clause (gimple_omp_sections_clauses
3499 : 73 : (ctx->outer->stmt),
3500 : : OMP_CLAUSE_NOWAIT))
3501 : 10 : warning_at (gimple_location (stmt), OPT_Wopenmp,
3502 : : "%<cancel sections%> inside "
3503 : : "%<nowait%> sections construct");
3504 : : }
3505 : : }
3506 : 10 : kind = "sections";
3507 : 10 : break;
3508 : 528 : case 8:
3509 : 528 : if (!is_task_ctx (ctx)
3510 : 528 : && (!is_taskloop_ctx (ctx)
3511 : 54 : || ctx->outer == NULL
3512 : 54 : || !is_task_ctx (ctx->outer)))
3513 : : bad = "task";
3514 : : else
3515 : : {
3516 : 248 : for (omp_context *octx = ctx->outer;
3517 : 439 : octx; octx = octx->outer)
3518 : : {
3519 : 425 : switch (gimple_code (octx->stmt))
3520 : : {
3521 : : case GIMPLE_OMP_TASKGROUP:
3522 : : break;
3523 : 40 : case GIMPLE_OMP_TARGET:
3524 : 40 : if (gimple_omp_target_kind (octx->stmt)
3525 : : != GF_OMP_TARGET_KIND_REGION)
3526 : 20 : continue;
3527 : : /* FALLTHRU */
3528 : 99 : case GIMPLE_OMP_PARALLEL:
3529 : 99 : case GIMPLE_OMP_TEAMS:
3530 : 99 : error_at (gimple_location (stmt),
3531 : : "%<%s taskgroup%> construct not closely "
3532 : : "nested inside of %<taskgroup%> region",
3533 : : construct);
3534 : 99 : return false;
3535 : 104 : case GIMPLE_OMP_TASK:
3536 : 104 : if (gimple_omp_task_taskloop_p (octx->stmt)
3537 : 94 : && octx->outer
3538 : 198 : && is_taskloop_ctx (octx->outer))
3539 : : {
3540 : 94 : tree clauses
3541 : 94 : = gimple_omp_for_clauses (octx->outer->stmt);
3542 : 94 : if (!omp_find_clause (clauses, OMP_CLAUSE_NOGROUP))
3543 : : break;
3544 : : }
3545 : 60 : continue;
3546 : 111 : default:
3547 : 111 : continue;
3548 : 171 : }
3549 : : break;
3550 : : }
3551 : 149 : ctx->cancellable = true;
3552 : : }
3553 : 149 : kind = "taskgroup";
3554 : 149 : break;
3555 : 0 : default:
3556 : 0 : error_at (gimple_location (stmt), "invalid arguments");
3557 : 0 : return false;
3558 : : }
3559 : 328 : if (bad)
3560 : : {
3561 : 1200 : error_at (gimple_location (stmt),
3562 : : "%<%s %s%> construct not closely nested inside of %qs",
3563 : : construct, kind, bad);
3564 : 1200 : return false;
3565 : : }
3566 : : }
3567 : : /* FALLTHRU */
3568 : : case GIMPLE_OMP_SECTIONS:
3569 : : case GIMPLE_OMP_SINGLE:
3570 : 55628 : for (; ctx != NULL; ctx = ctx->outer)
3571 : 37558 : switch (gimple_code (ctx->stmt))
3572 : : {
3573 : 6873 : case GIMPLE_OMP_FOR:
3574 : 6873 : if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3575 : 6873 : && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3576 : : break;
3577 : : /* FALLTHRU */
3578 : 775 : case GIMPLE_OMP_SECTIONS:
3579 : 775 : case GIMPLE_OMP_SINGLE:
3580 : 775 : case GIMPLE_OMP_ORDERED:
3581 : 775 : case GIMPLE_OMP_MASTER:
3582 : 775 : case GIMPLE_OMP_MASKED:
3583 : 775 : case GIMPLE_OMP_TASK:
3584 : 775 : case GIMPLE_OMP_CRITICAL:
3585 : 775 : if (is_gimple_call (stmt))
3586 : : {
3587 : 704 : if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt))
3588 : : != BUILT_IN_GOMP_BARRIER)
3589 : : return true;
3590 : 23 : 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 : 23 : return false;
3596 : : }
3597 : 71 : 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 : 71 : return false;
3603 : : case GIMPLE_OMP_PARALLEL:
3604 : : case GIMPLE_OMP_TEAMS:
3605 : : return true;
3606 : 13485 : case GIMPLE_OMP_TARGET:
3607 : 13485 : 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 : 1480 : for (; ctx != NULL; ctx = ctx->outer)
3618 : 983 : switch (gimple_code (ctx->stmt))
3619 : : {
3620 : 14 : case GIMPLE_OMP_FOR:
3621 : 14 : if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3622 : 14 : && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3623 : : break;
3624 : : /* FALLTHRU */
3625 : 30 : case GIMPLE_OMP_SECTIONS:
3626 : 30 : case GIMPLE_OMP_SINGLE:
3627 : 30 : case GIMPLE_OMP_TASK:
3628 : 60 : 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 : 30 : gimple_code (stmt) == GIMPLE_OMP_MASTER
3633 : : ? "master" : "masked");
3634 : 30 : return false;
3635 : : case GIMPLE_OMP_PARALLEL:
3636 : : case GIMPLE_OMP_TEAMS:
3637 : : return true;
3638 : 10 : case GIMPLE_OMP_TARGET:
3639 : 10 : 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 : 274 : for (; ctx != NULL; ctx = ctx->outer)
3649 : 175 : switch (gimple_code (ctx->stmt))
3650 : : {
3651 : 7 : case GIMPLE_OMP_FOR:
3652 : 7 : if (gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_FOR
3653 : 7 : && gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_TASKLOOP)
3654 : : break;
3655 : : /* FALLTHRU */
3656 : 25 : case GIMPLE_OMP_SECTIONS:
3657 : 25 : case GIMPLE_OMP_SINGLE:
3658 : 25 : case GIMPLE_OMP_TASK:
3659 : 25 : case GIMPLE_OMP_CRITICAL:
3660 : 25 : case GIMPLE_OMP_ORDERED:
3661 : 25 : case GIMPLE_OMP_MASTER:
3662 : 25 : case GIMPLE_OMP_MASKED:
3663 : 25 : 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 : 25 : return false;
3669 : : case GIMPLE_OMP_PARALLEL:
3670 : : case GIMPLE_OMP_TEAMS:
3671 : : return true;
3672 : 5 : case GIMPLE_OMP_TARGET:
3673 : 5 : 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 : 5547 : case GIMPLE_OMP_TASK:
3682 : 23197 : for (c = gimple_omp_task_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
3683 : 17654 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
3684 : : {
3685 : 4 : enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
3686 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
3687 : : "%<%s(%s)%> is only allowed in %<omp ordered%>",
3688 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross",
3689 : : kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
3690 : 4 : return false;
3691 : : }
3692 : : break;
3693 : 1621 : case GIMPLE_OMP_ORDERED:
3694 : 2853 : for (c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3695 : 2853 : c; c = OMP_CLAUSE_CHAIN (c))
3696 : : {
3697 : 1248 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS)
3698 : : {
3699 : 169 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
3700 : : {
3701 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
3702 : : "invalid depend kind in omp %<ordered%> %<depend%>");
3703 : 4 : return false;
3704 : : }
3705 : 165 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREADS
3706 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMD);
3707 : 165 : continue;
3708 : : }
3709 : :
3710 : 1079 : tree oclause;
3711 : : /* Look for containing ordered(N) loop. */
3712 : 1079 : if (ctx == NULL
3713 : 1067 : || gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
3714 : 1079 : || (oclause
3715 : 1067 : = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3716 : : OMP_CLAUSE_ORDERED)) == NULL_TREE)
3717 : : {
3718 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
3719 : : "%<ordered%> construct with %<depend%> clause "
3720 : : "must be closely nested inside an %<ordered%> loop");
3721 : 12 : return false;
3722 : : }
3723 : : }
3724 : 1605 : c = gimple_omp_ordered_clauses (as_a <gomp_ordered *> (stmt));
3725 : 1605 : 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 : 34 : if (ctx != NULL)
3733 : : {
3734 : 10 : error_at (gimple_location (stmt),
3735 : : "%<ordered%> %<simd%> must be closely nested inside "
3736 : : "%<simd%> region");
3737 : 10 : return false;
3738 : : }
3739 : : }
3740 : 1595 : for (; ctx != NULL; ctx = ctx->outer)
3741 : 1464 : switch (gimple_code (ctx->stmt))
3742 : : {
3743 : 38 : case GIMPLE_OMP_CRITICAL:
3744 : 38 : case GIMPLE_OMP_TASK:
3745 : 38 : case GIMPLE_OMP_ORDERED:
3746 : 38 : ordered_in_taskloop:
3747 : 38 : 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 : 38 : return false;
3752 : 1406 : case GIMPLE_OMP_FOR:
3753 : 1406 : if (gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_TASKLOOP)
3754 : 10 : goto ordered_in_taskloop;
3755 : 1396 : tree o;
3756 : 1396 : o = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3757 : : OMP_CLAUSE_ORDERED);
3758 : 1396 : if (o == NULL)
3759 : : {
3760 : 22 : error_at (gimple_location (stmt),
3761 : : "%<ordered%> region must be closely nested inside "
3762 : : "a loop region with an %<ordered%> clause");
3763 : 22 : return false;
3764 : : }
3765 : 1374 : if (!gimple_omp_ordered_standalone_p (stmt))
3766 : : {
3767 : 375 : if (OMP_CLAUSE_ORDERED_DOACROSS (o))
3768 : : {
3769 : 10 : 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 : 10 : return false;
3775 : : }
3776 : 365 : else if (OMP_CLAUSE_ORDERED_EXPR (o))
3777 : : {
3778 : 23 : tree co
3779 : 23 : = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
3780 : : OMP_CLAUSE_COLLAPSE);
3781 : 23 : HOST_WIDE_INT
3782 : 23 : o_n = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (o));
3783 : 23 : HOST_WIDE_INT c_n = 1;
3784 : 23 : if (co)
3785 : 5 : c_n = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (co));
3786 : 23 : if (o_n != c_n)
3787 : : {
3788 : 10 : 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 : 10 : return false;
3794 : : }
3795 : : }
3796 : : }
3797 : : return true;
3798 : 10 : case GIMPLE_OMP_TARGET:
3799 : 10 : if (gimple_omp_target_kind (ctx->stmt)
3800 : : != GF_OMP_TARGET_KIND_REGION)
3801 : : break;
3802 : : /* FALLTHRU */
3803 : 30 : case GIMPLE_OMP_PARALLEL:
3804 : 30 : case GIMPLE_OMP_TEAMS:
3805 : 30 : error_at (gimple_location (stmt),
3806 : : "%<ordered%> region must be closely nested inside "
3807 : : "a loop region with an %<ordered%> clause");
3808 : 30 : return false;
3809 : : default:
3810 : : break;
3811 : : }
3812 : : break;
3813 : 462 : case GIMPLE_OMP_CRITICAL:
3814 : 462 : {
3815 : 462 : tree this_stmt_name
3816 : 462 : = gimple_omp_critical_name (as_a <gomp_critical *> (stmt));
3817 : 888 : for (; ctx != NULL; ctx = ctx->outer)
3818 : 432 : if (gomp_critical *other_crit
3819 : 451 : = dyn_cast <gomp_critical *> (ctx->stmt))
3820 : 25 : if (this_stmt_name == gimple_omp_critical_name (other_crit))
3821 : : {
3822 : 6 : error_at (gimple_location (stmt),
3823 : : "%<critical%> region may not be nested inside "
3824 : : "a %<critical%> region with the same name");
3825 : 6 : return false;
3826 : : }
3827 : : }
3828 : : break;
3829 : 8766 : case GIMPLE_OMP_TEAMS:
3830 : 8766 : if (ctx == NULL)
3831 : : break;
3832 : 6109 : else if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET
3833 : 6109 : || (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 : 44 : 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 : 44 : return false;
3845 : : }
3846 : : break;
3847 : 40170 : case GIMPLE_OMP_TARGET:
3848 : 177907 : for (c = gimple_omp_target_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
3849 : 137741 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS)
3850 : : {
3851 : 4 : enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_KIND (c);
3852 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
3853 : : "%<depend(%s)%> is only allowed in %<omp ordered%>",
3854 : : kind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
3855 : 4 : return false;
3856 : : }
3857 : 40166 : if (is_gimple_omp_offloaded (stmt)
3858 : 40166 : && oacc_get_fn_attrib (cfun->decl) != NULL)
3859 : : {
3860 : 4 : error_at (gimple_location (stmt),
3861 : : "OpenACC region inside of OpenACC routine, nested "
3862 : : "parallelism not supported yet");
3863 : 4 : return false;
3864 : : }
3865 : 47610 : for (; ctx != NULL; ctx = ctx->outer)
3866 : : {
3867 : 7854 : if (gimple_code (ctx->stmt) != GIMPLE_OMP_TARGET)
3868 : : {
3869 : 756 : if (is_gimple_omp (stmt)
3870 : 756 : && is_gimple_omp_oacc (stmt)
3871 : 242 : && is_gimple_omp (ctx->stmt))
3872 : : {
3873 : 242 : error_at (gimple_location (stmt),
3874 : : "OpenACC construct inside of non-OpenACC region");
3875 : 242 : return false;
3876 : : }
3877 : 514 : continue;
3878 : : }
3879 : :
3880 : 7098 : const char *stmt_name, *ctx_stmt_name;
3881 : 7098 : switch (gimple_omp_target_kind (stmt))
3882 : : {
3883 : : case GF_OMP_TARGET_KIND_REGION: stmt_name = "target"; break;
3884 : 399 : case GF_OMP_TARGET_KIND_DATA: stmt_name = "target data"; break;
3885 : 1688 : case GF_OMP_TARGET_KIND_UPDATE: stmt_name = "target update"; break;
3886 : 12 : case GF_OMP_TARGET_KIND_ENTER_DATA:
3887 : 12 : stmt_name = "target enter data"; break;
3888 : 18 : case GF_OMP_TARGET_KIND_EXIT_DATA:
3889 : 18 : stmt_name = "target exit data"; break;
3890 : 1442 : case GF_OMP_TARGET_KIND_OACC_PARALLEL: stmt_name = "parallel"; break;
3891 : 1684 : case GF_OMP_TARGET_KIND_OACC_KERNELS: stmt_name = "kernels"; break;
3892 : 180 : case GF_OMP_TARGET_KIND_OACC_SERIAL: stmt_name = "serial"; break;
3893 : 386 : case GF_OMP_TARGET_KIND_OACC_DATA: stmt_name = "data"; break;
3894 : 256 : case GF_OMP_TARGET_KIND_OACC_UPDATE: stmt_name = "update"; break;
3895 : 95 : case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
3896 : 95 : stmt_name = "enter data"; break;
3897 : 87 : case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
3898 : 87 : stmt_name = "exit data"; break;
3899 : 64 : case GF_OMP_TARGET_KIND_OACC_DECLARE: stmt_name = "declare"; break;
3900 : 128 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA: stmt_name = "host_data";
3901 : 128 : 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 : 1684 : stmt_name = "kernels"; break;
3907 : 0 : default: gcc_unreachable ();
3908 : : }
3909 : 7098 : switch (gimple_omp_target_kind (ctx->stmt))
3910 : : {
3911 : : case GF_OMP_TARGET_KIND_REGION: ctx_stmt_name = "target"; break;
3912 : 2696 : case GF_OMP_TARGET_KIND_DATA: ctx_stmt_name = "target data"; break;
3913 : 35 : case GF_OMP_TARGET_KIND_OACC_PARALLEL:
3914 : 35 : ctx_stmt_name = "parallel"; break;
3915 : : case GF_OMP_TARGET_KIND_OACC_KERNELS:
3916 : 1100 : ctx_stmt_name = "kernels"; break;
3917 : 35 : case GF_OMP_TARGET_KIND_OACC_SERIAL:
3918 : 35 : ctx_stmt_name = "serial"; break;
3919 : 3116 : case GF_OMP_TARGET_KIND_OACC_DATA: ctx_stmt_name = "data"; break;
3920 : 8 : case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
3921 : 8 : 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 : 1100 : ctx_stmt_name = "kernels"; break;
3927 : 0 : default: gcc_unreachable ();
3928 : : }
3929 : :
3930 : : /* OpenACC/OpenMP mismatch? */
3931 : 14196 : if (is_gimple_omp_oacc (stmt)
3932 : 7098 : != is_gimple_omp_oacc (ctx->stmt))
3933 : : {
3934 : 56 : 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 : 28 : return false;
3941 : : }
3942 : 7070 : if (is_gimple_omp_offloaded (ctx->stmt))
3943 : : {
3944 : : /* No GIMPLE_OMP_TARGET inside offloaded OpenACC CTX. */
3945 : 185 : if (is_gimple_omp_oacc (ctx->stmt))
3946 : : {
3947 : 105 : error_at (gimple_location (stmt),
3948 : : "%qs construct inside of %qs region",
3949 : : stmt_name, ctx_stmt_name);
3950 : 105 : return false;
3951 : : }
3952 : : else
3953 : : {
3954 : 80 : if ((gimple_omp_target_kind (ctx->stmt)
3955 : : == GF_OMP_TARGET_KIND_REGION)
3956 : 80 : && (gimple_omp_target_kind (stmt)
3957 : : == GF_OMP_TARGET_KIND_REGION))
3958 : : {
3959 : 58 : c = omp_find_clause (gimple_omp_target_clauses (stmt),
3960 : : OMP_CLAUSE_DEVICE);
3961 : 58 : if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
3962 : : break;
3963 : : }
3964 : 49 : 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 : 8579418 : scan_omp_1_op (tree *tp, int *walk_subtrees, void *data)
3985 : : {
3986 : 8579418 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
3987 : 8579418 : omp_context *ctx = (omp_context *) wi->info;
3988 : 8579418 : tree t = *tp;
3989 : 8579418 : tree tmp;
3990 : :
3991 : 8579418 : switch (TREE_CODE (t))
3992 : : {
3993 : 3794967 : case VAR_DECL:
3994 : 3794967 : case PARM_DECL:
3995 : 3794967 : case LABEL_DECL:
3996 : 3794967 : case RESULT_DECL:
3997 : 3794967 : if (ctx)
3998 : : {
3999 : 2027031 : tmp = NULL_TREE;
4000 : 2027031 : if (TREE_CODE (t) == VAR_DECL
4001 : 3628856 : && (tmp = lookup_attribute ("omp allocate var",
4002 : 1601825 : DECL_ATTRIBUTES (t))) != NULL_TREE)
4003 : 61 : t = TREE_VALUE (TREE_VALUE (tmp));
4004 : 2027031 : tree repl = remap_decl (t, &ctx->cb);
4005 : 2027031 : gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
4006 : 2027031 : if (tmp != NULL_TREE && t != repl)
4007 : 31 : *tp = build_fold_addr_expr (repl);
4008 : 2027000 : else if (tmp == NULL_TREE)
4009 : 2026970 : *tp = repl;
4010 : : }
4011 : : break;
4012 : :
4013 : 156046 : case INDIRECT_REF:
4014 : 156046 : case MEM_REF:
4015 : 156046 : if (ctx
4016 : 73180 : && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
4017 : 210190 : && ((tmp = lookup_attribute ("omp allocate var",
4018 : 54144 : DECL_ATTRIBUTES (TREE_OPERAND (t, 0))))
4019 : : != NULL_TREE))
4020 : : {
4021 : 127 : tmp = TREE_VALUE (TREE_VALUE (tmp));
4022 : 127 : tree repl = remap_decl (tmp, &ctx->cb);
4023 : 127 : gcc_checking_assert (TREE_CODE (repl) != ERROR_MARK);
4024 : 127 : if (tmp != repl)
4025 : 118 : *tp = repl;
4026 : : break;
4027 : : }
4028 : 4784324 : gcc_fallthrough ();
4029 : :
4030 : 4784324 : default:
4031 : 4784324 : if (ctx && TYPE_P (t))
4032 : 0 : *tp = remap_type (t, &ctx->cb);
4033 : 4784324 : else if (!DECL_P (t))
4034 : : {
4035 : 4010958 : *walk_subtrees = 1;
4036 : 4010958 : if (ctx)
4037 : : {
4038 : 1345371 : tree tem = remap_type (TREE_TYPE (t), &ctx->cb);
4039 : 1345371 : if (tem != TREE_TYPE (t))
4040 : : {
4041 : 9690 : if (TREE_CODE (t) == INTEGER_CST)
4042 : 4060 : *tp = wide_int_to_tree (tem, wi::to_wide (t));
4043 : : else
4044 : 5630 : TREE_TYPE (t) = tem;
4045 : : }
4046 : : }
4047 : : }
4048 : : break;
4049 : : }
4050 : :
4051 : 8579418 : return NULL_TREE;
4052 : : }
4053 : :
4054 : : /* Return true if FNDECL is a setjmp or a longjmp. */
4055 : :
4056 : : static bool
4057 : 3045 : setjmp_or_longjmp_p (const_tree fndecl)
4058 : : {
4059 : 3045 : if (fndecl_built_in_p (fndecl, BUILT_IN_SETJMP, BUILT_IN_LONGJMP))
4060 : : return true;
4061 : :
4062 : 3045 : tree declname = DECL_NAME (fndecl);
4063 : 3045 : if (!declname
4064 : 3045 : || (DECL_CONTEXT (fndecl) != NULL_TREE
4065 : 2350 : && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
4066 : 5834 : || !TREE_PUBLIC (fndecl))
4067 : : return false;
4068 : :
4069 : 2627 : const char *name = IDENTIFIER_POINTER (declname);
4070 : 2627 : 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 : 3049688 : scan_omp_1_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
4080 : : struct walk_stmt_info *wi)
4081 : : {
4082 : 3049688 : gimple *stmt = gsi_stmt (*gsi);
4083 : 3049688 : omp_context *ctx = (omp_context *) wi->info;
4084 : :
4085 : 3049688 : if (gimple_has_location (stmt))
4086 : 2486136 : input_location = gimple_location (stmt);
4087 : :
4088 : : /* Check the nesting restrictions. */
4089 : 3049688 : bool remove = false;
4090 : 3049688 : if (is_gimple_omp (stmt))
4091 : 150617 : remove = !check_omp_nesting_restrictions (stmt, ctx);
4092 : 2899071 : else if (is_gimple_call (stmt))
4093 : : {
4094 : 194692 : tree fndecl = gimple_call_fndecl (stmt);
4095 : 194692 : if (fndecl)
4096 : : {
4097 : 183885 : if (ctx
4098 : 65025 : && gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
4099 : 14600 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD
4100 : 3045 : && setjmp_or_longjmp_p (fndecl)
4101 : 183893 : && !ctx->loop_p)
4102 : : {
4103 : 4 : remove = true;
4104 : 4 : error_at (gimple_location (stmt),
4105 : : "setjmp/longjmp inside %<simd%> construct");
4106 : : }
4107 : 183881 : else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4108 : 53626 : switch (DECL_FUNCTION_CODE (fndecl))
4109 : : {
4110 : 4231 : case BUILT_IN_GOMP_BARRIER:
4111 : 4231 : case BUILT_IN_GOMP_CANCEL:
4112 : 4231 : case BUILT_IN_GOMP_CANCELLATION_POINT:
4113 : 4231 : case BUILT_IN_GOMP_TASKYIELD:
4114 : 4231 : case BUILT_IN_GOMP_TASKWAIT:
4115 : 4231 : case BUILT_IN_GOMP_TASKGROUP_START:
4116 : 4231 : case BUILT_IN_GOMP_TASKGROUP_END:
4117 : 4231 : remove = !check_omp_nesting_restrictions (stmt, ctx);
4118 : 4231 : break;
4119 : : default:
4120 : : break;
4121 : : }
4122 : 130255 : else if (ctx)
4123 : : {
4124 : 43848 : omp_context *octx = ctx;
4125 : 43848 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_SCAN && ctx->outer)
4126 : 43848 : octx = ctx->outer;
4127 : 43848 : if (octx->order_concurrent && omp_runtime_api_call (fndecl))
4128 : : {
4129 : 240 : remove = true;
4130 : 240 : error_at (gimple_location (stmt),
4131 : : "OpenMP runtime API call %qD in a region with "
4132 : : "%<order(concurrent)%> clause", fndecl);
4133 : : }
4134 : 43848 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
4135 : 3274 : && omp_runtime_api_call (fndecl)
4136 : 309 : && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
4137 : : != strlen ("omp_get_num_teams"))
4138 : 178 : || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4139 : : "omp_get_num_teams") != 0)
4140 : 43994 : && ((IDENTIFIER_LENGTH (DECL_NAME (fndecl))
4141 : : != strlen ("omp_get_team_num"))
4142 : 119 : || strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4143 : : "omp_get_team_num") != 0))
4144 : : {
4145 : 27 : remove = true;
4146 : 27 : error_at (gimple_location (stmt),
4147 : : "OpenMP runtime API call %qD strictly nested in a "
4148 : : "%<teams%> region", fndecl);
4149 : : }
4150 : 43848 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TARGET
4151 : 11263 : && (gimple_omp_target_kind (ctx->stmt)
4152 : : == GF_OMP_TARGET_KIND_REGION)
4153 : 46843 : && omp_runtime_api_call (fndecl))
4154 : : {
4155 : 351 : tree tgt_clauses = gimple_omp_target_clauses (ctx->stmt);
4156 : 351 : tree c = omp_find_clause (tgt_clauses, OMP_CLAUSE_DEVICE);
4157 : 351 : if (c && OMP_CLAUSE_DEVICE_ANCESTOR (c))
4158 : 5 : 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 : 198700 : if (remove)
4166 : : {
4167 : 3326 : stmt = gimple_build_nop ();
4168 : 3326 : gsi_replace (gsi, stmt, false);
4169 : : }
4170 : :
4171 : 3049688 : *handled_ops_p = true;
4172 : :
4173 : 3049688 : switch (gimple_code (stmt))
4174 : : {
4175 : 18127 : case GIMPLE_OMP_PARALLEL:
4176 : 18127 : taskreg_nesting_level++;
4177 : 18127 : scan_omp_parallel (gsi, ctx);
4178 : 18127 : taskreg_nesting_level--;
4179 : 18127 : break;
4180 : :
4181 : 5543 : case GIMPLE_OMP_TASK:
4182 : 5543 : taskreg_nesting_level++;
4183 : 5543 : scan_omp_task (gsi, ctx);
4184 : 5543 : taskreg_nesting_level--;
4185 : 5543 : break;
4186 : :
4187 : 52729 : case GIMPLE_OMP_FOR:
4188 : 52729 : if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
4189 : : == GF_OMP_FOR_KIND_SIMD)
4190 : 10813 : && gimple_omp_for_combined_into_p (stmt)
4191 : 60416 : && gimple_code (ctx->stmt) != GIMPLE_OMP_SCAN)
4192 : : {
4193 : 7521 : tree clauses = gimple_omp_for_clauses (as_a <gomp_for *> (stmt));
4194 : 7521 : tree c = omp_find_clause (clauses, OMP_CLAUSE_REDUCTION);
4195 : 7521 : if (c && OMP_CLAUSE_REDUCTION_INSCAN (c) && !seen_error ())
4196 : : {
4197 : 83 : scan_omp_simd_scan (gsi, as_a <gomp_for *> (stmt), ctx);
4198 : 83 : break;
4199 : : }
4200 : : }
4201 : 52646 : if ((gimple_omp_for_kind (as_a <gomp_for *> (stmt))
4202 : : == GF_OMP_FOR_KIND_SIMD)
4203 : 10730 : && omp_maybe_offloaded_ctx (ctx)
4204 : 3650 : && omp_max_simt_vf ()
4205 : 52646 : && gimple_omp_for_collapse (stmt) == 1)
4206 : 0 : scan_omp_simd (gsi, as_a <gomp_for *> (stmt), ctx);
4207 : : else
4208 : 52646 : scan_omp_for (as_a <gomp_for *> (stmt), ctx);
4209 : : break;
4210 : :
4211 : 184 : case GIMPLE_OMP_SCOPE:
4212 : 184 : ctx = new_omp_context (stmt, ctx);
4213 : 184 : scan_sharing_clauses (gimple_omp_scope_clauses (stmt), ctx);
4214 : 184 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
4215 : 184 : break;
4216 : :
4217 : 819 : case GIMPLE_OMP_DISPATCH:
4218 : 819 : ctx = new_omp_context (stmt, ctx);
4219 : 819 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
4220 : 819 : break;
4221 : :
4222 : 559 : case GIMPLE_OMP_INTEROP:
4223 : 559 : ctx = new_omp_context (stmt, ctx);
4224 : 559 : break;
4225 : :
4226 : 581 : case GIMPLE_OMP_SECTIONS:
4227 : 581 : scan_omp_sections (as_a <gomp_sections *> (stmt), ctx);
4228 : 581 : break;
4229 : :
4230 : 1237 : case GIMPLE_OMP_SINGLE:
4231 : 1237 : scan_omp_single (as_a <gomp_single *> (stmt), ctx);
4232 : 1237 : break;
4233 : :
4234 : 1284 : case GIMPLE_OMP_SCAN:
4235 : 1284 : if (tree clauses = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)))
4236 : : {
4237 : 606 : if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_INCLUSIVE)
4238 : 304 : ctx->scan_inclusive = true;
4239 : 302 : else if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_EXCLUSIVE)
4240 : 302 : ctx->scan_exclusive = true;
4241 : : }
4242 : : /* FALLTHRU */
4243 : 6172 : case GIMPLE_OMP_SECTION:
4244 : 6172 : case GIMPLE_OMP_STRUCTURED_BLOCK:
4245 : 6172 : case GIMPLE_OMP_MASTER:
4246 : 6172 : case GIMPLE_OMP_ORDERED:
4247 : 6172 : case GIMPLE_OMP_CRITICAL:
4248 : 6172 : ctx = new_omp_context (stmt, ctx);
4249 : 6172 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
4250 : 6172 : break;
4251 : :
4252 : 426 : case GIMPLE_OMP_MASKED:
4253 : 426 : ctx = new_omp_context (stmt, ctx);
4254 : 426 : scan_sharing_clauses (gimple_omp_masked_clauses (stmt), ctx);
4255 : 426 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
4256 : 426 : break;
4257 : :
4258 : 607 : case GIMPLE_OMP_TASKGROUP:
4259 : 607 : ctx = new_omp_context (stmt, ctx);
4260 : 607 : scan_sharing_clauses (gimple_omp_taskgroup_clauses (stmt), ctx);
4261 : 607 : scan_omp (gimple_omp_body_ptr (stmt), ctx);
4262 : 607 : break;
4263 : :
4264 : 39787 : case GIMPLE_OMP_TARGET:
4265 : 39787 : if (is_gimple_omp_offloaded (stmt))
4266 : : {
4267 : 23314 : taskreg_nesting_level++;
4268 : 23314 : scan_omp_target (as_a <gomp_target *> (stmt), ctx);
4269 : 23314 : taskreg_nesting_level--;
4270 : : }
4271 : : else
4272 : 16473 : scan_omp_target (as_a <gomp_target *> (stmt), ctx);
4273 : : break;
4274 : :
4275 : 8722 : case GIMPLE_OMP_TEAMS:
4276 : 8722 : if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
4277 : : {
4278 : 2657 : taskreg_nesting_level++;
4279 : 2657 : scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
4280 : 2657 : taskreg_nesting_level--;
4281 : : }
4282 : : else
4283 : 6065 : scan_omp_teams (as_a <gomp_teams *> (stmt), ctx);
4284 : : break;
4285 : :
4286 : 256000 : case GIMPLE_BIND:
4287 : 256000 : {
4288 : 256000 : tree var;
4289 : :
4290 : 256000 : *handled_ops_p = false;
4291 : 256000 : if (ctx)
4292 : 147513 : for (var = gimple_bind_vars (as_a <gbind *> (stmt));
4293 : 552807 : var ;
4294 : 405294 : var = DECL_CHAIN (var))
4295 : 405294 : insert_decl_map (&ctx->cb, var, var);
4296 : : }
4297 : : break;
4298 : 2658195 : default:
4299 : 2658195 : *handled_ops_p = false;
4300 : 2658195 : break;
4301 : : }
4302 : :
4303 : 3049688 : 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 : 245366 : scan_omp (gimple_seq *body_p, omp_context *ctx)
4313 : : {
4314 : 245366 : location_t saved_location;
4315 : 245366 : struct walk_stmt_info wi;
4316 : :
4317 : 245366 : memset (&wi, 0, sizeof (wi));
4318 : 245366 : wi.info = ctx;
4319 : 245366 : wi.want_locations = true;
4320 : :
4321 : 245366 : saved_location = input_location;
4322 : 245366 : walk_gimple_seq_mod (body_p, scan_omp_1_stmt, scan_omp_1_op, &wi);
4323 : 245366 : input_location = saved_location;
4324 : 245366 : }
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 : 248441 : maybe_remove_omp_member_access_dummy_vars (gbind *bind)
4333 : : {
4334 : 248441 : if (DECL_ARGUMENTS (current_function_decl)
4335 : 95046 : && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
4336 : 258351 : && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
4337 : : == POINTER_TYPE))
4338 : : {
4339 : 3445 : tree vars = gimple_bind_vars (bind);
4340 : 19190 : for (tree *pvar = &vars; *pvar; )
4341 : 15745 : if (omp_member_access_dummy_var (*pvar))
4342 : 713 : *pvar = DECL_CHAIN (*pvar);
4343 : : else
4344 : 15032 : pvar = &DECL_CHAIN (*pvar);
4345 : 3445 : gimple_bind_set_vars (bind, vars);
4346 : : }
4347 : 248441 : }
4348 : :
4349 : : /* Remove omp_member_access_dummy_var variables from BLOCK_VARS of
4350 : : block and its subblocks. */
4351 : :
4352 : : static void
4353 : 6993 : remove_member_access_dummy_vars (tree block)
4354 : : {
4355 : 14433 : for (tree *pvar = &BLOCK_VARS (block); *pvar; )
4356 : 7440 : if (omp_member_access_dummy_var (*pvar))
4357 : 108 : *pvar = DECL_CHAIN (*pvar);
4358 : : else
4359 : 7332 : pvar = &DECL_CHAIN (*pvar);
4360 : :
4361 : 10121 : for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
4362 : 3128 : remove_member_access_dummy_vars (block);
4363 : 6993 : }
4364 : :
4365 : : /* If a context was created for STMT when it was scanned, return it. */
4366 : :
4367 : : static omp_context *
4368 : 119336 : maybe_lookup_ctx (gimple *stmt)
4369 : : {
4370 : 119336 : splay_tree_node n;
4371 : 119336 : n = splay_tree_lookup (all_contexts, (splay_tree_key) stmt);
4372 : 119336 : 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 : 123778 : lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
4427 : : {
4428 : 123778 : tree t;
4429 : 123778 : omp_context *up;
4430 : :
4431 : 180602 : for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
4432 : 56824 : t = maybe_lookup_decl (decl, up);
4433 : :
4434 : 123778 : gcc_assert (!ctx->is_nested || t || is_global_var (decl));
4435 : :
4436 : 95478 : 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 : 353416 : maybe_lookup_decl_in_outer_ctx (tree decl, omp_context *ctx)
4445 : : {
4446 : 353416 : tree t = NULL;
4447 : 353416 : omp_context *up;
4448 : :
4449 : 624733 : for (up = ctx->outer, t = NULL; up && t == NULL; up = up->outer)
4450 : 271317 : t = maybe_lookup_decl (decl, up);
4451 : :
4452 : 353416 : return t ? t : decl;
4453 : : }
4454 : :
4455 : :
4456 : : /* Construct the initialization value for reduction operation OP. */
4457 : :
4458 : : tree
4459 : 13136 : omp_reduction_init_op (location_t loc, enum tree_code op, tree type)
4460 : : {
4461 : 13136 : switch (op)
4462 : : {
4463 : 10801 : case PLUS_EXPR:
4464 : 10801 : case MINUS_EXPR:
4465 : 10801 : case BIT_IOR_EXPR:
4466 : 10801 : case BIT_XOR_EXPR:
4467 : 10801 : case TRUTH_OR_EXPR:
4468 : 10801 : case TRUTH_ORIF_EXPR:
4469 : 10801 : case TRUTH_XOR_EXPR:
4470 : 10801 : case NE_EXPR:
4471 : 10801 : return build_zero_cst (type);
4472 : :
4473 : 1431 : case MULT_EXPR:
4474 : 1431 : case TRUTH_AND_EXPR:
4475 : 1431 : case TRUTH_ANDIF_EXPR:
4476 : 1431 : case EQ_EXPR:
4477 : 1431 : return fold_convert_loc (loc, type, integer_one_node);
4478 : :
4479 : 208 : case BIT_AND_EXPR:
4480 : 208 : return fold_convert_loc (loc, type, integer_minus_one_node);
4481 : :
4482 : 388 : case MAX_EXPR:
4483 : 388 : if (SCALAR_FLOAT_TYPE_P (type))
4484 : : {
4485 : 158 : REAL_VALUE_TYPE min;
4486 : 158 : if (HONOR_INFINITIES (type))
4487 : 158 : real_arithmetic (&min, NEGATE_EXPR, &dconstinf, NULL);
4488 : : else
4489 : 0 : real_maxval (&min, 1, TYPE_MODE (type));
4490 : 158 : return build_real (type, min);
4491 : : }
4492 : 230 : else if (POINTER_TYPE_P (type))
4493 : : {
4494 : 2 : wide_int min
4495 : 2 : = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4496 : 2 : return wide_int_to_tree (type, min);
4497 : 2 : }
4498 : : else
4499 : : {
4500 : 228 : gcc_assert (INTEGRAL_TYPE_P (type));
4501 : 228 : return TYPE_MIN_VALUE (type);
4502 : : }
4503 : :
4504 : 308 : case MIN_EXPR:
4505 : 308 : if (SCALAR_FLOAT_TYPE_P (type))
4506 : : {
4507 : 108 : REAL_VALUE_TYPE max;
4508 : 108 : if (HONOR_INFINITIES (type))
4509 : 108 : max = dconstinf;
4510 : : else
4511 : 0 : real_maxval (&max, 0, TYPE_MODE (type));
4512 : 108 : return build_real (type, max);
4513 : : }
4514 : 200 : else if (POINTER_TYPE_P (type))
4515 : : {
4516 : 2 : wide_int max
4517 : 2 : = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4518 : 2 : return wide_int_to_tree (type, max);
4519 : 2 : }
4520 : : else
4521 : : {
4522 : 198 : gcc_assert (INTEGRAL_TYPE_P (type));
4523 : 198 : return TYPE_MAX_VALUE (type);
4524 : : }
4525 : :
4526 : 0 : default:
4527 : 0 : gcc_unreachable ();
4528 : : }
4529 : : }
4530 : :
4531 : : /* Construct the initialization value for reduction CLAUSE. */
4532 : :
4533 : : tree
4534 : 10504 : omp_reduction_init (tree clause, tree type)
4535 : : {
4536 : 10504 : return omp_reduction_init_op (OMP_CLAUSE_LOCATION (clause),
4537 : 10504 : 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 : 140 : omp_clause_aligned_alignment (tree clause)
4545 : : {
4546 : 140 : if (OMP_CLAUSE_ALIGNED_ALIGNMENT (clause))
4547 : 123 : return OMP_CLAUSE_ALIGNED_ALIGNMENT (clause);
4548 : :
4549 : : /* Otherwise return implementation defined alignment. */
4550 : 17 : unsigned int al = 1;
4551 : 17 : opt_scalar_mode mode_iter;
4552 : 17 : auto_vector_modes modes;
4553 : 17 : targetm.vectorize.autovectorize_vector_modes (&modes, true);
4554 : 17 : static enum mode_class classes[]
4555 : : = { MODE_INT, MODE_VECTOR_INT, MODE_FLOAT, MODE_VECTOR_FLOAT };
4556 : 51 : for (int i = 0; i < 4; i += 2)
4557 : : /* The for loop above dictates that we only walk through scalar classes. */
4558 : 255 : FOR_EACH_MODE_IN_CLASS (mode_iter, classes[i])
4559 : : {
4560 : 221 : scalar_mode mode = mode_iter.require ();
4561 : 221 : machine_mode vmode = targetm.vectorize.preferred_simd_mode (mode);
4562 : 221 : if (GET_MODE_CLASS (vmode) != classes[i + 1])
4563 : 323 : continue;
4564 : : machine_mode alt_vmode;
4565 : 476 : for (unsigned int j = 0; j < modes.length (); ++j)
4566 : 527 : if (related_vector_mode (modes[j], mode).exists (&alt_vmode)
4567 : 578 : && known_ge (GET_MODE_SIZE (alt_vmode), GET_MODE_SIZE (vmode)))
4568 : 119 : vmode = alt_vmode;
4569 : :
4570 : 119 : tree type = lang_hooks.types.type_for_mode (mode, 1);
4571 : 119 : if (type == NULL_TREE || TYPE_MODE (type) != mode)
4572 : 17 : continue;
4573 : 102 : type = build_vector_type_for_mode (type, vmode);
4574 : 102 : if (TYPE_MODE (type) != vmode)
4575 : 0 : continue;
4576 : 102 : if (TYPE_ALIGN_UNIT (type) > al)
4577 : 17 : al = TYPE_ALIGN_UNIT (type);
4578 : : }
4579 : 17 : return build_int_cst (integer_type_node, al);
4580 : 17 : }
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 : 77163 : 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 : 10223 : 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 : 10223 : if (known_eq (sctx->max_vf, 0U))
4608 : : {
4609 : 4788 : sctx->max_vf = (sctx->is_simt ? omp_max_simt_vf ()
4610 : 4788 : : omp_max_vf (omp_maybe_offloaded_ctx (ctx)));
4611 : 4788 : if (maybe_gt (sctx->max_vf, 1U))
4612 : : {
4613 : 3461 : tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
4614 : : OMP_CLAUSE_SAFELEN);
4615 : 3461 : if (c)
4616 : : {
4617 : 89 : poly_uint64 safe_len;
4618 : 89 : if (!poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
4619 : 89 : || maybe_lt (safe_len, 1U))
4620 : 6 : sctx->max_vf = 1;
4621 : : else
4622 : 83 : sctx->max_vf = lower_bound (sctx->max_vf, safe_len);
4623 : : }
4624 : : }
4625 : 4788 : if (sctx->is_simt && !known_eq (sctx->max_vf, 1U))
4626 : : {
4627 : 0 : for (tree c = gimple_omp_for_clauses (ctx->stmt); c;
4628 : 0 : c = OMP_CLAUSE_CHAIN (c))
4629 : : {
4630 : 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4631 : 0 : continue;
4632 : :
4633 : 0 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4634 : : {
4635 : : /* UDR reductions are not supported yet for SIMT, disable
4636 : : SIMT. */
4637 : 0 : sctx->max_vf = 1;
4638 : 0 : break;
4639 : : }
4640 : :
4641 : 0 : if (truth_value_p (OMP_CLAUSE_REDUCTION_CODE (c))
4642 : 0 : && !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 : 0 : sctx->max_vf = 1;
4648 : 0 : break;
4649 : : }
4650 : : }
4651 : : }
4652 : 4788 : if (maybe_gt (sctx->max_vf, 1U))
4653 : : {
4654 : 3442 : sctx->idx = create_tmp_var (unsigned_type_node);
4655 : 3442 : sctx->lane = create_tmp_var (unsigned_type_node);
4656 : : }
4657 : : }
4658 : 10223 : if (known_eq (sctx->max_vf, 1U))
4659 : : return false;
4660 : :
4661 : 7187 : if (sctx->is_simt)
4662 : : {
4663 : 0 : if (is_gimple_reg (new_var))
4664 : : {
4665 : 0 : ivar = lvar = new_var;
4666 : 0 : return true;
4667 : : }
4668 : 0 : tree type = TREE_TYPE (new_var), ptype = build_pointer_type (type);
4669 : 0 : ivar = lvar = create_tmp_var (type);
4670 : 0 : TREE_ADDRESSABLE (ivar) = 1;
4671 : 0 : DECL_ATTRIBUTES (ivar) = tree_cons (get_identifier ("omp simt private"),
4672 : 0 : NULL, DECL_ATTRIBUTES (ivar));
4673 : 0 : sctx->simt_eargs.safe_push (build1 (ADDR_EXPR, ptype, ivar));
4674 : 0 : tree clobber = build_clobber (type);
4675 : 0 : gimple *g = gimple_build_assign (ivar, clobber);
4676 : 0 : gimple_seq_add_stmt (&sctx->simt_dlist, g);
4677 : : }
4678 : : else
4679 : : {
4680 : 7187 : tree atype = build_array_type_nelts (TREE_TYPE (new_var), sctx->max_vf);
4681 : 7187 : tree avar = create_tmp_var_raw (atype);
4682 : 7187 : if (TREE_ADDRESSABLE (new_var))
4683 : 892 : TREE_ADDRESSABLE (avar) = 1;
4684 : 7187 : DECL_ATTRIBUTES (avar)
4685 : 7187 : = tree_cons (get_identifier ("omp simd array"), NULL,
4686 : 7187 : DECL_ATTRIBUTES (avar));
4687 : 7187 : gimple_add_tmp_var (avar);
4688 : 7187 : tree iavar = avar;
4689 : 7187 : if (rvar && !ctx->for_simd_scan_phase)
4690 : : {
4691 : : /* For inscan reductions, create another array temporary,
4692 : : which will hold the reduced value. */
4693 : 232 : iavar = create_tmp_var_raw (atype);
4694 : 232 : if (TREE_ADDRESSABLE (new_var))
4695 : 17 : TREE_ADDRESSABLE (iavar) = 1;
4696 : 232 : DECL_ATTRIBUTES (iavar)
4697 : 232 : = tree_cons (get_identifier ("omp simd array"), NULL,
4698 : : tree_cons (get_identifier ("omp simd inscan"), NULL,
4699 : 232 : DECL_ATTRIBUTES (iavar)));
4700 : 232 : gimple_add_tmp_var (iavar);
4701 : 232 : ctx->cb.decl_map->put (avar, iavar);
4702 : 232 : if (sctx->lastlane == NULL_TREE)
4703 : 184 : sctx->lastlane = create_tmp_var (unsigned_type_node);
4704 : 232 : *rvar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar,
4705 : : sctx->lastlane, NULL_TREE, NULL_TREE);
4706 : 232 : TREE_THIS_NOTRAP (*rvar) = 1;
4707 : :
4708 : 232 : if (ctx->scan_exclusive)
4709 : : {
4710 : : /* And for exclusive scan yet another one, which will
4711 : : hold the value during the scan phase. */
4712 : 114 : tree savar = create_tmp_var_raw (atype);
4713 : 114 : if (TREE_ADDRESSABLE (new_var))
4714 : 9 : TREE_ADDRESSABLE (savar) = 1;
4715 : 114 : DECL_ATTRIBUTES (savar)
4716 : 114 : = tree_cons (get_identifier ("omp simd array"), NULL,
4717 : : tree_cons (get_identifier ("omp simd inscan "
4718 : : "exclusive"), NULL,
4719 : 114 : DECL_ATTRIBUTES (savar)));
4720 : 114 : gimple_add_tmp_var (savar);
4721 : 114 : ctx->cb.decl_map->put (iavar, savar);
4722 : 114 : *rvar2 = build4 (ARRAY_REF, TREE_TYPE (new_var), savar,
4723 : : sctx->idx, NULL_TREE, NULL_TREE);
4724 : 114 : TREE_THIS_NOTRAP (*rvar2) = 1;
4725 : : }
4726 : : }
4727 : 7187 : ivar = build4 (ARRAY_REF, TREE_TYPE (new_var), iavar, sctx->idx,
4728 : : NULL_TREE, NULL_TREE);
4729 : 7187 : lvar = build4 (ARRAY_REF, TREE_TYPE (new_var), avar, sctx->lane,
4730 : : NULL_TREE, NULL_TREE);
4731 : 7187 : TREE_THIS_NOTRAP (ivar) = 1;
4732 : 7187 : TREE_THIS_NOTRAP (lvar) = 1;
4733 : : }
4734 : 7187 : if (DECL_P (new_var))
4735 : : {
4736 : 7013 : SET_DECL_VALUE_EXPR (new_var, lvar);
4737 : 7013 : 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 : 84 : handle_simd_reference (location_t loc, tree new_vard, gimple_seq *ilist)
4747 : : {
4748 : 84 : tree z = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_vard)));
4749 : 84 : if (TREE_CONSTANT (z))
4750 : : {
4751 : 84 : z = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_vard)),
4752 : : get_name (new_vard));
4753 : 84 : gimple_add_tmp_var (z);
4754 : 84 : TREE_ADDRESSABLE (z) = 1;
4755 : 84 : z = build_fold_addr_expr_loc (loc, z);
4756 : 84 : gimplify_assign (new_vard, z, ilist);
4757 : : }
4758 : 84 : }
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 : 1118 : task_reduction_read (gimple_seq *ilist, tree tskred_temp, tree type,
4765 : : unsigned idx)
4766 : : {
4767 : 1118 : unsigned HOST_WIDE_INT sz
4768 : 1118 : = tree_to_uhwi (TYPE_SIZE_UNIT (pointer_sized_int_node));
4769 : 1118 : tree r = build2 (MEM_REF, pointer_sized_int_node,
4770 : 1118 : tskred_temp, build_int_cst (TREE_TYPE (tskred_temp),
4771 : 1118 : idx * sz));
4772 : 1118 : tree v = create_tmp_var (pointer_sized_int_node);
4773 : 1118 : gimple *g = gimple_build_assign (v, r);
4774 : 1118 : gimple_seq_add_stmt (ilist, g);
4775 : 1118 : if (!useless_type_conversion_p (type, pointer_sized_int_node))
4776 : : {
4777 : 870 : v = create_tmp_var (type);
4778 : 870 : g = gimple_build_assign (v, NOP_EXPR, gimple_assign_lhs (g));
4779 : 870 : gimple_seq_add_stmt (ilist, g);
4780 : : }
4781 : 1118 : 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 : 132992 : 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 : 132992 : if (allocator)
4793 : : return false;
4794 : 132889 : gcc_assert (allocate_ptr == NULL_TREE);
4795 : 132889 : if (ctx->allocate_map
4796 : 3009 : && (DECL_P (new_var) || (TYPE_P (new_var) && size)))
4797 : 3006 : if (tree *allocatorp = ctx->allocate_map->get (var))
4798 : 1111 : allocator = *allocatorp;
4799 : 132889 : if (allocator == NULL_TREE)
4800 : : return false;
4801 : 1111 : if (!is_ref && omp_privatize_by_reference (var))
4802 : : {
4803 : 0 : allocator = NULL_TREE;
4804 : 0 : return false;
4805 : : }
4806 : :
4807 : 1111 : unsigned HOST_WIDE_INT ialign = 0;
4808 : 1111 : if (TREE_CODE (allocator) == TREE_LIST)
4809 : : {
4810 : 136 : ialign = tree_to_uhwi (TREE_VALUE (allocator));
4811 : 136 : allocator = TREE_PURPOSE (allocator);
4812 : : }
4813 : 1111 : if (TREE_CODE (allocator) != INTEGER_CST)
4814 : 427 : allocator = build_outer_var_ref (allocator, ctx, OMP_CLAUSE_ALLOCATE);
4815 : 1111 : allocator = fold_convert (pointer_sized_int_node, allocator);
4816 : 1111 : if (TREE_CODE (allocator) != INTEGER_CST)
4817 : : {
4818 : 427 : tree var = create_tmp_var (TREE_TYPE (allocator));
4819 : 427 : gimplify_assign (var, allocator, ilist);
4820 : 427 : allocator = var;
4821 : : }
4822 : :
4823 : 1111 : tree ptr_type, align, sz = size;
4824 : 1111 : if (TYPE_P (new_var))
4825 : : {
4826 : 300 : ptr_type = build_pointer_type (new_var);
4827 : 300 : ialign = MAX (ialign, TYPE_ALIGN_UNIT (new_var));
4828 : : }
4829 : 811 : else if (is_ref)
4830 : : {
4831 : 95 : ptr_type = build_pointer_type (TREE_TYPE (TREE_TYPE (new_var)));
4832 : 95 : ialign = MAX (ialign, TYPE_ALIGN_UNIT (TREE_TYPE (ptr_type)));
4833 : : }
4834 : : else
4835 : : {
4836 : 716 : ptr_type = build_pointer_type (TREE_TYPE (new_var));
4837 : 716 : ialign = MAX (ialign, DECL_ALIGN_UNIT (new_var));
4838 : 716 : if (sz == NULL_TREE)
4839 : 708 : sz = fold_convert (size_type_node, DECL_SIZE_UNIT (new_var));
4840 : : }
4841 : 1111 : align = build_int_cst (size_type_node, ialign);
4842 : 1111 : if (TREE_CODE (sz) != INTEGER_CST)
4843 : : {
4844 : 27 : tree szvar = create_tmp_var (size_type_node);
4845 : 27 : gimplify_assign (szvar, sz, ilist);
4846 : 27 : sz = szvar;
4847 : : }
4848 : 1111 : allocate_ptr = create_tmp_var (ptr_type);
4849 : 1111 : tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
4850 : 1111 : gimple *g = gimple_build_call (a, 3, align, sz, allocator);
4851 : 1111 : gimple_call_set_lhs (g, allocate_ptr);
4852 : 1111 : gimple_seq_add_stmt (ilist, g);
4853 : 1111 : if (!is_ref)
4854 : : {
4855 : 716 : tree x = build_simple_mem_ref (allocate_ptr);
4856 : 716 : TREE_THIS_NOTRAP (x) = 1;
4857 : 716 : SET_DECL_VALUE_EXPR (new_var, x);
4858 : 716 : 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 : 77163 : lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist,
4870 : : omp_context *ctx, struct omp_for_data *fd)
4871 : : {
4872 : 77163 : tree c, copyin_seq, x, ptr;
4873 : 77163 : bool copyin_by_ref = false;
4874 : 77163 : bool lastprivate_firstprivate = false;
4875 : 77163 : bool reduction_omp_orig_ref = false;
4876 : 77163 : int pass;
4877 : 77163 : bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
4878 : 77163 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
4879 : 77163 : omplow_simd_context sctx = omplow_simd_context ();
4880 : 77163 : tree simt_lane = NULL_TREE, simtrec = NULL_TREE;
4881 : 77163 : tree ivar = NULL_TREE, lvar = NULL_TREE, uid = NULL_TREE;
4882 : 77163 : gimple_seq llist[4] = { };
4883 : 77163 : tree nonconst_simd_if = NULL_TREE;
4884 : :
4885 : 77163 : copyin_seq = NULL;
4886 : 77163 : 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 : 9361 : if (is_simd)
4893 : 61398 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
4894 : 52037 : switch (OMP_CLAUSE_CODE (c))
4895 : : {
4896 : 7589 : case OMP_CLAUSE_LINEAR:
4897 : 7589 : if (OMP_CLAUSE_LINEAR_ARRAY (c))
4898 : 216 : sctx.max_vf = 1;
4899 : : /* FALLTHRU */
4900 : 25110 : case OMP_CLAUSE_PRIVATE:
4901 : 25110 : case OMP_CLAUSE_FIRSTPRIVATE:
4902 : 25110 : case OMP_CLAUSE_LASTPRIVATE:
4903 : 25110 : if (is_variable_sized (OMP_CLAUSE_DECL (c)))
4904 : 0 : sctx.max_vf = 1;
4905 : 25110 : else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
4906 : : {
4907 : 279 : tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4908 : 279 : if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
4909 : 96 : sctx.max_vf = 1;
4910 : : }
4911 : : break;
4912 : 2583 : case OMP_CLAUSE_REDUCTION:
4913 : 2583 : case OMP_CLAUSE_IN_REDUCTION:
4914 : 2583 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
4915 : 2583 : || is_variable_sized (OMP_CLAUSE_DECL (c)))
4916 : 180 : sctx.max_vf = 1;
4917 : 2403 : else if (omp_privatize_by_reference (OMP_CLAUSE_DECL (c)))
4918 : : {
4919 : 184 : tree rtype = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
4920 : 184 : if (!TREE_CONSTANT (TYPE_SIZE_UNIT (rtype)))
4921 : 0 : sctx.max_vf = 1;
4922 : : }
4923 : : break;
4924 : 759 : case OMP_CLAUSE_IF:
4925 : 759 : if (integer_zerop (OMP_CLAUSE_IF_EXPR (c)))
4926 : 124 : sctx.max_vf = 1;
4927 : 635 : else if (TREE_CODE (OMP_CLAUSE_IF_EXPR (c)) != INTEGER_CST)
4928 : 625 : nonconst_simd_if = OMP_CLAUSE_IF_EXPR (c);
4929 : : break;
4930 : 626 : case OMP_CLAUSE_SIMDLEN:
4931 : 626 : if (integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (c)))
4932 : 110 : sctx.max_vf = 1;
4933 : : break;
4934 : 184 : case OMP_CLAUSE__CONDTEMP_:
4935 : : /* FIXME: lastprivate(conditional:) not handled for SIMT yet. */
4936 : 184 : if (sctx.is_simt)
4937 : 0 : sctx.max_vf = 1;
4938 : : break;
4939 : 22775 : default:
4940 : 22775 : continue;
4941 : 22775 : }
4942 : :
4943 : : /* Add a placeholder for simduid. */
4944 : 77163 : if (sctx.is_simt && maybe_ne (sctx.max_vf, 1U))
4945 : 0 : 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 : 234534 : 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 : 391905 : for (pass = 0; pass < ((task_reduction_cnt || task_reduction_other_cnt)
4962 : 234534 : ? 4 : 2); ++pass)
4963 : : {
4964 : 157375 : if (pass == 2 && task_reduction_cnt)
4965 : : {
4966 : 867 : tskred_atype
4967 : 867 : = build_array_type_nelts (ptr_type_node, task_reduction_cnt
4968 : 867 : + task_reduction_cntorig);
4969 : 867 : tskred_avar = create_tmp_var_raw (tskred_atype);
4970 : 867 : gimple_add_tmp_var (tskred_avar);
4971 : 867 : TREE_ADDRESSABLE (tskred_avar) = 1;
4972 : 867 : task_reduction_cnt_full = task_reduction_cnt;
4973 : 867 : task_reduction_cntorig_full = task_reduction_cntorig;
4974 : : }
4975 : 156508 : else if (pass == 3 && task_reduction_cnt)
4976 : : {
4977 : 867 : x = builtin_decl_explicit (BUILT_IN_GOMP_TASK_REDUCTION_REMAP);
4978 : 867 : gimple *g
4979 : 867 : = gimple_build_call (x, 3, size_int (task_reduction_cnt),
4980 : 867 : size_int (task_reduction_cntorig),
4981 : : build_fold_addr_expr (tskred_avar));
4982 : 867 : gimple_seq_add_stmt (ilist, g);
4983 : : }
4984 : 157375 : 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 : 833 : tree v = create_tmp_var (integer_type_node);
4993 : 833 : x = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
4994 : 833 : gimple *g = gimple_build_call (x, 0);
4995 : 833 : gimple_call_set_lhs (g, v);
4996 : 833 : gimple_seq_add_stmt (ilist, g);
4997 : 833 : c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
4998 : 833 : tskred_temp = OMP_CLAUSE_DECL (c);
4999 : 833 : if (is_taskreg_ctx (ctx))
5000 : 552 : tskred_temp = lookup_decl (tskred_temp, ctx);
5001 : 833 : tree v2 = create_tmp_var (sizetype);
5002 : 833 : g = gimple_build_assign (v2, NOP_EXPR, v);
5003 : 833 : gimple_seq_add_stmt (ilist, g);
5004 : 833 : if (ctx->task_reductions[0])
5005 : 816 : v = fold_convert (sizetype, ctx->task_reductions[0]);
5006 : : else
5007 : 17 : v = task_reduction_read (ilist, tskred_temp, sizetype, 1);
5008 : 833 : tree v3 = create_tmp_var (sizetype);
5009 : 833 : g = gimple_build_assign (v3, MULT_EXPR, v2, v);
5010 : 833 : gimple_seq_add_stmt (ilist, g);
5011 : 833 : v = task_reduction_read (ilist, tskred_temp, ptr_type_node, 2);
5012 : 833 : tskred_base = create_tmp_var (ptr_type_node);
5013 : 833 : g = gimple_build_assign (tskred_base, POINTER_PLUS_EXPR, v, v3);
5014 : 833 : gimple_seq_add_stmt (ilist, g);
5015 : : }
5016 : 157375 : task_reduction_cnt = 0;
5017 : 157375 : task_reduction_cntorig = 0;
5018 : 157375 : task_reduction_other_cnt = 0;
5019 : 761100 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
5020 : : {
5021 : 603729 : enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5022 : 603729 : tree var, new_var;
5023 : 603729 : bool by_ref;
5024 : 603729 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
5025 : 603729 : bool task_reduction_p = false;
5026 : 603729 : bool task_reduction_needs_orig_p = false;
5027 : 603729 : tree cond = NULL_TREE;
5028 : 603729 : tree allocator, allocate_ptr;
5029 : :
5030 : 603729 : switch (c_kind)
5031 : : {
5032 : 135866 : case OMP_CLAUSE_PRIVATE:
5033 : 135866 : if (OMP_CLAUSE_PRIVATE_DEBUG (c))
5034 : 435402 : continue;
5035 : : break;
5036 : 59884 : case OMP_CLAUSE_SHARED:
5037 : : /* Ignore shared directives in teams construct inside
5038 : : of target construct. */
5039 : 59884 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
5040 : 59884 : && !is_host_teams_ctx (ctx))
5041 : 23390 : continue;
5042 : 36494 : if (maybe_lookup_decl (OMP_CLAUSE_DECL (c), ctx) == NULL)
5043 : : {
5044 : 12028 : gcc_assert (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c)
5045 : : || is_global_var (OMP_CLAUSE_DECL (c)));
5046 : 12028 : continue;
5047 : : }
5048 : : case OMP_CLAUSE_FIRSTPRIVATE:
5049 : : case OMP_CLAUSE_COPYIN:
5050 : : break;
5051 : 15632 : case OMP_CLAUSE_LINEAR:
5052 : 15632 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
5053 : 15632 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
5054 : : lastprivate_firstprivate = true;
5055 : : break;
5056 : 37226 : case OMP_CLAUSE_REDUCTION:
5057 : 37226 : case OMP_CLAUSE_IN_REDUCTION:
5058 : 37226 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5059 : 28994 : || is_task_ctx (ctx)
5060 : 63896 : || OMP_CLAUSE_REDUCTION_TASK (c))
5061 : : {
5062 : 12744 : task_reduction_p = true;
5063 : 12744 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5064 : : {
5065 : 4512 : task_reduction_other_cnt++;
5066 : 4512 : if (pass == 2)
5067 : 1128 : continue;
5068 : : }
5069 : : else
5070 : 8232 : task_reduction_cnt++;
5071 : 11616 : if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5072 : : {
5073 : 696 : 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 : 696 : if (!DECL_P (var)
5079 : 272 : || omp_privatize_by_reference (var)
5080 : 874 : || !is_global_var
5081 : 178 : (maybe_lookup_decl_in_outer_ctx (var, ctx)))
5082 : : {
5083 : 623 : task_reduction_needs_orig_p = true;
5084 : 623 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5085 : 512 : task_reduction_cntorig++;
5086 : : }
5087 : : }
5088 : : }
5089 : 24482 : else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5090 : 357420 : reduction_omp_orig_ref = true;
5091 : : break;
5092 : 3332 : case OMP_CLAUSE__REDUCTEMP_:
5093 : 3332 : if (!is_taskreg_ctx (ctx))
5094 : 1124 : continue;
5095 : : /* FALLTHRU */
5096 : 111418 : case OMP_CLAUSE__LOOPTEMP_:
5097 : : /* Handle _looptemp_/_reductemp_ clauses only on
5098 : : parallel/task. */
5099 : 111418 : if (fd)
5100 : 69196 : continue;
5101 : : break;
5102 : 43684 : case OMP_CLAUSE_LASTPRIVATE:
5103 : 43684 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
5104 : : {
5105 : 2052 : lastprivate_firstprivate = true;
5106 : 2052 : if (pass != 0 || is_taskloop_ctx (ctx))
5107 : 1337 : continue;
5108 : : }
5109 : : /* Even without corresponding firstprivate, if
5110 : : decl is Fortran allocatable, it needs outer var
5111 : : reference. */
5112 : 41632 : else if (pass == 0
5113 : 62412 : && lang_hooks.decls.omp_private_outer_ref
5114 : 20780 : (OMP_CLAUSE_DECL (c)))
5115 : : lastprivate_firstprivate = true;
5116 : : break;
5117 : 280 : case OMP_CLAUSE_ALIGNED:
5118 : 280 : if (pass != 1)
5119 : 140 : continue;
5120 : 140 : var = OMP_CLAUSE_DECL (c);
5121 : 140 : if (TREE_CODE (TREE_TYPE (var)) == POINTER_TYPE
5122 : 140 : && !is_global_var (var))
5123 : : {
5124 : 76 : new_var = maybe_lookup_decl (var, ctx);
5125 : 76 : if (new_var == NULL_TREE)
5126 : 6 : new_var = maybe_lookup_decl_in_outer_ctx (var, ctx);
5127 : 76 : x = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
5128 : 76 : tree alarg = omp_clause_aligned_alignment (c);
5129 : 76 : alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
5130 : 76 : x = build_call_expr_loc (clause_loc, x, 2, new_var, alarg);
5131 : 76 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5132 : 76 : x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
5133 : 76 : gimplify_and_add (x, ilist);
5134 : : }
5135 : 64 : else if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
5136 : 64 : && is_global_var (var))
5137 : : {
5138 : 64 : tree ptype = build_pointer_type (TREE_TYPE (var)), t, t2;
5139 : 64 : new_var = lookup_decl (var, ctx);
5140 : 64 : t = maybe_lookup_decl_in_outer_ctx (var, ctx);
5141 : 64 : t = build_fold_addr_expr_loc (clause_loc, t);
5142 : 64 : t2 = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
5143 : 64 : tree alarg = omp_clause_aligned_alignment (c);
5144 : 64 : alarg = fold_convert_loc (clause_loc, size_type_node, alarg);
5145 : 64 : t = build_call_expr_loc (clause_loc, t2, 2, t, alarg);
5146 : 64 : t = fold_convert_loc (clause_loc, ptype, t);
5147 : 64 : x = create_tmp_var (ptype);
5148 : 64 : t = build2 (MODIFY_EXPR, ptype, x, t);
5149 : 64 : gimplify_and_add (t, ilist);
5150 : 64 : t = build_simple_mem_ref_loc (clause_loc, x);
5151 : 64 : SET_DECL_VALUE_EXPR (new_var, t);
5152 : 64 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5153 : : }
5154 : 140 : continue;
5155 : 1514 : case OMP_CLAUSE__CONDTEMP_:
5156 : 1514 : if (is_parallel_ctx (ctx)
5157 : 1514 : || (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c)))
5158 : : break;
5159 : 1094 : continue;
5160 : 135826 : default:
5161 : 135826 : continue;
5162 : 137060 : }
5163 : :
5164 : 357420 : if (task_reduction_p != (pass >= 2))
5165 : 15564 : continue;
5166 : :
5167 : 341856 : allocator = NULL_TREE;
5168 : 341856 : allocate_ptr = NULL_TREE;
5169 : 341856 : new_var = var = OMP_CLAUSE_DECL (c);
5170 : 341856 : if ((c_kind == OMP_CLAUSE_REDUCTION
5171 : 341856 : || c_kind == OMP_CLAUSE_IN_REDUCTION)
5172 : 29672 : && TREE_CODE (var) == MEM_REF)
5173 : : {
5174 : 4506 : var = TREE_OPERAND (var, 0);
5175 : 4506 : if (TREE_CODE (var) == POINTER_PLUS_EXPR)
5176 : 773 : var = TREE_OPERAND (var, 0);
5177 : 4506 : if (TREE_CODE (var) == INDIRECT_REF
5178 : 4192 : || TREE_CODE (var) == ADDR_EXPR)
5179 : 2353 : var = TREE_OPERAND (var, 0);
5180 : 4506 : if (is_variable_sized (var))
5181 : : {
5182 : 179 : gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
5183 : 179 : var = DECL_VALUE_EXPR (var);
5184 : 179 : gcc_assert (TREE_CODE (var) == INDIRECT_REF);
5185 : 179 : var = TREE_OPERAND (var, 0);
5186 : 179 : gcc_assert (DECL_P (var));
5187 : : }
5188 : 4506 : new_var = var;
5189 : : }
5190 : 29672 : if (c_kind == OMP_CLAUSE_IN_REDUCTION && is_omp_target (ctx->stmt))
5191 : : {
5192 : 844 : splay_tree_key key = (splay_tree_key) &DECL_CONTEXT (var);
5193 : 844 : new_var = (tree) splay_tree_lookup (ctx->field_map, key)->value;
5194 : : }
5195 : 341012 : else if (c_kind != OMP_CLAUSE_COPYIN)
5196 : 339992 : new_var = lookup_decl (var, ctx);
5197 : :
5198 : 341856 : if (c_kind == OMP_CLAUSE_SHARED || c_kind == OMP_CLAUSE_COPYIN)
5199 : : {
5200 : 24810 : if (pass != 0)
5201 : 12404 : continue;
5202 : : }
5203 : : /* C/C++ array section reductions. */
5204 : 317046 : else if ((c_kind == OMP_CLAUSE_REDUCTION
5205 : : || c_kind == OMP_CLAUSE_IN_REDUCTION)
5206 : 317046 : && var != OMP_CLAUSE_DECL (c))
5207 : : {
5208 : 4506 : if (pass == 0)
5209 : 948 : continue;
5210 : :
5211 : 3558 : tree bias = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5212 : 3558 : tree orig_var = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
5213 : :
5214 : 3558 : if (TREE_CODE (orig_var) == POINTER_PLUS_EXPR)
5215 : : {
5216 : 723 : tree b = TREE_OPERAND (orig_var, 1);
5217 : 723 : if (is_omp_target (ctx->stmt))
5218 : : b = NULL_TREE;
5219 : : else
5220 : 723 : b = maybe_lookup_decl (b, ctx);
5221 : 723 : if (b == NULL)
5222 : : {
5223 : 17 : b = TREE_OPERAND (orig_var, 1);
5224 : 17 : b = maybe_lookup_decl_in_outer_ctx (b, ctx);
5225 : : }
5226 : 723 : if (integer_zerop (bias))
5227 : : bias = b;
5228 : : else
5229 : : {
5230 : 0 : bias = fold_convert_loc (clause_loc,
5231 : 0 : TREE_TYPE (b), bias);
5232 : 0 : bias = fold_build2_loc (clause_loc, PLUS_EXPR,
5233 : 0 : TREE_TYPE (b), b, bias);
5234 : : }
5235 : 723 : orig_var = TREE_OPERAND (orig_var, 0);
5236 : : }
5237 : 3558 : if (pass == 2)
5238 : : {
5239 : 1149 : tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
5240 : 1149 : if (is_global_var (out)
5241 : 241 : && TREE_CODE (TREE_TYPE (out)) != POINTER_TYPE
5242 : 1354 : && (TREE_CODE (TREE_TYPE (out)) != REFERENCE_TYPE
5243 : 81 : || (TREE_CODE (TREE_TYPE (TREE_TYPE (out)))
5244 : : != POINTER_TYPE)))
5245 : : x = var;
5246 : 962 : else if (is_omp_target (ctx->stmt))
5247 : : x = out;
5248 : : else
5249 : : {
5250 : 866 : bool by_ref = use_pointer_for_field (var, NULL);
5251 : 866 : x = build_receiver_ref (var, by_ref, ctx);
5252 : 866 : if (TREE_CODE (TREE_TYPE (var)) == REFERENCE_TYPE
5253 : 866 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (var)))
5254 : : == POINTER_TYPE))
5255 : 72 : x = build_fold_addr_expr (x);
5256 : : }
5257 : 1149 : if (TREE_CODE (orig_var) == INDIRECT_REF)
5258 : 80 : x = build_simple_mem_ref (x);
5259 : 1069 : else if (TREE_CODE (orig_var) == ADDR_EXPR)
5260 : : {
5261 : 506 : if (var == TREE_OPERAND (orig_var, 0))
5262 : 443 : x = build_fold_addr_expr (x);
5263 : : }
5264 : 1149 : bias = fold_convert (sizetype, bias);
5265 : 1149 : x = fold_convert (ptr_type_node, x);
5266 : 1149 : x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
5267 : 1149 : TREE_TYPE (x), x, bias);
5268 : 1149 : unsigned cnt = task_reduction_cnt - 1;
5269 : 1149 : if (!task_reduction_needs_orig_p)
5270 : 1061 : cnt += (task_reduction_cntorig_full
5271 : 1061 : - task_reduction_cntorig);
5272 : : else
5273 : 88 : cnt = task_reduction_cntorig - 1;
5274 : 1149 : tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5275 : 1149 : size_int (cnt), NULL_TREE, NULL_TREE);
5276 : 1149 : gimplify_assign (r, x, ilist);
5277 : 1149 : continue;
5278 : 1149 : }
5279 : :
5280 : 2409 : if (TREE_CODE (orig_var) == INDIRECT_REF
5281 : 2247 : || TREE_CODE (orig_var) == ADDR_EXPR)
5282 : 1273 : orig_var = TREE_OPERAND (orig_var, 0);
5283 : 2409 : tree d = OMP_CLAUSE_DECL (c);
5284 : 2409 : tree type = TREE_TYPE (d);
5285 : 2409 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5286 : 2409 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
5287 : 2409 : tree sz = v;
5288 : 2409 : const char *name = get_name (orig_var);
5289 : 2409 : if (pass != 3 && !TREE_CONSTANT (v))
5290 : : {
5291 : 93 : tree t;
5292 : 93 : if (is_omp_target (ctx->stmt))
5293 : : t = NULL_TREE;
5294 : : else
5295 : 93 : t = maybe_lookup_decl (v, ctx);
5296 : 93 : if (t)
5297 : 88 : v = t;
5298 : : else
5299 : 5 : v = maybe_lookup_decl_in_outer_ctx (v, ctx);
5300 : 93 : gimplify_expr (&v, ilist, NULL, is_gimple_val, fb_rvalue);
5301 : 186 : t = fold_build2_loc (clause_loc, PLUS_EXPR,
5302 : 93 : TREE_TYPE (v), v,
5303 : 93 : build_int_cst (TREE_TYPE (v), 1));
5304 : 93 : sz = fold_build2_loc (clause_loc, MULT_EXPR,
5305 : 93 : TREE_TYPE (v), t,
5306 : 93 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5307 : : }
5308 : 2409 : if (pass == 3)
5309 : : {
5310 : 1461 : tree xv = create_tmp_var (ptr_type_node);
5311 : 1461 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5312 : : {
5313 : 1149 : unsigned cnt = task_reduction_cnt - 1;
5314 : 1149 : if (!task_reduction_needs_orig_p)
5315 : 1061 : cnt += (task_reduction_cntorig_full
5316 : 1061 : - task_reduction_cntorig);
5317 : : else
5318 : 88 : cnt = task_reduction_cntorig - 1;
5319 : 1149 : x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5320 : 1149 : size_int (cnt), NULL_TREE, NULL_TREE);
5321 : :
5322 : 1149 : gimple *g = gimple_build_assign (xv, x);
5323 : 1149 : gimple_seq_add_stmt (ilist, g);
5324 : : }
5325 : : else
5326 : : {
5327 : 312 : unsigned int idx = *ctx->task_reduction_map->get (c);
5328 : 312 : tree off;
5329 : 312 : if (ctx->task_reductions[1 + idx])
5330 : 81 : off = fold_convert (sizetype,
5331 : : ctx->task_reductions[1 + idx]);
5332 : : else
5333 : 231 : off = task_reduction_read (ilist, tskred_temp, sizetype,
5334 : 231 : 7 + 3 * idx + 1);
5335 : 312 : gimple *g = gimple_build_assign (xv, POINTER_PLUS_EXPR,
5336 : : tskred_base, off);
5337 : 312 : gimple_seq_add_stmt (ilist, g);
5338 : : }
5339 : 1461 : x = fold_convert (build_pointer_type (boolean_type_node),
5340 : : xv);
5341 : 1461 : if (TREE_CONSTANT (v))
5342 : 1065 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x,
5343 : : TYPE_SIZE_UNIT (type));
5344 : : else
5345 : : {
5346 : 396 : tree t;
5347 : 396 : if (is_omp_target (ctx->stmt))
5348 : : t = NULL_TREE;
5349 : : else
5350 : 336 : t = maybe_lookup_decl (v, ctx);
5351 : 336 : if (t)
5352 : 316 : v = t;
5353 : : else
5354 : 80 : v = maybe_lookup_decl_in_outer_ctx (v, ctx);
5355 : 396 : gimplify_expr (&v, ilist, NULL, is_gimple_val,
5356 : : fb_rvalue);
5357 : 792 : t = fold_build2_loc (clause_loc, PLUS_EXPR,
5358 : 396 : TREE_TYPE (v), v,
5359 : 396 : build_int_cst (TREE_TYPE (v), 1));
5360 : 396 : t = fold_build2_loc (clause_loc, MULT_EXPR,
5361 : 396 : TREE_TYPE (v), t,
5362 : 396 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5363 : 396 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (x), x, t);
5364 : : }
5365 : 1461 : cond = create_tmp_var (TREE_TYPE (x));
5366 : 1461 : gimplify_assign (cond, x, ilist);
5367 : 1461 : x = xv;
5368 : : }
5369 : 948 : else if (lower_private_allocate (var, type, allocator,
5370 : : allocate_ptr, ilist, ctx,
5371 : : true,
5372 : 948 : TREE_CONSTANT (v)
5373 : 855 : ? TYPE_SIZE_UNIT (type)
5374 : : : sz))
5375 : 300 : x = allocate_ptr;
5376 : 648 : else if (TREE_CONSTANT (v))
5377 : : {
5378 : 560 : x = create_tmp_var_raw (type, name);
5379 : 560 : gimple_add_tmp_var (x);
5380 : 560 : TREE_ADDRESSABLE (x) = 1;
5381 : 560 : x = build_fold_addr_expr_loc (clause_loc, x);
5382 : : }
5383 : : else
5384 : : {
5385 : 88 : tree atmp
5386 : 88 : = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5387 : 88 : tree al = size_int (TYPE_ALIGN (TREE_TYPE (type)));
5388 : 88 : x = build_call_expr_loc (clause_loc, atmp, 2, sz, al);
5389 : : }
5390 : :
5391 : 2409 : tree ptype = build_pointer_type (TREE_TYPE (type));
5392 : 2409 : x = fold_convert_loc (clause_loc, ptype, x);
5393 : 2409 : tree y = create_tmp_var (ptype, name);
5394 : 2409 : gimplify_assign (y, x, ilist);
5395 : 2409 : x = y;
5396 : 2409 : tree yb = y;
5397 : :
5398 : 2409 : if (!integer_zerop (bias))
5399 : : {
5400 : 1542 : bias = fold_convert_loc (clause_loc, pointer_sized_int_node,
5401 : : bias);
5402 : 1542 : yb = fold_convert_loc (clause_loc, pointer_sized_int_node,
5403 : : x);
5404 : 1542 : yb = fold_build2_loc (clause_loc, MINUS_EXPR,
5405 : : pointer_sized_int_node, yb, bias);
5406 : 1542 : x = fold_convert_loc (clause_loc, TREE_TYPE (x), yb);
5407 : 1542 : yb = create_tmp_var (ptype, name);
5408 : 1542 : gimplify_assign (yb, x, ilist);
5409 : 1542 : x = yb;
5410 : : }
5411 : :
5412 : 2409 : d = TREE_OPERAND (d, 0);
5413 : 2409 : if (TREE_CODE (d) == POINTER_PLUS_EXPR)
5414 : 420 : d = TREE_OPERAND (d, 0);
5415 : 2409 : if (TREE_CODE (d) == ADDR_EXPR)
5416 : : {
5417 : 1111 : if (orig_var != var)
5418 : : {
5419 : 91 : gcc_assert (is_variable_sized (orig_var));
5420 : 91 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var),
5421 : : x);
5422 : 91 : gimplify_assign (new_var, x, ilist);
5423 : 91 : tree new_orig_var = lookup_decl (orig_var, ctx);
5424 : 91 : tree t = build_fold_indirect_ref (new_var);
5425 : 91 : DECL_IGNORED_P (new_var) = 0;
5426 : 91 : TREE_THIS_NOTRAP (t) = 1;
5427 : 91 : SET_DECL_VALUE_EXPR (new_orig_var, t);
5428 : 91 : DECL_HAS_VALUE_EXPR_P (new_orig_var) = 1;
5429 : : }
5430 : : else
5431 : : {
5432 : 1020 : x = build2 (MEM_REF, TREE_TYPE (new_var), x,
5433 : 1020 : build_int_cst (ptype, 0));
5434 : 1020 : SET_DECL_VALUE_EXPR (new_var, x);
5435 : 1020 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5436 : : }
5437 : : }
5438 : : else
5439 : : {
5440 : 1298 : gcc_assert (orig_var == var);
5441 : 1298 : if (TREE_CODE (d) == INDIRECT_REF)
5442 : : {
5443 : 162 : x = create_tmp_var (ptype, name);
5444 : 162 : TREE_ADDRESSABLE (x) = 1;
5445 : 162 : gimplify_assign (x, yb, ilist);
5446 : 162 : x = build_fold_addr_expr_loc (clause_loc, x);
5447 : : }
5448 : 1298 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5449 : 1298 : 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 : 2409 : if (cond
5456 : 1461 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
5457 : 3342 : && initializer_zerop (omp_reduction_init (c,
5458 : 933 : TREE_TYPE (type))))
5459 : : {
5460 : 624 : gimple *g = gimple_build_assign (build_simple_mem_ref (cond),
5461 : : boolean_true_node);
5462 : 624 : gimple_seq_add_stmt (ilist, g);
5463 : 624 : continue;
5464 : 624 : }
5465 : 1785 : tree end = create_artificial_label (UNKNOWN_LOCATION);
5466 : 1785 : if (cond)
5467 : : {
5468 : 837 : gimple *g;
5469 : 837 : if (!is_parallel_ctx (ctx))
5470 : : {
5471 : 786 : tree condv = create_tmp_var (boolean_type_node);
5472 : 786 : g = gimple_build_assign (condv,
5473 : : build_simple_mem_ref (cond));
5474 : 786 : gimple_seq_add_stmt (ilist, g);
5475 : 786 : tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
5476 : 786 : g = gimple_build_cond (NE_EXPR, condv,
5477 : : boolean_false_node, end, lab1);
5478 : 786 : gimple_seq_add_stmt (ilist, g);
5479 : 786 : gimple_seq_add_stmt (ilist, gimple_build_label (lab1));
5480 : : }
5481 : 837 : g = gimple_build_assign (build_simple_mem_ref (cond),
5482 : : boolean_true_node);
5483 : 837 : gimple_seq_add_stmt (ilist, g);
5484 : : }
5485 : :
5486 : 1785 : tree y1 = create_tmp_var (ptype);
5487 : 1785 : gimplify_assign (y1, y, ilist);
5488 : 1785 : tree i2 = NULL_TREE, y2 = NULL_TREE;
5489 : 1785 : tree body2 = NULL_TREE, end2 = NULL_TREE;
5490 : 1785 : tree y3 = NULL_TREE, y4 = NULL_TREE;
5491 : 1785 : if (task_reduction_needs_orig_p)
5492 : : {
5493 : 112 : y3 = create_tmp_var (ptype);
5494 : 112 : tree ref;
5495 : 112 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5496 : 88 : ref = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5497 : 88 : size_int (task_reduction_cnt_full
5498 : : + task_reduction_cntorig - 1),
5499 : : NULL_TREE, NULL_TREE);
5500 : : else
5501 : : {
5502 : 24 : unsigned int idx = *ctx->task_reduction_map->get (c);
5503 : 24 : ref = task_reduction_read (ilist, tskred_temp, ptype,
5504 : 24 : 7 + 3 * idx);
5505 : : }
5506 : 112 : gimplify_assign (y3, ref, ilist);
5507 : : }
5508 : 1673 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) || is_simd)
5509 : : {
5510 : 668 : if (pass != 3)
5511 : : {
5512 : 252 : y2 = create_tmp_var (ptype);
5513 : 252 : gimplify_assign (y2, y, ilist);
5514 : : }
5515 : 668 : if (is_simd || OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5516 : : {
5517 : 188 : tree ref = build_outer_var_ref (var, ctx);
5518 : : /* For ref build_outer_var_ref already performs this. */
5519 : 188 : if (TREE_CODE (d) == INDIRECT_REF)
5520 : 0 : gcc_assert (omp_privatize_by_reference (var));
5521 : 188 : else if (TREE_CODE (d) == ADDR_EXPR)
5522 : 96 : ref = build_fold_addr_expr (ref);
5523 : 92 : else if (omp_privatize_by_reference (var))
5524 : 8 : ref = build_fold_addr_expr (ref);
5525 : 188 : ref = fold_convert_loc (clause_loc, ptype, ref);
5526 : 188 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
5527 : 188 : && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
5528 : : {
5529 : 8 : y3 = create_tmp_var (ptype);
5530 : 8 : gimplify_assign (y3, unshare_expr (ref), ilist);
5531 : : }
5532 : 188 : if (is_simd)
5533 : : {
5534 : 180 : y4 = create_tmp_var (ptype);
5535 : 180 : gimplify_assign (y4, ref, dlist);
5536 : : }
5537 : : }
5538 : : }
5539 : 1785 : tree i = create_tmp_var (TREE_TYPE (v));
5540 : 1785 : gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), ilist);
5541 : 1785 : tree body = create_artificial_label (UNKNOWN_LOCATION);
5542 : 1785 : gimple_seq_add_stmt (ilist, gimple_build_label (body));
5543 : 1785 : if (y2)
5544 : : {
5545 : 252 : i2 = create_tmp_var (TREE_TYPE (v));
5546 : 252 : gimplify_assign (i2, build_int_cst (TREE_TYPE (v), 0), dlist);
5547 : 252 : body2 = create_artificial_label (UNKNOWN_LOCATION);
5548 : 252 : end2 = create_artificial_label (UNKNOWN_LOCATION);
5549 : 252 : gimple_seq_add_stmt (dlist, gimple_build_label (body2));
5550 : : }
5551 : 1785 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5552 : : {
5553 : 600 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5554 : 600 : tree decl_placeholder
5555 : 600 : = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
5556 : 600 : SET_DECL_VALUE_EXPR (decl_placeholder,
5557 : : build_simple_mem_ref (y1));
5558 : 600 : DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
5559 : 600 : SET_DECL_VALUE_EXPR (placeholder,
5560 : : y3 ? build_simple_mem_ref (y3)
5561 : : : error_mark_node);
5562 : 600 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
5563 : 600 : x = lang_hooks.decls.omp_clause_default_ctor
5564 : 720 : (c, build_simple_mem_ref (y1),
5565 : 120 : y3 ? build_simple_mem_ref (y3) : NULL_TREE);
5566 : 600 : if (x)
5567 : 152 : gimplify_and_add (x, ilist);
5568 : 600 : if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
5569 : : {
5570 : 576 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
5571 : 576 : lower_omp (&tseq, ctx);
5572 : 576 : gimple_seq_add_seq (ilist, tseq);
5573 : : }
5574 : 600 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
5575 : 600 : if (is_simd)
5576 : : {
5577 : 0 : SET_DECL_VALUE_EXPR (decl_placeholder,
5578 : : build_simple_mem_ref (y2));
5579 : 0 : SET_DECL_VALUE_EXPR (placeholder,
5580 : : build_simple_mem_ref (y4));
5581 : 0 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
5582 : 0 : lower_omp (&tseq, ctx);
5583 : 0 : gimple_seq_add_seq (dlist, tseq);
5584 : 0 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
5585 : : }
5586 : 600 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
5587 : 600 : DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 0;
5588 : 600 : if (y2)
5589 : : {
5590 : 72 : x = lang_hooks.decls.omp_clause_dtor
5591 : 72 : (c, build_simple_mem_ref (y2));
5592 : 72 : if (x)
5593 : 40 : gimplify_and_add (x, dlist);
5594 : : }
5595 : : }
5596 : : else
5597 : : {
5598 : 1185 : x = omp_reduction_init (c, TREE_TYPE (type));
5599 : 1185 : 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 : 1185 : if (code == MINUS_EXPR)
5604 : 0 : code = PLUS_EXPR;
5605 : :
5606 : 1185 : gimplify_assign (build_simple_mem_ref (y1), x, ilist);
5607 : 1185 : if (is_simd)
5608 : : {
5609 : 180 : x = build2 (code, TREE_TYPE (type),
5610 : : build_simple_mem_ref (y4),
5611 : : build_simple_mem_ref (y2));
5612 : 180 : gimplify_assign (build_simple_mem_ref (y4), x, dlist);
5613 : : }
5614 : : }
5615 : 1785 : gimple *g
5616 : 1785 : = gimple_build_assign (y1, POINTER_PLUS_EXPR, y1,
5617 : 1785 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5618 : 1785 : gimple_seq_add_stmt (ilist, g);
5619 : 1785 : if (y3)
5620 : : {
5621 : 120 : g = gimple_build_assign (y3, POINTER_PLUS_EXPR, y3,
5622 : 120 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5623 : 120 : gimple_seq_add_stmt (ilist, g);
5624 : : }
5625 : 1785 : g = gimple_build_assign (i, PLUS_EXPR, i,
5626 : 1785 : build_int_cst (TREE_TYPE (i), 1));
5627 : 1785 : gimple_seq_add_stmt (ilist, g);
5628 : 1785 : g = gimple_build_cond (LE_EXPR, i, v, body, end);
5629 : 1785 : gimple_seq_add_stmt (ilist, g);
5630 : 1785 : gimple_seq_add_stmt (ilist, gimple_build_label (end));
5631 : 1785 : if (y2)
5632 : : {
5633 : 252 : g = gimple_build_assign (y2, POINTER_PLUS_EXPR, y2,
5634 : 252 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5635 : 252 : gimple_seq_add_stmt (dlist, g);
5636 : 252 : if (y4)
5637 : : {
5638 : 180 : g = gimple_build_assign
5639 : 180 : (y4, POINTER_PLUS_EXPR, y4,
5640 : 180 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5641 : 180 : gimple_seq_add_stmt (dlist, g);
5642 : : }
5643 : 252 : g = gimple_build_assign (i2, PLUS_EXPR, i2,
5644 : 252 : build_int_cst (TREE_TYPE (i2), 1));
5645 : 252 : gimple_seq_add_stmt (dlist, g);
5646 : 252 : g = gimple_build_cond (LE_EXPR, i2, v, body2, end2);
5647 : 252 : gimple_seq_add_stmt (dlist, g);
5648 : 252 : gimple_seq_add_stmt (dlist, gimple_build_label (end2));
5649 : : }
5650 : 1785 : if (allocator)
5651 : : {
5652 : 300 : tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
5653 : 300 : g = gimple_build_call (f, 2, allocate_ptr, allocator);
5654 : 300 : gimple_seq_add_stmt (dlist, g);
5655 : : }
5656 : 1785 : continue;
5657 : 1785 : }
5658 : 312540 : else if (pass == 2)
5659 : : {
5660 : 909 : tree out = maybe_lookup_decl_in_outer_ctx (var, ctx);
5661 : 909 : if (is_global_var (out))
5662 : : x = var;
5663 : 361 : else if (is_omp_target (ctx->stmt))
5664 : : x = out;
5665 : : else
5666 : : {
5667 : 301 : bool by_ref = use_pointer_for_field (var, ctx);
5668 : 301 : x = build_receiver_ref (var, by_ref, ctx);
5669 : : }
5670 : 909 : if (!omp_privatize_by_reference (var))
5671 : 781 : x = build_fold_addr_expr (x);
5672 : 909 : x = fold_convert (ptr_type_node, x);
5673 : 909 : unsigned cnt = task_reduction_cnt - 1;
5674 : 909 : if (!task_reduction_needs_orig_p)
5675 : 869 : cnt += task_reduction_cntorig_full - task_reduction_cntorig;
5676 : : else
5677 : 40 : cnt = task_reduction_cntorig - 1;
5678 : 909 : tree r = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5679 : 909 : size_int (cnt), NULL_TREE, NULL_TREE);
5680 : 909 : gimplify_assign (r, x, ilist);
5681 : 909 : continue;
5682 : 909 : }
5683 : 311631 : else if (pass == 3)
5684 : : {
5685 : 1725 : tree type = TREE_TYPE (new_var);
5686 : 1725 : if (!omp_privatize_by_reference (var))
5687 : 1571 : type = build_pointer_type (type);
5688 : 1725 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
5689 : : {
5690 : 909 : unsigned cnt = task_reduction_cnt - 1;
5691 : 909 : if (!task_reduction_needs_orig_p)
5692 : 869 : cnt += (task_reduction_cntorig_full
5693 : 869 : - task_reduction_cntorig);
5694 : : else
5695 : 40 : cnt = task_reduction_cntorig - 1;
5696 : 909 : x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
5697 : 909 : size_int (cnt), NULL_TREE, NULL_TREE);
5698 : : }
5699 : : else
5700 : : {
5701 : 816 : unsigned int idx = *ctx->task_reduction_map->get (c);
5702 : 816 : tree off;
5703 : 816 : if (ctx->task_reductions[1 + idx])
5704 : 816 : off = fold_convert (sizetype,
5705 : : ctx->task_reductions[1 + idx]);
5706 : : else
5707 : 0 : off = task_reduction_read (ilist, tskred_temp, sizetype,
5708 : 0 : 7 + 3 * idx + 1);
5709 : 816 : x = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node,
5710 : : tskred_base, off);
5711 : : }
5712 : 1725 : x = fold_convert (type, x);
5713 : 1725 : tree t;
5714 : 1725 : if (omp_privatize_by_reference (var))
5715 : : {
5716 : 154 : gimplify_assign (new_var, x, ilist);
5717 : 154 : t = new_var;
5718 : 154 : new_var = build_simple_mem_ref (new_var);
5719 : : }
5720 : : else
5721 : : {
5722 : 1571 : t = create_tmp_var (type);
5723 : 1571 : gimplify_assign (t, x, ilist);
5724 : 1571 : SET_DECL_VALUE_EXPR (new_var, build_simple_mem_ref (t));
5725 : 1571 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5726 : : }
5727 : 1725 : t = fold_convert (build_pointer_type (boolean_type_node), t);
5728 : 1725 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
5729 : : TYPE_SIZE_UNIT (TREE_TYPE (type)));
5730 : 1725 : cond = create_tmp_var (TREE_TYPE (t));
5731 : 1725 : gimplify_assign (cond, t, ilist);
5732 : : }
5733 : 309906 : 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 : 254 : if (pass == 0)
5739 : 130 : continue;
5740 : :
5741 : 124 : if (c_kind != OMP_CLAUSE_FIRSTPRIVATE || !is_task_ctx (ctx))
5742 : : {
5743 : 110 : tree tmp;
5744 : :
5745 : 110 : ptr = DECL_VALUE_EXPR (new_var);
5746 : 110 : gcc_assert (TREE_CODE (ptr) == INDIRECT_REF);
5747 : 110 : ptr = TREE_OPERAND (ptr, 0);
5748 : 110 : gcc_assert (DECL_P (ptr));
5749 : 110 : x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
5750 : :
5751 : 110 : if (lower_private_allocate (var, new_var, allocator,
5752 : : allocate_ptr, ilist, ctx,
5753 : : false, x))
5754 : 8 : tmp = allocate_ptr;
5755 : : else
5756 : : {
5757 : : /* void *tmp = __builtin_alloca */
5758 : 102 : tree atmp
5759 : 102 : = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5760 : 102 : gcall *stmt
5761 : 102 : = gimple_build_call (atmp, 2, x,
5762 : 102 : size_int (DECL_ALIGN (var)));
5763 : 102 : cfun->calls_alloca = 1;
5764 : 102 : tmp = create_tmp_var_raw (ptr_type_node);
5765 : 102 : gimple_add_tmp_var (tmp);
5766 : 102 : gimple_call_set_lhs (stmt, tmp);
5767 : :
5768 : 102 : gimple_seq_add_stmt (ilist, stmt);
5769 : : }
5770 : :
5771 : 110 : x = fold_convert_loc (clause_loc, TREE_TYPE (ptr), tmp);
5772 : 110 : gimplify_assign (ptr, x, ilist);
5773 : : }
5774 : : }
5775 : 309652 : else if (omp_privatize_by_reference (var)
5776 : 309652 : && (c_kind != OMP_CLAUSE_FIRSTPRIVATE
5777 : 2044 : || !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 : 5005 : if (pass == 0)
5785 : 2605 : continue;
5786 : :
5787 : 2400 : x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
5788 : 2400 : if (c_kind == OMP_CLAUSE_FIRSTPRIVATE && is_task_ctx (ctx))
5789 : : {
5790 : 149 : x = build_receiver_ref (var, false, ctx);
5791 : 149 : if (ctx->allocate_map)
5792 : 16 : if (tree *allocatep = ctx->allocate_map->get (var))
5793 : : {
5794 : 16 : allocator = *allocatep;
5795 : 16 : if (TREE_CODE (allocator) == TREE_LIST)
5796 : 0 : allocator = TREE_PURPOSE (allocator);
5797 : 16 : if (TREE_CODE (allocator) != INTEGER_CST)
5798 : 8 : allocator = build_outer_var_ref (allocator, ctx);
5799 : 16 : allocator = fold_convert (pointer_sized_int_node,
5800 : : allocator);
5801 : 16 : allocate_ptr = unshare_expr (x);
5802 : : }
5803 : 149 : if (allocator == NULL_TREE)
5804 : 133 : x = build_fold_addr_expr_loc (clause_loc, x);
5805 : : }
5806 : 2251 : else if (lower_private_allocate (var, new_var, allocator,
5807 : : allocate_ptr,
5808 : : ilist, ctx, true, x))
5809 : 95 : x = allocate_ptr;
5810 : 2156 : 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 : 1460 : if (is_simd)
5817 : : x = NULL_TREE;
5818 : : else
5819 : : {
5820 : 1093 : x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
5821 : : get_name (var));
5822 : 1093 : gimple_add_tmp_var (x);
5823 : 1093 : TREE_ADDRESSABLE (x) = 1;
5824 : 1093 : x = build_fold_addr_expr_loc (clause_loc, x);
5825 : : }
5826 : : }
5827 : : else
5828 : : {
5829 : 696 : tree atmp
5830 : 696 : = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
5831 : 696 : tree rtype = TREE_TYPE (TREE_TYPE (new_var));
5832 : 696 : tree al = size_int (TYPE_ALIGN (rtype));
5833 : 696 : x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
5834 : : }
5835 : :
5836 : 2033 : if (x)
5837 : : {
5838 : 2033 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
5839 : 2033 : gimplify_assign (new_var, x, ilist);
5840 : : }
5841 : :
5842 : 2400 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
5843 : : }
5844 : 304647 : else if ((c_kind == OMP_CLAUSE_REDUCTION
5845 : : || c_kind == OMP_CLAUSE_IN_REDUCTION)
5846 : 304647 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5847 : : {
5848 : 1890 : if (pass == 0)
5849 : 945 : continue;
5850 : : }
5851 : 302757 : else if (pass != 0)
5852 : 151124 : continue;
5853 : :
5854 : 169233 : switch (OMP_CLAUSE_CODE (c))
5855 : : {
5856 : 11896 : case OMP_CLAUSE_SHARED:
5857 : : /* Ignore shared directives in teams construct inside
5858 : : target construct. */
5859 : 11896 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_TEAMS
5860 : 11896 : && !is_host_teams_ctx (ctx))
5861 : 0 : continue;
5862 : : /* Shared global vars are just accessed directly. */
5863 : 11896 : 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 : 11752 : 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 : 11443 : by_ref = use_pointer_for_field (var, ctx);
5874 : 11443 : x = build_receiver_ref (var, by_ref, ctx);
5875 : 11443 : SET_DECL_VALUE_EXPR (new_var, x);
5876 : 11443 : 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 : 11443 : if (!by_ref)
5884 : 5207 : suppress_warning (var, OPT_Wuninitialized);
5885 : : break;
5886 : :
5887 : 210 : case OMP_CLAUSE__CONDTEMP_:
5888 : 210 : if (is_parallel_ctx (ctx))
5889 : : {
5890 : 103 : x = build_receiver_ref (var, false, ctx);
5891 : 103 : SET_DECL_VALUE_EXPR (new_var, x);
5892 : 103 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
5893 : : }
5894 : 107 : else if (is_simd && !OMP_CLAUSE__CONDTEMP__ITER (c))
5895 : : {
5896 : 107 : x = build_zero_cst (TREE_TYPE (var));
5897 : 107 : goto do_private;
5898 : : }
5899 : : break;
5900 : :
5901 : 21284 : case OMP_CLAUSE_LASTPRIVATE:
5902 : 21284 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
5903 : : break;
5904 : : /* FALLTHRU */
5905 : :
5906 : 87321 : case OMP_CLAUSE_PRIVATE:
5907 : 87321 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE)
5908 : 20780 : x = build_outer_var_ref (var, ctx);
5909 : 66541 : else if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
5910 : : {
5911 : 162 : if (is_task_ctx (ctx))
5912 : 12 : x = build_receiver_ref (var, false, ctx);
5913 : : else
5914 : 150 : x = build_outer_var_ref (var, ctx, OMP_CLAUSE_PRIVATE);
5915 : : }
5916 : : else
5917 : : x = NULL;
5918 : 93975 : do_private:
5919 : 93975 : tree nx;
5920 : 93975 : bool copy_ctor;
5921 : 93975 : copy_ctor = false;
5922 : 93975 : lower_private_allocate (var, new_var, allocator, allocate_ptr,
5923 : : ilist, ctx, false, NULL_TREE);
5924 : 93975 : nx = unshare_expr (new_var);
5925 : 93975 : if (is_simd
5926 : 24154 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5927 : 101360 : && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
5928 : 22 : copy_ctor = true;
5929 : 93975 : if (copy_ctor)
5930 : 22 : nx = lang_hooks.decls.omp_clause_copy_ctor (c, nx, x);
5931 : : else
5932 : 93953 : nx = lang_hooks.decls.omp_clause_default_ctor (c, nx, x);
5933 : 93975 : if (is_simd)
5934 : : {
5935 : 24154 : tree y = lang_hooks.decls.omp_clause_dtor (c, new_var);
5936 : 23471 : if ((TREE_ADDRESSABLE (new_var) || nx || y
5937 : 23455 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5938 : 7009 : && (gimple_omp_for_collapse (ctx->stmt) != 1
5939 : 1385 : || (gimple_omp_for_index (ctx->stmt, 0)
5940 : : != new_var)))
5941 : 16758 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_
5942 : 16651 : || omp_privatize_by_reference (var))
5943 : 31008 : && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
5944 : : ivar, lvar))
5945 : : {
5946 : 5989 : if (omp_privatize_by_reference (var))
5947 : : {
5948 : 52 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
5949 : 52 : tree new_vard = TREE_OPERAND (new_var, 0);
5950 : 52 : gcc_assert (DECL_P (new_vard));
5951 : 52 : SET_DECL_VALUE_EXPR (new_vard,
5952 : : build_fold_addr_expr (lvar));
5953 : 52 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
5954 : : }
5955 : :
5956 : 5989 : if (nx)
5957 : : {
5958 : 30 : tree iv = unshare_expr (ivar);
5959 : 30 : if (copy_ctor)
5960 : 22 : x = lang_hooks.decls.omp_clause_copy_ctor (c, iv,
5961 : : x);
5962 : : else
5963 : 8 : x = lang_hooks.decls.omp_clause_default_ctor (c,
5964 : : iv,
5965 : : x);
5966 : : }
5967 : 5959 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__CONDTEMP_)
5968 : : {
5969 : 37 : x = build2 (MODIFY_EXPR, TREE_TYPE (ivar),
5970 : : unshare_expr (ivar), x);
5971 : 37 : nx = x;
5972 : : }
5973 : 5989 : if (nx && x)
5974 : 67 : gimplify_and_add (x, &llist[0]);
5975 : 5989 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5976 : 5989 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
5977 : : {
5978 : 37 : tree v = new_var;
5979 : 37 : if (!DECL_P (v))
5980 : : {
5981 : 4 : gcc_assert (TREE_CODE (v) == MEM_REF);
5982 : 4 : v = TREE_OPERAND (v, 0);
5983 : 4 : gcc_assert (DECL_P (v));
5984 : : }
5985 : 37 : v = *ctx->lastprivate_conditional_map->get (v);
5986 : 37 : tree t = create_tmp_var (TREE_TYPE (v));
5987 : 37 : tree z = build_zero_cst (TREE_TYPE (v));
5988 : 37 : tree orig_v
5989 : 37 : = build_outer_var_ref (var, ctx,
5990 : : OMP_CLAUSE_LASTPRIVATE);
5991 : 37 : gimple_seq_add_stmt (dlist,
5992 : 37 : gimple_build_assign (t, z));
5993 : 37 : gcc_assert (DECL_HAS_VALUE_EXPR_P (v));
5994 : 37 : tree civar = DECL_VALUE_EXPR (v);
5995 : 37 : gcc_assert (TREE_CODE (civar) == ARRAY_REF);
5996 : 37 : civar = unshare_expr (civar);
5997 : 37 : TREE_OPERAND (civar, 1) = sctx.idx;
5998 : 37 : x = build2 (MODIFY_EXPR, TREE_TYPE (t), t,
5999 : : unshare_expr (civar));
6000 : 74 : x = build2 (COMPOUND_EXPR, TREE_TYPE (orig_v), x,
6001 : 37 : build2 (MODIFY_EXPR, TREE_TYPE (orig_v),
6002 : : orig_v, unshare_expr (ivar)));
6003 : 37 : tree cond = build2 (LT_EXPR, boolean_type_node, t,
6004 : : civar);
6005 : 37 : x = build3 (COND_EXPR, void_type_node, cond, x,
6006 : : void_node);
6007 : 37 : gimple_seq tseq = NULL;
6008 : 37 : gimplify_and_add (x, &tseq);
6009 : 37 : if (ctx->outer)
6010 : 27 : lower_omp (&tseq, ctx->outer);
6011 : 37 : gimple_seq_add_seq (&llist[1], tseq);
6012 : : }
6013 : 5989 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6014 : 5989 : && ctx->for_simd_scan_phase)
6015 : : {
6016 : 9 : x = unshare_expr (ivar);
6017 : 9 : tree orig_v
6018 : 9 : = build_outer_var_ref (var, ctx,
6019 : : OMP_CLAUSE_LASTPRIVATE);
6020 : 9 : x = lang_hooks.decls.omp_clause_assign_op (c, x,
6021 : : orig_v);
6022 : 9 : gimplify_and_add (x, &llist[0]);
6023 : : }
6024 : 5989 : if (y)
6025 : : {
6026 : 30 : y = lang_hooks.decls.omp_clause_dtor (c, ivar);
6027 : 30 : if (y)
6028 : 30 : gimplify_and_add (y, &llist[1]);
6029 : : }
6030 : : break;
6031 : : }
6032 : 18165 : if (omp_privatize_by_reference (var))
6033 : : {
6034 : 28 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6035 : 28 : tree new_vard = TREE_OPERAND (new_var, 0);
6036 : 28 : gcc_assert (DECL_P (new_vard));
6037 : 28 : tree type = TREE_TYPE (TREE_TYPE (new_vard));
6038 : 28 : x = TYPE_SIZE_UNIT (type);
6039 : 28 : if (TREE_CONSTANT (x))
6040 : : {
6041 : 28 : x = create_tmp_var_raw (type, get_name (var));
6042 : 28 : gimple_add_tmp_var (x);
6043 : 28 : TREE_ADDRESSABLE (x) = 1;
6044 : 28 : x = build_fold_addr_expr_loc (clause_loc, x);
6045 : 28 : x = fold_convert_loc (clause_loc,
6046 : 28 : TREE_TYPE (new_vard), x);
6047 : 28 : gimplify_assign (new_vard, x, ilist);
6048 : : }
6049 : : }
6050 : : }
6051 : 87986 : if (nx)
6052 : 485 : gimplify_and_add (nx, ilist);
6053 : 87986 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6054 : 15149 : && is_simd
6055 : 89740 : && ctx->for_simd_scan_phase)
6056 : : {
6057 : 5 : tree orig_v = build_outer_var_ref (var, ctx,
6058 : : OMP_CLAUSE_LASTPRIVATE);
6059 : 5 : x = lang_hooks.decls.omp_clause_assign_op (c, new_var,
6060 : : orig_v);
6061 : 5 : gimplify_and_add (x, ilist);
6062 : : }
6063 : : /* FALLTHRU */
6064 : :
6065 : 118773 : do_dtor:
6066 : 118773 : x = lang_hooks.decls.omp_clause_dtor (c, new_var);
6067 : 118773 : if (x)
6068 : 1128 : gimplify_and_add (x, dlist);
6069 : 118773 : if (allocator)
6070 : : {
6071 : 861 : if (!is_gimple_val (allocator))
6072 : : {
6073 : 22 : tree avar = create_tmp_var (TREE_TYPE (allocator));
6074 : 22 : gimplify_assign (avar, allocator, dlist);
6075 : 22 : allocator = avar;
6076 : : }
6077 : 861 : if (!is_gimple_val (allocate_ptr))
6078 : : {
6079 : 50 : tree apvar = create_tmp_var (TREE_TYPE (allocate_ptr));
6080 : 50 : gimplify_assign (apvar, allocate_ptr, dlist);
6081 : 50 : allocate_ptr = apvar;
6082 : : }
6083 : 861 : tree f = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
6084 : 861 : gimple *g
6085 : 861 : = gimple_build_call (f, 2, allocate_ptr, allocator);
6086 : 861 : gimple_seq_add_stmt (dlist, g);
6087 : : }
6088 : : break;
6089 : :
6090 : 7816 : case OMP_CLAUSE_LINEAR:
6091 : 7816 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6092 : 1269 : goto do_firstprivate;
6093 : 6547 : if (OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6094 : : x = NULL;
6095 : : else
6096 : 3606 : x = build_outer_var_ref (var, ctx);
6097 : 6547 : goto do_private;
6098 : :
6099 : 28632 : case OMP_CLAUSE_FIRSTPRIVATE:
6100 : 28632 : if (is_task_ctx (ctx))
6101 : : {
6102 : 4722 : if ((omp_privatize_by_reference (var)
6103 : 149 : && !OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c))
6104 : 4722 : || is_variable_sized (var))
6105 : 163 : goto do_dtor;
6106 : 4559 : else if (is_global_var (maybe_lookup_decl_in_outer_ctx (var,
6107 : : ctx))
6108 : 4559 : || use_pointer_for_field (var, NULL))
6109 : : {
6110 : 420 : x = build_receiver_ref (var, false, ctx);
6111 : 420 : if (ctx->allocate_map)
6112 : 34 : if (tree *allocatep = ctx->allocate_map->get (var))
6113 : : {
6114 : 34 : allocator = *allocatep;
6115 : 34 : if (TREE_CODE (allocator) == TREE_LIST)
6116 : 4 : allocator = TREE_PURPOSE (allocator);
6117 : 34 : if (TREE_CODE (allocator) != INTEGER_CST)
6118 : 14 : allocator = build_outer_var_ref (allocator, ctx);
6119 : 34 : allocator = fold_convert (pointer_sized_int_node,
6120 : : allocator);
6121 : 34 : allocate_ptr = unshare_expr (x);
6122 : 34 : x = build_simple_mem_ref (x);
6123 : 34 : TREE_THIS_NOTRAP (x) = 1;
6124 : : }
6125 : 420 : SET_DECL_VALUE_EXPR (new_var, x);
6126 : 420 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
6127 : 420 : goto do_dtor;
6128 : : }
6129 : : }
6130 : 28049 : if (OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c)
6131 : 28049 : && omp_privatize_by_reference (var))
6132 : : {
6133 : 23 : x = build_outer_var_ref (var, ctx);
6134 : 23 : gcc_assert (TREE_CODE (x) == MEM_REF
6135 : : && integer_zerop (TREE_OPERAND (x, 1)));
6136 : 23 : x = TREE_OPERAND (x, 0);
6137 : 23 : x = lang_hooks.decls.omp_clause_copy_ctor
6138 : 23 : (c, unshare_expr (new_var), x);
6139 : 23 : gimplify_and_add (x, ilist);
6140 : 23 : goto do_dtor;
6141 : : }
6142 : 29295 : do_firstprivate:
6143 : 29295 : lower_private_allocate (var, new_var, allocator, allocate_ptr,
6144 : : ilist, ctx, false, NULL_TREE);
6145 : 29295 : x = build_outer_var_ref (var, ctx);
6146 : 29295 : if (is_simd)
6147 : : {
6148 : 1063 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6149 : 1063 : && gimple_omp_for_combined_into_p (ctx->stmt))
6150 : : {
6151 : 668 : tree t = OMP_CLAUSE_LINEAR_STEP (c);
6152 : 668 : if (DECL_P (t))
6153 : 114 : t = build_outer_var_ref (t, ctx);
6154 : 668 : tree stept = TREE_TYPE (t);
6155 : 668 : tree ct = omp_find_clause (clauses,
6156 : : OMP_CLAUSE__LOOPTEMP_);
6157 : 668 : gcc_assert (ct);
6158 : 668 : tree l = OMP_CLAUSE_DECL (ct);
6159 : 668 : tree n1 = fd->loop.n1;
6160 : 668 : tree step = fd->loop.step;
6161 : 668 : tree itype = TREE_TYPE (l);
6162 : 668 : if (POINTER_TYPE_P (itype))
6163 : 0 : itype = signed_type_for (itype);
6164 : 668 : l = fold_build2 (MINUS_EXPR, itype, l, n1);
6165 : 668 : if (TYPE_UNSIGNED (itype)
6166 : 668 : && fd->loop.cond_code == GT_EXPR)
6167 : 0 : 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 : 668 : l = fold_build2 (TRUNC_DIV_EXPR, itype, l, step);
6173 : 668 : t = fold_build2 (MULT_EXPR, stept,
6174 : : fold_convert (stept, l), t);
6175 : :
6176 : 668 : if (OMP_CLAUSE_LINEAR_ARRAY (c))
6177 : : {
6178 : 108 : if (omp_privatize_by_reference (var))
6179 : : {
6180 : 72 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6181 : 72 : tree new_vard = TREE_OPERAND (new_var, 0);
6182 : 72 : gcc_assert (DECL_P (new_vard));
6183 : 72 : tree type = TREE_TYPE (TREE_TYPE (new_vard));
6184 : 72 : nx = TYPE_SIZE_UNIT (type);
6185 : 72 : if (TREE_CONSTANT (nx))
6186 : : {
6187 : 24 : nx = create_tmp_var_raw (type,
6188 : : get_name (var));
6189 : 24 : gimple_add_tmp_var (nx);
6190 : 24 : TREE_ADDRESSABLE (nx) = 1;
6191 : 24 : nx = build_fold_addr_expr_loc (clause_loc,
6192 : : nx);
6193 : 24 : nx = fold_convert_loc (clause_loc,
6194 : 24 : TREE_TYPE (new_vard),
6195 : : nx);
6196 : 24 : gimplify_assign (new_vard, nx, ilist);
6197 : : }
6198 : : }
6199 : :
6200 : 108 : x = lang_hooks.decls.omp_clause_linear_ctor
6201 : 108 : (c, new_var, x, t);
6202 : 108 : gimplify_and_add (x, ilist);
6203 : 108 : goto do_dtor;
6204 : : }
6205 : :
6206 : 560 : if (POINTER_TYPE_P (TREE_TYPE (x)))
6207 : 11 : x = fold_build_pointer_plus (x, t);
6208 : : else
6209 : 549 : x = fold_build2 (PLUS_EXPR, TREE_TYPE (x), x,
6210 : : fold_convert (TREE_TYPE (x), t));
6211 : : }
6212 : :
6213 : 955 : if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6214 : 934 : || TREE_ADDRESSABLE (new_var)
6215 : 799 : || omp_privatize_by_reference (var))
6216 : 1217 : && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6217 : : ivar, lvar))
6218 : : {
6219 : 152 : if (omp_privatize_by_reference (var))
6220 : : {
6221 : 22 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6222 : 22 : tree new_vard = TREE_OPERAND (new_var, 0);
6223 : 22 : gcc_assert (DECL_P (new_vard));
6224 : 22 : SET_DECL_VALUE_EXPR (new_vard,
6225 : : build_fold_addr_expr (lvar));
6226 : 22 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6227 : : }
6228 : 152 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
6229 : : {
6230 : 131 : tree iv = create_tmp_var (TREE_TYPE (new_var));
6231 : 131 : x = lang_hooks.decls.omp_clause_copy_ctor (c, iv, x);
6232 : 131 : gimplify_and_add (x, ilist);
6233 : 131 : gimple_stmt_iterator gsi
6234 : 131 : = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
6235 : 131 : gassign *g
6236 : 131 : = gimple_build_assign (unshare_expr (lvar), iv);
6237 : 131 : gsi_insert_before_without_update (&gsi, g,
6238 : : GSI_SAME_STMT);
6239 : 131 : tree t = OMP_CLAUSE_LINEAR_STEP (c);
6240 : 131 : enum tree_code code = PLUS_EXPR;
6241 : 131 : if (POINTER_TYPE_P (TREE_TYPE (new_var)))
6242 : : code = POINTER_PLUS_EXPR;
6243 : 131 : g = gimple_build_assign (iv, code, iv, t);
6244 : 131 : gsi_insert_before_without_update (&gsi, g,
6245 : : GSI_SAME_STMT);
6246 : 131 : break;
6247 : : }
6248 : 21 : x = lang_hooks.decls.omp_clause_copy_ctor
6249 : 21 : (c, unshare_expr (ivar), x);
6250 : 21 : gimplify_and_add (x, &llist[0]);
6251 : 21 : x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6252 : 21 : if (x)
6253 : 21 : gimplify_and_add (x, &llist[1]);
6254 : : break;
6255 : : }
6256 : 803 : if (omp_privatize_by_reference (var))
6257 : : {
6258 : 105 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6259 : 105 : tree new_vard = TREE_OPERAND (new_var, 0);
6260 : 105 : gcc_assert (DECL_P (new_vard));
6261 : 105 : tree type = TREE_TYPE (TREE_TYPE (new_vard));
6262 : 105 : nx = TYPE_SIZE_UNIT (type);
6263 : 105 : if (TREE_CONSTANT (nx))
6264 : : {
6265 : 57 : nx = create_tmp_var_raw (type, get_name (var));
6266 : 57 : gimple_add_tmp_var (nx);
6267 : 57 : TREE_ADDRESSABLE (nx) = 1;
6268 : 57 : nx = build_fold_addr_expr_loc (clause_loc, nx);
6269 : 57 : nx = fold_convert_loc (clause_loc,
6270 : 57 : TREE_TYPE (new_vard), nx);
6271 : 57 : gimplify_assign (new_vard, nx, ilist);
6272 : : }
6273 : : }
6274 : : }
6275 : 29035 : x = lang_hooks.decls.omp_clause_copy_ctor
6276 : 29035 : (c, unshare_expr (new_var), x);
6277 : 29031 : gimplify_and_add (x, ilist);
6278 : 29031 : goto do_dtor;
6279 : :
6280 : 19353 : case OMP_CLAUSE__LOOPTEMP_:
6281 : 19353 : case OMP_CLAUSE__REDUCTEMP_:
6282 : 19353 : gcc_assert (is_taskreg_ctx (ctx));
6283 : 19353 : x = build_outer_var_ref (var, ctx);
6284 : 19353 : x = build2 (MODIFY_EXPR, TREE_TYPE (new_var), new_var, x);
6285 : 19353 : gimplify_and_add (x, ilist);
6286 : 19353 : break;
6287 : :
6288 : 510 : case OMP_CLAUSE_COPYIN:
6289 : 510 : by_ref = use_pointer_for_field (var, NULL);
6290 : 510 : x = build_receiver_ref (var, by_ref, ctx);
6291 : 510 : x = lang_hooks.decls.omp_clause_assign_op (c, new_var, x);
6292 : 510 : append_to_statement_list (x, ©in_seq);
6293 : 510 : copyin_by_ref |= by_ref;
6294 : 510 : break;
6295 : :
6296 : 12991 : case OMP_CLAUSE_REDUCTION:
6297 : 12991 : case OMP_CLAUSE_IN_REDUCTION:
6298 : : /* OpenACC reductions are initialized using the
6299 : : GOACC_REDUCTION internal function. */
6300 : 12991 : if (is_gimple_omp_oacc (ctx->stmt))
6301 : : break;
6302 : 9010 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6303 : : {
6304 : 1440 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6305 : 1440 : gimple *tseq;
6306 : 1440 : tree ptype = TREE_TYPE (placeholder);
6307 : 1440 : if (cond)
6308 : : {
6309 : 294 : x = error_mark_node;
6310 : 294 : if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)
6311 : 294 : && !task_reduction_needs_orig_p)
6312 : 20 : x = var;
6313 : 274 : else if (OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c))
6314 : : {
6315 : 53 : tree pptype = build_pointer_type (ptype);
6316 : 53 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
6317 : 40 : x = build4 (ARRAY_REF, ptr_type_node, tskred_avar,
6318 : 40 : size_int (task_reduction_cnt_full
6319 : : + task_reduction_cntorig - 1),
6320 : : NULL_TREE, NULL_TREE);
6321 : : else
6322 : : {
6323 : 13 : unsigned int idx
6324 : 13 : = *ctx->task_reduction_map->get (c);
6325 : 13 : x = task_reduction_read (ilist, tskred_temp,
6326 : 13 : pptype, 7 + 3 * idx);
6327 : : }
6328 : 53 : x = fold_convert (pptype, x);
6329 : 53 : x = build_simple_mem_ref (x);
6330 : : }
6331 : : }
6332 : : else
6333 : : {
6334 : 1146 : lower_private_allocate (var, new_var, allocator,
6335 : : allocate_ptr, ilist, ctx, false,
6336 : : NULL_TREE);
6337 : 1146 : x = build_outer_var_ref (var, ctx);
6338 : :
6339 : 1146 : if (omp_privatize_by_reference (var)
6340 : 1146 : && !useless_type_conversion_p (ptype, TREE_TYPE (x)))
6341 : 49 : x = build_fold_addr_expr_loc (clause_loc, x);
6342 : : }
6343 : 1440 : SET_DECL_VALUE_EXPR (placeholder, x);
6344 : 1440 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
6345 : 1440 : tree new_vard = new_var;
6346 : 1440 : if (omp_privatize_by_reference (var))
6347 : : {
6348 : 257 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6349 : 257 : new_vard = TREE_OPERAND (new_var, 0);
6350 : 257 : gcc_assert (DECL_P (new_vard));
6351 : : }
6352 : 1440 : tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
6353 : 1440 : if (is_simd
6354 : 311 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6355 : 1751 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
6356 : : rvarp = &rvar;
6357 : 1440 : if (is_simd
6358 : 1440 : && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6359 : : ivar, lvar, rvarp,
6360 : : &rvar2))
6361 : : {
6362 : 174 : if (new_vard == new_var)
6363 : : {
6364 : 118 : gcc_assert (DECL_VALUE_EXPR (new_var) == lvar);
6365 : 118 : SET_DECL_VALUE_EXPR (new_var, ivar);
6366 : : }
6367 : : else
6368 : : {
6369 : 56 : SET_DECL_VALUE_EXPR (new_vard,
6370 : : build_fold_addr_expr (ivar));
6371 : 56 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6372 : : }
6373 : 174 : x = lang_hooks.decls.omp_clause_default_ctor
6374 : 174 : (c, unshare_expr (ivar),
6375 : : build_outer_var_ref (var, ctx));
6376 : 174 : if (rvarp && ctx->for_simd_scan_phase)
6377 : : {
6378 : 16 : if (x)
6379 : 8 : gimplify_and_add (x, &llist[0]);
6380 : 16 : x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6381 : 16 : if (x)
6382 : 8 : gimplify_and_add (x, &llist[1]);
6383 : 468 : break;
6384 : : }
6385 : 78 : else if (rvarp)
6386 : : {
6387 : 78 : if (x)
6388 : : {
6389 : 38 : gimplify_and_add (x, &llist[0]);
6390 : :
6391 : 38 : tree ivar2 = unshare_expr (lvar);
6392 : 38 : TREE_OPERAND (ivar2, 1) = sctx.idx;
6393 : 38 : x = lang_hooks.decls.omp_clause_default_ctor
6394 : 38 : (c, ivar2, build_outer_var_ref (var, ctx));
6395 : 38 : gimplify_and_add (x, &llist[0]);
6396 : :
6397 : 38 : if (rvar2)
6398 : : {
6399 : 16 : x = lang_hooks.decls.omp_clause_default_ctor
6400 : 16 : (c, unshare_expr (rvar2),
6401 : : build_outer_var_ref (var, ctx));
6402 : 16 : 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 : 38 : tree nv = create_tmp_var_raw (TREE_TYPE (ivar));
6413 : 38 : gimple_add_tmp_var (nv);
6414 : 60 : ctx->cb.decl_map->put (TREE_OPERAND (rvar2
6415 : : ? rvar2
6416 : : : ivar, 0),
6417 : : nv);
6418 : 38 : x = lang_hooks.decls.omp_clause_default_ctor
6419 : 38 : (c, nv, build_outer_var_ref (var, ctx));
6420 : 38 : gimplify_and_add (x, ilist);
6421 : :
6422 : 38 : if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6423 : : {
6424 : 19 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6425 : 19 : x = DECL_VALUE_EXPR (new_vard);
6426 : 19 : tree vexpr = nv;
6427 : 19 : if (new_vard != new_var)
6428 : 11 : vexpr = build_fold_addr_expr (nv);
6429 : 19 : SET_DECL_VALUE_EXPR (new_vard, vexpr);
6430 : 19 : lower_omp (&tseq, ctx);
6431 : 19 : SET_DECL_VALUE_EXPR (new_vard, x);
6432 : 19 : gimple_seq_add_seq (ilist, tseq);
6433 : 19 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6434 : : }
6435 : :
6436 : 38 : x = lang_hooks.decls.omp_clause_dtor (c, nv);
6437 : 38 : if (x)
6438 : 38 : gimplify_and_add (x, dlist);
6439 : : }
6440 : :
6441 : 78 : tree ref = build_outer_var_ref (var, ctx);
6442 : 78 : x = unshare_expr (ivar);
6443 : 78 : x = lang_hooks.decls.omp_clause_assign_op (c, x,
6444 : : ref);
6445 : 78 : gimplify_and_add (x, &llist[0]);
6446 : :
6447 : 78 : ref = build_outer_var_ref (var, ctx);
6448 : 78 : x = lang_hooks.decls.omp_clause_assign_op (c, ref,
6449 : : rvar);
6450 : 78 : gimplify_and_add (x, &llist[3]);
6451 : :
6452 : 78 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6453 : 78 : if (new_vard == new_var)
6454 : 40 : SET_DECL_VALUE_EXPR (new_var, lvar);
6455 : : else
6456 : 38 : SET_DECL_VALUE_EXPR (new_vard,
6457 : : build_fold_addr_expr (lvar));
6458 : :
6459 : 78 : x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6460 : 78 : if (x)
6461 : 38 : gimplify_and_add (x, &llist[1]);
6462 : :
6463 : 78 : tree ivar2 = unshare_expr (lvar);
6464 : 78 : TREE_OPERAND (ivar2, 1) = sctx.idx;
6465 : 78 : x = lang_hooks.decls.omp_clause_dtor (c, ivar2);
6466 : 78 : if (x)
6467 : 38 : gimplify_and_add (x, &llist[1]);
6468 : :
6469 : 78 : if (rvar2)
6470 : : {
6471 : 36 : x = lang_hooks.decls.omp_clause_dtor (c, rvar2);
6472 : 36 : if (x)
6473 : 16 : gimplify_and_add (x, &llist[1]);
6474 : : }
6475 : : break;
6476 : : }
6477 : 80 : if (x)
6478 : 34 : gimplify_and_add (x, &llist[0]);
6479 : 80 : if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6480 : : {
6481 : 76 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6482 : 76 : lower_omp (&tseq, ctx);
6483 : 76 : gimple_seq_add_seq (&llist[0], tseq);
6484 : : }
6485 : 80 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6486 : 80 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
6487 : 80 : lower_omp (&tseq, ctx);
6488 : 80 : gimple_seq_add_seq (&llist[1], tseq);
6489 : 80 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6490 : 80 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6491 : 80 : if (new_vard == new_var)
6492 : 70 : SET_DECL_VALUE_EXPR (new_var, lvar);
6493 : : else
6494 : 10 : SET_DECL_VALUE_EXPR (new_vard,
6495 : : build_fold_addr_expr (lvar));
6496 : 80 : x = lang_hooks.decls.omp_clause_dtor (c, ivar);
6497 : 80 : if (x)
6498 : 38 : 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 : 1266 : else if (omp_privatize_by_reference (var) && is_simd)
6507 : 56 : handle_simd_reference (clause_loc, new_vard, ilist);
6508 : :
6509 : 1266 : tree lab2 = NULL_TREE;
6510 : 1266 : if (cond)
6511 : : {
6512 : 294 : gimple *g;
6513 : 294 : if (!is_parallel_ctx (ctx))
6514 : : {
6515 : 278 : tree condv = create_tmp_var (boolean_type_node);
6516 : 278 : tree m = build_simple_mem_ref (cond);
6517 : 278 : g = gimple_build_assign (condv, m);
6518 : 278 : gimple_seq_add_stmt (ilist, g);
6519 : 278 : tree lab1
6520 : 278 : = create_artificial_label (UNKNOWN_LOCATION);
6521 : 278 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
6522 : 278 : g = gimple_build_cond (NE_EXPR, condv,
6523 : : boolean_false_node,
6524 : : lab2, lab1);
6525 : 278 : gimple_seq_add_stmt (ilist, g);
6526 : 278 : gimple_seq_add_stmt (ilist,
6527 : 278 : gimple_build_label (lab1));
6528 : : }
6529 : 294 : g = gimple_build_assign (build_simple_mem_ref (cond),
6530 : : boolean_true_node);
6531 : 294 : gimple_seq_add_stmt (ilist, g);
6532 : : }
6533 : 294 : x = lang_hooks.decls.omp_clause_default_ctor
6534 : 1266 : (c, unshare_expr (new_var),
6535 : : cond ? NULL_TREE
6536 : 972 : : build_outer_var_ref (var, ctx));
6537 : 1266 : if (x)
6538 : 285 : gimplify_and_add (x, ilist);
6539 : :
6540 : 1266 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6541 : 1266 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
6542 : : {
6543 : 158 : if (ctx->for_simd_scan_phase)
6544 : 972 : goto do_dtor;
6545 : 142 : if (x || (!is_simd
6546 : 32 : && OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c)))
6547 : : {
6548 : 70 : tree nv = create_tmp_var_raw (TREE_TYPE (new_var));
6549 : 70 : gimple_add_tmp_var (nv);
6550 : 70 : ctx->cb.decl_map->put (new_vard, nv);
6551 : 70 : x = lang_hooks.decls.omp_clause_default_ctor
6552 : 70 : (c, nv, build_outer_var_ref (var, ctx));
6553 : 70 : if (x)
6554 : 70 : gimplify_and_add (x, ilist);
6555 : 70 : if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6556 : : {
6557 : 35 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6558 : 35 : tree vexpr = nv;
6559 : 35 : if (new_vard != new_var)
6560 : 19 : vexpr = build_fold_addr_expr (nv);
6561 : 35 : SET_DECL_VALUE_EXPR (new_vard, vexpr);
6562 : 35 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6563 : 35 : lower_omp (&tseq, ctx);
6564 : 35 : SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
6565 : 35 : DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
6566 : 35 : gimple_seq_add_seq (ilist, tseq);
6567 : : }
6568 : 70 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6569 : 70 : if (is_simd && ctx->scan_exclusive)
6570 : : {
6571 : 16 : tree nv2
6572 : 16 : = create_tmp_var_raw (TREE_TYPE (new_var));
6573 : 16 : gimple_add_tmp_var (nv2);
6574 : 16 : ctx->cb.decl_map->put (nv, nv2);
6575 : 16 : x = lang_hooks.decls.omp_clause_default_ctor
6576 : 16 : (c, nv2, build_outer_var_ref (var, ctx));
6577 : 16 : gimplify_and_add (x, ilist);
6578 : 16 : x = lang_hooks.decls.omp_clause_dtor (c, nv2);
6579 : 16 : if (x)
6580 : 16 : gimplify_and_add (x, dlist);
6581 : : }
6582 : 70 : x = lang_hooks.decls.omp_clause_dtor (c, nv);
6583 : 70 : if (x)
6584 : 70 : gimplify_and_add (x, dlist);
6585 : : }
6586 : 72 : else if (is_simd
6587 : 40 : && ctx->scan_exclusive
6588 : 92 : && TREE_ADDRESSABLE (TREE_TYPE (new_var)))
6589 : : {
6590 : 0 : tree nv2 = create_tmp_var_raw (TREE_TYPE (new_var));
6591 : 0 : gimple_add_tmp_var (nv2);
6592 : 0 : ctx->cb.decl_map->put (new_vard, nv2);
6593 : 0 : x = lang_hooks.decls.omp_clause_dtor (c, nv2);
6594 : 0 : if (x)
6595 : 0 : gimplify_and_add (x, dlist);
6596 : : }
6597 : 142 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6598 : 142 : goto do_dtor;
6599 : : }
6600 : :
6601 : 1108 : if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
6602 : : {
6603 : 1060 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
6604 : 1060 : if (c_kind == OMP_CLAUSE_IN_REDUCTION
6605 : 1060 : && is_omp_target (ctx->stmt))
6606 : : {
6607 : 12 : tree d = maybe_lookup_decl_in_outer_ctx (var, ctx);
6608 : 12 : tree oldv = NULL_TREE;
6609 : 12 : gcc_assert (d);
6610 : 12 : if (DECL_HAS_VALUE_EXPR_P (d))
6611 : 6 : oldv = DECL_VALUE_EXPR (d);
6612 : 12 : SET_DECL_VALUE_EXPR (d, new_vard);
6613 : 12 : DECL_HAS_VALUE_EXPR_P (d) = 1;
6614 : 12 : lower_omp (&tseq, ctx);
6615 : 12 : if (oldv)
6616 : 6 : SET_DECL_VALUE_EXPR (d, oldv);
6617 : : else
6618 : : {
6619 : 6 : SET_DECL_VALUE_EXPR (d, NULL_TREE);
6620 : 6 : DECL_HAS_VALUE_EXPR_P (d) = 0;
6621 : : }
6622 : : }
6623 : : else
6624 : 1048 : lower_omp (&tseq, ctx);
6625 : 1060 : gimple_seq_add_seq (ilist, tseq);
6626 : : }
6627 : 1108 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6628 : 1108 : if (is_simd)
6629 : : {
6630 : 43 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
6631 : 43 : lower_omp (&tseq, ctx);
6632 : 43 : gimple_seq_add_seq (dlist, tseq);
6633 : 43 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6634 : : }
6635 : 1108 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
6636 : 1108 : if (cond)
6637 : : {
6638 : 294 : if (lab2)
6639 : 278 : gimple_seq_add_stmt (ilist, gimple_build_label (lab2));
6640 : : break;
6641 : : }
6642 : 814 : goto do_dtor;
6643 : : }
6644 : : else
6645 : : {
6646 : 7570 : x = omp_reduction_init (c, TREE_TYPE (new_var));
6647 : 7570 : gcc_assert (TREE_CODE (TREE_TYPE (new_var)) != ARRAY_TYPE);
6648 : 7570 : enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
6649 : :
6650 : 7570 : if (cond)
6651 : : {
6652 : 1431 : gimple *g;
6653 : 1431 : 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 : 1431 : if (initializer_zerop (x))
6659 : : {
6660 : 1250 : g = gimple_build_assign (build_simple_mem_ref (cond),
6661 : : boolean_true_node);
6662 : 1250 : gimple_seq_add_stmt (ilist, g);
6663 : 3207 : break;
6664 : : }
6665 : :
6666 : : /* Otherwise, emit
6667 : : if (!cond) { cond = true; new_var = x; } */
6668 : 181 : if (!is_parallel_ctx (ctx))
6669 : : {
6670 : 177 : tree condv = create_tmp_var (boolean_type_node);
6671 : 177 : tree m = build_simple_mem_ref (cond);
6672 : 177 : g = gimple_build_assign (condv, m);
6673 : 177 : gimple_seq_add_stmt (ilist, g);
6674 : 177 : tree lab1
6675 : 177 : = create_artificial_label (UNKNOWN_LOCATION);
6676 : 177 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
6677 : 177 : g = gimple_build_cond (NE_EXPR, condv,
6678 : : boolean_false_node,
6679 : : lab2, lab1);
6680 : 177 : gimple_seq_add_stmt (ilist, g);
6681 : 177 : gimple_seq_add_stmt (ilist,
6682 : 177 : gimple_build_label (lab1));
6683 : : }
6684 : 181 : g = gimple_build_assign (build_simple_mem_ref (cond),
6685 : : boolean_true_node);
6686 : 181 : gimple_seq_add_stmt (ilist, g);
6687 : 181 : gimplify_assign (new_var, x, ilist);
6688 : 181 : if (lab2)
6689 : 177 : 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 : 6139 : if (code == MINUS_EXPR)
6696 : 25 : code = PLUS_EXPR;
6697 : :
6698 : 6139 : bool is_truth_op
6699 : 6139 : = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
6700 : 6139 : tree new_vard = new_var;
6701 : 6139 : if (is_simd && omp_privatize_by_reference (var))
6702 : : {
6703 : 72 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
6704 : 72 : new_vard = TREE_OPERAND (new_var, 0);
6705 : 72 : gcc_assert (DECL_P (new_vard));
6706 : : }
6707 : 6139 : tree rvar = NULL_TREE, *rvarp = NULL, rvar2 = NULL_TREE;
6708 : 6139 : if (is_simd
6709 : 2092 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6710 : 8231 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
6711 : : rvarp = &rvar;
6712 : 6139 : if (is_simd
6713 : 6139 : && lower_rec_simd_input_clauses (new_var, ctx, &sctx,
6714 : : ivar, lvar, rvarp,
6715 : : &rvar2))
6716 : : {
6717 : 872 : if (new_vard != new_var)
6718 : : {
6719 : 44 : SET_DECL_VALUE_EXPR (new_vard,
6720 : : build_fold_addr_expr (lvar));
6721 : 44 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
6722 : : }
6723 : :
6724 : 872 : tree ref = build_outer_var_ref (var, ctx);
6725 : :
6726 : 872 : if (rvarp)
6727 : : {
6728 : 193 : if (ctx->for_simd_scan_phase)
6729 : : break;
6730 : 154 : gimplify_assign (ivar, ref, &llist[0]);
6731 : 154 : ref = build_outer_var_ref (var, ctx);
6732 : 154 : gimplify_assign (ref, rvar, &llist[3]);
6733 : 154 : break;
6734 : : }
6735 : :
6736 : 679 : gimplify_assign (unshare_expr (ivar), x, &llist[0]);
6737 : :
6738 : 679 : if (sctx.is_simt)
6739 : : {
6740 : 0 : if (!simt_lane)
6741 : 0 : simt_lane = create_tmp_var (unsigned_type_node);
6742 : 0 : x = build_call_expr_internal_loc
6743 : 0 : (UNKNOWN_LOCATION, IFN_GOMP_SIMT_XCHG_BFLY,
6744 : 0 : TREE_TYPE (ivar), 2, ivar, simt_lane);
6745 : : /* Make sure x is evaluated unconditionally. */
6746 : 0 : tree bfly_var = create_tmp_var (TREE_TYPE (ivar));
6747 : 0 : gimplify_assign (bfly_var, x, &llist[2]);
6748 : 0 : x = build2 (code, TREE_TYPE (ivar), ivar, bfly_var);
6749 : 0 : gimplify_assign (ivar, x, &llist[2]);
6750 : : }
6751 : 679 : tree ivar2 = ivar;
6752 : 679 : tree ref2 = ref;
6753 : 679 : if (is_truth_op)
6754 : : {
6755 : 99 : tree zero = build_zero_cst (TREE_TYPE (ivar));
6756 : 99 : ivar2 = fold_build2_loc (clause_loc, NE_EXPR,
6757 : : boolean_type_node, ivar,
6758 : : zero);
6759 : 99 : ref2 = fold_build2_loc (clause_loc, NE_EXPR,
6760 : : boolean_type_node, ref,
6761 : : zero);
6762 : : }
6763 : 679 : x = build2 (code, TREE_TYPE (ref), ref2, ivar2);
6764 : 679 : if (is_truth_op)
6765 : 99 : x = fold_convert (TREE_TYPE (ref), x);
6766 : 679 : ref = build_outer_var_ref (var, ctx);
6767 : 679 : gimplify_assign (ref, x, &llist[1]);
6768 : :
6769 : : }
6770 : : else
6771 : : {
6772 : 5267 : lower_private_allocate (var, new_var, allocator,
6773 : : allocate_ptr, ilist, ctx,
6774 : : false, NULL_TREE);
6775 : 5267 : if (omp_privatize_by_reference (var) && is_simd)
6776 : 28 : handle_simd_reference (clause_loc, new_vard, ilist);
6777 : 5267 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6778 : 5267 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
6779 : : break;
6780 : 4934 : gimplify_assign (new_var, x, ilist);
6781 : 4934 : if (is_simd)
6782 : : {
6783 : 1028 : tree ref = build_outer_var_ref (var, ctx);
6784 : 1028 : tree new_var2 = new_var;
6785 : 1028 : tree ref2 = ref;
6786 : 1028 : if (is_truth_op)
6787 : : {
6788 : 24 : tree zero = build_zero_cst (TREE_TYPE (new_var));
6789 : 24 : new_var2
6790 : 24 : = fold_build2_loc (clause_loc, NE_EXPR,
6791 : : boolean_type_node, new_var,
6792 : : zero);
6793 : 24 : ref2 = fold_build2_loc (clause_loc, NE_EXPR,
6794 : : boolean_type_node, ref,
6795 : : zero);
6796 : : }
6797 : 1028 : x = build2 (code, TREE_TYPE (ref2), ref2, new_var2);
6798 : 1028 : if (is_truth_op)
6799 : 24 : x = fold_convert (TREE_TYPE (new_var), x);
6800 : 1028 : ref = build_outer_var_ref (var, ctx);
6801 : 1028 : gimplify_assign (ref, x, dlist);
6802 : : }
6803 : 4934 : if (allocator)
6804 : 70 : goto do_dtor;
6805 : : }
6806 : : }
6807 : 5543 : break;
6808 : :
6809 : 0 : default:
6810 : 0 : gcc_unreachable ();
6811 : : }
6812 : : }
6813 : : }
6814 : 77159 : if (tskred_avar)
6815 : : {
6816 : 867 : tree clobber = build_clobber (TREE_TYPE (tskred_avar));
6817 : 867 : gimple_seq_add_stmt (ilist, gimple_build_assign (tskred_avar, clobber));
6818 : : }
6819 : :
6820 : 77159 : if (known_eq (sctx.max_vf, 1U))
6821 : : {
6822 : 1768 : sctx.is_simt = false;
6823 : 1768 : if (ctx->lastprivate_conditional_map)
6824 : : {
6825 : 52 : if (gimple_omp_for_combined_into_p (ctx->stmt))
6826 : : {
6827 : : /* Signal to lower_omp_1 that it should use parent context. */
6828 : 43 : ctx->combined_into_simd_safelen1 = true;
6829 : 370 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6830 : 327 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6831 : 327 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
6832 : : {
6833 : 57 : tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
6834 : 57 : omp_context *outer = ctx->outer;
6835 : 57 : if (gimple_code (outer->stmt) == GIMPLE_OMP_SCAN)
6836 : 6 : outer = outer->outer;
6837 : 57 : tree *v = ctx->lastprivate_conditional_map->get (o);
6838 : 57 : tree po = lookup_decl (OMP_CLAUSE_DECL (c), outer);
6839 : 57 : tree *pv = outer->lastprivate_conditional_map->get (po);
6840 : 57 : *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 : 9 : delete ctx->lastprivate_conditional_map;
6849 : 9 : ctx->lastprivate_conditional_map = NULL;
6850 : : }
6851 : : }
6852 : : }
6853 : :
6854 : 77159 : if (nonconst_simd_if)
6855 : : {
6856 : 625 : if (sctx.lane == NULL_TREE)
6857 : : {
6858 : 570 : sctx.idx = create_tmp_var (unsigned_type_node);
6859 : 570 : sctx.lane = create_tmp_var (unsigned_type_node);
6860 : : }
6861 : : /* FIXME: For now. */
6862 : 625 : sctx.is_simt = false;
6863 : : }
6864 : :
6865 : 77159 : if (sctx.lane || sctx.is_simt)
6866 : : {
6867 : 4012 : 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 : 4012 : suppress_warning (uid, OPT_Wuninitialized);
6871 : 4012 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SIMDUID_);
6872 : 4012 : OMP_CLAUSE__SIMDUID__DECL (c) = uid;
6873 : 4012 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
6874 : 4012 : 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 : 77159 : if (sctx.is_simt)
6879 : : {
6880 : 0 : sctx.simt_eargs[0] = uid;
6881 : 0 : gimple *g
6882 : 0 : = gimple_build_call_internal_vec (IFN_GOMP_SIMT_ENTER, sctx.simt_eargs);
6883 : 0 : gimple_call_set_lhs (g, uid);
6884 : 0 : gimple_seq_add_stmt (ilist, g);
6885 : 0 : sctx.simt_eargs.release ();
6886 : :
6887 : 0 : simtrec = create_tmp_var (ptr_type_node, ".omp_simt");
6888 : 0 : g = gimple_build_call_internal (IFN_GOMP_SIMT_ENTER_ALLOC, 1, uid);
6889 : 0 : gimple_call_set_lhs (g, simtrec);
6890 : 0 : gimple_seq_add_stmt (ilist, g);
6891 : : }
6892 : 77159 : if (sctx.lane)
6893 : : {
6894 : 7399 : 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 : 4012 : gimple_call_set_lhs (g, sctx.lane);
6899 : 4012 : gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (ctx->stmt));
6900 : 4012 : gsi_insert_before_without_update (&gsi, g, GSI_SAME_STMT);
6901 : 4012 : g = gimple_build_assign (sctx.lane, INTEGER_CST,
6902 : 4012 : build_int_cst (unsigned_type_node, 0));
6903 : 4012 : gimple_seq_add_stmt (ilist, g);
6904 : 4012 : if (sctx.lastlane)
6905 : : {
6906 : 184 : g = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
6907 : : 2, uid, sctx.lane);
6908 : 184 : gimple_call_set_lhs (g, sctx.lastlane);
6909 : 184 : gimple_seq_add_stmt (dlist, g);
6910 : 184 : gimple_seq_add_seq (dlist, llist[3]);
6911 : : }
6912 : : /* Emit reductions across SIMT lanes in log_2(simt_vf) steps. */
6913 : 4012 : if (llist[2])
6914 : : {
6915 : 0 : tree simt_vf = create_tmp_var (unsigned_type_node);
6916 : 0 : g = gimple_build_call_internal (IFN_GOMP_SIMT_VF, 0);
6917 : 0 : gimple_call_set_lhs (g, simt_vf);
6918 : 0 : gimple_seq_add_stmt (dlist, g);
6919 : :
6920 : 0 : tree t = build_int_cst (unsigned_type_node, 1);
6921 : 0 : g = gimple_build_assign (simt_lane, INTEGER_CST, t);
6922 : 0 : gimple_seq_add_stmt (dlist, g);
6923 : :
6924 : 0 : t = build_int_cst (unsigned_type_node, 0);
6925 : 0 : g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
6926 : 0 : gimple_seq_add_stmt (dlist, g);
6927 : :
6928 : 0 : tree body = create_artificial_label (UNKNOWN_LOCATION);
6929 : 0 : tree header = create_artificial_label (UNKNOWN_LOCATION);
6930 : 0 : tree end = create_artificial_label (UNKNOWN_LOCATION);
6931 : 0 : gimple_seq_add_stmt (dlist, gimple_build_goto (header));
6932 : 0 : gimple_seq_add_stmt (dlist, gimple_build_label (body));
6933 : :
6934 : 0 : gimple_seq_add_seq (dlist, llist[2]);
6935 : :
6936 : 0 : g = gimple_build_assign (simt_lane, LSHIFT_EXPR, simt_lane, integer_one_node);
6937 : 0 : gimple_seq_add_stmt (dlist, g);
6938 : :
6939 : 0 : gimple_seq_add_stmt (dlist, gimple_build_label (header));
6940 : 0 : g = gimple_build_cond (LT_EXPR, simt_lane, simt_vf, body, end);
6941 : 0 : gimple_seq_add_stmt (dlist, g);
6942 : :
6943 : 0 : gimple_seq_add_stmt (dlist, gimple_build_label (end));
6944 : : }
6945 : 12036 : for (int i = 0; i < 2; i++)
6946 : 8024 : if (llist[i])
6947 : : {
6948 : 1726 : tree vf = create_tmp_var (unsigned_type_node);
6949 : 1726 : g = gimple_build_call_internal (IFN_GOMP_SIMD_VF, 1, uid);
6950 : 1726 : gimple_call_set_lhs (g, vf);
6951 : 1726 : gimple_seq *seq = i == 0 ? ilist : dlist;
6952 : 1726 : gimple_seq_add_stmt (seq, g);
6953 : 1726 : tree t = build_int_cst (unsigned_type_node, 0);
6954 : 1726 : g = gimple_build_assign (sctx.idx, INTEGER_CST, t);
6955 : 1726 : gimple_seq_add_stmt (seq, g);
6956 : 1726 : tree body = create_artificial_label (UNKNOWN_LOCATION);
6957 : 1726 : tree header = create_artificial_label (UNKNOWN_LOCATION);
6958 : 1726 : tree end = create_artificial_label (UNKNOWN_LOCATION);
6959 : 1726 : gimple_seq_add_stmt (seq, gimple_build_goto (header));
6960 : 1726 : gimple_seq_add_stmt (seq, gimple_build_label (body));
6961 : 1726 : gimple_seq_add_seq (seq, llist[i]);
6962 : 1726 : t = build_int_cst (unsigned_type_node, 1);
6963 : 1726 : g = gimple_build_assign (sctx.idx, PLUS_EXPR, sctx.idx, t);
6964 : 1726 : gimple_seq_add_stmt (seq, g);
6965 : 1726 : gimple_seq_add_stmt (seq, gimple_build_label (header));
6966 : 1726 : g = gimple_build_cond (LT_EXPR, sctx.idx, vf, body, end);
6967 : 1726 : gimple_seq_add_stmt (seq, g);
6968 : 1726 : gimple_seq_add_stmt (seq, gimple_build_label (end));
6969 : : }
6970 : : }
6971 : 77159 : if (sctx.is_simt)
6972 : : {
6973 : 0 : gimple_seq_add_seq (dlist, sctx.simt_dlist);
6974 : 0 : gimple *g
6975 : 0 : = gimple_build_call_internal (IFN_GOMP_SIMT_EXIT, 1, simtrec);
6976 : 0 : 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 : 77159 : if (copyin_seq)
6983 : : {
6984 : 436 : x = build_call_expr (builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM),
6985 : : 0);
6986 : 436 : x = build2 (NE_EXPR, boolean_type_node, x,
6987 : 436 : build_int_cst (TREE_TYPE (x), 0));
6988 : 436 : x = build3 (COND_EXPR, void_type_node, x, copyin_seq, NULL);
6989 : 436 : 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 : 77159 : if (copyin_by_ref || lastprivate_firstprivate
6999 : 75232 : || (reduction_omp_orig_ref
7000 : 89 : && !ctx->scan_inclusive
7001 : 89 : && !ctx->scan_exclusive))
7002 : : {
7003 : : /* Don't add any barrier for #pragma omp simd or
7004 : : #pragma omp distribute. */
7005 : 2016 : if (!is_task_ctx (ctx)
7006 : 2016 : && (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR
7007 : 1482 : || gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_FOR))
7008 : 882 : 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 : 77159 : if (maybe_ne (sctx.max_vf, 0U))
7014 : : {
7015 : 5210 : tree c = omp_find_clause (gimple_omp_for_clauses (ctx->stmt),
7016 : : OMP_CLAUSE_SAFELEN);
7017 : 5210 : poly_uint64 safe_len;
7018 : 5210 : if (c == NULL_TREE
7019 : 5210 : || (poly_int_tree_p (OMP_CLAUSE_SAFELEN_EXPR (c), &safe_len)
7020 : 613 : && maybe_gt (safe_len, sctx.max_vf)))
7021 : : {
7022 : 5120 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
7023 : 5120 : OMP_CLAUSE_SAFELEN_EXPR (c) = build_int_cst (integer_type_node,
7024 : : sctx.max_vf);
7025 : 5120 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt);
7026 : 5120 : gimple_omp_for_set_clauses (ctx->stmt, c);
7027 : : }
7028 : : }
7029 : 77159 : }
7030 : :
7031 : : /* Create temporary variables for lastprivate(conditional:) implementation
7032 : : in context CTX with CLAUSES. */
7033 : :
7034 : : static void
7035 : 47291 : lower_lastprivate_conditional_clauses (tree *clauses, omp_context *ctx)
7036 : : {
7037 : 47291 : tree iter_type = NULL_TREE;
7038 : 47291 : tree cond_ptr = NULL_TREE;
7039 : 47291 : tree iter_var = NULL_TREE;
7040 : 47291 : bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7041 : 47291 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD);
7042 : 47291 : tree next = *clauses;
7043 : 226949 : for (tree c = *clauses; c; c = OMP_CLAUSE_CHAIN (c))
7044 : 179658 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7045 : 179658 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
7046 : : {
7047 : 386 : if (is_simd)
7048 : : {
7049 : 107 : tree cc = omp_find_clause (next, OMP_CLAUSE__CONDTEMP_);
7050 : 107 : gcc_assert (cc);
7051 : 107 : if (iter_type == NULL_TREE)
7052 : : {
7053 : 77 : iter_type = TREE_TYPE (OMP_CLAUSE_DECL (cc));
7054 : 77 : iter_var = create_tmp_var_raw (iter_type);
7055 : 77 : DECL_CONTEXT (iter_var) = current_function_decl;
7056 : 77 : DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
7057 : 77 : DECL_CHAIN (iter_var) = ctx->block_vars;
7058 : 77 : ctx->block_vars = iter_var;
7059 : 77 : tree c3
7060 : 77 : = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
7061 : 77 : OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
7062 : 77 : OMP_CLAUSE_DECL (c3) = iter_var;
7063 : 77 : OMP_CLAUSE_CHAIN (c3) = *clauses;
7064 : 77 : *clauses = c3;
7065 : 77 : ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
7066 : : }
7067 : 107 : next = OMP_CLAUSE_CHAIN (cc);
7068 : 107 : tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7069 : 107 : tree v = lookup_decl (OMP_CLAUSE_DECL (cc), ctx);
7070 : 107 : ctx->lastprivate_conditional_map->put (o, v);
7071 : 107 : continue;
7072 : 107 : }
7073 : 279 : if (iter_type == NULL)
7074 : : {
7075 : 205 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR)
7076 : : {
7077 : 193 : struct omp_for_data fd;
7078 : 193 : omp_extract_for_data (as_a <gomp_for *> (ctx->stmt), &fd,
7079 : : NULL);
7080 : 193 : iter_type = unsigned_type_for (fd.iter_type);
7081 : : }
7082 : 12 : else if (gimple_code (ctx->stmt) == GIMPLE_OMP_SECTIONS)
7083 : 12 : iter_type = unsigned_type_node;
7084 : 205 : tree c2 = omp_find_clause (*clauses, OMP_CLAUSE__CONDTEMP_);
7085 : 205 : if (c2)
7086 : : {
7087 : 103 : cond_ptr
7088 : 103 : = lookup_decl_in_outer_ctx (OMP_CLAUSE_DECL (c2), ctx);
7089 : 103 : OMP_CLAUSE_DECL (c2) = cond_ptr;
7090 : : }
7091 : : else
7092 : : {
7093 : 102 : cond_ptr = create_tmp_var_raw (build_pointer_type (iter_type));
7094 : 102 : DECL_CONTEXT (cond_ptr) = current_function_decl;
7095 : 102 : DECL_SEEN_IN_BIND_EXPR_P (cond_ptr) = 1;
7096 : 102 : DECL_CHAIN (cond_ptr) = ctx->block_vars;
7097 : 102 : ctx->block_vars = cond_ptr;
7098 : 102 : c2 = build_omp_clause (UNKNOWN_LOCATION,
7099 : : OMP_CLAUSE__CONDTEMP_);
7100 : 102 : OMP_CLAUSE_DECL (c2) = cond_ptr;
7101 : 102 : OMP_CLAUSE_CHAIN (c2) = *clauses;
7102 : 102 : *clauses = c2;
7103 : : }
7104 : 205 : iter_var = create_tmp_var_raw (iter_type);
7105 : 205 : DECL_CONTEXT (iter_var) = current_function_decl;
7106 : 205 : DECL_SEEN_IN_BIND_EXPR_P (iter_var) = 1;
7107 : 205 : DECL_CHAIN (iter_var) = ctx->block_vars;
7108 : 205 : ctx->block_vars = iter_var;
7109 : 205 : tree c3
7110 : 205 : = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
7111 : 205 : OMP_CLAUSE__CONDTEMP__ITER (c3) = 1;
7112 : 205 : OMP_CLAUSE_DECL (c3) = iter_var;
7113 : 205 : OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
7114 : 205 : OMP_CLAUSE_CHAIN (c2) = c3;
7115 : 205 : ctx->lastprivate_conditional_map = new hash_map<tree, tree>;
7116 : : }
7117 : 279 : tree v = create_tmp_var_raw (iter_type);
7118 : 279 : DECL_CONTEXT (v) = current_function_decl;
7119 : 279 : DECL_SEEN_IN_BIND_EXPR_P (v) = 1;
7120 : 279 : DECL_CHAIN (v) = ctx->block_vars;
7121 : 279 : ctx->block_vars = v;
7122 : 279 : tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7123 : 279 : ctx->lastprivate_conditional_map->put (o, v);
7124 : : }
7125 : 47291 : }
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 : 47291 : 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 : 47291 : tree x, c, label = NULL, orig_clauses = clauses;
7141 : 47291 : bool par_clauses = false;
7142 : 47291 : tree simduid = NULL, lastlane = NULL, simtcond = NULL, simtlast = NULL;
7143 : 47291 : unsigned HOST_WIDE_INT conditional_off = 0;
7144 : 47291 : gimple_seq post_stmt_list = NULL;
7145 : :
7146 : : /* Early exit if there are no lastprivate or linear clauses. */
7147 : 196225 : for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
7148 : 161416 : if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LASTPRIVATE
7149 : 161416 : || (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_LINEAR
7150 : 6773 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (clauses)))
7151 : : break;
7152 : 47291 : 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 : 34809 : if (is_parallel_ctx (ctx))
7158 : 30805 : return;
7159 : :
7160 : 34809 : ctx = ctx->outer;
7161 : 34809 : if (ctx == NULL || !is_parallel_ctx (ctx))
7162 : : return;
7163 : :
7164 : 12248 : clauses = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
7165 : : OMP_CLAUSE_LASTPRIVATE);
7166 : 12248 : if (clauses == NULL)
7167 : : return;
7168 : : par_clauses = true;
7169 : : }
7170 : :
7171 : 16486 : bool maybe_simt = false;
7172 : 16486 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7173 : 16486 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
7174 : : {
7175 : 7435 : maybe_simt = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMT_);
7176 : 7435 : simduid = omp_find_clause (orig_clauses, OMP_CLAUSE__SIMDUID_);
7177 : 7435 : if (simduid)
7178 : 3560 : simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
7179 : : }
7180 : :
7181 : 16486 : if (predicate)
7182 : : {
7183 : 16367 : gcond *stmt;
7184 : 16367 : tree label_true, arm1, arm2;
7185 : 16367 : enum tree_code pred_code = TREE_CODE (predicate);
7186 : :
7187 : 16367 : label = create_artificial_label (UNKNOWN_LOCATION);
7188 : 16367 : label_true = create_artificial_label (UNKNOWN_LOCATION);
7189 : 16367 : if (TREE_CODE_CLASS (pred_code) == tcc_comparison)
7190 : : {
7191 : 16367 : arm1 = TREE_OPERAND (predicate, 0);
7192 : 16367 : arm2 = TREE_OPERAND (predicate, 1);
7193 : 16367 : gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
7194 : 16367 : gimplify_expr (&arm2, stmt_list, NULL, is_gimple_val, fb_rvalue);
7195 : : }
7196 : : else
7197 : : {
7198 : 0 : arm1 = predicate;
7199 : 0 : gimplify_expr (&arm1, stmt_list, NULL, is_gimple_val, fb_rvalue);
7200 : 0 : arm2 = boolean_false_node;
7201 : 0 : pred_code = NE_EXPR;
7202 : : }
7203 : 16367 : if (maybe_simt)
7204 : : {
7205 : 0 : c = build2 (pred_code, boolean_type_node, arm1, arm2);
7206 : 0 : c = fold_convert (integer_type_node, c);
7207 : 0 : simtcond = create_tmp_var (integer_type_node);
7208 : 0 : gimplify_assign (simtcond, c, stmt_list);
7209 : 0 : gcall *g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY,
7210 : : 1, simtcond);
7211 : 0 : c = create_tmp_var (integer_type_node);
7212 : 0 : gimple_call_set_lhs (g, c);
7213 : 0 : gimple_seq_add_stmt (stmt_list, g);
7214 : 0 : stmt = gimple_build_cond (NE_EXPR, c, integer_zero_node,
7215 : : label_true, label);
7216 : : }
7217 : : else
7218 : 16367 : stmt = gimple_build_cond (pred_code, arm1, arm2, label_true, label);
7219 : 16367 : gimple_seq_add_stmt (stmt_list, stmt);
7220 : 16367 : gimple_seq_add_stmt (stmt_list, gimple_build_label (label_true));
7221 : : }
7222 : :
7223 : 16486 : tree cond_ptr = NULL_TREE;
7224 : 61586 : for (c = clauses; c ;)
7225 : : {
7226 : 56354 : tree var, new_var;
7227 : 56354 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7228 : 56354 : gimple_seq *this_stmt_list = stmt_list;
7229 : 56354 : tree lab2 = NULL_TREE;
7230 : :
7231 : 56354 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7232 : 21804 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
7233 : 386 : && ctx->lastprivate_conditional_map
7234 : 56727 : && !ctx->combined_into_simd_safelen1)
7235 : : {
7236 : 316 : gcc_assert (body_p);
7237 : 316 : if (simduid)
7238 : 37 : goto next;
7239 : 279 : if (cond_ptr == NULL_TREE)
7240 : : {
7241 : 205 : cond_ptr = omp_find_clause (orig_clauses, OMP_CLAUSE__CONDTEMP_);
7242 : 205 : cond_ptr = OMP_CLAUSE_DECL (cond_ptr);
7243 : : }
7244 : 279 : tree type = TREE_TYPE (TREE_TYPE (cond_ptr));
7245 : 279 : tree o = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
7246 : 279 : tree v = *ctx->lastprivate_conditional_map->get (o);
7247 : 279 : gimplify_assign (v, build_zero_cst (type), body_p);
7248 : 279 : this_stmt_list = cstmt_list;
7249 : 279 : tree mem;
7250 : 279 : if (POINTER_TYPE_P (TREE_TYPE (cond_ptr)))
7251 : : {
7252 : 136 : mem = build2 (MEM_REF, type, cond_ptr,
7253 : 136 : build_int_cst (TREE_TYPE (cond_ptr),
7254 : : conditional_off));
7255 : 136 : conditional_off += tree_to_uhwi (TYPE_SIZE_UNIT (type));
7256 : : }
7257 : : else
7258 : 143 : mem = build4 (ARRAY_REF, type, cond_ptr,
7259 : 143 : size_int (conditional_off++), NULL_TREE, NULL_TREE);
7260 : 279 : tree mem2 = copy_node (mem);
7261 : 279 : gimple_seq seq = NULL;
7262 : 279 : mem = force_gimple_operand (mem, &seq, true, NULL_TREE);
7263 : 279 : gimple_seq_add_seq (this_stmt_list, seq);
7264 : 279 : tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
7265 : 279 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
7266 : 279 : gimple *g = gimple_build_cond (GT_EXPR, v, mem, lab1, lab2);
7267 : 279 : gimple_seq_add_stmt (this_stmt_list, g);
7268 : 279 : gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab1));
7269 : 279 : gimplify_assign (mem2, v, this_stmt_list);
7270 : : }
7271 : 56038 : else if (predicate
7272 : 55800 : && ctx->combined_into_simd_safelen1
7273 : 116 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7274 : 57 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
7275 : 56095 : && ctx->lastprivate_conditional_map)
7276 : : this_stmt_list = &post_stmt_list;
7277 : :
7278 : 56317 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7279 : 56317 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7280 : 4875 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7281 : : {
7282 : 26642 : var = OMP_CLAUSE_DECL (c);
7283 : 26642 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7284 : 21767 : && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7285 : 27666 : && is_taskloop_ctx (ctx))
7286 : : {
7287 : 309 : gcc_checking_assert (ctx->outer && is_task_ctx (ctx->outer));
7288 : 309 : new_var = lookup_decl (var, ctx->outer);
7289 : : }
7290 : : else
7291 : : {
7292 : 26333 : new_var = lookup_decl (var, ctx);
7293 : : /* Avoid uninitialized warnings for lastprivate and
7294 : : for linear iterators. */
7295 : 26333 : if (predicate
7296 : 52477 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7297 : 4875 : || OMP_CLAUSE_LINEAR_NO_COPYIN (c)))
7298 : 24875 : suppress_warning (new_var, OPT_Wuninitialized);
7299 : : }
7300 : :
7301 : 26642 : if (!maybe_simt && simduid && DECL_HAS_VALUE_EXPR_P (new_var))
7302 : : {
7303 : 5872 : tree val = DECL_VALUE_EXPR (new_var);
7304 : 5872 : if (TREE_CODE (val) == ARRAY_REF
7305 : 5824 : && VAR_P (TREE_OPERAND (val, 0))
7306 : 11696 : && lookup_attribute ("omp simd array",
7307 : 5824 : DECL_ATTRIBUTES (TREE_OPERAND (val,
7308 : : 0))))
7309 : : {
7310 : 5824 : if (lastlane == NULL)
7311 : : {
7312 : 2763 : lastlane = create_tmp_var (unsigned_type_node);
7313 : 2763 : gcall *g
7314 : 2763 : = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE,
7315 : : 2, simduid,
7316 : 2763 : TREE_OPERAND (val, 1));
7317 : 2763 : gimple_call_set_lhs (g, lastlane);
7318 : 2763 : gimple_seq_add_stmt (this_stmt_list, g);
7319 : : }
7320 : 5824 : new_var = build4 (ARRAY_REF, TREE_TYPE (val),
7321 : 5824 : TREE_OPERAND (val, 0), lastlane,
7322 : : NULL_TREE, NULL_TREE);
7323 : 5824 : TREE_THIS_NOTRAP (new_var) = 1;
7324 : : }
7325 : : }
7326 : 20770 : else if (maybe_simt)
7327 : : {
7328 : 0 : tree val = (DECL_HAS_VALUE_EXPR_P (new_var)
7329 : 0 : ? DECL_VALUE_EXPR (new_var)
7330 : 0 : : new_var);
7331 : 0 : if (simtlast == NULL)
7332 : : {
7333 : 0 : simtlast = create_tmp_var (unsigned_type_node);
7334 : 0 : gcall *g = gimple_build_call_internal
7335 : 0 : (IFN_GOMP_SIMT_LAST_LANE, 1, simtcond);
7336 : 0 : gimple_call_set_lhs (g, simtlast);
7337 : 0 : gimple_seq_add_stmt (this_stmt_list, g);
7338 : : }
7339 : 0 : x = build_call_expr_internal_loc
7340 : 0 : (UNKNOWN_LOCATION, IFN_GOMP_SIMT_XCHG_IDX,
7341 : 0 : TREE_TYPE (val), 2, val, simtlast);
7342 : 0 : new_var = unshare_expr (new_var);
7343 : 0 : gimplify_assign (new_var, x, this_stmt_list);
7344 : 0 : new_var = unshare_expr (new_var);
7345 : : }
7346 : :
7347 : 26642 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7348 : 26642 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c))
7349 : : {
7350 : 7148 : lower_omp (&OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c), ctx);
7351 : 7148 : gimple_seq_add_seq (this_stmt_list,
7352 : 7148 : OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7353 : 7148 : OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) = NULL;
7354 : : }
7355 : 19494 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7356 : 19494 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c))
7357 : : {
7358 : 424 : lower_omp (&OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c), ctx);
7359 : 424 : gimple_seq_add_seq (this_stmt_list,
7360 : 424 : OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
7361 : 424 : OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) = NULL;
7362 : : }
7363 : :
7364 : 26642 : x = NULL_TREE;
7365 : 26642 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7366 : 21767 : && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
7367 : 26702 : && is_taskloop_ctx (ctx))
7368 : : {
7369 : 76 : tree ovar = maybe_lookup_decl_in_outer_ctx (var,
7370 : 38 : ctx->outer->outer);
7371 : 38 : if (is_global_var (ovar))
7372 : 7 : x = ovar;
7373 : : }
7374 : 7 : if (!x)
7375 : 26635 : x = build_outer_var_ref (var, ctx, OMP_CLAUSE_LASTPRIVATE);
7376 : 26642 : if (omp_privatize_by_reference (var))
7377 : 657 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7378 : 26642 : x = lang_hooks.decls.omp_clause_assign_op (c, x, new_var);
7379 : 26642 : gimplify_and_add (x, this_stmt_list);
7380 : :
7381 : 26642 : if (lab2)
7382 : 279 : gimple_seq_add_stmt (this_stmt_list, gimple_build_label (lab2));
7383 : : }
7384 : :
7385 : 56354 : next:
7386 : 56354 : c = OMP_CLAUSE_CHAIN (c);
7387 : 56354 : 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 : 12482 : if (is_parallel_ctx (ctx))
7393 : : break;
7394 : :
7395 : 12482 : ctx = ctx->outer;
7396 : 12482 : if (ctx == NULL || !is_parallel_ctx (ctx))
7397 : : break;
7398 : :
7399 : 1228 : c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
7400 : : OMP_CLAUSE_LASTPRIVATE);
7401 : 1228 : par_clauses = true;
7402 : : }
7403 : : }
7404 : :
7405 : 16486 : if (label)
7406 : 16367 : gimple_seq_add_stmt (stmt_list, gimple_build_label (label));
7407 : 16486 : 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 : 25942 : 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 : 25942 : gimple_seq before_fork = NULL;
7432 : 25942 : gimple_seq after_fork = NULL;
7433 : 25942 : gimple_seq before_join = NULL;
7434 : 25942 : gimple_seq after_join = NULL;
7435 : 25942 : tree init_code = NULL_TREE, fini_code = NULL_TREE,
7436 : 25942 : setup_code = NULL_TREE, teardown_code = NULL_TREE;
7437 : 25942 : unsigned offset = 0;
7438 : :
7439 : 96575 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
7440 : 70633 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7441 : : {
7442 : : /* No 'reduction' clauses on OpenACC 'kernels'. */
7443 : 7747 : gcc_checking_assert (!is_oacc_kernels (ctx));
7444 : : /* Likewise, on OpenACC 'kernels' decomposed parts. */
7445 : 7747 : gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
7446 : :
7447 : 7747 : tree orig = OMP_CLAUSE_DECL (c);
7448 : 7747 : tree var = maybe_lookup_decl (orig, ctx);
7449 : 7747 : tree ref_to_res = NULL_TREE;
7450 : 7747 : tree incoming, outgoing, v1, v2, v3;
7451 : 7747 : bool is_private = false;
7452 : :
7453 : 7747 : enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
7454 : 7747 : if (rcode == MINUS_EXPR)
7455 : : rcode = PLUS_EXPR;
7456 : 6605 : else if (rcode == TRUTH_ANDIF_EXPR)
7457 : : rcode = BIT_AND_EXPR;
7458 : 6427 : else if (rcode == TRUTH_ORIF_EXPR)
7459 : 433 : rcode = BIT_IOR_EXPR;
7460 : 7747 : tree op = build_int_cst (unsigned_type_node, rcode);
7461 : :
7462 : 7747 : if (!var)
7463 : 4 : var = orig;
7464 : :
7465 : 7747 : incoming = outgoing = var;
7466 : :
7467 : 7747 : if (!inner)
7468 : : {
7469 : : /* See if an outer construct also reduces this variable. */
7470 : : omp_context *outer = ctx;
7471 : :
7472 : 7383 : while (omp_context *probe = outer->outer)
7473 : : {
7474 : 4708 : enum gimple_code type = gimple_code (probe->stmt);
7475 : 4708 : tree cls;
7476 : :
7477 : 4708 : switch (type)
7478 : : {
7479 : 2220 : case GIMPLE_OMP_FOR:
7480 : 2220 : cls = gimple_omp_for_clauses (probe->stmt);
7481 : 2220 : break;
7482 : :
7483 : 2488 : case GIMPLE_OMP_TARGET:
7484 : : /* No 'reduction' clauses inside OpenACC 'kernels'
7485 : : regions. */
7486 : 2488 : gcc_checking_assert (!is_oacc_kernels (probe));
7487 : :
7488 : 2488 : if (!is_gimple_omp_offloaded (probe->stmt))
7489 : 27 : goto do_lookup;
7490 : :
7491 : 2461 : cls = gimple_omp_target_clauses (probe->stmt);
7492 : 2461 : break;
7493 : :
7494 : 0 : default:
7495 : 0 : goto do_lookup;
7496 : : }
7497 : :
7498 : 14251 : outer = probe;
7499 : 14251 : for (; cls; cls = OMP_CLAUSE_CHAIN (cls))
7500 : 11633 : if (OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_REDUCTION
7501 : 11633 : && orig == OMP_CLAUSE_DECL (cls))
7502 : : {
7503 : 1770 : incoming = outgoing = lookup_decl (orig, probe);
7504 : 1770 : goto has_outer_reduction;
7505 : : }
7506 : 9863 : else if ((OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_FIRSTPRIVATE
7507 : 8974 : || OMP_CLAUSE_CODE (cls) == OMP_CLAUSE_PRIVATE)
7508 : 11167 : && orig == OMP_CLAUSE_DECL (cls))
7509 : : {
7510 : 293 : is_private = true;
7511 : 293 : goto do_lookup;
7512 : : }
7513 : : }
7514 : :
7515 : 2675 : do_lookup:
7516 : : /* This is the outermost construct with this reduction,
7517 : : see if there's a mapping for it. */
7518 : 2995 : if (gimple_code (outer->stmt) == GIMPLE_OMP_TARGET
7519 : 5854 : && maybe_lookup_field (orig, outer) && !is_private)
7520 : : {
7521 : 2554 : ref_to_res = build_receiver_ref (orig, false, outer);
7522 : 2554 : if (omp_privatize_by_reference (orig))
7523 : 79 : ref_to_res = build_simple_mem_ref (ref_to_res);
7524 : :
7525 : 2554 : tree type = TREE_TYPE (var);
7526 : 2554 : if (POINTER_TYPE_P (type))
7527 : 79 : type = TREE_TYPE (type);
7528 : :
7529 : 2554 : outgoing = var;
7530 : 2554 : 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 : 441 : tree t = NULL_TREE;
7537 : 441 : omp_context *c = ctx->outer;
7538 : 893 : while (c && !t)
7539 : : {
7540 : 452 : t = maybe_lookup_decl (orig, c);
7541 : 452 : c = c->outer;
7542 : : }
7543 : 441 : incoming = outgoing = (t ? t : orig);
7544 : : }
7545 : :
7546 : 2554 : has_outer_reduction:;
7547 : : }
7548 : :
7549 : 4324 : if (!ref_to_res)
7550 : 5193 : ref_to_res = integer_zero_node;
7551 : :
7552 : 7747 : if (omp_privatize_by_reference (orig))
7553 : : {
7554 : 159 : tree type = TREE_TYPE (var);
7555 : 159 : const char *id = IDENTIFIER_POINTER (DECL_NAME (var));
7556 : :
7557 : 159 : if (!inner)
7558 : : {
7559 : 110 : tree x = create_tmp_var (TREE_TYPE (type), id);
7560 : 110 : gimplify_assign (var, build_fold_addr_expr (x), fork_seq);
7561 : : }
7562 : :
7563 : 159 : v1 = create_tmp_var (type, id);
7564 : 159 : v2 = create_tmp_var (type, id);
7565 : 159 : v3 = create_tmp_var (type, id);
7566 : :
7567 : 159 : gimplify_assign (v1, var, fork_seq);
7568 : 159 : gimplify_assign (v2, var, fork_seq);
7569 : 159 : gimplify_assign (v3, var, fork_seq);
7570 : :
7571 : 159 : var = build_simple_mem_ref (var);
7572 : 159 : v1 = build_simple_mem_ref (v1);
7573 : 159 : v2 = build_simple_mem_ref (v2);
7574 : 159 : v3 = build_simple_mem_ref (v3);
7575 : 159 : outgoing = build_simple_mem_ref (outgoing);
7576 : :
7577 : 159 : if (!TREE_CONSTANT (incoming))
7578 : 80 : 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 : 7747 : fixed_size_mode mode
7588 : 7747 : = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (var)));
7589 : 7747 : unsigned align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
7590 : 7747 : offset = (offset + align - 1) & ~(align - 1);
7591 : 7747 : tree off = build_int_cst (sizetype, offset);
7592 : 7747 : offset += GET_MODE_SIZE (mode);
7593 : :
7594 : 7747 : if (!init_code)
7595 : : {
7596 : 13930 : init_code = build_int_cst (integer_type_node,
7597 : 6965 : IFN_GOACC_REDUCTION_INIT);
7598 : 13930 : fini_code = build_int_cst (integer_type_node,
7599 : 6965 : IFN_GOACC_REDUCTION_FINI);
7600 : 13930 : setup_code = build_int_cst (integer_type_node,
7601 : 6965 : IFN_GOACC_REDUCTION_SETUP);
7602 : 6965 : teardown_code = build_int_cst (integer_type_node,
7603 : 6965 : IFN_GOACC_REDUCTION_TEARDOWN);
7604 : : }
7605 : :
7606 : 7747 : tree setup_call
7607 : 15494 : = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7608 : 7747 : TREE_TYPE (var), 6, setup_code,
7609 : : unshare_expr (ref_to_res),
7610 : : unshare_expr (incoming),
7611 : : level, op, off);
7612 : 7747 : tree init_call
7613 : 15494 : = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7614 : 7747 : TREE_TYPE (var), 6, init_code,
7615 : : unshare_expr (ref_to_res),
7616 : : unshare_expr (v1), level, op, off);
7617 : 7747 : tree fini_call
7618 : 15494 : = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7619 : 7747 : TREE_TYPE (var), 6, fini_code,
7620 : : unshare_expr (ref_to_res),
7621 : : unshare_expr (v2), level, op, off);
7622 : 7747 : tree teardown_call
7623 : 15494 : = build_call_expr_internal_loc (loc, IFN_GOACC_REDUCTION,
7624 : 7747 : TREE_TYPE (var), 6, teardown_code,
7625 : : ref_to_res, unshare_expr (v3),
7626 : : level, op, off);
7627 : :
7628 : 7747 : gimplify_assign (unshare_expr (v1), setup_call, &before_fork);
7629 : 7747 : gimplify_assign (unshare_expr (v2), init_call, &after_fork);
7630 : 7747 : gimplify_assign (unshare_expr (v3), fini_call, &before_join);
7631 : 7747 : gimplify_assign (unshare_expr (outgoing), teardown_call, &after_join);
7632 : : }
7633 : :
7634 : : /* Now stitch things together. */
7635 : 25942 : gimple_seq_add_seq (fork_seq, before_fork);
7636 : 25942 : if (private_marker)
7637 : 254 : gimple_seq_add_stmt (fork_seq, private_marker);
7638 : 25942 : if (fork)
7639 : 16557 : gimple_seq_add_stmt (fork_seq, fork);
7640 : 25942 : gimple_seq_add_seq (fork_seq, after_fork);
7641 : :
7642 : 25942 : gimple_seq_add_seq (join_seq, before_join);
7643 : 25942 : if (join)
7644 : 16557 : gimple_seq_add_stmt (join_seq, join);
7645 : 25942 : gimple_seq_add_seq (join_seq, after_join);
7646 : 25942 : }
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 : 71934 : lower_reduction_clauses (tree clauses, gimple_seq *stmt_seqp,
7656 : : gimple_seq *clist, omp_context *ctx)
7657 : : {
7658 : 71934 : gimple_seq sub_seq = NULL;
7659 : 71934 : gimple *stmt;
7660 : 71934 : tree x, c;
7661 : 71934 : int count = 0;
7662 : :
7663 : : /* OpenACC loop reductions are handled elsewhere. */
7664 : 71934 : if (is_gimple_omp_oacc (ctx->stmt))
7665 : 70837 : return;
7666 : :
7667 : : /* SIMD reductions are handled in lower_rec_input_clauses. */
7668 : 60593 : if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR
7669 : 60593 : && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD)
7670 : : return;
7671 : :
7672 : : /* inscan reductions are handled elsewhere. */
7673 : 51232 : 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 : 241873 : for (c = clauses; c && count < 2; c = OMP_CLAUSE_CHAIN (c))
7679 : 191734 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7680 : 191734 : && !OMP_CLAUSE_REDUCTION_TASK (c))
7681 : : {
7682 : 4629 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
7683 : 4629 : || 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 : 3709 : count++;
7690 : : }
7691 : :
7692 : 51059 : if (count == 0)
7693 : : return;
7694 : :
7695 : 15144 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7696 : : {
7697 : 14047 : tree var, ref, new_var, orig_var;
7698 : 14047 : enum tree_code code;
7699 : 14047 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7700 : :
7701 : 22653 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
7702 : 14047 : || OMP_CLAUSE_REDUCTION_TASK (c))
7703 : 8606 : continue;
7704 : :
7705 : 5441 : enum omp_clause_code ccode = OMP_CLAUSE_REDUCTION;
7706 : 5441 : orig_var = var = OMP_CLAUSE_DECL (c);
7707 : 5441 : if (TREE_CODE (var) == MEM_REF)
7708 : : {
7709 : 764 : var = TREE_OPERAND (var, 0);
7710 : 764 : if (TREE_CODE (var) == POINTER_PLUS_EXPR)
7711 : 50 : var = TREE_OPERAND (var, 0);
7712 : 764 : if (TREE_CODE (var) == ADDR_EXPR)
7713 : 322 : 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 : 442 : ccode = OMP_CLAUSE_PRIVATE;
7721 : 442 : if (INDIRECT_REF_P (var))
7722 : 72 : var = TREE_OPERAND (var, 0);
7723 : : }
7724 : 764 : orig_var = var;
7725 : 764 : if (is_variable_sized (var))
7726 : : {
7727 : 25 : gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
7728 : 25 : var = DECL_VALUE_EXPR (var);
7729 : 25 : gcc_assert (INDIRECT_REF_P (var));
7730 : 25 : var = TREE_OPERAND (var, 0);
7731 : 25 : gcc_assert (DECL_P (var));
7732 : : }
7733 : : }
7734 : 5441 : new_var = lookup_decl (var, ctx);
7735 : 5441 : if (var == OMP_CLAUSE_DECL (c)
7736 : 5441 : && omp_privatize_by_reference (var))
7737 : 161 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7738 : 5441 : ref = build_outer_var_ref (var, ctx, ccode);
7739 : 5441 : code = OMP_CLAUSE_REDUCTION_CODE (c);
7740 : :
7741 : : /* reduction(-:var) sums up the partial results, so it acts
7742 : : identically to reduction(+:var). */
7743 : 5441 : if (code == MINUS_EXPR)
7744 : 49 : code = PLUS_EXPR;
7745 : :
7746 : 5441 : bool is_truth_op = (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR);
7747 : 5441 : if (count == 1)
7748 : : {
7749 : 3300 : tree addr = build_fold_addr_expr_loc (clause_loc, ref);
7750 : :
7751 : 3300 : addr = save_expr (addr);
7752 : 3300 : ref = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (addr)), addr);
7753 : 3300 : tree new_var2 = new_var;
7754 : 3300 : tree ref2 = ref;
7755 : 3300 : if (is_truth_op)
7756 : : {
7757 : 784 : tree zero = build_zero_cst (TREE_TYPE (new_var));
7758 : 784 : new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
7759 : : boolean_type_node, new_var, zero);
7760 : 784 : ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
7761 : : ref, zero);
7762 : : }
7763 : 3300 : x = fold_build2_loc (clause_loc, code, TREE_TYPE (new_var2), ref2,
7764 : : new_var2);
7765 : 3300 : if (is_truth_op)
7766 : 784 : x = fold_convert (TREE_TYPE (new_var), x);
7767 : 3300 : x = build2 (OMP_ATOMIC, void_type_node, addr, x);
7768 : 3300 : OMP_ATOMIC_MEMORY_ORDER (x) = OMP_MEMORY_ORDER_RELAXED;
7769 : 3300 : gimplify_and_add (x, stmt_seqp);
7770 : 3300 : return;
7771 : : }
7772 : 2141 : else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
7773 : : {
7774 : 764 : tree d = OMP_CLAUSE_DECL (c);
7775 : 764 : tree type = TREE_TYPE (d);
7776 : 764 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7777 : 764 : tree i = create_tmp_var (TREE_TYPE (v));
7778 : 764 : tree ptype = build_pointer_type (TREE_TYPE (type));
7779 : 764 : tree bias = TREE_OPERAND (d, 1);
7780 : 764 : d = TREE_OPERAND (d, 0);
7781 : 764 : if (TREE_CODE (d) == POINTER_PLUS_EXPR)
7782 : : {
7783 : 50 : tree b = TREE_OPERAND (d, 1);
7784 : 50 : b = maybe_lookup_decl (b, ctx);
7785 : 50 : if (b == NULL)
7786 : : {
7787 : 2 : b = TREE_OPERAND (d, 1);
7788 : 2 : b = maybe_lookup_decl_in_outer_ctx (b, ctx);
7789 : : }
7790 : 50 : if (integer_zerop (bias))
7791 : : bias = b;
7792 : : else
7793 : : {
7794 : 0 : bias = fold_convert_loc (clause_loc, TREE_TYPE (b), bias);
7795 : 0 : bias = fold_build2_loc (clause_loc, PLUS_EXPR,
7796 : 0 : TREE_TYPE (b), b, bias);
7797 : : }
7798 : 50 : 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 : 764 : if (INDIRECT_REF_P (d))
7803 : : {
7804 : 72 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7805 : 72 : gcc_assert (omp_privatize_by_reference (var)
7806 : : && var == orig_var);
7807 : : }
7808 : 692 : else if (TREE_CODE (d) == ADDR_EXPR)
7809 : : {
7810 : 322 : if (orig_var == var)
7811 : : {
7812 : 297 : new_var = build_fold_addr_expr (new_var);
7813 : 297 : ref = build_fold_addr_expr (ref);
7814 : : }
7815 : : }
7816 : : else
7817 : : {
7818 : 370 : gcc_assert (orig_var == var);
7819 : 370 : if (omp_privatize_by_reference (var))
7820 : 106 : ref = build_fold_addr_expr (ref);
7821 : : }
7822 : 764 : if (DECL_P (v))
7823 : : {
7824 : 93 : tree t = maybe_lookup_decl (v, ctx);
7825 : 93 : if (t)
7826 : 88 : v = t;
7827 : : else
7828 : 5 : v = maybe_lookup_decl_in_outer_ctx (v, ctx);
7829 : 93 : gimplify_expr (&v, stmt_seqp, NULL, is_gimple_val, fb_rvalue);
7830 : : }
7831 : 764 : if (!integer_zerop (bias))
7832 : : {
7833 : 432 : bias = fold_convert_loc (clause_loc, sizetype, bias);
7834 : 864 : new_var = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
7835 : 432 : TREE_TYPE (new_var), new_var,
7836 : : unshare_expr (bias));
7837 : 432 : ref = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
7838 : 432 : TREE_TYPE (ref), ref, bias);
7839 : : }
7840 : 764 : new_var = fold_convert_loc (clause_loc, ptype, new_var);
7841 : 764 : ref = fold_convert_loc (clause_loc, ptype, ref);
7842 : 764 : tree m = create_tmp_var (ptype);
7843 : 764 : gimplify_assign (m, new_var, stmt_seqp);
7844 : 764 : new_var = m;
7845 : 764 : m = create_tmp_var (ptype);
7846 : 764 : gimplify_assign (m, ref, stmt_seqp);
7847 : 764 : ref = m;
7848 : 764 : gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), stmt_seqp);
7849 : 764 : tree body = create_artificial_label (UNKNOWN_LOCATION);
7850 : 764 : tree end = create_artificial_label (UNKNOWN_LOCATION);
7851 : 764 : gimple_seq_add_stmt (&sub_seq, gimple_build_label (body));
7852 : 764 : tree priv = build_simple_mem_ref_loc (clause_loc, new_var);
7853 : 764 : tree out = build_simple_mem_ref_loc (clause_loc, ref);
7854 : 764 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7855 : : {
7856 : 72 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
7857 : 72 : tree decl_placeholder
7858 : 72 : = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
7859 : 72 : SET_DECL_VALUE_EXPR (placeholder, out);
7860 : 72 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
7861 : 72 : SET_DECL_VALUE_EXPR (decl_placeholder, priv);
7862 : 72 : DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
7863 : 72 : lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
7864 : 72 : gimple_seq_add_seq (&sub_seq,
7865 : 72 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7866 : 72 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7867 : 72 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
7868 : 72 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
7869 : : }
7870 : : else
7871 : : {
7872 : 692 : tree out2 = out;
7873 : 692 : tree priv2 = priv;
7874 : 692 : if (is_truth_op)
7875 : : {
7876 : 0 : tree zero = build_zero_cst (TREE_TYPE (out));
7877 : 0 : out2 = fold_build2_loc (clause_loc, NE_EXPR,
7878 : : boolean_type_node, out, zero);
7879 : 0 : priv2 = fold_build2_loc (clause_loc, NE_EXPR,
7880 : : boolean_type_node, priv, zero);
7881 : : }
7882 : 692 : x = build2 (code, TREE_TYPE (out2), out2, priv2);
7883 : 692 : if (is_truth_op)
7884 : 0 : x = fold_convert (TREE_TYPE (out), x);
7885 : 692 : out = unshare_expr (out);
7886 : 692 : gimplify_assign (out, x, &sub_seq);
7887 : : }
7888 : 764 : gimple *g = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
7889 : 764 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
7890 : 764 : gimple_seq_add_stmt (&sub_seq, g);
7891 : 764 : g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
7892 : 764 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
7893 : 764 : gimple_seq_add_stmt (&sub_seq, g);
7894 : 764 : g = gimple_build_assign (i, PLUS_EXPR, i,
7895 : 764 : build_int_cst (TREE_TYPE (i), 1));
7896 : 764 : gimple_seq_add_stmt (&sub_seq, g);
7897 : 764 : g = gimple_build_cond (LE_EXPR, i, v, body, end);
7898 : 764 : gimple_seq_add_stmt (&sub_seq, g);
7899 : 764 : gimple_seq_add_stmt (&sub_seq, gimple_build_label (end));
7900 : : }
7901 : 1377 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7902 : : {
7903 : 771 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
7904 : :
7905 : 771 : if (omp_privatize_by_reference (var)
7906 : 828 : && !useless_type_conversion_p (TREE_TYPE (placeholder),
7907 : 57 : TREE_TYPE (ref)))
7908 : 49 : ref = build_fold_addr_expr_loc (clause_loc, ref);
7909 : 771 : SET_DECL_VALUE_EXPR (placeholder, ref);
7910 : 771 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
7911 : 771 : lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
7912 : 771 : gimple_seq_add_seq (&sub_seq, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7913 : 771 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7914 : 771 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
7915 : : }
7916 : : else
7917 : : {
7918 : 606 : tree new_var2 = new_var;
7919 : 606 : tree ref2 = ref;
7920 : 606 : if (is_truth_op)
7921 : : {
7922 : 78 : tree zero = build_zero_cst (TREE_TYPE (new_var));
7923 : 78 : new_var2 = fold_build2_loc (clause_loc, NE_EXPR,
7924 : : boolean_type_node, new_var, zero);
7925 : 78 : ref2 = fold_build2_loc (clause_loc, NE_EXPR, boolean_type_node,
7926 : : ref, zero);
7927 : : }
7928 : 606 : x = build2 (code, TREE_TYPE (ref), ref2, new_var2);
7929 : 606 : if (is_truth_op)
7930 : 78 : x = fold_convert (TREE_TYPE (new_var), x);
7931 : 606 : ref = build_outer_var_ref (var, ctx);
7932 : 606 : gimplify_assign (ref, x, &sub_seq);
7933 : : }
7934 : : }
7935 : :
7936 : 1097 : stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START),
7937 : : 0);
7938 : 1097 : gimple_seq_add_stmt (stmt_seqp, stmt);
7939 : :
7940 : 1097 : gimple_seq_add_seq (stmt_seqp, sub_seq);
7941 : :
7942 : 1097 : if (clist)
7943 : : {
7944 : 157 : gimple_seq_add_seq (stmt_seqp, *clist);
7945 : 157 : *clist = NULL;
7946 : : }
7947 : :
7948 : 1097 : stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END),
7949 : : 0);
7950 : 1097 : gimple_seq_add_stmt (stmt_seqp, stmt);
7951 : : }
7952 : :
7953 : :
7954 : : /* Generate code to implement the COPYPRIVATE clauses. */
7955 : :
7956 : : static void
7957 : 115 : lower_copyprivate_clauses (tree clauses, gimple_seq *slist, gimple_seq *rlist,
7958 : : omp_context *ctx)
7959 : : {
7960 : 115 : tree c;
7961 : :
7962 : 463 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7963 : : {
7964 : 348 : tree var, new_var, ref, x;
7965 : 348 : bool by_ref;
7966 : 348 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
7967 : :
7968 : 348 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYPRIVATE)
7969 : 6 : continue;
7970 : :
7971 : 342 : var = OMP_CLAUSE_DECL (c);
7972 : 342 : by_ref = use_pointer_for_field (var, NULL);
7973 : :
7974 : 342 : ref = build_sender_ref (var, ctx);
7975 : 342 : x = new_var = lookup_decl_in_outer_ctx (var, ctx);
7976 : 342 : if (by_ref)
7977 : : {
7978 : 74 : x = build_fold_addr_expr_loc (clause_loc, new_var);
7979 : 74 : x = fold_convert_loc (clause_loc, TREE_TYPE (ref), x);
7980 : : }
7981 : 342 : gimplify_assign (ref, x, slist);
7982 : :
7983 : 342 : ref = build_receiver_ref (var, false, ctx);
7984 : 342 : if (by_ref)
7985 : : {
7986 : 74 : ref = fold_convert_loc (clause_loc,
7987 : 74 : build_pointer_type (TREE_TYPE (new_var)),
7988 : : ref);
7989 : 74 : ref = build_fold_indirect_ref_loc (clause_loc, ref);
7990 : : }
7991 : 342 : if (omp_privatize_by_reference (var))
7992 : : {
7993 : 171 : ref = fold_convert_loc (clause_loc, TREE_TYPE (new_var), ref);
7994 : 171 : ref = build_simple_mem_ref_loc (clause_loc, ref);
7995 : 171 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
7996 : : }
7997 : 342 : x = lang_hooks.decls.omp_clause_assign_op (c, new_var, ref);
7998 : 342 : gimplify_and_add (x, rlist);
7999 : : }
8000 : 115 : }
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 : 22405 : lower_send_clauses (tree clauses, gimple_seq *ilist, gimple_seq *olist,
8008 : : omp_context *ctx)
8009 : : {
8010 : 22405 : tree c, t;
8011 : 22405 : int ignored_looptemp = 0;
8012 : 22405 : bool is_taskloop = false;
8013 : :
8014 : : /* For taskloop, ignore first two _looptemp_ clauses, those are initialized
8015 : : by GOMP_taskloop. */
8016 : 22405 : if (is_task_ctx (ctx) && gimple_omp_task_taskloop_p (ctx->stmt))
8017 : : {
8018 : : ignored_looptemp = 2;
8019 : : is_taskloop = true;
8020 : : }
8021 : :
8022 : 117029 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8023 : : {
8024 : 94624 : tree val, ref, x, var;
8025 : 94624 : bool by_ref, do_in = false, do_out = false;
8026 : 94624 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
8027 : :
8028 : 94624 : switch (OMP_CLAUSE_CODE (c))
8029 : : {
8030 : 4673 : case OMP_CLAUSE_PRIVATE:
8031 : 4673 : if (OMP_CLAUSE_PRIVATE_OUTER_REF (c))
8032 : : break;
8033 : 4518 : 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 : 4841 : case OMP_CLAUSE_REDUCTION:
8041 : 4841 : if (is_task_ctx (ctx) || OMP_CLAUSE_REDUCTION_TASK (c))
8042 : 740 : continue;
8043 : : break;
8044 : 17538 : case OMP_CLAUSE_SHARED:
8045 : 17538 : if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
8046 : : break;
8047 : 17229 : continue;
8048 : 18801 : case OMP_CLAUSE__LOOPTEMP_:
8049 : 18801 : if (ignored_looptemp)
8050 : : {
8051 : 2686 : ignored_looptemp--;
8052 : 2686 : continue;
8053 : : }
8054 : : break;
8055 : 11971 : default:
8056 : 11971 : continue;
8057 : : }
8058 : :
8059 : 57480 : val = OMP_CLAUSE_DECL (c);
8060 : 57480 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8061 : 53379 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
8062 : 59116 : && TREE_CODE (val) == MEM_REF)
8063 : : {
8064 : 1653 : val = TREE_OPERAND (val, 0);
8065 : 1653 : if (TREE_CODE (val) == POINTER_PLUS_EXPR)
8066 : 351 : val = TREE_OPERAND (val, 0);
8067 : 1653 : if (INDIRECT_REF_P (val)
8068 : 1511 : || TREE_CODE (val) == ADDR_EXPR)
8069 : 842 : val = TREE_OPERAND (val, 0);
8070 : 1653 : if (is_variable_sized (val))
8071 : 56 : continue;
8072 : : }
8073 : :
8074 : : /* For OMP_CLAUSE_SHARED_FIRSTPRIVATE, look beyond the
8075 : : outer taskloop region. */
8076 : 57424 : omp_context *ctx_for_o = ctx;
8077 : 57424 : if (is_taskloop
8078 : 3994 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8079 : 57733 : && OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
8080 : 309 : ctx_for_o = ctx->outer;
8081 : :
8082 : 57424 : var = lookup_decl_in_outer_ctx (val, ctx_for_o);
8083 : :
8084 : 57424 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_COPYIN
8085 : 56914 : && is_global_var (var)
8086 : 59723 : && (val == OMP_CLAUSE_DECL (c)
8087 : 273 : || !is_task_ctx (ctx)
8088 : 241 : || (TREE_CODE (TREE_TYPE (val)) != POINTER_TYPE
8089 : 205 : && (TREE_CODE (TREE_TYPE (val)) != REFERENCE_TYPE
8090 : 81 : || (TREE_CODE (TREE_TYPE (TREE_TYPE (val)))
8091 : : != POINTER_TYPE)))))
8092 : 2245 : continue;
8093 : :
8094 : 55179 : t = omp_member_access_dummy_var (var);
8095 : 55179 : if (t)
8096 : : {
8097 : 243 : var = DECL_VALUE_EXPR (var);
8098 : 243 : tree o = maybe_lookup_decl_in_outer_ctx (t, ctx_for_o);
8099 : 243 : if (o != t)
8100 : 63 : var = unshare_and_remap (var, t, o);
8101 : : else
8102 : 180 : var = unshare_expr (var);
8103 : : }
8104 : :
8105 : 55179 : 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 : 76 : tree f = lookup_sfield ((splay_tree_key) &DECL_UID (val), ctx);
8111 : 76 : x = omp_build_component_ref (ctx->sender_decl, f);
8112 : 76 : if (use_pointer_for_field (val, ctx))
8113 : 56 : var = build_fold_addr_expr (var);
8114 : 76 : gimplify_assign (x, var, ilist);
8115 : 76 : DECL_ABSTRACT_ORIGIN (f) = NULL;
8116 : 76 : continue;
8117 : 76 : }
8118 : :
8119 : 55157 : if (((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8120 : 51747 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION)
8121 : 4492 : || val == OMP_CLAUSE_DECL (c))
8122 : 108828 : && is_variable_sized (val))
8123 : 54 : continue;
8124 : 55049 : by_ref = use_pointer_for_field (val, NULL);
8125 : :
8126 : 55049 : switch (OMP_CLAUSE_CODE (c))
8127 : : {
8128 : 26760 : case OMP_CLAUSE_FIRSTPRIVATE:
8129 : 26760 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
8130 : 7194 : && !by_ref
8131 : 33903 : && is_task_ctx (ctx))
8132 : 2351 : 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 : 6466 : case OMP_CLAUSE_LASTPRIVATE:
8144 : 6466 : if (by_ref || omp_privatize_by_reference (val))
8145 : : {
8146 : 383 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8147 : 239 : continue;
8148 : : do_in = true;
8149 : : }
8150 : : else
8151 : : {
8152 : 6083 : do_out = true;
8153 : 6083 : if (lang_hooks.decls.omp_private_outer_ref (val))
8154 : : do_in = true;
8155 : : }
8156 : : break;
8157 : :
8158 : 4492 : case OMP_CLAUSE_REDUCTION:
8159 : 4492 : case OMP_CLAUSE_IN_REDUCTION:
8160 : 4492 : do_in = true;
8161 : 4492 : if (val == OMP_CLAUSE_DECL (c))
8162 : : {
8163 : 3114 : if (is_task_ctx (ctx))
8164 : 301 : by_ref = use_pointer_for_field (val, ctx);
8165 : : else
8166 : 2813 : do_out = !(by_ref || omp_privatize_by_reference (val));
8167 : : }
8168 : : else
8169 : 1378 : by_ref = TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE;
8170 : : break;
8171 : :
8172 : 0 : default:
8173 : 0 : gcc_unreachable ();
8174 : : }
8175 : :
8176 : 1679 : if (do_in)
8177 : : {
8178 : 48751 : ref = build_sender_ref (val, ctx);
8179 : 48751 : x = by_ref ? build_fold_addr_expr_loc (clause_loc, var) : var;
8180 : 48751 : gimplify_assign (ref, x, ilist);
8181 : 48751 : if (is_task_ctx (ctx))
8182 : 6319 : DECL_ABSTRACT_ORIGIN (TREE_OPERAND (ref, 1)) = NULL;
8183 : : }
8184 : :
8185 : 54810 : if (do_out)
8186 : : {
8187 : 8357 : ref = build_sender_ref (val, ctx);
8188 : 8357 : gimplify_assign (var, ref, olist);
8189 : : }
8190 : : }
8191 : 22405 : }
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 : 22405 : lower_send_shared_vars (gimple_seq *ilist, gimple_seq *olist, omp_context *ctx)
8199 : : {
8200 : 22405 : tree var, ovar, nvar, t, f, x, record_type;
8201 : :
8202 : 22405 : if (ctx->record_type == NULL)
8203 : 4410 : return;
8204 : :
8205 : 17995 : record_type = ctx->srecord_type ? ctx->srecord_type : ctx->record_type;
8206 : 86858 : for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
8207 : : {
8208 : 68863 : ovar = DECL_ABSTRACT_ORIGIN (f);
8209 : 68863 : if (!ovar || TREE_CODE (ovar) == FIELD_DECL)
8210 : 6395 : continue;
8211 : :
8212 : 62468 : nvar = maybe_lookup_decl (ovar, ctx);
8213 : 113289 : if (!nvar
8214 : 62059 : || !DECL_HAS_VALUE_EXPR_P (nvar)
8215 : 74315 : || (ctx->allocate_map
8216 : 293 : && ctx->allocate_map->get (ovar)))
8217 : 50821 : 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 : 11647 : var = lookup_decl_in_outer_ctx (ovar, ctx);
8223 : :
8224 : 11647 : t = omp_member_access_dummy_var (var);
8225 : 11647 : if (t)
8226 : : {
8227 : 49 : var = DECL_VALUE_EXPR (var);
8228 : 49 : tree o = maybe_lookup_decl_in_outer_ctx (t, ctx);
8229 : 49 : if (o != t)
8230 : 18 : var = unshare_and_remap (var, t, o);
8231 : : else
8232 : 31 : var = unshare_expr (var);
8233 : : }
8234 : :
8235 : 11647 : if (use_pointer_for_field (ovar, ctx))
8236 : : {
8237 : 6442 : x = build_sender_ref (ovar, ctx);
8238 : 6442 : if (TREE_CODE (TREE_TYPE (f)) == ARRAY_TYPE
8239 : 6442 : && TREE_TYPE (f) == TREE_TYPE (ovar))
8240 : : {
8241 : 103 : gcc_assert (is_parallel_ctx (ctx)
8242 : : && DECL_ARTIFICIAL (ovar));
8243 : : /* _condtemp_ clause. */
8244 : 103 : var = build_constructor (TREE_TYPE (x), NULL);
8245 : : }
8246 : : else
8247 : 6339 : var = build_fold_addr_expr (var);
8248 : 6442 : gimplify_assign (x, var, ilist);
8249 : : }
8250 : : else
8251 : : {
8252 : 5205 : x = build_sender_ref (ovar, ctx);
8253 : 5205 : gimplify_assign (x, var, ilist);
8254 : :
8255 : 5205 : 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 : 5205 : && !((TREE_CODE (var) == RESULT_DECL
8261 : 3295 : || TREE_CODE (var) == PARM_DECL)
8262 : 179 : && DECL_BY_REFERENCE (var)))
8263 : : {
8264 : 3224 : x = build_sender_ref (ovar, ctx);
8265 : 3224 : 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 : 9669 : lower_oacc_head_mark (location_t loc, tree ddvar, tree clauses,
8278 : : gimple_seq *seq, omp_context *ctx)
8279 : : {
8280 : 9669 : unsigned levels = 0;
8281 : 9669 : unsigned tag = 0;
8282 : 9669 : tree gang_static = NULL_TREE;
8283 : 9669 : auto_vec<tree, 5> args;
8284 : :
8285 : 19338 : args.quick_push (build_int_cst
8286 : 9669 : (integer_type_node, IFN_UNIQUE_OACC_HEAD_MARK));
8287 : 9669 : args.quick_push (ddvar);
8288 : 30874 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
8289 : : {
8290 : 21205 : switch (OMP_CLAUSE_CODE (c))
8291 : : {
8292 : 1856 : case OMP_CLAUSE_GANG:
8293 : 1856 : tag |= OLF_DIM_GANG;
8294 : 1856 : 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 : 1856 : if (gang_static && integer_minus_onep (gang_static))
8298 : 70 : gang_static = NULL_TREE;
8299 : 1856 : levels++;
8300 : 1856 : break;
8301 : :
8302 : 1330 : case OMP_CLAUSE_WORKER:
8303 : 1330 : tag |= OLF_DIM_WORKER;
8304 : 1330 : levels++;
8305 : 1330 : break;
8306 : :
8307 : 1523 : case OMP_CLAUSE_VECTOR:
8308 : 1523 : tag |= OLF_DIM_VECTOR;
8309 : 1523 : levels++;
8310 : 1523 : break;
8311 : :
8312 : 316 : case OMP_CLAUSE_SEQ:
8313 : 316 : tag |= OLF_SEQ;
8314 : 316 : break;
8315 : :
8316 : 409 : case OMP_CLAUSE_AUTO:
8317 : 409 : tag |= OLF_AUTO;
8318 : 409 : break;
8319 : :
8320 : 268 : case OMP_CLAUSE_INDEPENDENT:
8321 : 268 : tag |= OLF_INDEPENDENT;
8322 : 268 : break;
8323 : :
8324 : 136 : case OMP_CLAUSE_TILE:
8325 : 136 : tag |= OLF_TILE;
8326 : 136 : break;
8327 : :
8328 : 3985 : case OMP_CLAUSE_REDUCTION:
8329 : 3985 : tag |= OLF_REDUCTION;
8330 : 3985 : break;
8331 : :
8332 : 11382 : default:
8333 : 11382 : continue;
8334 : : }
8335 : : }
8336 : :
8337 : 9669 : if (gang_static)
8338 : : {
8339 : 146 : if (DECL_P (gang_static))
8340 : 16 : gang_static = build_outer_var_ref (gang_static, ctx);
8341 : 146 : tag |= OLF_GANG_STATIC;
8342 : : }
8343 : :
8344 : 9669 : omp_context *tgt = enclosing_target_ctx (ctx);
8345 : 9669 : if (!tgt || is_oacc_parallel_or_serial (tgt))
8346 : : ;
8347 : 72 : else if (is_oacc_kernels (tgt))
8348 : : /* Not using this loops handling inside OpenACC 'kernels' regions. */
8349 : 0 : gcc_unreachable ();
8350 : 72 : else if (is_oacc_kernels_decomposed_part (tgt))
8351 : : ;
8352 : : else
8353 : 0 : gcc_unreachable ();
8354 : :
8355 : : /* In a parallel region, loops are implicitly INDEPENDENT. */
8356 : 9669 : if (!tgt || is_oacc_parallel_or_serial (tgt))
8357 : 9597 : 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 : 9669 : if (tgt && is_oacc_kernels_decomposed_part (tgt))
8362 : : {
8363 : 72 : gcc_assert (tag & (OLF_SEQ | OLF_INDEPENDENT));
8364 : 72 : gcc_assert (!(tag & OLF_AUTO));
8365 : : }
8366 : :
8367 : 9669 : 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 : 9533 : bool maybe_auto = !(tag & (((GOMP_DIM_MASK (GOMP_DIM_MAX) - 1)
8376 : : << OLF_DIM_BASE) | OLF_SEQ));
8377 : :
8378 : 9533 : if (levels < 1u + maybe_auto)
8379 : : levels = 1u + maybe_auto;
8380 : : }
8381 : :
8382 : 9669 : args.quick_push (build_int_cst (integer_type_node, levels));
8383 : 9669 : args.quick_push (build_int_cst (integer_type_node, tag));
8384 : 9669 : if (gang_static)
8385 : 146 : args.quick_push (gang_static);
8386 : :
8387 : 9669 : gcall *call = gimple_build_call_internal_vec (IFN_UNIQUE, args);
8388 : 9669 : gimple_set_location (call, loc);
8389 : 9669 : gimple_set_lhs (call, ddvar);
8390 : 9669 : gimple_seq_add_stmt (seq, call);
8391 : :
8392 : 9669 : return levels;
8393 : 9669 : }
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 : 42783 : lower_oacc_loop_marker (location_t loc, tree ddvar, bool head,
8400 : : tree tofollow, gimple_seq *seq)
8401 : : {
8402 : 42783 : int marker_kind = (head ? IFN_UNIQUE_OACC_HEAD_MARK
8403 : : : IFN_UNIQUE_OACC_TAIL_MARK);
8404 : 42783 : tree marker = build_int_cst (integer_type_node, marker_kind);
8405 : 42783 : int nargs = 2 + (tofollow != NULL_TREE);
8406 : 42783 : gcall *call = gimple_build_call_internal (IFN_UNIQUE, nargs,
8407 : : marker, ddvar, tofollow);
8408 : 42783 : gimple_set_location (call, loc);
8409 : 42783 : gimple_set_lhs (call, ddvar);
8410 : 42783 : gimple_seq_add_stmt (seq, call);
8411 : 42783 : }
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 : 9669 : lower_oacc_head_tail (location_t loc, tree clauses, gcall *private_marker,
8419 : : gimple_seq *head, gimple_seq *tail, omp_context *ctx)
8420 : : {
8421 : 9669 : bool inner = false;
8422 : 9669 : tree ddvar = create_tmp_var (integer_type_node, ".data_dep");
8423 : 9669 : gimple_seq_add_stmt (head, gimple_build_assign (ddvar, integer_zero_node));
8424 : :
8425 : 9669 : unsigned count = lower_oacc_head_mark (loc, ddvar, clauses, head, ctx);
8426 : :
8427 : 9669 : if (private_marker)
8428 : : {
8429 : 232 : gimple_set_location (private_marker, loc);
8430 : 232 : gimple_call_set_lhs (private_marker, ddvar);
8431 : 232 : gimple_call_set_arg (private_marker, 1, ddvar);
8432 : : }
8433 : :
8434 : 9669 : tree fork_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_FORK);
8435 : 9669 : tree join_kind = build_int_cst (unsigned_type_node, IFN_UNIQUE_OACC_JOIN);
8436 : :
8437 : 9669 : gcc_assert (count);
8438 : 26226 : for (unsigned done = 1; count; count--, done++)
8439 : : {
8440 : 16557 : gimple_seq fork_seq = NULL;
8441 : 16557 : gimple_seq join_seq = NULL;
8442 : :
8443 : 16557 : tree place = build_int_cst (integer_type_node, -1);
8444 : 16557 : gcall *fork = gimple_build_call_internal (IFN_UNIQUE, 3,
8445 : : fork_kind, ddvar, place);
8446 : 16557 : gimple_set_location (fork, loc);
8447 : 16557 : gimple_set_lhs (fork, ddvar);
8448 : :
8449 : 16557 : gcall *join = gimple_build_call_internal (IFN_UNIQUE, 3,
8450 : : join_kind, ddvar, place);
8451 : 16557 : gimple_set_location (join, loc);
8452 : 16557 : gimple_set_lhs (join, ddvar);
8453 : :
8454 : : /* Mark the beginning of this level sequence. */
8455 : 16557 : if (inner)
8456 : 6888 : lower_oacc_loop_marker (loc, ddvar, true,
8457 : 6888 : build_int_cst (integer_type_node, count),
8458 : : &fork_seq);
8459 : 16557 : lower_oacc_loop_marker (loc, ddvar, false,
8460 : 16557 : build_int_cst (integer_type_node, done),
8461 : : &join_seq);
8462 : :
8463 : 23445 : 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 : 16557 : gimple_seq_add_seq (head, fork_seq);
8469 : : /* Prepend it to tail. */
8470 : 16557 : gimple_seq_add_seq (&join_seq, *tail);
8471 : 16557 : *tail = join_seq;
8472 : :
8473 : 16557 : inner = true;
8474 : : }
8475 : :
8476 : : /* Mark the end of the sequence. */
8477 : 9669 : lower_oacc_loop_marker (loc, ddvar, true, NULL_TREE, head);
8478 : 9669 : lower_oacc_loop_marker (loc, ddvar, false, NULL_TREE, tail);
8479 : 9669 : }
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 : 93783 : maybe_catch_exception (gimple_seq body)
8487 : : {
8488 : 93783 : gimple *g;
8489 : 93783 : tree decl;
8490 : :
8491 : 93783 : if (!flag_exceptions)
8492 : : return body;
8493 : :
8494 : 42100 : if (lang_hooks.eh_protect_cleanup_actions != NULL)
8495 : 42072 : decl = lang_hooks.eh_protect_cleanup_actions ();
8496 : : else
8497 : 28 : decl = builtin_decl_explicit (BUILT_IN_TRAP);
8498 : :
8499 : 42100 : g = gimple_build_eh_must_not_throw (decl);
8500 : 42100 : g = gimple_build_try (body, gimple_seq_alloc_with_stmt (g),
8501 : : GIMPLE_TRY_CATCH);
8502 : :
8503 : 42100 : 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 : 48552 : maybe_add_implicit_barrier_cancel (omp_context *ctx, gimple *omp_return,
8516 : : gimple_seq *body)
8517 : : {
8518 : 48552 : gcc_assert (gimple_code (omp_return) == GIMPLE_OMP_RETURN);
8519 : 48552 : if (gimple_omp_return_nowait_p (omp_return))
8520 : : return;
8521 : 19596 : for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
8522 : 16039 : if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
8523 : 16039 : && outer->cancellable)
8524 : : {
8525 : 64 : tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8526 : 64 : tree c_bool_type = TREE_TYPE (TREE_TYPE (fndecl));
8527 : 64 : tree lhs = create_tmp_var (c_bool_type);
8528 : 64 : gimple_omp_return_set_lhs (omp_return, lhs);
8529 : 64 : tree fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
8530 : 64 : 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 : 64 : gimple_seq_add_stmt (body, g);
8535 : 64 : gimple_seq_add_stmt (body, gimple_build_label (fallthru_label));
8536 : : }
8537 : 15975 : else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
8538 : 15975 : && 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 : 48793 : omp_task_reductions_find_first (tree clauses, enum tree_code code,
8547 : : enum omp_clause_code ccode)
8548 : : {
8549 : 64119 : while (1)
8550 : : {
8551 : 56456 : clauses = omp_find_clause (clauses, ccode);
8552 : 56456 : if (clauses == NULL_TREE)
8553 : : return NULL_TREE;
8554 : 9104 : if (ccode != OMP_CLAUSE_REDUCTION
8555 : 9104 : || code == OMP_TASKLOOP
8556 : 9104 : || OMP_CLAUSE_REDUCTION_TASK (clauses))
8557 : : return clauses;
8558 : 7663 : 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 : 378 : lower_omp_sections (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8570 : : {
8571 : 378 : tree block, control;
8572 : 378 : gimple_stmt_iterator tgsi;
8573 : 378 : gomp_sections *stmt;
8574 : 378 : gimple *t;
8575 : 378 : gbind *new_stmt, *bind;
8576 : 378 : gimple_seq ilist, dlist, olist, tred_dlist = NULL, clist = NULL, new_body;
8577 : :
8578 : 378 : stmt = as_a <gomp_sections *> (gsi_stmt (*gsi_p));
8579 : :
8580 : 378 : push_gimplify_context ();
8581 : :
8582 : 378 : dlist = NULL;
8583 : 378 : ilist = NULL;
8584 : :
8585 : 378 : tree rclauses
8586 : 378 : = omp_task_reductions_find_first (gimple_omp_sections_clauses (stmt),
8587 : : OMP_SECTIONS, OMP_CLAUSE_REDUCTION);
8588 : 378 : tree rtmp = NULL_TREE;
8589 : 378 : if (rclauses)
8590 : : {
8591 : 8 : tree type = build_pointer_type (pointer_sized_int_node);
8592 : 8 : tree temp = create_tmp_var (type);
8593 : 8 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
8594 : 8 : OMP_CLAUSE_DECL (c) = temp;
8595 : 8 : OMP_CLAUSE_CHAIN (c) = gimple_omp_sections_clauses (stmt);
8596 : 8 : gimple_omp_sections_set_clauses (stmt, c);
8597 : 8 : lower_omp_task_reductions (ctx, OMP_SECTIONS,
8598 : : gimple_omp_sections_clauses (stmt),
8599 : : &ilist, &tred_dlist);
8600 : 8 : rclauses = c;
8601 : 8 : rtmp = make_ssa_name (type);
8602 : 8 : gimple_seq_add_stmt (&ilist, gimple_build_assign (rtmp, temp));
8603 : : }
8604 : :
8605 : 378 : tree *clauses_ptr = gimple_omp_sections_clauses_ptr (stmt);
8606 : 378 : lower_lastprivate_conditional_clauses (clauses_ptr, ctx);
8607 : :
8608 : 378 : lower_rec_input_clauses (gimple_omp_sections_clauses (stmt),
8609 : : &ilist, &dlist, ctx, NULL);
8610 : :
8611 : 378 : control = create_tmp_var (unsigned_type_node, ".section");
8612 : 378 : gimple_omp_sections_set_control (stmt, control);
8613 : :
8614 : 378 : new_body = gimple_omp_body (stmt);
8615 : 378 : gimple_omp_set_body (stmt, NULL);
8616 : 378 : tgsi = gsi_start (new_body);
8617 : 1233 : for (; !gsi_end_p (tgsi); gsi_next (&tgsi))
8618 : : {
8619 : 855 : omp_context *sctx;
8620 : 855 : gimple *sec_start;
8621 : :
8622 : 855 : sec_start = gsi_stmt (tgsi);
8623 : 855 : sctx = maybe_lookup_ctx (sec_start);
8624 : 855 : gcc_assert (sctx);
8625 : :
8626 : 855 : lower_omp (gimple_omp_body_ptr (sec_start), sctx);
8627 : 855 : gsi_insert_seq_after (&tgsi, gimple_omp_body (sec_start),
8628 : : GSI_CONTINUE_LINKING);
8629 : 855 : gimple_omp_set_body (sec_start, NULL);
8630 : :
8631 : 855 : if (gsi_one_before_end_p (tgsi))
8632 : : {
8633 : 378 : gimple_seq l = NULL;
8634 : 378 : lower_lastprivate_clauses (gimple_omp_sections_clauses (stmt), NULL,
8635 : : &ilist, &l, &clist, ctx);
8636 : 378 : gsi_insert_seq_after (&tgsi, l, GSI_CONTINUE_LINKING);
8637 : 378 : gimple_omp_section_set_last (sec_start);
8638 : : }
8639 : :
8640 : 855 : gsi_insert_after (&tgsi, gimple_build_omp_return (false),
8641 : : GSI_CONTINUE_LINKING);
8642 : : }
8643 : :
8644 : 378 : block = make_node (BLOCK);
8645 : 378 : bind = gimple_build_bind (NULL, new_body, block);
8646 : :
8647 : 378 : olist = NULL;
8648 : 378 : lower_reduction_clauses (gimple_omp_sections_clauses (stmt), &olist,
8649 : : &clist, ctx);
8650 : 378 : if (clist)
8651 : : {
8652 : 10 : tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
8653 : 10 : gcall *g = gimple_build_call (fndecl, 0);
8654 : 10 : gimple_seq_add_stmt (&olist, g);
8655 : 10 : gimple_seq_add_seq (&olist, clist);
8656 : 10 : fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
8657 : 10 : g = gimple_build_call (fndecl, 0);
8658 : 10 : gimple_seq_add_stmt (&olist, g);
8659 : : }
8660 : :
8661 : 378 : block = make_node (BLOCK);
8662 : 378 : new_stmt = gimple_build_bind (NULL, NULL, block);
8663 : 378 : gsi_replace (gsi_p, new_stmt, true);
8664 : :
8665 : 378 : pop_gimplify_context (new_stmt);
8666 : 378 : gimple_bind_append_vars (new_stmt, ctx->block_vars);
8667 : 378 : BLOCK_VARS (block) = gimple_bind_vars (bind);
8668 : 378 : if (BLOCK_VARS (block))
8669 : 0 : TREE_USED (block) = 1;
8670 : :
8671 : 378 : new_body = NULL;
8672 : 378 : gimple_seq_add_seq (&new_body, ilist);
8673 : 378 : gimple_seq_add_stmt (&new_body, stmt);
8674 : 378 : gimple_seq_add_stmt (&new_body, gimple_build_omp_sections_switch ());
8675 : 378 : gimple_seq_add_stmt (&new_body, bind);
8676 : :
8677 : 378 : t = gimple_build_omp_continue (control, control);
8678 : 378 : gimple_seq_add_stmt (&new_body, t);
8679 : :
8680 : 378 : gimple_seq_add_seq (&new_body, olist);
8681 : 378 : if (ctx->cancellable)
8682 : 19 : gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
8683 : 378 : gimple_seq_add_seq (&new_body, dlist);
8684 : :
8685 : 378 : new_body = maybe_catch_exception (new_body);
8686 : :
8687 : 378 : bool nowait = omp_find_clause (gimple_omp_sections_clauses (stmt),
8688 : 378 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
8689 : 378 : t = gimple_build_omp_return (nowait);
8690 : 378 : gimple_seq_add_stmt (&new_body, t);
8691 : 378 : gimple_seq_add_seq (&new_body, tred_dlist);
8692 : 378 : maybe_add_implicit_barrier_cancel (ctx, t, &new_body);
8693 : :
8694 : 378 : if (rclauses)
8695 : 8 : OMP_CLAUSE_DECL (rclauses) = rtmp;
8696 : :
8697 : 378 : gimple_bind_set_body (new_stmt, new_body);
8698 : 378 : }
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 : 1013 : lower_omp_single_simple (gomp_single *single_stmt, gimple_seq *pre_p)
8714 : : {
8715 : 1013 : location_t loc = gimple_location (single_stmt);
8716 : 1013 : tree tlabel = create_artificial_label (loc);
8717 : 1013 : tree flabel = create_artificial_label (loc);
8718 : 1013 : gimple *call, *cond;
8719 : 1013 : tree lhs, decl;
8720 : :
8721 : 1013 : decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_START);
8722 : 1013 : lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (decl)));
8723 : 1013 : call = gimple_build_call (decl, 0);
8724 : 1013 : gimple_call_set_lhs (call, lhs);
8725 : 1013 : gimple_seq_add_stmt (pre_p, call);
8726 : :
8727 : 1013 : cond = gimple_build_cond (EQ_EXPR, lhs,
8728 : 1013 : fold_convert_loc (loc, TREE_TYPE (lhs),
8729 : : boolean_true_node),
8730 : : tlabel, flabel);
8731 : 1013 : gimple_seq_add_stmt (pre_p, cond);
8732 : 1013 : gimple_seq_add_stmt (pre_p, gimple_build_label (tlabel));
8733 : 1013 : gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
8734 : 1013 : gimple_seq_add_stmt (pre_p, gimple_build_label (flabel));
8735 : 1013 : }
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 (©out);
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 : 115 : lower_omp_single_copy (gomp_single *single_stmt, gimple_seq *pre_p,
8769 : : omp_context *ctx)
8770 : : {
8771 : 115 : tree ptr_type, t, l0, l1, l2, bfn_decl;
8772 : 115 : gimple_seq copyin_seq;
8773 : 115 : location_t loc = gimple_location (single_stmt);
8774 : :
8775 : 115 : ctx->sender_decl = create_tmp_var (ctx->record_type, ".omp_copy_o");
8776 : :
8777 : 115 : ptr_type = build_pointer_type (ctx->record_type);
8778 : 115 : ctx->receiver_decl = create_tmp_var (ptr_type, ".omp_copy_i");
8779 : :
8780 : 115 : l0 = create_artificial_label (loc);
8781 : 115 : l1 = create_artificial_label (loc);
8782 : 115 : l2 = create_artificial_label (loc);
8783 : :
8784 : 115 : bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_START);
8785 : 115 : t = build_call_expr_loc (loc, bfn_decl, 0);
8786 : 115 : t = fold_convert_loc (loc, ptr_type, t);
8787 : 115 : gimplify_assign (ctx->receiver_decl, t, pre_p);
8788 : :
8789 : 115 : t = build2 (EQ_EXPR, boolean_type_node, ctx->receiver_decl,
8790 : 115 : build_int_cst (ptr_type, 0));
8791 : 115 : t = build3 (COND_EXPR, void_type_node, t,
8792 : : build_and_jump (&l0), build_and_jump (&l1));
8793 : 115 : gimplify_and_add (t, pre_p);
8794 : :
8795 : 115 : gimple_seq_add_stmt (pre_p, gimple_build_label (l0));
8796 : :
8797 : 115 : gimple_seq_add_seq (pre_p, gimple_omp_body (single_stmt));
8798 : :
8799 : 115 : copyin_seq = NULL;
8800 : 115 : lower_copyprivate_clauses (gimple_omp_single_clauses (single_stmt), pre_p,
8801 : : ©in_seq, ctx);
8802 : :
8803 : 115 : t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
8804 : 115 : bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SINGLE_COPY_END);
8805 : 115 : t = build_call_expr_loc (loc, bfn_decl, 1, t);
8806 : 115 : gimplify_and_add (t, pre_p);
8807 : :
8808 : 115 : t = build_and_jump (&l2);
8809 : 115 : gimplify_and_add (t, pre_p);
8810 : :
8811 : 115 : gimple_seq_add_stmt (pre_p, gimple_build_label (l1));
8812 : :
8813 : 115 : gimple_seq_add_seq (pre_p, copyin_seq);
8814 : :
8815 : 115 : gimple_seq_add_stmt (pre_p, gimple_build_label (l2));
8816 : 115 : }
8817 : :
8818 : :
8819 : : /* Expand code for an OpenMP single directive. */
8820 : :
8821 : : static void
8822 : 1128 : lower_omp_single (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8823 : : {
8824 : 1128 : tree block;
8825 : 1128 : gomp_single *single_stmt = as_a <gomp_single *> (gsi_stmt (*gsi_p));
8826 : 1128 : gbind *bind;
8827 : 1128 : gimple_seq bind_body, bind_body_tail = NULL, dlist;
8828 : :
8829 : 1128 : push_gimplify_context ();
8830 : :
8831 : 1128 : block = make_node (BLOCK);
8832 : 1128 : bind = gimple_build_bind (NULL, NULL, block);
8833 : 1128 : gsi_replace (gsi_p, bind, true);
8834 : 1128 : bind_body = NULL;
8835 : 1128 : dlist = NULL;
8836 : 1128 : lower_rec_input_clauses (gimple_omp_single_clauses (single_stmt),
8837 : : &bind_body, &dlist, ctx, NULL);
8838 : 1128 : lower_omp (gimple_omp_body_ptr (single_stmt), ctx);
8839 : :
8840 : 1128 : gimple_seq_add_stmt (&bind_body, single_stmt);
8841 : :
8842 : 1128 : if (ctx->record_type)
8843 : 115 : lower_omp_single_copy (single_stmt, &bind_body, ctx);
8844 : : else
8845 : 1013 : lower_omp_single_simple (single_stmt, &bind_body);
8846 : :
8847 : 1128 : gimple_omp_set_body (single_stmt, NULL);
8848 : :
8849 : 1128 : gimple_seq_add_seq (&bind_body, dlist);
8850 : :
8851 : 1128 : bind_body = maybe_catch_exception (bind_body);
8852 : :
8853 : 1128 : bool nowait = omp_find_clause (gimple_omp_single_clauses (single_stmt),
8854 : 1128 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
8855 : 1128 : gimple *g = gimple_build_omp_return (nowait);
8856 : 1128 : gimple_seq_add_stmt (&bind_body_tail, g);
8857 : 1128 : maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
8858 : 1128 : if (ctx->record_type)
8859 : : {
8860 : 115 : gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
8861 : 115 : tree clobber = build_clobber (ctx->record_type);
8862 : 115 : gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
8863 : : clobber), GSI_SAME_STMT);
8864 : : }
8865 : 1128 : gimple_seq_add_seq (&bind_body, bind_body_tail);
8866 : 1128 : gimple_bind_set_body (bind, bind_body);
8867 : :
8868 : 1128 : pop_gimplify_context (bind);
8869 : :
8870 : 1128 : gimple_bind_append_vars (bind, ctx->block_vars);
8871 : 1128 : BLOCK_VARS (block) = ctx->block_vars;
8872 : 1128 : if (BLOCK_VARS (block))
8873 : 26 : TREE_USED (block) = 1;
8874 : 1128 : }
8875 : :
8876 : :
8877 : : /* Lower code for an OMP scope directive. */
8878 : :
8879 : : static void
8880 : 133 : lower_omp_scope (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8881 : : {
8882 : 133 : tree block;
8883 : 133 : gimple *scope_stmt = gsi_stmt (*gsi_p);
8884 : 133 : gbind *bind;
8885 : 133 : gimple_seq bind_body, bind_body_tail = NULL, dlist;
8886 : 133 : gimple_seq tred_dlist = NULL;
8887 : :
8888 : 133 : push_gimplify_context ();
8889 : :
8890 : 133 : block = make_node (BLOCK);
8891 : 133 : bind = gimple_build_bind (NULL, NULL, block);
8892 : 133 : gsi_replace (gsi_p, bind, true);
8893 : 133 : bind_body = NULL;
8894 : 133 : dlist = NULL;
8895 : :
8896 : 133 : tree rclauses
8897 : 133 : = omp_task_reductions_find_first (gimple_omp_scope_clauses (scope_stmt),
8898 : : OMP_SCOPE, OMP_CLAUSE_REDUCTION);
8899 : 133 : if (rclauses)
8900 : : {
8901 : 46 : tree type = build_pointer_type (pointer_sized_int_node);
8902 : 46 : tree temp = create_tmp_var (type);
8903 : 46 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
8904 : 46 : OMP_CLAUSE_DECL (c) = temp;
8905 : 46 : OMP_CLAUSE_CHAIN (c) = gimple_omp_scope_clauses (scope_stmt);
8906 : 46 : gimple_omp_scope_set_clauses (scope_stmt, c);
8907 : 46 : lower_omp_task_reductions (ctx, OMP_SCOPE,
8908 : : gimple_omp_scope_clauses (scope_stmt),
8909 : : &bind_body, &tred_dlist);
8910 : 46 : rclauses = c;
8911 : 46 : tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_SCOPE_START);
8912 : 46 : gimple *stmt = gimple_build_call (fndecl, 1, temp);
8913 : 46 : gimple_seq_add_stmt (&bind_body, stmt);
8914 : : }
8915 : :
8916 : 133 : lower_rec_input_clauses (gimple_omp_scope_clauses (scope_stmt),
8917 : : &bind_body, &dlist, ctx, NULL);
8918 : 133 : lower_omp (gimple_omp_body_ptr (scope_stmt), ctx);
8919 : :
8920 : 133 : gimple_seq_add_stmt (&bind_body, scope_stmt);
8921 : :
8922 : 133 : gimple_seq_add_seq (&bind_body, gimple_omp_body (scope_stmt));
8923 : :
8924 : 133 : gimple_omp_set_body (scope_stmt, NULL);
8925 : :
8926 : 133 : gimple_seq clist = NULL;
8927 : 133 : lower_reduction_clauses (gimple_omp_scope_clauses (scope_stmt),
8928 : : &bind_body, &clist, ctx);
8929 : 133 : if (clist)
8930 : : {
8931 : 0 : tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
8932 : 0 : gcall *g = gimple_build_call (fndecl, 0);
8933 : 0 : gimple_seq_add_stmt (&bind_body, g);
8934 : 0 : gimple_seq_add_seq (&bind_body, clist);
8935 : 0 : fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
8936 : 0 : g = gimple_build_call (fndecl, 0);
8937 : 0 : gimple_seq_add_stmt (&bind_body, g);
8938 : : }
8939 : :
8940 : 133 : gimple_seq_add_seq (&bind_body, dlist);
8941 : :
8942 : 133 : bind_body = maybe_catch_exception (bind_body);
8943 : :
8944 : 133 : bool nowait = omp_find_clause (gimple_omp_scope_clauses (scope_stmt),
8945 : 133 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
8946 : 133 : gimple *g = gimple_build_omp_return (nowait);
8947 : 133 : gimple_seq_add_stmt (&bind_body_tail, g);
8948 : 133 : gimple_seq_add_seq (&bind_body_tail, tred_dlist);
8949 : 133 : maybe_add_implicit_barrier_cancel (ctx, g, &bind_body_tail);
8950 : 133 : if (ctx->record_type)
8951 : : {
8952 : 0 : gimple_stmt_iterator gsi = gsi_start (bind_body_tail);
8953 : 0 : tree clobber = build_clobber (ctx->record_type);
8954 : 0 : gsi_insert_after (&gsi, gimple_build_assign (ctx->sender_decl,
8955 : : clobber), GSI_SAME_STMT);
8956 : : }
8957 : 133 : gimple_seq_add_seq (&bind_body, bind_body_tail);
8958 : :
8959 : 133 : gimple_bind_set_body (bind, bind_body);
8960 : :
8961 : 133 : pop_gimplify_context (bind);
8962 : :
8963 : 133 : gimple_bind_append_vars (bind, ctx->block_vars);
8964 : 133 : BLOCK_VARS (block) = ctx->block_vars;
8965 : 133 : if (BLOCK_VARS (block))
8966 : 105 : TREE_USED (block) = 1;
8967 : 133 : }
8968 : :
8969 : : /* Lower code for an OMP dispatch directive. */
8970 : :
8971 : : static void
8972 : 627 : lower_omp_dispatch (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8973 : : {
8974 : 627 : tree block;
8975 : 627 : gimple *stmt = gsi_stmt (*gsi_p);
8976 : 627 : gbind *bind;
8977 : :
8978 : 627 : push_gimplify_context ();
8979 : :
8980 : 627 : block = make_node (BLOCK);
8981 : 627 : bind = gimple_build_bind (NULL, NULL, block);
8982 : 627 : gsi_replace (gsi_p, bind, true);
8983 : :
8984 : 627 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
8985 : 627 : gimple_bind_set_body (bind, maybe_catch_exception (gimple_omp_body (stmt)));
8986 : :
8987 : 627 : pop_gimplify_context (bind);
8988 : :
8989 : 627 : gimple_bind_append_vars (bind, ctx->block_vars);
8990 : 627 : BLOCK_VARS (block) = ctx->block_vars;
8991 : 627 : }
8992 : :
8993 : : /* Expand code for an OpenMP master or masked directive. */
8994 : :
8995 : : static void
8996 : 1079 : lower_omp_master (gimple_stmt_iterator *gsi_p, omp_context *ctx)
8997 : : {
8998 : 1079 : tree block, lab = NULL, x, bfn_decl;
8999 : 1079 : gimple *stmt = gsi_stmt (*gsi_p);
9000 : 1079 : gbind *bind;
9001 : 1079 : location_t loc = gimple_location (stmt);
9002 : 1079 : gimple_seq tseq;
9003 : 1079 : tree filter = integer_zero_node;
9004 : :
9005 : 1079 : push_gimplify_context ();
9006 : :
9007 : 1079 : if (gimple_code (stmt) == GIMPLE_OMP_MASKED)
9008 : : {
9009 : 360 : filter = omp_find_clause (gimple_omp_masked_clauses (stmt),
9010 : : OMP_CLAUSE_FILTER);
9011 : 360 : if (filter)
9012 : 248 : filter = fold_convert (integer_type_node,
9013 : : OMP_CLAUSE_FILTER_EXPR (filter));
9014 : : else
9015 : 112 : filter = integer_zero_node;
9016 : : }
9017 : 1079 : block = make_node (BLOCK);
9018 : 1079 : bind = gimple_build_bind (NULL, NULL, block);
9019 : 1079 : gsi_replace (gsi_p, bind, true);
9020 : 1079 : gimple_bind_add_stmt (bind, stmt);
9021 : :
9022 : 1079 : bfn_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
9023 : 1079 : x = build_call_expr_loc (loc, bfn_decl, 0);
9024 : 1079 : x = build2 (EQ_EXPR, boolean_type_node, x, filter);
9025 : 1079 : x = build3 (COND_EXPR, void_type_node, x, NULL, build_and_jump (&lab));
9026 : 1079 : tseq = NULL;
9027 : 1079 : gimplify_and_add (x, &tseq);
9028 : 1079 : gimple_bind_add_seq (bind, tseq);
9029 : :
9030 : 1079 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
9031 : 1079 : gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
9032 : 1079 : gimple_bind_add_seq (bind, gimple_omp_body (stmt));
9033 : 1079 : gimple_omp_set_body (stmt, NULL);
9034 : :
9035 : 1079 : gimple_bind_add_stmt (bind, gimple_build_label (lab));
9036 : :
9037 : 1079 : gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
9038 : :
9039 : 1079 : pop_gimplify_context (bind);
9040 : :
9041 : 1079 : gimple_bind_append_vars (bind, ctx->block_vars);
9042 : 1079 : BLOCK_VARS (block) = ctx->block_vars;
9043 : 1079 : }
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 : 7922 : 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 : 11260 : for (; *c; *c = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode))
9055 : : {
9056 : 6676 : if (ccode == OMP_CLAUSE_REDUCTION
9057 : 6620 : && code != OMP_TASKLOOP
9058 : 6620 : && !OMP_CLAUSE_REDUCTION_TASK (*c))
9059 : 56 : continue;
9060 : 6564 : *decl = OMP_CLAUSE_DECL (*c);
9061 : 6564 : *type = TREE_TYPE (*decl);
9062 : 6564 : if (TREE_CODE (*decl) == MEM_REF)
9063 : : {
9064 : 1940 : if (pass != 1)
9065 : 970 : continue;
9066 : : }
9067 : : else
9068 : : {
9069 : 4624 : if (omp_privatize_by_reference (*decl))
9070 : 208 : *type = TREE_TYPE (*type);
9071 : 4624 : if (pass != (!TREE_CONSTANT (TYPE_SIZE_UNIT (*type))))
9072 : 2312 : continue;
9073 : : }
9074 : 3282 : *next = omp_find_clause (OMP_CLAUSE_CHAIN (*c), ccode);
9075 : 3282 : return true;
9076 : : }
9077 : 4640 : *decl = NULL_TREE;
9078 : 4640 : *type = NULL_TREE;
9079 : 4640 : *next = NULL_TREE;
9080 : 4640 : 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 : 1369 : lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
9089 : : gimple_seq *start, gimple_seq *end)
9090 : : {
9091 : 2738 : enum omp_clause_code ccode
9092 : : = (code == OMP_TASKGROUP
9093 : 1369 : ? OMP_CLAUSE_TASK_REDUCTION : OMP_CLAUSE_REDUCTION);
9094 : 1369 : tree cancellable = NULL_TREE;
9095 : 1369 : clauses = omp_task_reductions_find_first (clauses, code, ccode);
9096 : 1369 : if (clauses == NULL_TREE)
9097 : 209 : return;
9098 : 1160 : if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
9099 : : {
9100 : 316 : for (omp_context *outer = ctx->outer; outer; outer = outer->outer)
9101 : 238 : if (gimple_code (outer->stmt) == GIMPLE_OMP_PARALLEL
9102 : 238 : && outer->cancellable)
9103 : : {
9104 : 14 : cancellable = error_mark_node;
9105 : 14 : break;
9106 : : }
9107 : 224 : else if (gimple_code (outer->stmt) != GIMPLE_OMP_TASKGROUP
9108 : 224 : && gimple_code (outer->stmt) != GIMPLE_OMP_SCOPE)
9109 : : break;
9110 : : }
9111 : 1160 : tree record_type = lang_hooks.types.make_type (RECORD_TYPE);
9112 : 1160 : tree *last = &TYPE_FIELDS (record_type);
9113 : 1160 : unsigned cnt = 0;
9114 : 1160 : if (cancellable)
9115 : : {
9116 : 14 : tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
9117 : : ptr_type_node);
9118 : 14 : tree ifield = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
9119 : : integer_type_node);
9120 : 14 : *last = field;
9121 : 14 : DECL_CHAIN (field) = ifield;
9122 : 14 : last = &DECL_CHAIN (ifield);
9123 : 14 : DECL_CONTEXT (field) = record_type;
9124 : 14 : if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
9125 : 0 : SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
9126 : 14 : DECL_CONTEXT (ifield) = record_type;
9127 : 14 : if (TYPE_ALIGN (record_type) < DECL_ALIGN (ifield))
9128 : 0 : SET_TYPE_ALIGN (record_type, DECL_ALIGN (ifield));
9129 : : }
9130 : 3480 : for (int pass = 0; pass < 2; pass++)
9131 : : {
9132 : 2320 : tree decl, type, next;
9133 : 2320 : for (tree c = clauses;
9134 : 3961 : omp_task_reduction_iterate (pass, code, ccode,
9135 : 1641 : &c, &decl, &type, &next); c = next)
9136 : : {
9137 : 1641 : ++cnt;
9138 : 1641 : tree new_type = type;
9139 : 1641 : if (ctx->outer)
9140 : 1160 : new_type = remap_type (type, &ctx->outer->cb);
9141 : 1641 : tree field
9142 : 1641 : = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL,
9143 : 1641 : DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE,
9144 : : new_type);
9145 : 1641 : if (DECL_P (decl) && type == TREE_TYPE (decl))
9146 : : {
9147 : 1104 : SET_DECL_ALIGN (field, DECL_ALIGN (decl));
9148 : 1104 : DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
9149 : 1104 : TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
9150 : : }
9151 : : else
9152 : 537 : SET_DECL_ALIGN (field, TYPE_ALIGN (type));
9153 : 1641 : DECL_CONTEXT (field) = record_type;
9154 : 1641 : if (TYPE_ALIGN (record_type) < DECL_ALIGN (field))
9155 : 1178 : SET_TYPE_ALIGN (record_type, DECL_ALIGN (field));
9156 : 1641 : *last = field;
9157 : 1641 : last = &DECL_CHAIN (field);
9158 : 1641 : tree bfield
9159 : 1641 : = build_decl (OMP_CLAUSE_LOCATION (c), FIELD_DECL, NULL_TREE,
9160 : : boolean_type_node);
9161 : 1641 : DECL_CONTEXT (bfield) = record_type;
9162 : 1641 : if (TYPE_ALIGN (record_type) < DECL_ALIGN (bfield))
9163 : 0 : SET_TYPE_ALIGN (record_type, DECL_ALIGN (bfield));
9164 : 1641 : *last = bfield;
9165 : 1641 : last = &DECL_CHAIN (bfield);
9166 : : }
9167 : : }
9168 : 1160 : *last = NULL_TREE;
9169 : 1160 : 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 : 1160 : tree atype = build_array_type_nelts (pointer_sized_int_node, 7 + cnt * 3);
9174 : 1160 : tree avar = create_tmp_var_raw (atype);
9175 : 1160 : gimple_add_tmp_var (avar);
9176 : 1160 : TREE_ADDRESSABLE (avar) = 1;
9177 : 1160 : tree r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_zero_node,
9178 : : NULL_TREE, NULL_TREE);
9179 : 1160 : tree t = build_int_cst (pointer_sized_int_node, cnt);
9180 : 1160 : gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9181 : 1160 : gimple_seq seq = NULL;
9182 : 1160 : tree sz = fold_convert (pointer_sized_int_node,
9183 : : TYPE_SIZE_UNIT (record_type));
9184 : 1160 : int cachesz = 64;
9185 : 1160 : sz = fold_build2 (PLUS_EXPR, pointer_sized_int_node, sz,
9186 : : build_int_cst (pointer_sized_int_node, cachesz - 1));
9187 : 1160 : sz = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, sz,
9188 : : build_int_cst (pointer_sized_int_node, ~(cachesz - 1)));
9189 : 1160 : ctx->task_reductions.create (1 + cnt);
9190 : 1160 : ctx->task_reduction_map = new hash_map<tree, unsigned>;
9191 : 2320 : ctx->task_reductions.quick_push (TREE_CODE (sz) == INTEGER_CST
9192 : 1200 : ? sz : NULL_TREE);
9193 : 1160 : sz = force_gimple_operand (sz, &seq, true, NULL_TREE);
9194 : 1160 : gimple_seq_add_seq (start, seq);
9195 : 1160 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_one_node,
9196 : : NULL_TREE, NULL_TREE);
9197 : 1160 : gimple_seq_add_stmt (start, gimple_build_assign (r, sz));
9198 : 1160 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
9199 : : NULL_TREE, NULL_TREE);
9200 : 1160 : t = build_int_cst (pointer_sized_int_node,
9201 : 1160 : MAX (TYPE_ALIGN_UNIT (record_type), (unsigned) cachesz));
9202 : 1160 : gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9203 : 1160 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (3),
9204 : : NULL_TREE, NULL_TREE);
9205 : 1160 : t = build_int_cst (pointer_sized_int_node, -1);
9206 : 1160 : gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9207 : 1160 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (4),
9208 : : NULL_TREE, NULL_TREE);
9209 : 1160 : t = build_int_cst (pointer_sized_int_node, 0);
9210 : 1160 : 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 : 1160 : tree idx = create_tmp_var (size_type_node);
9218 : 1160 : gimple_seq_add_stmt (end, gimple_build_assign (idx, size_zero_node));
9219 : 1160 : tree num_thr_sz = create_tmp_var (size_type_node);
9220 : 1160 : tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
9221 : 1160 : tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
9222 : 1160 : tree lab3 = NULL_TREE, lab7 = NULL_TREE;
9223 : 1160 : gimple *g;
9224 : 1160 : 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 : 281 : tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
9230 : 281 : t = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
9231 : 281 : tree thr_num = create_tmp_var (integer_type_node);
9232 : 281 : g = gimple_build_call (t, 0);
9233 : 281 : gimple_call_set_lhs (g, thr_num);
9234 : 281 : gimple_seq_add_stmt (end, g);
9235 : 281 : if (cancellable)
9236 : : {
9237 : 14 : tree c;
9238 : 14 : tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9239 : 14 : tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
9240 : 14 : lab3 = create_artificial_label (UNKNOWN_LOCATION);
9241 : 14 : if (code == OMP_FOR)
9242 : 4 : c = gimple_omp_for_clauses (ctx->stmt);
9243 : 10 : else if (code == OMP_SECTIONS)
9244 : 0 : c = gimple_omp_sections_clauses (ctx->stmt);
9245 : : else /* if (code == OMP_SCOPE) */
9246 : 10 : c = gimple_omp_scope_clauses (ctx->stmt);
9247 : 14 : c = OMP_CLAUSE_DECL (omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_));
9248 : 14 : cancellable = c;
9249 : 14 : g = gimple_build_cond (NE_EXPR, c, build_zero_cst (TREE_TYPE (c)),
9250 : : lab5, lab6);
9251 : 14 : gimple_seq_add_stmt (end, g);
9252 : 14 : gimple_seq_add_stmt (end, gimple_build_label (lab5));
9253 : 14 : g = gimple_build_assign (idx, NOP_EXPR, thr_num);
9254 : 14 : gimple_seq_add_stmt (end, g);
9255 : 14 : g = gimple_build_assign (num_thr_sz, PLUS_EXPR, idx,
9256 : 14 : build_one_cst (TREE_TYPE (idx)));
9257 : 14 : gimple_seq_add_stmt (end, g);
9258 : 14 : gimple_seq_add_stmt (end, gimple_build_goto (lab3));
9259 : 14 : gimple_seq_add_stmt (end, gimple_build_label (lab6));
9260 : : }
9261 : 281 : g = gimple_build_cond (NE_EXPR, thr_num, integer_zero_node, lab2, lab4);
9262 : 281 : gimple_seq_add_stmt (end, g);
9263 : 281 : gimple_seq_add_stmt (end, gimple_build_label (lab4));
9264 : : }
9265 : 1160 : if (code != OMP_PARALLEL)
9266 : : {
9267 : 1101 : t = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
9268 : 1101 : tree num_thr = create_tmp_var (integer_type_node);
9269 : 1101 : g = gimple_build_call (t, 0);
9270 : 1101 : gimple_call_set_lhs (g, num_thr);
9271 : 1101 : gimple_seq_add_stmt (end, g);
9272 : 1101 : g = gimple_build_assign (num_thr_sz, NOP_EXPR, num_thr);
9273 : 1101 : gimple_seq_add_stmt (end, g);
9274 : 1101 : if (cancellable)
9275 : 14 : gimple_seq_add_stmt (end, gimple_build_label (lab3));
9276 : : }
9277 : : else
9278 : : {
9279 : 59 : tree c = omp_find_clause (gimple_omp_parallel_clauses (ctx->stmt),
9280 : : OMP_CLAUSE__REDUCTEMP_);
9281 : 59 : t = fold_convert (pointer_sized_int_node, OMP_CLAUSE_DECL (c));
9282 : 59 : t = fold_convert (size_type_node, t);
9283 : 59 : gimplify_assign (num_thr_sz, t, end);
9284 : : }
9285 : 1160 : t = build4 (ARRAY_REF, pointer_sized_int_node, avar, size_int (2),
9286 : : NULL_TREE, NULL_TREE);
9287 : 1160 : tree data = create_tmp_var (pointer_sized_int_node);
9288 : 1160 : gimple_seq_add_stmt (end, gimple_build_assign (data, t));
9289 : 1160 : if (code == OMP_TASKLOOP)
9290 : : {
9291 : 493 : lab7 = create_artificial_label (UNKNOWN_LOCATION);
9292 : 493 : g = gimple_build_cond (NE_EXPR, data,
9293 : : build_zero_cst (pointer_sized_int_node),
9294 : : lab1, lab7);
9295 : 493 : gimple_seq_add_stmt (end, g);
9296 : : }
9297 : 1160 : gimple_seq_add_stmt (end, gimple_build_label (lab1));
9298 : 1160 : tree ptr;
9299 : 1160 : if (TREE_CODE (TYPE_SIZE_UNIT (record_type)) == INTEGER_CST)
9300 : 1120 : ptr = create_tmp_var (build_pointer_type (record_type));
9301 : : else
9302 : 40 : ptr = create_tmp_var (ptr_type_node);
9303 : 1160 : gimple_seq_add_stmt (end, gimple_build_assign (ptr, NOP_EXPR, data));
9304 : :
9305 : 1160 : tree field = TYPE_FIELDS (record_type);
9306 : 1160 : cnt = 0;
9307 : 1160 : if (cancellable)
9308 : 14 : field = DECL_CHAIN (DECL_CHAIN (field));
9309 : 3480 : for (int pass = 0; pass < 2; pass++)
9310 : : {
9311 : 2320 : tree decl, type, next;
9312 : 2320 : for (tree c = clauses;
9313 : 3961 : omp_task_reduction_iterate (pass, code, ccode,
9314 : 1641 : &c, &decl, &type, &next); c = next)
9315 : : {
9316 : 1641 : tree var = decl, ref;
9317 : 1641 : if (TREE_CODE (decl) == MEM_REF)
9318 : : {
9319 : 485 : var = TREE_OPERAND (var, 0);
9320 : 485 : if (TREE_CODE (var) == POINTER_PLUS_EXPR)
9321 : 108 : var = TREE_OPERAND (var, 0);
9322 : 485 : tree v = var;
9323 : 485 : if (TREE_CODE (var) == ADDR_EXPR)
9324 : 261 : var = TREE_OPERAND (var, 0);
9325 : 224 : else if (INDIRECT_REF_P (var))
9326 : 18 : var = TREE_OPERAND (var, 0);
9327 : 485 : tree orig_var = var;
9328 : 485 : if (is_variable_sized (var))
9329 : : {
9330 : 31 : gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
9331 : 31 : var = DECL_VALUE_EXPR (var);
9332 : 31 : gcc_assert (INDIRECT_REF_P (var));
9333 : 31 : var = TREE_OPERAND (var, 0);
9334 : 31 : gcc_assert (DECL_P (var));
9335 : : }
9336 : 485 : t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
9337 : 485 : if (orig_var != var)
9338 : 31 : gcc_assert (TREE_CODE (v) == ADDR_EXPR);
9339 : 454 : else if (TREE_CODE (v) == ADDR_EXPR)
9340 : 230 : t = build_fold_addr_expr (t);
9341 : 224 : else if (INDIRECT_REF_P (v))
9342 : 18 : t = build_fold_indirect_ref (t);
9343 : 485 : if (TREE_CODE (TREE_OPERAND (decl, 0)) == POINTER_PLUS_EXPR)
9344 : : {
9345 : 108 : tree b = TREE_OPERAND (TREE_OPERAND (decl, 0), 1);
9346 : 108 : b = maybe_lookup_decl_in_outer_ctx (b, ctx);
9347 : 108 : t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, b);
9348 : : }
9349 : 485 : if (!integer_zerop (TREE_OPERAND (decl, 1)))
9350 : 219 : 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 : 1156 : t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
9357 : 1156 : if (!omp_privatize_by_reference (decl))
9358 : 1104 : t = build_fold_addr_expr (t);
9359 : : }
9360 : 1641 : t = fold_convert (pointer_sized_int_node, t);
9361 : 1641 : seq = NULL;
9362 : 1641 : t = force_gimple_operand (t, &seq, true, NULL_TREE);
9363 : 1641 : gimple_seq_add_seq (start, seq);
9364 : 1641 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9365 : 1641 : size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
9366 : 1641 : gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9367 : 1641 : t = unshare_expr (byte_position (field));
9368 : 1641 : t = fold_convert (pointer_sized_int_node, t);
9369 : 1641 : ctx->task_reduction_map->put (c, cnt);
9370 : 3282 : ctx->task_reductions.quick_push (TREE_CODE (t) == INTEGER_CST
9371 : 2013 : ? t : NULL_TREE);
9372 : 1641 : seq = NULL;
9373 : 1641 : t = force_gimple_operand (t, &seq, true, NULL_TREE);
9374 : 1641 : gimple_seq_add_seq (start, seq);
9375 : 1641 : r = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9376 : 1641 : size_int (7 + cnt * 3 + 1), NULL_TREE, NULL_TREE);
9377 : 1641 : gimple_seq_add_stmt (start, gimple_build_assign (r, t));
9378 : :
9379 : 1641 : tree bfield = DECL_CHAIN (field);
9380 : 1641 : tree cond;
9381 : 1641 : if (code == OMP_PARALLEL
9382 : 1641 : || 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 : 547 : cond = boolean_true_node;
9388 : 1094 : else if (TREE_TYPE (ptr) == ptr_type_node)
9389 : : {
9390 : 261 : cond = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
9391 : : unshare_expr (byte_position (bfield)));
9392 : 261 : seq = NULL;
9393 : 261 : cond = force_gimple_operand (cond, &seq, true, NULL_TREE);
9394 : 261 : gimple_seq_add_seq (end, seq);
9395 : 261 : tree pbool = build_pointer_type (TREE_TYPE (bfield));
9396 : 261 : cond = build2 (MEM_REF, TREE_TYPE (bfield), cond,
9397 : 261 : build_int_cst (pbool, 0));
9398 : : }
9399 : : else
9400 : 833 : cond = build3 (COMPONENT_REF, TREE_TYPE (bfield),
9401 : : build_simple_mem_ref (ptr), bfield, NULL_TREE);
9402 : 1641 : tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
9403 : 1641 : tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
9404 : 1641 : tree condv = create_tmp_var (boolean_type_node);
9405 : 1641 : gimple_seq_add_stmt (end, gimple_build_assign (condv, cond));
9406 : 1641 : g = gimple_build_cond (NE_EXPR, condv, boolean_false_node,
9407 : : lab3, lab4);
9408 : 1641 : gimple_seq_add_stmt (end, g);
9409 : 1641 : gimple_seq_add_stmt (end, gimple_build_label (lab3));
9410 : 1641 : 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 : 18 : tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9416 : 18 : g = gimple_build_cond (NE_EXPR, cancellable,
9417 : 18 : build_zero_cst (TREE_TYPE (cancellable)),
9418 : : lab4, lab5);
9419 : 18 : gimple_seq_add_stmt (end, g);
9420 : 18 : gimple_seq_add_stmt (end, gimple_build_label (lab5));
9421 : : }
9422 : :
9423 : 1641 : tree new_var;
9424 : 1641 : if (TREE_TYPE (ptr) == ptr_type_node)
9425 : : {
9426 : 428 : new_var = build2 (POINTER_PLUS_EXPR, ptr_type_node, ptr,
9427 : : unshare_expr (byte_position (field)));
9428 : 428 : seq = NULL;
9429 : 428 : new_var = force_gimple_operand (new_var, &seq, true, NULL_TREE);
9430 : 428 : gimple_seq_add_seq (end, seq);
9431 : 428 : tree pbool = build_pointer_type (TREE_TYPE (field));
9432 : 428 : new_var = build2 (MEM_REF, TREE_TYPE (field), new_var,
9433 : 428 : build_int_cst (pbool, 0));
9434 : : }
9435 : : else
9436 : 1213 : new_var = build3 (COMPONENT_REF, TREE_TYPE (field),
9437 : : build_simple_mem_ref (ptr), field, NULL_TREE);
9438 : :
9439 : 1641 : enum tree_code rcode = OMP_CLAUSE_REDUCTION_CODE (c);
9440 : 1641 : if (TREE_CODE (decl) != MEM_REF
9441 : 1641 : && omp_privatize_by_reference (decl))
9442 : 52 : ref = build_simple_mem_ref (ref);
9443 : : /* reduction(-:var) sums up the partial results, so it acts
9444 : : identically to reduction(+:var). */
9445 : 1641 : if (rcode == MINUS_EXPR)
9446 : 0 : rcode = PLUS_EXPR;
9447 : 1641 : if (TREE_CODE (decl) == MEM_REF)
9448 : : {
9449 : 485 : tree type = TREE_TYPE (new_var);
9450 : 485 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9451 : 485 : tree i = create_tmp_var (TREE_TYPE (v));
9452 : 485 : tree ptype = build_pointer_type (TREE_TYPE (type));
9453 : 485 : if (DECL_P (v))
9454 : : {
9455 : 142 : v = maybe_lookup_decl_in_outer_ctx (v, ctx);
9456 : 142 : tree vv = create_tmp_var (TREE_TYPE (v));
9457 : 142 : gimplify_assign (vv, v, start);
9458 : 142 : v = vv;
9459 : : }
9460 : 485 : ref = build4 (ARRAY_REF, pointer_sized_int_node, avar,
9461 : 485 : size_int (7 + cnt * 3), NULL_TREE, NULL_TREE);
9462 : 485 : new_var = build_fold_addr_expr (new_var);
9463 : 485 : new_var = fold_convert (ptype, new_var);
9464 : 485 : ref = fold_convert (ptype, ref);
9465 : 485 : tree m = create_tmp_var (ptype);
9466 : 485 : gimplify_assign (m, new_var, end);
9467 : 485 : new_var = m;
9468 : 485 : m = create_tmp_var (ptype);
9469 : 485 : gimplify_assign (m, ref, end);
9470 : 485 : ref = m;
9471 : 485 : gimplify_assign (i, build_int_cst (TREE_TYPE (v), 0), end);
9472 : 485 : tree body = create_artificial_label (UNKNOWN_LOCATION);
9473 : 485 : tree endl = create_artificial_label (UNKNOWN_LOCATION);
9474 : 485 : gimple_seq_add_stmt (end, gimple_build_label (body));
9475 : 485 : tree priv = build_simple_mem_ref (new_var);
9476 : 485 : tree out = build_simple_mem_ref (ref);
9477 : 485 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
9478 : : {
9479 : 161 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
9480 : 161 : tree decl_placeholder
9481 : 161 : = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c);
9482 : 161 : tree lab6 = NULL_TREE;
9483 : 161 : 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 : 4 : tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9489 : 4 : lab6 = create_artificial_label (UNKNOWN_LOCATION);
9490 : 4 : tree zero = build_zero_cst (TREE_TYPE (cancellable));
9491 : 4 : g = gimple_build_cond (NE_EXPR, cancellable, zero,
9492 : : lab6, lab5);
9493 : 4 : gimple_seq_add_stmt (end, g);
9494 : 4 : gimple_seq_add_stmt (end, gimple_build_label (lab5));
9495 : : }
9496 : 161 : SET_DECL_VALUE_EXPR (placeholder, out);
9497 : 161 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
9498 : 161 : SET_DECL_VALUE_EXPR (decl_placeholder, priv);
9499 : 161 : DECL_HAS_VALUE_EXPR_P (decl_placeholder) = 1;
9500 : 161 : lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
9501 : 322 : gimple_seq_add_seq (end,
9502 : 161 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
9503 : 161 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
9504 : 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9505 : : {
9506 : 56 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
9507 : 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = NULL;
9508 : : }
9509 : 161 : if (cancellable)
9510 : 4 : gimple_seq_add_stmt (end, gimple_build_label (lab6));
9511 : 161 : tree x = lang_hooks.decls.omp_clause_dtor (c, priv);
9512 : 161 : if (x)
9513 : : {
9514 : 137 : gimple_seq tseq = NULL;
9515 : 137 : gimplify_stmt (&x, &tseq);
9516 : 137 : gimple_seq_add_seq (end, tseq);
9517 : : }
9518 : : }
9519 : : else
9520 : : {
9521 : 324 : tree x = build2 (rcode, TREE_TYPE (out), out, priv);
9522 : 324 : out = unshare_expr (out);
9523 : 324 : gimplify_assign (out, x, end);
9524 : : }
9525 : 485 : gimple *g
9526 : 485 : = gimple_build_assign (new_var, POINTER_PLUS_EXPR, new_var,
9527 : 485 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
9528 : 485 : gimple_seq_add_stmt (end, g);
9529 : 485 : g = gimple_build_assign (ref, POINTER_PLUS_EXPR, ref,
9530 : 485 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
9531 : 485 : gimple_seq_add_stmt (end, g);
9532 : 485 : g = gimple_build_assign (i, PLUS_EXPR, i,
9533 : 485 : build_int_cst (TREE_TYPE (i), 1));
9534 : 485 : gimple_seq_add_stmt (end, g);
9535 : 485 : g = gimple_build_cond (LE_EXPR, i, v, body, endl);
9536 : 485 : gimple_seq_add_stmt (end, g);
9537 : 485 : gimple_seq_add_stmt (end, gimple_build_label (endl));
9538 : : }
9539 : 1156 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
9540 : : {
9541 : 126 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
9542 : 126 : tree oldv = NULL_TREE;
9543 : 126 : tree lab6 = NULL_TREE;
9544 : 126 : 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 : 4 : tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
9550 : 4 : lab6 = create_artificial_label (UNKNOWN_LOCATION);
9551 : 4 : tree zero = build_zero_cst (TREE_TYPE (cancellable));
9552 : 4 : g = gimple_build_cond (NE_EXPR, cancellable, zero,
9553 : : lab6, lab5);
9554 : 4 : gimple_seq_add_stmt (end, g);
9555 : 4 : gimple_seq_add_stmt (end, gimple_build_label (lab5));
9556 : : }
9557 : 126 : if (omp_privatize_by_reference (decl)
9558 : 144 : && !useless_type_conversion_p (TREE_TYPE (placeholder),
9559 : 18 : TREE_TYPE (ref)))
9560 : 0 : ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
9561 : 126 : ref = build_fold_addr_expr_loc (OMP_CLAUSE_LOCATION (c), ref);
9562 : 126 : tree refv = create_tmp_var (TREE_TYPE (ref));
9563 : 126 : gimplify_assign (refv, ref, end);
9564 : 126 : ref = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), refv);
9565 : 126 : SET_DECL_VALUE_EXPR (placeholder, ref);
9566 : 126 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
9567 : 126 : tree d = maybe_lookup_decl (decl, ctx);
9568 : 126 : gcc_assert (d);
9569 : 126 : if (DECL_HAS_VALUE_EXPR_P (d))
9570 : 7 : oldv = DECL_VALUE_EXPR (d);
9571 : 126 : if (omp_privatize_by_reference (var))
9572 : : {
9573 : 18 : tree v = fold_convert (TREE_TYPE (d),
9574 : : build_fold_addr_expr (new_var));
9575 : 18 : SET_DECL_VALUE_EXPR (d, v);
9576 : : }
9577 : : else
9578 : 108 : SET_DECL_VALUE_EXPR (d, new_var);
9579 : 126 : DECL_HAS_VALUE_EXPR_P (d) = 1;
9580 : 126 : lower_omp (&OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c), ctx);
9581 : 126 : if (oldv)
9582 : 7 : SET_DECL_VALUE_EXPR (d, oldv);
9583 : : else
9584 : : {
9585 : 119 : SET_DECL_VALUE_EXPR (d, NULL_TREE);
9586 : 119 : DECL_HAS_VALUE_EXPR_P (d) = 0;
9587 : : }
9588 : 126 : gimple_seq_add_seq (end, OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
9589 : 126 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
9590 : 126 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9591 : 24 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL;
9592 : 126 : if (cancellable)
9593 : 4 : gimple_seq_add_stmt (end, gimple_build_label (lab6));
9594 : 126 : tree x = lang_hooks.decls.omp_clause_dtor (c, new_var);
9595 : 126 : if (x)
9596 : : {
9597 : 28 : gimple_seq tseq = NULL;
9598 : 28 : gimplify_stmt (&x, &tseq);
9599 : 28 : gimple_seq_add_seq (end, tseq);
9600 : : }
9601 : : }
9602 : : else
9603 : : {
9604 : 1030 : tree x = build2 (rcode, TREE_TYPE (ref), ref, new_var);
9605 : 1030 : ref = unshare_expr (ref);
9606 : 1030 : gimplify_assign (ref, x, end);
9607 : : }
9608 : 1641 : gimple_seq_add_stmt (end, gimple_build_label (lab4));
9609 : 1641 : ++cnt;
9610 : 1641 : field = DECL_CHAIN (bfield);
9611 : : }
9612 : : }
9613 : :
9614 : 1160 : if (code == OMP_TASKGROUP)
9615 : : {
9616 : 327 : t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_REGISTER);
9617 : 327 : g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
9618 : 327 : gimple_seq_add_stmt (start, g);
9619 : : }
9620 : : else
9621 : : {
9622 : 833 : tree c;
9623 : 833 : if (code == OMP_FOR)
9624 : 227 : c = gimple_omp_for_clauses (ctx->stmt);
9625 : 606 : else if (code == OMP_SECTIONS)
9626 : 8 : c = gimple_omp_sections_clauses (ctx->stmt);
9627 : 598 : else if (code == OMP_SCOPE)
9628 : 46 : c = gimple_omp_scope_clauses (ctx->stmt);
9629 : : else
9630 : 552 : c = gimple_omp_taskreg_clauses (ctx->stmt);
9631 : 833 : c = omp_find_clause (c, OMP_CLAUSE__REDUCTEMP_);
9632 : 833 : t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)),
9633 : : build_fold_addr_expr (avar));
9634 : 833 : gimplify_assign (OMP_CLAUSE_DECL (c), t, start);
9635 : : }
9636 : :
9637 : 1160 : gimple_seq_add_stmt (end, gimple_build_assign (data, PLUS_EXPR, data, sz));
9638 : 1160 : gimple_seq_add_stmt (end, gimple_build_assign (idx, PLUS_EXPR, idx,
9639 : : size_one_node));
9640 : 1160 : g = gimple_build_cond (NE_EXPR, idx, num_thr_sz, lab1, lab2);
9641 : 1160 : gimple_seq_add_stmt (end, g);
9642 : 1160 : gimple_seq_add_stmt (end, gimple_build_label (lab2));
9643 : 1160 : if (code == OMP_FOR || code == OMP_SECTIONS || code == OMP_SCOPE)
9644 : : {
9645 : 281 : enum built_in_function bfn
9646 : : = BUILT_IN_GOMP_WORKSHARE_TASK_REDUCTION_UNREGISTER;
9647 : 281 : t = builtin_decl_explicit (bfn);
9648 : 281 : tree c_bool_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t)));
9649 : 281 : tree arg;
9650 : 281 : if (cancellable)
9651 : : {
9652 : 14 : arg = create_tmp_var (c_bool_type);
9653 : 14 : gimple_seq_add_stmt (end, gimple_build_assign (arg, NOP_EXPR,
9654 : : cancellable));
9655 : : }
9656 : : else
9657 : 267 : arg = build_int_cst (c_bool_type, 0);
9658 : 281 : g = gimple_build_call (t, 1, arg);
9659 : 281 : }
9660 : : else
9661 : : {
9662 : 879 : t = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_REDUCTION_UNREGISTER);
9663 : 879 : g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
9664 : : }
9665 : 1160 : gimple_seq_add_stmt (end, g);
9666 : 1160 : if (lab7)
9667 : 493 : gimple_seq_add_stmt (end, gimple_build_label (lab7));
9668 : 1160 : t = build_constructor (atype, NULL);
9669 : 1160 : TREE_THIS_VOLATILE (t) = 1;
9670 : 1160 : 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 : 536 : lower_omp_taskgroup (gimple_stmt_iterator *gsi_p, omp_context *ctx)
9677 : : {
9678 : 536 : gimple *stmt = gsi_stmt (*gsi_p);
9679 : 536 : gcall *x;
9680 : 536 : gbind *bind;
9681 : 536 : gimple_seq dseq = NULL;
9682 : 536 : tree block = make_node (BLOCK);
9683 : :
9684 : 536 : bind = gimple_build_bind (NULL, NULL, block);
9685 : 536 : gsi_replace (gsi_p, bind, true);
9686 : 536 : gimple_bind_add_stmt (bind, stmt);
9687 : :
9688 : 536 : push_gimplify_context ();
9689 : :
9690 : 536 : x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_START),
9691 : : 0);
9692 : 536 : gimple_bind_add_stmt (bind, x);
9693 : :
9694 : 536 : lower_omp_task_reductions (ctx, OMP_TASKGROUP,
9695 : : gimple_omp_taskgroup_clauses (stmt),
9696 : : gimple_bind_body_ptr (bind), &dseq);
9697 : :
9698 : 536 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
9699 : 536 : gimple_bind_add_seq (bind, gimple_omp_body (stmt));
9700 : 536 : gimple_omp_set_body (stmt, NULL);
9701 : :
9702 : 536 : gimple_bind_add_seq (bind, dseq);
9703 : :
9704 : 536 : pop_gimplify_context (bind);
9705 : :
9706 : 536 : gimple_bind_append_vars (bind, ctx->block_vars);
9707 : 536 : BLOCK_VARS (block) = ctx->block_vars;
9708 : 536 : }
9709 : :
9710 : :
9711 : : /* Fold the OMP_ORDERED_CLAUSES for the OMP_ORDERED in STMT if possible. */
9712 : :
9713 : : static void
9714 : 0 : lower_omp_ordered_clauses (gimple_stmt_iterator *gsi_p, gomp_ordered *ord_stmt,
9715 : : omp_context *ctx)
9716 : : {
9717 : 0 : struct omp_for_data fd;
9718 : 0 : if (!ctx->outer || gimple_code (ctx->outer->stmt) != GIMPLE_OMP_FOR)
9719 : 0 : return;
9720 : :
9721 : 0 : unsigned int len = gimple_omp_for_collapse (ctx->outer->stmt);
9722 : 0 : struct omp_for_data_loop *loops = XALLOCAVEC (struct omp_for_data_loop, len);
9723 : 0 : omp_extract_for_data (as_a <gomp_for *> (ctx->outer->stmt), &fd, loops);
9724 : 0 : if (!fd.ordered)
9725 : : return;
9726 : :
9727 : 0 : tree *list_p = gimple_omp_ordered_clauses_ptr (ord_stmt);
9728 : 0 : tree c = gimple_omp_ordered_clauses (ord_stmt);
9729 : 0 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
9730 : 0 : && 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 : 0 : gimple_stmt_iterator gsi = *gsi_p;
9737 : 0 : gsi_next (&gsi);
9738 : 0 : while (!gsi_end_p (gsi))
9739 : : {
9740 : 0 : gimple *stmt = gsi_stmt (gsi);
9741 : 0 : if (is_gimple_debug (stmt)
9742 : 0 : || gimple_code (stmt) == GIMPLE_NOP)
9743 : : {
9744 : 0 : gsi_next (&gsi);
9745 : 0 : continue;
9746 : : }
9747 : 0 : if (gimple_code (stmt) != GIMPLE_OMP_ORDERED)
9748 : : break;
9749 : 0 : gomp_ordered *ord_stmt2 = as_a <gomp_ordered *> (stmt);
9750 : 0 : c = gimple_omp_ordered_clauses (ord_stmt2);
9751 : 0 : if (c == NULL_TREE
9752 : 0 : || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DOACROSS
9753 : 0 : || OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
9754 : : break;
9755 : 0 : while (*list_p)
9756 : 0 : list_p = &OMP_CLAUSE_CHAIN (*list_p);
9757 : 0 : *list_p = c;
9758 : 0 : 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 : 0 : if (fd.collapse > 1)
9802 : : return;
9803 : :
9804 : 0 : 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 : 0 : for (unsigned i = 0; i != 2 * len - 1; ++i)
9808 : 0 : 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 : 0 : while ((c = *list_p) != NULL)
9817 : : {
9818 : 0 : bool remove = false;
9819 : :
9820 : 0 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS);
9821 : 0 : if (OMP_CLAUSE_DOACROSS_KIND (c) != OMP_CLAUSE_DOACROSS_SINK)
9822 : 0 : goto next_ordered_clause;
9823 : :
9824 : 0 : tree vec;
9825 : 0 : for (vec = OMP_CLAUSE_DECL (c), i = 0;
9826 : 0 : vec && TREE_CODE (vec) == TREE_LIST;
9827 : 0 : vec = TREE_CHAIN (vec), ++i)
9828 : : {
9829 : 0 : gcc_assert (i < len);
9830 : :
9831 : : /* omp_extract_for_data has canonicalized the condition. */
9832 : 0 : gcc_assert (fd.loops[i].cond_code == LT_EXPR
9833 : : || fd.loops[i].cond_code == GT_EXPR);
9834 : 0 : bool forward = fd.loops[i].cond_code == LT_EXPR;
9835 : 0 : bool maybe_lexically_later = true;
9836 : :
9837 : : /* While the committee makes up its mind, bail if we have any
9838 : : non-constant steps. */
9839 : 0 : if (TREE_CODE (fd.loops[i].step) != INTEGER_CST)
9840 : 0 : goto lower_omp_ordered_ret;
9841 : :
9842 : 0 : tree itype = TREE_TYPE (TREE_VALUE (vec));
9843 : 0 : if (POINTER_TYPE_P (itype))
9844 : 0 : itype = sizetype;
9845 : 0 : wide_int offset = wide_int::from (wi::to_wide (TREE_PURPOSE (vec)),
9846 : 0 : TYPE_PRECISION (itype),
9847 : 0 : TYPE_SIGN (itype));
9848 : :
9849 : : /* Ignore invalid offsets that are not multiples of the step. */
9850 : 0 : if (!wi::multiple_of_p (wi::abs (offset),
9851 : 0 : wi::abs (wi::to_wide (fd.loops[i].step)),
9852 : : UNSIGNED))
9853 : : {
9854 : 0 : 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 : 0 : remove = true;
9858 : 0 : 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 : 0 : if (i == 0)
9866 : : {
9867 : : /* Ignore dependence vectors whose first dimension is 0. */
9868 : 0 : if (offset == 0)
9869 : : {
9870 : 0 : remove = true;
9871 : 0 : goto next_ordered_clause;
9872 : : }
9873 : : else
9874 : : {
9875 : 0 : if (!TYPE_UNSIGNED (itype) && (forward ^ wi::neg_p (offset)))
9876 : : {
9877 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
9878 : : "first offset must be in opposite direction "
9879 : : "of loop iterations");
9880 : 0 : goto lower_omp_ordered_ret;
9881 : : }
9882 : 0 : if (forward)
9883 : 0 : offset = -offset;
9884 : 0 : neg_offset_p = forward;
9885 : : /* Initialize the first time around. */
9886 : 0 : if (folded_dep == NULL_TREE)
9887 : : {
9888 : 0 : folded_dep = c;
9889 : 0 : folded_deps[0] = offset;
9890 : : }
9891 : : else
9892 : 0 : folded_deps[0] = wi::gcd (folded_deps[0],
9893 : 0 : offset, UNSIGNED);
9894 : : }
9895 : : }
9896 : : /* Calculate minimum for the remaining dimensions. */
9897 : : else
9898 : : {
9899 : 0 : folded_deps[len + i - 1] = offset;
9900 : 0 : if (folded_dep == c)
9901 : 0 : folded_deps[i] = offset;
9902 : 0 : else if (maybe_lexically_later
9903 : 0 : && !wi::eq_p (folded_deps[i], offset))
9904 : : {
9905 : 0 : if (forward ^ wi::gts_p (folded_deps[i], offset))
9906 : : {
9907 : : unsigned int j;
9908 : 0 : folded_dep = c;
9909 : 0 : for (j = 1; j <= i; j++)
9910 : 0 : folded_deps[j] = folded_deps[len + j - 1];
9911 : : }
9912 : : else
9913 : 0 : maybe_lexically_later = false;
9914 : : }
9915 : : }
9916 : 0 : }
9917 : 0 : gcc_assert (i == len);
9918 : :
9919 : : remove = true;
9920 : :
9921 : 0 : next_ordered_clause:
9922 : 0 : if (remove)
9923 : 0 : *list_p = OMP_CLAUSE_CHAIN (c);
9924 : : else
9925 : 0 : list_p = &OMP_CLAUSE_CHAIN (c);
9926 : : }
9927 : :
9928 : 0 : if (folded_dep)
9929 : : {
9930 : 0 : if (neg_offset_p)
9931 : 0 : folded_deps[0] = -folded_deps[0];
9932 : :
9933 : 0 : tree itype = TREE_TYPE (TREE_VALUE (OMP_CLAUSE_DECL (folded_dep)));
9934 : 0 : if (POINTER_TYPE_P (itype))
9935 : 0 : itype = sizetype;
9936 : :
9937 : 0 : TREE_PURPOSE (OMP_CLAUSE_DECL (folded_dep))
9938 : 0 : = wide_int_to_tree (itype, folded_deps[0]);
9939 : 0 : OMP_CLAUSE_CHAIN (folded_dep) = gimple_omp_ordered_clauses (ord_stmt);
9940 : 0 : *gimple_omp_ordered_clauses_ptr (ord_stmt) = folded_dep;
9941 : : }
9942 : :
9943 : 0 : 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 : 0 : if (gimple_omp_ordered_clauses (ord_stmt) == NULL_TREE)
9948 : 0 : gsi_replace (gsi_p, gimple_build_nop (), true);
9949 : : }
9950 : :
9951 : :
9952 : : /* Expand code for an OpenMP ordered directive. */
9953 : :
9954 : : static void
9955 : 1116 : lower_omp_ordered (gimple_stmt_iterator *gsi_p, omp_context *ctx)
9956 : : {
9957 : 1116 : tree block;
9958 : 1116 : gimple *stmt = gsi_stmt (*gsi_p), *g;
9959 : 1116 : gomp_ordered *ord_stmt = as_a <gomp_ordered *> (stmt);
9960 : 1116 : gcall *x;
9961 : 1116 : gbind *bind;
9962 : 1116 : bool simd = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
9963 : 1116 : OMP_CLAUSE_SIMD);
9964 : : /* FIXME: this should check presence of OMP_CLAUSE__SIMT_ on the enclosing
9965 : : loop. */
9966 : 1116 : bool maybe_simt
9967 : 1116 : = simd && omp_maybe_offloaded_ctx (ctx) && omp_max_simt_vf () > 1;
9968 : 1116 : bool threads = omp_find_clause (gimple_omp_ordered_clauses (ord_stmt),
9969 : 1116 : OMP_CLAUSE_THREADS);
9970 : :
9971 : 1116 : 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 : 1116 : return;
9980 : : }
9981 : :
9982 : 411 : push_gimplify_context ();
9983 : :
9984 : 411 : block = make_node (BLOCK);
9985 : 411 : bind = gimple_build_bind (NULL, NULL, block);
9986 : 411 : gsi_replace (gsi_p, bind, true);
9987 : 411 : gimple_bind_add_stmt (bind, stmt);
9988 : :
9989 : 411 : if (simd)
9990 : : {
9991 : 79 : x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_START, 1,
9992 : 79 : build_int_cst (NULL_TREE, threads));
9993 : 79 : cfun->has_simduid_loops = true;
9994 : : }
9995 : : else
9996 : 332 : x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_START),
9997 : : 0);
9998 : 411 : gimple_bind_add_stmt (bind, x);
9999 : :
10000 : 411 : tree counter = NULL_TREE, test = NULL_TREE, body = NULL_TREE;
10001 : 411 : if (maybe_simt)
10002 : : {
10003 : 0 : counter = create_tmp_var (integer_type_node);
10004 : 0 : g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
10005 : 0 : gimple_call_set_lhs (g, counter);
10006 : 0 : gimple_bind_add_stmt (bind, g);
10007 : :
10008 : 0 : body = create_artificial_label (UNKNOWN_LOCATION);
10009 : 0 : test = create_artificial_label (UNKNOWN_LOCATION);
10010 : 0 : gimple_bind_add_stmt (bind, gimple_build_label (body));
10011 : :
10012 : 0 : tree simt_pred = create_tmp_var (integer_type_node);
10013 : 0 : g = gimple_build_call_internal (IFN_GOMP_SIMT_ORDERED_PRED, 1, counter);
10014 : 0 : gimple_call_set_lhs (g, simt_pred);
10015 : 0 : gimple_bind_add_stmt (bind, g);
10016 : :
10017 : 0 : tree t = create_artificial_label (UNKNOWN_LOCATION);
10018 : 0 : g = gimple_build_cond (EQ_EXPR, simt_pred, integer_zero_node, t, test);
10019 : 0 : gimple_bind_add_stmt (bind, g);
10020 : :
10021 : 0 : gimple_bind_add_stmt (bind, gimple_build_label (t));
10022 : : }
10023 : 411 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
10024 : 411 : gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
10025 : 411 : gimple_bind_add_seq (bind, gimple_omp_body (stmt));
10026 : 411 : gimple_omp_set_body (stmt, NULL);
10027 : :
10028 : 411 : if (maybe_simt)
10029 : : {
10030 : 0 : gimple_bind_add_stmt (bind, gimple_build_label (test));
10031 : 0 : g = gimple_build_assign (counter, MINUS_EXPR, counter, integer_one_node);
10032 : 0 : gimple_bind_add_stmt (bind, g);
10033 : :
10034 : 0 : tree c = build2 (GE_EXPR, boolean_type_node, counter, integer_zero_node);
10035 : 0 : tree nonneg = create_tmp_var (integer_type_node);
10036 : 0 : gimple_seq tseq = NULL;
10037 : 0 : gimplify_assign (nonneg, fold_convert (integer_type_node, c), &tseq);
10038 : 0 : gimple_bind_add_seq (bind, tseq);
10039 : :
10040 : 0 : g = gimple_build_call_internal (IFN_GOMP_SIMT_VOTE_ANY, 1, nonneg);
10041 : 0 : gimple_call_set_lhs (g, nonneg);
10042 : 0 : gimple_bind_add_stmt (bind, g);
10043 : :
10044 : 0 : tree end = create_artificial_label (UNKNOWN_LOCATION);
10045 : 0 : g = gimple_build_cond (NE_EXPR, nonneg, integer_zero_node, body, end);
10046 : 0 : gimple_bind_add_stmt (bind, g);
10047 : :
10048 : 0 : gimple_bind_add_stmt (bind, gimple_build_label (end));
10049 : : }
10050 : 411 : if (simd)
10051 : 79 : x = gimple_build_call_internal (IFN_GOMP_SIMD_ORDERED_END, 1,
10052 : 79 : build_int_cst (NULL_TREE, threads));
10053 : : else
10054 : 332 : x = gimple_build_call (builtin_decl_explicit (BUILT_IN_GOMP_ORDERED_END),
10055 : : 0);
10056 : 411 : gimple_bind_add_stmt (bind, x);
10057 : :
10058 : 411 : gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
10059 : :
10060 : 411 : pop_gimplify_context (bind);
10061 : :
10062 : 411 : gimple_bind_append_vars (bind, ctx->block_vars);
10063 : 411 : 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 : 1536 : lower_omp_scan (gimple_stmt_iterator *gsi_p, omp_context *ctx)
10072 : : {
10073 : 1536 : gimple *stmt = gsi_stmt (*gsi_p);
10074 : 1536 : bool has_clauses
10075 : 1536 : = gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt)) != NULL;
10076 : 1536 : tree lane = NULL_TREE;
10077 : 1536 : gimple_seq before = NULL;
10078 : 1536 : omp_context *octx = ctx->outer;
10079 : 1536 : gcc_assert (octx);
10080 : 1536 : if (octx->scan_exclusive && !has_clauses)
10081 : : {
10082 : 536 : gimple_stmt_iterator gsi2 = *gsi_p;
10083 : 536 : gsi_next (&gsi2);
10084 : 536 : 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 : 536 : if (stmt2
10089 : 274 : && gimple_code (stmt2) == GIMPLE_OMP_SCAN
10090 : 804 : && gimple_omp_scan_clauses (as_a <gomp_scan *> (stmt2)) != NULL)
10091 : : {
10092 : 268 : gsi_remove (gsi_p, false);
10093 : 268 : gsi_insert_after (gsi_p, stmt, GSI_SAME_STMT);
10094 : 268 : ctx = maybe_lookup_ctx (stmt2);
10095 : 268 : gcc_assert (ctx);
10096 : 268 : lower_omp_scan (gsi_p, ctx);
10097 : 268 : return;
10098 : : }
10099 : : }
10100 : :
10101 : 1268 : bool input_phase = has_clauses ^ octx->scan_inclusive;
10102 : 1268 : bool is_simd = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
10103 : 1268 : && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_SIMD);
10104 : 1268 : bool is_for = (gimple_code (octx->stmt) == GIMPLE_OMP_FOR
10105 : 1268 : && gimple_omp_for_kind (octx->stmt) == GF_OMP_FOR_KIND_FOR
10106 : 1614 : && !gimple_omp_for_combined_p (octx->stmt));
10107 : 1268 : bool is_for_simd = is_simd && gimple_omp_for_combined_into_p (octx->stmt);
10108 : 332 : if (is_for_simd && octx->for_simd_scan_phase)
10109 : : is_simd = false;
10110 : 1102 : if (is_simd)
10111 : 756 : if (tree c = omp_find_clause (gimple_omp_for_clauses (octx->stmt),
10112 : : OMP_CLAUSE__SIMDUID_))
10113 : : {
10114 : 368 : tree uid = OMP_CLAUSE__SIMDUID__DECL (c);
10115 : 368 : lane = create_tmp_var (unsigned_type_node);
10116 : 368 : tree t = build_int_cst (integer_type_node,
10117 : 552 : input_phase ? 1
10118 : 184 : : octx->scan_inclusive ? 2 : 3);
10119 : 368 : gimple *g
10120 : 368 : = gimple_build_call_internal (IFN_GOMP_SIMD_LANE, 2, uid, t);
10121 : 368 : gimple_call_set_lhs (g, lane);
10122 : 368 : gimple_seq_add_stmt (&before, g);
10123 : : }
10124 : :
10125 : 1268 : if (is_simd || is_for)
10126 : : {
10127 : 4980 : for (tree c = gimple_omp_for_clauses (octx->stmt);
10128 : 4980 : c; c = OMP_CLAUSE_CHAIN (c))
10129 : 4044 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10130 : 4044 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
10131 : : {
10132 : 1160 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
10133 : 1160 : tree var = OMP_CLAUSE_DECL (c);
10134 : 1160 : tree new_var = lookup_decl (var, octx);
10135 : 1160 : tree val = new_var;
10136 : 1160 : tree var2 = NULL_TREE;
10137 : 1160 : tree var3 = NULL_TREE;
10138 : 1160 : tree var4 = NULL_TREE;
10139 : 1160 : tree lane0 = NULL_TREE;
10140 : 1160 : tree new_vard = new_var;
10141 : 1160 : if (omp_privatize_by_reference (var))
10142 : : {
10143 : 264 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
10144 : 264 : val = new_var;
10145 : : }
10146 : 1160 : if (DECL_HAS_VALUE_EXPR_P (new_vard))
10147 : : {
10148 : 464 : val = DECL_VALUE_EXPR (new_vard);
10149 : 464 : if (new_vard != new_var)
10150 : : {
10151 : 108 : gcc_assert (TREE_CODE (val) == ADDR_EXPR);
10152 : 108 : val = TREE_OPERAND (val, 0);
10153 : : }
10154 : 464 : if (TREE_CODE (val) == ARRAY_REF
10155 : 464 : && VAR_P (TREE_OPERAND (val, 0)))
10156 : : {
10157 : 464 : tree v = TREE_OPERAND (val, 0);
10158 : 464 : if (lookup_attribute ("omp simd array",
10159 : 464 : DECL_ATTRIBUTES (v)))
10160 : : {
10161 : 464 : val = unshare_expr (val);
10162 : 464 : lane0 = TREE_OPERAND (val, 1);
10163 : 464 : TREE_OPERAND (val, 1) = lane;
10164 : 464 : var2 = lookup_decl (v, octx);
10165 : 464 : if (octx->scan_exclusive)
10166 : 228 : var4 = lookup_decl (var2, octx);
10167 : 464 : if (input_phase
10168 : 464 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10169 : 120 : var3 = maybe_lookup_decl (var4 ? var4 : var2, octx);
10170 : 464 : if (!input_phase)
10171 : : {
10172 : 232 : var2 = build4 (ARRAY_REF, TREE_TYPE (val),
10173 : : var2, lane, NULL_TREE, NULL_TREE);
10174 : 232 : TREE_THIS_NOTRAP (var2) = 1;
10175 : 232 : if (octx->scan_exclusive)
10176 : : {
10177 : 114 : var4 = build4 (ARRAY_REF, TREE_TYPE (val),
10178 : : var4, lane, NULL_TREE,
10179 : : NULL_TREE);
10180 : 114 : TREE_THIS_NOTRAP (var4) = 1;
10181 : : }
10182 : : }
10183 : : else
10184 : : var2 = val;
10185 : : }
10186 : : }
10187 : 464 : gcc_assert (var2);
10188 : : }
10189 : : else
10190 : : {
10191 : 696 : var2 = build_outer_var_ref (var, octx);
10192 : 696 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10193 : : {
10194 : 220 : var3 = maybe_lookup_decl (new_vard, octx);
10195 : 220 : if (var3 == new_vard || var3 == NULL_TREE)
10196 : : var3 = NULL_TREE;
10197 : 108 : else if (is_simd && octx->scan_exclusive && !input_phase)
10198 : : {
10199 : 16 : var4 = maybe_lookup_decl (var3, octx);
10200 : 16 : if (var4 == var3 || var4 == NULL_TREE)
10201 : : {
10202 : 0 : if (TREE_ADDRESSABLE (TREE_TYPE (new_var)))
10203 : : {
10204 : : var4 = var3;
10205 : : var3 = NULL_TREE;
10206 : : }
10207 : : else
10208 : 16 : var4 = NULL_TREE;
10209 : : }
10210 : : }
10211 : : }
10212 : 664 : if (is_simd
10213 : 484 : && octx->scan_exclusive
10214 : 244 : && !input_phase
10215 : 244 : && var4 == NULL_TREE)
10216 : 106 : var4 = create_tmp_var (TREE_TYPE (val));
10217 : : }
10218 : 1160 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
10219 : : {
10220 : 376 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
10221 : 376 : if (input_phase)
10222 : : {
10223 : 188 : if (var3)
10224 : : {
10225 : : /* If we've added a separate identity element
10226 : : variable, copy it over into val. */
10227 : 92 : tree x = lang_hooks.decls.omp_clause_assign_op (c, val,
10228 : : var3);
10229 : 92 : gimplify_and_add (x, &before);
10230 : : }
10231 : 96 : else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
10232 : : {
10233 : : /* Otherwise, assign to it the identity element. */
10234 : 96 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
10235 : 96 : if (is_for)
10236 : 16 : tseq = copy_gimple_seq_and_replace_locals (tseq);
10237 : 96 : tree ref = build_outer_var_ref (var, octx);
10238 : 96 : tree x = (DECL_HAS_VALUE_EXPR_P (new_vard)
10239 : 96 : ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
10240 : 40 : if (x)
10241 : : {
10242 : 40 : if (new_vard != new_var)
10243 : 16 : val = build_fold_addr_expr_loc (clause_loc, val);
10244 : 40 : SET_DECL_VALUE_EXPR (new_vard, val);
10245 : : }
10246 : 96 : SET_DECL_VALUE_EXPR (placeholder, ref);
10247 : 96 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
10248 : 96 : lower_omp (&tseq, octx);
10249 : 96 : if (x)
10250 : 40 : SET_DECL_VALUE_EXPR (new_vard, x);
10251 : 96 : SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
10252 : 96 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
10253 : 96 : gimple_seq_add_seq (&before, tseq);
10254 : 96 : if (is_simd)
10255 : 80 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
10256 : : }
10257 : : }
10258 : 188 : else if (is_simd)
10259 : : {
10260 : 156 : tree x;
10261 : 156 : if (octx->scan_exclusive)
10262 : : {
10263 : 72 : tree v4 = unshare_expr (var4);
10264 : 72 : tree v2 = unshare_expr (var2);
10265 : 72 : x = lang_hooks.decls.omp_clause_assign_op (c, v4, v2);
10266 : 72 : gimplify_and_add (x, &before);
10267 : : }
10268 : 156 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
10269 : 156 : x = (DECL_HAS_VALUE_EXPR_P (new_vard)
10270 : 156 : ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
10271 : 156 : tree vexpr = val;
10272 : 156 : if (x && new_vard != new_var)
10273 : 38 : vexpr = build_fold_addr_expr_loc (clause_loc, val);
10274 : 156 : if (x)
10275 : 78 : SET_DECL_VALUE_EXPR (new_vard, vexpr);
10276 : 156 : SET_DECL_VALUE_EXPR (placeholder, var2);
10277 : 156 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
10278 : 156 : lower_omp (&tseq, octx);
10279 : 156 : gimple_seq_add_seq (&before, tseq);
10280 : 156 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
10281 : 156 : if (x)
10282 : 78 : SET_DECL_VALUE_EXPR (new_vard, x);
10283 : 156 : SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
10284 : 156 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
10285 : 156 : if (octx->scan_inclusive)
10286 : : {
10287 : 84 : x = lang_hooks.decls.omp_clause_assign_op (c, val,
10288 : : var2);
10289 : 84 : gimplify_and_add (x, &before);
10290 : : }
10291 : 72 : else if (lane0 == NULL_TREE)
10292 : : {
10293 : 36 : x = lang_hooks.decls.omp_clause_assign_op (c, val,
10294 : : var4);
10295 : 36 : gimplify_and_add (x, &before);
10296 : : }
10297 : : }
10298 : : }
10299 : : else
10300 : : {
10301 : 784 : if (input_phase)
10302 : : {
10303 : : /* input phase. Set val to initializer before
10304 : : the body. */
10305 : 392 : tree x = omp_reduction_init (c, TREE_TYPE (new_var));
10306 : 392 : gimplify_assign (val, x, &before);
10307 : : }
10308 : 392 : else if (is_simd)
10309 : : {
10310 : : /* scan phase. */
10311 : 318 : enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
10312 : 318 : if (code == MINUS_EXPR)
10313 : 0 : code = PLUS_EXPR;
10314 : :
10315 : 318 : tree x = build2 (code, TREE_TYPE (var2),
10316 : : unshare_expr (var2), unshare_expr (val));
10317 : 318 : if (octx->scan_inclusive)
10318 : : {
10319 : 154 : gimplify_assign (unshare_expr (var2), x, &before);
10320 : 154 : gimplify_assign (val, var2, &before);
10321 : : }
10322 : : else
10323 : : {
10324 : 164 : gimplify_assign (unshare_expr (var4),
10325 : : unshare_expr (var2), &before);
10326 : 164 : gimplify_assign (var2, x, &before);
10327 : 164 : if (lane0 == NULL_TREE)
10328 : 86 : gimplify_assign (val, var4, &before);
10329 : : }
10330 : : }
10331 : : }
10332 : 1160 : if (octx->scan_exclusive && !input_phase && lane0)
10333 : : {
10334 : 114 : tree vexpr = unshare_expr (var4);
10335 : 114 : TREE_OPERAND (vexpr, 1) = lane0;
10336 : 114 : if (new_vard != new_var)
10337 : 24 : vexpr = build_fold_addr_expr_loc (clause_loc, vexpr);
10338 : 114 : SET_DECL_VALUE_EXPR (new_vard, vexpr);
10339 : : }
10340 : : }
10341 : : }
10342 : 1268 : if (is_simd && !is_for_simd)
10343 : : {
10344 : 590 : gsi_insert_seq_after (gsi_p, gimple_omp_body (stmt), GSI_SAME_STMT);
10345 : 590 : gsi_insert_seq_after (gsi_p, before, GSI_SAME_STMT);
10346 : 590 : gsi_replace (gsi_p, gimple_build_nop (), true);
10347 : 590 : return;
10348 : : }
10349 : 678 : lower_omp (gimple_omp_body_ptr (stmt), octx);
10350 : 678 : if (before)
10351 : : {
10352 : 256 : gimple_stmt_iterator gsi = gsi_start (*gimple_omp_body_ptr (stmt));
10353 : 256 : 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 : 311 : lower_omp_critical (gimple_stmt_iterator *gsi_p, omp_context *ctx)
10367 : : {
10368 : 311 : tree block;
10369 : 311 : tree name, lock, unlock;
10370 : 311 : gomp_critical *stmt = as_a <gomp_critical *> (gsi_stmt (*gsi_p));
10371 : 311 : gbind *bind;
10372 : 311 : location_t loc = gimple_location (stmt);
10373 : 311 : gimple_seq tbody;
10374 : :
10375 : 311 : name = gimple_omp_critical_name (stmt);
10376 : 311 : if (name)
10377 : : {
10378 : 88 : tree decl;
10379 : :
10380 : 88 : if (!critical_name_mutexes)
10381 : 48 : critical_name_mutexes = hash_map<tree, tree>::create_ggc (10);
10382 : :
10383 : 88 : tree *n = critical_name_mutexes->get (name);
10384 : 88 : if (n == NULL)
10385 : : {
10386 : 70 : char *new_str;
10387 : :
10388 : 70 : decl = create_tmp_var_raw (ptr_type_node);
10389 : :
10390 : 70 : new_str = ACONCAT ((".gomp_critical_user_",
10391 : : IDENTIFIER_POINTER (name), NULL));
10392 : 70 : DECL_NAME (decl) = get_identifier (new_str);
10393 : 70 : TREE_PUBLIC (decl) = 1;
10394 : 70 : TREE_STATIC (decl) = 1;
10395 : 70 : DECL_COMMON (decl) = 1;
10396 : 70 : DECL_ARTIFICIAL (decl) = 1;
10397 : 70 : DECL_IGNORED_P (decl) = 1;
10398 : :
10399 : 70 : varpool_node::finalize_decl (decl);
10400 : :
10401 : 70 : critical_name_mutexes->put (name, decl);
10402 : : }
10403 : : else
10404 : 18 : 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 : 88 : omp_context *octx;
10410 : 88 : if (cgraph_node::get (current_function_decl)->offloadable)
10411 : 1 : varpool_node::get_create (decl)->offloadable = 1;
10412 : : else
10413 : 173 : for (octx = ctx->outer; octx; octx = octx->outer)
10414 : 87 : if (is_gimple_omp_offloaded (octx->stmt))
10415 : : {
10416 : 1 : varpool_node::get_create (decl)->offloadable = 1;
10417 : 1 : break;
10418 : : }
10419 : :
10420 : 88 : lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_START);
10421 : 88 : lock = build_call_expr_loc (loc, lock, 1,
10422 : : build_fold_addr_expr_loc (loc, decl));
10423 : :
10424 : 88 : unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_NAME_END);
10425 : 88 : unlock = build_call_expr_loc (loc, unlock, 1,
10426 : : build_fold_addr_expr_loc (loc, decl));
10427 : : }
10428 : : else
10429 : : {
10430 : 223 : lock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_START);
10431 : 223 : lock = build_call_expr_loc (loc, lock, 0);
10432 : :
10433 : 223 : unlock = builtin_decl_explicit (BUILT_IN_GOMP_CRITICAL_END);
10434 : 223 : unlock = build_call_expr_loc (loc, unlock, 0);
10435 : : }
10436 : :
10437 : 311 : push_gimplify_context ();
10438 : :
10439 : 311 : block = make_node (BLOCK);
10440 : 311 : bind = gimple_build_bind (NULL, NULL, block);
10441 : 311 : gsi_replace (gsi_p, bind, true);
10442 : 311 : gimple_bind_add_stmt (bind, stmt);
10443 : :
10444 : 311 : tbody = gimple_bind_body (bind);
10445 : 311 : gimplify_and_add (lock, &tbody);
10446 : 311 : gimple_bind_set_body (bind, tbody);
10447 : :
10448 : 311 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
10449 : 311 : gimple_omp_set_body (stmt, maybe_catch_exception (gimple_omp_body (stmt)));
10450 : 311 : gimple_bind_add_seq (bind, gimple_omp_body (stmt));
10451 : 311 : gimple_omp_set_body (stmt, NULL);
10452 : :
10453 : 311 : tbody = gimple_bind_body (bind);
10454 : 311 : gimplify_and_add (unlock, &tbody);
10455 : 311 : gimple_bind_set_body (bind, tbody);
10456 : :
10457 : 311 : gimple_bind_add_stmt (bind, gimple_build_omp_return (true));
10458 : :
10459 : 311 : pop_gimplify_context (bind);
10460 : 311 : gimple_bind_append_vars (bind, ctx->block_vars);
10461 : 311 : BLOCK_VARS (block) = gimple_bind_vars (bind);
10462 : 311 : }
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 : 46913 : 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 : 46913 : tree clauses, cond, vinit;
10477 : 46913 : enum tree_code cond_code;
10478 : 46913 : gimple_seq stmts;
10479 : :
10480 : 46913 : cond_code = fd->loop.cond_code;
10481 : 46913 : 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 : 46913 : if (tree_fits_shwi_p (fd->loop.step))
10486 : : {
10487 : 44678 : HOST_WIDE_INT step = tree_to_shwi (fd->loop.step);
10488 : 44678 : if (step == 1 || step == -1)
10489 : 46913 : cond_code = EQ_EXPR;
10490 : : }
10491 : :
10492 : 46913 : tree n2 = fd->loop.n2;
10493 : 46913 : if (fd->collapse > 1
10494 : 11197 : && TREE_CODE (n2) != INTEGER_CST
10495 : 53108 : && gimple_omp_for_combined_into_p (fd->for_stmt))
10496 : : {
10497 : 2680 : struct omp_context *taskreg_ctx = NULL;
10498 : 2680 : if (gimple_code (ctx->outer->stmt) == GIMPLE_OMP_FOR)
10499 : : {
10500 : 1220 : gomp_for *gfor = as_a <gomp_for *> (ctx->outer->stmt);
10501 : 1220 : if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_FOR
10502 : 1220 : || gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_DISTRIBUTE)
10503 : : {
10504 : 1188 : if (gimple_omp_for_combined_into_p (gfor))
10505 : : {
10506 : 667 : gcc_assert (ctx->outer->outer
10507 : : && is_parallel_ctx (ctx->outer->outer));
10508 : : taskreg_ctx = ctx->outer->outer;
10509 : : }
10510 : : else
10511 : : {
10512 : 521 : struct omp_for_data outer_fd;
10513 : 521 : omp_extract_for_data (gfor, &outer_fd, NULL);
10514 : 521 : n2 = fold_convert (TREE_TYPE (n2), outer_fd.loop.n2);
10515 : : }
10516 : : }
10517 : 32 : else if (gimple_omp_for_kind (gfor) == GF_OMP_FOR_KIND_TASKLOOP)
10518 : 32 : taskreg_ctx = ctx->outer->outer;
10519 : : }
10520 : 1460 : else if (is_taskreg_ctx (ctx->outer))
10521 : : taskreg_ctx = ctx->outer;
10522 : 2680 : if (taskreg_ctx)
10523 : : {
10524 : 2159 : int i;
10525 : 2159 : tree taskreg_clauses
10526 : 2159 : = gimple_omp_taskreg_clauses (taskreg_ctx->stmt);
10527 : 2159 : tree innerc = omp_find_clause (taskreg_clauses,
10528 : : OMP_CLAUSE__LOOPTEMP_);
10529 : 2159 : gcc_assert (innerc);
10530 : 2159 : int count = fd->collapse;
10531 : 2159 : if (fd->non_rect
10532 : 24 : && fd->last_nonrect == fd->first_nonrect + 1)
10533 : 12 : if (tree v = gimple_omp_for_index (fd->for_stmt, fd->last_nonrect))
10534 : 12 : if (!TYPE_UNSIGNED (TREE_TYPE (v)))
10535 : 12 : count += 4;
10536 : 8549 : for (i = 0; i < count; i++)
10537 : : {
10538 : 6390 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
10539 : : OMP_CLAUSE__LOOPTEMP_);
10540 : 6390 : gcc_assert (innerc);
10541 : : }
10542 : 2159 : innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
10543 : : OMP_CLAUSE__LOOPTEMP_);
10544 : 2159 : if (innerc)
10545 : 1411 : n2 = fold_convert (TREE_TYPE (n2),
10546 : : lookup_decl (OMP_CLAUSE_DECL (innerc),
10547 : : taskreg_ctx));
10548 : : }
10549 : : }
10550 : 46913 : cond = build2 (cond_code, boolean_type_node, fd->loop.v, n2);
10551 : :
10552 : 46913 : clauses = gimple_omp_for_clauses (fd->for_stmt);
10553 : 46913 : stmts = NULL;
10554 : 46913 : lower_lastprivate_clauses (clauses, cond, body_p, &stmts, clist, ctx);
10555 : 46913 : if (!gimple_seq_empty_p (stmts))
10556 : : {
10557 : 16367 : gimple_seq_add_seq (&stmts, *dlist);
10558 : 16367 : *dlist = stmts;
10559 : :
10560 : : /* Optimize: v = 0; is usually cheaper than v = some_other_constant. */
10561 : 16367 : vinit = fd->loop.n1;
10562 : 16367 : if (cond_code == EQ_EXPR
10563 : 14375 : && tree_fits_shwi_p (fd->loop.n2)
10564 : 25945 : && ! integer_zerop (fd->loop.n2))
10565 : 8362 : vinit = build_int_cst (TREE_TYPE (fd->loop.v), 0);
10566 : : else
10567 : 8005 : 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 : 16367 : gimplify_assign (fd->loop.v, vinit, body_p);
10572 : : }
10573 : 46913 : }
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 : 19090 : 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 : 19090 : const dump_user_location_t d_u_loc
10597 : 19090 : = dump_user_location_t::from_location_t (loc);
10598 : : /* PR100695 "Format decoder, quoting in 'dump_printf' etc." */
10599 : : #if __GNUC__ >= 10
10600 : 19090 : # pragma GCC diagnostic push
10601 : 19090 : # pragma GCC diagnostic ignored "-Wformat"
10602 : : #endif
10603 : 19090 : dump_printf_loc (l_dump_flags, d_u_loc,
10604 : : "variable %<%T%> ", decl);
10605 : : #if __GNUC__ >= 10
10606 : 19090 : # pragma GCC diagnostic pop
10607 : : #endif
10608 : 19090 : if (c)
10609 : 2826 : dump_printf (l_dump_flags,
10610 : : "in %qs clause ",
10611 : 2826 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10612 : : else
10613 : 16264 : dump_printf (l_dump_flags,
10614 : : "declared in block ");
10615 : 19090 : }
10616 : :
10617 : : static bool
10618 : 40768 : oacc_privatization_candidate_p (const location_t loc, const tree c,
10619 : : const tree decl)
10620 : : {
10621 : 40768 : dump_flags_t l_dump_flags = get_openacc_privatization_dump_flags ();
10622 : :
10623 : : /* There is some differentiation depending on block vs. clause. */
10624 : 40768 : bool block = !c;
10625 : :
10626 : 40768 : bool res = true;
10627 : :
10628 : 40768 : 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 : 694 : gcc_checking_assert (TREE_CODE (decl) != PARM_DECL);
10633 : :
10634 : 694 : res = false;
10635 : :
10636 : 694 : if (dump_enabled_p ())
10637 : : {
10638 : 683 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10639 : 683 : dump_printf (l_dump_flags,
10640 : : "potentially has improper OpenACC privatization level: %qs\n",
10641 : 683 : get_tree_code_name (TREE_CODE (decl)));
10642 : : }
10643 : : }
10644 : :
10645 : 40757 : if (res && block && TREE_STATIC (decl))
10646 : : {
10647 : 34 : res = false;
10648 : :
10649 : 34 : if (dump_enabled_p ())
10650 : : {
10651 : 28 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10652 : 28 : dump_printf (l_dump_flags,
10653 : : "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
10654 : : "static");
10655 : : }
10656 : : }
10657 : :
10658 : 40762 : if (res && block && DECL_EXTERNAL (decl))
10659 : : {
10660 : 12 : res = false;
10661 : :
10662 : 12 : if (dump_enabled_p ())
10663 : : {
10664 : 12 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10665 : 12 : dump_printf (l_dump_flags,
10666 : : "isn%'t candidate for adjusting OpenACC privatization level: %s\n",
10667 : : "external");
10668 : : }
10669 : : }
10670 : :
10671 : 40768 : if (res && !TREE_ADDRESSABLE (decl))
10672 : : {
10673 : 38937 : res = false;
10674 : :
10675 : 38937 : if (dump_enabled_p ())
10676 : : {
10677 : 17532 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10678 : 17532 : 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 : 18623 : if (res && block && DECL_ARTIFICIAL (decl))
10694 : : {
10695 : 547 : res = false;
10696 : :
10697 : 547 : if (dump_enabled_p ())
10698 : : {
10699 : 360 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10700 : 360 : dump_printf (l_dump_flags,
10701 : : "isn%'t candidate for adjusting OpenACC privatization "
10702 : : "level: %s\n", "artificial");
10703 : : }
10704 : : }
10705 : :
10706 : 40581 : if (res)
10707 : : {
10708 : 544 : if (dump_enabled_p ())
10709 : : {
10710 : 475 : oacc_privatization_begin_diagnose_var (l_dump_flags, loc, c, decl);
10711 : 475 : dump_printf (l_dump_flags,
10712 : : "is candidate for adjusting OpenACC privatization level\n");
10713 : : }
10714 : : }
10715 : :
10716 : 40768 : if (dump_file && (dump_flags & TDF_DETAILS))
10717 : : {
10718 : 0 : print_generic_decl (dump_file, decl, dump_flags);
10719 : 0 : fprintf (dump_file, "\n");
10720 : : }
10721 : :
10722 : 40768 : return res;
10723 : : }
10724 : :
10725 : : /* Scan CLAUSES for candidates for adjusting OpenACC privatization level in
10726 : : CTX. */
10727 : :
10728 : : static void
10729 : 11341 : oacc_privatization_scan_clause_chain (omp_context *ctx, tree clauses)
10730 : : {
10731 : 35265 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
10732 : 23924 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE)
10733 : : {
10734 : 13003 : tree decl = OMP_CLAUSE_DECL (c);
10735 : :
10736 : 13003 : tree new_decl = lookup_decl (decl, ctx);
10737 : :
10738 : 13003 : if (!oacc_privatization_candidate_p (OMP_CLAUSE_LOCATION (c), c,
10739 : : new_decl))
10740 : 12669 : continue;
10741 : :
10742 : 334 : gcc_checking_assert
10743 : : (!ctx->oacc_privatization_candidates.contains (new_decl));
10744 : 334 : ctx->oacc_privatization_candidates.safe_push (new_decl);
10745 : : }
10746 : 11341 : }
10747 : :
10748 : : /* Scan DECLS for candidates for adjusting OpenACC privatization level in
10749 : : CTX. */
10750 : :
10751 : : static void
10752 : 22946 : oacc_privatization_scan_decl_chain (omp_context *ctx, tree decls)
10753 : : {
10754 : 50711 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
10755 : : {
10756 : 27765 : tree new_decl = lookup_decl (decl, ctx);
10757 : 27765 : gcc_checking_assert (new_decl == decl);
10758 : :
10759 : 27765 : if (!oacc_privatization_candidate_p (gimple_location (ctx->stmt), NULL,
10760 : : new_decl))
10761 : 27555 : continue;
10762 : :
10763 : 210 : gcc_checking_assert
10764 : : (!ctx->oacc_privatization_candidates.contains (new_decl));
10765 : 210 : ctx->oacc_privatization_candidates.safe_push (new_decl);
10766 : : }
10767 : 22946 : }
10768 : :
10769 : : /* Callback for walk_gimple_seq. Find #pragma omp scan statement. */
10770 : :
10771 : : static tree
10772 : 2233 : omp_find_scan (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
10773 : : struct walk_stmt_info *wi)
10774 : : {
10775 : 2233 : gimple *stmt = gsi_stmt (*gsi_p);
10776 : :
10777 : 2233 : *handled_ops_p = true;
10778 : 2233 : switch (gimple_code (stmt))
10779 : : {
10780 : 418 : WALK_SUBSTMTS;
10781 : :
10782 : 166 : case GIMPLE_OMP_FOR:
10783 : 166 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD
10784 : 166 : && gimple_omp_for_combined_into_p (stmt))
10785 : 166 : *handled_ops_p = false;
10786 : : break;
10787 : :
10788 : 678 : case GIMPLE_OMP_SCAN:
10789 : 678 : *(gimple_stmt_iterator *) (wi->info) = *gsi_p;
10790 : 678 : 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 : 173 : 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 : 173 : bool is_for_simd = gimple_omp_for_combined_p (stmt);
10929 : 173 : gcc_assert (ctx->scan_inclusive || ctx->scan_exclusive);
10930 : :
10931 : 173 : gimple_seq body = gimple_omp_body (stmt);
10932 : 173 : gimple_stmt_iterator input1_gsi = gsi_none ();
10933 : 173 : struct walk_stmt_info wi;
10934 : 173 : memset (&wi, 0, sizeof (wi));
10935 : 173 : wi.val_only = true;
10936 : 173 : wi.info = (void *) &input1_gsi;
10937 : 173 : walk_gimple_seq_mod (&body, omp_find_scan, NULL, &wi);
10938 : 173 : gcc_assert (!gsi_end_p (input1_gsi));
10939 : :
10940 : 173 : gimple *input_stmt1 = gsi_stmt (input1_gsi);
10941 : 173 : gimple_stmt_iterator gsi = input1_gsi;
10942 : 173 : gsi_next (&gsi);
10943 : 173 : gimple_stmt_iterator scan1_gsi = gsi;
10944 : 173 : gimple *scan_stmt1 = gsi_stmt (gsi);
10945 : 173 : gcc_assert (scan_stmt1 && gimple_code (scan_stmt1) == GIMPLE_OMP_SCAN);
10946 : :
10947 : 173 : gimple_seq input_body = gimple_omp_body (input_stmt1);
10948 : 173 : gimple_seq scan_body = gimple_omp_body (scan_stmt1);
10949 : 173 : gimple_omp_set_body (input_stmt1, NULL);
10950 : 173 : gimple_omp_set_body (scan_stmt1, NULL);
10951 : 173 : gimple_omp_set_body (stmt, NULL);
10952 : :
10953 : 173 : gomp_for *new_stmt = as_a <gomp_for *> (gimple_copy (stmt));
10954 : 173 : gimple_seq new_body = copy_gimple_seq_and_replace_locals (body);
10955 : 173 : gimple_omp_set_body (stmt, body);
10956 : 173 : gimple_omp_set_body (input_stmt1, input_body);
10957 : :
10958 : 173 : gimple_stmt_iterator input2_gsi = gsi_none ();
10959 : 173 : memset (&wi, 0, sizeof (wi));
10960 : 173 : wi.val_only = true;
10961 : 173 : wi.info = (void *) &input2_gsi;
10962 : 173 : walk_gimple_seq_mod (&new_body, omp_find_scan, NULL, &wi);
10963 : 173 : gcc_assert (!gsi_end_p (input2_gsi));
10964 : :
10965 : 173 : gimple *input_stmt2 = gsi_stmt (input2_gsi);
10966 : 173 : gsi = input2_gsi;
10967 : 173 : gsi_next (&gsi);
10968 : 173 : gimple_stmt_iterator scan2_gsi = gsi;
10969 : 173 : gimple *scan_stmt2 = gsi_stmt (gsi);
10970 : 173 : gcc_assert (scan_stmt2 && gimple_code (scan_stmt2) == GIMPLE_OMP_SCAN);
10971 : 173 : gimple_omp_set_body (scan_stmt2, scan_body);
10972 : :
10973 : 173 : gimple_stmt_iterator input3_gsi = gsi_none ();
10974 : 173 : gimple_stmt_iterator scan3_gsi = gsi_none ();
10975 : 173 : gimple_stmt_iterator input4_gsi = gsi_none ();
10976 : 173 : gimple_stmt_iterator scan4_gsi = gsi_none ();
10977 : 173 : gimple *input_stmt3 = NULL, *scan_stmt3 = NULL;
10978 : 173 : gimple *input_stmt4 = NULL, *scan_stmt4 = NULL;
10979 : 173 : omp_context *input_simd_ctx = NULL, *scan_simd_ctx = NULL;
10980 : 173 : if (is_for_simd)
10981 : : {
10982 : 83 : memset (&wi, 0, sizeof (wi));
10983 : 83 : wi.val_only = true;
10984 : 83 : wi.info = (void *) &input3_gsi;
10985 : 83 : walk_gimple_seq_mod (&input_body, omp_find_scan, NULL, &wi);
10986 : 83 : gcc_assert (!gsi_end_p (input3_gsi));
10987 : :
10988 : 83 : input_stmt3 = gsi_stmt (input3_gsi);
10989 : 83 : gsi = input3_gsi;
10990 : 83 : gsi_next (&gsi);
10991 : 83 : scan3_gsi = gsi;
10992 : 83 : scan_stmt3 = gsi_stmt (gsi);
10993 : 83 : gcc_assert (scan_stmt3 && gimple_code (scan_stmt3) == GIMPLE_OMP_SCAN);
10994 : :
10995 : 83 : memset (&wi, 0, sizeof (wi));
10996 : 83 : wi.val_only = true;
10997 : 83 : wi.info = (void *) &input4_gsi;
10998 : 83 : walk_gimple_seq_mod (&scan_body, omp_find_scan, NULL, &wi);
10999 : 83 : gcc_assert (!gsi_end_p (input4_gsi));
11000 : :
11001 : 83 : input_stmt4 = gsi_stmt (input4_gsi);
11002 : 83 : gsi = input4_gsi;
11003 : 83 : gsi_next (&gsi);
11004 : 83 : scan4_gsi = gsi;
11005 : 83 : scan_stmt4 = gsi_stmt (gsi);
11006 : 83 : gcc_assert (scan_stmt4 && gimple_code (scan_stmt4) == GIMPLE_OMP_SCAN);
11007 : :
11008 : 83 : input_simd_ctx = maybe_lookup_ctx (input_stmt3)->outer;
11009 : 83 : scan_simd_ctx = maybe_lookup_ctx (input_stmt4)->outer;
11010 : : }
11011 : :
11012 : 173 : tree num_threads = create_tmp_var (integer_type_node);
11013 : 173 : tree thread_num = create_tmp_var (integer_type_node);
11014 : 173 : tree nthreads_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
11015 : 173 : tree threadnum_decl = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
11016 : 173 : gimple *g = gimple_build_call (nthreads_decl, 0);
11017 : 173 : gimple_call_set_lhs (g, num_threads);
11018 : 173 : gimple_seq_add_stmt (body_p, g);
11019 : 173 : g = gimple_build_call (threadnum_decl, 0);
11020 : 173 : gimple_call_set_lhs (g, thread_num);
11021 : 173 : gimple_seq_add_stmt (body_p, g);
11022 : :
11023 : 173 : tree ivar = create_tmp_var (sizetype);
11024 : 173 : tree new_clauses1 = NULL_TREE, new_clauses2 = NULL_TREE;
11025 : 173 : tree *cp1 = &new_clauses1, *cp2 = &new_clauses2;
11026 : 173 : tree k = create_tmp_var (unsigned_type_node);
11027 : 173 : tree l = create_tmp_var (unsigned_type_node);
11028 : :
11029 : 173 : gimple_seq clist = NULL, mdlist = NULL;
11030 : 173 : gimple_seq thr01_list = NULL, thrn1_list = NULL;
11031 : 173 : gimple_seq thr02_list = NULL, thrn2_list = NULL;
11032 : 173 : gimple_seq scan1_list = NULL, input2_list = NULL;
11033 : 173 : gimple_seq last_list = NULL, reduc_list = NULL;
11034 : 674 : for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
11035 : 501 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11036 : 501 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
11037 : : {
11038 : 205 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
11039 : 205 : tree var = OMP_CLAUSE_DECL (c);
11040 : 205 : tree new_var = lookup_decl (var, ctx);
11041 : 205 : tree var3 = NULL_TREE;
11042 : 205 : tree new_vard = new_var;
11043 : 205 : if (omp_privatize_by_reference (var))
11044 : 48 : new_var = build_simple_mem_ref_loc (clause_loc, new_var);
11045 : 205 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11046 : : {
11047 : 64 : var3 = maybe_lookup_decl (new_vard, ctx);
11048 : 64 : if (var3 == new_vard)
11049 : 173 : var3 = NULL_TREE;
11050 : : }
11051 : :
11052 : 205 : tree ptype = build_pointer_type (TREE_TYPE (new_var));
11053 : 205 : tree rpriva = create_tmp_var (ptype);
11054 : 205 : tree nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
11055 : 205 : OMP_CLAUSE_DECL (nc) = rpriva;
11056 : 205 : *cp1 = nc;
11057 : 205 : cp1 = &OMP_CLAUSE_CHAIN (nc);
11058 : :
11059 : 205 : tree rprivb = create_tmp_var (ptype);
11060 : 205 : nc = build_omp_clause (clause_loc, OMP_CLAUSE__SCANTEMP_);
11061 : 205 : OMP_CLAUSE_DECL (nc) = rprivb;
11062 : 205 : OMP_CLAUSE__SCANTEMP__ALLOC (nc) = 1;
11063 : 205 : *cp1 = nc;
11064 : 205 : cp1 = &OMP_CLAUSE_CHAIN (nc);
11065 : :
11066 : 205 : tree var2 = create_tmp_var_raw (TREE_TYPE (new_var));
11067 : 205 : if (new_vard != new_var)
11068 : 48 : TREE_ADDRESSABLE (var2) = 1;
11069 : 205 : gimple_add_tmp_var (var2);
11070 : :
11071 : 205 : tree x = fold_convert_loc (clause_loc, sizetype, thread_num);
11072 : 205 : x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11073 : 205 : TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11074 : 205 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11075 : 205 : tree rpriva_ref = build_simple_mem_ref_loc (clause_loc, x);
11076 : :
11077 : 205 : x = fold_build2_loc (clause_loc, PLUS_EXPR, integer_type_node,
11078 : : thread_num, integer_minus_one_node);
11079 : 205 : x = fold_convert_loc (clause_loc, sizetype, x);
11080 : 205 : x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11081 : 205 : TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11082 : 205 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11083 : 205 : tree rprivam1_ref = build_simple_mem_ref_loc (clause_loc, x);
11084 : :
11085 : 205 : x = fold_convert_loc (clause_loc, sizetype, l);
11086 : 205 : x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11087 : 205 : TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11088 : 205 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11089 : 205 : tree rprival_ref = build_simple_mem_ref_loc (clause_loc, x);
11090 : :
11091 : 205 : x = fold_build2_loc (clause_loc, MINUS_EXPR, unsigned_type_node, l, k);
11092 : 205 : x = fold_convert_loc (clause_loc, sizetype, x);
11093 : 205 : x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, x,
11094 : 205 : TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11095 : 205 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rpriva), rpriva, x);
11096 : 205 : tree rprivalmk_ref = build_simple_mem_ref_loc (clause_loc, x);
11097 : :
11098 : 205 : x = fold_build2_loc (clause_loc, MULT_EXPR, sizetype, ivar,
11099 : 205 : TYPE_SIZE_UNIT (TREE_TYPE (ptype)));
11100 : 205 : x = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (rprivb), rprivb, x);
11101 : 205 : tree rprivb_ref = build_simple_mem_ref_loc (clause_loc, x);
11102 : :
11103 : 205 : tree var4 = is_for_simd ? new_var : var2;
11104 : 99 : tree var5 = NULL_TREE, var6 = NULL_TREE;
11105 : 99 : if (is_for_simd)
11106 : : {
11107 : 99 : var5 = lookup_decl (var, input_simd_ctx);
11108 : 99 : var6 = lookup_decl (var, scan_simd_ctx);
11109 : 99 : if (new_vard != new_var)
11110 : : {
11111 : 24 : var5 = build_simple_mem_ref_loc (clause_loc, var5);
11112 : 24 : var6 = build_simple_mem_ref_loc (clause_loc, var6);
11113 : : }
11114 : : }
11115 : 205 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11116 : : {
11117 : 64 : tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11118 : 64 : tree val = var2;
11119 : :
11120 : 64 : x = lang_hooks.decls.omp_clause_default_ctor
11121 : 64 : (c, var2, build_outer_var_ref (var, ctx));
11122 : 64 : if (x)
11123 : 32 : gimplify_and_add (x, &clist);
11124 : :
11125 : 64 : x = build_outer_var_ref (var, ctx);
11126 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, unshare_expr (var4),
11127 : : x);
11128 : 64 : gimplify_and_add (x, &thr01_list);
11129 : :
11130 : 64 : tree y = (DECL_HAS_VALUE_EXPR_P (new_vard)
11131 : 64 : ? DECL_VALUE_EXPR (new_vard) : NULL_TREE);
11132 : 64 : if (var3)
11133 : : {
11134 : 32 : x = unshare_expr (var4);
11135 : 32 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
11136 : 32 : gimplify_and_add (x, &thrn1_list);
11137 : 32 : x = unshare_expr (var4);
11138 : 32 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var3);
11139 : 32 : gimplify_and_add (x, &thr02_list);
11140 : : }
11141 : 32 : else if (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c))
11142 : : {
11143 : : /* Otherwise, assign to it the identity element. */
11144 : 32 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
11145 : 32 : tseq = copy_gimple_seq_and_replace_locals (tseq);
11146 : 32 : if (!is_for_simd)
11147 : : {
11148 : 16 : if (new_vard != new_var)
11149 : 8 : val = build_fold_addr_expr_loc (clause_loc, val);
11150 : 16 : SET_DECL_VALUE_EXPR (new_vard, val);
11151 : 16 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11152 : : }
11153 : 32 : SET_DECL_VALUE_EXPR (placeholder, error_mark_node);
11154 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11155 : 32 : lower_omp (&tseq, ctx);
11156 : 32 : gimple_seq_add_seq (&thrn1_list, tseq);
11157 : 32 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c);
11158 : 32 : lower_omp (&tseq, ctx);
11159 : 32 : gimple_seq_add_seq (&thr02_list, tseq);
11160 : 32 : SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
11161 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11162 : 32 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
11163 : 32 : if (y)
11164 : 0 : SET_DECL_VALUE_EXPR (new_vard, y);
11165 : : else
11166 : : {
11167 : 32 : DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11168 : 32 : SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11169 : : }
11170 : : }
11171 : :
11172 : 64 : x = unshare_expr (var4);
11173 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, x, rprivam1_ref);
11174 : 64 : gimplify_and_add (x, &thrn2_list);
11175 : :
11176 : 64 : if (is_for_simd)
11177 : : {
11178 : 32 : x = unshare_expr (rprivb_ref);
11179 : 32 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var5);
11180 : 32 : gimplify_and_add (x, &scan1_list);
11181 : : }
11182 : : else
11183 : : {
11184 : 32 : if (ctx->scan_exclusive)
11185 : : {
11186 : 16 : x = unshare_expr (rprivb_ref);
11187 : 16 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
11188 : 16 : gimplify_and_add (x, &scan1_list);
11189 : : }
11190 : :
11191 : 32 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11192 : 32 : tseq = copy_gimple_seq_and_replace_locals (tseq);
11193 : 32 : SET_DECL_VALUE_EXPR (placeholder, var2);
11194 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11195 : 32 : lower_omp (&tseq, ctx);
11196 : 32 : gimple_seq_add_seq (&scan1_list, tseq);
11197 : :
11198 : 32 : if (ctx->scan_inclusive)
11199 : : {
11200 : 16 : x = unshare_expr (rprivb_ref);
11201 : 16 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var2);
11202 : 16 : gimplify_and_add (x, &scan1_list);
11203 : : }
11204 : : }
11205 : :
11206 : 64 : x = unshare_expr (rpriva_ref);
11207 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, x,
11208 : : unshare_expr (var4));
11209 : 64 : gimplify_and_add (x, &mdlist);
11210 : :
11211 : 96 : x = unshare_expr (is_for_simd ? var6 : new_var);
11212 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, x, var4);
11213 : 64 : gimplify_and_add (x, &input2_list);
11214 : :
11215 : 64 : val = rprivb_ref;
11216 : 64 : if (new_vard != new_var)
11217 : 32 : val = build_fold_addr_expr_loc (clause_loc, val);
11218 : :
11219 : 64 : gimple_seq tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11220 : 64 : tseq = copy_gimple_seq_and_replace_locals (tseq);
11221 : 64 : SET_DECL_VALUE_EXPR (new_vard, val);
11222 : 64 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11223 : 64 : if (is_for_simd)
11224 : : {
11225 : 32 : SET_DECL_VALUE_EXPR (placeholder, var6);
11226 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11227 : : }
11228 : : else
11229 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11230 : 64 : lower_omp (&tseq, ctx);
11231 : 64 : if (y)
11232 : 0 : SET_DECL_VALUE_EXPR (new_vard, y);
11233 : : else
11234 : : {
11235 : 64 : DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11236 : 64 : SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11237 : : }
11238 : 64 : if (!is_for_simd)
11239 : : {
11240 : 32 : SET_DECL_VALUE_EXPR (placeholder, new_var);
11241 : 32 : DECL_HAS_VALUE_EXPR_P (placeholder) = 1;
11242 : 32 : lower_omp (&tseq, ctx);
11243 : : }
11244 : 64 : gimple_seq_add_seq (&input2_list, tseq);
11245 : :
11246 : 64 : x = build_outer_var_ref (var, ctx);
11247 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, x, rpriva_ref);
11248 : 64 : gimplify_and_add (x, &last_list);
11249 : :
11250 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, var2, rprivalmk_ref);
11251 : 64 : gimplify_and_add (x, &reduc_list);
11252 : 64 : tseq = OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c);
11253 : 64 : tseq = copy_gimple_seq_and_replace_locals (tseq);
11254 : 64 : val = rprival_ref;
11255 : 64 : if (new_vard != new_var)
11256 : 32 : val = build_fold_addr_expr_loc (clause_loc, val);
11257 : 64 : SET_DECL_VALUE_EXPR (new_vard, val);
11258 : 64 : DECL_HAS_VALUE_EXPR_P (new_vard) = 1;
11259 : 64 : SET_DECL_VALUE_EXPR (placeholder, var2);
11260 : 64 : lower_omp (&tseq, ctx);
11261 : 64 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
11262 : 64 : SET_DECL_VALUE_EXPR (placeholder, NULL_TREE);
11263 : 64 : DECL_HAS_VALUE_EXPR_P (placeholder) = 0;
11264 : 64 : if (y)
11265 : 0 : SET_DECL_VALUE_EXPR (new_vard, y);
11266 : : else
11267 : : {
11268 : 64 : DECL_HAS_VALUE_EXPR_P (new_vard) = 0;
11269 : 64 : SET_DECL_VALUE_EXPR (new_vard, NULL_TREE);
11270 : : }
11271 : 64 : gimple_seq_add_seq (&reduc_list, tseq);
11272 : 64 : x = lang_hooks.decls.omp_clause_assign_op (c, rprival_ref, var2);
11273 : 64 : gimplify_and_add (x, &reduc_list);
11274 : :
11275 : 64 : x = lang_hooks.decls.omp_clause_dtor (c, var2);
11276 : 64 : if (x)
11277 : 32 : gimplify_and_add (x, dlist);
11278 : : }
11279 : : else
11280 : : {
11281 : 141 : x = build_outer_var_ref (var, ctx);
11282 : 141 : gimplify_assign (unshare_expr (var4), x, &thr01_list);
11283 : :
11284 : 141 : x = omp_reduction_init (c, TREE_TYPE (new_var));
11285 : 141 : gimplify_assign (unshare_expr (var4), unshare_expr (x),
11286 : : &thrn1_list);
11287 : 141 : gimplify_assign (unshare_expr (var4), x, &thr02_list);
11288 : :
11289 : 141 : gimplify_assign (unshare_expr (var4), rprivam1_ref, &thrn2_list);
11290 : :
11291 : 141 : enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c);
11292 : 141 : if (code == MINUS_EXPR)
11293 : 0 : code = PLUS_EXPR;
11294 : :
11295 : 141 : if (is_for_simd)
11296 : 67 : gimplify_assign (unshare_expr (rprivb_ref), var5, &scan1_list);
11297 : : else
11298 : : {
11299 : 74 : if (ctx->scan_exclusive)
11300 : 28 : gimplify_assign (unshare_expr (rprivb_ref), var2,
11301 : : &scan1_list);
11302 : 74 : x = build2 (code, TREE_TYPE (new_var), var2, new_var);
11303 : 74 : gimplify_assign (var2, x, &scan1_list);
11304 : 74 : if (ctx->scan_inclusive)
11305 : 46 : gimplify_assign (unshare_expr (rprivb_ref), var2,
11306 : : &scan1_list);
11307 : : }
11308 : :
11309 : 141 : gimplify_assign (unshare_expr (rpriva_ref), unshare_expr (var4),
11310 : : &mdlist);
11311 : :
11312 : 141 : x = build2 (code, TREE_TYPE (new_var), var4, rprivb_ref);
11313 : 215 : gimplify_assign (is_for_simd ? var6 : new_var, x, &input2_list);
11314 : :
11315 : 141 : gimplify_assign (build_outer_var_ref (var, ctx), rpriva_ref,
11316 : : &last_list);
11317 : :
11318 : 141 : x = build2 (code, TREE_TYPE (new_var), rprivalmk_ref,
11319 : : unshare_expr (rprival_ref));
11320 : 141 : gimplify_assign (rprival_ref, x, &reduc_list);
11321 : : }
11322 : : }
11323 : :
11324 : 173 : g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
11325 : 173 : gimple_seq_add_stmt (&scan1_list, g);
11326 : 173 : g = gimple_build_assign (ivar, PLUS_EXPR, ivar, size_one_node);
11327 : 263 : gimple_seq_add_stmt (gimple_omp_body_ptr (is_for_simd
11328 : : ? scan_stmt4 : scan_stmt2), g);
11329 : :
11330 : 173 : tree controlb = create_tmp_var (boolean_type_node);
11331 : 173 : tree controlp = create_tmp_var (ptr_type_node);
11332 : 173 : tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11333 : 173 : OMP_CLAUSE_DECL (nc) = controlb;
11334 : 173 : OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11335 : 173 : *cp1 = nc;
11336 : 173 : cp1 = &OMP_CLAUSE_CHAIN (nc);
11337 : 173 : nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11338 : 173 : OMP_CLAUSE_DECL (nc) = controlp;
11339 : 173 : OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11340 : 173 : *cp1 = nc;
11341 : 173 : cp1 = &OMP_CLAUSE_CHAIN (nc);
11342 : 173 : nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11343 : 173 : OMP_CLAUSE_DECL (nc) = controlb;
11344 : 173 : OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11345 : 173 : *cp2 = nc;
11346 : 173 : cp2 = &OMP_CLAUSE_CHAIN (nc);
11347 : 173 : nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SCANTEMP_);
11348 : 173 : OMP_CLAUSE_DECL (nc) = controlp;
11349 : 173 : OMP_CLAUSE__SCANTEMP__CONTROL (nc) = 1;
11350 : 173 : *cp2 = nc;
11351 : 173 : cp2 = &OMP_CLAUSE_CHAIN (nc);
11352 : :
11353 : 173 : *cp1 = gimple_omp_for_clauses (stmt);
11354 : 173 : gimple_omp_for_set_clauses (stmt, new_clauses1);
11355 : 173 : *cp2 = gimple_omp_for_clauses (new_stmt);
11356 : 173 : gimple_omp_for_set_clauses (new_stmt, new_clauses2);
11357 : :
11358 : 173 : if (is_for_simd)
11359 : : {
11360 : 83 : gimple_seq_add_seq (gimple_omp_body_ptr (scan_stmt3), scan1_list);
11361 : 83 : gimple_seq_add_seq (gimple_omp_body_ptr (input_stmt4), input2_list);
11362 : :
11363 : 83 : gsi_insert_seq_after (&input3_gsi, gimple_omp_body (input_stmt3),
11364 : : GSI_SAME_STMT);
11365 : 83 : gsi_remove (&input3_gsi, true);
11366 : 83 : gsi_insert_seq_after (&scan3_gsi, gimple_omp_body (scan_stmt3),
11367 : : GSI_SAME_STMT);
11368 : 83 : gsi_remove (&scan3_gsi, true);
11369 : 83 : gsi_insert_seq_after (&input4_gsi, gimple_omp_body (input_stmt4),
11370 : : GSI_SAME_STMT);
11371 : 83 : gsi_remove (&input4_gsi, true);
11372 : 83 : gsi_insert_seq_after (&scan4_gsi, gimple_omp_body (scan_stmt4),
11373 : : GSI_SAME_STMT);
11374 : 83 : gsi_remove (&scan4_gsi, true);
11375 : : }
11376 : : else
11377 : : {
11378 : 90 : gimple_omp_set_body (scan_stmt1, scan1_list);
11379 : 90 : gimple_omp_set_body (input_stmt2, input2_list);
11380 : : }
11381 : :
11382 : 173 : gsi_insert_seq_after (&input1_gsi, gimple_omp_body (input_stmt1),
11383 : : GSI_SAME_STMT);
11384 : 173 : gsi_remove (&input1_gsi, true);
11385 : 173 : gsi_insert_seq_after (&scan1_gsi, gimple_omp_body (scan_stmt1),
11386 : : GSI_SAME_STMT);
11387 : 173 : gsi_remove (&scan1_gsi, true);
11388 : 173 : gsi_insert_seq_after (&input2_gsi, gimple_omp_body (input_stmt2),
11389 : : GSI_SAME_STMT);
11390 : 173 : gsi_remove (&input2_gsi, true);
11391 : 173 : gsi_insert_seq_after (&scan2_gsi, gimple_omp_body (scan_stmt2),
11392 : : GSI_SAME_STMT);
11393 : 173 : gsi_remove (&scan2_gsi, true);
11394 : :
11395 : 173 : gimple_seq_add_seq (body_p, clist);
11396 : :
11397 : 173 : tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
11398 : 173 : tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
11399 : 173 : tree lab3 = create_artificial_label (UNKNOWN_LOCATION);
11400 : 173 : g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
11401 : 173 : gimple_seq_add_stmt (body_p, g);
11402 : 173 : g = gimple_build_label (lab1);
11403 : 173 : gimple_seq_add_stmt (body_p, g);
11404 : 173 : gimple_seq_add_seq (body_p, thr01_list);
11405 : 173 : g = gimple_build_goto (lab3);
11406 : 173 : gimple_seq_add_stmt (body_p, g);
11407 : 173 : g = gimple_build_label (lab2);
11408 : 173 : gimple_seq_add_stmt (body_p, g);
11409 : 173 : gimple_seq_add_seq (body_p, thrn1_list);
11410 : 173 : g = gimple_build_label (lab3);
11411 : 173 : gimple_seq_add_stmt (body_p, g);
11412 : :
11413 : 173 : g = gimple_build_assign (ivar, size_zero_node);
11414 : 173 : gimple_seq_add_stmt (body_p, g);
11415 : :
11416 : 173 : gimple_seq_add_stmt (body_p, stmt);
11417 : 173 : gimple_seq_add_seq (body_p, body);
11418 : 173 : gimple_seq_add_stmt (body_p, gimple_build_omp_continue (fd->loop.v,
11419 : : fd->loop.v));
11420 : :
11421 : 173 : g = gimple_build_omp_return (true);
11422 : 173 : gimple_seq_add_stmt (body_p, g);
11423 : 173 : gimple_seq_add_seq (body_p, mdlist);
11424 : :
11425 : 173 : lab1 = create_artificial_label (UNKNOWN_LOCATION);
11426 : 173 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
11427 : 173 : g = gimple_build_cond (GT_EXPR, num_threads, integer_one_node, lab1, lab2);
11428 : 173 : gimple_seq_add_stmt (body_p, g);
11429 : 173 : g = gimple_build_label (lab1);
11430 : 173 : gimple_seq_add_stmt (body_p, g);
11431 : :
11432 : 173 : g = omp_build_barrier (NULL);
11433 : 173 : gimple_seq_add_stmt (body_p, g);
11434 : :
11435 : 173 : tree down = create_tmp_var (unsigned_type_node);
11436 : 173 : g = gimple_build_assign (down, build_zero_cst (unsigned_type_node));
11437 : 173 : gimple_seq_add_stmt (body_p, g);
11438 : :
11439 : 173 : g = gimple_build_assign (k, build_one_cst (unsigned_type_node));
11440 : 173 : gimple_seq_add_stmt (body_p, g);
11441 : :
11442 : 173 : tree num_threadsu = create_tmp_var (unsigned_type_node);
11443 : 173 : g = gimple_build_assign (num_threadsu, NOP_EXPR, num_threads);
11444 : 173 : gimple_seq_add_stmt (body_p, g);
11445 : :
11446 : 173 : tree thread_numu = create_tmp_var (unsigned_type_node);
11447 : 173 : g = gimple_build_assign (thread_numu, NOP_EXPR, thread_num);
11448 : 173 : gimple_seq_add_stmt (body_p, g);
11449 : :
11450 : 173 : tree thread_nump1 = create_tmp_var (unsigned_type_node);
11451 : 173 : g = gimple_build_assign (thread_nump1, PLUS_EXPR, thread_numu,
11452 : 173 : build_int_cst (unsigned_type_node, 1));
11453 : 173 : gimple_seq_add_stmt (body_p, g);
11454 : :
11455 : 173 : lab3 = create_artificial_label (UNKNOWN_LOCATION);
11456 : 173 : g = gimple_build_label (lab3);
11457 : 173 : gimple_seq_add_stmt (body_p, g);
11458 : :
11459 : 173 : tree twok = create_tmp_var (unsigned_type_node);
11460 : 173 : g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
11461 : 173 : gimple_seq_add_stmt (body_p, g);
11462 : :
11463 : 173 : tree lab4 = create_artificial_label (UNKNOWN_LOCATION);
11464 : 173 : tree lab5 = create_artificial_label (UNKNOWN_LOCATION);
11465 : 173 : tree lab6 = create_artificial_label (UNKNOWN_LOCATION);
11466 : 173 : g = gimple_build_cond (GT_EXPR, twok, num_threadsu, lab4, lab5);
11467 : 173 : gimple_seq_add_stmt (body_p, g);
11468 : 173 : g = gimple_build_label (lab4);
11469 : 173 : gimple_seq_add_stmt (body_p, g);
11470 : 173 : g = gimple_build_assign (down, build_all_ones_cst (unsigned_type_node));
11471 : 173 : gimple_seq_add_stmt (body_p, g);
11472 : 173 : g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11473 : 173 : gimple_seq_add_stmt (body_p, g);
11474 : :
11475 : 173 : g = gimple_build_cond (EQ_EXPR, k, num_threadsu, lab6, lab5);
11476 : 173 : gimple_seq_add_stmt (body_p, g);
11477 : 173 : g = gimple_build_label (lab6);
11478 : 173 : gimple_seq_add_stmt (body_p, g);
11479 : :
11480 : 173 : g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11481 : 173 : gimple_seq_add_stmt (body_p, g);
11482 : :
11483 : 173 : g = gimple_build_label (lab5);
11484 : 173 : gimple_seq_add_stmt (body_p, g);
11485 : :
11486 : 173 : g = gimple_build_assign (twok, LSHIFT_EXPR, k, integer_one_node);
11487 : 173 : gimple_seq_add_stmt (body_p, g);
11488 : :
11489 : 173 : tree cplx = create_tmp_var (build_complex_type (unsigned_type_node, false));
11490 : 173 : g = gimple_build_call_internal (IFN_MUL_OVERFLOW, 2, thread_nump1, twok);
11491 : 173 : gimple_call_set_lhs (g, cplx);
11492 : 173 : gimple_seq_add_stmt (body_p, g);
11493 : 173 : tree mul = create_tmp_var (unsigned_type_node);
11494 : 173 : g = gimple_build_assign (mul, REALPART_EXPR,
11495 : : build1 (REALPART_EXPR, unsigned_type_node, cplx));
11496 : 173 : gimple_seq_add_stmt (body_p, g);
11497 : 173 : tree ovf = create_tmp_var (unsigned_type_node);
11498 : 173 : g = gimple_build_assign (ovf, IMAGPART_EXPR,
11499 : : build1 (IMAGPART_EXPR, unsigned_type_node, cplx));
11500 : 173 : gimple_seq_add_stmt (body_p, g);
11501 : :
11502 : 173 : tree lab7 = create_artificial_label (UNKNOWN_LOCATION);
11503 : 173 : tree lab8 = create_artificial_label (UNKNOWN_LOCATION);
11504 : 173 : g = gimple_build_cond (EQ_EXPR, ovf, build_zero_cst (unsigned_type_node),
11505 : : lab7, lab8);
11506 : 173 : gimple_seq_add_stmt (body_p, g);
11507 : 173 : g = gimple_build_label (lab7);
11508 : 173 : gimple_seq_add_stmt (body_p, g);
11509 : :
11510 : 173 : tree andv = create_tmp_var (unsigned_type_node);
11511 : 173 : g = gimple_build_assign (andv, BIT_AND_EXPR, k, down);
11512 : 173 : gimple_seq_add_stmt (body_p, g);
11513 : 173 : tree andvm1 = create_tmp_var (unsigned_type_node);
11514 : 173 : g = gimple_build_assign (andvm1, PLUS_EXPR, andv,
11515 : : build_minus_one_cst (unsigned_type_node));
11516 : 173 : gimple_seq_add_stmt (body_p, g);
11517 : :
11518 : 173 : g = gimple_build_assign (l, PLUS_EXPR, mul, andvm1);
11519 : 173 : gimple_seq_add_stmt (body_p, g);
11520 : :
11521 : 173 : tree lab9 = create_artificial_label (UNKNOWN_LOCATION);
11522 : 173 : g = gimple_build_cond (LT_EXPR, l, num_threadsu, lab9, lab8);
11523 : 173 : gimple_seq_add_stmt (body_p, g);
11524 : 173 : g = gimple_build_label (lab9);
11525 : 173 : gimple_seq_add_stmt (body_p, g);
11526 : 173 : gimple_seq_add_seq (body_p, reduc_list);
11527 : 173 : g = gimple_build_label (lab8);
11528 : 173 : gimple_seq_add_stmt (body_p, g);
11529 : :
11530 : 173 : tree lab10 = create_artificial_label (UNKNOWN_LOCATION);
11531 : 173 : tree lab11 = create_artificial_label (UNKNOWN_LOCATION);
11532 : 173 : tree lab12 = create_artificial_label (UNKNOWN_LOCATION);
11533 : 173 : g = gimple_build_cond (EQ_EXPR, down, build_zero_cst (unsigned_type_node),
11534 : : lab10, lab11);
11535 : 173 : gimple_seq_add_stmt (body_p, g);
11536 : 173 : g = gimple_build_label (lab10);
11537 : 173 : gimple_seq_add_stmt (body_p, g);
11538 : 173 : g = gimple_build_assign (k, LSHIFT_EXPR, k, integer_one_node);
11539 : 173 : gimple_seq_add_stmt (body_p, g);
11540 : 173 : g = gimple_build_goto (lab12);
11541 : 173 : gimple_seq_add_stmt (body_p, g);
11542 : 173 : g = gimple_build_label (lab11);
11543 : 173 : gimple_seq_add_stmt (body_p, g);
11544 : 173 : g = gimple_build_assign (k, RSHIFT_EXPR, k, integer_one_node);
11545 : 173 : gimple_seq_add_stmt (body_p, g);
11546 : 173 : g = gimple_build_label (lab12);
11547 : 173 : gimple_seq_add_stmt (body_p, g);
11548 : :
11549 : 173 : g = omp_build_barrier (NULL);
11550 : 173 : gimple_seq_add_stmt (body_p, g);
11551 : :
11552 : 173 : g = gimple_build_cond (NE_EXPR, k, build_zero_cst (unsigned_type_node),
11553 : : lab3, lab2);
11554 : 173 : gimple_seq_add_stmt (body_p, g);
11555 : :
11556 : 173 : g = gimple_build_label (lab2);
11557 : 173 : gimple_seq_add_stmt (body_p, g);
11558 : :
11559 : 173 : lab1 = create_artificial_label (UNKNOWN_LOCATION);
11560 : 173 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
11561 : 173 : lab3 = create_artificial_label (UNKNOWN_LOCATION);
11562 : 173 : g = gimple_build_cond (EQ_EXPR, thread_num, integer_zero_node, lab1, lab2);
11563 : 173 : gimple_seq_add_stmt (body_p, g);
11564 : 173 : g = gimple_build_label (lab1);
11565 : 173 : gimple_seq_add_stmt (body_p, g);
11566 : 173 : gimple_seq_add_seq (body_p, thr02_list);
11567 : 173 : g = gimple_build_goto (lab3);
11568 : 173 : gimple_seq_add_stmt (body_p, g);
11569 : 173 : g = gimple_build_label (lab2);
11570 : 173 : gimple_seq_add_stmt (body_p, g);
11571 : 173 : gimple_seq_add_seq (body_p, thrn2_list);
11572 : 173 : g = gimple_build_label (lab3);
11573 : 173 : gimple_seq_add_stmt (body_p, g);
11574 : :
11575 : 173 : g = gimple_build_assign (ivar, size_zero_node);
11576 : 173 : gimple_seq_add_stmt (body_p, g);
11577 : 173 : gimple_seq_add_stmt (body_p, new_stmt);
11578 : 173 : gimple_seq_add_seq (body_p, new_body);
11579 : :
11580 : 173 : gimple_seq new_dlist = NULL;
11581 : 173 : lab1 = create_artificial_label (UNKNOWN_LOCATION);
11582 : 173 : lab2 = create_artificial_label (UNKNOWN_LOCATION);
11583 : 173 : tree num_threadsm1 = create_tmp_var (integer_type_node);
11584 : 173 : g = gimple_build_assign (num_threadsm1, PLUS_EXPR, num_threads,
11585 : : integer_minus_one_node);
11586 : 173 : gimple_seq_add_stmt (&new_dlist, g);
11587 : 173 : g = gimple_build_cond (EQ_EXPR, thread_num, num_threadsm1, lab1, lab2);
11588 : 173 : gimple_seq_add_stmt (&new_dlist, g);
11589 : 173 : g = gimple_build_label (lab1);
11590 : 173 : gimple_seq_add_stmt (&new_dlist, g);
11591 : 173 : gimple_seq_add_seq (&new_dlist, last_list);
11592 : 173 : g = gimple_build_label (lab2);
11593 : 173 : gimple_seq_add_stmt (&new_dlist, g);
11594 : 173 : gimple_seq_add_seq (&new_dlist, *dlist);
11595 : 173 : *dlist = new_dlist;
11596 : 173 : }
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 : 19734 : lower_oacc_private_marker (omp_context *ctx)
11618 : : {
11619 : 19734 : if (ctx->oacc_privatization_candidates.length () == 0)
11620 : : return NULL;
11621 : :
11622 : 302 : auto_vec<tree, 5> args;
11623 : :
11624 : 302 : args.quick_push (build_int_cst (integer_type_node, IFN_UNIQUE_OACC_PRIVATE));
11625 : 302 : args.quick_push (integer_zero_node);
11626 : 302 : args.quick_push (integer_minus_one_node);
11627 : :
11628 : 302 : int i;
11629 : 302 : tree decl;
11630 : 678 : FOR_EACH_VEC_ELT (ctx->oacc_privatization_candidates, i, decl)
11631 : : {
11632 : 376 : gcc_checking_assert (TREE_ADDRESSABLE (decl));
11633 : 376 : tree addr = build_fold_addr_expr (decl);
11634 : 376 : args.safe_push (addr);
11635 : : }
11636 : :
11637 : 302 : return gimple_build_call_internal_vec (IFN_UNIQUE, args);
11638 : 302 : }
11639 : :
11640 : : /* Lower code for an OMP loop directive. */
11641 : :
11642 : : static void
11643 : 46913 : lower_omp_for (gimple_stmt_iterator *gsi_p, omp_context *ctx)
11644 : : {
11645 : 46913 : tree *rhs_p, block;
11646 : 46913 : struct omp_for_data fd, *fdp = NULL;
11647 : 46913 : gomp_for *stmt = as_a <gomp_for *> (gsi_stmt (*gsi_p));
11648 : 46913 : gbind *new_stmt;
11649 : 46913 : gimple_seq omp_for_body, body, dlist, tred_ilist = NULL, tred_dlist = NULL;
11650 : 46913 : gimple_seq cnt_list = NULL, clist = NULL;
11651 : 46913 : gimple_seq oacc_head = NULL, oacc_tail = NULL;
11652 : 46913 : size_t i;
11653 : :
11654 : 46913 : push_gimplify_context ();
11655 : :
11656 : 46913 : if (is_gimple_omp_oacc (ctx->stmt))
11657 : 11341 : oacc_privatization_scan_clause_chain (ctx, gimple_omp_for_clauses (stmt));
11658 : :
11659 : 46913 : lower_omp (gimple_omp_for_pre_body_ptr (stmt), ctx);
11660 : :
11661 : 46913 : block = make_node (BLOCK);
11662 : 46913 : 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 : 46913 : 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 : 46913 : omp_for_body = gimple_omp_body (stmt);
11671 : 46913 : if (!gimple_seq_empty_p (omp_for_body)
11672 : 46913 : && gimple_code (gimple_seq_first_stmt (omp_for_body)) == GIMPLE_BIND)
11673 : : {
11674 : 19615 : gbind *inner_bind
11675 : 19615 : = as_a <gbind *> (gimple_seq_first_stmt (omp_for_body));
11676 : 19615 : tree vars = gimple_bind_vars (inner_bind);
11677 : 19615 : if (is_gimple_omp_oacc (ctx->stmt))
11678 : 5302 : oacc_privatization_scan_decl_chain (ctx, vars);
11679 : 19615 : 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 : 19615 : gimple_bind_set_vars (inner_bind, NULL_TREE);
11683 : 19615 : if (gimple_bind_block (inner_bind))
11684 : 17023 : BLOCK_VARS (gimple_bind_block (inner_bind)) = NULL_TREE;
11685 : : }
11686 : :
11687 : 46913 : if (gimple_omp_for_combined_into_p (stmt))
11688 : : {
11689 : 14601 : omp_extract_for_data (stmt, &fd, NULL);
11690 : 14601 : 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 : 14601 : size_t count = 2;
11696 : 14601 : tree type = fd.iter_type;
11697 : 14601 : if (fd.collapse > 1
11698 : 4840 : && TREE_CODE (fd.loop.n2) != INTEGER_CST)
11699 : 2680 : count += fd.collapse - 1;
11700 : 14601 : size_t count2 = 0;
11701 : 14601 : tree type2 = NULL_TREE;
11702 : 14601 : bool taskreg_for
11703 : 14601 : = (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR
11704 : 14601 : || gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_TASKLOOP);
11705 : 14601 : tree outerc = NULL, *pc = gimple_omp_for_clauses_ptr (stmt);
11706 : 14601 : tree simtc = NULL;
11707 : 14601 : tree clauses = *pc;
11708 : 14601 : if (fd.collapse > 1
11709 : 4840 : && fd.non_rect
11710 : 137 : && fd.last_nonrect == fd.first_nonrect + 1
11711 : 89 : && TREE_CODE (fd.loop.n2) != INTEGER_CST)
11712 : 64 : if (tree v = gimple_omp_for_index (stmt, fd.last_nonrect))
11713 : 64 : if (!TYPE_UNSIGNED (TREE_TYPE (v)))
11714 : : {
11715 : 60 : v = gimple_omp_for_index (stmt, fd.first_nonrect);
11716 : 60 : type2 = TREE_TYPE (v);
11717 : 60 : count++;
11718 : 60 : count2 = 3;
11719 : : }
11720 : 14601 : if (taskreg_for)
11721 : 7602 : outerc
11722 : 7602 : = omp_find_clause (gimple_omp_taskreg_clauses (ctx->outer->stmt),
11723 : : OMP_CLAUSE__LOOPTEMP_);
11724 : 14601 : if (ctx->simt_stmt)
11725 : 0 : simtc = omp_find_clause (gimple_omp_for_clauses (ctx->simt_stmt),
11726 : : OMP_CLAUSE__LOOPTEMP_);
11727 : 49199 : for (i = 0; i < count + count2; i++)
11728 : : {
11729 : 34598 : tree temp;
11730 : 34598 : if (taskreg_for)
11731 : : {
11732 : 18079 : gcc_assert (outerc);
11733 : 18079 : temp = lookup_decl (OMP_CLAUSE_DECL (outerc), ctx->outer);
11734 : 18079 : 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 : 16519 : if (ctx->simt_stmt)
11744 : 0 : temp = OMP_CLAUSE_DECL (simtc);
11745 : : else
11746 : 32894 : temp = create_tmp_var (i >= count ? type2 : type);
11747 : 16519 : insert_decl_map (&ctx->outer->cb, temp, temp);
11748 : : }
11749 : 34598 : *pc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__LOOPTEMP_);
11750 : 34598 : OMP_CLAUSE_DECL (*pc) = temp;
11751 : 34598 : pc = &OMP_CLAUSE_CHAIN (*pc);
11752 : 34598 : if (ctx->simt_stmt)
11753 : 0 : simtc = omp_find_clause (OMP_CLAUSE_CHAIN (simtc),
11754 : : OMP_CLAUSE__LOOPTEMP_);
11755 : : }
11756 : 14601 : *pc = clauses;
11757 : : }
11758 : :
11759 : : /* The pre-body and input clauses go before the lowered GIMPLE_OMP_FOR. */
11760 : 46913 : dlist = NULL;
11761 : 46913 : body = NULL;
11762 : 46913 : tree rclauses
11763 : 46913 : = omp_task_reductions_find_first (gimple_omp_for_clauses (stmt), OMP_FOR,
11764 : : OMP_CLAUSE_REDUCTION);
11765 : 46913 : tree rtmp = NULL_TREE;
11766 : 46913 : if (rclauses)
11767 : : {
11768 : 227 : tree type = build_pointer_type (pointer_sized_int_node);
11769 : 227 : tree temp = create_tmp_var (type);
11770 : 227 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__REDUCTEMP_);
11771 : 227 : OMP_CLAUSE_DECL (c) = temp;
11772 : 227 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (stmt);
11773 : 227 : gimple_omp_for_set_clauses (stmt, c);
11774 : 227 : lower_omp_task_reductions (ctx, OMP_FOR,
11775 : : gimple_omp_for_clauses (stmt),
11776 : : &tred_ilist, &tred_dlist);
11777 : 227 : rclauses = c;
11778 : 227 : rtmp = make_ssa_name (type);
11779 : 227 : gimple_seq_add_stmt (&body, gimple_build_assign (rtmp, temp));
11780 : : }
11781 : :
11782 : 46913 : lower_lastprivate_conditional_clauses (gimple_omp_for_clauses_ptr (stmt),
11783 : : ctx);
11784 : :
11785 : 46913 : lower_rec_input_clauses (gimple_omp_for_clauses (stmt), &body, &dlist, ctx,
11786 : : fdp);
11787 : 93599 : gimple_seq_add_seq (rclauses ? &tred_ilist : &body,
11788 : : gimple_omp_for_pre_body (stmt));
11789 : :
11790 : 46913 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
11791 : :
11792 : 46913 : gcall *private_marker = NULL;
11793 : 46913 : if (is_gimple_omp_oacc (ctx->stmt)
11794 : 46913 : && !gimple_seq_empty_p (omp_for_body))
11795 : 10349 : 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 : 115049 : for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
11805 : : {
11806 : 68136 : rhs_p = gimple_omp_for_initial_ptr (stmt, i);
11807 : 68136 : if (TREE_CODE (*rhs_p) == TREE_VEC)
11808 : : {
11809 : 639 : if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
11810 : 149 : TREE_VEC_ELT (*rhs_p, 1)
11811 : 298 : = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
11812 : 639 : if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
11813 : 161 : TREE_VEC_ELT (*rhs_p, 2)
11814 : 322 : = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
11815 : : }
11816 : 67497 : else if (!is_gimple_min_invariant (*rhs_p))
11817 : 11533 : *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11818 : 55964 : else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
11819 : 4635 : recompute_tree_invariant_for_addr_expr (*rhs_p);
11820 : :
11821 : 68136 : rhs_p = gimple_omp_for_final_ptr (stmt, i);
11822 : 68136 : if (TREE_CODE (*rhs_p) == TREE_VEC)
11823 : : {
11824 : 519 : if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 1)))
11825 : 142 : TREE_VEC_ELT (*rhs_p, 1)
11826 : 284 : = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 1), &cnt_list);
11827 : 519 : if (!is_gimple_min_invariant (TREE_VEC_ELT (*rhs_p, 2)))
11828 : 157 : TREE_VEC_ELT (*rhs_p, 2)
11829 : 314 : = get_formal_tmp_var (TREE_VEC_ELT (*rhs_p, 2), &cnt_list);
11830 : : }
11831 : 67617 : else if (!is_gimple_min_invariant (*rhs_p))
11832 : 16766 : *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11833 : 50851 : else if (TREE_CODE (*rhs_p) == ADDR_EXPR)
11834 : 4630 : recompute_tree_invariant_for_addr_expr (*rhs_p);
11835 : :
11836 : 68136 : rhs_p = &TREE_OPERAND (gimple_omp_for_incr (stmt, i), 1);
11837 : 68136 : if (!is_gimple_min_invariant (*rhs_p))
11838 : 6314 : *rhs_p = get_formal_tmp_var (*rhs_p, &cnt_list);
11839 : : }
11840 : 46913 : if (rclauses)
11841 : 227 : gimple_seq_add_seq (&tred_ilist, cnt_list);
11842 : : else
11843 : 46686 : gimple_seq_add_seq (&body, cnt_list);
11844 : :
11845 : : /* Once lowered, extract the bounds and clauses. */
11846 : 46913 : omp_extract_for_data (stmt, &fd, NULL);
11847 : :
11848 : 46913 : if (is_gimple_omp_oacc (ctx->stmt)
11849 : 46913 : && !ctx_in_oacc_kernels_region (ctx))
11850 : 9669 : 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 : 46913 : if (oacc_head)
11856 : 9669 : gimple_seq_add_seq (&body, oacc_head);
11857 : :
11858 : 46913 : lower_omp_for_lastprivate (&fd, &body, &dlist, &clist, ctx);
11859 : :
11860 : 46913 : if (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
11861 : 85682 : for (tree c = gimple_omp_for_clauses (stmt); c; c = OMP_CLAUSE_CHAIN (c))
11862 : 70130 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
11863 : 70130 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
11864 : : {
11865 : 227 : OMP_CLAUSE_DECL (c) = lookup_decl (OMP_CLAUSE_DECL (c), ctx);
11866 : 227 : if (DECL_P (OMP_CLAUSE_LINEAR_STEP (c)))
11867 : 42 : OMP_CLAUSE_LINEAR_STEP (c)
11868 : 84 : = maybe_lookup_decl_in_outer_ctx (OMP_CLAUSE_LINEAR_STEP (c),
11869 : : ctx);
11870 : : }
11871 : :
11872 : 46547 : if ((ctx->scan_inclusive || ctx->scan_exclusive)
11873 : 47181 : && gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_FOR)
11874 : 173 : lower_omp_for_scan (&body, &dlist, stmt, &fd, ctx);
11875 : : else
11876 : : {
11877 : 46740 : gimple_seq_add_stmt (&body, stmt);
11878 : 46740 : gimple_seq_add_seq (&body, gimple_omp_body (stmt));
11879 : : }
11880 : :
11881 : 46913 : 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 : 46913 : lower_reduction_clauses (gimple_omp_for_clauses (stmt), &body, &clist, ctx);
11886 : :
11887 : 46913 : if (clist)
11888 : : {
11889 : 179 : tree fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
11890 : 179 : gcall *g = gimple_build_call (fndecl, 0);
11891 : 179 : gimple_seq_add_stmt (&body, g);
11892 : 179 : gimple_seq_add_seq (&body, clist);
11893 : 179 : fndecl = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
11894 : 179 : g = gimple_build_call (fndecl, 0);
11895 : 179 : gimple_seq_add_stmt (&body, g);
11896 : : }
11897 : :
11898 : 46913 : if (ctx->cancellable)
11899 : 84 : gimple_seq_add_stmt (&body, gimple_build_label (ctx->cancel_label));
11900 : :
11901 : 46913 : gimple_seq_add_seq (&body, dlist);
11902 : :
11903 : 46913 : if (rclauses)
11904 : : {
11905 : 227 : gimple_seq_add_seq (&tred_ilist, body);
11906 : 227 : body = tred_ilist;
11907 : : }
11908 : :
11909 : 46913 : body = maybe_catch_exception (body);
11910 : :
11911 : : /* Region exit marker goes at the end of the loop body. */
11912 : 46913 : gimple *g = gimple_build_omp_return (fd.have_nowait);
11913 : 46913 : gimple_seq_add_stmt (&body, g);
11914 : :
11915 : 46913 : gimple_seq_add_seq (&body, tred_dlist);
11916 : :
11917 : 46913 : maybe_add_implicit_barrier_cancel (ctx, g, &body);
11918 : :
11919 : 46913 : if (rclauses)
11920 : 227 : OMP_CLAUSE_DECL (rclauses) = rtmp;
11921 : :
11922 : : /* Add OpenACC joining and reduction markers just after the loop. */
11923 : 46913 : if (oacc_tail)
11924 : 9669 : gimple_seq_add_seq (&body, oacc_tail);
11925 : :
11926 : 46913 : pop_gimplify_context (new_stmt);
11927 : :
11928 : 46913 : gimple_bind_append_vars (new_stmt, ctx->block_vars);
11929 : 46913 : maybe_remove_omp_member_access_dummy_vars (new_stmt);
11930 : 46913 : BLOCK_VARS (block) = gimple_bind_vars (new_stmt);
11931 : 46913 : if (BLOCK_VARS (block))
11932 : 46277 : TREE_USED (block) = 1;
11933 : :
11934 : 46913 : gimple_bind_set_body (new_stmt, body);
11935 : 46913 : gimple_omp_set_body (stmt, NULL);
11936 : 46913 : gimple_omp_for_set_pre_body (stmt, NULL);
11937 : 46913 : }
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 : 212320 : check_combined_parallel (gimple_stmt_iterator *gsi_p,
11944 : : bool *handled_ops_p,
11945 : : struct walk_stmt_info *wi)
11946 : : {
11947 : 212320 : int *info = (int *) wi->info;
11948 : 212320 : gimple *stmt = gsi_stmt (*gsi_p);
11949 : :
11950 : 212320 : *handled_ops_p = true;
11951 : 212320 : switch (gimple_code (stmt))
11952 : : {
11953 : 11443 : WALK_SUBSTMTS;
11954 : :
11955 : : case GIMPLE_DEBUG:
11956 : : break;
11957 : 1896 : case GIMPLE_OMP_FOR:
11958 : 1896 : case GIMPLE_OMP_SECTIONS:
11959 : 1896 : *info = *info == 0 ? 1 : -1;
11960 : 1896 : break;
11961 : 198942 : default:
11962 : 198942 : *info = -1;
11963 : 198942 : break;
11964 : : }
11965 : 212320 : 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 : 366 : task_copyfn_copy_decl (tree var, copy_body_data *cb)
11980 : : {
11981 : 366 : struct omp_taskcopy_context *tcctx = (struct omp_taskcopy_context *) cb;
11982 : :
11983 : 366 : if (splay_tree_lookup (tcctx->ctx->sfield_map, (splay_tree_key) var))
11984 : 158 : return create_tmp_var (TREE_TYPE (var));
11985 : :
11986 : : return var;
11987 : : }
11988 : :
11989 : : static tree
11990 : 50 : task_copyfn_remap_type (struct omp_taskcopy_context *tcctx, tree orig_type)
11991 : : {
11992 : 50 : tree name, new_fields = NULL, type, f;
11993 : :
11994 : 50 : type = lang_hooks.types.make_type (RECORD_TYPE);
11995 : 50 : name = DECL_NAME (TYPE_NAME (orig_type));
11996 : 50 : name = build_decl (gimple_location (tcctx->ctx->stmt),
11997 : : TYPE_DECL, name, type);
11998 : 50 : TYPE_NAME (type) = name;
11999 : :
12000 : 1140 : for (f = TYPE_FIELDS (orig_type); f ; f = TREE_CHAIN (f))
12001 : : {
12002 : 1090 : tree new_f = copy_node (f);
12003 : 1090 : DECL_CONTEXT (new_f) = type;
12004 : 1090 : TREE_TYPE (new_f) = remap_type (TREE_TYPE (f), &tcctx->cb);
12005 : 1090 : TREE_CHAIN (new_f) = new_fields;
12006 : 1090 : walk_tree (&DECL_SIZE (new_f), copy_tree_body_r, &tcctx->cb, NULL);
12007 : 1090 : walk_tree (&DECL_SIZE_UNIT (new_f), copy_tree_body_r, &tcctx->cb, NULL);
12008 : 1090 : walk_tree (&DECL_FIELD_OFFSET (new_f), copy_tree_body_r,
12009 : : &tcctx->cb, NULL);
12010 : 1090 : new_fields = new_f;
12011 : 1090 : tcctx->cb.decl_map->put (f, new_f);
12012 : : }
12013 : 50 : TYPE_FIELDS (type) = nreverse (new_fields);
12014 : 50 : layout_type (type);
12015 : 50 : return type;
12016 : : }
12017 : :
12018 : : /* Create task copyfn. */
12019 : :
12020 : : static void
12021 : 518 : create_task_copyfn (gomp_task *task_stmt, omp_context *ctx)
12022 : : {
12023 : 518 : struct function *child_cfun;
12024 : 518 : tree child_fn, t, c, src, dst, f, sf, arg, sarg, decl;
12025 : 518 : tree record_type, srecord_type, bind, list;
12026 : 518 : bool record_needs_remap = false, srecord_needs_remap = false;
12027 : 518 : splay_tree_node n;
12028 : 518 : struct omp_taskcopy_context tcctx;
12029 : 518 : location_t loc = gimple_location (task_stmt);
12030 : 518 : size_t looptempno = 0;
12031 : :
12032 : 518 : child_fn = gimple_omp_task_copy_fn (task_stmt);
12033 : 518 : task_cpyfns.safe_push (task_stmt);
12034 : 518 : child_cfun = DECL_STRUCT_FUNCTION (child_fn);
12035 : 518 : gcc_assert (child_cfun->cfg == NULL);
12036 : 518 : DECL_SAVED_TREE (child_fn) = alloc_stmt_list ();
12037 : :
12038 : : /* Reset DECL_CONTEXT on function arguments. */
12039 : 1554 : for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
12040 : 1036 : DECL_CONTEXT (t) = child_fn;
12041 : :
12042 : : /* Populate the function. */
12043 : 518 : push_gimplify_context ();
12044 : 518 : push_cfun (child_cfun);
12045 : :
12046 : 518 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
12047 : 518 : TREE_SIDE_EFFECTS (bind) = 1;
12048 : 518 : list = NULL;
12049 : 518 : DECL_SAVED_TREE (child_fn) = bind;
12050 : 518 : DECL_SOURCE_LOCATION (child_fn) = gimple_location (task_stmt);
12051 : :
12052 : : /* Remap src and dst argument types if needed. */
12053 : 518 : record_type = ctx->record_type;
12054 : 518 : srecord_type = ctx->srecord_type;
12055 : 3408 : for (f = TYPE_FIELDS (record_type); f ; f = DECL_CHAIN (f))
12056 : 2915 : if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
12057 : : {
12058 : : record_needs_remap = true;
12059 : : break;
12060 : : }
12061 : 3147 : for (f = TYPE_FIELDS (srecord_type); f ; f = DECL_CHAIN (f))
12062 : 2654 : if (variably_modified_type_p (TREE_TYPE (f), ctx->cb.src_fn))
12063 : : {
12064 : : srecord_needs_remap = true;
12065 : : break;
12066 : : }
12067 : :
12068 : 518 : if (record_needs_remap || srecord_needs_remap)
12069 : : {
12070 : 25 : memset (&tcctx, '\0', sizeof (tcctx));
12071 : 25 : tcctx.cb.src_fn = ctx->cb.src_fn;
12072 : 25 : tcctx.cb.dst_fn = child_fn;
12073 : 25 : tcctx.cb.src_node = cgraph_node::get (tcctx.cb.src_fn);
12074 : 25 : gcc_checking_assert (tcctx.cb.src_node);
12075 : 25 : tcctx.cb.dst_node = tcctx.cb.src_node;
12076 : 25 : tcctx.cb.src_cfun = ctx->cb.src_cfun;
12077 : 25 : tcctx.cb.copy_decl = task_copyfn_copy_decl;
12078 : 25 : tcctx.cb.eh_lp_nr = 0;
12079 : 25 : tcctx.cb.transform_call_graph_edges = CB_CGE_MOVE;
12080 : 25 : tcctx.cb.decl_map = new hash_map<tree, tree>;
12081 : 25 : tcctx.ctx = ctx;
12082 : :
12083 : 25 : if (record_needs_remap)
12084 : 25 : record_type = task_copyfn_remap_type (&tcctx, record_type);
12085 : 25 : if (srecord_needs_remap)
12086 : 25 : srecord_type = task_copyfn_remap_type (&tcctx, srecord_type);
12087 : : }
12088 : : else
12089 : 493 : tcctx.cb.decl_map = NULL;
12090 : :
12091 : 518 : arg = DECL_ARGUMENTS (child_fn);
12092 : 518 : TREE_TYPE (arg) = build_pointer_type (record_type);
12093 : 518 : sarg = DECL_CHAIN (arg);
12094 : 518 : 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 : 518 : if (tcctx.cb.decl_map)
12099 : 631 : for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12100 : 606 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12101 : : {
12102 : 497 : tree *p;
12103 : :
12104 : 497 : decl = OMP_CLAUSE_DECL (c);
12105 : 497 : p = tcctx.cb.decl_map->get (decl);
12106 : 497 : if (p == NULL)
12107 : 339 : continue;
12108 : 158 : n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12109 : 158 : sf = (tree) n->value;
12110 : 158 : sf = *tcctx.cb.decl_map->get (sf);
12111 : 158 : src = build_simple_mem_ref_loc (loc, sarg);
12112 : 158 : src = omp_build_component_ref (src, sf);
12113 : 158 : t = build2 (MODIFY_EXPR, TREE_TYPE (*p), *p, src);
12114 : 158 : append_to_statement_list (t, &list);
12115 : : }
12116 : :
12117 : : /* Second pass: copy shared var pointers and copy construct non-VLA
12118 : : firstprivate vars. */
12119 : 6170 : for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12120 : 5652 : switch (OMP_CLAUSE_CODE (c))
12121 : : {
12122 : 593 : splay_tree_key key;
12123 : 593 : case OMP_CLAUSE_SHARED:
12124 : 593 : decl = OMP_CLAUSE_DECL (c);
12125 : 593 : key = (splay_tree_key) decl;
12126 : 593 : if (OMP_CLAUSE_SHARED_FIRSTPRIVATE (c))
12127 : 273 : key = (splay_tree_key) &DECL_UID (decl);
12128 : 593 : n = splay_tree_lookup (ctx->field_map, key);
12129 : 593 : if (n == NULL)
12130 : : break;
12131 : 159 : f = (tree) n->value;
12132 : 159 : if (tcctx.cb.decl_map)
12133 : 12 : f = *tcctx.cb.decl_map->get (f);
12134 : 159 : n = splay_tree_lookup (ctx->sfield_map, key);
12135 : 159 : sf = (tree) n->value;
12136 : 159 : if (tcctx.cb.decl_map)
12137 : 12 : sf = *tcctx.cb.decl_map->get (sf);
12138 : 159 : src = build_simple_mem_ref_loc (loc, sarg);
12139 : 159 : src = omp_build_component_ref (src, sf);
12140 : 159 : dst = build_simple_mem_ref_loc (loc, arg);
12141 : 159 : dst = omp_build_component_ref (dst, f);
12142 : 159 : t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12143 : 159 : append_to_statement_list (t, &list);
12144 : 159 : break;
12145 : 652 : case OMP_CLAUSE_REDUCTION:
12146 : 652 : case OMP_CLAUSE_IN_REDUCTION:
12147 : 652 : decl = OMP_CLAUSE_DECL (c);
12148 : 652 : if (TREE_CODE (decl) == MEM_REF)
12149 : : {
12150 : 352 : decl = TREE_OPERAND (decl, 0);
12151 : 352 : if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
12152 : 98 : decl = TREE_OPERAND (decl, 0);
12153 : 352 : if (TREE_CODE (decl) == INDIRECT_REF
12154 : 278 : || TREE_CODE (decl) == ADDR_EXPR)
12155 : 74 : decl = TREE_OPERAND (decl, 0);
12156 : : }
12157 : 652 : key = (splay_tree_key) decl;
12158 : 652 : n = splay_tree_lookup (ctx->field_map, key);
12159 : 652 : if (n == NULL)
12160 : : break;
12161 : 279 : f = (tree) n->value;
12162 : 279 : if (tcctx.cb.decl_map)
12163 : 20 : f = *tcctx.cb.decl_map->get (f);
12164 : 279 : n = splay_tree_lookup (ctx->sfield_map, key);
12165 : 279 : sf = (tree) n->value;
12166 : 279 : if (tcctx.cb.decl_map)
12167 : 20 : sf = *tcctx.cb.decl_map->get (sf);
12168 : 279 : src = build_simple_mem_ref_loc (loc, sarg);
12169 : 279 : src = omp_build_component_ref (src, sf);
12170 : 279 : if (decl != OMP_CLAUSE_DECL (c)
12171 : 273 : && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
12172 : 552 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
12173 : 72 : src = build_simple_mem_ref_loc (loc, src);
12174 : 279 : dst = build_simple_mem_ref_loc (loc, arg);
12175 : 279 : dst = omp_build_component_ref (dst, f);
12176 : 279 : t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12177 : 279 : append_to_statement_list (t, &list);
12178 : 279 : break;
12179 : 754 : case OMP_CLAUSE__LOOPTEMP_:
12180 : : /* Fields for first two _looptemp_ clauses are initialized by
12181 : : GOMP_taskloop*, the rest are handled like firstprivate. */
12182 : 754 : if (looptempno < 2)
12183 : : {
12184 : 748 : looptempno++;
12185 : 748 : break;
12186 : : }
12187 : : /* FALLTHRU */
12188 : 1835 : case OMP_CLAUSE__REDUCTEMP_:
12189 : 1835 : case OMP_CLAUSE_FIRSTPRIVATE:
12190 : 1835 : decl = OMP_CLAUSE_DECL (c);
12191 : 1835 : if (is_variable_sized (decl))
12192 : : break;
12193 : 1821 : n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12194 : 1821 : if (n == NULL)
12195 : : break;
12196 : 1821 : f = (tree) n->value;
12197 : 1821 : if (tcctx.cb.decl_map)
12198 : 490 : f = *tcctx.cb.decl_map->get (f);
12199 : 1821 : n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12200 : 1821 : if (n != NULL)
12201 : : {
12202 : 1571 : sf = (tree) n->value;
12203 : 1571 : if (tcctx.cb.decl_map)
12204 : 490 : sf = *tcctx.cb.decl_map->get (sf);
12205 : 1571 : src = build_simple_mem_ref_loc (loc, sarg);
12206 : 1571 : src = omp_build_component_ref (src, sf);
12207 : 1571 : if (use_pointer_for_field (decl, NULL)
12208 : 1571 : || omp_privatize_by_reference (decl))
12209 : 319 : src = build_simple_mem_ref_loc (loc, src);
12210 : : }
12211 : : else
12212 : : src = decl;
12213 : 1821 : dst = build_simple_mem_ref_loc (loc, arg);
12214 : 1821 : dst = omp_build_component_ref (dst, f);
12215 : 1821 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
12216 : 192 : t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12217 : : else
12218 : : {
12219 : 1629 : if (ctx->allocate_map)
12220 : 84 : if (tree *allocatorp = ctx->allocate_map->get (decl))
12221 : : {
12222 : 50 : tree allocator = *allocatorp;
12223 : 50 : HOST_WIDE_INT ialign = 0;
12224 : 50 : if (TREE_CODE (allocator) == TREE_LIST)
12225 : : {
12226 : 4 : ialign = tree_to_uhwi (TREE_VALUE (allocator));
12227 : 4 : allocator = TREE_PURPOSE (allocator);
12228 : : }
12229 : 50 : if (TREE_CODE (allocator) != INTEGER_CST)
12230 : : {
12231 : 22 : n = splay_tree_lookup (ctx->sfield_map,
12232 : : (splay_tree_key) allocator);
12233 : 22 : allocator = (tree) n->value;
12234 : 22 : if (tcctx.cb.decl_map)
12235 : 0 : allocator = *tcctx.cb.decl_map->get (allocator);
12236 : 22 : tree a = build_simple_mem_ref_loc (loc, sarg);
12237 : 22 : allocator = omp_build_component_ref (a, allocator);
12238 : : }
12239 : 50 : allocator = fold_convert (pointer_sized_int_node, allocator);
12240 : 50 : tree a = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
12241 : 50 : tree align = build_int_cst (size_type_node,
12242 : 50 : MAX (ialign,
12243 : : DECL_ALIGN_UNIT (decl)));
12244 : 50 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dst)));
12245 : 50 : tree ptr = build_call_expr_loc (loc, a, 3, align, sz,
12246 : : allocator);
12247 : 50 : ptr = fold_convert (TREE_TYPE (dst), ptr);
12248 : 50 : t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, ptr);
12249 : 50 : append_to_statement_list (t, &list);
12250 : 50 : dst = build_simple_mem_ref_loc (loc, dst);
12251 : : }
12252 : 1629 : t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
12253 : : }
12254 : 1821 : append_to_statement_list (t, &list);
12255 : 1821 : break;
12256 : 489 : case OMP_CLAUSE_PRIVATE:
12257 : 489 : if (! OMP_CLAUSE_PRIVATE_OUTER_REF (c))
12258 : : break;
12259 : 12 : decl = OMP_CLAUSE_DECL (c);
12260 : 12 : n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12261 : 12 : f = (tree) n->value;
12262 : 12 : if (tcctx.cb.decl_map)
12263 : 0 : f = *tcctx.cb.decl_map->get (f);
12264 : 12 : n = splay_tree_lookup (ctx->sfield_map, (splay_tree_key) decl);
12265 : 12 : if (n != NULL)
12266 : : {
12267 : 12 : sf = (tree) n->value;
12268 : 12 : if (tcctx.cb.decl_map)
12269 : 0 : sf = *tcctx.cb.decl_map->get (sf);
12270 : 12 : src = build_simple_mem_ref_loc (loc, sarg);
12271 : 12 : src = omp_build_component_ref (src, sf);
12272 : 12 : if (use_pointer_for_field (decl, NULL))
12273 : 12 : src = build_simple_mem_ref_loc (loc, src);
12274 : : }
12275 : : else
12276 : : src = decl;
12277 : 12 : dst = build_simple_mem_ref_loc (loc, arg);
12278 : 12 : dst = omp_build_component_ref (dst, f);
12279 : 12 : t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12280 : 12 : append_to_statement_list (t, &list);
12281 : 12 : break;
12282 : : default:
12283 : : break;
12284 : : }
12285 : :
12286 : : /* Last pass: handle VLA firstprivates. */
12287 : 518 : if (tcctx.cb.decl_map)
12288 : 631 : for (c = gimple_omp_task_clauses (task_stmt); c; c = OMP_CLAUSE_CHAIN (c))
12289 : 606 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12290 : : {
12291 : 497 : tree ind, ptr, df;
12292 : :
12293 : 497 : decl = OMP_CLAUSE_DECL (c);
12294 : 497 : if (!is_variable_sized (decl))
12295 : 483 : continue;
12296 : 14 : n = splay_tree_lookup (ctx->field_map, (splay_tree_key) decl);
12297 : 14 : if (n == NULL)
12298 : 0 : continue;
12299 : 14 : f = (tree) n->value;
12300 : 14 : f = *tcctx.cb.decl_map->get (f);
12301 : 14 : gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
12302 : 14 : ind = DECL_VALUE_EXPR (decl);
12303 : 14 : gcc_assert (TREE_CODE (ind) == INDIRECT_REF);
12304 : 14 : gcc_assert (DECL_P (TREE_OPERAND (ind, 0)));
12305 : 42 : n = splay_tree_lookup (ctx->sfield_map,
12306 : 14 : (splay_tree_key) TREE_OPERAND (ind, 0));
12307 : 14 : sf = (tree) n->value;
12308 : 14 : sf = *tcctx.cb.decl_map->get (sf);
12309 : 14 : src = build_simple_mem_ref_loc (loc, sarg);
12310 : 14 : src = omp_build_component_ref (src, sf);
12311 : 14 : src = build_simple_mem_ref_loc (loc, src);
12312 : 14 : dst = build_simple_mem_ref_loc (loc, arg);
12313 : 14 : dst = omp_build_component_ref (dst, f);
12314 : 14 : t = lang_hooks.decls.omp_clause_copy_ctor (c, dst, src);
12315 : 14 : append_to_statement_list (t, &list);
12316 : 42 : n = splay_tree_lookup (ctx->field_map,
12317 : 14 : (splay_tree_key) TREE_OPERAND (ind, 0));
12318 : 14 : df = (tree) n->value;
12319 : 14 : df = *tcctx.cb.decl_map->get (df);
12320 : 14 : ptr = build_simple_mem_ref_loc (loc, arg);
12321 : 14 : ptr = omp_build_component_ref (ptr, df);
12322 : 14 : t = build2 (MODIFY_EXPR, TREE_TYPE (ptr), ptr,
12323 : : build_fold_addr_expr_loc (loc, dst));
12324 : 14 : append_to_statement_list (t, &list);
12325 : : }
12326 : :
12327 : 518 : t = build1 (RETURN_EXPR, void_type_node, NULL);
12328 : 518 : append_to_statement_list (t, &list);
12329 : :
12330 : 518 : if (tcctx.cb.decl_map)
12331 : 25 : delete tcctx.cb.decl_map;
12332 : 518 : pop_gimplify_context (NULL);
12333 : 518 : BIND_EXPR_BODY (bind) = list;
12334 : 518 : pop_cfun ();
12335 : 518 : }
12336 : :
12337 : : static void
12338 : 1525 : lower_depend_clauses (tree *pclauses, gimple_seq *iseq, gimple_seq *oseq)
12339 : : {
12340 : 1525 : tree c, clauses;
12341 : 1525 : gimple *g;
12342 : 1525 : size_t cnt[5] = { 0, 0, 0, 0, 0 }, idx = 2, i;
12343 : :
12344 : 1525 : clauses = omp_find_clause (*pclauses, OMP_CLAUSE_DEPEND);
12345 : 1525 : gcc_assert (clauses);
12346 : 6213 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12347 : 4808 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12348 : 1685 : switch (OMP_CLAUSE_DEPEND_KIND (c))
12349 : : {
12350 : 120 : case OMP_CLAUSE_DEPEND_LAST:
12351 : : /* Lowering already done at gimplification. */
12352 : 120 : return;
12353 : 473 : case OMP_CLAUSE_DEPEND_IN:
12354 : 473 : cnt[2]++;
12355 : 473 : break;
12356 : 963 : case OMP_CLAUSE_DEPEND_OUT:
12357 : 963 : case OMP_CLAUSE_DEPEND_INOUT:
12358 : 963 : cnt[0]++;
12359 : 963 : break;
12360 : 30 : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
12361 : 30 : cnt[1]++;
12362 : 30 : break;
12363 : 67 : case OMP_CLAUSE_DEPEND_DEPOBJ:
12364 : 67 : cnt[3]++;
12365 : 67 : break;
12366 : 32 : case OMP_CLAUSE_DEPEND_INOUTSET:
12367 : 32 : cnt[4]++;
12368 : 32 : break;
12369 : 0 : default:
12370 : 0 : gcc_unreachable ();
12371 : : }
12372 : 1405 : if (cnt[1] || cnt[3] || cnt[4])
12373 : 126 : idx = 5;
12374 : 1405 : size_t total = cnt[0] + cnt[1] + cnt[2] + cnt[3] + cnt[4];
12375 : 1405 : size_t inoutidx = total + idx;
12376 : 1405 : tree type = build_array_type_nelts (ptr_type_node, total + idx + 2 * cnt[4]);
12377 : 1405 : tree array = create_tmp_var (type);
12378 : 1405 : TREE_ADDRESSABLE (array) = 1;
12379 : 1405 : tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
12380 : : NULL_TREE);
12381 : 1405 : if (idx == 5)
12382 : : {
12383 : 126 : g = gimple_build_assign (r, build_int_cst (ptr_type_node, 0));
12384 : 126 : gimple_seq_add_stmt (iseq, g);
12385 : 126 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
12386 : : NULL_TREE);
12387 : : }
12388 : 1405 : g = gimple_build_assign (r, build_int_cst (ptr_type_node, total));
12389 : 1405 : gimple_seq_add_stmt (iseq, g);
12390 : 7025 : for (i = 0; i < (idx == 5 ? 3 : 1); i++)
12391 : : {
12392 : 1657 : r = build4 (ARRAY_REF, ptr_type_node, array,
12393 : 1657 : size_int (i + 1 + (idx == 5)), NULL_TREE, NULL_TREE);
12394 : 1657 : g = gimple_build_assign (r, build_int_cst (ptr_type_node, cnt[i]));
12395 : 1657 : gimple_seq_add_stmt (iseq, g);
12396 : : }
12397 : 8430 : for (i = 0; i < 5; i++)
12398 : : {
12399 : 7025 : if (cnt[i] == 0)
12400 : 5564 : continue;
12401 : 6467 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12402 : 5006 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12403 : 3302 : continue;
12404 : : else
12405 : : {
12406 : 1704 : switch (OMP_CLAUSE_DEPEND_KIND (c))
12407 : : {
12408 : 539 : case OMP_CLAUSE_DEPEND_IN:
12409 : 539 : if (i != 2)
12410 : 139 : continue;
12411 : : break;
12412 : 1019 : case OMP_CLAUSE_DEPEND_OUT:
12413 : 1019 : case OMP_CLAUSE_DEPEND_INOUT:
12414 : 1019 : if (i != 0)
12415 : 56 : continue;
12416 : : break;
12417 : 44 : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
12418 : 44 : if (i != 1)
12419 : 14 : continue;
12420 : : break;
12421 : 70 : case OMP_CLAUSE_DEPEND_DEPOBJ:
12422 : 70 : if (i != 3)
12423 : 3 : continue;
12424 : : break;
12425 : 32 : case OMP_CLAUSE_DEPEND_INOUTSET:
12426 : 32 : if (i != 4)
12427 : 0 : continue;
12428 : : break;
12429 : 0 : default:
12430 : 0 : gcc_unreachable ();
12431 : : }
12432 : 1565 : tree t = OMP_CLAUSE_DECL (c);
12433 : 1565 : if (i == 4)
12434 : : {
12435 : 32 : t = build4 (ARRAY_REF, ptr_type_node, array,
12436 : 32 : size_int (inoutidx), NULL_TREE, NULL_TREE);
12437 : 32 : t = build_fold_addr_expr (t);
12438 : 32 : inoutidx += 2;
12439 : : }
12440 : 1565 : t = fold_convert (ptr_type_node, t);
12441 : 1565 : gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
12442 : 1565 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12443 : : NULL_TREE, NULL_TREE);
12444 : 1565 : g = gimple_build_assign (r, t);
12445 : 1565 : gimple_seq_add_stmt (iseq, g);
12446 : : }
12447 : : }
12448 : 1405 : if (cnt[4])
12449 : 81 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12450 : 49 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12451 : 49 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_INOUTSET)
12452 : : {
12453 : 32 : tree t = OMP_CLAUSE_DECL (c);
12454 : 32 : t = fold_convert (ptr_type_node, t);
12455 : 32 : gimplify_expr (&t, iseq, NULL, is_gimple_val, fb_rvalue);
12456 : 32 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12457 : : NULL_TREE, NULL_TREE);
12458 : 32 : g = gimple_build_assign (r, t);
12459 : 32 : gimple_seq_add_stmt (iseq, g);
12460 : 32 : t = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
12461 : 32 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (idx++),
12462 : : NULL_TREE, NULL_TREE);
12463 : 32 : g = gimple_build_assign (r, t);
12464 : 32 : gimple_seq_add_stmt (iseq, g);
12465 : : }
12466 : :
12467 : 1405 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
12468 : 1405 : OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
12469 : 1405 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
12470 : 1405 : OMP_CLAUSE_CHAIN (c) = *pclauses;
12471 : 1405 : *pclauses = c;
12472 : 1405 : tree clobber = build_clobber (type);
12473 : 1405 : g = gimple_build_assign (array, clobber);
12474 : 1405 : 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 : 22487 : lower_omp_taskreg (gimple_stmt_iterator *gsi_p, omp_context *ctx)
12482 : : {
12483 : 22487 : tree clauses;
12484 : 22487 : tree child_fn, t;
12485 : 22487 : gimple *stmt = gsi_stmt (*gsi_p);
12486 : 22487 : gbind *par_bind, *bind, *dep_bind = NULL;
12487 : 22487 : gimple_seq par_body;
12488 : 22487 : location_t loc = gimple_location (stmt);
12489 : :
12490 : 22487 : clauses = gimple_omp_taskreg_clauses (stmt);
12491 : 22487 : if (gimple_code (stmt) == GIMPLE_OMP_TASK
12492 : 22487 : && gimple_omp_task_taskwait_p (stmt))
12493 : : {
12494 : 78 : par_bind = NULL;
12495 : 78 : par_body = NULL;
12496 : : }
12497 : : else
12498 : : {
12499 : 22409 : par_bind
12500 : 22409 : = as_a <gbind *> (gimple_seq_first_stmt (gimple_omp_body (stmt)));
12501 : 22409 : par_body = gimple_bind_body (par_bind);
12502 : : }
12503 : 22487 : child_fn = ctx->cb.dst_fn;
12504 : 22487 : if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
12505 : 22487 : && !gimple_omp_parallel_combined_p (stmt))
12506 : : {
12507 : 4184 : struct walk_stmt_info wi;
12508 : 4184 : int ws_num = 0;
12509 : :
12510 : 4184 : memset (&wi, 0, sizeof (wi));
12511 : 4184 : wi.info = &ws_num;
12512 : 4184 : wi.val_only = true;
12513 : 4184 : walk_gimple_seq (par_body, check_combined_parallel, NULL, &wi);
12514 : 4184 : if (ws_num == 1)
12515 : 377 : gimple_omp_parallel_set_combined_p (stmt, true);
12516 : : }
12517 : 22487 : gimple_seq dep_ilist = NULL;
12518 : 22487 : gimple_seq dep_olist = NULL;
12519 : 22487 : if (gimple_code (stmt) == GIMPLE_OMP_TASK
12520 : 22487 : && omp_find_clause (clauses, OMP_CLAUSE_DEPEND))
12521 : : {
12522 : 1179 : push_gimplify_context ();
12523 : 1179 : dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12524 : 1179 : lower_depend_clauses (gimple_omp_task_clauses_ptr (stmt),
12525 : : &dep_ilist, &dep_olist);
12526 : : }
12527 : :
12528 : 22487 : if (gimple_code (stmt) == GIMPLE_OMP_TASK
12529 : 22487 : && gimple_omp_task_taskwait_p (stmt))
12530 : : {
12531 : 78 : if (dep_bind)
12532 : : {
12533 : 78 : gsi_replace (gsi_p, dep_bind, true);
12534 : 78 : gimple_bind_add_seq (dep_bind, dep_ilist);
12535 : 78 : gimple_bind_add_stmt (dep_bind, stmt);
12536 : 78 : gimple_bind_add_seq (dep_bind, dep_olist);
12537 : 78 : pop_gimplify_context (dep_bind);
12538 : : }
12539 : 78 : return;
12540 : : }
12541 : :
12542 : 22409 : if (ctx->srecord_type)
12543 : 518 : create_task_copyfn (as_a <gomp_task *> (stmt), ctx);
12544 : :
12545 : 22409 : gimple_seq tskred_ilist = NULL;
12546 : 22409 : gimple_seq tskred_olist = NULL;
12547 : 22409 : if ((is_task_ctx (ctx)
12548 : 3783 : && gimple_omp_task_taskloop_p (ctx->stmt)
12549 : 1343 : && omp_find_clause (gimple_omp_task_clauses (ctx->stmt),
12550 : : OMP_CLAUSE_REDUCTION))
12551 : 25699 : || (is_parallel_ctx (ctx)
12552 : 16091 : && omp_find_clause (gimple_omp_parallel_clauses (stmt),
12553 : : OMP_CLAUSE__REDUCTEMP_)))
12554 : : {
12555 : 552 : if (dep_bind == NULL)
12556 : : {
12557 : 552 : push_gimplify_context ();
12558 : 552 : dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12559 : : }
12560 : 611 : lower_omp_task_reductions (ctx, is_task_ctx (ctx) ? OMP_TASKLOOP
12561 : : : OMP_PARALLEL,
12562 : 552 : gimple_omp_taskreg_clauses (ctx->stmt),
12563 : : &tskred_ilist, &tskred_olist);
12564 : : }
12565 : :
12566 : 22409 : push_gimplify_context ();
12567 : :
12568 : 22409 : gimple_seq par_olist = NULL;
12569 : 22409 : gimple_seq par_ilist = NULL;
12570 : 22409 : gimple_seq par_rlist = NULL;
12571 : 22409 : lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx, NULL);
12572 : 22405 : lower_omp (&par_body, ctx);
12573 : 22405 : if (gimple_code (stmt) != GIMPLE_OMP_TASK)
12574 : 18622 : 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 : 22405 : record_vars_into (ctx->block_vars, child_fn);
12579 : 22405 : maybe_remove_omp_member_access_dummy_vars (par_bind);
12580 : 22405 : record_vars_into (gimple_bind_vars (par_bind), child_fn);
12581 : :
12582 : 22405 : if (ctx->record_type)
12583 : : {
12584 : 17995 : ctx->sender_decl
12585 : 35472 : = create_tmp_var (ctx->srecord_type ? ctx->srecord_type
12586 : : : ctx->record_type, ".omp_data_o");
12587 : 17995 : DECL_NAMELESS (ctx->sender_decl) = 1;
12588 : 17995 : TREE_ADDRESSABLE (ctx->sender_decl) = 1;
12589 : 17995 : gimple_omp_taskreg_set_data_arg (stmt, ctx->sender_decl);
12590 : : }
12591 : :
12592 : 22405 : gimple_seq olist = NULL;
12593 : 22405 : gimple_seq ilist = NULL;
12594 : 22405 : lower_send_clauses (clauses, &ilist, &olist, ctx);
12595 : 22405 : lower_send_shared_vars (&ilist, &olist, ctx);
12596 : :
12597 : 22405 : if (ctx->record_type)
12598 : : {
12599 : 17995 : tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
12600 : 17995 : 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 : 22405 : gimple_seq new_body = NULL;
12608 : :
12609 : 22405 : if (ctx->record_type)
12610 : : {
12611 : 17995 : t = build_fold_addr_expr_loc (loc, ctx->sender_decl);
12612 : : /* fixup_child_record_type might have changed receiver_decl's type. */
12613 : 17995 : t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
12614 : 17995 : gimple_seq_add_stmt (&new_body,
12615 : 17995 : gimple_build_assign (ctx->receiver_decl, t));
12616 : : }
12617 : :
12618 : 22405 : gimple_seq_add_seq (&new_body, par_ilist);
12619 : 22405 : gimple_seq_add_seq (&new_body, par_body);
12620 : 22405 : gimple_seq_add_seq (&new_body, par_rlist);
12621 : 22405 : if (ctx->cancellable)
12622 : 135 : gimple_seq_add_stmt (&new_body, gimple_build_label (ctx->cancel_label));
12623 : 22405 : gimple_seq_add_seq (&new_body, par_olist);
12624 : 22405 : new_body = maybe_catch_exception (new_body);
12625 : 22405 : if (gimple_code (stmt) == GIMPLE_OMP_TASK)
12626 : 3783 : gimple_seq_add_stmt (&new_body,
12627 : 3783 : gimple_build_omp_continue (integer_zero_node,
12628 : : integer_zero_node));
12629 : 22405 : gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
12630 : 22405 : gimple_omp_set_body (stmt, new_body);
12631 : :
12632 : 22405 : if (dep_bind && gimple_bind_block (par_bind) == NULL_TREE)
12633 : 493 : bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12634 : : else
12635 : 21912 : bind = gimple_build_bind (NULL, NULL, gimple_bind_block (par_bind));
12636 : 43157 : gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
12637 : 22405 : gimple_bind_add_seq (bind, ilist);
12638 : 22405 : gimple_bind_add_stmt (bind, stmt);
12639 : 22405 : gimple_bind_add_seq (bind, olist);
12640 : :
12641 : 22405 : pop_gimplify_context (NULL);
12642 : :
12643 : 22405 : if (dep_bind)
12644 : : {
12645 : 1653 : gimple_bind_add_seq (dep_bind, dep_ilist);
12646 : 1653 : gimple_bind_add_seq (dep_bind, tskred_ilist);
12647 : 1653 : gimple_bind_add_stmt (dep_bind, bind);
12648 : 1653 : gimple_bind_add_seq (dep_bind, tskred_olist);
12649 : 1653 : gimple_bind_add_seq (dep_bind, dep_olist);
12650 : 1653 : 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 : 35308 : lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
12659 : : {
12660 : 35308 : tree clauses;
12661 : 35308 : tree child_fn, t, c;
12662 : 35308 : gomp_target *stmt = as_a <gomp_target *> (gsi_stmt (*gsi_p));
12663 : 35308 : gbind *tgt_bind, *bind, *dep_bind = NULL;
12664 : 35308 : gimple_seq tgt_body, olist, ilist, fplist, new_body;
12665 : 35308 : location_t loc = gimple_location (stmt);
12666 : 35308 : bool offloaded, data_region;
12667 : 35308 : unsigned int map_cnt = 0;
12668 : 35308 : tree in_reduction_clauses = NULL_TREE;
12669 : :
12670 : 35308 : tree deep_map_cnt = NULL_TREE;
12671 : 35308 : tree deep_map_data = NULL_TREE;
12672 : 35308 : tree deep_map_offset_data = NULL_TREE;
12673 : 35308 : tree deep_map_offset = NULL_TREE;
12674 : :
12675 : 35308 : offloaded = is_gimple_omp_offloaded (stmt);
12676 : 35308 : switch (gimple_omp_target_kind (stmt))
12677 : : {
12678 : 10495 : case GF_OMP_TARGET_KIND_REGION:
12679 : 10495 : tree *p, *q;
12680 : 10495 : q = &in_reduction_clauses;
12681 : 71744 : for (p = gimple_omp_target_clauses_ptr (stmt); *p; )
12682 : 61249 : if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_IN_REDUCTION)
12683 : : {
12684 : 422 : *q = *p;
12685 : 422 : q = &OMP_CLAUSE_CHAIN (*q);
12686 : 422 : *p = OMP_CLAUSE_CHAIN (*p);
12687 : : }
12688 : : else
12689 : 60827 : p = &OMP_CLAUSE_CHAIN (*p);
12690 : 10495 : *q = NULL_TREE;
12691 : 10495 : *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 : 0 : default:
12714 : 0 : 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 : 35308 : if (ENABLE_OFFLOADING && (omp_requires_mask & OMP_REQUIRES_TARGET_USED))
12720 : : g->have_offload = true;
12721 : :
12722 : 35308 : clauses = gimple_omp_target_clauses (stmt);
12723 : :
12724 : 35308 : gimple_seq dep_ilist = NULL;
12725 : 35308 : gimple_seq dep_olist = NULL;
12726 : 35308 : bool has_depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND) != NULL_TREE;
12727 : 35308 : if (has_depend || in_reduction_clauses)
12728 : : {
12729 : 382 : push_gimplify_context ();
12730 : 382 : dep_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
12731 : 382 : if (has_depend)
12732 : 319 : lower_depend_clauses (gimple_omp_target_clauses_ptr (stmt),
12733 : : &dep_ilist, &dep_olist);
12734 : 382 : if (in_reduction_clauses)
12735 : 314 : lower_rec_input_clauses (in_reduction_clauses, &dep_ilist, &dep_olist,
12736 : : ctx, NULL);
12737 : : }
12738 : :
12739 : 35308 : tgt_bind = NULL;
12740 : 35308 : tgt_body = NULL;
12741 : 35308 : if (offloaded)
12742 : : {
12743 : 19880 : tgt_bind = gimple_seq_first_stmt_as_a_bind (gimple_omp_body (stmt));
12744 : 19880 : tgt_body = gimple_bind_body (tgt_bind);
12745 : : }
12746 : 15428 : else if (data_region)
12747 : 4030 : tgt_body = gimple_omp_body (stmt);
12748 : 35308 : child_fn = ctx->cb.dst_fn;
12749 : :
12750 : 35308 : push_gimplify_context ();
12751 : 35308 : fplist = NULL;
12752 : :
12753 : 35308 : ilist = NULL;
12754 : 35308 : olist = NULL;
12755 : 165661 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12756 : 130353 : switch (OMP_CLAUSE_CODE (c))
12757 : : {
12758 : : tree var, x;
12759 : :
12760 : : default:
12761 : : break;
12762 : 72702 : case OMP_CLAUSE_MAP:
12763 : : #if CHECKING_P
12764 : : /* First check what we're prepared to handle in the following. */
12765 : 72702 : 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 : 2577 : case GOMP_MAP_IF_PRESENT:
12794 : 2577 : case GOMP_MAP_FORCE_ALLOC:
12795 : 2577 : case GOMP_MAP_FORCE_TO:
12796 : 2577 : case GOMP_MAP_FORCE_FROM:
12797 : 2577 : case GOMP_MAP_FORCE_TOFROM:
12798 : 2577 : case GOMP_MAP_FORCE_DEVICEPTR:
12799 : 2577 : case GOMP_MAP_DEVICE_RESIDENT:
12800 : 2577 : case GOMP_MAP_LINK:
12801 : 2577 : case GOMP_MAP_FORCE_DETACH:
12802 : 2577 : gcc_assert (is_gimple_omp_oacc (stmt));
12803 : : break;
12804 : 0 : default:
12805 : 0 : gcc_unreachable ();
12806 : : }
12807 : : #endif
12808 : : /* FALLTHRU */
12809 : 84106 : case OMP_CLAUSE_TO:
12810 : 84106 : case OMP_CLAUSE_FROM:
12811 : 84106 : oacc_firstprivate:
12812 : 84106 : var = OMP_CLAUSE_DECL (c);
12813 : 84106 : {
12814 : 84106 : tree extra = lang_hooks.decls.omp_deep_mapping_cnt (stmt, c, &ilist);
12815 : 84106 : if (extra != NULL_TREE && deep_map_cnt != NULL_TREE)
12816 : 0 : deep_map_cnt = fold_build2_loc (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12817 : : size_type_node, deep_map_cnt,
12818 : : extra);
12819 : 84106 : else if (extra != NULL_TREE)
12820 : 0 : deep_map_cnt = extra;
12821 : : }
12822 : :
12823 : 84106 : if (!DECL_P (var))
12824 : : {
12825 : 33653 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12826 : 33653 : || (!OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
12827 : 32201 : && (OMP_CLAUSE_MAP_KIND (c)
12828 : : != GOMP_MAP_FIRSTPRIVATE_POINTER)))
12829 : 33653 : map_cnt++;
12830 : 33653 : continue;
12831 : : }
12832 : :
12833 : 50453 : if (DECL_SIZE (var)
12834 : 50453 : && !poly_int_tree_p (DECL_SIZE (var)))
12835 : : {
12836 : 717 : tree var2 = DECL_VALUE_EXPR (var);
12837 : 717 : gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
12838 : 717 : var2 = TREE_OPERAND (var2, 0);
12839 : 717 : gcc_assert (DECL_P (var2));
12840 : : var = var2;
12841 : : }
12842 : :
12843 : 50453 : if (offloaded
12844 : 35039 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12845 : 81602 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
12846 : 28026 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
12847 : : {
12848 : 3522 : if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
12849 : : {
12850 : 344 : if (is_global_var (maybe_lookup_decl_in_outer_ctx (var, ctx))
12851 : 344 : && varpool_node::get_create (var)->offloadable)
12852 : 2 : continue;
12853 : :
12854 : 342 : tree type = build_pointer_type (TREE_TYPE (var));
12855 : 342 : tree new_var = lookup_decl (var, ctx);
12856 : 342 : x = create_tmp_var_raw (type, get_name (new_var));
12857 : 342 : gimple_add_tmp_var (x);
12858 : 342 : x = build_simple_mem_ref (x);
12859 : 342 : SET_DECL_VALUE_EXPR (new_var, x);
12860 : 342 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12861 : : }
12862 : 3520 : continue;
12863 : 3520 : }
12864 : :
12865 : 46931 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12866 : 36979 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
12867 : 36792 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
12868 : 47161 : && is_omp_target (stmt))
12869 : : {
12870 : 208 : gcc_assert (maybe_lookup_field (c, ctx));
12871 : 208 : map_cnt++;
12872 : 208 : continue;
12873 : : }
12874 : :
12875 : 46723 : if (!maybe_lookup_field (var, ctx))
12876 : 5603 : continue;
12877 : :
12878 : : /* Don't remap compute constructs' reduction variables, because the
12879 : : intermediate result must be local to each gang. */
12880 : 67488 : if (offloaded && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12881 : 22478 : && is_gimple_omp_oacc (ctx->stmt)
12882 : 13585 : && OMP_CLAUSE_MAP_IN_REDUCTION (c)))
12883 : : {
12884 : 25588 : x = build_receiver_ref (var, true, ctx);
12885 : 25588 : tree new_var = lookup_decl (var, ctx);
12886 : :
12887 : 25588 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12888 : 21698 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12889 : 2921 : && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
12890 : 28509 : && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
12891 : 425 : x = build_simple_mem_ref (x);
12892 : 25588 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
12893 : : {
12894 : 3890 : gcc_assert (is_gimple_omp_oacc (ctx->stmt));
12895 : 3890 : if (omp_privatize_by_reference (new_var)
12896 : 3890 : && (TREE_CODE (TREE_TYPE (new_var)) != POINTER_TYPE
12897 : 44 : || DECL_BY_REFERENCE (var)))
12898 : : {
12899 : : /* Create a local object to hold the instance
12900 : : value. */
12901 : 455 : tree type = TREE_TYPE (TREE_TYPE (new_var));
12902 : 455 : const char *id = IDENTIFIER_POINTER (DECL_NAME (new_var));
12903 : 455 : tree inst = create_tmp_var (type, id);
12904 : 455 : gimplify_assign (inst, fold_indirect_ref (x), &fplist);
12905 : 455 : x = build_fold_addr_expr (inst);
12906 : : }
12907 : 3890 : gimplify_assign (new_var, x, &fplist);
12908 : : }
12909 : 21698 : else if (DECL_P (new_var))
12910 : : {
12911 : 21698 : SET_DECL_VALUE_EXPR (new_var, x);
12912 : 21698 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12913 : : }
12914 : : else
12915 : 0 : gcc_unreachable ();
12916 : : }
12917 : 41120 : map_cnt++;
12918 : 41120 : break;
12919 : :
12920 : 14708 : case OMP_CLAUSE_FIRSTPRIVATE:
12921 : 14708 : omp_firstprivate_recv:
12922 : 14708 : gcc_checking_assert (offloaded);
12923 : 14708 : if (is_gimple_omp_oacc (ctx->stmt))
12924 : : {
12925 : : /* No 'firstprivate' clauses on OpenACC 'kernels'. */
12926 : 3890 : gcc_checking_assert (!is_oacc_kernels (ctx));
12927 : : /* Likewise, on OpenACC 'kernels' decomposed parts. */
12928 : 3890 : gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
12929 : :
12930 : 3890 : goto oacc_firstprivate;
12931 : : }
12932 : 10818 : map_cnt++;
12933 : 10818 : var = OMP_CLAUSE_DECL (c);
12934 : 10818 : if (!omp_privatize_by_reference (var)
12935 : 10818 : && !is_gimple_reg_type (TREE_TYPE (var)))
12936 : : {
12937 : 99 : tree new_var = lookup_decl (var, ctx);
12938 : 99 : if (is_variable_sized (var))
12939 : : {
12940 : 4 : tree pvar = DECL_VALUE_EXPR (var);
12941 : 4 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
12942 : 4 : pvar = TREE_OPERAND (pvar, 0);
12943 : 4 : gcc_assert (DECL_P (pvar));
12944 : 4 : tree new_pvar = lookup_decl (pvar, ctx);
12945 : 4 : x = build_fold_indirect_ref (new_pvar);
12946 : 4 : TREE_THIS_NOTRAP (x) = 1;
12947 : : }
12948 : : else
12949 : 95 : x = build_receiver_ref (var, true, ctx);
12950 : 99 : SET_DECL_VALUE_EXPR (new_var, x);
12951 : 99 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12952 : : }
12953 : : /* Fortran array descriptors: firstprivate of data + attach. */
12954 : 10818 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
12955 : 10818 : && lang_hooks.decls.omp_array_data (var, true))
12956 : 18 : map_cnt += 2;
12957 : : break;
12958 : :
12959 : 172 : case OMP_CLAUSE_PRIVATE:
12960 : 172 : gcc_checking_assert (offloaded);
12961 : 172 : if (is_gimple_omp_oacc (ctx->stmt))
12962 : : {
12963 : : /* No 'private' clauses on OpenACC 'kernels'. */
12964 : 79 : gcc_checking_assert (!is_oacc_kernels (ctx));
12965 : : /* Likewise, on OpenACC 'kernels' decomposed parts. */
12966 : 79 : gcc_checking_assert (!is_oacc_kernels_decomposed_part (ctx));
12967 : :
12968 : : break;
12969 : : }
12970 : 93 : var = OMP_CLAUSE_DECL (c);
12971 : 93 : if (is_variable_sized (var))
12972 : : {
12973 : 1 : tree new_var = lookup_decl (var, ctx);
12974 : 1 : tree pvar = DECL_VALUE_EXPR (var);
12975 : 1 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
12976 : 1 : pvar = TREE_OPERAND (pvar, 0);
12977 : 1 : gcc_assert (DECL_P (pvar));
12978 : 1 : tree new_pvar = lookup_decl (pvar, ctx);
12979 : 1 : x = build_fold_indirect_ref (new_pvar);
12980 : 1 : TREE_THIS_NOTRAP (x) = 1;
12981 : 1 : SET_DECL_VALUE_EXPR (new_var, x);
12982 : 1 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
12983 : : }
12984 : : break;
12985 : :
12986 : 2397 : case OMP_CLAUSE_USE_DEVICE_PTR:
12987 : 2397 : case OMP_CLAUSE_USE_DEVICE_ADDR:
12988 : 2397 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
12989 : 2397 : case OMP_CLAUSE_IS_DEVICE_PTR:
12990 : 2397 : var = OMP_CLAUSE_DECL (c);
12991 : 2397 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
12992 : : {
12993 : 238 : while (TREE_CODE (var) == INDIRECT_REF
12994 : 238 : || TREE_CODE (var) == ARRAY_REF)
12995 : 17 : var = TREE_OPERAND (var, 0);
12996 : 221 : if (lang_hooks.decls.omp_array_data (var, true))
12997 : 13 : goto omp_firstprivate_recv;
12998 : : }
12999 : 2384 : map_cnt++;
13000 : 2384 : if (is_variable_sized (var))
13001 : : {
13002 : 12 : tree new_var = lookup_decl (var, ctx);
13003 : 12 : tree pvar = DECL_VALUE_EXPR (var);
13004 : 12 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
13005 : 12 : pvar = TREE_OPERAND (pvar, 0);
13006 : 12 : gcc_assert (DECL_P (pvar));
13007 : 12 : tree new_pvar = lookup_decl (pvar, ctx);
13008 : 12 : x = build_fold_indirect_ref (new_pvar);
13009 : 12 : TREE_THIS_NOTRAP (x) = 1;
13010 : 12 : SET_DECL_VALUE_EXPR (new_var, x);
13011 : 12 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13012 : : }
13013 : 2372 : else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
13014 : 515 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13015 : 2063 : && !omp_privatize_by_reference (var)
13016 : 695 : && !omp_is_allocatable_or_ptr (var)
13017 : 556 : && !lang_hooks.decls.omp_array_data (var, true))
13018 : 4387 : || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
13019 : : {
13020 : 381 : tree new_var = lookup_decl (var, ctx);
13021 : 381 : tree type = build_pointer_type (TREE_TYPE (var));
13022 : 381 : x = create_tmp_var_raw (type, get_name (new_var));
13023 : 381 : gimple_add_tmp_var (x);
13024 : 381 : x = build_simple_mem_ref (x);
13025 : 381 : SET_DECL_VALUE_EXPR (new_var, x);
13026 : 381 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13027 : : }
13028 : : else
13029 : : {
13030 : 1991 : tree new_var = lookup_decl (var, ctx);
13031 : 1991 : x = create_tmp_var_raw (TREE_TYPE (new_var), get_name (new_var));
13032 : 1991 : gimple_add_tmp_var (x);
13033 : 1991 : SET_DECL_VALUE_EXPR (new_var, x);
13034 : 1991 : DECL_HAS_VALUE_EXPR_P (new_var) = 1;
13035 : : }
13036 : : break;
13037 : : }
13038 : :
13039 : 35308 : if (offloaded)
13040 : : {
13041 : 19880 : target_nesting_level++;
13042 : 19880 : lower_omp (&tgt_body, ctx);
13043 : 19880 : target_nesting_level--;
13044 : : }
13045 : 15428 : else if (data_region)
13046 : 4030 : lower_omp (&tgt_body, ctx);
13047 : :
13048 : 35308 : if (offloaded)
13049 : : {
13050 : : /* Declare all the variables created by mapping and the variables
13051 : : declared in the scope of the target body. */
13052 : 19880 : record_vars_into (ctx->block_vars, child_fn);
13053 : 19880 : maybe_remove_omp_member_access_dummy_vars (tgt_bind);
13054 : 19880 : record_vars_into (gimple_bind_vars (tgt_bind), child_fn);
13055 : : }
13056 : :
13057 : 35308 : if (ctx->record_type)
13058 : : {
13059 : 30521 : 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 : 0 : gcc_unreachable ();
13063 : 0 : else if (deep_map_cnt)
13064 : : {
13065 : 0 : gcc_assert (flexible_array_type_p (ctx->record_type));
13066 : 0 : tree n = create_tmp_var_raw (size_type_node, "nn_map");
13067 : 0 : gimple_add_tmp_var (n);
13068 : 0 : gimplify_assign (n, deep_map_cnt, &ilist);
13069 : 0 : deep_map_cnt = n;
13070 : : }
13071 : 0 : ctx->sender_decl
13072 : 30521 : = create_tmp_var (deep_map_cnt ? build_pointer_type (ctx->record_type)
13073 : : : ctx->record_type, ".omp_data_arr");
13074 : 30521 : DECL_NAMELESS (ctx->sender_decl) = 1;
13075 : 30521 : TREE_ADDRESSABLE (ctx->sender_decl) = 1;
13076 : 61042 : t = make_tree_vec (deep_map_cnt ? 4 : 3);
13077 : 30521 : TREE_VEC_ELT (t, 0) = ctx->sender_decl;
13078 : 30521 : TREE_VEC_ELT (t, 1)
13079 : 61042 : = create_tmp_var (deep_map_cnt
13080 : 0 : ? build_pointer_type (size_type_node)
13081 : 30521 : : build_array_type_nelts (size_type_node, map_cnt),
13082 : : ".omp_data_sizes");
13083 : 30521 : DECL_NAMELESS (TREE_VEC_ELT (t, 1)) = 1;
13084 : 30521 : TREE_ADDRESSABLE (TREE_VEC_ELT (t, 1)) = 1;
13085 : 30521 : TREE_STATIC (TREE_VEC_ELT (t, 1)) = 1;
13086 : 30521 : tree tkind_type = short_unsigned_type_node;
13087 : 30521 : int talign_shift = 8;
13088 : 30521 : TREE_VEC_ELT (t, 2)
13089 : 61042 : = create_tmp_var (deep_map_cnt
13090 : 0 : ? build_pointer_type (tkind_type)
13091 : 30521 : : build_array_type_nelts (tkind_type, map_cnt),
13092 : : ".omp_data_kinds");
13093 : 30521 : DECL_NAMELESS (TREE_VEC_ELT (t, 2)) = 1;
13094 : 30521 : TREE_ADDRESSABLE (TREE_VEC_ELT (t, 2)) = 1;
13095 : 30521 : TREE_STATIC (TREE_VEC_ELT (t, 2)) = 1;
13096 : 30521 : gimple_omp_target_set_data_arg (stmt, t);
13097 : :
13098 : 30521 : if (deep_map_cnt)
13099 : : {
13100 : 0 : tree tmp, size;
13101 : 0 : size = create_tmp_var (size_type_node, NULL);
13102 : 0 : DECL_NAMELESS (size) = 1;
13103 : 0 : gimplify_assign (size,
13104 : : fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
13105 : : size_type_node, deep_map_cnt,
13106 : 0 : build_int_cst (size_type_node,
13107 : : map_cnt)), &ilist);
13108 : 0 : TREE_VEC_ELT (t, 3) = size;
13109 : :
13110 : 0 : tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
13111 : 0 : size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13112 : : size_type_node, deep_map_cnt,
13113 : 0 : TYPE_SIZE_UNIT (ptr_type_node));
13114 : 0 : size = fold_build2_loc (UNKNOWN_LOCATION, PLUS_EXPR,
13115 : : size_type_node, size,
13116 : 0 : TYPE_SIZE_UNIT (ctx->record_type));
13117 : 0 : tmp = build_call_expr_loc (input_location, call, 1, size);
13118 : 0 : gimplify_assign (ctx->sender_decl, tmp, &ilist);
13119 : :
13120 : 0 : size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13121 : 0 : size_type_node, TREE_VEC_ELT (t, 3),
13122 : 0 : TYPE_SIZE_UNIT (size_type_node));
13123 : 0 : tmp = build_call_expr_loc (input_location, call, 1, size);
13124 : 0 : gimplify_assign (TREE_VEC_ELT (t, 1), tmp, &ilist);
13125 : :
13126 : 0 : size = fold_build2_loc (UNKNOWN_LOCATION, MULT_EXPR,
13127 : 0 : size_type_node, TREE_VEC_ELT (t, 3),
13128 : 0 : TYPE_SIZE_UNIT (tkind_type));
13129 : 0 : tmp = build_call_expr_loc (input_location, call, 1, size);
13130 : 0 : gimplify_assign (TREE_VEC_ELT (t, 2), tmp, &ilist);
13131 : 0 : tree field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (ctx->sender_decl)));
13132 : 0 : for ( ; DECL_CHAIN (field) != NULL_TREE; field = DECL_CHAIN (field))
13133 : : ;
13134 : 0 : gcc_assert (TREE_CODE (TREE_TYPE (field)));
13135 : 0 : tmp = build_fold_indirect_ref (ctx->sender_decl);
13136 : 0 : deep_map_data = omp_build_component_ref (tmp, field);
13137 : 0 : deep_map_offset_data = create_tmp_var_raw (size_type_node,
13138 : : "map_offset_data");
13139 : 0 : deep_map_offset = create_tmp_var_raw (size_type_node, "map_offset");
13140 : 0 : gimple_add_tmp_var (deep_map_offset_data);
13141 : 0 : gimple_add_tmp_var (deep_map_offset);
13142 : 0 : gimplify_assign (deep_map_offset_data, build_int_cst (size_type_node,
13143 : 0 : 0), &ilist);
13144 : 0 : gimplify_assign (deep_map_offset, build_int_cst (size_type_node,
13145 : : map_cnt), &ilist);
13146 : : }
13147 : :
13148 : 30521 : vec<constructor_elt, va_gc> *vsize;
13149 : 30521 : vec<constructor_elt, va_gc> *vkind;
13150 : 30521 : vec_alloc (vsize, map_cnt);
13151 : 30521 : vec_alloc (vkind, map_cnt);
13152 : 30521 : unsigned int map_idx = 0;
13153 : :
13154 : 151748 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13155 : 121227 : switch (OMP_CLAUSE_CODE (c))
13156 : : {
13157 : : tree ovar, nc, s, purpose, var, x, type;
13158 : : unsigned int talign;
13159 : :
13160 : : default:
13161 : 117280 : break;
13162 : :
13163 : 82618 : case OMP_CLAUSE_MAP:
13164 : 82618 : case OMP_CLAUSE_TO:
13165 : 82618 : case OMP_CLAUSE_FROM:
13166 : 82618 : oacc_firstprivate_map:
13167 : 82618 : nc = c;
13168 : 82618 : ovar = OMP_CLAUSE_DECL (c);
13169 : 82618 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13170 : 82618 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
13171 : 67923 : || (OMP_CLAUSE_MAP_KIND (c)
13172 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
13173 : : break;
13174 : 78928 : if (deep_map_cnt)
13175 : : {
13176 : 0 : unsigned HOST_WIDE_INT tkind2;
13177 : 0 : switch (OMP_CLAUSE_CODE (c))
13178 : : {
13179 : 0 : 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 : 0 : case OMP_CLAUSE_FROM: tkind2 = GOMP_MAP_FROM; break;
13183 : 0 : default: gcc_unreachable ();
13184 : : }
13185 : 0 : lang_hooks.decls.omp_deep_mapping (stmt, c, tkind2,
13186 : : deep_map_data,
13187 : 0 : TREE_VEC_ELT (t, 1),
13188 : 0 : TREE_VEC_ELT (t, 2),
13189 : : deep_map_offset_data,
13190 : : deep_map_offset, &ilist);
13191 : : }
13192 : 78928 : if (!DECL_P (ovar))
13193 : : {
13194 : 33653 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13195 : 33653 : && OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c))
13196 : : {
13197 : 0 : nc = OMP_CLAUSE_CHAIN (c);
13198 : 0 : gcc_checking_assert (OMP_CLAUSE_DECL (nc)
13199 : : == get_base_address (ovar));
13200 : 0 : ovar = OMP_CLAUSE_DECL (nc);
13201 : : }
13202 : : else
13203 : : {
13204 : 33653 : tree x = build_sender_ref (ovar, ctx);
13205 : 33653 : tree v = ovar;
13206 : 33653 : if (in_reduction_clauses
13207 : 163 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13208 : 33816 : && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13209 : : {
13210 : 104 : v = unshare_expr (v);
13211 : 104 : tree *p = &v;
13212 : 104 : while (handled_component_p (*p)
13213 : : || TREE_CODE (*p) == INDIRECT_REF
13214 : : || TREE_CODE (*p) == ADDR_EXPR
13215 : : || TREE_CODE (*p) == MEM_REF
13216 : 256 : || TREE_CODE (*p) == NON_LVALUE_EXPR)
13217 : 152 : p = &TREE_OPERAND (*p, 0);
13218 : 104 : tree d = *p;
13219 : 104 : if (is_variable_sized (d))
13220 : : {
13221 : 16 : gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
13222 : 16 : d = DECL_VALUE_EXPR (d);
13223 : 16 : gcc_assert (TREE_CODE (d) == INDIRECT_REF);
13224 : 16 : d = TREE_OPERAND (d, 0);
13225 : 16 : gcc_assert (DECL_P (d));
13226 : : }
13227 : 104 : splay_tree_key key
13228 : 104 : = (splay_tree_key) &DECL_CONTEXT (d);
13229 : 104 : tree nd = (tree) splay_tree_lookup (ctx->field_map,
13230 : 104 : key)->value;
13231 : 104 : if (d == *p)
13232 : 88 : *p = nd;
13233 : : else
13234 : 16 : *p = build_fold_indirect_ref (nd);
13235 : : }
13236 : 33653 : v = build_fold_addr_expr_with_type (v, ptr_type_node);
13237 : 33653 : gimplify_assign (x, v, &ilist);
13238 : 33653 : nc = NULL_TREE;
13239 : : }
13240 : : }
13241 : : else
13242 : : {
13243 : 45275 : if (DECL_SIZE (ovar)
13244 : 45275 : && !poly_int_tree_p (DECL_SIZE (ovar)))
13245 : : {
13246 : 7 : tree ovar2 = DECL_VALUE_EXPR (ovar);
13247 : 7 : gcc_assert (TREE_CODE (ovar2) == INDIRECT_REF);
13248 : 7 : ovar2 = TREE_OPERAND (ovar2, 0);
13249 : 7 : gcc_assert (DECL_P (ovar2));
13250 : : ovar = ovar2;
13251 : : }
13252 : 45275 : if (!maybe_lookup_field (ovar, ctx)
13253 : 49338 : && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13254 : 4063 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13255 : 3982 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)))
13256 : 3947 : continue;
13257 : : }
13258 : :
13259 : 74981 : talign = TYPE_ALIGN_UNIT (TREE_TYPE (ovar));
13260 : 74981 : if (DECL_P (ovar) && DECL_ALIGN_UNIT (ovar) > talign)
13261 : 6288 : talign = DECL_ALIGN_UNIT (ovar);
13262 : :
13263 : 74981 : var = NULL_TREE;
13264 : 74981 : if (nc)
13265 : : {
13266 : 41328 : if (in_reduction_clauses
13267 : 897 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13268 : 42225 : && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13269 : : {
13270 : 268 : tree d = ovar;
13271 : 268 : if (is_variable_sized (d))
13272 : : {
13273 : 0 : gcc_assert (DECL_HAS_VALUE_EXPR_P (d));
13274 : 0 : d = DECL_VALUE_EXPR (d);
13275 : 0 : gcc_assert (TREE_CODE (d) == INDIRECT_REF);
13276 : 0 : d = TREE_OPERAND (d, 0);
13277 : 0 : gcc_assert (DECL_P (d));
13278 : : }
13279 : 268 : splay_tree_key key
13280 : 268 : = (splay_tree_key) &DECL_CONTEXT (d);
13281 : 268 : tree nd = (tree) splay_tree_lookup (ctx->field_map,
13282 : 268 : key)->value;
13283 : 268 : if (d == ovar)
13284 : : var = nd;
13285 : : else
13286 : 0 : var = build_fold_indirect_ref (nd);
13287 : : }
13288 : : else
13289 : 41060 : var = lookup_decl_in_outer_ctx (ovar, ctx);
13290 : : }
13291 : 41328 : if (nc
13292 : 41328 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13293 : 31376 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13294 : 31189 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13295 : 230 : && is_omp_target (stmt))
13296 : : {
13297 : 208 : x = build_sender_ref (c, ctx);
13298 : 208 : gimplify_assign (x, build_fold_addr_expr (var), &ilist);
13299 : : }
13300 : 74773 : else if (nc)
13301 : : {
13302 : 41120 : x = build_sender_ref (ovar, ctx);
13303 : :
13304 : 41120 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13305 : 31168 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
13306 : 5050 : && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)
13307 : 46170 : && TREE_CODE (TREE_TYPE (ovar)) == ARRAY_TYPE)
13308 : : {
13309 : 425 : gcc_assert (offloaded);
13310 : 425 : tree avar
13311 : 425 : = create_tmp_var (TREE_TYPE (TREE_TYPE (x)));
13312 : 425 : mark_addressable (avar);
13313 : 425 : gimplify_assign (avar, build_fold_addr_expr (var), &ilist);
13314 : 425 : talign = DECL_ALIGN_UNIT (avar);
13315 : 425 : avar = build_fold_addr_expr (avar);
13316 : 425 : gimplify_assign (x, avar, &ilist);
13317 : : }
13318 : 40695 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
13319 : : {
13320 : 3890 : gcc_assert (is_gimple_omp_oacc (ctx->stmt));
13321 : 3890 : if (!omp_privatize_by_reference (var))
13322 : : {
13323 : 3435 : if (is_gimple_reg (var)
13324 : 3435 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13325 : 2577 : suppress_warning (var);
13326 : 3435 : var = build_fold_addr_expr (var);
13327 : : }
13328 : : else
13329 : 455 : talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13330 : 3890 : gimplify_assign (x, var, &ilist);
13331 : : }
13332 : 36805 : else if (is_gimple_reg (var))
13333 : : {
13334 : 5090 : gcc_assert (offloaded);
13335 : 5090 : tree avar = create_tmp_var (TREE_TYPE (var));
13336 : 5090 : mark_addressable (avar);
13337 : 5090 : enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (c);
13338 : 5090 : if (GOMP_MAP_COPY_TO_P (map_kind)
13339 : : || map_kind == GOMP_MAP_POINTER
13340 : 473 : || map_kind == GOMP_MAP_TO_PSET
13341 : 2 : || 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 : 5088 : suppress_warning (var, OPT_Wuninitialized);
13350 : 5088 : gimplify_assign (avar, var, &ilist);
13351 : : }
13352 : 5090 : avar = build_fold_addr_expr (avar);
13353 : 5090 : gimplify_assign (x, avar, &ilist);
13354 : 5090 : if ((GOMP_MAP_COPY_FROM_P (map_kind)
13355 : 1297 : || map_kind == GOMP_MAP_FORCE_DEVICEPTR)
13356 : 8883 : && !TYPE_READONLY (TREE_TYPE (var)))
13357 : : {
13358 : 3793 : x = unshare_expr (x);
13359 : 3793 : x = build_simple_mem_ref (x);
13360 : 3793 : 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 : 31715 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM
13368 : 26594 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO)
13369 : 32656 : && (omp_is_allocatable_or_ptr (var)
13370 : 120 : && omp_check_optional_argument (var, false)))
13371 : 0 : var = build_fold_indirect_ref (var);
13372 : 31715 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FROM
13373 : 26594 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TO)
13374 : 32656 : || (!omp_is_allocatable_or_ptr (var)
13375 : 5942 : && !omp_check_optional_argument (var, false)))
13376 : 31595 : var = build_fold_addr_expr (var);
13377 : 31715 : gimplify_assign (x, var, &ilist);
13378 : : }
13379 : : }
13380 : 74981 : s = NULL_TREE;
13381 : 74981 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
13382 : : {
13383 : 3890 : gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
13384 : 3890 : s = TREE_TYPE (ovar);
13385 : 3890 : if (TREE_CODE (s) == REFERENCE_TYPE
13386 : 3890 : || omp_check_optional_argument (ovar, false))
13387 : 449 : s = TREE_TYPE (s);
13388 : 3890 : s = TYPE_SIZE_UNIT (s);
13389 : : }
13390 : : else
13391 : 71091 : s = OMP_CLAUSE_SIZE (c);
13392 : 74981 : if (s == NULL_TREE)
13393 : 123 : s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
13394 : 74981 : s = fold_convert (size_type_node, s);
13395 : 74981 : purpose = size_int (map_idx++);
13396 : 74981 : CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13397 : 74981 : if (TREE_CODE (s) != INTEGER_CST)
13398 : 15067 : TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13399 : :
13400 : 74981 : unsigned HOST_WIDE_INT tkind, tkind_zero;
13401 : 74981 : switch (OMP_CLAUSE_CODE (c))
13402 : : {
13403 : 63577 : case OMP_CLAUSE_MAP:
13404 : 63577 : tkind = OMP_CLAUSE_MAP_KIND (c);
13405 : 63577 : tkind_zero = tkind;
13406 : 63577 : if (OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c))
13407 : 4988 : 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 : 63577 : tkind_zero = GOMP_MAP_ZERO_LEN_ARRAY_SECTION;
13426 : : break;
13427 : 20 : case GOMP_MAP_DELETE:
13428 : 20 : tkind_zero = GOMP_MAP_DELETE_ZERO_LEN_ARRAY_SECTION;
13429 : : default:
13430 : : break;
13431 : : }
13432 : 63577 : if (tkind_zero != tkind)
13433 : : {
13434 : 4988 : if (integer_zerop (s))
13435 : : tkind = tkind_zero;
13436 : 4271 : else if (integer_nonzerop (s))
13437 : 63577 : tkind_zero = tkind;
13438 : : }
13439 : 63577 : if (tkind_zero == tkind
13440 : 62221 : && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (c)
13441 : 63577 : && (((tkind & GOMP_MAP_FLAG_SPECIAL_BITS)
13442 : 4778 : & ~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 : 4739 : tkind |= GOMP_MAP_IMPLICIT;
13450 : 4739 : tkind_zero = tkind;
13451 : : }
13452 : : break;
13453 : 3890 : case OMP_CLAUSE_FIRSTPRIVATE:
13454 : 3890 : gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
13455 : : tkind = GOMP_MAP_TO;
13456 : : tkind_zero = tkind;
13457 : : break;
13458 : 1560 : case OMP_CLAUSE_TO:
13459 : 1560 : tkind
13460 : 1560 : = (OMP_CLAUSE_MOTION_PRESENT (c)
13461 : 1560 : ? GOMP_MAP_ALWAYS_PRESENT_TO : GOMP_MAP_TO);
13462 : : tkind_zero = tkind;
13463 : : break;
13464 : 5954 : case OMP_CLAUSE_FROM:
13465 : 5954 : tkind
13466 : 5954 : = (OMP_CLAUSE_MOTION_PRESENT (c)
13467 : 5954 : ? GOMP_MAP_ALWAYS_PRESENT_FROM : GOMP_MAP_FROM);
13468 : : tkind_zero = tkind;
13469 : : break;
13470 : 0 : default:
13471 : 0 : gcc_unreachable ();
13472 : : }
13473 : 63577 : gcc_checking_assert (tkind
13474 : : < (HOST_WIDE_INT_C (1U) << talign_shift));
13475 : 74981 : gcc_checking_assert (tkind_zero
13476 : : < (HOST_WIDE_INT_C (1U) << talign_shift));
13477 : 74981 : talign = ceil_log2 (talign);
13478 : 74981 : tkind |= talign << talign_shift;
13479 : 74981 : tkind_zero |= talign << talign_shift;
13480 : 74981 : gcc_checking_assert (tkind
13481 : : <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13482 : 74981 : gcc_checking_assert (tkind_zero
13483 : : <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13484 : 74981 : if (tkind == tkind_zero)
13485 : 73625 : x = build_int_cstu (tkind_type, tkind);
13486 : : else
13487 : : {
13488 : 1356 : TREE_STATIC (TREE_VEC_ELT (t, 2)) = 0;
13489 : 1356 : x = build3 (COND_EXPR, tkind_type,
13490 : : fold_build2 (EQ_EXPR, boolean_type_node,
13491 : : unshare_expr (s), size_zero_node),
13492 : 1356 : build_int_cstu (tkind_type, tkind_zero),
13493 : 1356 : build_int_cstu (tkind_type, tkind));
13494 : : }
13495 : 74981 : CONSTRUCTOR_APPEND_ELT (vkind, purpose, x);
13496 : 74981 : if (nc && nc != c)
13497 : 0 : c = nc;
13498 : : break;
13499 : :
13500 : 14708 : case OMP_CLAUSE_FIRSTPRIVATE:
13501 : 14708 : omp_has_device_addr_descr:
13502 : 14708 : if (is_gimple_omp_oacc (ctx->stmt))
13503 : 3890 : goto oacc_firstprivate_map;
13504 : 10818 : ovar = OMP_CLAUSE_DECL (c);
13505 : 10818 : if (omp_privatize_by_reference (ovar))
13506 : 788 : talign = TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13507 : : else
13508 : 10030 : talign = DECL_ALIGN_UNIT (ovar);
13509 : 10818 : var = lookup_decl_in_outer_ctx (ovar, ctx);
13510 : 10818 : x = build_sender_ref (ovar, ctx);
13511 : 10818 : tkind = GOMP_MAP_FIRSTPRIVATE;
13512 : 10818 : type = TREE_TYPE (ovar);
13513 : 10818 : if (omp_privatize_by_reference (ovar))
13514 : 788 : type = TREE_TYPE (type);
13515 : 10818 : if ((INTEGRAL_TYPE_P (type)
13516 : 10502 : && TYPE_PRECISION (type) <= POINTER_SIZE)
13517 : 10828 : || TREE_CODE (type) == POINTER_TYPE)
13518 : : {
13519 : 10633 : tkind = GOMP_MAP_FIRSTPRIVATE_INT;
13520 : 10633 : tree t = var;
13521 : 10633 : if (omp_privatize_by_reference (var))
13522 : 761 : t = build_simple_mem_ref (var);
13523 : 9872 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13524 : 8880 : suppress_warning (var);
13525 : 10633 : if (TREE_CODE (type) != POINTER_TYPE)
13526 : 10492 : t = fold_convert (pointer_sized_int_node, t);
13527 : 10633 : t = fold_convert (TREE_TYPE (x), t);
13528 : 10633 : gimplify_assign (x, t, &ilist);
13529 : : }
13530 : 185 : else if (omp_privatize_by_reference (var))
13531 : 27 : gimplify_assign (x, var, &ilist);
13532 : 158 : else if (is_gimple_reg (var))
13533 : : {
13534 : 47 : tree avar = create_tmp_var (TREE_TYPE (var));
13535 : 47 : mark_addressable (avar);
13536 : 47 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13537 : 40 : suppress_warning (var);
13538 : 47 : gimplify_assign (avar, var, &ilist);
13539 : 47 : avar = build_fold_addr_expr (avar);
13540 : 47 : gimplify_assign (x, avar, &ilist);
13541 : : }
13542 : : else
13543 : : {
13544 : 111 : var = build_fold_addr_expr (var);
13545 : 111 : gimplify_assign (x, var, &ilist);
13546 : : }
13547 : 185 : if (tkind == GOMP_MAP_FIRSTPRIVATE_INT)
13548 : 10633 : s = size_int (0);
13549 : 185 : else if (omp_privatize_by_reference (ovar))
13550 : 27 : s = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ovar)));
13551 : : else
13552 : 158 : s = TYPE_SIZE_UNIT (TREE_TYPE (ovar));
13553 : 10818 : s = fold_convert (size_type_node, s);
13554 : 10818 : purpose = size_int (map_idx++);
13555 : 10818 : CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13556 : 10818 : if (TREE_CODE (s) != INTEGER_CST)
13557 : 8 : TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13558 : :
13559 : 10818 : gcc_checking_assert (tkind
13560 : : < (HOST_WIDE_INT_C (1U) << talign_shift));
13561 : 10818 : talign = ceil_log2 (talign);
13562 : 10818 : tkind |= talign << talign_shift;
13563 : 10818 : gcc_checking_assert (tkind
13564 : : <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13565 : 10818 : CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13566 : : build_int_cstu (tkind_type, tkind));
13567 : : /* Fortran array descriptors: firstprivate of data + attach. */
13568 : 10818 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
13569 : 10818 : && lang_hooks.decls.omp_array_data (ovar, true))
13570 : : {
13571 : 18 : tree not_null_lb, null_lb, after_lb;
13572 : 18 : tree var1, var2, size1, size2;
13573 : 18 : tree present = omp_check_optional_argument (ovar, true);
13574 : 18 : if (present)
13575 : : {
13576 : 1 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
13577 : 1 : not_null_lb = create_artificial_label (clause_loc);
13578 : 1 : null_lb = create_artificial_label (clause_loc);
13579 : 1 : after_lb = create_artificial_label (clause_loc);
13580 : 1 : gimple_seq seq = NULL;
13581 : 1 : present = force_gimple_operand (present, &seq, true,
13582 : : NULL_TREE);
13583 : 1 : gimple_seq_add_seq (&ilist, seq);
13584 : 1 : gimple_seq_add_stmt (&ilist,
13585 : 1 : gimple_build_cond_from_tree (present,
13586 : : not_null_lb, null_lb));
13587 : 1 : gimple_seq_add_stmt (&ilist,
13588 : 1 : gimple_build_label (not_null_lb));
13589 : : }
13590 : 18 : var1 = lang_hooks.decls.omp_array_data (var, false);
13591 : 18 : size1 = lang_hooks.decls.omp_array_size (var, &ilist);
13592 : 18 : var2 = build_fold_addr_expr (x);
13593 : 18 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
13594 : 0 : var = build_fold_addr_expr (var);
13595 : 18 : size2 = fold_build2 (POINTER_DIFF_EXPR, ssizetype,
13596 : : build_fold_addr_expr (var1), var);
13597 : 18 : size2 = fold_convert (sizetype, size2);
13598 : 18 : if (present)
13599 : : {
13600 : 1 : tree tmp = create_tmp_var (TREE_TYPE (var1));
13601 : 1 : gimplify_assign (tmp, var1, &ilist);
13602 : 1 : var1 = tmp;
13603 : 1 : tmp = create_tmp_var (TREE_TYPE (var2));
13604 : 1 : gimplify_assign (tmp, var2, &ilist);
13605 : 1 : var2 = tmp;
13606 : 1 : tmp = create_tmp_var (TREE_TYPE (size1));
13607 : 1 : gimplify_assign (tmp, size1, &ilist);
13608 : 1 : size1 = tmp;
13609 : 1 : tmp = create_tmp_var (TREE_TYPE (size2));
13610 : 1 : gimplify_assign (tmp, size2, &ilist);
13611 : 1 : size2 = tmp;
13612 : 1 : gimple_seq_add_stmt (&ilist, gimple_build_goto (after_lb));
13613 : 1 : gimple_seq_add_stmt (&ilist, gimple_build_label (null_lb));
13614 : 1 : gimplify_assign (var1, null_pointer_node, &ilist);
13615 : 1 : gimplify_assign (var2, null_pointer_node, &ilist);
13616 : 1 : gimplify_assign (size1, size_zero_node, &ilist);
13617 : 1 : gimplify_assign (size2, size_zero_node, &ilist);
13618 : 1 : gimple_seq_add_stmt (&ilist, gimple_build_label (after_lb));
13619 : : }
13620 : 18 : x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
13621 : 18 : gimplify_assign (x, var1, &ilist);
13622 : 18 : tkind = GOMP_MAP_FIRSTPRIVATE;
13623 : 18 : talign = DECL_ALIGN_UNIT (ovar);
13624 : 18 : talign = ceil_log2 (talign);
13625 : 18 : tkind |= talign << talign_shift;
13626 : 18 : gcc_checking_assert (tkind
13627 : : <= tree_to_uhwi (
13628 : : TYPE_MAX_VALUE (tkind_type)));
13629 : 18 : purpose = size_int (map_idx++);
13630 : 18 : CONSTRUCTOR_APPEND_ELT (vsize, purpose, size1);
13631 : 18 : if (TREE_CODE (size1) != INTEGER_CST)
13632 : 18 : TREE_STATIC (TREE_VEC_ELT (t, 1)) = 0;
13633 : 18 : CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13634 : : build_int_cstu (tkind_type, tkind));
13635 : 18 : x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
13636 : 18 : gimplify_assign (x, var2, &ilist);
13637 : 18 : tkind = GOMP_MAP_ATTACH;
13638 : 18 : purpose = size_int (map_idx++);
13639 : 18 : CONSTRUCTOR_APPEND_ELT (vsize, purpose, size2);
13640 : 18 : CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13641 : : build_int_cstu (tkind_type, tkind));
13642 : : }
13643 : : break;
13644 : :
13645 : 2397 : case OMP_CLAUSE_USE_DEVICE_PTR:
13646 : 2397 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13647 : 2397 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13648 : 2397 : case OMP_CLAUSE_IS_DEVICE_PTR:
13649 : 2397 : ovar = OMP_CLAUSE_DECL (c);
13650 : 2397 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13651 : : {
13652 : 221 : if (lang_hooks.decls.omp_array_data (ovar, true))
13653 : 13 : goto omp_has_device_addr_descr;
13654 : 225 : while (TREE_CODE (ovar) == INDIRECT_REF
13655 : 225 : || TREE_CODE (ovar) == ARRAY_REF)
13656 : 17 : ovar = TREE_OPERAND (ovar, 0);
13657 : : }
13658 : 2384 : var = lookup_decl_in_outer_ctx (ovar, ctx);
13659 : :
13660 : 2384 : if (lang_hooks.decls.omp_array_data (ovar, true))
13661 : : {
13662 : 601 : tkind = ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13663 : 601 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13664 : 601 : ? GOMP_MAP_USE_DEVICE_PTR : GOMP_MAP_FIRSTPRIVATE_INT);
13665 : 601 : x = build_sender_ref ((splay_tree_key) &DECL_NAME (ovar), ctx);
13666 : : }
13667 : 1783 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13668 : 1783 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13669 : : {
13670 : 1453 : tkind = GOMP_MAP_USE_DEVICE_PTR;
13671 : 1453 : x = build_sender_ref ((splay_tree_key) &DECL_UID (ovar), ctx);
13672 : : }
13673 : : else
13674 : : {
13675 : 330 : tkind = GOMP_MAP_FIRSTPRIVATE_INT;
13676 : 330 : x = build_sender_ref (ovar, ctx);
13677 : : }
13678 : :
13679 : 2384 : if (is_gimple_omp_oacc (ctx->stmt))
13680 : : {
13681 : 147 : gcc_assert (tkind == GOMP_MAP_USE_DEVICE_PTR);
13682 : :
13683 : 147 : if (OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c))
13684 : 57 : tkind = GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT;
13685 : : }
13686 : :
13687 : 2384 : type = TREE_TYPE (ovar);
13688 : 2384 : if (lang_hooks.decls.omp_array_data (ovar, true))
13689 : 601 : var = lang_hooks.decls.omp_array_data (var, false);
13690 : 1783 : else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
13691 : 515 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13692 : 1476 : && !omp_privatize_by_reference (ovar)
13693 : 504 : && !omp_is_allocatable_or_ptr (ovar))
13694 : 3201 : || TREE_CODE (type) == ARRAY_TYPE)
13695 : 393 : var = build_fold_addr_expr (var);
13696 : : else
13697 : : {
13698 : 1390 : if (omp_privatize_by_reference (ovar)
13699 : 351 : || omp_check_optional_argument (ovar, false)
13700 : 1741 : || omp_is_allocatable_or_ptr (ovar))
13701 : : {
13702 : 1212 : type = TREE_TYPE (type);
13703 : 1212 : if (POINTER_TYPE_P (type)
13704 : 1212 : && TREE_CODE (type) != ARRAY_TYPE
13705 : 1571 : && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
13706 : 36 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
13707 : 28 : && !omp_is_allocatable_or_ptr (ovar))
13708 : 337 : || (omp_privatize_by_reference (ovar)
13709 : 337 : && omp_is_allocatable_or_ptr (ovar))))
13710 : 357 : var = build_simple_mem_ref (var);
13711 : 1212 : var = fold_convert (TREE_TYPE (x), var);
13712 : : }
13713 : : }
13714 : 2384 : tree present;
13715 : 2384 : present = omp_check_optional_argument (ovar, true);
13716 : 2384 : if (present)
13717 : : {
13718 : 878 : tree null_label = create_artificial_label (UNKNOWN_LOCATION);
13719 : 878 : tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
13720 : 878 : tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
13721 : 878 : tree new_x = unshare_expr (x);
13722 : 878 : gimplify_expr (&present, &ilist, NULL, is_gimple_val,
13723 : : fb_rvalue);
13724 : 878 : gcond *cond = gimple_build_cond_from_tree (present,
13725 : : notnull_label,
13726 : : null_label);
13727 : 878 : gimple_seq_add_stmt (&ilist, cond);
13728 : 878 : gimple_seq_add_stmt (&ilist, gimple_build_label (null_label));
13729 : 878 : gimplify_assign (new_x, null_pointer_node, &ilist);
13730 : 878 : gimple_seq_add_stmt (&ilist, gimple_build_goto (opt_arg_label));
13731 : 878 : gimple_seq_add_stmt (&ilist,
13732 : 878 : gimple_build_label (notnull_label));
13733 : 878 : gimplify_assign (x, var, &ilist);
13734 : 878 : gimple_seq_add_stmt (&ilist,
13735 : 878 : gimple_build_label (opt_arg_label));
13736 : : }
13737 : : else
13738 : 1506 : gimplify_assign (x, var, &ilist);
13739 : 2384 : s = size_int (0);
13740 : 2384 : purpose = size_int (map_idx++);
13741 : 2384 : CONSTRUCTOR_APPEND_ELT (vsize, purpose, s);
13742 : 2384 : gcc_checking_assert (tkind
13743 : : < (HOST_WIDE_INT_C (1U) << talign_shift));
13744 : 2384 : gcc_checking_assert (tkind
13745 : : <= tree_to_uhwi (TYPE_MAX_VALUE (tkind_type)));
13746 : 2384 : CONSTRUCTOR_APPEND_ELT (vkind, purpose,
13747 : : build_int_cstu (tkind_type, tkind));
13748 : 2384 : break;
13749 : : }
13750 : :
13751 : 30521 : gcc_assert (map_idx == map_cnt);
13752 : :
13753 : 30521 : if (!deep_map_cnt)
13754 : : {
13755 : 30521 : DECL_INITIAL (TREE_VEC_ELT (t, 1))
13756 : 30521 : = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 1)), vsize);
13757 : 30521 : DECL_INITIAL (TREE_VEC_ELT (t, 2))
13758 : 61042 : = build_constructor (TREE_TYPE (TREE_VEC_ELT (t, 2)), vkind);
13759 : : }
13760 : 91563 : for (int i = 1; i <= 2; i++)
13761 : 61042 : if (deep_map_cnt || !TREE_STATIC (TREE_VEC_ELT (t, i)))
13762 : : {
13763 : 8757 : tree tmp = TREE_VEC_ELT (t, i);
13764 : 8757 : if (deep_map_cnt)
13765 : : {
13766 : 0 : const char *prefix = (i == 1 ? ".omp_data_sizes0"
13767 : : : ".omp_data_kinds0");
13768 : 0 : tree type = (i == 1) ? size_type_node : tkind_type;
13769 : 0 : type = build_array_type_nelts (type, map_cnt);
13770 : 0 : tree var = create_tmp_var (type, prefix);
13771 : 0 : DECL_NAMELESS (var) = 1;
13772 : 0 : TREE_ADDRESSABLE (var) = 1;
13773 : 0 : TREE_STATIC (var) = TREE_STATIC (tmp);
13774 : 0 : DECL_INITIAL (var) = build_constructor (type, i == 1
13775 : : ? vsize : vkind);
13776 : 0 : tmp = var;
13777 : 0 : TREE_STATIC (TREE_VEC_ELT (t, i)) = 0;
13778 : : }
13779 : :
13780 : 8757 : gimple_seq initlist = NULL;
13781 : 8757 : force_gimple_operand (build1 (DECL_EXPR, void_type_node, tmp),
13782 : : &initlist, true, NULL_TREE);
13783 : 8757 : gimple_seq_add_seq (&ilist, initlist);
13784 : :
13785 : 8757 : if (deep_map_cnt)
13786 : : {
13787 : 0 : tree tmp2;
13788 : 0 : tree call = builtin_decl_explicit (BUILT_IN_MEMCPY);
13789 : 0 : tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13790 : 0 : call = build_call_expr_loc (input_location, call, 3,
13791 : 0 : TREE_VEC_ELT (t, i),
13792 : : build_fold_addr_expr (tmp), tmp2);
13793 : 0 : gimplify_and_add (call, &ilist);
13794 : : }
13795 : :
13796 : 8757 : if (!TREE_STATIC (tmp))
13797 : : {
13798 : 8757 : tree clobber = build_clobber (TREE_TYPE (tmp));
13799 : 8757 : gimple_seq_add_stmt (&olist,
13800 : 8757 : gimple_build_assign (tmp, clobber));
13801 : : }
13802 : 8757 : if (deep_map_cnt)
13803 : : {
13804 : 0 : tmp = TREE_VEC_ELT (t, i);
13805 : 0 : tree call = builtin_decl_explicit (BUILT_IN_FREE);
13806 : 0 : call = build_call_expr_loc (input_location, call, 1, tmp);
13807 : 0 : gimplify_and_add (call, &olist);
13808 : 0 : tree clobber = build_clobber (TREE_TYPE (tmp));
13809 : 0 : gimple_seq_add_stmt (&olist,
13810 : 0 : gimple_build_assign (tmp, clobber));
13811 : : }
13812 : : }
13813 : 52285 : else if (omp_maybe_offloaded_ctx (ctx->outer))
13814 : : {
13815 : 3292 : tree id = get_identifier ("omp declare target");
13816 : 3292 : tree decl = TREE_VEC_ELT (t, i);
13817 : 3292 : DECL_ATTRIBUTES (decl)
13818 : 3292 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13819 : 3292 : varpool_node *node = varpool_node::get (decl);
13820 : 3292 : if (node)
13821 : : {
13822 : 0 : node->offloadable = 1;
13823 : 0 : if (ENABLE_OFFLOADING)
13824 : : {
13825 : : g->have_offload = true;
13826 : : vec_safe_push (offload_vars, t);
13827 : : }
13828 : : }
13829 : : }
13830 : :
13831 : 30521 : if (deep_map_cnt)
13832 : : {
13833 : 0 : tree call = builtin_decl_explicit (BUILT_IN_FREE);
13834 : 0 : call = build_call_expr_loc (input_location, call, 1,
13835 : 0 : TREE_VEC_ELT (t, 0));
13836 : 0 : gimplify_and_add (call, &olist);
13837 : :
13838 : 0 : gimplify_expr (&TREE_VEC_ELT (t, 1), &ilist, NULL, is_gimple_val,
13839 : : fb_rvalue);
13840 : : }
13841 : :
13842 : 30521 : tree clobber = build_clobber (TREE_TYPE (ctx->sender_decl));
13843 : 30521 : 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 : 35308 : new_body = NULL;
13851 : :
13852 : 35308 : if (offloaded
13853 : 19880 : && ctx->record_type)
13854 : : {
13855 : 15320 : t = ctx->sender_decl;
13856 : 15320 : if (!deep_map_cnt)
13857 : 15320 : t = build_fold_addr_expr_loc (loc, t);
13858 : : /* fixup_child_record_type might have changed receiver_decl's type. */
13859 : 15320 : t = fold_convert_loc (loc, TREE_TYPE (ctx->receiver_decl), t);
13860 : 15320 : if (!AGGREGATE_TYPE_P (TREE_TYPE (ctx->sender_decl)))
13861 : 0 : gimplify_assign (ctx->receiver_decl, t, &new_body);
13862 : : else
13863 : 15320 : gimple_seq_add_stmt (&new_body,
13864 : 15320 : gimple_build_assign (ctx->receiver_decl, t));
13865 : : }
13866 : 35308 : gimple_seq_add_seq (&new_body, fplist);
13867 : :
13868 : 35308 : if (offloaded || data_region)
13869 : : {
13870 : 133489 : tree prev = NULL_TREE;
13871 : 133489 : for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13872 : 109579 : switch (OMP_CLAUSE_CODE (c))
13873 : : {
13874 : : tree var, x;
13875 : : default:
13876 : : break;
13877 : 14708 : case OMP_CLAUSE_FIRSTPRIVATE:
13878 : 14708 : omp_firstprivatize_data_region:
13879 : 14708 : if (is_gimple_omp_oacc (ctx->stmt))
13880 : : break;
13881 : 10818 : var = OMP_CLAUSE_DECL (c);
13882 : 10818 : if (omp_privatize_by_reference (var)
13883 : 10818 : || is_gimple_reg_type (TREE_TYPE (var)))
13884 : : {
13885 : 10719 : tree new_var = lookup_decl (var, ctx);
13886 : 10719 : tree type;
13887 : 10719 : type = TREE_TYPE (var);
13888 : 10719 : if (omp_privatize_by_reference (var))
13889 : 788 : type = TREE_TYPE (type);
13890 : 10719 : if ((INTEGRAL_TYPE_P (type)
13891 : 10502 : && TYPE_PRECISION (type) <= POINTER_SIZE)
13892 : 10729 : || TREE_CODE (type) == POINTER_TYPE)
13893 : : {
13894 : 10633 : x = build_receiver_ref (var, false, ctx);
13895 : 10633 : if (TREE_CODE (type) != POINTER_TYPE)
13896 : 10492 : x = fold_convert (pointer_sized_int_node, x);
13897 : 10633 : x = fold_convert (type, x);
13898 : 10633 : gimplify_expr (&x, &new_body, NULL, is_gimple_val,
13899 : : fb_rvalue);
13900 : 10633 : if (omp_privatize_by_reference (var))
13901 : : {
13902 : 761 : tree v = create_tmp_var_raw (type, get_name (var));
13903 : 761 : gimple_add_tmp_var (v);
13904 : 761 : TREE_ADDRESSABLE (v) = 1;
13905 : 761 : gimple_seq_add_stmt (&new_body,
13906 : 761 : gimple_build_assign (v, x));
13907 : 761 : x = build_fold_addr_expr (v);
13908 : : }
13909 : 10633 : gimple_seq_add_stmt (&new_body,
13910 : 10633 : gimple_build_assign (new_var, x));
13911 : : }
13912 : : else
13913 : : {
13914 : 86 : bool by_ref = !omp_privatize_by_reference (var);
13915 : 86 : x = build_receiver_ref (var, by_ref, ctx);
13916 : 86 : gimplify_expr (&x, &new_body, NULL, is_gimple_val,
13917 : : fb_rvalue);
13918 : 86 : gimple_seq_add_stmt (&new_body,
13919 : 86 : gimple_build_assign (new_var, x));
13920 : : }
13921 : : }
13922 : 99 : else if (is_variable_sized (var))
13923 : : {
13924 : 4 : tree pvar = DECL_VALUE_EXPR (var);
13925 : 4 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
13926 : 4 : pvar = TREE_OPERAND (pvar, 0);
13927 : 4 : gcc_assert (DECL_P (pvar));
13928 : 4 : tree new_var = lookup_decl (pvar, ctx);
13929 : 4 : x = build_receiver_ref (var, false, ctx);
13930 : 4 : gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
13931 : 4 : gimple_seq_add_stmt (&new_body,
13932 : 4 : gimple_build_assign (new_var, x));
13933 : : }
13934 : : break;
13935 : 172 : case OMP_CLAUSE_PRIVATE:
13936 : 172 : if (is_gimple_omp_oacc (ctx->stmt))
13937 : : break;
13938 : 93 : var = OMP_CLAUSE_DECL (c);
13939 : 93 : if (omp_privatize_by_reference (var))
13940 : : {
13941 : 5 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
13942 : 5 : tree new_var = lookup_decl (var, ctx);
13943 : 5 : x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
13944 : 5 : if (TREE_CONSTANT (x))
13945 : : {
13946 : 3 : x = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (new_var)),
13947 : : get_name (var));
13948 : 3 : gimple_add_tmp_var (x);
13949 : 3 : TREE_ADDRESSABLE (x) = 1;
13950 : 3 : x = build_fold_addr_expr_loc (clause_loc, x);
13951 : : }
13952 : : else
13953 : : break;
13954 : :
13955 : 3 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
13956 : 3 : gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
13957 : 3 : gimple_seq_add_stmt (&new_body,
13958 : 3 : gimple_build_assign (new_var, x));
13959 : : }
13960 : : break;
13961 : 2397 : case OMP_CLAUSE_USE_DEVICE_PTR:
13962 : 2397 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13963 : 2397 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13964 : 2397 : case OMP_CLAUSE_IS_DEVICE_PTR:
13965 : 2397 : tree new_var;
13966 : 2397 : gimple_seq assign_body;
13967 : 2397 : bool is_array_data;
13968 : 2397 : bool do_optional_check;
13969 : 2397 : assign_body = NULL;
13970 : 2397 : do_optional_check = false;
13971 : 2397 : var = OMP_CLAUSE_DECL (c);
13972 : 2397 : is_array_data = lang_hooks.decls.omp_array_data (var, true) != NULL;
13973 : 2397 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR && is_array_data)
13974 : 13 : goto omp_firstprivatize_data_region;
13975 : :
13976 : 2384 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR
13977 : 2384 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
13978 : 4108 : x = build_sender_ref (is_array_data
13979 : 601 : ? (splay_tree_key) &DECL_NAME (var)
13980 : 1453 : : (splay_tree_key) &DECL_UID (var), ctx);
13981 : : else
13982 : : {
13983 : 330 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13984 : : {
13985 : 225 : while (TREE_CODE (var) == INDIRECT_REF
13986 : 225 : || TREE_CODE (var) == ARRAY_REF)
13987 : 17 : var = TREE_OPERAND (var, 0);
13988 : : }
13989 : 330 : x = build_receiver_ref (var, false, ctx);
13990 : : }
13991 : :
13992 : 2384 : if (is_array_data)
13993 : : {
13994 : 601 : bool is_ref = omp_privatize_by_reference (var);
13995 : 601 : 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 : 601 : new_var = lookup_decl (var, ctx);
13999 : 601 : new_var = DECL_VALUE_EXPR (new_var);
14000 : 601 : tree v = new_var;
14001 : 601 : tree v2 = var;
14002 : 601 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14003 : 601 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR)
14004 : 601 : v2 = maybe_lookup_decl_in_outer_ctx (var, ctx);
14005 : :
14006 : 601 : if (is_ref)
14007 : : {
14008 : 396 : v2 = build_fold_indirect_ref (v2);
14009 : 396 : v = create_tmp_var_raw (TREE_TYPE (v2), get_name (var));
14010 : 396 : gimple_add_tmp_var (v);
14011 : 396 : TREE_ADDRESSABLE (v) = 1;
14012 : 396 : gimplify_assign (v, v2, &assign_body);
14013 : 396 : tree rhs = build_fold_addr_expr (v);
14014 : 396 : gimple_seq_add_stmt (&assign_body,
14015 : 396 : gimple_build_assign (new_var, rhs));
14016 : : }
14017 : : else
14018 : 205 : gimplify_assign (new_var, v2, &assign_body);
14019 : :
14020 : 601 : v2 = lang_hooks.decls.omp_array_data (unshare_expr (v), false);
14021 : 601 : gcc_assert (v2);
14022 : 601 : gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14023 : 1202 : gimple_seq_add_stmt (&assign_body,
14024 : 601 : gimple_build_assign (v2, x));
14025 : : }
14026 : 1783 : else if (is_variable_sized (var))
14027 : : {
14028 : 12 : tree pvar = DECL_VALUE_EXPR (var);
14029 : 12 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
14030 : 12 : pvar = TREE_OPERAND (pvar, 0);
14031 : 12 : gcc_assert (DECL_P (pvar));
14032 : 12 : new_var = lookup_decl (pvar, ctx);
14033 : 12 : gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14034 : 24 : gimple_seq_add_stmt (&assign_body,
14035 : 12 : gimple_build_assign (new_var, x));
14036 : : }
14037 : 1771 : else if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR
14038 : 509 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14039 : 1468 : && !omp_privatize_by_reference (var)
14040 : 496 : && !omp_is_allocatable_or_ptr (var))
14041 : 3185 : || TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
14042 : : {
14043 : 381 : new_var = lookup_decl (var, ctx);
14044 : 381 : new_var = DECL_VALUE_EXPR (new_var);
14045 : 381 : gcc_assert (TREE_CODE (new_var) == MEM_REF);
14046 : 381 : new_var = TREE_OPERAND (new_var, 0);
14047 : 381 : gcc_assert (DECL_P (new_var));
14048 : 381 : gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14049 : 762 : gimple_seq_add_stmt (&assign_body,
14050 : 381 : gimple_build_assign (new_var, x));
14051 : : }
14052 : : else
14053 : : {
14054 : 1390 : tree type = TREE_TYPE (var);
14055 : 1390 : new_var = lookup_decl (var, ctx);
14056 : 1390 : if (omp_privatize_by_reference (var))
14057 : : {
14058 : 1039 : type = TREE_TYPE (type);
14059 : 1039 : if (POINTER_TYPE_P (type)
14060 : 1039 : && TREE_CODE (type) != ARRAY_TYPE
14061 : 1398 : && ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_USE_DEVICE_ADDR
14062 : 36 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR)
14063 : 331 : || (omp_privatize_by_reference (var)
14064 : 331 : && omp_is_allocatable_or_ptr (var))))
14065 : : {
14066 : 357 : tree v = create_tmp_var_raw (type, get_name (var));
14067 : 357 : gimple_add_tmp_var (v);
14068 : 357 : TREE_ADDRESSABLE (v) = 1;
14069 : 357 : x = fold_convert (type, x);
14070 : 357 : gimplify_expr (&x, &assign_body, NULL, is_gimple_val,
14071 : : fb_rvalue);
14072 : 357 : gimple_seq_add_stmt (&assign_body,
14073 : 357 : gimple_build_assign (v, x));
14074 : 357 : x = build_fold_addr_expr (v);
14075 : 357 : do_optional_check = true;
14076 : : }
14077 : : }
14078 : 1390 : new_var = DECL_VALUE_EXPR (new_var);
14079 : 1390 : x = fold_convert (TREE_TYPE (new_var), x);
14080 : 1390 : gimplify_expr (&x, &assign_body, NULL, is_gimple_val, fb_rvalue);
14081 : 1390 : gimple_seq_add_stmt (&assign_body,
14082 : 1390 : gimple_build_assign (new_var, x));
14083 : : }
14084 : 2384 : tree present;
14085 : 994 : present = ((do_optional_check
14086 : 958 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_HAS_DEVICE_ADDR
14087 : 952 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IS_DEVICE_PTR)
14088 : 2323 : ? omp_check_optional_argument (OMP_CLAUSE_DECL (c), true)
14089 : : : NULL_TREE);
14090 : 2384 : if (present)
14091 : : {
14092 : 480 : tree null_label = create_artificial_label (UNKNOWN_LOCATION);
14093 : 480 : tree notnull_label = create_artificial_label (UNKNOWN_LOCATION);
14094 : 480 : tree opt_arg_label = create_artificial_label (UNKNOWN_LOCATION);
14095 : 480 : glabel *null_glabel = gimple_build_label (null_label);
14096 : 480 : glabel *notnull_glabel = gimple_build_label (notnull_label);
14097 : 480 : ggoto *opt_arg_ggoto = gimple_build_goto (opt_arg_label);
14098 : 480 : gimplify_expr (&x, &new_body, NULL, is_gimple_val,
14099 : : fb_rvalue);
14100 : 480 : gimplify_expr (&present, &new_body, NULL, is_gimple_val,
14101 : : fb_rvalue);
14102 : 480 : gcond *cond = gimple_build_cond_from_tree (present,
14103 : : notnull_label,
14104 : : null_label);
14105 : 480 : gimple_seq_add_stmt (&new_body, cond);
14106 : 480 : gimple_seq_add_stmt (&new_body, null_glabel);
14107 : 480 : gimplify_assign (new_var, null_pointer_node, &new_body);
14108 : 480 : gimple_seq_add_stmt (&new_body, opt_arg_ggoto);
14109 : 480 : gimple_seq_add_stmt (&new_body, notnull_glabel);
14110 : 480 : gimple_seq_add_seq (&new_body, assign_body);
14111 : 480 : gimple_seq_add_stmt (&new_body,
14112 : 480 : gimple_build_label (opt_arg_label));
14113 : : }
14114 : : else
14115 : 1904 : 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 : 133489 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14123 : 109579 : switch (OMP_CLAUSE_CODE (c))
14124 : : {
14125 : : tree var;
14126 : : default:
14127 : : break;
14128 : 60757 : case OMP_CLAUSE_MAP:
14129 : 60757 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14130 : 60757 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
14131 : : {
14132 : 3522 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14133 : 3522 : poly_int64 offset = 0;
14134 : 3522 : gcc_assert (prev);
14135 : 3522 : var = OMP_CLAUSE_DECL (c);
14136 : 3522 : if (DECL_P (var)
14137 : 3522 : && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE
14138 : 1054 : && is_global_var (maybe_lookup_decl_in_outer_ctx (var,
14139 : : ctx))
14140 : 3619 : && varpool_node::get_create (var)->offloadable)
14141 : : break;
14142 : 3520 : if (TREE_CODE (var) == INDIRECT_REF
14143 : 3520 : && TREE_CODE (TREE_OPERAND (var, 0)) == COMPONENT_REF)
14144 : 0 : var = TREE_OPERAND (var, 0);
14145 : 3520 : if (TREE_CODE (var) == COMPONENT_REF)
14146 : : {
14147 : 0 : var = get_addr_base_and_unit_offset (var, &offset);
14148 : 0 : gcc_assert (var != NULL_TREE && DECL_P (var));
14149 : : }
14150 : 3520 : else if (DECL_SIZE (var)
14151 : 3520 : && TREE_CODE (DECL_SIZE (var)) != INTEGER_CST)
14152 : : {
14153 : 710 : tree var2 = DECL_VALUE_EXPR (var);
14154 : 710 : gcc_assert (TREE_CODE (var2) == INDIRECT_REF);
14155 : 710 : var2 = TREE_OPERAND (var2, 0);
14156 : 710 : gcc_assert (DECL_P (var2));
14157 : : var = var2;
14158 : : }
14159 : 3520 : tree new_var = lookup_decl (var, ctx), x;
14160 : 3520 : tree type = TREE_TYPE (new_var);
14161 : 3520 : bool is_ref;
14162 : 3520 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == INDIRECT_REF
14163 : 3520 : && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14164 : : == COMPONENT_REF))
14165 : : {
14166 : 0 : type = TREE_TYPE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0));
14167 : 0 : is_ref = true;
14168 : 0 : 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 : 3520 : else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14174 : : {
14175 : 0 : type = TREE_TYPE (OMP_CLAUSE_DECL (c));
14176 : 0 : is_ref = TREE_CODE (type) == REFERENCE_TYPE;
14177 : 0 : 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 : 3520 : is_ref = omp_privatize_by_reference (var);
14184 : 3520 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
14185 : : is_ref = false;
14186 : 6465 : bool ref_to_array = false;
14187 : 6465 : bool ref_to_ptr = false;
14188 : 3121 : if (is_ref)
14189 : : {
14190 : 176 : type = TREE_TYPE (type);
14191 : 176 : if (TREE_CODE (type) == ARRAY_TYPE)
14192 : : {
14193 : 170 : type = build_pointer_type (type);
14194 : 170 : ref_to_array = true;
14195 : : }
14196 : : }
14197 : 3344 : else if (TREE_CODE (type) == ARRAY_TYPE)
14198 : : {
14199 : 342 : tree decl2 = DECL_VALUE_EXPR (new_var);
14200 : 342 : gcc_assert (TREE_CODE (decl2) == MEM_REF);
14201 : 342 : decl2 = TREE_OPERAND (decl2, 0);
14202 : 342 : gcc_assert (DECL_P (decl2));
14203 : 342 : new_var = decl2;
14204 : 342 : type = TREE_TYPE (new_var);
14205 : : }
14206 : 3002 : else if (TREE_CODE (type) == REFERENCE_TYPE
14207 : 3002 : && TREE_CODE (TREE_TYPE (type)) == POINTER_TYPE)
14208 : : {
14209 : 157 : type = TREE_TYPE (type);
14210 : 157 : ref_to_ptr = true;
14211 : : }
14212 : 3520 : x = build_receiver_ref (OMP_CLAUSE_DECL (prev), false, ctx);
14213 : 3520 : x = fold_convert_loc (clause_loc, type, x);
14214 : 3520 : if (!integer_zerop (OMP_CLAUSE_SIZE (c)))
14215 : : {
14216 : 694 : tree bias = OMP_CLAUSE_SIZE (c);
14217 : 694 : if (DECL_P (bias))
14218 : 302 : bias = lookup_decl (bias, ctx);
14219 : 694 : bias = fold_convert_loc (clause_loc, sizetype, bias);
14220 : 694 : bias = fold_build1_loc (clause_loc, NEGATE_EXPR, sizetype,
14221 : : bias);
14222 : 694 : x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
14223 : 694 : TREE_TYPE (x), x, bias);
14224 : : }
14225 : 3520 : if (ref_to_array)
14226 : 170 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
14227 : 3520 : gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14228 : 3520 : if ((is_ref && !ref_to_array)
14229 : 3514 : || ref_to_ptr)
14230 : : {
14231 : 163 : tree t = create_tmp_var_raw (type, get_name (var));
14232 : 163 : gimple_add_tmp_var (t);
14233 : 163 : TREE_ADDRESSABLE (t) = 1;
14234 : 163 : gimple_seq_add_stmt (&new_body,
14235 : 163 : gimple_build_assign (t, x));
14236 : 163 : x = build_fold_addr_expr_loc (clause_loc, t);
14237 : : }
14238 : 3520 : gimple_seq_add_stmt (&new_body,
14239 : 3520 : gimple_build_assign (new_var, x));
14240 : 3520 : prev = NULL_TREE;
14241 : : }
14242 : 57235 : else if (OMP_CLAUSE_CHAIN (c)
14243 : 41604 : && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c))
14244 : : == OMP_CLAUSE_MAP
14245 : 93502 : && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
14246 : : == GOMP_MAP_FIRSTPRIVATE_POINTER
14247 : 33144 : || (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
14248 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)))
14249 : : prev = c;
14250 : : break;
14251 : 172 : case OMP_CLAUSE_PRIVATE:
14252 : 172 : var = OMP_CLAUSE_DECL (c);
14253 : 172 : if (is_variable_sized (var))
14254 : : {
14255 : 1 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14256 : 1 : tree new_var = lookup_decl (var, ctx);
14257 : 1 : tree pvar = DECL_VALUE_EXPR (var);
14258 : 1 : gcc_assert (TREE_CODE (pvar) == INDIRECT_REF);
14259 : 1 : pvar = TREE_OPERAND (pvar, 0);
14260 : 1 : gcc_assert (DECL_P (pvar));
14261 : 1 : tree new_pvar = lookup_decl (pvar, ctx);
14262 : 1 : tree atmp = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
14263 : 1 : tree al = size_int (DECL_ALIGN (var));
14264 : 1 : tree x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
14265 : 1 : x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
14266 : 1 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_pvar), x);
14267 : 1 : gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14268 : 1 : gimple_seq_add_stmt (&new_body,
14269 : 1 : gimple_build_assign (new_pvar, x));
14270 : : }
14271 : 171 : else if (omp_privatize_by_reference (var)
14272 : 171 : && !is_gimple_omp_oacc (ctx->stmt))
14273 : : {
14274 : 5 : location_t clause_loc = OMP_CLAUSE_LOCATION (c);
14275 : 5 : tree new_var = lookup_decl (var, ctx);
14276 : 5 : tree x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
14277 : 5 : if (TREE_CONSTANT (x))
14278 : : break;
14279 : : else
14280 : : {
14281 : 2 : tree atmp
14282 : 2 : = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
14283 : 2 : tree rtype = TREE_TYPE (TREE_TYPE (new_var));
14284 : 2 : tree al = size_int (TYPE_ALIGN (rtype));
14285 : 2 : x = build_call_expr_loc (clause_loc, atmp, 2, x, al);
14286 : : }
14287 : :
14288 : 2 : x = fold_convert_loc (clause_loc, TREE_TYPE (new_var), x);
14289 : 2 : gimplify_expr (&x, &new_body, NULL, is_gimple_val, fb_rvalue);
14290 : 2 : gimple_seq_add_stmt (&new_body,
14291 : 2 : gimple_build_assign (new_var, x));
14292 : : }
14293 : : break;
14294 : : }
14295 : :
14296 : 23910 : gimple_seq fork_seq = NULL;
14297 : 23910 : gimple_seq join_seq = NULL;
14298 : :
14299 : 23910 : 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 : 9385 : tree level = build_int_cst (integer_type_node, GOMP_DIM_GANG);
14304 : :
14305 : 9385 : gcall *private_marker = lower_oacc_private_marker (ctx);
14306 : :
14307 : 9385 : if (private_marker)
14308 : 22 : gimple_call_set_arg (private_marker, 2, level);
14309 : :
14310 : 9385 : lower_oacc_reductions (gimple_location (ctx->stmt), clauses, level,
14311 : : false, NULL, private_marker, NULL, &fork_seq,
14312 : : &join_seq, ctx);
14313 : : }
14314 : :
14315 : 23910 : gimple_seq_add_seq (&new_body, fork_seq);
14316 : 23910 : gimple_seq_add_seq (&new_body, tgt_body);
14317 : 23910 : gimple_seq_add_seq (&new_body, join_seq);
14318 : :
14319 : 23910 : if (offloaded)
14320 : : {
14321 : 19880 : new_body = maybe_catch_exception (new_body);
14322 : 19880 : gimple_seq_add_stmt (&new_body, gimple_build_omp_return (false));
14323 : : }
14324 : 23910 : gimple_omp_set_body (stmt, new_body);
14325 : : }
14326 : :
14327 : 55188 : bind = gimple_build_bind (NULL, NULL,
14328 : 19880 : tgt_bind ? gimple_bind_block (tgt_bind)
14329 : : : NULL_TREE);
14330 : 70234 : gsi_replace (gsi_p, dep_bind ? dep_bind : bind, true);
14331 : 35308 : gimple_bind_add_seq (bind, ilist);
14332 : 35308 : gimple_bind_add_stmt (bind, stmt);
14333 : 35308 : gimple_bind_add_seq (bind, olist);
14334 : :
14335 : 35308 : pop_gimplify_context (NULL);
14336 : :
14337 : 35308 : if (dep_bind)
14338 : : {
14339 : 382 : gimple_bind_add_seq (dep_bind, dep_ilist);
14340 : 382 : gimple_bind_add_stmt (dep_bind, bind);
14341 : 382 : gimple_bind_add_seq (dep_bind, dep_olist);
14342 : 382 : pop_gimplify_context (dep_bind);
14343 : : }
14344 : 35308 : }
14345 : :
14346 : : /* Generate code to implement the action-clauses (destroy, init, use) of an
14347 : : OpenMP interop construct. */
14348 : :
14349 : : static void
14350 : 441 : 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 : 441 : if (objs.length () == 0)
14355 : 195 : return;
14356 : :
14357 : 246 : enum omp_clause_code action = OMP_CLAUSE_CODE (objs[0]);
14358 : 246 : if (action == OMP_CLAUSE_INIT)
14359 : 393 : gcc_checking_assert (objs.length () == interop_types->length ()
14360 : : && objs.length () == prefer_types->length ());
14361 : : else
14362 : 115 : gcc_assert (prefer_types == NULL && interop_types == NULL);
14363 : :
14364 : 246 : tree ret_objs = NULL_TREE, ret_interop_types = NULL_TREE,
14365 : 246 : ret_prefer_types = NULL_TREE;
14366 : :
14367 : : /* Build an array of interop objects. */
14368 : :
14369 : 246 : tree type_obj_pref = build_array_type_nelts (ptr_type_node, objs.length ());
14370 : 246 : ret_objs = create_tmp_var (type_obj_pref, "interopobjs");
14371 : :
14372 : 246 : bool have_pref_type = false;
14373 : 246 : if (action == OMP_CLAUSE_INIT)
14374 : : {
14375 : 574 : for (tree pref_type : prefer_types)
14376 : 251 : if (pref_type != NULL_TREE)
14377 : : {
14378 : : have_pref_type = true;
14379 : : break;
14380 : : }
14381 : 131 : tree type_tgtsync
14382 : 131 : = build_array_type_nelts (integer_type_node, objs.length ());
14383 : 131 : ret_interop_types = create_tmp_var (type_tgtsync, "tgt_tgtsync");
14384 : 131 : if (have_pref_type)
14385 : 70 : ret_prefer_types = create_tmp_var (type_obj_pref, "pref_type");
14386 : : else
14387 : : {
14388 : 61 : ret_prefer_types = null_pointer_node;
14389 : 61 : prefer_types->truncate (0);
14390 : : }
14391 : : }
14392 : :
14393 : 808 : for (size_t i = 0; !objs.is_empty (); i++)
14394 : : {
14395 : 562 : tree offset = build_int_cst (integer_type_node, i);
14396 : 562 : tree init = build4 (ARRAY_REF, ptr_type_node, ret_objs, offset, NULL_TREE,
14397 : : NULL_TREE);
14398 : 562 : tree obj = OMP_CLAUSE_DECL (objs.pop ());
14399 : 562 : if (TREE_CODE (TREE_TYPE (obj)) == REFERENCE_TYPE)
14400 : 0 : obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
14401 : 562 : if (action != OMP_CLAUSE_USE
14402 : 562 : && TREE_CODE (TREE_TYPE (obj)) != POINTER_TYPE)
14403 : : /* For modifying actions, we need a pointer. */
14404 : 467 : obj = build_fold_addr_expr (obj);
14405 : 95 : else if (action == OMP_CLAUSE_USE
14406 : 95 : && TREE_CODE (TREE_TYPE (obj)) == POINTER_TYPE)
14407 : : /* For use action, we need the value. */
14408 : 0 : obj = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (obj)), obj);
14409 : 562 : init = build2 (MODIFY_EXPR, ptr_type_node, init,
14410 : : fold_convert (ptr_type_node, obj));
14411 : 562 : gimplify_and_add (init, seq);
14412 : :
14413 : 562 : if (action == OMP_CLAUSE_INIT)
14414 : : {
14415 : 375 : init = build4 (ARRAY_REF, integer_type_node, ret_interop_types,
14416 : : offset, NULL_TREE, NULL_TREE);
14417 : 375 : init = build2 (MODIFY_EXPR, integer_type_node, init,
14418 : 375 : interop_types->pop ());
14419 : 375 : gimplify_and_add (init, seq);
14420 : :
14421 : 375 : if (have_pref_type)
14422 : : {
14423 : 194 : tree prefer_type = prefer_types->pop ();
14424 : 194 : tree pref = (prefer_type == NULL_TREE
14425 : 194 : ? null_pointer_node
14426 : 179 : : build_fold_addr_expr (prefer_type));
14427 : 194 : init = build4 (ARRAY_REF, ptr_type_node, ret_prefer_types, offset,
14428 : : NULL_TREE, NULL_TREE);
14429 : 194 : init = build2 (MODIFY_EXPR, ptr_type_node, init, pref);
14430 : 194 : gimplify_and_add (init, seq);
14431 : : }
14432 : : }
14433 : : }
14434 : 246 : if (action == OMP_CLAUSE_INIT)
14435 : : {
14436 : 131 : if (have_pref_type)
14437 : 70 : ret_prefer_types = build_fold_addr_expr (ret_prefer_types);
14438 : 131 : ret_interop_types = build_fold_addr_expr (ret_interop_types);
14439 : : }
14440 : 246 : ret_objs = build_fold_addr_expr (ret_objs);
14441 : :
14442 : 623 : gcc_assert (objs.is_empty ()
14443 : : && (!interop_types || interop_types->is_empty ())
14444 : : && (!prefer_types || prefer_types->is_empty ()));
14445 : :
14446 : 246 : objs.safe_push (ret_objs);
14447 : 246 : if (action == OMP_CLAUSE_INIT)
14448 : : {
14449 : 131 : interop_types->safe_push (ret_interop_types);
14450 : 131 : prefer_types->safe_push (ret_prefer_types);
14451 : : }
14452 : : }
14453 : :
14454 : : /* Lower code for an OpenMP interop directive. */
14455 : :
14456 : : static void
14457 : 147 : lower_omp_interop (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14458 : : {
14459 : 147 : push_gimplify_context ();
14460 : :
14461 : 147 : tree block = make_node (BLOCK);
14462 : 147 : gbind *bind = gimple_build_bind (NULL, NULL, block);
14463 : 147 : 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 : 147 : tree flags = NULL_TREE;
14474 : 147 : tree depend = null_pointer_node;
14475 : 147 : tree device_num = NULL_TREE;
14476 : :
14477 : 147 : auto_vec<tree> init_objs, use_objs, destroy_objs, prefer_type,
14478 : 147 : target_targetsync;
14479 : 147 : gimple_seq dep_ilist = NULL, dep_olist = NULL;
14480 : 147 : tree clauses = gimple_omp_interop_clauses (gsi_stmt (*gsi_p));
14481 : 806 : for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14482 : : {
14483 : 659 : switch (OMP_CLAUSE_CODE (c))
14484 : : {
14485 : 375 : case OMP_CLAUSE_INIT:
14486 : 375 : {
14487 : 375 : init_objs.safe_push (c);
14488 : 375 : int target_targetsync_bits = 0;
14489 : 375 : if (OMP_CLAUSE_INIT_TARGET (c))
14490 : 179 : target_targetsync_bits |= GOMP_INTEROP_TARGET;
14491 : 375 : if (OMP_CLAUSE_INIT_TARGETSYNC (c))
14492 : 190 : target_targetsync_bits |= GOMP_INTEROP_TARGETSYNC;
14493 : 375 : tree t = build_int_cst (integer_type_node, target_targetsync_bits);
14494 : 375 : target_targetsync.safe_push (t);
14495 : 375 : prefer_type.safe_push (OMP_CLAUSE_INIT_PREFER_TYPE (c));
14496 : : }
14497 : 375 : break;
14498 : 95 : case OMP_CLAUSE_USE:
14499 : 95 : use_objs.safe_push (c);
14500 : 95 : break;
14501 : 92 : case OMP_CLAUSE_DESTROY:
14502 : 92 : destroy_objs.safe_push (c);
14503 : 92 : break;
14504 : 46 : case OMP_CLAUSE_NOWAIT:
14505 : 46 : flags = build_int_cst (integer_type_node, GOMP_INTEROP_FLAG_NOWAIT);
14506 : 46 : break;
14507 : 27 : case OMP_CLAUSE_DEPEND:
14508 : 27 : {
14509 : 27 : tree *cp = gimple_omp_interop_clauses_ptr (gsi_stmt (*gsi_p));
14510 : 27 : lower_depend_clauses (cp, &dep_ilist, &dep_olist);
14511 : 27 : depend = OMP_CLAUSE_DECL (*cp);
14512 : : }
14513 : 27 : break;
14514 : 24 : case OMP_CLAUSE_DEVICE:
14515 : 24 : device_num = OMP_CLAUSE_DEVICE_ID (c);
14516 : 24 : break;
14517 : 0 : default:
14518 : 0 : gcc_unreachable ();
14519 : : }
14520 : : }
14521 : :
14522 : 147 : if (flags == NULL_TREE)
14523 : 101 : flags = build_int_cst (integer_type_node, 0);
14524 : :
14525 : 147 : if (device_num == NULL_TREE)
14526 : 123 : device_num = build_int_cst (integer_type_node, GOMP_DEVICE_DEFAULT_OMP_61);
14527 : :
14528 : 278 : tree n_init = build_int_cst (integer_type_node, init_objs.length ());
14529 : 210 : tree n_use = build_int_cst (integer_type_node, use_objs.length ());
14530 : 199 : tree n_destroy = build_int_cst (integer_type_node, destroy_objs.length ());
14531 : :
14532 : 147 : lower_omp_interop_action_clauses (&bind_body, init_objs, &target_targetsync,
14533 : : &prefer_type);
14534 : 147 : lower_omp_interop_action_clauses (&bind_body, use_objs);
14535 : 147 : lower_omp_interop_action_clauses (&bind_body, destroy_objs);
14536 : :
14537 : 147 : gimple_seq_add_seq (&bind_body, dep_ilist);
14538 : 147 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
14539 : 278 : tree init_arg = init_objs.length () ? init_objs.pop () : null_pointer_node;
14540 : 147 : tree target_targetsync_arg = target_targetsync.length ()
14541 : 278 : ? target_targetsync.pop ()
14542 : 147 : : null_pointer_node;
14543 : 147 : tree prefer_type_arg
14544 : 278 : = prefer_type.length () ? prefer_type.pop () : null_pointer_node;
14545 : 210 : tree use_arg = use_objs.length () ? use_objs.pop () : null_pointer_node;
14546 : 147 : tree destroy_arg
14547 : 199 : = destroy_objs.length () ? destroy_objs.pop () : null_pointer_node;
14548 : 147 : gcall *call
14549 : 147 : = 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 : 147 : gimple_seq_add_stmt (&bind_body, call);
14553 : 147 : gimple_seq_add_seq (&bind_body, dep_olist);
14554 : :
14555 : 147 : gsi_replace (gsi_p, bind, true);
14556 : 147 : gimple_bind_set_body (bind, bind_body);
14557 : 147 : pop_gimplify_context (bind);
14558 : 147 : gimple_bind_append_vars (bind, ctx->block_vars);
14559 : 147 : BLOCK_VARS (block) = ctx->block_vars;
14560 : 147 : }
14561 : :
14562 : : /* Expand code for an OpenMP teams directive. */
14563 : :
14564 : : static void
14565 : 5888 : lower_omp_teams (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14566 : : {
14567 : 5888 : gomp_teams *teams_stmt = as_a <gomp_teams *> (gsi_stmt (*gsi_p));
14568 : 5888 : push_gimplify_context ();
14569 : :
14570 : 5888 : tree block = make_node (BLOCK);
14571 : 5888 : gbind *bind = gimple_build_bind (NULL, NULL, block);
14572 : 5888 : gsi_replace (gsi_p, bind, true);
14573 : 5888 : gimple_seq bind_body = NULL;
14574 : 5888 : gimple_seq dlist = NULL;
14575 : 5888 : gimple_seq olist = NULL;
14576 : :
14577 : 5888 : tree num_teams = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
14578 : 5888 : OMP_CLAUSE_NUM_TEAMS);
14579 : 5888 : tree num_teams_lower = NULL_TREE;
14580 : 5888 : if (num_teams == NULL_TREE)
14581 : 5271 : num_teams = build_int_cst (unsigned_type_node, 0);
14582 : : else
14583 : : {
14584 : 617 : num_teams_lower = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (num_teams);
14585 : 617 : if (num_teams_lower)
14586 : : {
14587 : 148 : num_teams_lower = fold_convert (unsigned_type_node, num_teams_lower);
14588 : 148 : gimplify_expr (&num_teams_lower, &bind_body, NULL, is_gimple_val,
14589 : : fb_rvalue);
14590 : : }
14591 : 617 : num_teams = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
14592 : 617 : num_teams = fold_convert (unsigned_type_node, num_teams);
14593 : 617 : gimplify_expr (&num_teams, &bind_body, NULL, is_gimple_val, fb_rvalue);
14594 : : }
14595 : 5888 : if (num_teams_lower == NULL_TREE)
14596 : 5740 : num_teams_lower = num_teams;
14597 : 5888 : tree thread_limit = omp_find_clause (gimple_omp_teams_clauses (teams_stmt),
14598 : 5888 : OMP_CLAUSE_THREAD_LIMIT);
14599 : 5888 : if (thread_limit == NULL_TREE)
14600 : 5417 : thread_limit = build_int_cst (unsigned_type_node, 0);
14601 : : else
14602 : : {
14603 : 471 : thread_limit = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
14604 : 471 : thread_limit = fold_convert (unsigned_type_node, thread_limit);
14605 : 471 : gimplify_expr (&thread_limit, &bind_body, NULL, is_gimple_val,
14606 : : fb_rvalue);
14607 : : }
14608 : 5888 : location_t loc = gimple_location (teams_stmt);
14609 : 5888 : tree decl = builtin_decl_explicit (BUILT_IN_GOMP_TEAMS4);
14610 : 5888 : tree rettype = TREE_TYPE (TREE_TYPE (decl));
14611 : 5888 : tree first = create_tmp_var (rettype);
14612 : 5888 : gimple_seq_add_stmt (&bind_body,
14613 : 5888 : gimple_build_assign (first, build_one_cst (rettype)));
14614 : 5888 : tree llabel = create_artificial_label (loc);
14615 : 5888 : gimple_seq_add_stmt (&bind_body, gimple_build_label (llabel));
14616 : 5888 : gimple *call
14617 : 5888 : = gimple_build_call (decl, 4, num_teams_lower, num_teams, thread_limit,
14618 : : first);
14619 : 5888 : gimple_set_location (call, loc);
14620 : 5888 : tree temp = create_tmp_var (rettype);
14621 : 5888 : gimple_call_set_lhs (call, temp);
14622 : 5888 : gimple_seq_add_stmt (&bind_body, call);
14623 : :
14624 : 5888 : tree tlabel = create_artificial_label (loc);
14625 : 5888 : tree flabel = create_artificial_label (loc);
14626 : 5888 : gimple *cond = gimple_build_cond (NE_EXPR, temp, build_zero_cst (rettype),
14627 : : tlabel, flabel);
14628 : 5888 : gimple_seq_add_stmt (&bind_body, cond);
14629 : 5888 : gimple_seq_add_stmt (&bind_body, gimple_build_label (tlabel));
14630 : 5888 : gimple_seq_add_stmt (&bind_body,
14631 : 5888 : gimple_build_assign (first, build_zero_cst (rettype)));
14632 : :
14633 : 5888 : lower_rec_input_clauses (gimple_omp_teams_clauses (teams_stmt),
14634 : : &bind_body, &dlist, ctx, NULL);
14635 : 5888 : lower_omp (gimple_omp_body_ptr (teams_stmt), ctx);
14636 : 5888 : lower_reduction_clauses (gimple_omp_teams_clauses (teams_stmt), &olist,
14637 : : NULL, ctx);
14638 : 5888 : gimple_seq_add_stmt (&bind_body, teams_stmt);
14639 : :
14640 : 5888 : gimple_seq_add_seq (&bind_body, gimple_omp_body (teams_stmt));
14641 : 5888 : gimple_omp_set_body (teams_stmt, NULL);
14642 : 5888 : gimple_seq_add_seq (&bind_body, olist);
14643 : 5888 : gimple_seq_add_seq (&bind_body, dlist);
14644 : 5888 : gimple_seq_add_stmt (&bind_body, gimple_build_omp_return (true));
14645 : 5888 : gimple_seq_add_stmt (&bind_body, gimple_build_goto (llabel));
14646 : 5888 : gimple_seq_add_stmt (&bind_body, gimple_build_label (flabel));
14647 : 5888 : gimple_bind_set_body (bind, bind_body);
14648 : :
14649 : 5888 : pop_gimplify_context (bind);
14650 : :
14651 : 5888 : gimple_bind_append_vars (bind, ctx->block_vars);
14652 : 5888 : BLOCK_VARS (block) = ctx->block_vars;
14653 : 5888 : if (BLOCK_VARS (block))
14654 : 673 : TREE_USED (block) = 1;
14655 : 5888 : }
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 : 3032304 : lower_omp_regimplify_p (tree *tp, int *walk_subtrees,
14663 : : void *data)
14664 : : {
14665 : 3032304 : tree t = *tp;
14666 : :
14667 : : /* Any variable with DECL_VALUE_EXPR needs to be regimplified. */
14668 : 3032304 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
14669 : 1421480 : && data == NULL
14670 : 1361124 : && DECL_HAS_VALUE_EXPR_P (t))
14671 : : return t;
14672 : :
14673 : 2937388 : if (make_addressable_vars
14674 : 421699 : && DECL_P (t)
14675 : 3142426 : && 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 : 2934819 : if (data == NULL && TREE_CODE (t) == ADDR_EXPR)
14681 : 102759 : recompute_tree_invariant_for_addr_expr (t);
14682 : :
14683 : 2934819 : *walk_subtrees = !IS_TYPE_OR_DECL_P (t);
14684 : 2934819 : 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 : 370412 : lower_omp_regimplify_operands_p (tree *tp, int *walk_subtrees,
14702 : : void *data)
14703 : : {
14704 : 370412 : tree t = omp_member_access_dummy_var (*tp);
14705 : 370412 : if (t)
14706 : : {
14707 : 6 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
14708 : 6 : lower_omp_regimplify_operands_data *ldata
14709 : : = (lower_omp_regimplify_operands_data *) wi->info;
14710 : 6 : tree o = maybe_lookup_decl (t, ldata->ctx);
14711 : 6 : if (o != t)
14712 : : {
14713 : 6 : ldata->decls->safe_push (DECL_VALUE_EXPR (*tp));
14714 : 6 : ldata->decls->safe_push (*tp);
14715 : 6 : tree v = unshare_and_remap (DECL_VALUE_EXPR (*tp), t, o);
14716 : 6 : SET_DECL_VALUE_EXPR (*tp, v);
14717 : : }
14718 : : }
14719 : 370412 : *walk_subtrees = !IS_TYPE_OR_DECL_P (*tp);
14720 : 370412 : return NULL_TREE;
14721 : : }
14722 : :
14723 : : /* Wrapper around gimple_regimplify_operands that adjusts DECL_VALUE_EXPRs
14724 : : of omp_member_access_dummy_var vars during regimplification. */
14725 : :
14726 : : static void
14727 : 97355 : lower_omp_regimplify_operands (omp_context *ctx, gimple *stmt,
14728 : : gimple_stmt_iterator *gsi_p)
14729 : : {
14730 : 97355 : auto_vec<tree, 10> decls;
14731 : 97355 : if (ctx)
14732 : : {
14733 : 94846 : struct walk_stmt_info wi;
14734 : 94846 : memset (&wi, '\0', sizeof (wi));
14735 : 94846 : struct lower_omp_regimplify_operands_data data;
14736 : 94846 : data.ctx = ctx;
14737 : 94846 : data.decls = &decls;
14738 : 94846 : wi.info = &data;
14739 : 94846 : walk_gimple_op (stmt, lower_omp_regimplify_operands_p, &wi);
14740 : : }
14741 : 97355 : gimple_regimplify_operands (stmt, gsi_p);
14742 : 292071 : while (!decls.is_empty ())
14743 : : {
14744 : 6 : tree t = decls.pop ();
14745 : 6 : tree v = decls.pop ();
14746 : 6 : SET_DECL_VALUE_EXPR (t, v);
14747 : : }
14748 : 97355 : }
14749 : :
14750 : : static void
14751 : 2319868 : lower_omp_1 (gimple_stmt_iterator *gsi_p, omp_context *ctx)
14752 : : {
14753 : 2319868 : gimple *stmt = gsi_stmt (*gsi_p);
14754 : 2319868 : struct walk_stmt_info wi;
14755 : 2319868 : gcall *call_stmt;
14756 : :
14757 : 2319868 : if (gimple_has_location (stmt))
14758 : 1897410 : input_location = gimple_location (stmt);
14759 : :
14760 : 2319868 : if (make_addressable_vars)
14761 : 154230 : memset (&wi, '\0', sizeof (wi));
14762 : :
14763 : : /* If we have issued syntax errors, avoid doing any heavy lifting.
14764 : : Just replace the OMP directives with a NOP to avoid
14765 : : confusing RTL expansion. */
14766 : 2319868 : if (seen_error () && is_gimple_omp (stmt))
14767 : : {
14768 : 11241 : gsi_replace (gsi_p, gimple_build_nop (), true);
14769 : 11241 : return;
14770 : : }
14771 : :
14772 : 2308627 : switch (gimple_code (stmt))
14773 : : {
14774 : 146938 : case GIMPLE_COND:
14775 : 146938 : {
14776 : 146938 : gcond *cond_stmt = as_a <gcond *> (stmt);
14777 : 82864 : if ((ctx || make_addressable_vars)
14778 : 150546 : && (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
14779 : : lower_omp_regimplify_p,
14780 : : ctx ? NULL : &wi, NULL)
14781 : 129813 : || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
14782 : : lower_omp_regimplify_p,
14783 : : ctx ? NULL : &wi, NULL)))
14784 : 1244 : lower_omp_regimplify_operands (ctx, cond_stmt, gsi_p);
14785 : : }
14786 : : break;
14787 : 15 : case GIMPLE_CATCH:
14788 : 15 : lower_omp (gimple_catch_handler_ptr (as_a <gcatch *> (stmt)), ctx);
14789 : 15 : break;
14790 : 0 : case GIMPLE_EH_FILTER:
14791 : 0 : lower_omp (gimple_eh_filter_failure_ptr (stmt), ctx);
14792 : 0 : break;
14793 : 23826 : case GIMPLE_TRY:
14794 : 23826 : lower_omp (gimple_try_eval_ptr (stmt), ctx);
14795 : 23821 : lower_omp (gimple_try_cleanup_ptr (stmt), ctx);
14796 : 23821 : break;
14797 : 0 : case GIMPLE_ASSUME:
14798 : 0 : lower_omp (gimple_assume_body_ptr (stmt), ctx);
14799 : 0 : break;
14800 : 5 : case GIMPLE_TRANSACTION:
14801 : 5 : lower_omp (gimple_transaction_body_ptr (as_a <gtransaction *> (stmt)),
14802 : : ctx);
14803 : 5 : break;
14804 : 159250 : case GIMPLE_BIND:
14805 : 159250 : if (ctx && is_gimple_omp_oacc (ctx->stmt))
14806 : : {
14807 : 17644 : tree vars = gimple_bind_vars (as_a <gbind *> (stmt));
14808 : 17644 : oacc_privatization_scan_decl_chain (ctx, vars);
14809 : : }
14810 : 159250 : lower_omp (gimple_bind_body_ptr (as_a <gbind *> (stmt)), ctx);
14811 : 159243 : maybe_remove_omp_member_access_dummy_vars (as_a <gbind *> (stmt));
14812 : 159243 : break;
14813 : 19952 : case GIMPLE_OMP_PARALLEL:
14814 : 19952 : case GIMPLE_OMP_TASK:
14815 : 19952 : ctx = maybe_lookup_ctx (stmt);
14816 : 19952 : gcc_assert (ctx);
14817 : 19952 : if (ctx->cancellable)
14818 : 135 : ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14819 : 19952 : lower_omp_taskreg (gsi_p, ctx);
14820 : 19952 : break;
14821 : 46913 : case GIMPLE_OMP_FOR:
14822 : 46913 : ctx = maybe_lookup_ctx (stmt);
14823 : 46913 : gcc_assert (ctx);
14824 : 46913 : if (ctx->cancellable)
14825 : 84 : ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14826 : 46913 : lower_omp_for (gsi_p, ctx);
14827 : 46913 : break;
14828 : 378 : case GIMPLE_OMP_SECTIONS:
14829 : 378 : ctx = maybe_lookup_ctx (stmt);
14830 : 378 : gcc_assert (ctx);
14831 : 378 : if (ctx->cancellable)
14832 : 19 : ctx->cancel_label = create_artificial_label (UNKNOWN_LOCATION);
14833 : 378 : lower_omp_sections (gsi_p, ctx);
14834 : 378 : break;
14835 : 133 : case GIMPLE_OMP_SCOPE:
14836 : 133 : ctx = maybe_lookup_ctx (stmt);
14837 : 133 : gcc_assert (ctx);
14838 : 133 : lower_omp_scope (gsi_p, ctx);
14839 : 133 : break;
14840 : 627 : case GIMPLE_OMP_DISPATCH:
14841 : 627 : ctx = maybe_lookup_ctx (stmt);
14842 : 627 : gcc_assert (ctx);
14843 : 627 : lower_omp_dispatch (gsi_p, ctx);
14844 : 627 : break;
14845 : 147 : case GIMPLE_OMP_INTEROP:
14846 : 147 : ctx = maybe_lookup_ctx (stmt);
14847 : 147 : gcc_assert (ctx);
14848 : 147 : lower_omp_interop (gsi_p, ctx);
14849 : 147 : break;
14850 : 1128 : case GIMPLE_OMP_SINGLE:
14851 : 1128 : ctx = maybe_lookup_ctx (stmt);
14852 : 1128 : gcc_assert (ctx);
14853 : 1128 : lower_omp_single (gsi_p, ctx);
14854 : 1128 : break;
14855 : 645 : case GIMPLE_OMP_STRUCTURED_BLOCK:
14856 : : /* We have already done error checking at this point, so these nodes
14857 : : can be completely removed and replaced with their body. */
14858 : 645 : ctx = maybe_lookup_ctx (stmt);
14859 : 645 : gcc_assert (ctx);
14860 : 645 : lower_omp (gimple_omp_body_ptr (stmt), ctx);
14861 : 645 : gsi_replace_with_seq (gsi_p, gimple_omp_body (stmt), true);
14862 : 645 : break;
14863 : 1079 : case GIMPLE_OMP_MASTER:
14864 : 1079 : case GIMPLE_OMP_MASKED:
14865 : 1079 : ctx = maybe_lookup_ctx (stmt);
14866 : 1079 : gcc_assert (ctx);
14867 : 1079 : lower_omp_master (gsi_p, ctx);
14868 : 1079 : break;
14869 : 536 : case GIMPLE_OMP_TASKGROUP:
14870 : 536 : ctx = maybe_lookup_ctx (stmt);
14871 : 536 : gcc_assert (ctx);
14872 : 536 : lower_omp_taskgroup (gsi_p, ctx);
14873 : 536 : break;
14874 : 1116 : case GIMPLE_OMP_ORDERED:
14875 : 1116 : ctx = maybe_lookup_ctx (stmt);
14876 : 1116 : gcc_assert (ctx);
14877 : 1116 : lower_omp_ordered (gsi_p, ctx);
14878 : 1116 : break;
14879 : 1268 : case GIMPLE_OMP_SCAN:
14880 : 1268 : ctx = maybe_lookup_ctx (stmt);
14881 : 1268 : gcc_assert (ctx);
14882 : 1268 : lower_omp_scan (gsi_p, ctx);
14883 : 1268 : break;
14884 : 311 : case GIMPLE_OMP_CRITICAL:
14885 : 311 : ctx = maybe_lookup_ctx (stmt);
14886 : 311 : gcc_assert (ctx);
14887 : 311 : lower_omp_critical (gsi_p, ctx);
14888 : 311 : break;
14889 : 2536 : case GIMPLE_OMP_ATOMIC_LOAD:
14890 : 85 : if ((ctx || make_addressable_vars)
14891 : 2536 : && walk_tree (gimple_omp_atomic_load_rhs_ptr (
14892 : : as_a <gomp_atomic_load *> (stmt)),
14893 : : lower_omp_regimplify_p, ctx ? NULL : &wi, NULL))
14894 : 1425 : lower_omp_regimplify_operands (ctx, stmt, gsi_p);
14895 : : break;
14896 : 35308 : case GIMPLE_OMP_TARGET:
14897 : 35308 : ctx = maybe_lookup_ctx (stmt);
14898 : 35308 : gcc_assert (ctx);
14899 : 35308 : lower_omp_target (gsi_p, ctx);
14900 : 35308 : break;
14901 : 8423 : case GIMPLE_OMP_TEAMS:
14902 : 8423 : ctx = maybe_lookup_ctx (stmt);
14903 : 8423 : gcc_assert (ctx);
14904 : 8423 : if (gimple_omp_teams_host (as_a <gomp_teams *> (stmt)))
14905 : 2535 : lower_omp_taskreg (gsi_p, ctx);
14906 : : else
14907 : 5888 : lower_omp_teams (gsi_p, ctx);
14908 : : break;
14909 : 123359 : case GIMPLE_CALL:
14910 : 123359 : tree fndecl;
14911 : 123359 : call_stmt = as_a <gcall *> (stmt);
14912 : 123359 : fndecl = gimple_call_fndecl (call_stmt);
14913 : 123359 : if (fndecl
14914 : 123359 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
14915 : 35322 : switch (DECL_FUNCTION_CODE (fndecl))
14916 : : {
14917 : 740 : case BUILT_IN_GOMP_BARRIER:
14918 : 740 : if (ctx == NULL)
14919 : : break;
14920 : : /* FALLTHRU */
14921 : 1034 : case BUILT_IN_GOMP_CANCEL:
14922 : 1034 : case BUILT_IN_GOMP_CANCELLATION_POINT:
14923 : 1034 : omp_context *cctx;
14924 : 1034 : cctx = ctx;
14925 : 1034 : if (gimple_code (cctx->stmt) == GIMPLE_OMP_SECTION)
14926 : 43 : cctx = cctx->outer;
14927 : 1034 : gcc_assert (gimple_call_lhs (call_stmt) == NULL_TREE);
14928 : 1034 : if (!cctx->cancellable)
14929 : : {
14930 : 676 : if (DECL_FUNCTION_CODE (fndecl)
14931 : : == BUILT_IN_GOMP_CANCELLATION_POINT)
14932 : : {
14933 : 0 : stmt = gimple_build_nop ();
14934 : 0 : gsi_replace (gsi_p, stmt, false);
14935 : : }
14936 : : break;
14937 : : }
14938 : 358 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_GOMP_BARRIER)
14939 : : {
14940 : 14 : fndecl = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER_CANCEL);
14941 : 14 : gimple_call_set_fndecl (call_stmt, fndecl);
14942 : 14 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fndecl));
14943 : : }
14944 : 358 : tree lhs;
14945 : 358 : lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (fndecl)));
14946 : 358 : gimple_call_set_lhs (call_stmt, lhs);
14947 : 358 : tree fallthru_label;
14948 : 358 : fallthru_label = create_artificial_label (UNKNOWN_LOCATION);
14949 : 358 : gimple *g;
14950 : 358 : g = gimple_build_label (fallthru_label);
14951 : 358 : gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
14952 : 358 : g = gimple_build_cond (NE_EXPR, lhs,
14953 : 358 : fold_convert (TREE_TYPE (lhs),
14954 : : boolean_false_node),
14955 : : cctx->cancel_label, fallthru_label);
14956 : 358 : gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
14957 : 358 : break;
14958 : : default:
14959 : : break;
14960 : : }
14961 : 123308 : goto regimplify;
14962 : :
14963 : : case GIMPLE_ASSIGN:
14964 : 1331977 : for (omp_context *up = ctx; up; up = up->outer)
14965 : : {
14966 : 649269 : if (gimple_code (up->stmt) == GIMPLE_OMP_ORDERED
14967 : : || gimple_code (up->stmt) == GIMPLE_OMP_CRITICAL
14968 : : || gimple_code (up->stmt) == GIMPLE_OMP_TASKGROUP
14969 : : || gimple_code (up->stmt) == GIMPLE_OMP_SCOPE
14970 : : || gimple_code (up->stmt) == GIMPLE_OMP_SECTION
14971 : : || gimple_code (up->stmt) == GIMPLE_OMP_SCAN
14972 : : || (gimple_code (up->stmt) == GIMPLE_OMP_TARGET
14973 : 195623 : && (gimple_omp_target_kind (up->stmt)
14974 : : == GF_OMP_TARGET_KIND_DATA)))
14975 : 89951 : continue;
14976 : 559318 : else if (!up->lastprivate_conditional_map)
14977 : : break;
14978 : 4204 : tree lhs = get_base_address (gimple_assign_lhs (stmt));
14979 : 4204 : if (TREE_CODE (lhs) == MEM_REF
14980 : 26 : && DECL_P (TREE_OPERAND (lhs, 0))
14981 : 4227 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs,
14982 : : 0))) == REFERENCE_TYPE)
14983 : 19 : lhs = TREE_OPERAND (lhs, 0);
14984 : 4204 : if (DECL_P (lhs))
14985 : 3161 : if (tree *v = up->lastprivate_conditional_map->get (lhs))
14986 : : {
14987 : 382 : tree clauses;
14988 : 382 : if (up->combined_into_simd_safelen1)
14989 : : {
14990 : 54 : up = up->outer;
14991 : 54 : if (gimple_code (up->stmt) == GIMPLE_OMP_SCAN)
14992 : 3 : up = up->outer;
14993 : : }
14994 : 382 : if (gimple_code (up->stmt) == GIMPLE_OMP_FOR)
14995 : 298 : clauses = gimple_omp_for_clauses (up->stmt);
14996 : : else
14997 : 84 : clauses = gimple_omp_sections_clauses (up->stmt);
14998 : 382 : tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
14999 : 382 : if (!OMP_CLAUSE__CONDTEMP__ITER (c))
15000 : 348 : c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
15001 : : OMP_CLAUSE__CONDTEMP_);
15002 : 382 : gcc_assert (OMP_CLAUSE__CONDTEMP__ITER (c));
15003 : 382 : gimple *g = gimple_build_assign (*v, OMP_CLAUSE_DECL (c));
15004 : 382 : gsi_insert_after (gsi_p, g, GSI_SAME_STMT);
15005 : : }
15006 : : }
15007 : : /* FALLTHRU */
15008 : :
15009 : 1858093 : default:
15010 : 1858093 : regimplify:
15011 : 952417 : if ((ctx || make_addressable_vars)
15012 : 1933798 : && walk_gimple_op (stmt, lower_omp_regimplify_p,
15013 : : ctx ? NULL : &wi))
15014 : : {
15015 : : /* Just remove clobbers, this should happen only if we have
15016 : : "privatized" local addressable variables in SIMD regions,
15017 : : the clobber isn't needed in that case and gimplifying address
15018 : : of the ARRAY_REF into a pointer and creating MEM_REF based
15019 : : clobber would create worse code than we get with the clobber
15020 : : dropped. */
15021 : 94816 : if (gimple_clobber_p (stmt))
15022 : : {
15023 : 130 : gsi_replace (gsi_p, gimple_build_nop (), true);
15024 : 130 : break;
15025 : : }
15026 : 94686 : lower_omp_regimplify_operands (ctx, stmt, gsi_p);
15027 : : }
15028 : : break;
15029 : : }
15030 : : }
15031 : :
15032 : : static void
15033 : 395211 : lower_omp (gimple_seq *body, omp_context *ctx)
15034 : : {
15035 : 395211 : location_t saved_location = input_location;
15036 : 395211 : gimple_stmt_iterator gsi;
15037 : 3061369 : for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
15038 : 2319868 : lower_omp_1 (&gsi, ctx);
15039 : : /* During gimplification, we haven't folded statments inside offloading
15040 : : or taskreg regions (gimplify.cc:maybe_fold_stmt); do that now. */
15041 : 395195 : if (target_nesting_level || taskreg_nesting_level)
15042 : 580571 : for (gsi = gsi_start (*body); !gsi_end_p (gsi); gsi_next (&gsi))
15043 : 379163 : fold_stmt (&gsi);
15044 : 395195 : input_location = saved_location;
15045 : 395195 : }
15046 : :
15047 : : /* Main entry point. */
15048 : :
15049 : : static unsigned int
15050 : 2765163 : execute_lower_omp (void)
15051 : : {
15052 : 2765163 : gimple_seq body;
15053 : 2765163 : int i;
15054 : 2765163 : omp_context *ctx;
15055 : :
15056 : : /* This pass always runs, to provide PROP_gimple_lomp.
15057 : : But often, there is nothing to do. */
15058 : 2765163 : if (flag_openacc == 0 && flag_openmp == 0
15059 : 2721814 : && flag_openmp_simd == 0)
15060 : : return 0;
15061 : :
15062 : 45604 : all_contexts = splay_tree_new (splay_tree_compare_pointers, 0,
15063 : : delete_omp_context);
15064 : :
15065 : 45604 : body = gimple_body (current_function_decl);
15066 : :
15067 : 45604 : scan_omp (&body, NULL);
15068 : 45604 : gcc_assert (taskreg_nesting_level == 0);
15069 : 71786 : FOR_EACH_VEC_ELT (taskreg_contexts, i, ctx)
15070 : 26182 : finish_taskreg_scan (ctx);
15071 : 45604 : taskreg_contexts.release ();
15072 : :
15073 : 45604 : if (all_contexts->root)
15074 : : {
15075 : 24736 : if (make_addressable_vars)
15076 : 631 : push_gimplify_context ();
15077 : 24736 : lower_omp (&body, NULL);
15078 : 24732 : if (make_addressable_vars)
15079 : 631 : pop_gimplify_context (NULL);
15080 : : }
15081 : :
15082 : 45600 : if (all_contexts)
15083 : : {
15084 : 45600 : splay_tree_delete (all_contexts);
15085 : 45600 : all_contexts = NULL;
15086 : : }
15087 : 45600 : BITMAP_FREE (make_addressable_vars);
15088 : 45600 : BITMAP_FREE (global_nonaddressable_vars);
15089 : :
15090 : : /* If current function is a method, remove artificial dummy VAR_DECL created
15091 : : for non-static data member privatization, they aren't needed for
15092 : : debuginfo nor anything else, have been already replaced everywhere in the
15093 : : IL and cause problems with LTO. */
15094 : 45600 : if (DECL_ARGUMENTS (current_function_decl)
15095 : 23277 : && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
15096 : 50000 : && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
15097 : : == POINTER_TYPE))
15098 : 3865 : remove_member_access_dummy_vars (DECL_INITIAL (current_function_decl));
15099 : :
15100 : 46600 : for (auto task_stmt : task_cpyfns)
15101 : 518 : finalize_task_copyfn (task_stmt);
15102 : 45600 : task_cpyfns.release ();
15103 : 45600 : return 0;
15104 : : }
15105 : :
15106 : : namespace {
15107 : :
15108 : : const pass_data pass_data_lower_omp =
15109 : : {
15110 : : GIMPLE_PASS, /* type */
15111 : : "omplower", /* name */
15112 : : OPTGROUP_OMP, /* optinfo_flags */
15113 : : TV_NONE, /* tv_id */
15114 : : PROP_gimple_any, /* properties_required */
15115 : : PROP_gimple_lomp | PROP_gimple_lomp_dev, /* properties_provided */
15116 : : 0, /* properties_destroyed */
15117 : : 0, /* todo_flags_start */
15118 : : 0, /* todo_flags_finish */
15119 : : };
15120 : :
15121 : : class pass_lower_omp : public gimple_opt_pass
15122 : : {
15123 : : public:
15124 : 283157 : pass_lower_omp (gcc::context *ctxt)
15125 : 566314 : : gimple_opt_pass (pass_data_lower_omp, ctxt)
15126 : : {}
15127 : :
15128 : : /* opt_pass methods: */
15129 : 2765163 : unsigned int execute (function *) final override
15130 : : {
15131 : 2765163 : return execute_lower_omp ();
15132 : : }
15133 : :
15134 : : }; // class pass_lower_omp
15135 : :
15136 : : } // anon namespace
15137 : :
15138 : : gimple_opt_pass *
15139 : 283157 : make_pass_lower_omp (gcc::context *ctxt)
15140 : : {
15141 : 283157 : return new pass_lower_omp (ctxt);
15142 : : }
15143 : :
15144 : : /* The following is a utility to diagnose structured block violations.
15145 : : It is not part of the "omplower" pass, as that's invoked too late. It
15146 : : should be invoked by the respective front ends after gimplification. */
15147 : :
15148 : : static splay_tree all_labels;
15149 : :
15150 : : /* Check for mismatched contexts and generate an error if needed. Return
15151 : : true if an error is detected. */
15152 : :
15153 : : static bool
15154 : 507200 : diagnose_sb_0 (gimple_stmt_iterator *gsi_p,
15155 : : gimple *branch_ctx, gimple *label_ctx)
15156 : : {
15157 : 507200 : gcc_checking_assert (!branch_ctx || is_gimple_omp (branch_ctx));
15158 : 507200 : gcc_checking_assert (!label_ctx || is_gimple_omp (label_ctx));
15159 : :
15160 : 507200 : if (label_ctx == branch_ctx)
15161 : : return false;
15162 : :
15163 : 174 : const char* kind = NULL;
15164 : :
15165 : 174 : if (flag_openacc)
15166 : : {
15167 : 7 : if ((branch_ctx && is_gimple_omp_oacc (branch_ctx))
15168 : 16 : || (label_ctx && is_gimple_omp_oacc (label_ctx)))
15169 : : {
15170 : : gcc_checking_assert (kind == NULL);
15171 : : kind = "OpenACC";
15172 : : }
15173 : : }
15174 : : if (kind == NULL)
15175 : : {
15176 : 158 : gcc_checking_assert (flag_openmp || flag_openmp_simd);
15177 : : kind = "OpenMP";
15178 : : }
15179 : :
15180 : : /* Previously we kept track of the label's entire context in diagnose_sb_[12]
15181 : : so we could traverse it and issue a correct "exit" or "enter" error
15182 : : message upon a structured block violation.
15183 : :
15184 : : We built the context by building a list with tree_cons'ing, but there is
15185 : : no easy counterpart in gimple tuples. It seems like far too much work
15186 : : for issuing exit/enter error messages. If someone really misses the
15187 : : distinct error message... patches welcome. */
15188 : :
15189 : : #if 0
15190 : : /* Try to avoid confusing the user by producing and error message
15191 : : with correct "exit" or "enter" verbiage. We prefer "exit"
15192 : : unless we can show that LABEL_CTX is nested within BRANCH_CTX. */
15193 : : if (branch_ctx == NULL)
15194 : : exit_p = false;
15195 : : else
15196 : : {
15197 : : while (label_ctx)
15198 : : {
15199 : : if (TREE_VALUE (label_ctx) == branch_ctx)
15200 : : {
15201 : : exit_p = false;
15202 : : break;
15203 : : }
15204 : : label_ctx = TREE_CHAIN (label_ctx);
15205 : : }
15206 : : }
15207 : :
15208 : : if (exit_p)
15209 : : error ("invalid exit from %s structured block", kind);
15210 : : else
15211 : : error ("invalid entry to %s structured block", kind);
15212 : : #endif
15213 : :
15214 : : /* If it's obvious we have an invalid entry, be specific about the error. */
15215 : 174 : if (branch_ctx == NULL)
15216 : 53 : error ("invalid entry to %s structured block", kind);
15217 : : else
15218 : : {
15219 : : /* Otherwise, be vague and lazy, but efficient. */
15220 : 121 : error ("invalid branch to/from %s structured block", kind);
15221 : : }
15222 : :
15223 : 174 : gsi_replace (gsi_p, gimple_build_nop (), false);
15224 : 174 : return true;
15225 : : }
15226 : :
15227 : : /* Pass 1: Create a minimal tree of structured blocks, and record
15228 : : where each label is found. */
15229 : :
15230 : : static tree
15231 : 2988943 : diagnose_sb_1 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
15232 : : struct walk_stmt_info *wi)
15233 : : {
15234 : 2988943 : gimple *context = (gimple *) wi->info;
15235 : 2988943 : gimple *inner_context;
15236 : 2988943 : gimple *stmt = gsi_stmt (*gsi_p);
15237 : :
15238 : 2988943 : *handled_ops_p = true;
15239 : :
15240 : 2988943 : switch (gimple_code (stmt))
15241 : : {
15242 : 283118 : WALK_SUBSTMTS;
15243 : :
15244 : 81533 : case GIMPLE_OMP_PARALLEL:
15245 : 81533 : case GIMPLE_OMP_TASK:
15246 : 81533 : case GIMPLE_OMP_SCOPE:
15247 : 81533 : case GIMPLE_OMP_SECTIONS:
15248 : 81533 : case GIMPLE_OMP_SINGLE:
15249 : 81533 : case GIMPLE_OMP_SECTION:
15250 : 81533 : case GIMPLE_OMP_STRUCTURED_BLOCK:
15251 : 81533 : case GIMPLE_OMP_MASTER:
15252 : 81533 : case GIMPLE_OMP_MASKED:
15253 : 81533 : case GIMPLE_OMP_ORDERED:
15254 : 81533 : case GIMPLE_OMP_SCAN:
15255 : 81533 : case GIMPLE_OMP_CRITICAL:
15256 : 81533 : case GIMPLE_OMP_TARGET:
15257 : 81533 : case GIMPLE_OMP_TEAMS:
15258 : 81533 : case GIMPLE_OMP_TASKGROUP:
15259 : : /* The minimal context here is just the current OMP construct. */
15260 : 81533 : inner_context = stmt;
15261 : 81533 : wi->info = inner_context;
15262 : 81533 : walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
15263 : 81533 : wi->info = context;
15264 : 81533 : break;
15265 : :
15266 : 52758 : case GIMPLE_OMP_FOR:
15267 : 52758 : inner_context = stmt;
15268 : 52758 : wi->info = inner_context;
15269 : : /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
15270 : : walk them. */
15271 : 52758 : walk_gimple_seq (gimple_omp_for_pre_body (stmt),
15272 : : diagnose_sb_1, NULL, wi);
15273 : 52758 : walk_gimple_seq (gimple_omp_body (stmt), diagnose_sb_1, NULL, wi);
15274 : 52758 : wi->info = context;
15275 : 52758 : break;
15276 : :
15277 : 510828 : case GIMPLE_LABEL:
15278 : 1021656 : splay_tree_insert (all_labels,
15279 : 510828 : (splay_tree_key) gimple_label_label (
15280 : 510828 : as_a <glabel *> (stmt)),
15281 : : (splay_tree_value) context);
15282 : 510828 : break;
15283 : :
15284 : : default:
15285 : : break;
15286 : : }
15287 : :
15288 : 2988943 : return NULL_TREE;
15289 : : }
15290 : :
15291 : : /* Pass 2: Check each branch and see if its context differs from that of
15292 : : the destination label's context. */
15293 : :
15294 : : static tree
15295 : 2988943 : diagnose_sb_2 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
15296 : : struct walk_stmt_info *wi)
15297 : : {
15298 : 2988943 : gimple *context = (gimple *) wi->info;
15299 : 2988943 : splay_tree_node n;
15300 : 2988943 : gimple *stmt = gsi_stmt (*gsi_p);
15301 : :
15302 : 2988943 : *handled_ops_p = true;
15303 : :
15304 : 2988943 : switch (gimple_code (stmt))
15305 : : {
15306 : 283118 : WALK_SUBSTMTS;
15307 : :
15308 : 81533 : case GIMPLE_OMP_PARALLEL:
15309 : 81533 : case GIMPLE_OMP_TASK:
15310 : 81533 : case GIMPLE_OMP_SCOPE:
15311 : 81533 : case GIMPLE_OMP_SECTIONS:
15312 : 81533 : case GIMPLE_OMP_SINGLE:
15313 : 81533 : case GIMPLE_OMP_SECTION:
15314 : 81533 : case GIMPLE_OMP_STRUCTURED_BLOCK:
15315 : 81533 : case GIMPLE_OMP_MASTER:
15316 : 81533 : case GIMPLE_OMP_MASKED:
15317 : 81533 : case GIMPLE_OMP_ORDERED:
15318 : 81533 : case GIMPLE_OMP_SCAN:
15319 : 81533 : case GIMPLE_OMP_CRITICAL:
15320 : 81533 : case GIMPLE_OMP_TARGET:
15321 : 81533 : case GIMPLE_OMP_TEAMS:
15322 : 81533 : case GIMPLE_OMP_TASKGROUP:
15323 : 81533 : wi->info = stmt;
15324 : 81533 : walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
15325 : 81533 : wi->info = context;
15326 : 81533 : break;
15327 : :
15328 : 52758 : case GIMPLE_OMP_FOR:
15329 : 52758 : wi->info = stmt;
15330 : : /* gimple_omp_for_{index,initial,final} are all DECLs; no need to
15331 : : walk them. */
15332 : 52758 : walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt),
15333 : : diagnose_sb_2, NULL, wi);
15334 : 52758 : walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), diagnose_sb_2, NULL, wi);
15335 : 52758 : wi->info = context;
15336 : 52758 : break;
15337 : :
15338 : 189065 : case GIMPLE_COND:
15339 : 189065 : {
15340 : 189065 : gcond *cond_stmt = as_a <gcond *> (stmt);
15341 : 189065 : tree lab = gimple_cond_true_label (cond_stmt);
15342 : 189065 : if (lab)
15343 : : {
15344 : 189065 : n = splay_tree_lookup (all_labels,
15345 : : (splay_tree_key) lab);
15346 : 378130 : diagnose_sb_0 (gsi_p, context,
15347 : 189065 : n ? (gimple *) n->value : NULL);
15348 : : }
15349 : 189065 : lab = gimple_cond_false_label (cond_stmt);
15350 : 189065 : if (lab)
15351 : : {
15352 : 189065 : n = splay_tree_lookup (all_labels,
15353 : : (splay_tree_key) lab);
15354 : 378130 : diagnose_sb_0 (gsi_p, context,
15355 : 189065 : n ? (gimple *) n->value : NULL);
15356 : : }
15357 : : }
15358 : : break;
15359 : :
15360 : 101658 : case GIMPLE_GOTO:
15361 : 101658 : {
15362 : 101658 : tree lab = gimple_goto_dest (stmt);
15363 : 101658 : if (TREE_CODE (lab) != LABEL_DECL)
15364 : : break;
15365 : :
15366 : 101658 : n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
15367 : 101658 : diagnose_sb_0 (gsi_p, context, n ? (gimple *) n->value : NULL);
15368 : : }
15369 : 101658 : break;
15370 : :
15371 : 420 : case GIMPLE_SWITCH:
15372 : 420 : {
15373 : 420 : gswitch *switch_stmt = as_a <gswitch *> (stmt);
15374 : 420 : unsigned int i;
15375 : 1691 : for (i = 0; i < gimple_switch_num_labels (switch_stmt); ++i)
15376 : : {
15377 : 1287 : tree lab = CASE_LABEL (gimple_switch_label (switch_stmt, i));
15378 : 1287 : n = splay_tree_lookup (all_labels, (splay_tree_key) lab);
15379 : 1287 : if (n && diagnose_sb_0 (gsi_p, context, (gimple *) n->value))
15380 : : break;
15381 : : }
15382 : : }
15383 : : break;
15384 : :
15385 : 26125 : case GIMPLE_RETURN:
15386 : 26125 : diagnose_sb_0 (gsi_p, context, NULL);
15387 : 26125 : break;
15388 : :
15389 : : default:
15390 : : break;
15391 : : }
15392 : :
15393 : 2988943 : return NULL_TREE;
15394 : : }
15395 : :
15396 : : static unsigned int
15397 : 45614 : diagnose_omp_structured_block_errors (void)
15398 : : {
15399 : 45614 : struct walk_stmt_info wi;
15400 : 45614 : gimple_seq body = gimple_body (current_function_decl);
15401 : :
15402 : 45614 : all_labels = splay_tree_new (splay_tree_compare_pointers, 0, 0);
15403 : :
15404 : 45614 : memset (&wi, 0, sizeof (wi));
15405 : 45614 : walk_gimple_seq (body, diagnose_sb_1, NULL, &wi);
15406 : :
15407 : 45614 : memset (&wi, 0, sizeof (wi));
15408 : 45614 : wi.want_locations = true;
15409 : 45614 : walk_gimple_seq_mod (&body, diagnose_sb_2, NULL, &wi);
15410 : :
15411 : 45614 : gimple_set_body (current_function_decl, body);
15412 : :
15413 : 45614 : splay_tree_delete (all_labels);
15414 : 45614 : all_labels = NULL;
15415 : :
15416 : 45614 : return 0;
15417 : : }
15418 : :
15419 : : namespace {
15420 : :
15421 : : const pass_data pass_data_diagnose_omp_blocks =
15422 : : {
15423 : : GIMPLE_PASS, /* type */
15424 : : "*diagnose_omp_blocks", /* name */
15425 : : OPTGROUP_OMP, /* optinfo_flags */
15426 : : TV_NONE, /* tv_id */
15427 : : PROP_gimple_any, /* properties_required */
15428 : : 0, /* properties_provided */
15429 : : 0, /* properties_destroyed */
15430 : : 0, /* todo_flags_start */
15431 : : 0, /* todo_flags_finish */
15432 : : };
15433 : :
15434 : : class pass_diagnose_omp_blocks : public gimple_opt_pass
15435 : : {
15436 : : public:
15437 : 283157 : pass_diagnose_omp_blocks (gcc::context *ctxt)
15438 : 566314 : : gimple_opt_pass (pass_data_diagnose_omp_blocks, ctxt)
15439 : : {}
15440 : :
15441 : : /* opt_pass methods: */
15442 : 2765178 : bool gate (function *) final override
15443 : : {
15444 : 2765178 : return flag_openacc || flag_openmp || flag_openmp_simd;
15445 : : }
15446 : 45614 : unsigned int execute (function *) final override
15447 : : {
15448 : 45614 : return diagnose_omp_structured_block_errors ();
15449 : : }
15450 : :
15451 : : }; // class pass_diagnose_omp_blocks
15452 : :
15453 : : } // anon namespace
15454 : :
15455 : : gimple_opt_pass *
15456 : 283157 : make_pass_diagnose_omp_blocks (gcc::context *ctxt)
15457 : : {
15458 : 283157 : return new pass_diagnose_omp_blocks (ctxt);
15459 : : }
15460 : :
15461 : :
15462 : : #include "gt-omp-low.h"
|