32d7ad63ac99e0f57a7d1df5a8b8e3070c6069e2
[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-2018 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
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
71
72 enum gimplify_omp_var_data
73 {
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
87
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
90
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
93
94 GOVD_MAP_0LEN_ARRAY = 32768,
95
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO = 65536,
98
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN = 131072,
101
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE = 262144,
104
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT = 524288,
107
108 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
109 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
110 | GOVD_LOCAL)
111 };
112
113
114 enum omp_region_type
115 {
116 ORT_WORKSHARE = 0x00,
117 ORT_SIMD = 0x01,
118
119 ORT_PARALLEL = 0x02,
120 ORT_COMBINED_PARALLEL = 0x03,
121
122 ORT_TASK = 0x04,
123 ORT_UNTIED_TASK = 0x05,
124
125 ORT_TEAMS = 0x08,
126 ORT_COMBINED_TEAMS = 0x09,
127
128 /* Data region. */
129 ORT_TARGET_DATA = 0x10,
130
131 /* Data region with offloading. */
132 ORT_TARGET = 0x20,
133 ORT_COMBINED_TARGET = 0x21,
134
135 /* OpenACC variants. */
136 ORT_ACC = 0x40, /* A generic OpenACC region. */
137 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
138 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
139 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
140 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
141
142 /* Dummy OpenMP region, used to disable expansion of
143 DECL_VALUE_EXPRs in taskloop pre body. */
144 ORT_NONE = 0x100
145 };
146
147 /* Gimplify hashtable helper. */
148
149 struct gimplify_hasher : free_ptr_hash <elt_t>
150 {
151 static inline hashval_t hash (const elt_t *);
152 static inline bool equal (const elt_t *, const elt_t *);
153 };
154
155 struct gimplify_ctx
156 {
157 struct gimplify_ctx *prev_context;
158
159 vec<gbind *> bind_expr_stack;
160 tree temps;
161 gimple_seq conditional_cleanups;
162 tree exit_label;
163 tree return_temp;
164
165 vec<tree> case_labels;
166 hash_set<tree> *live_switch_vars;
167 /* The formal temporary table. Should this be persistent? */
168 hash_table<gimplify_hasher> *temp_htab;
169
170 int conditions;
171 unsigned into_ssa : 1;
172 unsigned allow_rhs_cond_expr : 1;
173 unsigned in_cleanup_point_expr : 1;
174 unsigned keep_stack : 1;
175 unsigned save_stack : 1;
176 unsigned in_switch_expr : 1;
177 };
178
179 struct gimplify_omp_ctx
180 {
181 struct gimplify_omp_ctx *outer_context;
182 splay_tree variables;
183 hash_set<tree> *privatized_types;
184 /* Iteration variables in an OMP_FOR. */
185 vec<tree> loop_iter_var;
186 location_t location;
187 enum omp_clause_default_kind default_kind;
188 enum omp_region_type region_type;
189 bool combined_loop;
190 bool distribute;
191 bool target_map_scalars_firstprivate;
192 bool target_map_pointers_as_0len_arrays;
193 bool target_firstprivatize_array_bases;
194 };
195
196 static struct gimplify_ctx *gimplify_ctxp;
197 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
198
199 /* Forward declaration. */
200 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
201 static hash_map<tree, tree> *oacc_declare_returns;
202 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
203 bool (*) (tree), fallback_t, bool);
204
205 /* Shorter alias name for the above function for use in gimplify.c
206 only. */
207
208 static inline void
209 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
210 {
211 gimple_seq_add_stmt_without_update (seq_p, gs);
212 }
213
214 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
215 NULL, a new sequence is allocated. This function is
216 similar to gimple_seq_add_seq, but does not scan the operands.
217 During gimplification, we need to manipulate statement sequences
218 before the def/use vectors have been constructed. */
219
220 static void
221 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
222 {
223 gimple_stmt_iterator si;
224
225 if (src == NULL)
226 return;
227
228 si = gsi_last (*dst_p);
229 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
230 }
231
232
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234 and popping gimplify contexts. */
235
236 static struct gimplify_ctx *ctx_pool = NULL;
237
238 /* Return a gimplify context struct from the pool. */
239
240 static inline struct gimplify_ctx *
241 ctx_alloc (void)
242 {
243 struct gimplify_ctx * c = ctx_pool;
244
245 if (c)
246 ctx_pool = c->prev_context;
247 else
248 c = XNEW (struct gimplify_ctx);
249
250 memset (c, '\0', sizeof (*c));
251 return c;
252 }
253
254 /* Put gimplify context C back into the pool. */
255
256 static inline void
257 ctx_free (struct gimplify_ctx *c)
258 {
259 c->prev_context = ctx_pool;
260 ctx_pool = c;
261 }
262
263 /* Free allocated ctx stack memory. */
264
265 void
266 free_gimplify_stack (void)
267 {
268 struct gimplify_ctx *c;
269
270 while ((c = ctx_pool))
271 {
272 ctx_pool = c->prev_context;
273 free (c);
274 }
275 }
276
277
278 /* Set up a context for the gimplifier. */
279
280 void
281 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
282 {
283 struct gimplify_ctx *c = ctx_alloc ();
284
285 c->prev_context = gimplify_ctxp;
286 gimplify_ctxp = c;
287 gimplify_ctxp->into_ssa = in_ssa;
288 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
289 }
290
291 /* Tear down a context for the gimplifier. If BODY is non-null, then
292 put the temporaries into the outer BIND_EXPR. Otherwise, put them
293 in the local_decls.
294
295 BODY is not a sequence, but the first tuple in a sequence. */
296
297 void
298 pop_gimplify_context (gimple *body)
299 {
300 struct gimplify_ctx *c = gimplify_ctxp;
301
302 gcc_assert (c
303 && (!c->bind_expr_stack.exists ()
304 || c->bind_expr_stack.is_empty ()));
305 c->bind_expr_stack.release ();
306 gimplify_ctxp = c->prev_context;
307
308 if (body)
309 declare_vars (c->temps, body, false);
310 else
311 record_vars (c->temps);
312
313 delete c->temp_htab;
314 c->temp_htab = NULL;
315 ctx_free (c);
316 }
317
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
319
320 static void
321 gimple_push_bind_expr (gbind *bind_stmt)
322 {
323 gimplify_ctxp->bind_expr_stack.reserve (8);
324 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
325 }
326
327 /* Pop the first element off the stack of bindings. */
328
329 static void
330 gimple_pop_bind_expr (void)
331 {
332 gimplify_ctxp->bind_expr_stack.pop ();
333 }
334
335 /* Return the first element of the stack of bindings. */
336
337 gbind *
338 gimple_current_bind_expr (void)
339 {
340 return gimplify_ctxp->bind_expr_stack.last ();
341 }
342
343 /* Return the stack of bindings created during gimplification. */
344
345 vec<gbind *>
346 gimple_bind_expr_stack (void)
347 {
348 return gimplify_ctxp->bind_expr_stack;
349 }
350
351 /* Return true iff there is a COND_EXPR between us and the innermost
352 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
353
354 static bool
355 gimple_conditional_context (void)
356 {
357 return gimplify_ctxp->conditions > 0;
358 }
359
360 /* Note that we've entered a COND_EXPR. */
361
362 static void
363 gimple_push_condition (void)
364 {
365 #ifdef ENABLE_GIMPLE_CHECKING
366 if (gimplify_ctxp->conditions == 0)
367 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
368 #endif
369 ++(gimplify_ctxp->conditions);
370 }
371
372 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
373 now, add any conditional cleanups we've seen to the prequeue. */
374
375 static void
376 gimple_pop_condition (gimple_seq *pre_p)
377 {
378 int conds = --(gimplify_ctxp->conditions);
379
380 gcc_assert (conds >= 0);
381 if (conds == 0)
382 {
383 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
384 gimplify_ctxp->conditional_cleanups = NULL;
385 }
386 }
387
388 /* A stable comparison routine for use with splay trees and DECLs. */
389
390 static int
391 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
392 {
393 tree a = (tree) xa;
394 tree b = (tree) xb;
395
396 return DECL_UID (a) - DECL_UID (b);
397 }
398
399 /* Create a new omp construct that deals with variable remapping. */
400
401 static struct gimplify_omp_ctx *
402 new_omp_context (enum omp_region_type region_type)
403 {
404 struct gimplify_omp_ctx *c;
405
406 c = XCNEW (struct gimplify_omp_ctx);
407 c->outer_context = gimplify_omp_ctxp;
408 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
409 c->privatized_types = new hash_set<tree>;
410 c->location = input_location;
411 c->region_type = region_type;
412 if ((region_type & ORT_TASK) == 0)
413 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
414 else
415 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
416
417 return c;
418 }
419
420 /* Destroy an omp construct that deals with variable remapping. */
421
422 static void
423 delete_omp_context (struct gimplify_omp_ctx *c)
424 {
425 splay_tree_delete (c->variables);
426 delete c->privatized_types;
427 c->loop_iter_var.release ();
428 XDELETE (c);
429 }
430
431 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
433
434 /* Both gimplify the statement T and append it to *SEQ_P. This function
435 behaves exactly as gimplify_stmt, but you don't have to pass T as a
436 reference. */
437
438 void
439 gimplify_and_add (tree t, gimple_seq *seq_p)
440 {
441 gimplify_stmt (&t, seq_p);
442 }
443
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445 tuple in the sequence of generated tuples for this statement.
446 Return NULL if gimplifying T produced no tuples. */
447
448 static gimple *
449 gimplify_and_return_first (tree t, gimple_seq *seq_p)
450 {
451 gimple_stmt_iterator last = gsi_last (*seq_p);
452
453 gimplify_and_add (t, seq_p);
454
455 if (!gsi_end_p (last))
456 {
457 gsi_next (&last);
458 return gsi_stmt (last);
459 }
460 else
461 return gimple_seq_first_stmt (*seq_p);
462 }
463
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465 LHS, or for a call argument. */
466
467 static bool
468 is_gimple_mem_rhs (tree t)
469 {
470 /* If we're dealing with a renamable type, either source or dest must be
471 a renamed variable. */
472 if (is_gimple_reg_type (TREE_TYPE (t)))
473 return is_gimple_val (t);
474 else
475 return is_gimple_val (t) || is_gimple_lvalue (t);
476 }
477
478 /* Return true if T is a CALL_EXPR or an expression that can be
479 assigned to a temporary. Note that this predicate should only be
480 used during gimplification. See the rationale for this in
481 gimplify_modify_expr. */
482
483 static bool
484 is_gimple_reg_rhs_or_call (tree t)
485 {
486 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
487 || TREE_CODE (t) == CALL_EXPR);
488 }
489
490 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
491 this predicate should only be used during gimplification. See the
492 rationale for this in gimplify_modify_expr. */
493
494 static bool
495 is_gimple_mem_rhs_or_call (tree t)
496 {
497 /* If we're dealing with a renamable type, either source or dest must be
498 a renamed variable. */
499 if (is_gimple_reg_type (TREE_TYPE (t)))
500 return is_gimple_val (t);
501 else
502 return (is_gimple_val (t)
503 || is_gimple_lvalue (t)
504 || TREE_CLOBBER_P (t)
505 || TREE_CODE (t) == CALL_EXPR);
506 }
507
508 /* Create a temporary with a name derived from VAL. Subroutine of
509 lookup_tmp_var; nobody else should call this function. */
510
511 static inline tree
512 create_tmp_from_val (tree val)
513 {
514 /* Drop all qualifiers and address-space information from the value type. */
515 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
516 tree var = create_tmp_var (type, get_name (val));
517 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
518 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
519 DECL_GIMPLE_REG_P (var) = 1;
520 return var;
521 }
522
523 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
524 an existing expression temporary. */
525
526 static tree
527 lookup_tmp_var (tree val, bool is_formal)
528 {
529 tree ret;
530
531 /* If not optimizing, never really reuse a temporary. local-alloc
532 won't allocate any variable that is used in more than one basic
533 block, which means it will go into memory, causing much extra
534 work in reload and final and poorer code generation, outweighing
535 the extra memory allocation here. */
536 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
537 ret = create_tmp_from_val (val);
538 else
539 {
540 elt_t elt, *elt_p;
541 elt_t **slot;
542
543 elt.val = val;
544 if (!gimplify_ctxp->temp_htab)
545 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
546 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
547 if (*slot == NULL)
548 {
549 elt_p = XNEW (elt_t);
550 elt_p->val = val;
551 elt_p->temp = ret = create_tmp_from_val (val);
552 *slot = elt_p;
553 }
554 else
555 {
556 elt_p = *slot;
557 ret = elt_p->temp;
558 }
559 }
560
561 return ret;
562 }
563
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
565
566 static tree
567 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
568 bool is_formal, bool allow_ssa)
569 {
570 tree t, mod;
571
572 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
574 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
575 fb_rvalue);
576
577 if (allow_ssa
578 && gimplify_ctxp->into_ssa
579 && is_gimple_reg_type (TREE_TYPE (val)))
580 {
581 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
582 if (! gimple_in_ssa_p (cfun))
583 {
584 const char *name = get_name (val);
585 if (name)
586 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
587 }
588 }
589 else
590 t = lookup_tmp_var (val, is_formal);
591
592 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
593
594 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
595
596 /* gimplify_modify_expr might want to reduce this further. */
597 gimplify_and_add (mod, pre_p);
598 ggc_free (mod);
599
600 return t;
601 }
602
603 /* Return a formal temporary variable initialized with VAL. PRE_P is as
604 in gimplify_expr. Only use this function if:
605
606 1) The value of the unfactored expression represented by VAL will not
607 change between the initialization and use of the temporary, and
608 2) The temporary will not be otherwise modified.
609
610 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611 and #2 means it is inappropriate for && temps.
612
613 For other cases, use get_initialized_tmp_var instead. */
614
615 tree
616 get_formal_tmp_var (tree val, gimple_seq *pre_p)
617 {
618 return internal_get_tmp_var (val, pre_p, NULL, true, true);
619 }
620
621 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
622 are as in gimplify_expr. */
623
624 tree
625 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
626 bool allow_ssa)
627 {
628 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
629 }
630
631 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
632 generate debug info for them; otherwise don't. */
633
634 void
635 declare_vars (tree vars, gimple *gs, bool debug_info)
636 {
637 tree last = vars;
638 if (last)
639 {
640 tree temps, block;
641
642 gbind *scope = as_a <gbind *> (gs);
643
644 temps = nreverse (last);
645
646 block = gimple_bind_block (scope);
647 gcc_assert (!block || TREE_CODE (block) == BLOCK);
648 if (!block || !debug_info)
649 {
650 DECL_CHAIN (last) = gimple_bind_vars (scope);
651 gimple_bind_set_vars (scope, temps);
652 }
653 else
654 {
655 /* We need to attach the nodes both to the BIND_EXPR and to its
656 associated BLOCK for debugging purposes. The key point here
657 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
659 if (BLOCK_VARS (block))
660 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
661 else
662 {
663 gimple_bind_set_vars (scope,
664 chainon (gimple_bind_vars (scope), temps));
665 BLOCK_VARS (block) = temps;
666 }
667 }
668 }
669 }
670
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
673 no such upper bound can be obtained. */
674
675 static void
676 force_constant_size (tree var)
677 {
678 /* The only attempt we make is by querying the maximum size of objects
679 of the variable's type. */
680
681 HOST_WIDE_INT max_size;
682
683 gcc_assert (VAR_P (var));
684
685 max_size = max_int_size_in_bytes (TREE_TYPE (var));
686
687 gcc_assert (max_size >= 0);
688
689 DECL_SIZE_UNIT (var)
690 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
691 DECL_SIZE (var)
692 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
693 }
694
695 /* Push the temporary variable TMP into the current binding. */
696
697 void
698 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
699 {
700 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
701
702 /* Later processing assumes that the object size is constant, which might
703 not be true at this point. Force the use of a constant upper bound in
704 this case. */
705 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
706 force_constant_size (tmp);
707
708 DECL_CONTEXT (tmp) = fn->decl;
709 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
710
711 record_vars_into (tmp, fn->decl);
712 }
713
714 /* Push the temporary variable TMP into the current binding. */
715
716 void
717 gimple_add_tmp_var (tree tmp)
718 {
719 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
720
721 /* Later processing assumes that the object size is constant, which might
722 not be true at this point. Force the use of a constant upper bound in
723 this case. */
724 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
725 force_constant_size (tmp);
726
727 DECL_CONTEXT (tmp) = current_function_decl;
728 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
729
730 if (gimplify_ctxp)
731 {
732 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
733 gimplify_ctxp->temps = tmp;
734
735 /* Mark temporaries local within the nearest enclosing parallel. */
736 if (gimplify_omp_ctxp)
737 {
738 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
739 while (ctx
740 && (ctx->region_type == ORT_WORKSHARE
741 || ctx->region_type == ORT_SIMD
742 || ctx->region_type == ORT_ACC))
743 ctx = ctx->outer_context;
744 if (ctx)
745 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
746 }
747 }
748 else if (cfun)
749 record_vars (tmp);
750 else
751 {
752 gimple_seq body_seq;
753
754 /* This case is for nested functions. We need to expose the locals
755 they create. */
756 body_seq = gimple_body (current_function_decl);
757 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
758 }
759 }
760
761
762 \f
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764 nodes that are referenced more than once in GENERIC functions. This is
765 necessary because gimplification (translation into GIMPLE) is performed
766 by modifying tree nodes in-place, so gimplication of a shared node in a
767 first context could generate an invalid GIMPLE form in a second context.
768
769 This is achieved with a simple mark/copy/unmark algorithm that walks the
770 GENERIC representation top-down, marks nodes with TREE_VISITED the first
771 time it encounters them, duplicates them if they already have TREE_VISITED
772 set, and finally removes the TREE_VISITED marks it has set.
773
774 The algorithm works only at the function level, i.e. it generates a GENERIC
775 representation of a function with no nodes shared within the function when
776 passed a GENERIC function (except for nodes that are allowed to be shared).
777
778 At the global level, it is also necessary to unshare tree nodes that are
779 referenced in more than one function, for the same aforementioned reason.
780 This requires some cooperation from the front-end. There are 2 strategies:
781
782 1. Manual unsharing. The front-end needs to call unshare_expr on every
783 expression that might end up being shared across functions.
784
785 2. Deep unsharing. This is an extension of regular unsharing. Instead
786 of calling unshare_expr on expressions that might be shared across
787 functions, the front-end pre-marks them with TREE_VISITED. This will
788 ensure that they are unshared on the first reference within functions
789 when the regular unsharing algorithm runs. The counterpart is that
790 this algorithm must look deeper than for manual unsharing, which is
791 specified by LANG_HOOKS_DEEP_UNSHARING.
792
793 If there are only few specific cases of node sharing across functions, it is
794 probably easier for a front-end to unshare the expressions manually. On the
795 contrary, if the expressions generated at the global level are as widespread
796 as expressions generated within functions, deep unsharing is very likely the
797 way to go. */
798
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that must be done once. If we were to
801 unshare something like SAVE_EXPR(i++), the gimplification process would
802 create wrong code. However, if DATA is non-null, it must hold a pointer
803 set that is used to unshare the subtrees of these nodes. */
804
805 static tree
806 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
807 {
808 tree t = *tp;
809 enum tree_code code = TREE_CODE (t);
810
811 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812 copy their subtrees if we can make sure to do it only once. */
813 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
814 {
815 if (data && !((hash_set<tree> *)data)->add (t))
816 ;
817 else
818 *walk_subtrees = 0;
819 }
820
821 /* Stop at types, decls, constants like copy_tree_r. */
822 else if (TREE_CODE_CLASS (code) == tcc_type
823 || TREE_CODE_CLASS (code) == tcc_declaration
824 || TREE_CODE_CLASS (code) == tcc_constant)
825 *walk_subtrees = 0;
826
827 /* Cope with the statement expression extension. */
828 else if (code == STATEMENT_LIST)
829 ;
830
831 /* Leave the bulk of the work to copy_tree_r itself. */
832 else
833 copy_tree_r (tp, walk_subtrees, NULL);
834
835 return NULL_TREE;
836 }
837
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839 If *TP has been visited already, then *TP is deeply copied by calling
840 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
841
842 static tree
843 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
844 {
845 tree t = *tp;
846 enum tree_code code = TREE_CODE (t);
847
848 /* Skip types, decls, and constants. But we do want to look at their
849 types and the bounds of types. Mark them as visited so we properly
850 unmark their subtrees on the unmark pass. If we've already seen them,
851 don't look down further. */
852 if (TREE_CODE_CLASS (code) == tcc_type
853 || TREE_CODE_CLASS (code) == tcc_declaration
854 || TREE_CODE_CLASS (code) == tcc_constant)
855 {
856 if (TREE_VISITED (t))
857 *walk_subtrees = 0;
858 else
859 TREE_VISITED (t) = 1;
860 }
861
862 /* If this node has been visited already, unshare it and don't look
863 any deeper. */
864 else if (TREE_VISITED (t))
865 {
866 walk_tree (tp, mostly_copy_tree_r, data, NULL);
867 *walk_subtrees = 0;
868 }
869
870 /* Otherwise, mark the node as visited and keep looking. */
871 else
872 TREE_VISITED (t) = 1;
873
874 return NULL_TREE;
875 }
876
877 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
878 copy_if_shared_r callback unmodified. */
879
880 static inline void
881 copy_if_shared (tree *tp, void *data)
882 {
883 walk_tree (tp, copy_if_shared_r, data, NULL);
884 }
885
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887 any nested functions. */
888
889 static void
890 unshare_body (tree fndecl)
891 {
892 struct cgraph_node *cgn = cgraph_node::get (fndecl);
893 /* If the language requires deep unsharing, we need a pointer set to make
894 sure we don't repeatedly unshare subtrees of unshareable nodes. */
895 hash_set<tree> *visited
896 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
897
898 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
899 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
900 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
901
902 delete visited;
903
904 if (cgn)
905 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
906 unshare_body (cgn->decl);
907 }
908
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910 Subtrees are walked until the first unvisited node is encountered. */
911
912 static tree
913 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
914 {
915 tree t = *tp;
916
917 /* If this node has been visited, unmark it and keep looking. */
918 if (TREE_VISITED (t))
919 TREE_VISITED (t) = 0;
920
921 /* Otherwise, don't look any deeper. */
922 else
923 *walk_subtrees = 0;
924
925 return NULL_TREE;
926 }
927
928 /* Unmark the visited trees rooted at *TP. */
929
930 static inline void
931 unmark_visited (tree *tp)
932 {
933 walk_tree (tp, unmark_visited_r, NULL, NULL);
934 }
935
936 /* Likewise, but mark all trees as not visited. */
937
938 static void
939 unvisit_body (tree fndecl)
940 {
941 struct cgraph_node *cgn = cgraph_node::get (fndecl);
942
943 unmark_visited (&DECL_SAVED_TREE (fndecl));
944 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
945 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
946
947 if (cgn)
948 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
949 unvisit_body (cgn->decl);
950 }
951
952 /* Unconditionally make an unshared copy of EXPR. This is used when using
953 stored expressions which span multiple functions, such as BINFO_VTABLE,
954 as the normal unsharing process can't tell that they're shared. */
955
956 tree
957 unshare_expr (tree expr)
958 {
959 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
960 return expr;
961 }
962
963 /* Worker for unshare_expr_without_location. */
964
965 static tree
966 prune_expr_location (tree *tp, int *walk_subtrees, void *)
967 {
968 if (EXPR_P (*tp))
969 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
970 else
971 *walk_subtrees = 0;
972 return NULL_TREE;
973 }
974
975 /* Similar to unshare_expr but also prune all expression locations
976 from EXPR. */
977
978 tree
979 unshare_expr_without_location (tree expr)
980 {
981 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
982 if (EXPR_P (expr))
983 walk_tree (&expr, prune_expr_location, NULL, NULL);
984 return expr;
985 }
986
987 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
988 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
989 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
990 EXPR is the location of the EXPR. */
991
992 static location_t
993 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
994 {
995 if (!expr)
996 return or_else;
997
998 if (EXPR_HAS_LOCATION (expr))
999 return EXPR_LOCATION (expr);
1000
1001 if (TREE_CODE (expr) != STATEMENT_LIST)
1002 return or_else;
1003
1004 tree_stmt_iterator i = tsi_start (expr);
1005
1006 bool found = false;
1007 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1008 {
1009 found = true;
1010 tsi_next (&i);
1011 }
1012
1013 if (!found || !tsi_one_before_end_p (i))
1014 return or_else;
1015
1016 return rexpr_location (tsi_stmt (i), or_else);
1017 }
1018
1019 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1020 rexpr_location for the potential recursion. */
1021
1022 static inline bool
1023 rexpr_has_location (tree expr)
1024 {
1025 return rexpr_location (expr) != UNKNOWN_LOCATION;
1026 }
1027
1028 \f
1029 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1030 contain statements and have a value. Assign its value to a temporary
1031 and give it void_type_node. Return the temporary, or NULL_TREE if
1032 WRAPPER was already void. */
1033
1034 tree
1035 voidify_wrapper_expr (tree wrapper, tree temp)
1036 {
1037 tree type = TREE_TYPE (wrapper);
1038 if (type && !VOID_TYPE_P (type))
1039 {
1040 tree *p;
1041
1042 /* Set p to point to the body of the wrapper. Loop until we find
1043 something that isn't a wrapper. */
1044 for (p = &wrapper; p && *p; )
1045 {
1046 switch (TREE_CODE (*p))
1047 {
1048 case BIND_EXPR:
1049 TREE_SIDE_EFFECTS (*p) = 1;
1050 TREE_TYPE (*p) = void_type_node;
1051 /* For a BIND_EXPR, the body is operand 1. */
1052 p = &BIND_EXPR_BODY (*p);
1053 break;
1054
1055 case CLEANUP_POINT_EXPR:
1056 case TRY_FINALLY_EXPR:
1057 case TRY_CATCH_EXPR:
1058 TREE_SIDE_EFFECTS (*p) = 1;
1059 TREE_TYPE (*p) = void_type_node;
1060 p = &TREE_OPERAND (*p, 0);
1061 break;
1062
1063 case STATEMENT_LIST:
1064 {
1065 tree_stmt_iterator i = tsi_last (*p);
1066 TREE_SIDE_EFFECTS (*p) = 1;
1067 TREE_TYPE (*p) = void_type_node;
1068 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1069 }
1070 break;
1071
1072 case COMPOUND_EXPR:
1073 /* Advance to the last statement. Set all container types to
1074 void. */
1075 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1076 {
1077 TREE_SIDE_EFFECTS (*p) = 1;
1078 TREE_TYPE (*p) = void_type_node;
1079 }
1080 break;
1081
1082 case TRANSACTION_EXPR:
1083 TREE_SIDE_EFFECTS (*p) = 1;
1084 TREE_TYPE (*p) = void_type_node;
1085 p = &TRANSACTION_EXPR_BODY (*p);
1086 break;
1087
1088 default:
1089 /* Assume that any tree upon which voidify_wrapper_expr is
1090 directly called is a wrapper, and that its body is op0. */
1091 if (p == &wrapper)
1092 {
1093 TREE_SIDE_EFFECTS (*p) = 1;
1094 TREE_TYPE (*p) = void_type_node;
1095 p = &TREE_OPERAND (*p, 0);
1096 break;
1097 }
1098 goto out;
1099 }
1100 }
1101
1102 out:
1103 if (p == NULL || IS_EMPTY_STMT (*p))
1104 temp = NULL_TREE;
1105 else if (temp)
1106 {
1107 /* The wrapper is on the RHS of an assignment that we're pushing
1108 down. */
1109 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1110 || TREE_CODE (temp) == MODIFY_EXPR);
1111 TREE_OPERAND (temp, 1) = *p;
1112 *p = temp;
1113 }
1114 else
1115 {
1116 temp = create_tmp_var (type, "retval");
1117 *p = build2 (INIT_EXPR, type, temp, *p);
1118 }
1119
1120 return temp;
1121 }
1122
1123 return NULL_TREE;
1124 }
1125
1126 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1127 a temporary through which they communicate. */
1128
1129 static void
1130 build_stack_save_restore (gcall **save, gcall **restore)
1131 {
1132 tree tmp_var;
1133
1134 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1135 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1136 gimple_call_set_lhs (*save, tmp_var);
1137
1138 *restore
1139 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1140 1, tmp_var);
1141 }
1142
1143 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1144
1145 static tree
1146 build_asan_poison_call_expr (tree decl)
1147 {
1148 /* Do not poison variables that have size equal to zero. */
1149 tree unit_size = DECL_SIZE_UNIT (decl);
1150 if (zerop (unit_size))
1151 return NULL_TREE;
1152
1153 tree base = build_fold_addr_expr (decl);
1154
1155 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1156 void_type_node, 3,
1157 build_int_cst (integer_type_node,
1158 ASAN_MARK_POISON),
1159 base, unit_size);
1160 }
1161
1162 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1163 on POISON flag, shadow memory of a DECL variable. The call will be
1164 put on location identified by IT iterator, where BEFORE flag drives
1165 position where the stmt will be put. */
1166
1167 static void
1168 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1169 bool before)
1170 {
1171 tree unit_size = DECL_SIZE_UNIT (decl);
1172 tree base = build_fold_addr_expr (decl);
1173
1174 /* Do not poison variables that have size equal to zero. */
1175 if (zerop (unit_size))
1176 return;
1177
1178 /* It's necessary to have all stack variables aligned to ASAN granularity
1179 bytes. */
1180 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1181 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1182
1183 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1184
1185 gimple *g
1186 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1187 build_int_cst (integer_type_node, flags),
1188 base, unit_size);
1189
1190 if (before)
1191 gsi_insert_before (it, g, GSI_NEW_STMT);
1192 else
1193 gsi_insert_after (it, g, GSI_NEW_STMT);
1194 }
1195
1196 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1197 either poisons or unpoisons a DECL. Created statement is appended
1198 to SEQ_P gimple sequence. */
1199
1200 static void
1201 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1202 {
1203 gimple_stmt_iterator it = gsi_last (*seq_p);
1204 bool before = false;
1205
1206 if (gsi_end_p (it))
1207 before = true;
1208
1209 asan_poison_variable (decl, poison, &it, before);
1210 }
1211
1212 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1213
1214 static int
1215 sort_by_decl_uid (const void *a, const void *b)
1216 {
1217 const tree *t1 = (const tree *)a;
1218 const tree *t2 = (const tree *)b;
1219
1220 int uid1 = DECL_UID (*t1);
1221 int uid2 = DECL_UID (*t2);
1222
1223 if (uid1 < uid2)
1224 return -1;
1225 else if (uid1 > uid2)
1226 return 1;
1227 else
1228 return 0;
1229 }
1230
1231 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1232 depending on POISON flag. Created statement is appended
1233 to SEQ_P gimple sequence. */
1234
1235 static void
1236 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1237 {
1238 unsigned c = variables->elements ();
1239 if (c == 0)
1240 return;
1241
1242 auto_vec<tree> sorted_variables (c);
1243
1244 for (hash_set<tree>::iterator it = variables->begin ();
1245 it != variables->end (); ++it)
1246 sorted_variables.safe_push (*it);
1247
1248 sorted_variables.qsort (sort_by_decl_uid);
1249
1250 unsigned i;
1251 tree var;
1252 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1253 {
1254 asan_poison_variable (var, poison, seq_p);
1255
1256 /* Add use_after_scope_memory attribute for the variable in order
1257 to prevent re-written into SSA. */
1258 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1259 DECL_ATTRIBUTES (var)))
1260 DECL_ATTRIBUTES (var)
1261 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1262 integer_one_node,
1263 DECL_ATTRIBUTES (var));
1264 }
1265 }
1266
1267 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1268
1269 static enum gimplify_status
1270 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1271 {
1272 tree bind_expr = *expr_p;
1273 bool old_keep_stack = gimplify_ctxp->keep_stack;
1274 bool old_save_stack = gimplify_ctxp->save_stack;
1275 tree t;
1276 gbind *bind_stmt;
1277 gimple_seq body, cleanup;
1278 gcall *stack_save;
1279 location_t start_locus = 0, end_locus = 0;
1280 tree ret_clauses = NULL;
1281
1282 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1283
1284 /* Mark variables seen in this bind expr. */
1285 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1286 {
1287 if (VAR_P (t))
1288 {
1289 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1290
1291 /* Mark variable as local. */
1292 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1293 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1294 || splay_tree_lookup (ctx->variables,
1295 (splay_tree_key) t) == NULL))
1296 {
1297 if (ctx->region_type == ORT_SIMD
1298 && TREE_ADDRESSABLE (t)
1299 && !TREE_STATIC (t))
1300 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1301 else
1302 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1303 }
1304
1305 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1306
1307 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1308 cfun->has_local_explicit_reg_vars = true;
1309 }
1310
1311 /* Preliminarily mark non-addressed complex variables as eligible
1312 for promotion to gimple registers. We'll transform their uses
1313 as we find them. */
1314 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1315 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1316 && !TREE_THIS_VOLATILE (t)
1317 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1318 && !needs_to_live_in_memory (t))
1319 DECL_GIMPLE_REG_P (t) = 1;
1320 }
1321
1322 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1323 BIND_EXPR_BLOCK (bind_expr));
1324 gimple_push_bind_expr (bind_stmt);
1325
1326 gimplify_ctxp->keep_stack = false;
1327 gimplify_ctxp->save_stack = false;
1328
1329 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1330 body = NULL;
1331 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1332 gimple_bind_set_body (bind_stmt, body);
1333
1334 /* Source location wise, the cleanup code (stack_restore and clobbers)
1335 belongs to the end of the block, so propagate what we have. The
1336 stack_save operation belongs to the beginning of block, which we can
1337 infer from the bind_expr directly if the block has no explicit
1338 assignment. */
1339 if (BIND_EXPR_BLOCK (bind_expr))
1340 {
1341 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1342 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1343 }
1344 if (start_locus == 0)
1345 start_locus = EXPR_LOCATION (bind_expr);
1346
1347 cleanup = NULL;
1348 stack_save = NULL;
1349
1350 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1351 the stack space allocated to the VLAs. */
1352 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1353 {
1354 gcall *stack_restore;
1355
1356 /* Save stack on entry and restore it on exit. Add a try_finally
1357 block to achieve this. */
1358 build_stack_save_restore (&stack_save, &stack_restore);
1359
1360 gimple_set_location (stack_save, start_locus);
1361 gimple_set_location (stack_restore, end_locus);
1362
1363 gimplify_seq_add_stmt (&cleanup, stack_restore);
1364 }
1365
1366 /* Add clobbers for all variables that go out of scope. */
1367 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1368 {
1369 if (VAR_P (t)
1370 && !is_global_var (t)
1371 && DECL_CONTEXT (t) == current_function_decl)
1372 {
1373 if (!DECL_HARD_REGISTER (t)
1374 && !TREE_THIS_VOLATILE (t)
1375 && !DECL_HAS_VALUE_EXPR_P (t)
1376 /* Only care for variables that have to be in memory. Others
1377 will be rewritten into SSA names, hence moved to the
1378 top-level. */
1379 && !is_gimple_reg (t)
1380 && flag_stack_reuse != SR_NONE)
1381 {
1382 tree clobber = build_clobber (TREE_TYPE (t));
1383 gimple *clobber_stmt;
1384 clobber_stmt = gimple_build_assign (t, clobber);
1385 gimple_set_location (clobber_stmt, end_locus);
1386 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1387 }
1388
1389 if (flag_openacc && oacc_declare_returns != NULL)
1390 {
1391 tree *c = oacc_declare_returns->get (t);
1392 if (c != NULL)
1393 {
1394 if (ret_clauses)
1395 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1396
1397 ret_clauses = *c;
1398
1399 oacc_declare_returns->remove (t);
1400
1401 if (oacc_declare_returns->elements () == 0)
1402 {
1403 delete oacc_declare_returns;
1404 oacc_declare_returns = NULL;
1405 }
1406 }
1407 }
1408 }
1409
1410 if (asan_poisoned_variables != NULL
1411 && asan_poisoned_variables->contains (t))
1412 {
1413 asan_poisoned_variables->remove (t);
1414 asan_poison_variable (t, true, &cleanup);
1415 }
1416
1417 if (gimplify_ctxp->live_switch_vars != NULL
1418 && gimplify_ctxp->live_switch_vars->contains (t))
1419 gimplify_ctxp->live_switch_vars->remove (t);
1420 }
1421
1422 if (ret_clauses)
1423 {
1424 gomp_target *stmt;
1425 gimple_stmt_iterator si = gsi_start (cleanup);
1426
1427 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1428 ret_clauses);
1429 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1430 }
1431
1432 if (cleanup)
1433 {
1434 gtry *gs;
1435 gimple_seq new_body;
1436
1437 new_body = NULL;
1438 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1439 GIMPLE_TRY_FINALLY);
1440
1441 if (stack_save)
1442 gimplify_seq_add_stmt (&new_body, stack_save);
1443 gimplify_seq_add_stmt (&new_body, gs);
1444 gimple_bind_set_body (bind_stmt, new_body);
1445 }
1446
1447 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1448 if (!gimplify_ctxp->keep_stack)
1449 gimplify_ctxp->keep_stack = old_keep_stack;
1450 gimplify_ctxp->save_stack = old_save_stack;
1451
1452 gimple_pop_bind_expr ();
1453
1454 gimplify_seq_add_stmt (pre_p, bind_stmt);
1455
1456 if (temp)
1457 {
1458 *expr_p = temp;
1459 return GS_OK;
1460 }
1461
1462 *expr_p = NULL_TREE;
1463 return GS_ALL_DONE;
1464 }
1465
1466 /* Maybe add early return predict statement to PRE_P sequence. */
1467
1468 static void
1469 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1470 {
1471 /* If we are not in a conditional context, add PREDICT statement. */
1472 if (gimple_conditional_context ())
1473 {
1474 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1475 NOT_TAKEN);
1476 gimplify_seq_add_stmt (pre_p, predict);
1477 }
1478 }
1479
1480 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1481 GIMPLE value, it is assigned to a new temporary and the statement is
1482 re-written to return the temporary.
1483
1484 PRE_P points to the sequence where side effects that must happen before
1485 STMT should be stored. */
1486
1487 static enum gimplify_status
1488 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1489 {
1490 greturn *ret;
1491 tree ret_expr = TREE_OPERAND (stmt, 0);
1492 tree result_decl, result;
1493
1494 if (ret_expr == error_mark_node)
1495 return GS_ERROR;
1496
1497 if (!ret_expr
1498 || TREE_CODE (ret_expr) == RESULT_DECL)
1499 {
1500 maybe_add_early_return_predict_stmt (pre_p);
1501 greturn *ret = gimple_build_return (ret_expr);
1502 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1503 gimplify_seq_add_stmt (pre_p, ret);
1504 return GS_ALL_DONE;
1505 }
1506
1507 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1508 result_decl = NULL_TREE;
1509 else
1510 {
1511 result_decl = TREE_OPERAND (ret_expr, 0);
1512
1513 /* See through a return by reference. */
1514 if (TREE_CODE (result_decl) == INDIRECT_REF)
1515 result_decl = TREE_OPERAND (result_decl, 0);
1516
1517 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1518 || TREE_CODE (ret_expr) == INIT_EXPR)
1519 && TREE_CODE (result_decl) == RESULT_DECL);
1520 }
1521
1522 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1523 Recall that aggregate_value_p is FALSE for any aggregate type that is
1524 returned in registers. If we're returning values in registers, then
1525 we don't want to extend the lifetime of the RESULT_DECL, particularly
1526 across another call. In addition, for those aggregates for which
1527 hard_function_value generates a PARALLEL, we'll die during normal
1528 expansion of structure assignments; there's special code in expand_return
1529 to handle this case that does not exist in expand_expr. */
1530 if (!result_decl)
1531 result = NULL_TREE;
1532 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1533 {
1534 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1535 {
1536 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1537 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1538 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1539 should be effectively allocated by the caller, i.e. all calls to
1540 this function must be subject to the Return Slot Optimization. */
1541 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1542 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1543 }
1544 result = result_decl;
1545 }
1546 else if (gimplify_ctxp->return_temp)
1547 result = gimplify_ctxp->return_temp;
1548 else
1549 {
1550 result = create_tmp_reg (TREE_TYPE (result_decl));
1551
1552 /* ??? With complex control flow (usually involving abnormal edges),
1553 we can wind up warning about an uninitialized value for this. Due
1554 to how this variable is constructed and initialized, this is never
1555 true. Give up and never warn. */
1556 TREE_NO_WARNING (result) = 1;
1557
1558 gimplify_ctxp->return_temp = result;
1559 }
1560
1561 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1562 Then gimplify the whole thing. */
1563 if (result != result_decl)
1564 TREE_OPERAND (ret_expr, 0) = result;
1565
1566 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1567
1568 maybe_add_early_return_predict_stmt (pre_p);
1569 ret = gimple_build_return (result);
1570 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1571 gimplify_seq_add_stmt (pre_p, ret);
1572
1573 return GS_ALL_DONE;
1574 }
1575
1576 /* Gimplify a variable-length array DECL. */
1577
1578 static void
1579 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1580 {
1581 /* This is a variable-sized decl. Simplify its size and mark it
1582 for deferred expansion. */
1583 tree t, addr, ptr_type;
1584
1585 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1586 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1587
1588 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1589 if (DECL_HAS_VALUE_EXPR_P (decl))
1590 return;
1591
1592 /* All occurrences of this decl in final gimplified code will be
1593 replaced by indirection. Setting DECL_VALUE_EXPR does two
1594 things: First, it lets the rest of the gimplifier know what
1595 replacement to use. Second, it lets the debug info know
1596 where to find the value. */
1597 ptr_type = build_pointer_type (TREE_TYPE (decl));
1598 addr = create_tmp_var (ptr_type, get_name (decl));
1599 DECL_IGNORED_P (addr) = 0;
1600 t = build_fold_indirect_ref (addr);
1601 TREE_THIS_NOTRAP (t) = 1;
1602 SET_DECL_VALUE_EXPR (decl, t);
1603 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1604
1605 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1606 max_int_size_in_bytes (TREE_TYPE (decl)));
1607 /* The call has been built for a variable-sized object. */
1608 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1609 t = fold_convert (ptr_type, t);
1610 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1611
1612 gimplify_and_add (t, seq_p);
1613 }
1614
1615 /* A helper function to be called via walk_tree. Mark all labels under *TP
1616 as being forced. To be called for DECL_INITIAL of static variables. */
1617
1618 static tree
1619 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1620 {
1621 if (TYPE_P (*tp))
1622 *walk_subtrees = 0;
1623 if (TREE_CODE (*tp) == LABEL_DECL)
1624 {
1625 FORCED_LABEL (*tp) = 1;
1626 cfun->has_forced_label_in_static = 1;
1627 }
1628
1629 return NULL_TREE;
1630 }
1631
1632 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1633 and initialization explicit. */
1634
1635 static enum gimplify_status
1636 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1637 {
1638 tree stmt = *stmt_p;
1639 tree decl = DECL_EXPR_DECL (stmt);
1640
1641 *stmt_p = NULL_TREE;
1642
1643 if (TREE_TYPE (decl) == error_mark_node)
1644 return GS_ERROR;
1645
1646 if ((TREE_CODE (decl) == TYPE_DECL
1647 || VAR_P (decl))
1648 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1649 {
1650 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1651 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1652 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1653 }
1654
1655 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1656 in case its size expressions contain problematic nodes like CALL_EXPR. */
1657 if (TREE_CODE (decl) == TYPE_DECL
1658 && DECL_ORIGINAL_TYPE (decl)
1659 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1660 {
1661 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1662 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1663 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1664 }
1665
1666 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1667 {
1668 tree init = DECL_INITIAL (decl);
1669 bool is_vla = false;
1670
1671 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1672 || (!TREE_STATIC (decl)
1673 && flag_stack_check == GENERIC_STACK_CHECK
1674 && compare_tree_int (DECL_SIZE_UNIT (decl),
1675 STACK_CHECK_MAX_VAR_SIZE) > 0))
1676 {
1677 gimplify_vla_decl (decl, seq_p);
1678 is_vla = true;
1679 }
1680
1681 if (asan_poisoned_variables
1682 && !is_vla
1683 && TREE_ADDRESSABLE (decl)
1684 && !TREE_STATIC (decl)
1685 && !DECL_HAS_VALUE_EXPR_P (decl)
1686 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1687 && dbg_cnt (asan_use_after_scope)
1688 && !gimplify_omp_ctxp)
1689 {
1690 asan_poisoned_variables->add (decl);
1691 asan_poison_variable (decl, false, seq_p);
1692 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1693 gimplify_ctxp->live_switch_vars->add (decl);
1694 }
1695
1696 /* Some front ends do not explicitly declare all anonymous
1697 artificial variables. We compensate here by declaring the
1698 variables, though it would be better if the front ends would
1699 explicitly declare them. */
1700 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1701 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1702 gimple_add_tmp_var (decl);
1703
1704 if (init && init != error_mark_node)
1705 {
1706 if (!TREE_STATIC (decl))
1707 {
1708 DECL_INITIAL (decl) = NULL_TREE;
1709 init = build2 (INIT_EXPR, void_type_node, decl, init);
1710 gimplify_and_add (init, seq_p);
1711 ggc_free (init);
1712 }
1713 else
1714 /* We must still examine initializers for static variables
1715 as they may contain a label address. */
1716 walk_tree (&init, force_labels_r, NULL, NULL);
1717 }
1718 }
1719
1720 return GS_ALL_DONE;
1721 }
1722
1723 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1724 and replacing the LOOP_EXPR with goto, but if the loop contains an
1725 EXIT_EXPR, we need to append a label for it to jump to. */
1726
1727 static enum gimplify_status
1728 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1729 {
1730 tree saved_label = gimplify_ctxp->exit_label;
1731 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1732
1733 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1734
1735 gimplify_ctxp->exit_label = NULL_TREE;
1736
1737 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1738
1739 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1740
1741 if (gimplify_ctxp->exit_label)
1742 gimplify_seq_add_stmt (pre_p,
1743 gimple_build_label (gimplify_ctxp->exit_label));
1744
1745 gimplify_ctxp->exit_label = saved_label;
1746
1747 *expr_p = NULL;
1748 return GS_ALL_DONE;
1749 }
1750
1751 /* Gimplify a statement list onto a sequence. These may be created either
1752 by an enlightened front-end, or by shortcut_cond_expr. */
1753
1754 static enum gimplify_status
1755 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1756 {
1757 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1758
1759 tree_stmt_iterator i = tsi_start (*expr_p);
1760
1761 while (!tsi_end_p (i))
1762 {
1763 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1764 tsi_delink (&i);
1765 }
1766
1767 if (temp)
1768 {
1769 *expr_p = temp;
1770 return GS_OK;
1771 }
1772
1773 return GS_ALL_DONE;
1774 }
1775
1776 /* Callback for walk_gimple_seq. */
1777
1778 static tree
1779 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1780 struct walk_stmt_info *wi)
1781 {
1782 gimple *stmt = gsi_stmt (*gsi_p);
1783
1784 *handled_ops_p = true;
1785 switch (gimple_code (stmt))
1786 {
1787 case GIMPLE_TRY:
1788 /* A compiler-generated cleanup or a user-written try block.
1789 If it's empty, don't dive into it--that would result in
1790 worse location info. */
1791 if (gimple_try_eval (stmt) == NULL)
1792 {
1793 wi->info = stmt;
1794 return integer_zero_node;
1795 }
1796 /* Fall through. */
1797 case GIMPLE_BIND:
1798 case GIMPLE_CATCH:
1799 case GIMPLE_EH_FILTER:
1800 case GIMPLE_TRANSACTION:
1801 /* Walk the sub-statements. */
1802 *handled_ops_p = false;
1803 break;
1804
1805 case GIMPLE_DEBUG:
1806 /* Ignore these. We may generate them before declarations that
1807 are never executed. If there's something to warn about,
1808 there will be non-debug stmts too, and we'll catch those. */
1809 break;
1810
1811 case GIMPLE_CALL:
1812 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1813 {
1814 *handled_ops_p = false;
1815 break;
1816 }
1817 /* Fall through. */
1818 default:
1819 /* Save the first "real" statement (not a decl/lexical scope/...). */
1820 wi->info = stmt;
1821 return integer_zero_node;
1822 }
1823 return NULL_TREE;
1824 }
1825
1826 /* Possibly warn about unreachable statements between switch's controlling
1827 expression and the first case. SEQ is the body of a switch expression. */
1828
1829 static void
1830 maybe_warn_switch_unreachable (gimple_seq seq)
1831 {
1832 if (!warn_switch_unreachable
1833 /* This warning doesn't play well with Fortran when optimizations
1834 are on. */
1835 || lang_GNU_Fortran ()
1836 || seq == NULL)
1837 return;
1838
1839 struct walk_stmt_info wi;
1840 memset (&wi, 0, sizeof (wi));
1841 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1842 gimple *stmt = (gimple *) wi.info;
1843
1844 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1845 {
1846 if (gimple_code (stmt) == GIMPLE_GOTO
1847 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1848 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1849 /* Don't warn for compiler-generated gotos. These occur
1850 in Duff's devices, for example. */;
1851 else
1852 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1853 "statement will never be executed");
1854 }
1855 }
1856
1857
1858 /* A label entry that pairs label and a location. */
1859 struct label_entry
1860 {
1861 tree label;
1862 location_t loc;
1863 };
1864
1865 /* Find LABEL in vector of label entries VEC. */
1866
1867 static struct label_entry *
1868 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1869 {
1870 unsigned int i;
1871 struct label_entry *l;
1872
1873 FOR_EACH_VEC_ELT (*vec, i, l)
1874 if (l->label == label)
1875 return l;
1876 return NULL;
1877 }
1878
1879 /* Return true if LABEL, a LABEL_DECL, represents a case label
1880 in a vector of labels CASES. */
1881
1882 static bool
1883 case_label_p (const vec<tree> *cases, tree label)
1884 {
1885 unsigned int i;
1886 tree l;
1887
1888 FOR_EACH_VEC_ELT (*cases, i, l)
1889 if (CASE_LABEL (l) == label)
1890 return true;
1891 return false;
1892 }
1893
1894 /* Find the last nondebug statement in a scope STMT. */
1895
1896 static gimple *
1897 last_stmt_in_scope (gimple *stmt)
1898 {
1899 if (!stmt)
1900 return NULL;
1901
1902 switch (gimple_code (stmt))
1903 {
1904 case GIMPLE_BIND:
1905 {
1906 gbind *bind = as_a <gbind *> (stmt);
1907 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1908 return last_stmt_in_scope (stmt);
1909 }
1910
1911 case GIMPLE_TRY:
1912 {
1913 gtry *try_stmt = as_a <gtry *> (stmt);
1914 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1915 gimple *last_eval = last_stmt_in_scope (stmt);
1916 if (gimple_stmt_may_fallthru (last_eval)
1917 && (last_eval == NULL
1918 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1919 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1920 {
1921 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1922 return last_stmt_in_scope (stmt);
1923 }
1924 else
1925 return last_eval;
1926 }
1927
1928 case GIMPLE_DEBUG:
1929 gcc_unreachable ();
1930
1931 default:
1932 return stmt;
1933 }
1934 }
1935
1936 /* Collect interesting labels in LABELS and return the statement preceding
1937 another case label, or a user-defined label. */
1938
1939 static gimple *
1940 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1941 auto_vec <struct label_entry> *labels)
1942 {
1943 gimple *prev = NULL;
1944
1945 do
1946 {
1947 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1948 {
1949 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1950 which starts on a GIMPLE_SWITCH and ends with a break label.
1951 Handle that as a single statement that can fall through. */
1952 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1953 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1954 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1955 if (last
1956 && gimple_code (first) == GIMPLE_SWITCH
1957 && gimple_code (last) == GIMPLE_LABEL)
1958 {
1959 tree label = gimple_label_label (as_a <glabel *> (last));
1960 if (SWITCH_BREAK_LABEL_P (label))
1961 {
1962 prev = bind;
1963 gsi_next (gsi_p);
1964 continue;
1965 }
1966 }
1967 }
1968 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1969 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1970 {
1971 /* Nested scope. Only look at the last statement of
1972 the innermost scope. */
1973 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1974 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1975 if (last)
1976 {
1977 prev = last;
1978 /* It might be a label without a location. Use the
1979 location of the scope then. */
1980 if (!gimple_has_location (prev))
1981 gimple_set_location (prev, bind_loc);
1982 }
1983 gsi_next (gsi_p);
1984 continue;
1985 }
1986
1987 /* Ifs are tricky. */
1988 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1989 {
1990 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1991 tree false_lab = gimple_cond_false_label (cond_stmt);
1992 location_t if_loc = gimple_location (cond_stmt);
1993
1994 /* If we have e.g.
1995 if (i > 1) goto <D.2259>; else goto D;
1996 we can't do much with the else-branch. */
1997 if (!DECL_ARTIFICIAL (false_lab))
1998 break;
1999
2000 /* Go on until the false label, then one step back. */
2001 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2002 {
2003 gimple *stmt = gsi_stmt (*gsi_p);
2004 if (gimple_code (stmt) == GIMPLE_LABEL
2005 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2006 break;
2007 }
2008
2009 /* Not found? Oops. */
2010 if (gsi_end_p (*gsi_p))
2011 break;
2012
2013 struct label_entry l = { false_lab, if_loc };
2014 labels->safe_push (l);
2015
2016 /* Go to the last statement of the then branch. */
2017 gsi_prev (gsi_p);
2018
2019 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2020 <D.1759>:
2021 <stmt>;
2022 goto <D.1761>;
2023 <D.1760>:
2024 */
2025 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2026 && !gimple_has_location (gsi_stmt (*gsi_p)))
2027 {
2028 /* Look at the statement before, it might be
2029 attribute fallthrough, in which case don't warn. */
2030 gsi_prev (gsi_p);
2031 bool fallthru_before_dest
2032 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2033 gsi_next (gsi_p);
2034 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2035 if (!fallthru_before_dest)
2036 {
2037 struct label_entry l = { goto_dest, if_loc };
2038 labels->safe_push (l);
2039 }
2040 }
2041 /* And move back. */
2042 gsi_next (gsi_p);
2043 }
2044
2045 /* Remember the last statement. Skip labels that are of no interest
2046 to us. */
2047 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2048 {
2049 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2050 if (find_label_entry (labels, label))
2051 prev = gsi_stmt (*gsi_p);
2052 }
2053 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2054 ;
2055 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2056 prev = gsi_stmt (*gsi_p);
2057 gsi_next (gsi_p);
2058 }
2059 while (!gsi_end_p (*gsi_p)
2060 /* Stop if we find a case or a user-defined label. */
2061 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2062 || !gimple_has_location (gsi_stmt (*gsi_p))));
2063
2064 return prev;
2065 }
2066
2067 /* Return true if the switch fallthough warning should occur. LABEL is
2068 the label statement that we're falling through to. */
2069
2070 static bool
2071 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2072 {
2073 gimple_stmt_iterator gsi = *gsi_p;
2074
2075 /* Don't warn if the label is marked with a "falls through" comment. */
2076 if (FALLTHROUGH_LABEL_P (label))
2077 return false;
2078
2079 /* Don't warn for non-case labels followed by a statement:
2080 case 0:
2081 foo ();
2082 label:
2083 bar ();
2084 as these are likely intentional. */
2085 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2086 {
2087 tree l;
2088 while (!gsi_end_p (gsi)
2089 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2090 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2091 && !case_label_p (&gimplify_ctxp->case_labels, l))
2092 gsi_next_nondebug (&gsi);
2093 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2094 return false;
2095 }
2096
2097 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2098 immediately breaks. */
2099 gsi = *gsi_p;
2100
2101 /* Skip all immediately following labels. */
2102 while (!gsi_end_p (gsi)
2103 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2104 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2105 gsi_next_nondebug (&gsi);
2106
2107 /* { ... something; default:; } */
2108 if (gsi_end_p (gsi)
2109 /* { ... something; default: break; } or
2110 { ... something; default: goto L; } */
2111 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2112 /* { ... something; default: return; } */
2113 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2114 return false;
2115
2116 return true;
2117 }
2118
2119 /* Callback for walk_gimple_seq. */
2120
2121 static tree
2122 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2123 struct walk_stmt_info *)
2124 {
2125 gimple *stmt = gsi_stmt (*gsi_p);
2126
2127 *handled_ops_p = true;
2128 switch (gimple_code (stmt))
2129 {
2130 case GIMPLE_TRY:
2131 case GIMPLE_BIND:
2132 case GIMPLE_CATCH:
2133 case GIMPLE_EH_FILTER:
2134 case GIMPLE_TRANSACTION:
2135 /* Walk the sub-statements. */
2136 *handled_ops_p = false;
2137 break;
2138
2139 /* Find a sequence of form:
2140
2141 GIMPLE_LABEL
2142 [...]
2143 <may fallthru stmt>
2144 GIMPLE_LABEL
2145
2146 and possibly warn. */
2147 case GIMPLE_LABEL:
2148 {
2149 /* Found a label. Skip all immediately following labels. */
2150 while (!gsi_end_p (*gsi_p)
2151 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2152 gsi_next_nondebug (gsi_p);
2153
2154 /* There might be no more statements. */
2155 if (gsi_end_p (*gsi_p))
2156 return integer_zero_node;
2157
2158 /* Vector of labels that fall through. */
2159 auto_vec <struct label_entry> labels;
2160 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2161
2162 /* There might be no more statements. */
2163 if (gsi_end_p (*gsi_p))
2164 return integer_zero_node;
2165
2166 gimple *next = gsi_stmt (*gsi_p);
2167 tree label;
2168 /* If what follows is a label, then we may have a fallthrough. */
2169 if (gimple_code (next) == GIMPLE_LABEL
2170 && gimple_has_location (next)
2171 && (label = gimple_label_label (as_a <glabel *> (next)))
2172 && prev != NULL)
2173 {
2174 struct label_entry *l;
2175 bool warned_p = false;
2176 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2177 /* Quiet. */;
2178 else if (gimple_code (prev) == GIMPLE_LABEL
2179 && (label = gimple_label_label (as_a <glabel *> (prev)))
2180 && (l = find_label_entry (&labels, label)))
2181 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2182 "this statement may fall through");
2183 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2184 /* Try to be clever and don't warn when the statement
2185 can't actually fall through. */
2186 && gimple_stmt_may_fallthru (prev)
2187 && gimple_has_location (prev))
2188 warned_p = warning_at (gimple_location (prev),
2189 OPT_Wimplicit_fallthrough_,
2190 "this statement may fall through");
2191 if (warned_p)
2192 inform (gimple_location (next), "here");
2193
2194 /* Mark this label as processed so as to prevent multiple
2195 warnings in nested switches. */
2196 FALLTHROUGH_LABEL_P (label) = true;
2197
2198 /* So that next warn_implicit_fallthrough_r will start looking for
2199 a new sequence starting with this label. */
2200 gsi_prev (gsi_p);
2201 }
2202 }
2203 break;
2204 default:
2205 break;
2206 }
2207 return NULL_TREE;
2208 }
2209
2210 /* Warn when a switch case falls through. */
2211
2212 static void
2213 maybe_warn_implicit_fallthrough (gimple_seq seq)
2214 {
2215 if (!warn_implicit_fallthrough)
2216 return;
2217
2218 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2219 if (!(lang_GNU_C ()
2220 || lang_GNU_CXX ()
2221 || lang_GNU_OBJC ()))
2222 return;
2223
2224 struct walk_stmt_info wi;
2225 memset (&wi, 0, sizeof (wi));
2226 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2227 }
2228
2229 /* Callback for walk_gimple_seq. */
2230
2231 static tree
2232 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2233 struct walk_stmt_info *)
2234 {
2235 gimple *stmt = gsi_stmt (*gsi_p);
2236
2237 *handled_ops_p = true;
2238 switch (gimple_code (stmt))
2239 {
2240 case GIMPLE_TRY:
2241 case GIMPLE_BIND:
2242 case GIMPLE_CATCH:
2243 case GIMPLE_EH_FILTER:
2244 case GIMPLE_TRANSACTION:
2245 /* Walk the sub-statements. */
2246 *handled_ops_p = false;
2247 break;
2248 case GIMPLE_CALL:
2249 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2250 {
2251 gsi_remove (gsi_p, true);
2252 if (gsi_end_p (*gsi_p))
2253 return integer_zero_node;
2254
2255 bool found = false;
2256 location_t loc = gimple_location (stmt);
2257
2258 gimple_stmt_iterator gsi2 = *gsi_p;
2259 stmt = gsi_stmt (gsi2);
2260 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2261 {
2262 /* Go on until the artificial label. */
2263 tree goto_dest = gimple_goto_dest (stmt);
2264 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2265 {
2266 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2267 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2268 == goto_dest)
2269 break;
2270 }
2271
2272 /* Not found? Stop. */
2273 if (gsi_end_p (gsi2))
2274 break;
2275
2276 /* Look one past it. */
2277 gsi_next (&gsi2);
2278 }
2279
2280 /* We're looking for a case label or default label here. */
2281 while (!gsi_end_p (gsi2))
2282 {
2283 stmt = gsi_stmt (gsi2);
2284 if (gimple_code (stmt) == GIMPLE_LABEL)
2285 {
2286 tree label = gimple_label_label (as_a <glabel *> (stmt));
2287 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2288 {
2289 found = true;
2290 break;
2291 }
2292 }
2293 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2294 ;
2295 else if (!is_gimple_debug (stmt))
2296 /* Anything else is not expected. */
2297 break;
2298 gsi_next (&gsi2);
2299 }
2300 if (!found)
2301 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2302 "a case label or default label");
2303 }
2304 break;
2305 default:
2306 break;
2307 }
2308 return NULL_TREE;
2309 }
2310
2311 /* Expand all FALLTHROUGH () calls in SEQ. */
2312
2313 static void
2314 expand_FALLTHROUGH (gimple_seq *seq_p)
2315 {
2316 struct walk_stmt_info wi;
2317 memset (&wi, 0, sizeof (wi));
2318 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2319 }
2320
2321 \f
2322 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2323 branch to. */
2324
2325 static enum gimplify_status
2326 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2327 {
2328 tree switch_expr = *expr_p;
2329 gimple_seq switch_body_seq = NULL;
2330 enum gimplify_status ret;
2331 tree index_type = TREE_TYPE (switch_expr);
2332 if (index_type == NULL_TREE)
2333 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2334
2335 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2336 fb_rvalue);
2337 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2338 return ret;
2339
2340 if (SWITCH_BODY (switch_expr))
2341 {
2342 vec<tree> labels;
2343 vec<tree> saved_labels;
2344 hash_set<tree> *saved_live_switch_vars = NULL;
2345 tree default_case = NULL_TREE;
2346 gswitch *switch_stmt;
2347
2348 /* Save old labels, get new ones from body, then restore the old
2349 labels. Save all the things from the switch body to append after. */
2350 saved_labels = gimplify_ctxp->case_labels;
2351 gimplify_ctxp->case_labels.create (8);
2352
2353 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2354 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2355 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2356 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2357 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2358 else
2359 gimplify_ctxp->live_switch_vars = NULL;
2360
2361 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2362 gimplify_ctxp->in_switch_expr = true;
2363
2364 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2365
2366 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2367 maybe_warn_switch_unreachable (switch_body_seq);
2368 maybe_warn_implicit_fallthrough (switch_body_seq);
2369 /* Only do this for the outermost GIMPLE_SWITCH. */
2370 if (!gimplify_ctxp->in_switch_expr)
2371 expand_FALLTHROUGH (&switch_body_seq);
2372
2373 labels = gimplify_ctxp->case_labels;
2374 gimplify_ctxp->case_labels = saved_labels;
2375
2376 if (gimplify_ctxp->live_switch_vars)
2377 {
2378 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2379 delete gimplify_ctxp->live_switch_vars;
2380 }
2381 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2382
2383 preprocess_case_label_vec_for_gimple (labels, index_type,
2384 &default_case);
2385
2386 bool add_bind = false;
2387 if (!default_case)
2388 {
2389 glabel *new_default;
2390
2391 default_case
2392 = build_case_label (NULL_TREE, NULL_TREE,
2393 create_artificial_label (UNKNOWN_LOCATION));
2394 if (old_in_switch_expr)
2395 {
2396 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2397 add_bind = true;
2398 }
2399 new_default = gimple_build_label (CASE_LABEL (default_case));
2400 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2401 }
2402 else if (old_in_switch_expr)
2403 {
2404 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2405 if (last && gimple_code (last) == GIMPLE_LABEL)
2406 {
2407 tree label = gimple_label_label (as_a <glabel *> (last));
2408 if (SWITCH_BREAK_LABEL_P (label))
2409 add_bind = true;
2410 }
2411 }
2412
2413 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2414 default_case, labels);
2415 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2416 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2417 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2418 so that we can easily find the start and end of the switch
2419 statement. */
2420 if (add_bind)
2421 {
2422 gimple_seq bind_body = NULL;
2423 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2424 gimple_seq_add_seq (&bind_body, switch_body_seq);
2425 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2426 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2427 gimplify_seq_add_stmt (pre_p, bind);
2428 }
2429 else
2430 {
2431 gimplify_seq_add_stmt (pre_p, switch_stmt);
2432 gimplify_seq_add_seq (pre_p, switch_body_seq);
2433 }
2434 labels.release ();
2435 }
2436 else
2437 gcc_unreachable ();
2438
2439 return GS_ALL_DONE;
2440 }
2441
2442 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2443
2444 static enum gimplify_status
2445 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2446 {
2447 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2448 == current_function_decl);
2449
2450 tree label = LABEL_EXPR_LABEL (*expr_p);
2451 glabel *label_stmt = gimple_build_label (label);
2452 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2453 gimplify_seq_add_stmt (pre_p, label_stmt);
2454
2455 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2456 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2457 NOT_TAKEN));
2458 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2459 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2460 TAKEN));
2461
2462 return GS_ALL_DONE;
2463 }
2464
2465 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2466
2467 static enum gimplify_status
2468 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2469 {
2470 struct gimplify_ctx *ctxp;
2471 glabel *label_stmt;
2472
2473 /* Invalid programs can play Duff's Device type games with, for example,
2474 #pragma omp parallel. At least in the C front end, we don't
2475 detect such invalid branches until after gimplification, in the
2476 diagnose_omp_blocks pass. */
2477 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2478 if (ctxp->case_labels.exists ())
2479 break;
2480
2481 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2482 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2483 ctxp->case_labels.safe_push (*expr_p);
2484 gimplify_seq_add_stmt (pre_p, label_stmt);
2485
2486 return GS_ALL_DONE;
2487 }
2488
2489 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2490 if necessary. */
2491
2492 tree
2493 build_and_jump (tree *label_p)
2494 {
2495 if (label_p == NULL)
2496 /* If there's nowhere to jump, just fall through. */
2497 return NULL_TREE;
2498
2499 if (*label_p == NULL_TREE)
2500 {
2501 tree label = create_artificial_label (UNKNOWN_LOCATION);
2502 *label_p = label;
2503 }
2504
2505 return build1 (GOTO_EXPR, void_type_node, *label_p);
2506 }
2507
2508 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2509 This also involves building a label to jump to and communicating it to
2510 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2511
2512 static enum gimplify_status
2513 gimplify_exit_expr (tree *expr_p)
2514 {
2515 tree cond = TREE_OPERAND (*expr_p, 0);
2516 tree expr;
2517
2518 expr = build_and_jump (&gimplify_ctxp->exit_label);
2519 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2520 *expr_p = expr;
2521
2522 return GS_OK;
2523 }
2524
2525 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2526 different from its canonical type, wrap the whole thing inside a
2527 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2528 type.
2529
2530 The canonical type of a COMPONENT_REF is the type of the field being
2531 referenced--unless the field is a bit-field which can be read directly
2532 in a smaller mode, in which case the canonical type is the
2533 sign-appropriate type corresponding to that mode. */
2534
2535 static void
2536 canonicalize_component_ref (tree *expr_p)
2537 {
2538 tree expr = *expr_p;
2539 tree type;
2540
2541 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2542
2543 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2544 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2545 else
2546 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2547
2548 /* One could argue that all the stuff below is not necessary for
2549 the non-bitfield case and declare it a FE error if type
2550 adjustment would be needed. */
2551 if (TREE_TYPE (expr) != type)
2552 {
2553 #ifdef ENABLE_TYPES_CHECKING
2554 tree old_type = TREE_TYPE (expr);
2555 #endif
2556 int type_quals;
2557
2558 /* We need to preserve qualifiers and propagate them from
2559 operand 0. */
2560 type_quals = TYPE_QUALS (type)
2561 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2562 if (TYPE_QUALS (type) != type_quals)
2563 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2564
2565 /* Set the type of the COMPONENT_REF to the underlying type. */
2566 TREE_TYPE (expr) = type;
2567
2568 #ifdef ENABLE_TYPES_CHECKING
2569 /* It is now a FE error, if the conversion from the canonical
2570 type to the original expression type is not useless. */
2571 gcc_assert (useless_type_conversion_p (old_type, type));
2572 #endif
2573 }
2574 }
2575
2576 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2577 to foo, embed that change in the ADDR_EXPR by converting
2578 T array[U];
2579 (T *)&array
2580 ==>
2581 &array[L]
2582 where L is the lower bound. For simplicity, only do this for constant
2583 lower bound.
2584 The constraint is that the type of &array[L] is trivially convertible
2585 to T *. */
2586
2587 static void
2588 canonicalize_addr_expr (tree *expr_p)
2589 {
2590 tree expr = *expr_p;
2591 tree addr_expr = TREE_OPERAND (expr, 0);
2592 tree datype, ddatype, pddatype;
2593
2594 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2595 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2596 || TREE_CODE (addr_expr) != ADDR_EXPR)
2597 return;
2598
2599 /* The addr_expr type should be a pointer to an array. */
2600 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2601 if (TREE_CODE (datype) != ARRAY_TYPE)
2602 return;
2603
2604 /* The pointer to element type shall be trivially convertible to
2605 the expression pointer type. */
2606 ddatype = TREE_TYPE (datype);
2607 pddatype = build_pointer_type (ddatype);
2608 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2609 pddatype))
2610 return;
2611
2612 /* The lower bound and element sizes must be constant. */
2613 if (!TYPE_SIZE_UNIT (ddatype)
2614 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2615 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2616 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2617 return;
2618
2619 /* All checks succeeded. Build a new node to merge the cast. */
2620 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2621 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2622 NULL_TREE, NULL_TREE);
2623 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2624
2625 /* We can have stripped a required restrict qualifier above. */
2626 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2627 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2628 }
2629
2630 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2631 underneath as appropriate. */
2632
2633 static enum gimplify_status
2634 gimplify_conversion (tree *expr_p)
2635 {
2636 location_t loc = EXPR_LOCATION (*expr_p);
2637 gcc_assert (CONVERT_EXPR_P (*expr_p));
2638
2639 /* Then strip away all but the outermost conversion. */
2640 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2641
2642 /* And remove the outermost conversion if it's useless. */
2643 if (tree_ssa_useless_type_conversion (*expr_p))
2644 *expr_p = TREE_OPERAND (*expr_p, 0);
2645
2646 /* If we still have a conversion at the toplevel,
2647 then canonicalize some constructs. */
2648 if (CONVERT_EXPR_P (*expr_p))
2649 {
2650 tree sub = TREE_OPERAND (*expr_p, 0);
2651
2652 /* If a NOP conversion is changing the type of a COMPONENT_REF
2653 expression, then canonicalize its type now in order to expose more
2654 redundant conversions. */
2655 if (TREE_CODE (sub) == COMPONENT_REF)
2656 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2657
2658 /* If a NOP conversion is changing a pointer to array of foo
2659 to a pointer to foo, embed that change in the ADDR_EXPR. */
2660 else if (TREE_CODE (sub) == ADDR_EXPR)
2661 canonicalize_addr_expr (expr_p);
2662 }
2663
2664 /* If we have a conversion to a non-register type force the
2665 use of a VIEW_CONVERT_EXPR instead. */
2666 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2667 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2668 TREE_OPERAND (*expr_p, 0));
2669
2670 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2671 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2672 TREE_SET_CODE (*expr_p, NOP_EXPR);
2673
2674 return GS_OK;
2675 }
2676
2677 /* Nonlocal VLAs seen in the current function. */
2678 static hash_set<tree> *nonlocal_vlas;
2679
2680 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2681 static tree nonlocal_vla_vars;
2682
2683 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2684 DECL_VALUE_EXPR, and it's worth re-examining things. */
2685
2686 static enum gimplify_status
2687 gimplify_var_or_parm_decl (tree *expr_p)
2688 {
2689 tree decl = *expr_p;
2690
2691 /* ??? If this is a local variable, and it has not been seen in any
2692 outer BIND_EXPR, then it's probably the result of a duplicate
2693 declaration, for which we've already issued an error. It would
2694 be really nice if the front end wouldn't leak these at all.
2695 Currently the only known culprit is C++ destructors, as seen
2696 in g++.old-deja/g++.jason/binding.C. */
2697 if (VAR_P (decl)
2698 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2699 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2700 && decl_function_context (decl) == current_function_decl)
2701 {
2702 gcc_assert (seen_error ());
2703 return GS_ERROR;
2704 }
2705
2706 /* When within an OMP context, notice uses of variables. */
2707 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2708 return GS_ALL_DONE;
2709
2710 /* If the decl is an alias for another expression, substitute it now. */
2711 if (DECL_HAS_VALUE_EXPR_P (decl))
2712 {
2713 tree value_expr = DECL_VALUE_EXPR (decl);
2714
2715 /* For referenced nonlocal VLAs add a decl for debugging purposes
2716 to the current function. */
2717 if (VAR_P (decl)
2718 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2719 && nonlocal_vlas != NULL
2720 && TREE_CODE (value_expr) == INDIRECT_REF
2721 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2722 && decl_function_context (decl) != current_function_decl)
2723 {
2724 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2725 while (ctx
2726 && (ctx->region_type == ORT_WORKSHARE
2727 || ctx->region_type == ORT_SIMD
2728 || ctx->region_type == ORT_ACC))
2729 ctx = ctx->outer_context;
2730 if (!ctx && !nonlocal_vlas->add (decl))
2731 {
2732 tree copy = copy_node (decl);
2733
2734 lang_hooks.dup_lang_specific_decl (copy);
2735 SET_DECL_RTL (copy, 0);
2736 TREE_USED (copy) = 1;
2737 DECL_CHAIN (copy) = nonlocal_vla_vars;
2738 nonlocal_vla_vars = copy;
2739 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2740 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2741 }
2742 }
2743
2744 *expr_p = unshare_expr (value_expr);
2745 return GS_OK;
2746 }
2747
2748 return GS_ALL_DONE;
2749 }
2750
2751 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2752
2753 static void
2754 recalculate_side_effects (tree t)
2755 {
2756 enum tree_code code = TREE_CODE (t);
2757 int len = TREE_OPERAND_LENGTH (t);
2758 int i;
2759
2760 switch (TREE_CODE_CLASS (code))
2761 {
2762 case tcc_expression:
2763 switch (code)
2764 {
2765 case INIT_EXPR:
2766 case MODIFY_EXPR:
2767 case VA_ARG_EXPR:
2768 case PREDECREMENT_EXPR:
2769 case PREINCREMENT_EXPR:
2770 case POSTDECREMENT_EXPR:
2771 case POSTINCREMENT_EXPR:
2772 /* All of these have side-effects, no matter what their
2773 operands are. */
2774 return;
2775
2776 default:
2777 break;
2778 }
2779 /* Fall through. */
2780
2781 case tcc_comparison: /* a comparison expression */
2782 case tcc_unary: /* a unary arithmetic expression */
2783 case tcc_binary: /* a binary arithmetic expression */
2784 case tcc_reference: /* a reference */
2785 case tcc_vl_exp: /* a function call */
2786 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2787 for (i = 0; i < len; ++i)
2788 {
2789 tree op = TREE_OPERAND (t, i);
2790 if (op && TREE_SIDE_EFFECTS (op))
2791 TREE_SIDE_EFFECTS (t) = 1;
2792 }
2793 break;
2794
2795 case tcc_constant:
2796 /* No side-effects. */
2797 return;
2798
2799 default:
2800 gcc_unreachable ();
2801 }
2802 }
2803
2804 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2805 node *EXPR_P.
2806
2807 compound_lval
2808 : min_lval '[' val ']'
2809 | min_lval '.' ID
2810 | compound_lval '[' val ']'
2811 | compound_lval '.' ID
2812
2813 This is not part of the original SIMPLE definition, which separates
2814 array and member references, but it seems reasonable to handle them
2815 together. Also, this way we don't run into problems with union
2816 aliasing; gcc requires that for accesses through a union to alias, the
2817 union reference must be explicit, which was not always the case when we
2818 were splitting up array and member refs.
2819
2820 PRE_P points to the sequence where side effects that must happen before
2821 *EXPR_P should be stored.
2822
2823 POST_P points to the sequence where side effects that must happen after
2824 *EXPR_P should be stored. */
2825
2826 static enum gimplify_status
2827 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2828 fallback_t fallback)
2829 {
2830 tree *p;
2831 enum gimplify_status ret = GS_ALL_DONE, tret;
2832 int i;
2833 location_t loc = EXPR_LOCATION (*expr_p);
2834 tree expr = *expr_p;
2835
2836 /* Create a stack of the subexpressions so later we can walk them in
2837 order from inner to outer. */
2838 auto_vec<tree, 10> expr_stack;
2839
2840 /* We can handle anything that get_inner_reference can deal with. */
2841 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2842 {
2843 restart:
2844 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2845 if (TREE_CODE (*p) == INDIRECT_REF)
2846 *p = fold_indirect_ref_loc (loc, *p);
2847
2848 if (handled_component_p (*p))
2849 ;
2850 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2851 additional COMPONENT_REFs. */
2852 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2853 && gimplify_var_or_parm_decl (p) == GS_OK)
2854 goto restart;
2855 else
2856 break;
2857
2858 expr_stack.safe_push (*p);
2859 }
2860
2861 gcc_assert (expr_stack.length ());
2862
2863 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2864 walked through and P points to the innermost expression.
2865
2866 Java requires that we elaborated nodes in source order. That
2867 means we must gimplify the inner expression followed by each of
2868 the indices, in order. But we can't gimplify the inner
2869 expression until we deal with any variable bounds, sizes, or
2870 positions in order to deal with PLACEHOLDER_EXPRs.
2871
2872 So we do this in three steps. First we deal with the annotations
2873 for any variables in the components, then we gimplify the base,
2874 then we gimplify any indices, from left to right. */
2875 for (i = expr_stack.length () - 1; i >= 0; i--)
2876 {
2877 tree t = expr_stack[i];
2878
2879 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2880 {
2881 /* Gimplify the low bound and element type size and put them into
2882 the ARRAY_REF. If these values are set, they have already been
2883 gimplified. */
2884 if (TREE_OPERAND (t, 2) == NULL_TREE)
2885 {
2886 tree low = unshare_expr (array_ref_low_bound (t));
2887 if (!is_gimple_min_invariant (low))
2888 {
2889 TREE_OPERAND (t, 2) = low;
2890 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2891 post_p, is_gimple_reg,
2892 fb_rvalue);
2893 ret = MIN (ret, tret);
2894 }
2895 }
2896 else
2897 {
2898 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2899 is_gimple_reg, fb_rvalue);
2900 ret = MIN (ret, tret);
2901 }
2902
2903 if (TREE_OPERAND (t, 3) == NULL_TREE)
2904 {
2905 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2906 tree elmt_size = unshare_expr (array_ref_element_size (t));
2907 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2908
2909 /* Divide the element size by the alignment of the element
2910 type (above). */
2911 elmt_size
2912 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2913
2914 if (!is_gimple_min_invariant (elmt_size))
2915 {
2916 TREE_OPERAND (t, 3) = elmt_size;
2917 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2918 post_p, is_gimple_reg,
2919 fb_rvalue);
2920 ret = MIN (ret, tret);
2921 }
2922 }
2923 else
2924 {
2925 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2926 is_gimple_reg, fb_rvalue);
2927 ret = MIN (ret, tret);
2928 }
2929 }
2930 else if (TREE_CODE (t) == COMPONENT_REF)
2931 {
2932 /* Set the field offset into T and gimplify it. */
2933 if (TREE_OPERAND (t, 2) == NULL_TREE)
2934 {
2935 tree offset = unshare_expr (component_ref_field_offset (t));
2936 tree field = TREE_OPERAND (t, 1);
2937 tree factor
2938 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2939
2940 /* Divide the offset by its alignment. */
2941 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2942
2943 if (!is_gimple_min_invariant (offset))
2944 {
2945 TREE_OPERAND (t, 2) = offset;
2946 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2947 post_p, is_gimple_reg,
2948 fb_rvalue);
2949 ret = MIN (ret, tret);
2950 }
2951 }
2952 else
2953 {
2954 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2955 is_gimple_reg, fb_rvalue);
2956 ret = MIN (ret, tret);
2957 }
2958 }
2959 }
2960
2961 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2962 so as to match the min_lval predicate. Failure to do so may result
2963 in the creation of large aggregate temporaries. */
2964 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2965 fallback | fb_lvalue);
2966 ret = MIN (ret, tret);
2967
2968 /* And finally, the indices and operands of ARRAY_REF. During this
2969 loop we also remove any useless conversions. */
2970 for (; expr_stack.length () > 0; )
2971 {
2972 tree t = expr_stack.pop ();
2973
2974 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2975 {
2976 /* Gimplify the dimension. */
2977 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2978 {
2979 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2980 is_gimple_val, fb_rvalue);
2981 ret = MIN (ret, tret);
2982 }
2983 }
2984
2985 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2986
2987 /* The innermost expression P may have originally had
2988 TREE_SIDE_EFFECTS set which would have caused all the outer
2989 expressions in *EXPR_P leading to P to also have had
2990 TREE_SIDE_EFFECTS set. */
2991 recalculate_side_effects (t);
2992 }
2993
2994 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2995 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2996 {
2997 canonicalize_component_ref (expr_p);
2998 }
2999
3000 expr_stack.release ();
3001
3002 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3003
3004 return ret;
3005 }
3006
3007 /* Gimplify the self modifying expression pointed to by EXPR_P
3008 (++, --, +=, -=).
3009
3010 PRE_P points to the list where side effects that must happen before
3011 *EXPR_P should be stored.
3012
3013 POST_P points to the list where side effects that must happen after
3014 *EXPR_P should be stored.
3015
3016 WANT_VALUE is nonzero iff we want to use the value of this expression
3017 in another expression.
3018
3019 ARITH_TYPE is the type the computation should be performed in. */
3020
3021 enum gimplify_status
3022 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3023 bool want_value, tree arith_type)
3024 {
3025 enum tree_code code;
3026 tree lhs, lvalue, rhs, t1;
3027 gimple_seq post = NULL, *orig_post_p = post_p;
3028 bool postfix;
3029 enum tree_code arith_code;
3030 enum gimplify_status ret;
3031 location_t loc = EXPR_LOCATION (*expr_p);
3032
3033 code = TREE_CODE (*expr_p);
3034
3035 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3036 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3037
3038 /* Prefix or postfix? */
3039 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3040 /* Faster to treat as prefix if result is not used. */
3041 postfix = want_value;
3042 else
3043 postfix = false;
3044
3045 /* For postfix, make sure the inner expression's post side effects
3046 are executed after side effects from this expression. */
3047 if (postfix)
3048 post_p = &post;
3049
3050 /* Add or subtract? */
3051 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3052 arith_code = PLUS_EXPR;
3053 else
3054 arith_code = MINUS_EXPR;
3055
3056 /* Gimplify the LHS into a GIMPLE lvalue. */
3057 lvalue = TREE_OPERAND (*expr_p, 0);
3058 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3059 if (ret == GS_ERROR)
3060 return ret;
3061
3062 /* Extract the operands to the arithmetic operation. */
3063 lhs = lvalue;
3064 rhs = TREE_OPERAND (*expr_p, 1);
3065
3066 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3067 that as the result value and in the postqueue operation. */
3068 if (postfix)
3069 {
3070 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3071 if (ret == GS_ERROR)
3072 return ret;
3073
3074 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3075 }
3076
3077 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3078 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3079 {
3080 rhs = convert_to_ptrofftype_loc (loc, rhs);
3081 if (arith_code == MINUS_EXPR)
3082 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3083 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3084 }
3085 else
3086 t1 = fold_convert (TREE_TYPE (*expr_p),
3087 fold_build2 (arith_code, arith_type,
3088 fold_convert (arith_type, lhs),
3089 fold_convert (arith_type, rhs)));
3090
3091 if (postfix)
3092 {
3093 gimplify_assign (lvalue, t1, pre_p);
3094 gimplify_seq_add_seq (orig_post_p, post);
3095 *expr_p = lhs;
3096 return GS_ALL_DONE;
3097 }
3098 else
3099 {
3100 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3101 return GS_OK;
3102 }
3103 }
3104
3105 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3106
3107 static void
3108 maybe_with_size_expr (tree *expr_p)
3109 {
3110 tree expr = *expr_p;
3111 tree type = TREE_TYPE (expr);
3112 tree size;
3113
3114 /* If we've already wrapped this or the type is error_mark_node, we can't do
3115 anything. */
3116 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3117 || type == error_mark_node)
3118 return;
3119
3120 /* If the size isn't known or is a constant, we have nothing to do. */
3121 size = TYPE_SIZE_UNIT (type);
3122 if (!size || poly_int_tree_p (size))
3123 return;
3124
3125 /* Otherwise, make a WITH_SIZE_EXPR. */
3126 size = unshare_expr (size);
3127 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3128 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3129 }
3130
3131 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3132 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3133 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3134 gimplified to an SSA name. */
3135
3136 enum gimplify_status
3137 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3138 bool allow_ssa)
3139 {
3140 bool (*test) (tree);
3141 fallback_t fb;
3142
3143 /* In general, we allow lvalues for function arguments to avoid
3144 extra overhead of copying large aggregates out of even larger
3145 aggregates into temporaries only to copy the temporaries to
3146 the argument list. Make optimizers happy by pulling out to
3147 temporaries those types that fit in registers. */
3148 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3149 test = is_gimple_val, fb = fb_rvalue;
3150 else
3151 {
3152 test = is_gimple_lvalue, fb = fb_either;
3153 /* Also strip a TARGET_EXPR that would force an extra copy. */
3154 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3155 {
3156 tree init = TARGET_EXPR_INITIAL (*arg_p);
3157 if (init
3158 && !VOID_TYPE_P (TREE_TYPE (init)))
3159 *arg_p = init;
3160 }
3161 }
3162
3163 /* If this is a variable sized type, we must remember the size. */
3164 maybe_with_size_expr (arg_p);
3165
3166 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3167 /* Make sure arguments have the same location as the function call
3168 itself. */
3169 protected_set_expr_location (*arg_p, call_location);
3170
3171 /* There is a sequence point before a function call. Side effects in
3172 the argument list must occur before the actual call. So, when
3173 gimplifying arguments, force gimplify_expr to use an internal
3174 post queue which is then appended to the end of PRE_P. */
3175 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3176 }
3177
3178 /* Don't fold inside offloading or taskreg regions: it can break code by
3179 adding decl references that weren't in the source. We'll do it during
3180 omplower pass instead. */
3181
3182 static bool
3183 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3184 {
3185 struct gimplify_omp_ctx *ctx;
3186 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3187 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3188 return false;
3189 return fold_stmt (gsi);
3190 }
3191
3192 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3193 WANT_VALUE is true if the result of the call is desired. */
3194
3195 static enum gimplify_status
3196 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3197 {
3198 tree fndecl, parms, p, fnptrtype;
3199 enum gimplify_status ret;
3200 int i, nargs;
3201 gcall *call;
3202 bool builtin_va_start_p = false;
3203 location_t loc = EXPR_LOCATION (*expr_p);
3204
3205 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3206
3207 /* For reliable diagnostics during inlining, it is necessary that
3208 every call_expr be annotated with file and line. */
3209 if (! EXPR_HAS_LOCATION (*expr_p))
3210 SET_EXPR_LOCATION (*expr_p, input_location);
3211
3212 /* Gimplify internal functions created in the FEs. */
3213 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3214 {
3215 if (want_value)
3216 return GS_ALL_DONE;
3217
3218 nargs = call_expr_nargs (*expr_p);
3219 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3220 auto_vec<tree> vargs (nargs);
3221
3222 for (i = 0; i < nargs; i++)
3223 {
3224 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3225 EXPR_LOCATION (*expr_p));
3226 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3227 }
3228
3229 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3230 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3231 gimplify_seq_add_stmt (pre_p, call);
3232 return GS_ALL_DONE;
3233 }
3234
3235 /* This may be a call to a builtin function.
3236
3237 Builtin function calls may be transformed into different
3238 (and more efficient) builtin function calls under certain
3239 circumstances. Unfortunately, gimplification can muck things
3240 up enough that the builtin expanders are not aware that certain
3241 transformations are still valid.
3242
3243 So we attempt transformation/gimplification of the call before
3244 we gimplify the CALL_EXPR. At this time we do not manage to
3245 transform all calls in the same manner as the expanders do, but
3246 we do transform most of them. */
3247 fndecl = get_callee_fndecl (*expr_p);
3248 if (fndecl
3249 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3250 switch (DECL_FUNCTION_CODE (fndecl))
3251 {
3252 CASE_BUILT_IN_ALLOCA:
3253 /* If the call has been built for a variable-sized object, then we
3254 want to restore the stack level when the enclosing BIND_EXPR is
3255 exited to reclaim the allocated space; otherwise, we precisely
3256 need to do the opposite and preserve the latest stack level. */
3257 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3258 gimplify_ctxp->save_stack = true;
3259 else
3260 gimplify_ctxp->keep_stack = true;
3261 break;
3262
3263 case BUILT_IN_VA_START:
3264 {
3265 builtin_va_start_p = TRUE;
3266 if (call_expr_nargs (*expr_p) < 2)
3267 {
3268 error ("too few arguments to function %<va_start%>");
3269 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3270 return GS_OK;
3271 }
3272
3273 if (fold_builtin_next_arg (*expr_p, true))
3274 {
3275 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3276 return GS_OK;
3277 }
3278 break;
3279 }
3280
3281 default:
3282 ;
3283 }
3284 if (fndecl && DECL_BUILT_IN (fndecl))
3285 {
3286 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3287 if (new_tree && new_tree != *expr_p)
3288 {
3289 /* There was a transformation of this call which computes the
3290 same value, but in a more efficient way. Return and try
3291 again. */
3292 *expr_p = new_tree;
3293 return GS_OK;
3294 }
3295 }
3296
3297 /* Remember the original function pointer type. */
3298 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3299
3300 /* There is a sequence point before the call, so any side effects in
3301 the calling expression must occur before the actual call. Force
3302 gimplify_expr to use an internal post queue. */
3303 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3304 is_gimple_call_addr, fb_rvalue);
3305
3306 nargs = call_expr_nargs (*expr_p);
3307
3308 /* Get argument types for verification. */
3309 fndecl = get_callee_fndecl (*expr_p);
3310 parms = NULL_TREE;
3311 if (fndecl)
3312 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3313 else
3314 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3315
3316 if (fndecl && DECL_ARGUMENTS (fndecl))
3317 p = DECL_ARGUMENTS (fndecl);
3318 else if (parms)
3319 p = parms;
3320 else
3321 p = NULL_TREE;
3322 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3323 ;
3324
3325 /* If the last argument is __builtin_va_arg_pack () and it is not
3326 passed as a named argument, decrease the number of CALL_EXPR
3327 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3328 if (!p
3329 && i < nargs
3330 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3331 {
3332 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3333 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3334
3335 if (last_arg_fndecl
3336 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3337 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3338 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3339 {
3340 tree call = *expr_p;
3341
3342 --nargs;
3343 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3344 CALL_EXPR_FN (call),
3345 nargs, CALL_EXPR_ARGP (call));
3346
3347 /* Copy all CALL_EXPR flags, location and block, except
3348 CALL_EXPR_VA_ARG_PACK flag. */
3349 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3350 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3351 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3352 = CALL_EXPR_RETURN_SLOT_OPT (call);
3353 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3354 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3355
3356 /* Set CALL_EXPR_VA_ARG_PACK. */
3357 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3358 }
3359 }
3360
3361 /* If the call returns twice then after building the CFG the call
3362 argument computations will no longer dominate the call because
3363 we add an abnormal incoming edge to the call. So do not use SSA
3364 vars there. */
3365 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3366
3367 /* Gimplify the function arguments. */
3368 if (nargs > 0)
3369 {
3370 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3371 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3372 PUSH_ARGS_REVERSED ? i-- : i++)
3373 {
3374 enum gimplify_status t;
3375
3376 /* Avoid gimplifying the second argument to va_start, which needs to
3377 be the plain PARM_DECL. */
3378 if ((i != 1) || !builtin_va_start_p)
3379 {
3380 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3381 EXPR_LOCATION (*expr_p), ! returns_twice);
3382
3383 if (t == GS_ERROR)
3384 ret = GS_ERROR;
3385 }
3386 }
3387 }
3388
3389 /* Gimplify the static chain. */
3390 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3391 {
3392 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3393 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3394 else
3395 {
3396 enum gimplify_status t;
3397 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3398 EXPR_LOCATION (*expr_p), ! returns_twice);
3399 if (t == GS_ERROR)
3400 ret = GS_ERROR;
3401 }
3402 }
3403
3404 /* Verify the function result. */
3405 if (want_value && fndecl
3406 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3407 {
3408 error_at (loc, "using result of function returning %<void%>");
3409 ret = GS_ERROR;
3410 }
3411
3412 /* Try this again in case gimplification exposed something. */
3413 if (ret != GS_ERROR)
3414 {
3415 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3416
3417 if (new_tree && new_tree != *expr_p)
3418 {
3419 /* There was a transformation of this call which computes the
3420 same value, but in a more efficient way. Return and try
3421 again. */
3422 *expr_p = new_tree;
3423 return GS_OK;
3424 }
3425 }
3426 else
3427 {
3428 *expr_p = error_mark_node;
3429 return GS_ERROR;
3430 }
3431
3432 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3433 decl. This allows us to eliminate redundant or useless
3434 calls to "const" functions. */
3435 if (TREE_CODE (*expr_p) == CALL_EXPR)
3436 {
3437 int flags = call_expr_flags (*expr_p);
3438 if (flags & (ECF_CONST | ECF_PURE)
3439 /* An infinite loop is considered a side effect. */
3440 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3441 TREE_SIDE_EFFECTS (*expr_p) = 0;
3442 }
3443
3444 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3445 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3446 form and delegate the creation of a GIMPLE_CALL to
3447 gimplify_modify_expr. This is always possible because when
3448 WANT_VALUE is true, the caller wants the result of this call into
3449 a temporary, which means that we will emit an INIT_EXPR in
3450 internal_get_tmp_var which will then be handled by
3451 gimplify_modify_expr. */
3452 if (!want_value)
3453 {
3454 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3455 have to do is replicate it as a GIMPLE_CALL tuple. */
3456 gimple_stmt_iterator gsi;
3457 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3458 notice_special_calls (call);
3459 gimplify_seq_add_stmt (pre_p, call);
3460 gsi = gsi_last (*pre_p);
3461 maybe_fold_stmt (&gsi);
3462 *expr_p = NULL_TREE;
3463 }
3464 else
3465 /* Remember the original function type. */
3466 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3467 CALL_EXPR_FN (*expr_p));
3468
3469 return ret;
3470 }
3471
3472 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3473 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3474
3475 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3476 condition is true or false, respectively. If null, we should generate
3477 our own to skip over the evaluation of this specific expression.
3478
3479 LOCUS is the source location of the COND_EXPR.
3480
3481 This function is the tree equivalent of do_jump.
3482
3483 shortcut_cond_r should only be called by shortcut_cond_expr. */
3484
3485 static tree
3486 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3487 location_t locus)
3488 {
3489 tree local_label = NULL_TREE;
3490 tree t, expr = NULL;
3491
3492 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3493 retain the shortcut semantics. Just insert the gotos here;
3494 shortcut_cond_expr will append the real blocks later. */
3495 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3496 {
3497 location_t new_locus;
3498
3499 /* Turn if (a && b) into
3500
3501 if (a); else goto no;
3502 if (b) goto yes; else goto no;
3503 (no:) */
3504
3505 if (false_label_p == NULL)
3506 false_label_p = &local_label;
3507
3508 /* Keep the original source location on the first 'if'. */
3509 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3510 append_to_statement_list (t, &expr);
3511
3512 /* Set the source location of the && on the second 'if'. */
3513 new_locus = rexpr_location (pred, locus);
3514 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3515 new_locus);
3516 append_to_statement_list (t, &expr);
3517 }
3518 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3519 {
3520 location_t new_locus;
3521
3522 /* Turn if (a || b) into
3523
3524 if (a) goto yes;
3525 if (b) goto yes; else goto no;
3526 (yes:) */
3527
3528 if (true_label_p == NULL)
3529 true_label_p = &local_label;
3530
3531 /* Keep the original source location on the first 'if'. */
3532 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3533 append_to_statement_list (t, &expr);
3534
3535 /* Set the source location of the || on the second 'if'. */
3536 new_locus = rexpr_location (pred, locus);
3537 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3538 new_locus);
3539 append_to_statement_list (t, &expr);
3540 }
3541 else if (TREE_CODE (pred) == COND_EXPR
3542 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3543 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3544 {
3545 location_t new_locus;
3546
3547 /* As long as we're messing with gotos, turn if (a ? b : c) into
3548 if (a)
3549 if (b) goto yes; else goto no;
3550 else
3551 if (c) goto yes; else goto no;
3552
3553 Don't do this if one of the arms has void type, which can happen
3554 in C++ when the arm is throw. */
3555
3556 /* Keep the original source location on the first 'if'. Set the source
3557 location of the ? on the second 'if'. */
3558 new_locus = rexpr_location (pred, locus);
3559 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3560 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3561 false_label_p, locus),
3562 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3563 false_label_p, new_locus));
3564 }
3565 else
3566 {
3567 expr = build3 (COND_EXPR, void_type_node, pred,
3568 build_and_jump (true_label_p),
3569 build_and_jump (false_label_p));
3570 SET_EXPR_LOCATION (expr, locus);
3571 }
3572
3573 if (local_label)
3574 {
3575 t = build1 (LABEL_EXPR, void_type_node, local_label);
3576 append_to_statement_list (t, &expr);
3577 }
3578
3579 return expr;
3580 }
3581
3582 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3583 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3584 statement, if it is the last one. Otherwise, return NULL. */
3585
3586 static tree
3587 find_goto (tree expr)
3588 {
3589 if (!expr)
3590 return NULL_TREE;
3591
3592 if (TREE_CODE (expr) == GOTO_EXPR)
3593 return expr;
3594
3595 if (TREE_CODE (expr) != STATEMENT_LIST)
3596 return NULL_TREE;
3597
3598 tree_stmt_iterator i = tsi_start (expr);
3599
3600 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3601 tsi_next (&i);
3602
3603 if (!tsi_one_before_end_p (i))
3604 return NULL_TREE;
3605
3606 return find_goto (tsi_stmt (i));
3607 }
3608
3609 /* Same as find_goto, except that it returns NULL if the destination
3610 is not a LABEL_DECL. */
3611
3612 static inline tree
3613 find_goto_label (tree expr)
3614 {
3615 tree dest = find_goto (expr);
3616 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3617 return dest;
3618 return NULL_TREE;
3619 }
3620
3621 /* Given a conditional expression EXPR with short-circuit boolean
3622 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3623 predicate apart into the equivalent sequence of conditionals. */
3624
3625 static tree
3626 shortcut_cond_expr (tree expr)
3627 {
3628 tree pred = TREE_OPERAND (expr, 0);
3629 tree then_ = TREE_OPERAND (expr, 1);
3630 tree else_ = TREE_OPERAND (expr, 2);
3631 tree true_label, false_label, end_label, t;
3632 tree *true_label_p;
3633 tree *false_label_p;
3634 bool emit_end, emit_false, jump_over_else;
3635 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3636 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3637
3638 /* First do simple transformations. */
3639 if (!else_se)
3640 {
3641 /* If there is no 'else', turn
3642 if (a && b) then c
3643 into
3644 if (a) if (b) then c. */
3645 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3646 {
3647 /* Keep the original source location on the first 'if'. */
3648 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3649 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3650 /* Set the source location of the && on the second 'if'. */
3651 if (rexpr_has_location (pred))
3652 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3653 then_ = shortcut_cond_expr (expr);
3654 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3655 pred = TREE_OPERAND (pred, 0);
3656 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3657 SET_EXPR_LOCATION (expr, locus);
3658 }
3659 }
3660
3661 if (!then_se)
3662 {
3663 /* If there is no 'then', turn
3664 if (a || b); else d
3665 into
3666 if (a); else if (b); else d. */
3667 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3668 {
3669 /* Keep the original source location on the first 'if'. */
3670 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3671 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3672 /* Set the source location of the || on the second 'if'. */
3673 if (rexpr_has_location (pred))
3674 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3675 else_ = shortcut_cond_expr (expr);
3676 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3677 pred = TREE_OPERAND (pred, 0);
3678 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3679 SET_EXPR_LOCATION (expr, locus);
3680 }
3681 }
3682
3683 /* If we're done, great. */
3684 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3685 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3686 return expr;
3687
3688 /* Otherwise we need to mess with gotos. Change
3689 if (a) c; else d;
3690 to
3691 if (a); else goto no;
3692 c; goto end;
3693 no: d; end:
3694 and recursively gimplify the condition. */
3695
3696 true_label = false_label = end_label = NULL_TREE;
3697
3698 /* If our arms just jump somewhere, hijack those labels so we don't
3699 generate jumps to jumps. */
3700
3701 if (tree then_goto = find_goto_label (then_))
3702 {
3703 true_label = GOTO_DESTINATION (then_goto);
3704 then_ = NULL;
3705 then_se = false;
3706 }
3707
3708 if (tree else_goto = find_goto_label (else_))
3709 {
3710 false_label = GOTO_DESTINATION (else_goto);
3711 else_ = NULL;
3712 else_se = false;
3713 }
3714
3715 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3716 if (true_label)
3717 true_label_p = &true_label;
3718 else
3719 true_label_p = NULL;
3720
3721 /* The 'else' branch also needs a label if it contains interesting code. */
3722 if (false_label || else_se)
3723 false_label_p = &false_label;
3724 else
3725 false_label_p = NULL;
3726
3727 /* If there was nothing else in our arms, just forward the label(s). */
3728 if (!then_se && !else_se)
3729 return shortcut_cond_r (pred, true_label_p, false_label_p,
3730 EXPR_LOC_OR_LOC (expr, input_location));
3731
3732 /* If our last subexpression already has a terminal label, reuse it. */
3733 if (else_se)
3734 t = expr_last (else_);
3735 else if (then_se)
3736 t = expr_last (then_);
3737 else
3738 t = NULL;
3739 if (t && TREE_CODE (t) == LABEL_EXPR)
3740 end_label = LABEL_EXPR_LABEL (t);
3741
3742 /* If we don't care about jumping to the 'else' branch, jump to the end
3743 if the condition is false. */
3744 if (!false_label_p)
3745 false_label_p = &end_label;
3746
3747 /* We only want to emit these labels if we aren't hijacking them. */
3748 emit_end = (end_label == NULL_TREE);
3749 emit_false = (false_label == NULL_TREE);
3750
3751 /* We only emit the jump over the else clause if we have to--if the
3752 then clause may fall through. Otherwise we can wind up with a
3753 useless jump and a useless label at the end of gimplified code,
3754 which will cause us to think that this conditional as a whole
3755 falls through even if it doesn't. If we then inline a function
3756 which ends with such a condition, that can cause us to issue an
3757 inappropriate warning about control reaching the end of a
3758 non-void function. */
3759 jump_over_else = block_may_fallthru (then_);
3760
3761 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3762 EXPR_LOC_OR_LOC (expr, input_location));
3763
3764 expr = NULL;
3765 append_to_statement_list (pred, &expr);
3766
3767 append_to_statement_list (then_, &expr);
3768 if (else_se)
3769 {
3770 if (jump_over_else)
3771 {
3772 tree last = expr_last (expr);
3773 t = build_and_jump (&end_label);
3774 if (rexpr_has_location (last))
3775 SET_EXPR_LOCATION (t, rexpr_location (last));
3776 append_to_statement_list (t, &expr);
3777 }
3778 if (emit_false)
3779 {
3780 t = build1 (LABEL_EXPR, void_type_node, false_label);
3781 append_to_statement_list (t, &expr);
3782 }
3783 append_to_statement_list (else_, &expr);
3784 }
3785 if (emit_end && end_label)
3786 {
3787 t = build1 (LABEL_EXPR, void_type_node, end_label);
3788 append_to_statement_list (t, &expr);
3789 }
3790
3791 return expr;
3792 }
3793
3794 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3795
3796 tree
3797 gimple_boolify (tree expr)
3798 {
3799 tree type = TREE_TYPE (expr);
3800 location_t loc = EXPR_LOCATION (expr);
3801
3802 if (TREE_CODE (expr) == NE_EXPR
3803 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3804 && integer_zerop (TREE_OPERAND (expr, 1)))
3805 {
3806 tree call = TREE_OPERAND (expr, 0);
3807 tree fn = get_callee_fndecl (call);
3808
3809 /* For __builtin_expect ((long) (x), y) recurse into x as well
3810 if x is truth_value_p. */
3811 if (fn
3812 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3813 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3814 && call_expr_nargs (call) == 2)
3815 {
3816 tree arg = CALL_EXPR_ARG (call, 0);
3817 if (arg)
3818 {
3819 if (TREE_CODE (arg) == NOP_EXPR
3820 && TREE_TYPE (arg) == TREE_TYPE (call))
3821 arg = TREE_OPERAND (arg, 0);
3822 if (truth_value_p (TREE_CODE (arg)))
3823 {
3824 arg = gimple_boolify (arg);
3825 CALL_EXPR_ARG (call, 0)
3826 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3827 }
3828 }
3829 }
3830 }
3831
3832 switch (TREE_CODE (expr))
3833 {
3834 case TRUTH_AND_EXPR:
3835 case TRUTH_OR_EXPR:
3836 case TRUTH_XOR_EXPR:
3837 case TRUTH_ANDIF_EXPR:
3838 case TRUTH_ORIF_EXPR:
3839 /* Also boolify the arguments of truth exprs. */
3840 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3841 /* FALLTHRU */
3842
3843 case TRUTH_NOT_EXPR:
3844 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3845
3846 /* These expressions always produce boolean results. */
3847 if (TREE_CODE (type) != BOOLEAN_TYPE)
3848 TREE_TYPE (expr) = boolean_type_node;
3849 return expr;
3850
3851 case ANNOTATE_EXPR:
3852 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3853 {
3854 case annot_expr_ivdep_kind:
3855 case annot_expr_unroll_kind:
3856 case annot_expr_no_vector_kind:
3857 case annot_expr_vector_kind:
3858 case annot_expr_parallel_kind:
3859 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3860 if (TREE_CODE (type) != BOOLEAN_TYPE)
3861 TREE_TYPE (expr) = boolean_type_node;
3862 return expr;
3863 default:
3864 gcc_unreachable ();
3865 }
3866
3867 default:
3868 if (COMPARISON_CLASS_P (expr))
3869 {
3870 /* There expressions always prduce boolean results. */
3871 if (TREE_CODE (type) != BOOLEAN_TYPE)
3872 TREE_TYPE (expr) = boolean_type_node;
3873 return expr;
3874 }
3875 /* Other expressions that get here must have boolean values, but
3876 might need to be converted to the appropriate mode. */
3877 if (TREE_CODE (type) == BOOLEAN_TYPE)
3878 return expr;
3879 return fold_convert_loc (loc, boolean_type_node, expr);
3880 }
3881 }
3882
3883 /* Given a conditional expression *EXPR_P without side effects, gimplify
3884 its operands. New statements are inserted to PRE_P. */
3885
3886 static enum gimplify_status
3887 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3888 {
3889 tree expr = *expr_p, cond;
3890 enum gimplify_status ret, tret;
3891 enum tree_code code;
3892
3893 cond = gimple_boolify (COND_EXPR_COND (expr));
3894
3895 /* We need to handle && and || specially, as their gimplification
3896 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3897 code = TREE_CODE (cond);
3898 if (code == TRUTH_ANDIF_EXPR)
3899 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3900 else if (code == TRUTH_ORIF_EXPR)
3901 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3902 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3903 COND_EXPR_COND (*expr_p) = cond;
3904
3905 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3906 is_gimple_val, fb_rvalue);
3907 ret = MIN (ret, tret);
3908 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3909 is_gimple_val, fb_rvalue);
3910
3911 return MIN (ret, tret);
3912 }
3913
3914 /* Return true if evaluating EXPR could trap.
3915 EXPR is GENERIC, while tree_could_trap_p can be called
3916 only on GIMPLE. */
3917
3918 static bool
3919 generic_expr_could_trap_p (tree expr)
3920 {
3921 unsigned i, n;
3922
3923 if (!expr || is_gimple_val (expr))
3924 return false;
3925
3926 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3927 return true;
3928
3929 n = TREE_OPERAND_LENGTH (expr);
3930 for (i = 0; i < n; i++)
3931 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3932 return true;
3933
3934 return false;
3935 }
3936
3937 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3938 into
3939
3940 if (p) if (p)
3941 t1 = a; a;
3942 else or else
3943 t1 = b; b;
3944 t1;
3945
3946 The second form is used when *EXPR_P is of type void.
3947
3948 PRE_P points to the list where side effects that must happen before
3949 *EXPR_P should be stored. */
3950
3951 static enum gimplify_status
3952 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3953 {
3954 tree expr = *expr_p;
3955 tree type = TREE_TYPE (expr);
3956 location_t loc = EXPR_LOCATION (expr);
3957 tree tmp, arm1, arm2;
3958 enum gimplify_status ret;
3959 tree label_true, label_false, label_cont;
3960 bool have_then_clause_p, have_else_clause_p;
3961 gcond *cond_stmt;
3962 enum tree_code pred_code;
3963 gimple_seq seq = NULL;
3964
3965 /* If this COND_EXPR has a value, copy the values into a temporary within
3966 the arms. */
3967 if (!VOID_TYPE_P (type))
3968 {
3969 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3970 tree result;
3971
3972 /* If either an rvalue is ok or we do not require an lvalue, create the
3973 temporary. But we cannot do that if the type is addressable. */
3974 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3975 && !TREE_ADDRESSABLE (type))
3976 {
3977 if (gimplify_ctxp->allow_rhs_cond_expr
3978 /* If either branch has side effects or could trap, it can't be
3979 evaluated unconditionally. */
3980 && !TREE_SIDE_EFFECTS (then_)
3981 && !generic_expr_could_trap_p (then_)
3982 && !TREE_SIDE_EFFECTS (else_)
3983 && !generic_expr_could_trap_p (else_))
3984 return gimplify_pure_cond_expr (expr_p, pre_p);
3985
3986 tmp = create_tmp_var (type, "iftmp");
3987 result = tmp;
3988 }
3989
3990 /* Otherwise, only create and copy references to the values. */
3991 else
3992 {
3993 type = build_pointer_type (type);
3994
3995 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3996 then_ = build_fold_addr_expr_loc (loc, then_);
3997
3998 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3999 else_ = build_fold_addr_expr_loc (loc, else_);
4000
4001 expr
4002 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4003
4004 tmp = create_tmp_var (type, "iftmp");
4005 result = build_simple_mem_ref_loc (loc, tmp);
4006 }
4007
4008 /* Build the new then clause, `tmp = then_;'. But don't build the
4009 assignment if the value is void; in C++ it can be if it's a throw. */
4010 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4011 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4012
4013 /* Similarly, build the new else clause, `tmp = else_;'. */
4014 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4015 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4016
4017 TREE_TYPE (expr) = void_type_node;
4018 recalculate_side_effects (expr);
4019
4020 /* Move the COND_EXPR to the prequeue. */
4021 gimplify_stmt (&expr, pre_p);
4022
4023 *expr_p = result;
4024 return GS_ALL_DONE;
4025 }
4026
4027 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4028 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4029 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4030 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4031
4032 /* Make sure the condition has BOOLEAN_TYPE. */
4033 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4034
4035 /* Break apart && and || conditions. */
4036 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4037 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4038 {
4039 expr = shortcut_cond_expr (expr);
4040
4041 if (expr != *expr_p)
4042 {
4043 *expr_p = expr;
4044
4045 /* We can't rely on gimplify_expr to re-gimplify the expanded
4046 form properly, as cleanups might cause the target labels to be
4047 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4048 set up a conditional context. */
4049 gimple_push_condition ();
4050 gimplify_stmt (expr_p, &seq);
4051 gimple_pop_condition (pre_p);
4052 gimple_seq_add_seq (pre_p, seq);
4053
4054 return GS_ALL_DONE;
4055 }
4056 }
4057
4058 /* Now do the normal gimplification. */
4059
4060 /* Gimplify condition. */
4061 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4062 fb_rvalue);
4063 if (ret == GS_ERROR)
4064 return GS_ERROR;
4065 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4066
4067 gimple_push_condition ();
4068
4069 have_then_clause_p = have_else_clause_p = false;
4070 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4071 if (label_true
4072 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4073 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4074 have different locations, otherwise we end up with incorrect
4075 location information on the branches. */
4076 && (optimize
4077 || !EXPR_HAS_LOCATION (expr)
4078 || !rexpr_has_location (label_true)
4079 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4080 {
4081 have_then_clause_p = true;
4082 label_true = GOTO_DESTINATION (label_true);
4083 }
4084 else
4085 label_true = create_artificial_label (UNKNOWN_LOCATION);
4086 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4087 if (label_false
4088 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4089 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4090 have different locations, otherwise we end up with incorrect
4091 location information on the branches. */
4092 && (optimize
4093 || !EXPR_HAS_LOCATION (expr)
4094 || !rexpr_has_location (label_false)
4095 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4096 {
4097 have_else_clause_p = true;
4098 label_false = GOTO_DESTINATION (label_false);
4099 }
4100 else
4101 label_false = create_artificial_label (UNKNOWN_LOCATION);
4102
4103 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4104 &arm2);
4105 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4106 label_false);
4107 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4108 gimplify_seq_add_stmt (&seq, cond_stmt);
4109 gimple_stmt_iterator gsi = gsi_last (seq);
4110 maybe_fold_stmt (&gsi);
4111
4112 label_cont = NULL_TREE;
4113 if (!have_then_clause_p)
4114 {
4115 /* For if (...) {} else { code; } put label_true after
4116 the else block. */
4117 if (TREE_OPERAND (expr, 1) == NULL_TREE
4118 && !have_else_clause_p
4119 && TREE_OPERAND (expr, 2) != NULL_TREE)
4120 label_cont = label_true;
4121 else
4122 {
4123 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4124 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4125 /* For if (...) { code; } else {} or
4126 if (...) { code; } else goto label; or
4127 if (...) { code; return; } else { ... }
4128 label_cont isn't needed. */
4129 if (!have_else_clause_p
4130 && TREE_OPERAND (expr, 2) != NULL_TREE
4131 && gimple_seq_may_fallthru (seq))
4132 {
4133 gimple *g;
4134 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4135
4136 g = gimple_build_goto (label_cont);
4137
4138 /* GIMPLE_COND's are very low level; they have embedded
4139 gotos. This particular embedded goto should not be marked
4140 with the location of the original COND_EXPR, as it would
4141 correspond to the COND_EXPR's condition, not the ELSE or the
4142 THEN arms. To avoid marking it with the wrong location, flag
4143 it as "no location". */
4144 gimple_set_do_not_emit_location (g);
4145
4146 gimplify_seq_add_stmt (&seq, g);
4147 }
4148 }
4149 }
4150 if (!have_else_clause_p)
4151 {
4152 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4153 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4154 }
4155 if (label_cont)
4156 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4157
4158 gimple_pop_condition (pre_p);
4159 gimple_seq_add_seq (pre_p, seq);
4160
4161 if (ret == GS_ERROR)
4162 ; /* Do nothing. */
4163 else if (have_then_clause_p || have_else_clause_p)
4164 ret = GS_ALL_DONE;
4165 else
4166 {
4167 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4168 expr = TREE_OPERAND (expr, 0);
4169 gimplify_stmt (&expr, pre_p);
4170 }
4171
4172 *expr_p = NULL;
4173 return ret;
4174 }
4175
4176 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4177 to be marked addressable.
4178
4179 We cannot rely on such an expression being directly markable if a temporary
4180 has been created by the gimplification. In this case, we create another
4181 temporary and initialize it with a copy, which will become a store after we
4182 mark it addressable. This can happen if the front-end passed us something
4183 that it could not mark addressable yet, like a Fortran pass-by-reference
4184 parameter (int) floatvar. */
4185
4186 static void
4187 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4188 {
4189 while (handled_component_p (*expr_p))
4190 expr_p = &TREE_OPERAND (*expr_p, 0);
4191 if (is_gimple_reg (*expr_p))
4192 {
4193 /* Do not allow an SSA name as the temporary. */
4194 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4195 DECL_GIMPLE_REG_P (var) = 0;
4196 *expr_p = var;
4197 }
4198 }
4199
4200 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4201 a call to __builtin_memcpy. */
4202
4203 static enum gimplify_status
4204 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4205 gimple_seq *seq_p)
4206 {
4207 tree t, to, to_ptr, from, from_ptr;
4208 gcall *gs;
4209 location_t loc = EXPR_LOCATION (*expr_p);
4210
4211 to = TREE_OPERAND (*expr_p, 0);
4212 from = TREE_OPERAND (*expr_p, 1);
4213
4214 /* Mark the RHS addressable. Beware that it may not be possible to do so
4215 directly if a temporary has been created by the gimplification. */
4216 prepare_gimple_addressable (&from, seq_p);
4217
4218 mark_addressable (from);
4219 from_ptr = build_fold_addr_expr_loc (loc, from);
4220 gimplify_arg (&from_ptr, seq_p, loc);
4221
4222 mark_addressable (to);
4223 to_ptr = build_fold_addr_expr_loc (loc, to);
4224 gimplify_arg (&to_ptr, seq_p, loc);
4225
4226 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4227
4228 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4229
4230 if (want_value)
4231 {
4232 /* tmp = memcpy() */
4233 t = create_tmp_var (TREE_TYPE (to_ptr));
4234 gimple_call_set_lhs (gs, t);
4235 gimplify_seq_add_stmt (seq_p, gs);
4236
4237 *expr_p = build_simple_mem_ref (t);
4238 return GS_ALL_DONE;
4239 }
4240
4241 gimplify_seq_add_stmt (seq_p, gs);
4242 *expr_p = NULL;
4243 return GS_ALL_DONE;
4244 }
4245
4246 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4247 a call to __builtin_memset. In this case we know that the RHS is
4248 a CONSTRUCTOR with an empty element list. */
4249
4250 static enum gimplify_status
4251 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4252 gimple_seq *seq_p)
4253 {
4254 tree t, from, to, to_ptr;
4255 gcall *gs;
4256 location_t loc = EXPR_LOCATION (*expr_p);
4257
4258 /* Assert our assumptions, to abort instead of producing wrong code
4259 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4260 not be immediately exposed. */
4261 from = TREE_OPERAND (*expr_p, 1);
4262 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4263 from = TREE_OPERAND (from, 0);
4264
4265 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4266 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4267
4268 /* Now proceed. */
4269 to = TREE_OPERAND (*expr_p, 0);
4270
4271 to_ptr = build_fold_addr_expr_loc (loc, to);
4272 gimplify_arg (&to_ptr, seq_p, loc);
4273 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4274
4275 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4276
4277 if (want_value)
4278 {
4279 /* tmp = memset() */
4280 t = create_tmp_var (TREE_TYPE (to_ptr));
4281 gimple_call_set_lhs (gs, t);
4282 gimplify_seq_add_stmt (seq_p, gs);
4283
4284 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4285 return GS_ALL_DONE;
4286 }
4287
4288 gimplify_seq_add_stmt (seq_p, gs);
4289 *expr_p = NULL;
4290 return GS_ALL_DONE;
4291 }
4292
4293 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4294 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4295 assignment. Return non-null if we detect a potential overlap. */
4296
4297 struct gimplify_init_ctor_preeval_data
4298 {
4299 /* The base decl of the lhs object. May be NULL, in which case we
4300 have to assume the lhs is indirect. */
4301 tree lhs_base_decl;
4302
4303 /* The alias set of the lhs object. */
4304 alias_set_type lhs_alias_set;
4305 };
4306
4307 static tree
4308 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4309 {
4310 struct gimplify_init_ctor_preeval_data *data
4311 = (struct gimplify_init_ctor_preeval_data *) xdata;
4312 tree t = *tp;
4313
4314 /* If we find the base object, obviously we have overlap. */
4315 if (data->lhs_base_decl == t)
4316 return t;
4317
4318 /* If the constructor component is indirect, determine if we have a
4319 potential overlap with the lhs. The only bits of information we
4320 have to go on at this point are addressability and alias sets. */
4321 if ((INDIRECT_REF_P (t)
4322 || TREE_CODE (t) == MEM_REF)
4323 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4324 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4325 return t;
4326
4327 /* If the constructor component is a call, determine if it can hide a
4328 potential overlap with the lhs through an INDIRECT_REF like above.
4329 ??? Ugh - this is completely broken. In fact this whole analysis
4330 doesn't look conservative. */
4331 if (TREE_CODE (t) == CALL_EXPR)
4332 {
4333 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4334
4335 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4336 if (POINTER_TYPE_P (TREE_VALUE (type))
4337 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4338 && alias_sets_conflict_p (data->lhs_alias_set,
4339 get_alias_set
4340 (TREE_TYPE (TREE_VALUE (type)))))
4341 return t;
4342 }
4343
4344 if (IS_TYPE_OR_DECL_P (t))
4345 *walk_subtrees = 0;
4346 return NULL;
4347 }
4348
4349 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4350 force values that overlap with the lhs (as described by *DATA)
4351 into temporaries. */
4352
4353 static void
4354 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4355 struct gimplify_init_ctor_preeval_data *data)
4356 {
4357 enum gimplify_status one;
4358
4359 /* If the value is constant, then there's nothing to pre-evaluate. */
4360 if (TREE_CONSTANT (*expr_p))
4361 {
4362 /* Ensure it does not have side effects, it might contain a reference to
4363 the object we're initializing. */
4364 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4365 return;
4366 }
4367
4368 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4369 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4370 return;
4371
4372 /* Recurse for nested constructors. */
4373 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4374 {
4375 unsigned HOST_WIDE_INT ix;
4376 constructor_elt *ce;
4377 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4378
4379 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4380 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4381
4382 return;
4383 }
4384
4385 /* If this is a variable sized type, we must remember the size. */
4386 maybe_with_size_expr (expr_p);
4387
4388 /* Gimplify the constructor element to something appropriate for the rhs
4389 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4390 the gimplifier will consider this a store to memory. Doing this
4391 gimplification now means that we won't have to deal with complicated
4392 language-specific trees, nor trees like SAVE_EXPR that can induce
4393 exponential search behavior. */
4394 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4395 if (one == GS_ERROR)
4396 {
4397 *expr_p = NULL;
4398 return;
4399 }
4400
4401 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4402 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4403 always be true for all scalars, since is_gimple_mem_rhs insists on a
4404 temporary variable for them. */
4405 if (DECL_P (*expr_p))
4406 return;
4407
4408 /* If this is of variable size, we have no choice but to assume it doesn't
4409 overlap since we can't make a temporary for it. */
4410 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4411 return;
4412
4413 /* Otherwise, we must search for overlap ... */
4414 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4415 return;
4416
4417 /* ... and if found, force the value into a temporary. */
4418 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4419 }
4420
4421 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4422 a RANGE_EXPR in a CONSTRUCTOR for an array.
4423
4424 var = lower;
4425 loop_entry:
4426 object[var] = value;
4427 if (var == upper)
4428 goto loop_exit;
4429 var = var + 1;
4430 goto loop_entry;
4431 loop_exit:
4432
4433 We increment var _after_ the loop exit check because we might otherwise
4434 fail if upper == TYPE_MAX_VALUE (type for upper).
4435
4436 Note that we never have to deal with SAVE_EXPRs here, because this has
4437 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4438
4439 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4440 gimple_seq *, bool);
4441
4442 static void
4443 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4444 tree value, tree array_elt_type,
4445 gimple_seq *pre_p, bool cleared)
4446 {
4447 tree loop_entry_label, loop_exit_label, fall_thru_label;
4448 tree var, var_type, cref, tmp;
4449
4450 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4451 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4452 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4453
4454 /* Create and initialize the index variable. */
4455 var_type = TREE_TYPE (upper);
4456 var = create_tmp_var (var_type);
4457 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4458
4459 /* Add the loop entry label. */
4460 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4461
4462 /* Build the reference. */
4463 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4464 var, NULL_TREE, NULL_TREE);
4465
4466 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4467 the store. Otherwise just assign value to the reference. */
4468
4469 if (TREE_CODE (value) == CONSTRUCTOR)
4470 /* NB we might have to call ourself recursively through
4471 gimplify_init_ctor_eval if the value is a constructor. */
4472 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4473 pre_p, cleared);
4474 else
4475 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4476
4477 /* We exit the loop when the index var is equal to the upper bound. */
4478 gimplify_seq_add_stmt (pre_p,
4479 gimple_build_cond (EQ_EXPR, var, upper,
4480 loop_exit_label, fall_thru_label));
4481
4482 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4483
4484 /* Otherwise, increment the index var... */
4485 tmp = build2 (PLUS_EXPR, var_type, var,
4486 fold_convert (var_type, integer_one_node));
4487 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4488
4489 /* ...and jump back to the loop entry. */
4490 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4491
4492 /* Add the loop exit label. */
4493 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4494 }
4495
4496 /* Return true if FDECL is accessing a field that is zero sized. */
4497
4498 static bool
4499 zero_sized_field_decl (const_tree fdecl)
4500 {
4501 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4502 && integer_zerop (DECL_SIZE (fdecl)))
4503 return true;
4504 return false;
4505 }
4506
4507 /* Return true if TYPE is zero sized. */
4508
4509 static bool
4510 zero_sized_type (const_tree type)
4511 {
4512 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4513 && integer_zerop (TYPE_SIZE (type)))
4514 return true;
4515 return false;
4516 }
4517
4518 /* A subroutine of gimplify_init_constructor. Generate individual
4519 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4520 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4521 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4522 zeroed first. */
4523
4524 static void
4525 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4526 gimple_seq *pre_p, bool cleared)
4527 {
4528 tree array_elt_type = NULL;
4529 unsigned HOST_WIDE_INT ix;
4530 tree purpose, value;
4531
4532 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4533 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4534
4535 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4536 {
4537 tree cref;
4538
4539 /* NULL values are created above for gimplification errors. */
4540 if (value == NULL)
4541 continue;
4542
4543 if (cleared && initializer_zerop (value))
4544 continue;
4545
4546 /* ??? Here's to hoping the front end fills in all of the indices,
4547 so we don't have to figure out what's missing ourselves. */
4548 gcc_assert (purpose);
4549
4550 /* Skip zero-sized fields, unless value has side-effects. This can
4551 happen with calls to functions returning a zero-sized type, which
4552 we shouldn't discard. As a number of downstream passes don't
4553 expect sets of zero-sized fields, we rely on the gimplification of
4554 the MODIFY_EXPR we make below to drop the assignment statement. */
4555 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4556 continue;
4557
4558 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4559 whole range. */
4560 if (TREE_CODE (purpose) == RANGE_EXPR)
4561 {
4562 tree lower = TREE_OPERAND (purpose, 0);
4563 tree upper = TREE_OPERAND (purpose, 1);
4564
4565 /* If the lower bound is equal to upper, just treat it as if
4566 upper was the index. */
4567 if (simple_cst_equal (lower, upper))
4568 purpose = upper;
4569 else
4570 {
4571 gimplify_init_ctor_eval_range (object, lower, upper, value,
4572 array_elt_type, pre_p, cleared);
4573 continue;
4574 }
4575 }
4576
4577 if (array_elt_type)
4578 {
4579 /* Do not use bitsizetype for ARRAY_REF indices. */
4580 if (TYPE_DOMAIN (TREE_TYPE (object)))
4581 purpose
4582 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4583 purpose);
4584 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4585 purpose, NULL_TREE, NULL_TREE);
4586 }
4587 else
4588 {
4589 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4590 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4591 unshare_expr (object), purpose, NULL_TREE);
4592 }
4593
4594 if (TREE_CODE (value) == CONSTRUCTOR
4595 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4596 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4597 pre_p, cleared);
4598 else
4599 {
4600 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4601 gimplify_and_add (init, pre_p);
4602 ggc_free (init);
4603 }
4604 }
4605 }
4606
4607 /* Return the appropriate RHS predicate for this LHS. */
4608
4609 gimple_predicate
4610 rhs_predicate_for (tree lhs)
4611 {
4612 if (is_gimple_reg (lhs))
4613 return is_gimple_reg_rhs_or_call;
4614 else
4615 return is_gimple_mem_rhs_or_call;
4616 }
4617
4618 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4619 before the LHS has been gimplified. */
4620
4621 static gimple_predicate
4622 initial_rhs_predicate_for (tree lhs)
4623 {
4624 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4625 return is_gimple_reg_rhs_or_call;
4626 else
4627 return is_gimple_mem_rhs_or_call;
4628 }
4629
4630 /* Gimplify a C99 compound literal expression. This just means adding
4631 the DECL_EXPR before the current statement and using its anonymous
4632 decl instead. */
4633
4634 static enum gimplify_status
4635 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4636 bool (*gimple_test_f) (tree),
4637 fallback_t fallback)
4638 {
4639 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4640 tree decl = DECL_EXPR_DECL (decl_s);
4641 tree init = DECL_INITIAL (decl);
4642 /* Mark the decl as addressable if the compound literal
4643 expression is addressable now, otherwise it is marked too late
4644 after we gimplify the initialization expression. */
4645 if (TREE_ADDRESSABLE (*expr_p))
4646 TREE_ADDRESSABLE (decl) = 1;
4647 /* Otherwise, if we don't need an lvalue and have a literal directly
4648 substitute it. Check if it matches the gimple predicate, as
4649 otherwise we'd generate a new temporary, and we can as well just
4650 use the decl we already have. */
4651 else if (!TREE_ADDRESSABLE (decl)
4652 && init
4653 && (fallback & fb_lvalue) == 0
4654 && gimple_test_f (init))
4655 {
4656 *expr_p = init;
4657 return GS_OK;
4658 }
4659
4660 /* Preliminarily mark non-addressed complex variables as eligible
4661 for promotion to gimple registers. We'll transform their uses
4662 as we find them. */
4663 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4664 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4665 && !TREE_THIS_VOLATILE (decl)
4666 && !needs_to_live_in_memory (decl))
4667 DECL_GIMPLE_REG_P (decl) = 1;
4668
4669 /* If the decl is not addressable, then it is being used in some
4670 expression or on the right hand side of a statement, and it can
4671 be put into a readonly data section. */
4672 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4673 TREE_READONLY (decl) = 1;
4674
4675 /* This decl isn't mentioned in the enclosing block, so add it to the
4676 list of temps. FIXME it seems a bit of a kludge to say that
4677 anonymous artificial vars aren't pushed, but everything else is. */
4678 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4679 gimple_add_tmp_var (decl);
4680
4681 gimplify_and_add (decl_s, pre_p);
4682 *expr_p = decl;
4683 return GS_OK;
4684 }
4685
4686 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4687 return a new CONSTRUCTOR if something changed. */
4688
4689 static tree
4690 optimize_compound_literals_in_ctor (tree orig_ctor)
4691 {
4692 tree ctor = orig_ctor;
4693 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4694 unsigned int idx, num = vec_safe_length (elts);
4695
4696 for (idx = 0; idx < num; idx++)
4697 {
4698 tree value = (*elts)[idx].value;
4699 tree newval = value;
4700 if (TREE_CODE (value) == CONSTRUCTOR)
4701 newval = optimize_compound_literals_in_ctor (value);
4702 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4703 {
4704 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4705 tree decl = DECL_EXPR_DECL (decl_s);
4706 tree init = DECL_INITIAL (decl);
4707
4708 if (!TREE_ADDRESSABLE (value)
4709 && !TREE_ADDRESSABLE (decl)
4710 && init
4711 && TREE_CODE (init) == CONSTRUCTOR)
4712 newval = optimize_compound_literals_in_ctor (init);
4713 }
4714 if (newval == value)
4715 continue;
4716
4717 if (ctor == orig_ctor)
4718 {
4719 ctor = copy_node (orig_ctor);
4720 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4721 elts = CONSTRUCTOR_ELTS (ctor);
4722 }
4723 (*elts)[idx].value = newval;
4724 }
4725 return ctor;
4726 }
4727
4728 /* A subroutine of gimplify_modify_expr. Break out elements of a
4729 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4730
4731 Note that we still need to clear any elements that don't have explicit
4732 initializers, so if not all elements are initialized we keep the
4733 original MODIFY_EXPR, we just remove all of the constructor elements.
4734
4735 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4736 GS_ERROR if we would have to create a temporary when gimplifying
4737 this constructor. Otherwise, return GS_OK.
4738
4739 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4740
4741 static enum gimplify_status
4742 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4743 bool want_value, bool notify_temp_creation)
4744 {
4745 tree object, ctor, type;
4746 enum gimplify_status ret;
4747 vec<constructor_elt, va_gc> *elts;
4748
4749 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4750
4751 if (!notify_temp_creation)
4752 {
4753 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4754 is_gimple_lvalue, fb_lvalue);
4755 if (ret == GS_ERROR)
4756 return ret;
4757 }
4758
4759 object = TREE_OPERAND (*expr_p, 0);
4760 ctor = TREE_OPERAND (*expr_p, 1)
4761 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4762 type = TREE_TYPE (ctor);
4763 elts = CONSTRUCTOR_ELTS (ctor);
4764 ret = GS_ALL_DONE;
4765
4766 switch (TREE_CODE (type))
4767 {
4768 case RECORD_TYPE:
4769 case UNION_TYPE:
4770 case QUAL_UNION_TYPE:
4771 case ARRAY_TYPE:
4772 {
4773 struct gimplify_init_ctor_preeval_data preeval_data;
4774 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4775 bool cleared, complete_p, valid_const_initializer;
4776
4777 /* Aggregate types must lower constructors to initialization of
4778 individual elements. The exception is that a CONSTRUCTOR node
4779 with no elements indicates zero-initialization of the whole. */
4780 if (vec_safe_is_empty (elts))
4781 {
4782 if (notify_temp_creation)
4783 return GS_OK;
4784 break;
4785 }
4786
4787 /* Fetch information about the constructor to direct later processing.
4788 We might want to make static versions of it in various cases, and
4789 can only do so if it known to be a valid constant initializer. */
4790 valid_const_initializer
4791 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4792 &num_ctor_elements, &complete_p);
4793
4794 /* If a const aggregate variable is being initialized, then it
4795 should never be a lose to promote the variable to be static. */
4796 if (valid_const_initializer
4797 && num_nonzero_elements > 1
4798 && TREE_READONLY (object)
4799 && VAR_P (object)
4800 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4801 {
4802 if (notify_temp_creation)
4803 return GS_ERROR;
4804 DECL_INITIAL (object) = ctor;
4805 TREE_STATIC (object) = 1;
4806 if (!DECL_NAME (object))
4807 DECL_NAME (object) = create_tmp_var_name ("C");
4808 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4809
4810 /* ??? C++ doesn't automatically append a .<number> to the
4811 assembler name, and even when it does, it looks at FE private
4812 data structures to figure out what that number should be,
4813 which are not set for this variable. I suppose this is
4814 important for local statics for inline functions, which aren't
4815 "local" in the object file sense. So in order to get a unique
4816 TU-local symbol, we must invoke the lhd version now. */
4817 lhd_set_decl_assembler_name (object);
4818
4819 *expr_p = NULL_TREE;
4820 break;
4821 }
4822
4823 /* If there are "lots" of initialized elements, even discounting
4824 those that are not address constants (and thus *must* be
4825 computed at runtime), then partition the constructor into
4826 constant and non-constant parts. Block copy the constant
4827 parts in, then generate code for the non-constant parts. */
4828 /* TODO. There's code in cp/typeck.c to do this. */
4829
4830 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4831 /* store_constructor will ignore the clearing of variable-sized
4832 objects. Initializers for such objects must explicitly set
4833 every field that needs to be set. */
4834 cleared = false;
4835 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4836 /* If the constructor isn't complete, clear the whole object
4837 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4838
4839 ??? This ought not to be needed. For any element not present
4840 in the initializer, we should simply set them to zero. Except
4841 we'd need to *find* the elements that are not present, and that
4842 requires trickery to avoid quadratic compile-time behavior in
4843 large cases or excessive memory use in small cases. */
4844 cleared = true;
4845 else if (num_ctor_elements - num_nonzero_elements
4846 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4847 && num_nonzero_elements < num_ctor_elements / 4)
4848 /* If there are "lots" of zeros, it's more efficient to clear
4849 the memory and then set the nonzero elements. */
4850 cleared = true;
4851 else
4852 cleared = false;
4853
4854 /* If there are "lots" of initialized elements, and all of them
4855 are valid address constants, then the entire initializer can
4856 be dropped to memory, and then memcpy'd out. Don't do this
4857 for sparse arrays, though, as it's more efficient to follow
4858 the standard CONSTRUCTOR behavior of memset followed by
4859 individual element initialization. Also don't do this for small
4860 all-zero initializers (which aren't big enough to merit
4861 clearing), and don't try to make bitwise copies of
4862 TREE_ADDRESSABLE types. */
4863
4864 if (valid_const_initializer
4865 && !(cleared || num_nonzero_elements == 0)
4866 && !TREE_ADDRESSABLE (type))
4867 {
4868 HOST_WIDE_INT size = int_size_in_bytes (type);
4869 unsigned int align;
4870
4871 /* ??? We can still get unbounded array types, at least
4872 from the C++ front end. This seems wrong, but attempt
4873 to work around it for now. */
4874 if (size < 0)
4875 {
4876 size = int_size_in_bytes (TREE_TYPE (object));
4877 if (size >= 0)
4878 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4879 }
4880
4881 /* Find the maximum alignment we can assume for the object. */
4882 /* ??? Make use of DECL_OFFSET_ALIGN. */
4883 if (DECL_P (object))
4884 align = DECL_ALIGN (object);
4885 else
4886 align = TYPE_ALIGN (type);
4887
4888 /* Do a block move either if the size is so small as to make
4889 each individual move a sub-unit move on average, or if it
4890 is so large as to make individual moves inefficient. */
4891 if (size > 0
4892 && num_nonzero_elements > 1
4893 && (size < num_nonzero_elements
4894 || !can_move_by_pieces (size, align)))
4895 {
4896 if (notify_temp_creation)
4897 return GS_ERROR;
4898
4899 walk_tree (&ctor, force_labels_r, NULL, NULL);
4900 ctor = tree_output_constant_def (ctor);
4901 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4902 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4903 TREE_OPERAND (*expr_p, 1) = ctor;
4904
4905 /* This is no longer an assignment of a CONSTRUCTOR, but
4906 we still may have processing to do on the LHS. So
4907 pretend we didn't do anything here to let that happen. */
4908 return GS_UNHANDLED;
4909 }
4910 }
4911
4912 /* If the target is volatile, we have non-zero elements and more than
4913 one field to assign, initialize the target from a temporary. */
4914 if (TREE_THIS_VOLATILE (object)
4915 && !TREE_ADDRESSABLE (type)
4916 && num_nonzero_elements > 0
4917 && vec_safe_length (elts) > 1)
4918 {
4919 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4920 TREE_OPERAND (*expr_p, 0) = temp;
4921 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4922 *expr_p,
4923 build2 (MODIFY_EXPR, void_type_node,
4924 object, temp));
4925 return GS_OK;
4926 }
4927
4928 if (notify_temp_creation)
4929 return GS_OK;
4930
4931 /* If there are nonzero elements and if needed, pre-evaluate to capture
4932 elements overlapping with the lhs into temporaries. We must do this
4933 before clearing to fetch the values before they are zeroed-out. */
4934 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4935 {
4936 preeval_data.lhs_base_decl = get_base_address (object);
4937 if (!DECL_P (preeval_data.lhs_base_decl))
4938 preeval_data.lhs_base_decl = NULL;
4939 preeval_data.lhs_alias_set = get_alias_set (object);
4940
4941 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4942 pre_p, post_p, &preeval_data);
4943 }
4944
4945 bool ctor_has_side_effects_p
4946 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4947
4948 if (cleared)
4949 {
4950 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4951 Note that we still have to gimplify, in order to handle the
4952 case of variable sized types. Avoid shared tree structures. */
4953 CONSTRUCTOR_ELTS (ctor) = NULL;
4954 TREE_SIDE_EFFECTS (ctor) = 0;
4955 object = unshare_expr (object);
4956 gimplify_stmt (expr_p, pre_p);
4957 }
4958
4959 /* If we have not block cleared the object, or if there are nonzero
4960 elements in the constructor, or if the constructor has side effects,
4961 add assignments to the individual scalar fields of the object. */
4962 if (!cleared
4963 || num_nonzero_elements > 0
4964 || ctor_has_side_effects_p)
4965 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4966
4967 *expr_p = NULL_TREE;
4968 }
4969 break;
4970
4971 case COMPLEX_TYPE:
4972 {
4973 tree r, i;
4974
4975 if (notify_temp_creation)
4976 return GS_OK;
4977
4978 /* Extract the real and imaginary parts out of the ctor. */
4979 gcc_assert (elts->length () == 2);
4980 r = (*elts)[0].value;
4981 i = (*elts)[1].value;
4982 if (r == NULL || i == NULL)
4983 {
4984 tree zero = build_zero_cst (TREE_TYPE (type));
4985 if (r == NULL)
4986 r = zero;
4987 if (i == NULL)
4988 i = zero;
4989 }
4990
4991 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4992 represent creation of a complex value. */
4993 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4994 {
4995 ctor = build_complex (type, r, i);
4996 TREE_OPERAND (*expr_p, 1) = ctor;
4997 }
4998 else
4999 {
5000 ctor = build2 (COMPLEX_EXPR, type, r, i);
5001 TREE_OPERAND (*expr_p, 1) = ctor;
5002 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5003 pre_p,
5004 post_p,
5005 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5006 fb_rvalue);
5007 }
5008 }
5009 break;
5010
5011 case VECTOR_TYPE:
5012 {
5013 unsigned HOST_WIDE_INT ix;
5014 constructor_elt *ce;
5015
5016 if (notify_temp_creation)
5017 return GS_OK;
5018
5019 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5020 if (TREE_CONSTANT (ctor))
5021 {
5022 bool constant_p = true;
5023 tree value;
5024
5025 /* Even when ctor is constant, it might contain non-*_CST
5026 elements, such as addresses or trapping values like
5027 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5028 in VECTOR_CST nodes. */
5029 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5030 if (!CONSTANT_CLASS_P (value))
5031 {
5032 constant_p = false;
5033 break;
5034 }
5035
5036 if (constant_p)
5037 {
5038 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5039 break;
5040 }
5041
5042 TREE_CONSTANT (ctor) = 0;
5043 }
5044
5045 /* Vector types use CONSTRUCTOR all the way through gimple
5046 compilation as a general initializer. */
5047 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5048 {
5049 enum gimplify_status tret;
5050 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5051 fb_rvalue);
5052 if (tret == GS_ERROR)
5053 ret = GS_ERROR;
5054 else if (TREE_STATIC (ctor)
5055 && !initializer_constant_valid_p (ce->value,
5056 TREE_TYPE (ce->value)))
5057 TREE_STATIC (ctor) = 0;
5058 }
5059 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5060 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5061 }
5062 break;
5063
5064 default:
5065 /* So how did we get a CONSTRUCTOR for a scalar type? */
5066 gcc_unreachable ();
5067 }
5068
5069 if (ret == GS_ERROR)
5070 return GS_ERROR;
5071 /* If we have gimplified both sides of the initializer but have
5072 not emitted an assignment, do so now. */
5073 if (*expr_p)
5074 {
5075 tree lhs = TREE_OPERAND (*expr_p, 0);
5076 tree rhs = TREE_OPERAND (*expr_p, 1);
5077 if (want_value && object == lhs)
5078 lhs = unshare_expr (lhs);
5079 gassign *init = gimple_build_assign (lhs, rhs);
5080 gimplify_seq_add_stmt (pre_p, init);
5081 }
5082 if (want_value)
5083 {
5084 *expr_p = object;
5085 return GS_OK;
5086 }
5087 else
5088 {
5089 *expr_p = NULL;
5090 return GS_ALL_DONE;
5091 }
5092 }
5093
5094 /* Given a pointer value OP0, return a simplified version of an
5095 indirection through OP0, or NULL_TREE if no simplification is
5096 possible. This may only be applied to a rhs of an expression.
5097 Note that the resulting type may be different from the type pointed
5098 to in the sense that it is still compatible from the langhooks
5099 point of view. */
5100
5101 static tree
5102 gimple_fold_indirect_ref_rhs (tree t)
5103 {
5104 return gimple_fold_indirect_ref (t);
5105 }
5106
5107 /* Subroutine of gimplify_modify_expr to do simplifications of
5108 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5109 something changes. */
5110
5111 static enum gimplify_status
5112 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5113 gimple_seq *pre_p, gimple_seq *post_p,
5114 bool want_value)
5115 {
5116 enum gimplify_status ret = GS_UNHANDLED;
5117 bool changed;
5118
5119 do
5120 {
5121 changed = false;
5122 switch (TREE_CODE (*from_p))
5123 {
5124 case VAR_DECL:
5125 /* If we're assigning from a read-only variable initialized with
5126 a constructor, do the direct assignment from the constructor,
5127 but only if neither source nor target are volatile since this
5128 latter assignment might end up being done on a per-field basis. */
5129 if (DECL_INITIAL (*from_p)
5130 && TREE_READONLY (*from_p)
5131 && !TREE_THIS_VOLATILE (*from_p)
5132 && !TREE_THIS_VOLATILE (*to_p)
5133 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5134 {
5135 tree old_from = *from_p;
5136 enum gimplify_status subret;
5137
5138 /* Move the constructor into the RHS. */
5139 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5140
5141 /* Let's see if gimplify_init_constructor will need to put
5142 it in memory. */
5143 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5144 false, true);
5145 if (subret == GS_ERROR)
5146 {
5147 /* If so, revert the change. */
5148 *from_p = old_from;
5149 }
5150 else
5151 {
5152 ret = GS_OK;
5153 changed = true;
5154 }
5155 }
5156 break;
5157 case INDIRECT_REF:
5158 {
5159 /* If we have code like
5160
5161 *(const A*)(A*)&x
5162
5163 where the type of "x" is a (possibly cv-qualified variant
5164 of "A"), treat the entire expression as identical to "x".
5165 This kind of code arises in C++ when an object is bound
5166 to a const reference, and if "x" is a TARGET_EXPR we want
5167 to take advantage of the optimization below. */
5168 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5169 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5170 if (t)
5171 {
5172 if (TREE_THIS_VOLATILE (t) != volatile_p)
5173 {
5174 if (DECL_P (t))
5175 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5176 build_fold_addr_expr (t));
5177 if (REFERENCE_CLASS_P (t))
5178 TREE_THIS_VOLATILE (t) = volatile_p;
5179 }
5180 *from_p = t;
5181 ret = GS_OK;
5182 changed = true;
5183 }
5184 break;
5185 }
5186
5187 case TARGET_EXPR:
5188 {
5189 /* If we are initializing something from a TARGET_EXPR, strip the
5190 TARGET_EXPR and initialize it directly, if possible. This can't
5191 be done if the initializer is void, since that implies that the
5192 temporary is set in some non-trivial way.
5193
5194 ??? What about code that pulls out the temp and uses it
5195 elsewhere? I think that such code never uses the TARGET_EXPR as
5196 an initializer. If I'm wrong, we'll die because the temp won't
5197 have any RTL. In that case, I guess we'll need to replace
5198 references somehow. */
5199 tree init = TARGET_EXPR_INITIAL (*from_p);
5200
5201 if (init
5202 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5203 || !TARGET_EXPR_NO_ELIDE (*from_p))
5204 && !VOID_TYPE_P (TREE_TYPE (init)))
5205 {
5206 *from_p = init;
5207 ret = GS_OK;
5208 changed = true;
5209 }
5210 }
5211 break;
5212
5213 case COMPOUND_EXPR:
5214 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5215 caught. */
5216 gimplify_compound_expr (from_p, pre_p, true);
5217 ret = GS_OK;
5218 changed = true;
5219 break;
5220
5221 case CONSTRUCTOR:
5222 /* If we already made some changes, let the front end have a
5223 crack at this before we break it down. */
5224 if (ret != GS_UNHANDLED)
5225 break;
5226 /* If we're initializing from a CONSTRUCTOR, break this into
5227 individual MODIFY_EXPRs. */
5228 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5229 false);
5230
5231 case COND_EXPR:
5232 /* If we're assigning to a non-register type, push the assignment
5233 down into the branches. This is mandatory for ADDRESSABLE types,
5234 since we cannot generate temporaries for such, but it saves a
5235 copy in other cases as well. */
5236 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5237 {
5238 /* This code should mirror the code in gimplify_cond_expr. */
5239 enum tree_code code = TREE_CODE (*expr_p);
5240 tree cond = *from_p;
5241 tree result = *to_p;
5242
5243 ret = gimplify_expr (&result, pre_p, post_p,
5244 is_gimple_lvalue, fb_lvalue);
5245 if (ret != GS_ERROR)
5246 ret = GS_OK;
5247
5248 /* If we are going to write RESULT more than once, clear
5249 TREE_READONLY flag, otherwise we might incorrectly promote
5250 the variable to static const and initialize it at compile
5251 time in one of the branches. */
5252 if (VAR_P (result)
5253 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5254 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5255 TREE_READONLY (result) = 0;
5256 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5257 TREE_OPERAND (cond, 1)
5258 = build2 (code, void_type_node, result,
5259 TREE_OPERAND (cond, 1));
5260 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5261 TREE_OPERAND (cond, 2)
5262 = build2 (code, void_type_node, unshare_expr (result),
5263 TREE_OPERAND (cond, 2));
5264
5265 TREE_TYPE (cond) = void_type_node;
5266 recalculate_side_effects (cond);
5267
5268 if (want_value)
5269 {
5270 gimplify_and_add (cond, pre_p);
5271 *expr_p = unshare_expr (result);
5272 }
5273 else
5274 *expr_p = cond;
5275 return ret;
5276 }
5277 break;
5278
5279 case CALL_EXPR:
5280 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5281 return slot so that we don't generate a temporary. */
5282 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5283 && aggregate_value_p (*from_p, *from_p))
5284 {
5285 bool use_target;
5286
5287 if (!(rhs_predicate_for (*to_p))(*from_p))
5288 /* If we need a temporary, *to_p isn't accurate. */
5289 use_target = false;
5290 /* It's OK to use the return slot directly unless it's an NRV. */
5291 else if (TREE_CODE (*to_p) == RESULT_DECL
5292 && DECL_NAME (*to_p) == NULL_TREE
5293 && needs_to_live_in_memory (*to_p))
5294 use_target = true;
5295 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5296 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5297 /* Don't force regs into memory. */
5298 use_target = false;
5299 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5300 /* It's OK to use the target directly if it's being
5301 initialized. */
5302 use_target = true;
5303 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5304 != INTEGER_CST)
5305 /* Always use the target and thus RSO for variable-sized types.
5306 GIMPLE cannot deal with a variable-sized assignment
5307 embedded in a call statement. */
5308 use_target = true;
5309 else if (TREE_CODE (*to_p) != SSA_NAME
5310 && (!is_gimple_variable (*to_p)
5311 || needs_to_live_in_memory (*to_p)))
5312 /* Don't use the original target if it's already addressable;
5313 if its address escapes, and the called function uses the
5314 NRV optimization, a conforming program could see *to_p
5315 change before the called function returns; see c++/19317.
5316 When optimizing, the return_slot pass marks more functions
5317 as safe after we have escape info. */
5318 use_target = false;
5319 else
5320 use_target = true;
5321
5322 if (use_target)
5323 {
5324 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5325 mark_addressable (*to_p);
5326 }
5327 }
5328 break;
5329
5330 case WITH_SIZE_EXPR:
5331 /* Likewise for calls that return an aggregate of non-constant size,
5332 since we would not be able to generate a temporary at all. */
5333 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5334 {
5335 *from_p = TREE_OPERAND (*from_p, 0);
5336 /* We don't change ret in this case because the
5337 WITH_SIZE_EXPR might have been added in
5338 gimplify_modify_expr, so returning GS_OK would lead to an
5339 infinite loop. */
5340 changed = true;
5341 }
5342 break;
5343
5344 /* If we're initializing from a container, push the initialization
5345 inside it. */
5346 case CLEANUP_POINT_EXPR:
5347 case BIND_EXPR:
5348 case STATEMENT_LIST:
5349 {
5350 tree wrap = *from_p;
5351 tree t;
5352
5353 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5354 fb_lvalue);
5355 if (ret != GS_ERROR)
5356 ret = GS_OK;
5357
5358 t = voidify_wrapper_expr (wrap, *expr_p);
5359 gcc_assert (t == *expr_p);
5360
5361 if (want_value)
5362 {
5363 gimplify_and_add (wrap, pre_p);
5364 *expr_p = unshare_expr (*to_p);
5365 }
5366 else
5367 *expr_p = wrap;
5368 return GS_OK;
5369 }
5370
5371 case COMPOUND_LITERAL_EXPR:
5372 {
5373 tree complit = TREE_OPERAND (*expr_p, 1);
5374 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5375 tree decl = DECL_EXPR_DECL (decl_s);
5376 tree init = DECL_INITIAL (decl);
5377
5378 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5379 into struct T x = { 0, 1, 2 } if the address of the
5380 compound literal has never been taken. */
5381 if (!TREE_ADDRESSABLE (complit)
5382 && !TREE_ADDRESSABLE (decl)
5383 && init)
5384 {
5385 *expr_p = copy_node (*expr_p);
5386 TREE_OPERAND (*expr_p, 1) = init;
5387 return GS_OK;
5388 }
5389 }
5390
5391 default:
5392 break;
5393 }
5394 }
5395 while (changed);
5396
5397 return ret;
5398 }
5399
5400
5401 /* Return true if T looks like a valid GIMPLE statement. */
5402
5403 static bool
5404 is_gimple_stmt (tree t)
5405 {
5406 const enum tree_code code = TREE_CODE (t);
5407
5408 switch (code)
5409 {
5410 case NOP_EXPR:
5411 /* The only valid NOP_EXPR is the empty statement. */
5412 return IS_EMPTY_STMT (t);
5413
5414 case BIND_EXPR:
5415 case COND_EXPR:
5416 /* These are only valid if they're void. */
5417 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5418
5419 case SWITCH_EXPR:
5420 case GOTO_EXPR:
5421 case RETURN_EXPR:
5422 case LABEL_EXPR:
5423 case CASE_LABEL_EXPR:
5424 case TRY_CATCH_EXPR:
5425 case TRY_FINALLY_EXPR:
5426 case EH_FILTER_EXPR:
5427 case CATCH_EXPR:
5428 case ASM_EXPR:
5429 case STATEMENT_LIST:
5430 case OACC_PARALLEL:
5431 case OACC_KERNELS:
5432 case OACC_DATA:
5433 case OACC_HOST_DATA:
5434 case OACC_DECLARE:
5435 case OACC_UPDATE:
5436 case OACC_ENTER_DATA:
5437 case OACC_EXIT_DATA:
5438 case OACC_CACHE:
5439 case OMP_PARALLEL:
5440 case OMP_FOR:
5441 case OMP_SIMD:
5442 case OMP_DISTRIBUTE:
5443 case OACC_LOOP:
5444 case OMP_SECTIONS:
5445 case OMP_SECTION:
5446 case OMP_SINGLE:
5447 case OMP_MASTER:
5448 case OMP_TASKGROUP:
5449 case OMP_ORDERED:
5450 case OMP_CRITICAL:
5451 case OMP_TASK:
5452 case OMP_TARGET:
5453 case OMP_TARGET_DATA:
5454 case OMP_TARGET_UPDATE:
5455 case OMP_TARGET_ENTER_DATA:
5456 case OMP_TARGET_EXIT_DATA:
5457 case OMP_TASKLOOP:
5458 case OMP_TEAMS:
5459 /* These are always void. */
5460 return true;
5461
5462 case CALL_EXPR:
5463 case MODIFY_EXPR:
5464 case PREDICT_EXPR:
5465 /* These are valid regardless of their type. */
5466 return true;
5467
5468 default:
5469 return false;
5470 }
5471 }
5472
5473
5474 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5475 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5476 DECL_GIMPLE_REG_P set.
5477
5478 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5479 other, unmodified part of the complex object just before the total store.
5480 As a consequence, if the object is still uninitialized, an undefined value
5481 will be loaded into a register, which may result in a spurious exception
5482 if the register is floating-point and the value happens to be a signaling
5483 NaN for example. Then the fully-fledged complex operations lowering pass
5484 followed by a DCE pass are necessary in order to fix things up. */
5485
5486 static enum gimplify_status
5487 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5488 bool want_value)
5489 {
5490 enum tree_code code, ocode;
5491 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5492
5493 lhs = TREE_OPERAND (*expr_p, 0);
5494 rhs = TREE_OPERAND (*expr_p, 1);
5495 code = TREE_CODE (lhs);
5496 lhs = TREE_OPERAND (lhs, 0);
5497
5498 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5499 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5500 TREE_NO_WARNING (other) = 1;
5501 other = get_formal_tmp_var (other, pre_p);
5502
5503 realpart = code == REALPART_EXPR ? rhs : other;
5504 imagpart = code == REALPART_EXPR ? other : rhs;
5505
5506 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5507 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5508 else
5509 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5510
5511 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5512 *expr_p = (want_value) ? rhs : NULL_TREE;
5513
5514 return GS_ALL_DONE;
5515 }
5516
5517 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5518
5519 modify_expr
5520 : varname '=' rhs
5521 | '*' ID '=' rhs
5522
5523 PRE_P points to the list where side effects that must happen before
5524 *EXPR_P should be stored.
5525
5526 POST_P points to the list where side effects that must happen after
5527 *EXPR_P should be stored.
5528
5529 WANT_VALUE is nonzero iff we want to use the value of this expression
5530 in another expression. */
5531
5532 static enum gimplify_status
5533 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5534 bool want_value)
5535 {
5536 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5537 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5538 enum gimplify_status ret = GS_UNHANDLED;
5539 gimple *assign;
5540 location_t loc = EXPR_LOCATION (*expr_p);
5541 gimple_stmt_iterator gsi;
5542
5543 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5544 || TREE_CODE (*expr_p) == INIT_EXPR);
5545
5546 /* Trying to simplify a clobber using normal logic doesn't work,
5547 so handle it here. */
5548 if (TREE_CLOBBER_P (*from_p))
5549 {
5550 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5551 if (ret == GS_ERROR)
5552 return ret;
5553 gcc_assert (!want_value);
5554 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5555 {
5556 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5557 pre_p, post_p);
5558 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5559 }
5560 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5561 *expr_p = NULL;
5562 return GS_ALL_DONE;
5563 }
5564
5565 /* Insert pointer conversions required by the middle-end that are not
5566 required by the frontend. This fixes middle-end type checking for
5567 for example gcc.dg/redecl-6.c. */
5568 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5569 {
5570 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5571 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5572 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5573 }
5574
5575 /* See if any simplifications can be done based on what the RHS is. */
5576 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5577 want_value);
5578 if (ret != GS_UNHANDLED)
5579 return ret;
5580
5581 /* For zero sized types only gimplify the left hand side and right hand
5582 side as statements and throw away the assignment. Do this after
5583 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5584 types properly. */
5585 if (zero_sized_type (TREE_TYPE (*from_p))
5586 && !want_value
5587 /* Don't do this for calls that return addressable types, expand_call
5588 relies on those having a lhs. */
5589 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5590 && TREE_CODE (*from_p) == CALL_EXPR))
5591 {
5592 gimplify_stmt (from_p, pre_p);
5593 gimplify_stmt (to_p, pre_p);
5594 *expr_p = NULL_TREE;
5595 return GS_ALL_DONE;
5596 }
5597
5598 /* If the value being copied is of variable width, compute the length
5599 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5600 before gimplifying any of the operands so that we can resolve any
5601 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5602 the size of the expression to be copied, not of the destination, so
5603 that is what we must do here. */
5604 maybe_with_size_expr (from_p);
5605
5606 /* As a special case, we have to temporarily allow for assignments
5607 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5608 a toplevel statement, when gimplifying the GENERIC expression
5609 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5610 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5611
5612 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5613 prevent gimplify_expr from trying to create a new temporary for
5614 foo's LHS, we tell it that it should only gimplify until it
5615 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5616 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5617 and all we need to do here is set 'a' to be its LHS. */
5618
5619 /* Gimplify the RHS first for C++17 and bug 71104. */
5620 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5621 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5622 if (ret == GS_ERROR)
5623 return ret;
5624
5625 /* Then gimplify the LHS. */
5626 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5627 twice we have to make sure to gimplify into non-SSA as otherwise
5628 the abnormal edge added later will make those defs not dominate
5629 their uses.
5630 ??? Technically this applies only to the registers used in the
5631 resulting non-register *TO_P. */
5632 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5633 if (saved_into_ssa
5634 && TREE_CODE (*from_p) == CALL_EXPR
5635 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5636 gimplify_ctxp->into_ssa = false;
5637 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5638 gimplify_ctxp->into_ssa = saved_into_ssa;
5639 if (ret == GS_ERROR)
5640 return ret;
5641
5642 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5643 guess for the predicate was wrong. */
5644 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5645 if (final_pred != initial_pred)
5646 {
5647 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5648 if (ret == GS_ERROR)
5649 return ret;
5650 }
5651
5652 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5653 size as argument to the call. */
5654 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5655 {
5656 tree call = TREE_OPERAND (*from_p, 0);
5657 tree vlasize = TREE_OPERAND (*from_p, 1);
5658
5659 if (TREE_CODE (call) == CALL_EXPR
5660 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5661 {
5662 int nargs = call_expr_nargs (call);
5663 tree type = TREE_TYPE (call);
5664 tree ap = CALL_EXPR_ARG (call, 0);
5665 tree tag = CALL_EXPR_ARG (call, 1);
5666 tree aptag = CALL_EXPR_ARG (call, 2);
5667 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5668 IFN_VA_ARG, type,
5669 nargs + 1, ap, tag,
5670 aptag, vlasize);
5671 TREE_OPERAND (*from_p, 0) = newcall;
5672 }
5673 }
5674
5675 /* Now see if the above changed *from_p to something we handle specially. */
5676 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5677 want_value);
5678 if (ret != GS_UNHANDLED)
5679 return ret;
5680
5681 /* If we've got a variable sized assignment between two lvalues (i.e. does
5682 not involve a call), then we can make things a bit more straightforward
5683 by converting the assignment to memcpy or memset. */
5684 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5685 {
5686 tree from = TREE_OPERAND (*from_p, 0);
5687 tree size = TREE_OPERAND (*from_p, 1);
5688
5689 if (TREE_CODE (from) == CONSTRUCTOR)
5690 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5691
5692 if (is_gimple_addressable (from))
5693 {
5694 *from_p = from;
5695 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5696 pre_p);
5697 }
5698 }
5699
5700 /* Transform partial stores to non-addressable complex variables into
5701 total stores. This allows us to use real instead of virtual operands
5702 for these variables, which improves optimization. */
5703 if ((TREE_CODE (*to_p) == REALPART_EXPR
5704 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5705 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5706 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5707
5708 /* Try to alleviate the effects of the gimplification creating artificial
5709 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5710 make sure not to create DECL_DEBUG_EXPR links across functions. */
5711 if (!gimplify_ctxp->into_ssa
5712 && VAR_P (*from_p)
5713 && DECL_IGNORED_P (*from_p)
5714 && DECL_P (*to_p)
5715 && !DECL_IGNORED_P (*to_p)
5716 && decl_function_context (*to_p) == current_function_decl
5717 && decl_function_context (*from_p) == current_function_decl)
5718 {
5719 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5720 DECL_NAME (*from_p)
5721 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5722 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5723 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5724 }
5725
5726 if (want_value && TREE_THIS_VOLATILE (*to_p))
5727 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5728
5729 if (TREE_CODE (*from_p) == CALL_EXPR)
5730 {
5731 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5732 instead of a GIMPLE_ASSIGN. */
5733 gcall *call_stmt;
5734 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5735 {
5736 /* Gimplify internal functions created in the FEs. */
5737 int nargs = call_expr_nargs (*from_p), i;
5738 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5739 auto_vec<tree> vargs (nargs);
5740
5741 for (i = 0; i < nargs; i++)
5742 {
5743 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5744 EXPR_LOCATION (*from_p));
5745 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5746 }
5747 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5748 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5749 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5750 }
5751 else
5752 {
5753 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5754 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5755 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5756 tree fndecl = get_callee_fndecl (*from_p);
5757 if (fndecl
5758 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5759 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5760 && call_expr_nargs (*from_p) == 3)
5761 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5762 CALL_EXPR_ARG (*from_p, 0),
5763 CALL_EXPR_ARG (*from_p, 1),
5764 CALL_EXPR_ARG (*from_p, 2));
5765 else
5766 {
5767 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5768 }
5769 }
5770 notice_special_calls (call_stmt);
5771 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5772 gimple_call_set_lhs (call_stmt, *to_p);
5773 else if (TREE_CODE (*to_p) == SSA_NAME)
5774 /* The above is somewhat premature, avoid ICEing later for a
5775 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5776 ??? This doesn't make it a default-def. */
5777 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5778
5779 assign = call_stmt;
5780 }
5781 else
5782 {
5783 assign = gimple_build_assign (*to_p, *from_p);
5784 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5785 if (COMPARISON_CLASS_P (*from_p))
5786 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5787 }
5788
5789 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5790 {
5791 /* We should have got an SSA name from the start. */
5792 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5793 || ! gimple_in_ssa_p (cfun));
5794 }
5795
5796 gimplify_seq_add_stmt (pre_p, assign);
5797 gsi = gsi_last (*pre_p);
5798 maybe_fold_stmt (&gsi);
5799
5800 if (want_value)
5801 {
5802 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5803 return GS_OK;
5804 }
5805 else
5806 *expr_p = NULL;
5807
5808 return GS_ALL_DONE;
5809 }
5810
5811 /* Gimplify a comparison between two variable-sized objects. Do this
5812 with a call to BUILT_IN_MEMCMP. */
5813
5814 static enum gimplify_status
5815 gimplify_variable_sized_compare (tree *expr_p)
5816 {
5817 location_t loc = EXPR_LOCATION (*expr_p);
5818 tree op0 = TREE_OPERAND (*expr_p, 0);
5819 tree op1 = TREE_OPERAND (*expr_p, 1);
5820 tree t, arg, dest, src, expr;
5821
5822 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5823 arg = unshare_expr (arg);
5824 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5825 src = build_fold_addr_expr_loc (loc, op1);
5826 dest = build_fold_addr_expr_loc (loc, op0);
5827 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5828 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5829
5830 expr
5831 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5832 SET_EXPR_LOCATION (expr, loc);
5833 *expr_p = expr;
5834
5835 return GS_OK;
5836 }
5837
5838 /* Gimplify a comparison between two aggregate objects of integral scalar
5839 mode as a comparison between the bitwise equivalent scalar values. */
5840
5841 static enum gimplify_status
5842 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5843 {
5844 location_t loc = EXPR_LOCATION (*expr_p);
5845 tree op0 = TREE_OPERAND (*expr_p, 0);
5846 tree op1 = TREE_OPERAND (*expr_p, 1);
5847
5848 tree type = TREE_TYPE (op0);
5849 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5850
5851 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5852 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5853
5854 *expr_p
5855 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5856
5857 return GS_OK;
5858 }
5859
5860 /* Gimplify an expression sequence. This function gimplifies each
5861 expression and rewrites the original expression with the last
5862 expression of the sequence in GIMPLE form.
5863
5864 PRE_P points to the list where the side effects for all the
5865 expressions in the sequence will be emitted.
5866
5867 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5868
5869 static enum gimplify_status
5870 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5871 {
5872 tree t = *expr_p;
5873
5874 do
5875 {
5876 tree *sub_p = &TREE_OPERAND (t, 0);
5877
5878 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5879 gimplify_compound_expr (sub_p, pre_p, false);
5880 else
5881 gimplify_stmt (sub_p, pre_p);
5882
5883 t = TREE_OPERAND (t, 1);
5884 }
5885 while (TREE_CODE (t) == COMPOUND_EXPR);
5886
5887 *expr_p = t;
5888 if (want_value)
5889 return GS_OK;
5890 else
5891 {
5892 gimplify_stmt (expr_p, pre_p);
5893 return GS_ALL_DONE;
5894 }
5895 }
5896
5897 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5898 gimplify. After gimplification, EXPR_P will point to a new temporary
5899 that holds the original value of the SAVE_EXPR node.
5900
5901 PRE_P points to the list where side effects that must happen before
5902 *EXPR_P should be stored. */
5903
5904 static enum gimplify_status
5905 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5906 {
5907 enum gimplify_status ret = GS_ALL_DONE;
5908 tree val;
5909
5910 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5911 val = TREE_OPERAND (*expr_p, 0);
5912
5913 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5914 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5915 {
5916 /* The operand may be a void-valued expression. It is
5917 being executed only for its side-effects. */
5918 if (TREE_TYPE (val) == void_type_node)
5919 {
5920 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5921 is_gimple_stmt, fb_none);
5922 val = NULL;
5923 }
5924 else
5925 /* The temporary may not be an SSA name as later abnormal and EH
5926 control flow may invalidate use/def domination. When in SSA
5927 form then assume there are no such issues and SAVE_EXPRs only
5928 appear via GENERIC foldings. */
5929 val = get_initialized_tmp_var (val, pre_p, post_p,
5930 gimple_in_ssa_p (cfun));
5931
5932 TREE_OPERAND (*expr_p, 0) = val;
5933 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5934 }
5935
5936 *expr_p = val;
5937
5938 return ret;
5939 }
5940
5941 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5942
5943 unary_expr
5944 : ...
5945 | '&' varname
5946 ...
5947
5948 PRE_P points to the list where side effects that must happen before
5949 *EXPR_P should be stored.
5950
5951 POST_P points to the list where side effects that must happen after
5952 *EXPR_P should be stored. */
5953
5954 static enum gimplify_status
5955 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5956 {
5957 tree expr = *expr_p;
5958 tree op0 = TREE_OPERAND (expr, 0);
5959 enum gimplify_status ret;
5960 location_t loc = EXPR_LOCATION (*expr_p);
5961
5962 switch (TREE_CODE (op0))
5963 {
5964 case INDIRECT_REF:
5965 do_indirect_ref:
5966 /* Check if we are dealing with an expression of the form '&*ptr'.
5967 While the front end folds away '&*ptr' into 'ptr', these
5968 expressions may be generated internally by the compiler (e.g.,
5969 builtins like __builtin_va_end). */
5970 /* Caution: the silent array decomposition semantics we allow for
5971 ADDR_EXPR means we can't always discard the pair. */
5972 /* Gimplification of the ADDR_EXPR operand may drop
5973 cv-qualification conversions, so make sure we add them if
5974 needed. */
5975 {
5976 tree op00 = TREE_OPERAND (op0, 0);
5977 tree t_expr = TREE_TYPE (expr);
5978 tree t_op00 = TREE_TYPE (op00);
5979
5980 if (!useless_type_conversion_p (t_expr, t_op00))
5981 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5982 *expr_p = op00;
5983 ret = GS_OK;
5984 }
5985 break;
5986
5987 case VIEW_CONVERT_EXPR:
5988 /* Take the address of our operand and then convert it to the type of
5989 this ADDR_EXPR.
5990
5991 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5992 all clear. The impact of this transformation is even less clear. */
5993
5994 /* If the operand is a useless conversion, look through it. Doing so
5995 guarantees that the ADDR_EXPR and its operand will remain of the
5996 same type. */
5997 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5998 op0 = TREE_OPERAND (op0, 0);
5999
6000 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6001 build_fold_addr_expr_loc (loc,
6002 TREE_OPERAND (op0, 0)));
6003 ret = GS_OK;
6004 break;
6005
6006 case MEM_REF:
6007 if (integer_zerop (TREE_OPERAND (op0, 1)))
6008 goto do_indirect_ref;
6009
6010 /* fall through */
6011
6012 default:
6013 /* If we see a call to a declared builtin or see its address
6014 being taken (we can unify those cases here) then we can mark
6015 the builtin for implicit generation by GCC. */
6016 if (TREE_CODE (op0) == FUNCTION_DECL
6017 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
6018 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6019 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6020
6021 /* We use fb_either here because the C frontend sometimes takes
6022 the address of a call that returns a struct; see
6023 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6024 the implied temporary explicit. */
6025
6026 /* Make the operand addressable. */
6027 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6028 is_gimple_addressable, fb_either);
6029 if (ret == GS_ERROR)
6030 break;
6031
6032 /* Then mark it. Beware that it may not be possible to do so directly
6033 if a temporary has been created by the gimplification. */
6034 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6035
6036 op0 = TREE_OPERAND (expr, 0);
6037
6038 /* For various reasons, the gimplification of the expression
6039 may have made a new INDIRECT_REF. */
6040 if (TREE_CODE (op0) == INDIRECT_REF)
6041 goto do_indirect_ref;
6042
6043 mark_addressable (TREE_OPERAND (expr, 0));
6044
6045 /* The FEs may end up building ADDR_EXPRs early on a decl with
6046 an incomplete type. Re-build ADDR_EXPRs in canonical form
6047 here. */
6048 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6049 *expr_p = build_fold_addr_expr (op0);
6050
6051 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6052 recompute_tree_invariant_for_addr_expr (*expr_p);
6053
6054 /* If we re-built the ADDR_EXPR add a conversion to the original type
6055 if required. */
6056 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6057 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6058
6059 break;
6060 }
6061
6062 return ret;
6063 }
6064
6065 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6066 value; output operands should be a gimple lvalue. */
6067
6068 static enum gimplify_status
6069 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6070 {
6071 tree expr;
6072 int noutputs;
6073 const char **oconstraints;
6074 int i;
6075 tree link;
6076 const char *constraint;
6077 bool allows_mem, allows_reg, is_inout;
6078 enum gimplify_status ret, tret;
6079 gasm *stmt;
6080 vec<tree, va_gc> *inputs;
6081 vec<tree, va_gc> *outputs;
6082 vec<tree, va_gc> *clobbers;
6083 vec<tree, va_gc> *labels;
6084 tree link_next;
6085
6086 expr = *expr_p;
6087 noutputs = list_length (ASM_OUTPUTS (expr));
6088 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6089
6090 inputs = NULL;
6091 outputs = NULL;
6092 clobbers = NULL;
6093 labels = NULL;
6094
6095 ret = GS_ALL_DONE;
6096 link_next = NULL_TREE;
6097 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6098 {
6099 bool ok;
6100 size_t constraint_len;
6101
6102 link_next = TREE_CHAIN (link);
6103
6104 oconstraints[i]
6105 = constraint
6106 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6107 constraint_len = strlen (constraint);
6108 if (constraint_len == 0)
6109 continue;
6110
6111 ok = parse_output_constraint (&constraint, i, 0, 0,
6112 &allows_mem, &allows_reg, &is_inout);
6113 if (!ok)
6114 {
6115 ret = GS_ERROR;
6116 is_inout = false;
6117 }
6118
6119 if (!allows_reg && allows_mem)
6120 mark_addressable (TREE_VALUE (link));
6121
6122 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6123 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6124 fb_lvalue | fb_mayfail);
6125 if (tret == GS_ERROR)
6126 {
6127 error ("invalid lvalue in asm output %d", i);
6128 ret = tret;
6129 }
6130
6131 /* If the constraint does not allow memory make sure we gimplify
6132 it to a register if it is not already but its base is. This
6133 happens for complex and vector components. */
6134 if (!allows_mem)
6135 {
6136 tree op = TREE_VALUE (link);
6137 if (! is_gimple_val (op)
6138 && is_gimple_reg_type (TREE_TYPE (op))
6139 && is_gimple_reg (get_base_address (op)))
6140 {
6141 tree tem = create_tmp_reg (TREE_TYPE (op));
6142 tree ass;
6143 if (is_inout)
6144 {
6145 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6146 tem, unshare_expr (op));
6147 gimplify_and_add (ass, pre_p);
6148 }
6149 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6150 gimplify_and_add (ass, post_p);
6151
6152 TREE_VALUE (link) = tem;
6153 tret = GS_OK;
6154 }
6155 }
6156
6157 vec_safe_push (outputs, link);
6158 TREE_CHAIN (link) = NULL_TREE;
6159
6160 if (is_inout)
6161 {
6162 /* An input/output operand. To give the optimizers more
6163 flexibility, split it into separate input and output
6164 operands. */
6165 tree input;
6166 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6167 char buf[11];
6168
6169 /* Turn the in/out constraint into an output constraint. */
6170 char *p = xstrdup (constraint);
6171 p[0] = '=';
6172 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6173
6174 /* And add a matching input constraint. */
6175 if (allows_reg)
6176 {
6177 sprintf (buf, "%u", i);
6178
6179 /* If there are multiple alternatives in the constraint,
6180 handle each of them individually. Those that allow register
6181 will be replaced with operand number, the others will stay
6182 unchanged. */
6183 if (strchr (p, ',') != NULL)
6184 {
6185 size_t len = 0, buflen = strlen (buf);
6186 char *beg, *end, *str, *dst;
6187
6188 for (beg = p + 1;;)
6189 {
6190 end = strchr (beg, ',');
6191 if (end == NULL)
6192 end = strchr (beg, '\0');
6193 if ((size_t) (end - beg) < buflen)
6194 len += buflen + 1;
6195 else
6196 len += end - beg + 1;
6197 if (*end)
6198 beg = end + 1;
6199 else
6200 break;
6201 }
6202
6203 str = (char *) alloca (len);
6204 for (beg = p + 1, dst = str;;)
6205 {
6206 const char *tem;
6207 bool mem_p, reg_p, inout_p;
6208
6209 end = strchr (beg, ',');
6210 if (end)
6211 *end = '\0';
6212 beg[-1] = '=';
6213 tem = beg - 1;
6214 parse_output_constraint (&tem, i, 0, 0,
6215 &mem_p, &reg_p, &inout_p);
6216 if (dst != str)
6217 *dst++ = ',';
6218 if (reg_p)
6219 {
6220 memcpy (dst, buf, buflen);
6221 dst += buflen;
6222 }
6223 else
6224 {
6225 if (end)
6226 len = end - beg;
6227 else
6228 len = strlen (beg);
6229 memcpy (dst, beg, len);
6230 dst += len;
6231 }
6232 if (end)
6233 beg = end + 1;
6234 else
6235 break;
6236 }
6237 *dst = '\0';
6238 input = build_string (dst - str, str);
6239 }
6240 else
6241 input = build_string (strlen (buf), buf);
6242 }
6243 else
6244 input = build_string (constraint_len - 1, constraint + 1);
6245
6246 free (p);
6247
6248 input = build_tree_list (build_tree_list (NULL_TREE, input),
6249 unshare_expr (TREE_VALUE (link)));
6250 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6251 }
6252 }
6253
6254 link_next = NULL_TREE;
6255 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6256 {
6257 link_next = TREE_CHAIN (link);
6258 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6259 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6260 oconstraints, &allows_mem, &allows_reg);
6261
6262 /* If we can't make copies, we can only accept memory. */
6263 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6264 {
6265 if (allows_mem)
6266 allows_reg = 0;
6267 else
6268 {
6269 error ("impossible constraint in %<asm%>");
6270 error ("non-memory input %d must stay in memory", i);
6271 return GS_ERROR;
6272 }
6273 }
6274
6275 /* If the operand is a memory input, it should be an lvalue. */
6276 if (!allows_reg && allows_mem)
6277 {
6278 tree inputv = TREE_VALUE (link);
6279 STRIP_NOPS (inputv);
6280 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6281 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6282 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6283 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6284 || TREE_CODE (inputv) == MODIFY_EXPR)
6285 TREE_VALUE (link) = error_mark_node;
6286 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6287 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6288 if (tret != GS_ERROR)
6289 {
6290 /* Unlike output operands, memory inputs are not guaranteed
6291 to be lvalues by the FE, and while the expressions are
6292 marked addressable there, if it is e.g. a statement
6293 expression, temporaries in it might not end up being
6294 addressable. They might be already used in the IL and thus
6295 it is too late to make them addressable now though. */
6296 tree x = TREE_VALUE (link);
6297 while (handled_component_p (x))
6298 x = TREE_OPERAND (x, 0);
6299 if (TREE_CODE (x) == MEM_REF
6300 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6301 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6302 if ((VAR_P (x)
6303 || TREE_CODE (x) == PARM_DECL
6304 || TREE_CODE (x) == RESULT_DECL)
6305 && !TREE_ADDRESSABLE (x)
6306 && is_gimple_reg (x))
6307 {
6308 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6309 input_location), 0,
6310 "memory input %d is not directly addressable",
6311 i);
6312 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6313 }
6314 }
6315 mark_addressable (TREE_VALUE (link));
6316 if (tret == GS_ERROR)
6317 {
6318 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6319 "memory input %d is not directly addressable", i);
6320 ret = tret;
6321 }
6322 }
6323 else
6324 {
6325 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6326 is_gimple_asm_val, fb_rvalue);
6327 if (tret == GS_ERROR)
6328 ret = tret;
6329 }
6330
6331 TREE_CHAIN (link) = NULL_TREE;
6332 vec_safe_push (inputs, link);
6333 }
6334
6335 link_next = NULL_TREE;
6336 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6337 {
6338 link_next = TREE_CHAIN (link);
6339 TREE_CHAIN (link) = NULL_TREE;
6340 vec_safe_push (clobbers, link);
6341 }
6342
6343 link_next = NULL_TREE;
6344 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6345 {
6346 link_next = TREE_CHAIN (link);
6347 TREE_CHAIN (link) = NULL_TREE;
6348 vec_safe_push (labels, link);
6349 }
6350
6351 /* Do not add ASMs with errors to the gimple IL stream. */
6352 if (ret != GS_ERROR)
6353 {
6354 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6355 inputs, outputs, clobbers, labels);
6356
6357 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6358 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6359
6360 gimplify_seq_add_stmt (pre_p, stmt);
6361 }
6362
6363 return ret;
6364 }
6365
6366 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6367 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6368 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6369 return to this function.
6370
6371 FIXME should we complexify the prequeue handling instead? Or use flags
6372 for all the cleanups and let the optimizer tighten them up? The current
6373 code seems pretty fragile; it will break on a cleanup within any
6374 non-conditional nesting. But any such nesting would be broken, anyway;
6375 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6376 and continues out of it. We can do that at the RTL level, though, so
6377 having an optimizer to tighten up try/finally regions would be a Good
6378 Thing. */
6379
6380 static enum gimplify_status
6381 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6382 {
6383 gimple_stmt_iterator iter;
6384 gimple_seq body_sequence = NULL;
6385
6386 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6387
6388 /* We only care about the number of conditions between the innermost
6389 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6390 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6391 int old_conds = gimplify_ctxp->conditions;
6392 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6393 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6394 gimplify_ctxp->conditions = 0;
6395 gimplify_ctxp->conditional_cleanups = NULL;
6396 gimplify_ctxp->in_cleanup_point_expr = true;
6397
6398 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6399
6400 gimplify_ctxp->conditions = old_conds;
6401 gimplify_ctxp->conditional_cleanups = old_cleanups;
6402 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6403
6404 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6405 {
6406 gimple *wce = gsi_stmt (iter);
6407
6408 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6409 {
6410 if (gsi_one_before_end_p (iter))
6411 {
6412 /* Note that gsi_insert_seq_before and gsi_remove do not
6413 scan operands, unlike some other sequence mutators. */
6414 if (!gimple_wce_cleanup_eh_only (wce))
6415 gsi_insert_seq_before_without_update (&iter,
6416 gimple_wce_cleanup (wce),
6417 GSI_SAME_STMT);
6418 gsi_remove (&iter, true);
6419 break;
6420 }
6421 else
6422 {
6423 gtry *gtry;
6424 gimple_seq seq;
6425 enum gimple_try_flags kind;
6426
6427 if (gimple_wce_cleanup_eh_only (wce))
6428 kind = GIMPLE_TRY_CATCH;
6429 else
6430 kind = GIMPLE_TRY_FINALLY;
6431 seq = gsi_split_seq_after (iter);
6432
6433 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6434 /* Do not use gsi_replace here, as it may scan operands.
6435 We want to do a simple structural modification only. */
6436 gsi_set_stmt (&iter, gtry);
6437 iter = gsi_start (gtry->eval);
6438 }
6439 }
6440 else
6441 gsi_next (&iter);
6442 }
6443
6444 gimplify_seq_add_seq (pre_p, body_sequence);
6445 if (temp)
6446 {
6447 *expr_p = temp;
6448 return GS_OK;
6449 }
6450 else
6451 {
6452 *expr_p = NULL;
6453 return GS_ALL_DONE;
6454 }
6455 }
6456
6457 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6458 is the cleanup action required. EH_ONLY is true if the cleanup should
6459 only be executed if an exception is thrown, not on normal exit.
6460 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6461 only valid for clobbers. */
6462
6463 static void
6464 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6465 bool force_uncond = false)
6466 {
6467 gimple *wce;
6468 gimple_seq cleanup_stmts = NULL;
6469
6470 /* Errors can result in improperly nested cleanups. Which results in
6471 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6472 if (seen_error ())
6473 return;
6474
6475 if (gimple_conditional_context ())
6476 {
6477 /* If we're in a conditional context, this is more complex. We only
6478 want to run the cleanup if we actually ran the initialization that
6479 necessitates it, but we want to run it after the end of the
6480 conditional context. So we wrap the try/finally around the
6481 condition and use a flag to determine whether or not to actually
6482 run the destructor. Thus
6483
6484 test ? f(A()) : 0
6485
6486 becomes (approximately)
6487
6488 flag = 0;
6489 try {
6490 if (test) { A::A(temp); flag = 1; val = f(temp); }
6491 else { val = 0; }
6492 } finally {
6493 if (flag) A::~A(temp);
6494 }
6495 val
6496 */
6497 if (force_uncond)
6498 {
6499 gimplify_stmt (&cleanup, &cleanup_stmts);
6500 wce = gimple_build_wce (cleanup_stmts);
6501 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6502 }
6503 else
6504 {
6505 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6506 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6507 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6508
6509 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6510 gimplify_stmt (&cleanup, &cleanup_stmts);
6511 wce = gimple_build_wce (cleanup_stmts);
6512
6513 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6514 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6515 gimplify_seq_add_stmt (pre_p, ftrue);
6516
6517 /* Because of this manipulation, and the EH edges that jump
6518 threading cannot redirect, the temporary (VAR) will appear
6519 to be used uninitialized. Don't warn. */
6520 TREE_NO_WARNING (var) = 1;
6521 }
6522 }
6523 else
6524 {
6525 gimplify_stmt (&cleanup, &cleanup_stmts);
6526 wce = gimple_build_wce (cleanup_stmts);
6527 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6528 gimplify_seq_add_stmt (pre_p, wce);
6529 }
6530 }
6531
6532 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6533
6534 static enum gimplify_status
6535 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6536 {
6537 tree targ = *expr_p;
6538 tree temp = TARGET_EXPR_SLOT (targ);
6539 tree init = TARGET_EXPR_INITIAL (targ);
6540 enum gimplify_status ret;
6541
6542 bool unpoison_empty_seq = false;
6543 gimple_stmt_iterator unpoison_it;
6544
6545 if (init)
6546 {
6547 tree cleanup = NULL_TREE;
6548
6549 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6550 to the temps list. Handle also variable length TARGET_EXPRs. */
6551 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6552 {
6553 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6554 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6555 gimplify_vla_decl (temp, pre_p);
6556 }
6557 else
6558 {
6559 /* Save location where we need to place unpoisoning. It's possible
6560 that a variable will be converted to needs_to_live_in_memory. */
6561 unpoison_it = gsi_last (*pre_p);
6562 unpoison_empty_seq = gsi_end_p (unpoison_it);
6563
6564 gimple_add_tmp_var (temp);
6565 }
6566
6567 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6568 expression is supposed to initialize the slot. */
6569 if (VOID_TYPE_P (TREE_TYPE (init)))
6570 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6571 else
6572 {
6573 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6574 init = init_expr;
6575 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6576 init = NULL;
6577 ggc_free (init_expr);
6578 }
6579 if (ret == GS_ERROR)
6580 {
6581 /* PR c++/28266 Make sure this is expanded only once. */
6582 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6583 return GS_ERROR;
6584 }
6585 if (init)
6586 gimplify_and_add (init, pre_p);
6587
6588 /* If needed, push the cleanup for the temp. */
6589 if (TARGET_EXPR_CLEANUP (targ))
6590 {
6591 if (CLEANUP_EH_ONLY (targ))
6592 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6593 CLEANUP_EH_ONLY (targ), pre_p);
6594 else
6595 cleanup = TARGET_EXPR_CLEANUP (targ);
6596 }
6597
6598 /* Add a clobber for the temporary going out of scope, like
6599 gimplify_bind_expr. */
6600 if (gimplify_ctxp->in_cleanup_point_expr
6601 && needs_to_live_in_memory (temp))
6602 {
6603 if (flag_stack_reuse == SR_ALL)
6604 {
6605 tree clobber = build_clobber (TREE_TYPE (temp));
6606 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6607 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6608 }
6609 if (asan_poisoned_variables
6610 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6611 && dbg_cnt (asan_use_after_scope)
6612 && !gimplify_omp_ctxp)
6613 {
6614 tree asan_cleanup = build_asan_poison_call_expr (temp);
6615 if (asan_cleanup)
6616 {
6617 if (unpoison_empty_seq)
6618 unpoison_it = gsi_start (*pre_p);
6619
6620 asan_poison_variable (temp, false, &unpoison_it,
6621 unpoison_empty_seq);
6622 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6623 }
6624 }
6625 }
6626 if (cleanup)
6627 gimple_push_cleanup (temp, cleanup, false, pre_p);
6628
6629 /* Only expand this once. */
6630 TREE_OPERAND (targ, 3) = init;
6631 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6632 }
6633 else
6634 /* We should have expanded this before. */
6635 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6636
6637 *expr_p = temp;
6638 return GS_OK;
6639 }
6640
6641 /* Gimplification of expression trees. */
6642
6643 /* Gimplify an expression which appears at statement context. The
6644 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6645 NULL, a new sequence is allocated.
6646
6647 Return true if we actually added a statement to the queue. */
6648
6649 bool
6650 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6651 {
6652 gimple_seq_node last;
6653
6654 last = gimple_seq_last (*seq_p);
6655 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6656 return last != gimple_seq_last (*seq_p);
6657 }
6658
6659 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6660 to CTX. If entries already exist, force them to be some flavor of private.
6661 If there is no enclosing parallel, do nothing. */
6662
6663 void
6664 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6665 {
6666 splay_tree_node n;
6667
6668 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6669 return;
6670
6671 do
6672 {
6673 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6674 if (n != NULL)
6675 {
6676 if (n->value & GOVD_SHARED)
6677 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6678 else if (n->value & GOVD_MAP)
6679 n->value |= GOVD_MAP_TO_ONLY;
6680 else
6681 return;
6682 }
6683 else if ((ctx->region_type & ORT_TARGET) != 0)
6684 {
6685 if (ctx->target_map_scalars_firstprivate)
6686 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6687 else
6688 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6689 }
6690 else if (ctx->region_type != ORT_WORKSHARE
6691 && ctx->region_type != ORT_SIMD
6692 && ctx->region_type != ORT_ACC
6693 && !(ctx->region_type & ORT_TARGET_DATA))
6694 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6695
6696 ctx = ctx->outer_context;
6697 }
6698 while (ctx);
6699 }
6700
6701 /* Similarly for each of the type sizes of TYPE. */
6702
6703 static void
6704 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6705 {
6706 if (type == NULL || type == error_mark_node)
6707 return;
6708 type = TYPE_MAIN_VARIANT (type);
6709
6710 if (ctx->privatized_types->add (type))
6711 return;
6712
6713 switch (TREE_CODE (type))
6714 {
6715 case INTEGER_TYPE:
6716 case ENUMERAL_TYPE:
6717 case BOOLEAN_TYPE:
6718 case REAL_TYPE:
6719 case FIXED_POINT_TYPE:
6720 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6721 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6722 break;
6723
6724 case ARRAY_TYPE:
6725 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6726 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6727 break;
6728
6729 case RECORD_TYPE:
6730 case UNION_TYPE:
6731 case QUAL_UNION_TYPE:
6732 {
6733 tree field;
6734 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6735 if (TREE_CODE (field) == FIELD_DECL)
6736 {
6737 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6738 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6739 }
6740 }
6741 break;
6742
6743 case POINTER_TYPE:
6744 case REFERENCE_TYPE:
6745 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6746 break;
6747
6748 default:
6749 break;
6750 }
6751
6752 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6753 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6754 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6755 }
6756
6757 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6758
6759 static void
6760 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6761 {
6762 splay_tree_node n;
6763 unsigned int nflags;
6764 tree t;
6765
6766 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6767 return;
6768
6769 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6770 there are constructors involved somewhere. Exception is a shared clause,
6771 there is nothing privatized in that case. */
6772 if ((flags & GOVD_SHARED) == 0
6773 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6774 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6775 flags |= GOVD_SEEN;
6776
6777 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6778 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6779 {
6780 /* We shouldn't be re-adding the decl with the same data
6781 sharing class. */
6782 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6783 nflags = n->value | flags;
6784 /* The only combination of data sharing classes we should see is
6785 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6786 reduction variables to be used in data sharing clauses. */
6787 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6788 || ((nflags & GOVD_DATA_SHARE_CLASS)
6789 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6790 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6791 n->value = nflags;
6792 return;
6793 }
6794
6795 /* When adding a variable-sized variable, we have to handle all sorts
6796 of additional bits of data: the pointer replacement variable, and
6797 the parameters of the type. */
6798 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6799 {
6800 /* Add the pointer replacement variable as PRIVATE if the variable
6801 replacement is private, else FIRSTPRIVATE since we'll need the
6802 address of the original variable either for SHARED, or for the
6803 copy into or out of the context. */
6804 if (!(flags & GOVD_LOCAL))
6805 {
6806 if (flags & GOVD_MAP)
6807 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6808 else if (flags & GOVD_PRIVATE)
6809 nflags = GOVD_PRIVATE;
6810 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6811 && (flags & GOVD_FIRSTPRIVATE))
6812 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6813 else
6814 nflags = GOVD_FIRSTPRIVATE;
6815 nflags |= flags & GOVD_SEEN;
6816 t = DECL_VALUE_EXPR (decl);
6817 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6818 t = TREE_OPERAND (t, 0);
6819 gcc_assert (DECL_P (t));
6820 omp_add_variable (ctx, t, nflags);
6821 }
6822
6823 /* Add all of the variable and type parameters (which should have
6824 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6825 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6826 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6827 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6828
6829 /* The variable-sized variable itself is never SHARED, only some form
6830 of PRIVATE. The sharing would take place via the pointer variable
6831 which we remapped above. */
6832 if (flags & GOVD_SHARED)
6833 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6834 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6835
6836 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6837 alloca statement we generate for the variable, so make sure it
6838 is available. This isn't automatically needed for the SHARED
6839 case, since we won't be allocating local storage then.
6840 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6841 in this case omp_notice_variable will be called later
6842 on when it is gimplified. */
6843 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6844 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6845 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6846 }
6847 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6848 && lang_hooks.decls.omp_privatize_by_reference (decl))
6849 {
6850 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6851
6852 /* Similar to the direct variable sized case above, we'll need the
6853 size of references being privatized. */
6854 if ((flags & GOVD_SHARED) == 0)
6855 {
6856 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6857 if (DECL_P (t))
6858 omp_notice_variable (ctx, t, true);
6859 }
6860 }
6861
6862 if (n != NULL)
6863 n->value |= flags;
6864 else
6865 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6866
6867 /* For reductions clauses in OpenACC loop directives, by default create a
6868 copy clause on the enclosing parallel construct for carrying back the
6869 results. */
6870 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6871 {
6872 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6873 while (outer_ctx)
6874 {
6875 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6876 if (n != NULL)
6877 {
6878 /* Ignore local variables and explicitly declared clauses. */
6879 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6880 break;
6881 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6882 {
6883 /* According to the OpenACC spec, such a reduction variable
6884 should already have a copy map on a kernels construct,
6885 verify that here. */
6886 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6887 && (n->value & GOVD_MAP));
6888 }
6889 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6890 {
6891 /* Remove firstprivate and make it a copy map. */
6892 n->value &= ~GOVD_FIRSTPRIVATE;
6893 n->value |= GOVD_MAP;
6894 }
6895 }
6896 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6897 {
6898 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6899 GOVD_MAP | GOVD_SEEN);
6900 break;
6901 }
6902 outer_ctx = outer_ctx->outer_context;
6903 }
6904 }
6905 }
6906
6907 /* Notice a threadprivate variable DECL used in OMP context CTX.
6908 This just prints out diagnostics about threadprivate variable uses
6909 in untied tasks. If DECL2 is non-NULL, prevent this warning
6910 on that variable. */
6911
6912 static bool
6913 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6914 tree decl2)
6915 {
6916 splay_tree_node n;
6917 struct gimplify_omp_ctx *octx;
6918
6919 for (octx = ctx; octx; octx = octx->outer_context)
6920 if ((octx->region_type & ORT_TARGET) != 0)
6921 {
6922 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6923 if (n == NULL)
6924 {
6925 error ("threadprivate variable %qE used in target region",
6926 DECL_NAME (decl));
6927 error_at (octx->location, "enclosing target region");
6928 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6929 }
6930 if (decl2)
6931 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6932 }
6933
6934 if (ctx->region_type != ORT_UNTIED_TASK)
6935 return false;
6936 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6937 if (n == NULL)
6938 {
6939 error ("threadprivate variable %qE used in untied task",
6940 DECL_NAME (decl));
6941 error_at (ctx->location, "enclosing task");
6942 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6943 }
6944 if (decl2)
6945 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6946 return false;
6947 }
6948
6949 /* Return true if global var DECL is device resident. */
6950
6951 static bool
6952 device_resident_p (tree decl)
6953 {
6954 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6955
6956 if (!attr)
6957 return false;
6958
6959 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6960 {
6961 tree c = TREE_VALUE (t);
6962 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6963 return true;
6964 }
6965
6966 return false;
6967 }
6968
6969 /* Return true if DECL has an ACC DECLARE attribute. */
6970
6971 static bool
6972 is_oacc_declared (tree decl)
6973 {
6974 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6975 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6976 return declared != NULL_TREE;
6977 }
6978
6979 /* Determine outer default flags for DECL mentioned in an OMP region
6980 but not declared in an enclosing clause.
6981
6982 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6983 remapped firstprivate instead of shared. To some extent this is
6984 addressed in omp_firstprivatize_type_sizes, but not
6985 effectively. */
6986
6987 static unsigned
6988 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6989 bool in_code, unsigned flags)
6990 {
6991 enum omp_clause_default_kind default_kind = ctx->default_kind;
6992 enum omp_clause_default_kind kind;
6993
6994 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6995 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6996 default_kind = kind;
6997
6998 switch (default_kind)
6999 {
7000 case OMP_CLAUSE_DEFAULT_NONE:
7001 {
7002 const char *rtype;
7003
7004 if (ctx->region_type & ORT_PARALLEL)
7005 rtype = "parallel";
7006 else if (ctx->region_type & ORT_TASK)
7007 rtype = "task";
7008 else if (ctx->region_type & ORT_TEAMS)
7009 rtype = "teams";
7010 else
7011 gcc_unreachable ();
7012
7013 error ("%qE not specified in enclosing %qs",
7014 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7015 error_at (ctx->location, "enclosing %qs", rtype);
7016 }
7017 /* FALLTHRU */
7018 case OMP_CLAUSE_DEFAULT_SHARED:
7019 flags |= GOVD_SHARED;
7020 break;
7021 case OMP_CLAUSE_DEFAULT_PRIVATE:
7022 flags |= GOVD_PRIVATE;
7023 break;
7024 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7025 flags |= GOVD_FIRSTPRIVATE;
7026 break;
7027 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7028 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7029 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7030 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7031 {
7032 omp_notice_variable (octx, decl, in_code);
7033 for (; octx; octx = octx->outer_context)
7034 {
7035 splay_tree_node n2;
7036
7037 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7038 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7039 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7040 continue;
7041 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7042 {
7043 flags |= GOVD_FIRSTPRIVATE;
7044 goto found_outer;
7045 }
7046 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7047 {
7048 flags |= GOVD_SHARED;
7049 goto found_outer;
7050 }
7051 }
7052 }
7053
7054 if (TREE_CODE (decl) == PARM_DECL
7055 || (!is_global_var (decl)
7056 && DECL_CONTEXT (decl) == current_function_decl))
7057 flags |= GOVD_FIRSTPRIVATE;
7058 else
7059 flags |= GOVD_SHARED;
7060 found_outer:
7061 break;
7062
7063 default:
7064 gcc_unreachable ();
7065 }
7066
7067 return flags;
7068 }
7069
7070
7071 /* Determine outer default flags for DECL mentioned in an OACC region
7072 but not declared in an enclosing clause. */
7073
7074 static unsigned
7075 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7076 {
7077 const char *rkind;
7078 bool on_device = false;
7079 bool declared = is_oacc_declared (decl);
7080 tree type = TREE_TYPE (decl);
7081
7082 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7083 type = TREE_TYPE (type);
7084
7085 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7086 && is_global_var (decl)
7087 && device_resident_p (decl))
7088 {
7089 on_device = true;
7090 flags |= GOVD_MAP_TO_ONLY;
7091 }
7092
7093 switch (ctx->region_type)
7094 {
7095 case ORT_ACC_KERNELS:
7096 rkind = "kernels";
7097
7098 if (AGGREGATE_TYPE_P (type))
7099 {
7100 /* Aggregates default to 'present_or_copy', or 'present'. */
7101 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7102 flags |= GOVD_MAP;
7103 else
7104 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7105 }
7106 else
7107 /* Scalars default to 'copy'. */
7108 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7109
7110 break;
7111
7112 case ORT_ACC_PARALLEL:
7113 rkind = "parallel";
7114
7115 if (on_device || declared)
7116 flags |= GOVD_MAP;
7117 else if (AGGREGATE_TYPE_P (type))
7118 {
7119 /* Aggregates default to 'present_or_copy', or 'present'. */
7120 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7121 flags |= GOVD_MAP;
7122 else
7123 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7124 }
7125 else
7126 /* Scalars default to 'firstprivate'. */
7127 flags |= GOVD_FIRSTPRIVATE;
7128
7129 break;
7130
7131 default:
7132 gcc_unreachable ();
7133 }
7134
7135 if (DECL_ARTIFICIAL (decl))
7136 ; /* We can get compiler-generated decls, and should not complain
7137 about them. */
7138 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7139 {
7140 error ("%qE not specified in enclosing OpenACC %qs construct",
7141 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7142 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7143 }
7144 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7145 ; /* Handled above. */
7146 else
7147 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7148
7149 return flags;
7150 }
7151
7152 /* Record the fact that DECL was used within the OMP context CTX.
7153 IN_CODE is true when real code uses DECL, and false when we should
7154 merely emit default(none) errors. Return true if DECL is going to
7155 be remapped and thus DECL shouldn't be gimplified into its
7156 DECL_VALUE_EXPR (if any). */
7157
7158 static bool
7159 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7160 {
7161 splay_tree_node n;
7162 unsigned flags = in_code ? GOVD_SEEN : 0;
7163 bool ret = false, shared;
7164
7165 if (error_operand_p (decl))
7166 return false;
7167
7168 if (ctx->region_type == ORT_NONE)
7169 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7170
7171 if (is_global_var (decl))
7172 {
7173 /* Threadprivate variables are predetermined. */
7174 if (DECL_THREAD_LOCAL_P (decl))
7175 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7176
7177 if (DECL_HAS_VALUE_EXPR_P (decl))
7178 {
7179 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7180
7181 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7182 return omp_notice_threadprivate_variable (ctx, decl, value);
7183 }
7184
7185 if (gimplify_omp_ctxp->outer_context == NULL
7186 && VAR_P (decl)
7187 && oacc_get_fn_attrib (current_function_decl))
7188 {
7189 location_t loc = DECL_SOURCE_LOCATION (decl);
7190
7191 if (lookup_attribute ("omp declare target link",
7192 DECL_ATTRIBUTES (decl)))
7193 {
7194 error_at (loc,
7195 "%qE with %<link%> clause used in %<routine%> function",
7196 DECL_NAME (decl));
7197 return false;
7198 }
7199 else if (!lookup_attribute ("omp declare target",
7200 DECL_ATTRIBUTES (decl)))
7201 {
7202 error_at (loc,
7203 "%qE requires a %<declare%> directive for use "
7204 "in a %<routine%> function", DECL_NAME (decl));
7205 return false;
7206 }
7207 }
7208 }
7209
7210 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7211 if ((ctx->region_type & ORT_TARGET) != 0)
7212 {
7213 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7214 if (n == NULL)
7215 {
7216 unsigned nflags = flags;
7217 if (ctx->target_map_pointers_as_0len_arrays
7218 || ctx->target_map_scalars_firstprivate)
7219 {
7220 bool is_declare_target = false;
7221 bool is_scalar = false;
7222 if (is_global_var (decl)
7223 && varpool_node::get_create (decl)->offloadable)
7224 {
7225 struct gimplify_omp_ctx *octx;
7226 for (octx = ctx->outer_context;
7227 octx; octx = octx->outer_context)
7228 {
7229 n = splay_tree_lookup (octx->variables,
7230 (splay_tree_key)decl);
7231 if (n
7232 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7233 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7234 break;
7235 }
7236 is_declare_target = octx == NULL;
7237 }
7238 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7239 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7240 if (is_declare_target)
7241 ;
7242 else if (ctx->target_map_pointers_as_0len_arrays
7243 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7244 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7245 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7246 == POINTER_TYPE)))
7247 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7248 else if (is_scalar)
7249 nflags |= GOVD_FIRSTPRIVATE;
7250 }
7251
7252 struct gimplify_omp_ctx *octx = ctx->outer_context;
7253 if ((ctx->region_type & ORT_ACC) && octx)
7254 {
7255 /* Look in outer OpenACC contexts, to see if there's a
7256 data attribute for this variable. */
7257 omp_notice_variable (octx, decl, in_code);
7258
7259 for (; octx; octx = octx->outer_context)
7260 {
7261 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7262 break;
7263 splay_tree_node n2
7264 = splay_tree_lookup (octx->variables,
7265 (splay_tree_key) decl);
7266 if (n2)
7267 {
7268 if (octx->region_type == ORT_ACC_HOST_DATA)
7269 error ("variable %qE declared in enclosing "
7270 "%<host_data%> region", DECL_NAME (decl));
7271 nflags |= GOVD_MAP;
7272 if (octx->region_type == ORT_ACC_DATA
7273 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7274 nflags |= GOVD_MAP_0LEN_ARRAY;
7275 goto found_outer;
7276 }
7277 }
7278 }
7279
7280 {
7281 tree type = TREE_TYPE (decl);
7282
7283 if (nflags == flags
7284 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7285 && lang_hooks.decls.omp_privatize_by_reference (decl))
7286 type = TREE_TYPE (type);
7287 if (nflags == flags
7288 && !lang_hooks.types.omp_mappable_type (type))
7289 {
7290 error ("%qD referenced in target region does not have "
7291 "a mappable type", decl);
7292 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7293 }
7294 else if (nflags == flags)
7295 {
7296 if ((ctx->region_type & ORT_ACC) != 0)
7297 nflags = oacc_default_clause (ctx, decl, flags);
7298 else
7299 nflags |= GOVD_MAP;
7300 }
7301 }
7302 found_outer:
7303 omp_add_variable (ctx, decl, nflags);
7304 }
7305 else
7306 {
7307 /* If nothing changed, there's nothing left to do. */
7308 if ((n->value & flags) == flags)
7309 return ret;
7310 flags |= n->value;
7311 n->value = flags;
7312 }
7313 goto do_outer;
7314 }
7315
7316 if (n == NULL)
7317 {
7318 if (ctx->region_type == ORT_WORKSHARE
7319 || ctx->region_type == ORT_SIMD
7320 || ctx->region_type == ORT_ACC
7321 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7322 goto do_outer;
7323
7324 flags = omp_default_clause (ctx, decl, in_code, flags);
7325
7326 if ((flags & GOVD_PRIVATE)
7327 && lang_hooks.decls.omp_private_outer_ref (decl))
7328 flags |= GOVD_PRIVATE_OUTER_REF;
7329
7330 omp_add_variable (ctx, decl, flags);
7331
7332 shared = (flags & GOVD_SHARED) != 0;
7333 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7334 goto do_outer;
7335 }
7336
7337 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7338 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7339 && DECL_SIZE (decl))
7340 {
7341 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7342 {
7343 splay_tree_node n2;
7344 tree t = DECL_VALUE_EXPR (decl);
7345 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7346 t = TREE_OPERAND (t, 0);
7347 gcc_assert (DECL_P (t));
7348 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7349 n2->value |= GOVD_SEEN;
7350 }
7351 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7352 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7353 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7354 != INTEGER_CST))
7355 {
7356 splay_tree_node n2;
7357 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7358 gcc_assert (DECL_P (t));
7359 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7360 if (n2)
7361 omp_notice_variable (ctx, t, true);
7362 }
7363 }
7364
7365 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7366 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7367
7368 /* If nothing changed, there's nothing left to do. */
7369 if ((n->value & flags) == flags)
7370 return ret;
7371 flags |= n->value;
7372 n->value = flags;
7373
7374 do_outer:
7375 /* If the variable is private in the current context, then we don't
7376 need to propagate anything to an outer context. */
7377 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7378 return ret;
7379 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7380 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7381 return ret;
7382 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7383 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7384 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7385 return ret;
7386 if (ctx->outer_context
7387 && omp_notice_variable (ctx->outer_context, decl, in_code))
7388 return true;
7389 return ret;
7390 }
7391
7392 /* Verify that DECL is private within CTX. If there's specific information
7393 to the contrary in the innermost scope, generate an error. */
7394
7395 static bool
7396 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7397 {
7398 splay_tree_node n;
7399
7400 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7401 if (n != NULL)
7402 {
7403 if (n->value & GOVD_SHARED)
7404 {
7405 if (ctx == gimplify_omp_ctxp)
7406 {
7407 if (simd)
7408 error ("iteration variable %qE is predetermined linear",
7409 DECL_NAME (decl));
7410 else
7411 error ("iteration variable %qE should be private",
7412 DECL_NAME (decl));
7413 n->value = GOVD_PRIVATE;
7414 return true;
7415 }
7416 else
7417 return false;
7418 }
7419 else if ((n->value & GOVD_EXPLICIT) != 0
7420 && (ctx == gimplify_omp_ctxp
7421 || (ctx->region_type == ORT_COMBINED_PARALLEL
7422 && gimplify_omp_ctxp->outer_context == ctx)))
7423 {
7424 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7425 error ("iteration variable %qE should not be firstprivate",
7426 DECL_NAME (decl));
7427 else if ((n->value & GOVD_REDUCTION) != 0)
7428 error ("iteration variable %qE should not be reduction",
7429 DECL_NAME (decl));
7430 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7431 error ("iteration variable %qE should not be linear",
7432 DECL_NAME (decl));
7433 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7434 error ("iteration variable %qE should not be lastprivate",
7435 DECL_NAME (decl));
7436 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7437 error ("iteration variable %qE should not be private",
7438 DECL_NAME (decl));
7439 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7440 error ("iteration variable %qE is predetermined linear",
7441 DECL_NAME (decl));
7442 }
7443 return (ctx == gimplify_omp_ctxp
7444 || (ctx->region_type == ORT_COMBINED_PARALLEL
7445 && gimplify_omp_ctxp->outer_context == ctx));
7446 }
7447
7448 if (ctx->region_type != ORT_WORKSHARE
7449 && ctx->region_type != ORT_SIMD
7450 && ctx->region_type != ORT_ACC)
7451 return false;
7452 else if (ctx->outer_context)
7453 return omp_is_private (ctx->outer_context, decl, simd);
7454 return false;
7455 }
7456
7457 /* Return true if DECL is private within a parallel region
7458 that binds to the current construct's context or in parallel
7459 region's REDUCTION clause. */
7460
7461 static bool
7462 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7463 {
7464 splay_tree_node n;
7465
7466 do
7467 {
7468 ctx = ctx->outer_context;
7469 if (ctx == NULL)
7470 {
7471 if (is_global_var (decl))
7472 return false;
7473
7474 /* References might be private, but might be shared too,
7475 when checking for copyprivate, assume they might be
7476 private, otherwise assume they might be shared. */
7477 if (copyprivate)
7478 return true;
7479
7480 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7481 return false;
7482
7483 /* Treat C++ privatized non-static data members outside
7484 of the privatization the same. */
7485 if (omp_member_access_dummy_var (decl))
7486 return false;
7487
7488 return true;
7489 }
7490
7491 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7492
7493 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7494 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7495 continue;
7496
7497 if (n != NULL)
7498 {
7499 if ((n->value & GOVD_LOCAL) != 0
7500 && omp_member_access_dummy_var (decl))
7501 return false;
7502 return (n->value & GOVD_SHARED) == 0;
7503 }
7504 }
7505 while (ctx->region_type == ORT_WORKSHARE
7506 || ctx->region_type == ORT_SIMD
7507 || ctx->region_type == ORT_ACC);
7508 return false;
7509 }
7510
7511 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7512
7513 static tree
7514 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7515 {
7516 tree t = *tp;
7517
7518 /* If this node has been visited, unmark it and keep looking. */
7519 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7520 return t;
7521
7522 if (IS_TYPE_OR_DECL_P (t))
7523 *walk_subtrees = 0;
7524 return NULL_TREE;
7525 }
7526
7527 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7528 and previous omp contexts. */
7529
7530 static void
7531 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7532 enum omp_region_type region_type,
7533 enum tree_code code)
7534 {
7535 struct gimplify_omp_ctx *ctx, *outer_ctx;
7536 tree c;
7537 hash_map<tree, tree> *struct_map_to_clause = NULL;
7538 tree *prev_list_p = NULL;
7539
7540 ctx = new_omp_context (region_type);
7541 outer_ctx = ctx->outer_context;
7542 if (code == OMP_TARGET)
7543 {
7544 if (!lang_GNU_Fortran ())
7545 ctx->target_map_pointers_as_0len_arrays = true;
7546 ctx->target_map_scalars_firstprivate = true;
7547 }
7548 if (!lang_GNU_Fortran ())
7549 switch (code)
7550 {
7551 case OMP_TARGET:
7552 case OMP_TARGET_DATA:
7553 case OMP_TARGET_ENTER_DATA:
7554 case OMP_TARGET_EXIT_DATA:
7555 case OACC_DECLARE:
7556 case OACC_HOST_DATA:
7557 ctx->target_firstprivatize_array_bases = true;
7558 default:
7559 break;
7560 }
7561
7562 while ((c = *list_p) != NULL)
7563 {
7564 bool remove = false;
7565 bool notice_outer = true;
7566 const char *check_non_private = NULL;
7567 unsigned int flags;
7568 tree decl;
7569
7570 switch (OMP_CLAUSE_CODE (c))
7571 {
7572 case OMP_CLAUSE_PRIVATE:
7573 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7574 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7575 {
7576 flags |= GOVD_PRIVATE_OUTER_REF;
7577 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7578 }
7579 else
7580 notice_outer = false;
7581 goto do_add;
7582 case OMP_CLAUSE_SHARED:
7583 flags = GOVD_SHARED | GOVD_EXPLICIT;
7584 goto do_add;
7585 case OMP_CLAUSE_FIRSTPRIVATE:
7586 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7587 check_non_private = "firstprivate";
7588 goto do_add;
7589 case OMP_CLAUSE_LASTPRIVATE:
7590 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7591 check_non_private = "lastprivate";
7592 decl = OMP_CLAUSE_DECL (c);
7593 if (error_operand_p (decl))
7594 goto do_add;
7595 else if (outer_ctx
7596 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7597 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7598 && splay_tree_lookup (outer_ctx->variables,
7599 (splay_tree_key) decl) == NULL)
7600 {
7601 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7602 if (outer_ctx->outer_context)
7603 omp_notice_variable (outer_ctx->outer_context, decl, true);
7604 }
7605 else if (outer_ctx
7606 && (outer_ctx->region_type & ORT_TASK) != 0
7607 && outer_ctx->combined_loop
7608 && splay_tree_lookup (outer_ctx->variables,
7609 (splay_tree_key) decl) == NULL)
7610 {
7611 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7612 if (outer_ctx->outer_context)
7613 omp_notice_variable (outer_ctx->outer_context, decl, true);
7614 }
7615 else if (outer_ctx
7616 && (outer_ctx->region_type == ORT_WORKSHARE
7617 || outer_ctx->region_type == ORT_ACC)
7618 && outer_ctx->combined_loop
7619 && splay_tree_lookup (outer_ctx->variables,
7620 (splay_tree_key) decl) == NULL
7621 && !omp_check_private (outer_ctx, decl, false))
7622 {
7623 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7624 if (outer_ctx->outer_context
7625 && (outer_ctx->outer_context->region_type
7626 == ORT_COMBINED_PARALLEL)
7627 && splay_tree_lookup (outer_ctx->outer_context->variables,
7628 (splay_tree_key) decl) == NULL)
7629 {
7630 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7631 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7632 if (octx->outer_context)
7633 {
7634 octx = octx->outer_context;
7635 if (octx->region_type == ORT_WORKSHARE
7636 && octx->combined_loop
7637 && splay_tree_lookup (octx->variables,
7638 (splay_tree_key) decl) == NULL
7639 && !omp_check_private (octx, decl, false))
7640 {
7641 omp_add_variable (octx, decl,
7642 GOVD_LASTPRIVATE | GOVD_SEEN);
7643 octx = octx->outer_context;
7644 if (octx
7645 && octx->region_type == ORT_COMBINED_TEAMS
7646 && (splay_tree_lookup (octx->variables,
7647 (splay_tree_key) decl)
7648 == NULL))
7649 {
7650 omp_add_variable (octx, decl,
7651 GOVD_SHARED | GOVD_SEEN);
7652 octx = octx->outer_context;
7653 }
7654 }
7655 if (octx)
7656 omp_notice_variable (octx, decl, true);
7657 }
7658 }
7659 else if (outer_ctx->outer_context)
7660 omp_notice_variable (outer_ctx->outer_context, decl, true);
7661 }
7662 goto do_add;
7663 case OMP_CLAUSE_REDUCTION:
7664 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7665 /* OpenACC permits reductions on private variables. */
7666 if (!(region_type & ORT_ACC))
7667 check_non_private = "reduction";
7668 decl = OMP_CLAUSE_DECL (c);
7669 if (TREE_CODE (decl) == MEM_REF)
7670 {
7671 tree type = TREE_TYPE (decl);
7672 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7673 NULL, is_gimple_val, fb_rvalue, false)
7674 == GS_ERROR)
7675 {
7676 remove = true;
7677 break;
7678 }
7679 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7680 if (DECL_P (v))
7681 {
7682 omp_firstprivatize_variable (ctx, v);
7683 omp_notice_variable (ctx, v, true);
7684 }
7685 decl = TREE_OPERAND (decl, 0);
7686 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7687 {
7688 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7689 NULL, is_gimple_val, fb_rvalue, false)
7690 == GS_ERROR)
7691 {
7692 remove = true;
7693 break;
7694 }
7695 v = TREE_OPERAND (decl, 1);
7696 if (DECL_P (v))
7697 {
7698 omp_firstprivatize_variable (ctx, v);
7699 omp_notice_variable (ctx, v, true);
7700 }
7701 decl = TREE_OPERAND (decl, 0);
7702 }
7703 if (TREE_CODE (decl) == ADDR_EXPR
7704 || TREE_CODE (decl) == INDIRECT_REF)
7705 decl = TREE_OPERAND (decl, 0);
7706 }
7707 goto do_add_decl;
7708 case OMP_CLAUSE_LINEAR:
7709 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7710 is_gimple_val, fb_rvalue) == GS_ERROR)
7711 {
7712 remove = true;
7713 break;
7714 }
7715 else
7716 {
7717 if (code == OMP_SIMD
7718 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7719 {
7720 struct gimplify_omp_ctx *octx = outer_ctx;
7721 if (octx
7722 && octx->region_type == ORT_WORKSHARE
7723 && octx->combined_loop
7724 && !octx->distribute)
7725 {
7726 if (octx->outer_context
7727 && (octx->outer_context->region_type
7728 == ORT_COMBINED_PARALLEL))
7729 octx = octx->outer_context->outer_context;
7730 else
7731 octx = octx->outer_context;
7732 }
7733 if (octx
7734 && octx->region_type == ORT_WORKSHARE
7735 && octx->combined_loop
7736 && octx->distribute)
7737 {
7738 error_at (OMP_CLAUSE_LOCATION (c),
7739 "%<linear%> clause for variable other than "
7740 "loop iterator specified on construct "
7741 "combined with %<distribute%>");
7742 remove = true;
7743 break;
7744 }
7745 }
7746 /* For combined #pragma omp parallel for simd, need to put
7747 lastprivate and perhaps firstprivate too on the
7748 parallel. Similarly for #pragma omp for simd. */
7749 struct gimplify_omp_ctx *octx = outer_ctx;
7750 decl = NULL_TREE;
7751 do
7752 {
7753 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7754 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7755 break;
7756 decl = OMP_CLAUSE_DECL (c);
7757 if (error_operand_p (decl))
7758 {
7759 decl = NULL_TREE;
7760 break;
7761 }
7762 flags = GOVD_SEEN;
7763 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7764 flags |= GOVD_FIRSTPRIVATE;
7765 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7766 flags |= GOVD_LASTPRIVATE;
7767 if (octx
7768 && octx->region_type == ORT_WORKSHARE
7769 && octx->combined_loop)
7770 {
7771 if (octx->outer_context
7772 && (octx->outer_context->region_type
7773 == ORT_COMBINED_PARALLEL))
7774 octx = octx->outer_context;
7775 else if (omp_check_private (octx, decl, false))
7776 break;
7777 }
7778 else if (octx
7779 && (octx->region_type & ORT_TASK) != 0
7780 && octx->combined_loop)
7781 ;
7782 else if (octx
7783 && octx->region_type == ORT_COMBINED_PARALLEL
7784 && ctx->region_type == ORT_WORKSHARE
7785 && octx == outer_ctx)
7786 flags = GOVD_SEEN | GOVD_SHARED;
7787 else if (octx
7788 && octx->region_type == ORT_COMBINED_TEAMS)
7789 flags = GOVD_SEEN | GOVD_SHARED;
7790 else if (octx
7791 && octx->region_type == ORT_COMBINED_TARGET)
7792 {
7793 flags &= ~GOVD_LASTPRIVATE;
7794 if (flags == GOVD_SEEN)
7795 break;
7796 }
7797 else
7798 break;
7799 splay_tree_node on
7800 = splay_tree_lookup (octx->variables,
7801 (splay_tree_key) decl);
7802 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7803 {
7804 octx = NULL;
7805 break;
7806 }
7807 omp_add_variable (octx, decl, flags);
7808 if (octx->outer_context == NULL)
7809 break;
7810 octx = octx->outer_context;
7811 }
7812 while (1);
7813 if (octx
7814 && decl
7815 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7816 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7817 omp_notice_variable (octx, decl, true);
7818 }
7819 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7820 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7821 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7822 {
7823 notice_outer = false;
7824 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7825 }
7826 goto do_add;
7827
7828 case OMP_CLAUSE_MAP:
7829 decl = OMP_CLAUSE_DECL (c);
7830 if (error_operand_p (decl))
7831 remove = true;
7832 switch (code)
7833 {
7834 case OMP_TARGET:
7835 break;
7836 case OACC_DATA:
7837 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7838 break;
7839 /* FALLTHRU */
7840 case OMP_TARGET_DATA:
7841 case OMP_TARGET_ENTER_DATA:
7842 case OMP_TARGET_EXIT_DATA:
7843 case OACC_ENTER_DATA:
7844 case OACC_EXIT_DATA:
7845 case OACC_HOST_DATA:
7846 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7847 || (OMP_CLAUSE_MAP_KIND (c)
7848 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7849 /* For target {,enter ,exit }data only the array slice is
7850 mapped, but not the pointer to it. */
7851 remove = true;
7852 break;
7853 default:
7854 break;
7855 }
7856 if (remove)
7857 break;
7858 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7859 {
7860 struct gimplify_omp_ctx *octx;
7861 for (octx = outer_ctx; octx; octx = octx->outer_context)
7862 {
7863 if (octx->region_type != ORT_ACC_HOST_DATA)
7864 break;
7865 splay_tree_node n2
7866 = splay_tree_lookup (octx->variables,
7867 (splay_tree_key) decl);
7868 if (n2)
7869 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7870 "declared in enclosing %<host_data%> region",
7871 DECL_NAME (decl));
7872 }
7873 }
7874 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7875 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7876 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7877 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7878 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7879 {
7880 remove = true;
7881 break;
7882 }
7883 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7884 || (OMP_CLAUSE_MAP_KIND (c)
7885 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7886 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7887 {
7888 OMP_CLAUSE_SIZE (c)
7889 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7890 false);
7891 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7892 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7893 }
7894 if (!DECL_P (decl))
7895 {
7896 tree d = decl, *pd;
7897 if (TREE_CODE (d) == ARRAY_REF)
7898 {
7899 while (TREE_CODE (d) == ARRAY_REF)
7900 d = TREE_OPERAND (d, 0);
7901 if (TREE_CODE (d) == COMPONENT_REF
7902 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7903 decl = d;
7904 }
7905 pd = &OMP_CLAUSE_DECL (c);
7906 if (d == decl
7907 && TREE_CODE (decl) == INDIRECT_REF
7908 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7909 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7910 == REFERENCE_TYPE))
7911 {
7912 pd = &TREE_OPERAND (decl, 0);
7913 decl = TREE_OPERAND (decl, 0);
7914 }
7915 if (TREE_CODE (decl) == COMPONENT_REF)
7916 {
7917 while (TREE_CODE (decl) == COMPONENT_REF)
7918 decl = TREE_OPERAND (decl, 0);
7919 if (TREE_CODE (decl) == INDIRECT_REF
7920 && DECL_P (TREE_OPERAND (decl, 0))
7921 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7922 == REFERENCE_TYPE))
7923 decl = TREE_OPERAND (decl, 0);
7924 }
7925 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7926 == GS_ERROR)
7927 {
7928 remove = true;
7929 break;
7930 }
7931 if (DECL_P (decl))
7932 {
7933 if (error_operand_p (decl))
7934 {
7935 remove = true;
7936 break;
7937 }
7938
7939 tree stype = TREE_TYPE (decl);
7940 if (TREE_CODE (stype) == REFERENCE_TYPE)
7941 stype = TREE_TYPE (stype);
7942 if (TYPE_SIZE_UNIT (stype) == NULL
7943 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7944 {
7945 error_at (OMP_CLAUSE_LOCATION (c),
7946 "mapping field %qE of variable length "
7947 "structure", OMP_CLAUSE_DECL (c));
7948 remove = true;
7949 break;
7950 }
7951
7952 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7953 {
7954 /* Error recovery. */
7955 if (prev_list_p == NULL)
7956 {
7957 remove = true;
7958 break;
7959 }
7960 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7961 {
7962 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7963 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7964 {
7965 remove = true;
7966 break;
7967 }
7968 }
7969 }
7970
7971 tree offset;
7972 poly_int64 bitsize, bitpos;
7973 machine_mode mode;
7974 int unsignedp, reversep, volatilep = 0;
7975 tree base = OMP_CLAUSE_DECL (c);
7976 while (TREE_CODE (base) == ARRAY_REF)
7977 base = TREE_OPERAND (base, 0);
7978 if (TREE_CODE (base) == INDIRECT_REF)
7979 base = TREE_OPERAND (base, 0);
7980 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7981 &mode, &unsignedp, &reversep,
7982 &volatilep);
7983 tree orig_base = base;
7984 if ((TREE_CODE (base) == INDIRECT_REF
7985 || (TREE_CODE (base) == MEM_REF
7986 && integer_zerop (TREE_OPERAND (base, 1))))
7987 && DECL_P (TREE_OPERAND (base, 0))
7988 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7989 == REFERENCE_TYPE))
7990 base = TREE_OPERAND (base, 0);
7991 gcc_assert (base == decl
7992 && (offset == NULL_TREE
7993 || poly_int_tree_p (offset)));
7994
7995 splay_tree_node n
7996 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7997 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7998 == GOMP_MAP_ALWAYS_POINTER);
7999 if (n == NULL || (n->value & GOVD_MAP) == 0)
8000 {
8001 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8002 OMP_CLAUSE_MAP);
8003 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8004 if (orig_base != base)
8005 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8006 else
8007 OMP_CLAUSE_DECL (l) = decl;
8008 OMP_CLAUSE_SIZE (l) = size_int (1);
8009 if (struct_map_to_clause == NULL)
8010 struct_map_to_clause = new hash_map<tree, tree>;
8011 struct_map_to_clause->put (decl, l);
8012 if (ptr)
8013 {
8014 enum gomp_map_kind mkind
8015 = code == OMP_TARGET_EXIT_DATA
8016 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8017 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8018 OMP_CLAUSE_MAP);
8019 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8020 OMP_CLAUSE_DECL (c2)
8021 = unshare_expr (OMP_CLAUSE_DECL (c));
8022 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8023 OMP_CLAUSE_SIZE (c2)
8024 = TYPE_SIZE_UNIT (ptr_type_node);
8025 OMP_CLAUSE_CHAIN (l) = c2;
8026 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8027 {
8028 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8029 tree c3
8030 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8031 OMP_CLAUSE_MAP);
8032 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8033 OMP_CLAUSE_DECL (c3)
8034 = unshare_expr (OMP_CLAUSE_DECL (c4));
8035 OMP_CLAUSE_SIZE (c3)
8036 = TYPE_SIZE_UNIT (ptr_type_node);
8037 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8038 OMP_CLAUSE_CHAIN (c2) = c3;
8039 }
8040 *prev_list_p = l;
8041 prev_list_p = NULL;
8042 }
8043 else
8044 {
8045 OMP_CLAUSE_CHAIN (l) = c;
8046 *list_p = l;
8047 list_p = &OMP_CLAUSE_CHAIN (l);
8048 }
8049 if (orig_base != base && code == OMP_TARGET)
8050 {
8051 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8052 OMP_CLAUSE_MAP);
8053 enum gomp_map_kind mkind
8054 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8055 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8056 OMP_CLAUSE_DECL (c2) = decl;
8057 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8058 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8059 OMP_CLAUSE_CHAIN (l) = c2;
8060 }
8061 flags = GOVD_MAP | GOVD_EXPLICIT;
8062 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8063 flags |= GOVD_SEEN;
8064 goto do_add_decl;
8065 }
8066 else
8067 {
8068 tree *osc = struct_map_to_clause->get (decl);
8069 tree *sc = NULL, *scp = NULL;
8070 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8071 n->value |= GOVD_SEEN;
8072 poly_offset_int o1, o2;
8073 if (offset)
8074 o1 = wi::to_poly_offset (offset);
8075 else
8076 o1 = 0;
8077 if (maybe_ne (bitpos, 0))
8078 o1 += bits_to_bytes_round_down (bitpos);
8079 sc = &OMP_CLAUSE_CHAIN (*osc);
8080 if (*sc != c
8081 && (OMP_CLAUSE_MAP_KIND (*sc)
8082 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8083 sc = &OMP_CLAUSE_CHAIN (*sc);
8084 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8085 if (ptr && sc == prev_list_p)
8086 break;
8087 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8088 != COMPONENT_REF
8089 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8090 != INDIRECT_REF)
8091 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8092 != ARRAY_REF))
8093 break;
8094 else
8095 {
8096 tree offset2;
8097 poly_int64 bitsize2, bitpos2;
8098 base = OMP_CLAUSE_DECL (*sc);
8099 if (TREE_CODE (base) == ARRAY_REF)
8100 {
8101 while (TREE_CODE (base) == ARRAY_REF)
8102 base = TREE_OPERAND (base, 0);
8103 if (TREE_CODE (base) != COMPONENT_REF
8104 || (TREE_CODE (TREE_TYPE (base))
8105 != ARRAY_TYPE))
8106 break;
8107 }
8108 else if (TREE_CODE (base) == INDIRECT_REF
8109 && (TREE_CODE (TREE_OPERAND (base, 0))
8110 == COMPONENT_REF)
8111 && (TREE_CODE (TREE_TYPE
8112 (TREE_OPERAND (base, 0)))
8113 == REFERENCE_TYPE))
8114 base = TREE_OPERAND (base, 0);
8115 base = get_inner_reference (base, &bitsize2,
8116 &bitpos2, &offset2,
8117 &mode, &unsignedp,
8118 &reversep, &volatilep);
8119 if ((TREE_CODE (base) == INDIRECT_REF
8120 || (TREE_CODE (base) == MEM_REF
8121 && integer_zerop (TREE_OPERAND (base,
8122 1))))
8123 && DECL_P (TREE_OPERAND (base, 0))
8124 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8125 0)))
8126 == REFERENCE_TYPE))
8127 base = TREE_OPERAND (base, 0);
8128 if (base != decl)
8129 break;
8130 if (scp)
8131 continue;
8132 gcc_assert (offset == NULL_TREE
8133 || poly_int_tree_p (offset));
8134 tree d1 = OMP_CLAUSE_DECL (*sc);
8135 tree d2 = OMP_CLAUSE_DECL (c);
8136 while (TREE_CODE (d1) == ARRAY_REF)
8137 d1 = TREE_OPERAND (d1, 0);
8138 while (TREE_CODE (d2) == ARRAY_REF)
8139 d2 = TREE_OPERAND (d2, 0);
8140 if (TREE_CODE (d1) == INDIRECT_REF)
8141 d1 = TREE_OPERAND (d1, 0);
8142 if (TREE_CODE (d2) == INDIRECT_REF)
8143 d2 = TREE_OPERAND (d2, 0);
8144 while (TREE_CODE (d1) == COMPONENT_REF)
8145 if (TREE_CODE (d2) == COMPONENT_REF
8146 && TREE_OPERAND (d1, 1)
8147 == TREE_OPERAND (d2, 1))
8148 {
8149 d1 = TREE_OPERAND (d1, 0);
8150 d2 = TREE_OPERAND (d2, 0);
8151 }
8152 else
8153 break;
8154 if (d1 == d2)
8155 {
8156 error_at (OMP_CLAUSE_LOCATION (c),
8157 "%qE appears more than once in map "
8158 "clauses", OMP_CLAUSE_DECL (c));
8159 remove = true;
8160 break;
8161 }
8162 if (offset2)
8163 o2 = wi::to_poly_offset (offset2);
8164 else
8165 o2 = 0;
8166 o2 += bits_to_bytes_round_down (bitpos2);
8167 if (maybe_lt (o1, o2)
8168 || (known_eq (o1, 2)
8169 && maybe_lt (bitpos, bitpos2)))
8170 {
8171 if (ptr)
8172 scp = sc;
8173 else
8174 break;
8175 }
8176 }
8177 if (remove)
8178 break;
8179 OMP_CLAUSE_SIZE (*osc)
8180 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8181 size_one_node);
8182 if (ptr)
8183 {
8184 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8185 OMP_CLAUSE_MAP);
8186 tree cl = NULL_TREE;
8187 enum gomp_map_kind mkind
8188 = code == OMP_TARGET_EXIT_DATA
8189 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8190 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8191 OMP_CLAUSE_DECL (c2)
8192 = unshare_expr (OMP_CLAUSE_DECL (c));
8193 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8194 OMP_CLAUSE_SIZE (c2)
8195 = TYPE_SIZE_UNIT (ptr_type_node);
8196 cl = scp ? *prev_list_p : c2;
8197 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8198 {
8199 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8200 tree c3
8201 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8202 OMP_CLAUSE_MAP);
8203 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8204 OMP_CLAUSE_DECL (c3)
8205 = unshare_expr (OMP_CLAUSE_DECL (c4));
8206 OMP_CLAUSE_SIZE (c3)
8207 = TYPE_SIZE_UNIT (ptr_type_node);
8208 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8209 if (!scp)
8210 OMP_CLAUSE_CHAIN (c2) = c3;
8211 else
8212 cl = c3;
8213 }
8214 if (scp)
8215 *scp = c2;
8216 if (sc == prev_list_p)
8217 {
8218 *sc = cl;
8219 prev_list_p = NULL;
8220 }
8221 else
8222 {
8223 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8224 list_p = prev_list_p;
8225 prev_list_p = NULL;
8226 OMP_CLAUSE_CHAIN (c) = *sc;
8227 *sc = cl;
8228 continue;
8229 }
8230 }
8231 else if (*sc != c)
8232 {
8233 *list_p = OMP_CLAUSE_CHAIN (c);
8234 OMP_CLAUSE_CHAIN (c) = *sc;
8235 *sc = c;
8236 continue;
8237 }
8238 }
8239 }
8240 if (!remove
8241 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8242 && OMP_CLAUSE_CHAIN (c)
8243 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8244 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8245 == GOMP_MAP_ALWAYS_POINTER))
8246 prev_list_p = list_p;
8247 break;
8248 }
8249 flags = GOVD_MAP | GOVD_EXPLICIT;
8250 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8251 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8252 flags |= GOVD_MAP_ALWAYS_TO;
8253 goto do_add;
8254
8255 case OMP_CLAUSE_DEPEND:
8256 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8257 {
8258 tree deps = OMP_CLAUSE_DECL (c);
8259 while (deps && TREE_CODE (deps) == TREE_LIST)
8260 {
8261 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8262 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8263 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8264 pre_p, NULL, is_gimple_val, fb_rvalue);
8265 deps = TREE_CHAIN (deps);
8266 }
8267 break;
8268 }
8269 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8270 break;
8271 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8272 {
8273 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8274 NULL, is_gimple_val, fb_rvalue);
8275 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8276 }
8277 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8278 {
8279 remove = true;
8280 break;
8281 }
8282 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8283 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8284 is_gimple_val, fb_rvalue) == GS_ERROR)
8285 {
8286 remove = true;
8287 break;
8288 }
8289 break;
8290
8291 case OMP_CLAUSE_TO:
8292 case OMP_CLAUSE_FROM:
8293 case OMP_CLAUSE__CACHE_:
8294 decl = OMP_CLAUSE_DECL (c);
8295 if (error_operand_p (decl))
8296 {
8297 remove = true;
8298 break;
8299 }
8300 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8301 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8302 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8303 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8304 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8305 {
8306 remove = true;
8307 break;
8308 }
8309 if (!DECL_P (decl))
8310 {
8311 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8312 NULL, is_gimple_lvalue, fb_lvalue)
8313 == GS_ERROR)
8314 {
8315 remove = true;
8316 break;
8317 }
8318 break;
8319 }
8320 goto do_notice;
8321
8322 case OMP_CLAUSE_USE_DEVICE_PTR:
8323 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8324 goto do_add;
8325 case OMP_CLAUSE_IS_DEVICE_PTR:
8326 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8327 goto do_add;
8328
8329 do_add:
8330 decl = OMP_CLAUSE_DECL (c);
8331 do_add_decl:
8332 if (error_operand_p (decl))
8333 {
8334 remove = true;
8335 break;
8336 }
8337 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8338 {
8339 tree t = omp_member_access_dummy_var (decl);
8340 if (t)
8341 {
8342 tree v = DECL_VALUE_EXPR (decl);
8343 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8344 if (outer_ctx)
8345 omp_notice_variable (outer_ctx, t, true);
8346 }
8347 }
8348 if (code == OACC_DATA
8349 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8350 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8351 flags |= GOVD_MAP_0LEN_ARRAY;
8352 omp_add_variable (ctx, decl, flags);
8353 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8354 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8355 {
8356 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8357 GOVD_LOCAL | GOVD_SEEN);
8358 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8359 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8360 find_decl_expr,
8361 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8362 NULL) == NULL_TREE)
8363 omp_add_variable (ctx,
8364 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8365 GOVD_LOCAL | GOVD_SEEN);
8366 gimplify_omp_ctxp = ctx;
8367 push_gimplify_context ();
8368
8369 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8370 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8371
8372 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8373 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8374 pop_gimplify_context
8375 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8376 push_gimplify_context ();
8377 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8378 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8379 pop_gimplify_context
8380 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8381 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8382 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8383
8384 gimplify_omp_ctxp = outer_ctx;
8385 }
8386 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8387 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8388 {
8389 gimplify_omp_ctxp = ctx;
8390 push_gimplify_context ();
8391 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8392 {
8393 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8394 NULL, NULL);
8395 TREE_SIDE_EFFECTS (bind) = 1;
8396 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8397 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8398 }
8399 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8400 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8401 pop_gimplify_context
8402 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8403 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8404
8405 gimplify_omp_ctxp = outer_ctx;
8406 }
8407 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8408 && OMP_CLAUSE_LINEAR_STMT (c))
8409 {
8410 gimplify_omp_ctxp = ctx;
8411 push_gimplify_context ();
8412 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8413 {
8414 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8415 NULL, NULL);
8416 TREE_SIDE_EFFECTS (bind) = 1;
8417 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8418 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8419 }
8420 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8421 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8422 pop_gimplify_context
8423 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8424 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8425
8426 gimplify_omp_ctxp = outer_ctx;
8427 }
8428 if (notice_outer)
8429 goto do_notice;
8430 break;
8431
8432 case OMP_CLAUSE_COPYIN:
8433 case OMP_CLAUSE_COPYPRIVATE:
8434 decl = OMP_CLAUSE_DECL (c);
8435 if (error_operand_p (decl))
8436 {
8437 remove = true;
8438 break;
8439 }
8440 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8441 && !remove
8442 && !omp_check_private (ctx, decl, true))
8443 {
8444 remove = true;
8445 if (is_global_var (decl))
8446 {
8447 if (DECL_THREAD_LOCAL_P (decl))
8448 remove = false;
8449 else if (DECL_HAS_VALUE_EXPR_P (decl))
8450 {
8451 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8452
8453 if (value
8454 && DECL_P (value)
8455 && DECL_THREAD_LOCAL_P (value))
8456 remove = false;
8457 }
8458 }
8459 if (remove)
8460 error_at (OMP_CLAUSE_LOCATION (c),
8461 "copyprivate variable %qE is not threadprivate"
8462 " or private in outer context", DECL_NAME (decl));
8463 }
8464 do_notice:
8465 if (outer_ctx)
8466 omp_notice_variable (outer_ctx, decl, true);
8467 if (check_non_private
8468 && region_type == ORT_WORKSHARE
8469 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8470 || decl == OMP_CLAUSE_DECL (c)
8471 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8472 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8473 == ADDR_EXPR
8474 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8475 == POINTER_PLUS_EXPR
8476 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8477 (OMP_CLAUSE_DECL (c), 0), 0))
8478 == ADDR_EXPR)))))
8479 && omp_check_private (ctx, decl, false))
8480 {
8481 error ("%s variable %qE is private in outer context",
8482 check_non_private, DECL_NAME (decl));
8483 remove = true;
8484 }
8485 break;
8486
8487 case OMP_CLAUSE_IF:
8488 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8489 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8490 {
8491 const char *p[2];
8492 for (int i = 0; i < 2; i++)
8493 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8494 {
8495 case OMP_PARALLEL: p[i] = "parallel"; break;
8496 case OMP_TASK: p[i] = "task"; break;
8497 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8498 case OMP_TARGET_DATA: p[i] = "target data"; break;
8499 case OMP_TARGET: p[i] = "target"; break;
8500 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8501 case OMP_TARGET_ENTER_DATA:
8502 p[i] = "target enter data"; break;
8503 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8504 default: gcc_unreachable ();
8505 }
8506 error_at (OMP_CLAUSE_LOCATION (c),
8507 "expected %qs %<if%> clause modifier rather than %qs",
8508 p[0], p[1]);
8509 remove = true;
8510 }
8511 /* Fall through. */
8512
8513 case OMP_CLAUSE_FINAL:
8514 OMP_CLAUSE_OPERAND (c, 0)
8515 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8516 /* Fall through. */
8517
8518 case OMP_CLAUSE_SCHEDULE:
8519 case OMP_CLAUSE_NUM_THREADS:
8520 case OMP_CLAUSE_NUM_TEAMS:
8521 case OMP_CLAUSE_THREAD_LIMIT:
8522 case OMP_CLAUSE_DIST_SCHEDULE:
8523 case OMP_CLAUSE_DEVICE:
8524 case OMP_CLAUSE_PRIORITY:
8525 case OMP_CLAUSE_GRAINSIZE:
8526 case OMP_CLAUSE_NUM_TASKS:
8527 case OMP_CLAUSE_HINT:
8528 case OMP_CLAUSE_ASYNC:
8529 case OMP_CLAUSE_WAIT:
8530 case OMP_CLAUSE_NUM_GANGS:
8531 case OMP_CLAUSE_NUM_WORKERS:
8532 case OMP_CLAUSE_VECTOR_LENGTH:
8533 case OMP_CLAUSE_WORKER:
8534 case OMP_CLAUSE_VECTOR:
8535 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8536 is_gimple_val, fb_rvalue) == GS_ERROR)
8537 remove = true;
8538 break;
8539
8540 case OMP_CLAUSE_GANG:
8541 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8542 is_gimple_val, fb_rvalue) == GS_ERROR)
8543 remove = true;
8544 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8545 is_gimple_val, fb_rvalue) == GS_ERROR)
8546 remove = true;
8547 break;
8548
8549 case OMP_CLAUSE_NOWAIT:
8550 case OMP_CLAUSE_ORDERED:
8551 case OMP_CLAUSE_UNTIED:
8552 case OMP_CLAUSE_COLLAPSE:
8553 case OMP_CLAUSE_TILE:
8554 case OMP_CLAUSE_AUTO:
8555 case OMP_CLAUSE_SEQ:
8556 case OMP_CLAUSE_INDEPENDENT:
8557 case OMP_CLAUSE_MERGEABLE:
8558 case OMP_CLAUSE_PROC_BIND:
8559 case OMP_CLAUSE_SAFELEN:
8560 case OMP_CLAUSE_SIMDLEN:
8561 case OMP_CLAUSE_NOGROUP:
8562 case OMP_CLAUSE_THREADS:
8563 case OMP_CLAUSE_SIMD:
8564 break;
8565
8566 case OMP_CLAUSE_DEFAULTMAP:
8567 ctx->target_map_scalars_firstprivate = false;
8568 break;
8569
8570 case OMP_CLAUSE_ALIGNED:
8571 decl = OMP_CLAUSE_DECL (c);
8572 if (error_operand_p (decl))
8573 {
8574 remove = true;
8575 break;
8576 }
8577 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8578 is_gimple_val, fb_rvalue) == GS_ERROR)
8579 {
8580 remove = true;
8581 break;
8582 }
8583 if (!is_global_var (decl)
8584 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8585 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8586 break;
8587
8588 case OMP_CLAUSE_DEFAULT:
8589 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8590 break;
8591
8592 default:
8593 gcc_unreachable ();
8594 }
8595
8596 if (code == OACC_DATA
8597 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8598 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8599 remove = true;
8600 if (remove)
8601 *list_p = OMP_CLAUSE_CHAIN (c);
8602 else
8603 list_p = &OMP_CLAUSE_CHAIN (c);
8604 }
8605
8606 gimplify_omp_ctxp = ctx;
8607 if (struct_map_to_clause)
8608 delete struct_map_to_clause;
8609 }
8610
8611 /* Return true if DECL is a candidate for shared to firstprivate
8612 optimization. We only consider non-addressable scalars, not
8613 too big, and not references. */
8614
8615 static bool
8616 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8617 {
8618 if (TREE_ADDRESSABLE (decl))
8619 return false;
8620 tree type = TREE_TYPE (decl);
8621 if (!is_gimple_reg_type (type)
8622 || TREE_CODE (type) == REFERENCE_TYPE
8623 || TREE_ADDRESSABLE (type))
8624 return false;
8625 /* Don't optimize too large decls, as each thread/task will have
8626 its own. */
8627 HOST_WIDE_INT len = int_size_in_bytes (type);
8628 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8629 return false;
8630 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8631 return false;
8632 return true;
8633 }
8634
8635 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8636 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8637 GOVD_WRITTEN in outer contexts. */
8638
8639 static void
8640 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8641 {
8642 for (; ctx; ctx = ctx->outer_context)
8643 {
8644 splay_tree_node n = splay_tree_lookup (ctx->variables,
8645 (splay_tree_key) decl);
8646 if (n == NULL)
8647 continue;
8648 else if (n->value & GOVD_SHARED)
8649 {
8650 n->value |= GOVD_WRITTEN;
8651 return;
8652 }
8653 else if (n->value & GOVD_DATA_SHARE_CLASS)
8654 return;
8655 }
8656 }
8657
8658 /* Helper callback for walk_gimple_seq to discover possible stores
8659 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8660 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8661 for those. */
8662
8663 static tree
8664 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8665 {
8666 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8667
8668 *walk_subtrees = 0;
8669 if (!wi->is_lhs)
8670 return NULL_TREE;
8671
8672 tree op = *tp;
8673 do
8674 {
8675 if (handled_component_p (op))
8676 op = TREE_OPERAND (op, 0);
8677 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8678 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8679 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8680 else
8681 break;
8682 }
8683 while (1);
8684 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8685 return NULL_TREE;
8686
8687 omp_mark_stores (gimplify_omp_ctxp, op);
8688 return NULL_TREE;
8689 }
8690
8691 /* Helper callback for walk_gimple_seq to discover possible stores
8692 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8693 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8694 for those. */
8695
8696 static tree
8697 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8698 bool *handled_ops_p,
8699 struct walk_stmt_info *wi)
8700 {
8701 gimple *stmt = gsi_stmt (*gsi_p);
8702 switch (gimple_code (stmt))
8703 {
8704 /* Don't recurse on OpenMP constructs for which
8705 gimplify_adjust_omp_clauses already handled the bodies,
8706 except handle gimple_omp_for_pre_body. */
8707 case GIMPLE_OMP_FOR:
8708 *handled_ops_p = true;
8709 if (gimple_omp_for_pre_body (stmt))
8710 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8711 omp_find_stores_stmt, omp_find_stores_op, wi);
8712 break;
8713 case GIMPLE_OMP_PARALLEL:
8714 case GIMPLE_OMP_TASK:
8715 case GIMPLE_OMP_SECTIONS:
8716 case GIMPLE_OMP_SINGLE:
8717 case GIMPLE_OMP_TARGET:
8718 case GIMPLE_OMP_TEAMS:
8719 case GIMPLE_OMP_CRITICAL:
8720 *handled_ops_p = true;
8721 break;
8722 default:
8723 break;
8724 }
8725 return NULL_TREE;
8726 }
8727
8728 struct gimplify_adjust_omp_clauses_data
8729 {
8730 tree *list_p;
8731 gimple_seq *pre_p;
8732 };
8733
8734 /* For all variables that were not actually used within the context,
8735 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8736
8737 static int
8738 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8739 {
8740 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8741 gimple_seq *pre_p
8742 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8743 tree decl = (tree) n->key;
8744 unsigned flags = n->value;
8745 enum omp_clause_code code;
8746 tree clause;
8747 bool private_debug;
8748
8749 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8750 return 0;
8751 if ((flags & GOVD_SEEN) == 0)
8752 return 0;
8753 if (flags & GOVD_DEBUG_PRIVATE)
8754 {
8755 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8756 private_debug = true;
8757 }
8758 else if (flags & GOVD_MAP)
8759 private_debug = false;
8760 else
8761 private_debug
8762 = lang_hooks.decls.omp_private_debug_clause (decl,
8763 !!(flags & GOVD_SHARED));
8764 if (private_debug)
8765 code = OMP_CLAUSE_PRIVATE;
8766 else if (flags & GOVD_MAP)
8767 {
8768 code = OMP_CLAUSE_MAP;
8769 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8770 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8771 {
8772 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8773 return 0;
8774 }
8775 }
8776 else if (flags & GOVD_SHARED)
8777 {
8778 if (is_global_var (decl))
8779 {
8780 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8781 while (ctx != NULL)
8782 {
8783 splay_tree_node on
8784 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8785 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8786 | GOVD_PRIVATE | GOVD_REDUCTION
8787 | GOVD_LINEAR | GOVD_MAP)) != 0)
8788 break;
8789 ctx = ctx->outer_context;
8790 }
8791 if (ctx == NULL)
8792 return 0;
8793 }
8794 code = OMP_CLAUSE_SHARED;
8795 }
8796 else if (flags & GOVD_PRIVATE)
8797 code = OMP_CLAUSE_PRIVATE;
8798 else if (flags & GOVD_FIRSTPRIVATE)
8799 {
8800 code = OMP_CLAUSE_FIRSTPRIVATE;
8801 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8802 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8803 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8804 {
8805 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8806 "%<target%> construct", decl);
8807 return 0;
8808 }
8809 }
8810 else if (flags & GOVD_LASTPRIVATE)
8811 code = OMP_CLAUSE_LASTPRIVATE;
8812 else if (flags & GOVD_ALIGNED)
8813 return 0;
8814 else
8815 gcc_unreachable ();
8816
8817 if (((flags & GOVD_LASTPRIVATE)
8818 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8819 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8820 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8821
8822 tree chain = *list_p;
8823 clause = build_omp_clause (input_location, code);
8824 OMP_CLAUSE_DECL (clause) = decl;
8825 OMP_CLAUSE_CHAIN (clause) = chain;
8826 if (private_debug)
8827 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8828 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8829 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8830 else if (code == OMP_CLAUSE_SHARED
8831 && (flags & GOVD_WRITTEN) == 0
8832 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8833 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8834 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8835 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8836 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8837 {
8838 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8839 OMP_CLAUSE_DECL (nc) = decl;
8840 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8841 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8842 OMP_CLAUSE_DECL (clause)
8843 = build_simple_mem_ref_loc (input_location, decl);
8844 OMP_CLAUSE_DECL (clause)
8845 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8846 build_int_cst (build_pointer_type (char_type_node), 0));
8847 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8848 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8849 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8850 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8851 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8852 OMP_CLAUSE_CHAIN (nc) = chain;
8853 OMP_CLAUSE_CHAIN (clause) = nc;
8854 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8855 gimplify_omp_ctxp = ctx->outer_context;
8856 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8857 pre_p, NULL, is_gimple_val, fb_rvalue);
8858 gimplify_omp_ctxp = ctx;
8859 }
8860 else if (code == OMP_CLAUSE_MAP)
8861 {
8862 int kind;
8863 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8864 switch (flags & (GOVD_MAP_TO_ONLY
8865 | GOVD_MAP_FORCE
8866 | GOVD_MAP_FORCE_PRESENT))
8867 {
8868 case 0:
8869 kind = GOMP_MAP_TOFROM;
8870 break;
8871 case GOVD_MAP_FORCE:
8872 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8873 break;
8874 case GOVD_MAP_TO_ONLY:
8875 kind = GOMP_MAP_TO;
8876 break;
8877 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8878 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8879 break;
8880 case GOVD_MAP_FORCE_PRESENT:
8881 kind = GOMP_MAP_FORCE_PRESENT;
8882 break;
8883 default:
8884 gcc_unreachable ();
8885 }
8886 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8887 if (DECL_SIZE (decl)
8888 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8889 {
8890 tree decl2 = DECL_VALUE_EXPR (decl);
8891 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8892 decl2 = TREE_OPERAND (decl2, 0);
8893 gcc_assert (DECL_P (decl2));
8894 tree mem = build_simple_mem_ref (decl2);
8895 OMP_CLAUSE_DECL (clause) = mem;
8896 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8897 if (gimplify_omp_ctxp->outer_context)
8898 {
8899 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8900 omp_notice_variable (ctx, decl2, true);
8901 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8902 }
8903 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8904 OMP_CLAUSE_MAP);
8905 OMP_CLAUSE_DECL (nc) = decl;
8906 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8907 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8908 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8909 else
8910 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8911 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8912 OMP_CLAUSE_CHAIN (clause) = nc;
8913 }
8914 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8915 && lang_hooks.decls.omp_privatize_by_reference (decl))
8916 {
8917 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8918 OMP_CLAUSE_SIZE (clause)
8919 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8920 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8921 gimplify_omp_ctxp = ctx->outer_context;
8922 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8923 pre_p, NULL, is_gimple_val, fb_rvalue);
8924 gimplify_omp_ctxp = ctx;
8925 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8926 OMP_CLAUSE_MAP);
8927 OMP_CLAUSE_DECL (nc) = decl;
8928 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8929 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8930 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8931 OMP_CLAUSE_CHAIN (clause) = nc;
8932 }
8933 else
8934 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8935 }
8936 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8937 {
8938 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8939 OMP_CLAUSE_DECL (nc) = decl;
8940 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8941 OMP_CLAUSE_CHAIN (nc) = chain;
8942 OMP_CLAUSE_CHAIN (clause) = nc;
8943 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8944 gimplify_omp_ctxp = ctx->outer_context;
8945 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8946 gimplify_omp_ctxp = ctx;
8947 }
8948 *list_p = clause;
8949 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8950 gimplify_omp_ctxp = ctx->outer_context;
8951 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8952 if (gimplify_omp_ctxp)
8953 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8954 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8955 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8956 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8957 true);
8958 gimplify_omp_ctxp = ctx;
8959 return 0;
8960 }
8961
8962 static void
8963 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8964 enum tree_code code)
8965 {
8966 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8967 tree c, decl;
8968
8969 if (body)
8970 {
8971 struct gimplify_omp_ctx *octx;
8972 for (octx = ctx; octx; octx = octx->outer_context)
8973 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8974 break;
8975 if (octx)
8976 {
8977 struct walk_stmt_info wi;
8978 memset (&wi, 0, sizeof (wi));
8979 walk_gimple_seq (body, omp_find_stores_stmt,
8980 omp_find_stores_op, &wi);
8981 }
8982 }
8983 while ((c = *list_p) != NULL)
8984 {
8985 splay_tree_node n;
8986 bool remove = false;
8987
8988 switch (OMP_CLAUSE_CODE (c))
8989 {
8990 case OMP_CLAUSE_FIRSTPRIVATE:
8991 if ((ctx->region_type & ORT_TARGET)
8992 && (ctx->region_type & ORT_ACC) == 0
8993 && TYPE_ATOMIC (strip_array_types
8994 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8995 {
8996 error_at (OMP_CLAUSE_LOCATION (c),
8997 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8998 "%<target%> construct", OMP_CLAUSE_DECL (c));
8999 remove = true;
9000 break;
9001 }
9002 /* FALLTHRU */
9003 case OMP_CLAUSE_PRIVATE:
9004 case OMP_CLAUSE_SHARED:
9005 case OMP_CLAUSE_LINEAR:
9006 decl = OMP_CLAUSE_DECL (c);
9007 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9008 remove = !(n->value & GOVD_SEEN);
9009 if (! remove)
9010 {
9011 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9012 if ((n->value & GOVD_DEBUG_PRIVATE)
9013 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9014 {
9015 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9016 || ((n->value & GOVD_DATA_SHARE_CLASS)
9017 == GOVD_SHARED));
9018 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9019 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9020 }
9021 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9022 && (n->value & GOVD_WRITTEN) == 0
9023 && DECL_P (decl)
9024 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9025 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9026 else if (DECL_P (decl)
9027 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9028 && (n->value & GOVD_WRITTEN) != 0)
9029 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9030 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9031 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9032 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9033 }
9034 break;
9035
9036 case OMP_CLAUSE_LASTPRIVATE:
9037 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9038 accurately reflect the presence of a FIRSTPRIVATE clause. */
9039 decl = OMP_CLAUSE_DECL (c);
9040 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9041 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9042 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9043 if (code == OMP_DISTRIBUTE
9044 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9045 {
9046 remove = true;
9047 error_at (OMP_CLAUSE_LOCATION (c),
9048 "same variable used in %<firstprivate%> and "
9049 "%<lastprivate%> clauses on %<distribute%> "
9050 "construct");
9051 }
9052 if (!remove
9053 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9054 && DECL_P (decl)
9055 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9056 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9057 break;
9058
9059 case OMP_CLAUSE_ALIGNED:
9060 decl = OMP_CLAUSE_DECL (c);
9061 if (!is_global_var (decl))
9062 {
9063 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9064 remove = n == NULL || !(n->value & GOVD_SEEN);
9065 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9066 {
9067 struct gimplify_omp_ctx *octx;
9068 if (n != NULL
9069 && (n->value & (GOVD_DATA_SHARE_CLASS
9070 & ~GOVD_FIRSTPRIVATE)))
9071 remove = true;
9072 else
9073 for (octx = ctx->outer_context; octx;
9074 octx = octx->outer_context)
9075 {
9076 n = splay_tree_lookup (octx->variables,
9077 (splay_tree_key) decl);
9078 if (n == NULL)
9079 continue;
9080 if (n->value & GOVD_LOCAL)
9081 break;
9082 /* We have to avoid assigning a shared variable
9083 to itself when trying to add
9084 __builtin_assume_aligned. */
9085 if (n->value & GOVD_SHARED)
9086 {
9087 remove = true;
9088 break;
9089 }
9090 }
9091 }
9092 }
9093 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9094 {
9095 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9096 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9097 remove = true;
9098 }
9099 break;
9100
9101 case OMP_CLAUSE_MAP:
9102 if (code == OMP_TARGET_EXIT_DATA
9103 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9104 {
9105 remove = true;
9106 break;
9107 }
9108 decl = OMP_CLAUSE_DECL (c);
9109 /* Data clauses associated with acc parallel reductions must be
9110 compatible with present_or_copy. Warn and adjust the clause
9111 if that is not the case. */
9112 if (ctx->region_type == ORT_ACC_PARALLEL)
9113 {
9114 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9115 n = NULL;
9116
9117 if (DECL_P (t))
9118 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9119
9120 if (n && (n->value & GOVD_REDUCTION))
9121 {
9122 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9123
9124 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9125 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9126 && kind != GOMP_MAP_FORCE_PRESENT
9127 && kind != GOMP_MAP_POINTER)
9128 {
9129 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9130 "incompatible data clause with reduction "
9131 "on %qE; promoting to present_or_copy",
9132 DECL_NAME (t));
9133 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9134 }
9135 }
9136 }
9137 if (!DECL_P (decl))
9138 {
9139 if ((ctx->region_type & ORT_TARGET) != 0
9140 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9141 {
9142 if (TREE_CODE (decl) == INDIRECT_REF
9143 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9144 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9145 == REFERENCE_TYPE))
9146 decl = TREE_OPERAND (decl, 0);
9147 if (TREE_CODE (decl) == COMPONENT_REF)
9148 {
9149 while (TREE_CODE (decl) == COMPONENT_REF)
9150 decl = TREE_OPERAND (decl, 0);
9151 if (DECL_P (decl))
9152 {
9153 n = splay_tree_lookup (ctx->variables,
9154 (splay_tree_key) decl);
9155 if (!(n->value & GOVD_SEEN))
9156 remove = true;
9157 }
9158 }
9159 }
9160 break;
9161 }
9162 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9163 if ((ctx->region_type & ORT_TARGET) != 0
9164 && !(n->value & GOVD_SEEN)
9165 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9166 && (!is_global_var (decl)
9167 || !lookup_attribute ("omp declare target link",
9168 DECL_ATTRIBUTES (decl))))
9169 {
9170 remove = true;
9171 /* For struct element mapping, if struct is never referenced
9172 in target block and none of the mapping has always modifier,
9173 remove all the struct element mappings, which immediately
9174 follow the GOMP_MAP_STRUCT map clause. */
9175 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9176 {
9177 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9178 while (cnt--)
9179 OMP_CLAUSE_CHAIN (c)
9180 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9181 }
9182 }
9183 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9184 && code == OMP_TARGET_EXIT_DATA)
9185 remove = true;
9186 else if (DECL_SIZE (decl)
9187 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9188 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9189 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9190 && (OMP_CLAUSE_MAP_KIND (c)
9191 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9192 {
9193 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9194 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9195 INTEGER_CST. */
9196 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9197
9198 tree decl2 = DECL_VALUE_EXPR (decl);
9199 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9200 decl2 = TREE_OPERAND (decl2, 0);
9201 gcc_assert (DECL_P (decl2));
9202 tree mem = build_simple_mem_ref (decl2);
9203 OMP_CLAUSE_DECL (c) = mem;
9204 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9205 if (ctx->outer_context)
9206 {
9207 omp_notice_variable (ctx->outer_context, decl2, true);
9208 omp_notice_variable (ctx->outer_context,
9209 OMP_CLAUSE_SIZE (c), true);
9210 }
9211 if (((ctx->region_type & ORT_TARGET) != 0
9212 || !ctx->target_firstprivatize_array_bases)
9213 && ((n->value & GOVD_SEEN) == 0
9214 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9215 {
9216 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9217 OMP_CLAUSE_MAP);
9218 OMP_CLAUSE_DECL (nc) = decl;
9219 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9220 if (ctx->target_firstprivatize_array_bases)
9221 OMP_CLAUSE_SET_MAP_KIND (nc,
9222 GOMP_MAP_FIRSTPRIVATE_POINTER);
9223 else
9224 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9225 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9226 OMP_CLAUSE_CHAIN (c) = nc;
9227 c = nc;
9228 }
9229 }
9230 else
9231 {
9232 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9233 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9234 gcc_assert ((n->value & GOVD_SEEN) == 0
9235 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9236 == 0));
9237 }
9238 break;
9239
9240 case OMP_CLAUSE_TO:
9241 case OMP_CLAUSE_FROM:
9242 case OMP_CLAUSE__CACHE_:
9243 decl = OMP_CLAUSE_DECL (c);
9244 if (!DECL_P (decl))
9245 break;
9246 if (DECL_SIZE (decl)
9247 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9248 {
9249 tree decl2 = DECL_VALUE_EXPR (decl);
9250 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9251 decl2 = TREE_OPERAND (decl2, 0);
9252 gcc_assert (DECL_P (decl2));
9253 tree mem = build_simple_mem_ref (decl2);
9254 OMP_CLAUSE_DECL (c) = mem;
9255 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9256 if (ctx->outer_context)
9257 {
9258 omp_notice_variable (ctx->outer_context, decl2, true);
9259 omp_notice_variable (ctx->outer_context,
9260 OMP_CLAUSE_SIZE (c), true);
9261 }
9262 }
9263 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9264 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9265 break;
9266
9267 case OMP_CLAUSE_REDUCTION:
9268 decl = OMP_CLAUSE_DECL (c);
9269 /* OpenACC reductions need a present_or_copy data clause.
9270 Add one if necessary. Emit error when the reduction is private. */
9271 if (ctx->region_type == ORT_ACC_PARALLEL)
9272 {
9273 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9274 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9275 {
9276 remove = true;
9277 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9278 "reduction on %qE", DECL_NAME (decl));
9279 }
9280 else if ((n->value & GOVD_MAP) == 0)
9281 {
9282 tree next = OMP_CLAUSE_CHAIN (c);
9283 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9284 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9285 OMP_CLAUSE_DECL (nc) = decl;
9286 OMP_CLAUSE_CHAIN (c) = nc;
9287 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9288 while (1)
9289 {
9290 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9291 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9292 break;
9293 nc = OMP_CLAUSE_CHAIN (nc);
9294 }
9295 OMP_CLAUSE_CHAIN (nc) = next;
9296 n->value |= GOVD_MAP;
9297 }
9298 }
9299 if (DECL_P (decl)
9300 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9301 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9302 break;
9303 case OMP_CLAUSE_COPYIN:
9304 case OMP_CLAUSE_COPYPRIVATE:
9305 case OMP_CLAUSE_IF:
9306 case OMP_CLAUSE_NUM_THREADS:
9307 case OMP_CLAUSE_NUM_TEAMS:
9308 case OMP_CLAUSE_THREAD_LIMIT:
9309 case OMP_CLAUSE_DIST_SCHEDULE:
9310 case OMP_CLAUSE_DEVICE:
9311 case OMP_CLAUSE_SCHEDULE:
9312 case OMP_CLAUSE_NOWAIT:
9313 case OMP_CLAUSE_ORDERED:
9314 case OMP_CLAUSE_DEFAULT:
9315 case OMP_CLAUSE_UNTIED:
9316 case OMP_CLAUSE_COLLAPSE:
9317 case OMP_CLAUSE_FINAL:
9318 case OMP_CLAUSE_MERGEABLE:
9319 case OMP_CLAUSE_PROC_BIND:
9320 case OMP_CLAUSE_SAFELEN:
9321 case OMP_CLAUSE_SIMDLEN:
9322 case OMP_CLAUSE_DEPEND:
9323 case OMP_CLAUSE_PRIORITY:
9324 case OMP_CLAUSE_GRAINSIZE:
9325 case OMP_CLAUSE_NUM_TASKS:
9326 case OMP_CLAUSE_NOGROUP:
9327 case OMP_CLAUSE_THREADS:
9328 case OMP_CLAUSE_SIMD:
9329 case OMP_CLAUSE_HINT:
9330 case OMP_CLAUSE_DEFAULTMAP:
9331 case OMP_CLAUSE_USE_DEVICE_PTR:
9332 case OMP_CLAUSE_IS_DEVICE_PTR:
9333 case OMP_CLAUSE_ASYNC:
9334 case OMP_CLAUSE_WAIT:
9335 case OMP_CLAUSE_INDEPENDENT:
9336 case OMP_CLAUSE_NUM_GANGS:
9337 case OMP_CLAUSE_NUM_WORKERS:
9338 case OMP_CLAUSE_VECTOR_LENGTH:
9339 case OMP_CLAUSE_GANG:
9340 case OMP_CLAUSE_WORKER:
9341 case OMP_CLAUSE_VECTOR:
9342 case OMP_CLAUSE_AUTO:
9343 case OMP_CLAUSE_SEQ:
9344 case OMP_CLAUSE_TILE:
9345 break;
9346
9347 default:
9348 gcc_unreachable ();
9349 }
9350
9351 if (remove)
9352 *list_p = OMP_CLAUSE_CHAIN (c);
9353 else
9354 list_p = &OMP_CLAUSE_CHAIN (c);
9355 }
9356
9357 /* Add in any implicit data sharing. */
9358 struct gimplify_adjust_omp_clauses_data data;
9359 data.list_p = list_p;
9360 data.pre_p = pre_p;
9361 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9362
9363 gimplify_omp_ctxp = ctx->outer_context;
9364 delete_omp_context (ctx);
9365 }
9366
9367 /* Gimplify OACC_CACHE. */
9368
9369 static void
9370 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9371 {
9372 tree expr = *expr_p;
9373
9374 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9375 OACC_CACHE);
9376 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9377 OACC_CACHE);
9378
9379 /* TODO: Do something sensible with this information. */
9380
9381 *expr_p = NULL_TREE;
9382 }
9383
9384 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9385 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9386 kind. The entry kind will replace the one in CLAUSE, while the exit
9387 kind will be used in a new omp_clause and returned to the caller. */
9388
9389 static tree
9390 gimplify_oacc_declare_1 (tree clause)
9391 {
9392 HOST_WIDE_INT kind, new_op;
9393 bool ret = false;
9394 tree c = NULL;
9395
9396 kind = OMP_CLAUSE_MAP_KIND (clause);
9397
9398 switch (kind)
9399 {
9400 case GOMP_MAP_ALLOC:
9401 case GOMP_MAP_FORCE_ALLOC:
9402 case GOMP_MAP_FORCE_TO:
9403 new_op = GOMP_MAP_DELETE;
9404 ret = true;
9405 break;
9406
9407 case GOMP_MAP_FORCE_FROM:
9408 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9409 new_op = GOMP_MAP_FORCE_FROM;
9410 ret = true;
9411 break;
9412
9413 case GOMP_MAP_FORCE_TOFROM:
9414 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9415 new_op = GOMP_MAP_FORCE_FROM;
9416 ret = true;
9417 break;
9418
9419 case GOMP_MAP_FROM:
9420 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9421 new_op = GOMP_MAP_FROM;
9422 ret = true;
9423 break;
9424
9425 case GOMP_MAP_TOFROM:
9426 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9427 new_op = GOMP_MAP_FROM;
9428 ret = true;
9429 break;
9430
9431 case GOMP_MAP_DEVICE_RESIDENT:
9432 case GOMP_MAP_FORCE_DEVICEPTR:
9433 case GOMP_MAP_FORCE_PRESENT:
9434 case GOMP_MAP_LINK:
9435 case GOMP_MAP_POINTER:
9436 case GOMP_MAP_TO:
9437 break;
9438
9439 default:
9440 gcc_unreachable ();
9441 break;
9442 }
9443
9444 if (ret)
9445 {
9446 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9447 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9448 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9449 }
9450
9451 return c;
9452 }
9453
9454 /* Gimplify OACC_DECLARE. */
9455
9456 static void
9457 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9458 {
9459 tree expr = *expr_p;
9460 gomp_target *stmt;
9461 tree clauses, t, decl;
9462
9463 clauses = OACC_DECLARE_CLAUSES (expr);
9464
9465 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9466 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9467
9468 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9469 {
9470 decl = OMP_CLAUSE_DECL (t);
9471
9472 if (TREE_CODE (decl) == MEM_REF)
9473 decl = TREE_OPERAND (decl, 0);
9474
9475 if (VAR_P (decl) && !is_oacc_declared (decl))
9476 {
9477 tree attr = get_identifier ("oacc declare target");
9478 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9479 DECL_ATTRIBUTES (decl));
9480 }
9481
9482 if (VAR_P (decl)
9483 && !is_global_var (decl)
9484 && DECL_CONTEXT (decl) == current_function_decl)
9485 {
9486 tree c = gimplify_oacc_declare_1 (t);
9487 if (c)
9488 {
9489 if (oacc_declare_returns == NULL)
9490 oacc_declare_returns = new hash_map<tree, tree>;
9491
9492 oacc_declare_returns->put (decl, c);
9493 }
9494 }
9495
9496 if (gimplify_omp_ctxp)
9497 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9498 }
9499
9500 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9501 clauses);
9502
9503 gimplify_seq_add_stmt (pre_p, stmt);
9504
9505 *expr_p = NULL_TREE;
9506 }
9507
9508 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9509 gimplification of the body, as well as scanning the body for used
9510 variables. We need to do this scan now, because variable-sized
9511 decls will be decomposed during gimplification. */
9512
9513 static void
9514 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9515 {
9516 tree expr = *expr_p;
9517 gimple *g;
9518 gimple_seq body = NULL;
9519
9520 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9521 OMP_PARALLEL_COMBINED (expr)
9522 ? ORT_COMBINED_PARALLEL
9523 : ORT_PARALLEL, OMP_PARALLEL);
9524
9525 push_gimplify_context ();
9526
9527 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9528 if (gimple_code (g) == GIMPLE_BIND)
9529 pop_gimplify_context (g);
9530 else
9531 pop_gimplify_context (NULL);
9532
9533 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9534 OMP_PARALLEL);
9535
9536 g = gimple_build_omp_parallel (body,
9537 OMP_PARALLEL_CLAUSES (expr),
9538 NULL_TREE, NULL_TREE);
9539 if (OMP_PARALLEL_COMBINED (expr))
9540 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9541 gimplify_seq_add_stmt (pre_p, g);
9542 *expr_p = NULL_TREE;
9543 }
9544
9545 /* Gimplify the contents of an OMP_TASK statement. This involves
9546 gimplification of the body, as well as scanning the body for used
9547 variables. We need to do this scan now, because variable-sized
9548 decls will be decomposed during gimplification. */
9549
9550 static void
9551 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9552 {
9553 tree expr = *expr_p;
9554 gimple *g;
9555 gimple_seq body = NULL;
9556
9557 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9558 omp_find_clause (OMP_TASK_CLAUSES (expr),
9559 OMP_CLAUSE_UNTIED)
9560 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9561
9562 push_gimplify_context ();
9563
9564 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9565 if (gimple_code (g) == GIMPLE_BIND)
9566 pop_gimplify_context (g);
9567 else
9568 pop_gimplify_context (NULL);
9569
9570 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9571 OMP_TASK);
9572
9573 g = gimple_build_omp_task (body,
9574 OMP_TASK_CLAUSES (expr),
9575 NULL_TREE, NULL_TREE,
9576 NULL_TREE, NULL_TREE, NULL_TREE);
9577 gimplify_seq_add_stmt (pre_p, g);
9578 *expr_p = NULL_TREE;
9579 }
9580
9581 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9582 with non-NULL OMP_FOR_INIT. */
9583
9584 static tree
9585 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9586 {
9587 *walk_subtrees = 0;
9588 switch (TREE_CODE (*tp))
9589 {
9590 case OMP_FOR:
9591 *walk_subtrees = 1;
9592 /* FALLTHRU */
9593 case OMP_SIMD:
9594 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9595 return *tp;
9596 break;
9597 case BIND_EXPR:
9598 case STATEMENT_LIST:
9599 case OMP_PARALLEL:
9600 *walk_subtrees = 1;
9601 break;
9602 default:
9603 break;
9604 }
9605 return NULL_TREE;
9606 }
9607
9608 /* Gimplify the gross structure of an OMP_FOR statement. */
9609
9610 static enum gimplify_status
9611 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9612 {
9613 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9614 enum gimplify_status ret = GS_ALL_DONE;
9615 enum gimplify_status tret;
9616 gomp_for *gfor;
9617 gimple_seq for_body, for_pre_body;
9618 int i;
9619 bitmap has_decl_expr = NULL;
9620 enum omp_region_type ort = ORT_WORKSHARE;
9621
9622 orig_for_stmt = for_stmt = *expr_p;
9623
9624 switch (TREE_CODE (for_stmt))
9625 {
9626 case OMP_FOR:
9627 case OMP_DISTRIBUTE:
9628 break;
9629 case OACC_LOOP:
9630 ort = ORT_ACC;
9631 break;
9632 case OMP_TASKLOOP:
9633 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9634 ort = ORT_UNTIED_TASK;
9635 else
9636 ort = ORT_TASK;
9637 break;
9638 case OMP_SIMD:
9639 ort = ORT_SIMD;
9640 break;
9641 default:
9642 gcc_unreachable ();
9643 }
9644
9645 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9646 clause for the IV. */
9647 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9648 {
9649 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9650 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9651 decl = TREE_OPERAND (t, 0);
9652 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9654 && OMP_CLAUSE_DECL (c) == decl)
9655 {
9656 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9657 break;
9658 }
9659 }
9660
9661 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9662 {
9663 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9664 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9665 find_combined_omp_for, NULL, NULL);
9666 if (inner_for_stmt == NULL_TREE)
9667 {
9668 gcc_assert (seen_error ());
9669 *expr_p = NULL_TREE;
9670 return GS_ERROR;
9671 }
9672 }
9673
9674 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9675 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9676 TREE_CODE (for_stmt));
9677
9678 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9679 gimplify_omp_ctxp->distribute = true;
9680
9681 /* Handle OMP_FOR_INIT. */
9682 for_pre_body = NULL;
9683 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9684 {
9685 has_decl_expr = BITMAP_ALLOC (NULL);
9686 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9687 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9688 == VAR_DECL)
9689 {
9690 t = OMP_FOR_PRE_BODY (for_stmt);
9691 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9692 }
9693 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9694 {
9695 tree_stmt_iterator si;
9696 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9697 tsi_next (&si))
9698 {
9699 t = tsi_stmt (si);
9700 if (TREE_CODE (t) == DECL_EXPR
9701 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9702 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9703 }
9704 }
9705 }
9706 if (OMP_FOR_PRE_BODY (for_stmt))
9707 {
9708 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9709 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9710 else
9711 {
9712 struct gimplify_omp_ctx ctx;
9713 memset (&ctx, 0, sizeof (ctx));
9714 ctx.region_type = ORT_NONE;
9715 gimplify_omp_ctxp = &ctx;
9716 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9717 gimplify_omp_ctxp = NULL;
9718 }
9719 }
9720 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9721
9722 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9723 for_stmt = inner_for_stmt;
9724
9725 /* For taskloop, need to gimplify the start, end and step before the
9726 taskloop, outside of the taskloop omp context. */
9727 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9728 {
9729 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9730 {
9731 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9732 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9733 {
9734 TREE_OPERAND (t, 1)
9735 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9736 pre_p, NULL, false);
9737 tree c = build_omp_clause (input_location,
9738 OMP_CLAUSE_FIRSTPRIVATE);
9739 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9740 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9741 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9742 }
9743
9744 /* Handle OMP_FOR_COND. */
9745 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9746 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9747 {
9748 TREE_OPERAND (t, 1)
9749 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9750 gimple_seq_empty_p (for_pre_body)
9751 ? pre_p : &for_pre_body, NULL,
9752 false);
9753 tree c = build_omp_clause (input_location,
9754 OMP_CLAUSE_FIRSTPRIVATE);
9755 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9756 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9757 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9758 }
9759
9760 /* Handle OMP_FOR_INCR. */
9761 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9762 if (TREE_CODE (t) == MODIFY_EXPR)
9763 {
9764 decl = TREE_OPERAND (t, 0);
9765 t = TREE_OPERAND (t, 1);
9766 tree *tp = &TREE_OPERAND (t, 1);
9767 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9768 tp = &TREE_OPERAND (t, 0);
9769
9770 if (!is_gimple_constant (*tp))
9771 {
9772 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9773 ? pre_p : &for_pre_body;
9774 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9775 tree c = build_omp_clause (input_location,
9776 OMP_CLAUSE_FIRSTPRIVATE);
9777 OMP_CLAUSE_DECL (c) = *tp;
9778 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9779 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9780 }
9781 }
9782 }
9783
9784 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9785 OMP_TASKLOOP);
9786 }
9787
9788 if (orig_for_stmt != for_stmt)
9789 gimplify_omp_ctxp->combined_loop = true;
9790
9791 for_body = NULL;
9792 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9793 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9794 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9795 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9796
9797 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9798 bool is_doacross = false;
9799 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9800 {
9801 is_doacross = true;
9802 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9803 (OMP_FOR_INIT (for_stmt))
9804 * 2);
9805 }
9806 int collapse = 1, tile = 0;
9807 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9808 if (c)
9809 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9810 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9811 if (c)
9812 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9813 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9814 {
9815 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9816 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9817 decl = TREE_OPERAND (t, 0);
9818 gcc_assert (DECL_P (decl));
9819 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9820 || POINTER_TYPE_P (TREE_TYPE (decl)));
9821 if (is_doacross)
9822 {
9823 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9824 gimplify_omp_ctxp->loop_iter_var.quick_push
9825 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9826 else
9827 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9828 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9829 }
9830
9831 /* Make sure the iteration variable is private. */
9832 tree c = NULL_TREE;
9833 tree c2 = NULL_TREE;
9834 if (orig_for_stmt != for_stmt)
9835 /* Do this only on innermost construct for combined ones. */;
9836 else if (ort == ORT_SIMD)
9837 {
9838 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9839 (splay_tree_key) decl);
9840 omp_is_private (gimplify_omp_ctxp, decl,
9841 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9842 != 1));
9843 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9844 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9845 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9846 {
9847 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9848 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9849 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9850 if (has_decl_expr
9851 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9852 {
9853 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9854 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9855 }
9856 struct gimplify_omp_ctx *outer
9857 = gimplify_omp_ctxp->outer_context;
9858 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9859 {
9860 if (outer->region_type == ORT_WORKSHARE
9861 && outer->combined_loop)
9862 {
9863 n = splay_tree_lookup (outer->variables,
9864 (splay_tree_key)decl);
9865 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9866 {
9867 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9868 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9869 }
9870 else
9871 {
9872 struct gimplify_omp_ctx *octx = outer->outer_context;
9873 if (octx
9874 && octx->region_type == ORT_COMBINED_PARALLEL
9875 && octx->outer_context
9876 && (octx->outer_context->region_type
9877 == ORT_WORKSHARE)
9878 && octx->outer_context->combined_loop)
9879 {
9880 octx = octx->outer_context;
9881 n = splay_tree_lookup (octx->variables,
9882 (splay_tree_key)decl);
9883 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9884 {
9885 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9886 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9887 }
9888 }
9889 }
9890 }
9891 }
9892
9893 OMP_CLAUSE_DECL (c) = decl;
9894 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9895 OMP_FOR_CLAUSES (for_stmt) = c;
9896 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9897 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9898 {
9899 if (outer->region_type == ORT_WORKSHARE
9900 && outer->combined_loop)
9901 {
9902 if (outer->outer_context
9903 && (outer->outer_context->region_type
9904 == ORT_COMBINED_PARALLEL))
9905 outer = outer->outer_context;
9906 else if (omp_check_private (outer, decl, false))
9907 outer = NULL;
9908 }
9909 else if (((outer->region_type & ORT_TASK) != 0)
9910 && outer->combined_loop
9911 && !omp_check_private (gimplify_omp_ctxp,
9912 decl, false))
9913 ;
9914 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9915 {
9916 omp_notice_variable (outer, decl, true);
9917 outer = NULL;
9918 }
9919 if (outer)
9920 {
9921 n = splay_tree_lookup (outer->variables,
9922 (splay_tree_key)decl);
9923 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9924 {
9925 omp_add_variable (outer, decl,
9926 GOVD_LASTPRIVATE | GOVD_SEEN);
9927 if (outer->region_type == ORT_COMBINED_PARALLEL
9928 && outer->outer_context
9929 && (outer->outer_context->region_type
9930 == ORT_WORKSHARE)
9931 && outer->outer_context->combined_loop)
9932 {
9933 outer = outer->outer_context;
9934 n = splay_tree_lookup (outer->variables,
9935 (splay_tree_key)decl);
9936 if (omp_check_private (outer, decl, false))
9937 outer = NULL;
9938 else if (n == NULL
9939 || ((n->value & GOVD_DATA_SHARE_CLASS)
9940 == 0))
9941 omp_add_variable (outer, decl,
9942 GOVD_LASTPRIVATE
9943 | GOVD_SEEN);
9944 else
9945 outer = NULL;
9946 }
9947 if (outer && outer->outer_context
9948 && (outer->outer_context->region_type
9949 == ORT_COMBINED_TEAMS))
9950 {
9951 outer = outer->outer_context;
9952 n = splay_tree_lookup (outer->variables,
9953 (splay_tree_key)decl);
9954 if (n == NULL
9955 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9956 omp_add_variable (outer, decl,
9957 GOVD_SHARED | GOVD_SEEN);
9958 else
9959 outer = NULL;
9960 }
9961 if (outer && outer->outer_context)
9962 omp_notice_variable (outer->outer_context, decl,
9963 true);
9964 }
9965 }
9966 }
9967 }
9968 else
9969 {
9970 bool lastprivate
9971 = (!has_decl_expr
9972 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
9973 struct gimplify_omp_ctx *outer
9974 = gimplify_omp_ctxp->outer_context;
9975 if (outer && lastprivate)
9976 {
9977 if (outer->region_type == ORT_WORKSHARE
9978 && outer->combined_loop)
9979 {
9980 n = splay_tree_lookup (outer->variables,
9981 (splay_tree_key)decl);
9982 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9983 {
9984 lastprivate = false;
9985 outer = NULL;
9986 }
9987 else if (outer->outer_context
9988 && (outer->outer_context->region_type
9989 == ORT_COMBINED_PARALLEL))
9990 outer = outer->outer_context;
9991 else if (omp_check_private (outer, decl, false))
9992 outer = NULL;
9993 }
9994 else if (((outer->region_type & ORT_TASK) != 0)
9995 && outer->combined_loop
9996 && !omp_check_private (gimplify_omp_ctxp,
9997 decl, false))
9998 ;
9999 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10000 {
10001 omp_notice_variable (outer, decl, true);
10002 outer = NULL;
10003 }
10004 if (outer)
10005 {
10006 n = splay_tree_lookup (outer->variables,
10007 (splay_tree_key)decl);
10008 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10009 {
10010 omp_add_variable (outer, decl,
10011 GOVD_LASTPRIVATE | GOVD_SEEN);
10012 if (outer->region_type == ORT_COMBINED_PARALLEL
10013 && outer->outer_context
10014 && (outer->outer_context->region_type
10015 == ORT_WORKSHARE)
10016 && outer->outer_context->combined_loop)
10017 {
10018 outer = outer->outer_context;
10019 n = splay_tree_lookup (outer->variables,
10020 (splay_tree_key)decl);
10021 if (omp_check_private (outer, decl, false))
10022 outer = NULL;
10023 else if (n == NULL
10024 || ((n->value & GOVD_DATA_SHARE_CLASS)
10025 == 0))
10026 omp_add_variable (outer, decl,
10027 GOVD_LASTPRIVATE
10028 | GOVD_SEEN);
10029 else
10030 outer = NULL;
10031 }
10032 if (outer && outer->outer_context
10033 && (outer->outer_context->region_type
10034 == ORT_COMBINED_TEAMS))
10035 {
10036 outer = outer->outer_context;
10037 n = splay_tree_lookup (outer->variables,
10038 (splay_tree_key)decl);
10039 if (n == NULL
10040 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10041 omp_add_variable (outer, decl,
10042 GOVD_SHARED | GOVD_SEEN);
10043 else
10044 outer = NULL;
10045 }
10046 if (outer && outer->outer_context)
10047 omp_notice_variable (outer->outer_context, decl,
10048 true);
10049 }
10050 }
10051 }
10052
10053 c = build_omp_clause (input_location,
10054 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10055 : OMP_CLAUSE_PRIVATE);
10056 OMP_CLAUSE_DECL (c) = decl;
10057 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10058 OMP_FOR_CLAUSES (for_stmt) = c;
10059 omp_add_variable (gimplify_omp_ctxp, decl,
10060 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10061 | GOVD_EXPLICIT | GOVD_SEEN);
10062 c = NULL_TREE;
10063 }
10064 }
10065 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10066 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10067 else
10068 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10069
10070 /* If DECL is not a gimple register, create a temporary variable to act
10071 as an iteration counter. This is valid, since DECL cannot be
10072 modified in the body of the loop. Similarly for any iteration vars
10073 in simd with collapse > 1 where the iterator vars must be
10074 lastprivate. */
10075 if (orig_for_stmt != for_stmt)
10076 var = decl;
10077 else if (!is_gimple_reg (decl)
10078 || (ort == ORT_SIMD
10079 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10080 {
10081 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10082 /* Make sure omp_add_variable is not called on it prematurely.
10083 We call it ourselves a few lines later. */
10084 gimplify_omp_ctxp = NULL;
10085 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10086 gimplify_omp_ctxp = ctx;
10087 TREE_OPERAND (t, 0) = var;
10088
10089 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10090
10091 if (ort == ORT_SIMD
10092 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10093 {
10094 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10095 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10096 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10097 OMP_CLAUSE_DECL (c2) = var;
10098 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10099 OMP_FOR_CLAUSES (for_stmt) = c2;
10100 omp_add_variable (gimplify_omp_ctxp, var,
10101 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10102 if (c == NULL_TREE)
10103 {
10104 c = c2;
10105 c2 = NULL_TREE;
10106 }
10107 }
10108 else
10109 omp_add_variable (gimplify_omp_ctxp, var,
10110 GOVD_PRIVATE | GOVD_SEEN);
10111 }
10112 else
10113 var = decl;
10114
10115 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10116 is_gimple_val, fb_rvalue, false);
10117 ret = MIN (ret, tret);
10118 if (ret == GS_ERROR)
10119 return ret;
10120
10121 /* Handle OMP_FOR_COND. */
10122 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10123 gcc_assert (COMPARISON_CLASS_P (t));
10124 gcc_assert (TREE_OPERAND (t, 0) == decl);
10125
10126 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10127 is_gimple_val, fb_rvalue, false);
10128 ret = MIN (ret, tret);
10129
10130 /* Handle OMP_FOR_INCR. */
10131 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10132 switch (TREE_CODE (t))
10133 {
10134 case PREINCREMENT_EXPR:
10135 case POSTINCREMENT_EXPR:
10136 {
10137 tree decl = TREE_OPERAND (t, 0);
10138 /* c_omp_for_incr_canonicalize_ptr() should have been
10139 called to massage things appropriately. */
10140 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10141
10142 if (orig_for_stmt != for_stmt)
10143 break;
10144 t = build_int_cst (TREE_TYPE (decl), 1);
10145 if (c)
10146 OMP_CLAUSE_LINEAR_STEP (c) = t;
10147 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10148 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10149 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10150 break;
10151 }
10152
10153 case PREDECREMENT_EXPR:
10154 case POSTDECREMENT_EXPR:
10155 /* c_omp_for_incr_canonicalize_ptr() should have been
10156 called to massage things appropriately. */
10157 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10158 if (orig_for_stmt != for_stmt)
10159 break;
10160 t = build_int_cst (TREE_TYPE (decl), -1);
10161 if (c)
10162 OMP_CLAUSE_LINEAR_STEP (c) = t;
10163 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10164 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10165 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10166 break;
10167
10168 case MODIFY_EXPR:
10169 gcc_assert (TREE_OPERAND (t, 0) == decl);
10170 TREE_OPERAND (t, 0) = var;
10171
10172 t = TREE_OPERAND (t, 1);
10173 switch (TREE_CODE (t))
10174 {
10175 case PLUS_EXPR:
10176 if (TREE_OPERAND (t, 1) == decl)
10177 {
10178 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10179 TREE_OPERAND (t, 0) = var;
10180 break;
10181 }
10182
10183 /* Fallthru. */
10184 case MINUS_EXPR:
10185 case POINTER_PLUS_EXPR:
10186 gcc_assert (TREE_OPERAND (t, 0) == decl);
10187 TREE_OPERAND (t, 0) = var;
10188 break;
10189 default:
10190 gcc_unreachable ();
10191 }
10192
10193 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10194 is_gimple_val, fb_rvalue, false);
10195 ret = MIN (ret, tret);
10196 if (c)
10197 {
10198 tree step = TREE_OPERAND (t, 1);
10199 tree stept = TREE_TYPE (decl);
10200 if (POINTER_TYPE_P (stept))
10201 stept = sizetype;
10202 step = fold_convert (stept, step);
10203 if (TREE_CODE (t) == MINUS_EXPR)
10204 step = fold_build1 (NEGATE_EXPR, stept, step);
10205 OMP_CLAUSE_LINEAR_STEP (c) = step;
10206 if (step != TREE_OPERAND (t, 1))
10207 {
10208 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10209 &for_pre_body, NULL,
10210 is_gimple_val, fb_rvalue, false);
10211 ret = MIN (ret, tret);
10212 }
10213 }
10214 break;
10215
10216 default:
10217 gcc_unreachable ();
10218 }
10219
10220 if (c2)
10221 {
10222 gcc_assert (c);
10223 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10224 }
10225
10226 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10227 {
10228 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10229 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10230 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10231 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10232 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10233 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10234 && OMP_CLAUSE_DECL (c) == decl)
10235 {
10236 if (is_doacross && (collapse == 1 || i >= collapse))
10237 t = var;
10238 else
10239 {
10240 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10241 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10242 gcc_assert (TREE_OPERAND (t, 0) == var);
10243 t = TREE_OPERAND (t, 1);
10244 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10245 || TREE_CODE (t) == MINUS_EXPR
10246 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10247 gcc_assert (TREE_OPERAND (t, 0) == var);
10248 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10249 is_doacross ? var : decl,
10250 TREE_OPERAND (t, 1));
10251 }
10252 gimple_seq *seq;
10253 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10254 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10255 else
10256 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10257 gimplify_assign (decl, t, seq);
10258 }
10259 }
10260 }
10261
10262 BITMAP_FREE (has_decl_expr);
10263
10264 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10265 {
10266 push_gimplify_context ();
10267 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10268 {
10269 OMP_FOR_BODY (orig_for_stmt)
10270 = build3 (BIND_EXPR, void_type_node, NULL,
10271 OMP_FOR_BODY (orig_for_stmt), NULL);
10272 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10273 }
10274 }
10275
10276 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10277 &for_body);
10278
10279 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10280 {
10281 if (gimple_code (g) == GIMPLE_BIND)
10282 pop_gimplify_context (g);
10283 else
10284 pop_gimplify_context (NULL);
10285 }
10286
10287 if (orig_for_stmt != for_stmt)
10288 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10289 {
10290 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10291 decl = TREE_OPERAND (t, 0);
10292 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10293 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10294 gimplify_omp_ctxp = ctx->outer_context;
10295 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10296 gimplify_omp_ctxp = ctx;
10297 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10298 TREE_OPERAND (t, 0) = var;
10299 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10300 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10301 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10302 }
10303
10304 gimplify_adjust_omp_clauses (pre_p, for_body,
10305 &OMP_FOR_CLAUSES (orig_for_stmt),
10306 TREE_CODE (orig_for_stmt));
10307
10308 int kind;
10309 switch (TREE_CODE (orig_for_stmt))
10310 {
10311 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10312 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10313 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10314 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10315 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10316 default:
10317 gcc_unreachable ();
10318 }
10319 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10320 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10321 for_pre_body);
10322 if (orig_for_stmt != for_stmt)
10323 gimple_omp_for_set_combined_p (gfor, true);
10324 if (gimplify_omp_ctxp
10325 && (gimplify_omp_ctxp->combined_loop
10326 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10327 && gimplify_omp_ctxp->outer_context
10328 && gimplify_omp_ctxp->outer_context->combined_loop)))
10329 {
10330 gimple_omp_for_set_combined_into_p (gfor, true);
10331 if (gimplify_omp_ctxp->combined_loop)
10332 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10333 else
10334 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10335 }
10336
10337 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10338 {
10339 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10340 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10341 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10342 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10343 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10344 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10345 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10346 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10347 }
10348
10349 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10350 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10351 The outer taskloop stands for computing the number of iterations,
10352 counts for collapsed loops and holding taskloop specific clauses.
10353 The task construct stands for the effect of data sharing on the
10354 explicit task it creates and the inner taskloop stands for expansion
10355 of the static loop inside of the explicit task construct. */
10356 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10357 {
10358 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10359 tree task_clauses = NULL_TREE;
10360 tree c = *gfor_clauses_ptr;
10361 tree *gtask_clauses_ptr = &task_clauses;
10362 tree outer_for_clauses = NULL_TREE;
10363 tree *gforo_clauses_ptr = &outer_for_clauses;
10364 for (; c; c = OMP_CLAUSE_CHAIN (c))
10365 switch (OMP_CLAUSE_CODE (c))
10366 {
10367 /* These clauses are allowed on task, move them there. */
10368 case OMP_CLAUSE_SHARED:
10369 case OMP_CLAUSE_FIRSTPRIVATE:
10370 case OMP_CLAUSE_DEFAULT:
10371 case OMP_CLAUSE_IF:
10372 case OMP_CLAUSE_UNTIED:
10373 case OMP_CLAUSE_FINAL:
10374 case OMP_CLAUSE_MERGEABLE:
10375 case OMP_CLAUSE_PRIORITY:
10376 *gtask_clauses_ptr = c;
10377 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10378 break;
10379 case OMP_CLAUSE_PRIVATE:
10380 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10381 {
10382 /* We want private on outer for and firstprivate
10383 on task. */
10384 *gtask_clauses_ptr
10385 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10386 OMP_CLAUSE_FIRSTPRIVATE);
10387 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10388 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10389 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10390 *gforo_clauses_ptr = c;
10391 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10392 }
10393 else
10394 {
10395 *gtask_clauses_ptr = c;
10396 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10397 }
10398 break;
10399 /* These clauses go into outer taskloop clauses. */
10400 case OMP_CLAUSE_GRAINSIZE:
10401 case OMP_CLAUSE_NUM_TASKS:
10402 case OMP_CLAUSE_NOGROUP:
10403 *gforo_clauses_ptr = c;
10404 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10405 break;
10406 /* Taskloop clause we duplicate on both taskloops. */
10407 case OMP_CLAUSE_COLLAPSE:
10408 *gfor_clauses_ptr = c;
10409 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10410 *gforo_clauses_ptr = copy_node (c);
10411 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10412 break;
10413 /* For lastprivate, keep the clause on inner taskloop, and add
10414 a shared clause on task. If the same decl is also firstprivate,
10415 add also firstprivate clause on the inner taskloop. */
10416 case OMP_CLAUSE_LASTPRIVATE:
10417 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10418 {
10419 /* For taskloop C++ lastprivate IVs, we want:
10420 1) private on outer taskloop
10421 2) firstprivate and shared on task
10422 3) lastprivate on inner taskloop */
10423 *gtask_clauses_ptr
10424 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10425 OMP_CLAUSE_FIRSTPRIVATE);
10426 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10427 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10428 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10429 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10430 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10431 OMP_CLAUSE_PRIVATE);
10432 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10433 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10434 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10435 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10436 }
10437 *gfor_clauses_ptr = c;
10438 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10439 *gtask_clauses_ptr
10440 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10441 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10442 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10443 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10444 gtask_clauses_ptr
10445 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10446 break;
10447 default:
10448 gcc_unreachable ();
10449 }
10450 *gfor_clauses_ptr = NULL_TREE;
10451 *gtask_clauses_ptr = NULL_TREE;
10452 *gforo_clauses_ptr = NULL_TREE;
10453 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10454 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10455 NULL_TREE, NULL_TREE, NULL_TREE);
10456 gimple_omp_task_set_taskloop_p (g, true);
10457 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10458 gomp_for *gforo
10459 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10460 gimple_omp_for_collapse (gfor),
10461 gimple_omp_for_pre_body (gfor));
10462 gimple_omp_for_set_pre_body (gfor, NULL);
10463 gimple_omp_for_set_combined_p (gforo, true);
10464 gimple_omp_for_set_combined_into_p (gfor, true);
10465 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10466 {
10467 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10468 tree v = create_tmp_var (type);
10469 gimple_omp_for_set_index (gforo, i, v);
10470 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10471 gimple_omp_for_set_initial (gforo, i, t);
10472 gimple_omp_for_set_cond (gforo, i,
10473 gimple_omp_for_cond (gfor, i));
10474 t = unshare_expr (gimple_omp_for_final (gfor, i));
10475 gimple_omp_for_set_final (gforo, i, t);
10476 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10477 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10478 TREE_OPERAND (t, 0) = v;
10479 gimple_omp_for_set_incr (gforo, i, t);
10480 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10481 OMP_CLAUSE_DECL (t) = v;
10482 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10483 gimple_omp_for_set_clauses (gforo, t);
10484 }
10485 gimplify_seq_add_stmt (pre_p, gforo);
10486 }
10487 else
10488 gimplify_seq_add_stmt (pre_p, gfor);
10489 if (ret != GS_ALL_DONE)
10490 return GS_ERROR;
10491 *expr_p = NULL_TREE;
10492 return GS_ALL_DONE;
10493 }
10494
10495 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10496 of OMP_TARGET's body. */
10497
10498 static tree
10499 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10500 {
10501 *walk_subtrees = 0;
10502 switch (TREE_CODE (*tp))
10503 {
10504 case OMP_TEAMS:
10505 return *tp;
10506 case BIND_EXPR:
10507 case STATEMENT_LIST:
10508 *walk_subtrees = 1;
10509 break;
10510 default:
10511 break;
10512 }
10513 return NULL_TREE;
10514 }
10515
10516 /* Helper function of optimize_target_teams, determine if the expression
10517 can be computed safely before the target construct on the host. */
10518
10519 static tree
10520 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10521 {
10522 splay_tree_node n;
10523
10524 if (TYPE_P (*tp))
10525 {
10526 *walk_subtrees = 0;
10527 return NULL_TREE;
10528 }
10529 switch (TREE_CODE (*tp))
10530 {
10531 case VAR_DECL:
10532 case PARM_DECL:
10533 case RESULT_DECL:
10534 *walk_subtrees = 0;
10535 if (error_operand_p (*tp)
10536 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10537 || DECL_HAS_VALUE_EXPR_P (*tp)
10538 || DECL_THREAD_LOCAL_P (*tp)
10539 || TREE_SIDE_EFFECTS (*tp)
10540 || TREE_THIS_VOLATILE (*tp))
10541 return *tp;
10542 if (is_global_var (*tp)
10543 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10544 || lookup_attribute ("omp declare target link",
10545 DECL_ATTRIBUTES (*tp))))
10546 return *tp;
10547 if (VAR_P (*tp)
10548 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10549 && !is_global_var (*tp)
10550 && decl_function_context (*tp) == current_function_decl)
10551 return *tp;
10552 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10553 (splay_tree_key) *tp);
10554 if (n == NULL)
10555 {
10556 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10557 return NULL_TREE;
10558 return *tp;
10559 }
10560 else if (n->value & GOVD_LOCAL)
10561 return *tp;
10562 else if (n->value & GOVD_FIRSTPRIVATE)
10563 return NULL_TREE;
10564 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10565 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10566 return NULL_TREE;
10567 return *tp;
10568 case INTEGER_CST:
10569 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10570 return *tp;
10571 return NULL_TREE;
10572 case TARGET_EXPR:
10573 if (TARGET_EXPR_INITIAL (*tp)
10574 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10575 return *tp;
10576 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10577 walk_subtrees, NULL);
10578 /* Allow some reasonable subset of integral arithmetics. */
10579 case PLUS_EXPR:
10580 case MINUS_EXPR:
10581 case MULT_EXPR:
10582 case TRUNC_DIV_EXPR:
10583 case CEIL_DIV_EXPR:
10584 case FLOOR_DIV_EXPR:
10585 case ROUND_DIV_EXPR:
10586 case TRUNC_MOD_EXPR:
10587 case CEIL_MOD_EXPR:
10588 case FLOOR_MOD_EXPR:
10589 case ROUND_MOD_EXPR:
10590 case RDIV_EXPR:
10591 case EXACT_DIV_EXPR:
10592 case MIN_EXPR:
10593 case MAX_EXPR:
10594 case LSHIFT_EXPR:
10595 case RSHIFT_EXPR:
10596 case BIT_IOR_EXPR:
10597 case BIT_XOR_EXPR:
10598 case BIT_AND_EXPR:
10599 case NEGATE_EXPR:
10600 case ABS_EXPR:
10601 case BIT_NOT_EXPR:
10602 case NON_LVALUE_EXPR:
10603 CASE_CONVERT:
10604 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10605 return *tp;
10606 return NULL_TREE;
10607 /* And disallow anything else, except for comparisons. */
10608 default:
10609 if (COMPARISON_CLASS_P (*tp))
10610 return NULL_TREE;
10611 return *tp;
10612 }
10613 }
10614
10615 /* Try to determine if the num_teams and/or thread_limit expressions
10616 can have their values determined already before entering the
10617 target construct.
10618 INTEGER_CSTs trivially are,
10619 integral decls that are firstprivate (explicitly or implicitly)
10620 or explicitly map(always, to:) or map(always, tofrom:) on the target
10621 region too, and expressions involving simple arithmetics on those
10622 too, function calls are not ok, dereferencing something neither etc.
10623 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10624 EXPR based on what we find:
10625 0 stands for clause not specified at all, use implementation default
10626 -1 stands for value that can't be determined easily before entering
10627 the target construct.
10628 If teams construct is not present at all, use 1 for num_teams
10629 and 0 for thread_limit (only one team is involved, and the thread
10630 limit is implementation defined. */
10631
10632 static void
10633 optimize_target_teams (tree target, gimple_seq *pre_p)
10634 {
10635 tree body = OMP_BODY (target);
10636 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10637 tree num_teams = integer_zero_node;
10638 tree thread_limit = integer_zero_node;
10639 location_t num_teams_loc = EXPR_LOCATION (target);
10640 location_t thread_limit_loc = EXPR_LOCATION (target);
10641 tree c, *p, expr;
10642 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10643
10644 if (teams == NULL_TREE)
10645 num_teams = integer_one_node;
10646 else
10647 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10648 {
10649 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10650 {
10651 p = &num_teams;
10652 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10653 }
10654 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10655 {
10656 p = &thread_limit;
10657 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10658 }
10659 else
10660 continue;
10661 expr = OMP_CLAUSE_OPERAND (c, 0);
10662 if (TREE_CODE (expr) == INTEGER_CST)
10663 {
10664 *p = expr;
10665 continue;
10666 }
10667 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10668 {
10669 *p = integer_minus_one_node;
10670 continue;
10671 }
10672 *p = expr;
10673 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10674 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10675 == GS_ERROR)
10676 {
10677 gimplify_omp_ctxp = target_ctx;
10678 *p = integer_minus_one_node;
10679 continue;
10680 }
10681 gimplify_omp_ctxp = target_ctx;
10682 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10683 OMP_CLAUSE_OPERAND (c, 0) = *p;
10684 }
10685 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10686 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10687 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10688 OMP_TARGET_CLAUSES (target) = c;
10689 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10690 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10691 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10692 OMP_TARGET_CLAUSES (target) = c;
10693 }
10694
10695 /* Gimplify the gross structure of several OMP constructs. */
10696
10697 static void
10698 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10699 {
10700 tree expr = *expr_p;
10701 gimple *stmt;
10702 gimple_seq body = NULL;
10703 enum omp_region_type ort;
10704
10705 switch (TREE_CODE (expr))
10706 {
10707 case OMP_SECTIONS:
10708 case OMP_SINGLE:
10709 ort = ORT_WORKSHARE;
10710 break;
10711 case OMP_TARGET:
10712 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10713 break;
10714 case OACC_KERNELS:
10715 ort = ORT_ACC_KERNELS;
10716 break;
10717 case OACC_PARALLEL:
10718 ort = ORT_ACC_PARALLEL;
10719 break;
10720 case OACC_DATA:
10721 ort = ORT_ACC_DATA;
10722 break;
10723 case OMP_TARGET_DATA:
10724 ort = ORT_TARGET_DATA;
10725 break;
10726 case OMP_TEAMS:
10727 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10728 break;
10729 case OACC_HOST_DATA:
10730 ort = ORT_ACC_HOST_DATA;
10731 break;
10732 default:
10733 gcc_unreachable ();
10734 }
10735 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10736 TREE_CODE (expr));
10737 if (TREE_CODE (expr) == OMP_TARGET)
10738 optimize_target_teams (expr, pre_p);
10739 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10740 {
10741 push_gimplify_context ();
10742 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10743 if (gimple_code (g) == GIMPLE_BIND)
10744 pop_gimplify_context (g);
10745 else
10746 pop_gimplify_context (NULL);
10747 if ((ort & ORT_TARGET_DATA) != 0)
10748 {
10749 enum built_in_function end_ix;
10750 switch (TREE_CODE (expr))
10751 {
10752 case OACC_DATA:
10753 case OACC_HOST_DATA:
10754 end_ix = BUILT_IN_GOACC_DATA_END;
10755 break;
10756 case OMP_TARGET_DATA:
10757 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10758 break;
10759 default:
10760 gcc_unreachable ();
10761 }
10762 tree fn = builtin_decl_explicit (end_ix);
10763 g = gimple_build_call (fn, 0);
10764 gimple_seq cleanup = NULL;
10765 gimple_seq_add_stmt (&cleanup, g);
10766 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10767 body = NULL;
10768 gimple_seq_add_stmt (&body, g);
10769 }
10770 }
10771 else
10772 gimplify_and_add (OMP_BODY (expr), &body);
10773 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10774 TREE_CODE (expr));
10775
10776 switch (TREE_CODE (expr))
10777 {
10778 case OACC_DATA:
10779 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10780 OMP_CLAUSES (expr));
10781 break;
10782 case OACC_KERNELS:
10783 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10784 OMP_CLAUSES (expr));
10785 break;
10786 case OACC_HOST_DATA:
10787 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10788 OMP_CLAUSES (expr));
10789 break;
10790 case OACC_PARALLEL:
10791 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10792 OMP_CLAUSES (expr));
10793 break;
10794 case OMP_SECTIONS:
10795 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10796 break;
10797 case OMP_SINGLE:
10798 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10799 break;
10800 case OMP_TARGET:
10801 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10802 OMP_CLAUSES (expr));
10803 break;
10804 case OMP_TARGET_DATA:
10805 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10806 OMP_CLAUSES (expr));
10807 break;
10808 case OMP_TEAMS:
10809 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10810 break;
10811 default:
10812 gcc_unreachable ();
10813 }
10814
10815 gimplify_seq_add_stmt (pre_p, stmt);
10816 *expr_p = NULL_TREE;
10817 }
10818
10819 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10820 target update constructs. */
10821
10822 static void
10823 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10824 {
10825 tree expr = *expr_p;
10826 int kind;
10827 gomp_target *stmt;
10828 enum omp_region_type ort = ORT_WORKSHARE;
10829
10830 switch (TREE_CODE (expr))
10831 {
10832 case OACC_ENTER_DATA:
10833 case OACC_EXIT_DATA:
10834 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10835 ort = ORT_ACC;
10836 break;
10837 case OACC_UPDATE:
10838 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10839 ort = ORT_ACC;
10840 break;
10841 case OMP_TARGET_UPDATE:
10842 kind = GF_OMP_TARGET_KIND_UPDATE;
10843 break;
10844 case OMP_TARGET_ENTER_DATA:
10845 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10846 break;
10847 case OMP_TARGET_EXIT_DATA:
10848 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10849 break;
10850 default:
10851 gcc_unreachable ();
10852 }
10853 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10854 ort, TREE_CODE (expr));
10855 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10856 TREE_CODE (expr));
10857 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10858
10859 gimplify_seq_add_stmt (pre_p, stmt);
10860 *expr_p = NULL_TREE;
10861 }
10862
10863 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10864 stabilized the lhs of the atomic operation as *ADDR. Return true if
10865 EXPR is this stabilized form. */
10866
10867 static bool
10868 goa_lhs_expr_p (tree expr, tree addr)
10869 {
10870 /* Also include casts to other type variants. The C front end is fond
10871 of adding these for e.g. volatile variables. This is like
10872 STRIP_TYPE_NOPS but includes the main variant lookup. */
10873 STRIP_USELESS_TYPE_CONVERSION (expr);
10874
10875 if (TREE_CODE (expr) == INDIRECT_REF)
10876 {
10877 expr = TREE_OPERAND (expr, 0);
10878 while (expr != addr
10879 && (CONVERT_EXPR_P (expr)
10880 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10881 && TREE_CODE (expr) == TREE_CODE (addr)
10882 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10883 {
10884 expr = TREE_OPERAND (expr, 0);
10885 addr = TREE_OPERAND (addr, 0);
10886 }
10887 if (expr == addr)
10888 return true;
10889 return (TREE_CODE (addr) == ADDR_EXPR
10890 && TREE_CODE (expr) == ADDR_EXPR
10891 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10892 }
10893 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10894 return true;
10895 return false;
10896 }
10897
10898 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10899 expression does not involve the lhs, evaluate it into a temporary.
10900 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10901 or -1 if an error was encountered. */
10902
10903 static int
10904 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10905 tree lhs_var)
10906 {
10907 tree expr = *expr_p;
10908 int saw_lhs;
10909
10910 if (goa_lhs_expr_p (expr, lhs_addr))
10911 {
10912 *expr_p = lhs_var;
10913 return 1;
10914 }
10915 if (is_gimple_val (expr))
10916 return 0;
10917
10918 saw_lhs = 0;
10919 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10920 {
10921 case tcc_binary:
10922 case tcc_comparison:
10923 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10924 lhs_var);
10925 /* FALLTHRU */
10926 case tcc_unary:
10927 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10928 lhs_var);
10929 break;
10930 case tcc_expression:
10931 switch (TREE_CODE (expr))
10932 {
10933 case TRUTH_ANDIF_EXPR:
10934 case TRUTH_ORIF_EXPR:
10935 case TRUTH_AND_EXPR:
10936 case TRUTH_OR_EXPR:
10937 case TRUTH_XOR_EXPR:
10938 case BIT_INSERT_EXPR:
10939 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10940 lhs_addr, lhs_var);
10941 /* FALLTHRU */
10942 case TRUTH_NOT_EXPR:
10943 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10944 lhs_addr, lhs_var);
10945 break;
10946 case COMPOUND_EXPR:
10947 /* Break out any preevaluations from cp_build_modify_expr. */
10948 for (; TREE_CODE (expr) == COMPOUND_EXPR;
10949 expr = TREE_OPERAND (expr, 1))
10950 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10951 *expr_p = expr;
10952 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10953 default:
10954 break;
10955 }
10956 break;
10957 case tcc_reference:
10958 if (TREE_CODE (expr) == BIT_FIELD_REF)
10959 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10960 lhs_addr, lhs_var);
10961 break;
10962 default:
10963 break;
10964 }
10965
10966 if (saw_lhs == 0)
10967 {
10968 enum gimplify_status gs;
10969 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10970 if (gs != GS_ALL_DONE)
10971 saw_lhs = -1;
10972 }
10973
10974 return saw_lhs;
10975 }
10976
10977 /* Gimplify an OMP_ATOMIC statement. */
10978
10979 static enum gimplify_status
10980 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10981 {
10982 tree addr = TREE_OPERAND (*expr_p, 0);
10983 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10984 ? NULL : TREE_OPERAND (*expr_p, 1);
10985 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10986 tree tmp_load;
10987 gomp_atomic_load *loadstmt;
10988 gomp_atomic_store *storestmt;
10989
10990 tmp_load = create_tmp_reg (type);
10991 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10992 return GS_ERROR;
10993
10994 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10995 != GS_ALL_DONE)
10996 return GS_ERROR;
10997
10998 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
10999 gimplify_seq_add_stmt (pre_p, loadstmt);
11000 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11001 != GS_ALL_DONE)
11002 return GS_ERROR;
11003
11004 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11005 rhs = tmp_load;
11006 storestmt = gimple_build_omp_atomic_store (rhs);
11007 gimplify_seq_add_stmt (pre_p, storestmt);
11008 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11009 {
11010 gimple_omp_atomic_set_seq_cst (loadstmt);
11011 gimple_omp_atomic_set_seq_cst (storestmt);
11012 }
11013 switch (TREE_CODE (*expr_p))
11014 {
11015 case OMP_ATOMIC_READ:
11016 case OMP_ATOMIC_CAPTURE_OLD:
11017 *expr_p = tmp_load;
11018 gimple_omp_atomic_set_need_value (loadstmt);
11019 break;
11020 case OMP_ATOMIC_CAPTURE_NEW:
11021 *expr_p = rhs;
11022 gimple_omp_atomic_set_need_value (storestmt);
11023 break;
11024 default:
11025 *expr_p = NULL;
11026 break;
11027 }
11028
11029 return GS_ALL_DONE;
11030 }
11031
11032 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11033 body, and adding some EH bits. */
11034
11035 static enum gimplify_status
11036 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11037 {
11038 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11039 gimple *body_stmt;
11040 gtransaction *trans_stmt;
11041 gimple_seq body = NULL;
11042 int subcode = 0;
11043
11044 /* Wrap the transaction body in a BIND_EXPR so we have a context
11045 where to put decls for OMP. */
11046 if (TREE_CODE (tbody) != BIND_EXPR)
11047 {
11048 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11049 TREE_SIDE_EFFECTS (bind) = 1;
11050 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11051 TRANSACTION_EXPR_BODY (expr) = bind;
11052 }
11053
11054 push_gimplify_context ();
11055 temp = voidify_wrapper_expr (*expr_p, NULL);
11056
11057 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11058 pop_gimplify_context (body_stmt);
11059
11060 trans_stmt = gimple_build_transaction (body);
11061 if (TRANSACTION_EXPR_OUTER (expr))
11062 subcode = GTMA_IS_OUTER;
11063 else if (TRANSACTION_EXPR_RELAXED (expr))
11064 subcode = GTMA_IS_RELAXED;
11065 gimple_transaction_set_subcode (trans_stmt, subcode);
11066
11067 gimplify_seq_add_stmt (pre_p, trans_stmt);
11068
11069 if (temp)
11070 {
11071 *expr_p = temp;
11072 return GS_OK;
11073 }
11074
11075 *expr_p = NULL_TREE;
11076 return GS_ALL_DONE;
11077 }
11078
11079 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11080 is the OMP_BODY of the original EXPR (which has already been
11081 gimplified so it's not present in the EXPR).
11082
11083 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11084
11085 static gimple *
11086 gimplify_omp_ordered (tree expr, gimple_seq body)
11087 {
11088 tree c, decls;
11089 int failures = 0;
11090 unsigned int i;
11091 tree source_c = NULL_TREE;
11092 tree sink_c = NULL_TREE;
11093
11094 if (gimplify_omp_ctxp)
11095 {
11096 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11097 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11098 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11099 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11100 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11101 {
11102 error_at (OMP_CLAUSE_LOCATION (c),
11103 "%<ordered%> construct with %<depend%> clause must be "
11104 "closely nested inside a loop with %<ordered%> clause "
11105 "with a parameter");
11106 failures++;
11107 }
11108 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11109 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11110 {
11111 bool fail = false;
11112 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11113 decls && TREE_CODE (decls) == TREE_LIST;
11114 decls = TREE_CHAIN (decls), ++i)
11115 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11116 continue;
11117 else if (TREE_VALUE (decls)
11118 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11119 {
11120 error_at (OMP_CLAUSE_LOCATION (c),
11121 "variable %qE is not an iteration "
11122 "of outermost loop %d, expected %qE",
11123 TREE_VALUE (decls), i + 1,
11124 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11125 fail = true;
11126 failures++;
11127 }
11128 else
11129 TREE_VALUE (decls)
11130 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11131 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11132 {
11133 error_at (OMP_CLAUSE_LOCATION (c),
11134 "number of variables in %<depend(sink)%> "
11135 "clause does not match number of "
11136 "iteration variables");
11137 failures++;
11138 }
11139 sink_c = c;
11140 }
11141 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11142 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11143 {
11144 if (source_c)
11145 {
11146 error_at (OMP_CLAUSE_LOCATION (c),
11147 "more than one %<depend(source)%> clause on an "
11148 "%<ordered%> construct");
11149 failures++;
11150 }
11151 else
11152 source_c = c;
11153 }
11154 }
11155 if (source_c && sink_c)
11156 {
11157 error_at (OMP_CLAUSE_LOCATION (source_c),
11158 "%<depend(source)%> clause specified together with "
11159 "%<depend(sink:)%> clauses on the same construct");
11160 failures++;
11161 }
11162
11163 if (failures)
11164 return gimple_build_nop ();
11165 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11166 }
11167
11168 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11169 expression produces a value to be used as an operand inside a GIMPLE
11170 statement, the value will be stored back in *EXPR_P. This value will
11171 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11172 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11173 emitted in PRE_P and POST_P.
11174
11175 Additionally, this process may overwrite parts of the input
11176 expression during gimplification. Ideally, it should be
11177 possible to do non-destructive gimplification.
11178
11179 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11180 the expression needs to evaluate to a value to be used as
11181 an operand in a GIMPLE statement, this value will be stored in
11182 *EXPR_P on exit. This happens when the caller specifies one
11183 of fb_lvalue or fb_rvalue fallback flags.
11184
11185 PRE_P will contain the sequence of GIMPLE statements corresponding
11186 to the evaluation of EXPR and all the side-effects that must
11187 be executed before the main expression. On exit, the last
11188 statement of PRE_P is the core statement being gimplified. For
11189 instance, when gimplifying 'if (++a)' the last statement in
11190 PRE_P will be 'if (t.1)' where t.1 is the result of
11191 pre-incrementing 'a'.
11192
11193 POST_P will contain the sequence of GIMPLE statements corresponding
11194 to the evaluation of all the side-effects that must be executed
11195 after the main expression. If this is NULL, the post
11196 side-effects are stored at the end of PRE_P.
11197
11198 The reason why the output is split in two is to handle post
11199 side-effects explicitly. In some cases, an expression may have
11200 inner and outer post side-effects which need to be emitted in
11201 an order different from the one given by the recursive
11202 traversal. For instance, for the expression (*p--)++ the post
11203 side-effects of '--' must actually occur *after* the post
11204 side-effects of '++'. However, gimplification will first visit
11205 the inner expression, so if a separate POST sequence was not
11206 used, the resulting sequence would be:
11207
11208 1 t.1 = *p
11209 2 p = p - 1
11210 3 t.2 = t.1 + 1
11211 4 *p = t.2
11212
11213 However, the post-decrement operation in line #2 must not be
11214 evaluated until after the store to *p at line #4, so the
11215 correct sequence should be:
11216
11217 1 t.1 = *p
11218 2 t.2 = t.1 + 1
11219 3 *p = t.2
11220 4 p = p - 1
11221
11222 So, by specifying a separate post queue, it is possible
11223 to emit the post side-effects in the correct order.
11224 If POST_P is NULL, an internal queue will be used. Before
11225 returning to the caller, the sequence POST_P is appended to
11226 the main output sequence PRE_P.
11227
11228 GIMPLE_TEST_F points to a function that takes a tree T and
11229 returns nonzero if T is in the GIMPLE form requested by the
11230 caller. The GIMPLE predicates are in gimple.c.
11231
11232 FALLBACK tells the function what sort of a temporary we want if
11233 gimplification cannot produce an expression that complies with
11234 GIMPLE_TEST_F.
11235
11236 fb_none means that no temporary should be generated
11237 fb_rvalue means that an rvalue is OK to generate
11238 fb_lvalue means that an lvalue is OK to generate
11239 fb_either means that either is OK, but an lvalue is preferable.
11240 fb_mayfail means that gimplification may fail (in which case
11241 GS_ERROR will be returned)
11242
11243 The return value is either GS_ERROR or GS_ALL_DONE, since this
11244 function iterates until EXPR is completely gimplified or an error
11245 occurs. */
11246
11247 enum gimplify_status
11248 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11249 bool (*gimple_test_f) (tree), fallback_t fallback)
11250 {
11251 tree tmp;
11252 gimple_seq internal_pre = NULL;
11253 gimple_seq internal_post = NULL;
11254 tree save_expr;
11255 bool is_statement;
11256 location_t saved_location;
11257 enum gimplify_status ret;
11258 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11259 tree label;
11260
11261 save_expr = *expr_p;
11262 if (save_expr == NULL_TREE)
11263 return GS_ALL_DONE;
11264
11265 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11266 is_statement = gimple_test_f == is_gimple_stmt;
11267 if (is_statement)
11268 gcc_assert (pre_p);
11269
11270 /* Consistency checks. */
11271 if (gimple_test_f == is_gimple_reg)
11272 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11273 else if (gimple_test_f == is_gimple_val
11274 || gimple_test_f == is_gimple_call_addr
11275 || gimple_test_f == is_gimple_condexpr
11276 || gimple_test_f == is_gimple_mem_rhs
11277 || gimple_test_f == is_gimple_mem_rhs_or_call
11278 || gimple_test_f == is_gimple_reg_rhs
11279 || gimple_test_f == is_gimple_reg_rhs_or_call
11280 || gimple_test_f == is_gimple_asm_val
11281 || gimple_test_f == is_gimple_mem_ref_addr)
11282 gcc_assert (fallback & fb_rvalue);
11283 else if (gimple_test_f == is_gimple_min_lval
11284 || gimple_test_f == is_gimple_lvalue)
11285 gcc_assert (fallback & fb_lvalue);
11286 else if (gimple_test_f == is_gimple_addressable)
11287 gcc_assert (fallback & fb_either);
11288 else if (gimple_test_f == is_gimple_stmt)
11289 gcc_assert (fallback == fb_none);
11290 else
11291 {
11292 /* We should have recognized the GIMPLE_TEST_F predicate to
11293 know what kind of fallback to use in case a temporary is
11294 needed to hold the value or address of *EXPR_P. */
11295 gcc_unreachable ();
11296 }
11297
11298 /* We used to check the predicate here and return immediately if it
11299 succeeds. This is wrong; the design is for gimplification to be
11300 idempotent, and for the predicates to only test for valid forms, not
11301 whether they are fully simplified. */
11302 if (pre_p == NULL)
11303 pre_p = &internal_pre;
11304
11305 if (post_p == NULL)
11306 post_p = &internal_post;
11307
11308 /* Remember the last statements added to PRE_P and POST_P. Every
11309 new statement added by the gimplification helpers needs to be
11310 annotated with location information. To centralize the
11311 responsibility, we remember the last statement that had been
11312 added to both queues before gimplifying *EXPR_P. If
11313 gimplification produces new statements in PRE_P and POST_P, those
11314 statements will be annotated with the same location information
11315 as *EXPR_P. */
11316 pre_last_gsi = gsi_last (*pre_p);
11317 post_last_gsi = gsi_last (*post_p);
11318
11319 saved_location = input_location;
11320 if (save_expr != error_mark_node
11321 && EXPR_HAS_LOCATION (*expr_p))
11322 input_location = EXPR_LOCATION (*expr_p);
11323
11324 /* Loop over the specific gimplifiers until the toplevel node
11325 remains the same. */
11326 do
11327 {
11328 /* Strip away as many useless type conversions as possible
11329 at the toplevel. */
11330 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11331
11332 /* Remember the expr. */
11333 save_expr = *expr_p;
11334
11335 /* Die, die, die, my darling. */
11336 if (error_operand_p (save_expr))
11337 {
11338 ret = GS_ERROR;
11339 break;
11340 }
11341
11342 /* Do any language-specific gimplification. */
11343 ret = ((enum gimplify_status)
11344 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11345 if (ret == GS_OK)
11346 {
11347 if (*expr_p == NULL_TREE)
11348 break;
11349 if (*expr_p != save_expr)
11350 continue;
11351 }
11352 else if (ret != GS_UNHANDLED)
11353 break;
11354
11355 /* Make sure that all the cases set 'ret' appropriately. */
11356 ret = GS_UNHANDLED;
11357 switch (TREE_CODE (*expr_p))
11358 {
11359 /* First deal with the special cases. */
11360
11361 case POSTINCREMENT_EXPR:
11362 case POSTDECREMENT_EXPR:
11363 case PREINCREMENT_EXPR:
11364 case PREDECREMENT_EXPR:
11365 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11366 fallback != fb_none,
11367 TREE_TYPE (*expr_p));
11368 break;
11369
11370 case VIEW_CONVERT_EXPR:
11371 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11372 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11373 {
11374 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11375 post_p, is_gimple_val, fb_rvalue);
11376 recalculate_side_effects (*expr_p);
11377 break;
11378 }
11379 /* Fallthru. */
11380
11381 case ARRAY_REF:
11382 case ARRAY_RANGE_REF:
11383 case REALPART_EXPR:
11384 case IMAGPART_EXPR:
11385 case COMPONENT_REF:
11386 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11387 fallback ? fallback : fb_rvalue);
11388 break;
11389
11390 case COND_EXPR:
11391 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11392
11393 /* C99 code may assign to an array in a structure value of a
11394 conditional expression, and this has undefined behavior
11395 only on execution, so create a temporary if an lvalue is
11396 required. */
11397 if (fallback == fb_lvalue)
11398 {
11399 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11400 mark_addressable (*expr_p);
11401 ret = GS_OK;
11402 }
11403 break;
11404
11405 case CALL_EXPR:
11406 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11407
11408 /* C99 code may assign to an array in a structure returned
11409 from a function, and this has undefined behavior only on
11410 execution, so create a temporary if an lvalue is
11411 required. */
11412 if (fallback == fb_lvalue)
11413 {
11414 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11415 mark_addressable (*expr_p);
11416 ret = GS_OK;
11417 }
11418 break;
11419
11420 case TREE_LIST:
11421 gcc_unreachable ();
11422
11423 case COMPOUND_EXPR:
11424 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11425 break;
11426
11427 case COMPOUND_LITERAL_EXPR:
11428 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11429 gimple_test_f, fallback);
11430 break;
11431
11432 case MODIFY_EXPR:
11433 case INIT_EXPR:
11434 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11435 fallback != fb_none);
11436 break;
11437
11438 case TRUTH_ANDIF_EXPR:
11439 case TRUTH_ORIF_EXPR:
11440 {
11441 /* Preserve the original type of the expression and the
11442 source location of the outer expression. */
11443 tree org_type = TREE_TYPE (*expr_p);
11444 *expr_p = gimple_boolify (*expr_p);
11445 *expr_p = build3_loc (input_location, COND_EXPR,
11446 org_type, *expr_p,
11447 fold_convert_loc
11448 (input_location,
11449 org_type, boolean_true_node),
11450 fold_convert_loc
11451 (input_location,
11452 org_type, boolean_false_node));
11453 ret = GS_OK;
11454 break;
11455 }
11456
11457 case TRUTH_NOT_EXPR:
11458 {
11459 tree type = TREE_TYPE (*expr_p);
11460 /* The parsers are careful to generate TRUTH_NOT_EXPR
11461 only with operands that are always zero or one.
11462 We do not fold here but handle the only interesting case
11463 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11464 *expr_p = gimple_boolify (*expr_p);
11465 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11466 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11467 TREE_TYPE (*expr_p),
11468 TREE_OPERAND (*expr_p, 0));
11469 else
11470 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11471 TREE_TYPE (*expr_p),
11472 TREE_OPERAND (*expr_p, 0),
11473 build_int_cst (TREE_TYPE (*expr_p), 1));
11474 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11475 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11476 ret = GS_OK;
11477 break;
11478 }
11479
11480 case ADDR_EXPR:
11481 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11482 break;
11483
11484 case ANNOTATE_EXPR:
11485 {
11486 tree cond = TREE_OPERAND (*expr_p, 0);
11487 tree kind = TREE_OPERAND (*expr_p, 1);
11488 tree data = TREE_OPERAND (*expr_p, 2);
11489 tree type = TREE_TYPE (cond);
11490 if (!INTEGRAL_TYPE_P (type))
11491 {
11492 *expr_p = cond;
11493 ret = GS_OK;
11494 break;
11495 }
11496 tree tmp = create_tmp_var (type);
11497 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11498 gcall *call
11499 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11500 gimple_call_set_lhs (call, tmp);
11501 gimplify_seq_add_stmt (pre_p, call);
11502 *expr_p = tmp;
11503 ret = GS_ALL_DONE;
11504 break;
11505 }
11506
11507 case VA_ARG_EXPR:
11508 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11509 break;
11510
11511 CASE_CONVERT:
11512 if (IS_EMPTY_STMT (*expr_p))
11513 {
11514 ret = GS_ALL_DONE;
11515 break;
11516 }
11517
11518 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11519 || fallback == fb_none)
11520 {
11521 /* Just strip a conversion to void (or in void context) and
11522 try again. */
11523 *expr_p = TREE_OPERAND (*expr_p, 0);
11524 ret = GS_OK;
11525 break;
11526 }
11527
11528 ret = gimplify_conversion (expr_p);
11529 if (ret == GS_ERROR)
11530 break;
11531 if (*expr_p != save_expr)
11532 break;
11533 /* FALLTHRU */
11534
11535 case FIX_TRUNC_EXPR:
11536 /* unary_expr: ... | '(' cast ')' val | ... */
11537 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11538 is_gimple_val, fb_rvalue);
11539 recalculate_side_effects (*expr_p);
11540 break;
11541
11542 case INDIRECT_REF:
11543 {
11544 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11545 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11546 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11547
11548 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11549 if (*expr_p != save_expr)
11550 {
11551 ret = GS_OK;
11552 break;
11553 }
11554
11555 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11556 is_gimple_reg, fb_rvalue);
11557 if (ret == GS_ERROR)
11558 break;
11559
11560 recalculate_side_effects (*expr_p);
11561 *expr_p = fold_build2_loc (input_location, MEM_REF,
11562 TREE_TYPE (*expr_p),
11563 TREE_OPERAND (*expr_p, 0),
11564 build_int_cst (saved_ptr_type, 0));
11565 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11566 TREE_THIS_NOTRAP (*expr_p) = notrap;
11567 ret = GS_OK;
11568 break;
11569 }
11570
11571 /* We arrive here through the various re-gimplifcation paths. */
11572 case MEM_REF:
11573 /* First try re-folding the whole thing. */
11574 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11575 TREE_OPERAND (*expr_p, 0),
11576 TREE_OPERAND (*expr_p, 1));
11577 if (tmp)
11578 {
11579 REF_REVERSE_STORAGE_ORDER (tmp)
11580 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11581 *expr_p = tmp;
11582 recalculate_side_effects (*expr_p);
11583 ret = GS_OK;
11584 break;
11585 }
11586 /* Avoid re-gimplifying the address operand if it is already
11587 in suitable form. Re-gimplifying would mark the address
11588 operand addressable. Always gimplify when not in SSA form
11589 as we still may have to gimplify decls with value-exprs. */
11590 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11591 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11592 {
11593 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11594 is_gimple_mem_ref_addr, fb_rvalue);
11595 if (ret == GS_ERROR)
11596 break;
11597 }
11598 recalculate_side_effects (*expr_p);
11599 ret = GS_ALL_DONE;
11600 break;
11601
11602 /* Constants need not be gimplified. */
11603 case INTEGER_CST:
11604 case REAL_CST:
11605 case FIXED_CST:
11606 case STRING_CST:
11607 case COMPLEX_CST:
11608 case VECTOR_CST:
11609 /* Drop the overflow flag on constants, we do not want
11610 that in the GIMPLE IL. */
11611 if (TREE_OVERFLOW_P (*expr_p))
11612 *expr_p = drop_tree_overflow (*expr_p);
11613 ret = GS_ALL_DONE;
11614 break;
11615
11616 case CONST_DECL:
11617 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11618 CONST_DECL node. Otherwise the decl is replaceable by its
11619 value. */
11620 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11621 if (fallback & fb_lvalue)
11622 ret = GS_ALL_DONE;
11623 else
11624 {
11625 *expr_p = DECL_INITIAL (*expr_p);
11626 ret = GS_OK;
11627 }
11628 break;
11629
11630 case DECL_EXPR:
11631 ret = gimplify_decl_expr (expr_p, pre_p);
11632 break;
11633
11634 case BIND_EXPR:
11635 ret = gimplify_bind_expr (expr_p, pre_p);
11636 break;
11637
11638 case LOOP_EXPR:
11639 ret = gimplify_loop_expr (expr_p, pre_p);
11640 break;
11641
11642 case SWITCH_EXPR:
11643 ret = gimplify_switch_expr (expr_p, pre_p);
11644 break;
11645
11646 case EXIT_EXPR:
11647 ret = gimplify_exit_expr (expr_p);
11648 break;
11649
11650 case GOTO_EXPR:
11651 /* If the target is not LABEL, then it is a computed jump
11652 and the target needs to be gimplified. */
11653 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11654 {
11655 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11656 NULL, is_gimple_val, fb_rvalue);
11657 if (ret == GS_ERROR)
11658 break;
11659 }
11660 gimplify_seq_add_stmt (pre_p,
11661 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11662 ret = GS_ALL_DONE;
11663 break;
11664
11665 case PREDICT_EXPR:
11666 gimplify_seq_add_stmt (pre_p,
11667 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11668 PREDICT_EXPR_OUTCOME (*expr_p)));
11669 ret = GS_ALL_DONE;
11670 break;
11671
11672 case LABEL_EXPR:
11673 ret = gimplify_label_expr (expr_p, pre_p);
11674 label = LABEL_EXPR_LABEL (*expr_p);
11675 gcc_assert (decl_function_context (label) == current_function_decl);
11676
11677 /* If the label is used in a goto statement, or address of the label
11678 is taken, we need to unpoison all variables that were seen so far.
11679 Doing so would prevent us from reporting a false positives. */
11680 if (asan_poisoned_variables
11681 && asan_used_labels != NULL
11682 && asan_used_labels->contains (label))
11683 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11684 break;
11685
11686 case CASE_LABEL_EXPR:
11687 ret = gimplify_case_label_expr (expr_p, pre_p);
11688
11689 if (gimplify_ctxp->live_switch_vars)
11690 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11691 pre_p);
11692 break;
11693
11694 case RETURN_EXPR:
11695 ret = gimplify_return_expr (*expr_p, pre_p);
11696 break;
11697
11698 case CONSTRUCTOR:
11699 /* Don't reduce this in place; let gimplify_init_constructor work its
11700 magic. Buf if we're just elaborating this for side effects, just
11701 gimplify any element that has side-effects. */
11702 if (fallback == fb_none)
11703 {
11704 unsigned HOST_WIDE_INT ix;
11705 tree val;
11706 tree temp = NULL_TREE;
11707 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11708 if (TREE_SIDE_EFFECTS (val))
11709 append_to_statement_list (val, &temp);
11710
11711 *expr_p = temp;
11712 ret = temp ? GS_OK : GS_ALL_DONE;
11713 }
11714 /* C99 code may assign to an array in a constructed
11715 structure or union, and this has undefined behavior only
11716 on execution, so create a temporary if an lvalue is
11717 required. */
11718 else if (fallback == fb_lvalue)
11719 {
11720 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11721 mark_addressable (*expr_p);
11722 ret = GS_OK;
11723 }
11724 else
11725 ret = GS_ALL_DONE;
11726 break;
11727
11728 /* The following are special cases that are not handled by the
11729 original GIMPLE grammar. */
11730
11731 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11732 eliminated. */
11733 case SAVE_EXPR:
11734 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11735 break;
11736
11737 case BIT_FIELD_REF:
11738 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11739 post_p, is_gimple_lvalue, fb_either);
11740 recalculate_side_effects (*expr_p);
11741 break;
11742
11743 case TARGET_MEM_REF:
11744 {
11745 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11746
11747 if (TMR_BASE (*expr_p))
11748 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11749 post_p, is_gimple_mem_ref_addr, fb_either);
11750 if (TMR_INDEX (*expr_p))
11751 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11752 post_p, is_gimple_val, fb_rvalue);
11753 if (TMR_INDEX2 (*expr_p))
11754 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11755 post_p, is_gimple_val, fb_rvalue);
11756 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11757 ret = MIN (r0, r1);
11758 }
11759 break;
11760
11761 case NON_LVALUE_EXPR:
11762 /* This should have been stripped above. */
11763 gcc_unreachable ();
11764
11765 case ASM_EXPR:
11766 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11767 break;
11768
11769 case TRY_FINALLY_EXPR:
11770 case TRY_CATCH_EXPR:
11771 {
11772 gimple_seq eval, cleanup;
11773 gtry *try_;
11774
11775 /* Calls to destructors are generated automatically in FINALLY/CATCH
11776 block. They should have location as UNKNOWN_LOCATION. However,
11777 gimplify_call_expr will reset these call stmts to input_location
11778 if it finds stmt's location is unknown. To prevent resetting for
11779 destructors, we set the input_location to unknown.
11780 Note that this only affects the destructor calls in FINALLY/CATCH
11781 block, and will automatically reset to its original value by the
11782 end of gimplify_expr. */
11783 input_location = UNKNOWN_LOCATION;
11784 eval = cleanup = NULL;
11785 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11786 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11787 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11788 if (gimple_seq_empty_p (cleanup))
11789 {
11790 gimple_seq_add_seq (pre_p, eval);
11791 ret = GS_ALL_DONE;
11792 break;
11793 }
11794 try_ = gimple_build_try (eval, cleanup,
11795 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11796 ? GIMPLE_TRY_FINALLY
11797 : GIMPLE_TRY_CATCH);
11798 if (EXPR_HAS_LOCATION (save_expr))
11799 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11800 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11801 gimple_set_location (try_, saved_location);
11802 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11803 gimple_try_set_catch_is_cleanup (try_,
11804 TRY_CATCH_IS_CLEANUP (*expr_p));
11805 gimplify_seq_add_stmt (pre_p, try_);
11806 ret = GS_ALL_DONE;
11807 break;
11808 }
11809
11810 case CLEANUP_POINT_EXPR:
11811 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11812 break;
11813
11814 case TARGET_EXPR:
11815 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11816 break;
11817
11818 case CATCH_EXPR:
11819 {
11820 gimple *c;
11821 gimple_seq handler = NULL;
11822 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11823 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11824 gimplify_seq_add_stmt (pre_p, c);
11825 ret = GS_ALL_DONE;
11826 break;
11827 }
11828
11829 case EH_FILTER_EXPR:
11830 {
11831 gimple *ehf;
11832 gimple_seq failure = NULL;
11833
11834 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11835 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11836 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11837 gimplify_seq_add_stmt (pre_p, ehf);
11838 ret = GS_ALL_DONE;
11839 break;
11840 }
11841
11842 case OBJ_TYPE_REF:
11843 {
11844 enum gimplify_status r0, r1;
11845 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11846 post_p, is_gimple_val, fb_rvalue);
11847 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11848 post_p, is_gimple_val, fb_rvalue);
11849 TREE_SIDE_EFFECTS (*expr_p) = 0;
11850 ret = MIN (r0, r1);
11851 }
11852 break;
11853
11854 case LABEL_DECL:
11855 /* We get here when taking the address of a label. We mark
11856 the label as "forced"; meaning it can never be removed and
11857 it is a potential target for any computed goto. */
11858 FORCED_LABEL (*expr_p) = 1;
11859 ret = GS_ALL_DONE;
11860 break;
11861
11862 case STATEMENT_LIST:
11863 ret = gimplify_statement_list (expr_p, pre_p);
11864 break;
11865
11866 case WITH_SIZE_EXPR:
11867 {
11868 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11869 post_p == &internal_post ? NULL : post_p,
11870 gimple_test_f, fallback);
11871 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11872 is_gimple_val, fb_rvalue);
11873 ret = GS_ALL_DONE;
11874 }
11875 break;
11876
11877 case VAR_DECL:
11878 case PARM_DECL:
11879 ret = gimplify_var_or_parm_decl (expr_p);
11880 break;
11881
11882 case RESULT_DECL:
11883 /* When within an OMP context, notice uses of variables. */
11884 if (gimplify_omp_ctxp)
11885 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11886 ret = GS_ALL_DONE;
11887 break;
11888
11889 case DEBUG_EXPR_DECL:
11890 gcc_unreachable ();
11891
11892 case DEBUG_BEGIN_STMT:
11893 gimplify_seq_add_stmt (pre_p,
11894 gimple_build_debug_begin_stmt
11895 (TREE_BLOCK (*expr_p),
11896 EXPR_LOCATION (*expr_p)));
11897 ret = GS_ALL_DONE;
11898 *expr_p = NULL;
11899 break;
11900
11901 case SSA_NAME:
11902 /* Allow callbacks into the gimplifier during optimization. */
11903 ret = GS_ALL_DONE;
11904 break;
11905
11906 case OMP_PARALLEL:
11907 gimplify_omp_parallel (expr_p, pre_p);
11908 ret = GS_ALL_DONE;
11909 break;
11910
11911 case OMP_TASK:
11912 gimplify_omp_task (expr_p, pre_p);
11913 ret = GS_ALL_DONE;
11914 break;
11915
11916 case OMP_FOR:
11917 case OMP_SIMD:
11918 case OMP_DISTRIBUTE:
11919 case OMP_TASKLOOP:
11920 case OACC_LOOP:
11921 ret = gimplify_omp_for (expr_p, pre_p);
11922 break;
11923
11924 case OACC_CACHE:
11925 gimplify_oacc_cache (expr_p, pre_p);
11926 ret = GS_ALL_DONE;
11927 break;
11928
11929 case OACC_DECLARE:
11930 gimplify_oacc_declare (expr_p, pre_p);
11931 ret = GS_ALL_DONE;
11932 break;
11933
11934 case OACC_HOST_DATA:
11935 case OACC_DATA:
11936 case OACC_KERNELS:
11937 case OACC_PARALLEL:
11938 case OMP_SECTIONS:
11939 case OMP_SINGLE:
11940 case OMP_TARGET:
11941 case OMP_TARGET_DATA:
11942 case OMP_TEAMS:
11943 gimplify_omp_workshare (expr_p, pre_p);
11944 ret = GS_ALL_DONE;
11945 break;
11946
11947 case OACC_ENTER_DATA:
11948 case OACC_EXIT_DATA:
11949 case OACC_UPDATE:
11950 case OMP_TARGET_UPDATE:
11951 case OMP_TARGET_ENTER_DATA:
11952 case OMP_TARGET_EXIT_DATA:
11953 gimplify_omp_target_update (expr_p, pre_p);
11954 ret = GS_ALL_DONE;
11955 break;
11956
11957 case OMP_SECTION:
11958 case OMP_MASTER:
11959 case OMP_TASKGROUP:
11960 case OMP_ORDERED:
11961 case OMP_CRITICAL:
11962 {
11963 gimple_seq body = NULL;
11964 gimple *g;
11965
11966 gimplify_and_add (OMP_BODY (*expr_p), &body);
11967 switch (TREE_CODE (*expr_p))
11968 {
11969 case OMP_SECTION:
11970 g = gimple_build_omp_section (body);
11971 break;
11972 case OMP_MASTER:
11973 g = gimple_build_omp_master (body);
11974 break;
11975 case OMP_TASKGROUP:
11976 {
11977 gimple_seq cleanup = NULL;
11978 tree fn
11979 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11980 g = gimple_build_call (fn, 0);
11981 gimple_seq_add_stmt (&cleanup, g);
11982 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11983 body = NULL;
11984 gimple_seq_add_stmt (&body, g);
11985 g = gimple_build_omp_taskgroup (body);
11986 }
11987 break;
11988 case OMP_ORDERED:
11989 g = gimplify_omp_ordered (*expr_p, body);
11990 break;
11991 case OMP_CRITICAL:
11992 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11993 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11994 gimplify_adjust_omp_clauses (pre_p, body,
11995 &OMP_CRITICAL_CLAUSES (*expr_p),
11996 OMP_CRITICAL);
11997 g = gimple_build_omp_critical (body,
11998 OMP_CRITICAL_NAME (*expr_p),
11999 OMP_CRITICAL_CLAUSES (*expr_p));
12000 break;
12001 default:
12002 gcc_unreachable ();
12003 }
12004 gimplify_seq_add_stmt (pre_p, g);
12005 ret = GS_ALL_DONE;
12006 break;
12007 }
12008
12009 case OMP_ATOMIC:
12010 case OMP_ATOMIC_READ:
12011 case OMP_ATOMIC_CAPTURE_OLD:
12012 case OMP_ATOMIC_CAPTURE_NEW:
12013 ret = gimplify_omp_atomic (expr_p, pre_p);
12014 break;
12015
12016 case TRANSACTION_EXPR:
12017 ret = gimplify_transaction (expr_p, pre_p);
12018 break;
12019
12020 case TRUTH_AND_EXPR:
12021 case TRUTH_OR_EXPR:
12022 case TRUTH_XOR_EXPR:
12023 {
12024 tree orig_type = TREE_TYPE (*expr_p);
12025 tree new_type, xop0, xop1;
12026 *expr_p = gimple_boolify (*expr_p);
12027 new_type = TREE_TYPE (*expr_p);
12028 if (!useless_type_conversion_p (orig_type, new_type))
12029 {
12030 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12031 ret = GS_OK;
12032 break;
12033 }
12034
12035 /* Boolified binary truth expressions are semantically equivalent
12036 to bitwise binary expressions. Canonicalize them to the
12037 bitwise variant. */
12038 switch (TREE_CODE (*expr_p))
12039 {
12040 case TRUTH_AND_EXPR:
12041 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12042 break;
12043 case TRUTH_OR_EXPR:
12044 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12045 break;
12046 case TRUTH_XOR_EXPR:
12047 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12048 break;
12049 default:
12050 break;
12051 }
12052 /* Now make sure that operands have compatible type to
12053 expression's new_type. */
12054 xop0 = TREE_OPERAND (*expr_p, 0);
12055 xop1 = TREE_OPERAND (*expr_p, 1);
12056 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12057 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12058 new_type,
12059 xop0);
12060 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12061 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12062 new_type,
12063 xop1);
12064 /* Continue classified as tcc_binary. */
12065 goto expr_2;
12066 }
12067
12068 case VEC_COND_EXPR:
12069 {
12070 enum gimplify_status r0, r1, r2;
12071
12072 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12073 post_p, is_gimple_condexpr, fb_rvalue);
12074 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12075 post_p, is_gimple_val, fb_rvalue);
12076 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12077 post_p, is_gimple_val, fb_rvalue);
12078
12079 ret = MIN (MIN (r0, r1), r2);
12080 recalculate_side_effects (*expr_p);
12081 }
12082 break;
12083
12084 case VEC_PERM_EXPR:
12085 /* Classified as tcc_expression. */
12086 goto expr_3;
12087
12088 case BIT_INSERT_EXPR:
12089 /* Argument 3 is a constant. */
12090 goto expr_2;
12091
12092 case POINTER_PLUS_EXPR:
12093 {
12094 enum gimplify_status r0, r1;
12095 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12096 post_p, is_gimple_val, fb_rvalue);
12097 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12098 post_p, is_gimple_val, fb_rvalue);
12099 recalculate_side_effects (*expr_p);
12100 ret = MIN (r0, r1);
12101 break;
12102 }
12103
12104 default:
12105 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12106 {
12107 case tcc_comparison:
12108 /* Handle comparison of objects of non scalar mode aggregates
12109 with a call to memcmp. It would be nice to only have to do
12110 this for variable-sized objects, but then we'd have to allow
12111 the same nest of reference nodes we allow for MODIFY_EXPR and
12112 that's too complex.
12113
12114 Compare scalar mode aggregates as scalar mode values. Using
12115 memcmp for them would be very inefficient at best, and is
12116 plain wrong if bitfields are involved. */
12117 {
12118 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12119
12120 /* Vector comparisons need no boolification. */
12121 if (TREE_CODE (type) == VECTOR_TYPE)
12122 goto expr_2;
12123 else if (!AGGREGATE_TYPE_P (type))
12124 {
12125 tree org_type = TREE_TYPE (*expr_p);
12126 *expr_p = gimple_boolify (*expr_p);
12127 if (!useless_type_conversion_p (org_type,
12128 TREE_TYPE (*expr_p)))
12129 {
12130 *expr_p = fold_convert_loc (input_location,
12131 org_type, *expr_p);
12132 ret = GS_OK;
12133 }
12134 else
12135 goto expr_2;
12136 }
12137 else if (TYPE_MODE (type) != BLKmode)
12138 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12139 else
12140 ret = gimplify_variable_sized_compare (expr_p);
12141
12142 break;
12143 }
12144
12145 /* If *EXPR_P does not need to be special-cased, handle it
12146 according to its class. */
12147 case tcc_unary:
12148 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12149 post_p, is_gimple_val, fb_rvalue);
12150 break;
12151
12152 case tcc_binary:
12153 expr_2:
12154 {
12155 enum gimplify_status r0, r1;
12156
12157 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12158 post_p, is_gimple_val, fb_rvalue);
12159 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12160 post_p, is_gimple_val, fb_rvalue);
12161
12162 ret = MIN (r0, r1);
12163 break;
12164 }
12165
12166 expr_3:
12167 {
12168 enum gimplify_status r0, r1, r2;
12169
12170 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12171 post_p, is_gimple_val, fb_rvalue);
12172 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12173 post_p, is_gimple_val, fb_rvalue);
12174 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12175 post_p, is_gimple_val, fb_rvalue);
12176
12177 ret = MIN (MIN (r0, r1), r2);
12178 break;
12179 }
12180
12181 case tcc_declaration:
12182 case tcc_constant:
12183 ret = GS_ALL_DONE;
12184 goto dont_recalculate;
12185
12186 default:
12187 gcc_unreachable ();
12188 }
12189
12190 recalculate_side_effects (*expr_p);
12191
12192 dont_recalculate:
12193 break;
12194 }
12195
12196 gcc_assert (*expr_p || ret != GS_OK);
12197 }
12198 while (ret == GS_OK);
12199
12200 /* If we encountered an error_mark somewhere nested inside, either
12201 stub out the statement or propagate the error back out. */
12202 if (ret == GS_ERROR)
12203 {
12204 if (is_statement)
12205 *expr_p = NULL;
12206 goto out;
12207 }
12208
12209 /* This was only valid as a return value from the langhook, which
12210 we handled. Make sure it doesn't escape from any other context. */
12211 gcc_assert (ret != GS_UNHANDLED);
12212
12213 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12214 {
12215 /* We aren't looking for a value, and we don't have a valid
12216 statement. If it doesn't have side-effects, throw it away.
12217 We can also get here with code such as "*&&L;", where L is
12218 a LABEL_DECL that is marked as FORCED_LABEL. */
12219 if (TREE_CODE (*expr_p) == LABEL_DECL
12220 || !TREE_SIDE_EFFECTS (*expr_p))
12221 *expr_p = NULL;
12222 else if (!TREE_THIS_VOLATILE (*expr_p))
12223 {
12224 /* This is probably a _REF that contains something nested that
12225 has side effects. Recurse through the operands to find it. */
12226 enum tree_code code = TREE_CODE (*expr_p);
12227
12228 switch (code)
12229 {
12230 case COMPONENT_REF:
12231 case REALPART_EXPR:
12232 case IMAGPART_EXPR:
12233 case VIEW_CONVERT_EXPR:
12234 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12235 gimple_test_f, fallback);
12236 break;
12237
12238 case ARRAY_REF:
12239 case ARRAY_RANGE_REF:
12240 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12241 gimple_test_f, fallback);
12242 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12243 gimple_test_f, fallback);
12244 break;
12245
12246 default:
12247 /* Anything else with side-effects must be converted to
12248 a valid statement before we get here. */
12249 gcc_unreachable ();
12250 }
12251
12252 *expr_p = NULL;
12253 }
12254 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12255 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12256 {
12257 /* Historically, the compiler has treated a bare reference
12258 to a non-BLKmode volatile lvalue as forcing a load. */
12259 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12260
12261 /* Normally, we do not want to create a temporary for a
12262 TREE_ADDRESSABLE type because such a type should not be
12263 copied by bitwise-assignment. However, we make an
12264 exception here, as all we are doing here is ensuring that
12265 we read the bytes that make up the type. We use
12266 create_tmp_var_raw because create_tmp_var will abort when
12267 given a TREE_ADDRESSABLE type. */
12268 tree tmp = create_tmp_var_raw (type, "vol");
12269 gimple_add_tmp_var (tmp);
12270 gimplify_assign (tmp, *expr_p, pre_p);
12271 *expr_p = NULL;
12272 }
12273 else
12274 /* We can't do anything useful with a volatile reference to
12275 an incomplete type, so just throw it away. Likewise for
12276 a BLKmode type, since any implicit inner load should
12277 already have been turned into an explicit one by the
12278 gimplification process. */
12279 *expr_p = NULL;
12280 }
12281
12282 /* If we are gimplifying at the statement level, we're done. Tack
12283 everything together and return. */
12284 if (fallback == fb_none || is_statement)
12285 {
12286 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12287 it out for GC to reclaim it. */
12288 *expr_p = NULL_TREE;
12289
12290 if (!gimple_seq_empty_p (internal_pre)
12291 || !gimple_seq_empty_p (internal_post))
12292 {
12293 gimplify_seq_add_seq (&internal_pre, internal_post);
12294 gimplify_seq_add_seq (pre_p, internal_pre);
12295 }
12296
12297 /* The result of gimplifying *EXPR_P is going to be the last few
12298 statements in *PRE_P and *POST_P. Add location information
12299 to all the statements that were added by the gimplification
12300 helpers. */
12301 if (!gimple_seq_empty_p (*pre_p))
12302 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12303
12304 if (!gimple_seq_empty_p (*post_p))
12305 annotate_all_with_location_after (*post_p, post_last_gsi,
12306 input_location);
12307
12308 goto out;
12309 }
12310
12311 #ifdef ENABLE_GIMPLE_CHECKING
12312 if (*expr_p)
12313 {
12314 enum tree_code code = TREE_CODE (*expr_p);
12315 /* These expressions should already be in gimple IR form. */
12316 gcc_assert (code != MODIFY_EXPR
12317 && code != ASM_EXPR
12318 && code != BIND_EXPR
12319 && code != CATCH_EXPR
12320 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12321 && code != EH_FILTER_EXPR
12322 && code != GOTO_EXPR
12323 && code != LABEL_EXPR
12324 && code != LOOP_EXPR
12325 && code != SWITCH_EXPR
12326 && code != TRY_FINALLY_EXPR
12327 && code != OACC_PARALLEL
12328 && code != OACC_KERNELS
12329 && code != OACC_DATA
12330 && code != OACC_HOST_DATA
12331 && code != OACC_DECLARE
12332 && code != OACC_UPDATE
12333 && code != OACC_ENTER_DATA
12334 && code != OACC_EXIT_DATA
12335 && code != OACC_CACHE
12336 && code != OMP_CRITICAL
12337 && code != OMP_FOR
12338 && code != OACC_LOOP
12339 && code != OMP_MASTER
12340 && code != OMP_TASKGROUP
12341 && code != OMP_ORDERED
12342 && code != OMP_PARALLEL
12343 && code != OMP_SECTIONS
12344 && code != OMP_SECTION
12345 && code != OMP_SINGLE);
12346 }
12347 #endif
12348
12349 /* Otherwise we're gimplifying a subexpression, so the resulting
12350 value is interesting. If it's a valid operand that matches
12351 GIMPLE_TEST_F, we're done. Unless we are handling some
12352 post-effects internally; if that's the case, we need to copy into
12353 a temporary before adding the post-effects to POST_P. */
12354 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12355 goto out;
12356
12357 /* Otherwise, we need to create a new temporary for the gimplified
12358 expression. */
12359
12360 /* We can't return an lvalue if we have an internal postqueue. The
12361 object the lvalue refers to would (probably) be modified by the
12362 postqueue; we need to copy the value out first, which means an
12363 rvalue. */
12364 if ((fallback & fb_lvalue)
12365 && gimple_seq_empty_p (internal_post)
12366 && is_gimple_addressable (*expr_p))
12367 {
12368 /* An lvalue will do. Take the address of the expression, store it
12369 in a temporary, and replace the expression with an INDIRECT_REF of
12370 that temporary. */
12371 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12372 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12373 *expr_p = build_simple_mem_ref (tmp);
12374 }
12375 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12376 {
12377 /* An rvalue will do. Assign the gimplified expression into a
12378 new temporary TMP and replace the original expression with
12379 TMP. First, make sure that the expression has a type so that
12380 it can be assigned into a temporary. */
12381 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12382 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12383 }
12384 else
12385 {
12386 #ifdef ENABLE_GIMPLE_CHECKING
12387 if (!(fallback & fb_mayfail))
12388 {
12389 fprintf (stderr, "gimplification failed:\n");
12390 print_generic_expr (stderr, *expr_p);
12391 debug_tree (*expr_p);
12392 internal_error ("gimplification failed");
12393 }
12394 #endif
12395 gcc_assert (fallback & fb_mayfail);
12396
12397 /* If this is an asm statement, and the user asked for the
12398 impossible, don't die. Fail and let gimplify_asm_expr
12399 issue an error. */
12400 ret = GS_ERROR;
12401 goto out;
12402 }
12403
12404 /* Make sure the temporary matches our predicate. */
12405 gcc_assert ((*gimple_test_f) (*expr_p));
12406
12407 if (!gimple_seq_empty_p (internal_post))
12408 {
12409 annotate_all_with_location (internal_post, input_location);
12410 gimplify_seq_add_seq (pre_p, internal_post);
12411 }
12412
12413 out:
12414 input_location = saved_location;
12415 return ret;
12416 }
12417
12418 /* Like gimplify_expr but make sure the gimplified result is not itself
12419 a SSA name (but a decl if it were). Temporaries required by
12420 evaluating *EXPR_P may be still SSA names. */
12421
12422 static enum gimplify_status
12423 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12424 bool (*gimple_test_f) (tree), fallback_t fallback,
12425 bool allow_ssa)
12426 {
12427 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12428 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12429 gimple_test_f, fallback);
12430 if (! allow_ssa
12431 && TREE_CODE (*expr_p) == SSA_NAME)
12432 {
12433 tree name = *expr_p;
12434 if (was_ssa_name_p)
12435 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12436 else
12437 {
12438 /* Avoid the extra copy if possible. */
12439 *expr_p = create_tmp_reg (TREE_TYPE (name));
12440 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12441 release_ssa_name (name);
12442 }
12443 }
12444 return ret;
12445 }
12446
12447 /* Look through TYPE for variable-sized objects and gimplify each such
12448 size that we find. Add to LIST_P any statements generated. */
12449
12450 void
12451 gimplify_type_sizes (tree type, gimple_seq *list_p)
12452 {
12453 tree field, t;
12454
12455 if (type == NULL || type == error_mark_node)
12456 return;
12457
12458 /* We first do the main variant, then copy into any other variants. */
12459 type = TYPE_MAIN_VARIANT (type);
12460
12461 /* Avoid infinite recursion. */
12462 if (TYPE_SIZES_GIMPLIFIED (type))
12463 return;
12464
12465 TYPE_SIZES_GIMPLIFIED (type) = 1;
12466
12467 switch (TREE_CODE (type))
12468 {
12469 case INTEGER_TYPE:
12470 case ENUMERAL_TYPE:
12471 case BOOLEAN_TYPE:
12472 case REAL_TYPE:
12473 case FIXED_POINT_TYPE:
12474 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12475 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12476
12477 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12478 {
12479 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12480 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12481 }
12482 break;
12483
12484 case ARRAY_TYPE:
12485 /* These types may not have declarations, so handle them here. */
12486 gimplify_type_sizes (TREE_TYPE (type), list_p);
12487 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12488 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12489 with assigned stack slots, for -O1+ -g they should be tracked
12490 by VTA. */
12491 if (!(TYPE_NAME (type)
12492 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12493 && DECL_IGNORED_P (TYPE_NAME (type)))
12494 && TYPE_DOMAIN (type)
12495 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12496 {
12497 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12498 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12499 DECL_IGNORED_P (t) = 0;
12500 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12501 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12502 DECL_IGNORED_P (t) = 0;
12503 }
12504 break;
12505
12506 case RECORD_TYPE:
12507 case UNION_TYPE:
12508 case QUAL_UNION_TYPE:
12509 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12510 if (TREE_CODE (field) == FIELD_DECL)
12511 {
12512 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12513 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12514 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12515 gimplify_type_sizes (TREE_TYPE (field), list_p);
12516 }
12517 break;
12518
12519 case POINTER_TYPE:
12520 case REFERENCE_TYPE:
12521 /* We used to recurse on the pointed-to type here, which turned out to
12522 be incorrect because its definition might refer to variables not
12523 yet initialized at this point if a forward declaration is involved.
12524
12525 It was actually useful for anonymous pointed-to types to ensure
12526 that the sizes evaluation dominates every possible later use of the
12527 values. Restricting to such types here would be safe since there
12528 is no possible forward declaration around, but would introduce an
12529 undesirable middle-end semantic to anonymity. We then defer to
12530 front-ends the responsibility of ensuring that the sizes are
12531 evaluated both early and late enough, e.g. by attaching artificial
12532 type declarations to the tree. */
12533 break;
12534
12535 default:
12536 break;
12537 }
12538
12539 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12540 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12541
12542 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12543 {
12544 TYPE_SIZE (t) = TYPE_SIZE (type);
12545 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12546 TYPE_SIZES_GIMPLIFIED (t) = 1;
12547 }
12548 }
12549
12550 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12551 a size or position, has had all of its SAVE_EXPRs evaluated.
12552 We add any required statements to *STMT_P. */
12553
12554 void
12555 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12556 {
12557 tree expr = *expr_p;
12558
12559 /* We don't do anything if the value isn't there, is constant, or contains
12560 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12561 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12562 will want to replace it with a new variable, but that will cause problems
12563 if this type is from outside the function. It's OK to have that here. */
12564 if (expr == NULL_TREE
12565 || is_gimple_constant (expr)
12566 || TREE_CODE (expr) == VAR_DECL
12567 || CONTAINS_PLACEHOLDER_P (expr))
12568 return;
12569
12570 *expr_p = unshare_expr (expr);
12571
12572 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12573 if the def vanishes. */
12574 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12575
12576 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12577 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12578 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12579 if (is_gimple_constant (*expr_p))
12580 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12581 }
12582
12583 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12584 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12585 is true, also gimplify the parameters. */
12586
12587 gbind *
12588 gimplify_body (tree fndecl, bool do_parms)
12589 {
12590 location_t saved_location = input_location;
12591 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12592 gimple *outer_stmt;
12593 gbind *outer_bind;
12594 struct cgraph_node *cgn;
12595
12596 timevar_push (TV_TREE_GIMPLIFY);
12597
12598 init_tree_ssa (cfun);
12599
12600 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12601 gimplification. */
12602 default_rtl_profile ();
12603
12604 gcc_assert (gimplify_ctxp == NULL);
12605 push_gimplify_context (true);
12606
12607 if (flag_openacc || flag_openmp)
12608 {
12609 gcc_assert (gimplify_omp_ctxp == NULL);
12610 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12611 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12612 }
12613
12614 /* Unshare most shared trees in the body and in that of any nested functions.
12615 It would seem we don't have to do this for nested functions because
12616 they are supposed to be output and then the outer function gimplified
12617 first, but the g++ front end doesn't always do it that way. */
12618 unshare_body (fndecl);
12619 unvisit_body (fndecl);
12620
12621 cgn = cgraph_node::get (fndecl);
12622 if (cgn && cgn->origin)
12623 nonlocal_vlas = new hash_set<tree>;
12624
12625 /* Make sure input_location isn't set to something weird. */
12626 input_location = DECL_SOURCE_LOCATION (fndecl);
12627
12628 /* Resolve callee-copies. This has to be done before processing
12629 the body so that DECL_VALUE_EXPR gets processed correctly. */
12630 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12631
12632 /* Gimplify the function's body. */
12633 seq = NULL;
12634 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12635 outer_stmt = gimple_seq_first_stmt (seq);
12636 if (!outer_stmt)
12637 {
12638 outer_stmt = gimple_build_nop ();
12639 gimplify_seq_add_stmt (&seq, outer_stmt);
12640 }
12641
12642 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12643 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12644 if (gimple_code (outer_stmt) == GIMPLE_BIND
12645 && gimple_seq_first (seq) == gimple_seq_last (seq))
12646 outer_bind = as_a <gbind *> (outer_stmt);
12647 else
12648 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12649
12650 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12651
12652 /* If we had callee-copies statements, insert them at the beginning
12653 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12654 if (!gimple_seq_empty_p (parm_stmts))
12655 {
12656 tree parm;
12657
12658 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12659 if (parm_cleanup)
12660 {
12661 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12662 GIMPLE_TRY_FINALLY);
12663 parm_stmts = NULL;
12664 gimple_seq_add_stmt (&parm_stmts, g);
12665 }
12666 gimple_bind_set_body (outer_bind, parm_stmts);
12667
12668 for (parm = DECL_ARGUMENTS (current_function_decl);
12669 parm; parm = DECL_CHAIN (parm))
12670 if (DECL_HAS_VALUE_EXPR_P (parm))
12671 {
12672 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12673 DECL_IGNORED_P (parm) = 0;
12674 }
12675 }
12676
12677 if (nonlocal_vlas)
12678 {
12679 if (nonlocal_vla_vars)
12680 {
12681 /* tree-nested.c may later on call declare_vars (..., true);
12682 which relies on BLOCK_VARS chain to be the tail of the
12683 gimple_bind_vars chain. Ensure we don't violate that
12684 assumption. */
12685 if (gimple_bind_block (outer_bind)
12686 == DECL_INITIAL (current_function_decl))
12687 declare_vars (nonlocal_vla_vars, outer_bind, true);
12688 else
12689 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12690 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12691 nonlocal_vla_vars);
12692 nonlocal_vla_vars = NULL_TREE;
12693 }
12694 delete nonlocal_vlas;
12695 nonlocal_vlas = NULL;
12696 }
12697
12698 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12699 && gimplify_omp_ctxp)
12700 {
12701 delete_omp_context (gimplify_omp_ctxp);
12702 gimplify_omp_ctxp = NULL;
12703 }
12704
12705 pop_gimplify_context (outer_bind);
12706 gcc_assert (gimplify_ctxp == NULL);
12707
12708 if (flag_checking && !seen_error ())
12709 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12710
12711 timevar_pop (TV_TREE_GIMPLIFY);
12712 input_location = saved_location;
12713
12714 return outer_bind;
12715 }
12716
12717 typedef char *char_p; /* For DEF_VEC_P. */
12718
12719 /* Return whether we should exclude FNDECL from instrumentation. */
12720
12721 static bool
12722 flag_instrument_functions_exclude_p (tree fndecl)
12723 {
12724 vec<char_p> *v;
12725
12726 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12727 if (v && v->length () > 0)
12728 {
12729 const char *name;
12730 int i;
12731 char *s;
12732
12733 name = lang_hooks.decl_printable_name (fndecl, 0);
12734 FOR_EACH_VEC_ELT (*v, i, s)
12735 if (strstr (name, s) != NULL)
12736 return true;
12737 }
12738
12739 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12740 if (v && v->length () > 0)
12741 {
12742 const char *name;
12743 int i;
12744 char *s;
12745
12746 name = DECL_SOURCE_FILE (fndecl);
12747 FOR_EACH_VEC_ELT (*v, i, s)
12748 if (strstr (name, s) != NULL)
12749 return true;
12750 }
12751
12752 return false;
12753 }
12754
12755 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12756 node for the function we want to gimplify.
12757
12758 Return the sequence of GIMPLE statements corresponding to the body
12759 of FNDECL. */
12760
12761 void
12762 gimplify_function_tree (tree fndecl)
12763 {
12764 tree parm, ret;
12765 gimple_seq seq;
12766 gbind *bind;
12767
12768 gcc_assert (!gimple_body (fndecl));
12769
12770 if (DECL_STRUCT_FUNCTION (fndecl))
12771 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12772 else
12773 push_struct_function (fndecl);
12774
12775 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12776 if necessary. */
12777 cfun->curr_properties |= PROP_gimple_lva;
12778
12779 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12780 {
12781 /* Preliminarily mark non-addressed complex variables as eligible
12782 for promotion to gimple registers. We'll transform their uses
12783 as we find them. */
12784 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12785 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12786 && !TREE_THIS_VOLATILE (parm)
12787 && !needs_to_live_in_memory (parm))
12788 DECL_GIMPLE_REG_P (parm) = 1;
12789 }
12790
12791 ret = DECL_RESULT (fndecl);
12792 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12793 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12794 && !needs_to_live_in_memory (ret))
12795 DECL_GIMPLE_REG_P (ret) = 1;
12796
12797 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12798 asan_poisoned_variables = new hash_set<tree> ();
12799 bind = gimplify_body (fndecl, true);
12800 if (asan_poisoned_variables)
12801 {
12802 delete asan_poisoned_variables;
12803 asan_poisoned_variables = NULL;
12804 }
12805
12806 /* The tree body of the function is no longer needed, replace it
12807 with the new GIMPLE body. */
12808 seq = NULL;
12809 gimple_seq_add_stmt (&seq, bind);
12810 gimple_set_body (fndecl, seq);
12811
12812 /* If we're instrumenting function entry/exit, then prepend the call to
12813 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12814 catch the exit hook. */
12815 /* ??? Add some way to ignore exceptions for this TFE. */
12816 if (flag_instrument_function_entry_exit
12817 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12818 /* Do not instrument extern inline functions. */
12819 && !(DECL_DECLARED_INLINE_P (fndecl)
12820 && DECL_EXTERNAL (fndecl)
12821 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12822 && !flag_instrument_functions_exclude_p (fndecl))
12823 {
12824 tree x;
12825 gbind *new_bind;
12826 gimple *tf;
12827 gimple_seq cleanup = NULL, body = NULL;
12828 tree tmp_var;
12829 gcall *call;
12830
12831 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12832 call = gimple_build_call (x, 1, integer_zero_node);
12833 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12834 gimple_call_set_lhs (call, tmp_var);
12835 gimplify_seq_add_stmt (&cleanup, call);
12836 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12837 call = gimple_build_call (x, 2,
12838 build_fold_addr_expr (current_function_decl),
12839 tmp_var);
12840 gimplify_seq_add_stmt (&cleanup, call);
12841 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12842
12843 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12844 call = gimple_build_call (x, 1, integer_zero_node);
12845 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12846 gimple_call_set_lhs (call, tmp_var);
12847 gimplify_seq_add_stmt (&body, call);
12848 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12849 call = gimple_build_call (x, 2,
12850 build_fold_addr_expr (current_function_decl),
12851 tmp_var);
12852 gimplify_seq_add_stmt (&body, call);
12853 gimplify_seq_add_stmt (&body, tf);
12854 new_bind = gimple_build_bind (NULL, body, NULL);
12855
12856 /* Replace the current function body with the body
12857 wrapped in the try/finally TF. */
12858 seq = NULL;
12859 gimple_seq_add_stmt (&seq, new_bind);
12860 gimple_set_body (fndecl, seq);
12861 bind = new_bind;
12862 }
12863
12864 if (sanitize_flags_p (SANITIZE_THREAD))
12865 {
12866 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12867 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12868 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12869 /* Replace the current function body with the body
12870 wrapped in the try/finally TF. */
12871 seq = NULL;
12872 gimple_seq_add_stmt (&seq, new_bind);
12873 gimple_set_body (fndecl, seq);
12874 }
12875
12876 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12877 cfun->curr_properties |= PROP_gimple_any;
12878
12879 pop_cfun ();
12880
12881 dump_function (TDI_gimple, fndecl);
12882 }
12883
12884 /* Return a dummy expression of type TYPE in order to keep going after an
12885 error. */
12886
12887 static tree
12888 dummy_object (tree type)
12889 {
12890 tree t = build_int_cst (build_pointer_type (type), 0);
12891 return build2 (MEM_REF, type, t, t);
12892 }
12893
12894 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12895 builtin function, but a very special sort of operator. */
12896
12897 enum gimplify_status
12898 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12899 gimple_seq *post_p ATTRIBUTE_UNUSED)
12900 {
12901 tree promoted_type, have_va_type;
12902 tree valist = TREE_OPERAND (*expr_p, 0);
12903 tree type = TREE_TYPE (*expr_p);
12904 tree t, tag, aptag;
12905 location_t loc = EXPR_LOCATION (*expr_p);
12906
12907 /* Verify that valist is of the proper type. */
12908 have_va_type = TREE_TYPE (valist);
12909 if (have_va_type == error_mark_node)
12910 return GS_ERROR;
12911 have_va_type = targetm.canonical_va_list_type (have_va_type);
12912 if (have_va_type == NULL_TREE
12913 && POINTER_TYPE_P (TREE_TYPE (valist)))
12914 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12915 have_va_type
12916 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12917 gcc_assert (have_va_type != NULL_TREE);
12918
12919 /* Generate a diagnostic for requesting data of a type that cannot
12920 be passed through `...' due to type promotion at the call site. */
12921 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12922 != type)
12923 {
12924 static bool gave_help;
12925 bool warned;
12926 /* Use the expansion point to handle cases such as passing bool (defined
12927 in a system header) through `...'. */
12928 source_location xloc
12929 = expansion_point_location_if_in_system_header (loc);
12930
12931 /* Unfortunately, this is merely undefined, rather than a constraint
12932 violation, so we cannot make this an error. If this call is never
12933 executed, the program is still strictly conforming. */
12934 warned = warning_at (xloc, 0,
12935 "%qT is promoted to %qT when passed through %<...%>",
12936 type, promoted_type);
12937 if (!gave_help && warned)
12938 {
12939 gave_help = true;
12940 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12941 promoted_type, type);
12942 }
12943
12944 /* We can, however, treat "undefined" any way we please.
12945 Call abort to encourage the user to fix the program. */
12946 if (warned)
12947 inform (xloc, "if this code is reached, the program will abort");
12948 /* Before the abort, allow the evaluation of the va_list
12949 expression to exit or longjmp. */
12950 gimplify_and_add (valist, pre_p);
12951 t = build_call_expr_loc (loc,
12952 builtin_decl_implicit (BUILT_IN_TRAP), 0);
12953 gimplify_and_add (t, pre_p);
12954
12955 /* This is dead code, but go ahead and finish so that the
12956 mode of the result comes out right. */
12957 *expr_p = dummy_object (type);
12958 return GS_ALL_DONE;
12959 }
12960
12961 tag = build_int_cst (build_pointer_type (type), 0);
12962 aptag = build_int_cst (TREE_TYPE (valist), 0);
12963
12964 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12965 valist, tag, aptag);
12966
12967 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12968 needs to be expanded. */
12969 cfun->curr_properties &= ~PROP_gimple_lva;
12970
12971 return GS_OK;
12972 }
12973
12974 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12975
12976 DST/SRC are the destination and source respectively. You can pass
12977 ungimplified trees in DST or SRC, in which case they will be
12978 converted to a gimple operand if necessary.
12979
12980 This function returns the newly created GIMPLE_ASSIGN tuple. */
12981
12982 gimple *
12983 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12984 {
12985 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12986 gimplify_and_add (t, seq_p);
12987 ggc_free (t);
12988 return gimple_seq_last_stmt (*seq_p);
12989 }
12990
12991 inline hashval_t
12992 gimplify_hasher::hash (const elt_t *p)
12993 {
12994 tree t = p->val;
12995 return iterative_hash_expr (t, 0);
12996 }
12997
12998 inline bool
12999 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13000 {
13001 tree t1 = p1->val;
13002 tree t2 = p2->val;
13003 enum tree_code code = TREE_CODE (t1);
13004
13005 if (TREE_CODE (t2) != code
13006 || TREE_TYPE (t1) != TREE_TYPE (t2))
13007 return false;
13008
13009 if (!operand_equal_p (t1, t2, 0))
13010 return false;
13011
13012 /* Only allow them to compare equal if they also hash equal; otherwise
13013 results are nondeterminate, and we fail bootstrap comparison. */
13014 gcc_checking_assert (hash (p1) == hash (p2));
13015
13016 return true;
13017 }