Debian Codegraph @

gcc/gimplify.cc

raw
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <[email protected]>,
5 Diego Novillo <[email protected]> and Jason Merrill <[email protected]>.
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
75 /* Identifier for a basic condition, mapping it to other basic conditions of
76 its Boolean expression. Basic conditions given the same uid (in the same
77 function) are parts of the same ANDIF/ORIF expression. Used for condition
78 coverage. */
79 static unsigned nextconduid = 1;
80
81 /* Annotated gconds so that basic conditions in the same expression map to
82 the same uid. This is used for condition coverage. */
83 static hash_map <tree, unsigned> *cond_uids;
84
85 /* Get a fresh identifier for a new condition expression. This is used for
86 condition coverage. */
87 static unsigned
88 next_cond_uid ()
89 {
90 return nextconduid++;
91 }
92
93 /* Reset the condition uid to the value it should have when compiling a new
94 function. 0 is already the default/untouched value, so start at non-zero.
95 A valid and set id should always be > 0. This is used for condition
96 coverage. */
97 static void
98 reset_cond_uid ()
99 {
100 nextconduid = 1;
101 }
102
103 /* Associate the condition STMT with the discriminator UID. STMTs that are
104 broken down with ANDIF/ORIF from the same Boolean expression should be given
105 the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
106 { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
107 for condition coverage. */
108 static void
109 tree_associate_condition_with_expr (tree stmt, unsigned uid)
110 {
111 if (!condition_coverage_flag)
112 return;
113
114 if (!cond_uids)
115 cond_uids = new hash_map <tree, unsigned> ();
116
117 cond_uids->put (stmt, uid);
118 }
119
120 /* Hash set of poisoned variables in a bind expr. */
121 static hash_set<tree> *asan_poisoned_variables = NULL;
122
123 /* Hash set of already-resolved calls to OpenMP "declare variant"
124 functions. A call can resolve to the original function and
125 we don't want to repeat the resolution multiple times. */
126 static hash_set<tree> *omp_resolved_variant_calls = NULL;
127
128 enum gimplify_omp_var_data
129 {
130 GOVD_SEEN = 0x000001,
131 GOVD_EXPLICIT = 0x000002,
132 GOVD_SHARED = 0x000004,
133 GOVD_PRIVATE = 0x000008,
134 GOVD_FIRSTPRIVATE = 0x000010,
135 GOVD_LASTPRIVATE = 0x000020,
136 GOVD_REDUCTION = 0x000040,
137 GOVD_LOCAL = 0x00080,
138 GOVD_MAP = 0x000100,
139 GOVD_DEBUG_PRIVATE = 0x000200,
140 GOVD_PRIVATE_OUTER_REF = 0x000400,
141 GOVD_LINEAR = 0x000800,
142 GOVD_ALIGNED = 0x001000,
143
144 /* Flag for GOVD_MAP: don't copy back. */
145 GOVD_MAP_TO_ONLY = 0x002000,
146
147 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
148 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
149
150 GOVD_MAP_0LEN_ARRAY = 0x008000,
151
152 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
153 GOVD_MAP_ALWAYS_TO = 0x010000,
154
155 /* Flag for shared vars that are or might be stored to in the region. */
156 GOVD_WRITTEN = 0x020000,
157
158 /* Flag for GOVD_MAP, if it is a forced mapping. */
159 GOVD_MAP_FORCE = 0x040000,
160
161 /* Flag for GOVD_MAP: must be present already. */
162 GOVD_MAP_FORCE_PRESENT = 0x080000,
163
164 /* Flag for GOVD_MAP: only allocate. */
165 GOVD_MAP_ALLOC_ONLY = 0x100000,
166
167 /* Flag for GOVD_MAP: only copy back. */
168 GOVD_MAP_FROM_ONLY = 0x200000,
169
170 GOVD_NONTEMPORAL = 0x400000,
171
172 /* Flag for GOVD_LASTPRIVATE: conditional modifier. */
173 GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
174
175 GOVD_CONDTEMP = 0x1000000,
176
177 /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause. */
178 GOVD_REDUCTION_INSCAN = 0x2000000,
179
180 /* Flag for GOVD_FIRSTPRIVATE: OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT. */
181 GOVD_FIRSTPRIVATE_IMPLICIT = 0x4000000,
182
183 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
184 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
185 | GOVD_LOCAL)
186 };
187
188
189 enum omp_region_type
190 {
191 ORT_WORKSHARE = 0x00,
192 ORT_TASKGROUP = 0x01,
193 ORT_DISPATCH = 0x02,
194 ORT_SIMD = 0x04,
195
196 ORT_PARALLEL = 0x08,
197 ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
198
199 ORT_TASK = 0x10,
200 ORT_UNTIED_TASK = ORT_TASK | 1,
201 ORT_TASKLOOP = ORT_TASK | 2,
202 ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
203
204 ORT_TEAMS = 0x20,
205 ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
206 ORT_HOST_TEAMS = ORT_TEAMS | 2,
207 ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
208
209 /* Data region. */
210 ORT_TARGET_DATA = 0x40,
211
212 /* Data region with offloading. */
213 ORT_TARGET = 0x80,
214 ORT_COMBINED_TARGET = ORT_TARGET | 1,
215 ORT_IMPLICIT_TARGET = ORT_TARGET | 2,
216
217 /* OpenACC variants. */
218 ORT_ACC = 0x100, /* A generic OpenACC region. */
219 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
220 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
221 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
222 ORT_ACC_SERIAL = ORT_ACC | ORT_TARGET | 4, /* Serial construct. */
223 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
224
225 /* Dummy OpenMP region, used to disable expansion of
226 DECL_VALUE_EXPRs in taskloop pre body. */
227 ORT_NONE = 0x200
228 };
229
230 /* Gimplify hashtable helper. */
231
232 struct gimplify_hasher : free_ptr_hash <elt_t>
233 {
234 static inline hashval_t hash (const elt_t *);
235 static inline bool equal (const elt_t *, const elt_t *);
236 };
237
238 struct gimplify_ctx
239 {
240 struct gimplify_ctx *prev_context;
241
242 vec<gbind *> bind_expr_stack;
243 tree temps;
244 gimple_seq conditional_cleanups;
245 tree exit_label;
246 tree return_temp;
247
248 vec<tree> case_labels;
249 hash_set<tree> *live_switch_vars;
250 /* The formal temporary table. Should this be persistent? */
251 hash_table<gimplify_hasher> *temp_htab;
252
253 int conditions;
254 unsigned into_ssa : 1;
255 unsigned allow_rhs_cond_expr : 1;
256 unsigned in_cleanup_point_expr : 1;
257 unsigned keep_stack : 1;
258 unsigned save_stack : 1;
259 unsigned in_switch_expr : 1;
260 unsigned in_handler_expr : 1;
261 };
262
263 enum gimplify_defaultmap_kind
264 {
265 GDMK_SCALAR,
266 GDMK_SCALAR_TARGET, /* w/ Fortran's target attr, implicit mapping, only. */
267 GDMK_AGGREGATE,
268 GDMK_ALLOCATABLE,
269 GDMK_POINTER
270 };
271
272 struct gimplify_omp_ctx
273 {
274 struct gimplify_omp_ctx *outer_context;
275 splay_tree variables;
276 hash_set<tree> *privatized_types;
277 tree clauses;
278 /* Iteration variables in an OMP_FOR. */
279 vec<tree> loop_iter_var;
280 location_t location;
281 enum omp_clause_default_kind default_kind;
282 enum omp_region_type region_type;
283 enum tree_code code;
284 bool combined_loop;
285 bool distribute;
286 bool target_firstprivatize_array_bases;
287 bool add_safelen1;
288 bool order_concurrent;
289 bool has_depend;
290 bool in_for_exprs;
291 bool in_call_args;
292 int defaultmap[5];
293 };
294
295 static struct gimplify_ctx *gimplify_ctxp;
296 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
297 static bool in_omp_construct;
298
299 /* Forward declaration. */
300 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
301 static hash_map<tree, tree> *oacc_declare_returns;
302 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
303 bool (*) (tree), fallback_t, bool);
304 static void prepare_gimple_addressable (tree *, gimple_seq *);
305
306 /* Shorter alias name for the above function for use in gimplify.cc
307 only. */
308
309 static inline void
310 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
311 {
312 gimple_seq_add_stmt_without_update (seq_p, gs);
313 }
314
315 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
316 NULL, a new sequence is allocated. This function is
317 similar to gimple_seq_add_seq, but does not scan the operands.
318 During gimplification, we need to manipulate statement sequences
319 before the def/use vectors have been constructed. */
320
321 static void
322 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
323 {
324 gimple_stmt_iterator si;
325
326 if (src == NULL)
327 return;
328
329 si = gsi_last (*dst_p);
330 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
331 }
332
333
334 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
335 and popping gimplify contexts. */
336
337 static struct gimplify_ctx *ctx_pool = NULL;
338
339 /* Return a gimplify context struct from the pool. */
340
341 static inline struct gimplify_ctx *
342 ctx_alloc (void)
343 {
344 struct gimplify_ctx * c = ctx_pool;
345
346 if (c)
347 ctx_pool = c->prev_context;
348 else
349 c = XNEW (struct gimplify_ctx);
350
351 memset (c, '\0', sizeof (*c));
352 return c;
353 }
354
355 /* Put gimplify context C back into the pool. */
356
357 static inline void
358 ctx_free (struct gimplify_ctx *c)
359 {
360 c->prev_context = ctx_pool;
361 ctx_pool = c;
362 }
363
364 /* Free allocated ctx stack memory. */
365
366 void
367 free_gimplify_stack (void)
368 {
369 struct gimplify_ctx *c;
370
371 while ((c = ctx_pool))
372 {
373 ctx_pool = c->prev_context;
374 free (c);
375 }
376 }
377
378
379 /* Set up a context for the gimplifier. */
380
381 void
382 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
383 {
384 struct gimplify_ctx *c = ctx_alloc ();
385
386 c->prev_context = gimplify_ctxp;
387 gimplify_ctxp = c;
388 gimplify_ctxp->into_ssa = in_ssa;
389 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
390 }
391
392 /* Tear down a context for the gimplifier. If BODY is non-null, then
393 put the temporaries into the outer BIND_EXPR. Otherwise, put them
394 in the local_decls.
395
396 BODY is not a sequence, but the first tuple in a sequence. */
397
398 void
399 pop_gimplify_context (gimple *body)
400 {
401 struct gimplify_ctx *c = gimplify_ctxp;
402
403 gcc_assert (c
404 && (!c->bind_expr_stack.exists ()
405 || c->bind_expr_stack.is_empty ()));
406 c->bind_expr_stack.release ();
407 gimplify_ctxp = c->prev_context;
408
409 if (body)
410 declare_vars (c->temps, body, false);
411 else
412 record_vars (c->temps);
413
414 delete c->temp_htab;
415 c->temp_htab = NULL;
416 ctx_free (c);
417 }
418
419 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
420
421 static void
422 gimple_push_bind_expr (gbind *bind_stmt)
423 {
424 gimplify_ctxp->bind_expr_stack.reserve (8);
425 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
426 }
427
428 /* Pop the first element off the stack of bindings. */
429
430 static void
431 gimple_pop_bind_expr (void)
432 {
433 gimplify_ctxp->bind_expr_stack.pop ();
434 }
435
436 /* Return the first element of the stack of bindings. */
437
438 gbind *
439 gimple_current_bind_expr (void)
440 {
441 return gimplify_ctxp->bind_expr_stack.last ();
442 }
443
444 /* Return the stack of bindings created during gimplification. */
445
446 vec<gbind *>
447 gimple_bind_expr_stack (void)
448 {
449 return gimplify_ctxp->bind_expr_stack;
450 }
451
452 /* Return true iff there is a COND_EXPR between us and the innermost
453 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
454
455 static bool
456 gimple_conditional_context (void)
457 {
458 return gimplify_ctxp->conditions > 0;
459 }
460
461 /* Note that we've entered a COND_EXPR. */
462
463 static void
464 gimple_push_condition (void)
465 {
466 #ifdef ENABLE_GIMPLE_CHECKING
467 if (gimplify_ctxp->conditions == 0)
468 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
469 #endif
470 ++(gimplify_ctxp->conditions);
471 }
472
473 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
474 now, add any conditional cleanups we've seen to the prequeue. */
475
476 static void
477 gimple_pop_condition (gimple_seq *pre_p)
478 {
479 int conds = --(gimplify_ctxp->conditions);
480
481 gcc_assert (conds >= 0);
482 if (conds == 0)
483 {
484 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
485 gimplify_ctxp->conditional_cleanups = NULL;
486 }
487 }
488
489 /* A stable comparison routine for use with splay trees and DECLs. */
490
491 static int
492 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
493 {
494 tree a = (tree) xa;
495 tree b = (tree) xb;
496
497 return DECL_UID (a) - DECL_UID (b);
498 }
499
500 /* Create a new omp construct that deals with variable remapping. */
501
502 static struct gimplify_omp_ctx *
503 new_omp_context (enum omp_region_type region_type)
504 {
505 struct gimplify_omp_ctx *c;
506
507 c = XCNEW (struct gimplify_omp_ctx);
508 c->outer_context = gimplify_omp_ctxp;
509 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
510 c->privatized_types = new hash_set<tree>;
511 c->location = input_location;
512 c->region_type = region_type;
513 if ((region_type & ORT_TASK) == 0)
514 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
515 else
516 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
517 c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
518 c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
519 c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
520 c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
521 c->defaultmap[GDMK_POINTER] = GOVD_MAP;
522
523 return c;
524 }
525
526 /* Destroy an omp construct that deals with variable remapping. */
527
528 static void
529 delete_omp_context (struct gimplify_omp_ctx *c)
530 {
531 splay_tree_delete (c->variables);
532 delete c->privatized_types;
533 c->loop_iter_var.release ();
534 XDELETE (c);
535 }
536
537 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
538 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
539
540 /* Both gimplify the statement T and append it to *SEQ_P. This function
541 behaves exactly as gimplify_stmt, but you don't have to pass T as a
542 reference. */
543
544 void
545 gimplify_and_add (tree t, gimple_seq *seq_p)
546 {
547 gimplify_stmt (&t, seq_p);
548 }
549
550 /* Gimplify statement T into sequence *SEQ_P, and return the first
551 tuple in the sequence of generated tuples for this statement.
552 Return NULL if gimplifying T produced no tuples. */
553
554 static gimple *
555 gimplify_and_return_first (tree t, gimple_seq *seq_p)
556 {
557 gimple_stmt_iterator last = gsi_last (*seq_p);
558
559 gimplify_and_add (t, seq_p);
560
561 if (!gsi_end_p (last))
562 {
563 gsi_next (&last);
564 return gsi_stmt (last);
565 }
566 else
567 return gimple_seq_first_stmt (*seq_p);
568 }
569
570 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
571 LHS, or for a call argument. */
572
573 static bool
574 is_gimple_mem_rhs (tree t)
575 {
576 /* If we're dealing with a renamable type, either source or dest must be
577 a renamed variable. */
578 if (is_gimple_reg_type (TREE_TYPE (t)))
579 return is_gimple_val (t);
580 else
581 return is_gimple_val (t) || is_gimple_lvalue (t);
582 }
583
584 /* Return true if T is a CALL_EXPR or an expression that can be
585 assigned to a temporary. Note that this predicate should only be
586 used during gimplification. See the rationale for this in
587 gimplify_modify_expr. */
588
589 static bool
590 is_gimple_reg_rhs_or_call (tree t)
591 {
592 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
593 || TREE_CODE (t) == CALL_EXPR);
594 }
595
596 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
597 this predicate should only be used during gimplification. See the
598 rationale for this in gimplify_modify_expr. */
599
600 static bool
601 is_gimple_mem_rhs_or_call (tree t)
602 {
603 /* If we're dealing with a renamable type, either source or dest must be
604 a renamed variable. */
605 if (is_gimple_reg_type (TREE_TYPE (t)))
606 return is_gimple_val (t);
607 else
608 return (is_gimple_val (t)
609 || is_gimple_lvalue (t)
610 || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
611 || TREE_CODE (t) == CALL_EXPR);
612 }
613
614 /* Create a temporary with a name derived from VAL. Subroutine of
615 lookup_tmp_var; nobody else should call this function. */
616
617 static inline tree
618 create_tmp_from_val (tree val)
619 {
620 /* Drop all qualifiers and address-space information from the value type. */
621 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
622 tree var = create_tmp_var (type, get_name (val));
623 return var;
624 }
625
626 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
627 an existing expression temporary. If NOT_GIMPLE_REG, mark it as such. */
628
629 static tree
630 lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
631 {
632 tree ret;
633
634 /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P. */
635 gcc_assert (!is_formal || !not_gimple_reg);
636
637 /* If not optimizing, never really reuse a temporary. local-alloc
638 won't allocate any variable that is used in more than one basic
639 block, which means it will go into memory, causing much extra
640 work in reload and final and poorer code generation, outweighing
641 the extra memory allocation here. */
642 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
643 {
644 ret = create_tmp_from_val (val);
645 DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
646 }
647 else
648 {
649 elt_t elt, *elt_p;
650 elt_t **slot;
651
652 elt.val = val;
653 if (!gimplify_ctxp->temp_htab)
654 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
655 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
656 if (*slot == NULL)
657 {
658 elt_p = XNEW (elt_t);
659 elt_p->val = val;
660 elt_p->temp = ret = create_tmp_from_val (val);
661 *slot = elt_p;
662 }
663 else
664 {
665 elt_p = *slot;
666 ret = elt_p->temp;
667 }
668 }
669
670 return ret;
671 }
672
673 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
674
675 static tree
676 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
677 bool is_formal, bool allow_ssa, bool not_gimple_reg)
678 {
679 tree t, mod;
680
681 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
682 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
683 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
684 fb_rvalue);
685
686 if (allow_ssa
687 && gimplify_ctxp->into_ssa
688 && is_gimple_reg_type (TREE_TYPE (val)))
689 {
690 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
691 if (! gimple_in_ssa_p (cfun))
692 {
693 const char *name = get_name (val);
694 if (name)
695 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
696 }
697 }
698 else
699 t = lookup_tmp_var (val, is_formal, not_gimple_reg);
700
701 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
702
703 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
704
705 /* gimplify_modify_expr might want to reduce this further. */
706 gimplify_and_add (mod, pre_p);
707 ggc_free (mod);
708
709 /* If we failed to gimplify VAL then we can end up with the temporary
710 SSA name not having a definition. In this case return a decl. */
711 if (TREE_CODE (t) == SSA_NAME && ! SSA_NAME_DEF_STMT (t))
712 return lookup_tmp_var (val, is_formal, not_gimple_reg);
713
714 return t;
715 }
716
717 /* Return a formal temporary variable initialized with VAL. PRE_P is as
718 in gimplify_expr. Only use this function if:
719
720 1) The value of the unfactored expression represented by VAL will not
721 change between the initialization and use of the temporary, and
722 2) The temporary will not be otherwise modified.
723
724 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
725 and #2 means it is inappropriate for && temps.
726
727 For other cases, use get_initialized_tmp_var instead. */
728
729 tree
730 get_formal_tmp_var (tree val, gimple_seq *pre_p)
731 {
732 return internal_get_tmp_var (val, pre_p, NULL, true, true, false);
733 }
734
735 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
736 are as in gimplify_expr. */
737
738 tree
739 get_initialized_tmp_var (tree val, gimple_seq *pre_p,
740 gimple_seq *post_p /* = NULL */,
741 bool allow_ssa /* = true */)
742 {
743 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa, false);
744 }
745
746 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
747 generate debug info for them; otherwise don't. */
748
749 void
750 declare_vars (tree vars, gimple *gs, bool debug_info)
751 {
752 tree last = vars;
753 if (last)
754 {
755 tree temps, block;
756
757 gbind *scope = as_a <gbind *> (gs);
758
759 temps = nreverse (last);
760
761 block = gimple_bind_block (scope);
762 gcc_assert (!block || TREE_CODE (block) == BLOCK);
763 if (!block || !debug_info)
764 {
765 DECL_CHAIN (last) = gimple_bind_vars (scope);
766 gimple_bind_set_vars (scope, temps);
767 }
768 else
769 {
770 /* We need to attach the nodes both to the BIND_EXPR and to its
771 associated BLOCK for debugging purposes. The key point here
772 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
773 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
774 if (BLOCK_VARS (block))
775 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
776 else
777 {
778 gimple_bind_set_vars (scope,
779 chainon (gimple_bind_vars (scope), temps));
780 BLOCK_VARS (block) = temps;
781 }
782 }
783 }
784 }
785
786 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
787 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
788 no such upper bound can be obtained. */
789
790 static void
791 force_constant_size (tree var)
792 {
793 /* The only attempt we make is by querying the maximum size of objects
794 of the variable's type. */
795
796 HOST_WIDE_INT max_size;
797
798 gcc_assert (VAR_P (var));
799
800 max_size = max_int_size_in_bytes (TREE_TYPE (var));
801
802 gcc_assert (max_size >= 0);
803
804 DECL_SIZE_UNIT (var)
805 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
806 DECL_SIZE (var)
807 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
808 }
809
810 /* Push the temporary variable TMP into the current binding. */
811
812 void
813 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
814 {
815 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
816
817 /* Later processing assumes that the object size is constant, which might
818 not be true at this point. Force the use of a constant upper bound in
819 this case. */
820 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
821 force_constant_size (tmp);
822
823 DECL_CONTEXT (tmp) = fn->decl;
824 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
825
826 record_vars_into (tmp, fn->decl);
827 }
828
829 /* Push the temporary variable TMP into the current binding. */
830
831 void
832 gimple_add_tmp_var (tree tmp)
833 {
834 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
835
836 /* Later processing assumes that the object size is constant, which might
837 not be true at this point. Force the use of a constant upper bound in
838 this case. */
839 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
840 force_constant_size (tmp);
841
842 DECL_CONTEXT (tmp) = current_function_decl;
843 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
844
845 if (gimplify_ctxp)
846 {
847 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
848 gimplify_ctxp->temps = tmp;
849
850 /* Mark temporaries local within the nearest enclosing parallel. */
851 if (gimplify_omp_ctxp)
852 {
853 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
854 int flag = GOVD_LOCAL | GOVD_SEEN;
855 while (ctx
856 && (ctx->region_type == ORT_WORKSHARE
857 || ctx->region_type == ORT_TASKGROUP
858 || ctx->region_type == ORT_SIMD
859 || ctx->region_type == ORT_ACC))
860 {
861 if (ctx->region_type == ORT_SIMD
862 && TREE_ADDRESSABLE (tmp)
863 && !TREE_STATIC (tmp))
864 {
865 if (TREE_CODE (DECL_SIZE_UNIT (tmp)) != INTEGER_CST)
866 ctx->add_safelen1 = true;
867 else if (ctx->in_for_exprs)
868 flag = GOVD_PRIVATE;
869 else
870 flag = GOVD_PRIVATE | GOVD_SEEN;
871 break;
872 }
873 ctx = ctx->outer_context;
874 }
875 if (ctx)
876 omp_add_variable (ctx, tmp, flag);
877 }
878 }
879 else if (cfun)
880 record_vars (tmp);
881 else
882 {
883 gimple_seq body_seq;
884
885 /* This case is for nested functions. We need to expose the locals
886 they create. */
887 body_seq = gimple_body (current_function_decl);
888 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
889 }
890 }
891
892
893
894 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
895 nodes that are referenced more than once in GENERIC functions. This is
896 necessary because gimplification (translation into GIMPLE) is performed
897 by modifying tree nodes in-place, so gimplication of a shared node in a
898 first context could generate an invalid GIMPLE form in a second context.
899
900 This is achieved with a simple mark/copy/unmark algorithm that walks the
901 GENERIC representation top-down, marks nodes with TREE_VISITED the first
902 time it encounters them, duplicates them if they already have TREE_VISITED
903 set, and finally removes the TREE_VISITED marks it has set.
904
905 The algorithm works only at the function level, i.e. it generates a GENERIC
906 representation of a function with no nodes shared within the function when
907 passed a GENERIC function (except for nodes that are allowed to be shared).
908
909 At the global level, it is also necessary to unshare tree nodes that are
910 referenced in more than one function, for the same aforementioned reason.
911 This requires some cooperation from the front-end. There are 2 strategies:
912
913 1. Manual unsharing. The front-end needs to call unshare_expr on every
914 expression that might end up being shared across functions.
915
916 2. Deep unsharing. This is an extension of regular unsharing. Instead
917 of calling unshare_expr on expressions that might be shared across
918 functions, the front-end pre-marks them with TREE_VISITED. This will
919 ensure that they are unshared on the first reference within functions
920 when the regular unsharing algorithm runs. The counterpart is that
921 this algorithm must look deeper than for manual unsharing, which is
922 specified by LANG_HOOKS_DEEP_UNSHARING.
923
924 If there are only few specific cases of node sharing across functions, it is
925 probably easier for a front-end to unshare the expressions manually. On the
926 contrary, if the expressions generated at the global level are as widespread
927 as expressions generated within functions, deep unsharing is very likely the
928 way to go. */
929
930 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
931 These nodes model computations that must be done once. If we were to
932 unshare something like SAVE_EXPR(i++), the gimplification process would
933 create wrong code. However, if DATA is non-null, it must hold a pointer
934 set that is used to unshare the subtrees of these nodes. */
935
936 static tree
937 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
938 {
939 tree t = *tp;
940 enum tree_code code = TREE_CODE (t);
941
942 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
943 copy their subtrees if we can make sure to do it only once. */
944 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
945 {
946 if (data && !((hash_set<tree> *)data)->add (t))
947 ;
948 else
949 *walk_subtrees = 0;
950 }
951
952 /* Stop at types, decls, constants like copy_tree_r. */
953 else if (TREE_CODE_CLASS (code) == tcc_type
954 || TREE_CODE_CLASS (code) == tcc_declaration
955 || TREE_CODE_CLASS (code) == tcc_constant)
956 *walk_subtrees = 0;
957
958 /* Cope with the statement expression extension. */
959 else if (code == STATEMENT_LIST)
960 ;
961
962 /* Leave the bulk of the work to copy_tree_r itself. */
963 else
964 copy_tree_r (tp, walk_subtrees, NULL);
965
966 return NULL_TREE;
967 }
968
969 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
970 If *TP has been visited already, then *TP is deeply copied by calling
971 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
972
973 static tree
974 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
975 {
976 tree t = *tp;
977 enum tree_code code = TREE_CODE (t);
978
979 /* Skip types, decls, and constants. But we do want to look at their
980 types and the bounds of types. Mark them as visited so we properly
981 unmark their subtrees on the unmark pass. If we've already seen them,
982 don't look down further. */
983 if (TREE_CODE_CLASS (code) == tcc_type
984 || TREE_CODE_CLASS (code) == tcc_declaration
985 || TREE_CODE_CLASS (code) == tcc_constant)
986 {
987 if (TREE_VISITED (t))
988 *walk_subtrees = 0;
989 else
990 TREE_VISITED (t) = 1;
991 }
992
993 /* If this node has been visited already, unshare it and don't look
994 any deeper. */
995 else if (TREE_VISITED (t))
996 {
997 walk_tree (tp, mostly_copy_tree_r, data, NULL);
998 *walk_subtrees = 0;
999 }
1000
1001 /* Otherwise, mark the node as visited and keep looking. */
1002 else
1003 TREE_VISITED (t) = 1;
1004
1005 return NULL_TREE;
1006 }
1007
1008 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
1009 copy_if_shared_r callback unmodified. */
1010
1011 void
1012 copy_if_shared (tree *tp, void *data)
1013 {
1014 walk_tree (tp, copy_if_shared_r, data, NULL);
1015 }
1016
1017 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
1018 any nested functions. */
1019
1020 static void
1021 unshare_body (tree fndecl)
1022 {
1023 struct cgraph_node *cgn = cgraph_node::get (fndecl);
1024 /* If the language requires deep unsharing, we need a pointer set to make
1025 sure we don't repeatedly unshare subtrees of unshareable nodes. */
1026 hash_set<tree> *visited
1027 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
1028
1029 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
1030 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
1031 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1032
1033 delete visited;
1034
1035 if (cgn)
1036 for (cgn = first_nested_function (cgn); cgn;
1037 cgn = next_nested_function (cgn))
1038 unshare_body (cgn->decl);
1039 }
1040
1041 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1042 Subtrees are walked until the first unvisited node is encountered. */
1043
1044 static tree
1045 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1046 {
1047 tree t = *tp;
1048
1049 /* If this node has been visited, unmark it and keep looking. */
1050 if (TREE_VISITED (t))
1051 TREE_VISITED (t) = 0;
1052
1053 /* Otherwise, don't look any deeper. */
1054 else
1055 *walk_subtrees = 0;
1056
1057 return NULL_TREE;
1058 }
1059
1060 /* Unmark the visited trees rooted at *TP. */
1061
1062 static inline void
1063 unmark_visited (tree *tp)
1064 {
1065 walk_tree (tp, unmark_visited_r, NULL, NULL);
1066 }
1067
1068 /* Likewise, but mark all trees as not visited. */
1069
1070 static void
1071 unvisit_body (tree fndecl)
1072 {
1073 struct cgraph_node *cgn = cgraph_node::get (fndecl);
1074
1075 unmark_visited (&DECL_SAVED_TREE (fndecl));
1076 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1077 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1078
1079 if (cgn)
1080 for (cgn = first_nested_function (cgn);
1081 cgn; cgn = next_nested_function (cgn))
1082 unvisit_body (cgn->decl);
1083 }
1084
1085 /* Unconditionally make an unshared copy of EXPR. This is used when using
1086 stored expressions which span multiple functions, such as BINFO_VTABLE,
1087 as the normal unsharing process can't tell that they're shared. */
1088
1089 tree
1090 unshare_expr (tree expr)
1091 {
1092 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1093 return expr;
1094 }
1095
1096 /* Worker for unshare_expr_without_location. */
1097
1098 static tree
1099 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1100 {
1101 if (EXPR_P (*tp))
1102 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1103 else
1104 *walk_subtrees = 0;
1105 return NULL_TREE;
1106 }
1107
1108 /* Similar to unshare_expr but also prune all expression locations
1109 from EXPR. */
1110
1111 tree
1112 unshare_expr_without_location (tree expr)
1113 {
1114 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1115 if (EXPR_P (expr))
1116 walk_tree (&expr, prune_expr_location, NULL, NULL);
1117 return expr;
1118 }
1119
1120 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
1121 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
1122 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
1123 EXPR is the location of the EXPR. */
1124
1125 static location_t
1126 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1127 {
1128 if (!expr)
1129 return or_else;
1130
1131 if (EXPR_HAS_LOCATION (expr))
1132 return EXPR_LOCATION (expr);
1133
1134 if (TREE_CODE (expr) != STATEMENT_LIST)
1135 return or_else;
1136
1137 tree_stmt_iterator i = tsi_start (expr);
1138
1139 bool found = false;
1140 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1141 {
1142 found = true;
1143 tsi_next (&i);
1144 }
1145
1146 if (!found || !tsi_one_before_end_p (i))
1147 return or_else;
1148
1149 return rexpr_location (tsi_stmt (i), or_else);
1150 }
1151
1152 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1153 rexpr_location for the potential recursion. */
1154
1155 static inline bool
1156 rexpr_has_location (tree expr)
1157 {
1158 return rexpr_location (expr) != UNKNOWN_LOCATION;
1159 }
1160
1161
1162 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1163 contain statements and have a value. Assign its value to a temporary
1164 and give it void_type_node. Return the temporary, or NULL_TREE if
1165 WRAPPER was already void. */
1166
1167 tree
1168 voidify_wrapper_expr (tree wrapper, tree temp)
1169 {
1170 tree type = TREE_TYPE (wrapper);
1171 if (type && !VOID_TYPE_P (type))
1172 {
1173 tree *p;
1174
1175 /* Set p to point to the body of the wrapper. Loop until we find
1176 something that isn't a wrapper. */
1177 for (p = &wrapper; p && *p; )
1178 {
1179 switch (TREE_CODE (*p))
1180 {
1181 case BIND_EXPR:
1182 TREE_SIDE_EFFECTS (*p) = 1;
1183 TREE_TYPE (*p) = void_type_node;
1184 /* For a BIND_EXPR, the body is operand 1. */
1185 p = &BIND_EXPR_BODY (*p);
1186 break;
1187
1188 case CLEANUP_POINT_EXPR:
1189 case TRY_FINALLY_EXPR:
1190 case TRY_CATCH_EXPR:
1191 TREE_SIDE_EFFECTS (*p) = 1;
1192 TREE_TYPE (*p) = void_type_node;
1193 p = &TREE_OPERAND (*p, 0);
1194 break;
1195
1196 case STATEMENT_LIST:
1197 {
1198 tree_stmt_iterator i = tsi_last (*p);
1199 TREE_SIDE_EFFECTS (*p) = 1;
1200 TREE_TYPE (*p) = void_type_node;
1201 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1202 }
1203 break;
1204
1205 case COMPOUND_EXPR:
1206 /* Advance to the last statement. Set all container types to
1207 void. */
1208 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1209 {
1210 TREE_SIDE_EFFECTS (*p) = 1;
1211 TREE_TYPE (*p) = void_type_node;
1212 }
1213 break;
1214
1215 case TRANSACTION_EXPR:
1216 TREE_SIDE_EFFECTS (*p) = 1;
1217 TREE_TYPE (*p) = void_type_node;
1218 p = &TRANSACTION_EXPR_BODY (*p);
1219 break;
1220
1221 default:
1222 /* Assume that any tree upon which voidify_wrapper_expr is
1223 directly called is a wrapper, and that its body is op0. */
1224 if (p == &wrapper)
1225 {
1226 TREE_SIDE_EFFECTS (*p) = 1;
1227 TREE_TYPE (*p) = void_type_node;
1228 p = &TREE_OPERAND (*p, 0);
1229 break;
1230 }
1231 goto out;
1232 }
1233 }
1234
1235 out:
1236 if (p == NULL || IS_EMPTY_STMT (*p))
1237 temp = NULL_TREE;
1238 else if (temp)
1239 {
1240 /* The wrapper is on the RHS of an assignment that we're pushing
1241 down. */
1242 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1243 || TREE_CODE (temp) == MODIFY_EXPR);
1244 TREE_OPERAND (temp, 1) = *p;
1245 *p = temp;
1246 }
1247 else
1248 {
1249 temp = create_tmp_var (type, "retval");
1250 *p = build2 (INIT_EXPR, type, temp, *p);
1251 }
1252
1253 return temp;
1254 }
1255
1256 return NULL_TREE;
1257 }
1258
1259 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1260 a temporary through which they communicate. */
1261
1262 static void
1263 build_stack_save_restore (gcall **save, gcall **restore)
1264 {
1265 tree tmp_var;
1266
1267 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1268 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1269 gimple_call_set_lhs (*save, tmp_var);
1270
1271 *restore
1272 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1273 1, tmp_var);
1274 }
1275
1276 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1277
1278 static tree
1279 build_asan_poison_call_expr (tree decl)
1280 {
1281 /* Do not poison variables that have size equal to zero. */
1282 tree unit_size = DECL_SIZE_UNIT (decl);
1283 if (zerop (unit_size))
1284 return NULL_TREE;
1285
1286 tree base = build_fold_addr_expr (decl);
1287
1288 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1289 void_type_node, 3,
1290 build_int_cst (integer_type_node,
1291 ASAN_MARK_POISON),
1292 base, unit_size);
1293 }
1294
1295 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1296 on POISON flag, shadow memory of a DECL variable. The call will be
1297 put on location identified by IT iterator, where BEFORE flag drives
1298 position where the stmt will be put. */
1299
1300 static void
1301 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1302 bool before)
1303 {
1304 tree unit_size = DECL_SIZE_UNIT (decl);
1305 tree base = build_fold_addr_expr (decl);
1306
1307 /* Do not poison variables that have size equal to zero. */
1308 if (zerop (unit_size))
1309 return;
1310
1311 /* It's necessary to have all stack variables aligned to ASAN granularity
1312 bytes. */
1313 gcc_assert (!hwasan_sanitize_p () || hwasan_sanitize_stack_p ());
1314 unsigned shadow_granularity
1315 = hwasan_sanitize_p () ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY;
1316 if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
1317 SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
1318
1319 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1320
1321 gimple *g
1322 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1323 build_int_cst (integer_type_node, flags),
1324 base, unit_size);
1325
1326 if (before)
1327 gsi_insert_before (it, g, GSI_NEW_STMT);
1328 else
1329 gsi_insert_after (it, g, GSI_NEW_STMT);
1330 }
1331
1332 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1333 either poisons or unpoisons a DECL. Created statement is appended
1334 to SEQ_P gimple sequence. */
1335
1336 static void
1337 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1338 {
1339 gimple_stmt_iterator it = gsi_last (*seq_p);
1340 bool before = false;
1341
1342 if (gsi_end_p (it))
1343 before = true;
1344
1345 asan_poison_variable (decl, poison, &it, before);
1346 }
1347
1348 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1349
1350 static int
1351 sort_by_decl_uid (const void *a, const void *b)
1352 {
1353 const tree *t1 = (const tree *)a;
1354 const tree *t2 = (const tree *)b;
1355
1356 int uid1 = DECL_UID (*t1);
1357 int uid2 = DECL_UID (*t2);
1358
1359 if (uid1 < uid2)
1360 return -1;
1361 else if (uid1 > uid2)
1362 return 1;
1363 else
1364 return 0;
1365 }
1366
1367 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1368 depending on POISON flag. Created statement is appended
1369 to SEQ_P gimple sequence. */
1370
1371 static void
1372 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1373 {
1374 unsigned c = variables->elements ();
1375 if (c == 0)
1376 return;
1377
1378 auto_vec<tree> sorted_variables (c);
1379
1380 for (hash_set<tree>::iterator it = variables->begin ();
1381 it != variables->end (); ++it)
1382 sorted_variables.safe_push (*it);
1383
1384 sorted_variables.qsort (sort_by_decl_uid);
1385
1386 unsigned i;
1387 tree var;
1388 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1389 {
1390 asan_poison_variable (var, poison, seq_p);
1391
1392 /* Add use_after_scope_memory attribute for the variable in order
1393 to prevent re-written into SSA. */
1394 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1395 DECL_ATTRIBUTES (var)))
1396 DECL_ATTRIBUTES (var)
1397 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1398 integer_one_node,
1399 DECL_ATTRIBUTES (var));
1400 }
1401 }
1402
1403 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1404
1405 static enum gimplify_status
1406 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1407 {
1408 tree bind_expr = *expr_p;
1409 bool old_keep_stack = gimplify_ctxp->keep_stack;
1410 bool old_save_stack = gimplify_ctxp->save_stack;
1411 tree t;
1412 gbind *bind_stmt;
1413 gimple_seq body, cleanup;
1414 gcall *stack_save;
1415 location_t start_locus = 0, end_locus = 0;
1416 tree ret_clauses = NULL;
1417
1418 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1419
1420 /* Mark variables seen in this bind expr. */
1421 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1422 {
1423 if (VAR_P (t))
1424 {
1425 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1426 tree attr;
1427
1428 if (flag_openmp
1429 && !is_global_var (t)
1430 && !TREE_STATIC (t)
1431 && DECL_CONTEXT (t) == current_function_decl
1432 && TREE_USED (t)
1433 && (attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1434 != NULL_TREE)
1435 {
1436 gcc_assert (!DECL_HAS_VALUE_EXPR_P (t));
1437 tree alloc = TREE_PURPOSE (TREE_VALUE (attr));
1438 tree align = TREE_VALUE (TREE_VALUE (attr));
1439 /* Allocate directives that appear in a target region must specify
1440 an allocator clause unless a requires directive with the
1441 dynamic_allocators clause is present in the same compilation
1442 unit. */
1443 bool missing_dyn_alloc = false;
1444 if (alloc == NULL_TREE
1445 && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS)
1446 == 0))
1447 {
1448 /* This comes too early for omp_discover_declare_target...,
1449 but should at least catch the most common cases. */
1450 missing_dyn_alloc
1451 = cgraph_node::get (current_function_decl)->offloadable;
1452 for (struct gimplify_omp_ctx *ctx2 = ctx;
1453 ctx2 && !missing_dyn_alloc; ctx2 = ctx2->outer_context)
1454 if (ctx2->code == OMP_TARGET)
1455 missing_dyn_alloc = true;
1456 }
1457 if (missing_dyn_alloc)
1458 error_at (DECL_SOURCE_LOCATION (t),
1459 "%<allocate%> directive for %qD inside a target "
1460 "region must specify an %<allocator%> clause", t);
1461 /* Skip for omp_default_mem_alloc (= 1),
1462 unless align is present. For C/C++, there should be always a
1463 statement list following if TREE_USED, except for, e.g., using
1464 this decl in a static_assert; in that case, only a single
1465 DECL_EXPR remains, which can be skipped here. */
1466 else if (!errorcount
1467 && (align != NULL_TREE
1468 || alloc == NULL_TREE
1469 || !integer_onep (alloc))
1470 && (lang_GNU_Fortran ()
1471 || (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1472 != DECL_EXPR)))
1473 {
1474 /* Fortran might already use a pointer type internally;
1475 use that pointer except for type(C_ptr) and type(C_funptr);
1476 note that normal proc pointers are rejected. */
1477 tree type = TREE_TYPE (t);
1478 tree tmp, v;
1479 if (lang_GNU_Fortran ()
1480 && POINTER_TYPE_P (type)
1481 && TREE_TYPE (type) != void_type_node
1482 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
1483 {
1484 type = TREE_TYPE (type);
1485 v = t;
1486 }
1487 else
1488 {
1489 tmp = build_pointer_type (type);
1490 v = create_tmp_var (tmp, get_name (t));
1491 DECL_IGNORED_P (v) = 0;
1492 DECL_ATTRIBUTES (v)
1493 = tree_cons (get_identifier ("omp allocate var"),
1494 build_tree_list (NULL_TREE, t),
1495 remove_attribute ("omp allocate",
1496 DECL_ATTRIBUTES (t)));
1497 tmp = build_fold_indirect_ref (v);
1498 TREE_THIS_NOTRAP (tmp) = 1;
1499 SET_DECL_VALUE_EXPR (t, tmp);
1500 DECL_HAS_VALUE_EXPR_P (t) = 1;
1501 }
1502 tree sz = TYPE_SIZE_UNIT (type);
1503 /* The size to use in Fortran might not match TYPE_SIZE_UNIT;
1504 hence, for some decls, a size variable is saved in the
1505 attributes; use it, if available. */
1506 if (TREE_CHAIN (TREE_VALUE (attr))
1507 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))
1508 && TREE_PURPOSE (
1509 TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))))
1510 {
1511 sz = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1512 sz = TREE_PURPOSE (sz);
1513 }
1514 if (alloc == NULL_TREE)
1515 alloc = build_zero_cst (ptr_type_node);
1516 if (align == NULL_TREE)
1517 align = build_int_cst (size_type_node, DECL_ALIGN_UNIT (t));
1518 else
1519 align = build_int_cst (size_type_node,
1520 MAX (tree_to_uhwi (align),
1521 DECL_ALIGN_UNIT (t)));
1522 location_t loc = DECL_SOURCE_LOCATION (t);
1523 tmp = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
1524 tmp = build_call_expr_loc (loc, tmp, 3, align, sz, alloc);
1525 tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1526 fold_convert (TREE_TYPE (v), tmp));
1527 gcc_assert (BIND_EXPR_BODY (bind_expr) != NULL_TREE);
1528 /* Ensure that either TREE_CHAIN (TREE_VALUE (attr) is set
1529 and GOMP_FREE added here or that DECL_HAS_VALUE_EXPR_P (t)
1530 is set, using in a condition much further below. */
1531 gcc_assert (DECL_HAS_VALUE_EXPR_P (t)
1532 || TREE_CHAIN (TREE_VALUE (attr)));
1533 if (TREE_CHAIN (TREE_VALUE (attr)))
1534 {
1535 /* Fortran is special as it does not have properly nest
1536 declarations in blocks. And as there is no
1537 initializer, there is also no expression to look for.
1538 Hence, the FE makes the statement list of the
1539 try-finally block available. We can put the GOMP_alloc
1540 at the top, unless an allocator or size expression
1541 requires to put it afterward; note that the size is
1542 always later in generated code; for strings, no
1543 size expr but still an expr might be available.
1544 As LTO does not handle a statement list, 'sl' has
1545 to be removed; done so by removing the attribute. */
1546 DECL_ATTRIBUTES (t)
1547 = remove_attribute ("omp allocate",
1548 DECL_ATTRIBUTES (t));
1549 tree sl = TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)));
1550 tree_stmt_iterator e = tsi_start (sl);
1551 tree needle = NULL_TREE;
1552 if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1553 {
1554 needle = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1555 needle = (TREE_VALUE (needle) ? TREE_VALUE (needle)
1556 : sz);
1557 }
1558 else if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1559 needle = sz;
1560 else if (DECL_P (alloc) && DECL_ARTIFICIAL (alloc))
1561 needle = alloc;
1562
1563 if (needle != NULL_TREE)
1564 {
1565 while (!tsi_end_p (e))
1566 {
1567 if (*e == needle
1568 || (TREE_CODE (*e) == MODIFY_EXPR
1569 && TREE_OPERAND (*e, 0) == needle))
1570 break;
1571 ++e;
1572 }
1573 gcc_assert (!tsi_end_p (e));
1574 }
1575 tsi_link_after (&e, tmp, TSI_SAME_STMT);
1576
1577 /* As the cleanup is in BIND_EXPR_BODY, GOMP_free is added
1578 here; for C/C++ it will be added in the 'cleanup'
1579 section after gimplification. But Fortran already has
1580 a try-finally block. */
1581 sl = TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)));
1582 e = tsi_last (sl);
1583 tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1584 tmp = build_call_expr_loc (EXPR_LOCATION (*e), tmp, 2, v,
1585 build_zero_cst (ptr_type_node));
1586 tsi_link_after (&e, tmp, TSI_SAME_STMT);
1587 tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1588 tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1589 fold_convert (TREE_TYPE (v), tmp));
1590 ++e;
1591 tsi_link_after (&e, tmp, TSI_SAME_STMT);
1592 }
1593 else
1594 {
1595 gcc_assert (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1596 == STATEMENT_LIST);
1597 tree_stmt_iterator e;
1598 e = tsi_start (BIND_EXPR_BODY (bind_expr));
1599 while (!tsi_end_p (e))
1600 {
1601 if ((TREE_CODE (*e) == DECL_EXPR
1602 && TREE_OPERAND (*e, 0) == t)
1603 || (TREE_CODE (*e) == CLEANUP_POINT_EXPR
1604 && (TREE_CODE (TREE_OPERAND (*e, 0))
1605 == DECL_EXPR)
1606 && (TREE_OPERAND (TREE_OPERAND (*e, 0), 0)
1607 == t)))
1608 break;
1609 ++e;
1610 }
1611 gcc_assert (!tsi_end_p (e));
1612 tsi_link_before (&e, tmp, TSI_SAME_STMT);
1613 }
1614 }
1615 }
1616
1617 /* Mark variable as local. */
1618 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1619 {
1620 if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1621 || splay_tree_lookup (ctx->variables,
1622 (splay_tree_key) t) == NULL)
1623 {
1624 int flag = GOVD_LOCAL;
1625 if (ctx->region_type == ORT_SIMD
1626 && TREE_ADDRESSABLE (t)
1627 && !TREE_STATIC (t))
1628 {
1629 if (TREE_CODE (DECL_SIZE_UNIT (t)) != INTEGER_CST)
1630 ctx->add_safelen1 = true;
1631 else
1632 flag = GOVD_PRIVATE;
1633 }
1634 omp_add_variable (ctx, t, flag | GOVD_SEEN);
1635 }
1636 /* Static locals inside of target construct or offloaded
1637 routines need to be "omp declare target". */
1638 if (TREE_STATIC (t))
1639 for (; ctx; ctx = ctx->outer_context)
1640 if ((ctx->region_type & ORT_TARGET) != 0)
1641 {
1642 if (!lookup_attribute ("omp declare target",
1643 DECL_ATTRIBUTES (t)))
1644 {
1645 tree id = get_identifier ("omp declare target");
1646 DECL_ATTRIBUTES (t)
1647 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
1648 varpool_node *node = varpool_node::get (t);
1649 if (node)
1650 {
1651 node->offloadable = 1;
1652 if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
1653 {
1654 g->have_offload = true;
1655 if (!in_lto_p)
1656 vec_safe_push (offload_vars, t);
1657 }
1658 }
1659 }
1660 break;
1661 }
1662 }
1663
1664 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1665
1666 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1667 cfun->has_local_explicit_reg_vars = true;
1668 }
1669 }
1670
1671 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1672 BIND_EXPR_BLOCK (bind_expr));
1673 gimple_push_bind_expr (bind_stmt);
1674
1675 gimplify_ctxp->keep_stack = false;
1676 gimplify_ctxp->save_stack = false;
1677
1678 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1679 body = NULL;
1680 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1681 gimple_bind_set_body (bind_stmt, body);
1682
1683 /* Source location wise, the cleanup code (stack_restore and clobbers)
1684 belongs to the end of the block, so propagate what we have. The
1685 stack_save operation belongs to the beginning of block, which we can
1686 infer from the bind_expr directly if the block has no explicit
1687 assignment. */
1688 if (BIND_EXPR_BLOCK (bind_expr))
1689 {
1690 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1691 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1692 }
1693 if (start_locus == 0)
1694 start_locus = EXPR_LOCATION (bind_expr);
1695
1696 cleanup = NULL;
1697 stack_save = NULL;
1698
1699 /* Add clobbers for all variables that go out of scope. */
1700 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1701 {
1702 if (VAR_P (t)
1703 && !is_global_var (t)
1704 && DECL_CONTEXT (t) == current_function_decl)
1705 {
1706 if (flag_openmp
1707 && DECL_HAS_VALUE_EXPR_P (t)
1708 && TREE_USED (t)
1709 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1710 {
1711 /* For Fortran, TREE_CHAIN (TREE_VALUE (attr)) is set, which
1712 causes that the GOMP_free call is already added above;
1713 and "omp allocate" is removed from DECL_ATTRIBUTES. */
1714 tree v = TREE_OPERAND (DECL_VALUE_EXPR (t), 0);
1715 tree tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1716 tmp = build_call_expr_loc (end_locus, tmp, 2, v,
1717 build_zero_cst (ptr_type_node));
1718 gimplify_and_add (tmp, &cleanup);
1719 gimple *clobber_stmt;
1720 tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1721 clobber_stmt = gimple_build_assign (v, tmp);
1722 gimple_set_location (clobber_stmt, end_locus);
1723 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1724 }
1725 if (!DECL_HARD_REGISTER (t)
1726 && !TREE_THIS_VOLATILE (t)
1727 && !DECL_HAS_VALUE_EXPR_P (t)
1728 /* Only care for variables that have to be in memory. Others
1729 will be rewritten into SSA names, hence moved to the
1730 top-level. */
1731 && !is_gimple_reg (t)
1732 && flag_stack_reuse != SR_NONE)
1733 {
1734 tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
1735 gimple *clobber_stmt;
1736 clobber_stmt = gimple_build_assign (t, clobber);
1737 gimple_set_location (clobber_stmt, end_locus);
1738 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1739 }
1740
1741 if (flag_openacc && oacc_declare_returns != NULL)
1742 {
1743 tree key = t;
1744 if (DECL_HAS_VALUE_EXPR_P (key))
1745 {
1746 key = DECL_VALUE_EXPR (key);
1747 if (INDIRECT_REF_P (key))
1748 key = TREE_OPERAND (key, 0);
1749 }
1750 tree *c = oacc_declare_returns->get (key);
1751 if (c != NULL)
1752 {
1753 if (ret_clauses)
1754 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1755
1756 ret_clauses = unshare_expr (*c);
1757
1758 oacc_declare_returns->remove (key);
1759
1760 if (oacc_declare_returns->is_empty ())
1761 {
1762 delete oacc_declare_returns;
1763 oacc_declare_returns = NULL;
1764 }
1765 }
1766 }
1767 }
1768
1769 if (asan_poisoned_variables != NULL
1770 && asan_poisoned_variables->contains (t))
1771 {
1772 asan_poisoned_variables->remove (t);
1773 asan_poison_variable (t, true, &cleanup);
1774 }
1775
1776 if (gimplify_ctxp->live_switch_vars != NULL
1777 && gimplify_ctxp->live_switch_vars->contains (t))
1778 gimplify_ctxp->live_switch_vars->remove (t);
1779 }
1780
1781 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1782 the stack space allocated to the VLAs. */
1783 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1784 {
1785 gcall *stack_restore;
1786
1787 /* Save stack on entry and restore it on exit. Add a try_finally
1788 block to achieve this. */
1789 build_stack_save_restore (&stack_save, &stack_restore);
1790
1791 gimple_set_location (stack_save, start_locus);
1792 gimple_set_location (stack_restore, end_locus);
1793
1794 gimplify_seq_add_stmt (&cleanup, stack_restore);
1795 }
1796
1797 if (ret_clauses)
1798 {
1799 gomp_target *stmt;
1800 gimple_stmt_iterator si = gsi_start (cleanup);
1801
1802 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1803 ret_clauses);
1804 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1805 }
1806
1807 if (cleanup)
1808 {
1809 gtry *gs;
1810 gimple_seq new_body;
1811
1812 new_body = NULL;
1813 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1814 GIMPLE_TRY_FINALLY);
1815
1816 if (stack_save)
1817 gimplify_seq_add_stmt (&new_body, stack_save);
1818 gimplify_seq_add_stmt (&new_body, gs);
1819 gimple_bind_set_body (bind_stmt, new_body);
1820 }
1821
1822 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1823 if (!gimplify_ctxp->keep_stack)
1824 gimplify_ctxp->keep_stack = old_keep_stack;
1825 gimplify_ctxp->save_stack = old_save_stack;
1826
1827 gimple_pop_bind_expr ();
1828
1829 gimplify_seq_add_stmt (pre_p, bind_stmt);
1830
1831 if (temp)
1832 {
1833 *expr_p = temp;
1834 return GS_OK;
1835 }
1836
1837 *expr_p = NULL_TREE;
1838 return GS_ALL_DONE;
1839 }
1840
1841 /* Maybe add early return predict statement to PRE_P sequence. */
1842
1843 static void
1844 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1845 {
1846 /* If we are not in a conditional context, add PREDICT statement. */
1847 if (gimple_conditional_context ())
1848 {
1849 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1850 NOT_TAKEN);
1851 gimplify_seq_add_stmt (pre_p, predict);
1852 }
1853 }
1854
1855 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1856 GIMPLE value, it is assigned to a new temporary and the statement is
1857 re-written to return the temporary.
1858
1859 PRE_P points to the sequence where side effects that must happen before
1860 STMT should be stored. */
1861
1862 static enum gimplify_status
1863 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1864 {
1865 greturn *ret;
1866 tree ret_expr = TREE_OPERAND (stmt, 0);
1867 tree result_decl, result;
1868
1869 if (ret_expr == error_mark_node)
1870 return GS_ERROR;
1871
1872 if (!ret_expr
1873 || TREE_CODE (ret_expr) == RESULT_DECL)
1874 {
1875 maybe_add_early_return_predict_stmt (pre_p);
1876 greturn *ret = gimple_build_return (ret_expr);
1877 copy_warning (ret, stmt);
1878 gimplify_seq_add_stmt (pre_p, ret);
1879 return GS_ALL_DONE;
1880 }
1881
1882 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1883 result_decl = NULL_TREE;
1884 else if (TREE_CODE (ret_expr) == COMPOUND_EXPR)
1885 {
1886 /* Used in C++ for handling EH cleanup of the return value if a local
1887 cleanup throws. Assume the front-end knows what it's doing. */
1888 result_decl = DECL_RESULT (current_function_decl);
1889 /* But crash if we end up trying to modify ret_expr below. */
1890 ret_expr = NULL_TREE;
1891 }
1892 else
1893 {
1894 result_decl = TREE_OPERAND (ret_expr, 0);
1895
1896 /* See through a return by reference. */
1897 if (INDIRECT_REF_P (result_decl))
1898 result_decl = TREE_OPERAND (result_decl, 0);
1899
1900 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1901 || TREE_CODE (ret_expr) == INIT_EXPR)
1902 && TREE_CODE (result_decl) == RESULT_DECL);
1903 }
1904
1905 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1906 Recall that aggregate_value_p is FALSE for any aggregate type that is
1907 returned in registers. If we're returning values in registers, then
1908 we don't want to extend the lifetime of the RESULT_DECL, particularly
1909 across another call. In addition, for those aggregates for which
1910 hard_function_value generates a PARALLEL, we'll die during normal
1911 expansion of structure assignments; there's special code in expand_return
1912 to handle this case that does not exist in expand_expr. */
1913 if (!result_decl)
1914 result = NULL_TREE;
1915 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1916 {
1917 if (!poly_int_tree_p (DECL_SIZE (result_decl)))
1918 {
1919 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1920 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1921 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1922 should be effectively allocated by the caller, i.e. all calls to
1923 this function must be subject to the Return Slot Optimization. */
1924 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1925 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1926 }
1927 result = result_decl;
1928 }
1929 else if (gimplify_ctxp->return_temp)
1930 result = gimplify_ctxp->return_temp;
1931 else
1932 {
1933 result = create_tmp_reg (TREE_TYPE (result_decl));
1934
1935 /* ??? With complex control flow (usually involving abnormal edges),
1936 we can wind up warning about an uninitialized value for this. Due
1937 to how this variable is constructed and initialized, this is never
1938 true. Give up and never warn. */
1939 suppress_warning (result, OPT_Wuninitialized);
1940
1941 gimplify_ctxp->return_temp = result;
1942 }
1943
1944 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1945 Then gimplify the whole thing. */
1946 if (result != result_decl)
1947 TREE_OPERAND (ret_expr, 0) = result;
1948
1949 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1950
1951 maybe_add_early_return_predict_stmt (pre_p);
1952 ret = gimple_build_return (result);
1953 copy_warning (ret, stmt);
1954 gimplify_seq_add_stmt (pre_p, ret);
1955
1956 return GS_ALL_DONE;
1957 }
1958
1959 /* Gimplify a variable-length array DECL. */
1960
1961 static void
1962 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1963 {
1964 /* This is a variable-sized decl. Simplify its size and mark it
1965 for deferred expansion. */
1966 tree t, addr, ptr_type;
1967
1968 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1969 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1970
1971 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1972 if (DECL_HAS_VALUE_EXPR_P (decl))
1973 return;
1974
1975 /* All occurrences of this decl in final gimplified code will be
1976 replaced by indirection. Setting DECL_VALUE_EXPR does two
1977 things: First, it lets the rest of the gimplifier know what
1978 replacement to use. Second, it lets the debug info know
1979 where to find the value. */
1980 ptr_type = build_pointer_type (TREE_TYPE (decl));
1981 addr = create_tmp_var (ptr_type, get_name (decl));
1982 DECL_IGNORED_P (addr) = 0;
1983 t = build_fold_indirect_ref (addr);
1984 TREE_THIS_NOTRAP (t) = 1;
1985 SET_DECL_VALUE_EXPR (decl, t);
1986 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1987
1988 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1989 max_int_size_in_bytes (TREE_TYPE (decl)));
1990 /* The call has been built for a variable-sized object. */
1991 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1992 t = fold_convert (ptr_type, t);
1993 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1994
1995 gimplify_and_add (t, seq_p);
1996
1997 /* Record the dynamic allocation associated with DECL if requested. */
1998 if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
1999 record_dynamic_alloc (decl);
2000 }
2001
2002 /* A helper function to be called via walk_tree. Mark all labels under *TP
2003 as being forced. To be called for DECL_INITIAL of static variables. */
2004
2005 static tree
2006 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2007 {
2008 if (TYPE_P (*tp))
2009 *walk_subtrees = 0;
2010 if (TREE_CODE (*tp) == LABEL_DECL)
2011 {
2012 FORCED_LABEL (*tp) = 1;
2013 cfun->has_forced_label_in_static = 1;
2014 }
2015
2016 return NULL_TREE;
2017 }
2018
2019 /* Generate an initialization to automatic variable DECL based on INIT_TYPE.
2020 Build a call to internal const function DEFERRED_INIT:
2021 1st argument: SIZE of the DECL;
2022 2nd argument: INIT_TYPE;
2023 3rd argument: NAME of the DECL;
2024
2025 as LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL). */
2026
2027 static void
2028 gimple_add_init_for_auto_var (tree decl,
2029 enum auto_init_type init_type,
2030 gimple_seq *seq_p)
2031 {
2032 gcc_assert (auto_var_p (decl));
2033 gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
2034
2035 const location_t loc = DECL_SOURCE_LOCATION (decl);
2036 tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
2037 tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
2038 tree decl_name;
2039
2040 if (DECL_NAME (decl))
2041 decl_name = build_string_literal (DECL_NAME (decl));
2042 else
2043 {
2044 char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
2045 sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
2046 decl_name = build_string_literal (decl_name_anonymous);
2047 }
2048
2049 tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
2050 TREE_TYPE (decl), 3,
2051 decl_size, init_type_node,
2052 decl_name);
2053
2054 gimplify_assign (decl, call, seq_p);
2055 }
2056
2057 /* Generate padding initialization for automatic variable DECL.
2058 C guarantees that brace-init with fewer initializers than members
2059 aggregate will initialize the rest of the aggregate as-if it were
2060 static initialization. In turn static initialization guarantees
2061 that padding is initialized to zero. So, we always initialize paddings
2062 to zeroes regardless INIT_TYPE.
2063 To do the padding initialization, we insert a call to
2064 __builtin_clear_padding (&decl, 0, for_auto_init = true).
2065 Note, we add an additional dummy argument for __builtin_clear_padding,
2066 'for_auto_init' to distinguish whether this call is for automatic
2067 variable initialization or not.
2068 */
2069 static void
2070 gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
2071 gimple_seq *seq_p)
2072 {
2073 tree addr_of_decl = NULL_TREE;
2074 tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
2075
2076 if (is_vla)
2077 {
2078 /* The temporary address variable for this vla should be
2079 created in gimplify_vla_decl. */
2080 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
2081 gcc_assert (INDIRECT_REF_P (DECL_VALUE_EXPR (decl)));
2082 addr_of_decl = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
2083 }
2084 else
2085 {
2086 mark_addressable (decl);
2087 addr_of_decl = build_fold_addr_expr (decl);
2088 }
2089
2090 gimple *call = gimple_build_call (fn, 2, addr_of_decl,
2091 build_one_cst (TREE_TYPE (addr_of_decl)));
2092 gimplify_seq_add_stmt (seq_p, call);
2093 }
2094
2095 /* Return true if the DECL need to be automaticly initialized by the
2096 compiler. */
2097 static bool
2098 is_var_need_auto_init (tree decl)
2099 {
2100 if (auto_var_p (decl)
2101 && (TREE_CODE (decl) != VAR_DECL
2102 || !DECL_HARD_REGISTER (decl))
2103 && (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2104 && (!lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl)))
2105 && !OPAQUE_TYPE_P (TREE_TYPE (decl))
2106 && !is_empty_type (TREE_TYPE (decl)))
2107 return true;
2108 return false;
2109 }
2110
2111 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
2112 and initialization explicit. */
2113
2114 static enum gimplify_status
2115 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
2116 {
2117 tree stmt = *stmt_p;
2118 tree decl = DECL_EXPR_DECL (stmt);
2119
2120 *stmt_p = NULL_TREE;
2121
2122 if (TREE_TYPE (decl) == error_mark_node)
2123 return GS_ERROR;
2124
2125 if ((TREE_CODE (decl) == TYPE_DECL
2126 || VAR_P (decl))
2127 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
2128 {
2129 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
2130 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
2131 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
2132 }
2133
2134 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
2135 in case its size expressions contain problematic nodes like CALL_EXPR. */
2136 if (TREE_CODE (decl) == TYPE_DECL
2137 && DECL_ORIGINAL_TYPE (decl)
2138 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
2139 {
2140 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
2141 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
2142 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
2143 }
2144
2145 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
2146 {
2147 tree init = DECL_INITIAL (decl);
2148 bool is_vla = false;
2149 /* Check whether a decl has FE created VALUE_EXPR here BEFORE
2150 gimplify_vla_decl creates VALUE_EXPR for a vla decl.
2151 If the decl has VALUE_EXPR that was created by FE (usually
2152 C++FE), it's a proxy varaible, and FE already initialized
2153 the VALUE_EXPR of it, we should not initialize it anymore. */
2154 bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
2155
2156 poly_uint64 size;
2157 if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
2158 || (!TREE_STATIC (decl)
2159 && flag_stack_check == GENERIC_STACK_CHECK
2160 && maybe_gt (size,
2161 (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
2162 {
2163 gimplify_vla_decl (decl, seq_p);
2164 is_vla = true;
2165 }
2166
2167 if (asan_poisoned_variables
2168 && !is_vla
2169 && TREE_ADDRESSABLE (decl)
2170 && !TREE_STATIC (decl)
2171 && !DECL_HAS_VALUE_EXPR_P (decl)
2172 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
2173 && dbg_cnt (asan_use_after_scope)
2174 && !gimplify_omp_ctxp
2175 /* GNAT introduces temporaries to hold return values of calls in
2176 initializers of variables defined in other units, so the
2177 declaration of the variable is discarded completely. We do not
2178 want to issue poison calls for such dropped variables. */
2179 && (DECL_SEEN_IN_BIND_EXPR_P (decl)
2180 || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
2181 {
2182 asan_poisoned_variables->add (decl);
2183 asan_poison_variable (decl, false, seq_p);
2184 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
2185 gimplify_ctxp->live_switch_vars->add (decl);
2186 }
2187
2188 /* Some front ends do not explicitly declare all anonymous
2189 artificial variables. We compensate here by declaring the
2190 variables, though it would be better if the front ends would
2191 explicitly declare them. */
2192 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
2193 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
2194 gimple_add_tmp_var (decl);
2195
2196 if (init && init != error_mark_node)
2197 {
2198 if (!TREE_STATIC (decl))
2199 {
2200 DECL_INITIAL (decl) = NULL_TREE;
2201 init = build2 (INIT_EXPR, void_type_node, decl, init);
2202 gimplify_and_add (init, seq_p);
2203 ggc_free (init);
2204 /* Clear TREE_READONLY if we really have an initialization. */
2205 if (!DECL_INITIAL (decl)
2206 && !omp_privatize_by_reference (decl))
2207 TREE_READONLY (decl) = 0;
2208 }
2209 else
2210 /* We must still examine initializers for static variables
2211 as they may contain a label address. */
2212 walk_tree (&init, force_labels_r, NULL, NULL);
2213 }
2214 /* When there is no explicit initializer, if the user requested,
2215 We should insert an artifical initializer for this automatic
2216 variable. */
2217 else if (is_var_need_auto_init (decl)
2218 && !decl_had_value_expr_p)
2219 {
2220 gimple_add_init_for_auto_var (decl,
2221 flag_auto_var_init,
2222 seq_p);
2223 /* The expanding of a call to the above .DEFERRED_INIT will apply
2224 block initialization to the whole space covered by this variable.
2225 As a result, all the paddings will be initialized to zeroes
2226 for zero initialization and 0xFE byte-repeatable patterns for
2227 pattern initialization.
2228 In order to make the paddings as zeroes for pattern init, We
2229 should add a call to __builtin_clear_padding to clear the
2230 paddings to zero in compatiple with CLANG.
2231 We cannot insert this call if the variable is a gimple register
2232 since __builtin_clear_padding will take the address of the
2233 variable. As a result, if a long double/_Complex long double
2234 variable will spilled into stack later, its padding is 0XFE. */
2235 if (flag_auto_var_init == AUTO_INIT_PATTERN
2236 && !is_gimple_reg (decl)
2237 && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
2238 gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
2239 }
2240 }
2241
2242 return GS_ALL_DONE;
2243 }
2244
2245 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
2246 and replacing the LOOP_EXPR with goto, but if the loop contains an
2247 EXIT_EXPR, we need to append a label for it to jump to. */
2248
2249 static enum gimplify_status
2250 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
2251 {
2252 tree saved_label = gimplify_ctxp->exit_label;
2253 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
2254
2255 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
2256
2257 gimplify_ctxp->exit_label = NULL_TREE;
2258
2259 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
2260
2261 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
2262
2263 if (gimplify_ctxp->exit_label)
2264 gimplify_seq_add_stmt (pre_p,
2265 gimple_build_label (gimplify_ctxp->exit_label));
2266
2267 gimplify_ctxp->exit_label = saved_label;
2268
2269 *expr_p = NULL;
2270 return GS_ALL_DONE;
2271 }
2272
2273 /* Gimplify a statement list onto a sequence. These may be created either
2274 by an enlightened front-end, or by shortcut_cond_expr. */
2275
2276 static enum gimplify_status
2277 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
2278 {
2279 tree temp = voidify_wrapper_expr (*expr_p, NULL);
2280
2281 tree_stmt_iterator i = tsi_start (*expr_p);
2282
2283 while (!tsi_end_p (i))
2284 {
2285 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
2286 tsi_delink (&i);
2287 }
2288
2289 if (temp)
2290 {
2291 *expr_p = temp;
2292 return GS_OK;
2293 }
2294
2295 return GS_ALL_DONE;
2296 }
2297
2298
2299 /* Emit warning for the unreachable statment STMT if needed.
2300 Return the gimple itself when the warning is emitted, otherwise
2301 return NULL. */
2302 static gimple *
2303 emit_warn_switch_unreachable (gimple *stmt)
2304 {
2305 if (gimple_code (stmt) == GIMPLE_GOTO
2306 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
2307 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
2308 /* Don't warn for compiler-generated gotos. These occur
2309 in Duff's devices, for example. */
2310 return NULL;
2311 else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2312 && ((gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2313 || (gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
2314 && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
2315 || (is_gimple_assign (stmt)
2316 && gimple_assign_single_p (stmt)
2317 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2318 && gimple_call_internal_p (
2319 SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)),
2320 IFN_DEFERRED_INIT))))
2321 /* Don't warn for compiler-generated initializations for
2322 -ftrivial-auto-var-init.
2323 There are 3 cases:
2324 case 1: a call to .DEFERRED_INIT;
2325 case 2: a call to __builtin_clear_padding with the 2nd argument is
2326 present and non-zero;
2327 case 3: a gimple assign store right after the call to .DEFERRED_INIT
2328 that has the LHS of .DEFERRED_INIT as the RHS as following:
2329 _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
2330 i1 = _1. */
2331 return NULL;
2332 else
2333 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
2334 "statement will never be executed");
2335 return stmt;
2336 }
2337
2338 /* Callback for walk_gimple_seq. */
2339
2340 static tree
2341 warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
2342 bool *handled_ops_p,
2343 struct walk_stmt_info *wi)
2344 {
2345 gimple *stmt = gsi_stmt (*gsi_p);
2346 bool unreachable_issued = wi->info != NULL;
2347
2348 *handled_ops_p = true;
2349 switch (gimple_code (stmt))
2350 {
2351 case GIMPLE_TRY:
2352 /* A compiler-generated cleanup or a user-written try block.
2353 If it's empty, don't dive into it--that would result in
2354 worse location info. */
2355 if (gimple_try_eval (stmt) == NULL)
2356 {
2357 if (warn_switch_unreachable && !unreachable_issued)
2358 wi->info = emit_warn_switch_unreachable (stmt);
2359
2360 /* Stop when auto var init warning is not on. */
2361 if (!warn_trivial_auto_var_init)
2362 return integer_zero_node;
2363 }
2364 /* Fall through. */
2365 case GIMPLE_BIND:
2366 case GIMPLE_CATCH:
2367 case GIMPLE_EH_FILTER:
2368 case GIMPLE_TRANSACTION:
2369 /* Walk the sub-statements. */
2370 *handled_ops_p = false;
2371 break;
2372
2373 case GIMPLE_DEBUG:
2374 /* Ignore these. We may generate them before declarations that
2375 are never executed. If there's something to warn about,
2376 there will be non-debug stmts too, and we'll catch those. */
2377 break;
2378
2379 case GIMPLE_LABEL:
2380 /* Stop till the first Label. */
2381 return integer_zero_node;
2382 case GIMPLE_CALL:
2383 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2384 {
2385 *handled_ops_p = false;
2386 break;
2387 }
2388 if (warn_trivial_auto_var_init
2389 && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2390 && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2391 {
2392 /* Get the variable name from the 3rd argument of call. */
2393 tree var_name = gimple_call_arg (stmt, 2);
2394 var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
2395 const char *var_name_str = TREE_STRING_POINTER (var_name);
2396
2397 warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
2398 "%qs cannot be initialized with "
2399 "%<-ftrivial-auto-var_init%>",
2400 var_name_str);
2401 break;
2402 }
2403
2404 /* Fall through. */
2405 default:
2406 /* check the first "real" statement (not a decl/lexical scope/...), issue
2407 warning if needed. */
2408 if (warn_switch_unreachable && !unreachable_issued)
2409 wi->info = emit_warn_switch_unreachable (stmt);
2410 /* Stop when auto var init warning is not on. */
2411 if (!warn_trivial_auto_var_init)
2412 return integer_zero_node;
2413 break;
2414 }
2415 return NULL_TREE;
2416 }
2417
2418
2419 /* Possibly warn about unreachable statements between switch's controlling
2420 expression and the first case. Also warn about -ftrivial-auto-var-init
2421 cannot initialize the auto variable under such situation.
2422 SEQ is the body of a switch expression. */
2423
2424 static void
2425 maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
2426 {
2427 if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
2428 /* This warning doesn't play well with Fortran when optimizations
2429 are on. */
2430 || lang_GNU_Fortran ()
2431 || seq == NULL)
2432 return;
2433
2434 struct walk_stmt_info wi;
2435
2436 memset (&wi, 0, sizeof (wi));
2437 walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
2438 }
2439
2440
2441 /* A label entry that pairs label and a location. */
2442 struct label_entry
2443 {
2444 tree label;
2445 location_t loc;
2446 };
2447
2448 /* Find LABEL in vector of label entries VEC. */
2449
2450 static struct label_entry *
2451 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
2452 {
2453 unsigned int i;
2454 struct label_entry *l;
2455
2456 FOR_EACH_VEC_ELT (*vec, i, l)
2457 if (l->label == label)
2458 return l;
2459 return NULL;
2460 }
2461
2462 /* Return true if LABEL, a LABEL_DECL, represents a case label
2463 in a vector of labels CASES. */
2464
2465 static bool
2466 case_label_p (const vec<tree> *cases, tree label)
2467 {
2468 unsigned int i;
2469 tree l;
2470
2471 FOR_EACH_VEC_ELT (*cases, i, l)
2472 if (CASE_LABEL (l) == label)
2473 return true;
2474 return false;
2475 }
2476
2477 /* Find the last nondebug statement in a scope STMT. */
2478
2479 static gimple *
2480 last_stmt_in_scope (gimple *stmt)
2481 {
2482 if (!stmt)
2483 return NULL;
2484
2485 switch (gimple_code (stmt))
2486 {
2487 case GIMPLE_BIND:
2488 {
2489 gbind *bind = as_a <gbind *> (stmt);
2490 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
2491 return last_stmt_in_scope (stmt);
2492 }
2493
2494 case GIMPLE_TRY:
2495 {
2496 gtry *try_stmt = as_a <gtry *> (stmt);
2497 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
2498 gimple *last_eval = last_stmt_in_scope (stmt);
2499 if (gimple_stmt_may_fallthru (last_eval)
2500 && (last_eval == NULL
2501 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
2502 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2503 {
2504 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
2505 return last_stmt_in_scope (stmt);
2506 }
2507 else
2508 return last_eval;
2509 }
2510
2511 case GIMPLE_DEBUG:
2512 gcc_unreachable ();
2513
2514 default:
2515 return stmt;
2516 }
2517 }
2518
2519 /* Collect labels that may fall through into LABELS and return the statement
2520 preceding another case label, or a user-defined label. Store a location
2521 useful to give warnings at *PREVLOC (usually the location of the returned
2522 statement or of its surrounding scope). */
2523
2524 static gimple *
2525 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
2526 auto_vec <struct label_entry> *labels,
2527 location_t *prevloc)
2528 {
2529 gimple *prev = NULL;
2530
2531 *prevloc = UNKNOWN_LOCATION;
2532 do
2533 {
2534 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
2535 {
2536 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
2537 which starts on a GIMPLE_SWITCH and ends with a break label.
2538 Handle that as a single statement that can fall through. */
2539 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
2540 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
2541 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
2542 if (last
2543 && gimple_code (first) == GIMPLE_SWITCH
2544 && gimple_code (last) == GIMPLE_LABEL)
2545 {
2546 tree label = gimple_label_label (as_a <glabel *> (last));
2547 if (SWITCH_BREAK_LABEL_P (label))
2548 {
2549 prev = bind;
2550 gsi_next (gsi_p);
2551 continue;
2552 }
2553 }
2554 }
2555 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
2556 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2557 {
2558 /* Nested scope. Only look at the last statement of
2559 the innermost scope. */
2560 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2561 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2562 if (last)
2563 {
2564 prev = last;
2565 /* It might be a label without a location. Use the
2566 location of the scope then. */
2567 if (!gimple_has_location (prev))
2568 *prevloc = bind_loc;
2569 }
2570 gsi_next (gsi_p);
2571 continue;
2572 }
2573
2574 /* Ifs are tricky. */
2575 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2576 {
2577 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2578 tree false_lab = gimple_cond_false_label (cond_stmt);
2579 location_t if_loc = gimple_location (cond_stmt);
2580
2581 /* If we have e.g.
2582 if (i > 1) goto <D.2259>; else goto D;
2583 we can't do much with the else-branch. */
2584 if (!DECL_ARTIFICIAL (false_lab))
2585 break;
2586
2587 /* Go on until the false label, then one step back. */
2588 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2589 {
2590 gimple *stmt = gsi_stmt (*gsi_p);
2591 if (gimple_code (stmt) == GIMPLE_LABEL
2592 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2593 break;
2594 }
2595
2596 /* Not found? Oops. */
2597 if (gsi_end_p (*gsi_p))
2598 break;
2599
2600 /* A dead label can't fall through. */
2601 if (!UNUSED_LABEL_P (false_lab))
2602 {
2603 struct label_entry l = { false_lab, if_loc };
2604 labels->safe_push (l);
2605 }
2606
2607 /* Go to the last statement of the then branch. */
2608 gsi_prev (gsi_p);
2609
2610 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2611 <D.1759>:
2612 <stmt>;
2613 goto <D.1761>;
2614 <D.1760>:
2615 */
2616 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2617 && !gimple_has_location (gsi_stmt (*gsi_p)))
2618 {
2619 /* Look at the statement before, it might be
2620 attribute fallthrough, in which case don't warn. */
2621 gsi_prev (gsi_p);
2622 bool fallthru_before_dest
2623 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2624 gsi_next (gsi_p);
2625 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2626 if (!fallthru_before_dest)
2627 {
2628 struct label_entry l = { goto_dest, if_loc };
2629 labels->safe_push (l);
2630 }
2631 }
2632 /* This case is about
2633 if (1 != 0) goto <D.2022>; else goto <D.2023>;
2634 <D.2022>:
2635 n = n + 1; // #1
2636 <D.2023>: // #2
2637 <D.1988>: // #3
2638 where #2 is UNUSED_LABEL_P and we want to warn about #1 falling
2639 through to #3. So set PREV to #1. */
2640 else if (UNUSED_LABEL_P (false_lab))
2641 prev = gsi_stmt (*gsi_p);
2642
2643 /* And move back. */
2644 gsi_next (gsi_p);
2645 }
2646
2647 /* Remember the last statement. Skip labels that are of no interest
2648 to us. */
2649 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2650 {
2651 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2652 if (find_label_entry (labels, label))
2653 prev = gsi_stmt (*gsi_p);
2654 }
2655 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2656 ;
2657 else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
2658 ;
2659 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2660 prev = gsi_stmt (*gsi_p);
2661 gsi_next (gsi_p);
2662 }
2663 while (!gsi_end_p (*gsi_p)
2664 /* Stop if we find a case or a user-defined label. */
2665 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2666 || !gimple_has_location (gsi_stmt (*gsi_p))));
2667
2668 if (prev && gimple_has_location (prev))
2669 *prevloc = gimple_location (prev);
2670 return prev;
2671 }
2672
2673 /* Return true if the switch fallthough warning should occur. LABEL is
2674 the label statement that we're falling through to. */
2675
2676 static bool
2677 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2678 {
2679 gimple_stmt_iterator gsi = *gsi_p;
2680
2681 /* Don't warn if the label is marked with a "falls through" comment. */
2682 if (FALLTHROUGH_LABEL_P (label))
2683 return false;
2684
2685 /* Don't warn for non-case labels followed by a statement:
2686 case 0:
2687 foo ();
2688 label:
2689 bar ();
2690 as these are likely intentional. */
2691 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2692 {
2693 tree l;
2694 while (!gsi_end_p (gsi)
2695 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2696 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2697 && !case_label_p (&gimplify_ctxp->case_labels, l))
2698 gsi_next_nondebug (&gsi);
2699 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2700 return false;
2701 }
2702
2703 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2704 immediately breaks. */
2705 gsi = *gsi_p;
2706
2707 /* Skip all immediately following labels. */
2708 while (!gsi_end_p (gsi)
2709 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2710 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2711 gsi_next_nondebug (&gsi);
2712
2713 /* { ... something; default:; } */
2714 if (gsi_end_p (gsi)
2715 /* { ... something; default: break; } or
2716 { ... something; default: goto L; } */
2717 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2718 /* { ... something; default: return; } */
2719 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2720 return false;
2721
2722 return true;
2723 }
2724
2725 /* Callback for walk_gimple_seq. */
2726
2727 static tree
2728 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2729 struct walk_stmt_info *)
2730 {
2731 gimple *stmt = gsi_stmt (*gsi_p);
2732
2733 *handled_ops_p = true;
2734 switch (gimple_code (stmt))
2735 {
2736 case GIMPLE_TRY:
2737 case GIMPLE_BIND:
2738 case GIMPLE_CATCH:
2739 case GIMPLE_EH_FILTER:
2740 case GIMPLE_TRANSACTION:
2741 /* Walk the sub-statements. */
2742 *handled_ops_p = false;
2743 break;
2744
2745 /* Find a sequence of form:
2746
2747 GIMPLE_LABEL
2748 [...]
2749 <may fallthru stmt>
2750 GIMPLE_LABEL
2751
2752 and possibly warn. */
2753 case GIMPLE_LABEL:
2754 {
2755 /* Found a label. Skip all immediately following labels. */
2756 while (!gsi_end_p (*gsi_p)
2757 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2758 gsi_next_nondebug (gsi_p);
2759
2760 /* There might be no more statements. */
2761 if (gsi_end_p (*gsi_p))
2762 return integer_zero_node;
2763
2764 /* Vector of labels that fall through. */
2765 auto_vec <struct label_entry> labels;
2766 location_t prevloc;
2767 gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2768
2769 /* There might be no more statements. */
2770 if (gsi_end_p (*gsi_p))
2771 return integer_zero_node;
2772
2773 gimple *next = gsi_stmt (*gsi_p);
2774 tree label;
2775 /* If what follows is a label, then we may have a fallthrough. */
2776 if (gimple_code (next) == GIMPLE_LABEL
2777 && gimple_has_location (next)
2778 && (label = gimple_label_label (as_a <glabel *> (next)))
2779 && prev != NULL)
2780 {
2781 struct label_entry *l;
2782 bool warned_p = false;
2783 auto_diagnostic_group d;
2784 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2785 /* Quiet. */;
2786 else if (gimple_code (prev) == GIMPLE_LABEL
2787 && (label = gimple_label_label (as_a <glabel *> (prev)))
2788 && (l = find_label_entry (&labels, label)))
2789 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2790 "this statement may fall through");
2791 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2792 /* Try to be clever and don't warn when the statement
2793 can't actually fall through. */
2794 && gimple_stmt_may_fallthru (prev)
2795 && prevloc != UNKNOWN_LOCATION)
2796 warned_p = warning_at (prevloc,
2797 OPT_Wimplicit_fallthrough_,
2798 "this statement may fall through");
2799 if (warned_p)
2800 inform (gimple_location (next), "here");
2801
2802 /* Mark this label as processed so as to prevent multiple
2803 warnings in nested switches. */
2804 FALLTHROUGH_LABEL_P (label) = true;
2805
2806 /* So that next warn_implicit_fallthrough_r will start looking for
2807 a new sequence starting with this label. */
2808 gsi_prev (gsi_p);
2809 }
2810 }
2811 break;
2812 default:
2813 break;
2814 }
2815 return NULL_TREE;
2816 }
2817
2818 /* Warn when a switch case falls through. */
2819
2820 static void
2821 maybe_warn_implicit_fallthrough (gimple_seq seq)
2822 {
2823 if (!warn_implicit_fallthrough)
2824 return;
2825
2826 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2827 if (!(lang_GNU_C ()
2828 || lang_GNU_CXX ()
2829 || lang_GNU_OBJC ()))
2830 return;
2831
2832 struct walk_stmt_info wi;
2833 memset (&wi, 0, sizeof (wi));
2834 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2835 }
2836
2837 /* Callback for walk_gimple_seq. */
2838
2839 static tree
2840 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2841 struct walk_stmt_info *wi)
2842 {
2843 gimple *stmt = gsi_stmt (*gsi_p);
2844
2845 *handled_ops_p = true;
2846 switch (gimple_code (stmt))
2847 {
2848 case GIMPLE_TRY:
2849 case GIMPLE_BIND:
2850 case GIMPLE_CATCH:
2851 case GIMPLE_EH_FILTER:
2852 case GIMPLE_TRANSACTION:
2853 /* Walk the sub-statements. */
2854 *handled_ops_p = false;
2855 break;
2856 case GIMPLE_CALL:
2857 static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2858 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2859 {
2860 location_t loc = gimple_location (stmt);
2861 gsi_remove (gsi_p, true);
2862 wi->removed_stmt = true;
2863
2864 /* nothrow flag is added by genericize_c_loop to mark fallthrough
2865 statement at the end of some loop's body. Those should be
2866 always diagnosed, either because they indeed don't precede
2867 a case label or default label, or because the next statement
2868 is not within the same iteration statement. */
2869 if ((stmt->subcode & GF_CALL_NOTHROW) != 0)
2870 {
2871 pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2872 "a case label or default label");
2873 break;
2874 }
2875
2876 if (gsi_end_p (*gsi_p))
2877 {
2878 static_cast<location_t *>(wi->info)[0] = BUILTINS_LOCATION;
2879 static_cast<location_t *>(wi->info)[1] = loc;
2880 break;
2881 }
2882
2883 bool found = false;
2884
2885 gimple_stmt_iterator gsi2 = *gsi_p;
2886 stmt = gsi_stmt (gsi2);
2887 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2888 {
2889 /* Go on until the artificial label. */
2890 tree goto_dest = gimple_goto_dest (stmt);
2891 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2892 {
2893 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2894 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2895 == goto_dest)
2896 break;
2897 }
2898
2899 /* Not found? Stop. */
2900 if (gsi_end_p (gsi2))
2901 break;
2902
2903 /* Look one past it. */
2904 gsi_next (&gsi2);
2905 }
2906
2907 /* We're looking for a case label or default label here. */
2908 while (!gsi_end_p (gsi2))
2909 {
2910 stmt = gsi_stmt (gsi2);
2911 if (gimple_code (stmt) == GIMPLE_LABEL)
2912 {
2913 tree label = gimple_label_label (as_a <glabel *> (stmt));
2914 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2915 {
2916 found = true;
2917 break;
2918 }
2919 }
2920 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2921 ;
2922 else if (!is_gimple_debug (stmt))
2923 /* Anything else is not expected. */
2924 break;
2925 gsi_next (&gsi2);
2926 }
2927 if (!found)
2928 pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2929 "a case label or default label");
2930 }
2931 break;
2932 default:
2933 static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2934 break;
2935 }
2936 return NULL_TREE;
2937 }
2938
2939 /* Expand all FALLTHROUGH () calls in SEQ. */
2940
2941 static void
2942 expand_FALLTHROUGH (gimple_seq *seq_p)
2943 {
2944 auto_urlify_attributes sentinel;
2945
2946 struct walk_stmt_info wi;
2947 location_t loc[2];
2948 memset (&wi, 0, sizeof (wi));
2949 loc[0] = UNKNOWN_LOCATION;
2950 loc[1] = UNKNOWN_LOCATION;
2951 wi.info = (void *) &loc[0];
2952 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2953 if (loc[0] != UNKNOWN_LOCATION)
2954 /* We've found [[fallthrough]]; at the end of a switch, which the C++
2955 standard says is ill-formed; see [dcl.attr.fallthrough]. */
2956 pedwarn (loc[1], 0, "attribute %<fallthrough%> not preceding "
2957 "a case label or default label");
2958 }
2959
2960
2961 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2962 branch to. */
2963
2964 static enum gimplify_status
2965 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2966 {
2967 tree switch_expr = *expr_p;
2968 gimple_seq switch_body_seq = NULL;
2969 enum gimplify_status ret;
2970 tree index_type = TREE_TYPE (switch_expr);
2971 if (index_type == NULL_TREE)
2972 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2973
2974 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2975 fb_rvalue);
2976 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2977 return ret;
2978
2979 if (SWITCH_BODY (switch_expr))
2980 {
2981 vec<tree> labels;
2982 vec<tree> saved_labels;
2983 hash_set<tree> *saved_live_switch_vars = NULL;
2984 tree default_case = NULL_TREE;
2985 gswitch *switch_stmt;
2986
2987 /* Save old labels, get new ones from body, then restore the old
2988 labels. Save all the things from the switch body to append after. */
2989 saved_labels = gimplify_ctxp->case_labels;
2990 gimplify_ctxp->case_labels.create (8);
2991
2992 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2993 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2994 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2995 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2996 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2997 else
2998 gimplify_ctxp->live_switch_vars = NULL;
2999
3000 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
3001 gimplify_ctxp->in_switch_expr = true;
3002
3003 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
3004
3005 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
3006 maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
3007 maybe_warn_implicit_fallthrough (switch_body_seq);
3008 /* Only do this for the outermost GIMPLE_SWITCH. */
3009 if (!gimplify_ctxp->in_switch_expr)
3010 expand_FALLTHROUGH (&switch_body_seq);
3011
3012 labels = gimplify_ctxp->case_labels;
3013 gimplify_ctxp->case_labels = saved_labels;
3014
3015 if (gimplify_ctxp->live_switch_vars)
3016 {
3017 gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
3018 delete gimplify_ctxp->live_switch_vars;
3019 }
3020 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
3021
3022 preprocess_case_label_vec_for_gimple (labels, index_type,
3023 &default_case);
3024
3025 bool add_bind = false;
3026 if (!default_case)
3027 {
3028 glabel *new_default;
3029
3030 default_case
3031 = build_case_label (NULL_TREE, NULL_TREE,
3032 create_artificial_label (UNKNOWN_LOCATION));
3033 if (old_in_switch_expr)
3034 {
3035 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
3036 add_bind = true;
3037 }
3038 new_default = gimple_build_label (CASE_LABEL (default_case));
3039 gimplify_seq_add_stmt (&switch_body_seq, new_default);
3040 }
3041 else if (old_in_switch_expr)
3042 {
3043 gimple *last = gimple_seq_last_stmt (switch_body_seq);
3044 if (last && gimple_code (last) == GIMPLE_LABEL)
3045 {
3046 tree label = gimple_label_label (as_a <glabel *> (last));
3047 if (SWITCH_BREAK_LABEL_P (label))
3048 add_bind = true;
3049 }
3050 }
3051
3052 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
3053 default_case, labels);
3054 gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
3055 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
3056 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
3057 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
3058 so that we can easily find the start and end of the switch
3059 statement. */
3060 if (add_bind)
3061 {
3062 gimple_seq bind_body = NULL;
3063 gimplify_seq_add_stmt (&bind_body, switch_stmt);
3064 gimple_seq_add_seq (&bind_body, switch_body_seq);
3065 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
3066 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
3067 gimplify_seq_add_stmt (pre_p, bind);
3068 }
3069 else
3070 {
3071 gimplify_seq_add_stmt (pre_p, switch_stmt);
3072 gimplify_seq_add_seq (pre_p, switch_body_seq);
3073 }
3074 labels.release ();
3075 }
3076 else
3077 gcc_unreachable ();
3078
3079 return GS_ALL_DONE;
3080 }
3081
3082 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
3083
3084 static enum gimplify_status
3085 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
3086 {
3087 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3088 == current_function_decl);
3089
3090 tree label = LABEL_EXPR_LABEL (*expr_p);
3091 glabel *label_stmt = gimple_build_label (label);
3092 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3093 gimplify_seq_add_stmt (pre_p, label_stmt);
3094
3095 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3096 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3097 NOT_TAKEN));
3098 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3099 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3100 TAKEN));
3101
3102 return GS_ALL_DONE;
3103 }
3104
3105 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
3106
3107 static enum gimplify_status
3108 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
3109 {
3110 struct gimplify_ctx *ctxp;
3111 glabel *label_stmt;
3112
3113 /* Invalid programs can play Duff's Device type games with, for example,
3114 #pragma omp parallel. At least in the C front end, we don't
3115 detect such invalid branches until after gimplification, in the
3116 diagnose_omp_blocks pass. */
3117 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
3118 if (ctxp->case_labels.exists ())
3119 break;
3120
3121 tree label = CASE_LABEL (*expr_p);
3122 label_stmt = gimple_build_label (label);
3123 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3124 ctxp->case_labels.safe_push (*expr_p);
3125 gimplify_seq_add_stmt (pre_p, label_stmt);
3126
3127 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3128 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3129 NOT_TAKEN));
3130 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3131 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3132 TAKEN));
3133
3134 return GS_ALL_DONE;
3135 }
3136
3137 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
3138 if necessary. */
3139
3140 tree
3141 build_and_jump (tree *label_p)
3142 {
3143 if (label_p == NULL)
3144 /* If there's nowhere to jump, just fall through. */
3145 return NULL_TREE;
3146
3147 if (*label_p == NULL_TREE)
3148 {
3149 tree label = create_artificial_label (UNKNOWN_LOCATION);
3150 *label_p = label;
3151 }
3152
3153 return build1 (GOTO_EXPR, void_type_node, *label_p);
3154 }
3155
3156 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
3157 This also involves building a label to jump to and communicating it to
3158 gimplify_loop_expr through gimplify_ctxp->exit_label. */
3159
3160 static enum gimplify_status
3161 gimplify_exit_expr (tree *expr_p)
3162 {
3163 tree cond = TREE_OPERAND (*expr_p, 0);
3164 tree expr;
3165
3166 expr = build_and_jump (&gimplify_ctxp->exit_label);
3167 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
3168 *expr_p = expr;
3169
3170 return GS_OK;
3171 }
3172
3173 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
3174 different from its canonical type, wrap the whole thing inside a
3175 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
3176 type.
3177
3178 The canonical type of a COMPONENT_REF is the type of the field being
3179 referenced--unless the field is a bit-field which can be read directly
3180 in a smaller mode, in which case the canonical type is the
3181 sign-appropriate type corresponding to that mode. */
3182
3183 static void
3184 canonicalize_component_ref (tree *expr_p)
3185 {
3186 tree expr = *expr_p;
3187 tree type;
3188
3189 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
3190
3191 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
3192 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
3193 else
3194 type = TREE_TYPE (TREE_OPERAND (expr, 1));
3195
3196 /* One could argue that all the stuff below is not necessary for
3197 the non-bitfield case and declare it a FE error if type
3198 adjustment would be needed. */
3199 if (TREE_TYPE (expr) != type)
3200 {
3201 #ifdef ENABLE_TYPES_CHECKING
3202 tree old_type = TREE_TYPE (expr);
3203 #endif
3204 int type_quals;
3205
3206 /* We need to preserve qualifiers and propagate them from
3207 operand 0. */
3208 type_quals = TYPE_QUALS (type)
3209 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
3210 if (TYPE_QUALS (type) != type_quals)
3211 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
3212
3213 /* Set the type of the COMPONENT_REF to the underlying type. */
3214 TREE_TYPE (expr) = type;
3215
3216 #ifdef ENABLE_TYPES_CHECKING
3217 /* It is now a FE error, if the conversion from the canonical
3218 type to the original expression type is not useless. */
3219 gcc_assert (useless_type_conversion_p (old_type, type));
3220 #endif
3221 }
3222 }
3223
3224 /* If a NOP conversion is changing a pointer to array of foo to a pointer
3225 to foo, embed that change in the ADDR_EXPR by converting
3226 T array[U];
3227 (T *)&array
3228 ==>
3229 &array[L]
3230 where L is the lower bound. For simplicity, only do this for constant
3231 lower bound.
3232 The constraint is that the type of &array[L] is trivially convertible
3233 to T *. */
3234
3235 static void
3236 canonicalize_addr_expr (tree *expr_p)
3237 {
3238 tree expr = *expr_p;
3239 tree addr_expr = TREE_OPERAND (expr, 0);
3240 tree datype, ddatype, pddatype;
3241
3242 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
3243 if (!POINTER_TYPE_P (TREE_TYPE (expr))
3244 || TREE_CODE (addr_expr) != ADDR_EXPR)
3245 return;
3246
3247 /* The addr_expr type should be a pointer to an array. */
3248 datype = TREE_TYPE (TREE_TYPE (addr_expr));
3249 if (TREE_CODE (datype) != ARRAY_TYPE)
3250 return;
3251
3252 /* The pointer to element type shall be trivially convertible to
3253 the expression pointer type. */
3254 ddatype = TREE_TYPE (datype);
3255 pddatype = build_pointer_type (ddatype);
3256 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
3257 pddatype))
3258 return;
3259
3260 /* The lower bound and element sizes must be constant. */
3261 if (!TYPE_SIZE_UNIT (ddatype)
3262 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
3263 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
3264 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
3265 return;
3266
3267 /* All checks succeeded. Build a new node to merge the cast. */
3268 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
3269 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
3270 NULL_TREE, NULL_TREE);
3271 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
3272
3273 /* We can have stripped a required restrict qualifier above. */
3274 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
3275 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
3276 }
3277
3278 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
3279 underneath as appropriate. */
3280
3281 static enum gimplify_status
3282 gimplify_conversion (tree *expr_p)
3283 {
3284 location_t loc = EXPR_LOCATION (*expr_p);
3285 gcc_assert (CONVERT_EXPR_P (*expr_p));
3286
3287 /* Then strip away all but the outermost conversion. */
3288 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
3289
3290 /* And remove the outermost conversion if it's useless. */
3291 if (tree_ssa_useless_type_conversion (*expr_p))
3292 *expr_p = TREE_OPERAND (*expr_p, 0);
3293
3294 /* If we still have a conversion at the toplevel,
3295 then canonicalize some constructs. */
3296 if (CONVERT_EXPR_P (*expr_p))
3297 {
3298 tree sub = TREE_OPERAND (*expr_p, 0);
3299
3300 /* If a NOP conversion is changing the type of a COMPONENT_REF
3301 expression, then canonicalize its type now in order to expose more
3302 redundant conversions. */
3303 if (TREE_CODE (sub) == COMPONENT_REF)
3304 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
3305
3306 /* If a NOP conversion is changing a pointer to array of foo
3307 to a pointer to foo, embed that change in the ADDR_EXPR. */
3308 else if (TREE_CODE (sub) == ADDR_EXPR)
3309 canonicalize_addr_expr (expr_p);
3310 }
3311
3312 /* If we have a conversion to a non-register type force the
3313 use of a VIEW_CONVERT_EXPR instead. */
3314 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
3315 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
3316 TREE_OPERAND (*expr_p, 0));
3317
3318 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
3319 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
3320 TREE_SET_CODE (*expr_p, NOP_EXPR);
3321
3322 return GS_OK;
3323 }
3324
3325 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
3326 DECL_VALUE_EXPR, and it's worth re-examining things. */
3327
3328 static enum gimplify_status
3329 gimplify_var_or_parm_decl (tree *expr_p)
3330 {
3331 tree decl = *expr_p;
3332
3333 /* ??? If this is a local variable, and it has not been seen in any
3334 outer BIND_EXPR, then it's probably the result of a duplicate
3335 declaration, for which we've already issued an error. It would
3336 be really nice if the front end wouldn't leak these at all.
3337 Currently the only known culprit is C++ destructors, as seen
3338 in g++.old-deja/g++.jason/binding.C.
3339 Another possible culpit are size expressions for variably modified
3340 types which are lost in the FE or not gimplified correctly. */
3341 if (VAR_P (decl)
3342 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
3343 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
3344 && decl_function_context (decl) == current_function_decl)
3345 {
3346 gcc_assert (seen_error ());
3347 return GS_ERROR;
3348 }
3349
3350 /* When within an OMP context, notice uses of variables. */
3351 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
3352 return GS_ALL_DONE;
3353
3354 /* If the decl is an alias for another expression, substitute it now. */
3355 if (DECL_HAS_VALUE_EXPR_P (decl))
3356 {
3357 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
3358 return GS_OK;
3359 }
3360
3361 return GS_ALL_DONE;
3362 }
3363
3364 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
3365
3366 static void
3367 recalculate_side_effects (tree t)
3368 {
3369 enum tree_code code = TREE_CODE (t);
3370 int len = TREE_OPERAND_LENGTH (t);
3371 int i;
3372
3373 switch (TREE_CODE_CLASS (code))
3374 {
3375 case tcc_expression:
3376 switch (code)
3377 {
3378 case INIT_EXPR:
3379 case MODIFY_EXPR:
3380 case VA_ARG_EXPR:
3381 case PREDECREMENT_EXPR:
3382 case PREINCREMENT_EXPR:
3383 case POSTDECREMENT_EXPR:
3384 case POSTINCREMENT_EXPR:
3385 /* All of these have side-effects, no matter what their
3386 operands are. */
3387 return;
3388
3389 default:
3390 break;
3391 }
3392 /* Fall through. */
3393
3394 case tcc_comparison: /* a comparison expression */
3395 case tcc_unary: /* a unary arithmetic expression */
3396 case tcc_binary: /* a binary arithmetic expression */
3397 case tcc_reference: /* a reference */
3398 case tcc_vl_exp: /* a function call */
3399 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3400 for (i = 0; i < len; ++i)
3401 {
3402 tree op = TREE_OPERAND (t, i);
3403 if (op && TREE_SIDE_EFFECTS (op))
3404 TREE_SIDE_EFFECTS (t) = 1;
3405 }
3406 break;
3407
3408 case tcc_constant:
3409 /* No side-effects. */
3410 return;
3411
3412 default:
3413 if (code == SSA_NAME)
3414 /* No side-effects. */
3415 return;
3416 gcc_unreachable ();
3417 }
3418 }
3419
3420 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
3421 node *EXPR_P.
3422
3423 compound_lval
3424 : min_lval '[' val ']'
3425 | min_lval '.' ID
3426 | compound_lval '[' val ']'
3427 | compound_lval '.' ID
3428
3429 This is not part of the original SIMPLE definition, which separates
3430 array and member references, but it seems reasonable to handle them
3431 together. Also, this way we don't run into problems with union
3432 aliasing; gcc requires that for accesses through a union to alias, the
3433 union reference must be explicit, which was not always the case when we
3434 were splitting up array and member refs.
3435
3436 PRE_P points to the sequence where side effects that must happen before
3437 *EXPR_P should be stored.
3438
3439 POST_P points to the sequence where side effects that must happen after
3440 *EXPR_P should be stored. */
3441
3442 static enum gimplify_status
3443 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3444 fallback_t fallback)
3445 {
3446 tree *p;
3447 enum gimplify_status ret = GS_ALL_DONE, tret;
3448 int i;
3449 location_t loc = EXPR_LOCATION (*expr_p);
3450 tree expr = *expr_p;
3451
3452 /* Create a stack of the subexpressions so later we can walk them in
3453 order from inner to outer. */
3454 auto_vec<tree, 10> expr_stack;
3455
3456 /* We can handle anything that get_inner_reference can deal with. */
3457 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
3458 {
3459 restart:
3460 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
3461 if (TREE_CODE (*p) == INDIRECT_REF)
3462 *p = fold_indirect_ref_loc (loc, *p);
3463
3464 if (handled_component_p (*p))
3465 ;
3466 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
3467 additional COMPONENT_REFs. */
3468 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
3469 && gimplify_var_or_parm_decl (p) == GS_OK)
3470 goto restart;
3471 else
3472 break;
3473
3474 expr_stack.safe_push (*p);
3475 }
3476
3477 gcc_assert (expr_stack.length ());
3478
3479 /* Now EXPR_STACK is a stack of pointers to all the refs we've
3480 walked through and P points to the innermost expression.
3481
3482 Java requires that we elaborated nodes in source order. That
3483 means we must gimplify the inner expression followed by each of
3484 the indices, in order. But we can't gimplify the inner
3485 expression until we deal with any variable bounds, sizes, or
3486 positions in order to deal with PLACEHOLDER_EXPRs.
3487
3488 The base expression may contain a statement expression that
3489 has declarations used in size expressions, so has to be
3490 gimplified before gimplifying the size expressions.
3491
3492 So we do this in three steps. First we deal with variable
3493 bounds, sizes, and positions, then we gimplify the base and
3494 ensure it is memory if needed, then we deal with the annotations
3495 for any variables in the components and any indices, from left
3496 to right. */
3497
3498 bool need_non_reg = false;
3499 for (i = expr_stack.length () - 1; i >= 0; i--)
3500 {
3501 tree t = expr_stack[i];
3502
3503 if (error_operand_p (TREE_OPERAND (t, 0)))
3504 return GS_ERROR;
3505
3506 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3507 {
3508 /* Deal with the low bound and element type size and put them into
3509 the ARRAY_REF. If these values are set, they have already been
3510 gimplified. */
3511 if (TREE_OPERAND (t, 2) == NULL_TREE)
3512 {
3513 tree low = unshare_expr (array_ref_low_bound (t));
3514 if (!is_gimple_min_invariant (low))
3515 {
3516 TREE_OPERAND (t, 2) = low;
3517 }
3518 }
3519
3520 if (TREE_OPERAND (t, 3) == NULL_TREE)
3521 {
3522 tree elmt_size = array_ref_element_size (t);
3523 if (!is_gimple_min_invariant (elmt_size))
3524 {
3525 elmt_size = unshare_expr (elmt_size);
3526 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
3527 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
3528
3529 /* Divide the element size by the alignment of the element
3530 type (above). */
3531 elmt_size = size_binop_loc (loc, EXACT_DIV_EXPR,
3532 elmt_size, factor);
3533
3534 TREE_OPERAND (t, 3) = elmt_size;
3535 }
3536 }
3537 need_non_reg = true;
3538 }
3539 else if (TREE_CODE (t) == COMPONENT_REF)
3540 {
3541 /* Set the field offset into T and gimplify it. */
3542 if (TREE_OPERAND (t, 2) == NULL_TREE)
3543 {
3544 tree offset = component_ref_field_offset (t);
3545 if (!is_gimple_min_invariant (offset))
3546 {
3547 offset = unshare_expr (offset);
3548 tree field = TREE_OPERAND (t, 1);
3549 tree factor
3550 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
3551
3552 /* Divide the offset by its alignment. */
3553 offset = size_binop_loc (loc, EXACT_DIV_EXPR,
3554 offset, factor);
3555
3556 TREE_OPERAND (t, 2) = offset;
3557 }
3558 }
3559 need_non_reg = true;
3560 }
3561 else if (!is_gimple_reg_type (TREE_TYPE (t)))
3562 /* When the result of an operation, in particular a VIEW_CONVERT_EXPR
3563 is a non-register type then require the base object to be a
3564 non-register as well. */
3565 need_non_reg = true;
3566 }
3567
3568 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
3569 so as to match the min_lval predicate. Failure to do so may result
3570 in the creation of large aggregate temporaries. */
3571 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3572 fallback | fb_lvalue);
3573 ret = MIN (ret, tret);
3574 if (ret == GS_ERROR)
3575 return GS_ERROR;
3576
3577 /* Step 2a: if we have component references we do not support on
3578 registers then make sure the base isn't a register. Of course
3579 we can only do so if an rvalue is OK. */
3580 if (need_non_reg && (fallback & fb_rvalue))
3581 prepare_gimple_addressable (p, pre_p);
3582
3583
3584 /* Step 3: gimplify size expressions and the indices and operands of
3585 ARRAY_REF. During this loop we also remove any useless conversions.
3586 If we operate on a register also make sure to properly gimplify
3587 to individual operations. */
3588
3589 bool reg_operations = is_gimple_reg (*p);
3590 for (; expr_stack.length () > 0; )
3591 {
3592 tree t = expr_stack.pop ();
3593
3594 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3595 {
3596 gcc_assert (!reg_operations);
3597
3598 /* Gimplify the low bound and element type size. */
3599 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3600 is_gimple_reg, fb_rvalue);
3601 ret = MIN (ret, tret);
3602
3603 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
3604 is_gimple_reg, fb_rvalue);
3605 ret = MIN (ret, tret);
3606
3607 /* Gimplify the dimension. */
3608 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3609 is_gimple_val, fb_rvalue);
3610 ret = MIN (ret, tret);
3611 }
3612 else if (TREE_CODE (t) == COMPONENT_REF)
3613 {
3614 gcc_assert (!reg_operations);
3615
3616 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3617 is_gimple_reg, fb_rvalue);
3618 ret = MIN (ret, tret);
3619 }
3620 else if (reg_operations)
3621 {
3622 tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
3623 is_gimple_val, fb_rvalue);
3624 ret = MIN (ret, tret);
3625 }
3626
3627 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
3628
3629 /* The innermost expression P may have originally had
3630 TREE_SIDE_EFFECTS set which would have caused all the outer
3631 expressions in *EXPR_P leading to P to also have had
3632 TREE_SIDE_EFFECTS set. */
3633 recalculate_side_effects (t);
3634 }
3635
3636 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3637 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3638 {
3639 canonicalize_component_ref (expr_p);
3640 }
3641
3642 expr_stack.release ();
3643
3644 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3645
3646 return ret;
3647 }
3648
3649 /* Gimplify the self modifying expression pointed to by EXPR_P
3650 (++, --, +=, -=).
3651
3652 PRE_P points to the list where side effects that must happen before
3653 *EXPR_P should be stored.
3654
3655 POST_P points to the list where side effects that must happen after
3656 *EXPR_P should be stored.
3657
3658 WANT_VALUE is nonzero iff we want to use the value of this expression
3659 in another expression.
3660
3661 ARITH_TYPE is the type the computation should be performed in. */
3662
3663 enum gimplify_status
3664 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3665 bool want_value, tree arith_type)
3666 {
3667 enum tree_code code;
3668 tree lhs, lvalue, rhs, t1;
3669 gimple_seq post = NULL, *orig_post_p = post_p;
3670 bool postfix;
3671 enum tree_code arith_code;
3672 enum gimplify_status ret;
3673 location_t loc = EXPR_LOCATION (*expr_p);
3674
3675 code = TREE_CODE (*expr_p);
3676
3677 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3678 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3679
3680 /* Prefix or postfix? */
3681 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3682 /* Faster to treat as prefix if result is not used. */
3683 postfix = want_value;
3684 else
3685 postfix = false;
3686
3687 /* For postfix, make sure the inner expression's post side effects
3688 are executed after side effects from this expression. */
3689 if (postfix)
3690 post_p = &post;
3691
3692 /* Add or subtract? */
3693 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3694 arith_code = PLUS_EXPR;
3695 else
3696 arith_code = MINUS_EXPR;
3697
3698 /* Gimplify the LHS into a GIMPLE lvalue. */
3699 lvalue = TREE_OPERAND (*expr_p, 0);
3700 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3701 if (ret == GS_ERROR)
3702 return ret;
3703
3704 /* Extract the operands to the arithmetic operation. */
3705 lhs = lvalue;
3706 rhs = TREE_OPERAND (*expr_p, 1);
3707
3708 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3709 that as the result value and in the postqueue operation. */
3710 if (postfix)
3711 {
3712 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3713 if (ret == GS_ERROR)
3714 return ret;
3715
3716 lhs = get_initialized_tmp_var (lhs, pre_p);
3717 }
3718
3719 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3720 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3721 {
3722 rhs = convert_to_ptrofftype_loc (loc, rhs);
3723 if (arith_code == MINUS_EXPR)
3724 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3725 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3726 }
3727 else
3728 t1 = fold_convert (TREE_TYPE (*expr_p),
3729 fold_build2 (arith_code, arith_type,
3730 fold_convert (arith_type, lhs),
3731 fold_convert (arith_type, rhs)));
3732
3733 if (postfix)
3734 {
3735 gimplify_assign (lvalue, t1, pre_p);
3736 gimplify_seq_add_seq (orig_post_p, post);
3737 *expr_p = lhs;
3738 return GS_ALL_DONE;
3739 }
3740 else
3741 {
3742 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3743 return GS_OK;
3744 }
3745 }
3746
3747 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3748
3749 static void
3750 maybe_with_size_expr (tree *expr_p)
3751 {
3752 tree expr = *expr_p;
3753 tree type = TREE_TYPE (expr);
3754 tree size;
3755
3756 /* If we've already wrapped this or the type is error_mark_node, we can't do
3757 anything. */
3758 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3759 || type == error_mark_node)
3760 return;
3761
3762 /* If the size isn't known or is a constant, we have nothing to do. */
3763 size = TYPE_SIZE_UNIT (type);
3764 if (!size || poly_int_tree_p (size))
3765 return;
3766
3767 /* Otherwise, make a WITH_SIZE_EXPR. */
3768 size = unshare_expr (size);
3769 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3770 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3771 }
3772
3773 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3774 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3775 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3776 gimplified to an SSA name. */
3777
3778 enum gimplify_status
3779 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3780 bool allow_ssa)
3781 {
3782 bool (*test) (tree);
3783 fallback_t fb;
3784
3785 /* In general, we allow lvalues for function arguments to avoid
3786 extra overhead of copying large aggregates out of even larger
3787 aggregates into temporaries only to copy the temporaries to
3788 the argument list. Make optimizers happy by pulling out to
3789 temporaries those types that fit in registers. */
3790 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3791 test = is_gimple_val, fb = fb_rvalue;
3792 else
3793 {
3794 test = is_gimple_lvalue, fb = fb_either;
3795 /* Also strip a TARGET_EXPR that would force an extra copy. */
3796 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3797 {
3798 tree init = TARGET_EXPR_INITIAL (*arg_p);
3799 if (init
3800 && !VOID_TYPE_P (TREE_TYPE (init))
3801 /* Currently, due to c++/116015, it is not desirable to
3802 strip a TARGET_EXPR whose initializer is a {}. The
3803 problem is that if we do elide it, we also have to
3804 replace all the occurrences of the slot temporary in the
3805 initializer with the temporary created for the argument.
3806 But we do not have that temporary yet so the replacement
3807 would be quite awkward and it might be needed to resort
3808 back to a PLACEHOLDER_EXPR. Note that stripping the
3809 TARGET_EXPR wouldn't help anyway, as gimplify_expr would
3810 just allocate a temporary to store the CONSTRUCTOR into.
3811 (FIXME PR116375.)
3812
3813 See convert_for_arg_passing for the C++ code that marks
3814 the TARGET_EXPR as eliding or not. */
3815 && TREE_CODE (init) != CONSTRUCTOR)
3816 *arg_p = init;
3817 }
3818 }
3819
3820 /* If this is a variable sized type, we must remember the size. */
3821 maybe_with_size_expr (arg_p);
3822
3823 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3824 /* Make sure arguments have the same location as the function call
3825 itself. */
3826 protected_set_expr_location (*arg_p, call_location);
3827
3828 /* There is a sequence point before a function call. Side effects in
3829 the argument list must occur before the actual call. So, when
3830 gimplifying arguments, force gimplify_expr to use an internal
3831 post queue which is then appended to the end of PRE_P. */
3832 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3833 }
3834
3835 /* Don't fold inside offloading or taskreg regions: it can break code by
3836 adding decl references that weren't in the source. We'll do it during
3837 omplower pass instead. */
3838
3839 static bool
3840 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3841 {
3842 struct gimplify_omp_ctx *ctx;
3843 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3844 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3845 return false;
3846 else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3847 return false;
3848 /* Delay folding of builtins until the IL is in consistent state
3849 so the diagnostic machinery can do a better job. */
3850 if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3851 return false;
3852 return fold_stmt (gsi);
3853 }
3854
3855 static tree
3856 expand_late_variant_directive (vec<struct omp_variant> all_candidates,
3857 tree construct_context);
3858
3859
3860 /* Helper function for calls to omp_dynamic_cond: find the current
3861 enclosing block in the gimplification context. */
3862 static tree
3863 find_supercontext (void)
3864 {
3865 vec<gbind *>stack = gimple_bind_expr_stack ();
3866 for (int i = stack.length () - 1; i >= 0; i++)
3867 {
3868 gbind *b = stack[i];
3869 if (b->block)
3870 return b->block;
3871 }
3872 return NULL_TREE;
3873 }
3874
3875 /* OpenMP: Handle the append_args and adjust_args clauses of
3876 declare_variant for EXPR, which is a CALL_EXPR whose CALL_EXPR_FN
3877 is the variant, within a dispatch construct with clauses DISPATCH_CLAUSES.
3878 WANT_VALUE and POINTERIZE are as for expand_variant_call_expr.
3879
3880 'append_args' causes interop objects are added after the last regular
3881 (nonhidden, nonvariadic) arguments of the variant function.
3882 'adjust_args' with need_device_{addr,ptr} converts the pointer target of
3883 a pointer from a host to a device address. This uses either the default
3884 device or the passed device number, which then sets the default device
3885 address. */
3886 static tree
3887 modify_call_for_omp_dispatch (tree expr, tree dispatch_clauses,
3888 bool want_value, bool pointerize)
3889 {
3890 location_t loc = EXPR_LOCATION (expr);
3891 tree fndecl = get_callee_fndecl (expr);
3892
3893 /* Skip processing if we don't get the expected call form. */
3894 if (!fndecl)
3895 return expr;
3896
3897 tree init_code = NULL_TREE;
3898 tree cleanup = NULL_TREE;
3899 tree clobbers = NULL_TREE;
3900 int nargs = call_expr_nargs (expr);
3901 tree dispatch_device_num = NULL_TREE;
3902 tree dispatch_interop = NULL_TREE;
3903 tree dispatch_append_args = NULL_TREE;
3904 int nfirst_args = 0;
3905 tree dispatch_adjust_args_list
3906 = lookup_attribute ("omp declare variant variant args",
3907 DECL_ATTRIBUTES (fndecl));
3908
3909 if (dispatch_adjust_args_list)
3910 {
3911 dispatch_adjust_args_list = TREE_VALUE (dispatch_adjust_args_list);
3912 dispatch_append_args = TREE_CHAIN (dispatch_adjust_args_list);
3913 if (TREE_PURPOSE (dispatch_adjust_args_list) == NULL_TREE
3914 && TREE_VALUE (dispatch_adjust_args_list) == NULL_TREE)
3915 dispatch_adjust_args_list = NULL_TREE;
3916 }
3917 if (dispatch_append_args)
3918 {
3919 nfirst_args = tree_to_shwi (TREE_PURPOSE (dispatch_append_args));
3920 dispatch_append_args = TREE_VALUE (dispatch_append_args);
3921 }
3922 dispatch_device_num = omp_find_clause (dispatch_clauses, OMP_CLAUSE_DEVICE);
3923 if (dispatch_device_num)
3924 dispatch_device_num = OMP_CLAUSE_DEVICE_ID (dispatch_device_num);
3925 dispatch_interop = omp_find_clause (dispatch_clauses, OMP_CLAUSE_INTEROP);
3926 int nappend = 0, ninterop = 0;
3927 for (tree t = dispatch_append_args; t; t = TREE_CHAIN (t))
3928 nappend++;
3929
3930 /* FIXME: error checking should be taken out of this function and
3931 handled before any attempt at filtering or resolution happens.
3932 Otherwise whether or not diagnostics appear is determined by
3933 GCC internals, how good the front ends are at constant-folding,
3934 the split between early/late resolution, etc instead of the code
3935 as written by the user. */
3936 if (dispatch_interop)
3937 for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
3938 if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3939 ninterop++;
3940 if (dispatch_interop && !dispatch_device_num)
3941 {
3942 gcc_checking_assert (ninterop > 1);
3943 error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3944 "the %<device%> clause must be present if the %<interop%> "
3945 "clause has more than one list item");
3946 }
3947 if (nappend < ninterop)
3948 {
3949 error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3950 "number of list items in %<interop%> clause (%d) "
3951 "exceeds the number of %<append_args%> items (%d) for "
3952 "%<declare variant%> candidate %qD", ninterop, nappend, fndecl);
3953 inform (dispatch_append_args
3954 ? EXPR_LOCATION (TREE_PURPOSE (dispatch_append_args))
3955 : DECL_SOURCE_LOCATION (fndecl),
3956 "%<declare variant%> candidate %qD declared here", fndecl);
3957 ninterop = nappend;
3958 }
3959 if (dispatch_append_args)
3960 {
3961 tree *buffer = XALLOCAVEC (tree, nargs + nappend);
3962 tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3963 /* Copy the first arguments; insert then the interop objects,
3964 and then copy the rest (nargs - nfirst_args) args. */
3965 int i;
3966 for (i = 0; i < nfirst_args; i++)
3967 {
3968 arg = TREE_CHAIN (arg);
3969 buffer[i] = CALL_EXPR_ARG (expr, i);
3970 }
3971 int j = ninterop;
3972 for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
3973 if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3974 buffer[i + --j] = OMP_CLAUSE_DECL (t);
3975 gcc_checking_assert (j == 0);
3976
3977 /* Do we need to create additional interop objects? */
3978 if (ninterop < nappend)
3979 {
3980 if (dispatch_device_num == NULL_TREE)
3981 /* Not remapping device number. */
3982 dispatch_device_num = build_int_cst (integer_type_node,
3983 GOMP_DEVICE_DEFAULT_OMP_61);
3984 int nnew = nappend - ninterop;
3985 tree nobjs = build_int_cst (integer_type_node, nnew);
3986 tree a, t;
3987
3988 /* Skip to the append_args clause for the first constructed
3989 interop argument. */
3990 tree apparg = dispatch_append_args;
3991 for (j = 0; j < ninterop; j++)
3992 apparg = TREE_CHAIN (apparg);
3993
3994 /* omp_interop_t *objs[n]; */
3995 tree objtype = build_pointer_type (pointer_sized_int_node);
3996 t = build_array_type_nelts (objtype, nnew);
3997 tree objs = create_tmp_var (t, "interopobjs");
3998
3999 /* int target_tgtsync[n]; */
4000 t = build_array_type_nelts (integer_type_node, nnew);
4001 tree target_tgtsync = create_tmp_var (t, "tgt_tgtsync");
4002
4003 /* Scan first to determine if we need a prefer_type array. */
4004 tree prefer_type = NULL_TREE;
4005 tree prefer_type_type = NULL_TREE;
4006 for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4007 if (TREE_VALUE (a) != NULL_TREE)
4008 {
4009 /* const char *prefer_type[n]; */
4010 t = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4011 prefer_type_type = build_pointer_type (t);
4012 t = build_array_type_nelts (prefer_type_type, nnew);
4013 prefer_type = create_tmp_var (t, "pref_type");
4014 break;
4015 }
4016
4017 /* Initialize the arrays, generating temp vars and clobbers for
4018 the interobject objects. (The constructed array holding the
4019 pointers to these objects shouldn't need clobbering as there's
4020 no reason for GOMP_interop to modify its contents.) */
4021 for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4022 {
4023 /* The allocated temporaries for the interop objects
4024 have type omp_interop_t, which is an integer type that
4025 can encode a pointer. */
4026 tree objvar = create_tmp_var (pointer_sized_int_node, "interop");
4027 buffer[i + j] = objvar;
4028 TREE_ADDRESSABLE (objvar) = 1;
4029 /* Generate a clobber for the temporary for when we're done
4030 with it. */
4031 tree c = build_clobber (pointer_sized_int_node,
4032 CLOBBER_OBJECT_END);
4033 c = build2 (MODIFY_EXPR, pointer_sized_int_node, objvar, c);
4034 if (clobbers)
4035 clobbers = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4036 c, clobbers);
4037 else
4038 clobbers = c;
4039
4040 /* objs[offset] = &objvar; */
4041 tree offset = build_int_cst (integer_type_node, j - ninterop);
4042 tree init = build4 (ARRAY_REF, objtype, objs, offset,
4043 NULL_TREE, NULL_TREE);
4044 init = build2 (MODIFY_EXPR, objtype, init,
4045 build_fold_addr_expr (objvar));
4046 if (init_code)
4047 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4048 init_code, init);
4049 else
4050 init_code = init;
4051
4052 /* target_tgtsync[offset] = tgt;
4053 (Don't blame me, I didn't design the encoding of this
4054 info into the dispatch interop clause data structure,
4055 but the runtime wants a bit mask.) */
4056 tree tree_tgt = TREE_OPERAND (TREE_PURPOSE (a), 0);
4057 int tgt = 0;
4058 if (TREE_PURPOSE (tree_tgt) == boolean_true_node)
4059 tgt |= GOMP_INTEROP_TARGET;
4060 if (TREE_VALUE (tree_tgt) == boolean_true_node)
4061 tgt |= GOMP_INTEROP_TARGETSYNC;
4062 init = build4 (ARRAY_REF, integer_type_node,
4063 target_tgtsync, offset, NULL_TREE, NULL_TREE);
4064 init = build2 (MODIFY_EXPR, integer_type_node, init,
4065 build_int_cst (integer_type_node, tgt));
4066 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4067 init_code, init);
4068
4069 if (prefer_type)
4070 {
4071 tree pref = TREE_VALUE (a);
4072 if (pref == NULL_TREE)
4073 pref = null_pointer_node;
4074 else
4075 pref = build_fold_addr_expr (pref);
4076 init = build4 (ARRAY_REF, prefer_type_type, prefer_type,
4077 offset, NULL_TREE, NULL_TREE);
4078 init = build2 (MODIFY_EXPR, prefer_type_type, init,
4079 pref);
4080 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4081 init_code, init);
4082 }
4083 }
4084
4085 objs = build_fold_addr_expr (objs);
4086 target_tgtsync = build_fold_addr_expr (target_tgtsync);
4087 prefer_type = prefer_type ? build_fold_addr_expr (prefer_type)
4088 : null_pointer_node;
4089 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
4090 tree create
4091 = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4092 nobjs, objs, target_tgtsync, prefer_type,
4093 integer_zero_node, null_pointer_node,
4094 integer_zero_node, null_pointer_node,
4095 integer_zero_node, null_pointer_node);
4096 if (init_code)
4097 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (create),
4098 init_code, create);
4099 else
4100 init_code = create;
4101
4102 cleanup
4103 = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4104 integer_zero_node, null_pointer_node,
4105 null_pointer_node, null_pointer_node,
4106 integer_zero_node, null_pointer_node,
4107 nobjs, objs,
4108 integer_zero_node, null_pointer_node);
4109 if (clobbers)
4110 cleanup = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4111 cleanup, clobbers);
4112 }
4113
4114 for (j = 0; j < nappend; j++)
4115 {
4116 /* Fortran permits by-reference or by-value for the dummy arg
4117 and by-value, by-reference, ptr by-reference as actual
4118 argument. Handle this. */
4119 tree obj = buffer[i + j]; // interop object
4120 tree a2 = TREE_VALUE (arg); // parameter type
4121 if (POINTER_TYPE_P (TREE_TYPE (obj))
4122 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (obj))))
4123 {
4124 tree t = TREE_TYPE (TREE_TYPE (obj));
4125 gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (t)));
4126 obj = fold_build1 (INDIRECT_REF, t, obj);
4127 }
4128 if (POINTER_TYPE_P (TREE_TYPE (obj))
4129 && INTEGRAL_TYPE_P (a2))
4130 {
4131 tree t = TREE_TYPE (TREE_TYPE (obj));
4132 gcc_checking_assert (INTEGRAL_TYPE_P (t));
4133 obj = fold_build1 (INDIRECT_REF, t, obj);
4134 }
4135 else if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
4136 && POINTER_TYPE_P (a2))
4137 {
4138 gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4139 obj = build_fold_addr_expr (obj);
4140 }
4141 else if (!INTEGRAL_TYPE_P (a2)
4142 || !INTEGRAL_TYPE_P (TREE_TYPE (obj)))
4143 {
4144 tree t = TREE_TYPE (obj);
4145 gcc_checking_assert (POINTER_TYPE_P (t)
4146 && POINTER_TYPE_P (a2)
4147 && INTEGRAL_TYPE_P (TREE_TYPE (t))
4148 && INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4149 }
4150 buffer[i + j] = obj;
4151 arg = TREE_CHAIN (arg);
4152 }
4153 i += nappend;
4154 for (j = nfirst_args; j < nargs; j++)
4155 buffer[i++] = CALL_EXPR_ARG (expr, j);
4156 nargs += nappend;
4157 tree call = expr;
4158 expr = build_call_array_loc (EXPR_LOCATION (expr), TREE_TYPE (call),
4159 CALL_EXPR_FN (call), nargs, buffer);
4160
4161 /* Copy all CALL_EXPR flags. */
4162 CALL_EXPR_STATIC_CHAIN (expr) = CALL_EXPR_STATIC_CHAIN (call);
4163 CALL_EXPR_TAILCALL (expr) = CALL_EXPR_TAILCALL (call);
4164 CALL_EXPR_RETURN_SLOT_OPT (expr)
4165 = CALL_EXPR_RETURN_SLOT_OPT (call);
4166 CALL_FROM_THUNK_P (expr) = CALL_FROM_THUNK_P (call);
4167 SET_EXPR_LOCATION (expr, EXPR_LOCATION (call));
4168 CALL_EXPR_VA_ARG_PACK (expr) = CALL_EXPR_VA_ARG_PACK (call);
4169 }
4170
4171 /* Nothing to do for adjust_args? */
4172 if (!dispatch_adjust_args_list || !TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
4173 goto add_cleanup;
4174
4175 /* Handle adjust_args. */
4176 for (int i = 0; i < nargs; i++)
4177 {
4178 tree *arg_p = &CALL_EXPR_ARG (expr, i);
4179
4180 /* Nothing to do if arg is constant null pointer. */
4181 if (integer_zerop (*arg_p))
4182 continue;
4183
4184 bool need_device_ptr = false;
4185 bool need_device_addr = false;
4186 for (int need_addr = 0; need_addr <= 1; need_addr++)
4187 for (tree arg = (need_addr
4188 ? TREE_VALUE (dispatch_adjust_args_list)
4189 : TREE_PURPOSE (dispatch_adjust_args_list));
4190 arg != NULL; arg = TREE_CHAIN (arg))
4191 {
4192 if (TREE_VALUE (arg)
4193 && TREE_CODE (TREE_VALUE (arg)) == INTEGER_CST
4194 && wi::eq_p (i, wi::to_wide (TREE_VALUE (arg))))
4195 {
4196 if (need_addr)
4197 need_device_addr = true;
4198 else
4199 need_device_ptr = true;
4200 break;
4201 }
4202 }
4203
4204 if (need_device_ptr || need_device_addr)
4205 {
4206 bool is_device_ptr = false;
4207 bool has_device_addr = false;
4208
4209 for (tree c = dispatch_clauses; c; c = TREE_CHAIN (c))
4210 {
4211 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
4212 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
4213 {
4214 tree decl1 = DECL_NAME (OMP_CLAUSE_DECL (c));
4215 tree decl2 = tree_strip_nop_conversions (*arg_p);
4216 if (TREE_CODE (decl2) == ADDR_EXPR)
4217 decl2 = TREE_OPERAND (decl2, 0);
4218 if (VAR_P (decl2) || TREE_CODE (decl2) == PARM_DECL)
4219 {
4220 decl2 = DECL_NAME (decl2);
4221 if (decl1 == decl2
4222 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
4223 {
4224 if (need_device_addr)
4225 warning_at (OMP_CLAUSE_LOCATION (c),
4226 OPT_Wopenmp,
4227 "%<is_device_ptr%> for %qD does"
4228 " not imply %<has_device_addr%> "
4229 "required for %<need_device_addr%>",
4230 OMP_CLAUSE_DECL (c));
4231 is_device_ptr = true;
4232 break;
4233 }
4234 else if (decl1 == decl2)
4235 {
4236 if (need_device_ptr)
4237 warning_at (OMP_CLAUSE_LOCATION (c),
4238 OPT_Wopenmp,
4239 "%<has_device_addr%> for %qD does"
4240 " not imply %<is_device_ptr%> "
4241 "required for %<need_device_ptr%>",
4242 OMP_CLAUSE_DECL (c));
4243 has_device_addr = true;
4244 break;
4245 }
4246 }
4247 }
4248 }
4249
4250 if ((need_device_ptr && !is_device_ptr)
4251 || (need_device_addr && !has_device_addr))
4252 {
4253 if (dispatch_device_num == NULL_TREE)
4254 {
4255 // device_num = omp_get_default_device ()
4256 tree fn
4257 = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
4258 tree call = build_call_expr (fn, 0);
4259 dispatch_device_num = create_tmp_var_raw (TREE_TYPE (call));
4260 tree init
4261 = build4 (TARGET_EXPR, TREE_TYPE (call),
4262 dispatch_device_num, call, NULL_TREE, NULL_TREE);
4263 if (init_code)
4264 init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4265 init_code, init);
4266 else
4267 init_code = init;
4268 }
4269
4270 // We want to emit the following statement:
4271 // mapped_arg = omp_get_mapped_ptr (arg,
4272 // device_num)
4273 // but arg has to be the actual pointer, not a
4274 // reference or a conversion expression.
4275 tree actual_ptr
4276 = ((TREE_CODE (*arg_p) == ADDR_EXPR)
4277 ? TREE_OPERAND (*arg_p, 0)
4278 : *arg_p);
4279 if (TREE_CODE (actual_ptr) == NOP_EXPR
4280 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (actual_ptr, 0)))
4281 == REFERENCE_TYPE))
4282 {
4283 actual_ptr = TREE_OPERAND (actual_ptr, 0);
4284 actual_ptr = build1 (INDIRECT_REF,
4285 TREE_TYPE (actual_ptr),
4286 actual_ptr);
4287 }
4288 tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_MAPPED_PTR);
4289 tree mapped_arg = build_call_expr_loc (loc, fn, 2, actual_ptr,
4290 dispatch_device_num);
4291
4292 if (TREE_CODE (*arg_p) == ADDR_EXPR
4293 || (TREE_CODE (TREE_TYPE (actual_ptr)) == REFERENCE_TYPE))
4294 mapped_arg = build_fold_addr_expr (mapped_arg);
4295 else if (TREE_CODE (*arg_p) == NOP_EXPR)
4296 mapped_arg = build1 (NOP_EXPR, TREE_TYPE (*arg_p),
4297 mapped_arg);
4298 *arg_p = mapped_arg;
4299 }
4300 }
4301 }
4302
4303 add_cleanup:
4304 if (cleanup)
4305 {
4306 tree result = NULL_TREE;
4307 if (want_value && pointerize)
4308 {
4309 tree tmp = create_tmp_var (build_pointer_type (TREE_TYPE (expr)),
4310 "cleanuptmp");
4311 result = build_simple_mem_ref (tmp);
4312 expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp,
4313 build_fold_addr_expr (expr));
4314 }
4315 else if (want_value)
4316 {
4317 tree tmp = create_tmp_var (TREE_TYPE (expr), "cleanuptmp");
4318 result = tmp;
4319 expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp, expr);
4320 }
4321 if (init_code)
4322 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4323 expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, cleanup);
4324
4325 if (result)
4326 expr = build2 (COMPOUND_EXPR, TREE_TYPE (result), expr, result);
4327 }
4328 else if (init_code)
4329 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4330
4331 return expr;
4332 }
4333
4334 /* Helper function for gimplify_call_expr: handle "declare variant"
4335 resolution and expansion of the CALL_EXPR EXPR. WANT_VALUE is true
4336 if the result value of the call is needed; POINTERIZE is true if it
4337 also needs to be pointerized. If OMP_DISPATCH_P is true, apply
4338 associated transformations using DISPATCH_CLAUSES.
4339 This function may return either the original call or some other
4340 expression such as a conditional to select one of multiple calls.
4341
4342 FIXME: this function is written to be independent of gimplifier internals
4343 so that it could be moved to omp-general.cc and invoked from the
4344 front ends instead, per PR115076. */
4345
4346 static tree
4347 expand_variant_call_expr (tree expr, bool want_value, bool pointerize,
4348 bool omp_dispatch_p, tree dispatch_clauses)
4349 {
4350 /* If we've already processed this call, stop now. This can happen
4351 if the variant call resolves to the original function, or to
4352 a dynamic conditional that includes the default call to the original
4353 function. */
4354 gcc_assert (omp_resolved_variant_calls != NULL);
4355 if (omp_resolved_variant_calls->contains (expr))
4356 return expr;
4357
4358 tree fndecl = get_callee_fndecl (expr);
4359 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
4360 location_t loc = EXPR_LOCATION (expr);
4361 tree construct_context = omp_get_construct_context ();
4362 vec<struct omp_variant> all_candidates
4363 = omp_declare_variant_candidates (fndecl, construct_context);
4364 gcc_assert (!all_candidates.is_empty ());
4365 vec<struct omp_variant> candidates
4366 = omp_get_dynamic_candidates (all_candidates, construct_context);
4367
4368 /* If the variant call could be resolved now, build a nest of COND_EXPRs
4369 if there are dynamic candidates, and/or a new CALL_EXPR for each
4370 candidate call. */
4371 if (!candidates.is_empty ())
4372 {
4373 int n = candidates.length ();
4374 tree tail = NULL_TREE;
4375
4376 for (int i = n - 1; i >= 0; i--)
4377 {
4378 if (tail)
4379 gcc_assert (candidates[i].dynamic_selector);
4380 else
4381 gcc_assert (!candidates[i].dynamic_selector);
4382 if (candidates[i].alternative == fndecl)
4383 {
4384 /* We should only get the original function back as the
4385 default. */
4386 gcc_assert (!tail);
4387 omp_resolved_variant_calls->add (expr);
4388 tail = expr;
4389 }
4390 else
4391 {
4392 /* For the final static selector, we can re-use the old
4393 CALL_EXPR and just replace the function, unless it may
4394 need dispatch argument modification. Otherwise,
4395 make a copy of it. */
4396 tree thiscall = (tail || omp_dispatch_p
4397 ? unshare_expr (expr) : expr);
4398 CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
4399 candidates[i].alternative);
4400 if (omp_dispatch_p)
4401 thiscall = modify_call_for_omp_dispatch (thiscall,
4402 dispatch_clauses,
4403 want_value,
4404 pointerize);
4405 if (!tail)
4406 tail = thiscall;
4407 else
4408 tail = build3 (COND_EXPR, TREE_TYPE (expr),
4409 omp_dynamic_cond (candidates[i].selector,
4410 find_supercontext ()),
4411 thiscall, tail);
4412 }
4413 }
4414 return tail;
4415 }
4416
4417 /* If we couldn't resolve the variant call now, expand it into a loop using
4418 a switch and OMP_NEXT_VARIANT for dispatch. The ompdevlow pass will
4419 handle OMP_NEXT_VARIANT expansion. */
4420 else
4421 {
4422 /* If we need a usable return value, we need a temporary
4423 and an assignment in each alternative. This logic was borrowed
4424 from gimplify_cond_expr. */
4425 tree type = TREE_TYPE (expr);
4426 tree tmp = NULL_TREE, result = NULL_TREE;
4427
4428 if (want_value)
4429 {
4430 if (pointerize)
4431 {
4432 type = build_pointer_type (type);
4433 tmp = create_tmp_var (type, "iftmp");
4434 result = build_simple_mem_ref_loc (loc, tmp);
4435 }
4436 else
4437 {
4438 tmp = create_tmp_var (type, "iftmp");
4439 result = tmp;
4440 }
4441 }
4442
4443 /* Preprocess the all_candidates array so that the alternative field of
4444 each element holds the actual function call expression and possible
4445 assignment, instead of just the decl for the variant function. */
4446 for (unsigned int i = 0; i < all_candidates.length (); i++)
4447 {
4448 tree decl = all_candidates[i].alternative;
4449 tree thiscall;
4450
4451 /* We need to turn the decl from the candidate into a function
4452 call and possible assignment, and stuff that in
4453 the directive seq of the gomp_variant. */
4454 if (decl == fndecl)
4455 {
4456 thiscall = expr;
4457 omp_resolved_variant_calls->add (expr);
4458 }
4459 else
4460 {
4461 thiscall = unshare_expr (expr);
4462 CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype, decl);
4463 if (omp_dispatch_p)
4464 thiscall = modify_call_for_omp_dispatch (thiscall,
4465 dispatch_clauses,
4466 want_value,
4467 pointerize);
4468 }
4469 if (pointerize)
4470 thiscall = build_fold_addr_expr_loc (loc, thiscall);
4471 if (want_value)
4472 thiscall = build2 (INIT_EXPR, type, tmp, thiscall);
4473 all_candidates[i].alternative = thiscall;
4474 }
4475
4476 cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
4477 tree expansion = expand_late_variant_directive (all_candidates,
4478 construct_context);
4479 if (result)
4480 expansion = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4481 expansion, result);
4482 return expansion;
4483 }
4484 }
4485
4486 /* Wrapper around expand_variant_call_expr to interface with gimplifier
4487 state. EXPR and OMP_DISPATCH_P are as for expand_variant_call_expr,
4488 FALLBACK is used to compute the WANT_VALUE and POINTERIZE arguments. */
4489 static tree
4490 gimplify_variant_call_expr (tree expr, fallback_t fallback,
4491 bool omp_dispatch_p)
4492 {
4493 tree type = TREE_TYPE (expr);
4494 bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
4495 bool pointerize = false;
4496 /* If the result value must be an lvalue or the result type must
4497 live in memory, then we have to pointerize it if we need a temporary. */
4498 if (want_value
4499 && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
4500 || TREE_ADDRESSABLE (type)))
4501 pointerize = true;
4502
4503 return expand_variant_call_expr (expr, want_value, pointerize,
4504 omp_dispatch_p,
4505 (omp_dispatch_p
4506 ? gimplify_omp_ctxp->clauses
4507 : NULL_TREE));
4508 }
4509
4510
4511 /* Helper function for gimplify_call_expr, called via walk_tree.
4512 Find used user labels. */
4513
4514 static tree
4515 find_used_user_labels (tree *tp, int *, void *)
4516 {
4517 if (TREE_CODE (*tp) == LABEL_EXPR
4518 && !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (*tp))
4519 && DECL_NAME (LABEL_EXPR_LABEL (*tp))
4520 && TREE_USED (LABEL_EXPR_LABEL (*tp)))
4521 return *tp;
4522 return NULL_TREE;
4523 }
4524
4525
4526 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
4527 WANT_VALUE is true if the result of the call is desired. */
4528
4529 static enum gimplify_status
4530 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4531 {
4532 bool want_value = (fallback != fb_none);
4533 tree fndecl, parms, p, fnptrtype;
4534 enum gimplify_status ret;
4535 int i, nargs;
4536 gcall *call;
4537 bool builtin_va_start_p = false, omp_dispatch_p = false;
4538 location_t loc = EXPR_LOCATION (*expr_p);
4539
4540 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4541
4542 /* For reliable diagnostics during inlining, it is necessary that
4543 every call_expr be annotated with file and line. */
4544 if (! EXPR_HAS_LOCATION (*expr_p))
4545 SET_EXPR_LOCATION (*expr_p, input_location);
4546
4547 /* Gimplify internal functions created in the FEs. */
4548 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
4549 {
4550 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
4551 if (ifn == IFN_GOMP_DISPATCH)
4552 {
4553 gcc_assert (flag_openmp
4554 && gimplify_omp_ctxp
4555 && gimplify_omp_ctxp->code == OMP_DISPATCH);
4556 *expr_p = CALL_EXPR_ARG (*expr_p, 0);
4557 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4558 if (! EXPR_HAS_LOCATION (*expr_p))
4559 SET_EXPR_LOCATION (*expr_p, input_location);
4560 omp_dispatch_p = true;
4561 }
4562 else
4563 {
4564 if (want_value)
4565 return GS_ALL_DONE;
4566
4567 nargs = call_expr_nargs (*expr_p);
4568 auto_vec<tree> vargs (nargs);
4569
4570 if (ifn == IFN_ASSUME)
4571 {
4572 if (simple_condition_p (CALL_EXPR_ARG (*expr_p, 0)))
4573 {
4574 /* If the [[assume (cond)]]; condition is simple
4575 enough and can be evaluated unconditionally
4576 without side-effects, expand it as
4577 if (!cond) __builtin_unreachable (); */
4578 tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
4579 *expr_p
4580 = build3 (COND_EXPR, void_type_node,
4581 CALL_EXPR_ARG (*expr_p, 0), void_node,
4582 build_call_expr_loc (EXPR_LOCATION (*expr_p),
4583 fndecl, 0));
4584 return GS_OK;
4585 }
4586 /* If not optimizing, ignore the assumptions unless there
4587 are used user labels in it. */
4588 if ((!optimize
4589 && !walk_tree_without_duplicates (&CALL_EXPR_ARG (*expr_p,
4590 0),
4591 find_used_user_labels,
4592 NULL))
4593 || seen_error ())
4594 {
4595 *expr_p = NULL_TREE;
4596 return GS_ALL_DONE;
4597 }
4598 /* Temporarily, until gimple lowering, transform
4599 .ASSUME (cond);
4600 into:
4601 [[assume (guard)]]
4602 {
4603 guard = cond;
4604 }
4605 such that gimple lowering can outline the condition into
4606 a separate function easily. */
4607 tree guard = create_tmp_var (boolean_type_node);
4608 *expr_p = build2 (MODIFY_EXPR, void_type_node, guard,
4609 gimple_boolify (CALL_EXPR_ARG (*expr_p, 0)));
4610 *expr_p = build3 (BIND_EXPR, void_type_node, NULL, *expr_p, NULL);
4611 push_gimplify_context ();
4612 gimple_seq body = NULL;
4613 gimple *g = gimplify_and_return_first (*expr_p, &body);
4614 pop_gimplify_context (g);
4615 g = gimple_build_assume (guard, body);
4616 gimple_set_location (g, loc);
4617 gimplify_seq_add_stmt (pre_p, g);
4618 *expr_p = NULL_TREE;
4619 return GS_ALL_DONE;
4620 }
4621 else if (ifn == IFN_UBSAN_BOUNDS
4622 && nargs == 3
4623 && integer_onep (CALL_EXPR_ARG (*expr_p, 0)))
4624 {
4625 /* If first argument is one, add TYPE_MAX_VALUE (TYPE_DOMAIN (t))
4626 to 3rd argument and change first argument to 0. This is
4627 done by ubsan_instrument_bounds so that we can use the
4628 max value from gimplify_type_sizes here instead of original
4629 expression for VLAs. */
4630 tree type = TREE_TYPE (CALL_EXPR_ARG (*expr_p, 0));
4631 CALL_EXPR_ARG (*expr_p, 0) = build_int_cst (type, 0);
4632 gcc_assert (TREE_CODE (type) == POINTER_TYPE);
4633 type = TREE_TYPE (type);
4634 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4635 tree maxv = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4636 gcc_assert (maxv);
4637 tree arg3 = CALL_EXPR_ARG (*expr_p, 2);
4638 CALL_EXPR_ARG (*expr_p, 2)
4639 = fold_build2 (PLUS_EXPR, TREE_TYPE (arg3), maxv, arg3);
4640 }
4641
4642 for (i = 0; i < nargs; i++)
4643 {
4644 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
4645 EXPR_LOCATION (*expr_p));
4646 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
4647 }
4648
4649 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
4650 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
4651 gimplify_seq_add_stmt (pre_p, call);
4652 return GS_ALL_DONE;
4653 }
4654 }
4655
4656 /* This may be a call to a builtin function.
4657
4658 Builtin function calls may be transformed into different
4659 (and more efficient) builtin function calls under certain
4660 circumstances. Unfortunately, gimplification can muck things
4661 up enough that the builtin expanders are not aware that certain
4662 transformations are still valid.
4663
4664 So we attempt transformation/gimplification of the call before
4665 we gimplify the CALL_EXPR. At this time we do not manage to
4666 transform all calls in the same manner as the expanders do, but
4667 we do transform most of them. */
4668 fndecl = get_callee_fndecl (*expr_p);
4669 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4670 switch (DECL_FUNCTION_CODE (fndecl))
4671 {
4672 CASE_BUILT_IN_ALLOCA:
4673 /* If the call has been built for a variable-sized object, then we
4674 want to restore the stack level when the enclosing BIND_EXPR is
4675 exited to reclaim the allocated space; otherwise, we precisely
4676 need to do the opposite and preserve the latest stack level. */
4677 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
4678 gimplify_ctxp->save_stack = true;
4679 else
4680 gimplify_ctxp->keep_stack = true;
4681 break;
4682
4683 case BUILT_IN_VA_START:
4684 {
4685 builtin_va_start_p = true;
4686 if (call_expr_nargs (*expr_p) < 2)
4687 {
4688 error ("too few arguments to function %<va_start%>");
4689 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4690 return GS_OK;
4691 }
4692
4693 if (fold_builtin_next_arg (*expr_p, true))
4694 {
4695 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4696 return GS_OK;
4697 }
4698 break;
4699 }
4700
4701 case BUILT_IN_EH_RETURN:
4702 cfun->calls_eh_return = true;
4703 break;
4704
4705 case BUILT_IN_CLEAR_PADDING:
4706 if (call_expr_nargs (*expr_p) == 1)
4707 {
4708 /* Remember the original type of the argument in an internal
4709 dummy second argument, as in GIMPLE pointer conversions are
4710 useless. Also mark this call as not for automatic
4711 initialization in the internal dummy third argument. */
4712 p = CALL_EXPR_ARG (*expr_p, 0);
4713 *expr_p
4714 = build_call_expr_loc (EXPR_LOCATION (*expr_p), fndecl, 2, p,
4715 build_zero_cst (TREE_TYPE (p)));
4716 return GS_OK;
4717 }
4718 break;
4719
4720 default:
4721 ;
4722 }
4723 if (fndecl && fndecl_built_in_p (fndecl))
4724 {
4725 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4726 if (new_tree && new_tree != *expr_p)
4727 {
4728 /* There was a transformation of this call which computes the
4729 same value, but in a more efficient way. Return and try
4730 again. */
4731 *expr_p = new_tree;
4732 return GS_OK;
4733 }
4734 }
4735
4736 /* Remember the original function pointer type. */
4737 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
4738
4739 /* Handle "declare variant" resolution and arglist processing. */
4740 if (flag_openmp
4741 && fndecl
4742 && cfun
4743 && (cfun->curr_properties & PROP_gimple_any) == 0
4744 && !omp_has_novariants ()
4745 && lookup_attribute ("omp declare variant base",
4746 DECL_ATTRIBUTES (fndecl)))
4747 {
4748 tree orig = *expr_p;
4749 *expr_p = gimplify_variant_call_expr (*expr_p, fallback,
4750 omp_dispatch_p);
4751
4752 /* This may resolve to the same call, or the call expr with just
4753 the function replaced, in which case we should just continue to
4754 gimplify it normally. Otherwise, if we get something else back,
4755 stop here and re-gimplify the whole replacement expr. */
4756 if (*expr_p != orig)
4757 return GS_OK;
4758 }
4759
4760 /* There is a sequence point before the call, so any side effects in
4761 the calling expression must occur before the actual call. Force
4762 gimplify_expr to use an internal post queue. */
4763 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
4764 is_gimple_call_addr, fb_rvalue);
4765
4766 if (ret == GS_ERROR)
4767 return GS_ERROR;
4768
4769 nargs = call_expr_nargs (*expr_p);
4770
4771 /* Get argument types for verification. */
4772 fndecl = get_callee_fndecl (*expr_p);
4773 parms = NULL_TREE;
4774 if (fndecl)
4775 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4776 else
4777 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
4778
4779 if (fndecl && DECL_ARGUMENTS (fndecl))
4780 p = DECL_ARGUMENTS (fndecl);
4781 else if (parms)
4782 p = parms;
4783 else
4784 p = NULL_TREE;
4785 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
4786 ;
4787
4788 /* If the last argument is __builtin_va_arg_pack () and it is not
4789 passed as a named argument, decrease the number of CALL_EXPR
4790 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
4791 if (!p
4792 && i < nargs
4793 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
4794 {
4795 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
4796 tree last_arg_fndecl = get_callee_fndecl (last_arg);
4797
4798 if (last_arg_fndecl
4799 && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
4800 {
4801 tree call = *expr_p;
4802
4803 --nargs;
4804 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
4805 CALL_EXPR_FN (call),
4806 nargs, CALL_EXPR_ARGP (call));
4807
4808 /* Copy all CALL_EXPR flags, location and block, except
4809 CALL_EXPR_VA_ARG_PACK flag. */
4810 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
4811 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
4812 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
4813 = CALL_EXPR_RETURN_SLOT_OPT (call);
4814 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
4815 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
4816
4817 /* Set CALL_EXPR_VA_ARG_PACK. */
4818 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
4819 }
4820 }
4821
4822 /* If the call returns twice then after building the CFG the call
4823 argument computations will no longer dominate the call because
4824 we add an abnormal incoming edge to the call. So do not use SSA
4825 vars there. */
4826 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
4827
4828
4829 /* Gimplify the function arguments. */
4830 if (nargs > 0)
4831 {
4832 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
4833 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
4834 PUSH_ARGS_REVERSED ? i-- : i++)
4835 {
4836 enum gimplify_status t;
4837
4838 /* Avoid gimplifying the second argument to va_start, which needs to
4839 be the plain PARM_DECL. */
4840 if ((i != 1) || !builtin_va_start_p)
4841 {
4842 tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
4843
4844 if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4845 gimplify_omp_ctxp->in_call_args = true;
4846 t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
4847 !returns_twice);
4848 if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4849 gimplify_omp_ctxp->in_call_args = false;
4850
4851 if (t == GS_ERROR)
4852 ret = GS_ERROR;
4853 }
4854 }
4855 }
4856
4857 /* Gimplify the static chain. */
4858 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
4859 {
4860 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
4861 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
4862 else
4863 {
4864 enum gimplify_status t;
4865 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
4866 EXPR_LOCATION (*expr_p), ! returns_twice);
4867 if (t == GS_ERROR)
4868 ret = GS_ERROR;
4869 }
4870 }
4871
4872 /* Verify the function result. */
4873 if (want_value && fndecl
4874 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
4875 {
4876 error_at (loc, "using result of function returning %<void%>");
4877 ret = GS_ERROR;
4878 }
4879
4880 /* Try this again in case gimplification exposed something. */
4881 if (ret != GS_ERROR)
4882 {
4883 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4884
4885 if (new_tree && new_tree != *expr_p)
4886 {
4887 /* There was a transformation of this call which computes the
4888 same value, but in a more efficient way. Return and try
4889 again. */
4890 *expr_p = new_tree;
4891 return GS_OK;
4892 }
4893 }
4894 else
4895 {
4896 *expr_p = error_mark_node;
4897 return GS_ERROR;
4898 }
4899
4900 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
4901 decl. This allows us to eliminate redundant or useless
4902 calls to "const" functions. */
4903 if (TREE_CODE (*expr_p) == CALL_EXPR)
4904 {
4905 int flags = call_expr_flags (*expr_p);
4906 if (flags & (ECF_CONST | ECF_PURE)
4907 /* An infinite loop is considered a side effect. */
4908 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
4909 TREE_SIDE_EFFECTS (*expr_p) = 0;
4910 }
4911
4912 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
4913 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
4914 form and delegate the creation of a GIMPLE_CALL to
4915 gimplify_modify_expr. This is always possible because when
4916 WANT_VALUE is true, the caller wants the result of this call into
4917 a temporary, which means that we will emit an INIT_EXPR in
4918 internal_get_tmp_var which will then be handled by
4919 gimplify_modify_expr. */
4920 if (!want_value)
4921 {
4922 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
4923 have to do is replicate it as a GIMPLE_CALL tuple. */
4924 gimple_stmt_iterator gsi;
4925 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
4926 notice_special_calls (call);
4927 gimplify_seq_add_stmt (pre_p, call);
4928 gsi = gsi_last (*pre_p);
4929 maybe_fold_stmt (&gsi);
4930 *expr_p = NULL_TREE;
4931 }
4932 else
4933 /* Remember the original function type. */
4934 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
4935 CALL_EXPR_FN (*expr_p));
4936
4937 return ret;
4938 }
4939
4940 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
4941 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
4942
4943 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
4944 condition is true or false, respectively. If null, we should generate
4945 our own to skip over the evaluation of this specific expression.
4946
4947 LOCUS is the source location of the COND_EXPR.
4948
4949 The condition_uid is a discriminator tag for condition coverage used to map
4950 conditions to its corresponding full Boolean function.
4951
4952 This function is the tree equivalent of do_jump.
4953
4954 shortcut_cond_r should only be called by shortcut_cond_expr. */
4955
4956 static tree
4957 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
4958 location_t locus, unsigned condition_uid)
4959 {
4960 tree local_label = NULL_TREE;
4961 tree t, expr = NULL;
4962
4963 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
4964 retain the shortcut semantics. Just insert the gotos here;
4965 shortcut_cond_expr will append the real blocks later. */
4966 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
4967 {
4968 location_t new_locus;
4969
4970 /* Turn if (a && b) into
4971
4972 if (a); else goto no;
4973 if (b) goto yes; else goto no;
4974 (no:) */
4975
4976 if (false_label_p == NULL)
4977 false_label_p = &local_label;
4978
4979 /* Keep the original source location on the first 'if'. */
4980 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
4981 condition_uid);
4982 append_to_statement_list (t, &expr);
4983
4984 /* Set the source location of the && on the second 'if'. */
4985 new_locus = rexpr_location (pred, locus);
4986 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4987 new_locus, condition_uid);
4988 append_to_statement_list (t, &expr);
4989 }
4990 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
4991 {
4992 location_t new_locus;
4993
4994 /* Turn if (a || b) into
4995
4996 if (a) goto yes;
4997 if (b) goto yes; else goto no;
4998 (yes:) */
4999
5000 if (true_label_p == NULL)
5001 true_label_p = &local_label;
5002
5003 /* Keep the original source location on the first 'if'. */
5004 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
5005 condition_uid);
5006 append_to_statement_list (t, &expr);
5007
5008 /* Set the source location of the || on the second 'if'. */
5009 new_locus = rexpr_location (pred, locus);
5010 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
5011 new_locus, condition_uid);
5012 append_to_statement_list (t, &expr);
5013 }
5014 else if (TREE_CODE (pred) == COND_EXPR
5015 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
5016 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
5017 {
5018 location_t new_locus;
5019
5020 /* As long as we're messing with gotos, turn if (a ? b : c) into
5021 if (a)
5022 if (b) goto yes; else goto no;
5023 else
5024 if (c) goto yes; else goto no;
5025
5026 Don't do this if one of the arms has void type, which can happen
5027 in C++ when the arm is throw. */
5028
5029 /* Keep the original source location on the first 'if'. Set the source
5030 location of the ? on the second 'if'. */
5031 new_locus = rexpr_location (pred, locus);
5032 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
5033 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
5034 false_label_p, locus, condition_uid),
5035 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
5036 false_label_p, new_locus,
5037 condition_uid));
5038 tree_associate_condition_with_expr (expr, condition_uid);
5039 }
5040 else
5041 {
5042 expr = build3 (COND_EXPR, void_type_node, pred,
5043 build_and_jump (true_label_p),
5044 build_and_jump (false_label_p));
5045 SET_EXPR_LOCATION (expr, locus);
5046 tree_associate_condition_with_expr (expr, condition_uid);
5047 }
5048
5049 if (local_label)
5050 {
5051 t = build1 (LABEL_EXPR, void_type_node, local_label);
5052 append_to_statement_list (t, &expr);
5053 }
5054
5055 return expr;
5056 }
5057
5058 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
5059 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
5060 statement, if it is the last one. Otherwise, return NULL. */
5061
5062 static tree
5063 find_goto (tree expr)
5064 {
5065 if (!expr)
5066 return NULL_TREE;
5067
5068 if (TREE_CODE (expr) == GOTO_EXPR)
5069 return expr;
5070
5071 if (TREE_CODE (expr) != STATEMENT_LIST)
5072 return NULL_TREE;
5073
5074 tree_stmt_iterator i = tsi_start (expr);
5075
5076 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
5077 tsi_next (&i);
5078
5079 if (!tsi_one_before_end_p (i))
5080 return NULL_TREE;
5081
5082 return find_goto (tsi_stmt (i));
5083 }
5084
5085 /* Same as find_goto, except that it returns NULL if the destination
5086 is not a LABEL_DECL. */
5087
5088 static inline tree
5089 find_goto_label (tree expr)
5090 {
5091 tree dest = find_goto (expr);
5092 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
5093 return dest;
5094 return NULL_TREE;
5095 }
5096
5097
5098 /* Given a multi-term condition (ANDIF, ORIF), walk the predicate PRED and tag
5099 every basic condition with CONDITION_UID. Two basic conditions share the
5100 CONDITION_UID discriminator when they belong to the same predicate, which is
5101 used by the condition coverage. Doing this as an explicit step makes for a
5102 simpler implementation than weaving it into the splitting code as the
5103 splitting code eventually calls the entry point gimplfiy_expr which makes
5104 bookkeeping complicated. */
5105 static void
5106 tag_shortcut_cond (tree pred, unsigned condition_uid)
5107 {
5108 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
5109 || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5110 {
5111 tree fst = TREE_OPERAND (pred, 0);
5112 tree lst = TREE_OPERAND (pred, 1);
5113
5114 if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
5115 || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
5116 tag_shortcut_cond (fst, condition_uid);
5117 else if (TREE_CODE (fst) == COND_EXPR)
5118 tree_associate_condition_with_expr (fst, condition_uid);
5119
5120 if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
5121 || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
5122 tag_shortcut_cond (lst, condition_uid);
5123 else if (TREE_CODE (lst) == COND_EXPR)
5124 tree_associate_condition_with_expr (lst, condition_uid);
5125 }
5126 }
5127
5128 /* Given a conditional expression EXPR with short-circuit boolean
5129 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
5130 predicate apart into the equivalent sequence of conditionals. CONDITION_UID
5131 is a the tag/discriminator for this EXPR - all basic conditions in the
5132 expression will be given the same CONDITION_UID. */
5133 static tree
5134 shortcut_cond_expr (tree expr, unsigned condition_uid)
5135 {
5136 tree pred = TREE_OPERAND (expr, 0);
5137 tree then_ = TREE_OPERAND (expr, 1);
5138 tree else_ = TREE_OPERAND (expr, 2);
5139 tree true_label, false_label, end_label, t;
5140 tree *true_label_p;
5141 tree *false_label_p;
5142 bool emit_end, emit_false, jump_over_else;
5143 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
5144 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
5145
5146 tag_shortcut_cond (pred, condition_uid);
5147
5148 /* First do simple transformations. */
5149 if (!else_se)
5150 {
5151 /* If there is no 'else', turn
5152 if (a && b) then c
5153 into
5154 if (a) if (b) then c. */
5155 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
5156 {
5157 /* Keep the original source location on the first 'if'. */
5158 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5159 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5160 /* Set the source location of the && on the second 'if'. */
5161 if (rexpr_has_location (pred))
5162 SET_EXPR_LOCATION (expr, rexpr_location (pred));
5163 then_ = shortcut_cond_expr (expr, condition_uid);
5164 then_se = then_ && TREE_SIDE_EFFECTS (then_);
5165 pred = TREE_OPERAND (pred, 0);
5166 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
5167 SET_EXPR_LOCATION (expr, locus);
5168 }
5169 }
5170
5171 if (!then_se)
5172 {
5173 /* If there is no 'then', turn
5174 if (a || b); else d
5175 into
5176 if (a); else if (b); else d. */
5177 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5178 {
5179 /* Keep the original source location on the first 'if'. */
5180 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5181 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5182 /* Set the source location of the || on the second 'if'. */
5183 if (rexpr_has_location (pred))
5184 SET_EXPR_LOCATION (expr, rexpr_location (pred));
5185 else_ = shortcut_cond_expr (expr, condition_uid);
5186 else_se = else_ && TREE_SIDE_EFFECTS (else_);
5187 pred = TREE_OPERAND (pred, 0);
5188 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
5189 SET_EXPR_LOCATION (expr, locus);
5190 }
5191 }
5192
5193 /* The expr tree should also have the expression id set. */
5194 tree_associate_condition_with_expr (expr, condition_uid);
5195
5196 /* If we're done, great. */
5197 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
5198 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
5199 return expr;
5200
5201 /* Otherwise we need to mess with gotos. Change
5202 if (a) c; else d;
5203 to
5204 if (a); else goto no;
5205 c; goto end;
5206 no: d; end:
5207 and recursively gimplify the condition. */
5208
5209 true_label = false_label = end_label = NULL_TREE;
5210
5211 /* If our arms just jump somewhere, hijack those labels so we don't
5212 generate jumps to jumps. */
5213
5214 if (tree then_goto = find_goto_label (then_))
5215 {
5216 true_label = GOTO_DESTINATION (then_goto);
5217 then_ = NULL;
5218 then_se = false;
5219 }
5220
5221 if (tree else_goto = find_goto_label (else_))
5222 {
5223 false_label = GOTO_DESTINATION (else_goto);
5224 else_ = NULL;
5225 else_se = false;
5226 }
5227
5228 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
5229 if (true_label)
5230 true_label_p = &true_label;
5231 else
5232 true_label_p = NULL;
5233
5234 /* The 'else' branch also needs a label if it contains interesting code. */
5235 if (false_label || else_se)
5236 false_label_p = &false_label;
5237 else
5238 false_label_p = NULL;
5239
5240 /* If there was nothing else in our arms, just forward the label(s). */
5241 if (!then_se && !else_se)
5242 return shortcut_cond_r (pred, true_label_p, false_label_p,
5243 EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
5244
5245 /* If our last subexpression already has a terminal label, reuse it. */
5246 if (else_se)
5247 t = expr_last (else_);
5248 else if (then_se)
5249 t = expr_last (then_);
5250 else
5251 t = NULL;
5252 if (t && TREE_CODE (t) == LABEL_EXPR)
5253 end_label = LABEL_EXPR_LABEL (t);
5254
5255 /* If we don't care about jumping to the 'else' branch, jump to the end
5256 if the condition is false. */
5257 if (!false_label_p)
5258 false_label_p = &end_label;
5259
5260 /* We only want to emit these labels if we aren't hijacking them. */
5261 emit_end = (end_label == NULL_TREE);
5262 emit_false = (false_label == NULL_TREE);
5263
5264 /* We only emit the jump over the else clause if we have to--if the
5265 then clause may fall through. Otherwise we can wind up with a
5266 useless jump and a useless label at the end of gimplified code,
5267 which will cause us to think that this conditional as a whole
5268 falls through even if it doesn't. If we then inline a function
5269 which ends with such a condition, that can cause us to issue an
5270 inappropriate warning about control reaching the end of a
5271 non-void function. */
5272 jump_over_else = block_may_fallthru (then_);
5273
5274 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
5275 EXPR_LOC_OR_LOC (expr, input_location),
5276 condition_uid);
5277
5278 expr = NULL;
5279 append_to_statement_list (pred, &expr);
5280
5281 append_to_statement_list (then_, &expr);
5282 if (else_se)
5283 {
5284 if (jump_over_else)
5285 {
5286 tree last = expr_last (expr);
5287 t = build_and_jump (&end_label);
5288 if (rexpr_has_location (last))
5289 SET_EXPR_LOCATION (t, rexpr_location (last));
5290 append_to_statement_list (t, &expr);
5291 }
5292 if (emit_false)
5293 {
5294 t = build1 (LABEL_EXPR, void_type_node, false_label);
5295 append_to_statement_list (t, &expr);
5296 }
5297 append_to_statement_list (else_, &expr);
5298 }
5299 if (emit_end && end_label)
5300 {
5301 t = build1 (LABEL_EXPR, void_type_node, end_label);
5302 append_to_statement_list (t, &expr);
5303 }
5304
5305 return expr;
5306 }
5307
5308 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
5309
5310 tree
5311 gimple_boolify (tree expr)
5312 {
5313 tree type = TREE_TYPE (expr);
5314 location_t loc = EXPR_LOCATION (expr);
5315
5316 if (TREE_CODE (expr) == NE_EXPR
5317 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
5318 && integer_zerop (TREE_OPERAND (expr, 1)))
5319 {
5320 tree call = TREE_OPERAND (expr, 0);
5321 tree fn = get_callee_fndecl (call);
5322
5323 /* For __builtin_expect ((long) (x), y) recurse into x as well
5324 if x is truth_value_p. */
5325 if (fn
5326 && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
5327 && call_expr_nargs (call) == 2)
5328 {
5329 tree arg = CALL_EXPR_ARG (call, 0);
5330 if (arg)
5331 {
5332 if (TREE_CODE (arg) == NOP_EXPR
5333 && TREE_TYPE (arg) == TREE_TYPE (call))
5334 arg = TREE_OPERAND (arg, 0);
5335 if (truth_value_p (TREE_CODE (arg)))
5336 {
5337 arg = gimple_boolify (arg);
5338 CALL_EXPR_ARG (call, 0)
5339 = fold_convert_loc (loc, TREE_TYPE (call), arg);
5340 }
5341 }
5342 }
5343 }
5344
5345 switch (TREE_CODE (expr))
5346 {
5347 case TRUTH_AND_EXPR:
5348 case TRUTH_OR_EXPR:
5349 case TRUTH_XOR_EXPR:
5350 case TRUTH_ANDIF_EXPR:
5351 case TRUTH_ORIF_EXPR:
5352 /* Also boolify the arguments of truth exprs. */
5353 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
5354 /* FALLTHRU */
5355
5356 case TRUTH_NOT_EXPR:
5357 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5358
5359 /* These expressions always produce boolean results. */
5360 if (TREE_CODE (type) != BOOLEAN_TYPE)
5361 TREE_TYPE (expr) = boolean_type_node;
5362 return expr;
5363
5364 case ANNOTATE_EXPR:
5365 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
5366 {
5367 case annot_expr_ivdep_kind:
5368 case annot_expr_unroll_kind:
5369 case annot_expr_no_vector_kind:
5370 case annot_expr_vector_kind:
5371 case annot_expr_parallel_kind:
5372 case annot_expr_maybe_infinite_kind:
5373 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5374 if (TREE_CODE (type) != BOOLEAN_TYPE)
5375 TREE_TYPE (expr) = boolean_type_node;
5376 return expr;
5377 default:
5378 gcc_unreachable ();
5379 }
5380
5381 default:
5382 if (COMPARISON_CLASS_P (expr))
5383 {
5384 /* These expressions always produce boolean results. */
5385 if (TREE_CODE (type) != BOOLEAN_TYPE)
5386 TREE_TYPE (expr) = boolean_type_node;
5387 return expr;
5388 }
5389 /* Other expressions that get here must have boolean values, but
5390 might need to be converted to the appropriate mode. */
5391 if (TREE_CODE (type) == BOOLEAN_TYPE)
5392 return expr;
5393 return fold_convert_loc (loc, boolean_type_node, expr);
5394 }
5395 }
5396
5397 /* Given a conditional expression *EXPR_P without side effects, gimplify
5398 its operands. New statements are inserted to PRE_P. */
5399
5400 static enum gimplify_status
5401 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
5402 {
5403 tree expr = *expr_p, cond;
5404 enum gimplify_status ret, tret;
5405 enum tree_code code;
5406
5407 cond = gimple_boolify (COND_EXPR_COND (expr));
5408
5409 /* We need to handle && and || specially, as their gimplification
5410 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
5411 code = TREE_CODE (cond);
5412 if (code == TRUTH_ANDIF_EXPR)
5413 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
5414 else if (code == TRUTH_ORIF_EXPR)
5415 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
5416 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
5417 COND_EXPR_COND (*expr_p) = cond;
5418
5419 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
5420 is_gimple_val, fb_rvalue);
5421 ret = MIN (ret, tret);
5422 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
5423 is_gimple_val, fb_rvalue);
5424
5425 return MIN (ret, tret);
5426 }
5427
5428 /* Return true if evaluating EXPR could trap.
5429 EXPR is GENERIC, while tree_could_trap_p can be called
5430 only on GIMPLE. */
5431
5432 bool
5433 generic_expr_could_trap_p (tree expr)
5434 {
5435 unsigned i, n;
5436
5437 if (!expr || is_gimple_val (expr))
5438 return false;
5439
5440 if (!EXPR_P (expr) || tree_could_trap_p (expr))
5441 return true;
5442
5443 n = TREE_OPERAND_LENGTH (expr);
5444 for (i = 0; i < n; i++)
5445 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
5446 return true;
5447
5448 return false;
5449 }
5450
5451 /* Associate the condition STMT with the discriminator UID. STMTs that are
5452 broken down with ANDIF/ORIF from the same Boolean expression should be given
5453 the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
5454 { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
5455 for condition coverage. */
5456 static void
5457 gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
5458 unsigned uid)
5459 {
5460 if (!condition_coverage_flag)
5461 return;
5462
5463 if (!fn->cond_uids)
5464 fn->cond_uids = new hash_map <gcond*, unsigned> ();
5465
5466 fn->cond_uids->put (stmt, uid);
5467 }
5468
5469 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
5470 into
5471
5472 if (p) if (p)
5473 t1 = a; a;
5474 else or else
5475 t1 = b; b;
5476 t1;
5477
5478 The second form is used when *EXPR_P is of type void.
5479
5480 PRE_P points to the list where side effects that must happen before
5481 *EXPR_P should be stored. */
5482
5483 static enum gimplify_status
5484 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
5485 {
5486 tree expr = *expr_p;
5487 tree type = TREE_TYPE (expr);
5488 location_t loc = EXPR_LOCATION (expr);
5489 tree tmp, arm1, arm2;
5490 enum gimplify_status ret;
5491 tree label_true, label_false, label_cont;
5492 bool have_then_clause_p, have_else_clause_p;
5493 gcond *cond_stmt;
5494 enum tree_code pred_code;
5495 gimple_seq seq = NULL;
5496
5497 /* If this COND_EXPR has a value, copy the values into a temporary within
5498 the arms. */
5499 if (!VOID_TYPE_P (type))
5500 {
5501 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
5502 tree result;
5503
5504 /* If either an rvalue is ok or we do not require an lvalue, create the
5505 temporary. But we cannot do that if the type is addressable. */
5506 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
5507 && !TREE_ADDRESSABLE (type))
5508 {
5509 if (gimplify_ctxp->allow_rhs_cond_expr
5510 /* If either branch has side effects or could trap, it can't be
5511 evaluated unconditionally. */
5512 && !TREE_SIDE_EFFECTS (then_)
5513 && !generic_expr_could_trap_p (then_)
5514 && !TREE_SIDE_EFFECTS (else_)
5515 && !generic_expr_could_trap_p (else_))
5516 return gimplify_pure_cond_expr (expr_p, pre_p);
5517
5518 tmp = create_tmp_var (type, "iftmp");
5519 result = tmp;
5520 }
5521
5522 /* Otherwise, only create and copy references to the values. */
5523 else
5524 {
5525 type = build_pointer_type (type);
5526
5527 if (!VOID_TYPE_P (TREE_TYPE (then_)))
5528 then_ = build_fold_addr_expr_loc (loc, then_);
5529
5530 if (!VOID_TYPE_P (TREE_TYPE (else_)))
5531 else_ = build_fold_addr_expr_loc (loc, else_);
5532
5533 expr
5534 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
5535
5536 tmp = create_tmp_var (type, "iftmp");
5537 result = build_simple_mem_ref_loc (loc, tmp);
5538 }
5539
5540 /* Build the new then clause, `tmp = then_;'. But don't build the
5541 assignment if the value is void; in C++ it can be if it's a throw. */
5542 if (!VOID_TYPE_P (TREE_TYPE (then_)))
5543 TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
5544
5545 /* Similarly, build the new else clause, `tmp = else_;'. */
5546 if (!VOID_TYPE_P (TREE_TYPE (else_)))
5547 TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
5548
5549 TREE_TYPE (expr) = void_type_node;
5550 recalculate_side_effects (expr);
5551
5552 /* Move the COND_EXPR to the prequeue. */
5553 gimplify_stmt (&expr, pre_p);
5554
5555 *expr_p = result;
5556 return GS_ALL_DONE;
5557 }
5558
5559 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
5560 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
5561 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
5562 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
5563
5564 /* Make sure the condition has BOOLEAN_TYPE. */
5565 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5566
5567 /* Break apart && and || conditions. */
5568 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
5569 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
5570 {
5571 expr = shortcut_cond_expr (expr, next_cond_uid ());
5572
5573 if (expr != *expr_p)
5574 {
5575 *expr_p = expr;
5576
5577 /* We can't rely on gimplify_expr to re-gimplify the expanded
5578 form properly, as cleanups might cause the target labels to be
5579 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
5580 set up a conditional context. */
5581 gimple_push_condition ();
5582 gimplify_stmt (expr_p, &seq);
5583 gimple_pop_condition (pre_p);
5584 gimple_seq_add_seq (pre_p, seq);
5585
5586 return GS_ALL_DONE;
5587 }
5588 }
5589
5590 /* Now do the normal gimplification. */
5591
5592 /* Gimplify condition. */
5593 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
5594 is_gimple_condexpr_for_cond, fb_rvalue);
5595 if (ret == GS_ERROR)
5596 return GS_ERROR;
5597 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
5598
5599 gimple_push_condition ();
5600
5601 have_then_clause_p = have_else_clause_p = false;
5602 label_true = find_goto_label (TREE_OPERAND (expr, 1));
5603 if (label_true
5604 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
5605 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5606 have different locations, otherwise we end up with incorrect
5607 location information on the branches. */
5608 && (optimize
5609 || !EXPR_HAS_LOCATION (expr)
5610 || !rexpr_has_location (label_true)
5611 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
5612 {
5613 have_then_clause_p = true;
5614 label_true = GOTO_DESTINATION (label_true);
5615 }
5616 else
5617 label_true = create_artificial_label (UNKNOWN_LOCATION);
5618 label_false = find_goto_label (TREE_OPERAND (expr, 2));
5619 if (label_false
5620 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
5621 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5622 have different locations, otherwise we end up with incorrect
5623 location information on the branches. */
5624 && (optimize
5625 || !EXPR_HAS_LOCATION (expr)
5626 || !rexpr_has_location (label_false)
5627 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
5628 {
5629 have_else_clause_p = true;
5630 label_false = GOTO_DESTINATION (label_false);
5631 }
5632 else
5633 label_false = create_artificial_label (UNKNOWN_LOCATION);
5634
5635 unsigned cond_uid = 0;
5636 if (cond_uids)
5637 if (unsigned *v = cond_uids->get (expr))
5638 cond_uid = *v;
5639 if (cond_uid == 0)
5640 cond_uid = next_cond_uid ();
5641
5642 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
5643 &arm2);
5644 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
5645 label_false);
5646 gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
5647 gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
5648 copy_warning (cond_stmt, COND_EXPR_COND (expr));
5649 gimplify_seq_add_stmt (&seq, cond_stmt);
5650 gimple_stmt_iterator gsi = gsi_last (seq);
5651 maybe_fold_stmt (&gsi);
5652
5653 label_cont = NULL_TREE;
5654 if (!have_then_clause_p)
5655 {
5656 /* For if (...) {} else { code; } put label_true after
5657 the else block. */
5658 if (TREE_OPERAND (expr, 1) == NULL_TREE
5659 && !have_else_clause_p
5660 && TREE_OPERAND (expr, 2) != NULL_TREE)
5661 {
5662 /* For if (0) {} else { code; } tell -Wimplicit-fallthrough
5663 handling that label_cont == label_true can be only reached
5664 through fallthrough from { code; }. */
5665 if (integer_zerop (COND_EXPR_COND (expr)))
5666 UNUSED_LABEL_P (label_true) = 1;
5667 label_cont = label_true;
5668 }
5669 else
5670 {
5671 bool then_side_effects
5672 = (TREE_OPERAND (expr, 1)
5673 && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
5674 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
5675 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
5676 /* For if (...) { code; } else {} or
5677 if (...) { code; } else goto label; or
5678 if (...) { code; return; } else { ... }
5679 label_cont isn't needed. */
5680 if (!have_else_clause_p
5681 && TREE_OPERAND (expr, 2) != NULL_TREE
5682 && gimple_seq_may_fallthru (seq))
5683 {
5684 gimple *g;
5685 label_cont = create_artificial_label (UNKNOWN_LOCATION);
5686
5687 /* For if (0) { non-side-effect-code } else { code }
5688 tell -Wimplicit-fallthrough handling that label_cont can
5689 be only reached through fallthrough from { code }. */
5690 if (integer_zerop (COND_EXPR_COND (expr)))
5691 {
5692 UNUSED_LABEL_P (label_true) = 1;
5693 if (!then_side_effects)
5694 UNUSED_LABEL_P (label_cont) = 1;
5695 }
5696
5697 g = gimple_build_goto (label_cont);
5698
5699 /* GIMPLE_COND's are very low level; they have embedded
5700 gotos. This particular embedded goto should not be marked
5701 with the location of the original COND_EXPR, as it would
5702 correspond to the COND_EXPR's condition, not the ELSE or the
5703 THEN arms. To avoid marking it with the wrong location, flag
5704 it as "no location". */
5705 gimple_set_do_not_emit_location (g);
5706
5707 gimplify_seq_add_stmt (&seq, g);
5708 }
5709 }
5710 }
5711 if (!have_else_clause_p)
5712 {
5713 /* For if (1) { code } or if (1) { code } else { non-side-effect-code }
5714 tell -Wimplicit-fallthrough handling that label_false can be only
5715 reached through fallthrough from { code }. */
5716 if (integer_nonzerop (COND_EXPR_COND (expr))
5717 && (TREE_OPERAND (expr, 2) == NULL_TREE
5718 || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
5719 UNUSED_LABEL_P (label_false) = 1;
5720 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
5721 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
5722 }
5723 if (label_cont)
5724 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
5725
5726 gimple_pop_condition (pre_p);
5727 gimple_seq_add_seq (pre_p, seq);
5728
5729 if (ret == GS_ERROR)
5730 ; /* Do nothing. */
5731 else if (have_then_clause_p || have_else_clause_p)
5732 ret = GS_ALL_DONE;
5733 else
5734 {
5735 /* Both arms are empty; replace the COND_EXPR with its predicate. */
5736 expr = TREE_OPERAND (expr, 0);
5737 gimplify_stmt (&expr, pre_p);
5738 }
5739
5740 *expr_p = NULL;
5741 return ret;
5742 }
5743
5744 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
5745 to be marked addressable.
5746
5747 We cannot rely on such an expression being directly markable if a temporary
5748 has been created by the gimplification. In this case, we create another
5749 temporary and initialize it with a copy, which will become a store after we
5750 mark it addressable. This can happen if the front-end passed us something
5751 that it could not mark addressable yet, like a Fortran pass-by-reference
5752 parameter (int) floatvar. */
5753
5754 static void
5755 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
5756 {
5757 while (handled_component_p (*expr_p))
5758 expr_p = &TREE_OPERAND (*expr_p, 0);
5759
5760 /* Do not allow an SSA name as the temporary. */
5761 if (is_gimple_reg (*expr_p))
5762 *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
5763 }
5764
5765 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5766 a call to __builtin_memcpy. */
5767
5768 static enum gimplify_status
5769 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
5770 gimple_seq *seq_p)
5771 {
5772 tree t, to, to_ptr, from, from_ptr;
5773 gcall *gs;
5774 location_t loc = EXPR_LOCATION (*expr_p);
5775
5776 to = TREE_OPERAND (*expr_p, 0);
5777 from = TREE_OPERAND (*expr_p, 1);
5778 gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to)))
5779 && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))));
5780
5781 /* Mark the RHS addressable. Beware that it may not be possible to do so
5782 directly if a temporary has been created by the gimplification. */
5783 prepare_gimple_addressable (&from, seq_p);
5784
5785 mark_addressable (from);
5786 from_ptr = build_fold_addr_expr_loc (loc, from);
5787 gimplify_arg (&from_ptr, seq_p, loc);
5788
5789 mark_addressable (to);
5790 to_ptr = build_fold_addr_expr_loc (loc, to);
5791 gimplify_arg (&to_ptr, seq_p, loc);
5792
5793 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
5794
5795 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
5796 gimple_call_set_alloca_for_var (gs, true);
5797
5798 if (want_value)
5799 {
5800 /* tmp = memcpy() */
5801 t = create_tmp_var (TREE_TYPE (to_ptr));
5802 gimple_call_set_lhs (gs, t);
5803 gimplify_seq_add_stmt (seq_p, gs);
5804
5805 *expr_p = build_simple_mem_ref (t);
5806 return GS_ALL_DONE;
5807 }
5808
5809 gimplify_seq_add_stmt (seq_p, gs);
5810 *expr_p = NULL;
5811 return GS_ALL_DONE;
5812 }
5813
5814 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5815 a call to __builtin_memset. In this case we know that the RHS is
5816 a CONSTRUCTOR with an empty element list. */
5817
5818 static enum gimplify_status
5819 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
5820 gimple_seq *seq_p)
5821 {
5822 tree t, from, to, to_ptr;
5823 gcall *gs;
5824 location_t loc = EXPR_LOCATION (*expr_p);
5825
5826 /* Assert our assumptions, to abort instead of producing wrong code
5827 silently if they are not met. Beware that the RHS CONSTRUCTOR might
5828 not be immediately exposed. */
5829 from = TREE_OPERAND (*expr_p, 1);
5830 if (TREE_CODE (from) == WITH_SIZE_EXPR)
5831 from = TREE_OPERAND (from, 0);
5832
5833 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
5834 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
5835
5836 /* Now proceed. */
5837 to = TREE_OPERAND (*expr_p, 0);
5838 gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to))));
5839
5840 to_ptr = build_fold_addr_expr_loc (loc, to);
5841 gimplify_arg (&to_ptr, seq_p, loc);
5842 t = builtin_decl_implicit (BUILT_IN_MEMSET);
5843
5844 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
5845
5846 if (want_value)
5847 {
5848 /* tmp = memset() */
5849 t = create_tmp_var (TREE_TYPE (to_ptr));
5850 gimple_call_set_lhs (gs, t);
5851 gimplify_seq_add_stmt (seq_p, gs);
5852
5853 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
5854 return GS_ALL_DONE;
5855 }
5856
5857 gimplify_seq_add_stmt (seq_p, gs);
5858 *expr_p = NULL;
5859 return GS_ALL_DONE;
5860 }
5861
5862 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
5863 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
5864 assignment. Return non-null if we detect a potential overlap. */
5865
5866 struct gimplify_init_ctor_preeval_data
5867 {
5868 /* The base decl of the lhs object. May be NULL, in which case we
5869 have to assume the lhs is indirect. */
5870 tree lhs_base_decl;
5871
5872 /* The alias set of the lhs object. */
5873 alias_set_type lhs_alias_set;
5874 };
5875
5876 static tree
5877 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
5878 {
5879 struct gimplify_init_ctor_preeval_data *data
5880 = (struct gimplify_init_ctor_preeval_data *) xdata;
5881 tree t = *tp;
5882
5883 /* If we find the base object, obviously we have overlap. */
5884 if (data->lhs_base_decl == t)
5885 return t;
5886
5887 /* If the constructor component is indirect, determine if we have a
5888 potential overlap with the lhs. The only bits of information we
5889 have to go on at this point are addressability and alias sets. */
5890 if ((INDIRECT_REF_P (t)
5891 || TREE_CODE (t) == MEM_REF)
5892 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5893 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
5894 return t;
5895
5896 /* If the constructor component is a call, determine if it can hide a
5897 potential overlap with the lhs through an INDIRECT_REF like above.
5898 ??? Ugh - this is completely broken. In fact this whole analysis
5899 doesn't look conservative. */
5900 if (TREE_CODE (t) == CALL_EXPR)
5901 {
5902 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
5903
5904 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
5905 if (POINTER_TYPE_P (TREE_VALUE (type))
5906 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5907 && alias_sets_conflict_p (data->lhs_alias_set,
5908 get_alias_set
5909 (TREE_TYPE (TREE_VALUE (type)))))
5910 return t;
5911 }
5912
5913 if (IS_TYPE_OR_DECL_P (t))
5914 *walk_subtrees = 0;
5915 return NULL;
5916 }
5917
5918 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
5919 force values that overlap with the lhs (as described by *DATA)
5920 into temporaries. */
5921
5922 static void
5923 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5924 struct gimplify_init_ctor_preeval_data *data)
5925 {
5926 enum gimplify_status one;
5927
5928 /* If the value is constant, then there's nothing to pre-evaluate. */
5929 if (TREE_CONSTANT (*expr_p))
5930 {
5931 /* Ensure it does not have side effects, it might contain a reference to
5932 the object we're initializing. */
5933 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
5934 return;
5935 }
5936
5937 /* If the type has non-trivial constructors, we can't pre-evaluate. */
5938 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
5939 return;
5940
5941 /* Recurse for nested constructors. */
5942 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
5943 {
5944 unsigned HOST_WIDE_INT ix;
5945 constructor_elt *ce;
5946 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
5947
5948 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
5949 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
5950
5951 return;
5952 }
5953
5954 /* If this is a variable sized type, we must remember the size. */
5955 maybe_with_size_expr (expr_p);
5956
5957 /* Gimplify the constructor element to something appropriate for the rhs
5958 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
5959 the gimplifier will consider this a store to memory. Doing this
5960 gimplification now means that we won't have to deal with complicated
5961 language-specific trees, nor trees like SAVE_EXPR that can induce
5962 exponential search behavior. */
5963 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
5964 if (one == GS_ERROR)
5965 {
5966 *expr_p = NULL;
5967 return;
5968 }
5969
5970 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
5971 with the lhs, since "a = { .x=a }" doesn't make sense. This will
5972 always be true for all scalars, since is_gimple_mem_rhs insists on a
5973 temporary variable for them. */
5974 if (DECL_P (*expr_p))
5975 return;
5976
5977 /* If this is of variable size, we have no choice but to assume it doesn't
5978 overlap since we can't make a temporary for it. */
5979 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
5980 return;
5981
5982 /* Otherwise, we must search for overlap ... */
5983 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
5984 return;
5985
5986 /* ... and if found, force the value into a temporary. */
5987 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
5988 }
5989
5990 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
5991 a RANGE_EXPR in a CONSTRUCTOR for an array.
5992
5993 var = lower;
5994 loop_entry:
5995 object[var] = value;
5996 if (var == upper)
5997 goto loop_exit;
5998 var = var + 1;
5999 goto loop_entry;
6000 loop_exit:
6001
6002 We increment var _after_ the loop exit check because we might otherwise
6003 fail if upper == TYPE_MAX_VALUE (type for upper).
6004
6005 Note that we never have to deal with SAVE_EXPRs here, because this has
6006 already been taken care of for us, in gimplify_init_ctor_preeval(). */
6007
6008 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
6009 gimple_seq *, bool);
6010
6011 static void
6012 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
6013 tree value, tree array_elt_type,
6014 gimple_seq *pre_p, bool cleared)
6015 {
6016 tree loop_entry_label, loop_exit_label, fall_thru_label;
6017 tree var, var_type, cref, tmp;
6018
6019 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
6020 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
6021 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
6022
6023 /* Create and initialize the index variable. */
6024 var_type = TREE_TYPE (upper);
6025 var = create_tmp_var (var_type);
6026 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
6027
6028 /* Add the loop entry label. */
6029 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
6030
6031 /* Build the reference. */
6032 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6033 var, NULL_TREE, NULL_TREE);
6034
6035 /* If we are a constructor, just call gimplify_init_ctor_eval to do
6036 the store. Otherwise just assign value to the reference. */
6037
6038 if (TREE_CODE (value) == CONSTRUCTOR)
6039 /* NB we might have to call ourself recursively through
6040 gimplify_init_ctor_eval if the value is a constructor. */
6041 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6042 pre_p, cleared);
6043 else
6044 {
6045 if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
6046 != GS_ERROR)
6047 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
6048 }
6049
6050 /* We exit the loop when the index var is equal to the upper bound. */
6051 gimplify_seq_add_stmt (pre_p,
6052 gimple_build_cond (EQ_EXPR, var, upper,
6053 loop_exit_label, fall_thru_label));
6054
6055 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
6056
6057 /* Otherwise, increment the index var... */
6058 tmp = build2 (PLUS_EXPR, var_type, var,
6059 fold_convert (var_type, integer_one_node));
6060 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
6061
6062 /* ...and jump back to the loop entry. */
6063 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
6064
6065 /* Add the loop exit label. */
6066 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
6067 }
6068
6069 /* A subroutine of gimplify_init_constructor. Generate individual
6070 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
6071 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
6072 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
6073 zeroed first. */
6074
6075 static void
6076 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
6077 gimple_seq *pre_p, bool cleared)
6078 {
6079 tree array_elt_type = NULL;
6080 unsigned HOST_WIDE_INT ix;
6081 tree purpose, value;
6082
6083 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
6084 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
6085
6086 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
6087 {
6088 tree cref;
6089
6090 /* NULL values are created above for gimplification errors. */
6091 if (value == NULL)
6092 continue;
6093
6094 if (cleared && initializer_zerop (value))
6095 continue;
6096
6097 /* ??? Here's to hoping the front end fills in all of the indices,
6098 so we don't have to figure out what's missing ourselves. */
6099 gcc_assert (purpose);
6100
6101 /* Skip zero-sized fields, unless value has side-effects. This can
6102 happen with calls to functions returning a empty type, which
6103 we shouldn't discard. As a number of downstream passes don't
6104 expect sets of empty type fields, we rely on the gimplification of
6105 the MODIFY_EXPR we make below to drop the assignment statement. */
6106 if (!TREE_SIDE_EFFECTS (value)
6107 && TREE_CODE (purpose) == FIELD_DECL
6108 && is_empty_type (TREE_TYPE (purpose)))
6109 continue;
6110
6111 /* If we have a RANGE_EXPR, we have to build a loop to assign the
6112 whole range. */
6113 if (TREE_CODE (purpose) == RANGE_EXPR)
6114 {
6115 tree lower = TREE_OPERAND (purpose, 0);
6116 tree upper = TREE_OPERAND (purpose, 1);
6117
6118 /* If the lower bound is equal to upper, just treat it as if
6119 upper was the index. */
6120 if (simple_cst_equal (lower, upper))
6121 purpose = upper;
6122 else
6123 {
6124 gimplify_init_ctor_eval_range (object, lower, upper, value,
6125 array_elt_type, pre_p, cleared);
6126 continue;
6127 }
6128 }
6129
6130 if (array_elt_type)
6131 {
6132 /* Do not use bitsizetype for ARRAY_REF indices. */
6133 if (TYPE_DOMAIN (TREE_TYPE (object)))
6134 purpose
6135 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
6136 purpose);
6137 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6138 purpose, NULL_TREE, NULL_TREE);
6139 }
6140 else
6141 {
6142 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
6143 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
6144 unshare_expr (object), purpose, NULL_TREE);
6145 }
6146
6147 if (TREE_CODE (value) == CONSTRUCTOR
6148 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
6149 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6150 pre_p, cleared);
6151 else if (TREE_CODE (value) == RAW_DATA_CST)
6152 {
6153 if (RAW_DATA_LENGTH (value) <= 32)
6154 {
6155 for (unsigned int i = 0; i < (unsigned) RAW_DATA_LENGTH (value);
6156 ++i)
6157 if (!cleared || RAW_DATA_POINTER (value)[i])
6158 {
6159 if (i)
6160 {
6161 tree p
6162 = fold_build2 (PLUS_EXPR, TREE_TYPE (purpose),
6163 purpose,
6164 build_int_cst (TREE_TYPE (purpose),
6165 i));
6166 cref = build4 (ARRAY_REF, array_elt_type,
6167 unshare_expr (object), p, NULL_TREE,
6168 NULL_TREE);
6169 }
6170 tree init
6171 = build2 (INIT_EXPR, TREE_TYPE (cref), cref,
6172 build_int_cst (TREE_TYPE (value),
6173 RAW_DATA_UCHAR_ELT (value, i)));
6174 gimplify_and_add (init, pre_p);
6175 ggc_free (init);
6176 }
6177 }
6178 else
6179 {
6180 tree rtype = build_array_type_nelts (TREE_TYPE (value),
6181 RAW_DATA_LENGTH (value));
6182 tree rctor = build_constructor_single (rtype, bitsize_zero_node,
6183 value);
6184 tree addr = build_fold_addr_expr (cref);
6185 cref = build2 (MEM_REF, rtype, addr,
6186 build_int_cst (ptr_type_node, 0));
6187 rctor = tree_output_constant_def (rctor);
6188 if (!useless_type_conversion_p (rtype, TREE_TYPE (rctor)))
6189 rctor = build1 (VIEW_CONVERT_EXPR, rtype, rctor);
6190 if (gimplify_expr (&cref, pre_p, NULL, is_gimple_lvalue,
6191 fb_lvalue) != GS_ERROR)
6192 gimplify_seq_add_stmt (pre_p,
6193 gimple_build_assign (cref, rctor));
6194 }
6195 }
6196 else
6197 {
6198 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
6199 gimplify_and_add (init, pre_p);
6200 ggc_free (init);
6201 }
6202 }
6203 }
6204
6205 /* Return the appropriate RHS predicate for this LHS. */
6206
6207 gimple_predicate
6208 rhs_predicate_for (tree lhs)
6209 {
6210 if (is_gimple_reg (lhs))
6211 return is_gimple_reg_rhs_or_call;
6212 else
6213 return is_gimple_mem_rhs_or_call;
6214 }
6215
6216 /* Return the initial guess for an appropriate RHS predicate for this LHS,
6217 before the LHS has been gimplified. */
6218
6219 static gimple_predicate
6220 initial_rhs_predicate_for (tree lhs)
6221 {
6222 if (is_gimple_reg_type (TREE_TYPE (lhs)))
6223 return is_gimple_reg_rhs_or_call;
6224 else
6225 return is_gimple_mem_rhs_or_call;
6226 }
6227
6228 /* Gimplify a C99 compound literal expression. This just means adding
6229 the DECL_EXPR before the current statement and using its anonymous
6230 decl instead. */
6231
6232 static enum gimplify_status
6233 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
6234 bool (*gimple_test_f) (tree),
6235 fallback_t fallback)
6236 {
6237 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
6238 tree decl = DECL_EXPR_DECL (decl_s);
6239 tree init = DECL_INITIAL (decl);
6240 /* Mark the decl as addressable if the compound literal
6241 expression is addressable now, otherwise it is marked too late
6242 after we gimplify the initialization expression. */
6243 if (TREE_ADDRESSABLE (*expr_p))
6244 TREE_ADDRESSABLE (decl) = 1;
6245 /* Otherwise, if we don't need an lvalue and have a literal directly
6246 substitute it. Check if it matches the gimple predicate, as
6247 otherwise we'd generate a new temporary, and we can as well just
6248 use the decl we already have. */
6249 else if (!TREE_ADDRESSABLE (decl)
6250 && !TREE_THIS_VOLATILE (decl)
6251 && init
6252 && (fallback & fb_lvalue) == 0
6253 && gimple_test_f (init))
6254 {
6255 *expr_p = init;
6256 return GS_OK;
6257 }
6258
6259 /* If the decl is not addressable, then it is being used in some
6260 expression or on the right hand side of a statement, and it can
6261 be put into a readonly data section. */
6262 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
6263 TREE_READONLY (decl) = 1;
6264
6265 /* This decl isn't mentioned in the enclosing block, so add it to the
6266 list of temps. FIXME it seems a bit of a kludge to say that
6267 anonymous artificial vars aren't pushed, but everything else is. */
6268 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
6269 gimple_add_tmp_var (decl);
6270
6271 gimplify_and_add (decl_s, pre_p);
6272 *expr_p = decl;
6273 return GS_OK;
6274 }
6275
6276 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
6277 return a new CONSTRUCTOR if something changed. */
6278
6279 static tree
6280 optimize_compound_literals_in_ctor (tree orig_ctor)
6281 {
6282 tree ctor = orig_ctor;
6283 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6284 unsigned int idx, num = vec_safe_length (elts);
6285
6286 for (idx = 0; idx < num; idx++)
6287 {
6288 tree value = (*elts)[idx].value;
6289 tree newval = value;
6290 if (TREE_CODE (value) == CONSTRUCTOR)
6291 newval = optimize_compound_literals_in_ctor (value);
6292 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6293 {
6294 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
6295 tree decl = DECL_EXPR_DECL (decl_s);
6296 tree init = DECL_INITIAL (decl);
6297
6298 if (!TREE_ADDRESSABLE (value)
6299 && !TREE_ADDRESSABLE (decl)
6300 && init
6301 && TREE_CODE (init) == CONSTRUCTOR)
6302 newval = optimize_compound_literals_in_ctor (init);
6303 }
6304 if (newval == value)
6305 continue;
6306
6307 if (ctor == orig_ctor)
6308 {
6309 ctor = copy_node (orig_ctor);
6310 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
6311 elts = CONSTRUCTOR_ELTS (ctor);
6312 }
6313 (*elts)[idx].value = newval;
6314 }
6315 return ctor;
6316 }
6317
6318 /* A subroutine of gimplify_modify_expr. Break out elements of a
6319 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
6320
6321 Note that we still need to clear any elements that don't have explicit
6322 initializers, so if not all elements are initialized we keep the
6323 original MODIFY_EXPR, we just remove all of the constructor elements.
6324
6325 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
6326 GS_ERROR if we would have to create a temporary when gimplifying
6327 this constructor. Otherwise, return GS_OK.
6328
6329 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
6330
6331 static enum gimplify_status
6332 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6333 bool want_value, bool notify_temp_creation)
6334 {
6335 tree object, ctor, type;
6336 enum gimplify_status ret;
6337 vec<constructor_elt, va_gc> *elts;
6338 bool cleared = false;
6339 bool is_empty_ctor = false;
6340 bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
6341
6342 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
6343
6344 if (!notify_temp_creation)
6345 {
6346 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6347 is_gimple_lvalue, fb_lvalue);
6348 if (ret == GS_ERROR)
6349 return ret;
6350 }
6351
6352 object = TREE_OPERAND (*expr_p, 0);
6353 ctor = TREE_OPERAND (*expr_p, 1)
6354 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
6355 type = TREE_TYPE (ctor);
6356 elts = CONSTRUCTOR_ELTS (ctor);
6357 ret = GS_ALL_DONE;
6358
6359 switch (TREE_CODE (type))
6360 {
6361 case RECORD_TYPE:
6362 case UNION_TYPE:
6363 case QUAL_UNION_TYPE:
6364 case ARRAY_TYPE:
6365 {
6366 /* Use readonly data for initializers of this or smaller size
6367 regardless of the num_nonzero_elements / num_unique_nonzero_elements
6368 ratio. */
6369 const HOST_WIDE_INT min_unique_size = 64;
6370 /* If num_nonzero_elements / num_unique_nonzero_elements ratio
6371 is smaller than this, use readonly data. */
6372 const int unique_nonzero_ratio = 8;
6373 /* True if a single access of the object must be ensured. This is the
6374 case if the target is volatile, the type is non-addressable and more
6375 than one field need to be assigned. */
6376 const bool ensure_single_access
6377 = TREE_THIS_VOLATILE (object)
6378 && !TREE_ADDRESSABLE (type)
6379 && vec_safe_length (elts) > 1;
6380 struct gimplify_init_ctor_preeval_data preeval_data;
6381 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
6382 HOST_WIDE_INT num_unique_nonzero_elements;
6383 int complete_p;
6384 bool valid_const_initializer;
6385
6386 /* Aggregate types must lower constructors to initialization of
6387 individual elements. The exception is that a CONSTRUCTOR node
6388 with no elements indicates zero-initialization of the whole. */
6389 if (vec_safe_is_empty (elts))
6390 {
6391 if (notify_temp_creation)
6392 return GS_OK;
6393
6394 /* The var will be initialized and so appear on lhs of
6395 assignment, it can't be TREE_READONLY anymore. */
6396 if (VAR_P (object))
6397 TREE_READONLY (object) = 0;
6398
6399 is_empty_ctor = true;
6400 break;
6401 }
6402
6403 /* Fetch information about the constructor to direct later processing.
6404 We might want to make static versions of it in various cases, and
6405 can only do so if it known to be a valid constant initializer. */
6406 valid_const_initializer
6407 = categorize_ctor_elements (ctor, &num_nonzero_elements,
6408 &num_unique_nonzero_elements,
6409 &num_ctor_elements, &complete_p);
6410
6411 /* If a const aggregate variable is being initialized, then it
6412 should never be a lose to promote the variable to be static. */
6413 if (valid_const_initializer
6414 && num_nonzero_elements > 1
6415 && TREE_READONLY (object)
6416 && VAR_P (object)
6417 && !DECL_REGISTER (object)
6418 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
6419 || DECL_MERGEABLE (object))
6420 /* For ctors that have many repeated nonzero elements
6421 represented through RANGE_EXPRs, prefer initializing
6422 those through runtime loops over copies of large amounts
6423 of data from readonly data section. */
6424 && (num_unique_nonzero_elements
6425 > num_nonzero_elements / unique_nonzero_ratio
6426 || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
6427 <= (unsigned HOST_WIDE_INT) min_unique_size)))
6428 {
6429 if (notify_temp_creation)
6430 return GS_ERROR;
6431
6432 DECL_INITIAL (object) = ctor;
6433 TREE_STATIC (object) = 1;
6434 if (!DECL_NAME (object) || DECL_NAMELESS (object))
6435 DECL_NAME (object) = create_tmp_var_name ("C");
6436 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
6437
6438 /* ??? C++ doesn't automatically append a .<number> to the
6439 assembler name, and even when it does, it looks at FE private
6440 data structures to figure out what that number should be,
6441 which are not set for this variable. I suppose this is
6442 important for local statics for inline functions, which aren't
6443 "local" in the object file sense. So in order to get a unique
6444 TU-local symbol, we must invoke the lhd version now. */
6445 lhd_set_decl_assembler_name (object);
6446
6447 *expr_p = NULL_TREE;
6448 break;
6449 }
6450
6451 /* The var will be initialized and so appear on lhs of
6452 assignment, it can't be TREE_READONLY anymore. */
6453 if (VAR_P (object) && !notify_temp_creation)
6454 TREE_READONLY (object) = 0;
6455
6456 /* If there are "lots" of initialized elements, even discounting
6457 those that are not address constants (and thus *must* be
6458 computed at runtime), then partition the constructor into
6459 constant and non-constant parts. Block copy the constant
6460 parts in, then generate code for the non-constant parts. */
6461 /* TODO. There's code in cp/typeck.cc to do this. */
6462
6463 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
6464 /* store_constructor will ignore the clearing of variable-sized
6465 objects. Initializers for such objects must explicitly set
6466 every field that needs to be set. */
6467 cleared = false;
6468 else if (!complete_p)
6469 /* If the constructor isn't complete, clear the whole object
6470 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
6471
6472 ??? This ought not to be needed. For any element not present
6473 in the initializer, we should simply set them to zero. Except
6474 we'd need to *find* the elements that are not present, and that
6475 requires trickery to avoid quadratic compile-time behavior in
6476 large cases or excessive memory use in small cases. */
6477 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
6478 else if (num_ctor_elements - num_nonzero_elements
6479 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
6480 && num_nonzero_elements < num_ctor_elements / 4)
6481 /* If there are "lots" of zeros, it's more efficient to clear
6482 the memory and then set the nonzero elements. */
6483 cleared = true;
6484 else if (ensure_single_access && num_nonzero_elements == 0)
6485 /* If a single access to the target must be ensured and all elements
6486 are zero, then it's optimal to clear whatever their number. */
6487 cleared = true;
6488 /* If the object is small enough to go in registers, and it's
6489 not required to be constructed in memory, clear it first.
6490 That will avoid wasting cycles preserving any padding bits
6491 that might be there, and if there aren't any, the compiler
6492 is smart enough to optimize the clearing out. */
6493 else if (complete_p <= 0
6494 && !TREE_ADDRESSABLE (ctor)
6495 && !TREE_THIS_VOLATILE (object)
6496 && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
6497 && optimize)
6498 cleared = true;
6499 else
6500 cleared = false;
6501
6502 /* If there are "lots" of initialized elements, and all of them
6503 are valid address constants, then the entire initializer can
6504 be dropped to memory, and then memcpy'd out. Don't do this
6505 for sparse arrays, though, as it's more efficient to follow
6506 the standard CONSTRUCTOR behavior of memset followed by
6507 individual element initialization. Also don't do this for small
6508 all-zero initializers (which aren't big enough to merit
6509 clearing), and don't try to make bitwise copies of
6510 TREE_ADDRESSABLE types. */
6511 if (valid_const_initializer
6512 && complete_p
6513 && !(cleared || num_nonzero_elements == 0)
6514 && !TREE_ADDRESSABLE (type))
6515 {
6516 HOST_WIDE_INT size = int_size_in_bytes (type);
6517 unsigned int align;
6518
6519 /* ??? We can still get unbounded array types, at least
6520 from the C++ front end. This seems wrong, but attempt
6521 to work around it for now. */
6522 if (size < 0)
6523 {
6524 size = int_size_in_bytes (TREE_TYPE (object));
6525 if (size >= 0)
6526 TREE_TYPE (ctor) = type = TREE_TYPE (object);
6527 }
6528
6529 /* Find the maximum alignment we can assume for the object. */
6530 /* ??? Make use of DECL_OFFSET_ALIGN. */
6531 if (DECL_P (object))
6532 align = DECL_ALIGN (object);
6533 else
6534 align = TYPE_ALIGN (type);
6535
6536 /* Do a block move either if the size is so small as to make
6537 each individual move a sub-unit move on average, or if it
6538 is so large as to make individual moves inefficient. */
6539 if (size > 0
6540 && num_nonzero_elements > 1
6541 /* For ctors that have many repeated nonzero elements
6542 represented through RANGE_EXPRs, prefer initializing
6543 those through runtime loops over copies of large amounts
6544 of data from readonly data section. */
6545 && (num_unique_nonzero_elements
6546 > num_nonzero_elements / unique_nonzero_ratio
6547 || size <= min_unique_size)
6548 && (size < num_nonzero_elements
6549 || !can_move_by_pieces (size, align)))
6550 {
6551 if (notify_temp_creation)
6552 return GS_ERROR;
6553
6554 walk_tree (&ctor, force_labels_r, NULL, NULL);
6555 ctor = tree_output_constant_def (ctor);
6556 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
6557 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
6558 TREE_OPERAND (*expr_p, 1) = ctor;
6559
6560 /* This is no longer an assignment of a CONSTRUCTOR, but
6561 we still may have processing to do on the LHS. So
6562 pretend we didn't do anything here to let that happen. */
6563 return GS_UNHANDLED;
6564 }
6565 }
6566
6567 /* If a single access to the target must be ensured and there are
6568 nonzero elements or the zero elements are not assigned en masse,
6569 initialize the target from a temporary. */
6570 if (ensure_single_access && (num_nonzero_elements > 0 || !cleared))
6571 {
6572 if (notify_temp_creation)
6573 return GS_ERROR;
6574
6575 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
6576 TREE_OPERAND (*expr_p, 0) = temp;
6577 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
6578 *expr_p,
6579 build2 (MODIFY_EXPR, void_type_node,
6580 object, temp));
6581 return GS_OK;
6582 }
6583
6584 if (notify_temp_creation)
6585 return GS_OK;
6586
6587 /* If there are nonzero elements and if needed, pre-evaluate to capture
6588 elements overlapping with the lhs into temporaries. We must do this
6589 before clearing to fetch the values before they are zeroed-out. */
6590 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
6591 {
6592 preeval_data.lhs_base_decl = get_base_address (object);
6593 if (!DECL_P (preeval_data.lhs_base_decl))
6594 preeval_data.lhs_base_decl = NULL;
6595 preeval_data.lhs_alias_set = get_alias_set (object);
6596
6597 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
6598 pre_p, post_p, &preeval_data);
6599 }
6600
6601 bool ctor_has_side_effects_p
6602 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
6603
6604 if (cleared)
6605 {
6606 /* Zap the CONSTRUCTOR element list, which simplifies this case.
6607 Note that we still have to gimplify, in order to handle the
6608 case of variable sized types. Avoid shared tree structures. */
6609 CONSTRUCTOR_ELTS (ctor) = NULL;
6610 TREE_SIDE_EFFECTS (ctor) = 0;
6611 object = unshare_expr (object);
6612 gimplify_stmt (expr_p, pre_p);
6613 }
6614
6615 /* If we have not block cleared the object, or if there are nonzero
6616 elements in the constructor, or if the constructor has side effects,
6617 add assignments to the individual scalar fields of the object. */
6618 if (!cleared
6619 || num_nonzero_elements > 0
6620 || ctor_has_side_effects_p)
6621 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
6622
6623 *expr_p = NULL_TREE;
6624 }
6625 break;
6626
6627 case COMPLEX_TYPE:
6628 {
6629 tree r, i;
6630
6631 if (notify_temp_creation)
6632 return GS_OK;
6633
6634 /* Extract the real and imaginary parts out of the ctor. */
6635 gcc_assert (elts->length () == 2);
6636 r = (*elts)[0].value;
6637 i = (*elts)[1].value;
6638 if (r == NULL || i == NULL)
6639 {
6640 tree zero = build_zero_cst (TREE_TYPE (type));
6641 if (r == NULL)
6642 r = zero;
6643 if (i == NULL)
6644 i = zero;
6645 }
6646
6647 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
6648 represent creation of a complex value. */
6649 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
6650 {
6651 ctor = build_complex (type, r, i);
6652 TREE_OPERAND (*expr_p, 1) = ctor;
6653 }
6654 else
6655 {
6656 ctor = build2 (COMPLEX_EXPR, type, r, i);
6657 TREE_OPERAND (*expr_p, 1) = ctor;
6658 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
6659 pre_p,
6660 post_p,
6661 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
6662 fb_rvalue);
6663 }
6664 }
6665 break;
6666
6667 case VECTOR_TYPE:
6668 {
6669 unsigned HOST_WIDE_INT ix;
6670 constructor_elt *ce;
6671
6672 if (notify_temp_creation)
6673 return GS_OK;
6674
6675 /* Vector types use CONSTRUCTOR all the way through gimple
6676 compilation as a general initializer. */
6677 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
6678 {
6679 enum gimplify_status tret;
6680 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
6681 fb_rvalue);
6682 if (tret == GS_ERROR)
6683 ret = GS_ERROR;
6684 else if (TREE_STATIC (ctor)
6685 && !initializer_constant_valid_p (ce->value,
6686 TREE_TYPE (ce->value)))
6687 TREE_STATIC (ctor) = 0;
6688 }
6689 recompute_constructor_flags (ctor);
6690
6691 /* Go ahead and simplify constant constructors to VECTOR_CST. */
6692 if (TREE_CONSTANT (ctor))
6693 {
6694 bool constant_p = true;
6695 tree value;
6696
6697 /* Even when ctor is constant, it might contain non-*_CST
6698 elements, such as addresses or trapping values like
6699 1.0/0.0 - 1.0/0.0. Such expressions don't belong
6700 in VECTOR_CST nodes. */
6701 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
6702 if (!CONSTANT_CLASS_P (value))
6703 {
6704 constant_p = false;
6705 break;
6706 }
6707
6708 if (constant_p)
6709 {
6710 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
6711 break;
6712 }
6713 }
6714
6715 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
6716 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
6717 }
6718 break;
6719
6720 default:
6721 /* So how did we get a CONSTRUCTOR for a scalar type? */
6722 gcc_unreachable ();
6723 }
6724
6725 if (ret == GS_ERROR)
6726 return GS_ERROR;
6727 /* If we have gimplified both sides of the initializer but have
6728 not emitted an assignment, do so now. */
6729 if (*expr_p
6730 /* If the type is an empty type, we don't need to emit the
6731 assignment. */
6732 && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
6733 {
6734 tree lhs = TREE_OPERAND (*expr_p, 0);
6735 tree rhs = TREE_OPERAND (*expr_p, 1);
6736 if (want_value && object == lhs)
6737 lhs = unshare_expr (lhs);
6738 gassign *init = gimple_build_assign (lhs, rhs);
6739 gimplify_seq_add_stmt (pre_p, init);
6740 }
6741 if (want_value)
6742 {
6743 *expr_p = object;
6744 ret = GS_OK;
6745 }
6746 else
6747 {
6748 *expr_p = NULL;
6749 ret = GS_ALL_DONE;
6750 }
6751
6752 /* If the user requests to initialize automatic variables, we
6753 should initialize paddings inside the variable. Add a call to
6754 __builtin_clear_pading (&object, 0, for_auto_init = true) to
6755 initialize paddings of object always to zero regardless of
6756 INIT_TYPE. Note, we will not insert this call if the aggregate
6757 variable has be completely cleared already or it's initialized
6758 with an empty constructor. We cannot insert this call if the
6759 variable is a gimple register since __builtin_clear_padding will take
6760 the address of the variable. As a result, if a long double/_Complex long
6761 double variable will be spilled into stack later, its padding cannot
6762 be cleared with __builtin_clear_padding. We should clear its padding
6763 when it is spilled into memory. */
6764 if (is_init_expr
6765 && !is_gimple_reg (object)
6766 && clear_padding_type_may_have_padding_p (type)
6767 && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
6768 || !AGGREGATE_TYPE_P (type))
6769 && is_var_need_auto_init (object))
6770 gimple_add_padding_init_for_auto_var (object, false, pre_p);
6771
6772 return ret;
6773 }
6774
6775 /* Given a pointer value OP0, return a simplified version of an
6776 indirection through OP0, or NULL_TREE if no simplification is
6777 possible. This may only be applied to a rhs of an expression.
6778 Note that the resulting type may be different from the type pointed
6779 to in the sense that it is still compatible from the langhooks
6780 point of view. */
6781
6782 static tree
6783 gimple_fold_indirect_ref_rhs (tree t)
6784 {
6785 return gimple_fold_indirect_ref (t);
6786 }
6787
6788 /* Subroutine of gimplify_modify_expr to do simplifications of
6789 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
6790 something changes. */
6791
6792 static enum gimplify_status
6793 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
6794 gimple_seq *pre_p, gimple_seq *post_p,
6795 bool want_value)
6796 {
6797 enum gimplify_status ret = GS_UNHANDLED;
6798 bool changed;
6799
6800 do
6801 {
6802 changed = false;
6803 switch (TREE_CODE (*from_p))
6804 {
6805 case VAR_DECL:
6806 /* If we're assigning from a read-only variable initialized with
6807 a constructor and not volatile, do the direct assignment from
6808 the constructor, but only if the target is not volatile either
6809 since this latter assignment might end up being done on a per
6810 field basis. However, if the target is volatile and the type
6811 is aggregate and non-addressable, gimplify_init_constructor
6812 knows that it needs to ensure a single access to the target
6813 and it will return GS_OK only in this case. */
6814 if (TREE_READONLY (*from_p)
6815 && DECL_INITIAL (*from_p)
6816 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
6817 && !TREE_THIS_VOLATILE (*from_p)
6818 && (!TREE_THIS_VOLATILE (*to_p)
6819 || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
6820 && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
6821 {
6822 tree old_from = *from_p;
6823 enum gimplify_status subret;
6824
6825 /* Move the constructor into the RHS. */
6826 *from_p = unshare_expr (DECL_INITIAL (*from_p));
6827
6828 /* Let's see if gimplify_init_constructor will need to put
6829 it in memory. */
6830 subret = gimplify_init_constructor (expr_p, NULL, NULL,
6831 false, true);
6832 if (subret == GS_ERROR)
6833 {
6834 /* If so, revert the change. */
6835 *from_p = old_from;
6836 }
6837 else
6838 {
6839 ret = GS_OK;
6840 changed = true;
6841 }
6842 }
6843 break;
6844 case INDIRECT_REF:
6845 if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
6846 /* If we have code like
6847
6848 *(const A*)(A*)&x
6849
6850 where the type of "x" is a (possibly cv-qualified variant
6851 of "A"), treat the entire expression as identical to "x".
6852 This kind of code arises in C++ when an object is bound
6853 to a const reference, and if "x" is a TARGET_EXPR we want
6854 to take advantage of the optimization below. But not if
6855 the type is TREE_ADDRESSABLE; then C++17 says that the
6856 TARGET_EXPR needs to be a temporary. */
6857 if (tree t
6858 = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
6859 {
6860 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
6861 if (TREE_THIS_VOLATILE (t) != volatile_p)
6862 {
6863 if (DECL_P (t))
6864 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
6865 build_fold_addr_expr (t));
6866 if (REFERENCE_CLASS_P (t))
6867 TREE_THIS_VOLATILE (t) = volatile_p;
6868 }
6869 *from_p = t;
6870 ret = GS_OK;
6871 changed = true;
6872 }
6873 break;
6874
6875 case TARGET_EXPR:
6876 {
6877 /* If we are initializing something from a TARGET_EXPR, strip the
6878 TARGET_EXPR and initialize it directly, if possible. This can't
6879 be done if the initializer is void, since that implies that the
6880 temporary is set in some non-trivial way.
6881
6882 ??? What about code that pulls out the temp and uses it
6883 elsewhere? I think that such code never uses the TARGET_EXPR as
6884 an initializer. If I'm wrong, we'll die because the temp won't
6885 have any RTL. In that case, I guess we'll need to replace
6886 references somehow. */
6887 tree init = TARGET_EXPR_INITIAL (*from_p);
6888
6889 if (init
6890 && (TREE_CODE (*expr_p) != MODIFY_EXPR
6891 || !TARGET_EXPR_NO_ELIDE (*from_p))
6892 && !VOID_TYPE_P (TREE_TYPE (init)))
6893 {
6894 *from_p = init;
6895 ret = GS_OK;
6896 changed = true;
6897 }
6898 }
6899 break;
6900
6901 case COMPOUND_EXPR:
6902 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
6903 caught. */
6904 gimplify_compound_expr (from_p, pre_p, true);
6905 ret = GS_OK;
6906 changed = true;
6907 break;
6908
6909 case CONSTRUCTOR:
6910 /* If we already made some changes, let the front end have a
6911 crack at this before we break it down. */
6912 if (ret != GS_UNHANDLED)
6913 break;
6914
6915 /* If we're initializing from a CONSTRUCTOR, break this into
6916 individual MODIFY_EXPRs. */
6917 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
6918 false);
6919 return ret;
6920
6921 case COND_EXPR:
6922 /* If we're assigning to a non-register type, push the assignment
6923 down into the branches. This is mandatory for ADDRESSABLE types,
6924 since we cannot generate temporaries for such, but it saves a
6925 copy in other cases as well.
6926 Also avoid an extra temporary and copy when assigning to
6927 a register. */
6928 if (!is_gimple_reg_type (TREE_TYPE (*from_p))
6929 || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
6930 {
6931 /* This code should mirror the code in gimplify_cond_expr. */
6932 enum tree_code code = TREE_CODE (*expr_p);
6933 tree cond = *from_p;
6934 tree result = *to_p;
6935
6936 ret = gimplify_expr (&result, pre_p, post_p,
6937 is_gimple_lvalue, fb_lvalue);
6938 if (ret != GS_ERROR)
6939 ret = GS_OK;
6940
6941 /* If we are going to write RESULT more than once, clear
6942 TREE_READONLY flag, otherwise we might incorrectly promote
6943 the variable to static const and initialize it at compile
6944 time in one of the branches. */
6945 if (VAR_P (result)
6946 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
6947 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6948 TREE_READONLY (result) = 0;
6949 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
6950 TREE_OPERAND (cond, 1)
6951 = build2 (code, void_type_node, result,
6952 TREE_OPERAND (cond, 1));
6953 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6954 TREE_OPERAND (cond, 2)
6955 = build2 (code, void_type_node, unshare_expr (result),
6956 TREE_OPERAND (cond, 2));
6957
6958 TREE_TYPE (cond) = void_type_node;
6959 recalculate_side_effects (cond);
6960
6961 if (want_value)
6962 {
6963 gimplify_and_add (cond, pre_p);
6964 *expr_p = unshare_expr (result);
6965 }
6966 else
6967 *expr_p = cond;
6968 return ret;
6969 }
6970 break;
6971
6972 case CALL_EXPR:
6973 /* For calls that return in memory, give *to_p as the CALL_EXPR's
6974 return slot so that we don't generate a temporary. */
6975 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
6976 && aggregate_value_p (*from_p, *from_p))
6977 {
6978 bool use_target;
6979
6980 if (!(rhs_predicate_for (*to_p))(*from_p))
6981 /* If we need a temporary, *to_p isn't accurate. */
6982 use_target = false;
6983 /* It's OK to use the return slot directly unless it's an NRV. */
6984 else if (TREE_CODE (*to_p) == RESULT_DECL
6985 && DECL_NAME (*to_p) == NULL_TREE
6986 && needs_to_live_in_memory (*to_p))
6987 use_target = true;
6988 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
6989 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
6990 /* Don't force regs into memory. */
6991 use_target = false;
6992 else if (TREE_CODE (*expr_p) == INIT_EXPR)
6993 /* It's OK to use the target directly if it's being
6994 initialized. */
6995 use_target = true;
6996 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
6997 != INTEGER_CST)
6998 /* Always use the target and thus RSO for variable-sized types.
6999 GIMPLE cannot deal with a variable-sized assignment
7000 embedded in a call statement. */
7001 use_target = true;
7002 else if (TREE_CODE (*to_p) != SSA_NAME
7003 && (!is_gimple_variable (*to_p)
7004 || needs_to_live_in_memory (*to_p)))
7005 /* Don't use the original target if it's already addressable;
7006 if its address escapes, and the called function uses the
7007 NRV optimization, a conforming program could see *to_p
7008 change before the called function returns; see c++/19317.
7009 When optimizing, the return_slot pass marks more functions
7010 as safe after we have escape info. */
7011 use_target = false;
7012 else
7013 use_target = true;
7014
7015 if (use_target)
7016 {
7017 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
7018 mark_addressable (*to_p);
7019 }
7020 }
7021 break;
7022
7023 case WITH_SIZE_EXPR:
7024 /* Likewise for calls that return an aggregate of non-constant size,
7025 since we would not be able to generate a temporary at all. */
7026 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
7027 {
7028 *from_p = TREE_OPERAND (*from_p, 0);
7029 /* We don't change ret in this case because the
7030 WITH_SIZE_EXPR might have been added in
7031 gimplify_modify_expr, so returning GS_OK would lead to an
7032 infinite loop. */
7033 changed = true;
7034 }
7035 break;
7036
7037 /* If we're initializing from a container, push the initialization
7038 inside it. */
7039 case CLEANUP_POINT_EXPR:
7040 case BIND_EXPR:
7041 case STATEMENT_LIST:
7042 {
7043 tree wrap = *from_p;
7044 tree t;
7045
7046 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
7047 fb_lvalue);
7048 if (ret != GS_ERROR)
7049 ret = GS_OK;
7050
7051 t = voidify_wrapper_expr (wrap, *expr_p);
7052 gcc_assert (t == *expr_p);
7053
7054 if (want_value)
7055 {
7056 gimplify_and_add (wrap, pre_p);
7057 *expr_p = unshare_expr (*to_p);
7058 }
7059 else
7060 *expr_p = wrap;
7061 return GS_OK;
7062 }
7063
7064 case NOP_EXPR:
7065 /* Pull out compound literal expressions from a NOP_EXPR.
7066 Those are created in the C FE to drop qualifiers during
7067 lvalue conversion. */
7068 if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
7069 && tree_ssa_useless_type_conversion (*from_p))
7070 {
7071 *from_p = TREE_OPERAND (*from_p, 0);
7072 ret = GS_OK;
7073 changed = true;
7074 }
7075 break;
7076
7077 case COMPOUND_LITERAL_EXPR:
7078 {
7079 tree complit = TREE_OPERAND (*expr_p, 1);
7080 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
7081 tree decl = DECL_EXPR_DECL (decl_s);
7082 tree init = DECL_INITIAL (decl);
7083
7084 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
7085 into struct T x = { 0, 1, 2 } if the address of the
7086 compound literal has never been taken. */
7087 if (!TREE_ADDRESSABLE (complit)
7088 && !TREE_ADDRESSABLE (decl)
7089 && init)
7090 {
7091 *expr_p = copy_node (*expr_p);
7092 TREE_OPERAND (*expr_p, 1) = init;
7093 return GS_OK;
7094 }
7095 }
7096
7097 default:
7098 break;
7099 }
7100 }
7101 while (changed);
7102
7103 return ret;
7104 }
7105
7106
7107 /* Return true if T looks like a valid GIMPLE statement. */
7108
7109 static bool
7110 is_gimple_stmt (tree t)
7111 {
7112 const enum tree_code code = TREE_CODE (t);
7113
7114 switch (code)
7115 {
7116 case NOP_EXPR:
7117 /* The only valid NOP_EXPR is the empty statement. */
7118 return IS_EMPTY_STMT (t);
7119
7120 case BIND_EXPR:
7121 case COND_EXPR:
7122 /* These are only valid if they're void. */
7123 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
7124
7125 case SWITCH_EXPR:
7126 case GOTO_EXPR:
7127 case RETURN_EXPR:
7128 case LABEL_EXPR:
7129 case CASE_LABEL_EXPR:
7130 case TRY_CATCH_EXPR:
7131 case TRY_FINALLY_EXPR:
7132 case EH_FILTER_EXPR:
7133 case CATCH_EXPR:
7134 case ASM_EXPR:
7135 case STATEMENT_LIST:
7136 case OACC_PARALLEL:
7137 case OACC_KERNELS:
7138 case OACC_SERIAL:
7139 case OACC_DATA:
7140 case OACC_HOST_DATA:
7141 case OACC_DECLARE:
7142 case OACC_UPDATE:
7143 case OACC_ENTER_DATA:
7144 case OACC_EXIT_DATA:
7145 case OACC_CACHE:
7146 case OMP_PARALLEL:
7147 case OMP_FOR:
7148 case OMP_SIMD:
7149 case OMP_DISTRIBUTE:
7150 case OMP_LOOP:
7151 case OMP_TILE:
7152 case OMP_UNROLL:
7153 case OACC_LOOP:
7154 case OMP_SCAN:
7155 case OMP_SCOPE:
7156 case OMP_DISPATCH:
7157 case OMP_SECTIONS:
7158 case OMP_SECTION:
7159 case OMP_STRUCTURED_BLOCK:
7160 case OMP_SINGLE:
7161 case OMP_MASTER:
7162 case OMP_MASKED:
7163 case OMP_TASKGROUP:
7164 case OMP_ORDERED:
7165 case OMP_CRITICAL:
7166 case OMP_METADIRECTIVE:
7167 case OMP_TASK:
7168 case OMP_TARGET:
7169 case OMP_TARGET_DATA:
7170 case OMP_TARGET_UPDATE:
7171 case OMP_TARGET_ENTER_DATA:
7172 case OMP_TARGET_EXIT_DATA:
7173 case OMP_TASKLOOP:
7174 case OMP_TEAMS:
7175 /* These are always void. */
7176 return true;
7177
7178 case CALL_EXPR:
7179 case MODIFY_EXPR:
7180 case PREDICT_EXPR:
7181 /* These are valid regardless of their type. */
7182 return true;
7183
7184 default:
7185 return false;
7186 }
7187 }
7188
7189
7190 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
7191 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a gimple register.
7192
7193 IMPORTANT NOTE: This promotion is performed by introducing a load of the
7194 other, unmodified part of the complex object just before the total store.
7195 As a consequence, if the object is still uninitialized, an undefined value
7196 will be loaded into a register, which may result in a spurious exception
7197 if the register is floating-point and the value happens to be a signaling
7198 NaN for example. Then the fully-fledged complex operations lowering pass
7199 followed by a DCE pass are necessary in order to fix things up. */
7200
7201 static enum gimplify_status
7202 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
7203 bool want_value)
7204 {
7205 enum tree_code code, ocode;
7206 tree lhs, rhs, new_rhs, other, realpart, imagpart;
7207
7208 lhs = TREE_OPERAND (*expr_p, 0);
7209 rhs = TREE_OPERAND (*expr_p, 1);
7210 code = TREE_CODE (lhs);
7211 lhs = TREE_OPERAND (lhs, 0);
7212
7213 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
7214 other = build1 (ocode, TREE_TYPE (rhs), lhs);
7215 suppress_warning (other);
7216 other = get_formal_tmp_var (other, pre_p);
7217
7218 realpart = code == REALPART_EXPR ? rhs : other;
7219 imagpart = code == REALPART_EXPR ? other : rhs;
7220
7221 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
7222 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
7223 else
7224 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
7225
7226 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
7227 *expr_p = (want_value) ? rhs : NULL_TREE;
7228
7229 return GS_ALL_DONE;
7230 }
7231
7232 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
7233
7234 modify_expr
7235 : varname '=' rhs
7236 | '*' ID '=' rhs
7237
7238 PRE_P points to the list where side effects that must happen before
7239 *EXPR_P should be stored.
7240
7241 POST_P points to the list where side effects that must happen after
7242 *EXPR_P should be stored.
7243
7244 WANT_VALUE is nonzero iff we want to use the value of this expression
7245 in another expression. */
7246
7247 static enum gimplify_status
7248 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7249 bool want_value)
7250 {
7251 tree *from_p = &TREE_OPERAND (*expr_p, 1);
7252 tree *to_p = &TREE_OPERAND (*expr_p, 0);
7253 enum gimplify_status ret = GS_UNHANDLED;
7254 gimple *assign;
7255 location_t loc = EXPR_LOCATION (*expr_p);
7256 gimple_stmt_iterator gsi;
7257
7258 if (error_operand_p (*from_p) || error_operand_p (*to_p))
7259 return GS_ERROR;
7260
7261 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
7262 || TREE_CODE (*expr_p) == INIT_EXPR);
7263
7264 /* Trying to simplify a clobber using normal logic doesn't work,
7265 so handle it here. */
7266 if (TREE_CLOBBER_P (*from_p))
7267 {
7268 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7269 if (ret == GS_ERROR)
7270 return ret;
7271 gcc_assert (!want_value);
7272 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
7273 {
7274 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
7275 pre_p, post_p);
7276 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
7277 }
7278 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
7279 *expr_p = NULL;
7280 return GS_ALL_DONE;
7281 }
7282
7283 /* Convert initialization from an empty variable-size CONSTRUCTOR to
7284 memset. */
7285 if (TREE_TYPE (*from_p) != error_mark_node
7286 && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
7287 && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
7288 && TREE_CODE (*from_p) == CONSTRUCTOR
7289 && CONSTRUCTOR_NELTS (*from_p) == 0)
7290 {
7291 maybe_with_size_expr (from_p);
7292 gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR);
7293 return gimplify_modify_expr_to_memset (expr_p,
7294 TREE_OPERAND (*from_p, 1),
7295 want_value, pre_p);
7296 }
7297
7298 /* Insert pointer conversions required by the middle-end that are not
7299 required by the frontend. This fixes middle-end type checking for
7300 for example gcc.dg/redecl-6.c. */
7301 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
7302 {
7303 STRIP_USELESS_TYPE_CONVERSION (*from_p);
7304 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
7305 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
7306 }
7307
7308 /* See if any simplifications can be done based on what the RHS is. */
7309 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7310 want_value);
7311 if (ret != GS_UNHANDLED)
7312 return ret;
7313
7314 /* For empty types only gimplify the left hand side and right hand
7315 side as statements and throw away the assignment. Do this after
7316 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
7317 types properly. */
7318 if (is_empty_type (TREE_TYPE (*from_p))
7319 && !want_value
7320 /* Don't do this for calls that return addressable types, expand_call
7321 relies on those having a lhs. */
7322 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
7323 && TREE_CODE (*from_p) == CALL_EXPR))
7324 {
7325 gimplify_stmt (from_p, pre_p);
7326 gimplify_stmt (to_p, pre_p);
7327 *expr_p = NULL_TREE;
7328 return GS_ALL_DONE;
7329 }
7330
7331 /* If the value being copied is of variable width, compute the length
7332 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
7333 before gimplifying any of the operands so that we can resolve any
7334 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
7335 the size of the expression to be copied, not of the destination, so
7336 that is what we must do here. */
7337 maybe_with_size_expr (from_p);
7338
7339 /* As a special case, we have to temporarily allow for assignments
7340 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
7341 a toplevel statement, when gimplifying the GENERIC expression
7342 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
7343 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
7344
7345 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
7346 prevent gimplify_expr from trying to create a new temporary for
7347 foo's LHS, we tell it that it should only gimplify until it
7348 reaches the CALL_EXPR. On return from gimplify_expr, the newly
7349 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
7350 and all we need to do here is set 'a' to be its LHS. */
7351
7352 /* Gimplify the RHS first for C++17 and bug 71104. */
7353 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
7354 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
7355 if (ret == GS_ERROR)
7356 return ret;
7357
7358 /* Then gimplify the LHS. */
7359 /* If we gimplified the RHS to a CALL_EXPR and that call may return
7360 twice we have to make sure to gimplify into non-SSA as otherwise
7361 the abnormal edge added later will make those defs not dominate
7362 their uses.
7363 ??? Technically this applies only to the registers used in the
7364 resulting non-register *TO_P. */
7365 bool saved_into_ssa = gimplify_ctxp->into_ssa;
7366 if (saved_into_ssa
7367 && TREE_CODE (*from_p) == CALL_EXPR
7368 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
7369 gimplify_ctxp->into_ssa = false;
7370 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7371 gimplify_ctxp->into_ssa = saved_into_ssa;
7372 if (ret == GS_ERROR)
7373 return ret;
7374
7375 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
7376 guess for the predicate was wrong. */
7377 gimple_predicate final_pred = rhs_predicate_for (*to_p);
7378 if (final_pred != initial_pred)
7379 {
7380 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
7381 if (ret == GS_ERROR)
7382 return ret;
7383 }
7384
7385 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
7386 size as argument to the call. */
7387 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7388 {
7389 tree call = TREE_OPERAND (*from_p, 0);
7390 tree vlasize = TREE_OPERAND (*from_p, 1);
7391
7392 if (TREE_CODE (call) == CALL_EXPR
7393 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
7394 {
7395 int nargs = call_expr_nargs (call);
7396 tree type = TREE_TYPE (call);
7397 tree ap = CALL_EXPR_ARG (call, 0);
7398 tree tag = CALL_EXPR_ARG (call, 1);
7399 tree aptag = CALL_EXPR_ARG (call, 2);
7400 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
7401 IFN_VA_ARG, type,
7402 nargs + 1, ap, tag,
7403 aptag, vlasize);
7404 TREE_OPERAND (*from_p, 0) = newcall;
7405 }
7406 }
7407
7408 /* Now see if the above changed *from_p to something we handle specially. */
7409 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7410 want_value);
7411 if (ret != GS_UNHANDLED)
7412 return ret;
7413
7414 /* If we've got a variable sized assignment between two lvalues (i.e. does
7415 not involve a call), then we can make things a bit more straightforward
7416 by converting the assignment to memcpy or memset. */
7417 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7418 {
7419 tree from = TREE_OPERAND (*from_p, 0);
7420 tree size = TREE_OPERAND (*from_p, 1);
7421
7422 if (TREE_CODE (from) == CONSTRUCTOR)
7423 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
7424 else if (is_gimple_addressable (from)
7425 && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (*to_p)))
7426 && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))))
7427 {
7428 *from_p = from;
7429 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
7430 pre_p);
7431 }
7432 }
7433
7434 /* Transform partial stores to non-addressable complex variables into
7435 total stores. This allows us to use real instead of virtual operands
7436 for these variables, which improves optimization. */
7437 if ((TREE_CODE (*to_p) == REALPART_EXPR
7438 || TREE_CODE (*to_p) == IMAGPART_EXPR)
7439 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
7440 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
7441
7442 /* Try to alleviate the effects of the gimplification creating artificial
7443 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
7444 make sure not to create DECL_DEBUG_EXPR links across functions. */
7445 if (!gimplify_ctxp->into_ssa
7446 && VAR_P (*from_p)
7447 && DECL_IGNORED_P (*from_p)
7448 && DECL_P (*to_p)
7449 && !DECL_IGNORED_P (*to_p)
7450 && decl_function_context (*to_p) == current_function_decl
7451 && decl_function_context (*from_p) == current_function_decl)
7452 {
7453 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
7454 DECL_NAME (*from_p)
7455 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
7456 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
7457 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
7458 }
7459
7460 if (want_value && TREE_THIS_VOLATILE (*to_p))
7461 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
7462
7463 if (TREE_CODE (*from_p) == CALL_EXPR)
7464 {
7465 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
7466 instead of a GIMPLE_ASSIGN. */
7467 gcall *call_stmt;
7468 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
7469 {
7470 /* Gimplify internal functions created in the FEs. */
7471 int nargs = call_expr_nargs (*from_p), i;
7472 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
7473 auto_vec<tree> vargs (nargs);
7474
7475 for (i = 0; i < nargs; i++)
7476 {
7477 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
7478 EXPR_LOCATION (*from_p));
7479 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
7480 }
7481 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
7482 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
7483 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
7484 }
7485 else
7486 {
7487 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
7488 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
7489 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
7490 tree fndecl = get_callee_fndecl (*from_p);
7491 if (fndecl
7492 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
7493 && call_expr_nargs (*from_p) == 3)
7494 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
7495 CALL_EXPR_ARG (*from_p, 0),
7496 CALL_EXPR_ARG (*from_p, 1),
7497 CALL_EXPR_ARG (*from_p, 2));
7498 else
7499 {
7500 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
7501 }
7502 }
7503 notice_special_calls (call_stmt);
7504 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
7505 gimple_call_set_lhs (call_stmt, *to_p);
7506 else if (TREE_CODE (*to_p) == SSA_NAME)
7507 /* The above is somewhat premature, avoid ICEing later for a
7508 SSA name w/o a definition. We may have uses in the GIMPLE IL.
7509 ??? This doesn't make it a default-def. */
7510 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
7511
7512 assign = call_stmt;
7513 }
7514 else
7515 {
7516 assign = gimple_build_assign (*to_p, *from_p);
7517 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
7518 if (COMPARISON_CLASS_P (*from_p))
7519 copy_warning (assign, *from_p);
7520 }
7521
7522 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
7523 {
7524 /* We should have got an SSA name from the start. */
7525 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
7526 || ! gimple_in_ssa_p (cfun));
7527 }
7528
7529 gimplify_seq_add_stmt (pre_p, assign);
7530 gsi = gsi_last (*pre_p);
7531 maybe_fold_stmt (&gsi);
7532
7533 if (want_value)
7534 {
7535 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
7536 return GS_OK;
7537 }
7538 else
7539 *expr_p = NULL;
7540
7541 return GS_ALL_DONE;
7542 }
7543
7544 /* Gimplify a comparison between two variable-sized objects. Do this
7545 with a call to BUILT_IN_MEMCMP. */
7546
7547 static enum gimplify_status
7548 gimplify_variable_sized_compare (tree *expr_p)
7549 {
7550 location_t loc = EXPR_LOCATION (*expr_p);
7551 tree op0 = TREE_OPERAND (*expr_p, 0);
7552 tree op1 = TREE_OPERAND (*expr_p, 1);
7553 tree t, arg, dest, src, expr;
7554
7555 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
7556 arg = unshare_expr (arg);
7557 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
7558 src = build_fold_addr_expr_loc (loc, op1);
7559 dest = build_fold_addr_expr_loc (loc, op0);
7560 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
7561 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
7562
7563 expr
7564 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
7565 SET_EXPR_LOCATION (expr, loc);
7566 *expr_p = expr;
7567
7568 return GS_OK;
7569 }
7570
7571 /* Gimplify a comparison between two aggregate objects of integral scalar
7572 mode as a comparison between the bitwise equivalent scalar values. */
7573
7574 static enum gimplify_status
7575 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
7576 {
7577 const location_t loc = EXPR_LOCATION (*expr_p);
7578 const enum tree_code code = TREE_CODE (*expr_p);
7579 tree op0 = TREE_OPERAND (*expr_p, 0);
7580 tree op1 = TREE_OPERAND (*expr_p, 1);
7581 tree type = TREE_TYPE (op0);
7582 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
7583
7584 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
7585 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
7586
7587 /* We need to perform ordering comparisons in memory order like memcmp and,
7588 therefore, may need to byte-swap operands for little-endian targets. */
7589 if (code != EQ_EXPR && code != NE_EXPR)
7590 {
7591 gcc_assert (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN);
7592 gcc_assert (TREE_CODE (scalar_type) == INTEGER_TYPE);
7593 tree fndecl;
7594
7595 if (BYTES_BIG_ENDIAN)
7596 fndecl = NULL_TREE;
7597 else
7598 switch (int_size_in_bytes (scalar_type))
7599 {
7600 case 1:
7601 fndecl = NULL_TREE;
7602 break;
7603 case 2:
7604 fndecl = builtin_decl_implicit (BUILT_IN_BSWAP16);
7605 break;
7606 case 4:
7607 fndecl = builtin_decl_implicit (BUILT_IN_BSWAP32);
7608 break;
7609 case 8:
7610 fndecl = builtin_decl_implicit (BUILT_IN_BSWAP64);
7611 break;
7612 case 16:
7613 fndecl = builtin_decl_implicit (BUILT_IN_BSWAP128);
7614 break;
7615 default:
7616 gcc_unreachable ();
7617 }
7618
7619 if (fndecl)
7620 {
7621 op0 = build_call_expr_loc (loc, fndecl, 1, op0);
7622 op1 = build_call_expr_loc (loc, fndecl, 1, op1);
7623 }
7624 }
7625
7626 *expr_p = fold_build2_loc (loc, code, TREE_TYPE (*expr_p), op0, op1);
7627
7628 return GS_OK;
7629 }
7630
7631 /* Gimplify an expression sequence. This function gimplifies each
7632 expression and rewrites the original expression with the last
7633 expression of the sequence in GIMPLE form.
7634
7635 PRE_P points to the list where the side effects for all the
7636 expressions in the sequence will be emitted.
7637
7638 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
7639
7640 static enum gimplify_status
7641 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
7642 {
7643 tree t = *expr_p;
7644
7645 do
7646 {
7647 tree *sub_p = &TREE_OPERAND (t, 0);
7648
7649 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
7650 gimplify_compound_expr (sub_p, pre_p, false);
7651 else
7652 gimplify_stmt (sub_p, pre_p);
7653
7654 t = TREE_OPERAND (t, 1);
7655 }
7656 while (TREE_CODE (t) == COMPOUND_EXPR);
7657
7658 *expr_p = t;
7659 if (want_value)
7660 return GS_OK;
7661 else
7662 {
7663 gimplify_stmt (expr_p, pre_p);
7664 return GS_ALL_DONE;
7665 }
7666 }
7667
7668 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
7669 gimplify. After gimplification, EXPR_P will point to a new temporary
7670 that holds the original value of the SAVE_EXPR node.
7671
7672 PRE_P points to the list where side effects that must happen before
7673 *EXPR_P should be stored. */
7674
7675 static enum gimplify_status
7676 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7677 {
7678 enum gimplify_status ret = GS_ALL_DONE;
7679 tree val;
7680
7681 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
7682 val = TREE_OPERAND (*expr_p, 0);
7683
7684 if (val && TREE_TYPE (val) == error_mark_node)
7685 return GS_ERROR;
7686
7687 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
7688 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
7689 {
7690 /* The operand may be a void-valued expression. It is
7691 being executed only for its side-effects. */
7692 if (TREE_TYPE (val) == void_type_node)
7693 {
7694 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7695 is_gimple_stmt, fb_none);
7696 val = NULL;
7697 }
7698 else
7699 /* The temporary may not be an SSA name as later abnormal and EH
7700 control flow may invalidate use/def domination. When in SSA
7701 form then assume there are no such issues and SAVE_EXPRs only
7702 appear via GENERIC foldings. */
7703 val = get_initialized_tmp_var (val, pre_p, post_p,
7704 gimple_in_ssa_p (cfun));
7705
7706 TREE_OPERAND (*expr_p, 0) = val;
7707 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
7708 }
7709
7710 *expr_p = val;
7711
7712 return ret;
7713 }
7714
7715 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
7716
7717 unary_expr
7718 : ...
7719 | '&' varname
7720 ...
7721
7722 PRE_P points to the list where side effects that must happen before
7723 *EXPR_P should be stored.
7724
7725 POST_P points to the list where side effects that must happen after
7726 *EXPR_P should be stored. */
7727
7728 static enum gimplify_status
7729 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7730 {
7731 tree expr = *expr_p;
7732 tree op0 = TREE_OPERAND (expr, 0);
7733 enum gimplify_status ret;
7734 location_t loc = EXPR_LOCATION (*expr_p);
7735
7736 switch (TREE_CODE (op0))
7737 {
7738 case INDIRECT_REF:
7739 do_indirect_ref:
7740 /* Check if we are dealing with an expression of the form '&*ptr'.
7741 While the front end folds away '&*ptr' into 'ptr', these
7742 expressions may be generated internally by the compiler (e.g.,
7743 builtins like __builtin_va_end). */
7744 /* Caution: the silent array decomposition semantics we allow for
7745 ADDR_EXPR means we can't always discard the pair. */
7746 /* Gimplification of the ADDR_EXPR operand may drop
7747 cv-qualification conversions, so make sure we add them if
7748 needed. */
7749 {
7750 tree op00 = TREE_OPERAND (op0, 0);
7751 tree t_expr = TREE_TYPE (expr);
7752 tree t_op00 = TREE_TYPE (op00);
7753
7754 if (!useless_type_conversion_p (t_expr, t_op00))
7755 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
7756 *expr_p = op00;
7757 ret = GS_OK;
7758 }
7759 break;
7760
7761 case VIEW_CONVERT_EXPR:
7762 /* Take the address of our operand and then convert it to the type of
7763 this ADDR_EXPR.
7764
7765 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
7766 all clear. The impact of this transformation is even less clear. */
7767
7768 /* If the operand is a useless conversion, look through it. Doing so
7769 guarantees that the ADDR_EXPR and its operand will remain of the
7770 same type. */
7771 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
7772 op0 = TREE_OPERAND (op0, 0);
7773
7774 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
7775 build_fold_addr_expr_loc (loc,
7776 TREE_OPERAND (op0, 0)));
7777 ret = GS_OK;
7778 break;
7779
7780 case MEM_REF:
7781 if (integer_zerop (TREE_OPERAND (op0, 1)))
7782 goto do_indirect_ref;
7783
7784 /* fall through */
7785
7786 default:
7787 /* If we see a call to a declared builtin or see its address
7788 being taken (we can unify those cases here) then we can mark
7789 the builtin for implicit generation by GCC. */
7790 if (TREE_CODE (op0) == FUNCTION_DECL
7791 && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
7792 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
7793 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
7794
7795 /* We use fb_either here because the C frontend sometimes takes
7796 the address of a call that returns a struct; see
7797 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
7798 the implied temporary explicit. */
7799
7800 /* Make the operand addressable. */
7801 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
7802 is_gimple_addressable, fb_either);
7803 if (ret == GS_ERROR)
7804 break;
7805
7806 /* Then mark it. Beware that it may not be possible to do so directly
7807 if a temporary has been created by the gimplification. */
7808 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
7809
7810 op0 = TREE_OPERAND (expr, 0);
7811
7812 /* For various reasons, the gimplification of the expression
7813 may have made a new INDIRECT_REF. */
7814 if (INDIRECT_REF_P (op0)
7815 || (TREE_CODE (op0) == MEM_REF
7816 && integer_zerop (TREE_OPERAND (op0, 1))))
7817 goto do_indirect_ref;
7818
7819 mark_addressable (TREE_OPERAND (expr, 0));
7820
7821 /* The FEs may end up building ADDR_EXPRs early on a decl with
7822 an incomplete type. Re-build ADDR_EXPRs in canonical form
7823 here. */
7824 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
7825 *expr_p = build_fold_addr_expr (op0);
7826
7827 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
7828 if (TREE_CODE (*expr_p) == ADDR_EXPR)
7829 recompute_tree_invariant_for_addr_expr (*expr_p);
7830
7831 /* If we re-built the ADDR_EXPR add a conversion to the original type
7832 if required. */
7833 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
7834 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
7835
7836 break;
7837 }
7838
7839 return ret;
7840 }
7841
7842 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
7843 value; output operands should be a gimple lvalue. */
7844
7845 static enum gimplify_status
7846 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7847 {
7848 tree expr;
7849 int noutputs;
7850 const char **oconstraints;
7851 int i;
7852 tree link;
7853 const char *constraint;
7854 bool allows_mem, allows_reg, is_inout;
7855 enum gimplify_status ret, tret;
7856 gasm *stmt;
7857 vec<tree, va_gc> *inputs;
7858 vec<tree, va_gc> *outputs;
7859 vec<tree, va_gc> *clobbers;
7860 vec<tree, va_gc> *labels;
7861 tree link_next;
7862
7863 expr = *expr_p;
7864 noutputs = list_length (ASM_OUTPUTS (expr));
7865 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
7866
7867 inputs = NULL;
7868 outputs = NULL;
7869 clobbers = NULL;
7870 labels = NULL;
7871
7872 ret = GS_ALL_DONE;
7873 link_next = NULL_TREE;
7874 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
7875 {
7876 bool ok;
7877 size_t constraint_len;
7878
7879 link_next = TREE_CHAIN (link);
7880
7881 oconstraints[i]
7882 = constraint
7883 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7884 constraint_len = strlen (constraint);
7885 if (constraint_len == 0)
7886 continue;
7887
7888 ok = parse_output_constraint (&constraint, i, 0, 0,
7889 &allows_mem, &allows_reg, &is_inout);
7890 if (!ok)
7891 {
7892 ret = GS_ERROR;
7893 is_inout = false;
7894 }
7895
7896 /* If we can't make copies, we can only accept memory.
7897 Similarly for VLAs. */
7898 tree outtype = TREE_TYPE (TREE_VALUE (link));
7899 if (outtype != error_mark_node
7900 && (TREE_ADDRESSABLE (outtype)
7901 || !COMPLETE_TYPE_P (outtype)
7902 || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype))))
7903 {
7904 if (allows_mem)
7905 allows_reg = 0;
7906 else
7907 {
7908 error ("impossible constraint in %<asm%>");
7909 error ("non-memory output %d must stay in memory", i);
7910 return GS_ERROR;
7911 }
7912 }
7913
7914 if (!allows_reg && allows_mem)
7915 mark_addressable (TREE_VALUE (link));
7916
7917 tree orig = TREE_VALUE (link);
7918 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
7919 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7920 fb_lvalue | fb_mayfail);
7921 if (tret == GS_ERROR)
7922 {
7923 if (orig != error_mark_node)
7924 error ("invalid lvalue in %<asm%> output %d", i);
7925 ret = tret;
7926 }
7927
7928 /* If the gimplified operand is a register we do not allow memory. */
7929 if (allows_reg
7930 && allows_mem
7931 && (is_gimple_reg (TREE_VALUE (link))
7932 || (handled_component_p (TREE_VALUE (link))
7933 && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
7934 allows_mem = 0;
7935
7936 /* If the constraint does not allow memory make sure we gimplify
7937 it to a register if it is not already but its base is. This
7938 happens for complex and vector components. */
7939 if (!allows_mem)
7940 {
7941 tree op = TREE_VALUE (link);
7942 if (! is_gimple_val (op)
7943 && is_gimple_reg_type (TREE_TYPE (op))
7944 && is_gimple_reg (get_base_address (op)))
7945 {
7946 tree tem = create_tmp_reg (TREE_TYPE (op));
7947 tree ass;
7948 if (is_inout)
7949 {
7950 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
7951 tem, unshare_expr (op));
7952 gimplify_and_add (ass, pre_p);
7953 }
7954 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
7955 gimplify_and_add (ass, post_p);
7956
7957 TREE_VALUE (link) = tem;
7958 tret = GS_OK;
7959 }
7960 }
7961
7962 vec_safe_push (outputs, link);
7963 TREE_CHAIN (link) = NULL_TREE;
7964
7965 if (is_inout)
7966 {
7967 /* An input/output operand. To give the optimizers more
7968 flexibility, split it into separate input and output
7969 operands. */
7970 tree input;
7971 /* Buffer big enough to format a 32-bit UINT_MAX into. */
7972 char buf[11];
7973
7974 /* Turn the in/out constraint into an output constraint. */
7975 char *p = xstrdup (constraint);
7976 p[0] = '=';
7977 TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
7978 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
7979
7980 /* And add a matching input constraint. */
7981 if (allows_reg)
7982 {
7983 sprintf (buf, "%u", i);
7984
7985 /* If there are multiple alternatives in the constraint,
7986 handle each of them individually. Those that allow register
7987 will be replaced with operand number, the others will stay
7988 unchanged. */
7989 if (strchr (p, ',') != NULL)
7990 {
7991 size_t len = 0, buflen = strlen (buf);
7992 char *beg, *end, *str, *dst;
7993
7994 for (beg = p + 1;;)
7995 {
7996 end = strchr (beg, ',');
7997 if (end == NULL)
7998 end = strchr (beg, '\0');
7999 if ((size_t) (end - beg) < buflen)
8000 len += buflen + 1;
8001 else
8002 len += end - beg + 1;
8003 if (*end)
8004 beg = end + 1;
8005 else
8006 break;
8007 }
8008
8009 str = (char *) alloca (len);
8010 for (beg = p + 1, dst = str;;)
8011 {
8012 const char *tem;
8013 bool mem_p, reg_p, inout_p;
8014
8015 end = strchr (beg, ',');
8016 if (end)
8017 *end = '\0';
8018 beg[-1] = '=';
8019 tem = beg - 1;
8020 parse_output_constraint (&tem, i, 0, 0,
8021 &mem_p, &reg_p, &inout_p);
8022 if (dst != str)
8023 *dst++ = ',';
8024 if (reg_p)
8025 {
8026 memcpy (dst, buf, buflen);
8027 dst += buflen;
8028 }
8029 else
8030 {
8031 if (end)
8032 len = end - beg;
8033 else
8034 len = strlen (beg);
8035 memcpy (dst, beg, len);
8036 dst += len;
8037 }
8038 if (end)
8039 beg = end + 1;
8040 else
8041 break;
8042 }
8043 *dst = '\0';
8044 input = build_string (dst - str, str);
8045 }
8046 else
8047 input = build_string (strlen (buf), buf);
8048 }
8049 else
8050 input = build_string (constraint_len - 1, constraint + 1);
8051
8052 free (p);
8053
8054 input = build_tree_list (build_tree_list (NULL_TREE, input),
8055 unshare_expr (TREE_VALUE (link)));
8056 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
8057 }
8058 }
8059
8060 link_next = NULL_TREE;
8061 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
8062 {
8063 link_next = TREE_CHAIN (link);
8064 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8065 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8066 oconstraints, &allows_mem, &allows_reg);
8067
8068 /* If we can't make copies, we can only accept memory. */
8069 tree intype = TREE_TYPE (TREE_VALUE (link));
8070 if (intype != error_mark_node
8071 && (TREE_ADDRESSABLE (intype)
8072 || !COMPLETE_TYPE_P (intype)
8073 || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype))))
8074 {
8075 if (allows_mem)
8076 allows_reg = 0;
8077 else
8078 {
8079 error ("impossible constraint in %<asm%>");
8080 error ("non-memory input %d must stay in memory", i);
8081 return GS_ERROR;
8082 }
8083 }
8084
8085 /* If the operand is a memory input, it should be an lvalue. */
8086 if (!allows_reg && allows_mem)
8087 {
8088 tree inputv = TREE_VALUE (link);
8089 STRIP_NOPS (inputv);
8090 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
8091 || TREE_CODE (inputv) == PREINCREMENT_EXPR
8092 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
8093 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
8094 || TREE_CODE (inputv) == MODIFY_EXPR
8095 || VOID_TYPE_P (TREE_TYPE (inputv)))
8096 TREE_VALUE (link) = error_mark_node;
8097 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8098 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8099 if (tret != GS_ERROR)
8100 {
8101 /* Unlike output operands, memory inputs are not guaranteed
8102 to be lvalues by the FE, and while the expressions are
8103 marked addressable there, if it is e.g. a statement
8104 expression, temporaries in it might not end up being
8105 addressable. They might be already used in the IL and thus
8106 it is too late to make them addressable now though. */
8107 tree x = TREE_VALUE (link);
8108 while (handled_component_p (x))
8109 x = TREE_OPERAND (x, 0);
8110 if (TREE_CODE (x) == MEM_REF
8111 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
8112 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
8113 if ((VAR_P (x)
8114 || TREE_CODE (x) == PARM_DECL
8115 || TREE_CODE (x) == RESULT_DECL)
8116 && !TREE_ADDRESSABLE (x)
8117 && is_gimple_reg (x))
8118 {
8119 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
8120 input_location), 0,
8121 "memory input %d is not directly addressable",
8122 i);
8123 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
8124 }
8125 }
8126 mark_addressable (TREE_VALUE (link));
8127 if (tret == GS_ERROR)
8128 {
8129 if (inputv != error_mark_node)
8130 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
8131 "memory input %d is not directly addressable", i);
8132 ret = tret;
8133 }
8134 }
8135 else
8136 {
8137 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8138 is_gimple_asm_val, fb_rvalue);
8139 if (tret == GS_ERROR)
8140 ret = tret;
8141 }
8142
8143 TREE_CHAIN (link) = NULL_TREE;
8144 vec_safe_push (inputs, link);
8145 }
8146
8147 link_next = NULL_TREE;
8148 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
8149 {
8150 link_next = TREE_CHAIN (link);
8151 TREE_CHAIN (link) = NULL_TREE;
8152 vec_safe_push (clobbers, link);
8153 }
8154
8155 link_next = NULL_TREE;
8156 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
8157 {
8158 link_next = TREE_CHAIN (link);
8159 TREE_CHAIN (link) = NULL_TREE;
8160 vec_safe_push (labels, link);
8161 }
8162
8163 /* Do not add ASMs with errors to the gimple IL stream. */
8164 if (ret != GS_ERROR)
8165 {
8166 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
8167 inputs, outputs, clobbers, labels);
8168
8169 /* asm is volatile if it was marked by the user as volatile or
8170 there are no outputs or this is an asm goto. */
8171 gimple_asm_set_volatile (stmt,
8172 ASM_VOLATILE_P (expr)
8173 || noutputs == 0
8174 || labels);
8175 gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
8176 gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
8177
8178 gimplify_seq_add_stmt (pre_p, stmt);
8179 }
8180
8181 return ret;
8182 }
8183
8184 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
8185 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
8186 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
8187 return to this function.
8188
8189 FIXME should we complexify the prequeue handling instead? Or use flags
8190 for all the cleanups and let the optimizer tighten them up? The current
8191 code seems pretty fragile; it will break on a cleanup within any
8192 non-conditional nesting. But any such nesting would be broken, anyway;
8193 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
8194 and continues out of it. We can do that at the RTL level, though, so
8195 having an optimizer to tighten up try/finally regions would be a Good
8196 Thing. */
8197
8198 static enum gimplify_status
8199 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
8200 {
8201 gimple_stmt_iterator iter;
8202 gimple_seq body_sequence = NULL;
8203
8204 tree temp = voidify_wrapper_expr (*expr_p, NULL);
8205
8206 /* We only care about the number of conditions between the innermost
8207 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
8208 any cleanups collected outside the CLEANUP_POINT_EXPR. */
8209 int old_conds = gimplify_ctxp->conditions;
8210 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
8211 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
8212 gimplify_ctxp->conditions = 0;
8213 gimplify_ctxp->conditional_cleanups = NULL;
8214 gimplify_ctxp->in_cleanup_point_expr = true;
8215
8216 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
8217
8218 gimplify_ctxp->conditions = old_conds;
8219 gimplify_ctxp->conditional_cleanups = old_cleanups;
8220 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
8221
8222 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
8223 {
8224 gimple *wce = gsi_stmt (iter);
8225
8226 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
8227 {
8228 if (gsi_one_before_end_p (iter))
8229 {
8230 /* Note that gsi_insert_seq_before and gsi_remove do not
8231 scan operands, unlike some other sequence mutators. */
8232 if (!gimple_wce_cleanup_eh_only (wce))
8233 gsi_insert_seq_before_without_update (&iter,
8234 gimple_wce_cleanup (wce),
8235 GSI_SAME_STMT);
8236 gsi_remove (&iter, true);
8237 break;
8238 }
8239 else
8240 {
8241 gtry *gtry;
8242 gimple_seq seq;
8243 enum gimple_try_flags kind;
8244
8245 if (gimple_wce_cleanup_eh_only (wce))
8246 kind = GIMPLE_TRY_CATCH;
8247 else
8248 kind = GIMPLE_TRY_FINALLY;
8249 seq = gsi_split_seq_after (iter);
8250
8251 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
8252 /* Do not use gsi_replace here, as it may scan operands.
8253 We want to do a simple structural modification only. */
8254 gsi_set_stmt (&iter, gtry);
8255 iter = gsi_start (gtry->eval);
8256 }
8257 }
8258 else
8259 gsi_next (&iter);
8260 }
8261
8262 gimplify_seq_add_seq (pre_p, body_sequence);
8263 if (temp)
8264 {
8265 *expr_p = temp;
8266 return GS_OK;
8267 }
8268 else
8269 {
8270 *expr_p = NULL;
8271 return GS_ALL_DONE;
8272 }
8273 }
8274
8275 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
8276 is the cleanup action required. EH_ONLY is true if the cleanup should
8277 only be executed if an exception is thrown, not on normal exit.
8278 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
8279 only valid for clobbers. */
8280
8281 static void
8282 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
8283 bool force_uncond = false)
8284 {
8285 gimple *wce;
8286 gimple_seq cleanup_stmts = NULL;
8287
8288 /* Errors can result in improperly nested cleanups. Which results in
8289 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
8290 if (seen_error ())
8291 return;
8292
8293 if (gimple_conditional_context ())
8294 {
8295 /* If we're in a conditional context, this is more complex. We only
8296 want to run the cleanup if we actually ran the initialization that
8297 necessitates it, but we want to run it after the end of the
8298 conditional context. So we wrap the try/finally around the
8299 condition and use a flag to determine whether or not to actually
8300 run the destructor. Thus
8301
8302 test ? f(A()) : 0
8303
8304 becomes (approximately)
8305
8306 flag = 0;
8307 try {
8308 if (test) { A::A(temp); flag = 1; val = f(temp); }
8309 else { val = 0; }
8310 } finally {
8311 if (flag) A::~A(temp);
8312 }
8313 val
8314 */
8315 if (force_uncond)
8316 {
8317 gimplify_stmt (&cleanup, &cleanup_stmts);
8318 wce = gimple_build_wce (cleanup_stmts);
8319 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8320 }
8321 else
8322 {
8323 tree flag = create_tmp_var (boolean_type_node, "cleanup");
8324 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
8325 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
8326
8327 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
8328 gimplify_stmt (&cleanup, &cleanup_stmts);
8329 wce = gimple_build_wce (cleanup_stmts);
8330 gimple_wce_set_cleanup_eh_only (wce, eh_only);
8331
8332 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
8333 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8334 gimplify_seq_add_stmt (pre_p, ftrue);
8335
8336 /* Because of this manipulation, and the EH edges that jump
8337 threading cannot redirect, the temporary (VAR) will appear
8338 to be used uninitialized. Don't warn. */
8339 suppress_warning (var, OPT_Wuninitialized);
8340 }
8341 }
8342 else
8343 {
8344 gimplify_stmt (&cleanup, &cleanup_stmts);
8345 wce = gimple_build_wce (cleanup_stmts);
8346 gimple_wce_set_cleanup_eh_only (wce, eh_only);
8347 gimplify_seq_add_stmt (pre_p, wce);
8348 }
8349 }
8350
8351 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
8352
8353 static enum gimplify_status
8354 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8355 {
8356 tree targ = *expr_p;
8357 tree temp = TARGET_EXPR_SLOT (targ);
8358 tree init = TARGET_EXPR_INITIAL (targ);
8359 enum gimplify_status ret;
8360
8361 bool unpoison_empty_seq = false;
8362 gimple_stmt_iterator unpoison_it;
8363
8364 if (init)
8365 {
8366 gimple_seq init_pre_p = NULL;
8367
8368 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
8369 to the temps list. Handle also variable length TARGET_EXPRs. */
8370 if (!poly_int_tree_p (DECL_SIZE (temp)))
8371 {
8372 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
8373 gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
8374 /* FIXME: this is correct only when the size of the type does
8375 not depend on expressions evaluated in init. */
8376 gimplify_vla_decl (temp, &init_pre_p);
8377 }
8378 else
8379 {
8380 /* Save location where we need to place unpoisoning. It's possible
8381 that a variable will be converted to needs_to_live_in_memory. */
8382 unpoison_it = gsi_last (*pre_p);
8383 unpoison_empty_seq = gsi_end_p (unpoison_it);
8384
8385 gimple_add_tmp_var (temp);
8386 }
8387
8388 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
8389 expression is supposed to initialize the slot. */
8390 if (VOID_TYPE_P (TREE_TYPE (init)))
8391 ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8392 fb_none);
8393 else
8394 {
8395 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
8396 init = init_expr;
8397 ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8398 fb_none);
8399 init = NULL;
8400 ggc_free (init_expr);
8401 }
8402 if (ret == GS_ERROR)
8403 {
8404 /* PR c++/28266 Make sure this is expanded only once. */
8405 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8406 return GS_ERROR;
8407 }
8408
8409 if (init)
8410 gimplify_and_add (init, &init_pre_p);
8411
8412 /* Add a clobber for the temporary going out of scope, like
8413 gimplify_bind_expr. But only if we did not promote the
8414 temporary to static storage. */
8415 if (gimplify_ctxp->in_cleanup_point_expr
8416 && !TREE_STATIC (temp)
8417 && needs_to_live_in_memory (temp))
8418 {
8419 if (flag_stack_reuse == SR_ALL)
8420 {
8421 tree clobber = build_clobber (TREE_TYPE (temp),
8422 CLOBBER_STORAGE_END);
8423 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
8424 gimple_push_cleanup (temp, clobber, false, pre_p, true);
8425 }
8426 if (asan_poisoned_variables
8427 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
8428 && !TREE_STATIC (temp)
8429 && dbg_cnt (asan_use_after_scope)
8430 && !gimplify_omp_ctxp)
8431 {
8432 tree asan_cleanup = build_asan_poison_call_expr (temp);
8433 if (asan_cleanup)
8434 {
8435 if (unpoison_empty_seq)
8436 unpoison_it = gsi_start (*pre_p);
8437
8438 asan_poison_variable (temp, false, &unpoison_it,
8439 unpoison_empty_seq);
8440 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
8441 }
8442 }
8443 }
8444
8445 gimple_seq_add_seq (pre_p, init_pre_p);
8446
8447 /* If needed, push the cleanup for the temp. */
8448 if (TARGET_EXPR_CLEANUP (targ))
8449 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
8450 CLEANUP_EH_ONLY (targ), pre_p);
8451
8452 /* Only expand this once. */
8453 TREE_OPERAND (targ, 3) = init;
8454 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8455 }
8456 else
8457 /* We should have expanded this before. */
8458 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
8459
8460 *expr_p = temp;
8461 return GS_OK;
8462 }
8463
8464 /* Gimplification of expression trees. */
8465
8466 /* Gimplify an expression which appears at statement context. The
8467 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
8468 NULL, a new sequence is allocated.
8469
8470 Return true if we actually added a statement to the queue. */
8471
8472 bool
8473 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
8474 {
8475 gimple_seq_node last;
8476
8477 last = gimple_seq_last (*seq_p);
8478 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
8479 return last != gimple_seq_last (*seq_p);
8480 }
8481
8482 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
8483 to CTX. If entries already exist, force them to be some flavor of private.
8484 If there is no enclosing parallel, do nothing. */
8485
8486 void
8487 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
8488 {
8489 splay_tree_node n;
8490
8491 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
8492 return;
8493
8494 do
8495 {
8496 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8497 if (n != NULL)
8498 {
8499 if (n->value & GOVD_SHARED)
8500 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
8501 else if (n->value & GOVD_MAP)
8502 n->value |= GOVD_MAP_TO_ONLY;
8503 else
8504 return;
8505 }
8506 else if ((ctx->region_type & ORT_TARGET) != 0)
8507 {
8508 if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
8509 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8510 else
8511 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
8512 }
8513 else if (ctx->region_type != ORT_WORKSHARE
8514 && ctx->region_type != ORT_TASKGROUP
8515 && ctx->region_type != ORT_SIMD
8516 && ctx->region_type != ORT_ACC
8517 && !(ctx->region_type & ORT_TARGET_DATA))
8518 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8519
8520 ctx = ctx->outer_context;
8521 }
8522 while (ctx);
8523 }
8524
8525 /* Similarly for each of the type sizes of TYPE. */
8526
8527 static void
8528 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
8529 {
8530 if (type == NULL || type == error_mark_node)
8531 return;
8532 type = TYPE_MAIN_VARIANT (type);
8533
8534 if (ctx->privatized_types->add (type))
8535 return;
8536
8537 switch (TREE_CODE (type))
8538 {
8539 case INTEGER_TYPE:
8540 case ENUMERAL_TYPE:
8541 case BOOLEAN_TYPE:
8542 case REAL_TYPE:
8543 case FIXED_POINT_TYPE:
8544 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
8545 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
8546 break;
8547
8548 case ARRAY_TYPE:
8549 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8550 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
8551 break;
8552
8553 case RECORD_TYPE:
8554 case UNION_TYPE:
8555 case QUAL_UNION_TYPE:
8556 {
8557 tree field;
8558 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8559 if (TREE_CODE (field) == FIELD_DECL)
8560 {
8561 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
8562 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
8563 }
8564 }
8565 break;
8566
8567 case POINTER_TYPE:
8568 case REFERENCE_TYPE:
8569 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8570 break;
8571
8572 default:
8573 break;
8574 }
8575
8576 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
8577 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
8578 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
8579 }
8580
8581 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
8582
8583 static void
8584 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
8585 {
8586 splay_tree_node n;
8587 unsigned int nflags;
8588 tree t;
8589
8590 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
8591 return;
8592
8593 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
8594 there are constructors involved somewhere. Exception is a shared clause,
8595 there is nothing privatized in that case. */
8596 if ((flags & GOVD_SHARED) == 0
8597 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
8598 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
8599 flags |= GOVD_SEEN;
8600
8601 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8602 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8603 {
8604 /* We shouldn't be re-adding the decl with the same data
8605 sharing class. */
8606 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
8607 nflags = n->value | flags;
8608 /* The only combination of data sharing classes we should see is
8609 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
8610 reduction variables to be used in data sharing clauses. */
8611 gcc_assert ((ctx->region_type & ORT_ACC) != 0
8612 || ((nflags & GOVD_DATA_SHARE_CLASS)
8613 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
8614 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
8615 n->value = nflags;
8616 return;
8617 }
8618
8619 /* When adding a variable-sized variable, we have to handle all sorts
8620 of additional bits of data: the pointer replacement variable, and
8621 the parameters of the type. */
8622 if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
8623 {
8624 /* Add the pointer replacement variable as PRIVATE if the variable
8625 replacement is private, else FIRSTPRIVATE since we'll need the
8626 address of the original variable either for SHARED, or for the
8627 copy into or out of the context. */
8628 if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
8629 {
8630 if (flags & GOVD_MAP)
8631 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
8632 else if (flags & GOVD_PRIVATE)
8633 nflags = GOVD_PRIVATE;
8634 else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
8635 && (flags & GOVD_FIRSTPRIVATE))
8636 || (ctx->region_type == ORT_TARGET_DATA
8637 && (flags & GOVD_DATA_SHARE_CLASS) == 0))
8638 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
8639 else
8640 nflags = GOVD_FIRSTPRIVATE;
8641 nflags |= flags & GOVD_SEEN;
8642 t = DECL_VALUE_EXPR (decl);
8643 gcc_assert (INDIRECT_REF_P (t));
8644 t = TREE_OPERAND (t, 0);
8645 gcc_assert (DECL_P (t));
8646 omp_add_variable (ctx, t, nflags);
8647 }
8648
8649 /* Add all of the variable and type parameters (which should have
8650 been gimplified to a formal temporary) as FIRSTPRIVATE. */
8651 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
8652 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
8653 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8654
8655 /* The variable-sized variable itself is never SHARED, only some form
8656 of PRIVATE. The sharing would take place via the pointer variable
8657 which we remapped above. */
8658 if (flags & GOVD_SHARED)
8659 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
8660 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
8661
8662 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
8663 alloca statement we generate for the variable, so make sure it
8664 is available. This isn't automatically needed for the SHARED
8665 case, since we won't be allocating local storage then.
8666 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
8667 in this case omp_notice_variable will be called later
8668 on when it is gimplified. */
8669 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
8670 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
8671 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
8672 }
8673 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
8674 && omp_privatize_by_reference (decl))
8675 {
8676 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8677
8678 /* Similar to the direct variable sized case above, we'll need the
8679 size of references being privatized. */
8680 if ((flags & GOVD_SHARED) == 0)
8681 {
8682 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
8683 if (t && DECL_P (t))
8684 omp_notice_variable (ctx, t, true);
8685 }
8686 }
8687
8688 if (n != NULL)
8689 n->value |= flags;
8690 else
8691 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
8692
8693 /* For reductions clauses in OpenACC loop directives, by default create a
8694 copy clause on the enclosing parallel construct for carrying back the
8695 results. */
8696 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
8697 {
8698 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
8699 while (outer_ctx)
8700 {
8701 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
8702 if (n != NULL)
8703 {
8704 /* Ignore local variables and explicitly declared clauses. */
8705 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
8706 break;
8707 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
8708 {
8709 /* According to the OpenACC spec, such a reduction variable
8710 should already have a copy map on a kernels construct,
8711 verify that here. */
8712 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
8713 && (n->value & GOVD_MAP));
8714 }
8715 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8716 {
8717 /* Remove firstprivate and make it a copy map. */
8718 n->value &= ~GOVD_FIRSTPRIVATE;
8719 n->value |= GOVD_MAP;
8720 }
8721 }
8722 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8723 {
8724 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
8725 GOVD_MAP | GOVD_SEEN);
8726 break;
8727 }
8728 outer_ctx = outer_ctx->outer_context;
8729 }
8730 }
8731 }
8732
8733 /* Notice a threadprivate variable DECL used in OMP context CTX.
8734 This just prints out diagnostics about threadprivate variable uses
8735 in untied tasks. If DECL2 is non-NULL, prevent this warning
8736 on that variable. */
8737
8738 static bool
8739 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
8740 tree decl2)
8741 {
8742 splay_tree_node n;
8743 struct gimplify_omp_ctx *octx;
8744
8745 for (octx = ctx; octx; octx = octx->outer_context)
8746 if ((octx->region_type & ORT_TARGET) != 0
8747 || octx->order_concurrent)
8748 {
8749 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
8750 if (n == NULL)
8751 {
8752 if (octx->order_concurrent)
8753 {
8754 error ("threadprivate variable %qE used in a region with"
8755 " %<order(concurrent)%> clause", DECL_NAME (decl));
8756 inform (octx->location, "enclosing region");
8757 }
8758 else
8759 {
8760 error ("threadprivate variable %qE used in target region",
8761 DECL_NAME (decl));
8762 inform (octx->location, "enclosing target region");
8763 }
8764 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
8765 }
8766 if (decl2)
8767 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
8768 }
8769
8770 if (ctx->region_type != ORT_UNTIED_TASK)
8771 return false;
8772 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8773 if (n == NULL)
8774 {
8775 error ("threadprivate variable %qE used in untied task",
8776 DECL_NAME (decl));
8777 inform (ctx->location, "enclosing task");
8778 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
8779 }
8780 if (decl2)
8781 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
8782 return false;
8783 }
8784
8785 /* Return true if global var DECL is device resident. */
8786
8787 static bool
8788 device_resident_p (tree decl)
8789 {
8790 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
8791
8792 if (!attr)
8793 return false;
8794
8795 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
8796 {
8797 tree c = TREE_VALUE (t);
8798 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
8799 return true;
8800 }
8801
8802 return false;
8803 }
8804
8805 /* Return true if DECL has an ACC DECLARE attribute. */
8806
8807 static bool
8808 is_oacc_declared (tree decl)
8809 {
8810 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
8811 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
8812 return declared != NULL_TREE;
8813 }
8814
8815 /* Determine outer default flags for DECL mentioned in an OMP region
8816 but not declared in an enclosing clause.
8817
8818 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
8819 remapped firstprivate instead of shared. To some extent this is
8820 addressed in omp_firstprivatize_type_sizes, but not
8821 effectively. */
8822
8823 static unsigned
8824 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
8825 bool in_code, unsigned flags)
8826 {
8827 enum omp_clause_default_kind default_kind = ctx->default_kind;
8828 enum omp_clause_default_kind kind;
8829
8830 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
8831 if (ctx->region_type & ORT_TASK)
8832 {
8833 tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
8834
8835 /* The event-handle specified by a detach clause should always be firstprivate,
8836 regardless of the current default. */
8837 if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
8838 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
8839 }
8840 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8841 default_kind = kind;
8842 else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
8843 default_kind = OMP_CLAUSE_DEFAULT_SHARED;
8844 /* For C/C++ default({,first}private), variables with static storage duration
8845 declared in a namespace or global scope and referenced in construct
8846 must be explicitly specified, i.e. acts as default(none). */
8847 else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
8848 || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
8849 && VAR_P (decl)
8850 && is_global_var (decl)
8851 && (DECL_FILE_SCOPE_P (decl)
8852 || (DECL_CONTEXT (decl)
8853 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
8854 && !lang_GNU_Fortran ())
8855 default_kind = OMP_CLAUSE_DEFAULT_NONE;
8856
8857 switch (default_kind)
8858 {
8859 case OMP_CLAUSE_DEFAULT_NONE:
8860 {
8861 const char *rtype;
8862
8863 if (ctx->region_type & ORT_PARALLEL)
8864 rtype = "parallel";
8865 else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
8866 rtype = "taskloop";
8867 else if (ctx->region_type & ORT_TASK)
8868 rtype = "task";
8869 else if (ctx->region_type & ORT_TEAMS)
8870 rtype = "teams";
8871 else
8872 gcc_unreachable ();
8873
8874 error ("%qE not specified in enclosing %qs",
8875 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
8876 inform (ctx->location, "enclosing %qs", rtype);
8877 }
8878 /* FALLTHRU */
8879 case OMP_CLAUSE_DEFAULT_SHARED:
8880 flags |= GOVD_SHARED;
8881 break;
8882 case OMP_CLAUSE_DEFAULT_PRIVATE:
8883 flags |= GOVD_PRIVATE;
8884 break;
8885 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
8886 flags |= GOVD_FIRSTPRIVATE;
8887 break;
8888 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8889 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
8890 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
8891 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
8892 {
8893 omp_notice_variable (octx, decl, in_code);
8894 for (; octx; octx = octx->outer_context)
8895 {
8896 splay_tree_node n2;
8897
8898 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
8899 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
8900 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
8901 continue;
8902 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
8903 {
8904 flags |= GOVD_FIRSTPRIVATE;
8905 goto found_outer;
8906 }
8907 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
8908 {
8909 flags |= GOVD_SHARED;
8910 goto found_outer;
8911 }
8912 }
8913 }
8914
8915 if (TREE_CODE (decl) == PARM_DECL
8916 || (!is_global_var (decl)
8917 && DECL_CONTEXT (decl) == current_function_decl))
8918 flags |= GOVD_FIRSTPRIVATE;
8919 else
8920 flags |= GOVD_SHARED;
8921 found_outer:
8922 break;
8923
8924 default:
8925 gcc_unreachable ();
8926 }
8927
8928 return flags;
8929 }
8930
8931 /* Return string name for types of OpenACC constructs from ORT_* values. */
8932
8933 static const char *
8934 oacc_region_type_name (enum omp_region_type region_type)
8935 {
8936 switch (region_type)
8937 {
8938 case ORT_ACC_DATA:
8939 return "data";
8940 case ORT_ACC_PARALLEL:
8941 return "parallel";
8942 case ORT_ACC_KERNELS:
8943 return "kernels";
8944 case ORT_ACC_SERIAL:
8945 return "serial";
8946 default:
8947 gcc_unreachable ();
8948 }
8949 }
8950
8951 /* Determine outer default flags for DECL mentioned in an OACC region
8952 but not declared in an enclosing clause. */
8953
8954 static unsigned
8955 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
8956 {
8957 struct gimplify_omp_ctx *ctx_default = ctx;
8958 /* If no 'default' clause appears on this compute construct... */
8959 if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
8960 {
8961 /* ..., see if one appears on a lexically containing 'data'
8962 construct. */
8963 while ((ctx_default = ctx_default->outer_context))
8964 {
8965 if (ctx_default->region_type == ORT_ACC_DATA
8966 && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
8967 break;
8968 }
8969 /* If not, reset. */
8970 if (!ctx_default)
8971 ctx_default = ctx;
8972 }
8973
8974 bool on_device = false;
8975 bool is_private = false;
8976 bool declared = is_oacc_declared (decl);
8977 tree type = TREE_TYPE (decl);
8978
8979 if (omp_privatize_by_reference (decl))
8980 type = TREE_TYPE (type);
8981
8982 /* For Fortran COMMON blocks, only used variables in those blocks are
8983 transfered and remapped. The block itself will have a private clause to
8984 avoid transfering the data twice.
8985 The hook evaluates to false by default. For a variable in Fortran's COMMON
8986 or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
8987 the variables in such a COMMON/EQUIVALENCE block shall be privatized not
8988 the whole block. For C++ and Fortran, it can also be true under certain
8989 other conditions, if DECL_HAS_VALUE_EXPR. */
8990 if (RECORD_OR_UNION_TYPE_P (type))
8991 is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
8992
8993 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
8994 && is_global_var (decl)
8995 && device_resident_p (decl)
8996 && !is_private)
8997 {
8998 on_device = true;
8999 flags |= GOVD_MAP_TO_ONLY;
9000 }
9001
9002 switch (ctx->region_type)
9003 {
9004 case ORT_ACC_KERNELS:
9005 if (is_private)
9006 flags |= GOVD_FIRSTPRIVATE;
9007 else if (AGGREGATE_TYPE_P (type))
9008 {
9009 /* Aggregates default to 'present_or_copy', or 'present'. */
9010 if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9011 flags |= GOVD_MAP;
9012 else
9013 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9014 }
9015 else
9016 /* Scalars default to 'copy'. */
9017 flags |= GOVD_MAP | GOVD_MAP_FORCE;
9018
9019 break;
9020
9021 case ORT_ACC_PARALLEL:
9022 case ORT_ACC_SERIAL:
9023 if (is_private)
9024 flags |= GOVD_FIRSTPRIVATE;
9025 else if (on_device || declared)
9026 flags |= GOVD_MAP;
9027 else if (AGGREGATE_TYPE_P (type))
9028 {
9029 /* Aggregates default to 'present_or_copy', or 'present'. */
9030 if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9031 flags |= GOVD_MAP;
9032 else
9033 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9034 }
9035 else
9036 /* Scalars default to 'firstprivate'. */
9037 flags |= GOVD_FIRSTPRIVATE;
9038
9039 break;
9040
9041 default:
9042 gcc_unreachable ();
9043 }
9044
9045 if (DECL_ARTIFICIAL (decl))
9046 ; /* We can get compiler-generated decls, and should not complain
9047 about them. */
9048 else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
9049 {
9050 error ("%qE not specified in enclosing OpenACC %qs construct",
9051 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
9052 oacc_region_type_name (ctx->region_type));
9053 if (ctx_default != ctx)
9054 inform (ctx->location, "enclosing OpenACC %qs construct and",
9055 oacc_region_type_name (ctx->region_type));
9056 inform (ctx_default->location,
9057 "enclosing OpenACC %qs construct with %qs clause",
9058 oacc_region_type_name (ctx_default->region_type),
9059 "default(none)");
9060 }
9061 else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
9062 ; /* Handled above. */
9063 else
9064 gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
9065
9066 return flags;
9067 }
9068
9069 /* Record the fact that DECL was used within the OMP context CTX.
9070 IN_CODE is true when real code uses DECL, and false when we should
9071 merely emit default(none) errors. Return true if DECL is going to
9072 be remapped and thus DECL shouldn't be gimplified into its
9073 DECL_VALUE_EXPR (if any). */
9074
9075 static bool
9076 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
9077 {
9078 splay_tree_node n;
9079 unsigned flags = in_code ? GOVD_SEEN : 0;
9080 bool ret = false, shared;
9081
9082 if (error_operand_p (decl))
9083 return false;
9084
9085 if (DECL_ARTIFICIAL (decl))
9086 {
9087 tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
9088 if (attr)
9089 decl = TREE_VALUE (TREE_VALUE (attr));
9090 }
9091
9092 if (ctx->region_type == ORT_NONE)
9093 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
9094
9095 if (is_global_var (decl))
9096 {
9097 /* Threadprivate variables are predetermined. */
9098 if (DECL_THREAD_LOCAL_P (decl))
9099 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
9100
9101 if (DECL_HAS_VALUE_EXPR_P (decl))
9102 {
9103 if (ctx->region_type & ORT_ACC)
9104 /* For OpenACC, defer expansion of value to avoid transfering
9105 privatized common block data instead of im-/explicitly transfered
9106 variables which are in common blocks. */
9107 ;
9108 else
9109 {
9110 tree value = get_base_address (DECL_VALUE_EXPR (decl));
9111
9112 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
9113 return omp_notice_threadprivate_variable (ctx, decl, value);
9114 }
9115 }
9116
9117 if (gimplify_omp_ctxp->outer_context == NULL
9118 && VAR_P (decl)
9119 && oacc_get_fn_attrib (current_function_decl))
9120 {
9121 location_t loc = DECL_SOURCE_LOCATION (decl);
9122
9123 if (lookup_attribute ("omp declare target link",
9124 DECL_ATTRIBUTES (decl)))
9125 {
9126 error_at (loc,
9127 "%qE with %<link%> clause used in %<routine%> function",
9128 DECL_NAME (decl));
9129 return false;
9130 }
9131 else if (!lookup_attribute ("omp declare target",
9132 DECL_ATTRIBUTES (decl)))
9133 {
9134 error_at (loc,
9135 "%qE requires a %<declare%> directive for use "
9136 "in a %<routine%> function", DECL_NAME (decl));
9137 return false;
9138 }
9139 }
9140 }
9141
9142 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9143 if ((ctx->region_type & ORT_TARGET) != 0)
9144 {
9145 if (n == NULL)
9146 {
9147 unsigned nflags = flags;
9148 if ((ctx->region_type & ORT_ACC) == 0)
9149 {
9150 bool is_declare_target = false;
9151 if (is_global_var (decl)
9152 && varpool_node::get_create (decl)->offloadable)
9153 {
9154 struct gimplify_omp_ctx *octx;
9155 for (octx = ctx->outer_context;
9156 octx; octx = octx->outer_context)
9157 {
9158 n = splay_tree_lookup (octx->variables,
9159 (splay_tree_key)decl);
9160 if (n
9161 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
9162 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9163 break;
9164 }
9165 is_declare_target = octx == NULL;
9166 }
9167 if (!is_declare_target)
9168 {
9169 int gdmk;
9170 enum omp_clause_defaultmap_kind kind;
9171 if (lang_hooks.decls.omp_allocatable_p (decl))
9172 gdmk = GDMK_ALLOCATABLE;
9173 else if (lang_hooks.decls.omp_scalar_target_p (decl))
9174 gdmk = GDMK_SCALAR_TARGET;
9175 else if (lang_hooks.decls.omp_scalar_p (decl, false))
9176 gdmk = GDMK_SCALAR;
9177 else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9178 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9179 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9180 == POINTER_TYPE)))
9181 gdmk = GDMK_POINTER;
9182 else
9183 gdmk = GDMK_AGGREGATE;
9184 kind = lang_hooks.decls.omp_predetermined_mapping (decl);
9185 if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
9186 {
9187 if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
9188 nflags |= GOVD_FIRSTPRIVATE;
9189 else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
9190 nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
9191 else
9192 gcc_unreachable ();
9193 }
9194 else if (ctx->defaultmap[gdmk] == 0)
9195 {
9196 tree d = lang_hooks.decls.omp_report_decl (decl);
9197 error ("%qE not specified in enclosing %<target%>",
9198 DECL_NAME (d));
9199 inform (ctx->location, "enclosing %<target%>");
9200 }
9201 else if (ctx->defaultmap[gdmk]
9202 & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
9203 nflags |= ctx->defaultmap[gdmk];
9204 else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
9205 {
9206 gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9207 nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
9208 }
9209 else
9210 {
9211 gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9212 nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
9213 }
9214 }
9215 }
9216
9217 struct gimplify_omp_ctx *octx = ctx->outer_context;
9218 if ((ctx->region_type & ORT_ACC) && octx)
9219 {
9220 /* Look in outer OpenACC contexts, to see if there's a
9221 data attribute for this variable. */
9222 omp_notice_variable (octx, decl, in_code);
9223
9224 for (; octx; octx = octx->outer_context)
9225 {
9226 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
9227 break;
9228 splay_tree_node n2
9229 = splay_tree_lookup (octx->variables,
9230 (splay_tree_key) decl);
9231 if (n2)
9232 {
9233 if (octx->region_type == ORT_ACC_HOST_DATA)
9234 error ("variable %qE declared in enclosing "
9235 "%<host_data%> region", DECL_NAME (decl));
9236 nflags |= GOVD_MAP;
9237 if (octx->region_type == ORT_ACC_DATA
9238 && (n2->value & GOVD_MAP_0LEN_ARRAY))
9239 nflags |= GOVD_MAP_0LEN_ARRAY;
9240 goto found_outer;
9241 }
9242 }
9243 }
9244
9245 if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
9246 | GOVD_MAP_ALLOC_ONLY)) == flags)
9247 {
9248 tree type = TREE_TYPE (decl);
9249 location_t loc = DECL_SOURCE_LOCATION (decl);
9250
9251 if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9252 && omp_privatize_by_reference (decl))
9253 type = TREE_TYPE (type);
9254
9255 if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
9256 /* Check if TYPE can appear in a target region.
9257 verify_type_context has already issued an error if it
9258 can't. */
9259 nflags |= GOVD_MAP | GOVD_EXPLICIT;
9260 else if (!omp_mappable_type (type))
9261 {
9262 error ("%qD referenced in target region does not have "
9263 "a mappable type", decl);
9264 nflags |= GOVD_MAP | GOVD_EXPLICIT;
9265 }
9266 else
9267 {
9268 if ((ctx->region_type & ORT_ACC) != 0)
9269 nflags = oacc_default_clause (ctx, decl, flags);
9270 else
9271 nflags |= GOVD_MAP;
9272 }
9273 }
9274 found_outer:
9275 omp_add_variable (ctx, decl, nflags);
9276 if (ctx->region_type & ORT_ACC)
9277 /* For OpenACC, as remarked above, defer expansion. */
9278 shared = false;
9279 else
9280 shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9281 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9282 }
9283 else
9284 {
9285 if (ctx->region_type & ORT_ACC)
9286 /* For OpenACC, as remarked above, defer expansion. */
9287 shared = false;
9288 else
9289 shared = ((n->value | flags)
9290 & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9291 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9292 /* If nothing changed, there's nothing left to do. */
9293 if ((n->value & flags) == flags)
9294 return ret;
9295 flags |= n->value;
9296 n->value = flags;
9297 }
9298 goto do_outer;
9299 }
9300
9301 if (n == NULL)
9302 {
9303 if (ctx->region_type == ORT_WORKSHARE
9304 || ctx->region_type == ORT_TASKGROUP
9305 || ctx->region_type == ORT_SIMD
9306 || ctx->region_type == ORT_ACC
9307 || (ctx->region_type & ORT_TARGET_DATA) != 0)
9308 goto do_outer;
9309
9310 flags = omp_default_clause (ctx, decl, in_code, flags);
9311
9312 if ((flags & GOVD_PRIVATE)
9313 && lang_hooks.decls.omp_private_outer_ref (decl))
9314 flags |= GOVD_PRIVATE_OUTER_REF;
9315
9316 omp_add_variable (ctx, decl, flags);
9317
9318 shared = (flags & GOVD_SHARED) != 0;
9319 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9320 goto do_outer;
9321 }
9322
9323 /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
9324 lb, b or incr expressions, those shouldn't be turned into simd arrays. */
9325 if (ctx->region_type == ORT_SIMD
9326 && ctx->in_for_exprs
9327 && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
9328 == GOVD_PRIVATE))
9329 flags &= ~GOVD_SEEN;
9330
9331 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
9332 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
9333 && DECL_SIZE (decl))
9334 {
9335 tree size;
9336 if (!poly_int_tree_p (DECL_SIZE (decl)))
9337 {
9338 splay_tree_node n2;
9339 tree t = DECL_VALUE_EXPR (decl);
9340 gcc_assert (INDIRECT_REF_P (t));
9341 t = TREE_OPERAND (t, 0);
9342 gcc_assert (DECL_P (t));
9343 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9344 n2->value |= GOVD_SEEN;
9345 }
9346 else if (omp_privatize_by_reference (decl)
9347 && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
9348 && !poly_int_tree_p (size))
9349 {
9350 splay_tree_node n2;
9351 gcc_assert (DECL_P (size));
9352 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
9353 if (n2)
9354 omp_notice_variable (ctx, size, true);
9355 }
9356 }
9357
9358 if (ctx->region_type & ORT_ACC)
9359 /* For OpenACC, as remarked above, defer expansion. */
9360 shared = false;
9361 else
9362 shared = ((flags | n->value) & GOVD_SHARED) != 0;
9363 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9364
9365 /* If nothing changed, there's nothing left to do. */
9366 if ((n->value & flags) == flags)
9367 return ret;
9368 flags |= n->value;
9369 n->value = flags;
9370
9371 do_outer:
9372 /* If the variable is private in the current context, then we don't
9373 need to propagate anything to an outer context. */
9374 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
9375 return ret;
9376 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9377 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9378 return ret;
9379 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9380 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9381 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9382 return ret;
9383 if (ctx->outer_context
9384 && omp_notice_variable (ctx->outer_context, decl, in_code))
9385 return true;
9386 return ret;
9387 }
9388
9389 /* Verify that DECL is private within CTX. If there's specific information
9390 to the contrary in the innermost scope, generate an error. */
9391
9392 static bool
9393 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
9394 {
9395 splay_tree_node n;
9396
9397 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9398 if (n != NULL)
9399 {
9400 if (n->value & GOVD_SHARED)
9401 {
9402 if (ctx == gimplify_omp_ctxp)
9403 {
9404 if (simd)
9405 error ("iteration variable %qE is predetermined linear",
9406 DECL_NAME (decl));
9407 else
9408 error ("iteration variable %qE should be private",
9409 DECL_NAME (decl));
9410 n->value = GOVD_PRIVATE;
9411 return true;
9412 }
9413 else
9414 return false;
9415 }
9416 else if ((n->value & GOVD_EXPLICIT) != 0
9417 && (ctx == gimplify_omp_ctxp
9418 || (ctx->region_type == ORT_COMBINED_PARALLEL
9419 && gimplify_omp_ctxp->outer_context == ctx)))
9420 {
9421 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
9422 error ("iteration variable %qE should not be firstprivate",
9423 DECL_NAME (decl));
9424 else if ((n->value & GOVD_REDUCTION) != 0)
9425 error ("iteration variable %qE should not be reduction",
9426 DECL_NAME (decl));
9427 else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
9428 error ("iteration variable %qE should not be linear",
9429 DECL_NAME (decl));
9430 }
9431 return (ctx == gimplify_omp_ctxp
9432 || (ctx->region_type == ORT_COMBINED_PARALLEL
9433 && gimplify_omp_ctxp->outer_context == ctx));
9434 }
9435
9436 if (ctx->region_type != ORT_WORKSHARE
9437 && ctx->region_type != ORT_TASKGROUP
9438 && ctx->region_type != ORT_SIMD
9439 && ctx->region_type != ORT_ACC)
9440 return false;
9441 else if (ctx->outer_context)
9442 return omp_is_private (ctx->outer_context, decl, simd);
9443 return false;
9444 }
9445
9446 /* Return true if DECL is private within a parallel region
9447 that binds to the current construct's context or in parallel
9448 region's REDUCTION clause. */
9449
9450 static bool
9451 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
9452 {
9453 splay_tree_node n;
9454
9455 do
9456 {
9457 ctx = ctx->outer_context;
9458 if (ctx == NULL)
9459 {
9460 if (is_global_var (decl))
9461 return false;
9462
9463 /* References might be private, but might be shared too,
9464 when checking for copyprivate, assume they might be
9465 private, otherwise assume they might be shared. */
9466 if (copyprivate)
9467 return true;
9468
9469 if (omp_privatize_by_reference (decl))
9470 return false;
9471
9472 /* Treat C++ privatized non-static data members outside
9473 of the privatization the same. */
9474 if (omp_member_access_dummy_var (decl))
9475 return false;
9476
9477 return true;
9478 }
9479
9480 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9481
9482 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
9483 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
9484 {
9485 if ((ctx->region_type & ORT_TARGET_DATA) != 0
9486 || n == NULL
9487 || (n->value & GOVD_MAP) == 0)
9488 continue;
9489 return false;
9490 }
9491
9492 if (n != NULL)
9493 {
9494 if ((n->value & GOVD_LOCAL) != 0
9495 && omp_member_access_dummy_var (decl))
9496 return false;
9497 return (n->value & GOVD_SHARED) == 0;
9498 }
9499
9500 if (ctx->region_type == ORT_WORKSHARE
9501 || ctx->region_type == ORT_TASKGROUP
9502 || ctx->region_type == ORT_SIMD
9503 || ctx->region_type == ORT_ACC)
9504 continue;
9505
9506 break;
9507 }
9508 while (1);
9509 return false;
9510 }
9511
9512 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
9513
9514 static tree
9515 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
9516 {
9517 tree t = *tp;
9518
9519 /* If this node has been visited, unmark it and keep looking. */
9520 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
9521 return t;
9522
9523 if (IS_TYPE_OR_DECL_P (t))
9524 *walk_subtrees = 0;
9525 return NULL_TREE;
9526 }
9527
9528
9529 /* Gimplify the affinity clause but effectively ignore it.
9530 Generate:
9531 var = begin;
9532 if ((step > 1) ? var <= end : var > end)
9533 locatator_var_expr; */
9534
9535 static void
9536 gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
9537 {
9538 tree last_iter = NULL_TREE;
9539 tree last_bind = NULL_TREE;
9540 tree label = NULL_TREE;
9541 tree *last_body = NULL;
9542 for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9543 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
9544 {
9545 tree t = OMP_CLAUSE_DECL (c);
9546 if (TREE_CODE (t) == TREE_LIST
9547 && TREE_PURPOSE (t)
9548 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9549 {
9550 if (TREE_VALUE (t) == null_pointer_node)
9551 continue;
9552 if (TREE_PURPOSE (t) != last_iter)
9553 {
9554 if (last_bind)
9555 {
9556 append_to_statement_list (label, last_body);
9557 gimplify_and_add (last_bind, pre_p);
9558 last_bind = NULL_TREE;
9559 }
9560 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9561 {
9562 if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9563 is_gimple_val, fb_rvalue) == GS_ERROR
9564 || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9565 is_gimple_val, fb_rvalue) == GS_ERROR
9566 || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9567 is_gimple_val, fb_rvalue) == GS_ERROR
9568 || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9569 is_gimple_val, fb_rvalue)
9570 == GS_ERROR))
9571 return;
9572 }
9573 last_iter = TREE_PURPOSE (t);
9574 tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9575 last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
9576 NULL, block);
9577 last_body = &BIND_EXPR_BODY (last_bind);
9578 tree cond = NULL_TREE;
9579 location_t loc = OMP_CLAUSE_LOCATION (c);
9580 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9581 {
9582 tree var = TREE_VEC_ELT (it, 0);
9583 tree begin = TREE_VEC_ELT (it, 1);
9584 tree end = TREE_VEC_ELT (it, 2);
9585 tree step = TREE_VEC_ELT (it, 3);
9586 loc = DECL_SOURCE_LOCATION (var);
9587 tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9588 var, begin);
9589 append_to_statement_list_force (tem, last_body);
9590
9591 tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9592 step, build_zero_cst (TREE_TYPE (step)));
9593 tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
9594 var, end);
9595 tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9596 var, end);
9597 cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
9598 cond1, cond2, cond3);
9599 if (cond)
9600 cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
9601 boolean_type_node, cond, cond1);
9602 else
9603 cond = cond1;
9604 }
9605 tree cont_label = create_artificial_label (loc);
9606 label = build1 (LABEL_EXPR, void_type_node, cont_label);
9607 tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9608 void_node,
9609 build_and_jump (&cont_label));
9610 append_to_statement_list_force (tem, last_body);
9611 }
9612 if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
9613 {
9614 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
9615 last_body);
9616 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
9617 }
9618 if (error_operand_p (TREE_VALUE (t)))
9619 return;
9620 append_to_statement_list_force (TREE_VALUE (t), last_body);
9621 TREE_VALUE (t) = null_pointer_node;
9622 }
9623 else
9624 {
9625 if (last_bind)
9626 {
9627 append_to_statement_list (label, last_body);
9628 gimplify_and_add (last_bind, pre_p);
9629 last_bind = NULL_TREE;
9630 }
9631 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
9632 {
9633 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
9634 NULL, is_gimple_val, fb_rvalue);
9635 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
9636 }
9637 if (error_operand_p (OMP_CLAUSE_DECL (c)))
9638 return;
9639 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
9640 is_gimple_lvalue, fb_lvalue) == GS_ERROR)
9641 return;
9642 gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
9643 }
9644 }
9645 if (last_bind)
9646 {
9647 append_to_statement_list (label, last_body);
9648 gimplify_and_add (last_bind, pre_p);
9649 }
9650 return;
9651 }
9652
9653 /* If *LIST_P contains any OpenMP depend clauses with iterators,
9654 lower all the depend clauses by populating corresponding depend
9655 array. Returns 0 if there are no such depend clauses, or
9656 2 if all depend clauses should be removed, 1 otherwise. */
9657
9658 static int
9659 gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
9660 {
9661 tree c;
9662 gimple *g;
9663 size_t n[5] = { 0, 0, 0, 0, 0 };
9664 bool unused[5];
9665 tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
9666 tree last_iter = NULL_TREE, last_count = NULL_TREE;
9667 size_t i, j;
9668 location_t first_loc = UNKNOWN_LOCATION;
9669
9670 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9671 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
9672 {
9673 switch (OMP_CLAUSE_DEPEND_KIND (c))
9674 {
9675 case OMP_CLAUSE_DEPEND_IN:
9676 i = 2;
9677 break;
9678 case OMP_CLAUSE_DEPEND_OUT:
9679 case OMP_CLAUSE_DEPEND_INOUT:
9680 i = 0;
9681 break;
9682 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
9683 i = 1;
9684 break;
9685 case OMP_CLAUSE_DEPEND_DEPOBJ:
9686 i = 3;
9687 break;
9688 case OMP_CLAUSE_DEPEND_INOUTSET:
9689 i = 4;
9690 break;
9691 default:
9692 gcc_unreachable ();
9693 }
9694 tree t = OMP_CLAUSE_DECL (c);
9695 if (first_loc == UNKNOWN_LOCATION)
9696 first_loc = OMP_CLAUSE_LOCATION (c);
9697 if (TREE_CODE (t) == TREE_LIST
9698 && TREE_PURPOSE (t)
9699 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9700 {
9701 if (TREE_PURPOSE (t) != last_iter)
9702 {
9703 tree tcnt = size_one_node;
9704 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9705 {
9706 if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9707 is_gimple_val, fb_rvalue) == GS_ERROR
9708 || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9709 is_gimple_val, fb_rvalue) == GS_ERROR
9710 || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9711 is_gimple_val, fb_rvalue) == GS_ERROR
9712 || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9713 is_gimple_val, fb_rvalue)
9714 == GS_ERROR))
9715 return 2;
9716 tree var = TREE_VEC_ELT (it, 0);
9717 tree begin = TREE_VEC_ELT (it, 1);
9718 tree end = TREE_VEC_ELT (it, 2);
9719 tree step = TREE_VEC_ELT (it, 3);
9720 tree orig_step = TREE_VEC_ELT (it, 4);
9721 tree type = TREE_TYPE (var);
9722 tree stype = TREE_TYPE (step);
9723 location_t loc = DECL_SOURCE_LOCATION (var);
9724 tree endmbegin;
9725 /* Compute count for this iterator as
9726 orig_step > 0
9727 ? (begin < end ? (end - begin + (step - 1)) / step : 0)
9728 : (begin > end ? (end - begin + (step + 1)) / step : 0)
9729 and compute product of those for the entire depend
9730 clause. */
9731 if (POINTER_TYPE_P (type))
9732 endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR,
9733 stype, end, begin);
9734 else
9735 endmbegin = fold_build2_loc (loc, MINUS_EXPR, type,
9736 end, begin);
9737 tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype,
9738 step,
9739 build_int_cst (stype, 1));
9740 tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
9741 build_int_cst (stype, 1));
9742 tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
9743 unshare_expr (endmbegin),
9744 stepm1);
9745 pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
9746 pos, step);
9747 tree neg = fold_build2_loc (loc, PLUS_EXPR, stype,
9748 endmbegin, stepp1);
9749 if (TYPE_UNSIGNED (stype))
9750 {
9751 neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
9752 step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
9753 }
9754 neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
9755 neg, step);
9756 step = NULL_TREE;
9757 tree cond = fold_build2_loc (loc, LT_EXPR,
9758 boolean_type_node,
9759 begin, end);
9760 pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
9761 build_int_cst (stype, 0));
9762 cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
9763 end, begin);
9764 neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
9765 build_int_cst (stype, 0));
9766 tree osteptype = TREE_TYPE (orig_step);
9767 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9768 orig_step,
9769 build_int_cst (osteptype, 0));
9770 tree cnt = fold_build3_loc (loc, COND_EXPR, stype,
9771 cond, pos, neg);
9772 cnt = fold_convert_loc (loc, sizetype, cnt);
9773 if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
9774 fb_rvalue) == GS_ERROR)
9775 return 2;
9776 tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
9777 }
9778 if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val,
9779 fb_rvalue) == GS_ERROR)
9780 return 2;
9781 last_iter = TREE_PURPOSE (t);
9782 last_count = tcnt;
9783 }
9784 if (counts[i] == NULL_TREE)
9785 counts[i] = last_count;
9786 else
9787 counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
9788 PLUS_EXPR, counts[i], last_count);
9789 }
9790 else
9791 n[i]++;
9792 }
9793 for (i = 0; i < 5; i++)
9794 if (counts[i])
9795 break;
9796 if (i == 5)
9797 return 0;
9798
9799 tree total = size_zero_node;
9800 for (i = 0; i < 5; i++)
9801 {
9802 unused[i] = counts[i] == NULL_TREE && n[i] == 0;
9803 if (counts[i] == NULL_TREE)
9804 counts[i] = size_zero_node;
9805 if (n[i])
9806 counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
9807 if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
9808 fb_rvalue) == GS_ERROR)
9809 return 2;
9810 total = size_binop (PLUS_EXPR, total, counts[i]);
9811 }
9812
9813 if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
9814 == GS_ERROR)
9815 return 2;
9816 bool is_old = unused[1] && unused[3] && unused[4];
9817 tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
9818 size_int (is_old ? 1 : 4));
9819 if (!unused[4])
9820 totalpx = size_binop (PLUS_EXPR, totalpx,
9821 size_binop (MULT_EXPR, counts[4], size_int (2)));
9822 tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
9823 tree array = create_tmp_var_raw (type);
9824 TREE_ADDRESSABLE (array) = 1;
9825 if (!poly_int_tree_p (totalpx))
9826 {
9827 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
9828 gimplify_type_sizes (TREE_TYPE (array), pre_p);
9829 if (gimplify_omp_ctxp)
9830 {
9831 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9832 while (ctx
9833 && (ctx->region_type == ORT_WORKSHARE
9834 || ctx->region_type == ORT_TASKGROUP
9835 || ctx->region_type == ORT_SIMD
9836 || ctx->region_type == ORT_ACC))
9837 ctx = ctx->outer_context;
9838 if (ctx)
9839 omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
9840 }
9841 gimplify_vla_decl (array, pre_p);
9842 }
9843 else
9844 gimple_add_tmp_var (array);
9845 tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
9846 NULL_TREE);
9847 tree tem;
9848 if (!is_old)
9849 {
9850 tem = build2 (MODIFY_EXPR, void_type_node, r,
9851 build_int_cst (ptr_type_node, 0));
9852 gimplify_and_add (tem, pre_p);
9853 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
9854 NULL_TREE);
9855 }
9856 tem = build2 (MODIFY_EXPR, void_type_node, r,
9857 fold_convert (ptr_type_node, total));
9858 gimplify_and_add (tem, pre_p);
9859 for (i = 1; i < (is_old ? 2 : 4); i++)
9860 {
9861 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
9862 NULL_TREE, NULL_TREE);
9863 tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
9864 gimplify_and_add (tem, pre_p);
9865 }
9866
9867 tree cnts[6];
9868 for (j = 5; j; j--)
9869 if (!unused[j - 1])
9870 break;
9871 for (i = 0; i < 5; i++)
9872 {
9873 if (i && (i >= j || unused[i - 1]))
9874 {
9875 cnts[i] = cnts[i - 1];
9876 continue;
9877 }
9878 cnts[i] = create_tmp_var (sizetype);
9879 if (i == 0)
9880 g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
9881 else
9882 {
9883 tree t;
9884 if (is_old)
9885 t = size_binop (PLUS_EXPR, counts[0], size_int (2));
9886 else
9887 t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
9888 if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
9889 == GS_ERROR)
9890 return 2;
9891 g = gimple_build_assign (cnts[i], t);
9892 }
9893 gimple_seq_add_stmt (pre_p, g);
9894 }
9895 if (unused[4])
9896 cnts[5] = NULL_TREE;
9897 else
9898 {
9899 tree t = size_binop (PLUS_EXPR, total, size_int (5));
9900 cnts[5] = create_tmp_var (sizetype);
9901 g = gimple_build_assign (cnts[i], t);
9902 gimple_seq_add_stmt (pre_p, g);
9903 }
9904
9905 last_iter = NULL_TREE;
9906 tree last_bind = NULL_TREE;
9907 tree *last_body = NULL;
9908 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9909 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
9910 {
9911 switch (OMP_CLAUSE_DEPEND_KIND (c))
9912 {
9913 case OMP_CLAUSE_DEPEND_IN:
9914 i = 2;
9915 break;
9916 case OMP_CLAUSE_DEPEND_OUT:
9917 case OMP_CLAUSE_DEPEND_INOUT:
9918 i = 0;
9919 break;
9920 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
9921 i = 1;
9922 break;
9923 case OMP_CLAUSE_DEPEND_DEPOBJ:
9924 i = 3;
9925 break;
9926 case OMP_CLAUSE_DEPEND_INOUTSET:
9927 i = 4;
9928 break;
9929 default:
9930 gcc_unreachable ();
9931 }
9932 tree t = OMP_CLAUSE_DECL (c);
9933 if (TREE_CODE (t) == TREE_LIST
9934 && TREE_PURPOSE (t)
9935 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9936 {
9937 if (TREE_PURPOSE (t) != last_iter)
9938 {
9939 if (last_bind)
9940 gimplify_and_add (last_bind, pre_p);
9941 tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9942 last_bind = build3 (BIND_EXPR, void_type_node,
9943 BLOCK_VARS (block), NULL, block);
9944 TREE_SIDE_EFFECTS (last_bind) = 1;
9945 SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
9946 tree *p = &BIND_EXPR_BODY (last_bind);
9947 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9948 {
9949 tree var = TREE_VEC_ELT (it, 0);
9950 tree begin = TREE_VEC_ELT (it, 1);
9951 tree end = TREE_VEC_ELT (it, 2);
9952 tree step = TREE_VEC_ELT (it, 3);
9953 tree orig_step = TREE_VEC_ELT (it, 4);
9954 tree type = TREE_TYPE (var);
9955 location_t loc = DECL_SOURCE_LOCATION (var);
9956 /* Emit:
9957 var = begin;
9958 goto cond_label;
9959 beg_label:
9960 ...
9961 var = var + step;
9962 cond_label:
9963 if (orig_step > 0) {
9964 if (var < end) goto beg_label;
9965 } else {
9966 if (var > end) goto beg_label;
9967 }
9968 for each iterator, with inner iterators added to
9969 the ... above. */
9970 tree beg_label = create_artificial_label (loc);
9971 tree cond_label = NULL_TREE;
9972 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9973 var, begin);
9974 append_to_statement_list_force (tem, p);
9975 tem = build_and_jump (&cond_label);
9976 append_to_statement_list_force (tem, p);
9977 tem = build1 (LABEL_EXPR, void_type_node, beg_label);
9978 append_to_statement_list (tem, p);
9979 tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9980 NULL_TREE, NULL_TREE);
9981 TREE_SIDE_EFFECTS (bind) = 1;
9982 SET_EXPR_LOCATION (bind, loc);
9983 append_to_statement_list_force (bind, p);
9984 if (POINTER_TYPE_P (type))
9985 tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
9986 var, fold_convert_loc (loc, sizetype,
9987 step));
9988 else
9989 tem = build2_loc (loc, PLUS_EXPR, type, var, step);
9990 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9991 var, tem);
9992 append_to_statement_list_force (tem, p);
9993 tem = build1 (LABEL_EXPR, void_type_node, cond_label);
9994 append_to_statement_list (tem, p);
9995 tree cond = fold_build2_loc (loc, LT_EXPR,
9996 boolean_type_node,
9997 var, end);
9998 tree pos
9999 = fold_build3_loc (loc, COND_EXPR, void_type_node,
10000 cond, build_and_jump (&beg_label),
10001 void_node);
10002 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
10003 var, end);
10004 tree neg
10005 = fold_build3_loc (loc, COND_EXPR, void_type_node,
10006 cond, build_and_jump (&beg_label),
10007 void_node);
10008 tree osteptype = TREE_TYPE (orig_step);
10009 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
10010 orig_step,
10011 build_int_cst (osteptype, 0));
10012 tem = fold_build3_loc (loc, COND_EXPR, void_type_node,
10013 cond, pos, neg);
10014 append_to_statement_list_force (tem, p);
10015 p = &BIND_EXPR_BODY (bind);
10016 }
10017 last_body = p;
10018 }
10019 last_iter = TREE_PURPOSE (t);
10020 if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
10021 {
10022 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
10023 0), last_body);
10024 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
10025 }
10026 if (error_operand_p (TREE_VALUE (t)))
10027 return 2;
10028 if (TREE_VALUE (t) != null_pointer_node)
10029 TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
10030 if (i == 4)
10031 {
10032 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10033 NULL_TREE, NULL_TREE);
10034 tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10035 NULL_TREE, NULL_TREE);
10036 r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10037 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10038 void_type_node, r, r2);
10039 append_to_statement_list_force (tem, last_body);
10040 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10041 void_type_node, cnts[i],
10042 size_binop (PLUS_EXPR, cnts[i],
10043 size_int (1)));
10044 append_to_statement_list_force (tem, last_body);
10045 i = 5;
10046 }
10047 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10048 NULL_TREE, NULL_TREE);
10049 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10050 void_type_node, r, TREE_VALUE (t));
10051 append_to_statement_list_force (tem, last_body);
10052 if (i == 5)
10053 {
10054 r = build4 (ARRAY_REF, ptr_type_node, array,
10055 size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10056 NULL_TREE, NULL_TREE);
10057 tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10058 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10059 void_type_node, r, tem);
10060 append_to_statement_list_force (tem, last_body);
10061 }
10062 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10063 void_type_node, cnts[i],
10064 size_binop (PLUS_EXPR, cnts[i],
10065 size_int (1 + (i == 5))));
10066 append_to_statement_list_force (tem, last_body);
10067 TREE_VALUE (t) = null_pointer_node;
10068 }
10069 else
10070 {
10071 if (last_bind)
10072 {
10073 gimplify_and_add (last_bind, pre_p);
10074 last_bind = NULL_TREE;
10075 }
10076 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
10077 {
10078 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
10079 NULL, is_gimple_val, fb_rvalue);
10080 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
10081 }
10082 if (error_operand_p (OMP_CLAUSE_DECL (c)))
10083 return 2;
10084 if (OMP_CLAUSE_DECL (c) != null_pointer_node)
10085 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
10086 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
10087 is_gimple_val, fb_rvalue) == GS_ERROR)
10088 return 2;
10089 if (i == 4)
10090 {
10091 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10092 NULL_TREE, NULL_TREE);
10093 tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10094 NULL_TREE, NULL_TREE);
10095 r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10096 tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
10097 gimplify_and_add (tem, pre_p);
10098 g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
10099 cnts[i],
10100 size_int (1)));
10101 gimple_seq_add_stmt (pre_p, g);
10102 i = 5;
10103 }
10104 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10105 NULL_TREE, NULL_TREE);
10106 tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
10107 gimplify_and_add (tem, pre_p);
10108 if (i == 5)
10109 {
10110 r = build4 (ARRAY_REF, ptr_type_node, array,
10111 size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10112 NULL_TREE, NULL_TREE);
10113 tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10114 tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
10115 append_to_statement_list_force (tem, last_body);
10116 gimplify_and_add (tem, pre_p);
10117 }
10118 g = gimple_build_assign (cnts[i],
10119 size_binop (PLUS_EXPR, cnts[i],
10120 size_int (1 + (i == 5))));
10121 gimple_seq_add_stmt (pre_p, g);
10122 }
10123 }
10124 if (last_bind)
10125 gimplify_and_add (last_bind, pre_p);
10126 tree cond = boolean_false_node;
10127 if (is_old)
10128 {
10129 if (!unused[0])
10130 cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
10131 size_binop_loc (first_loc, PLUS_EXPR, counts[0],
10132 size_int (2)));
10133 if (!unused[2])
10134 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10135 build2_loc (first_loc, NE_EXPR, boolean_type_node,
10136 cnts[2],
10137 size_binop_loc (first_loc, PLUS_EXPR,
10138 totalpx,
10139 size_int (1))));
10140 }
10141 else
10142 {
10143 tree prev = size_int (5);
10144 for (i = 0; i < 5; i++)
10145 {
10146 if (unused[i])
10147 continue;
10148 prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
10149 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10150 build2_loc (first_loc, NE_EXPR, boolean_type_node,
10151 cnts[i], unshare_expr (prev)));
10152 }
10153 }
10154 tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
10155 build_call_expr_loc (first_loc,
10156 builtin_decl_explicit (BUILT_IN_TRAP),
10157 0), void_node);
10158 gimplify_and_add (tem, pre_p);
10159 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
10160 OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
10161 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
10162 OMP_CLAUSE_CHAIN (c) = *list_p;
10163 *list_p = c;
10164 return 1;
10165 }
10166
10167 /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor. */
10168
10169 static bool
10170 omp_map_clause_descriptor_p (tree c)
10171 {
10172 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10173 return false;
10174
10175 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
10176 return true;
10177
10178 if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
10179 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
10180 && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
10181 return true;
10182
10183 return false;
10184 }
10185
10186 /* For a set of mappings describing an array section pointed to by a struct
10187 (or derived type, etc.) component, create an "alloc" or "release" node to
10188 insert into a list following a GOMP_MAP_STRUCT node. For some types of
10189 mapping (e.g. Fortran arrays with descriptors), an additional mapping may
10190 be created that is inserted into the list of mapping nodes attached to the
10191 directive being processed -- not part of the sorted list of nodes after
10192 GOMP_MAP_STRUCT.
10193
10194 CODE is the code of the directive being processed. GRP_START and GRP_END
10195 are the first and last of two or three nodes representing this array section
10196 mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
10197 GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER). EXTRA_NODE is
10198 filled with the additional node described above, if needed.
10199
10200 This function does not add the new nodes to any lists itself. It is the
10201 responsibility of the caller to do that. */
10202
10203 static tree
10204 build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
10205 tree *extra_node)
10206 {
10207 enum gomp_map_kind mkind
10208 = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
10209 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
10210
10211 gcc_assert (grp_start != grp_end);
10212
10213 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10214 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
10215 OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
10216 OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
10217 tree grp_mid = NULL_TREE;
10218 if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
10219 grp_mid = OMP_CLAUSE_CHAIN (grp_start);
10220
10221 if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
10222 OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
10223 else
10224 OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
10225
10226 if (grp_mid
10227 && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
10228 && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
10229 {
10230 tree c3
10231 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10232 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
10233 OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
10234 OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
10235 OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
10236
10237 *extra_node = c3;
10238 }
10239 else
10240 *extra_node = NULL_TREE;
10241
10242 return c2;
10243 }
10244
10245 /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
10246 and set *BITPOSP and *POFFSETP to the bit offset of the access.
10247 If BASE_REF is non-NULL and the containing object is a reference, set
10248 *BASE_REF to that reference before dereferencing the object.
10249 If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
10250 has array type, else return NULL. */
10251
10252 static tree
10253 extract_base_bit_offset (tree base, poly_int64 *bitposp,
10254 poly_offset_int *poffsetp,
10255 bool *variable_offset)
10256 {
10257 tree offset;
10258 poly_int64 bitsize, bitpos;
10259 machine_mode mode;
10260 int unsignedp, reversep, volatilep = 0;
10261 poly_offset_int poffset;
10262
10263 STRIP_NOPS (base);
10264
10265 base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
10266 &unsignedp, &reversep, &volatilep);
10267
10268 STRIP_NOPS (base);
10269
10270 if (offset && poly_int_tree_p (offset))
10271 {
10272 poffset = wi::to_poly_offset (offset);
10273 *variable_offset = false;
10274 }
10275 else
10276 {
10277 poffset = 0;
10278 *variable_offset = (offset != NULL_TREE);
10279 }
10280
10281 if (maybe_ne (bitpos, 0))
10282 poffset += bits_to_bytes_round_down (bitpos);
10283
10284 *bitposp = bitpos;
10285 *poffsetp = poffset;
10286
10287 return base;
10288 }
10289
10290 /* Used for topological sorting of mapping groups. UNVISITED means we haven't
10291 started processing the group yet. The TEMPORARY mark is used when we first
10292 encounter a group on a depth-first traversal, and the PERMANENT mark is used
10293 when we have processed all the group's children (i.e. all the base pointers
10294 referred to by the group's mapping nodes, recursively). */
10295
10296 enum omp_tsort_mark {
10297 UNVISITED,
10298 TEMPORARY,
10299 PERMANENT
10300 };
10301
10302 /* Hash for trees based on operand_equal_p. Like tree_operand_hash
10303 but ignores side effects in the equality comparisons. */
10304
10305 struct tree_operand_hash_no_se : tree_operand_hash
10306 {
10307 static inline bool equal (const value_type &,
10308 const compare_type &);
10309 };
10310
10311 inline bool
10312 tree_operand_hash_no_se::equal (const value_type &t1,
10313 const compare_type &t2)
10314 {
10315 return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
10316 }
10317
10318 /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
10319 clause. */
10320
10321 struct omp_mapping_group {
10322 tree *grp_start;
10323 tree grp_end;
10324 omp_tsort_mark mark;
10325 /* If we've removed the group but need to reindex, mark the group as
10326 deleted. */
10327 bool deleted;
10328 /* The group points to an already-created "GOMP_MAP_STRUCT
10329 GOMP_MAP_ATTACH_DETACH" pair. */
10330 bool reprocess_struct;
10331 /* The group should use "zero-length" allocations for pointers that are not
10332 mapped "to" on the same directive. */
10333 bool fragile;
10334 struct omp_mapping_group *sibling;
10335 struct omp_mapping_group *next;
10336 };
10337
10338 DEBUG_FUNCTION void
10339 debug_mapping_group (omp_mapping_group *grp)
10340 {
10341 tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
10342 OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
10343 debug_generic_expr (*grp->grp_start);
10344 OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
10345 }
10346
10347 /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
10348 isn't one. */
10349
10350 static tree
10351 omp_get_base_pointer (tree expr)
10352 {
10353 while (TREE_CODE (expr) == ARRAY_REF
10354 || TREE_CODE (expr) == COMPONENT_REF)
10355 expr = TREE_OPERAND (expr, 0);
10356
10357 if (INDIRECT_REF_P (expr)
10358 || (TREE_CODE (expr) == MEM_REF
10359 && integer_zerop (TREE_OPERAND (expr, 1))))
10360 {
10361 expr = TREE_OPERAND (expr, 0);
10362 while (TREE_CODE (expr) == COMPOUND_EXPR)
10363 expr = TREE_OPERAND (expr, 1);
10364 if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
10365 expr = TREE_OPERAND (expr, 0);
10366 if (TREE_CODE (expr) == SAVE_EXPR)
10367 expr = TREE_OPERAND (expr, 0);
10368 STRIP_NOPS (expr);
10369 return expr;
10370 }
10371
10372 return NULL_TREE;
10373 }
10374
10375 /* An attach or detach operation depends directly on the address being
10376 attached/detached. Return that address, or none if there are no
10377 attachments/detachments. */
10378
10379 static tree
10380 omp_get_attachment (omp_mapping_group *grp)
10381 {
10382 tree node = *grp->grp_start;
10383
10384 switch (OMP_CLAUSE_MAP_KIND (node))
10385 {
10386 case GOMP_MAP_TO:
10387 case GOMP_MAP_FROM:
10388 case GOMP_MAP_TOFROM:
10389 case GOMP_MAP_ALWAYS_FROM:
10390 case GOMP_MAP_ALWAYS_TO:
10391 case GOMP_MAP_ALWAYS_TOFROM:
10392 case GOMP_MAP_FORCE_FROM:
10393 case GOMP_MAP_FORCE_TO:
10394 case GOMP_MAP_FORCE_TOFROM:
10395 case GOMP_MAP_FORCE_PRESENT:
10396 case GOMP_MAP_PRESENT_ALLOC:
10397 case GOMP_MAP_PRESENT_FROM:
10398 case GOMP_MAP_PRESENT_TO:
10399 case GOMP_MAP_PRESENT_TOFROM:
10400 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10401 case GOMP_MAP_ALWAYS_PRESENT_TO:
10402 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10403 case GOMP_MAP_ALLOC:
10404 case GOMP_MAP_RELEASE:
10405 case GOMP_MAP_DELETE:
10406 case GOMP_MAP_FORCE_ALLOC:
10407 if (node == grp->grp_end)
10408 return NULL_TREE;
10409
10410 node = OMP_CLAUSE_CHAIN (node);
10411 if (node && omp_map_clause_descriptor_p (node))
10412 {
10413 gcc_assert (node != grp->grp_end);
10414 node = OMP_CLAUSE_CHAIN (node);
10415 }
10416 if (node)
10417 switch (OMP_CLAUSE_MAP_KIND (node))
10418 {
10419 case GOMP_MAP_POINTER:
10420 case GOMP_MAP_ALWAYS_POINTER:
10421 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10422 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10423 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10424 return NULL_TREE;
10425
10426 case GOMP_MAP_ATTACH_DETACH:
10427 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10428 case GOMP_MAP_DETACH:
10429 return OMP_CLAUSE_DECL (node);
10430
10431 default:
10432 internal_error ("unexpected mapping node");
10433 }
10434 return error_mark_node;
10435
10436 case GOMP_MAP_TO_PSET:
10437 gcc_assert (node != grp->grp_end);
10438 node = OMP_CLAUSE_CHAIN (node);
10439 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10440 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10441 return OMP_CLAUSE_DECL (node);
10442 else
10443 internal_error ("unexpected mapping node");
10444 return error_mark_node;
10445
10446 case GOMP_MAP_ATTACH:
10447 case GOMP_MAP_DETACH:
10448 node = OMP_CLAUSE_CHAIN (node);
10449 if (!node || *grp->grp_start == grp->grp_end)
10450 return OMP_CLAUSE_DECL (*grp->grp_start);
10451 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10452 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10453 return OMP_CLAUSE_DECL (*grp->grp_start);
10454 else
10455 internal_error ("unexpected mapping node");
10456 return error_mark_node;
10457
10458 case GOMP_MAP_STRUCT:
10459 case GOMP_MAP_STRUCT_UNORD:
10460 case GOMP_MAP_FORCE_DEVICEPTR:
10461 case GOMP_MAP_DEVICE_RESIDENT:
10462 case GOMP_MAP_LINK:
10463 case GOMP_MAP_IF_PRESENT:
10464 case GOMP_MAP_FIRSTPRIVATE:
10465 case GOMP_MAP_FIRSTPRIVATE_INT:
10466 case GOMP_MAP_USE_DEVICE_PTR:
10467 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10468 return NULL_TREE;
10469
10470 default:
10471 internal_error ("unexpected mapping node");
10472 }
10473
10474 return error_mark_node;
10475 }
10476
10477 /* Given a pointer START_P to the start of a group of related (e.g. pointer)
10478 mappings, return the chain pointer to the end of that group in the list. */
10479
10480 static tree *
10481 omp_group_last (tree *start_p)
10482 {
10483 tree c = *start_p, nc, *grp_last_p = start_p;
10484
10485 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
10486
10487 nc = OMP_CLAUSE_CHAIN (c);
10488
10489 if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
10490 return grp_last_p;
10491
10492 switch (OMP_CLAUSE_MAP_KIND (c))
10493 {
10494 default:
10495 while (nc
10496 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10497 && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10498 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10499 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
10500 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
10501 || (OMP_CLAUSE_MAP_KIND (nc)
10502 == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10503 || (OMP_CLAUSE_MAP_KIND (nc)
10504 == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
10505 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
10506 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
10507 || omp_map_clause_descriptor_p (nc)))
10508 {
10509 tree nc2 = OMP_CLAUSE_CHAIN (nc);
10510 if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
10511 {
10512 /* In the specific case we're doing "exit data" on an array
10513 slice of a reference-to-pointer struct component, we will see
10514 DETACH followed by ATTACH_DETACH here. We want to treat that
10515 as a single group. In other cases DETACH might represent a
10516 stand-alone "detach" clause, so we don't want to consider
10517 that part of the group. */
10518 if (nc2
10519 && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10520 && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
10521 goto consume_two_nodes;
10522 else
10523 break;
10524 }
10525 if (nc2
10526 && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10527 && (OMP_CLAUSE_MAP_KIND (nc)
10528 == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10529 && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
10530 {
10531 consume_two_nodes:
10532 grp_last_p = &OMP_CLAUSE_CHAIN (nc);
10533 c = nc2;
10534 nc = OMP_CLAUSE_CHAIN (nc2);
10535 }
10536 else
10537 {
10538 grp_last_p = &OMP_CLAUSE_CHAIN (c);
10539 c = nc;
10540 nc = nc2;
10541 }
10542 }
10543 break;
10544
10545 case GOMP_MAP_ATTACH:
10546 case GOMP_MAP_DETACH:
10547 /* This is a weird artifact of how directives are parsed: bare attach or
10548 detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
10549 FIRSTPRIVATE_REFERENCE node. FIXME. */
10550 if (nc
10551 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10552 && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10553 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
10554 grp_last_p = &OMP_CLAUSE_CHAIN (c);
10555 break;
10556
10557 case GOMP_MAP_TO_PSET:
10558 if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10559 && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
10560 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
10561 grp_last_p = &OMP_CLAUSE_CHAIN (c);
10562 break;
10563
10564 case GOMP_MAP_STRUCT:
10565 case GOMP_MAP_STRUCT_UNORD:
10566 {
10567 unsigned HOST_WIDE_INT num_mappings
10568 = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
10569 if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10570 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10571 || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
10572 grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
10573 for (unsigned i = 0; i < num_mappings; i++)
10574 grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
10575 }
10576 break;
10577 }
10578
10579 return grp_last_p;
10580 }
10581
10582 /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
10583 OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
10584 associated GOMP_MAP_POINTER mappings). Return a vector of omp_mapping_group
10585 if we have more than one such group, else return NULL. */
10586
10587 static void
10588 omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
10589 tree gather_sentinel)
10590 {
10591 for (tree *cp = list_p;
10592 *cp && *cp != gather_sentinel;
10593 cp = &OMP_CLAUSE_CHAIN (*cp))
10594 {
10595 if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
10596 continue;
10597
10598 tree *grp_last_p = omp_group_last (cp);
10599 omp_mapping_group grp;
10600
10601 grp.grp_start = cp;
10602 grp.grp_end = *grp_last_p;
10603 grp.mark = UNVISITED;
10604 grp.sibling = NULL;
10605 grp.deleted = false;
10606 grp.reprocess_struct = false;
10607 grp.fragile = false;
10608 grp.next = NULL;
10609 groups->safe_push (grp);
10610
10611 cp = grp_last_p;
10612 }
10613 }
10614
10615 static vec<omp_mapping_group> *
10616 omp_gather_mapping_groups (tree *list_p)
10617 {
10618 vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
10619
10620 omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
10621
10622 if (groups->length () > 0)
10623 return groups;
10624 else
10625 {
10626 delete groups;
10627 return NULL;
10628 }
10629 }
10630
10631 /* A pointer mapping group GRP may define a block of memory starting at some
10632 base address, and maybe also define a firstprivate pointer or firstprivate
10633 reference that points to that block. The return value is a node containing
10634 the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
10635 If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
10636 return the number of consecutive chained nodes in CHAINED. */
10637
10638 static tree
10639 omp_group_base (omp_mapping_group *grp, unsigned int *chained,
10640 tree *firstprivate)
10641 {
10642 tree node = *grp->grp_start;
10643
10644 *firstprivate = NULL_TREE;
10645 *chained = 1;
10646
10647 switch (OMP_CLAUSE_MAP_KIND (node))
10648 {
10649 case GOMP_MAP_TO:
10650 case GOMP_MAP_FROM:
10651 case GOMP_MAP_TOFROM:
10652 case GOMP_MAP_ALWAYS_FROM:
10653 case GOMP_MAP_ALWAYS_TO:
10654 case GOMP_MAP_ALWAYS_TOFROM:
10655 case GOMP_MAP_FORCE_FROM:
10656 case GOMP_MAP_FORCE_TO:
10657 case GOMP_MAP_FORCE_TOFROM:
10658 case GOMP_MAP_FORCE_PRESENT:
10659 case GOMP_MAP_PRESENT_ALLOC:
10660 case GOMP_MAP_PRESENT_FROM:
10661 case GOMP_MAP_PRESENT_TO:
10662 case GOMP_MAP_PRESENT_TOFROM:
10663 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10664 case GOMP_MAP_ALWAYS_PRESENT_TO:
10665 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10666 case GOMP_MAP_ALLOC:
10667 case GOMP_MAP_RELEASE:
10668 case GOMP_MAP_DELETE:
10669 case GOMP_MAP_FORCE_ALLOC:
10670 case GOMP_MAP_IF_PRESENT:
10671 if (node == grp->grp_end)
10672 return node;
10673
10674 node = OMP_CLAUSE_CHAIN (node);
10675 if (!node)
10676 internal_error ("unexpected mapping node");
10677 if (omp_map_clause_descriptor_p (node))
10678 {
10679 if (node == grp->grp_end)
10680 return *grp->grp_start;
10681 node = OMP_CLAUSE_CHAIN (node);
10682 }
10683 switch (OMP_CLAUSE_MAP_KIND (node))
10684 {
10685 case GOMP_MAP_POINTER:
10686 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10687 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10688 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10689 *firstprivate = OMP_CLAUSE_DECL (node);
10690 return *grp->grp_start;
10691
10692 case GOMP_MAP_ALWAYS_POINTER:
10693 case GOMP_MAP_ATTACH_DETACH:
10694 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10695 case GOMP_MAP_DETACH:
10696 return *grp->grp_start;
10697
10698 default:
10699 internal_error ("unexpected mapping node");
10700 }
10701 return error_mark_node;
10702
10703 case GOMP_MAP_TO_PSET:
10704 gcc_assert (node != grp->grp_end);
10705 node = OMP_CLAUSE_CHAIN (node);
10706 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10707 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10708 return NULL_TREE;
10709 else
10710 internal_error ("unexpected mapping node");
10711 return error_mark_node;
10712
10713 case GOMP_MAP_ATTACH:
10714 case GOMP_MAP_DETACH:
10715 node = OMP_CLAUSE_CHAIN (node);
10716 if (!node || *grp->grp_start == grp->grp_end)
10717 return NULL_TREE;
10718 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10719 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10720 {
10721 /* We're mapping the base pointer itself in a bare attach or detach
10722 node. This is a side effect of how parsing works, and the mapping
10723 will be removed anyway (at least for enter/exit data directives).
10724 We should ignore the mapping here. FIXME. */
10725 return NULL_TREE;
10726 }
10727 else
10728 internal_error ("unexpected mapping node");
10729 return error_mark_node;
10730
10731 case GOMP_MAP_STRUCT:
10732 case GOMP_MAP_STRUCT_UNORD:
10733 {
10734 unsigned HOST_WIDE_INT num_mappings
10735 = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
10736 node = OMP_CLAUSE_CHAIN (node);
10737 if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10738 || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10739 {
10740 *firstprivate = OMP_CLAUSE_DECL (node);
10741 node = OMP_CLAUSE_CHAIN (node);
10742 }
10743 else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
10744 node = OMP_CLAUSE_CHAIN (node);
10745 *chained = num_mappings;
10746 return node;
10747 }
10748
10749 case GOMP_MAP_FORCE_DEVICEPTR:
10750 case GOMP_MAP_DEVICE_RESIDENT:
10751 case GOMP_MAP_LINK:
10752 case GOMP_MAP_FIRSTPRIVATE:
10753 case GOMP_MAP_FIRSTPRIVATE_INT:
10754 case GOMP_MAP_USE_DEVICE_PTR:
10755 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10756 return NULL_TREE;
10757
10758 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10759 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10760 case GOMP_MAP_POINTER:
10761 case GOMP_MAP_ALWAYS_POINTER:
10762 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10763 /* These shouldn't appear by themselves. */
10764 if (!seen_error ())
10765 internal_error ("unexpected pointer mapping node");
10766 return error_mark_node;
10767
10768 default:
10769 gcc_unreachable ();
10770 }
10771
10772 return error_mark_node;
10773 }
10774
10775 /* Given a vector of omp_mapping_groups, build a hash table so we can look up
10776 nodes by tree_operand_hash_no_se. */
10777
10778 static void
10779 omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
10780 omp_mapping_group *> *grpmap,
10781 vec<omp_mapping_group> *groups,
10782 tree reindex_sentinel)
10783 {
10784 omp_mapping_group *grp;
10785 unsigned int i;
10786 bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
10787
10788 FOR_EACH_VEC_ELT (*groups, i, grp)
10789 {
10790 if (reindexing && *grp->grp_start == reindex_sentinel)
10791 above_hwm = true;
10792
10793 if (reindexing && !above_hwm)
10794 continue;
10795
10796 if (grp->reprocess_struct)
10797 continue;
10798
10799 tree fpp;
10800 unsigned int chained;
10801 tree node = omp_group_base (grp, &chained, &fpp);
10802
10803 if (node == error_mark_node || (!node && !fpp))
10804 continue;
10805
10806 for (unsigned j = 0;
10807 node && j < chained;
10808 node = OMP_CLAUSE_CHAIN (node), j++)
10809 {
10810 tree decl = OMP_CLAUSE_DECL (node);
10811 /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
10812 meaning node-hash lookups don't work. This is a workaround for
10813 that, but ideally we should just create the INDIRECT_REF at
10814 source instead. FIXME. */
10815 if (TREE_CODE (decl) == MEM_REF
10816 && integer_zerop (TREE_OPERAND (decl, 1)))
10817 decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
10818
10819 omp_mapping_group **prev = grpmap->get (decl);
10820
10821 if (prev && *prev == grp)
10822 /* Empty. */;
10823 else if (prev)
10824 {
10825 /* Mapping the same thing twice is normally diagnosed as an error,
10826 but can happen under some circumstances, e.g. in pr99928-16.c,
10827 the directive:
10828
10829 #pragma omp target simd reduction(+:a[:3]) \
10830 map(always, tofrom: a[:6])
10831 ...
10832
10833 will result in two "a[0]" mappings (of different sizes). */
10834
10835 grp->sibling = (*prev)->sibling;
10836 (*prev)->sibling = grp;
10837 }
10838 else
10839 grpmap->put (decl, grp);
10840 }
10841
10842 if (!fpp)
10843 continue;
10844
10845 omp_mapping_group **prev = grpmap->get (fpp);
10846 if (prev && *prev != grp)
10847 {
10848 grp->sibling = (*prev)->sibling;
10849 (*prev)->sibling = grp;
10850 }
10851 else
10852 grpmap->put (fpp, grp);
10853 }
10854 }
10855
10856 static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
10857 omp_index_mapping_groups (vec<omp_mapping_group> *groups)
10858 {
10859 hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
10860 = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
10861
10862 omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
10863
10864 return grpmap;
10865 }
10866
10867 /* Rebuild group map from partially-processed clause list (during
10868 omp_build_struct_sibling_lists). We have already processed nodes up until
10869 a high-water mark (HWM). This is a bit tricky because the list is being
10870 reordered as it is scanned, but we know:
10871
10872 1. The list after HWM has not been touched yet, so we can reindex it safely.
10873
10874 2. The list before and including HWM has been altered, but remains
10875 well-formed throughout the sibling-list building operation.
10876
10877 so, we can do the reindex operation in two parts, on the processed and
10878 then the unprocessed halves of the list. */
10879
10880 static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
10881 omp_reindex_mapping_groups (tree *list_p,
10882 vec<omp_mapping_group> *groups,
10883 vec<omp_mapping_group> *processed_groups,
10884 tree sentinel)
10885 {
10886 hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
10887 = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
10888
10889 processed_groups->truncate (0);
10890
10891 omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
10892 omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
10893 if (sentinel)
10894 omp_index_mapping_groups_1 (grpmap, groups, sentinel);
10895
10896 return grpmap;
10897 }
10898
10899 /* Find the immediately-containing struct for a component ref (etc.)
10900 expression EXPR. */
10901
10902 static tree
10903 omp_containing_struct (tree expr)
10904 {
10905 tree expr0 = expr;
10906
10907 STRIP_NOPS (expr);
10908
10909 /* Note: don't strip NOPs unless we're also stripping off array refs or a
10910 component ref. */
10911 if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
10912 return expr0;
10913
10914 while (TREE_CODE (expr) == ARRAY_REF)
10915 expr = TREE_OPERAND (expr, 0);
10916
10917 if (TREE_CODE (expr) == COMPONENT_REF)
10918 expr = TREE_OPERAND (expr, 0);
10919
10920 return expr;
10921 }
10922
10923 /* Return TRUE if DECL describes a component that is part of a whole structure
10924 that is mapped elsewhere in GRPMAP. *MAPPED_BY_GROUP is set to the group
10925 that maps that structure, if present. */
10926
10927 static bool
10928 omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
10929 omp_mapping_group *> *grpmap,
10930 tree decl,
10931 omp_mapping_group **mapped_by_group)
10932 {
10933 tree wsdecl = NULL_TREE;
10934
10935 *mapped_by_group = NULL;
10936
10937 while (true)
10938 {
10939 wsdecl = omp_containing_struct (decl);
10940 if (wsdecl == decl)
10941 break;
10942 omp_mapping_group **wholestruct = grpmap->get (wsdecl);
10943 if (!wholestruct
10944 && TREE_CODE (wsdecl) == MEM_REF
10945 && integer_zerop (TREE_OPERAND (wsdecl, 1)))
10946 {
10947 tree deref = TREE_OPERAND (wsdecl, 0);
10948 deref = build_fold_indirect_ref (deref);
10949 wholestruct = grpmap->get (deref);
10950 }
10951 if (wholestruct)
10952 {
10953 *mapped_by_group = *wholestruct;
10954 return true;
10955 }
10956 decl = wsdecl;
10957 }
10958
10959 return false;
10960 }
10961
10962 /* Helper function for omp_tsort_mapping_groups. Returns TRUE on success, or
10963 FALSE on error. */
10964
10965 static bool
10966 omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
10967 vec<omp_mapping_group> *groups,
10968 hash_map<tree_operand_hash_no_se,
10969 omp_mapping_group *> *grpmap,
10970 omp_mapping_group *grp)
10971 {
10972 if (grp->mark == PERMANENT)
10973 return true;
10974 if (grp->mark == TEMPORARY)
10975 {
10976 fprintf (stderr, "when processing group:\n");
10977 debug_mapping_group (grp);
10978 internal_error ("base pointer cycle detected");
10979 return false;
10980 }
10981 grp->mark = TEMPORARY;
10982
10983 tree attaches_to = omp_get_attachment (grp);
10984
10985 if (attaches_to)
10986 {
10987 omp_mapping_group **basep = grpmap->get (attaches_to);
10988
10989 if (basep && *basep != grp)
10990 {
10991 for (omp_mapping_group *w = *basep; w; w = w->sibling)
10992 if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
10993 return false;
10994 }
10995 }
10996
10997 tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
10998
10999 while (decl)
11000 {
11001 tree base = omp_get_base_pointer (decl);
11002
11003 if (!base)
11004 break;
11005
11006 omp_mapping_group **innerp = grpmap->get (base);
11007 omp_mapping_group *wholestruct;
11008
11009 /* We should treat whole-structure mappings as if all (pointer, in this
11010 case) members are mapped as individual list items. Check if we have
11011 such a whole-structure mapping, if we don't have an explicit reference
11012 to the pointer member itself. */
11013 if (!innerp
11014 && TREE_CODE (base) == COMPONENT_REF
11015 && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
11016 innerp = &wholestruct;
11017
11018 if (innerp && *innerp != grp)
11019 {
11020 for (omp_mapping_group *w = *innerp; w; w = w->sibling)
11021 if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11022 return false;
11023 break;
11024 }
11025
11026 decl = base;
11027 }
11028
11029 grp->mark = PERMANENT;
11030
11031 /* Emit grp to output list. */
11032
11033 **outlist = grp;
11034 *outlist = &grp->next;
11035
11036 return true;
11037 }
11038
11039 /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
11040 before mappings that use those pointers. This is an implementation of the
11041 depth-first search algorithm, described e.g. at:
11042
11043 https://en.wikipedia.org/wiki/Topological_sorting
11044 */
11045
11046 static omp_mapping_group *
11047 omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
11048 hash_map<tree_operand_hash_no_se, omp_mapping_group *>
11049 *grpmap,
11050 bool enter_exit_data)
11051 {
11052 omp_mapping_group *grp, *outlist = NULL, **cursor;
11053 unsigned int i;
11054 bool saw_runtime_implicit = false;
11055
11056 cursor = &outlist;
11057
11058 FOR_EACH_VEC_ELT (*groups, i, grp)
11059 {
11060 if (grp->mark != PERMANENT)
11061 {
11062 if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11063 {
11064 saw_runtime_implicit = true;
11065 continue;
11066 }
11067 if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11068 return NULL;
11069 }
11070 }
11071
11072 if (!saw_runtime_implicit)
11073 return outlist;
11074
11075 FOR_EACH_VEC_ELT (*groups, i, grp)
11076 {
11077 if (grp->mark != PERMANENT
11078 && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11079 {
11080 /* Clear the flag for enter/exit data because it is currently
11081 meaningless for those operations in libgomp. */
11082 if (enter_exit_data)
11083 OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
11084
11085 if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11086 return NULL;
11087 }
11088 }
11089
11090 return outlist;
11091 }
11092
11093 /* Split INLIST into three parts:
11094
11095 - "present" alloc/to/from groups
11096 - other to/from groups
11097 - other alloc/release/delete groups
11098
11099 These sub-lists are then concatenated together to form the final list.
11100 Each sub-list retains the order of the original list.
11101 Note that ATTACH nodes are later moved to the end of the list in
11102 gimplify_adjust_omp_clauses, for target regions. */
11103
11104 static omp_mapping_group *
11105 omp_segregate_mapping_groups (omp_mapping_group *inlist)
11106 {
11107 omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
11108 omp_mapping_group *p_groups = NULL;
11109 omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
11110 omp_mapping_group **p_tail = &p_groups;
11111
11112 for (omp_mapping_group *w = inlist; w;)
11113 {
11114 tree c = *w->grp_start;
11115 omp_mapping_group *next = w->next;
11116
11117 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
11118
11119 switch (OMP_CLAUSE_MAP_KIND (c))
11120 {
11121 case GOMP_MAP_ALLOC:
11122 case GOMP_MAP_RELEASE:
11123 case GOMP_MAP_DELETE:
11124 *ard_tail = w;
11125 w->next = NULL;
11126 ard_tail = &w->next;
11127 break;
11128
11129 /* These map types are all semantically identical, so are moved into a
11130 single group. They will each be changed into GOMP_MAP_FORCE_PRESENT
11131 in gimplify_adjust_omp_clauses. */
11132 case GOMP_MAP_PRESENT_ALLOC:
11133 case GOMP_MAP_PRESENT_FROM:
11134 case GOMP_MAP_PRESENT_TO:
11135 case GOMP_MAP_PRESENT_TOFROM:
11136 *p_tail = w;
11137 w->next = NULL;
11138 p_tail = &w->next;
11139 break;
11140
11141 default:
11142 *tf_tail = w;
11143 w->next = NULL;
11144 tf_tail = &w->next;
11145 }
11146
11147 w = next;
11148 }
11149
11150 /* Now splice the lists together... */
11151 *tf_tail = ard_groups;
11152 *p_tail = tf_groups;
11153
11154 return p_groups;
11155 }
11156
11157 /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
11158 those groups based on the output list of omp_tsort_mapping_groups --
11159 singly-linked, threaded through each element's NEXT pointer starting at
11160 HEAD. Each list element appears exactly once in that linked list.
11161
11162 Each element of GROUPS may correspond to one or several mapping nodes.
11163 Node groups are kept together, and in the reordered list, the positions of
11164 the original groups are reused for the positions of the reordered list.
11165 Hence if we have e.g.
11166
11167 {to ptr ptr} firstprivate {tofrom ptr} ...
11168 ^ ^ ^
11169 first group non-"map" second group
11170
11171 and say the second group contains a base pointer for the first so must be
11172 moved before it, the resulting list will contain:
11173
11174 {tofrom ptr} firstprivate {to ptr ptr} ...
11175 ^ prev. second group ^ prev. first group
11176 */
11177
11178 static tree *
11179 omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
11180 omp_mapping_group *head,
11181 tree *list_p)
11182 {
11183 omp_mapping_group *grp;
11184 unsigned int i;
11185 unsigned numgroups = groups->length ();
11186 auto_vec<tree> old_heads (numgroups);
11187 auto_vec<tree *> old_headps (numgroups);
11188 auto_vec<tree> new_heads (numgroups);
11189 auto_vec<tree> old_succs (numgroups);
11190 bool map_at_start = (list_p == (*groups)[0].grp_start);
11191
11192 tree *new_grp_tail = NULL;
11193
11194 /* Stash the start & end nodes of each mapping group before we start
11195 modifying the list. */
11196 FOR_EACH_VEC_ELT (*groups, i, grp)
11197 {
11198 old_headps.quick_push (grp->grp_start);
11199 old_heads.quick_push (*grp->grp_start);
11200 old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
11201 }
11202
11203 /* And similarly, the heads of the groups in the order we want to rearrange
11204 the list to. */
11205 for (omp_mapping_group *w = head; w; w = w->next)
11206 new_heads.quick_push (*w->grp_start);
11207
11208 FOR_EACH_VEC_ELT (*groups, i, grp)
11209 {
11210 gcc_assert (head);
11211
11212 if (new_grp_tail && old_succs[i - 1] == old_heads[i])
11213 {
11214 /* a {b c d} {e f g} h i j (original)
11215 -->
11216 a {k l m} {e f g} h i j (inserted new group on last iter)
11217 -->
11218 a {k l m} {n o p} h i j (this time, chain last group to new one)
11219 ^new_grp_tail
11220 */
11221 *new_grp_tail = new_heads[i];
11222 }
11223 else if (new_grp_tail)
11224 {
11225 /* a {b c d} e {f g h} i j k (original)
11226 -->
11227 a {l m n} e {f g h} i j k (gap after last iter's group)
11228 -->
11229 a {l m n} e {o p q} h i j (chain last group to old successor)
11230 ^new_grp_tail
11231 */
11232 *new_grp_tail = old_succs[i - 1];
11233 *old_headps[i] = new_heads[i];
11234 }
11235 else
11236 {
11237 /* The first inserted group -- point to new group, and leave end
11238 open.
11239 a {b c d} e f
11240 -->
11241 a {g h i...
11242 */
11243 *grp->grp_start = new_heads[i];
11244 }
11245
11246 new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
11247
11248 head = head->next;
11249 }
11250
11251 if (new_grp_tail)
11252 *new_grp_tail = old_succs[numgroups - 1];
11253
11254 gcc_assert (!head);
11255
11256 return map_at_start ? (*groups)[0].grp_start : list_p;
11257 }
11258
11259 /* DECL is supposed to have lastprivate semantics in the outer contexts
11260 of combined/composite constructs, starting with OCTX.
11261 Add needed lastprivate, shared or map clause if no data sharing or
11262 mapping clause are present. IMPLICIT_P is true if it is an implicit
11263 clause (IV on simd), in which case the lastprivate will not be
11264 copied to some constructs. */
11265
11266 static void
11267 omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
11268 tree decl, bool implicit_p)
11269 {
11270 struct gimplify_omp_ctx *orig_octx = octx;
11271 for (; octx; octx = octx->outer_context)
11272 {
11273 if ((octx->region_type == ORT_COMBINED_PARALLEL
11274 || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
11275 && splay_tree_lookup (octx->variables,
11276 (splay_tree_key) decl) == NULL)
11277 {
11278 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
11279 continue;
11280 }
11281 if ((octx->region_type & ORT_TASK) != 0
11282 && octx->combined_loop
11283 && splay_tree_lookup (octx->variables,
11284 (splay_tree_key) decl) == NULL)
11285 {
11286 omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11287 continue;
11288 }
11289 if (implicit_p
11290 && octx->region_type == ORT_WORKSHARE
11291 && octx->combined_loop
11292 && splay_tree_lookup (octx->variables,
11293 (splay_tree_key) decl) == NULL
11294 && octx->outer_context
11295 && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
11296 && splay_tree_lookup (octx->outer_context->variables,
11297 (splay_tree_key) decl) == NULL)
11298 {
11299 octx = octx->outer_context;
11300 omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11301 continue;
11302 }
11303 if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
11304 && octx->combined_loop
11305 && splay_tree_lookup (octx->variables,
11306 (splay_tree_key) decl) == NULL
11307 && !omp_check_private (octx, decl, false))
11308 {
11309 omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11310 continue;
11311 }
11312 if (octx->region_type == ORT_COMBINED_TARGET)
11313 {
11314 splay_tree_node n = splay_tree_lookup (octx->variables,
11315 (splay_tree_key) decl);
11316 if (n == NULL)
11317 {
11318 omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11319 octx = octx->outer_context;
11320 }
11321 else if (!implicit_p
11322 && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
11323 {
11324 n->value &= ~(GOVD_FIRSTPRIVATE
11325 | GOVD_FIRSTPRIVATE_IMPLICIT
11326 | GOVD_EXPLICIT);
11327 omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11328 octx = octx->outer_context;
11329 }
11330 }
11331 break;
11332 }
11333 if (octx && (implicit_p || octx != orig_octx))
11334 omp_notice_variable (octx, decl, true);
11335 }
11336
11337 /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
11338 a "FIRSTPRIVATE" mapping. Return the one that isn't firstprivate, etc. */
11339
11340 static omp_mapping_group *
11341 omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
11342 omp_mapping_group *> *grpmap,
11343 tree decl, bool allow_deleted = false)
11344 {
11345 omp_mapping_group **to_group_p = grpmap->get (decl);
11346
11347 if (!to_group_p)
11348 return NULL;
11349
11350 omp_mapping_group *to_group = *to_group_p;
11351
11352 for (; to_group; to_group = to_group->sibling)
11353 {
11354 tree grp_end = to_group->grp_end;
11355 switch (OMP_CLAUSE_MAP_KIND (grp_end))
11356 {
11357 case GOMP_MAP_FIRSTPRIVATE_POINTER:
11358 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11359 break;
11360
11361 default:
11362 if (allow_deleted || !to_group->deleted)
11363 return to_group;
11364 }
11365 }
11366
11367 return NULL;
11368 }
11369
11370 /* Return TRUE if the directive (whose clauses are described by the hash table
11371 of mapping groups, GRPMAP) maps DECL explicitly. If TO_SPECIFICALLY is
11372 true, only count TO mappings. If ALLOW_DELETED is true, ignore the
11373 "deleted" flag for groups. If CONTAINED_IN_STRUCT is true, also return
11374 TRUE if DECL is mapped as a member of a whole-struct mapping. */
11375
11376 static bool
11377 omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
11378 omp_mapping_group *> *grpmap,
11379 tree decl, omp_mapping_group **base_group,
11380 bool to_specifically, bool allow_deleted,
11381 bool contained_in_struct)
11382 {
11383 omp_mapping_group *decl_group
11384 = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
11385
11386 *base_group = NULL;
11387
11388 if (decl_group)
11389 {
11390 tree grp_first = *decl_group->grp_start;
11391 /* We might be called during omp_build_struct_sibling_lists, when
11392 GOMP_MAP_STRUCT might have been inserted at the start of the group.
11393 Skip over that, and also possibly the node after it. */
11394 if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
11395 || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
11396 {
11397 grp_first = OMP_CLAUSE_CHAIN (grp_first);
11398 if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
11399 || (OMP_CLAUSE_MAP_KIND (grp_first)
11400 == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11401 || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
11402 grp_first = OMP_CLAUSE_CHAIN (grp_first);
11403 }
11404 enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11405 if (!to_specifically
11406 || GOMP_MAP_COPY_TO_P (first_kind)
11407 || first_kind == GOMP_MAP_ALLOC)
11408 {
11409 *base_group = decl_group;
11410 return true;
11411 }
11412 }
11413
11414 if (contained_in_struct
11415 && omp_mapped_by_containing_struct (grpmap, decl, base_group))
11416 return true;
11417
11418 return false;
11419 }
11420
11421 /* If we have mappings INNER and OUTER, where INNER is a component access and
11422 OUTER is a mapping of the whole containing struct, check that the mappings
11423 are compatible. We'll be deleting the inner mapping, so we need to make
11424 sure the outer mapping does (at least) the same transfers to/from the device
11425 as the inner mapping. */
11426
11427 bool
11428 omp_check_mapping_compatibility (location_t loc,
11429 omp_mapping_group *outer,
11430 omp_mapping_group *inner)
11431 {
11432 tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
11433
11434 gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
11435 gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
11436
11437 enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
11438 enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
11439
11440 if (outer_kind == inner_kind)
11441 return true;
11442
11443 switch (outer_kind)
11444 {
11445 case GOMP_MAP_ALWAYS_TO:
11446 if (inner_kind == GOMP_MAP_FORCE_PRESENT
11447 || inner_kind == GOMP_MAP_ALLOC
11448 || inner_kind == GOMP_MAP_TO)
11449 return true;
11450 break;
11451
11452 case GOMP_MAP_ALWAYS_FROM:
11453 if (inner_kind == GOMP_MAP_FORCE_PRESENT
11454 || inner_kind == GOMP_MAP_RELEASE
11455 || inner_kind == GOMP_MAP_FROM)
11456 return true;
11457 break;
11458
11459 case GOMP_MAP_TO:
11460 if (inner_kind == GOMP_MAP_FORCE_PRESENT
11461 || inner_kind == GOMP_MAP_ALLOC)
11462 return true;
11463 break;
11464
11465 case GOMP_MAP_FROM:
11466 if (inner_kind == GOMP_MAP_RELEASE
11467 || inner_kind == GOMP_MAP_FORCE_PRESENT)
11468 return true;
11469 break;
11470
11471 case GOMP_MAP_ALWAYS_TOFROM:
11472 case GOMP_MAP_TOFROM:
11473 if (inner_kind == GOMP_MAP_FORCE_PRESENT
11474 || inner_kind == GOMP_MAP_ALLOC
11475 || inner_kind == GOMP_MAP_TO
11476 || inner_kind == GOMP_MAP_FROM
11477 || inner_kind == GOMP_MAP_TOFROM)
11478 return true;
11479 break;
11480
11481 default:
11482 ;
11483 }
11484
11485 error_at (loc, "data movement for component %qE is not compatible with "
11486 "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
11487 OMP_CLAUSE_DECL (first_outer));
11488
11489 return false;
11490 }
11491
11492 /* This function handles several cases where clauses on a mapping directive
11493 can interact with each other.
11494
11495 If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
11496 on the same directive, change the mapping of the first node to
11497 ATTACH_DETACH. We should have detected that this will happen already in
11498 c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
11499 as addressable. (If we didn't, bail out.)
11500
11501 If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
11502 mapping the base pointer also, we may need to change the mapping type to
11503 ATTACH_DETACH and synthesize an alloc node for the reference itself.
11504
11505 If we have an ATTACH_DETACH node, this is an array section with a pointer
11506 base. If we're mapping the base on the same directive too, we can drop its
11507 mapping. However, if we have a reference to pointer, make other appropriate
11508 adjustments to the mapping nodes instead.
11509
11510 If we have an ATTACH_DETACH node with a Fortran pointer-set (array
11511 descriptor) mapping for a derived-type component, and we're also mapping the
11512 whole of the derived-type variable on another clause, the pointer-set
11513 mapping is removed.
11514
11515 If we have a component access but we're also mapping the whole of the
11516 containing struct, drop the former access.
11517
11518 If the expression is a component access, and we're also mapping a base
11519 pointer used in that component access in the same expression, change the
11520 mapping type of the latter to ALLOC (ready for processing by
11521 omp_build_struct_sibling_lists). */
11522
11523 void
11524 omp_resolve_clause_dependencies (enum tree_code code,
11525 vec<omp_mapping_group> *groups,
11526 hash_map<tree_operand_hash_no_se,
11527 omp_mapping_group *> *grpmap)
11528 {
11529 int i;
11530 omp_mapping_group *grp;
11531 bool repair_chain = false;
11532
11533 FOR_EACH_VEC_ELT (*groups, i, grp)
11534 {
11535 tree grp_end = grp->grp_end;
11536 tree decl = OMP_CLAUSE_DECL (grp_end);
11537
11538 gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11539
11540 switch (OMP_CLAUSE_MAP_KIND (grp_end))
11541 {
11542 case GOMP_MAP_FIRSTPRIVATE_POINTER:
11543 {
11544 omp_mapping_group *to_group
11545 = omp_get_nonfirstprivate_group (grpmap, decl);
11546
11547 if (!to_group || to_group == grp)
11548 continue;
11549
11550 tree grp_first = *to_group->grp_start;
11551 enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11552
11553 if ((GOMP_MAP_COPY_TO_P (first_kind)
11554 || first_kind == GOMP_MAP_ALLOC)
11555 && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
11556 != GOMP_MAP_FIRSTPRIVATE_POINTER))
11557 {
11558 gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
11559 OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
11560 }
11561 }
11562 break;
11563
11564 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11565 {
11566 tree ptr = build_fold_indirect_ref (decl);
11567
11568 omp_mapping_group *to_group
11569 = omp_get_nonfirstprivate_group (grpmap, ptr);
11570
11571 if (!to_group || to_group == grp)
11572 continue;
11573
11574 tree grp_first = *to_group->grp_start;
11575 enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11576
11577 if (GOMP_MAP_COPY_TO_P (first_kind)
11578 || first_kind == GOMP_MAP_ALLOC)
11579 {
11580 OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
11581 OMP_CLAUSE_DECL (grp_end) = ptr;
11582 if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
11583 == to_group->grp_end)
11584 && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
11585 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
11586 {
11587 gcc_assert (TREE_ADDRESSABLE
11588 (OMP_CLAUSE_DECL (to_group->grp_end)));
11589 OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
11590 GOMP_MAP_ATTACH_DETACH);
11591
11592 location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
11593 tree alloc
11594 = build_omp_clause (loc, OMP_CLAUSE_MAP);
11595 OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
11596 tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
11597 (to_group->grp_end));
11598 tree char_ptr_type = build_pointer_type (char_type_node);
11599 OMP_CLAUSE_DECL (alloc)
11600 = build2 (MEM_REF, char_type_node,
11601 tmp,
11602 build_int_cst (char_ptr_type, 0));
11603 OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
11604
11605 OMP_CLAUSE_CHAIN (alloc)
11606 = OMP_CLAUSE_CHAIN (*to_group->grp_start);
11607 OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
11608 }
11609 }
11610 }
11611 break;
11612
11613 case GOMP_MAP_ATTACH_DETACH:
11614 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11615 {
11616 tree base_ptr, referenced_ptr_node = NULL_TREE;
11617
11618 while (TREE_CODE (decl) == ARRAY_REF)
11619 decl = TREE_OPERAND (decl, 0);
11620
11621 if (TREE_CODE (decl) == INDIRECT_REF)
11622 decl = TREE_OPERAND (decl, 0);
11623
11624 /* Only component accesses. */
11625 if (DECL_P (decl))
11626 continue;
11627
11628 /* We want the pointer itself when checking if the base pointer is
11629 mapped elsewhere in the same directive -- if we have a
11630 reference to the pointer, don't use that. */
11631
11632 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
11633 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
11634 {
11635 referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
11636 base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
11637 }
11638 else
11639 base_ptr = decl;
11640
11641 gomp_map_kind zlas_kind
11642 = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
11643 ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
11644
11645 if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
11646 {
11647 /* If we map the base TO, and we're doing an attachment, we can
11648 skip the TO mapping altogether and create an ALLOC mapping
11649 instead, since the attachment will overwrite the device
11650 pointer in that location immediately anyway. Otherwise,
11651 change our mapping to
11652 GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
11653 attachment target has not been copied to the device already
11654 by some earlier directive. */
11655
11656 bool base_mapped_to = false;
11657
11658 omp_mapping_group *base_group;
11659
11660 if (omp_directive_maps_explicitly (grpmap, base_ptr,
11661 &base_group, false, true,
11662 false))
11663 {
11664 if (referenced_ptr_node)
11665 {
11666 base_mapped_to = true;
11667 if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
11668 == GOMP_MAP_ATTACH_DETACH)
11669 && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
11670 == base_group->grp_end))
11671 {
11672 OMP_CLAUSE_CHAIN (*base_group->grp_start)
11673 = OMP_CLAUSE_CHAIN (base_group->grp_end);
11674 base_group->grp_end = *base_group->grp_start;
11675 repair_chain = true;
11676 }
11677 }
11678 else
11679 {
11680 base_group->deleted = true;
11681 OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
11682 }
11683 }
11684
11685 /* We're dealing with a reference to a pointer, and we are
11686 attaching both the reference and the pointer. We know the
11687 reference itself is on the target, because we are going to
11688 create an ALLOC node for it in accumulate_sibling_list. The
11689 pointer might be on the target already or it might not, but
11690 if it isn't then it's not an error, so use
11691 GOMP_MAP_ATTACH_ZLAS for it. */
11692 if (!base_mapped_to && referenced_ptr_node)
11693 OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
11694
11695 omp_mapping_group *struct_group;
11696 tree desc;
11697 if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
11698 && omp_map_clause_descriptor_p (desc)
11699 && omp_mapped_by_containing_struct (grpmap, decl,
11700 &struct_group))
11701 /* If we have a pointer set but we're mapping (or unmapping)
11702 the whole of the containing struct, we can remove the
11703 pointer set mapping. */
11704 OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
11705 }
11706 else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
11707 && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
11708 == ARRAY_TYPE)
11709 && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
11710 (*grp->grp_start))
11711 OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
11712 }
11713 break;
11714
11715 case GOMP_MAP_ATTACH:
11716 /* Ignore standalone attach here. */
11717 break;
11718
11719 default:
11720 {
11721 omp_mapping_group *struct_group;
11722 if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
11723 && *grp->grp_start == grp_end)
11724 {
11725 omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
11726 struct_group, grp);
11727 /* Remove the whole of this mapping -- redundant. */
11728 grp->deleted = true;
11729 }
11730
11731 tree base = decl;
11732 while ((base = omp_get_base_pointer (base)))
11733 {
11734 omp_mapping_group *base_group;
11735
11736 if (omp_directive_maps_explicitly (grpmap, base, &base_group,
11737 true, true, false))
11738 {
11739 tree grp_first = *base_group->grp_start;
11740 OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
11741 }
11742 }
11743 }
11744 }
11745 }
11746
11747 if (repair_chain)
11748 {
11749 /* Group start pointers may have become detached from the
11750 OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
11751 end of those groups. Fix that now. */
11752 tree *new_next = NULL;
11753 FOR_EACH_VEC_ELT (*groups, i, grp)
11754 {
11755 if (new_next)
11756 grp->grp_start = new_next;
11757
11758 new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
11759 }
11760 }
11761 }
11762
11763 /* Similar to omp_resolve_clause_dependencies, but for OpenACC. The only
11764 clause dependencies we handle for now are struct element mappings and
11765 whole-struct mappings on the same directive, and duplicate clause
11766 detection. */
11767
11768 void
11769 oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
11770 hash_map<tree_operand_hash_no_se,
11771 omp_mapping_group *> *grpmap)
11772 {
11773 int i;
11774 omp_mapping_group *grp;
11775 hash_set<tree_operand_hash> *seen_components = NULL;
11776 hash_set<tree_operand_hash> *shown_error = NULL;
11777
11778 FOR_EACH_VEC_ELT (*groups, i, grp)
11779 {
11780 tree grp_end = grp->grp_end;
11781 tree decl = OMP_CLAUSE_DECL (grp_end);
11782
11783 gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11784
11785 if (DECL_P (grp_end))
11786 continue;
11787
11788 tree c = OMP_CLAUSE_DECL (*grp->grp_start);
11789 while (TREE_CODE (c) == ARRAY_REF)
11790 c = TREE_OPERAND (c, 0);
11791 if (TREE_CODE (c) != COMPONENT_REF)
11792 continue;
11793 if (!seen_components)
11794 seen_components = new hash_set<tree_operand_hash> ();
11795 if (!shown_error)
11796 shown_error = new hash_set<tree_operand_hash> ();
11797 if (seen_components->contains (c)
11798 && !shown_error->contains (c))
11799 {
11800 error_at (OMP_CLAUSE_LOCATION (grp_end),
11801 "%qE appears more than once in map clauses",
11802 OMP_CLAUSE_DECL (grp_end));
11803 shown_error->add (c);
11804 }
11805 else
11806 seen_components->add (c);
11807
11808 omp_mapping_group *struct_group;
11809 if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
11810 && *grp->grp_start == grp_end)
11811 {
11812 omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
11813 struct_group, grp);
11814 /* Remove the whole of this mapping -- redundant. */
11815 grp->deleted = true;
11816 }
11817 }
11818
11819 if (seen_components)
11820 delete seen_components;
11821 if (shown_error)
11822 delete shown_error;
11823 }
11824
11825 /* Link node NEWNODE so it is pointed to by chain INSERT_AT. NEWNODE's chain
11826 is linked to the previous node pointed to by INSERT_AT. */
11827
11828 static tree *
11829 omp_siblist_insert_node_after (tree newnode, tree *insert_at)
11830 {
11831 OMP_CLAUSE_CHAIN (newnode) = *insert_at;
11832 *insert_at = newnode;
11833 return &OMP_CLAUSE_CHAIN (newnode);
11834 }
11835
11836 /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
11837 pointed to by chain MOVE_AFTER instead. */
11838
11839 static void
11840 omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
11841 {
11842 gcc_assert (node == *old_pos);
11843 *old_pos = OMP_CLAUSE_CHAIN (node);
11844 OMP_CLAUSE_CHAIN (node) = *move_after;
11845 *move_after = node;
11846 }
11847
11848 /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
11849 LAST_NODE to after MOVE_AFTER chain. Similar to below function, but no
11850 new nodes are prepended to the list before splicing into the new position.
11851 Return the position we should continue scanning the list at, or NULL to
11852 stay where we were. */
11853
11854 static tree *
11855 omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
11856 tree *move_after)
11857 {
11858 if (first_ptr == move_after)
11859 return NULL;
11860
11861 tree tmp = *first_ptr;
11862 *first_ptr = OMP_CLAUSE_CHAIN (last_node);
11863 OMP_CLAUSE_CHAIN (last_node) = *move_after;
11864 *move_after = tmp;
11865
11866 return first_ptr;
11867 }
11868
11869 /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
11870 [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
11871 pointer MOVE_AFTER.
11872
11873 The latter list was previously part of the OMP clause list, and the former
11874 (prepended) part is comprised of new nodes.
11875
11876 We start with a list of nodes starting with a struct mapping node. We
11877 rearrange the list so that new nodes starting from FIRST_NEW and whose last
11878 node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
11879 the group of mapping nodes we are currently processing (from the chain
11880 FIRST_PTR to LAST_NODE). The return value is the pointer to the next chain
11881 we should continue processing from, or NULL to stay where we were.
11882
11883 The transformation (in the case where MOVE_AFTER and FIRST_PTR are
11884 different) is worked through below. Here we are processing LAST_NODE, and
11885 FIRST_PTR points at the preceding mapping clause:
11886
11887 #. mapping node chain
11888 ---------------------------------------------------
11889 A. struct_node [->B]
11890 B. comp_1 [->C]
11891 C. comp_2 [->D (move_after)]
11892 D. map_to_3 [->E]
11893 E. attach_3 [->F (first_ptr)]
11894 F. map_to_4 [->G (continue_at)]
11895 G. attach_4 (last_node) [->H]
11896 H. ...
11897
11898 *last_new_tail = *first_ptr;
11899
11900 I. new_node (first_new) [->F (last_new_tail)]
11901
11902 *first_ptr = OMP_CLAUSE_CHAIN (last_node)
11903
11904 #. mapping node chain
11905 ----------------------------------------------------
11906 A. struct_node [->B]
11907 B. comp_1 [->C]
11908 C. comp_2 [->D (move_after)]
11909 D. map_to_3 [->E]
11910 E. attach_3 [->H (first_ptr)]
11911 F. map_to_4 [->G (continue_at)]
11912 G. attach_4 (last_node) [->H]
11913 H. ...
11914
11915 I. new_node (first_new) [->F (last_new_tail)]
11916
11917 OMP_CLAUSE_CHAIN (last_node) = *move_after;
11918
11919 #. mapping node chain
11920 ---------------------------------------------------
11921 A. struct_node [->B]
11922 B. comp_1 [->C]
11923 C. comp_2 [->D (move_after)]
11924 D. map_to_3 [->E]
11925 E. attach_3 [->H (continue_at)]
11926 F. map_to_4 [->G]
11927 G. attach_4 (last_node) [->D]
11928 H. ...
11929
11930 I. new_node (first_new) [->F (last_new_tail)]
11931
11932 *move_after = first_new;
11933
11934 #. mapping node chain
11935 ---------------------------------------------------
11936 A. struct_node [->B]
11937 B. comp_1 [->C]
11938 C. comp_2 [->I (move_after)]
11939 D. map_to_3 [->E]
11940 E. attach_3 [->H (continue_at)]
11941 F. map_to_4 [->G]
11942 G. attach_4 (last_node) [->D]
11943 H. ...
11944 I. new_node (first_new) [->F (last_new_tail)]
11945
11946 or, in order:
11947
11948 #. mapping node chain
11949 ---------------------------------------------------
11950 A. struct_node [->B]
11951 B. comp_1 [->C]
11952 C. comp_2 [->I (move_after)]
11953 I. new_node (first_new) [->F (last_new_tail)]
11954 F. map_to_4 [->G]
11955 G. attach_4 (last_node) [->D]
11956 D. map_to_3 [->E]
11957 E. attach_3 [->H (continue_at)]
11958 H. ...
11959 */
11960
11961 static tree *
11962 omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
11963 tree *first_ptr, tree last_node,
11964 tree *move_after)
11965 {
11966 tree *continue_at = NULL;
11967 *last_new_tail = *first_ptr;
11968 if (first_ptr == move_after)
11969 *move_after = first_new;
11970 else
11971 {
11972 *first_ptr = OMP_CLAUSE_CHAIN (last_node);
11973 continue_at = first_ptr;
11974 OMP_CLAUSE_CHAIN (last_node) = *move_after;
11975 *move_after = first_new;
11976 }
11977 return continue_at;
11978 }
11979
11980 static omp_addr_token *
11981 omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
11982 {
11983 using namespace omp_addr_tokenizer;
11984 int idx = addr_tokens.length () - 1;
11985 gcc_assert (idx >= 0);
11986 if (addr_tokens[idx]->type != ACCESS_METHOD)
11987 return addr_tokens[idx];
11988 while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
11989 idx--;
11990 return addr_tokens[idx];
11991 }
11992
11993 /* Mapping struct members causes an additional set of nodes to be created,
11994 starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
11995 number of members being mapped, in order of ascending position (address or
11996 bitwise).
11997
11998 We scan through the list of mapping clauses, calling this function for each
11999 struct member mapping we find, and build up the list of mappings after the
12000 initial GOMP_MAP_STRUCT node. For pointer members, these will be
12001 newly-created ALLOC nodes. For non-pointer members, the existing mapping is
12002 moved into place in the sorted list.
12003
12004 struct {
12005 int *a;
12006 int *b;
12007 int c;
12008 int *d;
12009 };
12010
12011 #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
12012 struct.d[0:n])
12013
12014 GOMP_MAP_STRUCT (4)
12015 [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
12016 GOMP_MAP_ALLOC (struct.a)
12017 GOMP_MAP_ALLOC (struct.b)
12018 GOMP_MAP_TO (struct.c)
12019 GOMP_MAP_ALLOC (struct.d)
12020 ...
12021
12022 In the case where we are mapping references to pointers, or in Fortran if
12023 we are mapping an array with a descriptor, additional nodes may be created
12024 after the struct node list also.
12025
12026 The return code is either a pointer to the next node to process (if the
12027 list has been rearranged), else NULL to continue with the next node in the
12028 original list. */
12029
12030 static tree *
12031 omp_accumulate_sibling_list (enum omp_region_type region_type,
12032 enum tree_code code,
12033 hash_map<tree_operand_hash, tree>
12034 *&struct_map_to_clause,
12035 hash_map<tree_operand_hash_no_se,
12036 omp_mapping_group *> *group_map,
12037 tree *grp_start_p, tree grp_end,
12038 vec<omp_addr_token *> &addr_tokens, tree **inner,
12039 bool *fragile_p, bool reprocessing_struct,
12040 tree **added_tail)
12041 {
12042 using namespace omp_addr_tokenizer;
12043 poly_offset_int coffset;
12044 poly_int64 cbitpos;
12045 tree ocd = OMP_CLAUSE_DECL (grp_end);
12046 bool openmp = !(region_type & ORT_ACC);
12047 bool target = (region_type & ORT_TARGET) != 0;
12048 tree *continue_at = NULL;
12049
12050 while (TREE_CODE (ocd) == ARRAY_REF)
12051 ocd = TREE_OPERAND (ocd, 0);
12052
12053 if (*fragile_p)
12054 {
12055 omp_mapping_group *to_group
12056 = omp_get_nonfirstprivate_group (group_map, ocd, true);
12057
12058 if (to_group)
12059 return NULL;
12060 }
12061
12062 omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
12063 if (last_token->type == ACCESS_METHOD)
12064 {
12065 switch (last_token->u.access_kind)
12066 {
12067 case ACCESS_REF:
12068 case ACCESS_REF_TO_POINTER:
12069 case ACCESS_REF_TO_POINTER_OFFSET:
12070 case ACCESS_INDEXED_REF_TO_ARRAY:
12071 /* We may see either a bare reference or a dereferenced
12072 "convert_from_reference"-like one here. Handle either way. */
12073 if (TREE_CODE (ocd) == INDIRECT_REF)
12074 ocd = TREE_OPERAND (ocd, 0);
12075 gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
12076 break;
12077
12078 default:
12079 ;
12080 }
12081 }
12082
12083 bool variable_offset;
12084 tree base
12085 = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset);
12086
12087 int base_token;
12088 for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
12089 {
12090 if (addr_tokens[base_token]->type == ARRAY_BASE
12091 || addr_tokens[base_token]->type == STRUCTURE_BASE)
12092 break;
12093 }
12094
12095 /* The two expressions in the assertion below aren't quite the same: if we
12096 have 'struct_base_decl access_indexed_array' for something like
12097 "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
12098 will be "myvar[2]" -- the actual base of the structure.
12099 The former interpretation leads to a strange situation where we get
12100 struct(myvar) alloc(myvar[2].ptr1)
12101 That is, the array of structures is kind of treated as one big structure
12102 for the purposes of gathering sibling lists, etc. */
12103 /* gcc_assert (base == addr_tokens[base_token]->expr); */
12104
12105 bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
12106 == GOMP_MAP_ATTACH_DETACH)
12107 || (OMP_CLAUSE_MAP_KIND (grp_end)
12108 == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
12109 bool has_descriptor = false;
12110 if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12111 {
12112 tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
12113 if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
12114 has_descriptor = true;
12115 }
12116
12117 if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
12118 {
12119 enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
12120
12121 if (struct_map_to_clause == NULL)
12122 struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
12123
12124 if (variable_offset)
12125 str_kind = GOMP_MAP_STRUCT_UNORD;
12126
12127 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
12128
12129 OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
12130 OMP_CLAUSE_DECL (l) = unshare_expr (base);
12131 OMP_CLAUSE_SIZE (l) = size_int (1);
12132
12133 struct_map_to_clause->put (base, l);
12134
12135 /* On first iterating through the clause list, we insert the struct node
12136 just before the component access node that triggers the initial
12137 omp_accumulate_sibling_list call for a particular sibling list (and
12138 it then forms the first entry in that list). When reprocessing
12139 struct bases that are themselves component accesses, we insert the
12140 struct node on an off-side list to avoid inserting the new
12141 GOMP_MAP_STRUCT into the middle of the old one. */
12142 tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
12143
12144 if (has_descriptor)
12145 {
12146 tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12147 if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
12148 OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12149 tree sc = *insert_node_pos;
12150 OMP_CLAUSE_CHAIN (l) = desc;
12151 OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
12152 OMP_CLAUSE_CHAIN (desc) = sc;
12153 *insert_node_pos = l;
12154 }
12155 else if (attach_detach)
12156 {
12157 tree extra_node;
12158 tree alloc_node
12159 = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
12160 &extra_node);
12161 tree *tail;
12162 OMP_CLAUSE_CHAIN (l) = alloc_node;
12163
12164 if (extra_node)
12165 {
12166 OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
12167 OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12168 tail = &OMP_CLAUSE_CHAIN (extra_node);
12169 }
12170 else
12171 {
12172 OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
12173 tail = &OMP_CLAUSE_CHAIN (alloc_node);
12174 }
12175
12176 /* For OpenMP semantics, we don't want to implicitly allocate
12177 space for the pointer here for non-compute regions (e.g. "enter
12178 data"). A FRAGILE_P node is only being created so that
12179 omp-low.cc is able to rewrite the struct properly.
12180 For references (to pointers), we want to actually allocate the
12181 space for the reference itself in the sorted list following the
12182 struct node.
12183 For pointers, we want to allocate space if we had an explicit
12184 mapping of the attachment point, but not otherwise. */
12185 if (*fragile_p
12186 || (openmp
12187 && !target
12188 && attach_detach
12189 && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12190 && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12191 {
12192 if (!lang_GNU_Fortran ())
12193 /* In Fortran, pointers are dereferenced automatically, but may
12194 be unassociated. So we still want to allocate space for the
12195 pointer (as the base for an attach operation that should be
12196 present in the same directive's clause list also). */
12197 OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12198 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12199 }
12200
12201 *insert_node_pos = l;
12202
12203 if (reprocessing_struct)
12204 {
12205 /* When reprocessing a struct node group used as the base of a
12206 subcomponent access, if we have a reference-to-pointer base,
12207 we will see:
12208 struct(**ptr) attach(*ptr)
12209 whereas for a non-reprocess-struct group, we see, e.g.:
12210 tofrom(**ptr) attach(*ptr) attach(ptr)
12211 and we create the "alloc" for the second "attach", i.e.
12212 for the reference itself. When reprocessing a struct group we
12213 thus change the pointer attachment into a reference attachment
12214 by stripping the indirection. (The attachment of the
12215 referenced pointer must happen elsewhere, either on the same
12216 directive, or otherwise.) */
12217 tree adecl = OMP_CLAUSE_DECL (alloc_node);
12218
12219 if ((TREE_CODE (adecl) == INDIRECT_REF
12220 || (TREE_CODE (adecl) == MEM_REF
12221 && integer_zerop (TREE_OPERAND (adecl, 1))))
12222 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
12223 == REFERENCE_TYPE)
12224 && (TREE_CODE (TREE_TYPE (TREE_TYPE
12225 (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
12226 OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
12227
12228 *added_tail = tail;
12229 }
12230 }
12231 else
12232 {
12233 gcc_assert (*grp_start_p == grp_end);
12234 if (reprocessing_struct)
12235 {
12236 /* If we don't have an attach/detach node, this is a
12237 "target data" directive or similar, not an offload region.
12238 Synthesize an "alloc" node using just the initiating
12239 GOMP_MAP_STRUCT decl. */
12240 gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12241 || code == OACC_EXIT_DATA)
12242 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12243 tree alloc_node
12244 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12245 OMP_CLAUSE_MAP);
12246 OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12247 OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12248 OMP_CLAUSE_SIZE (alloc_node)
12249 = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12250
12251 OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
12252 OMP_CLAUSE_CHAIN (l) = alloc_node;
12253 *insert_node_pos = l;
12254 *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
12255 }
12256 else
12257 grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
12258 }
12259
12260 unsigned last_access = base_token + 1;
12261
12262 while (last_access + 1 < addr_tokens.length ()
12263 && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
12264 last_access++;
12265
12266 if ((region_type & ORT_TARGET)
12267 && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
12268 {
12269 bool base_ref = false;
12270 access_method_kinds access_kind
12271 = addr_tokens[last_access]->u.access_kind;
12272
12273 switch (access_kind)
12274 {
12275 case ACCESS_DIRECT:
12276 case ACCESS_INDEXED_ARRAY:
12277 return NULL;
12278
12279 case ACCESS_REF:
12280 case ACCESS_REF_TO_POINTER:
12281 case ACCESS_REF_TO_POINTER_OFFSET:
12282 case ACCESS_INDEXED_REF_TO_ARRAY:
12283 base_ref = true;
12284 break;
12285
12286 default:
12287 ;
12288 }
12289 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12290 OMP_CLAUSE_MAP);
12291 enum gomp_map_kind mkind;
12292 omp_mapping_group *decl_group;
12293 tree use_base;
12294 switch (access_kind)
12295 {
12296 case ACCESS_POINTER:
12297 case ACCESS_POINTER_OFFSET:
12298 use_base = addr_tokens[last_access]->expr;
12299 break;
12300 case ACCESS_REF_TO_POINTER:
12301 case ACCESS_REF_TO_POINTER_OFFSET:
12302 use_base
12303 = build_fold_indirect_ref (addr_tokens[last_access]->expr);
12304 break;
12305 default:
12306 use_base = addr_tokens[base_token]->expr;
12307 }
12308 bool mapped_to_p
12309 = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
12310 true, false, true);
12311 if (addr_tokens[base_token]->type == STRUCTURE_BASE
12312 && DECL_P (addr_tokens[last_access]->expr)
12313 && !mapped_to_p)
12314 mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
12315 : GOMP_MAP_FIRSTPRIVATE_POINTER;
12316 else
12317 mkind = GOMP_MAP_ATTACH_DETACH;
12318
12319 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
12320 /* If we have a reference to pointer base, we want to attach the
12321 pointer here, not the reference. The reference attachment happens
12322 elsewhere. */
12323 bool ref_to_ptr
12324 = (access_kind == ACCESS_REF_TO_POINTER
12325 || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
12326 tree sdecl = addr_tokens[last_access]->expr;
12327 tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
12328 : sdecl;
12329 /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
12330 want to use the reference itself for the decl, but we
12331 still want to use the pointer to calculate the bias. */
12332 OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
12333 ? sdecl_ptr : sdecl;
12334 sdecl = sdecl_ptr;
12335 tree baddr = build_fold_addr_expr (base);
12336 baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12337 ptrdiff_type_node, baddr);
12338 tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12339 ptrdiff_type_node, sdecl);
12340 OMP_CLAUSE_SIZE (c2)
12341 = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
12342 ptrdiff_type_node, baddr, decladdr);
12343 /* Insert after struct node. */
12344 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
12345 OMP_CLAUSE_CHAIN (l) = c2;
12346
12347 if (addr_tokens[base_token]->type == STRUCTURE_BASE
12348 && (addr_tokens[base_token]->u.structure_base_kind
12349 == BASE_COMPONENT_EXPR)
12350 && mkind == GOMP_MAP_ATTACH_DETACH
12351 && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
12352 {
12353 *inner = insert_node_pos;
12354 if (openmp)
12355 *fragile_p = true;
12356 return NULL;
12357 }
12358 }
12359
12360 if (addr_tokens[base_token]->type == STRUCTURE_BASE
12361 && (addr_tokens[base_token]->u.structure_base_kind
12362 == BASE_COMPONENT_EXPR)
12363 && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
12364 *inner = insert_node_pos;
12365
12366 return NULL;
12367 }
12368 else if (struct_map_to_clause)
12369 {
12370 tree *osc = struct_map_to_clause->get (base);
12371 tree *sc = NULL, *scp = NULL;
12372 bool unordered = false;
12373
12374 if (osc && OMP_CLAUSE_MAP_KIND (*osc) == GOMP_MAP_STRUCT_UNORD)
12375 unordered = true;
12376
12377 unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
12378 sc = &OMP_CLAUSE_CHAIN (*osc);
12379 /* The struct mapping might be immediately followed by a
12380 FIRSTPRIVATE_POINTER, FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
12381 if it's an indirect access or a reference, or if the structure base
12382 is not a decl. The FIRSTPRIVATE_* nodes are removed in omp-low.cc
12383 after they have been processed there, and ATTACH_DETACH nodes are
12384 recomputed and moved out of the GOMP_MAP_STRUCT construct once
12385 sibling list building is complete. */
12386 if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
12387 || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
12388 || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
12389 sc = &OMP_CLAUSE_CHAIN (*sc);
12390 for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
12391 if (attach_detach && sc == grp_start_p)
12392 break;
12393 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
12394 && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
12395 && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
12396 break;
12397 else
12398 {
12399 tree sc_decl = OMP_CLAUSE_DECL (*sc);
12400 poly_offset_int offset;
12401 poly_int64 bitpos;
12402
12403 if (TREE_CODE (sc_decl) == ARRAY_REF)
12404 {
12405 while (TREE_CODE (sc_decl) == ARRAY_REF)
12406 sc_decl = TREE_OPERAND (sc_decl, 0);
12407 if (TREE_CODE (sc_decl) != COMPONENT_REF
12408 || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
12409 break;
12410 }
12411 else if (INDIRECT_REF_P (sc_decl)
12412 && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
12413 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
12414 == REFERENCE_TYPE))
12415 sc_decl = TREE_OPERAND (sc_decl, 0);
12416
12417 bool variable_offset2;
12418 tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
12419 &variable_offset2);
12420 if (!base2 || !operand_equal_p (base2, base, 0))
12421 break;
12422 if (scp)
12423 continue;
12424 if (variable_offset2)
12425 {
12426 OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
12427 unordered = true;
12428 break;
12429 }
12430 else if ((region_type & ORT_ACC) != 0)
12431 {
12432 /* For OpenACC, allow (ignore) duplicate struct accesses in
12433 the middle of a mapping clause, e.g. "mystruct->foo" in:
12434 copy(mystruct->foo->bar) copy(mystruct->foo->qux). */
12435 if (reprocessing_struct
12436 && known_eq (coffset, offset)
12437 && known_eq (cbitpos, bitpos))
12438 return NULL;
12439 }
12440 else if (known_eq (coffset, offset)
12441 && known_eq (cbitpos, bitpos))
12442 {
12443 /* Having two struct members at the same offset doesn't work,
12444 so make sure we don't. (We're allowed to ignore this.
12445 Should we report the error?) */
12446 /*error_at (OMP_CLAUSE_LOCATION (grp_end),
12447 "duplicate struct member %qE in map clauses",
12448 OMP_CLAUSE_DECL (grp_end));*/
12449 return NULL;
12450 }
12451 if (maybe_lt (coffset, offset)
12452 || (known_eq (coffset, offset)
12453 && maybe_lt (cbitpos, bitpos)))
12454 {
12455 if (attach_detach)
12456 scp = sc;
12457 else
12458 break;
12459 }
12460 }
12461
12462 /* If this is an unordered struct, just insert the new element at the
12463 end of the list. */
12464 if (unordered)
12465 {
12466 for (; i < elems; i++)
12467 sc = &OMP_CLAUSE_CHAIN (*sc);
12468 scp = NULL;
12469 }
12470
12471 OMP_CLAUSE_SIZE (*osc)
12472 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
12473
12474 if (reprocessing_struct)
12475 {
12476 /* If we're reprocessing a struct node, we don't want to do most of
12477 the list manipulation below. We only need to handle the (pointer
12478 or reference) attach/detach case. */
12479 tree extra_node, alloc_node;
12480 if (has_descriptor)
12481 gcc_unreachable ();
12482 else if (attach_detach)
12483 alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12484 grp_end, &extra_node);
12485 else
12486 {
12487 /* If we don't have an attach/detach node, this is a
12488 "target data" directive or similar, not an offload region.
12489 Synthesize an "alloc" node using just the initiating
12490 GOMP_MAP_STRUCT decl. */
12491 gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12492 || code == OACC_EXIT_DATA)
12493 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12494 alloc_node
12495 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12496 OMP_CLAUSE_MAP);
12497 OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12498 OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12499 OMP_CLAUSE_SIZE (alloc_node)
12500 = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12501 }
12502
12503 if (scp)
12504 omp_siblist_insert_node_after (alloc_node, scp);
12505 else
12506 {
12507 tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
12508 if (sc == *added_tail)
12509 *added_tail = new_end;
12510 }
12511
12512 return NULL;
12513 }
12514
12515 if (has_descriptor)
12516 {
12517 tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12518 if (code == OMP_TARGET_EXIT_DATA
12519 || code == OACC_EXIT_DATA)
12520 OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12521 omp_siblist_move_node_after (desc,
12522 &OMP_CLAUSE_CHAIN (*grp_start_p),
12523 scp ? scp : sc);
12524 }
12525 else if (attach_detach)
12526 {
12527 tree cl = NULL_TREE, extra_node;
12528 tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12529 grp_end, &extra_node);
12530 tree *tail_chain = NULL;
12531
12532 if (*fragile_p
12533 || (openmp
12534 && !target
12535 && attach_detach
12536 && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12537 && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12538 {
12539 if (!lang_GNU_Fortran ())
12540 OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12541 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12542 }
12543
12544 /* Here, we have:
12545
12546 grp_end : the last (or only) node in this group.
12547 grp_start_p : pointer to the first node in a pointer mapping group
12548 up to and including GRP_END.
12549 sc : pointer to the chain for the end of the struct component
12550 list.
12551 scp : pointer to the chain for the sorted position at which we
12552 should insert in the middle of the struct component list
12553 (else NULL to insert at end).
12554 alloc_node : the "alloc" node for the structure (pointer-type)
12555 component. We insert at SCP (if present), else SC
12556 (the end of the struct component list).
12557 extra_node : a newly-synthesized node for an additional indirect
12558 pointer mapping or a Fortran pointer set, if needed.
12559 cl : first node to prepend before grp_start_p.
12560 tail_chain : pointer to chain of last prepended node.
12561
12562 The general idea is we move the nodes for this struct mapping
12563 together: the alloc node goes into the sorted list directly after
12564 the struct mapping, and any extra nodes (together with the nodes
12565 mapping arrays pointed to by struct components) get moved after
12566 that list. When SCP is NULL, we insert the nodes at SC, i.e. at
12567 the end of the struct component mapping list. It's important that
12568 the alloc_node comes first in that case because it's part of the
12569 sorted component mapping list (but subsequent nodes are not!). */
12570
12571 if (scp)
12572 omp_siblist_insert_node_after (alloc_node, scp);
12573
12574 /* Make [cl,tail_chain] a list of the alloc node (if we haven't
12575 already inserted it) and the extra_node (if it is present). The
12576 list can be empty if we added alloc_node above and there is no
12577 extra node. */
12578 if (scp && extra_node)
12579 {
12580 cl = extra_node;
12581 tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
12582 }
12583 else if (extra_node)
12584 {
12585 OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12586 cl = alloc_node;
12587 tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
12588 }
12589 else if (!scp)
12590 {
12591 cl = alloc_node;
12592 tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
12593 }
12594
12595 continue_at
12596 = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
12597 grp_start_p, grp_end,
12598 sc)
12599 : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
12600 }
12601 else if (*sc != grp_end)
12602 {
12603 gcc_assert (*grp_start_p == grp_end);
12604
12605 /* We are moving the current node back to a previous struct node:
12606 the node that used to point to the current node will now point to
12607 the next node. */
12608 continue_at = grp_start_p;
12609 /* In the non-pointer case, the mapping clause itself is moved into
12610 the correct position in the struct component list, which in this
12611 case is just SC. */
12612 omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
12613 }
12614 }
12615 return continue_at;
12616 }
12617
12618 /* Scan through GROUPS, and create sorted structure sibling lists without
12619 gimplifying. */
12620
12621 static bool
12622 omp_build_struct_sibling_lists (enum tree_code code,
12623 enum omp_region_type region_type,
12624 vec<omp_mapping_group> *groups,
12625 hash_map<tree_operand_hash_no_se,
12626 omp_mapping_group *> **grpmap,
12627 tree *list_p)
12628 {
12629 using namespace omp_addr_tokenizer;
12630 unsigned i;
12631 omp_mapping_group *grp;
12632 hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
12633 bool success = true;
12634 tree *new_next = NULL;
12635 tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
12636 tree added_nodes = NULL_TREE;
12637 tree *added_tail = &added_nodes;
12638 auto_vec<omp_mapping_group> pre_hwm_groups;
12639
12640 FOR_EACH_VEC_ELT (*groups, i, grp)
12641 {
12642 tree c = grp->grp_end;
12643 tree decl = OMP_CLAUSE_DECL (c);
12644 tree grp_end = grp->grp_end;
12645 auto_vec<omp_addr_token *> addr_tokens;
12646 tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
12647
12648 if (new_next && !grp->reprocess_struct)
12649 grp->grp_start = new_next;
12650
12651 new_next = NULL;
12652
12653 tree *grp_start_p = grp->grp_start;
12654
12655 if (DECL_P (decl))
12656 continue;
12657
12658 /* Skip groups we marked for deletion in
12659 {omp,oacc}_resolve_clause_dependencies. */
12660 if (grp->deleted)
12661 continue;
12662
12663 if (OMP_CLAUSE_CHAIN (*grp_start_p)
12664 && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12665 {
12666 /* Don't process an array descriptor that isn't inside a derived type
12667 as a struct (the GOMP_MAP_POINTER following will have the form
12668 "var.data", but such mappings are handled specially). */
12669 tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
12670 if (omp_map_clause_descriptor_p (grpmid)
12671 && DECL_P (OMP_CLAUSE_DECL (grpmid)))
12672 continue;
12673 }
12674
12675 tree expr = decl;
12676
12677 while (TREE_CODE (expr) == ARRAY_REF)
12678 expr = TREE_OPERAND (expr, 0);
12679
12680 if (!omp_parse_expr (addr_tokens, expr))
12681 continue;
12682
12683 omp_addr_token *last_token
12684 = omp_first_chained_access_token (addr_tokens);
12685
12686 /* A mapping of a reference to a pointer member that doesn't specify an
12687 array section, etc., like this:
12688 *mystruct.ref_to_ptr
12689 should not be processed by the struct sibling-list handling code --
12690 it just transfers the referenced pointer.
12691
12692 In contrast, the quite similar-looking construct:
12693 *mystruct.ptr
12694 which is equivalent to e.g.
12695 mystruct.ptr[0]
12696 *does* trigger sibling-list processing.
12697
12698 An exception for the former case is for "fragile" groups where the
12699 reference itself is not handled otherwise; this is subject to special
12700 handling in omp_accumulate_sibling_list also. */
12701
12702 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
12703 && last_token->type == ACCESS_METHOD
12704 && last_token->u.access_kind == ACCESS_REF
12705 && !grp->fragile)
12706 continue;
12707
12708 tree d = decl;
12709 if (TREE_CODE (d) == ARRAY_REF)
12710 {
12711 while (TREE_CODE (d) == ARRAY_REF)
12712 d = TREE_OPERAND (d, 0);
12713 if (TREE_CODE (d) == COMPONENT_REF
12714 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
12715 decl = d;
12716 }
12717 if (d == decl
12718 && INDIRECT_REF_P (decl)
12719 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
12720 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
12721 == REFERENCE_TYPE)
12722 && (OMP_CLAUSE_MAP_KIND (c)
12723 != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
12724 decl = TREE_OPERAND (decl, 0);
12725
12726 STRIP_NOPS (decl);
12727
12728 if (TREE_CODE (decl) != COMPONENT_REF)
12729 continue;
12730
12731 /* If we're mapping the whole struct in another node, skip adding this
12732 node to a sibling list. */
12733 omp_mapping_group *wholestruct;
12734 if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
12735 &wholestruct))
12736 continue;
12737
12738 if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
12739 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
12740 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
12741 && code != OACC_UPDATE
12742 && code != OMP_TARGET_UPDATE)
12743 {
12744 if (error_operand_p (decl))
12745 {
12746 success = false;
12747 goto error_out;
12748 }
12749
12750 tree stype = TREE_TYPE (decl);
12751 if (TREE_CODE (stype) == REFERENCE_TYPE)
12752 stype = TREE_TYPE (stype);
12753 if (TYPE_SIZE_UNIT (stype) == NULL
12754 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
12755 {
12756 error_at (OMP_CLAUSE_LOCATION (c),
12757 "mapping field %qE of variable length "
12758 "structure", OMP_CLAUSE_DECL (c));
12759 success = false;
12760 goto error_out;
12761 }
12762
12763 tree *inner = NULL;
12764 bool fragile_p = grp->fragile;
12765
12766 new_next
12767 = omp_accumulate_sibling_list (region_type, code,
12768 struct_map_to_clause, *grpmap,
12769 grp_start_p, grp_end, addr_tokens,
12770 &inner, &fragile_p,
12771 grp->reprocess_struct, &added_tail);
12772
12773 if (inner)
12774 {
12775 omp_mapping_group newgrp;
12776 newgrp.grp_start = inner;
12777 if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
12778 == GOMP_MAP_ATTACH_DETACH)
12779 newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
12780 else
12781 newgrp.grp_end = *inner;
12782 newgrp.mark = UNVISITED;
12783 newgrp.sibling = NULL;
12784 newgrp.deleted = false;
12785 newgrp.reprocess_struct = true;
12786 newgrp.fragile = fragile_p;
12787 newgrp.next = NULL;
12788 groups->safe_push (newgrp);
12789
12790 /* !!! Growing GROUPS might invalidate the pointers in the group
12791 map. Rebuild it here. This is a bit inefficient, but
12792 shouldn't happen very often. */
12793 delete (*grpmap);
12794 *grpmap
12795 = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
12796 sentinel);
12797 }
12798 }
12799 }
12800
12801 /* Delete groups marked for deletion above. At this point the order of the
12802 groups may no longer correspond to the order of the underlying list,
12803 which complicates this a little. First clear out OMP_CLAUSE_DECL for
12804 deleted nodes... */
12805
12806 FOR_EACH_VEC_ELT (*groups, i, grp)
12807 if (grp->deleted)
12808 for (tree d = *grp->grp_start;
12809 d != OMP_CLAUSE_CHAIN (grp->grp_end);
12810 d = OMP_CLAUSE_CHAIN (d))
12811 OMP_CLAUSE_DECL (d) = NULL_TREE;
12812
12813 /* ...then sweep through the list removing the now-empty nodes. */
12814
12815 tail = list_p;
12816 while (*tail)
12817 {
12818 if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
12819 && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
12820 *tail = OMP_CLAUSE_CHAIN (*tail);
12821 else
12822 tail = &OMP_CLAUSE_CHAIN (*tail);
12823 }
12824
12825 /* Tack on the struct nodes added during nested struct reprocessing. */
12826 if (added_nodes)
12827 {
12828 *tail = added_nodes;
12829 tail = added_tail;
12830 }
12831
12832 /* Now we have finished building the struct sibling lists, reprocess
12833 newly-added "attach" nodes: we need the address of the first
12834 mapped element of each struct sibling list for the bias of the attach
12835 operation -- not necessarily the base address of the whole struct. */
12836 if (struct_map_to_clause)
12837 for (hash_map<tree_operand_hash, tree>::iterator iter
12838 = struct_map_to_clause->begin ();
12839 iter != struct_map_to_clause->end ();
12840 ++iter)
12841 {
12842 tree struct_node = (*iter).second;
12843 gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
12844 tree attach = OMP_CLAUSE_CHAIN (struct_node);
12845
12846 if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
12847 || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
12848 continue;
12849
12850 OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
12851
12852 /* Sanity check: the standalone attach node will not work if we have
12853 an "enter data" operation (because for those, variables need to be
12854 mapped separately and attach nodes must be grouped together with the
12855 base they attach to). We should only have created the
12856 ATTACH_DETACH node after GOMP_MAP_STRUCT for a target region, so
12857 this should never be true. */
12858 gcc_assert ((region_type & ORT_TARGET) != 0);
12859
12860 /* This is the first sorted node in the struct sibling list. Use it
12861 to recalculate the correct bias to use.
12862 (&first_node - attach_decl).
12863 For GOMP_MAP_STRUCT_UNORD, we need e.g. the
12864 min(min(min(first,second),third),fourth) element, because the
12865 elements aren't in any particular order. */
12866 tree lowest_addr;
12867 if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
12868 {
12869 tree first_node = OMP_CLAUSE_CHAIN (attach);
12870 unsigned HOST_WIDE_INT num_mappings
12871 = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
12872 lowest_addr = OMP_CLAUSE_DECL (first_node);
12873 lowest_addr = build_fold_addr_expr (lowest_addr);
12874 lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
12875 tree next_node = OMP_CLAUSE_CHAIN (first_node);
12876 while (num_mappings > 1)
12877 {
12878 tree tmp = OMP_CLAUSE_DECL (next_node);
12879 tmp = build_fold_addr_expr (tmp);
12880 tmp = fold_convert (pointer_sized_int_node, tmp);
12881 lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
12882 lowest_addr, tmp);
12883 next_node = OMP_CLAUSE_CHAIN (next_node);
12884 num_mappings--;
12885 }
12886 lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
12887 }
12888 else
12889 {
12890 tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
12891 first_node = build_fold_addr_expr (first_node);
12892 lowest_addr = fold_convert (ptrdiff_type_node, first_node);
12893 }
12894 tree attach_decl = OMP_CLAUSE_DECL (attach);
12895 attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
12896 OMP_CLAUSE_SIZE (attach)
12897 = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
12898 attach_decl);
12899
12900 /* Remove GOMP_MAP_ATTACH node from after struct node. */
12901 OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
12902 /* ...and re-insert it at the end of our clause list. */
12903 *tail = attach;
12904 OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
12905 tail = &OMP_CLAUSE_CHAIN (attach);
12906 }
12907
12908 error_out:
12909 if (struct_map_to_clause)
12910 delete struct_map_to_clause;
12911
12912 return success;
12913 }
12914
12915 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
12916 and previous omp contexts. */
12917
12918 static void
12919 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
12920 enum omp_region_type region_type,
12921 enum tree_code code)
12922 {
12923 using namespace omp_addr_tokenizer;
12924 struct gimplify_omp_ctx *ctx, *outer_ctx;
12925 tree c;
12926 tree *orig_list_p = list_p;
12927 int handled_depend_iterators = -1;
12928 int nowait = -1;
12929
12930 ctx = new_omp_context (region_type);
12931 ctx->code = code;
12932 outer_ctx = ctx->outer_context;
12933 if (code == OMP_TARGET)
12934 {
12935 if (!lang_GNU_Fortran ())
12936 ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
12937 ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
12938 ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
12939 ? GOVD_MAP : GOVD_FIRSTPRIVATE);
12940 }
12941 if (!lang_GNU_Fortran ())
12942 switch (code)
12943 {
12944 case OMP_TARGET:
12945 case OMP_TARGET_DATA:
12946 case OMP_TARGET_ENTER_DATA:
12947 case OMP_TARGET_EXIT_DATA:
12948 case OACC_DECLARE:
12949 case OACC_HOST_DATA:
12950 case OACC_PARALLEL:
12951 case OACC_KERNELS:
12952 ctx->target_firstprivatize_array_bases = true;
12953 default:
12954 break;
12955 }
12956
12957 vec<omp_mapping_group> *groups = NULL;
12958 hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
12959 unsigned grpnum = 0;
12960 tree *grp_start_p = NULL, grp_end = NULL_TREE;
12961
12962 if (code == OMP_TARGET
12963 || code == OMP_TARGET_DATA
12964 || code == OMP_TARGET_ENTER_DATA
12965 || code == OMP_TARGET_EXIT_DATA
12966 || code == OACC_DATA
12967 || code == OACC_KERNELS
12968 || code == OACC_PARALLEL
12969 || code == OACC_SERIAL
12970 || code == OACC_ENTER_DATA
12971 || code == OACC_EXIT_DATA
12972 || code == OACC_UPDATE
12973 || code == OACC_DECLARE)
12974 {
12975 groups = omp_gather_mapping_groups (list_p);
12976
12977 if (groups)
12978 grpmap = omp_index_mapping_groups (groups);
12979 }
12980
12981 while ((c = *list_p) != NULL)
12982 {
12983 bool remove = false;
12984 bool notice_outer = true;
12985 bool map_descriptor;
12986 const char *check_non_private = NULL;
12987 unsigned int flags;
12988 tree decl;
12989 auto_vec<omp_addr_token *, 10> addr_tokens;
12990 tree op = NULL_TREE;
12991 location_t loc = OMP_CLAUSE_LOCATION (c);
12992
12993 if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
12994 {
12995 grp_start_p = NULL;
12996 grp_end = NULL_TREE;
12997 }
12998
12999 if (code == OMP_TARGET
13000 || code == OMP_TARGET_DATA
13001 || code == OMP_TARGET_ENTER_DATA
13002 || code == OMP_TARGET_EXIT_DATA)
13003 /* Do some target-specific type checks for map operands. */
13004 switch (OMP_CLAUSE_CODE (c))
13005 {
13006 case OMP_CLAUSE_MAP:
13007 op = OMP_CLAUSE_OPERAND (c, 0);
13008 verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
13009 break;
13010 case OMP_CLAUSE_PRIVATE:
13011 op = OMP_CLAUSE_OPERAND (c, 0);
13012 verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
13013 break;
13014 case OMP_CLAUSE_FIRSTPRIVATE:
13015 op = OMP_CLAUSE_OPERAND (c, 0);
13016 verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
13017 break;
13018 case OMP_CLAUSE_IS_DEVICE_PTR:
13019 case OMP_CLAUSE_USE_DEVICE_ADDR:
13020 case OMP_CLAUSE_USE_DEVICE_PTR:
13021 case OMP_CLAUSE_HAS_DEVICE_ADDR:
13022 op = OMP_CLAUSE_OPERAND (c, 0);
13023 verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
13024 break;
13025 default:
13026 break;
13027 }
13028
13029 switch (OMP_CLAUSE_CODE (c))
13030 {
13031 case OMP_CLAUSE_PRIVATE:
13032 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
13033 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
13034 {
13035 flags |= GOVD_PRIVATE_OUTER_REF;
13036 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
13037 }
13038 else
13039 notice_outer = false;
13040 goto do_add;
13041 case OMP_CLAUSE_SHARED:
13042 flags = GOVD_SHARED | GOVD_EXPLICIT;
13043 goto do_add;
13044 case OMP_CLAUSE_FIRSTPRIVATE:
13045 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13046 check_non_private = "firstprivate";
13047 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13048 {
13049 gcc_assert (code == OMP_TARGET);
13050 flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
13051 }
13052 goto do_add;
13053 case OMP_CLAUSE_LASTPRIVATE:
13054 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13055 switch (code)
13056 {
13057 case OMP_DISTRIBUTE:
13058 error_at (OMP_CLAUSE_LOCATION (c),
13059 "conditional %<lastprivate%> clause on "
13060 "%qs construct", "distribute");
13061 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13062 break;
13063 case OMP_TASKLOOP:
13064 error_at (OMP_CLAUSE_LOCATION (c),
13065 "conditional %<lastprivate%> clause on "
13066 "%qs construct", "taskloop");
13067 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13068 break;
13069 default:
13070 break;
13071 }
13072 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
13073 if (code != OMP_LOOP)
13074 check_non_private = "lastprivate";
13075 decl = OMP_CLAUSE_DECL (c);
13076 if (error_operand_p (decl))
13077 goto do_add;
13078 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
13079 && !lang_hooks.decls.omp_scalar_p (decl, true))
13080 {
13081 error_at (OMP_CLAUSE_LOCATION (c),
13082 "non-scalar variable %qD in conditional "
13083 "%<lastprivate%> clause", decl);
13084 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13085 }
13086 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13087 flags |= GOVD_LASTPRIVATE_CONDITIONAL;
13088 omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
13089 false);
13090 goto do_add;
13091 case OMP_CLAUSE_REDUCTION:
13092 if (OMP_CLAUSE_REDUCTION_TASK (c))
13093 {
13094 if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13095 {
13096 if (nowait == -1)
13097 nowait = omp_find_clause (*list_p,
13098 OMP_CLAUSE_NOWAIT) != NULL_TREE;
13099 if (nowait
13100 && (outer_ctx == NULL
13101 || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
13102 {
13103 error_at (OMP_CLAUSE_LOCATION (c),
13104 "%<task%> reduction modifier on a construct "
13105 "with a %<nowait%> clause");
13106 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13107 }
13108 }
13109 else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
13110 {
13111 error_at (OMP_CLAUSE_LOCATION (c),
13112 "invalid %<task%> reduction modifier on construct "
13113 "other than %<parallel%>, %qs, %<sections%> or "
13114 "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
13115 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13116 }
13117 }
13118 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
13119 switch (code)
13120 {
13121 case OMP_SECTIONS:
13122 error_at (OMP_CLAUSE_LOCATION (c),
13123 "%<inscan%> %<reduction%> clause on "
13124 "%qs construct", "sections");
13125 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13126 break;
13127 case OMP_PARALLEL:
13128 error_at (OMP_CLAUSE_LOCATION (c),
13129 "%<inscan%> %<reduction%> clause on "
13130 "%qs construct", "parallel");
13131 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13132 break;
13133 case OMP_TEAMS:
13134 error_at (OMP_CLAUSE_LOCATION (c),
13135 "%<inscan%> %<reduction%> clause on "
13136 "%qs construct", "teams");
13137 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13138 break;
13139 case OMP_TASKLOOP:
13140 error_at (OMP_CLAUSE_LOCATION (c),
13141 "%<inscan%> %<reduction%> clause on "
13142 "%qs construct", "taskloop");
13143 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13144 break;
13145 case OMP_SCOPE:
13146 error_at (OMP_CLAUSE_LOCATION (c),
13147 "%<inscan%> %<reduction%> clause on "
13148 "%qs construct", "scope");
13149 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13150 break;
13151 default:
13152 break;
13153 }
13154 /* FALLTHRU */
13155 case OMP_CLAUSE_IN_REDUCTION:
13156 case OMP_CLAUSE_TASK_REDUCTION:
13157 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
13158 /* OpenACC permits reductions on private variables. */
13159 if (!(region_type & ORT_ACC)
13160 /* taskgroup is actually not a worksharing region. */
13161 && code != OMP_TASKGROUP)
13162 check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
13163 decl = OMP_CLAUSE_DECL (c);
13164 if (TREE_CODE (decl) == MEM_REF)
13165 {
13166 tree type = TREE_TYPE (decl);
13167 bool saved_into_ssa = gimplify_ctxp->into_ssa;
13168 gimplify_ctxp->into_ssa = false;
13169 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
13170 NULL, is_gimple_val, fb_rvalue, false)
13171 == GS_ERROR)
13172 {
13173 gimplify_ctxp->into_ssa = saved_into_ssa;
13174 remove = true;
13175 break;
13176 }
13177 gimplify_ctxp->into_ssa = saved_into_ssa;
13178 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13179 if (DECL_P (v))
13180 {
13181 omp_firstprivatize_variable (ctx, v);
13182 omp_notice_variable (ctx, v, true);
13183 }
13184 decl = TREE_OPERAND (decl, 0);
13185 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
13186 {
13187 gimplify_ctxp->into_ssa = false;
13188 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
13189 NULL, is_gimple_val, fb_rvalue, false)
13190 == GS_ERROR)
13191 {
13192 gimplify_ctxp->into_ssa = saved_into_ssa;
13193 remove = true;
13194 break;
13195 }
13196 gimplify_ctxp->into_ssa = saved_into_ssa;
13197 v = TREE_OPERAND (decl, 1);
13198 if (DECL_P (v))
13199 {
13200 omp_firstprivatize_variable (ctx, v);
13201 omp_notice_variable (ctx, v, true);
13202 }
13203 decl = TREE_OPERAND (decl, 0);
13204 }
13205 if (TREE_CODE (decl) == ADDR_EXPR
13206 || TREE_CODE (decl) == INDIRECT_REF)
13207 decl = TREE_OPERAND (decl, 0);
13208 }
13209 goto do_add_decl;
13210 case OMP_CLAUSE_LINEAR:
13211 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
13212 is_gimple_val, fb_rvalue) == GS_ERROR)
13213 {
13214 remove = true;
13215 break;
13216 }
13217 else
13218 {
13219 if (code == OMP_SIMD
13220 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13221 {
13222 struct gimplify_omp_ctx *octx = outer_ctx;
13223 if (octx
13224 && octx->region_type == ORT_WORKSHARE
13225 && octx->combined_loop
13226 && !octx->distribute)
13227 {
13228 if (octx->outer_context
13229 && (octx->outer_context->region_type
13230 == ORT_COMBINED_PARALLEL))
13231 octx = octx->outer_context->outer_context;
13232 else
13233 octx = octx->outer_context;
13234 }
13235 if (octx
13236 && octx->region_type == ORT_WORKSHARE
13237 && octx->combined_loop
13238 && octx->distribute)
13239 {
13240 error_at (OMP_CLAUSE_LOCATION (c),
13241 "%<linear%> clause for variable other than "
13242 "loop iterator specified on construct "
13243 "combined with %<distribute%>");
13244 remove = true;
13245 break;
13246 }
13247 }
13248 /* For combined #pragma omp parallel for simd, need to put
13249 lastprivate and perhaps firstprivate too on the
13250 parallel. Similarly for #pragma omp for simd. */
13251 struct gimplify_omp_ctx *octx = outer_ctx;
13252 bool taskloop_seen = false;
13253 decl = NULL_TREE;
13254 do
13255 {
13256 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13257 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13258 break;
13259 decl = OMP_CLAUSE_DECL (c);
13260 if (error_operand_p (decl))
13261 {
13262 decl = NULL_TREE;
13263 break;
13264 }
13265 flags = GOVD_SEEN;
13266 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13267 flags |= GOVD_FIRSTPRIVATE;
13268 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13269 flags |= GOVD_LASTPRIVATE;
13270 if (octx
13271 && octx->region_type == ORT_WORKSHARE
13272 && octx->combined_loop)
13273 {
13274 if (octx->outer_context
13275 && (octx->outer_context->region_type
13276 == ORT_COMBINED_PARALLEL))
13277 octx = octx->outer_context;
13278 else if (omp_check_private (octx, decl, false))
13279 break;
13280 }
13281 else if (octx
13282 && (octx->region_type & ORT_TASK) != 0
13283 && octx->combined_loop)
13284 taskloop_seen = true;
13285 else if (octx
13286 && octx->region_type == ORT_COMBINED_PARALLEL
13287 && ((ctx->region_type == ORT_WORKSHARE
13288 && octx == outer_ctx)
13289 || taskloop_seen))
13290 flags = GOVD_SEEN | GOVD_SHARED;
13291 else if (octx
13292 && ((octx->region_type & ORT_COMBINED_TEAMS)
13293 == ORT_COMBINED_TEAMS))
13294 flags = GOVD_SEEN | GOVD_SHARED;
13295 else if (octx
13296 && octx->region_type == ORT_COMBINED_TARGET)
13297 {
13298 if (flags & GOVD_LASTPRIVATE)
13299 flags = GOVD_SEEN | GOVD_MAP;
13300 }
13301 else
13302 break;
13303 splay_tree_node on
13304 = splay_tree_lookup (octx->variables,
13305 (splay_tree_key) decl);
13306 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
13307 {
13308 octx = NULL;
13309 break;
13310 }
13311 omp_add_variable (octx, decl, flags);
13312 if (octx->outer_context == NULL)
13313 break;
13314 octx = octx->outer_context;
13315 }
13316 while (1);
13317 if (octx
13318 && decl
13319 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13320 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
13321 omp_notice_variable (octx, decl, true);
13322 }
13323 flags = GOVD_LINEAR | GOVD_EXPLICIT;
13324 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13325 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13326 {
13327 notice_outer = false;
13328 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
13329 }
13330 goto do_add;
13331
13332 case OMP_CLAUSE_MAP:
13333 if (!grp_start_p)
13334 {
13335 grp_start_p = list_p;
13336 grp_end = (*groups)[grpnum].grp_end;
13337 grpnum++;
13338 }
13339 decl = OMP_CLAUSE_DECL (c);
13340
13341 if (error_operand_p (decl))
13342 {
13343 remove = true;
13344 break;
13345 }
13346
13347 if (!omp_parse_expr (addr_tokens, decl))
13348 {
13349 remove = true;
13350 break;
13351 }
13352
13353 if (remove)
13354 break;
13355 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
13356 {
13357 struct gimplify_omp_ctx *octx;
13358 for (octx = outer_ctx; octx; octx = octx->outer_context)
13359 {
13360 if (octx->region_type != ORT_ACC_HOST_DATA)
13361 break;
13362 splay_tree_node n2
13363 = splay_tree_lookup (octx->variables,
13364 (splay_tree_key) decl);
13365 if (n2)
13366 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
13367 "declared in enclosing %<host_data%> region",
13368 DECL_NAME (decl));
13369 }
13370 }
13371
13372 map_descriptor = false;
13373
13374 /* This condition checks if we're mapping an array descriptor that
13375 isn't inside a derived type -- these have special handling, and
13376 are not handled as structs in omp_build_struct_sibling_lists.
13377 See that function for further details. */
13378 if (*grp_start_p != grp_end
13379 && OMP_CLAUSE_CHAIN (*grp_start_p)
13380 && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
13381 {
13382 tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
13383 if (omp_map_clause_descriptor_p (grp_mid)
13384 && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
13385 map_descriptor = true;
13386 }
13387 else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
13388 && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
13389 || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
13390 && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
13391 map_descriptor = true;
13392
13393 /* Adding the decl for a struct access: we haven't created
13394 GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
13395 whether they will be created in gimplify_adjust_omp_clauses.
13396 NOTE: Technically we should probably look through DECL_VALUE_EXPR
13397 here because something that looks like a DECL_P may actually be a
13398 struct access, e.g. variables in a lambda closure
13399 (__closure->__foo) or class members (this->foo). Currently in both
13400 those cases we map the whole of the containing object (directly in
13401 the C++ FE) though, so struct nodes are not created. */
13402 if (c == grp_end
13403 && addr_tokens[0]->type == STRUCTURE_BASE
13404 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13405 && !map_descriptor)
13406 {
13407 gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
13408 /* If we got to this struct via a chain of pointers, maybe we
13409 want to map it implicitly instead. */
13410 if (omp_access_chain_p (addr_tokens, 1))
13411 break;
13412 omp_mapping_group *wholestruct;
13413 if (!(region_type & ORT_ACC)
13414 && omp_mapped_by_containing_struct (grpmap,
13415 OMP_CLAUSE_DECL (c),
13416 &wholestruct))
13417 break;
13418 decl = addr_tokens[1]->expr;
13419 if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
13420 break;
13421 /* Standalone attach or detach clauses for a struct element
13422 should not inhibit implicit mapping of the whole struct. */
13423 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13424 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13425 break;
13426 flags = GOVD_MAP | GOVD_EXPLICIT;
13427
13428 gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
13429 || TREE_ADDRESSABLE (decl));
13430 goto do_add_decl;
13431 }
13432
13433 if (!DECL_P (decl))
13434 {
13435 tree d = decl, *pd;
13436 if (TREE_CODE (d) == ARRAY_REF)
13437 {
13438 while (TREE_CODE (d) == ARRAY_REF)
13439 d = TREE_OPERAND (d, 0);
13440 if (TREE_CODE (d) == COMPONENT_REF
13441 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
13442 decl = d;
13443 }
13444 pd = &OMP_CLAUSE_DECL (c);
13445 if (d == decl
13446 && TREE_CODE (decl) == INDIRECT_REF
13447 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
13448 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
13449 == REFERENCE_TYPE)
13450 && (OMP_CLAUSE_MAP_KIND (c)
13451 != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
13452 {
13453 pd = &TREE_OPERAND (decl, 0);
13454 decl = TREE_OPERAND (decl, 0);
13455 }
13456
13457 if (addr_tokens[0]->type == STRUCTURE_BASE
13458 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13459 && addr_tokens[1]->type == ACCESS_METHOD
13460 && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
13461 || (addr_tokens[1]->u.access_kind
13462 == ACCESS_POINTER_OFFSET))
13463 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
13464 {
13465 tree base = addr_tokens[1]->expr;
13466 splay_tree_node n
13467 = splay_tree_lookup (ctx->variables,
13468 (splay_tree_key) base);
13469 n->value |= GOVD_SEEN;
13470 }
13471
13472 if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13473 {
13474 /* Don't gimplify *pd fully at this point, as the base
13475 will need to be adjusted during omp lowering. */
13476 auto_vec<tree, 10> expr_stack;
13477 tree *p = pd;
13478 while (handled_component_p (*p)
13479 || TREE_CODE (*p) == INDIRECT_REF
13480 || TREE_CODE (*p) == ADDR_EXPR
13481 || TREE_CODE (*p) == MEM_REF
13482 || TREE_CODE (*p) == NON_LVALUE_EXPR)
13483 {
13484 expr_stack.safe_push (*p);
13485 p = &TREE_OPERAND (*p, 0);
13486 }
13487 for (int i = expr_stack.length () - 1; i >= 0; i--)
13488 {
13489 tree t = expr_stack[i];
13490 if (TREE_CODE (t) == ARRAY_REF
13491 || TREE_CODE (t) == ARRAY_RANGE_REF)
13492 {
13493 if (TREE_OPERAND (t, 2) == NULL_TREE)
13494 {
13495 tree low = unshare_expr (array_ref_low_bound (t));
13496 if (!is_gimple_min_invariant (low))
13497 {
13498 TREE_OPERAND (t, 2) = low;
13499 if (gimplify_expr (&TREE_OPERAND (t, 2),
13500 pre_p, NULL,
13501 is_gimple_reg,
13502 fb_rvalue) == GS_ERROR)
13503 remove = true;
13504 }
13505 }
13506 else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
13507 NULL, is_gimple_reg,
13508 fb_rvalue) == GS_ERROR)
13509 remove = true;
13510 if (TREE_OPERAND (t, 3) == NULL_TREE)
13511 {
13512 tree elmt_size = array_ref_element_size (t);
13513 if (!is_gimple_min_invariant (elmt_size))
13514 {
13515 elmt_size = unshare_expr (elmt_size);
13516 tree elmt_type
13517 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
13518 0)));
13519 tree factor
13520 = size_int (TYPE_ALIGN_UNIT (elmt_type));
13521 elmt_size
13522 = size_binop (EXACT_DIV_EXPR, elmt_size,
13523 factor);
13524 TREE_OPERAND (t, 3) = elmt_size;
13525 if (gimplify_expr (&TREE_OPERAND (t, 3),
13526 pre_p, NULL,
13527 is_gimple_reg,
13528 fb_rvalue) == GS_ERROR)
13529 remove = true;
13530 }
13531 }
13532 else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
13533 NULL, is_gimple_reg,
13534 fb_rvalue) == GS_ERROR)
13535 remove = true;
13536 }
13537 else if (TREE_CODE (t) == COMPONENT_REF)
13538 {
13539 if (TREE_OPERAND (t, 2) == NULL_TREE)
13540 {
13541 tree offset = component_ref_field_offset (t);
13542 if (!is_gimple_min_invariant (offset))
13543 {
13544 offset = unshare_expr (offset);
13545 tree field = TREE_OPERAND (t, 1);
13546 tree factor
13547 = size_int (DECL_OFFSET_ALIGN (field)
13548 / BITS_PER_UNIT);
13549 offset = size_binop (EXACT_DIV_EXPR, offset,
13550 factor);
13551 TREE_OPERAND (t, 2) = offset;
13552 if (gimplify_expr (&TREE_OPERAND (t, 2),
13553 pre_p, NULL,
13554 is_gimple_reg,
13555 fb_rvalue) == GS_ERROR)
13556 remove = true;
13557 }
13558 }
13559 else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
13560 NULL, is_gimple_reg,
13561 fb_rvalue) == GS_ERROR)
13562 remove = true;
13563 }
13564 }
13565 for (; expr_stack.length () > 0; )
13566 {
13567 tree t = expr_stack.pop ();
13568
13569 if (TREE_CODE (t) == ARRAY_REF
13570 || TREE_CODE (t) == ARRAY_RANGE_REF)
13571 {
13572 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
13573 && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
13574 NULL, is_gimple_val,
13575 fb_rvalue) == GS_ERROR)
13576 remove = true;
13577 }
13578 }
13579 }
13580 break;
13581 }
13582
13583 if ((code == OMP_TARGET
13584 || code == OMP_TARGET_DATA
13585 || code == OMP_TARGET_ENTER_DATA
13586 || code == OMP_TARGET_EXIT_DATA)
13587 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
13588 {
13589 /* If we have attach/detach but the decl we have is a pointer to
13590 pointer, we're probably mapping the "base level" array
13591 implicitly. Make sure we don't add the decl as if we mapped
13592 it explicitly. That is,
13593
13594 int **arr;
13595 [...]
13596 #pragma omp target map(arr[a][b:c])
13597
13598 should *not* map "arr" explicitly. That way we get a
13599 zero-length "alloc" mapping for it, and assuming it's been
13600 mapped by some previous directive, etc., things work as they
13601 should. */
13602
13603 tree basetype = TREE_TYPE (addr_tokens[0]->expr);
13604
13605 if (TREE_CODE (basetype) == REFERENCE_TYPE)
13606 basetype = TREE_TYPE (basetype);
13607
13608 if (code == OMP_TARGET
13609 && addr_tokens[0]->type == ARRAY_BASE
13610 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13611 && TREE_CODE (basetype) == POINTER_TYPE
13612 && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
13613 break;
13614 }
13615
13616 flags = GOVD_MAP | GOVD_EXPLICIT;
13617 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
13618 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
13619 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
13620 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
13621 flags |= GOVD_MAP_ALWAYS_TO;
13622
13623 goto do_add;
13624
13625 case OMP_CLAUSE_AFFINITY:
13626 gimplify_omp_affinity (list_p, pre_p);
13627 remove = true;
13628 break;
13629 case OMP_CLAUSE_DOACROSS:
13630 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
13631 {
13632 tree deps = OMP_CLAUSE_DECL (c);
13633 while (deps && TREE_CODE (deps) == TREE_LIST)
13634 {
13635 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
13636 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
13637 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
13638 pre_p, NULL, is_gimple_val, fb_rvalue);
13639 deps = TREE_CHAIN (deps);
13640 }
13641 }
13642 else
13643 gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
13644 == OMP_CLAUSE_DOACROSS_SOURCE);
13645 break;
13646 case OMP_CLAUSE_DEPEND:
13647 if (handled_depend_iterators == -1)
13648 handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
13649 if (handled_depend_iterators)
13650 {
13651 if (handled_depend_iterators == 2)
13652 remove = true;
13653 break;
13654 }
13655 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
13656 {
13657 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
13658 NULL, is_gimple_val, fb_rvalue);
13659 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
13660 }
13661 if (error_operand_p (OMP_CLAUSE_DECL (c)))
13662 {
13663 remove = true;
13664 break;
13665 }
13666 if (OMP_CLAUSE_DECL (c) != null_pointer_node)
13667 {
13668 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
13669 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
13670 is_gimple_val, fb_rvalue) == GS_ERROR)
13671 {
13672 remove = true;
13673 break;
13674 }
13675 }
13676 if (code == OMP_TASK)
13677 ctx->has_depend = true;
13678 break;
13679
13680 case OMP_CLAUSE_TO:
13681 case OMP_CLAUSE_FROM:
13682 case OMP_CLAUSE__CACHE_:
13683 decl = OMP_CLAUSE_DECL (c);
13684 if (error_operand_p (decl))
13685 {
13686 remove = true;
13687 break;
13688 }
13689 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
13690 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
13691 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
13692 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
13693 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
13694 {
13695 remove = true;
13696 break;
13697 }
13698 if (!DECL_P (decl))
13699 {
13700 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
13701 NULL, is_gimple_lvalue, fb_lvalue)
13702 == GS_ERROR)
13703 {
13704 remove = true;
13705 break;
13706 }
13707 break;
13708 }
13709 goto do_notice;
13710
13711 case OMP_CLAUSE_USE_DEVICE_PTR:
13712 case OMP_CLAUSE_USE_DEVICE_ADDR:
13713 flags = GOVD_EXPLICIT;
13714 goto do_add;
13715
13716 case OMP_CLAUSE_HAS_DEVICE_ADDR:
13717 decl = OMP_CLAUSE_DECL (c);
13718 while (TREE_CODE (decl) == INDIRECT_REF
13719 || TREE_CODE (decl) == ARRAY_REF)
13720 decl = TREE_OPERAND (decl, 0);
13721 flags = GOVD_EXPLICIT;
13722 goto do_add_decl;
13723
13724 case OMP_CLAUSE_IS_DEVICE_PTR:
13725 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13726 goto do_add;
13727
13728 do_add:
13729 decl = OMP_CLAUSE_DECL (c);
13730 do_add_decl:
13731 if (error_operand_p (decl))
13732 {
13733 remove = true;
13734 break;
13735 }
13736 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
13737 {
13738 tree t = omp_member_access_dummy_var (decl);
13739 if (t)
13740 {
13741 tree v = DECL_VALUE_EXPR (decl);
13742 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
13743 if (outer_ctx)
13744 omp_notice_variable (outer_ctx, t, true);
13745 }
13746 }
13747 if (code == OACC_DATA
13748 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13749 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13750 flags |= GOVD_MAP_0LEN_ARRAY;
13751 omp_add_variable (ctx, decl, flags);
13752 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13753 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13754 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13755 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13756 {
13757 struct gimplify_omp_ctx *pctx
13758 = code == OMP_TARGET ? outer_ctx : ctx;
13759 if (pctx)
13760 omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
13761 GOVD_LOCAL | GOVD_SEEN);
13762 if (pctx
13763 && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
13764 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13765 find_decl_expr,
13766 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
13767 NULL) == NULL_TREE)
13768 omp_add_variable (pctx,
13769 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
13770 GOVD_LOCAL | GOVD_SEEN);
13771 gimplify_omp_ctxp = pctx;
13772 push_gimplify_context ();
13773
13774 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
13775 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
13776
13777 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
13778 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
13779 pop_gimplify_context
13780 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
13781 push_gimplify_context ();
13782 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
13783 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
13784 pop_gimplify_context
13785 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
13786 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
13787 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
13788
13789 gimplify_omp_ctxp = outer_ctx;
13790 }
13791 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
13792 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
13793 {
13794 gimplify_omp_ctxp = ctx;
13795 push_gimplify_context ();
13796 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
13797 {
13798 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
13799 NULL, NULL);
13800 TREE_SIDE_EFFECTS (bind) = 1;
13801 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
13802 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
13803 }
13804 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
13805 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
13806 pop_gimplify_context
13807 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
13808 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
13809
13810 gimplify_omp_ctxp = outer_ctx;
13811 }
13812 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13813 && OMP_CLAUSE_LINEAR_STMT (c))
13814 {
13815 gimplify_omp_ctxp = ctx;
13816 push_gimplify_context ();
13817 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
13818 {
13819 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
13820 NULL, NULL);
13821 TREE_SIDE_EFFECTS (bind) = 1;
13822 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
13823 OMP_CLAUSE_LINEAR_STMT (c) = bind;
13824 }
13825 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
13826 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
13827 pop_gimplify_context
13828 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
13829 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
13830
13831 gimplify_omp_ctxp = outer_ctx;
13832 }
13833 if (notice_outer)
13834 goto do_notice;
13835 break;
13836
13837 case OMP_CLAUSE_COPYIN:
13838 case OMP_CLAUSE_COPYPRIVATE:
13839 decl = OMP_CLAUSE_DECL (c);
13840 if (error_operand_p (decl))
13841 {
13842 remove = true;
13843 break;
13844 }
13845 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
13846 && !remove
13847 && !omp_check_private (ctx, decl, true))
13848 {
13849 remove = true;
13850 if (is_global_var (decl))
13851 {
13852 if (DECL_THREAD_LOCAL_P (decl))
13853 remove = false;
13854 else if (DECL_HAS_VALUE_EXPR_P (decl))
13855 {
13856 tree value = get_base_address (DECL_VALUE_EXPR (decl));
13857
13858 if (value
13859 && DECL_P (value)
13860 && DECL_THREAD_LOCAL_P (value))
13861 remove = false;
13862 }
13863 }
13864 if (remove)
13865 error_at (OMP_CLAUSE_LOCATION (c),
13866 "copyprivate variable %qE is not threadprivate"
13867 " or private in outer context", DECL_NAME (decl));
13868 }
13869 do_notice:
13870 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13871 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13872 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13873 && outer_ctx
13874 && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
13875 || (region_type == ORT_WORKSHARE
13876 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13877 && (OMP_CLAUSE_REDUCTION_INSCAN (c)
13878 || code == OMP_LOOP)))
13879 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
13880 || (code == OMP_LOOP
13881 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13882 && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
13883 == ORT_COMBINED_TEAMS))))
13884 {
13885 splay_tree_node on
13886 = splay_tree_lookup (outer_ctx->variables,
13887 (splay_tree_key)decl);
13888 if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
13889 {
13890 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13891 && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
13892 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
13893 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
13894 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
13895 == POINTER_TYPE))))
13896 omp_firstprivatize_variable (outer_ctx, decl);
13897 else
13898 {
13899 omp_add_variable (outer_ctx, decl,
13900 GOVD_SEEN | GOVD_SHARED);
13901 if (outer_ctx->outer_context)
13902 omp_notice_variable (outer_ctx->outer_context, decl,
13903 true);
13904 }
13905 }
13906 }
13907 if (outer_ctx)
13908 omp_notice_variable (outer_ctx, decl, true);
13909 if (check_non_private
13910 && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13911 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13912 || decl == OMP_CLAUSE_DECL (c)
13913 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
13914 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
13915 == ADDR_EXPR
13916 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
13917 == POINTER_PLUS_EXPR
13918 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
13919 (OMP_CLAUSE_DECL (c), 0), 0))
13920 == ADDR_EXPR)))))
13921 && omp_check_private (ctx, decl, false))
13922 {
13923 error ("%s variable %qE is private in outer context",
13924 check_non_private, DECL_NAME (decl));
13925 remove = true;
13926 }
13927 break;
13928
13929 case OMP_CLAUSE_DETACH:
13930 flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
13931 goto do_add;
13932
13933 case OMP_CLAUSE_IF:
13934 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
13935 && OMP_CLAUSE_IF_MODIFIER (c) != code)
13936 {
13937 const char *p[2];
13938 for (int i = 0; i < 2; i++)
13939 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
13940 {
13941 case VOID_CST: p[i] = "cancel"; break;
13942 case OMP_PARALLEL: p[i] = "parallel"; break;
13943 case OMP_SIMD: p[i] = "simd"; break;
13944 case OMP_TASK: p[i] = "task"; break;
13945 case OMP_TASKLOOP: p[i] = "taskloop"; break;
13946 case OMP_TARGET_DATA: p[i] = "target data"; break;
13947 case OMP_TARGET: p[i] = "target"; break;
13948 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
13949 case OMP_TARGET_ENTER_DATA:
13950 p[i] = "target enter data"; break;
13951 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
13952 default: gcc_unreachable ();
13953 }
13954 error_at (OMP_CLAUSE_LOCATION (c),
13955 "expected %qs %<if%> clause modifier rather than %qs",
13956 p[0], p[1]);
13957 remove = true;
13958 }
13959 /* Fall through. */
13960
13961 case OMP_CLAUSE_SELF:
13962 case OMP_CLAUSE_FINAL:
13963 OMP_CLAUSE_OPERAND (c, 0)
13964 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
13965 /* Fall through. */
13966
13967 case OMP_CLAUSE_NUM_TEAMS:
13968 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
13969 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
13970 && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
13971 {
13972 if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
13973 {
13974 remove = true;
13975 break;
13976 }
13977 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
13978 = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
13979 pre_p, NULL, true);
13980 }
13981 /* Fall through. */
13982
13983 case OMP_CLAUSE_SCHEDULE:
13984 case OMP_CLAUSE_NUM_THREADS:
13985 case OMP_CLAUSE_THREAD_LIMIT:
13986 case OMP_CLAUSE_DIST_SCHEDULE:
13987 case OMP_CLAUSE_DEVICE:
13988 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
13989 && OMP_CLAUSE_DEVICE_ANCESTOR (c))
13990 {
13991 if (code != OMP_TARGET)
13992 {
13993 error_at (OMP_CLAUSE_LOCATION (c),
13994 "%<device%> clause with %<ancestor%> is only "
13995 "allowed on %<target%> construct");
13996 remove = true;
13997 break;
13998 }
13999
14000 tree clauses = *orig_list_p;
14001 for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
14002 if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
14003 && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
14004 && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
14005 && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
14006 && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
14007 )
14008 {
14009 error_at (OMP_CLAUSE_LOCATION (c),
14010 "with %<ancestor%>, only the %<device%>, "
14011 "%<firstprivate%>, %<private%>, %<defaultmap%>, "
14012 "and %<map%> clauses may appear on the "
14013 "construct");
14014 remove = true;
14015 break;
14016 }
14017 }
14018 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14019 && code == OMP_DISPATCH)
14020 {
14021 bool saved_into_ssa = gimplify_ctxp->into_ssa;
14022 gimplify_ctxp->into_ssa = false;
14023 if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
14024 is_gimple_val, fb_rvalue)
14025 == GS_ERROR)
14026 remove = true;
14027 else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
14028 omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
14029 GOVD_SHARED | GOVD_SEEN);
14030 gimplify_ctxp->into_ssa = saved_into_ssa;
14031 break;
14032 }
14033 /* Fall through. */
14034
14035 case OMP_CLAUSE_PRIORITY:
14036 case OMP_CLAUSE_GRAINSIZE:
14037 case OMP_CLAUSE_NUM_TASKS:
14038 case OMP_CLAUSE_FILTER:
14039 case OMP_CLAUSE_HINT:
14040 case OMP_CLAUSE_ASYNC:
14041 case OMP_CLAUSE_WAIT:
14042 case OMP_CLAUSE_NUM_GANGS:
14043 case OMP_CLAUSE_NUM_WORKERS:
14044 case OMP_CLAUSE_VECTOR_LENGTH:
14045 case OMP_CLAUSE_WORKER:
14046 case OMP_CLAUSE_VECTOR:
14047 if (OMP_CLAUSE_OPERAND (c, 0)
14048 && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
14049 {
14050 if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
14051 {
14052 remove = true;
14053 break;
14054 }
14055 /* All these clauses care about value, not a particular decl,
14056 so try to force it into a SSA_NAME or fresh temporary. */
14057 OMP_CLAUSE_OPERAND (c, 0)
14058 = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
14059 pre_p, NULL, true);
14060 }
14061 break;
14062
14063 case OMP_CLAUSE_GANG:
14064 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
14065 is_gimple_val, fb_rvalue) == GS_ERROR)
14066 remove = true;
14067 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
14068 is_gimple_val, fb_rvalue) == GS_ERROR)
14069 remove = true;
14070 break;
14071
14072 case OMP_CLAUSE_NOWAIT:
14073 nowait = 1;
14074 break;
14075
14076 case OMP_CLAUSE_ORDERED:
14077 case OMP_CLAUSE_UNTIED:
14078 case OMP_CLAUSE_COLLAPSE:
14079 case OMP_CLAUSE_TILE:
14080 case OMP_CLAUSE_AUTO:
14081 case OMP_CLAUSE_SEQ:
14082 case OMP_CLAUSE_INDEPENDENT:
14083 case OMP_CLAUSE_MERGEABLE:
14084 case OMP_CLAUSE_PROC_BIND:
14085 case OMP_CLAUSE_SAFELEN:
14086 case OMP_CLAUSE_SIMDLEN:
14087 case OMP_CLAUSE_NOGROUP:
14088 case OMP_CLAUSE_THREADS:
14089 case OMP_CLAUSE_SIMD:
14090 case OMP_CLAUSE_BIND:
14091 case OMP_CLAUSE_IF_PRESENT:
14092 case OMP_CLAUSE_FINALIZE:
14093 case OMP_CLAUSE_INTEROP:
14094 case OMP_CLAUSE_INIT:
14095 case OMP_CLAUSE_USE:
14096 case OMP_CLAUSE_DESTROY:
14097 break;
14098
14099 case OMP_CLAUSE_ORDER:
14100 ctx->order_concurrent = true;
14101 break;
14102
14103 case OMP_CLAUSE_DEFAULTMAP:
14104 enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
14105 switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
14106 {
14107 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
14108 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
14109 gdmkmin = GDMK_SCALAR;
14110 gdmkmax = GDMK_POINTER;
14111 break;
14112 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14113 gdmkmin = GDMK_SCALAR;
14114 gdmkmax = GDMK_SCALAR_TARGET;
14115 break;
14116 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14117 gdmkmin = gdmkmax = GDMK_AGGREGATE;
14118 break;
14119 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
14120 gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
14121 break;
14122 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14123 gdmkmin = gdmkmax = GDMK_POINTER;
14124 break;
14125 default:
14126 gcc_unreachable ();
14127 }
14128 for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
14129 switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
14130 {
14131 case OMP_CLAUSE_DEFAULTMAP_ALLOC:
14132 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
14133 break;
14134 case OMP_CLAUSE_DEFAULTMAP_TO:
14135 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
14136 break;
14137 case OMP_CLAUSE_DEFAULTMAP_FROM:
14138 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
14139 break;
14140 case OMP_CLAUSE_DEFAULTMAP_TOFROM:
14141 ctx->defaultmap[gdmk] = GOVD_MAP;
14142 break;
14143 case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
14144 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14145 break;
14146 case OMP_CLAUSE_DEFAULTMAP_NONE:
14147 ctx->defaultmap[gdmk] = 0;
14148 break;
14149 case OMP_CLAUSE_DEFAULTMAP_PRESENT:
14150 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
14151 break;
14152 case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
14153 switch (gdmk)
14154 {
14155 case GDMK_SCALAR:
14156 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14157 break;
14158 case GDMK_SCALAR_TARGET:
14159 ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
14160 ? GOVD_MAP : GOVD_FIRSTPRIVATE);
14161 break;
14162 case GDMK_AGGREGATE:
14163 case GDMK_ALLOCATABLE:
14164 ctx->defaultmap[gdmk] = GOVD_MAP;
14165 break;
14166 case GDMK_POINTER:
14167 ctx->defaultmap[gdmk] = GOVD_MAP;
14168 if (!lang_GNU_Fortran ())
14169 ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
14170 break;
14171 default:
14172 gcc_unreachable ();
14173 }
14174 break;
14175 default:
14176 gcc_unreachable ();
14177 }
14178 break;
14179
14180 case OMP_CLAUSE_ALIGNED:
14181 decl = OMP_CLAUSE_DECL (c);
14182 if (error_operand_p (decl))
14183 {
14184 remove = true;
14185 break;
14186 }
14187 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
14188 is_gimple_val, fb_rvalue) == GS_ERROR)
14189 {
14190 remove = true;
14191 break;
14192 }
14193 if (!is_global_var (decl)
14194 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14195 omp_add_variable (ctx, decl, GOVD_ALIGNED);
14196 break;
14197
14198 case OMP_CLAUSE_NONTEMPORAL:
14199 decl = OMP_CLAUSE_DECL (c);
14200 if (error_operand_p (decl))
14201 {
14202 remove = true;
14203 break;
14204 }
14205 omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
14206 break;
14207
14208 case OMP_CLAUSE_ALLOCATE:
14209 decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
14210 if (decl
14211 && TREE_CODE (decl) == INTEGER_CST
14212 && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREADS)
14213 && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
14214 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
14215 "allocator with access trait set to %<thread%> "
14216 "results in undefined behavior for %qs directive",
14217 code == OMP_TARGET ? "target"
14218 : (code == OMP_TASK
14219 ? "task" : "taskloop"));
14220 decl = OMP_CLAUSE_DECL (c);
14221 if (error_operand_p (decl))
14222 {
14223 remove = true;
14224 break;
14225 }
14226 if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
14227 is_gimple_val, fb_rvalue) == GS_ERROR)
14228 {
14229 remove = true;
14230 break;
14231 }
14232 else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
14233 || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
14234 == INTEGER_CST))
14235 ;
14236 else if (code == OMP_TASKLOOP
14237 || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
14238 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
14239 = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
14240 pre_p, NULL, false);
14241 break;
14242
14243 case OMP_CLAUSE_DEFAULT:
14244 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
14245 break;
14246
14247 case OMP_CLAUSE_INCLUSIVE:
14248 case OMP_CLAUSE_EXCLUSIVE:
14249 decl = OMP_CLAUSE_DECL (c);
14250 {
14251 splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
14252 (splay_tree_key) decl);
14253 if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
14254 {
14255 error_at (OMP_CLAUSE_LOCATION (c),
14256 "%qD specified in %qs clause but not in %<inscan%> "
14257 "%<reduction%> clause on the containing construct",
14258 decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14259 remove = true;
14260 }
14261 else
14262 {
14263 n->value |= GOVD_REDUCTION_INSCAN;
14264 if (outer_ctx->region_type == ORT_SIMD
14265 && outer_ctx->outer_context
14266 && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
14267 {
14268 n = splay_tree_lookup (outer_ctx->outer_context->variables,
14269 (splay_tree_key) decl);
14270 if (n && (n->value & GOVD_REDUCTION) != 0)
14271 n->value |= GOVD_REDUCTION_INSCAN;
14272 }
14273 }
14274 }
14275 break;
14276
14277 case OMP_CLAUSE_NOVARIANTS:
14278 OMP_CLAUSE_NOVARIANTS_EXPR (c)
14279 = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
14280 break;
14281 case OMP_CLAUSE_NOCONTEXT:
14282 OMP_CLAUSE_NOCONTEXT_EXPR (c)
14283 = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
14284 break;
14285 case OMP_CLAUSE_NOHOST:
14286 default:
14287 gcc_unreachable ();
14288 }
14289
14290 if (code == OACC_DATA
14291 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14292 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14293 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
14294 remove = true;
14295 if (remove)
14296 *list_p = OMP_CLAUSE_CHAIN (c);
14297 else
14298 list_p = &OMP_CLAUSE_CHAIN (c);
14299 }
14300
14301 if (groups)
14302 {
14303 delete grpmap;
14304 delete groups;
14305 }
14306
14307 ctx->clauses = *orig_list_p;
14308 gimplify_omp_ctxp = ctx;
14309 }
14310
14311 /* Return true if DECL is a candidate for shared to firstprivate
14312 optimization. We only consider non-addressable scalars, not
14313 too big, and not references. */
14314
14315 static bool
14316 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
14317 {
14318 if (TREE_ADDRESSABLE (decl))
14319 return false;
14320 tree type = TREE_TYPE (decl);
14321 if (!is_gimple_reg_type (type)
14322 || TREE_CODE (type) == REFERENCE_TYPE
14323 || TREE_ADDRESSABLE (type))
14324 return false;
14325 /* Don't optimize too large decls, as each thread/task will have
14326 its own. */
14327 HOST_WIDE_INT len = int_size_in_bytes (type);
14328 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
14329 return false;
14330 if (omp_privatize_by_reference (decl))
14331 return false;
14332 return true;
14333 }
14334
14335 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
14336 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
14337 GOVD_WRITTEN in outer contexts. */
14338
14339 static void
14340 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
14341 {
14342 for (; ctx; ctx = ctx->outer_context)
14343 {
14344 splay_tree_node n = splay_tree_lookup (ctx->variables,
14345 (splay_tree_key) decl);
14346 if (n == NULL)
14347 continue;
14348 else if (n->value & GOVD_SHARED)
14349 {
14350 n->value |= GOVD_WRITTEN;
14351 return;
14352 }
14353 else if (n->value & GOVD_DATA_SHARE_CLASS)
14354 return;
14355 }
14356 }
14357
14358 /* Helper callback for walk_gimple_seq to discover possible stores
14359 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
14360 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
14361 for those. */
14362
14363 static tree
14364 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
14365 {
14366 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
14367
14368 *walk_subtrees = 0;
14369 if (!wi->is_lhs)
14370 return NULL_TREE;
14371
14372 tree op = *tp;
14373 do
14374 {
14375 if (handled_component_p (op))
14376 op = TREE_OPERAND (op, 0);
14377 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
14378 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
14379 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
14380 else
14381 break;
14382 }
14383 while (1);
14384 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
14385 return NULL_TREE;
14386
14387 omp_mark_stores (gimplify_omp_ctxp, op);
14388 return NULL_TREE;
14389 }
14390
14391 /* Helper callback for walk_gimple_seq to discover possible stores
14392 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
14393 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
14394 for those. */
14395
14396 static tree
14397 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
14398 bool *handled_ops_p,
14399 struct walk_stmt_info *wi)
14400 {
14401 gimple *stmt = gsi_stmt (*gsi_p);
14402 switch (gimple_code (stmt))
14403 {
14404 /* Don't recurse on OpenMP constructs for which
14405 gimplify_adjust_omp_clauses already handled the bodies,
14406 except handle gimple_omp_for_pre_body. */
14407 case GIMPLE_OMP_FOR:
14408 *handled_ops_p = true;
14409 if (gimple_omp_for_pre_body (stmt))
14410 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
14411 omp_find_stores_stmt, omp_find_stores_op, wi);
14412 break;
14413 case GIMPLE_OMP_PARALLEL:
14414 case GIMPLE_OMP_TASK:
14415 case GIMPLE_OMP_SECTIONS:
14416 case GIMPLE_OMP_SINGLE:
14417 case GIMPLE_OMP_SCOPE:
14418 case GIMPLE_OMP_TARGET:
14419 case GIMPLE_OMP_TEAMS:
14420 case GIMPLE_OMP_CRITICAL:
14421 *handled_ops_p = true;
14422 break;
14423 default:
14424 break;
14425 }
14426 return NULL_TREE;
14427 }
14428
14429 struct gimplify_adjust_omp_clauses_data
14430 {
14431 tree *list_p;
14432 gimple_seq *pre_p;
14433 };
14434
14435 /* For all variables that were not actually used within the context,
14436 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
14437
14438 static int
14439 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
14440 {
14441 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
14442 gimple_seq *pre_p
14443 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
14444 tree decl = (tree) n->key;
14445 unsigned flags = n->value;
14446 enum omp_clause_code code;
14447 tree clause;
14448 bool private_debug;
14449
14450 if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
14451 && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
14452 flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
14453 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
14454 return 0;
14455 if ((flags & GOVD_SEEN) == 0)
14456 return 0;
14457 if (flags & GOVD_DEBUG_PRIVATE)
14458 {
14459 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
14460 private_debug = true;
14461 }
14462 else if (flags & GOVD_MAP)
14463 private_debug = false;
14464 else
14465 private_debug
14466 = lang_hooks.decls.omp_private_debug_clause (decl,
14467 !!(flags & GOVD_SHARED));
14468 if (private_debug)
14469 code = OMP_CLAUSE_PRIVATE;
14470 else if (flags & GOVD_MAP)
14471 {
14472 code = OMP_CLAUSE_MAP;
14473 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
14474 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
14475 {
14476 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
14477 return 0;
14478 }
14479 if (VAR_P (decl)
14480 && DECL_IN_CONSTANT_POOL (decl)
14481 && !lookup_attribute ("omp declare target",
14482 DECL_ATTRIBUTES (decl)))
14483 {
14484 tree id = get_identifier ("omp declare target");
14485 DECL_ATTRIBUTES (decl)
14486 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14487 varpool_node *node = varpool_node::get (decl);
14488 if (node)
14489 {
14490 node->offloadable = 1;
14491 if (ENABLE_OFFLOADING)
14492 g->have_offload = true;
14493 }
14494 }
14495 }
14496 else if (flags & GOVD_SHARED)
14497 {
14498 if (is_global_var (decl))
14499 {
14500 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
14501 while (ctx != NULL)
14502 {
14503 splay_tree_node on
14504 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14505 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
14506 | GOVD_PRIVATE | GOVD_REDUCTION
14507 | GOVD_LINEAR | GOVD_MAP)) != 0)
14508 break;
14509 ctx = ctx->outer_context;
14510 }
14511 if (ctx == NULL)
14512 return 0;
14513 }
14514 code = OMP_CLAUSE_SHARED;
14515 /* Don't optimize shared into firstprivate for read-only vars
14516 on tasks with depend clause, we shouldn't try to copy them
14517 until the dependencies are satisfied. */
14518 if (gimplify_omp_ctxp->has_depend)
14519 flags |= GOVD_WRITTEN;
14520 }
14521 else if (flags & GOVD_PRIVATE)
14522 code = OMP_CLAUSE_PRIVATE;
14523 else if (flags & GOVD_FIRSTPRIVATE)
14524 {
14525 code = OMP_CLAUSE_FIRSTPRIVATE;
14526 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
14527 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
14528 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
14529 {
14530 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
14531 "%<target%> construct", decl);
14532 return 0;
14533 }
14534 }
14535 else if (flags & GOVD_LASTPRIVATE)
14536 code = OMP_CLAUSE_LASTPRIVATE;
14537 else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
14538 return 0;
14539 else if (flags & GOVD_CONDTEMP)
14540 {
14541 code = OMP_CLAUSE__CONDTEMP_;
14542 gimple_add_tmp_var (decl);
14543 }
14544 else
14545 gcc_unreachable ();
14546
14547 if (((flags & GOVD_LASTPRIVATE)
14548 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
14549 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14550 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
14551
14552 tree chain = *list_p;
14553 clause = build_omp_clause (input_location, code);
14554 OMP_CLAUSE_DECL (clause) = decl;
14555 OMP_CLAUSE_CHAIN (clause) = chain;
14556 if (private_debug)
14557 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
14558 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
14559 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
14560 else if (code == OMP_CLAUSE_SHARED
14561 && (flags & GOVD_WRITTEN) == 0
14562 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14563 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
14564 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
14565 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
14566 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
14567 {
14568 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
14569 OMP_CLAUSE_DECL (nc) = decl;
14570 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14571 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
14572 OMP_CLAUSE_DECL (clause)
14573 = build_fold_indirect_ref_loc (input_location, decl);
14574 OMP_CLAUSE_DECL (clause)
14575 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
14576 build_int_cst (build_pointer_type (char_type_node), 0));
14577 OMP_CLAUSE_SIZE (clause) = size_zero_node;
14578 OMP_CLAUSE_SIZE (nc) = size_zero_node;
14579 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
14580 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
14581 tree dtype = TREE_TYPE (decl);
14582 if (TREE_CODE (dtype) == REFERENCE_TYPE)
14583 dtype = TREE_TYPE (dtype);
14584 /* FIRSTPRIVATE_POINTER doesn't work well if we have a
14585 multiply-indirected pointer. If we have a reference to a pointer to
14586 a pointer, it's possible that this should really be
14587 GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
14588 moment, so stick with this. (See PR113279 and testcases
14589 baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice). */
14590 if (TREE_CODE (dtype) == POINTER_TYPE
14591 && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
14592 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
14593 else
14594 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
14595 OMP_CLAUSE_CHAIN (nc) = chain;
14596 OMP_CLAUSE_CHAIN (clause) = nc;
14597 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14598 gimplify_omp_ctxp = ctx->outer_context;
14599 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
14600 pre_p, NULL, is_gimple_val, fb_rvalue);
14601 gimplify_omp_ctxp = ctx;
14602 }
14603 else if (code == OMP_CLAUSE_MAP)
14604 {
14605 int kind;
14606 /* Not all combinations of these GOVD_MAP flags are actually valid. */
14607 switch (flags & (GOVD_MAP_TO_ONLY
14608 | GOVD_MAP_FORCE
14609 | GOVD_MAP_FORCE_PRESENT
14610 | GOVD_MAP_ALLOC_ONLY
14611 | GOVD_MAP_FROM_ONLY))
14612 {
14613 case 0:
14614 kind = GOMP_MAP_TOFROM;
14615 break;
14616 case GOVD_MAP_FORCE:
14617 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
14618 break;
14619 case GOVD_MAP_TO_ONLY:
14620 kind = GOMP_MAP_TO;
14621 break;
14622 case GOVD_MAP_FROM_ONLY:
14623 kind = GOMP_MAP_FROM;
14624 break;
14625 case GOVD_MAP_ALLOC_ONLY:
14626 kind = GOMP_MAP_ALLOC;
14627 break;
14628 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
14629 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
14630 break;
14631 case GOVD_MAP_FORCE_PRESENT:
14632 kind = GOMP_MAP_FORCE_PRESENT;
14633 break;
14634 case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
14635 kind = GOMP_MAP_FORCE_PRESENT;
14636 break;
14637 default:
14638 gcc_unreachable ();
14639 }
14640 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
14641 /* Setting of the implicit flag for the runtime is currently disabled for
14642 OpenACC. */
14643 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
14644 OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
14645 if (DECL_SIZE (decl)
14646 && !poly_int_tree_p (DECL_SIZE (decl)))
14647 {
14648 tree decl2 = DECL_VALUE_EXPR (decl);
14649 gcc_assert (INDIRECT_REF_P (decl2));
14650 decl2 = TREE_OPERAND (decl2, 0);
14651 gcc_assert (DECL_P (decl2));
14652 tree mem = build_simple_mem_ref (decl2);
14653 OMP_CLAUSE_DECL (clause) = mem;
14654 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
14655 if (gimplify_omp_ctxp->outer_context)
14656 {
14657 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
14658 omp_notice_variable (ctx, decl2, true);
14659 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
14660 }
14661 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
14662 OMP_CLAUSE_MAP);
14663 OMP_CLAUSE_DECL (nc) = decl;
14664 OMP_CLAUSE_SIZE (nc) = size_zero_node;
14665 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
14666 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
14667 else
14668 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
14669 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
14670 OMP_CLAUSE_CHAIN (clause) = nc;
14671 }
14672 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
14673 && omp_privatize_by_reference (decl))
14674 {
14675 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
14676 OMP_CLAUSE_SIZE (clause)
14677 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
14678 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14679 gimplify_omp_ctxp = ctx->outer_context;
14680 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
14681 pre_p, NULL, is_gimple_val, fb_rvalue);
14682 gimplify_omp_ctxp = ctx;
14683 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
14684 OMP_CLAUSE_MAP);
14685 OMP_CLAUSE_DECL (nc) = decl;
14686 OMP_CLAUSE_SIZE (nc) = size_zero_node;
14687 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
14688 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
14689 OMP_CLAUSE_CHAIN (clause) = nc;
14690 }
14691 else
14692 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
14693 }
14694 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
14695 {
14696 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
14697 OMP_CLAUSE_DECL (nc) = decl;
14698 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
14699 OMP_CLAUSE_CHAIN (nc) = chain;
14700 OMP_CLAUSE_CHAIN (clause) = nc;
14701 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14702 gimplify_omp_ctxp = ctx->outer_context;
14703 lang_hooks.decls.omp_finish_clause (nc, pre_p,
14704 (ctx->region_type & ORT_ACC) != 0);
14705 gimplify_omp_ctxp = ctx;
14706 }
14707 *list_p = clause;
14708 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14709 gimplify_omp_ctxp = ctx->outer_context;
14710 /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
14711 in simd. Those are only added for the local vars inside of simd body
14712 and they don't need to be e.g. default constructible. */
14713 if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
14714 lang_hooks.decls.omp_finish_clause (clause, pre_p,
14715 (ctx->region_type & ORT_ACC) != 0);
14716 if (gimplify_omp_ctxp)
14717 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
14718 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
14719 && DECL_P (OMP_CLAUSE_SIZE (clause)))
14720 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
14721 true);
14722 gimplify_omp_ctxp = ctx;
14723 return 0;
14724 }
14725
14726 static void
14727 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
14728 enum tree_code code)
14729 {
14730 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14731 tree *orig_list_p = list_p;
14732 tree c, decl;
14733 bool has_inscan_reductions = false;
14734
14735 if (body)
14736 {
14737 struct gimplify_omp_ctx *octx;
14738 for (octx = ctx; octx; octx = octx->outer_context)
14739 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
14740 break;
14741 if (octx)
14742 {
14743 struct walk_stmt_info wi;
14744 memset (&wi, 0, sizeof (wi));
14745 walk_gimple_seq (body, omp_find_stores_stmt,
14746 omp_find_stores_op, &wi);
14747 }
14748 }
14749
14750 if (ctx->add_safelen1)
14751 {
14752 /* If there are VLAs in the body of simd loop, prevent
14753 vectorization. */
14754 gcc_assert (ctx->region_type == ORT_SIMD);
14755 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
14756 OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
14757 OMP_CLAUSE_CHAIN (c) = *list_p;
14758 *list_p = c;
14759 list_p = &OMP_CLAUSE_CHAIN (c);
14760 }
14761
14762 if (ctx->region_type == ORT_WORKSHARE
14763 && ctx->outer_context
14764 && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
14765 {
14766 for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
14767 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14768 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
14769 {
14770 decl = OMP_CLAUSE_DECL (c);
14771 splay_tree_node n
14772 = splay_tree_lookup (ctx->outer_context->variables,
14773 (splay_tree_key) decl);
14774 gcc_checking_assert (!splay_tree_lookup (ctx->variables,
14775 (splay_tree_key) decl));
14776 omp_add_variable (ctx, decl, n->value);
14777 tree c2 = copy_node (c);
14778 OMP_CLAUSE_CHAIN (c2) = *list_p;
14779 *list_p = c2;
14780 if ((n->value & GOVD_FIRSTPRIVATE) == 0)
14781 continue;
14782 c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
14783 OMP_CLAUSE_FIRSTPRIVATE);
14784 OMP_CLAUSE_DECL (c2) = decl;
14785 OMP_CLAUSE_CHAIN (c2) = *list_p;
14786 *list_p = c2;
14787 }
14788 }
14789
14790 if (code == OMP_TARGET
14791 || code == OMP_TARGET_DATA
14792 || code == OMP_TARGET_ENTER_DATA
14793 || code == OMP_TARGET_EXIT_DATA)
14794 {
14795 vec<omp_mapping_group> *groups;
14796 groups = omp_gather_mapping_groups (list_p);
14797 hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
14798
14799 if (groups)
14800 {
14801 grpmap = omp_index_mapping_groups (groups);
14802
14803 omp_resolve_clause_dependencies (code, groups, grpmap);
14804 omp_build_struct_sibling_lists (code, ctx->region_type, groups,
14805 &grpmap, list_p);
14806
14807 omp_mapping_group *outlist = NULL;
14808
14809 delete grpmap;
14810 delete groups;
14811
14812 /* Rebuild now we have struct sibling lists. */
14813 groups = omp_gather_mapping_groups (list_p);
14814 grpmap = omp_index_mapping_groups (groups);
14815
14816 bool enter_exit = (code == OMP_TARGET_ENTER_DATA
14817 || code == OMP_TARGET_EXIT_DATA);
14818
14819 outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
14820 outlist = omp_segregate_mapping_groups (outlist);
14821 list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
14822
14823 delete grpmap;
14824 delete groups;
14825 }
14826 }
14827 else if (ctx->region_type & ORT_ACC)
14828 {
14829 vec<omp_mapping_group> *groups;
14830 groups = omp_gather_mapping_groups (list_p);
14831 if (groups)
14832 {
14833 hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
14834 grpmap = omp_index_mapping_groups (groups);
14835
14836 oacc_resolve_clause_dependencies (groups, grpmap);
14837 omp_build_struct_sibling_lists (code, ctx->region_type, groups,
14838 &grpmap, list_p);
14839
14840 delete groups;
14841 delete grpmap;
14842 }
14843 }
14844
14845 tree attach_list = NULL_TREE;
14846 tree *attach_tail = &attach_list;
14847
14848 tree *grp_start_p = NULL, grp_end = NULL_TREE;
14849
14850 while ((c = *list_p) != NULL)
14851 {
14852 splay_tree_node n;
14853 bool remove = false;
14854 bool move_attach = false;
14855
14856 if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
14857 grp_end = NULL_TREE;
14858
14859 switch (OMP_CLAUSE_CODE (c))
14860 {
14861 case OMP_CLAUSE_FIRSTPRIVATE:
14862 if ((ctx->region_type & ORT_TARGET)
14863 && (ctx->region_type & ORT_ACC) == 0
14864 && TYPE_ATOMIC (strip_array_types
14865 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
14866 {
14867 error_at (OMP_CLAUSE_LOCATION (c),
14868 "%<_Atomic%> %qD in %<firstprivate%> clause on "
14869 "%<target%> construct", OMP_CLAUSE_DECL (c));
14870 remove = true;
14871 break;
14872 }
14873 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
14874 {
14875 decl = OMP_CLAUSE_DECL (c);
14876 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14877 if ((n->value & GOVD_MAP) != 0)
14878 {
14879 remove = true;
14880 break;
14881 }
14882 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
14883 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
14884 }
14885 /* FALLTHRU */
14886 case OMP_CLAUSE_PRIVATE:
14887 case OMP_CLAUSE_SHARED:
14888 case OMP_CLAUSE_LINEAR:
14889 decl = OMP_CLAUSE_DECL (c);
14890 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14891 remove = !(n->value & GOVD_SEEN);
14892 if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
14893 && code == OMP_PARALLEL
14894 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
14895 remove = true;
14896 if (! remove)
14897 {
14898 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
14899 if ((n->value & GOVD_DEBUG_PRIVATE)
14900 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
14901 {
14902 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
14903 || ((n->value & GOVD_DATA_SHARE_CLASS)
14904 == GOVD_SHARED));
14905 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
14906 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
14907 }
14908 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
14909 && ctx->has_depend
14910 && DECL_P (decl))
14911 n->value |= GOVD_WRITTEN;
14912 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
14913 && (n->value & GOVD_WRITTEN) == 0
14914 && DECL_P (decl)
14915 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14916 OMP_CLAUSE_SHARED_READONLY (c) = 1;
14917 else if (DECL_P (decl)
14918 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
14919 && (n->value & GOVD_WRITTEN) != 0)
14920 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14921 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
14922 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14923 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
14924 }
14925 else
14926 n->value &= ~GOVD_EXPLICIT;
14927 break;
14928
14929 case OMP_CLAUSE_LASTPRIVATE:
14930 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
14931 accurately reflect the presence of a FIRSTPRIVATE clause. */
14932 decl = OMP_CLAUSE_DECL (c);
14933 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14934 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
14935 = (n->value & GOVD_FIRSTPRIVATE) != 0;
14936 if (code == OMP_DISTRIBUTE
14937 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
14938 {
14939 remove = true;
14940 error_at (OMP_CLAUSE_LOCATION (c),
14941 "same variable used in %<firstprivate%> and "
14942 "%<lastprivate%> clauses on %<distribute%> "
14943 "construct");
14944 }
14945 if (!remove
14946 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14947 && DECL_P (decl)
14948 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14949 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
14950 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
14951 remove = true;
14952 break;
14953
14954 case OMP_CLAUSE_ALIGNED:
14955 decl = OMP_CLAUSE_DECL (c);
14956 if (!is_global_var (decl))
14957 {
14958 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14959 remove = n == NULL || !(n->value & GOVD_SEEN);
14960 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14961 {
14962 struct gimplify_omp_ctx *octx;
14963 if (n != NULL
14964 && (n->value & (GOVD_DATA_SHARE_CLASS
14965 & ~GOVD_FIRSTPRIVATE)))
14966 remove = true;
14967 else
14968 for (octx = ctx->outer_context; octx;
14969 octx = octx->outer_context)
14970 {
14971 n = splay_tree_lookup (octx->variables,
14972 (splay_tree_key) decl);
14973 if (n == NULL)
14974 continue;
14975 if (n->value & GOVD_LOCAL)
14976 break;
14977 /* We have to avoid assigning a shared variable
14978 to itself when trying to add
14979 __builtin_assume_aligned. */
14980 if (n->value & GOVD_SHARED)
14981 {
14982 remove = true;
14983 break;
14984 }
14985 }
14986 }
14987 }
14988 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
14989 {
14990 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14991 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
14992 remove = true;
14993 }
14994 break;
14995
14996 case OMP_CLAUSE_HAS_DEVICE_ADDR:
14997 decl = OMP_CLAUSE_DECL (c);
14998 while (INDIRECT_REF_P (decl)
14999 || TREE_CODE (decl) == ARRAY_REF)
15000 decl = TREE_OPERAND (decl, 0);
15001 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15002 remove = n == NULL || !(n->value & GOVD_SEEN);
15003 break;
15004
15005 case OMP_CLAUSE_IS_DEVICE_PTR:
15006 case OMP_CLAUSE_NONTEMPORAL:
15007 decl = OMP_CLAUSE_DECL (c);
15008 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15009 remove = n == NULL || !(n->value & GOVD_SEEN);
15010 break;
15011
15012 case OMP_CLAUSE_MAP:
15013 decl = OMP_CLAUSE_DECL (c);
15014 if (!grp_end)
15015 {
15016 grp_start_p = list_p;
15017 grp_end = *omp_group_last (grp_start_p);
15018 }
15019 switch (OMP_CLAUSE_MAP_KIND (c))
15020 {
15021 case GOMP_MAP_PRESENT_ALLOC:
15022 case GOMP_MAP_PRESENT_TO:
15023 case GOMP_MAP_PRESENT_FROM:
15024 case GOMP_MAP_PRESENT_TOFROM:
15025 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
15026 break;
15027 default:
15028 break;
15029 }
15030 switch (code)
15031 {
15032 case OACC_DATA:
15033 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
15034 break;
15035 /* Fallthrough. */
15036 case OACC_HOST_DATA:
15037 case OACC_ENTER_DATA:
15038 case OACC_EXIT_DATA:
15039 case OMP_TARGET_DATA:
15040 case OMP_TARGET_ENTER_DATA:
15041 case OMP_TARGET_EXIT_DATA:
15042 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15043 || (OMP_CLAUSE_MAP_KIND (c)
15044 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15045 /* For target {,enter ,exit }data only the array slice is
15046 mapped, but not the pointer to it. */
15047 remove = true;
15048 if (code == OMP_TARGET_EXIT_DATA
15049 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
15050 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
15051 remove = true;
15052 break;
15053 case OMP_TARGET:
15054 break;
15055 default:
15056 break;
15057 }
15058 if (remove)
15059 break;
15060 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15061 {
15062 /* Sanity check: attach/detach map kinds use the size as a bias,
15063 and it's never right to use the decl size for such
15064 mappings. */
15065 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15066 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15067 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
15068 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15069 && (OMP_CLAUSE_MAP_KIND (c)
15070 != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
15071 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
15072 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
15073 }
15074 gimplify_omp_ctxp = ctx->outer_context;
15075 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p, NULL,
15076 is_gimple_val, fb_rvalue) == GS_ERROR)
15077 {
15078 gimplify_omp_ctxp = ctx;
15079 remove = true;
15080 break;
15081 }
15082 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15083 || (OMP_CLAUSE_MAP_KIND (c)
15084 == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15085 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15086 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
15087 {
15088 OMP_CLAUSE_SIZE (c)
15089 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
15090 false);
15091 if ((ctx->region_type & ORT_TARGET) != 0)
15092 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
15093 GOVD_FIRSTPRIVATE | GOVD_SEEN);
15094 }
15095 gimplify_omp_ctxp = ctx;
15096 /* Data clauses associated with reductions must be
15097 compatible with present_or_copy. Warn and adjust the clause
15098 if that is not the case. */
15099 if (ctx->region_type == ORT_ACC_PARALLEL
15100 || ctx->region_type == ORT_ACC_SERIAL)
15101 {
15102 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
15103 n = NULL;
15104
15105 if (DECL_P (t))
15106 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
15107
15108 if (n && (n->value & GOVD_REDUCTION))
15109 {
15110 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
15111
15112 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15113 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
15114 && kind != GOMP_MAP_FORCE_PRESENT
15115 && kind != GOMP_MAP_POINTER)
15116 {
15117 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15118 "incompatible data clause with reduction "
15119 "on %qE; promoting to %<present_or_copy%>",
15120 DECL_NAME (t));
15121 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
15122 }
15123 }
15124 }
15125 if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15126 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15127 && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
15128 {
15129 remove = true;
15130 break;
15131 }
15132 /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
15133 a variable captured in a lambda closure), look through that now
15134 before the DECL_P check below. (A code other than COMPONENT_REF,
15135 i.e. INDIRECT_REF, will be a VLA/variable-length array
15136 section. A global var may be a variable in a common block. We
15137 don't want to do this here for either of those.) */
15138 if ((ctx->region_type & ORT_ACC) == 0
15139 && DECL_P (decl)
15140 && !is_global_var (decl)
15141 && DECL_HAS_VALUE_EXPR_P (decl)
15142 && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
15143 decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
15144 if (TREE_CODE (decl) == TARGET_EXPR)
15145 {
15146 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
15147 is_gimple_lvalue, fb_lvalue) == GS_ERROR)
15148 remove = true;
15149 }
15150 else if (!DECL_P (decl))
15151 {
15152 if ((ctx->region_type & ORT_TARGET) != 0
15153 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15154 {
15155 if (INDIRECT_REF_P (decl)
15156 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15157 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15158 == REFERENCE_TYPE))
15159 decl = TREE_OPERAND (decl, 0);
15160 if (TREE_CODE (decl) == COMPONENT_REF)
15161 {
15162 while (TREE_CODE (decl) == COMPONENT_REF)
15163 decl = TREE_OPERAND (decl, 0);
15164 if (DECL_P (decl))
15165 {
15166 n = splay_tree_lookup (ctx->variables,
15167 (splay_tree_key) decl);
15168 if (!(n->value & GOVD_SEEN))
15169 remove = true;
15170 }
15171 }
15172 }
15173
15174 tree d = decl, *pd;
15175 if (TREE_CODE (d) == ARRAY_REF)
15176 {
15177 while (TREE_CODE (d) == ARRAY_REF)
15178 d = TREE_OPERAND (d, 0);
15179 if (TREE_CODE (d) == COMPONENT_REF
15180 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
15181 decl = d;
15182 }
15183 pd = &OMP_CLAUSE_DECL (c);
15184 if (d == decl
15185 && TREE_CODE (decl) == INDIRECT_REF
15186 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15187 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15188 == REFERENCE_TYPE)
15189 && (OMP_CLAUSE_MAP_KIND (c)
15190 != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
15191 {
15192 pd = &TREE_OPERAND (decl, 0);
15193 decl = TREE_OPERAND (decl, 0);
15194 }
15195
15196 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15197 switch (code)
15198 {
15199 case OACC_ENTER_DATA:
15200 case OACC_EXIT_DATA:
15201 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
15202 == ARRAY_TYPE)
15203 remove = true;
15204 else if (code == OACC_ENTER_DATA)
15205 goto change_to_attach;
15206 /* Fallthrough. */
15207 case OMP_TARGET_EXIT_DATA:
15208 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
15209 break;
15210 case OACC_UPDATE:
15211 /* An "attach/detach" operation on an update directive
15212 should behave as a GOMP_MAP_ALWAYS_POINTER. Note that
15213 both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
15214 kinds depend on the previous mapping (for non-TARGET
15215 regions). */
15216 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
15217 break;
15218 default:
15219 change_to_attach:
15220 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
15221 if ((ctx->region_type & ORT_TARGET) != 0)
15222 move_attach = true;
15223 }
15224 else if ((ctx->region_type & ORT_TARGET) != 0
15225 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15226 || (OMP_CLAUSE_MAP_KIND (c)
15227 == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15228 move_attach = true;
15229
15230 /* If we have e.g. map(struct: *var), don't gimplify the
15231 argument since omp-low.cc wants to see the decl itself. */
15232 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
15233 break;
15234
15235 /* We've already partly gimplified this in
15236 gimplify_scan_omp_clauses. Don't do any more. */
15237 if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
15238 break;
15239
15240 gimplify_omp_ctxp = ctx->outer_context;
15241 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue,
15242 fb_lvalue) == GS_ERROR)
15243 remove = true;
15244 gimplify_omp_ctxp = ctx;
15245 break;
15246 }
15247
15248 if ((code == OMP_TARGET
15249 || code == OMP_TARGET_DATA
15250 || code == OMP_TARGET_ENTER_DATA
15251 || code == OMP_TARGET_EXIT_DATA)
15252 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15253 {
15254 bool firstprivatize = false;
15255
15256 for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
15257 octx = octx->outer_context)
15258 {
15259 splay_tree_node n
15260 = splay_tree_lookup (octx->variables,
15261 (splay_tree_key) OMP_CLAUSE_DECL (c));
15262 /* If this is contained in an outer OpenMP region as a
15263 firstprivate value, remove the attach/detach. */
15264 if (n && (n->value & GOVD_FIRSTPRIVATE))
15265 {
15266 firstprivatize = true;
15267 break;
15268 }
15269 }
15270
15271 enum gomp_map_kind map_kind;
15272 if (firstprivatize)
15273 map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
15274 else if (code == OMP_TARGET_EXIT_DATA)
15275 map_kind = GOMP_MAP_DETACH;
15276 else
15277 map_kind = GOMP_MAP_ATTACH;
15278 OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15279 }
15280 else if ((ctx->region_type & ORT_ACC) != 0
15281 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15282 {
15283 enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
15284 ? GOMP_MAP_DETACH
15285 : GOMP_MAP_ATTACH);
15286 OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15287 }
15288
15289 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15290 if ((ctx->region_type & ORT_TARGET) != 0
15291 && !(n->value & GOVD_SEEN)
15292 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
15293 && (!is_global_var (decl)
15294 || !lookup_attribute ("omp declare target link",
15295 DECL_ATTRIBUTES (decl))))
15296 {
15297 remove = true;
15298 /* For struct element mapping, if struct is never referenced
15299 in target block and none of the mapping has always modifier,
15300 remove all the struct element mappings, which immediately
15301 follow the GOMP_MAP_STRUCT map clause. */
15302 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15303 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15304 {
15305 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
15306 while (cnt--)
15307 OMP_CLAUSE_CHAIN (c)
15308 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
15309 }
15310 }
15311 else if (DECL_SIZE (decl)
15312 && !poly_int_tree_p (DECL_SIZE (decl))
15313 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
15314 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
15315 && (OMP_CLAUSE_MAP_KIND (c)
15316 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15317 {
15318 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
15319 for these, TREE_CODE (DECL_SIZE (decl)) will always be
15320 INTEGER_CST. */
15321 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
15322
15323 tree decl2 = DECL_VALUE_EXPR (decl);
15324 gcc_assert (INDIRECT_REF_P (decl2));
15325 decl2 = TREE_OPERAND (decl2, 0);
15326 gcc_assert (DECL_P (decl2));
15327 tree mem = build_simple_mem_ref (decl2);
15328 OMP_CLAUSE_DECL (c) = mem;
15329 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15330 if (ctx->outer_context)
15331 {
15332 omp_notice_variable (ctx->outer_context, decl2, true);
15333 omp_notice_variable (ctx->outer_context,
15334 OMP_CLAUSE_SIZE (c), true);
15335 }
15336 if (((ctx->region_type & ORT_TARGET) != 0
15337 || !ctx->target_firstprivatize_array_bases)
15338 && ((n->value & GOVD_SEEN) == 0
15339 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
15340 {
15341 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15342 OMP_CLAUSE_MAP);
15343 OMP_CLAUSE_DECL (nc) = decl;
15344 OMP_CLAUSE_SIZE (nc) = size_zero_node;
15345 if (ctx->target_firstprivatize_array_bases)
15346 OMP_CLAUSE_SET_MAP_KIND (nc,
15347 GOMP_MAP_FIRSTPRIVATE_POINTER);
15348 else
15349 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15350 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
15351 OMP_CLAUSE_CHAIN (c) = nc;
15352 c = nc;
15353 }
15354 }
15355 else
15356 {
15357 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15358 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
15359 gcc_assert ((n->value & GOVD_SEEN) == 0
15360 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
15361 == 0));
15362 }
15363
15364 /* If we have a target region, we can push all the attaches to the
15365 end of the list (we may have standalone "attach" operations
15366 synthesized for GOMP_MAP_STRUCT nodes that must be processed after
15367 the attachment point AND the pointed-to block have been mapped).
15368 If we have something else, e.g. "enter data", we need to keep
15369 "attach" nodes together with the previous node they attach to so
15370 that separate "exit data" operations work properly (see
15371 libgomp/target.c). */
15372 if ((ctx->region_type & ORT_TARGET) != 0
15373 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15374 || (OMP_CLAUSE_MAP_KIND (c)
15375 == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15376 move_attach = true;
15377
15378 break;
15379
15380 case OMP_CLAUSE_TO:
15381 case OMP_CLAUSE_FROM:
15382 case OMP_CLAUSE__CACHE_:
15383 decl = OMP_CLAUSE_DECL (c);
15384 if (!DECL_P (decl))
15385 break;
15386 if (DECL_SIZE (decl)
15387 && !poly_int_tree_p (DECL_SIZE (decl)))
15388 {
15389 tree decl2 = DECL_VALUE_EXPR (decl);
15390 gcc_assert (INDIRECT_REF_P (decl2));
15391 decl2 = TREE_OPERAND (decl2, 0);
15392 gcc_assert (DECL_P (decl2));
15393 tree mem = build_simple_mem_ref (decl2);
15394 OMP_CLAUSE_DECL (c) = mem;
15395 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15396 if (ctx->outer_context)
15397 {
15398 omp_notice_variable (ctx->outer_context, decl2, true);
15399 omp_notice_variable (ctx->outer_context,
15400 OMP_CLAUSE_SIZE (c), true);
15401 }
15402 }
15403 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15404 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
15405 break;
15406
15407 case OMP_CLAUSE_REDUCTION:
15408 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
15409 {
15410 decl = OMP_CLAUSE_DECL (c);
15411 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15412 if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
15413 {
15414 remove = true;
15415 error_at (OMP_CLAUSE_LOCATION (c),
15416 "%qD specified in %<inscan%> %<reduction%> clause "
15417 "but not in %<scan%> directive clause", decl);
15418 break;
15419 }
15420 has_inscan_reductions = true;
15421 }
15422 /* FALLTHRU */
15423 case OMP_CLAUSE_IN_REDUCTION:
15424 case OMP_CLAUSE_TASK_REDUCTION:
15425 decl = OMP_CLAUSE_DECL (c);
15426 /* OpenACC reductions need a present_or_copy data clause.
15427 Add one if necessary. Emit error when the reduction is private. */
15428 if (ctx->region_type == ORT_ACC_PARALLEL
15429 || ctx->region_type == ORT_ACC_SERIAL)
15430 {
15431 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15432 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
15433 {
15434 remove = true;
15435 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
15436 "reduction on %qE", DECL_NAME (decl));
15437 }
15438 else if ((n->value & GOVD_MAP) == 0)
15439 {
15440 tree next = OMP_CLAUSE_CHAIN (c);
15441 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
15442 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
15443 OMP_CLAUSE_DECL (nc) = decl;
15444 OMP_CLAUSE_CHAIN (c) = nc;
15445 lang_hooks.decls.omp_finish_clause (nc, pre_p,
15446 (ctx->region_type
15447 & ORT_ACC) != 0);
15448 while (1)
15449 {
15450 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
15451 if (OMP_CLAUSE_CHAIN (nc) == NULL)
15452 break;
15453 nc = OMP_CLAUSE_CHAIN (nc);
15454 }
15455 OMP_CLAUSE_CHAIN (nc) = next;
15456 n->value |= GOVD_MAP;
15457 }
15458 }
15459 if (DECL_P (decl)
15460 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15461 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15462 break;
15463
15464 case OMP_CLAUSE_ALLOCATE:
15465 decl = OMP_CLAUSE_DECL (c);
15466 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15467 if (n != NULL && !(n->value & GOVD_SEEN))
15468 {
15469 if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
15470 != 0
15471 && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
15472 remove = true;
15473 }
15474 if (!remove
15475 && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
15476 && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
15477 && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
15478 || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
15479 || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
15480 {
15481 tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
15482 n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
15483 if (n == NULL)
15484 {
15485 enum omp_clause_default_kind default_kind
15486 = ctx->default_kind;
15487 ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
15488 omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
15489 true);
15490 ctx->default_kind = default_kind;
15491 }
15492 else
15493 omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
15494 true);
15495 }
15496 break;
15497
15498 case OMP_CLAUSE_COPYIN:
15499 case OMP_CLAUSE_COPYPRIVATE:
15500 case OMP_CLAUSE_IF:
15501 case OMP_CLAUSE_SELF:
15502 case OMP_CLAUSE_NUM_THREADS:
15503 case OMP_CLAUSE_NUM_TEAMS:
15504 case OMP_CLAUSE_THREAD_LIMIT:
15505 case OMP_CLAUSE_DIST_SCHEDULE:
15506 case OMP_CLAUSE_DEVICE:
15507 case OMP_CLAUSE_SCHEDULE:
15508 case OMP_CLAUSE_NOWAIT:
15509 case OMP_CLAUSE_ORDERED:
15510 case OMP_CLAUSE_DEFAULT:
15511 case OMP_CLAUSE_UNTIED:
15512 case OMP_CLAUSE_COLLAPSE:
15513 case OMP_CLAUSE_FINAL:
15514 case OMP_CLAUSE_MERGEABLE:
15515 case OMP_CLAUSE_PROC_BIND:
15516 case OMP_CLAUSE_SAFELEN:
15517 case OMP_CLAUSE_SIMDLEN:
15518 case OMP_CLAUSE_DEPEND:
15519 case OMP_CLAUSE_DOACROSS:
15520 case OMP_CLAUSE_PRIORITY:
15521 case OMP_CLAUSE_GRAINSIZE:
15522 case OMP_CLAUSE_NUM_TASKS:
15523 case OMP_CLAUSE_NOGROUP:
15524 case OMP_CLAUSE_THREADS:
15525 case OMP_CLAUSE_SIMD:
15526 case OMP_CLAUSE_FILTER:
15527 case OMP_CLAUSE_HINT:
15528 case OMP_CLAUSE_DEFAULTMAP:
15529 case OMP_CLAUSE_ORDER:
15530 case OMP_CLAUSE_BIND:
15531 case OMP_CLAUSE_DETACH:
15532 case OMP_CLAUSE_USE_DEVICE_PTR:
15533 case OMP_CLAUSE_USE_DEVICE_ADDR:
15534 case OMP_CLAUSE_ASYNC:
15535 case OMP_CLAUSE_WAIT:
15536 case OMP_CLAUSE_INDEPENDENT:
15537 case OMP_CLAUSE_NUM_GANGS:
15538 case OMP_CLAUSE_NUM_WORKERS:
15539 case OMP_CLAUSE_VECTOR_LENGTH:
15540 case OMP_CLAUSE_GANG:
15541 case OMP_CLAUSE_WORKER:
15542 case OMP_CLAUSE_VECTOR:
15543 case OMP_CLAUSE_AUTO:
15544 case OMP_CLAUSE_SEQ:
15545 case OMP_CLAUSE_TILE:
15546 case OMP_CLAUSE_IF_PRESENT:
15547 case OMP_CLAUSE_FINALIZE:
15548 case OMP_CLAUSE_INCLUSIVE:
15549 case OMP_CLAUSE_EXCLUSIVE:
15550 break;
15551
15552 case OMP_CLAUSE_NOHOST:
15553 default:
15554 gcc_unreachable ();
15555 }
15556
15557 if (remove)
15558 *list_p = OMP_CLAUSE_CHAIN (c);
15559 else if (move_attach)
15560 {
15561 /* Remove attach node from here, separate out into its own list. */
15562 *attach_tail = c;
15563 *list_p = OMP_CLAUSE_CHAIN (c);
15564 OMP_CLAUSE_CHAIN (c) = NULL_TREE;
15565 attach_tail = &OMP_CLAUSE_CHAIN (c);
15566 }
15567 else
15568 list_p = &OMP_CLAUSE_CHAIN (c);
15569 }
15570
15571 /* Splice attach nodes at the end of the list. */
15572 if (attach_list)
15573 {
15574 *list_p = attach_list;
15575 list_p = attach_tail;
15576 }
15577
15578 /* Add in any implicit data sharing. */
15579 struct gimplify_adjust_omp_clauses_data data;
15580 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
15581 {
15582 /* OpenMP. Implicit clauses are added at the start of the clause list,
15583 but after any non-map clauses. */
15584 tree *implicit_add_list_p = orig_list_p;
15585 while (*implicit_add_list_p
15586 && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
15587 implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
15588 data.list_p = implicit_add_list_p;
15589 }
15590 else
15591 /* OpenACC. */
15592 data.list_p = list_p;
15593 data.pre_p = pre_p;
15594 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
15595
15596 if (has_inscan_reductions)
15597 for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
15598 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15599 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
15600 {
15601 error_at (OMP_CLAUSE_LOCATION (c),
15602 "%<inscan%> %<reduction%> clause used together with "
15603 "%<linear%> clause for a variable other than loop "
15604 "iterator");
15605 break;
15606 }
15607
15608 gimplify_omp_ctxp = ctx->outer_context;
15609 delete_omp_context (ctx);
15610 }
15611
15612 /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
15613 * -1 if run-time evaluation is needed. */
15614
15615 int
15616 omp_has_novariants (void)
15617 {
15618 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15619 if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
15620 {
15621 tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
15622 if (c != NULL_TREE)
15623 {
15624 if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
15625 return 1;
15626 else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
15627 return 0;
15628 else
15629 return -1;
15630 }
15631 return 0;
15632 }
15633 return 0;
15634 }
15635
15636 /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
15637 * -1 if run-time evaluation is needed. */
15638
15639 static int
15640 omp_has_nocontext (void)
15641 {
15642 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15643 if (ctx != NULL && ctx->code == OMP_DISPATCH)
15644 {
15645 tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
15646 if (c != NULL_TREE)
15647 {
15648 if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
15649 return 1;
15650 else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
15651 return 0;
15652 else
15653 return -1;
15654 }
15655 return 0;
15656 }
15657 return 0;
15658 }
15659
15660 /* Collect a list of traits for enclosing constructs in the current
15661 OpenMP context. The list is in the same format as the trait selector
15662 list of construct trait sets built by the front ends.
15663
15664 Per the OpenMP specification, the construct trait set includes constructs
15665 up to an enclosing "target" construct. If there is no "target" construct,
15666 then additional things may be added to the construct trait set (simd for
15667 simd clones, additional constructs associated with "declare variant",
15668 the target trait for "declare target"); those are not handled here.
15669 In particular simd clones are not known during gimplification so
15670 matching/scoring of context selectors that might involve them needs
15671 to be deferred to the omp_device_lower pass. */
15672
15673 tree
15674 omp_get_construct_context (void)
15675 {
15676 tree result = NULL_TREE;
15677 for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
15678 {
15679 if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
15680 == ORT_TARGET)
15681 && ctx->code == OMP_TARGET)
15682 {
15683 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
15684 NULL_TREE, NULL_TREE, result);
15685 /* We're not interested in any outer constructs. */
15686 break;
15687 }
15688 else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
15689 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
15690 NULL_TREE, NULL_TREE, result);
15691 else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
15692 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
15693 NULL_TREE, NULL_TREE, result);
15694 else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
15695 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
15696 NULL_TREE, NULL_TREE, result);
15697 else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
15698 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
15699 NULL_TREE, NULL_TREE, result);
15700 else if (ctx->region_type == ORT_SIMD
15701 && ctx->code == OMP_SIMD
15702 && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
15703 {
15704 tree props = NULL_TREE;
15705 tree *last = &props;
15706 for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
15707 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
15708 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
15709 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
15710 {
15711 *last = unshare_expr (c);
15712 last = &(OMP_CLAUSE_CHAIN (c));
15713 }
15714 result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
15715 NULL_TREE, props, result);
15716 }
15717 else if (ctx->region_type == ORT_WORKSHARE
15718 && ctx->code == OMP_LOOP
15719 && ctx->outer_context
15720 && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
15721 && ctx->outer_context->outer_context
15722 && ctx->outer_context->outer_context->code == OMP_LOOP
15723 && ctx->outer_context->outer_context->distribute)
15724 ctx = ctx->outer_context->outer_context;
15725 ctx = ctx->outer_context;
15726 }
15727
15728 return result;
15729 }
15730
15731 /* Gimplify OACC_CACHE. */
15732
15733 static void
15734 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
15735 {
15736 tree expr = *expr_p;
15737
15738 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
15739 OACC_CACHE);
15740 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
15741 OACC_CACHE);
15742
15743 /* TODO: Do something sensible with this information. */
15744
15745 *expr_p = NULL_TREE;
15746 }
15747
15748 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
15749 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
15750 kind. The entry kind will replace the one in CLAUSE, while the exit
15751 kind will be used in a new omp_clause and returned to the caller. */
15752
15753 static tree
15754 gimplify_oacc_declare_1 (tree clause)
15755 {
15756 HOST_WIDE_INT kind, new_op;
15757 bool ret = false;
15758 tree c = NULL;
15759
15760 kind = OMP_CLAUSE_MAP_KIND (clause);
15761
15762 switch (kind)
15763 {
15764 case GOMP_MAP_ALLOC:
15765 new_op = GOMP_MAP_RELEASE;
15766 ret = true;
15767 break;
15768
15769 case GOMP_MAP_FROM:
15770 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
15771 new_op = GOMP_MAP_FROM;
15772 ret = true;
15773 break;
15774
15775 case GOMP_MAP_TOFROM:
15776 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
15777 new_op = GOMP_MAP_FROM;
15778 ret = true;
15779 break;
15780
15781 case GOMP_MAP_DEVICE_RESIDENT:
15782 case GOMP_MAP_FORCE_DEVICEPTR:
15783 case GOMP_MAP_FORCE_PRESENT:
15784 case GOMP_MAP_LINK:
15785 case GOMP_MAP_POINTER:
15786 case GOMP_MAP_TO:
15787 break;
15788
15789 default:
15790 gcc_unreachable ();
15791 break;
15792 }
15793
15794 if (ret)
15795 {
15796 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
15797 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
15798 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
15799 }
15800
15801 return c;
15802 }
15803
15804 /* Gimplify OACC_DECLARE. */
15805
15806 static void
15807 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
15808 {
15809 tree expr = *expr_p;
15810 gomp_target *stmt;
15811 tree clauses, t, decl;
15812
15813 clauses = OACC_DECLARE_CLAUSES (expr);
15814
15815 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
15816 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
15817
15818 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
15819 {
15820 decl = OMP_CLAUSE_DECL (t);
15821
15822 if (TREE_CODE (decl) == MEM_REF)
15823 decl = TREE_OPERAND (decl, 0);
15824
15825 if (VAR_P (decl) && !is_oacc_declared (decl))
15826 {
15827 tree attr = get_identifier ("oacc declare target");
15828 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
15829 DECL_ATTRIBUTES (decl));
15830 }
15831
15832 if (VAR_P (decl)
15833 && !is_global_var (decl)
15834 && DECL_CONTEXT (decl) == current_function_decl)
15835 {
15836 tree c = gimplify_oacc_declare_1 (t);
15837 if (c)
15838 {
15839 if (oacc_declare_returns == NULL)
15840 oacc_declare_returns = new hash_map<tree, tree>;
15841
15842 oacc_declare_returns->put (decl, c);
15843 }
15844 }
15845
15846 if (gimplify_omp_ctxp)
15847 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
15848 }
15849
15850 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
15851 clauses);
15852
15853 gimplify_seq_add_stmt (pre_p, stmt);
15854
15855 *expr_p = NULL_TREE;
15856 }
15857
15858 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
15859 gimplification of the body, as well as scanning the body for used
15860 variables. We need to do this scan now, because variable-sized
15861 decls will be decomposed during gimplification. */
15862
15863 static void
15864 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
15865 {
15866 tree expr = *expr_p;
15867 gimple *g;
15868 gimple_seq body = NULL;
15869
15870 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
15871 OMP_PARALLEL_COMBINED (expr)
15872 ? ORT_COMBINED_PARALLEL
15873 : ORT_PARALLEL, OMP_PARALLEL);
15874
15875 push_gimplify_context ();
15876
15877 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
15878 if (gimple_code (g) == GIMPLE_BIND)
15879 pop_gimplify_context (g);
15880 else
15881 pop_gimplify_context (NULL);
15882
15883 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
15884 OMP_PARALLEL);
15885
15886 g = gimple_build_omp_parallel (body,
15887 OMP_PARALLEL_CLAUSES (expr),
15888 NULL_TREE, NULL_TREE);
15889 if (OMP_PARALLEL_COMBINED (expr))
15890 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
15891 gimplify_seq_add_stmt (pre_p, g);
15892 *expr_p = NULL_TREE;
15893 }
15894
15895 /* Gimplify the contents of an OMP_TASK statement. This involves
15896 gimplification of the body, as well as scanning the body for used
15897 variables. We need to do this scan now, because variable-sized
15898 decls will be decomposed during gimplification. */
15899
15900 static void
15901 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
15902 {
15903 tree expr = *expr_p;
15904 gimple *g;
15905 gimple_seq body = NULL;
15906 bool nowait = false;
15907 bool has_depend = false;
15908
15909 if (OMP_TASK_BODY (expr) == NULL_TREE)
15910 {
15911 for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
15912 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15913 {
15914 has_depend = true;
15915 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
15916 {
15917 error_at (OMP_CLAUSE_LOCATION (c),
15918 "%<mutexinoutset%> kind in %<depend%> clause on a "
15919 "%<taskwait%> construct");
15920 break;
15921 }
15922 }
15923 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
15924 nowait = true;
15925 if (nowait && !has_depend)
15926 {
15927 error_at (EXPR_LOCATION (expr),
15928 "%<taskwait%> construct with %<nowait%> clause but no "
15929 "%<depend%> clauses");
15930 *expr_p = NULL_TREE;
15931 return;
15932 }
15933 }
15934
15935 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
15936 omp_find_clause (OMP_TASK_CLAUSES (expr),
15937 OMP_CLAUSE_UNTIED)
15938 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
15939
15940 if (OMP_TASK_BODY (expr))
15941 {
15942 push_gimplify_context ();
15943
15944 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
15945 if (gimple_code (g) == GIMPLE_BIND)
15946 pop_gimplify_context (g);
15947 else
15948 pop_gimplify_context (NULL);
15949 }
15950
15951 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
15952 OMP_TASK);
15953
15954 g = gimple_build_omp_task (body,
15955 OMP_TASK_CLAUSES (expr),
15956 NULL_TREE, NULL_TREE,
15957 NULL_TREE, NULL_TREE, NULL_TREE);
15958 if (OMP_TASK_BODY (expr) == NULL_TREE)
15959 gimple_omp_task_set_taskwait_p (g, true);
15960 gimplify_seq_add_stmt (pre_p, g);
15961 *expr_p = NULL_TREE;
15962 }
15963
15964 /* Helper function for gimplify_omp_for. If *TP is not a gimple constant,
15965 force it into a temporary initialized in PRE_P and add firstprivate clause
15966 to ORIG_FOR_STMT. */
15967
15968 static void
15969 gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
15970 tree orig_for_stmt)
15971 {
15972 if (*tp == NULL || is_gimple_constant (*tp))
15973 return;
15974
15975 if (TREE_CODE (*tp) == SAVE_EXPR)
15976 gimplify_save_expr (tp, pre_p, NULL);
15977 else
15978 *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
15979 /* Reference to pointer conversion is considered useless,
15980 but is significant for firstprivate clause. Force it
15981 here. */
15982 if (type
15983 && TREE_CODE (type) == POINTER_TYPE
15984 && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
15985 {
15986 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
15987 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
15988 gimplify_and_add (m, pre_p);
15989 *tp = v;
15990 }
15991
15992 tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15993 OMP_CLAUSE_DECL (c) = *tp;
15994 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
15995 OMP_FOR_CLAUSES (orig_for_stmt) = c;
15996 }
15997
15998 /* Helper function of gimplify_omp_for, find OMP_ORDERED with
15999 null OMP_ORDERED_BODY inside of OMP_FOR's body. */
16000
16001 static tree
16002 find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
16003 {
16004 switch (TREE_CODE (*tp))
16005 {
16006 case OMP_ORDERED:
16007 if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
16008 return *tp;
16009 break;
16010 case OMP_SIMD:
16011 case OMP_PARALLEL:
16012 case OMP_TARGET:
16013 *walk_subtrees = 0;
16014 break;
16015 default:
16016 break;
16017 }
16018 return NULL_TREE;
16019 }
16020
16021 /* Gimplify standalone loop transforming directive which has the
16022 transformations applied already. So, all that is needed is gimplify
16023 the remaining loops as normal loops. */
16024
16025 static enum gimplify_status
16026 gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
16027 {
16028 tree for_stmt = *expr_p;
16029
16030 if (OMP_FOR_PRE_BODY (for_stmt))
16031 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
16032
16033 gimple_seq pre_body = NULL, post_body = NULL;
16034 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16035 {
16036 if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
16037 continue;
16038 tree iters = NULL_TREE;
16039 if (i == 0
16040 && TREE_CODE (for_stmt) == OMP_UNROLL
16041 && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
16042 {
16043 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
16044 iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
16045 else
16046 iters = build_int_cst (integer_type_node, 8);
16047 }
16048 tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16049 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16050 tree decl = TREE_OPERAND (t, 0);
16051 gcc_assert (DECL_P (decl));
16052 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16053 || POINTER_TYPE_P (TREE_TYPE (decl)));
16054 if (DECL_ARTIFICIAL (decl)
16055 && TREE_PRIVATE (t)
16056 && gimplify_omp_ctxp
16057 && gimplify_omp_ctxp->region_type != ORT_NONE)
16058 {
16059 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16060 do
16061 {
16062 splay_tree_node n
16063 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16064 if (n != NULL)
16065 break;
16066 else if (ctx->region_type != ORT_WORKSHARE
16067 && ctx->region_type != ORT_TASKGROUP
16068 && ctx->region_type != ORT_SIMD
16069 && ctx->region_type != ORT_ACC
16070 && !(ctx->region_type & ORT_TARGET_DATA))
16071 {
16072 omp_add_variable (ctx, decl, GOVD_PRIVATE);
16073 break;
16074 }
16075 ctx = ctx->outer_context;
16076 }
16077 while (ctx);
16078 }
16079 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16080 {
16081 gcc_assert (seen_error ());
16082 continue;
16083 }
16084 gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16085 fb_rvalue);
16086 gimplify_and_add (t, &pre_body);
16087 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16088 gcc_assert (TREE_OPERAND (t, 0) == decl);
16089 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16090 {
16091 gcc_assert (seen_error ());
16092 continue;
16093 }
16094 gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16095 fb_rvalue);
16096 tree l1 = create_artificial_label (UNKNOWN_LOCATION);
16097 tree l2 = create_artificial_label (UNKNOWN_LOCATION);
16098 tree l3 = create_artificial_label (UNKNOWN_LOCATION);
16099 gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
16100 gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
16101 gimple_seq this_post_body = NULL;
16102 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16103 if (TREE_CODE (t) == MODIFY_EXPR)
16104 {
16105 t = TREE_OPERAND (t, 1);
16106 if (TREE_CODE (t) == PLUS_EXPR
16107 && TREE_OPERAND (t, 1) == decl)
16108 {
16109 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
16110 TREE_OPERAND (t, 0) = decl;
16111 }
16112 gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16113 fb_rvalue);
16114 }
16115 gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
16116 &this_post_body);
16117 gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
16118 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16119 gcond *cond = NULL;
16120 tree d = decl;
16121 gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
16122 if (iters && tree_fits_uhwi_p (iters))
16123 {
16124 unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
16125 if ((unsigned HOST_WIDE_INT) (int) niters == niters
16126 && (int) niters > 0)
16127 {
16128 t = build2 (TREE_CODE (t), boolean_type_node, d,
16129 TREE_OPERAND (t, 1));
16130 t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
16131 build_int_cst (integer_type_node,
16132 annot_expr_unroll_kind),
16133 build_int_cst (integer_type_node, niters));
16134 gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
16135 fb_rvalue);
16136 cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
16137 l1, l3);
16138 }
16139 }
16140 if (cond == NULL)
16141 cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
16142 l1, l3);
16143 gimplify_seq_add_stmt (&this_post_body, cond);
16144 gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
16145 gimplify_seq_add_seq (&this_post_body, post_body);
16146 post_body = this_post_body;
16147 }
16148 gimplify_seq_add_seq (pre_p, pre_body);
16149 gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
16150 gimplify_seq_add_seq (pre_p, post_body);
16151
16152 *expr_p = NULL_TREE;
16153 return GS_ALL_DONE;
16154 }
16155
16156 /* Gimplify the gross structure of an OMP_FOR statement. */
16157
16158 static enum gimplify_status
16159 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
16160 {
16161 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
16162 enum gimplify_status ret = GS_ALL_DONE;
16163 enum gimplify_status tret;
16164 gomp_for *gfor;
16165 gimple_seq for_body, for_pre_body;
16166 int i;
16167 bitmap has_decl_expr = NULL;
16168 enum omp_region_type ort = ORT_WORKSHARE;
16169 bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
16170
16171 orig_for_stmt = for_stmt = *expr_p;
16172
16173 bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
16174 != NULL_TREE);
16175 while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16176 {
16177 tree *data[4] = { NULL, NULL, NULL, NULL };
16178 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
16179 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
16180 find_combined_omp_for, data, NULL);
16181 if (inner_for_stmt == NULL_TREE)
16182 {
16183 gcc_assert (seen_error ());
16184 *expr_p = NULL_TREE;
16185 return GS_ERROR;
16186 }
16187 gcc_assert (inner_for_stmt == *data[3]);
16188 omp_maybe_apply_loop_xforms (data[3],
16189 data[2]
16190 ? OMP_FOR_CLAUSES (*data[2])
16191 : TREE_CODE (for_stmt) == OMP_FOR
16192 ? OMP_FOR_CLAUSES (for_stmt)
16193 : NULL_TREE);
16194 if (inner_for_stmt != *data[3])
16195 continue;
16196 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
16197 {
16198 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
16199 &OMP_FOR_PRE_BODY (for_stmt));
16200 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
16201 }
16202 if (OMP_FOR_PRE_BODY (inner_for_stmt))
16203 {
16204 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
16205 &OMP_FOR_PRE_BODY (for_stmt));
16206 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
16207 }
16208
16209 if (data[0])
16210 {
16211 /* We have some statements or variable declarations in between
16212 the composite construct directives. Move them around the
16213 inner_for_stmt. */
16214 data[0] = expr_p;
16215 for (i = 0; i < 3; i++)
16216 if (data[i])
16217 {
16218 tree t = *data[i];
16219 if (i < 2 && data[i + 1] == &OMP_BODY (t))
16220 data[i + 1] = data[i];
16221 *data[i] = OMP_BODY (t);
16222 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
16223 NULL_TREE, make_node (BLOCK));
16224 OMP_BODY (t) = body;
16225 append_to_statement_list_force (inner_for_stmt,
16226 &BIND_EXPR_BODY (body));
16227 *data[3] = t;
16228 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
16229 gcc_assert (*data[3] == inner_for_stmt);
16230 }
16231 return GS_OK;
16232 }
16233
16234 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16235 if (!loop_p
16236 && OMP_FOR_ORIG_DECLS (inner_for_stmt)
16237 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16238 i)) == TREE_LIST
16239 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16240 i)))
16241 {
16242 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16243 /* Class iterators aren't allowed on OMP_SIMD, so the only
16244 case we need to solve is distribute parallel for. They are
16245 allowed on the loop construct, but that is already handled
16246 in gimplify_omp_loop. */
16247 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
16248 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
16249 && data[1]);
16250 tree orig_decl = TREE_PURPOSE (orig);
16251 tree last = TREE_VALUE (orig);
16252 tree *pc;
16253 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
16254 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
16255 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16256 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
16257 && OMP_CLAUSE_DECL (*pc) == orig_decl)
16258 break;
16259 if (*pc == NULL_TREE)
16260 {
16261 tree *spc;
16262 for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
16263 *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
16264 if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
16265 && OMP_CLAUSE_DECL (*spc) == orig_decl)
16266 break;
16267 if (*spc)
16268 {
16269 tree c = *spc;
16270 *spc = OMP_CLAUSE_CHAIN (c);
16271 OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16272 *pc = c;
16273 }
16274 }
16275 if (*pc == NULL_TREE)
16276 ;
16277 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
16278 {
16279 /* private clause will appear only on inner_for_stmt.
16280 Change it into firstprivate, and add private clause
16281 on for_stmt. */
16282 tree c = copy_node (*pc);
16283 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16284 OMP_FOR_CLAUSES (for_stmt) = c;
16285 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
16286 lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16287 }
16288 else
16289 {
16290 /* lastprivate clause will appear on both inner_for_stmt
16291 and for_stmt. Add firstprivate clause to
16292 inner_for_stmt. */
16293 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
16294 OMP_CLAUSE_FIRSTPRIVATE);
16295 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
16296 OMP_CLAUSE_CHAIN (c) = *pc;
16297 *pc = c;
16298 lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16299 }
16300 tree c = build_omp_clause (UNKNOWN_LOCATION,
16301 OMP_CLAUSE_FIRSTPRIVATE);
16302 OMP_CLAUSE_DECL (c) = last;
16303 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16304 OMP_PARALLEL_CLAUSES (*data[1]) = c;
16305 c = build_omp_clause (UNKNOWN_LOCATION,
16306 *pc ? OMP_CLAUSE_SHARED
16307 : OMP_CLAUSE_FIRSTPRIVATE);
16308 OMP_CLAUSE_DECL (c) = orig_decl;
16309 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16310 OMP_PARALLEL_CLAUSES (*data[1]) = c;
16311 }
16312 /* Similarly, take care of C++ range for temporaries, those should
16313 be firstprivate on OMP_PARALLEL if any. */
16314 if (data[1])
16315 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16316 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
16317 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16318 i)) == TREE_LIST
16319 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16320 i)))
16321 {
16322 tree orig
16323 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16324 tree v = TREE_CHAIN (orig);
16325 tree c = build_omp_clause (UNKNOWN_LOCATION,
16326 OMP_CLAUSE_FIRSTPRIVATE);
16327 /* First add firstprivate clause for the __for_end artificial
16328 decl. */
16329 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
16330 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
16331 == REFERENCE_TYPE)
16332 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
16333 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16334 OMP_PARALLEL_CLAUSES (*data[1]) = c;
16335 if (TREE_VEC_ELT (v, 0))
16336 {
16337 /* And now the same for __for_range artificial decl if it
16338 exists. */
16339 c = build_omp_clause (UNKNOWN_LOCATION,
16340 OMP_CLAUSE_FIRSTPRIVATE);
16341 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
16342 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
16343 == REFERENCE_TYPE)
16344 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
16345 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16346 OMP_PARALLEL_CLAUSES (*data[1]) = c;
16347 }
16348 }
16349 break;
16350 }
16351 if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
16352 {
16353 omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
16354 if (*expr_p != for_stmt)
16355 return GS_OK;
16356 }
16357
16358 switch (TREE_CODE (for_stmt))
16359 {
16360 case OMP_FOR:
16361 if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
16362 {
16363 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16364 OMP_CLAUSE_SCHEDULE))
16365 error_at (EXPR_LOCATION (for_stmt),
16366 "%qs clause may not appear on non-rectangular %qs",
16367 "schedule", lang_GNU_Fortran () ? "do" : "for");
16368 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
16369 error_at (EXPR_LOCATION (for_stmt),
16370 "%qs clause may not appear on non-rectangular %qs",
16371 "ordered", lang_GNU_Fortran () ? "do" : "for");
16372 }
16373 break;
16374 case OMP_DISTRIBUTE:
16375 if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
16376 && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16377 OMP_CLAUSE_DIST_SCHEDULE))
16378 error_at (EXPR_LOCATION (for_stmt),
16379 "%qs clause may not appear on non-rectangular %qs",
16380 "dist_schedule", "distribute");
16381 break;
16382 case OACC_LOOP:
16383 ort = ORT_ACC;
16384 break;
16385 case OMP_TASKLOOP:
16386 if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
16387 {
16388 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16389 OMP_CLAUSE_GRAINSIZE))
16390 error_at (EXPR_LOCATION (for_stmt),
16391 "%qs clause may not appear on non-rectangular %qs",
16392 "grainsize", "taskloop");
16393 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16394 OMP_CLAUSE_NUM_TASKS))
16395 error_at (EXPR_LOCATION (for_stmt),
16396 "%qs clause may not appear on non-rectangular %qs",
16397 "num_tasks", "taskloop");
16398 }
16399 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
16400 ort = ORT_UNTIED_TASKLOOP;
16401 else
16402 ort = ORT_TASKLOOP;
16403 break;
16404 case OMP_SIMD:
16405 ort = ORT_SIMD;
16406 break;
16407 case OMP_TILE:
16408 case OMP_UNROLL:
16409 gcc_assert (inner_for_stmt == NULL_TREE);
16410 return gimplify_omp_loop_xform (expr_p, pre_p);
16411 default:
16412 gcc_unreachable ();
16413 }
16414
16415 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
16416 clause for the IV. */
16417 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
16418 {
16419 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
16420 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16421 decl = TREE_OPERAND (t, 0);
16422 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
16423 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16424 && OMP_CLAUSE_DECL (c) == decl)
16425 {
16426 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
16427 break;
16428 }
16429 }
16430
16431 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
16432 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
16433 loop_p && TREE_CODE (for_stmt) != OMP_SIMD
16434 ? OMP_LOOP : TREE_CODE (for_stmt));
16435
16436 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
16437 gimplify_omp_ctxp->distribute = true;
16438
16439 /* Handle OMP_FOR_INIT. */
16440 for_pre_body = NULL;
16441 if ((ort == ORT_SIMD
16442 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
16443 && OMP_FOR_PRE_BODY (for_stmt))
16444 {
16445 has_decl_expr = BITMAP_ALLOC (NULL);
16446 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
16447 && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
16448 {
16449 t = OMP_FOR_PRE_BODY (for_stmt);
16450 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
16451 }
16452 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
16453 {
16454 tree_stmt_iterator si;
16455 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
16456 tsi_next (&si))
16457 {
16458 t = tsi_stmt (si);
16459 if (TREE_CODE (t) == DECL_EXPR
16460 && VAR_P (DECL_EXPR_DECL (t)))
16461 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
16462 }
16463 }
16464 }
16465 if (OMP_FOR_PRE_BODY (for_stmt))
16466 {
16467 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
16468 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
16469 else
16470 {
16471 struct gimplify_omp_ctx ctx;
16472 memset (&ctx, 0, sizeof (ctx));
16473 ctx.region_type = ORT_NONE;
16474 gimplify_omp_ctxp = &ctx;
16475 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
16476 gimplify_omp_ctxp = NULL;
16477 }
16478 }
16479 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
16480
16481 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16482 for_stmt = inner_for_stmt;
16483
16484 /* For taskloop, need to gimplify the start, end and step before the
16485 taskloop, outside of the taskloop omp context. */
16486 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
16487 {
16488 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16489 {
16490 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16491 gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
16492 ? pre_p : &for_pre_body);
16493 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
16494 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16495 {
16496 tree v = TREE_OPERAND (t, 1);
16497 gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
16498 for_pre_p, orig_for_stmt);
16499 gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
16500 for_pre_p, orig_for_stmt);
16501 }
16502 else
16503 gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
16504 orig_for_stmt);
16505
16506 /* Handle OMP_FOR_COND. */
16507 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16508 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16509 {
16510 tree v = TREE_OPERAND (t, 1);
16511 gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
16512 for_pre_p, orig_for_stmt);
16513 gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
16514 for_pre_p, orig_for_stmt);
16515 }
16516 else
16517 gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
16518 orig_for_stmt);
16519
16520 /* Handle OMP_FOR_INCR. */
16521 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16522 if (TREE_CODE (t) == MODIFY_EXPR)
16523 {
16524 decl = TREE_OPERAND (t, 0);
16525 t = TREE_OPERAND (t, 1);
16526 tree *tp = &TREE_OPERAND (t, 1);
16527 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
16528 tp = &TREE_OPERAND (t, 0);
16529
16530 gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
16531 orig_for_stmt);
16532 }
16533 }
16534
16535 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
16536 OMP_TASKLOOP);
16537 }
16538
16539 if (orig_for_stmt != for_stmt)
16540 gimplify_omp_ctxp->combined_loop = true;
16541
16542 for_body = NULL;
16543 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16544 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
16545 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16546 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
16547
16548 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
16549 bool is_doacross = false;
16550 if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
16551 find_standalone_omp_ordered, NULL))
16552 {
16553 OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
16554 is_doacross = true;
16555 int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
16556 gimplify_omp_ctxp->loop_iter_var.create (len * 2);
16557 for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
16558 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
16559 {
16560 error_at (OMP_CLAUSE_LOCATION (*pc),
16561 "%<linear%> clause may not be specified together "
16562 "with %<ordered%> clause if stand-alone %<ordered%> "
16563 "construct is nested in it");
16564 *pc = OMP_CLAUSE_CHAIN (*pc);
16565 }
16566 else
16567 pc = &OMP_CLAUSE_CHAIN (*pc);
16568 }
16569 int collapse = 1, tile = 0;
16570 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
16571 if (c)
16572 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
16573 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
16574 if (c)
16575 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
16576 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
16577 hash_set<tree> *allocate_uids = NULL;
16578 if (c)
16579 {
16580 allocate_uids = new hash_set<tree>;
16581 for (; c; c = OMP_CLAUSE_CHAIN (c))
16582 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
16583 allocate_uids->add (OMP_CLAUSE_DECL (c));
16584 }
16585 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16586 {
16587 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16588 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16589 decl = TREE_OPERAND (t, 0);
16590 gcc_assert (DECL_P (decl));
16591 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16592 || POINTER_TYPE_P (TREE_TYPE (decl)));
16593 if (is_doacross)
16594 {
16595 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
16596 {
16597 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16598 if (TREE_CODE (orig_decl) == TREE_LIST)
16599 {
16600 orig_decl = TREE_PURPOSE (orig_decl);
16601 if (!orig_decl)
16602 orig_decl = decl;
16603 }
16604 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
16605 }
16606 else
16607 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
16608 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
16609 }
16610
16611 if (for_stmt == orig_for_stmt)
16612 {
16613 tree orig_decl = decl;
16614 if (OMP_FOR_ORIG_DECLS (for_stmt))
16615 {
16616 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16617 if (TREE_CODE (orig_decl) == TREE_LIST)
16618 {
16619 orig_decl = TREE_PURPOSE (orig_decl);
16620 if (!orig_decl)
16621 orig_decl = decl;
16622 }
16623 }
16624 if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
16625 error_at (EXPR_LOCATION (for_stmt),
16626 "threadprivate iteration variable %qD", orig_decl);
16627 }
16628
16629 /* Make sure the iteration variable is private. */
16630 tree c = NULL_TREE;
16631 tree c2 = NULL_TREE;
16632 if (orig_for_stmt != for_stmt)
16633 {
16634 /* Preserve this information until we gimplify the inner simd. */
16635 if (has_decl_expr
16636 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
16637 TREE_PRIVATE (t) = 1;
16638 }
16639 else if (ort == ORT_SIMD)
16640 {
16641 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
16642 (splay_tree_key) decl);
16643 omp_is_private (gimplify_omp_ctxp, decl,
16644 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16645 != 1));
16646 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
16647 {
16648 omp_notice_variable (gimplify_omp_ctxp, decl, true);
16649 if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
16650 for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16651 OMP_CLAUSE_LASTPRIVATE);
16652 c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
16653 OMP_CLAUSE_LASTPRIVATE))
16654 if (OMP_CLAUSE_DECL (c3) == decl)
16655 {
16656 warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
16657 "conditional %<lastprivate%> on loop "
16658 "iterator %qD ignored", decl);
16659 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
16660 n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
16661 }
16662 }
16663 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
16664 {
16665 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
16666 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
16667 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
16668 if ((has_decl_expr
16669 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
16670 || TREE_PRIVATE (t))
16671 {
16672 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16673 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16674 }
16675 struct gimplify_omp_ctx *outer
16676 = gimplify_omp_ctxp->outer_context;
16677 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
16678 {
16679 if (outer->region_type == ORT_WORKSHARE
16680 && outer->combined_loop)
16681 {
16682 n = splay_tree_lookup (outer->variables,
16683 (splay_tree_key)decl);
16684 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
16685 {
16686 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16687 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16688 }
16689 else
16690 {
16691 struct gimplify_omp_ctx *octx = outer->outer_context;
16692 if (octx
16693 && octx->region_type == ORT_COMBINED_PARALLEL
16694 && octx->outer_context
16695 && (octx->outer_context->region_type
16696 == ORT_WORKSHARE)
16697 && octx->outer_context->combined_loop)
16698 {
16699 octx = octx->outer_context;
16700 n = splay_tree_lookup (octx->variables,
16701 (splay_tree_key)decl);
16702 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
16703 {
16704 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16705 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16706 }
16707 }
16708 }
16709 }
16710 }
16711
16712 OMP_CLAUSE_DECL (c) = decl;
16713 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16714 OMP_FOR_CLAUSES (for_stmt) = c;
16715 omp_add_variable (gimplify_omp_ctxp, decl, flags);
16716 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
16717 omp_lastprivate_for_combined_outer_constructs (outer, decl,
16718 true);
16719 }
16720 else
16721 {
16722 bool lastprivate
16723 = (!has_decl_expr
16724 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
16725 if (TREE_PRIVATE (t))
16726 lastprivate = false;
16727 if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
16728 {
16729 tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16730 if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
16731 lastprivate = false;
16732 }
16733
16734 struct gimplify_omp_ctx *outer
16735 = gimplify_omp_ctxp->outer_context;
16736 if (outer && lastprivate)
16737 omp_lastprivate_for_combined_outer_constructs (outer, decl,
16738 true);
16739
16740 c = build_omp_clause (input_location,
16741 lastprivate ? OMP_CLAUSE_LASTPRIVATE
16742 : OMP_CLAUSE_PRIVATE);
16743 OMP_CLAUSE_DECL (c) = decl;
16744 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16745 OMP_FOR_CLAUSES (for_stmt) = c;
16746 omp_add_variable (gimplify_omp_ctxp, decl,
16747 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
16748 | GOVD_EXPLICIT | GOVD_SEEN);
16749 c = NULL_TREE;
16750 }
16751 }
16752 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
16753 {
16754 omp_notice_variable (gimplify_omp_ctxp, decl, true);
16755 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
16756 (splay_tree_key) decl);
16757 if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
16758 for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16759 OMP_CLAUSE_LASTPRIVATE);
16760 c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
16761 OMP_CLAUSE_LASTPRIVATE))
16762 if (OMP_CLAUSE_DECL (c3) == decl)
16763 {
16764 warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
16765 "conditional %<lastprivate%> on loop "
16766 "iterator %qD ignored", decl);
16767 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
16768 n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
16769 }
16770 }
16771 else
16772 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
16773
16774 /* If DECL is not a gimple register, create a temporary variable to act
16775 as an iteration counter. This is valid, since DECL cannot be
16776 modified in the body of the loop. Similarly for any iteration vars
16777 in simd with collapse > 1 where the iterator vars must be
16778 lastprivate. And similarly for vars mentioned in allocate clauses. */
16779 if (orig_for_stmt != for_stmt)
16780 var = decl;
16781 else if (!is_gimple_reg (decl)
16782 || (ort == ORT_SIMD
16783 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
16784 || (allocate_uids && allocate_uids->contains (decl)))
16785 {
16786 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16787 /* Make sure omp_add_variable is not called on it prematurely.
16788 We call it ourselves a few lines later. */
16789 gimplify_omp_ctxp = NULL;
16790 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
16791 gimplify_omp_ctxp = ctx;
16792 TREE_OPERAND (t, 0) = var;
16793
16794 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
16795
16796 if (ort == ORT_SIMD
16797 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
16798 {
16799 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
16800 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
16801 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
16802 OMP_CLAUSE_DECL (c2) = var;
16803 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
16804 OMP_FOR_CLAUSES (for_stmt) = c2;
16805 omp_add_variable (gimplify_omp_ctxp, var,
16806 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
16807 if (c == NULL_TREE)
16808 {
16809 c = c2;
16810 c2 = NULL_TREE;
16811 }
16812 }
16813 else
16814 omp_add_variable (gimplify_omp_ctxp, var,
16815 GOVD_PRIVATE | GOVD_SEEN);
16816 }
16817 else
16818 var = decl;
16819
16820 gimplify_omp_ctxp->in_for_exprs = true;
16821 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16822 {
16823 tree lb = TREE_OPERAND (t, 1);
16824 tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
16825 is_gimple_val, fb_rvalue, false);
16826 ret = MIN (ret, tret);
16827 tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
16828 is_gimple_val, fb_rvalue, false);
16829 }
16830 else
16831 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
16832 is_gimple_val, fb_rvalue, false);
16833 gimplify_omp_ctxp->in_for_exprs = false;
16834 ret = MIN (ret, tret);
16835 if (ret == GS_ERROR)
16836 return ret;
16837
16838 /* Handle OMP_FOR_COND. */
16839 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16840 gcc_assert (COMPARISON_CLASS_P (t));
16841 gcc_assert (TREE_OPERAND (t, 0) == decl);
16842
16843 gimplify_omp_ctxp->in_for_exprs = true;
16844 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16845 {
16846 tree ub = TREE_OPERAND (t, 1);
16847 tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
16848 is_gimple_val, fb_rvalue, false);
16849 ret = MIN (ret, tret);
16850 tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
16851 is_gimple_val, fb_rvalue, false);
16852 }
16853 else
16854 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
16855 is_gimple_val, fb_rvalue, false);
16856 gimplify_omp_ctxp->in_for_exprs = false;
16857 ret = MIN (ret, tret);
16858
16859 /* Handle OMP_FOR_INCR. */
16860 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16861 switch (TREE_CODE (t))
16862 {
16863 case PREINCREMENT_EXPR:
16864 case POSTINCREMENT_EXPR:
16865 {
16866 tree decl = TREE_OPERAND (t, 0);
16867 /* c_omp_for_incr_canonicalize_ptr() should have been
16868 called to massage things appropriately. */
16869 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
16870
16871 if (orig_for_stmt != for_stmt)
16872 break;
16873 t = build_int_cst (TREE_TYPE (decl), 1);
16874 if (c)
16875 OMP_CLAUSE_LINEAR_STEP (c) = t;
16876 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
16877 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
16878 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
16879 break;
16880 }
16881
16882 case PREDECREMENT_EXPR:
16883 case POSTDECREMENT_EXPR:
16884 /* c_omp_for_incr_canonicalize_ptr() should have been
16885 called to massage things appropriately. */
16886 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
16887 if (orig_for_stmt != for_stmt)
16888 break;
16889 t = build_int_cst (TREE_TYPE (decl), -1);
16890 if (c)
16891 OMP_CLAUSE_LINEAR_STEP (c) = t;
16892 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
16893 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
16894 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
16895 break;
16896
16897 case MODIFY_EXPR:
16898 gcc_assert (TREE_OPERAND (t, 0) == decl);
16899 TREE_OPERAND (t, 0) = var;
16900
16901 t = TREE_OPERAND (t, 1);
16902 switch (TREE_CODE (t))
16903 {
16904 case PLUS_EXPR:
16905 if (TREE_OPERAND (t, 1) == decl)
16906 {
16907 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
16908 TREE_OPERAND (t, 0) = var;
16909 break;
16910 }
16911
16912 /* Fallthru. */
16913 case MINUS_EXPR:
16914 case POINTER_PLUS_EXPR:
16915 gcc_assert (TREE_OPERAND (t, 0) == decl);
16916 TREE_OPERAND (t, 0) = var;
16917 break;
16918 default:
16919 gcc_unreachable ();
16920 }
16921
16922 gimplify_omp_ctxp->in_for_exprs = true;
16923 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
16924 is_gimple_val, fb_rvalue, false);
16925 ret = MIN (ret, tret);
16926 if (c)
16927 {
16928 tree step = TREE_OPERAND (t, 1);
16929 tree stept = TREE_TYPE (decl);
16930 if (POINTER_TYPE_P (stept))
16931 stept = sizetype;
16932 step = fold_convert (stept, step);
16933 if (TREE_CODE (t) == MINUS_EXPR)
16934 step = fold_build1 (NEGATE_EXPR, stept, step);
16935 OMP_CLAUSE_LINEAR_STEP (c) = step;
16936 if (step != TREE_OPERAND (t, 1))
16937 {
16938 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
16939 &for_pre_body, NULL,
16940 is_gimple_val, fb_rvalue, false);
16941 ret = MIN (ret, tret);
16942 }
16943 }
16944 gimplify_omp_ctxp->in_for_exprs = false;
16945 break;
16946
16947 default:
16948 gcc_unreachable ();
16949 }
16950
16951 if (c2)
16952 {
16953 gcc_assert (c);
16954 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
16955 }
16956
16957 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
16958 {
16959 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
16960 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
16961 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
16962 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16963 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
16964 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
16965 && OMP_CLAUSE_DECL (c) == decl)
16966 {
16967 if (is_doacross && (collapse == 1 || i >= collapse))
16968 t = var;
16969 else
16970 {
16971 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16972 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16973 gcc_assert (TREE_OPERAND (t, 0) == var);
16974 t = TREE_OPERAND (t, 1);
16975 gcc_assert (TREE_CODE (t) == PLUS_EXPR
16976 || TREE_CODE (t) == MINUS_EXPR
16977 || TREE_CODE (t) == POINTER_PLUS_EXPR);
16978 gcc_assert (TREE_OPERAND (t, 0) == var);
16979 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
16980 is_doacross ? var : decl,
16981 TREE_OPERAND (t, 1));
16982 }
16983 gimple_seq *seq;
16984 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16985 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
16986 else
16987 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
16988 push_gimplify_context ();
16989 gimplify_assign (decl, t, seq);
16990 gimple *bind = NULL;
16991 if (gimplify_ctxp->temps)
16992 {
16993 bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
16994 *seq = NULL;
16995 gimplify_seq_add_stmt (seq, bind);
16996 }
16997 pop_gimplify_context (bind);
16998 }
16999 }
17000 if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
17001 for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17002 {
17003 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17004 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17005 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17006 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17007 TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17008 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17009 gcc_assert (COMPARISON_CLASS_P (t));
17010 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17011 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17012 TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17013 }
17014 }
17015
17016 BITMAP_FREE (has_decl_expr);
17017 delete allocate_uids;
17018
17019 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17020 || (loop_p && orig_for_stmt == for_stmt))
17021 {
17022 push_gimplify_context ();
17023 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
17024 {
17025 OMP_FOR_BODY (orig_for_stmt)
17026 = build3 (BIND_EXPR, void_type_node, NULL,
17027 OMP_FOR_BODY (orig_for_stmt), NULL);
17028 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
17029 }
17030 }
17031
17032 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
17033 &for_body);
17034
17035 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17036 || (loop_p && orig_for_stmt == for_stmt))
17037 {
17038 if (gimple_code (g) == GIMPLE_BIND)
17039 pop_gimplify_context (g);
17040 else
17041 pop_gimplify_context (NULL);
17042 }
17043
17044 if (orig_for_stmt != for_stmt)
17045 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17046 {
17047 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17048 decl = TREE_OPERAND (t, 0);
17049 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17050 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17051 gimplify_omp_ctxp = ctx->outer_context;
17052 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17053 gimplify_omp_ctxp = ctx;
17054 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
17055 TREE_OPERAND (t, 0) = var;
17056 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17057 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17058 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
17059 if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17060 for (int j = i + 1;
17061 j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17062 {
17063 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17064 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17065 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17066 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17067 {
17068 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17069 TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17070 }
17071 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17072 gcc_assert (COMPARISON_CLASS_P (t));
17073 if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17074 && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17075 {
17076 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17077 TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17078 }
17079 }
17080 }
17081
17082 gimplify_adjust_omp_clauses (pre_p, for_body,
17083 &OMP_FOR_CLAUSES (orig_for_stmt),
17084 TREE_CODE (orig_for_stmt));
17085
17086 int kind;
17087 switch (TREE_CODE (orig_for_stmt))
17088 {
17089 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
17090 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
17091 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
17092 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
17093 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
17094 default:
17095 gcc_unreachable ();
17096 }
17097 if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
17098 {
17099 gimplify_seq_add_seq (pre_p, for_pre_body);
17100 for_pre_body = NULL;
17101 }
17102 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
17103 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
17104 for_pre_body);
17105 if (orig_for_stmt != for_stmt)
17106 gimple_omp_for_set_combined_p (gfor, true);
17107 if (gimplify_omp_ctxp
17108 && (gimplify_omp_ctxp->combined_loop
17109 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
17110 && gimplify_omp_ctxp->outer_context
17111 && gimplify_omp_ctxp->outer_context->combined_loop)))
17112 {
17113 gimple_omp_for_set_combined_into_p (gfor, true);
17114 if (gimplify_omp_ctxp->combined_loop)
17115 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
17116 else
17117 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
17118 }
17119
17120 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17121 {
17122 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17123 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
17124 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
17125 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17126 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
17127 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
17128 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17129 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
17130 }
17131
17132 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
17133 constructs with GIMPLE_OMP_TASK sandwiched in between them.
17134 The outer taskloop stands for computing the number of iterations,
17135 counts for collapsed loops and holding taskloop specific clauses.
17136 The task construct stands for the effect of data sharing on the
17137 explicit task it creates and the inner taskloop stands for expansion
17138 of the static loop inside of the explicit task construct. */
17139 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17140 {
17141 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
17142 tree task_clauses = NULL_TREE;
17143 tree c = *gfor_clauses_ptr;
17144 tree *gtask_clauses_ptr = &task_clauses;
17145 tree outer_for_clauses = NULL_TREE;
17146 tree *gforo_clauses_ptr = &outer_for_clauses;
17147 bitmap lastprivate_uids = NULL;
17148 if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
17149 {
17150 c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
17151 if (c)
17152 {
17153 lastprivate_uids = BITMAP_ALLOC (NULL);
17154 for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
17155 OMP_CLAUSE_LASTPRIVATE))
17156 bitmap_set_bit (lastprivate_uids,
17157 DECL_UID (OMP_CLAUSE_DECL (c)));
17158 }
17159 c = *gfor_clauses_ptr;
17160 }
17161 for (; c; c = OMP_CLAUSE_CHAIN (c))
17162 switch (OMP_CLAUSE_CODE (c))
17163 {
17164 /* These clauses are allowed on task, move them there. */
17165 case OMP_CLAUSE_SHARED:
17166 case OMP_CLAUSE_FIRSTPRIVATE:
17167 case OMP_CLAUSE_DEFAULT:
17168 case OMP_CLAUSE_IF:
17169 case OMP_CLAUSE_UNTIED:
17170 case OMP_CLAUSE_FINAL:
17171 case OMP_CLAUSE_MERGEABLE:
17172 case OMP_CLAUSE_PRIORITY:
17173 case OMP_CLAUSE_REDUCTION:
17174 case OMP_CLAUSE_IN_REDUCTION:
17175 *gtask_clauses_ptr = c;
17176 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17177 break;
17178 case OMP_CLAUSE_PRIVATE:
17179 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
17180 {
17181 /* We want private on outer for and firstprivate
17182 on task. */
17183 *gtask_clauses_ptr
17184 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17185 OMP_CLAUSE_FIRSTPRIVATE);
17186 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17187 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17188 openacc);
17189 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17190 *gforo_clauses_ptr = c;
17191 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17192 }
17193 else
17194 {
17195 *gtask_clauses_ptr = c;
17196 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17197 }
17198 break;
17199 /* These clauses go into outer taskloop clauses. */
17200 case OMP_CLAUSE_GRAINSIZE:
17201 case OMP_CLAUSE_NUM_TASKS:
17202 case OMP_CLAUSE_NOGROUP:
17203 *gforo_clauses_ptr = c;
17204 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17205 break;
17206 /* Collapse clause we duplicate on both taskloops. */
17207 case OMP_CLAUSE_COLLAPSE:
17208 *gfor_clauses_ptr = c;
17209 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17210 *gforo_clauses_ptr = copy_node (c);
17211 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17212 break;
17213 /* For lastprivate, keep the clause on inner taskloop, and add
17214 a shared clause on task. If the same decl is also firstprivate,
17215 add also firstprivate clause on the inner taskloop. */
17216 case OMP_CLAUSE_LASTPRIVATE:
17217 if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17218 {
17219 /* For taskloop C++ lastprivate IVs, we want:
17220 1) private on outer taskloop
17221 2) firstprivate and shared on task
17222 3) lastprivate on inner taskloop */
17223 *gtask_clauses_ptr
17224 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17225 OMP_CLAUSE_FIRSTPRIVATE);
17226 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17227 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17228 openacc);
17229 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17230 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
17231 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17232 OMP_CLAUSE_PRIVATE);
17233 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
17234 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
17235 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
17236 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17237 }
17238 *gfor_clauses_ptr = c;
17239 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17240 *gtask_clauses_ptr
17241 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
17242 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17243 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
17244 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
17245 gtask_clauses_ptr
17246 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17247 break;
17248 /* Allocate clause we duplicate on task and inner taskloop
17249 if the decl is lastprivate, otherwise just put on task. */
17250 case OMP_CLAUSE_ALLOCATE:
17251 if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
17252 && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
17253 {
17254 /* Additionally, put firstprivate clause on task
17255 for the allocator if it is not constant. */
17256 *gtask_clauses_ptr
17257 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17258 OMP_CLAUSE_FIRSTPRIVATE);
17259 OMP_CLAUSE_DECL (*gtask_clauses_ptr)
17260 = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
17261 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17262 }
17263 if (lastprivate_uids
17264 && bitmap_bit_p (lastprivate_uids,
17265 DECL_UID (OMP_CLAUSE_DECL (c))))
17266 {
17267 *gfor_clauses_ptr = c;
17268 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17269 *gtask_clauses_ptr = copy_node (c);
17270 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17271 }
17272 else
17273 {
17274 *gtask_clauses_ptr = c;
17275 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17276 }
17277 break;
17278 default:
17279 gcc_unreachable ();
17280 }
17281 *gfor_clauses_ptr = NULL_TREE;
17282 *gtask_clauses_ptr = NULL_TREE;
17283 *gforo_clauses_ptr = NULL_TREE;
17284 BITMAP_FREE (lastprivate_uids);
17285 gimple_set_location (gfor, input_location);
17286 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
17287 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
17288 NULL_TREE, NULL_TREE, NULL_TREE);
17289 gimple_set_location (g, input_location);
17290 gimple_omp_task_set_taskloop_p (g, true);
17291 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
17292 gomp_for *gforo
17293 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
17294 gimple_omp_for_collapse (gfor),
17295 gimple_omp_for_pre_body (gfor));
17296 gimple_omp_for_set_pre_body (gfor, NULL);
17297 gimple_omp_for_set_combined_p (gforo, true);
17298 gimple_omp_for_set_combined_into_p (gfor, true);
17299 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
17300 {
17301 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
17302 tree v = create_tmp_var (type);
17303 gimple_omp_for_set_index (gforo, i, v);
17304 t = unshare_expr (gimple_omp_for_initial (gfor, i));
17305 gimple_omp_for_set_initial (gforo, i, t);
17306 gimple_omp_for_set_cond (gforo, i,
17307 gimple_omp_for_cond (gfor, i));
17308 t = unshare_expr (gimple_omp_for_final (gfor, i));
17309 gimple_omp_for_set_final (gforo, i, t);
17310 t = unshare_expr (gimple_omp_for_incr (gfor, i));
17311 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
17312 TREE_OPERAND (t, 0) = v;
17313 gimple_omp_for_set_incr (gforo, i, t);
17314 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17315 OMP_CLAUSE_DECL (t) = v;
17316 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
17317 gimple_omp_for_set_clauses (gforo, t);
17318 if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17319 {
17320 tree *p1 = NULL, *p2 = NULL;
17321 t = gimple_omp_for_initial (gforo, i);
17322 if (TREE_CODE (t) == TREE_VEC)
17323 p1 = &TREE_VEC_ELT (t, 0);
17324 t = gimple_omp_for_final (gforo, i);
17325 if (TREE_CODE (t) == TREE_VEC)
17326 {
17327 if (p1)
17328 p2 = &TREE_VEC_ELT (t, 0);
17329 else
17330 p1 = &TREE_VEC_ELT (t, 0);
17331 }
17332 if (p1)
17333 {
17334 int j;
17335 for (j = 0; j < i; j++)
17336 if (*p1 == gimple_omp_for_index (gfor, j))
17337 {
17338 *p1 = gimple_omp_for_index (gforo, j);
17339 if (p2)
17340 *p2 = *p1;
17341 break;
17342 }
17343 gcc_assert (j < i);
17344 }
17345 }
17346 }
17347 gimplify_seq_add_stmt (pre_p, gforo);
17348 }
17349 else
17350 gimplify_seq_add_stmt (pre_p, gfor);
17351
17352 if (TREE_CODE (orig_for_stmt) == OMP_FOR)
17353 {
17354 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17355 unsigned lastprivate_conditional = 0;
17356 while (ctx
17357 && (ctx->region_type == ORT_TARGET_DATA
17358 || ctx->region_type == ORT_TASKGROUP))
17359 ctx = ctx->outer_context;
17360 if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
17361 for (tree c = gimple_omp_for_clauses (gfor);
17362 c; c = OMP_CLAUSE_CHAIN (c))
17363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17364 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
17365 ++lastprivate_conditional;
17366 if (lastprivate_conditional)
17367 {
17368 struct omp_for_data fd;
17369 omp_extract_for_data (gfor, &fd, NULL);
17370 tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
17371 lastprivate_conditional);
17372 tree var = create_tmp_var_raw (type);
17373 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
17374 OMP_CLAUSE_DECL (c) = var;
17375 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
17376 gimple_omp_for_set_clauses (gfor, c);
17377 omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
17378 }
17379 }
17380 else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
17381 {
17382 unsigned lastprivate_conditional = 0;
17383 for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
17384 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17385 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
17386 ++lastprivate_conditional;
17387 if (lastprivate_conditional)
17388 {
17389 struct omp_for_data fd;
17390 omp_extract_for_data (gfor, &fd, NULL);
17391 tree type = unsigned_type_for (fd.iter_type);
17392 while (lastprivate_conditional--)
17393 {
17394 tree c = build_omp_clause (UNKNOWN_LOCATION,
17395 OMP_CLAUSE__CONDTEMP_);
17396 OMP_CLAUSE_DECL (c) = create_tmp_var (type);
17397 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
17398 gimple_omp_for_set_clauses (gfor, c);
17399 }
17400 }
17401 }
17402
17403 if (ret != GS_ALL_DONE)
17404 return GS_ERROR;
17405 *expr_p = NULL_TREE;
17406 return GS_ALL_DONE;
17407 }
17408
17409 /* Helper for gimplify_omp_loop, called through walk_tree. */
17410
17411 static tree
17412 note_no_context_vars (tree *tp, int *, void *data)
17413 {
17414 if (VAR_P (*tp)
17415 && DECL_CONTEXT (*tp) == NULL_TREE
17416 && !is_global_var (*tp))
17417 {
17418 vec<tree> *d = (vec<tree> *) data;
17419 d->safe_push (*tp);
17420 DECL_CONTEXT (*tp) = current_function_decl;
17421 }
17422 return NULL_TREE;
17423 }
17424
17425 /* Gimplify the gross structure of an OMP_LOOP statement. */
17426
17427 static enum gimplify_status
17428 gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
17429 {
17430 tree for_stmt = *expr_p;
17431 tree clauses = OMP_FOR_CLAUSES (for_stmt);
17432 struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
17433 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
17434 int i;
17435
17436 omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
17437 if (*expr_p != for_stmt)
17438 return GS_OK;
17439
17440 /* If order is not present, the behavior is as if order(concurrent)
17441 appeared. */
17442 tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
17443 if (order == NULL_TREE)
17444 {
17445 order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
17446 OMP_CLAUSE_CHAIN (order) = clauses;
17447 OMP_FOR_CLAUSES (for_stmt) = clauses = order;
17448 }
17449
17450 tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
17451 if (bind == NULL_TREE)
17452 {
17453 if (!flag_openmp) /* flag_openmp_simd */
17454 ;
17455 else if (octx && (octx->region_type & ORT_TEAMS) != 0)
17456 kind = OMP_CLAUSE_BIND_TEAMS;
17457 else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
17458 kind = OMP_CLAUSE_BIND_PARALLEL;
17459 else
17460 {
17461 for (; octx; octx = octx->outer_context)
17462 {
17463 if ((octx->region_type & ORT_ACC) != 0
17464 || octx->region_type == ORT_NONE
17465 || octx->region_type == ORT_IMPLICIT_TARGET)
17466 continue;
17467 break;
17468 }
17469 if (octx == NULL && !in_omp_construct)
17470 error_at (EXPR_LOCATION (for_stmt),
17471 "%<bind%> clause not specified on a %<loop%> "
17472 "construct not nested inside another OpenMP construct");
17473 }
17474 bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
17475 OMP_CLAUSE_CHAIN (bind) = clauses;
17476 OMP_CLAUSE_BIND_KIND (bind) = kind;
17477 OMP_FOR_CLAUSES (for_stmt) = bind;
17478 }
17479 else
17480 switch (OMP_CLAUSE_BIND_KIND (bind))
17481 {
17482 case OMP_CLAUSE_BIND_THREAD:
17483 break;
17484 case OMP_CLAUSE_BIND_PARALLEL:
17485 if (!flag_openmp) /* flag_openmp_simd */
17486 {
17487 OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17488 break;
17489 }
17490 for (; octx; octx = octx->outer_context)
17491 if (octx->region_type == ORT_SIMD
17492 && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
17493 {
17494 error_at (EXPR_LOCATION (for_stmt),
17495 "%<bind(parallel)%> on a %<loop%> construct nested "
17496 "inside %<simd%> construct");
17497 OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17498 break;
17499 }
17500 kind = OMP_CLAUSE_BIND_PARALLEL;
17501 break;
17502 case OMP_CLAUSE_BIND_TEAMS:
17503 if (!flag_openmp) /* flag_openmp_simd */
17504 {
17505 OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17506 break;
17507 }
17508 if ((octx
17509 && octx->region_type != ORT_IMPLICIT_TARGET
17510 && octx->region_type != ORT_NONE
17511 && (octx->region_type & ORT_TEAMS) == 0)
17512 || in_omp_construct)
17513 {
17514 error_at (EXPR_LOCATION (for_stmt),
17515 "%<bind(teams)%> on a %<loop%> region not strictly "
17516 "nested inside of a %<teams%> region");
17517 OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17518 break;
17519 }
17520 kind = OMP_CLAUSE_BIND_TEAMS;
17521 break;
17522 default:
17523 gcc_unreachable ();
17524 }
17525
17526 for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
17527 switch (OMP_CLAUSE_CODE (*pc))
17528 {
17529 case OMP_CLAUSE_REDUCTION:
17530 if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
17531 {
17532 error_at (OMP_CLAUSE_LOCATION (*pc),
17533 "%<inscan%> %<reduction%> clause on "
17534 "%qs construct", "loop");
17535 OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
17536 }
17537 if (OMP_CLAUSE_REDUCTION_TASK (*pc))
17538 {
17539 error_at (OMP_CLAUSE_LOCATION (*pc),
17540 "invalid %<task%> reduction modifier on construct "
17541 "other than %<parallel%>, %qs or %<sections%>",
17542 lang_GNU_Fortran () ? "do" : "for");
17543 OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
17544 }
17545 pc = &OMP_CLAUSE_CHAIN (*pc);
17546 break;
17547 case OMP_CLAUSE_LASTPRIVATE:
17548 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17549 {
17550 tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17551 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17552 if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
17553 break;
17554 if (OMP_FOR_ORIG_DECLS (for_stmt)
17555 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
17556 i)) == TREE_LIST
17557 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
17558 i)))
17559 {
17560 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17561 if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
17562 break;
17563 }
17564 }
17565 if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
17566 {
17567 error_at (OMP_CLAUSE_LOCATION (*pc),
17568 "%<lastprivate%> clause on a %<loop%> construct refers "
17569 "to a variable %qD which is not the loop iterator",
17570 OMP_CLAUSE_DECL (*pc));
17571 *pc = OMP_CLAUSE_CHAIN (*pc);
17572 break;
17573 }
17574 pc = &OMP_CLAUSE_CHAIN (*pc);
17575 break;
17576 default:
17577 pc = &OMP_CLAUSE_CHAIN (*pc);
17578 break;
17579 }
17580
17581 TREE_SET_CODE (for_stmt, OMP_SIMD);
17582
17583 int last;
17584 switch (kind)
17585 {
17586 case OMP_CLAUSE_BIND_THREAD: last = 0; break;
17587 case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
17588 case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
17589 }
17590 for (int pass = 1; pass <= last; pass++)
17591 {
17592 if (pass == 2)
17593 {
17594 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
17595 make_node (BLOCK));
17596 append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
17597 *expr_p = make_node (OMP_PARALLEL);
17598 TREE_TYPE (*expr_p) = void_type_node;
17599 OMP_PARALLEL_BODY (*expr_p) = bind;
17600 OMP_PARALLEL_COMBINED (*expr_p) = 1;
17601 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
17602 tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
17603 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17604 if (OMP_FOR_ORIG_DECLS (for_stmt)
17605 && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
17606 == TREE_LIST))
17607 {
17608 tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17609 if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
17610 {
17611 *pc = build_omp_clause (UNKNOWN_LOCATION,
17612 OMP_CLAUSE_FIRSTPRIVATE);
17613 OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
17614 pc = &OMP_CLAUSE_CHAIN (*pc);
17615 }
17616 }
17617 }
17618 tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
17619 tree *pc = &OMP_FOR_CLAUSES (t);
17620 TREE_TYPE (t) = void_type_node;
17621 OMP_FOR_BODY (t) = *expr_p;
17622 SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
17623 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
17624 switch (OMP_CLAUSE_CODE (c))
17625 {
17626 case OMP_CLAUSE_BIND:
17627 case OMP_CLAUSE_ORDER:
17628 case OMP_CLAUSE_COLLAPSE:
17629 *pc = copy_node (c);
17630 pc = &OMP_CLAUSE_CHAIN (*pc);
17631 break;
17632 case OMP_CLAUSE_PRIVATE:
17633 case OMP_CLAUSE_FIRSTPRIVATE:
17634 /* Only needed on innermost. */
17635 break;
17636 case OMP_CLAUSE_LASTPRIVATE:
17637 if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
17638 {
17639 *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17640 OMP_CLAUSE_FIRSTPRIVATE);
17641 OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
17642 lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
17643 pc = &OMP_CLAUSE_CHAIN (*pc);
17644 }
17645 *pc = copy_node (c);
17646 OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
17647 TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
17648 if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17649 {
17650 if (pass != last)
17651 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
17652 else
17653 lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
17654 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
17655 }
17656 pc = &OMP_CLAUSE_CHAIN (*pc);
17657 break;
17658 case OMP_CLAUSE_REDUCTION:
17659 *pc = copy_node (c);
17660 OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
17661 TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
17662 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
17663 {
17664 auto_vec<tree> no_context_vars;
17665 int walk_subtrees = 0;
17666 note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
17667 &walk_subtrees, &no_context_vars);
17668 if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
17669 note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
17670 walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
17671 note_no_context_vars,
17672 &no_context_vars);
17673 walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
17674 note_no_context_vars,
17675 &no_context_vars);
17676
17677 OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
17678 = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
17679 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
17680 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
17681 = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
17682
17683 hash_map<tree, tree> decl_map;
17684 decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
17685 decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
17686 OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
17687 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
17688 decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
17689 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
17690
17691 copy_body_data id;
17692 memset (&id, 0, sizeof (id));
17693 id.src_fn = current_function_decl;
17694 id.dst_fn = current_function_decl;
17695 id.src_cfun = cfun;
17696 id.decl_map = &decl_map;
17697 id.copy_decl = copy_decl_no_change;
17698 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
17699 id.transform_new_cfg = true;
17700 id.transform_return_to_modify = false;
17701 id.eh_lp_nr = 0;
17702 walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
17703 &id, NULL);
17704 walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
17705 &id, NULL);
17706
17707 for (tree d : no_context_vars)
17708 {
17709 DECL_CONTEXT (d) = NULL_TREE;
17710 DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
17711 }
17712 }
17713 else
17714 {
17715 OMP_CLAUSE_REDUCTION_INIT (*pc)
17716 = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
17717 OMP_CLAUSE_REDUCTION_MERGE (*pc)
17718 = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
17719 }
17720 pc = &OMP_CLAUSE_CHAIN (*pc);
17721 break;
17722 default:
17723 gcc_unreachable ();
17724 }
17725 *pc = NULL_TREE;
17726 *expr_p = t;
17727 }
17728 return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
17729 }
17730
17731
17732 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
17733 of OMP_TARGET's body. */
17734
17735 static tree
17736 find_omp_teams (tree *tp, int *walk_subtrees, void *)
17737 {
17738 *walk_subtrees = 0;
17739 switch (TREE_CODE (*tp))
17740 {
17741 case OMP_TEAMS:
17742 return *tp;
17743 case BIND_EXPR:
17744 case STATEMENT_LIST:
17745 *walk_subtrees = 1;
17746 break;
17747 default:
17748 break;
17749 }
17750 return NULL_TREE;
17751 }
17752
17753 /* Helper function of optimize_target_teams, determine if the expression
17754 can be computed safely before the target construct on the host. */
17755
17756 static tree
17757 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
17758 {
17759 splay_tree_node n;
17760
17761 if (TYPE_P (*tp))
17762 {
17763 *walk_subtrees = 0;
17764 return NULL_TREE;
17765 }
17766 switch (TREE_CODE (*tp))
17767 {
17768 case VAR_DECL:
17769 case PARM_DECL:
17770 case RESULT_DECL:
17771 *walk_subtrees = 0;
17772 if (error_operand_p (*tp)
17773 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
17774 || DECL_HAS_VALUE_EXPR_P (*tp)
17775 || DECL_THREAD_LOCAL_P (*tp)
17776 || TREE_SIDE_EFFECTS (*tp)
17777 || TREE_THIS_VOLATILE (*tp))
17778 return *tp;
17779 if (is_global_var (*tp)
17780 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
17781 || lookup_attribute ("omp declare target link",
17782 DECL_ATTRIBUTES (*tp))))
17783 return *tp;
17784 if (VAR_P (*tp)
17785 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
17786 && !is_global_var (*tp)
17787 && decl_function_context (*tp) == current_function_decl)
17788 return *tp;
17789 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17790 (splay_tree_key) *tp);
17791 if (n == NULL)
17792 {
17793 if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
17794 return NULL_TREE;
17795 return *tp;
17796 }
17797 else if (n->value & GOVD_LOCAL)
17798 return *tp;
17799 else if (n->value & GOVD_FIRSTPRIVATE)
17800 return NULL_TREE;
17801 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
17802 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
17803 return NULL_TREE;
17804 return *tp;
17805 case INTEGER_CST:
17806 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
17807 return *tp;
17808 return NULL_TREE;
17809 case TARGET_EXPR:
17810 if (TARGET_EXPR_INITIAL (*tp)
17811 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
17812 return *tp;
17813 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
17814 walk_subtrees, NULL);
17815 /* Allow some reasonable subset of integral arithmetics. */
17816 case PLUS_EXPR:
17817 case MINUS_EXPR:
17818 case MULT_EXPR:
17819 case TRUNC_DIV_EXPR:
17820 case CEIL_DIV_EXPR:
17821 case FLOOR_DIV_EXPR:
17822 case ROUND_DIV_EXPR:
17823 case TRUNC_MOD_EXPR:
17824 case CEIL_MOD_EXPR:
17825 case FLOOR_MOD_EXPR:
17826 case ROUND_MOD_EXPR:
17827 case RDIV_EXPR:
17828 case EXACT_DIV_EXPR:
17829 case MIN_EXPR:
17830 case MAX_EXPR:
17831 case LSHIFT_EXPR:
17832 case RSHIFT_EXPR:
17833 case BIT_IOR_EXPR:
17834 case BIT_XOR_EXPR:
17835 case BIT_AND_EXPR:
17836 case NEGATE_EXPR:
17837 case ABS_EXPR:
17838 case BIT_NOT_EXPR:
17839 case NON_LVALUE_EXPR:
17840 CASE_CONVERT:
17841 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
17842 return *tp;
17843 return NULL_TREE;
17844 /* And disallow anything else, except for comparisons. */
17845 default:
17846 if (COMPARISON_CLASS_P (*tp))
17847 return NULL_TREE;
17848 return *tp;
17849 }
17850 }
17851
17852 /* Try to determine if the num_teams and/or thread_limit expressions
17853 can have their values determined already before entering the
17854 target construct.
17855 INTEGER_CSTs trivially are,
17856 integral decls that are firstprivate (explicitly or implicitly)
17857 or explicitly map(always, to:) or map(always, tofrom:) on the target
17858 region too, and expressions involving simple arithmetics on those
17859 too, function calls are not ok, dereferencing something neither etc.
17860 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
17861 EXPR based on what we find:
17862 0 stands for clause not specified at all, use implementation default
17863 -1 stands for value that can't be determined easily before entering
17864 the target construct.
17865 -2 means that no explicit teams construct was specified
17866 If teams construct is not present at all, use 1 for num_teams
17867 and 0 for thread_limit (only one team is involved, and the thread
17868 limit is implementation defined. */
17869
17870 static void
17871 optimize_target_teams (tree target, gimple_seq *pre_p)
17872 {
17873 tree body = OMP_BODY (target);
17874 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
17875 tree num_teams_lower = NULL_TREE;
17876 tree num_teams_upper = integer_zero_node;
17877 tree thread_limit = integer_zero_node;
17878 location_t num_teams_loc = EXPR_LOCATION (target);
17879 location_t thread_limit_loc = EXPR_LOCATION (target);
17880 tree c, *p, expr;
17881 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
17882
17883 if (teams == NULL_TREE)
17884 num_teams_upper = build_int_cst (integer_type_node, -2);
17885 else
17886 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
17887 {
17888 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
17889 {
17890 p = &num_teams_upper;
17891 num_teams_loc = OMP_CLAUSE_LOCATION (c);
17892 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
17893 {
17894 expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
17895 if (TREE_CODE (expr) == INTEGER_CST)
17896 num_teams_lower = expr;
17897 else if (walk_tree (&expr, computable_teams_clause,
17898 NULL, NULL))
17899 num_teams_lower = integer_minus_one_node;
17900 else
17901 {
17902 num_teams_lower = expr;
17903 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
17904 if (gimplify_expr (&num_teams_lower, pre_p, NULL,
17905 is_gimple_val, fb_rvalue, false)
17906 == GS_ERROR)
17907 {
17908 gimplify_omp_ctxp = target_ctx;
17909 num_teams_lower = integer_minus_one_node;
17910 }
17911 else
17912 {
17913 gimplify_omp_ctxp = target_ctx;
17914 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
17915 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
17916 = num_teams_lower;
17917 }
17918 }
17919 }
17920 }
17921 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17922 {
17923 p = &thread_limit;
17924 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
17925 }
17926 else
17927 continue;
17928 expr = OMP_CLAUSE_OPERAND (c, 0);
17929 if (TREE_CODE (expr) == INTEGER_CST)
17930 {
17931 *p = expr;
17932 continue;
17933 }
17934 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
17935 {
17936 *p = integer_minus_one_node;
17937 continue;
17938 }
17939 *p = expr;
17940 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
17941 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
17942 == GS_ERROR)
17943 {
17944 gimplify_omp_ctxp = target_ctx;
17945 *p = integer_minus_one_node;
17946 continue;
17947 }
17948 gimplify_omp_ctxp = target_ctx;
17949 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
17950 OMP_CLAUSE_OPERAND (c, 0) = *p;
17951 }
17952 if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
17953 {
17954 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
17955 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
17956 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
17957 OMP_TARGET_CLAUSES (target) = c;
17958 }
17959 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
17960 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
17961 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
17962 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
17963 OMP_TARGET_CLAUSES (target) = c;
17964 }
17965
17966 /* Gimplify the gross structure of several OMP constructs. */
17967
17968 static void
17969 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
17970 {
17971 tree expr = *expr_p;
17972 gimple *stmt;
17973 gimple_seq body = NULL;
17974 enum omp_region_type ort;
17975
17976 switch (TREE_CODE (expr))
17977 {
17978 case OMP_SECTIONS:
17979 case OMP_SINGLE:
17980 ort = ORT_WORKSHARE;
17981 break;
17982 case OMP_SCOPE:
17983 ort = ORT_TASKGROUP;
17984 break;
17985 case OMP_TARGET:
17986 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
17987 break;
17988 case OACC_KERNELS:
17989 ort = ORT_ACC_KERNELS;
17990 break;
17991 case OACC_PARALLEL:
17992 ort = ORT_ACC_PARALLEL;
17993 break;
17994 case OACC_SERIAL:
17995 ort = ORT_ACC_SERIAL;
17996 break;
17997 case OACC_DATA:
17998 ort = ORT_ACC_DATA;
17999 break;
18000 case OMP_TARGET_DATA:
18001 ort = ORT_TARGET_DATA;
18002 break;
18003 case OMP_TEAMS:
18004 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
18005 if (gimplify_omp_ctxp == NULL
18006 || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
18007 ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
18008 break;
18009 case OACC_HOST_DATA:
18010 ort = ORT_ACC_HOST_DATA;
18011 break;
18012 default:
18013 gcc_unreachable ();
18014 }
18015
18016 bool save_in_omp_construct = in_omp_construct;
18017 if ((ort & ORT_ACC) == 0)
18018 in_omp_construct = false;
18019 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
18020 TREE_CODE (expr));
18021 if (TREE_CODE (expr) == OMP_TARGET)
18022 optimize_target_teams (expr, pre_p);
18023 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
18024 || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18025 {
18026 push_gimplify_context ();
18027 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
18028 if (gimple_code (g) == GIMPLE_BIND)
18029 pop_gimplify_context (g);
18030 else
18031 pop_gimplify_context (NULL);
18032 if ((ort & ORT_TARGET_DATA) != 0)
18033 {
18034 enum built_in_function end_ix;
18035 switch (TREE_CODE (expr))
18036 {
18037 case OACC_DATA:
18038 case OACC_HOST_DATA:
18039 end_ix = BUILT_IN_GOACC_DATA_END;
18040 break;
18041 case OMP_TARGET_DATA:
18042 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
18043 break;
18044 default:
18045 gcc_unreachable ();
18046 }
18047 tree fn = builtin_decl_explicit (end_ix);
18048 g = gimple_build_call (fn, 0);
18049 gimple_seq cleanup = NULL;
18050 gimple_seq_add_stmt (&cleanup, g);
18051 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
18052 body = NULL;
18053 gimple_seq_add_stmt (&body, g);
18054 }
18055 }
18056 else
18057 gimplify_and_add (OMP_BODY (expr), &body);
18058 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
18059 TREE_CODE (expr));
18060 in_omp_construct = save_in_omp_construct;
18061
18062 switch (TREE_CODE (expr))
18063 {
18064 case OACC_DATA:
18065 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
18066 OMP_CLAUSES (expr));
18067 break;
18068 case OACC_HOST_DATA:
18069 if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
18070 {
18071 for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18072 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
18073 OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
18074 }
18075
18076 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
18077 OMP_CLAUSES (expr));
18078 break;
18079 case OACC_KERNELS:
18080 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
18081 OMP_CLAUSES (expr));
18082 break;
18083 case OACC_PARALLEL:
18084 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
18085 OMP_CLAUSES (expr));
18086 break;
18087 case OACC_SERIAL:
18088 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
18089 OMP_CLAUSES (expr));
18090 break;
18091 case OMP_SECTIONS:
18092 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
18093 break;
18094 case OMP_SINGLE:
18095 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
18096 break;
18097 case OMP_SCOPE:
18098 stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
18099 break;
18100 case OMP_TARGET:
18101 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
18102 OMP_CLAUSES (expr));
18103 break;
18104 case OMP_TARGET_DATA:
18105 /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
18106 to be evaluated before the use_device_{ptr,addr} clauses if they
18107 refer to the same variables. */
18108 {
18109 tree use_device_clauses;
18110 tree *pc, *uc = &use_device_clauses;
18111 for (pc = &OMP_CLAUSES (expr); *pc; )
18112 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
18113 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
18114 {
18115 *uc = *pc;
18116 *pc = OMP_CLAUSE_CHAIN (*pc);
18117 uc = &OMP_CLAUSE_CHAIN (*uc);
18118 }
18119 else
18120 pc = &OMP_CLAUSE_CHAIN (*pc);
18121 *uc = NULL_TREE;
18122 *pc = use_device_clauses;
18123 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
18124 OMP_CLAUSES (expr));
18125 }
18126 break;
18127 case OMP_TEAMS:
18128 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
18129 if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18130 gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
18131 break;
18132 default:
18133 gcc_unreachable ();
18134 }
18135
18136 gimplify_seq_add_stmt (pre_p, stmt);
18137 *expr_p = NULL_TREE;
18138 }
18139
18140 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
18141 target update constructs. */
18142
18143 static void
18144 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
18145 {
18146 tree expr = *expr_p;
18147 int kind;
18148 gomp_target *stmt;
18149 enum omp_region_type ort = ORT_WORKSHARE;
18150
18151 switch (TREE_CODE (expr))
18152 {
18153 case OACC_ENTER_DATA:
18154 kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
18155 ort = ORT_ACC;
18156 break;
18157 case OACC_EXIT_DATA:
18158 kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
18159 ort = ORT_ACC;
18160 break;
18161 case OACC_UPDATE:
18162 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
18163 ort = ORT_ACC;
18164 break;
18165 case OMP_TARGET_UPDATE:
18166 kind = GF_OMP_TARGET_KIND_UPDATE;
18167 break;
18168 case OMP_TARGET_ENTER_DATA:
18169 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
18170 break;
18171 case OMP_TARGET_EXIT_DATA:
18172 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
18173 break;
18174 default:
18175 gcc_unreachable ();
18176 }
18177 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
18178 ort, TREE_CODE (expr));
18179 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
18180 TREE_CODE (expr));
18181 if (TREE_CODE (expr) == OACC_UPDATE
18182 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18183 OMP_CLAUSE_IF_PRESENT))
18184 {
18185 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
18186 clause. */
18187 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18188 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18189 switch (OMP_CLAUSE_MAP_KIND (c))
18190 {
18191 case GOMP_MAP_FORCE_TO:
18192 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
18193 break;
18194 case GOMP_MAP_FORCE_FROM:
18195 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
18196 break;
18197 default:
18198 break;
18199 }
18200 }
18201 else if (TREE_CODE (expr) == OACC_EXIT_DATA
18202 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18203 OMP_CLAUSE_FINALIZE))
18204 {
18205 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
18206 semantics. */
18207 bool have_clause = false;
18208 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18209 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18210 switch (OMP_CLAUSE_MAP_KIND (c))
18211 {
18212 case GOMP_MAP_FROM:
18213 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
18214 have_clause = true;
18215 break;
18216 case GOMP_MAP_RELEASE:
18217 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
18218 have_clause = true;
18219 break;
18220 case GOMP_MAP_TO_PSET:
18221 /* Fortran arrays with descriptors must map that descriptor when
18222 doing standalone "attach" operations (in OpenACC). In that
18223 case GOMP_MAP_TO_PSET appears by itself with no preceding
18224 clause (see trans-openmp.cc:gfc_trans_omp_clauses). */
18225 break;
18226 case GOMP_MAP_POINTER:
18227 /* TODO PR92929: we may see these here, but they'll always follow
18228 one of the clauses above, and will be handled by libgomp as
18229 one group, so no handling required here. */
18230 gcc_assert (have_clause);
18231 break;
18232 case GOMP_MAP_DETACH:
18233 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
18234 have_clause = false;
18235 break;
18236 case GOMP_MAP_STRUCT:
18237 case GOMP_MAP_STRUCT_UNORD:
18238 have_clause = false;
18239 break;
18240 default:
18241 gcc_unreachable ();
18242 }
18243 }
18244 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
18245
18246 gimplify_seq_add_stmt (pre_p, stmt);
18247 *expr_p = NULL_TREE;
18248 }
18249
18250 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
18251 stabilized the lhs of the atomic operation as *ADDR. Return true if
18252 EXPR is this stabilized form. */
18253
18254 static bool
18255 goa_lhs_expr_p (tree expr, tree addr)
18256 {
18257 /* Also include casts to other type variants. The C front end is fond
18258 of adding these for e.g. volatile variables. This is like
18259 STRIP_TYPE_NOPS but includes the main variant lookup. */
18260 STRIP_USELESS_TYPE_CONVERSION (expr);
18261
18262 if (INDIRECT_REF_P (expr))
18263 {
18264 expr = TREE_OPERAND (expr, 0);
18265 while (expr != addr
18266 && (CONVERT_EXPR_P (expr)
18267 || TREE_CODE (expr) == NON_LVALUE_EXPR)
18268 && TREE_CODE (expr) == TREE_CODE (addr)
18269 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
18270 {
18271 expr = TREE_OPERAND (expr, 0);
18272 addr = TREE_OPERAND (addr, 0);
18273 }
18274 if (expr == addr)
18275 return true;
18276 return (TREE_CODE (addr) == ADDR_EXPR
18277 && TREE_CODE (expr) == ADDR_EXPR
18278 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
18279 }
18280 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
18281 return true;
18282 return false;
18283 }
18284
18285 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
18286 expression does not involve the lhs, evaluate it into a temporary.
18287 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
18288 or -1 if an error was encountered. */
18289
18290 static int
18291 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
18292 tree lhs_var, tree &target_expr, bool rhs, int depth)
18293 {
18294 tree expr = *expr_p;
18295 int saw_lhs = 0;
18296
18297 if (goa_lhs_expr_p (expr, lhs_addr))
18298 {
18299 if (pre_p)
18300 *expr_p = lhs_var;
18301 return 1;
18302 }
18303 if (is_gimple_val (expr))
18304 return 0;
18305
18306 /* Maximum depth of lhs in expression is for the
18307 __builtin_clear_padding (...), __builtin_clear_padding (...),
18308 __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs; */
18309 if (++depth > 7)
18310 goto finish;
18311
18312 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
18313 {
18314 case tcc_binary:
18315 case tcc_comparison:
18316 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
18317 lhs_var, target_expr, true, depth);
18318 /* FALLTHRU */
18319 case tcc_unary:
18320 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
18321 lhs_var, target_expr, true, depth);
18322 break;
18323 case tcc_expression:
18324 switch (TREE_CODE (expr))
18325 {
18326 case TRUTH_ANDIF_EXPR:
18327 case TRUTH_ORIF_EXPR:
18328 case TRUTH_AND_EXPR:
18329 case TRUTH_OR_EXPR:
18330 case TRUTH_XOR_EXPR:
18331 case BIT_INSERT_EXPR:
18332 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18333 lhs_addr, lhs_var, target_expr, true,
18334 depth);
18335 /* FALLTHRU */
18336 case TRUTH_NOT_EXPR:
18337 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18338 lhs_addr, lhs_var, target_expr, true,
18339 depth);
18340 break;
18341 case MODIFY_EXPR:
18342 if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
18343 target_expr, true, depth))
18344 break;
18345 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18346 lhs_addr, lhs_var, target_expr, true,
18347 depth);
18348 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18349 lhs_addr, lhs_var, target_expr, false,
18350 depth);
18351 break;
18352 /* FALLTHRU */
18353 case ADDR_EXPR:
18354 if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
18355 target_expr, true, depth))
18356 break;
18357 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18358 lhs_addr, lhs_var, target_expr, false,
18359 depth);
18360 break;
18361 case COMPOUND_EXPR:
18362 /* Break out any preevaluations from cp_build_modify_expr. */
18363 for (; TREE_CODE (expr) == COMPOUND_EXPR;
18364 expr = TREE_OPERAND (expr, 1))
18365 {
18366 /* Special-case __builtin_clear_padding call before
18367 __builtin_memcmp. */
18368 if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
18369 {
18370 tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
18371 if (fndecl
18372 && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
18373 && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
18374 && (!pre_p
18375 || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
18376 lhs_addr, lhs_var,
18377 target_expr, true, depth)))
18378 {
18379 if (pre_p)
18380 *expr_p = expr;
18381 saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
18382 pre_p, lhs_addr, lhs_var,
18383 target_expr, true, depth);
18384 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
18385 pre_p, lhs_addr, lhs_var,
18386 target_expr, rhs, depth);
18387 return saw_lhs;
18388 }
18389 }
18390
18391 if (pre_p)
18392 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
18393 }
18394 if (!pre_p)
18395 return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
18396 target_expr, rhs, depth);
18397 *expr_p = expr;
18398 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
18399 target_expr, rhs, depth);
18400 case COND_EXPR:
18401 if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
18402 lhs_var, target_expr, true, depth))
18403 break;
18404 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18405 lhs_addr, lhs_var, target_expr, true,
18406 depth);
18407 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18408 lhs_addr, lhs_var, target_expr, true,
18409 depth);
18410 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
18411 lhs_addr, lhs_var, target_expr, true,
18412 depth);
18413 break;
18414 case TARGET_EXPR:
18415 if (TARGET_EXPR_INITIAL (expr))
18416 {
18417 if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
18418 lhs_var, target_expr, true,
18419 depth))
18420 break;
18421 if (expr == target_expr)
18422 saw_lhs = 1;
18423 else
18424 {
18425 saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
18426 pre_p, lhs_addr, lhs_var,
18427 target_expr, true, depth);
18428 if (saw_lhs && target_expr == NULL_TREE && pre_p)
18429 target_expr = expr;
18430 }
18431 }
18432 break;
18433 default:
18434 break;
18435 }
18436 break;
18437 case tcc_reference:
18438 if (TREE_CODE (expr) == BIT_FIELD_REF
18439 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
18440 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18441 lhs_addr, lhs_var, target_expr, true,
18442 depth);
18443 break;
18444 case tcc_vl_exp:
18445 if (TREE_CODE (expr) == CALL_EXPR)
18446 {
18447 if (tree fndecl = get_callee_fndecl (expr))
18448 if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
18449 BUILT_IN_MEMCMP))
18450 {
18451 int nargs = call_expr_nargs (expr);
18452 for (int i = 0; i < nargs; i++)
18453 saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
18454 pre_p, lhs_addr, lhs_var,
18455 target_expr, true, depth);
18456 }
18457 }
18458 break;
18459 default:
18460 break;
18461 }
18462
18463 finish:
18464 if (saw_lhs == 0 && pre_p)
18465 {
18466 enum gimplify_status gs;
18467 if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
18468 {
18469 gimplify_stmt (&expr, pre_p);
18470 return saw_lhs;
18471 }
18472 else if (rhs)
18473 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
18474 else
18475 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
18476 if (gs != GS_ALL_DONE)
18477 saw_lhs = -1;
18478 }
18479
18480 return saw_lhs;
18481 }
18482
18483 /* Gimplify an OMP_ATOMIC statement. */
18484
18485 static enum gimplify_status
18486 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
18487 {
18488 tree addr = TREE_OPERAND (*expr_p, 0);
18489 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
18490 ? NULL : TREE_OPERAND (*expr_p, 1);
18491 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
18492 tree tmp_load;
18493 gomp_atomic_load *loadstmt;
18494 gomp_atomic_store *storestmt;
18495 tree target_expr = NULL_TREE;
18496
18497 tmp_load = create_tmp_reg (type);
18498 if (rhs
18499 && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
18500 true, 0) < 0)
18501 return GS_ERROR;
18502
18503 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
18504 != GS_ALL_DONE)
18505 return GS_ERROR;
18506
18507 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
18508 OMP_ATOMIC_MEMORY_ORDER (*expr_p));
18509 gimplify_seq_add_stmt (pre_p, loadstmt);
18510 if (rhs)
18511 {
18512 /* BIT_INSERT_EXPR is not valid for non-integral bitfield
18513 representatives. Use BIT_FIELD_REF on the lhs instead. */
18514 tree rhsarg = rhs;
18515 if (TREE_CODE (rhs) == COND_EXPR)
18516 rhsarg = TREE_OPERAND (rhs, 1);
18517 if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
18518 && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
18519 {
18520 tree bitpos = TREE_OPERAND (rhsarg, 2);
18521 tree op1 = TREE_OPERAND (rhsarg, 1);
18522 tree bitsize;
18523 tree tmp_store = tmp_load;
18524 if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
18525 tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
18526 if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
18527 bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
18528 else
18529 bitsize = TYPE_SIZE (TREE_TYPE (op1));
18530 gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
18531 tree t = build2_loc (EXPR_LOCATION (rhsarg),
18532 MODIFY_EXPR, void_type_node,
18533 build3_loc (EXPR_LOCATION (rhsarg),
18534 BIT_FIELD_REF, TREE_TYPE (op1),
18535 tmp_store, bitsize, bitpos), op1);
18536 if (TREE_CODE (rhs) == COND_EXPR)
18537 t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
18538 TREE_OPERAND (rhs, 0), t, void_node);
18539 gimplify_and_add (t, pre_p);
18540 rhs = tmp_store;
18541 }
18542 bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
18543 if (TREE_CODE (rhs) == COND_EXPR)
18544 gimplify_ctxp->allow_rhs_cond_expr = true;
18545 enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
18546 is_gimple_val, fb_rvalue);
18547 gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
18548 if (gs != GS_ALL_DONE)
18549 return GS_ERROR;
18550 }
18551
18552 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
18553 rhs = tmp_load;
18554 storestmt
18555 = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
18556 if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
18557 {
18558 gimple_omp_atomic_set_weak (loadstmt);
18559 gimple_omp_atomic_set_weak (storestmt);
18560 }
18561 gimplify_seq_add_stmt (pre_p, storestmt);
18562 switch (TREE_CODE (*expr_p))
18563 {
18564 case OMP_ATOMIC_READ:
18565 case OMP_ATOMIC_CAPTURE_OLD:
18566 *expr_p = tmp_load;
18567 gimple_omp_atomic_set_need_value (loadstmt);
18568 break;
18569 case OMP_ATOMIC_CAPTURE_NEW:
18570 *expr_p = rhs;
18571 gimple_omp_atomic_set_need_value (storestmt);
18572 break;
18573 default:
18574 *expr_p = NULL;
18575 break;
18576 }
18577
18578 return GS_ALL_DONE;
18579 }
18580
18581 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
18582 body, and adding some EH bits. */
18583
18584 static enum gimplify_status
18585 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
18586 {
18587 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
18588 gimple *body_stmt;
18589 gtransaction *trans_stmt;
18590 gimple_seq body = NULL;
18591 int subcode = 0;
18592
18593 /* Wrap the transaction body in a BIND_EXPR so we have a context
18594 where to put decls for OMP. */
18595 if (TREE_CODE (tbody) != BIND_EXPR)
18596 {
18597 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
18598 TREE_SIDE_EFFECTS (bind) = 1;
18599 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
18600 TRANSACTION_EXPR_BODY (expr) = bind;
18601 }
18602
18603 push_gimplify_context ();
18604 temp = voidify_wrapper_expr (*expr_p, NULL);
18605
18606 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
18607 pop_gimplify_context (body_stmt);
18608
18609 trans_stmt = gimple_build_transaction (body);
18610 if (TRANSACTION_EXPR_OUTER (expr))
18611 subcode = GTMA_IS_OUTER;
18612 else if (TRANSACTION_EXPR_RELAXED (expr))
18613 subcode = GTMA_IS_RELAXED;
18614 gimple_transaction_set_subcode (trans_stmt, subcode);
18615
18616 gimplify_seq_add_stmt (pre_p, trans_stmt);
18617
18618 if (temp)
18619 {
18620 *expr_p = temp;
18621 return GS_OK;
18622 }
18623
18624 *expr_p = NULL_TREE;
18625 return GS_ALL_DONE;
18626 }
18627
18628 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
18629 is the OMP_BODY of the original EXPR (which has already been
18630 gimplified so it's not present in the EXPR).
18631
18632 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
18633
18634 static gimple *
18635 gimplify_omp_ordered (tree expr, gimple_seq body)
18636 {
18637 tree c, decls;
18638 int failures = 0;
18639 unsigned int i;
18640 tree source_c = NULL_TREE;
18641 tree sink_c = NULL_TREE;
18642
18643 if (gimplify_omp_ctxp)
18644 {
18645 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18646 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18647 && gimplify_omp_ctxp->loop_iter_var.is_empty ())
18648 {
18649 error_at (OMP_CLAUSE_LOCATION (c),
18650 "%<ordered%> construct with %qs clause must be "
18651 "closely nested inside a loop with %<ordered%> clause",
18652 OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
18653 failures++;
18654 }
18655 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18656 && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
18657 {
18658 bool fail = false;
18659 sink_c = c;
18660 if (OMP_CLAUSE_DECL (c) == NULL_TREE)
18661 continue; /* omp_cur_iteration - 1 */
18662 for (decls = OMP_CLAUSE_DECL (c), i = 0;
18663 decls && TREE_CODE (decls) == TREE_LIST;
18664 decls = TREE_CHAIN (decls), ++i)
18665 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
18666 continue;
18667 else if (TREE_VALUE (decls)
18668 != gimplify_omp_ctxp->loop_iter_var[2 * i])
18669 {
18670 error_at (OMP_CLAUSE_LOCATION (c),
18671 "variable %qE is not an iteration "
18672 "of outermost loop %d, expected %qE",
18673 TREE_VALUE (decls), i + 1,
18674 gimplify_omp_ctxp->loop_iter_var[2 * i]);
18675 fail = true;
18676 failures++;
18677 }
18678 else
18679 TREE_VALUE (decls)
18680 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
18681 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
18682 {
18683 error_at (OMP_CLAUSE_LOCATION (c),
18684 "number of variables in %qs clause with "
18685 "%<sink%> modifier does not match number of "
18686 "iteration variables",
18687 OMP_CLAUSE_DOACROSS_DEPEND (c)
18688 ? "depend" : "doacross");
18689 failures++;
18690 }
18691 }
18692 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18693 && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
18694 {
18695 if (source_c)
18696 {
18697 error_at (OMP_CLAUSE_LOCATION (c),
18698 "more than one %qs clause with %<source%> "
18699 "modifier on an %<ordered%> construct",
18700 OMP_CLAUSE_DOACROSS_DEPEND (source_c)
18701 ? "depend" : "doacross");
18702 failures++;
18703 }
18704 else
18705 source_c = c;
18706 }
18707 }
18708 if (source_c && sink_c)
18709 {
18710 error_at (OMP_CLAUSE_LOCATION (source_c),
18711 "%qs clause with %<source%> modifier specified "
18712 "together with %qs clauses with %<sink%> modifier "
18713 "on the same construct",
18714 OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
18715 OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
18716 failures++;
18717 }
18718
18719 if (failures)
18720 return gimple_build_nop ();
18721 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
18722 }
18723
18724 /* Gimplify an OMP_INTEROP statement. */
18725
18726 static enum gimplify_status
18727 gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
18728 {
18729 tree expr = *expr_p;
18730
18731 gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
18732 OMP_INTEROP);
18733 gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
18734 gimplify_seq_add_stmt (pre_p, stmt);
18735 *expr_p = NULL_TREE;
18736 return GS_ALL_DONE;
18737 }
18738
18739 /* Callback for walk_tree to find an IFN_GOMP_DISPATCH. */
18740
18741 static tree
18742 find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
18743 {
18744 tree t = *tp;
18745
18746 if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
18747 return *(tree *) modify ? *(tree *) modify : *tp;
18748
18749 if (TREE_CODE (t) == MODIFY_EXPR)
18750 *(tree *) modify = *tp;
18751
18752 return NULL_TREE;
18753 }
18754
18755 /* Gimplify an OMP_DISPATCH construct. */
18756
18757 static enum gimplify_status
18758 gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
18759 {
18760 tree expr = *expr_p;
18761 gimple_seq body = NULL;
18762
18763 gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
18764 OMP_DISPATCH);
18765 push_gimplify_context ();
18766
18767 // If device clause, adjust ICV
18768 tree device
18769 = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
18770 // If no device clause exists but an interop clause with a single list
18771 // item, use it to obtain the device number.
18772 if (device)
18773 device = OMP_CLAUSE_DEVICE_ID (device);
18774 else
18775 {
18776 tree first_interop_obj
18777 = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
18778 if (first_interop_obj)
18779 for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
18780 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
18781 {
18782 first_interop_obj = NULL_TREE;
18783 break;
18784 }
18785 if (first_interop_obj)
18786 {
18787 device = create_tmp_var (integer_type_node);
18788 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
18789 OMP_CLAUSE_DEVICE);
18790 OMP_CLAUSE_DEVICE_ID (c) = device;
18791 TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
18792 TREE_CHAIN (first_interop_obj) = c;
18793 first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
18794 /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL); */
18795 tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
18796 fn = build_call_expr (fn, 3, first_interop_obj,
18797 build_int_cst (integer_type_node, -5),
18798 null_pointer_node);
18799 gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
18800 }
18801 }
18802 tree saved_device_icv = NULL_TREE;
18803 if (device
18804 && (TREE_CODE (device) != INTEGER_CST
18805 || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
18806 {
18807 // Save current default-device-var ICV
18808 saved_device_icv = create_tmp_var (integer_type_node);
18809 tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
18810 gcall *call = gimple_build_call (fn, 0);
18811 gimple_call_set_lhs (call, saved_device_icv);
18812 gimplify_seq_add_stmt (&body, call);
18813
18814 // Set default device
18815 fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
18816 call = gimple_build_call (fn, 1, device);
18817 gimplify_seq_add_stmt (&body, call);
18818 }
18819
18820 // If the novariants and nocontext clauses are not compile-time constants,
18821 // we need to generate code for all possible cases:
18822 // if (novariants) // implies nocontext
18823 // base()
18824 // else if (nocontext)
18825 // variant1()
18826 // else
18827 // variant2()
18828 tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
18829 if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
18830 dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
18831 tree dispatch_body = *dispatch_body_p;
18832
18833 // Look for IFN_GOMP_DISPATCH and extract the base function call
18834 tree base_call_expr = NULL_TREE;
18835 if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
18836 for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
18837 tsi_next (&tsi))
18838 {
18839 tree modify = NULL_TREE;
18840 tree stmt = tsi_stmt (tsi);
18841 base_call_expr
18842 = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
18843 if (base_call_expr != NULL_TREE)
18844 break;
18845 }
18846 else
18847 {
18848 tree modify = NULL_TREE;
18849 base_call_expr
18850 = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
18851 }
18852 gcc_assert (base_call_expr != NULL_TREE);
18853
18854 tree dst = NULL_TREE;
18855 if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
18856 {
18857 dst = TREE_OPERAND (base_call_expr, 0);
18858 base_call_expr = TREE_OPERAND (base_call_expr, 1);
18859 }
18860
18861 while (TREE_CODE (base_call_expr) == FLOAT_EXPR
18862 || TREE_CODE (base_call_expr) == CONVERT_EXPR
18863 || TREE_CODE (base_call_expr) == COMPLEX_EXPR
18864 || TREE_CODE (base_call_expr) == INDIRECT_REF
18865 || TREE_CODE (base_call_expr) == NOP_EXPR)
18866 base_call_expr = TREE_OPERAND (base_call_expr, 0);
18867
18868 gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
18869 base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
18870
18871 tree base_fndecl = get_callee_fndecl (base_call_expr);
18872 if (base_fndecl != NULL_TREE)
18873 {
18874 if (DECL_VIRTUAL_P (base_fndecl))
18875 {
18876 error_at (
18877 EXPR_LOCATION (base_call_expr),
18878 "%qD is a virtual function but only a direct call is allowed "
18879 "in a dispatch construct",
18880 DECL_NAME (base_fndecl));
18881 }
18882
18883 /* We are not actually going to expand the variant call or use
18884 the result of omp_get_dynamic candidates here; only check that
18885 it does not trivially resolve to a call to the base function
18886 so that we can avoid some extra work in building code that's
18887 not needed in that case. */
18888 tree construct_context = omp_get_construct_context ();
18889 vec<struct omp_variant> all_candidates
18890 = omp_declare_variant_candidates (base_fndecl, construct_context);
18891 gcc_assert (!all_candidates.is_empty ());
18892 vec<struct omp_variant> candidates
18893 = omp_get_dynamic_candidates (all_candidates, construct_context);
18894 tree variant_fndecl
18895 = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
18896
18897 if (base_fndecl != variant_fndecl
18898 && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
18899 {
18900 tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
18901 novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
18902 for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
18903 {
18904 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
18905 && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
18906 {
18907 gcc_assert (novariants_cond == NULL_TREE);
18908 novariants_clause = c;
18909 novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
18910 }
18911 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
18912 && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
18913 {
18914 gcc_assert (nocontext_cond == NULL_TREE);
18915 nocontext_clause = c;
18916 nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
18917 }
18918 }
18919 gcc_assert (novariants_cond != NULL_TREE
18920 || nocontext_cond != NULL_TREE);
18921
18922 enum gimplify_status ret
18923 = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
18924 fb_rvalue);
18925 if (ret == GS_ERROR || ret == GS_UNHANDLED)
18926 return ret;
18927 ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
18928 fb_rvalue);
18929 if (ret == GS_ERROR || ret == GS_UNHANDLED)
18930 return ret;
18931
18932 tree end_label = create_artificial_label (UNKNOWN_LOCATION);
18933
18934 if (novariants_cond != NULL_TREE)
18935 {
18936 tree base_label = create_artificial_label (UNKNOWN_LOCATION);
18937 tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
18938 gcond *novariants_cond_stmt
18939 = gimple_build_cond_from_tree (novariants_cond, base_label,
18940 cond_label);
18941 gimplify_seq_add_stmt (&body, novariants_cond_stmt);
18942
18943 gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
18944 tree base_call_expr2 = copy_node (base_call_expr);
18945 base_call_expr2
18946 = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
18947 IFN_GOMP_DISPATCH,
18948 TREE_TYPE (base_call_expr2), 1,
18949 base_call_expr2);
18950 if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
18951 {
18952 base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
18953 base_call_expr2);
18954 }
18955 OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
18956 = boolean_true_node;
18957 gimplify_and_add (base_call_expr2, &body);
18958 gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
18959
18960 OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
18961 = boolean_false_node;
18962 gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
18963 }
18964
18965 if (nocontext_cond != NULL_TREE)
18966 {
18967 tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
18968 tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
18969 gcond *nocontext_cond_stmt
18970 = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
18971 variant2_label);
18972 gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
18973
18974 gimplify_seq_add_stmt (&body,
18975 gimple_build_label (variant1_label));
18976 tree variant_call_expr = copy_node (base_call_expr);
18977 variant_call_expr = build_call_expr_internal_loc (
18978 EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
18979 TREE_TYPE (variant_call_expr), 1, variant_call_expr);
18980 if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
18981 {
18982 variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
18983 variant_call_expr);
18984 }
18985 OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
18986 gimplify_and_add (variant_call_expr, &body);
18987 gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
18988 OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
18989 gimplify_seq_add_stmt (&body,
18990 gimple_build_label (variant2_label));
18991 }
18992
18993 tree variant_call_expr = base_call_expr;
18994 variant_call_expr
18995 = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
18996 IFN_GOMP_DISPATCH,
18997 TREE_TYPE (variant_call_expr), 1,
18998 variant_call_expr);
18999 if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19000 {
19001 variant_call_expr
19002 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
19003 }
19004 gimplify_and_add (variant_call_expr, &body);
19005 gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
19006 }
19007 else
19008 gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19009 }
19010 else
19011 gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19012
19013 // Restore default-device-var ICV
19014 if (saved_device_icv != NULL_TREE)
19015 {
19016 tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19017 gcall *call = gimple_build_call (fn, 1, saved_device_icv);
19018 gimplify_seq_add_stmt (&body, call);
19019 }
19020
19021 // Wrap dispatch body into a bind
19022 gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
19023 pop_gimplify_context (bind);
19024
19025 // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
19026 // call to gimplify_adjust_omp_clauses
19027 gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
19028 if (ctx != NULL)
19029 {
19030 gcc_assert (ctx->code == OMP_DISPATCH);
19031 gimplify_omp_ctxp = ctx->outer_context;
19032 delete_omp_context (ctx);
19033 }
19034
19035 // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
19036 // has been handled above, and depend as the front end handled it by inserting
19037 // taskwait.
19038 tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
19039 for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
19040 {
19041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
19042 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
19043 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
19044 {
19045 *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
19046 break;
19047 }
19048 else
19049 dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
19050 }
19051
19052 gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
19053 gimplify_seq_add_stmt (pre_p, stmt);
19054 *expr_p = NULL_TREE;
19055 return GS_ALL_DONE;
19056 }
19057
19058 /* Expand a metadirective that has been resolved at gimplification time
19059 into the candidate directive variants in CANDIDATES. */
19060
19061 static enum gimplify_status
19062 expand_omp_metadirective (vec<struct omp_variant> &candidates,
19063 gimple_seq *pre_p)
19064 {
19065 auto_vec<tree> selectors;
19066 auto_vec<tree> directive_labels;
19067 auto_vec<gimple_seq> directive_bodies;
19068 tree body_label = NULL_TREE;
19069 tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19070
19071 /* Construct bodies for each candidate. */
19072 for (unsigned i = 0; i < candidates.length(); i++)
19073 {
19074 struct omp_variant &candidate = candidates[i];
19075 gimple_seq body = NULL;
19076
19077 selectors.safe_push (omp_dynamic_cond (candidate.selector,
19078 find_supercontext ()));
19079 directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19080
19081 gimplify_seq_add_stmt (&body,
19082 gimple_build_label (directive_labels.last ()));
19083 if (candidate.alternative != NULL_TREE)
19084 gimplify_stmt (&candidate.alternative, &body);
19085 if (candidate.body != NULL_TREE)
19086 {
19087 if (body_label != NULL_TREE)
19088 gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
19089 else
19090 {
19091 body_label = create_artificial_label (UNKNOWN_LOCATION);
19092 gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
19093 gimplify_stmt (&candidate.body, &body);
19094 }
19095 }
19096
19097 directive_bodies.safe_push (body);
19098 }
19099
19100 auto_vec<tree> cond_labels;
19101
19102 cond_labels.safe_push (NULL_TREE);
19103 for (unsigned i = 1; i < candidates.length () - 1; i++)
19104 cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19105 if (candidates.length () > 1)
19106 cond_labels.safe_push (directive_labels.last ());
19107
19108 /* Generate conditionals to test each dynamic selector in turn, executing
19109 the directive candidate if successful. */
19110 for (unsigned i = 0; i < candidates.length () - 1; i++)
19111 {
19112 if (i != 0)
19113 gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
19114
19115 enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
19116 is_gimple_val, fb_rvalue);
19117 if (ret == GS_ERROR || ret == GS_UNHANDLED)
19118 return ret;
19119
19120 gcond *cond_stmt
19121 = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
19122 cond_labels[i + 1]);
19123
19124 gimplify_seq_add_stmt (pre_p, cond_stmt);
19125 gimplify_seq_add_seq (pre_p, directive_bodies[i]);
19126 gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
19127 }
19128
19129 gimplify_seq_add_seq (pre_p, directive_bodies.last ());
19130 gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
19131
19132 return GS_ALL_DONE;
19133 }
19134
19135 /* Expand a variant construct that requires late resolution in the ompdevlow
19136 pass. It's a bit easier to do this in tree form and then gimplify that,
19137 than to emit gimple. The output is going to look something like:
19138
19139 switch_var = OMP_NEXT_VARIANT (0, state);
19140 loop_label:
19141 switch (switch_var)
19142 {
19143 case 1:
19144 if (dynamic_selector_predicate_1)
19145 {
19146 alternative_1;
19147 goto end_label;
19148 }
19149 else
19150 {
19151 switch_var = OMP_NEXT_VARIANT (1, state);
19152 goto loop_label;
19153 }
19154 case 2:
19155 ...
19156 }
19157 end_label:
19158
19159 OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
19160 index of the next variant to try, after late resolution. */
19161
19162 static tree
19163 expand_late_variant_directive (vec<struct omp_variant> all_candidates,
19164 tree construct_context)
19165 {
19166 tree body_label = NULL_TREE;
19167 tree standalone_body = NULL_TREE;
19168 tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
19169 tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19170 tree selectors = make_tree_vec (all_candidates.length ());
19171 tree switch_body = NULL_TREE;
19172 tree switch_var = create_tmp_var (integer_type_node, "variant");
19173 tree state = tree_cons (NULL_TREE, construct_context, selectors);
19174
19175 for (unsigned int i = 0; i < all_candidates.length (); i++)
19176 {
19177 tree selector = all_candidates[i].selector;
19178 tree alternative = all_candidates[i].alternative;
19179 tree body = all_candidates[i].body;
19180 TREE_VEC_ELT (selectors, i) = selector;
19181
19182 /* Case label. Numbering is 1-based. */
19183 tree case_val = build_int_cst (integer_type_node, i + 1);
19184 tree case_label
19185 = build_case_label (case_val, NULL_TREE,
19186 create_artificial_label (UNKNOWN_LOCATION));
19187 append_to_statement_list (case_label, &switch_body);
19188
19189 /* The actual body of the variant. */
19190 tree variant_body = NULL_TREE;
19191 append_to_statement_list (alternative, &variant_body);
19192
19193 if (body != NULL_TREE)
19194 {
19195 if (standalone_body == NULL)
19196 {
19197 standalone_body = body;
19198 body_label = create_artificial_label (UNKNOWN_LOCATION);
19199 }
19200 append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19201 body_label),
19202 &variant_body);
19203 }
19204 else
19205 append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19206 end_label),
19207 &variant_body);
19208
19209 /* If this is a dynamic selector, wrap variant_body with a conditional.
19210 If the predicate doesn't match, the else clause sets switch_var and
19211 jumps to loop_var to try again. */
19212 tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
19213 if (dynamic_selector)
19214 {
19215 tree else_stmt = NULL_TREE;
19216 tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
19217 case_val, state);
19218 append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19219 switch_var, next),
19220 &else_stmt);
19221 append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19222 loop_label),
19223 &else_stmt);
19224 variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
19225 variant_body, else_stmt);
19226 }
19227 append_to_statement_list (variant_body, &switch_body);
19228 }
19229
19230 /* Put it all together. */
19231 tree result = NULL_TREE;
19232 tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
19233 state);
19234 append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19235 switch_var, first),
19236 &result);
19237 append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
19238 &result);
19239 append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
19240 switch_var, switch_body),
19241 &result);
19242 if (standalone_body)
19243 {
19244 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
19245 body_label),
19246 &result);
19247 append_to_statement_list (standalone_body, &result);
19248 }
19249 append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
19250 &result);
19251 cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
19252 return result;
19253 }
19254
19255
19256 /* Gimplify an OMP_METADIRECTIVE construct. EXPR is the tree version.
19257 The metadirective will be resolved at this point if possible, otherwise
19258 a GIMPLE_OMP_VARIANT_CONSTRUCT is created. */
19259
19260 static enum gimplify_status
19261 gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
19262 bool (*) (tree), fallback_t)
19263 {
19264 /* Try to resolve the metadirective. */
19265 tree construct_context = omp_get_construct_context ();
19266 vec<struct omp_variant> all_candidates
19267 = omp_metadirective_candidates (*expr_p, construct_context);
19268 vec<struct omp_variant> candidates
19269 = omp_get_dynamic_candidates (all_candidates, construct_context);
19270 if (!candidates.is_empty ())
19271 return expand_omp_metadirective (candidates, pre_p);
19272
19273 /* The metadirective cannot be resolved yet. Turn it into a loop with
19274 a nested switch statement, using OMP_NEXT_VARIANT to set the control
19275 variable for the switch. */
19276 *expr_p = expand_late_variant_directive (all_candidates, construct_context);
19277 return GS_OK;
19278 }
19279
19280 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
19281 expression produces a value to be used as an operand inside a GIMPLE
19282 statement, the value will be stored back in *EXPR_P. This value will
19283 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
19284 an SSA_NAME. The corresponding sequence of GIMPLE statements is
19285 emitted in PRE_P and POST_P.
19286
19287 Additionally, this process may overwrite parts of the input
19288 expression during gimplification. Ideally, it should be
19289 possible to do non-destructive gimplification.
19290
19291 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
19292 the expression needs to evaluate to a value to be used as
19293 an operand in a GIMPLE statement, this value will be stored in
19294 *EXPR_P on exit. This happens when the caller specifies one
19295 of fb_lvalue or fb_rvalue fallback flags.
19296
19297 PRE_P will contain the sequence of GIMPLE statements corresponding
19298 to the evaluation of EXPR and all the side-effects that must
19299 be executed before the main expression. On exit, the last
19300 statement of PRE_P is the core statement being gimplified. For
19301 instance, when gimplifying 'if (++a)' the last statement in
19302 PRE_P will be 'if (t.1)' where t.1 is the result of
19303 pre-incrementing 'a'.
19304
19305 POST_P will contain the sequence of GIMPLE statements corresponding
19306 to the evaluation of all the side-effects that must be executed
19307 after the main expression. If this is NULL, the post
19308 side-effects are stored at the end of PRE_P.
19309
19310 The reason why the output is split in two is to handle post
19311 side-effects explicitly. In some cases, an expression may have
19312 inner and outer post side-effects which need to be emitted in
19313 an order different from the one given by the recursive
19314 traversal. For instance, for the expression (*p--)++ the post
19315 side-effects of '--' must actually occur *after* the post
19316 side-effects of '++'. However, gimplification will first visit
19317 the inner expression, so if a separate POST sequence was not
19318 used, the resulting sequence would be:
19319
19320 1 t.1 = *p
19321 2 p = p - 1
19322 3 t.2 = t.1 + 1
19323 4 *p = t.2
19324
19325 However, the post-decrement operation in line #2 must not be
19326 evaluated until after the store to *p at line #4, so the
19327 correct sequence should be:
19328
19329 1 t.1 = *p
19330 2 t.2 = t.1 + 1
19331 3 *p = t.2
19332 4 p = p - 1
19333
19334 So, by specifying a separate post queue, it is possible
19335 to emit the post side-effects in the correct order.
19336 If POST_P is NULL, an internal queue will be used. Before
19337 returning to the caller, the sequence POST_P is appended to
19338 the main output sequence PRE_P.
19339
19340 GIMPLE_TEST_F points to a function that takes a tree T and
19341 returns nonzero if T is in the GIMPLE form requested by the
19342 caller. The GIMPLE predicates are in gimple.cc.
19343
19344 FALLBACK tells the function what sort of a temporary we want if
19345 gimplification cannot produce an expression that complies with
19346 GIMPLE_TEST_F.
19347
19348 fb_none means that no temporary should be generated
19349 fb_rvalue means that an rvalue is OK to generate
19350 fb_lvalue means that an lvalue is OK to generate
19351 fb_either means that either is OK, but an lvalue is preferable.
19352 fb_mayfail means that gimplification may fail (in which case
19353 GS_ERROR will be returned)
19354
19355 The return value is either GS_ERROR or GS_ALL_DONE, since this
19356 function iterates until EXPR is completely gimplified or an error
19357 occurs. */
19358
19359 enum gimplify_status
19360 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
19361 bool (*gimple_test_f) (tree), fallback_t fallback)
19362 {
19363 tree tmp;
19364 gimple_seq internal_pre = NULL;
19365 gimple_seq internal_post = NULL;
19366 tree save_expr;
19367 bool is_statement;
19368 location_t saved_location;
19369 enum gimplify_status ret;
19370 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
19371 tree label;
19372
19373 save_expr = *expr_p;
19374 if (save_expr == NULL_TREE)
19375 return GS_ALL_DONE;
19376
19377 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
19378 is_statement = gimple_test_f == is_gimple_stmt;
19379 if (is_statement)
19380 gcc_assert (pre_p);
19381
19382 /* Consistency checks. */
19383 if (gimple_test_f == is_gimple_reg)
19384 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
19385 else if (gimple_test_f == is_gimple_val
19386 || gimple_test_f == is_gimple_call_addr
19387 || gimple_test_f == is_gimple_condexpr_for_cond
19388 || gimple_test_f == is_gimple_mem_rhs
19389 || gimple_test_f == is_gimple_mem_rhs_or_call
19390 || gimple_test_f == is_gimple_reg_rhs
19391 || gimple_test_f == is_gimple_reg_rhs_or_call
19392 || gimple_test_f == is_gimple_asm_val
19393 || gimple_test_f == is_gimple_mem_ref_addr)
19394 gcc_assert (fallback & fb_rvalue);
19395 else if (gimple_test_f == is_gimple_min_lval
19396 || gimple_test_f == is_gimple_lvalue)
19397 gcc_assert (fallback & fb_lvalue);
19398 else if (gimple_test_f == is_gimple_addressable)
19399 gcc_assert (fallback & fb_either);
19400 else if (gimple_test_f == is_gimple_stmt)
19401 gcc_assert (fallback == fb_none);
19402 else
19403 {
19404 /* We should have recognized the GIMPLE_TEST_F predicate to
19405 know what kind of fallback to use in case a temporary is
19406 needed to hold the value or address of *EXPR_P. */
19407 gcc_unreachable ();
19408 }
19409
19410 /* We used to check the predicate here and return immediately if it
19411 succeeds. This is wrong; the design is for gimplification to be
19412 idempotent, and for the predicates to only test for valid forms, not
19413 whether they are fully simplified. */
19414 if (pre_p == NULL)
19415 pre_p = &internal_pre;
19416
19417 if (post_p == NULL)
19418 post_p = &internal_post;
19419
19420 /* Remember the last statements added to PRE_P and POST_P. Every
19421 new statement added by the gimplification helpers needs to be
19422 annotated with location information. To centralize the
19423 responsibility, we remember the last statement that had been
19424 added to both queues before gimplifying *EXPR_P. If
19425 gimplification produces new statements in PRE_P and POST_P, those
19426 statements will be annotated with the same location information
19427 as *EXPR_P. */
19428 pre_last_gsi = gsi_last (*pre_p);
19429 post_last_gsi = gsi_last (*post_p);
19430
19431 saved_location = input_location;
19432 if (save_expr != error_mark_node
19433 && EXPR_HAS_LOCATION (*expr_p))
19434 input_location = EXPR_LOCATION (*expr_p);
19435
19436 /* Loop over the specific gimplifiers until the toplevel node
19437 remains the same. */
19438 do
19439 {
19440 /* Strip away as many useless type conversions as possible
19441 at the toplevel. */
19442 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
19443
19444 /* Remember the expr. */
19445 save_expr = *expr_p;
19446
19447 /* Die, die, die, my darling. */
19448 if (error_operand_p (save_expr))
19449 {
19450 ret = GS_ERROR;
19451 break;
19452 }
19453
19454 /* Do any language-specific gimplification. */
19455 ret = ((enum gimplify_status)
19456 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
19457 if (ret == GS_OK)
19458 {
19459 if (*expr_p == NULL_TREE)
19460 break;
19461 if (*expr_p != save_expr)
19462 continue;
19463 }
19464 else if (ret != GS_UNHANDLED)
19465 break;
19466
19467 /* Make sure that all the cases set 'ret' appropriately. */
19468 ret = GS_UNHANDLED;
19469 switch (TREE_CODE (*expr_p))
19470 {
19471 /* First deal with the special cases. */
19472
19473 case POSTINCREMENT_EXPR:
19474 case POSTDECREMENT_EXPR:
19475 case PREINCREMENT_EXPR:
19476 case PREDECREMENT_EXPR:
19477 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
19478 fallback != fb_none,
19479 TREE_TYPE (*expr_p));
19480 break;
19481
19482 case VIEW_CONVERT_EXPR:
19483 if ((fallback & fb_rvalue)
19484 && is_gimple_reg_type (TREE_TYPE (*expr_p))
19485 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
19486 {
19487 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
19488 post_p, is_gimple_val, fb_rvalue);
19489 recalculate_side_effects (*expr_p);
19490 break;
19491 }
19492 /* Fallthru. */
19493
19494 case ARRAY_REF:
19495 case ARRAY_RANGE_REF:
19496 case REALPART_EXPR:
19497 case IMAGPART_EXPR:
19498 case COMPONENT_REF:
19499 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
19500 fallback ? fallback : fb_rvalue);
19501 break;
19502
19503 case COND_EXPR:
19504 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
19505
19506 /* C99 code may assign to an array in a structure value of a
19507 conditional expression, and this has undefined behavior
19508 only on execution, so create a temporary if an lvalue is
19509 required. */
19510 if (fallback == fb_lvalue)
19511 {
19512 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
19513 mark_addressable (*expr_p);
19514 ret = GS_OK;
19515 }
19516 break;
19517
19518 case CALL_EXPR:
19519 ret = gimplify_call_expr (expr_p, pre_p, fallback);
19520
19521 /* C99 code may assign to an array in a structure returned
19522 from a function, and this has undefined behavior only on
19523 execution, so create a temporary if an lvalue is
19524 required. */
19525 if (fallback == fb_lvalue)
19526 {
19527 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
19528 mark_addressable (*expr_p);
19529 ret = GS_OK;
19530 }
19531 break;
19532
19533 case TREE_LIST:
19534 gcc_unreachable ();
19535
19536 case OMP_ARRAY_SECTION:
19537 gcc_unreachable ();
19538
19539 case COMPOUND_EXPR:
19540 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
19541 break;
19542
19543 case COMPOUND_LITERAL_EXPR:
19544 ret = gimplify_compound_literal_expr (expr_p, pre_p,
19545 gimple_test_f, fallback);
19546 break;
19547
19548 case MODIFY_EXPR:
19549 case INIT_EXPR:
19550 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
19551 fallback != fb_none);
19552 break;
19553
19554 case TRUTH_ANDIF_EXPR:
19555 case TRUTH_ORIF_EXPR:
19556 {
19557 /* Preserve the original type of the expression and the
19558 source location of the outer expression. */
19559 tree org_type = TREE_TYPE (*expr_p);
19560 *expr_p = gimple_boolify (*expr_p);
19561 *expr_p = build3_loc (input_location, COND_EXPR,
19562 org_type, *expr_p,
19563 fold_convert_loc
19564 (input_location,
19565 org_type, boolean_true_node),
19566 fold_convert_loc
19567 (input_location,
19568 org_type, boolean_false_node));
19569 ret = GS_OK;
19570 break;
19571 }
19572
19573 case TRUTH_NOT_EXPR:
19574 {
19575 tree type = TREE_TYPE (*expr_p);
19576 /* The parsers are careful to generate TRUTH_NOT_EXPR
19577 only with operands that are always zero or one.
19578 We do not fold here but handle the only interesting case
19579 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
19580 *expr_p = gimple_boolify (*expr_p);
19581 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
19582 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
19583 TREE_TYPE (*expr_p),
19584 TREE_OPERAND (*expr_p, 0));
19585 else
19586 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
19587 TREE_TYPE (*expr_p),
19588 TREE_OPERAND (*expr_p, 0),
19589 build_int_cst (TREE_TYPE (*expr_p), 1));
19590 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
19591 *expr_p = fold_convert_loc (input_location, type, *expr_p);
19592 ret = GS_OK;
19593 break;
19594 }
19595
19596 case ADDR_EXPR:
19597 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
19598 break;
19599
19600 case ANNOTATE_EXPR:
19601 {
19602 tree cond = TREE_OPERAND (*expr_p, 0);
19603 tree kind = TREE_OPERAND (*expr_p, 1);
19604 tree data = TREE_OPERAND (*expr_p, 2);
19605 tree type = TREE_TYPE (cond);
19606 if (!INTEGRAL_TYPE_P (type))
19607 {
19608 *expr_p = cond;
19609 ret = GS_OK;
19610 break;
19611 }
19612 tree tmp = create_tmp_var (type);
19613 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
19614 gcall *call
19615 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
19616 gimple_call_set_lhs (call, tmp);
19617 gimplify_seq_add_stmt (pre_p, call);
19618 *expr_p = tmp;
19619 ret = GS_ALL_DONE;
19620 break;
19621 }
19622
19623 case VA_ARG_EXPR:
19624 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
19625 break;
19626
19627 CASE_CONVERT:
19628 if (IS_EMPTY_STMT (*expr_p))
19629 {
19630 ret = GS_ALL_DONE;
19631 break;
19632 }
19633
19634 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
19635 || fallback == fb_none)
19636 {
19637 /* Just strip a conversion to void (or in void context) and
19638 try again. */
19639 *expr_p = TREE_OPERAND (*expr_p, 0);
19640 ret = GS_OK;
19641 break;
19642 }
19643
19644 ret = gimplify_conversion (expr_p);
19645 if (ret == GS_ERROR)
19646 break;
19647 if (*expr_p != save_expr)
19648 break;
19649 /* FALLTHRU */
19650
19651 case FIX_TRUNC_EXPR:
19652 /* unary_expr: ... | '(' cast ')' val | ... */
19653 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19654 is_gimple_val, fb_rvalue);
19655 recalculate_side_effects (*expr_p);
19656 break;
19657
19658 case INDIRECT_REF:
19659 {
19660 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
19661 bool notrap = TREE_THIS_NOTRAP (*expr_p);
19662 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
19663
19664 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
19665 if (*expr_p != save_expr)
19666 {
19667 ret = GS_OK;
19668 break;
19669 }
19670
19671 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19672 is_gimple_reg, fb_rvalue);
19673 if (ret == GS_ERROR)
19674 break;
19675
19676 recalculate_side_effects (*expr_p);
19677 *expr_p = fold_build2_loc (input_location, MEM_REF,
19678 TREE_TYPE (*expr_p),
19679 TREE_OPERAND (*expr_p, 0),
19680 build_int_cst (saved_ptr_type, 0));
19681 TREE_THIS_VOLATILE (*expr_p) = volatilep;
19682 TREE_THIS_NOTRAP (*expr_p) = notrap;
19683 ret = GS_OK;
19684 break;
19685 }
19686
19687 /* We arrive here through the various re-gimplifcation paths. */
19688 case MEM_REF:
19689 /* First try re-folding the whole thing. */
19690 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
19691 TREE_OPERAND (*expr_p, 0),
19692 TREE_OPERAND (*expr_p, 1));
19693 if (tmp)
19694 {
19695 REF_REVERSE_STORAGE_ORDER (tmp)
19696 = REF_REVERSE_STORAGE_ORDER (*expr_p);
19697 *expr_p = tmp;
19698 recalculate_side_effects (*expr_p);
19699 ret = GS_OK;
19700 break;
19701 }
19702 /* Avoid re-gimplifying the address operand if it is already
19703 in suitable form. Re-gimplifying would mark the address
19704 operand addressable. Always gimplify when not in SSA form
19705 as we still may have to gimplify decls with value-exprs. */
19706 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
19707 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
19708 {
19709 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19710 is_gimple_mem_ref_addr, fb_rvalue);
19711 if (ret == GS_ERROR)
19712 break;
19713 }
19714 recalculate_side_effects (*expr_p);
19715 ret = GS_ALL_DONE;
19716 break;
19717
19718 /* Constants need not be gimplified. */
19719 case INTEGER_CST:
19720 case REAL_CST:
19721 case FIXED_CST:
19722 case STRING_CST:
19723 case COMPLEX_CST:
19724 case VECTOR_CST:
19725 /* Drop the overflow flag on constants, we do not want
19726 that in the GIMPLE IL. */
19727 if (TREE_OVERFLOW_P (*expr_p))
19728 *expr_p = drop_tree_overflow (*expr_p);
19729 ret = GS_ALL_DONE;
19730 break;
19731
19732 case CONST_DECL:
19733 /* If we require an lvalue, such as for ADDR_EXPR, retain the
19734 CONST_DECL node. Otherwise the decl is replaceable by its
19735 value. */
19736 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
19737 if (fallback & fb_lvalue)
19738 ret = GS_ALL_DONE;
19739 else
19740 {
19741 *expr_p = DECL_INITIAL (*expr_p);
19742 ret = GS_OK;
19743 }
19744 break;
19745
19746 case DECL_EXPR:
19747 ret = gimplify_decl_expr (expr_p, pre_p);
19748 break;
19749
19750 case BIND_EXPR:
19751 ret = gimplify_bind_expr (expr_p, pre_p);
19752 break;
19753
19754 case LOOP_EXPR:
19755 ret = gimplify_loop_expr (expr_p, pre_p);
19756 break;
19757
19758 case SWITCH_EXPR:
19759 ret = gimplify_switch_expr (expr_p, pre_p);
19760 break;
19761
19762 case EXIT_EXPR:
19763 ret = gimplify_exit_expr (expr_p);
19764 break;
19765
19766 case GOTO_EXPR:
19767 /* If the target is not LABEL, then it is a computed jump
19768 and the target needs to be gimplified. */
19769 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
19770 {
19771 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
19772 NULL, is_gimple_val, fb_rvalue);
19773 if (ret == GS_ERROR)
19774 break;
19775 }
19776 gimplify_seq_add_stmt (pre_p,
19777 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
19778 ret = GS_ALL_DONE;
19779 break;
19780
19781 case PREDICT_EXPR:
19782 gimplify_seq_add_stmt (pre_p,
19783 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
19784 PREDICT_EXPR_OUTCOME (*expr_p)));
19785 ret = GS_ALL_DONE;
19786 break;
19787
19788 case LABEL_EXPR:
19789 ret = gimplify_label_expr (expr_p, pre_p);
19790 label = LABEL_EXPR_LABEL (*expr_p);
19791 gcc_assert (decl_function_context (label) == current_function_decl);
19792
19793 /* If the label is used in a goto statement, or address of the label
19794 is taken, we need to unpoison all variables that were seen so far.
19795 Doing so would prevent us from reporting a false positives. */
19796 if (asan_poisoned_variables
19797 && asan_used_labels != NULL
19798 && asan_used_labels->contains (label)
19799 && !gimplify_omp_ctxp)
19800 asan_poison_variables (asan_poisoned_variables, false, pre_p);
19801 break;
19802
19803 case CASE_LABEL_EXPR:
19804 ret = gimplify_case_label_expr (expr_p, pre_p);
19805
19806 if (gimplify_ctxp->live_switch_vars)
19807 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
19808 pre_p);
19809 break;
19810
19811 case RETURN_EXPR:
19812 ret = gimplify_return_expr (*expr_p, pre_p);
19813 break;
19814
19815 case CONSTRUCTOR:
19816 /* Don't reduce this in place; let gimplify_init_constructor work its
19817 magic. Buf if we're just elaborating this for side effects, just
19818 gimplify any element that has side-effects. */
19819 if (fallback == fb_none)
19820 {
19821 unsigned HOST_WIDE_INT ix;
19822 tree val;
19823 tree temp = NULL_TREE;
19824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
19825 if (TREE_SIDE_EFFECTS (val))
19826 append_to_statement_list (val, &temp);
19827
19828 *expr_p = temp;
19829 ret = temp ? GS_OK : GS_ALL_DONE;
19830 }
19831 /* C99 code may assign to an array in a constructed
19832 structure or union, and this has undefined behavior only
19833 on execution, so create a temporary if an lvalue is
19834 required. */
19835 else if (fallback == fb_lvalue)
19836 {
19837 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
19838 mark_addressable (*expr_p);
19839 ret = GS_OK;
19840 }
19841 else
19842 ret = GS_ALL_DONE;
19843 break;
19844
19845 /* The following are special cases that are not handled by the
19846 original GIMPLE grammar. */
19847
19848 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
19849 eliminated. */
19850 case SAVE_EXPR:
19851 ret = gimplify_save_expr (expr_p, pre_p, post_p);
19852 break;
19853
19854 case BIT_FIELD_REF:
19855 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
19856 post_p, is_gimple_lvalue, fb_either);
19857 recalculate_side_effects (*expr_p);
19858 break;
19859
19860 case TARGET_MEM_REF:
19861 {
19862 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
19863
19864 if (TMR_BASE (*expr_p))
19865 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
19866 post_p, is_gimple_mem_ref_addr, fb_either);
19867 if (TMR_INDEX (*expr_p))
19868 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
19869 post_p, is_gimple_val, fb_rvalue);
19870 if (TMR_INDEX2 (*expr_p))
19871 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
19872 post_p, is_gimple_val, fb_rvalue);
19873 /* TMR_STEP and TMR_OFFSET are always integer constants. */
19874 ret = MIN (r0, r1);
19875 }
19876 break;
19877
19878 case NON_LVALUE_EXPR:
19879 /* This should have been stripped above. */
19880 gcc_unreachable ();
19881
19882 case ASM_EXPR:
19883 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
19884 break;
19885
19886 case TRY_FINALLY_EXPR:
19887 case TRY_CATCH_EXPR:
19888 {
19889 gimple_seq eval, cleanup;
19890 gtry *try_;
19891
19892 /* Calls to destructors are generated automatically in FINALLY/CATCH
19893 block. They should have location as UNKNOWN_LOCATION. However,
19894 gimplify_call_expr will reset these call stmts to input_location
19895 if it finds stmt's location is unknown. To prevent resetting for
19896 destructors, we set the input_location to unknown.
19897 Note that this only affects the destructor calls in FINALLY/CATCH
19898 block, and will automatically reset to its original value by the
19899 end of gimplify_expr. */
19900 input_location = UNKNOWN_LOCATION;
19901 eval = cleanup = NULL;
19902 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
19903 bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
19904 if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
19905 && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
19906 {
19907 gimple_seq n = NULL, e = NULL;
19908 gimplify_ctxp->in_handler_expr = true;
19909 gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
19910 0), &n);
19911 gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
19912 1), &e);
19913 if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
19914 {
19915 geh_else *stmt = gimple_build_eh_else (n, e);
19916 gimple_seq_add_stmt (&cleanup, stmt);
19917 }
19918 }
19919 else
19920 {
19921 gimplify_ctxp->in_handler_expr = true;
19922 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
19923 }
19924 gimplify_ctxp->in_handler_expr = save_in_handler_expr;
19925 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
19926 if (gimple_seq_empty_p (cleanup))
19927 {
19928 gimple_seq_add_seq (pre_p, eval);
19929 ret = GS_ALL_DONE;
19930 break;
19931 }
19932 try_ = gimple_build_try (eval, cleanup,
19933 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
19934 ? GIMPLE_TRY_FINALLY
19935 : GIMPLE_TRY_CATCH);
19936 if (EXPR_HAS_LOCATION (save_expr))
19937 gimple_set_location (try_, EXPR_LOCATION (save_expr));
19938 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
19939 gimple_set_location (try_, saved_location);
19940 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
19941 gimple_try_set_catch_is_cleanup (try_,
19942 TRY_CATCH_IS_CLEANUP (*expr_p));
19943 gimplify_seq_add_stmt (pre_p, try_);
19944 ret = GS_ALL_DONE;
19945 break;
19946 }
19947
19948 case CLEANUP_POINT_EXPR:
19949 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
19950 break;
19951
19952 case TARGET_EXPR:
19953 ret = gimplify_target_expr (expr_p, pre_p, post_p);
19954 break;
19955
19956 case CATCH_EXPR:
19957 {
19958 gimple *c;
19959 gimple_seq handler = NULL;
19960 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
19961 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
19962 gimplify_seq_add_stmt (pre_p, c);
19963 ret = GS_ALL_DONE;
19964 break;
19965 }
19966
19967 case EH_FILTER_EXPR:
19968 {
19969 gimple *ehf;
19970 gimple_seq failure = NULL;
19971
19972 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
19973 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
19974 copy_warning (ehf, *expr_p);
19975 gimplify_seq_add_stmt (pre_p, ehf);
19976 ret = GS_ALL_DONE;
19977 break;
19978 }
19979
19980 case OBJ_TYPE_REF:
19981 {
19982 enum gimplify_status r0, r1;
19983 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
19984 post_p, is_gimple_val, fb_rvalue);
19985 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
19986 post_p, is_gimple_val, fb_rvalue);
19987 TREE_SIDE_EFFECTS (*expr_p) = 0;
19988 ret = MIN (r0, r1);
19989 }
19990 break;
19991
19992 case LABEL_DECL:
19993 /* We get here when taking the address of a label. We mark
19994 the label as "forced"; meaning it can never be removed and
19995 it is a potential target for any computed goto. */
19996 FORCED_LABEL (*expr_p) = 1;
19997 ret = GS_ALL_DONE;
19998 break;
19999
20000 case STATEMENT_LIST:
20001 ret = gimplify_statement_list (expr_p, pre_p);
20002 break;
20003
20004 case WITH_SIZE_EXPR:
20005 {
20006 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20007 post_p == &internal_post ? NULL : post_p,
20008 gimple_test_f, fallback);
20009 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20010 is_gimple_val, fb_rvalue);
20011 ret = GS_ALL_DONE;
20012 }
20013 break;
20014
20015 case VAR_DECL:
20016 case PARM_DECL:
20017 ret = gimplify_var_or_parm_decl (expr_p);
20018 break;
20019
20020 case RESULT_DECL:
20021 /* When within an OMP context, notice uses of variables. */
20022 if (gimplify_omp_ctxp)
20023 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
20024 /* Handlers can refer to the function result; if that has been
20025 moved, we need to track it. */
20026 if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
20027 *expr_p = gimplify_ctxp->return_temp;
20028 ret = GS_ALL_DONE;
20029 break;
20030
20031 case DEBUG_EXPR_DECL:
20032 gcc_unreachable ();
20033
20034 case DEBUG_BEGIN_STMT:
20035 gimplify_seq_add_stmt (pre_p,
20036 gimple_build_debug_begin_stmt
20037 (TREE_BLOCK (*expr_p),
20038 EXPR_LOCATION (*expr_p)));
20039 ret = GS_ALL_DONE;
20040 *expr_p = NULL;
20041 break;
20042
20043 case SSA_NAME:
20044 /* Allow callbacks into the gimplifier during optimization. */
20045 ret = GS_ALL_DONE;
20046 break;
20047
20048 case OMP_PARALLEL:
20049 gimplify_omp_parallel (expr_p, pre_p);
20050 ret = GS_ALL_DONE;
20051 break;
20052
20053 case OMP_TASK:
20054 gimplify_omp_task (expr_p, pre_p);
20055 ret = GS_ALL_DONE;
20056 break;
20057
20058 case OMP_SIMD:
20059 {
20060 /* Temporarily disable into_ssa, as scan_omp_simd
20061 which calls copy_gimple_seq_and_replace_locals can't deal
20062 with SSA_NAMEs defined outside of the body properly. */
20063 bool saved_into_ssa = gimplify_ctxp->into_ssa;
20064 gimplify_ctxp->into_ssa = false;
20065 ret = gimplify_omp_for (expr_p, pre_p);
20066 gimplify_ctxp->into_ssa = saved_into_ssa;
20067 break;
20068 }
20069
20070 case OMP_FOR:
20071 case OMP_DISTRIBUTE:
20072 case OMP_TASKLOOP:
20073 case OMP_TILE:
20074 case OMP_UNROLL:
20075 case OACC_LOOP:
20076 ret = gimplify_omp_for (expr_p, pre_p);
20077 break;
20078
20079 case OMP_LOOP:
20080 ret = gimplify_omp_loop (expr_p, pre_p);
20081 break;
20082
20083 case OACC_CACHE:
20084 gimplify_oacc_cache (expr_p, pre_p);
20085 ret = GS_ALL_DONE;
20086 break;
20087
20088 case OACC_DECLARE:
20089 gimplify_oacc_declare (expr_p, pre_p);
20090 ret = GS_ALL_DONE;
20091 break;
20092
20093 case OACC_HOST_DATA:
20094 case OACC_DATA:
20095 case OACC_KERNELS:
20096 case OACC_PARALLEL:
20097 case OACC_SERIAL:
20098 case OMP_SCOPE:
20099 case OMP_SECTIONS:
20100 case OMP_SINGLE:
20101 case OMP_TARGET:
20102 case OMP_TARGET_DATA:
20103 case OMP_TEAMS:
20104 gimplify_omp_workshare (expr_p, pre_p);
20105 ret = GS_ALL_DONE;
20106 break;
20107
20108 case OACC_ENTER_DATA:
20109 case OACC_EXIT_DATA:
20110 case OACC_UPDATE:
20111 case OMP_TARGET_UPDATE:
20112 case OMP_TARGET_ENTER_DATA:
20113 case OMP_TARGET_EXIT_DATA:
20114 gimplify_omp_target_update (expr_p, pre_p);
20115 ret = GS_ALL_DONE;
20116 break;
20117
20118 case OMP_SECTION:
20119 case OMP_STRUCTURED_BLOCK:
20120 case OMP_MASTER:
20121 case OMP_MASKED:
20122 case OMP_ORDERED:
20123 case OMP_CRITICAL:
20124 case OMP_SCAN:
20125 {
20126 gimple_seq body = NULL;
20127 gimple *g;
20128 bool saved_in_omp_construct = in_omp_construct;
20129
20130 in_omp_construct = true;
20131 gimplify_and_add (OMP_BODY (*expr_p), &body);
20132 in_omp_construct = saved_in_omp_construct;
20133 switch (TREE_CODE (*expr_p))
20134 {
20135 case OMP_SECTION:
20136 g = gimple_build_omp_section (body);
20137 break;
20138 case OMP_STRUCTURED_BLOCK:
20139 g = gimple_build_omp_structured_block (body);
20140 break;
20141 case OMP_MASTER:
20142 g = gimple_build_omp_master (body);
20143 break;
20144 case OMP_ORDERED:
20145 g = gimplify_omp_ordered (*expr_p, body);
20146 if (OMP_BODY (*expr_p) == NULL_TREE
20147 && gimple_code (g) == GIMPLE_OMP_ORDERED)
20148 gimple_omp_ordered_standalone (g);
20149 break;
20150 case OMP_MASKED:
20151 gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
20152 pre_p, ORT_WORKSHARE, OMP_MASKED);
20153 gimplify_adjust_omp_clauses (pre_p, body,
20154 &OMP_MASKED_CLAUSES (*expr_p),
20155 OMP_MASKED);
20156 g = gimple_build_omp_masked (body,
20157 OMP_MASKED_CLAUSES (*expr_p));
20158 break;
20159 case OMP_CRITICAL:
20160 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
20161 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
20162 gimplify_adjust_omp_clauses (pre_p, body,
20163 &OMP_CRITICAL_CLAUSES (*expr_p),
20164 OMP_CRITICAL);
20165 g = gimple_build_omp_critical (body,
20166 OMP_CRITICAL_NAME (*expr_p),
20167 OMP_CRITICAL_CLAUSES (*expr_p));
20168 break;
20169 case OMP_SCAN:
20170 gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
20171 pre_p, ORT_WORKSHARE, OMP_SCAN);
20172 gimplify_adjust_omp_clauses (pre_p, body,
20173 &OMP_SCAN_CLAUSES (*expr_p),
20174 OMP_SCAN);
20175 g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
20176 break;
20177 default:
20178 gcc_unreachable ();
20179 }
20180 gimplify_seq_add_stmt (pre_p, g);
20181 ret = GS_ALL_DONE;
20182 break;
20183 }
20184
20185 case OMP_TASKGROUP:
20186 {
20187 gimple_seq body = NULL;
20188
20189 tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
20190 bool saved_in_omp_construct = in_omp_construct;
20191 gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
20192 OMP_TASKGROUP);
20193 gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
20194
20195 in_omp_construct = true;
20196 gimplify_and_add (OMP_BODY (*expr_p), &body);
20197 in_omp_construct = saved_in_omp_construct;
20198 gimple_seq cleanup = NULL;
20199 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
20200 gimple *g = gimple_build_call (fn, 0);
20201 gimple_seq_add_stmt (&cleanup, g);
20202 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
20203 body = NULL;
20204 gimple_seq_add_stmt (&body, g);
20205 g = gimple_build_omp_taskgroup (body, *pclauses);
20206 gimplify_seq_add_stmt (pre_p, g);
20207 ret = GS_ALL_DONE;
20208 break;
20209 }
20210
20211 case OMP_INTEROP:
20212 ret = gimplify_omp_interop (expr_p, pre_p);
20213 break;
20214 case OMP_ATOMIC:
20215 case OMP_ATOMIC_READ:
20216 case OMP_ATOMIC_CAPTURE_OLD:
20217 case OMP_ATOMIC_CAPTURE_NEW:
20218 ret = gimplify_omp_atomic (expr_p, pre_p);
20219 break;
20220
20221 case OMP_DISPATCH:
20222 ret = gimplify_omp_dispatch (expr_p, pre_p);
20223 break;
20224
20225 case OMP_METADIRECTIVE:
20226 ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
20227 gimple_test_f, fallback);
20228 break;
20229
20230 case OMP_NEXT_VARIANT:
20231 case OMP_TARGET_DEVICE_MATCHES:
20232 /* These are placeholders for constants. There's nothing to do with
20233 them here but we must mark the containing function as needing
20234 to run the ompdevlow pass to resolve them. Note that
20235 OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
20236 the front ends. */
20237 cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
20238 ret = GS_ALL_DONE;
20239 break;
20240
20241 case TRANSACTION_EXPR:
20242 ret = gimplify_transaction (expr_p, pre_p);
20243 break;
20244
20245 case TRUTH_AND_EXPR:
20246 case TRUTH_OR_EXPR:
20247 case TRUTH_XOR_EXPR:
20248 {
20249 tree orig_type = TREE_TYPE (*expr_p);
20250 tree new_type, xop0, xop1;
20251 *expr_p = gimple_boolify (*expr_p);
20252 new_type = TREE_TYPE (*expr_p);
20253 if (!useless_type_conversion_p (orig_type, new_type))
20254 {
20255 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
20256 ret = GS_OK;
20257 break;
20258 }
20259
20260 /* Boolified binary truth expressions are semantically equivalent
20261 to bitwise binary expressions. Canonicalize them to the
20262 bitwise variant. */
20263 switch (TREE_CODE (*expr_p))
20264 {
20265 case TRUTH_AND_EXPR:
20266 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
20267 break;
20268 case TRUTH_OR_EXPR:
20269 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
20270 break;
20271 case TRUTH_XOR_EXPR:
20272 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
20273 break;
20274 default:
20275 break;
20276 }
20277 /* Now make sure that operands have compatible type to
20278 expression's new_type. */
20279 xop0 = TREE_OPERAND (*expr_p, 0);
20280 xop1 = TREE_OPERAND (*expr_p, 1);
20281 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
20282 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
20283 new_type,
20284 xop0);
20285 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
20286 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
20287 new_type,
20288 xop1);
20289 /* Continue classified as tcc_binary. */
20290 goto expr_2;
20291 }
20292
20293 case VEC_COND_EXPR:
20294 goto expr_3;
20295
20296 case VEC_PERM_EXPR:
20297 /* Classified as tcc_expression. */
20298 goto expr_3;
20299
20300 case BIT_INSERT_EXPR:
20301 /* Argument 3 is a constant. */
20302 goto expr_2;
20303
20304 case POINTER_PLUS_EXPR:
20305 {
20306 enum gimplify_status r0, r1;
20307 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20308 post_p, is_gimple_val, fb_rvalue);
20309 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20310 post_p, is_gimple_val, fb_rvalue);
20311 recalculate_side_effects (*expr_p);
20312 ret = MIN (r0, r1);
20313 break;
20314 }
20315
20316 default:
20317 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
20318 {
20319 case tcc_comparison:
20320 /* Handle comparison of objects of non scalar mode aggregates
20321 with a call to memcmp. It would be nice to only have to do
20322 this for variable-sized objects, but then we'd have to allow
20323 the same nest of reference nodes we allow for MODIFY_EXPR and
20324 that's too complex.
20325
20326 Compare scalar mode aggregates as scalar mode values. Using
20327 memcmp for them would be very inefficient at best, and is
20328 plain wrong if bitfields are involved. */
20329 if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
20330 ret = GS_ERROR;
20331 else
20332 {
20333 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
20334
20335 /* Vector comparisons need no boolification. */
20336 if (TREE_CODE (type) == VECTOR_TYPE)
20337 goto expr_2;
20338 else if (!AGGREGATE_TYPE_P (type))
20339 {
20340 tree org_type = TREE_TYPE (*expr_p);
20341 *expr_p = gimple_boolify (*expr_p);
20342 if (!useless_type_conversion_p (org_type,
20343 TREE_TYPE (*expr_p)))
20344 {
20345 *expr_p = fold_convert_loc (input_location,
20346 org_type, *expr_p);
20347 ret = GS_OK;
20348 }
20349 else
20350 goto expr_2;
20351 }
20352 else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
20353 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
20354 else
20355 ret = gimplify_variable_sized_compare (expr_p);
20356 }
20357 break;
20358
20359 /* If *EXPR_P does not need to be special-cased, handle it
20360 according to its class. */
20361 case tcc_unary:
20362 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20363 post_p, is_gimple_val, fb_rvalue);
20364 break;
20365
20366 case tcc_binary:
20367 expr_2:
20368 {
20369 enum gimplify_status r0, r1;
20370
20371 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20372 post_p, is_gimple_val, fb_rvalue);
20373 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20374 post_p, is_gimple_val, fb_rvalue);
20375
20376 ret = MIN (r0, r1);
20377 break;
20378 }
20379
20380 expr_3:
20381 {
20382 enum gimplify_status r0, r1, r2;
20383
20384 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20385 post_p, is_gimple_val, fb_rvalue);
20386 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20387 post_p, is_gimple_val, fb_rvalue);
20388 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
20389 post_p, is_gimple_val, fb_rvalue);
20390
20391 ret = MIN (MIN (r0, r1), r2);
20392 break;
20393 }
20394
20395 case tcc_declaration:
20396 case tcc_constant:
20397 ret = GS_ALL_DONE;
20398 goto dont_recalculate;
20399
20400 default:
20401 gcc_unreachable ();
20402 }
20403
20404 recalculate_side_effects (*expr_p);
20405
20406 dont_recalculate:
20407 break;
20408 }
20409
20410 gcc_assert (*expr_p || ret != GS_OK);
20411 }
20412 while (ret == GS_OK);
20413
20414 /* If we encountered an error_mark somewhere nested inside, either
20415 stub out the statement or propagate the error back out. */
20416 if (ret == GS_ERROR)
20417 {
20418 if (is_statement)
20419 *expr_p = NULL;
20420 goto out;
20421 }
20422
20423 /* This was only valid as a return value from the langhook, which
20424 we handled. Make sure it doesn't escape from any other context. */
20425 gcc_assert (ret != GS_UNHANDLED);
20426
20427 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
20428 {
20429 /* We aren't looking for a value, and we don't have a valid
20430 statement. If it doesn't have side-effects, throw it away.
20431 We can also get here with code such as "*&&L;", where L is
20432 a LABEL_DECL that is marked as FORCED_LABEL. */
20433 if (TREE_CODE (*expr_p) == LABEL_DECL
20434 || !TREE_SIDE_EFFECTS (*expr_p))
20435 *expr_p = NULL;
20436 else if (!TREE_THIS_VOLATILE (*expr_p))
20437 {
20438 /* This is probably a _REF that contains something nested that
20439 has side effects. Recurse through the operands to find it. */
20440 enum tree_code code = TREE_CODE (*expr_p);
20441
20442 switch (code)
20443 {
20444 case COMPONENT_REF:
20445 case REALPART_EXPR:
20446 case IMAGPART_EXPR:
20447 case VIEW_CONVERT_EXPR:
20448 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20449 gimple_test_f, fallback);
20450 break;
20451
20452 case ARRAY_REF:
20453 case ARRAY_RANGE_REF:
20454 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20455 gimple_test_f, fallback);
20456 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20457 gimple_test_f, fallback);
20458 break;
20459
20460 default:
20461 /* Anything else with side-effects must be converted to
20462 a valid statement before we get here. */
20463 gcc_unreachable ();
20464 }
20465
20466 *expr_p = NULL;
20467 }
20468 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
20469 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
20470 && !is_empty_type (TREE_TYPE (*expr_p)))
20471 {
20472 /* Historically, the compiler has treated a bare reference
20473 to a non-BLKmode volatile lvalue as forcing a load. */
20474 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
20475
20476 /* Normally, we do not want to create a temporary for a
20477 TREE_ADDRESSABLE type because such a type should not be
20478 copied by bitwise-assignment. However, we make an
20479 exception here, as all we are doing here is ensuring that
20480 we read the bytes that make up the type. We use
20481 create_tmp_var_raw because create_tmp_var will abort when
20482 given a TREE_ADDRESSABLE type. */
20483 tree tmp = create_tmp_var_raw (type, "vol");
20484 gimple_add_tmp_var (tmp);
20485 gimplify_assign (tmp, *expr_p, pre_p);
20486 *expr_p = NULL;
20487 }
20488 else
20489 /* We can't do anything useful with a volatile reference to
20490 an incomplete type, so just throw it away. Likewise for
20491 a BLKmode type, since any implicit inner load should
20492 already have been turned into an explicit one by the
20493 gimplification process. */
20494 *expr_p = NULL;
20495 }
20496
20497 /* If we are gimplifying at the statement level, we're done. Tack
20498 everything together and return. */
20499 if (fallback == fb_none || is_statement)
20500 {
20501 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
20502 it out for GC to reclaim it. */
20503 *expr_p = NULL_TREE;
20504
20505 if (!gimple_seq_empty_p (internal_pre)
20506 || !gimple_seq_empty_p (internal_post))
20507 {
20508 gimplify_seq_add_seq (&internal_pre, internal_post);
20509 gimplify_seq_add_seq (pre_p, internal_pre);
20510 }
20511
20512 /* The result of gimplifying *EXPR_P is going to be the last few
20513 statements in *PRE_P and *POST_P. Add location information
20514 to all the statements that were added by the gimplification
20515 helpers. */
20516 if (!gimple_seq_empty_p (*pre_p))
20517 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
20518
20519 if (!gimple_seq_empty_p (*post_p))
20520 annotate_all_with_location_after (*post_p, post_last_gsi,
20521 input_location);
20522
20523 goto out;
20524 }
20525
20526 #ifdef ENABLE_GIMPLE_CHECKING
20527 if (*expr_p)
20528 {
20529 enum tree_code code = TREE_CODE (*expr_p);
20530 /* These expressions should already be in gimple IR form. */
20531 gcc_assert (code != MODIFY_EXPR
20532 && code != ASM_EXPR
20533 && code != BIND_EXPR
20534 && code != CATCH_EXPR
20535 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
20536 && code != EH_FILTER_EXPR
20537 && code != GOTO_EXPR
20538 && code != LABEL_EXPR
20539 && code != LOOP_EXPR
20540 && code != SWITCH_EXPR
20541 && code != TRY_FINALLY_EXPR
20542 && code != EH_ELSE_EXPR
20543 && code != OACC_PARALLEL
20544 && code != OACC_KERNELS
20545 && code != OACC_SERIAL
20546 && code != OACC_DATA
20547 && code != OACC_HOST_DATA
20548 && code != OACC_DECLARE
20549 && code != OACC_UPDATE
20550 && code != OACC_ENTER_DATA
20551 && code != OACC_EXIT_DATA
20552 && code != OACC_CACHE
20553 && code != OMP_CRITICAL
20554 && code != OMP_FOR
20555 && code != OACC_LOOP
20556 && code != OMP_MASTER
20557 && code != OMP_MASKED
20558 && code != OMP_TASKGROUP
20559 && code != OMP_ORDERED
20560 && code != OMP_PARALLEL
20561 && code != OMP_SCAN
20562 && code != OMP_SECTIONS
20563 && code != OMP_SECTION
20564 && code != OMP_STRUCTURED_BLOCK
20565 && code != OMP_SINGLE
20566 && code != OMP_SCOPE
20567 && code != OMP_DISPATCH);
20568 }
20569 #endif
20570
20571 /* Otherwise we're gimplifying a subexpression, so the resulting
20572 value is interesting. If it's a valid operand that matches
20573 GIMPLE_TEST_F, we're done. Unless we are handling some
20574 post-effects internally; if that's the case, we need to copy into
20575 a temporary before adding the post-effects to POST_P. */
20576 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
20577 goto out;
20578
20579 /* Otherwise, we need to create a new temporary for the gimplified
20580 expression. */
20581
20582 /* We can't return an lvalue if we have an internal postqueue. The
20583 object the lvalue refers to would (probably) be modified by the
20584 postqueue; we need to copy the value out first, which means an
20585 rvalue. */
20586 if ((fallback & fb_lvalue)
20587 && gimple_seq_empty_p (internal_post)
20588 && is_gimple_addressable (*expr_p))
20589 {
20590 /* An lvalue will do. Take the address of the expression, store it
20591 in a temporary, and replace the expression with an INDIRECT_REF of
20592 that temporary. */
20593 tree ref_alias_type = reference_alias_ptr_type (*expr_p);
20594 unsigned int ref_align = get_object_alignment (*expr_p);
20595 tree ref_type = TREE_TYPE (*expr_p);
20596 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
20597 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
20598 if (TYPE_ALIGN (ref_type) != ref_align)
20599 ref_type = build_aligned_type (ref_type, ref_align);
20600 *expr_p = build2 (MEM_REF, ref_type,
20601 tmp, build_zero_cst (ref_alias_type));
20602 }
20603 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
20604 {
20605 /* An rvalue will do. Assign the gimplified expression into a
20606 new temporary TMP and replace the original expression with
20607 TMP. First, make sure that the expression has a type so that
20608 it can be assigned into a temporary. */
20609 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
20610 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
20611 }
20612 else
20613 {
20614 #ifdef ENABLE_GIMPLE_CHECKING
20615 if (!(fallback & fb_mayfail))
20616 {
20617 fprintf (stderr, "gimplification failed:\n");
20618 print_generic_expr (stderr, *expr_p);
20619 debug_tree (*expr_p);
20620 internal_error ("gimplification failed");
20621 }
20622 #endif
20623 gcc_assert (fallback & fb_mayfail);
20624
20625 /* If this is an asm statement, and the user asked for the
20626 impossible, don't die. Fail and let gimplify_asm_expr
20627 issue an error. */
20628 ret = GS_ERROR;
20629 goto out;
20630 }
20631
20632 /* Make sure the temporary matches our predicate. */
20633 gcc_assert ((*gimple_test_f) (*expr_p));
20634
20635 if (!gimple_seq_empty_p (internal_post))
20636 {
20637 annotate_all_with_location (internal_post, input_location);
20638 gimplify_seq_add_seq (pre_p, internal_post);
20639 }
20640
20641 out:
20642 input_location = saved_location;
20643 return ret;
20644 }
20645
20646 /* Like gimplify_expr but make sure the gimplified result is not itself
20647 a SSA name (but a decl if it were). Temporaries required by
20648 evaluating *EXPR_P may be still SSA names. */
20649
20650 static enum gimplify_status
20651 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
20652 bool (*gimple_test_f) (tree), fallback_t fallback,
20653 bool allow_ssa)
20654 {
20655 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
20656 gimple_test_f, fallback);
20657 if (! allow_ssa
20658 && TREE_CODE (*expr_p) == SSA_NAME)
20659 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
20660 return ret;
20661 }
20662
20663 /* Look through TYPE for variable-sized objects and gimplify each such
20664 size that we find. Add to LIST_P any statements generated. */
20665
20666 void
20667 gimplify_type_sizes (tree type, gimple_seq *list_p)
20668 {
20669 if (type == NULL || type == error_mark_node)
20670 return;
20671
20672 const bool ignored_p
20673 = TYPE_NAME (type)
20674 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20675 && DECL_IGNORED_P (TYPE_NAME (type));
20676 tree t;
20677
20678 /* We first do the main variant, then copy into any other variants. */
20679 type = TYPE_MAIN_VARIANT (type);
20680
20681 /* Avoid infinite recursion. */
20682 if (TYPE_SIZES_GIMPLIFIED (type))
20683 return;
20684
20685 TYPE_SIZES_GIMPLIFIED (type) = 1;
20686
20687 switch (TREE_CODE (type))
20688 {
20689 case INTEGER_TYPE:
20690 case ENUMERAL_TYPE:
20691 case BOOLEAN_TYPE:
20692 case REAL_TYPE:
20693 case FIXED_POINT_TYPE:
20694 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
20695 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
20696
20697 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
20698 {
20699 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
20700 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
20701 }
20702 break;
20703
20704 case ARRAY_TYPE:
20705 /* These types may not have declarations, so handle them here. */
20706 gimplify_type_sizes (TREE_TYPE (type), list_p);
20707 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
20708 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
20709 with assigned stack slots, for -O1+ -g they should be tracked
20710 by VTA. */
20711 if (!ignored_p
20712 && TYPE_DOMAIN (type)
20713 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
20714 {
20715 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
20716 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
20717 DECL_IGNORED_P (t) = 0;
20718 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20719 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
20720 DECL_IGNORED_P (t) = 0;
20721 }
20722 break;
20723
20724 case RECORD_TYPE:
20725 case UNION_TYPE:
20726 case QUAL_UNION_TYPE:
20727 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
20728 if (TREE_CODE (field) == FIELD_DECL)
20729 {
20730 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
20731 /* Likewise, ensure variable offsets aren't removed. */
20732 if (!ignored_p
20733 && (t = DECL_FIELD_OFFSET (field))
20734 && VAR_P (t)
20735 && DECL_ARTIFICIAL (t))
20736 DECL_IGNORED_P (t) = 0;
20737 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
20738 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
20739 gimplify_type_sizes (TREE_TYPE (field), list_p);
20740 }
20741 break;
20742
20743 case POINTER_TYPE:
20744 case REFERENCE_TYPE:
20745 /* We used to recurse on the pointed-to type here, which turned out to
20746 be incorrect because its definition might refer to variables not
20747 yet initialized at this point if a forward declaration is involved.
20748
20749 It was actually useful for anonymous pointed-to types to ensure
20750 that the sizes evaluation dominates every possible later use of the
20751 values. Restricting to such types here would be safe since there
20752 is no possible forward declaration around, but would introduce an
20753 undesirable middle-end semantic to anonymity. We then defer to
20754 front-ends the responsibility of ensuring that the sizes are
20755 evaluated both early and late enough, e.g. by attaching artificial
20756 type declarations to the tree. */
20757 break;
20758
20759 default:
20760 break;
20761 }
20762
20763 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
20764 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
20765
20766 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
20767 {
20768 TYPE_SIZE (t) = TYPE_SIZE (type);
20769 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
20770 TYPE_SIZES_GIMPLIFIED (t) = 1;
20771 }
20772 }
20773
20774 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
20775 a size or position, has had all of its SAVE_EXPRs evaluated.
20776 We add any required statements to *STMT_P. */
20777
20778 void
20779 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
20780 {
20781 tree expr = *expr_p;
20782
20783 /* We don't do anything if the value isn't there, is constant, or contains
20784 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
20785 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
20786 will want to replace it with a new variable, but that will cause problems
20787 if this type is from outside the function. It's OK to have that here. */
20788 if (expr == NULL_TREE
20789 || is_gimple_constant (expr)
20790 || VAR_P (expr)
20791 || CONTAINS_PLACEHOLDER_P (expr))
20792 return;
20793
20794 *expr_p = unshare_expr (expr);
20795
20796 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
20797 if the def vanishes. */
20798 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
20799
20800 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
20801 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
20802 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
20803 if (is_gimple_constant (*expr_p))
20804 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
20805 }
20806
20807 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
20808 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
20809 is true, also gimplify the parameters. */
20810
20811 gbind *
20812 gimplify_body (tree fndecl, bool do_parms)
20813 {
20814 location_t saved_location = input_location;
20815 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
20816 gimple *outer_stmt;
20817 gbind *outer_bind;
20818
20819 timevar_push (TV_TREE_GIMPLIFY);
20820
20821 init_tree_ssa (cfun);
20822
20823 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
20824 gimplification. */
20825 default_rtl_profile ();
20826
20827 gcc_assert (gimplify_ctxp == NULL);
20828 push_gimplify_context (true);
20829
20830 if (flag_openacc || flag_openmp)
20831 {
20832 gcc_assert (gimplify_omp_ctxp == NULL);
20833 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
20834 gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
20835 }
20836
20837 /* Unshare most shared trees in the body and in that of any nested functions.
20838 It would seem we don't have to do this for nested functions because
20839 they are supposed to be output and then the outer function gimplified
20840 first, but the g++ front end doesn't always do it that way. */
20841 unshare_body (fndecl);
20842 unvisit_body (fndecl);
20843
20844 /* Make sure input_location isn't set to something weird. */
20845 input_location = DECL_SOURCE_LOCATION (fndecl);
20846
20847 /* Resolve callee-copies. This has to be done before processing
20848 the body so that DECL_VALUE_EXPR gets processed correctly. */
20849 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
20850
20851 /* Gimplify the function's body. */
20852 seq = NULL;
20853 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
20854 outer_stmt = gimple_seq_first_nondebug_stmt (seq);
20855 if (!outer_stmt)
20856 {
20857 outer_stmt = gimple_build_nop ();
20858 gimplify_seq_add_stmt (&seq, outer_stmt);
20859 }
20860
20861 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
20862 not the case, wrap everything in a GIMPLE_BIND to make it so. */
20863 if (gimple_code (outer_stmt) == GIMPLE_BIND
20864 && (gimple_seq_first_nondebug_stmt (seq)
20865 == gimple_seq_last_nondebug_stmt (seq)))
20866 {
20867 outer_bind = as_a <gbind *> (outer_stmt);
20868 if (gimple_seq_first_stmt (seq) != outer_stmt
20869 || gimple_seq_last_stmt (seq) != outer_stmt)
20870 {
20871 /* If there are debug stmts before or after outer_stmt, move them
20872 inside of outer_bind body. */
20873 gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
20874 gimple_seq second_seq = NULL;
20875 if (gimple_seq_first_stmt (seq) != outer_stmt
20876 && gimple_seq_last_stmt (seq) != outer_stmt)
20877 {
20878 second_seq = gsi_split_seq_after (gsi);
20879 gsi_remove (&gsi, false);
20880 }
20881 else if (gimple_seq_first_stmt (seq) != outer_stmt)
20882 gsi_remove (&gsi, false);
20883 else
20884 {
20885 gsi_remove (&gsi, false);
20886 second_seq = seq;
20887 seq = NULL;
20888 }
20889 gimple_seq_add_seq_without_update (&seq,
20890 gimple_bind_body (outer_bind));
20891 gimple_seq_add_seq_without_update (&seq, second_seq);
20892 gimple_bind_set_body (outer_bind, seq);
20893 }
20894 }
20895 else
20896 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
20897
20898 DECL_SAVED_TREE (fndecl) = NULL_TREE;
20899
20900 /* If we had callee-copies statements, insert them at the beginning
20901 of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters. */
20902 if (!gimple_seq_empty_p (parm_stmts))
20903 {
20904 tree parm;
20905
20906 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
20907 if (parm_cleanup)
20908 {
20909 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
20910 GIMPLE_TRY_FINALLY);
20911 parm_stmts = NULL;
20912 gimple_seq_add_stmt (&parm_stmts, g);
20913 }
20914 gimple_bind_set_body (outer_bind, parm_stmts);
20915
20916 for (parm = DECL_ARGUMENTS (current_function_decl);
20917 parm; parm = DECL_CHAIN (parm))
20918 if (DECL_HAS_VALUE_EXPR_P (parm))
20919 {
20920 DECL_HAS_VALUE_EXPR_P (parm) = 0;
20921 DECL_IGNORED_P (parm) = 0;
20922 }
20923 }
20924
20925 if ((flag_openacc || flag_openmp || flag_openmp_simd)
20926 && gimplify_omp_ctxp)
20927 {
20928 delete_omp_context (gimplify_omp_ctxp);
20929 gimplify_omp_ctxp = NULL;
20930 }
20931
20932 pop_gimplify_context (outer_bind);
20933 gcc_assert (gimplify_ctxp == NULL);
20934
20935 if (flag_checking && !seen_error ())
20936 verify_gimple_in_seq (gimple_bind_body (outer_bind));
20937
20938 timevar_pop (TV_TREE_GIMPLIFY);
20939 input_location = saved_location;
20940
20941 return outer_bind;
20942 }
20943
20944 typedef char *char_p; /* For DEF_VEC_P. */
20945
20946 /* Return whether we should exclude FNDECL from instrumentation. */
20947
20948 static bool
20949 flag_instrument_functions_exclude_p (tree fndecl)
20950 {
20951 vec<char_p> *v;
20952
20953 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
20954 if (v && v->length () > 0)
20955 {
20956 const char *name;
20957 int i;
20958 char *s;
20959
20960 name = lang_hooks.decl_printable_name (fndecl, 1);
20961 FOR_EACH_VEC_ELT (*v, i, s)
20962 if (strstr (name, s) != NULL)
20963 return true;
20964 }
20965
20966 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
20967 if (v && v->length () > 0)
20968 {
20969 const char *name;
20970 int i;
20971 char *s;
20972
20973 name = DECL_SOURCE_FILE (fndecl);
20974 FOR_EACH_VEC_ELT (*v, i, s)
20975 if (strstr (name, s) != NULL)
20976 return true;
20977 }
20978
20979 return false;
20980 }
20981
20982 /* Build a call to the instrumentation function FNCODE and add it to SEQ.
20983 If COND_VAR is not NULL, it is a boolean variable guarding the call to
20984 the instrumentation function. IF STMT is not NULL, it is a statement
20985 to be executed just before the call to the instrumentation function. */
20986
20987 static void
20988 build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
20989 tree cond_var, gimple *stmt)
20990 {
20991 /* The instrumentation hooks aren't going to call the instrumented
20992 function and the address they receive is expected to be matchable
20993 against symbol addresses. Make sure we don't create a trampoline,
20994 in case the current function is nested. */
20995 tree this_fn_addr = build_fold_addr_expr (current_function_decl);
20996 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
20997
20998 tree label_true, label_false;
20999 if (cond_var)
21000 {
21001 label_true = create_artificial_label (UNKNOWN_LOCATION);
21002 label_false = create_artificial_label (UNKNOWN_LOCATION);
21003 gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
21004 label_true, label_false);
21005 gimplify_seq_add_stmt (seq, cond);
21006 gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
21007 gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
21008 NOT_TAKEN));
21009 }
21010
21011 if (stmt)
21012 gimplify_seq_add_stmt (seq, stmt);
21013
21014 tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
21015 gcall *call = gimple_build_call (x, 1, integer_zero_node);
21016 tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
21017 gimple_call_set_lhs (call, tmp_var);
21018 gimplify_seq_add_stmt (seq, call);
21019 x = builtin_decl_implicit (fncode);
21020 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
21021 gimplify_seq_add_stmt (seq, call);
21022
21023 if (cond_var)
21024 gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
21025 }
21026
21027 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
21028 node for the function we want to gimplify.
21029
21030 Return the sequence of GIMPLE statements corresponding to the body
21031 of FNDECL. */
21032
21033 void
21034 gimplify_function_tree (tree fndecl)
21035 {
21036 gimple_seq seq;
21037 gbind *bind;
21038
21039 gcc_assert (!gimple_body (fndecl));
21040
21041 if (DECL_STRUCT_FUNCTION (fndecl))
21042 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
21043 else
21044 push_struct_function (fndecl);
21045
21046 reset_cond_uid ();
21047 if (cond_uids)
21048 {
21049 delete cond_uids;
21050 cond_uids = NULL;
21051 }
21052
21053 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
21054 if necessary. */
21055 cfun->curr_properties |= PROP_gimple_lva;
21056
21057 if (asan_sanitize_use_after_scope ())
21058 asan_poisoned_variables = new hash_set<tree> ();
21059 if (flag_openmp)
21060 omp_resolved_variant_calls = new hash_set<tree> ();
21061
21062 bind = gimplify_body (fndecl, true);
21063
21064 if (omp_resolved_variant_calls)
21065 {
21066 delete omp_resolved_variant_calls;
21067 omp_resolved_variant_calls = NULL;
21068 }
21069 if (asan_poisoned_variables)
21070 {
21071 delete asan_poisoned_variables;
21072 asan_poisoned_variables = NULL;
21073 }
21074
21075 /* The tree body of the function is no longer needed, replace it
21076 with the new GIMPLE body. */
21077 seq = NULL;
21078 gimple_seq_add_stmt (&seq, bind);
21079 gimple_set_body (fndecl, seq);
21080
21081 /* If we're instrumenting function entry/exit, then prepend the call to
21082 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
21083 catch the exit hook. */
21084 /* ??? Add some way to ignore exceptions for this TFE. */
21085 if (flag_instrument_function_entry_exit
21086 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
21087 /* Do not instrument extern inline functions. */
21088 && !(DECL_DECLARED_INLINE_P (fndecl)
21089 && DECL_EXTERNAL (fndecl)
21090 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
21091 && !flag_instrument_functions_exclude_p (fndecl))
21092 {
21093 gimple_seq body = NULL, cleanup = NULL;
21094 gassign *assign;
21095 tree cond_var;
21096
21097 /* If -finstrument-functions-once is specified, generate:
21098
21099 static volatile bool C.0 = false;
21100 bool tmp_called;
21101
21102 tmp_called = C.0;
21103 if (!tmp_called)
21104 {
21105 C.0 = true;
21106 [call profiling enter function]
21107 }
21108
21109 without specific protection for data races. */
21110 if (flag_instrument_function_entry_exit > 1)
21111 {
21112 tree first_var
21113 = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
21114 VAR_DECL,
21115 create_tmp_var_name ("C"),
21116 boolean_type_node);
21117 DECL_ARTIFICIAL (first_var) = 1;
21118 DECL_IGNORED_P (first_var) = 1;
21119 TREE_STATIC (first_var) = 1;
21120 TREE_THIS_VOLATILE (first_var) = 1;
21121 TREE_USED (first_var) = 1;
21122 DECL_INITIAL (first_var) = boolean_false_node;
21123 varpool_node::add (first_var);
21124
21125 cond_var = create_tmp_var (boolean_type_node, "tmp_called");
21126 assign = gimple_build_assign (cond_var, first_var);
21127 gimplify_seq_add_stmt (&body, assign);
21128
21129 assign = gimple_build_assign (first_var, boolean_true_node);
21130 }
21131
21132 else
21133 {
21134 cond_var = NULL_TREE;
21135 assign = NULL;
21136 }
21137
21138 build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
21139 cond_var, assign);
21140
21141 /* If -finstrument-functions-once is specified, generate:
21142
21143 if (!tmp_called)
21144 [call profiling exit function]
21145
21146 without specific protection for data races. */
21147 build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
21148 cond_var, NULL);
21149
21150 gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
21151 gimplify_seq_add_stmt (&body, tf);
21152 gbind *new_bind = gimple_build_bind (NULL, body, NULL);
21153
21154 /* Replace the current function body with the body
21155 wrapped in the try/finally TF. */
21156 seq = NULL;
21157 gimple_seq_add_stmt (&seq, new_bind);
21158 gimple_set_body (fndecl, seq);
21159 bind = new_bind;
21160 }
21161
21162 if (sanitize_flags_p (SANITIZE_THREAD)
21163 && param_tsan_instrument_func_entry_exit)
21164 {
21165 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
21166 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
21167 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
21168 /* Replace the current function body with the body
21169 wrapped in the try/finally TF. */
21170 seq = NULL;
21171 gimple_seq_add_stmt (&seq, new_bind);
21172 gimple_set_body (fndecl, seq);
21173 }
21174
21175 DECL_SAVED_TREE (fndecl) = NULL_TREE;
21176 cfun->curr_properties |= PROP_gimple_any;
21177
21178 pop_cfun ();
21179
21180 dump_function (TDI_gimple, fndecl);
21181 }
21182
21183 /* Return a dummy expression of type TYPE in order to keep going after an
21184 error. */
21185
21186 static tree
21187 dummy_object (tree type)
21188 {
21189 tree t = build_int_cst (build_pointer_type (type), 0);
21190 return build2 (MEM_REF, type, t, t);
21191 }
21192
21193 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
21194 builtin function, but a very special sort of operator. */
21195
21196 enum gimplify_status
21197 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
21198 gimple_seq *post_p ATTRIBUTE_UNUSED)
21199 {
21200 tree promoted_type, have_va_type;
21201 tree valist = TREE_OPERAND (*expr_p, 0);
21202 tree type = TREE_TYPE (*expr_p);
21203 tree t, tag, aptag;
21204 location_t loc = EXPR_LOCATION (*expr_p);
21205
21206 /* Verify that valist is of the proper type. */
21207 have_va_type = TREE_TYPE (valist);
21208 if (have_va_type == error_mark_node)
21209 return GS_ERROR;
21210 have_va_type = targetm.canonical_va_list_type (have_va_type);
21211 if (have_va_type == NULL_TREE
21212 && POINTER_TYPE_P (TREE_TYPE (valist)))
21213 /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg. */
21214 have_va_type
21215 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
21216 gcc_assert (have_va_type != NULL_TREE);
21217
21218 /* Generate a diagnostic for requesting data of a type that cannot
21219 be passed through `...' due to type promotion at the call site. */
21220 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
21221 != type)
21222 {
21223 static bool gave_help;
21224 bool warned;
21225 /* Use the expansion point to handle cases such as passing bool (defined
21226 in a system header) through `...'. */
21227 location_t xloc
21228 = expansion_point_location_if_in_system_header (loc);
21229
21230 /* Unfortunately, this is merely undefined, rather than a constraint
21231 violation, so we cannot make this an error. If this call is never
21232 executed, the program is still strictly conforming. */
21233 auto_diagnostic_group d;
21234 warned = warning_at (xloc, 0,
21235 "%qT is promoted to %qT when passed through %<...%>",
21236 type, promoted_type);
21237 if (!gave_help && warned)
21238 {
21239 gave_help = true;
21240 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
21241 promoted_type, type);
21242 }
21243
21244 /* We can, however, treat "undefined" any way we please.
21245 Call abort to encourage the user to fix the program. */
21246 if (warned)
21247 inform (xloc, "if this code is reached, the program will abort");
21248 /* Before the abort, allow the evaluation of the va_list
21249 expression to exit or longjmp. */
21250 gimplify_and_add (valist, pre_p);
21251 t = build_call_expr_loc (loc,
21252 builtin_decl_implicit (BUILT_IN_TRAP), 0);
21253 gimplify_and_add (t, pre_p);
21254
21255 /* This is dead code, but go ahead and finish so that the
21256 mode of the result comes out right. */
21257 *expr_p = dummy_object (type);
21258 return GS_ALL_DONE;
21259 }
21260
21261 tag = build_int_cst (build_pointer_type (type), 0);
21262 aptag = build_int_cst (TREE_TYPE (valist), 0);
21263
21264 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
21265 valist, tag, aptag);
21266
21267 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
21268 needs to be expanded. */
21269 cfun->curr_properties &= ~PROP_gimple_lva;
21270
21271 return GS_OK;
21272 }
21273
21274 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
21275
21276 DST/SRC are the destination and source respectively. You can pass
21277 ungimplified trees in DST or SRC, in which case they will be
21278 converted to a gimple operand if necessary.
21279
21280 This function returns the newly created GIMPLE_ASSIGN tuple. */
21281
21282 gimple *
21283 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
21284 {
21285 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
21286 gimplify_and_add (t, seq_p);
21287 ggc_free (t);
21288 return gimple_seq_last_stmt (*seq_p);
21289 }
21290
21291 inline hashval_t
21292 gimplify_hasher::hash (const elt_t *p)
21293 {
21294 tree t = p->val;
21295 return iterative_hash_expr (t, 0);
21296 }
21297
21298 inline bool
21299 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
21300 {
21301 tree t1 = p1->val;
21302 tree t2 = p2->val;
21303 enum tree_code code = TREE_CODE (t1);
21304
21305 if (TREE_CODE (t2) != code
21306 || TREE_TYPE (t1) != TREE_TYPE (t2))
21307 return false;
21308
21309 if (!operand_equal_p (t1, t2, 0))
21310 return false;
21311
21312 return true;
21313 }