re PR c++/91024 (-Wimplicit-fallthrough is confused by likely/unlikely attributes)
[gcc.git] / gcc / gimplify.c
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2019 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple.h"
33 #include "gimple-predict.h"
34 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "ssa.h"
36 #include "cgraph.h"
37 #include "tree-pretty-print.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
68 #include "omp-offload.h"
69 #include "context.h"
70
71 /* Hash set of poisoned variables in a bind expr. */
72 static hash_set<tree> *asan_poisoned_variables = NULL;
73
74 enum gimplify_omp_var_data
75 {
76 GOVD_SEEN = 0x000001,
77 GOVD_EXPLICIT = 0x000002,
78 GOVD_SHARED = 0x000004,
79 GOVD_PRIVATE = 0x000008,
80 GOVD_FIRSTPRIVATE = 0x000010,
81 GOVD_LASTPRIVATE = 0x000020,
82 GOVD_REDUCTION = 0x000040,
83 GOVD_LOCAL = 0x00080,
84 GOVD_MAP = 0x000100,
85 GOVD_DEBUG_PRIVATE = 0x000200,
86 GOVD_PRIVATE_OUTER_REF = 0x000400,
87 GOVD_LINEAR = 0x000800,
88 GOVD_ALIGNED = 0x001000,
89
90 /* Flag for GOVD_MAP: don't copy back. */
91 GOVD_MAP_TO_ONLY = 0x002000,
92
93 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
94 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
95
96 GOVD_MAP_0LEN_ARRAY = 0x008000,
97
98 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
99 GOVD_MAP_ALWAYS_TO = 0x010000,
100
101 /* Flag for shared vars that are or might be stored to in the region. */
102 GOVD_WRITTEN = 0x020000,
103
104 /* Flag for GOVD_MAP, if it is a forced mapping. */
105 GOVD_MAP_FORCE = 0x040000,
106
107 /* Flag for GOVD_MAP: must be present already. */
108 GOVD_MAP_FORCE_PRESENT = 0x080000,
109
110 /* Flag for GOVD_MAP: only allocate. */
111 GOVD_MAP_ALLOC_ONLY = 0x100000,
112
113 /* Flag for GOVD_MAP: only copy back. */
114 GOVD_MAP_FROM_ONLY = 0x200000,
115
116 GOVD_NONTEMPORAL = 0x400000,
117
118 /* Flag for GOVD_LASTPRIVATE: conditional modifier. */
119 GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
120
121 GOVD_CONDTEMP = 0x1000000,
122
123 /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause. */
124 GOVD_REDUCTION_INSCAN = 0x2000000,
125
126 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
127 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
128 | GOVD_LOCAL)
129 };
130
131
132 enum omp_region_type
133 {
134 ORT_WORKSHARE = 0x00,
135 ORT_TASKGROUP = 0x01,
136 ORT_SIMD = 0x04,
137
138 ORT_PARALLEL = 0x08,
139 ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
140
141 ORT_TASK = 0x10,
142 ORT_UNTIED_TASK = ORT_TASK | 1,
143 ORT_TASKLOOP = ORT_TASK | 2,
144 ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
145
146 ORT_TEAMS = 0x20,
147 ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
148 ORT_HOST_TEAMS = ORT_TEAMS | 2,
149 ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
150
151 /* Data region. */
152 ORT_TARGET_DATA = 0x40,
153
154 /* Data region with offloading. */
155 ORT_TARGET = 0x80,
156 ORT_COMBINED_TARGET = ORT_TARGET | 1,
157
158 /* OpenACC variants. */
159 ORT_ACC = 0x100, /* A generic OpenACC region. */
160 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
161 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
162 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
163 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
164
165 /* Dummy OpenMP region, used to disable expansion of
166 DECL_VALUE_EXPRs in taskloop pre body. */
167 ORT_NONE = 0x200
168 };
169
170 /* Gimplify hashtable helper. */
171
172 struct gimplify_hasher : free_ptr_hash <elt_t>
173 {
174 static inline hashval_t hash (const elt_t *);
175 static inline bool equal (const elt_t *, const elt_t *);
176 };
177
178 struct gimplify_ctx
179 {
180 struct gimplify_ctx *prev_context;
181
182 vec<gbind *> bind_expr_stack;
183 tree temps;
184 gimple_seq conditional_cleanups;
185 tree exit_label;
186 tree return_temp;
187
188 vec<tree> case_labels;
189 hash_set<tree> *live_switch_vars;
190 /* The formal temporary table. Should this be persistent? */
191 hash_table<gimplify_hasher> *temp_htab;
192
193 int conditions;
194 unsigned into_ssa : 1;
195 unsigned allow_rhs_cond_expr : 1;
196 unsigned in_cleanup_point_expr : 1;
197 unsigned keep_stack : 1;
198 unsigned save_stack : 1;
199 unsigned in_switch_expr : 1;
200 };
201
202 enum gimplify_defaultmap_kind
203 {
204 GDMK_SCALAR,
205 GDMK_AGGREGATE,
206 GDMK_ALLOCATABLE,
207 GDMK_POINTER
208 };
209
210 struct gimplify_omp_ctx
211 {
212 struct gimplify_omp_ctx *outer_context;
213 splay_tree variables;
214 hash_set<tree> *privatized_types;
215 tree clauses;
216 /* Iteration variables in an OMP_FOR. */
217 vec<tree> loop_iter_var;
218 location_t location;
219 enum omp_clause_default_kind default_kind;
220 enum omp_region_type region_type;
221 bool combined_loop;
222 bool distribute;
223 bool target_firstprivatize_array_bases;
224 int defaultmap[4];
225 };
226
227 static struct gimplify_ctx *gimplify_ctxp;
228 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
229
230 /* Forward declaration. */
231 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
232 static hash_map<tree, tree> *oacc_declare_returns;
233 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
234 bool (*) (tree), fallback_t, bool);
235
236 /* Shorter alias name for the above function for use in gimplify.c
237 only. */
238
239 static inline void
240 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
241 {
242 gimple_seq_add_stmt_without_update (seq_p, gs);
243 }
244
245 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
246 NULL, a new sequence is allocated. This function is
247 similar to gimple_seq_add_seq, but does not scan the operands.
248 During gimplification, we need to manipulate statement sequences
249 before the def/use vectors have been constructed. */
250
251 static void
252 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
253 {
254 gimple_stmt_iterator si;
255
256 if (src == NULL)
257 return;
258
259 si = gsi_last (*dst_p);
260 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
261 }
262
263
264 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
265 and popping gimplify contexts. */
266
267 static struct gimplify_ctx *ctx_pool = NULL;
268
269 /* Return a gimplify context struct from the pool. */
270
271 static inline struct gimplify_ctx *
272 ctx_alloc (void)
273 {
274 struct gimplify_ctx * c = ctx_pool;
275
276 if (c)
277 ctx_pool = c->prev_context;
278 else
279 c = XNEW (struct gimplify_ctx);
280
281 memset (c, '\0', sizeof (*c));
282 return c;
283 }
284
285 /* Put gimplify context C back into the pool. */
286
287 static inline void
288 ctx_free (struct gimplify_ctx *c)
289 {
290 c->prev_context = ctx_pool;
291 ctx_pool = c;
292 }
293
294 /* Free allocated ctx stack memory. */
295
296 void
297 free_gimplify_stack (void)
298 {
299 struct gimplify_ctx *c;
300
301 while ((c = ctx_pool))
302 {
303 ctx_pool = c->prev_context;
304 free (c);
305 }
306 }
307
308
309 /* Set up a context for the gimplifier. */
310
311 void
312 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
313 {
314 struct gimplify_ctx *c = ctx_alloc ();
315
316 c->prev_context = gimplify_ctxp;
317 gimplify_ctxp = c;
318 gimplify_ctxp->into_ssa = in_ssa;
319 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
320 }
321
322 /* Tear down a context for the gimplifier. If BODY is non-null, then
323 put the temporaries into the outer BIND_EXPR. Otherwise, put them
324 in the local_decls.
325
326 BODY is not a sequence, but the first tuple in a sequence. */
327
328 void
329 pop_gimplify_context (gimple *body)
330 {
331 struct gimplify_ctx *c = gimplify_ctxp;
332
333 gcc_assert (c
334 && (!c->bind_expr_stack.exists ()
335 || c->bind_expr_stack.is_empty ()));
336 c->bind_expr_stack.release ();
337 gimplify_ctxp = c->prev_context;
338
339 if (body)
340 declare_vars (c->temps, body, false);
341 else
342 record_vars (c->temps);
343
344 delete c->temp_htab;
345 c->temp_htab = NULL;
346 ctx_free (c);
347 }
348
349 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
350
351 static void
352 gimple_push_bind_expr (gbind *bind_stmt)
353 {
354 gimplify_ctxp->bind_expr_stack.reserve (8);
355 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
356 }
357
358 /* Pop the first element off the stack of bindings. */
359
360 static void
361 gimple_pop_bind_expr (void)
362 {
363 gimplify_ctxp->bind_expr_stack.pop ();
364 }
365
366 /* Return the first element of the stack of bindings. */
367
368 gbind *
369 gimple_current_bind_expr (void)
370 {
371 return gimplify_ctxp->bind_expr_stack.last ();
372 }
373
374 /* Return the stack of bindings created during gimplification. */
375
376 vec<gbind *>
377 gimple_bind_expr_stack (void)
378 {
379 return gimplify_ctxp->bind_expr_stack;
380 }
381
382 /* Return true iff there is a COND_EXPR between us and the innermost
383 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
384
385 static bool
386 gimple_conditional_context (void)
387 {
388 return gimplify_ctxp->conditions > 0;
389 }
390
391 /* Note that we've entered a COND_EXPR. */
392
393 static void
394 gimple_push_condition (void)
395 {
396 #ifdef ENABLE_GIMPLE_CHECKING
397 if (gimplify_ctxp->conditions == 0)
398 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
399 #endif
400 ++(gimplify_ctxp->conditions);
401 }
402
403 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
404 now, add any conditional cleanups we've seen to the prequeue. */
405
406 static void
407 gimple_pop_condition (gimple_seq *pre_p)
408 {
409 int conds = --(gimplify_ctxp->conditions);
410
411 gcc_assert (conds >= 0);
412 if (conds == 0)
413 {
414 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
415 gimplify_ctxp->conditional_cleanups = NULL;
416 }
417 }
418
419 /* A stable comparison routine for use with splay trees and DECLs. */
420
421 static int
422 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
423 {
424 tree a = (tree) xa;
425 tree b = (tree) xb;
426
427 return DECL_UID (a) - DECL_UID (b);
428 }
429
430 /* Create a new omp construct that deals with variable remapping. */
431
432 static struct gimplify_omp_ctx *
433 new_omp_context (enum omp_region_type region_type)
434 {
435 struct gimplify_omp_ctx *c;
436
437 c = XCNEW (struct gimplify_omp_ctx);
438 c->outer_context = gimplify_omp_ctxp;
439 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
440 c->privatized_types = new hash_set<tree>;
441 c->location = input_location;
442 c->region_type = region_type;
443 if ((region_type & ORT_TASK) == 0)
444 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
445 else
446 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
447 c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
448 c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
449 c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
450 c->defaultmap[GDMK_POINTER] = GOVD_MAP;
451
452 return c;
453 }
454
455 /* Destroy an omp construct that deals with variable remapping. */
456
457 static void
458 delete_omp_context (struct gimplify_omp_ctx *c)
459 {
460 splay_tree_delete (c->variables);
461 delete c->privatized_types;
462 c->loop_iter_var.release ();
463 XDELETE (c);
464 }
465
466 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
467 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
468
469 /* Both gimplify the statement T and append it to *SEQ_P. This function
470 behaves exactly as gimplify_stmt, but you don't have to pass T as a
471 reference. */
472
473 void
474 gimplify_and_add (tree t, gimple_seq *seq_p)
475 {
476 gimplify_stmt (&t, seq_p);
477 }
478
479 /* Gimplify statement T into sequence *SEQ_P, and return the first
480 tuple in the sequence of generated tuples for this statement.
481 Return NULL if gimplifying T produced no tuples. */
482
483 static gimple *
484 gimplify_and_return_first (tree t, gimple_seq *seq_p)
485 {
486 gimple_stmt_iterator last = gsi_last (*seq_p);
487
488 gimplify_and_add (t, seq_p);
489
490 if (!gsi_end_p (last))
491 {
492 gsi_next (&last);
493 return gsi_stmt (last);
494 }
495 else
496 return gimple_seq_first_stmt (*seq_p);
497 }
498
499 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
500 LHS, or for a call argument. */
501
502 static bool
503 is_gimple_mem_rhs (tree t)
504 {
505 /* If we're dealing with a renamable type, either source or dest must be
506 a renamed variable. */
507 if (is_gimple_reg_type (TREE_TYPE (t)))
508 return is_gimple_val (t);
509 else
510 return is_gimple_val (t) || is_gimple_lvalue (t);
511 }
512
513 /* Return true if T is a CALL_EXPR or an expression that can be
514 assigned to a temporary. Note that this predicate should only be
515 used during gimplification. See the rationale for this in
516 gimplify_modify_expr. */
517
518 static bool
519 is_gimple_reg_rhs_or_call (tree t)
520 {
521 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
522 || TREE_CODE (t) == CALL_EXPR);
523 }
524
525 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
526 this predicate should only be used during gimplification. See the
527 rationale for this in gimplify_modify_expr. */
528
529 static bool
530 is_gimple_mem_rhs_or_call (tree t)
531 {
532 /* If we're dealing with a renamable type, either source or dest must be
533 a renamed variable. */
534 if (is_gimple_reg_type (TREE_TYPE (t)))
535 return is_gimple_val (t);
536 else
537 return (is_gimple_val (t)
538 || is_gimple_lvalue (t)
539 || TREE_CLOBBER_P (t)
540 || TREE_CODE (t) == CALL_EXPR);
541 }
542
543 /* Create a temporary with a name derived from VAL. Subroutine of
544 lookup_tmp_var; nobody else should call this function. */
545
546 static inline tree
547 create_tmp_from_val (tree val)
548 {
549 /* Drop all qualifiers and address-space information from the value type. */
550 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
551 tree var = create_tmp_var (type, get_name (val));
552 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
553 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
554 DECL_GIMPLE_REG_P (var) = 1;
555 return var;
556 }
557
558 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
559 an existing expression temporary. */
560
561 static tree
562 lookup_tmp_var (tree val, bool is_formal)
563 {
564 tree ret;
565
566 /* If not optimizing, never really reuse a temporary. local-alloc
567 won't allocate any variable that is used in more than one basic
568 block, which means it will go into memory, causing much extra
569 work in reload and final and poorer code generation, outweighing
570 the extra memory allocation here. */
571 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
572 ret = create_tmp_from_val (val);
573 else
574 {
575 elt_t elt, *elt_p;
576 elt_t **slot;
577
578 elt.val = val;
579 if (!gimplify_ctxp->temp_htab)
580 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
581 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
582 if (*slot == NULL)
583 {
584 elt_p = XNEW (elt_t);
585 elt_p->val = val;
586 elt_p->temp = ret = create_tmp_from_val (val);
587 *slot = elt_p;
588 }
589 else
590 {
591 elt_p = *slot;
592 ret = elt_p->temp;
593 }
594 }
595
596 return ret;
597 }
598
599 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
600
601 static tree
602 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
603 bool is_formal, bool allow_ssa)
604 {
605 tree t, mod;
606
607 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
608 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
609 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
610 fb_rvalue);
611
612 if (allow_ssa
613 && gimplify_ctxp->into_ssa
614 && is_gimple_reg_type (TREE_TYPE (val)))
615 {
616 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
617 if (! gimple_in_ssa_p (cfun))
618 {
619 const char *name = get_name (val);
620 if (name)
621 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
622 }
623 }
624 else
625 t = lookup_tmp_var (val, is_formal);
626
627 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
628
629 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
630
631 /* gimplify_modify_expr might want to reduce this further. */
632 gimplify_and_add (mod, pre_p);
633 ggc_free (mod);
634
635 return t;
636 }
637
638 /* Return a formal temporary variable initialized with VAL. PRE_P is as
639 in gimplify_expr. Only use this function if:
640
641 1) The value of the unfactored expression represented by VAL will not
642 change between the initialization and use of the temporary, and
643 2) The temporary will not be otherwise modified.
644
645 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
646 and #2 means it is inappropriate for && temps.
647
648 For other cases, use get_initialized_tmp_var instead. */
649
650 tree
651 get_formal_tmp_var (tree val, gimple_seq *pre_p)
652 {
653 return internal_get_tmp_var (val, pre_p, NULL, true, true);
654 }
655
656 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
657 are as in gimplify_expr. */
658
659 tree
660 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
661 bool allow_ssa)
662 {
663 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
664 }
665
666 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
667 generate debug info for them; otherwise don't. */
668
669 void
670 declare_vars (tree vars, gimple *gs, bool debug_info)
671 {
672 tree last = vars;
673 if (last)
674 {
675 tree temps, block;
676
677 gbind *scope = as_a <gbind *> (gs);
678
679 temps = nreverse (last);
680
681 block = gimple_bind_block (scope);
682 gcc_assert (!block || TREE_CODE (block) == BLOCK);
683 if (!block || !debug_info)
684 {
685 DECL_CHAIN (last) = gimple_bind_vars (scope);
686 gimple_bind_set_vars (scope, temps);
687 }
688 else
689 {
690 /* We need to attach the nodes both to the BIND_EXPR and to its
691 associated BLOCK for debugging purposes. The key point here
692 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
693 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
694 if (BLOCK_VARS (block))
695 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
696 else
697 {
698 gimple_bind_set_vars (scope,
699 chainon (gimple_bind_vars (scope), temps));
700 BLOCK_VARS (block) = temps;
701 }
702 }
703 }
704 }
705
706 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
707 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
708 no such upper bound can be obtained. */
709
710 static void
711 force_constant_size (tree var)
712 {
713 /* The only attempt we make is by querying the maximum size of objects
714 of the variable's type. */
715
716 HOST_WIDE_INT max_size;
717
718 gcc_assert (VAR_P (var));
719
720 max_size = max_int_size_in_bytes (TREE_TYPE (var));
721
722 gcc_assert (max_size >= 0);
723
724 DECL_SIZE_UNIT (var)
725 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
726 DECL_SIZE (var)
727 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
728 }
729
730 /* Push the temporary variable TMP into the current binding. */
731
732 void
733 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
734 {
735 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
736
737 /* Later processing assumes that the object size is constant, which might
738 not be true at this point. Force the use of a constant upper bound in
739 this case. */
740 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
741 force_constant_size (tmp);
742
743 DECL_CONTEXT (tmp) = fn->decl;
744 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
745
746 record_vars_into (tmp, fn->decl);
747 }
748
749 /* Push the temporary variable TMP into the current binding. */
750
751 void
752 gimple_add_tmp_var (tree tmp)
753 {
754 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
755
756 /* Later processing assumes that the object size is constant, which might
757 not be true at this point. Force the use of a constant upper bound in
758 this case. */
759 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
760 force_constant_size (tmp);
761
762 DECL_CONTEXT (tmp) = current_function_decl;
763 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
764
765 if (gimplify_ctxp)
766 {
767 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
768 gimplify_ctxp->temps = tmp;
769
770 /* Mark temporaries local within the nearest enclosing parallel. */
771 if (gimplify_omp_ctxp)
772 {
773 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
774 while (ctx
775 && (ctx->region_type == ORT_WORKSHARE
776 || ctx->region_type == ORT_TASKGROUP
777 || ctx->region_type == ORT_SIMD
778 || ctx->region_type == ORT_ACC))
779 ctx = ctx->outer_context;
780 if (ctx)
781 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
782 }
783 }
784 else if (cfun)
785 record_vars (tmp);
786 else
787 {
788 gimple_seq body_seq;
789
790 /* This case is for nested functions. We need to expose the locals
791 they create. */
792 body_seq = gimple_body (current_function_decl);
793 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
794 }
795 }
796
797
798 \f
799 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
800 nodes that are referenced more than once in GENERIC functions. This is
801 necessary because gimplification (translation into GIMPLE) is performed
802 by modifying tree nodes in-place, so gimplication of a shared node in a
803 first context could generate an invalid GIMPLE form in a second context.
804
805 This is achieved with a simple mark/copy/unmark algorithm that walks the
806 GENERIC representation top-down, marks nodes with TREE_VISITED the first
807 time it encounters them, duplicates them if they already have TREE_VISITED
808 set, and finally removes the TREE_VISITED marks it has set.
809
810 The algorithm works only at the function level, i.e. it generates a GENERIC
811 representation of a function with no nodes shared within the function when
812 passed a GENERIC function (except for nodes that are allowed to be shared).
813
814 At the global level, it is also necessary to unshare tree nodes that are
815 referenced in more than one function, for the same aforementioned reason.
816 This requires some cooperation from the front-end. There are 2 strategies:
817
818 1. Manual unsharing. The front-end needs to call unshare_expr on every
819 expression that might end up being shared across functions.
820
821 2. Deep unsharing. This is an extension of regular unsharing. Instead
822 of calling unshare_expr on expressions that might be shared across
823 functions, the front-end pre-marks them with TREE_VISITED. This will
824 ensure that they are unshared on the first reference within functions
825 when the regular unsharing algorithm runs. The counterpart is that
826 this algorithm must look deeper than for manual unsharing, which is
827 specified by LANG_HOOKS_DEEP_UNSHARING.
828
829 If there are only few specific cases of node sharing across functions, it is
830 probably easier for a front-end to unshare the expressions manually. On the
831 contrary, if the expressions generated at the global level are as widespread
832 as expressions generated within functions, deep unsharing is very likely the
833 way to go. */
834
835 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
836 These nodes model computations that must be done once. If we were to
837 unshare something like SAVE_EXPR(i++), the gimplification process would
838 create wrong code. However, if DATA is non-null, it must hold a pointer
839 set that is used to unshare the subtrees of these nodes. */
840
841 static tree
842 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
843 {
844 tree t = *tp;
845 enum tree_code code = TREE_CODE (t);
846
847 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
848 copy their subtrees if we can make sure to do it only once. */
849 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
850 {
851 if (data && !((hash_set<tree> *)data)->add (t))
852 ;
853 else
854 *walk_subtrees = 0;
855 }
856
857 /* Stop at types, decls, constants like copy_tree_r. */
858 else if (TREE_CODE_CLASS (code) == tcc_type
859 || TREE_CODE_CLASS (code) == tcc_declaration
860 || TREE_CODE_CLASS (code) == tcc_constant)
861 *walk_subtrees = 0;
862
863 /* Cope with the statement expression extension. */
864 else if (code == STATEMENT_LIST)
865 ;
866
867 /* Leave the bulk of the work to copy_tree_r itself. */
868 else
869 copy_tree_r (tp, walk_subtrees, NULL);
870
871 return NULL_TREE;
872 }
873
874 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
875 If *TP has been visited already, then *TP is deeply copied by calling
876 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
877
878 static tree
879 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
880 {
881 tree t = *tp;
882 enum tree_code code = TREE_CODE (t);
883
884 /* Skip types, decls, and constants. But we do want to look at their
885 types and the bounds of types. Mark them as visited so we properly
886 unmark their subtrees on the unmark pass. If we've already seen them,
887 don't look down further. */
888 if (TREE_CODE_CLASS (code) == tcc_type
889 || TREE_CODE_CLASS (code) == tcc_declaration
890 || TREE_CODE_CLASS (code) == tcc_constant)
891 {
892 if (TREE_VISITED (t))
893 *walk_subtrees = 0;
894 else
895 TREE_VISITED (t) = 1;
896 }
897
898 /* If this node has been visited already, unshare it and don't look
899 any deeper. */
900 else if (TREE_VISITED (t))
901 {
902 walk_tree (tp, mostly_copy_tree_r, data, NULL);
903 *walk_subtrees = 0;
904 }
905
906 /* Otherwise, mark the node as visited and keep looking. */
907 else
908 TREE_VISITED (t) = 1;
909
910 return NULL_TREE;
911 }
912
913 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
914 copy_if_shared_r callback unmodified. */
915
916 static inline void
917 copy_if_shared (tree *tp, void *data)
918 {
919 walk_tree (tp, copy_if_shared_r, data, NULL);
920 }
921
922 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
923 any nested functions. */
924
925 static void
926 unshare_body (tree fndecl)
927 {
928 struct cgraph_node *cgn = cgraph_node::get (fndecl);
929 /* If the language requires deep unsharing, we need a pointer set to make
930 sure we don't repeatedly unshare subtrees of unshareable nodes. */
931 hash_set<tree> *visited
932 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
933
934 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
935 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
936 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
937
938 delete visited;
939
940 if (cgn)
941 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
942 unshare_body (cgn->decl);
943 }
944
945 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
946 Subtrees are walked until the first unvisited node is encountered. */
947
948 static tree
949 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
950 {
951 tree t = *tp;
952
953 /* If this node has been visited, unmark it and keep looking. */
954 if (TREE_VISITED (t))
955 TREE_VISITED (t) = 0;
956
957 /* Otherwise, don't look any deeper. */
958 else
959 *walk_subtrees = 0;
960
961 return NULL_TREE;
962 }
963
964 /* Unmark the visited trees rooted at *TP. */
965
966 static inline void
967 unmark_visited (tree *tp)
968 {
969 walk_tree (tp, unmark_visited_r, NULL, NULL);
970 }
971
972 /* Likewise, but mark all trees as not visited. */
973
974 static void
975 unvisit_body (tree fndecl)
976 {
977 struct cgraph_node *cgn = cgraph_node::get (fndecl);
978
979 unmark_visited (&DECL_SAVED_TREE (fndecl));
980 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
981 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
982
983 if (cgn)
984 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
985 unvisit_body (cgn->decl);
986 }
987
988 /* Unconditionally make an unshared copy of EXPR. This is used when using
989 stored expressions which span multiple functions, such as BINFO_VTABLE,
990 as the normal unsharing process can't tell that they're shared. */
991
992 tree
993 unshare_expr (tree expr)
994 {
995 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
996 return expr;
997 }
998
999 /* Worker for unshare_expr_without_location. */
1000
1001 static tree
1002 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1003 {
1004 if (EXPR_P (*tp))
1005 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1006 else
1007 *walk_subtrees = 0;
1008 return NULL_TREE;
1009 }
1010
1011 /* Similar to unshare_expr but also prune all expression locations
1012 from EXPR. */
1013
1014 tree
1015 unshare_expr_without_location (tree expr)
1016 {
1017 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1018 if (EXPR_P (expr))
1019 walk_tree (&expr, prune_expr_location, NULL, NULL);
1020 return expr;
1021 }
1022
1023 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
1024 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
1025 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
1026 EXPR is the location of the EXPR. */
1027
1028 static location_t
1029 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1030 {
1031 if (!expr)
1032 return or_else;
1033
1034 if (EXPR_HAS_LOCATION (expr))
1035 return EXPR_LOCATION (expr);
1036
1037 if (TREE_CODE (expr) != STATEMENT_LIST)
1038 return or_else;
1039
1040 tree_stmt_iterator i = tsi_start (expr);
1041
1042 bool found = false;
1043 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1044 {
1045 found = true;
1046 tsi_next (&i);
1047 }
1048
1049 if (!found || !tsi_one_before_end_p (i))
1050 return or_else;
1051
1052 return rexpr_location (tsi_stmt (i), or_else);
1053 }
1054
1055 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1056 rexpr_location for the potential recursion. */
1057
1058 static inline bool
1059 rexpr_has_location (tree expr)
1060 {
1061 return rexpr_location (expr) != UNKNOWN_LOCATION;
1062 }
1063
1064 \f
1065 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1066 contain statements and have a value. Assign its value to a temporary
1067 and give it void_type_node. Return the temporary, or NULL_TREE if
1068 WRAPPER was already void. */
1069
1070 tree
1071 voidify_wrapper_expr (tree wrapper, tree temp)
1072 {
1073 tree type = TREE_TYPE (wrapper);
1074 if (type && !VOID_TYPE_P (type))
1075 {
1076 tree *p;
1077
1078 /* Set p to point to the body of the wrapper. Loop until we find
1079 something that isn't a wrapper. */
1080 for (p = &wrapper; p && *p; )
1081 {
1082 switch (TREE_CODE (*p))
1083 {
1084 case BIND_EXPR:
1085 TREE_SIDE_EFFECTS (*p) = 1;
1086 TREE_TYPE (*p) = void_type_node;
1087 /* For a BIND_EXPR, the body is operand 1. */
1088 p = &BIND_EXPR_BODY (*p);
1089 break;
1090
1091 case CLEANUP_POINT_EXPR:
1092 case TRY_FINALLY_EXPR:
1093 case TRY_CATCH_EXPR:
1094 TREE_SIDE_EFFECTS (*p) = 1;
1095 TREE_TYPE (*p) = void_type_node;
1096 p = &TREE_OPERAND (*p, 0);
1097 break;
1098
1099 case STATEMENT_LIST:
1100 {
1101 tree_stmt_iterator i = tsi_last (*p);
1102 TREE_SIDE_EFFECTS (*p) = 1;
1103 TREE_TYPE (*p) = void_type_node;
1104 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1105 }
1106 break;
1107
1108 case COMPOUND_EXPR:
1109 /* Advance to the last statement. Set all container types to
1110 void. */
1111 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1112 {
1113 TREE_SIDE_EFFECTS (*p) = 1;
1114 TREE_TYPE (*p) = void_type_node;
1115 }
1116 break;
1117
1118 case TRANSACTION_EXPR:
1119 TREE_SIDE_EFFECTS (*p) = 1;
1120 TREE_TYPE (*p) = void_type_node;
1121 p = &TRANSACTION_EXPR_BODY (*p);
1122 break;
1123
1124 default:
1125 /* Assume that any tree upon which voidify_wrapper_expr is
1126 directly called is a wrapper, and that its body is op0. */
1127 if (p == &wrapper)
1128 {
1129 TREE_SIDE_EFFECTS (*p) = 1;
1130 TREE_TYPE (*p) = void_type_node;
1131 p = &TREE_OPERAND (*p, 0);
1132 break;
1133 }
1134 goto out;
1135 }
1136 }
1137
1138 out:
1139 if (p == NULL || IS_EMPTY_STMT (*p))
1140 temp = NULL_TREE;
1141 else if (temp)
1142 {
1143 /* The wrapper is on the RHS of an assignment that we're pushing
1144 down. */
1145 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1146 || TREE_CODE (temp) == MODIFY_EXPR);
1147 TREE_OPERAND (temp, 1) = *p;
1148 *p = temp;
1149 }
1150 else
1151 {
1152 temp = create_tmp_var (type, "retval");
1153 *p = build2 (INIT_EXPR, type, temp, *p);
1154 }
1155
1156 return temp;
1157 }
1158
1159 return NULL_TREE;
1160 }
1161
1162 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1163 a temporary through which they communicate. */
1164
1165 static void
1166 build_stack_save_restore (gcall **save, gcall **restore)
1167 {
1168 tree tmp_var;
1169
1170 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1171 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1172 gimple_call_set_lhs (*save, tmp_var);
1173
1174 *restore
1175 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1176 1, tmp_var);
1177 }
1178
1179 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1180
1181 static tree
1182 build_asan_poison_call_expr (tree decl)
1183 {
1184 /* Do not poison variables that have size equal to zero. */
1185 tree unit_size = DECL_SIZE_UNIT (decl);
1186 if (zerop (unit_size))
1187 return NULL_TREE;
1188
1189 tree base = build_fold_addr_expr (decl);
1190
1191 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1192 void_type_node, 3,
1193 build_int_cst (integer_type_node,
1194 ASAN_MARK_POISON),
1195 base, unit_size);
1196 }
1197
1198 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1199 on POISON flag, shadow memory of a DECL variable. The call will be
1200 put on location identified by IT iterator, where BEFORE flag drives
1201 position where the stmt will be put. */
1202
1203 static void
1204 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1205 bool before)
1206 {
1207 tree unit_size = DECL_SIZE_UNIT (decl);
1208 tree base = build_fold_addr_expr (decl);
1209
1210 /* Do not poison variables that have size equal to zero. */
1211 if (zerop (unit_size))
1212 return;
1213
1214 /* It's necessary to have all stack variables aligned to ASAN granularity
1215 bytes. */
1216 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1217 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1218
1219 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1220
1221 gimple *g
1222 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1223 build_int_cst (integer_type_node, flags),
1224 base, unit_size);
1225
1226 if (before)
1227 gsi_insert_before (it, g, GSI_NEW_STMT);
1228 else
1229 gsi_insert_after (it, g, GSI_NEW_STMT);
1230 }
1231
1232 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1233 either poisons or unpoisons a DECL. Created statement is appended
1234 to SEQ_P gimple sequence. */
1235
1236 static void
1237 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1238 {
1239 gimple_stmt_iterator it = gsi_last (*seq_p);
1240 bool before = false;
1241
1242 if (gsi_end_p (it))
1243 before = true;
1244
1245 asan_poison_variable (decl, poison, &it, before);
1246 }
1247
1248 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1249
1250 static int
1251 sort_by_decl_uid (const void *a, const void *b)
1252 {
1253 const tree *t1 = (const tree *)a;
1254 const tree *t2 = (const tree *)b;
1255
1256 int uid1 = DECL_UID (*t1);
1257 int uid2 = DECL_UID (*t2);
1258
1259 if (uid1 < uid2)
1260 return -1;
1261 else if (uid1 > uid2)
1262 return 1;
1263 else
1264 return 0;
1265 }
1266
1267 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1268 depending on POISON flag. Created statement is appended
1269 to SEQ_P gimple sequence. */
1270
1271 static void
1272 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1273 {
1274 unsigned c = variables->elements ();
1275 if (c == 0)
1276 return;
1277
1278 auto_vec<tree> sorted_variables (c);
1279
1280 for (hash_set<tree>::iterator it = variables->begin ();
1281 it != variables->end (); ++it)
1282 sorted_variables.safe_push (*it);
1283
1284 sorted_variables.qsort (sort_by_decl_uid);
1285
1286 unsigned i;
1287 tree var;
1288 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1289 {
1290 asan_poison_variable (var, poison, seq_p);
1291
1292 /* Add use_after_scope_memory attribute for the variable in order
1293 to prevent re-written into SSA. */
1294 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1295 DECL_ATTRIBUTES (var)))
1296 DECL_ATTRIBUTES (var)
1297 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1298 integer_one_node,
1299 DECL_ATTRIBUTES (var));
1300 }
1301 }
1302
1303 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1304
1305 static enum gimplify_status
1306 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1307 {
1308 tree bind_expr = *expr_p;
1309 bool old_keep_stack = gimplify_ctxp->keep_stack;
1310 bool old_save_stack = gimplify_ctxp->save_stack;
1311 tree t;
1312 gbind *bind_stmt;
1313 gimple_seq body, cleanup;
1314 gcall *stack_save;
1315 location_t start_locus = 0, end_locus = 0;
1316 tree ret_clauses = NULL;
1317
1318 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1319
1320 /* Mark variables seen in this bind expr. */
1321 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1322 {
1323 if (VAR_P (t))
1324 {
1325 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1326
1327 /* Mark variable as local. */
1328 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1329 {
1330 if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1331 || splay_tree_lookup (ctx->variables,
1332 (splay_tree_key) t) == NULL)
1333 {
1334 if (ctx->region_type == ORT_SIMD
1335 && TREE_ADDRESSABLE (t)
1336 && !TREE_STATIC (t))
1337 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1338 else
1339 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1340 }
1341 /* Static locals inside of target construct or offloaded
1342 routines need to be "omp declare target". */
1343 if (TREE_STATIC (t))
1344 for (; ctx; ctx = ctx->outer_context)
1345 if ((ctx->region_type & ORT_TARGET) != 0)
1346 {
1347 if (!lookup_attribute ("omp declare target",
1348 DECL_ATTRIBUTES (t)))
1349 {
1350 tree id = get_identifier ("omp declare target");
1351 DECL_ATTRIBUTES (t)
1352 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
1353 varpool_node *node = varpool_node::get (t);
1354 if (node)
1355 {
1356 node->offloadable = 1;
1357 if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
1358 {
1359 g->have_offload = true;
1360 if (!in_lto_p)
1361 vec_safe_push (offload_vars, t);
1362 }
1363 }
1364 }
1365 break;
1366 }
1367 }
1368
1369 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1370
1371 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1372 cfun->has_local_explicit_reg_vars = true;
1373 }
1374
1375 /* Preliminarily mark non-addressed complex variables as eligible
1376 for promotion to gimple registers. We'll transform their uses
1377 as we find them. */
1378 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1379 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1380 && !TREE_THIS_VOLATILE (t)
1381 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1382 && !needs_to_live_in_memory (t))
1383 DECL_GIMPLE_REG_P (t) = 1;
1384 }
1385
1386 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1387 BIND_EXPR_BLOCK (bind_expr));
1388 gimple_push_bind_expr (bind_stmt);
1389
1390 gimplify_ctxp->keep_stack = false;
1391 gimplify_ctxp->save_stack = false;
1392
1393 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1394 body = NULL;
1395 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1396 gimple_bind_set_body (bind_stmt, body);
1397
1398 /* Source location wise, the cleanup code (stack_restore and clobbers)
1399 belongs to the end of the block, so propagate what we have. The
1400 stack_save operation belongs to the beginning of block, which we can
1401 infer from the bind_expr directly if the block has no explicit
1402 assignment. */
1403 if (BIND_EXPR_BLOCK (bind_expr))
1404 {
1405 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1406 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1407 }
1408 if (start_locus == 0)
1409 start_locus = EXPR_LOCATION (bind_expr);
1410
1411 cleanup = NULL;
1412 stack_save = NULL;
1413
1414 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1415 the stack space allocated to the VLAs. */
1416 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1417 {
1418 gcall *stack_restore;
1419
1420 /* Save stack on entry and restore it on exit. Add a try_finally
1421 block to achieve this. */
1422 build_stack_save_restore (&stack_save, &stack_restore);
1423
1424 gimple_set_location (stack_save, start_locus);
1425 gimple_set_location (stack_restore, end_locus);
1426
1427 gimplify_seq_add_stmt (&cleanup, stack_restore);
1428 }
1429
1430 /* Add clobbers for all variables that go out of scope. */
1431 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1432 {
1433 if (VAR_P (t)
1434 && !is_global_var (t)
1435 && DECL_CONTEXT (t) == current_function_decl)
1436 {
1437 if (!DECL_HARD_REGISTER (t)
1438 && !TREE_THIS_VOLATILE (t)
1439 && !DECL_HAS_VALUE_EXPR_P (t)
1440 /* Only care for variables that have to be in memory. Others
1441 will be rewritten into SSA names, hence moved to the
1442 top-level. */
1443 && !is_gimple_reg (t)
1444 && flag_stack_reuse != SR_NONE)
1445 {
1446 tree clobber = build_clobber (TREE_TYPE (t));
1447 gimple *clobber_stmt;
1448 clobber_stmt = gimple_build_assign (t, clobber);
1449 gimple_set_location (clobber_stmt, end_locus);
1450 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1451 }
1452
1453 if (flag_openacc && oacc_declare_returns != NULL)
1454 {
1455 tree *c = oacc_declare_returns->get (t);
1456 if (c != NULL)
1457 {
1458 if (ret_clauses)
1459 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1460
1461 ret_clauses = *c;
1462
1463 oacc_declare_returns->remove (t);
1464
1465 if (oacc_declare_returns->is_empty ())
1466 {
1467 delete oacc_declare_returns;
1468 oacc_declare_returns = NULL;
1469 }
1470 }
1471 }
1472 }
1473
1474 if (asan_poisoned_variables != NULL
1475 && asan_poisoned_variables->contains (t))
1476 {
1477 asan_poisoned_variables->remove (t);
1478 asan_poison_variable (t, true, &cleanup);
1479 }
1480
1481 if (gimplify_ctxp->live_switch_vars != NULL
1482 && gimplify_ctxp->live_switch_vars->contains (t))
1483 gimplify_ctxp->live_switch_vars->remove (t);
1484 }
1485
1486 if (ret_clauses)
1487 {
1488 gomp_target *stmt;
1489 gimple_stmt_iterator si = gsi_start (cleanup);
1490
1491 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1492 ret_clauses);
1493 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1494 }
1495
1496 if (cleanup)
1497 {
1498 gtry *gs;
1499 gimple_seq new_body;
1500
1501 new_body = NULL;
1502 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1503 GIMPLE_TRY_FINALLY);
1504
1505 if (stack_save)
1506 gimplify_seq_add_stmt (&new_body, stack_save);
1507 gimplify_seq_add_stmt (&new_body, gs);
1508 gimple_bind_set_body (bind_stmt, new_body);
1509 }
1510
1511 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1512 if (!gimplify_ctxp->keep_stack)
1513 gimplify_ctxp->keep_stack = old_keep_stack;
1514 gimplify_ctxp->save_stack = old_save_stack;
1515
1516 gimple_pop_bind_expr ();
1517
1518 gimplify_seq_add_stmt (pre_p, bind_stmt);
1519
1520 if (temp)
1521 {
1522 *expr_p = temp;
1523 return GS_OK;
1524 }
1525
1526 *expr_p = NULL_TREE;
1527 return GS_ALL_DONE;
1528 }
1529
1530 /* Maybe add early return predict statement to PRE_P sequence. */
1531
1532 static void
1533 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1534 {
1535 /* If we are not in a conditional context, add PREDICT statement. */
1536 if (gimple_conditional_context ())
1537 {
1538 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1539 NOT_TAKEN);
1540 gimplify_seq_add_stmt (pre_p, predict);
1541 }
1542 }
1543
1544 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1545 GIMPLE value, it is assigned to a new temporary and the statement is
1546 re-written to return the temporary.
1547
1548 PRE_P points to the sequence where side effects that must happen before
1549 STMT should be stored. */
1550
1551 static enum gimplify_status
1552 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1553 {
1554 greturn *ret;
1555 tree ret_expr = TREE_OPERAND (stmt, 0);
1556 tree result_decl, result;
1557
1558 if (ret_expr == error_mark_node)
1559 return GS_ERROR;
1560
1561 if (!ret_expr
1562 || TREE_CODE (ret_expr) == RESULT_DECL)
1563 {
1564 maybe_add_early_return_predict_stmt (pre_p);
1565 greturn *ret = gimple_build_return (ret_expr);
1566 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1567 gimplify_seq_add_stmt (pre_p, ret);
1568 return GS_ALL_DONE;
1569 }
1570
1571 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1572 result_decl = NULL_TREE;
1573 else
1574 {
1575 result_decl = TREE_OPERAND (ret_expr, 0);
1576
1577 /* See through a return by reference. */
1578 if (TREE_CODE (result_decl) == INDIRECT_REF)
1579 result_decl = TREE_OPERAND (result_decl, 0);
1580
1581 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1582 || TREE_CODE (ret_expr) == INIT_EXPR)
1583 && TREE_CODE (result_decl) == RESULT_DECL);
1584 }
1585
1586 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1587 Recall that aggregate_value_p is FALSE for any aggregate type that is
1588 returned in registers. If we're returning values in registers, then
1589 we don't want to extend the lifetime of the RESULT_DECL, particularly
1590 across another call. In addition, for those aggregates for which
1591 hard_function_value generates a PARALLEL, we'll die during normal
1592 expansion of structure assignments; there's special code in expand_return
1593 to handle this case that does not exist in expand_expr. */
1594 if (!result_decl)
1595 result = NULL_TREE;
1596 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1597 {
1598 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1599 {
1600 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1601 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1602 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1603 should be effectively allocated by the caller, i.e. all calls to
1604 this function must be subject to the Return Slot Optimization. */
1605 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1606 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1607 }
1608 result = result_decl;
1609 }
1610 else if (gimplify_ctxp->return_temp)
1611 result = gimplify_ctxp->return_temp;
1612 else
1613 {
1614 result = create_tmp_reg (TREE_TYPE (result_decl));
1615
1616 /* ??? With complex control flow (usually involving abnormal edges),
1617 we can wind up warning about an uninitialized value for this. Due
1618 to how this variable is constructed and initialized, this is never
1619 true. Give up and never warn. */
1620 TREE_NO_WARNING (result) = 1;
1621
1622 gimplify_ctxp->return_temp = result;
1623 }
1624
1625 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1626 Then gimplify the whole thing. */
1627 if (result != result_decl)
1628 TREE_OPERAND (ret_expr, 0) = result;
1629
1630 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1631
1632 maybe_add_early_return_predict_stmt (pre_p);
1633 ret = gimple_build_return (result);
1634 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1635 gimplify_seq_add_stmt (pre_p, ret);
1636
1637 return GS_ALL_DONE;
1638 }
1639
1640 /* Gimplify a variable-length array DECL. */
1641
1642 static void
1643 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1644 {
1645 /* This is a variable-sized decl. Simplify its size and mark it
1646 for deferred expansion. */
1647 tree t, addr, ptr_type;
1648
1649 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1650 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1651
1652 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1653 if (DECL_HAS_VALUE_EXPR_P (decl))
1654 return;
1655
1656 /* All occurrences of this decl in final gimplified code will be
1657 replaced by indirection. Setting DECL_VALUE_EXPR does two
1658 things: First, it lets the rest of the gimplifier know what
1659 replacement to use. Second, it lets the debug info know
1660 where to find the value. */
1661 ptr_type = build_pointer_type (TREE_TYPE (decl));
1662 addr = create_tmp_var (ptr_type, get_name (decl));
1663 DECL_IGNORED_P (addr) = 0;
1664 t = build_fold_indirect_ref (addr);
1665 TREE_THIS_NOTRAP (t) = 1;
1666 SET_DECL_VALUE_EXPR (decl, t);
1667 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1668
1669 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1670 max_int_size_in_bytes (TREE_TYPE (decl)));
1671 /* The call has been built for a variable-sized object. */
1672 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1673 t = fold_convert (ptr_type, t);
1674 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1675
1676 gimplify_and_add (t, seq_p);
1677 }
1678
1679 /* A helper function to be called via walk_tree. Mark all labels under *TP
1680 as being forced. To be called for DECL_INITIAL of static variables. */
1681
1682 static tree
1683 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1684 {
1685 if (TYPE_P (*tp))
1686 *walk_subtrees = 0;
1687 if (TREE_CODE (*tp) == LABEL_DECL)
1688 {
1689 FORCED_LABEL (*tp) = 1;
1690 cfun->has_forced_label_in_static = 1;
1691 }
1692
1693 return NULL_TREE;
1694 }
1695
1696 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1697 and initialization explicit. */
1698
1699 static enum gimplify_status
1700 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1701 {
1702 tree stmt = *stmt_p;
1703 tree decl = DECL_EXPR_DECL (stmt);
1704
1705 *stmt_p = NULL_TREE;
1706
1707 if (TREE_TYPE (decl) == error_mark_node)
1708 return GS_ERROR;
1709
1710 if ((TREE_CODE (decl) == TYPE_DECL
1711 || VAR_P (decl))
1712 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1713 {
1714 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1715 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1716 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1717 }
1718
1719 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1720 in case its size expressions contain problematic nodes like CALL_EXPR. */
1721 if (TREE_CODE (decl) == TYPE_DECL
1722 && DECL_ORIGINAL_TYPE (decl)
1723 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1724 {
1725 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1726 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1727 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1728 }
1729
1730 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1731 {
1732 tree init = DECL_INITIAL (decl);
1733 bool is_vla = false;
1734
1735 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1736 || (!TREE_STATIC (decl)
1737 && flag_stack_check == GENERIC_STACK_CHECK
1738 && compare_tree_int (DECL_SIZE_UNIT (decl),
1739 STACK_CHECK_MAX_VAR_SIZE) > 0))
1740 {
1741 gimplify_vla_decl (decl, seq_p);
1742 is_vla = true;
1743 }
1744
1745 if (asan_poisoned_variables
1746 && !is_vla
1747 && TREE_ADDRESSABLE (decl)
1748 && !TREE_STATIC (decl)
1749 && !DECL_HAS_VALUE_EXPR_P (decl)
1750 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1751 && dbg_cnt (asan_use_after_scope)
1752 && !gimplify_omp_ctxp)
1753 {
1754 asan_poisoned_variables->add (decl);
1755 asan_poison_variable (decl, false, seq_p);
1756 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1757 gimplify_ctxp->live_switch_vars->add (decl);
1758 }
1759
1760 /* Some front ends do not explicitly declare all anonymous
1761 artificial variables. We compensate here by declaring the
1762 variables, though it would be better if the front ends would
1763 explicitly declare them. */
1764 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1765 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1766 gimple_add_tmp_var (decl);
1767
1768 if (init && init != error_mark_node)
1769 {
1770 if (!TREE_STATIC (decl))
1771 {
1772 DECL_INITIAL (decl) = NULL_TREE;
1773 init = build2 (INIT_EXPR, void_type_node, decl, init);
1774 gimplify_and_add (init, seq_p);
1775 ggc_free (init);
1776 }
1777 else
1778 /* We must still examine initializers for static variables
1779 as they may contain a label address. */
1780 walk_tree (&init, force_labels_r, NULL, NULL);
1781 }
1782 }
1783
1784 return GS_ALL_DONE;
1785 }
1786
1787 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1788 and replacing the LOOP_EXPR with goto, but if the loop contains an
1789 EXIT_EXPR, we need to append a label for it to jump to. */
1790
1791 static enum gimplify_status
1792 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1793 {
1794 tree saved_label = gimplify_ctxp->exit_label;
1795 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1796
1797 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1798
1799 gimplify_ctxp->exit_label = NULL_TREE;
1800
1801 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1802
1803 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1804
1805 if (gimplify_ctxp->exit_label)
1806 gimplify_seq_add_stmt (pre_p,
1807 gimple_build_label (gimplify_ctxp->exit_label));
1808
1809 gimplify_ctxp->exit_label = saved_label;
1810
1811 *expr_p = NULL;
1812 return GS_ALL_DONE;
1813 }
1814
1815 /* Gimplify a statement list onto a sequence. These may be created either
1816 by an enlightened front-end, or by shortcut_cond_expr. */
1817
1818 static enum gimplify_status
1819 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1820 {
1821 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1822
1823 tree_stmt_iterator i = tsi_start (*expr_p);
1824
1825 while (!tsi_end_p (i))
1826 {
1827 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1828 tsi_delink (&i);
1829 }
1830
1831 if (temp)
1832 {
1833 *expr_p = temp;
1834 return GS_OK;
1835 }
1836
1837 return GS_ALL_DONE;
1838 }
1839
1840 /* Callback for walk_gimple_seq. */
1841
1842 static tree
1843 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1844 struct walk_stmt_info *wi)
1845 {
1846 gimple *stmt = gsi_stmt (*gsi_p);
1847
1848 *handled_ops_p = true;
1849 switch (gimple_code (stmt))
1850 {
1851 case GIMPLE_TRY:
1852 /* A compiler-generated cleanup or a user-written try block.
1853 If it's empty, don't dive into it--that would result in
1854 worse location info. */
1855 if (gimple_try_eval (stmt) == NULL)
1856 {
1857 wi->info = stmt;
1858 return integer_zero_node;
1859 }
1860 /* Fall through. */
1861 case GIMPLE_BIND:
1862 case GIMPLE_CATCH:
1863 case GIMPLE_EH_FILTER:
1864 case GIMPLE_TRANSACTION:
1865 /* Walk the sub-statements. */
1866 *handled_ops_p = false;
1867 break;
1868
1869 case GIMPLE_DEBUG:
1870 /* Ignore these. We may generate them before declarations that
1871 are never executed. If there's something to warn about,
1872 there will be non-debug stmts too, and we'll catch those. */
1873 break;
1874
1875 case GIMPLE_CALL:
1876 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1877 {
1878 *handled_ops_p = false;
1879 break;
1880 }
1881 /* Fall through. */
1882 default:
1883 /* Save the first "real" statement (not a decl/lexical scope/...). */
1884 wi->info = stmt;
1885 return integer_zero_node;
1886 }
1887 return NULL_TREE;
1888 }
1889
1890 /* Possibly warn about unreachable statements between switch's controlling
1891 expression and the first case. SEQ is the body of a switch expression. */
1892
1893 static void
1894 maybe_warn_switch_unreachable (gimple_seq seq)
1895 {
1896 if (!warn_switch_unreachable
1897 /* This warning doesn't play well with Fortran when optimizations
1898 are on. */
1899 || lang_GNU_Fortran ()
1900 || seq == NULL)
1901 return;
1902
1903 struct walk_stmt_info wi;
1904 memset (&wi, 0, sizeof (wi));
1905 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1906 gimple *stmt = (gimple *) wi.info;
1907
1908 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1909 {
1910 if (gimple_code (stmt) == GIMPLE_GOTO
1911 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1912 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1913 /* Don't warn for compiler-generated gotos. These occur
1914 in Duff's devices, for example. */;
1915 else
1916 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1917 "statement will never be executed");
1918 }
1919 }
1920
1921
1922 /* A label entry that pairs label and a location. */
1923 struct label_entry
1924 {
1925 tree label;
1926 location_t loc;
1927 };
1928
1929 /* Find LABEL in vector of label entries VEC. */
1930
1931 static struct label_entry *
1932 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1933 {
1934 unsigned int i;
1935 struct label_entry *l;
1936
1937 FOR_EACH_VEC_ELT (*vec, i, l)
1938 if (l->label == label)
1939 return l;
1940 return NULL;
1941 }
1942
1943 /* Return true if LABEL, a LABEL_DECL, represents a case label
1944 in a vector of labels CASES. */
1945
1946 static bool
1947 case_label_p (const vec<tree> *cases, tree label)
1948 {
1949 unsigned int i;
1950 tree l;
1951
1952 FOR_EACH_VEC_ELT (*cases, i, l)
1953 if (CASE_LABEL (l) == label)
1954 return true;
1955 return false;
1956 }
1957
1958 /* Find the last nondebug statement in a scope STMT. */
1959
1960 static gimple *
1961 last_stmt_in_scope (gimple *stmt)
1962 {
1963 if (!stmt)
1964 return NULL;
1965
1966 switch (gimple_code (stmt))
1967 {
1968 case GIMPLE_BIND:
1969 {
1970 gbind *bind = as_a <gbind *> (stmt);
1971 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1972 return last_stmt_in_scope (stmt);
1973 }
1974
1975 case GIMPLE_TRY:
1976 {
1977 gtry *try_stmt = as_a <gtry *> (stmt);
1978 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1979 gimple *last_eval = last_stmt_in_scope (stmt);
1980 if (gimple_stmt_may_fallthru (last_eval)
1981 && (last_eval == NULL
1982 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1983 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1984 {
1985 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1986 return last_stmt_in_scope (stmt);
1987 }
1988 else
1989 return last_eval;
1990 }
1991
1992 case GIMPLE_DEBUG:
1993 gcc_unreachable ();
1994
1995 default:
1996 return stmt;
1997 }
1998 }
1999
2000 /* Collect interesting labels in LABELS and return the statement preceding
2001 another case label, or a user-defined label. Store a location useful
2002 to give warnings at *PREVLOC (usually the location of the returned
2003 statement or of its surrounding scope). */
2004
2005 static gimple *
2006 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
2007 auto_vec <struct label_entry> *labels,
2008 location_t *prevloc)
2009 {
2010 gimple *prev = NULL;
2011
2012 *prevloc = UNKNOWN_LOCATION;
2013 do
2014 {
2015 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
2016 {
2017 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
2018 which starts on a GIMPLE_SWITCH and ends with a break label.
2019 Handle that as a single statement that can fall through. */
2020 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
2021 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
2022 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
2023 if (last
2024 && gimple_code (first) == GIMPLE_SWITCH
2025 && gimple_code (last) == GIMPLE_LABEL)
2026 {
2027 tree label = gimple_label_label (as_a <glabel *> (last));
2028 if (SWITCH_BREAK_LABEL_P (label))
2029 {
2030 prev = bind;
2031 gsi_next (gsi_p);
2032 continue;
2033 }
2034 }
2035 }
2036 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
2037 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2038 {
2039 /* Nested scope. Only look at the last statement of
2040 the innermost scope. */
2041 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2042 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2043 if (last)
2044 {
2045 prev = last;
2046 /* It might be a label without a location. Use the
2047 location of the scope then. */
2048 if (!gimple_has_location (prev))
2049 *prevloc = bind_loc;
2050 }
2051 gsi_next (gsi_p);
2052 continue;
2053 }
2054
2055 /* Ifs are tricky. */
2056 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2057 {
2058 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2059 tree false_lab = gimple_cond_false_label (cond_stmt);
2060 location_t if_loc = gimple_location (cond_stmt);
2061
2062 /* If we have e.g.
2063 if (i > 1) goto <D.2259>; else goto D;
2064 we can't do much with the else-branch. */
2065 if (!DECL_ARTIFICIAL (false_lab))
2066 break;
2067
2068 /* Go on until the false label, then one step back. */
2069 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2070 {
2071 gimple *stmt = gsi_stmt (*gsi_p);
2072 if (gimple_code (stmt) == GIMPLE_LABEL
2073 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2074 break;
2075 }
2076
2077 /* Not found? Oops. */
2078 if (gsi_end_p (*gsi_p))
2079 break;
2080
2081 struct label_entry l = { false_lab, if_loc };
2082 labels->safe_push (l);
2083
2084 /* Go to the last statement of the then branch. */
2085 gsi_prev (gsi_p);
2086
2087 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2088 <D.1759>:
2089 <stmt>;
2090 goto <D.1761>;
2091 <D.1760>:
2092 */
2093 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2094 && !gimple_has_location (gsi_stmt (*gsi_p)))
2095 {
2096 /* Look at the statement before, it might be
2097 attribute fallthrough, in which case don't warn. */
2098 gsi_prev (gsi_p);
2099 bool fallthru_before_dest
2100 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2101 gsi_next (gsi_p);
2102 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2103 if (!fallthru_before_dest)
2104 {
2105 struct label_entry l = { goto_dest, if_loc };
2106 labels->safe_push (l);
2107 }
2108 }
2109 /* And move back. */
2110 gsi_next (gsi_p);
2111 }
2112
2113 /* Remember the last statement. Skip labels that are of no interest
2114 to us. */
2115 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2116 {
2117 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2118 if (find_label_entry (labels, label))
2119 prev = gsi_stmt (*gsi_p);
2120 }
2121 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2122 ;
2123 else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
2124 ;
2125 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2126 prev = gsi_stmt (*gsi_p);
2127 gsi_next (gsi_p);
2128 }
2129 while (!gsi_end_p (*gsi_p)
2130 /* Stop if we find a case or a user-defined label. */
2131 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2132 || !gimple_has_location (gsi_stmt (*gsi_p))));
2133
2134 if (prev && gimple_has_location (prev))
2135 *prevloc = gimple_location (prev);
2136 return prev;
2137 }
2138
2139 /* Return true if the switch fallthough warning should occur. LABEL is
2140 the label statement that we're falling through to. */
2141
2142 static bool
2143 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2144 {
2145 gimple_stmt_iterator gsi = *gsi_p;
2146
2147 /* Don't warn if the label is marked with a "falls through" comment. */
2148 if (FALLTHROUGH_LABEL_P (label))
2149 return false;
2150
2151 /* Don't warn for non-case labels followed by a statement:
2152 case 0:
2153 foo ();
2154 label:
2155 bar ();
2156 as these are likely intentional. */
2157 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2158 {
2159 tree l;
2160 while (!gsi_end_p (gsi)
2161 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2162 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2163 && !case_label_p (&gimplify_ctxp->case_labels, l))
2164 gsi_next_nondebug (&gsi);
2165 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2166 return false;
2167 }
2168
2169 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2170 immediately breaks. */
2171 gsi = *gsi_p;
2172
2173 /* Skip all immediately following labels. */
2174 while (!gsi_end_p (gsi)
2175 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2176 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2177 gsi_next_nondebug (&gsi);
2178
2179 /* { ... something; default:; } */
2180 if (gsi_end_p (gsi)
2181 /* { ... something; default: break; } or
2182 { ... something; default: goto L; } */
2183 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2184 /* { ... something; default: return; } */
2185 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2186 return false;
2187
2188 return true;
2189 }
2190
2191 /* Callback for walk_gimple_seq. */
2192
2193 static tree
2194 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2195 struct walk_stmt_info *)
2196 {
2197 gimple *stmt = gsi_stmt (*gsi_p);
2198
2199 *handled_ops_p = true;
2200 switch (gimple_code (stmt))
2201 {
2202 case GIMPLE_TRY:
2203 case GIMPLE_BIND:
2204 case GIMPLE_CATCH:
2205 case GIMPLE_EH_FILTER:
2206 case GIMPLE_TRANSACTION:
2207 /* Walk the sub-statements. */
2208 *handled_ops_p = false;
2209 break;
2210
2211 /* Find a sequence of form:
2212
2213 GIMPLE_LABEL
2214 [...]
2215 <may fallthru stmt>
2216 GIMPLE_LABEL
2217
2218 and possibly warn. */
2219 case GIMPLE_LABEL:
2220 {
2221 /* Found a label. Skip all immediately following labels. */
2222 while (!gsi_end_p (*gsi_p)
2223 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2224 gsi_next_nondebug (gsi_p);
2225
2226 /* There might be no more statements. */
2227 if (gsi_end_p (*gsi_p))
2228 return integer_zero_node;
2229
2230 /* Vector of labels that fall through. */
2231 auto_vec <struct label_entry> labels;
2232 location_t prevloc;
2233 gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2234
2235 /* There might be no more statements. */
2236 if (gsi_end_p (*gsi_p))
2237 return integer_zero_node;
2238
2239 gimple *next = gsi_stmt (*gsi_p);
2240 tree label;
2241 /* If what follows is a label, then we may have a fallthrough. */
2242 if (gimple_code (next) == GIMPLE_LABEL
2243 && gimple_has_location (next)
2244 && (label = gimple_label_label (as_a <glabel *> (next)))
2245 && prev != NULL)
2246 {
2247 struct label_entry *l;
2248 bool warned_p = false;
2249 auto_diagnostic_group d;
2250 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2251 /* Quiet. */;
2252 else if (gimple_code (prev) == GIMPLE_LABEL
2253 && (label = gimple_label_label (as_a <glabel *> (prev)))
2254 && (l = find_label_entry (&labels, label)))
2255 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2256 "this statement may fall through");
2257 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2258 /* Try to be clever and don't warn when the statement
2259 can't actually fall through. */
2260 && gimple_stmt_may_fallthru (prev)
2261 && prevloc != UNKNOWN_LOCATION)
2262 warned_p = warning_at (prevloc,
2263 OPT_Wimplicit_fallthrough_,
2264 "this statement may fall through");
2265 if (warned_p)
2266 inform (gimple_location (next), "here");
2267
2268 /* Mark this label as processed so as to prevent multiple
2269 warnings in nested switches. */
2270 FALLTHROUGH_LABEL_P (label) = true;
2271
2272 /* So that next warn_implicit_fallthrough_r will start looking for
2273 a new sequence starting with this label. */
2274 gsi_prev (gsi_p);
2275 }
2276 }
2277 break;
2278 default:
2279 break;
2280 }
2281 return NULL_TREE;
2282 }
2283
2284 /* Warn when a switch case falls through. */
2285
2286 static void
2287 maybe_warn_implicit_fallthrough (gimple_seq seq)
2288 {
2289 if (!warn_implicit_fallthrough)
2290 return;
2291
2292 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2293 if (!(lang_GNU_C ()
2294 || lang_GNU_CXX ()
2295 || lang_GNU_OBJC ()))
2296 return;
2297
2298 struct walk_stmt_info wi;
2299 memset (&wi, 0, sizeof (wi));
2300 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2301 }
2302
2303 /* Callback for walk_gimple_seq. */
2304
2305 static tree
2306 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2307 struct walk_stmt_info *wi)
2308 {
2309 gimple *stmt = gsi_stmt (*gsi_p);
2310
2311 *handled_ops_p = true;
2312 switch (gimple_code (stmt))
2313 {
2314 case GIMPLE_TRY:
2315 case GIMPLE_BIND:
2316 case GIMPLE_CATCH:
2317 case GIMPLE_EH_FILTER:
2318 case GIMPLE_TRANSACTION:
2319 /* Walk the sub-statements. */
2320 *handled_ops_p = false;
2321 break;
2322 case GIMPLE_CALL:
2323 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2324 {
2325 gsi_remove (gsi_p, true);
2326 if (gsi_end_p (*gsi_p))
2327 {
2328 *static_cast<location_t *>(wi->info) = gimple_location (stmt);
2329 return integer_zero_node;
2330 }
2331
2332 bool found = false;
2333 location_t loc = gimple_location (stmt);
2334
2335 gimple_stmt_iterator gsi2 = *gsi_p;
2336 stmt = gsi_stmt (gsi2);
2337 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2338 {
2339 /* Go on until the artificial label. */
2340 tree goto_dest = gimple_goto_dest (stmt);
2341 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2342 {
2343 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2344 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2345 == goto_dest)
2346 break;
2347 }
2348
2349 /* Not found? Stop. */
2350 if (gsi_end_p (gsi2))
2351 break;
2352
2353 /* Look one past it. */
2354 gsi_next (&gsi2);
2355 }
2356
2357 /* We're looking for a case label or default label here. */
2358 while (!gsi_end_p (gsi2))
2359 {
2360 stmt = gsi_stmt (gsi2);
2361 if (gimple_code (stmt) == GIMPLE_LABEL)
2362 {
2363 tree label = gimple_label_label (as_a <glabel *> (stmt));
2364 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2365 {
2366 found = true;
2367 break;
2368 }
2369 }
2370 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2371 ;
2372 else if (!is_gimple_debug (stmt))
2373 /* Anything else is not expected. */
2374 break;
2375 gsi_next (&gsi2);
2376 }
2377 if (!found)
2378 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2379 "a case label or default label");
2380 }
2381 break;
2382 default:
2383 break;
2384 }
2385 return NULL_TREE;
2386 }
2387
2388 /* Expand all FALLTHROUGH () calls in SEQ. */
2389
2390 static void
2391 expand_FALLTHROUGH (gimple_seq *seq_p)
2392 {
2393 struct walk_stmt_info wi;
2394 location_t loc;
2395 memset (&wi, 0, sizeof (wi));
2396 wi.info = (void *) &loc;
2397 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2398 if (wi.callback_result == integer_zero_node)
2399 /* We've found [[fallthrough]]; at the end of a switch, which the C++
2400 standard says is ill-formed; see [dcl.attr.fallthrough]. */
2401 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2402 "a case label or default label");
2403 }
2404
2405 \f
2406 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2407 branch to. */
2408
2409 static enum gimplify_status
2410 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2411 {
2412 tree switch_expr = *expr_p;
2413 gimple_seq switch_body_seq = NULL;
2414 enum gimplify_status ret;
2415 tree index_type = TREE_TYPE (switch_expr);
2416 if (index_type == NULL_TREE)
2417 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2418
2419 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2420 fb_rvalue);
2421 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2422 return ret;
2423
2424 if (SWITCH_BODY (switch_expr))
2425 {
2426 vec<tree> labels;
2427 vec<tree> saved_labels;
2428 hash_set<tree> *saved_live_switch_vars = NULL;
2429 tree default_case = NULL_TREE;
2430 gswitch *switch_stmt;
2431
2432 /* Save old labels, get new ones from body, then restore the old
2433 labels. Save all the things from the switch body to append after. */
2434 saved_labels = gimplify_ctxp->case_labels;
2435 gimplify_ctxp->case_labels.create (8);
2436
2437 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2438 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2439 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2440 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2441 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2442 else
2443 gimplify_ctxp->live_switch_vars = NULL;
2444
2445 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2446 gimplify_ctxp->in_switch_expr = true;
2447
2448 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2449
2450 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2451 maybe_warn_switch_unreachable (switch_body_seq);
2452 maybe_warn_implicit_fallthrough (switch_body_seq);
2453 /* Only do this for the outermost GIMPLE_SWITCH. */
2454 if (!gimplify_ctxp->in_switch_expr)
2455 expand_FALLTHROUGH (&switch_body_seq);
2456
2457 labels = gimplify_ctxp->case_labels;
2458 gimplify_ctxp->case_labels = saved_labels;
2459
2460 if (gimplify_ctxp->live_switch_vars)
2461 {
2462 gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
2463 delete gimplify_ctxp->live_switch_vars;
2464 }
2465 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2466
2467 preprocess_case_label_vec_for_gimple (labels, index_type,
2468 &default_case);
2469
2470 bool add_bind = false;
2471 if (!default_case)
2472 {
2473 glabel *new_default;
2474
2475 default_case
2476 = build_case_label (NULL_TREE, NULL_TREE,
2477 create_artificial_label (UNKNOWN_LOCATION));
2478 if (old_in_switch_expr)
2479 {
2480 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2481 add_bind = true;
2482 }
2483 new_default = gimple_build_label (CASE_LABEL (default_case));
2484 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2485 }
2486 else if (old_in_switch_expr)
2487 {
2488 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2489 if (last && gimple_code (last) == GIMPLE_LABEL)
2490 {
2491 tree label = gimple_label_label (as_a <glabel *> (last));
2492 if (SWITCH_BREAK_LABEL_P (label))
2493 add_bind = true;
2494 }
2495 }
2496
2497 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2498 default_case, labels);
2499 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2500 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2501 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2502 so that we can easily find the start and end of the switch
2503 statement. */
2504 if (add_bind)
2505 {
2506 gimple_seq bind_body = NULL;
2507 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2508 gimple_seq_add_seq (&bind_body, switch_body_seq);
2509 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2510 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2511 gimplify_seq_add_stmt (pre_p, bind);
2512 }
2513 else
2514 {
2515 gimplify_seq_add_stmt (pre_p, switch_stmt);
2516 gimplify_seq_add_seq (pre_p, switch_body_seq);
2517 }
2518 labels.release ();
2519 }
2520 else
2521 gcc_unreachable ();
2522
2523 return GS_ALL_DONE;
2524 }
2525
2526 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2527
2528 static enum gimplify_status
2529 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2530 {
2531 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2532 == current_function_decl);
2533
2534 tree label = LABEL_EXPR_LABEL (*expr_p);
2535 glabel *label_stmt = gimple_build_label (label);
2536 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2537 gimplify_seq_add_stmt (pre_p, label_stmt);
2538
2539 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2540 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2541 NOT_TAKEN));
2542 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2543 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2544 TAKEN));
2545
2546 return GS_ALL_DONE;
2547 }
2548
2549 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2550
2551 static enum gimplify_status
2552 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2553 {
2554 struct gimplify_ctx *ctxp;
2555 glabel *label_stmt;
2556
2557 /* Invalid programs can play Duff's Device type games with, for example,
2558 #pragma omp parallel. At least in the C front end, we don't
2559 detect such invalid branches until after gimplification, in the
2560 diagnose_omp_blocks pass. */
2561 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2562 if (ctxp->case_labels.exists ())
2563 break;
2564
2565 tree label = CASE_LABEL (*expr_p);
2566 label_stmt = gimple_build_label (label);
2567 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2568 ctxp->case_labels.safe_push (*expr_p);
2569 gimplify_seq_add_stmt (pre_p, label_stmt);
2570
2571 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2572 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2573 NOT_TAKEN));
2574 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2575 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2576 TAKEN));
2577
2578 return GS_ALL_DONE;
2579 }
2580
2581 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2582 if necessary. */
2583
2584 tree
2585 build_and_jump (tree *label_p)
2586 {
2587 if (label_p == NULL)
2588 /* If there's nowhere to jump, just fall through. */
2589 return NULL_TREE;
2590
2591 if (*label_p == NULL_TREE)
2592 {
2593 tree label = create_artificial_label (UNKNOWN_LOCATION);
2594 *label_p = label;
2595 }
2596
2597 return build1 (GOTO_EXPR, void_type_node, *label_p);
2598 }
2599
2600 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2601 This also involves building a label to jump to and communicating it to
2602 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2603
2604 static enum gimplify_status
2605 gimplify_exit_expr (tree *expr_p)
2606 {
2607 tree cond = TREE_OPERAND (*expr_p, 0);
2608 tree expr;
2609
2610 expr = build_and_jump (&gimplify_ctxp->exit_label);
2611 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2612 *expr_p = expr;
2613
2614 return GS_OK;
2615 }
2616
2617 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2618 different from its canonical type, wrap the whole thing inside a
2619 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2620 type.
2621
2622 The canonical type of a COMPONENT_REF is the type of the field being
2623 referenced--unless the field is a bit-field which can be read directly
2624 in a smaller mode, in which case the canonical type is the
2625 sign-appropriate type corresponding to that mode. */
2626
2627 static void
2628 canonicalize_component_ref (tree *expr_p)
2629 {
2630 tree expr = *expr_p;
2631 tree type;
2632
2633 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2634
2635 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2636 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2637 else
2638 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2639
2640 /* One could argue that all the stuff below is not necessary for
2641 the non-bitfield case and declare it a FE error if type
2642 adjustment would be needed. */
2643 if (TREE_TYPE (expr) != type)
2644 {
2645 #ifdef ENABLE_TYPES_CHECKING
2646 tree old_type = TREE_TYPE (expr);
2647 #endif
2648 int type_quals;
2649
2650 /* We need to preserve qualifiers and propagate them from
2651 operand 0. */
2652 type_quals = TYPE_QUALS (type)
2653 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2654 if (TYPE_QUALS (type) != type_quals)
2655 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2656
2657 /* Set the type of the COMPONENT_REF to the underlying type. */
2658 TREE_TYPE (expr) = type;
2659
2660 #ifdef ENABLE_TYPES_CHECKING
2661 /* It is now a FE error, if the conversion from the canonical
2662 type to the original expression type is not useless. */
2663 gcc_assert (useless_type_conversion_p (old_type, type));
2664 #endif
2665 }
2666 }
2667
2668 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2669 to foo, embed that change in the ADDR_EXPR by converting
2670 T array[U];
2671 (T *)&array
2672 ==>
2673 &array[L]
2674 where L is the lower bound. For simplicity, only do this for constant
2675 lower bound.
2676 The constraint is that the type of &array[L] is trivially convertible
2677 to T *. */
2678
2679 static void
2680 canonicalize_addr_expr (tree *expr_p)
2681 {
2682 tree expr = *expr_p;
2683 tree addr_expr = TREE_OPERAND (expr, 0);
2684 tree datype, ddatype, pddatype;
2685
2686 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2687 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2688 || TREE_CODE (addr_expr) != ADDR_EXPR)
2689 return;
2690
2691 /* The addr_expr type should be a pointer to an array. */
2692 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2693 if (TREE_CODE (datype) != ARRAY_TYPE)
2694 return;
2695
2696 /* The pointer to element type shall be trivially convertible to
2697 the expression pointer type. */
2698 ddatype = TREE_TYPE (datype);
2699 pddatype = build_pointer_type (ddatype);
2700 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2701 pddatype))
2702 return;
2703
2704 /* The lower bound and element sizes must be constant. */
2705 if (!TYPE_SIZE_UNIT (ddatype)
2706 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2707 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2708 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2709 return;
2710
2711 /* All checks succeeded. Build a new node to merge the cast. */
2712 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2713 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2714 NULL_TREE, NULL_TREE);
2715 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2716
2717 /* We can have stripped a required restrict qualifier above. */
2718 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2719 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2720 }
2721
2722 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2723 underneath as appropriate. */
2724
2725 static enum gimplify_status
2726 gimplify_conversion (tree *expr_p)
2727 {
2728 location_t loc = EXPR_LOCATION (*expr_p);
2729 gcc_assert (CONVERT_EXPR_P (*expr_p));
2730
2731 /* Then strip away all but the outermost conversion. */
2732 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2733
2734 /* And remove the outermost conversion if it's useless. */
2735 if (tree_ssa_useless_type_conversion (*expr_p))
2736 *expr_p = TREE_OPERAND (*expr_p, 0);
2737
2738 /* If we still have a conversion at the toplevel,
2739 then canonicalize some constructs. */
2740 if (CONVERT_EXPR_P (*expr_p))
2741 {
2742 tree sub = TREE_OPERAND (*expr_p, 0);
2743
2744 /* If a NOP conversion is changing the type of a COMPONENT_REF
2745 expression, then canonicalize its type now in order to expose more
2746 redundant conversions. */
2747 if (TREE_CODE (sub) == COMPONENT_REF)
2748 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2749
2750 /* If a NOP conversion is changing a pointer to array of foo
2751 to a pointer to foo, embed that change in the ADDR_EXPR. */
2752 else if (TREE_CODE (sub) == ADDR_EXPR)
2753 canonicalize_addr_expr (expr_p);
2754 }
2755
2756 /* If we have a conversion to a non-register type force the
2757 use of a VIEW_CONVERT_EXPR instead. */
2758 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2759 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2760 TREE_OPERAND (*expr_p, 0));
2761
2762 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2763 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2764 TREE_SET_CODE (*expr_p, NOP_EXPR);
2765
2766 return GS_OK;
2767 }
2768
2769 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2770 DECL_VALUE_EXPR, and it's worth re-examining things. */
2771
2772 static enum gimplify_status
2773 gimplify_var_or_parm_decl (tree *expr_p)
2774 {
2775 tree decl = *expr_p;
2776
2777 /* ??? If this is a local variable, and it has not been seen in any
2778 outer BIND_EXPR, then it's probably the result of a duplicate
2779 declaration, for which we've already issued an error. It would
2780 be really nice if the front end wouldn't leak these at all.
2781 Currently the only known culprit is C++ destructors, as seen
2782 in g++.old-deja/g++.jason/binding.C. */
2783 if (VAR_P (decl)
2784 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2785 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2786 && decl_function_context (decl) == current_function_decl)
2787 {
2788 gcc_assert (seen_error ());
2789 return GS_ERROR;
2790 }
2791
2792 /* When within an OMP context, notice uses of variables. */
2793 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2794 return GS_ALL_DONE;
2795
2796 /* If the decl is an alias for another expression, substitute it now. */
2797 if (DECL_HAS_VALUE_EXPR_P (decl))
2798 {
2799 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
2800 return GS_OK;
2801 }
2802
2803 return GS_ALL_DONE;
2804 }
2805
2806 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2807
2808 static void
2809 recalculate_side_effects (tree t)
2810 {
2811 enum tree_code code = TREE_CODE (t);
2812 int len = TREE_OPERAND_LENGTH (t);
2813 int i;
2814
2815 switch (TREE_CODE_CLASS (code))
2816 {
2817 case tcc_expression:
2818 switch (code)
2819 {
2820 case INIT_EXPR:
2821 case MODIFY_EXPR:
2822 case VA_ARG_EXPR:
2823 case PREDECREMENT_EXPR:
2824 case PREINCREMENT_EXPR:
2825 case POSTDECREMENT_EXPR:
2826 case POSTINCREMENT_EXPR:
2827 /* All of these have side-effects, no matter what their
2828 operands are. */
2829 return;
2830
2831 default:
2832 break;
2833 }
2834 /* Fall through. */
2835
2836 case tcc_comparison: /* a comparison expression */
2837 case tcc_unary: /* a unary arithmetic expression */
2838 case tcc_binary: /* a binary arithmetic expression */
2839 case tcc_reference: /* a reference */
2840 case tcc_vl_exp: /* a function call */
2841 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2842 for (i = 0; i < len; ++i)
2843 {
2844 tree op = TREE_OPERAND (t, i);
2845 if (op && TREE_SIDE_EFFECTS (op))
2846 TREE_SIDE_EFFECTS (t) = 1;
2847 }
2848 break;
2849
2850 case tcc_constant:
2851 /* No side-effects. */
2852 return;
2853
2854 default:
2855 gcc_unreachable ();
2856 }
2857 }
2858
2859 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2860 node *EXPR_P.
2861
2862 compound_lval
2863 : min_lval '[' val ']'
2864 | min_lval '.' ID
2865 | compound_lval '[' val ']'
2866 | compound_lval '.' ID
2867
2868 This is not part of the original SIMPLE definition, which separates
2869 array and member references, but it seems reasonable to handle them
2870 together. Also, this way we don't run into problems with union
2871 aliasing; gcc requires that for accesses through a union to alias, the
2872 union reference must be explicit, which was not always the case when we
2873 were splitting up array and member refs.
2874
2875 PRE_P points to the sequence where side effects that must happen before
2876 *EXPR_P should be stored.
2877
2878 POST_P points to the sequence where side effects that must happen after
2879 *EXPR_P should be stored. */
2880
2881 static enum gimplify_status
2882 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2883 fallback_t fallback)
2884 {
2885 tree *p;
2886 enum gimplify_status ret = GS_ALL_DONE, tret;
2887 int i;
2888 location_t loc = EXPR_LOCATION (*expr_p);
2889 tree expr = *expr_p;
2890
2891 /* Create a stack of the subexpressions so later we can walk them in
2892 order from inner to outer. */
2893 auto_vec<tree, 10> expr_stack;
2894
2895 /* We can handle anything that get_inner_reference can deal with. */
2896 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2897 {
2898 restart:
2899 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2900 if (TREE_CODE (*p) == INDIRECT_REF)
2901 *p = fold_indirect_ref_loc (loc, *p);
2902
2903 if (handled_component_p (*p))
2904 ;
2905 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2906 additional COMPONENT_REFs. */
2907 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2908 && gimplify_var_or_parm_decl (p) == GS_OK)
2909 goto restart;
2910 else
2911 break;
2912
2913 expr_stack.safe_push (*p);
2914 }
2915
2916 gcc_assert (expr_stack.length ());
2917
2918 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2919 walked through and P points to the innermost expression.
2920
2921 Java requires that we elaborated nodes in source order. That
2922 means we must gimplify the inner expression followed by each of
2923 the indices, in order. But we can't gimplify the inner
2924 expression until we deal with any variable bounds, sizes, or
2925 positions in order to deal with PLACEHOLDER_EXPRs.
2926
2927 So we do this in three steps. First we deal with the annotations
2928 for any variables in the components, then we gimplify the base,
2929 then we gimplify any indices, from left to right. */
2930 for (i = expr_stack.length () - 1; i >= 0; i--)
2931 {
2932 tree t = expr_stack[i];
2933
2934 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2935 {
2936 /* Gimplify the low bound and element type size and put them into
2937 the ARRAY_REF. If these values are set, they have already been
2938 gimplified. */
2939 if (TREE_OPERAND (t, 2) == NULL_TREE)
2940 {
2941 tree low = unshare_expr (array_ref_low_bound (t));
2942 if (!is_gimple_min_invariant (low))
2943 {
2944 TREE_OPERAND (t, 2) = low;
2945 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2946 post_p, is_gimple_reg,
2947 fb_rvalue);
2948 ret = MIN (ret, tret);
2949 }
2950 }
2951 else
2952 {
2953 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2954 is_gimple_reg, fb_rvalue);
2955 ret = MIN (ret, tret);
2956 }
2957
2958 if (TREE_OPERAND (t, 3) == NULL_TREE)
2959 {
2960 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2961 tree elmt_size = unshare_expr (array_ref_element_size (t));
2962 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2963
2964 /* Divide the element size by the alignment of the element
2965 type (above). */
2966 elmt_size
2967 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2968
2969 if (!is_gimple_min_invariant (elmt_size))
2970 {
2971 TREE_OPERAND (t, 3) = elmt_size;
2972 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2973 post_p, is_gimple_reg,
2974 fb_rvalue);
2975 ret = MIN (ret, tret);
2976 }
2977 }
2978 else
2979 {
2980 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2981 is_gimple_reg, fb_rvalue);
2982 ret = MIN (ret, tret);
2983 }
2984 }
2985 else if (TREE_CODE (t) == COMPONENT_REF)
2986 {
2987 /* Set the field offset into T and gimplify it. */
2988 if (TREE_OPERAND (t, 2) == NULL_TREE)
2989 {
2990 tree offset = unshare_expr (component_ref_field_offset (t));
2991 tree field = TREE_OPERAND (t, 1);
2992 tree factor
2993 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2994
2995 /* Divide the offset by its alignment. */
2996 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2997
2998 if (!is_gimple_min_invariant (offset))
2999 {
3000 TREE_OPERAND (t, 2) = offset;
3001 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
3002 post_p, is_gimple_reg,
3003 fb_rvalue);
3004 ret = MIN (ret, tret);
3005 }
3006 }
3007 else
3008 {
3009 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3010 is_gimple_reg, fb_rvalue);
3011 ret = MIN (ret, tret);
3012 }
3013 }
3014 }
3015
3016 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
3017 so as to match the min_lval predicate. Failure to do so may result
3018 in the creation of large aggregate temporaries. */
3019 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3020 fallback | fb_lvalue);
3021 ret = MIN (ret, tret);
3022
3023 /* And finally, the indices and operands of ARRAY_REF. During this
3024 loop we also remove any useless conversions. */
3025 for (; expr_stack.length () > 0; )
3026 {
3027 tree t = expr_stack.pop ();
3028
3029 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3030 {
3031 /* Gimplify the dimension. */
3032 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
3033 {
3034 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3035 is_gimple_val, fb_rvalue);
3036 ret = MIN (ret, tret);
3037 }
3038 }
3039
3040 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
3041
3042 /* The innermost expression P may have originally had
3043 TREE_SIDE_EFFECTS set which would have caused all the outer
3044 expressions in *EXPR_P leading to P to also have had
3045 TREE_SIDE_EFFECTS set. */
3046 recalculate_side_effects (t);
3047 }
3048
3049 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3050 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3051 {
3052 canonicalize_component_ref (expr_p);
3053 }
3054
3055 expr_stack.release ();
3056
3057 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3058
3059 return ret;
3060 }
3061
3062 /* Gimplify the self modifying expression pointed to by EXPR_P
3063 (++, --, +=, -=).
3064
3065 PRE_P points to the list where side effects that must happen before
3066 *EXPR_P should be stored.
3067
3068 POST_P points to the list where side effects that must happen after
3069 *EXPR_P should be stored.
3070
3071 WANT_VALUE is nonzero iff we want to use the value of this expression
3072 in another expression.
3073
3074 ARITH_TYPE is the type the computation should be performed in. */
3075
3076 enum gimplify_status
3077 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3078 bool want_value, tree arith_type)
3079 {
3080 enum tree_code code;
3081 tree lhs, lvalue, rhs, t1;
3082 gimple_seq post = NULL, *orig_post_p = post_p;
3083 bool postfix;
3084 enum tree_code arith_code;
3085 enum gimplify_status ret;
3086 location_t loc = EXPR_LOCATION (*expr_p);
3087
3088 code = TREE_CODE (*expr_p);
3089
3090 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3091 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3092
3093 /* Prefix or postfix? */
3094 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3095 /* Faster to treat as prefix if result is not used. */
3096 postfix = want_value;
3097 else
3098 postfix = false;
3099
3100 /* For postfix, make sure the inner expression's post side effects
3101 are executed after side effects from this expression. */
3102 if (postfix)
3103 post_p = &post;
3104
3105 /* Add or subtract? */
3106 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3107 arith_code = PLUS_EXPR;
3108 else
3109 arith_code = MINUS_EXPR;
3110
3111 /* Gimplify the LHS into a GIMPLE lvalue. */
3112 lvalue = TREE_OPERAND (*expr_p, 0);
3113 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3114 if (ret == GS_ERROR)
3115 return ret;
3116
3117 /* Extract the operands to the arithmetic operation. */
3118 lhs = lvalue;
3119 rhs = TREE_OPERAND (*expr_p, 1);
3120
3121 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3122 that as the result value and in the postqueue operation. */
3123 if (postfix)
3124 {
3125 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3126 if (ret == GS_ERROR)
3127 return ret;
3128
3129 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3130 }
3131
3132 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3133 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3134 {
3135 rhs = convert_to_ptrofftype_loc (loc, rhs);
3136 if (arith_code == MINUS_EXPR)
3137 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3138 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3139 }
3140 else
3141 t1 = fold_convert (TREE_TYPE (*expr_p),
3142 fold_build2 (arith_code, arith_type,
3143 fold_convert (arith_type, lhs),
3144 fold_convert (arith_type, rhs)));
3145
3146 if (postfix)
3147 {
3148 gimplify_assign (lvalue, t1, pre_p);
3149 gimplify_seq_add_seq (orig_post_p, post);
3150 *expr_p = lhs;
3151 return GS_ALL_DONE;
3152 }
3153 else
3154 {
3155 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3156 return GS_OK;
3157 }
3158 }
3159
3160 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3161
3162 static void
3163 maybe_with_size_expr (tree *expr_p)
3164 {
3165 tree expr = *expr_p;
3166 tree type = TREE_TYPE (expr);
3167 tree size;
3168
3169 /* If we've already wrapped this or the type is error_mark_node, we can't do
3170 anything. */
3171 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3172 || type == error_mark_node)
3173 return;
3174
3175 /* If the size isn't known or is a constant, we have nothing to do. */
3176 size = TYPE_SIZE_UNIT (type);
3177 if (!size || poly_int_tree_p (size))
3178 return;
3179
3180 /* Otherwise, make a WITH_SIZE_EXPR. */
3181 size = unshare_expr (size);
3182 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3183 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3184 }
3185
3186 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3187 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3188 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3189 gimplified to an SSA name. */
3190
3191 enum gimplify_status
3192 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3193 bool allow_ssa)
3194 {
3195 bool (*test) (tree);
3196 fallback_t fb;
3197
3198 /* In general, we allow lvalues for function arguments to avoid
3199 extra overhead of copying large aggregates out of even larger
3200 aggregates into temporaries only to copy the temporaries to
3201 the argument list. Make optimizers happy by pulling out to
3202 temporaries those types that fit in registers. */
3203 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3204 test = is_gimple_val, fb = fb_rvalue;
3205 else
3206 {
3207 test = is_gimple_lvalue, fb = fb_either;
3208 /* Also strip a TARGET_EXPR that would force an extra copy. */
3209 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3210 {
3211 tree init = TARGET_EXPR_INITIAL (*arg_p);
3212 if (init
3213 && !VOID_TYPE_P (TREE_TYPE (init)))
3214 *arg_p = init;
3215 }
3216 }
3217
3218 /* If this is a variable sized type, we must remember the size. */
3219 maybe_with_size_expr (arg_p);
3220
3221 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3222 /* Make sure arguments have the same location as the function call
3223 itself. */
3224 protected_set_expr_location (*arg_p, call_location);
3225
3226 /* There is a sequence point before a function call. Side effects in
3227 the argument list must occur before the actual call. So, when
3228 gimplifying arguments, force gimplify_expr to use an internal
3229 post queue which is then appended to the end of PRE_P. */
3230 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3231 }
3232
3233 /* Don't fold inside offloading or taskreg regions: it can break code by
3234 adding decl references that weren't in the source. We'll do it during
3235 omplower pass instead. */
3236
3237 static bool
3238 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3239 {
3240 struct gimplify_omp_ctx *ctx;
3241 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3242 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3243 return false;
3244 else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3245 return false;
3246 /* Delay folding of builtins until the IL is in consistent state
3247 so the diagnostic machinery can do a better job. */
3248 if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3249 return false;
3250 return fold_stmt (gsi);
3251 }
3252
3253 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3254 WANT_VALUE is true if the result of the call is desired. */
3255
3256 static enum gimplify_status
3257 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3258 {
3259 tree fndecl, parms, p, fnptrtype;
3260 enum gimplify_status ret;
3261 int i, nargs;
3262 gcall *call;
3263 bool builtin_va_start_p = false;
3264 location_t loc = EXPR_LOCATION (*expr_p);
3265
3266 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3267
3268 /* For reliable diagnostics during inlining, it is necessary that
3269 every call_expr be annotated with file and line. */
3270 if (! EXPR_HAS_LOCATION (*expr_p))
3271 SET_EXPR_LOCATION (*expr_p, input_location);
3272
3273 /* Gimplify internal functions created in the FEs. */
3274 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3275 {
3276 if (want_value)
3277 return GS_ALL_DONE;
3278
3279 nargs = call_expr_nargs (*expr_p);
3280 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3281 auto_vec<tree> vargs (nargs);
3282
3283 for (i = 0; i < nargs; i++)
3284 {
3285 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3286 EXPR_LOCATION (*expr_p));
3287 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3288 }
3289
3290 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3291 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3292 gimplify_seq_add_stmt (pre_p, call);
3293 return GS_ALL_DONE;
3294 }
3295
3296 /* This may be a call to a builtin function.
3297
3298 Builtin function calls may be transformed into different
3299 (and more efficient) builtin function calls under certain
3300 circumstances. Unfortunately, gimplification can muck things
3301 up enough that the builtin expanders are not aware that certain
3302 transformations are still valid.
3303
3304 So we attempt transformation/gimplification of the call before
3305 we gimplify the CALL_EXPR. At this time we do not manage to
3306 transform all calls in the same manner as the expanders do, but
3307 we do transform most of them. */
3308 fndecl = get_callee_fndecl (*expr_p);
3309 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
3310 switch (DECL_FUNCTION_CODE (fndecl))
3311 {
3312 CASE_BUILT_IN_ALLOCA:
3313 /* If the call has been built for a variable-sized object, then we
3314 want to restore the stack level when the enclosing BIND_EXPR is
3315 exited to reclaim the allocated space; otherwise, we precisely
3316 need to do the opposite and preserve the latest stack level. */
3317 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3318 gimplify_ctxp->save_stack = true;
3319 else
3320 gimplify_ctxp->keep_stack = true;
3321 break;
3322
3323 case BUILT_IN_VA_START:
3324 {
3325 builtin_va_start_p = TRUE;
3326 if (call_expr_nargs (*expr_p) < 2)
3327 {
3328 error ("too few arguments to function %<va_start%>");
3329 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3330 return GS_OK;
3331 }
3332
3333 if (fold_builtin_next_arg (*expr_p, true))
3334 {
3335 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3336 return GS_OK;
3337 }
3338 break;
3339 }
3340
3341 case BUILT_IN_EH_RETURN:
3342 cfun->calls_eh_return = true;
3343 break;
3344
3345 default:
3346 ;
3347 }
3348 if (fndecl && fndecl_built_in_p (fndecl))
3349 {
3350 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3351 if (new_tree && new_tree != *expr_p)
3352 {
3353 /* There was a transformation of this call which computes the
3354 same value, but in a more efficient way. Return and try
3355 again. */
3356 *expr_p = new_tree;
3357 return GS_OK;
3358 }
3359 }
3360
3361 /* Remember the original function pointer type. */
3362 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3363
3364 /* There is a sequence point before the call, so any side effects in
3365 the calling expression must occur before the actual call. Force
3366 gimplify_expr to use an internal post queue. */
3367 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3368 is_gimple_call_addr, fb_rvalue);
3369
3370 nargs = call_expr_nargs (*expr_p);
3371
3372 /* Get argument types for verification. */
3373 fndecl = get_callee_fndecl (*expr_p);
3374 parms = NULL_TREE;
3375 if (fndecl)
3376 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3377 else
3378 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3379
3380 if (fndecl && DECL_ARGUMENTS (fndecl))
3381 p = DECL_ARGUMENTS (fndecl);
3382 else if (parms)
3383 p = parms;
3384 else
3385 p = NULL_TREE;
3386 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3387 ;
3388
3389 /* If the last argument is __builtin_va_arg_pack () and it is not
3390 passed as a named argument, decrease the number of CALL_EXPR
3391 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3392 if (!p
3393 && i < nargs
3394 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3395 {
3396 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3397 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3398
3399 if (last_arg_fndecl
3400 && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
3401 {
3402 tree call = *expr_p;
3403
3404 --nargs;
3405 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3406 CALL_EXPR_FN (call),
3407 nargs, CALL_EXPR_ARGP (call));
3408
3409 /* Copy all CALL_EXPR flags, location and block, except
3410 CALL_EXPR_VA_ARG_PACK flag. */
3411 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3412 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3413 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3414 = CALL_EXPR_RETURN_SLOT_OPT (call);
3415 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3416 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3417
3418 /* Set CALL_EXPR_VA_ARG_PACK. */
3419 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3420 }
3421 }
3422
3423 /* If the call returns twice then after building the CFG the call
3424 argument computations will no longer dominate the call because
3425 we add an abnormal incoming edge to the call. So do not use SSA
3426 vars there. */
3427 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3428
3429 /* Gimplify the function arguments. */
3430 if (nargs > 0)
3431 {
3432 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3433 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3434 PUSH_ARGS_REVERSED ? i-- : i++)
3435 {
3436 enum gimplify_status t;
3437
3438 /* Avoid gimplifying the second argument to va_start, which needs to
3439 be the plain PARM_DECL. */
3440 if ((i != 1) || !builtin_va_start_p)
3441 {
3442 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3443 EXPR_LOCATION (*expr_p), ! returns_twice);
3444
3445 if (t == GS_ERROR)
3446 ret = GS_ERROR;
3447 }
3448 }
3449 }
3450
3451 /* Gimplify the static chain. */
3452 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3453 {
3454 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3455 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3456 else
3457 {
3458 enum gimplify_status t;
3459 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3460 EXPR_LOCATION (*expr_p), ! returns_twice);
3461 if (t == GS_ERROR)
3462 ret = GS_ERROR;
3463 }
3464 }
3465
3466 /* Verify the function result. */
3467 if (want_value && fndecl
3468 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3469 {
3470 error_at (loc, "using result of function returning %<void%>");
3471 ret = GS_ERROR;
3472 }
3473
3474 /* Try this again in case gimplification exposed something. */
3475 if (ret != GS_ERROR)
3476 {
3477 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3478
3479 if (new_tree && new_tree != *expr_p)
3480 {
3481 /* There was a transformation of this call which computes the
3482 same value, but in a more efficient way. Return and try
3483 again. */
3484 *expr_p = new_tree;
3485 return GS_OK;
3486 }
3487 }
3488 else
3489 {
3490 *expr_p = error_mark_node;
3491 return GS_ERROR;
3492 }
3493
3494 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3495 decl. This allows us to eliminate redundant or useless
3496 calls to "const" functions. */
3497 if (TREE_CODE (*expr_p) == CALL_EXPR)
3498 {
3499 int flags = call_expr_flags (*expr_p);
3500 if (flags & (ECF_CONST | ECF_PURE)
3501 /* An infinite loop is considered a side effect. */
3502 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3503 TREE_SIDE_EFFECTS (*expr_p) = 0;
3504 }
3505
3506 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3507 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3508 form and delegate the creation of a GIMPLE_CALL to
3509 gimplify_modify_expr. This is always possible because when
3510 WANT_VALUE is true, the caller wants the result of this call into
3511 a temporary, which means that we will emit an INIT_EXPR in
3512 internal_get_tmp_var which will then be handled by
3513 gimplify_modify_expr. */
3514 if (!want_value)
3515 {
3516 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3517 have to do is replicate it as a GIMPLE_CALL tuple. */
3518 gimple_stmt_iterator gsi;
3519 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3520 notice_special_calls (call);
3521 gimplify_seq_add_stmt (pre_p, call);
3522 gsi = gsi_last (*pre_p);
3523 maybe_fold_stmt (&gsi);
3524 *expr_p = NULL_TREE;
3525 }
3526 else
3527 /* Remember the original function type. */
3528 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3529 CALL_EXPR_FN (*expr_p));
3530
3531 return ret;
3532 }
3533
3534 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3535 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3536
3537 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3538 condition is true or false, respectively. If null, we should generate
3539 our own to skip over the evaluation of this specific expression.
3540
3541 LOCUS is the source location of the COND_EXPR.
3542
3543 This function is the tree equivalent of do_jump.
3544
3545 shortcut_cond_r should only be called by shortcut_cond_expr. */
3546
3547 static tree
3548 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3549 location_t locus)
3550 {
3551 tree local_label = NULL_TREE;
3552 tree t, expr = NULL;
3553
3554 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3555 retain the shortcut semantics. Just insert the gotos here;
3556 shortcut_cond_expr will append the real blocks later. */
3557 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3558 {
3559 location_t new_locus;
3560
3561 /* Turn if (a && b) into
3562
3563 if (a); else goto no;
3564 if (b) goto yes; else goto no;
3565 (no:) */
3566
3567 if (false_label_p == NULL)
3568 false_label_p = &local_label;
3569
3570 /* Keep the original source location on the first 'if'. */
3571 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3572 append_to_statement_list (t, &expr);
3573
3574 /* Set the source location of the && on the second 'if'. */
3575 new_locus = rexpr_location (pred, locus);
3576 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3577 new_locus);
3578 append_to_statement_list (t, &expr);
3579 }
3580 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3581 {
3582 location_t new_locus;
3583
3584 /* Turn if (a || b) into
3585
3586 if (a) goto yes;
3587 if (b) goto yes; else goto no;
3588 (yes:) */
3589
3590 if (true_label_p == NULL)
3591 true_label_p = &local_label;
3592
3593 /* Keep the original source location on the first 'if'. */
3594 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3595 append_to_statement_list (t, &expr);
3596
3597 /* Set the source location of the || on the second 'if'. */
3598 new_locus = rexpr_location (pred, locus);
3599 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3600 new_locus);
3601 append_to_statement_list (t, &expr);
3602 }
3603 else if (TREE_CODE (pred) == COND_EXPR
3604 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3605 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3606 {
3607 location_t new_locus;
3608
3609 /* As long as we're messing with gotos, turn if (a ? b : c) into
3610 if (a)
3611 if (b) goto yes; else goto no;
3612 else
3613 if (c) goto yes; else goto no;
3614
3615 Don't do this if one of the arms has void type, which can happen
3616 in C++ when the arm is throw. */
3617
3618 /* Keep the original source location on the first 'if'. Set the source
3619 location of the ? on the second 'if'. */
3620 new_locus = rexpr_location (pred, locus);
3621 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3622 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3623 false_label_p, locus),
3624 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3625 false_label_p, new_locus));
3626 }
3627 else
3628 {
3629 expr = build3 (COND_EXPR, void_type_node, pred,
3630 build_and_jump (true_label_p),
3631 build_and_jump (false_label_p));
3632 SET_EXPR_LOCATION (expr, locus);
3633 }
3634
3635 if (local_label)
3636 {
3637 t = build1 (LABEL_EXPR, void_type_node, local_label);
3638 append_to_statement_list (t, &expr);
3639 }
3640
3641 return expr;
3642 }
3643
3644 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3645 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3646 statement, if it is the last one. Otherwise, return NULL. */
3647
3648 static tree
3649 find_goto (tree expr)
3650 {
3651 if (!expr)
3652 return NULL_TREE;
3653
3654 if (TREE_CODE (expr) == GOTO_EXPR)
3655 return expr;
3656
3657 if (TREE_CODE (expr) != STATEMENT_LIST)
3658 return NULL_TREE;
3659
3660 tree_stmt_iterator i = tsi_start (expr);
3661
3662 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3663 tsi_next (&i);
3664
3665 if (!tsi_one_before_end_p (i))
3666 return NULL_TREE;
3667
3668 return find_goto (tsi_stmt (i));
3669 }
3670
3671 /* Same as find_goto, except that it returns NULL if the destination
3672 is not a LABEL_DECL. */
3673
3674 static inline tree
3675 find_goto_label (tree expr)
3676 {
3677 tree dest = find_goto (expr);
3678 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3679 return dest;
3680 return NULL_TREE;
3681 }
3682
3683 /* Given a conditional expression EXPR with short-circuit boolean
3684 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3685 predicate apart into the equivalent sequence of conditionals. */
3686
3687 static tree
3688 shortcut_cond_expr (tree expr)
3689 {
3690 tree pred = TREE_OPERAND (expr, 0);
3691 tree then_ = TREE_OPERAND (expr, 1);
3692 tree else_ = TREE_OPERAND (expr, 2);
3693 tree true_label, false_label, end_label, t;
3694 tree *true_label_p;
3695 tree *false_label_p;
3696 bool emit_end, emit_false, jump_over_else;
3697 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3698 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3699
3700 /* First do simple transformations. */
3701 if (!else_se)
3702 {
3703 /* If there is no 'else', turn
3704 if (a && b) then c
3705 into
3706 if (a) if (b) then c. */
3707 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3708 {
3709 /* Keep the original source location on the first 'if'. */
3710 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3711 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3712 /* Set the source location of the && on the second 'if'. */
3713 if (rexpr_has_location (pred))
3714 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3715 then_ = shortcut_cond_expr (expr);
3716 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3717 pred = TREE_OPERAND (pred, 0);
3718 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3719 SET_EXPR_LOCATION (expr, locus);
3720 }
3721 }
3722
3723 if (!then_se)
3724 {
3725 /* If there is no 'then', turn
3726 if (a || b); else d
3727 into
3728 if (a); else if (b); else d. */
3729 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3730 {
3731 /* Keep the original source location on the first 'if'. */
3732 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3733 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3734 /* Set the source location of the || on the second 'if'. */
3735 if (rexpr_has_location (pred))
3736 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3737 else_ = shortcut_cond_expr (expr);
3738 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3739 pred = TREE_OPERAND (pred, 0);
3740 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3741 SET_EXPR_LOCATION (expr, locus);
3742 }
3743 }
3744
3745 /* If we're done, great. */
3746 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3747 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3748 return expr;
3749
3750 /* Otherwise we need to mess with gotos. Change
3751 if (a) c; else d;
3752 to
3753 if (a); else goto no;
3754 c; goto end;
3755 no: d; end:
3756 and recursively gimplify the condition. */
3757
3758 true_label = false_label = end_label = NULL_TREE;
3759
3760 /* If our arms just jump somewhere, hijack those labels so we don't
3761 generate jumps to jumps. */
3762
3763 if (tree then_goto = find_goto_label (then_))
3764 {
3765 true_label = GOTO_DESTINATION (then_goto);
3766 then_ = NULL;
3767 then_se = false;
3768 }
3769
3770 if (tree else_goto = find_goto_label (else_))
3771 {
3772 false_label = GOTO_DESTINATION (else_goto);
3773 else_ = NULL;
3774 else_se = false;
3775 }
3776
3777 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3778 if (true_label)
3779 true_label_p = &true_label;
3780 else
3781 true_label_p = NULL;
3782
3783 /* The 'else' branch also needs a label if it contains interesting code. */
3784 if (false_label || else_se)
3785 false_label_p = &false_label;
3786 else
3787 false_label_p = NULL;
3788
3789 /* If there was nothing else in our arms, just forward the label(s). */
3790 if (!then_se && !else_se)
3791 return shortcut_cond_r (pred, true_label_p, false_label_p,
3792 EXPR_LOC_OR_LOC (expr, input_location));
3793
3794 /* If our last subexpression already has a terminal label, reuse it. */
3795 if (else_se)
3796 t = expr_last (else_);
3797 else if (then_se)
3798 t = expr_last (then_);
3799 else
3800 t = NULL;
3801 if (t && TREE_CODE (t) == LABEL_EXPR)
3802 end_label = LABEL_EXPR_LABEL (t);
3803
3804 /* If we don't care about jumping to the 'else' branch, jump to the end
3805 if the condition is false. */
3806 if (!false_label_p)
3807 false_label_p = &end_label;
3808
3809 /* We only want to emit these labels if we aren't hijacking them. */
3810 emit_end = (end_label == NULL_TREE);
3811 emit_false = (false_label == NULL_TREE);
3812
3813 /* We only emit the jump over the else clause if we have to--if the
3814 then clause may fall through. Otherwise we can wind up with a
3815 useless jump and a useless label at the end of gimplified code,
3816 which will cause us to think that this conditional as a whole
3817 falls through even if it doesn't. If we then inline a function
3818 which ends with such a condition, that can cause us to issue an
3819 inappropriate warning about control reaching the end of a
3820 non-void function. */
3821 jump_over_else = block_may_fallthru (then_);
3822
3823 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3824 EXPR_LOC_OR_LOC (expr, input_location));
3825
3826 expr = NULL;
3827 append_to_statement_list (pred, &expr);
3828
3829 append_to_statement_list (then_, &expr);
3830 if (else_se)
3831 {
3832 if (jump_over_else)
3833 {
3834 tree last = expr_last (expr);
3835 t = build_and_jump (&end_label);
3836 if (rexpr_has_location (last))
3837 SET_EXPR_LOCATION (t, rexpr_location (last));
3838 append_to_statement_list (t, &expr);
3839 }
3840 if (emit_false)
3841 {
3842 t = build1 (LABEL_EXPR, void_type_node, false_label);
3843 append_to_statement_list (t, &expr);
3844 }
3845 append_to_statement_list (else_, &expr);
3846 }
3847 if (emit_end && end_label)
3848 {
3849 t = build1 (LABEL_EXPR, void_type_node, end_label);
3850 append_to_statement_list (t, &expr);
3851 }
3852
3853 return expr;
3854 }
3855
3856 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3857
3858 tree
3859 gimple_boolify (tree expr)
3860 {
3861 tree type = TREE_TYPE (expr);
3862 location_t loc = EXPR_LOCATION (expr);
3863
3864 if (TREE_CODE (expr) == NE_EXPR
3865 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3866 && integer_zerop (TREE_OPERAND (expr, 1)))
3867 {
3868 tree call = TREE_OPERAND (expr, 0);
3869 tree fn = get_callee_fndecl (call);
3870
3871 /* For __builtin_expect ((long) (x), y) recurse into x as well
3872 if x is truth_value_p. */
3873 if (fn
3874 && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
3875 && call_expr_nargs (call) == 2)
3876 {
3877 tree arg = CALL_EXPR_ARG (call, 0);
3878 if (arg)
3879 {
3880 if (TREE_CODE (arg) == NOP_EXPR
3881 && TREE_TYPE (arg) == TREE_TYPE (call))
3882 arg = TREE_OPERAND (arg, 0);
3883 if (truth_value_p (TREE_CODE (arg)))
3884 {
3885 arg = gimple_boolify (arg);
3886 CALL_EXPR_ARG (call, 0)
3887 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3888 }
3889 }
3890 }
3891 }
3892
3893 switch (TREE_CODE (expr))
3894 {
3895 case TRUTH_AND_EXPR:
3896 case TRUTH_OR_EXPR:
3897 case TRUTH_XOR_EXPR:
3898 case TRUTH_ANDIF_EXPR:
3899 case TRUTH_ORIF_EXPR:
3900 /* Also boolify the arguments of truth exprs. */
3901 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3902 /* FALLTHRU */
3903
3904 case TRUTH_NOT_EXPR:
3905 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3906
3907 /* These expressions always produce boolean results. */
3908 if (TREE_CODE (type) != BOOLEAN_TYPE)
3909 TREE_TYPE (expr) = boolean_type_node;
3910 return expr;
3911
3912 case ANNOTATE_EXPR:
3913 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3914 {
3915 case annot_expr_ivdep_kind:
3916 case annot_expr_unroll_kind:
3917 case annot_expr_no_vector_kind:
3918 case annot_expr_vector_kind:
3919 case annot_expr_parallel_kind:
3920 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3921 if (TREE_CODE (type) != BOOLEAN_TYPE)
3922 TREE_TYPE (expr) = boolean_type_node;
3923 return expr;
3924 default:
3925 gcc_unreachable ();
3926 }
3927
3928 default:
3929 if (COMPARISON_CLASS_P (expr))
3930 {
3931 /* There expressions always prduce boolean results. */
3932 if (TREE_CODE (type) != BOOLEAN_TYPE)
3933 TREE_TYPE (expr) = boolean_type_node;
3934 return expr;
3935 }
3936 /* Other expressions that get here must have boolean values, but
3937 might need to be converted to the appropriate mode. */
3938 if (TREE_CODE (type) == BOOLEAN_TYPE)
3939 return expr;
3940 return fold_convert_loc (loc, boolean_type_node, expr);
3941 }
3942 }
3943
3944 /* Given a conditional expression *EXPR_P without side effects, gimplify
3945 its operands. New statements are inserted to PRE_P. */
3946
3947 static enum gimplify_status
3948 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3949 {
3950 tree expr = *expr_p, cond;
3951 enum gimplify_status ret, tret;
3952 enum tree_code code;
3953
3954 cond = gimple_boolify (COND_EXPR_COND (expr));
3955
3956 /* We need to handle && and || specially, as their gimplification
3957 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3958 code = TREE_CODE (cond);
3959 if (code == TRUTH_ANDIF_EXPR)
3960 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3961 else if (code == TRUTH_ORIF_EXPR)
3962 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3963 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3964 COND_EXPR_COND (*expr_p) = cond;
3965
3966 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3967 is_gimple_val, fb_rvalue);
3968 ret = MIN (ret, tret);
3969 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3970 is_gimple_val, fb_rvalue);
3971
3972 return MIN (ret, tret);
3973 }
3974
3975 /* Return true if evaluating EXPR could trap.
3976 EXPR is GENERIC, while tree_could_trap_p can be called
3977 only on GIMPLE. */
3978
3979 bool
3980 generic_expr_could_trap_p (tree expr)
3981 {
3982 unsigned i, n;
3983
3984 if (!expr || is_gimple_val (expr))
3985 return false;
3986
3987 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3988 return true;
3989
3990 n = TREE_OPERAND_LENGTH (expr);
3991 for (i = 0; i < n; i++)
3992 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3993 return true;
3994
3995 return false;
3996 }
3997
3998 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3999 into
4000
4001 if (p) if (p)
4002 t1 = a; a;
4003 else or else
4004 t1 = b; b;
4005 t1;
4006
4007 The second form is used when *EXPR_P is of type void.
4008
4009 PRE_P points to the list where side effects that must happen before
4010 *EXPR_P should be stored. */
4011
4012 static enum gimplify_status
4013 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4014 {
4015 tree expr = *expr_p;
4016 tree type = TREE_TYPE (expr);
4017 location_t loc = EXPR_LOCATION (expr);
4018 tree tmp, arm1, arm2;
4019 enum gimplify_status ret;
4020 tree label_true, label_false, label_cont;
4021 bool have_then_clause_p, have_else_clause_p;
4022 gcond *cond_stmt;
4023 enum tree_code pred_code;
4024 gimple_seq seq = NULL;
4025
4026 /* If this COND_EXPR has a value, copy the values into a temporary within
4027 the arms. */
4028 if (!VOID_TYPE_P (type))
4029 {
4030 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
4031 tree result;
4032
4033 /* If either an rvalue is ok or we do not require an lvalue, create the
4034 temporary. But we cannot do that if the type is addressable. */
4035 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
4036 && !TREE_ADDRESSABLE (type))
4037 {
4038 if (gimplify_ctxp->allow_rhs_cond_expr
4039 /* If either branch has side effects or could trap, it can't be
4040 evaluated unconditionally. */
4041 && !TREE_SIDE_EFFECTS (then_)
4042 && !generic_expr_could_trap_p (then_)
4043 && !TREE_SIDE_EFFECTS (else_)
4044 && !generic_expr_could_trap_p (else_))
4045 return gimplify_pure_cond_expr (expr_p, pre_p);
4046
4047 tmp = create_tmp_var (type, "iftmp");
4048 result = tmp;
4049 }
4050
4051 /* Otherwise, only create and copy references to the values. */
4052 else
4053 {
4054 type = build_pointer_type (type);
4055
4056 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4057 then_ = build_fold_addr_expr_loc (loc, then_);
4058
4059 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4060 else_ = build_fold_addr_expr_loc (loc, else_);
4061
4062 expr
4063 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4064
4065 tmp = create_tmp_var (type, "iftmp");
4066 result = build_simple_mem_ref_loc (loc, tmp);
4067 }
4068
4069 /* Build the new then clause, `tmp = then_;'. But don't build the
4070 assignment if the value is void; in C++ it can be if it's a throw. */
4071 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4072 TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
4073
4074 /* Similarly, build the new else clause, `tmp = else_;'. */
4075 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4076 TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
4077
4078 TREE_TYPE (expr) = void_type_node;
4079 recalculate_side_effects (expr);
4080
4081 /* Move the COND_EXPR to the prequeue. */
4082 gimplify_stmt (&expr, pre_p);
4083
4084 *expr_p = result;
4085 return GS_ALL_DONE;
4086 }
4087
4088 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4089 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4090 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4091 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4092
4093 /* Make sure the condition has BOOLEAN_TYPE. */
4094 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4095
4096 /* Break apart && and || conditions. */
4097 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4098 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4099 {
4100 expr = shortcut_cond_expr (expr);
4101
4102 if (expr != *expr_p)
4103 {
4104 *expr_p = expr;
4105
4106 /* We can't rely on gimplify_expr to re-gimplify the expanded
4107 form properly, as cleanups might cause the target labels to be
4108 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4109 set up a conditional context. */
4110 gimple_push_condition ();
4111 gimplify_stmt (expr_p, &seq);
4112 gimple_pop_condition (pre_p);
4113 gimple_seq_add_seq (pre_p, seq);
4114
4115 return GS_ALL_DONE;
4116 }
4117 }
4118
4119 /* Now do the normal gimplification. */
4120
4121 /* Gimplify condition. */
4122 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4123 fb_rvalue);
4124 if (ret == GS_ERROR)
4125 return GS_ERROR;
4126 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4127
4128 gimple_push_condition ();
4129
4130 have_then_clause_p = have_else_clause_p = false;
4131 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4132 if (label_true
4133 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4134 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4135 have different locations, otherwise we end up with incorrect
4136 location information on the branches. */
4137 && (optimize
4138 || !EXPR_HAS_LOCATION (expr)
4139 || !rexpr_has_location (label_true)
4140 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4141 {
4142 have_then_clause_p = true;
4143 label_true = GOTO_DESTINATION (label_true);
4144 }
4145 else
4146 label_true = create_artificial_label (UNKNOWN_LOCATION);
4147 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4148 if (label_false
4149 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4150 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4151 have different locations, otherwise we end up with incorrect
4152 location information on the branches. */
4153 && (optimize
4154 || !EXPR_HAS_LOCATION (expr)
4155 || !rexpr_has_location (label_false)
4156 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4157 {
4158 have_else_clause_p = true;
4159 label_false = GOTO_DESTINATION (label_false);
4160 }
4161 else
4162 label_false = create_artificial_label (UNKNOWN_LOCATION);
4163
4164 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4165 &arm2);
4166 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4167 label_false);
4168 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4169 gimplify_seq_add_stmt (&seq, cond_stmt);
4170 gimple_stmt_iterator gsi = gsi_last (seq);
4171 maybe_fold_stmt (&gsi);
4172
4173 label_cont = NULL_TREE;
4174 if (!have_then_clause_p)
4175 {
4176 /* For if (...) {} else { code; } put label_true after
4177 the else block. */
4178 if (TREE_OPERAND (expr, 1) == NULL_TREE
4179 && !have_else_clause_p
4180 && TREE_OPERAND (expr, 2) != NULL_TREE)
4181 label_cont = label_true;
4182 else
4183 {
4184 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4185 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4186 /* For if (...) { code; } else {} or
4187 if (...) { code; } else goto label; or
4188 if (...) { code; return; } else { ... }
4189 label_cont isn't needed. */
4190 if (!have_else_clause_p
4191 && TREE_OPERAND (expr, 2) != NULL_TREE
4192 && gimple_seq_may_fallthru (seq))
4193 {
4194 gimple *g;
4195 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4196
4197 g = gimple_build_goto (label_cont);
4198
4199 /* GIMPLE_COND's are very low level; they have embedded
4200 gotos. This particular embedded goto should not be marked
4201 with the location of the original COND_EXPR, as it would
4202 correspond to the COND_EXPR's condition, not the ELSE or the
4203 THEN arms. To avoid marking it with the wrong location, flag
4204 it as "no location". */
4205 gimple_set_do_not_emit_location (g);
4206
4207 gimplify_seq_add_stmt (&seq, g);
4208 }
4209 }
4210 }
4211 if (!have_else_clause_p)
4212 {
4213 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4214 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4215 }
4216 if (label_cont)
4217 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4218
4219 gimple_pop_condition (pre_p);
4220 gimple_seq_add_seq (pre_p, seq);
4221
4222 if (ret == GS_ERROR)
4223 ; /* Do nothing. */
4224 else if (have_then_clause_p || have_else_clause_p)
4225 ret = GS_ALL_DONE;
4226 else
4227 {
4228 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4229 expr = TREE_OPERAND (expr, 0);
4230 gimplify_stmt (&expr, pre_p);
4231 }
4232
4233 *expr_p = NULL;
4234 return ret;
4235 }
4236
4237 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4238 to be marked addressable.
4239
4240 We cannot rely on such an expression being directly markable if a temporary
4241 has been created by the gimplification. In this case, we create another
4242 temporary and initialize it with a copy, which will become a store after we
4243 mark it addressable. This can happen if the front-end passed us something
4244 that it could not mark addressable yet, like a Fortran pass-by-reference
4245 parameter (int) floatvar. */
4246
4247 static void
4248 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4249 {
4250 while (handled_component_p (*expr_p))
4251 expr_p = &TREE_OPERAND (*expr_p, 0);
4252 if (is_gimple_reg (*expr_p))
4253 {
4254 /* Do not allow an SSA name as the temporary. */
4255 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4256 DECL_GIMPLE_REG_P (var) = 0;
4257 *expr_p = var;
4258 }
4259 }
4260
4261 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4262 a call to __builtin_memcpy. */
4263
4264 static enum gimplify_status
4265 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4266 gimple_seq *seq_p)
4267 {
4268 tree t, to, to_ptr, from, from_ptr;
4269 gcall *gs;
4270 location_t loc = EXPR_LOCATION (*expr_p);
4271
4272 to = TREE_OPERAND (*expr_p, 0);
4273 from = TREE_OPERAND (*expr_p, 1);
4274
4275 /* Mark the RHS addressable. Beware that it may not be possible to do so
4276 directly if a temporary has been created by the gimplification. */
4277 prepare_gimple_addressable (&from, seq_p);
4278
4279 mark_addressable (from);
4280 from_ptr = build_fold_addr_expr_loc (loc, from);
4281 gimplify_arg (&from_ptr, seq_p, loc);
4282
4283 mark_addressable (to);
4284 to_ptr = build_fold_addr_expr_loc (loc, to);
4285 gimplify_arg (&to_ptr, seq_p, loc);
4286
4287 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4288
4289 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4290
4291 if (want_value)
4292 {
4293 /* tmp = memcpy() */
4294 t = create_tmp_var (TREE_TYPE (to_ptr));
4295 gimple_call_set_lhs (gs, t);
4296 gimplify_seq_add_stmt (seq_p, gs);
4297
4298 *expr_p = build_simple_mem_ref (t);
4299 return GS_ALL_DONE;
4300 }
4301
4302 gimplify_seq_add_stmt (seq_p, gs);
4303 *expr_p = NULL;
4304 return GS_ALL_DONE;
4305 }
4306
4307 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4308 a call to __builtin_memset. In this case we know that the RHS is
4309 a CONSTRUCTOR with an empty element list. */
4310
4311 static enum gimplify_status
4312 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4313 gimple_seq *seq_p)
4314 {
4315 tree t, from, to, to_ptr;
4316 gcall *gs;
4317 location_t loc = EXPR_LOCATION (*expr_p);
4318
4319 /* Assert our assumptions, to abort instead of producing wrong code
4320 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4321 not be immediately exposed. */
4322 from = TREE_OPERAND (*expr_p, 1);
4323 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4324 from = TREE_OPERAND (from, 0);
4325
4326 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4327 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4328
4329 /* Now proceed. */
4330 to = TREE_OPERAND (*expr_p, 0);
4331
4332 to_ptr = build_fold_addr_expr_loc (loc, to);
4333 gimplify_arg (&to_ptr, seq_p, loc);
4334 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4335
4336 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4337
4338 if (want_value)
4339 {
4340 /* tmp = memset() */
4341 t = create_tmp_var (TREE_TYPE (to_ptr));
4342 gimple_call_set_lhs (gs, t);
4343 gimplify_seq_add_stmt (seq_p, gs);
4344
4345 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4346 return GS_ALL_DONE;
4347 }
4348
4349 gimplify_seq_add_stmt (seq_p, gs);
4350 *expr_p = NULL;
4351 return GS_ALL_DONE;
4352 }
4353
4354 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4355 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4356 assignment. Return non-null if we detect a potential overlap. */
4357
4358 struct gimplify_init_ctor_preeval_data
4359 {
4360 /* The base decl of the lhs object. May be NULL, in which case we
4361 have to assume the lhs is indirect. */
4362 tree lhs_base_decl;
4363
4364 /* The alias set of the lhs object. */
4365 alias_set_type lhs_alias_set;
4366 };
4367
4368 static tree
4369 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4370 {
4371 struct gimplify_init_ctor_preeval_data *data
4372 = (struct gimplify_init_ctor_preeval_data *) xdata;
4373 tree t = *tp;
4374
4375 /* If we find the base object, obviously we have overlap. */
4376 if (data->lhs_base_decl == t)
4377 return t;
4378
4379 /* If the constructor component is indirect, determine if we have a
4380 potential overlap with the lhs. The only bits of information we
4381 have to go on at this point are addressability and alias sets. */
4382 if ((INDIRECT_REF_P (t)
4383 || TREE_CODE (t) == MEM_REF)
4384 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4385 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4386 return t;
4387
4388 /* If the constructor component is a call, determine if it can hide a
4389 potential overlap with the lhs through an INDIRECT_REF like above.
4390 ??? Ugh - this is completely broken. In fact this whole analysis
4391 doesn't look conservative. */
4392 if (TREE_CODE (t) == CALL_EXPR)
4393 {
4394 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4395
4396 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4397 if (POINTER_TYPE_P (TREE_VALUE (type))
4398 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4399 && alias_sets_conflict_p (data->lhs_alias_set,
4400 get_alias_set
4401 (TREE_TYPE (TREE_VALUE (type)))))
4402 return t;
4403 }
4404
4405 if (IS_TYPE_OR_DECL_P (t))
4406 *walk_subtrees = 0;
4407 return NULL;
4408 }
4409
4410 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4411 force values that overlap with the lhs (as described by *DATA)
4412 into temporaries. */
4413
4414 static void
4415 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4416 struct gimplify_init_ctor_preeval_data *data)
4417 {
4418 enum gimplify_status one;
4419
4420 /* If the value is constant, then there's nothing to pre-evaluate. */
4421 if (TREE_CONSTANT (*expr_p))
4422 {
4423 /* Ensure it does not have side effects, it might contain a reference to
4424 the object we're initializing. */
4425 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4426 return;
4427 }
4428
4429 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4430 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4431 return;
4432
4433 /* Recurse for nested constructors. */
4434 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4435 {
4436 unsigned HOST_WIDE_INT ix;
4437 constructor_elt *ce;
4438 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4439
4440 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4441 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4442
4443 return;
4444 }
4445
4446 /* If this is a variable sized type, we must remember the size. */
4447 maybe_with_size_expr (expr_p);
4448
4449 /* Gimplify the constructor element to something appropriate for the rhs
4450 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4451 the gimplifier will consider this a store to memory. Doing this
4452 gimplification now means that we won't have to deal with complicated
4453 language-specific trees, nor trees like SAVE_EXPR that can induce
4454 exponential search behavior. */
4455 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4456 if (one == GS_ERROR)
4457 {
4458 *expr_p = NULL;
4459 return;
4460 }
4461
4462 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4463 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4464 always be true for all scalars, since is_gimple_mem_rhs insists on a
4465 temporary variable for them. */
4466 if (DECL_P (*expr_p))
4467 return;
4468
4469 /* If this is of variable size, we have no choice but to assume it doesn't
4470 overlap since we can't make a temporary for it. */
4471 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4472 return;
4473
4474 /* Otherwise, we must search for overlap ... */
4475 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4476 return;
4477
4478 /* ... and if found, force the value into a temporary. */
4479 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4480 }
4481
4482 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4483 a RANGE_EXPR in a CONSTRUCTOR for an array.
4484
4485 var = lower;
4486 loop_entry:
4487 object[var] = value;
4488 if (var == upper)
4489 goto loop_exit;
4490 var = var + 1;
4491 goto loop_entry;
4492 loop_exit:
4493
4494 We increment var _after_ the loop exit check because we might otherwise
4495 fail if upper == TYPE_MAX_VALUE (type for upper).
4496
4497 Note that we never have to deal with SAVE_EXPRs here, because this has
4498 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4499
4500 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4501 gimple_seq *, bool);
4502
4503 static void
4504 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4505 tree value, tree array_elt_type,
4506 gimple_seq *pre_p, bool cleared)
4507 {
4508 tree loop_entry_label, loop_exit_label, fall_thru_label;
4509 tree var, var_type, cref, tmp;
4510
4511 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4512 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4513 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4514
4515 /* Create and initialize the index variable. */
4516 var_type = TREE_TYPE (upper);
4517 var = create_tmp_var (var_type);
4518 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4519
4520 /* Add the loop entry label. */
4521 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4522
4523 /* Build the reference. */
4524 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4525 var, NULL_TREE, NULL_TREE);
4526
4527 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4528 the store. Otherwise just assign value to the reference. */
4529
4530 if (TREE_CODE (value) == CONSTRUCTOR)
4531 /* NB we might have to call ourself recursively through
4532 gimplify_init_ctor_eval if the value is a constructor. */
4533 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4534 pre_p, cleared);
4535 else
4536 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4537
4538 /* We exit the loop when the index var is equal to the upper bound. */
4539 gimplify_seq_add_stmt (pre_p,
4540 gimple_build_cond (EQ_EXPR, var, upper,
4541 loop_exit_label, fall_thru_label));
4542
4543 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4544
4545 /* Otherwise, increment the index var... */
4546 tmp = build2 (PLUS_EXPR, var_type, var,
4547 fold_convert (var_type, integer_one_node));
4548 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4549
4550 /* ...and jump back to the loop entry. */
4551 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4552
4553 /* Add the loop exit label. */
4554 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4555 }
4556
4557 /* Return true if FDECL is accessing a field that is zero sized. */
4558
4559 static bool
4560 zero_sized_field_decl (const_tree fdecl)
4561 {
4562 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4563 && integer_zerop (DECL_SIZE (fdecl)))
4564 return true;
4565 return false;
4566 }
4567
4568 /* Return true if TYPE is zero sized. */
4569
4570 static bool
4571 zero_sized_type (const_tree type)
4572 {
4573 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4574 && integer_zerop (TYPE_SIZE (type)))
4575 return true;
4576 return false;
4577 }
4578
4579 /* A subroutine of gimplify_init_constructor. Generate individual
4580 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4581 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4582 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4583 zeroed first. */
4584
4585 static void
4586 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4587 gimple_seq *pre_p, bool cleared)
4588 {
4589 tree array_elt_type = NULL;
4590 unsigned HOST_WIDE_INT ix;
4591 tree purpose, value;
4592
4593 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4594 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4595
4596 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4597 {
4598 tree cref;
4599
4600 /* NULL values are created above for gimplification errors. */
4601 if (value == NULL)
4602 continue;
4603
4604 if (cleared && initializer_zerop (value))
4605 continue;
4606
4607 /* ??? Here's to hoping the front end fills in all of the indices,
4608 so we don't have to figure out what's missing ourselves. */
4609 gcc_assert (purpose);
4610
4611 /* Skip zero-sized fields, unless value has side-effects. This can
4612 happen with calls to functions returning a zero-sized type, which
4613 we shouldn't discard. As a number of downstream passes don't
4614 expect sets of zero-sized fields, we rely on the gimplification of
4615 the MODIFY_EXPR we make below to drop the assignment statement. */
4616 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4617 continue;
4618
4619 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4620 whole range. */
4621 if (TREE_CODE (purpose) == RANGE_EXPR)
4622 {
4623 tree lower = TREE_OPERAND (purpose, 0);
4624 tree upper = TREE_OPERAND (purpose, 1);
4625
4626 /* If the lower bound is equal to upper, just treat it as if
4627 upper was the index. */
4628 if (simple_cst_equal (lower, upper))
4629 purpose = upper;
4630 else
4631 {
4632 gimplify_init_ctor_eval_range (object, lower, upper, value,
4633 array_elt_type, pre_p, cleared);
4634 continue;
4635 }
4636 }
4637
4638 if (array_elt_type)
4639 {
4640 /* Do not use bitsizetype for ARRAY_REF indices. */
4641 if (TYPE_DOMAIN (TREE_TYPE (object)))
4642 purpose
4643 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4644 purpose);
4645 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4646 purpose, NULL_TREE, NULL_TREE);
4647 }
4648 else
4649 {
4650 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4651 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4652 unshare_expr (object), purpose, NULL_TREE);
4653 }
4654
4655 if (TREE_CODE (value) == CONSTRUCTOR
4656 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4657 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4658 pre_p, cleared);
4659 else
4660 {
4661 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4662 gimplify_and_add (init, pre_p);
4663 ggc_free (init);
4664 }
4665 }
4666 }
4667
4668 /* Return the appropriate RHS predicate for this LHS. */
4669
4670 gimple_predicate
4671 rhs_predicate_for (tree lhs)
4672 {
4673 if (is_gimple_reg (lhs))
4674 return is_gimple_reg_rhs_or_call;
4675 else
4676 return is_gimple_mem_rhs_or_call;
4677 }
4678
4679 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4680 before the LHS has been gimplified. */
4681
4682 static gimple_predicate
4683 initial_rhs_predicate_for (tree lhs)
4684 {
4685 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4686 return is_gimple_reg_rhs_or_call;
4687 else
4688 return is_gimple_mem_rhs_or_call;
4689 }
4690
4691 /* Gimplify a C99 compound literal expression. This just means adding
4692 the DECL_EXPR before the current statement and using its anonymous
4693 decl instead. */
4694
4695 static enum gimplify_status
4696 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4697 bool (*gimple_test_f) (tree),
4698 fallback_t fallback)
4699 {
4700 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4701 tree decl = DECL_EXPR_DECL (decl_s);
4702 tree init = DECL_INITIAL (decl);
4703 /* Mark the decl as addressable if the compound literal
4704 expression is addressable now, otherwise it is marked too late
4705 after we gimplify the initialization expression. */
4706 if (TREE_ADDRESSABLE (*expr_p))
4707 TREE_ADDRESSABLE (decl) = 1;
4708 /* Otherwise, if we don't need an lvalue and have a literal directly
4709 substitute it. Check if it matches the gimple predicate, as
4710 otherwise we'd generate a new temporary, and we can as well just
4711 use the decl we already have. */
4712 else if (!TREE_ADDRESSABLE (decl)
4713 && !TREE_THIS_VOLATILE (decl)
4714 && init
4715 && (fallback & fb_lvalue) == 0
4716 && gimple_test_f (init))
4717 {
4718 *expr_p = init;
4719 return GS_OK;
4720 }
4721
4722 /* Preliminarily mark non-addressed complex variables as eligible
4723 for promotion to gimple registers. We'll transform their uses
4724 as we find them. */
4725 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4726 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4727 && !TREE_THIS_VOLATILE (decl)
4728 && !needs_to_live_in_memory (decl))
4729 DECL_GIMPLE_REG_P (decl) = 1;
4730
4731 /* If the decl is not addressable, then it is being used in some
4732 expression or on the right hand side of a statement, and it can
4733 be put into a readonly data section. */
4734 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4735 TREE_READONLY (decl) = 1;
4736
4737 /* This decl isn't mentioned in the enclosing block, so add it to the
4738 list of temps. FIXME it seems a bit of a kludge to say that
4739 anonymous artificial vars aren't pushed, but everything else is. */
4740 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4741 gimple_add_tmp_var (decl);
4742
4743 gimplify_and_add (decl_s, pre_p);
4744 *expr_p = decl;
4745 return GS_OK;
4746 }
4747
4748 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4749 return a new CONSTRUCTOR if something changed. */
4750
4751 static tree
4752 optimize_compound_literals_in_ctor (tree orig_ctor)
4753 {
4754 tree ctor = orig_ctor;
4755 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4756 unsigned int idx, num = vec_safe_length (elts);
4757
4758 for (idx = 0; idx < num; idx++)
4759 {
4760 tree value = (*elts)[idx].value;
4761 tree newval = value;
4762 if (TREE_CODE (value) == CONSTRUCTOR)
4763 newval = optimize_compound_literals_in_ctor (value);
4764 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4765 {
4766 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4767 tree decl = DECL_EXPR_DECL (decl_s);
4768 tree init = DECL_INITIAL (decl);
4769
4770 if (!TREE_ADDRESSABLE (value)
4771 && !TREE_ADDRESSABLE (decl)
4772 && init
4773 && TREE_CODE (init) == CONSTRUCTOR)
4774 newval = optimize_compound_literals_in_ctor (init);
4775 }
4776 if (newval == value)
4777 continue;
4778
4779 if (ctor == orig_ctor)
4780 {
4781 ctor = copy_node (orig_ctor);
4782 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4783 elts = CONSTRUCTOR_ELTS (ctor);
4784 }
4785 (*elts)[idx].value = newval;
4786 }
4787 return ctor;
4788 }
4789
4790 /* A subroutine of gimplify_modify_expr. Break out elements of a
4791 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4792
4793 Note that we still need to clear any elements that don't have explicit
4794 initializers, so if not all elements are initialized we keep the
4795 original MODIFY_EXPR, we just remove all of the constructor elements.
4796
4797 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4798 GS_ERROR if we would have to create a temporary when gimplifying
4799 this constructor. Otherwise, return GS_OK.
4800
4801 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4802
4803 static enum gimplify_status
4804 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4805 bool want_value, bool notify_temp_creation)
4806 {
4807 tree object, ctor, type;
4808 enum gimplify_status ret;
4809 vec<constructor_elt, va_gc> *elts;
4810
4811 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4812
4813 if (!notify_temp_creation)
4814 {
4815 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4816 is_gimple_lvalue, fb_lvalue);
4817 if (ret == GS_ERROR)
4818 return ret;
4819 }
4820
4821 object = TREE_OPERAND (*expr_p, 0);
4822 ctor = TREE_OPERAND (*expr_p, 1)
4823 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4824 type = TREE_TYPE (ctor);
4825 elts = CONSTRUCTOR_ELTS (ctor);
4826 ret = GS_ALL_DONE;
4827
4828 switch (TREE_CODE (type))
4829 {
4830 case RECORD_TYPE:
4831 case UNION_TYPE:
4832 case QUAL_UNION_TYPE:
4833 case ARRAY_TYPE:
4834 {
4835 struct gimplify_init_ctor_preeval_data preeval_data;
4836 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4837 HOST_WIDE_INT num_unique_nonzero_elements;
4838 bool cleared, complete_p, valid_const_initializer;
4839 /* Use readonly data for initializers of this or smaller size
4840 regardless of the num_nonzero_elements / num_unique_nonzero_elements
4841 ratio. */
4842 const HOST_WIDE_INT min_unique_size = 64;
4843 /* If num_nonzero_elements / num_unique_nonzero_elements ratio
4844 is smaller than this, use readonly data. */
4845 const int unique_nonzero_ratio = 8;
4846
4847 /* Aggregate types must lower constructors to initialization of
4848 individual elements. The exception is that a CONSTRUCTOR node
4849 with no elements indicates zero-initialization of the whole. */
4850 if (vec_safe_is_empty (elts))
4851 {
4852 if (notify_temp_creation)
4853 return GS_OK;
4854 break;
4855 }
4856
4857 /* Fetch information about the constructor to direct later processing.
4858 We might want to make static versions of it in various cases, and
4859 can only do so if it known to be a valid constant initializer. */
4860 valid_const_initializer
4861 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4862 &num_unique_nonzero_elements,
4863 &num_ctor_elements, &complete_p);
4864
4865 /* If a const aggregate variable is being initialized, then it
4866 should never be a lose to promote the variable to be static. */
4867 if (valid_const_initializer
4868 && num_nonzero_elements > 1
4869 && TREE_READONLY (object)
4870 && VAR_P (object)
4871 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object))
4872 /* For ctors that have many repeated nonzero elements
4873 represented through RANGE_EXPRs, prefer initializing
4874 those through runtime loops over copies of large amounts
4875 of data from readonly data section. */
4876 && (num_unique_nonzero_elements
4877 > num_nonzero_elements / unique_nonzero_ratio
4878 || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
4879 <= (unsigned HOST_WIDE_INT) min_unique_size)))
4880 {
4881 if (notify_temp_creation)
4882 return GS_ERROR;
4883 DECL_INITIAL (object) = ctor;
4884 TREE_STATIC (object) = 1;
4885 if (!DECL_NAME (object))
4886 DECL_NAME (object) = create_tmp_var_name ("C");
4887 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4888
4889 /* ??? C++ doesn't automatically append a .<number> to the
4890 assembler name, and even when it does, it looks at FE private
4891 data structures to figure out what that number should be,
4892 which are not set for this variable. I suppose this is
4893 important for local statics for inline functions, which aren't
4894 "local" in the object file sense. So in order to get a unique
4895 TU-local symbol, we must invoke the lhd version now. */
4896 lhd_set_decl_assembler_name (object);
4897
4898 *expr_p = NULL_TREE;
4899 break;
4900 }
4901
4902 /* If there are "lots" of initialized elements, even discounting
4903 those that are not address constants (and thus *must* be
4904 computed at runtime), then partition the constructor into
4905 constant and non-constant parts. Block copy the constant
4906 parts in, then generate code for the non-constant parts. */
4907 /* TODO. There's code in cp/typeck.c to do this. */
4908
4909 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4910 /* store_constructor will ignore the clearing of variable-sized
4911 objects. Initializers for such objects must explicitly set
4912 every field that needs to be set. */
4913 cleared = false;
4914 else if (!complete_p)
4915 /* If the constructor isn't complete, clear the whole object
4916 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4917
4918 ??? This ought not to be needed. For any element not present
4919 in the initializer, we should simply set them to zero. Except
4920 we'd need to *find* the elements that are not present, and that
4921 requires trickery to avoid quadratic compile-time behavior in
4922 large cases or excessive memory use in small cases. */
4923 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4924 else if (num_ctor_elements - num_nonzero_elements
4925 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4926 && num_nonzero_elements < num_ctor_elements / 4)
4927 /* If there are "lots" of zeros, it's more efficient to clear
4928 the memory and then set the nonzero elements. */
4929 cleared = true;
4930 else
4931 cleared = false;
4932
4933 /* If there are "lots" of initialized elements, and all of them
4934 are valid address constants, then the entire initializer can
4935 be dropped to memory, and then memcpy'd out. Don't do this
4936 for sparse arrays, though, as it's more efficient to follow
4937 the standard CONSTRUCTOR behavior of memset followed by
4938 individual element initialization. Also don't do this for small
4939 all-zero initializers (which aren't big enough to merit
4940 clearing), and don't try to make bitwise copies of
4941 TREE_ADDRESSABLE types. */
4942
4943 if (valid_const_initializer
4944 && !(cleared || num_nonzero_elements == 0)
4945 && !TREE_ADDRESSABLE (type))
4946 {
4947 HOST_WIDE_INT size = int_size_in_bytes (type);
4948 unsigned int align;
4949
4950 /* ??? We can still get unbounded array types, at least
4951 from the C++ front end. This seems wrong, but attempt
4952 to work around it for now. */
4953 if (size < 0)
4954 {
4955 size = int_size_in_bytes (TREE_TYPE (object));
4956 if (size >= 0)
4957 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4958 }
4959
4960 /* Find the maximum alignment we can assume for the object. */
4961 /* ??? Make use of DECL_OFFSET_ALIGN. */
4962 if (DECL_P (object))
4963 align = DECL_ALIGN (object);
4964 else
4965 align = TYPE_ALIGN (type);
4966
4967 /* Do a block move either if the size is so small as to make
4968 each individual move a sub-unit move on average, or if it
4969 is so large as to make individual moves inefficient. */
4970 if (size > 0
4971 && num_nonzero_elements > 1
4972 /* For ctors that have many repeated nonzero elements
4973 represented through RANGE_EXPRs, prefer initializing
4974 those through runtime loops over copies of large amounts
4975 of data from readonly data section. */
4976 && (num_unique_nonzero_elements
4977 > num_nonzero_elements / unique_nonzero_ratio
4978 || size <= min_unique_size)
4979 && (size < num_nonzero_elements
4980 || !can_move_by_pieces (size, align)))
4981 {
4982 if (notify_temp_creation)
4983 return GS_ERROR;
4984
4985 walk_tree (&ctor, force_labels_r, NULL, NULL);
4986 ctor = tree_output_constant_def (ctor);
4987 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4988 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4989 TREE_OPERAND (*expr_p, 1) = ctor;
4990
4991 /* This is no longer an assignment of a CONSTRUCTOR, but
4992 we still may have processing to do on the LHS. So
4993 pretend we didn't do anything here to let that happen. */
4994 return GS_UNHANDLED;
4995 }
4996 }
4997
4998 /* If the target is volatile, we have non-zero elements and more than
4999 one field to assign, initialize the target from a temporary. */
5000 if (TREE_THIS_VOLATILE (object)
5001 && !TREE_ADDRESSABLE (type)
5002 && num_nonzero_elements > 0
5003 && vec_safe_length (elts) > 1)
5004 {
5005 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
5006 TREE_OPERAND (*expr_p, 0) = temp;
5007 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
5008 *expr_p,
5009 build2 (MODIFY_EXPR, void_type_node,
5010 object, temp));
5011 return GS_OK;
5012 }
5013
5014 if (notify_temp_creation)
5015 return GS_OK;
5016
5017 /* If there are nonzero elements and if needed, pre-evaluate to capture
5018 elements overlapping with the lhs into temporaries. We must do this
5019 before clearing to fetch the values before they are zeroed-out. */
5020 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
5021 {
5022 preeval_data.lhs_base_decl = get_base_address (object);
5023 if (!DECL_P (preeval_data.lhs_base_decl))
5024 preeval_data.lhs_base_decl = NULL;
5025 preeval_data.lhs_alias_set = get_alias_set (object);
5026
5027 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
5028 pre_p, post_p, &preeval_data);
5029 }
5030
5031 bool ctor_has_side_effects_p
5032 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
5033
5034 if (cleared)
5035 {
5036 /* Zap the CONSTRUCTOR element list, which simplifies this case.
5037 Note that we still have to gimplify, in order to handle the
5038 case of variable sized types. Avoid shared tree structures. */
5039 CONSTRUCTOR_ELTS (ctor) = NULL;
5040 TREE_SIDE_EFFECTS (ctor) = 0;
5041 object = unshare_expr (object);
5042 gimplify_stmt (expr_p, pre_p);
5043 }
5044
5045 /* If we have not block cleared the object, or if there are nonzero
5046 elements in the constructor, or if the constructor has side effects,
5047 add assignments to the individual scalar fields of the object. */
5048 if (!cleared
5049 || num_nonzero_elements > 0
5050 || ctor_has_side_effects_p)
5051 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
5052
5053 *expr_p = NULL_TREE;
5054 }
5055 break;
5056
5057 case COMPLEX_TYPE:
5058 {
5059 tree r, i;
5060
5061 if (notify_temp_creation)
5062 return GS_OK;
5063
5064 /* Extract the real and imaginary parts out of the ctor. */
5065 gcc_assert (elts->length () == 2);
5066 r = (*elts)[0].value;
5067 i = (*elts)[1].value;
5068 if (r == NULL || i == NULL)
5069 {
5070 tree zero = build_zero_cst (TREE_TYPE (type));
5071 if (r == NULL)
5072 r = zero;
5073 if (i == NULL)
5074 i = zero;
5075 }
5076
5077 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5078 represent creation of a complex value. */
5079 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5080 {
5081 ctor = build_complex (type, r, i);
5082 TREE_OPERAND (*expr_p, 1) = ctor;
5083 }
5084 else
5085 {
5086 ctor = build2 (COMPLEX_EXPR, type, r, i);
5087 TREE_OPERAND (*expr_p, 1) = ctor;
5088 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5089 pre_p,
5090 post_p,
5091 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5092 fb_rvalue);
5093 }
5094 }
5095 break;
5096
5097 case VECTOR_TYPE:
5098 {
5099 unsigned HOST_WIDE_INT ix;
5100 constructor_elt *ce;
5101
5102 if (notify_temp_creation)
5103 return GS_OK;
5104
5105 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5106 if (TREE_CONSTANT (ctor))
5107 {
5108 bool constant_p = true;
5109 tree value;
5110
5111 /* Even when ctor is constant, it might contain non-*_CST
5112 elements, such as addresses or trapping values like
5113 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5114 in VECTOR_CST nodes. */
5115 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5116 if (!CONSTANT_CLASS_P (value))
5117 {
5118 constant_p = false;
5119 break;
5120 }
5121
5122 if (constant_p)
5123 {
5124 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5125 break;
5126 }
5127
5128 TREE_CONSTANT (ctor) = 0;
5129 }
5130
5131 /* Vector types use CONSTRUCTOR all the way through gimple
5132 compilation as a general initializer. */
5133 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5134 {
5135 enum gimplify_status tret;
5136 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5137 fb_rvalue);
5138 if (tret == GS_ERROR)
5139 ret = GS_ERROR;
5140 else if (TREE_STATIC (ctor)
5141 && !initializer_constant_valid_p (ce->value,
5142 TREE_TYPE (ce->value)))
5143 TREE_STATIC (ctor) = 0;
5144 }
5145 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5146 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5147 }
5148 break;
5149
5150 default:
5151 /* So how did we get a CONSTRUCTOR for a scalar type? */
5152 gcc_unreachable ();
5153 }
5154
5155 if (ret == GS_ERROR)
5156 return GS_ERROR;
5157 /* If we have gimplified both sides of the initializer but have
5158 not emitted an assignment, do so now. */
5159 if (*expr_p)
5160 {
5161 tree lhs = TREE_OPERAND (*expr_p, 0);
5162 tree rhs = TREE_OPERAND (*expr_p, 1);
5163 if (want_value && object == lhs)
5164 lhs = unshare_expr (lhs);
5165 gassign *init = gimple_build_assign (lhs, rhs);
5166 gimplify_seq_add_stmt (pre_p, init);
5167 }
5168 if (want_value)
5169 {
5170 *expr_p = object;
5171 return GS_OK;
5172 }
5173 else
5174 {
5175 *expr_p = NULL;
5176 return GS_ALL_DONE;
5177 }
5178 }
5179
5180 /* Given a pointer value OP0, return a simplified version of an
5181 indirection through OP0, or NULL_TREE if no simplification is
5182 possible. This may only be applied to a rhs of an expression.
5183 Note that the resulting type may be different from the type pointed
5184 to in the sense that it is still compatible from the langhooks
5185 point of view. */
5186
5187 static tree
5188 gimple_fold_indirect_ref_rhs (tree t)
5189 {
5190 return gimple_fold_indirect_ref (t);
5191 }
5192
5193 /* Subroutine of gimplify_modify_expr to do simplifications of
5194 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5195 something changes. */
5196
5197 static enum gimplify_status
5198 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5199 gimple_seq *pre_p, gimple_seq *post_p,
5200 bool want_value)
5201 {
5202 enum gimplify_status ret = GS_UNHANDLED;
5203 bool changed;
5204
5205 do
5206 {
5207 changed = false;
5208 switch (TREE_CODE (*from_p))
5209 {
5210 case VAR_DECL:
5211 /* If we're assigning from a read-only variable initialized with
5212 a constructor, do the direct assignment from the constructor,
5213 but only if neither source nor target are volatile since this
5214 latter assignment might end up being done on a per-field basis. */
5215 if (DECL_INITIAL (*from_p)
5216 && TREE_READONLY (*from_p)
5217 && !TREE_THIS_VOLATILE (*from_p)
5218 && !TREE_THIS_VOLATILE (*to_p)
5219 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5220 {
5221 tree old_from = *from_p;
5222 enum gimplify_status subret;
5223
5224 /* Move the constructor into the RHS. */
5225 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5226
5227 /* Let's see if gimplify_init_constructor will need to put
5228 it in memory. */
5229 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5230 false, true);
5231 if (subret == GS_ERROR)
5232 {
5233 /* If so, revert the change. */
5234 *from_p = old_from;
5235 }
5236 else
5237 {
5238 ret = GS_OK;
5239 changed = true;
5240 }
5241 }
5242 break;
5243 case INDIRECT_REF:
5244 {
5245 /* If we have code like
5246
5247 *(const A*)(A*)&x
5248
5249 where the type of "x" is a (possibly cv-qualified variant
5250 of "A"), treat the entire expression as identical to "x".
5251 This kind of code arises in C++ when an object is bound
5252 to a const reference, and if "x" is a TARGET_EXPR we want
5253 to take advantage of the optimization below. */
5254 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5255 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5256 if (t)
5257 {
5258 if (TREE_THIS_VOLATILE (t) != volatile_p)
5259 {
5260 if (DECL_P (t))
5261 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5262 build_fold_addr_expr (t));
5263 if (REFERENCE_CLASS_P (t))
5264 TREE_THIS_VOLATILE (t) = volatile_p;
5265 }
5266 *from_p = t;
5267 ret = GS_OK;
5268 changed = true;
5269 }
5270 break;
5271 }
5272
5273 case TARGET_EXPR:
5274 {
5275 /* If we are initializing something from a TARGET_EXPR, strip the
5276 TARGET_EXPR and initialize it directly, if possible. This can't
5277 be done if the initializer is void, since that implies that the
5278 temporary is set in some non-trivial way.
5279
5280 ??? What about code that pulls out the temp and uses it
5281 elsewhere? I think that such code never uses the TARGET_EXPR as
5282 an initializer. If I'm wrong, we'll die because the temp won't
5283 have any RTL. In that case, I guess we'll need to replace
5284 references somehow. */
5285 tree init = TARGET_EXPR_INITIAL (*from_p);
5286
5287 if (init
5288 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5289 || !TARGET_EXPR_NO_ELIDE (*from_p))
5290 && !VOID_TYPE_P (TREE_TYPE (init)))
5291 {
5292 *from_p = init;
5293 ret = GS_OK;
5294 changed = true;
5295 }
5296 }
5297 break;
5298
5299 case COMPOUND_EXPR:
5300 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5301 caught. */
5302 gimplify_compound_expr (from_p, pre_p, true);
5303 ret = GS_OK;
5304 changed = true;
5305 break;
5306
5307 case CONSTRUCTOR:
5308 /* If we already made some changes, let the front end have a
5309 crack at this before we break it down. */
5310 if (ret != GS_UNHANDLED)
5311 break;
5312 /* If we're initializing from a CONSTRUCTOR, break this into
5313 individual MODIFY_EXPRs. */
5314 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5315 false);
5316
5317 case COND_EXPR:
5318 /* If we're assigning to a non-register type, push the assignment
5319 down into the branches. This is mandatory for ADDRESSABLE types,
5320 since we cannot generate temporaries for such, but it saves a
5321 copy in other cases as well. */
5322 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5323 {
5324 /* This code should mirror the code in gimplify_cond_expr. */
5325 enum tree_code code = TREE_CODE (*expr_p);
5326 tree cond = *from_p;
5327 tree result = *to_p;
5328
5329 ret = gimplify_expr (&result, pre_p, post_p,
5330 is_gimple_lvalue, fb_lvalue);
5331 if (ret != GS_ERROR)
5332 ret = GS_OK;
5333
5334 /* If we are going to write RESULT more than once, clear
5335 TREE_READONLY flag, otherwise we might incorrectly promote
5336 the variable to static const and initialize it at compile
5337 time in one of the branches. */
5338 if (VAR_P (result)
5339 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5340 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5341 TREE_READONLY (result) = 0;
5342 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5343 TREE_OPERAND (cond, 1)
5344 = build2 (code, void_type_node, result,
5345 TREE_OPERAND (cond, 1));
5346 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5347 TREE_OPERAND (cond, 2)
5348 = build2 (code, void_type_node, unshare_expr (result),
5349 TREE_OPERAND (cond, 2));
5350
5351 TREE_TYPE (cond) = void_type_node;
5352 recalculate_side_effects (cond);
5353
5354 if (want_value)
5355 {
5356 gimplify_and_add (cond, pre_p);
5357 *expr_p = unshare_expr (result);
5358 }
5359 else
5360 *expr_p = cond;
5361 return ret;
5362 }
5363 break;
5364
5365 case CALL_EXPR:
5366 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5367 return slot so that we don't generate a temporary. */
5368 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5369 && aggregate_value_p (*from_p, *from_p))
5370 {
5371 bool use_target;
5372
5373 if (!(rhs_predicate_for (*to_p))(*from_p))
5374 /* If we need a temporary, *to_p isn't accurate. */
5375 use_target = false;
5376 /* It's OK to use the return slot directly unless it's an NRV. */
5377 else if (TREE_CODE (*to_p) == RESULT_DECL
5378 && DECL_NAME (*to_p) == NULL_TREE
5379 && needs_to_live_in_memory (*to_p))
5380 use_target = true;
5381 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5382 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5383 /* Don't force regs into memory. */
5384 use_target = false;
5385 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5386 /* It's OK to use the target directly if it's being
5387 initialized. */
5388 use_target = true;
5389 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5390 != INTEGER_CST)
5391 /* Always use the target and thus RSO for variable-sized types.
5392 GIMPLE cannot deal with a variable-sized assignment
5393 embedded in a call statement. */
5394 use_target = true;
5395 else if (TREE_CODE (*to_p) != SSA_NAME
5396 && (!is_gimple_variable (*to_p)
5397 || needs_to_live_in_memory (*to_p)))
5398 /* Don't use the original target if it's already addressable;
5399 if its address escapes, and the called function uses the
5400 NRV optimization, a conforming program could see *to_p
5401 change before the called function returns; see c++/19317.
5402 When optimizing, the return_slot pass marks more functions
5403 as safe after we have escape info. */
5404 use_target = false;
5405 else
5406 use_target = true;
5407
5408 if (use_target)
5409 {
5410 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5411 mark_addressable (*to_p);
5412 }
5413 }
5414 break;
5415
5416 case WITH_SIZE_EXPR:
5417 /* Likewise for calls that return an aggregate of non-constant size,
5418 since we would not be able to generate a temporary at all. */
5419 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5420 {
5421 *from_p = TREE_OPERAND (*from_p, 0);
5422 /* We don't change ret in this case because the
5423 WITH_SIZE_EXPR might have been added in
5424 gimplify_modify_expr, so returning GS_OK would lead to an
5425 infinite loop. */
5426 changed = true;
5427 }
5428 break;
5429
5430 /* If we're initializing from a container, push the initialization
5431 inside it. */
5432 case CLEANUP_POINT_EXPR:
5433 case BIND_EXPR:
5434 case STATEMENT_LIST:
5435 {
5436 tree wrap = *from_p;
5437 tree t;
5438
5439 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5440 fb_lvalue);
5441 if (ret != GS_ERROR)
5442 ret = GS_OK;
5443
5444 t = voidify_wrapper_expr (wrap, *expr_p);
5445 gcc_assert (t == *expr_p);
5446
5447 if (want_value)
5448 {
5449 gimplify_and_add (wrap, pre_p);
5450 *expr_p = unshare_expr (*to_p);
5451 }
5452 else
5453 *expr_p = wrap;
5454 return GS_OK;
5455 }
5456
5457 case COMPOUND_LITERAL_EXPR:
5458 {
5459 tree complit = TREE_OPERAND (*expr_p, 1);
5460 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5461 tree decl = DECL_EXPR_DECL (decl_s);
5462 tree init = DECL_INITIAL (decl);
5463
5464 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5465 into struct T x = { 0, 1, 2 } if the address of the
5466 compound literal has never been taken. */
5467 if (!TREE_ADDRESSABLE (complit)
5468 && !TREE_ADDRESSABLE (decl)
5469 && init)
5470 {
5471 *expr_p = copy_node (*expr_p);
5472 TREE_OPERAND (*expr_p, 1) = init;
5473 return GS_OK;
5474 }
5475 }
5476
5477 default:
5478 break;
5479 }
5480 }
5481 while (changed);
5482
5483 return ret;
5484 }
5485
5486
5487 /* Return true if T looks like a valid GIMPLE statement. */
5488
5489 static bool
5490 is_gimple_stmt (tree t)
5491 {
5492 const enum tree_code code = TREE_CODE (t);
5493
5494 switch (code)
5495 {
5496 case NOP_EXPR:
5497 /* The only valid NOP_EXPR is the empty statement. */
5498 return IS_EMPTY_STMT (t);
5499
5500 case BIND_EXPR:
5501 case COND_EXPR:
5502 /* These are only valid if they're void. */
5503 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5504
5505 case SWITCH_EXPR:
5506 case GOTO_EXPR:
5507 case RETURN_EXPR:
5508 case LABEL_EXPR:
5509 case CASE_LABEL_EXPR:
5510 case TRY_CATCH_EXPR:
5511 case TRY_FINALLY_EXPR:
5512 case EH_FILTER_EXPR:
5513 case CATCH_EXPR:
5514 case ASM_EXPR:
5515 case STATEMENT_LIST:
5516 case OACC_PARALLEL:
5517 case OACC_KERNELS:
5518 case OACC_DATA:
5519 case OACC_HOST_DATA:
5520 case OACC_DECLARE:
5521 case OACC_UPDATE:
5522 case OACC_ENTER_DATA:
5523 case OACC_EXIT_DATA:
5524 case OACC_CACHE:
5525 case OMP_PARALLEL:
5526 case OMP_FOR:
5527 case OMP_SIMD:
5528 case OMP_DISTRIBUTE:
5529 case OACC_LOOP:
5530 case OMP_SCAN:
5531 case OMP_SECTIONS:
5532 case OMP_SECTION:
5533 case OMP_SINGLE:
5534 case OMP_MASTER:
5535 case OMP_TASKGROUP:
5536 case OMP_ORDERED:
5537 case OMP_CRITICAL:
5538 case OMP_TASK:
5539 case OMP_TARGET:
5540 case OMP_TARGET_DATA:
5541 case OMP_TARGET_UPDATE:
5542 case OMP_TARGET_ENTER_DATA:
5543 case OMP_TARGET_EXIT_DATA:
5544 case OMP_TASKLOOP:
5545 case OMP_TEAMS:
5546 /* These are always void. */
5547 return true;
5548
5549 case CALL_EXPR:
5550 case MODIFY_EXPR:
5551 case PREDICT_EXPR:
5552 /* These are valid regardless of their type. */
5553 return true;
5554
5555 default:
5556 return false;
5557 }
5558 }
5559
5560
5561 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5562 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5563 DECL_GIMPLE_REG_P set.
5564
5565 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5566 other, unmodified part of the complex object just before the total store.
5567 As a consequence, if the object is still uninitialized, an undefined value
5568 will be loaded into a register, which may result in a spurious exception
5569 if the register is floating-point and the value happens to be a signaling
5570 NaN for example. Then the fully-fledged complex operations lowering pass
5571 followed by a DCE pass are necessary in order to fix things up. */
5572
5573 static enum gimplify_status
5574 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5575 bool want_value)
5576 {
5577 enum tree_code code, ocode;
5578 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5579
5580 lhs = TREE_OPERAND (*expr_p, 0);
5581 rhs = TREE_OPERAND (*expr_p, 1);
5582 code = TREE_CODE (lhs);
5583 lhs = TREE_OPERAND (lhs, 0);
5584
5585 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5586 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5587 TREE_NO_WARNING (other) = 1;
5588 other = get_formal_tmp_var (other, pre_p);
5589
5590 realpart = code == REALPART_EXPR ? rhs : other;
5591 imagpart = code == REALPART_EXPR ? other : rhs;
5592
5593 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5594 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5595 else
5596 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5597
5598 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5599 *expr_p = (want_value) ? rhs : NULL_TREE;
5600
5601 return GS_ALL_DONE;
5602 }
5603
5604 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5605
5606 modify_expr
5607 : varname '=' rhs
5608 | '*' ID '=' rhs
5609
5610 PRE_P points to the list where side effects that must happen before
5611 *EXPR_P should be stored.
5612
5613 POST_P points to the list where side effects that must happen after
5614 *EXPR_P should be stored.
5615
5616 WANT_VALUE is nonzero iff we want to use the value of this expression
5617 in another expression. */
5618
5619 static enum gimplify_status
5620 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5621 bool want_value)
5622 {
5623 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5624 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5625 enum gimplify_status ret = GS_UNHANDLED;
5626 gimple *assign;
5627 location_t loc = EXPR_LOCATION (*expr_p);
5628 gimple_stmt_iterator gsi;
5629
5630 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5631 || TREE_CODE (*expr_p) == INIT_EXPR);
5632
5633 /* Trying to simplify a clobber using normal logic doesn't work,
5634 so handle it here. */
5635 if (TREE_CLOBBER_P (*from_p))
5636 {
5637 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5638 if (ret == GS_ERROR)
5639 return ret;
5640 gcc_assert (!want_value);
5641 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5642 {
5643 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5644 pre_p, post_p);
5645 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5646 }
5647 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5648 *expr_p = NULL;
5649 return GS_ALL_DONE;
5650 }
5651
5652 /* Insert pointer conversions required by the middle-end that are not
5653 required by the frontend. This fixes middle-end type checking for
5654 for example gcc.dg/redecl-6.c. */
5655 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5656 {
5657 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5658 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5659 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5660 }
5661
5662 /* See if any simplifications can be done based on what the RHS is. */
5663 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5664 want_value);
5665 if (ret != GS_UNHANDLED)
5666 return ret;
5667
5668 /* For zero sized types only gimplify the left hand side and right hand
5669 side as statements and throw away the assignment. Do this after
5670 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5671 types properly. */
5672 if (zero_sized_type (TREE_TYPE (*from_p))
5673 && !want_value
5674 /* Don't do this for calls that return addressable types, expand_call
5675 relies on those having a lhs. */
5676 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5677 && TREE_CODE (*from_p) == CALL_EXPR))
5678 {
5679 gimplify_stmt (from_p, pre_p);
5680 gimplify_stmt (to_p, pre_p);
5681 *expr_p = NULL_TREE;
5682 return GS_ALL_DONE;
5683 }
5684
5685 /* If the value being copied is of variable width, compute the length
5686 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5687 before gimplifying any of the operands so that we can resolve any
5688 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5689 the size of the expression to be copied, not of the destination, so
5690 that is what we must do here. */
5691 maybe_with_size_expr (from_p);
5692
5693 /* As a special case, we have to temporarily allow for assignments
5694 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5695 a toplevel statement, when gimplifying the GENERIC expression
5696 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5697 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5698
5699 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5700 prevent gimplify_expr from trying to create a new temporary for
5701 foo's LHS, we tell it that it should only gimplify until it
5702 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5703 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5704 and all we need to do here is set 'a' to be its LHS. */
5705
5706 /* Gimplify the RHS first for C++17 and bug 71104. */
5707 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5708 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5709 if (ret == GS_ERROR)
5710 return ret;
5711
5712 /* Then gimplify the LHS. */
5713 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5714 twice we have to make sure to gimplify into non-SSA as otherwise
5715 the abnormal edge added later will make those defs not dominate
5716 their uses.
5717 ??? Technically this applies only to the registers used in the
5718 resulting non-register *TO_P. */
5719 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5720 if (saved_into_ssa
5721 && TREE_CODE (*from_p) == CALL_EXPR
5722 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5723 gimplify_ctxp->into_ssa = false;
5724 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5725 gimplify_ctxp->into_ssa = saved_into_ssa;
5726 if (ret == GS_ERROR)
5727 return ret;
5728
5729 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5730 guess for the predicate was wrong. */
5731 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5732 if (final_pred != initial_pred)
5733 {
5734 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5735 if (ret == GS_ERROR)
5736 return ret;
5737 }
5738
5739 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5740 size as argument to the call. */
5741 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5742 {
5743 tree call = TREE_OPERAND (*from_p, 0);
5744 tree vlasize = TREE_OPERAND (*from_p, 1);
5745
5746 if (TREE_CODE (call) == CALL_EXPR
5747 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5748 {
5749 int nargs = call_expr_nargs (call);
5750 tree type = TREE_TYPE (call);
5751 tree ap = CALL_EXPR_ARG (call, 0);
5752 tree tag = CALL_EXPR_ARG (call, 1);
5753 tree aptag = CALL_EXPR_ARG (call, 2);
5754 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5755 IFN_VA_ARG, type,
5756 nargs + 1, ap, tag,
5757 aptag, vlasize);
5758 TREE_OPERAND (*from_p, 0) = newcall;
5759 }
5760 }
5761
5762 /* Now see if the above changed *from_p to something we handle specially. */
5763 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5764 want_value);
5765 if (ret != GS_UNHANDLED)
5766 return ret;
5767
5768 /* If we've got a variable sized assignment between two lvalues (i.e. does
5769 not involve a call), then we can make things a bit more straightforward
5770 by converting the assignment to memcpy or memset. */
5771 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5772 {
5773 tree from = TREE_OPERAND (*from_p, 0);
5774 tree size = TREE_OPERAND (*from_p, 1);
5775
5776 if (TREE_CODE (from) == CONSTRUCTOR)
5777 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5778
5779 if (is_gimple_addressable (from))
5780 {
5781 *from_p = from;
5782 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5783 pre_p);
5784 }
5785 }
5786
5787 /* Transform partial stores to non-addressable complex variables into
5788 total stores. This allows us to use real instead of virtual operands
5789 for these variables, which improves optimization. */
5790 if ((TREE_CODE (*to_p) == REALPART_EXPR
5791 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5792 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5793 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5794
5795 /* Try to alleviate the effects of the gimplification creating artificial
5796 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5797 make sure not to create DECL_DEBUG_EXPR links across functions. */
5798 if (!gimplify_ctxp->into_ssa
5799 && VAR_P (*from_p)
5800 && DECL_IGNORED_P (*from_p)
5801 && DECL_P (*to_p)
5802 && !DECL_IGNORED_P (*to_p)
5803 && decl_function_context (*to_p) == current_function_decl
5804 && decl_function_context (*from_p) == current_function_decl)
5805 {
5806 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5807 DECL_NAME (*from_p)
5808 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5809 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5810 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5811 }
5812
5813 if (want_value && TREE_THIS_VOLATILE (*to_p))
5814 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5815
5816 if (TREE_CODE (*from_p) == CALL_EXPR)
5817 {
5818 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5819 instead of a GIMPLE_ASSIGN. */
5820 gcall *call_stmt;
5821 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5822 {
5823 /* Gimplify internal functions created in the FEs. */
5824 int nargs = call_expr_nargs (*from_p), i;
5825 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5826 auto_vec<tree> vargs (nargs);
5827
5828 for (i = 0; i < nargs; i++)
5829 {
5830 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5831 EXPR_LOCATION (*from_p));
5832 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5833 }
5834 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5835 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5836 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5837 }
5838 else
5839 {
5840 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5841 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5842 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5843 tree fndecl = get_callee_fndecl (*from_p);
5844 if (fndecl
5845 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
5846 && call_expr_nargs (*from_p) == 3)
5847 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5848 CALL_EXPR_ARG (*from_p, 0),
5849 CALL_EXPR_ARG (*from_p, 1),
5850 CALL_EXPR_ARG (*from_p, 2));
5851 else
5852 {
5853 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5854 }
5855 }
5856 notice_special_calls (call_stmt);
5857 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5858 gimple_call_set_lhs (call_stmt, *to_p);
5859 else if (TREE_CODE (*to_p) == SSA_NAME)
5860 /* The above is somewhat premature, avoid ICEing later for a
5861 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5862 ??? This doesn't make it a default-def. */
5863 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5864
5865 assign = call_stmt;
5866 }
5867 else
5868 {
5869 assign = gimple_build_assign (*to_p, *from_p);
5870 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5871 if (COMPARISON_CLASS_P (*from_p))
5872 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5873 }
5874
5875 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5876 {
5877 /* We should have got an SSA name from the start. */
5878 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5879 || ! gimple_in_ssa_p (cfun));
5880 }
5881
5882 gimplify_seq_add_stmt (pre_p, assign);
5883 gsi = gsi_last (*pre_p);
5884 maybe_fold_stmt (&gsi);
5885
5886 if (want_value)
5887 {
5888 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5889 return GS_OK;
5890 }
5891 else
5892 *expr_p = NULL;
5893
5894 return GS_ALL_DONE;
5895 }
5896
5897 /* Gimplify a comparison between two variable-sized objects. Do this
5898 with a call to BUILT_IN_MEMCMP. */
5899
5900 static enum gimplify_status
5901 gimplify_variable_sized_compare (tree *expr_p)
5902 {
5903 location_t loc = EXPR_LOCATION (*expr_p);
5904 tree op0 = TREE_OPERAND (*expr_p, 0);
5905 tree op1 = TREE_OPERAND (*expr_p, 1);
5906 tree t, arg, dest, src, expr;
5907
5908 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5909 arg = unshare_expr (arg);
5910 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5911 src = build_fold_addr_expr_loc (loc, op1);
5912 dest = build_fold_addr_expr_loc (loc, op0);
5913 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5914 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5915
5916 expr
5917 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5918 SET_EXPR_LOCATION (expr, loc);
5919 *expr_p = expr;
5920
5921 return GS_OK;
5922 }
5923
5924 /* Gimplify a comparison between two aggregate objects of integral scalar
5925 mode as a comparison between the bitwise equivalent scalar values. */
5926
5927 static enum gimplify_status
5928 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5929 {
5930 location_t loc = EXPR_LOCATION (*expr_p);
5931 tree op0 = TREE_OPERAND (*expr_p, 0);
5932 tree op1 = TREE_OPERAND (*expr_p, 1);
5933
5934 tree type = TREE_TYPE (op0);
5935 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5936
5937 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5938 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5939
5940 *expr_p
5941 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5942
5943 return GS_OK;
5944 }
5945
5946 /* Gimplify an expression sequence. This function gimplifies each
5947 expression and rewrites the original expression with the last
5948 expression of the sequence in GIMPLE form.
5949
5950 PRE_P points to the list where the side effects for all the
5951 expressions in the sequence will be emitted.
5952
5953 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5954
5955 static enum gimplify_status
5956 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5957 {
5958 tree t = *expr_p;
5959
5960 do
5961 {
5962 tree *sub_p = &TREE_OPERAND (t, 0);
5963
5964 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5965 gimplify_compound_expr (sub_p, pre_p, false);
5966 else
5967 gimplify_stmt (sub_p, pre_p);
5968
5969 t = TREE_OPERAND (t, 1);
5970 }
5971 while (TREE_CODE (t) == COMPOUND_EXPR);
5972
5973 *expr_p = t;
5974 if (want_value)
5975 return GS_OK;
5976 else
5977 {
5978 gimplify_stmt (expr_p, pre_p);
5979 return GS_ALL_DONE;
5980 }
5981 }
5982
5983 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5984 gimplify. After gimplification, EXPR_P will point to a new temporary
5985 that holds the original value of the SAVE_EXPR node.
5986
5987 PRE_P points to the list where side effects that must happen before
5988 *EXPR_P should be stored. */
5989
5990 static enum gimplify_status
5991 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5992 {
5993 enum gimplify_status ret = GS_ALL_DONE;
5994 tree val;
5995
5996 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5997 val = TREE_OPERAND (*expr_p, 0);
5998
5999 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
6000 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
6001 {
6002 /* The operand may be a void-valued expression. It is
6003 being executed only for its side-effects. */
6004 if (TREE_TYPE (val) == void_type_node)
6005 {
6006 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6007 is_gimple_stmt, fb_none);
6008 val = NULL;
6009 }
6010 else
6011 /* The temporary may not be an SSA name as later abnormal and EH
6012 control flow may invalidate use/def domination. When in SSA
6013 form then assume there are no such issues and SAVE_EXPRs only
6014 appear via GENERIC foldings. */
6015 val = get_initialized_tmp_var (val, pre_p, post_p,
6016 gimple_in_ssa_p (cfun));
6017
6018 TREE_OPERAND (*expr_p, 0) = val;
6019 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
6020 }
6021
6022 *expr_p = val;
6023
6024 return ret;
6025 }
6026
6027 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
6028
6029 unary_expr
6030 : ...
6031 | '&' varname
6032 ...
6033
6034 PRE_P points to the list where side effects that must happen before
6035 *EXPR_P should be stored.
6036
6037 POST_P points to the list where side effects that must happen after
6038 *EXPR_P should be stored. */
6039
6040 static enum gimplify_status
6041 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6042 {
6043 tree expr = *expr_p;
6044 tree op0 = TREE_OPERAND (expr, 0);
6045 enum gimplify_status ret;
6046 location_t loc = EXPR_LOCATION (*expr_p);
6047
6048 switch (TREE_CODE (op0))
6049 {
6050 case INDIRECT_REF:
6051 do_indirect_ref:
6052 /* Check if we are dealing with an expression of the form '&*ptr'.
6053 While the front end folds away '&*ptr' into 'ptr', these
6054 expressions may be generated internally by the compiler (e.g.,
6055 builtins like __builtin_va_end). */
6056 /* Caution: the silent array decomposition semantics we allow for
6057 ADDR_EXPR means we can't always discard the pair. */
6058 /* Gimplification of the ADDR_EXPR operand may drop
6059 cv-qualification conversions, so make sure we add them if
6060 needed. */
6061 {
6062 tree op00 = TREE_OPERAND (op0, 0);
6063 tree t_expr = TREE_TYPE (expr);
6064 tree t_op00 = TREE_TYPE (op00);
6065
6066 if (!useless_type_conversion_p (t_expr, t_op00))
6067 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
6068 *expr_p = op00;
6069 ret = GS_OK;
6070 }
6071 break;
6072
6073 case VIEW_CONVERT_EXPR:
6074 /* Take the address of our operand and then convert it to the type of
6075 this ADDR_EXPR.
6076
6077 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
6078 all clear. The impact of this transformation is even less clear. */
6079
6080 /* If the operand is a useless conversion, look through it. Doing so
6081 guarantees that the ADDR_EXPR and its operand will remain of the
6082 same type. */
6083 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
6084 op0 = TREE_OPERAND (op0, 0);
6085
6086 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6087 build_fold_addr_expr_loc (loc,
6088 TREE_OPERAND (op0, 0)));
6089 ret = GS_OK;
6090 break;
6091
6092 case MEM_REF:
6093 if (integer_zerop (TREE_OPERAND (op0, 1)))
6094 goto do_indirect_ref;
6095
6096 /* fall through */
6097
6098 default:
6099 /* If we see a call to a declared builtin or see its address
6100 being taken (we can unify those cases here) then we can mark
6101 the builtin for implicit generation by GCC. */
6102 if (TREE_CODE (op0) == FUNCTION_DECL
6103 && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
6104 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6105 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6106
6107 /* We use fb_either here because the C frontend sometimes takes
6108 the address of a call that returns a struct; see
6109 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6110 the implied temporary explicit. */
6111
6112 /* Make the operand addressable. */
6113 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6114 is_gimple_addressable, fb_either);
6115 if (ret == GS_ERROR)
6116 break;
6117
6118 /* Then mark it. Beware that it may not be possible to do so directly
6119 if a temporary has been created by the gimplification. */
6120 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6121
6122 op0 = TREE_OPERAND (expr, 0);
6123
6124 /* For various reasons, the gimplification of the expression
6125 may have made a new INDIRECT_REF. */
6126 if (TREE_CODE (op0) == INDIRECT_REF)
6127 goto do_indirect_ref;
6128
6129 mark_addressable (TREE_OPERAND (expr, 0));
6130
6131 /* The FEs may end up building ADDR_EXPRs early on a decl with
6132 an incomplete type. Re-build ADDR_EXPRs in canonical form
6133 here. */
6134 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6135 *expr_p = build_fold_addr_expr (op0);
6136
6137 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6138 recompute_tree_invariant_for_addr_expr (*expr_p);
6139
6140 /* If we re-built the ADDR_EXPR add a conversion to the original type
6141 if required. */
6142 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6143 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6144
6145 break;
6146 }
6147
6148 return ret;
6149 }
6150
6151 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6152 value; output operands should be a gimple lvalue. */
6153
6154 static enum gimplify_status
6155 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6156 {
6157 tree expr;
6158 int noutputs;
6159 const char **oconstraints;
6160 int i;
6161 tree link;
6162 const char *constraint;
6163 bool allows_mem, allows_reg, is_inout;
6164 enum gimplify_status ret, tret;
6165 gasm *stmt;
6166 vec<tree, va_gc> *inputs;
6167 vec<tree, va_gc> *outputs;
6168 vec<tree, va_gc> *clobbers;
6169 vec<tree, va_gc> *labels;
6170 tree link_next;
6171
6172 expr = *expr_p;
6173 noutputs = list_length (ASM_OUTPUTS (expr));
6174 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6175
6176 inputs = NULL;
6177 outputs = NULL;
6178 clobbers = NULL;
6179 labels = NULL;
6180
6181 ret = GS_ALL_DONE;
6182 link_next = NULL_TREE;
6183 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6184 {
6185 bool ok;
6186 size_t constraint_len;
6187
6188 link_next = TREE_CHAIN (link);
6189
6190 oconstraints[i]
6191 = constraint
6192 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6193 constraint_len = strlen (constraint);
6194 if (constraint_len == 0)
6195 continue;
6196
6197 ok = parse_output_constraint (&constraint, i, 0, 0,
6198 &allows_mem, &allows_reg, &is_inout);
6199 if (!ok)
6200 {
6201 ret = GS_ERROR;
6202 is_inout = false;
6203 }
6204
6205 /* If we can't make copies, we can only accept memory. */
6206 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6207 {
6208 if (allows_mem)
6209 allows_reg = 0;
6210 else
6211 {
6212 error ("impossible constraint in %<asm%>");
6213 error ("non-memory output %d must stay in memory", i);
6214 return GS_ERROR;
6215 }
6216 }
6217
6218 if (!allows_reg && allows_mem)
6219 mark_addressable (TREE_VALUE (link));
6220
6221 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6222 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6223 fb_lvalue | fb_mayfail);
6224 if (tret == GS_ERROR)
6225 {
6226 error ("invalid lvalue in %<asm%> output %d", i);
6227 ret = tret;
6228 }
6229
6230 /* If the constraint does not allow memory make sure we gimplify
6231 it to a register if it is not already but its base is. This
6232 happens for complex and vector components. */
6233 if (!allows_mem)
6234 {
6235 tree op = TREE_VALUE (link);
6236 if (! is_gimple_val (op)
6237 && is_gimple_reg_type (TREE_TYPE (op))
6238 && is_gimple_reg (get_base_address (op)))
6239 {
6240 tree tem = create_tmp_reg (TREE_TYPE (op));
6241 tree ass;
6242 if (is_inout)
6243 {
6244 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6245 tem, unshare_expr (op));
6246 gimplify_and_add (ass, pre_p);
6247 }
6248 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6249 gimplify_and_add (ass, post_p);
6250
6251 TREE_VALUE (link) = tem;
6252 tret = GS_OK;
6253 }
6254 }
6255
6256 vec_safe_push (outputs, link);
6257 TREE_CHAIN (link) = NULL_TREE;
6258
6259 if (is_inout)
6260 {
6261 /* An input/output operand. To give the optimizers more
6262 flexibility, split it into separate input and output
6263 operands. */
6264 tree input;
6265 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6266 char buf[11];
6267
6268 /* Turn the in/out constraint into an output constraint. */
6269 char *p = xstrdup (constraint);
6270 p[0] = '=';
6271 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6272
6273 /* And add a matching input constraint. */
6274 if (allows_reg)
6275 {
6276 sprintf (buf, "%u", i);
6277
6278 /* If there are multiple alternatives in the constraint,
6279 handle each of them individually. Those that allow register
6280 will be replaced with operand number, the others will stay
6281 unchanged. */
6282 if (strchr (p, ',') != NULL)
6283 {
6284 size_t len = 0, buflen = strlen (buf);
6285 char *beg, *end, *str, *dst;
6286
6287 for (beg = p + 1;;)
6288 {
6289 end = strchr (beg, ',');
6290 if (end == NULL)
6291 end = strchr (beg, '\0');
6292 if ((size_t) (end - beg) < buflen)
6293 len += buflen + 1;
6294 else
6295 len += end - beg + 1;
6296 if (*end)
6297 beg = end + 1;
6298 else
6299 break;
6300 }
6301
6302 str = (char *) alloca (len);
6303 for (beg = p + 1, dst = str;;)
6304 {
6305 const char *tem;
6306 bool mem_p, reg_p, inout_p;
6307
6308 end = strchr (beg, ',');
6309 if (end)
6310 *end = '\0';
6311 beg[-1] = '=';
6312 tem = beg - 1;
6313 parse_output_constraint (&tem, i, 0, 0,
6314 &mem_p, &reg_p, &inout_p);
6315 if (dst != str)
6316 *dst++ = ',';
6317 if (reg_p)
6318 {
6319 memcpy (dst, buf, buflen);
6320 dst += buflen;
6321 }
6322 else
6323 {
6324 if (end)
6325 len = end - beg;
6326 else
6327 len = strlen (beg);
6328 memcpy (dst, beg, len);
6329 dst += len;
6330 }
6331 if (end)
6332 beg = end + 1;
6333 else
6334 break;
6335 }
6336 *dst = '\0';
6337 input = build_string (dst - str, str);
6338 }
6339 else
6340 input = build_string (strlen (buf), buf);
6341 }
6342 else
6343 input = build_string (constraint_len - 1, constraint + 1);
6344
6345 free (p);
6346
6347 input = build_tree_list (build_tree_list (NULL_TREE, input),
6348 unshare_expr (TREE_VALUE (link)));
6349 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6350 }
6351 }
6352
6353 link_next = NULL_TREE;
6354 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6355 {
6356 link_next = TREE_CHAIN (link);
6357 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6358 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6359 oconstraints, &allows_mem, &allows_reg);
6360
6361 /* If we can't make copies, we can only accept memory. */
6362 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6363 {
6364 if (allows_mem)
6365 allows_reg = 0;
6366 else
6367 {
6368 error ("impossible constraint in %<asm%>");
6369 error ("non-memory input %d must stay in memory", i);
6370 return GS_ERROR;
6371 }
6372 }
6373
6374 /* If the operand is a memory input, it should be an lvalue. */
6375 if (!allows_reg && allows_mem)
6376 {
6377 tree inputv = TREE_VALUE (link);
6378 STRIP_NOPS (inputv);
6379 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6380 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6381 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6382 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6383 || TREE_CODE (inputv) == MODIFY_EXPR)
6384 TREE_VALUE (link) = error_mark_node;
6385 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6386 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6387 if (tret != GS_ERROR)
6388 {
6389 /* Unlike output operands, memory inputs are not guaranteed
6390 to be lvalues by the FE, and while the expressions are
6391 marked addressable there, if it is e.g. a statement
6392 expression, temporaries in it might not end up being
6393 addressable. They might be already used in the IL and thus
6394 it is too late to make them addressable now though. */
6395 tree x = TREE_VALUE (link);
6396 while (handled_component_p (x))
6397 x = TREE_OPERAND (x, 0);
6398 if (TREE_CODE (x) == MEM_REF
6399 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6400 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6401 if ((VAR_P (x)
6402 || TREE_CODE (x) == PARM_DECL
6403 || TREE_CODE (x) == RESULT_DECL)
6404 && !TREE_ADDRESSABLE (x)
6405 && is_gimple_reg (x))
6406 {
6407 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6408 input_location), 0,
6409 "memory input %d is not directly addressable",
6410 i);
6411 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6412 }
6413 }
6414 mark_addressable (TREE_VALUE (link));
6415 if (tret == GS_ERROR)
6416 {
6417 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6418 "memory input %d is not directly addressable", i);
6419 ret = tret;
6420 }
6421 }
6422 else
6423 {
6424 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6425 is_gimple_asm_val, fb_rvalue);
6426 if (tret == GS_ERROR)
6427 ret = tret;
6428 }
6429
6430 TREE_CHAIN (link) = NULL_TREE;
6431 vec_safe_push (inputs, link);
6432 }
6433
6434 link_next = NULL_TREE;
6435 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6436 {
6437 link_next = TREE_CHAIN (link);
6438 TREE_CHAIN (link) = NULL_TREE;
6439 vec_safe_push (clobbers, link);
6440 }
6441
6442 link_next = NULL_TREE;
6443 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6444 {
6445 link_next = TREE_CHAIN (link);
6446 TREE_CHAIN (link) = NULL_TREE;
6447 vec_safe_push (labels, link);
6448 }
6449
6450 /* Do not add ASMs with errors to the gimple IL stream. */
6451 if (ret != GS_ERROR)
6452 {
6453 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6454 inputs, outputs, clobbers, labels);
6455
6456 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6457 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6458 gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
6459
6460 gimplify_seq_add_stmt (pre_p, stmt);
6461 }
6462
6463 return ret;
6464 }
6465
6466 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6467 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6468 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6469 return to this function.
6470
6471 FIXME should we complexify the prequeue handling instead? Or use flags
6472 for all the cleanups and let the optimizer tighten them up? The current
6473 code seems pretty fragile; it will break on a cleanup within any
6474 non-conditional nesting. But any such nesting would be broken, anyway;
6475 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6476 and continues out of it. We can do that at the RTL level, though, so
6477 having an optimizer to tighten up try/finally regions would be a Good
6478 Thing. */
6479
6480 static enum gimplify_status
6481 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6482 {
6483 gimple_stmt_iterator iter;
6484 gimple_seq body_sequence = NULL;
6485
6486 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6487
6488 /* We only care about the number of conditions between the innermost
6489 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6490 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6491 int old_conds = gimplify_ctxp->conditions;
6492 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6493 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6494 gimplify_ctxp->conditions = 0;
6495 gimplify_ctxp->conditional_cleanups = NULL;
6496 gimplify_ctxp->in_cleanup_point_expr = true;
6497
6498 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6499
6500 gimplify_ctxp->conditions = old_conds;
6501 gimplify_ctxp->conditional_cleanups = old_cleanups;
6502 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6503
6504 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6505 {
6506 gimple *wce = gsi_stmt (iter);
6507
6508 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6509 {
6510 if (gsi_one_before_end_p (iter))
6511 {
6512 /* Note that gsi_insert_seq_before and gsi_remove do not
6513 scan operands, unlike some other sequence mutators. */
6514 if (!gimple_wce_cleanup_eh_only (wce))
6515 gsi_insert_seq_before_without_update (&iter,
6516 gimple_wce_cleanup (wce),
6517 GSI_SAME_STMT);
6518 gsi_remove (&iter, true);
6519 break;
6520 }
6521 else
6522 {
6523 gtry *gtry;
6524 gimple_seq seq;
6525 enum gimple_try_flags kind;
6526
6527 if (gimple_wce_cleanup_eh_only (wce))
6528 kind = GIMPLE_TRY_CATCH;
6529 else
6530 kind = GIMPLE_TRY_FINALLY;
6531 seq = gsi_split_seq_after (iter);
6532
6533 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6534 /* Do not use gsi_replace here, as it may scan operands.
6535 We want to do a simple structural modification only. */
6536 gsi_set_stmt (&iter, gtry);
6537 iter = gsi_start (gtry->eval);
6538 }
6539 }
6540 else
6541 gsi_next (&iter);
6542 }
6543
6544 gimplify_seq_add_seq (pre_p, body_sequence);
6545 if (temp)
6546 {
6547 *expr_p = temp;
6548 return GS_OK;
6549 }
6550 else
6551 {
6552 *expr_p = NULL;
6553 return GS_ALL_DONE;
6554 }
6555 }
6556
6557 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6558 is the cleanup action required. EH_ONLY is true if the cleanup should
6559 only be executed if an exception is thrown, not on normal exit.
6560 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6561 only valid for clobbers. */
6562
6563 static void
6564 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6565 bool force_uncond = false)
6566 {
6567 gimple *wce;
6568 gimple_seq cleanup_stmts = NULL;
6569
6570 /* Errors can result in improperly nested cleanups. Which results in
6571 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6572 if (seen_error ())
6573 return;
6574
6575 if (gimple_conditional_context ())
6576 {
6577 /* If we're in a conditional context, this is more complex. We only
6578 want to run the cleanup if we actually ran the initialization that
6579 necessitates it, but we want to run it after the end of the
6580 conditional context. So we wrap the try/finally around the
6581 condition and use a flag to determine whether or not to actually
6582 run the destructor. Thus
6583
6584 test ? f(A()) : 0
6585
6586 becomes (approximately)
6587
6588 flag = 0;
6589 try {
6590 if (test) { A::A(temp); flag = 1; val = f(temp); }
6591 else { val = 0; }
6592 } finally {
6593 if (flag) A::~A(temp);
6594 }
6595 val
6596 */
6597 if (force_uncond)
6598 {
6599 gimplify_stmt (&cleanup, &cleanup_stmts);
6600 wce = gimple_build_wce (cleanup_stmts);
6601 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6602 }
6603 else
6604 {
6605 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6606 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6607 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6608
6609 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6610 gimplify_stmt (&cleanup, &cleanup_stmts);
6611 wce = gimple_build_wce (cleanup_stmts);
6612
6613 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6614 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6615 gimplify_seq_add_stmt (pre_p, ftrue);
6616
6617 /* Because of this manipulation, and the EH edges that jump
6618 threading cannot redirect, the temporary (VAR) will appear
6619 to be used uninitialized. Don't warn. */
6620 TREE_NO_WARNING (var) = 1;
6621 }
6622 }
6623 else
6624 {
6625 gimplify_stmt (&cleanup, &cleanup_stmts);
6626 wce = gimple_build_wce (cleanup_stmts);
6627 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6628 gimplify_seq_add_stmt (pre_p, wce);
6629 }
6630 }
6631
6632 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6633
6634 static enum gimplify_status
6635 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6636 {
6637 tree targ = *expr_p;
6638 tree temp = TARGET_EXPR_SLOT (targ);
6639 tree init = TARGET_EXPR_INITIAL (targ);
6640 enum gimplify_status ret;
6641
6642 bool unpoison_empty_seq = false;
6643 gimple_stmt_iterator unpoison_it;
6644
6645 if (init)
6646 {
6647 tree cleanup = NULL_TREE;
6648
6649 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6650 to the temps list. Handle also variable length TARGET_EXPRs. */
6651 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6652 {
6653 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6654 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6655 gimplify_vla_decl (temp, pre_p);
6656 }
6657 else
6658 {
6659 /* Save location where we need to place unpoisoning. It's possible
6660 that a variable will be converted to needs_to_live_in_memory. */
6661 unpoison_it = gsi_last (*pre_p);
6662 unpoison_empty_seq = gsi_end_p (unpoison_it);
6663
6664 gimple_add_tmp_var (temp);
6665 }
6666
6667 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6668 expression is supposed to initialize the slot. */
6669 if (VOID_TYPE_P (TREE_TYPE (init)))
6670 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6671 else
6672 {
6673 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6674 init = init_expr;
6675 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6676 init = NULL;
6677 ggc_free (init_expr);
6678 }
6679 if (ret == GS_ERROR)
6680 {
6681 /* PR c++/28266 Make sure this is expanded only once. */
6682 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6683 return GS_ERROR;
6684 }
6685 if (init)
6686 gimplify_and_add (init, pre_p);
6687
6688 /* If needed, push the cleanup for the temp. */
6689 if (TARGET_EXPR_CLEANUP (targ))
6690 {
6691 if (CLEANUP_EH_ONLY (targ))
6692 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6693 CLEANUP_EH_ONLY (targ), pre_p);
6694 else
6695 cleanup = TARGET_EXPR_CLEANUP (targ);
6696 }
6697
6698 /* Add a clobber for the temporary going out of scope, like
6699 gimplify_bind_expr. */
6700 if (gimplify_ctxp->in_cleanup_point_expr
6701 && needs_to_live_in_memory (temp))
6702 {
6703 if (flag_stack_reuse == SR_ALL)
6704 {
6705 tree clobber = build_clobber (TREE_TYPE (temp));
6706 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6707 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6708 }
6709 if (asan_poisoned_variables
6710 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6711 && !TREE_STATIC (temp)
6712 && dbg_cnt (asan_use_after_scope)
6713 && !gimplify_omp_ctxp)
6714 {
6715 tree asan_cleanup = build_asan_poison_call_expr (temp);
6716 if (asan_cleanup)
6717 {
6718 if (unpoison_empty_seq)
6719 unpoison_it = gsi_start (*pre_p);
6720
6721 asan_poison_variable (temp, false, &unpoison_it,
6722 unpoison_empty_seq);
6723 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6724 }
6725 }
6726 }
6727 if (cleanup)
6728 gimple_push_cleanup (temp, cleanup, false, pre_p);
6729
6730 /* Only expand this once. */
6731 TREE_OPERAND (targ, 3) = init;
6732 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6733 }
6734 else
6735 /* We should have expanded this before. */
6736 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6737
6738 *expr_p = temp;
6739 return GS_OK;
6740 }
6741
6742 /* Gimplification of expression trees. */
6743
6744 /* Gimplify an expression which appears at statement context. The
6745 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6746 NULL, a new sequence is allocated.
6747
6748 Return true if we actually added a statement to the queue. */
6749
6750 bool
6751 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6752 {
6753 gimple_seq_node last;
6754
6755 last = gimple_seq_last (*seq_p);
6756 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6757 return last != gimple_seq_last (*seq_p);
6758 }
6759
6760 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6761 to CTX. If entries already exist, force them to be some flavor of private.
6762 If there is no enclosing parallel, do nothing. */
6763
6764 void
6765 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6766 {
6767 splay_tree_node n;
6768
6769 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6770 return;
6771
6772 do
6773 {
6774 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6775 if (n != NULL)
6776 {
6777 if (n->value & GOVD_SHARED)
6778 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6779 else if (n->value & GOVD_MAP)
6780 n->value |= GOVD_MAP_TO_ONLY;
6781 else
6782 return;
6783 }
6784 else if ((ctx->region_type & ORT_TARGET) != 0)
6785 {
6786 if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
6787 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6788 else
6789 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6790 }
6791 else if (ctx->region_type != ORT_WORKSHARE
6792 && ctx->region_type != ORT_TASKGROUP
6793 && ctx->region_type != ORT_SIMD
6794 && ctx->region_type != ORT_ACC
6795 && !(ctx->region_type & ORT_TARGET_DATA))
6796 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6797
6798 ctx = ctx->outer_context;
6799 }
6800 while (ctx);
6801 }
6802
6803 /* Similarly for each of the type sizes of TYPE. */
6804
6805 static void
6806 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6807 {
6808 if (type == NULL || type == error_mark_node)
6809 return;
6810 type = TYPE_MAIN_VARIANT (type);
6811
6812 if (ctx->privatized_types->add (type))
6813 return;
6814
6815 switch (TREE_CODE (type))
6816 {
6817 case INTEGER_TYPE:
6818 case ENUMERAL_TYPE:
6819 case BOOLEAN_TYPE:
6820 case REAL_TYPE:
6821 case FIXED_POINT_TYPE:
6822 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6823 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6824 break;
6825
6826 case ARRAY_TYPE:
6827 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6828 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6829 break;
6830
6831 case RECORD_TYPE:
6832 case UNION_TYPE:
6833 case QUAL_UNION_TYPE:
6834 {
6835 tree field;
6836 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6837 if (TREE_CODE (field) == FIELD_DECL)
6838 {
6839 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6840 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6841 }
6842 }
6843 break;
6844
6845 case POINTER_TYPE:
6846 case REFERENCE_TYPE:
6847 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6848 break;
6849
6850 default:
6851 break;
6852 }
6853
6854 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6855 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6856 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6857 }
6858
6859 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6860
6861 static void
6862 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6863 {
6864 splay_tree_node n;
6865 unsigned int nflags;
6866 tree t;
6867
6868 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6869 return;
6870
6871 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6872 there are constructors involved somewhere. Exception is a shared clause,
6873 there is nothing privatized in that case. */
6874 if ((flags & GOVD_SHARED) == 0
6875 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6876 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6877 flags |= GOVD_SEEN;
6878
6879 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6880 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6881 {
6882 /* We shouldn't be re-adding the decl with the same data
6883 sharing class. */
6884 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6885 nflags = n->value | flags;
6886 /* The only combination of data sharing classes we should see is
6887 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6888 reduction variables to be used in data sharing clauses. */
6889 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6890 || ((nflags & GOVD_DATA_SHARE_CLASS)
6891 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6892 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6893 n->value = nflags;
6894 return;
6895 }
6896
6897 /* When adding a variable-sized variable, we have to handle all sorts
6898 of additional bits of data: the pointer replacement variable, and
6899 the parameters of the type. */
6900 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6901 {
6902 /* Add the pointer replacement variable as PRIVATE if the variable
6903 replacement is private, else FIRSTPRIVATE since we'll need the
6904 address of the original variable either for SHARED, or for the
6905 copy into or out of the context. */
6906 if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
6907 {
6908 if (flags & GOVD_MAP)
6909 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6910 else if (flags & GOVD_PRIVATE)
6911 nflags = GOVD_PRIVATE;
6912 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6913 && (flags & GOVD_FIRSTPRIVATE))
6914 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6915 else
6916 nflags = GOVD_FIRSTPRIVATE;
6917 nflags |= flags & GOVD_SEEN;
6918 t = DECL_VALUE_EXPR (decl);
6919 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6920 t = TREE_OPERAND (t, 0);
6921 gcc_assert (DECL_P (t));
6922 omp_add_variable (ctx, t, nflags);
6923 }
6924
6925 /* Add all of the variable and type parameters (which should have
6926 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6927 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6928 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6929 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6930
6931 /* The variable-sized variable itself is never SHARED, only some form
6932 of PRIVATE. The sharing would take place via the pointer variable
6933 which we remapped above. */
6934 if (flags & GOVD_SHARED)
6935 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6936 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6937
6938 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6939 alloca statement we generate for the variable, so make sure it
6940 is available. This isn't automatically needed for the SHARED
6941 case, since we won't be allocating local storage then.
6942 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6943 in this case omp_notice_variable will be called later
6944 on when it is gimplified. */
6945 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6946 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6947 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6948 }
6949 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6950 && lang_hooks.decls.omp_privatize_by_reference (decl))
6951 {
6952 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6953
6954 /* Similar to the direct variable sized case above, we'll need the
6955 size of references being privatized. */
6956 if ((flags & GOVD_SHARED) == 0)
6957 {
6958 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6959 if (DECL_P (t))
6960 omp_notice_variable (ctx, t, true);
6961 }
6962 }
6963
6964 if (n != NULL)
6965 n->value |= flags;
6966 else
6967 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6968
6969 /* For reductions clauses in OpenACC loop directives, by default create a
6970 copy clause on the enclosing parallel construct for carrying back the
6971 results. */
6972 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6973 {
6974 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6975 while (outer_ctx)
6976 {
6977 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6978 if (n != NULL)
6979 {
6980 /* Ignore local variables and explicitly declared clauses. */
6981 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6982 break;
6983 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6984 {
6985 /* According to the OpenACC spec, such a reduction variable
6986 should already have a copy map on a kernels construct,
6987 verify that here. */
6988 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6989 && (n->value & GOVD_MAP));
6990 }
6991 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6992 {
6993 /* Remove firstprivate and make it a copy map. */
6994 n->value &= ~GOVD_FIRSTPRIVATE;
6995 n->value |= GOVD_MAP;
6996 }
6997 }
6998 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6999 {
7000 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
7001 GOVD_MAP | GOVD_SEEN);
7002 break;
7003 }
7004 outer_ctx = outer_ctx->outer_context;
7005 }
7006 }
7007 }
7008
7009 /* Notice a threadprivate variable DECL used in OMP context CTX.
7010 This just prints out diagnostics about threadprivate variable uses
7011 in untied tasks. If DECL2 is non-NULL, prevent this warning
7012 on that variable. */
7013
7014 static bool
7015 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
7016 tree decl2)
7017 {
7018 splay_tree_node n;
7019 struct gimplify_omp_ctx *octx;
7020
7021 for (octx = ctx; octx; octx = octx->outer_context)
7022 if ((octx->region_type & ORT_TARGET) != 0)
7023 {
7024 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
7025 if (n == NULL)
7026 {
7027 error ("threadprivate variable %qE used in target region",
7028 DECL_NAME (decl));
7029 error_at (octx->location, "enclosing target region");
7030 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
7031 }
7032 if (decl2)
7033 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
7034 }
7035
7036 if (ctx->region_type != ORT_UNTIED_TASK)
7037 return false;
7038 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7039 if (n == NULL)
7040 {
7041 error ("threadprivate variable %qE used in untied task",
7042 DECL_NAME (decl));
7043 error_at (ctx->location, "enclosing task");
7044 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
7045 }
7046 if (decl2)
7047 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
7048 return false;
7049 }
7050
7051 /* Return true if global var DECL is device resident. */
7052
7053 static bool
7054 device_resident_p (tree decl)
7055 {
7056 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
7057
7058 if (!attr)
7059 return false;
7060
7061 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
7062 {
7063 tree c = TREE_VALUE (t);
7064 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
7065 return true;
7066 }
7067
7068 return false;
7069 }
7070
7071 /* Return true if DECL has an ACC DECLARE attribute. */
7072
7073 static bool
7074 is_oacc_declared (tree decl)
7075 {
7076 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
7077 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
7078 return declared != NULL_TREE;
7079 }
7080
7081 /* Determine outer default flags for DECL mentioned in an OMP region
7082 but not declared in an enclosing clause.
7083
7084 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
7085 remapped firstprivate instead of shared. To some extent this is
7086 addressed in omp_firstprivatize_type_sizes, but not
7087 effectively. */
7088
7089 static unsigned
7090 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
7091 bool in_code, unsigned flags)
7092 {
7093 enum omp_clause_default_kind default_kind = ctx->default_kind;
7094 enum omp_clause_default_kind kind;
7095
7096 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7097 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7098 default_kind = kind;
7099
7100 switch (default_kind)
7101 {
7102 case OMP_CLAUSE_DEFAULT_NONE:
7103 {
7104 const char *rtype;
7105
7106 if (ctx->region_type & ORT_PARALLEL)
7107 rtype = "parallel";
7108 else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
7109 rtype = "taskloop";
7110 else if (ctx->region_type & ORT_TASK)
7111 rtype = "task";
7112 else if (ctx->region_type & ORT_TEAMS)
7113 rtype = "teams";
7114 else
7115 gcc_unreachable ();
7116
7117 error ("%qE not specified in enclosing %qs",
7118 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7119 error_at (ctx->location, "enclosing %qs", rtype);
7120 }
7121 /* FALLTHRU */
7122 case OMP_CLAUSE_DEFAULT_SHARED:
7123 flags |= GOVD_SHARED;
7124 break;
7125 case OMP_CLAUSE_DEFAULT_PRIVATE:
7126 flags |= GOVD_PRIVATE;
7127 break;
7128 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7129 flags |= GOVD_FIRSTPRIVATE;
7130 break;
7131 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7132 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7133 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7134 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7135 {
7136 omp_notice_variable (octx, decl, in_code);
7137 for (; octx; octx = octx->outer_context)
7138 {
7139 splay_tree_node n2;
7140
7141 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7142 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7143 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7144 continue;
7145 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7146 {
7147 flags |= GOVD_FIRSTPRIVATE;
7148 goto found_outer;
7149 }
7150 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7151 {
7152 flags |= GOVD_SHARED;
7153 goto found_outer;
7154 }
7155 }
7156 }
7157
7158 if (TREE_CODE (decl) == PARM_DECL
7159 || (!is_global_var (decl)
7160 && DECL_CONTEXT (decl) == current_function_decl))
7161 flags |= GOVD_FIRSTPRIVATE;
7162 else
7163 flags |= GOVD_SHARED;
7164 found_outer:
7165 break;
7166
7167 default:
7168 gcc_unreachable ();
7169 }
7170
7171 return flags;
7172 }
7173
7174
7175 /* Determine outer default flags for DECL mentioned in an OACC region
7176 but not declared in an enclosing clause. */
7177
7178 static unsigned
7179 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7180 {
7181 const char *rkind;
7182 bool on_device = false;
7183 bool declared = is_oacc_declared (decl);
7184 tree type = TREE_TYPE (decl);
7185
7186 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7187 type = TREE_TYPE (type);
7188
7189 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7190 && is_global_var (decl)
7191 && device_resident_p (decl))
7192 {
7193 on_device = true;
7194 flags |= GOVD_MAP_TO_ONLY;
7195 }
7196
7197 switch (ctx->region_type)
7198 {
7199 case ORT_ACC_KERNELS:
7200 rkind = "kernels";
7201
7202 if (AGGREGATE_TYPE_P (type))
7203 {
7204 /* Aggregates default to 'present_or_copy', or 'present'. */
7205 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7206 flags |= GOVD_MAP;
7207 else
7208 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7209 }
7210 else
7211 /* Scalars default to 'copy'. */
7212 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7213
7214 break;
7215
7216 case ORT_ACC_PARALLEL:
7217 rkind = "parallel";
7218
7219 if (on_device || declared)
7220 flags |= GOVD_MAP;
7221 else if (AGGREGATE_TYPE_P (type))
7222 {
7223 /* Aggregates default to 'present_or_copy', or 'present'. */
7224 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7225 flags |= GOVD_MAP;
7226 else
7227 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7228 }
7229 else
7230 /* Scalars default to 'firstprivate'. */
7231 flags |= GOVD_FIRSTPRIVATE;
7232
7233 break;
7234
7235 default:
7236 gcc_unreachable ();
7237 }
7238
7239 if (DECL_ARTIFICIAL (decl))
7240 ; /* We can get compiler-generated decls, and should not complain
7241 about them. */
7242 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7243 {
7244 error ("%qE not specified in enclosing OpenACC %qs construct",
7245 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7246 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7247 }
7248 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7249 ; /* Handled above. */
7250 else
7251 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7252
7253 return flags;
7254 }
7255
7256 /* Record the fact that DECL was used within the OMP context CTX.
7257 IN_CODE is true when real code uses DECL, and false when we should
7258 merely emit default(none) errors. Return true if DECL is going to
7259 be remapped and thus DECL shouldn't be gimplified into its
7260 DECL_VALUE_EXPR (if any). */
7261
7262 static bool
7263 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7264 {
7265 splay_tree_node n;
7266 unsigned flags = in_code ? GOVD_SEEN : 0;
7267 bool ret = false, shared;
7268
7269 if (error_operand_p (decl))
7270 return false;
7271
7272 if (ctx->region_type == ORT_NONE)
7273 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7274
7275 if (is_global_var (decl))
7276 {
7277 /* Threadprivate variables are predetermined. */
7278 if (DECL_THREAD_LOCAL_P (decl))
7279 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7280
7281 if (DECL_HAS_VALUE_EXPR_P (decl))
7282 {
7283 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7284
7285 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7286 return omp_notice_threadprivate_variable (ctx, decl, value);
7287 }
7288
7289 if (gimplify_omp_ctxp->outer_context == NULL
7290 && VAR_P (decl)
7291 && oacc_get_fn_attrib (current_function_decl))
7292 {
7293 location_t loc = DECL_SOURCE_LOCATION (decl);
7294
7295 if (lookup_attribute ("omp declare target link",
7296 DECL_ATTRIBUTES (decl)))
7297 {
7298 error_at (loc,
7299 "%qE with %<link%> clause used in %<routine%> function",
7300 DECL_NAME (decl));
7301 return false;
7302 }
7303 else if (!lookup_attribute ("omp declare target",
7304 DECL_ATTRIBUTES (decl)))
7305 {
7306 error_at (loc,
7307 "%qE requires a %<declare%> directive for use "
7308 "in a %<routine%> function", DECL_NAME (decl));
7309 return false;
7310 }
7311 }
7312 }
7313
7314 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7315 if ((ctx->region_type & ORT_TARGET) != 0)
7316 {
7317 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7318 if (n == NULL)
7319 {
7320 unsigned nflags = flags;
7321 if ((ctx->region_type & ORT_ACC) == 0)
7322 {
7323 bool is_declare_target = false;
7324 if (is_global_var (decl)
7325 && varpool_node::get_create (decl)->offloadable)
7326 {
7327 struct gimplify_omp_ctx *octx;
7328 for (octx = ctx->outer_context;
7329 octx; octx = octx->outer_context)
7330 {
7331 n = splay_tree_lookup (octx->variables,
7332 (splay_tree_key)decl);
7333 if (n
7334 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7335 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7336 break;
7337 }
7338 is_declare_target = octx == NULL;
7339 }
7340 if (!is_declare_target)
7341 {
7342 int gdmk;
7343 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7344 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7345 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7346 == POINTER_TYPE)))
7347 gdmk = GDMK_POINTER;
7348 else if (lang_hooks.decls.omp_scalar_p (decl))
7349 gdmk = GDMK_SCALAR;
7350 else
7351 gdmk = GDMK_AGGREGATE;
7352 if (ctx->defaultmap[gdmk] == 0)
7353 {
7354 tree d = lang_hooks.decls.omp_report_decl (decl);
7355 error ("%qE not specified in enclosing %<target%>",
7356 DECL_NAME (d));
7357 error_at (ctx->location, "enclosing %<target%>");
7358 }
7359 else if (ctx->defaultmap[gdmk]
7360 & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
7361 nflags |= ctx->defaultmap[gdmk];
7362 else
7363 {
7364 gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
7365 nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
7366 }
7367 }
7368 }
7369
7370 struct gimplify_omp_ctx *octx = ctx->outer_context;
7371 if ((ctx->region_type & ORT_ACC) && octx)
7372 {
7373 /* Look in outer OpenACC contexts, to see if there's a
7374 data attribute for this variable. */
7375 omp_notice_variable (octx, decl, in_code);
7376
7377 for (; octx; octx = octx->outer_context)
7378 {
7379 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7380 break;
7381 splay_tree_node n2
7382 = splay_tree_lookup (octx->variables,
7383 (splay_tree_key) decl);
7384 if (n2)
7385 {
7386 if (octx->region_type == ORT_ACC_HOST_DATA)
7387 error ("variable %qE declared in enclosing "
7388 "%<host_data%> region", DECL_NAME (decl));
7389 nflags |= GOVD_MAP;
7390 if (octx->region_type == ORT_ACC_DATA
7391 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7392 nflags |= GOVD_MAP_0LEN_ARRAY;
7393 goto found_outer;
7394 }
7395 }
7396 }
7397
7398 if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
7399 | GOVD_MAP_ALLOC_ONLY)) == flags)
7400 {
7401 tree type = TREE_TYPE (decl);
7402
7403 if (gimplify_omp_ctxp->target_firstprivatize_array_bases
7404 && lang_hooks.decls.omp_privatize_by_reference (decl))
7405 type = TREE_TYPE (type);
7406 if (!lang_hooks.types.omp_mappable_type (type))
7407 {
7408 error ("%qD referenced in target region does not have "
7409 "a mappable type", decl);
7410 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7411 }
7412 else
7413 {
7414 if ((ctx->region_type & ORT_ACC) != 0)
7415 nflags = oacc_default_clause (ctx, decl, flags);
7416 else
7417 nflags |= GOVD_MAP;
7418 }
7419 }
7420 found_outer:
7421 omp_add_variable (ctx, decl, nflags);
7422 }
7423 else
7424 {
7425 /* If nothing changed, there's nothing left to do. */
7426 if ((n->value & flags) == flags)
7427 return ret;
7428 flags |= n->value;
7429 n->value = flags;
7430 }
7431 goto do_outer;
7432 }
7433
7434 if (n == NULL)
7435 {
7436 if (ctx->region_type == ORT_WORKSHARE
7437 || ctx->region_type == ORT_TASKGROUP
7438 || ctx->region_type == ORT_SIMD
7439 || ctx->region_type == ORT_ACC
7440 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7441 goto do_outer;
7442
7443 flags = omp_default_clause (ctx, decl, in_code, flags);
7444
7445 if ((flags & GOVD_PRIVATE)
7446 && lang_hooks.decls.omp_private_outer_ref (decl))
7447 flags |= GOVD_PRIVATE_OUTER_REF;
7448
7449 omp_add_variable (ctx, decl, flags);
7450
7451 shared = (flags & GOVD_SHARED) != 0;
7452 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7453 goto do_outer;
7454 }
7455
7456 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7457 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7458 && DECL_SIZE (decl))
7459 {
7460 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7461 {
7462 splay_tree_node n2;
7463 tree t = DECL_VALUE_EXPR (decl);
7464 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7465 t = TREE_OPERAND (t, 0);
7466 gcc_assert (DECL_P (t));
7467 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7468 n2->value |= GOVD_SEEN;
7469 }
7470 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7471 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7472 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7473 != INTEGER_CST))
7474 {
7475 splay_tree_node n2;
7476 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7477 gcc_assert (DECL_P (t));
7478 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7479 if (n2)
7480 omp_notice_variable (ctx, t, true);
7481 }
7482 }
7483
7484 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7485 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7486
7487 /* If nothing changed, there's nothing left to do. */
7488 if ((n->value & flags) == flags)
7489 return ret;
7490 flags |= n->value;
7491 n->value = flags;
7492
7493 do_outer:
7494 /* If the variable is private in the current context, then we don't
7495 need to propagate anything to an outer context. */
7496 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7497 return ret;
7498 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7499 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7500 return ret;
7501 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7502 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7503 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7504 return ret;
7505 if (ctx->outer_context
7506 && omp_notice_variable (ctx->outer_context, decl, in_code))
7507 return true;
7508 return ret;
7509 }
7510
7511 /* Verify that DECL is private within CTX. If there's specific information
7512 to the contrary in the innermost scope, generate an error. */
7513
7514 static bool
7515 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7516 {
7517 splay_tree_node n;
7518
7519 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7520 if (n != NULL)
7521 {
7522 if (n->value & GOVD_SHARED)
7523 {
7524 if (ctx == gimplify_omp_ctxp)
7525 {
7526 if (simd)
7527 error ("iteration variable %qE is predetermined linear",
7528 DECL_NAME (decl));
7529 else
7530 error ("iteration variable %qE should be private",
7531 DECL_NAME (decl));
7532 n->value = GOVD_PRIVATE;
7533 return true;
7534 }
7535 else
7536 return false;
7537 }
7538 else if ((n->value & GOVD_EXPLICIT) != 0
7539 && (ctx == gimplify_omp_ctxp
7540 || (ctx->region_type == ORT_COMBINED_PARALLEL
7541 && gimplify_omp_ctxp->outer_context == ctx)))
7542 {
7543 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7544 error ("iteration variable %qE should not be firstprivate",
7545 DECL_NAME (decl));
7546 else if ((n->value & GOVD_REDUCTION) != 0)
7547 error ("iteration variable %qE should not be reduction",
7548 DECL_NAME (decl));
7549 else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
7550 error ("iteration variable %qE should not be linear",
7551 DECL_NAME (decl));
7552 }
7553 return (ctx == gimplify_omp_ctxp
7554 || (ctx->region_type == ORT_COMBINED_PARALLEL
7555 && gimplify_omp_ctxp->outer_context == ctx));
7556 }
7557
7558 if (ctx->region_type != ORT_WORKSHARE
7559 && ctx->region_type != ORT_TASKGROUP
7560 && ctx->region_type != ORT_SIMD
7561 && ctx->region_type != ORT_ACC)
7562 return false;
7563 else if (ctx->outer_context)
7564 return omp_is_private (ctx->outer_context, decl, simd);
7565 return false;
7566 }
7567
7568 /* Return true if DECL is private within a parallel region
7569 that binds to the current construct's context or in parallel
7570 region's REDUCTION clause. */
7571
7572 static bool
7573 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7574 {
7575 splay_tree_node n;
7576
7577 do
7578 {
7579 ctx = ctx->outer_context;
7580 if (ctx == NULL)
7581 {
7582 if (is_global_var (decl))
7583 return false;
7584
7585 /* References might be private, but might be shared too,
7586 when checking for copyprivate, assume they might be
7587 private, otherwise assume they might be shared. */
7588 if (copyprivate)
7589 return true;
7590
7591 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7592 return false;
7593
7594 /* Treat C++ privatized non-static data members outside
7595 of the privatization the same. */
7596 if (omp_member_access_dummy_var (decl))
7597 return false;
7598
7599 return true;
7600 }
7601
7602 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7603
7604 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7605 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7606 continue;
7607
7608 if (n != NULL)
7609 {
7610 if ((n->value & GOVD_LOCAL) != 0
7611 && omp_member_access_dummy_var (decl))
7612 return false;
7613 return (n->value & GOVD_SHARED) == 0;
7614 }
7615 }
7616 while (ctx->region_type == ORT_WORKSHARE
7617 || ctx->region_type == ORT_TASKGROUP
7618 || ctx->region_type == ORT_SIMD
7619 || ctx->region_type == ORT_ACC);
7620 return false;
7621 }
7622
7623 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7624
7625 static tree
7626 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7627 {
7628 tree t = *tp;
7629
7630 /* If this node has been visited, unmark it and keep looking. */
7631 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7632 return t;
7633
7634 if (IS_TYPE_OR_DECL_P (t))
7635 *walk_subtrees = 0;
7636 return NULL_TREE;
7637 }
7638
7639 /* If *LIST_P contains any OpenMP depend clauses with iterators,
7640 lower all the depend clauses by populating corresponding depend
7641 array. Returns 0 if there are no such depend clauses, or
7642 2 if all depend clauses should be removed, 1 otherwise. */
7643
7644 static int
7645 gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
7646 {
7647 tree c;
7648 gimple *g;
7649 size_t n[4] = { 0, 0, 0, 0 };
7650 bool unused[4];
7651 tree counts[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
7652 tree last_iter = NULL_TREE, last_count = NULL_TREE;
7653 size_t i, j;
7654 location_t first_loc = UNKNOWN_LOCATION;
7655
7656 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7657 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7658 {
7659 switch (OMP_CLAUSE_DEPEND_KIND (c))
7660 {
7661 case OMP_CLAUSE_DEPEND_IN:
7662 i = 2;
7663 break;
7664 case OMP_CLAUSE_DEPEND_OUT:
7665 case OMP_CLAUSE_DEPEND_INOUT:
7666 i = 0;
7667 break;
7668 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7669 i = 1;
7670 break;
7671 case OMP_CLAUSE_DEPEND_DEPOBJ:
7672 i = 3;
7673 break;
7674 case OMP_CLAUSE_DEPEND_SOURCE:
7675 case OMP_CLAUSE_DEPEND_SINK:
7676 continue;
7677 default:
7678 gcc_unreachable ();
7679 }
7680 tree t = OMP_CLAUSE_DECL (c);
7681 if (first_loc == UNKNOWN_LOCATION)
7682 first_loc = OMP_CLAUSE_LOCATION (c);
7683 if (TREE_CODE (t) == TREE_LIST
7684 && TREE_PURPOSE (t)
7685 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7686 {
7687 if (TREE_PURPOSE (t) != last_iter)
7688 {
7689 tree tcnt = size_one_node;
7690 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7691 {
7692 if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
7693 is_gimple_val, fb_rvalue) == GS_ERROR
7694 || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
7695 is_gimple_val, fb_rvalue) == GS_ERROR
7696 || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
7697 is_gimple_val, fb_rvalue) == GS_ERROR
7698 || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
7699 is_gimple_val, fb_rvalue)
7700 == GS_ERROR))
7701 return 2;
7702 tree var = TREE_VEC_ELT (it, 0);
7703 tree begin = TREE_VEC_ELT (it, 1);
7704 tree end = TREE_VEC_ELT (it, 2);
7705 tree step = TREE_VEC_ELT (it, 3);
7706 tree orig_step = TREE_VEC_ELT (it, 4);
7707 tree type = TREE_TYPE (var);
7708 tree stype = TREE_TYPE (step);
7709 location_t loc = DECL_SOURCE_LOCATION (var);
7710 tree endmbegin;
7711 /* Compute count for this iterator as
7712 orig_step > 0
7713 ? (begin < end ? (end - begin + (step - 1)) / step : 0)
7714 : (begin > end ? (end - begin + (step + 1)) / step : 0)
7715 and compute product of those for the entire depend
7716 clause. */
7717 if (POINTER_TYPE_P (type))
7718 endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR,
7719 stype, end, begin);
7720 else
7721 endmbegin = fold_build2_loc (loc, MINUS_EXPR, type,
7722 end, begin);
7723 tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype,
7724 step,
7725 build_int_cst (stype, 1));
7726 tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
7727 build_int_cst (stype, 1));
7728 tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
7729 unshare_expr (endmbegin),
7730 stepm1);
7731 pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7732 pos, step);
7733 tree neg = fold_build2_loc (loc, PLUS_EXPR, stype,
7734 endmbegin, stepp1);
7735 if (TYPE_UNSIGNED (stype))
7736 {
7737 neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
7738 step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
7739 }
7740 neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7741 neg, step);
7742 step = NULL_TREE;
7743 tree cond = fold_build2_loc (loc, LT_EXPR,
7744 boolean_type_node,
7745 begin, end);
7746 pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
7747 build_int_cst (stype, 0));
7748 cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
7749 end, begin);
7750 neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
7751 build_int_cst (stype, 0));
7752 tree osteptype = TREE_TYPE (orig_step);
7753 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7754 orig_step,
7755 build_int_cst (osteptype, 0));
7756 tree cnt = fold_build3_loc (loc, COND_EXPR, stype,
7757 cond, pos, neg);
7758 cnt = fold_convert_loc (loc, sizetype, cnt);
7759 if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
7760 fb_rvalue) == GS_ERROR)
7761 return 2;
7762 tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
7763 }
7764 if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val,
7765 fb_rvalue) == GS_ERROR)
7766 return 2;
7767 last_iter = TREE_PURPOSE (t);
7768 last_count = tcnt;
7769 }
7770 if (counts[i] == NULL_TREE)
7771 counts[i] = last_count;
7772 else
7773 counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
7774 PLUS_EXPR, counts[i], last_count);
7775 }
7776 else
7777 n[i]++;
7778 }
7779 for (i = 0; i < 4; i++)
7780 if (counts[i])
7781 break;
7782 if (i == 4)
7783 return 0;
7784
7785 tree total = size_zero_node;
7786 for (i = 0; i < 4; i++)
7787 {
7788 unused[i] = counts[i] == NULL_TREE && n[i] == 0;
7789 if (counts[i] == NULL_TREE)
7790 counts[i] = size_zero_node;
7791 if (n[i])
7792 counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
7793 if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
7794 fb_rvalue) == GS_ERROR)
7795 return 2;
7796 total = size_binop (PLUS_EXPR, total, counts[i]);
7797 }
7798
7799 if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
7800 == GS_ERROR)
7801 return 2;
7802 bool is_old = unused[1] && unused[3];
7803 tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
7804 size_int (is_old ? 1 : 4));
7805 tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
7806 tree array = create_tmp_var_raw (type);
7807 TREE_ADDRESSABLE (array) = 1;
7808 if (TREE_CODE (totalpx) != INTEGER_CST)
7809 {
7810 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
7811 gimplify_type_sizes (TREE_TYPE (array), pre_p);
7812 if (gimplify_omp_ctxp)
7813 {
7814 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7815 while (ctx
7816 && (ctx->region_type == ORT_WORKSHARE
7817 || ctx->region_type == ORT_TASKGROUP
7818 || ctx->region_type == ORT_SIMD
7819 || ctx->region_type == ORT_ACC))
7820 ctx = ctx->outer_context;
7821 if (ctx)
7822 omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
7823 }
7824 gimplify_vla_decl (array, pre_p);
7825 }
7826 else
7827 gimple_add_tmp_var (array);
7828 tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
7829 NULL_TREE);
7830 tree tem;
7831 if (!is_old)
7832 {
7833 tem = build2 (MODIFY_EXPR, void_type_node, r,
7834 build_int_cst (ptr_type_node, 0));
7835 gimplify_and_add (tem, pre_p);
7836 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
7837 NULL_TREE);
7838 }
7839 tem = build2 (MODIFY_EXPR, void_type_node, r,
7840 fold_convert (ptr_type_node, total));
7841 gimplify_and_add (tem, pre_p);
7842 for (i = 1; i < (is_old ? 2 : 4); i++)
7843 {
7844 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
7845 NULL_TREE, NULL_TREE);
7846 tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
7847 gimplify_and_add (tem, pre_p);
7848 }
7849
7850 tree cnts[4];
7851 for (j = 4; j; j--)
7852 if (!unused[j - 1])
7853 break;
7854 for (i = 0; i < 4; i++)
7855 {
7856 if (i && (i >= j || unused[i - 1]))
7857 {
7858 cnts[i] = cnts[i - 1];
7859 continue;
7860 }
7861 cnts[i] = create_tmp_var (sizetype);
7862 if (i == 0)
7863 g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
7864 else
7865 {
7866 tree t;
7867 if (is_old)
7868 t = size_binop (PLUS_EXPR, counts[0], size_int (2));
7869 else
7870 t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
7871 if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
7872 == GS_ERROR)
7873 return 2;
7874 g = gimple_build_assign (cnts[i], t);
7875 }
7876 gimple_seq_add_stmt (pre_p, g);
7877 }
7878
7879 last_iter = NULL_TREE;
7880 tree last_bind = NULL_TREE;
7881 tree *last_body = NULL;
7882 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7883 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7884 {
7885 switch (OMP_CLAUSE_DEPEND_KIND (c))
7886 {
7887 case OMP_CLAUSE_DEPEND_IN:
7888 i = 2;
7889 break;
7890 case OMP_CLAUSE_DEPEND_OUT:
7891 case OMP_CLAUSE_DEPEND_INOUT:
7892 i = 0;
7893 break;
7894 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7895 i = 1;
7896 break;
7897 case OMP_CLAUSE_DEPEND_DEPOBJ:
7898 i = 3;
7899 break;
7900 case OMP_CLAUSE_DEPEND_SOURCE:
7901 case OMP_CLAUSE_DEPEND_SINK:
7902 continue;
7903 default:
7904 gcc_unreachable ();
7905 }
7906 tree t = OMP_CLAUSE_DECL (c);
7907 if (TREE_CODE (t) == TREE_LIST
7908 && TREE_PURPOSE (t)
7909 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7910 {
7911 if (TREE_PURPOSE (t) != last_iter)
7912 {
7913 if (last_bind)
7914 gimplify_and_add (last_bind, pre_p);
7915 tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
7916 last_bind = build3 (BIND_EXPR, void_type_node,
7917 BLOCK_VARS (block), NULL, block);
7918 TREE_SIDE_EFFECTS (last_bind) = 1;
7919 SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
7920 tree *p = &BIND_EXPR_BODY (last_bind);
7921 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7922 {
7923 tree var = TREE_VEC_ELT (it, 0);
7924 tree begin = TREE_VEC_ELT (it, 1);
7925 tree end = TREE_VEC_ELT (it, 2);
7926 tree step = TREE_VEC_ELT (it, 3);
7927 tree orig_step = TREE_VEC_ELT (it, 4);
7928 tree type = TREE_TYPE (var);
7929 location_t loc = DECL_SOURCE_LOCATION (var);
7930 /* Emit:
7931 var = begin;
7932 goto cond_label;
7933 beg_label:
7934 ...
7935 var = var + step;
7936 cond_label:
7937 if (orig_step > 0) {
7938 if (var < end) goto beg_label;
7939 } else {
7940 if (var > end) goto beg_label;
7941 }
7942 for each iterator, with inner iterators added to
7943 the ... above. */
7944 tree beg_label = create_artificial_label (loc);
7945 tree cond_label = NULL_TREE;
7946 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7947 var, begin);
7948 append_to_statement_list_force (tem, p);
7949 tem = build_and_jump (&cond_label);
7950 append_to_statement_list_force (tem, p);
7951 tem = build1 (LABEL_EXPR, void_type_node, beg_label);
7952 append_to_statement_list (tem, p);
7953 tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7954 NULL_TREE, NULL_TREE);
7955 TREE_SIDE_EFFECTS (bind) = 1;
7956 SET_EXPR_LOCATION (bind, loc);
7957 append_to_statement_list_force (bind, p);
7958 if (POINTER_TYPE_P (type))
7959 tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
7960 var, fold_convert_loc (loc, sizetype,
7961 step));
7962 else
7963 tem = build2_loc (loc, PLUS_EXPR, type, var, step);
7964 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7965 var, tem);
7966 append_to_statement_list_force (tem, p);
7967 tem = build1 (LABEL_EXPR, void_type_node, cond_label);
7968 append_to_statement_list (tem, p);
7969 tree cond = fold_build2_loc (loc, LT_EXPR,
7970 boolean_type_node,
7971 var, end);
7972 tree pos
7973 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7974 cond, build_and_jump (&beg_label),
7975 void_node);
7976 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7977 var, end);
7978 tree neg
7979 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7980 cond, build_and_jump (&beg_label),
7981 void_node);
7982 tree osteptype = TREE_TYPE (orig_step);
7983 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7984 orig_step,
7985 build_int_cst (osteptype, 0));
7986 tem = fold_build3_loc (loc, COND_EXPR, void_type_node,
7987 cond, pos, neg);
7988 append_to_statement_list_force (tem, p);
7989 p = &BIND_EXPR_BODY (bind);
7990 }
7991 last_body = p;
7992 }
7993 last_iter = TREE_PURPOSE (t);
7994 if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
7995 {
7996 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
7997 0), last_body);
7998 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
7999 }
8000 if (error_operand_p (TREE_VALUE (t)))
8001 return 2;
8002 TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
8003 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
8004 NULL_TREE, NULL_TREE);
8005 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
8006 void_type_node, r, TREE_VALUE (t));
8007 append_to_statement_list_force (tem, last_body);
8008 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
8009 void_type_node, cnts[i],
8010 size_binop (PLUS_EXPR, cnts[i], size_int (1)));
8011 append_to_statement_list_force (tem, last_body);
8012 TREE_VALUE (t) = null_pointer_node;
8013 }
8014 else
8015 {
8016 if (last_bind)
8017 {
8018 gimplify_and_add (last_bind, pre_p);
8019 last_bind = NULL_TREE;
8020 }
8021 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8022 {
8023 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8024 NULL, is_gimple_val, fb_rvalue);
8025 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8026 }
8027 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8028 return 2;
8029 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8030 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8031 is_gimple_val, fb_rvalue) == GS_ERROR)
8032 return 2;
8033 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
8034 NULL_TREE, NULL_TREE);
8035 tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
8036 gimplify_and_add (tem, pre_p);
8037 g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR, cnts[i],
8038 size_int (1)));
8039 gimple_seq_add_stmt (pre_p, g);
8040 }
8041 }
8042 if (last_bind)
8043 gimplify_and_add (last_bind, pre_p);
8044 tree cond = boolean_false_node;
8045 if (is_old)
8046 {
8047 if (!unused[0])
8048 cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
8049 size_binop_loc (first_loc, PLUS_EXPR, counts[0],
8050 size_int (2)));
8051 if (!unused[2])
8052 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
8053 build2_loc (first_loc, NE_EXPR, boolean_type_node,
8054 cnts[2],
8055 size_binop_loc (first_loc, PLUS_EXPR,
8056 totalpx,
8057 size_int (1))));
8058 }
8059 else
8060 {
8061 tree prev = size_int (5);
8062 for (i = 0; i < 4; i++)
8063 {
8064 if (unused[i])
8065 continue;
8066 prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
8067 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
8068 build2_loc (first_loc, NE_EXPR, boolean_type_node,
8069 cnts[i], unshare_expr (prev)));
8070 }
8071 }
8072 tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
8073 build_call_expr_loc (first_loc,
8074 builtin_decl_explicit (BUILT_IN_TRAP),
8075 0), void_node);
8076 gimplify_and_add (tem, pre_p);
8077 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
8078 OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
8079 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
8080 OMP_CLAUSE_CHAIN (c) = *list_p;
8081 *list_p = c;
8082 return 1;
8083 }
8084
8085 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
8086 and previous omp contexts. */
8087
8088 static void
8089 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
8090 enum omp_region_type region_type,
8091 enum tree_code code)
8092 {
8093 struct gimplify_omp_ctx *ctx, *outer_ctx;
8094 tree c;
8095 hash_map<tree, tree> *struct_map_to_clause = NULL;
8096 tree *prev_list_p = NULL, *orig_list_p = list_p;
8097 int handled_depend_iterators = -1;
8098 int nowait = -1;
8099
8100 ctx = new_omp_context (region_type);
8101 outer_ctx = ctx->outer_context;
8102 if (code == OMP_TARGET)
8103 {
8104 if (!lang_GNU_Fortran ())
8105 ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
8106 ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
8107 }
8108 if (!lang_GNU_Fortran ())
8109 switch (code)
8110 {
8111 case OMP_TARGET:
8112 case OMP_TARGET_DATA:
8113 case OMP_TARGET_ENTER_DATA:
8114 case OMP_TARGET_EXIT_DATA:
8115 case OACC_DECLARE:
8116 case OACC_HOST_DATA:
8117 case OACC_PARALLEL:
8118 case OACC_KERNELS:
8119 ctx->target_firstprivatize_array_bases = true;
8120 default:
8121 break;
8122 }
8123
8124 while ((c = *list_p) != NULL)
8125 {
8126 bool remove = false;
8127 bool notice_outer = true;
8128 const char *check_non_private = NULL;
8129 unsigned int flags;
8130 tree decl;
8131
8132 switch (OMP_CLAUSE_CODE (c))
8133 {
8134 case OMP_CLAUSE_PRIVATE:
8135 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
8136 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
8137 {
8138 flags |= GOVD_PRIVATE_OUTER_REF;
8139 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
8140 }
8141 else
8142 notice_outer = false;
8143 goto do_add;
8144 case OMP_CLAUSE_SHARED:
8145 flags = GOVD_SHARED | GOVD_EXPLICIT;
8146 goto do_add;
8147 case OMP_CLAUSE_FIRSTPRIVATE:
8148 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8149 check_non_private = "firstprivate";
8150 goto do_add;
8151 case OMP_CLAUSE_LASTPRIVATE:
8152 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8153 switch (code)
8154 {
8155 case OMP_DISTRIBUTE:
8156 error_at (OMP_CLAUSE_LOCATION (c),
8157 "conditional %<lastprivate%> clause on "
8158 "%qs construct", "distribute");
8159 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8160 break;
8161 case OMP_TASKLOOP:
8162 error_at (OMP_CLAUSE_LOCATION (c),
8163 "conditional %<lastprivate%> clause on "
8164 "%qs construct", "taskloop");
8165 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8166 break;
8167 default:
8168 break;
8169 }
8170 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
8171 check_non_private = "lastprivate";
8172 decl = OMP_CLAUSE_DECL (c);
8173 if (error_operand_p (decl))
8174 goto do_add;
8175 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
8176 && !lang_hooks.decls.omp_scalar_p (decl))
8177 {
8178 error_at (OMP_CLAUSE_LOCATION (c),
8179 "non-scalar variable %qD in conditional "
8180 "%<lastprivate%> clause", decl);
8181 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8182 }
8183 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8184 flags |= GOVD_LASTPRIVATE_CONDITIONAL;
8185 if (outer_ctx
8186 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
8187 || ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
8188 == ORT_COMBINED_TEAMS))
8189 && splay_tree_lookup (outer_ctx->variables,
8190 (splay_tree_key) decl) == NULL)
8191 {
8192 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
8193 if (outer_ctx->outer_context)
8194 omp_notice_variable (outer_ctx->outer_context, decl, true);
8195 }
8196 else if (outer_ctx
8197 && (outer_ctx->region_type & ORT_TASK) != 0
8198 && outer_ctx->combined_loop
8199 && splay_tree_lookup (outer_ctx->variables,
8200 (splay_tree_key) decl) == NULL)
8201 {
8202 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8203 if (outer_ctx->outer_context)
8204 omp_notice_variable (outer_ctx->outer_context, decl, true);
8205 }
8206 else if (outer_ctx
8207 && (outer_ctx->region_type == ORT_WORKSHARE
8208 || outer_ctx->region_type == ORT_ACC)
8209 && outer_ctx->combined_loop
8210 && splay_tree_lookup (outer_ctx->variables,
8211 (splay_tree_key) decl) == NULL
8212 && !omp_check_private (outer_ctx, decl, false))
8213 {
8214 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8215 if (outer_ctx->outer_context
8216 && (outer_ctx->outer_context->region_type
8217 == ORT_COMBINED_PARALLEL)
8218 && splay_tree_lookup (outer_ctx->outer_context->variables,
8219 (splay_tree_key) decl) == NULL)
8220 {
8221 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
8222 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
8223 if (octx->outer_context)
8224 {
8225 octx = octx->outer_context;
8226 if (octx->region_type == ORT_WORKSHARE
8227 && octx->combined_loop
8228 && splay_tree_lookup (octx->variables,
8229 (splay_tree_key) decl) == NULL
8230 && !omp_check_private (octx, decl, false))
8231 {
8232 omp_add_variable (octx, decl,
8233 GOVD_LASTPRIVATE | GOVD_SEEN);
8234 octx = octx->outer_context;
8235 if (octx
8236 && ((octx->region_type & ORT_COMBINED_TEAMS)
8237 == ORT_COMBINED_TEAMS)
8238 && (splay_tree_lookup (octx->variables,
8239 (splay_tree_key) decl)
8240 == NULL))
8241 {
8242 omp_add_variable (octx, decl,
8243 GOVD_SHARED | GOVD_SEEN);
8244 octx = octx->outer_context;
8245 }
8246 }
8247 if (octx)
8248 omp_notice_variable (octx, decl, true);
8249 }
8250 }
8251 else if (outer_ctx->outer_context)
8252 omp_notice_variable (outer_ctx->outer_context, decl, true);
8253 }
8254 goto do_add;
8255 case OMP_CLAUSE_REDUCTION:
8256 if (OMP_CLAUSE_REDUCTION_TASK (c))
8257 {
8258 if (region_type == ORT_WORKSHARE)
8259 {
8260 if (nowait == -1)
8261 nowait = omp_find_clause (*list_p,
8262 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8263 if (nowait
8264 && (outer_ctx == NULL
8265 || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
8266 {
8267 error_at (OMP_CLAUSE_LOCATION (c),
8268 "%<task%> reduction modifier on a construct "
8269 "with a %<nowait%> clause");
8270 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8271 }
8272 }
8273 else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
8274 {
8275 error_at (OMP_CLAUSE_LOCATION (c),
8276 "invalid %<task%> reduction modifier on construct "
8277 "other than %<parallel%>, %<for%> or %<sections%>");
8278 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8279 }
8280 }
8281 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8282 switch (code)
8283 {
8284 case OMP_SECTIONS:
8285 error_at (OMP_CLAUSE_LOCATION (c),
8286 "%<inscan%> %<reduction%> clause on "
8287 "%qs construct", "sections");
8288 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8289 break;
8290 case OMP_PARALLEL:
8291 error_at (OMP_CLAUSE_LOCATION (c),
8292 "%<inscan%> %<reduction%> clause on "
8293 "%qs construct", "parallel");
8294 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8295 break;
8296 case OMP_TEAMS:
8297 error_at (OMP_CLAUSE_LOCATION (c),
8298 "%<inscan%> %<reduction%> clause on "
8299 "%qs construct", "teams");
8300 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8301 break;
8302 case OMP_TASKLOOP:
8303 error_at (OMP_CLAUSE_LOCATION (c),
8304 "%<inscan%> %<reduction%> clause on "
8305 "%qs construct", "taskloop");
8306 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8307 break;
8308 default:
8309 break;
8310 }
8311 /* FALLTHRU */
8312 case OMP_CLAUSE_IN_REDUCTION:
8313 case OMP_CLAUSE_TASK_REDUCTION:
8314 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
8315 /* OpenACC permits reductions on private variables. */
8316 if (!(region_type & ORT_ACC)
8317 /* taskgroup is actually not a worksharing region. */
8318 && code != OMP_TASKGROUP)
8319 check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8320 decl = OMP_CLAUSE_DECL (c);
8321 if (TREE_CODE (decl) == MEM_REF)
8322 {
8323 tree type = TREE_TYPE (decl);
8324 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
8325 NULL, is_gimple_val, fb_rvalue, false)
8326 == GS_ERROR)
8327 {
8328 remove = true;
8329 break;
8330 }
8331 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8332 if (DECL_P (v))
8333 {
8334 omp_firstprivatize_variable (ctx, v);
8335 omp_notice_variable (ctx, v, true);
8336 }
8337 decl = TREE_OPERAND (decl, 0);
8338 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
8339 {
8340 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
8341 NULL, is_gimple_val, fb_rvalue, false)
8342 == GS_ERROR)
8343 {
8344 remove = true;
8345 break;
8346 }
8347 v = TREE_OPERAND (decl, 1);
8348 if (DECL_P (v))
8349 {
8350 omp_firstprivatize_variable (ctx, v);
8351 omp_notice_variable (ctx, v, true);
8352 }
8353 decl = TREE_OPERAND (decl, 0);
8354 }
8355 if (TREE_CODE (decl) == ADDR_EXPR
8356 || TREE_CODE (decl) == INDIRECT_REF)
8357 decl = TREE_OPERAND (decl, 0);
8358 }
8359 goto do_add_decl;
8360 case OMP_CLAUSE_LINEAR:
8361 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
8362 is_gimple_val, fb_rvalue) == GS_ERROR)
8363 {
8364 remove = true;
8365 break;
8366 }
8367 else
8368 {
8369 if (code == OMP_SIMD
8370 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8371 {
8372 struct gimplify_omp_ctx *octx = outer_ctx;
8373 if (octx
8374 && octx->region_type == ORT_WORKSHARE
8375 && octx->combined_loop
8376 && !octx->distribute)
8377 {
8378 if (octx->outer_context
8379 && (octx->outer_context->region_type
8380 == ORT_COMBINED_PARALLEL))
8381 octx = octx->outer_context->outer_context;
8382 else
8383 octx = octx->outer_context;
8384 }
8385 if (octx
8386 && octx->region_type == ORT_WORKSHARE
8387 && octx->combined_loop
8388 && octx->distribute)
8389 {
8390 error_at (OMP_CLAUSE_LOCATION (c),
8391 "%<linear%> clause for variable other than "
8392 "loop iterator specified on construct "
8393 "combined with %<distribute%>");
8394 remove = true;
8395 break;
8396 }
8397 }
8398 /* For combined #pragma omp parallel for simd, need to put
8399 lastprivate and perhaps firstprivate too on the
8400 parallel. Similarly for #pragma omp for simd. */
8401 struct gimplify_omp_ctx *octx = outer_ctx;
8402 decl = NULL_TREE;
8403 do
8404 {
8405 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8406 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8407 break;
8408 decl = OMP_CLAUSE_DECL (c);
8409 if (error_operand_p (decl))
8410 {
8411 decl = NULL_TREE;
8412 break;
8413 }
8414 flags = GOVD_SEEN;
8415 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8416 flags |= GOVD_FIRSTPRIVATE;
8417 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8418 flags |= GOVD_LASTPRIVATE;
8419 if (octx
8420 && octx->region_type == ORT_WORKSHARE
8421 && octx->combined_loop)
8422 {
8423 if (octx->outer_context
8424 && (octx->outer_context->region_type
8425 == ORT_COMBINED_PARALLEL))
8426 octx = octx->outer_context;
8427 else if (omp_check_private (octx, decl, false))
8428 break;
8429 }
8430 else if (octx
8431 && (octx->region_type & ORT_TASK) != 0
8432 && octx->combined_loop)
8433 ;
8434 else if (octx
8435 && octx->region_type == ORT_COMBINED_PARALLEL
8436 && ctx->region_type == ORT_WORKSHARE
8437 && octx == outer_ctx)
8438 flags = GOVD_SEEN | GOVD_SHARED;
8439 else if (octx
8440 && ((octx->region_type & ORT_COMBINED_TEAMS)
8441 == ORT_COMBINED_TEAMS))
8442 flags = GOVD_SEEN | GOVD_SHARED;
8443 else if (octx
8444 && octx->region_type == ORT_COMBINED_TARGET)
8445 {
8446 flags &= ~GOVD_LASTPRIVATE;
8447 if (flags == GOVD_SEEN)
8448 break;
8449 }
8450 else
8451 break;
8452 splay_tree_node on
8453 = splay_tree_lookup (octx->variables,
8454 (splay_tree_key) decl);
8455 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
8456 {
8457 octx = NULL;
8458 break;
8459 }
8460 omp_add_variable (octx, decl, flags);
8461 if (octx->outer_context == NULL)
8462 break;
8463 octx = octx->outer_context;
8464 }
8465 while (1);
8466 if (octx
8467 && decl
8468 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8469 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8470 omp_notice_variable (octx, decl, true);
8471 }
8472 flags = GOVD_LINEAR | GOVD_EXPLICIT;
8473 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8474 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8475 {
8476 notice_outer = false;
8477 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8478 }
8479 goto do_add;
8480
8481 case OMP_CLAUSE_MAP:
8482 decl = OMP_CLAUSE_DECL (c);
8483 if (error_operand_p (decl))
8484 remove = true;
8485 switch (code)
8486 {
8487 case OMP_TARGET:
8488 break;
8489 case OACC_DATA:
8490 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
8491 break;
8492 /* FALLTHRU */
8493 case OMP_TARGET_DATA:
8494 case OMP_TARGET_ENTER_DATA:
8495 case OMP_TARGET_EXIT_DATA:
8496 case OACC_ENTER_DATA:
8497 case OACC_EXIT_DATA:
8498 case OACC_HOST_DATA:
8499 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8500 || (OMP_CLAUSE_MAP_KIND (c)
8501 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8502 /* For target {,enter ,exit }data only the array slice is
8503 mapped, but not the pointer to it. */
8504 remove = true;
8505 break;
8506 default:
8507 break;
8508 }
8509 if (remove)
8510 break;
8511 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
8512 {
8513 struct gimplify_omp_ctx *octx;
8514 for (octx = outer_ctx; octx; octx = octx->outer_context)
8515 {
8516 if (octx->region_type != ORT_ACC_HOST_DATA)
8517 break;
8518 splay_tree_node n2
8519 = splay_tree_lookup (octx->variables,
8520 (splay_tree_key) decl);
8521 if (n2)
8522 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
8523 "declared in enclosing %<host_data%> region",
8524 DECL_NAME (decl));
8525 }
8526 }
8527 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8528 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8529 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8530 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8531 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8532 {
8533 remove = true;
8534 break;
8535 }
8536 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8537 || (OMP_CLAUSE_MAP_KIND (c)
8538 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8539 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
8540 {
8541 OMP_CLAUSE_SIZE (c)
8542 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
8543 false);
8544 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
8545 GOVD_FIRSTPRIVATE | GOVD_SEEN);
8546 }
8547 if (!DECL_P (decl))
8548 {
8549 tree d = decl, *pd;
8550 if (TREE_CODE (d) == ARRAY_REF)
8551 {
8552 while (TREE_CODE (d) == ARRAY_REF)
8553 d = TREE_OPERAND (d, 0);
8554 if (TREE_CODE (d) == COMPONENT_REF
8555 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
8556 decl = d;
8557 }
8558 pd = &OMP_CLAUSE_DECL (c);
8559 if (d == decl
8560 && TREE_CODE (decl) == INDIRECT_REF
8561 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8562 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8563 == REFERENCE_TYPE))
8564 {
8565 pd = &TREE_OPERAND (decl, 0);
8566 decl = TREE_OPERAND (decl, 0);
8567 }
8568 if (TREE_CODE (decl) == COMPONENT_REF)
8569 {
8570 while (TREE_CODE (decl) == COMPONENT_REF)
8571 decl = TREE_OPERAND (decl, 0);
8572 if (TREE_CODE (decl) == INDIRECT_REF
8573 && DECL_P (TREE_OPERAND (decl, 0))
8574 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8575 == REFERENCE_TYPE))
8576 decl = TREE_OPERAND (decl, 0);
8577 }
8578 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
8579 == GS_ERROR)
8580 {
8581 remove = true;
8582 break;
8583 }
8584 if (DECL_P (decl))
8585 {
8586 if (error_operand_p (decl))
8587 {
8588 remove = true;
8589 break;
8590 }
8591
8592 tree stype = TREE_TYPE (decl);
8593 if (TREE_CODE (stype) == REFERENCE_TYPE)
8594 stype = TREE_TYPE (stype);
8595 if (TYPE_SIZE_UNIT (stype) == NULL
8596 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
8597 {
8598 error_at (OMP_CLAUSE_LOCATION (c),
8599 "mapping field %qE of variable length "
8600 "structure", OMP_CLAUSE_DECL (c));
8601 remove = true;
8602 break;
8603 }
8604
8605 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
8606 {
8607 /* Error recovery. */
8608 if (prev_list_p == NULL)
8609 {
8610 remove = true;
8611 break;
8612 }
8613 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8614 {
8615 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
8616 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
8617 {
8618 remove = true;
8619 break;
8620 }
8621 }
8622 }
8623
8624 tree offset;
8625 poly_int64 bitsize, bitpos;
8626 machine_mode mode;
8627 int unsignedp, reversep, volatilep = 0;
8628 tree base = OMP_CLAUSE_DECL (c);
8629 while (TREE_CODE (base) == ARRAY_REF)
8630 base = TREE_OPERAND (base, 0);
8631 if (TREE_CODE (base) == INDIRECT_REF)
8632 base = TREE_OPERAND (base, 0);
8633 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8634 &mode, &unsignedp, &reversep,
8635 &volatilep);
8636 tree orig_base = base;
8637 if ((TREE_CODE (base) == INDIRECT_REF
8638 || (TREE_CODE (base) == MEM_REF
8639 && integer_zerop (TREE_OPERAND (base, 1))))
8640 && DECL_P (TREE_OPERAND (base, 0))
8641 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
8642 == REFERENCE_TYPE))
8643 base = TREE_OPERAND (base, 0);
8644 gcc_assert (base == decl
8645 && (offset == NULL_TREE
8646 || poly_int_tree_p (offset)));
8647
8648 splay_tree_node n
8649 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8650 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8651 == GOMP_MAP_ALWAYS_POINTER);
8652 if (n == NULL || (n->value & GOVD_MAP) == 0)
8653 {
8654 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8655 OMP_CLAUSE_MAP);
8656 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8657 if (orig_base != base)
8658 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8659 else
8660 OMP_CLAUSE_DECL (l) = decl;
8661 OMP_CLAUSE_SIZE (l) = size_int (1);
8662 if (struct_map_to_clause == NULL)
8663 struct_map_to_clause = new hash_map<tree, tree>;
8664 struct_map_to_clause->put (decl, l);
8665 if (ptr)
8666 {
8667 enum gomp_map_kind mkind
8668 = code == OMP_TARGET_EXIT_DATA
8669 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8670 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8671 OMP_CLAUSE_MAP);
8672 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8673 OMP_CLAUSE_DECL (c2)
8674 = unshare_expr (OMP_CLAUSE_DECL (c));
8675 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8676 OMP_CLAUSE_SIZE (c2)
8677 = TYPE_SIZE_UNIT (ptr_type_node);
8678 OMP_CLAUSE_CHAIN (l) = c2;
8679 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8680 {
8681 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8682 tree c3
8683 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8684 OMP_CLAUSE_MAP);
8685 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8686 OMP_CLAUSE_DECL (c3)
8687 = unshare_expr (OMP_CLAUSE_DECL (c4));
8688 OMP_CLAUSE_SIZE (c3)
8689 = TYPE_SIZE_UNIT (ptr_type_node);
8690 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8691 OMP_CLAUSE_CHAIN (c2) = c3;
8692 }
8693 *prev_list_p = l;
8694 prev_list_p = NULL;
8695 }
8696 else
8697 {
8698 OMP_CLAUSE_CHAIN (l) = c;
8699 *list_p = l;
8700 list_p = &OMP_CLAUSE_CHAIN (l);
8701 }
8702 if (orig_base != base && code == OMP_TARGET)
8703 {
8704 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8705 OMP_CLAUSE_MAP);
8706 enum gomp_map_kind mkind
8707 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8708 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8709 OMP_CLAUSE_DECL (c2) = decl;
8710 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8711 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8712 OMP_CLAUSE_CHAIN (l) = c2;
8713 }
8714 flags = GOVD_MAP | GOVD_EXPLICIT;
8715 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8716 flags |= GOVD_SEEN;
8717 goto do_add_decl;
8718 }
8719 else
8720 {
8721 tree *osc = struct_map_to_clause->get (decl);
8722 tree *sc = NULL, *scp = NULL;
8723 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8724 n->value |= GOVD_SEEN;
8725 poly_offset_int o1, o2;
8726 if (offset)
8727 o1 = wi::to_poly_offset (offset);
8728 else
8729 o1 = 0;
8730 if (maybe_ne (bitpos, 0))
8731 o1 += bits_to_bytes_round_down (bitpos);
8732 sc = &OMP_CLAUSE_CHAIN (*osc);
8733 if (*sc != c
8734 && (OMP_CLAUSE_MAP_KIND (*sc)
8735 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8736 sc = &OMP_CLAUSE_CHAIN (*sc);
8737 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8738 if (ptr && sc == prev_list_p)
8739 break;
8740 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8741 != COMPONENT_REF
8742 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8743 != INDIRECT_REF)
8744 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8745 != ARRAY_REF))
8746 break;
8747 else
8748 {
8749 tree offset2;
8750 poly_int64 bitsize2, bitpos2;
8751 base = OMP_CLAUSE_DECL (*sc);
8752 if (TREE_CODE (base) == ARRAY_REF)
8753 {
8754 while (TREE_CODE (base) == ARRAY_REF)
8755 base = TREE_OPERAND (base, 0);
8756 if (TREE_CODE (base) != COMPONENT_REF
8757 || (TREE_CODE (TREE_TYPE (base))
8758 != ARRAY_TYPE))
8759 break;
8760 }
8761 else if (TREE_CODE (base) == INDIRECT_REF
8762 && (TREE_CODE (TREE_OPERAND (base, 0))
8763 == COMPONENT_REF)
8764 && (TREE_CODE (TREE_TYPE
8765 (TREE_OPERAND (base, 0)))
8766 == REFERENCE_TYPE))
8767 base = TREE_OPERAND (base, 0);
8768 base = get_inner_reference (base, &bitsize2,
8769 &bitpos2, &offset2,
8770 &mode, &unsignedp,
8771 &reversep, &volatilep);
8772 if ((TREE_CODE (base) == INDIRECT_REF
8773 || (TREE_CODE (base) == MEM_REF
8774 && integer_zerop (TREE_OPERAND (base,
8775 1))))
8776 && DECL_P (TREE_OPERAND (base, 0))
8777 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8778 0)))
8779 == REFERENCE_TYPE))
8780 base = TREE_OPERAND (base, 0);
8781 if (base != decl)
8782 break;
8783 if (scp)
8784 continue;
8785 gcc_assert (offset == NULL_TREE
8786 || poly_int_tree_p (offset));
8787 tree d1 = OMP_CLAUSE_DECL (*sc);
8788 tree d2 = OMP_CLAUSE_DECL (c);
8789 while (TREE_CODE (d1) == ARRAY_REF)
8790 d1 = TREE_OPERAND (d1, 0);
8791 while (TREE_CODE (d2) == ARRAY_REF)
8792 d2 = TREE_OPERAND (d2, 0);
8793 if (TREE_CODE (d1) == INDIRECT_REF)
8794 d1 = TREE_OPERAND (d1, 0);
8795 if (TREE_CODE (d2) == INDIRECT_REF)
8796 d2 = TREE_OPERAND (d2, 0);
8797 while (TREE_CODE (d1) == COMPONENT_REF)
8798 if (TREE_CODE (d2) == COMPONENT_REF
8799 && TREE_OPERAND (d1, 1)
8800 == TREE_OPERAND (d2, 1))
8801 {
8802 d1 = TREE_OPERAND (d1, 0);
8803 d2 = TREE_OPERAND (d2, 0);
8804 }
8805 else
8806 break;
8807 if (d1 == d2)
8808 {
8809 error_at (OMP_CLAUSE_LOCATION (c),
8810 "%qE appears more than once in map "
8811 "clauses", OMP_CLAUSE_DECL (c));
8812 remove = true;
8813 break;
8814 }
8815 if (offset2)
8816 o2 = wi::to_poly_offset (offset2);
8817 else
8818 o2 = 0;
8819 o2 += bits_to_bytes_round_down (bitpos2);
8820 if (maybe_lt (o1, o2)
8821 || (known_eq (o1, o2)
8822 && maybe_lt (bitpos, bitpos2)))
8823 {
8824 if (ptr)
8825 scp = sc;
8826 else
8827 break;
8828 }
8829 }
8830 if (remove)
8831 break;
8832 OMP_CLAUSE_SIZE (*osc)
8833 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8834 size_one_node);
8835 if (ptr)
8836 {
8837 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8838 OMP_CLAUSE_MAP);
8839 tree cl = NULL_TREE;
8840 enum gomp_map_kind mkind
8841 = code == OMP_TARGET_EXIT_DATA
8842 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8843 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8844 OMP_CLAUSE_DECL (c2)
8845 = unshare_expr (OMP_CLAUSE_DECL (c));
8846 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8847 OMP_CLAUSE_SIZE (c2)
8848 = TYPE_SIZE_UNIT (ptr_type_node);
8849 cl = scp ? *prev_list_p : c2;
8850 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8851 {
8852 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8853 tree c3
8854 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8855 OMP_CLAUSE_MAP);
8856 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8857 OMP_CLAUSE_DECL (c3)
8858 = unshare_expr (OMP_CLAUSE_DECL (c4));
8859 OMP_CLAUSE_SIZE (c3)
8860 = TYPE_SIZE_UNIT (ptr_type_node);
8861 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8862 if (!scp)
8863 OMP_CLAUSE_CHAIN (c2) = c3;
8864 else
8865 cl = c3;
8866 }
8867 if (scp)
8868 *scp = c2;
8869 if (sc == prev_list_p)
8870 {
8871 *sc = cl;
8872 prev_list_p = NULL;
8873 }
8874 else
8875 {
8876 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8877 list_p = prev_list_p;
8878 prev_list_p = NULL;
8879 OMP_CLAUSE_CHAIN (c) = *sc;
8880 *sc = cl;
8881 continue;
8882 }
8883 }
8884 else if (*sc != c)
8885 {
8886 *list_p = OMP_CLAUSE_CHAIN (c);
8887 OMP_CLAUSE_CHAIN (c) = *sc;
8888 *sc = c;
8889 continue;
8890 }
8891 }
8892 }
8893 if (!remove
8894 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8895 && OMP_CLAUSE_CHAIN (c)
8896 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8897 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8898 == GOMP_MAP_ALWAYS_POINTER))
8899 prev_list_p = list_p;
8900 break;
8901 }
8902 flags = GOVD_MAP | GOVD_EXPLICIT;
8903 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8904 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8905 flags |= GOVD_MAP_ALWAYS_TO;
8906 goto do_add;
8907
8908 case OMP_CLAUSE_DEPEND:
8909 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8910 {
8911 tree deps = OMP_CLAUSE_DECL (c);
8912 while (deps && TREE_CODE (deps) == TREE_LIST)
8913 {
8914 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8915 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8916 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8917 pre_p, NULL, is_gimple_val, fb_rvalue);
8918 deps = TREE_CHAIN (deps);
8919 }
8920 break;
8921 }
8922 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8923 break;
8924 if (handled_depend_iterators == -1)
8925 handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
8926 if (handled_depend_iterators)
8927 {
8928 if (handled_depend_iterators == 2)
8929 remove = true;
8930 break;
8931 }
8932 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8933 {
8934 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8935 NULL, is_gimple_val, fb_rvalue);
8936 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8937 }
8938 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8939 {
8940 remove = true;
8941 break;
8942 }
8943 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8944 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8945 is_gimple_val, fb_rvalue) == GS_ERROR)
8946 {
8947 remove = true;
8948 break;
8949 }
8950 break;
8951
8952 case OMP_CLAUSE_TO:
8953 case OMP_CLAUSE_FROM:
8954 case OMP_CLAUSE__CACHE_:
8955 decl = OMP_CLAUSE_DECL (c);
8956 if (error_operand_p (decl))
8957 {
8958 remove = true;
8959 break;
8960 }
8961 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8962 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8963 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8964 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8965 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8966 {
8967 remove = true;
8968 break;
8969 }
8970 if (!DECL_P (decl))
8971 {
8972 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8973 NULL, is_gimple_lvalue, fb_lvalue)
8974 == GS_ERROR)
8975 {
8976 remove = true;
8977 break;
8978 }
8979 break;
8980 }
8981 goto do_notice;
8982
8983 case OMP_CLAUSE_USE_DEVICE_PTR:
8984 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8985 goto do_add;
8986 case OMP_CLAUSE_IS_DEVICE_PTR:
8987 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8988 goto do_add;
8989
8990 do_add:
8991 decl = OMP_CLAUSE_DECL (c);
8992 do_add_decl:
8993 if (error_operand_p (decl))
8994 {
8995 remove = true;
8996 break;
8997 }
8998 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8999 {
9000 tree t = omp_member_access_dummy_var (decl);
9001 if (t)
9002 {
9003 tree v = DECL_VALUE_EXPR (decl);
9004 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
9005 if (outer_ctx)
9006 omp_notice_variable (outer_ctx, t, true);
9007 }
9008 }
9009 if (code == OACC_DATA
9010 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9011 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9012 flags |= GOVD_MAP_0LEN_ARRAY;
9013 omp_add_variable (ctx, decl, flags);
9014 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9015 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
9016 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9017 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
9018 {
9019 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
9020 GOVD_LOCAL | GOVD_SEEN);
9021 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
9022 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
9023 find_decl_expr,
9024 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
9025 NULL) == NULL_TREE)
9026 omp_add_variable (ctx,
9027 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
9028 GOVD_LOCAL | GOVD_SEEN);
9029 gimplify_omp_ctxp = ctx;
9030 push_gimplify_context ();
9031
9032 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
9033 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
9034
9035 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
9036 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
9037 pop_gimplify_context
9038 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
9039 push_gimplify_context ();
9040 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
9041 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
9042 pop_gimplify_context
9043 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
9044 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
9045 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
9046
9047 gimplify_omp_ctxp = outer_ctx;
9048 }
9049 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9050 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
9051 {
9052 gimplify_omp_ctxp = ctx;
9053 push_gimplify_context ();
9054 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
9055 {
9056 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
9057 NULL, NULL);
9058 TREE_SIDE_EFFECTS (bind) = 1;
9059 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
9060 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
9061 }
9062 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
9063 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
9064 pop_gimplify_context
9065 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
9066 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
9067
9068 gimplify_omp_ctxp = outer_ctx;
9069 }
9070 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9071 && OMP_CLAUSE_LINEAR_STMT (c))
9072 {
9073 gimplify_omp_ctxp = ctx;
9074 push_gimplify_context ();
9075 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
9076 {
9077 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
9078 NULL, NULL);
9079 TREE_SIDE_EFFECTS (bind) = 1;
9080 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
9081 OMP_CLAUSE_LINEAR_STMT (c) = bind;
9082 }
9083 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
9084 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
9085 pop_gimplify_context
9086 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
9087 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
9088
9089 gimplify_omp_ctxp = outer_ctx;
9090 }
9091 if (notice_outer)
9092 goto do_notice;
9093 break;
9094
9095 case OMP_CLAUSE_COPYIN:
9096 case OMP_CLAUSE_COPYPRIVATE:
9097 decl = OMP_CLAUSE_DECL (c);
9098 if (error_operand_p (decl))
9099 {
9100 remove = true;
9101 break;
9102 }
9103 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
9104 && !remove
9105 && !omp_check_private (ctx, decl, true))
9106 {
9107 remove = true;
9108 if (is_global_var (decl))
9109 {
9110 if (DECL_THREAD_LOCAL_P (decl))
9111 remove = false;
9112 else if (DECL_HAS_VALUE_EXPR_P (decl))
9113 {
9114 tree value = get_base_address (DECL_VALUE_EXPR (decl));
9115
9116 if (value
9117 && DECL_P (value)
9118 && DECL_THREAD_LOCAL_P (value))
9119 remove = false;
9120 }
9121 }
9122 if (remove)
9123 error_at (OMP_CLAUSE_LOCATION (c),
9124 "copyprivate variable %qE is not threadprivate"
9125 " or private in outer context", DECL_NAME (decl));
9126 }
9127 do_notice:
9128 if ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
9129 && outer_ctx
9130 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
9131 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9132 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9133 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE))
9134 {
9135 splay_tree_node on
9136 = splay_tree_lookup (outer_ctx->variables,
9137 (splay_tree_key)decl);
9138 if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
9139 {
9140 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9141 && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9142 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9143 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9144 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9145 == POINTER_TYPE))))
9146 omp_firstprivatize_variable (outer_ctx, decl);
9147 else
9148 omp_add_variable (outer_ctx, decl,
9149 GOVD_SEEN | GOVD_SHARED);
9150 omp_notice_variable (outer_ctx, decl, true);
9151 }
9152 }
9153 if (outer_ctx)
9154 omp_notice_variable (outer_ctx, decl, true);
9155 if (check_non_private
9156 && region_type == ORT_WORKSHARE
9157 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
9158 || decl == OMP_CLAUSE_DECL (c)
9159 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9160 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9161 == ADDR_EXPR
9162 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9163 == POINTER_PLUS_EXPR
9164 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
9165 (OMP_CLAUSE_DECL (c), 0), 0))
9166 == ADDR_EXPR)))))
9167 && omp_check_private (ctx, decl, false))
9168 {
9169 error ("%s variable %qE is private in outer context",
9170 check_non_private, DECL_NAME (decl));
9171 remove = true;
9172 }
9173 break;
9174
9175 case OMP_CLAUSE_IF:
9176 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
9177 && OMP_CLAUSE_IF_MODIFIER (c) != code)
9178 {
9179 const char *p[2];
9180 for (int i = 0; i < 2; i++)
9181 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
9182 {
9183 case VOID_CST: p[i] = "cancel"; break;
9184 case OMP_PARALLEL: p[i] = "parallel"; break;
9185 case OMP_SIMD: p[i] = "simd"; break;
9186 case OMP_TASK: p[i] = "task"; break;
9187 case OMP_TASKLOOP: p[i] = "taskloop"; break;
9188 case OMP_TARGET_DATA: p[i] = "target data"; break;
9189 case OMP_TARGET: p[i] = "target"; break;
9190 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
9191 case OMP_TARGET_ENTER_DATA:
9192 p[i] = "target enter data"; break;
9193 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
9194 default: gcc_unreachable ();
9195 }
9196 error_at (OMP_CLAUSE_LOCATION (c),
9197 "expected %qs %<if%> clause modifier rather than %qs",
9198 p[0], p[1]);
9199 remove = true;
9200 }
9201 /* Fall through. */
9202
9203 case OMP_CLAUSE_FINAL:
9204 OMP_CLAUSE_OPERAND (c, 0)
9205 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
9206 /* Fall through. */
9207
9208 case OMP_CLAUSE_SCHEDULE:
9209 case OMP_CLAUSE_NUM_THREADS:
9210 case OMP_CLAUSE_NUM_TEAMS:
9211 case OMP_CLAUSE_THREAD_LIMIT:
9212 case OMP_CLAUSE_DIST_SCHEDULE:
9213 case OMP_CLAUSE_DEVICE:
9214 case OMP_CLAUSE_PRIORITY:
9215 case OMP_CLAUSE_GRAINSIZE:
9216 case OMP_CLAUSE_NUM_TASKS:
9217 case OMP_CLAUSE_HINT:
9218 case OMP_CLAUSE_ASYNC:
9219 case OMP_CLAUSE_WAIT:
9220 case OMP_CLAUSE_NUM_GANGS:
9221 case OMP_CLAUSE_NUM_WORKERS:
9222 case OMP_CLAUSE_VECTOR_LENGTH:
9223 case OMP_CLAUSE_WORKER:
9224 case OMP_CLAUSE_VECTOR:
9225 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9226 is_gimple_val, fb_rvalue) == GS_ERROR)
9227 remove = true;
9228 break;
9229
9230 case OMP_CLAUSE_GANG:
9231 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9232 is_gimple_val, fb_rvalue) == GS_ERROR)
9233 remove = true;
9234 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
9235 is_gimple_val, fb_rvalue) == GS_ERROR)
9236 remove = true;
9237 break;
9238
9239 case OMP_CLAUSE_NOWAIT:
9240 nowait = 1;
9241 break;
9242
9243 case OMP_CLAUSE_ORDERED:
9244 case OMP_CLAUSE_UNTIED:
9245 case OMP_CLAUSE_COLLAPSE:
9246 case OMP_CLAUSE_TILE:
9247 case OMP_CLAUSE_AUTO:
9248 case OMP_CLAUSE_SEQ:
9249 case OMP_CLAUSE_INDEPENDENT:
9250 case OMP_CLAUSE_MERGEABLE:
9251 case OMP_CLAUSE_PROC_BIND:
9252 case OMP_CLAUSE_SAFELEN:
9253 case OMP_CLAUSE_SIMDLEN:
9254 case OMP_CLAUSE_NOGROUP:
9255 case OMP_CLAUSE_THREADS:
9256 case OMP_CLAUSE_SIMD:
9257 case OMP_CLAUSE_IF_PRESENT:
9258 case OMP_CLAUSE_FINALIZE:
9259 break;
9260
9261 case OMP_CLAUSE_DEFAULTMAP:
9262 enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
9263 switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
9264 {
9265 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
9266 gdmkmin = GDMK_SCALAR;
9267 gdmkmax = GDMK_POINTER;
9268 break;
9269 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
9270 gdmkmin = gdmkmax = GDMK_SCALAR;
9271 break;
9272 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
9273 gdmkmin = gdmkmax = GDMK_AGGREGATE;
9274 break;
9275 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
9276 gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
9277 break;
9278 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
9279 gdmkmin = gdmkmax = GDMK_POINTER;
9280 break;
9281 default:
9282 gcc_unreachable ();
9283 }
9284 for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
9285 switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
9286 {
9287 case OMP_CLAUSE_DEFAULTMAP_ALLOC:
9288 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
9289 break;
9290 case OMP_CLAUSE_DEFAULTMAP_TO:
9291 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
9292 break;
9293 case OMP_CLAUSE_DEFAULTMAP_FROM:
9294 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
9295 break;
9296 case OMP_CLAUSE_DEFAULTMAP_TOFROM:
9297 ctx->defaultmap[gdmk] = GOVD_MAP;
9298 break;
9299 case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
9300 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9301 break;
9302 case OMP_CLAUSE_DEFAULTMAP_NONE:
9303 ctx->defaultmap[gdmk] = 0;
9304 break;
9305 case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
9306 switch (gdmk)
9307 {
9308 case GDMK_SCALAR:
9309 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9310 break;
9311 case GDMK_AGGREGATE:
9312 case GDMK_ALLOCATABLE:
9313 ctx->defaultmap[gdmk] = GOVD_MAP;
9314 break;
9315 case GDMK_POINTER:
9316 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
9317 break;
9318 default:
9319 gcc_unreachable ();
9320 }
9321 break;
9322 default:
9323 gcc_unreachable ();
9324 }
9325 break;
9326
9327 case OMP_CLAUSE_ALIGNED:
9328 decl = OMP_CLAUSE_DECL (c);
9329 if (error_operand_p (decl))
9330 {
9331 remove = true;
9332 break;
9333 }
9334 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
9335 is_gimple_val, fb_rvalue) == GS_ERROR)
9336 {
9337 remove = true;
9338 break;
9339 }
9340 if (!is_global_var (decl)
9341 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9342 omp_add_variable (ctx, decl, GOVD_ALIGNED);
9343 break;
9344
9345 case OMP_CLAUSE_NONTEMPORAL:
9346 decl = OMP_CLAUSE_DECL (c);
9347 if (error_operand_p (decl))
9348 {
9349 remove = true;
9350 break;
9351 }
9352 omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
9353 break;
9354
9355 case OMP_CLAUSE_DEFAULT:
9356 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
9357 break;
9358
9359 case OMP_CLAUSE_INCLUSIVE:
9360 case OMP_CLAUSE_EXCLUSIVE:
9361 decl = OMP_CLAUSE_DECL (c);
9362 {
9363 splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
9364 (splay_tree_key) decl);
9365 if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
9366 {
9367 error_at (OMP_CLAUSE_LOCATION (c),
9368 "%qD specified in %qs clause but not in %<inscan%> "
9369 "%<reduction%> clause on the containing construct",
9370 decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9371 remove = true;
9372 }
9373 else
9374 {
9375 n->value |= GOVD_REDUCTION_INSCAN;
9376 if (outer_ctx->region_type == ORT_SIMD
9377 && outer_ctx->outer_context
9378 && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
9379 {
9380 n = splay_tree_lookup (outer_ctx->outer_context->variables,
9381 (splay_tree_key) decl);
9382 if (n && (n->value & GOVD_REDUCTION) != 0)
9383 n->value |= GOVD_REDUCTION_INSCAN;
9384 }
9385 }
9386 }
9387 break;
9388
9389 default:
9390 gcc_unreachable ();
9391 }
9392
9393 if (code == OACC_DATA
9394 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9395 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
9396 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9397 remove = true;
9398 if (remove)
9399 *list_p = OMP_CLAUSE_CHAIN (c);
9400 else
9401 list_p = &OMP_CLAUSE_CHAIN (c);
9402 }
9403
9404 ctx->clauses = *orig_list_p;
9405 gimplify_omp_ctxp = ctx;
9406 if (struct_map_to_clause)
9407 delete struct_map_to_clause;
9408 }
9409
9410 /* Return true if DECL is a candidate for shared to firstprivate
9411 optimization. We only consider non-addressable scalars, not
9412 too big, and not references. */
9413
9414 static bool
9415 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
9416 {
9417 if (TREE_ADDRESSABLE (decl))
9418 return false;
9419 tree type = TREE_TYPE (decl);
9420 if (!is_gimple_reg_type (type)
9421 || TREE_CODE (type) == REFERENCE_TYPE
9422 || TREE_ADDRESSABLE (type))
9423 return false;
9424 /* Don't optimize too large decls, as each thread/task will have
9425 its own. */
9426 HOST_WIDE_INT len = int_size_in_bytes (type);
9427 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
9428 return false;
9429 if (lang_hooks.decls.omp_privatize_by_reference (decl))
9430 return false;
9431 return true;
9432 }
9433
9434 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
9435 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
9436 GOVD_WRITTEN in outer contexts. */
9437
9438 static void
9439 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
9440 {
9441 for (; ctx; ctx = ctx->outer_context)
9442 {
9443 splay_tree_node n = splay_tree_lookup (ctx->variables,
9444 (splay_tree_key) decl);
9445 if (n == NULL)
9446 continue;
9447 else if (n->value & GOVD_SHARED)
9448 {
9449 n->value |= GOVD_WRITTEN;
9450 return;
9451 }
9452 else if (n->value & GOVD_DATA_SHARE_CLASS)
9453 return;
9454 }
9455 }
9456
9457 /* Helper callback for walk_gimple_seq to discover possible stores
9458 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9459 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9460 for those. */
9461
9462 static tree
9463 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
9464 {
9465 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
9466
9467 *walk_subtrees = 0;
9468 if (!wi->is_lhs)
9469 return NULL_TREE;
9470
9471 tree op = *tp;
9472 do
9473 {
9474 if (handled_component_p (op))
9475 op = TREE_OPERAND (op, 0);
9476 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
9477 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
9478 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9479 else
9480 break;
9481 }
9482 while (1);
9483 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
9484 return NULL_TREE;
9485
9486 omp_mark_stores (gimplify_omp_ctxp, op);
9487 return NULL_TREE;
9488 }
9489
9490 /* Helper callback for walk_gimple_seq to discover possible stores
9491 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9492 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9493 for those. */
9494
9495 static tree
9496 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
9497 bool *handled_ops_p,
9498 struct walk_stmt_info *wi)
9499 {
9500 gimple *stmt = gsi_stmt (*gsi_p);
9501 switch (gimple_code (stmt))
9502 {
9503 /* Don't recurse on OpenMP constructs for which
9504 gimplify_adjust_omp_clauses already handled the bodies,
9505 except handle gimple_omp_for_pre_body. */
9506 case GIMPLE_OMP_FOR:
9507 *handled_ops_p = true;
9508 if (gimple_omp_for_pre_body (stmt))
9509 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
9510 omp_find_stores_stmt, omp_find_stores_op, wi);
9511 break;
9512 case GIMPLE_OMP_PARALLEL:
9513 case GIMPLE_OMP_TASK:
9514 case GIMPLE_OMP_SECTIONS:
9515 case GIMPLE_OMP_SINGLE:
9516 case GIMPLE_OMP_TARGET:
9517 case GIMPLE_OMP_TEAMS:
9518 case GIMPLE_OMP_CRITICAL:
9519 *handled_ops_p = true;
9520 break;
9521 default:
9522 break;
9523 }
9524 return NULL_TREE;
9525 }
9526
9527 struct gimplify_adjust_omp_clauses_data
9528 {
9529 tree *list_p;
9530 gimple_seq *pre_p;
9531 };
9532
9533 /* For all variables that were not actually used within the context,
9534 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
9535
9536 static int
9537 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
9538 {
9539 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
9540 gimple_seq *pre_p
9541 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
9542 tree decl = (tree) n->key;
9543 unsigned flags = n->value;
9544 enum omp_clause_code code;
9545 tree clause;
9546 bool private_debug;
9547
9548 if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
9549 && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
9550 flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
9551 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
9552 return 0;
9553 if ((flags & GOVD_SEEN) == 0)
9554 return 0;
9555 if (flags & GOVD_DEBUG_PRIVATE)
9556 {
9557 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
9558 private_debug = true;
9559 }
9560 else if (flags & GOVD_MAP)
9561 private_debug = false;
9562 else
9563 private_debug
9564 = lang_hooks.decls.omp_private_debug_clause (decl,
9565 !!(flags & GOVD_SHARED));
9566 if (private_debug)
9567 code = OMP_CLAUSE_PRIVATE;
9568 else if (flags & GOVD_MAP)
9569 {
9570 code = OMP_CLAUSE_MAP;
9571 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9572 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9573 {
9574 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
9575 return 0;
9576 }
9577 }
9578 else if (flags & GOVD_SHARED)
9579 {
9580 if (is_global_var (decl))
9581 {
9582 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9583 while (ctx != NULL)
9584 {
9585 splay_tree_node on
9586 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9587 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9588 | GOVD_PRIVATE | GOVD_REDUCTION
9589 | GOVD_LINEAR | GOVD_MAP)) != 0)
9590 break;
9591 ctx = ctx->outer_context;
9592 }
9593 if (ctx == NULL)
9594 return 0;
9595 }
9596 code = OMP_CLAUSE_SHARED;
9597 }
9598 else if (flags & GOVD_PRIVATE)
9599 code = OMP_CLAUSE_PRIVATE;
9600 else if (flags & GOVD_FIRSTPRIVATE)
9601 {
9602 code = OMP_CLAUSE_FIRSTPRIVATE;
9603 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
9604 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9605 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9606 {
9607 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
9608 "%<target%> construct", decl);
9609 return 0;
9610 }
9611 }
9612 else if (flags & GOVD_LASTPRIVATE)
9613 code = OMP_CLAUSE_LASTPRIVATE;
9614 else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
9615 return 0;
9616 else if (flags & GOVD_CONDTEMP)
9617 {
9618 code = OMP_CLAUSE__CONDTEMP_;
9619 gimple_add_tmp_var (decl);
9620 }
9621 else
9622 gcc_unreachable ();
9623
9624 if (((flags & GOVD_LASTPRIVATE)
9625 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
9626 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9627 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9628
9629 tree chain = *list_p;
9630 clause = build_omp_clause (input_location, code);
9631 OMP_CLAUSE_DECL (clause) = decl;
9632 OMP_CLAUSE_CHAIN (clause) = chain;
9633 if (private_debug)
9634 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
9635 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
9636 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
9637 else if (code == OMP_CLAUSE_SHARED
9638 && (flags & GOVD_WRITTEN) == 0
9639 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9640 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
9641 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
9642 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
9643 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
9644 {
9645 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
9646 OMP_CLAUSE_DECL (nc) = decl;
9647 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9648 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
9649 OMP_CLAUSE_DECL (clause)
9650 = build_simple_mem_ref_loc (input_location, decl);
9651 OMP_CLAUSE_DECL (clause)
9652 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
9653 build_int_cst (build_pointer_type (char_type_node), 0));
9654 OMP_CLAUSE_SIZE (clause) = size_zero_node;
9655 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9656 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
9657 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
9658 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9659 OMP_CLAUSE_CHAIN (nc) = chain;
9660 OMP_CLAUSE_CHAIN (clause) = nc;
9661 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9662 gimplify_omp_ctxp = ctx->outer_context;
9663 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
9664 pre_p, NULL, is_gimple_val, fb_rvalue);
9665 gimplify_omp_ctxp = ctx;
9666 }
9667 else if (code == OMP_CLAUSE_MAP)
9668 {
9669 int kind;
9670 /* Not all combinations of these GOVD_MAP flags are actually valid. */
9671 switch (flags & (GOVD_MAP_TO_ONLY
9672 | GOVD_MAP_FORCE
9673 | GOVD_MAP_FORCE_PRESENT
9674 | GOVD_MAP_ALLOC_ONLY
9675 | GOVD_MAP_FROM_ONLY))
9676 {
9677 case 0:
9678 kind = GOMP_MAP_TOFROM;
9679 break;
9680 case GOVD_MAP_FORCE:
9681 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
9682 break;
9683 case GOVD_MAP_TO_ONLY:
9684 kind = GOMP_MAP_TO;
9685 break;
9686 case GOVD_MAP_FROM_ONLY:
9687 kind = GOMP_MAP_FROM;
9688 break;
9689 case GOVD_MAP_ALLOC_ONLY:
9690 kind = GOMP_MAP_ALLOC;
9691 break;
9692 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
9693 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
9694 break;
9695 case GOVD_MAP_FORCE_PRESENT:
9696 kind = GOMP_MAP_FORCE_PRESENT;
9697 break;
9698 default:
9699 gcc_unreachable ();
9700 }
9701 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
9702 if (DECL_SIZE (decl)
9703 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9704 {
9705 tree decl2 = DECL_VALUE_EXPR (decl);
9706 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9707 decl2 = TREE_OPERAND (decl2, 0);
9708 gcc_assert (DECL_P (decl2));
9709 tree mem = build_simple_mem_ref (decl2);
9710 OMP_CLAUSE_DECL (clause) = mem;
9711 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9712 if (gimplify_omp_ctxp->outer_context)
9713 {
9714 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9715 omp_notice_variable (ctx, decl2, true);
9716 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
9717 }
9718 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9719 OMP_CLAUSE_MAP);
9720 OMP_CLAUSE_DECL (nc) = decl;
9721 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9722 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
9723 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9724 else
9725 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9726 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9727 OMP_CLAUSE_CHAIN (clause) = nc;
9728 }
9729 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9730 && lang_hooks.decls.omp_privatize_by_reference (decl))
9731 {
9732 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
9733 OMP_CLAUSE_SIZE (clause)
9734 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
9735 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9736 gimplify_omp_ctxp = ctx->outer_context;
9737 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
9738 pre_p, NULL, is_gimple_val, fb_rvalue);
9739 gimplify_omp_ctxp = ctx;
9740 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9741 OMP_CLAUSE_MAP);
9742 OMP_CLAUSE_DECL (nc) = decl;
9743 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9744 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
9745 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9746 OMP_CLAUSE_CHAIN (clause) = nc;
9747 }
9748 else
9749 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
9750 }
9751 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
9752 {
9753 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
9754 OMP_CLAUSE_DECL (nc) = decl;
9755 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
9756 OMP_CLAUSE_CHAIN (nc) = chain;
9757 OMP_CLAUSE_CHAIN (clause) = nc;
9758 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9759 gimplify_omp_ctxp = ctx->outer_context;
9760 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9761 gimplify_omp_ctxp = ctx;
9762 }
9763 *list_p = clause;
9764 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9765 gimplify_omp_ctxp = ctx->outer_context;
9766 lang_hooks.decls.omp_finish_clause (clause, pre_p);
9767 if (gimplify_omp_ctxp)
9768 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
9769 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
9770 && DECL_P (OMP_CLAUSE_SIZE (clause)))
9771 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
9772 true);
9773 gimplify_omp_ctxp = ctx;
9774 return 0;
9775 }
9776
9777 static void
9778 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
9779 enum tree_code code)
9780 {
9781 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9782 tree *orig_list_p = list_p;
9783 tree c, decl;
9784 bool has_inscan_reductions = false;
9785
9786 if (body)
9787 {
9788 struct gimplify_omp_ctx *octx;
9789 for (octx = ctx; octx; octx = octx->outer_context)
9790 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
9791 break;
9792 if (octx)
9793 {
9794 struct walk_stmt_info wi;
9795 memset (&wi, 0, sizeof (wi));
9796 walk_gimple_seq (body, omp_find_stores_stmt,
9797 omp_find_stores_op, &wi);
9798 }
9799 }
9800
9801 if (ctx->region_type == ORT_WORKSHARE
9802 && ctx->outer_context
9803 && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
9804 {
9805 for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
9806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9807 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
9808 {
9809 decl = OMP_CLAUSE_DECL (c);
9810 splay_tree_node n
9811 = splay_tree_lookup (ctx->outer_context->variables,
9812 (splay_tree_key) decl);
9813 gcc_checking_assert (!splay_tree_lookup (ctx->variables,
9814 (splay_tree_key) decl));
9815 omp_add_variable (ctx, decl, n->value);
9816 tree c2 = copy_node (c);
9817 OMP_CLAUSE_CHAIN (c2) = *list_p;
9818 *list_p = c2;
9819 if ((n->value & GOVD_FIRSTPRIVATE) == 0)
9820 continue;
9821 c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9822 OMP_CLAUSE_FIRSTPRIVATE);
9823 OMP_CLAUSE_DECL (c2) = decl;
9824 OMP_CLAUSE_CHAIN (c2) = *list_p;
9825 *list_p = c2;
9826 }
9827 }
9828 while ((c = *list_p) != NULL)
9829 {
9830 splay_tree_node n;
9831 bool remove = false;
9832
9833 switch (OMP_CLAUSE_CODE (c))
9834 {
9835 case OMP_CLAUSE_FIRSTPRIVATE:
9836 if ((ctx->region_type & ORT_TARGET)
9837 && (ctx->region_type & ORT_ACC) == 0
9838 && TYPE_ATOMIC (strip_array_types
9839 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9840 {
9841 error_at (OMP_CLAUSE_LOCATION (c),
9842 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9843 "%<target%> construct", OMP_CLAUSE_DECL (c));
9844 remove = true;
9845 break;
9846 }
9847 /* FALLTHRU */
9848 case OMP_CLAUSE_PRIVATE:
9849 case OMP_CLAUSE_SHARED:
9850 case OMP_CLAUSE_LINEAR:
9851 decl = OMP_CLAUSE_DECL (c);
9852 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9853 remove = !(n->value & GOVD_SEEN);
9854 if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
9855 && code == OMP_PARALLEL
9856 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
9857 remove = true;
9858 if (! remove)
9859 {
9860 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9861 if ((n->value & GOVD_DEBUG_PRIVATE)
9862 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9863 {
9864 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9865 || ((n->value & GOVD_DATA_SHARE_CLASS)
9866 == GOVD_SHARED));
9867 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9868 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9869 }
9870 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9871 && (n->value & GOVD_WRITTEN) == 0
9872 && DECL_P (decl)
9873 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9874 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9875 else if (DECL_P (decl)
9876 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9877 && (n->value & GOVD_WRITTEN) != 0)
9878 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9879 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9880 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9881 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9882 }
9883 break;
9884
9885 case OMP_CLAUSE_LASTPRIVATE:
9886 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9887 accurately reflect the presence of a FIRSTPRIVATE clause. */
9888 decl = OMP_CLAUSE_DECL (c);
9889 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9890 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9891 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9892 if (code == OMP_DISTRIBUTE
9893 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9894 {
9895 remove = true;
9896 error_at (OMP_CLAUSE_LOCATION (c),
9897 "same variable used in %<firstprivate%> and "
9898 "%<lastprivate%> clauses on %<distribute%> "
9899 "construct");
9900 }
9901 if (!remove
9902 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9903 && DECL_P (decl)
9904 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9905 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9906 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
9907 remove = true;
9908 break;
9909
9910 case OMP_CLAUSE_ALIGNED:
9911 decl = OMP_CLAUSE_DECL (c);
9912 if (!is_global_var (decl))
9913 {
9914 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9915 remove = n == NULL || !(n->value & GOVD_SEEN);
9916 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9917 {
9918 struct gimplify_omp_ctx *octx;
9919 if (n != NULL
9920 && (n->value & (GOVD_DATA_SHARE_CLASS
9921 & ~GOVD_FIRSTPRIVATE)))
9922 remove = true;
9923 else
9924 for (octx = ctx->outer_context; octx;
9925 octx = octx->outer_context)
9926 {
9927 n = splay_tree_lookup (octx->variables,
9928 (splay_tree_key) decl);
9929 if (n == NULL)
9930 continue;
9931 if (n->value & GOVD_LOCAL)
9932 break;
9933 /* We have to avoid assigning a shared variable
9934 to itself when trying to add
9935 __builtin_assume_aligned. */
9936 if (n->value & GOVD_SHARED)
9937 {
9938 remove = true;
9939 break;
9940 }
9941 }
9942 }
9943 }
9944 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9945 {
9946 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9947 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9948 remove = true;
9949 }
9950 break;
9951
9952 case OMP_CLAUSE_NONTEMPORAL:
9953 decl = OMP_CLAUSE_DECL (c);
9954 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9955 remove = n == NULL || !(n->value & GOVD_SEEN);
9956 break;
9957
9958 case OMP_CLAUSE_MAP:
9959 if (code == OMP_TARGET_EXIT_DATA
9960 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9961 {
9962 remove = true;
9963 break;
9964 }
9965 decl = OMP_CLAUSE_DECL (c);
9966 /* Data clauses associated with acc parallel reductions must be
9967 compatible with present_or_copy. Warn and adjust the clause
9968 if that is not the case. */
9969 if (ctx->region_type == ORT_ACC_PARALLEL)
9970 {
9971 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9972 n = NULL;
9973
9974 if (DECL_P (t))
9975 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9976
9977 if (n && (n->value & GOVD_REDUCTION))
9978 {
9979 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9980
9981 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9982 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9983 && kind != GOMP_MAP_FORCE_PRESENT
9984 && kind != GOMP_MAP_POINTER)
9985 {
9986 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9987 "incompatible data clause with reduction "
9988 "on %qE; promoting to %<present_or_copy%>",
9989 DECL_NAME (t));
9990 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9991 }
9992 }
9993 }
9994 if (!DECL_P (decl))
9995 {
9996 if ((ctx->region_type & ORT_TARGET) != 0
9997 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9998 {
9999 if (TREE_CODE (decl) == INDIRECT_REF
10000 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
10001 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
10002 == REFERENCE_TYPE))
10003 decl = TREE_OPERAND (decl, 0);
10004 if (TREE_CODE (decl) == COMPONENT_REF)
10005 {
10006 while (TREE_CODE (decl) == COMPONENT_REF)
10007 decl = TREE_OPERAND (decl, 0);
10008 if (DECL_P (decl))
10009 {
10010 n = splay_tree_lookup (ctx->variables,
10011 (splay_tree_key) decl);
10012 if (!(n->value & GOVD_SEEN))
10013 remove = true;
10014 }
10015 }
10016 }
10017 break;
10018 }
10019 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
10020 if ((ctx->region_type & ORT_TARGET) != 0
10021 && !(n->value & GOVD_SEEN)
10022 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
10023 && (!is_global_var (decl)
10024 || !lookup_attribute ("omp declare target link",
10025 DECL_ATTRIBUTES (decl))))
10026 {
10027 remove = true;
10028 /* For struct element mapping, if struct is never referenced
10029 in target block and none of the mapping has always modifier,
10030 remove all the struct element mappings, which immediately
10031 follow the GOMP_MAP_STRUCT map clause. */
10032 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
10033 {
10034 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
10035 while (cnt--)
10036 OMP_CLAUSE_CHAIN (c)
10037 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
10038 }
10039 }
10040 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
10041 && code == OMP_TARGET_EXIT_DATA)
10042 remove = true;
10043 else if (DECL_SIZE (decl)
10044 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
10045 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
10046 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
10047 && (OMP_CLAUSE_MAP_KIND (c)
10048 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
10049 {
10050 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
10051 for these, TREE_CODE (DECL_SIZE (decl)) will always be
10052 INTEGER_CST. */
10053 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
10054
10055 tree decl2 = DECL_VALUE_EXPR (decl);
10056 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
10057 decl2 = TREE_OPERAND (decl2, 0);
10058 gcc_assert (DECL_P (decl2));
10059 tree mem = build_simple_mem_ref (decl2);
10060 OMP_CLAUSE_DECL (c) = mem;
10061 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
10062 if (ctx->outer_context)
10063 {
10064 omp_notice_variable (ctx->outer_context, decl2, true);
10065 omp_notice_variable (ctx->outer_context,
10066 OMP_CLAUSE_SIZE (c), true);
10067 }
10068 if (((ctx->region_type & ORT_TARGET) != 0
10069 || !ctx->target_firstprivatize_array_bases)
10070 && ((n->value & GOVD_SEEN) == 0
10071 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
10072 {
10073 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10074 OMP_CLAUSE_MAP);
10075 OMP_CLAUSE_DECL (nc) = decl;
10076 OMP_CLAUSE_SIZE (nc) = size_zero_node;
10077 if (ctx->target_firstprivatize_array_bases)
10078 OMP_CLAUSE_SET_MAP_KIND (nc,
10079 GOMP_MAP_FIRSTPRIVATE_POINTER);
10080 else
10081 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
10082 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
10083 OMP_CLAUSE_CHAIN (c) = nc;
10084 c = nc;
10085 }
10086 }
10087 else
10088 {
10089 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
10090 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
10091 gcc_assert ((n->value & GOVD_SEEN) == 0
10092 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
10093 == 0));
10094 }
10095 break;
10096
10097 case OMP_CLAUSE_TO:
10098 case OMP_CLAUSE_FROM:
10099 case OMP_CLAUSE__CACHE_:
10100 decl = OMP_CLAUSE_DECL (c);
10101 if (!DECL_P (decl))
10102 break;
10103 if (DECL_SIZE (decl)
10104 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
10105 {
10106 tree decl2 = DECL_VALUE_EXPR (decl);
10107 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
10108 decl2 = TREE_OPERAND (decl2, 0);
10109 gcc_assert (DECL_P (decl2));
10110 tree mem = build_simple_mem_ref (decl2);
10111 OMP_CLAUSE_DECL (c) = mem;
10112 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
10113 if (ctx->outer_context)
10114 {
10115 omp_notice_variable (ctx->outer_context, decl2, true);
10116 omp_notice_variable (ctx->outer_context,
10117 OMP_CLAUSE_SIZE (c), true);
10118 }
10119 }
10120 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
10121 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
10122 break;
10123
10124 case OMP_CLAUSE_REDUCTION:
10125 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
10126 {
10127 decl = OMP_CLAUSE_DECL (c);
10128 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
10129 if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
10130 {
10131 remove = true;
10132 error_at (OMP_CLAUSE_LOCATION (c),
10133 "%qD specified in %<inscan%> %<reduction%> clause "
10134 "but not in %<scan%> directive clause", decl);
10135 break;
10136 }
10137 has_inscan_reductions = true;
10138 }
10139 /* FALLTHRU */
10140 case OMP_CLAUSE_IN_REDUCTION:
10141 case OMP_CLAUSE_TASK_REDUCTION:
10142 decl = OMP_CLAUSE_DECL (c);
10143 /* OpenACC reductions need a present_or_copy data clause.
10144 Add one if necessary. Emit error when the reduction is private. */
10145 if (ctx->region_type == ORT_ACC_PARALLEL)
10146 {
10147 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
10148 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
10149 {
10150 remove = true;
10151 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
10152 "reduction on %qE", DECL_NAME (decl));
10153 }
10154 else if ((n->value & GOVD_MAP) == 0)
10155 {
10156 tree next = OMP_CLAUSE_CHAIN (c);
10157 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
10158 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
10159 OMP_CLAUSE_DECL (nc) = decl;
10160 OMP_CLAUSE_CHAIN (c) = nc;
10161 lang_hooks.decls.omp_finish_clause (nc, pre_p);
10162 while (1)
10163 {
10164 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
10165 if (OMP_CLAUSE_CHAIN (nc) == NULL)
10166 break;
10167 nc = OMP_CLAUSE_CHAIN (nc);
10168 }
10169 OMP_CLAUSE_CHAIN (nc) = next;
10170 n->value |= GOVD_MAP;
10171 }
10172 }
10173 if (DECL_P (decl)
10174 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
10175 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
10176 break;
10177 case OMP_CLAUSE_COPYIN:
10178 case OMP_CLAUSE_COPYPRIVATE:
10179 case OMP_CLAUSE_IF:
10180 case OMP_CLAUSE_NUM_THREADS:
10181 case OMP_CLAUSE_NUM_TEAMS:
10182 case OMP_CLAUSE_THREAD_LIMIT:
10183 case OMP_CLAUSE_DIST_SCHEDULE:
10184 case OMP_CLAUSE_DEVICE:
10185 case OMP_CLAUSE_SCHEDULE:
10186 case OMP_CLAUSE_NOWAIT:
10187 case OMP_CLAUSE_ORDERED:
10188 case OMP_CLAUSE_DEFAULT:
10189 case OMP_CLAUSE_UNTIED:
10190 case OMP_CLAUSE_COLLAPSE:
10191 case OMP_CLAUSE_FINAL:
10192 case OMP_CLAUSE_MERGEABLE:
10193 case OMP_CLAUSE_PROC_BIND:
10194 case OMP_CLAUSE_SAFELEN:
10195 case OMP_CLAUSE_SIMDLEN:
10196 case OMP_CLAUSE_DEPEND:
10197 case OMP_CLAUSE_PRIORITY:
10198 case OMP_CLAUSE_GRAINSIZE:
10199 case OMP_CLAUSE_NUM_TASKS:
10200 case OMP_CLAUSE_NOGROUP:
10201 case OMP_CLAUSE_THREADS:
10202 case OMP_CLAUSE_SIMD:
10203 case OMP_CLAUSE_HINT:
10204 case OMP_CLAUSE_DEFAULTMAP:
10205 case OMP_CLAUSE_USE_DEVICE_PTR:
10206 case OMP_CLAUSE_IS_DEVICE_PTR:
10207 case OMP_CLAUSE_ASYNC:
10208 case OMP_CLAUSE_WAIT:
10209 case OMP_CLAUSE_INDEPENDENT:
10210 case OMP_CLAUSE_NUM_GANGS:
10211 case OMP_CLAUSE_NUM_WORKERS:
10212 case OMP_CLAUSE_VECTOR_LENGTH:
10213 case OMP_CLAUSE_GANG:
10214 case OMP_CLAUSE_WORKER:
10215 case OMP_CLAUSE_VECTOR:
10216 case OMP_CLAUSE_AUTO:
10217 case OMP_CLAUSE_SEQ:
10218 case OMP_CLAUSE_TILE:
10219 case OMP_CLAUSE_IF_PRESENT:
10220 case OMP_CLAUSE_FINALIZE:
10221 case OMP_CLAUSE_INCLUSIVE:
10222 case OMP_CLAUSE_EXCLUSIVE:
10223 break;
10224
10225 default:
10226 gcc_unreachable ();
10227 }
10228
10229 if (remove)
10230 *list_p = OMP_CLAUSE_CHAIN (c);
10231 else
10232 list_p = &OMP_CLAUSE_CHAIN (c);
10233 }
10234
10235 /* Add in any implicit data sharing. */
10236 struct gimplify_adjust_omp_clauses_data data;
10237 data.list_p = list_p;
10238 data.pre_p = pre_p;
10239 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
10240
10241 if (has_inscan_reductions)
10242 for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
10243 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10244 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
10245 {
10246 error_at (OMP_CLAUSE_LOCATION (c),
10247 "%<inscan%> %<reduction%> clause used together with "
10248 "%<linear%> clause for a variable other than loop "
10249 "iterator");
10250 break;
10251 }
10252
10253 gimplify_omp_ctxp = ctx->outer_context;
10254 delete_omp_context (ctx);
10255 }
10256
10257 /* Gimplify OACC_CACHE. */
10258
10259 static void
10260 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
10261 {
10262 tree expr = *expr_p;
10263
10264 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
10265 OACC_CACHE);
10266 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
10267 OACC_CACHE);
10268
10269 /* TODO: Do something sensible with this information. */
10270
10271 *expr_p = NULL_TREE;
10272 }
10273
10274 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
10275 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
10276 kind. The entry kind will replace the one in CLAUSE, while the exit
10277 kind will be used in a new omp_clause and returned to the caller. */
10278
10279 static tree
10280 gimplify_oacc_declare_1 (tree clause)
10281 {
10282 HOST_WIDE_INT kind, new_op;
10283 bool ret = false;
10284 tree c = NULL;
10285
10286 kind = OMP_CLAUSE_MAP_KIND (clause);
10287
10288 switch (kind)
10289 {
10290 case GOMP_MAP_ALLOC:
10291 new_op = GOMP_MAP_RELEASE;
10292 ret = true;
10293 break;
10294
10295 case GOMP_MAP_FROM:
10296 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
10297 new_op = GOMP_MAP_FROM;
10298 ret = true;
10299 break;
10300
10301 case GOMP_MAP_TOFROM:
10302 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
10303 new_op = GOMP_MAP_FROM;
10304 ret = true;
10305 break;
10306
10307 case GOMP_MAP_DEVICE_RESIDENT:
10308 case GOMP_MAP_FORCE_DEVICEPTR:
10309 case GOMP_MAP_FORCE_PRESENT:
10310 case GOMP_MAP_LINK:
10311 case GOMP_MAP_POINTER:
10312 case GOMP_MAP_TO:
10313 break;
10314
10315 default:
10316 gcc_unreachable ();
10317 break;
10318 }
10319
10320 if (ret)
10321 {
10322 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
10323 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
10324 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
10325 }
10326
10327 return c;
10328 }
10329
10330 /* Gimplify OACC_DECLARE. */
10331
10332 static void
10333 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
10334 {
10335 tree expr = *expr_p;
10336 gomp_target *stmt;
10337 tree clauses, t, decl;
10338
10339 clauses = OACC_DECLARE_CLAUSES (expr);
10340
10341 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
10342 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
10343
10344 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
10345 {
10346 decl = OMP_CLAUSE_DECL (t);
10347
10348 if (TREE_CODE (decl) == MEM_REF)
10349 decl = TREE_OPERAND (decl, 0);
10350
10351 if (VAR_P (decl) && !is_oacc_declared (decl))
10352 {
10353 tree attr = get_identifier ("oacc declare target");
10354 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
10355 DECL_ATTRIBUTES (decl));
10356 }
10357
10358 if (VAR_P (decl)
10359 && !is_global_var (decl)
10360 && DECL_CONTEXT (decl) == current_function_decl)
10361 {
10362 tree c = gimplify_oacc_declare_1 (t);
10363 if (c)
10364 {
10365 if (oacc_declare_returns == NULL)
10366 oacc_declare_returns = new hash_map<tree, tree>;
10367
10368 oacc_declare_returns->put (decl, c);
10369 }
10370 }
10371
10372 if (gimplify_omp_ctxp)
10373 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
10374 }
10375
10376 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
10377 clauses);
10378
10379 gimplify_seq_add_stmt (pre_p, stmt);
10380
10381 *expr_p = NULL_TREE;
10382 }
10383
10384 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
10385 gimplification of the body, as well as scanning the body for used
10386 variables. We need to do this scan now, because variable-sized
10387 decls will be decomposed during gimplification. */
10388
10389 static void
10390 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
10391 {
10392 tree expr = *expr_p;
10393 gimple *g;
10394 gimple_seq body = NULL;
10395
10396 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
10397 OMP_PARALLEL_COMBINED (expr)
10398 ? ORT_COMBINED_PARALLEL
10399 : ORT_PARALLEL, OMP_PARALLEL);
10400
10401 push_gimplify_context ();
10402
10403 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
10404 if (gimple_code (g) == GIMPLE_BIND)
10405 pop_gimplify_context (g);
10406 else
10407 pop_gimplify_context (NULL);
10408
10409 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
10410 OMP_PARALLEL);
10411
10412 g = gimple_build_omp_parallel (body,
10413 OMP_PARALLEL_CLAUSES (expr),
10414 NULL_TREE, NULL_TREE);
10415 if (OMP_PARALLEL_COMBINED (expr))
10416 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
10417 gimplify_seq_add_stmt (pre_p, g);
10418 *expr_p = NULL_TREE;
10419 }
10420
10421 /* Gimplify the contents of an OMP_TASK statement. This involves
10422 gimplification of the body, as well as scanning the body for used
10423 variables. We need to do this scan now, because variable-sized
10424 decls will be decomposed during gimplification. */
10425
10426 static void
10427 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
10428 {
10429 tree expr = *expr_p;
10430 gimple *g;
10431 gimple_seq body = NULL;
10432
10433 if (OMP_TASK_BODY (expr) == NULL_TREE)
10434 for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10435 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10436 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
10437 {
10438 error_at (OMP_CLAUSE_LOCATION (c),
10439 "%<mutexinoutset%> kind in %<depend%> clause on a "
10440 "%<taskwait%> construct");
10441 break;
10442 }
10443
10444 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
10445 omp_find_clause (OMP_TASK_CLAUSES (expr),
10446 OMP_CLAUSE_UNTIED)
10447 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
10448
10449 if (OMP_TASK_BODY (expr))
10450 {
10451 push_gimplify_context ();
10452
10453 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
10454 if (gimple_code (g) == GIMPLE_BIND)
10455 pop_gimplify_context (g);
10456 else
10457 pop_gimplify_context (NULL);
10458 }
10459
10460 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
10461 OMP_TASK);
10462
10463 g = gimple_build_omp_task (body,
10464 OMP_TASK_CLAUSES (expr),
10465 NULL_TREE, NULL_TREE,
10466 NULL_TREE, NULL_TREE, NULL_TREE);
10467 if (OMP_TASK_BODY (expr) == NULL_TREE)
10468 gimple_omp_task_set_taskwait_p (g, true);
10469 gimplify_seq_add_stmt (pre_p, g);
10470 *expr_p = NULL_TREE;
10471 }
10472
10473 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
10474 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
10475 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
10476 is address of OMP_PARALLEL in between if any, pdata[2] is address of
10477 OMP_FOR in between if any and pdata[3] is address of the inner
10478 OMP_FOR/OMP_SIMD. */
10479
10480 static tree
10481 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
10482 {
10483 tree **pdata = (tree **) data;
10484 *walk_subtrees = 0;
10485 switch (TREE_CODE (*tp))
10486 {
10487 case OMP_FOR:
10488 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10489 {
10490 pdata[3] = tp;
10491 return *tp;
10492 }
10493 pdata[2] = tp;
10494 *walk_subtrees = 1;
10495 break;
10496 case OMP_SIMD:
10497 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10498 {
10499 pdata[3] = tp;
10500 return *tp;
10501 }
10502 break;
10503 case BIND_EXPR:
10504 if (BIND_EXPR_VARS (*tp)
10505 || (BIND_EXPR_BLOCK (*tp)
10506 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
10507 pdata[0] = tp;
10508 *walk_subtrees = 1;
10509 break;
10510 case STATEMENT_LIST:
10511 if (!tsi_one_before_end_p (tsi_start (*tp)))
10512 pdata[0] = tp;
10513 *walk_subtrees = 1;
10514 break;
10515 case TRY_FINALLY_EXPR:
10516 pdata[0] = tp;
10517 *walk_subtrees = 1;
10518 break;
10519 case OMP_PARALLEL:
10520 pdata[1] = tp;
10521 *walk_subtrees = 1;
10522 break;
10523 default:
10524 break;
10525 }
10526 return NULL_TREE;
10527 }
10528
10529 /* Gimplify the gross structure of an OMP_FOR statement. */
10530
10531 static enum gimplify_status
10532 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
10533 {
10534 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
10535 enum gimplify_status ret = GS_ALL_DONE;
10536 enum gimplify_status tret;
10537 gomp_for *gfor;
10538 gimple_seq for_body, for_pre_body;
10539 int i;
10540 bitmap has_decl_expr = NULL;
10541 enum omp_region_type ort = ORT_WORKSHARE;
10542
10543 orig_for_stmt = for_stmt = *expr_p;
10544
10545 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10546 {
10547 tree *data[4] = { NULL, NULL, NULL, NULL };
10548 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
10549 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
10550 find_combined_omp_for, data, NULL);
10551 if (inner_for_stmt == NULL_TREE)
10552 {
10553 gcc_assert (seen_error ());
10554 *expr_p = NULL_TREE;
10555 return GS_ERROR;
10556 }
10557 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
10558 {
10559 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
10560 &OMP_FOR_PRE_BODY (for_stmt));
10561 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
10562 }
10563 if (OMP_FOR_PRE_BODY (inner_for_stmt))
10564 {
10565 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
10566 &OMP_FOR_PRE_BODY (for_stmt));
10567 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
10568 }
10569
10570 if (data[0])
10571 {
10572 /* We have some statements or variable declarations in between
10573 the composite construct directives. Move them around the
10574 inner_for_stmt. */
10575 data[0] = expr_p;
10576 for (i = 0; i < 3; i++)
10577 if (data[i])
10578 {
10579 tree t = *data[i];
10580 if (i < 2 && data[i + 1] == &OMP_BODY (t))
10581 data[i + 1] = data[i];
10582 *data[i] = OMP_BODY (t);
10583 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10584 NULL_TREE, make_node (BLOCK));
10585 OMP_BODY (t) = body;
10586 append_to_statement_list_force (inner_for_stmt,
10587 &BIND_EXPR_BODY (body));
10588 *data[3] = t;
10589 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
10590 gcc_assert (*data[3] == inner_for_stmt);
10591 }
10592 return GS_OK;
10593 }
10594
10595 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10596 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10597 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10598 i)) == TREE_LIST
10599 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10600 i)))
10601 {
10602 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10603 /* Class iterators aren't allowed on OMP_SIMD, so the only
10604 case we need to solve is distribute parallel for. */
10605 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
10606 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
10607 && data[1]);
10608 tree orig_decl = TREE_PURPOSE (orig);
10609 tree last = TREE_VALUE (orig);
10610 tree *pc;
10611 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
10612 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
10613 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
10614 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
10615 && OMP_CLAUSE_DECL (*pc) == orig_decl)
10616 break;
10617 if (*pc == NULL_TREE)
10618 ;
10619 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
10620 {
10621 /* private clause will appear only on inner_for_stmt.
10622 Change it into firstprivate, and add private clause
10623 on for_stmt. */
10624 tree c = copy_node (*pc);
10625 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10626 OMP_FOR_CLAUSES (for_stmt) = c;
10627 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
10628 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10629 }
10630 else
10631 {
10632 /* lastprivate clause will appear on both inner_for_stmt
10633 and for_stmt. Add firstprivate clause to
10634 inner_for_stmt. */
10635 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
10636 OMP_CLAUSE_FIRSTPRIVATE);
10637 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
10638 OMP_CLAUSE_CHAIN (c) = *pc;
10639 *pc = c;
10640 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10641 }
10642 tree c = build_omp_clause (UNKNOWN_LOCATION,
10643 OMP_CLAUSE_FIRSTPRIVATE);
10644 OMP_CLAUSE_DECL (c) = last;
10645 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10646 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10647 c = build_omp_clause (UNKNOWN_LOCATION,
10648 *pc ? OMP_CLAUSE_SHARED
10649 : OMP_CLAUSE_FIRSTPRIVATE);
10650 OMP_CLAUSE_DECL (c) = orig_decl;
10651 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10652 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10653 }
10654 /* Similarly, take care of C++ range for temporaries, those should
10655 be firstprivate on OMP_PARALLEL if any. */
10656 if (data[1])
10657 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10658 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10659 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10660 i)) == TREE_LIST
10661 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10662 i)))
10663 {
10664 tree orig
10665 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10666 tree v = TREE_CHAIN (orig);
10667 tree c = build_omp_clause (UNKNOWN_LOCATION,
10668 OMP_CLAUSE_FIRSTPRIVATE);
10669 /* First add firstprivate clause for the __for_end artificial
10670 decl. */
10671 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
10672 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10673 == REFERENCE_TYPE)
10674 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10675 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10676 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10677 if (TREE_VEC_ELT (v, 0))
10678 {
10679 /* And now the same for __for_range artificial decl if it
10680 exists. */
10681 c = build_omp_clause (UNKNOWN_LOCATION,
10682 OMP_CLAUSE_FIRSTPRIVATE);
10683 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
10684 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10685 == REFERENCE_TYPE)
10686 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10687 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10688 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10689 }
10690 }
10691 }
10692
10693 switch (TREE_CODE (for_stmt))
10694 {
10695 case OMP_FOR:
10696 case OMP_DISTRIBUTE:
10697 break;
10698 case OACC_LOOP:
10699 ort = ORT_ACC;
10700 break;
10701 case OMP_TASKLOOP:
10702 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
10703 ort = ORT_UNTIED_TASKLOOP;
10704 else
10705 ort = ORT_TASKLOOP;
10706 break;
10707 case OMP_SIMD:
10708 ort = ORT_SIMD;
10709 break;
10710 default:
10711 gcc_unreachable ();
10712 }
10713
10714 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
10715 clause for the IV. */
10716 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10717 {
10718 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
10719 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10720 decl = TREE_OPERAND (t, 0);
10721 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
10722 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10723 && OMP_CLAUSE_DECL (c) == decl)
10724 {
10725 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10726 break;
10727 }
10728 }
10729
10730 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
10731 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
10732 TREE_CODE (for_stmt));
10733
10734 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
10735 gimplify_omp_ctxp->distribute = true;
10736
10737 /* Handle OMP_FOR_INIT. */
10738 for_pre_body = NULL;
10739 if ((ort == ORT_SIMD
10740 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
10741 && OMP_FOR_PRE_BODY (for_stmt))
10742 {
10743 has_decl_expr = BITMAP_ALLOC (NULL);
10744 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
10745 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
10746 == VAR_DECL)
10747 {
10748 t = OMP_FOR_PRE_BODY (for_stmt);
10749 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10750 }
10751 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
10752 {
10753 tree_stmt_iterator si;
10754 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
10755 tsi_next (&si))
10756 {
10757 t = tsi_stmt (si);
10758 if (TREE_CODE (t) == DECL_EXPR
10759 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
10760 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10761 }
10762 }
10763 }
10764 if (OMP_FOR_PRE_BODY (for_stmt))
10765 {
10766 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
10767 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10768 else
10769 {
10770 struct gimplify_omp_ctx ctx;
10771 memset (&ctx, 0, sizeof (ctx));
10772 ctx.region_type = ORT_NONE;
10773 gimplify_omp_ctxp = &ctx;
10774 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10775 gimplify_omp_ctxp = NULL;
10776 }
10777 }
10778 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
10779
10780 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10781 for_stmt = inner_for_stmt;
10782
10783 /* For taskloop, need to gimplify the start, end and step before the
10784 taskloop, outside of the taskloop omp context. */
10785 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10786 {
10787 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10788 {
10789 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10790 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10791 {
10792 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10793 TREE_OPERAND (t, 1)
10794 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10795 gimple_seq_empty_p (for_pre_body)
10796 ? pre_p : &for_pre_body, NULL,
10797 false);
10798 /* Reference to pointer conversion is considered useless,
10799 but is significant for firstprivate clause. Force it
10800 here. */
10801 if (TREE_CODE (type) == POINTER_TYPE
10802 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10803 == REFERENCE_TYPE))
10804 {
10805 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10806 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10807 TREE_OPERAND (t, 1));
10808 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10809 ? pre_p : &for_pre_body);
10810 TREE_OPERAND (t, 1) = v;
10811 }
10812 tree c = build_omp_clause (input_location,
10813 OMP_CLAUSE_FIRSTPRIVATE);
10814 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10815 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10816 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10817 }
10818
10819 /* Handle OMP_FOR_COND. */
10820 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10821 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10822 {
10823 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10824 TREE_OPERAND (t, 1)
10825 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10826 gimple_seq_empty_p (for_pre_body)
10827 ? pre_p : &for_pre_body, NULL,
10828 false);
10829 /* Reference to pointer conversion is considered useless,
10830 but is significant for firstprivate clause. Force it
10831 here. */
10832 if (TREE_CODE (type) == POINTER_TYPE
10833 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10834 == REFERENCE_TYPE))
10835 {
10836 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10837 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10838 TREE_OPERAND (t, 1));
10839 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10840 ? pre_p : &for_pre_body);
10841 TREE_OPERAND (t, 1) = v;
10842 }
10843 tree c = build_omp_clause (input_location,
10844 OMP_CLAUSE_FIRSTPRIVATE);
10845 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10846 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10847 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10848 }
10849
10850 /* Handle OMP_FOR_INCR. */
10851 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10852 if (TREE_CODE (t) == MODIFY_EXPR)
10853 {
10854 decl = TREE_OPERAND (t, 0);
10855 t = TREE_OPERAND (t, 1);
10856 tree *tp = &TREE_OPERAND (t, 1);
10857 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
10858 tp = &TREE_OPERAND (t, 0);
10859
10860 if (!is_gimple_constant (*tp))
10861 {
10862 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
10863 ? pre_p : &for_pre_body;
10864 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
10865 tree c = build_omp_clause (input_location,
10866 OMP_CLAUSE_FIRSTPRIVATE);
10867 OMP_CLAUSE_DECL (c) = *tp;
10868 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10869 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10870 }
10871 }
10872 }
10873
10874 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
10875 OMP_TASKLOOP);
10876 }
10877
10878 if (orig_for_stmt != for_stmt)
10879 gimplify_omp_ctxp->combined_loop = true;
10880
10881 for_body = NULL;
10882 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10883 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
10884 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10885 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
10886
10887 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
10888 bool is_doacross = false;
10889 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
10890 {
10891 is_doacross = true;
10892 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
10893 (OMP_FOR_INIT (for_stmt))
10894 * 2);
10895 }
10896 int collapse = 1, tile = 0;
10897 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
10898 if (c)
10899 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10900 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
10901 if (c)
10902 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
10903 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10904 {
10905 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10906 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10907 decl = TREE_OPERAND (t, 0);
10908 gcc_assert (DECL_P (decl));
10909 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
10910 || POINTER_TYPE_P (TREE_TYPE (decl)));
10911 if (is_doacross)
10912 {
10913 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
10914 {
10915 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
10916 if (TREE_CODE (orig_decl) == TREE_LIST)
10917 {
10918 orig_decl = TREE_PURPOSE (orig_decl);
10919 if (!orig_decl)
10920 orig_decl = decl;
10921 }
10922 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
10923 }
10924 else
10925 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10926 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10927 }
10928
10929 /* Make sure the iteration variable is private. */
10930 tree c = NULL_TREE;
10931 tree c2 = NULL_TREE;
10932 if (orig_for_stmt != for_stmt)
10933 {
10934 /* Preserve this information until we gimplify the inner simd. */
10935 if (has_decl_expr
10936 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10937 TREE_PRIVATE (t) = 1;
10938 }
10939 else if (ort == ORT_SIMD)
10940 {
10941 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10942 (splay_tree_key) decl);
10943 omp_is_private (gimplify_omp_ctxp, decl,
10944 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10945 != 1));
10946 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
10947 {
10948 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10949 if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
10950 for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
10951 OMP_CLAUSE_LASTPRIVATE);
10952 c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
10953 OMP_CLAUSE_LASTPRIVATE))
10954 if (OMP_CLAUSE_DECL (c3) == decl)
10955 {
10956 warning_at (OMP_CLAUSE_LOCATION (c3), 0,
10957 "conditional %<lastprivate%> on loop "
10958 "iterator %qD ignored", decl);
10959 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
10960 n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
10961 }
10962 }
10963 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10964 {
10965 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10966 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10967 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
10968 if ((has_decl_expr
10969 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10970 || TREE_PRIVATE (t))
10971 {
10972 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10973 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10974 }
10975 struct gimplify_omp_ctx *outer
10976 = gimplify_omp_ctxp->outer_context;
10977 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10978 {
10979 if (outer->region_type == ORT_WORKSHARE
10980 && outer->combined_loop)
10981 {
10982 n = splay_tree_lookup (outer->variables,
10983 (splay_tree_key)decl);
10984 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10985 {
10986 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10987 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10988 }
10989 else
10990 {
10991 struct gimplify_omp_ctx *octx = outer->outer_context;
10992 if (octx
10993 && octx->region_type == ORT_COMBINED_PARALLEL
10994 && octx->outer_context
10995 && (octx->outer_context->region_type
10996 == ORT_WORKSHARE)
10997 && octx->outer_context->combined_loop)
10998 {
10999 octx = octx->outer_context;
11000 n = splay_tree_lookup (octx->variables,
11001 (splay_tree_key)decl);
11002 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
11003 {
11004 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
11005 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
11006 }
11007 }
11008 }
11009 }
11010 }
11011
11012 OMP_CLAUSE_DECL (c) = decl;
11013 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
11014 OMP_FOR_CLAUSES (for_stmt) = c;
11015 omp_add_variable (gimplify_omp_ctxp, decl, flags);
11016 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
11017 {
11018 if (outer->region_type == ORT_WORKSHARE
11019 && outer->combined_loop)
11020 {
11021 if (outer->outer_context
11022 && (outer->outer_context->region_type
11023 == ORT_COMBINED_PARALLEL))
11024 outer = outer->outer_context;
11025 else if (omp_check_private (outer, decl, false))
11026 outer = NULL;
11027 }
11028 else if (((outer->region_type & ORT_TASKLOOP)
11029 == ORT_TASKLOOP)
11030 && outer->combined_loop
11031 && !omp_check_private (gimplify_omp_ctxp,
11032 decl, false))
11033 ;
11034 else if (outer->region_type != ORT_COMBINED_PARALLEL)
11035 {
11036 omp_notice_variable (outer, decl, true);
11037 outer = NULL;
11038 }
11039 if (outer)
11040 {
11041 n = splay_tree_lookup (outer->variables,
11042 (splay_tree_key)decl);
11043 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
11044 {
11045 omp_add_variable (outer, decl,
11046 GOVD_LASTPRIVATE | GOVD_SEEN);
11047 if (outer->region_type == ORT_COMBINED_PARALLEL
11048 && outer->outer_context
11049 && (outer->outer_context->region_type
11050 == ORT_WORKSHARE)
11051 && outer->outer_context->combined_loop)
11052 {
11053 outer = outer->outer_context;
11054 n = splay_tree_lookup (outer->variables,
11055 (splay_tree_key)decl);
11056 if (omp_check_private (outer, decl, false))
11057 outer = NULL;
11058 else if (n == NULL
11059 || ((n->value & GOVD_DATA_SHARE_CLASS)
11060 == 0))
11061 omp_add_variable (outer, decl,
11062 GOVD_LASTPRIVATE
11063 | GOVD_SEEN);
11064 else
11065 outer = NULL;
11066 }
11067 if (outer && outer->outer_context
11068 && ((outer->outer_context->region_type
11069 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
11070 || (((outer->region_type & ORT_TASKLOOP)
11071 == ORT_TASKLOOP)
11072 && (outer->outer_context->region_type
11073 == ORT_COMBINED_PARALLEL))))
11074 {
11075 outer = outer->outer_context;
11076 n = splay_tree_lookup (outer->variables,
11077 (splay_tree_key)decl);
11078 if (n == NULL
11079 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
11080 omp_add_variable (outer, decl,
11081 GOVD_SHARED | GOVD_SEEN);
11082 else
11083 outer = NULL;
11084 }
11085 if (outer && outer->outer_context)
11086 omp_notice_variable (outer->outer_context, decl,
11087 true);
11088 }
11089 }
11090 }
11091 }
11092 else
11093 {
11094 bool lastprivate
11095 = (!has_decl_expr
11096 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
11097 if (TREE_PRIVATE (t))
11098 lastprivate = false;
11099 struct gimplify_omp_ctx *outer
11100 = gimplify_omp_ctxp->outer_context;
11101 if (outer && lastprivate)
11102 {
11103 if (outer->region_type == ORT_WORKSHARE
11104 && outer->combined_loop)
11105 {
11106 n = splay_tree_lookup (outer->variables,
11107 (splay_tree_key)decl);
11108 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
11109 {
11110 lastprivate = false;
11111 outer = NULL;
11112 }
11113 else if (outer->outer_context
11114 && (outer->outer_context->region_type
11115 == ORT_COMBINED_PARALLEL))
11116 outer = outer->outer_context;
11117 else if (omp_check_private (outer, decl, false))
11118 outer = NULL;
11119 }
11120 else if (((outer->region_type & ORT_TASKLOOP)
11121 == ORT_TASKLOOP)
11122 && outer->combined_loop
11123 && !omp_check_private (gimplify_omp_ctxp,
11124 decl, false))
11125 ;
11126 else if (outer->region_type != ORT_COMBINED_PARALLEL)
11127 {
11128 omp_notice_variable (outer, decl, true);
11129 outer = NULL;
11130 }
11131 if (outer)
11132 {
11133 n = splay_tree_lookup (outer->variables,
11134 (splay_tree_key)decl);
11135 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
11136 {
11137 omp_add_variable (outer, decl,
11138 GOVD_LASTPRIVATE | GOVD_SEEN);
11139 if (outer->region_type == ORT_COMBINED_PARALLEL
11140 && outer->outer_context
11141 && (outer->outer_context->region_type
11142 == ORT_WORKSHARE)
11143 && outer->outer_context->combined_loop)
11144 {
11145 outer = outer->outer_context;
11146 n = splay_tree_lookup (outer->variables,
11147 (splay_tree_key)decl);
11148 if (omp_check_private (outer, decl, false))
11149 outer = NULL;
11150 else if (n == NULL
11151 || ((n->value & GOVD_DATA_SHARE_CLASS)
11152 == 0))
11153 omp_add_variable (outer, decl,
11154 GOVD_LASTPRIVATE
11155 | GOVD_SEEN);
11156 else
11157 outer = NULL;
11158 }
11159 if (outer && outer->outer_context
11160 && ((outer->outer_context->region_type
11161 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
11162 || (((outer->region_type & ORT_TASKLOOP)
11163 == ORT_TASKLOOP)
11164 && (outer->outer_context->region_type
11165 == ORT_COMBINED_PARALLEL))))
11166 {
11167 outer = outer->outer_context;
11168 n = splay_tree_lookup (outer->variables,
11169 (splay_tree_key)decl);
11170 if (n == NULL
11171 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
11172 omp_add_variable (outer, decl,
11173 GOVD_SHARED | GOVD_SEEN);
11174 else
11175 outer = NULL;
11176 }
11177 if (outer && outer->outer_context)
11178 omp_notice_variable (outer->outer_context, decl,
11179 true);
11180 }
11181 }
11182 }
11183
11184 c = build_omp_clause (input_location,
11185 lastprivate ? OMP_CLAUSE_LASTPRIVATE
11186 : OMP_CLAUSE_PRIVATE);
11187 OMP_CLAUSE_DECL (c) = decl;
11188 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
11189 OMP_FOR_CLAUSES (for_stmt) = c;
11190 omp_add_variable (gimplify_omp_ctxp, decl,
11191 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
11192 | GOVD_EXPLICIT | GOVD_SEEN);
11193 c = NULL_TREE;
11194 }
11195 }
11196 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
11197 {
11198 omp_notice_variable (gimplify_omp_ctxp, decl, true);
11199 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
11200 (splay_tree_key) decl);
11201 if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
11202 for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
11203 OMP_CLAUSE_LASTPRIVATE);
11204 c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
11205 OMP_CLAUSE_LASTPRIVATE))
11206 if (OMP_CLAUSE_DECL (c3) == decl)
11207 {
11208 warning_at (OMP_CLAUSE_LOCATION (c3), 0,
11209 "conditional %<lastprivate%> on loop "
11210 "iterator %qD ignored", decl);
11211 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
11212 n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
11213 }
11214 }
11215 else
11216 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
11217
11218 /* If DECL is not a gimple register, create a temporary variable to act
11219 as an iteration counter. This is valid, since DECL cannot be
11220 modified in the body of the loop. Similarly for any iteration vars
11221 in simd with collapse > 1 where the iterator vars must be
11222 lastprivate. */
11223 if (orig_for_stmt != for_stmt)
11224 var = decl;
11225 else if (!is_gimple_reg (decl)
11226 || (ort == ORT_SIMD
11227 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
11228 {
11229 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
11230 /* Make sure omp_add_variable is not called on it prematurely.
11231 We call it ourselves a few lines later. */
11232 gimplify_omp_ctxp = NULL;
11233 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
11234 gimplify_omp_ctxp = ctx;
11235 TREE_OPERAND (t, 0) = var;
11236
11237 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
11238
11239 if (ort == ORT_SIMD
11240 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
11241 {
11242 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
11243 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
11244 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
11245 OMP_CLAUSE_DECL (c2) = var;
11246 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
11247 OMP_FOR_CLAUSES (for_stmt) = c2;
11248 omp_add_variable (gimplify_omp_ctxp, var,
11249 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
11250 if (c == NULL_TREE)
11251 {
11252 c = c2;
11253 c2 = NULL_TREE;
11254 }
11255 }
11256 else
11257 omp_add_variable (gimplify_omp_ctxp, var,
11258 GOVD_PRIVATE | GOVD_SEEN);
11259 }
11260 else
11261 var = decl;
11262
11263 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11264 is_gimple_val, fb_rvalue, false);
11265 ret = MIN (ret, tret);
11266 if (ret == GS_ERROR)
11267 return ret;
11268
11269 /* Handle OMP_FOR_COND. */
11270 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11271 gcc_assert (COMPARISON_CLASS_P (t));
11272 gcc_assert (TREE_OPERAND (t, 0) == decl);
11273
11274 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11275 is_gimple_val, fb_rvalue, false);
11276 ret = MIN (ret, tret);
11277
11278 /* Handle OMP_FOR_INCR. */
11279 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11280 switch (TREE_CODE (t))
11281 {
11282 case PREINCREMENT_EXPR:
11283 case POSTINCREMENT_EXPR:
11284 {
11285 tree decl = TREE_OPERAND (t, 0);
11286 /* c_omp_for_incr_canonicalize_ptr() should have been
11287 called to massage things appropriately. */
11288 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11289
11290 if (orig_for_stmt != for_stmt)
11291 break;
11292 t = build_int_cst (TREE_TYPE (decl), 1);
11293 if (c)
11294 OMP_CLAUSE_LINEAR_STEP (c) = t;
11295 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11296 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11297 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11298 break;
11299 }
11300
11301 case PREDECREMENT_EXPR:
11302 case POSTDECREMENT_EXPR:
11303 /* c_omp_for_incr_canonicalize_ptr() should have been
11304 called to massage things appropriately. */
11305 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11306 if (orig_for_stmt != for_stmt)
11307 break;
11308 t = build_int_cst (TREE_TYPE (decl), -1);
11309 if (c)
11310 OMP_CLAUSE_LINEAR_STEP (c) = t;
11311 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11312 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11313 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11314 break;
11315
11316 case MODIFY_EXPR:
11317 gcc_assert (TREE_OPERAND (t, 0) == decl);
11318 TREE_OPERAND (t, 0) = var;
11319
11320 t = TREE_OPERAND (t, 1);
11321 switch (TREE_CODE (t))
11322 {
11323 case PLUS_EXPR:
11324 if (TREE_OPERAND (t, 1) == decl)
11325 {
11326 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
11327 TREE_OPERAND (t, 0) = var;
11328 break;
11329 }
11330
11331 /* Fallthru. */
11332 case MINUS_EXPR:
11333 case POINTER_PLUS_EXPR:
11334 gcc_assert (TREE_OPERAND (t, 0) == decl);
11335 TREE_OPERAND (t, 0) = var;
11336 break;
11337 default:
11338 gcc_unreachable ();
11339 }
11340
11341 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11342 is_gimple_val, fb_rvalue, false);
11343 ret = MIN (ret, tret);
11344 if (c)
11345 {
11346 tree step = TREE_OPERAND (t, 1);
11347 tree stept = TREE_TYPE (decl);
11348 if (POINTER_TYPE_P (stept))
11349 stept = sizetype;
11350 step = fold_convert (stept, step);
11351 if (TREE_CODE (t) == MINUS_EXPR)
11352 step = fold_build1 (NEGATE_EXPR, stept, step);
11353 OMP_CLAUSE_LINEAR_STEP (c) = step;
11354 if (step != TREE_OPERAND (t, 1))
11355 {
11356 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
11357 &for_pre_body, NULL,
11358 is_gimple_val, fb_rvalue, false);
11359 ret = MIN (ret, tret);
11360 }
11361 }
11362 break;
11363
11364 default:
11365 gcc_unreachable ();
11366 }
11367
11368 if (c2)
11369 {
11370 gcc_assert (c);
11371 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
11372 }
11373
11374 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
11375 {
11376 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
11377 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11378 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
11379 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
11380 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
11381 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
11382 && OMP_CLAUSE_DECL (c) == decl)
11383 {
11384 if (is_doacross && (collapse == 1 || i >= collapse))
11385 t = var;
11386 else
11387 {
11388 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11389 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
11390 gcc_assert (TREE_OPERAND (t, 0) == var);
11391 t = TREE_OPERAND (t, 1);
11392 gcc_assert (TREE_CODE (t) == PLUS_EXPR
11393 || TREE_CODE (t) == MINUS_EXPR
11394 || TREE_CODE (t) == POINTER_PLUS_EXPR);
11395 gcc_assert (TREE_OPERAND (t, 0) == var);
11396 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
11397 is_doacross ? var : decl,
11398 TREE_OPERAND (t, 1));
11399 }
11400 gimple_seq *seq;
11401 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11402 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
11403 else
11404 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
11405 push_gimplify_context ();
11406 gimplify_assign (decl, t, seq);
11407 gimple *bind = NULL;
11408 if (gimplify_ctxp->temps)
11409 {
11410 bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
11411 *seq = NULL;
11412 gimplify_seq_add_stmt (seq, bind);
11413 }
11414 pop_gimplify_context (bind);
11415 }
11416 }
11417 }
11418
11419 BITMAP_FREE (has_decl_expr);
11420
11421 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11422 {
11423 push_gimplify_context ();
11424 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
11425 {
11426 OMP_FOR_BODY (orig_for_stmt)
11427 = build3 (BIND_EXPR, void_type_node, NULL,
11428 OMP_FOR_BODY (orig_for_stmt), NULL);
11429 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
11430 }
11431 }
11432
11433 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
11434 &for_body);
11435
11436 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11437 {
11438 if (gimple_code (g) == GIMPLE_BIND)
11439 pop_gimplify_context (g);
11440 else
11441 pop_gimplify_context (NULL);
11442 }
11443
11444 if (orig_for_stmt != for_stmt)
11445 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11446 {
11447 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11448 decl = TREE_OPERAND (t, 0);
11449 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
11450 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11451 gimplify_omp_ctxp = ctx->outer_context;
11452 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
11453 gimplify_omp_ctxp = ctx;
11454 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
11455 TREE_OPERAND (t, 0) = var;
11456 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11457 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
11458 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
11459 }
11460
11461 gimplify_adjust_omp_clauses (pre_p, for_body,
11462 &OMP_FOR_CLAUSES (orig_for_stmt),
11463 TREE_CODE (orig_for_stmt));
11464
11465 int kind;
11466 switch (TREE_CODE (orig_for_stmt))
11467 {
11468 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
11469 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
11470 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
11471 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
11472 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
11473 default:
11474 gcc_unreachable ();
11475 }
11476 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
11477 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
11478 for_pre_body);
11479 if (orig_for_stmt != for_stmt)
11480 gimple_omp_for_set_combined_p (gfor, true);
11481 if (gimplify_omp_ctxp
11482 && (gimplify_omp_ctxp->combined_loop
11483 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
11484 && gimplify_omp_ctxp->outer_context
11485 && gimplify_omp_ctxp->outer_context->combined_loop)))
11486 {
11487 gimple_omp_for_set_combined_into_p (gfor, true);
11488 if (gimplify_omp_ctxp->combined_loop)
11489 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
11490 else
11491 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
11492 }
11493
11494 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11495 {
11496 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11497 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
11498 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
11499 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11500 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
11501 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
11502 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11503 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
11504 }
11505
11506 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
11507 constructs with GIMPLE_OMP_TASK sandwiched in between them.
11508 The outer taskloop stands for computing the number of iterations,
11509 counts for collapsed loops and holding taskloop specific clauses.
11510 The task construct stands for the effect of data sharing on the
11511 explicit task it creates and the inner taskloop stands for expansion
11512 of the static loop inside of the explicit task construct. */
11513 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11514 {
11515 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
11516 tree task_clauses = NULL_TREE;
11517 tree c = *gfor_clauses_ptr;
11518 tree *gtask_clauses_ptr = &task_clauses;
11519 tree outer_for_clauses = NULL_TREE;
11520 tree *gforo_clauses_ptr = &outer_for_clauses;
11521 for (; c; c = OMP_CLAUSE_CHAIN (c))
11522 switch (OMP_CLAUSE_CODE (c))
11523 {
11524 /* These clauses are allowed on task, move them there. */
11525 case OMP_CLAUSE_SHARED:
11526 case OMP_CLAUSE_FIRSTPRIVATE:
11527 case OMP_CLAUSE_DEFAULT:
11528 case OMP_CLAUSE_IF:
11529 case OMP_CLAUSE_UNTIED:
11530 case OMP_CLAUSE_FINAL:
11531 case OMP_CLAUSE_MERGEABLE:
11532 case OMP_CLAUSE_PRIORITY:
11533 case OMP_CLAUSE_REDUCTION:
11534 case OMP_CLAUSE_IN_REDUCTION:
11535 *gtask_clauses_ptr = c;
11536 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11537 break;
11538 case OMP_CLAUSE_PRIVATE:
11539 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
11540 {
11541 /* We want private on outer for and firstprivate
11542 on task. */
11543 *gtask_clauses_ptr
11544 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11545 OMP_CLAUSE_FIRSTPRIVATE);
11546 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11547 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11548 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11549 *gforo_clauses_ptr = c;
11550 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11551 }
11552 else
11553 {
11554 *gtask_clauses_ptr = c;
11555 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11556 }
11557 break;
11558 /* These clauses go into outer taskloop clauses. */
11559 case OMP_CLAUSE_GRAINSIZE:
11560 case OMP_CLAUSE_NUM_TASKS:
11561 case OMP_CLAUSE_NOGROUP:
11562 *gforo_clauses_ptr = c;
11563 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11564 break;
11565 /* Taskloop clause we duplicate on both taskloops. */
11566 case OMP_CLAUSE_COLLAPSE:
11567 *gfor_clauses_ptr = c;
11568 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11569 *gforo_clauses_ptr = copy_node (c);
11570 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11571 break;
11572 /* For lastprivate, keep the clause on inner taskloop, and add
11573 a shared clause on task. If the same decl is also firstprivate,
11574 add also firstprivate clause on the inner taskloop. */
11575 case OMP_CLAUSE_LASTPRIVATE:
11576 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
11577 {
11578 /* For taskloop C++ lastprivate IVs, we want:
11579 1) private on outer taskloop
11580 2) firstprivate and shared on task
11581 3) lastprivate on inner taskloop */
11582 *gtask_clauses_ptr
11583 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11584 OMP_CLAUSE_FIRSTPRIVATE);
11585 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11586 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11587 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11588 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
11589 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11590 OMP_CLAUSE_PRIVATE);
11591 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
11592 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
11593 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
11594 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11595 }
11596 *gfor_clauses_ptr = c;
11597 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11598 *gtask_clauses_ptr
11599 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
11600 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11601 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
11602 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
11603 gtask_clauses_ptr
11604 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11605 break;
11606 default:
11607 gcc_unreachable ();
11608 }
11609 *gfor_clauses_ptr = NULL_TREE;
11610 *gtask_clauses_ptr = NULL_TREE;
11611 *gforo_clauses_ptr = NULL_TREE;
11612 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
11613 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
11614 NULL_TREE, NULL_TREE, NULL_TREE);
11615 gimple_omp_task_set_taskloop_p (g, true);
11616 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
11617 gomp_for *gforo
11618 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
11619 gimple_omp_for_collapse (gfor),
11620 gimple_omp_for_pre_body (gfor));
11621 gimple_omp_for_set_pre_body (gfor, NULL);
11622 gimple_omp_for_set_combined_p (gforo, true);
11623 gimple_omp_for_set_combined_into_p (gfor, true);
11624 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
11625 {
11626 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
11627 tree v = create_tmp_var (type);
11628 gimple_omp_for_set_index (gforo, i, v);
11629 t = unshare_expr (gimple_omp_for_initial (gfor, i));
11630 gimple_omp_for_set_initial (gforo, i, t);
11631 gimple_omp_for_set_cond (gforo, i,
11632 gimple_omp_for_cond (gfor, i));
11633 t = unshare_expr (gimple_omp_for_final (gfor, i));
11634 gimple_omp_for_set_final (gforo, i, t);
11635 t = unshare_expr (gimple_omp_for_incr (gfor, i));
11636 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
11637 TREE_OPERAND (t, 0) = v;
11638 gimple_omp_for_set_incr (gforo, i, t);
11639 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11640 OMP_CLAUSE_DECL (t) = v;
11641 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
11642 gimple_omp_for_set_clauses (gforo, t);
11643 }
11644 gimplify_seq_add_stmt (pre_p, gforo);
11645 }
11646 else
11647 gimplify_seq_add_stmt (pre_p, gfor);
11648
11649 if (TREE_CODE (orig_for_stmt) == OMP_FOR)
11650 {
11651 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
11652 unsigned lastprivate_conditional = 0;
11653 while (ctx
11654 && (ctx->region_type == ORT_TARGET_DATA
11655 || ctx->region_type == ORT_TASKGROUP))
11656 ctx = ctx->outer_context;
11657 if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
11658 for (tree c = gimple_omp_for_clauses (gfor);
11659 c; c = OMP_CLAUSE_CHAIN (c))
11660 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11661 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
11662 ++lastprivate_conditional;
11663 if (lastprivate_conditional)
11664 {
11665 struct omp_for_data fd;
11666 omp_extract_for_data (gfor, &fd, NULL);
11667 tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
11668 lastprivate_conditional);
11669 tree var = create_tmp_var_raw (type);
11670 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
11671 OMP_CLAUSE_DECL (c) = var;
11672 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
11673 gimple_omp_for_set_clauses (gfor, c);
11674 omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
11675 }
11676 }
11677 else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
11678 {
11679 unsigned lastprivate_conditional = 0;
11680 for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
11681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11682 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
11683 ++lastprivate_conditional;
11684 if (lastprivate_conditional)
11685 {
11686 struct omp_for_data fd;
11687 omp_extract_for_data (gfor, &fd, NULL);
11688 tree type = unsigned_type_for (fd.iter_type);
11689 while (lastprivate_conditional--)
11690 {
11691 tree c = build_omp_clause (UNKNOWN_LOCATION,
11692 OMP_CLAUSE__CONDTEMP_);
11693 OMP_CLAUSE_DECL (c) = create_tmp_var (type);
11694 OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
11695 gimple_omp_for_set_clauses (gfor, c);
11696 }
11697 }
11698 }
11699
11700 if (ret != GS_ALL_DONE)
11701 return GS_ERROR;
11702 *expr_p = NULL_TREE;
11703 return GS_ALL_DONE;
11704 }
11705
11706 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
11707 of OMP_TARGET's body. */
11708
11709 static tree
11710 find_omp_teams (tree *tp, int *walk_subtrees, void *)
11711 {
11712 *walk_subtrees = 0;
11713 switch (TREE_CODE (*tp))
11714 {
11715 case OMP_TEAMS:
11716 return *tp;
11717 case BIND_EXPR:
11718 case STATEMENT_LIST:
11719 *walk_subtrees = 1;
11720 break;
11721 default:
11722 break;
11723 }
11724 return NULL_TREE;
11725 }
11726
11727 /* Helper function of optimize_target_teams, determine if the expression
11728 can be computed safely before the target construct on the host. */
11729
11730 static tree
11731 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
11732 {
11733 splay_tree_node n;
11734
11735 if (TYPE_P (*tp))
11736 {
11737 *walk_subtrees = 0;
11738 return NULL_TREE;
11739 }
11740 switch (TREE_CODE (*tp))
11741 {
11742 case VAR_DECL:
11743 case PARM_DECL:
11744 case RESULT_DECL:
11745 *walk_subtrees = 0;
11746 if (error_operand_p (*tp)
11747 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
11748 || DECL_HAS_VALUE_EXPR_P (*tp)
11749 || DECL_THREAD_LOCAL_P (*tp)
11750 || TREE_SIDE_EFFECTS (*tp)
11751 || TREE_THIS_VOLATILE (*tp))
11752 return *tp;
11753 if (is_global_var (*tp)
11754 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
11755 || lookup_attribute ("omp declare target link",
11756 DECL_ATTRIBUTES (*tp))))
11757 return *tp;
11758 if (VAR_P (*tp)
11759 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
11760 && !is_global_var (*tp)
11761 && decl_function_context (*tp) == current_function_decl)
11762 return *tp;
11763 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
11764 (splay_tree_key) *tp);
11765 if (n == NULL)
11766 {
11767 if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
11768 return NULL_TREE;
11769 return *tp;
11770 }
11771 else if (n->value & GOVD_LOCAL)
11772 return *tp;
11773 else if (n->value & GOVD_FIRSTPRIVATE)
11774 return NULL_TREE;
11775 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11776 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11777 return NULL_TREE;
11778 return *tp;
11779 case INTEGER_CST:
11780 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11781 return *tp;
11782 return NULL_TREE;
11783 case TARGET_EXPR:
11784 if (TARGET_EXPR_INITIAL (*tp)
11785 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
11786 return *tp;
11787 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
11788 walk_subtrees, NULL);
11789 /* Allow some reasonable subset of integral arithmetics. */
11790 case PLUS_EXPR:
11791 case MINUS_EXPR:
11792 case MULT_EXPR:
11793 case TRUNC_DIV_EXPR:
11794 case CEIL_DIV_EXPR:
11795 case FLOOR_DIV_EXPR:
11796 case ROUND_DIV_EXPR:
11797 case TRUNC_MOD_EXPR:
11798 case CEIL_MOD_EXPR:
11799 case FLOOR_MOD_EXPR:
11800 case ROUND_MOD_EXPR:
11801 case RDIV_EXPR:
11802 case EXACT_DIV_EXPR:
11803 case MIN_EXPR:
11804 case MAX_EXPR:
11805 case LSHIFT_EXPR:
11806 case RSHIFT_EXPR:
11807 case BIT_IOR_EXPR:
11808 case BIT_XOR_EXPR:
11809 case BIT_AND_EXPR:
11810 case NEGATE_EXPR:
11811 case ABS_EXPR:
11812 case BIT_NOT_EXPR:
11813 case NON_LVALUE_EXPR:
11814 CASE_CONVERT:
11815 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11816 return *tp;
11817 return NULL_TREE;
11818 /* And disallow anything else, except for comparisons. */
11819 default:
11820 if (COMPARISON_CLASS_P (*tp))
11821 return NULL_TREE;
11822 return *tp;
11823 }
11824 }
11825
11826 /* Try to determine if the num_teams and/or thread_limit expressions
11827 can have their values determined already before entering the
11828 target construct.
11829 INTEGER_CSTs trivially are,
11830 integral decls that are firstprivate (explicitly or implicitly)
11831 or explicitly map(always, to:) or map(always, tofrom:) on the target
11832 region too, and expressions involving simple arithmetics on those
11833 too, function calls are not ok, dereferencing something neither etc.
11834 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
11835 EXPR based on what we find:
11836 0 stands for clause not specified at all, use implementation default
11837 -1 stands for value that can't be determined easily before entering
11838 the target construct.
11839 If teams construct is not present at all, use 1 for num_teams
11840 and 0 for thread_limit (only one team is involved, and the thread
11841 limit is implementation defined. */
11842
11843 static void
11844 optimize_target_teams (tree target, gimple_seq *pre_p)
11845 {
11846 tree body = OMP_BODY (target);
11847 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
11848 tree num_teams = integer_zero_node;
11849 tree thread_limit = integer_zero_node;
11850 location_t num_teams_loc = EXPR_LOCATION (target);
11851 location_t thread_limit_loc = EXPR_LOCATION (target);
11852 tree c, *p, expr;
11853 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
11854
11855 if (teams == NULL_TREE)
11856 num_teams = integer_one_node;
11857 else
11858 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
11859 {
11860 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
11861 {
11862 p = &num_teams;
11863 num_teams_loc = OMP_CLAUSE_LOCATION (c);
11864 }
11865 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
11866 {
11867 p = &thread_limit;
11868 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
11869 }
11870 else
11871 continue;
11872 expr = OMP_CLAUSE_OPERAND (c, 0);
11873 if (TREE_CODE (expr) == INTEGER_CST)
11874 {
11875 *p = expr;
11876 continue;
11877 }
11878 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
11879 {
11880 *p = integer_minus_one_node;
11881 continue;
11882 }
11883 *p = expr;
11884 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
11885 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
11886 == GS_ERROR)
11887 {
11888 gimplify_omp_ctxp = target_ctx;
11889 *p = integer_minus_one_node;
11890 continue;
11891 }
11892 gimplify_omp_ctxp = target_ctx;
11893 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
11894 OMP_CLAUSE_OPERAND (c, 0) = *p;
11895 }
11896 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11897 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
11898 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11899 OMP_TARGET_CLAUSES (target) = c;
11900 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11901 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
11902 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11903 OMP_TARGET_CLAUSES (target) = c;
11904 }
11905
11906 /* Gimplify the gross structure of several OMP constructs. */
11907
11908 static void
11909 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
11910 {
11911 tree expr = *expr_p;
11912 gimple *stmt;
11913 gimple_seq body = NULL;
11914 enum omp_region_type ort;
11915
11916 switch (TREE_CODE (expr))
11917 {
11918 case OMP_SECTIONS:
11919 case OMP_SINGLE:
11920 ort = ORT_WORKSHARE;
11921 break;
11922 case OMP_TARGET:
11923 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
11924 break;
11925 case OACC_KERNELS:
11926 ort = ORT_ACC_KERNELS;
11927 break;
11928 case OACC_PARALLEL:
11929 ort = ORT_ACC_PARALLEL;
11930 break;
11931 case OACC_DATA:
11932 ort = ORT_ACC_DATA;
11933 break;
11934 case OMP_TARGET_DATA:
11935 ort = ORT_TARGET_DATA;
11936 break;
11937 case OMP_TEAMS:
11938 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
11939 if (gimplify_omp_ctxp == NULL
11940 || (gimplify_omp_ctxp->region_type == ORT_TARGET
11941 && gimplify_omp_ctxp->outer_context == NULL
11942 && lookup_attribute ("omp declare target",
11943 DECL_ATTRIBUTES (current_function_decl))))
11944 ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
11945 break;
11946 case OACC_HOST_DATA:
11947 ort = ORT_ACC_HOST_DATA;
11948 break;
11949 default:
11950 gcc_unreachable ();
11951 }
11952 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
11953 TREE_CODE (expr));
11954 if (TREE_CODE (expr) == OMP_TARGET)
11955 optimize_target_teams (expr, pre_p);
11956 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
11957 || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
11958 {
11959 push_gimplify_context ();
11960 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
11961 if (gimple_code (g) == GIMPLE_BIND)
11962 pop_gimplify_context (g);
11963 else
11964 pop_gimplify_context (NULL);
11965 if ((ort & ORT_TARGET_DATA) != 0)
11966 {
11967 enum built_in_function end_ix;
11968 switch (TREE_CODE (expr))
11969 {
11970 case OACC_DATA:
11971 case OACC_HOST_DATA:
11972 end_ix = BUILT_IN_GOACC_DATA_END;
11973 break;
11974 case OMP_TARGET_DATA:
11975 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
11976 break;
11977 default:
11978 gcc_unreachable ();
11979 }
11980 tree fn = builtin_decl_explicit (end_ix);
11981 g = gimple_build_call (fn, 0);
11982 gimple_seq cleanup = NULL;
11983 gimple_seq_add_stmt (&cleanup, g);
11984 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11985 body = NULL;
11986 gimple_seq_add_stmt (&body, g);
11987 }
11988 }
11989 else
11990 gimplify_and_add (OMP_BODY (expr), &body);
11991 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
11992 TREE_CODE (expr));
11993
11994 switch (TREE_CODE (expr))
11995 {
11996 case OACC_DATA:
11997 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
11998 OMP_CLAUSES (expr));
11999 break;
12000 case OACC_KERNELS:
12001 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
12002 OMP_CLAUSES (expr));
12003 break;
12004 case OACC_HOST_DATA:
12005 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
12006 OMP_CLAUSES (expr));
12007 break;
12008 case OACC_PARALLEL:
12009 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
12010 OMP_CLAUSES (expr));
12011 break;
12012 case OMP_SECTIONS:
12013 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
12014 break;
12015 case OMP_SINGLE:
12016 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
12017 break;
12018 case OMP_TARGET:
12019 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
12020 OMP_CLAUSES (expr));
12021 break;
12022 case OMP_TARGET_DATA:
12023 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
12024 OMP_CLAUSES (expr));
12025 break;
12026 case OMP_TEAMS:
12027 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
12028 if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
12029 gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
12030 break;
12031 default:
12032 gcc_unreachable ();
12033 }
12034
12035 gimplify_seq_add_stmt (pre_p, stmt);
12036 *expr_p = NULL_TREE;
12037 }
12038
12039 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
12040 target update constructs. */
12041
12042 static void
12043 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
12044 {
12045 tree expr = *expr_p;
12046 int kind;
12047 gomp_target *stmt;
12048 enum omp_region_type ort = ORT_WORKSHARE;
12049
12050 switch (TREE_CODE (expr))
12051 {
12052 case OACC_ENTER_DATA:
12053 case OACC_EXIT_DATA:
12054 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
12055 ort = ORT_ACC;
12056 break;
12057 case OACC_UPDATE:
12058 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
12059 ort = ORT_ACC;
12060 break;
12061 case OMP_TARGET_UPDATE:
12062 kind = GF_OMP_TARGET_KIND_UPDATE;
12063 break;
12064 case OMP_TARGET_ENTER_DATA:
12065 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
12066 break;
12067 case OMP_TARGET_EXIT_DATA:
12068 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
12069 break;
12070 default:
12071 gcc_unreachable ();
12072 }
12073 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
12074 ort, TREE_CODE (expr));
12075 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
12076 TREE_CODE (expr));
12077 if (TREE_CODE (expr) == OACC_UPDATE
12078 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
12079 OMP_CLAUSE_IF_PRESENT))
12080 {
12081 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
12082 clause. */
12083 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
12084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
12085 switch (OMP_CLAUSE_MAP_KIND (c))
12086 {
12087 case GOMP_MAP_FORCE_TO:
12088 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
12089 break;
12090 case GOMP_MAP_FORCE_FROM:
12091 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
12092 break;
12093 default:
12094 break;
12095 }
12096 }
12097 else if (TREE_CODE (expr) == OACC_EXIT_DATA
12098 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
12099 OMP_CLAUSE_FINALIZE))
12100 {
12101 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
12102 semantics apply to all mappings of this OpenACC directive. */
12103 bool finalize_marked = false;
12104 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
12105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
12106 switch (OMP_CLAUSE_MAP_KIND (c))
12107 {
12108 case GOMP_MAP_FROM:
12109 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
12110 finalize_marked = true;
12111 break;
12112 case GOMP_MAP_RELEASE:
12113 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
12114 finalize_marked = true;
12115 break;
12116 default:
12117 /* Check consistency: libgomp relies on the very first data
12118 mapping clause being marked, so make sure we did that before
12119 any other mapping clauses. */
12120 gcc_assert (finalize_marked);
12121 break;
12122 }
12123 }
12124 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
12125
12126 gimplify_seq_add_stmt (pre_p, stmt);
12127 *expr_p = NULL_TREE;
12128 }
12129
12130 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
12131 stabilized the lhs of the atomic operation as *ADDR. Return true if
12132 EXPR is this stabilized form. */
12133
12134 static bool
12135 goa_lhs_expr_p (tree expr, tree addr)
12136 {
12137 /* Also include casts to other type variants. The C front end is fond
12138 of adding these for e.g. volatile variables. This is like
12139 STRIP_TYPE_NOPS but includes the main variant lookup. */
12140 STRIP_USELESS_TYPE_CONVERSION (expr);
12141
12142 if (TREE_CODE (expr) == INDIRECT_REF)
12143 {
12144 expr = TREE_OPERAND (expr, 0);
12145 while (expr != addr
12146 && (CONVERT_EXPR_P (expr)
12147 || TREE_CODE (expr) == NON_LVALUE_EXPR)
12148 && TREE_CODE (expr) == TREE_CODE (addr)
12149 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
12150 {
12151 expr = TREE_OPERAND (expr, 0);
12152 addr = TREE_OPERAND (addr, 0);
12153 }
12154 if (expr == addr)
12155 return true;
12156 return (TREE_CODE (addr) == ADDR_EXPR
12157 && TREE_CODE (expr) == ADDR_EXPR
12158 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
12159 }
12160 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
12161 return true;
12162 return false;
12163 }
12164
12165 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
12166 expression does not involve the lhs, evaluate it into a temporary.
12167 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
12168 or -1 if an error was encountered. */
12169
12170 static int
12171 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
12172 tree lhs_var)
12173 {
12174 tree expr = *expr_p;
12175 int saw_lhs;
12176
12177 if (goa_lhs_expr_p (expr, lhs_addr))
12178 {
12179 *expr_p = lhs_var;
12180 return 1;
12181 }
12182 if (is_gimple_val (expr))
12183 return 0;
12184
12185 saw_lhs = 0;
12186 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
12187 {
12188 case tcc_binary:
12189 case tcc_comparison:
12190 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
12191 lhs_var);
12192 /* FALLTHRU */
12193 case tcc_unary:
12194 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
12195 lhs_var);
12196 break;
12197 case tcc_expression:
12198 switch (TREE_CODE (expr))
12199 {
12200 case TRUTH_ANDIF_EXPR:
12201 case TRUTH_ORIF_EXPR:
12202 case TRUTH_AND_EXPR:
12203 case TRUTH_OR_EXPR:
12204 case TRUTH_XOR_EXPR:
12205 case BIT_INSERT_EXPR:
12206 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
12207 lhs_addr, lhs_var);
12208 /* FALLTHRU */
12209 case TRUTH_NOT_EXPR:
12210 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
12211 lhs_addr, lhs_var);
12212 break;
12213 case COMPOUND_EXPR:
12214 /* Break out any preevaluations from cp_build_modify_expr. */
12215 for (; TREE_CODE (expr) == COMPOUND_EXPR;
12216 expr = TREE_OPERAND (expr, 1))
12217 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
12218 *expr_p = expr;
12219 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
12220 default:
12221 break;
12222 }
12223 break;
12224 case tcc_reference:
12225 if (TREE_CODE (expr) == BIT_FIELD_REF)
12226 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
12227 lhs_addr, lhs_var);
12228 break;
12229 default:
12230 break;
12231 }
12232
12233 if (saw_lhs == 0)
12234 {
12235 enum gimplify_status gs;
12236 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
12237 if (gs != GS_ALL_DONE)
12238 saw_lhs = -1;
12239 }
12240
12241 return saw_lhs;
12242 }
12243
12244 /* Gimplify an OMP_ATOMIC statement. */
12245
12246 static enum gimplify_status
12247 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
12248 {
12249 tree addr = TREE_OPERAND (*expr_p, 0);
12250 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
12251 ? NULL : TREE_OPERAND (*expr_p, 1);
12252 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
12253 tree tmp_load;
12254 gomp_atomic_load *loadstmt;
12255 gomp_atomic_store *storestmt;
12256
12257 tmp_load = create_tmp_reg (type);
12258 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
12259 return GS_ERROR;
12260
12261 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
12262 != GS_ALL_DONE)
12263 return GS_ERROR;
12264
12265 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
12266 OMP_ATOMIC_MEMORY_ORDER (*expr_p));
12267 gimplify_seq_add_stmt (pre_p, loadstmt);
12268 if (rhs)
12269 {
12270 /* BIT_INSERT_EXPR is not valid for non-integral bitfield
12271 representatives. Use BIT_FIELD_REF on the lhs instead. */
12272 if (TREE_CODE (rhs) == BIT_INSERT_EXPR
12273 && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
12274 {
12275 tree bitpos = TREE_OPERAND (rhs, 2);
12276 tree op1 = TREE_OPERAND (rhs, 1);
12277 tree bitsize;
12278 tree tmp_store = tmp_load;
12279 if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
12280 tmp_store = get_initialized_tmp_var (tmp_load, pre_p, NULL);
12281 if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
12282 bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
12283 else
12284 bitsize = TYPE_SIZE (TREE_TYPE (op1));
12285 gcc_assert (TREE_OPERAND (rhs, 0) == tmp_load);
12286 tree t = build2_loc (EXPR_LOCATION (rhs),
12287 MODIFY_EXPR, void_type_node,
12288 build3_loc (EXPR_LOCATION (rhs), BIT_FIELD_REF,
12289 TREE_TYPE (op1), tmp_store, bitsize,
12290 bitpos), op1);
12291 gimplify_and_add (t, pre_p);
12292 rhs = tmp_store;
12293 }
12294 if (gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
12295 != GS_ALL_DONE)
12296 return GS_ERROR;
12297 }
12298
12299 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
12300 rhs = tmp_load;
12301 storestmt
12302 = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
12303 gimplify_seq_add_stmt (pre_p, storestmt);
12304 switch (TREE_CODE (*expr_p))
12305 {
12306 case OMP_ATOMIC_READ:
12307 case OMP_ATOMIC_CAPTURE_OLD:
12308 *expr_p = tmp_load;
12309 gimple_omp_atomic_set_need_value (loadstmt);
12310 break;
12311 case OMP_ATOMIC_CAPTURE_NEW:
12312 *expr_p = rhs;
12313 gimple_omp_atomic_set_need_value (storestmt);
12314 break;
12315 default:
12316 *expr_p = NULL;
12317 break;
12318 }
12319
12320 return GS_ALL_DONE;
12321 }
12322
12323 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
12324 body, and adding some EH bits. */
12325
12326 static enum gimplify_status
12327 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
12328 {
12329 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
12330 gimple *body_stmt;
12331 gtransaction *trans_stmt;
12332 gimple_seq body = NULL;
12333 int subcode = 0;
12334
12335 /* Wrap the transaction body in a BIND_EXPR so we have a context
12336 where to put decls for OMP. */
12337 if (TREE_CODE (tbody) != BIND_EXPR)
12338 {
12339 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
12340 TREE_SIDE_EFFECTS (bind) = 1;
12341 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
12342 TRANSACTION_EXPR_BODY (expr) = bind;
12343 }
12344
12345 push_gimplify_context ();
12346 temp = voidify_wrapper_expr (*expr_p, NULL);
12347
12348 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
12349 pop_gimplify_context (body_stmt);
12350
12351 trans_stmt = gimple_build_transaction (body);
12352 if (TRANSACTION_EXPR_OUTER (expr))
12353 subcode = GTMA_IS_OUTER;
12354 else if (TRANSACTION_EXPR_RELAXED (expr))
12355 subcode = GTMA_IS_RELAXED;
12356 gimple_transaction_set_subcode (trans_stmt, subcode);
12357
12358 gimplify_seq_add_stmt (pre_p, trans_stmt);
12359
12360 if (temp)
12361 {
12362 *expr_p = temp;
12363 return GS_OK;
12364 }
12365
12366 *expr_p = NULL_TREE;
12367 return GS_ALL_DONE;
12368 }
12369
12370 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
12371 is the OMP_BODY of the original EXPR (which has already been
12372 gimplified so it's not present in the EXPR).
12373
12374 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
12375
12376 static gimple *
12377 gimplify_omp_ordered (tree expr, gimple_seq body)
12378 {
12379 tree c, decls;
12380 int failures = 0;
12381 unsigned int i;
12382 tree source_c = NULL_TREE;
12383 tree sink_c = NULL_TREE;
12384
12385 if (gimplify_omp_ctxp)
12386 {
12387 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
12388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12389 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
12390 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
12391 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
12392 {
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%<ordered%> construct with %<depend%> clause must be "
12395 "closely nested inside a loop with %<ordered%> clause "
12396 "with a parameter");
12397 failures++;
12398 }
12399 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12400 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12401 {
12402 bool fail = false;
12403 for (decls = OMP_CLAUSE_DECL (c), i = 0;
12404 decls && TREE_CODE (decls) == TREE_LIST;
12405 decls = TREE_CHAIN (decls), ++i)
12406 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
12407 continue;
12408 else if (TREE_VALUE (decls)
12409 != gimplify_omp_ctxp->loop_iter_var[2 * i])
12410 {
12411 error_at (OMP_CLAUSE_LOCATION (c),
12412 "variable %qE is not an iteration "
12413 "of outermost loop %d, expected %qE",
12414 TREE_VALUE (decls), i + 1,
12415 gimplify_omp_ctxp->loop_iter_var[2 * i]);
12416 fail = true;
12417 failures++;
12418 }
12419 else
12420 TREE_VALUE (decls)
12421 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
12422 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
12423 {
12424 error_at (OMP_CLAUSE_LOCATION (c),
12425 "number of variables in %<depend%> clause with "
12426 "%<sink%> modifier does not match number of "
12427 "iteration variables");
12428 failures++;
12429 }
12430 sink_c = c;
12431 }
12432 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12433 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
12434 {
12435 if (source_c)
12436 {
12437 error_at (OMP_CLAUSE_LOCATION (c),
12438 "more than one %<depend%> clause with %<source%> "
12439 "modifier on an %<ordered%> construct");
12440 failures++;
12441 }
12442 else
12443 source_c = c;
12444 }
12445 }
12446 if (source_c && sink_c)
12447 {
12448 error_at (OMP_CLAUSE_LOCATION (source_c),
12449 "%<depend%> clause with %<source%> modifier specified "
12450 "together with %<depend%> clauses with %<sink%> modifier "
12451 "on the same construct");
12452 failures++;
12453 }
12454
12455 if (failures)
12456 return gimple_build_nop ();
12457 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
12458 }
12459
12460 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
12461 expression produces a value to be used as an operand inside a GIMPLE
12462 statement, the value will be stored back in *EXPR_P. This value will
12463 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
12464 an SSA_NAME. The corresponding sequence of GIMPLE statements is
12465 emitted in PRE_P and POST_P.
12466
12467 Additionally, this process may overwrite parts of the input
12468 expression during gimplification. Ideally, it should be
12469 possible to do non-destructive gimplification.
12470
12471 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
12472 the expression needs to evaluate to a value to be used as
12473 an operand in a GIMPLE statement, this value will be stored in
12474 *EXPR_P on exit. This happens when the caller specifies one
12475 of fb_lvalue or fb_rvalue fallback flags.
12476
12477 PRE_P will contain the sequence of GIMPLE statements corresponding
12478 to the evaluation of EXPR and all the side-effects that must
12479 be executed before the main expression. On exit, the last
12480 statement of PRE_P is the core statement being gimplified. For
12481 instance, when gimplifying 'if (++a)' the last statement in
12482 PRE_P will be 'if (t.1)' where t.1 is the result of
12483 pre-incrementing 'a'.
12484
12485 POST_P will contain the sequence of GIMPLE statements corresponding
12486 to the evaluation of all the side-effects that must be executed
12487 after the main expression. If this is NULL, the post
12488 side-effects are stored at the end of PRE_P.
12489
12490 The reason why the output is split in two is to handle post
12491 side-effects explicitly. In some cases, an expression may have
12492 inner and outer post side-effects which need to be emitted in
12493 an order different from the one given by the recursive
12494 traversal. For instance, for the expression (*p--)++ the post
12495 side-effects of '--' must actually occur *after* the post
12496 side-effects of '++'. However, gimplification will first visit
12497 the inner expression, so if a separate POST sequence was not
12498 used, the resulting sequence would be:
12499
12500 1 t.1 = *p
12501 2 p = p - 1
12502 3 t.2 = t.1 + 1
12503 4 *p = t.2
12504
12505 However, the post-decrement operation in line #2 must not be
12506 evaluated until after the store to *p at line #4, so the
12507 correct sequence should be:
12508
12509 1 t.1 = *p
12510 2 t.2 = t.1 + 1
12511 3 *p = t.2
12512 4 p = p - 1
12513
12514 So, by specifying a separate post queue, it is possible
12515 to emit the post side-effects in the correct order.
12516 If POST_P is NULL, an internal queue will be used. Before
12517 returning to the caller, the sequence POST_P is appended to
12518 the main output sequence PRE_P.
12519
12520 GIMPLE_TEST_F points to a function that takes a tree T and
12521 returns nonzero if T is in the GIMPLE form requested by the
12522 caller. The GIMPLE predicates are in gimple.c.
12523
12524 FALLBACK tells the function what sort of a temporary we want if
12525 gimplification cannot produce an expression that complies with
12526 GIMPLE_TEST_F.
12527
12528 fb_none means that no temporary should be generated
12529 fb_rvalue means that an rvalue is OK to generate
12530 fb_lvalue means that an lvalue is OK to generate
12531 fb_either means that either is OK, but an lvalue is preferable.
12532 fb_mayfail means that gimplification may fail (in which case
12533 GS_ERROR will be returned)
12534
12535 The return value is either GS_ERROR or GS_ALL_DONE, since this
12536 function iterates until EXPR is completely gimplified or an error
12537 occurs. */
12538
12539 enum gimplify_status
12540 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12541 bool (*gimple_test_f) (tree), fallback_t fallback)
12542 {
12543 tree tmp;
12544 gimple_seq internal_pre = NULL;
12545 gimple_seq internal_post = NULL;
12546 tree save_expr;
12547 bool is_statement;
12548 location_t saved_location;
12549 enum gimplify_status ret;
12550 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
12551 tree label;
12552
12553 save_expr = *expr_p;
12554 if (save_expr == NULL_TREE)
12555 return GS_ALL_DONE;
12556
12557 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
12558 is_statement = gimple_test_f == is_gimple_stmt;
12559 if (is_statement)
12560 gcc_assert (pre_p);
12561
12562 /* Consistency checks. */
12563 if (gimple_test_f == is_gimple_reg)
12564 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
12565 else if (gimple_test_f == is_gimple_val
12566 || gimple_test_f == is_gimple_call_addr
12567 || gimple_test_f == is_gimple_condexpr
12568 || gimple_test_f == is_gimple_mem_rhs
12569 || gimple_test_f == is_gimple_mem_rhs_or_call
12570 || gimple_test_f == is_gimple_reg_rhs
12571 || gimple_test_f == is_gimple_reg_rhs_or_call
12572 || gimple_test_f == is_gimple_asm_val
12573 || gimple_test_f == is_gimple_mem_ref_addr)
12574 gcc_assert (fallback & fb_rvalue);
12575 else if (gimple_test_f == is_gimple_min_lval
12576 || gimple_test_f == is_gimple_lvalue)
12577 gcc_assert (fallback & fb_lvalue);
12578 else if (gimple_test_f == is_gimple_addressable)
12579 gcc_assert (fallback & fb_either);
12580 else if (gimple_test_f == is_gimple_stmt)
12581 gcc_assert (fallback == fb_none);
12582 else
12583 {
12584 /* We should have recognized the GIMPLE_TEST_F predicate to
12585 know what kind of fallback to use in case a temporary is
12586 needed to hold the value or address of *EXPR_P. */
12587 gcc_unreachable ();
12588 }
12589
12590 /* We used to check the predicate here and return immediately if it
12591 succeeds. This is wrong; the design is for gimplification to be
12592 idempotent, and for the predicates to only test for valid forms, not
12593 whether they are fully simplified. */
12594 if (pre_p == NULL)
12595 pre_p = &internal_pre;
12596
12597 if (post_p == NULL)
12598 post_p = &internal_post;
12599
12600 /* Remember the last statements added to PRE_P and POST_P. Every
12601 new statement added by the gimplification helpers needs to be
12602 annotated with location information. To centralize the
12603 responsibility, we remember the last statement that had been
12604 added to both queues before gimplifying *EXPR_P. If
12605 gimplification produces new statements in PRE_P and POST_P, those
12606 statements will be annotated with the same location information
12607 as *EXPR_P. */
12608 pre_last_gsi = gsi_last (*pre_p);
12609 post_last_gsi = gsi_last (*post_p);
12610
12611 saved_location = input_location;
12612 if (save_expr != error_mark_node
12613 && EXPR_HAS_LOCATION (*expr_p))
12614 input_location = EXPR_LOCATION (*expr_p);
12615
12616 /* Loop over the specific gimplifiers until the toplevel node
12617 remains the same. */
12618 do
12619 {
12620 /* Strip away as many useless type conversions as possible
12621 at the toplevel. */
12622 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
12623
12624 /* Remember the expr. */
12625 save_expr = *expr_p;
12626
12627 /* Die, die, die, my darling. */
12628 if (error_operand_p (save_expr))
12629 {
12630 ret = GS_ERROR;
12631 break;
12632 }
12633
12634 /* Do any language-specific gimplification. */
12635 ret = ((enum gimplify_status)
12636 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
12637 if (ret == GS_OK)
12638 {
12639 if (*expr_p == NULL_TREE)
12640 break;
12641 if (*expr_p != save_expr)
12642 continue;
12643 }
12644 else if (ret != GS_UNHANDLED)
12645 break;
12646
12647 /* Make sure that all the cases set 'ret' appropriately. */
12648 ret = GS_UNHANDLED;
12649 switch (TREE_CODE (*expr_p))
12650 {
12651 /* First deal with the special cases. */
12652
12653 case POSTINCREMENT_EXPR:
12654 case POSTDECREMENT_EXPR:
12655 case PREINCREMENT_EXPR:
12656 case PREDECREMENT_EXPR:
12657 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
12658 fallback != fb_none,
12659 TREE_TYPE (*expr_p));
12660 break;
12661
12662 case VIEW_CONVERT_EXPR:
12663 if ((fallback & fb_rvalue)
12664 && is_gimple_reg_type (TREE_TYPE (*expr_p))
12665 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
12666 {
12667 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12668 post_p, is_gimple_val, fb_rvalue);
12669 recalculate_side_effects (*expr_p);
12670 break;
12671 }
12672 /* Fallthru. */
12673
12674 case ARRAY_REF:
12675 case ARRAY_RANGE_REF:
12676 case REALPART_EXPR:
12677 case IMAGPART_EXPR:
12678 case COMPONENT_REF:
12679 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
12680 fallback ? fallback : fb_rvalue);
12681 break;
12682
12683 case COND_EXPR:
12684 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
12685
12686 /* C99 code may assign to an array in a structure value of a
12687 conditional expression, and this has undefined behavior
12688 only on execution, so create a temporary if an lvalue is
12689 required. */
12690 if (fallback == fb_lvalue)
12691 {
12692 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12693 mark_addressable (*expr_p);
12694 ret = GS_OK;
12695 }
12696 break;
12697
12698 case CALL_EXPR:
12699 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
12700
12701 /* C99 code may assign to an array in a structure returned
12702 from a function, and this has undefined behavior only on
12703 execution, so create a temporary if an lvalue is
12704 required. */
12705 if (fallback == fb_lvalue)
12706 {
12707 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12708 mark_addressable (*expr_p);
12709 ret = GS_OK;
12710 }
12711 break;
12712
12713 case TREE_LIST:
12714 gcc_unreachable ();
12715
12716 case COMPOUND_EXPR:
12717 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
12718 break;
12719
12720 case COMPOUND_LITERAL_EXPR:
12721 ret = gimplify_compound_literal_expr (expr_p, pre_p,
12722 gimple_test_f, fallback);
12723 break;
12724
12725 case MODIFY_EXPR:
12726 case INIT_EXPR:
12727 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
12728 fallback != fb_none);
12729 break;
12730
12731 case TRUTH_ANDIF_EXPR:
12732 case TRUTH_ORIF_EXPR:
12733 {
12734 /* Preserve the original type of the expression and the
12735 source location of the outer expression. */
12736 tree org_type = TREE_TYPE (*expr_p);
12737 *expr_p = gimple_boolify (*expr_p);
12738 *expr_p = build3_loc (input_location, COND_EXPR,
12739 org_type, *expr_p,
12740 fold_convert_loc
12741 (input_location,
12742 org_type, boolean_true_node),
12743 fold_convert_loc
12744 (input_location,
12745 org_type, boolean_false_node));
12746 ret = GS_OK;
12747 break;
12748 }
12749
12750 case TRUTH_NOT_EXPR:
12751 {
12752 tree type = TREE_TYPE (*expr_p);
12753 /* The parsers are careful to generate TRUTH_NOT_EXPR
12754 only with operands that are always zero or one.
12755 We do not fold here but handle the only interesting case
12756 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
12757 *expr_p = gimple_boolify (*expr_p);
12758 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
12759 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
12760 TREE_TYPE (*expr_p),
12761 TREE_OPERAND (*expr_p, 0));
12762 else
12763 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
12764 TREE_TYPE (*expr_p),
12765 TREE_OPERAND (*expr_p, 0),
12766 build_int_cst (TREE_TYPE (*expr_p), 1));
12767 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
12768 *expr_p = fold_convert_loc (input_location, type, *expr_p);
12769 ret = GS_OK;
12770 break;
12771 }
12772
12773 case ADDR_EXPR:
12774 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
12775 break;
12776
12777 case ANNOTATE_EXPR:
12778 {
12779 tree cond = TREE_OPERAND (*expr_p, 0);
12780 tree kind = TREE_OPERAND (*expr_p, 1);
12781 tree data = TREE_OPERAND (*expr_p, 2);
12782 tree type = TREE_TYPE (cond);
12783 if (!INTEGRAL_TYPE_P (type))
12784 {
12785 *expr_p = cond;
12786 ret = GS_OK;
12787 break;
12788 }
12789 tree tmp = create_tmp_var (type);
12790 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
12791 gcall *call
12792 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
12793 gimple_call_set_lhs (call, tmp);
12794 gimplify_seq_add_stmt (pre_p, call);
12795 *expr_p = tmp;
12796 ret = GS_ALL_DONE;
12797 break;
12798 }
12799
12800 case VA_ARG_EXPR:
12801 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
12802 break;
12803
12804 CASE_CONVERT:
12805 if (IS_EMPTY_STMT (*expr_p))
12806 {
12807 ret = GS_ALL_DONE;
12808 break;
12809 }
12810
12811 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
12812 || fallback == fb_none)
12813 {
12814 /* Just strip a conversion to void (or in void context) and
12815 try again. */
12816 *expr_p = TREE_OPERAND (*expr_p, 0);
12817 ret = GS_OK;
12818 break;
12819 }
12820
12821 ret = gimplify_conversion (expr_p);
12822 if (ret == GS_ERROR)
12823 break;
12824 if (*expr_p != save_expr)
12825 break;
12826 /* FALLTHRU */
12827
12828 case FIX_TRUNC_EXPR:
12829 /* unary_expr: ... | '(' cast ')' val | ... */
12830 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12831 is_gimple_val, fb_rvalue);
12832 recalculate_side_effects (*expr_p);
12833 break;
12834
12835 case INDIRECT_REF:
12836 {
12837 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
12838 bool notrap = TREE_THIS_NOTRAP (*expr_p);
12839 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
12840
12841 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
12842 if (*expr_p != save_expr)
12843 {
12844 ret = GS_OK;
12845 break;
12846 }
12847
12848 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12849 is_gimple_reg, fb_rvalue);
12850 if (ret == GS_ERROR)
12851 break;
12852
12853 recalculate_side_effects (*expr_p);
12854 *expr_p = fold_build2_loc (input_location, MEM_REF,
12855 TREE_TYPE (*expr_p),
12856 TREE_OPERAND (*expr_p, 0),
12857 build_int_cst (saved_ptr_type, 0));
12858 TREE_THIS_VOLATILE (*expr_p) = volatilep;
12859 TREE_THIS_NOTRAP (*expr_p) = notrap;
12860 ret = GS_OK;
12861 break;
12862 }
12863
12864 /* We arrive here through the various re-gimplifcation paths. */
12865 case MEM_REF:
12866 /* First try re-folding the whole thing. */
12867 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
12868 TREE_OPERAND (*expr_p, 0),
12869 TREE_OPERAND (*expr_p, 1));
12870 if (tmp)
12871 {
12872 REF_REVERSE_STORAGE_ORDER (tmp)
12873 = REF_REVERSE_STORAGE_ORDER (*expr_p);
12874 *expr_p = tmp;
12875 recalculate_side_effects (*expr_p);
12876 ret = GS_OK;
12877 break;
12878 }
12879 /* Avoid re-gimplifying the address operand if it is already
12880 in suitable form. Re-gimplifying would mark the address
12881 operand addressable. Always gimplify when not in SSA form
12882 as we still may have to gimplify decls with value-exprs. */
12883 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
12884 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
12885 {
12886 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12887 is_gimple_mem_ref_addr, fb_rvalue);
12888 if (ret == GS_ERROR)
12889 break;
12890 }
12891 recalculate_side_effects (*expr_p);
12892 ret = GS_ALL_DONE;
12893 break;
12894
12895 /* Constants need not be gimplified. */
12896 case INTEGER_CST:
12897 case REAL_CST:
12898 case FIXED_CST:
12899 case STRING_CST:
12900 case COMPLEX_CST:
12901 case VECTOR_CST:
12902 /* Drop the overflow flag on constants, we do not want
12903 that in the GIMPLE IL. */
12904 if (TREE_OVERFLOW_P (*expr_p))
12905 *expr_p = drop_tree_overflow (*expr_p);
12906 ret = GS_ALL_DONE;
12907 break;
12908
12909 case CONST_DECL:
12910 /* If we require an lvalue, such as for ADDR_EXPR, retain the
12911 CONST_DECL node. Otherwise the decl is replaceable by its
12912 value. */
12913 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
12914 if (fallback & fb_lvalue)
12915 ret = GS_ALL_DONE;
12916 else
12917 {
12918 *expr_p = DECL_INITIAL (*expr_p);
12919 ret = GS_OK;
12920 }
12921 break;
12922
12923 case DECL_EXPR:
12924 ret = gimplify_decl_expr (expr_p, pre_p);
12925 break;
12926
12927 case BIND_EXPR:
12928 ret = gimplify_bind_expr (expr_p, pre_p);
12929 break;
12930
12931 case LOOP_EXPR:
12932 ret = gimplify_loop_expr (expr_p, pre_p);
12933 break;
12934
12935 case SWITCH_EXPR:
12936 ret = gimplify_switch_expr (expr_p, pre_p);
12937 break;
12938
12939 case EXIT_EXPR:
12940 ret = gimplify_exit_expr (expr_p);
12941 break;
12942
12943 case GOTO_EXPR:
12944 /* If the target is not LABEL, then it is a computed jump
12945 and the target needs to be gimplified. */
12946 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
12947 {
12948 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
12949 NULL, is_gimple_val, fb_rvalue);
12950 if (ret == GS_ERROR)
12951 break;
12952 }
12953 gimplify_seq_add_stmt (pre_p,
12954 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
12955 ret = GS_ALL_DONE;
12956 break;
12957
12958 case PREDICT_EXPR:
12959 gimplify_seq_add_stmt (pre_p,
12960 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
12961 PREDICT_EXPR_OUTCOME (*expr_p)));
12962 ret = GS_ALL_DONE;
12963 break;
12964
12965 case LABEL_EXPR:
12966 ret = gimplify_label_expr (expr_p, pre_p);
12967 label = LABEL_EXPR_LABEL (*expr_p);
12968 gcc_assert (decl_function_context (label) == current_function_decl);
12969
12970 /* If the label is used in a goto statement, or address of the label
12971 is taken, we need to unpoison all variables that were seen so far.
12972 Doing so would prevent us from reporting a false positives. */
12973 if (asan_poisoned_variables
12974 && asan_used_labels != NULL
12975 && asan_used_labels->contains (label))
12976 asan_poison_variables (asan_poisoned_variables, false, pre_p);
12977 break;
12978
12979 case CASE_LABEL_EXPR:
12980 ret = gimplify_case_label_expr (expr_p, pre_p);
12981
12982 if (gimplify_ctxp->live_switch_vars)
12983 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
12984 pre_p);
12985 break;
12986
12987 case RETURN_EXPR:
12988 ret = gimplify_return_expr (*expr_p, pre_p);
12989 break;
12990
12991 case CONSTRUCTOR:
12992 /* Don't reduce this in place; let gimplify_init_constructor work its
12993 magic. Buf if we're just elaborating this for side effects, just
12994 gimplify any element that has side-effects. */
12995 if (fallback == fb_none)
12996 {
12997 unsigned HOST_WIDE_INT ix;
12998 tree val;
12999 tree temp = NULL_TREE;
13000 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
13001 if (TREE_SIDE_EFFECTS (val))
13002 append_to_statement_list (val, &temp);
13003
13004 *expr_p = temp;
13005 ret = temp ? GS_OK : GS_ALL_DONE;
13006 }
13007 /* C99 code may assign to an array in a constructed
13008 structure or union, and this has undefined behavior only
13009 on execution, so create a temporary if an lvalue is
13010 required. */
13011 else if (fallback == fb_lvalue)
13012 {
13013 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
13014 mark_addressable (*expr_p);
13015 ret = GS_OK;
13016 }
13017 else
13018 ret = GS_ALL_DONE;
13019 break;
13020
13021 /* The following are special cases that are not handled by the
13022 original GIMPLE grammar. */
13023
13024 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
13025 eliminated. */
13026 case SAVE_EXPR:
13027 ret = gimplify_save_expr (expr_p, pre_p, post_p);
13028 break;
13029
13030 case BIT_FIELD_REF:
13031 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13032 post_p, is_gimple_lvalue, fb_either);
13033 recalculate_side_effects (*expr_p);
13034 break;
13035
13036 case TARGET_MEM_REF:
13037 {
13038 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
13039
13040 if (TMR_BASE (*expr_p))
13041 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
13042 post_p, is_gimple_mem_ref_addr, fb_either);
13043 if (TMR_INDEX (*expr_p))
13044 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
13045 post_p, is_gimple_val, fb_rvalue);
13046 if (TMR_INDEX2 (*expr_p))
13047 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
13048 post_p, is_gimple_val, fb_rvalue);
13049 /* TMR_STEP and TMR_OFFSET are always integer constants. */
13050 ret = MIN (r0, r1);
13051 }
13052 break;
13053
13054 case NON_LVALUE_EXPR:
13055 /* This should have been stripped above. */
13056 gcc_unreachable ();
13057
13058 case ASM_EXPR:
13059 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
13060 break;
13061
13062 case TRY_FINALLY_EXPR:
13063 case TRY_CATCH_EXPR:
13064 {
13065 gimple_seq eval, cleanup;
13066 gtry *try_;
13067
13068 /* Calls to destructors are generated automatically in FINALLY/CATCH
13069 block. They should have location as UNKNOWN_LOCATION. However,
13070 gimplify_call_expr will reset these call stmts to input_location
13071 if it finds stmt's location is unknown. To prevent resetting for
13072 destructors, we set the input_location to unknown.
13073 Note that this only affects the destructor calls in FINALLY/CATCH
13074 block, and will automatically reset to its original value by the
13075 end of gimplify_expr. */
13076 input_location = UNKNOWN_LOCATION;
13077 eval = cleanup = NULL;
13078 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
13079 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
13080 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
13081 if (gimple_seq_empty_p (cleanup))
13082 {
13083 gimple_seq_add_seq (pre_p, eval);
13084 ret = GS_ALL_DONE;
13085 break;
13086 }
13087 try_ = gimple_build_try (eval, cleanup,
13088 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
13089 ? GIMPLE_TRY_FINALLY
13090 : GIMPLE_TRY_CATCH);
13091 if (EXPR_HAS_LOCATION (save_expr))
13092 gimple_set_location (try_, EXPR_LOCATION (save_expr));
13093 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
13094 gimple_set_location (try_, saved_location);
13095 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
13096 gimple_try_set_catch_is_cleanup (try_,
13097 TRY_CATCH_IS_CLEANUP (*expr_p));
13098 gimplify_seq_add_stmt (pre_p, try_);
13099 ret = GS_ALL_DONE;
13100 break;
13101 }
13102
13103 case CLEANUP_POINT_EXPR:
13104 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
13105 break;
13106
13107 case TARGET_EXPR:
13108 ret = gimplify_target_expr (expr_p, pre_p, post_p);
13109 break;
13110
13111 case CATCH_EXPR:
13112 {
13113 gimple *c;
13114 gimple_seq handler = NULL;
13115 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
13116 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
13117 gimplify_seq_add_stmt (pre_p, c);
13118 ret = GS_ALL_DONE;
13119 break;
13120 }
13121
13122 case EH_FILTER_EXPR:
13123 {
13124 gimple *ehf;
13125 gimple_seq failure = NULL;
13126
13127 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
13128 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
13129 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
13130 gimplify_seq_add_stmt (pre_p, ehf);
13131 ret = GS_ALL_DONE;
13132 break;
13133 }
13134
13135 case OBJ_TYPE_REF:
13136 {
13137 enum gimplify_status r0, r1;
13138 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
13139 post_p, is_gimple_val, fb_rvalue);
13140 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
13141 post_p, is_gimple_val, fb_rvalue);
13142 TREE_SIDE_EFFECTS (*expr_p) = 0;
13143 ret = MIN (r0, r1);
13144 }
13145 break;
13146
13147 case LABEL_DECL:
13148 /* We get here when taking the address of a label. We mark
13149 the label as "forced"; meaning it can never be removed and
13150 it is a potential target for any computed goto. */
13151 FORCED_LABEL (*expr_p) = 1;
13152 ret = GS_ALL_DONE;
13153 break;
13154
13155 case STATEMENT_LIST:
13156 ret = gimplify_statement_list (expr_p, pre_p);
13157 break;
13158
13159 case WITH_SIZE_EXPR:
13160 {
13161 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13162 post_p == &internal_post ? NULL : post_p,
13163 gimple_test_f, fallback);
13164 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
13165 is_gimple_val, fb_rvalue);
13166 ret = GS_ALL_DONE;
13167 }
13168 break;
13169
13170 case VAR_DECL:
13171 case PARM_DECL:
13172 ret = gimplify_var_or_parm_decl (expr_p);
13173 break;
13174
13175 case RESULT_DECL:
13176 /* When within an OMP context, notice uses of variables. */
13177 if (gimplify_omp_ctxp)
13178 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
13179 ret = GS_ALL_DONE;
13180 break;
13181
13182 case DEBUG_EXPR_DECL:
13183 gcc_unreachable ();
13184
13185 case DEBUG_BEGIN_STMT:
13186 gimplify_seq_add_stmt (pre_p,
13187 gimple_build_debug_begin_stmt
13188 (TREE_BLOCK (*expr_p),
13189 EXPR_LOCATION (*expr_p)));
13190 ret = GS_ALL_DONE;
13191 *expr_p = NULL;
13192 break;
13193
13194 case SSA_NAME:
13195 /* Allow callbacks into the gimplifier during optimization. */
13196 ret = GS_ALL_DONE;
13197 break;
13198
13199 case OMP_PARALLEL:
13200 gimplify_omp_parallel (expr_p, pre_p);
13201 ret = GS_ALL_DONE;
13202 break;
13203
13204 case OMP_TASK:
13205 gimplify_omp_task (expr_p, pre_p);
13206 ret = GS_ALL_DONE;
13207 break;
13208
13209 case OMP_FOR:
13210 case OMP_SIMD:
13211 case OMP_DISTRIBUTE:
13212 case OMP_TASKLOOP:
13213 case OACC_LOOP:
13214 ret = gimplify_omp_for (expr_p, pre_p);
13215 break;
13216
13217 case OACC_CACHE:
13218 gimplify_oacc_cache (expr_p, pre_p);
13219 ret = GS_ALL_DONE;
13220 break;
13221
13222 case OACC_DECLARE:
13223 gimplify_oacc_declare (expr_p, pre_p);
13224 ret = GS_ALL_DONE;
13225 break;
13226
13227 case OACC_HOST_DATA:
13228 case OACC_DATA:
13229 case OACC_KERNELS:
13230 case OACC_PARALLEL:
13231 case OMP_SECTIONS:
13232 case OMP_SINGLE:
13233 case OMP_TARGET:
13234 case OMP_TARGET_DATA:
13235 case OMP_TEAMS:
13236 gimplify_omp_workshare (expr_p, pre_p);
13237 ret = GS_ALL_DONE;
13238 break;
13239
13240 case OACC_ENTER_DATA:
13241 case OACC_EXIT_DATA:
13242 case OACC_UPDATE:
13243 case OMP_TARGET_UPDATE:
13244 case OMP_TARGET_ENTER_DATA:
13245 case OMP_TARGET_EXIT_DATA:
13246 gimplify_omp_target_update (expr_p, pre_p);
13247 ret = GS_ALL_DONE;
13248 break;
13249
13250 case OMP_SECTION:
13251 case OMP_MASTER:
13252 case OMP_ORDERED:
13253 case OMP_CRITICAL:
13254 case OMP_SCAN:
13255 {
13256 gimple_seq body = NULL;
13257 gimple *g;
13258
13259 gimplify_and_add (OMP_BODY (*expr_p), &body);
13260 switch (TREE_CODE (*expr_p))
13261 {
13262 case OMP_SECTION:
13263 g = gimple_build_omp_section (body);
13264 break;
13265 case OMP_MASTER:
13266 g = gimple_build_omp_master (body);
13267 break;
13268 case OMP_ORDERED:
13269 g = gimplify_omp_ordered (*expr_p, body);
13270 break;
13271 case OMP_CRITICAL:
13272 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
13273 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
13274 gimplify_adjust_omp_clauses (pre_p, body,
13275 &OMP_CRITICAL_CLAUSES (*expr_p),
13276 OMP_CRITICAL);
13277 g = gimple_build_omp_critical (body,
13278 OMP_CRITICAL_NAME (*expr_p),
13279 OMP_CRITICAL_CLAUSES (*expr_p));
13280 break;
13281 case OMP_SCAN:
13282 gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
13283 pre_p, ORT_WORKSHARE, OMP_SCAN);
13284 gimplify_adjust_omp_clauses (pre_p, body,
13285 &OMP_SCAN_CLAUSES (*expr_p),
13286 OMP_SCAN);
13287 g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
13288 break;
13289 default:
13290 gcc_unreachable ();
13291 }
13292 gimplify_seq_add_stmt (pre_p, g);
13293 ret = GS_ALL_DONE;
13294 break;
13295 }
13296
13297 case OMP_TASKGROUP:
13298 {
13299 gimple_seq body = NULL;
13300
13301 tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
13302 gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
13303 OMP_TASKGROUP);
13304 gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
13305 gimplify_and_add (OMP_BODY (*expr_p), &body);
13306 gimple_seq cleanup = NULL;
13307 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
13308 gimple *g = gimple_build_call (fn, 0);
13309 gimple_seq_add_stmt (&cleanup, g);
13310 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
13311 body = NULL;
13312 gimple_seq_add_stmt (&body, g);
13313 g = gimple_build_omp_taskgroup (body, *pclauses);
13314 gimplify_seq_add_stmt (pre_p, g);
13315 ret = GS_ALL_DONE;
13316 break;
13317 }
13318
13319 case OMP_ATOMIC:
13320 case OMP_ATOMIC_READ:
13321 case OMP_ATOMIC_CAPTURE_OLD:
13322 case OMP_ATOMIC_CAPTURE_NEW:
13323 ret = gimplify_omp_atomic (expr_p, pre_p);
13324 break;
13325
13326 case TRANSACTION_EXPR:
13327 ret = gimplify_transaction (expr_p, pre_p);
13328 break;
13329
13330 case TRUTH_AND_EXPR:
13331 case TRUTH_OR_EXPR:
13332 case TRUTH_XOR_EXPR:
13333 {
13334 tree orig_type = TREE_TYPE (*expr_p);
13335 tree new_type, xop0, xop1;
13336 *expr_p = gimple_boolify (*expr_p);
13337 new_type = TREE_TYPE (*expr_p);
13338 if (!useless_type_conversion_p (orig_type, new_type))
13339 {
13340 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
13341 ret = GS_OK;
13342 break;
13343 }
13344
13345 /* Boolified binary truth expressions are semantically equivalent
13346 to bitwise binary expressions. Canonicalize them to the
13347 bitwise variant. */
13348 switch (TREE_CODE (*expr_p))
13349 {
13350 case TRUTH_AND_EXPR:
13351 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
13352 break;
13353 case TRUTH_OR_EXPR:
13354 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
13355 break;
13356 case TRUTH_XOR_EXPR:
13357 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
13358 break;
13359 default:
13360 break;
13361 }
13362 /* Now make sure that operands have compatible type to
13363 expression's new_type. */
13364 xop0 = TREE_OPERAND (*expr_p, 0);
13365 xop1 = TREE_OPERAND (*expr_p, 1);
13366 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
13367 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
13368 new_type,
13369 xop0);
13370 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
13371 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
13372 new_type,
13373 xop1);
13374 /* Continue classified as tcc_binary. */
13375 goto expr_2;
13376 }
13377
13378 case VEC_COND_EXPR:
13379 {
13380 enum gimplify_status r0, r1, r2;
13381
13382 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13383 post_p, is_gimple_condexpr, fb_rvalue);
13384 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13385 post_p, is_gimple_val, fb_rvalue);
13386 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13387 post_p, is_gimple_val, fb_rvalue);
13388
13389 ret = MIN (MIN (r0, r1), r2);
13390 recalculate_side_effects (*expr_p);
13391 }
13392 break;
13393
13394 case VEC_PERM_EXPR:
13395 /* Classified as tcc_expression. */
13396 goto expr_3;
13397
13398 case BIT_INSERT_EXPR:
13399 /* Argument 3 is a constant. */
13400 goto expr_2;
13401
13402 case POINTER_PLUS_EXPR:
13403 {
13404 enum gimplify_status r0, r1;
13405 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13406 post_p, is_gimple_val, fb_rvalue);
13407 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13408 post_p, is_gimple_val, fb_rvalue);
13409 recalculate_side_effects (*expr_p);
13410 ret = MIN (r0, r1);
13411 break;
13412 }
13413
13414 default:
13415 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
13416 {
13417 case tcc_comparison:
13418 /* Handle comparison of objects of non scalar mode aggregates
13419 with a call to memcmp. It would be nice to only have to do
13420 this for variable-sized objects, but then we'd have to allow
13421 the same nest of reference nodes we allow for MODIFY_EXPR and
13422 that's too complex.
13423
13424 Compare scalar mode aggregates as scalar mode values. Using
13425 memcmp for them would be very inefficient at best, and is
13426 plain wrong if bitfields are involved. */
13427 {
13428 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
13429
13430 /* Vector comparisons need no boolification. */
13431 if (TREE_CODE (type) == VECTOR_TYPE)
13432 goto expr_2;
13433 else if (!AGGREGATE_TYPE_P (type))
13434 {
13435 tree org_type = TREE_TYPE (*expr_p);
13436 *expr_p = gimple_boolify (*expr_p);
13437 if (!useless_type_conversion_p (org_type,
13438 TREE_TYPE (*expr_p)))
13439 {
13440 *expr_p = fold_convert_loc (input_location,
13441 org_type, *expr_p);
13442 ret = GS_OK;
13443 }
13444 else
13445 goto expr_2;
13446 }
13447 else if (TYPE_MODE (type) != BLKmode)
13448 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
13449 else
13450 ret = gimplify_variable_sized_compare (expr_p);
13451
13452 break;
13453 }
13454
13455 /* If *EXPR_P does not need to be special-cased, handle it
13456 according to its class. */
13457 case tcc_unary:
13458 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13459 post_p, is_gimple_val, fb_rvalue);
13460 break;
13461
13462 case tcc_binary:
13463 expr_2:
13464 {
13465 enum gimplify_status r0, r1;
13466
13467 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13468 post_p, is_gimple_val, fb_rvalue);
13469 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13470 post_p, is_gimple_val, fb_rvalue);
13471
13472 ret = MIN (r0, r1);
13473 break;
13474 }
13475
13476 expr_3:
13477 {
13478 enum gimplify_status r0, r1, r2;
13479
13480 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13481 post_p, is_gimple_val, fb_rvalue);
13482 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13483 post_p, is_gimple_val, fb_rvalue);
13484 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13485 post_p, is_gimple_val, fb_rvalue);
13486
13487 ret = MIN (MIN (r0, r1), r2);
13488 break;
13489 }
13490
13491 case tcc_declaration:
13492 case tcc_constant:
13493 ret = GS_ALL_DONE;
13494 goto dont_recalculate;
13495
13496 default:
13497 gcc_unreachable ();
13498 }
13499
13500 recalculate_side_effects (*expr_p);
13501
13502 dont_recalculate:
13503 break;
13504 }
13505
13506 gcc_assert (*expr_p || ret != GS_OK);
13507 }
13508 while (ret == GS_OK);
13509
13510 /* If we encountered an error_mark somewhere nested inside, either
13511 stub out the statement or propagate the error back out. */
13512 if (ret == GS_ERROR)
13513 {
13514 if (is_statement)
13515 *expr_p = NULL;
13516 goto out;
13517 }
13518
13519 /* This was only valid as a return value from the langhook, which
13520 we handled. Make sure it doesn't escape from any other context. */
13521 gcc_assert (ret != GS_UNHANDLED);
13522
13523 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
13524 {
13525 /* We aren't looking for a value, and we don't have a valid
13526 statement. If it doesn't have side-effects, throw it away.
13527 We can also get here with code such as "*&&L;", where L is
13528 a LABEL_DECL that is marked as FORCED_LABEL. */
13529 if (TREE_CODE (*expr_p) == LABEL_DECL
13530 || !TREE_SIDE_EFFECTS (*expr_p))
13531 *expr_p = NULL;
13532 else if (!TREE_THIS_VOLATILE (*expr_p))
13533 {
13534 /* This is probably a _REF that contains something nested that
13535 has side effects. Recurse through the operands to find it. */
13536 enum tree_code code = TREE_CODE (*expr_p);
13537
13538 switch (code)
13539 {
13540 case COMPONENT_REF:
13541 case REALPART_EXPR:
13542 case IMAGPART_EXPR:
13543 case VIEW_CONVERT_EXPR:
13544 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13545 gimple_test_f, fallback);
13546 break;
13547
13548 case ARRAY_REF:
13549 case ARRAY_RANGE_REF:
13550 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13551 gimple_test_f, fallback);
13552 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
13553 gimple_test_f, fallback);
13554 break;
13555
13556 default:
13557 /* Anything else with side-effects must be converted to
13558 a valid statement before we get here. */
13559 gcc_unreachable ();
13560 }
13561
13562 *expr_p = NULL;
13563 }
13564 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
13565 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
13566 {
13567 /* Historically, the compiler has treated a bare reference
13568 to a non-BLKmode volatile lvalue as forcing a load. */
13569 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
13570
13571 /* Normally, we do not want to create a temporary for a
13572 TREE_ADDRESSABLE type because such a type should not be
13573 copied by bitwise-assignment. However, we make an
13574 exception here, as all we are doing here is ensuring that
13575 we read the bytes that make up the type. We use
13576 create_tmp_var_raw because create_tmp_var will abort when
13577 given a TREE_ADDRESSABLE type. */
13578 tree tmp = create_tmp_var_raw (type, "vol");
13579 gimple_add_tmp_var (tmp);
13580 gimplify_assign (tmp, *expr_p, pre_p);
13581 *expr_p = NULL;
13582 }
13583 else
13584 /* We can't do anything useful with a volatile reference to
13585 an incomplete type, so just throw it away. Likewise for
13586 a BLKmode type, since any implicit inner load should
13587 already have been turned into an explicit one by the
13588 gimplification process. */
13589 *expr_p = NULL;
13590 }
13591
13592 /* If we are gimplifying at the statement level, we're done. Tack
13593 everything together and return. */
13594 if (fallback == fb_none || is_statement)
13595 {
13596 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
13597 it out for GC to reclaim it. */
13598 *expr_p = NULL_TREE;
13599
13600 if (!gimple_seq_empty_p (internal_pre)
13601 || !gimple_seq_empty_p (internal_post))
13602 {
13603 gimplify_seq_add_seq (&internal_pre, internal_post);
13604 gimplify_seq_add_seq (pre_p, internal_pre);
13605 }
13606
13607 /* The result of gimplifying *EXPR_P is going to be the last few
13608 statements in *PRE_P and *POST_P. Add location information
13609 to all the statements that were added by the gimplification
13610 helpers. */
13611 if (!gimple_seq_empty_p (*pre_p))
13612 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
13613
13614 if (!gimple_seq_empty_p (*post_p))
13615 annotate_all_with_location_after (*post_p, post_last_gsi,
13616 input_location);
13617
13618 goto out;
13619 }
13620
13621 #ifdef ENABLE_GIMPLE_CHECKING
13622 if (*expr_p)
13623 {
13624 enum tree_code code = TREE_CODE (*expr_p);
13625 /* These expressions should already be in gimple IR form. */
13626 gcc_assert (code != MODIFY_EXPR
13627 && code != ASM_EXPR
13628 && code != BIND_EXPR
13629 && code != CATCH_EXPR
13630 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
13631 && code != EH_FILTER_EXPR
13632 && code != GOTO_EXPR
13633 && code != LABEL_EXPR
13634 && code != LOOP_EXPR
13635 && code != SWITCH_EXPR
13636 && code != TRY_FINALLY_EXPR
13637 && code != OACC_PARALLEL
13638 && code != OACC_KERNELS
13639 && code != OACC_DATA
13640 && code != OACC_HOST_DATA
13641 && code != OACC_DECLARE
13642 && code != OACC_UPDATE
13643 && code != OACC_ENTER_DATA
13644 && code != OACC_EXIT_DATA
13645 && code != OACC_CACHE
13646 && code != OMP_CRITICAL
13647 && code != OMP_FOR
13648 && code != OACC_LOOP
13649 && code != OMP_MASTER
13650 && code != OMP_TASKGROUP
13651 && code != OMP_ORDERED
13652 && code != OMP_PARALLEL
13653 && code != OMP_SCAN
13654 && code != OMP_SECTIONS
13655 && code != OMP_SECTION
13656 && code != OMP_SINGLE);
13657 }
13658 #endif
13659
13660 /* Otherwise we're gimplifying a subexpression, so the resulting
13661 value is interesting. If it's a valid operand that matches
13662 GIMPLE_TEST_F, we're done. Unless we are handling some
13663 post-effects internally; if that's the case, we need to copy into
13664 a temporary before adding the post-effects to POST_P. */
13665 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
13666 goto out;
13667
13668 /* Otherwise, we need to create a new temporary for the gimplified
13669 expression. */
13670
13671 /* We can't return an lvalue if we have an internal postqueue. The
13672 object the lvalue refers to would (probably) be modified by the
13673 postqueue; we need to copy the value out first, which means an
13674 rvalue. */
13675 if ((fallback & fb_lvalue)
13676 && gimple_seq_empty_p (internal_post)
13677 && is_gimple_addressable (*expr_p))
13678 {
13679 /* An lvalue will do. Take the address of the expression, store it
13680 in a temporary, and replace the expression with an INDIRECT_REF of
13681 that temporary. */
13682 tree ref_alias_type = reference_alias_ptr_type (*expr_p);
13683 unsigned int ref_align = get_object_alignment (*expr_p);
13684 tree ref_type = TREE_TYPE (*expr_p);
13685 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
13686 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
13687 if (TYPE_ALIGN (ref_type) != ref_align)
13688 ref_type = build_aligned_type (ref_type, ref_align);
13689 *expr_p = build2 (MEM_REF, ref_type,
13690 tmp, build_zero_cst (ref_alias_type));
13691 }
13692 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
13693 {
13694 /* An rvalue will do. Assign the gimplified expression into a
13695 new temporary TMP and replace the original expression with
13696 TMP. First, make sure that the expression has a type so that
13697 it can be assigned into a temporary. */
13698 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
13699 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
13700 }
13701 else
13702 {
13703 #ifdef ENABLE_GIMPLE_CHECKING
13704 if (!(fallback & fb_mayfail))
13705 {
13706 fprintf (stderr, "gimplification failed:\n");
13707 print_generic_expr (stderr, *expr_p);
13708 debug_tree (*expr_p);
13709 internal_error ("gimplification failed");
13710 }
13711 #endif
13712 gcc_assert (fallback & fb_mayfail);
13713
13714 /* If this is an asm statement, and the user asked for the
13715 impossible, don't die. Fail and let gimplify_asm_expr
13716 issue an error. */
13717 ret = GS_ERROR;
13718 goto out;
13719 }
13720
13721 /* Make sure the temporary matches our predicate. */
13722 gcc_assert ((*gimple_test_f) (*expr_p));
13723
13724 if (!gimple_seq_empty_p (internal_post))
13725 {
13726 annotate_all_with_location (internal_post, input_location);
13727 gimplify_seq_add_seq (pre_p, internal_post);
13728 }
13729
13730 out:
13731 input_location = saved_location;
13732 return ret;
13733 }
13734
13735 /* Like gimplify_expr but make sure the gimplified result is not itself
13736 a SSA name (but a decl if it were). Temporaries required by
13737 evaluating *EXPR_P may be still SSA names. */
13738
13739 static enum gimplify_status
13740 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
13741 bool (*gimple_test_f) (tree), fallback_t fallback,
13742 bool allow_ssa)
13743 {
13744 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
13745 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
13746 gimple_test_f, fallback);
13747 if (! allow_ssa
13748 && TREE_CODE (*expr_p) == SSA_NAME)
13749 {
13750 tree name = *expr_p;
13751 if (was_ssa_name_p)
13752 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
13753 else
13754 {
13755 /* Avoid the extra copy if possible. */
13756 *expr_p = create_tmp_reg (TREE_TYPE (name));
13757 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
13758 release_ssa_name (name);
13759 }
13760 }
13761 return ret;
13762 }
13763
13764 /* Look through TYPE for variable-sized objects and gimplify each such
13765 size that we find. Add to LIST_P any statements generated. */
13766
13767 void
13768 gimplify_type_sizes (tree type, gimple_seq *list_p)
13769 {
13770 tree field, t;
13771
13772 if (type == NULL || type == error_mark_node)
13773 return;
13774
13775 /* We first do the main variant, then copy into any other variants. */
13776 type = TYPE_MAIN_VARIANT (type);
13777
13778 /* Avoid infinite recursion. */
13779 if (TYPE_SIZES_GIMPLIFIED (type))
13780 return;
13781
13782 TYPE_SIZES_GIMPLIFIED (type) = 1;
13783
13784 switch (TREE_CODE (type))
13785 {
13786 case INTEGER_TYPE:
13787 case ENUMERAL_TYPE:
13788 case BOOLEAN_TYPE:
13789 case REAL_TYPE:
13790 case FIXED_POINT_TYPE:
13791 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
13792 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
13793
13794 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13795 {
13796 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
13797 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
13798 }
13799 break;
13800
13801 case ARRAY_TYPE:
13802 /* These types may not have declarations, so handle them here. */
13803 gimplify_type_sizes (TREE_TYPE (type), list_p);
13804 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
13805 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
13806 with assigned stack slots, for -O1+ -g they should be tracked
13807 by VTA. */
13808 if (!(TYPE_NAME (type)
13809 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13810 && DECL_IGNORED_P (TYPE_NAME (type)))
13811 && TYPE_DOMAIN (type)
13812 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
13813 {
13814 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
13815 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13816 DECL_IGNORED_P (t) = 0;
13817 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13818 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13819 DECL_IGNORED_P (t) = 0;
13820 }
13821 break;
13822
13823 case RECORD_TYPE:
13824 case UNION_TYPE:
13825 case QUAL_UNION_TYPE:
13826 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
13827 if (TREE_CODE (field) == FIELD_DECL)
13828 {
13829 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
13830 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
13831 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
13832 gimplify_type_sizes (TREE_TYPE (field), list_p);
13833 }
13834 break;
13835
13836 case POINTER_TYPE:
13837 case REFERENCE_TYPE:
13838 /* We used to recurse on the pointed-to type here, which turned out to
13839 be incorrect because its definition might refer to variables not
13840 yet initialized at this point if a forward declaration is involved.
13841
13842 It was actually useful for anonymous pointed-to types to ensure
13843 that the sizes evaluation dominates every possible later use of the
13844 values. Restricting to such types here would be safe since there
13845 is no possible forward declaration around, but would introduce an
13846 undesirable middle-end semantic to anonymity. We then defer to
13847 front-ends the responsibility of ensuring that the sizes are
13848 evaluated both early and late enough, e.g. by attaching artificial
13849 type declarations to the tree. */
13850 break;
13851
13852 default:
13853 break;
13854 }
13855
13856 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
13857 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
13858
13859 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13860 {
13861 TYPE_SIZE (t) = TYPE_SIZE (type);
13862 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
13863 TYPE_SIZES_GIMPLIFIED (t) = 1;
13864 }
13865 }
13866
13867 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
13868 a size or position, has had all of its SAVE_EXPRs evaluated.
13869 We add any required statements to *STMT_P. */
13870
13871 void
13872 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
13873 {
13874 tree expr = *expr_p;
13875
13876 /* We don't do anything if the value isn't there, is constant, or contains
13877 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
13878 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
13879 will want to replace it with a new variable, but that will cause problems
13880 if this type is from outside the function. It's OK to have that here. */
13881 if (expr == NULL_TREE
13882 || is_gimple_constant (expr)
13883 || TREE_CODE (expr) == VAR_DECL
13884 || CONTAINS_PLACEHOLDER_P (expr))
13885 return;
13886
13887 *expr_p = unshare_expr (expr);
13888
13889 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
13890 if the def vanishes. */
13891 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
13892
13893 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
13894 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
13895 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
13896 if (is_gimple_constant (*expr_p))
13897 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
13898 }
13899
13900 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
13901 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
13902 is true, also gimplify the parameters. */
13903
13904 gbind *
13905 gimplify_body (tree fndecl, bool do_parms)
13906 {
13907 location_t saved_location = input_location;
13908 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
13909 gimple *outer_stmt;
13910 gbind *outer_bind;
13911
13912 timevar_push (TV_TREE_GIMPLIFY);
13913
13914 init_tree_ssa (cfun);
13915
13916 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
13917 gimplification. */
13918 default_rtl_profile ();
13919
13920 gcc_assert (gimplify_ctxp == NULL);
13921 push_gimplify_context (true);
13922
13923 if (flag_openacc || flag_openmp)
13924 {
13925 gcc_assert (gimplify_omp_ctxp == NULL);
13926 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
13927 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
13928 }
13929
13930 /* Unshare most shared trees in the body and in that of any nested functions.
13931 It would seem we don't have to do this for nested functions because
13932 they are supposed to be output and then the outer function gimplified
13933 first, but the g++ front end doesn't always do it that way. */
13934 unshare_body (fndecl);
13935 unvisit_body (fndecl);
13936
13937 /* Make sure input_location isn't set to something weird. */
13938 input_location = DECL_SOURCE_LOCATION (fndecl);
13939
13940 /* Resolve callee-copies. This has to be done before processing
13941 the body so that DECL_VALUE_EXPR gets processed correctly. */
13942 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
13943
13944 /* Gimplify the function's body. */
13945 seq = NULL;
13946 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
13947 outer_stmt = gimple_seq_first_stmt (seq);
13948 if (!outer_stmt)
13949 {
13950 outer_stmt = gimple_build_nop ();
13951 gimplify_seq_add_stmt (&seq, outer_stmt);
13952 }
13953
13954 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
13955 not the case, wrap everything in a GIMPLE_BIND to make it so. */
13956 if (gimple_code (outer_stmt) == GIMPLE_BIND
13957 && gimple_seq_first (seq) == gimple_seq_last (seq))
13958 outer_bind = as_a <gbind *> (outer_stmt);
13959 else
13960 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
13961
13962 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13963
13964 /* If we had callee-copies statements, insert them at the beginning
13965 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
13966 if (!gimple_seq_empty_p (parm_stmts))
13967 {
13968 tree parm;
13969
13970 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
13971 if (parm_cleanup)
13972 {
13973 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
13974 GIMPLE_TRY_FINALLY);
13975 parm_stmts = NULL;
13976 gimple_seq_add_stmt (&parm_stmts, g);
13977 }
13978 gimple_bind_set_body (outer_bind, parm_stmts);
13979
13980 for (parm = DECL_ARGUMENTS (current_function_decl);
13981 parm; parm = DECL_CHAIN (parm))
13982 if (DECL_HAS_VALUE_EXPR_P (parm))
13983 {
13984 DECL_HAS_VALUE_EXPR_P (parm) = 0;
13985 DECL_IGNORED_P (parm) = 0;
13986 }
13987 }
13988
13989 if ((flag_openacc || flag_openmp || flag_openmp_simd)
13990 && gimplify_omp_ctxp)
13991 {
13992 delete_omp_context (gimplify_omp_ctxp);
13993 gimplify_omp_ctxp = NULL;
13994 }
13995
13996 pop_gimplify_context (outer_bind);
13997 gcc_assert (gimplify_ctxp == NULL);
13998
13999 if (flag_checking && !seen_error ())
14000 verify_gimple_in_seq (gimple_bind_body (outer_bind));
14001
14002 timevar_pop (TV_TREE_GIMPLIFY);
14003 input_location = saved_location;
14004
14005 return outer_bind;
14006 }
14007
14008 typedef char *char_p; /* For DEF_VEC_P. */
14009
14010 /* Return whether we should exclude FNDECL from instrumentation. */
14011
14012 static bool
14013 flag_instrument_functions_exclude_p (tree fndecl)
14014 {
14015 vec<char_p> *v;
14016
14017 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
14018 if (v && v->length () > 0)
14019 {
14020 const char *name;
14021 int i;
14022 char *s;
14023
14024 name = lang_hooks.decl_printable_name (fndecl, 0);
14025 FOR_EACH_VEC_ELT (*v, i, s)
14026 if (strstr (name, s) != NULL)
14027 return true;
14028 }
14029
14030 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
14031 if (v && v->length () > 0)
14032 {
14033 const char *name;
14034 int i;
14035 char *s;
14036
14037 name = DECL_SOURCE_FILE (fndecl);
14038 FOR_EACH_VEC_ELT (*v, i, s)
14039 if (strstr (name, s) != NULL)
14040 return true;
14041 }
14042
14043 return false;
14044 }
14045
14046 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
14047 node for the function we want to gimplify.
14048
14049 Return the sequence of GIMPLE statements corresponding to the body
14050 of FNDECL. */
14051
14052 void
14053 gimplify_function_tree (tree fndecl)
14054 {
14055 tree parm, ret;
14056 gimple_seq seq;
14057 gbind *bind;
14058
14059 gcc_assert (!gimple_body (fndecl));
14060
14061 if (DECL_STRUCT_FUNCTION (fndecl))
14062 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
14063 else
14064 push_struct_function (fndecl);
14065
14066 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
14067 if necessary. */
14068 cfun->curr_properties |= PROP_gimple_lva;
14069
14070 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
14071 {
14072 /* Preliminarily mark non-addressed complex variables as eligible
14073 for promotion to gimple registers. We'll transform their uses
14074 as we find them. */
14075 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
14076 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
14077 && !TREE_THIS_VOLATILE (parm)
14078 && !needs_to_live_in_memory (parm))
14079 DECL_GIMPLE_REG_P (parm) = 1;
14080 }
14081
14082 ret = DECL_RESULT (fndecl);
14083 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
14084 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
14085 && !needs_to_live_in_memory (ret))
14086 DECL_GIMPLE_REG_P (ret) = 1;
14087
14088 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
14089 asan_poisoned_variables = new hash_set<tree> ();
14090 bind = gimplify_body (fndecl, true);
14091 if (asan_poisoned_variables)
14092 {
14093 delete asan_poisoned_variables;
14094 asan_poisoned_variables = NULL;
14095 }
14096
14097 /* The tree body of the function is no longer needed, replace it
14098 with the new GIMPLE body. */
14099 seq = NULL;
14100 gimple_seq_add_stmt (&seq, bind);
14101 gimple_set_body (fndecl, seq);
14102
14103 /* If we're instrumenting function entry/exit, then prepend the call to
14104 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
14105 catch the exit hook. */
14106 /* ??? Add some way to ignore exceptions for this TFE. */
14107 if (flag_instrument_function_entry_exit
14108 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
14109 /* Do not instrument extern inline functions. */
14110 && !(DECL_DECLARED_INLINE_P (fndecl)
14111 && DECL_EXTERNAL (fndecl)
14112 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
14113 && !flag_instrument_functions_exclude_p (fndecl))
14114 {
14115 tree x;
14116 gbind *new_bind;
14117 gimple *tf;
14118 gimple_seq cleanup = NULL, body = NULL;
14119 tree tmp_var, this_fn_addr;
14120 gcall *call;
14121
14122 /* The instrumentation hooks aren't going to call the instrumented
14123 function and the address they receive is expected to be matchable
14124 against symbol addresses. Make sure we don't create a trampoline,
14125 in case the current function is nested. */
14126 this_fn_addr = build_fold_addr_expr (current_function_decl);
14127 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
14128
14129 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
14130 call = gimple_build_call (x, 1, integer_zero_node);
14131 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
14132 gimple_call_set_lhs (call, tmp_var);
14133 gimplify_seq_add_stmt (&cleanup, call);
14134 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
14135 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
14136 gimplify_seq_add_stmt (&cleanup, call);
14137 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
14138
14139 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
14140 call = gimple_build_call (x, 1, integer_zero_node);
14141 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
14142 gimple_call_set_lhs (call, tmp_var);
14143 gimplify_seq_add_stmt (&body, call);
14144 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
14145 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
14146 gimplify_seq_add_stmt (&body, call);
14147 gimplify_seq_add_stmt (&body, tf);
14148 new_bind = gimple_build_bind (NULL, body, NULL);
14149
14150 /* Replace the current function body with the body
14151 wrapped in the try/finally TF. */
14152 seq = NULL;
14153 gimple_seq_add_stmt (&seq, new_bind);
14154 gimple_set_body (fndecl, seq);
14155 bind = new_bind;
14156 }
14157
14158 if (sanitize_flags_p (SANITIZE_THREAD))
14159 {
14160 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
14161 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
14162 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
14163 /* Replace the current function body with the body
14164 wrapped in the try/finally TF. */
14165 seq = NULL;
14166 gimple_seq_add_stmt (&seq, new_bind);
14167 gimple_set_body (fndecl, seq);
14168 }
14169
14170 DECL_SAVED_TREE (fndecl) = NULL_TREE;
14171 cfun->curr_properties |= PROP_gimple_any;
14172
14173 pop_cfun ();
14174
14175 dump_function (TDI_gimple, fndecl);
14176 }
14177
14178 /* Return a dummy expression of type TYPE in order to keep going after an
14179 error. */
14180
14181 static tree
14182 dummy_object (tree type)
14183 {
14184 tree t = build_int_cst (build_pointer_type (type), 0);
14185 return build2 (MEM_REF, type, t, t);
14186 }
14187
14188 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
14189 builtin function, but a very special sort of operator. */
14190
14191 enum gimplify_status
14192 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
14193 gimple_seq *post_p ATTRIBUTE_UNUSED)
14194 {
14195 tree promoted_type, have_va_type;
14196 tree valist = TREE_OPERAND (*expr_p, 0);
14197 tree type = TREE_TYPE (*expr_p);
14198 tree t, tag, aptag;
14199 location_t loc = EXPR_LOCATION (*expr_p);
14200
14201 /* Verify that valist is of the proper type. */
14202 have_va_type = TREE_TYPE (valist);
14203 if (have_va_type == error_mark_node)
14204 return GS_ERROR;
14205 have_va_type = targetm.canonical_va_list_type (have_va_type);
14206 if (have_va_type == NULL_TREE
14207 && POINTER_TYPE_P (TREE_TYPE (valist)))
14208 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
14209 have_va_type
14210 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
14211 gcc_assert (have_va_type != NULL_TREE);
14212
14213 /* Generate a diagnostic for requesting data of a type that cannot
14214 be passed through `...' due to type promotion at the call site. */
14215 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
14216 != type)
14217 {
14218 static bool gave_help;
14219 bool warned;
14220 /* Use the expansion point to handle cases such as passing bool (defined
14221 in a system header) through `...'. */
14222 location_t xloc
14223 = expansion_point_location_if_in_system_header (loc);
14224
14225 /* Unfortunately, this is merely undefined, rather than a constraint
14226 violation, so we cannot make this an error. If this call is never
14227 executed, the program is still strictly conforming. */
14228 auto_diagnostic_group d;
14229 warned = warning_at (xloc, 0,
14230 "%qT is promoted to %qT when passed through %<...%>",
14231 type, promoted_type);
14232 if (!gave_help && warned)
14233 {
14234 gave_help = true;
14235 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
14236 promoted_type, type);
14237 }
14238
14239 /* We can, however, treat "undefined" any way we please.
14240 Call abort to encourage the user to fix the program. */
14241 if (warned)
14242 inform (xloc, "if this code is reached, the program will abort");
14243 /* Before the abort, allow the evaluation of the va_list
14244 expression to exit or longjmp. */
14245 gimplify_and_add (valist, pre_p);
14246 t = build_call_expr_loc (loc,
14247 builtin_decl_implicit (BUILT_IN_TRAP), 0);
14248 gimplify_and_add (t, pre_p);
14249
14250 /* This is dead code, but go ahead and finish so that the
14251 mode of the result comes out right. */
14252 *expr_p = dummy_object (type);
14253 return GS_ALL_DONE;
14254 }
14255
14256 tag = build_int_cst (build_pointer_type (type), 0);
14257 aptag = build_int_cst (TREE_TYPE (valist), 0);
14258
14259 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
14260 valist, tag, aptag);
14261
14262 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
14263 needs to be expanded. */
14264 cfun->curr_properties &= ~PROP_gimple_lva;
14265
14266 return GS_OK;
14267 }
14268
14269 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
14270
14271 DST/SRC are the destination and source respectively. You can pass
14272 ungimplified trees in DST or SRC, in which case they will be
14273 converted to a gimple operand if necessary.
14274
14275 This function returns the newly created GIMPLE_ASSIGN tuple. */
14276
14277 gimple *
14278 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
14279 {
14280 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14281 gimplify_and_add (t, seq_p);
14282 ggc_free (t);
14283 return gimple_seq_last_stmt (*seq_p);
14284 }
14285
14286 inline hashval_t
14287 gimplify_hasher::hash (const elt_t *p)
14288 {
14289 tree t = p->val;
14290 return iterative_hash_expr (t, 0);
14291 }
14292
14293 inline bool
14294 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
14295 {
14296 tree t1 = p1->val;
14297 tree t2 = p2->val;
14298 enum tree_code code = TREE_CODE (t1);
14299
14300 if (TREE_CODE (t2) != code
14301 || TREE_TYPE (t1) != TREE_TYPE (t2))
14302 return false;
14303
14304 if (!operand_equal_p (t1, t2, 0))
14305 return false;
14306
14307 /* Only allow them to compare equal if they also hash equal; otherwise
14308 results are nondeterminate, and we fail bootstrap comparison. */
14309 gcc_checking_assert (hash (p1) == hash (p2));
14310
14311 return true;
14312 }