Line data Source code
1 : /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 : tree representation into the GIMPLE form.
3 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
4 : Major work done by Sebastian Pop <s.pop@laposte.net>,
5 : Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
6 :
7 : This file is part of GCC.
8 :
9 : GCC is free software; you can redistribute it and/or modify it under
10 : the terms of the GNU General Public License as published by the Free
11 : Software Foundation; either version 3, or (at your option) any later
12 : version.
13 :
14 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with GCC; see the file COPYING3. If not see
21 : <http://www.gnu.org/licenses/>. */
22 :
23 : #include "config.h"
24 : #include "system.h"
25 : #include "coretypes.h"
26 : #include "backend.h"
27 : #include "target.h"
28 : #include "rtl.h"
29 : #include "tree.h"
30 : #include "memmodel.h"
31 : #include "tm_p.h"
32 : #include "gimple.h"
33 : #include "gimple-predict.h"
34 : #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 : #include "ssa.h"
36 : #include "cgraph.h"
37 : #include "tree-pretty-print.h"
38 : #include "diagnostic-core.h"
39 : #include "diagnostic.h" /* For errorcount. */
40 : #include "alias.h"
41 : #include "fold-const.h"
42 : #include "calls.h"
43 : #include "varasm.h"
44 : #include "stmt.h"
45 : #include "expr.h"
46 : #include "gimple-iterator.h"
47 : #include "gimple-fold.h"
48 : #include "tree-eh.h"
49 : #include "gimplify.h"
50 : #include "stor-layout.h"
51 : #include "print-tree.h"
52 : #include "tree-iterator.h"
53 : #include "tree-inline.h"
54 : #include "langhooks.h"
55 : #include "tree-cfg.h"
56 : #include "tree-ssa.h"
57 : #include "tree-hash-traits.h"
58 : #include "omp-general.h"
59 : #include "omp-low.h"
60 : #include "gimple-low.h"
61 : #include "gomp-constants.h"
62 : #include "splay-tree.h"
63 : #include "gimple-walk.h"
64 : #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
65 : #include "builtins.h"
66 : #include "stringpool.h"
67 : #include "attribs.h"
68 : #include "asan.h"
69 : #include "dbgcnt.h"
70 : #include "omp-offload.h"
71 : #include "context.h"
72 : #include "tree-nested.h"
73 : #include "gcc-urlifier.h"
74 : #include "insn-config.h"
75 : #include "recog.h"
76 : #include "output.h"
77 : #include "gimplify_reg_info.h"
78 : #include "tree-ssa-loop-niter.h" /* For simplify_replace_tree. */
79 :
80 : /* Identifier for a basic condition, mapping it to other basic conditions of
81 : its Boolean expression. Basic conditions given the same uid (in the same
82 : function) are parts of the same ANDIF/ORIF expression. Used for condition
83 : coverage. */
84 : static unsigned nextconduid = 1;
85 :
86 : /* Annotated gconds so that basic conditions in the same expression map to
87 : the same uid. This is used for condition coverage. */
88 : static hash_map <tree, unsigned> *cond_uids;
89 :
90 : /* Get a fresh identifier for a new condition expression. This is used for
91 : condition coverage. */
92 : static unsigned
93 5882246 : next_cond_uid ()
94 : {
95 5882246 : return nextconduid++;
96 : }
97 :
98 : /* Reset the condition uid to the value it should have when compiling a new
99 : function. 0 is already the default/untouched value, so start at non-zero.
100 : A valid and set id should always be > 0. This is used for condition
101 : coverage. */
102 : static void
103 3025267 : reset_cond_uid ()
104 : {
105 3025267 : nextconduid = 1;
106 0 : }
107 :
108 : /* Associate the condition STMT with the discriminator UID. STMTs that are
109 : broken down with ANDIF/ORIF from the same Boolean expression should be given
110 : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
111 : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
112 : for condition coverage. */
113 : static void
114 1223994 : tree_associate_condition_with_expr (tree stmt, unsigned uid)
115 : {
116 1223994 : if (!condition_coverage_flag)
117 : return;
118 :
119 542 : if (!cond_uids)
120 68 : cond_uids = new hash_map <tree, unsigned> ();
121 :
122 542 : cond_uids->put (stmt, uid);
123 : }
124 :
125 : /* Hash set of poisoned variables in a bind expr. */
126 : static hash_set<tree> *asan_poisoned_variables = NULL;
127 :
128 : /* Hash set of already-resolved calls to OpenMP "declare variant"
129 : functions. A call can resolve to the original function and
130 : we don't want to repeat the resolution multiple times. */
131 : static hash_set<tree> *omp_resolved_variant_calls = NULL;
132 :
133 : enum gimplify_omp_var_data
134 : {
135 : GOVD_SEEN = 0x000001,
136 : GOVD_EXPLICIT = 0x000002,
137 : GOVD_SHARED = 0x000004,
138 : GOVD_PRIVATE = 0x000008,
139 : GOVD_FIRSTPRIVATE = 0x000010,
140 : GOVD_LASTPRIVATE = 0x000020,
141 : GOVD_REDUCTION = 0x000040,
142 : GOVD_LOCAL = 0x00080,
143 : GOVD_MAP = 0x000100,
144 : GOVD_DEBUG_PRIVATE = 0x000200,
145 : GOVD_PRIVATE_OUTER_REF = 0x000400,
146 : GOVD_LINEAR = 0x000800,
147 : GOVD_ALIGNED = 0x001000,
148 :
149 : /* Flag for GOVD_MAP: don't copy back. */
150 : GOVD_MAP_TO_ONLY = 0x002000,
151 :
152 : /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
153 : GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
154 :
155 : GOVD_MAP_0LEN_ARRAY = 0x008000,
156 :
157 : /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
158 : GOVD_MAP_ALWAYS_TO = 0x010000,
159 :
160 : /* Flag for shared vars that are or might be stored to in the region. */
161 : GOVD_WRITTEN = 0x020000,
162 :
163 : /* Flag for GOVD_MAP, if it is a forced mapping. */
164 : GOVD_MAP_FORCE = 0x040000,
165 :
166 : /* Flag for GOVD_MAP: must be present already. */
167 : GOVD_MAP_FORCE_PRESENT = 0x080000,
168 :
169 : /* Flag for GOVD_MAP: only allocate. */
170 : GOVD_MAP_ALLOC_ONLY = 0x100000,
171 :
172 : /* Flag for GOVD_MAP: only copy back. */
173 : GOVD_MAP_FROM_ONLY = 0x200000,
174 :
175 : GOVD_NONTEMPORAL = 0x400000,
176 :
177 : /* Flag for GOVD_LASTPRIVATE: conditional modifier. */
178 : GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
179 :
180 : GOVD_CONDTEMP = 0x1000000,
181 :
182 : /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause. */
183 : GOVD_REDUCTION_INSCAN = 0x2000000,
184 :
185 : /* Flag for GOVD_FIRSTPRIVATE: OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT. */
186 : GOVD_FIRSTPRIVATE_IMPLICIT = 0x4000000,
187 :
188 : /* Flag to indicate this an allocator for the uses_allocators clause. */
189 : GOVD_USES_ALLOCATORS_ALLOCATOR = 0x8000000,
190 :
191 : GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
192 : | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
193 : | GOVD_LOCAL)
194 : };
195 :
196 :
197 : enum omp_region_type
198 : {
199 : ORT_WORKSHARE = 0x00,
200 : ORT_TASKGROUP = 0x01,
201 : ORT_DISPATCH = 0x02,
202 : ORT_SIMD = 0x04,
203 :
204 : ORT_PARALLEL = 0x08,
205 : ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
206 :
207 : ORT_TASK = 0x10,
208 : ORT_UNTIED_TASK = ORT_TASK | 1,
209 : ORT_TASKLOOP = ORT_TASK | 2,
210 : ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
211 :
212 : ORT_TEAMS = 0x20,
213 : ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
214 : ORT_HOST_TEAMS = ORT_TEAMS | 2,
215 : ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
216 :
217 : /* Data region. */
218 : ORT_TARGET_DATA = 0x40,
219 :
220 : /* Data region with offloading. */
221 : ORT_TARGET = 0x80,
222 : ORT_COMBINED_TARGET = ORT_TARGET | 1,
223 : ORT_IMPLICIT_TARGET = ORT_TARGET | 2,
224 :
225 : /* OpenACC variants. */
226 : ORT_ACC = 0x100, /* A generic OpenACC region. */
227 : ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
228 : ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
229 : ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
230 : ORT_ACC_SERIAL = ORT_ACC | ORT_TARGET | 4, /* Serial construct. */
231 : ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
232 :
233 : /* Dummy OpenMP region, used to disable expansion of
234 : DECL_VALUE_EXPRs in taskloop pre body. */
235 : ORT_NONE = 0x200
236 : };
237 :
238 : /* Gimplify hashtable helper. */
239 :
240 : struct gimplify_hasher : free_ptr_hash <elt_t>
241 : {
242 : static inline hashval_t hash (const elt_t *);
243 : static inline bool equal (const elt_t *, const elt_t *);
244 : };
245 :
246 : struct gimplify_ctx
247 : {
248 : struct gimplify_ctx *prev_context;
249 :
250 : vec<gbind *> bind_expr_stack;
251 : tree temps;
252 : gimple_seq conditional_cleanups;
253 : tree exit_label;
254 : tree return_temp;
255 :
256 : vec<tree> case_labels;
257 : hash_set<tree> *live_switch_vars;
258 : /* The formal temporary table. Should this be persistent? */
259 : hash_table<gimplify_hasher> *temp_htab;
260 :
261 : int conditions;
262 : unsigned into_ssa : 1;
263 : unsigned allow_rhs_cond_expr : 1;
264 : unsigned in_cleanup_point_expr : 1;
265 : unsigned keep_stack : 1;
266 : unsigned save_stack : 1;
267 : unsigned in_switch_expr : 1;
268 : unsigned in_handler_expr : 1;
269 : };
270 :
271 : enum gimplify_defaultmap_kind
272 : {
273 : GDMK_SCALAR,
274 : GDMK_SCALAR_TARGET, /* w/ Fortran's target attr, implicit mapping, only. */
275 : GDMK_AGGREGATE,
276 : GDMK_ALLOCATABLE,
277 : GDMK_POINTER
278 : };
279 :
280 : struct gimplify_omp_ctx
281 : {
282 : struct gimplify_omp_ctx *outer_context;
283 : splay_tree variables;
284 : hash_map<omp_name_type<tree>, tree> *implicit_mappers;
285 : hash_set<tree> *privatized_types;
286 : tree clauses;
287 : /* Iteration variables in an OMP_FOR. */
288 : vec<tree> loop_iter_var;
289 : location_t location;
290 : enum omp_clause_default_kind default_kind;
291 : enum omp_region_type region_type;
292 : enum tree_code code;
293 : bool combined_loop;
294 : bool distribute;
295 : bool target_firstprivatize_array_bases;
296 : bool add_safelen1;
297 : bool order_concurrent;
298 : bool has_depend;
299 : bool in_for_exprs;
300 : bool in_call_args;
301 : int defaultmap[5];
302 : };
303 :
304 : static struct gimplify_ctx *gimplify_ctxp;
305 : static struct gimplify_omp_ctx *gimplify_omp_ctxp;
306 : static bool in_omp_construct;
307 :
308 : /* Forward declaration. */
309 : static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
310 : static hash_map<tree, tree> *oacc_declare_returns;
311 : static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
312 : bool (*) (tree), fallback_t, bool);
313 : static void prepare_gimple_addressable (tree *, gimple_seq *);
314 :
315 : /* Shorter alias name for the above function for use in gimplify.cc
316 : only. */
317 :
318 : static inline void
319 94550170 : gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
320 : {
321 94550170 : gimple_seq_add_stmt_without_update (seq_p, gs);
322 : }
323 :
324 : /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
325 : NULL, a new sequence is allocated. This function is
326 : similar to gimple_seq_add_seq, but does not scan the operands.
327 : During gimplification, we need to manipulate statement sequences
328 : before the def/use vectors have been constructed. */
329 :
330 : static void
331 9555308 : gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
332 : {
333 9555308 : gimple_stmt_iterator si;
334 :
335 9555308 : if (src == NULL)
336 4097335 : return;
337 :
338 5457973 : si = gsi_last (*dst_p);
339 5457973 : gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
340 : }
341 :
342 :
343 : /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
344 : and popping gimplify contexts. */
345 :
346 : static struct gimplify_ctx *ctx_pool = NULL;
347 :
348 : /* Return a gimplify context struct from the pool. */
349 :
350 : static inline struct gimplify_ctx *
351 9209289 : ctx_alloc (void)
352 : {
353 9209289 : struct gimplify_ctx * c = ctx_pool;
354 :
355 9209289 : if (c)
356 8938890 : ctx_pool = c->prev_context;
357 : else
358 270399 : c = XNEW (struct gimplify_ctx);
359 :
360 9209289 : memset (c, '\0', sizeof (*c));
361 9209289 : return c;
362 : }
363 :
364 : /* Put gimplify context C back into the pool. */
365 :
366 : static inline void
367 9209285 : ctx_free (struct gimplify_ctx *c)
368 : {
369 9209285 : c->prev_context = ctx_pool;
370 9209285 : ctx_pool = c;
371 : }
372 :
373 : /* Free allocated ctx stack memory. */
374 :
375 : void
376 237651 : free_gimplify_stack (void)
377 : {
378 237651 : struct gimplify_ctx *c;
379 :
380 477746 : while ((c = ctx_pool))
381 : {
382 240095 : ctx_pool = c->prev_context;
383 240095 : free (c);
384 : }
385 237651 : }
386 :
387 :
388 : /* Set up a context for the gimplifier. */
389 :
390 : void
391 9209289 : push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
392 : {
393 9209289 : struct gimplify_ctx *c = ctx_alloc ();
394 :
395 9209289 : c->prev_context = gimplify_ctxp;
396 9209289 : gimplify_ctxp = c;
397 9209289 : gimplify_ctxp->into_ssa = in_ssa;
398 9209289 : gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
399 9209289 : }
400 :
401 : /* Tear down a context for the gimplifier. If BODY is non-null, then
402 : put the temporaries into the outer BIND_EXPR. Otherwise, put them
403 : in the local_decls.
404 :
405 : BODY is not a sequence, but the first tuple in a sequence. */
406 :
407 : void
408 9209285 : pop_gimplify_context (gimple *body)
409 : {
410 9209285 : struct gimplify_ctx *c = gimplify_ctxp;
411 :
412 9209285 : gcc_assert (c
413 : && (!c->bind_expr_stack.exists ()
414 : || c->bind_expr_stack.is_empty ()));
415 9209285 : c->bind_expr_stack.release ();
416 9209285 : gimplify_ctxp = c->prev_context;
417 :
418 9209285 : if (body)
419 3150556 : declare_vars (c->temps, body, false);
420 : else
421 6058729 : record_vars (c->temps);
422 :
423 9209285 : delete c->temp_htab;
424 9209285 : c->temp_htab = NULL;
425 9209285 : ctx_free (c);
426 9209285 : }
427 :
428 : /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
429 :
430 : static void
431 6072849 : gimple_push_bind_expr (gbind *bind_stmt)
432 : {
433 6072849 : gimplify_ctxp->bind_expr_stack.reserve (8);
434 6072849 : gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
435 6072849 : }
436 :
437 : /* Pop the first element off the stack of bindings. */
438 :
439 : static void
440 6072849 : gimple_pop_bind_expr (void)
441 : {
442 0 : gimplify_ctxp->bind_expr_stack.pop ();
443 0 : }
444 :
445 : /* Return the first element of the stack of bindings. */
446 :
447 : gbind *
448 0 : gimple_current_bind_expr (void)
449 : {
450 0 : return gimplify_ctxp->bind_expr_stack.last ();
451 : }
452 :
453 : /* Return the stack of bindings created during gimplification. */
454 :
455 : vec<gbind *>
456 359 : gimple_bind_expr_stack (void)
457 : {
458 359 : return gimplify_ctxp->bind_expr_stack;
459 : }
460 :
461 : /* Return true iff there is a COND_EXPR between us and the innermost
462 : CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
463 :
464 : static bool
465 2954812 : gimple_conditional_context (void)
466 : {
467 2954812 : return gimplify_ctxp->conditions > 0;
468 : }
469 :
470 : /* Note that we've entered a COND_EXPR. */
471 :
472 : static void
473 5882693 : gimple_push_condition (void)
474 : {
475 : #ifdef ENABLE_GIMPLE_CHECKING
476 5882693 : if (gimplify_ctxp->conditions == 0)
477 3869033 : gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
478 : #endif
479 5882693 : ++(gimplify_ctxp->conditions);
480 5882693 : }
481 :
482 : /* Note that we've left a COND_EXPR. If we're back at unconditional scope
483 : now, add any conditional cleanups we've seen to the prequeue. */
484 :
485 : static void
486 5882693 : gimple_pop_condition (gimple_seq *pre_p)
487 : {
488 5882693 : int conds = --(gimplify_ctxp->conditions);
489 :
490 5882693 : gcc_assert (conds >= 0);
491 5882693 : if (conds == 0)
492 : {
493 3869033 : gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
494 3869033 : gimplify_ctxp->conditional_cleanups = NULL;
495 : }
496 5882693 : }
497 :
498 : /* A stable comparison routine for use with splay trees and DECLs. */
499 :
500 : static int
501 18353524 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
502 : {
503 18353524 : tree a = (tree) xa;
504 18353524 : tree b = (tree) xb;
505 :
506 18353524 : return DECL_UID (a) - DECL_UID (b);
507 : }
508 :
509 : /* Create a new omp construct that deals with variable remapping. */
510 :
511 : static struct gimplify_omp_ctx *
512 141215 : new_omp_context (enum omp_region_type region_type)
513 : {
514 141215 : struct gimplify_omp_ctx *c;
515 :
516 141215 : c = XCNEW (struct gimplify_omp_ctx);
517 141215 : c->outer_context = gimplify_omp_ctxp;
518 141215 : c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
519 141215 : c->implicit_mappers = new hash_map<omp_name_type<tree>, tree>;
520 141215 : c->privatized_types = new hash_set<tree>;
521 141215 : c->location = input_location;
522 141215 : c->region_type = region_type;
523 141215 : if ((region_type & ORT_TASK) == 0)
524 : c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
525 : else
526 6302 : c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
527 141215 : c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
528 141215 : c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
529 141215 : c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
530 141215 : c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
531 141215 : c->defaultmap[GDMK_POINTER] = GOVD_MAP;
532 :
533 141215 : return c;
534 : }
535 :
536 : /* Destroy an omp construct that deals with variable remapping. */
537 :
538 : static void
539 140656 : delete_omp_context (struct gimplify_omp_ctx *c)
540 : {
541 140656 : splay_tree_delete (c->variables);
542 281312 : delete c->privatized_types;
543 281312 : delete c->implicit_mappers;
544 140656 : c->loop_iter_var.release ();
545 140656 : XDELETE (c);
546 140656 : }
547 :
548 : static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
549 : static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
550 :
551 : /* Both gimplify the statement T and append it to *SEQ_P. This function
552 : behaves exactly as gimplify_stmt, but you don't have to pass T as a
553 : reference. */
554 :
555 : void
556 38897832 : gimplify_and_add (tree t, gimple_seq *seq_p)
557 : {
558 38897832 : gimplify_stmt (&t, seq_p);
559 38897832 : }
560 :
561 : /* Gimplify statement T into sequence *SEQ_P, and return the first
562 : tuple in the sequence of generated tuples for this statement.
563 : Return NULL if gimplifying T produced no tuples. */
564 :
565 : static gimple *
566 106061 : gimplify_and_return_first (tree t, gimple_seq *seq_p)
567 : {
568 106061 : gimple_stmt_iterator last = gsi_last (*seq_p);
569 :
570 106061 : gimplify_and_add (t, seq_p);
571 :
572 106061 : if (!gsi_end_p (last))
573 : {
574 4881 : gsi_next (&last);
575 4881 : return gsi_stmt (last);
576 : }
577 : else
578 101180 : return gimple_seq_first_stmt (*seq_p);
579 : }
580 :
581 : /* Returns true iff T is a valid RHS for an assignment to an un-renamed
582 : LHS, or for a call argument. */
583 :
584 : static bool
585 238179 : is_gimple_mem_rhs (tree t)
586 : {
587 : /* If we're dealing with a renamable type, either source or dest must be
588 : a renamed variable. */
589 238179 : if (is_gimple_reg_type (TREE_TYPE (t)))
590 234805 : return is_gimple_val (t);
591 : else
592 3374 : return is_gimple_val (t) || is_gimple_lvalue (t);
593 : }
594 :
595 : /* Return true if T is a CALL_EXPR or an expression that can be
596 : assigned to a temporary. Note that this predicate should only be
597 : used during gimplification. See the rationale for this in
598 : gimplify_modify_expr. */
599 :
600 : static bool
601 99720907 : is_gimple_reg_rhs_or_call (tree t)
602 : {
603 73227645 : return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
604 99720907 : || TREE_CODE (t) == CALL_EXPR);
605 : }
606 :
607 : /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
608 : this predicate should only be used during gimplification. See the
609 : rationale for this in gimplify_modify_expr. */
610 :
611 : static bool
612 14598561 : is_gimple_mem_rhs_or_call (tree t)
613 : {
614 : /* If we're dealing with a renamable type, either source or dest must be
615 : a renamed variable. */
616 14598561 : if (is_gimple_reg_type (TREE_TYPE (t)))
617 11110087 : return is_gimple_val (t);
618 : else
619 3488474 : return (is_gimple_val (t)
620 1764034 : || is_gimple_lvalue (t)
621 1236317 : || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
622 4724399 : || TREE_CODE (t) == CALL_EXPR);
623 : }
624 :
625 : /* Create a temporary with a name derived from VAL. Subroutine of
626 : lookup_tmp_var; nobody else should call this function. */
627 :
628 : static inline tree
629 2195944 : create_tmp_from_val (tree val)
630 : {
631 : /* Drop all qualifiers and address-space information from the value type. */
632 2195944 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
633 2195944 : tree var = create_tmp_var (type, get_name (val));
634 2195944 : return var;
635 : }
636 :
637 : /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
638 : an existing expression temporary. If NOT_GIMPLE_REG, mark it as such. */
639 :
640 : static tree
641 2435136 : lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
642 : {
643 2435136 : tree ret;
644 :
645 : /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P. */
646 2435136 : gcc_assert (!is_formal || !not_gimple_reg);
647 :
648 : /* If not optimizing, never really reuse a temporary. local-alloc
649 : won't allocate any variable that is used in more than one basic
650 : block, which means it will go into memory, causing much extra
651 : work in reload and final and poorer code generation, outweighing
652 : the extra memory allocation here. */
653 2435136 : if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
654 : {
655 1106873 : ret = create_tmp_from_val (val);
656 1106873 : DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
657 : }
658 : else
659 : {
660 1328263 : elt_t elt, *elt_p;
661 1328263 : elt_t **slot;
662 :
663 1328263 : elt.val = val;
664 1328263 : if (!gimplify_ctxp->temp_htab)
665 419561 : gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
666 1328263 : slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
667 1328263 : if (*slot == NULL)
668 : {
669 1089071 : elt_p = XNEW (elt_t);
670 1089071 : elt_p->val = val;
671 1089071 : elt_p->temp = ret = create_tmp_from_val (val);
672 1089071 : *slot = elt_p;
673 : }
674 : else
675 : {
676 239192 : elt_p = *slot;
677 239192 : ret = elt_p->temp;
678 : }
679 : }
680 :
681 2435136 : return ret;
682 : }
683 :
684 : /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
685 :
686 : static tree
687 27032528 : internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
688 : bool is_formal, bool allow_ssa, bool not_gimple_reg)
689 : {
690 27032528 : tree t, mod;
691 :
692 : /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
693 : can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
694 27032528 : gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
695 : fb_rvalue);
696 :
697 27032528 : if (allow_ssa
698 26762547 : && gimplify_ctxp->into_ssa
699 52319332 : && is_gimple_reg_type (TREE_TYPE (val)))
700 : {
701 24597393 : t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
702 24597393 : if (! gimple_in_ssa_p (cfun))
703 : {
704 21172786 : const char *name = get_name (val);
705 21172786 : if (name)
706 6839082 : SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
707 : }
708 : }
709 : else
710 2435135 : t = lookup_tmp_var (val, is_formal, not_gimple_reg);
711 :
712 27032528 : mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
713 :
714 27032528 : SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
715 :
716 : /* gimplify_modify_expr might want to reduce this further. */
717 27032528 : gimplify_and_add (mod, pre_p);
718 27032528 : ggc_free (mod);
719 :
720 : /* If we failed to gimplify VAL then we can end up with the temporary
721 : SSA name not having a definition. In this case return a decl. */
722 27032528 : if (TREE_CODE (t) == SSA_NAME && ! SSA_NAME_DEF_STMT (t))
723 1 : return lookup_tmp_var (val, is_formal, not_gimple_reg);
724 :
725 : return t;
726 : }
727 :
728 : /* Return a formal temporary variable initialized with VAL. PRE_P is as
729 : in gimplify_expr. Only use this function if:
730 :
731 : 1) The value of the unfactored expression represented by VAL will not
732 : change between the initialization and use of the temporary, and
733 : 2) The temporary will not be otherwise modified.
734 :
735 : For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
736 : and #2 means it is inappropriate for && temps.
737 :
738 : For other cases, use get_initialized_tmp_var instead. */
739 :
740 : tree
741 26531918 : get_formal_tmp_var (tree val, gimple_seq *pre_p)
742 : {
743 26531918 : return internal_get_tmp_var (val, pre_p, NULL, true, true, false);
744 : }
745 :
746 : /* Return a temporary variable initialized with VAL. PRE_P and POST_P
747 : are as in gimplify_expr. */
748 :
749 : tree
750 488477 : get_initialized_tmp_var (tree val, gimple_seq *pre_p,
751 : gimple_seq *post_p /* = NULL */,
752 : bool allow_ssa /* = true */)
753 : {
754 488477 : return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa, false);
755 : }
756 :
757 : /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
758 : generate debug info for them; otherwise don't. */
759 :
760 : void
761 3158794 : declare_vars (tree vars, gimple *gs, bool debug_info)
762 : {
763 3158794 : tree last = vars;
764 3158794 : if (last)
765 : {
766 1986257 : tree temps, block;
767 :
768 1986257 : gbind *scope = as_a <gbind *> (gs);
769 :
770 1986257 : temps = nreverse (last);
771 :
772 1986257 : block = gimple_bind_block (scope);
773 1986257 : gcc_assert (!block || TREE_CODE (block) == BLOCK);
774 1986257 : if (!block || !debug_info)
775 : {
776 1982422 : DECL_CHAIN (last) = gimple_bind_vars (scope);
777 1982422 : gimple_bind_set_vars (scope, temps);
778 : }
779 : else
780 : {
781 : /* We need to attach the nodes both to the BIND_EXPR and to its
782 : associated BLOCK for debugging purposes. The key point here
783 : is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
784 : is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
785 3835 : if (BLOCK_VARS (block))
786 3642 : BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
787 : else
788 : {
789 193 : gimple_bind_set_vars (scope,
790 : chainon (gimple_bind_vars (scope), temps));
791 193 : BLOCK_VARS (block) = temps;
792 : }
793 : }
794 : }
795 3158794 : }
796 :
797 : /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
798 : for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
799 : no such upper bound can be obtained. */
800 :
801 : static void
802 0 : force_constant_size (tree var)
803 : {
804 : /* The only attempt we make is by querying the maximum size of objects
805 : of the variable's type. */
806 :
807 0 : HOST_WIDE_INT max_size;
808 :
809 0 : gcc_assert (VAR_P (var));
810 :
811 0 : max_size = max_int_size_in_bytes (TREE_TYPE (var));
812 :
813 0 : gcc_assert (max_size >= 0);
814 :
815 0 : DECL_SIZE_UNIT (var)
816 0 : = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
817 0 : DECL_SIZE (var)
818 0 : = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
819 0 : }
820 :
821 : /* Push the temporary variable TMP into the current binding. */
822 :
823 : void
824 28301 : gimple_add_tmp_var_fn (struct function *fn, tree tmp)
825 : {
826 28301 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
827 :
828 : /* Later processing assumes that the object size is constant, which might
829 : not be true at this point. Force the use of a constant upper bound in
830 : this case. */
831 28301 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
832 0 : force_constant_size (tmp);
833 :
834 28301 : DECL_CONTEXT (tmp) = fn->decl;
835 28301 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
836 :
837 28301 : record_vars_into (tmp, fn->decl);
838 28301 : }
839 :
840 : /* Push the temporary variable TMP into the current binding. */
841 :
842 : void
843 16817094 : gimple_add_tmp_var (tree tmp)
844 : {
845 16817094 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
846 :
847 : /* Later processing assumes that the object size is constant, which might
848 : not be true at this point. Force the use of a constant upper bound in
849 : this case. */
850 16817094 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
851 0 : force_constant_size (tmp);
852 :
853 16817094 : DECL_CONTEXT (tmp) = current_function_decl;
854 16817094 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
855 :
856 16817094 : if (gimplify_ctxp)
857 : {
858 5812055 : DECL_CHAIN (tmp) = gimplify_ctxp->temps;
859 5812055 : gimplify_ctxp->temps = tmp;
860 :
861 : /* Mark temporaries local within the nearest enclosing parallel. */
862 5812055 : if (gimplify_omp_ctxp)
863 : {
864 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
865 554057 : int flag = GOVD_LOCAL | GOVD_SEEN;
866 : while (ctx
867 554057 : && (ctx->region_type == ORT_WORKSHARE
868 : || ctx->region_type == ORT_TASKGROUP
869 406364 : || ctx->region_type == ORT_SIMD
870 356150 : || ctx->region_type == ORT_ACC))
871 : {
872 204763 : if (ctx->region_type == ORT_SIMD
873 50214 : && TREE_ADDRESSABLE (tmp)
874 95 : && !TREE_STATIC (tmp))
875 : {
876 95 : if (TREE_CODE (DECL_SIZE_UNIT (tmp)) != INTEGER_CST)
877 0 : ctx->add_safelen1 = true;
878 95 : else if (ctx->in_for_exprs)
879 : flag = GOVD_PRIVATE;
880 : else
881 : flag = GOVD_PRIVATE | GOVD_SEEN;
882 : break;
883 : }
884 204668 : ctx = ctx->outer_context;
885 : }
886 349294 : if (ctx)
887 322206 : omp_add_variable (ctx, tmp, flag);
888 : }
889 : }
890 11005039 : else if (cfun)
891 11005039 : record_vars (tmp);
892 : else
893 : {
894 0 : gimple_seq body_seq;
895 :
896 : /* This case is for nested functions. We need to expose the locals
897 : they create. */
898 0 : body_seq = gimple_body (current_function_decl);
899 0 : declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
900 : }
901 16817094 : }
902 :
903 : /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
904 : any nested functions. */
905 :
906 : static void
907 3052435 : unshare_body (tree fndecl)
908 : {
909 3052435 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
910 : /* If the language requires deep unsharing, we need a pointer set to make
911 : sure we don't repeatedly unshare subtrees of unshareable nodes. */
912 3052435 : hash_set<tree> *visited
913 3052435 : = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
914 :
915 3052435 : copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
916 3052435 : copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
917 3052435 : copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
918 :
919 3056611 : delete visited;
920 :
921 3052435 : if (cgn)
922 6149804 : for (cgn = first_nested_function (cgn); cgn;
923 26664 : cgn = next_nested_function (cgn))
924 26664 : unshare_body (cgn->decl);
925 3052435 : }
926 :
927 : /* Callback for walk_tree to unmark the visited trees rooted at *TP.
928 : Subtrees are walked until the first unvisited node is encountered. */
929 :
930 : static tree
931 286979162 : unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
932 : {
933 286979162 : tree t = *tp;
934 :
935 : /* If this node has been visited, unmark it and keep looking. */
936 286979162 : if (TREE_VISITED (t))
937 201776015 : TREE_VISITED (t) = 0;
938 :
939 : /* Otherwise, don't look any deeper. */
940 : else
941 85203147 : *walk_subtrees = 0;
942 :
943 286979162 : return NULL_TREE;
944 : }
945 :
946 : /* Unmark the visited trees rooted at *TP. */
947 :
948 : static inline void
949 9157305 : unmark_visited (tree *tp)
950 : {
951 9157305 : walk_tree (tp, unmark_visited_r, NULL, NULL);
952 9157305 : }
953 :
954 : /* Likewise, but mark all trees as not visited. */
955 :
956 : static void
957 3052435 : unvisit_body (tree fndecl)
958 : {
959 3052435 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
960 :
961 3052435 : unmark_visited (&DECL_SAVED_TREE (fndecl));
962 3052435 : unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
963 3052435 : unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
964 :
965 3052435 : if (cgn)
966 6123140 : for (cgn = first_nested_function (cgn);
967 3074902 : cgn; cgn = next_nested_function (cgn))
968 26664 : unvisit_body (cgn->decl);
969 3052435 : }
970 :
971 : /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
972 : one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
973 : comprising at least one DEBUG_BEGIN_STMT followed by exactly one
974 : EXPR is the location of the EXPR. */
975 :
976 : static location_t
977 1185595 : rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
978 : {
979 1185595 : if (!expr)
980 : return or_else;
981 :
982 1185595 : if (EXPR_HAS_LOCATION (expr))
983 823037 : return EXPR_LOCATION (expr);
984 :
985 362558 : if (TREE_CODE (expr) != STATEMENT_LIST)
986 : return or_else;
987 :
988 0 : tree_stmt_iterator i = tsi_start (expr);
989 :
990 0 : bool found = false;
991 0 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
992 : {
993 0 : found = true;
994 0 : tsi_next (&i);
995 : }
996 :
997 0 : if (!found || !tsi_one_before_end_p (i))
998 : return or_else;
999 :
1000 0 : return rexpr_location (tsi_stmt (i), or_else);
1001 : }
1002 :
1003 : /* Return TRUE iff EXPR (maybe recursively) has a location; see
1004 : rexpr_location for the potential recursion. */
1005 :
1006 : static inline bool
1007 518238 : rexpr_has_location (tree expr)
1008 : {
1009 309577 : return rexpr_location (expr) != UNKNOWN_LOCATION;
1010 : }
1011 :
1012 :
1013 : /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1014 : contain statements and have a value. Assign its value to a temporary
1015 : and give it void_type_node. Return the temporary, or NULL_TREE if
1016 : WRAPPER was already void. */
1017 :
1018 : tree
1019 20778248 : voidify_wrapper_expr (tree wrapper, tree temp)
1020 : {
1021 20778248 : tree type = TREE_TYPE (wrapper);
1022 20778248 : if (type && !VOID_TYPE_P (type))
1023 : {
1024 : tree *p;
1025 :
1026 : /* Set p to point to the body of the wrapper. Loop until we find
1027 : something that isn't a wrapper. */
1028 801827 : for (p = &wrapper; p && *p; )
1029 : {
1030 801827 : switch (TREE_CODE (*p))
1031 : {
1032 3148 : case BIND_EXPR:
1033 3148 : TREE_SIDE_EFFECTS (*p) = 1;
1034 3148 : TREE_TYPE (*p) = void_type_node;
1035 : /* For a BIND_EXPR, the body is operand 1. */
1036 3148 : p = &BIND_EXPR_BODY (*p);
1037 3148 : break;
1038 :
1039 371017 : case CLEANUP_POINT_EXPR:
1040 371017 : case TRY_FINALLY_EXPR:
1041 371017 : case TRY_CATCH_EXPR:
1042 371017 : TREE_SIDE_EFFECTS (*p) = 1;
1043 371017 : TREE_TYPE (*p) = void_type_node;
1044 371017 : p = &TREE_OPERAND (*p, 0);
1045 371017 : break;
1046 :
1047 17981 : case STATEMENT_LIST:
1048 17981 : {
1049 17981 : tree_stmt_iterator i = tsi_last (*p);
1050 17981 : TREE_SIDE_EFFECTS (*p) = 1;
1051 17981 : TREE_TYPE (*p) = void_type_node;
1052 17981 : p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1053 : }
1054 17981 : break;
1055 :
1056 : case COMPOUND_EXPR:
1057 : /* Advance to the last statement. Set all container types to
1058 : void. */
1059 65041 : for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1060 : {
1061 32528 : TREE_SIDE_EFFECTS (*p) = 1;
1062 32528 : TREE_TYPE (*p) = void_type_node;
1063 : }
1064 : break;
1065 :
1066 76 : case TRANSACTION_EXPR:
1067 76 : TREE_SIDE_EFFECTS (*p) = 1;
1068 76 : TREE_TYPE (*p) = void_type_node;
1069 76 : p = &TRANSACTION_EXPR_BODY (*p);
1070 76 : break;
1071 :
1072 377092 : default:
1073 : /* Assume that any tree upon which voidify_wrapper_expr is
1074 : directly called is a wrapper, and that its body is op0. */
1075 377092 : if (p == &wrapper)
1076 : {
1077 33 : TREE_SIDE_EFFECTS (*p) = 1;
1078 33 : TREE_TYPE (*p) = void_type_node;
1079 33 : p = &TREE_OPERAND (*p, 0);
1080 33 : break;
1081 : }
1082 377059 : goto out;
1083 : }
1084 : }
1085 :
1086 0 : out:
1087 377059 : if (p == NULL || IS_EMPTY_STMT (*p))
1088 : temp = NULL_TREE;
1089 377059 : else if (temp)
1090 : {
1091 : /* The wrapper is on the RHS of an assignment that we're pushing
1092 : down. */
1093 1937 : gcc_assert (TREE_CODE (temp) == INIT_EXPR
1094 : || TREE_CODE (temp) == MODIFY_EXPR);
1095 1937 : TREE_OPERAND (temp, 1) = *p;
1096 1937 : *p = temp;
1097 : }
1098 : else
1099 : {
1100 375122 : temp = create_tmp_var (type, "retval");
1101 375122 : *p = build2 (INIT_EXPR, type, temp, *p);
1102 : }
1103 :
1104 : return temp;
1105 : }
1106 :
1107 : return NULL_TREE;
1108 : }
1109 :
1110 : /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1111 : a temporary through which they communicate. */
1112 :
1113 : static void
1114 7923 : build_stack_save_restore (gcall **save, gcall **restore)
1115 : {
1116 7923 : tree tmp_var;
1117 :
1118 15846 : *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1119 7923 : tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1120 7923 : gimple_call_set_lhs (*save, tmp_var);
1121 :
1122 7923 : *restore
1123 7923 : = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1124 : 1, tmp_var);
1125 7923 : }
1126 :
1127 : /* Generate IFN_ASAN_MARK call that poisons shadow memory of the DECL
1128 : variable. */
1129 :
1130 : static tree
1131 472 : build_asan_poison_call_expr (tree decl)
1132 : {
1133 : /* Do not poison variables that have size equal to zero. */
1134 472 : tree unit_size = DECL_SIZE_UNIT (decl);
1135 472 : if (zerop (unit_size))
1136 : return NULL_TREE;
1137 :
1138 472 : tree base = build_fold_addr_expr (decl);
1139 :
1140 472 : return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1141 : void_type_node, 3,
1142 : build_int_cst (integer_type_node,
1143 : ASAN_MARK_POISON),
1144 : base, unit_size);
1145 : }
1146 :
1147 : /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1148 : on POISON flag, shadow memory of a DECL variable. The call will be
1149 : put on location identified by IT iterator, where BEFORE flag drives
1150 : position where the stmt will be put. */
1151 :
1152 : static void
1153 5097 : asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1154 : bool before)
1155 : {
1156 5097 : tree unit_size = DECL_SIZE_UNIT (decl);
1157 5097 : tree base = build_fold_addr_expr (decl);
1158 :
1159 : /* Do not poison variables that have size equal to zero. */
1160 5097 : if (zerop (unit_size))
1161 5097 : return;
1162 :
1163 : /* It's necessary to have all stack variables aligned to ASAN granularity
1164 : bytes. */
1165 5083 : gcc_assert (!hwassist_sanitize_p () || hwassist_sanitize_stack_p ());
1166 5083 : unsigned shadow_granularity
1167 5083 : = (hwassist_sanitize_p ()
1168 5083 : ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY);
1169 5083 : if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
1170 4881 : SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
1171 :
1172 5083 : HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1173 :
1174 5083 : gimple *g
1175 5083 : = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1176 5083 : build_int_cst (integer_type_node, flags),
1177 : base, unit_size);
1178 :
1179 5083 : if (before)
1180 2654 : gsi_insert_before (it, g, GSI_NEW_STMT);
1181 : else
1182 2429 : gsi_insert_after (it, g, GSI_NEW_STMT);
1183 : }
1184 :
1185 : /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1186 : either poisons or unpoisons a DECL. Created statement is appended
1187 : to SEQ_P gimple sequence. */
1188 :
1189 : static void
1190 4625 : asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1191 : {
1192 4625 : gimple_stmt_iterator it = gsi_last (*seq_p);
1193 4625 : bool before = false;
1194 :
1195 4625 : if (gsi_end_p (it))
1196 2445 : before = true;
1197 :
1198 4625 : asan_poison_variable (decl, poison, &it, before);
1199 4625 : }
1200 :
1201 : /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1202 :
1203 : static int
1204 135 : sort_by_decl_uid (const void *a, const void *b)
1205 : {
1206 135 : const tree *t1 = (const tree *)a;
1207 135 : const tree *t2 = (const tree *)b;
1208 :
1209 135 : int uid1 = DECL_UID (*t1);
1210 135 : int uid2 = DECL_UID (*t2);
1211 :
1212 135 : if (uid1 < uid2)
1213 : return -1;
1214 51 : else if (uid1 > uid2)
1215 : return 1;
1216 : else
1217 0 : return 0;
1218 : }
1219 :
1220 : /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1221 : depending on POISON flag. Created statement is appended
1222 : to SEQ_P gimple sequence. */
1223 :
1224 : static void
1225 1041225 : asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1226 : {
1227 1041225 : unsigned c = variables->elements ();
1228 1041225 : if (c == 0)
1229 1040951 : return;
1230 :
1231 274 : auto_vec<tree> sorted_variables (c);
1232 :
1233 274 : for (hash_set<tree>::iterator it = variables->begin ();
1234 569 : it != variables->end (); ++it)
1235 295 : sorted_variables.safe_push (*it);
1236 :
1237 274 : sorted_variables.qsort (sort_by_decl_uid);
1238 :
1239 274 : unsigned i;
1240 274 : tree var;
1241 843 : FOR_EACH_VEC_ELT (sorted_variables, i, var)
1242 : {
1243 295 : asan_poison_variable (var, poison, seq_p);
1244 :
1245 : /* Add use_after_scope_memory attribute for the variable in order
1246 : to prevent re-written into SSA. */
1247 295 : if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1248 295 : DECL_ATTRIBUTES (var)))
1249 97 : DECL_ATTRIBUTES (var)
1250 194 : = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1251 : integer_one_node,
1252 97 : DECL_ATTRIBUTES (var));
1253 : }
1254 274 : }
1255 :
1256 : /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1257 :
1258 : static enum gimplify_status
1259 6070557 : gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1260 : {
1261 6070557 : tree bind_expr = *expr_p;
1262 6070557 : bool old_keep_stack = gimplify_ctxp->keep_stack;
1263 6070557 : bool old_save_stack = gimplify_ctxp->save_stack;
1264 6070557 : tree t;
1265 6070557 : gbind *bind_stmt;
1266 6070557 : gimple_seq body, cleanup;
1267 6070557 : gcall *stack_save;
1268 6070557 : location_t start_locus = 0, end_locus = 0;
1269 6070557 : tree ret_clauses = NULL;
1270 :
1271 6070557 : tree temp = voidify_wrapper_expr (bind_expr, NULL);
1272 :
1273 : /* Mark variables seen in this bind expr. */
1274 13057086 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1275 : {
1276 6986529 : if (VAR_P (t))
1277 : {
1278 6318418 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1279 6318418 : tree attr;
1280 :
1281 6318418 : if (flag_openmp
1282 259497 : && !is_global_var (t)
1283 250572 : && !TREE_STATIC (t)
1284 250572 : && DECL_CONTEXT (t) == current_function_decl
1285 250572 : && TREE_USED (t)
1286 6567374 : && (attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1287 : != NULL_TREE)
1288 : {
1289 76 : gcc_assert (!DECL_HAS_VALUE_EXPR_P (t));
1290 76 : tree alloc = TREE_PURPOSE (TREE_VALUE (attr));
1291 76 : tree align = TREE_VALUE (TREE_VALUE (attr));
1292 : /* Allocate directives that appear in a target region must specify
1293 : an allocator clause unless a requires directive with the
1294 : dynamic_allocators clause is present in the same compilation
1295 : unit. */
1296 76 : bool missing_dyn_alloc = false;
1297 76 : if ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0)
1298 : {
1299 : /* This comes too early for omp_discover_declare_target...,
1300 : but should at least catch the most common cases. */
1301 70 : missing_dyn_alloc
1302 70 : = (alloc == NULL_TREE
1303 70 : && cgraph_node::get (current_function_decl)->offloadable);
1304 70 : for (struct gimplify_omp_ctx *ctx2 = ctx;
1305 74 : ctx2 && !missing_dyn_alloc; ctx2 = ctx2->outer_context)
1306 13 : if (ctx2->code == OMP_TARGET)
1307 : {
1308 9 : if (alloc == NULL_TREE)
1309 : missing_dyn_alloc = true;
1310 7 : else if (TREE_CODE (alloc) != INTEGER_CST)
1311 : {
1312 2 : tree alloc2 = alloc;
1313 2 : if (TREE_CODE (alloc2) == MEM_REF
1314 2 : || TREE_CODE (alloc2) == INDIRECT_REF)
1315 0 : alloc2 = TREE_OPERAND (alloc2, 0);
1316 2 : tree c2;
1317 2 : for (c2 = ctx2->clauses; c2;
1318 0 : c2 = OMP_CLAUSE_CHAIN (c2))
1319 1 : if (OMP_CLAUSE_CODE (c2)
1320 : == OMP_CLAUSE_USES_ALLOCATORS)
1321 : {
1322 1 : tree t2
1323 1 : = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c2);
1324 1 : if (operand_equal_p (alloc2, t2))
1325 : break;
1326 : }
1327 2 : if (c2 == NULL_TREE)
1328 1 : error_at (EXPR_LOC_OR_LOC (
1329 : alloc, DECL_SOURCE_LOCATION (t)),
1330 : "%qE in %<allocator%> clause inside a "
1331 : "target region must be specified in an "
1332 : "%<uses_allocators%> clause on the "
1333 : "%<target%> directive", alloc2);
1334 : }
1335 : break;
1336 : }
1337 : }
1338 68 : if (missing_dyn_alloc)
1339 4 : error_at (DECL_SOURCE_LOCATION (t),
1340 : "%<allocate%> directive for %qD inside a target "
1341 : "region must specify an %<allocator%> clause", t);
1342 : /* Skip for omp_default_mem_alloc (= 1),
1343 : unless align is present. For C/C++, there should be always a
1344 : statement list following if TREE_USED, except for, e.g., using
1345 : this decl in a static_assert; in that case, only a single
1346 : DECL_EXPR remains, which can be skipped here. */
1347 72 : else if (!errorcount
1348 55 : && (align != NULL_TREE
1349 55 : || alloc == NULL_TREE
1350 10 : || !integer_onep (alloc))
1351 125 : && (lang_GNU_Fortran ()
1352 25 : || (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1353 : != DECL_EXPR)))
1354 : {
1355 : /* Fortran might already use a pointer type internally;
1356 : use that pointer except for type(C_ptr) and type(C_funptr);
1357 : note that normal proc pointers are rejected. */
1358 53 : tree type = TREE_TYPE (t);
1359 53 : tree tmp, v;
1360 53 : if (lang_GNU_Fortran ()
1361 28 : && POINTER_TYPE_P (type)
1362 8 : && TREE_TYPE (type) != void_type_node
1363 59 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
1364 : {
1365 6 : type = TREE_TYPE (type);
1366 6 : v = t;
1367 : }
1368 : else
1369 : {
1370 47 : tmp = build_pointer_type (type);
1371 47 : v = create_tmp_var (tmp, get_name (t));
1372 47 : DECL_IGNORED_P (v) = 0;
1373 47 : DECL_ATTRIBUTES (v)
1374 47 : = tree_cons (get_identifier ("omp allocate var"),
1375 : build_tree_list (NULL_TREE, t),
1376 : remove_attribute ("omp allocate",
1377 47 : DECL_ATTRIBUTES (t)));
1378 47 : tmp = build_fold_indirect_ref (v);
1379 47 : TREE_THIS_NOTRAP (tmp) = 1;
1380 47 : SET_DECL_VALUE_EXPR (t, tmp);
1381 47 : DECL_HAS_VALUE_EXPR_P (t) = 1;
1382 : }
1383 53 : tree sz = TYPE_SIZE_UNIT (type);
1384 : /* The size to use in Fortran might not match TYPE_SIZE_UNIT;
1385 : hence, for some decls, a size variable is saved in the
1386 : attributes; use it, if available. */
1387 53 : if (TREE_CHAIN (TREE_VALUE (attr))
1388 28 : && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))
1389 59 : && TREE_PURPOSE (
1390 : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))))
1391 : {
1392 6 : sz = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1393 6 : sz = TREE_PURPOSE (sz);
1394 : }
1395 53 : if (alloc == NULL_TREE)
1396 36 : alloc = build_zero_cst (ptr_type_node);
1397 53 : if (align == NULL_TREE)
1398 42 : align = build_int_cst (size_type_node, DECL_ALIGN_UNIT (t));
1399 : else
1400 11 : align = build_int_cst (size_type_node,
1401 11 : MAX (tree_to_uhwi (align),
1402 : DECL_ALIGN_UNIT (t)));
1403 53 : location_t loc = DECL_SOURCE_LOCATION (t);
1404 53 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
1405 53 : tmp = build_call_expr_loc (loc, tmp, 3, align, sz, alloc);
1406 53 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1407 53 : fold_convert (TREE_TYPE (v), tmp));
1408 53 : gcc_assert (BIND_EXPR_BODY (bind_expr) != NULL_TREE);
1409 : /* Ensure that either TREE_CHAIN (TREE_VALUE (attr) is set
1410 : and GOMP_FREE added here or that DECL_HAS_VALUE_EXPR_P (t)
1411 : is set, using in a condition much further below. */
1412 59 : gcc_assert (DECL_HAS_VALUE_EXPR_P (t)
1413 : || TREE_CHAIN (TREE_VALUE (attr)));
1414 53 : if (TREE_CHAIN (TREE_VALUE (attr)))
1415 : {
1416 : /* Fortran is special as it does not have properly nest
1417 : declarations in blocks. And as there is no
1418 : initializer, there is also no expression to look for.
1419 : Hence, the FE makes the statement list of the
1420 : try-finally block available. We can put the GOMP_alloc
1421 : at the top, unless an allocator or size expression
1422 : requires to put it afterward; note that the size is
1423 : always later in generated code; for strings, no
1424 : size expr but still an expr might be available.
1425 : As LTO does not handle a statement list, 'sl' has
1426 : to be removed; done so by removing the attribute. */
1427 28 : DECL_ATTRIBUTES (t)
1428 28 : = remove_attribute ("omp allocate",
1429 28 : DECL_ATTRIBUTES (t));
1430 28 : tree sl = TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)));
1431 28 : tree_stmt_iterator e = tsi_start (sl);
1432 28 : tree needle = NULL_TREE;
1433 28 : if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1434 : {
1435 6 : needle = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1436 6 : needle = (TREE_VALUE (needle) ? TREE_VALUE (needle)
1437 : : sz);
1438 : }
1439 22 : else if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1440 : needle = sz;
1441 22 : else if (DECL_P (alloc) && DECL_ARTIFICIAL (alloc))
1442 : needle = alloc;
1443 :
1444 10 : if (needle != NULL_TREE)
1445 : {
1446 49 : while (!tsi_end_p (e))
1447 : {
1448 49 : if (*e == needle
1449 49 : || (TREE_CODE (*e) == MODIFY_EXPR
1450 49 : && TREE_OPERAND (*e, 0) == needle))
1451 : break;
1452 39 : ++e;
1453 : }
1454 10 : gcc_assert (!tsi_end_p (e));
1455 : }
1456 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1457 :
1458 : /* As the cleanup is in BIND_EXPR_BODY, GOMP_free is added
1459 : here; for C/C++ it will be added in the 'cleanup'
1460 : section after gimplification. But Fortran already has
1461 : a try-finally block. */
1462 28 : sl = TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)));
1463 28 : e = tsi_last (sl);
1464 28 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1465 28 : tmp = build_call_expr_loc (EXPR_LOCATION (*e), tmp, 2, v,
1466 : build_zero_cst (ptr_type_node));
1467 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1468 28 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1469 28 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1470 28 : fold_convert (TREE_TYPE (v), tmp));
1471 28 : ++e;
1472 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1473 : }
1474 : else
1475 : {
1476 25 : gcc_assert (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1477 : == STATEMENT_LIST);
1478 25 : tree_stmt_iterator e;
1479 25 : e = tsi_start (BIND_EXPR_BODY (bind_expr));
1480 113 : while (!tsi_end_p (e))
1481 : {
1482 88 : if ((TREE_CODE (*e) == DECL_EXPR
1483 58 : && TREE_OPERAND (*e, 0) == t)
1484 121 : || (TREE_CODE (*e) == CLEANUP_POINT_EXPR
1485 0 : && (TREE_CODE (TREE_OPERAND (*e, 0))
1486 : == DECL_EXPR)
1487 0 : && (TREE_OPERAND (TREE_OPERAND (*e, 0), 0)
1488 : == t)))
1489 : break;
1490 63 : ++e;
1491 : }
1492 25 : gcc_assert (!tsi_end_p (e));
1493 25 : tsi_link_before (&e, tmp, TSI_SAME_STMT);
1494 : }
1495 : }
1496 : }
1497 :
1498 : /* Mark variable as local. */
1499 6318418 : if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1500 : {
1501 100897 : if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1502 100897 : || splay_tree_lookup (ctx->variables,
1503 : (splay_tree_key) t) == NULL)
1504 : {
1505 100897 : int flag = GOVD_LOCAL;
1506 100897 : if (ctx->region_type == ORT_SIMD
1507 4376 : && TREE_ADDRESSABLE (t)
1508 81 : && !TREE_STATIC (t))
1509 : {
1510 69 : if (TREE_CODE (DECL_SIZE_UNIT (t)) != INTEGER_CST)
1511 2 : ctx->add_safelen1 = true;
1512 : else
1513 : flag = GOVD_PRIVATE;
1514 : }
1515 100897 : omp_add_variable (ctx, t, flag | GOVD_SEEN);
1516 : }
1517 : /* Static locals inside of target construct or offloaded
1518 : routines need to be "omp declare target". */
1519 100897 : if (TREE_STATIC (t))
1520 1386 : for (; ctx; ctx = ctx->outer_context)
1521 1207 : if ((ctx->region_type & ORT_TARGET) != 0)
1522 : {
1523 969 : if (!lookup_attribute ("omp declare target",
1524 969 : DECL_ATTRIBUTES (t)))
1525 : {
1526 957 : tree id = get_identifier ("omp declare target");
1527 957 : DECL_ATTRIBUTES (t)
1528 957 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
1529 957 : varpool_node *node = varpool_node::get (t);
1530 957 : if (node)
1531 : {
1532 33 : node->offloadable = 1;
1533 33 : if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
1534 : {
1535 : g->have_offload = true;
1536 : if (!in_lto_p)
1537 : vec_safe_push (offload_vars, t);
1538 : }
1539 : }
1540 : }
1541 : break;
1542 : }
1543 : }
1544 :
1545 6318418 : DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1546 :
1547 6318418 : if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1548 1089 : cfun->has_local_explicit_reg_vars = true;
1549 : }
1550 : }
1551 :
1552 12141114 : bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1553 6070557 : BIND_EXPR_BLOCK (bind_expr));
1554 6070557 : gimple_push_bind_expr (bind_stmt);
1555 :
1556 6070557 : gimplify_ctxp->keep_stack = false;
1557 6070557 : gimplify_ctxp->save_stack = false;
1558 :
1559 : /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1560 6070557 : body = NULL;
1561 6070557 : gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1562 6070557 : gimple_bind_set_body (bind_stmt, body);
1563 :
1564 : /* Source location wise, the cleanup code (stack_restore and clobbers)
1565 : belongs to the end of the block, so propagate what we have. The
1566 : stack_save operation belongs to the beginning of block, which we can
1567 : infer from the bind_expr directly if the block has no explicit
1568 : assignment. */
1569 6070557 : if (BIND_EXPR_BLOCK (bind_expr))
1570 : {
1571 5938947 : end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1572 5938947 : start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1573 : }
1574 5938947 : if (start_locus == 0)
1575 6070557 : start_locus = EXPR_LOCATION (bind_expr);
1576 :
1577 6070557 : cleanup = NULL;
1578 6070557 : stack_save = NULL;
1579 :
1580 : /* Add clobbers for all variables that go out of scope. */
1581 13057086 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1582 : {
1583 6986529 : if (VAR_P (t)
1584 6318418 : && !is_global_var (t)
1585 13082506 : && DECL_CONTEXT (t) == current_function_decl)
1586 : {
1587 6095977 : if (flag_openmp
1588 250535 : && DECL_HAS_VALUE_EXPR_P (t)
1589 1083 : && TREE_USED (t)
1590 6097043 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1591 : {
1592 : /* For Fortran, TREE_CHAIN (TREE_VALUE (attr)) is set, which
1593 : causes that the GOMP_free call is already added above;
1594 : and "omp allocate" is removed from DECL_ATTRIBUTES. */
1595 25 : tree v = TREE_OPERAND (DECL_VALUE_EXPR (t), 0);
1596 25 : tree tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1597 25 : tmp = build_call_expr_loc (end_locus, tmp, 2, v,
1598 : build_zero_cst (ptr_type_node));
1599 25 : gimplify_and_add (tmp, &cleanup);
1600 25 : gimple *clobber_stmt;
1601 25 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1602 25 : clobber_stmt = gimple_build_assign (v, tmp);
1603 25 : gimple_set_location (clobber_stmt, end_locus);
1604 25 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1605 : }
1606 6095977 : if (!DECL_HARD_REGISTER (t)
1607 6094888 : && !TREE_THIS_VOLATILE (t)
1608 6055368 : && !DECL_HAS_VALUE_EXPR_P (t)
1609 : /* Only care for variables that have to be in memory. Others
1610 : will be rewritten into SSA names, hence moved to the
1611 : top-level. */
1612 5928281 : && !is_gimple_reg (t)
1613 7376830 : && flag_stack_reuse != SR_NONE)
1614 : {
1615 1277063 : tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
1616 1277063 : gimple *clobber_stmt;
1617 1277063 : clobber_stmt = gimple_build_assign (t, clobber);
1618 1277063 : gimple_set_location (clobber_stmt, end_locus);
1619 1277063 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1620 : }
1621 :
1622 6095977 : if (flag_openacc && oacc_declare_returns != NULL)
1623 : {
1624 207 : tree key = t;
1625 207 : if (DECL_HAS_VALUE_EXPR_P (key))
1626 : {
1627 8 : key = DECL_VALUE_EXPR (key);
1628 8 : if (INDIRECT_REF_P (key))
1629 8 : key = TREE_OPERAND (key, 0);
1630 : }
1631 207 : tree *c = oacc_declare_returns->get (key);
1632 207 : if (c != NULL)
1633 : {
1634 116 : if (ret_clauses)
1635 64 : OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1636 :
1637 116 : ret_clauses = unshare_expr (*c);
1638 :
1639 116 : oacc_declare_returns->remove (key);
1640 :
1641 116 : if (oacc_declare_returns->is_empty ())
1642 : {
1643 40 : delete oacc_declare_returns;
1644 40 : oacc_declare_returns = NULL;
1645 : }
1646 : }
1647 : }
1648 : }
1649 :
1650 6986529 : if (asan_poisoned_variables != NULL
1651 6986529 : && asan_poisoned_variables->contains (t))
1652 : {
1653 2165 : asan_poisoned_variables->remove (t);
1654 2165 : asan_poison_variable (t, true, &cleanup);
1655 : }
1656 :
1657 6986529 : if (gimplify_ctxp->live_switch_vars != NULL
1658 6986529 : && gimplify_ctxp->live_switch_vars->contains (t))
1659 55 : gimplify_ctxp->live_switch_vars->remove (t);
1660 : }
1661 :
1662 : /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1663 : the stack space allocated to the VLAs. */
1664 6070557 : if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1665 : {
1666 7923 : gcall *stack_restore;
1667 :
1668 : /* Save stack on entry and restore it on exit. Add a try_finally
1669 : block to achieve this. */
1670 7923 : build_stack_save_restore (&stack_save, &stack_restore);
1671 :
1672 7923 : gimple_set_location (stack_save, start_locus);
1673 7923 : gimple_set_location (stack_restore, end_locus);
1674 :
1675 7923 : gimplify_seq_add_stmt (&cleanup, stack_restore);
1676 : }
1677 :
1678 6070557 : if (ret_clauses)
1679 : {
1680 52 : gomp_target *stmt;
1681 52 : gimple_stmt_iterator si = gsi_start (cleanup);
1682 :
1683 52 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1684 : ret_clauses);
1685 52 : gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1686 : }
1687 :
1688 6070557 : if (cleanup)
1689 : {
1690 788721 : gtry *gs;
1691 788721 : gimple_seq new_body;
1692 :
1693 788721 : new_body = NULL;
1694 788721 : gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1695 : GIMPLE_TRY_FINALLY);
1696 :
1697 788721 : if (stack_save)
1698 7923 : gimplify_seq_add_stmt (&new_body, stack_save);
1699 788721 : gimplify_seq_add_stmt (&new_body, gs);
1700 788721 : gimple_bind_set_body (bind_stmt, new_body);
1701 : }
1702 :
1703 : /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1704 6070557 : if (!gimplify_ctxp->keep_stack)
1705 6055360 : gimplify_ctxp->keep_stack = old_keep_stack;
1706 6070557 : gimplify_ctxp->save_stack = old_save_stack;
1707 :
1708 6070557 : gimple_pop_bind_expr ();
1709 :
1710 6070557 : gimplify_seq_add_stmt (pre_p, bind_stmt);
1711 :
1712 6070557 : if (temp)
1713 : {
1714 1709 : *expr_p = temp;
1715 1709 : return GS_OK;
1716 : }
1717 :
1718 : *expr_p = NULL_TREE;
1719 : return GS_ALL_DONE;
1720 : }
1721 :
1722 : /* Maybe add early return predict statement to PRE_P sequence. */
1723 :
1724 : static void
1725 2301008 : maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1726 : {
1727 : /* If we are not in a conditional context, add PREDICT statement. */
1728 2301008 : if (gimple_conditional_context ())
1729 : {
1730 447003 : gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1731 : NOT_TAKEN);
1732 447003 : gimplify_seq_add_stmt (pre_p, predict);
1733 : }
1734 2301008 : }
1735 :
1736 : /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1737 : GIMPLE value, it is assigned to a new temporary and the statement is
1738 : re-written to return the temporary.
1739 :
1740 : PRE_P points to the sequence where side effects that must happen before
1741 : STMT should be stored. */
1742 :
1743 : static enum gimplify_status
1744 2301014 : gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1745 : {
1746 2301014 : greturn *ret;
1747 2301014 : tree ret_expr = TREE_OPERAND (stmt, 0);
1748 2301014 : tree result_decl, result;
1749 :
1750 2301014 : if (ret_expr == error_mark_node)
1751 : return GS_ERROR;
1752 :
1753 2301008 : if (!ret_expr
1754 2210239 : || TREE_CODE (ret_expr) == RESULT_DECL)
1755 : {
1756 106552 : maybe_add_early_return_predict_stmt (pre_p);
1757 106552 : greturn *ret = gimple_build_return (ret_expr);
1758 106552 : copy_warning (ret, stmt);
1759 106552 : gimplify_seq_add_stmt (pre_p, ret);
1760 106552 : return GS_ALL_DONE;
1761 : }
1762 :
1763 2194456 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1764 : result_decl = NULL_TREE;
1765 2194131 : else if (TREE_CODE (ret_expr) == COMPOUND_EXPR)
1766 : {
1767 : /* Used in C++ for handling EH cleanup of the return value if a local
1768 : cleanup throws. Assume the front-end knows what it's doing. */
1769 5245 : result_decl = DECL_RESULT (current_function_decl);
1770 : /* But crash if we end up trying to modify ret_expr below. */
1771 5245 : ret_expr = NULL_TREE;
1772 : }
1773 : else
1774 : {
1775 2188886 : result_decl = TREE_OPERAND (ret_expr, 0);
1776 :
1777 : /* See through a return by reference. */
1778 2188886 : if (INDIRECT_REF_P (result_decl))
1779 42704 : result_decl = TREE_OPERAND (result_decl, 0);
1780 :
1781 2188886 : gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1782 : || TREE_CODE (ret_expr) == INIT_EXPR)
1783 : && TREE_CODE (result_decl) == RESULT_DECL);
1784 : }
1785 :
1786 : /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1787 : Recall that aggregate_value_p is FALSE for any aggregate type that is
1788 : returned in registers. If we're returning values in registers, then
1789 : we don't want to extend the lifetime of the RESULT_DECL, particularly
1790 : across another call. In addition, for those aggregates for which
1791 : hard_function_value generates a PARALLEL, we'll die during normal
1792 : expansion of structure assignments; there's special code in expand_return
1793 : to handle this case that does not exist in expand_expr. */
1794 5245 : if (!result_decl)
1795 : result = NULL_TREE;
1796 2194131 : else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1797 : {
1798 181299 : if (!poly_int_tree_p (DECL_SIZE (result_decl)))
1799 : {
1800 48 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1801 0 : gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1802 : /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1803 : should be effectively allocated by the caller, i.e. all calls to
1804 : this function must be subject to the Return Slot Optimization. */
1805 48 : gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1806 48 : gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1807 : }
1808 : result = result_decl;
1809 : }
1810 2012832 : else if (gimplify_ctxp->return_temp)
1811 : result = gimplify_ctxp->return_temp;
1812 : else
1813 : {
1814 1516335 : result = create_tmp_reg (TREE_TYPE (result_decl));
1815 :
1816 : /* ??? With complex control flow (usually involving abnormal edges),
1817 : we can wind up warning about an uninitialized value for this. Due
1818 : to how this variable is constructed and initialized, this is never
1819 : true. Give up and never warn. */
1820 1516335 : suppress_warning (result, OPT_Wuninitialized);
1821 :
1822 1516335 : gimplify_ctxp->return_temp = result;
1823 : }
1824 :
1825 : /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1826 : Then gimplify the whole thing. */
1827 2194456 : if (result != result_decl)
1828 2012832 : TREE_OPERAND (ret_expr, 0) = result;
1829 :
1830 2194456 : gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1831 :
1832 2194456 : maybe_add_early_return_predict_stmt (pre_p);
1833 2194456 : ret = gimple_build_return (result);
1834 2194456 : copy_warning (ret, stmt);
1835 2194456 : gimplify_seq_add_stmt (pre_p, ret);
1836 :
1837 2194456 : return GS_ALL_DONE;
1838 : }
1839 :
1840 : /* Gimplify a variable-length array DECL. */
1841 :
1842 : static void
1843 9034 : gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1844 : {
1845 : /* This is a variable-sized decl. Simplify its size and mark it
1846 : for deferred expansion. */
1847 9034 : tree t, addr, ptr_type;
1848 :
1849 9034 : gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1850 9034 : gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1851 :
1852 : /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1853 9034 : if (DECL_HAS_VALUE_EXPR_P (decl))
1854 : return;
1855 :
1856 : /* All occurrences of this decl in final gimplified code will be
1857 : replaced by indirection. Setting DECL_VALUE_EXPR does two
1858 : things: First, it lets the rest of the gimplifier know what
1859 : replacement to use. Second, it lets the debug info know
1860 : where to find the value. */
1861 9029 : ptr_type = build_pointer_type (TREE_TYPE (decl));
1862 9029 : addr = create_tmp_var (ptr_type, get_name (decl));
1863 9029 : DECL_IGNORED_P (addr) = 0;
1864 9029 : t = build_fold_indirect_ref (addr);
1865 9029 : TREE_THIS_NOTRAP (t) = 1;
1866 9029 : SET_DECL_VALUE_EXPR (decl, t);
1867 9029 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1868 :
1869 9029 : t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1870 9029 : max_int_size_in_bytes (TREE_TYPE (decl)));
1871 : /* The call has been built for a variable-sized object. */
1872 9029 : CALL_ALLOCA_FOR_VAR_P (t) = 1;
1873 9029 : t = fold_convert (ptr_type, t);
1874 9029 : t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1875 :
1876 9029 : gimplify_and_add (t, seq_p);
1877 :
1878 : /* Record the dynamic allocation associated with DECL if requested. */
1879 9029 : if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
1880 0 : record_dynamic_alloc (decl);
1881 : }
1882 :
1883 : /* A helper function to be called via walk_tree. Mark all labels under *TP
1884 : as being forced. To be called for DECL_INITIAL of static variables. */
1885 :
1886 : static tree
1887 871420 : force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1888 : {
1889 871420 : if (TYPE_P (*tp))
1890 0 : *walk_subtrees = 0;
1891 871420 : if (TREE_CODE (*tp) == LABEL_DECL)
1892 : {
1893 926 : FORCED_LABEL (*tp) = 1;
1894 926 : cfun->has_forced_label_in_static = 1;
1895 : }
1896 :
1897 871420 : return NULL_TREE;
1898 : }
1899 :
1900 : /* Generate an initialization to automatic variable DECL based on INIT_TYPE.
1901 : Build a call to internal const function DEFERRED_INIT:
1902 : 1st argument: SIZE of the DECL;
1903 : 2nd argument: INIT_TYPE;
1904 : 3rd argument: NAME of the DECL;
1905 :
1906 : as LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL). */
1907 :
1908 : static void
1909 157716 : gimple_add_init_for_auto_var (tree decl,
1910 : enum auto_init_type init_type,
1911 : gimple_seq *seq_p)
1912 : {
1913 157716 : gcc_assert (auto_var_p (decl));
1914 157716 : gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
1915 :
1916 157716 : const location_t loc = DECL_SOURCE_LOCATION (decl);
1917 157716 : tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
1918 157716 : tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
1919 157716 : tree decl_name;
1920 :
1921 157716 : if (DECL_NAME (decl))
1922 90126 : decl_name = build_string_literal (DECL_NAME (decl));
1923 : else
1924 : {
1925 67590 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
1926 67590 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
1927 67590 : decl_name = build_string_literal (decl_name_anonymous);
1928 : }
1929 :
1930 157716 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
1931 157716 : TREE_TYPE (decl), 3,
1932 : decl_size, init_type_node,
1933 : decl_name);
1934 :
1935 157716 : gimplify_assign (decl, call, seq_p);
1936 157716 : }
1937 :
1938 : /* Generate padding initialization for automatic variable DECL.
1939 : C guarantees that brace-init with fewer initializers than members
1940 : aggregate will initialize the rest of the aggregate as-if it were
1941 : static initialization. In turn static initialization guarantees
1942 : that padding is initialized to zero. So, we always initialize paddings
1943 : to zeroes regardless INIT_TYPE.
1944 : To do the padding initialization, we insert a call to
1945 : __builtin_clear_padding (&decl, 0, for_auto_init = true).
1946 : Note, we add an additional dummy argument for __builtin_clear_padding,
1947 : 'for_auto_init' to distinguish whether this call is for automatic
1948 : variable initialization or not.
1949 : */
1950 : static void
1951 116 : gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
1952 : gimple_seq *seq_p)
1953 : {
1954 116 : tree addr_of_decl = NULL_TREE;
1955 116 : tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
1956 :
1957 116 : if (is_vla)
1958 : {
1959 : /* The temporary address variable for this vla should be
1960 : created in gimplify_vla_decl. */
1961 0 : gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
1962 0 : gcc_assert (INDIRECT_REF_P (DECL_VALUE_EXPR (decl)));
1963 0 : addr_of_decl = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
1964 : }
1965 : else
1966 : {
1967 116 : mark_addressable (decl);
1968 116 : addr_of_decl = build_fold_addr_expr (decl);
1969 : }
1970 :
1971 116 : gimple *call = gimple_build_call (fn, 2, addr_of_decl,
1972 116 : build_one_cst (TREE_TYPE (addr_of_decl)));
1973 116 : gimplify_seq_add_stmt (seq_p, call);
1974 116 : }
1975 :
1976 : /* Return true if the DECL need to be automatically initialized by the
1977 : compiler. */
1978 : static bool
1979 4340256 : var_needs_auto_init_p (tree decl)
1980 : {
1981 4340256 : if (auto_var_p (decl)
1982 4241984 : && (TREE_CODE (decl) != VAR_DECL || !DECL_HARD_REGISTER (decl))
1983 4241276 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
1984 353413 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
1985 353405 : && !lookup_attribute ("indeterminate", DECL_ATTRIBUTES (decl))
1986 353396 : && !OPAQUE_TYPE_P (TREE_TYPE (decl))
1987 4693652 : && !is_empty_type (TREE_TYPE (decl)))
1988 325937 : return true;
1989 : return false;
1990 : }
1991 :
1992 : /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1993 : and initialization explicit. */
1994 :
1995 : static enum gimplify_status
1996 6627196 : gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1997 : {
1998 6627196 : tree stmt = *stmt_p;
1999 6627196 : tree decl = DECL_EXPR_DECL (stmt);
2000 :
2001 6627196 : *stmt_p = NULL_TREE;
2002 :
2003 6627196 : if (TREE_TYPE (decl) == error_mark_node)
2004 : return GS_ERROR;
2005 :
2006 6627109 : if ((TREE_CODE (decl) == TYPE_DECL
2007 6583339 : || VAR_P (decl))
2008 13207362 : && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
2009 : {
2010 715259 : gimplify_type_sizes (TREE_TYPE (decl), seq_p);
2011 715259 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
2012 23566 : gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
2013 : }
2014 :
2015 : /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
2016 : in case its size expressions contain problematic nodes like CALL_EXPR. */
2017 6627109 : if (TREE_CODE (decl) == TYPE_DECL
2018 43770 : && DECL_ORIGINAL_TYPE (decl)
2019 6632430 : && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
2020 : {
2021 0 : gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
2022 0 : if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
2023 0 : gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
2024 : }
2025 :
2026 6627109 : if (VAR_P (decl) && !DECL_EXTERNAL (decl))
2027 : {
2028 6580253 : tree init = DECL_INITIAL (decl);
2029 6580253 : bool is_vla = false;
2030 : /* Check whether a decl has FE created VALUE_EXPR here BEFORE
2031 : gimplify_vla_decl creates VALUE_EXPR for a vla decl.
2032 : If the decl has VALUE_EXPR that was created by FE (usually
2033 : C++FE), it's a proxy variable, and FE already initialized
2034 : the VALUE_EXPR of it, we should not initialize it anymore. */
2035 6580253 : bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
2036 :
2037 6580253 : poly_uint64 size;
2038 6580253 : if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
2039 6580253 : || (!TREE_STATIC (decl)
2040 6453973 : && flag_stack_check == GENERIC_STACK_CHECK
2041 234 : && maybe_gt (size,
2042 : (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
2043 : {
2044 8907 : gimplify_vla_decl (decl, seq_p);
2045 8907 : is_vla = true;
2046 : }
2047 :
2048 6580253 : if (asan_poisoned_variables
2049 5244 : && !is_vla
2050 5058 : && TREE_ADDRESSABLE (decl)
2051 2460 : && !TREE_STATIC (decl)
2052 2250 : && !DECL_HAS_VALUE_EXPR_P (decl)
2053 2173 : && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
2054 2173 : && dbg_cnt (asan_use_after_scope)
2055 2173 : && !gimplify_omp_ctxp
2056 : /* GNAT introduces temporaries to hold return values of calls in
2057 : initializers of variables defined in other units, so the
2058 : declaration of the variable is discarded completely. We do not
2059 : want to issue poison calls for such dropped variables. */
2060 6582418 : && (DECL_SEEN_IN_BIND_EXPR_P (decl)
2061 0 : || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
2062 : {
2063 2165 : asan_poisoned_variables->add (decl);
2064 2165 : asan_poison_variable (decl, false, seq_p);
2065 2165 : if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
2066 55 : gimplify_ctxp->live_switch_vars->add (decl);
2067 : }
2068 :
2069 : /* Some front ends do not explicitly declare all anonymous
2070 : artificial variables. We compensate here by declaring the
2071 : variables, though it would be better if the front ends would
2072 : explicitly declare them. */
2073 6580253 : if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
2074 6580253 : && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
2075 16713 : gimple_add_tmp_var (decl);
2076 :
2077 6580253 : if (init && init != error_mark_node)
2078 : {
2079 3744470 : if (!TREE_STATIC (decl))
2080 : {
2081 3714558 : DECL_INITIAL (decl) = NULL_TREE;
2082 3714558 : init = build2 (INIT_EXPR, void_type_node, decl, init);
2083 3714558 : gimplify_and_add (init, seq_p);
2084 3714558 : ggc_free (init);
2085 : /* Clear TREE_READONLY if we really have an initialization. */
2086 3714558 : if (!DECL_INITIAL (decl)
2087 3714558 : && !omp_privatize_by_reference (decl))
2088 3692065 : TREE_READONLY (decl) = 0;
2089 : }
2090 : else
2091 : /* We must still examine initializers for static variables
2092 : as they may contain a label address. */
2093 29912 : walk_tree (&init, force_labels_r, NULL, NULL);
2094 : }
2095 : /* When there is no explicit initializer, if the user requested,
2096 : We should insert an artificial initializer for this automatic
2097 : variable. */
2098 2835783 : else if (var_needs_auto_init_p (decl)
2099 2835783 : && !decl_had_value_expr_p)
2100 : {
2101 98218 : gimple_add_init_for_auto_var (decl,
2102 : flag_auto_var_init,
2103 : seq_p);
2104 : /* The expanding of a call to the above .DEFERRED_INIT will apply
2105 : block initialization to the whole space covered by this variable.
2106 : As a result, all the paddings will be initialized to zeroes
2107 : for zero initialization and 0xFE byte-repeatable patterns for
2108 : pattern initialization.
2109 : In order to make the paddings as zeroes for pattern init, We
2110 : should add a call to __builtin_clear_padding to clear the
2111 : paddings to zero in compatible with CLANG.
2112 : We cannot insert this call if the variable is a gimple register
2113 : since __builtin_clear_padding will take the address of the
2114 : variable. As a result, if a long double/_Complex long double
2115 : variable will spilled into stack later, its padding is 0XFE. */
2116 98218 : if (flag_auto_var_init == AUTO_INIT_PATTERN
2117 184 : && !is_gimple_reg (decl)
2118 98316 : && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
2119 75 : gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
2120 : }
2121 : }
2122 :
2123 : return GS_ALL_DONE;
2124 : }
2125 :
2126 : /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
2127 : and replacing the LOOP_EXPR with goto, but if the loop contains an
2128 : EXIT_EXPR, we need to append a label for it to jump to. */
2129 :
2130 : static enum gimplify_status
2131 206812 : gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
2132 : {
2133 206812 : tree saved_label = gimplify_ctxp->exit_label;
2134 206812 : tree start_label = create_artificial_label (UNKNOWN_LOCATION);
2135 :
2136 206812 : gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
2137 :
2138 206812 : gimplify_ctxp->exit_label = NULL_TREE;
2139 :
2140 206812 : gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
2141 :
2142 206812 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
2143 :
2144 206812 : if (gimplify_ctxp->exit_label)
2145 6402 : gimplify_seq_add_stmt (pre_p,
2146 3201 : gimple_build_label (gimplify_ctxp->exit_label));
2147 :
2148 206812 : gimplify_ctxp->exit_label = saved_label;
2149 :
2150 206812 : *expr_p = NULL;
2151 206812 : return GS_ALL_DONE;
2152 : }
2153 :
2154 : /* Gimplify a statement list onto a sequence. These may be created either
2155 : by an enlightened front-end, or by shortcut_cond_expr. */
2156 :
2157 : static enum gimplify_status
2158 8677906 : gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
2159 : {
2160 8677906 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
2161 :
2162 8677906 : tree_stmt_iterator i = tsi_start (*expr_p);
2163 :
2164 55868169 : while (!tsi_end_p (i))
2165 : {
2166 38512357 : gimplify_stmt (tsi_stmt_ptr (i), pre_p);
2167 38512357 : tsi_delink (&i);
2168 : }
2169 :
2170 8677906 : if (temp)
2171 : {
2172 14833 : *expr_p = temp;
2173 14833 : return GS_OK;
2174 : }
2175 :
2176 : return GS_ALL_DONE;
2177 : }
2178 :
2179 :
2180 : /* Emit warning for the unreachable statement STMT if needed.
2181 : Return the gimple itself when the warning is emitted, otherwise
2182 : return NULL. */
2183 : static gimple *
2184 181 : emit_warn_switch_unreachable (gimple *stmt)
2185 : {
2186 181 : if (gimple_code (stmt) == GIMPLE_GOTO
2187 50 : && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
2188 231 : && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
2189 : /* Don't warn for compiler-generated gotos. These occur
2190 : in Duff's devices, for example. */
2191 : return NULL;
2192 : else
2193 131 : warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
2194 : "statement will never be executed");
2195 131 : return stmt;
2196 : }
2197 :
2198 : /* Callback for walk_gimple_seq. */
2199 :
2200 : static tree
2201 50074 : warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
2202 : bool *handled_ops_p,
2203 : struct walk_stmt_info *wi)
2204 : {
2205 50074 : gimple *stmt = gsi_stmt (*gsi_p);
2206 50074 : bool unreachable_issued = wi->info != NULL;
2207 :
2208 50074 : *handled_ops_p = true;
2209 50074 : switch (gimple_code (stmt))
2210 : {
2211 440 : case GIMPLE_TRY:
2212 : /* A compiler-generated cleanup or a user-written try block.
2213 : If it's empty, don't dive into it--that would result in
2214 : worse location info. */
2215 440 : if (gimple_try_eval (stmt) == NULL)
2216 : {
2217 3 : if (warn_switch_unreachable && !unreachable_issued)
2218 3 : wi->info = emit_warn_switch_unreachable (stmt);
2219 :
2220 : /* Stop when auto var init warning is not on. */
2221 3 : if (!warn_trivial_auto_var_init)
2222 3 : return integer_zero_node;
2223 : }
2224 : /* Fall through. */
2225 2469 : case GIMPLE_BIND:
2226 2469 : case GIMPLE_CATCH:
2227 2469 : case GIMPLE_EH_FILTER:
2228 2469 : case GIMPLE_TRANSACTION:
2229 : /* Walk the sub-statements. */
2230 2469 : *handled_ops_p = false;
2231 2469 : break;
2232 :
2233 : case GIMPLE_DEBUG:
2234 : /* Ignore these. We may generate them before declarations that
2235 : are never executed. If there's something to warn about,
2236 : there will be non-debug stmts too, and we'll catch those. */
2237 : break;
2238 :
2239 60 : case GIMPLE_ASSIGN:
2240 : /* See comment below in the GIMPLE_CALL case. */
2241 60 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2242 20 : && gimple_assign_single_p (stmt)
2243 79 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2244 : {
2245 6 : gimple *g = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
2246 6 : if (gimple_call_internal_p (g, IFN_DEFERRED_INIT))
2247 : break;
2248 : }
2249 54 : goto do_default;
2250 :
2251 46152 : case GIMPLE_LABEL:
2252 : /* Stop till the first Label. */
2253 46152 : return integer_zero_node;
2254 62 : case GIMPLE_CALL:
2255 62 : if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2256 : {
2257 8 : *handled_ops_p = false;
2258 8 : break;
2259 : }
2260 : /* Don't warn for compiler-generated initializations for
2261 : -ftrivial-auto-var-init for -Wswitch-unreachable. Though
2262 : do warn for -Wtrivial-auto-var-init.
2263 : There are 3 cases:
2264 : case 1: a call to .DEFERRED_INIT;
2265 : case 2: a call to __builtin_clear_padding with the 2nd argument is
2266 : present and non-zero;
2267 : case 3: a gimple assign store right after the call to .DEFERRED_INIT
2268 : that has the LHS of .DEFERRED_INIT as the RHS as following:
2269 : _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
2270 : i1 = _1.
2271 : case 3 is handled above in the GIMPLE_ASSIGN case. */
2272 54 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2273 54 : && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2274 : {
2275 33 : if (warn_trivial_auto_var_init)
2276 : {
2277 : /* Get the variable name from the 3rd argument of call. */
2278 12 : tree var_name = gimple_call_arg (stmt, 2);
2279 12 : var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
2280 12 : const char *var_name_str = TREE_STRING_POINTER (var_name);
2281 :
2282 12 : warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
2283 : "%qs cannot be initialized with "
2284 : "%<-ftrivial-auto-var_init%>", var_name_str);
2285 : }
2286 : break;
2287 : }
2288 21 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2289 7 : && gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
2290 24 : && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
2291 : break;
2292 : /* Fall through. */
2293 178 : default:
2294 178 : do_default:
2295 : /* check the first "real" statement (not a decl/lexical scope/...), issue
2296 : warning if needed. */
2297 178 : if (warn_switch_unreachable && !unreachable_issued)
2298 178 : wi->info = emit_warn_switch_unreachable (stmt);
2299 : /* Stop when auto var init warning is not on. */
2300 178 : if (!warn_trivial_auto_var_init)
2301 178 : return integer_zero_node;
2302 : break;
2303 : }
2304 : return NULL_TREE;
2305 : }
2306 :
2307 :
2308 : /* Possibly warn about unreachable statements between switch's controlling
2309 : expression and the first case. Also warn about -ftrivial-auto-var-init
2310 : cannot initialize the auto variable under such situation.
2311 : SEQ is the body of a switch expression. */
2312 :
2313 : static void
2314 50248 : maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
2315 : {
2316 60 : if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
2317 : /* This warning doesn't play well with Fortran when optimizations
2318 : are on. */
2319 50188 : || lang_GNU_Fortran ()
2320 96755 : || seq == NULL)
2321 3912 : return;
2322 :
2323 46336 : struct walk_stmt_info wi;
2324 :
2325 46336 : memset (&wi, 0, sizeof (wi));
2326 46336 : walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
2327 : }
2328 :
2329 :
2330 : /* A label entry that pairs label and a location. */
2331 : struct label_entry
2332 : {
2333 : tree label;
2334 : location_t loc;
2335 : };
2336 :
2337 : /* Find LABEL in vector of label entries VEC. */
2338 :
2339 : static struct label_entry *
2340 12352 : find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
2341 : {
2342 12352 : unsigned int i;
2343 12352 : struct label_entry *l;
2344 :
2345 23634 : FOR_EACH_VEC_ELT (*vec, i, l)
2346 20270 : if (l->label == label)
2347 : return l;
2348 : return NULL;
2349 : }
2350 :
2351 : /* Return true if LABEL, a LABEL_DECL, represents a case label
2352 : in a vector of labels CASES. */
2353 :
2354 : static bool
2355 16478 : case_label_p (const vec<tree> *cases, tree label)
2356 : {
2357 16478 : unsigned int i;
2358 16478 : tree l;
2359 :
2360 165417 : FOR_EACH_VEC_ELT (*cases, i, l)
2361 165083 : if (CASE_LABEL (l) == label)
2362 : return true;
2363 : return false;
2364 : }
2365 :
2366 : /* Find the last nondebug statement in a scope STMT. */
2367 :
2368 : static gimple *
2369 2257 : last_stmt_in_scope (gimple *stmt)
2370 : {
2371 4196 : if (!stmt)
2372 : return NULL;
2373 :
2374 6834 : auto last_stmt_in_seq = [] (gimple_seq s)
2375 : {
2376 2649 : gimple_seq_node n;
2377 5289 : for (n = gimple_seq_last (s);
2378 2654 : n && (is_gimple_debug (n)
2379 2641 : || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2380 109 : && gimple_call_internal_p (n, IFN_DEFERRED_INIT)));
2381 5 : n = n->prev)
2382 7 : if (n == s)
2383 : return (gimple *) NULL;
2384 : return (gimple *) n;
2385 : };
2386 :
2387 4185 : switch (gimple_code (stmt))
2388 : {
2389 1391 : case GIMPLE_BIND:
2390 1391 : {
2391 1391 : gbind *bind = as_a <gbind *> (stmt);
2392 1391 : stmt = last_stmt_in_seq (gimple_bind_body (bind));
2393 1391 : return last_stmt_in_scope (stmt);
2394 : }
2395 :
2396 710 : case GIMPLE_TRY:
2397 710 : {
2398 710 : gtry *try_stmt = as_a <gtry *> (stmt);
2399 710 : stmt = last_stmt_in_seq (gimple_try_eval (try_stmt));
2400 710 : gimple *last_eval = last_stmt_in_scope (stmt);
2401 710 : if (gimple_stmt_may_fallthru (last_eval)
2402 567 : && (last_eval == NULL
2403 564 : || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
2404 1270 : && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2405 : {
2406 548 : stmt = last_stmt_in_seq (gimple_try_cleanup (try_stmt));
2407 548 : return last_stmt_in_scope (stmt);
2408 : }
2409 : else
2410 : return last_eval;
2411 : }
2412 :
2413 0 : case GIMPLE_DEBUG:
2414 0 : gcc_unreachable ();
2415 :
2416 : default:
2417 : return stmt;
2418 : }
2419 : }
2420 :
2421 : /* Collect labels that may fall through into LABELS and return the statement
2422 : preceding another case label, or a user-defined label. Store a location
2423 : useful to give warnings at *PREVLOC (usually the location of the returned
2424 : statement or of its surrounding scope). */
2425 :
2426 : static gimple *
2427 21093 : collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
2428 : auto_vec <struct label_entry> *labels,
2429 : location_t *prevloc)
2430 : {
2431 21093 : gimple *prev = NULL;
2432 :
2433 21093 : *prevloc = UNKNOWN_LOCATION;
2434 102348 : do
2435 : {
2436 102348 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
2437 : {
2438 : /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
2439 : which starts on a GIMPLE_SWITCH and ends with a break label.
2440 : Handle that as a single statement that can fall through. */
2441 1452 : gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
2442 1452 : gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
2443 1452 : gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
2444 1446 : if (last
2445 1446 : && gimple_code (first) == GIMPLE_SWITCH
2446 1548 : && gimple_code (last) == GIMPLE_LABEL)
2447 : {
2448 102 : tree label = gimple_label_label (as_a <glabel *> (last));
2449 102 : if (SWITCH_BREAK_LABEL_P (label))
2450 : {
2451 102 : prev = bind;
2452 102 : gsi_next (gsi_p);
2453 102 : continue;
2454 : }
2455 : }
2456 : }
2457 102246 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
2458 102246 : || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2459 : {
2460 : /* Nested scope. Only look at the last statement of
2461 : the innermost scope. */
2462 1547 : location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2463 1547 : gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2464 1547 : if (last)
2465 : {
2466 1539 : prev = last;
2467 : /* It might be a label without a location. Use the
2468 : location of the scope then. */
2469 1539 : if (!gimple_has_location (prev))
2470 671 : *prevloc = bind_loc;
2471 : }
2472 1547 : gsi_next (gsi_p);
2473 1547 : continue;
2474 1547 : }
2475 :
2476 : /* Ifs are tricky. */
2477 100699 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2478 : {
2479 7268 : gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2480 7268 : tree false_lab = gimple_cond_false_label (cond_stmt);
2481 7268 : location_t if_loc = gimple_location (cond_stmt);
2482 :
2483 : /* If we have e.g.
2484 : if (i > 1) goto <D.2259>; else goto D;
2485 : we can't do much with the else-branch. */
2486 7268 : if (!DECL_ARTIFICIAL (false_lab))
2487 : break;
2488 :
2489 : /* Go on until the false label, then one step back. */
2490 55328 : for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2491 : {
2492 55328 : gimple *stmt = gsi_stmt (*gsi_p);
2493 55328 : if (gimple_code (stmt) == GIMPLE_LABEL
2494 55328 : && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2495 : break;
2496 : }
2497 :
2498 : /* Not found? Oops. */
2499 7268 : if (gsi_end_p (*gsi_p))
2500 : break;
2501 :
2502 : /* A dead label can't fall through. */
2503 7268 : if (!UNUSED_LABEL_P (false_lab))
2504 : {
2505 7221 : struct label_entry l = { false_lab, if_loc };
2506 7221 : labels->safe_push (l);
2507 : }
2508 :
2509 : /* Go to the last statement of the then branch. */
2510 7268 : gsi_prev (gsi_p);
2511 :
2512 : /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2513 : <D.1759>:
2514 : <stmt>;
2515 : goto <D.1761>;
2516 : <D.1760>:
2517 : */
2518 7268 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2519 7268 : && !gimple_has_location (gsi_stmt (*gsi_p)))
2520 : {
2521 : /* Look at the statement before, it might be
2522 : attribute fallthrough, in which case don't warn. */
2523 1626 : gsi_prev (gsi_p);
2524 1626 : bool fallthru_before_dest
2525 1626 : = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2526 1626 : gsi_next (gsi_p);
2527 1626 : tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2528 1626 : if (!fallthru_before_dest)
2529 : {
2530 1569 : struct label_entry l = { goto_dest, if_loc };
2531 1569 : labels->safe_push (l);
2532 : }
2533 : }
2534 : /* This case is about
2535 : if (1 != 0) goto <D.2022>; else goto <D.2023>;
2536 : <D.2022>:
2537 : n = n + 1; // #1
2538 : <D.2023>: // #2
2539 : <D.1988>: // #3
2540 : where #2 is UNUSED_LABEL_P and we want to warn about #1 falling
2541 : through to #3. So set PREV to #1. */
2542 5642 : else if (UNUSED_LABEL_P (false_lab))
2543 47 : prev = gsi_stmt (*gsi_p);
2544 :
2545 : /* And move back. */
2546 7268 : gsi_next (gsi_p);
2547 : }
2548 :
2549 100699 : tree lab;
2550 : /* Remember the last statement. Skip labels that are of no interest
2551 : to us. */
2552 100699 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2553 : {
2554 12134 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2555 12134 : if (find_label_entry (labels, label))
2556 8790 : prev = gsi_stmt (*gsi_p);
2557 : }
2558 88565 : else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2559 : ;
2560 88565 : else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2561 88565 : && gimple_call_internal_p (gsi_stmt (*gsi_p),
2562 : IFN_DEFERRED_INIT))
2563 : ;
2564 88559 : else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
2565 : ;
2566 87331 : else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2567 2382 : && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2568 348 : && (lab = gimple_goto_dest (gsi_stmt (*gsi_p)))
2569 348 : && TREE_CODE (lab) == LABEL_DECL
2570 87678 : && VACUOUS_INIT_LABEL_P (lab))
2571 : ;
2572 87331 : else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2573 67015 : prev = gsi_stmt (*gsi_p);
2574 100699 : gsi_next (gsi_p);
2575 : }
2576 102348 : while (!gsi_end_p (*gsi_p)
2577 : /* Stop if we find a case or a user-defined label. */
2578 200609 : && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2579 21872 : || !gimple_has_location (gsi_stmt (*gsi_p))));
2580 :
2581 21093 : if (prev && gimple_has_location (prev))
2582 20640 : *prevloc = gimple_location (prev);
2583 21093 : return prev;
2584 : }
2585 :
2586 : /* Return true if the switch fallthrough warning should occur. LABEL is
2587 : the label statement that we're falling through to. */
2588 :
2589 : static bool
2590 16999 : should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2591 : {
2592 16999 : gimple_stmt_iterator gsi = *gsi_p;
2593 :
2594 : /* Don't warn if the label is marked with a "falls through" comment. */
2595 16999 : if (FALLTHROUGH_LABEL_P (label))
2596 : return false;
2597 :
2598 : /* Don't warn for non-case labels followed by a statement:
2599 : case 0:
2600 : foo ();
2601 : label:
2602 : bar ();
2603 : as these are likely intentional. */
2604 16219 : if (!case_label_p (&gimplify_ctxp->case_labels, label))
2605 : {
2606 : tree l;
2607 334 : while (!gsi_end_p (gsi)
2608 334 : && ((gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2609 259 : && (l
2610 259 : = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2611 259 : && !case_label_p (&gimplify_ctxp->case_labels, l))
2612 153 : || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2613 31 : && gimple_call_internal_p (gsi_stmt (gsi),
2614 : IFN_DEFERRED_INIT))))
2615 173 : gsi_next_nondebug (&gsi);
2616 161 : if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2617 : return false;
2618 : }
2619 :
2620 : /* Don't warn for terminated branches, i.e. when the subsequent case labels
2621 : immediately breaks. */
2622 16144 : gsi = *gsi_p;
2623 :
2624 : /* Skip all immediately following labels. */
2625 35963 : while (!gsi_end_p (gsi)
2626 35963 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2627 16293 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT
2628 16084 : || (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2629 775 : && gimple_call_internal_p (gsi_stmt (gsi),
2630 : IFN_DEFERRED_INIT))))
2631 19819 : gsi_next_nondebug (&gsi);
2632 :
2633 : /* { ... something; default:; } */
2634 16144 : if (gsi_end_p (gsi)
2635 : /* { ... something; default: break; } or
2636 : { ... something; default: goto L; } */
2637 16078 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2638 : /* { ... something; default: return; } */
2639 31123 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2640 1192 : return false;
2641 :
2642 : return true;
2643 : }
2644 :
2645 : /* Callback for walk_gimple_seq. */
2646 :
2647 : static tree
2648 22101 : warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2649 : struct walk_stmt_info *)
2650 : {
2651 22101 : gimple *stmt = gsi_stmt (*gsi_p);
2652 :
2653 22101 : *handled_ops_p = true;
2654 22101 : switch (gimple_code (stmt))
2655 : {
2656 299 : case GIMPLE_TRY:
2657 299 : case GIMPLE_BIND:
2658 299 : case GIMPLE_CATCH:
2659 299 : case GIMPLE_EH_FILTER:
2660 299 : case GIMPLE_TRANSACTION:
2661 : /* Walk the sub-statements. */
2662 299 : *handled_ops_p = false;
2663 299 : break;
2664 :
2665 : /* Find a sequence of form:
2666 :
2667 : GIMPLE_LABEL
2668 : [...]
2669 : <may fallthru stmt>
2670 : GIMPLE_LABEL
2671 :
2672 : and possibly warn. */
2673 : case GIMPLE_LABEL:
2674 : {
2675 : /* Found a label. Skip all immediately following labels. */
2676 49612 : while (!gsi_end_p (*gsi_p)
2677 49612 : && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2678 28285 : gsi_next_nondebug (gsi_p);
2679 :
2680 : /* There might be no more statements. */
2681 21327 : if (gsi_end_p (*gsi_p))
2682 4321 : return integer_zero_node;
2683 :
2684 : /* Vector of labels that fall through. */
2685 21093 : auto_vec <struct label_entry> labels;
2686 21093 : location_t prevloc;
2687 21093 : gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2688 :
2689 : /* There might be no more statements. */
2690 21093 : if (gsi_end_p (*gsi_p))
2691 4087 : return integer_zero_node;
2692 :
2693 17006 : gimple *next = gsi_stmt (*gsi_p);
2694 17006 : tree label;
2695 : /* If what follows is a label, then we may have a fallthrough. */
2696 17006 : if (gimple_code (next) == GIMPLE_LABEL
2697 17006 : && gimple_has_location (next)
2698 17006 : && (label = gimple_label_label (as_a <glabel *> (next)))
2699 34012 : && prev != NULL)
2700 : {
2701 16999 : struct label_entry *l;
2702 16999 : bool warned_p = false;
2703 16999 : auto_diagnostic_group d;
2704 16999 : if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2705 : /* Quiet. */;
2706 14952 : else if (gimple_code (prev) == GIMPLE_LABEL
2707 218 : && (label = gimple_label_label (as_a <glabel *> (prev)))
2708 15170 : && (l = find_label_entry (&labels, label)))
2709 198 : warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2710 : "this statement may fall through");
2711 14754 : else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2712 : /* Try to be clever and don't warn when the statement
2713 : can't actually fall through. */
2714 14174 : && gimple_stmt_may_fallthru (prev)
2715 15055 : && prevloc != UNKNOWN_LOCATION)
2716 301 : warned_p = warning_at (prevloc,
2717 301 : OPT_Wimplicit_fallthrough_,
2718 : "this statement may fall through");
2719 499 : if (warned_p)
2720 499 : inform (gimple_location (next), "here");
2721 :
2722 : /* Mark this label as processed so as to prevent multiple
2723 : warnings in nested switches. */
2724 16999 : FALLTHROUGH_LABEL_P (label) = true;
2725 :
2726 : /* So that next warn_implicit_fallthrough_r will start looking for
2727 : a new sequence starting with this label. */
2728 16999 : gsi_prev (gsi_p);
2729 16999 : }
2730 4087 : }
2731 17006 : break;
2732 : default:
2733 : break;
2734 : }
2735 : return NULL_TREE;
2736 : }
2737 :
2738 : /* Warn when a switch case falls through. */
2739 :
2740 : static void
2741 50248 : maybe_warn_implicit_fallthrough (gimple_seq seq)
2742 : {
2743 50248 : if (!warn_implicit_fallthrough)
2744 46068 : return;
2745 :
2746 : /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2747 4180 : if (!(lang_GNU_C ()
2748 1232 : || lang_GNU_CXX ()
2749 0 : || lang_GNU_OBJC ()))
2750 : return;
2751 :
2752 4180 : struct walk_stmt_info wi;
2753 4180 : memset (&wi, 0, sizeof (wi));
2754 4180 : walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2755 : }
2756 :
2757 : /* Callback for walk_gimple_seq. */
2758 :
2759 : static tree
2760 3248216 : expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2761 : struct walk_stmt_info *wi)
2762 : {
2763 3248216 : gimple *stmt = gsi_stmt (*gsi_p);
2764 :
2765 3248216 : *handled_ops_p = true;
2766 3248216 : switch (gimple_code (stmt))
2767 : {
2768 146638 : case GIMPLE_TRY:
2769 146638 : case GIMPLE_BIND:
2770 146638 : case GIMPLE_CATCH:
2771 146638 : case GIMPLE_EH_FILTER:
2772 146638 : case GIMPLE_TRANSACTION:
2773 : /* Walk the sub-statements. */
2774 146638 : *handled_ops_p = false;
2775 146638 : break;
2776 250850 : case GIMPLE_CALL:
2777 250850 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2778 250850 : if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2779 : {
2780 3732 : location_t loc = gimple_location (stmt);
2781 3732 : gsi_remove (gsi_p, true);
2782 3732 : wi->removed_stmt = true;
2783 :
2784 : /* nothrow flag is added by genericize_c_loop to mark fallthrough
2785 : statement at the end of some loop's body. Those should be
2786 : always diagnosed, either because they indeed don't precede
2787 : a case label or default label, or because the next statement
2788 : is not within the same iteration statement. */
2789 3732 : if ((stmt->subcode & GF_CALL_NOTHROW) != 0)
2790 : {
2791 12 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2792 : "a case label or default label");
2793 12 : break;
2794 : }
2795 :
2796 3720 : if (gsi_end_p (*gsi_p))
2797 : {
2798 32 : static_cast<location_t *>(wi->info)[0] = BUILTINS_LOCATION;
2799 32 : static_cast<location_t *>(wi->info)[1] = loc;
2800 32 : break;
2801 : }
2802 :
2803 3688 : bool found = false;
2804 :
2805 3688 : gimple_stmt_iterator gsi2 = *gsi_p;
2806 3688 : stmt = gsi_stmt (gsi2);
2807 3688 : tree lab;
2808 3688 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2809 1666 : && gimple_code (stmt) == GIMPLE_GOTO
2810 20 : && (lab = gimple_goto_dest (stmt))
2811 20 : && TREE_CODE (lab) == LABEL_DECL
2812 3708 : && VACUOUS_INIT_LABEL_P (lab))
2813 : {
2814 : /* Handle for C++ artificial -ftrivial-auto-var-init=
2815 : sequences. Those look like:
2816 : goto lab1;
2817 : lab2:;
2818 : v1 = .DEFERRED_INIT (...);
2819 : v2 = .DEFERRED_INIT (...);
2820 : lab3:;
2821 : v3 = .DEFERRED_INIT (...);
2822 : lab1:;
2823 : In this case, a case/default label can be either in between
2824 : the GIMPLE_GOTO and the corresponding GIMPLE_LABEL, if jumps
2825 : from the switch condition to the case/default label cross
2826 : vacuous initialization of some variables, or after the
2827 : corresponding GIMPLE_LABEL, if those jumps don't cross
2828 : any such initialization but there is an adjacent named label
2829 : which crosses such initialization. So, for the purpose of
2830 : this function, just ignore the goto but until reaching the
2831 : corresponding GIMPLE_LABEL allow also .DEFERRED_INIT
2832 : calls. */
2833 5 : gsi_next (&gsi2);
2834 : }
2835 3683 : else if (gimple_code (stmt) == GIMPLE_GOTO
2836 3683 : && !gimple_has_location (stmt))
2837 : {
2838 : /* Go on until the artificial label. */
2839 57 : tree goto_dest = gimple_goto_dest (stmt);
2840 414 : for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2841 : {
2842 357 : if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2843 357 : && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2844 : == goto_dest)
2845 : break;
2846 : }
2847 :
2848 : /* Not found? Stop. */
2849 57 : if (gsi_end_p (gsi2))
2850 : break;
2851 :
2852 : /* Look one past it. */
2853 57 : gsi_next (&gsi2);
2854 : }
2855 :
2856 : /* We're looking for a case label or default label here. */
2857 3786 : while (!gsi_end_p (gsi2))
2858 : {
2859 3786 : stmt = gsi_stmt (gsi2);
2860 3786 : if (gimple_code (stmt) == GIMPLE_LABEL)
2861 : {
2862 3743 : tree label = gimple_label_label (as_a <glabel *> (stmt));
2863 3743 : if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2864 : {
2865 : found = true;
2866 : break;
2867 : }
2868 : }
2869 43 : else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2870 : ;
2871 43 : else if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2872 43 : && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2873 : ;
2874 43 : else if (!is_gimple_debug (stmt))
2875 : /* Anything else is not expected. */
2876 : break;
2877 98 : gsi_next (&gsi2);
2878 : }
2879 3688 : if (!found)
2880 23 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2881 : "a case label or default label");
2882 : }
2883 : break;
2884 2850728 : default:
2885 2850728 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2886 2850728 : break;
2887 : }
2888 3248216 : return NULL_TREE;
2889 : }
2890 :
2891 : /* Expand all FALLTHROUGH () calls in SEQ. */
2892 :
2893 : static void
2894 48563 : expand_FALLTHROUGH (gimple_seq *seq_p)
2895 : {
2896 48563 : auto_urlify_attributes sentinel;
2897 :
2898 48563 : struct walk_stmt_info wi;
2899 48563 : location_t loc[2];
2900 48563 : memset (&wi, 0, sizeof (wi));
2901 48563 : loc[0] = UNKNOWN_LOCATION;
2902 48563 : loc[1] = UNKNOWN_LOCATION;
2903 48563 : wi.info = (void *) &loc[0];
2904 48563 : walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2905 48563 : if (loc[0] != UNKNOWN_LOCATION)
2906 : /* We've found [[fallthrough]]; at the end of a switch, which the C++
2907 : standard says is ill-formed; see [dcl.attr.fallthrough]. */
2908 8 : pedwarn (loc[1], 0, "attribute %<fallthrough%> not preceding "
2909 : "a case label or default label");
2910 48563 : }
2911 :
2912 :
2913 : /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2914 : branch to. */
2915 :
2916 : static enum gimplify_status
2917 50248 : gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2918 : {
2919 50248 : tree switch_expr = *expr_p;
2920 50248 : gimple_seq switch_body_seq = NULL;
2921 50248 : enum gimplify_status ret;
2922 50248 : tree index_type = TREE_TYPE (switch_expr);
2923 50248 : if (index_type == NULL_TREE)
2924 11427 : index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2925 :
2926 50248 : ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2927 : fb_rvalue);
2928 50248 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
2929 : return ret;
2930 :
2931 50248 : if (SWITCH_BODY (switch_expr))
2932 : {
2933 50248 : vec<tree> labels;
2934 50248 : vec<tree> saved_labels;
2935 50248 : hash_set<tree> *saved_live_switch_vars = NULL;
2936 50248 : tree default_case = NULL_TREE;
2937 50248 : gswitch *switch_stmt;
2938 :
2939 : /* Save old labels, get new ones from body, then restore the old
2940 : labels. Save all the things from the switch body to append after. */
2941 50248 : saved_labels = gimplify_ctxp->case_labels;
2942 50248 : gimplify_ctxp->case_labels.create (8);
2943 :
2944 : /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2945 50248 : saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2946 50248 : tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2947 50248 : if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2948 49964 : gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2949 : else
2950 284 : gimplify_ctxp->live_switch_vars = NULL;
2951 :
2952 50248 : bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2953 50248 : gimplify_ctxp->in_switch_expr = true;
2954 :
2955 50248 : gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2956 :
2957 50248 : gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2958 50248 : maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
2959 50248 : maybe_warn_implicit_fallthrough (switch_body_seq);
2960 : /* Only do this for the outermost GIMPLE_SWITCH. */
2961 50248 : if (!gimplify_ctxp->in_switch_expr)
2962 48563 : expand_FALLTHROUGH (&switch_body_seq);
2963 :
2964 50248 : labels = gimplify_ctxp->case_labels;
2965 50248 : gimplify_ctxp->case_labels = saved_labels;
2966 :
2967 50248 : if (gimplify_ctxp->live_switch_vars)
2968 : {
2969 49964 : gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
2970 49964 : delete gimplify_ctxp->live_switch_vars;
2971 : }
2972 50248 : gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2973 :
2974 50248 : preprocess_case_label_vec_for_gimple (labels, index_type,
2975 : &default_case);
2976 :
2977 50248 : bool add_bind = false;
2978 50248 : if (!default_case)
2979 : {
2980 14914 : glabel *new_default;
2981 :
2982 14914 : default_case
2983 14914 : = build_case_label (NULL_TREE, NULL_TREE,
2984 : create_artificial_label (UNKNOWN_LOCATION));
2985 14914 : if (old_in_switch_expr)
2986 : {
2987 1082 : SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2988 1082 : add_bind = true;
2989 : }
2990 14914 : new_default = gimple_build_label (CASE_LABEL (default_case));
2991 14914 : gimplify_seq_add_stmt (&switch_body_seq, new_default);
2992 : }
2993 35334 : else if (old_in_switch_expr)
2994 : {
2995 603 : gimple *last = gimple_seq_last_stmt (switch_body_seq);
2996 603 : if (last && gimple_code (last) == GIMPLE_LABEL)
2997 : {
2998 338 : tree label = gimple_label_label (as_a <glabel *> (last));
2999 338 : if (SWITCH_BREAK_LABEL_P (label))
3000 50248 : add_bind = true;
3001 : }
3002 : }
3003 :
3004 50248 : switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
3005 : default_case, labels);
3006 50248 : gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
3007 : /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
3008 : ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
3009 : wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
3010 : so that we can easily find the start and end of the switch
3011 : statement. */
3012 50248 : if (add_bind)
3013 : {
3014 1419 : gimple_seq bind_body = NULL;
3015 1419 : gimplify_seq_add_stmt (&bind_body, switch_stmt);
3016 1419 : gimple_seq_add_seq (&bind_body, switch_body_seq);
3017 1419 : gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
3018 1419 : gimple_set_location (bind, EXPR_LOCATION (switch_expr));
3019 1419 : gimplify_seq_add_stmt (pre_p, bind);
3020 : }
3021 : else
3022 : {
3023 48829 : gimplify_seq_add_stmt (pre_p, switch_stmt);
3024 48829 : gimplify_seq_add_seq (pre_p, switch_body_seq);
3025 : }
3026 50248 : labels.release ();
3027 : }
3028 : else
3029 0 : gcc_unreachable ();
3030 :
3031 50248 : return GS_ALL_DONE;
3032 : }
3033 :
3034 : /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
3035 :
3036 : static enum gimplify_status
3037 2603932 : gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
3038 : {
3039 2603932 : gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3040 : == current_function_decl);
3041 :
3042 2603932 : tree label = LABEL_EXPR_LABEL (*expr_p);
3043 2603932 : glabel *label_stmt = gimple_build_label (label);
3044 2603932 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3045 2603932 : gimplify_seq_add_stmt (pre_p, label_stmt);
3046 :
3047 2603932 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3048 21 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3049 : NOT_TAKEN));
3050 2603911 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3051 11 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3052 : TAKEN));
3053 :
3054 2603932 : return GS_ALL_DONE;
3055 : }
3056 :
3057 : /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
3058 :
3059 : static enum gimplify_status
3060 1040508 : gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
3061 : {
3062 1040508 : struct gimplify_ctx *ctxp;
3063 1040508 : glabel *label_stmt;
3064 :
3065 : /* Invalid programs can play Duff's Device type games with, for example,
3066 : #pragma omp parallel. At least in the C front end, we don't
3067 : detect such invalid branches until after gimplification, in the
3068 : diagnose_omp_blocks pass. */
3069 1040518 : for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
3070 1040518 : if (ctxp->case_labels.exists ())
3071 : break;
3072 :
3073 1040508 : tree label = CASE_LABEL (*expr_p);
3074 1040508 : label_stmt = gimple_build_label (label);
3075 1040508 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3076 1040508 : ctxp->case_labels.safe_push (*expr_p);
3077 1040508 : gimplify_seq_add_stmt (pre_p, label_stmt);
3078 :
3079 1040508 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3080 16 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3081 : NOT_TAKEN));
3082 1040492 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3083 20 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3084 : TAKEN));
3085 :
3086 1040508 : return GS_ALL_DONE;
3087 : }
3088 :
3089 : /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
3090 : if necessary. */
3091 :
3092 : tree
3093 1797332 : build_and_jump (tree *label_p)
3094 : {
3095 1797332 : if (label_p == NULL)
3096 : /* If there's nowhere to jump, just fall through. */
3097 : return NULL_TREE;
3098 :
3099 1218431 : if (*label_p == NULL_TREE)
3100 : {
3101 697834 : tree label = create_artificial_label (UNKNOWN_LOCATION);
3102 697834 : *label_p = label;
3103 : }
3104 :
3105 1218431 : return build1 (GOTO_EXPR, void_type_node, *label_p);
3106 : }
3107 :
3108 : /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
3109 : This also involves building a label to jump to and communicating it to
3110 : gimplify_loop_expr through gimplify_ctxp->exit_label. */
3111 :
3112 : static enum gimplify_status
3113 3207 : gimplify_exit_expr (tree *expr_p)
3114 : {
3115 3207 : tree cond = TREE_OPERAND (*expr_p, 0);
3116 3207 : tree expr;
3117 :
3118 3207 : expr = build_and_jump (&gimplify_ctxp->exit_label);
3119 3207 : expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
3120 3207 : *expr_p = expr;
3121 :
3122 3207 : return GS_OK;
3123 : }
3124 :
3125 : /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
3126 : different from its canonical type, wrap the whole thing inside a
3127 : NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
3128 : type.
3129 :
3130 : The canonical type of a COMPONENT_REF is the type of the field being
3131 : referenced--unless the field is a bit-field which can be read directly
3132 : in a smaller mode, in which case the canonical type is the
3133 : sign-appropriate type corresponding to that mode. */
3134 :
3135 : static void
3136 18963298 : canonicalize_component_ref (tree *expr_p)
3137 : {
3138 18963298 : tree expr = *expr_p;
3139 18963298 : tree type;
3140 :
3141 18963298 : gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
3142 :
3143 18963298 : if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
3144 7628520 : type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
3145 : else
3146 11334778 : type = TREE_TYPE (TREE_OPERAND (expr, 1));
3147 :
3148 : /* One could argue that all the stuff below is not necessary for
3149 : the non-bitfield case and declare it a FE error if type
3150 : adjustment would be needed. */
3151 18963298 : if (TREE_TYPE (expr) != type)
3152 : {
3153 : #ifdef ENABLE_TYPES_CHECKING
3154 806589 : tree old_type = TREE_TYPE (expr);
3155 : #endif
3156 806589 : int type_quals;
3157 :
3158 : /* We need to preserve qualifiers and propagate them from
3159 : operand 0. */
3160 806589 : type_quals = TYPE_QUALS (type)
3161 806589 : | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
3162 806589 : if (TYPE_QUALS (type) != type_quals)
3163 806332 : type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
3164 :
3165 : /* Set the type of the COMPONENT_REF to the underlying type. */
3166 806589 : TREE_TYPE (expr) = type;
3167 :
3168 : #ifdef ENABLE_TYPES_CHECKING
3169 : /* It is now a FE error, if the conversion from the canonical
3170 : type to the original expression type is not useless. */
3171 806589 : gcc_assert (useless_type_conversion_p (old_type, type));
3172 : #endif
3173 : }
3174 18963298 : }
3175 :
3176 : /* If a NOP conversion is changing a pointer to array of foo to a pointer
3177 : to foo, embed that change in the ADDR_EXPR by converting
3178 : T array[U];
3179 : (T *)&array
3180 : ==>
3181 : &array[L]
3182 : where L is the lower bound. For simplicity, only do this for constant
3183 : lower bound.
3184 : The constraint is that the type of &array[L] is trivially convertible
3185 : to T *. */
3186 :
3187 : static void
3188 560455 : canonicalize_addr_expr (tree *expr_p)
3189 : {
3190 560455 : tree expr = *expr_p;
3191 560455 : tree addr_expr = TREE_OPERAND (expr, 0);
3192 560455 : tree datype, ddatype, pddatype;
3193 :
3194 : /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
3195 1120564 : if (!POINTER_TYPE_P (TREE_TYPE (expr))
3196 560455 : || TREE_CODE (addr_expr) != ADDR_EXPR)
3197 : return;
3198 :
3199 : /* The addr_expr type should be a pointer to an array. */
3200 346 : datype = TREE_TYPE (TREE_TYPE (addr_expr));
3201 346 : if (TREE_CODE (datype) != ARRAY_TYPE)
3202 : return;
3203 :
3204 : /* The pointer to element type shall be trivially convertible to
3205 : the expression pointer type. */
3206 28 : ddatype = TREE_TYPE (datype);
3207 28 : pddatype = build_pointer_type (ddatype);
3208 28 : if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
3209 : pddatype))
3210 : return;
3211 :
3212 : /* The lower bound and element sizes must be constant. */
3213 0 : if (!TYPE_SIZE_UNIT (ddatype)
3214 0 : || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
3215 0 : || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
3216 0 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
3217 : return;
3218 :
3219 : /* All checks succeeded. Build a new node to merge the cast. */
3220 0 : *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
3221 0 : TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
3222 : NULL_TREE, NULL_TREE);
3223 0 : *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
3224 :
3225 : /* We can have stripped a required restrict qualifier above. */
3226 0 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
3227 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
3228 : }
3229 :
3230 : /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
3231 : underneath as appropriate. */
3232 :
3233 : static enum gimplify_status
3234 13256919 : gimplify_conversion (tree *expr_p)
3235 : {
3236 13256919 : location_t loc = EXPR_LOCATION (*expr_p);
3237 13256919 : gcc_assert (CONVERT_EXPR_P (*expr_p));
3238 :
3239 : /* Then strip away all but the outermost conversion. */
3240 13256919 : STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
3241 :
3242 : /* And remove the outermost conversion if it's useless. */
3243 13256919 : if (tree_ssa_useless_type_conversion (*expr_p))
3244 0 : *expr_p = TREE_OPERAND (*expr_p, 0);
3245 :
3246 : /* If we still have a conversion at the toplevel,
3247 : then canonicalize some constructs. */
3248 13256919 : if (CONVERT_EXPR_P (*expr_p))
3249 : {
3250 13256919 : tree sub = TREE_OPERAND (*expr_p, 0);
3251 :
3252 : /* If a NOP conversion is changing the type of a COMPONENT_REF
3253 : expression, then canonicalize its type now in order to expose more
3254 : redundant conversions. */
3255 13256919 : if (TREE_CODE (sub) == COMPONENT_REF)
3256 276218 : canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
3257 :
3258 : /* If a NOP conversion is changing a pointer to array of foo
3259 : to a pointer to foo, embed that change in the ADDR_EXPR. */
3260 12980701 : else if (TREE_CODE (sub) == ADDR_EXPR)
3261 560455 : canonicalize_addr_expr (expr_p);
3262 : }
3263 :
3264 : /* If we have a conversion to a non-register type force the
3265 : use of a VIEW_CONVERT_EXPR instead. */
3266 13256919 : if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
3267 90 : *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
3268 90 : TREE_OPERAND (*expr_p, 0));
3269 :
3270 : /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
3271 13256919 : if (TREE_CODE (*expr_p) == CONVERT_EXPR)
3272 233152 : TREE_SET_CODE (*expr_p, NOP_EXPR);
3273 :
3274 13256919 : return GS_OK;
3275 : }
3276 :
3277 : /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
3278 : DECL_VALUE_EXPR, and it's worth re-examining things. */
3279 :
3280 : static enum gimplify_status
3281 129894985 : gimplify_var_or_parm_decl (tree *expr_p)
3282 : {
3283 129894985 : tree decl = *expr_p;
3284 :
3285 : /* ??? If this is a local variable, and it has not been seen in any
3286 : outer BIND_EXPR, then it's probably the result of a duplicate
3287 : declaration, for which we've already issued an error. It would
3288 : be really nice if the front end wouldn't leak these at all.
3289 : Currently the only known culprit is C++ destructors, as seen
3290 : in g++.old-deja/g++.jason/binding.C.
3291 : Another possible culpit are size expressions for variably modified
3292 : types which are lost in the FE or not gimplified correctly. */
3293 129894985 : if (VAR_P (decl)
3294 101090116 : && !DECL_SEEN_IN_BIND_EXPR_P (decl)
3295 17216926 : && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
3296 129955081 : && decl_function_context (decl) == current_function_decl)
3297 : {
3298 77 : gcc_assert (seen_error ());
3299 : return GS_ERROR;
3300 : }
3301 :
3302 : /* When within an OMP context, notice uses of variables. */
3303 129894908 : if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
3304 : return GS_ALL_DONE;
3305 :
3306 : /* If the decl is an alias for another expression, substitute it now. */
3307 129889088 : if (DECL_HAS_VALUE_EXPR_P (decl))
3308 : {
3309 460040 : *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
3310 460040 : return GS_OK;
3311 : }
3312 :
3313 : return GS_ALL_DONE;
3314 : }
3315 :
3316 : /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
3317 :
3318 : static void
3319 113126522 : recalculate_side_effects (tree t)
3320 : {
3321 113126522 : enum tree_code code = TREE_CODE (t);
3322 113126522 : int len = TREE_OPERAND_LENGTH (t);
3323 113126522 : int i;
3324 :
3325 113126522 : switch (TREE_CODE_CLASS (code))
3326 : {
3327 671638 : case tcc_expression:
3328 671638 : switch (code)
3329 : {
3330 : case INIT_EXPR:
3331 : case MODIFY_EXPR:
3332 : case VA_ARG_EXPR:
3333 : case PREDECREMENT_EXPR:
3334 : case PREINCREMENT_EXPR:
3335 : case POSTDECREMENT_EXPR:
3336 : case POSTINCREMENT_EXPR:
3337 : /* All of these have side-effects, no matter what their
3338 : operands are. */
3339 : return;
3340 :
3341 : default:
3342 : break;
3343 : }
3344 : /* Fall through. */
3345 :
3346 113126518 : case tcc_comparison: /* a comparison expression */
3347 113126518 : case tcc_unary: /* a unary arithmetic expression */
3348 113126518 : case tcc_binary: /* a binary arithmetic expression */
3349 113126518 : case tcc_reference: /* a reference */
3350 113126518 : case tcc_vl_exp: /* a function call */
3351 113126518 : TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3352 368497703 : for (i = 0; i < len; ++i)
3353 : {
3354 255371185 : tree op = TREE_OPERAND (t, i);
3355 255371185 : if (op && TREE_SIDE_EFFECTS (op))
3356 1298828 : TREE_SIDE_EFFECTS (t) = 1;
3357 : }
3358 : break;
3359 :
3360 : case tcc_constant:
3361 : /* No side-effects. */
3362 : return;
3363 :
3364 : case tcc_declaration:
3365 : /* These can have side-effects if TREE_THIS_VOLATILE,
3366 : but those should be set elsewhere, not in
3367 : recalculate_side_effects. Can be triggered e.g. if
3368 : a comparison is folded into one of its operands. */
3369 : return;
3370 :
3371 2 : default:
3372 2 : if (code == SSA_NAME)
3373 : /* No side-effects. */
3374 : return;
3375 0 : gcc_unreachable ();
3376 : }
3377 : }
3378 :
3379 : /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
3380 : node *EXPR_P.
3381 :
3382 : compound_lval
3383 : : min_lval '[' val ']'
3384 : | min_lval '.' ID
3385 : | compound_lval '[' val ']'
3386 : | compound_lval '.' ID
3387 :
3388 : This is not part of the original SIMPLE definition, which separates
3389 : array and member references, but it seems reasonable to handle them
3390 : together. Also, this way we don't run into problems with union
3391 : aliasing; gcc requires that for accesses through a union to alias, the
3392 : union reference must be explicit, which was not always the case when we
3393 : were splitting up array and member refs.
3394 :
3395 : PRE_P points to the sequence where side effects that must happen before
3396 : *EXPR_P should be stored.
3397 :
3398 : POST_P points to the sequence where side effects that must happen after
3399 : *EXPR_P should be stored. */
3400 :
3401 : static enum gimplify_status
3402 30724720 : gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3403 : fallback_t fallback)
3404 : {
3405 30724720 : tree *p;
3406 30724720 : enum gimplify_status ret = GS_ALL_DONE, tret;
3407 30724720 : int i;
3408 30724720 : location_t loc = EXPR_LOCATION (*expr_p);
3409 30724720 : tree expr = *expr_p;
3410 :
3411 : /* Create a stack of the subexpressions so later we can walk them in
3412 : order from inner to outer. */
3413 30724720 : auto_vec<tree, 10> expr_stack;
3414 :
3415 : /* We can handle anything that get_inner_reference can deal with. */
3416 74122659 : for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
3417 : {
3418 43464054 : restart:
3419 : /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
3420 74188774 : if (TREE_CODE (*p) == INDIRECT_REF)
3421 4048370 : *p = fold_indirect_ref_loc (loc, *p);
3422 :
3423 74188774 : if (handled_component_p (*p))
3424 : ;
3425 : /* Expand DECL_VALUE_EXPR now. In some cases that may expose
3426 : additional COMPONENT_REFs. */
3427 13568772 : else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
3428 32241510 : && gimplify_var_or_parm_decl (p) == GS_OK)
3429 66115 : goto restart;
3430 : else
3431 : break;
3432 :
3433 43397939 : expr_stack.safe_push (*p);
3434 : }
3435 :
3436 30724720 : gcc_assert (expr_stack.length ());
3437 :
3438 : /* Now EXPR_STACK is a stack of pointers to all the refs we've
3439 : walked through and P points to the innermost expression.
3440 :
3441 : Java requires that we elaborated nodes in source order. That
3442 : means we must gimplify the inner expression followed by each of
3443 : the indices, in order. But we can't gimplify the inner
3444 : expression until we deal with any variable bounds, sizes, or
3445 : positions in order to deal with PLACEHOLDER_EXPRs.
3446 :
3447 : The base expression may contain a statement expression that
3448 : has declarations used in size expressions, so has to be
3449 : gimplified before gimplifying the size expressions.
3450 :
3451 : So we do this in three steps. First we deal with variable
3452 : bounds, sizes, and positions, then we gimplify the base and
3453 : ensure it is memory if needed, then we deal with the annotations
3454 : for any variables in the components and any indices, from left
3455 : to right. */
3456 :
3457 30724720 : bool need_non_reg = false;
3458 74122656 : for (i = expr_stack.length () - 1; i >= 0; i--)
3459 : {
3460 43397939 : tree t = expr_stack[i];
3461 :
3462 43397939 : if (error_operand_p (TREE_OPERAND (t, 0)))
3463 : return GS_ERROR;
3464 :
3465 43397936 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3466 : {
3467 : /* Deal with the low bound and element type size and put them into
3468 : the ARRAY_REF. If these values are set, they have already been
3469 : gimplified. */
3470 9072815 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3471 : {
3472 9007994 : tree low = unshare_expr (array_ref_low_bound (t));
3473 9007994 : if (!is_gimple_min_invariant (low))
3474 : {
3475 0 : TREE_OPERAND (t, 2) = low;
3476 : }
3477 : }
3478 :
3479 9072815 : if (TREE_OPERAND (t, 3) == NULL_TREE)
3480 : {
3481 9059048 : tree elmt_size = array_ref_element_size (t);
3482 9059048 : if (!is_gimple_min_invariant (elmt_size))
3483 : {
3484 8601 : elmt_size = unshare_expr (elmt_size);
3485 8601 : tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
3486 8601 : tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
3487 :
3488 : /* Divide the element size by the alignment of the element
3489 : type (above). */
3490 8601 : elmt_size = size_binop_loc (loc, EXACT_DIV_EXPR,
3491 : elmt_size, factor);
3492 :
3493 8601 : TREE_OPERAND (t, 3) = elmt_size;
3494 : }
3495 : }
3496 : need_non_reg = true;
3497 : }
3498 34325121 : else if (TREE_CODE (t) == COMPONENT_REF)
3499 : {
3500 : /* Set the field offset into T and gimplify it. */
3501 33013952 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3502 : {
3503 33013573 : tree offset = component_ref_field_offset (t);
3504 33013573 : if (!is_gimple_min_invariant (offset))
3505 : {
3506 287 : offset = unshare_expr (offset);
3507 287 : tree field = TREE_OPERAND (t, 1);
3508 287 : tree factor
3509 287 : = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
3510 :
3511 : /* Divide the offset by its alignment. */
3512 287 : offset = size_binop_loc (loc, EXACT_DIV_EXPR,
3513 : offset, factor);
3514 :
3515 287 : TREE_OPERAND (t, 2) = offset;
3516 : }
3517 : }
3518 : need_non_reg = true;
3519 : }
3520 1311169 : else if (!is_gimple_reg_type (TREE_TYPE (t)))
3521 : /* When the result of an operation, in particular a VIEW_CONVERT_EXPR
3522 : is a non-register type then require the base object to be a
3523 : non-register as well. */
3524 42369991 : need_non_reg = true;
3525 : }
3526 :
3527 : /* Step 2 is to gimplify the base expression. Make sure lvalue is set
3528 : so as to match the min_lval predicate. Failure to do so may result
3529 : in the creation of large aggregate temporaries. */
3530 30724717 : tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3531 : fallback | fb_lvalue);
3532 30724717 : ret = MIN (ret, tret);
3533 30724717 : if (ret == GS_ERROR)
3534 : return GS_ERROR;
3535 :
3536 : /* Step 2a: if we have component references we do not support on
3537 : registers then make sure the base isn't a register. Of course
3538 : we can only do so if an rvalue is OK. */
3539 30724717 : if (need_non_reg && (fallback & fb_rvalue))
3540 22891633 : prepare_gimple_addressable (p, pre_p);
3541 :
3542 :
3543 : /* Step 3: gimplify size expressions and the indices and operands of
3544 : ARRAY_REF. During this loop we also remove any useless conversions.
3545 : If we operate on a register also make sure to properly gimplify
3546 : to individual operations. */
3547 :
3548 30724717 : bool reg_operations = is_gimple_reg (*p);
3549 104847370 : for (; expr_stack.length () > 0; )
3550 : {
3551 43397936 : tree t = expr_stack.pop ();
3552 :
3553 43397936 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3554 : {
3555 9072815 : gcc_assert (!reg_operations);
3556 :
3557 : /* Gimplify the low bound and element type size. */
3558 9072815 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3559 : is_gimple_reg, fb_rvalue);
3560 9072815 : ret = MIN (ret, tret);
3561 :
3562 9072815 : tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
3563 : is_gimple_reg, fb_rvalue);
3564 9072815 : ret = MIN (ret, tret);
3565 :
3566 : /* Gimplify the dimension. */
3567 9072815 : tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3568 : is_gimple_val, fb_rvalue);
3569 9072815 : ret = MIN (ret, tret);
3570 : }
3571 34325121 : else if (TREE_CODE (t) == COMPONENT_REF)
3572 : {
3573 33013952 : gcc_assert (!reg_operations);
3574 :
3575 33013952 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3576 : is_gimple_reg, fb_rvalue);
3577 33013952 : ret = MIN (ret, tret);
3578 : }
3579 1311169 : else if (reg_operations)
3580 : {
3581 908863 : tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
3582 : is_gimple_val, fb_rvalue);
3583 1311169 : ret = MIN (ret, tret);
3584 : }
3585 :
3586 43397936 : STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
3587 :
3588 : /* The innermost expression P may have originally had
3589 : TREE_SIDE_EFFECTS set which would have caused all the outer
3590 : expressions in *EXPR_P leading to P to also have had
3591 : TREE_SIDE_EFFECTS set. */
3592 43397936 : recalculate_side_effects (t);
3593 : }
3594 :
3595 : /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3596 30724717 : if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3597 : {
3598 18687080 : canonicalize_component_ref (expr_p);
3599 : }
3600 :
3601 30724717 : expr_stack.release ();
3602 :
3603 30724717 : gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3604 :
3605 : return ret;
3606 30724720 : }
3607 :
3608 : /* Gimplify the self modifying expression pointed to by EXPR_P
3609 : (++, --, +=, -=).
3610 :
3611 : PRE_P points to the list where side effects that must happen before
3612 : *EXPR_P should be stored.
3613 :
3614 : POST_P points to the list where side effects that must happen after
3615 : *EXPR_P should be stored.
3616 :
3617 : WANT_VALUE is nonzero iff we want to use the value of this expression
3618 : in another expression.
3619 :
3620 : ARITH_TYPE is the type the computation should be performed in. */
3621 :
3622 : enum gimplify_status
3623 1151494 : gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3624 : bool want_value, tree arith_type)
3625 : {
3626 1151494 : enum tree_code code;
3627 1151494 : tree lhs, lvalue, rhs, t1;
3628 1151494 : gimple_seq post = NULL, *orig_post_p = post_p;
3629 1151494 : bool postfix;
3630 1151494 : enum tree_code arith_code;
3631 1151494 : enum gimplify_status ret;
3632 1151494 : location_t loc = EXPR_LOCATION (*expr_p);
3633 :
3634 1151494 : code = TREE_CODE (*expr_p);
3635 :
3636 1151494 : gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3637 : || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3638 :
3639 : /* Prefix or postfix? */
3640 1151494 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3641 : /* Faster to treat as prefix if result is not used. */
3642 : postfix = want_value;
3643 : else
3644 : postfix = false;
3645 :
3646 : /* For postfix, make sure the inner expression's post side effects
3647 : are executed after side effects from this expression. */
3648 410425 : if (postfix)
3649 1151494 : post_p = &post;
3650 :
3651 : /* Add or subtract? */
3652 1151494 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3653 : arith_code = PLUS_EXPR;
3654 : else
3655 45802 : arith_code = MINUS_EXPR;
3656 :
3657 : /* Gimplify the LHS into a GIMPLE lvalue. */
3658 1151494 : lvalue = TREE_OPERAND (*expr_p, 0);
3659 1151494 : ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3660 1151494 : if (ret == GS_ERROR)
3661 : return ret;
3662 :
3663 : /* Extract the operands to the arithmetic operation. */
3664 1151491 : lhs = lvalue;
3665 1151491 : rhs = TREE_OPERAND (*expr_p, 1);
3666 :
3667 : /* For postfix operator, we evaluate the LHS to an rvalue and then use
3668 : that as the result value and in the postqueue operation. */
3669 1151491 : if (postfix)
3670 : {
3671 189005 : ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3672 189005 : if (ret == GS_ERROR)
3673 : return ret;
3674 :
3675 189005 : lhs = get_initialized_tmp_var (lhs, pre_p);
3676 : }
3677 :
3678 : /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3679 1151491 : if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3680 : {
3681 238345 : rhs = convert_to_ptrofftype_loc (loc, rhs);
3682 238345 : if (arith_code == MINUS_EXPR)
3683 10233 : rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3684 238345 : t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3685 : }
3686 : else
3687 913146 : t1 = fold_convert (TREE_TYPE (*expr_p),
3688 : fold_build2 (arith_code, arith_type,
3689 : fold_convert (arith_type, lhs),
3690 : fold_convert (arith_type, rhs)));
3691 :
3692 1151491 : if (postfix)
3693 : {
3694 189005 : gimplify_assign (lvalue, t1, pre_p);
3695 189005 : gimplify_seq_add_seq (orig_post_p, post);
3696 189005 : *expr_p = lhs;
3697 189005 : return GS_ALL_DONE;
3698 : }
3699 : else
3700 : {
3701 962486 : *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3702 962486 : return GS_OK;
3703 : }
3704 : }
3705 :
3706 : /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3707 :
3708 : static void
3709 83287573 : maybe_with_size_expr (tree *expr_p)
3710 : {
3711 83287573 : tree expr = *expr_p;
3712 83287573 : tree type = TREE_TYPE (expr);
3713 83287573 : tree size;
3714 :
3715 : /* If we've already wrapped this or the type is error_mark_node, we can't do
3716 : anything. */
3717 83287573 : if (TREE_CODE (expr) == WITH_SIZE_EXPR
3718 83287448 : || type == error_mark_node)
3719 : return;
3720 :
3721 : /* If the size isn't known or is a constant, we have nothing to do. */
3722 83287300 : size = TYPE_SIZE_UNIT (type);
3723 83287300 : if (!size || poly_int_tree_p (size))
3724 : return;
3725 :
3726 : /* Otherwise, make a WITH_SIZE_EXPR. */
3727 1698 : size = unshare_expr (size);
3728 1698 : size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3729 1698 : *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3730 : }
3731 :
3732 : /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3733 : Store any side-effects in PRE_P. CALL_LOCATION is the location of
3734 : the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3735 : gimplified to an SSA name. */
3736 :
3737 : enum gimplify_status
3738 34689036 : gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3739 : bool allow_ssa)
3740 : {
3741 34689036 : bool (*test) (tree);
3742 34689036 : fallback_t fb;
3743 :
3744 : /* In general, we allow lvalues for function arguments to avoid
3745 : extra overhead of copying large aggregates out of even larger
3746 : aggregates into temporaries only to copy the temporaries to
3747 : the argument list. Make optimizers happy by pulling out to
3748 : temporaries those types that fit in registers. */
3749 34689036 : if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3750 : test = is_gimple_val, fb = fb_rvalue;
3751 : else
3752 : {
3753 1609780 : test = is_gimple_lvalue, fb = fb_either;
3754 : /* Also strip a TARGET_EXPR that would force an extra copy. */
3755 1609780 : if (TREE_CODE (*arg_p) == TARGET_EXPR)
3756 : {
3757 332507 : tree init = TARGET_EXPR_INITIAL (*arg_p);
3758 332507 : if (init
3759 332504 : && !VOID_TYPE_P (TREE_TYPE (init))
3760 : /* Currently, due to c++/116015, it is not desirable to
3761 : strip a TARGET_EXPR whose initializer is a {}. The
3762 : problem is that if we do elide it, we also have to
3763 : replace all the occurrences of the slot temporary in the
3764 : initializer with the temporary created for the argument.
3765 : But we do not have that temporary yet so the replacement
3766 : would be quite awkward and it might be needed to resort
3767 : back to a PLACEHOLDER_EXPR. Note that stripping the
3768 : TARGET_EXPR wouldn't help anyway, as gimplify_expr would
3769 : just allocate a temporary to store the CONSTRUCTOR into.
3770 : (FIXME PR116375.)
3771 :
3772 : See convert_for_arg_passing for the C++ code that marks
3773 : the TARGET_EXPR as eliding or not. */
3774 623014 : && TREE_CODE (init) != CONSTRUCTOR)
3775 252024 : *arg_p = init;
3776 : }
3777 : }
3778 :
3779 : /* If this is a variable sized type, we must remember the size. */
3780 34689036 : maybe_with_size_expr (arg_p);
3781 :
3782 : /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3783 : /* Make sure arguments have the same location as the function call
3784 : itself. */
3785 34689036 : protected_set_expr_location (*arg_p, call_location);
3786 :
3787 : /* There is a sequence point before a function call. Side effects in
3788 : the argument list must occur before the actual call. So, when
3789 : gimplifying arguments, force gimplify_expr to use an internal
3790 : post queue which is then appended to the end of PRE_P. */
3791 34689036 : return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3792 : }
3793 :
3794 : /* Don't fold inside offloading or taskreg regions: it can break code by
3795 : adding decl references that weren't in the source. We'll do it during
3796 : omplower pass instead. */
3797 :
3798 : static bool
3799 60357768 : maybe_fold_stmt (gimple_stmt_iterator *gsi)
3800 : {
3801 60357768 : struct gimplify_omp_ctx *ctx;
3802 60886445 : for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3803 1200657 : if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3804 : return false;
3805 533696 : else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3806 : return false;
3807 : /* Delay folding of builtins until the IL is in consistent state
3808 : so the diagnostic machinery can do a better job. */
3809 59685788 : if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3810 : return false;
3811 57283228 : return fold_stmt (gsi);
3812 : }
3813 :
3814 : static tree
3815 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
3816 : tree construct_context);
3817 :
3818 :
3819 : /* Helper function for calls to omp_dynamic_cond: find the current
3820 : enclosing block in the gimplification context. */
3821 : static tree
3822 359 : find_supercontext (void)
3823 : {
3824 359 : vec<gbind *>stack = gimple_bind_expr_stack ();
3825 686 : for (int i = stack.length () - 1; i >= 0; i++)
3826 : {
3827 327 : gbind *b = stack[i];
3828 327 : if (b->block)
3829 : return b->block;
3830 : }
3831 : return NULL_TREE;
3832 : }
3833 :
3834 : /* OpenMP: Handle the append_args and adjust_args clauses of
3835 : declare_variant for EXPR, which is a CALL_EXPR whose CALL_EXPR_FN
3836 : is the variant, within a dispatch construct with clauses DISPATCH_CLAUSES.
3837 : WANT_VALUE and POINTERIZE are as for expand_variant_call_expr.
3838 :
3839 : 'append_args' causes interop objects are added after the last regular
3840 : (nonhidden, nonvariadic) arguments of the variant function.
3841 : 'adjust_args' with need_device_{addr,ptr} converts the pointer target of
3842 : a pointer from a host to a device address. This uses either the default
3843 : device or the passed device number, which then sets the default device
3844 : address. */
3845 : static tree
3846 471 : modify_call_for_omp_dispatch (tree expr, tree dispatch_clauses,
3847 : bool want_value, bool pointerize)
3848 : {
3849 471 : location_t loc = EXPR_LOCATION (expr);
3850 471 : tree fndecl = get_callee_fndecl (expr);
3851 :
3852 : /* Skip processing if we don't get the expected call form. */
3853 471 : if (!fndecl)
3854 : return expr;
3855 :
3856 471 : tree init_code = NULL_TREE;
3857 471 : tree cleanup = NULL_TREE;
3858 471 : tree clobbers = NULL_TREE;
3859 471 : int nargs = call_expr_nargs (expr);
3860 471 : tree dispatch_device_num = NULL_TREE;
3861 471 : tree dispatch_interop = NULL_TREE;
3862 471 : tree dispatch_append_args = NULL_TREE;
3863 471 : int nfirst_args = 0;
3864 471 : tree dispatch_adjust_args_list
3865 471 : = lookup_attribute ("omp declare variant variant args",
3866 471 : DECL_ATTRIBUTES (fndecl));
3867 :
3868 471 : if (dispatch_adjust_args_list)
3869 : {
3870 389 : dispatch_adjust_args_list = TREE_VALUE (dispatch_adjust_args_list);
3871 389 : dispatch_append_args = TREE_CHAIN (dispatch_adjust_args_list);
3872 389 : if (TREE_PURPOSE (dispatch_adjust_args_list) == NULL_TREE
3873 389 : && TREE_VALUE (dispatch_adjust_args_list) == NULL_TREE)
3874 : dispatch_adjust_args_list = NULL_TREE;
3875 : }
3876 389 : if (dispatch_append_args)
3877 : {
3878 245 : nfirst_args = tree_to_shwi (TREE_PURPOSE (dispatch_append_args));
3879 245 : dispatch_append_args = TREE_VALUE (dispatch_append_args);
3880 : }
3881 471 : dispatch_device_num = omp_find_clause (dispatch_clauses, OMP_CLAUSE_DEVICE);
3882 471 : if (dispatch_device_num)
3883 287 : dispatch_device_num = OMP_CLAUSE_DEVICE_ID (dispatch_device_num);
3884 471 : dispatch_interop = omp_find_clause (dispatch_clauses, OMP_CLAUSE_INTEROP);
3885 471 : int nappend = 0, ninterop = 0;
3886 1351 : for (tree t = dispatch_append_args; t; t = TREE_CHAIN (t))
3887 409 : nappend++;
3888 :
3889 : /* FIXME: error checking should be taken out of this function and
3890 : handled before any attempt at filtering or resolution happens.
3891 : Otherwise whether or not diagnostics appear is determined by
3892 : GCC internals, how good the front ends are at constant-folding,
3893 : the split between early/late resolution, etc instead of the code
3894 : as written by the user. */
3895 471 : if (dispatch_interop)
3896 861 : for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
3897 605 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3898 409 : ninterop++;
3899 471 : if (dispatch_interop && !dispatch_device_num)
3900 : {
3901 7 : gcc_checking_assert (ninterop > 1);
3902 7 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3903 : "the %<device%> clause must be present if the %<interop%> "
3904 : "clause has more than one list item");
3905 : }
3906 471 : if (nappend < ninterop)
3907 : {
3908 48 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3909 : "number of list items in %<interop%> clause (%d) "
3910 : "exceeds the number of %<append_args%> items (%d) for "
3911 : "%<declare variant%> candidate %qD", ninterop, nappend, fndecl);
3912 88 : inform (dispatch_append_args
3913 8 : ? EXPR_LOCATION (TREE_PURPOSE (dispatch_append_args))
3914 40 : : DECL_SOURCE_LOCATION (fndecl),
3915 : "%<declare variant%> candidate %qD declared here", fndecl);
3916 48 : ninterop = nappend;
3917 : }
3918 471 : if (dispatch_append_args)
3919 : {
3920 245 : tree *buffer = XALLOCAVEC (tree, nargs + nappend);
3921 245 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3922 : /* Copy the first arguments; insert then the interop objects,
3923 : and then copy the rest (nargs - nfirst_args) args. */
3924 245 : int i;
3925 679 : for (i = 0; i < nfirst_args; i++)
3926 : {
3927 434 : arg = TREE_CHAIN (arg);
3928 434 : buffer[i] = CALL_EXPR_ARG (expr, i);
3929 : }
3930 : int j = ninterop;
3931 583 : for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
3932 338 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3933 338 : buffer[i + --j] = OMP_CLAUSE_DECL (t);
3934 245 : gcc_checking_assert (j == 0);
3935 :
3936 : /* Do we need to create additional interop objects? */
3937 245 : if (ninterop < nappend)
3938 : {
3939 56 : if (dispatch_device_num == NULL_TREE)
3940 : /* Not remapping device number. */
3941 22 : dispatch_device_num = build_int_cst (integer_type_node,
3942 : GOMP_DEVICE_DEFAULT_OMP_61);
3943 56 : int nnew = nappend - ninterop;
3944 56 : tree nobjs = build_int_cst (integer_type_node, nnew);
3945 56 : tree a, t;
3946 :
3947 : /* Skip to the append_args clause for the first constructed
3948 : interop argument. */
3949 56 : tree apparg = dispatch_append_args;
3950 83 : for (j = 0; j < ninterop; j++)
3951 27 : apparg = TREE_CHAIN (apparg);
3952 :
3953 : /* omp_interop_t *objs[n]; */
3954 56 : tree objtype = build_pointer_type (pointer_sized_int_node);
3955 56 : t = build_array_type_nelts (objtype, nnew);
3956 56 : tree objs = create_tmp_var (t, "interopobjs");
3957 :
3958 : /* int target_tgtsync[n]; */
3959 56 : t = build_array_type_nelts (integer_type_node, nnew);
3960 56 : tree target_tgtsync = create_tmp_var (t, "tgt_tgtsync");
3961 :
3962 : /* Scan first to determine if we need a prefer_type array. */
3963 56 : tree prefer_type = NULL_TREE;
3964 56 : tree prefer_type_type = NULL_TREE;
3965 141 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
3966 64 : if (TREE_VALUE (a) != NULL_TREE)
3967 : {
3968 : /* const char *prefer_type[n]; */
3969 35 : t = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3970 35 : prefer_type_type = build_pointer_type (t);
3971 35 : t = build_array_type_nelts (prefer_type_type, nnew);
3972 35 : prefer_type = create_tmp_var (t, "pref_type");
3973 35 : break;
3974 : }
3975 :
3976 : /* Initialize the arrays, generating temp vars and clobbers for
3977 : the interop objects. (The constructed array holding the
3978 : pointers to these objects shouldn't need clobbering as there's
3979 : no reason for GOMP_interop to modify its contents.) */
3980 127 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
3981 : {
3982 : /* The allocated temporaries for the interop objects
3983 : have type omp_interop_t, which is an integer type that
3984 : can encode a pointer. */
3985 71 : tree objvar = create_tmp_var (pointer_sized_int_node, "interop");
3986 71 : buffer[i + j] = objvar;
3987 71 : TREE_ADDRESSABLE (objvar) = 1;
3988 : /* Generate a clobber for the temporary for when we're done
3989 : with it. */
3990 71 : tree c = build_clobber (pointer_sized_int_node,
3991 : CLOBBER_OBJECT_END);
3992 71 : c = build2 (MODIFY_EXPR, pointer_sized_int_node, objvar, c);
3993 71 : if (clobbers)
3994 15 : clobbers = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
3995 : c, clobbers);
3996 : else
3997 : clobbers = c;
3998 :
3999 : /* objs[offset] = &objvar; */
4000 71 : tree offset = build_int_cst (integer_type_node, j - ninterop);
4001 71 : tree init = build4 (ARRAY_REF, objtype, objs, offset,
4002 : NULL_TREE, NULL_TREE);
4003 71 : init = build2 (MODIFY_EXPR, objtype, init,
4004 : build_fold_addr_expr (objvar));
4005 71 : if (init_code)
4006 15 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4007 : init_code, init);
4008 : else
4009 : init_code = init;
4010 :
4011 : /* target_tgtsync[offset] = tgt;
4012 : (Don't blame me, I didn't design the encoding of this
4013 : info into the dispatch interop clause data structure,
4014 : but the runtime wants a bit mask.) */
4015 71 : tree tree_tgt = TREE_OPERAND (TREE_PURPOSE (a), 0);
4016 71 : int tgt = 0;
4017 71 : if (TREE_PURPOSE (tree_tgt) == boolean_true_node)
4018 42 : tgt |= GOMP_INTEROP_TARGET;
4019 71 : if (TREE_VALUE (tree_tgt) == boolean_true_node)
4020 36 : tgt |= GOMP_INTEROP_TARGETSYNC;
4021 71 : init = build4 (ARRAY_REF, integer_type_node,
4022 : target_tgtsync, offset, NULL_TREE, NULL_TREE);
4023 71 : init = build2 (MODIFY_EXPR, integer_type_node, init,
4024 71 : build_int_cst (integer_type_node, tgt));
4025 71 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4026 : init_code, init);
4027 :
4028 71 : if (prefer_type)
4029 : {
4030 45 : tree pref = TREE_VALUE (a);
4031 45 : if (pref == NULL_TREE)
4032 6 : pref = null_pointer_node;
4033 : else
4034 39 : pref = build_fold_addr_expr (pref);
4035 45 : init = build4 (ARRAY_REF, prefer_type_type, prefer_type,
4036 : offset, NULL_TREE, NULL_TREE);
4037 45 : init = build2 (MODIFY_EXPR, prefer_type_type, init,
4038 : pref);
4039 45 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4040 : init_code, init);
4041 : }
4042 : }
4043 :
4044 56 : objs = build_fold_addr_expr (objs);
4045 56 : target_tgtsync = build_fold_addr_expr (target_tgtsync);
4046 56 : prefer_type = prefer_type ? build_fold_addr_expr (prefer_type)
4047 : : null_pointer_node;
4048 56 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
4049 56 : tree create
4050 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4051 : nobjs, objs, target_tgtsync, prefer_type,
4052 : integer_zero_node, null_pointer_node,
4053 : integer_zero_node, null_pointer_node,
4054 : integer_zero_node, null_pointer_node);
4055 56 : if (init_code)
4056 56 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (create),
4057 : init_code, create);
4058 : else
4059 : init_code = create;
4060 :
4061 56 : cleanup
4062 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4063 : integer_zero_node, null_pointer_node,
4064 : null_pointer_node, null_pointer_node,
4065 : integer_zero_node, null_pointer_node,
4066 : nobjs, objs,
4067 : integer_zero_node, null_pointer_node);
4068 56 : if (clobbers)
4069 56 : cleanup = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4070 : cleanup, clobbers);
4071 : }
4072 :
4073 654 : for (j = 0; j < nappend; j++)
4074 : {
4075 : /* Fortran permits by-reference or by-value for the dummy arg
4076 : and by-value, by-reference, ptr by-reference as actual
4077 : argument. Handle this. */
4078 409 : tree obj = buffer[i + j]; // interop object
4079 409 : tree a2 = TREE_VALUE (arg); // parameter type
4080 766 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4081 436 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (obj))))
4082 : {
4083 32 : tree t = TREE_TYPE (TREE_TYPE (obj));
4084 32 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (t)));
4085 32 : obj = fold_build1 (INDIRECT_REF, t, obj);
4086 : }
4087 750 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4088 420 : && INTEGRAL_TYPE_P (a2))
4089 : {
4090 43 : tree t = TREE_TYPE (TREE_TYPE (obj));
4091 43 : gcc_checking_assert (INTEGRAL_TYPE_P (t));
4092 43 : obj = fold_build1 (INDIRECT_REF, t, obj);
4093 : }
4094 497 : else if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
4095 461 : && POINTER_TYPE_P (a2))
4096 : {
4097 18 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4098 18 : obj = build_fold_addr_expr (obj);
4099 : }
4100 348 : else if (!INTEGRAL_TYPE_P (a2)
4101 348 : || !INTEGRAL_TYPE_P (TREE_TYPE (obj)))
4102 : {
4103 36 : tree t = TREE_TYPE (obj);
4104 36 : gcc_checking_assert (POINTER_TYPE_P (t)
4105 : && POINTER_TYPE_P (a2)
4106 : && INTEGRAL_TYPE_P (TREE_TYPE (t))
4107 : && INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4108 : }
4109 409 : buffer[i + j] = obj;
4110 409 : arg = TREE_CHAIN (arg);
4111 : }
4112 245 : i += nappend;
4113 534 : for (j = nfirst_args; j < nargs; j++)
4114 289 : buffer[i++] = CALL_EXPR_ARG (expr, j);
4115 245 : nargs += nappend;
4116 245 : tree call = expr;
4117 490 : expr = build_call_array_loc (EXPR_LOCATION (expr), TREE_TYPE (call),
4118 245 : CALL_EXPR_FN (call), nargs, buffer);
4119 :
4120 : /* Copy all CALL_EXPR flags. */
4121 245 : CALL_EXPR_STATIC_CHAIN (expr) = CALL_EXPR_STATIC_CHAIN (call);
4122 245 : CALL_EXPR_TAILCALL (expr) = CALL_EXPR_TAILCALL (call);
4123 245 : CALL_EXPR_RETURN_SLOT_OPT (expr)
4124 245 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4125 245 : CALL_FROM_THUNK_P (expr) = CALL_FROM_THUNK_P (call);
4126 245 : SET_EXPR_LOCATION (expr, EXPR_LOCATION (call));
4127 245 : CALL_EXPR_VA_ARG_PACK (expr) = CALL_EXPR_VA_ARG_PACK (call);
4128 : }
4129 :
4130 : /* Nothing to do for adjust_args? */
4131 471 : if (!dispatch_adjust_args_list || !TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
4132 260 : goto add_cleanup;
4133 :
4134 : /* Handle adjust_args. */
4135 966 : for (int i = 0; i < nargs; i++)
4136 : {
4137 755 : tree *arg_p = &CALL_EXPR_ARG (expr, i);
4138 :
4139 : /* Nothing to do if arg is constant null pointer. */
4140 755 : if (integer_zerop (*arg_p))
4141 6 : continue;
4142 :
4143 : bool need_device_ptr = false;
4144 : bool need_device_addr = false;
4145 2247 : for (int need_addr = 0; need_addr <= 1; need_addr++)
4146 2301 : for (tree arg = (need_addr
4147 1498 : ? TREE_VALUE (dispatch_adjust_args_list)
4148 1498 : : TREE_PURPOSE (dispatch_adjust_args_list));
4149 2301 : arg != NULL; arg = TREE_CHAIN (arg))
4150 : {
4151 1152 : if (TREE_VALUE (arg)
4152 1152 : && TREE_CODE (TREE_VALUE (arg)) == INTEGER_CST
4153 2304 : && wi::eq_p (i, wi::to_wide (TREE_VALUE (arg))))
4154 : {
4155 349 : if (need_addr)
4156 : need_device_addr = true;
4157 : else
4158 349 : need_device_ptr = true;
4159 : break;
4160 : }
4161 : }
4162 :
4163 749 : if (need_device_ptr || need_device_addr)
4164 : {
4165 740 : bool is_device_ptr = false;
4166 740 : bool has_device_addr = false;
4167 :
4168 740 : for (tree c = dispatch_clauses; c; c = TREE_CHAIN (c))
4169 : {
4170 454 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
4171 454 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
4172 : {
4173 164 : tree decl1 = DECL_NAME (OMP_CLAUSE_DECL (c));
4174 164 : tree decl2 = tree_strip_nop_conversions (*arg_p);
4175 164 : if (TREE_CODE (decl2) == ADDR_EXPR)
4176 19 : decl2 = TREE_OPERAND (decl2, 0);
4177 164 : if (VAR_P (decl2) || TREE_CODE (decl2) == PARM_DECL)
4178 : {
4179 140 : decl2 = DECL_NAME (decl2);
4180 140 : if (decl1 == decl2
4181 203 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
4182 : {
4183 58 : if (need_device_addr)
4184 0 : warning_at (OMP_CLAUSE_LOCATION (c),
4185 0 : OPT_Wopenmp,
4186 : "%<is_device_ptr%> for %qD does"
4187 : " not imply %<has_device_addr%> "
4188 : "required for %<need_device_addr%>",
4189 0 : OMP_CLAUSE_DECL (c));
4190 : is_device_ptr = true;
4191 : break;
4192 : }
4193 82 : else if (decl1 == decl2)
4194 : {
4195 5 : if (need_device_ptr)
4196 10 : warning_at (OMP_CLAUSE_LOCATION (c),
4197 5 : OPT_Wopenmp,
4198 : "%<has_device_addr%> for %qD does"
4199 : " not imply %<is_device_ptr%> "
4200 : "required for %<need_device_ptr%>",
4201 5 : OMP_CLAUSE_DECL (c));
4202 : has_device_addr = true;
4203 : break;
4204 : }
4205 : }
4206 : }
4207 : }
4208 :
4209 349 : if ((need_device_ptr && !is_device_ptr)
4210 58 : || (need_device_addr && !has_device_addr))
4211 : {
4212 291 : if (dispatch_device_num == NULL_TREE)
4213 : {
4214 : // device_num = omp_get_default_device ()
4215 105 : tree fn
4216 105 : = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
4217 105 : tree call = build_call_expr (fn, 0);
4218 105 : dispatch_device_num = create_tmp_var_raw (TREE_TYPE (call));
4219 105 : tree init
4220 105 : = build4 (TARGET_EXPR, TREE_TYPE (call),
4221 : dispatch_device_num, call, NULL_TREE, NULL_TREE);
4222 105 : if (init_code)
4223 0 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4224 : init_code, init);
4225 : else
4226 : init_code = init;
4227 : }
4228 :
4229 : // We want to emit the following statement:
4230 : // mapped_arg = omp_get_mapped_ptr (arg,
4231 : // device_num)
4232 : // but arg has to be the actual pointer, not a
4233 : // reference or a conversion expression.
4234 291 : tree actual_ptr
4235 291 : = ((TREE_CODE (*arg_p) == ADDR_EXPR)
4236 291 : ? TREE_OPERAND (*arg_p, 0)
4237 38 : : *arg_p);
4238 291 : if (TREE_CODE (actual_ptr) == NOP_EXPR
4239 291 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (actual_ptr, 0)))
4240 : == REFERENCE_TYPE))
4241 : {
4242 6 : actual_ptr = TREE_OPERAND (actual_ptr, 0);
4243 6 : actual_ptr = build1 (INDIRECT_REF,
4244 6 : TREE_TYPE (actual_ptr),
4245 : actual_ptr);
4246 : }
4247 291 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_MAPPED_PTR);
4248 291 : tree mapped_arg = build_call_expr_loc (loc, fn, 2, actual_ptr,
4249 : dispatch_device_num);
4250 :
4251 291 : if (TREE_CODE (*arg_p) == ADDR_EXPR
4252 291 : || (TREE_CODE (TREE_TYPE (actual_ptr)) == REFERENCE_TYPE))
4253 47 : mapped_arg = build_fold_addr_expr (mapped_arg);
4254 244 : else if (TREE_CODE (*arg_p) == NOP_EXPR)
4255 35 : mapped_arg = build1 (NOP_EXPR, TREE_TYPE (*arg_p),
4256 : mapped_arg);
4257 291 : *arg_p = mapped_arg;
4258 : }
4259 : }
4260 : }
4261 :
4262 471 : add_cleanup:
4263 471 : if (cleanup)
4264 : {
4265 56 : tree result = NULL_TREE;
4266 56 : if (want_value && pointerize)
4267 : {
4268 0 : tree tmp = create_tmp_var (build_pointer_type (TREE_TYPE (expr)),
4269 : "cleanuptmp");
4270 0 : result = build_simple_mem_ref (tmp);
4271 0 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp,
4272 : build_fold_addr_expr (expr));
4273 : }
4274 56 : else if (want_value)
4275 : {
4276 18 : tree tmp = create_tmp_var (TREE_TYPE (expr), "cleanuptmp");
4277 18 : result = tmp;
4278 18 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp, expr);
4279 : }
4280 56 : if (init_code)
4281 56 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4282 56 : expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, cleanup);
4283 :
4284 56 : if (result)
4285 18 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (result), expr, result);
4286 : }
4287 415 : else if (init_code)
4288 105 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4289 :
4290 : return expr;
4291 : }
4292 :
4293 : /* Helper function for gimplify_call_expr: handle "declare variant"
4294 : resolution and expansion of the CALL_EXPR EXPR. WANT_VALUE is true
4295 : if the result value of the call is needed; POINTERIZE is true if it
4296 : also needs to be pointerized. If OMP_DISPATCH_P is true, apply
4297 : associated transformations using DISPATCH_CLAUSES.
4298 : This function may return either the original call or some other
4299 : expression such as a conditional to select one of multiple calls.
4300 :
4301 : FIXME: this function is written to be independent of gimplifier internals
4302 : so that it could be moved to omp-general.cc and invoked from the
4303 : front ends instead, per PR115076. */
4304 :
4305 : static tree
4306 1326 : expand_variant_call_expr (tree expr, bool want_value, bool pointerize,
4307 : bool omp_dispatch_p, tree dispatch_clauses)
4308 : {
4309 : /* If we've already processed this call, stop now. This can happen
4310 : if the variant call resolves to the original function, or to
4311 : a dynamic conditional that includes the default call to the original
4312 : function. */
4313 1326 : gcc_assert (omp_resolved_variant_calls != NULL);
4314 1326 : if (omp_resolved_variant_calls->contains (expr))
4315 131 : return expr;
4316 :
4317 1195 : tree fndecl = get_callee_fndecl (expr);
4318 1195 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
4319 1195 : location_t loc = EXPR_LOCATION (expr);
4320 1195 : tree construct_context = omp_get_construct_context ();
4321 1195 : vec<struct omp_variant> all_candidates
4322 1195 : = omp_declare_variant_candidates (fndecl, construct_context);
4323 1195 : gcc_assert (!all_candidates.is_empty ());
4324 1195 : vec<struct omp_variant> candidates
4325 1195 : = omp_get_dynamic_candidates (all_candidates, construct_context);
4326 :
4327 : /* If the variant call could be resolved now, build a nest of COND_EXPRs
4328 : if there are dynamic candidates, and/or a new CALL_EXPR for each
4329 : candidate call. */
4330 1195 : if (!candidates.is_empty ())
4331 : {
4332 1183 : int n = candidates.length ();
4333 1183 : tree tail = NULL_TREE;
4334 :
4335 2404 : for (int i = n - 1; i >= 0; i--)
4336 : {
4337 1221 : if (tail)
4338 38 : gcc_assert (candidates[i].dynamic_selector);
4339 : else
4340 1183 : gcc_assert (!candidates[i].dynamic_selector);
4341 1221 : if (candidates[i].alternative == fndecl)
4342 : {
4343 : /* We should only get the original function back as the
4344 : default. */
4345 261 : gcc_assert (!tail);
4346 261 : omp_resolved_variant_calls->add (expr);
4347 261 : tail = expr;
4348 : }
4349 : else
4350 : {
4351 : /* For the final static selector, we can re-use the old
4352 : CALL_EXPR and just replace the function, unless it may
4353 : need dispatch argument modification. Otherwise,
4354 : make a copy of it. */
4355 960 : tree thiscall = (tail || omp_dispatch_p
4356 960 : ? unshare_expr (expr) : expr);
4357 960 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
4358 960 : candidates[i].alternative);
4359 960 : if (omp_dispatch_p)
4360 471 : thiscall = modify_call_for_omp_dispatch (thiscall,
4361 : dispatch_clauses,
4362 : want_value,
4363 : pointerize);
4364 960 : if (!tail)
4365 : tail = thiscall;
4366 : else
4367 76 : tail = build3 (COND_EXPR, TREE_TYPE (expr),
4368 38 : omp_dynamic_cond (candidates[i].selector,
4369 : find_supercontext ()),
4370 : thiscall, tail);
4371 : }
4372 : }
4373 : return tail;
4374 : }
4375 :
4376 : /* If we couldn't resolve the variant call now, expand it into a loop using
4377 : a switch and OMP_NEXT_VARIANT for dispatch. The ompdevlow pass will
4378 : handle OMP_NEXT_VARIANT expansion. */
4379 : else
4380 : {
4381 : /* If we need a usable return value, we need a temporary
4382 : and an assignment in each alternative. This logic was borrowed
4383 : from gimplify_cond_expr. */
4384 12 : tree type = TREE_TYPE (expr);
4385 12 : tree tmp = NULL_TREE, result = NULL_TREE;
4386 :
4387 12 : if (want_value)
4388 : {
4389 12 : if (pointerize)
4390 : {
4391 0 : type = build_pointer_type (type);
4392 0 : tmp = create_tmp_var (type, "iftmp");
4393 0 : result = build_simple_mem_ref_loc (loc, tmp);
4394 : }
4395 : else
4396 : {
4397 12 : tmp = create_tmp_var (type, "iftmp");
4398 12 : result = tmp;
4399 : }
4400 : }
4401 :
4402 : /* Preprocess the all_candidates array so that the alternative field of
4403 : each element holds the actual function call expression and possible
4404 : assignment, instead of just the decl for the variant function. */
4405 60 : for (unsigned int i = 0; i < all_candidates.length (); i++)
4406 : {
4407 48 : tree decl = all_candidates[i].alternative;
4408 48 : tree thiscall;
4409 :
4410 : /* We need to turn the decl from the candidate into a function
4411 : call and possible assignment, and stuff that in
4412 : the directive seq of the gomp_variant. */
4413 48 : if (decl == fndecl)
4414 : {
4415 12 : thiscall = expr;
4416 12 : omp_resolved_variant_calls->add (expr);
4417 : }
4418 : else
4419 : {
4420 36 : thiscall = unshare_expr (expr);
4421 36 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype, decl);
4422 36 : if (omp_dispatch_p)
4423 0 : thiscall = modify_call_for_omp_dispatch (thiscall,
4424 : dispatch_clauses,
4425 : want_value,
4426 : pointerize);
4427 : }
4428 48 : if (pointerize)
4429 0 : thiscall = build_fold_addr_expr_loc (loc, thiscall);
4430 48 : if (want_value)
4431 48 : thiscall = build2 (INIT_EXPR, type, tmp, thiscall);
4432 48 : all_candidates[i].alternative = thiscall;
4433 : }
4434 :
4435 12 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
4436 12 : tree expansion = expand_late_variant_directive (all_candidates,
4437 : construct_context);
4438 12 : if (result)
4439 12 : expansion = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4440 : expansion, result);
4441 : return expansion;
4442 : }
4443 : }
4444 :
4445 : /* Wrapper around expand_variant_call_expr to interface with gimplifier
4446 : state. EXPR and OMP_DISPATCH_P are as for expand_variant_call_expr,
4447 : FALLBACK is used to compute the WANT_VALUE and POINTERIZE arguments. */
4448 : static tree
4449 1326 : gimplify_variant_call_expr (tree expr, fallback_t fallback,
4450 : bool omp_dispatch_p)
4451 : {
4452 1326 : tree type = TREE_TYPE (expr);
4453 1326 : bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
4454 673 : bool pointerize = false;
4455 : /* If the result value must be an lvalue or the result type must
4456 : live in memory, then we have to pointerize it if we need a temporary. */
4457 673 : if (want_value
4458 673 : && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
4459 673 : || TREE_ADDRESSABLE (type)))
4460 0 : pointerize = true;
4461 :
4462 1838 : return expand_variant_call_expr (expr, want_value, pointerize,
4463 : omp_dispatch_p,
4464 : (omp_dispatch_p
4465 512 : ? gimplify_omp_ctxp->clauses
4466 1326 : : NULL_TREE));
4467 : }
4468 :
4469 :
4470 : /* Helper function for gimplify_call_expr, called via walk_tree.
4471 : Find used user labels. */
4472 :
4473 : static tree
4474 766 : find_used_user_labels (tree *tp, int *, void *)
4475 : {
4476 766 : if (TREE_CODE (*tp) == LABEL_EXPR
4477 15 : && !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (*tp))
4478 15 : && DECL_NAME (LABEL_EXPR_LABEL (*tp))
4479 781 : && TREE_USED (LABEL_EXPR_LABEL (*tp)))
4480 15 : return *tp;
4481 : return NULL_TREE;
4482 : }
4483 :
4484 :
4485 : /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
4486 : WANT_VALUE is true if the result of the call is desired. */
4487 :
4488 : static enum gimplify_status
4489 17342414 : gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4490 : {
4491 17342414 : bool want_value = (fallback != fb_none);
4492 17342414 : tree fndecl, parms, p, fnptrtype;
4493 17342414 : enum gimplify_status ret;
4494 17342414 : int i, nargs;
4495 17342414 : gcall *call;
4496 17342414 : bool builtin_va_start_p = false, omp_dispatch_p = false;
4497 17342414 : location_t loc = EXPR_LOCATION (*expr_p);
4498 :
4499 17342414 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4500 :
4501 : /* For reliable diagnostics during inlining, it is necessary that
4502 : every call_expr be annotated with file and line. */
4503 17342414 : if (! EXPR_HAS_LOCATION (*expr_p))
4504 519523 : SET_EXPR_LOCATION (*expr_p, input_location);
4505 :
4506 : /* Gimplify internal functions created in the FEs. */
4507 17342414 : if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
4508 : {
4509 604043 : enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
4510 604043 : if (ifn == IFN_GOMP_DISPATCH)
4511 : {
4512 884 : gcc_assert (flag_openmp
4513 : && gimplify_omp_ctxp
4514 : && gimplify_omp_ctxp->code == OMP_DISPATCH);
4515 884 : *expr_p = CALL_EXPR_ARG (*expr_p, 0);
4516 884 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4517 884 : if (! EXPR_HAS_LOCATION (*expr_p))
4518 94 : SET_EXPR_LOCATION (*expr_p, input_location);
4519 : omp_dispatch_p = true;
4520 : }
4521 : else
4522 : {
4523 603159 : if (want_value)
4524 : return GS_ALL_DONE;
4525 :
4526 20851 : nargs = call_expr_nargs (*expr_p);
4527 20851 : auto_vec<tree> vargs (nargs);
4528 :
4529 20851 : if (ifn == IFN_ASSUME)
4530 : {
4531 531 : if (simple_condition_p (CALL_EXPR_ARG (*expr_p, 0)))
4532 : {
4533 : /* If the [[assume (cond)]]; condition is simple
4534 : enough and can be evaluated unconditionally
4535 : without side-effects, expand it as
4536 : if (!cond) __builtin_unreachable (); */
4537 311 : tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
4538 311 : *expr_p
4539 622 : = build3 (COND_EXPR, void_type_node,
4540 311 : CALL_EXPR_ARG (*expr_p, 0), void_node,
4541 311 : build_call_expr_loc (EXPR_LOCATION (*expr_p),
4542 : fndecl, 0));
4543 311 : return GS_OK;
4544 : }
4545 : /* If not optimizing, ignore the assumptions unless there
4546 : are used user labels in it. */
4547 220 : if ((!optimize
4548 98 : && !walk_tree_without_duplicates (&CALL_EXPR_ARG (*expr_p,
4549 : 0),
4550 : find_used_user_labels,
4551 : NULL))
4552 235 : || seen_error ())
4553 : {
4554 94 : *expr_p = NULL_TREE;
4555 94 : return GS_ALL_DONE;
4556 : }
4557 : /* Temporarily, until gimple lowering, transform
4558 : .ASSUME (cond);
4559 : into:
4560 : [[assume (guard)]]
4561 : {
4562 : guard = cond;
4563 : }
4564 : such that gimple lowering can outline the condition into
4565 : a separate function easily. */
4566 126 : tree guard = create_tmp_var (boolean_type_node);
4567 126 : *expr_p = build2 (MODIFY_EXPR, void_type_node, guard,
4568 126 : gimple_boolify (CALL_EXPR_ARG (*expr_p, 0)));
4569 126 : *expr_p = build3 (BIND_EXPR, void_type_node, NULL, *expr_p, NULL);
4570 126 : push_gimplify_context ();
4571 126 : gimple_seq body = NULL;
4572 126 : gimple *g = gimplify_and_return_first (*expr_p, &body);
4573 126 : pop_gimplify_context (g);
4574 126 : g = gimple_build_assume (guard, body);
4575 126 : gimple_set_location (g, loc);
4576 126 : gimplify_seq_add_stmt (pre_p, g);
4577 126 : *expr_p = NULL_TREE;
4578 126 : return GS_ALL_DONE;
4579 : }
4580 20320 : else if (ifn == IFN_UBSAN_BOUNDS
4581 20320 : && nargs == 3
4582 20320 : && integer_onep (CALL_EXPR_ARG (*expr_p, 0)))
4583 : {
4584 : /* If first argument is one, add TYPE_MAX_VALUE (TYPE_DOMAIN (t))
4585 : to 3rd argument and change first argument to 0. This is
4586 : done by ubsan_instrument_bounds so that we can use the
4587 : max value from gimplify_type_sizes here instead of original
4588 : expression for VLAs. */
4589 406 : tree type = TREE_TYPE (CALL_EXPR_ARG (*expr_p, 0));
4590 406 : CALL_EXPR_ARG (*expr_p, 0) = build_int_cst (type, 0);
4591 406 : gcc_assert (TREE_CODE (type) == POINTER_TYPE);
4592 406 : type = TREE_TYPE (type);
4593 406 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4594 406 : tree maxv = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4595 406 : gcc_assert (maxv);
4596 406 : tree arg3 = CALL_EXPR_ARG (*expr_p, 2);
4597 406 : CALL_EXPR_ARG (*expr_p, 2)
4598 812 : = fold_build2 (PLUS_EXPR, TREE_TYPE (arg3), maxv, arg3);
4599 : }
4600 :
4601 46019 : for (i = 0; i < nargs; i++)
4602 : {
4603 25699 : gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
4604 25699 : EXPR_LOCATION (*expr_p));
4605 25699 : vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
4606 : }
4607 :
4608 20320 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
4609 20320 : gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
4610 20320 : gimplify_seq_add_stmt (pre_p, call);
4611 20320 : return GS_ALL_DONE;
4612 20851 : }
4613 : }
4614 :
4615 : /* This may be a call to a builtin function.
4616 :
4617 : Builtin function calls may be transformed into different
4618 : (and more efficient) builtin function calls under certain
4619 : circumstances. Unfortunately, gimplification can muck things
4620 : up enough that the builtin expanders are not aware that certain
4621 : transformations are still valid.
4622 :
4623 : So we attempt transformation/gimplification of the call before
4624 : we gimplify the CALL_EXPR. At this time we do not manage to
4625 : transform all calls in the same manner as the expanders do, but
4626 : we do transform most of them. */
4627 16739255 : fndecl = get_callee_fndecl (*expr_p);
4628 16739255 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4629 3975780 : switch (DECL_FUNCTION_CODE (fndecl))
4630 : {
4631 94885 : CASE_BUILT_IN_ALLOCA:
4632 : /* If the call has been built for a variable-sized object, then we
4633 : want to restore the stack level when the enclosing BIND_EXPR is
4634 : exited to reclaim the allocated space; otherwise, we precisely
4635 : need to do the opposite and preserve the latest stack level. */
4636 94885 : if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
4637 9029 : gimplify_ctxp->save_stack = true;
4638 : else
4639 85856 : gimplify_ctxp->keep_stack = true;
4640 : break;
4641 :
4642 20893 : case BUILT_IN_VA_START:
4643 20893 : {
4644 20893 : builtin_va_start_p = true;
4645 20893 : if (call_expr_nargs (*expr_p) < 2)
4646 : {
4647 1 : error ("too few arguments to function %<va_start%>");
4648 1 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4649 1 : return GS_OK;
4650 : }
4651 :
4652 20892 : if (fold_builtin_next_arg (*expr_p, true))
4653 : {
4654 8 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4655 8 : return GS_OK;
4656 : }
4657 : break;
4658 : }
4659 :
4660 30 : case BUILT_IN_EH_RETURN:
4661 30 : cfun->calls_eh_return = true;
4662 30 : break;
4663 :
4664 1036 : case BUILT_IN_CLEAR_PADDING:
4665 1036 : if (call_expr_nargs (*expr_p) == 1)
4666 : {
4667 : /* Remember the original type of the argument in an internal
4668 : dummy second argument, as in GIMPLE pointer conversions are
4669 : useless. Also mark this call as not for automatic
4670 : initialization in the internal dummy third argument. */
4671 518 : p = CALL_EXPR_ARG (*expr_p, 0);
4672 518 : *expr_p
4673 518 : = build_call_expr_loc (EXPR_LOCATION (*expr_p), fndecl, 2, p,
4674 518 : build_zero_cst (TREE_TYPE (p)));
4675 518 : return GS_OK;
4676 : }
4677 : break;
4678 :
4679 : default:
4680 : ;
4681 : }
4682 16738728 : if (fndecl && fndecl_built_in_p (fndecl))
4683 : {
4684 4216176 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4685 4216176 : if (new_tree && new_tree != *expr_p)
4686 : {
4687 : /* There was a transformation of this call which computes the
4688 : same value, but in a more efficient way. Return and try
4689 : again. */
4690 773 : *expr_p = new_tree;
4691 773 : return GS_OK;
4692 : }
4693 : }
4694 :
4695 : /* Remember the original function pointer type. */
4696 16737955 : fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
4697 :
4698 : /* Handle "declare variant" resolution and arglist processing. */
4699 16737955 : if (flag_openmp
4700 391156 : && fndecl
4701 390475 : && cfun
4702 390475 : && (cfun->curr_properties & PROP_gimple_any) == 0
4703 251293 : && !omp_has_novariants ()
4704 16989136 : && lookup_attribute ("omp declare variant base",
4705 251181 : DECL_ATTRIBUTES (fndecl)))
4706 : {
4707 1326 : tree orig = *expr_p;
4708 1326 : *expr_p = gimplify_variant_call_expr (*expr_p, fallback,
4709 : omp_dispatch_p);
4710 :
4711 : /* This may resolve to the same call, or the call expr with just
4712 : the function replaced, in which case we should just continue to
4713 : gimplify it normally. Otherwise, if we get something else back,
4714 : stop here and re-gimplify the whole replacement expr. */
4715 1326 : if (*expr_p != orig)
4716 : return GS_OK;
4717 : }
4718 :
4719 : /* There is a sequence point before the call, so any side effects in
4720 : the calling expression must occur before the actual call. Force
4721 : gimplify_expr to use an internal post queue. */
4722 16737466 : ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
4723 : is_gimple_call_addr, fb_rvalue);
4724 :
4725 16737466 : if (ret == GS_ERROR)
4726 : return GS_ERROR;
4727 :
4728 16737464 : nargs = call_expr_nargs (*expr_p);
4729 :
4730 : /* Get argument types for verification. */
4731 16737464 : fndecl = get_callee_fndecl (*expr_p);
4732 16737464 : parms = NULL_TREE;
4733 16737464 : if (fndecl)
4734 16513840 : parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4735 : else
4736 223624 : parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
4737 :
4738 33251304 : if (fndecl && DECL_ARGUMENTS (fndecl))
4739 : p = DECL_ARGUMENTS (fndecl);
4740 7381111 : else if (parms)
4741 : p = parms;
4742 : else
4743 : p = NULL_TREE;
4744 44535138 : for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
4745 : ;
4746 :
4747 : /* If the last argument is __builtin_va_arg_pack () and it is not
4748 : passed as a named argument, decrease the number of CALL_EXPR
4749 : arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
4750 16737464 : if (!p
4751 16737464 : && i < nargs
4752 16737464 : && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
4753 : {
4754 5960 : tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
4755 5960 : tree last_arg_fndecl = get_callee_fndecl (last_arg);
4756 :
4757 5960 : if (last_arg_fndecl
4758 5960 : && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
4759 : {
4760 132 : tree call = *expr_p;
4761 :
4762 132 : --nargs;
4763 264 : *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
4764 132 : CALL_EXPR_FN (call),
4765 132 : nargs, CALL_EXPR_ARGP (call));
4766 :
4767 : /* Copy all CALL_EXPR flags, location and block, except
4768 : CALL_EXPR_VA_ARG_PACK flag. */
4769 132 : CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
4770 132 : CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
4771 132 : CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
4772 132 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4773 132 : CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
4774 132 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
4775 :
4776 : /* Set CALL_EXPR_VA_ARG_PACK. */
4777 132 : CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
4778 : }
4779 : }
4780 :
4781 : /* If the call returns twice then after building the CFG the call
4782 : argument computations will no longer dominate the call because
4783 : we add an abnormal incoming edge to the call. So do not use SSA
4784 : vars there. */
4785 16737464 : bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
4786 :
4787 :
4788 : /* Gimplify the function arguments. */
4789 16737464 : if (nargs > 0)
4790 : {
4791 14632477 : for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
4792 43902303 : PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
4793 29269826 : PUSH_ARGS_REVERSED ? i-- : i++)
4794 : {
4795 29269826 : enum gimplify_status t;
4796 :
4797 : /* Avoid gimplifying the second argument to va_start, which needs to
4798 : be the plain PARM_DECL. */
4799 29269826 : if ((i != 1) || !builtin_va_start_p)
4800 : {
4801 29248942 : tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
4802 :
4803 29248942 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4804 6687 : gimplify_omp_ctxp->in_call_args = true;
4805 29248942 : t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
4806 : !returns_twice);
4807 29248942 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4808 6687 : gimplify_omp_ctxp->in_call_args = false;
4809 :
4810 29248942 : if (t == GS_ERROR)
4811 29269826 : ret = GS_ERROR;
4812 : }
4813 : }
4814 : }
4815 :
4816 : /* Gimplify the static chain. */
4817 16737464 : if (CALL_EXPR_STATIC_CHAIN (*expr_p))
4818 : {
4819 28853 : if (fndecl && !DECL_STATIC_CHAIN (fndecl))
4820 3 : CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
4821 : else
4822 : {
4823 28174 : enum gimplify_status t;
4824 28174 : t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
4825 28174 : EXPR_LOCATION (*expr_p), ! returns_twice);
4826 28174 : if (t == GS_ERROR)
4827 16737464 : ret = GS_ERROR;
4828 : }
4829 : }
4830 :
4831 : /* Verify the function result. */
4832 16737464 : if (want_value && fndecl
4833 16737464 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
4834 : {
4835 0 : error_at (loc, "using result of function returning %<void%>");
4836 0 : ret = GS_ERROR;
4837 : }
4838 :
4839 : /* Try this again in case gimplification exposed something. */
4840 16737464 : if (ret != GS_ERROR)
4841 : {
4842 16737314 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4843 :
4844 16737314 : if (new_tree && new_tree != *expr_p)
4845 : {
4846 : /* There was a transformation of this call which computes the
4847 : same value, but in a more efficient way. Return and try
4848 : again. */
4849 11459 : *expr_p = new_tree;
4850 11459 : return GS_OK;
4851 : }
4852 : }
4853 : else
4854 : {
4855 150 : *expr_p = error_mark_node;
4856 150 : return GS_ERROR;
4857 : }
4858 :
4859 : /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
4860 : decl. This allows us to eliminate redundant or useless
4861 : calls to "const" functions. */
4862 16725855 : if (TREE_CODE (*expr_p) == CALL_EXPR)
4863 : {
4864 16725855 : int flags = call_expr_flags (*expr_p);
4865 16725855 : if (flags & (ECF_CONST | ECF_PURE)
4866 : /* An infinite loop is considered a side effect. */
4867 2451087 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
4868 2043859 : TREE_SIDE_EFFECTS (*expr_p) = 0;
4869 : }
4870 :
4871 : /* If the value is not needed by the caller, emit a new GIMPLE_CALL
4872 : and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
4873 : form and delegate the creation of a GIMPLE_CALL to
4874 : gimplify_modify_expr. This is always possible because when
4875 : WANT_VALUE is true, the caller wants the result of this call into
4876 : a temporary, which means that we will emit an INIT_EXPR in
4877 : internal_get_tmp_var which will then be handled by
4878 : gimplify_modify_expr. */
4879 16725855 : if (!want_value)
4880 : {
4881 : /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
4882 : have to do is replicate it as a GIMPLE_CALL tuple. */
4883 6378338 : gimple_stmt_iterator gsi;
4884 6378338 : call = gimple_build_call_from_tree (*expr_p, fnptrtype);
4885 6378338 : notice_special_calls (call);
4886 6378338 : gimplify_seq_add_stmt (pre_p, call);
4887 6378338 : gsi = gsi_last (*pre_p);
4888 6378338 : maybe_fold_stmt (&gsi);
4889 6378338 : *expr_p = NULL_TREE;
4890 : }
4891 : else
4892 : /* Remember the original function type. */
4893 10347517 : CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
4894 10347517 : CALL_EXPR_FN (*expr_p));
4895 :
4896 : return ret;
4897 : }
4898 :
4899 : /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
4900 : rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
4901 :
4902 : TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
4903 : condition is true or false, respectively. If null, we should generate
4904 : our own to skip over the evaluation of this specific expression.
4905 :
4906 : LOCUS is the source location of the COND_EXPR.
4907 :
4908 : The condition_uid is a discriminator tag for condition coverage used to map
4909 : conditions to its corresponding full Boolean function.
4910 :
4911 : This function is the tree equivalent of do_jump.
4912 :
4913 : shortcut_cond_r should only be called by shortcut_cond_expr. */
4914 :
4915 : static tree
4916 1326197 : shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
4917 : location_t locus, unsigned condition_uid)
4918 : {
4919 1326197 : tree local_label = NULL_TREE;
4920 1326197 : tree t, expr = NULL;
4921 :
4922 : /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
4923 : retain the shortcut semantics. Just insert the gotos here;
4924 : shortcut_cond_expr will append the real blocks later. */
4925 1326197 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
4926 : {
4927 124466 : location_t new_locus;
4928 :
4929 : /* Turn if (a && b) into
4930 :
4931 : if (a); else goto no;
4932 : if (b) goto yes; else goto no;
4933 : (no:) */
4934 :
4935 124466 : if (false_label_p == NULL)
4936 3994 : false_label_p = &local_label;
4937 :
4938 : /* Keep the original source location on the first 'if'. */
4939 124466 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
4940 : condition_uid);
4941 124466 : append_to_statement_list (t, &expr);
4942 :
4943 : /* Set the source location of the && on the second 'if'. */
4944 124466 : new_locus = rexpr_location (pred, locus);
4945 124466 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4946 : new_locus, condition_uid);
4947 124466 : append_to_statement_list (t, &expr);
4948 : }
4949 1201731 : else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
4950 : {
4951 383645 : location_t new_locus;
4952 :
4953 : /* Turn if (a || b) into
4954 :
4955 : if (a) goto yes;
4956 : if (b) goto yes; else goto no;
4957 : (yes:) */
4958 :
4959 383645 : if (true_label_p == NULL)
4960 229654 : true_label_p = &local_label;
4961 :
4962 : /* Keep the original source location on the first 'if'. */
4963 383645 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
4964 : condition_uid);
4965 383645 : append_to_statement_list (t, &expr);
4966 :
4967 : /* Set the source location of the || on the second 'if'. */
4968 383645 : new_locus = rexpr_location (pred, locus);
4969 383645 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4970 : new_locus, condition_uid);
4971 383645 : append_to_statement_list (t, &expr);
4972 : }
4973 818086 : else if (TREE_CODE (pred) == COND_EXPR
4974 852 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
4975 818938 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
4976 : {
4977 846 : location_t new_locus;
4978 :
4979 : /* As long as we're messing with gotos, turn if (a ? b : c) into
4980 : if (a)
4981 : if (b) goto yes; else goto no;
4982 : else
4983 : if (c) goto yes; else goto no;
4984 :
4985 : Don't do this if one of the arms has void type, which can happen
4986 : in C++ when the arm is throw. */
4987 :
4988 : /* Keep the original source location on the first 'if'. Set the source
4989 : location of the ? on the second 'if'. */
4990 846 : new_locus = rexpr_location (pred, locus);
4991 1692 : expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
4992 846 : shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
4993 : false_label_p, locus, condition_uid),
4994 846 : shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
4995 : false_label_p, new_locus,
4996 : condition_uid));
4997 846 : tree_associate_condition_with_expr (expr, condition_uid);
4998 : }
4999 : else
5000 : {
5001 817240 : expr = build3 (COND_EXPR, void_type_node, pred,
5002 : build_and_jump (true_label_p),
5003 : build_and_jump (false_label_p));
5004 817240 : SET_EXPR_LOCATION (expr, locus);
5005 817240 : tree_associate_condition_with_expr (expr, condition_uid);
5006 : }
5007 :
5008 1326197 : if (local_label)
5009 : {
5010 233648 : t = build1 (LABEL_EXPR, void_type_node, local_label);
5011 233648 : append_to_statement_list (t, &expr);
5012 : }
5013 :
5014 1326197 : return expr;
5015 : }
5016 :
5017 : /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
5018 : any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
5019 : statement, if it is the last one. Otherwise, return NULL. */
5020 :
5021 : static tree
5022 11675528 : find_goto (tree expr)
5023 : {
5024 12007385 : if (!expr)
5025 : return NULL_TREE;
5026 :
5027 10047508 : if (TREE_CODE (expr) == GOTO_EXPR)
5028 : return expr;
5029 :
5030 8020257 : if (TREE_CODE (expr) != STATEMENT_LIST)
5031 : return NULL_TREE;
5032 :
5033 1048798 : tree_stmt_iterator i = tsi_start (expr);
5034 :
5035 1410562 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
5036 361764 : tsi_next (&i);
5037 :
5038 1048798 : if (!tsi_one_before_end_p (i))
5039 : return NULL_TREE;
5040 :
5041 331857 : return find_goto (tsi_stmt (i));
5042 : }
5043 :
5044 : /* Same as find_goto, except that it returns NULL if the destination
5045 : is not a LABEL_DECL. */
5046 :
5047 : static inline tree
5048 11675528 : find_goto_label (tree expr)
5049 : {
5050 11675528 : tree dest = find_goto (expr);
5051 13702779 : if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
5052 2027140 : return dest;
5053 : return NULL_TREE;
5054 : }
5055 :
5056 :
5057 : /* Given a multi-term condition (ANDIF, ORIF), walk the predicate PRED and tag
5058 : every basic condition with CONDITION_UID. Two basic conditions share the
5059 : CONDITION_UID discriminator when they belong to the same predicate, which is
5060 : used by the condition coverage. Doing this as an explicit step makes for a
5061 : simpler implementation than weaving it into the splitting code as the
5062 : splitting code eventually calls the entry point gimplfiy_expr which makes
5063 : bookkeeping complicated. */
5064 : static void
5065 573581 : tag_shortcut_cond (tree pred, unsigned condition_uid)
5066 : {
5067 612865 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
5068 612865 : || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5069 : {
5070 562568 : tree fst = TREE_OPERAND (pred, 0);
5071 562568 : tree lst = TREE_OPERAND (pred, 1);
5072 :
5073 562568 : if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
5074 562568 : || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
5075 168362 : tag_shortcut_cond (fst, condition_uid);
5076 394206 : else if (TREE_CODE (fst) == COND_EXPR)
5077 244 : tree_associate_condition_with_expr (fst, condition_uid);
5078 :
5079 562568 : if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
5080 562568 : || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
5081 : tag_shortcut_cond (lst, condition_uid);
5082 523284 : else if (TREE_CODE (lst) == COND_EXPR)
5083 445 : tree_associate_condition_with_expr (lst, condition_uid);
5084 : }
5085 573581 : }
5086 :
5087 : /* Given a conditional expression EXPR with short-circuit boolean
5088 : predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
5089 : predicate apart into the equivalent sequence of conditionals. CONDITION_UID
5090 : is a the tag/discriminator for this EXPR - all basic conditions in the
5091 : expression will be given the same CONDITION_UID. */
5092 : static tree
5093 405219 : shortcut_cond_expr (tree expr, unsigned condition_uid)
5094 : {
5095 405219 : tree pred = TREE_OPERAND (expr, 0);
5096 405219 : tree then_ = TREE_OPERAND (expr, 1);
5097 405219 : tree else_ = TREE_OPERAND (expr, 2);
5098 405219 : tree true_label, false_label, end_label, t;
5099 405219 : tree *true_label_p;
5100 405219 : tree *false_label_p;
5101 405219 : bool emit_end, emit_false, jump_over_else;
5102 405219 : bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
5103 405219 : bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
5104 :
5105 405219 : tag_shortcut_cond (pred, condition_uid);
5106 :
5107 : /* First do simple transformations. */
5108 405219 : if (!else_se)
5109 : {
5110 : /* If there is no 'else', turn
5111 : if (a && b) then c
5112 : into
5113 : if (a) if (b) then c. */
5114 292114 : while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
5115 : {
5116 : /* Keep the original source location on the first 'if'. */
5117 51212 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5118 51212 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5119 : /* Set the source location of the && on the second 'if'. */
5120 51212 : if (rexpr_has_location (pred))
5121 50188 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5122 51212 : then_ = shortcut_cond_expr (expr, condition_uid);
5123 51212 : then_se = then_ && TREE_SIDE_EFFECTS (then_);
5124 51212 : pred = TREE_OPERAND (pred, 0);
5125 51212 : expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
5126 51212 : SET_EXPR_LOCATION (expr, locus);
5127 : }
5128 : }
5129 :
5130 405219 : if (!then_se)
5131 : {
5132 : /* If there is no 'then', turn
5133 : if (a || b); else d
5134 : into
5135 : if (a); else if (b); else d. */
5136 25576 : while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5137 : {
5138 : /* Keep the original source location on the first 'if'. */
5139 795 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5140 795 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5141 : /* Set the source location of the || on the second 'if'. */
5142 795 : if (rexpr_has_location (pred))
5143 794 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5144 795 : else_ = shortcut_cond_expr (expr, condition_uid);
5145 795 : else_se = else_ && TREE_SIDE_EFFECTS (else_);
5146 795 : pred = TREE_OPERAND (pred, 0);
5147 795 : expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
5148 795 : SET_EXPR_LOCATION (expr, locus);
5149 : }
5150 : }
5151 :
5152 : /* The expr tree should also have the expression id set. */
5153 405219 : tree_associate_condition_with_expr (expr, condition_uid);
5154 :
5155 : /* If we're done, great. */
5156 405219 : if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
5157 405219 : && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
5158 96936 : return expr;
5159 :
5160 : /* Otherwise we need to mess with gotos. Change
5161 : if (a) c; else d;
5162 : to
5163 : if (a); else goto no;
5164 : c; goto end;
5165 : no: d; end:
5166 : and recursively gimplify the condition. */
5167 :
5168 308283 : true_label = false_label = end_label = NULL_TREE;
5169 :
5170 : /* If our arms just jump somewhere, hijack those labels so we don't
5171 : generate jumps to jumps. */
5172 :
5173 308283 : if (tree then_goto = find_goto_label (then_))
5174 : {
5175 4615 : true_label = GOTO_DESTINATION (then_goto);
5176 4615 : then_ = NULL;
5177 4615 : then_se = false;
5178 : }
5179 :
5180 308283 : if (tree else_goto = find_goto_label (else_))
5181 : {
5182 4208 : false_label = GOTO_DESTINATION (else_goto);
5183 4208 : else_ = NULL;
5184 4208 : else_se = false;
5185 : }
5186 :
5187 : /* If we aren't hijacking a label for the 'then' branch, it falls through. */
5188 308283 : if (true_label)
5189 : true_label_p = &true_label;
5190 : else
5191 303668 : true_label_p = NULL;
5192 :
5193 : /* The 'else' branch also needs a label if it contains interesting code. */
5194 308283 : if (false_label || else_se)
5195 : false_label_p = &false_label;
5196 : else
5197 : false_label_p = NULL;
5198 :
5199 : /* If there was nothing else in our arms, just forward the label(s). */
5200 149141 : if (!then_se && !else_se)
5201 8560 : return shortcut_cond_r (pred, true_label_p, false_label_p,
5202 12840 : EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
5203 :
5204 : /* If our last subexpression already has a terminal label, reuse it. */
5205 304003 : if (else_se)
5206 159142 : t = expr_last (else_);
5207 144861 : else if (then_se)
5208 144861 : t = expr_last (then_);
5209 : else
5210 : t = NULL;
5211 304003 : if (t && TREE_CODE (t) == LABEL_EXPR)
5212 1011 : end_label = LABEL_EXPR_LABEL (t);
5213 :
5214 : /* If we don't care about jumping to the 'else' branch, jump to the end
5215 : if the condition is false. */
5216 304003 : if (!false_label_p)
5217 144619 : false_label_p = &end_label;
5218 :
5219 : /* We only want to emit these labels if we aren't hijacking them. */
5220 304003 : emit_end = (end_label == NULL_TREE);
5221 304003 : emit_false = (false_label == NULL_TREE);
5222 :
5223 : /* We only emit the jump over the else clause if we have to--if the
5224 : then clause may fall through. Otherwise we can wind up with a
5225 : useless jump and a useless label at the end of gimplified code,
5226 : which will cause us to think that this conditional as a whole
5227 : falls through even if it doesn't. If we then inline a function
5228 : which ends with such a condition, that can cause us to issue an
5229 : inappropriate warning about control reaching the end of a
5230 : non-void function. */
5231 304003 : jump_over_else = block_may_fallthru (then_);
5232 :
5233 607375 : pred = shortcut_cond_r (pred, true_label_p, false_label_p,
5234 607375 : EXPR_LOC_OR_LOC (expr, input_location),
5235 : condition_uid);
5236 :
5237 304003 : expr = NULL;
5238 304003 : append_to_statement_list (pred, &expr);
5239 :
5240 304003 : append_to_statement_list (then_, &expr);
5241 304003 : if (else_se)
5242 : {
5243 159142 : if (jump_over_else)
5244 : {
5245 156654 : tree last = expr_last (expr);
5246 156654 : t = build_and_jump (&end_label);
5247 156654 : if (rexpr_has_location (last))
5248 37898 : SET_EXPR_LOCATION (t, rexpr_location (last));
5249 156654 : append_to_statement_list (t, &expr);
5250 : }
5251 159142 : if (emit_false)
5252 : {
5253 159142 : t = build1 (LABEL_EXPR, void_type_node, false_label);
5254 159142 : append_to_statement_list (t, &expr);
5255 : }
5256 159142 : append_to_statement_list (else_, &expr);
5257 : }
5258 304003 : if (emit_end && end_label)
5259 : {
5260 300267 : t = build1 (LABEL_EXPR, void_type_node, end_label);
5261 300267 : append_to_statement_list (t, &expr);
5262 : }
5263 :
5264 304003 : return expr;
5265 : }
5266 :
5267 : /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
5268 :
5269 : tree
5270 17058607 : gimple_boolify (tree expr)
5271 : {
5272 17058607 : tree type = TREE_TYPE (expr);
5273 17058607 : location_t loc = EXPR_LOCATION (expr);
5274 :
5275 17058607 : if (TREE_CODE (expr) == NE_EXPR
5276 5778921 : && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
5277 18013432 : && integer_zerop (TREE_OPERAND (expr, 1)))
5278 : {
5279 497099 : tree call = TREE_OPERAND (expr, 0);
5280 497099 : tree fn = get_callee_fndecl (call);
5281 :
5282 : /* For __builtin_expect ((long) (x), y) recurse into x as well
5283 : if x is truth_value_p. */
5284 497099 : if (fn
5285 496128 : && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
5286 636546 : && call_expr_nargs (call) == 2)
5287 : {
5288 139447 : tree arg = CALL_EXPR_ARG (call, 0);
5289 139447 : if (arg)
5290 : {
5291 139447 : if (TREE_CODE (arg) == NOP_EXPR
5292 139447 : && TREE_TYPE (arg) == TREE_TYPE (call))
5293 65787 : arg = TREE_OPERAND (arg, 0);
5294 139447 : if (truth_value_p (TREE_CODE (arg)))
5295 : {
5296 115946 : arg = gimple_boolify (arg);
5297 115946 : CALL_EXPR_ARG (call, 0)
5298 231892 : = fold_convert_loc (loc, TREE_TYPE (call), arg);
5299 : }
5300 : }
5301 : }
5302 : }
5303 :
5304 17058607 : switch (TREE_CODE (expr))
5305 : {
5306 1405097 : case TRUTH_AND_EXPR:
5307 1405097 : case TRUTH_OR_EXPR:
5308 1405097 : case TRUTH_XOR_EXPR:
5309 1405097 : case TRUTH_ANDIF_EXPR:
5310 1405097 : case TRUTH_ORIF_EXPR:
5311 : /* Also boolify the arguments of truth exprs. */
5312 1405097 : TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
5313 : /* FALLTHRU */
5314 :
5315 1834038 : case TRUTH_NOT_EXPR:
5316 1834038 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5317 :
5318 : /* These expressions always produce boolean results. */
5319 1834038 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5320 308705 : TREE_TYPE (expr) = boolean_type_node;
5321 : return expr;
5322 :
5323 5396 : case ANNOTATE_EXPR:
5324 5396 : switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
5325 : {
5326 5396 : case annot_expr_ivdep_kind:
5327 5396 : case annot_expr_unroll_kind:
5328 5396 : case annot_expr_no_vector_kind:
5329 5396 : case annot_expr_vector_kind:
5330 5396 : case annot_expr_parallel_kind:
5331 5396 : case annot_expr_maybe_infinite_kind:
5332 5396 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5333 5396 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5334 3373 : TREE_TYPE (expr) = boolean_type_node;
5335 : return expr;
5336 0 : default:
5337 0 : gcc_unreachable ();
5338 : }
5339 :
5340 15219173 : default:
5341 15219173 : if (COMPARISON_CLASS_P (expr))
5342 : {
5343 : /* These expressions always produce boolean results. */
5344 13323042 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5345 1604287 : TREE_TYPE (expr) = boolean_type_node;
5346 : return expr;
5347 : }
5348 : /* Other expressions that get here must have boolean values, but
5349 : might need to be converted to the appropriate mode. */
5350 1896131 : if (TREE_CODE (type) == BOOLEAN_TYPE)
5351 : return expr;
5352 204306 : return fold_convert_loc (loc, boolean_type_node, expr);
5353 : }
5354 : }
5355 :
5356 : /* Given a conditional expression *EXPR_P without side effects, gimplify
5357 : its operands. New statements are inserted to PRE_P. */
5358 :
5359 : static enum gimplify_status
5360 45761 : gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
5361 : {
5362 45761 : tree expr = *expr_p, cond;
5363 45761 : enum gimplify_status ret, tret;
5364 45761 : enum tree_code code;
5365 :
5366 45761 : cond = gimple_boolify (COND_EXPR_COND (expr));
5367 :
5368 : /* We need to handle && and || specially, as their gimplification
5369 : creates pure cond_expr, thus leading to an infinite cycle otherwise. */
5370 45761 : code = TREE_CODE (cond);
5371 45761 : if (code == TRUTH_ANDIF_EXPR)
5372 35 : TREE_SET_CODE (cond, TRUTH_AND_EXPR);
5373 45726 : else if (code == TRUTH_ORIF_EXPR)
5374 4596 : TREE_SET_CODE (cond, TRUTH_OR_EXPR);
5375 45761 : ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
5376 45761 : COND_EXPR_COND (*expr_p) = cond;
5377 :
5378 45761 : tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
5379 : is_gimple_val, fb_rvalue);
5380 45761 : ret = MIN (ret, tret);
5381 45761 : tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
5382 : is_gimple_val, fb_rvalue);
5383 :
5384 45761 : return MIN (ret, tret);
5385 : }
5386 :
5387 : /* Return true if evaluating EXPR could trap.
5388 : EXPR is GENERIC, while tree_could_trap_p can be called
5389 : only on GIMPLE. */
5390 :
5391 : bool
5392 16719205 : generic_expr_could_trap_p (tree expr)
5393 : {
5394 16719205 : unsigned i, n;
5395 :
5396 16719205 : if (!expr || is_gimple_val (expr))
5397 : return false;
5398 :
5399 9316475 : if (!EXPR_P (expr) || tree_could_trap_p (expr))
5400 : return true;
5401 :
5402 6845894 : n = TREE_OPERAND_LENGTH (expr);
5403 16369794 : for (i = 0; i < n; i++)
5404 11858503 : if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
5405 : return true;
5406 :
5407 : return false;
5408 : }
5409 :
5410 : /* Associate the condition STMT with the discriminator UID. STMTs that are
5411 : broken down with ANDIF/ORIF from the same Boolean expression should be given
5412 : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
5413 : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
5414 : for condition coverage. */
5415 : static void
5416 5529481 : gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
5417 : unsigned uid)
5418 : {
5419 5529481 : if (!condition_coverage_flag)
5420 : return;
5421 :
5422 653 : if (!fn->cond_uids)
5423 142 : fn->cond_uids = new hash_map <gcond*, unsigned> ();
5424 :
5425 653 : fn->cond_uids->put (stmt, uid);
5426 : }
5427 :
5428 : /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
5429 : into
5430 :
5431 : if (p) if (p)
5432 : t1 = a; a;
5433 : else or else
5434 : t1 = b; b;
5435 : t1;
5436 :
5437 : The second form is used when *EXPR_P is of type void.
5438 :
5439 : PRE_P points to the list where side effects that must happen before
5440 : *EXPR_P should be stored. */
5441 :
5442 : static enum gimplify_status
5443 6302467 : gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
5444 : {
5445 6302467 : tree expr = *expr_p;
5446 6302467 : tree type = TREE_TYPE (expr);
5447 6302467 : location_t loc = EXPR_LOCATION (expr);
5448 6302467 : tree tmp, arm1, arm2;
5449 6302467 : enum gimplify_status ret;
5450 6302467 : tree label_true, label_false, label_cont;
5451 6302467 : bool have_then_clause_p, have_else_clause_p;
5452 6302467 : gcond *cond_stmt;
5453 6302467 : enum tree_code pred_code;
5454 6302467 : gimple_seq seq = NULL;
5455 :
5456 : /* If this COND_EXPR has a value, copy the values into a temporary within
5457 : the arms. */
5458 6302467 : if (!VOID_TYPE_P (type))
5459 : {
5460 418998 : tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
5461 418998 : tree result;
5462 :
5463 : /* If either an rvalue is ok or we do not require an lvalue, create the
5464 : temporary. But we cannot do that if the type is addressable. */
5465 418998 : if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
5466 418991 : && !TREE_ADDRESSABLE (type))
5467 : {
5468 418991 : if (gimplify_ctxp->allow_rhs_cond_expr
5469 : /* If either branch has side effects or could trap, it can't be
5470 : evaluated unconditionally. */
5471 45761 : && !TREE_SIDE_EFFECTS (then_)
5472 45761 : && !generic_expr_could_trap_p (then_)
5473 45761 : && !TREE_SIDE_EFFECTS (else_)
5474 464752 : && !generic_expr_could_trap_p (else_))
5475 45761 : return gimplify_pure_cond_expr (expr_p, pre_p);
5476 :
5477 373230 : tmp = create_tmp_var (type, "iftmp");
5478 373230 : result = tmp;
5479 : }
5480 :
5481 : /* Otherwise, only create and copy references to the values. */
5482 : else
5483 : {
5484 7 : type = build_pointer_type (type);
5485 :
5486 7 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5487 7 : then_ = build_fold_addr_expr_loc (loc, then_);
5488 :
5489 7 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5490 7 : else_ = build_fold_addr_expr_loc (loc, else_);
5491 :
5492 7 : expr
5493 7 : = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
5494 :
5495 7 : tmp = create_tmp_var (type, "iftmp");
5496 7 : result = build_simple_mem_ref_loc (loc, tmp);
5497 : }
5498 :
5499 : /* Build the new then clause, `tmp = then_;'. But don't build the
5500 : assignment if the value is void; in C++ it can be if it's a throw. */
5501 373237 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5502 291677 : TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
5503 :
5504 : /* Similarly, build the new else clause, `tmp = else_;'. */
5505 373237 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5506 362581 : TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
5507 :
5508 373237 : TREE_TYPE (expr) = void_type_node;
5509 373237 : recalculate_side_effects (expr);
5510 :
5511 : /* Move the COND_EXPR to the prequeue. */
5512 373237 : gimplify_stmt (&expr, pre_p);
5513 :
5514 373237 : *expr_p = result;
5515 373237 : return GS_ALL_DONE;
5516 : }
5517 :
5518 : /* Remove any COMPOUND_EXPR so the following cases will be caught. */
5519 5898388 : STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
5520 5883469 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
5521 81030 : gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
5522 :
5523 : /* Make sure the condition has BOOLEAN_TYPE. */
5524 5883469 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5525 :
5526 : /* Break apart && and || conditions. */
5527 5883469 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
5528 5883469 : || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
5529 : {
5530 353212 : expr = shortcut_cond_expr (expr, next_cond_uid ());
5531 :
5532 353212 : if (expr != *expr_p)
5533 : {
5534 353212 : *expr_p = expr;
5535 :
5536 : /* We can't rely on gimplify_expr to re-gimplify the expanded
5537 : form properly, as cleanups might cause the target labels to be
5538 : wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
5539 : set up a conditional context. */
5540 353212 : gimple_push_condition ();
5541 353212 : gimplify_stmt (expr_p, &seq);
5542 353212 : gimple_pop_condition (pre_p);
5543 353212 : gimple_seq_add_seq (pre_p, seq);
5544 :
5545 353212 : return GS_ALL_DONE;
5546 : }
5547 : }
5548 :
5549 : /* Now do the normal gimplification. */
5550 :
5551 : /* Gimplify condition. */
5552 5530257 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
5553 : is_gimple_condexpr_for_cond, fb_rvalue);
5554 5530257 : if (ret == GS_ERROR)
5555 : return GS_ERROR;
5556 5529481 : gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
5557 :
5558 5529481 : gimple_push_condition ();
5559 :
5560 5529481 : have_then_clause_p = have_else_clause_p = false;
5561 5529481 : label_true = find_goto_label (TREE_OPERAND (expr, 1));
5562 5529481 : if (label_true
5563 1288610 : && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
5564 : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5565 : have different locations, otherwise we end up with incorrect
5566 : location information on the branches. */
5567 6818091 : && (optimize
5568 232524 : || !EXPR_HAS_LOCATION (expr)
5569 205623 : || !rexpr_has_location (label_true)
5570 69405 : || EXPR_LOCATION (expr) == rexpr_location (label_true)))
5571 : {
5572 1287436 : have_then_clause_p = true;
5573 1287436 : label_true = GOTO_DESTINATION (label_true);
5574 : }
5575 : else
5576 4242045 : label_true = create_artificial_label (UNKNOWN_LOCATION);
5577 5529481 : label_false = find_goto_label (TREE_OPERAND (expr, 2));
5578 5529481 : if (label_false
5579 729707 : && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
5580 : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5581 : have different locations, otherwise we end up with incorrect
5582 : location information on the branches. */
5583 6259188 : && (optimize
5584 104836 : || !EXPR_HAS_LOCATION (expr)
5585 103954 : || !rexpr_has_location (label_false)
5586 115 : || EXPR_LOCATION (expr) == rexpr_location (label_false)))
5587 : {
5588 729604 : have_else_clause_p = true;
5589 729604 : label_false = GOTO_DESTINATION (label_false);
5590 : }
5591 : else
5592 4799877 : label_false = create_artificial_label (UNKNOWN_LOCATION);
5593 :
5594 5529481 : unsigned cond_uid = 0;
5595 5529481 : if (cond_uids)
5596 467 : if (unsigned *v = cond_uids->get (expr))
5597 447 : cond_uid = *v;
5598 447 : if (cond_uid == 0)
5599 5529034 : cond_uid = next_cond_uid ();
5600 :
5601 5529481 : gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
5602 : &arm2);
5603 5529481 : cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
5604 : label_false);
5605 5529481 : gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
5606 5529481 : gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
5607 5529481 : copy_warning (cond_stmt, COND_EXPR_COND (expr));
5608 5529481 : gimplify_seq_add_stmt (&seq, cond_stmt);
5609 5529481 : gimple_stmt_iterator gsi = gsi_last (seq);
5610 5529481 : maybe_fold_stmt (&gsi);
5611 :
5612 5529481 : label_cont = NULL_TREE;
5613 5529481 : if (!have_then_clause_p)
5614 : {
5615 : /* For if (...) {} else { code; } put label_true after
5616 : the else block. */
5617 4242045 : if (TREE_OPERAND (expr, 1) == NULL_TREE
5618 200602 : && !have_else_clause_p
5619 4242813 : && TREE_OPERAND (expr, 2) != NULL_TREE)
5620 : {
5621 : /* For if (0) {} else { code; } tell -Wimplicit-fallthrough
5622 : handling that label_cont == label_true can be only reached
5623 : through fallthrough from { code; }. */
5624 768 : if (integer_zerop (COND_EXPR_COND (expr)))
5625 0 : UNUSED_LABEL_P (label_true) = 1;
5626 : label_cont = label_true;
5627 : }
5628 : else
5629 : {
5630 4241277 : bool then_side_effects
5631 4241277 : = (TREE_OPERAND (expr, 1)
5632 4241277 : && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
5633 4241277 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
5634 4241277 : have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
5635 : /* For if (...) { code; } else {} or
5636 : if (...) { code; } else goto label; or
5637 : if (...) { code; return; } else { ... }
5638 : label_cont isn't needed. */
5639 4241277 : if (!have_else_clause_p
5640 4040297 : && TREE_OPERAND (expr, 2) != NULL_TREE
5641 7055206 : && gimple_seq_may_fallthru (seq))
5642 : {
5643 1958077 : gimple *g;
5644 1958077 : label_cont = create_artificial_label (UNKNOWN_LOCATION);
5645 :
5646 : /* For if (0) { non-side-effect-code } else { code }
5647 : tell -Wimplicit-fallthrough handling that label_cont can
5648 : be only reached through fallthrough from { code }. */
5649 1958077 : if (integer_zerop (COND_EXPR_COND (expr)))
5650 : {
5651 76085 : UNUSED_LABEL_P (label_true) = 1;
5652 76085 : if (!then_side_effects)
5653 12372 : UNUSED_LABEL_P (label_cont) = 1;
5654 : }
5655 :
5656 1958077 : g = gimple_build_goto (label_cont);
5657 :
5658 : /* GIMPLE_COND's are very low level; they have embedded
5659 : gotos. This particular embedded goto should not be marked
5660 : with the location of the original COND_EXPR, as it would
5661 : correspond to the COND_EXPR's condition, not the ELSE or the
5662 : THEN arms. To avoid marking it with the wrong location, flag
5663 : it as "no location". */
5664 1958077 : gimple_set_do_not_emit_location (g);
5665 :
5666 1958077 : gimplify_seq_add_stmt (&seq, g);
5667 : }
5668 : }
5669 : }
5670 5529481 : if (!have_else_clause_p)
5671 : {
5672 : /* For if (1) { code } or if (1) { code } else { non-side-effect-code }
5673 : tell -Wimplicit-fallthrough handling that label_false can be only
5674 : reached through fallthrough from { code }. */
5675 4799877 : if (integer_nonzerop (COND_EXPR_COND (expr))
5676 4799877 : && (TREE_OPERAND (expr, 2) == NULL_TREE
5677 56842 : || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
5678 7884 : UNUSED_LABEL_P (label_false) = 1;
5679 4799877 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
5680 4799877 : have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
5681 : }
5682 5529481 : if (label_cont)
5683 1958845 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
5684 :
5685 5529481 : gimple_pop_condition (pre_p);
5686 5529481 : gimple_seq_add_seq (pre_p, seq);
5687 :
5688 5529481 : if (ret == GS_ERROR)
5689 : ; /* Do nothing. */
5690 5529481 : else if (have_then_clause_p || have_else_clause_p)
5691 : ret = GS_ALL_DONE;
5692 : else
5693 : {
5694 : /* Both arms are empty; replace the COND_EXPR with its predicate. */
5695 2955 : expr = TREE_OPERAND (expr, 0);
5696 2955 : gimplify_stmt (&expr, pre_p);
5697 : }
5698 :
5699 5529481 : *expr_p = NULL;
5700 5529481 : return ret;
5701 : }
5702 :
5703 : /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
5704 : to be marked addressable.
5705 :
5706 : We cannot rely on such an expression being directly markable if a temporary
5707 : has been created by the gimplification. In this case, we create another
5708 : temporary and initialize it with a copy, which will become a store after we
5709 : mark it addressable. This can happen if the front-end passed us something
5710 : that it could not mark addressable yet, like a Fortran pass-by-reference
5711 : parameter (int) floatvar. */
5712 :
5713 : static void
5714 56753366 : prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
5715 : {
5716 62301395 : while (handled_component_p (*expr_p))
5717 5548029 : expr_p = &TREE_OPERAND (*expr_p, 0);
5718 :
5719 : /* Do not allow an SSA name as the temporary. */
5720 56753366 : if (is_gimple_reg (*expr_p))
5721 12133 : *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
5722 56753366 : }
5723 :
5724 : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5725 : a call to __builtin_memcpy. */
5726 :
5727 : static enum gimplify_status
5728 1106 : gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
5729 : gimple_seq *seq_p)
5730 : {
5731 1106 : tree t, to, to_ptr, from, from_ptr;
5732 1106 : gcall *gs;
5733 1106 : location_t loc = EXPR_LOCATION (*expr_p);
5734 :
5735 1106 : to = TREE_OPERAND (*expr_p, 0);
5736 1106 : from = TREE_OPERAND (*expr_p, 1);
5737 1106 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to)))
5738 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))));
5739 :
5740 : /* Mark the RHS addressable. Beware that it may not be possible to do so
5741 : directly if a temporary has been created by the gimplification. */
5742 1106 : prepare_gimple_addressable (&from, seq_p);
5743 :
5744 1106 : mark_addressable (from);
5745 1106 : from_ptr = build_fold_addr_expr_loc (loc, from);
5746 1106 : gimplify_arg (&from_ptr, seq_p, loc);
5747 :
5748 1106 : mark_addressable (to);
5749 1106 : to_ptr = build_fold_addr_expr_loc (loc, to);
5750 1106 : gimplify_arg (&to_ptr, seq_p, loc);
5751 :
5752 1106 : t = builtin_decl_implicit (BUILT_IN_MEMCPY);
5753 :
5754 1106 : gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
5755 1106 : gimple_call_set_alloca_for_var (gs, true);
5756 :
5757 1106 : if (want_value)
5758 : {
5759 : /* tmp = memcpy() */
5760 8 : t = create_tmp_var (TREE_TYPE (to_ptr));
5761 8 : gimple_call_set_lhs (gs, t);
5762 8 : gimplify_seq_add_stmt (seq_p, gs);
5763 :
5764 8 : *expr_p = build_simple_mem_ref (t);
5765 8 : return GS_ALL_DONE;
5766 : }
5767 :
5768 1098 : gimplify_seq_add_stmt (seq_p, gs);
5769 1098 : *expr_p = NULL;
5770 1098 : return GS_ALL_DONE;
5771 : }
5772 :
5773 : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5774 : a call to __builtin_memset. In this case we know that the RHS is
5775 : a CONSTRUCTOR with an empty element list. */
5776 :
5777 : static enum gimplify_status
5778 84 : gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
5779 : gimple_seq *seq_p)
5780 : {
5781 84 : tree t, from, to, to_ptr;
5782 84 : gcall *gs;
5783 84 : location_t loc = EXPR_LOCATION (*expr_p);
5784 :
5785 : /* Assert our assumptions, to abort instead of producing wrong code
5786 : silently if they are not met. Beware that the RHS CONSTRUCTOR might
5787 : not be immediately exposed. */
5788 84 : from = TREE_OPERAND (*expr_p, 1);
5789 84 : if (TREE_CODE (from) == WITH_SIZE_EXPR)
5790 84 : from = TREE_OPERAND (from, 0);
5791 :
5792 84 : gcc_assert (TREE_CODE (from) == CONSTRUCTOR
5793 : && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
5794 :
5795 : /* Now proceed. */
5796 84 : to = TREE_OPERAND (*expr_p, 0);
5797 84 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to))));
5798 :
5799 84 : to_ptr = build_fold_addr_expr_loc (loc, to);
5800 84 : gimplify_arg (&to_ptr, seq_p, loc);
5801 84 : t = builtin_decl_implicit (BUILT_IN_MEMSET);
5802 :
5803 84 : gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
5804 :
5805 84 : if (want_value)
5806 : {
5807 : /* tmp = memset() */
5808 0 : t = create_tmp_var (TREE_TYPE (to_ptr));
5809 0 : gimple_call_set_lhs (gs, t);
5810 0 : gimplify_seq_add_stmt (seq_p, gs);
5811 :
5812 0 : *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
5813 0 : return GS_ALL_DONE;
5814 : }
5815 :
5816 84 : gimplify_seq_add_stmt (seq_p, gs);
5817 84 : *expr_p = NULL;
5818 84 : return GS_ALL_DONE;
5819 : }
5820 :
5821 : /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
5822 : determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
5823 : assignment. Return non-null if we detect a potential overlap. */
5824 :
5825 : struct gimplify_init_ctor_preeval_data
5826 : {
5827 : /* The base decl of the lhs object. May be NULL, in which case we
5828 : have to assume the lhs is indirect. */
5829 : tree lhs_base_decl;
5830 :
5831 : /* The alias set of the lhs object. */
5832 : alias_set_type lhs_alias_set;
5833 : };
5834 :
5835 : static tree
5836 116396 : gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
5837 : {
5838 116396 : struct gimplify_init_ctor_preeval_data *data
5839 : = (struct gimplify_init_ctor_preeval_data *) xdata;
5840 116396 : tree t = *tp;
5841 :
5842 : /* If we find the base object, obviously we have overlap. */
5843 116396 : if (data->lhs_base_decl == t)
5844 : return t;
5845 :
5846 : /* If the constructor component is indirect, determine if we have a
5847 : potential overlap with the lhs. The only bits of information we
5848 : have to go on at this point are addressability and alias sets. */
5849 116395 : if ((INDIRECT_REF_P (t)
5850 116395 : || TREE_CODE (t) == MEM_REF)
5851 651 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5852 116911 : && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
5853 : return t;
5854 :
5855 : /* If the constructor component is a call, determine if it can hide a
5856 : potential overlap with the lhs through an INDIRECT_REF like above.
5857 : ??? Ugh - this is completely broken. In fact this whole analysis
5858 : doesn't look conservative. */
5859 115879 : if (TREE_CODE (t) == CALL_EXPR)
5860 : {
5861 0 : tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
5862 :
5863 0 : for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
5864 0 : if (POINTER_TYPE_P (TREE_VALUE (type))
5865 0 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5866 0 : && alias_sets_conflict_p (data->lhs_alias_set,
5867 : get_alias_set
5868 0 : (TREE_TYPE (TREE_VALUE (type)))))
5869 : return t;
5870 : }
5871 :
5872 115879 : if (IS_TYPE_OR_DECL_P (t))
5873 10968 : *walk_subtrees = 0;
5874 : return NULL;
5875 : }
5876 :
5877 : /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
5878 : force values that overlap with the lhs (as described by *DATA)
5879 : into temporaries. */
5880 :
5881 : static void
5882 417506 : gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5883 : struct gimplify_init_ctor_preeval_data *data)
5884 : {
5885 417506 : enum gimplify_status one;
5886 :
5887 : /* If the value is constant, then there's nothing to pre-evaluate. */
5888 417506 : if (TREE_CONSTANT (*expr_p))
5889 : {
5890 : /* Ensure it does not have side effects, it might contain a reference to
5891 : the object we're initializing. */
5892 199322 : gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
5893 : return;
5894 : }
5895 :
5896 : /* If the type has non-trivial constructors, we can't pre-evaluate. */
5897 218184 : if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
5898 : return;
5899 :
5900 : /* Recurse for nested constructors. */
5901 218184 : if (TREE_CODE (*expr_p) == CONSTRUCTOR)
5902 : {
5903 73916 : unsigned HOST_WIDE_INT ix;
5904 73916 : constructor_elt *ce;
5905 73916 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
5906 :
5907 271983 : FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
5908 198067 : gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
5909 :
5910 : return;
5911 : }
5912 :
5913 : /* If this is a variable sized type, we must remember the size. */
5914 144268 : maybe_with_size_expr (expr_p);
5915 :
5916 : /* Gimplify the constructor element to something appropriate for the rhs
5917 : of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
5918 : the gimplifier will consider this a store to memory. Doing this
5919 : gimplification now means that we won't have to deal with complicated
5920 : language-specific trees, nor trees like SAVE_EXPR that can induce
5921 : exponential search behavior. */
5922 144268 : one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
5923 144268 : if (one == GS_ERROR)
5924 : {
5925 0 : *expr_p = NULL;
5926 0 : return;
5927 : }
5928 :
5929 : /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
5930 : with the lhs, since "a = { .x=a }" doesn't make sense. This will
5931 : always be true for all scalars, since is_gimple_mem_rhs insists on a
5932 : temporary variable for them. */
5933 144268 : if (DECL_P (*expr_p))
5934 : return;
5935 :
5936 : /* If this is of variable size, we have no choice but to assume it doesn't
5937 : overlap since we can't make a temporary for it. */
5938 104832 : if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
5939 : return;
5940 :
5941 : /* Otherwise, we must search for overlap ... */
5942 104832 : if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
5943 : return;
5944 :
5945 : /* ... and if found, force the value into a temporary. */
5946 517 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
5947 : }
5948 :
5949 : /* A subroutine of gimplify_init_ctor_eval. Create a loop for
5950 : a RANGE_EXPR in a CONSTRUCTOR for an array.
5951 :
5952 : var = lower;
5953 : loop_entry:
5954 : object[var] = value;
5955 : if (var == upper)
5956 : goto loop_exit;
5957 : var = var + 1;
5958 : goto loop_entry;
5959 : loop_exit:
5960 :
5961 : We increment var _after_ the loop exit check because we might otherwise
5962 : fail if upper == TYPE_MAX_VALUE (type for upper).
5963 :
5964 : Note that we never have to deal with SAVE_EXPRs here, because this has
5965 : already been taken care of for us, in gimplify_init_ctor_preeval(). */
5966 :
5967 : static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
5968 : gimple_seq *, bool);
5969 :
5970 : static void
5971 579 : gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
5972 : tree value, tree array_elt_type,
5973 : gimple_seq *pre_p, bool cleared)
5974 : {
5975 579 : tree loop_entry_label, loop_exit_label, fall_thru_label;
5976 579 : tree var, var_type, cref, tmp;
5977 :
5978 579 : loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
5979 579 : loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
5980 579 : fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
5981 :
5982 : /* Create and initialize the index variable. */
5983 579 : var_type = TREE_TYPE (upper);
5984 579 : var = create_tmp_var (var_type);
5985 579 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
5986 :
5987 : /* Add the loop entry label. */
5988 579 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
5989 :
5990 : /* Build the reference. */
5991 579 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
5992 : var, NULL_TREE, NULL_TREE);
5993 :
5994 : /* If we are a constructor, just call gimplify_init_ctor_eval to do
5995 : the store. Otherwise just assign value to the reference. */
5996 :
5997 579 : if (TREE_CODE (value) == CONSTRUCTOR)
5998 : /* NB we might have to call ourself recursively through
5999 : gimplify_init_ctor_eval if the value is a constructor. */
6000 129 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6001 : pre_p, cleared);
6002 : else
6003 : {
6004 450 : if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
6005 : != GS_ERROR)
6006 450 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
6007 : }
6008 :
6009 : /* We exit the loop when the index var is equal to the upper bound. */
6010 1158 : gimplify_seq_add_stmt (pre_p,
6011 579 : gimple_build_cond (EQ_EXPR, var, upper,
6012 : loop_exit_label, fall_thru_label));
6013 :
6014 579 : gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
6015 :
6016 : /* Otherwise, increment the index var... */
6017 579 : tmp = build2 (PLUS_EXPR, var_type, var,
6018 : fold_convert (var_type, integer_one_node));
6019 579 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
6020 :
6021 : /* ...and jump back to the loop entry. */
6022 579 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
6023 :
6024 : /* Add the loop exit label. */
6025 579 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
6026 579 : }
6027 :
6028 : /* A subroutine of gimplify_init_constructor. Generate individual
6029 : MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
6030 : assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
6031 : CONSTRUCTOR. CLEARED is true if the entire LHS object has been
6032 : zeroed first. */
6033 :
6034 : static void
6035 1271615 : gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
6036 : gimple_seq *pre_p, bool cleared)
6037 : {
6038 1271615 : tree array_elt_type = NULL;
6039 1271615 : unsigned HOST_WIDE_INT ix;
6040 1271615 : tree purpose, value;
6041 :
6042 1271615 : if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
6043 142348 : array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
6044 :
6045 4693927 : FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
6046 : {
6047 3422312 : tree cref;
6048 :
6049 : /* NULL values are created above for gimplification errors. */
6050 3422312 : if (value == NULL)
6051 325634 : continue;
6052 :
6053 3422312 : if (cleared && initializer_zerop (value))
6054 324889 : continue;
6055 :
6056 : /* ??? Here's to hoping the front end fills in all of the indices,
6057 : so we don't have to figure out what's missing ourselves. */
6058 3097423 : gcc_assert (purpose);
6059 :
6060 : /* Skip zero-sized fields, unless value has side-effects. This can
6061 : happen with calls to functions returning a empty type, which
6062 : we shouldn't discard. As a number of downstream passes don't
6063 : expect sets of empty type fields, we rely on the gimplification of
6064 : the MODIFY_EXPR we make below to drop the assignment statement. */
6065 3097423 : if (!TREE_SIDE_EFFECTS (value)
6066 2803009 : && TREE_CODE (purpose) == FIELD_DECL
6067 5428254 : && is_empty_type (TREE_TYPE (purpose)))
6068 166 : continue;
6069 :
6070 : /* If we have a RANGE_EXPR, we have to build a loop to assign the
6071 : whole range. */
6072 3097257 : if (TREE_CODE (purpose) == RANGE_EXPR)
6073 : {
6074 595 : tree lower = TREE_OPERAND (purpose, 0);
6075 595 : tree upper = TREE_OPERAND (purpose, 1);
6076 :
6077 : /* If the lower bound is equal to upper, just treat it as if
6078 : upper was the index. */
6079 595 : if (simple_cst_equal (lower, upper))
6080 : purpose = upper;
6081 : else
6082 : {
6083 579 : gimplify_init_ctor_eval_range (object, lower, upper, value,
6084 : array_elt_type, pre_p, cleared);
6085 579 : continue;
6086 : }
6087 : }
6088 :
6089 3096678 : if (array_elt_type)
6090 : {
6091 : /* Do not use bitsizetype for ARRAY_REF indices. */
6092 562396 : if (TYPE_DOMAIN (TREE_TYPE (object)))
6093 562396 : purpose
6094 562396 : = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
6095 : purpose);
6096 562396 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6097 : purpose, NULL_TREE, NULL_TREE);
6098 : }
6099 : else
6100 : {
6101 2534282 : gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
6102 2534282 : cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
6103 : unshare_expr (object), purpose, NULL_TREE);
6104 : }
6105 :
6106 3096678 : if (TREE_CODE (value) == CONSTRUCTOR
6107 3096678 : && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
6108 301409 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6109 : pre_p, cleared);
6110 2795269 : else if (TREE_CODE (value) == RAW_DATA_CST)
6111 : {
6112 43 : if (RAW_DATA_LENGTH (value) <= 32)
6113 : {
6114 13 : for (unsigned int i = 0; i < (unsigned) RAW_DATA_LENGTH (value);
6115 : ++i)
6116 12 : if (!cleared || RAW_DATA_POINTER (value)[i])
6117 : {
6118 12 : if (i)
6119 : {
6120 11 : tree p
6121 11 : = fold_build2 (PLUS_EXPR, TREE_TYPE (purpose),
6122 : purpose,
6123 : build_int_cst (TREE_TYPE (purpose),
6124 : i));
6125 11 : cref = build4 (ARRAY_REF, array_elt_type,
6126 : unshare_expr (object), p, NULL_TREE,
6127 : NULL_TREE);
6128 : }
6129 12 : tree init
6130 12 : = build2 (INIT_EXPR, TREE_TYPE (cref), cref,
6131 12 : build_int_cst (TREE_TYPE (value),
6132 12 : RAW_DATA_UCHAR_ELT (value, i)));
6133 12 : gimplify_and_add (init, pre_p);
6134 12 : ggc_free (init);
6135 : }
6136 : }
6137 : else
6138 : {
6139 42 : tree rtype = build_array_type_nelts (TREE_TYPE (value),
6140 42 : RAW_DATA_LENGTH (value));
6141 42 : tree rctor = build_constructor_single (rtype, bitsize_zero_node,
6142 : value);
6143 42 : tree addr = build_fold_addr_expr (cref);
6144 42 : cref = build2 (MEM_REF, rtype, addr,
6145 : build_int_cst (ptr_type_node, 0));
6146 42 : rctor = tree_output_constant_def (rctor);
6147 42 : if (!useless_type_conversion_p (rtype, TREE_TYPE (rctor)))
6148 4 : rctor = build1 (VIEW_CONVERT_EXPR, rtype, rctor);
6149 42 : if (gimplify_expr (&cref, pre_p, NULL, is_gimple_lvalue,
6150 : fb_lvalue) != GS_ERROR)
6151 84 : gimplify_seq_add_stmt (pre_p,
6152 42 : gimple_build_assign (cref, rctor));
6153 : }
6154 : }
6155 : else
6156 : {
6157 2795226 : tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
6158 2795226 : gimplify_and_add (init, pre_p);
6159 2795226 : ggc_free (init);
6160 : }
6161 : }
6162 1271615 : }
6163 :
6164 : /* Return the appropriate RHS predicate for this LHS. */
6165 :
6166 : gimple_predicate
6167 49659164 : rhs_predicate_for (tree lhs)
6168 : {
6169 49659164 : if (is_gimple_reg (lhs))
6170 : return is_gimple_reg_rhs_or_call;
6171 : else
6172 11702614 : return is_gimple_mem_rhs_or_call;
6173 : }
6174 :
6175 : /* Return the initial guess for an appropriate RHS predicate for this LHS,
6176 : before the LHS has been gimplified. */
6177 :
6178 : static gimple_predicate
6179 48454185 : initial_rhs_predicate_for (tree lhs)
6180 : {
6181 48454185 : if (is_gimple_reg_type (TREE_TYPE (lhs)))
6182 : return is_gimple_reg_rhs_or_call;
6183 : else
6184 2447008 : return is_gimple_mem_rhs_or_call;
6185 : }
6186 :
6187 : /* Gimplify a C99 compound literal expression. This just means adding
6188 : the DECL_EXPR before the current statement and using its anonymous
6189 : decl instead. */
6190 :
6191 : static enum gimplify_status
6192 37225 : gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
6193 : bool (*gimple_test_f) (tree),
6194 : fallback_t fallback)
6195 : {
6196 37225 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
6197 37225 : tree decl = DECL_EXPR_DECL (decl_s);
6198 37225 : tree init = DECL_INITIAL (decl);
6199 : /* Mark the decl as addressable if the compound literal
6200 : expression is addressable now, otherwise it is marked too late
6201 : after we gimplify the initialization expression. */
6202 37225 : if (TREE_ADDRESSABLE (*expr_p))
6203 549 : TREE_ADDRESSABLE (decl) = 1;
6204 : /* Otherwise, if we don't need an lvalue and have a literal directly
6205 : substitute it. Check if it matches the gimple predicate, as
6206 : otherwise we'd generate a new temporary, and we can as well just
6207 : use the decl we already have. */
6208 36676 : else if (!TREE_ADDRESSABLE (decl)
6209 36676 : && !TREE_THIS_VOLATILE (decl)
6210 36671 : && init
6211 36663 : && (fallback & fb_lvalue) == 0
6212 73183 : && gimple_test_f (init))
6213 : {
6214 35416 : *expr_p = init;
6215 35416 : return GS_OK;
6216 : }
6217 :
6218 : /* If the decl is not addressable, then it is being used in some
6219 : expression or on the right hand side of a statement, and it can
6220 : be put into a readonly data section. */
6221 1809 : if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
6222 1096 : TREE_READONLY (decl) = 1;
6223 :
6224 : /* This decl isn't mentioned in the enclosing block, so add it to the
6225 : list of temps. FIXME it seems a bit of a kludge to say that
6226 : anonymous artificial vars aren't pushed, but everything else is. */
6227 1809 : if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
6228 2 : gimple_add_tmp_var (decl);
6229 :
6230 1809 : gimplify_and_add (decl_s, pre_p);
6231 1809 : *expr_p = decl;
6232 1809 : return GS_OK;
6233 : }
6234 :
6235 : /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
6236 : return a new CONSTRUCTOR if something changed. */
6237 :
6238 : static tree
6239 1898197 : optimize_compound_literals_in_ctor (tree orig_ctor)
6240 : {
6241 1898197 : tree ctor = orig_ctor;
6242 1898197 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6243 1898197 : unsigned int idx, num = vec_safe_length (elts);
6244 :
6245 6295777 : for (idx = 0; idx < num; idx++)
6246 : {
6247 4397580 : tree value = (*elts)[idx].value;
6248 4397580 : tree newval = value;
6249 4397580 : if (TREE_CODE (value) == CONSTRUCTOR)
6250 547066 : newval = optimize_compound_literals_in_ctor (value);
6251 3850514 : else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6252 : {
6253 103 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
6254 103 : tree decl = DECL_EXPR_DECL (decl_s);
6255 103 : tree init = DECL_INITIAL (decl);
6256 :
6257 103 : if (!TREE_ADDRESSABLE (value)
6258 103 : && !TREE_ADDRESSABLE (decl)
6259 103 : && init
6260 103 : && TREE_CODE (init) == CONSTRUCTOR)
6261 52 : newval = optimize_compound_literals_in_ctor (init);
6262 : }
6263 4397580 : if (newval == value)
6264 4397520 : continue;
6265 :
6266 60 : if (ctor == orig_ctor)
6267 : {
6268 49 : ctor = copy_node (orig_ctor);
6269 98 : CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
6270 49 : elts = CONSTRUCTOR_ELTS (ctor);
6271 : }
6272 60 : (*elts)[idx].value = newval;
6273 : }
6274 1898197 : return ctor;
6275 : }
6276 :
6277 : /* A subroutine of gimplify_modify_expr. Break out elements of a
6278 : CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
6279 :
6280 : Note that we still need to clear any elements that don't have explicit
6281 : initializers, so if not all elements are initialized we keep the
6282 : original MODIFY_EXPR, we just remove all of the constructor elements.
6283 :
6284 : If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
6285 : GS_ERROR if we would have to create a temporary when gimplifying
6286 : this constructor. Otherwise, return GS_OK.
6287 :
6288 : If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
6289 :
6290 : static enum gimplify_status
6291 1351079 : gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6292 : bool want_value, bool notify_temp_creation)
6293 : {
6294 1351079 : tree object, ctor, type;
6295 1351079 : enum gimplify_status ret;
6296 1351079 : vec<constructor_elt, va_gc> *elts;
6297 1351079 : bool cleared = false;
6298 1351079 : bool is_empty_ctor = false;
6299 1351079 : bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
6300 :
6301 1351079 : gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
6302 :
6303 1351079 : if (!notify_temp_creation)
6304 : {
6305 1342407 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6306 : is_gimple_lvalue, fb_lvalue);
6307 1342407 : if (ret == GS_ERROR)
6308 : return ret;
6309 : }
6310 :
6311 1351079 : object = TREE_OPERAND (*expr_p, 0);
6312 1351079 : ctor = TREE_OPERAND (*expr_p, 1)
6313 1351079 : = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
6314 1351079 : type = TREE_TYPE (ctor);
6315 1351079 : elts = CONSTRUCTOR_ELTS (ctor);
6316 1351079 : ret = GS_ALL_DONE;
6317 :
6318 1351079 : switch (TREE_CODE (type))
6319 : {
6320 1342457 : case RECORD_TYPE:
6321 1342457 : case UNION_TYPE:
6322 1342457 : case QUAL_UNION_TYPE:
6323 1342457 : case ARRAY_TYPE:
6324 1342457 : {
6325 : /* Use readonly data for initializers of this or smaller size
6326 : regardless of the num_nonzero_elements / num_unique_nonzero_elements
6327 : ratio. */
6328 1342457 : const HOST_WIDE_INT min_unique_size = 64;
6329 : /* If num_nonzero_elements / num_unique_nonzero_elements ratio
6330 : is smaller than this, use readonly data. */
6331 1342457 : const int unique_nonzero_ratio = 8;
6332 : /* True if a single access of the object must be ensured. This is the
6333 : case if the target is volatile, the type is non-addressable and more
6334 : than one field need to be assigned. */
6335 1342457 : const bool ensure_single_access
6336 1342457 : = TREE_THIS_VOLATILE (object)
6337 259 : && !TREE_ADDRESSABLE (type)
6338 1342662 : && vec_safe_length (elts) > 1;
6339 1342457 : struct gimplify_init_ctor_preeval_data preeval_data;
6340 1342457 : HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
6341 1342457 : HOST_WIDE_INT num_unique_nonzero_elements;
6342 1342457 : ctor_completeness complete_p;
6343 1342457 : bool valid_const_initializer;
6344 :
6345 : /* Aggregate types must lower constructors to initialization of
6346 : individual elements. The exception is that a CONSTRUCTOR node
6347 : with no elements indicates zero-initialization of the whole. */
6348 1342457 : if (vec_safe_is_empty (elts))
6349 : {
6350 338219 : if (notify_temp_creation)
6351 10255 : return GS_OK;
6352 :
6353 : /* The var will be initialized and so appear on lhs of
6354 : assignment, it can't be TREE_READONLY anymore. */
6355 338171 : if (VAR_P (object))
6356 188903 : TREE_READONLY (object) = 0;
6357 :
6358 : is_empty_ctor = true;
6359 342135 : break;
6360 : }
6361 :
6362 : /* Fetch information about the constructor to direct later processing.
6363 : We might want to make static versions of it in various cases, and
6364 : can only do so if it known to be a valid constant initializer. */
6365 1004238 : valid_const_initializer
6366 1004238 : = categorize_ctor_elements (ctor, &num_nonzero_elements,
6367 : &num_unique_nonzero_elements,
6368 : &num_ctor_elements, &complete_p);
6369 :
6370 : /* If a const aggregate variable is being initialized, then it
6371 : should never be a lose to promote the variable to be static. */
6372 1004238 : if (valid_const_initializer
6373 621936 : && num_nonzero_elements > 1
6374 455275 : && TREE_READONLY (object)
6375 7671 : && VAR_P (object)
6376 7294 : && !DECL_REGISTER (object)
6377 7287 : && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
6378 5137 : || DECL_MERGEABLE (object))
6379 : /* For ctors that have many repeated nonzero elements
6380 : represented through RANGE_EXPRs, prefer initializing
6381 : those through runtime loops over copies of large amounts
6382 : of data from readonly data section. */
6383 1004238 : && (num_unique_nonzero_elements
6384 3964 : > num_nonzero_elements / unique_nonzero_ratio
6385 0 : || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
6386 : <= (unsigned HOST_WIDE_INT) min_unique_size)))
6387 : {
6388 3964 : if (notify_temp_creation)
6389 : return GS_ERROR;
6390 :
6391 3964 : DECL_INITIAL (object) = ctor;
6392 3964 : TREE_STATIC (object) = 1;
6393 3964 : if (!DECL_NAME (object) || DECL_NAMELESS (object))
6394 2999 : DECL_NAME (object) = create_tmp_var_name ("C");
6395 3964 : walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
6396 :
6397 : /* ??? C++ doesn't automatically append a .<number> to the
6398 : assembler name, and even when it does, it looks at FE private
6399 : data structures to figure out what that number should be,
6400 : which are not set for this variable. I suppose this is
6401 : important for local statics for inline functions, which aren't
6402 : "local" in the object file sense. So in order to get a unique
6403 : TU-local symbol, we must invoke the lhd version now. */
6404 3964 : lhd_set_decl_assembler_name (object);
6405 :
6406 3964 : *expr_p = NULL_TREE;
6407 3964 : break;
6408 : }
6409 :
6410 : /* The var will be initialized and so appear on lhs of
6411 : assignment, it can't be TREE_READONLY anymore. */
6412 1000274 : if (VAR_P (object) && !notify_temp_creation)
6413 789563 : TREE_READONLY (object) = 0;
6414 :
6415 : /* If there are "lots" of initialized elements, even discounting
6416 : those that are not address constants (and thus *must* be
6417 : computed at runtime), then partition the constructor into
6418 : constant and non-constant parts. Block copy the constant
6419 : parts in, then generate code for the non-constant parts. */
6420 : /* TODO. There's code in cp/typeck.cc to do this. */
6421 :
6422 1000274 : if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
6423 : /* store_constructor will ignore the clearing of variable-sized
6424 : objects. Initializers for such objects must explicitly set
6425 : every field that needs to be set. */
6426 : cleared = false;
6427 1000254 : else if (complete_p.sparse)
6428 : /* If the constructor isn't complete, clear the whole object
6429 : beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
6430 :
6431 : ??? This ought not to be needed. For any element not present
6432 : in the initializer, we should simply set them to zero. Except
6433 : we'd need to *find* the elements that are not present, and that
6434 : requires trickery to avoid quadratic compile-time behavior in
6435 : large cases or excessive memory use in small cases. */
6436 191437 : cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
6437 808817 : else if (num_ctor_elements - num_nonzero_elements
6438 808817 : > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
6439 808817 : && num_nonzero_elements < num_ctor_elements / 4)
6440 : /* If there are "lots" of zeros, it's more efficient to clear
6441 : the memory and then set the nonzero elements. */
6442 : cleared = true;
6443 807937 : else if (ensure_single_access && num_nonzero_elements == 0)
6444 : /* If a single access to the target must be ensured and all elements
6445 : are zero, then it's optimal to clear whatever their number. */
6446 : cleared = true;
6447 : /* If the object is small enough to go in registers, and it's
6448 : not required to be constructed in memory, clear it first.
6449 : That will avoid wasting cycles preserving any padding bits
6450 : that might be there, and if there aren't any, the compiler
6451 : is smart enough to optimize the clearing out. */
6452 807928 : else if ((complete_p.sparse || complete_p.padded_union
6453 807444 : || complete_p.padded_non_union)
6454 15024 : && !TREE_ADDRESSABLE (ctor)
6455 14938 : && !TREE_THIS_VOLATILE (object)
6456 14935 : && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
6457 815155 : && optimize)
6458 : cleared = true;
6459 : else
6460 : cleared = false;
6461 :
6462 : /* If there are "lots" of initialized elements, and all of them
6463 : are valid address constants, then the entire initializer can
6464 : be dropped to memory, and then memcpy'd out. Don't do this
6465 : for sparse arrays, though, as it's more efficient to follow
6466 : the standard CONSTRUCTOR behavior of memset followed by
6467 : individual element initialization. Also don't do this for small
6468 : all-zero initializers (which aren't big enough to merit
6469 : clearing), and don't try to make bitwise copies of
6470 : TREE_ADDRESSABLE types. */
6471 1000274 : if (valid_const_initializer
6472 617972 : && !complete_p.sparse
6473 441625 : && !(cleared || num_nonzero_elements == 0)
6474 344851 : && !TREE_ADDRESSABLE (type))
6475 : {
6476 342975 : HOST_WIDE_INT size = int_size_in_bytes (type);
6477 342975 : unsigned int align;
6478 :
6479 : /* ??? We can still get unbounded array types, at least
6480 : from the C++ front end. This seems wrong, but attempt
6481 : to work around it for now. */
6482 342975 : if (size < 0)
6483 : {
6484 9 : size = int_size_in_bytes (TREE_TYPE (object));
6485 9 : if (size >= 0)
6486 0 : TREE_TYPE (ctor) = type = TREE_TYPE (object);
6487 : }
6488 :
6489 : /* Find the maximum alignment we can assume for the object. */
6490 : /* ??? Make use of DECL_OFFSET_ALIGN. */
6491 342975 : if (DECL_P (object))
6492 328609 : align = DECL_ALIGN (object);
6493 : else
6494 14366 : align = TYPE_ALIGN (type);
6495 :
6496 : /* Do a block move either if the size is so small as to make
6497 : each individual move a sub-unit move on average, or if it
6498 : is so large as to make individual moves inefficient. */
6499 342975 : if (size > 0
6500 342966 : && num_nonzero_elements > 1
6501 : /* For ctors that have many repeated nonzero elements
6502 : represented through RANGE_EXPRs, prefer initializing
6503 : those through runtime loops over copies of large amounts
6504 : of data from readonly data section. */
6505 303709 : && (num_unique_nonzero_elements
6506 303709 : > num_nonzero_elements / unique_nonzero_ratio
6507 56 : || size <= min_unique_size)
6508 646628 : && (size < num_nonzero_elements
6509 303547 : || !can_move_by_pieces (size, align)))
6510 : {
6511 2840 : if (notify_temp_creation)
6512 : return GS_ERROR;
6513 :
6514 1422 : walk_tree (&ctor, force_labels_r, NULL, NULL);
6515 1422 : ctor = tree_output_constant_def (ctor);
6516 1422 : if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
6517 0 : ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
6518 1422 : TREE_OPERAND (*expr_p, 1) = ctor;
6519 :
6520 : /* This is no longer an assignment of a CONSTRUCTOR, but
6521 : we still may have processing to do on the LHS. So
6522 : pretend we didn't do anything here to let that happen. */
6523 1422 : return GS_UNHANDLED;
6524 : }
6525 : }
6526 :
6527 992712 : if (!cleared)
6528 : {
6529 798634 : if (complete_p.padded_non_union
6530 7924 : && warn_zero_init_padding_bits >= ZERO_INIT_PADDING_BITS_ALL)
6531 12 : warning (OPT_Wzero_init_padding_bits_,
6532 : "padding might not be initialized to zero; "
6533 : "if code relies on it being zero, consider "
6534 : "using %<-fzero-init-padding-bits=all%>");
6535 798622 : else if (complete_p.padded_union
6536 120 : && warn_zero_init_padding_bits
6537 : >= ZERO_INIT_PADDING_BITS_UNIONS)
6538 14 : warning (OPT_Wzero_init_padding_bits_,
6539 : "padding might not be initialized to zero; "
6540 : "if code relies on it being zero, consider "
6541 : "using %<-fzero-init-padding-bits=unions%> "
6542 : "or %<-fzero-init-padding-bits=all%>");
6543 : }
6544 :
6545 : /* If a single access to the target must be ensured and there are
6546 : nonzero elements or the zero elements are not assigned en masse,
6547 : initialize the target from a temporary. */
6548 997434 : if (ensure_single_access && (num_nonzero_elements > 0 || !cleared))
6549 : {
6550 167 : if (notify_temp_creation)
6551 : return GS_ERROR;
6552 :
6553 161 : tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
6554 161 : TREE_OPERAND (*expr_p, 0) = temp;
6555 161 : *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
6556 : *expr_p,
6557 : build2 (MODIFY_EXPR, void_type_node,
6558 : object, temp));
6559 161 : return GS_OK;
6560 : }
6561 :
6562 997267 : if (notify_temp_creation)
6563 : return GS_OK;
6564 :
6565 : /* If there are nonzero elements and if needed, pre-evaluate to capture
6566 : elements overlapping with the lhs into temporaries. We must do this
6567 : before clearing to fetch the values before they are zeroed-out. */
6568 990067 : if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
6569 : {
6570 219439 : preeval_data.lhs_base_decl = get_base_address (object);
6571 219439 : if (!DECL_P (preeval_data.lhs_base_decl))
6572 41308 : preeval_data.lhs_base_decl = NULL;
6573 219439 : preeval_data.lhs_alias_set = get_alias_set (object);
6574 :
6575 219439 : gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
6576 : pre_p, post_p, &preeval_data);
6577 : }
6578 :
6579 990067 : bool ctor_has_side_effects_p
6580 990067 : = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
6581 :
6582 990067 : if (cleared)
6583 : {
6584 : /* Zap the CONSTRUCTOR element list, which simplifies this case.
6585 : Note that we still have to gimplify, in order to handle the
6586 : case of variable sized types. Avoid shared tree structures. */
6587 198563 : CONSTRUCTOR_ELTS (ctor) = NULL;
6588 198563 : TREE_SIDE_EFFECTS (ctor) = 0;
6589 198563 : object = unshare_expr (object);
6590 198563 : gimplify_stmt (expr_p, pre_p);
6591 : }
6592 :
6593 : /* If we have not block cleared the object, or if there are nonzero
6594 : elements in the constructor, or if the constructor has side effects,
6595 : add assignments to the individual scalar fields of the object. */
6596 198563 : if (!cleared
6597 198563 : || num_nonzero_elements > 0
6598 20384 : || ctor_has_side_effects_p)
6599 970077 : gimplify_init_ctor_eval (object, elts, pre_p, cleared);
6600 :
6601 990067 : *expr_p = NULL_TREE;
6602 : }
6603 990067 : break;
6604 :
6605 0 : case COMPLEX_TYPE:
6606 0 : {
6607 0 : tree r, i;
6608 :
6609 0 : if (notify_temp_creation)
6610 : return GS_OK;
6611 :
6612 : /* Extract the real and imaginary parts out of the ctor. */
6613 0 : gcc_assert (elts->length () == 2);
6614 0 : r = (*elts)[0].value;
6615 0 : i = (*elts)[1].value;
6616 0 : if (r == NULL || i == NULL)
6617 : {
6618 0 : tree zero = build_zero_cst (TREE_TYPE (type));
6619 0 : if (r == NULL)
6620 0 : r = zero;
6621 0 : if (i == NULL)
6622 0 : i = zero;
6623 : }
6624 :
6625 : /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
6626 : represent creation of a complex value. */
6627 0 : if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
6628 : {
6629 0 : ctor = build_complex (type, r, i);
6630 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6631 : }
6632 : else
6633 : {
6634 0 : ctor = build2 (COMPLEX_EXPR, type, r, i);
6635 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6636 0 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
6637 : pre_p,
6638 : post_p,
6639 0 : rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
6640 : fb_rvalue);
6641 : }
6642 : }
6643 : break;
6644 :
6645 8622 : case VECTOR_TYPE:
6646 8622 : {
6647 8622 : unsigned HOST_WIDE_INT ix;
6648 8622 : constructor_elt *ce;
6649 :
6650 8622 : if (notify_temp_creation)
6651 1351079 : return GS_OK;
6652 :
6653 : /* Vector types use CONSTRUCTOR all the way through gimple
6654 : compilation as a general initializer. */
6655 74694 : FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
6656 : {
6657 66072 : enum gimplify_status tret;
6658 66072 : tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
6659 : fb_rvalue);
6660 66072 : if (tret == GS_ERROR)
6661 : ret = GS_ERROR;
6662 66071 : else if (TREE_STATIC (ctor)
6663 66104 : && !initializer_constant_valid_p (ce->value,
6664 33 : TREE_TYPE (ce->value)))
6665 33 : TREE_STATIC (ctor) = 0;
6666 : }
6667 8622 : recompute_constructor_flags (ctor);
6668 :
6669 : /* Go ahead and simplify constant constructors to VECTOR_CST. */
6670 8622 : if (TREE_CONSTANT (ctor))
6671 : {
6672 2705 : bool constant_p = true;
6673 : tree value;
6674 :
6675 : /* Even when ctor is constant, it might contain non-*_CST
6676 : elements, such as addresses or trapping values like
6677 : 1.0/0.0 - 1.0/0.0. Such expressions don't belong
6678 : in VECTOR_CST nodes. */
6679 2705 : FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
6680 2483 : if (!CONSTANT_CLASS_P (value))
6681 : {
6682 : constant_p = false;
6683 : break;
6684 : }
6685 :
6686 222 : if (constant_p)
6687 : {
6688 222 : TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
6689 222 : break;
6690 : }
6691 : }
6692 :
6693 8400 : if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
6694 938 : TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
6695 : }
6696 : break;
6697 :
6698 0 : default:
6699 : /* So how did we get a CONSTRUCTOR for a scalar type? */
6700 0 : gcc_unreachable ();
6701 : }
6702 :
6703 1340824 : if (ret == GS_ERROR)
6704 : return GS_ERROR;
6705 : /* If we have gimplified both sides of the initializer but have
6706 : not emitted an assignment, do so now. */
6707 1340823 : if (*expr_p
6708 : /* If the type is an empty type, we don't need to emit the
6709 : assignment. */
6710 1340823 : && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
6711 : {
6712 342725 : tree lhs = TREE_OPERAND (*expr_p, 0);
6713 342725 : tree rhs = TREE_OPERAND (*expr_p, 1);
6714 342725 : if (want_value && object == lhs)
6715 12 : lhs = unshare_expr (lhs);
6716 342725 : gassign *init = gimple_build_assign (lhs, rhs);
6717 342725 : gimplify_seq_add_stmt (pre_p, init);
6718 : }
6719 1340823 : if (want_value)
6720 : {
6721 : *expr_p = object;
6722 1340823 : ret = GS_OK;
6723 : }
6724 : else
6725 : {
6726 1340804 : *expr_p = NULL;
6727 1340804 : ret = GS_ALL_DONE;
6728 : }
6729 :
6730 : /* If the user requests to initialize automatic variables, we
6731 : should initialize paddings inside the variable. Add a call to
6732 : __builtin_clear_pading (&object, 0, for_auto_init = true) to
6733 : initialize paddings of object always to zero regardless of
6734 : INIT_TYPE. Note, we will not insert this call if the aggregate
6735 : variable has be completely cleared already or it's initialized
6736 : with an empty constructor. We cannot insert this call if the
6737 : variable is a gimple register since __builtin_clear_padding will take
6738 : the address of the variable. As a result, if a long double/_Complex long
6739 : double variable will be spilled into stack later, its padding cannot
6740 : be cleared with __builtin_clear_padding. We should clear its padding
6741 : when it is spilled into memory. */
6742 1340823 : if (is_init_expr
6743 954065 : && !is_gimple_reg (object)
6744 948638 : && clear_padding_type_may_have_padding_p (type)
6745 903429 : && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
6746 228702 : || !AGGREGATE_TYPE_P (type))
6747 674727 : && var_needs_auto_init_p (object)
6748 1376473 : && flag_auto_var_init != AUTO_INIT_CXX26)
6749 41 : gimple_add_padding_init_for_auto_var (object, false, pre_p);
6750 :
6751 : return ret;
6752 : }
6753 :
6754 : /* Given a pointer value OP0, return a simplified version of an
6755 : indirection through OP0, or NULL_TREE if no simplification is
6756 : possible. This may only be applied to a rhs of an expression.
6757 : Note that the resulting type may be different from the type pointed
6758 : to in the sense that it is still compatible from the langhooks
6759 : point of view. */
6760 :
6761 : static tree
6762 530803 : gimple_fold_indirect_ref_rhs (tree t)
6763 : {
6764 0 : return gimple_fold_indirect_ref (t);
6765 : }
6766 :
6767 : /* Subroutine of gimplify_modify_expr to do simplifications of
6768 : MODIFY_EXPRs based on the code of the RHS. We loop for as long as
6769 : something changes. */
6770 :
6771 : static enum gimplify_status
6772 98982785 : gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
6773 : gimple_seq *pre_p, gimple_seq *post_p,
6774 : bool want_value)
6775 : {
6776 98982785 : enum gimplify_status ret = GS_UNHANDLED;
6777 99634818 : bool changed;
6778 :
6779 98140240 : do
6780 : {
6781 99634818 : changed = false;
6782 99634818 : switch (TREE_CODE (*from_p))
6783 : {
6784 12006279 : case VAR_DECL:
6785 : /* If we're assigning from a read-only variable initialized with
6786 : a constructor and not volatile, do the direct assignment from
6787 : the constructor, but only if the target is not volatile either
6788 : since this latter assignment might end up being done on a per
6789 : field basis. However, if the target is volatile and the type
6790 : is aggregate and non-addressable, gimplify_init_constructor
6791 : knows that it needs to ensure a single access to the target
6792 : and it will return GS_OK only in this case. */
6793 12006279 : if (TREE_READONLY (*from_p)
6794 94920 : && DECL_INITIAL (*from_p)
6795 61551 : && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
6796 8672 : && !TREE_THIS_VOLATILE (*from_p)
6797 12014951 : && (!TREE_THIS_VOLATILE (*to_p)
6798 6 : || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
6799 6 : && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
6800 : {
6801 8672 : tree old_from = *from_p;
6802 8672 : enum gimplify_status subret;
6803 :
6804 : /* Move the constructor into the RHS. */
6805 8672 : *from_p = unshare_expr (DECL_INITIAL (*from_p));
6806 :
6807 : /* Let's see if gimplify_init_constructor will need to put
6808 : it in memory. */
6809 8672 : subret = gimplify_init_constructor (expr_p, NULL, NULL,
6810 : false, true);
6811 8672 : if (subret == GS_ERROR)
6812 : {
6813 : /* If so, revert the change. */
6814 1424 : *from_p = old_from;
6815 : }
6816 : else
6817 : {
6818 : ret = GS_OK;
6819 : changed = true;
6820 : }
6821 : }
6822 : break;
6823 538940 : case INDIRECT_REF:
6824 538940 : if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
6825 : /* If we have code like
6826 :
6827 : *(const A*)(A*)&x
6828 :
6829 : where the type of "x" is a (possibly cv-qualified variant
6830 : of "A"), treat the entire expression as identical to "x".
6831 : This kind of code arises in C++ when an object is bound
6832 : to a const reference, and if "x" is a TARGET_EXPR we want
6833 : to take advantage of the optimization below. But not if
6834 : the type is TREE_ADDRESSABLE; then C++17 says that the
6835 : TARGET_EXPR needs to be a temporary. */
6836 1061606 : if (tree t
6837 530803 : = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
6838 : {
6839 129397 : bool volatile_p = TREE_THIS_VOLATILE (*from_p);
6840 129397 : if (TREE_THIS_VOLATILE (t) != volatile_p)
6841 : {
6842 5 : if (DECL_P (t))
6843 0 : t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
6844 : build_fold_addr_expr (t));
6845 5 : if (REFERENCE_CLASS_P (t))
6846 5 : TREE_THIS_VOLATILE (t) = volatile_p;
6847 : }
6848 129397 : *from_p = t;
6849 129397 : ret = GS_OK;
6850 129397 : changed = true;
6851 : }
6852 : break;
6853 :
6854 197582 : case TARGET_EXPR:
6855 197582 : {
6856 : /* If we are initializing something from a TARGET_EXPR, strip the
6857 : TARGET_EXPR and initialize it directly, if possible. This can't
6858 : be done if the initializer is void, since that implies that the
6859 : temporary is set in some non-trivial way.
6860 :
6861 : ??? What about code that pulls out the temp and uses it
6862 : elsewhere? I think that such code never uses the TARGET_EXPR as
6863 : an initializer. If I'm wrong, we'll die because the temp won't
6864 : have any RTL. In that case, I guess we'll need to replace
6865 : references somehow. */
6866 197582 : tree init = TARGET_EXPR_INITIAL (*from_p);
6867 :
6868 197582 : if (init
6869 196866 : && (TREE_CODE (*expr_p) != MODIFY_EXPR
6870 51387 : || !TARGET_EXPR_NO_ELIDE (*from_p))
6871 394238 : && !VOID_TYPE_P (TREE_TYPE (init)))
6872 : {
6873 181316 : *from_p = init;
6874 181316 : ret = GS_OK;
6875 181316 : changed = true;
6876 : }
6877 : }
6878 : break;
6879 :
6880 333709 : case COMPOUND_EXPR:
6881 : /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
6882 : caught. */
6883 333709 : gimplify_compound_expr (from_p, pre_p, true);
6884 333709 : ret = GS_OK;
6885 333709 : changed = true;
6886 333709 : break;
6887 :
6888 1363067 : case CONSTRUCTOR:
6889 : /* If we already made some changes, let the front end have a
6890 : crack at this before we break it down. */
6891 1363067 : if (ret != GS_UNHANDLED)
6892 : break;
6893 :
6894 : /* If we're initializing from a CONSTRUCTOR, break this into
6895 : individual MODIFY_EXPRs. */
6896 1342407 : ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
6897 : false);
6898 1342407 : return ret;
6899 :
6900 226864 : case COND_EXPR:
6901 : /* If we're assigning to a non-register type, push the assignment
6902 : down into the branches. This is mandatory for ADDRESSABLE types,
6903 : since we cannot generate temporaries for such, but it saves a
6904 : copy in other cases as well.
6905 : Also avoid an extra temporary and copy when assigning to
6906 : a register. */
6907 226864 : if (!is_gimple_reg_type (TREE_TYPE (*from_p))
6908 226864 : || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
6909 : {
6910 : /* This code should mirror the code in gimplify_cond_expr. */
6911 145352 : enum tree_code code = TREE_CODE (*expr_p);
6912 145352 : tree cond = *from_p;
6913 145352 : tree result = *to_p;
6914 :
6915 145352 : ret = gimplify_expr (&result, pre_p, post_p,
6916 : is_gimple_lvalue, fb_lvalue);
6917 145352 : if (ret != GS_ERROR)
6918 145352 : ret = GS_OK;
6919 :
6920 : /* If we are going to write RESULT more than once, clear
6921 : TREE_READONLY flag, otherwise we might incorrectly promote
6922 : the variable to static const and initialize it at compile
6923 : time in one of the branches. */
6924 145352 : if (VAR_P (result)
6925 144330 : && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
6926 280609 : && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6927 135156 : TREE_READONLY (result) = 0;
6928 145352 : if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
6929 136143 : TREE_OPERAND (cond, 1)
6930 272286 : = build2 (code, void_type_node, result,
6931 136143 : TREE_OPERAND (cond, 1));
6932 145352 : if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6933 145251 : TREE_OPERAND (cond, 2)
6934 290502 : = build2 (code, void_type_node, unshare_expr (result),
6935 145251 : TREE_OPERAND (cond, 2));
6936 :
6937 145352 : TREE_TYPE (cond) = void_type_node;
6938 145352 : recalculate_side_effects (cond);
6939 :
6940 145352 : if (want_value)
6941 : {
6942 68 : gimplify_and_add (cond, pre_p);
6943 68 : *expr_p = unshare_expr (result);
6944 : }
6945 : else
6946 : *expr_p = cond;
6947 145352 : return ret;
6948 : }
6949 : break;
6950 :
6951 10247877 : case CALL_EXPR:
6952 : /* For calls that return in memory, give *to_p as the CALL_EXPR's
6953 : return slot so that we don't generate a temporary. */
6954 10247877 : if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
6955 10247877 : && aggregate_value_p (*from_p, *from_p))
6956 : {
6957 253432 : bool use_target;
6958 :
6959 253432 : if (!(rhs_predicate_for (*to_p))(*from_p))
6960 : /* If we need a temporary, *to_p isn't accurate. */
6961 : use_target = false;
6962 : /* It's OK to use the return slot directly unless it's an NRV. */
6963 252611 : else if (TREE_CODE (*to_p) == RESULT_DECL
6964 6724 : && DECL_NAME (*to_p) == NULL_TREE
6965 259191 : && needs_to_live_in_memory (*to_p))
6966 : use_target = true;
6967 246031 : else if (is_gimple_reg_type (TREE_TYPE (*to_p))
6968 246031 : || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
6969 : /* Don't force regs into memory. */
6970 : use_target = false;
6971 231501 : else if (TREE_CODE (*expr_p) == INIT_EXPR)
6972 : /* It's OK to use the target directly if it's being
6973 : initialized. */
6974 : use_target = true;
6975 14647 : else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
6976 : != INTEGER_CST)
6977 : /* Always use the target and thus RSO for variable-sized types.
6978 : GIMPLE cannot deal with a variable-sized assignment
6979 : embedded in a call statement. */
6980 : use_target = true;
6981 14619 : else if (TREE_CODE (*to_p) != SSA_NAME
6982 14619 : && (!is_gimple_variable (*to_p)
6983 11282 : || needs_to_live_in_memory (*to_p)))
6984 : /* Don't use the original target if it's already addressable;
6985 : if its address escapes, and the called function uses the
6986 : NRV optimization, a conforming program could see *to_p
6987 : change before the called function returns; see c++/19317.
6988 : When optimizing, the return_slot pass marks more functions
6989 : as safe after we have escape info. */
6990 : use_target = false;
6991 : else
6992 : use_target = true;
6993 :
6994 : if (use_target)
6995 : {
6996 225647 : CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
6997 225647 : mark_addressable (*to_p);
6998 : }
6999 : }
7000 : break;
7001 :
7002 1560 : case WITH_SIZE_EXPR:
7003 : /* Likewise for calls that return an aggregate of non-constant size,
7004 : since we would not be able to generate a temporary at all. */
7005 1560 : if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
7006 : {
7007 350 : *from_p = TREE_OPERAND (*from_p, 0);
7008 : /* We don't change ret in this case because the
7009 : WITH_SIZE_EXPR might have been added in
7010 : gimplify_modify_expr, so returning GS_OK would lead to an
7011 : infinite loop. */
7012 350 : changed = true;
7013 : }
7014 : break;
7015 :
7016 : /* If we're initializing from a container, push the initialization
7017 : inside it. */
7018 1937 : case CLEANUP_POINT_EXPR:
7019 1937 : case BIND_EXPR:
7020 1937 : case STATEMENT_LIST:
7021 1937 : {
7022 1937 : tree wrap = *from_p;
7023 1937 : tree t;
7024 :
7025 1937 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
7026 : fb_lvalue);
7027 1937 : if (ret != GS_ERROR)
7028 1937 : ret = GS_OK;
7029 :
7030 1937 : t = voidify_wrapper_expr (wrap, *expr_p);
7031 1937 : gcc_assert (t == *expr_p);
7032 :
7033 1937 : if (want_value)
7034 : {
7035 6 : gimplify_and_add (wrap, pre_p);
7036 6 : *expr_p = unshare_expr (*to_p);
7037 : }
7038 : else
7039 1931 : *expr_p = wrap;
7040 : return GS_OK;
7041 : }
7042 :
7043 9801239 : case NOP_EXPR:
7044 : /* Pull out compound literal expressions from a NOP_EXPR.
7045 : Those are created in the C FE to drop qualifiers during
7046 : lvalue conversion. */
7047 9801239 : if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
7048 9801239 : && tree_ssa_useless_type_conversion (*from_p))
7049 : {
7050 13 : *from_p = TREE_OPERAND (*from_p, 0);
7051 13 : ret = GS_OK;
7052 13 : changed = true;
7053 : }
7054 : break;
7055 :
7056 4882 : case COMPOUND_LITERAL_EXPR:
7057 4882 : {
7058 4882 : tree complit = TREE_OPERAND (*expr_p, 1);
7059 4882 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
7060 4882 : tree decl = DECL_EXPR_DECL (decl_s);
7061 4882 : tree init = DECL_INITIAL (decl);
7062 :
7063 : /* struct T x = (struct T) { 0, 1, 2 } can be optimized
7064 : into struct T x = { 0, 1, 2 } if the address of the
7065 : compound literal has never been taken. */
7066 4882 : if (!TREE_ADDRESSABLE (complit)
7067 4882 : && !TREE_ADDRESSABLE (decl)
7068 4882 : && init)
7069 : {
7070 4882 : *expr_p = copy_node (*expr_p);
7071 4882 : TREE_OPERAND (*expr_p, 1) = init;
7072 4882 : return GS_OK;
7073 : }
7074 : }
7075 :
7076 : default:
7077 : break;
7078 : }
7079 : }
7080 : while (changed);
7081 :
7082 : return ret;
7083 : }
7084 :
7085 :
7086 : /* Return true if T looks like a valid GIMPLE statement. */
7087 :
7088 : static bool
7089 19572471 : is_gimple_stmt (tree t)
7090 : {
7091 19572471 : const enum tree_code code = TREE_CODE (t);
7092 :
7093 19572471 : switch (code)
7094 : {
7095 1762544 : case NOP_EXPR:
7096 : /* The only valid NOP_EXPR is the empty statement. */
7097 1762544 : return IS_EMPTY_STMT (t);
7098 :
7099 0 : case BIND_EXPR:
7100 0 : case COND_EXPR:
7101 : /* These are only valid if they're void. */
7102 0 : return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
7103 :
7104 : case SWITCH_EXPR:
7105 : case GOTO_EXPR:
7106 : case RETURN_EXPR:
7107 : case LABEL_EXPR:
7108 : case CASE_LABEL_EXPR:
7109 : case TRY_CATCH_EXPR:
7110 : case TRY_FINALLY_EXPR:
7111 : case EH_FILTER_EXPR:
7112 : case CATCH_EXPR:
7113 : case ASM_EXPR:
7114 : case STATEMENT_LIST:
7115 : case OACC_PARALLEL:
7116 : case OACC_KERNELS:
7117 : case OACC_SERIAL:
7118 : case OACC_DATA:
7119 : case OACC_HOST_DATA:
7120 : case OACC_DECLARE:
7121 : case OACC_UPDATE:
7122 : case OACC_ENTER_DATA:
7123 : case OACC_EXIT_DATA:
7124 : case OACC_CACHE:
7125 : case OMP_PARALLEL:
7126 : case OMP_FOR:
7127 : case OMP_SIMD:
7128 : case OMP_DISTRIBUTE:
7129 : case OMP_LOOP:
7130 : case OMP_TILE:
7131 : case OMP_UNROLL:
7132 : case OACC_LOOP:
7133 : case OMP_SCAN:
7134 : case OMP_SCOPE:
7135 : case OMP_DISPATCH:
7136 : case OMP_SECTIONS:
7137 : case OMP_SECTION:
7138 : case OMP_STRUCTURED_BLOCK:
7139 : case OMP_SINGLE:
7140 : case OMP_MASTER:
7141 : case OMP_MASKED:
7142 : case OMP_TASKGROUP:
7143 : case OMP_ORDERED:
7144 : case OMP_CRITICAL:
7145 : case OMP_METADIRECTIVE:
7146 : case OMP_TASK:
7147 : case OMP_TARGET:
7148 : case OMP_TARGET_DATA:
7149 : case OMP_TARGET_UPDATE:
7150 : case OMP_TARGET_ENTER_DATA:
7151 : case OMP_TARGET_EXIT_DATA:
7152 : case OMP_TASKLOOP:
7153 : case OMP_TEAMS:
7154 : /* These are always void. */
7155 : return true;
7156 :
7157 : case CALL_EXPR:
7158 : case MODIFY_EXPR:
7159 : case PREDICT_EXPR:
7160 : /* These are valid regardless of their type. */
7161 : return true;
7162 :
7163 : default:
7164 : return false;
7165 : }
7166 : }
7167 :
7168 :
7169 : /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
7170 : a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a gimple register.
7171 :
7172 : IMPORTANT NOTE: This promotion is performed by introducing a load of the
7173 : other, unmodified part of the complex object just before the total store.
7174 : As a consequence, if the object is still uninitialized, an undefined value
7175 : will be loaded into a register, which may result in a spurious exception
7176 : if the register is floating-point and the value happens to be a signaling
7177 : NaN for example. Then the fully-fledged complex operations lowering pass
7178 : followed by a DCE pass are necessary in order to fix things up. */
7179 :
7180 : static enum gimplify_status
7181 1942 : gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
7182 : bool want_value)
7183 : {
7184 1942 : enum tree_code code, ocode;
7185 1942 : tree lhs, rhs, new_rhs, other, realpart, imagpart;
7186 :
7187 1942 : lhs = TREE_OPERAND (*expr_p, 0);
7188 1942 : rhs = TREE_OPERAND (*expr_p, 1);
7189 1942 : code = TREE_CODE (lhs);
7190 1942 : lhs = TREE_OPERAND (lhs, 0);
7191 :
7192 1942 : ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
7193 1942 : other = build1 (ocode, TREE_TYPE (rhs), lhs);
7194 1942 : suppress_warning (other);
7195 1942 : other = get_formal_tmp_var (other, pre_p);
7196 :
7197 1942 : realpart = code == REALPART_EXPR ? rhs : other;
7198 957 : imagpart = code == REALPART_EXPR ? other : rhs;
7199 :
7200 1942 : if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
7201 0 : new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
7202 : else
7203 1942 : new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
7204 :
7205 1942 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
7206 1942 : *expr_p = (want_value) ? rhs : NULL_TREE;
7207 :
7208 1942 : return GS_ALL_DONE;
7209 : }
7210 :
7211 : /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
7212 :
7213 : modify_expr
7214 : : varname '=' rhs
7215 : | '*' ID '=' rhs
7216 :
7217 : PRE_P points to the list where side effects that must happen before
7218 : *EXPR_P should be stored.
7219 :
7220 : POST_P points to the list where side effects that must happen after
7221 : *EXPR_P should be stored.
7222 :
7223 : WANT_VALUE is nonzero iff we want to use the value of this expression
7224 : in another expression. */
7225 :
7226 : static enum gimplify_status
7227 51148885 : gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7228 : bool want_value)
7229 : {
7230 51148885 : tree *from_p = &TREE_OPERAND (*expr_p, 1);
7231 51148885 : tree *to_p = &TREE_OPERAND (*expr_p, 0);
7232 51148885 : enum gimplify_status ret = GS_UNHANDLED;
7233 51148885 : gimple *assign;
7234 51148885 : location_t loc = EXPR_LOCATION (*expr_p);
7235 51148885 : gimple_stmt_iterator gsi;
7236 :
7237 51148885 : if (error_operand_p (*from_p) || error_operand_p (*to_p))
7238 : return GS_ERROR;
7239 :
7240 51148857 : gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
7241 : || TREE_CODE (*expr_p) == INIT_EXPR);
7242 :
7243 : /* Trying to simplify a clobber using normal logic doesn't work,
7244 : so handle it here. */
7245 51148857 : if (TREE_CLOBBER_P (*from_p))
7246 : {
7247 619948 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7248 619948 : if (ret == GS_ERROR)
7249 : return ret;
7250 619948 : gcc_assert (!want_value);
7251 619948 : if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
7252 : {
7253 208 : tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
7254 : pre_p, post_p);
7255 208 : *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
7256 : }
7257 619948 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
7258 619948 : *expr_p = NULL;
7259 619948 : return GS_ALL_DONE;
7260 : }
7261 :
7262 : /* Convert initialization from an empty variable-size CONSTRUCTOR to
7263 : memset. */
7264 50528909 : if (TREE_TYPE (*from_p) != error_mark_node
7265 50528909 : && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
7266 50528909 : && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
7267 1593 : && TREE_CODE (*from_p) == CONSTRUCTOR
7268 50529013 : && CONSTRUCTOR_NELTS (*from_p) == 0)
7269 : {
7270 84 : maybe_with_size_expr (from_p);
7271 84 : gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR);
7272 84 : return gimplify_modify_expr_to_memset (expr_p,
7273 84 : TREE_OPERAND (*from_p, 1),
7274 84 : want_value, pre_p);
7275 : }
7276 :
7277 : /* Insert pointer conversions required by the middle-end that are not
7278 : required by the frontend. This fixes middle-end type checking for
7279 : for example gcc.dg/redecl-6.c. */
7280 50528825 : if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
7281 : {
7282 13707076 : STRIP_USELESS_TYPE_CONVERSION (*from_p);
7283 13707076 : if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
7284 754 : *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
7285 : }
7286 :
7287 : /* See if any simplifications can be done based on what the RHS is. */
7288 50528825 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7289 : want_value);
7290 50528825 : if (ret != GS_UNHANDLED)
7291 : return ret;
7292 :
7293 : /* For empty types only gimplify the left hand side and right hand
7294 : side as statements and throw away the assignment. Do this after
7295 : gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
7296 : types properly. */
7297 48456244 : if (is_empty_type (TREE_TYPE (*from_p))
7298 13647 : && !want_value
7299 : /* Don't do this for calls that return addressable types, expand_call
7300 : relies on those having a lhs. */
7301 48469884 : && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
7302 11581 : && TREE_CODE (*from_p) == CALL_EXPR))
7303 : {
7304 2059 : suppress_warning (*from_p, OPT_Wunused_result);
7305 2059 : gimplify_stmt (from_p, pre_p);
7306 2059 : gimplify_stmt (to_p, pre_p);
7307 2059 : *expr_p = NULL_TREE;
7308 2059 : return GS_ALL_DONE;
7309 : }
7310 :
7311 : /* If the value being copied is of variable width, compute the length
7312 : of the copy into a WITH_SIZE_EXPR. Note that we need to do this
7313 : before gimplifying any of the operands so that we can resolve any
7314 : PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
7315 : the size of the expression to be copied, not of the destination, so
7316 : that is what we must do here. */
7317 48454185 : maybe_with_size_expr (from_p);
7318 :
7319 : /* As a special case, we have to temporarily allow for assignments
7320 : with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
7321 : a toplevel statement, when gimplifying the GENERIC expression
7322 : MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
7323 : GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
7324 :
7325 : Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
7326 : prevent gimplify_expr from trying to create a new temporary for
7327 : foo's LHS, we tell it that it should only gimplify until it
7328 : reaches the CALL_EXPR. On return from gimplify_expr, the newly
7329 : created GIMPLE_CALL <foo> will be the last statement in *PRE_P
7330 : and all we need to do here is set 'a' to be its LHS. */
7331 :
7332 : /* Gimplify the RHS first for C++17 and bug 71104. */
7333 48454185 : gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
7334 48454185 : ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
7335 48454185 : if (ret == GS_ERROR)
7336 : return ret;
7337 :
7338 : /* Then gimplify the LHS. */
7339 : /* If we gimplified the RHS to a CALL_EXPR and that call may return
7340 : twice we have to make sure to gimplify into non-SSA as otherwise
7341 : the abnormal edge added later will make those defs not dominate
7342 : their uses.
7343 : ??? Technically this applies only to the registers used in the
7344 : resulting non-register *TO_P. */
7345 48454035 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
7346 48454035 : if (saved_into_ssa
7347 46060343 : && TREE_CODE (*from_p) == CALL_EXPR
7348 53678318 : && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
7349 1539 : gimplify_ctxp->into_ssa = false;
7350 48454035 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7351 48454035 : gimplify_ctxp->into_ssa = saved_into_ssa;
7352 48454035 : if (ret == GS_ERROR)
7353 : return ret;
7354 :
7355 : /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
7356 : guess for the predicate was wrong. */
7357 48453960 : gimple_predicate final_pred = rhs_predicate_for (*to_p);
7358 48453960 : if (final_pred != initial_pred)
7359 : {
7360 8236259 : ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
7361 8236259 : if (ret == GS_ERROR)
7362 : return ret;
7363 : }
7364 :
7365 : /* In case of va_arg internal fn wrapped in a WITH_SIZE_EXPR, add the type
7366 : size as argument to the call. */
7367 48453960 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7368 : {
7369 1457 : tree call = TREE_OPERAND (*from_p, 0);
7370 1457 : tree vlasize = TREE_OPERAND (*from_p, 1);
7371 :
7372 1457 : if (TREE_CODE (call) == CALL_EXPR
7373 1457 : && CALL_EXPR_IFN (call) == IFN_VA_ARG)
7374 : {
7375 95 : int nargs = call_expr_nargs (call);
7376 95 : tree type = TREE_TYPE (call);
7377 95 : tree ap = CALL_EXPR_ARG (call, 0);
7378 95 : tree tag = CALL_EXPR_ARG (call, 1);
7379 95 : tree aptag = CALL_EXPR_ARG (call, 2);
7380 95 : tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
7381 : IFN_VA_ARG, type,
7382 : nargs + 1, ap, tag,
7383 : aptag, vlasize);
7384 95 : TREE_OPERAND (*from_p, 0) = newcall;
7385 : }
7386 : }
7387 :
7388 : /* Now see if the above changed *from_p to something we handle specially. */
7389 48453960 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7390 : want_value);
7391 48453960 : if (ret != GS_UNHANDLED)
7392 : return ret;
7393 :
7394 : /* If we've got a variable sized assignment between two lvalues (i.e. does
7395 : not involve a call), then we can make things a bit more straightforward
7396 : by converting the assignment to memcpy or memset. */
7397 48452997 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7398 : {
7399 1107 : tree from = TREE_OPERAND (*from_p, 0);
7400 1107 : tree size = TREE_OPERAND (*from_p, 1);
7401 :
7402 1107 : if (TREE_CODE (from) == CONSTRUCTOR)
7403 0 : return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
7404 1107 : else if (is_gimple_addressable (from)
7405 1107 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (*to_p)))
7406 2213 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))))
7407 : {
7408 1106 : *from_p = from;
7409 1106 : return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
7410 1106 : pre_p);
7411 : }
7412 : }
7413 :
7414 : /* Transform partial stores to non-addressable complex variables into
7415 : total stores. This allows us to use real instead of virtual operands
7416 : for these variables, which improves optimization. */
7417 48451891 : if ((TREE_CODE (*to_p) == REALPART_EXPR
7418 48451891 : || TREE_CODE (*to_p) == IMAGPART_EXPR)
7419 48451891 : && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
7420 1942 : return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
7421 :
7422 : /* Try to alleviate the effects of the gimplification creating artificial
7423 : temporaries (see for example is_gimple_reg_rhs) on the debug info, but
7424 : make sure not to create DECL_DEBUG_EXPR links across functions. */
7425 48449949 : if (!gimplify_ctxp->into_ssa
7426 2392766 : && VAR_P (*from_p)
7427 472296 : && DECL_IGNORED_P (*from_p)
7428 351216 : && DECL_P (*to_p)
7429 138540 : && !DECL_IGNORED_P (*to_p)
7430 38205 : && decl_function_context (*to_p) == current_function_decl
7431 48482574 : && decl_function_context (*from_p) == current_function_decl)
7432 : {
7433 32620 : if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
7434 27329 : DECL_NAME (*from_p)
7435 54658 : = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
7436 32620 : DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
7437 32620 : SET_DECL_DEBUG_EXPR (*from_p, *to_p);
7438 : }
7439 :
7440 48449949 : if (want_value && TREE_THIS_VOLATILE (*to_p))
7441 526 : *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
7442 :
7443 48449949 : if (TREE_CODE (*from_p) == CALL_EXPR)
7444 : {
7445 : /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
7446 : instead of a GIMPLE_ASSIGN. */
7447 4902869 : gcall *call_stmt;
7448 4902869 : if (CALL_EXPR_FN (*from_p) == NULL_TREE)
7449 : {
7450 : /* Gimplify internal functions created in the FEs. */
7451 343245 : int nargs = call_expr_nargs (*from_p), i;
7452 343245 : enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
7453 343245 : auto_vec<tree> vargs (nargs);
7454 :
7455 1727844 : for (i = 0; i < nargs; i++)
7456 : {
7457 1041354 : gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
7458 1041354 : EXPR_LOCATION (*from_p));
7459 1041354 : vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
7460 : }
7461 343245 : call_stmt = gimple_build_call_internal_vec (ifn, vargs);
7462 343245 : gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
7463 686490 : gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
7464 343245 : }
7465 : else
7466 : {
7467 4559624 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
7468 4559624 : CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
7469 4559624 : STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
7470 4559624 : tree fndecl = get_callee_fndecl (*from_p);
7471 4559624 : if (fndecl
7472 4444301 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
7473 4697989 : && call_expr_nargs (*from_p) == 3)
7474 67687 : call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
7475 67687 : CALL_EXPR_ARG (*from_p, 0),
7476 67687 : CALL_EXPR_ARG (*from_p, 1),
7477 67687 : CALL_EXPR_ARG (*from_p, 2));
7478 : else
7479 : {
7480 4491937 : call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
7481 : }
7482 : }
7483 4902869 : notice_special_calls (call_stmt);
7484 4902869 : if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
7485 4902625 : gimple_call_set_lhs (call_stmt, *to_p);
7486 244 : else if (TREE_CODE (*to_p) == SSA_NAME)
7487 : /* The above is somewhat premature, avoid ICEing later for a
7488 : SSA name w/o a definition. We may have uses in the GIMPLE IL.
7489 : ??? This doesn't make it a default-def. */
7490 137 : SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
7491 :
7492 : assign = call_stmt;
7493 : }
7494 : else
7495 : {
7496 43547080 : assign = gimple_build_assign (*to_p, *from_p);
7497 43547080 : gimple_set_location (assign, EXPR_LOCATION (*expr_p));
7498 43547080 : if (COMPARISON_CLASS_P (*from_p))
7499 1261764 : copy_warning (assign, *from_p);
7500 : }
7501 :
7502 48449949 : if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
7503 : {
7504 : /* We should have got an SSA name from the start. */
7505 35853370 : gcc_assert (TREE_CODE (*to_p) == SSA_NAME
7506 : || ! gimple_in_ssa_p (cfun));
7507 : }
7508 :
7509 48449949 : gimplify_seq_add_stmt (pre_p, assign);
7510 48449949 : gsi = gsi_last (*pre_p);
7511 48449949 : maybe_fold_stmt (&gsi);
7512 :
7513 48449949 : if (want_value)
7514 : {
7515 698903 : *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
7516 698903 : return GS_OK;
7517 : }
7518 : else
7519 47751046 : *expr_p = NULL;
7520 :
7521 47751046 : return GS_ALL_DONE;
7522 : }
7523 :
7524 : /* Gimplify a comparison between two variable-sized objects. Do this
7525 : with a call to BUILT_IN_MEMCMP. */
7526 :
7527 : static enum gimplify_status
7528 0 : gimplify_variable_sized_compare (tree *expr_p)
7529 : {
7530 0 : location_t loc = EXPR_LOCATION (*expr_p);
7531 0 : tree op0 = TREE_OPERAND (*expr_p, 0);
7532 0 : tree op1 = TREE_OPERAND (*expr_p, 1);
7533 0 : tree t, arg, dest, src, expr;
7534 :
7535 0 : arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
7536 0 : arg = unshare_expr (arg);
7537 0 : arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
7538 0 : src = build_fold_addr_expr_loc (loc, op1);
7539 0 : dest = build_fold_addr_expr_loc (loc, op0);
7540 0 : t = builtin_decl_implicit (BUILT_IN_MEMCMP);
7541 0 : t = build_call_expr_loc (loc, t, 3, dest, src, arg);
7542 :
7543 0 : expr
7544 0 : = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
7545 0 : SET_EXPR_LOCATION (expr, loc);
7546 0 : *expr_p = expr;
7547 :
7548 0 : return GS_OK;
7549 : }
7550 :
7551 : /* Gimplify a comparison between two aggregate objects of integral scalar
7552 : mode as a comparison between the bitwise equivalent scalar values. */
7553 :
7554 : static enum gimplify_status
7555 16 : gimplify_scalar_mode_aggregate_compare (tree *expr_p)
7556 : {
7557 16 : const location_t loc = EXPR_LOCATION (*expr_p);
7558 16 : const enum tree_code code = TREE_CODE (*expr_p);
7559 16 : tree op0 = TREE_OPERAND (*expr_p, 0);
7560 16 : tree op1 = TREE_OPERAND (*expr_p, 1);
7561 16 : tree type = TREE_TYPE (op0);
7562 16 : tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
7563 :
7564 16 : op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
7565 16 : op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
7566 :
7567 : /* We need to perform ordering comparisons in memory order like memcmp and,
7568 : therefore, may need to byte-swap operands for little-endian targets. */
7569 16 : if (code != EQ_EXPR && code != NE_EXPR)
7570 : {
7571 0 : gcc_assert (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN);
7572 0 : gcc_assert (TREE_CODE (scalar_type) == INTEGER_TYPE);
7573 0 : tree fndecl;
7574 :
7575 0 : if (BYTES_BIG_ENDIAN)
7576 : fndecl = NULL_TREE;
7577 : else
7578 0 : switch (int_size_in_bytes (scalar_type))
7579 : {
7580 : case 1:
7581 : fndecl = NULL_TREE;
7582 : break;
7583 0 : case 2:
7584 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP16);
7585 : break;
7586 0 : case 4:
7587 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP32);
7588 : break;
7589 0 : case 8:
7590 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP64);
7591 : break;
7592 0 : case 16:
7593 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP128);
7594 : break;
7595 0 : default:
7596 0 : gcc_unreachable ();
7597 : }
7598 :
7599 0 : if (fndecl)
7600 : {
7601 0 : op0 = build_call_expr_loc (loc, fndecl, 1, op0);
7602 0 : op1 = build_call_expr_loc (loc, fndecl, 1, op1);
7603 : }
7604 : }
7605 :
7606 16 : *expr_p = fold_build2_loc (loc, code, TREE_TYPE (*expr_p), op0, op1);
7607 :
7608 16 : return GS_OK;
7609 : }
7610 :
7611 : /* Gimplify an expression sequence. This function gimplifies each
7612 : expression and rewrites the original expression with the last
7613 : expression of the sequence in GIMPLE form.
7614 :
7615 : PRE_P points to the list where the side effects for all the
7616 : expressions in the sequence will be emitted.
7617 :
7618 : WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
7619 :
7620 : static enum gimplify_status
7621 1121112 : gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
7622 : {
7623 1121112 : tree t = *expr_p;
7624 :
7625 1211638 : do
7626 : {
7627 1211638 : tree *sub_p = &TREE_OPERAND (t, 0);
7628 :
7629 1211638 : if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
7630 209257 : gimplify_compound_expr (sub_p, pre_p, false);
7631 : else
7632 1002381 : gimplify_stmt (sub_p, pre_p);
7633 :
7634 1211638 : t = TREE_OPERAND (t, 1);
7635 : }
7636 1211638 : while (TREE_CODE (t) == COMPOUND_EXPR);
7637 :
7638 1121112 : *expr_p = t;
7639 1121112 : if (want_value)
7640 : return GS_OK;
7641 : else
7642 : {
7643 620168 : gimplify_stmt (expr_p, pre_p);
7644 620168 : return GS_ALL_DONE;
7645 : }
7646 : }
7647 :
7648 : /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
7649 : gimplify. After gimplification, EXPR_P will point to a new temporary
7650 : that holds the original value of the SAVE_EXPR node.
7651 :
7652 : PRE_P points to the list where side effects that must happen before
7653 : *EXPR_P should be stored. */
7654 :
7655 : static enum gimplify_status
7656 443944 : gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7657 : {
7658 443944 : enum gimplify_status ret = GS_ALL_DONE;
7659 443944 : tree val;
7660 :
7661 443944 : gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
7662 443944 : val = TREE_OPERAND (*expr_p, 0);
7663 :
7664 443944 : if (val && TREE_TYPE (val) == error_mark_node)
7665 : return GS_ERROR;
7666 :
7667 : /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
7668 443942 : if (!SAVE_EXPR_RESOLVED_P (*expr_p))
7669 : {
7670 : /* The operand may be a void-valued expression. It is
7671 : being executed only for its side-effects. */
7672 170572 : if (TREE_TYPE (val) == void_type_node)
7673 : {
7674 751 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7675 : is_gimple_stmt, fb_none);
7676 751 : val = NULL;
7677 : }
7678 : else
7679 : /* The temporary may not be an SSA name as later abnormal and EH
7680 : control flow may invalidate use/def domination. When in SSA
7681 : form then assume there are no such issues and SAVE_EXPRs only
7682 : appear via GENERIC foldings. */
7683 339642 : val = get_initialized_tmp_var (val, pre_p, post_p,
7684 : gimple_in_ssa_p (cfun));
7685 :
7686 170572 : TREE_OPERAND (*expr_p, 0) = val;
7687 170572 : SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
7688 : }
7689 :
7690 443942 : *expr_p = val;
7691 :
7692 443942 : return ret;
7693 : }
7694 :
7695 : /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
7696 :
7697 : unary_expr
7698 : : ...
7699 : | '&' varname
7700 : ...
7701 :
7702 : PRE_P points to the list where side effects that must happen before
7703 : *EXPR_P should be stored.
7704 :
7705 : POST_P points to the list where side effects that must happen after
7706 : *EXPR_P should be stored. */
7707 :
7708 : static enum gimplify_status
7709 33941928 : gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7710 : {
7711 33941928 : tree expr = *expr_p;
7712 33941928 : tree op0 = TREE_OPERAND (expr, 0);
7713 33941928 : enum gimplify_status ret;
7714 33941928 : location_t loc = EXPR_LOCATION (*expr_p);
7715 :
7716 33941928 : switch (TREE_CODE (op0))
7717 : {
7718 98754 : case INDIRECT_REF:
7719 98754 : do_indirect_ref:
7720 : /* Check if we are dealing with an expression of the form '&*ptr'.
7721 : While the front end folds away '&*ptr' into 'ptr', these
7722 : expressions may be generated internally by the compiler (e.g.,
7723 : builtins like __builtin_va_end). */
7724 : /* Caution: the silent array decomposition semantics we allow for
7725 : ADDR_EXPR means we can't always discard the pair. */
7726 : /* Gimplification of the ADDR_EXPR operand may drop
7727 : cv-qualification conversions, so make sure we add them if
7728 : needed. */
7729 98754 : {
7730 98754 : tree op00 = TREE_OPERAND (op0, 0);
7731 98754 : tree t_expr = TREE_TYPE (expr);
7732 98754 : tree t_op00 = TREE_TYPE (op00);
7733 :
7734 98754 : if (!useless_type_conversion_p (t_expr, t_op00))
7735 0 : op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
7736 98754 : *expr_p = op00;
7737 98754 : ret = GS_OK;
7738 : }
7739 98754 : break;
7740 :
7741 0 : case VIEW_CONVERT_EXPR:
7742 : /* Take the address of our operand and then convert it to the type of
7743 : this ADDR_EXPR.
7744 :
7745 : ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
7746 : all clear. The impact of this transformation is even less clear. */
7747 :
7748 : /* If the operand is a useless conversion, look through it. Doing so
7749 : guarantees that the ADDR_EXPR and its operand will remain of the
7750 : same type. */
7751 0 : if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
7752 0 : op0 = TREE_OPERAND (op0, 0);
7753 :
7754 0 : *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
7755 : build_fold_addr_expr_loc (loc,
7756 0 : TREE_OPERAND (op0, 0)));
7757 0 : ret = GS_OK;
7758 0 : break;
7759 :
7760 68301 : case MEM_REF:
7761 68301 : if (integer_zerop (TREE_OPERAND (op0, 1)))
7762 38908 : goto do_indirect_ref;
7763 :
7764 : /* fall through */
7765 :
7766 33860614 : default:
7767 : /* If we see a call to a declared builtin or see its address
7768 : being taken (we can unify those cases here) then we can mark
7769 : the builtin for implicit generation by GCC. */
7770 33860614 : if (TREE_CODE (op0) == FUNCTION_DECL
7771 16859339 : && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
7772 37835875 : && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
7773 1300333 : set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
7774 :
7775 : /* We use fb_either here because the C frontend sometimes takes
7776 : the address of a call that returns a struct; see
7777 : gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
7778 : the implied temporary explicit. */
7779 :
7780 : /* Make the operand addressable. */
7781 33860614 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
7782 : is_gimple_addressable, fb_either);
7783 33860614 : if (ret == GS_ERROR)
7784 : break;
7785 :
7786 : /* Then mark it. Beware that it may not be possible to do so directly
7787 : if a temporary has been created by the gimplification. */
7788 33860610 : prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
7789 :
7790 33860610 : op0 = TREE_OPERAND (expr, 0);
7791 :
7792 : /* For various reasons, the gimplification of the expression
7793 : may have made a new INDIRECT_REF. */
7794 33860610 : if (INDIRECT_REF_P (op0)
7795 33860610 : || (TREE_CODE (op0) == MEM_REF
7796 46833 : && integer_zerop (TREE_OPERAND (op0, 1))))
7797 17440 : goto do_indirect_ref;
7798 :
7799 33843170 : mark_addressable (TREE_OPERAND (expr, 0));
7800 :
7801 : /* The FEs may end up building ADDR_EXPRs early on a decl with
7802 : an incomplete type. Re-build ADDR_EXPRs in canonical form
7803 : here. */
7804 33843170 : if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
7805 150010 : *expr_p = build_fold_addr_expr (op0);
7806 :
7807 : /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
7808 33843170 : if (TREE_CODE (*expr_p) == ADDR_EXPR)
7809 33843170 : recompute_tree_invariant_for_addr_expr (*expr_p);
7810 :
7811 : /* If we re-built the ADDR_EXPR add a conversion to the original type
7812 : if required. */
7813 33843170 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
7814 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
7815 :
7816 : break;
7817 : }
7818 :
7819 33941928 : return ret;
7820 : }
7821 :
7822 : /* Return the number of times character C occurs in string S. */
7823 :
7824 : static int
7825 94843 : num_occurrences (int c, const char *s)
7826 : {
7827 94843 : int n = 0;
7828 257183 : while (*s)
7829 162340 : n += (*s++ == c);
7830 94843 : return n;
7831 : }
7832 :
7833 : /* A subroutine of gimplify_asm_expr. Check that all operands have
7834 : the same number of alternatives. Return -1 if this is violated. Otherwise
7835 : return the number of alternatives. */
7836 :
7837 : static int
7838 166028 : num_alternatives (const_tree link)
7839 : {
7840 166028 : if (link == nullptr)
7841 : return 0;
7842 :
7843 48771 : const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7844 48771 : int num = num_occurrences (',', constraint);
7845 :
7846 48771 : if (num + 1 > MAX_RECOG_ALTERNATIVES)
7847 : return -1;
7848 :
7849 94843 : for (link = TREE_CHAIN (link); link; link = TREE_CHAIN (link))
7850 : {
7851 46072 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7852 92144 : if (num_occurrences (',', constraint) != num)
7853 : return -1;
7854 : }
7855 48771 : return num + 1;
7856 : }
7857 :
7858 : /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
7859 : value; output operands should be a gimple lvalue. */
7860 :
7861 : static enum gimplify_status
7862 83014 : gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7863 : {
7864 83014 : tree expr;
7865 83014 : int noutputs;
7866 83014 : const char **oconstraints;
7867 83014 : int i;
7868 83014 : tree link;
7869 83014 : const char *constraint;
7870 83014 : bool allows_mem, allows_reg, is_inout;
7871 83014 : enum gimplify_status ret, tret;
7872 83014 : gasm *stmt;
7873 83014 : vec<tree, va_gc> *inputs;
7874 83014 : vec<tree, va_gc> *outputs;
7875 83014 : vec<tree, va_gc> *clobbers;
7876 83014 : vec<tree, va_gc> *labels;
7877 83014 : tree link_next;
7878 :
7879 83014 : expr = *expr_p;
7880 83014 : noutputs = list_length (ASM_OUTPUTS (expr));
7881 83014 : oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
7882 :
7883 83014 : inputs = NULL;
7884 83014 : outputs = NULL;
7885 83014 : clobbers = NULL;
7886 83014 : labels = NULL;
7887 :
7888 83014 : int num_alternatives_out = num_alternatives (ASM_OUTPUTS (expr));
7889 83014 : int num_alternatives_in = num_alternatives (ASM_INPUTS (expr));
7890 83014 : if (num_alternatives_out == -1 || num_alternatives_in == -1
7891 83014 : || (num_alternatives_out > 0 && num_alternatives_in > 0
7892 17263 : && num_alternatives_out != num_alternatives_in))
7893 : {
7894 0 : error ("operand constraints for %<asm%> differ "
7895 : "in number of alternatives");
7896 0 : return GS_ERROR;
7897 : }
7898 83014 : int num_alternatives = MAX (num_alternatives_out, num_alternatives_in);
7899 :
7900 83014 : gimplify_reg_info reg_info (num_alternatives, noutputs);
7901 :
7902 83014 : link_next = NULL_TREE;
7903 163229 : for (link = ASM_CLOBBERS (expr); link; link = link_next)
7904 : {
7905 : /* The clobber entry could also be an error marker. */
7906 80215 : if (TREE_CODE (TREE_VALUE (link)) == STRING_CST)
7907 : {
7908 80192 : const char *regname= TREE_STRING_POINTER (TREE_VALUE (link));
7909 80192 : int regno = decode_reg_name (regname);
7910 80192 : if (regno >= 0)
7911 41262 : reg_info.set_clobbered (regno);
7912 : }
7913 80215 : link_next = TREE_CHAIN (link);
7914 80215 : TREE_CHAIN (link) = NULL_TREE;
7915 80215 : vec_safe_push (clobbers, link);
7916 : }
7917 :
7918 83014 : ret = GS_ALL_DONE;
7919 83014 : link_next = NULL_TREE;
7920 144190 : for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
7921 : {
7922 61209 : bool ok;
7923 61209 : size_t constraint_len;
7924 :
7925 61209 : if (error_operand_p (TREE_VALUE (link)))
7926 : return GS_ERROR;
7927 61188 : link_next = TREE_CHAIN (link);
7928 :
7929 122376 : oconstraints[i]
7930 61188 : = constraint
7931 61188 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7932 61188 : constraint_len = strlen (constraint);
7933 61188 : if (constraint_len == 0)
7934 0 : continue;
7935 :
7936 61188 : reg_info.operand = TREE_VALUE (link);
7937 61188 : ok = parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
7938 : &allows_reg, &is_inout, ®_info);
7939 61188 : if (!ok)
7940 : {
7941 11 : ret = GS_ERROR;
7942 11 : is_inout = false;
7943 : }
7944 :
7945 : /* If we can't make copies, we can only accept memory.
7946 : Similarly for VLAs. */
7947 61188 : tree outtype = TREE_TYPE (TREE_VALUE (link));
7948 61188 : if (TREE_ADDRESSABLE (outtype)
7949 61165 : || !COMPLETE_TYPE_P (outtype)
7950 122312 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype)))
7951 : {
7952 288 : if (allows_mem)
7953 276 : allows_reg = 0;
7954 : else
7955 : {
7956 12 : error ("impossible constraint in %<asm%>");
7957 12 : error ("non-memory output %d must stay in memory", i);
7958 12 : return GS_ERROR;
7959 : }
7960 : }
7961 :
7962 61176 : if (!allows_reg && allows_mem)
7963 2078 : mark_addressable (TREE_VALUE (link));
7964 :
7965 61176 : tree orig = TREE_VALUE (link);
7966 113635 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
7967 : is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7968 : fb_lvalue | fb_mayfail);
7969 61176 : if (tret == GS_ERROR)
7970 : {
7971 0 : if (orig != error_mark_node)
7972 0 : error ("invalid lvalue in %<asm%> output %d", i);
7973 : ret = tret;
7974 : }
7975 :
7976 : /* If the gimplified operand is a register we do not allow memory. */
7977 61176 : if (allows_reg
7978 59095 : && allows_mem
7979 67371 : && (is_gimple_reg (TREE_VALUE (link))
7980 267 : || (handled_component_p (TREE_VALUE (link))
7981 36 : && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
7982 5929 : allows_mem = 0;
7983 :
7984 : /* If the constraint does not allow memory make sure we gimplify
7985 : it to a register if it is not already but its base is. This
7986 : happens for complex and vector components. */
7987 61176 : if (!allows_mem)
7988 : {
7989 58832 : tree op = TREE_VALUE (link);
7990 58832 : if (! is_gimple_val (op)
7991 21367 : && is_gimple_reg_type (TREE_TYPE (op))
7992 80198 : && is_gimple_reg (get_base_address (op)))
7993 : {
7994 19 : tree tem = create_tmp_reg (TREE_TYPE (op));
7995 19 : tree ass;
7996 19 : if (is_inout)
7997 : {
7998 7 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
7999 : tem, unshare_expr (op));
8000 7 : gimplify_and_add (ass, pre_p);
8001 : }
8002 19 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
8003 19 : gimplify_and_add (ass, post_p);
8004 :
8005 19 : TREE_VALUE (link) = tem;
8006 19 : tret = GS_OK;
8007 : }
8008 : }
8009 :
8010 61176 : vec_safe_push (outputs, link);
8011 61176 : TREE_CHAIN (link) = NULL_TREE;
8012 :
8013 61176 : if (is_inout)
8014 : {
8015 : /* An input/output operand. To give the optimizers more
8016 : flexibility, split it into separate input and output
8017 : operands. */
8018 8717 : tree input;
8019 : /* Buffer big enough to format a 32-bit UINT_MAX into. */
8020 8717 : char buf[11];
8021 :
8022 : /* Turn the in/out constraint into an output constraint. */
8023 8717 : char *p = xstrdup (constraint);
8024 8717 : p[0] = '=';
8025 8717 : TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
8026 8717 : TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
8027 :
8028 : /* And add a matching input constraint. */
8029 8717 : if (allows_reg)
8030 : {
8031 8216 : sprintf (buf, "%u", i);
8032 :
8033 : /* If there are multiple alternatives in the constraint,
8034 : handle each of them individually. Those that allow register
8035 : will be replaced with operand number, the others will stay
8036 : unchanged. */
8037 8216 : if (strchr (p, ',') != NULL)
8038 : {
8039 100 : size_t len = 0, buflen = strlen (buf);
8040 100 : char *beg, *end, *str, *dst;
8041 :
8042 100 : for (beg = p + 1;;)
8043 : {
8044 200 : end = strchr (beg, ',');
8045 200 : if (end == NULL)
8046 100 : end = strchr (beg, '\0');
8047 200 : if ((size_t) (end - beg) < buflen)
8048 28 : len += buflen + 1;
8049 : else
8050 172 : len += end - beg + 1;
8051 200 : if (*end)
8052 100 : beg = end + 1;
8053 : else
8054 : break;
8055 : }
8056 :
8057 100 : str = (char *) alloca (len);
8058 100 : for (beg = p + 1, dst = str;;)
8059 : {
8060 200 : const char *tem;
8061 200 : bool mem_p, reg_p, inout_p;
8062 :
8063 200 : end = strchr (beg, ',');
8064 200 : if (end)
8065 100 : *end = '\0';
8066 200 : beg[-1] = '=';
8067 200 : tem = beg - 1;
8068 200 : parse_output_constraint (&tem, i, 0, 0, &mem_p, ®_p,
8069 : &inout_p, nullptr);
8070 200 : if (dst != str)
8071 100 : *dst++ = ',';
8072 200 : if (reg_p)
8073 : {
8074 115 : memcpy (dst, buf, buflen);
8075 115 : dst += buflen;
8076 : }
8077 : else
8078 : {
8079 85 : if (end)
8080 0 : len = end - beg;
8081 : else
8082 85 : len = strlen (beg);
8083 85 : memcpy (dst, beg, len);
8084 85 : dst += len;
8085 : }
8086 200 : if (end)
8087 100 : beg = end + 1;
8088 : else
8089 : break;
8090 100 : }
8091 100 : *dst = '\0';
8092 100 : input = build_string (dst - str, str);
8093 : }
8094 : else
8095 8116 : input = build_string (strlen (buf), buf);
8096 : }
8097 : else
8098 501 : input = build_string (constraint_len - 1, constraint + 1);
8099 :
8100 8717 : free (p);
8101 :
8102 8717 : input = build_tree_list (build_tree_list (NULL_TREE, input),
8103 8717 : unshare_expr (TREE_VALUE (link)));
8104 8717 : ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
8105 : }
8106 : }
8107 :
8108 : /* After all output operands have been gimplified, verify that each output
8109 : operand is used at most once in case of hard register constraints. Thus,
8110 : error out in cases like
8111 : asm ("" : "={0}" (x), "={1}" (x));
8112 : or even for
8113 : asm ("" : "=r" (x), "={1}" (x));
8114 :
8115 : FIXME: Ideally we would also error out for cases like
8116 : int x;
8117 : asm ("" : "=r" (x), "=r" (x));
8118 : However, since code like that was previously accepted, erroring out now might
8119 : break existing code. On the other hand, we already error out for register
8120 : asm like
8121 : register int x asm ("0");
8122 : asm ("" : "=r" (x), "=r" (x));
8123 : Thus, maybe it wouldn't be too bad to also error out in the former
8124 : non-register-asm case.
8125 : */
8126 144153 : for (unsigned i = 0; i < vec_safe_length (outputs); ++i)
8127 : {
8128 61174 : tree link = (*outputs)[i];
8129 61174 : tree op1 = TREE_VALUE (link);
8130 61174 : const char *constraint
8131 61174 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8132 61174 : if (strchr (constraint, '{') != nullptr)
8133 61314 : for (unsigned j = 0; j < vec_safe_length (outputs); ++j)
8134 : {
8135 142 : if (i == j)
8136 95 : continue;
8137 47 : tree link2 = (*outputs)[j];
8138 47 : tree op2 = TREE_VALUE (link2);
8139 47 : if (op1 == op2)
8140 : {
8141 2 : error ("multiple outputs to lvalue %qE", op2);
8142 2 : return GS_ERROR;
8143 : }
8144 : }
8145 : }
8146 :
8147 82979 : link_next = NULL_TREE;
8148 82979 : int input_num = 0;
8149 125268 : for (link = ASM_INPUTS (expr); link; ++input_num, ++i, link = link_next)
8150 : {
8151 42347 : if (error_operand_p (TREE_VALUE (link)))
8152 : return GS_ERROR;
8153 42298 : link_next = TREE_CHAIN (link);
8154 42298 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8155 42298 : reg_info.operand = TREE_VALUE (link);
8156 42298 : bool ok = parse_input_constraint (&constraint, input_num, 0, noutputs, 0,
8157 : oconstraints, &allows_mem, &allows_reg,
8158 : ®_info);
8159 42298 : if (!ok)
8160 : {
8161 30 : ret = GS_ERROR;
8162 30 : is_inout = false;
8163 : }
8164 :
8165 : /* If we can't make copies, we can only accept memory. */
8166 42298 : tree intype = TREE_TYPE (TREE_VALUE (link));
8167 42298 : if (TREE_ADDRESSABLE (intype)
8168 42272 : || !COMPLETE_TYPE_P (intype)
8169 84515 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype)))
8170 : {
8171 297 : if (allows_mem)
8172 288 : allows_reg = 0;
8173 : else
8174 : {
8175 9 : error ("impossible constraint in %<asm%>");
8176 9 : error ("non-memory input %d must stay in memory", i);
8177 9 : return GS_ERROR;
8178 : }
8179 : }
8180 :
8181 : /* If the operand is a memory input, it should be an lvalue. */
8182 42289 : if (!allows_reg && allows_mem)
8183 : {
8184 2394 : tree inputv = TREE_VALUE (link);
8185 2394 : STRIP_NOPS (inputv);
8186 2394 : if (TREE_CODE (inputv) == PREDECREMENT_EXPR
8187 : || TREE_CODE (inputv) == PREINCREMENT_EXPR
8188 : || TREE_CODE (inputv) == POSTDECREMENT_EXPR
8189 2394 : || TREE_CODE (inputv) == POSTINCREMENT_EXPR
8190 2382 : || TREE_CODE (inputv) == MODIFY_EXPR
8191 4774 : || VOID_TYPE_P (TREE_TYPE (inputv)))
8192 38 : TREE_VALUE (link) = error_mark_node;
8193 2394 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8194 : is_gimple_lvalue, fb_lvalue | fb_mayfail);
8195 2394 : if (tret != GS_ERROR)
8196 : {
8197 : /* Unlike output operands, memory inputs are not guaranteed
8198 : to be lvalues by the FE, and while the expressions are
8199 : marked addressable there, if it is e.g. a statement
8200 : expression, temporaries in it might not end up being
8201 : addressable. They might be already used in the IL and thus
8202 : it is too late to make them addressable now though. */
8203 2348 : tree x = TREE_VALUE (link);
8204 2490 : while (handled_component_p (x))
8205 142 : x = TREE_OPERAND (x, 0);
8206 2348 : if (TREE_CODE (x) == MEM_REF
8207 2348 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
8208 0 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
8209 2348 : if ((VAR_P (x)
8210 : || TREE_CODE (x) == PARM_DECL
8211 : || TREE_CODE (x) == RESULT_DECL)
8212 1491 : && !TREE_ADDRESSABLE (x)
8213 23 : && is_gimple_reg (x))
8214 : {
8215 17 : warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
8216 17 : input_location), 0,
8217 : "memory input %d is not directly addressable",
8218 : i);
8219 17 : prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
8220 : }
8221 : }
8222 2394 : mark_addressable (TREE_VALUE (link));
8223 2394 : if (tret == GS_ERROR)
8224 : {
8225 46 : if (inputv != error_mark_node)
8226 46 : error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
8227 : "memory input %d is not directly addressable", i);
8228 : ret = tret;
8229 : }
8230 : }
8231 : else
8232 : {
8233 39895 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8234 : is_gimple_asm_val, fb_rvalue);
8235 39895 : if (tret == GS_ERROR)
8236 46 : ret = tret;
8237 : }
8238 :
8239 42289 : TREE_CHAIN (link) = NULL_TREE;
8240 42289 : vec_safe_push (inputs, link);
8241 : }
8242 :
8243 82921 : link_next = NULL_TREE;
8244 83868 : for (link = ASM_LABELS (expr); link; link = link_next)
8245 : {
8246 947 : link_next = TREE_CHAIN (link);
8247 947 : TREE_CHAIN (link) = NULL_TREE;
8248 947 : vec_safe_push (labels, link);
8249 : }
8250 :
8251 : /* Do not add ASMs with errors to the gimple IL stream. */
8252 82921 : if (ret != GS_ERROR)
8253 : {
8254 82838 : stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
8255 : inputs, outputs, clobbers, labels);
8256 :
8257 : /* asm is volatile if it was marked by the user as volatile or
8258 : there are no outputs or this is an asm goto. */
8259 90356 : gimple_asm_set_volatile (stmt,
8260 82838 : ASM_VOLATILE_P (expr)
8261 7532 : || noutputs == 0
8262 : || labels);
8263 82838 : gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
8264 82838 : gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
8265 :
8266 82838 : gimplify_seq_add_stmt (pre_p, stmt);
8267 : }
8268 :
8269 : return ret;
8270 83014 : }
8271 :
8272 : /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
8273 : GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
8274 : gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
8275 : return to this function.
8276 :
8277 : FIXME should we complexify the prequeue handling instead? Or use flags
8278 : for all the cleanups and let the optimizer tighten them up? The current
8279 : code seems pretty fragile; it will break on a cleanup within any
8280 : non-conditional nesting. But any such nesting would be broken, anyway;
8281 : we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
8282 : and continues out of it. We can do that at the RTL level, though, so
8283 : having an optimizer to tighten up try/finally regions would be a Good
8284 : Thing. */
8285 :
8286 : static enum gimplify_status
8287 5441301 : gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
8288 : {
8289 5441301 : gimple_stmt_iterator iter;
8290 5441301 : gimple_seq body_sequence = NULL;
8291 :
8292 5441301 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
8293 :
8294 : /* We only care about the number of conditions between the innermost
8295 : CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
8296 : any cleanups collected outside the CLEANUP_POINT_EXPR. */
8297 5441301 : int old_conds = gimplify_ctxp->conditions;
8298 5441301 : gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
8299 5441301 : bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
8300 5441301 : gimplify_ctxp->conditions = 0;
8301 5441301 : gimplify_ctxp->conditional_cleanups = NULL;
8302 5441301 : gimplify_ctxp->in_cleanup_point_expr = true;
8303 :
8304 5441301 : gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
8305 :
8306 5441301 : gimplify_ctxp->conditions = old_conds;
8307 5441301 : gimplify_ctxp->conditional_cleanups = old_cleanups;
8308 5441301 : gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
8309 :
8310 28167006 : for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
8311 : {
8312 17330859 : gimple *wce = gsi_stmt (iter);
8313 :
8314 17330859 : if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
8315 : {
8316 653804 : if (gsi_one_before_end_p (iter))
8317 : {
8318 : /* Note that gsi_insert_seq_before and gsi_remove do not
8319 : scan operands, unlike some other sequence mutators. */
8320 3099 : if (!gimple_wce_cleanup_eh_only (wce))
8321 3099 : gsi_insert_seq_before_without_update (&iter,
8322 : gimple_wce_cleanup (wce),
8323 : GSI_SAME_STMT);
8324 3099 : gsi_remove (&iter, true);
8325 3099 : break;
8326 : }
8327 : else
8328 : {
8329 650705 : gtry *gtry;
8330 650705 : gimple_seq seq;
8331 650705 : enum gimple_try_flags kind;
8332 :
8333 650705 : if (gimple_wce_cleanup_eh_only (wce))
8334 : kind = GIMPLE_TRY_CATCH;
8335 : else
8336 635779 : kind = GIMPLE_TRY_FINALLY;
8337 650705 : seq = gsi_split_seq_after (iter);
8338 :
8339 650705 : gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
8340 : /* Do not use gsi_replace here, as it may scan operands.
8341 : We want to do a simple structural modification only. */
8342 650705 : gsi_set_stmt (&iter, gtry);
8343 1301410 : iter = gsi_start (gtry->eval);
8344 : }
8345 : }
8346 : else
8347 16677055 : gsi_next (&iter);
8348 : }
8349 :
8350 5441301 : gimplify_seq_add_seq (pre_p, body_sequence);
8351 5441301 : if (temp)
8352 : {
8353 : *expr_p = temp;
8354 : return GS_OK;
8355 : }
8356 : else
8357 : {
8358 5082830 : *expr_p = NULL;
8359 5082830 : return GS_ALL_DONE;
8360 : }
8361 : }
8362 :
8363 : /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
8364 : is the cleanup action required. EH_ONLY is true if the cleanup should
8365 : only be executed if an exception is thrown, not on normal exit.
8366 : If FORCE_UNCOND is true perform the cleanup unconditionally; this is
8367 : only valid for clobbers. */
8368 :
8369 : static void
8370 658929 : gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
8371 : bool force_uncond = false)
8372 : {
8373 658929 : gimple *wce;
8374 658929 : gimple_seq cleanup_stmts = NULL;
8375 :
8376 : /* Errors can result in improperly nested cleanups. Which results in
8377 : confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
8378 658929 : if (seen_error ())
8379 5125 : return;
8380 :
8381 653804 : if (gimple_conditional_context ())
8382 : {
8383 : /* If we're in a conditional context, this is more complex. We only
8384 : want to run the cleanup if we actually ran the initialization that
8385 : necessitates it, but we want to run it after the end of the
8386 : conditional context. So we wrap the try/finally around the
8387 : condition and use a flag to determine whether or not to actually
8388 : run the destructor. Thus
8389 :
8390 : test ? f(A()) : 0
8391 :
8392 : becomes (approximately)
8393 :
8394 : flag = 0;
8395 : try {
8396 : if (test) { A::A(temp); flag = 1; val = f(temp); }
8397 : else { val = 0; }
8398 : } finally {
8399 : if (flag) A::~A(temp);
8400 : }
8401 : val
8402 : */
8403 15717 : if (force_uncond)
8404 : {
8405 14847 : gimplify_stmt (&cleanup, &cleanup_stmts);
8406 14847 : wce = gimple_build_wce (cleanup_stmts);
8407 14847 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8408 : }
8409 : else
8410 : {
8411 870 : tree flag = create_tmp_var (boolean_type_node, "cleanup");
8412 870 : gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
8413 870 : gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
8414 :
8415 870 : cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
8416 870 : gimplify_stmt (&cleanup, &cleanup_stmts);
8417 870 : wce = gimple_build_wce (cleanup_stmts);
8418 870 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8419 :
8420 870 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
8421 870 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8422 870 : gimplify_seq_add_stmt (pre_p, ftrue);
8423 :
8424 : /* Because of this manipulation, and the EH edges that jump
8425 : threading cannot redirect, the temporary (VAR) will appear
8426 : to be used uninitialized. Don't warn. */
8427 870 : suppress_warning (var, OPT_Wuninitialized);
8428 : }
8429 : }
8430 : else
8431 : {
8432 638087 : gimplify_stmt (&cleanup, &cleanup_stmts);
8433 638087 : wce = gimple_build_wce (cleanup_stmts);
8434 638087 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8435 638087 : gimplify_seq_add_stmt (pre_p, wce);
8436 : }
8437 : }
8438 :
8439 : /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
8440 :
8441 : static enum gimplify_status
8442 863972 : gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8443 : {
8444 863972 : tree targ = *expr_p;
8445 863972 : tree temp = TARGET_EXPR_SLOT (targ);
8446 863972 : tree init = TARGET_EXPR_INITIAL (targ);
8447 863972 : enum gimplify_status ret;
8448 :
8449 863972 : bool unpoison_empty_seq = false;
8450 863972 : gimple_stmt_iterator unpoison_it;
8451 :
8452 863972 : if (init)
8453 : {
8454 829746 : gimple_seq init_pre_p = NULL;
8455 829746 : bool is_vla = false;
8456 :
8457 : /* TARGET_EXPR temps aren't part of the enclosing block, so add it
8458 : to the temps list. Handle also variable length TARGET_EXPRs. */
8459 829746 : if (!poly_int_tree_p (DECL_SIZE (temp)))
8460 : {
8461 44 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
8462 1 : gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
8463 : /* FIXME: this is correct only when the size of the type does
8464 : not depend on expressions evaluated in init. */
8465 44 : gimplify_vla_decl (temp, &init_pre_p);
8466 44 : is_vla = true;
8467 : }
8468 : else
8469 : {
8470 : /* Save location where we need to place unpoisoning. It's possible
8471 : that a variable will be converted to needs_to_live_in_memory. */
8472 829702 : unpoison_it = gsi_last (*pre_p);
8473 829702 : unpoison_empty_seq = gsi_end_p (unpoison_it);
8474 :
8475 829702 : gimple_add_tmp_var (temp);
8476 : }
8477 :
8478 829746 : if (var_needs_auto_init_p (temp) && VOID_TYPE_P (TREE_TYPE (init)))
8479 : {
8480 59498 : gimple_add_init_for_auto_var (temp, flag_auto_var_init, &init_pre_p);
8481 59498 : if (flag_auto_var_init == AUTO_INIT_PATTERN
8482 0 : && !is_gimple_reg (temp)
8483 59498 : && clear_padding_type_may_have_padding_p (TREE_TYPE (temp)))
8484 0 : gimple_add_padding_init_for_auto_var (temp, is_vla, &init_pre_p);
8485 : }
8486 :
8487 : /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
8488 : expression is supposed to initialize the slot. */
8489 829746 : if (VOID_TYPE_P (TREE_TYPE (init)))
8490 293545 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8491 : fb_none);
8492 : else
8493 : {
8494 536201 : tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
8495 536201 : init = init_expr;
8496 536201 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8497 : fb_none);
8498 536201 : init = NULL;
8499 536201 : ggc_free (init_expr);
8500 : }
8501 829746 : if (ret == GS_ERROR)
8502 : {
8503 : /* PR c++/28266 Make sure this is expanded only once. */
8504 10 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8505 10 : return GS_ERROR;
8506 : }
8507 :
8508 829736 : if (init)
8509 0 : gimplify_and_add (init, &init_pre_p);
8510 :
8511 : /* Add a clobber for the temporary going out of scope, like
8512 : gimplify_bind_expr. But only if we did not promote the
8513 : temporary to static storage. */
8514 829736 : if (gimplify_ctxp->in_cleanup_point_expr
8515 685744 : && !TREE_STATIC (temp)
8516 1513930 : && needs_to_live_in_memory (temp))
8517 : {
8518 504501 : if (flag_stack_reuse == SR_ALL)
8519 : {
8520 504021 : tree clobber = build_clobber (TREE_TYPE (temp),
8521 : CLOBBER_STORAGE_END);
8522 504021 : clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
8523 504021 : gimple_push_cleanup (temp, clobber, false, pre_p, true);
8524 : }
8525 504501 : if (asan_poisoned_variables
8526 480 : && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
8527 480 : && !TREE_STATIC (temp)
8528 480 : && dbg_cnt (asan_use_after_scope)
8529 504981 : && !gimplify_omp_ctxp)
8530 : {
8531 472 : tree asan_cleanup = build_asan_poison_call_expr (temp);
8532 472 : if (asan_cleanup)
8533 : {
8534 472 : if (unpoison_empty_seq)
8535 217 : unpoison_it = gsi_start (*pre_p);
8536 :
8537 472 : asan_poison_variable (temp, false, &unpoison_it,
8538 : unpoison_empty_seq);
8539 472 : gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
8540 : }
8541 : }
8542 : }
8543 :
8544 829736 : gimple_seq_add_seq (pre_p, init_pre_p);
8545 :
8546 : /* If needed, push the cleanup for the temp. */
8547 829736 : if (TARGET_EXPR_CLEANUP (targ))
8548 154436 : gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
8549 154436 : CLEANUP_EH_ONLY (targ), pre_p);
8550 :
8551 : /* Only expand this once. */
8552 829736 : TREE_OPERAND (targ, 3) = init;
8553 829736 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8554 : }
8555 : else
8556 : /* We should have expanded this before. */
8557 34226 : gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
8558 :
8559 863962 : *expr_p = temp;
8560 863962 : return GS_OK;
8561 : }
8562 :
8563 : /* Gimplification of expression trees. */
8564 :
8565 : /* Gimplify an expression which appears at statement context. The
8566 : corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
8567 : NULL, a new sequence is allocated.
8568 :
8569 : Return true if we actually added a statement to the queue. */
8570 :
8571 : bool
8572 104248139 : gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
8573 : {
8574 104248139 : gimple_seq_node last;
8575 :
8576 104248139 : last = gimple_seq_last (*seq_p);
8577 104248139 : gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
8578 104248139 : return last != gimple_seq_last (*seq_p);
8579 : }
8580 :
8581 : /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
8582 : to CTX. If entries already exist, force them to be some flavor of private.
8583 : If there is no enclosing parallel, do nothing. */
8584 :
8585 : void
8586 126226 : omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
8587 : {
8588 126226 : splay_tree_node n;
8589 :
8590 126226 : if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
8591 : return;
8592 :
8593 36987 : do
8594 : {
8595 36987 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8596 36987 : if (n != NULL)
8597 : {
8598 4302 : if (n->value & GOVD_SHARED)
8599 183 : n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
8600 4119 : else if (n->value & GOVD_MAP)
8601 58 : n->value |= GOVD_MAP_TO_ONLY;
8602 : else
8603 : return;
8604 : }
8605 32685 : else if ((ctx->region_type & ORT_TARGET) != 0)
8606 : {
8607 7027 : if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
8608 4268 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8609 : else
8610 2759 : omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
8611 : }
8612 25658 : else if (ctx->region_type != ORT_WORKSHARE
8613 : && ctx->region_type != ORT_TASKGROUP
8614 23829 : && ctx->region_type != ORT_SIMD
8615 23205 : && ctx->region_type != ORT_ACC
8616 23088 : && !(ctx->region_type & ORT_TARGET_DATA))
8617 14507 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8618 :
8619 32926 : ctx = ctx->outer_context;
8620 : }
8621 32926 : while (ctx);
8622 : }
8623 :
8624 : /* Similarly for each of the type sizes of TYPE. */
8625 :
8626 : static void
8627 40270 : omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
8628 : {
8629 40270 : if (type == NULL || type == error_mark_node)
8630 : return;
8631 40266 : type = TYPE_MAIN_VARIANT (type);
8632 :
8633 40266 : if (ctx->privatized_types->add (type))
8634 : return;
8635 :
8636 31315 : switch (TREE_CODE (type))
8637 : {
8638 13592 : case INTEGER_TYPE:
8639 13592 : case ENUMERAL_TYPE:
8640 13592 : case BOOLEAN_TYPE:
8641 13592 : case REAL_TYPE:
8642 13592 : case FIXED_POINT_TYPE:
8643 13592 : omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
8644 13592 : omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
8645 13592 : break;
8646 :
8647 5418 : case ARRAY_TYPE:
8648 5418 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8649 5418 : omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
8650 5418 : break;
8651 :
8652 2502 : case RECORD_TYPE:
8653 2502 : case UNION_TYPE:
8654 2502 : case QUAL_UNION_TYPE:
8655 2502 : {
8656 2502 : tree field;
8657 26645 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8658 24143 : if (TREE_CODE (field) == FIELD_DECL)
8659 : {
8660 8997 : omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
8661 8997 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
8662 : }
8663 : }
8664 : break;
8665 :
8666 9153 : case POINTER_TYPE:
8667 9153 : case REFERENCE_TYPE:
8668 9153 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8669 9153 : break;
8670 :
8671 : default:
8672 : break;
8673 : }
8674 :
8675 31315 : omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
8676 31315 : omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
8677 31315 : lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
8678 : }
8679 :
8680 : /* Add an entry for DECL in the OMP context CTX with FLAGS. */
8681 :
8682 : static void
8683 717788 : omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
8684 : {
8685 717788 : splay_tree_node n;
8686 717788 : unsigned int nflags;
8687 717788 : tree t;
8688 :
8689 717788 : if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
8690 : return;
8691 :
8692 : /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
8693 : there are constructors involved somewhere. Exception is a shared clause,
8694 : there is nothing privatized in that case. */
8695 717640 : if ((flags & GOVD_SHARED) == 0
8696 717640 : && (TREE_ADDRESSABLE (TREE_TYPE (decl))
8697 657100 : || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
8698 7758 : flags |= GOVD_SEEN;
8699 :
8700 717640 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8701 717640 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8702 : {
8703 : /* We shouldn't be re-adding the decl with the same data
8704 : sharing class. */
8705 828 : gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
8706 828 : nflags = n->value | flags;
8707 : /* The only combination of data sharing classes we should see is
8708 : FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
8709 : reduction variables to be used in data sharing clauses. */
8710 828 : gcc_assert ((ctx->region_type & ORT_ACC) != 0
8711 : || ((nflags & GOVD_DATA_SHARE_CLASS)
8712 : == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
8713 : || (flags & GOVD_DATA_SHARE_CLASS) == 0);
8714 828 : n->value = nflags;
8715 828 : return;
8716 : }
8717 :
8718 : /* When adding a variable-sized variable, we have to handle all sorts
8719 : of additional bits of data: the pointer replacement variable, and
8720 : the parameters of the type. */
8721 716812 : if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
8722 : {
8723 : /* Add the pointer replacement variable as PRIVATE if the variable
8724 : replacement is private, else FIRSTPRIVATE since we'll need the
8725 : address of the original variable either for SHARED, or for the
8726 : copy into or out of the context. */
8727 1330 : if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
8728 : {
8729 1238 : if (flags & GOVD_MAP)
8730 : nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
8731 531 : else if (flags & GOVD_PRIVATE)
8732 : nflags = GOVD_PRIVATE;
8733 441 : else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
8734 50 : && (flags & GOVD_FIRSTPRIVATE))
8735 431 : || (ctx->region_type == ORT_TARGET_DATA
8736 6 : && (flags & GOVD_DATA_SHARE_CLASS) == 0))
8737 : nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
8738 : else
8739 1238 : nflags = GOVD_FIRSTPRIVATE;
8740 1238 : nflags |= flags & GOVD_SEEN;
8741 1238 : t = DECL_VALUE_EXPR (decl);
8742 1238 : gcc_assert (INDIRECT_REF_P (t));
8743 1238 : t = TREE_OPERAND (t, 0);
8744 1238 : gcc_assert (DECL_P (t));
8745 1238 : omp_add_variable (ctx, t, nflags);
8746 : }
8747 :
8748 : /* Add all of the variable and type parameters (which should have
8749 : been gimplified to a formal temporary) as FIRSTPRIVATE. */
8750 1330 : omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
8751 1330 : omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
8752 1330 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8753 :
8754 : /* The variable-sized variable itself is never SHARED, only some form
8755 : of PRIVATE. The sharing would take place via the pointer variable
8756 : which we remapped above. */
8757 1330 : if (flags & GOVD_SHARED)
8758 284 : flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
8759 284 : | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
8760 :
8761 : /* We're going to make use of the TYPE_SIZE_UNIT at least in the
8762 : alloca statement we generate for the variable, so make sure it
8763 : is available. This isn't automatically needed for the SHARED
8764 : case, since we won't be allocating local storage then.
8765 : For local variables TYPE_SIZE_UNIT might not be gimplified yet,
8766 : in this case omp_notice_variable will be called later
8767 : on when it is gimplified. */
8768 1046 : else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
8769 1046 : && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
8770 275 : omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
8771 : }
8772 715482 : else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
8773 715482 : && omp_privatize_by_reference (decl))
8774 : {
8775 9954 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8776 :
8777 : /* Similar to the direct variable sized case above, we'll need the
8778 : size of references being privatized. */
8779 9954 : if ((flags & GOVD_SHARED) == 0)
8780 : {
8781 6221 : t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
8782 6221 : if (t && DECL_P (t))
8783 1398 : omp_notice_variable (ctx, t, true);
8784 : }
8785 : }
8786 :
8787 716812 : if (n != NULL)
8788 1870 : n->value |= flags;
8789 : else
8790 714942 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
8791 :
8792 : /* For reductions clauses in OpenACC loop directives, by default create a
8793 : copy clause on the enclosing parallel construct for carrying back the
8794 : results. */
8795 716812 : if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
8796 : {
8797 4716 : struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
8798 6454 : while (outer_ctx)
8799 : {
8800 5788 : n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
8801 5788 : if (n != NULL)
8802 : {
8803 : /* Ignore local variables and explicitly declared clauses. */
8804 3990 : if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
8805 : break;
8806 443 : else if (outer_ctx->region_type == ORT_ACC_KERNELS)
8807 : {
8808 : /* According to the OpenACC spec, such a reduction variable
8809 : should already have a copy map on a kernels construct,
8810 : verify that here. */
8811 143 : gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
8812 : && (n->value & GOVD_MAP));
8813 : }
8814 300 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8815 : {
8816 : /* Remove firstprivate and make it a copy map. */
8817 150 : n->value &= ~GOVD_FIRSTPRIVATE;
8818 150 : n->value |= GOVD_MAP;
8819 : }
8820 : }
8821 1798 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8822 : {
8823 503 : splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
8824 : GOVD_MAP | GOVD_SEEN);
8825 503 : break;
8826 : }
8827 1738 : outer_ctx = outer_ctx->outer_context;
8828 : }
8829 : }
8830 : }
8831 :
8832 : /* Notice a threadprivate variable DECL used in OMP context CTX.
8833 : This just prints out diagnostics about threadprivate variable uses
8834 : in untied tasks. If DECL2 is non-NULL, prevent this warning
8835 : on that variable. */
8836 :
8837 : static bool
8838 14183 : omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
8839 : tree decl2)
8840 : {
8841 14183 : splay_tree_node n;
8842 14183 : struct gimplify_omp_ctx *octx;
8843 :
8844 28798 : for (octx = ctx; octx; octx = octx->outer_context)
8845 14615 : if ((octx->region_type & ORT_TARGET) != 0
8846 14615 : || octx->order_concurrent)
8847 : {
8848 112 : n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
8849 112 : if (n == NULL)
8850 : {
8851 20 : if (octx->order_concurrent)
8852 : {
8853 20 : error ("threadprivate variable %qE used in a region with"
8854 20 : " %<order(concurrent)%> clause", DECL_NAME (decl));
8855 20 : inform (octx->location, "enclosing region");
8856 : }
8857 : else
8858 : {
8859 0 : error ("threadprivate variable %qE used in target region",
8860 0 : DECL_NAME (decl));
8861 0 : inform (octx->location, "enclosing target region");
8862 : }
8863 20 : splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
8864 : }
8865 112 : if (decl2)
8866 0 : splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
8867 : }
8868 :
8869 14183 : if (ctx->region_type != ORT_UNTIED_TASK)
8870 : return false;
8871 37 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8872 37 : if (n == NULL)
8873 : {
8874 6 : error ("threadprivate variable %qE used in untied task",
8875 6 : DECL_NAME (decl));
8876 6 : inform (ctx->location, "enclosing task");
8877 6 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
8878 : }
8879 37 : if (decl2)
8880 4 : splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
8881 : return false;
8882 : }
8883 :
8884 : /* Return true if global var DECL is device resident. */
8885 :
8886 : static bool
8887 642 : device_resident_p (tree decl)
8888 : {
8889 642 : tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
8890 :
8891 642 : if (!attr)
8892 : return false;
8893 :
8894 0 : for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
8895 : {
8896 0 : tree c = TREE_VALUE (t);
8897 0 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
8898 : return true;
8899 : }
8900 :
8901 : return false;
8902 : }
8903 :
8904 : /* Return true if DECL has an ACC DECLARE attribute. */
8905 :
8906 : static bool
8907 9101 : is_oacc_declared (tree decl)
8908 : {
8909 9101 : tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
8910 9101 : tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
8911 9101 : return declared != NULL_TREE;
8912 : }
8913 :
8914 : /* Determine outer default flags for DECL mentioned in an OMP region
8915 : but not declared in an enclosing clause.
8916 :
8917 : ??? Some compiler-generated variables (like SAVE_EXPRs) could be
8918 : remapped firstprivate instead of shared. To some extent this is
8919 : addressed in omp_firstprivatize_type_sizes, but not
8920 : effectively. */
8921 :
8922 : static unsigned
8923 48045 : omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
8924 : bool in_code, unsigned flags)
8925 : {
8926 48045 : enum omp_clause_default_kind default_kind = ctx->default_kind;
8927 48045 : enum omp_clause_default_kind kind;
8928 :
8929 48045 : kind = lang_hooks.decls.omp_predetermined_sharing (decl);
8930 48045 : if (ctx->region_type & ORT_TASK)
8931 : {
8932 3994 : tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
8933 :
8934 : /* The event-handle specified by a detach clause should always be firstprivate,
8935 : regardless of the current default. */
8936 4206 : if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
8937 : kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
8938 : }
8939 48045 : if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8940 : default_kind = kind;
8941 45228 : else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
8942 : default_kind = OMP_CLAUSE_DEFAULT_SHARED;
8943 : /* For C/C++ default({,first}private), variables with static storage duration
8944 : declared in a namespace or global scope and referenced in construct
8945 : must be explicitly specified, i.e. acts as default(none). */
8946 45227 : else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
8947 45227 : || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
8948 371 : && VAR_P (decl)
8949 328 : && is_global_var (decl)
8950 184 : && (DECL_FILE_SCOPE_P (decl)
8951 104 : || (DECL_CONTEXT (decl)
8952 104 : && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
8953 45355 : && !lang_GNU_Fortran ())
8954 : default_kind = OMP_CLAUSE_DEFAULT_NONE;
8955 :
8956 47916 : switch (default_kind)
8957 : {
8958 267 : case OMP_CLAUSE_DEFAULT_NONE:
8959 267 : {
8960 267 : const char *rtype;
8961 :
8962 267 : if (ctx->region_type & ORT_PARALLEL)
8963 : rtype = "parallel";
8964 108 : else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
8965 : rtype = "taskloop";
8966 72 : else if (ctx->region_type & ORT_TASK)
8967 : rtype = "task";
8968 36 : else if (ctx->region_type & ORT_TEAMS)
8969 : rtype = "teams";
8970 : else
8971 0 : gcc_unreachable ();
8972 :
8973 267 : error ("%qE not specified in enclosing %qs",
8974 267 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
8975 267 : inform (ctx->location, "enclosing %qs", rtype);
8976 : }
8977 : /* FALLTHRU */
8978 44388 : case OMP_CLAUSE_DEFAULT_SHARED:
8979 44388 : flags |= GOVD_SHARED;
8980 44388 : break;
8981 155 : case OMP_CLAUSE_DEFAULT_PRIVATE:
8982 155 : flags |= GOVD_PRIVATE;
8983 155 : break;
8984 280 : case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
8985 280 : flags |= GOVD_FIRSTPRIVATE;
8986 280 : break;
8987 3222 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8988 : /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
8989 3222 : gcc_assert ((ctx->region_type & ORT_TASK) != 0);
8990 3222 : if (struct gimplify_omp_ctx *octx = ctx->outer_context)
8991 : {
8992 2164 : omp_notice_variable (octx, decl, in_code);
8993 5158 : for (; octx; octx = octx->outer_context)
8994 : {
8995 2938 : splay_tree_node n2;
8996 :
8997 2938 : n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
8998 2938 : if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
8999 5 : && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
9000 5 : continue;
9001 2933 : if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
9002 : {
9003 898 : flags |= GOVD_FIRSTPRIVATE;
9004 898 : goto found_outer;
9005 : }
9006 2035 : if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
9007 : {
9008 1210 : flags |= GOVD_SHARED;
9009 1210 : goto found_outer;
9010 : }
9011 : }
9012 : }
9013 :
9014 1114 : if (TREE_CODE (decl) == PARM_DECL
9015 1114 : || (!is_global_var (decl)
9016 294 : && DECL_CONTEXT (decl) == current_function_decl))
9017 734 : flags |= GOVD_FIRSTPRIVATE;
9018 : else
9019 380 : flags |= GOVD_SHARED;
9020 48045 : found_outer:
9021 : break;
9022 :
9023 0 : default:
9024 0 : gcc_unreachable ();
9025 : }
9026 :
9027 48045 : return flags;
9028 : }
9029 :
9030 : /* Return string name for types of OpenACC constructs from ORT_* values. */
9031 :
9032 : static const char *
9033 570 : oacc_region_type_name (enum omp_region_type region_type)
9034 : {
9035 570 : switch (region_type)
9036 : {
9037 : case ORT_ACC_DATA:
9038 : return "data";
9039 140 : case ORT_ACC_PARALLEL:
9040 140 : return "parallel";
9041 140 : case ORT_ACC_KERNELS:
9042 140 : return "kernels";
9043 140 : case ORT_ACC_SERIAL:
9044 140 : return "serial";
9045 0 : default:
9046 0 : gcc_unreachable ();
9047 : }
9048 : }
9049 :
9050 : /* Determine outer default flags for DECL mentioned in an OACC region
9051 : but not declared in an enclosing clause. */
9052 :
9053 : static unsigned
9054 8839 : oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
9055 : {
9056 8839 : struct gimplify_omp_ctx *ctx_default = ctx;
9057 : /* If no 'default' clause appears on this compute construct... */
9058 8839 : if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
9059 : {
9060 : /* ..., see if one appears on a lexically containing 'data'
9061 : construct. */
9062 9618 : while ((ctx_default = ctx_default->outer_context))
9063 : {
9064 1534 : if (ctx_default->region_type == ORT_ACC_DATA
9065 1534 : && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
9066 : break;
9067 : }
9068 : /* If not, reset. */
9069 8260 : if (!ctx_default)
9070 8663 : ctx_default = ctx;
9071 : }
9072 :
9073 8839 : bool on_device = false;
9074 8839 : bool is_private = false;
9075 8839 : bool declared = is_oacc_declared (decl);
9076 8839 : tree type = TREE_TYPE (decl);
9077 :
9078 8839 : if (omp_privatize_by_reference (decl))
9079 338 : type = TREE_TYPE (type);
9080 :
9081 : /* For Fortran COMMON blocks, only used variables in those blocks are
9082 : transferred and remapped. The block itself will have a private clause to
9083 : avoid transferring the data twice.
9084 : The hook evaluates to false by default. For a variable in Fortran's COMMON
9085 : or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
9086 : the variables in such a COMMON/EQUIVALENCE block shall be privatized not
9087 : the whole block. For C++ and Fortran, it can also be true under certain
9088 : other conditions, if DECL_HAS_VALUE_EXPR. */
9089 8839 : if (RECORD_OR_UNION_TYPE_P (type))
9090 983 : is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
9091 :
9092 8839 : if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
9093 8839 : && is_global_var (decl)
9094 642 : && device_resident_p (decl)
9095 8839 : && !is_private)
9096 : {
9097 0 : on_device = true;
9098 0 : flags |= GOVD_MAP_TO_ONLY;
9099 : }
9100 :
9101 8839 : switch (ctx->region_type)
9102 : {
9103 1460 : case ORT_ACC_KERNELS:
9104 1460 : if (is_private)
9105 0 : flags |= GOVD_FIRSTPRIVATE;
9106 1460 : else if (AGGREGATE_TYPE_P (type))
9107 : {
9108 : /* Aggregates default to 'present_or_copy', or 'present'. */
9109 415 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9110 397 : flags |= GOVD_MAP;
9111 : else
9112 18 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9113 : }
9114 : else
9115 : /* Scalars default to 'copy'. */
9116 1045 : flags |= GOVD_MAP | GOVD_MAP_FORCE;
9117 :
9118 : break;
9119 :
9120 7379 : case ORT_ACC_PARALLEL:
9121 7379 : case ORT_ACC_SERIAL:
9122 7379 : if (is_private)
9123 0 : flags |= GOVD_FIRSTPRIVATE;
9124 7379 : else if (on_device || declared)
9125 17 : flags |= GOVD_MAP;
9126 7362 : else if (AGGREGATE_TYPE_P (type))
9127 : {
9128 : /* Aggregates default to 'present_or_copy', or 'present'. */
9129 3697 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9130 3431 : flags |= GOVD_MAP;
9131 : else
9132 266 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9133 : }
9134 : else
9135 : /* Scalars default to 'firstprivate'. */
9136 3665 : flags |= GOVD_FIRSTPRIVATE;
9137 :
9138 : break;
9139 :
9140 0 : default:
9141 0 : gcc_unreachable ();
9142 : }
9143 :
9144 8839 : if (DECL_ARTIFICIAL (decl))
9145 : ; /* We can get compiler-generated decls, and should not complain
9146 : about them. */
9147 8528 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
9148 : {
9149 420 : error ("%qE not specified in enclosing OpenACC %qs construct",
9150 210 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
9151 : oacc_region_type_name (ctx->region_type));
9152 210 : if (ctx_default != ctx)
9153 150 : inform (ctx->location, "enclosing OpenACC %qs construct and",
9154 : oacc_region_type_name (ctx->region_type));
9155 210 : inform (ctx_default->location,
9156 : "enclosing OpenACC %qs construct with %qs clause",
9157 : oacc_region_type_name (ctx_default->region_type),
9158 : "default(none)");
9159 : }
9160 8318 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
9161 : ; /* Handled above. */
9162 : else
9163 7785 : gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
9164 :
9165 8839 : return flags;
9166 : }
9167 :
9168 : /* Record the fact that DECL was used within the OMP context CTX.
9169 : IN_CODE is true when real code uses DECL, and false when we should
9170 : merely emit default(none) errors. Return true if DECL is going to
9171 : be remapped and thus DECL shouldn't be gimplified into its
9172 : DECL_VALUE_EXPR (if any). */
9173 :
9174 : static bool
9175 3915375 : omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
9176 : {
9177 3915375 : splay_tree_node n;
9178 3915375 : unsigned flags = in_code ? GOVD_SEEN : 0;
9179 3915375 : bool ret = false, shared;
9180 :
9181 3915375 : if (error_operand_p (decl))
9182 : return false;
9183 :
9184 3915375 : if (DECL_ARTIFICIAL (decl))
9185 : {
9186 2235800 : tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
9187 2235800 : if (attr)
9188 531 : decl = TREE_VALUE (TREE_VALUE (attr));
9189 : }
9190 :
9191 3915375 : if (ctx->region_type == ORT_NONE)
9192 248 : return lang_hooks.decls.omp_disregard_value_expr (decl, false);
9193 :
9194 3915127 : if (is_global_var (decl))
9195 : {
9196 : /* Threadprivate variables are predetermined. */
9197 502432 : if (DECL_THREAD_LOCAL_P (decl))
9198 13565 : return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
9199 :
9200 488867 : if (DECL_HAS_VALUE_EXPR_P (decl))
9201 : {
9202 4380 : if (ctx->region_type & ORT_ACC)
9203 : /* For OpenACC, defer expansion of value to avoid transferring
9204 : privatized common block data instead of im-/explicitly
9205 : transferred variables which are in common blocks. */
9206 : ;
9207 : else
9208 : {
9209 2104 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
9210 :
9211 2104 : if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
9212 618 : return omp_notice_threadprivate_variable (ctx, decl, value);
9213 : }
9214 : }
9215 :
9216 488249 : if (gimplify_omp_ctxp->outer_context == NULL
9217 114312 : && VAR_P (decl)
9218 602561 : && oacc_get_fn_attrib (current_function_decl))
9219 : {
9220 765 : location_t loc = DECL_SOURCE_LOCATION (decl);
9221 :
9222 765 : if (lookup_attribute ("omp declare target link",
9223 765 : DECL_ATTRIBUTES (decl)))
9224 : {
9225 48 : error_at (loc,
9226 : "%qE with %<link%> clause used in %<routine%> function",
9227 24 : DECL_NAME (decl));
9228 24 : return false;
9229 : }
9230 741 : else if (!lookup_attribute ("omp declare target",
9231 741 : DECL_ATTRIBUTES (decl)))
9232 : {
9233 132 : error_at (loc,
9234 : "%qE requires a %<declare%> directive for use "
9235 66 : "in a %<routine%> function", DECL_NAME (decl));
9236 66 : return false;
9237 : }
9238 : }
9239 : }
9240 :
9241 3900854 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9242 3900854 : if ((ctx->region_type & ORT_TARGET) != 0)
9243 : {
9244 852585 : if (n == NULL)
9245 : {
9246 45095 : unsigned nflags = flags;
9247 45095 : if ((ctx->region_type & ORT_ACC) == 0)
9248 : {
9249 34141 : bool is_declare_target = false;
9250 34141 : if (is_global_var (decl)
9251 34141 : && varpool_node::get_create (decl)->offloadable)
9252 : {
9253 7040 : struct gimplify_omp_ctx *octx;
9254 7040 : for (octx = ctx->outer_context;
9255 7048 : octx; octx = octx->outer_context)
9256 : {
9257 9 : n = splay_tree_lookup (octx->variables,
9258 : (splay_tree_key)decl);
9259 9 : if (n
9260 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
9261 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9262 : break;
9263 : }
9264 7040 : is_declare_target = octx == NULL;
9265 : }
9266 7040 : if (!is_declare_target)
9267 : {
9268 27102 : int gdmk;
9269 27102 : enum omp_clause_defaultmap_kind kind;
9270 27102 : if (lang_hooks.decls.omp_allocatable_p (decl))
9271 : gdmk = GDMK_ALLOCATABLE;
9272 26753 : else if (lang_hooks.decls.omp_scalar_target_p (decl))
9273 : gdmk = GDMK_SCALAR_TARGET;
9274 26678 : else if (lang_hooks.decls.omp_scalar_p (decl, false))
9275 : gdmk = GDMK_SCALAR;
9276 11178 : else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9277 11178 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9278 3105 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9279 : == POINTER_TYPE)))
9280 : gdmk = GDMK_POINTER;
9281 : else
9282 : gdmk = GDMK_AGGREGATE;
9283 27102 : kind = lang_hooks.decls.omp_predetermined_mapping (decl);
9284 27102 : if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
9285 : {
9286 1096 : if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
9287 101 : nflags |= GOVD_FIRSTPRIVATE;
9288 995 : else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
9289 995 : nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
9290 : else
9291 0 : gcc_unreachable ();
9292 : }
9293 26006 : else if (ctx->defaultmap[gdmk] == 0)
9294 : {
9295 75 : tree d = lang_hooks.decls.omp_report_decl (decl);
9296 75 : error ("%qE not specified in enclosing %<target%>",
9297 75 : DECL_NAME (d));
9298 75 : inform (ctx->location, "enclosing %<target%>");
9299 : }
9300 25931 : else if (ctx->defaultmap[gdmk]
9301 25931 : & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
9302 9139 : nflags |= ctx->defaultmap[gdmk];
9303 16792 : else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
9304 : {
9305 42 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9306 42 : nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
9307 : }
9308 : else
9309 : {
9310 16750 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9311 16750 : nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
9312 : }
9313 : }
9314 : }
9315 :
9316 45095 : struct gimplify_omp_ctx *octx = ctx->outer_context;
9317 45095 : if ((ctx->region_type & ORT_ACC) && octx)
9318 : {
9319 : /* Look in outer OpenACC contexts, to see if there's a
9320 : data attribute for this variable. */
9321 3535 : omp_notice_variable (octx, decl, in_code);
9322 :
9323 8842 : for (; octx; octx = octx->outer_context)
9324 : {
9325 3883 : if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
9326 : break;
9327 3883 : splay_tree_node n2
9328 3883 : = splay_tree_lookup (octx->variables,
9329 : (splay_tree_key) decl);
9330 3883 : if (n2)
9331 : {
9332 2111 : if (octx->region_type == ORT_ACC_HOST_DATA)
9333 4 : error ("variable %qE declared in enclosing "
9334 4 : "%<host_data%> region", DECL_NAME (decl));
9335 2111 : nflags |= GOVD_MAP;
9336 2111 : if (octx->region_type == ORT_ACC_DATA
9337 2107 : && (n2->value & GOVD_MAP_0LEN_ARRAY))
9338 288 : nflags |= GOVD_MAP_0LEN_ARRAY;
9339 2111 : goto found_outer;
9340 : }
9341 : }
9342 : }
9343 :
9344 42984 : if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
9345 : | GOVD_MAP_ALLOC_ONLY)) == flags)
9346 : {
9347 32707 : tree type = TREE_TYPE (decl);
9348 32707 : location_t loc = DECL_SOURCE_LOCATION (decl);
9349 :
9350 32707 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9351 32707 : && omp_privatize_by_reference (decl))
9352 28 : type = TREE_TYPE (type);
9353 :
9354 32707 : if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
9355 : /* Check if TYPE can appear in a target region.
9356 : verify_type_context has already issued an error if it
9357 : can't. */
9358 0 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9359 32707 : else if (!omp_mappable_type (type))
9360 : {
9361 8 : error ("%qD referenced in target region does not have "
9362 : "a mappable type", decl);
9363 8 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9364 : }
9365 : else
9366 : {
9367 32699 : if ((ctx->region_type & ORT_ACC) != 0)
9368 8839 : nflags = oacc_default_clause (ctx, decl, flags);
9369 : else
9370 23860 : nflags |= GOVD_MAP;
9371 : }
9372 : }
9373 10277 : found_outer:
9374 45095 : omp_add_variable (ctx, decl, nflags);
9375 45095 : if (ctx->region_type & ORT_ACC)
9376 : /* For OpenACC, as remarked above, defer expansion. */
9377 : shared = false;
9378 : else
9379 34141 : shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9380 45095 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9381 : }
9382 : else
9383 : {
9384 807490 : if (ctx->region_type & ORT_ACC)
9385 : /* For OpenACC, as remarked above, defer expansion. */
9386 : shared = false;
9387 : else
9388 511065 : shared = ((n->value | flags)
9389 511065 : & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9390 807490 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9391 : /* If nothing changed, there's nothing left to do. */
9392 807490 : if ((n->value & flags) == flags)
9393 : return ret;
9394 16615 : flags |= n->value;
9395 16615 : n->value = flags;
9396 : }
9397 61710 : goto do_outer;
9398 : }
9399 :
9400 3048269 : if (n == NULL)
9401 : {
9402 1182882 : if (ctx->region_type == ORT_WORKSHARE
9403 : || ctx->region_type == ORT_TASKGROUP
9404 561150 : || ctx->region_type == ORT_SIMD
9405 289025 : || ctx->region_type == ORT_ACC
9406 92772 : || (ctx->region_type & ORT_TARGET_DATA) != 0)
9407 1134837 : goto do_outer;
9408 :
9409 48045 : flags = omp_default_clause (ctx, decl, in_code, flags);
9410 :
9411 48045 : if ((flags & GOVD_PRIVATE)
9412 48045 : && lang_hooks.decls.omp_private_outer_ref (decl))
9413 6 : flags |= GOVD_PRIVATE_OUTER_REF;
9414 :
9415 48045 : omp_add_variable (ctx, decl, flags);
9416 :
9417 48045 : shared = (flags & GOVD_SHARED) != 0;
9418 48045 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9419 48045 : goto do_outer;
9420 : }
9421 :
9422 : /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
9423 : lb, b or incr expressions, those shouldn't be turned into simd arrays. */
9424 1865387 : if (ctx->region_type == ORT_SIMD
9425 151237 : && ctx->in_for_exprs
9426 70 : && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
9427 : == GOVD_PRIVATE))
9428 1865387 : flags &= ~GOVD_SEEN;
9429 :
9430 1865387 : if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
9431 31897 : && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
9432 1897245 : && DECL_SIZE (decl))
9433 : {
9434 31857 : tree size;
9435 31857 : if (!poly_int_tree_p (DECL_SIZE (decl)))
9436 : {
9437 148 : splay_tree_node n2;
9438 148 : tree t = DECL_VALUE_EXPR (decl);
9439 148 : gcc_assert (INDIRECT_REF_P (t));
9440 148 : t = TREE_OPERAND (t, 0);
9441 148 : gcc_assert (DECL_P (t));
9442 148 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9443 148 : n2->value |= GOVD_SEEN;
9444 : }
9445 31709 : else if (omp_privatize_by_reference (decl)
9446 4193 : && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
9447 35865 : && !poly_int_tree_p (size))
9448 : {
9449 1337 : splay_tree_node n2;
9450 1337 : gcc_assert (DECL_P (size));
9451 1337 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
9452 1337 : if (n2)
9453 629 : omp_notice_variable (ctx, size, true);
9454 : }
9455 : }
9456 :
9457 1865387 : if (ctx->region_type & ORT_ACC)
9458 : /* For OpenACC, as remarked above, defer expansion. */
9459 : shared = false;
9460 : else
9461 1683452 : shared = ((flags | n->value) & GOVD_SHARED) != 0;
9462 1865387 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9463 :
9464 : /* If nothing changed, there's nothing left to do. */
9465 1865387 : if ((n->value & flags) == flags)
9466 : return ret;
9467 31858 : flags |= n->value;
9468 31858 : n->value = flags;
9469 :
9470 1276450 : do_outer:
9471 : /* If the variable is private in the current context, then we don't
9472 : need to propagate anything to an outer context. */
9473 1276450 : if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
9474 : return ret;
9475 1266050 : if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9476 : == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9477 : return ret;
9478 1265990 : if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9479 : | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9480 : == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9481 : return ret;
9482 1265990 : if (ctx->outer_context
9483 1265990 : && omp_notice_variable (ctx->outer_context, decl, in_code))
9484 2151 : return true;
9485 : return ret;
9486 : }
9487 :
9488 : /* Verify that DECL is private within CTX. If there's specific information
9489 : to the contrary in the innermost scope, generate an error. */
9490 :
9491 : static bool
9492 50265 : omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
9493 : {
9494 96050 : splay_tree_node n;
9495 :
9496 96050 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9497 96050 : if (n != NULL)
9498 : {
9499 17976 : if (n->value & GOVD_SHARED)
9500 : {
9501 407 : if (ctx == gimplify_omp_ctxp)
9502 : {
9503 0 : if (simd)
9504 0 : error ("iteration variable %qE is predetermined linear",
9505 0 : DECL_NAME (decl));
9506 : else
9507 0 : error ("iteration variable %qE should be private",
9508 0 : DECL_NAME (decl));
9509 0 : n->value = GOVD_PRIVATE;
9510 0 : return true;
9511 : }
9512 : else
9513 : return false;
9514 : }
9515 17569 : else if ((n->value & GOVD_EXPLICIT) != 0
9516 9719 : && (ctx == gimplify_omp_ctxp
9517 340 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9518 220 : && gimplify_omp_ctxp->outer_context == ctx)))
9519 : {
9520 9599 : if ((n->value & GOVD_FIRSTPRIVATE) != 0)
9521 4 : error ("iteration variable %qE should not be firstprivate",
9522 4 : DECL_NAME (decl));
9523 9595 : else if ((n->value & GOVD_REDUCTION) != 0)
9524 8 : error ("iteration variable %qE should not be reduction",
9525 8 : DECL_NAME (decl));
9526 9587 : else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
9527 58 : error ("iteration variable %qE should not be linear",
9528 58 : DECL_NAME (decl));
9529 : }
9530 17569 : return (ctx == gimplify_omp_ctxp
9531 17569 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9532 2618 : && gimplify_omp_ctxp->outer_context == ctx));
9533 : }
9534 :
9535 78074 : if (ctx->region_type != ORT_WORKSHARE
9536 : && ctx->region_type != ORT_TASKGROUP
9537 49502 : && ctx->region_type != ORT_SIMD
9538 36229 : && ctx->region_type != ORT_ACC)
9539 : return false;
9540 54018 : else if (ctx->outer_context)
9541 : return omp_is_private (ctx->outer_context, decl, simd);
9542 : return false;
9543 : }
9544 :
9545 : /* Return true if DECL is private within a parallel region
9546 : that binds to the current construct's context or in parallel
9547 : region's REDUCTION clause. */
9548 :
9549 : static bool
9550 11606 : omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
9551 : {
9552 11678 : splay_tree_node n;
9553 :
9554 11678 : do
9555 : {
9556 11678 : ctx = ctx->outer_context;
9557 11678 : if (ctx == NULL)
9558 : {
9559 2784 : if (is_global_var (decl))
9560 : return false;
9561 :
9562 : /* References might be private, but might be shared too,
9563 : when checking for copyprivate, assume they might be
9564 : private, otherwise assume they might be shared. */
9565 1290 : if (copyprivate)
9566 : return true;
9567 :
9568 1244 : if (omp_privatize_by_reference (decl))
9569 : return false;
9570 :
9571 : /* Treat C++ privatized non-static data members outside
9572 : of the privatization the same. */
9573 1198 : if (omp_member_access_dummy_var (decl))
9574 : return false;
9575 :
9576 1142 : return true;
9577 : }
9578 :
9579 8894 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9580 :
9581 8894 : if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
9582 1291 : && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
9583 : {
9584 242 : if ((ctx->region_type & ORT_TARGET_DATA) != 0
9585 238 : || n == NULL
9586 230 : || (n->value & GOVD_MAP) == 0)
9587 12 : continue;
9588 : return false;
9589 : }
9590 :
9591 7603 : if (n != NULL)
9592 : {
9593 5397 : if ((n->value & GOVD_LOCAL) != 0
9594 5397 : && omp_member_access_dummy_var (decl))
9595 : return false;
9596 5358 : return (n->value & GOVD_SHARED) == 0;
9597 : }
9598 :
9599 3255 : if (ctx->region_type == ORT_WORKSHARE
9600 : || ctx->region_type == ORT_TASKGROUP
9601 3199 : || ctx->region_type == ORT_SIMD
9602 3195 : || ctx->region_type == ORT_ACC)
9603 60 : continue;
9604 :
9605 : break;
9606 : }
9607 : while (1);
9608 : return false;
9609 : }
9610 :
9611 : /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
9612 :
9613 : static tree
9614 3148 : find_decl_expr (tree *tp, int *walk_subtrees, void *data)
9615 : {
9616 3148 : tree t = *tp;
9617 :
9618 : /* If this node has been visited, unmark it and keep looking. */
9619 3148 : if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
9620 : return t;
9621 :
9622 2724 : if (IS_TYPE_OR_DECL_P (t))
9623 452 : *walk_subtrees = 0;
9624 : return NULL_TREE;
9625 : }
9626 :
9627 :
9628 : /* Gimplify the affinity clause but effectively ignore it.
9629 : Generate:
9630 : var = begin;
9631 : if ((step > 1) ? var <= end : var > end)
9632 : locatator_var_expr; */
9633 :
9634 : static void
9635 510 : gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
9636 : {
9637 510 : tree last_iter = NULL_TREE;
9638 510 : tree last_bind = NULL_TREE;
9639 510 : tree label = NULL_TREE;
9640 510 : tree *last_body = NULL;
9641 1287 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9642 777 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
9643 : {
9644 777 : tree t = OMP_CLAUSE_DECL (c);
9645 777 : if (OMP_ITERATOR_DECL_P (t))
9646 : {
9647 389 : if (TREE_VALUE (t) == null_pointer_node)
9648 201 : continue;
9649 188 : if (TREE_PURPOSE (t) != last_iter)
9650 : {
9651 127 : if (last_bind)
9652 : {
9653 9 : append_to_statement_list (label, last_body);
9654 9 : gimplify_and_add (last_bind, pre_p);
9655 9 : last_bind = NULL_TREE;
9656 : }
9657 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9658 : {
9659 147 : if (gimplify_expr (&OMP_ITERATOR_BEGIN (it), pre_p, NULL,
9660 : is_gimple_val, fb_rvalue) == GS_ERROR
9661 147 : || gimplify_expr (&OMP_ITERATOR_END (it), pre_p, NULL,
9662 : is_gimple_val, fb_rvalue) == GS_ERROR
9663 147 : || gimplify_expr (&OMP_ITERATOR_STEP (it), pre_p, NULL,
9664 : is_gimple_val, fb_rvalue) == GS_ERROR
9665 294 : || (gimplify_expr (&OMP_ITERATOR_ORIG_STEP (it), pre_p,
9666 : NULL, is_gimple_val, fb_rvalue)
9667 : == GS_ERROR))
9668 0 : return;
9669 : }
9670 127 : last_iter = TREE_PURPOSE (t);
9671 127 : tree block = OMP_ITERATOR_BLOCK (TREE_PURPOSE (t));
9672 127 : last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
9673 : NULL, block);
9674 127 : last_body = &BIND_EXPR_BODY (last_bind);
9675 127 : tree cond = NULL_TREE;
9676 127 : location_t loc = OMP_CLAUSE_LOCATION (c);
9677 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9678 : {
9679 147 : tree var = OMP_ITERATOR_VAR (it);
9680 147 : tree begin = OMP_ITERATOR_BEGIN (it);
9681 147 : tree end = OMP_ITERATOR_END (it);
9682 147 : tree step = OMP_ITERATOR_STEP (it);
9683 147 : loc = DECL_SOURCE_LOCATION (var);
9684 147 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9685 : var, begin);
9686 147 : append_to_statement_list_force (tem, last_body);
9687 :
9688 147 : tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9689 147 : step, build_zero_cst (TREE_TYPE (step)));
9690 147 : tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
9691 : var, end);
9692 147 : tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9693 : var, end);
9694 147 : cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
9695 : cond1, cond2, cond3);
9696 147 : if (cond)
9697 20 : cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
9698 : boolean_type_node, cond, cond1);
9699 : else
9700 : cond = cond1;
9701 : }
9702 127 : tree cont_label = create_artificial_label (loc);
9703 127 : label = build1 (LABEL_EXPR, void_type_node, cont_label);
9704 127 : tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9705 : void_node,
9706 : build_and_jump (&cont_label));
9707 127 : append_to_statement_list_force (tem, last_body);
9708 : }
9709 188 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
9710 : {
9711 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
9712 : last_body);
9713 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
9714 : }
9715 188 : if (error_operand_p (TREE_VALUE (t)))
9716 : return;
9717 188 : append_to_statement_list_force (TREE_VALUE (t), last_body);
9718 188 : TREE_VALUE (t) = null_pointer_node;
9719 : }
9720 : else
9721 : {
9722 388 : if (last_bind)
9723 : {
9724 9 : append_to_statement_list (label, last_body);
9725 9 : gimplify_and_add (last_bind, pre_p);
9726 9 : last_bind = NULL_TREE;
9727 : }
9728 388 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
9729 : {
9730 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
9731 : NULL, is_gimple_val, fb_rvalue);
9732 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
9733 : }
9734 388 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
9735 : return;
9736 388 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
9737 : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
9738 : return;
9739 388 : gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
9740 : }
9741 : }
9742 510 : if (last_bind)
9743 : {
9744 109 : append_to_statement_list (label, last_body);
9745 109 : gimplify_and_add (last_bind, pre_p);
9746 : }
9747 : return;
9748 : }
9749 :
9750 : /* Returns a tree expression containing the total iteration count of the
9751 : OpenMP iterator IT. */
9752 :
9753 : static tree
9754 379 : compute_omp_iterator_count (tree it, gimple_seq *pre_p)
9755 : {
9756 379 : tree tcnt = size_one_node;
9757 837 : for (; it; it = TREE_CHAIN (it))
9758 : {
9759 458 : if (gimplify_expr (&OMP_ITERATOR_BEGIN (it), pre_p, NULL,
9760 : is_gimple_val, fb_rvalue) == GS_ERROR
9761 458 : || gimplify_expr (&OMP_ITERATOR_END (it), pre_p, NULL,
9762 : is_gimple_val, fb_rvalue) == GS_ERROR
9763 458 : || gimplify_expr (&OMP_ITERATOR_STEP (it), pre_p, NULL,
9764 : is_gimple_val, fb_rvalue) == GS_ERROR
9765 916 : || (gimplify_expr (&OMP_ITERATOR_ORIG_STEP (it), pre_p, NULL,
9766 : is_gimple_val, fb_rvalue) == GS_ERROR))
9767 0 : return NULL_TREE;
9768 458 : tree var = OMP_ITERATOR_VAR (it);
9769 458 : tree begin = OMP_ITERATOR_BEGIN (it);
9770 458 : tree end = OMP_ITERATOR_END (it);
9771 458 : tree step = OMP_ITERATOR_STEP (it);
9772 458 : tree orig_step = OMP_ITERATOR_ORIG_STEP (it);
9773 458 : tree type = TREE_TYPE (var);
9774 458 : tree stype = TREE_TYPE (step);
9775 458 : location_t loc = DECL_SOURCE_LOCATION (var);
9776 458 : tree endmbegin;
9777 : /* Compute count for this iterator as
9778 : orig_step > 0
9779 : ? (begin < end ? (end - begin + (step - 1)) / step : 0)
9780 : : (begin > end ? (end - begin + (step + 1)) / step : 0)
9781 : and compute product of those for the entire clause. */
9782 458 : if (POINTER_TYPE_P (type))
9783 42 : endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR, stype, end, begin);
9784 : else
9785 416 : endmbegin = fold_build2_loc (loc, MINUS_EXPR, type, end, begin);
9786 : /* Account for iteration stopping on the end value in Fortran rather
9787 : than before it. */
9788 458 : tree stepm1 = step;
9789 458 : tree stepp1 = step;
9790 458 : if (!lang_GNU_Fortran ())
9791 : {
9792 360 : stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype, step,
9793 : build_int_cst (stype, 1));
9794 360 : stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
9795 : build_int_cst (stype, 1));
9796 : }
9797 458 : tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
9798 : unshare_expr (endmbegin), stepm1);
9799 458 : pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, pos, step);
9800 458 : tree neg = fold_build2_loc (loc, PLUS_EXPR, stype, endmbegin, stepp1);
9801 458 : if (TYPE_UNSIGNED (stype))
9802 : {
9803 31 : neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
9804 31 : step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
9805 : }
9806 458 : neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, neg, step);
9807 458 : step = NULL_TREE;
9808 458 : tree_code cmp_op = lang_GNU_Fortran () ? LE_EXPR : LT_EXPR;
9809 458 : tree cond = fold_build2_loc (loc, cmp_op, boolean_type_node, begin, end);
9810 458 : pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
9811 : build_int_cst (stype, 0));
9812 458 : cond = fold_build2_loc (loc, cmp_op, boolean_type_node, end, begin);
9813 458 : neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
9814 : build_int_cst (stype, 0));
9815 458 : tree osteptype = TREE_TYPE (orig_step);
9816 458 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9817 : build_int_cst (osteptype, 0));
9818 458 : tree cnt = fold_build3_loc (loc, COND_EXPR, stype, cond, pos, neg);
9819 458 : cnt = fold_convert_loc (loc, sizetype, cnt);
9820 458 : if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
9821 : fb_rvalue) == GS_ERROR)
9822 : return NULL_TREE;
9823 458 : tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
9824 : }
9825 379 : if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
9826 : return NULL_TREE;
9827 :
9828 379 : return tcnt;
9829 : }
9830 :
9831 : /* Build loops iterating over the space defined by the OpenMP iterator IT.
9832 : Returns a pointer to the BIND_EXPR_BODY in the innermost loop body.
9833 : LAST_BIND is set to point to the BIND_EXPR containing the whole loop. */
9834 :
9835 : static tree *
9836 379 : build_omp_iterator_loop (tree it, gimple_seq *pre_p, tree *last_bind)
9837 : {
9838 379 : if (*last_bind)
9839 31 : gimplify_and_add (*last_bind, pre_p);
9840 379 : tree block = OMP_ITERATOR_BLOCK (it);
9841 464 : tree block_stmts = lang_GNU_Fortran () ? BLOCK_SUBBLOCKS (block) : NULL_TREE;
9842 379 : *last_bind = build3 (BIND_EXPR, void_type_node,
9843 379 : BLOCK_VARS (block), NULL, block);
9844 379 : TREE_SIDE_EFFECTS (*last_bind) = 1;
9845 379 : tree *p = &BIND_EXPR_BODY (*last_bind);
9846 1216 : for (; it; it = TREE_CHAIN (it))
9847 : {
9848 458 : tree var = OMP_ITERATOR_VAR (it);
9849 458 : tree begin = OMP_ITERATOR_BEGIN (it);
9850 458 : tree end = OMP_ITERATOR_END (it);
9851 458 : tree step = OMP_ITERATOR_STEP (it);
9852 458 : tree orig_step = OMP_ITERATOR_ORIG_STEP (it);
9853 458 : block = OMP_ITERATOR_BLOCK (it);
9854 458 : tree type = TREE_TYPE (var);
9855 458 : location_t loc = DECL_SOURCE_LOCATION (var);
9856 : /* Emit:
9857 : var = begin;
9858 : goto cond_label;
9859 : beg_label:
9860 : ...
9861 : var = var + step;
9862 : cond_label:
9863 : if (orig_step > 0) {
9864 : if (var < end) goto beg_label; // <= for Fortran
9865 : } else {
9866 : if (var > end) goto beg_label; // >= for Fortran
9867 : }
9868 : for each iterator, with inner iterators added to
9869 : the ... above. */
9870 458 : tree beg_label = create_artificial_label (loc);
9871 458 : tree cond_label = NULL_TREE;
9872 458 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, begin);
9873 458 : append_to_statement_list_force (tem, p);
9874 458 : tem = build_and_jump (&cond_label);
9875 458 : append_to_statement_list_force (tem, p);
9876 458 : tem = build1 (LABEL_EXPR, void_type_node, beg_label);
9877 458 : append_to_statement_list (tem, p);
9878 458 : tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9879 : NULL_TREE, NULL_TREE);
9880 458 : TREE_SIDE_EFFECTS (bind) = 1;
9881 458 : SET_EXPR_LOCATION (bind, loc);
9882 458 : append_to_statement_list_force (bind, p);
9883 458 : if (POINTER_TYPE_P (type))
9884 42 : tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
9885 : var, fold_convert_loc (loc, sizetype, step));
9886 : else
9887 416 : tem = build2_loc (loc, PLUS_EXPR, type, var, step);
9888 458 : tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, tem);
9889 458 : append_to_statement_list_force (tem, p);
9890 458 : tem = build1 (LABEL_EXPR, void_type_node, cond_label);
9891 458 : append_to_statement_list (tem, p);
9892 818 : tree cond = fold_build2_loc (loc, lang_GNU_Fortran () ? LE_EXPR : LT_EXPR,
9893 : boolean_type_node, var, end);
9894 458 : tree pos = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9895 : build_and_jump (&beg_label), void_node);
9896 818 : cond = fold_build2_loc (loc, lang_GNU_Fortran () ? GE_EXPR : GT_EXPR,
9897 : boolean_type_node, var, end);
9898 458 : tree neg = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9899 : build_and_jump (&beg_label), void_node);
9900 458 : tree osteptype = TREE_TYPE (orig_step);
9901 458 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9902 : build_int_cst (osteptype, 0));
9903 458 : tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, pos, neg);
9904 458 : append_to_statement_list_force (tem, p);
9905 458 : p = &BIND_EXPR_BODY (bind);
9906 : /* The Fortran front-end stashes statements into the BLOCK_SUBBLOCKS
9907 : of the last element of the first iterator. These should go into the
9908 : body of the innermost loop. */
9909 458 : if (!TREE_CHAIN (it))
9910 379 : append_to_statement_list_force (block_stmts, p);
9911 : }
9912 :
9913 379 : return p;
9914 : }
9915 :
9916 :
9917 : /* Callback for walk_tree to find a VAR_DECL (stored in DATA) in the
9918 : tree TP. */
9919 :
9920 : static tree
9921 3813 : find_var_decl (tree *tp, int *, void *data)
9922 : {
9923 3813 : if (*tp == (tree) data)
9924 357 : return *tp;
9925 :
9926 : return NULL_TREE;
9927 : }
9928 :
9929 : /* Returns an element-by-element copy of OMP iterator tree IT. If
9930 : EXPAND is true, force the result to be an expanded iterator; otherwise
9931 : it makes the copy the same size as IT. */
9932 : static tree
9933 337 : copy_omp_iterator (tree it, bool expand = false)
9934 : {
9935 337 : int elem_count = TREE_VEC_LENGTH (it);
9936 337 : tree new_it =
9937 337 : (expand ? make_expanded_omp_iterator () : make_tree_vec (elem_count));
9938 2359 : for (int i = 0; i < TREE_VEC_LENGTH (it); i++)
9939 2022 : TREE_VEC_ELT (new_it, i) = TREE_VEC_ELT (it, i);
9940 337 : return new_it;
9941 : }
9942 :
9943 : /* Helper function for walk_tree in remap_omp_iterator_var. */
9944 :
9945 : static tree
9946 1063 : remap_omp_iterator_var_1 (tree *tp, int *, void *data)
9947 : {
9948 1063 : tree old_var = ((tree *) data)[0];
9949 1063 : tree new_var = ((tree *) data)[1];
9950 :
9951 1063 : if (*tp == old_var)
9952 106 : *tp = new_var;
9953 1063 : return NULL_TREE;
9954 : }
9955 :
9956 : /* Replace instances of OLD_VAR in TP with NEW_VAR. */
9957 :
9958 : static void
9959 212 : remap_omp_iterator_var (tree *tp, tree old_var, tree new_var)
9960 : {
9961 212 : tree vars[2] = { old_var, new_var };
9962 212 : walk_tree (tp, remap_omp_iterator_var_1, vars, NULL);
9963 212 : }
9964 :
9965 : /* Scan through all clauses using OpenMP iterators in LIST_P. If any
9966 : clauses have iterators with variables that are not used by the clause
9967 : decl or size, issue a warning and replace the iterator with a copy with
9968 : the unused variables removed. */
9969 :
9970 : static void
9971 27783 : remove_unused_omp_iterator_vars (tree *list_p)
9972 : {
9973 27783 : auto_vec< vec<tree> > iter_vars;
9974 27783 : auto_vec<tree> new_iterators;
9975 :
9976 81028 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9977 : {
9978 53245 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
9979 53181 : continue;
9980 276 : auto_vec<tree> vars;
9981 276 : bool need_new_iterators = false;
9982 710 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
9983 : {
9984 434 : tree var = OMP_ITERATOR_VAR (it);
9985 434 : tree t = walk_tree (&OMP_CLAUSE_DECL (c), find_var_decl, var, NULL);
9986 434 : if (t == NULL_TREE)
9987 143 : t = walk_tree (&OMP_CLAUSE_SIZE (c), find_var_decl, var, NULL);
9988 143 : if (t == NULL_TREE)
9989 : need_new_iterators = true;
9990 : else
9991 357 : vars.safe_push (var);
9992 : }
9993 276 : if (!need_new_iterators)
9994 199 : continue;
9995 77 : if (vars.is_empty ())
9996 : {
9997 : /* No iteration variables are used in the clause - remove the
9998 : iterator from the clause. */
9999 13 : OMP_CLAUSE_ITERATORS (c) = NULL_TREE;
10000 13 : continue;
10001 : }
10002 :
10003 : /* If a new iterator has been created for the current set of used
10004 : iterator variables, then use that as the iterator. Otherwise,
10005 : create a new iterator for the current iterator variable set. */
10006 : unsigned i;
10007 106 : for (i = 0; i < iter_vars.length (); i++)
10008 : {
10009 124 : if (vars.length () != iter_vars[i].length ())
10010 0 : continue;
10011 : bool identical_p = true;
10012 150 : for (unsigned j = 0; j < vars.length () && identical_p; j++)
10013 88 : identical_p = vars[j] == iter_vars[i][j];
10014 :
10015 62 : if (identical_p)
10016 : break;
10017 : }
10018 64 : if (i < iter_vars.length ())
10019 20 : OMP_CLAUSE_ITERATORS (c) = new_iterators[i];
10020 : else
10021 : {
10022 44 : tree new_iters = NULL_TREE;
10023 44 : tree *new_iters_p = &new_iters;
10024 44 : tree new_vars = NULL_TREE;
10025 44 : tree *new_vars_p = &new_vars;
10026 44 : i = 0;
10027 140 : for (tree it = OMP_CLAUSE_ITERATORS (c); it && i < vars.length();
10028 96 : it = TREE_CHAIN (it))
10029 : {
10030 96 : tree var = OMP_ITERATOR_VAR (it);
10031 96 : if (var == vars[i])
10032 : {
10033 74 : *new_iters_p = copy_omp_iterator (it);
10034 74 : *new_vars_p = build_decl (OMP_CLAUSE_LOCATION (c), VAR_DECL,
10035 74 : DECL_NAME (var), TREE_TYPE (var));
10036 74 : DECL_ARTIFICIAL (*new_vars_p) = 1;
10037 74 : DECL_CONTEXT (*new_vars_p) = DECL_CONTEXT (var);
10038 74 : OMP_ITERATOR_VAR (*new_iters_p) = *new_vars_p;
10039 74 : new_iters_p = &TREE_CHAIN (*new_iters_p);
10040 74 : new_vars_p = &DECL_CHAIN (*new_vars_p);
10041 74 : i++;
10042 : }
10043 : }
10044 44 : tree old_block = OMP_ITERATOR_BLOCK (OMP_CLAUSE_ITERATORS (c));
10045 44 : tree new_block = make_node (BLOCK);
10046 44 : BLOCK_VARS (new_block) = new_vars;
10047 44 : if (BLOCK_SUBBLOCKS (old_block))
10048 : {
10049 4 : BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block);
10050 4 : BLOCK_SUBBLOCKS (old_block) = NULL_TREE;
10051 : }
10052 44 : OMP_ITERATOR_BLOCK (new_iters) = new_block;
10053 44 : new_iterators.safe_push (new_iters);
10054 44 : iter_vars.safe_push (vars.copy ());
10055 44 : OMP_CLAUSE_ITERATORS (c) = new_iters;
10056 : }
10057 :
10058 : /* Remap clause to use the new variables. */
10059 64 : i = 0;
10060 170 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
10061 : {
10062 106 : tree old_var = vars[i++];
10063 106 : tree new_var = OMP_ITERATOR_VAR (it);
10064 106 : remap_omp_iterator_var (&OMP_CLAUSE_DECL (c), old_var, new_var);
10065 106 : remap_omp_iterator_var (&OMP_CLAUSE_SIZE (c), old_var, new_var);
10066 : }
10067 276 : }
10068 :
10069 27827 : for (unsigned i = 0; i < iter_vars.length (); i++)
10070 44 : iter_vars[i].release ();
10071 27783 : }
10072 :
10073 248 : struct iterator_loop_info_t
10074 : {
10075 : tree bind;
10076 : tree count;
10077 : tree index;
10078 : tree body_label;
10079 : auto_vec<tree> clauses;
10080 : };
10081 :
10082 : typedef hash_map<tree, iterator_loop_info_t> iterator_loop_info_map_t;
10083 :
10084 : /* Builds a loop to expand any OpenMP iterators in the clauses in LIST_P,
10085 : reusing any previously built loops if they use the same set of iterators.
10086 : Generated Gimple statements are placed into LOOPS_SEQ_P. The clause
10087 : iterators are updated with information on how and where to insert code into
10088 : the loop body. */
10089 :
10090 : static void
10091 27783 : build_omp_iterators_loops (tree *list_p, gimple_seq *loops_seq_p)
10092 : {
10093 27783 : iterator_loop_info_map_t loops;
10094 :
10095 81028 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10096 : {
10097 53245 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10098 52982 : continue;
10099 :
10100 263 : bool built_p;
10101 263 : iterator_loop_info_t &loop
10102 263 : = loops.get_or_insert (OMP_CLAUSE_ITERATORS (c), &built_p);
10103 :
10104 263 : if (!built_p)
10105 : {
10106 124 : loop.count = compute_omp_iterator_count (OMP_CLAUSE_ITERATORS (c),
10107 : loops_seq_p);
10108 124 : if (!loop.count)
10109 0 : continue;
10110 124 : if (integer_zerop (loop.count))
10111 8 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
10112 : "iteration count is zero");
10113 :
10114 124 : loop.bind = NULL_TREE;
10115 124 : tree *body = build_omp_iterator_loop (OMP_CLAUSE_ITERATORS (c),
10116 : loops_seq_p, &loop.bind);
10117 :
10118 124 : loop.index = create_tmp_var (sizetype);
10119 124 : SET_EXPR_LOCATION (loop.bind, OMP_CLAUSE_LOCATION (c));
10120 :
10121 : /* BEFORE LOOP: */
10122 : /* idx = -1; */
10123 : /* This should be initialized to before the individual elements,
10124 : as idx is pre-incremented in the loop body. */
10125 124 : gimple *assign = gimple_build_assign (loop.index, size_int (-1));
10126 124 : gimple_seq_add_stmt (loops_seq_p, assign);
10127 :
10128 : /* IN LOOP BODY: */
10129 : /* Create a label so we can find this point later. */
10130 124 : loop.body_label = create_artificial_label (OMP_CLAUSE_LOCATION (c));
10131 124 : tree tem = build1 (LABEL_EXPR, void_type_node, loop.body_label);
10132 124 : append_to_statement_list_force (tem, body);
10133 :
10134 : /* idx += 2; */
10135 124 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10136 : void_type_node, loop.index,
10137 : size_binop (PLUS_EXPR, loop.index, size_int (2)));
10138 124 : append_to_statement_list_force (tem, body);
10139 : }
10140 :
10141 : /* Create array to hold expanded values. */
10142 263 : tree last_count_2 = size_binop (MULT_EXPR, loop.count, size_int (2));
10143 263 : tree arr_length = size_binop (PLUS_EXPR, last_count_2, size_int (1));
10144 263 : tree elems = NULL_TREE;
10145 263 : if (TREE_CONSTANT (arr_length))
10146 : {
10147 263 : tree type = build_array_type (ptr_type_node,
10148 : build_index_type (arr_length));
10149 263 : elems = create_tmp_var_raw (type, "omp_iter_data");
10150 263 : TREE_ADDRESSABLE (elems) = 1;
10151 263 : gimple_add_tmp_var (elems);
10152 : }
10153 : else
10154 : {
10155 : /* Handle dynamic sizes. */
10156 0 : sorry ("dynamic iterator sizes not implemented yet");
10157 : }
10158 :
10159 : /* BEFORE LOOP: */
10160 : /* elems[0] = count; */
10161 263 : tree lhs = build4 (ARRAY_REF, ptr_type_node, elems, size_int (0),
10162 : NULL_TREE, NULL_TREE);
10163 263 : tree tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10164 : void_type_node, lhs, loop.count);
10165 263 : gimplify_and_add (tem, loops_seq_p);
10166 :
10167 : /* Make a copy of the iterator with extra info at the end. */
10168 263 : tree new_iterator = copy_omp_iterator (OMP_CLAUSE_ITERATORS (c), true);
10169 263 : OMP_ITERATOR_LABEL (new_iterator) = loop.body_label;
10170 263 : OMP_ITERATOR_INDEX (new_iterator) = loop.index;
10171 263 : OMP_ITERATOR_ELEMS (new_iterator) = elems;
10172 263 : TREE_CHAIN (new_iterator) = TREE_CHAIN (OMP_CLAUSE_ITERATORS (c));
10173 263 : OMP_CLAUSE_ITERATORS (c) = new_iterator;
10174 :
10175 263 : loop.clauses.safe_push (c);
10176 : }
10177 :
10178 : /* Now gimplify and add all the loops that were built. */
10179 27907 : for (hash_map<tree, iterator_loop_info_t>::iterator it = loops.begin ();
10180 55690 : it != loops.end (); ++it)
10181 124 : gimplify_and_add ((*it).second.bind, loops_seq_p);
10182 27783 : }
10183 :
10184 : /* Helper function for enter_omp_iterator_loop_context. */
10185 :
10186 : static gimple_seq *
10187 2990 : enter_omp_iterator_loop_context_1 (tree iterator, gimple_seq *loops_seq_p)
10188 : {
10189 : /* Drill into the nested bind expressions to get to the loop body. */
10190 2990 : for (gimple_stmt_iterator gsi = gsi_start (*loops_seq_p);
10191 19998 : !gsi_end_p (gsi); gsi_next (&gsi))
10192 : {
10193 19354 : gimple *stmt = gsi_stmt (gsi);
10194 :
10195 19354 : switch (gimple_code (stmt))
10196 : {
10197 2292 : case GIMPLE_BIND:
10198 2292 : {
10199 2292 : gbind *bind_stmt = as_a<gbind *> (stmt);
10200 2292 : gimple_push_bind_expr (bind_stmt);
10201 2292 : gimple_seq *bind_body_p = gimple_bind_body_ptr (bind_stmt);
10202 2292 : gimple_seq *seq =
10203 2292 : enter_omp_iterator_loop_context_1 (iterator, bind_body_p);
10204 2292 : if (seq)
10205 : return seq;
10206 644 : gimple_pop_bind_expr ();
10207 : }
10208 644 : break;
10209 3 : case GIMPLE_TRY:
10210 3 : {
10211 3 : gimple_seq *try_eval_p = gimple_try_eval_ptr (stmt);
10212 3 : gimple_seq *seq =
10213 3 : enter_omp_iterator_loop_context_1 (iterator, try_eval_p);
10214 3 : if (seq)
10215 : return seq;
10216 : }
10217 : break;
10218 3072 : case GIMPLE_LABEL:
10219 3072 : {
10220 3072 : glabel *label_stmt = as_a<glabel *> (stmt);
10221 3072 : tree label = gimple_label_label (label_stmt);
10222 3072 : if (label == OMP_ITERATOR_LABEL (iterator))
10223 : return loops_seq_p;
10224 : }
10225 : break;
10226 : default:
10227 : break;
10228 : }
10229 : }
10230 :
10231 : return NULL;
10232 : }
10233 :
10234 : /* Enter the Gimplification context in LOOPS_SEQ_P for the iterator loop
10235 : associated with OpenMP clause C. Returns the gimple_seq for the loop body
10236 : if C has OpenMP iterators, or ALT_SEQ_P if not. */
10237 :
10238 : static gimple_seq *
10239 65230 : enter_omp_iterator_loop_context (tree c, gimple_seq *loops_seq_p,
10240 : gimple_seq *alt_seq_p)
10241 : {
10242 65230 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10243 : return alt_seq_p;
10244 :
10245 695 : push_gimplify_context ();
10246 :
10247 695 : gimple_seq *seq = enter_omp_iterator_loop_context_1 (OMP_CLAUSE_ITERATORS (c),
10248 : loops_seq_p);
10249 695 : gcc_assert (seq);
10250 : return seq;
10251 : }
10252 :
10253 : /* Enter the Gimplification context in STMT for the iterator loop associated
10254 : with OpenMP clause C. Returns the gimple_seq for the loop body if C has
10255 : OpenMP iterators, or ALT_SEQ_P if not. */
10256 :
10257 : gimple_seq *
10258 434 : enter_omp_iterator_loop_context (tree c, gomp_target *stmt,
10259 : gimple_seq *alt_seq_p)
10260 : {
10261 434 : gimple_seq *loops_seq_p = gimple_omp_target_iterator_loops_ptr (stmt);
10262 434 : return enter_omp_iterator_loop_context (c, loops_seq_p, alt_seq_p);
10263 : }
10264 :
10265 : /* Exit the Gimplification context for the OpenMP clause C. */
10266 :
10267 : void
10268 65320 : exit_omp_iterator_loop_context (tree c)
10269 : {
10270 65320 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10271 : return;
10272 2343 : while (!gimplify_ctxp->bind_expr_stack.is_empty ())
10273 1648 : gimple_pop_bind_expr ();
10274 695 : pop_gimplify_context (NULL);
10275 : }
10276 :
10277 : /* If *LIST_P contains any OpenMP depend clauses with iterators,
10278 : lower all the depend clauses by populating corresponding depend
10279 : array. Returns 0 if there are no such depend clauses, or
10280 : 2 if all depend clauses should be removed, 1 otherwise. */
10281 :
10282 : static int
10283 1905 : gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
10284 : {
10285 1905 : tree c;
10286 1905 : gimple *g;
10287 1905 : size_t n[5] = { 0, 0, 0, 0, 0 };
10288 1905 : bool unused[5];
10289 1905 : tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
10290 1905 : tree last_iter = NULL_TREE, last_count = NULL_TREE;
10291 1905 : size_t i, j;
10292 1905 : location_t first_loc = UNKNOWN_LOCATION;
10293 :
10294 6250 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10295 4345 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10296 : {
10297 2207 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10298 : {
10299 : case OMP_CLAUSE_DEPEND_IN:
10300 : i = 2;
10301 : break;
10302 : case OMP_CLAUSE_DEPEND_OUT:
10303 : case OMP_CLAUSE_DEPEND_INOUT:
10304 : i = 0;
10305 : break;
10306 : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10307 : i = 1;
10308 : break;
10309 : case OMP_CLAUSE_DEPEND_DEPOBJ:
10310 : i = 3;
10311 : break;
10312 : case OMP_CLAUSE_DEPEND_INOUTSET:
10313 : i = 4;
10314 : break;
10315 0 : default:
10316 0 : gcc_unreachable ();
10317 : }
10318 2207 : tree t = OMP_CLAUSE_DECL (c);
10319 2207 : if (first_loc == UNKNOWN_LOCATION)
10320 1905 : first_loc = OMP_CLAUSE_LOCATION (c);
10321 2207 : if (OMP_ITERATOR_DECL_P (t))
10322 : {
10323 302 : if (TREE_PURPOSE (t) != last_iter)
10324 : {
10325 255 : tree tcnt = compute_omp_iterator_count (TREE_PURPOSE (t),
10326 : pre_p);
10327 255 : if (!tcnt)
10328 : return 2;
10329 255 : last_iter = TREE_PURPOSE (t);
10330 255 : last_count = tcnt;
10331 : }
10332 302 : if (counts[i] == NULL_TREE)
10333 248 : counts[i] = last_count;
10334 : else
10335 54 : counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
10336 : PLUS_EXPR, counts[i], last_count);
10337 : }
10338 : else
10339 1905 : n[i]++;
10340 : }
10341 10620 : for (i = 0; i < 5; i++)
10342 8925 : if (counts[i])
10343 : break;
10344 1905 : if (i == 5)
10345 : return 0;
10346 :
10347 210 : tree total = size_zero_node;
10348 1260 : for (i = 0; i < 5; i++)
10349 : {
10350 1050 : unused[i] = counts[i] == NULL_TREE && n[i] == 0;
10351 1050 : if (counts[i] == NULL_TREE)
10352 802 : counts[i] = size_zero_node;
10353 1050 : if (n[i])
10354 44 : counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
10355 1050 : if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
10356 : fb_rvalue) == GS_ERROR)
10357 : return 2;
10358 1050 : total = size_binop (PLUS_EXPR, total, counts[i]);
10359 : }
10360 :
10361 210 : if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
10362 : == GS_ERROR)
10363 : return 2;
10364 210 : bool is_old = unused[1] && unused[3] && unused[4];
10365 210 : tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
10366 : size_int (is_old ? 1 : 4));
10367 210 : if (!unused[4])
10368 3 : totalpx = size_binop (PLUS_EXPR, totalpx,
10369 : size_binop (MULT_EXPR, counts[4], size_int (2)));
10370 210 : tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
10371 210 : tree array = create_tmp_var_raw (type);
10372 210 : TREE_ADDRESSABLE (array) = 1;
10373 210 : if (!poly_int_tree_p (totalpx))
10374 : {
10375 83 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
10376 83 : gimplify_type_sizes (TREE_TYPE (array), pre_p);
10377 83 : if (gimplify_omp_ctxp)
10378 : {
10379 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10380 : while (ctx
10381 22 : && (ctx->region_type == ORT_WORKSHARE
10382 : || ctx->region_type == ORT_TASKGROUP
10383 22 : || ctx->region_type == ORT_SIMD
10384 22 : || ctx->region_type == ORT_ACC))
10385 0 : ctx = ctx->outer_context;
10386 22 : if (ctx)
10387 22 : omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
10388 : }
10389 83 : gimplify_vla_decl (array, pre_p);
10390 : }
10391 : else
10392 127 : gimple_add_tmp_var (array);
10393 210 : tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
10394 : NULL_TREE);
10395 210 : tree tem;
10396 210 : if (!is_old)
10397 : {
10398 31 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10399 : build_int_cst (ptr_type_node, 0));
10400 31 : gimplify_and_add (tem, pre_p);
10401 31 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
10402 : NULL_TREE);
10403 : }
10404 210 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10405 : fold_convert (ptr_type_node, total));
10406 210 : gimplify_and_add (tem, pre_p);
10407 816 : for (i = 1; i < (is_old ? 2 : 4); i++)
10408 : {
10409 272 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
10410 : NULL_TREE, NULL_TREE);
10411 272 : tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
10412 272 : gimplify_and_add (tem, pre_p);
10413 : }
10414 :
10415 : tree cnts[6];
10416 740 : for (j = 5; j; j--)
10417 740 : if (!unused[j - 1])
10418 : break;
10419 1260 : for (i = 0; i < 5; i++)
10420 : {
10421 1050 : if (i && (i >= j || unused[i - 1]))
10422 : {
10423 790 : cnts[i] = cnts[i - 1];
10424 790 : continue;
10425 : }
10426 260 : cnts[i] = create_tmp_var (sizetype);
10427 260 : if (i == 0)
10428 241 : g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
10429 : else
10430 : {
10431 50 : tree t;
10432 50 : if (is_old)
10433 38 : t = size_binop (PLUS_EXPR, counts[0], size_int (2));
10434 : else
10435 12 : t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
10436 50 : if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
10437 : == GS_ERROR)
10438 0 : return 2;
10439 50 : g = gimple_build_assign (cnts[i], t);
10440 : }
10441 260 : gimple_seq_add_stmt (pre_p, g);
10442 : }
10443 210 : if (unused[4])
10444 207 : cnts[5] = NULL_TREE;
10445 : else
10446 : {
10447 3 : tree t = size_binop (PLUS_EXPR, total, size_int (5));
10448 3 : cnts[5] = create_tmp_var (sizetype);
10449 3 : g = gimple_build_assign (cnts[i], t);
10450 3 : gimple_seq_add_stmt (pre_p, g);
10451 : }
10452 :
10453 210 : last_iter = NULL_TREE;
10454 210 : tree last_bind = NULL_TREE;
10455 210 : tree *last_body = NULL;
10456 573 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10457 363 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10458 : {
10459 348 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10460 : {
10461 : case OMP_CLAUSE_DEPEND_IN:
10462 : i = 2;
10463 : break;
10464 : case OMP_CLAUSE_DEPEND_OUT:
10465 : case OMP_CLAUSE_DEPEND_INOUT:
10466 : i = 0;
10467 : break;
10468 : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10469 : i = 1;
10470 : break;
10471 : case OMP_CLAUSE_DEPEND_DEPOBJ:
10472 : i = 3;
10473 : break;
10474 : case OMP_CLAUSE_DEPEND_INOUTSET:
10475 : i = 4;
10476 : break;
10477 0 : default:
10478 0 : gcc_unreachable ();
10479 : }
10480 348 : tree t = OMP_CLAUSE_DECL (c);
10481 348 : if (OMP_ITERATOR_DECL_P (t))
10482 : {
10483 302 : if (TREE_PURPOSE (t) != last_iter)
10484 : {
10485 255 : last_body = build_omp_iterator_loop (TREE_PURPOSE (t), pre_p,
10486 : &last_bind);
10487 255 : SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
10488 : }
10489 302 : last_iter = TREE_PURPOSE (t);
10490 302 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
10491 : {
10492 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
10493 : 0), last_body);
10494 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
10495 : }
10496 302 : if (error_operand_p (TREE_VALUE (t)))
10497 : return 2;
10498 302 : if (TREE_VALUE (t) != null_pointer_node)
10499 296 : TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
10500 302 : if (i == 4)
10501 : {
10502 3 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10503 : NULL_TREE, NULL_TREE);
10504 3 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10505 : NULL_TREE, NULL_TREE);
10506 3 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10507 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10508 : void_type_node, r, r2);
10509 3 : append_to_statement_list_force (tem, last_body);
10510 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10511 : void_type_node, cnts[i],
10512 : size_binop (PLUS_EXPR, cnts[i],
10513 : size_int (1)));
10514 3 : append_to_statement_list_force (tem, last_body);
10515 3 : i = 5;
10516 : }
10517 302 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10518 : NULL_TREE, NULL_TREE);
10519 604 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10520 302 : void_type_node, r, TREE_VALUE (t));
10521 302 : append_to_statement_list_force (tem, last_body);
10522 302 : if (i == 5)
10523 : {
10524 3 : r = build4 (ARRAY_REF, ptr_type_node, array,
10525 : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10526 : NULL_TREE, NULL_TREE);
10527 3 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10528 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10529 : void_type_node, r, tem);
10530 3 : append_to_statement_list_force (tem, last_body);
10531 : }
10532 302 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10533 : void_type_node, cnts[i],
10534 302 : size_binop (PLUS_EXPR, cnts[i],
10535 : size_int (1 + (i == 5))));
10536 302 : append_to_statement_list_force (tem, last_body);
10537 302 : TREE_VALUE (t) = null_pointer_node;
10538 : }
10539 : else
10540 : {
10541 46 : if (last_bind)
10542 : {
10543 18 : gimplify_and_add (last_bind, pre_p);
10544 18 : last_bind = NULL_TREE;
10545 : }
10546 46 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
10547 : {
10548 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
10549 : NULL, is_gimple_val, fb_rvalue);
10550 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
10551 : }
10552 46 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
10553 : return 2;
10554 46 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
10555 46 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
10556 46 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
10557 : is_gimple_val, fb_rvalue) == GS_ERROR)
10558 : return 2;
10559 46 : if (i == 4)
10560 : {
10561 0 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10562 : NULL_TREE, NULL_TREE);
10563 0 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10564 : NULL_TREE, NULL_TREE);
10565 0 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10566 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
10567 0 : gimplify_and_add (tem, pre_p);
10568 0 : g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
10569 : cnts[i],
10570 : size_int (1)));
10571 0 : gimple_seq_add_stmt (pre_p, g);
10572 0 : i = 5;
10573 : }
10574 46 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10575 : NULL_TREE, NULL_TREE);
10576 46 : tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
10577 46 : gimplify_and_add (tem, pre_p);
10578 46 : if (i == 5)
10579 : {
10580 0 : r = build4 (ARRAY_REF, ptr_type_node, array,
10581 : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10582 : NULL_TREE, NULL_TREE);
10583 0 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10584 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
10585 0 : append_to_statement_list_force (tem, last_body);
10586 0 : gimplify_and_add (tem, pre_p);
10587 : }
10588 46 : g = gimple_build_assign (cnts[i],
10589 46 : size_binop (PLUS_EXPR, cnts[i],
10590 : size_int (1 + (i == 5))));
10591 46 : gimple_seq_add_stmt (pre_p, g);
10592 : }
10593 : }
10594 210 : if (last_bind)
10595 206 : gimplify_and_add (last_bind, pre_p);
10596 210 : tree cond = boolean_false_node;
10597 210 : if (is_old)
10598 : {
10599 179 : if (!unused[0])
10600 92 : cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
10601 : size_binop_loc (first_loc, PLUS_EXPR, counts[0],
10602 : size_int (2)));
10603 179 : if (!unused[2])
10604 125 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10605 : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10606 : cnts[2],
10607 : size_binop_loc (first_loc, PLUS_EXPR,
10608 : totalpx,
10609 : size_int (1))));
10610 : }
10611 : else
10612 : {
10613 31 : tree prev = size_int (5);
10614 186 : for (i = 0; i < 5; i++)
10615 : {
10616 155 : if (unused[i])
10617 112 : continue;
10618 43 : prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
10619 43 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10620 : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10621 : cnts[i], unshare_expr (prev)));
10622 : }
10623 : }
10624 210 : tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
10625 : build_call_expr_loc (first_loc,
10626 : builtin_decl_explicit (BUILT_IN_TRAP),
10627 : 0), void_node);
10628 210 : gimplify_and_add (tem, pre_p);
10629 210 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
10630 210 : OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
10631 210 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
10632 210 : OMP_CLAUSE_CHAIN (c) = *list_p;
10633 210 : *list_p = c;
10634 210 : return 1;
10635 : }
10636 :
10637 : /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor. */
10638 :
10639 : static bool
10640 126082 : omp_map_clause_descriptor_p (tree c)
10641 : {
10642 126082 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10643 : return false;
10644 :
10645 126078 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
10646 : return true;
10647 :
10648 82860 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
10649 74822 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
10650 83342 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
10651 6135 : return true;
10652 :
10653 : return false;
10654 : }
10655 :
10656 : /* For a set of mappings describing an array section pointed to by a struct
10657 : (or derived type, etc.) component, create an "alloc" or "release" node to
10658 : insert into a list following a GOMP_MAP_STRUCT node. For some types of
10659 : mapping (e.g. Fortran arrays with descriptors), an additional mapping may
10660 : be created that is inserted into the list of mapping nodes attached to the
10661 : directive being processed -- not part of the sorted list of nodes after
10662 : GOMP_MAP_STRUCT.
10663 :
10664 : CODE is the code of the directive being processed. GRP_START and GRP_END
10665 : are the first and last of two or three nodes representing this array section
10666 : mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
10667 : GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER). EXTRA_NODE is
10668 : filled with the additional node described above, if needed.
10669 :
10670 : This function does not add the new nodes to any lists itself. It is the
10671 : responsibility of the caller to do that. */
10672 :
10673 : static tree
10674 1729 : build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
10675 : tree *extra_node)
10676 : {
10677 1524 : enum gomp_map_kind mkind
10678 1729 : = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
10679 1729 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
10680 :
10681 1729 : gcc_assert (grp_start != grp_end);
10682 :
10683 1729 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10684 1729 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
10685 1729 : OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
10686 1729 : OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
10687 1729 : tree grp_mid = NULL_TREE;
10688 1729 : if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
10689 197 : grp_mid = OMP_CLAUSE_CHAIN (grp_start);
10690 :
10691 197 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
10692 0 : OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
10693 : else
10694 1729 : OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
10695 :
10696 1729 : if (grp_mid
10697 197 : && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
10698 1926 : && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
10699 : {
10700 0 : tree c3
10701 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10702 0 : OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
10703 0 : OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
10704 0 : OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
10705 0 : OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
10706 :
10707 0 : *extra_node = c3;
10708 : }
10709 : else
10710 1729 : *extra_node = NULL_TREE;
10711 :
10712 1729 : return c2;
10713 : }
10714 :
10715 : /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
10716 : and set *BITPOSP and *POFFSETP to the bit offset of the access.
10717 : If BASE_REF is non-NULL and the containing object is a reference, set
10718 : *BASE_REF to that reference before dereferencing the object.
10719 : If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
10720 : has array type, else return NULL. */
10721 :
10722 : static tree
10723 7741 : extract_base_bit_offset (tree base, poly_int64 *bitposp,
10724 : poly_offset_int *poffsetp,
10725 : bool *variable_offset,
10726 : tree iterator)
10727 : {
10728 7741 : tree offset;
10729 7741 : poly_int64 bitsize, bitpos;
10730 7741 : machine_mode mode;
10731 7741 : int unsignedp, reversep, volatilep = 0;
10732 7741 : poly_offset_int poffset;
10733 :
10734 7741 : STRIP_NOPS (base);
10735 :
10736 7741 : if (iterator)
10737 : {
10738 : /* Replace any iterator variables with the lower bound of the iterator.
10739 : This will give us the nominal offset and bit position of the first
10740 : element, which is all we should need to lay out the mappings. The
10741 : actual locations of the iterated mappings are elsewhere. */
10742 : tree it;
10743 72 : for (it = iterator; it; it = TREE_CHAIN (it))
10744 39 : base = simplify_replace_tree (base, OMP_ITERATOR_VAR (it),
10745 39 : OMP_ITERATOR_BEGIN (it));
10746 : }
10747 :
10748 7741 : base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
10749 : &unsignedp, &reversep, &volatilep);
10750 :
10751 7741 : STRIP_NOPS (base);
10752 :
10753 7741 : if (offset && poly_int_tree_p (offset))
10754 : {
10755 0 : poffset = wi::to_poly_offset (offset);
10756 0 : *variable_offset = false;
10757 : }
10758 : else
10759 : {
10760 7741 : poffset = 0;
10761 7741 : *variable_offset = (offset != NULL_TREE);
10762 : }
10763 :
10764 7741 : if (maybe_ne (bitpos, 0))
10765 5627 : poffset += bits_to_bytes_round_down (bitpos);
10766 :
10767 7741 : *bitposp = bitpos;
10768 7741 : *poffsetp = poffset;
10769 :
10770 7741 : return base;
10771 : }
10772 :
10773 : /* Used for topological sorting of mapping groups. UNVISITED means we haven't
10774 : started processing the group yet. The TEMPORARY mark is used when we first
10775 : encounter a group on a depth-first traversal, and the PERMANENT mark is used
10776 : when we have processed all the group's children (i.e. all the base pointers
10777 : referred to by the group's mapping nodes, recursively). */
10778 :
10779 : enum omp_tsort_mark {
10780 : UNVISITED,
10781 : TEMPORARY,
10782 : PERMANENT
10783 : };
10784 :
10785 : /* Hash for trees based on operand_equal_p. Like tree_operand_hash
10786 : but ignores side effects in the equality comparisons. */
10787 :
10788 : struct tree_operand_hash_no_se : tree_operand_hash
10789 : {
10790 : static inline bool equal (const value_type &,
10791 : const compare_type &);
10792 : };
10793 :
10794 : inline bool
10795 415934 : tree_operand_hash_no_se::equal (const value_type &t1,
10796 : const compare_type &t2)
10797 : {
10798 415934 : return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
10799 : }
10800 :
10801 : /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
10802 : clause. */
10803 :
10804 : struct omp_mapping_group {
10805 : tree *grp_start;
10806 : tree grp_end;
10807 : omp_tsort_mark mark;
10808 : /* If we've removed the group but need to reindex, mark the group as
10809 : deleted. */
10810 : bool deleted;
10811 : /* The group points to an already-created "GOMP_MAP_STRUCT
10812 : GOMP_MAP_ATTACH_DETACH" pair. */
10813 : bool reprocess_struct;
10814 : /* The group should use "zero-length" allocations for pointers that are not
10815 : mapped "to" on the same directive. */
10816 : bool fragile;
10817 : struct omp_mapping_group *sibling;
10818 : struct omp_mapping_group *next;
10819 : };
10820 :
10821 : DEBUG_FUNCTION void
10822 0 : debug_mapping_group (omp_mapping_group *grp)
10823 : {
10824 0 : tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
10825 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
10826 0 : debug_generic_expr (*grp->grp_start);
10827 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
10828 0 : }
10829 :
10830 : /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
10831 : isn't one. */
10832 :
10833 : static tree
10834 36610 : omp_get_base_pointer (tree expr)
10835 : {
10836 36610 : while (TREE_CODE (expr) == ARRAY_REF
10837 43947 : || TREE_CODE (expr) == COMPONENT_REF)
10838 7337 : expr = TREE_OPERAND (expr, 0);
10839 :
10840 36610 : if (INDIRECT_REF_P (expr)
10841 36610 : || (TREE_CODE (expr) == MEM_REF
10842 1 : && integer_zerop (TREE_OPERAND (expr, 1))))
10843 : {
10844 10597 : expr = TREE_OPERAND (expr, 0);
10845 10634 : while (TREE_CODE (expr) == COMPOUND_EXPR)
10846 37 : expr = TREE_OPERAND (expr, 1);
10847 10597 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
10848 841 : expr = TREE_OPERAND (expr, 0);
10849 10597 : if (TREE_CODE (expr) == SAVE_EXPR)
10850 31 : expr = TREE_OPERAND (expr, 0);
10851 10597 : STRIP_NOPS (expr);
10852 10597 : return expr;
10853 : }
10854 :
10855 : return NULL_TREE;
10856 : }
10857 :
10858 : /* An attach or detach operation depends directly on the address being
10859 : attached/detached. Return that address, or none if there are no
10860 : attachments/detachments. */
10861 :
10862 : static tree
10863 16967 : omp_get_attachment (omp_mapping_group *grp)
10864 : {
10865 16967 : tree node = *grp->grp_start;
10866 :
10867 16967 : switch (OMP_CLAUSE_MAP_KIND (node))
10868 : {
10869 14007 : case GOMP_MAP_TO:
10870 14007 : case GOMP_MAP_FROM:
10871 14007 : case GOMP_MAP_TOFROM:
10872 14007 : case GOMP_MAP_ALWAYS_FROM:
10873 14007 : case GOMP_MAP_ALWAYS_TO:
10874 14007 : case GOMP_MAP_ALWAYS_TOFROM:
10875 14007 : case GOMP_MAP_FORCE_FROM:
10876 14007 : case GOMP_MAP_FORCE_TO:
10877 14007 : case GOMP_MAP_FORCE_TOFROM:
10878 14007 : case GOMP_MAP_FORCE_PRESENT:
10879 14007 : case GOMP_MAP_PRESENT_ALLOC:
10880 14007 : case GOMP_MAP_PRESENT_FROM:
10881 14007 : case GOMP_MAP_PRESENT_TO:
10882 14007 : case GOMP_MAP_PRESENT_TOFROM:
10883 14007 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
10884 14007 : case GOMP_MAP_ALWAYS_PRESENT_TO:
10885 14007 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10886 14007 : case GOMP_MAP_ALLOC:
10887 14007 : case GOMP_MAP_RELEASE:
10888 14007 : case GOMP_MAP_DELETE:
10889 14007 : case GOMP_MAP_FORCE_ALLOC:
10890 14007 : if (node == grp->grp_end)
10891 : return NULL_TREE;
10892 :
10893 6644 : node = OMP_CLAUSE_CHAIN (node);
10894 6644 : if (node && omp_map_clause_descriptor_p (node))
10895 : {
10896 1038 : gcc_assert (node != grp->grp_end);
10897 1038 : node = OMP_CLAUSE_CHAIN (node);
10898 : }
10899 6644 : if (node)
10900 6644 : switch (OMP_CLAUSE_MAP_KIND (node))
10901 : {
10902 : case GOMP_MAP_POINTER:
10903 : case GOMP_MAP_ALWAYS_POINTER:
10904 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10905 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10906 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10907 : case GOMP_MAP_USES_ALLOCATORS:
10908 : return NULL_TREE;
10909 :
10910 2559 : case GOMP_MAP_ATTACH_DETACH:
10911 2559 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10912 2559 : case GOMP_MAP_DETACH:
10913 2559 : return OMP_CLAUSE_DECL (node);
10914 :
10915 0 : default:
10916 0 : internal_error ("unexpected mapping node");
10917 : }
10918 0 : return error_mark_node;
10919 :
10920 0 : case GOMP_MAP_TO_PSET:
10921 0 : gcc_assert (node != grp->grp_end);
10922 0 : node = OMP_CLAUSE_CHAIN (node);
10923 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10924 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10925 0 : return OMP_CLAUSE_DECL (node);
10926 : else
10927 0 : internal_error ("unexpected mapping node");
10928 : return error_mark_node;
10929 :
10930 541 : case GOMP_MAP_ATTACH:
10931 541 : case GOMP_MAP_DETACH:
10932 541 : node = OMP_CLAUSE_CHAIN (node);
10933 541 : if (!node || *grp->grp_start == grp->grp_end)
10934 541 : return OMP_CLAUSE_DECL (*grp->grp_start);
10935 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10936 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10937 0 : return OMP_CLAUSE_DECL (*grp->grp_start);
10938 : else
10939 0 : internal_error ("unexpected mapping node");
10940 : return error_mark_node;
10941 :
10942 : case GOMP_MAP_STRUCT:
10943 : case GOMP_MAP_STRUCT_UNORD:
10944 : case GOMP_MAP_FORCE_DEVICEPTR:
10945 : case GOMP_MAP_DEVICE_RESIDENT:
10946 : case GOMP_MAP_LINK:
10947 : case GOMP_MAP_IF_PRESENT:
10948 : case GOMP_MAP_FIRSTPRIVATE:
10949 : case GOMP_MAP_FIRSTPRIVATE_INT:
10950 : case GOMP_MAP_USE_DEVICE_PTR:
10951 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10952 : case GOMP_MAP_USES_ALLOCATORS:
10953 : return NULL_TREE;
10954 :
10955 0 : default:
10956 0 : internal_error ("unexpected mapping node");
10957 : }
10958 :
10959 : return error_mark_node;
10960 : }
10961 :
10962 : /* Given a pointer START_P to the start of a group of related (e.g. pointer)
10963 : mappings, return the chain pointer to the end of that group in the list. */
10964 :
10965 : static tree *
10966 111756 : omp_group_last (tree *start_p)
10967 : {
10968 111756 : tree c = *start_p, nc, *grp_last_p = start_p;
10969 :
10970 111756 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
10971 :
10972 111756 : nc = OMP_CLAUSE_CHAIN (c);
10973 :
10974 194853 : if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
10975 : return grp_last_p;
10976 :
10977 70860 : switch (OMP_CLAUSE_MAP_KIND (c))
10978 : {
10979 : default:
10980 : while (nc
10981 113864 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10982 239143 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10983 106686 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10984 95997 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
10985 83170 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
10986 57082 : || (OMP_CLAUSE_MAP_KIND (nc)
10987 : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10988 57066 : || (OMP_CLAUSE_MAP_KIND (nc)
10989 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
10990 56699 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
10991 56644 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
10992 54732 : || omp_map_clause_descriptor_p (nc)))
10993 : {
10994 67055 : tree nc2 = OMP_CLAUSE_CHAIN (nc);
10995 67055 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
10996 : {
10997 : /* In the specific case we're doing "exit data" on an array
10998 : slice of a reference-to-pointer struct component, we will see
10999 : DETACH followed by ATTACH_DETACH here. We want to treat that
11000 : as a single group. In other cases DETACH might represent a
11001 : stand-alone "detach" clause, so we don't want to consider
11002 : that part of the group. */
11003 55 : if (nc2
11004 16 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
11005 71 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
11006 0 : goto consume_two_nodes;
11007 : else
11008 : break;
11009 : }
11010 67000 : if (nc2
11011 49076 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
11012 42563 : && (OMP_CLAUSE_MAP_KIND (nc)
11013 : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
11014 67016 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
11015 : {
11016 16 : consume_two_nodes:
11017 16 : grp_last_p = &OMP_CLAUSE_CHAIN (nc);
11018 16 : c = nc2;
11019 16 : nc = OMP_CLAUSE_CHAIN (nc2);
11020 : }
11021 : else
11022 : {
11023 66984 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11024 66984 : c = nc;
11025 66984 : nc = nc2;
11026 : }
11027 : }
11028 : break;
11029 :
11030 302 : case GOMP_MAP_ATTACH:
11031 302 : case GOMP_MAP_DETACH:
11032 : /* This is a weird artifact of how directives are parsed: bare attach or
11033 : detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
11034 : FIRSTPRIVATE_REFERENCE node. FIXME. */
11035 302 : if (nc
11036 302 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11037 302 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11038 302 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
11039 0 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11040 : break;
11041 :
11042 39 : case GOMP_MAP_TO_PSET:
11043 39 : if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11044 39 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
11045 12 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
11046 39 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11047 : break;
11048 :
11049 5727 : case GOMP_MAP_STRUCT:
11050 5727 : case GOMP_MAP_STRUCT_UNORD:
11051 5727 : {
11052 5727 : unsigned HOST_WIDE_INT num_mappings
11053 5727 : = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
11054 5727 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
11055 5389 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11056 10634 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
11057 1134 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11058 14397 : for (unsigned i = 0; i < num_mappings; i++)
11059 8670 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11060 : }
11061 : break;
11062 : }
11063 :
11064 : return grp_last_p;
11065 : }
11066 :
11067 : /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
11068 : OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
11069 : associated GOMP_MAP_POINTER mappings). Return a vector of omp_mapping_group
11070 : if we have more than one such group, else return NULL. */
11071 :
11072 : static void
11073 90013 : omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
11074 : tree gather_sentinel)
11075 : {
11076 90013 : for (tree *cp = list_p;
11077 269028 : *cp && *cp != gather_sentinel;
11078 179015 : cp = &OMP_CLAUSE_CHAIN (*cp))
11079 : {
11080 179015 : if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
11081 99099 : continue;
11082 :
11083 79916 : tree *grp_last_p = omp_group_last (cp);
11084 79916 : omp_mapping_group grp;
11085 :
11086 79916 : grp.grp_start = cp;
11087 79916 : grp.grp_end = *grp_last_p;
11088 79916 : grp.mark = UNVISITED;
11089 79916 : grp.sibling = NULL;
11090 79916 : grp.deleted = false;
11091 79916 : grp.reprocess_struct = false;
11092 79916 : grp.fragile = false;
11093 79916 : grp.next = NULL;
11094 79916 : groups->safe_push (grp);
11095 :
11096 79916 : cp = grp_last_p;
11097 : }
11098 90013 : }
11099 :
11100 : static vec<omp_mapping_group> *
11101 89695 : omp_gather_mapping_groups (tree *list_p)
11102 : {
11103 89695 : vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
11104 :
11105 89695 : omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
11106 :
11107 89695 : if (groups->length () > 0)
11108 : return groups;
11109 : else
11110 : {
11111 44716 : delete groups;
11112 44716 : return NULL;
11113 : }
11114 : }
11115 :
11116 : /* A pointer mapping group GRP may define a block of memory starting at some
11117 : base address, and maybe also define a firstprivate pointer or firstprivate
11118 : reference that points to that block. The return value is a node containing
11119 : the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
11120 : If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
11121 : return the number of consecutive chained nodes in CHAINED. */
11122 :
11123 : static tree
11124 80310 : omp_group_base (omp_mapping_group *grp, unsigned int *chained,
11125 : tree *firstprivate)
11126 : {
11127 80310 : tree node = *grp->grp_start;
11128 :
11129 80310 : *firstprivate = NULL_TREE;
11130 80310 : *chained = 1;
11131 :
11132 80310 : switch (OMP_CLAUSE_MAP_KIND (node))
11133 : {
11134 76112 : case GOMP_MAP_TO:
11135 76112 : case GOMP_MAP_FROM:
11136 76112 : case GOMP_MAP_TOFROM:
11137 76112 : case GOMP_MAP_ALWAYS_FROM:
11138 76112 : case GOMP_MAP_ALWAYS_TO:
11139 76112 : case GOMP_MAP_ALWAYS_TOFROM:
11140 76112 : case GOMP_MAP_FORCE_FROM:
11141 76112 : case GOMP_MAP_FORCE_TO:
11142 76112 : case GOMP_MAP_FORCE_TOFROM:
11143 76112 : case GOMP_MAP_FORCE_PRESENT:
11144 76112 : case GOMP_MAP_PRESENT_ALLOC:
11145 76112 : case GOMP_MAP_PRESENT_FROM:
11146 76112 : case GOMP_MAP_PRESENT_TO:
11147 76112 : case GOMP_MAP_PRESENT_TOFROM:
11148 76112 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
11149 76112 : case GOMP_MAP_ALWAYS_PRESENT_TO:
11150 76112 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
11151 76112 : case GOMP_MAP_ALLOC:
11152 76112 : case GOMP_MAP_RELEASE:
11153 76112 : case GOMP_MAP_DELETE:
11154 76112 : case GOMP_MAP_FORCE_ALLOC:
11155 76112 : case GOMP_MAP_IF_PRESENT:
11156 76112 : if (node == grp->grp_end)
11157 : return node;
11158 :
11159 34849 : node = OMP_CLAUSE_CHAIN (node);
11160 34849 : if (!node)
11161 0 : internal_error ("unexpected mapping node");
11162 34849 : if (omp_map_clause_descriptor_p (node))
11163 : {
11164 10754 : if (node == grp->grp_end)
11165 0 : return *grp->grp_start;
11166 10754 : node = OMP_CLAUSE_CHAIN (node);
11167 : }
11168 34849 : switch (OMP_CLAUSE_MAP_KIND (node))
11169 : {
11170 23536 : case GOMP_MAP_POINTER:
11171 23536 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11172 23536 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11173 23536 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11174 23536 : *firstprivate = OMP_CLAUSE_DECL (node);
11175 23536 : return *grp->grp_start;
11176 :
11177 11313 : case GOMP_MAP_ALWAYS_POINTER:
11178 11313 : case GOMP_MAP_ATTACH_DETACH:
11179 11313 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11180 11313 : case GOMP_MAP_DETACH:
11181 11313 : return *grp->grp_start;
11182 :
11183 0 : default:
11184 0 : internal_error ("unexpected mapping node");
11185 : }
11186 : return error_mark_node;
11187 :
11188 26 : case GOMP_MAP_TO_PSET:
11189 26 : gcc_assert (node != grp->grp_end);
11190 26 : node = OMP_CLAUSE_CHAIN (node);
11191 26 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
11192 26 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
11193 : return NULL_TREE;
11194 : else
11195 0 : internal_error ("unexpected mapping node");
11196 : return error_mark_node;
11197 :
11198 1109 : case GOMP_MAP_ATTACH:
11199 1109 : case GOMP_MAP_DETACH:
11200 1109 : node = OMP_CLAUSE_CHAIN (node);
11201 1109 : if (!node || *grp->grp_start == grp->grp_end)
11202 : return NULL_TREE;
11203 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11204 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11205 : {
11206 : /* We're mapping the base pointer itself in a bare attach or detach
11207 : node. This is a side effect of how parsing works, and the mapping
11208 : will be removed anyway (at least for enter/exit data directives).
11209 : We should ignore the mapping here. FIXME. */
11210 : return NULL_TREE;
11211 : }
11212 : else
11213 0 : internal_error ("unexpected mapping node");
11214 : return error_mark_node;
11215 :
11216 2779 : case GOMP_MAP_STRUCT:
11217 2779 : case GOMP_MAP_STRUCT_UNORD:
11218 2779 : {
11219 2779 : unsigned HOST_WIDE_INT num_mappings
11220 2779 : = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
11221 2779 : node = OMP_CLAUSE_CHAIN (node);
11222 2779 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11223 2779 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11224 : {
11225 393 : *firstprivate = OMP_CLAUSE_DECL (node);
11226 393 : node = OMP_CLAUSE_CHAIN (node);
11227 : }
11228 2386 : else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
11229 314 : node = OMP_CLAUSE_CHAIN (node);
11230 2779 : *chained = num_mappings;
11231 2779 : return node;
11232 : }
11233 :
11234 : case GOMP_MAP_FORCE_DEVICEPTR:
11235 : case GOMP_MAP_DEVICE_RESIDENT:
11236 : case GOMP_MAP_LINK:
11237 : case GOMP_MAP_FIRSTPRIVATE:
11238 : case GOMP_MAP_FIRSTPRIVATE_INT:
11239 : case GOMP_MAP_USE_DEVICE_PTR:
11240 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11241 : case GOMP_MAP_USES_ALLOCATORS:
11242 : return NULL_TREE;
11243 :
11244 0 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11245 0 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11246 0 : case GOMP_MAP_POINTER:
11247 0 : case GOMP_MAP_ALWAYS_POINTER:
11248 0 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11249 : /* These shouldn't appear by themselves. */
11250 0 : if (!seen_error ())
11251 0 : internal_error ("unexpected pointer mapping node");
11252 0 : return error_mark_node;
11253 :
11254 0 : default:
11255 0 : gcc_unreachable ();
11256 : }
11257 :
11258 : return error_mark_node;
11259 : }
11260 :
11261 : /* Given a vector of omp_mapping_groups, build a hash table so we can look up
11262 : nodes by tree_operand_hash_no_se. */
11263 :
11264 : static void
11265 45463 : omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
11266 : omp_mapping_group *> *grpmap,
11267 : vec<omp_mapping_group> *groups,
11268 : tree reindex_sentinel)
11269 : {
11270 45463 : omp_mapping_group *grp;
11271 45463 : unsigned int i;
11272 45463 : bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
11273 :
11274 126227 : FOR_EACH_VEC_ELT (*groups, i, grp)
11275 : {
11276 80764 : if (reindexing && *grp->grp_start == reindex_sentinel)
11277 80764 : above_hwm = true;
11278 :
11279 80764 : if (reindexing && !above_hwm)
11280 56835 : continue;
11281 :
11282 80464 : if (grp->reprocess_struct)
11283 154 : continue;
11284 :
11285 80310 : tree fpp;
11286 80310 : unsigned int chained;
11287 80310 : tree node = omp_group_base (grp, &chained, &fpp);
11288 :
11289 80310 : if (node == error_mark_node || (!node && !fpp))
11290 1419 : continue;
11291 :
11292 : for (unsigned j = 0;
11293 159226 : node && j < chained;
11294 80335 : node = OMP_CLAUSE_CHAIN (node), j++)
11295 : {
11296 80335 : tree decl = OMP_CLAUSE_DECL (node);
11297 : /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
11298 : meaning node-hash lookups don't work. This is a workaround for
11299 : that, but ideally we should just create the INDIRECT_REF at
11300 : source instead. FIXME. */
11301 80335 : if (TREE_CODE (decl) == MEM_REF
11302 80335 : && integer_zerop (TREE_OPERAND (decl, 1)))
11303 0 : decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
11304 :
11305 80335 : omp_mapping_group **prev = grpmap->get (decl);
11306 :
11307 80335 : if (prev && *prev == grp)
11308 : /* Empty. */;
11309 80335 : else if (prev)
11310 : {
11311 : /* Mapping the same thing twice is normally diagnosed as an error,
11312 : but can happen under some circumstances, e.g. in pr99928-16.c,
11313 : the directive:
11314 :
11315 : #pragma omp target simd reduction(+:a[:3]) \
11316 : map(always, tofrom: a[:6])
11317 : ...
11318 :
11319 : will result in two "a[0]" mappings (of different sizes). */
11320 :
11321 326 : grp->sibling = (*prev)->sibling;
11322 326 : (*prev)->sibling = grp;
11323 : }
11324 : else
11325 80009 : grpmap->put (decl, grp);
11326 : }
11327 :
11328 78891 : if (!fpp)
11329 54962 : continue;
11330 :
11331 23929 : omp_mapping_group **prev = grpmap->get (fpp);
11332 23929 : if (prev && *prev != grp)
11333 : {
11334 12 : grp->sibling = (*prev)->sibling;
11335 12 : (*prev)->sibling = grp;
11336 : }
11337 : else
11338 23917 : grpmap->put (fpp, grp);
11339 : }
11340 45463 : }
11341 :
11342 : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11343 44979 : omp_index_mapping_groups (vec<omp_mapping_group> *groups)
11344 : {
11345 44979 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11346 44979 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11347 :
11348 44979 : omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
11349 :
11350 44979 : return grpmap;
11351 : }
11352 :
11353 : /* Rebuild group map from partially-processed clause list (during
11354 : omp_build_struct_sibling_lists). We have already processed nodes up until
11355 : a high-water mark (HWM). This is a bit tricky because the list is being
11356 : reordered as it is scanned, but we know:
11357 :
11358 : 1. The list after HWM has not been touched yet, so we can reindex it safely.
11359 :
11360 : 2. The list before and including HWM has been altered, but remains
11361 : well-formed throughout the sibling-list building operation.
11362 :
11363 : so, we can do the reindex operation in two parts, on the processed and
11364 : then the unprocessed halves of the list. */
11365 :
11366 : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11367 318 : omp_reindex_mapping_groups (tree *list_p,
11368 : vec<omp_mapping_group> *groups,
11369 : vec<omp_mapping_group> *processed_groups,
11370 : tree sentinel)
11371 : {
11372 318 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11373 318 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11374 :
11375 318 : processed_groups->truncate (0);
11376 :
11377 318 : omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
11378 318 : omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
11379 318 : if (sentinel)
11380 166 : omp_index_mapping_groups_1 (grpmap, groups, sentinel);
11381 :
11382 318 : return grpmap;
11383 : }
11384 :
11385 : /* Find the immediately-containing struct for a component ref (etc.)
11386 : expression EXPR. */
11387 :
11388 : static tree
11389 48151 : omp_containing_struct (tree expr)
11390 : {
11391 48151 : tree expr0 = expr;
11392 :
11393 48151 : STRIP_NOPS (expr);
11394 :
11395 : /* Note: don't strip NOPs unless we're also stripping off array refs or a
11396 : component ref. */
11397 48151 : if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
11398 : return expr0;
11399 :
11400 27293 : while (TREE_CODE (expr) == ARRAY_REF)
11401 3706 : expr = TREE_OPERAND (expr, 0);
11402 :
11403 23587 : if (TREE_CODE (expr) == COMPONENT_REF)
11404 21106 : expr = TREE_OPERAND (expr, 0);
11405 :
11406 : return expr;
11407 : }
11408 :
11409 : /* Return TRUE if DECL describes a component that is part of a whole structure
11410 : that is mapped elsewhere in GRPMAP. *MAPPED_BY_GROUP is set to the group
11411 : that maps that structure, if present. */
11412 :
11413 : static bool
11414 25806 : omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
11415 : omp_mapping_group *> *grpmap,
11416 : tree decl,
11417 : omp_mapping_group **mapped_by_group)
11418 : {
11419 25806 : tree wsdecl = NULL_TREE;
11420 :
11421 25806 : *mapped_by_group = NULL;
11422 :
11423 70496 : while (true)
11424 : {
11425 48151 : wsdecl = omp_containing_struct (decl);
11426 48151 : if (wsdecl == decl)
11427 : break;
11428 23587 : omp_mapping_group **wholestruct = grpmap->get (wsdecl);
11429 23587 : if (!wholestruct
11430 21015 : && TREE_CODE (wsdecl) == MEM_REF
11431 23588 : && integer_zerop (TREE_OPERAND (wsdecl, 1)))
11432 : {
11433 1 : tree deref = TREE_OPERAND (wsdecl, 0);
11434 1 : deref = build_fold_indirect_ref (deref);
11435 1 : wholestruct = grpmap->get (deref);
11436 : }
11437 23587 : if (wholestruct)
11438 : {
11439 : /* An intermediate descriptor should not match here because the
11440 : pointee is actually not mapped by this group -- it is just a
11441 : zero-length alloc. */
11442 2572 : tree desc = OMP_CLAUSE_CHAIN (*(*wholestruct)->grp_start);
11443 2572 : if (desc != NULL_TREE && omp_map_clause_descriptor_p (desc))
11444 1330 : goto next;
11445 1242 : *mapped_by_group = *wholestruct;
11446 1242 : return true;
11447 : }
11448 21015 : next:
11449 22345 : decl = wsdecl;
11450 22345 : }
11451 :
11452 : return false;
11453 : }
11454 :
11455 : /* Helper function for omp_tsort_mapping_groups. Returns TRUE on success, or
11456 : FALSE on error. */
11457 :
11458 : static bool
11459 20305 : omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
11460 : vec<omp_mapping_group> *groups,
11461 : hash_map<tree_operand_hash_no_se,
11462 : omp_mapping_group *> *grpmap,
11463 : omp_mapping_group *grp)
11464 : {
11465 20305 : if (grp->mark == PERMANENT)
11466 : return true;
11467 16967 : if (grp->mark == TEMPORARY)
11468 : {
11469 0 : fprintf (stderr, "when processing group:\n");
11470 0 : debug_mapping_group (grp);
11471 0 : internal_error ("base pointer cycle detected");
11472 : return false;
11473 : }
11474 16967 : grp->mark = TEMPORARY;
11475 :
11476 16967 : tree attaches_to = omp_get_attachment (grp);
11477 :
11478 16967 : if (attaches_to)
11479 : {
11480 3100 : omp_mapping_group **basep = grpmap->get (attaches_to);
11481 :
11482 3100 : if (basep && *basep != grp)
11483 : {
11484 2924 : for (omp_mapping_group *w = *basep; w; w = w->sibling)
11485 1462 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11486 : return false;
11487 : }
11488 : }
11489 :
11490 16967 : tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
11491 :
11492 23189 : while (decl)
11493 : {
11494 23189 : tree base = omp_get_base_pointer (decl);
11495 :
11496 23189 : if (!base)
11497 : break;
11498 :
11499 8985 : omp_mapping_group **innerp = grpmap->get (base);
11500 8985 : omp_mapping_group *wholestruct;
11501 :
11502 : /* We should treat whole-structure mappings as if all (pointer, in this
11503 : case) members are mapped as individual list items. Check if we have
11504 : such a whole-structure mapping, if we don't have an explicit reference
11505 : to the pointer member itself. */
11506 8985 : if (!innerp
11507 4172 : && TREE_CODE (base) == COMPONENT_REF
11508 11217 : && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
11509 : innerp = &wholestruct;
11510 :
11511 8985 : if (innerp && *innerp != grp)
11512 : {
11513 5538 : for (omp_mapping_group *w = *innerp; w; w = w->sibling)
11514 2775 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11515 0 : return false;
11516 : break;
11517 : }
11518 :
11519 6222 : decl = base;
11520 : }
11521 :
11522 16967 : grp->mark = PERMANENT;
11523 :
11524 : /* Emit grp to output list. */
11525 :
11526 16967 : **outlist = grp;
11527 16967 : *outlist = &grp->next;
11528 :
11529 16967 : return true;
11530 : }
11531 :
11532 : /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
11533 : before mappings that use those pointers. This is an implementation of the
11534 : depth-first search algorithm, described e.g. at:
11535 :
11536 : https://en.wikipedia.org/wiki/Topological_sorting
11537 : */
11538 :
11539 : static omp_mapping_group *
11540 8618 : omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
11541 : hash_map<tree_operand_hash_no_se, omp_mapping_group *>
11542 : *grpmap,
11543 : bool enter_exit_data)
11544 : {
11545 8618 : omp_mapping_group *grp, *outlist = NULL, **cursor;
11546 8618 : unsigned int i;
11547 8618 : bool saw_runtime_implicit = false;
11548 :
11549 8618 : cursor = &outlist;
11550 :
11551 25585 : FOR_EACH_VEC_ELT (*groups, i, grp)
11552 : {
11553 16967 : if (grp->mark != PERMANENT)
11554 : {
11555 16092 : if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11556 : {
11557 683 : saw_runtime_implicit = true;
11558 683 : continue;
11559 : }
11560 15409 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11561 : return NULL;
11562 : }
11563 : }
11564 :
11565 8618 : if (!saw_runtime_implicit)
11566 8336 : return outlist;
11567 :
11568 1634 : FOR_EACH_VEC_ELT (*groups, i, grp)
11569 : {
11570 1352 : if (grp->mark != PERMANENT
11571 1352 : && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11572 : {
11573 : /* Clear the flag for enter/exit data because it is currently
11574 : meaningless for those operations in libgomp. */
11575 659 : if (enter_exit_data)
11576 446 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
11577 :
11578 659 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11579 : return NULL;
11580 : }
11581 : }
11582 :
11583 282 : return outlist;
11584 : }
11585 :
11586 : /* Split INLIST into three parts:
11587 :
11588 : - "present" alloc/to/from groups
11589 : - other to/from groups
11590 : - other alloc/release/delete groups
11591 :
11592 : These sub-lists are then concatenated together to form the final list.
11593 : Each sub-list retains the order of the original list.
11594 : Note that ATTACH nodes are later moved to the end of the list in
11595 : gimplify_adjust_omp_clauses, for target regions. */
11596 :
11597 : static omp_mapping_group *
11598 8618 : omp_segregate_mapping_groups (omp_mapping_group *inlist)
11599 : {
11600 8618 : omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
11601 8618 : omp_mapping_group *p_groups = NULL;
11602 8618 : omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
11603 8618 : omp_mapping_group **p_tail = &p_groups;
11604 :
11605 25585 : for (omp_mapping_group *w = inlist; w;)
11606 : {
11607 16967 : tree c = *w->grp_start;
11608 16967 : omp_mapping_group *next = w->next;
11609 :
11610 16967 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
11611 :
11612 16967 : switch (OMP_CLAUSE_MAP_KIND (c))
11613 : {
11614 1939 : case GOMP_MAP_ALLOC:
11615 1939 : case GOMP_MAP_RELEASE:
11616 1939 : case GOMP_MAP_DELETE:
11617 1939 : *ard_tail = w;
11618 1939 : w->next = NULL;
11619 1939 : ard_tail = &w->next;
11620 1939 : break;
11621 :
11622 : /* These map types are all semantically identical, so are moved into a
11623 : single group. They will each be changed into GOMP_MAP_FORCE_PRESENT
11624 : in gimplify_adjust_omp_clauses. */
11625 151 : case GOMP_MAP_PRESENT_ALLOC:
11626 151 : case GOMP_MAP_PRESENT_FROM:
11627 151 : case GOMP_MAP_PRESENT_TO:
11628 151 : case GOMP_MAP_PRESENT_TOFROM:
11629 151 : *p_tail = w;
11630 151 : w->next = NULL;
11631 151 : p_tail = &w->next;
11632 151 : break;
11633 :
11634 14877 : default:
11635 14877 : *tf_tail = w;
11636 14877 : w->next = NULL;
11637 14877 : tf_tail = &w->next;
11638 : }
11639 :
11640 16967 : w = next;
11641 : }
11642 :
11643 : /* Now splice the lists together... */
11644 8618 : *tf_tail = ard_groups;
11645 8618 : *p_tail = tf_groups;
11646 :
11647 8618 : return p_groups;
11648 : }
11649 :
11650 : /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
11651 : those groups based on the output list of omp_tsort_mapping_groups --
11652 : singly-linked, threaded through each element's NEXT pointer starting at
11653 : HEAD. Each list element appears exactly once in that linked list.
11654 :
11655 : Each element of GROUPS may correspond to one or several mapping nodes.
11656 : Node groups are kept together, and in the reordered list, the positions of
11657 : the original groups are reused for the positions of the reordered list.
11658 : Hence if we have e.g.
11659 :
11660 : {to ptr ptr} firstprivate {tofrom ptr} ...
11661 : ^ ^ ^
11662 : first group non-"map" second group
11663 :
11664 : and say the second group contains a base pointer for the first so must be
11665 : moved before it, the resulting list will contain:
11666 :
11667 : {tofrom ptr} firstprivate {to ptr ptr} ...
11668 : ^ prev. second group ^ prev. first group
11669 : */
11670 :
11671 : static tree *
11672 8618 : omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
11673 : omp_mapping_group *head,
11674 : tree *list_p)
11675 : {
11676 8618 : omp_mapping_group *grp;
11677 8618 : unsigned int i;
11678 8618 : unsigned numgroups = groups->length ();
11679 8618 : auto_vec<tree> old_heads (numgroups);
11680 8618 : auto_vec<tree *> old_headps (numgroups);
11681 8618 : auto_vec<tree> new_heads (numgroups);
11682 8618 : auto_vec<tree> old_succs (numgroups);
11683 8618 : bool map_at_start = (list_p == (*groups)[0].grp_start);
11684 :
11685 8618 : tree *new_grp_tail = NULL;
11686 :
11687 : /* Stash the start & end nodes of each mapping group before we start
11688 : modifying the list. */
11689 25585 : FOR_EACH_VEC_ELT (*groups, i, grp)
11690 : {
11691 16967 : old_headps.quick_push (grp->grp_start);
11692 16967 : old_heads.quick_push (*grp->grp_start);
11693 16967 : old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
11694 : }
11695 :
11696 : /* And similarly, the heads of the groups in the order we want to rearrange
11697 : the list to. */
11698 25585 : for (omp_mapping_group *w = head; w; w = w->next)
11699 16967 : new_heads.quick_push (*w->grp_start);
11700 :
11701 25585 : FOR_EACH_VEC_ELT (*groups, i, grp)
11702 : {
11703 16967 : gcc_assert (head);
11704 :
11705 16967 : if (new_grp_tail && old_succs[i - 1] == old_heads[i])
11706 : {
11707 : /* a {b c d} {e f g} h i j (original)
11708 : -->
11709 : a {k l m} {e f g} h i j (inserted new group on last iter)
11710 : -->
11711 : a {k l m} {n o p} h i j (this time, chain last group to new one)
11712 : ^new_grp_tail
11713 : */
11714 7727 : *new_grp_tail = new_heads[i];
11715 : }
11716 9240 : else if (new_grp_tail)
11717 : {
11718 : /* a {b c d} e {f g h} i j k (original)
11719 : -->
11720 : a {l m n} e {f g h} i j k (gap after last iter's group)
11721 : -->
11722 : a {l m n} e {o p q} h i j (chain last group to old successor)
11723 : ^new_grp_tail
11724 : */
11725 622 : *new_grp_tail = old_succs[i - 1];
11726 622 : *old_headps[i] = new_heads[i];
11727 : }
11728 : else
11729 : {
11730 : /* The first inserted group -- point to new group, and leave end
11731 : open.
11732 : a {b c d} e f
11733 : -->
11734 : a {g h i...
11735 : */
11736 8618 : *grp->grp_start = new_heads[i];
11737 : }
11738 :
11739 16967 : new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
11740 :
11741 16967 : head = head->next;
11742 : }
11743 :
11744 8618 : if (new_grp_tail)
11745 8618 : *new_grp_tail = old_succs[numgroups - 1];
11746 :
11747 8618 : gcc_assert (!head);
11748 :
11749 12076 : return map_at_start ? (*groups)[0].grp_start : list_p;
11750 8618 : }
11751 :
11752 : /* DECL is supposed to have lastprivate semantics in the outer contexts
11753 : of combined/composite constructs, starting with OCTX.
11754 : Add needed lastprivate, shared or map clause if no data sharing or
11755 : mapping clause are present. IMPLICIT_P is true if it is an implicit
11756 : clause (IV on simd), in which case the lastprivate will not be
11757 : copied to some constructs. */
11758 :
11759 : static void
11760 14658 : omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
11761 : tree decl, bool implicit_p)
11762 : {
11763 14658 : struct gimplify_omp_ctx *orig_octx = octx;
11764 26979 : for (; octx; octx = octx->outer_context)
11765 : {
11766 25901 : if ((octx->region_type == ORT_COMBINED_PARALLEL
11767 20865 : || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
11768 25332 : && splay_tree_lookup (octx->variables,
11769 : (splay_tree_key) decl) == NULL)
11770 : {
11771 3887 : omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
11772 3887 : continue;
11773 : }
11774 18375 : if ((octx->region_type & ORT_TASK) != 0
11775 575 : && octx->combined_loop
11776 18656 : && splay_tree_lookup (octx->variables,
11777 : (splay_tree_key) decl) == NULL)
11778 : {
11779 248 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11780 248 : continue;
11781 : }
11782 22711 : if (implicit_p
11783 13619 : && octx->region_type == ORT_WORKSHARE
11784 10405 : && octx->combined_loop
11785 10300 : && splay_tree_lookup (octx->variables,
11786 : (splay_tree_key) decl) == NULL
11787 10300 : && octx->outer_context
11788 8907 : && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
11789 22718 : && splay_tree_lookup (octx->outer_context->variables,
11790 : (splay_tree_key) decl) == NULL)
11791 : {
11792 4832 : octx = octx->outer_context;
11793 4832 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11794 4832 : continue;
11795 : }
11796 5565 : if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
11797 7482 : && octx->combined_loop
11798 7295 : && splay_tree_lookup (octx->variables,
11799 : (splay_tree_key) decl) == NULL
11800 19072 : && !omp_check_private (octx, decl, false))
11801 : {
11802 3354 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11803 3354 : continue;
11804 : }
11805 9693 : if (octx->region_type == ORT_COMBINED_TARGET)
11806 : {
11807 2008 : splay_tree_node n = splay_tree_lookup (octx->variables,
11808 : (splay_tree_key) decl);
11809 2008 : if (n == NULL)
11810 : {
11811 1986 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11812 1986 : octx = octx->outer_context;
11813 : }
11814 22 : else if (!implicit_p
11815 22 : && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
11816 : {
11817 12 : n->value &= ~(GOVD_FIRSTPRIVATE
11818 : | GOVD_FIRSTPRIVATE_IMPLICIT
11819 : | GOVD_EXPLICIT);
11820 12 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11821 12 : octx = octx->outer_context;
11822 : }
11823 : }
11824 : break;
11825 : }
11826 14658 : if (octx && (implicit_p || octx != orig_octx))
11827 4046 : omp_notice_variable (octx, decl, true);
11828 14658 : }
11829 :
11830 : /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
11831 : a "FIRSTPRIVATE" mapping. Return the one that isn't firstprivate, etc. */
11832 :
11833 : static omp_mapping_group *
11834 6007 : omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
11835 : omp_mapping_group *> *grpmap,
11836 : tree decl, bool allow_deleted = false)
11837 : {
11838 6007 : omp_mapping_group **to_group_p = grpmap->get (decl);
11839 :
11840 6007 : if (!to_group_p)
11841 : return NULL;
11842 :
11843 2474 : omp_mapping_group *to_group = *to_group_p;
11844 :
11845 3826 : for (; to_group; to_group = to_group->sibling)
11846 : {
11847 2511 : tree grp_end = to_group->grp_end;
11848 2511 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11849 : {
11850 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11851 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11852 : break;
11853 :
11854 1161 : default:
11855 1161 : if (allow_deleted || !to_group->deleted)
11856 : return to_group;
11857 : }
11858 : }
11859 :
11860 : return NULL;
11861 : }
11862 :
11863 : /* Return TRUE if the directive (whose clauses are described by the hash table
11864 : of mapping groups, GRPMAP) maps DECL explicitly. If TO_SPECIFICALLY is
11865 : true, only count TO mappings. If ALLOW_DELETED is true, ignore the
11866 : "deleted" flag for groups. If CONTAINED_IN_STRUCT is true, also return
11867 : TRUE if DECL is mapped as a member of a whole-struct mapping. */
11868 :
11869 : static bool
11870 4450 : omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
11871 : omp_mapping_group *> *grpmap,
11872 : tree decl, omp_mapping_group **base_group,
11873 : bool to_specifically, bool allow_deleted,
11874 : bool contained_in_struct)
11875 : {
11876 4450 : omp_mapping_group *decl_group
11877 4450 : = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
11878 :
11879 4450 : *base_group = NULL;
11880 :
11881 4450 : if (decl_group)
11882 : {
11883 1042 : tree grp_first = *decl_group->grp_start;
11884 : /* We might be called during omp_build_struct_sibling_lists, when
11885 : GOMP_MAP_STRUCT might have been inserted at the start of the group.
11886 : Skip over that, and also possibly the node after it. */
11887 1042 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
11888 1042 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
11889 : {
11890 6 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11891 6 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
11892 6 : || (OMP_CLAUSE_MAP_KIND (grp_first)
11893 : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11894 12 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
11895 0 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11896 : }
11897 1042 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11898 1042 : if (!to_specifically
11899 592 : || GOMP_MAP_COPY_TO_P (first_kind)
11900 382 : || first_kind == GOMP_MAP_ALLOC)
11901 : {
11902 960 : *base_group = decl_group;
11903 960 : return true;
11904 : }
11905 : }
11906 :
11907 3490 : if (contained_in_struct
11908 3490 : && omp_mapped_by_containing_struct (grpmap, decl, base_group))
11909 : return true;
11910 :
11911 : return false;
11912 : }
11913 :
11914 : /* If we have mappings INNER and OUTER, where INNER is a component access and
11915 : OUTER is a mapping of the whole containing struct, check that the mappings
11916 : are compatible. We'll be deleting the inner mapping, so we need to make
11917 : sure the outer mapping does (at least) the same transfers to/from the device
11918 : as the inner mapping. */
11919 :
11920 : bool
11921 172 : omp_check_mapping_compatibility (location_t loc,
11922 : omp_mapping_group *outer,
11923 : omp_mapping_group *inner)
11924 : {
11925 172 : tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
11926 :
11927 172 : gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
11928 172 : gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
11929 :
11930 172 : enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
11931 172 : enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
11932 :
11933 172 : if (outer_kind == inner_kind)
11934 : return true;
11935 :
11936 70 : switch (outer_kind)
11937 : {
11938 0 : case GOMP_MAP_ALWAYS_TO:
11939 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11940 0 : || inner_kind == GOMP_MAP_ALLOC
11941 0 : || inner_kind == GOMP_MAP_TO)
11942 : return true;
11943 : break;
11944 :
11945 0 : case GOMP_MAP_ALWAYS_FROM:
11946 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11947 0 : || inner_kind == GOMP_MAP_RELEASE
11948 : || inner_kind == GOMP_MAP_FROM)
11949 : return true;
11950 : break;
11951 :
11952 10 : case GOMP_MAP_TO:
11953 10 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11954 10 : || inner_kind == GOMP_MAP_ALLOC)
11955 : return true;
11956 : break;
11957 :
11958 8 : case GOMP_MAP_FROM:
11959 8 : if (inner_kind == GOMP_MAP_RELEASE
11960 8 : || inner_kind == GOMP_MAP_FORCE_PRESENT)
11961 : return true;
11962 : break;
11963 :
11964 32 : case GOMP_MAP_ALWAYS_TOFROM:
11965 32 : case GOMP_MAP_TOFROM:
11966 32 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11967 32 : || inner_kind == GOMP_MAP_ALLOC
11968 : || inner_kind == GOMP_MAP_TO
11969 24 : || inner_kind == GOMP_MAP_FROM
11970 12 : || inner_kind == GOMP_MAP_TOFROM)
11971 : return true;
11972 : break;
11973 :
11974 28 : default:
11975 28 : ;
11976 : }
11977 :
11978 84 : error_at (loc, "data movement for component %qE is not compatible with "
11979 28 : "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
11980 28 : OMP_CLAUSE_DECL (first_outer));
11981 :
11982 28 : return false;
11983 : }
11984 :
11985 : /* This function handles several cases where clauses on a mapping directive
11986 : can interact with each other.
11987 :
11988 : If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
11989 : on the same directive, change the mapping of the first node to
11990 : ATTACH_DETACH. We should have detected that this will happen already in
11991 : c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
11992 : as addressable. (If we didn't, bail out.)
11993 :
11994 : If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
11995 : mapping the base pointer also, we may need to change the mapping type to
11996 : ATTACH_DETACH and synthesize an alloc node for the reference itself.
11997 :
11998 : If we have an ATTACH_DETACH node, this is an array section with a pointer
11999 : base. If we're mapping the base on the same directive too, we can drop its
12000 : mapping. However, if we have a reference to pointer, make other appropriate
12001 : adjustments to the mapping nodes instead.
12002 :
12003 : If we have an ATTACH_DETACH node with a Fortran pointer-set (array
12004 : descriptor) mapping for a derived-type component, and we're also mapping the
12005 : whole of the derived-type variable on another clause, the pointer-set
12006 : mapping is removed.
12007 :
12008 : If we have a component access but we're also mapping the whole of the
12009 : containing struct, drop the former access.
12010 :
12011 : If the expression is a component access, and we're also mapping a base
12012 : pointer used in that component access in the same expression, change the
12013 : mapping type of the latter to ALLOC (ready for processing by
12014 : omp_build_struct_sibling_lists). */
12015 :
12016 : void
12017 8618 : omp_resolve_clause_dependencies (enum tree_code code,
12018 : vec<omp_mapping_group> *groups,
12019 : hash_map<tree_operand_hash_no_se,
12020 : omp_mapping_group *> *grpmap)
12021 : {
12022 8618 : int i;
12023 8618 : omp_mapping_group *grp;
12024 8618 : bool repair_chain = false;
12025 :
12026 24521 : FOR_EACH_VEC_ELT (*groups, i, grp)
12027 : {
12028 15903 : tree grp_end = grp->grp_end;
12029 15903 : tree decl = OMP_CLAUSE_DECL (grp_end);
12030 :
12031 15903 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
12032 :
12033 15903 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
12034 : {
12035 1241 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
12036 1241 : {
12037 1241 : omp_mapping_group *to_group
12038 1241 : = omp_get_nonfirstprivate_group (grpmap, decl);
12039 :
12040 1241 : if (!to_group || to_group == grp)
12041 1216 : continue;
12042 :
12043 25 : tree grp_first = *to_group->grp_start;
12044 25 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12045 :
12046 25 : if ((GOMP_MAP_COPY_TO_P (first_kind)
12047 7 : || first_kind == GOMP_MAP_ALLOC)
12048 50 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12049 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
12050 : {
12051 25 : gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
12052 25 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12053 : }
12054 : }
12055 : break;
12056 :
12057 160 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
12058 160 : {
12059 160 : tree ptr = build_fold_indirect_ref (decl);
12060 :
12061 160 : omp_mapping_group *to_group
12062 160 : = omp_get_nonfirstprivate_group (grpmap, ptr);
12063 :
12064 160 : if (!to_group || to_group == grp)
12065 156 : continue;
12066 :
12067 4 : tree grp_first = *to_group->grp_start;
12068 4 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12069 :
12070 4 : if (GOMP_MAP_COPY_TO_P (first_kind)
12071 4 : || first_kind == GOMP_MAP_ALLOC)
12072 : {
12073 4 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12074 4 : OMP_CLAUSE_DECL (grp_end) = ptr;
12075 4 : if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
12076 4 : == to_group->grp_end)
12077 4 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12078 : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
12079 : {
12080 0 : gcc_assert (TREE_ADDRESSABLE
12081 : (OMP_CLAUSE_DECL (to_group->grp_end)));
12082 0 : OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
12083 : GOMP_MAP_ATTACH_DETACH);
12084 :
12085 0 : location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
12086 0 : tree alloc
12087 0 : = build_omp_clause (loc, OMP_CLAUSE_MAP);
12088 0 : OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
12089 0 : tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
12090 : (to_group->grp_end));
12091 0 : tree char_ptr_type = build_pointer_type (char_type_node);
12092 0 : OMP_CLAUSE_DECL (alloc)
12093 0 : = build2 (MEM_REF, char_type_node,
12094 : tmp,
12095 : build_int_cst (char_ptr_type, 0));
12096 0 : OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
12097 :
12098 0 : OMP_CLAUSE_CHAIN (alloc)
12099 0 : = OMP_CLAUSE_CHAIN (*to_group->grp_start);
12100 0 : OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
12101 : }
12102 : }
12103 : }
12104 : break;
12105 :
12106 : case GOMP_MAP_ATTACH_DETACH:
12107 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
12108 : {
12109 2626 : tree base_ptr, referenced_ptr_node = NULL_TREE;
12110 :
12111 2626 : while (TREE_CODE (decl) == ARRAY_REF)
12112 0 : decl = TREE_OPERAND (decl, 0);
12113 :
12114 2626 : if (TREE_CODE (decl) == INDIRECT_REF)
12115 13 : decl = TREE_OPERAND (decl, 0);
12116 :
12117 : /* Only component accesses. */
12118 2626 : if (DECL_P (decl))
12119 230 : continue;
12120 :
12121 : /* We want the pointer itself when checking if the base pointer is
12122 : mapped elsewhere in the same directive -- if we have a
12123 : reference to the pointer, don't use that. */
12124 :
12125 2396 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
12126 2396 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
12127 : {
12128 394 : referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
12129 394 : base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
12130 : }
12131 : else
12132 : base_ptr = decl;
12133 :
12134 1978 : gomp_map_kind zlas_kind
12135 2396 : = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
12136 2396 : ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
12137 :
12138 2396 : if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
12139 : {
12140 : /* If we map the base TO, and we're doing an attachment, we can
12141 : skip the TO mapping altogether and create an ALLOC mapping
12142 : instead, since the attachment will overwrite the device
12143 : pointer in that location immediately anyway. Otherwise,
12144 : change our mapping to
12145 : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
12146 : attachment target has not been copied to the device already
12147 : by some earlier directive. */
12148 :
12149 1972 : bool base_mapped_to = false;
12150 :
12151 1972 : omp_mapping_group *base_group;
12152 :
12153 1972 : if (omp_directive_maps_explicitly (grpmap, base_ptr,
12154 : &base_group, false, true,
12155 : false))
12156 : {
12157 450 : if (referenced_ptr_node)
12158 : {
12159 129 : base_mapped_to = true;
12160 129 : if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
12161 : == GOMP_MAP_ATTACH_DETACH)
12162 129 : && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
12163 : == base_group->grp_end))
12164 : {
12165 258 : OMP_CLAUSE_CHAIN (*base_group->grp_start)
12166 129 : = OMP_CLAUSE_CHAIN (base_group->grp_end);
12167 129 : base_group->grp_end = *base_group->grp_start;
12168 129 : repair_chain = true;
12169 : }
12170 : }
12171 : else
12172 : {
12173 321 : base_group->deleted = true;
12174 321 : OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
12175 : }
12176 : }
12177 :
12178 : /* We're dealing with a reference to a pointer, and we are
12179 : attaching both the reference and the pointer. We know the
12180 : reference itself is on the target, because we are going to
12181 : create an ALLOC node for it in accumulate_sibling_list. The
12182 : pointer might be on the target already or it might not, but
12183 : if it isn't then it's not an error, so use
12184 : GOMP_MAP_ATTACH_ZLAS for it. */
12185 1972 : if (!base_mapped_to && referenced_ptr_node)
12186 56 : OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
12187 :
12188 1972 : omp_mapping_group *struct_group;
12189 1972 : tree desc;
12190 1972 : if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
12191 1972 : && omp_map_clause_descriptor_p (desc)
12192 3050 : && omp_mapped_by_containing_struct (grpmap, decl,
12193 : &struct_group))
12194 : /* If we have a pointer set but we're mapping (or unmapping)
12195 : the whole of the containing struct, we can remove the
12196 : pointer set mapping. */
12197 15 : OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
12198 : }
12199 424 : else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
12200 424 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
12201 : == ARRAY_TYPE)
12202 562 : && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
12203 : (*grp->grp_start))
12204 92 : OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
12205 : }
12206 : break;
12207 :
12208 : case GOMP_MAP_ATTACH:
12209 : /* Ignore standalone attach here. */
12210 : break;
12211 :
12212 11809 : default:
12213 11809 : {
12214 11809 : omp_mapping_group *struct_group;
12215 11809 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12216 11809 : && *grp->grp_start == grp_end)
12217 : {
12218 94 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12219 : struct_group, grp);
12220 : /* Remove the whole of this mapping -- redundant. */
12221 94 : grp->deleted = true;
12222 : }
12223 :
12224 11809 : tree base = decl;
12225 13421 : while ((base = omp_get_base_pointer (base)))
12226 : {
12227 1612 : omp_mapping_group *base_group;
12228 :
12229 1612 : if (omp_directive_maps_explicitly (grpmap, base, &base_group,
12230 : true, true, false))
12231 : {
12232 343 : tree grp_first = *base_group->grp_start;
12233 343 : OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
12234 : }
12235 : }
12236 : }
12237 : }
12238 : }
12239 :
12240 8618 : if (repair_chain)
12241 : {
12242 : /* Group start pointers may have become detached from the
12243 : OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
12244 : end of those groups. Fix that now. */
12245 : tree *new_next = NULL;
12246 716 : FOR_EACH_VEC_ELT (*groups, i, grp)
12247 : {
12248 587 : if (new_next)
12249 458 : grp->grp_start = new_next;
12250 :
12251 587 : new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
12252 : }
12253 : }
12254 8618 : }
12255 :
12256 : /* Similar to omp_resolve_clause_dependencies, but for OpenACC. The only
12257 : clause dependencies we handle for now are struct element mappings and
12258 : whole-struct mappings on the same directive, and duplicate clause
12259 : detection. */
12260 :
12261 : void
12262 9462 : oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
12263 : hash_map<tree_operand_hash_no_se,
12264 : omp_mapping_group *> *grpmap)
12265 : {
12266 9462 : int i;
12267 9462 : omp_mapping_group *grp;
12268 9462 : hash_set<tree_operand_hash> *seen_components = NULL;
12269 9462 : hash_set<tree_operand_hash> *shown_error = NULL;
12270 :
12271 24643 : FOR_EACH_VEC_ELT (*groups, i, grp)
12272 : {
12273 15181 : tree grp_end = grp->grp_end;
12274 15181 : tree decl = OMP_CLAUSE_DECL (grp_end);
12275 :
12276 15181 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
12277 :
12278 15181 : if (DECL_P (grp_end))
12279 14664 : continue;
12280 :
12281 15181 : tree c = OMP_CLAUSE_DECL (*grp->grp_start);
12282 16806 : while (TREE_CODE (c) == ARRAY_REF)
12283 1625 : c = TREE_OPERAND (c, 0);
12284 15181 : if (TREE_CODE (c) != COMPONENT_REF)
12285 14664 : continue;
12286 517 : if (!seen_components)
12287 474 : seen_components = new hash_set<tree_operand_hash> ();
12288 517 : if (!shown_error)
12289 474 : shown_error = new hash_set<tree_operand_hash> ();
12290 517 : if (seen_components->contains (c)
12291 517 : && !shown_error->contains (c))
12292 : {
12293 10 : error_at (OMP_CLAUSE_LOCATION (grp_end),
12294 : "%qE appears more than once in map clauses",
12295 5 : OMP_CLAUSE_DECL (grp_end));
12296 5 : shown_error->add (c);
12297 : }
12298 : else
12299 512 : seen_components->add (c);
12300 :
12301 517 : omp_mapping_group *struct_group;
12302 517 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12303 517 : && *grp->grp_start == grp_end)
12304 : {
12305 78 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12306 : struct_group, grp);
12307 : /* Remove the whole of this mapping -- redundant. */
12308 78 : grp->deleted = true;
12309 : }
12310 : }
12311 :
12312 9462 : if (seen_components)
12313 474 : delete seen_components;
12314 9462 : if (shown_error)
12315 474 : delete shown_error;
12316 9462 : }
12317 :
12318 : /* Link node NEWNODE so it is pointed to by chain INSERT_AT. NEWNODE's chain
12319 : is linked to the previous node pointed to by INSERT_AT. */
12320 :
12321 : static tree *
12322 1135 : omp_siblist_insert_node_after (tree newnode, tree *insert_at)
12323 : {
12324 1135 : OMP_CLAUSE_CHAIN (newnode) = *insert_at;
12325 1135 : *insert_at = newnode;
12326 1135 : return &OMP_CLAUSE_CHAIN (newnode);
12327 : }
12328 :
12329 : /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
12330 : pointed to by chain MOVE_AFTER instead. */
12331 :
12332 : static void
12333 1320 : omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
12334 : {
12335 1320 : gcc_assert (node == *old_pos);
12336 1320 : *old_pos = OMP_CLAUSE_CHAIN (node);
12337 1320 : OMP_CLAUSE_CHAIN (node) = *move_after;
12338 1320 : *move_after = node;
12339 1320 : }
12340 :
12341 : /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
12342 : LAST_NODE to after MOVE_AFTER chain. Similar to below function, but no
12343 : new nodes are prepended to the list before splicing into the new position.
12344 : Return the position we should continue scanning the list at, or NULL to
12345 : stay where we were. */
12346 :
12347 : static tree *
12348 254 : omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
12349 : tree *move_after)
12350 : {
12351 254 : if (first_ptr == move_after)
12352 : return NULL;
12353 :
12354 243 : tree tmp = *first_ptr;
12355 243 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12356 243 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12357 243 : *move_after = tmp;
12358 :
12359 243 : return first_ptr;
12360 : }
12361 :
12362 : /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
12363 : [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
12364 : pointer MOVE_AFTER.
12365 :
12366 : The latter list was previously part of the OMP clause list, and the former
12367 : (prepended) part is comprised of new nodes.
12368 :
12369 : We start with a list of nodes starting with a struct mapping node. We
12370 : rearrange the list so that new nodes starting from FIRST_NEW and whose last
12371 : node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
12372 : the group of mapping nodes we are currently processing (from the chain
12373 : FIRST_PTR to LAST_NODE). The return value is the pointer to the next chain
12374 : we should continue processing from, or NULL to stay where we were.
12375 :
12376 : The transformation (in the case where MOVE_AFTER and FIRST_PTR are
12377 : different) is worked through below. Here we are processing LAST_NODE, and
12378 : FIRST_PTR points at the preceding mapping clause:
12379 :
12380 : #. mapping node chain
12381 : ---------------------------------------------------
12382 : A. struct_node [->B]
12383 : B. comp_1 [->C]
12384 : C. comp_2 [->D (move_after)]
12385 : D. map_to_3 [->E]
12386 : E. attach_3 [->F (first_ptr)]
12387 : F. map_to_4 [->G (continue_at)]
12388 : G. attach_4 (last_node) [->H]
12389 : H. ...
12390 :
12391 : *last_new_tail = *first_ptr;
12392 :
12393 : I. new_node (first_new) [->F (last_new_tail)]
12394 :
12395 : *first_ptr = OMP_CLAUSE_CHAIN (last_node)
12396 :
12397 : #. mapping node chain
12398 : ----------------------------------------------------
12399 : A. struct_node [->B]
12400 : B. comp_1 [->C]
12401 : C. comp_2 [->D (move_after)]
12402 : D. map_to_3 [->E]
12403 : E. attach_3 [->H (first_ptr)]
12404 : F. map_to_4 [->G (continue_at)]
12405 : G. attach_4 (last_node) [->H]
12406 : H. ...
12407 :
12408 : I. new_node (first_new) [->F (last_new_tail)]
12409 :
12410 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12411 :
12412 : #. mapping node chain
12413 : ---------------------------------------------------
12414 : A. struct_node [->B]
12415 : B. comp_1 [->C]
12416 : C. comp_2 [->D (move_after)]
12417 : D. map_to_3 [->E]
12418 : E. attach_3 [->H (continue_at)]
12419 : F. map_to_4 [->G]
12420 : G. attach_4 (last_node) [->D]
12421 : H. ...
12422 :
12423 : I. new_node (first_new) [->F (last_new_tail)]
12424 :
12425 : *move_after = first_new;
12426 :
12427 : #. mapping node chain
12428 : ---------------------------------------------------
12429 : A. struct_node [->B]
12430 : B. comp_1 [->C]
12431 : C. comp_2 [->I (move_after)]
12432 : D. map_to_3 [->E]
12433 : E. attach_3 [->H (continue_at)]
12434 : F. map_to_4 [->G]
12435 : G. attach_4 (last_node) [->D]
12436 : H. ...
12437 : I. new_node (first_new) [->F (last_new_tail)]
12438 :
12439 : or, in order:
12440 :
12441 : #. mapping node chain
12442 : ---------------------------------------------------
12443 : A. struct_node [->B]
12444 : B. comp_1 [->C]
12445 : C. comp_2 [->I (move_after)]
12446 : I. new_node (first_new) [->F (last_new_tail)]
12447 : F. map_to_4 [->G]
12448 : G. attach_4 (last_node) [->D]
12449 : D. map_to_3 [->E]
12450 : E. attach_3 [->H (continue_at)]
12451 : H. ...
12452 : */
12453 :
12454 : static tree *
12455 71 : omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
12456 : tree *first_ptr, tree last_node,
12457 : tree *move_after)
12458 : {
12459 71 : tree *continue_at = NULL;
12460 71 : *last_new_tail = *first_ptr;
12461 71 : if (first_ptr == move_after)
12462 12 : *move_after = first_new;
12463 : else
12464 : {
12465 59 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12466 59 : continue_at = first_ptr;
12467 59 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12468 59 : *move_after = first_new;
12469 : }
12470 71 : return continue_at;
12471 : }
12472 :
12473 : static omp_addr_token *
12474 11580 : omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
12475 : {
12476 11580 : using namespace omp_addr_tokenizer;
12477 11580 : int idx = addr_tokens.length () - 1;
12478 11580 : gcc_assert (idx >= 0);
12479 11580 : if (addr_tokens[idx]->type != ACCESS_METHOD)
12480 : return addr_tokens[idx];
12481 11611 : while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
12482 : idx--;
12483 11580 : return addr_tokens[idx];
12484 : }
12485 :
12486 : /* Mapping struct members causes an additional set of nodes to be created,
12487 : starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
12488 : number of members being mapped, in order of ascending position (address or
12489 : bitwise).
12490 :
12491 : We scan through the list of mapping clauses, calling this function for each
12492 : struct member mapping we find, and build up the list of mappings after the
12493 : initial GOMP_MAP_STRUCT node. For pointer members, these will be
12494 : newly-created ALLOC nodes. For non-pointer members, the existing mapping is
12495 : moved into place in the sorted list.
12496 :
12497 : struct {
12498 : int *a;
12499 : int *b;
12500 : int c;
12501 : int *d;
12502 : };
12503 :
12504 : #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
12505 : struct.d[0:n])
12506 :
12507 : GOMP_MAP_STRUCT (4)
12508 : [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
12509 : GOMP_MAP_ALLOC (struct.a)
12510 : GOMP_MAP_ALLOC (struct.b)
12511 : GOMP_MAP_TO (struct.c)
12512 : GOMP_MAP_ALLOC (struct.d)
12513 : ...
12514 :
12515 : In the case where we are mapping references to pointers, or in Fortran if
12516 : we are mapping an array with a descriptor, additional nodes may be created
12517 : after the struct node list also.
12518 :
12519 : The return code is either a pointer to the next node to process (if the
12520 : list has been rearranged), else NULL to continue with the next node in the
12521 : original list. */
12522 :
12523 : static tree *
12524 4767 : omp_accumulate_sibling_list (enum omp_region_type region_type,
12525 : enum tree_code code,
12526 : hash_map<tree_operand_hash, tree>
12527 : *&struct_map_to_clause,
12528 : hash_map<tree_operand_hash_no_se,
12529 : omp_mapping_group *> *group_map,
12530 : tree *grp_start_p, tree grp_end,
12531 : vec<omp_addr_token *> &addr_tokens, tree **inner,
12532 : bool *fragile_p, bool reprocessing_struct,
12533 : tree **added_tail)
12534 : {
12535 4767 : using namespace omp_addr_tokenizer;
12536 4767 : poly_offset_int coffset;
12537 4767 : poly_int64 cbitpos;
12538 4767 : tree ocd = OMP_CLAUSE_DECL (grp_end);
12539 4767 : bool openmp = !(region_type & ORT_ACC);
12540 4767 : bool target = (region_type & ORT_TARGET) != 0;
12541 4767 : tree *continue_at = NULL;
12542 :
12543 5080 : while (TREE_CODE (ocd) == ARRAY_REF)
12544 313 : ocd = TREE_OPERAND (ocd, 0);
12545 :
12546 4767 : if (*fragile_p)
12547 : {
12548 156 : omp_mapping_group *to_group
12549 156 : = omp_get_nonfirstprivate_group (group_map, ocd, true);
12550 :
12551 156 : if (to_group)
12552 : return NULL;
12553 : }
12554 :
12555 4679 : omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
12556 4679 : if (last_token->type == ACCESS_METHOD)
12557 : {
12558 4679 : switch (last_token->u.access_kind)
12559 : {
12560 735 : case ACCESS_REF:
12561 735 : case ACCESS_REF_TO_POINTER:
12562 735 : case ACCESS_REF_TO_POINTER_OFFSET:
12563 735 : case ACCESS_INDEXED_REF_TO_ARRAY:
12564 : /* We may see either a bare reference or a dereferenced
12565 : "convert_from_reference"-like one here. Handle either way. */
12566 735 : if (TREE_CODE (ocd) == INDIRECT_REF)
12567 64 : ocd = TREE_OPERAND (ocd, 0);
12568 735 : gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
12569 : break;
12570 :
12571 : default:
12572 : ;
12573 : }
12574 : }
12575 :
12576 4679 : bool variable_offset;
12577 9358 : tree iterators = OMP_CLAUSE_HAS_ITERATORS (grp_end)
12578 4712 : ? OMP_CLAUSE_ITERATORS (grp_end) : NULL_TREE;
12579 4679 : tree base
12580 4679 : = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset,
12581 4679 : iterators);
12582 :
12583 4679 : int base_token;
12584 23907 : for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
12585 : {
12586 19228 : if (addr_tokens[base_token]->type == ARRAY_BASE
12587 19228 : || addr_tokens[base_token]->type == STRUCTURE_BASE)
12588 : break;
12589 : }
12590 :
12591 : /* The two expressions in the assertion below aren't quite the same: if we
12592 : have 'struct_base_decl access_indexed_array' for something like
12593 : "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
12594 : will be "myvar[2]" -- the actual base of the structure.
12595 : The former interpretation leads to a strange situation where we get
12596 : struct(myvar) alloc(myvar[2].ptr1)
12597 : That is, the array of structures is kind of treated as one big structure
12598 : for the purposes of gathering sibling lists, etc. */
12599 : /* gcc_assert (base == addr_tokens[base_token]->expr); */
12600 :
12601 4679 : bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
12602 : == GOMP_MAP_ATTACH_DETACH)
12603 4679 : || (OMP_CLAUSE_MAP_KIND (grp_end)
12604 4679 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
12605 4679 : bool has_descriptor = false;
12606 4679 : if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12607 : {
12608 3139 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
12609 3139 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
12610 : has_descriptor = true;
12611 : }
12612 :
12613 4679 : if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
12614 : {
12615 3060 : enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
12616 :
12617 3060 : if (struct_map_to_clause == NULL)
12618 2497 : struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
12619 :
12620 3060 : if (variable_offset)
12621 274 : str_kind = GOMP_MAP_STRUCT_UNORD;
12622 :
12623 3060 : tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
12624 :
12625 3060 : OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
12626 3060 : OMP_CLAUSE_DECL (l) = unshare_expr (base);
12627 3060 : OMP_CLAUSE_SIZE (l) = size_int (1);
12628 :
12629 3060 : struct_map_to_clause->put (base, l);
12630 :
12631 : /* On first iterating through the clause list, we insert the struct node
12632 : just before the component access node that triggers the initial
12633 : omp_accumulate_sibling_list call for a particular sibling list (and
12634 : it then forms the first entry in that list). When reprocessing
12635 : struct bases that are themselves component accesses, we insert the
12636 : struct node on an off-side list to avoid inserting the new
12637 : GOMP_MAP_STRUCT into the middle of the old one. */
12638 3060 : tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
12639 :
12640 3060 : if (has_descriptor)
12641 : {
12642 759 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12643 759 : if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
12644 187 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12645 759 : tree sc = *insert_node_pos;
12646 759 : OMP_CLAUSE_CHAIN (l) = desc;
12647 759 : OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
12648 759 : OMP_CLAUSE_CHAIN (desc) = sc;
12649 759 : *insert_node_pos = l;
12650 : }
12651 2301 : else if (attach_detach)
12652 : {
12653 1396 : tree extra_node;
12654 1396 : tree alloc_node
12655 1396 : = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
12656 : &extra_node);
12657 1396 : tree *tail;
12658 1396 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12659 :
12660 1396 : if (extra_node)
12661 : {
12662 0 : OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
12663 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12664 0 : tail = &OMP_CLAUSE_CHAIN (extra_node);
12665 : }
12666 : else
12667 : {
12668 1396 : OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
12669 1396 : tail = &OMP_CLAUSE_CHAIN (alloc_node);
12670 : }
12671 :
12672 : /* For OpenMP semantics, we don't want to implicitly allocate
12673 : space for the pointer here for non-compute regions (e.g. "enter
12674 : data"). A FRAGILE_P node is only being created so that
12675 : omp-low.cc is able to rewrite the struct properly.
12676 : For references (to pointers), we want to actually allocate the
12677 : space for the reference itself in the sorted list following the
12678 : struct node.
12679 : For pointers, we want to allocate space if we had an explicit
12680 : mapping of the attachment point, but not otherwise. */
12681 1396 : if (*fragile_p
12682 1396 : || (openmp
12683 : && !target
12684 : && attach_detach
12685 234 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12686 88 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12687 : {
12688 133 : if (!lang_GNU_Fortran ())
12689 : /* In Fortran, pointers are dereferenced automatically, but may
12690 : be unassociated. So we still want to allocate space for the
12691 : pointer (as the base for an attach operation that should be
12692 : present in the same directive's clause list also). */
12693 103 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12694 133 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12695 : }
12696 :
12697 1396 : *insert_node_pos = l;
12698 :
12699 1396 : if (reprocessing_struct)
12700 : {
12701 : /* When reprocessing a struct node group used as the base of a
12702 : subcomponent access, if we have a reference-to-pointer base,
12703 : we will see:
12704 : struct(**ptr) attach(*ptr)
12705 : whereas for a non-reprocess-struct group, we see, e.g.:
12706 : tofrom(**ptr) attach(*ptr) attach(ptr)
12707 : and we create the "alloc" for the second "attach", i.e.
12708 : for the reference itself. When reprocessing a struct group we
12709 : thus change the pointer attachment into a reference attachment
12710 : by stripping the indirection. (The attachment of the
12711 : referenced pointer must happen elsewhere, either on the same
12712 : directive, or otherwise.) */
12713 180 : tree adecl = OMP_CLAUSE_DECL (alloc_node);
12714 :
12715 180 : if ((TREE_CODE (adecl) == INDIRECT_REF
12716 148 : || (TREE_CODE (adecl) == MEM_REF
12717 0 : && integer_zerop (TREE_OPERAND (adecl, 1))))
12718 32 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
12719 : == REFERENCE_TYPE)
12720 212 : && (TREE_CODE (TREE_TYPE (TREE_TYPE
12721 : (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
12722 32 : OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
12723 :
12724 180 : *added_tail = tail;
12725 : }
12726 : }
12727 : else
12728 : {
12729 905 : gcc_assert (*grp_start_p == grp_end);
12730 905 : if (reprocessing_struct)
12731 : {
12732 : /* If we don't have an attach/detach node, this is a
12733 : "target data" directive or similar, not an offload region.
12734 : Synthesize an "alloc" node using just the initiating
12735 : GOMP_MAP_STRUCT decl. */
12736 16 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12737 32 : || code == OACC_EXIT_DATA)
12738 32 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12739 32 : tree alloc_node
12740 32 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12741 : OMP_CLAUSE_MAP);
12742 32 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12743 32 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12744 64 : OMP_CLAUSE_SIZE (alloc_node)
12745 32 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12746 :
12747 32 : OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
12748 32 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12749 32 : *insert_node_pos = l;
12750 32 : *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
12751 : }
12752 : else
12753 873 : grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
12754 : }
12755 :
12756 3060 : unsigned last_access = base_token + 1;
12757 :
12758 3060 : while (last_access + 1 < addr_tokens.length ()
12759 3395 : && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
12760 : last_access++;
12761 :
12762 3060 : if ((region_type & ORT_TARGET)
12763 3060 : && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
12764 : {
12765 1708 : bool base_ref = false;
12766 1708 : access_method_kinds access_kind
12767 1708 : = addr_tokens[last_access]->u.access_kind;
12768 :
12769 1708 : switch (access_kind)
12770 : {
12771 : case ACCESS_DIRECT:
12772 : case ACCESS_INDEXED_ARRAY:
12773 1060 : return NULL;
12774 :
12775 403 : case ACCESS_REF:
12776 403 : case ACCESS_REF_TO_POINTER:
12777 403 : case ACCESS_REF_TO_POINTER_OFFSET:
12778 403 : case ACCESS_INDEXED_REF_TO_ARRAY:
12779 403 : base_ref = true;
12780 403 : break;
12781 :
12782 866 : default:
12783 866 : ;
12784 : }
12785 866 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12786 : OMP_CLAUSE_MAP);
12787 866 : enum gomp_map_kind mkind;
12788 866 : omp_mapping_group *decl_group;
12789 866 : tree use_base;
12790 866 : switch (access_kind)
12791 : {
12792 463 : case ACCESS_POINTER:
12793 463 : case ACCESS_POINTER_OFFSET:
12794 463 : use_base = addr_tokens[last_access]->expr;
12795 463 : break;
12796 198 : case ACCESS_REF_TO_POINTER:
12797 198 : case ACCESS_REF_TO_POINTER_OFFSET:
12798 198 : use_base
12799 198 : = build_fold_indirect_ref (addr_tokens[last_access]->expr);
12800 198 : break;
12801 205 : default:
12802 205 : use_base = addr_tokens[base_token]->expr;
12803 : }
12804 866 : bool mapped_to_p
12805 866 : = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
12806 : true, false, true);
12807 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12808 866 : && DECL_P (addr_tokens[last_access]->expr)
12809 1372 : && !mapped_to_p)
12810 444 : mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
12811 : : GOMP_MAP_FIRSTPRIVATE_POINTER;
12812 : else
12813 : mkind = GOMP_MAP_ATTACH_DETACH;
12814 :
12815 866 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
12816 : /* If we have a reference to pointer base, we want to attach the
12817 : pointer here, not the reference. The reference attachment happens
12818 : elsewhere. */
12819 866 : bool ref_to_ptr
12820 866 : = (access_kind == ACCESS_REF_TO_POINTER
12821 866 : || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
12822 866 : tree sdecl = addr_tokens[last_access]->expr;
12823 866 : tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
12824 : : sdecl;
12825 : /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
12826 : want to use the reference itself for the decl, but we
12827 : still want to use the pointer to calculate the bias. */
12828 866 : OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
12829 866 : ? sdecl_ptr : sdecl;
12830 866 : sdecl = sdecl_ptr;
12831 866 : tree baddr = build_fold_addr_expr (base);
12832 866 : baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12833 : ptrdiff_type_node, baddr);
12834 866 : tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12835 : ptrdiff_type_node, sdecl);
12836 866 : OMP_CLAUSE_SIZE (c2)
12837 866 : = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
12838 : ptrdiff_type_node, baddr, decladdr);
12839 : /* Insert after struct node. */
12840 866 : OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
12841 866 : OMP_CLAUSE_CHAIN (l) = c2;
12842 :
12843 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12844 866 : && (addr_tokens[base_token]->u.structure_base_kind
12845 : == BASE_COMPONENT_EXPR)
12846 286 : && mkind == GOMP_MAP_ATTACH_DETACH
12847 1152 : && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
12848 : {
12849 218 : *inner = insert_node_pos;
12850 218 : if (openmp)
12851 166 : *fragile_p = true;
12852 : return NULL;
12853 : }
12854 : }
12855 :
12856 2000 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12857 2000 : && (addr_tokens[base_token]->u.structure_base_kind
12858 : == BASE_COMPONENT_EXPR)
12859 2467 : && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
12860 100 : *inner = insert_node_pos;
12861 :
12862 : return NULL;
12863 : }
12864 1619 : else if (struct_map_to_clause)
12865 : {
12866 1619 : tree *osc = struct_map_to_clause->get (base);
12867 1619 : tree *sc = NULL, *scp = NULL;
12868 :
12869 1619 : unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
12870 1619 : sc = &OMP_CLAUSE_CHAIN (*osc);
12871 : /* The struct mapping might be immediately followed by a
12872 : FIRSTPRIVATE_POINTER, FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
12873 : if it's an indirect access or a reference, or if the structure base
12874 : is not a decl. The FIRSTPRIVATE_* nodes are removed in omp-low.cc
12875 : after they have been processed there, and ATTACH_DETACH nodes are
12876 : recomputed and moved out of the GOMP_MAP_STRUCT construct once
12877 : sibling list building is complete. */
12878 1619 : if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
12879 1574 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
12880 3148 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
12881 201 : sc = &OMP_CLAUSE_CHAIN (*sc);
12882 4108 : for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
12883 3062 : if (attach_detach && sc == grp_start_p)
12884 : break;
12885 3062 : else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
12886 182 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
12887 3244 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
12888 : break;
12889 : else
12890 : {
12891 3062 : tree sc_decl = OMP_CLAUSE_DECL (*sc);
12892 3062 : poly_offset_int offset;
12893 3062 : poly_int64 bitpos;
12894 :
12895 3062 : if (TREE_CODE (sc_decl) == ARRAY_REF)
12896 : {
12897 366 : while (TREE_CODE (sc_decl) == ARRAY_REF)
12898 184 : sc_decl = TREE_OPERAND (sc_decl, 0);
12899 182 : if (TREE_CODE (sc_decl) != COMPONENT_REF
12900 182 : || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
12901 : break;
12902 : }
12903 2880 : else if (INDIRECT_REF_P (sc_decl)
12904 0 : && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
12905 2880 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
12906 : == REFERENCE_TYPE))
12907 0 : sc_decl = TREE_OPERAND (sc_decl, 0);
12908 :
12909 3062 : bool variable_offset2;
12910 3062 : tree iterators2 = OMP_CLAUSE_HAS_ITERATORS (*sc)
12911 3062 : ? OMP_CLAUSE_ITERATORS (*sc) : NULL_TREE;
12912 :
12913 3062 : tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
12914 : &variable_offset2,
12915 : iterators2);
12916 3062 : if (!base2 || !operand_equal_p (base2, base, 0))
12917 : break;
12918 3062 : if (scp)
12919 469 : continue;
12920 2842 : if (variable_offset2)
12921 : {
12922 341 : OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
12923 :
12924 341 : if (has_descriptor)
12925 : {
12926 : /* Sort mapped components by offset. This is needed for
12927 : libgomp to handle Fortran derived-type allocatable
12928 : components transparently. */
12929 :
12930 323 : poly_int64 bitsize;
12931 323 : tree offset, coffset;
12932 323 : machine_mode mode;
12933 323 : int unsignedp, reversep, volatilep;
12934 323 : tree inner_ref1
12935 323 : = get_inner_reference (sc_decl, &bitsize, &bitpos,
12936 : &offset, &mode, &unsignedp,
12937 : &reversep, &volatilep);
12938 323 : tree osc_decl = ocd;
12939 323 : STRIP_NOPS (osc_decl);
12940 323 : tree inner_ref2
12941 323 : = get_inner_reference (osc_decl, &bitsize, &bitpos,
12942 : &coffset, &mode, &unsignedp,
12943 : &reversep, &volatilep);
12944 323 : gcc_assert (operand_equal_p (inner_ref1, inner_ref2, 0));
12945 323 : tree offset_diff
12946 323 : = fold_binary_to_constant (MINUS_EXPR, size_type_node,
12947 : coffset, offset);
12948 572 : if (offset_diff == NULL_TREE
12949 323 : || TREE_INT_CST_ELT (offset_diff, 0) > 0)
12950 249 : continue;
12951 : else
12952 : break;
12953 : }
12954 : }
12955 2501 : else if ((region_type & ORT_ACC) != 0)
12956 : {
12957 : /* For OpenACC, allow (ignore) duplicate struct accesses in
12958 : the middle of a mapping clause, e.g. "mystruct->foo" in:
12959 : copy(mystruct->foo->bar) copy(mystruct->foo->qux). */
12960 223 : if (reprocessing_struct
12961 8 : && known_eq (coffset, offset)
12962 223 : && known_eq (cbitpos, bitpos))
12963 16 : return NULL;
12964 : }
12965 2278 : else if (known_eq (coffset, offset)
12966 2278 : && known_eq (cbitpos, bitpos))
12967 : {
12968 : /* Having two struct members at the same offset doesn't work,
12969 : so make sure we don't. (We're allowed to ignore this.
12970 : Should we report the error?) */
12971 : /*error_at (OMP_CLAUSE_LOCATION (grp_end),
12972 : "duplicate struct member %qE in map clauses",
12973 : OMP_CLAUSE_DECL (grp_end));*/
12974 : return NULL;
12975 : }
12976 2503 : if (maybe_lt (coffset, offset)
12977 4231 : || (known_eq (coffset, offset)
12978 20 : && maybe_lt (cbitpos, bitpos)))
12979 : {
12980 775 : if (attach_detach)
12981 : scp = sc;
12982 : else
12983 : break;
12984 : }
12985 : }
12986 :
12987 1603 : OMP_CLAUSE_SIZE (*osc)
12988 1603 : = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
12989 :
12990 1603 : if (reprocessing_struct)
12991 : {
12992 : /* If we're reprocessing a struct node, we don't want to do most of
12993 : the list manipulation below. We only need to handle the (pointer
12994 : or reference) attach/detach case. */
12995 8 : tree extra_node, alloc_node;
12996 8 : if (has_descriptor)
12997 0 : gcc_unreachable ();
12998 8 : else if (attach_detach)
12999 8 : alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
13000 : grp_end, &extra_node);
13001 : else
13002 : {
13003 : /* If we don't have an attach/detach node, this is a
13004 : "target data" directive or similar, not an offload region.
13005 : Synthesize an "alloc" node using just the initiating
13006 : GOMP_MAP_STRUCT decl. */
13007 0 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
13008 0 : || code == OACC_EXIT_DATA)
13009 0 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
13010 0 : alloc_node
13011 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
13012 : OMP_CLAUSE_MAP);
13013 0 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
13014 0 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
13015 0 : OMP_CLAUSE_SIZE (alloc_node)
13016 0 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
13017 : }
13018 :
13019 8 : if (scp)
13020 0 : omp_siblist_insert_node_after (alloc_node, scp);
13021 : else
13022 : {
13023 8 : tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
13024 8 : if (sc == *added_tail)
13025 8 : *added_tail = new_end;
13026 : }
13027 :
13028 8 : return NULL;
13029 : }
13030 :
13031 1595 : if (has_descriptor)
13032 : {
13033 538 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
13034 538 : if (code == OMP_TARGET_EXIT_DATA
13035 538 : || code == OACC_EXIT_DATA)
13036 105 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
13037 1076 : omp_siblist_move_node_after (desc,
13038 538 : &OMP_CLAUSE_CHAIN (*grp_start_p),
13039 : scp ? scp : sc);
13040 : }
13041 1057 : else if (attach_detach)
13042 : {
13043 325 : tree cl = NULL_TREE, extra_node;
13044 325 : tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
13045 : grp_end, &extra_node);
13046 325 : tree *tail_chain = NULL;
13047 :
13048 325 : if (*fragile_p
13049 325 : || (openmp
13050 : && !target
13051 : && attach_detach
13052 62 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
13053 23 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
13054 : {
13055 6 : if (!lang_GNU_Fortran ())
13056 6 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
13057 6 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
13058 : }
13059 :
13060 : /* Here, we have:
13061 :
13062 : grp_end : the last (or only) node in this group.
13063 : grp_start_p : pointer to the first node in a pointer mapping group
13064 : up to and including GRP_END.
13065 : sc : pointer to the chain for the end of the struct component
13066 : list.
13067 : scp : pointer to the chain for the sorted position at which we
13068 : should insert in the middle of the struct component list
13069 : (else NULL to insert at end).
13070 : alloc_node : the "alloc" node for the structure (pointer-type)
13071 : component. We insert at SCP (if present), else SC
13072 : (the end of the struct component list).
13073 : extra_node : a newly-synthesized node for an additional indirect
13074 : pointer mapping or a Fortran pointer set, if needed.
13075 : cl : first node to prepend before grp_start_p.
13076 : tail_chain : pointer to chain of last prepended node.
13077 :
13078 : The general idea is we move the nodes for this struct mapping
13079 : together: the alloc node goes into the sorted list directly after
13080 : the struct mapping, and any extra nodes (together with the nodes
13081 : mapping arrays pointed to by struct components) get moved after
13082 : that list. When SCP is NULL, we insert the nodes at SC, i.e. at
13083 : the end of the struct component mapping list. It's important that
13084 : the alloc_node comes first in that case because it's part of the
13085 : sorted component mapping list (but subsequent nodes are not!). */
13086 :
13087 325 : if (scp)
13088 254 : omp_siblist_insert_node_after (alloc_node, scp);
13089 :
13090 : /* Make [cl,tail_chain] a list of the alloc node (if we haven't
13091 : already inserted it) and the extra_node (if it is present). The
13092 : list can be empty if we added alloc_node above and there is no
13093 : extra node. */
13094 254 : if (scp && extra_node)
13095 : {
13096 0 : cl = extra_node;
13097 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13098 : }
13099 325 : else if (extra_node)
13100 : {
13101 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
13102 0 : cl = alloc_node;
13103 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13104 : }
13105 325 : else if (!scp)
13106 : {
13107 71 : cl = alloc_node;
13108 71 : tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
13109 : }
13110 :
13111 71 : continue_at
13112 71 : = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
13113 : grp_start_p, grp_end,
13114 : sc)
13115 254 : : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
13116 : }
13117 732 : else if (*sc != grp_end)
13118 : {
13119 692 : gcc_assert (*grp_start_p == grp_end);
13120 :
13121 : /* We are moving the current node back to a previous struct node:
13122 : the node that used to point to the current node will now point to
13123 : the next node. */
13124 692 : continue_at = grp_start_p;
13125 : /* In the non-pointer case, the mapping clause itself is moved into
13126 : the correct position in the struct component list, which in this
13127 : case is just SC. */
13128 692 : omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
13129 : }
13130 : }
13131 : return continue_at;
13132 : }
13133 :
13134 : /* Scan through GROUPS, and create sorted structure sibling lists without
13135 : gimplifying. */
13136 :
13137 : static bool
13138 18080 : omp_build_struct_sibling_lists (enum tree_code code,
13139 : enum omp_region_type region_type,
13140 : vec<omp_mapping_group> *groups,
13141 : hash_map<tree_operand_hash_no_se,
13142 : omp_mapping_group *> **grpmap,
13143 : tree *list_p)
13144 : {
13145 18080 : using namespace omp_addr_tokenizer;
13146 18080 : unsigned i;
13147 18080 : omp_mapping_group *grp;
13148 18080 : hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
13149 18080 : bool success = true;
13150 18080 : tree *new_next = NULL;
13151 36160 : tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
13152 18080 : tree added_nodes = NULL_TREE;
13153 18080 : tree *added_tail = &added_nodes;
13154 18080 : auto_vec<omp_mapping_group> pre_hwm_groups;
13155 :
13156 67562 : FOR_EACH_VEC_ELT (*groups, i, grp)
13157 : {
13158 31402 : tree c = grp->grp_end;
13159 31402 : tree decl = OMP_CLAUSE_DECL (c);
13160 31402 : tree grp_end = grp->grp_end;
13161 31402 : auto_vec<omp_addr_token *> addr_tokens;
13162 31402 : tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
13163 :
13164 31402 : if (new_next && !grp->reprocess_struct)
13165 622 : grp->grp_start = new_next;
13166 :
13167 31402 : new_next = NULL;
13168 :
13169 31402 : tree *grp_start_p = grp->grp_start;
13170 :
13171 31402 : if (DECL_P (decl))
13172 21232 : continue;
13173 :
13174 : /* Skip groups we marked for deletion in
13175 : {omp,oacc}_resolve_clause_dependencies. */
13176 10170 : if (grp->deleted)
13177 462 : continue;
13178 :
13179 9708 : if (OMP_CLAUSE_CHAIN (*grp_start_p)
13180 9708 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
13181 : {
13182 : /* Don't process an array descriptor that isn't inside a derived type
13183 : as a struct (the GOMP_MAP_POINTER following will have the form
13184 : "var.data", but such mappings are handled specially). */
13185 6134 : tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
13186 6134 : if (omp_map_clause_descriptor_p (grpmid)
13187 10285 : && DECL_P (OMP_CLAUSE_DECL (grpmid)))
13188 2807 : continue;
13189 : }
13190 :
13191 7945 : tree expr = decl;
13192 :
13193 7945 : while (TREE_CODE (expr) == ARRAY_REF)
13194 1044 : expr = TREE_OPERAND (expr, 0);
13195 :
13196 6901 : if (!omp_parse_expr (addr_tokens, expr))
13197 0 : continue;
13198 :
13199 6901 : omp_addr_token *last_token
13200 6901 : = omp_first_chained_access_token (addr_tokens);
13201 :
13202 : /* A mapping of a reference to a pointer member that doesn't specify an
13203 : array section, etc., like this:
13204 : *mystruct.ref_to_ptr
13205 : should not be processed by the struct sibling-list handling code --
13206 : it just transfers the referenced pointer.
13207 :
13208 : In contrast, the quite similar-looking construct:
13209 : *mystruct.ptr
13210 : which is equivalent to e.g.
13211 : mystruct.ptr[0]
13212 : *does* trigger sibling-list processing.
13213 :
13214 : An exception for the former case is for "fragile" groups where the
13215 : reference itself is not handled otherwise; this is subject to special
13216 : handling in omp_accumulate_sibling_list also. */
13217 :
13218 6901 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
13219 3402 : && last_token->type == ACCESS_METHOD
13220 3402 : && last_token->u.access_kind == ACCESS_REF
13221 7183 : && !grp->fragile)
13222 217 : continue;
13223 :
13224 6684 : tree d = decl;
13225 6684 : if (TREE_CODE (d) == ARRAY_REF)
13226 : {
13227 2036 : while (TREE_CODE (d) == ARRAY_REF)
13228 1029 : d = TREE_OPERAND (d, 0);
13229 1007 : if (TREE_CODE (d) == COMPONENT_REF
13230 1007 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
13231 : decl = d;
13232 : }
13233 6684 : if (d == decl
13234 5990 : && INDIRECT_REF_P (decl)
13235 738 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
13236 105 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
13237 : == REFERENCE_TYPE)
13238 6781 : && (OMP_CLAUSE_MAP_KIND (c)
13239 : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
13240 97 : decl = TREE_OPERAND (decl, 0);
13241 :
13242 6684 : STRIP_NOPS (decl);
13243 :
13244 6684 : if (TREE_CODE (decl) != COMPONENT_REF)
13245 1376 : continue;
13246 :
13247 : /* If we're mapping the whole struct in another node, skip adding this
13248 : node to a sibling list. */
13249 5308 : omp_mapping_group *wholestruct;
13250 5308 : if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
13251 : &wholestruct))
13252 : {
13253 178 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
13254 161 : OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c) = 0;
13255 178 : continue;
13256 : }
13257 :
13258 5130 : if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
13259 5130 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
13260 5025 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
13261 : && code != OACC_UPDATE
13262 10088 : && code != OMP_TARGET_UPDATE)
13263 : {
13264 4767 : if (error_operand_p (decl))
13265 : {
13266 0 : success = false;
13267 0 : goto error_out;
13268 : }
13269 :
13270 4767 : tree stype = TREE_TYPE (decl);
13271 4767 : if (TREE_CODE (stype) == REFERENCE_TYPE)
13272 768 : stype = TREE_TYPE (stype);
13273 4767 : if (TYPE_SIZE_UNIT (stype) == NULL
13274 4767 : || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
13275 : {
13276 0 : error_at (OMP_CLAUSE_LOCATION (c),
13277 : "mapping field %qE of variable length "
13278 0 : "structure", OMP_CLAUSE_DECL (c));
13279 0 : success = false;
13280 0 : goto error_out;
13281 : }
13282 :
13283 4767 : tree *inner = NULL;
13284 4767 : bool fragile_p = grp->fragile;
13285 :
13286 4767 : new_next
13287 4767 : = omp_accumulate_sibling_list (region_type, code,
13288 : struct_map_to_clause, *grpmap,
13289 : grp_start_p, grp_end, addr_tokens,
13290 : &inner, &fragile_p,
13291 : grp->reprocess_struct, &added_tail);
13292 :
13293 4767 : if (inner)
13294 : {
13295 318 : omp_mapping_group newgrp;
13296 318 : newgrp.grp_start = inner;
13297 318 : if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
13298 : == GOMP_MAP_ATTACH_DETACH)
13299 286 : newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
13300 : else
13301 : newgrp.grp_end = *inner;
13302 318 : newgrp.mark = UNVISITED;
13303 318 : newgrp.sibling = NULL;
13304 318 : newgrp.deleted = false;
13305 318 : newgrp.reprocess_struct = true;
13306 318 : newgrp.fragile = fragile_p;
13307 318 : newgrp.next = NULL;
13308 318 : groups->safe_push (newgrp);
13309 :
13310 : /* !!! Growing GROUPS might invalidate the pointers in the group
13311 : map. Rebuild it here. This is a bit inefficient, but
13312 : shouldn't happen very often. */
13313 636 : delete (*grpmap);
13314 318 : *grpmap
13315 318 : = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
13316 : sentinel);
13317 : }
13318 : }
13319 31402 : }
13320 :
13321 : /* Delete groups marked for deletion above. At this point the order of the
13322 : groups may no longer correspond to the order of the underlying list,
13323 : which complicates this a little. First clear out OMP_CLAUSE_DECL for
13324 : deleted nodes... */
13325 :
13326 49482 : FOR_EACH_VEC_ELT (*groups, i, grp)
13327 31402 : if (grp->deleted)
13328 462 : for (tree d = *grp->grp_start;
13329 924 : d != OMP_CLAUSE_CHAIN (grp->grp_end);
13330 462 : d = OMP_CLAUSE_CHAIN (d))
13331 462 : OMP_CLAUSE_DECL (d) = NULL_TREE;
13332 :
13333 : /* ...then sweep through the list removing the now-empty nodes. */
13334 :
13335 : tail = list_p;
13336 96293 : while (*tail)
13337 : {
13338 78213 : if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
13339 78213 : && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
13340 462 : *tail = OMP_CLAUSE_CHAIN (*tail);
13341 : else
13342 77751 : tail = &OMP_CLAUSE_CHAIN (*tail);
13343 : }
13344 :
13345 : /* Tack on the struct nodes added during nested struct reprocessing. */
13346 18080 : if (added_nodes)
13347 : {
13348 192 : *tail = added_nodes;
13349 192 : tail = added_tail;
13350 : }
13351 :
13352 : /* Find each attach node whose bias needs to be adjusted and move it to the
13353 : group containing its pointee, right after the struct node, so that it can
13354 : be picked up by the adjustment code further down in this function. */
13355 18080 : bool attach_bias_needs_adjustment;
13356 18080 : attach_bias_needs_adjustment = false;
13357 67562 : FOR_EACH_VEC_ELT_REVERSE (*groups, i, grp)
13358 : {
13359 31402 : tree c = *grp->grp_start;
13360 30463 : if (c != NULL && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13361 30448 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
13362 27375 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
13363 3353 : && OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c)) == GOMP_MAP_TO_PSET
13364 560 : && OMP_CLAUSE_MAP_KIND (grp->grp_end) == GOMP_MAP_ATTACH_DETACH
13365 31962 : && OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (grp->grp_end))
13366 : {
13367 90 : OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (grp->grp_end) = 0;
13368 90 : attach_bias_needs_adjustment = true;
13369 90 : tree *cp;
13370 90 : for (cp = &OMP_CLAUSE_CHAIN (c); cp != NULL;
13371 206 : cp = &OMP_CLAUSE_CHAIN (*cp))
13372 296 : if (*cp == grp->grp_end)
13373 : {
13374 : c = *cp;
13375 : break;
13376 : }
13377 :
13378 90 : tree base = OMP_CLAUSE_DECL (c);
13379 90 : gcc_assert (TREE_CODE (base) == NOP_EXPR);
13380 90 : base = build_fold_indirect_ref (base);
13381 90 : tree *struct_node = struct_map_to_clause->get (base);
13382 90 : omp_siblist_move_node_after (c, cp, &OMP_CLAUSE_CHAIN (*struct_node));
13383 : }
13384 : }
13385 :
13386 : /* Now we have finished building the struct sibling lists, reprocess
13387 : newly-added "attach" nodes: we need the address of the first
13388 : mapped element of each struct sibling list for the bias of the attach
13389 : operation -- not necessarily the base address of the whole struct. */
13390 18080 : if (struct_map_to_clause)
13391 3060 : for (hash_map<tree_operand_hash, tree>::iterator iter
13392 2497 : = struct_map_to_clause->begin ();
13393 8054 : iter != struct_map_to_clause->end ();
13394 3060 : ++iter)
13395 : {
13396 3060 : tree struct_node = (*iter).second;
13397 3060 : gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
13398 3060 : tree attach = OMP_CLAUSE_CHAIN (struct_node);
13399 :
13400 3060 : if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
13401 3060 : || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
13402 2548 : continue;
13403 :
13404 512 : OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
13405 :
13406 : /* Sanity check: the standalone attach node will not work if we have
13407 : an "enter data" operation (because for those, variables need to be
13408 : mapped separately and attach nodes must be grouped together with the
13409 : base they attach to). We should only have created the
13410 : ATTACH_DETACH node either after GOMP_MAP_STRUCT for a target region
13411 : or for an intermediate descriptor that needs adjustment -- so this
13412 : should never be true. */
13413 512 : gcc_assert ((region_type & ORT_TARGET) != 0
13414 : || attach_bias_needs_adjustment);
13415 :
13416 : /* This is the first sorted node in the struct sibling list. Use it
13417 : to recalculate the correct bias to use.
13418 : (&first_node - attach_decl).
13419 : For GOMP_MAP_STRUCT_UNORD, we need e.g. the
13420 : min(min(min(first,second),third),fourth) element, because the
13421 : elements aren't in any particular order. */
13422 512 : tree lowest_addr;
13423 512 : if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
13424 : {
13425 94 : tree first_node = OMP_CLAUSE_CHAIN (attach);
13426 94 : unsigned HOST_WIDE_INT num_mappings
13427 94 : = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
13428 94 : lowest_addr = OMP_CLAUSE_DECL (first_node);
13429 94 : lowest_addr = build_fold_addr_expr (lowest_addr);
13430 94 : lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
13431 94 : tree next_node = OMP_CLAUSE_CHAIN (first_node);
13432 185 : while (num_mappings > 1)
13433 : {
13434 91 : tree tmp = OMP_CLAUSE_DECL (next_node);
13435 91 : tmp = build_fold_addr_expr (tmp);
13436 91 : tmp = fold_convert (pointer_sized_int_node, tmp);
13437 91 : lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
13438 : lowest_addr, tmp);
13439 91 : next_node = OMP_CLAUSE_CHAIN (next_node);
13440 91 : num_mappings--;
13441 : }
13442 94 : lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
13443 : }
13444 : else
13445 : {
13446 418 : tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
13447 418 : first_node = build_fold_addr_expr (first_node);
13448 418 : lowest_addr = fold_convert (ptrdiff_type_node, first_node);
13449 : }
13450 512 : tree attach_decl = OMP_CLAUSE_DECL (attach);
13451 512 : attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
13452 512 : OMP_CLAUSE_SIZE (attach)
13453 512 : = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
13454 : attach_decl);
13455 :
13456 : /* Remove GOMP_MAP_ATTACH node from after struct node. */
13457 512 : OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
13458 : /* ...and re-insert it at the end of our clause list. */
13459 512 : *tail = attach;
13460 512 : OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
13461 512 : tail = &OMP_CLAUSE_CHAIN (attach);
13462 : }
13463 :
13464 15583 : error_out:
13465 18080 : if (struct_map_to_clause)
13466 2497 : delete struct_map_to_clause;
13467 :
13468 18080 : return success;
13469 18080 : }
13470 :
13471 : struct instantiate_mapper_info
13472 : {
13473 : tree *mapper_clauses_p;
13474 : struct gimplify_omp_ctx *omp_ctx;
13475 : gimple_seq *pre_p;
13476 : };
13477 :
13478 : /* Helper function for omp_instantiate_mapper. */
13479 :
13480 : static tree
13481 1592 : remap_mapper_decl_1 (tree *tp, int *walk_subtrees, void *data)
13482 : {
13483 1592 : copy_body_data *id = (copy_body_data *) data;
13484 :
13485 1592 : if (DECL_P (*tp))
13486 : {
13487 724 : tree replacement = remap_decl (*tp, id);
13488 724 : if (*tp != replacement)
13489 : {
13490 369 : *tp = unshare_expr (replacement);
13491 369 : *walk_subtrees = 0;
13492 : }
13493 : }
13494 :
13495 1592 : return NULL_TREE;
13496 : }
13497 :
13498 : /* A copy_decl implementation (for use with tree-inline.cc functions) that
13499 : only transform decls or SSA names that are part of a map we already
13500 : prepared. */
13501 :
13502 : static tree
13503 126 : omp_mapper_copy_decl (tree var, copy_body_data *cb)
13504 : {
13505 126 : tree *repl = cb->decl_map->get (var);
13506 :
13507 126 : if (repl)
13508 0 : return *repl;
13509 :
13510 126 : return var;
13511 : }
13512 :
13513 : static tree *
13514 90 : omp_instantiate_mapper (gimple_seq *pre_p,
13515 : hash_map<omp_name_type<tree>, tree> *implicit_mappers,
13516 : tree mapperfn, tree expr, enum gomp_map_kind outer_kind,
13517 : tree *mapper_clauses_p)
13518 : {
13519 90 : tree mapper_name = NULL_TREE;
13520 90 : tree mapper = lang_hooks.decls.omp_extract_mapper_directive (mapperfn);
13521 90 : gcc_assert (TREE_CODE (mapper) == OMP_DECLARE_MAPPER);
13522 :
13523 90 : tree clause = OMP_DECLARE_MAPPER_CLAUSES (mapper);
13524 90 : tree dummy_var = OMP_DECLARE_MAPPER_DECL (mapper);
13525 :
13526 : /* The "extraction map" is used to map the mapper variable in the "declare
13527 : mapper" directive, and also any temporary variables that have been created
13528 : as part of expanding the mapper function's body (which are expanded as a
13529 : "bind" expression in the pre_p sequence). */
13530 90 : hash_map<tree, tree> extraction_map;
13531 :
13532 90 : extraction_map.put (dummy_var, expr);
13533 90 : extraction_map.put (expr, expr);
13534 :
13535 : /* This copy_body_data is only used to remap the decls in the
13536 : OMP_DECLARE_MAPPER tree node expansion itself. All relevant decls should
13537 : already be in the current function. */
13538 90 : copy_body_data id;
13539 90 : memset (&id, 0, sizeof (id));
13540 90 : id.src_fn = current_function_decl;
13541 90 : id.dst_fn = current_function_decl;
13542 90 : id.src_cfun = cfun;
13543 90 : id.decl_map = &extraction_map;
13544 90 : id.copy_decl = omp_mapper_copy_decl;
13545 90 : id.transform_call_graph_edges = CB_CGE_DUPLICATE; // ???
13546 90 : id.transform_new_cfg = true; // ???
13547 :
13548 282 : for (; clause; clause = OMP_CLAUSE_CHAIN (clause))
13549 : {
13550 192 : enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (clause);
13551 192 : tree *nested_mapper_p = NULL;
13552 :
13553 192 : if (map_kind == GOMP_MAP_PUSH_MAPPER_NAME)
13554 : {
13555 0 : mapper_name = OMP_CLAUSE_DECL (clause);
13556 25 : continue;
13557 : }
13558 192 : else if (map_kind == GOMP_MAP_POP_MAPPER_NAME)
13559 : {
13560 0 : mapper_name = NULL_TREE;
13561 0 : continue;
13562 : }
13563 :
13564 192 : if (OMP_CLAUSE_HAS_ITERATORS (clause))
13565 : {
13566 2 : sorry_at (OMP_CLAUSE_LOCATION (clause),
13567 : "user-defined mapper that uses a %<map%> clause "
13568 : "with %<iterator%>");
13569 2 : continue;
13570 : }
13571 :
13572 190 : tree decl = OMP_CLAUSE_DECL (clause);
13573 190 : tree unshared, type;
13574 190 : bool nonunit_array_with_mapper = false;
13575 :
13576 190 : if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
13577 : {
13578 62 : location_t loc = OMP_CLAUSE_LOCATION (clause);
13579 62 : tree tmp = lang_hooks.decls.omp_map_array_section (loc, decl);
13580 62 : if (tmp == decl)
13581 : {
13582 48 : unshared = unshare_expr (clause);
13583 48 : nonunit_array_with_mapper = true;
13584 48 : type = TREE_TYPE (TREE_TYPE (decl));
13585 : }
13586 : else
13587 : {
13588 14 : unshared = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
13589 14 : OMP_CLAUSE_CODE (clause));
13590 14 : OMP_CLAUSE_DECL (unshared) = tmp;
13591 14 : OMP_CLAUSE_SIZE (unshared)
13592 28 : = DECL_P (tmp) ? DECL_SIZE_UNIT (tmp)
13593 14 : : TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13594 14 : type = TREE_TYPE (tmp);
13595 : }
13596 : }
13597 : else
13598 : {
13599 128 : unshared = unshare_expr (clause);
13600 128 : type = TREE_TYPE (decl);
13601 : }
13602 :
13603 190 : walk_tree (&unshared, remap_mapper_decl_1, &id, NULL);
13604 :
13605 190 : if (OMP_CLAUSE_MAP_KIND (unshared) == GOMP_MAP_UNSET)
13606 28 : OMP_CLAUSE_SET_MAP_KIND (unshared, outer_kind);
13607 :
13608 190 : decl = OMP_CLAUSE_DECL (unshared);
13609 190 : type = TYPE_MAIN_VARIANT (type);
13610 :
13611 190 : nested_mapper_p = implicit_mappers->get ({ mapper_name, type });
13612 :
13613 190 : if (nested_mapper_p && *nested_mapper_p != mapperfn)
13614 : {
13615 23 : if (nonunit_array_with_mapper)
13616 : {
13617 8 : sorry ("user-defined mapper with non-unit length array section");
13618 8 : continue;
13619 : }
13620 :
13621 15 : if (map_kind == GOMP_MAP_UNSET)
13622 0 : map_kind = outer_kind;
13623 :
13624 15 : mapper_clauses_p
13625 15 : = omp_instantiate_mapper (pre_p, implicit_mappers,
13626 : *nested_mapper_p, decl, map_kind,
13627 : mapper_clauses_p);
13628 15 : continue;
13629 : }
13630 :
13631 167 : *mapper_clauses_p = unshared;
13632 167 : mapper_clauses_p = &OMP_CLAUSE_CHAIN (unshared);
13633 : }
13634 :
13635 90 : return mapper_clauses_p;
13636 90 : }
13637 :
13638 : static int
13639 138587 : omp_instantiate_implicit_mappers (splay_tree_node n, void *data)
13640 : {
13641 138587 : tree decl = (tree) n->key;
13642 138587 : instantiate_mapper_info *im_info = (instantiate_mapper_info *) data;
13643 138587 : gimplify_omp_ctx *ctx = im_info->omp_ctx;
13644 138587 : tree *mapper_p = NULL;
13645 138587 : tree type = TREE_TYPE (decl);
13646 138587 : bool ref_p = false;
13647 138587 : unsigned flags = n->value;
13648 :
13649 138587 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
13650 : return 0;
13651 23772 : if ((flags & GOVD_SEEN) == 0)
13652 : return 0;
13653 : /* If we already have clauses pertaining to a struct variable, then we don't
13654 : want to implicitly invoke a user-defined mapper. */
13655 20238 : if ((flags & GOVD_EXPLICIT) != 0 && AGGREGATE_TYPE_P (TREE_TYPE (decl)))
13656 : return 0;
13657 :
13658 20238 : if (TREE_CODE (type) == REFERENCE_TYPE)
13659 : {
13660 909 : ref_p = true;
13661 909 : type = TREE_TYPE (type);
13662 : }
13663 :
13664 20238 : type = TYPE_MAIN_VARIANT (type);
13665 :
13666 20238 : if (DECL_P (decl) && type && AGGREGATE_TYPE_P (type))
13667 : {
13668 5311 : gcc_assert (ctx);
13669 5311 : mapper_p = ctx->implicit_mappers->get ({ NULL_TREE, type });
13670 : }
13671 :
13672 5311 : if (mapper_p)
13673 : {
13674 : /* If we have a reference, map the pointed-to object rather than the
13675 : reference itself. */
13676 75 : if (ref_p)
13677 2 : decl = build_fold_indirect_ref (decl);
13678 :
13679 75 : im_info->mapper_clauses_p
13680 75 : = omp_instantiate_mapper (im_info->pre_p, ctx->implicit_mappers,
13681 : *mapper_p, decl, GOMP_MAP_TOFROM,
13682 : im_info->mapper_clauses_p);
13683 : /* Make sure we don't map the same variable implicitly in
13684 : gimplify_adjust_omp_clauses_1 also. */
13685 75 : n->value |= GOVD_EXPLICIT;
13686 : }
13687 :
13688 : return 0;
13689 : }
13690 :
13691 : /* Scan the OMP clauses in *LIST_P, installing mappings into a new
13692 : and previous omp contexts. */
13693 :
13694 : static void
13695 131480 : gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
13696 : enum omp_region_type region_type,
13697 : enum tree_code code,
13698 : gimple_seq *loops_seq_p = NULL)
13699 : {
13700 131480 : using namespace omp_addr_tokenizer;
13701 131480 : struct gimplify_omp_ctx *ctx, *outer_ctx;
13702 131480 : tree c;
13703 131480 : tree *orig_list_p = list_p;
13704 131480 : int handled_depend_iterators = -1;
13705 131480 : int nowait = -1;
13706 :
13707 131480 : ctx = new_omp_context (region_type);
13708 131480 : ctx->code = code;
13709 131480 : outer_ctx = ctx->outer_context;
13710 131480 : if (code == OMP_TARGET)
13711 : {
13712 13538 : if (!lang_GNU_Fortran ())
13713 11166 : ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
13714 13538 : ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
13715 27076 : ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
13716 13538 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
13717 : }
13718 131480 : if (!lang_GNU_Fortran ())
13719 100144 : switch (code)
13720 : {
13721 18669 : case OMP_TARGET:
13722 18669 : case OMP_TARGET_DATA:
13723 18669 : case OMP_TARGET_ENTER_DATA:
13724 18669 : case OMP_TARGET_EXIT_DATA:
13725 18669 : case OACC_DECLARE:
13726 18669 : case OACC_HOST_DATA:
13727 18669 : case OACC_PARALLEL:
13728 18669 : case OACC_KERNELS:
13729 18669 : ctx->target_firstprivatize_array_bases = true;
13730 : default:
13731 : break;
13732 : }
13733 :
13734 131480 : vec<omp_mapping_group> *groups = NULL;
13735 131480 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
13736 131480 : unsigned grpnum = 0;
13737 131480 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
13738 :
13739 131480 : if (code == OMP_TARGET
13740 131480 : || code == OMP_TARGET_DATA
13741 131480 : || code == OMP_TARGET_ENTER_DATA
13742 : || code == OMP_TARGET_EXIT_DATA
13743 : || code == OACC_DATA
13744 : || code == OACC_KERNELS
13745 : || code == OACC_PARALLEL
13746 : || code == OACC_SERIAL
13747 : || code == OACC_ENTER_DATA
13748 : || code == OACC_EXIT_DATA
13749 : || code == OACC_UPDATE
13750 : || code == OACC_DECLARE)
13751 : {
13752 33972 : if (!(region_type & ORT_ACC))
13753 17460 : *list_p = omp_remove_duplicate_maps (*list_p, false);
13754 33972 : groups = omp_gather_mapping_groups (list_p);
13755 :
13756 33972 : if (groups)
13757 18281 : grpmap = omp_index_mapping_groups (groups);
13758 : }
13759 :
13760 337918 : while ((c = *list_p) != NULL)
13761 : {
13762 206438 : bool remove = false;
13763 206438 : bool notice_outer = true;
13764 206438 : bool map_descriptor;
13765 206438 : const char *check_non_private = NULL;
13766 206438 : unsigned int flags;
13767 206438 : tree decl;
13768 206438 : auto_vec<omp_addr_token *, 10> addr_tokens;
13769 206438 : tree op = NULL_TREE;
13770 206438 : location_t loc = OMP_CLAUSE_LOCATION (c);
13771 :
13772 244018 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
13773 : {
13774 : grp_start_p = NULL;
13775 : grp_end = NULL_TREE;
13776 : }
13777 :
13778 206438 : if (code == OMP_TARGET
13779 : || code == OMP_TARGET_DATA
13780 : || code == OMP_TARGET_ENTER_DATA
13781 173561 : || code == OMP_TARGET_EXIT_DATA)
13782 : /* Do some target-specific type checks for map operands. */
13783 35310 : switch (OMP_CLAUSE_CODE (c))
13784 : {
13785 25799 : case OMP_CLAUSE_MAP:
13786 25799 : op = OMP_CLAUSE_OPERAND (c, 0);
13787 25799 : verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
13788 25799 : break;
13789 186 : case OMP_CLAUSE_PRIVATE:
13790 186 : op = OMP_CLAUSE_OPERAND (c, 0);
13791 186 : verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
13792 186 : break;
13793 1374 : case OMP_CLAUSE_FIRSTPRIVATE:
13794 1374 : op = OMP_CLAUSE_OPERAND (c, 0);
13795 1374 : verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
13796 1374 : break;
13797 2923 : case OMP_CLAUSE_IS_DEVICE_PTR:
13798 2923 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13799 2923 : case OMP_CLAUSE_USE_DEVICE_PTR:
13800 2923 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13801 2923 : op = OMP_CLAUSE_OPERAND (c, 0);
13802 2923 : verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
13803 2923 : break;
13804 : default:
13805 : break;
13806 : }
13807 :
13808 206438 : switch (OMP_CLAUSE_CODE (c))
13809 : {
13810 12249 : case OMP_CLAUSE_PRIVATE:
13811 12249 : flags = GOVD_PRIVATE | GOVD_EXPLICIT;
13812 12249 : if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
13813 : {
13814 167 : flags |= GOVD_PRIVATE_OUTER_REF;
13815 167 : OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
13816 : }
13817 : else
13818 : notice_outer = false;
13819 12249 : goto do_add;
13820 5536 : case OMP_CLAUSE_SHARED:
13821 5536 : flags = GOVD_SHARED | GOVD_EXPLICIT;
13822 5536 : goto do_add;
13823 8053 : case OMP_CLAUSE_FIRSTPRIVATE:
13824 8053 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13825 8053 : check_non_private = "firstprivate";
13826 8053 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13827 : {
13828 380 : gcc_assert (code == OMP_TARGET);
13829 : flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
13830 : }
13831 8053 : goto do_add;
13832 7338 : case OMP_CLAUSE_LASTPRIVATE:
13833 7338 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13834 496 : switch (code)
13835 : {
13836 25 : case OMP_DISTRIBUTE:
13837 25 : error_at (OMP_CLAUSE_LOCATION (c),
13838 : "conditional %<lastprivate%> clause on "
13839 : "%qs construct", "distribute");
13840 25 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13841 25 : break;
13842 13 : case OMP_TASKLOOP:
13843 13 : error_at (OMP_CLAUSE_LOCATION (c),
13844 : "conditional %<lastprivate%> clause on "
13845 : "%qs construct", "taskloop");
13846 13 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13847 13 : break;
13848 : default:
13849 : break;
13850 : }
13851 7338 : flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
13852 7338 : if (code != OMP_LOOP)
13853 6987 : check_non_private = "lastprivate";
13854 7338 : decl = OMP_CLAUSE_DECL (c);
13855 7338 : if (error_operand_p (decl))
13856 0 : goto do_add;
13857 7338 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
13858 7338 : && !lang_hooks.decls.omp_scalar_p (decl, true))
13859 : {
13860 5 : error_at (OMP_CLAUSE_LOCATION (c),
13861 : "non-scalar variable %qD in conditional "
13862 : "%<lastprivate%> clause", decl);
13863 5 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13864 : }
13865 7338 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13866 453 : flags |= GOVD_LASTPRIVATE_CONDITIONAL;
13867 7338 : omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
13868 : false);
13869 7338 : goto do_add;
13870 15385 : case OMP_CLAUSE_REDUCTION:
13871 15385 : if (OMP_CLAUSE_REDUCTION_TASK (c))
13872 : {
13873 603 : if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13874 : {
13875 416 : if (nowait == -1)
13876 301 : nowait = omp_find_clause (*list_p,
13877 301 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
13878 416 : if (nowait
13879 15 : && (outer_ctx == NULL
13880 0 : || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
13881 : {
13882 15 : error_at (OMP_CLAUSE_LOCATION (c),
13883 : "%<task%> reduction modifier on a construct "
13884 : "with a %<nowait%> clause");
13885 15 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13886 : }
13887 : }
13888 187 : else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
13889 : {
13890 40 : error_at (OMP_CLAUSE_LOCATION (c),
13891 : "invalid %<task%> reduction modifier on construct "
13892 : "other than %<parallel%>, %qs, %<sections%> or "
13893 20 : "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
13894 20 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13895 : }
13896 : }
13897 15385 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
13898 851 : switch (code)
13899 : {
13900 4 : case OMP_SECTIONS:
13901 4 : error_at (OMP_CLAUSE_LOCATION (c),
13902 : "%<inscan%> %<reduction%> clause on "
13903 : "%qs construct", "sections");
13904 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13905 4 : break;
13906 4 : case OMP_PARALLEL:
13907 4 : error_at (OMP_CLAUSE_LOCATION (c),
13908 : "%<inscan%> %<reduction%> clause on "
13909 : "%qs construct", "parallel");
13910 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13911 4 : break;
13912 4 : case OMP_TEAMS:
13913 4 : error_at (OMP_CLAUSE_LOCATION (c),
13914 : "%<inscan%> %<reduction%> clause on "
13915 : "%qs construct", "teams");
13916 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13917 4 : break;
13918 4 : case OMP_TASKLOOP:
13919 4 : error_at (OMP_CLAUSE_LOCATION (c),
13920 : "%<inscan%> %<reduction%> clause on "
13921 : "%qs construct", "taskloop");
13922 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13923 4 : break;
13924 4 : case OMP_SCOPE:
13925 4 : error_at (OMP_CLAUSE_LOCATION (c),
13926 : "%<inscan%> %<reduction%> clause on "
13927 : "%qs construct", "scope");
13928 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13929 4 : break;
13930 : default:
13931 : break;
13932 : }
13933 : /* FALLTHRU */
13934 17989 : case OMP_CLAUSE_IN_REDUCTION:
13935 17989 : case OMP_CLAUSE_TASK_REDUCTION:
13936 17989 : flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
13937 : /* OpenACC permits reductions on private variables. */
13938 17989 : if (!(region_type & ORT_ACC)
13939 : /* taskgroup is actually not a worksharing region. */
13940 12470 : && code != OMP_TASKGROUP)
13941 11940 : check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
13942 17989 : decl = OMP_CLAUSE_DECL (c);
13943 17989 : if (TREE_CODE (decl) == MEM_REF)
13944 : {
13945 2614 : tree type = TREE_TYPE (decl);
13946 2614 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
13947 2614 : gimplify_ctxp->into_ssa = false;
13948 2614 : if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
13949 : NULL, is_gimple_val, fb_rvalue, false)
13950 : == GS_ERROR)
13951 : {
13952 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13953 0 : remove = true;
13954 0 : break;
13955 : }
13956 2614 : gimplify_ctxp->into_ssa = saved_into_ssa;
13957 2614 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13958 2614 : if (DECL_P (v))
13959 : {
13960 571 : omp_firstprivatize_variable (ctx, v);
13961 571 : omp_notice_variable (ctx, v, true);
13962 : }
13963 2614 : decl = TREE_OPERAND (decl, 0);
13964 2614 : if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
13965 : {
13966 477 : gimplify_ctxp->into_ssa = false;
13967 477 : if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
13968 : NULL, is_gimple_val, fb_rvalue, false)
13969 : == GS_ERROR)
13970 : {
13971 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13972 0 : remove = true;
13973 0 : break;
13974 : }
13975 477 : gimplify_ctxp->into_ssa = saved_into_ssa;
13976 477 : v = TREE_OPERAND (decl, 1);
13977 477 : if (DECL_P (v))
13978 : {
13979 477 : omp_firstprivatize_variable (ctx, v);
13980 477 : omp_notice_variable (ctx, v, true);
13981 : }
13982 477 : decl = TREE_OPERAND (decl, 0);
13983 : }
13984 2614 : if (TREE_CODE (decl) == ADDR_EXPR
13985 1220 : || TREE_CODE (decl) == INDIRECT_REF)
13986 1510 : decl = TREE_OPERAND (decl, 0);
13987 : }
13988 17989 : goto do_add_decl;
13989 2672 : case OMP_CLAUSE_LINEAR:
13990 2672 : if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
13991 : is_gimple_val, fb_rvalue) == GS_ERROR)
13992 : {
13993 : remove = true;
13994 : break;
13995 : }
13996 : else
13997 : {
13998 2672 : if (code == OMP_SIMD
13999 2672 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
14000 : {
14001 1154 : struct gimplify_omp_ctx *octx = outer_ctx;
14002 1154 : if (octx
14003 741 : && octx->region_type == ORT_WORKSHARE
14004 500 : && octx->combined_loop
14005 500 : && !octx->distribute)
14006 : {
14007 492 : if (octx->outer_context
14008 430 : && (octx->outer_context->region_type
14009 : == ORT_COMBINED_PARALLEL))
14010 393 : octx = octx->outer_context->outer_context;
14011 : else
14012 : octx = octx->outer_context;
14013 : }
14014 905 : if (octx
14015 316 : && octx->region_type == ORT_WORKSHARE
14016 16 : && octx->combined_loop
14017 16 : && octx->distribute)
14018 : {
14019 16 : error_at (OMP_CLAUSE_LOCATION (c),
14020 : "%<linear%> clause for variable other than "
14021 : "loop iterator specified on construct "
14022 : "combined with %<distribute%>");
14023 16 : remove = true;
14024 16 : break;
14025 : }
14026 : }
14027 : /* For combined #pragma omp parallel for simd, need to put
14028 : lastprivate and perhaps firstprivate too on the
14029 : parallel. Similarly for #pragma omp for simd. */
14030 : struct gimplify_omp_ctx *octx = outer_ctx;
14031 : bool taskloop_seen = false;
14032 : decl = NULL_TREE;
14033 3453 : do
14034 : {
14035 3453 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
14036 3453 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
14037 : break;
14038 3393 : decl = OMP_CLAUSE_DECL (c);
14039 3393 : if (error_operand_p (decl))
14040 : {
14041 : decl = NULL_TREE;
14042 : break;
14043 : }
14044 3393 : flags = GOVD_SEEN;
14045 3393 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
14046 1672 : flags |= GOVD_FIRSTPRIVATE;
14047 3393 : if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
14048 3393 : flags |= GOVD_LASTPRIVATE;
14049 3393 : if (octx
14050 2515 : && octx->region_type == ORT_WORKSHARE
14051 1218 : && octx->combined_loop)
14052 : {
14053 1212 : if (octx->outer_context
14054 1042 : && (octx->outer_context->region_type
14055 : == ORT_COMBINED_PARALLEL))
14056 : octx = octx->outer_context;
14057 478 : else if (omp_check_private (octx, decl, false))
14058 : break;
14059 : }
14060 : else if (octx
14061 1303 : && (octx->region_type & ORT_TASK) != 0
14062 307 : && octx->combined_loop)
14063 : taskloop_seen = true;
14064 : else if (octx
14065 1000 : && octx->region_type == ORT_COMBINED_PARALLEL
14066 301 : && ((ctx->region_type == ORT_WORKSHARE
14067 201 : && octx == outer_ctx)
14068 100 : || taskloop_seen))
14069 : flags = GOVD_SEEN | GOVD_SHARED;
14070 : else if (octx
14071 699 : && ((octx->region_type & ORT_COMBINED_TEAMS)
14072 : == ORT_COMBINED_TEAMS))
14073 : flags = GOVD_SEEN | GOVD_SHARED;
14074 542 : else if (octx
14075 542 : && octx->region_type == ORT_COMBINED_TARGET)
14076 : {
14077 197 : if (flags & GOVD_LASTPRIVATE)
14078 197 : flags = GOVD_SEEN | GOVD_MAP;
14079 : }
14080 : else
14081 : break;
14082 2080 : splay_tree_node on
14083 2080 : = splay_tree_lookup (octx->variables,
14084 : (splay_tree_key) decl);
14085 2080 : if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
14086 : {
14087 : octx = NULL;
14088 : break;
14089 : }
14090 2076 : omp_add_variable (octx, decl, flags);
14091 2076 : if (octx->outer_context == NULL)
14092 : break;
14093 : octx = octx->outer_context;
14094 : }
14095 : while (1);
14096 2656 : if (octx
14097 2656 : && decl
14098 2656 : && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
14099 731 : || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
14100 1714 : omp_notice_variable (octx, decl, true);
14101 : }
14102 2656 : flags = GOVD_LINEAR | GOVD_EXPLICIT;
14103 2656 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
14104 2656 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
14105 : {
14106 : notice_outer = false;
14107 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
14108 : }
14109 2656 : goto do_add;
14110 :
14111 51384 : case OMP_CLAUSE_MAP:
14112 51384 : if (!grp_start_p)
14113 : {
14114 31179 : grp_start_p = list_p;
14115 31179 : grp_end = (*groups)[grpnum].grp_end;
14116 31179 : grpnum++;
14117 : }
14118 51384 : decl = OMP_CLAUSE_DECL (c);
14119 :
14120 51384 : if (error_operand_p (decl))
14121 : {
14122 : remove = true;
14123 : break;
14124 : }
14125 :
14126 51384 : if (!omp_parse_expr (addr_tokens, decl))
14127 : {
14128 : remove = true;
14129 : break;
14130 : }
14131 :
14132 51384 : if (remove)
14133 : break;
14134 51384 : if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
14135 : {
14136 : struct gimplify_omp_ctx *octx;
14137 1167 : for (octx = outer_ctx; octx; octx = octx->outer_context)
14138 : {
14139 1167 : if (octx->region_type != ORT_ACC_HOST_DATA)
14140 : break;
14141 12 : splay_tree_node n2
14142 12 : = splay_tree_lookup (octx->variables,
14143 : (splay_tree_key) decl);
14144 12 : if (n2)
14145 4 : error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
14146 : "declared in enclosing %<host_data%> region",
14147 4 : DECL_NAME (decl));
14148 : }
14149 : }
14150 :
14151 51384 : map_descriptor = false;
14152 :
14153 : /* This condition checks if we're mapping an array descriptor that
14154 : isn't inside a derived type -- these have special handling, and
14155 : are not handled as structs in omp_build_struct_sibling_lists.
14156 : See that function for further details. */
14157 51384 : if (*grp_start_p != grp_end
14158 34232 : && OMP_CLAUSE_CHAIN (*grp_start_p)
14159 85616 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
14160 : {
14161 17096 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
14162 17096 : if (omp_map_clause_descriptor_p (grp_mid)
14163 32365 : && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
14164 : map_descriptor = true;
14165 : }
14166 34288 : else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
14167 34288 : && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
14168 33544 : || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
14169 35210 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
14170 : map_descriptor = true;
14171 :
14172 : /* Adding the decl for a struct access: we haven't created
14173 : GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
14174 : whether they will be created in gimplify_adjust_omp_clauses.
14175 : NOTE: Technically we should probably look through DECL_VALUE_EXPR
14176 : here because something that looks like a DECL_P may actually be a
14177 : struct access, e.g. variables in a lambda closure
14178 : (__closure->__foo) or class members (this->foo). Currently in both
14179 : those cases we map the whole of the containing object (directly in
14180 : the C++ FE) though, so struct nodes are not created. */
14181 51384 : if (c == grp_end
14182 31179 : && addr_tokens[0]->type == STRUCTURE_BASE
14183 8403 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14184 59787 : && !map_descriptor)
14185 : {
14186 5599 : gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
14187 : /* If we got to this struct via a chain of pointers, maybe we
14188 : want to map it implicitly instead. */
14189 5599 : if (omp_access_chain_p (addr_tokens, 1))
14190 : break;
14191 5438 : omp_mapping_group *wholestruct;
14192 5438 : if (!(region_type & ORT_ACC)
14193 9601 : && omp_mapped_by_containing_struct (grpmap,
14194 4163 : OMP_CLAUSE_DECL (c),
14195 : &wholestruct))
14196 : break;
14197 5201 : decl = addr_tokens[1]->expr;
14198 5201 : if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
14199 : break;
14200 : /* Standalone attach or detach clauses for a struct element
14201 : should not inhibit implicit mapping of the whole struct. */
14202 2987 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14203 2987 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14204 : break;
14205 2837 : flags = GOVD_MAP | GOVD_EXPLICIT;
14206 :
14207 2837 : gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
14208 : || TREE_ADDRESSABLE (decl));
14209 2837 : goto do_add_decl;
14210 : }
14211 :
14212 45785 : if (!DECL_P (decl))
14213 : {
14214 21142 : tree d = decl, *pd;
14215 21142 : if (TREE_CODE (d) == ARRAY_REF)
14216 : {
14217 5128 : while (TREE_CODE (d) == ARRAY_REF)
14218 2621 : d = TREE_OPERAND (d, 0);
14219 2507 : if (TREE_CODE (d) == COMPONENT_REF
14220 2507 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
14221 : decl = d;
14222 : }
14223 21142 : pd = &OMP_CLAUSE_DECL (c);
14224 21142 : if (d == decl
14225 18681 : && TREE_CODE (decl) == INDIRECT_REF
14226 13945 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
14227 1070 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
14228 : == REFERENCE_TYPE)
14229 21793 : && (OMP_CLAUSE_MAP_KIND (c)
14230 : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
14231 : {
14232 647 : pd = &TREE_OPERAND (decl, 0);
14233 647 : decl = TREE_OPERAND (decl, 0);
14234 : }
14235 :
14236 21142 : if (addr_tokens[0]->type == STRUCTURE_BASE
14237 11440 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14238 11439 : && addr_tokens[1]->type == ACCESS_METHOD
14239 11439 : && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
14240 10941 : || (addr_tokens[1]->u.access_kind
14241 : == ACCESS_POINTER_OFFSET))
14242 21722 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
14243 : {
14244 0 : tree base = addr_tokens[1]->expr;
14245 0 : splay_tree_node n
14246 0 : = splay_tree_lookup (ctx->variables,
14247 : (splay_tree_key) base);
14248 0 : n->value |= GOVD_SEEN;
14249 : }
14250 :
14251 21142 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
14252 : {
14253 : /* Don't gimplify *pd fully at this point, as the base
14254 : will need to be adjusted during omp lowering. */
14255 88 : auto_vec<tree, 10> expr_stack;
14256 88 : tree *p = pd;
14257 88 : while (handled_component_p (*p)
14258 : || TREE_CODE (*p) == INDIRECT_REF
14259 : || TREE_CODE (*p) == ADDR_EXPR
14260 : || TREE_CODE (*p) == MEM_REF
14261 224 : || TREE_CODE (*p) == NON_LVALUE_EXPR)
14262 : {
14263 136 : expr_stack.safe_push (*p);
14264 136 : p = &TREE_OPERAND (*p, 0);
14265 : }
14266 312 : for (int i = expr_stack.length () - 1; i >= 0; i--)
14267 : {
14268 136 : tree t = expr_stack[i];
14269 136 : if (TREE_CODE (t) == ARRAY_REF
14270 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14271 : {
14272 56 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14273 : {
14274 56 : tree low = unshare_expr (array_ref_low_bound (t));
14275 56 : if (!is_gimple_min_invariant (low))
14276 : {
14277 0 : TREE_OPERAND (t, 2) = low;
14278 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14279 : pre_p, NULL,
14280 : is_gimple_reg,
14281 : fb_rvalue) == GS_ERROR)
14282 0 : remove = true;
14283 : }
14284 : }
14285 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14286 : NULL, is_gimple_reg,
14287 : fb_rvalue) == GS_ERROR)
14288 0 : remove = true;
14289 56 : if (TREE_OPERAND (t, 3) == NULL_TREE)
14290 : {
14291 56 : tree elmt_size = array_ref_element_size (t);
14292 56 : if (!is_gimple_min_invariant (elmt_size))
14293 : {
14294 0 : elmt_size = unshare_expr (elmt_size);
14295 0 : tree elmt_type
14296 0 : = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
14297 : 0)));
14298 0 : tree factor
14299 0 : = size_int (TYPE_ALIGN_UNIT (elmt_type));
14300 0 : elmt_size
14301 0 : = size_binop (EXACT_DIV_EXPR, elmt_size,
14302 : factor);
14303 0 : TREE_OPERAND (t, 3) = elmt_size;
14304 0 : if (gimplify_expr (&TREE_OPERAND (t, 3),
14305 : pre_p, NULL,
14306 : is_gimple_reg,
14307 : fb_rvalue) == GS_ERROR)
14308 0 : remove = true;
14309 : }
14310 : }
14311 0 : else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
14312 : NULL, is_gimple_reg,
14313 : fb_rvalue) == GS_ERROR)
14314 0 : remove = true;
14315 : }
14316 80 : else if (TREE_CODE (t) == COMPONENT_REF)
14317 : {
14318 0 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14319 : {
14320 0 : tree offset = component_ref_field_offset (t);
14321 0 : if (!is_gimple_min_invariant (offset))
14322 : {
14323 0 : offset = unshare_expr (offset);
14324 0 : tree field = TREE_OPERAND (t, 1);
14325 0 : tree factor
14326 0 : = size_int (DECL_OFFSET_ALIGN (field)
14327 : / BITS_PER_UNIT);
14328 0 : offset = size_binop (EXACT_DIV_EXPR, offset,
14329 : factor);
14330 0 : TREE_OPERAND (t, 2) = offset;
14331 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14332 : pre_p, NULL,
14333 : is_gimple_reg,
14334 : fb_rvalue) == GS_ERROR)
14335 0 : remove = true;
14336 : }
14337 : }
14338 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14339 : NULL, is_gimple_reg,
14340 : fb_rvalue) == GS_ERROR)
14341 0 : remove = true;
14342 : }
14343 : }
14344 224 : for (; expr_stack.length () > 0; )
14345 : {
14346 136 : tree t = expr_stack.pop ();
14347 :
14348 136 : if (TREE_CODE (t) == ARRAY_REF
14349 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14350 : {
14351 56 : if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
14352 56 : && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
14353 : NULL, is_gimple_val,
14354 : fb_rvalue) == GS_ERROR)
14355 : remove = true;
14356 : }
14357 : }
14358 88 : }
14359 : break;
14360 : }
14361 :
14362 24643 : if ((code == OMP_TARGET
14363 : || code == OMP_TARGET_DATA
14364 : || code == OMP_TARGET_ENTER_DATA
14365 14229 : || code == OMP_TARGET_EXIT_DATA)
14366 25285 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
14367 : {
14368 : /* If we have attach/detach but the decl we have is a pointer to
14369 : pointer, we're probably mapping the "base level" array
14370 : implicitly. Make sure we don't add the decl as if we mapped
14371 : it explicitly. That is,
14372 :
14373 : int **arr;
14374 : [...]
14375 : #pragma omp target map(arr[a][b:c])
14376 :
14377 : should *not* map "arr" explicitly. That way we get a
14378 : zero-length "alloc" mapping for it, and assuming it's been
14379 : mapped by some previous directive, etc., things work as they
14380 : should. */
14381 :
14382 217 : tree basetype = TREE_TYPE (addr_tokens[0]->expr);
14383 :
14384 217 : if (TREE_CODE (basetype) == REFERENCE_TYPE)
14385 34 : basetype = TREE_TYPE (basetype);
14386 :
14387 217 : if (code == OMP_TARGET
14388 54 : && addr_tokens[0]->type == ARRAY_BASE
14389 54 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14390 54 : && TREE_CODE (basetype) == POINTER_TYPE
14391 271 : && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
14392 : break;
14393 : }
14394 :
14395 24621 : flags = GOVD_MAP | GOVD_EXPLICIT;
14396 24621 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
14397 24484 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
14398 23976 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
14399 48567 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
14400 : flags |= GOVD_MAP_ALWAYS_TO;
14401 :
14402 24621 : goto do_add;
14403 :
14404 510 : case OMP_CLAUSE_AFFINITY:
14405 510 : gimplify_omp_affinity (list_p, pre_p);
14406 510 : remove = true;
14407 510 : break;
14408 8 : case OMP_CLAUSE_DOACROSS:
14409 8 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
14410 : {
14411 4 : tree deps = OMP_CLAUSE_DECL (c);
14412 8 : while (deps && TREE_CODE (deps) == TREE_LIST)
14413 : {
14414 4 : if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
14415 4 : && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
14416 0 : gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
14417 : pre_p, NULL, is_gimple_val, fb_rvalue);
14418 4 : deps = TREE_CHAIN (deps);
14419 : }
14420 : }
14421 : else
14422 4 : gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
14423 : == OMP_CLAUSE_DOACROSS_SOURCE);
14424 : break;
14425 2207 : case OMP_CLAUSE_DEPEND:
14426 2207 : if (handled_depend_iterators == -1)
14427 1905 : handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
14428 2207 : if (handled_depend_iterators)
14429 : {
14430 348 : if (handled_depend_iterators == 2)
14431 0 : remove = true;
14432 : break;
14433 : }
14434 1859 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
14435 : {
14436 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
14437 : NULL, is_gimple_val, fb_rvalue);
14438 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
14439 : }
14440 1859 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
14441 : {
14442 : remove = true;
14443 : break;
14444 : }
14445 1859 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
14446 : {
14447 1828 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
14448 1828 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
14449 : is_gimple_val, fb_rvalue) == GS_ERROR)
14450 : {
14451 : remove = true;
14452 : break;
14453 : }
14454 : }
14455 1859 : if (code == OMP_TASK)
14456 1401 : ctx->has_depend = true;
14457 : break;
14458 :
14459 8371 : case OMP_CLAUSE_TO:
14460 8371 : case OMP_CLAUSE_FROM:
14461 8371 : case OMP_CLAUSE__CACHE_:
14462 8371 : decl = OMP_CLAUSE_DECL (c);
14463 8371 : if (error_operand_p (decl))
14464 : {
14465 : remove = true;
14466 : break;
14467 : }
14468 8371 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
14469 7682 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
14470 800 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
14471 8371 : gimple_seq *seq_p;
14472 8371 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
14473 8371 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
14474 : is_gimple_val, fb_rvalue) == GS_ERROR)
14475 : {
14476 0 : remove = true;
14477 0 : exit_omp_iterator_loop_context (c);
14478 0 : break;
14479 : }
14480 8371 : if (!DECL_P (decl))
14481 : {
14482 2168 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
14483 : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
14484 0 : remove = true;
14485 2168 : exit_omp_iterator_loop_context (c);
14486 2168 : break;
14487 : }
14488 6203 : exit_omp_iterator_loop_context (c);
14489 6203 : goto do_notice;
14490 :
14491 135 : case OMP_CLAUSE__MAPPER_BINDING_:
14492 135 : {
14493 135 : tree name = OMP_CLAUSE__MAPPER_BINDING__ID (c);
14494 135 : tree var = OMP_CLAUSE__MAPPER_BINDING__DECL (c);
14495 135 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (var));
14496 135 : tree fndecl = OMP_CLAUSE__MAPPER_BINDING__MAPPER (c);
14497 135 : ctx->implicit_mappers->put ({ name, type }, fndecl);
14498 135 : remove = true;
14499 135 : break;
14500 : }
14501 :
14502 2118 : case OMP_CLAUSE_USE_DEVICE_PTR:
14503 2118 : case OMP_CLAUSE_USE_DEVICE_ADDR:
14504 2118 : flags = GOVD_EXPLICIT;
14505 2118 : goto do_add;
14506 :
14507 558 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
14508 558 : decl = OMP_CLAUSE_DECL (c);
14509 558 : while (TREE_CODE (decl) == INDIRECT_REF
14510 607 : || TREE_CODE (decl) == ARRAY_REF)
14511 49 : decl = TREE_OPERAND (decl, 0);
14512 558 : flags = GOVD_EXPLICIT;
14513 558 : goto do_add_decl;
14514 :
14515 500 : case OMP_CLAUSE_IS_DEVICE_PTR:
14516 500 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
14517 500 : goto do_add;
14518 :
14519 63261 : do_add:
14520 63261 : decl = OMP_CLAUSE_DECL (c);
14521 84645 : do_add_decl:
14522 84645 : if (error_operand_p (decl))
14523 : {
14524 : remove = true;
14525 : break;
14526 : }
14527 84637 : if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
14528 : {
14529 1755 : tree t = omp_member_access_dummy_var (decl);
14530 1755 : if (t)
14531 : {
14532 668 : tree v = DECL_VALUE_EXPR (decl);
14533 668 : DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
14534 668 : if (outer_ctx)
14535 140 : omp_notice_variable (outer_ctx, t, true);
14536 : }
14537 : }
14538 84637 : if (code == OACC_DATA
14539 2402 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14540 87039 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14541 307 : flags |= GOVD_MAP_0LEN_ARRAY;
14542 84637 : omp_add_variable (ctx, decl, flags);
14543 84637 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14544 69252 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14545 67178 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14546 87241 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14547 : {
14548 2106 : struct gimplify_omp_ctx *pctx
14549 2166 : = code == OMP_TARGET ? outer_ctx : ctx;
14550 2166 : if (pctx)
14551 2136 : omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
14552 : GOVD_LOCAL | GOVD_SEEN);
14553 2136 : if (pctx
14554 2136 : && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
14555 632 : && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14556 : find_decl_expr,
14557 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14558 : NULL) == NULL_TREE)
14559 208 : omp_add_variable (pctx,
14560 208 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14561 : GOVD_LOCAL | GOVD_SEEN);
14562 2166 : gimplify_omp_ctxp = pctx;
14563 2166 : push_gimplify_context ();
14564 :
14565 2166 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
14566 2166 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
14567 :
14568 2166 : gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
14569 2166 : &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
14570 2166 : pop_gimplify_context
14571 2166 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
14572 2166 : push_gimplify_context ();
14573 4332 : gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
14574 2166 : &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
14575 2166 : pop_gimplify_context
14576 2166 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
14577 2166 : OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
14578 2166 : OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
14579 :
14580 2166 : gimplify_omp_ctxp = outer_ctx;
14581 : }
14582 82471 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14583 82471 : && OMP_CLAUSE_LASTPRIVATE_STMT (c))
14584 : {
14585 303 : gimplify_omp_ctxp = ctx;
14586 303 : push_gimplify_context ();
14587 303 : if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
14588 : {
14589 303 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14590 : NULL, NULL);
14591 303 : TREE_SIDE_EFFECTS (bind) = 1;
14592 303 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
14593 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
14594 : }
14595 606 : gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
14596 303 : &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
14597 303 : pop_gimplify_context
14598 303 : (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
14599 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
14600 :
14601 303 : gimplify_omp_ctxp = outer_ctx;
14602 : }
14603 82168 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14604 82168 : && OMP_CLAUSE_LINEAR_STMT (c))
14605 : {
14606 60 : gimplify_omp_ctxp = ctx;
14607 60 : push_gimplify_context ();
14608 60 : if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
14609 : {
14610 60 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14611 : NULL, NULL);
14612 60 : TREE_SIDE_EFFECTS (bind) = 1;
14613 60 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
14614 60 : OMP_CLAUSE_LINEAR_STMT (c) = bind;
14615 : }
14616 120 : gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
14617 60 : &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
14618 60 : pop_gimplify_context
14619 60 : (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
14620 60 : OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
14621 :
14622 60 : gimplify_omp_ctxp = outer_ctx;
14623 : }
14624 84637 : if (notice_outer)
14625 72495 : goto do_notice;
14626 : break;
14627 :
14628 910 : case OMP_CLAUSE_COPYIN:
14629 910 : case OMP_CLAUSE_COPYPRIVATE:
14630 910 : decl = OMP_CLAUSE_DECL (c);
14631 910 : if (error_operand_p (decl))
14632 : {
14633 : remove = true;
14634 : break;
14635 : }
14636 910 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
14637 : && !remove
14638 910 : && !omp_check_private (ctx, decl, true))
14639 : {
14640 35 : remove = true;
14641 35 : if (is_global_var (decl))
14642 : {
14643 30 : if (DECL_THREAD_LOCAL_P (decl))
14644 : remove = false;
14645 10 : else if (DECL_HAS_VALUE_EXPR_P (decl))
14646 : {
14647 2 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
14648 :
14649 2 : if (value
14650 2 : && DECL_P (value)
14651 4 : && DECL_THREAD_LOCAL_P (value))
14652 : remove = false;
14653 : }
14654 : }
14655 : if (remove)
14656 13 : error_at (OMP_CLAUSE_LOCATION (c),
14657 : "copyprivate variable %qE is not threadprivate"
14658 13 : " or private in outer context", DECL_NAME (decl));
14659 : }
14660 79608 : do_notice:
14661 79608 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14662 64223 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14663 56170 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14664 30776 : && outer_ctx
14665 17630 : && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
14666 16291 : || (region_type == ORT_WORKSHARE
14667 3909 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14668 1111 : && (OMP_CLAUSE_REDUCTION_INSCAN (c)
14669 928 : || code == OMP_LOOP)))
14670 81561 : && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
14671 857 : || (code == OMP_LOOP
14672 138 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14673 138 : && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
14674 : == ORT_COMBINED_TEAMS))))
14675 : {
14676 1217 : splay_tree_node on
14677 1217 : = splay_tree_lookup (outer_ctx->variables,
14678 : (splay_tree_key)decl);
14679 1217 : if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
14680 : {
14681 899 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14682 713 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14683 1011 : && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
14684 56 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14685 0 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
14686 : == POINTER_TYPE))))
14687 56 : omp_firstprivatize_variable (outer_ctx, decl);
14688 : else
14689 : {
14690 843 : omp_add_variable (outer_ctx, decl,
14691 : GOVD_SEEN | GOVD_SHARED);
14692 843 : if (outer_ctx->outer_context)
14693 235 : omp_notice_variable (outer_ctx->outer_context, decl,
14694 : true);
14695 : }
14696 : }
14697 : }
14698 79000 : if (outer_ctx)
14699 29671 : omp_notice_variable (outer_ctx, decl, true);
14700 79608 : if (check_non_private
14701 26980 : && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
14702 4804 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14703 1689 : || decl == OMP_CLAUSE_DECL (c)
14704 224 : || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14705 224 : && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14706 : == ADDR_EXPR
14707 103 : || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14708 : == POINTER_PLUS_EXPR
14709 22 : && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
14710 : (OMP_CLAUSE_DECL (c), 0), 0))
14711 : == ADDR_EXPR)))))
14712 84324 : && omp_check_private (ctx, decl, false))
14713 : {
14714 58 : error ("%s variable %qE is private in outer context",
14715 29 : check_non_private, DECL_NAME (decl));
14716 29 : remove = true;
14717 : }
14718 : break;
14719 :
14720 190 : case OMP_CLAUSE_DETACH:
14721 190 : flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
14722 190 : goto do_add;
14723 :
14724 4114 : case OMP_CLAUSE_IF:
14725 4114 : if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
14726 4114 : && OMP_CLAUSE_IF_MODIFIER (c) != code)
14727 : {
14728 : const char *p[2];
14729 168 : for (int i = 0; i < 2; i++)
14730 112 : switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
14731 : {
14732 8 : case VOID_CST: p[i] = "cancel"; break;
14733 24 : case OMP_PARALLEL: p[i] = "parallel"; break;
14734 4 : case OMP_SIMD: p[i] = "simd"; break;
14735 12 : case OMP_TASK: p[i] = "task"; break;
14736 12 : case OMP_TASKLOOP: p[i] = "taskloop"; break;
14737 8 : case OMP_TARGET_DATA: p[i] = "target data"; break;
14738 12 : case OMP_TARGET: p[i] = "target"; break;
14739 12 : case OMP_TARGET_UPDATE: p[i] = "target update"; break;
14740 8 : case OMP_TARGET_ENTER_DATA:
14741 8 : p[i] = "target enter data"; break;
14742 12 : case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
14743 0 : default: gcc_unreachable ();
14744 : }
14745 56 : error_at (OMP_CLAUSE_LOCATION (c),
14746 : "expected %qs %<if%> clause modifier rather than %qs",
14747 : p[0], p[1]);
14748 56 : remove = true;
14749 : }
14750 : /* Fall through. */
14751 :
14752 4824 : case OMP_CLAUSE_SELF:
14753 4824 : case OMP_CLAUSE_FINAL:
14754 4824 : OMP_CLAUSE_OPERAND (c, 0)
14755 9648 : = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
14756 : /* Fall through. */
14757 :
14758 5753 : case OMP_CLAUSE_NUM_TEAMS:
14759 5753 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
14760 929 : && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14761 5977 : && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14762 : {
14763 194 : if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14764 : {
14765 : remove = true;
14766 : break;
14767 : }
14768 194 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14769 388 : = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
14770 : pre_p, NULL, true);
14771 : }
14772 : /* Fall through. */
14773 :
14774 21958 : case OMP_CLAUSE_SCHEDULE:
14775 21958 : case OMP_CLAUSE_NUM_THREADS:
14776 21958 : case OMP_CLAUSE_THREAD_LIMIT:
14777 21958 : case OMP_CLAUSE_DIST_SCHEDULE:
14778 21958 : case OMP_CLAUSE_DEVICE:
14779 21958 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14780 21958 : && OMP_CLAUSE_DEVICE_ANCESTOR (c))
14781 : {
14782 125 : if (code != OMP_TARGET)
14783 : {
14784 20 : error_at (OMP_CLAUSE_LOCATION (c),
14785 : "%<device%> clause with %<ancestor%> is only "
14786 : "allowed on %<target%> construct");
14787 20 : remove = true;
14788 20 : break;
14789 : }
14790 :
14791 105 : tree clauses = *orig_list_p;
14792 330 : for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
14793 236 : if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
14794 : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
14795 : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
14796 : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
14797 : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
14798 : )
14799 : {
14800 11 : error_at (OMP_CLAUSE_LOCATION (c),
14801 : "with %<ancestor%>, only the %<device%>, "
14802 : "%<firstprivate%>, %<private%>, %<defaultmap%>, "
14803 : "and %<map%> clauses may appear on the "
14804 : "construct");
14805 11 : remove = true;
14806 11 : break;
14807 : }
14808 : }
14809 21833 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14810 21833 : && code == OMP_DISPATCH)
14811 : {
14812 272 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
14813 272 : gimplify_ctxp->into_ssa = false;
14814 272 : if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
14815 : is_gimple_val, fb_rvalue)
14816 : == GS_ERROR)
14817 : remove = true;
14818 272 : else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
14819 55 : omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
14820 : GOVD_SHARED | GOVD_SEEN);
14821 272 : gimplify_ctxp->into_ssa = saved_into_ssa;
14822 272 : break;
14823 : }
14824 : /* Fall through. */
14825 :
14826 31407 : case OMP_CLAUSE_PRIORITY:
14827 31407 : case OMP_CLAUSE_GRAINSIZE:
14828 31407 : case OMP_CLAUSE_NUM_TASKS:
14829 31407 : case OMP_CLAUSE_FILTER:
14830 31407 : case OMP_CLAUSE_HINT:
14831 31407 : case OMP_CLAUSE_ASYNC:
14832 31407 : case OMP_CLAUSE_WAIT:
14833 31407 : case OMP_CLAUSE_NUM_GANGS:
14834 31407 : case OMP_CLAUSE_NUM_WORKERS:
14835 31407 : case OMP_CLAUSE_VECTOR_LENGTH:
14836 31407 : case OMP_CLAUSE_WORKER:
14837 31407 : case OMP_CLAUSE_VECTOR:
14838 31407 : if (OMP_CLAUSE_OPERAND (c, 0)
14839 31407 : && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) == TREE_LIST)
14840 : {
14841 2604 : for (tree t = OMP_CLAUSE_OPERAND (c, 0); t; t = TREE_CHAIN (t))
14842 1346 : if (!is_gimple_min_invariant (TREE_VALUE (t)))
14843 : {
14844 280 : if (error_operand_p (TREE_VALUE (t)))
14845 : {
14846 : remove = true;
14847 : break;
14848 : }
14849 560 : TREE_VALUE (t)
14850 280 : = get_initialized_tmp_var (TREE_VALUE (t), pre_p,
14851 : NULL, true);
14852 : }
14853 : }
14854 30149 : else if (OMP_CLAUSE_OPERAND (c, 0)
14855 30149 : && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
14856 : {
14857 7520 : if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
14858 : {
14859 : remove = true;
14860 : break;
14861 : }
14862 : /* All these clauses care about value, not a particular decl,
14863 : so try to force it into a SSA_NAME or fresh temporary. */
14864 7513 : OMP_CLAUSE_OPERAND (c, 0)
14865 15026 : = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
14866 : pre_p, NULL, true);
14867 : }
14868 : break;
14869 :
14870 2332 : case OMP_CLAUSE_GANG:
14871 2332 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
14872 : is_gimple_val, fb_rvalue) == GS_ERROR)
14873 0 : remove = true;
14874 2332 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
14875 : is_gimple_val, fb_rvalue) == GS_ERROR)
14876 0 : remove = true;
14877 : break;
14878 :
14879 13141 : case OMP_CLAUSE_NOWAIT:
14880 13141 : nowait = 1;
14881 13141 : break;
14882 :
14883 116 : case OMP_CLAUSE_USES_ALLOCATORS:
14884 116 : if (TREE_CODE (OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c))
14885 : != INTEGER_CST)
14886 : {
14887 112 : decl = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
14888 112 : omp_add_variable (ctx, decl,
14889 : GOVD_SEEN | GOVD_USES_ALLOCATORS_ALLOCATOR);
14890 : }
14891 : else
14892 : remove = true;
14893 : break;
14894 :
14895 : case OMP_CLAUSE_ORDERED:
14896 : case OMP_CLAUSE_UNTIED:
14897 : case OMP_CLAUSE_COLLAPSE:
14898 : case OMP_CLAUSE_TILE:
14899 : case OMP_CLAUSE_AUTO:
14900 : case OMP_CLAUSE_SEQ:
14901 : case OMP_CLAUSE_INDEPENDENT:
14902 : case OMP_CLAUSE_MERGEABLE:
14903 : case OMP_CLAUSE_MESSAGE:
14904 : case OMP_CLAUSE_PROC_BIND:
14905 : case OMP_CLAUSE_SAFELEN:
14906 : case OMP_CLAUSE_SIMDLEN:
14907 : case OMP_CLAUSE_NOGROUP:
14908 : case OMP_CLAUSE_THREADS:
14909 : case OMP_CLAUSE_SIMD:
14910 : case OMP_CLAUSE_BIND:
14911 : case OMP_CLAUSE_IF_PRESENT:
14912 : case OMP_CLAUSE_FINALIZE:
14913 : case OMP_CLAUSE_INTEROP:
14914 : case OMP_CLAUSE_INIT:
14915 : case OMP_CLAUSE_USE:
14916 : case OMP_CLAUSE_DESTROY:
14917 : case OMP_CLAUSE_DEVICE_TYPE:
14918 : break;
14919 :
14920 52 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
14921 52 : remove = true;
14922 52 : sorry_at (OMP_CLAUSE_LOCATION (c),"%<dyn_groupprivate%> clause");
14923 52 : break;
14924 :
14925 3976 : case OMP_CLAUSE_ORDER:
14926 3976 : ctx->order_concurrent = true;
14927 3976 : break;
14928 :
14929 1017 : case OMP_CLAUSE_DEFAULTMAP:
14930 1017 : enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
14931 1017 : switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
14932 : {
14933 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
14934 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
14935 : gdmkmin = GDMK_SCALAR;
14936 : gdmkmax = GDMK_POINTER;
14937 : break;
14938 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14939 : gdmkmin = GDMK_SCALAR;
14940 : gdmkmax = GDMK_SCALAR_TARGET;
14941 : break;
14942 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14943 : gdmkmin = gdmkmax = GDMK_AGGREGATE;
14944 : break;
14945 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
14946 : gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
14947 : break;
14948 : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14949 : gdmkmin = gdmkmax = GDMK_POINTER;
14950 : break;
14951 0 : default:
14952 0 : gcc_unreachable ();
14953 : }
14954 4513 : for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
14955 3496 : switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
14956 : {
14957 91 : case OMP_CLAUSE_DEFAULTMAP_ALLOC:
14958 91 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
14959 91 : break;
14960 96 : case OMP_CLAUSE_DEFAULTMAP_TO:
14961 96 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
14962 96 : break;
14963 28 : case OMP_CLAUSE_DEFAULTMAP_FROM:
14964 28 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
14965 28 : break;
14966 671 : case OMP_CLAUSE_DEFAULTMAP_TOFROM:
14967 671 : ctx->defaultmap[gdmk] = GOVD_MAP;
14968 671 : break;
14969 380 : case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
14970 380 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14971 380 : break;
14972 2129 : case OMP_CLAUSE_DEFAULTMAP_NONE:
14973 2129 : ctx->defaultmap[gdmk] = 0;
14974 2129 : break;
14975 45 : case OMP_CLAUSE_DEFAULTMAP_PRESENT:
14976 45 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
14977 45 : break;
14978 56 : case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
14979 56 : switch (gdmk)
14980 : {
14981 11 : case GDMK_SCALAR:
14982 11 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14983 11 : break;
14984 11 : case GDMK_SCALAR_TARGET:
14985 11 : ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
14986 11 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
14987 11 : break;
14988 14 : case GDMK_AGGREGATE:
14989 14 : case GDMK_ALLOCATABLE:
14990 14 : ctx->defaultmap[gdmk] = GOVD_MAP;
14991 14 : break;
14992 20 : case GDMK_POINTER:
14993 20 : ctx->defaultmap[gdmk] = GOVD_MAP;
14994 20 : if (!lang_GNU_Fortran ())
14995 12 : ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
14996 : break;
14997 : default:
14998 : gcc_unreachable ();
14999 : }
15000 : break;
15001 0 : default:
15002 0 : gcc_unreachable ();
15003 : }
15004 : break;
15005 :
15006 824 : case OMP_CLAUSE_ALIGNED:
15007 824 : decl = OMP_CLAUSE_DECL (c);
15008 824 : if (error_operand_p (decl))
15009 : {
15010 : remove = true;
15011 : break;
15012 : }
15013 824 : if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
15014 : is_gimple_val, fb_rvalue) == GS_ERROR)
15015 : {
15016 : remove = true;
15017 : break;
15018 : }
15019 824 : if (!is_global_var (decl)
15020 824 : && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15021 592 : omp_add_variable (ctx, decl, GOVD_ALIGNED);
15022 : break;
15023 :
15024 489 : case OMP_CLAUSE_NONTEMPORAL:
15025 489 : decl = OMP_CLAUSE_DECL (c);
15026 489 : if (error_operand_p (decl))
15027 : {
15028 : remove = true;
15029 : break;
15030 : }
15031 489 : omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
15032 489 : break;
15033 :
15034 3691 : case OMP_CLAUSE_ALLOCATE:
15035 3691 : decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
15036 3691 : if (decl
15037 1722 : && TREE_CODE (decl) == INTEGER_CST
15038 3720 : && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREAD)
15039 3720 : && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
15040 35 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
15041 : "allocator with access trait set to %<thread%> "
15042 : "results in undefined behavior for %qs directive",
15043 : code == OMP_TARGET ? "target"
15044 : : (code == OMP_TASK
15045 15 : ? "task" : "taskloop"));
15046 3691 : decl = OMP_CLAUSE_DECL (c);
15047 3691 : if (error_operand_p (decl))
15048 : {
15049 : remove = true;
15050 : break;
15051 : }
15052 3691 : if ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS) == 0
15053 3679 : && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
15054 5413 : && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST)
15055 : {
15056 521 : tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
15057 521 : tree clauses = NULL_TREE;
15058 :
15059 : /* Get clause list of the nearest enclosing target construct. */
15060 521 : if (ctx->code == OMP_TARGET)
15061 14 : clauses = *orig_list_p;
15062 : else
15063 : {
15064 507 : struct gimplify_omp_ctx *tctx = ctx->outer_context;
15065 826 : while (tctx && tctx->code != OMP_TARGET)
15066 319 : tctx = tctx->outer_context;
15067 507 : if (tctx)
15068 18 : clauses = tctx->clauses;
15069 : }
15070 :
15071 32 : if (clauses)
15072 : {
15073 30 : tree uc;
15074 30 : if (TREE_CODE (allocator) == MEM_REF
15075 30 : || TREE_CODE (allocator) == INDIRECT_REF)
15076 0 : allocator = TREE_OPERAND (allocator, 0);
15077 132 : for (uc = clauses; uc; uc = OMP_CLAUSE_CHAIN (uc))
15078 123 : if (OMP_CLAUSE_CODE (uc) == OMP_CLAUSE_USES_ALLOCATORS)
15079 : {
15080 29 : tree uc_allocator
15081 29 : = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (uc);
15082 29 : if (operand_equal_p (allocator, uc_allocator))
15083 : break;
15084 : }
15085 30 : if (uc == NULL_TREE)
15086 : {
15087 9 : error_at (OMP_CLAUSE_LOCATION (c), "allocator %qE "
15088 : "requires %<uses_allocators(%E)%> clause in "
15089 : "target region", allocator, allocator);
15090 9 : remove = true;
15091 9 : break;
15092 : }
15093 : }
15094 : }
15095 3682 : if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
15096 : is_gimple_val, fb_rvalue) == GS_ERROR)
15097 : {
15098 : remove = true;
15099 : break;
15100 : }
15101 3682 : else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
15102 3682 : || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
15103 : == INTEGER_CST))
15104 : ;
15105 512 : else if (code == OMP_TASKLOOP
15106 512 : || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
15107 66 : OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
15108 132 : = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
15109 : pre_p, NULL, false);
15110 : break;
15111 :
15112 4365 : case OMP_CLAUSE_DEFAULT:
15113 4365 : ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
15114 4365 : break;
15115 :
15116 652 : case OMP_CLAUSE_INCLUSIVE:
15117 652 : case OMP_CLAUSE_EXCLUSIVE:
15118 652 : decl = OMP_CLAUSE_DECL (c);
15119 652 : {
15120 652 : splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
15121 : (splay_tree_key) decl);
15122 652 : if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
15123 : {
15124 5 : error_at (OMP_CLAUSE_LOCATION (c),
15125 : "%qD specified in %qs clause but not in %<inscan%> "
15126 : "%<reduction%> clause on the containing construct",
15127 5 : decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15128 5 : remove = true;
15129 : }
15130 : else
15131 : {
15132 647 : n->value |= GOVD_REDUCTION_INSCAN;
15133 647 : if (outer_ctx->region_type == ORT_SIMD
15134 528 : && outer_ctx->outer_context
15135 115 : && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
15136 : {
15137 115 : n = splay_tree_lookup (outer_ctx->outer_context->variables,
15138 : (splay_tree_key) decl);
15139 115 : if (n && (n->value & GOVD_REDUCTION) != 0)
15140 115 : n->value |= GOVD_REDUCTION_INSCAN;
15141 : }
15142 : }
15143 : }
15144 : break;
15145 :
15146 103 : case OMP_CLAUSE_NOVARIANTS:
15147 103 : OMP_CLAUSE_NOVARIANTS_EXPR (c)
15148 103 : = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
15149 103 : break;
15150 115 : case OMP_CLAUSE_NOCONTEXT:
15151 115 : OMP_CLAUSE_NOCONTEXT_EXPR (c)
15152 115 : = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
15153 115 : break;
15154 0 : case OMP_CLAUSE_NOHOST:
15155 0 : default:
15156 0 : gcc_unreachable ();
15157 : }
15158 :
15159 206438 : if (code == OACC_DATA
15160 5123 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15161 211249 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15162 4504 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15163 : remove = true;
15164 206131 : if (remove)
15165 1182 : *list_p = OMP_CLAUSE_CHAIN (c);
15166 : else
15167 205256 : list_p = &OMP_CLAUSE_CHAIN (c);
15168 206438 : }
15169 :
15170 131480 : if (groups)
15171 : {
15172 36562 : delete grpmap;
15173 18281 : delete groups;
15174 : }
15175 :
15176 131480 : ctx->clauses = *orig_list_p;
15177 131480 : gimplify_omp_ctxp = ctx;
15178 131480 : }
15179 :
15180 : /* Return true if DECL is a candidate for shared to firstprivate
15181 : optimization. We only consider non-addressable scalars, not
15182 : too big, and not references. */
15183 :
15184 : static bool
15185 406227 : omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
15186 : {
15187 406227 : if (TREE_ADDRESSABLE (decl))
15188 : return false;
15189 357051 : tree type = TREE_TYPE (decl);
15190 357051 : if (!is_gimple_reg_type (type)
15191 332232 : || TREE_CODE (type) == REFERENCE_TYPE
15192 684961 : || TREE_ADDRESSABLE (type))
15193 : return false;
15194 : /* Don't optimize too large decls, as each thread/task will have
15195 : its own. */
15196 327910 : HOST_WIDE_INT len = int_size_in_bytes (type);
15197 327910 : if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
15198 : return false;
15199 327893 : if (omp_privatize_by_reference (decl))
15200 1324 : return false;
15201 : return true;
15202 : }
15203 :
15204 : /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
15205 : For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
15206 : GOVD_WRITTEN in outer contexts. */
15207 :
15208 : static void
15209 304959 : omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
15210 : {
15211 467301 : for (; ctx; ctx = ctx->outer_context)
15212 : {
15213 450221 : splay_tree_node n = splay_tree_lookup (ctx->variables,
15214 : (splay_tree_key) decl);
15215 450221 : if (n == NULL)
15216 158761 : continue;
15217 291460 : else if (n->value & GOVD_SHARED)
15218 : {
15219 9168 : n->value |= GOVD_WRITTEN;
15220 9168 : return;
15221 : }
15222 282292 : else if (n->value & GOVD_DATA_SHARE_CLASS)
15223 : return;
15224 : }
15225 : }
15226 :
15227 : /* Helper callback for walk_gimple_seq to discover possible stores
15228 : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15229 : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15230 : for those. */
15231 :
15232 : static tree
15233 1131070 : omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
15234 : {
15235 1131070 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
15236 :
15237 1131070 : *walk_subtrees = 0;
15238 1131070 : if (!wi->is_lhs)
15239 : return NULL_TREE;
15240 :
15241 322805 : tree op = *tp;
15242 401745 : do
15243 : {
15244 401745 : if (handled_component_p (op))
15245 78940 : op = TREE_OPERAND (op, 0);
15246 322805 : else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
15247 322805 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
15248 0 : op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
15249 : else
15250 : break;
15251 : }
15252 : while (1);
15253 322805 : if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
15254 : return NULL_TREE;
15255 :
15256 264527 : omp_mark_stores (gimplify_omp_ctxp, op);
15257 264527 : return NULL_TREE;
15258 : }
15259 :
15260 : /* Helper callback for walk_gimple_seq to discover possible stores
15261 : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15262 : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15263 : for those. */
15264 :
15265 : static tree
15266 629100 : omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
15267 : bool *handled_ops_p,
15268 : struct walk_stmt_info *wi)
15269 : {
15270 629100 : gimple *stmt = gsi_stmt (*gsi_p);
15271 629100 : switch (gimple_code (stmt))
15272 : {
15273 : /* Don't recurse on OpenMP constructs for which
15274 : gimplify_adjust_omp_clauses already handled the bodies,
15275 : except handle gimple_omp_for_pre_body. */
15276 26821 : case GIMPLE_OMP_FOR:
15277 26821 : *handled_ops_p = true;
15278 26821 : if (gimple_omp_for_pre_body (stmt))
15279 1782 : walk_gimple_seq (gimple_omp_for_pre_body (stmt),
15280 : omp_find_stores_stmt, omp_find_stores_op, wi);
15281 : break;
15282 8625 : case GIMPLE_OMP_PARALLEL:
15283 8625 : case GIMPLE_OMP_TASK:
15284 8625 : case GIMPLE_OMP_SECTIONS:
15285 8625 : case GIMPLE_OMP_SINGLE:
15286 8625 : case GIMPLE_OMP_SCOPE:
15287 8625 : case GIMPLE_OMP_TARGET:
15288 8625 : case GIMPLE_OMP_TEAMS:
15289 8625 : case GIMPLE_OMP_CRITICAL:
15290 8625 : *handled_ops_p = true;
15291 8625 : break;
15292 : default:
15293 : break;
15294 : }
15295 629100 : return NULL_TREE;
15296 : }
15297 :
15298 : struct gimplify_adjust_omp_clauses_data
15299 : {
15300 : tree *list_p;
15301 : gimple_seq *pre_p;
15302 : };
15303 :
15304 : /* For all variables that were not actually used within the context,
15305 : remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
15306 :
15307 : static int
15308 678707 : gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
15309 : {
15310 678707 : tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
15311 678707 : gimple_seq *pre_p
15312 : = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
15313 678707 : tree decl = (tree) n->key;
15314 678707 : unsigned flags = n->value;
15315 678707 : enum omp_clause_code code;
15316 678707 : tree clause;
15317 678707 : bool private_debug;
15318 :
15319 678707 : if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
15320 132984 : && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
15321 : flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
15322 678582 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
15323 : return 0;
15324 179169 : if ((flags & GOVD_SEEN) == 0)
15325 : return 0;
15326 158072 : if (flags & GOVD_DEBUG_PRIVATE)
15327 : {
15328 260 : gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
15329 : private_debug = true;
15330 : }
15331 157812 : else if (flags & GOVD_MAP)
15332 : private_debug = false;
15333 : else
15334 139613 : private_debug
15335 139613 : = lang_hooks.decls.omp_private_debug_clause (decl,
15336 139613 : !!(flags & GOVD_SHARED));
15337 139613 : if (private_debug)
15338 : code = OMP_CLAUSE_PRIVATE;
15339 157690 : else if (flags & GOVD_MAP)
15340 : {
15341 18199 : code = OMP_CLAUSE_MAP;
15342 18199 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15343 18199 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15344 : {
15345 2 : error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
15346 2 : return 0;
15347 : }
15348 18197 : if (VAR_P (decl)
15349 16255 : && DECL_IN_CONSTANT_POOL (decl)
15350 18198 : && !lookup_attribute ("omp declare target",
15351 1 : DECL_ATTRIBUTES (decl)))
15352 : {
15353 1 : tree id = get_identifier ("omp declare target");
15354 1 : DECL_ATTRIBUTES (decl)
15355 1 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
15356 1 : varpool_node *node = varpool_node::get (decl);
15357 1 : if (node)
15358 : {
15359 1 : node->offloadable = 1;
15360 1 : if (ENABLE_OFFLOADING)
15361 : g->have_offload = true;
15362 : }
15363 : }
15364 : }
15365 139491 : else if (flags & GOVD_SHARED)
15366 : {
15367 49208 : if (is_global_var (decl))
15368 : {
15369 16142 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15370 25329 : while (ctx != NULL)
15371 : {
15372 17604 : splay_tree_node on
15373 17604 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15374 17604 : if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
15375 : | GOVD_PRIVATE | GOVD_REDUCTION
15376 : | GOVD_LINEAR | GOVD_MAP)) != 0)
15377 : break;
15378 9187 : ctx = ctx->outer_context;
15379 : }
15380 16142 : if (ctx == NULL)
15381 : return 0;
15382 : }
15383 41483 : code = OMP_CLAUSE_SHARED;
15384 : /* Don't optimize shared into firstprivate for read-only vars
15385 : on tasks with depend clause, we shouldn't try to copy them
15386 : until the dependencies are satisfied. */
15387 41483 : if (gimplify_omp_ctxp->has_depend)
15388 350 : flags |= GOVD_WRITTEN;
15389 : }
15390 90283 : else if (flags & GOVD_PRIVATE)
15391 : code = OMP_CLAUSE_PRIVATE;
15392 31653 : else if (flags & GOVD_FIRSTPRIVATE)
15393 : {
15394 22135 : code = OMP_CLAUSE_FIRSTPRIVATE;
15395 22135 : if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
15396 14040 : && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15397 32494 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15398 : {
15399 1 : error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
15400 : "%<target%> construct", decl);
15401 1 : return 0;
15402 : }
15403 : }
15404 9518 : else if (flags & GOVD_LASTPRIVATE)
15405 : code = OMP_CLAUSE_LASTPRIVATE;
15406 353 : else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
15407 : return 0;
15408 230 : else if (flags & GOVD_CONDTEMP)
15409 : {
15410 118 : code = OMP_CLAUSE__CONDTEMP_;
15411 118 : gimple_add_tmp_var (decl);
15412 : }
15413 112 : else if (flags & GOVD_USES_ALLOCATORS_ALLOCATOR)
15414 : return 0;
15415 : else
15416 0 : gcc_unreachable ();
15417 :
15418 140944 : if (((flags & GOVD_LASTPRIVATE)
15419 140254 : || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
15420 156209 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15421 15251 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15422 :
15423 150109 : tree chain = *list_p;
15424 150109 : clause = build_omp_clause (input_location, code);
15425 150109 : OMP_CLAUSE_DECL (clause) = decl;
15426 150109 : OMP_CLAUSE_CHAIN (clause) = chain;
15427 150109 : if (private_debug)
15428 382 : OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
15429 149727 : else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
15430 6 : OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
15431 149721 : else if (code == OMP_CLAUSE_SHARED
15432 41483 : && (flags & GOVD_WRITTEN) == 0
15433 185104 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15434 21084 : OMP_CLAUSE_SHARED_READONLY (clause) = 1;
15435 128637 : else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
15436 22134 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
15437 106503 : else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
15438 : {
15439 630 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
15440 630 : OMP_CLAUSE_DECL (nc) = decl;
15441 630 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
15442 630 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
15443 15 : OMP_CLAUSE_DECL (clause)
15444 30 : = build_fold_indirect_ref_loc (input_location, decl);
15445 630 : OMP_CLAUSE_DECL (clause)
15446 630 : = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
15447 : build_int_cst (build_pointer_type (char_type_node), 0));
15448 630 : OMP_CLAUSE_SIZE (clause) = size_zero_node;
15449 630 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15450 630 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
15451 630 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
15452 630 : tree dtype = TREE_TYPE (decl);
15453 630 : if (TREE_CODE (dtype) == REFERENCE_TYPE)
15454 15 : dtype = TREE_TYPE (dtype);
15455 : /* FIRSTPRIVATE_POINTER doesn't work well if we have a
15456 : multiply-indirected pointer. If we have a reference to a pointer to
15457 : a pointer, it's possible that this should really be
15458 : GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
15459 : moment, so stick with this. (See PR113279 and testcases
15460 : baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice). */
15461 630 : if (TREE_CODE (dtype) == POINTER_TYPE
15462 630 : && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
15463 19 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15464 : else
15465 611 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15466 630 : OMP_CLAUSE_CHAIN (nc) = chain;
15467 630 : OMP_CLAUSE_CHAIN (clause) = nc;
15468 630 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15469 630 : gimplify_omp_ctxp = ctx->outer_context;
15470 630 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
15471 : pre_p, NULL, is_gimple_val, fb_rvalue);
15472 630 : gimplify_omp_ctxp = ctx;
15473 630 : }
15474 17567 : else if (code == OMP_CLAUSE_MAP)
15475 : {
15476 17567 : int kind;
15477 : /* Not all combinations of these GOVD_MAP flags are actually valid. */
15478 17567 : switch (flags & (GOVD_MAP_TO_ONLY
15479 : | GOVD_MAP_FORCE
15480 : | GOVD_MAP_FORCE_PRESENT
15481 : | GOVD_MAP_ALLOC_ONLY
15482 : | GOVD_MAP_FROM_ONLY))
15483 : {
15484 : case 0:
15485 : kind = GOMP_MAP_TOFROM;
15486 : break;
15487 1042 : case GOVD_MAP_FORCE:
15488 1042 : kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
15489 1042 : break;
15490 1015 : case GOVD_MAP_TO_ONLY:
15491 1015 : kind = GOMP_MAP_TO;
15492 1015 : break;
15493 16 : case GOVD_MAP_FROM_ONLY:
15494 16 : kind = GOMP_MAP_FROM;
15495 16 : break;
15496 37 : case GOVD_MAP_ALLOC_ONLY:
15497 37 : kind = GOMP_MAP_ALLOC;
15498 37 : break;
15499 3 : case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
15500 3 : kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
15501 3 : break;
15502 : case GOVD_MAP_FORCE_PRESENT:
15503 326 : kind = GOMP_MAP_FORCE_PRESENT;
15504 : break;
15505 : case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
15506 326 : kind = GOMP_MAP_FORCE_PRESENT;
15507 : break;
15508 0 : default:
15509 0 : gcc_unreachable ();
15510 : }
15511 17567 : OMP_CLAUSE_SET_MAP_KIND (clause, kind);
15512 : /* Setting of the implicit flag for the runtime is currently disabled for
15513 : OpenACC. */
15514 17567 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
15515 10033 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
15516 17567 : if (DECL_SIZE (decl)
15517 17567 : && !poly_int_tree_p (DECL_SIZE (decl)))
15518 : {
15519 535 : tree decl2 = DECL_VALUE_EXPR (decl);
15520 535 : gcc_assert (INDIRECT_REF_P (decl2));
15521 535 : decl2 = TREE_OPERAND (decl2, 0);
15522 535 : gcc_assert (DECL_P (decl2));
15523 535 : tree mem = build_simple_mem_ref (decl2);
15524 535 : OMP_CLAUSE_DECL (clause) = mem;
15525 535 : OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15526 535 : if (gimplify_omp_ctxp->outer_context)
15527 : {
15528 446 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15529 446 : omp_notice_variable (ctx, decl2, true);
15530 446 : omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
15531 : }
15532 535 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15533 : OMP_CLAUSE_MAP);
15534 535 : OMP_CLAUSE_DECL (nc) = decl;
15535 535 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15536 535 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
15537 535 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15538 : else
15539 0 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15540 535 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15541 535 : OMP_CLAUSE_CHAIN (clause) = nc;
15542 : }
15543 17032 : else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
15544 17032 : && omp_privatize_by_reference (decl))
15545 : {
15546 28 : OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
15547 28 : OMP_CLAUSE_SIZE (clause)
15548 28 : = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
15549 28 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15550 28 : gimplify_omp_ctxp = ctx->outer_context;
15551 28 : gimplify_expr (&OMP_CLAUSE_SIZE (clause),
15552 : pre_p, NULL, is_gimple_val, fb_rvalue);
15553 28 : gimplify_omp_ctxp = ctx;
15554 28 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15555 : OMP_CLAUSE_MAP);
15556 28 : OMP_CLAUSE_DECL (nc) = decl;
15557 28 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15558 28 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
15559 28 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15560 28 : OMP_CLAUSE_CHAIN (clause) = nc;
15561 : }
15562 : else
15563 17004 : OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
15564 : }
15565 150109 : if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
15566 : {
15567 690 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
15568 690 : OMP_CLAUSE_DECL (nc) = decl;
15569 690 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
15570 690 : OMP_CLAUSE_CHAIN (nc) = chain;
15571 690 : OMP_CLAUSE_CHAIN (clause) = nc;
15572 690 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15573 690 : gimplify_omp_ctxp = ctx->outer_context;
15574 690 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
15575 690 : (ctx->region_type & ORT_ACC) != 0);
15576 690 : gimplify_omp_ctxp = ctx;
15577 : }
15578 150109 : *list_p = clause;
15579 150109 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15580 150109 : gimplify_omp_ctxp = ctx->outer_context;
15581 : /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
15582 : in simd. Those are only added for the local vars inside of simd body
15583 : and they don't need to be e.g. default constructible. */
15584 150109 : if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
15585 142059 : lang_hooks.decls.omp_finish_clause (clause, pre_p,
15586 142059 : (ctx->region_type & ORT_ACC) != 0);
15587 150109 : if (gimplify_omp_ctxp)
15588 184459 : for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
15589 93642 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
15590 93642 : && DECL_P (OMP_CLAUSE_SIZE (clause)))
15591 1241 : omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
15592 : true);
15593 150109 : gimplify_omp_ctxp = ctx;
15594 150109 : return 0;
15595 : }
15596 :
15597 : static void
15598 130016 : gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
15599 : enum tree_code code,
15600 : gimple_seq *loops_seq_p = NULL)
15601 : {
15602 130016 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15603 130016 : tree *orig_list_p = list_p;
15604 130016 : tree c, decl;
15605 130016 : bool has_inscan_reductions = false;
15606 :
15607 130016 : if (body)
15608 : {
15609 : struct gimplify_omp_ctx *octx;
15610 226058 : for (octx = ctx; octx; octx = octx->outer_context)
15611 174627 : if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
15612 : break;
15613 112293 : if (octx)
15614 : {
15615 60862 : struct walk_stmt_info wi;
15616 60862 : memset (&wi, 0, sizeof (wi));
15617 60862 : walk_gimple_seq (body, omp_find_stores_stmt,
15618 : omp_find_stores_op, &wi);
15619 : }
15620 : }
15621 :
15622 130016 : if (ctx->add_safelen1)
15623 : {
15624 : /* If there are VLAs in the body of simd loop, prevent
15625 : vectorization. */
15626 2 : gcc_assert (ctx->region_type == ORT_SIMD);
15627 2 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
15628 2 : OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
15629 2 : OMP_CLAUSE_CHAIN (c) = *list_p;
15630 2 : *list_p = c;
15631 2 : list_p = &OMP_CLAUSE_CHAIN (c);
15632 : }
15633 :
15634 130016 : if (ctx->region_type == ORT_WORKSHARE
15635 39966 : && ctx->outer_context
15636 28039 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
15637 : {
15638 25038 : for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
15639 12641 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15640 12641 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
15641 : {
15642 125 : decl = OMP_CLAUSE_DECL (c);
15643 125 : splay_tree_node n
15644 125 : = splay_tree_lookup (ctx->outer_context->variables,
15645 : (splay_tree_key) decl);
15646 125 : gcc_checking_assert (!splay_tree_lookup (ctx->variables,
15647 : (splay_tree_key) decl));
15648 125 : omp_add_variable (ctx, decl, n->value);
15649 125 : tree c2 = copy_node (c);
15650 125 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15651 125 : *list_p = c2;
15652 125 : if ((n->value & GOVD_FIRSTPRIVATE) == 0)
15653 103 : continue;
15654 22 : c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15655 : OMP_CLAUSE_FIRSTPRIVATE);
15656 22 : OMP_CLAUSE_DECL (c2) = decl;
15657 22 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15658 22 : *list_p = c2;
15659 : }
15660 : }
15661 :
15662 130016 : if (code == OMP_TARGET
15663 130016 : || code == OMP_TARGET_DATA
15664 130016 : || code == OMP_TARGET_ENTER_DATA
15665 113601 : || code == OMP_TARGET_EXIT_DATA)
15666 : {
15667 17206 : tree mapper_clauses = NULL_TREE;
15668 17206 : instantiate_mapper_info im_info;
15669 :
15670 17206 : im_info.mapper_clauses_p = &mapper_clauses;
15671 17206 : im_info.omp_ctx = ctx;
15672 17206 : im_info.pre_p = pre_p;
15673 :
15674 17206 : splay_tree_foreach (ctx->variables,
15675 : omp_instantiate_implicit_mappers,
15676 : (void *) &im_info);
15677 :
15678 17206 : if (mapper_clauses)
15679 : {
15680 53 : mapper_clauses
15681 53 : = lang_hooks.decls.omp_finish_mapper_clauses (mapper_clauses);
15682 :
15683 : /* Stick the implicitly-expanded mapper clauses at the end of the
15684 : clause list. */
15685 53 : tree *tail = list_p;
15686 163 : while (*tail)
15687 110 : tail = &OMP_CLAUSE_CHAIN (*tail);
15688 53 : *tail = mapper_clauses;
15689 : }
15690 :
15691 17206 : vec<omp_mapping_group> *groups;
15692 17206 : groups = omp_gather_mapping_groups (list_p);
15693 17206 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
15694 :
15695 17206 : if (groups)
15696 : {
15697 8618 : grpmap = omp_index_mapping_groups (groups);
15698 :
15699 8618 : omp_resolve_clause_dependencies (code, groups, grpmap);
15700 8618 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15701 : &grpmap, list_p);
15702 :
15703 8618 : omp_mapping_group *outlist = NULL;
15704 :
15705 17236 : delete grpmap;
15706 8618 : delete groups;
15707 :
15708 : /* Rebuild now we have struct sibling lists. */
15709 8618 : groups = omp_gather_mapping_groups (list_p);
15710 8618 : grpmap = omp_index_mapping_groups (groups);
15711 :
15712 8618 : bool enter_exit = (code == OMP_TARGET_ENTER_DATA
15713 8618 : || code == OMP_TARGET_EXIT_DATA);
15714 :
15715 8618 : outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
15716 8618 : outlist = omp_segregate_mapping_groups (outlist);
15717 8618 : list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
15718 :
15719 8618 : delete grpmap;
15720 8618 : delete groups;
15721 : }
15722 17206 : }
15723 112810 : else if (ctx->region_type & ORT_ACC)
15724 : {
15725 29899 : vec<omp_mapping_group> *groups;
15726 29899 : groups = omp_gather_mapping_groups (list_p);
15727 29899 : if (groups)
15728 : {
15729 9462 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
15730 9462 : grpmap = omp_index_mapping_groups (groups);
15731 :
15732 9462 : oacc_resolve_clause_dependencies (groups, grpmap);
15733 9462 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15734 : &grpmap, list_p);
15735 :
15736 9462 : delete groups;
15737 18924 : delete grpmap;
15738 : }
15739 : }
15740 :
15741 130016 : tree attach_list = NULL_TREE;
15742 130016 : tree *attach_tail = &attach_list;
15743 :
15744 130016 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
15745 :
15746 379897 : while ((c = *list_p) != NULL)
15747 : {
15748 249881 : splay_tree_node n;
15749 249881 : bool remove = false;
15750 249881 : bool move_attach = false;
15751 :
15752 292559 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
15753 : grp_end = NULL_TREE;
15754 :
15755 249881 : switch (OMP_CLAUSE_CODE (c))
15756 : {
15757 8069 : case OMP_CLAUSE_FIRSTPRIVATE:
15758 8069 : if ((ctx->region_type & ORT_TARGET)
15759 1980 : && (ctx->region_type & ORT_ACC) == 0
15760 9443 : && TYPE_ATOMIC (strip_array_types
15761 : (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
15762 : {
15763 4 : error_at (OMP_CLAUSE_LOCATION (c),
15764 : "%<_Atomic%> %qD in %<firstprivate%> clause on "
15765 2 : "%<target%> construct", OMP_CLAUSE_DECL (c));
15766 2 : remove = true;
15767 2 : break;
15768 : }
15769 8067 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
15770 : {
15771 380 : decl = OMP_CLAUSE_DECL (c);
15772 380 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15773 380 : if ((n->value & GOVD_MAP) != 0)
15774 : {
15775 : remove = true;
15776 : break;
15777 : }
15778 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
15779 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
15780 : }
15781 : /* FALLTHRU */
15782 36899 : case OMP_CLAUSE_PRIVATE:
15783 36899 : case OMP_CLAUSE_SHARED:
15784 36899 : case OMP_CLAUSE_LINEAR:
15785 36899 : decl = OMP_CLAUSE_DECL (c);
15786 36899 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15787 36899 : remove = !(n->value & GOVD_SEEN);
15788 36899 : if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
15789 48 : && code == OMP_PARALLEL
15790 36921 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15791 : remove = true;
15792 36877 : if (! remove)
15793 : {
15794 32273 : bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
15795 32273 : if ((n->value & GOVD_DEBUG_PRIVATE)
15796 32273 : || lang_hooks.decls.omp_private_debug_clause (decl, shared))
15797 : {
15798 87 : gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
15799 : || ((n->value & GOVD_DATA_SHARE_CLASS)
15800 : == GOVD_SHARED));
15801 87 : OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
15802 87 : OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
15803 : }
15804 32273 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15805 4159 : && ctx->has_depend
15806 32831 : && DECL_P (decl))
15807 558 : n->value |= GOVD_WRITTEN;
15808 32273 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15809 4159 : && (n->value & GOVD_WRITTEN) == 0
15810 3042 : && DECL_P (decl)
15811 35315 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15812 526 : OMP_CLAUSE_SHARED_READONLY (c) = 1;
15813 31747 : else if (DECL_P (decl)
15814 31747 : && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15815 3633 : && (n->value & GOVD_WRITTEN) != 0)
15816 30630 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15817 9044 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
15818 38789 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15819 5738 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15820 : }
15821 : else
15822 4626 : n->value &= ~GOVD_EXPLICIT;
15823 : break;
15824 :
15825 13010 : case OMP_CLAUSE_LASTPRIVATE:
15826 : /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
15827 : accurately reflect the presence of a FIRSTPRIVATE clause. */
15828 13010 : decl = OMP_CLAUSE_DECL (c);
15829 13010 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15830 13010 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
15831 13010 : = (n->value & GOVD_FIRSTPRIVATE) != 0;
15832 13010 : if (code == OMP_DISTRIBUTE
15833 13010 : && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
15834 : {
15835 4 : remove = true;
15836 4 : error_at (OMP_CLAUSE_LOCATION (c),
15837 : "same variable used in %<firstprivate%> and "
15838 : "%<lastprivate%> clauses on %<distribute%> "
15839 : "construct");
15840 : }
15841 13010 : if (!remove
15842 13006 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15843 13006 : && DECL_P (decl)
15844 13006 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15845 11411 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15846 13010 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
15847 : remove = true;
15848 : break;
15849 :
15850 824 : case OMP_CLAUSE_ALIGNED:
15851 824 : decl = OMP_CLAUSE_DECL (c);
15852 824 : if (!is_global_var (decl))
15853 : {
15854 738 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15855 738 : remove = n == NULL || !(n->value & GOVD_SEEN);
15856 88 : if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15857 : {
15858 88 : struct gimplify_omp_ctx *octx;
15859 88 : if (n != NULL
15860 88 : && (n->value & (GOVD_DATA_SHARE_CLASS
15861 : & ~GOVD_FIRSTPRIVATE)))
15862 : remove = true;
15863 : else
15864 100 : for (octx = ctx->outer_context; octx;
15865 12 : octx = octx->outer_context)
15866 : {
15867 24 : n = splay_tree_lookup (octx->variables,
15868 : (splay_tree_key) decl);
15869 24 : if (n == NULL)
15870 12 : continue;
15871 12 : if (n->value & GOVD_LOCAL)
15872 : break;
15873 : /* We have to avoid assigning a shared variable
15874 : to itself when trying to add
15875 : __builtin_assume_aligned. */
15876 12 : if (n->value & GOVD_SHARED)
15877 : {
15878 : remove = true;
15879 : break;
15880 : }
15881 : }
15882 : }
15883 : }
15884 86 : else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
15885 : {
15886 86 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15887 86 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
15888 : remove = true;
15889 : }
15890 : break;
15891 :
15892 553 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
15893 553 : decl = OMP_CLAUSE_DECL (c);
15894 553 : while (INDIRECT_REF_P (decl)
15895 602 : || TREE_CODE (decl) == ARRAY_REF)
15896 49 : decl = TREE_OPERAND (decl, 0);
15897 553 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15898 553 : remove = n == NULL || !(n->value & GOVD_SEEN);
15899 : break;
15900 :
15901 900 : case OMP_CLAUSE_IS_DEVICE_PTR:
15902 900 : case OMP_CLAUSE_NONTEMPORAL:
15903 900 : decl = OMP_CLAUSE_DECL (c);
15904 900 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15905 900 : remove = n == NULL || !(n->value & GOVD_SEEN);
15906 : break;
15907 :
15908 56850 : case OMP_CLAUSE_MAP:
15909 56850 : if (OMP_CLAUSE_MAP_GIMPLE_ONLY (c))
15910 : {
15911 90 : remove = true;
15912 90 : goto end_adjust_omp_map_clause;
15913 : }
15914 56760 : decl = OMP_CLAUSE_DECL (c);
15915 56760 : if (!grp_end)
15916 : {
15917 31840 : grp_start_p = list_p;
15918 31840 : grp_end = *omp_group_last (grp_start_p);
15919 : }
15920 56760 : switch (OMP_CLAUSE_MAP_KIND (c))
15921 : {
15922 183 : case GOMP_MAP_PRESENT_ALLOC:
15923 183 : case GOMP_MAP_PRESENT_TO:
15924 183 : case GOMP_MAP_PRESENT_FROM:
15925 183 : case GOMP_MAP_PRESENT_TOFROM:
15926 183 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
15927 183 : break;
15928 : default:
15929 : break;
15930 : }
15931 56760 : switch (code)
15932 : {
15933 4563 : case OACC_DATA:
15934 4563 : if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
15935 : break;
15936 : /* Fallthrough. */
15937 19317 : case OACC_HOST_DATA:
15938 19317 : case OACC_ENTER_DATA:
15939 19317 : case OACC_EXIT_DATA:
15940 19317 : case OMP_TARGET_DATA:
15941 19317 : case OMP_TARGET_ENTER_DATA:
15942 19317 : case OMP_TARGET_EXIT_DATA:
15943 19317 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15944 19317 : || (OMP_CLAUSE_MAP_KIND (c)
15945 : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15946 : /* For target {,enter ,exit }data only the array slice is
15947 : mapped, but not the pointer to it. */
15948 : remove = true;
15949 19317 : if (code == OMP_TARGET_EXIT_DATA
15950 19317 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
15951 2805 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
15952 : remove = true;
15953 : break;
15954 : case OMP_TARGET:
15955 : break;
15956 : default:
15957 : break;
15958 : }
15959 19315 : if (remove)
15960 : break;
15961 56425 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15962 : {
15963 : /* Sanity check: attach/detach map kinds use the size as a bias,
15964 : and it's never right to use the decl size for such
15965 : mappings. */
15966 17797 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15967 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15968 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
15969 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15970 : && (OMP_CLAUSE_MAP_KIND (c)
15971 : != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
15972 21856 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
15973 4059 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
15974 : }
15975 56425 : gimplify_omp_ctxp = ctx->outer_context;
15976 56425 : gimple_seq *seq_p;
15977 56425 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
15978 56425 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
15979 : is_gimple_val, fb_rvalue) == GS_ERROR)
15980 : {
15981 0 : gimplify_omp_ctxp = ctx;
15982 0 : remove = true;
15983 0 : goto end_adjust_omp_map_clause;
15984 : }
15985 56425 : else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15986 53505 : || (OMP_CLAUSE_MAP_KIND (c)
15987 : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15988 53092 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15989 63067 : && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
15990 : {
15991 912 : OMP_CLAUSE_SIZE (c)
15992 912 : = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), seq_p, NULL,
15993 : false);
15994 912 : if ((ctx->region_type & ORT_TARGET) != 0)
15995 703 : omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
15996 : GOVD_FIRSTPRIVATE | GOVD_SEEN);
15997 : }
15998 56425 : gimplify_omp_ctxp = ctx;
15999 : /* Data clauses associated with reductions must be
16000 : compatible with present_or_copy. Warn and adjust the clause
16001 : if that is not the case. */
16002 56425 : if (ctx->region_type == ORT_ACC_PARALLEL
16003 47446 : || ctx->region_type == ORT_ACC_SERIAL)
16004 : {
16005 9535 : tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
16006 9535 : n = NULL;
16007 :
16008 9535 : if (DECL_P (t))
16009 7770 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
16010 :
16011 7770 : if (n && (n->value & GOVD_REDUCTION))
16012 : {
16013 809 : enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
16014 :
16015 809 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16016 809 : if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
16017 159 : && kind != GOMP_MAP_FORCE_PRESENT
16018 159 : && kind != GOMP_MAP_POINTER)
16019 : {
16020 120 : warning_at (OMP_CLAUSE_LOCATION (c), 0,
16021 : "incompatible data clause with reduction "
16022 : "on %qE; promoting to %<present_or_copy%>",
16023 120 : DECL_NAME (t));
16024 120 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
16025 : }
16026 : }
16027 : }
16028 56425 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
16029 53639 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
16030 56699 : && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
16031 : {
16032 536 : remove = true;
16033 536 : goto end_adjust_omp_map_clause;
16034 : }
16035 : /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
16036 : a variable captured in a lambda closure), look through that now
16037 : before the DECL_P check below. (A code other than COMPONENT_REF,
16038 : i.e. INDIRECT_REF, will be a VLA/variable-length array
16039 : section. A global var may be a variable in a common block. We
16040 : don't want to do this here for either of those.) */
16041 55889 : if ((ctx->region_type & ORT_ACC) == 0
16042 29682 : && DECL_P (decl)
16043 12726 : && !is_global_var (decl)
16044 11314 : && DECL_HAS_VALUE_EXPR_P (decl)
16045 56079 : && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
16046 0 : decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
16047 55889 : if (TREE_CODE (decl) == TARGET_EXPR)
16048 : {
16049 0 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
16050 : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
16051 26285 : remove = true;
16052 : }
16053 55889 : else if (!DECL_P (decl))
16054 : {
16055 29604 : if ((ctx->region_type & ORT_TARGET) != 0
16056 29604 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
16057 : {
16058 24 : if (INDIRECT_REF_P (decl)
16059 0 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
16060 24 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
16061 : == REFERENCE_TYPE))
16062 0 : decl = TREE_OPERAND (decl, 0);
16063 24 : if (TREE_CODE (decl) == COMPONENT_REF)
16064 : {
16065 0 : while (TREE_CODE (decl) == COMPONENT_REF)
16066 0 : decl = TREE_OPERAND (decl, 0);
16067 0 : if (DECL_P (decl))
16068 : {
16069 0 : n = splay_tree_lookup (ctx->variables,
16070 : (splay_tree_key) decl);
16071 0 : if (!(n->value & GOVD_SEEN))
16072 29604 : remove = true;
16073 : }
16074 : }
16075 : }
16076 :
16077 29604 : tree d = decl, *pd;
16078 29604 : if (TREE_CODE (d) == ARRAY_REF)
16079 : {
16080 5836 : while (TREE_CODE (d) == ARRAY_REF)
16081 2978 : d = TREE_OPERAND (d, 0);
16082 2858 : if (TREE_CODE (d) == COMPONENT_REF
16083 2858 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
16084 : decl = d;
16085 : }
16086 29604 : pd = &OMP_CLAUSE_DECL (c);
16087 29604 : if (d == decl
16088 27105 : && TREE_CODE (decl) == INDIRECT_REF
16089 15461 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
16090 1471 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
16091 : == REFERENCE_TYPE)
16092 30404 : && (OMP_CLAUSE_MAP_KIND (c)
16093 : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
16094 : {
16095 796 : pd = &TREE_OPERAND (decl, 0);
16096 796 : decl = TREE_OPERAND (decl, 0);
16097 : }
16098 :
16099 29604 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
16100 3067 : switch (code)
16101 : {
16102 427 : case OACC_ENTER_DATA:
16103 427 : case OACC_EXIT_DATA:
16104 427 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
16105 : == ARRAY_TYPE)
16106 : remove = true;
16107 417 : else if (code == OACC_ENTER_DATA)
16108 263 : goto change_to_attach;
16109 : /* Fallthrough. */
16110 605 : case OMP_TARGET_EXIT_DATA:
16111 605 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
16112 605 : OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c) = 0;
16113 605 : break;
16114 71 : case OACC_UPDATE:
16115 : /* An "attach/detach" operation on an update directive
16116 : should behave as a GOMP_MAP_ALWAYS_POINTER. Note that
16117 : both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
16118 : kinds depend on the previous mapping (for non-TARGET
16119 : regions). */
16120 71 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
16121 71 : break;
16122 2391 : default:
16123 2391 : change_to_attach:
16124 2391 : gcc_assert (!OMP_CLAUSE_MAP_SIZE_NEEDS_ADJUSTMENT (c));
16125 2391 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
16126 2391 : if ((ctx->region_type & ORT_TARGET) != 0)
16127 2113 : move_attach = true;
16128 : }
16129 26537 : else if ((ctx->region_type & ORT_TARGET) != 0
16130 26537 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16131 11823 : || (OMP_CLAUSE_MAP_KIND (c)
16132 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
16133 : move_attach = true;
16134 :
16135 : /* If we have e.g. map(struct: *var), don't gimplify the
16136 : argument since omp-low.cc wants to see the decl itself. */
16137 29604 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
16138 1136 : goto end_adjust_omp_map_clause;
16139 :
16140 : /* We've already partly gimplified this in
16141 : gimplify_scan_omp_clauses. Don't do any more. */
16142 28468 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
16143 88 : goto end_adjust_omp_map_clause;
16144 :
16145 28380 : gimplify_omp_ctxp = ctx->outer_context;
16146 28380 : if (gimplify_expr (pd, seq_p, NULL, is_gimple_lvalue,
16147 : fb_lvalue) == GS_ERROR)
16148 0 : remove = true;
16149 28380 : gimplify_omp_ctxp = ctx;
16150 28380 : goto end_adjust_omp_map_clause;
16151 : }
16152 :
16153 26285 : if ((code == OMP_TARGET
16154 : || code == OMP_TARGET_DATA
16155 : || code == OMP_TARGET_ENTER_DATA
16156 14453 : || code == OMP_TARGET_EXIT_DATA)
16157 26917 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
16158 : {
16159 242 : bool firstprivatize = false;
16160 :
16161 247 : for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
16162 5 : octx = octx->outer_context)
16163 : {
16164 6 : splay_tree_node n
16165 12 : = splay_tree_lookup (octx->variables,
16166 6 : (splay_tree_key) OMP_CLAUSE_DECL (c));
16167 : /* If this is contained in an outer OpenMP region as a
16168 : firstprivate value, remove the attach/detach. */
16169 6 : if (n && (n->value & GOVD_FIRSTPRIVATE))
16170 : {
16171 : firstprivatize = true;
16172 : break;
16173 : }
16174 : }
16175 :
16176 242 : enum gomp_map_kind map_kind;
16177 242 : if (firstprivatize)
16178 : map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
16179 241 : else if (code == OMP_TARGET_EXIT_DATA)
16180 : map_kind = GOMP_MAP_DETACH;
16181 : else
16182 190 : map_kind = GOMP_MAP_ATTACH;
16183 242 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
16184 : }
16185 26043 : else if ((ctx->region_type & ORT_ACC) != 0
16186 26043 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
16187 : {
16188 0 : enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
16189 0 : ? GOMP_MAP_DETACH
16190 : : GOMP_MAP_ATTACH);
16191 0 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
16192 : }
16193 :
16194 26285 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16195 26285 : if ((ctx->region_type & ORT_TARGET) != 0
16196 16320 : && !(n->value & GOVD_SEEN)
16197 1511 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
16198 27549 : && (!is_global_var (decl)
16199 75 : || !lookup_attribute ("omp declare target link",
16200 75 : DECL_ATTRIBUTES (decl))))
16201 : {
16202 1252 : remove = true;
16203 : /* For struct element mapping, if struct is never referenced
16204 : in target block and none of the mapping has always modifier,
16205 : remove all the struct element mappings, which immediately
16206 : follow the GOMP_MAP_STRUCT map clause. */
16207 1252 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
16208 1252 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
16209 : {
16210 30 : HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
16211 80 : while (cnt--)
16212 50 : OMP_CLAUSE_CHAIN (c)
16213 50 : = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
16214 : }
16215 : }
16216 25033 : else if (DECL_SIZE (decl)
16217 25009 : && !poly_int_tree_p (DECL_SIZE (decl))
16218 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
16219 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
16220 25084 : && (OMP_CLAUSE_MAP_KIND (c)
16221 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
16222 : {
16223 : /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
16224 : for these, TREE_CODE (DECL_SIZE (decl)) will always be
16225 : INTEGER_CST. */
16226 51 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
16227 :
16228 51 : tree decl2 = DECL_VALUE_EXPR (decl);
16229 51 : gcc_assert (INDIRECT_REF_P (decl2));
16230 51 : decl2 = TREE_OPERAND (decl2, 0);
16231 51 : gcc_assert (DECL_P (decl2));
16232 51 : tree mem = build_simple_mem_ref (decl2);
16233 51 : OMP_CLAUSE_DECL (c) = mem;
16234 51 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16235 51 : if (ctx->outer_context)
16236 : {
16237 15 : omp_notice_variable (ctx->outer_context, decl2, true);
16238 30 : omp_notice_variable (ctx->outer_context,
16239 15 : OMP_CLAUSE_SIZE (c), true);
16240 : }
16241 51 : if (((ctx->region_type & ORT_TARGET) != 0
16242 24 : || !ctx->target_firstprivatize_array_bases)
16243 34 : && ((n->value & GOVD_SEEN) == 0
16244 30 : || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
16245 : {
16246 34 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16247 : OMP_CLAUSE_MAP);
16248 34 : OMP_CLAUSE_DECL (nc) = decl;
16249 34 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
16250 34 : if (ctx->target_firstprivatize_array_bases)
16251 27 : OMP_CLAUSE_SET_MAP_KIND (nc,
16252 : GOMP_MAP_FIRSTPRIVATE_POINTER);
16253 : else
16254 7 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
16255 34 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
16256 34 : OMP_CLAUSE_CHAIN (c) = nc;
16257 34 : c = nc;
16258 : }
16259 : }
16260 : else
16261 : {
16262 24982 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16263 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16264 24982 : gcc_assert ((n->value & GOVD_SEEN) == 0
16265 : || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16266 : == 0));
16267 : }
16268 :
16269 : /* If we have a target region, we can push all the attaches to the
16270 : end of the list (we may have standalone "attach" operations
16271 : synthesized for GOMP_MAP_STRUCT nodes that must be processed after
16272 : the attachment point AND the pointed-to block have been mapped).
16273 : If we have something else, e.g. "enter data", we need to keep
16274 : "attach" nodes together with the previous node they attach to so
16275 : that separate "exit data" operations work properly (see
16276 : libgomp/target.c). */
16277 26285 : if ((ctx->region_type & ORT_TARGET) != 0
16278 26285 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16279 16204 : || (OMP_CLAUSE_MAP_KIND (c)
16280 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
16281 : move_attach = true;
16282 :
16283 56515 : end_adjust_omp_map_clause:
16284 56515 : exit_omp_iterator_loop_context (c);
16285 56515 : break;
16286 :
16287 8371 : case OMP_CLAUSE_TO:
16288 8371 : case OMP_CLAUSE_FROM:
16289 8371 : case OMP_CLAUSE__CACHE_:
16290 8371 : decl = OMP_CLAUSE_DECL (c);
16291 8371 : if (!DECL_P (decl))
16292 : break;
16293 6203 : if (DECL_SIZE (decl)
16294 6203 : && !poly_int_tree_p (DECL_SIZE (decl)))
16295 : {
16296 4 : tree decl2 = DECL_VALUE_EXPR (decl);
16297 4 : gcc_assert (INDIRECT_REF_P (decl2));
16298 4 : decl2 = TREE_OPERAND (decl2, 0);
16299 4 : gcc_assert (DECL_P (decl2));
16300 4 : tree mem = build_simple_mem_ref (decl2);
16301 4 : OMP_CLAUSE_DECL (c) = mem;
16302 4 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16303 4 : if (ctx->outer_context)
16304 : {
16305 4 : omp_notice_variable (ctx->outer_context, decl2, true);
16306 4 : omp_notice_variable (ctx->outer_context,
16307 4 : OMP_CLAUSE_SIZE (c), true);
16308 : }
16309 : }
16310 6199 : else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16311 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16312 : break;
16313 :
16314 15380 : case OMP_CLAUSE_REDUCTION:
16315 15380 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
16316 : {
16317 831 : decl = OMP_CLAUSE_DECL (c);
16318 831 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16319 831 : if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
16320 : {
16321 69 : remove = true;
16322 69 : error_at (OMP_CLAUSE_LOCATION (c),
16323 : "%qD specified in %<inscan%> %<reduction%> clause "
16324 : "but not in %<scan%> directive clause", decl);
16325 69 : break;
16326 : }
16327 : has_inscan_reductions = true;
16328 : }
16329 : /* FALLTHRU */
16330 17915 : case OMP_CLAUSE_IN_REDUCTION:
16331 17915 : case OMP_CLAUSE_TASK_REDUCTION:
16332 17915 : decl = OMP_CLAUSE_DECL (c);
16333 : /* OpenACC reductions need a present_or_copy data clause.
16334 : Add one if necessary. Emit error when the reduction is private. */
16335 17915 : if (ctx->region_type == ORT_ACC_PARALLEL
16336 17383 : || ctx->region_type == ORT_ACC_SERIAL)
16337 : {
16338 803 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16339 803 : if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16340 : {
16341 13 : remove = true;
16342 13 : error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
16343 13 : "reduction on %qE", DECL_NAME (decl));
16344 : }
16345 790 : else if ((n->value & GOVD_MAP) == 0)
16346 : {
16347 524 : tree next = OMP_CLAUSE_CHAIN (c);
16348 524 : tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
16349 524 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
16350 524 : OMP_CLAUSE_DECL (nc) = decl;
16351 524 : OMP_CLAUSE_CHAIN (c) = nc;
16352 524 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
16353 524 : (ctx->region_type
16354 : & ORT_ACC) != 0);
16355 550 : while (1)
16356 : {
16357 537 : OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
16358 537 : if (OMP_CLAUSE_CHAIN (nc) == NULL)
16359 : break;
16360 13 : nc = OMP_CLAUSE_CHAIN (nc);
16361 : }
16362 524 : OMP_CLAUSE_CHAIN (nc) = next;
16363 524 : n->value |= GOVD_MAP;
16364 : }
16365 : }
16366 17915 : if (DECL_P (decl)
16367 17915 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
16368 8032 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
16369 : break;
16370 :
16371 3682 : case OMP_CLAUSE_ALLOCATE:
16372 3682 : decl = OMP_CLAUSE_DECL (c);
16373 3682 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16374 3682 : if (n != NULL && !(n->value & GOVD_SEEN))
16375 : {
16376 1180 : if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
16377 : != 0
16378 1180 : && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
16379 : remove = true;
16380 : }
16381 : if (!remove
16382 2502 : && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
16383 1286 : && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
16384 492 : && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
16385 347 : || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
16386 287 : || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
16387 : {
16388 223 : tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
16389 223 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
16390 223 : if (n == NULL)
16391 : {
16392 92 : enum omp_clause_default_kind default_kind
16393 : = ctx->default_kind;
16394 92 : ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
16395 92 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16396 : true);
16397 92 : ctx->default_kind = default_kind;
16398 : }
16399 : else
16400 131 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16401 : true);
16402 : }
16403 : break;
16404 :
16405 : case OMP_CLAUSE_COPYIN:
16406 : case OMP_CLAUSE_COPYPRIVATE:
16407 : case OMP_CLAUSE_IF:
16408 : case OMP_CLAUSE_SELF:
16409 : case OMP_CLAUSE_NUM_THREADS:
16410 : case OMP_CLAUSE_NUM_TEAMS:
16411 : case OMP_CLAUSE_THREAD_LIMIT:
16412 : case OMP_CLAUSE_DIST_SCHEDULE:
16413 : case OMP_CLAUSE_DEVICE:
16414 : case OMP_CLAUSE_SCHEDULE:
16415 : case OMP_CLAUSE_NOWAIT:
16416 : case OMP_CLAUSE_ORDERED:
16417 : case OMP_CLAUSE_DEFAULT:
16418 : case OMP_CLAUSE_UNTIED:
16419 : case OMP_CLAUSE_COLLAPSE:
16420 : case OMP_CLAUSE_FINAL:
16421 : case OMP_CLAUSE_MERGEABLE:
16422 : case OMP_CLAUSE_PROC_BIND:
16423 : case OMP_CLAUSE_SAFELEN:
16424 : case OMP_CLAUSE_SIMDLEN:
16425 : case OMP_CLAUSE_DEPEND:
16426 : case OMP_CLAUSE_DOACROSS:
16427 : case OMP_CLAUSE_PRIORITY:
16428 : case OMP_CLAUSE_GRAINSIZE:
16429 : case OMP_CLAUSE_NUM_TASKS:
16430 : case OMP_CLAUSE_NOGROUP:
16431 : case OMP_CLAUSE_THREADS:
16432 : case OMP_CLAUSE_SIMD:
16433 : case OMP_CLAUSE_FILTER:
16434 : case OMP_CLAUSE_HINT:
16435 : case OMP_CLAUSE_DEFAULTMAP:
16436 : case OMP_CLAUSE_ORDER:
16437 : case OMP_CLAUSE_BIND:
16438 : case OMP_CLAUSE_DETACH:
16439 : case OMP_CLAUSE_USE_DEVICE_PTR:
16440 : case OMP_CLAUSE_USE_DEVICE_ADDR:
16441 : case OMP_CLAUSE_ASYNC:
16442 : case OMP_CLAUSE_WAIT:
16443 : case OMP_CLAUSE_INDEPENDENT:
16444 : case OMP_CLAUSE_NUM_GANGS:
16445 : case OMP_CLAUSE_NUM_WORKERS:
16446 : case OMP_CLAUSE_VECTOR_LENGTH:
16447 : case OMP_CLAUSE_GANG:
16448 : case OMP_CLAUSE_WORKER:
16449 : case OMP_CLAUSE_VECTOR:
16450 : case OMP_CLAUSE_AUTO:
16451 : case OMP_CLAUSE_SEQ:
16452 : case OMP_CLAUSE_TILE:
16453 : case OMP_CLAUSE_IF_PRESENT:
16454 : case OMP_CLAUSE_FINALIZE:
16455 : case OMP_CLAUSE_INCLUSIVE:
16456 : case OMP_CLAUSE_EXCLUSIVE:
16457 : case OMP_CLAUSE_USES_ALLOCATORS:
16458 : case OMP_CLAUSE_DEVICE_TYPE:
16459 : case OMP_CLAUSE_MESSAGE:
16460 : break;
16461 :
16462 0 : case OMP_CLAUSE_NOHOST:
16463 0 : default:
16464 0 : gcc_unreachable ();
16465 : }
16466 :
16467 128709 : if (remove)
16468 9943 : *list_p = OMP_CLAUSE_CHAIN (c);
16469 239938 : else if (move_attach)
16470 : {
16471 : /* Remove attach node from here, separate out into its own list. */
16472 2360 : *attach_tail = c;
16473 2360 : *list_p = OMP_CLAUSE_CHAIN (c);
16474 2360 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16475 2360 : attach_tail = &OMP_CLAUSE_CHAIN (c);
16476 : }
16477 : else
16478 237578 : list_p = &OMP_CLAUSE_CHAIN (c);
16479 : }
16480 :
16481 : /* Splice attach nodes at the end of the list. */
16482 130016 : if (attach_list)
16483 : {
16484 1127 : *list_p = attach_list;
16485 1127 : list_p = attach_tail;
16486 : }
16487 :
16488 : /* Add in any implicit data sharing. */
16489 130016 : struct gimplify_adjust_omp_clauses_data data;
16490 130016 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
16491 : {
16492 : /* OpenMP. Implicit clauses are added at the start of the clause list,
16493 : but after any non-map clauses. */
16494 : tree *implicit_add_list_p = orig_list_p;
16495 254168 : while (*implicit_add_list_p
16496 254168 : && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
16497 154051 : implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
16498 100117 : data.list_p = implicit_add_list_p;
16499 : }
16500 : else
16501 : /* OpenACC. */
16502 29899 : data.list_p = list_p;
16503 130016 : data.pre_p = pre_p;
16504 130016 : splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
16505 :
16506 130016 : if (has_inscan_reductions)
16507 2499 : for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
16508 1870 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16509 1870 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
16510 : {
16511 5 : error_at (OMP_CLAUSE_LOCATION (c),
16512 : "%<inscan%> %<reduction%> clause used together with "
16513 : "%<linear%> clause for a variable other than loop "
16514 : "iterator");
16515 5 : break;
16516 : }
16517 :
16518 130016 : gimplify_omp_ctxp = ctx->outer_context;
16519 130016 : delete_omp_context (ctx);
16520 130016 : }
16521 :
16522 : /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
16523 : * -1 if run-time evaluation is needed. */
16524 :
16525 : int
16526 251784 : omp_has_novariants (void)
16527 : {
16528 251784 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16529 251784 : if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
16530 : {
16531 3290 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
16532 3290 : if (c != NULL_TREE)
16533 : {
16534 299 : if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16535 : return 1;
16536 171 : else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16537 : return 0;
16538 : else
16539 38 : return -1;
16540 : }
16541 : return 0;
16542 : }
16543 : return 0;
16544 : }
16545 :
16546 : /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
16547 : * -1 if run-time evaluation is needed. */
16548 :
16549 : static int
16550 1903 : omp_has_nocontext (void)
16551 : {
16552 1903 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16553 1903 : if (ctx != NULL && ctx->code == OMP_DISPATCH)
16554 : {
16555 1903 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
16556 1903 : if (c != NULL_TREE)
16557 : {
16558 256 : if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16559 : return 1;
16560 94 : else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16561 : return 0;
16562 : else
16563 44 : return -1;
16564 : }
16565 : return 0;
16566 : }
16567 : return 0;
16568 : }
16569 :
16570 : /* Collect a list of traits for enclosing constructs in the current
16571 : OpenMP context. The list is in the same format as the trait selector
16572 : list of construct trait sets built by the front ends.
16573 :
16574 : Per the OpenMP specification, the construct trait set includes constructs
16575 : up to an enclosing "target" construct. If there is no "target" construct,
16576 : then additional things may be added to the construct trait set (simd for
16577 : simd clones, additional constructs associated with "declare variant",
16578 : the target trait for "declare target"); those are not handled here.
16579 : In particular simd clones are not known during gimplification so
16580 : matching/scoring of context selectors that might involve them needs
16581 : to be deferred to the omp_device_lower pass. */
16582 :
16583 : tree
16584 2213 : omp_get_construct_context (void)
16585 : {
16586 2213 : tree result = NULL_TREE;
16587 4184 : for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
16588 : {
16589 2092 : if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
16590 : == ORT_TARGET)
16591 121 : && ctx->code == OMP_TARGET)
16592 : {
16593 121 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
16594 : NULL_TREE, NULL_TREE, result);
16595 : /* We're not interested in any outer constructs. */
16596 121 : break;
16597 : }
16598 1971 : else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
16599 214 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
16600 : NULL_TREE, NULL_TREE, result);
16601 1757 : else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
16602 75 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
16603 : NULL_TREE, NULL_TREE, result);
16604 1682 : else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
16605 164 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
16606 : NULL_TREE, NULL_TREE, result);
16607 1518 : else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
16608 1279 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
16609 : NULL_TREE, NULL_TREE, result);
16610 239 : else if (ctx->region_type == ORT_SIMD
16611 32 : && ctx->code == OMP_SIMD
16612 271 : && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
16613 : {
16614 32 : tree props = NULL_TREE;
16615 32 : tree *last = &props;
16616 72 : for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
16617 40 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
16618 32 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
16619 72 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
16620 : {
16621 8 : *last = unshare_expr (c);
16622 8 : last = &(OMP_CLAUSE_CHAIN (c));
16623 : }
16624 32 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
16625 : NULL_TREE, props, result);
16626 : }
16627 207 : else if (ctx->region_type == ORT_WORKSHARE
16628 5 : && ctx->code == OMP_LOOP
16629 0 : && ctx->outer_context
16630 0 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
16631 0 : && ctx->outer_context->outer_context
16632 0 : && ctx->outer_context->outer_context->code == OMP_LOOP
16633 0 : && ctx->outer_context->outer_context->distribute)
16634 1971 : ctx = ctx->outer_context->outer_context;
16635 1971 : ctx = ctx->outer_context;
16636 : }
16637 :
16638 2213 : return result;
16639 : }
16640 :
16641 : /* Gimplify OACC_CACHE. */
16642 :
16643 : static void
16644 717 : gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
16645 : {
16646 717 : tree expr = *expr_p;
16647 :
16648 717 : gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
16649 : OACC_CACHE);
16650 717 : gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
16651 : OACC_CACHE);
16652 :
16653 : /* TODO: Do something sensible with this information. */
16654 :
16655 717 : *expr_p = NULL_TREE;
16656 717 : }
16657 :
16658 : /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
16659 : if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
16660 : kind. The entry kind will replace the one in CLAUSE, while the exit
16661 : kind will be used in a new omp_clause and returned to the caller. */
16662 :
16663 : static tree
16664 190 : gimplify_oacc_declare_1 (tree clause)
16665 : {
16666 190 : HOST_WIDE_INT kind, new_op;
16667 190 : bool ret = false;
16668 190 : tree c = NULL;
16669 :
16670 190 : kind = OMP_CLAUSE_MAP_KIND (clause);
16671 :
16672 190 : switch (kind)
16673 : {
16674 : case GOMP_MAP_ALLOC:
16675 : new_op = GOMP_MAP_RELEASE;
16676 : ret = true;
16677 : break;
16678 :
16679 29 : case GOMP_MAP_FROM:
16680 29 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
16681 29 : new_op = GOMP_MAP_FROM;
16682 29 : ret = true;
16683 29 : break;
16684 :
16685 40 : case GOMP_MAP_TOFROM:
16686 40 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
16687 40 : new_op = GOMP_MAP_FROM;
16688 40 : ret = true;
16689 40 : break;
16690 :
16691 : case GOMP_MAP_DEVICE_RESIDENT:
16692 : case GOMP_MAP_FORCE_DEVICEPTR:
16693 : case GOMP_MAP_FORCE_PRESENT:
16694 : case GOMP_MAP_LINK:
16695 : case GOMP_MAP_POINTER:
16696 : case GOMP_MAP_TO:
16697 : break;
16698 :
16699 0 : default:
16700 0 : gcc_unreachable ();
16701 69 : break;
16702 : }
16703 :
16704 69 : if (ret)
16705 : {
16706 116 : c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
16707 116 : OMP_CLAUSE_SET_MAP_KIND (c, new_op);
16708 116 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
16709 : }
16710 :
16711 190 : return c;
16712 : }
16713 :
16714 : /* Gimplify OACC_DECLARE. */
16715 :
16716 : static void
16717 254 : gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
16718 : {
16719 254 : tree expr = *expr_p;
16720 254 : gomp_target *stmt;
16721 254 : tree clauses, t, decl;
16722 :
16723 254 : clauses = OACC_DECLARE_CLAUSES (expr);
16724 :
16725 254 : gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
16726 254 : gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
16727 :
16728 516 : for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16729 : {
16730 262 : decl = OMP_CLAUSE_DECL (t);
16731 :
16732 262 : if (TREE_CODE (decl) == MEM_REF)
16733 8 : decl = TREE_OPERAND (decl, 0);
16734 :
16735 262 : if (VAR_P (decl) && !is_oacc_declared (decl))
16736 : {
16737 262 : tree attr = get_identifier ("oacc declare target");
16738 262 : DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
16739 262 : DECL_ATTRIBUTES (decl));
16740 : }
16741 :
16742 262 : if (VAR_P (decl)
16743 262 : && !is_global_var (decl)
16744 452 : && DECL_CONTEXT (decl) == current_function_decl)
16745 : {
16746 190 : tree c = gimplify_oacc_declare_1 (t);
16747 190 : if (c)
16748 : {
16749 116 : if (oacc_declare_returns == NULL)
16750 40 : oacc_declare_returns = new hash_map<tree, tree>;
16751 :
16752 116 : oacc_declare_returns->put (decl, c);
16753 : }
16754 : }
16755 :
16756 262 : if (gimplify_omp_ctxp)
16757 72 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
16758 : }
16759 :
16760 254 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
16761 : clauses);
16762 :
16763 254 : gimplify_seq_add_stmt (pre_p, stmt);
16764 :
16765 254 : *expr_p = NULL_TREE;
16766 254 : }
16767 :
16768 : /* Gimplify the contents of an OMP_PARALLEL statement. This involves
16769 : gimplification of the body, as well as scanning the body for used
16770 : variables. We need to do this scan now, because variable-sized
16771 : decls will be decomposed during gimplification. */
16772 :
16773 : static void
16774 18384 : gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
16775 : {
16776 18384 : tree expr = *expr_p;
16777 18384 : gimple *g;
16778 18384 : gimple_seq body = NULL;
16779 :
16780 36768 : gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
16781 18384 : OMP_PARALLEL_COMBINED (expr)
16782 : ? ORT_COMBINED_PARALLEL
16783 : : ORT_PARALLEL, OMP_PARALLEL);
16784 :
16785 18384 : push_gimplify_context ();
16786 :
16787 18384 : g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
16788 18384 : if (gimple_code (g) == GIMPLE_BIND)
16789 18384 : pop_gimplify_context (g);
16790 : else
16791 0 : pop_gimplify_context (NULL);
16792 :
16793 18384 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
16794 : OMP_PARALLEL);
16795 :
16796 36768 : g = gimple_build_omp_parallel (body,
16797 18384 : OMP_PARALLEL_CLAUSES (expr),
16798 : NULL_TREE, NULL_TREE);
16799 18384 : if (OMP_PARALLEL_COMBINED (expr))
16800 12584 : gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
16801 18384 : gimplify_seq_add_stmt (pre_p, g);
16802 18384 : *expr_p = NULL_TREE;
16803 18384 : }
16804 :
16805 : /* Gimplify the contents of an OMP_TASK statement. This involves
16806 : gimplification of the body, as well as scanning the body for used
16807 : variables. We need to do this scan now, because variable-sized
16808 : decls will be decomposed during gimplification. */
16809 :
16810 : static void
16811 4109 : gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
16812 : {
16813 4109 : tree expr = *expr_p;
16814 4109 : gimple *g;
16815 4109 : gimple_seq body = NULL;
16816 4109 : bool nowait = false;
16817 4109 : bool has_depend = false;
16818 :
16819 4109 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16820 : {
16821 242 : for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
16822 153 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
16823 : {
16824 111 : has_depend = true;
16825 111 : if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
16826 : {
16827 9 : error_at (OMP_CLAUSE_LOCATION (c),
16828 : "%<mutexinoutset%> kind in %<depend%> clause on a "
16829 : "%<taskwait%> construct");
16830 9 : break;
16831 : }
16832 : }
16833 42 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
16834 42 : nowait = true;
16835 98 : if (nowait && !has_depend)
16836 : {
16837 5 : error_at (EXPR_LOCATION (expr),
16838 : "%<taskwait%> construct with %<nowait%> clause but no "
16839 : "%<depend%> clauses");
16840 5 : *expr_p = NULL_TREE;
16841 5 : return;
16842 : }
16843 : }
16844 :
16845 12312 : gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
16846 4104 : omp_find_clause (OMP_TASK_CLAUSES (expr),
16847 : OMP_CLAUSE_UNTIED)
16848 : ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
16849 :
16850 4104 : if (OMP_TASK_BODY (expr))
16851 : {
16852 4011 : push_gimplify_context ();
16853 :
16854 4011 : g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
16855 4011 : if (gimple_code (g) == GIMPLE_BIND)
16856 4011 : pop_gimplify_context (g);
16857 : else
16858 0 : pop_gimplify_context (NULL);
16859 : }
16860 :
16861 4104 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
16862 : OMP_TASK);
16863 :
16864 8208 : g = gimple_build_omp_task (body,
16865 4104 : OMP_TASK_CLAUSES (expr),
16866 : NULL_TREE, NULL_TREE,
16867 : NULL_TREE, NULL_TREE, NULL_TREE);
16868 4104 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16869 93 : gimple_omp_task_set_taskwait_p (g, true);
16870 4104 : gimplify_seq_add_stmt (pre_p, g);
16871 4104 : *expr_p = NULL_TREE;
16872 : }
16873 :
16874 : /* Helper function for gimplify_omp_for. If *TP is not a gimple constant,
16875 : force it into a temporary initialized in PRE_P and add firstprivate clause
16876 : to ORIG_FOR_STMT. */
16877 :
16878 : static void
16879 4399 : gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
16880 : tree orig_for_stmt)
16881 : {
16882 4399 : if (*tp == NULL || is_gimple_constant (*tp))
16883 : return;
16884 :
16885 779 : if (TREE_CODE (*tp) == SAVE_EXPR)
16886 135 : gimplify_save_expr (tp, pre_p, NULL);
16887 : else
16888 644 : *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
16889 : /* Reference to pointer conversion is considered useless,
16890 : but is significant for firstprivate clause. Force it
16891 : here. */
16892 779 : if (type
16893 687 : && TREE_CODE (type) == POINTER_TYPE
16894 869 : && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
16895 : {
16896 2 : tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
16897 2 : tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
16898 2 : gimplify_and_add (m, pre_p);
16899 2 : *tp = v;
16900 : }
16901 :
16902 779 : tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16903 779 : OMP_CLAUSE_DECL (c) = *tp;
16904 779 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
16905 779 : OMP_FOR_CLAUSES (orig_for_stmt) = c;
16906 : }
16907 :
16908 : /* Helper function of gimplify_omp_for, find OMP_ORDERED with
16909 : null OMP_ORDERED_BODY inside of OMP_FOR's body. */
16910 :
16911 : static tree
16912 16496 : find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
16913 : {
16914 16496 : switch (TREE_CODE (*tp))
16915 : {
16916 901 : case OMP_ORDERED:
16917 901 : if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
16918 531 : return *tp;
16919 : break;
16920 23 : case OMP_SIMD:
16921 23 : case OMP_PARALLEL:
16922 23 : case OMP_TARGET:
16923 23 : *walk_subtrees = 0;
16924 23 : break;
16925 : default:
16926 : break;
16927 : }
16928 : return NULL_TREE;
16929 : }
16930 :
16931 : /* Gimplify standalone loop transforming directive which has the
16932 : transformations applied already. So, all that is needed is gimplify
16933 : the remaining loops as normal loops. */
16934 :
16935 : static enum gimplify_status
16936 2264 : gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
16937 : {
16938 2264 : tree for_stmt = *expr_p;
16939 :
16940 2264 : if (OMP_FOR_PRE_BODY (for_stmt))
16941 759 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
16942 :
16943 2264 : gimple_seq pre_body = NULL, post_body = NULL;
16944 5203 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16945 : {
16946 2939 : if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
16947 1707 : continue;
16948 1283 : tree iters = NULL_TREE;
16949 1283 : if (i == 0
16950 826 : && TREE_CODE (for_stmt) == OMP_UNROLL
16951 1771 : && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
16952 : {
16953 311 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
16954 269 : iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
16955 : else
16956 42 : iters = build_int_cst (integer_type_node, 8);
16957 : }
16958 1283 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16959 1283 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16960 1283 : tree decl = TREE_OPERAND (t, 0);
16961 1283 : gcc_assert (DECL_P (decl));
16962 1283 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16963 : || POINTER_TYPE_P (TREE_TYPE (decl)));
16964 1283 : if (DECL_ARTIFICIAL (decl)
16965 876 : && TREE_PRIVATE (t)
16966 867 : && gimplify_omp_ctxp
16967 1613 : && gimplify_omp_ctxp->region_type != ORT_NONE)
16968 : {
16969 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16970 504 : do
16971 : {
16972 504 : splay_tree_node n
16973 504 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16974 504 : if (n != NULL)
16975 : break;
16976 346 : else if (ctx->region_type != ORT_WORKSHARE
16977 : && ctx->region_type != ORT_TASKGROUP
16978 62 : && ctx->region_type != ORT_SIMD
16979 38 : && ctx->region_type != ORT_ACC
16980 38 : && !(ctx->region_type & ORT_TARGET_DATA))
16981 : {
16982 38 : omp_add_variable (ctx, decl, GOVD_PRIVATE);
16983 38 : break;
16984 : }
16985 308 : ctx = ctx->outer_context;
16986 : }
16987 308 : while (ctx);
16988 : }
16989 1283 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16990 : {
16991 23 : gcc_assert (seen_error ());
16992 23 : continue;
16993 : }
16994 1260 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16995 : fb_rvalue);
16996 1260 : gimplify_and_add (t, &pre_body);
16997 1260 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16998 1260 : gcc_assert (TREE_OPERAND (t, 0) == decl);
16999 1260 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17000 : {
17001 28 : gcc_assert (seen_error ());
17002 28 : continue;
17003 : }
17004 1232 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
17005 : fb_rvalue);
17006 1232 : tree l1 = create_artificial_label (UNKNOWN_LOCATION);
17007 1232 : tree l2 = create_artificial_label (UNKNOWN_LOCATION);
17008 1232 : tree l3 = create_artificial_label (UNKNOWN_LOCATION);
17009 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
17010 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
17011 1232 : gimple_seq this_post_body = NULL;
17012 1232 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17013 1232 : if (TREE_CODE (t) == MODIFY_EXPR)
17014 : {
17015 975 : t = TREE_OPERAND (t, 1);
17016 975 : if (TREE_CODE (t) == PLUS_EXPR
17017 975 : && TREE_OPERAND (t, 1) == decl)
17018 : {
17019 0 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
17020 0 : TREE_OPERAND (t, 0) = decl;
17021 : }
17022 975 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
17023 : fb_rvalue);
17024 : }
17025 1232 : gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
17026 : &this_post_body);
17027 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
17028 1232 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17029 1232 : gcond *cond = NULL;
17030 1232 : tree d = decl;
17031 1232 : gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
17032 1232 : if (iters && tree_fits_uhwi_p (iters))
17033 : {
17034 299 : unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
17035 299 : if ((unsigned HOST_WIDE_INT) (int) niters == niters
17036 299 : && (int) niters > 0)
17037 : {
17038 299 : t = build2 (TREE_CODE (t), boolean_type_node, d,
17039 299 : TREE_OPERAND (t, 1));
17040 299 : t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
17041 : build_int_cst (integer_type_node,
17042 : annot_expr_unroll_kind),
17043 299 : build_int_cst (integer_type_node, niters));
17044 299 : gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
17045 : fb_rvalue);
17046 299 : cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
17047 : l1, l3);
17048 : }
17049 : }
17050 299 : if (cond == NULL)
17051 933 : cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
17052 : l1, l3);
17053 1232 : gimplify_seq_add_stmt (&this_post_body, cond);
17054 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
17055 1232 : gimplify_seq_add_seq (&this_post_body, post_body);
17056 1232 : post_body = this_post_body;
17057 : }
17058 2264 : gimplify_seq_add_seq (pre_p, pre_body);
17059 2264 : gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
17060 2264 : gimplify_seq_add_seq (pre_p, post_body);
17061 :
17062 2264 : *expr_p = NULL_TREE;
17063 2264 : return GS_ALL_DONE;
17064 : }
17065 :
17066 : /* Gimplify the gross structure of an OMP_FOR statement. */
17067 :
17068 : static enum gimplify_status
17069 58268 : gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
17070 : {
17071 58268 : tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
17072 58268 : enum gimplify_status ret = GS_ALL_DONE;
17073 58268 : enum gimplify_status tret;
17074 58268 : gomp_for *gfor;
17075 58268 : gimple_seq for_body, for_pre_body;
17076 58268 : int i;
17077 58268 : bitmap has_decl_expr = NULL;
17078 58268 : enum omp_region_type ort = ORT_WORKSHARE;
17079 58268 : bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
17080 :
17081 58268 : orig_for_stmt = for_stmt = *expr_p;
17082 :
17083 58268 : bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
17084 58268 : != NULL_TREE);
17085 58339 : while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
17086 : {
17087 17792 : tree *data[4] = { NULL, NULL, NULL, NULL };
17088 17792 : gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
17089 17792 : inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
17090 : find_combined_omp_for, data, NULL);
17091 17792 : if (inner_for_stmt == NULL_TREE)
17092 : {
17093 36 : gcc_assert (seen_error ());
17094 36 : *expr_p = NULL_TREE;
17095 3748 : return GS_ERROR;
17096 : }
17097 17756 : gcc_assert (inner_for_stmt == *data[3]);
17098 17756 : omp_maybe_apply_loop_xforms (data[3],
17099 17756 : data[2]
17100 4281 : ? OMP_FOR_CLAUSES (*data[2])
17101 13475 : : TREE_CODE (for_stmt) == OMP_FOR
17102 13475 : ? OMP_FOR_CLAUSES (for_stmt)
17103 : : NULL_TREE);
17104 17756 : if (inner_for_stmt != *data[3])
17105 71 : continue;
17106 17685 : if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
17107 : {
17108 2 : append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
17109 : &OMP_FOR_PRE_BODY (for_stmt));
17110 2 : OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
17111 : }
17112 17685 : if (OMP_FOR_PRE_BODY (inner_for_stmt))
17113 : {
17114 7231 : append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
17115 : &OMP_FOR_PRE_BODY (for_stmt));
17116 7231 : OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
17117 : }
17118 :
17119 17685 : if (data[0])
17120 : {
17121 : /* We have some statements or variable declarations in between
17122 : the composite construct directives. Move them around the
17123 : inner_for_stmt. */
17124 3712 : data[0] = expr_p;
17125 14848 : for (i = 0; i < 3; i++)
17126 11136 : if (data[i])
17127 : {
17128 7034 : tree t = *data[i];
17129 7034 : if (i < 2 && data[i + 1] == &OMP_BODY (t))
17130 1005 : data[i + 1] = data[i];
17131 7034 : *data[i] = OMP_BODY (t);
17132 7034 : tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
17133 : NULL_TREE, make_node (BLOCK));
17134 7034 : OMP_BODY (t) = body;
17135 7034 : append_to_statement_list_force (inner_for_stmt,
17136 : &BIND_EXPR_BODY (body));
17137 7034 : *data[3] = t;
17138 7034 : data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
17139 7034 : gcc_assert (*data[3] == inner_for_stmt);
17140 : }
17141 : return GS_OK;
17142 : }
17143 :
17144 36828 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
17145 22855 : if (!loop_p
17146 21974 : && OMP_FOR_ORIG_DECLS (inner_for_stmt)
17147 11134 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17148 : i)) == TREE_LIST
17149 22923 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17150 : i)))
17151 : {
17152 40 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
17153 : /* Class iterators aren't allowed on OMP_SIMD, so the only
17154 : case we need to solve is distribute parallel for. They are
17155 : allowed on the loop construct, but that is already handled
17156 : in gimplify_omp_loop. */
17157 40 : gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
17158 : && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
17159 : && data[1]);
17160 40 : tree orig_decl = TREE_PURPOSE (orig);
17161 40 : tree last = TREE_VALUE (orig);
17162 40 : tree *pc;
17163 40 : for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
17164 74 : *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
17165 42 : if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17166 35 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
17167 43 : && OMP_CLAUSE_DECL (*pc) == orig_decl)
17168 : break;
17169 40 : if (*pc == NULL_TREE)
17170 : {
17171 32 : tree *spc;
17172 32 : for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
17173 98 : *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
17174 67 : if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
17175 67 : && OMP_CLAUSE_DECL (*spc) == orig_decl)
17176 : break;
17177 32 : if (*spc)
17178 : {
17179 1 : tree c = *spc;
17180 1 : *spc = OMP_CLAUSE_CHAIN (c);
17181 1 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
17182 1 : *pc = c;
17183 : }
17184 : }
17185 40 : if (*pc == NULL_TREE)
17186 : ;
17187 9 : else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
17188 : {
17189 : /* private clause will appear only on inner_for_stmt.
17190 : Change it into firstprivate, and add private clause
17191 : on for_stmt. */
17192 8 : tree c = copy_node (*pc);
17193 8 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17194 8 : OMP_FOR_CLAUSES (for_stmt) = c;
17195 8 : OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
17196 8 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
17197 : }
17198 : else
17199 : {
17200 : /* lastprivate clause will appear on both inner_for_stmt
17201 : and for_stmt. Add firstprivate clause to
17202 : inner_for_stmt. */
17203 1 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
17204 : OMP_CLAUSE_FIRSTPRIVATE);
17205 1 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
17206 1 : OMP_CLAUSE_CHAIN (c) = *pc;
17207 1 : *pc = c;
17208 1 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
17209 : }
17210 40 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17211 : OMP_CLAUSE_FIRSTPRIVATE);
17212 40 : OMP_CLAUSE_DECL (c) = last;
17213 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17214 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17215 40 : c = build_omp_clause (UNKNOWN_LOCATION,
17216 40 : *pc ? OMP_CLAUSE_SHARED
17217 : : OMP_CLAUSE_FIRSTPRIVATE);
17218 40 : OMP_CLAUSE_DECL (c) = orig_decl;
17219 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17220 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17221 : }
17222 : /* Similarly, take care of C++ range for temporaries, those should
17223 : be firstprivate on OMP_PARALLEL if any. */
17224 13973 : if (data[1])
17225 17575 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
17226 11144 : if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
17227 5763 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17228 : i)) == TREE_LIST
17229 11220 : && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17230 : i)))
17231 : {
17232 49 : tree orig
17233 49 : = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
17234 49 : tree v = TREE_CHAIN (orig);
17235 49 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17236 : OMP_CLAUSE_FIRSTPRIVATE);
17237 : /* First add firstprivate clause for the __for_end artificial
17238 : decl. */
17239 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
17240 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17241 : == REFERENCE_TYPE)
17242 0 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17243 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17244 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17245 49 : if (TREE_VEC_ELT (v, 0))
17246 : {
17247 : /* And now the same for __for_range artificial decl if it
17248 : exists. */
17249 49 : c = build_omp_clause (UNKNOWN_LOCATION,
17250 : OMP_CLAUSE_FIRSTPRIVATE);
17251 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
17252 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17253 : == REFERENCE_TYPE)
17254 49 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17255 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17256 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17257 : }
17258 : }
17259 13973 : break;
17260 : }
17261 54520 : if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
17262 : {
17263 40547 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
17264 40547 : if (*expr_p != for_stmt)
17265 : return GS_OK;
17266 : }
17267 :
17268 53638 : switch (TREE_CODE (for_stmt))
17269 : {
17270 18284 : case OMP_FOR:
17271 30588 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17272 : {
17273 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17274 : OMP_CLAUSE_SCHEDULE))
17275 47 : error_at (EXPR_LOCATION (for_stmt),
17276 : "%qs clause may not appear on non-rectangular %qs",
17277 27 : "schedule", lang_GNU_Fortran () ? "do" : "for");
17278 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
17279 34 : error_at (EXPR_LOCATION (for_stmt),
17280 : "%qs clause may not appear on non-rectangular %qs",
17281 18 : "ordered", lang_GNU_Fortran () ? "do" : "for");
17282 : }
17283 : break;
17284 8287 : case OMP_DISTRIBUTE:
17285 9230 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
17286 8287 : && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17287 : OMP_CLAUSE_DIST_SCHEDULE))
17288 29 : error_at (EXPR_LOCATION (for_stmt),
17289 : "%qs clause may not appear on non-rectangular %qs",
17290 : "dist_schedule", "distribute");
17291 : break;
17292 : case OACC_LOOP:
17293 : ort = ORT_ACC;
17294 : break;
17295 1586 : case OMP_TASKLOOP:
17296 2523 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17297 : {
17298 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17299 : OMP_CLAUSE_GRAINSIZE))
17300 11 : error_at (EXPR_LOCATION (for_stmt),
17301 : "%qs clause may not appear on non-rectangular %qs",
17302 : "grainsize", "taskloop");
17303 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17304 : OMP_CLAUSE_NUM_TASKS))
17305 6 : error_at (EXPR_LOCATION (for_stmt),
17306 : "%qs clause may not appear on non-rectangular %qs",
17307 : "num_tasks", "taskloop");
17308 : }
17309 1586 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
17310 : ort = ORT_UNTIED_TASKLOOP;
17311 : else
17312 : ort = ORT_TASKLOOP;
17313 : break;
17314 10665 : case OMP_SIMD:
17315 10665 : ort = ORT_SIMD;
17316 10665 : break;
17317 2264 : case OMP_TILE:
17318 2264 : case OMP_UNROLL:
17319 2264 : gcc_assert (inner_for_stmt == NULL_TREE);
17320 2264 : return gimplify_omp_loop_xform (expr_p, pre_p);
17321 0 : default:
17322 0 : gcc_unreachable ();
17323 : }
17324 :
17325 : /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
17326 : clause for the IV. */
17327 10712 : if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17328 : {
17329 7837 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
17330 7837 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17331 7837 : decl = TREE_OPERAND (t, 0);
17332 18655 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
17333 11936 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17334 11936 : && OMP_CLAUSE_DECL (c) == decl)
17335 : {
17336 1118 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17337 1118 : break;
17338 : }
17339 : }
17340 :
17341 51374 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
17342 51698 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
17343 1910 : loop_p && TREE_CODE (for_stmt) != OMP_SIMD
17344 : ? OMP_LOOP : TREE_CODE (for_stmt));
17345 :
17346 51374 : if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
17347 8287 : gimplify_omp_ctxp->distribute = true;
17348 :
17349 : /* Handle OMP_FOR_INIT. */
17350 51374 : for_pre_body = NULL;
17351 51374 : if ((ort == ORT_SIMD
17352 40709 : || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
17353 62127 : && OMP_FOR_PRE_BODY (for_stmt))
17354 : {
17355 6792 : has_decl_expr = BITMAP_ALLOC (NULL);
17356 6792 : if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
17357 6792 : && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
17358 : {
17359 18 : t = OMP_FOR_PRE_BODY (for_stmt);
17360 18 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17361 : }
17362 6774 : else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
17363 : {
17364 6772 : tree_stmt_iterator si;
17365 11250 : for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
17366 4478 : tsi_next (&si))
17367 : {
17368 4478 : t = tsi_stmt (si);
17369 4478 : if (TREE_CODE (t) == DECL_EXPR
17370 4478 : && VAR_P (DECL_EXPR_DECL (t)))
17371 4405 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17372 : }
17373 : }
17374 : }
17375 51374 : if (OMP_FOR_PRE_BODY (for_stmt))
17376 : {
17377 21077 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
17378 20352 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17379 : else
17380 : {
17381 725 : struct gimplify_omp_ctx ctx;
17382 725 : memset (&ctx, 0, sizeof (ctx));
17383 725 : ctx.region_type = ORT_NONE;
17384 725 : gimplify_omp_ctxp = &ctx;
17385 725 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17386 725 : gimplify_omp_ctxp = NULL;
17387 : }
17388 : }
17389 51374 : OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
17390 :
17391 51374 : if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
17392 13973 : for_stmt = inner_for_stmt;
17393 :
17394 : /* For taskloop, need to gimplify the start, end and step before the
17395 : taskloop, outside of the taskloop omp context. */
17396 51374 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17397 : {
17398 3458 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17399 : {
17400 1872 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17401 1872 : gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
17402 1872 : ? pre_p : &for_pre_body);
17403 1872 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
17404 1872 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17405 : {
17406 34 : tree v = TREE_OPERAND (t, 1);
17407 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17408 : for_pre_p, orig_for_stmt);
17409 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17410 : for_pre_p, orig_for_stmt);
17411 : }
17412 : else
17413 1838 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17414 : orig_for_stmt);
17415 :
17416 : /* Handle OMP_FOR_COND. */
17417 1872 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17418 1872 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17419 : {
17420 31 : tree v = TREE_OPERAND (t, 1);
17421 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17422 : for_pre_p, orig_for_stmt);
17423 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17424 : for_pre_p, orig_for_stmt);
17425 : }
17426 : else
17427 1841 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17428 : orig_for_stmt);
17429 :
17430 : /* Handle OMP_FOR_INCR. */
17431 1872 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17432 1872 : if (TREE_CODE (t) == MODIFY_EXPR)
17433 : {
17434 590 : decl = TREE_OPERAND (t, 0);
17435 590 : t = TREE_OPERAND (t, 1);
17436 590 : tree *tp = &TREE_OPERAND (t, 1);
17437 590 : if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
17438 22 : tp = &TREE_OPERAND (t, 0);
17439 :
17440 590 : gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
17441 : orig_for_stmt);
17442 : }
17443 : }
17444 :
17445 1586 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
17446 : OMP_TASKLOOP);
17447 : }
17448 :
17449 51374 : if (orig_for_stmt != for_stmt)
17450 13973 : gimplify_omp_ctxp->combined_loop = true;
17451 :
17452 51374 : for_body = NULL;
17453 51374 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17454 : == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
17455 51374 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17456 : == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
17457 :
17458 51374 : tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
17459 51374 : bool is_doacross = false;
17460 51374 : if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
17461 : find_standalone_omp_ordered, NULL))
17462 : {
17463 531 : OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
17464 531 : is_doacross = true;
17465 531 : int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
17466 531 : gimplify_omp_ctxp->loop_iter_var.create (len * 2);
17467 2200 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
17468 1669 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
17469 : {
17470 10 : error_at (OMP_CLAUSE_LOCATION (*pc),
17471 : "%<linear%> clause may not be specified together "
17472 : "with %<ordered%> clause if stand-alone %<ordered%> "
17473 : "construct is nested in it");
17474 10 : *pc = OMP_CLAUSE_CHAIN (*pc);
17475 : }
17476 : else
17477 1659 : pc = &OMP_CLAUSE_CHAIN (*pc);
17478 : }
17479 51374 : int collapse = 1, tile = 0;
17480 51374 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
17481 51374 : if (c)
17482 13727 : collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
17483 51374 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
17484 51374 : if (c)
17485 384 : tile = list_length (OMP_CLAUSE_TILE_LIST (c));
17486 51374 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
17487 51374 : hash_set<tree> *allocate_uids = NULL;
17488 51374 : if (c)
17489 : {
17490 275 : allocate_uids = new hash_set<tree>;
17491 1682 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17492 1132 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
17493 478 : allocate_uids->add (OMP_CLAUSE_DECL (c));
17494 : }
17495 124494 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17496 : {
17497 73120 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17498 73120 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17499 73120 : decl = TREE_OPERAND (t, 0);
17500 73120 : gcc_assert (DECL_P (decl));
17501 73120 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
17502 : || POINTER_TYPE_P (TREE_TYPE (decl)));
17503 73120 : if (is_doacross)
17504 : {
17505 1654 : if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
17506 : {
17507 960 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17508 960 : if (TREE_CODE (orig_decl) == TREE_LIST)
17509 : {
17510 15 : orig_decl = TREE_PURPOSE (orig_decl);
17511 15 : if (!orig_decl)
17512 0 : orig_decl = decl;
17513 : }
17514 960 : gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
17515 : }
17516 : else
17517 694 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17518 1654 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17519 : }
17520 :
17521 73120 : if (for_stmt == orig_for_stmt)
17522 : {
17523 50265 : tree orig_decl = decl;
17524 50265 : if (OMP_FOR_ORIG_DECLS (for_stmt))
17525 : {
17526 24012 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17527 24012 : if (TREE_CODE (orig_decl) == TREE_LIST)
17528 : {
17529 : orig_decl = TREE_PURPOSE (orig_decl);
17530 : if (!orig_decl)
17531 : orig_decl = decl;
17532 : }
17533 : }
17534 50265 : if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
17535 36 : error_at (EXPR_LOCATION (for_stmt),
17536 : "threadprivate iteration variable %qD", orig_decl);
17537 : }
17538 :
17539 : /* Make sure the iteration variable is private. */
17540 73120 : tree c = NULL_TREE;
17541 73120 : tree c2 = NULL_TREE;
17542 73120 : if (orig_for_stmt != for_stmt)
17543 : {
17544 : /* Preserve this information until we gimplify the inner simd. */
17545 22855 : if (has_decl_expr
17546 22855 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17547 3421 : TREE_PRIVATE (t) = 1;
17548 : }
17549 50265 : else if (ort == ORT_SIMD)
17550 : {
17551 15738 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17552 : (splay_tree_key) decl);
17553 15738 : omp_is_private (gimplify_omp_ctxp, decl,
17554 15738 : 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17555 : != 1));
17556 15738 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
17557 : {
17558 2522 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17559 2522 : if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
17560 0 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17561 : OMP_CLAUSE_LASTPRIVATE);
17562 0 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17563 : OMP_CLAUSE_LASTPRIVATE))
17564 0 : if (OMP_CLAUSE_DECL (c3) == decl)
17565 : {
17566 0 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17567 : "conditional %<lastprivate%> on loop "
17568 : "iterator %qD ignored", decl);
17569 0 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17570 0 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17571 : }
17572 : }
17573 13216 : else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
17574 : {
17575 5645 : c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17576 5645 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17577 5645 : unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
17578 5645 : if ((has_decl_expr
17579 1309 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17580 6329 : || TREE_PRIVATE (t))
17581 : {
17582 2289 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17583 2289 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17584 : }
17585 5645 : struct gimplify_omp_ctx *outer
17586 5645 : = gimplify_omp_ctxp->outer_context;
17587 5645 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17588 : {
17589 2433 : if (outer->region_type == ORT_WORKSHARE
17590 2193 : && outer->combined_loop)
17591 : {
17592 2142 : n = splay_tree_lookup (outer->variables,
17593 : (splay_tree_key)decl);
17594 2142 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17595 : {
17596 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17597 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17598 : }
17599 : else
17600 : {
17601 2142 : struct gimplify_omp_ctx *octx = outer->outer_context;
17602 2142 : if (octx
17603 1714 : && octx->region_type == ORT_COMBINED_PARALLEL
17604 1476 : && octx->outer_context
17605 1209 : && (octx->outer_context->region_type
17606 : == ORT_WORKSHARE)
17607 1079 : && octx->outer_context->combined_loop)
17608 : {
17609 1079 : octx = octx->outer_context;
17610 1079 : n = splay_tree_lookup (octx->variables,
17611 : (splay_tree_key)decl);
17612 1079 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17613 : {
17614 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17615 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17616 : }
17617 : }
17618 : }
17619 : }
17620 : }
17621 :
17622 5645 : OMP_CLAUSE_DECL (c) = decl;
17623 5645 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17624 5645 : OMP_FOR_CLAUSES (for_stmt) = c;
17625 5645 : omp_add_variable (gimplify_omp_ctxp, decl, flags);
17626 5645 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17627 2433 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17628 : true);
17629 : }
17630 : else
17631 : {
17632 7571 : bool lastprivate
17633 : = (!has_decl_expr
17634 7571 : || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
17635 7571 : if (TREE_PRIVATE (t))
17636 1787 : lastprivate = false;
17637 7571 : if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
17638 : {
17639 509 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17640 509 : if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
17641 : lastprivate = false;
17642 : }
17643 :
17644 7571 : struct gimplify_omp_ctx *outer
17645 7571 : = gimplify_omp_ctxp->outer_context;
17646 7571 : if (outer && lastprivate)
17647 4887 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17648 : true);
17649 :
17650 9577 : c = build_omp_clause (input_location,
17651 : lastprivate ? OMP_CLAUSE_LASTPRIVATE
17652 : : OMP_CLAUSE_PRIVATE);
17653 7571 : OMP_CLAUSE_DECL (c) = decl;
17654 7571 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17655 7571 : OMP_FOR_CLAUSES (for_stmt) = c;
17656 7571 : omp_add_variable (gimplify_omp_ctxp, decl,
17657 : (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
17658 : | GOVD_EXPLICIT | GOVD_SEEN);
17659 7571 : c = NULL_TREE;
17660 : }
17661 : }
17662 34527 : else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
17663 : {
17664 8739 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17665 8739 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17666 : (splay_tree_key) decl);
17667 8739 : if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
17668 80 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17669 : OMP_CLAUSE_LASTPRIVATE);
17670 80 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17671 : OMP_CLAUSE_LASTPRIVATE))
17672 40 : if (OMP_CLAUSE_DECL (c3) == decl)
17673 : {
17674 40 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17675 : "conditional %<lastprivate%> on loop "
17676 : "iterator %qD ignored", decl);
17677 40 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17678 40 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17679 : }
17680 : }
17681 : else
17682 25788 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
17683 :
17684 : /* If DECL is not a gimple register, create a temporary variable to act
17685 : as an iteration counter. This is valid, since DECL cannot be
17686 : modified in the body of the loop. Similarly for any iteration vars
17687 : in simd with collapse > 1 where the iterator vars must be
17688 : lastprivate. And similarly for vars mentioned in allocate clauses. */
17689 73120 : if (orig_for_stmt != for_stmt)
17690 22855 : var = decl;
17691 50265 : else if (!is_gimple_reg (decl)
17692 46998 : || (ort == ORT_SIMD
17693 14137 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
17694 90193 : || (allocate_uids && allocate_uids->contains (decl)))
17695 : {
17696 10369 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17697 : /* Make sure omp_add_variable is not called on it prematurely.
17698 : We call it ourselves a few lines later. */
17699 10369 : gimplify_omp_ctxp = NULL;
17700 10369 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17701 10369 : gimplify_omp_ctxp = ctx;
17702 10369 : TREE_OPERAND (t, 0) = var;
17703 :
17704 10369 : gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
17705 :
17706 10369 : if (ort == ORT_SIMD
17707 10369 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17708 : {
17709 770 : c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17710 770 : OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
17711 770 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
17712 770 : OMP_CLAUSE_DECL (c2) = var;
17713 770 : OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
17714 770 : OMP_FOR_CLAUSES (for_stmt) = c2;
17715 770 : omp_add_variable (gimplify_omp_ctxp, var,
17716 : GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
17717 770 : if (c == NULL_TREE)
17718 : {
17719 518 : c = c2;
17720 518 : c2 = NULL_TREE;
17721 : }
17722 : }
17723 : else
17724 9599 : omp_add_variable (gimplify_omp_ctxp, var,
17725 : GOVD_PRIVATE | GOVD_SEEN);
17726 : }
17727 : else
17728 : var = decl;
17729 :
17730 73120 : gimplify_omp_ctxp->in_for_exprs = true;
17731 73120 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17732 : {
17733 719 : tree lb = TREE_OPERAND (t, 1);
17734 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
17735 : is_gimple_val, fb_rvalue, false);
17736 719 : ret = MIN (ret, tret);
17737 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
17738 : is_gimple_val, fb_rvalue, false);
17739 : }
17740 : else
17741 72401 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17742 : is_gimple_val, fb_rvalue, false);
17743 73120 : gimplify_omp_ctxp->in_for_exprs = false;
17744 73120 : ret = MIN (ret, tret);
17745 73120 : if (ret == GS_ERROR)
17746 : return ret;
17747 :
17748 : /* Handle OMP_FOR_COND. */
17749 73120 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17750 73120 : gcc_assert (COMPARISON_CLASS_P (t));
17751 73120 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17752 :
17753 73120 : gimplify_omp_ctxp->in_for_exprs = true;
17754 73120 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17755 : {
17756 599 : tree ub = TREE_OPERAND (t, 1);
17757 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
17758 : is_gimple_val, fb_rvalue, false);
17759 599 : ret = MIN (ret, tret);
17760 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
17761 : is_gimple_val, fb_rvalue, false);
17762 : }
17763 : else
17764 72521 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17765 : is_gimple_val, fb_rvalue, false);
17766 73120 : gimplify_omp_ctxp->in_for_exprs = false;
17767 73120 : ret = MIN (ret, tret);
17768 :
17769 : /* Handle OMP_FOR_INCR. */
17770 73120 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17771 73120 : switch (TREE_CODE (t))
17772 : {
17773 35828 : case PREINCREMENT_EXPR:
17774 35828 : case POSTINCREMENT_EXPR:
17775 35828 : {
17776 35828 : tree decl = TREE_OPERAND (t, 0);
17777 : /* c_omp_for_incr_canonicalize_ptr() should have been
17778 : called to massage things appropriately. */
17779 35828 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17780 :
17781 35828 : if (orig_for_stmt != for_stmt)
17782 : break;
17783 25405 : t = build_int_cst (TREE_TYPE (decl), 1);
17784 25405 : if (c)
17785 4478 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17786 25405 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17787 25405 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17788 25405 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17789 25405 : break;
17790 : }
17791 :
17792 3830 : case PREDECREMENT_EXPR:
17793 3830 : case POSTDECREMENT_EXPR:
17794 : /* c_omp_for_incr_canonicalize_ptr() should have been
17795 : called to massage things appropriately. */
17796 3830 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17797 3830 : if (orig_for_stmt != for_stmt)
17798 : break;
17799 1992 : t = build_int_cst (TREE_TYPE (decl), -1);
17800 1992 : if (c)
17801 112 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17802 1992 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17803 1992 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17804 1992 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17805 1992 : break;
17806 :
17807 33462 : case MODIFY_EXPR:
17808 33462 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17809 33462 : TREE_OPERAND (t, 0) = var;
17810 :
17811 33462 : t = TREE_OPERAND (t, 1);
17812 33462 : switch (TREE_CODE (t))
17813 : {
17814 26530 : case PLUS_EXPR:
17815 26530 : if (TREE_OPERAND (t, 1) == decl)
17816 : {
17817 215 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
17818 215 : TREE_OPERAND (t, 0) = var;
17819 215 : break;
17820 : }
17821 :
17822 : /* Fallthru. */
17823 33247 : case MINUS_EXPR:
17824 33247 : case POINTER_PLUS_EXPR:
17825 33247 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17826 33247 : TREE_OPERAND (t, 0) = var;
17827 33247 : break;
17828 0 : default:
17829 0 : gcc_unreachable ();
17830 : }
17831 :
17832 33462 : gimplify_omp_ctxp->in_for_exprs = true;
17833 33462 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17834 : is_gimple_val, fb_rvalue, false);
17835 33462 : ret = MIN (ret, tret);
17836 33462 : if (c)
17837 : {
17838 1573 : tree step = TREE_OPERAND (t, 1);
17839 1573 : tree stept = TREE_TYPE (decl);
17840 1573 : if (POINTER_TYPE_P (stept))
17841 280 : stept = sizetype;
17842 1573 : step = fold_convert (stept, step);
17843 1573 : if (TREE_CODE (t) == MINUS_EXPR)
17844 295 : step = fold_build1 (NEGATE_EXPR, stept, step);
17845 1573 : OMP_CLAUSE_LINEAR_STEP (c) = step;
17846 1573 : if (step != TREE_OPERAND (t, 1))
17847 : {
17848 295 : tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
17849 : &for_pre_body, NULL,
17850 : is_gimple_val, fb_rvalue, false);
17851 295 : ret = MIN (ret, tret);
17852 : }
17853 : }
17854 33462 : gimplify_omp_ctxp->in_for_exprs = false;
17855 33462 : break;
17856 :
17857 0 : default:
17858 0 : gcc_unreachable ();
17859 : }
17860 :
17861 73120 : if (c2)
17862 : {
17863 252 : gcc_assert (c);
17864 252 : OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
17865 : }
17866 :
17867 73120 : if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
17868 : {
17869 86698 : for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
17870 65136 : if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17871 16218 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
17872 56127 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17873 2159 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
17874 1373 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
17875 75496 : && OMP_CLAUSE_DECL (c) == decl)
17876 : {
17877 7535 : if (is_doacross && (collapse == 1 || i >= collapse))
17878 : t = var;
17879 : else
17880 : {
17881 7462 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17882 7462 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17883 7462 : gcc_assert (TREE_OPERAND (t, 0) == var);
17884 7462 : t = TREE_OPERAND (t, 1);
17885 7462 : gcc_assert (TREE_CODE (t) == PLUS_EXPR
17886 : || TREE_CODE (t) == MINUS_EXPR
17887 : || TREE_CODE (t) == POINTER_PLUS_EXPR);
17888 7462 : gcc_assert (TREE_OPERAND (t, 0) == var);
17889 14822 : t = build2 (TREE_CODE (t), TREE_TYPE (decl),
17890 : is_doacross ? var : decl,
17891 7462 : TREE_OPERAND (t, 1));
17892 : }
17893 7535 : gimple_seq *seq;
17894 7535 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
17895 6939 : seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
17896 : else
17897 596 : seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
17898 7535 : push_gimplify_context ();
17899 7535 : gimplify_assign (decl, t, seq);
17900 7535 : gimple *bind = NULL;
17901 7535 : if (gimplify_ctxp->temps)
17902 : {
17903 2072 : bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
17904 2072 : *seq = NULL;
17905 2072 : gimplify_seq_add_stmt (seq, bind);
17906 : }
17907 7535 : pop_gimplify_context (bind);
17908 : }
17909 : }
17910 73120 : if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
17911 2609 : for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17912 : {
17913 1197 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17914 1197 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17915 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17916 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17917 336 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17918 1197 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17919 1197 : gcc_assert (COMPARISON_CLASS_P (t));
17920 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17921 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17922 343 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17923 : }
17924 : }
17925 :
17926 51374 : BITMAP_FREE (has_decl_expr);
17927 51649 : delete allocate_uids;
17928 :
17929 51374 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17930 49788 : || (loop_p && orig_for_stmt == for_stmt))
17931 : {
17932 2642 : push_gimplify_context ();
17933 2642 : if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
17934 : {
17935 1891 : OMP_FOR_BODY (orig_for_stmt)
17936 1891 : = build3 (BIND_EXPR, void_type_node, NULL,
17937 1891 : OMP_FOR_BODY (orig_for_stmt), NULL);
17938 1891 : TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
17939 : }
17940 : }
17941 :
17942 51374 : gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
17943 : &for_body);
17944 :
17945 51374 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17946 49788 : || (loop_p && orig_for_stmt == for_stmt))
17947 : {
17948 2642 : if (gimple_code (g) == GIMPLE_BIND)
17949 2642 : pop_gimplify_context (g);
17950 : else
17951 0 : pop_gimplify_context (NULL);
17952 : }
17953 :
17954 51374 : if (orig_for_stmt != for_stmt)
17955 36828 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17956 : {
17957 22855 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17958 22855 : decl = TREE_OPERAND (t, 0);
17959 22855 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17960 22855 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17961 772 : gimplify_omp_ctxp = ctx->outer_context;
17962 22855 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17963 22855 : gimplify_omp_ctxp = ctx;
17964 22855 : omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
17965 22855 : TREE_OPERAND (t, 0) = var;
17966 22855 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17967 22855 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17968 22855 : TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
17969 22855 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17970 791 : for (int j = i + 1;
17971 791 : j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17972 : {
17973 377 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17974 377 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17975 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17976 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17977 : {
17978 133 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17979 133 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17980 : }
17981 377 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17982 377 : gcc_assert (COMPARISON_CLASS_P (t));
17983 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17984 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17985 : {
17986 67 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17987 67 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17988 : }
17989 : }
17990 : }
17991 :
17992 51374 : gimplify_adjust_omp_clauses (pre_p, for_body,
17993 : &OMP_FOR_CLAUSES (orig_for_stmt),
17994 51374 : TREE_CODE (orig_for_stmt));
17995 :
17996 51374 : int kind;
17997 51374 : switch (TREE_CODE (orig_for_stmt))
17998 : {
17999 : case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
18000 10665 : case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
18001 8287 : case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
18002 1586 : case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
18003 12552 : case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
18004 0 : default:
18005 0 : gcc_unreachable ();
18006 : }
18007 51374 : if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
18008 : {
18009 1056 : gimplify_seq_add_seq (pre_p, for_pre_body);
18010 1056 : for_pre_body = NULL;
18011 : }
18012 51374 : gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
18013 51374 : TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
18014 : for_pre_body);
18015 51374 : if (orig_for_stmt != for_stmt)
18016 13973 : gimple_omp_for_set_combined_p (gfor, true);
18017 51374 : if (gimplify_omp_ctxp
18018 42701 : && (gimplify_omp_ctxp->combined_loop
18019 35159 : || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
18020 12347 : && gimplify_omp_ctxp->outer_context
18021 7895 : && gimplify_omp_ctxp->outer_context->combined_loop)))
18022 : {
18023 13973 : gimple_omp_for_set_combined_into_p (gfor, true);
18024 13973 : if (gimplify_omp_ctxp->combined_loop)
18025 7542 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
18026 : else
18027 6431 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
18028 : }
18029 :
18030 124494 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18031 : {
18032 73120 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
18033 73120 : gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
18034 73120 : gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
18035 73120 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
18036 73120 : gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
18037 73120 : gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
18038 73120 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
18039 73120 : gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
18040 : }
18041 :
18042 : /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
18043 : constructs with GIMPLE_OMP_TASK sandwiched in between them.
18044 : The outer taskloop stands for computing the number of iterations,
18045 : counts for collapsed loops and holding taskloop specific clauses.
18046 : The task construct stands for the effect of data sharing on the
18047 : explicit task it creates and the inner taskloop stands for expansion
18048 : of the static loop inside of the explicit task construct. */
18049 51374 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
18050 : {
18051 1586 : tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
18052 1586 : tree task_clauses = NULL_TREE;
18053 1586 : tree c = *gfor_clauses_ptr;
18054 1586 : tree *gtask_clauses_ptr = &task_clauses;
18055 1586 : tree outer_for_clauses = NULL_TREE;
18056 1586 : tree *gforo_clauses_ptr = &outer_for_clauses;
18057 1586 : bitmap lastprivate_uids = NULL;
18058 1586 : if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
18059 : {
18060 36 : c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
18061 36 : if (c)
18062 : {
18063 18 : lastprivate_uids = BITMAP_ALLOC (NULL);
18064 54 : for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
18065 : OMP_CLAUSE_LASTPRIVATE))
18066 18 : bitmap_set_bit (lastprivate_uids,
18067 18 : DECL_UID (OMP_CLAUSE_DECL (c)));
18068 : }
18069 36 : c = *gfor_clauses_ptr;
18070 : }
18071 12254 : for (; c; c = OMP_CLAUSE_CHAIN (c))
18072 10668 : switch (OMP_CLAUSE_CODE (c))
18073 : {
18074 : /* These clauses are allowed on task, move them there. */
18075 6230 : case OMP_CLAUSE_SHARED:
18076 6230 : case OMP_CLAUSE_FIRSTPRIVATE:
18077 6230 : case OMP_CLAUSE_DEFAULT:
18078 6230 : case OMP_CLAUSE_IF:
18079 6230 : case OMP_CLAUSE_UNTIED:
18080 6230 : case OMP_CLAUSE_FINAL:
18081 6230 : case OMP_CLAUSE_MERGEABLE:
18082 6230 : case OMP_CLAUSE_PRIORITY:
18083 6230 : case OMP_CLAUSE_REDUCTION:
18084 6230 : case OMP_CLAUSE_IN_REDUCTION:
18085 6230 : *gtask_clauses_ptr = c;
18086 6230 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18087 6230 : break;
18088 2068 : case OMP_CLAUSE_PRIVATE:
18089 2068 : if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
18090 : {
18091 : /* We want private on outer for and firstprivate
18092 : on task. */
18093 26 : *gtask_clauses_ptr
18094 26 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18095 : OMP_CLAUSE_FIRSTPRIVATE);
18096 26 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
18097 26 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
18098 : openacc);
18099 26 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
18100 26 : *gforo_clauses_ptr = c;
18101 26 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18102 : }
18103 : else
18104 : {
18105 2042 : *gtask_clauses_ptr = c;
18106 2042 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18107 : }
18108 : break;
18109 : /* These clauses go into outer taskloop clauses. */
18110 533 : case OMP_CLAUSE_GRAINSIZE:
18111 533 : case OMP_CLAUSE_NUM_TASKS:
18112 533 : case OMP_CLAUSE_NOGROUP:
18113 533 : *gforo_clauses_ptr = c;
18114 533 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18115 533 : break;
18116 : /* Collapse clause we duplicate on both taskloops. */
18117 584 : case OMP_CLAUSE_COLLAPSE:
18118 584 : *gfor_clauses_ptr = c;
18119 584 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18120 584 : *gforo_clauses_ptr = copy_node (c);
18121 584 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
18122 584 : break;
18123 : /* For lastprivate, keep the clause on inner taskloop, and add
18124 : a shared clause on task. If the same decl is also firstprivate,
18125 : add also firstprivate clause on the inner taskloop. */
18126 1191 : case OMP_CLAUSE_LASTPRIVATE:
18127 1191 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
18128 : {
18129 : /* For taskloop C++ lastprivate IVs, we want:
18130 : 1) private on outer taskloop
18131 : 2) firstprivate and shared on task
18132 : 3) lastprivate on inner taskloop */
18133 38 : *gtask_clauses_ptr
18134 38 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18135 : OMP_CLAUSE_FIRSTPRIVATE);
18136 38 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
18137 38 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
18138 : openacc);
18139 38 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
18140 38 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
18141 38 : *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18142 : OMP_CLAUSE_PRIVATE);
18143 38 : OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
18144 38 : OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
18145 38 : TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
18146 38 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
18147 : }
18148 1191 : *gfor_clauses_ptr = c;
18149 1191 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18150 1191 : *gtask_clauses_ptr
18151 1191 : = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
18152 1191 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
18153 1191 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
18154 312 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
18155 1191 : gtask_clauses_ptr
18156 1191 : = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
18157 1191 : break;
18158 : /* Allocate clause we duplicate on task and inner taskloop
18159 : if the decl is lastprivate, otherwise just put on task. */
18160 62 : case OMP_CLAUSE_ALLOCATE:
18161 62 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
18162 62 : && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
18163 : {
18164 : /* Additionally, put firstprivate clause on task
18165 : for the allocator if it is not constant. */
18166 34 : *gtask_clauses_ptr
18167 34 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18168 : OMP_CLAUSE_FIRSTPRIVATE);
18169 34 : OMP_CLAUSE_DECL (*gtask_clauses_ptr)
18170 34 : = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
18171 34 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
18172 : }
18173 62 : if (lastprivate_uids
18174 97 : && bitmap_bit_p (lastprivate_uids,
18175 35 : DECL_UID (OMP_CLAUSE_DECL (c))))
18176 : {
18177 17 : *gfor_clauses_ptr = c;
18178 17 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18179 17 : *gtask_clauses_ptr = copy_node (c);
18180 17 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
18181 : }
18182 : else
18183 : {
18184 45 : *gtask_clauses_ptr = c;
18185 45 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
18186 : }
18187 : break;
18188 0 : default:
18189 0 : gcc_unreachable ();
18190 : }
18191 1586 : *gfor_clauses_ptr = NULL_TREE;
18192 1586 : *gtask_clauses_ptr = NULL_TREE;
18193 1586 : *gforo_clauses_ptr = NULL_TREE;
18194 1586 : BITMAP_FREE (lastprivate_uids);
18195 1586 : gimple_set_location (gfor, input_location);
18196 1586 : g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
18197 1586 : g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
18198 : NULL_TREE, NULL_TREE, NULL_TREE);
18199 1586 : gimple_set_location (g, input_location);
18200 1586 : gimple_omp_task_set_taskloop_p (g, true);
18201 1586 : g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
18202 1586 : gomp_for *gforo
18203 1586 : = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
18204 : gimple_omp_for_collapse (gfor),
18205 : gimple_omp_for_pre_body (gfor));
18206 1586 : gimple_omp_for_set_pre_body (gfor, NULL);
18207 1586 : gimple_omp_for_set_combined_p (gforo, true);
18208 1586 : gimple_omp_for_set_combined_into_p (gfor, true);
18209 3458 : for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
18210 : {
18211 1872 : tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
18212 1872 : tree v = create_tmp_var (type);
18213 1872 : gimple_omp_for_set_index (gforo, i, v);
18214 1872 : t = unshare_expr (gimple_omp_for_initial (gfor, i));
18215 1872 : gimple_omp_for_set_initial (gforo, i, t);
18216 1872 : gimple_omp_for_set_cond (gforo, i,
18217 : gimple_omp_for_cond (gfor, i));
18218 1872 : t = unshare_expr (gimple_omp_for_final (gfor, i));
18219 1872 : gimple_omp_for_set_final (gforo, i, t);
18220 1872 : t = unshare_expr (gimple_omp_for_incr (gfor, i));
18221 1872 : gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
18222 1872 : TREE_OPERAND (t, 0) = v;
18223 1872 : gimple_omp_for_set_incr (gforo, i, t);
18224 1872 : t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18225 1872 : OMP_CLAUSE_DECL (t) = v;
18226 1872 : OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
18227 1872 : gimple_omp_for_set_clauses (gforo, t);
18228 1872 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
18229 : {
18230 90 : tree *p1 = NULL, *p2 = NULL;
18231 90 : t = gimple_omp_for_initial (gforo, i);
18232 90 : if (TREE_CODE (t) == TREE_VEC)
18233 34 : p1 = &TREE_VEC_ELT (t, 0);
18234 90 : t = gimple_omp_for_final (gforo, i);
18235 90 : if (TREE_CODE (t) == TREE_VEC)
18236 : {
18237 31 : if (p1)
18238 23 : p2 = &TREE_VEC_ELT (t, 0);
18239 : else
18240 8 : p1 = &TREE_VEC_ELT (t, 0);
18241 : }
18242 90 : if (p1)
18243 : {
18244 : int j;
18245 58 : for (j = 0; j < i; j++)
18246 58 : if (*p1 == gimple_omp_for_index (gfor, j))
18247 : {
18248 42 : *p1 = gimple_omp_for_index (gforo, j);
18249 42 : if (p2)
18250 23 : *p2 = *p1;
18251 : break;
18252 : }
18253 42 : gcc_assert (j < i);
18254 : }
18255 : }
18256 : }
18257 1586 : gimplify_seq_add_stmt (pre_p, gforo);
18258 : }
18259 : else
18260 49788 : gimplify_seq_add_stmt (pre_p, gfor);
18261 :
18262 51374 : if (TREE_CODE (orig_for_stmt) == OMP_FOR)
18263 : {
18264 18284 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
18265 18284 : unsigned lastprivate_conditional = 0;
18266 18284 : while (ctx
18267 18320 : && (ctx->region_type == ORT_TARGET_DATA
18268 14238 : || ctx->region_type == ORT_TASKGROUP))
18269 36 : ctx = ctx->outer_context;
18270 18284 : if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
18271 13978 : for (tree c = gimple_omp_for_clauses (gfor);
18272 63348 : c; c = OMP_CLAUSE_CHAIN (c))
18273 49370 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18274 49370 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18275 158 : ++lastprivate_conditional;
18276 13978 : if (lastprivate_conditional)
18277 : {
18278 118 : struct omp_for_data fd;
18279 118 : omp_extract_for_data (gfor, &fd, NULL);
18280 118 : tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
18281 118 : lastprivate_conditional);
18282 118 : tree var = create_tmp_var_raw (type);
18283 118 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
18284 118 : OMP_CLAUSE_DECL (c) = var;
18285 118 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18286 118 : gimple_omp_for_set_clauses (gfor, c);
18287 118 : omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
18288 : }
18289 : }
18290 33090 : else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
18291 : {
18292 10665 : unsigned lastprivate_conditional = 0;
18293 49192 : for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
18294 38527 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18295 38527 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18296 119 : ++lastprivate_conditional;
18297 10665 : if (lastprivate_conditional)
18298 : {
18299 91 : struct omp_for_data fd;
18300 91 : omp_extract_for_data (gfor, &fd, NULL);
18301 91 : tree type = unsigned_type_for (fd.iter_type);
18302 301 : while (lastprivate_conditional--)
18303 : {
18304 119 : tree c = build_omp_clause (UNKNOWN_LOCATION,
18305 : OMP_CLAUSE__CONDTEMP_);
18306 119 : OMP_CLAUSE_DECL (c) = create_tmp_var (type);
18307 119 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18308 119 : gimple_omp_for_set_clauses (gfor, c);
18309 : }
18310 : }
18311 : }
18312 :
18313 51374 : if (ret != GS_ALL_DONE)
18314 : return GS_ERROR;
18315 51374 : *expr_p = NULL_TREE;
18316 51374 : return GS_ALL_DONE;
18317 : }
18318 :
18319 : /* Helper for gimplify_omp_loop, called through walk_tree. */
18320 :
18321 : static tree
18322 294 : note_no_context_vars (tree *tp, int *, void *data)
18323 : {
18324 294 : if (VAR_P (*tp)
18325 56 : && DECL_CONTEXT (*tp) == NULL_TREE
18326 309 : && !is_global_var (*tp))
18327 : {
18328 15 : vec<tree> *d = (vec<tree> *) data;
18329 15 : d->safe_push (*tp);
18330 15 : DECL_CONTEXT (*tp) = current_function_decl;
18331 : }
18332 294 : return NULL_TREE;
18333 : }
18334 :
18335 : /* Gimplify the gross structure of an OMP_LOOP statement. */
18336 :
18337 : static enum gimplify_status
18338 1056 : gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
18339 : {
18340 1056 : tree for_stmt = *expr_p;
18341 1056 : tree clauses = OMP_FOR_CLAUSES (for_stmt);
18342 1056 : struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
18343 1056 : enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
18344 1056 : int i;
18345 :
18346 1056 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
18347 1056 : if (*expr_p != for_stmt)
18348 : return GS_OK;
18349 :
18350 : /* If order is not present, the behavior is as if order(concurrent)
18351 : appeared. */
18352 1056 : tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
18353 1056 : if (order == NULL_TREE)
18354 : {
18355 799 : order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
18356 799 : OMP_CLAUSE_CHAIN (order) = clauses;
18357 799 : OMP_FOR_CLAUSES (for_stmt) = clauses = order;
18358 : }
18359 :
18360 1056 : tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
18361 1056 : if (bind == NULL_TREE)
18362 : {
18363 574 : if (!flag_openmp) /* flag_openmp_simd */
18364 : ;
18365 557 : else if (octx && (octx->region_type & ORT_TEAMS) != 0)
18366 : kind = OMP_CLAUSE_BIND_TEAMS;
18367 327 : else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
18368 : kind = OMP_CLAUSE_BIND_PARALLEL;
18369 : else
18370 : {
18371 187 : for (; octx; octx = octx->outer_context)
18372 : {
18373 79 : if ((octx->region_type & ORT_ACC) != 0
18374 79 : || octx->region_type == ORT_NONE
18375 79 : || octx->region_type == ORT_IMPLICIT_TARGET)
18376 0 : continue;
18377 : break;
18378 : }
18379 187 : if (octx == NULL && !in_omp_construct)
18380 4 : error_at (EXPR_LOCATION (for_stmt),
18381 : "%<bind%> clause not specified on a %<loop%> "
18382 : "construct not nested inside another OpenMP construct");
18383 : }
18384 574 : bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
18385 574 : OMP_CLAUSE_CHAIN (bind) = clauses;
18386 574 : OMP_CLAUSE_BIND_KIND (bind) = kind;
18387 574 : OMP_FOR_CLAUSES (for_stmt) = bind;
18388 : }
18389 : else
18390 482 : switch (OMP_CLAUSE_BIND_KIND (bind))
18391 : {
18392 : case OMP_CLAUSE_BIND_THREAD:
18393 : break;
18394 166 : case OMP_CLAUSE_BIND_PARALLEL:
18395 166 : if (!flag_openmp) /* flag_openmp_simd */
18396 : {
18397 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18398 0 : break;
18399 : }
18400 240 : for (; octx; octx = octx->outer_context)
18401 78 : if (octx->region_type == ORT_SIMD
18402 78 : && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
18403 : {
18404 4 : error_at (EXPR_LOCATION (for_stmt),
18405 : "%<bind(parallel)%> on a %<loop%> construct nested "
18406 : "inside %<simd%> construct");
18407 4 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18408 4 : break;
18409 : }
18410 : kind = OMP_CLAUSE_BIND_PARALLEL;
18411 : break;
18412 162 : case OMP_CLAUSE_BIND_TEAMS:
18413 162 : if (!flag_openmp) /* flag_openmp_simd */
18414 : {
18415 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18416 0 : break;
18417 : }
18418 162 : if ((octx
18419 128 : && octx->region_type != ORT_IMPLICIT_TARGET
18420 111 : && octx->region_type != ORT_NONE
18421 111 : && (octx->region_type & ORT_TEAMS) == 0)
18422 110 : || in_omp_construct)
18423 : {
18424 64 : error_at (EXPR_LOCATION (for_stmt),
18425 : "%<bind(teams)%> on a %<loop%> region not strictly "
18426 : "nested inside of a %<teams%> region");
18427 64 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18428 64 : break;
18429 : }
18430 : kind = OMP_CLAUSE_BIND_TEAMS;
18431 : break;
18432 0 : default:
18433 0 : gcc_unreachable ();
18434 : }
18435 :
18436 4280 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
18437 3224 : switch (OMP_CLAUSE_CODE (*pc))
18438 : {
18439 343 : case OMP_CLAUSE_REDUCTION:
18440 343 : if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
18441 : {
18442 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18443 : "%<inscan%> %<reduction%> clause on "
18444 : "%qs construct", "loop");
18445 4 : OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
18446 : }
18447 343 : if (OMP_CLAUSE_REDUCTION_TASK (*pc))
18448 : {
18449 8 : error_at (OMP_CLAUSE_LOCATION (*pc),
18450 : "invalid %<task%> reduction modifier on construct "
18451 : "other than %<parallel%>, %qs or %<sections%>",
18452 4 : lang_GNU_Fortran () ? "do" : "for");
18453 4 : OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
18454 : }
18455 343 : pc = &OMP_CLAUSE_CHAIN (*pc);
18456 343 : break;
18457 : case OMP_CLAUSE_LASTPRIVATE:
18458 293 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18459 : {
18460 289 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
18461 289 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
18462 289 : if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
18463 : break;
18464 26 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18465 25 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18466 : i)) == TREE_LIST
18467 48 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18468 : i)))
18469 : {
18470 22 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18471 22 : if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
18472 : break;
18473 : }
18474 : }
18475 289 : if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
18476 : {
18477 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18478 : "%<lastprivate%> clause on a %<loop%> construct refers "
18479 : "to a variable %qD which is not the loop iterator",
18480 4 : OMP_CLAUSE_DECL (*pc));
18481 4 : *pc = OMP_CLAUSE_CHAIN (*pc);
18482 4 : break;
18483 : }
18484 285 : pc = &OMP_CLAUSE_CHAIN (*pc);
18485 285 : break;
18486 2592 : default:
18487 2592 : pc = &OMP_CLAUSE_CHAIN (*pc);
18488 2592 : break;
18489 : }
18490 :
18491 1056 : TREE_SET_CODE (for_stmt, OMP_SIMD);
18492 :
18493 1056 : int last;
18494 1056 : switch (kind)
18495 : {
18496 : case OMP_CLAUSE_BIND_THREAD: last = 0; break;
18497 414 : case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
18498 220 : case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
18499 : }
18500 1910 : for (int pass = 1; pass <= last; pass++)
18501 : {
18502 854 : if (pass == 2)
18503 : {
18504 220 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
18505 : make_node (BLOCK));
18506 220 : append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
18507 220 : *expr_p = make_node (OMP_PARALLEL);
18508 220 : TREE_TYPE (*expr_p) = void_type_node;
18509 220 : OMP_PARALLEL_BODY (*expr_p) = bind;
18510 220 : OMP_PARALLEL_COMBINED (*expr_p) = 1;
18511 220 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
18512 220 : tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
18513 672 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18514 232 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18515 232 : && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
18516 : == TREE_LIST))
18517 : {
18518 16 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18519 16 : if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
18520 : {
18521 13 : *pc = build_omp_clause (UNKNOWN_LOCATION,
18522 : OMP_CLAUSE_FIRSTPRIVATE);
18523 13 : OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
18524 13 : pc = &OMP_CLAUSE_CHAIN (*pc);
18525 : }
18526 : }
18527 : }
18528 854 : tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
18529 854 : tree *pc = &OMP_FOR_CLAUSES (t);
18530 854 : TREE_TYPE (t) = void_type_node;
18531 854 : OMP_FOR_BODY (t) = *expr_p;
18532 854 : SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
18533 3968 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
18534 3114 : switch (OMP_CLAUSE_CODE (c))
18535 : {
18536 1992 : case OMP_CLAUSE_BIND:
18537 1992 : case OMP_CLAUSE_ORDER:
18538 1992 : case OMP_CLAUSE_COLLAPSE:
18539 1992 : *pc = copy_node (c);
18540 1992 : pc = &OMP_CLAUSE_CHAIN (*pc);
18541 1992 : break;
18542 : case OMP_CLAUSE_PRIVATE:
18543 : case OMP_CLAUSE_FIRSTPRIVATE:
18544 : /* Only needed on innermost. */
18545 : break;
18546 351 : case OMP_CLAUSE_LASTPRIVATE:
18547 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
18548 : {
18549 7 : *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18550 : OMP_CLAUSE_FIRSTPRIVATE);
18551 7 : OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
18552 7 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18553 7 : pc = &OMP_CLAUSE_CHAIN (*pc);
18554 : }
18555 351 : *pc = copy_node (c);
18556 351 : OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
18557 351 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18558 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
18559 : {
18560 22 : if (pass != last)
18561 7 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
18562 : else
18563 15 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18564 22 : OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
18565 : }
18566 351 : pc = &OMP_CLAUSE_CHAIN (*pc);
18567 351 : break;
18568 463 : case OMP_CLAUSE_REDUCTION:
18569 463 : *pc = copy_node (c);
18570 463 : OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
18571 463 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18572 463 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
18573 : {
18574 15 : auto_vec<tree> no_context_vars;
18575 15 : int walk_subtrees = 0;
18576 15 : note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18577 : &walk_subtrees, &no_context_vars);
18578 15 : if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
18579 0 : note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
18580 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
18581 : note_no_context_vars,
18582 : &no_context_vars);
18583 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
18584 : note_no_context_vars,
18585 : &no_context_vars);
18586 :
18587 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
18588 15 : = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
18589 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18590 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
18591 0 : = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
18592 :
18593 15 : hash_map<tree, tree> decl_map;
18594 15 : decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
18595 15 : decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18596 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
18597 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18598 0 : decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
18599 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
18600 :
18601 15 : copy_body_data id;
18602 15 : memset (&id, 0, sizeof (id));
18603 15 : id.src_fn = current_function_decl;
18604 15 : id.dst_fn = current_function_decl;
18605 15 : id.src_cfun = cfun;
18606 15 : id.decl_map = &decl_map;
18607 15 : id.copy_decl = copy_decl_no_change;
18608 15 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
18609 15 : id.transform_new_cfg = true;
18610 15 : id.transform_return_to_modify = false;
18611 15 : id.eh_lp_nr = 0;
18612 15 : walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
18613 : &id, NULL);
18614 15 : walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
18615 : &id, NULL);
18616 :
18617 60 : for (tree d : no_context_vars)
18618 : {
18619 15 : DECL_CONTEXT (d) = NULL_TREE;
18620 15 : DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
18621 : }
18622 15 : }
18623 : else
18624 : {
18625 448 : OMP_CLAUSE_REDUCTION_INIT (*pc)
18626 448 : = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
18627 448 : OMP_CLAUSE_REDUCTION_MERGE (*pc)
18628 896 : = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
18629 : }
18630 463 : pc = &OMP_CLAUSE_CHAIN (*pc);
18631 463 : break;
18632 0 : default:
18633 0 : gcc_unreachable ();
18634 : }
18635 854 : *pc = NULL_TREE;
18636 854 : *expr_p = t;
18637 : }
18638 1056 : return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
18639 : }
18640 :
18641 :
18642 : /* Helper function of optimize_target_teams, find OMP_TEAMS inside
18643 : of OMP_TARGET's body. */
18644 :
18645 : static tree
18646 99001 : find_omp_teams (tree *tp, int *walk_subtrees, void *)
18647 : {
18648 99001 : *walk_subtrees = 0;
18649 99001 : switch (TREE_CODE (*tp))
18650 : {
18651 : case OMP_TEAMS:
18652 : return *tp;
18653 27367 : case BIND_EXPR:
18654 27367 : case STATEMENT_LIST:
18655 27367 : *walk_subtrees = 1;
18656 27367 : break;
18657 : default:
18658 : break;
18659 : }
18660 : return NULL_TREE;
18661 : }
18662 :
18663 : /* Helper function of optimize_target_teams, determine if the expression
18664 : can be computed safely before the target construct on the host. */
18665 :
18666 : static tree
18667 1341 : computable_teams_clause (tree *tp, int *walk_subtrees, void *)
18668 : {
18669 1861 : splay_tree_node n;
18670 :
18671 1861 : if (TYPE_P (*tp))
18672 : {
18673 0 : *walk_subtrees = 0;
18674 0 : return NULL_TREE;
18675 : }
18676 1861 : switch (TREE_CODE (*tp))
18677 : {
18678 858 : case VAR_DECL:
18679 858 : case PARM_DECL:
18680 858 : case RESULT_DECL:
18681 858 : *walk_subtrees = 0;
18682 858 : if (error_operand_p (*tp)
18683 858 : || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
18684 858 : || DECL_HAS_VALUE_EXPR_P (*tp)
18685 858 : || DECL_THREAD_LOCAL_P (*tp)
18686 858 : || TREE_SIDE_EFFECTS (*tp)
18687 1716 : || TREE_THIS_VOLATILE (*tp))
18688 0 : return *tp;
18689 858 : if (is_global_var (*tp)
18690 858 : && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
18691 0 : || lookup_attribute ("omp declare target link",
18692 0 : DECL_ATTRIBUTES (*tp))))
18693 16 : return *tp;
18694 842 : if (VAR_P (*tp)
18695 536 : && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
18696 0 : && !is_global_var (*tp)
18697 842 : && decl_function_context (*tp) == current_function_decl)
18698 0 : return *tp;
18699 1684 : n = splay_tree_lookup (gimplify_omp_ctxp->variables,
18700 842 : (splay_tree_key) *tp);
18701 842 : if (n == NULL)
18702 : {
18703 210 : if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
18704 : return NULL_TREE;
18705 0 : return *tp;
18706 : }
18707 632 : else if (n->value & GOVD_LOCAL)
18708 0 : return *tp;
18709 632 : else if (n->value & GOVD_FIRSTPRIVATE)
18710 : return NULL_TREE;
18711 112 : else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18712 : == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18713 : return NULL_TREE;
18714 96 : return *tp;
18715 76 : case INTEGER_CST:
18716 76 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18717 0 : return *tp;
18718 : return NULL_TREE;
18719 520 : case TARGET_EXPR:
18720 520 : if (TARGET_EXPR_INITIAL (*tp)
18721 520 : || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
18722 : return *tp;
18723 520 : return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
18724 520 : walk_subtrees, NULL);
18725 : /* Allow some reasonable subset of integral arithmetics. */
18726 193 : case PLUS_EXPR:
18727 193 : case MINUS_EXPR:
18728 193 : case MULT_EXPR:
18729 193 : case TRUNC_DIV_EXPR:
18730 193 : case CEIL_DIV_EXPR:
18731 193 : case FLOOR_DIV_EXPR:
18732 193 : case ROUND_DIV_EXPR:
18733 193 : case TRUNC_MOD_EXPR:
18734 193 : case CEIL_MOD_EXPR:
18735 193 : case FLOOR_MOD_EXPR:
18736 193 : case ROUND_MOD_EXPR:
18737 193 : case RDIV_EXPR:
18738 193 : case EXACT_DIV_EXPR:
18739 193 : case MIN_EXPR:
18740 193 : case MAX_EXPR:
18741 193 : case LSHIFT_EXPR:
18742 193 : case RSHIFT_EXPR:
18743 193 : case BIT_IOR_EXPR:
18744 193 : case BIT_XOR_EXPR:
18745 193 : case BIT_AND_EXPR:
18746 193 : case NEGATE_EXPR:
18747 193 : case ABS_EXPR:
18748 193 : case BIT_NOT_EXPR:
18749 193 : case NON_LVALUE_EXPR:
18750 193 : CASE_CONVERT:
18751 193 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18752 0 : return *tp;
18753 : return NULL_TREE;
18754 : /* And disallow anything else, except for comparisons. */
18755 214 : default:
18756 214 : if (COMPARISON_CLASS_P (*tp))
18757 0 : return NULL_TREE;
18758 : return *tp;
18759 : }
18760 : }
18761 :
18762 : /* Try to determine if the num_teams and/or thread_limit expressions
18763 : can have their values determined already before entering the
18764 : target construct.
18765 : INTEGER_CSTs trivially are,
18766 : integral decls that are firstprivate (explicitly or implicitly)
18767 : or explicitly map(always, to:) or map(always, tofrom:) on the target
18768 : region too, and expressions involving simple arithmetics on those
18769 : too, function calls are not ok, dereferencing something neither etc.
18770 : Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
18771 : EXPR based on what we find:
18772 : 0 stands for clause not specified at all, use implementation default
18773 : -1 stands for value that can't be determined easily before entering
18774 : the target construct.
18775 : -2 means that no explicit teams construct was specified
18776 : If teams construct is not present at all, use 1 for num_teams
18777 : and 0 for thread_limit (only one team is involved, and the thread
18778 : limit is implementation defined. */
18779 :
18780 : static void
18781 13538 : optimize_target_teams (tree target, gimple_seq *pre_p)
18782 : {
18783 13538 : tree body = OMP_BODY (target);
18784 13538 : tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
18785 13538 : tree num_teams_lower = NULL_TREE;
18786 13538 : tree num_teams_upper = integer_zero_node;
18787 13538 : tree thread_limit = integer_zero_node;
18788 13538 : location_t num_teams_loc = EXPR_LOCATION (target);
18789 13538 : location_t thread_limit_loc = EXPR_LOCATION (target);
18790 13538 : tree c, *p, expr;
18791 13538 : struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
18792 :
18793 13538 : if (teams == NULL_TREE)
18794 7409 : num_teams_upper = build_int_cst (integer_type_node, -2);
18795 : else
18796 10615 : for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
18797 : {
18798 4486 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
18799 : {
18800 625 : p = &num_teams_upper;
18801 625 : num_teams_loc = OMP_CLAUSE_LOCATION (c);
18802 625 : if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
18803 : {
18804 136 : expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
18805 136 : if (TREE_CODE (expr) == INTEGER_CST)
18806 18 : num_teams_lower = expr;
18807 118 : else if (walk_tree (&expr, computable_teams_clause,
18808 : NULL, NULL))
18809 12 : num_teams_lower = integer_minus_one_node;
18810 : else
18811 : {
18812 106 : num_teams_lower = expr;
18813 106 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18814 106 : if (gimplify_expr (&num_teams_lower, pre_p, NULL,
18815 : is_gimple_val, fb_rvalue, false)
18816 : == GS_ERROR)
18817 : {
18818 0 : gimplify_omp_ctxp = target_ctx;
18819 0 : num_teams_lower = integer_minus_one_node;
18820 : }
18821 : else
18822 : {
18823 106 : gimplify_omp_ctxp = target_ctx;
18824 106 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18825 28 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
18826 56 : = num_teams_lower;
18827 : }
18828 : }
18829 : }
18830 : }
18831 3861 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18832 : {
18833 479 : p = &thread_limit;
18834 479 : thread_limit_loc = OMP_CLAUSE_LOCATION (c);
18835 : }
18836 : else
18837 3382 : continue;
18838 1104 : expr = OMP_CLAUSE_OPERAND (c, 0);
18839 1104 : if (TREE_CODE (expr) == INTEGER_CST)
18840 : {
18841 150 : *p = expr;
18842 150 : continue;
18843 : }
18844 954 : if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
18845 : {
18846 314 : *p = integer_minus_one_node;
18847 314 : continue;
18848 : }
18849 640 : *p = expr;
18850 640 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18851 640 : if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
18852 : == GS_ERROR)
18853 : {
18854 0 : gimplify_omp_ctxp = target_ctx;
18855 0 : *p = integer_minus_one_node;
18856 0 : continue;
18857 : }
18858 640 : gimplify_omp_ctxp = target_ctx;
18859 640 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18860 48 : OMP_CLAUSE_OPERAND (c, 0) = *p;
18861 : }
18862 13538 : if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
18863 : {
18864 13258 : c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
18865 13258 : OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
18866 13258 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18867 13258 : OMP_TARGET_CLAUSES (target) = c;
18868 : }
18869 13538 : c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
18870 13538 : OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
18871 13538 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
18872 13538 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18873 13538 : OMP_TARGET_CLAUSES (target) = c;
18874 13538 : }
18875 :
18876 : /* Gimplify the gross structure of several OMP constructs. */
18877 :
18878 : static void
18879 40016 : gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
18880 : {
18881 40016 : tree expr = *expr_p;
18882 40016 : gimple *stmt;
18883 40016 : gimple_seq body = NULL;
18884 40016 : enum omp_region_type ort;
18885 :
18886 40016 : switch (TREE_CODE (expr))
18887 : {
18888 : case OMP_SECTIONS:
18889 : case OMP_SINGLE:
18890 : ort = ORT_WORKSHARE;
18891 : break;
18892 242 : case OMP_SCOPE:
18893 242 : ort = ORT_TASKGROUP;
18894 242 : break;
18895 13538 : case OMP_TARGET:
18896 13538 : ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
18897 : break;
18898 2543 : case OACC_KERNELS:
18899 2543 : ort = ORT_ACC_KERNELS;
18900 2543 : break;
18901 7901 : case OACC_PARALLEL:
18902 7901 : ort = ORT_ACC_PARALLEL;
18903 7901 : break;
18904 1040 : case OACC_SERIAL:
18905 1040 : ort = ORT_ACC_SERIAL;
18906 1040 : break;
18907 1950 : case OACC_DATA:
18908 1950 : ort = ORT_ACC_DATA;
18909 1950 : break;
18910 1875 : case OMP_TARGET_DATA:
18911 1875 : ort = ORT_TARGET_DATA;
18912 1875 : break;
18913 8892 : case OMP_TEAMS:
18914 8892 : ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
18915 8892 : if (gimplify_omp_ctxp == NULL
18916 6169 : || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
18917 2723 : ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
18918 : break;
18919 118 : case OACC_HOST_DATA:
18920 118 : ort = ORT_ACC_HOST_DATA;
18921 118 : break;
18922 0 : default:
18923 0 : gcc_unreachable ();
18924 : }
18925 :
18926 40016 : gimple_seq iterator_loops_seq = NULL;
18927 40016 : if (TREE_CODE (expr) == OMP_TARGET || TREE_CODE (expr) == OMP_TARGET_DATA)
18928 : {
18929 15413 : remove_unused_omp_iterator_vars (&OMP_CLAUSES (expr));
18930 15413 : build_omp_iterators_loops (&OMP_CLAUSES (expr), &iterator_loops_seq);
18931 : }
18932 :
18933 40016 : bool save_in_omp_construct = in_omp_construct;
18934 40016 : if ((ort & ORT_ACC) == 0)
18935 26464 : in_omp_construct = false;
18936 40016 : gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
18937 40016 : TREE_CODE (expr), &iterator_loops_seq);
18938 40016 : if (TREE_CODE (expr) == OMP_TARGET)
18939 13538 : optimize_target_teams (expr, pre_p);
18940 40016 : if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
18941 11051 : || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18942 : {
18943 31688 : push_gimplify_context ();
18944 31688 : gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
18945 31688 : if (gimple_code (g) == GIMPLE_BIND)
18946 31688 : pop_gimplify_context (g);
18947 : else
18948 0 : pop_gimplify_context (NULL);
18949 31688 : if ((ort & ORT_TARGET_DATA) != 0)
18950 : {
18951 3943 : enum built_in_function end_ix;
18952 3943 : switch (TREE_CODE (expr))
18953 : {
18954 : case OACC_DATA:
18955 : case OACC_HOST_DATA:
18956 : end_ix = BUILT_IN_GOACC_DATA_END;
18957 : break;
18958 1875 : case OMP_TARGET_DATA:
18959 1875 : end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
18960 1875 : break;
18961 0 : default:
18962 0 : gcc_unreachable ();
18963 : }
18964 3943 : tree fn = builtin_decl_explicit (end_ix);
18965 3943 : g = gimple_build_call (fn, 0);
18966 3943 : gimple_seq cleanup = NULL;
18967 3943 : gimple_seq_add_stmt (&cleanup, g);
18968 3943 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
18969 3943 : body = NULL;
18970 3943 : gimple_seq_add_stmt (&body, g);
18971 : }
18972 : }
18973 : else
18974 8328 : gimplify_and_add (OMP_BODY (expr), &body);
18975 40016 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
18976 40016 : TREE_CODE (expr), &iterator_loops_seq);
18977 40016 : in_omp_construct = save_in_omp_construct;
18978 :
18979 40016 : switch (TREE_CODE (expr))
18980 : {
18981 1950 : case OACC_DATA:
18982 3900 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
18983 1950 : OMP_CLAUSES (expr));
18984 1950 : break;
18985 118 : case OACC_HOST_DATA:
18986 118 : if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
18987 : {
18988 128 : for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18989 95 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
18990 57 : OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
18991 : }
18992 :
18993 236 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
18994 118 : OMP_CLAUSES (expr));
18995 118 : break;
18996 2543 : case OACC_KERNELS:
18997 5086 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
18998 2543 : OMP_CLAUSES (expr));
18999 2543 : break;
19000 7901 : case OACC_PARALLEL:
19001 15802 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
19002 7901 : OMP_CLAUSES (expr));
19003 7901 : break;
19004 1040 : case OACC_SERIAL:
19005 2080 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
19006 1040 : OMP_CLAUSES (expr));
19007 1040 : break;
19008 634 : case OMP_SECTIONS:
19009 634 : stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
19010 634 : break;
19011 1283 : case OMP_SINGLE:
19012 1283 : stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
19013 1283 : break;
19014 242 : case OMP_SCOPE:
19015 242 : stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
19016 242 : break;
19017 13538 : case OMP_TARGET:
19018 : /* Handle 'device_type(nohost)'. */
19019 13538 : if (tree c = omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_DEVICE_TYPE))
19020 20 : if (OMP_CLAUSE_DEVICE_TYPE_KIND (c) == OMP_CLAUSE_DEVICE_TYPE_NOHOST)
19021 : {
19022 9 : location_t loc = gimple_location (body);
19023 9 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
19024 9 : fn = build_call_expr_loc (loc, fn, 0);
19025 9 : tree err = builtin_decl_explicit (BUILT_IN_GOMP_ERROR);
19026 9 : const char *str = "Executing device-type 'nohost' target region "
19027 : "on the host";
19028 9 : tree msg = build_string_literal (strlen (str) + 1, str);
19029 9 : tree msglen = build_int_cst (size_type_node, -2);
19030 9 : err = build_call_expr_loc (loc, err, 2, msg, msglen);
19031 9 : tree l1 = create_artificial_label (UNKNOWN_LOCATION);
19032 9 : tree l2 = create_artificial_label (UNKNOWN_LOCATION);
19033 9 : tree l3 = create_artificial_label (UNKNOWN_LOCATION);
19034 :
19035 9 : gimple_seq new_body = NULL;
19036 :
19037 9 : gimplify_expr (&fn, &new_body, NULL, is_gimple_val, fb_rvalue);
19038 9 : gcond *cond = gimple_build_cond (NE_EXPR, fn,
19039 9 : build_int_cst (TREE_TYPE (fn), 0),
19040 : l1, l2);
19041 9 : gimplify_seq_add_stmt (&new_body, cond);
19042 9 : gimplify_seq_add_stmt (&new_body, gimple_build_label (l1));
19043 9 : gimplify_and_add (err, &new_body);
19044 9 : gimplify_seq_add_stmt (&new_body, gimple_build_goto (l3));
19045 9 : gimplify_seq_add_stmt (&new_body, gimple_build_label (l2));
19046 9 : gimple_seq_add_seq (&new_body, body);
19047 9 : gimplify_seq_add_stmt (&new_body, gimple_build_label (l3));
19048 9 : body = gimple_build_bind (NULL_TREE, new_body, make_node (BLOCK));
19049 9 : gimple_set_location (body, loc);
19050 : }
19051 27076 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
19052 13538 : OMP_CLAUSES (expr), iterator_loops_seq);
19053 13538 : break;
19054 1875 : case OMP_TARGET_DATA:
19055 : /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
19056 : to be evaluated before the use_device_{ptr,addr} clauses if they
19057 : refer to the same variables. */
19058 1875 : {
19059 1875 : tree use_device_clauses;
19060 1875 : tree *pc, *uc = &use_device_clauses;
19061 9974 : for (pc = &OMP_CLAUSES (expr); *pc; )
19062 8099 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
19063 8099 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
19064 : {
19065 1959 : *uc = *pc;
19066 1959 : *pc = OMP_CLAUSE_CHAIN (*pc);
19067 1959 : uc = &OMP_CLAUSE_CHAIN (*uc);
19068 : }
19069 : else
19070 6140 : pc = &OMP_CLAUSE_CHAIN (*pc);
19071 1875 : *uc = NULL_TREE;
19072 1875 : *pc = use_device_clauses;
19073 1875 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
19074 1875 : OMP_CLAUSES (expr), iterator_loops_seq);
19075 : }
19076 1875 : break;
19077 8892 : case OMP_TEAMS:
19078 8892 : stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
19079 8892 : if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
19080 2723 : gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
19081 : break;
19082 0 : default:
19083 0 : gcc_unreachable ();
19084 : }
19085 :
19086 40016 : gimplify_seq_add_stmt (pre_p, stmt);
19087 40016 : *expr_p = NULL_TREE;
19088 40016 : }
19089 :
19090 : /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
19091 : target update constructs. */
19092 :
19093 : static void
19094 12370 : gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
19095 : {
19096 12370 : tree expr = *expr_p;
19097 12370 : int kind;
19098 12370 : gomp_target *stmt;
19099 12370 : enum omp_region_type ort = ORT_WORKSHARE;
19100 :
19101 12370 : switch (TREE_CODE (expr))
19102 : {
19103 : case OACC_ENTER_DATA:
19104 : kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
19105 : ort = ORT_ACC;
19106 : break;
19107 : case OACC_EXIT_DATA:
19108 : kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
19109 : ort = ORT_ACC;
19110 : break;
19111 : case OACC_UPDATE:
19112 : kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
19113 : ort = ORT_ACC;
19114 : break;
19115 : case OMP_TARGET_UPDATE:
19116 : kind = GF_OMP_TARGET_KIND_UPDATE;
19117 : break;
19118 : case OMP_TARGET_ENTER_DATA:
19119 : kind = GF_OMP_TARGET_KIND_ENTER_DATA;
19120 : break;
19121 : case OMP_TARGET_EXIT_DATA:
19122 : kind = GF_OMP_TARGET_KIND_EXIT_DATA;
19123 : break;
19124 0 : default:
19125 0 : gcc_unreachable ();
19126 : }
19127 :
19128 12370 : gimple_seq iterator_loops_seq = NULL;
19129 12370 : remove_unused_omp_iterator_vars (&OMP_STANDALONE_CLAUSES (expr));
19130 12370 : build_omp_iterators_loops (&OMP_STANDALONE_CLAUSES (expr),
19131 : &iterator_loops_seq);
19132 :
19133 12370 : gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
19134 12370 : ort, TREE_CODE (expr), &iterator_loops_seq);
19135 12370 : gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
19136 12370 : TREE_CODE (expr), &iterator_loops_seq);
19137 12370 : if (TREE_CODE (expr) == OACC_UPDATE
19138 12370 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
19139 : OMP_CLAUSE_IF_PRESENT))
19140 : {
19141 : /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
19142 : clause. */
19143 123 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
19144 97 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
19145 47 : switch (OMP_CLAUSE_MAP_KIND (c))
19146 : {
19147 14 : case GOMP_MAP_FORCE_TO:
19148 14 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
19149 14 : break;
19150 24 : case GOMP_MAP_FORCE_FROM:
19151 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
19152 24 : break;
19153 : default:
19154 : break;
19155 : }
19156 : }
19157 12344 : else if (TREE_CODE (expr) == OACC_EXIT_DATA
19158 12344 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
19159 : OMP_CLAUSE_FINALIZE))
19160 : {
19161 : /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
19162 : semantics. */
19163 75 : bool have_clause = false;
19164 275 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
19165 200 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
19166 117 : switch (OMP_CLAUSE_MAP_KIND (c))
19167 : {
19168 45 : case GOMP_MAP_FROM:
19169 45 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
19170 45 : have_clause = true;
19171 45 : break;
19172 47 : case GOMP_MAP_RELEASE:
19173 47 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
19174 47 : have_clause = true;
19175 47 : break;
19176 : case GOMP_MAP_TO_PSET:
19177 : /* Fortran arrays with descriptors must map that descriptor when
19178 : doing standalone "attach" operations (in OpenACC). In that
19179 : case GOMP_MAP_TO_PSET appears by itself with no preceding
19180 : clause (see trans-openmp.cc:gfc_trans_omp_clauses). */
19181 : break;
19182 2 : case GOMP_MAP_POINTER:
19183 : /* TODO PR92929: we may see these here, but they'll always follow
19184 : one of the clauses above, and will be handled by libgomp as
19185 : one group, so no handling required here. */
19186 2 : gcc_assert (have_clause);
19187 : break;
19188 22 : case GOMP_MAP_DETACH:
19189 22 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
19190 22 : have_clause = false;
19191 22 : break;
19192 : case GOMP_MAP_STRUCT:
19193 : case GOMP_MAP_STRUCT_UNORD:
19194 200 : have_clause = false;
19195 : break;
19196 0 : default:
19197 0 : gcc_unreachable ();
19198 : }
19199 : }
19200 12370 : stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr),
19201 : iterator_loops_seq);
19202 :
19203 12370 : gimplify_seq_add_stmt (pre_p, stmt);
19204 12370 : *expr_p = NULL_TREE;
19205 12370 : }
19206 :
19207 : /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
19208 : stabilized the lhs of the atomic operation as *ADDR. Return true if
19209 : EXPR is this stabilized form. */
19210 :
19211 : static bool
19212 36822 : goa_lhs_expr_p (tree expr, tree addr)
19213 : {
19214 : /* Also include casts to other type variants. The C front end is fond
19215 : of adding these for e.g. volatile variables. This is like
19216 : STRIP_TYPE_NOPS but includes the main variant lookup. */
19217 36822 : STRIP_USELESS_TYPE_CONVERSION (expr);
19218 :
19219 36822 : if (INDIRECT_REF_P (expr))
19220 : {
19221 5024 : expr = TREE_OPERAND (expr, 0);
19222 5024 : while (expr != addr
19223 92 : && (CONVERT_EXPR_P (expr)
19224 92 : || TREE_CODE (expr) == NON_LVALUE_EXPR)
19225 0 : && TREE_CODE (expr) == TREE_CODE (addr)
19226 5024 : && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
19227 : {
19228 0 : expr = TREE_OPERAND (expr, 0);
19229 0 : addr = TREE_OPERAND (addr, 0);
19230 : }
19231 5024 : if (expr == addr)
19232 : return true;
19233 92 : return (TREE_CODE (addr) == ADDR_EXPR
19234 62 : && TREE_CODE (expr) == ADDR_EXPR
19235 92 : && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
19236 : }
19237 31798 : if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
19238 : return true;
19239 : return false;
19240 : }
19241 :
19242 : /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
19243 : expression does not involve the lhs, evaluate it into a temporary.
19244 : Return 1 if the lhs appeared as a subexpression, 0 if it did not,
19245 : or -1 if an error was encountered. */
19246 :
19247 : static int
19248 36822 : goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
19249 : tree lhs_var, tree &target_expr, bool rhs, int depth)
19250 : {
19251 36822 : tree expr = *expr_p;
19252 36822 : int saw_lhs = 0;
19253 :
19254 36822 : if (goa_lhs_expr_p (expr, lhs_addr))
19255 : {
19256 9196 : if (pre_p)
19257 8538 : *expr_p = lhs_var;
19258 : return 1;
19259 : }
19260 27626 : if (is_gimple_val (expr))
19261 : return 0;
19262 :
19263 : /* Maximum depth of lhs in expression is for the
19264 : __builtin_clear_padding (...), __builtin_clear_padding (...),
19265 : __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs; */
19266 17292 : if (++depth > 7)
19267 16 : goto finish;
19268 :
19269 17276 : switch (TREE_CODE_CLASS (TREE_CODE (expr)))
19270 : {
19271 9663 : case tcc_binary:
19272 9663 : case tcc_comparison:
19273 9663 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
19274 : lhs_var, target_expr, true, depth);
19275 : /* FALLTHRU */
19276 11706 : case tcc_unary:
19277 11706 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
19278 : lhs_var, target_expr, true, depth);
19279 11706 : break;
19280 2982 : case tcc_expression:
19281 2982 : switch (TREE_CODE (expr))
19282 : {
19283 836 : case TRUTH_ANDIF_EXPR:
19284 836 : case TRUTH_ORIF_EXPR:
19285 836 : case TRUTH_AND_EXPR:
19286 836 : case TRUTH_OR_EXPR:
19287 836 : case TRUTH_XOR_EXPR:
19288 836 : case BIT_INSERT_EXPR:
19289 836 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19290 : lhs_addr, lhs_var, target_expr, true,
19291 : depth);
19292 : /* FALLTHRU */
19293 888 : case TRUTH_NOT_EXPR:
19294 888 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19295 : lhs_addr, lhs_var, target_expr, true,
19296 : depth);
19297 888 : break;
19298 550 : case MODIFY_EXPR:
19299 550 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19300 : target_expr, true, depth))
19301 : break;
19302 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19303 : lhs_addr, lhs_var, target_expr, true,
19304 : depth);
19305 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19306 : lhs_addr, lhs_var, target_expr, false,
19307 : depth);
19308 544 : break;
19309 : /* FALLTHRU */
19310 164 : case ADDR_EXPR:
19311 164 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19312 : target_expr, true, depth))
19313 : break;
19314 138 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19315 : lhs_addr, lhs_var, target_expr, false,
19316 : depth);
19317 138 : break;
19318 : case COMPOUND_EXPR:
19319 : /* Break out any preevaluations from cp_build_modify_expr. */
19320 76 : for (; TREE_CODE (expr) == COMPOUND_EXPR;
19321 38 : expr = TREE_OPERAND (expr, 1))
19322 : {
19323 : /* Special-case __builtin_clear_padding call before
19324 : __builtin_memcmp. */
19325 38 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
19326 : {
19327 2 : tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
19328 2 : if (fndecl
19329 2 : && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
19330 0 : && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
19331 2 : && (!pre_p
19332 0 : || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
19333 : lhs_addr, lhs_var,
19334 : target_expr, true, depth)))
19335 : {
19336 0 : if (pre_p)
19337 0 : *expr_p = expr;
19338 0 : saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
19339 : pre_p, lhs_addr, lhs_var,
19340 : target_expr, true, depth);
19341 0 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
19342 : pre_p, lhs_addr, lhs_var,
19343 : target_expr, rhs, depth);
19344 0 : return saw_lhs;
19345 : }
19346 : }
19347 :
19348 38 : if (pre_p)
19349 35 : gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
19350 : }
19351 38 : if (!pre_p)
19352 3 : return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
19353 3 : target_expr, rhs, depth);
19354 35 : *expr_p = expr;
19355 35 : return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
19356 35 : target_expr, rhs, depth);
19357 441 : case COND_EXPR:
19358 441 : if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
19359 : lhs_var, target_expr, true, depth))
19360 : break;
19361 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19362 : lhs_addr, lhs_var, target_expr, true,
19363 : depth);
19364 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19365 : lhs_addr, lhs_var, target_expr, true,
19366 : depth);
19367 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
19368 : lhs_addr, lhs_var, target_expr, true,
19369 : depth);
19370 428 : break;
19371 880 : case TARGET_EXPR:
19372 880 : if (TARGET_EXPR_INITIAL (expr))
19373 : {
19374 880 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
19375 : lhs_var, target_expr, true,
19376 : depth))
19377 : break;
19378 702 : if (expr == target_expr)
19379 : saw_lhs = 1;
19380 : else
19381 : {
19382 702 : saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
19383 : pre_p, lhs_addr, lhs_var,
19384 : target_expr, true, depth);
19385 702 : if (saw_lhs && target_expr == NULL_TREE && pre_p)
19386 26 : target_expr = expr;
19387 : }
19388 : }
19389 : break;
19390 : default:
19391 : break;
19392 : }
19393 : break;
19394 565 : case tcc_reference:
19395 565 : if (TREE_CODE (expr) == BIT_FIELD_REF
19396 499 : || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
19397 362 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19398 : lhs_addr, lhs_var, target_expr, true,
19399 : depth);
19400 : break;
19401 58 : case tcc_vl_exp:
19402 58 : if (TREE_CODE (expr) == CALL_EXPR)
19403 : {
19404 58 : if (tree fndecl = get_callee_fndecl (expr))
19405 58 : if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
19406 : BUILT_IN_MEMCMP))
19407 : {
19408 56 : int nargs = call_expr_nargs (expr);
19409 224 : for (int i = 0; i < nargs; i++)
19410 168 : saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
19411 : pre_p, lhs_addr, lhs_var,
19412 : target_expr, true, depth);
19413 : }
19414 : }
19415 : break;
19416 : default:
19417 : break;
19418 : }
19419 :
19420 17254 : finish:
19421 17254 : if (saw_lhs == 0 && pre_p)
19422 : {
19423 3492 : enum gimplify_status gs;
19424 3492 : if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
19425 : {
19426 0 : gimplify_stmt (&expr, pre_p);
19427 0 : return saw_lhs;
19428 : }
19429 3492 : else if (rhs)
19430 3314 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
19431 : else
19432 178 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
19433 3492 : if (gs != GS_ALL_DONE)
19434 36822 : saw_lhs = -1;
19435 : }
19436 :
19437 : return saw_lhs;
19438 : }
19439 :
19440 : /* Gimplify an OMP_ATOMIC statement. */
19441 :
19442 : static enum gimplify_status
19443 10249 : gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
19444 : {
19445 10249 : tree addr = TREE_OPERAND (*expr_p, 0);
19446 10249 : tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
19447 10249 : ? NULL : TREE_OPERAND (*expr_p, 1);
19448 10249 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
19449 10249 : tree tmp_load;
19450 10249 : gomp_atomic_load *loadstmt;
19451 10249 : gomp_atomic_store *storestmt;
19452 10249 : tree target_expr = NULL_TREE;
19453 :
19454 10249 : tmp_load = create_tmp_reg (type);
19455 10249 : if (rhs
19456 10249 : && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
19457 : true, 0) < 0)
19458 : return GS_ERROR;
19459 :
19460 10249 : if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
19461 : != GS_ALL_DONE)
19462 : return GS_ERROR;
19463 :
19464 10249 : loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
19465 10249 : OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19466 10249 : gimplify_seq_add_stmt (pre_p, loadstmt);
19467 10249 : if (rhs)
19468 : {
19469 : /* BIT_INSERT_EXPR is not valid for non-integral bitfield
19470 : representatives. Use BIT_FIELD_REF on the lhs instead. */
19471 9068 : tree rhsarg = rhs;
19472 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19473 428 : rhsarg = TREE_OPERAND (rhs, 1);
19474 9068 : if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
19475 9068 : && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
19476 : {
19477 32 : tree bitpos = TREE_OPERAND (rhsarg, 2);
19478 32 : tree op1 = TREE_OPERAND (rhsarg, 1);
19479 32 : tree bitsize;
19480 32 : tree tmp_store = tmp_load;
19481 32 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
19482 12 : tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
19483 32 : if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
19484 32 : bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
19485 : else
19486 0 : bitsize = TYPE_SIZE (TREE_TYPE (op1));
19487 32 : gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
19488 32 : tree t = build2_loc (EXPR_LOCATION (rhsarg),
19489 : MODIFY_EXPR, void_type_node,
19490 32 : build3_loc (EXPR_LOCATION (rhsarg),
19491 32 : BIT_FIELD_REF, TREE_TYPE (op1),
19492 : tmp_store, bitsize, bitpos), op1);
19493 32 : if (TREE_CODE (rhs) == COND_EXPR)
19494 16 : t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
19495 16 : TREE_OPERAND (rhs, 0), t, void_node);
19496 32 : gimplify_and_add (t, pre_p);
19497 32 : rhs = tmp_store;
19498 : }
19499 9068 : bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
19500 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19501 412 : gimplify_ctxp->allow_rhs_cond_expr = true;
19502 9068 : enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
19503 : is_gimple_val, fb_rvalue);
19504 9068 : gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
19505 9068 : if (gs != GS_ALL_DONE)
19506 : return GS_ERROR;
19507 : }
19508 :
19509 10249 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
19510 1181 : rhs = tmp_load;
19511 10249 : storestmt
19512 10249 : = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19513 10249 : if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
19514 : {
19515 37 : gimple_omp_atomic_set_weak (loadstmt);
19516 37 : gimple_omp_atomic_set_weak (storestmt);
19517 : }
19518 10249 : gimplify_seq_add_stmt (pre_p, storestmt);
19519 10249 : switch (TREE_CODE (*expr_p))
19520 : {
19521 2069 : case OMP_ATOMIC_READ:
19522 2069 : case OMP_ATOMIC_CAPTURE_OLD:
19523 2069 : *expr_p = tmp_load;
19524 2069 : gimple_omp_atomic_set_need_value (loadstmt);
19525 2069 : break;
19526 868 : case OMP_ATOMIC_CAPTURE_NEW:
19527 868 : *expr_p = rhs;
19528 868 : gimple_omp_atomic_set_need_value (storestmt);
19529 868 : break;
19530 7312 : default:
19531 7312 : *expr_p = NULL;
19532 7312 : break;
19533 : }
19534 :
19535 : return GS_ALL_DONE;
19536 : }
19537 :
19538 : /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
19539 : body, and adding some EH bits. */
19540 :
19541 : static enum gimplify_status
19542 478 : gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
19543 : {
19544 478 : tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
19545 478 : gimple *body_stmt;
19546 478 : gtransaction *trans_stmt;
19547 478 : gimple_seq body = NULL;
19548 478 : int subcode = 0;
19549 :
19550 : /* Wrap the transaction body in a BIND_EXPR so we have a context
19551 : where to put decls for OMP. */
19552 478 : if (TREE_CODE (tbody) != BIND_EXPR)
19553 : {
19554 439 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
19555 439 : TREE_SIDE_EFFECTS (bind) = 1;
19556 439 : SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
19557 439 : TRANSACTION_EXPR_BODY (expr) = bind;
19558 : }
19559 :
19560 478 : push_gimplify_context ();
19561 478 : temp = voidify_wrapper_expr (*expr_p, NULL);
19562 :
19563 478 : body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
19564 478 : pop_gimplify_context (body_stmt);
19565 :
19566 478 : trans_stmt = gimple_build_transaction (body);
19567 478 : if (TRANSACTION_EXPR_OUTER (expr))
19568 : subcode = GTMA_IS_OUTER;
19569 447 : else if (TRANSACTION_EXPR_RELAXED (expr))
19570 81 : subcode = GTMA_IS_RELAXED;
19571 478 : gimple_transaction_set_subcode (trans_stmt, subcode);
19572 :
19573 478 : gimplify_seq_add_stmt (pre_p, trans_stmt);
19574 :
19575 478 : if (temp)
19576 : {
19577 76 : *expr_p = temp;
19578 76 : return GS_OK;
19579 : }
19580 :
19581 : *expr_p = NULL_TREE;
19582 : return GS_ALL_DONE;
19583 : }
19584 :
19585 : /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
19586 : is the OMP_BODY of the original EXPR (which has already been
19587 : gimplified so it's not present in the EXPR).
19588 :
19589 : Return the gimplified GIMPLE_OMP_ORDERED tuple. */
19590 :
19591 : static gimple *
19592 1950 : gimplify_omp_ordered (tree expr, gimple_seq body)
19593 : {
19594 1950 : tree c, decls;
19595 1950 : int failures = 0;
19596 1950 : unsigned int i;
19597 1950 : tree source_c = NULL_TREE;
19598 1950 : tree sink_c = NULL_TREE;
19599 :
19600 1950 : if (gimplify_omp_ctxp)
19601 : {
19602 3291 : for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
19603 1515 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19604 1515 : && gimplify_omp_ctxp->loop_iter_var.is_empty ())
19605 : {
19606 74 : error_at (OMP_CLAUSE_LOCATION (c),
19607 : "%<ordered%> construct with %qs clause must be "
19608 : "closely nested inside a loop with %<ordered%> clause",
19609 74 : OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
19610 74 : failures++;
19611 : }
19612 1441 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19613 1441 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
19614 : {
19615 642 : bool fail = false;
19616 642 : sink_c = c;
19617 642 : if (OMP_CLAUSE_DECL (c) == NULL_TREE)
19618 72 : continue; /* omp_cur_iteration - 1 */
19619 570 : for (decls = OMP_CLAUSE_DECL (c), i = 0;
19620 3201 : decls && TREE_CODE (decls) == TREE_LIST;
19621 2631 : decls = TREE_CHAIN (decls), ++i)
19622 2631 : if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
19623 4 : continue;
19624 5254 : else if (TREE_VALUE (decls)
19625 2627 : != gimplify_omp_ctxp->loop_iter_var[2 * i])
19626 : {
19627 8 : error_at (OMP_CLAUSE_LOCATION (c),
19628 : "variable %qE is not an iteration "
19629 : "of outermost loop %d, expected %qE",
19630 8 : TREE_VALUE (decls), i + 1,
19631 8 : gimplify_omp_ctxp->loop_iter_var[2 * i]);
19632 8 : fail = true;
19633 8 : failures++;
19634 : }
19635 : else
19636 5238 : TREE_VALUE (decls)
19637 2619 : = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
19638 1136 : if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
19639 : {
19640 16 : error_at (OMP_CLAUSE_LOCATION (c),
19641 : "number of variables in %qs clause with "
19642 : "%<sink%> modifier does not match number of "
19643 : "iteration variables",
19644 16 : OMP_CLAUSE_DOACROSS_DEPEND (c)
19645 : ? "depend" : "doacross");
19646 16 : failures++;
19647 : }
19648 : }
19649 799 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19650 799 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
19651 : {
19652 475 : if (source_c)
19653 : {
19654 4 : error_at (OMP_CLAUSE_LOCATION (c),
19655 : "more than one %qs clause with %<source%> "
19656 : "modifier on an %<ordered%> construct",
19657 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c)
19658 : ? "depend" : "doacross");
19659 4 : failures++;
19660 : }
19661 : else
19662 : source_c = c;
19663 : }
19664 : }
19665 1950 : if (source_c && sink_c)
19666 : {
19667 4 : error_at (OMP_CLAUSE_LOCATION (source_c),
19668 : "%qs clause with %<source%> modifier specified "
19669 : "together with %qs clauses with %<sink%> modifier "
19670 : "on the same construct",
19671 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
19672 4 : OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
19673 4 : failures++;
19674 : }
19675 :
19676 1950 : if (failures)
19677 102 : return gimple_build_nop ();
19678 1848 : return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
19679 : }
19680 :
19681 : /* Gimplify an OMP_INTEROP statement. */
19682 :
19683 : static enum gimplify_status
19684 612 : gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
19685 : {
19686 612 : tree expr = *expr_p;
19687 :
19688 612 : gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
19689 : OMP_INTEROP);
19690 612 : gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
19691 612 : gimplify_seq_add_stmt (pre_p, stmt);
19692 612 : *expr_p = NULL_TREE;
19693 612 : return GS_ALL_DONE;
19694 : }
19695 :
19696 : /* Callback for walk_tree to find an IFN_GOMP_DISPATCH. */
19697 :
19698 : static tree
19699 3100 : find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
19700 : {
19701 3100 : tree t = *tp;
19702 :
19703 3100 : if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
19704 852 : return *(tree *) modify ? *(tree *) modify : *tp;
19705 :
19706 2248 : if (TREE_CODE (t) == MODIFY_EXPR)
19707 513 : *(tree *) modify = *tp;
19708 :
19709 : return NULL_TREE;
19710 : }
19711 :
19712 : /* Gimplify an OMP_DISPATCH construct. */
19713 :
19714 : static enum gimplify_status
19715 852 : gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
19716 : {
19717 852 : tree expr = *expr_p;
19718 852 : gimple_seq body = NULL;
19719 :
19720 852 : gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
19721 : OMP_DISPATCH);
19722 852 : push_gimplify_context ();
19723 :
19724 : // If device clause, adjust ICV
19725 852 : tree device
19726 852 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
19727 : // If no device clause exists but an interop clause with a single list
19728 : // item, use it to obtain the device number.
19729 852 : if (device)
19730 272 : device = OMP_CLAUSE_DEVICE_ID (device);
19731 : else
19732 : {
19733 580 : tree first_interop_obj
19734 580 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
19735 580 : if (first_interop_obj)
19736 96 : for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
19737 8 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
19738 : {
19739 : first_interop_obj = NULL_TREE;
19740 : break;
19741 : }
19742 95 : if (first_interop_obj)
19743 : {
19744 88 : device = create_tmp_var (integer_type_node);
19745 88 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
19746 : OMP_CLAUSE_DEVICE);
19747 88 : OMP_CLAUSE_DEVICE_ID (c) = device;
19748 88 : TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
19749 88 : TREE_CHAIN (first_interop_obj) = c;
19750 88 : first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
19751 : /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL); */
19752 88 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
19753 88 : fn = build_call_expr (fn, 3, first_interop_obj,
19754 : build_int_cst (integer_type_node, -5),
19755 : null_pointer_node);
19756 88 : gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
19757 : }
19758 : }
19759 852 : tree saved_device_icv = NULL_TREE;
19760 852 : if (device
19761 852 : && (TREE_CODE (device) != INTEGER_CST
19762 852 : || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
19763 : {
19764 : // Save current default-device-var ICV
19765 360 : saved_device_icv = create_tmp_var (integer_type_node);
19766 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
19767 360 : gcall *call = gimple_build_call (fn, 0);
19768 360 : gimple_call_set_lhs (call, saved_device_icv);
19769 360 : gimplify_seq_add_stmt (&body, call);
19770 :
19771 : // Set default device
19772 360 : fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19773 360 : call = gimple_build_call (fn, 1, device);
19774 360 : gimplify_seq_add_stmt (&body, call);
19775 : }
19776 :
19777 : // If the novariants and nocontext clauses are not compile-time constants,
19778 : // we need to generate code for all possible cases:
19779 : // if (novariants) // implies nocontext
19780 : // base()
19781 : // else if (nocontext)
19782 : // variant1()
19783 : // else
19784 : // variant2()
19785 852 : tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
19786 852 : if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
19787 28 : dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
19788 852 : tree dispatch_body = *dispatch_body_p;
19789 :
19790 : // Look for IFN_GOMP_DISPATCH and extract the base function call
19791 852 : tree base_call_expr = NULL_TREE;
19792 852 : if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
19793 243 : for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
19794 187 : tsi_next (&tsi))
19795 : {
19796 243 : tree modify = NULL_TREE;
19797 243 : tree stmt = tsi_stmt (tsi);
19798 243 : base_call_expr
19799 243 : = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
19800 243 : if (base_call_expr != NULL_TREE)
19801 : break;
19802 : }
19803 : else
19804 : {
19805 796 : tree modify = NULL_TREE;
19806 796 : base_call_expr
19807 796 : = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
19808 : }
19809 852 : gcc_assert (base_call_expr != NULL_TREE);
19810 :
19811 852 : tree dst = NULL_TREE;
19812 852 : if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
19813 : {
19814 290 : dst = TREE_OPERAND (base_call_expr, 0);
19815 290 : base_call_expr = TREE_OPERAND (base_call_expr, 1);
19816 : }
19817 :
19818 880 : while (TREE_CODE (base_call_expr) == FLOAT_EXPR
19819 : || TREE_CODE (base_call_expr) == CONVERT_EXPR
19820 : || TREE_CODE (base_call_expr) == COMPLEX_EXPR
19821 : || TREE_CODE (base_call_expr) == INDIRECT_REF
19822 880 : || TREE_CODE (base_call_expr) == NOP_EXPR)
19823 28 : base_call_expr = TREE_OPERAND (base_call_expr, 0);
19824 :
19825 852 : gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
19826 852 : base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
19827 :
19828 852 : tree base_fndecl = get_callee_fndecl (base_call_expr);
19829 852 : if (base_fndecl != NULL_TREE)
19830 : {
19831 844 : if (DECL_VIRTUAL_P (base_fndecl))
19832 : {
19833 6 : error_at (
19834 3 : EXPR_LOCATION (base_call_expr),
19835 : "%qD is a virtual function but only a direct call is allowed "
19836 : "in a dispatch construct",
19837 3 : DECL_NAME (base_fndecl));
19838 : }
19839 :
19840 : /* We are not actually going to expand the variant call or use
19841 : the result of omp_get_dynamic candidates here; only check that
19842 : it does not trivially resolve to a call to the base function
19843 : so that we can avoid some extra work in building code that's
19844 : not needed in that case. */
19845 844 : tree construct_context = omp_get_construct_context ();
19846 844 : vec<struct omp_variant> all_candidates
19847 844 : = omp_declare_variant_candidates (base_fndecl, construct_context);
19848 844 : gcc_assert (!all_candidates.is_empty ());
19849 844 : vec<struct omp_variant> candidates
19850 844 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19851 844 : tree variant_fndecl
19852 1676 : = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
19853 :
19854 844 : if (base_fndecl != variant_fndecl
19855 844 : && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
19856 : {
19857 22 : tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
19858 22 : novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
19859 59 : for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
19860 : {
19861 37 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
19862 37 : && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
19863 : {
19864 16 : gcc_assert (novariants_cond == NULL_TREE);
19865 16 : novariants_clause = c;
19866 16 : novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
19867 : }
19868 21 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
19869 21 : && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
19870 : {
19871 16 : gcc_assert (nocontext_cond == NULL_TREE);
19872 16 : nocontext_clause = c;
19873 16 : nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
19874 : }
19875 : }
19876 22 : gcc_assert (novariants_cond != NULL_TREE
19877 : || nocontext_cond != NULL_TREE);
19878 :
19879 22 : enum gimplify_status ret
19880 22 : = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
19881 : fb_rvalue);
19882 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19883 0 : return ret;
19884 22 : ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
19885 : fb_rvalue);
19886 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19887 : return ret;
19888 :
19889 22 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19890 :
19891 22 : if (novariants_cond != NULL_TREE)
19892 : {
19893 16 : tree base_label = create_artificial_label (UNKNOWN_LOCATION);
19894 16 : tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
19895 16 : gcond *novariants_cond_stmt
19896 16 : = gimple_build_cond_from_tree (novariants_cond, base_label,
19897 : cond_label);
19898 16 : gimplify_seq_add_stmt (&body, novariants_cond_stmt);
19899 :
19900 16 : gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
19901 16 : tree base_call_expr2 = copy_node (base_call_expr);
19902 16 : base_call_expr2
19903 16 : = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
19904 : IFN_GOMP_DISPATCH,
19905 16 : TREE_TYPE (base_call_expr2), 1,
19906 : base_call_expr2);
19907 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19908 : {
19909 16 : base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19910 : base_call_expr2);
19911 : }
19912 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19913 16 : = boolean_true_node;
19914 16 : gimplify_and_add (base_call_expr2, &body);
19915 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19916 :
19917 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19918 16 : = boolean_false_node;
19919 16 : gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
19920 : }
19921 :
19922 22 : if (nocontext_cond != NULL_TREE)
19923 : {
19924 16 : tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
19925 16 : tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
19926 16 : gcond *nocontext_cond_stmt
19927 16 : = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
19928 : variant2_label);
19929 16 : gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
19930 :
19931 32 : gimplify_seq_add_stmt (&body,
19932 16 : gimple_build_label (variant1_label));
19933 16 : tree variant_call_expr = copy_node (base_call_expr);
19934 32 : variant_call_expr = build_call_expr_internal_loc (
19935 16 : EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
19936 16 : TREE_TYPE (variant_call_expr), 1, variant_call_expr);
19937 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19938 : {
19939 16 : variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19940 : variant_call_expr);
19941 : }
19942 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
19943 16 : gimplify_and_add (variant_call_expr, &body);
19944 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19945 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
19946 32 : gimplify_seq_add_stmt (&body,
19947 16 : gimple_build_label (variant2_label));
19948 : }
19949 :
19950 22 : tree variant_call_expr = base_call_expr;
19951 22 : variant_call_expr
19952 22 : = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
19953 : IFN_GOMP_DISPATCH,
19954 22 : TREE_TYPE (variant_call_expr), 1,
19955 : variant_call_expr);
19956 22 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19957 : {
19958 22 : variant_call_expr
19959 22 : = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
19960 : }
19961 22 : gimplify_and_add (variant_call_expr, &body);
19962 22 : gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
19963 : }
19964 : else
19965 822 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19966 : }
19967 : else
19968 8 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19969 :
19970 : // Restore default-device-var ICV
19971 852 : if (saved_device_icv != NULL_TREE)
19972 : {
19973 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19974 360 : gcall *call = gimple_build_call (fn, 1, saved_device_icv);
19975 360 : gimplify_seq_add_stmt (&body, call);
19976 : }
19977 :
19978 : // Wrap dispatch body into a bind
19979 852 : gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
19980 852 : pop_gimplify_context (bind);
19981 :
19982 : // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
19983 : // call to gimplify_adjust_omp_clauses
19984 852 : gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
19985 852 : if (ctx != NULL)
19986 : {
19987 852 : gcc_assert (ctx->code == OMP_DISPATCH);
19988 852 : gimplify_omp_ctxp = ctx->outer_context;
19989 852 : delete_omp_context (ctx);
19990 : }
19991 :
19992 : // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
19993 : // has been handled above, and depend as the front end handled it by inserting
19994 : // taskwait.
19995 852 : tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
19996 1445 : for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
19997 : {
19998 991 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
19999 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
20000 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
20001 : {
20002 398 : *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
20003 398 : break;
20004 : }
20005 : else
20006 593 : dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
20007 : }
20008 :
20009 852 : gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
20010 852 : gimplify_seq_add_stmt (pre_p, stmt);
20011 852 : *expr_p = NULL_TREE;
20012 852 : return GS_ALL_DONE;
20013 : }
20014 :
20015 : /* Expand a metadirective that has been resolved at gimplification time
20016 : into the candidate directive variants in CANDIDATES. */
20017 :
20018 : static enum gimplify_status
20019 170 : expand_omp_metadirective (vec<struct omp_variant> &candidates,
20020 : gimple_seq *pre_p)
20021 : {
20022 170 : auto_vec<tree> selectors;
20023 170 : auto_vec<tree> directive_labels;
20024 170 : auto_vec<gimple_seq> directive_bodies;
20025 170 : tree body_label = NULL_TREE;
20026 170 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
20027 :
20028 : /* Construct bodies for each candidate. */
20029 585 : for (unsigned i = 0; i < candidates.length(); i++)
20030 : {
20031 245 : struct omp_variant &candidate = candidates[i];
20032 245 : gimple_seq body = NULL;
20033 :
20034 245 : selectors.safe_push (omp_dynamic_cond (candidate.selector,
20035 : find_supercontext ()));
20036 245 : directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
20037 :
20038 245 : gimplify_seq_add_stmt (&body,
20039 245 : gimple_build_label (directive_labels.last ()));
20040 245 : if (candidate.alternative != NULL_TREE)
20041 245 : gimplify_stmt (&candidate.alternative, &body);
20042 245 : if (candidate.body != NULL_TREE)
20043 : {
20044 35 : if (body_label != NULL_TREE)
20045 0 : gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
20046 : else
20047 : {
20048 35 : body_label = create_artificial_label (UNKNOWN_LOCATION);
20049 35 : gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
20050 35 : gimplify_stmt (&candidate.body, &body);
20051 : }
20052 : }
20053 :
20054 245 : directive_bodies.safe_push (body);
20055 : }
20056 :
20057 170 : auto_vec<tree> cond_labels;
20058 :
20059 170 : cond_labels.safe_push (NULL_TREE);
20060 376 : for (unsigned i = 1; i < candidates.length () - 1; i++)
20061 18 : cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
20062 170 : if (candidates.length () > 1)
20063 57 : cond_labels.safe_push (directive_labels.last ());
20064 :
20065 : /* Generate conditionals to test each dynamic selector in turn, executing
20066 : the directive candidate if successful. */
20067 490 : for (unsigned i = 0; i < candidates.length () - 1; i++)
20068 : {
20069 75 : if (i != 0)
20070 18 : gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
20071 :
20072 75 : enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
20073 : is_gimple_val, fb_rvalue);
20074 75 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
20075 : return ret;
20076 :
20077 75 : gcond *cond_stmt
20078 75 : = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
20079 75 : cond_labels[i + 1]);
20080 :
20081 75 : gimplify_seq_add_stmt (pre_p, cond_stmt);
20082 75 : gimplify_seq_add_seq (pre_p, directive_bodies[i]);
20083 75 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
20084 : }
20085 :
20086 170 : gimplify_seq_add_seq (pre_p, directive_bodies.last ());
20087 170 : gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
20088 :
20089 170 : return GS_ALL_DONE;
20090 170 : }
20091 :
20092 : /* Expand a variant construct that requires late resolution in the ompdevlow
20093 : pass. It's a bit easier to do this in tree form and then gimplify that,
20094 : than to emit gimple. The output is going to look something like:
20095 :
20096 : switch_var = OMP_NEXT_VARIANT (0, state);
20097 : loop_label:
20098 : switch (switch_var)
20099 : {
20100 : case 1:
20101 : if (dynamic_selector_predicate_1)
20102 : {
20103 : alternative_1;
20104 : goto end_label;
20105 : }
20106 : else
20107 : {
20108 : switch_var = OMP_NEXT_VARIANT (1, state);
20109 : goto loop_label;
20110 : }
20111 : case 2:
20112 : ...
20113 : }
20114 : end_label:
20115 :
20116 : OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
20117 : index of the next variant to try, after late resolution. */
20118 :
20119 : static tree
20120 16 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
20121 : tree construct_context)
20122 : {
20123 16 : tree body_label = NULL_TREE;
20124 16 : tree standalone_body = NULL_TREE;
20125 16 : tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
20126 16 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
20127 32 : tree selectors = make_tree_vec (all_candidates.length ());
20128 16 : tree switch_body = NULL_TREE;
20129 16 : tree switch_var = create_tmp_var (integer_type_node, "variant");
20130 16 : tree state = tree_cons (NULL_TREE, construct_context, selectors);
20131 :
20132 108 : for (unsigned int i = 0; i < all_candidates.length (); i++)
20133 : {
20134 76 : tree selector = all_candidates[i].selector;
20135 76 : tree alternative = all_candidates[i].alternative;
20136 76 : tree body = all_candidates[i].body;
20137 76 : TREE_VEC_ELT (selectors, i) = selector;
20138 :
20139 : /* Case label. Numbering is 1-based. */
20140 76 : tree case_val = build_int_cst (integer_type_node, i + 1);
20141 76 : tree case_label
20142 76 : = build_case_label (case_val, NULL_TREE,
20143 : create_artificial_label (UNKNOWN_LOCATION));
20144 76 : append_to_statement_list (case_label, &switch_body);
20145 :
20146 : /* The actual body of the variant. */
20147 76 : tree variant_body = NULL_TREE;
20148 76 : append_to_statement_list (alternative, &variant_body);
20149 :
20150 76 : if (body != NULL_TREE)
20151 : {
20152 0 : if (standalone_body == NULL)
20153 : {
20154 0 : standalone_body = body;
20155 0 : body_label = create_artificial_label (UNKNOWN_LOCATION);
20156 : }
20157 0 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
20158 : body_label),
20159 : &variant_body);
20160 : }
20161 : else
20162 76 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
20163 : end_label),
20164 : &variant_body);
20165 :
20166 : /* If this is a dynamic selector, wrap variant_body with a conditional.
20167 : If the predicate doesn't match, the else clause sets switch_var and
20168 : jumps to loop_var to try again. */
20169 76 : tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
20170 76 : if (dynamic_selector)
20171 : {
20172 20 : tree else_stmt = NULL_TREE;
20173 20 : tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
20174 : case_val, state);
20175 20 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
20176 : switch_var, next),
20177 : &else_stmt);
20178 20 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
20179 : loop_label),
20180 : &else_stmt);
20181 20 : variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
20182 : variant_body, else_stmt);
20183 : }
20184 76 : append_to_statement_list (variant_body, &switch_body);
20185 : }
20186 :
20187 : /* Put it all together. */
20188 16 : tree result = NULL_TREE;
20189 16 : tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
20190 : state);
20191 16 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
20192 : switch_var, first),
20193 : &result);
20194 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
20195 : &result);
20196 16 : append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
20197 : switch_var, switch_body),
20198 : &result);
20199 16 : if (standalone_body)
20200 : {
20201 0 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
20202 : body_label),
20203 : &result);
20204 0 : append_to_statement_list (standalone_body, &result);
20205 : }
20206 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
20207 : &result);
20208 16 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
20209 16 : return result;
20210 : }
20211 :
20212 :
20213 : /* Gimplify an OMP_METADIRECTIVE construct. EXPR is the tree version.
20214 : The metadirective will be resolved at this point if possible, otherwise
20215 : a GIMPLE_OMP_VARIANT_CONSTRUCT is created. */
20216 :
20217 : static enum gimplify_status
20218 174 : gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
20219 : bool (*) (tree), fallback_t)
20220 : {
20221 : /* Try to resolve the metadirective. */
20222 174 : tree construct_context = omp_get_construct_context ();
20223 174 : vec<struct omp_variant> all_candidates
20224 174 : = omp_metadirective_candidates (*expr_p, construct_context);
20225 174 : vec<struct omp_variant> candidates
20226 174 : = omp_get_dynamic_candidates (all_candidates, construct_context);
20227 174 : if (!candidates.is_empty ())
20228 170 : return expand_omp_metadirective (candidates, pre_p);
20229 :
20230 : /* The metadirective cannot be resolved yet. Turn it into a loop with
20231 : a nested switch statement, using OMP_NEXT_VARIANT to set the control
20232 : variable for the switch. */
20233 4 : *expr_p = expand_late_variant_directive (all_candidates, construct_context);
20234 4 : return GS_OK;
20235 : }
20236 :
20237 : /* Gimplify an OMP_DECLARE_MAPPER node (by just removing it). */
20238 :
20239 : static enum gimplify_status
20240 0 : gimplify_omp_declare_mapper (tree *expr_p)
20241 : {
20242 0 : *expr_p = NULL_TREE;
20243 0 : return GS_ALL_DONE;
20244 : }
20245 :
20246 : /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
20247 : expression produces a value to be used as an operand inside a GIMPLE
20248 : statement, the value will be stored back in *EXPR_P. This value will
20249 : be a tree of class tcc_declaration, tcc_constant, tcc_reference or
20250 : an SSA_NAME. The corresponding sequence of GIMPLE statements is
20251 : emitted in PRE_P and POST_P.
20252 :
20253 : Additionally, this process may overwrite parts of the input
20254 : expression during gimplification. Ideally, it should be
20255 : possible to do non-destructive gimplification.
20256 :
20257 : EXPR_P points to the GENERIC expression to convert to GIMPLE. If
20258 : the expression needs to evaluate to a value to be used as
20259 : an operand in a GIMPLE statement, this value will be stored in
20260 : *EXPR_P on exit. This happens when the caller specifies one
20261 : of fb_lvalue or fb_rvalue fallback flags.
20262 :
20263 : PRE_P will contain the sequence of GIMPLE statements corresponding
20264 : to the evaluation of EXPR and all the side-effects that must
20265 : be executed before the main expression. On exit, the last
20266 : statement of PRE_P is the core statement being gimplified. For
20267 : instance, when gimplifying 'if (++a)' the last statement in
20268 : PRE_P will be 'if (t.1)' where t.1 is the result of
20269 : pre-incrementing 'a'.
20270 :
20271 : POST_P will contain the sequence of GIMPLE statements corresponding
20272 : to the evaluation of all the side-effects that must be executed
20273 : after the main expression. If this is NULL, the post
20274 : side-effects are stored at the end of PRE_P.
20275 :
20276 : The reason why the output is split in two is to handle post
20277 : side-effects explicitly. In some cases, an expression may have
20278 : inner and outer post side-effects which need to be emitted in
20279 : an order different from the one given by the recursive
20280 : traversal. For instance, for the expression (*p--)++ the post
20281 : side-effects of '--' must actually occur *after* the post
20282 : side-effects of '++'. However, gimplification will first visit
20283 : the inner expression, so if a separate POST sequence was not
20284 : used, the resulting sequence would be:
20285 :
20286 : 1 t.1 = *p
20287 : 2 p = p - 1
20288 : 3 t.2 = t.1 + 1
20289 : 4 *p = t.2
20290 :
20291 : However, the post-decrement operation in line #2 must not be
20292 : evaluated until after the store to *p at line #4, so the
20293 : correct sequence should be:
20294 :
20295 : 1 t.1 = *p
20296 : 2 t.2 = t.1 + 1
20297 : 3 *p = t.2
20298 : 4 p = p - 1
20299 :
20300 : So, by specifying a separate post queue, it is possible
20301 : to emit the post side-effects in the correct order.
20302 : If POST_P is NULL, an internal queue will be used. Before
20303 : returning to the caller, the sequence POST_P is appended to
20304 : the main output sequence PRE_P.
20305 :
20306 : GIMPLE_TEST_F points to a function that takes a tree T and
20307 : returns nonzero if T is in the GIMPLE form requested by the
20308 : caller. The GIMPLE predicates are in gimple.cc.
20309 :
20310 : FALLBACK tells the function what sort of a temporary we want if
20311 : gimplification cannot produce an expression that complies with
20312 : GIMPLE_TEST_F.
20313 :
20314 : fb_none means that no temporary should be generated
20315 : fb_rvalue means that an rvalue is OK to generate
20316 : fb_lvalue means that an lvalue is OK to generate
20317 : fb_either means that either is OK, but an lvalue is preferable.
20318 : fb_mayfail means that gimplification may fail (in which case
20319 : GS_ERROR will be returned)
20320 :
20321 : The return value is either GS_ERROR or GS_ALL_DONE, since this
20322 : function iterates until EXPR is completely gimplified or an error
20323 : occurs. */
20324 :
20325 : enum gimplify_status
20326 528745451 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
20327 : bool (*gimple_test_f) (tree), fallback_t fallback)
20328 : {
20329 528745451 : tree tmp;
20330 528745451 : gimple_seq internal_pre = NULL;
20331 528745451 : gimple_seq internal_post = NULL;
20332 528745451 : tree save_expr;
20333 528745451 : bool is_statement;
20334 528745451 : location_t saved_location;
20335 528745451 : enum gimplify_status ret;
20336 528745451 : gimple_stmt_iterator pre_last_gsi, post_last_gsi;
20337 528745451 : tree label;
20338 :
20339 528745451 : save_expr = *expr_p;
20340 528745451 : if (save_expr == NULL_TREE)
20341 : return GS_ALL_DONE;
20342 :
20343 : /* If we are gimplifying a top-level statement, PRE_P must be valid. */
20344 475773478 : is_statement = gimple_test_f == is_gimple_stmt;
20345 475773478 : if (is_statement)
20346 103185566 : gcc_assert (pre_p);
20347 :
20348 : /* Consistency checks. */
20349 475773478 : if (gimple_test_f == is_gimple_reg)
20350 6435674 : gcc_assert (fallback & (fb_rvalue | fb_lvalue));
20351 469337804 : else if (gimple_test_f == is_gimple_val
20352 346117344 : || gimple_test_f == is_gimple_call_addr
20353 329338842 : || gimple_test_f == is_gimple_condexpr_for_cond
20354 323650869 : || gimple_test_f == is_gimple_mem_rhs
20355 323506601 : || gimple_test_f == is_gimple_mem_rhs_or_call
20356 312041300 : || gimple_test_f == is_gimple_reg_rhs
20357 311435267 : || gimple_test_f == is_gimple_reg_rhs_or_call
20358 238224367 : || gimple_test_f == is_gimple_asm_val
20359 238184450 : || gimple_test_f == is_gimple_mem_ref_addr)
20360 246879989 : gcc_assert (fallback & fb_rvalue);
20361 222457815 : else if (gimple_test_f == is_gimple_min_lval
20362 191670140 : || gimple_test_f == is_gimple_lvalue)
20363 85411635 : gcc_assert (fallback & fb_lvalue);
20364 137046180 : else if (gimple_test_f == is_gimple_addressable)
20365 33860614 : gcc_assert (fallback & fb_either);
20366 103185566 : else if (gimple_test_f == is_gimple_stmt)
20367 103185566 : gcc_assert (fallback == fb_none);
20368 : else
20369 : {
20370 : /* We should have recognized the GIMPLE_TEST_F predicate to
20371 : know what kind of fallback to use in case a temporary is
20372 : needed to hold the value or address of *EXPR_P. */
20373 0 : gcc_unreachable ();
20374 : }
20375 :
20376 : /* We used to check the predicate here and return immediately if it
20377 : succeeds. This is wrong; the design is for gimplification to be
20378 : idempotent, and for the predicates to only test for valid forms, not
20379 : whether they are fully simplified. */
20380 475773478 : if (pre_p == NULL)
20381 0 : pre_p = &internal_pre;
20382 :
20383 475773478 : if (post_p == NULL)
20384 191690171 : post_p = &internal_post;
20385 :
20386 : /* Remember the last statements added to PRE_P and POST_P. Every
20387 : new statement added by the gimplification helpers needs to be
20388 : annotated with location information. To centralize the
20389 : responsibility, we remember the last statement that had been
20390 : added to both queues before gimplifying *EXPR_P. If
20391 : gimplification produces new statements in PRE_P and POST_P, those
20392 : statements will be annotated with the same location information
20393 : as *EXPR_P. */
20394 475773478 : pre_last_gsi = gsi_last (*pre_p);
20395 475773478 : post_last_gsi = gsi_last (*post_p);
20396 :
20397 475773478 : saved_location = input_location;
20398 475773478 : if (save_expr != error_mark_node
20399 475773478 : && EXPR_HAS_LOCATION (*expr_p))
20400 178421892 : input_location = EXPR_LOCATION (*expr_p);
20401 :
20402 : /* Loop over the specific gimplifiers until the toplevel node
20403 : remains the same. */
20404 494570666 : do
20405 : {
20406 : /* Strip away as many useless type conversions as possible
20407 : at the toplevel. */
20408 494570666 : STRIP_USELESS_TYPE_CONVERSION (*expr_p);
20409 :
20410 : /* Remember the expr. */
20411 494570666 : save_expr = *expr_p;
20412 :
20413 : /* Die, die, die, my darling. */
20414 494570666 : if (error_operand_p (save_expr))
20415 : {
20416 : ret = GS_ERROR;
20417 : break;
20418 : }
20419 :
20420 : /* Do any language-specific gimplification. */
20421 494566990 : ret = ((enum gimplify_status)
20422 494566990 : lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
20423 494566990 : if (ret == GS_OK)
20424 : {
20425 29719838 : if (*expr_p == NULL_TREE)
20426 : break;
20427 29719838 : if (*expr_p != save_expr)
20428 4953580 : continue;
20429 : }
20430 464847152 : else if (ret != GS_UNHANDLED)
20431 : break;
20432 :
20433 : /* Make sure that all the cases set 'ret' appropriately. */
20434 489002550 : ret = GS_UNHANDLED;
20435 489002550 : switch (TREE_CODE (*expr_p))
20436 : {
20437 : /* First deal with the special cases. */
20438 :
20439 1145863 : case POSTINCREMENT_EXPR:
20440 1145863 : case POSTDECREMENT_EXPR:
20441 1145863 : case PREINCREMENT_EXPR:
20442 1145863 : case PREDECREMENT_EXPR:
20443 2291726 : ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
20444 : fallback != fb_none,
20445 1145863 : TREE_TYPE (*expr_p));
20446 1145863 : break;
20447 :
20448 604631 : case VIEW_CONVERT_EXPR:
20449 604631 : if ((fallback & fb_rvalue)
20450 604511 : && is_gimple_reg_type (TREE_TYPE (*expr_p))
20451 1032082 : && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
20452 : {
20453 415579 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20454 : post_p, is_gimple_val, fb_rvalue);
20455 415579 : recalculate_side_effects (*expr_p);
20456 415579 : break;
20457 : }
20458 : /* Fallthru. */
20459 :
20460 30724720 : case ARRAY_REF:
20461 30724720 : case ARRAY_RANGE_REF:
20462 30724720 : case REALPART_EXPR:
20463 30724720 : case IMAGPART_EXPR:
20464 30724720 : case COMPONENT_REF:
20465 30724720 : ret = gimplify_compound_lval (expr_p, pre_p, post_p,
20466 : fallback ? fallback : fb_rvalue);
20467 30724720 : break;
20468 :
20469 6302467 : case COND_EXPR:
20470 6302467 : ret = gimplify_cond_expr (expr_p, pre_p, fallback);
20471 :
20472 : /* C99 code may assign to an array in a structure value of a
20473 : conditional expression, and this has undefined behavior
20474 : only on execution, so create a temporary if an lvalue is
20475 : required. */
20476 6302467 : if (fallback == fb_lvalue)
20477 : {
20478 7 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20479 7 : mark_addressable (*expr_p);
20480 7 : ret = GS_OK;
20481 : }
20482 : break;
20483 :
20484 17342414 : case CALL_EXPR:
20485 17342414 : ret = gimplify_call_expr (expr_p, pre_p, fallback);
20486 :
20487 : /* C99 code may assign to an array in a structure returned
20488 : from a function, and this has undefined behavior only on
20489 : execution, so create a temporary if an lvalue is
20490 : required. */
20491 17342414 : if (fallback == fb_lvalue)
20492 : {
20493 17203 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20494 17203 : mark_addressable (*expr_p);
20495 17203 : ret = GS_OK;
20496 : }
20497 : break;
20498 :
20499 0 : case TREE_LIST:
20500 0 : gcc_unreachable ();
20501 :
20502 0 : case OMP_ARRAY_SECTION:
20503 0 : gcc_unreachable ();
20504 :
20505 497116 : case COMPOUND_EXPR:
20506 497116 : ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
20507 497116 : break;
20508 :
20509 37225 : case COMPOUND_LITERAL_EXPR:
20510 37225 : ret = gimplify_compound_literal_expr (expr_p, pre_p,
20511 : gimple_test_f, fallback);
20512 37225 : break;
20513 :
20514 51148885 : case MODIFY_EXPR:
20515 51148885 : case INIT_EXPR:
20516 51148885 : ret = gimplify_modify_expr (expr_p, pre_p, post_p,
20517 : fallback != fb_none);
20518 51148885 : break;
20519 :
20520 115634 : case TRUTH_ANDIF_EXPR:
20521 115634 : case TRUTH_ORIF_EXPR:
20522 115634 : {
20523 : /* Preserve the original type of the expression and the
20524 : source location of the outer expression. */
20525 115634 : tree org_type = TREE_TYPE (*expr_p);
20526 115634 : *expr_p = gimple_boolify (*expr_p);
20527 115634 : *expr_p = build3_loc (input_location, COND_EXPR,
20528 : org_type, *expr_p,
20529 : fold_convert_loc
20530 : (input_location,
20531 : org_type, boolean_true_node),
20532 : fold_convert_loc
20533 : (input_location,
20534 : org_type, boolean_false_node));
20535 115634 : ret = GS_OK;
20536 115634 : break;
20537 : }
20538 :
20539 235998 : case TRUTH_NOT_EXPR:
20540 235998 : {
20541 235998 : tree type = TREE_TYPE (*expr_p);
20542 : /* The parsers are careful to generate TRUTH_NOT_EXPR
20543 : only with operands that are always zero or one.
20544 : We do not fold here but handle the only interesting case
20545 : manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
20546 235998 : *expr_p = gimple_boolify (*expr_p);
20547 235998 : if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
20548 235998 : *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
20549 235998 : TREE_TYPE (*expr_p),
20550 235998 : TREE_OPERAND (*expr_p, 0));
20551 : else
20552 0 : *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
20553 0 : TREE_TYPE (*expr_p),
20554 0 : TREE_OPERAND (*expr_p, 0),
20555 0 : build_int_cst (TREE_TYPE (*expr_p), 1));
20556 235998 : if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
20557 6454 : *expr_p = fold_convert_loc (input_location, type, *expr_p);
20558 : ret = GS_OK;
20559 : break;
20560 : }
20561 :
20562 33941928 : case ADDR_EXPR:
20563 33941928 : ret = gimplify_addr_expr (expr_p, pre_p, post_p);
20564 33941928 : break;
20565 :
20566 5695 : case ANNOTATE_EXPR:
20567 5695 : {
20568 5695 : tree cond = TREE_OPERAND (*expr_p, 0);
20569 5695 : tree kind = TREE_OPERAND (*expr_p, 1);
20570 5695 : tree data = TREE_OPERAND (*expr_p, 2);
20571 5695 : tree type = TREE_TYPE (cond);
20572 5695 : if (!INTEGRAL_TYPE_P (type))
20573 : {
20574 0 : *expr_p = cond;
20575 0 : ret = GS_OK;
20576 0 : break;
20577 : }
20578 5695 : tree tmp = create_tmp_var (type);
20579 5695 : gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
20580 5695 : gcall *call
20581 5695 : = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
20582 5695 : gimple_call_set_lhs (call, tmp);
20583 5695 : gimplify_seq_add_stmt (pre_p, call);
20584 5695 : *expr_p = tmp;
20585 5695 : ret = GS_ALL_DONE;
20586 5695 : break;
20587 : }
20588 :
20589 51167 : case VA_ARG_EXPR:
20590 51167 : ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
20591 51167 : break;
20592 :
20593 17742448 : CASE_CONVERT:
20594 17742448 : if (IS_EMPTY_STMT (*expr_p))
20595 : {
20596 : ret = GS_ALL_DONE;
20597 : break;
20598 : }
20599 :
20600 15979914 : if (VOID_TYPE_P (TREE_TYPE (*expr_p))
20601 15979914 : || fallback == fb_none)
20602 : {
20603 : /* Just strip a conversion to void (or in void context) and
20604 : try again. */
20605 2722995 : *expr_p = TREE_OPERAND (*expr_p, 0);
20606 2722995 : ret = GS_OK;
20607 2722995 : break;
20608 : }
20609 :
20610 13256919 : ret = gimplify_conversion (expr_p);
20611 13256919 : if (ret == GS_ERROR)
20612 : break;
20613 13256919 : if (*expr_p != save_expr)
20614 : break;
20615 : /* FALLTHRU */
20616 :
20617 13384788 : case FIX_TRUNC_EXPR:
20618 : /* unary_expr: ... | '(' cast ')' val | ... */
20619 13384788 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20620 : is_gimple_val, fb_rvalue);
20621 13384788 : recalculate_side_effects (*expr_p);
20622 13384788 : break;
20623 :
20624 6366384 : case INDIRECT_REF:
20625 6366384 : {
20626 6366384 : bool volatilep = TREE_THIS_VOLATILE (*expr_p);
20627 6366384 : bool notrap = TREE_THIS_NOTRAP (*expr_p);
20628 6366384 : tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
20629 :
20630 6366384 : *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
20631 6366384 : if (*expr_p != save_expr)
20632 : {
20633 : ret = GS_OK;
20634 : break;
20635 : }
20636 :
20637 6347804 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20638 : is_gimple_reg, fb_rvalue);
20639 6347804 : if (ret == GS_ERROR)
20640 : break;
20641 :
20642 6347803 : recalculate_side_effects (*expr_p);
20643 12695606 : *expr_p = fold_build2_loc (input_location, MEM_REF,
20644 6347803 : TREE_TYPE (*expr_p),
20645 6347803 : TREE_OPERAND (*expr_p, 0),
20646 : build_int_cst (saved_ptr_type, 0));
20647 6347803 : TREE_THIS_VOLATILE (*expr_p) = volatilep;
20648 6347803 : TREE_THIS_NOTRAP (*expr_p) = notrap;
20649 6347803 : ret = GS_OK;
20650 6347803 : break;
20651 : }
20652 :
20653 : /* We arrive here through the various re-gimplifcation paths. */
20654 16181344 : case MEM_REF:
20655 : /* First try re-folding the whole thing. */
20656 16181344 : tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
20657 : TREE_OPERAND (*expr_p, 0),
20658 : TREE_OPERAND (*expr_p, 1));
20659 16181344 : if (tmp)
20660 : {
20661 6632 : REF_REVERSE_STORAGE_ORDER (tmp)
20662 3316 : = REF_REVERSE_STORAGE_ORDER (*expr_p);
20663 3316 : *expr_p = tmp;
20664 3316 : recalculate_side_effects (*expr_p);
20665 3316 : ret = GS_OK;
20666 3316 : break;
20667 : }
20668 : /* Avoid re-gimplifying the address operand if it is already
20669 : in suitable form. Re-gimplifying would mark the address
20670 : operand addressable. Always gimplify when not in SSA form
20671 : as we still may have to gimplify decls with value-exprs. */
20672 16178028 : if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
20673 17200101 : || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
20674 : {
20675 15211103 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20676 : is_gimple_mem_ref_addr, fb_rvalue);
20677 15211103 : if (ret == GS_ERROR)
20678 : break;
20679 : }
20680 16178028 : recalculate_side_effects (*expr_p);
20681 16178028 : ret = GS_ALL_DONE;
20682 16178028 : break;
20683 :
20684 : /* Constants need not be gimplified. */
20685 44850120 : case INTEGER_CST:
20686 44850120 : case REAL_CST:
20687 44850120 : case FIXED_CST:
20688 44850120 : case STRING_CST:
20689 44850120 : case COMPLEX_CST:
20690 44850120 : case VECTOR_CST:
20691 : /* Drop the overflow flag on constants, we do not want
20692 : that in the GIMPLE IL. */
20693 44850120 : if (TREE_OVERFLOW_P (*expr_p))
20694 886 : *expr_p = drop_tree_overflow (*expr_p);
20695 : ret = GS_ALL_DONE;
20696 : break;
20697 :
20698 118384 : case CONST_DECL:
20699 : /* If we require an lvalue, such as for ADDR_EXPR, retain the
20700 : CONST_DECL node. Otherwise the decl is replaceable by its
20701 : value. */
20702 : /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
20703 118384 : if (fallback & fb_lvalue)
20704 : ret = GS_ALL_DONE;
20705 : else
20706 : {
20707 3288 : *expr_p = DECL_INITIAL (*expr_p);
20708 3288 : ret = GS_OK;
20709 : }
20710 : break;
20711 :
20712 6627196 : case DECL_EXPR:
20713 6627196 : ret = gimplify_decl_expr (expr_p, pre_p);
20714 6627196 : break;
20715 :
20716 6070557 : case BIND_EXPR:
20717 6070557 : ret = gimplify_bind_expr (expr_p, pre_p);
20718 6070557 : break;
20719 :
20720 206812 : case LOOP_EXPR:
20721 206812 : ret = gimplify_loop_expr (expr_p, pre_p);
20722 206812 : break;
20723 :
20724 50248 : case SWITCH_EXPR:
20725 50248 : ret = gimplify_switch_expr (expr_p, pre_p);
20726 50248 : break;
20727 :
20728 3207 : case EXIT_EXPR:
20729 3207 : ret = gimplify_exit_expr (expr_p);
20730 3207 : break;
20731 :
20732 1119012 : case GOTO_EXPR:
20733 : /* If the target is not LABEL, then it is a computed jump
20734 : and the target needs to be gimplified. */
20735 1119012 : if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
20736 : {
20737 1137 : ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
20738 : NULL, is_gimple_val, fb_rvalue);
20739 1137 : if (ret == GS_ERROR)
20740 : break;
20741 : }
20742 2238022 : gimplify_seq_add_stmt (pre_p,
20743 1119011 : gimple_build_goto (GOTO_DESTINATION (*expr_p)));
20744 1119011 : ret = GS_ALL_DONE;
20745 1119011 : break;
20746 :
20747 104224 : case PREDICT_EXPR:
20748 416896 : gimplify_seq_add_stmt (pre_p,
20749 104224 : gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
20750 104224 : PREDICT_EXPR_OUTCOME (*expr_p)));
20751 104224 : ret = GS_ALL_DONE;
20752 104224 : break;
20753 :
20754 2603932 : case LABEL_EXPR:
20755 2603932 : ret = gimplify_label_expr (expr_p, pre_p);
20756 2603932 : label = LABEL_EXPR_LABEL (*expr_p);
20757 2603932 : gcc_assert (decl_function_context (label) == current_function_decl);
20758 :
20759 : /* If the label is used in a goto statement, or address of the label
20760 : is taken, we need to unpoison all variables that were seen so far.
20761 : Doing so would prevent us from reporting a false positives. */
20762 2603932 : if (asan_poisoned_variables
20763 5015 : && asan_used_labels != NULL
20764 1325 : && asan_used_labels->contains (label)
20765 2604844 : && !gimplify_omp_ctxp)
20766 904 : asan_poison_variables (asan_poisoned_variables, false, pre_p);
20767 : break;
20768 :
20769 1040508 : case CASE_LABEL_EXPR:
20770 1040508 : ret = gimplify_case_label_expr (expr_p, pre_p);
20771 :
20772 1040508 : if (gimplify_ctxp->live_switch_vars)
20773 1040321 : asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
20774 : pre_p);
20775 : break;
20776 :
20777 2301014 : case RETURN_EXPR:
20778 2301014 : ret = gimplify_return_expr (*expr_p, pre_p);
20779 2301014 : break;
20780 :
20781 980487 : case CONSTRUCTOR:
20782 : /* Don't reduce this in place; let gimplify_init_constructor work its
20783 : magic. Buf if we're just elaborating this for side effects, just
20784 : gimplify any element that has side-effects. */
20785 980487 : if (fallback == fb_none)
20786 : {
20787 404 : unsigned HOST_WIDE_INT ix;
20788 404 : tree val;
20789 404 : tree temp = NULL_TREE;
20790 421 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
20791 17 : if (TREE_SIDE_EFFECTS (val))
20792 4 : append_to_statement_list (val, &temp);
20793 :
20794 404 : *expr_p = temp;
20795 404 : ret = temp ? GS_OK : GS_ALL_DONE;
20796 : }
20797 : /* C99 code may assign to an array in a constructed
20798 : structure or union, and this has undefined behavior only
20799 : on execution, so create a temporary if an lvalue is
20800 : required. */
20801 980083 : else if (fallback == fb_lvalue)
20802 : {
20803 11 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20804 11 : mark_addressable (*expr_p);
20805 11 : ret = GS_OK;
20806 : }
20807 : else
20808 : ret = GS_ALL_DONE;
20809 : break;
20810 :
20811 : /* The following are special cases that are not handled by the
20812 : original GIMPLE grammar. */
20813 :
20814 : /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
20815 : eliminated. */
20816 443809 : case SAVE_EXPR:
20817 443809 : ret = gimplify_save_expr (expr_p, pre_p, post_p);
20818 443809 : break;
20819 :
20820 424159 : case BIT_FIELD_REF:
20821 424159 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20822 : post_p, is_gimple_lvalue, fb_either);
20823 424159 : recalculate_side_effects (*expr_p);
20824 424159 : break;
20825 :
20826 1980 : case TARGET_MEM_REF:
20827 1980 : {
20828 1980 : enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
20829 :
20830 1980 : if (TMR_BASE (*expr_p))
20831 1980 : r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
20832 : post_p, is_gimple_mem_ref_addr, fb_either);
20833 1980 : if (TMR_INDEX (*expr_p))
20834 1158 : r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
20835 : post_p, is_gimple_val, fb_rvalue);
20836 1980 : if (TMR_INDEX2 (*expr_p))
20837 69 : r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
20838 : post_p, is_gimple_val, fb_rvalue);
20839 : /* TMR_STEP and TMR_OFFSET are always integer constants. */
20840 1980 : ret = MIN (r0, r1);
20841 : }
20842 : break;
20843 :
20844 0 : case NON_LVALUE_EXPR:
20845 : /* This should have been stripped above. */
20846 0 : gcc_unreachable ();
20847 :
20848 83014 : case ASM_EXPR:
20849 83014 : ret = gimplify_asm_expr (expr_p, pre_p, post_p);
20850 83014 : break;
20851 :
20852 517098 : case TRY_FINALLY_EXPR:
20853 517098 : case TRY_CATCH_EXPR:
20854 517098 : {
20855 517098 : gimple_seq eval, cleanup;
20856 517098 : gtry *try_;
20857 :
20858 : /* Calls to destructors are generated automatically in FINALLY/CATCH
20859 : block. They should have location as UNKNOWN_LOCATION. However,
20860 : gimplify_call_expr will reset these call stmts to input_location
20861 : if it finds stmt's location is unknown. To prevent resetting for
20862 : destructors, we set the input_location to unknown.
20863 : Note that this only affects the destructor calls in FINALLY/CATCH
20864 : block, and will automatically reset to its original value by the
20865 : end of gimplify_expr. */
20866 517098 : input_location = UNKNOWN_LOCATION;
20867 517098 : eval = cleanup = NULL;
20868 517098 : gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
20869 517098 : bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
20870 517098 : if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20871 517098 : && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
20872 : {
20873 282 : gimple_seq n = NULL, e = NULL;
20874 282 : gimplify_ctxp->in_handler_expr = true;
20875 282 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20876 : 0), &n);
20877 282 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20878 : 1), &e);
20879 282 : if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
20880 : {
20881 276 : geh_else *stmt = gimple_build_eh_else (n, e);
20882 276 : gimple_seq_add_stmt (&cleanup, stmt);
20883 : }
20884 : }
20885 : else
20886 : {
20887 516816 : gimplify_ctxp->in_handler_expr = true;
20888 516816 : gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
20889 : }
20890 517098 : gimplify_ctxp->in_handler_expr = save_in_handler_expr;
20891 : /* Don't create bogus GIMPLE_TRY with empty cleanup. */
20892 517098 : if (gimple_seq_empty_p (cleanup))
20893 : {
20894 27618 : gimple_seq_add_seq (pre_p, eval);
20895 27618 : ret = GS_ALL_DONE;
20896 27618 : break;
20897 : }
20898 489480 : try_ = gimple_build_try (eval, cleanup,
20899 489480 : TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20900 : ? GIMPLE_TRY_FINALLY
20901 : : GIMPLE_TRY_CATCH);
20902 489480 : if (EXPR_HAS_LOCATION (save_expr))
20903 896430 : gimple_set_location (try_, EXPR_LOCATION (save_expr));
20904 41265 : else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
20905 30700 : gimple_set_location (try_, saved_location);
20906 489480 : if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
20907 174667 : gimple_try_set_catch_is_cleanup (try_,
20908 174667 : TRY_CATCH_IS_CLEANUP (*expr_p));
20909 489480 : gimplify_seq_add_stmt (pre_p, try_);
20910 489480 : ret = GS_ALL_DONE;
20911 489480 : break;
20912 : }
20913 :
20914 5441301 : case CLEANUP_POINT_EXPR:
20915 5441301 : ret = gimplify_cleanup_point_expr (expr_p, pre_p);
20916 5441301 : break;
20917 :
20918 863972 : case TARGET_EXPR:
20919 863972 : ret = gimplify_target_expr (expr_p, pre_p, post_p);
20920 863972 : break;
20921 :
20922 41033 : case CATCH_EXPR:
20923 41033 : {
20924 41033 : gimple *c;
20925 41033 : gimple_seq handler = NULL;
20926 41033 : gimplify_and_add (CATCH_BODY (*expr_p), &handler);
20927 41033 : c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
20928 41033 : gimplify_seq_add_stmt (pre_p, c);
20929 41033 : ret = GS_ALL_DONE;
20930 41033 : break;
20931 : }
20932 :
20933 5841 : case EH_FILTER_EXPR:
20934 5841 : {
20935 5841 : gimple *ehf;
20936 5841 : gimple_seq failure = NULL;
20937 :
20938 5841 : gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
20939 5841 : ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
20940 5841 : copy_warning (ehf, *expr_p);
20941 5841 : gimplify_seq_add_stmt (pre_p, ehf);
20942 5841 : ret = GS_ALL_DONE;
20943 5841 : break;
20944 : }
20945 :
20946 51417 : case OBJ_TYPE_REF:
20947 51417 : {
20948 51417 : enum gimplify_status r0, r1;
20949 51417 : r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
20950 : post_p, is_gimple_val, fb_rvalue);
20951 51417 : r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
20952 : post_p, is_gimple_val, fb_rvalue);
20953 51417 : TREE_SIDE_EFFECTS (*expr_p) = 0;
20954 51417 : ret = MIN (r0, r1);
20955 : }
20956 : break;
20957 :
20958 40972 : case LABEL_DECL:
20959 : /* We get here when taking the address of a label. We mark
20960 : the label as "forced"; meaning it can never be removed and
20961 : it is a potential target for any computed goto. */
20962 40972 : FORCED_LABEL (*expr_p) = 1;
20963 40972 : ret = GS_ALL_DONE;
20964 40972 : break;
20965 :
20966 8677906 : case STATEMENT_LIST:
20967 8677906 : ret = gimplify_statement_list (expr_p, pre_p);
20968 8677906 : break;
20969 :
20970 1739 : case WITH_SIZE_EXPR:
20971 1739 : {
20972 2021 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20973 : post_p == &internal_post ? NULL : post_p,
20974 : gimple_test_f, fallback);
20975 1739 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20976 : is_gimple_val, fb_rvalue);
20977 1739 : ret = GS_ALL_DONE;
20978 : }
20979 1739 : break;
20980 :
20981 111222247 : case VAR_DECL:
20982 111222247 : case PARM_DECL:
20983 111222247 : ret = gimplify_var_or_parm_decl (expr_p);
20984 111222247 : break;
20985 :
20986 348563 : case RESULT_DECL:
20987 : /* When within an OMP context, notice uses of variables. */
20988 348563 : if (gimplify_omp_ctxp)
20989 1412 : omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
20990 : /* Handlers can refer to the function result; if that has been
20991 : moved, we need to track it. */
20992 348563 : if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
20993 68 : *expr_p = gimplify_ctxp->return_temp;
20994 : ret = GS_ALL_DONE;
20995 : break;
20996 :
20997 0 : case DEBUG_EXPR_DECL:
20998 0 : gcc_unreachable ();
20999 :
21000 2507429 : case DEBUG_BEGIN_STMT:
21001 5014858 : gimplify_seq_add_stmt (pre_p,
21002 : gimple_build_debug_begin_stmt
21003 2507429 : (TREE_BLOCK (*expr_p),
21004 2507429 : EXPR_LOCATION (*expr_p)));
21005 2507429 : ret = GS_ALL_DONE;
21006 2507429 : *expr_p = NULL;
21007 2507429 : break;
21008 :
21009 : case SSA_NAME:
21010 : /* Allow callbacks into the gimplifier during optimization. */
21011 : ret = GS_ALL_DONE;
21012 : break;
21013 :
21014 18384 : case OMP_PARALLEL:
21015 18384 : gimplify_omp_parallel (expr_p, pre_p);
21016 18384 : ret = GS_ALL_DONE;
21017 18384 : break;
21018 :
21019 4109 : case OMP_TASK:
21020 4109 : gimplify_omp_task (expr_p, pre_p);
21021 4109 : ret = GS_ALL_DONE;
21022 4109 : break;
21023 :
21024 10695 : case OMP_SIMD:
21025 10695 : {
21026 : /* Temporarily disable into_ssa, as scan_omp_simd
21027 : which calls copy_gimple_seq_and_replace_locals can't deal
21028 : with SSA_NAMEs defined outside of the body properly. */
21029 10695 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
21030 10695 : gimplify_ctxp->into_ssa = false;
21031 10695 : ret = gimplify_omp_for (expr_p, pre_p);
21032 10695 : gimplify_ctxp->into_ssa = saved_into_ssa;
21033 10695 : break;
21034 : }
21035 :
21036 47573 : case OMP_FOR:
21037 47573 : case OMP_DISTRIBUTE:
21038 47573 : case OMP_TASKLOOP:
21039 47573 : case OMP_TILE:
21040 47573 : case OMP_UNROLL:
21041 47573 : case OACC_LOOP:
21042 47573 : ret = gimplify_omp_for (expr_p, pre_p);
21043 47573 : break;
21044 :
21045 1056 : case OMP_LOOP:
21046 1056 : ret = gimplify_omp_loop (expr_p, pre_p);
21047 1056 : break;
21048 :
21049 717 : case OACC_CACHE:
21050 717 : gimplify_oacc_cache (expr_p, pre_p);
21051 717 : ret = GS_ALL_DONE;
21052 717 : break;
21053 :
21054 254 : case OACC_DECLARE:
21055 254 : gimplify_oacc_declare (expr_p, pre_p);
21056 254 : ret = GS_ALL_DONE;
21057 254 : break;
21058 :
21059 40016 : case OACC_HOST_DATA:
21060 40016 : case OACC_DATA:
21061 40016 : case OACC_KERNELS:
21062 40016 : case OACC_PARALLEL:
21063 40016 : case OACC_SERIAL:
21064 40016 : case OMP_SCOPE:
21065 40016 : case OMP_SECTIONS:
21066 40016 : case OMP_SINGLE:
21067 40016 : case OMP_TARGET:
21068 40016 : case OMP_TARGET_DATA:
21069 40016 : case OMP_TEAMS:
21070 40016 : gimplify_omp_workshare (expr_p, pre_p);
21071 40016 : ret = GS_ALL_DONE;
21072 40016 : break;
21073 :
21074 12370 : case OACC_ENTER_DATA:
21075 12370 : case OACC_EXIT_DATA:
21076 12370 : case OACC_UPDATE:
21077 12370 : case OMP_TARGET_UPDATE:
21078 12370 : case OMP_TARGET_ENTER_DATA:
21079 12370 : case OMP_TARGET_EXIT_DATA:
21080 12370 : gimplify_omp_target_update (expr_p, pre_p);
21081 12370 : ret = GS_ALL_DONE;
21082 12370 : break;
21083 :
21084 7080 : case OMP_SECTION:
21085 7080 : case OMP_STRUCTURED_BLOCK:
21086 7080 : case OMP_MASTER:
21087 7080 : case OMP_MASKED:
21088 7080 : case OMP_ORDERED:
21089 7080 : case OMP_CRITICAL:
21090 7080 : case OMP_SCAN:
21091 7080 : {
21092 7080 : gimple_seq body = NULL;
21093 7080 : gimple *g;
21094 7080 : bool saved_in_omp_construct = in_omp_construct;
21095 :
21096 7080 : in_omp_construct = true;
21097 7080 : gimplify_and_add (OMP_BODY (*expr_p), &body);
21098 7080 : in_omp_construct = saved_in_omp_construct;
21099 7080 : switch (TREE_CODE (*expr_p))
21100 : {
21101 1279 : case OMP_SECTION:
21102 1279 : g = gimple_build_omp_section (body);
21103 1279 : break;
21104 791 : case OMP_STRUCTURED_BLOCK:
21105 791 : g = gimple_build_omp_structured_block (body);
21106 791 : break;
21107 874 : case OMP_MASTER:
21108 874 : g = gimple_build_omp_master (body);
21109 874 : break;
21110 1950 : case OMP_ORDERED:
21111 1950 : g = gimplify_omp_ordered (*expr_p, body);
21112 1950 : if (OMP_BODY (*expr_p) == NULL_TREE
21113 1950 : && gimple_code (g) == GIMPLE_OMP_ORDERED)
21114 1025 : gimple_omp_ordered_standalone (g);
21115 : break;
21116 497 : case OMP_MASKED:
21117 497 : gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
21118 : pre_p, ORT_WORKSHARE, OMP_MASKED);
21119 497 : gimplify_adjust_omp_clauses (pre_p, body,
21120 : &OMP_MASKED_CLAUSES (*expr_p),
21121 : OMP_MASKED);
21122 994 : g = gimple_build_omp_masked (body,
21123 497 : OMP_MASKED_CLAUSES (*expr_p));
21124 497 : break;
21125 547 : case OMP_CRITICAL:
21126 547 : gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
21127 : pre_p, ORT_WORKSHARE, OMP_CRITICAL);
21128 547 : gimplify_adjust_omp_clauses (pre_p, body,
21129 : &OMP_CRITICAL_CLAUSES (*expr_p),
21130 : OMP_CRITICAL);
21131 1641 : g = gimple_build_omp_critical (body,
21132 547 : OMP_CRITICAL_NAME (*expr_p),
21133 547 : OMP_CRITICAL_CLAUSES (*expr_p));
21134 547 : break;
21135 1142 : case OMP_SCAN:
21136 1142 : gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
21137 : pre_p, ORT_WORKSHARE, OMP_SCAN);
21138 1142 : gimplify_adjust_omp_clauses (pre_p, body,
21139 : &OMP_SCAN_CLAUSES (*expr_p),
21140 : OMP_SCAN);
21141 1142 : g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
21142 1142 : break;
21143 0 : default:
21144 0 : gcc_unreachable ();
21145 : }
21146 7080 : gimplify_seq_add_stmt (pre_p, g);
21147 7080 : ret = GS_ALL_DONE;
21148 7080 : break;
21149 : }
21150 :
21151 611 : case OMP_TASKGROUP:
21152 611 : {
21153 611 : gimple_seq body = NULL;
21154 :
21155 611 : tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
21156 611 : bool saved_in_omp_construct = in_omp_construct;
21157 611 : gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
21158 : OMP_TASKGROUP);
21159 611 : gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
21160 :
21161 611 : in_omp_construct = true;
21162 611 : gimplify_and_add (OMP_BODY (*expr_p), &body);
21163 611 : in_omp_construct = saved_in_omp_construct;
21164 611 : gimple_seq cleanup = NULL;
21165 611 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
21166 611 : gimple *g = gimple_build_call (fn, 0);
21167 611 : gimple_seq_add_stmt (&cleanup, g);
21168 611 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
21169 611 : body = NULL;
21170 611 : gimple_seq_add_stmt (&body, g);
21171 611 : g = gimple_build_omp_taskgroup (body, *pclauses);
21172 611 : gimplify_seq_add_stmt (pre_p, g);
21173 611 : ret = GS_ALL_DONE;
21174 611 : break;
21175 : }
21176 :
21177 612 : case OMP_INTEROP:
21178 612 : ret = gimplify_omp_interop (expr_p, pre_p);
21179 612 : break;
21180 10249 : case OMP_ATOMIC:
21181 10249 : case OMP_ATOMIC_READ:
21182 10249 : case OMP_ATOMIC_CAPTURE_OLD:
21183 10249 : case OMP_ATOMIC_CAPTURE_NEW:
21184 10249 : ret = gimplify_omp_atomic (expr_p, pre_p);
21185 10249 : break;
21186 :
21187 852 : case OMP_DISPATCH:
21188 852 : ret = gimplify_omp_dispatch (expr_p, pre_p);
21189 852 : break;
21190 :
21191 174 : case OMP_METADIRECTIVE:
21192 174 : ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
21193 : gimple_test_f, fallback);
21194 174 : break;
21195 :
21196 36 : case OMP_NEXT_VARIANT:
21197 36 : case OMP_TARGET_DEVICE_MATCHES:
21198 : /* These are placeholders for constants. There's nothing to do with
21199 : them here but we must mark the containing function as needing
21200 : to run the ompdevlow pass to resolve them. Note that
21201 : OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
21202 : the front ends. */
21203 36 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
21204 36 : ret = GS_ALL_DONE;
21205 36 : break;
21206 :
21207 0 : case OMP_DECLARE_MAPPER:
21208 0 : ret = gimplify_omp_declare_mapper (expr_p);
21209 0 : break;
21210 :
21211 478 : case TRANSACTION_EXPR:
21212 478 : ret = gimplify_transaction (expr_p, pre_p);
21213 478 : break;
21214 :
21215 302878 : case TRUTH_AND_EXPR:
21216 302878 : case TRUTH_OR_EXPR:
21217 302878 : case TRUTH_XOR_EXPR:
21218 302878 : {
21219 302878 : tree orig_type = TREE_TYPE (*expr_p);
21220 302878 : tree new_type, xop0, xop1;
21221 302878 : *expr_p = gimple_boolify (*expr_p);
21222 302878 : new_type = TREE_TYPE (*expr_p);
21223 302878 : if (!useless_type_conversion_p (orig_type, new_type))
21224 : {
21225 1166 : *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
21226 1166 : ret = GS_OK;
21227 1166 : break;
21228 : }
21229 :
21230 : /* Boolified binary truth expressions are semantically equivalent
21231 : to bitwise binary expressions. Canonicalize them to the
21232 : bitwise variant. */
21233 301712 : switch (TREE_CODE (*expr_p))
21234 : {
21235 177042 : case TRUTH_AND_EXPR:
21236 177042 : TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
21237 177042 : break;
21238 124046 : case TRUTH_OR_EXPR:
21239 124046 : TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
21240 124046 : break;
21241 624 : case TRUTH_XOR_EXPR:
21242 624 : TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
21243 624 : break;
21244 : default:
21245 : break;
21246 : }
21247 : /* Now make sure that operands have compatible type to
21248 : expression's new_type. */
21249 301712 : xop0 = TREE_OPERAND (*expr_p, 0);
21250 301712 : xop1 = TREE_OPERAND (*expr_p, 1);
21251 301712 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
21252 326 : TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
21253 : new_type,
21254 : xop0);
21255 301712 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
21256 366 : TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
21257 : new_type,
21258 : xop1);
21259 : /* Continue classified as tcc_binary. */
21260 301712 : goto expr_2;
21261 : }
21262 :
21263 11375 : case VEC_COND_EXPR:
21264 11375 : goto expr_3;
21265 :
21266 134306 : case VEC_PERM_EXPR:
21267 : /* Classified as tcc_expression. */
21268 134306 : goto expr_3;
21269 :
21270 48 : case BIT_INSERT_EXPR:
21271 : /* Argument 3 is a constant. */
21272 48 : goto expr_2;
21273 :
21274 3875807 : case POINTER_PLUS_EXPR:
21275 3875807 : {
21276 3875807 : enum gimplify_status r0, r1;
21277 3875807 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21278 : post_p, is_gimple_val, fb_rvalue);
21279 3875807 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21280 : post_p, is_gimple_val, fb_rvalue);
21281 3875807 : recalculate_side_effects (*expr_p);
21282 3875807 : ret = MIN (r0, r1);
21283 : break;
21284 : }
21285 :
21286 45182471 : default:
21287 45182471 : switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
21288 : {
21289 7118729 : case tcc_comparison:
21290 : /* Handle comparison of objects of non scalar mode aggregates
21291 : with a call to memcmp. It would be nice to only have to do
21292 : this for variable-sized objects, but then we'd have to allow
21293 : the same nest of reference nodes we allow for MODIFY_EXPR and
21294 : that's too complex.
21295 :
21296 : Compare scalar mode aggregates as scalar mode values. Using
21297 : memcmp for them would be very inefficient at best, and is
21298 : plain wrong if bitfields are involved. */
21299 7118729 : if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
21300 : ret = GS_ERROR;
21301 : else
21302 : {
21303 7118729 : tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
21304 :
21305 : /* Vector comparisons need no boolification. */
21306 7118729 : if (TREE_CODE (type) == VECTOR_TYPE)
21307 13413 : goto expr_2;
21308 7105316 : else if (!AGGREGATE_TYPE_P (type))
21309 : {
21310 7105300 : tree org_type = TREE_TYPE (*expr_p);
21311 7105300 : *expr_p = gimple_boolify (*expr_p);
21312 7105300 : if (!useless_type_conversion_p (org_type,
21313 7105300 : TREE_TYPE (*expr_p)))
21314 : {
21315 57715 : *expr_p = fold_convert_loc (input_location,
21316 : org_type, *expr_p);
21317 57715 : ret = GS_OK;
21318 : }
21319 : else
21320 7047585 : goto expr_2;
21321 : }
21322 16 : else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
21323 16 : ret = gimplify_scalar_mode_aggregate_compare (expr_p);
21324 : else
21325 0 : ret = gimplify_variable_sized_compare (expr_p);
21326 : }
21327 : break;
21328 :
21329 : /* If *EXPR_P does not need to be special-cased, handle it
21330 : according to its class. */
21331 1627627 : case tcc_unary:
21332 1627627 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21333 : post_p, is_gimple_val, fb_rvalue);
21334 1627627 : break;
21335 :
21336 26749478 : case tcc_binary:
21337 26749478 : expr_2:
21338 26749478 : {
21339 26749478 : enum gimplify_status r0, r1;
21340 :
21341 26749478 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21342 : post_p, is_gimple_val, fb_rvalue);
21343 26749478 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21344 : post_p, is_gimple_val, fb_rvalue);
21345 :
21346 26749478 : ret = MIN (r0, r1);
21347 : break;
21348 : }
21349 :
21350 145681 : expr_3:
21351 145681 : {
21352 145681 : enum gimplify_status r0, r1, r2;
21353 :
21354 145681 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21355 : post_p, is_gimple_val, fb_rvalue);
21356 145681 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21357 : post_p, is_gimple_val, fb_rvalue);
21358 145681 : r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
21359 : post_p, is_gimple_val, fb_rvalue);
21360 :
21361 145681 : ret = MIN (MIN (r0, r1), r2);
21362 : break;
21363 : }
21364 :
21365 17049395 : case tcc_declaration:
21366 17049395 : case tcc_constant:
21367 17049395 : ret = GS_ALL_DONE;
21368 17049395 : goto dont_recalculate;
21369 :
21370 0 : default:
21371 0 : gcc_unreachable ();
21372 : }
21373 :
21374 28580517 : recalculate_side_effects (*expr_p);
21375 :
21376 489002550 : dont_recalculate:
21377 : break;
21378 : }
21379 :
21380 489002550 : gcc_assert (*expr_p || ret != GS_OK);
21381 : }
21382 493956130 : while (ret == GS_OK);
21383 :
21384 : /* If we encountered an error_mark somewhere nested inside, either
21385 : stub out the statement or propagate the error back out. */
21386 475773478 : if (ret == GS_ERROR)
21387 : {
21388 5304 : if (is_statement)
21389 4081 : *expr_p = NULL;
21390 5304 : goto out;
21391 : }
21392 :
21393 : /* This was only valid as a return value from the langhook, which
21394 : we handled. Make sure it doesn't escape from any other context. */
21395 475768174 : gcc_assert (ret != GS_UNHANDLED);
21396 :
21397 475768174 : if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
21398 : {
21399 : /* We aren't looking for a value, and we don't have a valid
21400 : statement. If it doesn't have side-effects, throw it away.
21401 : We can also get here with code such as "*&&L;", where L is
21402 : a LABEL_DECL that is marked as FORCED_LABEL. */
21403 1231690 : if (TREE_CODE (*expr_p) == LABEL_DECL
21404 1231690 : || !TREE_SIDE_EFFECTS (*expr_p))
21405 1230674 : *expr_p = NULL;
21406 1016 : else if (!TREE_THIS_VOLATILE (*expr_p))
21407 : {
21408 : /* This is probably a _REF that contains something nested that
21409 : has side effects. Recurse through the operands to find it. */
21410 0 : enum tree_code code = TREE_CODE (*expr_p);
21411 :
21412 0 : switch (code)
21413 : {
21414 0 : case COMPONENT_REF:
21415 0 : case REALPART_EXPR:
21416 0 : case IMAGPART_EXPR:
21417 0 : case VIEW_CONVERT_EXPR:
21418 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21419 : gimple_test_f, fallback);
21420 0 : break;
21421 :
21422 0 : case ARRAY_REF:
21423 0 : case ARRAY_RANGE_REF:
21424 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21425 : gimple_test_f, fallback);
21426 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
21427 : gimple_test_f, fallback);
21428 0 : break;
21429 :
21430 0 : default:
21431 : /* Anything else with side-effects must be converted to
21432 : a valid statement before we get here. */
21433 0 : gcc_unreachable ();
21434 : }
21435 :
21436 0 : *expr_p = NULL;
21437 : }
21438 1016 : else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
21439 966 : && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
21440 1969 : && !is_empty_type (TREE_TYPE (*expr_p)))
21441 : {
21442 : /* Historically, the compiler has treated a bare reference
21443 : to a non-BLKmode volatile lvalue as forcing a load. */
21444 893 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
21445 :
21446 : /* Normally, we do not want to create a temporary for a
21447 : TREE_ADDRESSABLE type because such a type should not be
21448 : copied by bitwise-assignment. However, we make an
21449 : exception here, as all we are doing here is ensuring that
21450 : we read the bytes that make up the type. We use
21451 : create_tmp_var_raw because create_tmp_var will abort when
21452 : given a TREE_ADDRESSABLE type. */
21453 893 : tree tmp = create_tmp_var_raw (type, "vol");
21454 893 : gimple_add_tmp_var (tmp);
21455 893 : gimplify_assign (tmp, *expr_p, pre_p);
21456 893 : *expr_p = NULL;
21457 : }
21458 : else
21459 : /* We can't do anything useful with a volatile reference to
21460 : an incomplete type, so just throw it away. Likewise for
21461 : a BLKmode type, since any implicit inner load should
21462 : already have been turned into an explicit one by the
21463 : gimplification process. */
21464 123 : *expr_p = NULL;
21465 : }
21466 :
21467 : /* If we are gimplifying at the statement level, we're done. Tack
21468 : everything together and return. */
21469 475768174 : if (fallback == fb_none || is_statement)
21470 : {
21471 : /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
21472 : it out for GC to reclaim it. */
21473 103181485 : *expr_p = NULL_TREE;
21474 :
21475 103181485 : if (!gimple_seq_empty_p (internal_pre)
21476 103181485 : || !gimple_seq_empty_p (internal_post))
21477 : {
21478 19 : gimplify_seq_add_seq (&internal_pre, internal_post);
21479 19 : gimplify_seq_add_seq (pre_p, internal_pre);
21480 : }
21481 :
21482 : /* The result of gimplifying *EXPR_P is going to be the last few
21483 : statements in *PRE_P and *POST_P. Add location information
21484 : to all the statements that were added by the gimplification
21485 : helpers. */
21486 103181485 : if (!gimple_seq_empty_p (*pre_p))
21487 100650786 : annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
21488 :
21489 103181485 : if (!gimple_seq_empty_p (*post_p))
21490 19 : annotate_all_with_location_after (*post_p, post_last_gsi,
21491 : input_location);
21492 :
21493 103181485 : goto out;
21494 : }
21495 :
21496 : #ifdef ENABLE_GIMPLE_CHECKING
21497 372586689 : if (*expr_p)
21498 : {
21499 372586689 : enum tree_code code = TREE_CODE (*expr_p);
21500 : /* These expressions should already be in gimple IR form. */
21501 372586689 : gcc_assert (code != MODIFY_EXPR
21502 : && code != ASM_EXPR
21503 : && code != BIND_EXPR
21504 : && code != CATCH_EXPR
21505 : && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
21506 : && code != EH_FILTER_EXPR
21507 : && code != GOTO_EXPR
21508 : && code != LABEL_EXPR
21509 : && code != LOOP_EXPR
21510 : && code != SWITCH_EXPR
21511 : && code != TRY_FINALLY_EXPR
21512 : && code != EH_ELSE_EXPR
21513 : && code != OACC_PARALLEL
21514 : && code != OACC_KERNELS
21515 : && code != OACC_SERIAL
21516 : && code != OACC_DATA
21517 : && code != OACC_HOST_DATA
21518 : && code != OACC_DECLARE
21519 : && code != OACC_UPDATE
21520 : && code != OACC_ENTER_DATA
21521 : && code != OACC_EXIT_DATA
21522 : && code != OACC_CACHE
21523 : && code != OMP_CRITICAL
21524 : && code != OMP_FOR
21525 : && code != OACC_LOOP
21526 : && code != OMP_MASTER
21527 : && code != OMP_MASKED
21528 : && code != OMP_TASKGROUP
21529 : && code != OMP_ORDERED
21530 : && code != OMP_PARALLEL
21531 : && code != OMP_SCAN
21532 : && code != OMP_SECTIONS
21533 : && code != OMP_SECTION
21534 : && code != OMP_STRUCTURED_BLOCK
21535 : && code != OMP_SINGLE
21536 : && code != OMP_SCOPE
21537 : && code != OMP_DISPATCH);
21538 : }
21539 : #endif
21540 :
21541 : /* Otherwise we're gimplifying a subexpression, so the resulting
21542 : value is interesting. If it's a valid operand that matches
21543 : GIMPLE_TEST_F, we're done. Unless we are handling some
21544 : post-effects internally; if that's the case, we need to copy into
21545 : a temporary before adding the post-effects to POST_P. */
21546 372586689 : if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
21547 346093404 : goto out;
21548 :
21549 : /* Otherwise, we need to create a new temporary for the gimplified
21550 : expression. */
21551 :
21552 : /* We can't return an lvalue if we have an internal postqueue. The
21553 : object the lvalue refers to would (probably) be modified by the
21554 : postqueue; we need to copy the value out first, which means an
21555 : rvalue. */
21556 26493285 : if ((fallback & fb_lvalue)
21557 597241 : && gimple_seq_empty_p (internal_post)
21558 27090526 : && is_gimple_addressable (*expr_p))
21559 : {
21560 : /* An lvalue will do. Take the address of the expression, store it
21561 : in a temporary, and replace the expression with an INDIRECT_REF of
21562 : that temporary. */
21563 15 : tree ref_alias_type = reference_alias_ptr_type (*expr_p);
21564 15 : unsigned int ref_align = get_object_alignment (*expr_p);
21565 15 : tree ref_type = TREE_TYPE (*expr_p);
21566 15 : tmp = build_fold_addr_expr_loc (input_location, *expr_p);
21567 15 : gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
21568 15 : if (TYPE_ALIGN (ref_type) != ref_align)
21569 1 : ref_type = build_aligned_type (ref_type, ref_align);
21570 15 : *expr_p = build2 (MEM_REF, ref_type,
21571 : tmp, build_zero_cst (ref_alias_type));
21572 : }
21573 26493270 : else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
21574 : {
21575 : /* An rvalue will do. Assign the gimplified expression into a
21576 : new temporary TMP and replace the original expression with
21577 : TMP. First, make sure that the expression has a type so that
21578 : it can be assigned into a temporary. */
21579 26493262 : gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
21580 26493262 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
21581 : }
21582 : else
21583 : {
21584 : #ifdef ENABLE_GIMPLE_CHECKING
21585 8 : if (!(fallback & fb_mayfail))
21586 : {
21587 0 : fprintf (stderr, "gimplification failed:\n");
21588 0 : print_generic_expr (stderr, *expr_p);
21589 0 : debug_tree (*expr_p);
21590 0 : internal_error ("gimplification failed");
21591 : }
21592 : #endif
21593 8 : gcc_assert (fallback & fb_mayfail);
21594 :
21595 : /* If this is an asm statement, and the user asked for the
21596 : impossible, don't die. Fail and let gimplify_asm_expr
21597 : issue an error. */
21598 8 : ret = GS_ERROR;
21599 8 : goto out;
21600 : }
21601 :
21602 : /* Make sure the temporary matches our predicate. */
21603 26493277 : gcc_assert ((*gimple_test_f) (*expr_p));
21604 :
21605 26493277 : if (!gimple_seq_empty_p (internal_post))
21606 : {
21607 0 : annotate_all_with_location (internal_post, input_location);
21608 0 : gimplify_seq_add_seq (pre_p, internal_post);
21609 : }
21610 :
21611 26493277 : out:
21612 475773478 : input_location = saved_location;
21613 475773478 : return ret;
21614 : }
21615 :
21616 : /* Like gimplify_expr but make sure the gimplified result is not itself
21617 : a SSA name (but a decl if it were). Temporaries required by
21618 : evaluating *EXPR_P may be still SSA names. */
21619 :
21620 : static enum gimplify_status
21621 34951399 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
21622 : bool (*gimple_test_f) (tree), fallback_t fallback,
21623 : bool allow_ssa)
21624 : {
21625 34951399 : enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
21626 : gimple_test_f, fallback);
21627 34951399 : if (! allow_ssa
21628 266827 : && TREE_CODE (*expr_p) == SSA_NAME)
21629 68648 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
21630 34951399 : return ret;
21631 : }
21632 :
21633 : /* Look through TYPE for variable-sized objects and gimplify each such
21634 : size that we find. Add to LIST_P any statements generated. */
21635 :
21636 : void
21637 8452482 : gimplify_type_sizes (tree type, gimple_seq *list_p)
21638 : {
21639 8452482 : if (type == NULL || type == error_mark_node)
21640 : return;
21641 :
21642 8452236 : const bool ignored_p
21643 8452236 : = TYPE_NAME (type)
21644 4194627 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21645 12417865 : && DECL_IGNORED_P (TYPE_NAME (type));
21646 8452236 : tree t;
21647 :
21648 : /* We first do the main variant, then copy into any other variants. */
21649 8452236 : type = TYPE_MAIN_VARIANT (type);
21650 :
21651 : /* Avoid infinite recursion. */
21652 8452236 : if (TYPE_SIZES_GIMPLIFIED (type))
21653 : return;
21654 :
21655 2518422 : TYPE_SIZES_GIMPLIFIED (type) = 1;
21656 :
21657 2518422 : switch (TREE_CODE (type))
21658 : {
21659 498698 : case INTEGER_TYPE:
21660 498698 : case ENUMERAL_TYPE:
21661 498698 : case BOOLEAN_TYPE:
21662 498698 : case REAL_TYPE:
21663 498698 : case FIXED_POINT_TYPE:
21664 498698 : gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
21665 498698 : gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
21666 :
21667 10256446 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21668 : {
21669 9757748 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
21670 9757748 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
21671 : }
21672 : break;
21673 :
21674 183070 : case ARRAY_TYPE:
21675 : /* These types may not have declarations, so handle them here. */
21676 183070 : gimplify_type_sizes (TREE_TYPE (type), list_p);
21677 183070 : gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
21678 : /* Ensure VLA bounds aren't removed, for -O0 they should be variables
21679 : with assigned stack slots, for -O1+ -g they should be tracked
21680 : by VTA. */
21681 183070 : if (!ignored_p
21682 183070 : && TYPE_DOMAIN (type)
21683 365915 : && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
21684 : {
21685 182845 : t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
21686 182845 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21687 0 : DECL_IGNORED_P (t) = 0;
21688 182845 : t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21689 182845 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21690 19052 : DECL_IGNORED_P (t) = 0;
21691 : }
21692 : break;
21693 :
21694 511420 : case RECORD_TYPE:
21695 511420 : case UNION_TYPE:
21696 511420 : case QUAL_UNION_TYPE:
21697 11362066 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
21698 10850646 : if (TREE_CODE (field) == FIELD_DECL)
21699 : {
21700 1111927 : gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
21701 : /* Likewise, ensure variable offsets aren't removed. */
21702 1111927 : if (!ignored_p
21703 1111927 : && (t = DECL_FIELD_OFFSET (field))
21704 1111915 : && VAR_P (t)
21705 1112089 : && DECL_ARTIFICIAL (t))
21706 162 : DECL_IGNORED_P (t) = 0;
21707 1111927 : gimplify_one_sizepos (&DECL_SIZE (field), list_p);
21708 1111927 : gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
21709 1111927 : gimplify_type_sizes (TREE_TYPE (field), list_p);
21710 : }
21711 : break;
21712 :
21713 : case POINTER_TYPE:
21714 : case REFERENCE_TYPE:
21715 : /* We used to recurse on the pointed-to type here, which turned out to
21716 : be incorrect because its definition might refer to variables not
21717 : yet initialized at this point if a forward declaration is involved.
21718 :
21719 : It was actually useful for anonymous pointed-to types to ensure
21720 : that the sizes evaluation dominates every possible later use of the
21721 : values. Restricting to such types here would be safe since there
21722 : is no possible forward declaration around, but would introduce an
21723 : undesirable middle-end semantic to anonymity. We then defer to
21724 : front-ends the responsibility of ensuring that the sizes are
21725 : evaluated both early and late enough, e.g. by attaching artificial
21726 : type declarations to the tree. */
21727 : break;
21728 :
21729 : default:
21730 : break;
21731 : }
21732 :
21733 2518422 : gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
21734 2518422 : gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
21735 :
21736 16922386 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21737 : {
21738 14403964 : TYPE_SIZE (t) = TYPE_SIZE (type);
21739 14403964 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
21740 14403964 : TYPE_SIZES_GIMPLIFIED (t) = 1;
21741 : }
21742 : }
21743 :
21744 : /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
21745 : a size or position, has had all of its SAVE_EXPRs evaluated.
21746 : We add any required statements to *STMT_P. */
21747 :
21748 : void
21749 9388267 : gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
21750 : {
21751 9388267 : tree expr = *expr_p;
21752 :
21753 : /* We don't do anything if the value isn't there, is constant, or contains
21754 : A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
21755 : a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
21756 : will want to replace it with a new variable, but that will cause problems
21757 : if this type is from outside the function. It's OK to have that here. */
21758 9388267 : if (expr == NULL_TREE
21759 9469521 : || is_gimple_constant (expr)
21760 81254 : || VAR_P (expr)
21761 9465478 : || CONTAINS_PLACEHOLDER_P (expr))
21762 : return;
21763 :
21764 77211 : *expr_p = unshare_expr (expr);
21765 :
21766 : /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
21767 : if the def vanishes. */
21768 77211 : gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
21769 :
21770 : /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
21771 : FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
21772 : as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
21773 77211 : if (is_gimple_constant (*expr_p))
21774 548 : *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
21775 : }
21776 :
21777 : /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
21778 : containing the sequence of corresponding GIMPLE statements. If DO_PARMS
21779 : is true, also gimplify the parameters. */
21780 :
21781 : gbind *
21782 3025771 : gimplify_body (tree fndecl, bool do_parms)
21783 : {
21784 3025771 : location_t saved_location = input_location;
21785 3025771 : gimple_seq parm_stmts, parm_cleanup = NULL, seq;
21786 3025771 : gimple *outer_stmt;
21787 3025771 : gbind *outer_bind;
21788 :
21789 3025771 : timevar_push (TV_TREE_GIMPLIFY);
21790 :
21791 3025771 : init_tree_ssa (cfun);
21792 :
21793 : /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
21794 : gimplification. */
21795 3025771 : default_rtl_profile ();
21796 :
21797 3025771 : gcc_assert (gimplify_ctxp == NULL);
21798 3025771 : push_gimplify_context (true);
21799 :
21800 3025771 : if (flag_openacc || flag_openmp)
21801 : {
21802 55159 : gcc_assert (gimplify_omp_ctxp == NULL);
21803 55159 : if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
21804 9735 : gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
21805 : }
21806 :
21807 : /* Unshare most shared trees in the body and in that of any nested functions.
21808 : It would seem we don't have to do this for nested functions because
21809 : they are supposed to be output and then the outer function gimplified
21810 : first, but the g++ front end doesn't always do it that way. */
21811 3025771 : unshare_body (fndecl);
21812 3025771 : unvisit_body (fndecl);
21813 :
21814 : /* Make sure input_location isn't set to something weird. */
21815 3025771 : input_location = DECL_SOURCE_LOCATION (fndecl);
21816 :
21817 : /* Resolve callee-copies. This has to be done before processing
21818 : the body so that DECL_VALUE_EXPR gets processed correctly. */
21819 3025771 : parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
21820 :
21821 : /* Gimplify the function's body. */
21822 3025771 : seq = NULL;
21823 3025771 : gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
21824 3025771 : outer_stmt = gimple_seq_first_nondebug_stmt (seq);
21825 3025771 : if (!outer_stmt)
21826 : {
21827 26818 : outer_stmt = gimple_build_nop ();
21828 26818 : gimplify_seq_add_stmt (&seq, outer_stmt);
21829 : }
21830 :
21831 : /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
21832 : not the case, wrap everything in a GIMPLE_BIND to make it so. */
21833 3025771 : if (gimple_code (outer_stmt) == GIMPLE_BIND
21834 3025771 : && (gimple_seq_first_nondebug_stmt (seq)
21835 1735641 : == gimple_seq_last_nondebug_stmt (seq)))
21836 : {
21837 1670992 : outer_bind = as_a <gbind *> (outer_stmt);
21838 1670992 : if (gimple_seq_first_stmt (seq) != outer_stmt
21839 1670992 : || gimple_seq_last_stmt (seq) != outer_stmt)
21840 : {
21841 : /* If there are debug stmts before or after outer_stmt, move them
21842 : inside of outer_bind body. */
21843 1 : gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
21844 1 : gimple_seq second_seq = NULL;
21845 1 : if (gimple_seq_first_stmt (seq) != outer_stmt
21846 2 : && gimple_seq_last_stmt (seq) != outer_stmt)
21847 : {
21848 0 : second_seq = gsi_split_seq_after (gsi);
21849 0 : gsi_remove (&gsi, false);
21850 : }
21851 1 : else if (gimple_seq_first_stmt (seq) != outer_stmt)
21852 1 : gsi_remove (&gsi, false);
21853 : else
21854 : {
21855 0 : gsi_remove (&gsi, false);
21856 0 : second_seq = seq;
21857 0 : seq = NULL;
21858 : }
21859 1 : gimple_seq_add_seq_without_update (&seq,
21860 : gimple_bind_body (outer_bind));
21861 1 : gimple_seq_add_seq_without_update (&seq, second_seq);
21862 1 : gimple_bind_set_body (outer_bind, seq);
21863 : }
21864 : }
21865 : else
21866 1354779 : outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
21867 :
21868 3025771 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21869 :
21870 : /* If we had callee-copies statements, insert them at the beginning
21871 : of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters. */
21872 3025771 : if (!gimple_seq_empty_p (parm_stmts))
21873 : {
21874 41 : tree parm;
21875 :
21876 41 : gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
21877 41 : if (parm_cleanup)
21878 : {
21879 0 : gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
21880 : GIMPLE_TRY_FINALLY);
21881 0 : parm_stmts = NULL;
21882 0 : gimple_seq_add_stmt (&parm_stmts, g);
21883 : }
21884 41 : gimple_bind_set_body (outer_bind, parm_stmts);
21885 :
21886 41 : for (parm = DECL_ARGUMENTS (current_function_decl);
21887 101 : parm; parm = DECL_CHAIN (parm))
21888 60 : if (DECL_HAS_VALUE_EXPR_P (parm))
21889 : {
21890 0 : DECL_HAS_VALUE_EXPR_P (parm) = 0;
21891 0 : DECL_IGNORED_P (parm) = 0;
21892 : }
21893 : }
21894 :
21895 3025771 : if ((flag_openacc || flag_openmp || flag_openmp_simd)
21896 57416 : && gimplify_omp_ctxp)
21897 : {
21898 9788 : delete_omp_context (gimplify_omp_ctxp);
21899 9788 : gimplify_omp_ctxp = NULL;
21900 : }
21901 :
21902 3025771 : pop_gimplify_context (outer_bind);
21903 3025771 : gcc_assert (gimplify_ctxp == NULL);
21904 :
21905 3025771 : if (flag_checking && !seen_error ())
21906 2974488 : verify_gimple_in_seq (gimple_bind_body (outer_bind));
21907 :
21908 3025771 : timevar_pop (TV_TREE_GIMPLIFY);
21909 3025771 : input_location = saved_location;
21910 :
21911 3025771 : return outer_bind;
21912 : }
21913 :
21914 : typedef char *char_p; /* For DEF_VEC_P. */
21915 :
21916 : /* Return whether we should exclude FNDECL from instrumentation. */
21917 :
21918 : static bool
21919 43 : flag_instrument_functions_exclude_p (tree fndecl)
21920 : {
21921 43 : vec<char_p> *v;
21922 :
21923 43 : v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
21924 44 : if (v && v->length () > 0)
21925 : {
21926 1 : const char *name;
21927 1 : int i;
21928 1 : char *s;
21929 :
21930 1 : name = lang_hooks.decl_printable_name (fndecl, 1);
21931 2 : FOR_EACH_VEC_ELT (*v, i, s)
21932 1 : if (strstr (name, s) != NULL)
21933 43 : return true;
21934 : }
21935 :
21936 42 : v = (vec<char_p> *) flag_instrument_functions_exclude_files;
21937 43 : if (v && v->length () > 0)
21938 : {
21939 1 : const char *name;
21940 1 : int i;
21941 1 : char *s;
21942 :
21943 1 : name = DECL_SOURCE_FILE (fndecl);
21944 1 : FOR_EACH_VEC_ELT (*v, i, s)
21945 1 : if (strstr (name, s) != NULL)
21946 43 : return true;
21947 : }
21948 :
21949 : return false;
21950 : }
21951 :
21952 : /* Build a call to the instrumentation function FNCODE and add it to SEQ.
21953 : If COND_VAR is not NULL, it is a boolean variable guarding the call to
21954 : the instrumentation function. IF STMT is not NULL, it is a statement
21955 : to be executed just before the call to the instrumentation function. */
21956 :
21957 : static void
21958 82 : build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
21959 : tree cond_var, gimple *stmt)
21960 : {
21961 : /* The instrumentation hooks aren't going to call the instrumented
21962 : function and the address they receive is expected to be matchable
21963 : against symbol addresses. Make sure we don't create a trampoline,
21964 : in case the current function is nested. */
21965 82 : tree this_fn_addr = build_fold_addr_expr (current_function_decl);
21966 82 : TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
21967 :
21968 82 : tree label_true, label_false;
21969 82 : if (cond_var)
21970 : {
21971 20 : label_true = create_artificial_label (UNKNOWN_LOCATION);
21972 20 : label_false = create_artificial_label (UNKNOWN_LOCATION);
21973 20 : gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
21974 : label_true, label_false);
21975 20 : gimplify_seq_add_stmt (seq, cond);
21976 20 : gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
21977 20 : gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
21978 : NOT_TAKEN));
21979 : }
21980 :
21981 82 : if (stmt)
21982 10 : gimplify_seq_add_stmt (seq, stmt);
21983 :
21984 82 : tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
21985 82 : gcall *call = gimple_build_call (x, 1, integer_zero_node);
21986 82 : tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
21987 82 : gimple_call_set_lhs (call, tmp_var);
21988 82 : gimplify_seq_add_stmt (seq, call);
21989 82 : x = builtin_decl_implicit (fncode);
21990 82 : call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
21991 82 : gimplify_seq_add_stmt (seq, call);
21992 :
21993 82 : if (cond_var)
21994 20 : gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
21995 82 : }
21996 :
21997 : /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
21998 : node for the function we want to gimplify.
21999 :
22000 : Return the sequence of GIMPLE statements corresponding to the body
22001 : of FNDECL. */
22002 :
22003 : void
22004 3025267 : gimplify_function_tree (tree fndecl)
22005 : {
22006 3025267 : gimple_seq seq;
22007 3025267 : gbind *bind;
22008 :
22009 3025267 : gcc_assert (!gimple_body (fndecl));
22010 :
22011 3025267 : if (DECL_STRUCT_FUNCTION (fndecl))
22012 3020868 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
22013 : else
22014 4399 : push_struct_function (fndecl);
22015 :
22016 3025267 : reset_cond_uid ();
22017 3025267 : if (cond_uids)
22018 : {
22019 68 : delete cond_uids;
22020 68 : cond_uids = NULL;
22021 : }
22022 :
22023 : /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
22024 : if necessary. */
22025 3025267 : cfun->curr_properties |= PROP_gimple_lva;
22026 :
22027 3025267 : if (asan_sanitize_use_after_scope ())
22028 8605 : asan_poisoned_variables = new hash_set<tree> ();
22029 3025267 : if (flag_openmp)
22030 47424 : omp_resolved_variant_calls = new hash_set<tree> ();
22031 :
22032 3025267 : bind = gimplify_body (fndecl, true);
22033 :
22034 3025267 : if (omp_resolved_variant_calls)
22035 : {
22036 47424 : delete omp_resolved_variant_calls;
22037 47424 : omp_resolved_variant_calls = NULL;
22038 : }
22039 3025267 : if (asan_poisoned_variables)
22040 : {
22041 8605 : delete asan_poisoned_variables;
22042 8605 : asan_poisoned_variables = NULL;
22043 : }
22044 :
22045 : /* The tree body of the function is no longer needed, replace it
22046 : with the new GIMPLE body. */
22047 3025267 : seq = NULL;
22048 3025267 : gimple_seq_add_stmt (&seq, bind);
22049 3025267 : gimple_set_body (fndecl, seq);
22050 :
22051 : /* If we're instrumenting function entry/exit, then prepend the call to
22052 : the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
22053 : catch the exit hook. */
22054 : /* ??? Add some way to ignore exceptions for this TFE. */
22055 3025267 : if (flag_instrument_function_entry_exit
22056 97 : && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
22057 : /* Do not instrument extern inline functions. */
22058 44 : && !(DECL_DECLARED_INLINE_P (fndecl)
22059 6 : && DECL_EXTERNAL (fndecl)
22060 1 : && DECL_DISREGARD_INLINE_LIMITS (fndecl))
22061 3025310 : && !flag_instrument_functions_exclude_p (fndecl))
22062 : {
22063 41 : gimple_seq body = NULL, cleanup = NULL;
22064 41 : gassign *assign;
22065 41 : tree cond_var;
22066 :
22067 : /* If -finstrument-functions-once is specified, generate:
22068 :
22069 : static volatile bool C.0 = false;
22070 : bool tmp_called;
22071 :
22072 : tmp_called = C.0;
22073 : if (!tmp_called)
22074 : {
22075 : C.0 = true;
22076 : [call profiling enter function]
22077 : }
22078 :
22079 : without specific protection for data races. */
22080 41 : if (flag_instrument_function_entry_exit > 1)
22081 : {
22082 10 : tree first_var
22083 10 : = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
22084 : VAR_DECL,
22085 : create_tmp_var_name ("C"),
22086 : boolean_type_node);
22087 10 : DECL_ARTIFICIAL (first_var) = 1;
22088 10 : DECL_IGNORED_P (first_var) = 1;
22089 10 : TREE_STATIC (first_var) = 1;
22090 10 : TREE_THIS_VOLATILE (first_var) = 1;
22091 10 : TREE_USED (first_var) = 1;
22092 10 : DECL_INITIAL (first_var) = boolean_false_node;
22093 10 : varpool_node::add (first_var);
22094 :
22095 10 : cond_var = create_tmp_var (boolean_type_node, "tmp_called");
22096 10 : assign = gimple_build_assign (cond_var, first_var);
22097 10 : gimplify_seq_add_stmt (&body, assign);
22098 :
22099 10 : assign = gimple_build_assign (first_var, boolean_true_node);
22100 : }
22101 :
22102 : else
22103 : {
22104 : cond_var = NULL_TREE;
22105 : assign = NULL;
22106 : }
22107 :
22108 41 : build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
22109 : cond_var, assign);
22110 :
22111 : /* If -finstrument-functions-once is specified, generate:
22112 :
22113 : if (!tmp_called)
22114 : [call profiling exit function]
22115 :
22116 : without specific protection for data races. */
22117 41 : build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
22118 : cond_var, NULL);
22119 :
22120 41 : gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
22121 41 : gimplify_seq_add_stmt (&body, tf);
22122 41 : gbind *new_bind = gimple_build_bind (NULL, body, NULL);
22123 :
22124 : /* Replace the current function body with the body
22125 : wrapped in the try/finally TF. */
22126 41 : seq = NULL;
22127 41 : gimple_seq_add_stmt (&seq, new_bind);
22128 41 : gimple_set_body (fndecl, seq);
22129 41 : bind = new_bind;
22130 : }
22131 :
22132 3025267 : if (sanitize_flags_p (SANITIZE_THREAD)
22133 3025267 : && param_tsan_instrument_func_entry_exit)
22134 : {
22135 979 : gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
22136 979 : gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
22137 979 : gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
22138 : /* Replace the current function body with the body
22139 : wrapped in the try/finally TF. */
22140 979 : seq = NULL;
22141 979 : gimple_seq_add_stmt (&seq, new_bind);
22142 979 : gimple_set_body (fndecl, seq);
22143 : }
22144 :
22145 3025267 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
22146 3025267 : cfun->curr_properties |= PROP_gimple_any;
22147 :
22148 3025267 : pop_cfun ();
22149 :
22150 3025267 : dump_function (TDI_gimple, fndecl);
22151 3025267 : }
22152 :
22153 : /* Return a dummy expression of type TYPE in order to keep going after an
22154 : error. */
22155 :
22156 : static tree
22157 30 : dummy_object (tree type)
22158 : {
22159 30 : tree t = build_int_cst (build_pointer_type (type), 0);
22160 30 : return build2 (MEM_REF, type, t, t);
22161 : }
22162 :
22163 : /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
22164 : builtin function, but a very special sort of operator. */
22165 :
22166 : enum gimplify_status
22167 51167 : gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
22168 : gimple_seq *post_p ATTRIBUTE_UNUSED)
22169 : {
22170 51167 : tree promoted_type, have_va_type;
22171 51167 : tree valist = TREE_OPERAND (*expr_p, 0);
22172 51167 : tree type = TREE_TYPE (*expr_p);
22173 51167 : tree t, tag, aptag;
22174 51167 : location_t loc = EXPR_LOCATION (*expr_p);
22175 :
22176 : /* Verify that valist is of the proper type. */
22177 51167 : have_va_type = TREE_TYPE (valist);
22178 51167 : if (have_va_type == error_mark_node)
22179 : return GS_ERROR;
22180 51148 : have_va_type = targetm.canonical_va_list_type (have_va_type);
22181 51148 : if (have_va_type == NULL_TREE
22182 51148 : && POINTER_TYPE_P (TREE_TYPE (valist)))
22183 : /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg. */
22184 258 : have_va_type
22185 258 : = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
22186 51148 : gcc_assert (have_va_type != NULL_TREE);
22187 :
22188 : /* Generate a diagnostic for requesting data of a type that cannot
22189 : be passed through `...' due to type promotion at the call site. */
22190 51148 : if ((promoted_type = lang_hooks.types.type_promotes_to (type))
22191 : != type)
22192 : {
22193 30 : static bool gave_help;
22194 30 : bool warned;
22195 : /* Use the expansion point to handle cases such as passing bool (defined
22196 : in a system header) through `...'. */
22197 30 : location_t xloc
22198 30 : = expansion_point_location_if_in_system_header (loc);
22199 :
22200 : /* Unfortunately, this is merely undefined, rather than a constraint
22201 : violation, so we cannot make this an error. If this call is never
22202 : executed, the program is still strictly conforming. */
22203 30 : auto_diagnostic_group d;
22204 30 : warned = warning_at (xloc, 0,
22205 : "%qT is promoted to %qT when passed through %<...%>",
22206 : type, promoted_type);
22207 30 : if (!gave_help && warned)
22208 : {
22209 15 : gave_help = true;
22210 15 : inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
22211 : promoted_type, type);
22212 : }
22213 :
22214 : /* We can, however, treat "undefined" any way we please.
22215 : Call abort to encourage the user to fix the program. */
22216 23 : if (warned)
22217 23 : inform (xloc, "if this code is reached, the program will abort");
22218 : /* Before the abort, allow the evaluation of the va_list
22219 : expression to exit or longjmp. */
22220 30 : gimplify_and_add (valist, pre_p);
22221 60 : t = build_call_expr_loc (loc,
22222 : builtin_decl_implicit (BUILT_IN_TRAP), 0);
22223 30 : gimplify_and_add (t, pre_p);
22224 :
22225 : /* This is dead code, but go ahead and finish so that the
22226 : mode of the result comes out right. */
22227 30 : *expr_p = dummy_object (type);
22228 30 : return GS_ALL_DONE;
22229 30 : }
22230 :
22231 51118 : tag = build_int_cst (build_pointer_type (type), 0);
22232 51118 : aptag = build_int_cst (TREE_TYPE (valist), 0);
22233 :
22234 51118 : *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
22235 : valist, tag, aptag);
22236 :
22237 : /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
22238 : needs to be expanded. */
22239 51118 : cfun->curr_properties &= ~PROP_gimple_lva;
22240 :
22241 51118 : return GS_OK;
22242 : }
22243 :
22244 : /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
22245 :
22246 : DST/SRC are the destination and source respectively. You can pass
22247 : ungimplified trees in DST or SRC, in which case they will be
22248 : converted to a gimple operand if necessary.
22249 :
22250 : This function returns the newly created GIMPLE_ASSIGN tuple. */
22251 :
22252 : gimple *
22253 912980 : gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
22254 : {
22255 912980 : tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
22256 912980 : gimplify_and_add (t, seq_p);
22257 912980 : ggc_free (t);
22258 912980 : return gimple_seq_last_stmt (*seq_p);
22259 : }
22260 :
22261 : inline hashval_t
22262 1619027 : gimplify_hasher::hash (const elt_t *p)
22263 : {
22264 1619027 : tree t = p->val;
22265 1619027 : return iterative_hash_expr (t, 0);
22266 : }
22267 :
22268 : inline bool
22269 602218 : gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
22270 : {
22271 602218 : tree t1 = p1->val;
22272 602218 : tree t2 = p2->val;
22273 602218 : enum tree_code code = TREE_CODE (t1);
22274 :
22275 602218 : if (TREE_CODE (t2) != code
22276 602218 : || TREE_TYPE (t1) != TREE_TYPE (t2))
22277 : return false;
22278 :
22279 367795 : if (!operand_equal_p (t1, t2, 0))
22280 : return false;
22281 :
22282 : return true;
22283 : }
|