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