[PR c++/84729] reject parenthesized array init
[official-gcc.git] / gcc / gimplify.c
blob115f80bef9b6fa9238495a53d791fcb0e820a776
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
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.
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.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple.h"
33 #include "gimple-predict.h"
34 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "ssa.h"
36 #include "cgraph.h"
37 #include "tree-pretty-print.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
72 enum gimplify_omp_var_data
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
94 GOVD_MAP_0LEN_ARRAY = 32768,
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO = 65536,
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN = 131072,
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE = 262144,
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT = 524288,
108 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
109 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
110 | GOVD_LOCAL)
114 enum omp_region_type
116 ORT_WORKSHARE = 0x00,
117 ORT_SIMD = 0x01,
119 ORT_PARALLEL = 0x02,
120 ORT_COMBINED_PARALLEL = 0x03,
122 ORT_TASK = 0x04,
123 ORT_UNTIED_TASK = 0x05,
125 ORT_TEAMS = 0x08,
126 ORT_COMBINED_TEAMS = 0x09,
128 /* Data region. */
129 ORT_TARGET_DATA = 0x10,
131 /* Data region with offloading. */
132 ORT_TARGET = 0x20,
133 ORT_COMBINED_TARGET = 0x21,
135 /* OpenACC variants. */
136 ORT_ACC = 0x40, /* A generic OpenACC region. */
137 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
138 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
139 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
140 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
142 /* Dummy OpenMP region, used to disable expansion of
143 DECL_VALUE_EXPRs in taskloop pre body. */
144 ORT_NONE = 0x100
147 /* Gimplify hashtable helper. */
149 struct gimplify_hasher : free_ptr_hash <elt_t>
151 static inline hashval_t hash (const elt_t *);
152 static inline bool equal (const elt_t *, const elt_t *);
155 struct gimplify_ctx
157 struct gimplify_ctx *prev_context;
159 vec<gbind *> bind_expr_stack;
160 tree temps;
161 gimple_seq conditional_cleanups;
162 tree exit_label;
163 tree return_temp;
165 vec<tree> case_labels;
166 hash_set<tree> *live_switch_vars;
167 /* The formal temporary table. Should this be persistent? */
168 hash_table<gimplify_hasher> *temp_htab;
170 int conditions;
171 unsigned into_ssa : 1;
172 unsigned allow_rhs_cond_expr : 1;
173 unsigned in_cleanup_point_expr : 1;
174 unsigned keep_stack : 1;
175 unsigned save_stack : 1;
176 unsigned in_switch_expr : 1;
179 struct gimplify_omp_ctx
181 struct gimplify_omp_ctx *outer_context;
182 splay_tree variables;
183 hash_set<tree> *privatized_types;
184 /* Iteration variables in an OMP_FOR. */
185 vec<tree> loop_iter_var;
186 location_t location;
187 enum omp_clause_default_kind default_kind;
188 enum omp_region_type region_type;
189 bool combined_loop;
190 bool distribute;
191 bool target_map_scalars_firstprivate;
192 bool target_map_pointers_as_0len_arrays;
193 bool target_firstprivatize_array_bases;
196 static struct gimplify_ctx *gimplify_ctxp;
197 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
199 /* Forward declaration. */
200 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
201 static hash_map<tree, tree> *oacc_declare_returns;
202 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
203 bool (*) (tree), fallback_t, bool);
205 /* Shorter alias name for the above function for use in gimplify.c
206 only. */
208 static inline void
209 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
211 gimple_seq_add_stmt_without_update (seq_p, gs);
214 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
215 NULL, a new sequence is allocated. This function is
216 similar to gimple_seq_add_seq, but does not scan the operands.
217 During gimplification, we need to manipulate statement sequences
218 before the def/use vectors have been constructed. */
220 static void
221 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
223 gimple_stmt_iterator si;
225 if (src == NULL)
226 return;
228 si = gsi_last (*dst_p);
229 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234 and popping gimplify contexts. */
236 static struct gimplify_ctx *ctx_pool = NULL;
238 /* Return a gimplify context struct from the pool. */
240 static inline struct gimplify_ctx *
241 ctx_alloc (void)
243 struct gimplify_ctx * c = ctx_pool;
245 if (c)
246 ctx_pool = c->prev_context;
247 else
248 c = XNEW (struct gimplify_ctx);
250 memset (c, '\0', sizeof (*c));
251 return c;
254 /* Put gimplify context C back into the pool. */
256 static inline void
257 ctx_free (struct gimplify_ctx *c)
259 c->prev_context = ctx_pool;
260 ctx_pool = c;
263 /* Free allocated ctx stack memory. */
265 void
266 free_gimplify_stack (void)
268 struct gimplify_ctx *c;
270 while ((c = ctx_pool))
272 ctx_pool = c->prev_context;
273 free (c);
278 /* Set up a context for the gimplifier. */
280 void
281 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
283 struct gimplify_ctx *c = ctx_alloc ();
285 c->prev_context = gimplify_ctxp;
286 gimplify_ctxp = c;
287 gimplify_ctxp->into_ssa = in_ssa;
288 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
291 /* Tear down a context for the gimplifier. If BODY is non-null, then
292 put the temporaries into the outer BIND_EXPR. Otherwise, put them
293 in the local_decls.
295 BODY is not a sequence, but the first tuple in a sequence. */
297 void
298 pop_gimplify_context (gimple *body)
300 struct gimplify_ctx *c = gimplify_ctxp;
302 gcc_assert (c
303 && (!c->bind_expr_stack.exists ()
304 || c->bind_expr_stack.is_empty ()));
305 c->bind_expr_stack.release ();
306 gimplify_ctxp = c->prev_context;
308 if (body)
309 declare_vars (c->temps, body, false);
310 else
311 record_vars (c->temps);
313 delete c->temp_htab;
314 c->temp_htab = NULL;
315 ctx_free (c);
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
320 static void
321 gimple_push_bind_expr (gbind *bind_stmt)
323 gimplify_ctxp->bind_expr_stack.reserve (8);
324 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
327 /* Pop the first element off the stack of bindings. */
329 static void
330 gimple_pop_bind_expr (void)
332 gimplify_ctxp->bind_expr_stack.pop ();
335 /* Return the first element of the stack of bindings. */
337 gbind *
338 gimple_current_bind_expr (void)
340 return gimplify_ctxp->bind_expr_stack.last ();
343 /* Return the stack of bindings created during gimplification. */
345 vec<gbind *>
346 gimple_bind_expr_stack (void)
348 return gimplify_ctxp->bind_expr_stack;
351 /* Return true iff there is a COND_EXPR between us and the innermost
352 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
354 static bool
355 gimple_conditional_context (void)
357 return gimplify_ctxp->conditions > 0;
360 /* Note that we've entered a COND_EXPR. */
362 static void
363 gimple_push_condition (void)
365 #ifdef ENABLE_GIMPLE_CHECKING
366 if (gimplify_ctxp->conditions == 0)
367 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
368 #endif
369 ++(gimplify_ctxp->conditions);
372 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
373 now, add any conditional cleanups we've seen to the prequeue. */
375 static void
376 gimple_pop_condition (gimple_seq *pre_p)
378 int conds = --(gimplify_ctxp->conditions);
380 gcc_assert (conds >= 0);
381 if (conds == 0)
383 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
384 gimplify_ctxp->conditional_cleanups = NULL;
388 /* A stable comparison routine for use with splay trees and DECLs. */
390 static int
391 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
393 tree a = (tree) xa;
394 tree b = (tree) xb;
396 return DECL_UID (a) - DECL_UID (b);
399 /* Create a new omp construct that deals with variable remapping. */
401 static struct gimplify_omp_ctx *
402 new_omp_context (enum omp_region_type region_type)
404 struct gimplify_omp_ctx *c;
406 c = XCNEW (struct gimplify_omp_ctx);
407 c->outer_context = gimplify_omp_ctxp;
408 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
409 c->privatized_types = new hash_set<tree>;
410 c->location = input_location;
411 c->region_type = region_type;
412 if ((region_type & ORT_TASK) == 0)
413 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
414 else
415 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
417 return c;
420 /* Destroy an omp construct that deals with variable remapping. */
422 static void
423 delete_omp_context (struct gimplify_omp_ctx *c)
425 splay_tree_delete (c->variables);
426 delete c->privatized_types;
427 c->loop_iter_var.release ();
428 XDELETE (c);
431 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
434 /* Both gimplify the statement T and append it to *SEQ_P. This function
435 behaves exactly as gimplify_stmt, but you don't have to pass T as a
436 reference. */
438 void
439 gimplify_and_add (tree t, gimple_seq *seq_p)
441 gimplify_stmt (&t, seq_p);
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445 tuple in the sequence of generated tuples for this statement.
446 Return NULL if gimplifying T produced no tuples. */
448 static gimple *
449 gimplify_and_return_first (tree t, gimple_seq *seq_p)
451 gimple_stmt_iterator last = gsi_last (*seq_p);
453 gimplify_and_add (t, seq_p);
455 if (!gsi_end_p (last))
457 gsi_next (&last);
458 return gsi_stmt (last);
460 else
461 return gimple_seq_first_stmt (*seq_p);
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465 LHS, or for a call argument. */
467 static bool
468 is_gimple_mem_rhs (tree t)
470 /* If we're dealing with a renamable type, either source or dest must be
471 a renamed variable. */
472 if (is_gimple_reg_type (TREE_TYPE (t)))
473 return is_gimple_val (t);
474 else
475 return is_gimple_val (t) || is_gimple_lvalue (t);
478 /* Return true if T is a CALL_EXPR or an expression that can be
479 assigned to a temporary. Note that this predicate should only be
480 used during gimplification. See the rationale for this in
481 gimplify_modify_expr. */
483 static bool
484 is_gimple_reg_rhs_or_call (tree t)
486 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
487 || TREE_CODE (t) == CALL_EXPR);
490 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
491 this predicate should only be used during gimplification. See the
492 rationale for this in gimplify_modify_expr. */
494 static bool
495 is_gimple_mem_rhs_or_call (tree t)
497 /* If we're dealing with a renamable type, either source or dest must be
498 a renamed variable. */
499 if (is_gimple_reg_type (TREE_TYPE (t)))
500 return is_gimple_val (t);
501 else
502 return (is_gimple_val (t)
503 || is_gimple_lvalue (t)
504 || TREE_CLOBBER_P (t)
505 || TREE_CODE (t) == CALL_EXPR);
508 /* Create a temporary with a name derived from VAL. Subroutine of
509 lookup_tmp_var; nobody else should call this function. */
511 static inline tree
512 create_tmp_from_val (tree val)
514 /* Drop all qualifiers and address-space information from the value type. */
515 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
516 tree var = create_tmp_var (type, get_name (val));
517 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
518 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
519 DECL_GIMPLE_REG_P (var) = 1;
520 return var;
523 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
524 an existing expression temporary. */
526 static tree
527 lookup_tmp_var (tree val, bool is_formal)
529 tree ret;
531 /* If not optimizing, never really reuse a temporary. local-alloc
532 won't allocate any variable that is used in more than one basic
533 block, which means it will go into memory, causing much extra
534 work in reload and final and poorer code generation, outweighing
535 the extra memory allocation here. */
536 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
537 ret = create_tmp_from_val (val);
538 else
540 elt_t elt, *elt_p;
541 elt_t **slot;
543 elt.val = val;
544 if (!gimplify_ctxp->temp_htab)
545 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
546 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
547 if (*slot == NULL)
549 elt_p = XNEW (elt_t);
550 elt_p->val = val;
551 elt_p->temp = ret = create_tmp_from_val (val);
552 *slot = elt_p;
554 else
556 elt_p = *slot;
557 ret = elt_p->temp;
561 return ret;
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
566 static tree
567 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
568 bool is_formal, bool allow_ssa)
570 tree t, mod;
572 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
574 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
575 fb_rvalue);
577 if (allow_ssa
578 && gimplify_ctxp->into_ssa
579 && is_gimple_reg_type (TREE_TYPE (val)))
581 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
582 if (! gimple_in_ssa_p (cfun))
584 const char *name = get_name (val);
585 if (name)
586 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
589 else
590 t = lookup_tmp_var (val, is_formal);
592 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
594 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
596 /* gimplify_modify_expr might want to reduce this further. */
597 gimplify_and_add (mod, pre_p);
598 ggc_free (mod);
600 return t;
603 /* Return a formal temporary variable initialized with VAL. PRE_P is as
604 in gimplify_expr. Only use this function if:
606 1) The value of the unfactored expression represented by VAL will not
607 change between the initialization and use of the temporary, and
608 2) The temporary will not be otherwise modified.
610 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611 and #2 means it is inappropriate for && temps.
613 For other cases, use get_initialized_tmp_var instead. */
615 tree
616 get_formal_tmp_var (tree val, gimple_seq *pre_p)
618 return internal_get_tmp_var (val, pre_p, NULL, true, true);
621 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
622 are as in gimplify_expr. */
624 tree
625 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
626 bool allow_ssa)
628 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
631 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
632 generate debug info for them; otherwise don't. */
634 void
635 declare_vars (tree vars, gimple *gs, bool debug_info)
637 tree last = vars;
638 if (last)
640 tree temps, block;
642 gbind *scope = as_a <gbind *> (gs);
644 temps = nreverse (last);
646 block = gimple_bind_block (scope);
647 gcc_assert (!block || TREE_CODE (block) == BLOCK);
648 if (!block || !debug_info)
650 DECL_CHAIN (last) = gimple_bind_vars (scope);
651 gimple_bind_set_vars (scope, temps);
653 else
655 /* We need to attach the nodes both to the BIND_EXPR and to its
656 associated BLOCK for debugging purposes. The key point here
657 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
659 if (BLOCK_VARS (block))
660 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
661 else
663 gimple_bind_set_vars (scope,
664 chainon (gimple_bind_vars (scope), temps));
665 BLOCK_VARS (block) = temps;
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
673 no such upper bound can be obtained. */
675 static void
676 force_constant_size (tree var)
678 /* The only attempt we make is by querying the maximum size of objects
679 of the variable's type. */
681 HOST_WIDE_INT max_size;
683 gcc_assert (VAR_P (var));
685 max_size = max_int_size_in_bytes (TREE_TYPE (var));
687 gcc_assert (max_size >= 0);
689 DECL_SIZE_UNIT (var)
690 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
691 DECL_SIZE (var)
692 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
695 /* Push the temporary variable TMP into the current binding. */
697 void
698 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
700 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
702 /* Later processing assumes that the object size is constant, which might
703 not be true at this point. Force the use of a constant upper bound in
704 this case. */
705 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
706 force_constant_size (tmp);
708 DECL_CONTEXT (tmp) = fn->decl;
709 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
711 record_vars_into (tmp, fn->decl);
714 /* Push the temporary variable TMP into the current binding. */
716 void
717 gimple_add_tmp_var (tree tmp)
719 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
721 /* Later processing assumes that the object size is constant, which might
722 not be true at this point. Force the use of a constant upper bound in
723 this case. */
724 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
725 force_constant_size (tmp);
727 DECL_CONTEXT (tmp) = current_function_decl;
728 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
730 if (gimplify_ctxp)
732 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
733 gimplify_ctxp->temps = tmp;
735 /* Mark temporaries local within the nearest enclosing parallel. */
736 if (gimplify_omp_ctxp)
738 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
739 while (ctx
740 && (ctx->region_type == ORT_WORKSHARE
741 || ctx->region_type == ORT_SIMD
742 || ctx->region_type == ORT_ACC))
743 ctx = ctx->outer_context;
744 if (ctx)
745 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
748 else if (cfun)
749 record_vars (tmp);
750 else
752 gimple_seq body_seq;
754 /* This case is for nested functions. We need to expose the locals
755 they create. */
756 body_seq = gimple_body (current_function_decl);
757 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764 nodes that are referenced more than once in GENERIC functions. This is
765 necessary because gimplification (translation into GIMPLE) is performed
766 by modifying tree nodes in-place, so gimplication of a shared node in a
767 first context could generate an invalid GIMPLE form in a second context.
769 This is achieved with a simple mark/copy/unmark algorithm that walks the
770 GENERIC representation top-down, marks nodes with TREE_VISITED the first
771 time it encounters them, duplicates them if they already have TREE_VISITED
772 set, and finally removes the TREE_VISITED marks it has set.
774 The algorithm works only at the function level, i.e. it generates a GENERIC
775 representation of a function with no nodes shared within the function when
776 passed a GENERIC function (except for nodes that are allowed to be shared).
778 At the global level, it is also necessary to unshare tree nodes that are
779 referenced in more than one function, for the same aforementioned reason.
780 This requires some cooperation from the front-end. There are 2 strategies:
782 1. Manual unsharing. The front-end needs to call unshare_expr on every
783 expression that might end up being shared across functions.
785 2. Deep unsharing. This is an extension of regular unsharing. Instead
786 of calling unshare_expr on expressions that might be shared across
787 functions, the front-end pre-marks them with TREE_VISITED. This will
788 ensure that they are unshared on the first reference within functions
789 when the regular unsharing algorithm runs. The counterpart is that
790 this algorithm must look deeper than for manual unsharing, which is
791 specified by LANG_HOOKS_DEEP_UNSHARING.
793 If there are only few specific cases of node sharing across functions, it is
794 probably easier for a front-end to unshare the expressions manually. On the
795 contrary, if the expressions generated at the global level are as widespread
796 as expressions generated within functions, deep unsharing is very likely the
797 way to go. */
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that must be done once. If we were to
801 unshare something like SAVE_EXPR(i++), the gimplification process would
802 create wrong code. However, if DATA is non-null, it must hold a pointer
803 set that is used to unshare the subtrees of these nodes. */
805 static tree
806 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
808 tree t = *tp;
809 enum tree_code code = TREE_CODE (t);
811 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812 copy their subtrees if we can make sure to do it only once. */
813 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
815 if (data && !((hash_set<tree> *)data)->add (t))
817 else
818 *walk_subtrees = 0;
821 /* Stop at types, decls, constants like copy_tree_r. */
822 else if (TREE_CODE_CLASS (code) == tcc_type
823 || TREE_CODE_CLASS (code) == tcc_declaration
824 || TREE_CODE_CLASS (code) == tcc_constant)
825 *walk_subtrees = 0;
827 /* Cope with the statement expression extension. */
828 else if (code == STATEMENT_LIST)
831 /* Leave the bulk of the work to copy_tree_r itself. */
832 else
833 copy_tree_r (tp, walk_subtrees, NULL);
835 return NULL_TREE;
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839 If *TP has been visited already, then *TP is deeply copied by calling
840 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
842 static tree
843 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
845 tree t = *tp;
846 enum tree_code code = TREE_CODE (t);
848 /* Skip types, decls, and constants. But we do want to look at their
849 types and the bounds of types. Mark them as visited so we properly
850 unmark their subtrees on the unmark pass. If we've already seen them,
851 don't look down further. */
852 if (TREE_CODE_CLASS (code) == tcc_type
853 || TREE_CODE_CLASS (code) == tcc_declaration
854 || TREE_CODE_CLASS (code) == tcc_constant)
856 if (TREE_VISITED (t))
857 *walk_subtrees = 0;
858 else
859 TREE_VISITED (t) = 1;
862 /* If this node has been visited already, unshare it and don't look
863 any deeper. */
864 else if (TREE_VISITED (t))
866 walk_tree (tp, mostly_copy_tree_r, data, NULL);
867 *walk_subtrees = 0;
870 /* Otherwise, mark the node as visited and keep looking. */
871 else
872 TREE_VISITED (t) = 1;
874 return NULL_TREE;
877 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
878 copy_if_shared_r callback unmodified. */
880 static inline void
881 copy_if_shared (tree *tp, void *data)
883 walk_tree (tp, copy_if_shared_r, data, NULL);
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887 any nested functions. */
889 static void
890 unshare_body (tree fndecl)
892 struct cgraph_node *cgn = cgraph_node::get (fndecl);
893 /* If the language requires deep unsharing, we need a pointer set to make
894 sure we don't repeatedly unshare subtrees of unshareable nodes. */
895 hash_set<tree> *visited
896 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
898 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
899 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
900 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
902 delete visited;
904 if (cgn)
905 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
906 unshare_body (cgn->decl);
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910 Subtrees are walked until the first unvisited node is encountered. */
912 static tree
913 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
915 tree t = *tp;
917 /* If this node has been visited, unmark it and keep looking. */
918 if (TREE_VISITED (t))
919 TREE_VISITED (t) = 0;
921 /* Otherwise, don't look any deeper. */
922 else
923 *walk_subtrees = 0;
925 return NULL_TREE;
928 /* Unmark the visited trees rooted at *TP. */
930 static inline void
931 unmark_visited (tree *tp)
933 walk_tree (tp, unmark_visited_r, NULL, NULL);
936 /* Likewise, but mark all trees as not visited. */
938 static void
939 unvisit_body (tree fndecl)
941 struct cgraph_node *cgn = cgraph_node::get (fndecl);
943 unmark_visited (&DECL_SAVED_TREE (fndecl));
944 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
945 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
947 if (cgn)
948 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
949 unvisit_body (cgn->decl);
952 /* Unconditionally make an unshared copy of EXPR. This is used when using
953 stored expressions which span multiple functions, such as BINFO_VTABLE,
954 as the normal unsharing process can't tell that they're shared. */
956 tree
957 unshare_expr (tree expr)
959 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
960 return expr;
963 /* Worker for unshare_expr_without_location. */
965 static tree
966 prune_expr_location (tree *tp, int *walk_subtrees, void *)
968 if (EXPR_P (*tp))
969 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
970 else
971 *walk_subtrees = 0;
972 return NULL_TREE;
975 /* Similar to unshare_expr but also prune all expression locations
976 from EXPR. */
978 tree
979 unshare_expr_without_location (tree expr)
981 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
982 if (EXPR_P (expr))
983 walk_tree (&expr, prune_expr_location, NULL, NULL);
984 return expr;
987 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
988 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
989 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
990 EXPR is the location of the EXPR. */
992 static location_t
993 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
995 if (!expr)
996 return or_else;
998 if (EXPR_HAS_LOCATION (expr))
999 return EXPR_LOCATION (expr);
1001 if (TREE_CODE (expr) != STATEMENT_LIST)
1002 return or_else;
1004 tree_stmt_iterator i = tsi_start (expr);
1006 bool found = false;
1007 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1009 found = true;
1010 tsi_next (&i);
1013 if (!found || !tsi_one_before_end_p (i))
1014 return or_else;
1016 return rexpr_location (tsi_stmt (i), or_else);
1019 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1020 rexpr_location for the potential recursion. */
1022 static inline bool
1023 rexpr_has_location (tree expr)
1025 return rexpr_location (expr) != UNKNOWN_LOCATION;
1029 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1030 contain statements and have a value. Assign its value to a temporary
1031 and give it void_type_node. Return the temporary, or NULL_TREE if
1032 WRAPPER was already void. */
1034 tree
1035 voidify_wrapper_expr (tree wrapper, tree temp)
1037 tree type = TREE_TYPE (wrapper);
1038 if (type && !VOID_TYPE_P (type))
1040 tree *p;
1042 /* Set p to point to the body of the wrapper. Loop until we find
1043 something that isn't a wrapper. */
1044 for (p = &wrapper; p && *p; )
1046 switch (TREE_CODE (*p))
1048 case BIND_EXPR:
1049 TREE_SIDE_EFFECTS (*p) = 1;
1050 TREE_TYPE (*p) = void_type_node;
1051 /* For a BIND_EXPR, the body is operand 1. */
1052 p = &BIND_EXPR_BODY (*p);
1053 break;
1055 case CLEANUP_POINT_EXPR:
1056 case TRY_FINALLY_EXPR:
1057 case TRY_CATCH_EXPR:
1058 TREE_SIDE_EFFECTS (*p) = 1;
1059 TREE_TYPE (*p) = void_type_node;
1060 p = &TREE_OPERAND (*p, 0);
1061 break;
1063 case STATEMENT_LIST:
1065 tree_stmt_iterator i = tsi_last (*p);
1066 TREE_SIDE_EFFECTS (*p) = 1;
1067 TREE_TYPE (*p) = void_type_node;
1068 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1070 break;
1072 case COMPOUND_EXPR:
1073 /* Advance to the last statement. Set all container types to
1074 void. */
1075 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1077 TREE_SIDE_EFFECTS (*p) = 1;
1078 TREE_TYPE (*p) = void_type_node;
1080 break;
1082 case TRANSACTION_EXPR:
1083 TREE_SIDE_EFFECTS (*p) = 1;
1084 TREE_TYPE (*p) = void_type_node;
1085 p = &TRANSACTION_EXPR_BODY (*p);
1086 break;
1088 default:
1089 /* Assume that any tree upon which voidify_wrapper_expr is
1090 directly called is a wrapper, and that its body is op0. */
1091 if (p == &wrapper)
1093 TREE_SIDE_EFFECTS (*p) = 1;
1094 TREE_TYPE (*p) = void_type_node;
1095 p = &TREE_OPERAND (*p, 0);
1096 break;
1098 goto out;
1102 out:
1103 if (p == NULL || IS_EMPTY_STMT (*p))
1104 temp = NULL_TREE;
1105 else if (temp)
1107 /* The wrapper is on the RHS of an assignment that we're pushing
1108 down. */
1109 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1110 || TREE_CODE (temp) == MODIFY_EXPR);
1111 TREE_OPERAND (temp, 1) = *p;
1112 *p = temp;
1114 else
1116 temp = create_tmp_var (type, "retval");
1117 *p = build2 (INIT_EXPR, type, temp, *p);
1120 return temp;
1123 return NULL_TREE;
1126 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1127 a temporary through which they communicate. */
1129 static void
1130 build_stack_save_restore (gcall **save, gcall **restore)
1132 tree tmp_var;
1134 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1135 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1136 gimple_call_set_lhs (*save, tmp_var);
1138 *restore
1139 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1140 1, tmp_var);
1143 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1145 static tree
1146 build_asan_poison_call_expr (tree decl)
1148 /* Do not poison variables that have size equal to zero. */
1149 tree unit_size = DECL_SIZE_UNIT (decl);
1150 if (zerop (unit_size))
1151 return NULL_TREE;
1153 tree base = build_fold_addr_expr (decl);
1155 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1156 void_type_node, 3,
1157 build_int_cst (integer_type_node,
1158 ASAN_MARK_POISON),
1159 base, unit_size);
1162 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1163 on POISON flag, shadow memory of a DECL variable. The call will be
1164 put on location identified by IT iterator, where BEFORE flag drives
1165 position where the stmt will be put. */
1167 static void
1168 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1169 bool before)
1171 /* When within an OMP context, do not emit ASAN_MARK internal fns. */
1172 if (gimplify_omp_ctxp)
1173 return;
1175 tree unit_size = DECL_SIZE_UNIT (decl);
1176 tree base = build_fold_addr_expr (decl);
1178 /* Do not poison variables that have size equal to zero. */
1179 if (zerop (unit_size))
1180 return;
1182 /* It's necessary to have all stack variables aligned to ASAN granularity
1183 bytes. */
1184 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1185 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1187 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1189 gimple *g
1190 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1191 build_int_cst (integer_type_node, flags),
1192 base, unit_size);
1194 if (before)
1195 gsi_insert_before (it, g, GSI_NEW_STMT);
1196 else
1197 gsi_insert_after (it, g, GSI_NEW_STMT);
1200 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1201 either poisons or unpoisons a DECL. Created statement is appended
1202 to SEQ_P gimple sequence. */
1204 static void
1205 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1207 gimple_stmt_iterator it = gsi_last (*seq_p);
1208 bool before = false;
1210 if (gsi_end_p (it))
1211 before = true;
1213 asan_poison_variable (decl, poison, &it, before);
1216 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1218 static int
1219 sort_by_decl_uid (const void *a, const void *b)
1221 const tree *t1 = (const tree *)a;
1222 const tree *t2 = (const tree *)b;
1224 int uid1 = DECL_UID (*t1);
1225 int uid2 = DECL_UID (*t2);
1227 if (uid1 < uid2)
1228 return -1;
1229 else if (uid1 > uid2)
1230 return 1;
1231 else
1232 return 0;
1235 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1236 depending on POISON flag. Created statement is appended
1237 to SEQ_P gimple sequence. */
1239 static void
1240 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1242 unsigned c = variables->elements ();
1243 if (c == 0)
1244 return;
1246 auto_vec<tree> sorted_variables (c);
1248 for (hash_set<tree>::iterator it = variables->begin ();
1249 it != variables->end (); ++it)
1250 sorted_variables.safe_push (*it);
1252 sorted_variables.qsort (sort_by_decl_uid);
1254 unsigned i;
1255 tree var;
1256 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1258 asan_poison_variable (var, poison, seq_p);
1260 /* Add use_after_scope_memory attribute for the variable in order
1261 to prevent re-written into SSA. */
1262 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1263 DECL_ATTRIBUTES (var)))
1264 DECL_ATTRIBUTES (var)
1265 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1266 integer_one_node,
1267 DECL_ATTRIBUTES (var));
1271 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1273 static enum gimplify_status
1274 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1276 tree bind_expr = *expr_p;
1277 bool old_keep_stack = gimplify_ctxp->keep_stack;
1278 bool old_save_stack = gimplify_ctxp->save_stack;
1279 tree t;
1280 gbind *bind_stmt;
1281 gimple_seq body, cleanup;
1282 gcall *stack_save;
1283 location_t start_locus = 0, end_locus = 0;
1284 tree ret_clauses = NULL;
1286 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1288 /* Mark variables seen in this bind expr. */
1289 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1291 if (VAR_P (t))
1293 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1295 /* Mark variable as local. */
1296 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1297 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1298 || splay_tree_lookup (ctx->variables,
1299 (splay_tree_key) t) == NULL))
1301 if (ctx->region_type == ORT_SIMD
1302 && TREE_ADDRESSABLE (t)
1303 && !TREE_STATIC (t))
1304 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1305 else
1306 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1309 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1311 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1312 cfun->has_local_explicit_reg_vars = true;
1315 /* Preliminarily mark non-addressed complex variables as eligible
1316 for promotion to gimple registers. We'll transform their uses
1317 as we find them. */
1318 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1319 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1320 && !TREE_THIS_VOLATILE (t)
1321 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1322 && !needs_to_live_in_memory (t))
1323 DECL_GIMPLE_REG_P (t) = 1;
1326 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1327 BIND_EXPR_BLOCK (bind_expr));
1328 gimple_push_bind_expr (bind_stmt);
1330 gimplify_ctxp->keep_stack = false;
1331 gimplify_ctxp->save_stack = false;
1333 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1334 body = NULL;
1335 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1336 gimple_bind_set_body (bind_stmt, body);
1338 /* Source location wise, the cleanup code (stack_restore and clobbers)
1339 belongs to the end of the block, so propagate what we have. The
1340 stack_save operation belongs to the beginning of block, which we can
1341 infer from the bind_expr directly if the block has no explicit
1342 assignment. */
1343 if (BIND_EXPR_BLOCK (bind_expr))
1345 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1346 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1348 if (start_locus == 0)
1349 start_locus = EXPR_LOCATION (bind_expr);
1351 cleanup = NULL;
1352 stack_save = NULL;
1354 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1355 the stack space allocated to the VLAs. */
1356 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1358 gcall *stack_restore;
1360 /* Save stack on entry and restore it on exit. Add a try_finally
1361 block to achieve this. */
1362 build_stack_save_restore (&stack_save, &stack_restore);
1364 gimple_set_location (stack_save, start_locus);
1365 gimple_set_location (stack_restore, end_locus);
1367 gimplify_seq_add_stmt (&cleanup, stack_restore);
1370 /* Add clobbers for all variables that go out of scope. */
1371 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1373 if (VAR_P (t)
1374 && !is_global_var (t)
1375 && DECL_CONTEXT (t) == current_function_decl)
1377 if (!DECL_HARD_REGISTER (t)
1378 && !TREE_THIS_VOLATILE (t)
1379 && !DECL_HAS_VALUE_EXPR_P (t)
1380 /* Only care for variables that have to be in memory. Others
1381 will be rewritten into SSA names, hence moved to the
1382 top-level. */
1383 && !is_gimple_reg (t)
1384 && flag_stack_reuse != SR_NONE)
1386 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1387 gimple *clobber_stmt;
1388 TREE_THIS_VOLATILE (clobber) = 1;
1389 clobber_stmt = gimple_build_assign (t, clobber);
1390 gimple_set_location (clobber_stmt, end_locus);
1391 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1394 if (flag_openacc && oacc_declare_returns != NULL)
1396 tree *c = oacc_declare_returns->get (t);
1397 if (c != NULL)
1399 if (ret_clauses)
1400 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1402 ret_clauses = *c;
1404 oacc_declare_returns->remove (t);
1406 if (oacc_declare_returns->elements () == 0)
1408 delete oacc_declare_returns;
1409 oacc_declare_returns = NULL;
1415 if (asan_poisoned_variables != NULL
1416 && asan_poisoned_variables->contains (t))
1418 asan_poisoned_variables->remove (t);
1419 asan_poison_variable (t, true, &cleanup);
1422 if (gimplify_ctxp->live_switch_vars != NULL
1423 && gimplify_ctxp->live_switch_vars->contains (t))
1424 gimplify_ctxp->live_switch_vars->remove (t);
1427 if (ret_clauses)
1429 gomp_target *stmt;
1430 gimple_stmt_iterator si = gsi_start (cleanup);
1432 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1433 ret_clauses);
1434 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1437 if (cleanup)
1439 gtry *gs;
1440 gimple_seq new_body;
1442 new_body = NULL;
1443 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1444 GIMPLE_TRY_FINALLY);
1446 if (stack_save)
1447 gimplify_seq_add_stmt (&new_body, stack_save);
1448 gimplify_seq_add_stmt (&new_body, gs);
1449 gimple_bind_set_body (bind_stmt, new_body);
1452 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1453 if (!gimplify_ctxp->keep_stack)
1454 gimplify_ctxp->keep_stack = old_keep_stack;
1455 gimplify_ctxp->save_stack = old_save_stack;
1457 gimple_pop_bind_expr ();
1459 gimplify_seq_add_stmt (pre_p, bind_stmt);
1461 if (temp)
1463 *expr_p = temp;
1464 return GS_OK;
1467 *expr_p = NULL_TREE;
1468 return GS_ALL_DONE;
1471 /* Maybe add early return predict statement to PRE_P sequence. */
1473 static void
1474 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1476 /* If we are not in a conditional context, add PREDICT statement. */
1477 if (gimple_conditional_context ())
1479 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1480 NOT_TAKEN);
1481 gimplify_seq_add_stmt (pre_p, predict);
1485 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1486 GIMPLE value, it is assigned to a new temporary and the statement is
1487 re-written to return the temporary.
1489 PRE_P points to the sequence where side effects that must happen before
1490 STMT should be stored. */
1492 static enum gimplify_status
1493 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1495 greturn *ret;
1496 tree ret_expr = TREE_OPERAND (stmt, 0);
1497 tree result_decl, result;
1499 if (ret_expr == error_mark_node)
1500 return GS_ERROR;
1502 if (!ret_expr
1503 || TREE_CODE (ret_expr) == RESULT_DECL)
1505 maybe_add_early_return_predict_stmt (pre_p);
1506 greturn *ret = gimple_build_return (ret_expr);
1507 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1508 gimplify_seq_add_stmt (pre_p, ret);
1509 return GS_ALL_DONE;
1512 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1513 result_decl = NULL_TREE;
1514 else
1516 result_decl = TREE_OPERAND (ret_expr, 0);
1518 /* See through a return by reference. */
1519 if (TREE_CODE (result_decl) == INDIRECT_REF)
1520 result_decl = TREE_OPERAND (result_decl, 0);
1522 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1523 || TREE_CODE (ret_expr) == INIT_EXPR)
1524 && TREE_CODE (result_decl) == RESULT_DECL);
1527 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1528 Recall that aggregate_value_p is FALSE for any aggregate type that is
1529 returned in registers. If we're returning values in registers, then
1530 we don't want to extend the lifetime of the RESULT_DECL, particularly
1531 across another call. In addition, for those aggregates for which
1532 hard_function_value generates a PARALLEL, we'll die during normal
1533 expansion of structure assignments; there's special code in expand_return
1534 to handle this case that does not exist in expand_expr. */
1535 if (!result_decl)
1536 result = NULL_TREE;
1537 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1539 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1541 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1542 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1543 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1544 should be effectively allocated by the caller, i.e. all calls to
1545 this function must be subject to the Return Slot Optimization. */
1546 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1547 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1549 result = result_decl;
1551 else if (gimplify_ctxp->return_temp)
1552 result = gimplify_ctxp->return_temp;
1553 else
1555 result = create_tmp_reg (TREE_TYPE (result_decl));
1557 /* ??? With complex control flow (usually involving abnormal edges),
1558 we can wind up warning about an uninitialized value for this. Due
1559 to how this variable is constructed and initialized, this is never
1560 true. Give up and never warn. */
1561 TREE_NO_WARNING (result) = 1;
1563 gimplify_ctxp->return_temp = result;
1566 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1567 Then gimplify the whole thing. */
1568 if (result != result_decl)
1569 TREE_OPERAND (ret_expr, 0) = result;
1571 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1573 maybe_add_early_return_predict_stmt (pre_p);
1574 ret = gimple_build_return (result);
1575 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1576 gimplify_seq_add_stmt (pre_p, ret);
1578 return GS_ALL_DONE;
1581 /* Gimplify a variable-length array DECL. */
1583 static void
1584 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1586 /* This is a variable-sized decl. Simplify its size and mark it
1587 for deferred expansion. */
1588 tree t, addr, ptr_type;
1590 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1591 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1593 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1594 if (DECL_HAS_VALUE_EXPR_P (decl))
1595 return;
1597 /* All occurrences of this decl in final gimplified code will be
1598 replaced by indirection. Setting DECL_VALUE_EXPR does two
1599 things: First, it lets the rest of the gimplifier know what
1600 replacement to use. Second, it lets the debug info know
1601 where to find the value. */
1602 ptr_type = build_pointer_type (TREE_TYPE (decl));
1603 addr = create_tmp_var (ptr_type, get_name (decl));
1604 DECL_IGNORED_P (addr) = 0;
1605 t = build_fold_indirect_ref (addr);
1606 TREE_THIS_NOTRAP (t) = 1;
1607 SET_DECL_VALUE_EXPR (decl, t);
1608 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1610 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1611 max_int_size_in_bytes (TREE_TYPE (decl)));
1612 /* The call has been built for a variable-sized object. */
1613 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1614 t = fold_convert (ptr_type, t);
1615 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1617 gimplify_and_add (t, seq_p);
1620 /* A helper function to be called via walk_tree. Mark all labels under *TP
1621 as being forced. To be called for DECL_INITIAL of static variables. */
1623 static tree
1624 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1626 if (TYPE_P (*tp))
1627 *walk_subtrees = 0;
1628 if (TREE_CODE (*tp) == LABEL_DECL)
1630 FORCED_LABEL (*tp) = 1;
1631 cfun->has_forced_label_in_static = 1;
1634 return NULL_TREE;
1637 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1638 and initialization explicit. */
1640 static enum gimplify_status
1641 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1643 tree stmt = *stmt_p;
1644 tree decl = DECL_EXPR_DECL (stmt);
1646 *stmt_p = NULL_TREE;
1648 if (TREE_TYPE (decl) == error_mark_node)
1649 return GS_ERROR;
1651 if ((TREE_CODE (decl) == TYPE_DECL
1652 || VAR_P (decl))
1653 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1655 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1656 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1657 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1660 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1661 in case its size expressions contain problematic nodes like CALL_EXPR. */
1662 if (TREE_CODE (decl) == TYPE_DECL
1663 && DECL_ORIGINAL_TYPE (decl)
1664 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1666 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1667 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1668 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1671 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1673 tree init = DECL_INITIAL (decl);
1674 bool is_vla = false;
1676 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1677 || (!TREE_STATIC (decl)
1678 && flag_stack_check == GENERIC_STACK_CHECK
1679 && compare_tree_int (DECL_SIZE_UNIT (decl),
1680 STACK_CHECK_MAX_VAR_SIZE) > 0))
1682 gimplify_vla_decl (decl, seq_p);
1683 is_vla = true;
1686 if (asan_poisoned_variables
1687 && !is_vla
1688 && TREE_ADDRESSABLE (decl)
1689 && !TREE_STATIC (decl)
1690 && !DECL_HAS_VALUE_EXPR_P (decl)
1691 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1692 && dbg_cnt (asan_use_after_scope))
1694 asan_poisoned_variables->add (decl);
1695 asan_poison_variable (decl, false, seq_p);
1696 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1697 gimplify_ctxp->live_switch_vars->add (decl);
1700 /* Some front ends do not explicitly declare all anonymous
1701 artificial variables. We compensate here by declaring the
1702 variables, though it would be better if the front ends would
1703 explicitly declare them. */
1704 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1705 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1706 gimple_add_tmp_var (decl);
1708 if (init && init != error_mark_node)
1710 if (!TREE_STATIC (decl))
1712 DECL_INITIAL (decl) = NULL_TREE;
1713 init = build2 (INIT_EXPR, void_type_node, decl, init);
1714 gimplify_and_add (init, seq_p);
1715 ggc_free (init);
1717 else
1718 /* We must still examine initializers for static variables
1719 as they may contain a label address. */
1720 walk_tree (&init, force_labels_r, NULL, NULL);
1724 return GS_ALL_DONE;
1727 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1728 and replacing the LOOP_EXPR with goto, but if the loop contains an
1729 EXIT_EXPR, we need to append a label for it to jump to. */
1731 static enum gimplify_status
1732 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1734 tree saved_label = gimplify_ctxp->exit_label;
1735 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1737 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1739 gimplify_ctxp->exit_label = NULL_TREE;
1741 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1743 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1745 if (gimplify_ctxp->exit_label)
1746 gimplify_seq_add_stmt (pre_p,
1747 gimple_build_label (gimplify_ctxp->exit_label));
1749 gimplify_ctxp->exit_label = saved_label;
1751 *expr_p = NULL;
1752 return GS_ALL_DONE;
1755 /* Gimplify a statement list onto a sequence. These may be created either
1756 by an enlightened front-end, or by shortcut_cond_expr. */
1758 static enum gimplify_status
1759 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1761 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1763 tree_stmt_iterator i = tsi_start (*expr_p);
1765 while (!tsi_end_p (i))
1767 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1768 tsi_delink (&i);
1771 if (temp)
1773 *expr_p = temp;
1774 return GS_OK;
1777 return GS_ALL_DONE;
1780 /* Callback for walk_gimple_seq. */
1782 static tree
1783 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1784 struct walk_stmt_info *wi)
1786 gimple *stmt = gsi_stmt (*gsi_p);
1788 *handled_ops_p = true;
1789 switch (gimple_code (stmt))
1791 case GIMPLE_TRY:
1792 /* A compiler-generated cleanup or a user-written try block.
1793 If it's empty, don't dive into it--that would result in
1794 worse location info. */
1795 if (gimple_try_eval (stmt) == NULL)
1797 wi->info = stmt;
1798 return integer_zero_node;
1800 /* Fall through. */
1801 case GIMPLE_BIND:
1802 case GIMPLE_CATCH:
1803 case GIMPLE_EH_FILTER:
1804 case GIMPLE_TRANSACTION:
1805 /* Walk the sub-statements. */
1806 *handled_ops_p = false;
1807 break;
1809 case GIMPLE_DEBUG:
1810 /* Ignore these. We may generate them before declarations that
1811 are never executed. If there's something to warn about,
1812 there will be non-debug stmts too, and we'll catch those. */
1813 break;
1815 case GIMPLE_CALL:
1816 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1818 *handled_ops_p = false;
1819 break;
1821 /* Fall through. */
1822 default:
1823 /* Save the first "real" statement (not a decl/lexical scope/...). */
1824 wi->info = stmt;
1825 return integer_zero_node;
1827 return NULL_TREE;
1830 /* Possibly warn about unreachable statements between switch's controlling
1831 expression and the first case. SEQ is the body of a switch expression. */
1833 static void
1834 maybe_warn_switch_unreachable (gimple_seq seq)
1836 if (!warn_switch_unreachable
1837 /* This warning doesn't play well with Fortran when optimizations
1838 are on. */
1839 || lang_GNU_Fortran ()
1840 || seq == NULL)
1841 return;
1843 struct walk_stmt_info wi;
1844 memset (&wi, 0, sizeof (wi));
1845 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1846 gimple *stmt = (gimple *) wi.info;
1848 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1850 if (gimple_code (stmt) == GIMPLE_GOTO
1851 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1852 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1853 /* Don't warn for compiler-generated gotos. These occur
1854 in Duff's devices, for example. */;
1855 else
1856 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1857 "statement will never be executed");
1862 /* A label entry that pairs label and a location. */
1863 struct label_entry
1865 tree label;
1866 location_t loc;
1869 /* Find LABEL in vector of label entries VEC. */
1871 static struct label_entry *
1872 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1874 unsigned int i;
1875 struct label_entry *l;
1877 FOR_EACH_VEC_ELT (*vec, i, l)
1878 if (l->label == label)
1879 return l;
1880 return NULL;
1883 /* Return true if LABEL, a LABEL_DECL, represents a case label
1884 in a vector of labels CASES. */
1886 static bool
1887 case_label_p (const vec<tree> *cases, tree label)
1889 unsigned int i;
1890 tree l;
1892 FOR_EACH_VEC_ELT (*cases, i, l)
1893 if (CASE_LABEL (l) == label)
1894 return true;
1895 return false;
1898 /* Find the last nondebug statement in a scope STMT. */
1900 static gimple *
1901 last_stmt_in_scope (gimple *stmt)
1903 if (!stmt)
1904 return NULL;
1906 switch (gimple_code (stmt))
1908 case GIMPLE_BIND:
1910 gbind *bind = as_a <gbind *> (stmt);
1911 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1912 return last_stmt_in_scope (stmt);
1915 case GIMPLE_TRY:
1917 gtry *try_stmt = as_a <gtry *> (stmt);
1918 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1919 gimple *last_eval = last_stmt_in_scope (stmt);
1920 if (gimple_stmt_may_fallthru (last_eval)
1921 && (last_eval == NULL
1922 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1923 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1925 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1926 return last_stmt_in_scope (stmt);
1928 else
1929 return last_eval;
1932 case GIMPLE_DEBUG:
1933 gcc_unreachable ();
1935 default:
1936 return stmt;
1940 /* Collect interesting labels in LABELS and return the statement preceding
1941 another case label, or a user-defined label. */
1943 static gimple *
1944 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1945 auto_vec <struct label_entry> *labels)
1947 gimple *prev = NULL;
1951 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1953 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1954 which starts on a GIMPLE_SWITCH and ends with a break label.
1955 Handle that as a single statement that can fall through. */
1956 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1957 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1958 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1959 if (last
1960 && gimple_code (first) == GIMPLE_SWITCH
1961 && gimple_code (last) == GIMPLE_LABEL)
1963 tree label = gimple_label_label (as_a <glabel *> (last));
1964 if (SWITCH_BREAK_LABEL_P (label))
1966 prev = bind;
1967 gsi_next (gsi_p);
1968 continue;
1972 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1973 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1975 /* Nested scope. Only look at the last statement of
1976 the innermost scope. */
1977 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1978 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1979 if (last)
1981 prev = last;
1982 /* It might be a label without a location. Use the
1983 location of the scope then. */
1984 if (!gimple_has_location (prev))
1985 gimple_set_location (prev, bind_loc);
1987 gsi_next (gsi_p);
1988 continue;
1991 /* Ifs are tricky. */
1992 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1994 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1995 tree false_lab = gimple_cond_false_label (cond_stmt);
1996 location_t if_loc = gimple_location (cond_stmt);
1998 /* If we have e.g.
1999 if (i > 1) goto <D.2259>; else goto D;
2000 we can't do much with the else-branch. */
2001 if (!DECL_ARTIFICIAL (false_lab))
2002 break;
2004 /* Go on until the false label, then one step back. */
2005 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2007 gimple *stmt = gsi_stmt (*gsi_p);
2008 if (gimple_code (stmt) == GIMPLE_LABEL
2009 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2010 break;
2013 /* Not found? Oops. */
2014 if (gsi_end_p (*gsi_p))
2015 break;
2017 struct label_entry l = { false_lab, if_loc };
2018 labels->safe_push (l);
2020 /* Go to the last statement of the then branch. */
2021 gsi_prev (gsi_p);
2023 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2024 <D.1759>:
2025 <stmt>;
2026 goto <D.1761>;
2027 <D.1760>:
2029 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2030 && !gimple_has_location (gsi_stmt (*gsi_p)))
2032 /* Look at the statement before, it might be
2033 attribute fallthrough, in which case don't warn. */
2034 gsi_prev (gsi_p);
2035 bool fallthru_before_dest
2036 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2037 gsi_next (gsi_p);
2038 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2039 if (!fallthru_before_dest)
2041 struct label_entry l = { goto_dest, if_loc };
2042 labels->safe_push (l);
2045 /* And move back. */
2046 gsi_next (gsi_p);
2049 /* Remember the last statement. Skip labels that are of no interest
2050 to us. */
2051 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2053 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2054 if (find_label_entry (labels, label))
2055 prev = gsi_stmt (*gsi_p);
2057 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2059 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2060 prev = gsi_stmt (*gsi_p);
2061 gsi_next (gsi_p);
2063 while (!gsi_end_p (*gsi_p)
2064 /* Stop if we find a case or a user-defined label. */
2065 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2066 || !gimple_has_location (gsi_stmt (*gsi_p))));
2068 return prev;
2071 /* Return true if the switch fallthough warning should occur. LABEL is
2072 the label statement that we're falling through to. */
2074 static bool
2075 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2077 gimple_stmt_iterator gsi = *gsi_p;
2079 /* Don't warn if the label is marked with a "falls through" comment. */
2080 if (FALLTHROUGH_LABEL_P (label))
2081 return false;
2083 /* Don't warn for non-case labels followed by a statement:
2084 case 0:
2085 foo ();
2086 label:
2087 bar ();
2088 as these are likely intentional. */
2089 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2091 tree l;
2092 while (!gsi_end_p (gsi)
2093 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2094 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2095 && !case_label_p (&gimplify_ctxp->case_labels, l))
2096 gsi_next_nondebug (&gsi);
2097 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2098 return false;
2101 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2102 immediately breaks. */
2103 gsi = *gsi_p;
2105 /* Skip all immediately following labels. */
2106 while (!gsi_end_p (gsi)
2107 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2108 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2109 gsi_next_nondebug (&gsi);
2111 /* { ... something; default:; } */
2112 if (gsi_end_p (gsi)
2113 /* { ... something; default: break; } or
2114 { ... something; default: goto L; } */
2115 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2116 /* { ... something; default: return; } */
2117 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2118 return false;
2120 return true;
2123 /* Callback for walk_gimple_seq. */
2125 static tree
2126 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2127 struct walk_stmt_info *)
2129 gimple *stmt = gsi_stmt (*gsi_p);
2131 *handled_ops_p = true;
2132 switch (gimple_code (stmt))
2134 case GIMPLE_TRY:
2135 case GIMPLE_BIND:
2136 case GIMPLE_CATCH:
2137 case GIMPLE_EH_FILTER:
2138 case GIMPLE_TRANSACTION:
2139 /* Walk the sub-statements. */
2140 *handled_ops_p = false;
2141 break;
2143 /* Find a sequence of form:
2145 GIMPLE_LABEL
2146 [...]
2147 <may fallthru stmt>
2148 GIMPLE_LABEL
2150 and possibly warn. */
2151 case GIMPLE_LABEL:
2153 /* Found a label. Skip all immediately following labels. */
2154 while (!gsi_end_p (*gsi_p)
2155 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2156 gsi_next_nondebug (gsi_p);
2158 /* There might be no more statements. */
2159 if (gsi_end_p (*gsi_p))
2160 return integer_zero_node;
2162 /* Vector of labels that fall through. */
2163 auto_vec <struct label_entry> labels;
2164 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2166 /* There might be no more statements. */
2167 if (gsi_end_p (*gsi_p))
2168 return integer_zero_node;
2170 gimple *next = gsi_stmt (*gsi_p);
2171 tree label;
2172 /* If what follows is a label, then we may have a fallthrough. */
2173 if (gimple_code (next) == GIMPLE_LABEL
2174 && gimple_has_location (next)
2175 && (label = gimple_label_label (as_a <glabel *> (next)))
2176 && prev != NULL)
2178 struct label_entry *l;
2179 bool warned_p = false;
2180 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2181 /* Quiet. */;
2182 else if (gimple_code (prev) == GIMPLE_LABEL
2183 && (label = gimple_label_label (as_a <glabel *> (prev)))
2184 && (l = find_label_entry (&labels, label)))
2185 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2186 "this statement may fall through");
2187 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2188 /* Try to be clever and don't warn when the statement
2189 can't actually fall through. */
2190 && gimple_stmt_may_fallthru (prev)
2191 && gimple_has_location (prev))
2192 warned_p = warning_at (gimple_location (prev),
2193 OPT_Wimplicit_fallthrough_,
2194 "this statement may fall through");
2195 if (warned_p)
2196 inform (gimple_location (next), "here");
2198 /* Mark this label as processed so as to prevent multiple
2199 warnings in nested switches. */
2200 FALLTHROUGH_LABEL_P (label) = true;
2202 /* So that next warn_implicit_fallthrough_r will start looking for
2203 a new sequence starting with this label. */
2204 gsi_prev (gsi_p);
2207 break;
2208 default:
2209 break;
2211 return NULL_TREE;
2214 /* Warn when a switch case falls through. */
2216 static void
2217 maybe_warn_implicit_fallthrough (gimple_seq seq)
2219 if (!warn_implicit_fallthrough)
2220 return;
2222 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2223 if (!(lang_GNU_C ()
2224 || lang_GNU_CXX ()
2225 || lang_GNU_OBJC ()))
2226 return;
2228 struct walk_stmt_info wi;
2229 memset (&wi, 0, sizeof (wi));
2230 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2233 /* Callback for walk_gimple_seq. */
2235 static tree
2236 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2237 struct walk_stmt_info *)
2239 gimple *stmt = gsi_stmt (*gsi_p);
2241 *handled_ops_p = true;
2242 switch (gimple_code (stmt))
2244 case GIMPLE_TRY:
2245 case GIMPLE_BIND:
2246 case GIMPLE_CATCH:
2247 case GIMPLE_EH_FILTER:
2248 case GIMPLE_TRANSACTION:
2249 /* Walk the sub-statements. */
2250 *handled_ops_p = false;
2251 break;
2252 case GIMPLE_CALL:
2253 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2255 gsi_remove (gsi_p, true);
2256 if (gsi_end_p (*gsi_p))
2257 return integer_zero_node;
2259 bool found = false;
2260 location_t loc = gimple_location (stmt);
2262 gimple_stmt_iterator gsi2 = *gsi_p;
2263 stmt = gsi_stmt (gsi2);
2264 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2266 /* Go on until the artificial label. */
2267 tree goto_dest = gimple_goto_dest (stmt);
2268 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2270 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2271 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2272 == goto_dest)
2273 break;
2276 /* Not found? Stop. */
2277 if (gsi_end_p (gsi2))
2278 break;
2280 /* Look one past it. */
2281 gsi_next (&gsi2);
2284 /* We're looking for a case label or default label here. */
2285 while (!gsi_end_p (gsi2))
2287 stmt = gsi_stmt (gsi2);
2288 if (gimple_code (stmt) == GIMPLE_LABEL)
2290 tree label = gimple_label_label (as_a <glabel *> (stmt));
2291 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2293 found = true;
2294 break;
2297 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2299 else if (!is_gimple_debug (stmt))
2300 /* Anything else is not expected. */
2301 break;
2302 gsi_next (&gsi2);
2304 if (!found)
2305 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2306 "a case label or default label");
2308 break;
2309 default:
2310 break;
2312 return NULL_TREE;
2315 /* Expand all FALLTHROUGH () calls in SEQ. */
2317 static void
2318 expand_FALLTHROUGH (gimple_seq *seq_p)
2320 struct walk_stmt_info wi;
2321 memset (&wi, 0, sizeof (wi));
2322 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2326 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2327 branch to. */
2329 static enum gimplify_status
2330 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2332 tree switch_expr = *expr_p;
2333 gimple_seq switch_body_seq = NULL;
2334 enum gimplify_status ret;
2335 tree index_type = TREE_TYPE (switch_expr);
2336 if (index_type == NULL_TREE)
2337 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2339 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2340 fb_rvalue);
2341 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2342 return ret;
2344 if (SWITCH_BODY (switch_expr))
2346 vec<tree> labels;
2347 vec<tree> saved_labels;
2348 hash_set<tree> *saved_live_switch_vars = NULL;
2349 tree default_case = NULL_TREE;
2350 gswitch *switch_stmt;
2352 /* Save old labels, get new ones from body, then restore the old
2353 labels. Save all the things from the switch body to append after. */
2354 saved_labels = gimplify_ctxp->case_labels;
2355 gimplify_ctxp->case_labels.create (8);
2357 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2358 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2359 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2360 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2361 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2362 else
2363 gimplify_ctxp->live_switch_vars = NULL;
2365 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2366 gimplify_ctxp->in_switch_expr = true;
2368 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2370 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2371 maybe_warn_switch_unreachable (switch_body_seq);
2372 maybe_warn_implicit_fallthrough (switch_body_seq);
2373 /* Only do this for the outermost GIMPLE_SWITCH. */
2374 if (!gimplify_ctxp->in_switch_expr)
2375 expand_FALLTHROUGH (&switch_body_seq);
2377 labels = gimplify_ctxp->case_labels;
2378 gimplify_ctxp->case_labels = saved_labels;
2380 if (gimplify_ctxp->live_switch_vars)
2382 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2383 delete gimplify_ctxp->live_switch_vars;
2385 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2387 preprocess_case_label_vec_for_gimple (labels, index_type,
2388 &default_case);
2390 bool add_bind = false;
2391 if (!default_case)
2393 glabel *new_default;
2395 default_case
2396 = build_case_label (NULL_TREE, NULL_TREE,
2397 create_artificial_label (UNKNOWN_LOCATION));
2398 if (old_in_switch_expr)
2400 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2401 add_bind = true;
2403 new_default = gimple_build_label (CASE_LABEL (default_case));
2404 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2406 else if (old_in_switch_expr)
2408 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2409 if (last && gimple_code (last) == GIMPLE_LABEL)
2411 tree label = gimple_label_label (as_a <glabel *> (last));
2412 if (SWITCH_BREAK_LABEL_P (label))
2413 add_bind = true;
2417 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2418 default_case, labels);
2419 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2420 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2421 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2422 so that we can easily find the start and end of the switch
2423 statement. */
2424 if (add_bind)
2426 gimple_seq bind_body = NULL;
2427 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2428 gimple_seq_add_seq (&bind_body, switch_body_seq);
2429 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2430 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2431 gimplify_seq_add_stmt (pre_p, bind);
2433 else
2435 gimplify_seq_add_stmt (pre_p, switch_stmt);
2436 gimplify_seq_add_seq (pre_p, switch_body_seq);
2438 labels.release ();
2440 else
2441 gcc_unreachable ();
2443 return GS_ALL_DONE;
2446 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2448 static enum gimplify_status
2449 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2451 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2452 == current_function_decl);
2454 tree label = LABEL_EXPR_LABEL (*expr_p);
2455 glabel *label_stmt = gimple_build_label (label);
2456 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2457 gimplify_seq_add_stmt (pre_p, label_stmt);
2459 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2460 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2461 NOT_TAKEN));
2462 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2463 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2464 TAKEN));
2466 return GS_ALL_DONE;
2469 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2471 static enum gimplify_status
2472 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2474 struct gimplify_ctx *ctxp;
2475 glabel *label_stmt;
2477 /* Invalid programs can play Duff's Device type games with, for example,
2478 #pragma omp parallel. At least in the C front end, we don't
2479 detect such invalid branches until after gimplification, in the
2480 diagnose_omp_blocks pass. */
2481 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2482 if (ctxp->case_labels.exists ())
2483 break;
2485 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2486 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2487 ctxp->case_labels.safe_push (*expr_p);
2488 gimplify_seq_add_stmt (pre_p, label_stmt);
2490 return GS_ALL_DONE;
2493 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2494 if necessary. */
2496 tree
2497 build_and_jump (tree *label_p)
2499 if (label_p == NULL)
2500 /* If there's nowhere to jump, just fall through. */
2501 return NULL_TREE;
2503 if (*label_p == NULL_TREE)
2505 tree label = create_artificial_label (UNKNOWN_LOCATION);
2506 *label_p = label;
2509 return build1 (GOTO_EXPR, void_type_node, *label_p);
2512 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2513 This also involves building a label to jump to and communicating it to
2514 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2516 static enum gimplify_status
2517 gimplify_exit_expr (tree *expr_p)
2519 tree cond = TREE_OPERAND (*expr_p, 0);
2520 tree expr;
2522 expr = build_and_jump (&gimplify_ctxp->exit_label);
2523 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2524 *expr_p = expr;
2526 return GS_OK;
2529 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2530 different from its canonical type, wrap the whole thing inside a
2531 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2532 type.
2534 The canonical type of a COMPONENT_REF is the type of the field being
2535 referenced--unless the field is a bit-field which can be read directly
2536 in a smaller mode, in which case the canonical type is the
2537 sign-appropriate type corresponding to that mode. */
2539 static void
2540 canonicalize_component_ref (tree *expr_p)
2542 tree expr = *expr_p;
2543 tree type;
2545 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2547 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2548 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2549 else
2550 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2552 /* One could argue that all the stuff below is not necessary for
2553 the non-bitfield case and declare it a FE error if type
2554 adjustment would be needed. */
2555 if (TREE_TYPE (expr) != type)
2557 #ifdef ENABLE_TYPES_CHECKING
2558 tree old_type = TREE_TYPE (expr);
2559 #endif
2560 int type_quals;
2562 /* We need to preserve qualifiers and propagate them from
2563 operand 0. */
2564 type_quals = TYPE_QUALS (type)
2565 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2566 if (TYPE_QUALS (type) != type_quals)
2567 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2569 /* Set the type of the COMPONENT_REF to the underlying type. */
2570 TREE_TYPE (expr) = type;
2572 #ifdef ENABLE_TYPES_CHECKING
2573 /* It is now a FE error, if the conversion from the canonical
2574 type to the original expression type is not useless. */
2575 gcc_assert (useless_type_conversion_p (old_type, type));
2576 #endif
2580 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2581 to foo, embed that change in the ADDR_EXPR by converting
2582 T array[U];
2583 (T *)&array
2585 &array[L]
2586 where L is the lower bound. For simplicity, only do this for constant
2587 lower bound.
2588 The constraint is that the type of &array[L] is trivially convertible
2589 to T *. */
2591 static void
2592 canonicalize_addr_expr (tree *expr_p)
2594 tree expr = *expr_p;
2595 tree addr_expr = TREE_OPERAND (expr, 0);
2596 tree datype, ddatype, pddatype;
2598 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2599 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2600 || TREE_CODE (addr_expr) != ADDR_EXPR)
2601 return;
2603 /* The addr_expr type should be a pointer to an array. */
2604 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2605 if (TREE_CODE (datype) != ARRAY_TYPE)
2606 return;
2608 /* The pointer to element type shall be trivially convertible to
2609 the expression pointer type. */
2610 ddatype = TREE_TYPE (datype);
2611 pddatype = build_pointer_type (ddatype);
2612 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2613 pddatype))
2614 return;
2616 /* The lower bound and element sizes must be constant. */
2617 if (!TYPE_SIZE_UNIT (ddatype)
2618 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2619 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2620 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2621 return;
2623 /* All checks succeeded. Build a new node to merge the cast. */
2624 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2625 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2626 NULL_TREE, NULL_TREE);
2627 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2629 /* We can have stripped a required restrict qualifier above. */
2630 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2631 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2634 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2635 underneath as appropriate. */
2637 static enum gimplify_status
2638 gimplify_conversion (tree *expr_p)
2640 location_t loc = EXPR_LOCATION (*expr_p);
2641 gcc_assert (CONVERT_EXPR_P (*expr_p));
2643 /* Then strip away all but the outermost conversion. */
2644 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2646 /* And remove the outermost conversion if it's useless. */
2647 if (tree_ssa_useless_type_conversion (*expr_p))
2648 *expr_p = TREE_OPERAND (*expr_p, 0);
2650 /* If we still have a conversion at the toplevel,
2651 then canonicalize some constructs. */
2652 if (CONVERT_EXPR_P (*expr_p))
2654 tree sub = TREE_OPERAND (*expr_p, 0);
2656 /* If a NOP conversion is changing the type of a COMPONENT_REF
2657 expression, then canonicalize its type now in order to expose more
2658 redundant conversions. */
2659 if (TREE_CODE (sub) == COMPONENT_REF)
2660 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2662 /* If a NOP conversion is changing a pointer to array of foo
2663 to a pointer to foo, embed that change in the ADDR_EXPR. */
2664 else if (TREE_CODE (sub) == ADDR_EXPR)
2665 canonicalize_addr_expr (expr_p);
2668 /* If we have a conversion to a non-register type force the
2669 use of a VIEW_CONVERT_EXPR instead. */
2670 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2671 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2672 TREE_OPERAND (*expr_p, 0));
2674 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2675 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2676 TREE_SET_CODE (*expr_p, NOP_EXPR);
2678 return GS_OK;
2681 /* Nonlocal VLAs seen in the current function. */
2682 static hash_set<tree> *nonlocal_vlas;
2684 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2685 static tree nonlocal_vla_vars;
2687 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2688 DECL_VALUE_EXPR, and it's worth re-examining things. */
2690 static enum gimplify_status
2691 gimplify_var_or_parm_decl (tree *expr_p)
2693 tree decl = *expr_p;
2695 /* ??? If this is a local variable, and it has not been seen in any
2696 outer BIND_EXPR, then it's probably the result of a duplicate
2697 declaration, for which we've already issued an error. It would
2698 be really nice if the front end wouldn't leak these at all.
2699 Currently the only known culprit is C++ destructors, as seen
2700 in g++.old-deja/g++.jason/binding.C. */
2701 if (VAR_P (decl)
2702 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2703 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2704 && decl_function_context (decl) == current_function_decl)
2706 gcc_assert (seen_error ());
2707 return GS_ERROR;
2710 /* When within an OMP context, notice uses of variables. */
2711 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2712 return GS_ALL_DONE;
2714 /* If the decl is an alias for another expression, substitute it now. */
2715 if (DECL_HAS_VALUE_EXPR_P (decl))
2717 tree value_expr = DECL_VALUE_EXPR (decl);
2719 /* For referenced nonlocal VLAs add a decl for debugging purposes
2720 to the current function. */
2721 if (VAR_P (decl)
2722 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2723 && nonlocal_vlas != NULL
2724 && TREE_CODE (value_expr) == INDIRECT_REF
2725 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2726 && decl_function_context (decl) != current_function_decl)
2728 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2729 while (ctx
2730 && (ctx->region_type == ORT_WORKSHARE
2731 || ctx->region_type == ORT_SIMD
2732 || ctx->region_type == ORT_ACC))
2733 ctx = ctx->outer_context;
2734 if (!ctx && !nonlocal_vlas->add (decl))
2736 tree copy = copy_node (decl);
2738 lang_hooks.dup_lang_specific_decl (copy);
2739 SET_DECL_RTL (copy, 0);
2740 TREE_USED (copy) = 1;
2741 DECL_CHAIN (copy) = nonlocal_vla_vars;
2742 nonlocal_vla_vars = copy;
2743 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2744 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2748 *expr_p = unshare_expr (value_expr);
2749 return GS_OK;
2752 return GS_ALL_DONE;
2755 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2757 static void
2758 recalculate_side_effects (tree t)
2760 enum tree_code code = TREE_CODE (t);
2761 int len = TREE_OPERAND_LENGTH (t);
2762 int i;
2764 switch (TREE_CODE_CLASS (code))
2766 case tcc_expression:
2767 switch (code)
2769 case INIT_EXPR:
2770 case MODIFY_EXPR:
2771 case VA_ARG_EXPR:
2772 case PREDECREMENT_EXPR:
2773 case PREINCREMENT_EXPR:
2774 case POSTDECREMENT_EXPR:
2775 case POSTINCREMENT_EXPR:
2776 /* All of these have side-effects, no matter what their
2777 operands are. */
2778 return;
2780 default:
2781 break;
2783 /* Fall through. */
2785 case tcc_comparison: /* a comparison expression */
2786 case tcc_unary: /* a unary arithmetic expression */
2787 case tcc_binary: /* a binary arithmetic expression */
2788 case tcc_reference: /* a reference */
2789 case tcc_vl_exp: /* a function call */
2790 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2791 for (i = 0; i < len; ++i)
2793 tree op = TREE_OPERAND (t, i);
2794 if (op && TREE_SIDE_EFFECTS (op))
2795 TREE_SIDE_EFFECTS (t) = 1;
2797 break;
2799 case tcc_constant:
2800 /* No side-effects. */
2801 return;
2803 default:
2804 gcc_unreachable ();
2808 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2809 node *EXPR_P.
2811 compound_lval
2812 : min_lval '[' val ']'
2813 | min_lval '.' ID
2814 | compound_lval '[' val ']'
2815 | compound_lval '.' ID
2817 This is not part of the original SIMPLE definition, which separates
2818 array and member references, but it seems reasonable to handle them
2819 together. Also, this way we don't run into problems with union
2820 aliasing; gcc requires that for accesses through a union to alias, the
2821 union reference must be explicit, which was not always the case when we
2822 were splitting up array and member refs.
2824 PRE_P points to the sequence where side effects that must happen before
2825 *EXPR_P should be stored.
2827 POST_P points to the sequence where side effects that must happen after
2828 *EXPR_P should be stored. */
2830 static enum gimplify_status
2831 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2832 fallback_t fallback)
2834 tree *p;
2835 enum gimplify_status ret = GS_ALL_DONE, tret;
2836 int i;
2837 location_t loc = EXPR_LOCATION (*expr_p);
2838 tree expr = *expr_p;
2840 /* Create a stack of the subexpressions so later we can walk them in
2841 order from inner to outer. */
2842 auto_vec<tree, 10> expr_stack;
2844 /* We can handle anything that get_inner_reference can deal with. */
2845 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2847 restart:
2848 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2849 if (TREE_CODE (*p) == INDIRECT_REF)
2850 *p = fold_indirect_ref_loc (loc, *p);
2852 if (handled_component_p (*p))
2854 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2855 additional COMPONENT_REFs. */
2856 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2857 && gimplify_var_or_parm_decl (p) == GS_OK)
2858 goto restart;
2859 else
2860 break;
2862 expr_stack.safe_push (*p);
2865 gcc_assert (expr_stack.length ());
2867 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2868 walked through and P points to the innermost expression.
2870 Java requires that we elaborated nodes in source order. That
2871 means we must gimplify the inner expression followed by each of
2872 the indices, in order. But we can't gimplify the inner
2873 expression until we deal with any variable bounds, sizes, or
2874 positions in order to deal with PLACEHOLDER_EXPRs.
2876 So we do this in three steps. First we deal with the annotations
2877 for any variables in the components, then we gimplify the base,
2878 then we gimplify any indices, from left to right. */
2879 for (i = expr_stack.length () - 1; i >= 0; i--)
2881 tree t = expr_stack[i];
2883 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2885 /* Gimplify the low bound and element type size and put them into
2886 the ARRAY_REF. If these values are set, they have already been
2887 gimplified. */
2888 if (TREE_OPERAND (t, 2) == NULL_TREE)
2890 tree low = unshare_expr (array_ref_low_bound (t));
2891 if (!is_gimple_min_invariant (low))
2893 TREE_OPERAND (t, 2) = low;
2894 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2895 post_p, is_gimple_reg,
2896 fb_rvalue);
2897 ret = MIN (ret, tret);
2900 else
2902 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2903 is_gimple_reg, fb_rvalue);
2904 ret = MIN (ret, tret);
2907 if (TREE_OPERAND (t, 3) == NULL_TREE)
2909 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2910 tree elmt_size = unshare_expr (array_ref_element_size (t));
2911 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2913 /* Divide the element size by the alignment of the element
2914 type (above). */
2915 elmt_size
2916 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2918 if (!is_gimple_min_invariant (elmt_size))
2920 TREE_OPERAND (t, 3) = elmt_size;
2921 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2922 post_p, is_gimple_reg,
2923 fb_rvalue);
2924 ret = MIN (ret, tret);
2927 else
2929 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2930 is_gimple_reg, fb_rvalue);
2931 ret = MIN (ret, tret);
2934 else if (TREE_CODE (t) == COMPONENT_REF)
2936 /* Set the field offset into T and gimplify it. */
2937 if (TREE_OPERAND (t, 2) == NULL_TREE)
2939 tree offset = unshare_expr (component_ref_field_offset (t));
2940 tree field = TREE_OPERAND (t, 1);
2941 tree factor
2942 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2944 /* Divide the offset by its alignment. */
2945 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2947 if (!is_gimple_min_invariant (offset))
2949 TREE_OPERAND (t, 2) = offset;
2950 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2951 post_p, is_gimple_reg,
2952 fb_rvalue);
2953 ret = MIN (ret, tret);
2956 else
2958 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2959 is_gimple_reg, fb_rvalue);
2960 ret = MIN (ret, tret);
2965 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2966 so as to match the min_lval predicate. Failure to do so may result
2967 in the creation of large aggregate temporaries. */
2968 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2969 fallback | fb_lvalue);
2970 ret = MIN (ret, tret);
2972 /* And finally, the indices and operands of ARRAY_REF. During this
2973 loop we also remove any useless conversions. */
2974 for (; expr_stack.length () > 0; )
2976 tree t = expr_stack.pop ();
2978 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2980 /* Gimplify the dimension. */
2981 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2983 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2984 is_gimple_val, fb_rvalue);
2985 ret = MIN (ret, tret);
2989 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2991 /* The innermost expression P may have originally had
2992 TREE_SIDE_EFFECTS set which would have caused all the outer
2993 expressions in *EXPR_P leading to P to also have had
2994 TREE_SIDE_EFFECTS set. */
2995 recalculate_side_effects (t);
2998 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2999 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3001 canonicalize_component_ref (expr_p);
3004 expr_stack.release ();
3006 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3008 return ret;
3011 /* Gimplify the self modifying expression pointed to by EXPR_P
3012 (++, --, +=, -=).
3014 PRE_P points to the list where side effects that must happen before
3015 *EXPR_P should be stored.
3017 POST_P points to the list where side effects that must happen after
3018 *EXPR_P should be stored.
3020 WANT_VALUE is nonzero iff we want to use the value of this expression
3021 in another expression.
3023 ARITH_TYPE is the type the computation should be performed in. */
3025 enum gimplify_status
3026 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3027 bool want_value, tree arith_type)
3029 enum tree_code code;
3030 tree lhs, lvalue, rhs, t1;
3031 gimple_seq post = NULL, *orig_post_p = post_p;
3032 bool postfix;
3033 enum tree_code arith_code;
3034 enum gimplify_status ret;
3035 location_t loc = EXPR_LOCATION (*expr_p);
3037 code = TREE_CODE (*expr_p);
3039 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3040 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3042 /* Prefix or postfix? */
3043 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3044 /* Faster to treat as prefix if result is not used. */
3045 postfix = want_value;
3046 else
3047 postfix = false;
3049 /* For postfix, make sure the inner expression's post side effects
3050 are executed after side effects from this expression. */
3051 if (postfix)
3052 post_p = &post;
3054 /* Add or subtract? */
3055 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3056 arith_code = PLUS_EXPR;
3057 else
3058 arith_code = MINUS_EXPR;
3060 /* Gimplify the LHS into a GIMPLE lvalue. */
3061 lvalue = TREE_OPERAND (*expr_p, 0);
3062 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3063 if (ret == GS_ERROR)
3064 return ret;
3066 /* Extract the operands to the arithmetic operation. */
3067 lhs = lvalue;
3068 rhs = TREE_OPERAND (*expr_p, 1);
3070 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3071 that as the result value and in the postqueue operation. */
3072 if (postfix)
3074 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3075 if (ret == GS_ERROR)
3076 return ret;
3078 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3081 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3082 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3084 rhs = convert_to_ptrofftype_loc (loc, rhs);
3085 if (arith_code == MINUS_EXPR)
3086 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3087 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3089 else
3090 t1 = fold_convert (TREE_TYPE (*expr_p),
3091 fold_build2 (arith_code, arith_type,
3092 fold_convert (arith_type, lhs),
3093 fold_convert (arith_type, rhs)));
3095 if (postfix)
3097 gimplify_assign (lvalue, t1, pre_p);
3098 gimplify_seq_add_seq (orig_post_p, post);
3099 *expr_p = lhs;
3100 return GS_ALL_DONE;
3102 else
3104 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3105 return GS_OK;
3109 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3111 static void
3112 maybe_with_size_expr (tree *expr_p)
3114 tree expr = *expr_p;
3115 tree type = TREE_TYPE (expr);
3116 tree size;
3118 /* If we've already wrapped this or the type is error_mark_node, we can't do
3119 anything. */
3120 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3121 || type == error_mark_node)
3122 return;
3124 /* If the size isn't known or is a constant, we have nothing to do. */
3125 size = TYPE_SIZE_UNIT (type);
3126 if (!size || poly_int_tree_p (size))
3127 return;
3129 /* Otherwise, make a WITH_SIZE_EXPR. */
3130 size = unshare_expr (size);
3131 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3132 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3135 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3136 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3137 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3138 gimplified to an SSA name. */
3140 enum gimplify_status
3141 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3142 bool allow_ssa)
3144 bool (*test) (tree);
3145 fallback_t fb;
3147 /* In general, we allow lvalues for function arguments to avoid
3148 extra overhead of copying large aggregates out of even larger
3149 aggregates into temporaries only to copy the temporaries to
3150 the argument list. Make optimizers happy by pulling out to
3151 temporaries those types that fit in registers. */
3152 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3153 test = is_gimple_val, fb = fb_rvalue;
3154 else
3156 test = is_gimple_lvalue, fb = fb_either;
3157 /* Also strip a TARGET_EXPR that would force an extra copy. */
3158 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3160 tree init = TARGET_EXPR_INITIAL (*arg_p);
3161 if (init
3162 && !VOID_TYPE_P (TREE_TYPE (init)))
3163 *arg_p = init;
3167 /* If this is a variable sized type, we must remember the size. */
3168 maybe_with_size_expr (arg_p);
3170 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3171 /* Make sure arguments have the same location as the function call
3172 itself. */
3173 protected_set_expr_location (*arg_p, call_location);
3175 /* There is a sequence point before a function call. Side effects in
3176 the argument list must occur before the actual call. So, when
3177 gimplifying arguments, force gimplify_expr to use an internal
3178 post queue which is then appended to the end of PRE_P. */
3179 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3182 /* Don't fold inside offloading or taskreg regions: it can break code by
3183 adding decl references that weren't in the source. We'll do it during
3184 omplower pass instead. */
3186 static bool
3187 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3189 struct gimplify_omp_ctx *ctx;
3190 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3191 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3192 return false;
3193 return fold_stmt (gsi);
3196 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3197 WANT_VALUE is true if the result of the call is desired. */
3199 static enum gimplify_status
3200 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3202 tree fndecl, parms, p, fnptrtype;
3203 enum gimplify_status ret;
3204 int i, nargs;
3205 gcall *call;
3206 bool builtin_va_start_p = false;
3207 location_t loc = EXPR_LOCATION (*expr_p);
3209 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3211 /* For reliable diagnostics during inlining, it is necessary that
3212 every call_expr be annotated with file and line. */
3213 if (! EXPR_HAS_LOCATION (*expr_p))
3214 SET_EXPR_LOCATION (*expr_p, input_location);
3216 /* Gimplify internal functions created in the FEs. */
3217 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3219 if (want_value)
3220 return GS_ALL_DONE;
3222 nargs = call_expr_nargs (*expr_p);
3223 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3224 auto_vec<tree> vargs (nargs);
3226 for (i = 0; i < nargs; i++)
3228 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3229 EXPR_LOCATION (*expr_p));
3230 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3233 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3234 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3235 gimplify_seq_add_stmt (pre_p, call);
3236 return GS_ALL_DONE;
3239 /* This may be a call to a builtin function.
3241 Builtin function calls may be transformed into different
3242 (and more efficient) builtin function calls under certain
3243 circumstances. Unfortunately, gimplification can muck things
3244 up enough that the builtin expanders are not aware that certain
3245 transformations are still valid.
3247 So we attempt transformation/gimplification of the call before
3248 we gimplify the CALL_EXPR. At this time we do not manage to
3249 transform all calls in the same manner as the expanders do, but
3250 we do transform most of them. */
3251 fndecl = get_callee_fndecl (*expr_p);
3252 if (fndecl
3253 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3254 switch (DECL_FUNCTION_CODE (fndecl))
3256 CASE_BUILT_IN_ALLOCA:
3257 /* If the call has been built for a variable-sized object, then we
3258 want to restore the stack level when the enclosing BIND_EXPR is
3259 exited to reclaim the allocated space; otherwise, we precisely
3260 need to do the opposite and preserve the latest stack level. */
3261 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3262 gimplify_ctxp->save_stack = true;
3263 else
3264 gimplify_ctxp->keep_stack = true;
3265 break;
3267 case BUILT_IN_VA_START:
3269 builtin_va_start_p = TRUE;
3270 if (call_expr_nargs (*expr_p) < 2)
3272 error ("too few arguments to function %<va_start%>");
3273 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3274 return GS_OK;
3277 if (fold_builtin_next_arg (*expr_p, true))
3279 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3280 return GS_OK;
3282 break;
3285 default:
3288 if (fndecl && DECL_BUILT_IN (fndecl))
3290 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3291 if (new_tree && new_tree != *expr_p)
3293 /* There was a transformation of this call which computes the
3294 same value, but in a more efficient way. Return and try
3295 again. */
3296 *expr_p = new_tree;
3297 return GS_OK;
3301 /* Remember the original function pointer type. */
3302 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3304 /* There is a sequence point before the call, so any side effects in
3305 the calling expression must occur before the actual call. Force
3306 gimplify_expr to use an internal post queue. */
3307 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3308 is_gimple_call_addr, fb_rvalue);
3310 nargs = call_expr_nargs (*expr_p);
3312 /* Get argument types for verification. */
3313 fndecl = get_callee_fndecl (*expr_p);
3314 parms = NULL_TREE;
3315 if (fndecl)
3316 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3317 else
3318 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3320 if (fndecl && DECL_ARGUMENTS (fndecl))
3321 p = DECL_ARGUMENTS (fndecl);
3322 else if (parms)
3323 p = parms;
3324 else
3325 p = NULL_TREE;
3326 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3329 /* If the last argument is __builtin_va_arg_pack () and it is not
3330 passed as a named argument, decrease the number of CALL_EXPR
3331 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3332 if (!p
3333 && i < nargs
3334 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3336 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3337 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3339 if (last_arg_fndecl
3340 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3341 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3342 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3344 tree call = *expr_p;
3346 --nargs;
3347 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3348 CALL_EXPR_FN (call),
3349 nargs, CALL_EXPR_ARGP (call));
3351 /* Copy all CALL_EXPR flags, location and block, except
3352 CALL_EXPR_VA_ARG_PACK flag. */
3353 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3354 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3355 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3356 = CALL_EXPR_RETURN_SLOT_OPT (call);
3357 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3358 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3360 /* Set CALL_EXPR_VA_ARG_PACK. */
3361 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3365 /* If the call returns twice then after building the CFG the call
3366 argument computations will no longer dominate the call because
3367 we add an abnormal incoming edge to the call. So do not use SSA
3368 vars there. */
3369 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3371 /* Gimplify the function arguments. */
3372 if (nargs > 0)
3374 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3375 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3376 PUSH_ARGS_REVERSED ? i-- : i++)
3378 enum gimplify_status t;
3380 /* Avoid gimplifying the second argument to va_start, which needs to
3381 be the plain PARM_DECL. */
3382 if ((i != 1) || !builtin_va_start_p)
3384 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3385 EXPR_LOCATION (*expr_p), ! returns_twice);
3387 if (t == GS_ERROR)
3388 ret = GS_ERROR;
3393 /* Gimplify the static chain. */
3394 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3396 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3397 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3398 else
3400 enum gimplify_status t;
3401 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3402 EXPR_LOCATION (*expr_p), ! returns_twice);
3403 if (t == GS_ERROR)
3404 ret = GS_ERROR;
3408 /* Verify the function result. */
3409 if (want_value && fndecl
3410 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3412 error_at (loc, "using result of function returning %<void%>");
3413 ret = GS_ERROR;
3416 /* Try this again in case gimplification exposed something. */
3417 if (ret != GS_ERROR)
3419 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3421 if (new_tree && new_tree != *expr_p)
3423 /* There was a transformation of this call which computes the
3424 same value, but in a more efficient way. Return and try
3425 again. */
3426 *expr_p = new_tree;
3427 return GS_OK;
3430 else
3432 *expr_p = error_mark_node;
3433 return GS_ERROR;
3436 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3437 decl. This allows us to eliminate redundant or useless
3438 calls to "const" functions. */
3439 if (TREE_CODE (*expr_p) == CALL_EXPR)
3441 int flags = call_expr_flags (*expr_p);
3442 if (flags & (ECF_CONST | ECF_PURE)
3443 /* An infinite loop is considered a side effect. */
3444 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3445 TREE_SIDE_EFFECTS (*expr_p) = 0;
3448 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3449 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3450 form and delegate the creation of a GIMPLE_CALL to
3451 gimplify_modify_expr. This is always possible because when
3452 WANT_VALUE is true, the caller wants the result of this call into
3453 a temporary, which means that we will emit an INIT_EXPR in
3454 internal_get_tmp_var which will then be handled by
3455 gimplify_modify_expr. */
3456 if (!want_value)
3458 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3459 have to do is replicate it as a GIMPLE_CALL tuple. */
3460 gimple_stmt_iterator gsi;
3461 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3462 notice_special_calls (call);
3463 gimplify_seq_add_stmt (pre_p, call);
3464 gsi = gsi_last (*pre_p);
3465 maybe_fold_stmt (&gsi);
3466 *expr_p = NULL_TREE;
3468 else
3469 /* Remember the original function type. */
3470 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3471 CALL_EXPR_FN (*expr_p));
3473 return ret;
3476 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3477 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3479 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3480 condition is true or false, respectively. If null, we should generate
3481 our own to skip over the evaluation of this specific expression.
3483 LOCUS is the source location of the COND_EXPR.
3485 This function is the tree equivalent of do_jump.
3487 shortcut_cond_r should only be called by shortcut_cond_expr. */
3489 static tree
3490 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3491 location_t locus)
3493 tree local_label = NULL_TREE;
3494 tree t, expr = NULL;
3496 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3497 retain the shortcut semantics. Just insert the gotos here;
3498 shortcut_cond_expr will append the real blocks later. */
3499 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3501 location_t new_locus;
3503 /* Turn if (a && b) into
3505 if (a); else goto no;
3506 if (b) goto yes; else goto no;
3507 (no:) */
3509 if (false_label_p == NULL)
3510 false_label_p = &local_label;
3512 /* Keep the original source location on the first 'if'. */
3513 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3514 append_to_statement_list (t, &expr);
3516 /* Set the source location of the && on the second 'if'. */
3517 new_locus = rexpr_location (pred, locus);
3518 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3519 new_locus);
3520 append_to_statement_list (t, &expr);
3522 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3524 location_t new_locus;
3526 /* Turn if (a || b) into
3528 if (a) goto yes;
3529 if (b) goto yes; else goto no;
3530 (yes:) */
3532 if (true_label_p == NULL)
3533 true_label_p = &local_label;
3535 /* Keep the original source location on the first 'if'. */
3536 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3537 append_to_statement_list (t, &expr);
3539 /* Set the source location of the || on the second 'if'. */
3540 new_locus = rexpr_location (pred, locus);
3541 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3542 new_locus);
3543 append_to_statement_list (t, &expr);
3545 else if (TREE_CODE (pred) == COND_EXPR
3546 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3547 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3549 location_t new_locus;
3551 /* As long as we're messing with gotos, turn if (a ? b : c) into
3552 if (a)
3553 if (b) goto yes; else goto no;
3554 else
3555 if (c) goto yes; else goto no;
3557 Don't do this if one of the arms has void type, which can happen
3558 in C++ when the arm is throw. */
3560 /* Keep the original source location on the first 'if'. Set the source
3561 location of the ? on the second 'if'. */
3562 new_locus = rexpr_location (pred, locus);
3563 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3564 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3565 false_label_p, locus),
3566 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3567 false_label_p, new_locus));
3569 else
3571 expr = build3 (COND_EXPR, void_type_node, pred,
3572 build_and_jump (true_label_p),
3573 build_and_jump (false_label_p));
3574 SET_EXPR_LOCATION (expr, locus);
3577 if (local_label)
3579 t = build1 (LABEL_EXPR, void_type_node, local_label);
3580 append_to_statement_list (t, &expr);
3583 return expr;
3586 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3587 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3588 statement, if it is the last one. Otherwise, return NULL. */
3590 static tree
3591 find_goto (tree expr)
3593 if (!expr)
3594 return NULL_TREE;
3596 if (TREE_CODE (expr) == GOTO_EXPR)
3597 return expr;
3599 if (TREE_CODE (expr) != STATEMENT_LIST)
3600 return NULL_TREE;
3602 tree_stmt_iterator i = tsi_start (expr);
3604 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3605 tsi_next (&i);
3607 if (!tsi_one_before_end_p (i))
3608 return NULL_TREE;
3610 return find_goto (tsi_stmt (i));
3613 /* Same as find_goto, except that it returns NULL if the destination
3614 is not a LABEL_DECL. */
3616 static inline tree
3617 find_goto_label (tree expr)
3619 tree dest = find_goto (expr);
3620 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3621 return dest;
3622 return NULL_TREE;
3625 /* Given a conditional expression EXPR with short-circuit boolean
3626 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3627 predicate apart into the equivalent sequence of conditionals. */
3629 static tree
3630 shortcut_cond_expr (tree expr)
3632 tree pred = TREE_OPERAND (expr, 0);
3633 tree then_ = TREE_OPERAND (expr, 1);
3634 tree else_ = TREE_OPERAND (expr, 2);
3635 tree true_label, false_label, end_label, t;
3636 tree *true_label_p;
3637 tree *false_label_p;
3638 bool emit_end, emit_false, jump_over_else;
3639 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3640 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3642 /* First do simple transformations. */
3643 if (!else_se)
3645 /* If there is no 'else', turn
3646 if (a && b) then c
3647 into
3648 if (a) if (b) then c. */
3649 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3651 /* Keep the original source location on the first 'if'. */
3652 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3653 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3654 /* Set the source location of the && on the second 'if'. */
3655 if (rexpr_has_location (pred))
3656 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3657 then_ = shortcut_cond_expr (expr);
3658 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3659 pred = TREE_OPERAND (pred, 0);
3660 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3661 SET_EXPR_LOCATION (expr, locus);
3665 if (!then_se)
3667 /* If there is no 'then', turn
3668 if (a || b); else d
3669 into
3670 if (a); else if (b); else d. */
3671 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3673 /* Keep the original source location on the first 'if'. */
3674 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3675 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3676 /* Set the source location of the || on the second 'if'. */
3677 if (rexpr_has_location (pred))
3678 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3679 else_ = shortcut_cond_expr (expr);
3680 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3681 pred = TREE_OPERAND (pred, 0);
3682 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3683 SET_EXPR_LOCATION (expr, locus);
3687 /* If we're done, great. */
3688 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3689 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3690 return expr;
3692 /* Otherwise we need to mess with gotos. Change
3693 if (a) c; else d;
3695 if (a); else goto no;
3696 c; goto end;
3697 no: d; end:
3698 and recursively gimplify the condition. */
3700 true_label = false_label = end_label = NULL_TREE;
3702 /* If our arms just jump somewhere, hijack those labels so we don't
3703 generate jumps to jumps. */
3705 if (tree then_goto = find_goto_label (then_))
3707 true_label = GOTO_DESTINATION (then_goto);
3708 then_ = NULL;
3709 then_se = false;
3712 if (tree else_goto = find_goto_label (else_))
3714 false_label = GOTO_DESTINATION (else_goto);
3715 else_ = NULL;
3716 else_se = false;
3719 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3720 if (true_label)
3721 true_label_p = &true_label;
3722 else
3723 true_label_p = NULL;
3725 /* The 'else' branch also needs a label if it contains interesting code. */
3726 if (false_label || else_se)
3727 false_label_p = &false_label;
3728 else
3729 false_label_p = NULL;
3731 /* If there was nothing else in our arms, just forward the label(s). */
3732 if (!then_se && !else_se)
3733 return shortcut_cond_r (pred, true_label_p, false_label_p,
3734 EXPR_LOC_OR_LOC (expr, input_location));
3736 /* If our last subexpression already has a terminal label, reuse it. */
3737 if (else_se)
3738 t = expr_last (else_);
3739 else if (then_se)
3740 t = expr_last (then_);
3741 else
3742 t = NULL;
3743 if (t && TREE_CODE (t) == LABEL_EXPR)
3744 end_label = LABEL_EXPR_LABEL (t);
3746 /* If we don't care about jumping to the 'else' branch, jump to the end
3747 if the condition is false. */
3748 if (!false_label_p)
3749 false_label_p = &end_label;
3751 /* We only want to emit these labels if we aren't hijacking them. */
3752 emit_end = (end_label == NULL_TREE);
3753 emit_false = (false_label == NULL_TREE);
3755 /* We only emit the jump over the else clause if we have to--if the
3756 then clause may fall through. Otherwise we can wind up with a
3757 useless jump and a useless label at the end of gimplified code,
3758 which will cause us to think that this conditional as a whole
3759 falls through even if it doesn't. If we then inline a function
3760 which ends with such a condition, that can cause us to issue an
3761 inappropriate warning about control reaching the end of a
3762 non-void function. */
3763 jump_over_else = block_may_fallthru (then_);
3765 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3766 EXPR_LOC_OR_LOC (expr, input_location));
3768 expr = NULL;
3769 append_to_statement_list (pred, &expr);
3771 append_to_statement_list (then_, &expr);
3772 if (else_se)
3774 if (jump_over_else)
3776 tree last = expr_last (expr);
3777 t = build_and_jump (&end_label);
3778 if (rexpr_has_location (last))
3779 SET_EXPR_LOCATION (t, rexpr_location (last));
3780 append_to_statement_list (t, &expr);
3782 if (emit_false)
3784 t = build1 (LABEL_EXPR, void_type_node, false_label);
3785 append_to_statement_list (t, &expr);
3787 append_to_statement_list (else_, &expr);
3789 if (emit_end && end_label)
3791 t = build1 (LABEL_EXPR, void_type_node, end_label);
3792 append_to_statement_list (t, &expr);
3795 return expr;
3798 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3800 tree
3801 gimple_boolify (tree expr)
3803 tree type = TREE_TYPE (expr);
3804 location_t loc = EXPR_LOCATION (expr);
3806 if (TREE_CODE (expr) == NE_EXPR
3807 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3808 && integer_zerop (TREE_OPERAND (expr, 1)))
3810 tree call = TREE_OPERAND (expr, 0);
3811 tree fn = get_callee_fndecl (call);
3813 /* For __builtin_expect ((long) (x), y) recurse into x as well
3814 if x is truth_value_p. */
3815 if (fn
3816 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3817 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3818 && call_expr_nargs (call) == 2)
3820 tree arg = CALL_EXPR_ARG (call, 0);
3821 if (arg)
3823 if (TREE_CODE (arg) == NOP_EXPR
3824 && TREE_TYPE (arg) == TREE_TYPE (call))
3825 arg = TREE_OPERAND (arg, 0);
3826 if (truth_value_p (TREE_CODE (arg)))
3828 arg = gimple_boolify (arg);
3829 CALL_EXPR_ARG (call, 0)
3830 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3836 switch (TREE_CODE (expr))
3838 case TRUTH_AND_EXPR:
3839 case TRUTH_OR_EXPR:
3840 case TRUTH_XOR_EXPR:
3841 case TRUTH_ANDIF_EXPR:
3842 case TRUTH_ORIF_EXPR:
3843 /* Also boolify the arguments of truth exprs. */
3844 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3845 /* FALLTHRU */
3847 case TRUTH_NOT_EXPR:
3848 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3850 /* These expressions always produce boolean results. */
3851 if (TREE_CODE (type) != BOOLEAN_TYPE)
3852 TREE_TYPE (expr) = boolean_type_node;
3853 return expr;
3855 case ANNOTATE_EXPR:
3856 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3858 case annot_expr_ivdep_kind:
3859 case annot_expr_unroll_kind:
3860 case annot_expr_no_vector_kind:
3861 case annot_expr_vector_kind:
3862 case annot_expr_parallel_kind:
3863 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3864 if (TREE_CODE (type) != BOOLEAN_TYPE)
3865 TREE_TYPE (expr) = boolean_type_node;
3866 return expr;
3867 default:
3868 gcc_unreachable ();
3871 default:
3872 if (COMPARISON_CLASS_P (expr))
3874 /* There expressions always prduce boolean results. */
3875 if (TREE_CODE (type) != BOOLEAN_TYPE)
3876 TREE_TYPE (expr) = boolean_type_node;
3877 return expr;
3879 /* Other expressions that get here must have boolean values, but
3880 might need to be converted to the appropriate mode. */
3881 if (TREE_CODE (type) == BOOLEAN_TYPE)
3882 return expr;
3883 return fold_convert_loc (loc, boolean_type_node, expr);
3887 /* Given a conditional expression *EXPR_P without side effects, gimplify
3888 its operands. New statements are inserted to PRE_P. */
3890 static enum gimplify_status
3891 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3893 tree expr = *expr_p, cond;
3894 enum gimplify_status ret, tret;
3895 enum tree_code code;
3897 cond = gimple_boolify (COND_EXPR_COND (expr));
3899 /* We need to handle && and || specially, as their gimplification
3900 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3901 code = TREE_CODE (cond);
3902 if (code == TRUTH_ANDIF_EXPR)
3903 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3904 else if (code == TRUTH_ORIF_EXPR)
3905 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3906 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3907 COND_EXPR_COND (*expr_p) = cond;
3909 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3910 is_gimple_val, fb_rvalue);
3911 ret = MIN (ret, tret);
3912 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3913 is_gimple_val, fb_rvalue);
3915 return MIN (ret, tret);
3918 /* Return true if evaluating EXPR could trap.
3919 EXPR is GENERIC, while tree_could_trap_p can be called
3920 only on GIMPLE. */
3922 static bool
3923 generic_expr_could_trap_p (tree expr)
3925 unsigned i, n;
3927 if (!expr || is_gimple_val (expr))
3928 return false;
3930 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3931 return true;
3933 n = TREE_OPERAND_LENGTH (expr);
3934 for (i = 0; i < n; i++)
3935 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3936 return true;
3938 return false;
3941 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3942 into
3944 if (p) if (p)
3945 t1 = a; a;
3946 else or else
3947 t1 = b; b;
3950 The second form is used when *EXPR_P is of type void.
3952 PRE_P points to the list where side effects that must happen before
3953 *EXPR_P should be stored. */
3955 static enum gimplify_status
3956 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3958 tree expr = *expr_p;
3959 tree type = TREE_TYPE (expr);
3960 location_t loc = EXPR_LOCATION (expr);
3961 tree tmp, arm1, arm2;
3962 enum gimplify_status ret;
3963 tree label_true, label_false, label_cont;
3964 bool have_then_clause_p, have_else_clause_p;
3965 gcond *cond_stmt;
3966 enum tree_code pred_code;
3967 gimple_seq seq = NULL;
3969 /* If this COND_EXPR has a value, copy the values into a temporary within
3970 the arms. */
3971 if (!VOID_TYPE_P (type))
3973 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3974 tree result;
3976 /* If either an rvalue is ok or we do not require an lvalue, create the
3977 temporary. But we cannot do that if the type is addressable. */
3978 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3979 && !TREE_ADDRESSABLE (type))
3981 if (gimplify_ctxp->allow_rhs_cond_expr
3982 /* If either branch has side effects or could trap, it can't be
3983 evaluated unconditionally. */
3984 && !TREE_SIDE_EFFECTS (then_)
3985 && !generic_expr_could_trap_p (then_)
3986 && !TREE_SIDE_EFFECTS (else_)
3987 && !generic_expr_could_trap_p (else_))
3988 return gimplify_pure_cond_expr (expr_p, pre_p);
3990 tmp = create_tmp_var (type, "iftmp");
3991 result = tmp;
3994 /* Otherwise, only create and copy references to the values. */
3995 else
3997 type = build_pointer_type (type);
3999 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4000 then_ = build_fold_addr_expr_loc (loc, then_);
4002 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4003 else_ = build_fold_addr_expr_loc (loc, else_);
4005 expr
4006 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4008 tmp = create_tmp_var (type, "iftmp");
4009 result = build_simple_mem_ref_loc (loc, tmp);
4012 /* Build the new then clause, `tmp = then_;'. But don't build the
4013 assignment if the value is void; in C++ it can be if it's a throw. */
4014 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4015 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4017 /* Similarly, build the new else clause, `tmp = else_;'. */
4018 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4019 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4021 TREE_TYPE (expr) = void_type_node;
4022 recalculate_side_effects (expr);
4024 /* Move the COND_EXPR to the prequeue. */
4025 gimplify_stmt (&expr, pre_p);
4027 *expr_p = result;
4028 return GS_ALL_DONE;
4031 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4032 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4033 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4034 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4036 /* Make sure the condition has BOOLEAN_TYPE. */
4037 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4039 /* Break apart && and || conditions. */
4040 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4041 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4043 expr = shortcut_cond_expr (expr);
4045 if (expr != *expr_p)
4047 *expr_p = expr;
4049 /* We can't rely on gimplify_expr to re-gimplify the expanded
4050 form properly, as cleanups might cause the target labels to be
4051 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4052 set up a conditional context. */
4053 gimple_push_condition ();
4054 gimplify_stmt (expr_p, &seq);
4055 gimple_pop_condition (pre_p);
4056 gimple_seq_add_seq (pre_p, seq);
4058 return GS_ALL_DONE;
4062 /* Now do the normal gimplification. */
4064 /* Gimplify condition. */
4065 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4066 fb_rvalue);
4067 if (ret == GS_ERROR)
4068 return GS_ERROR;
4069 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4071 gimple_push_condition ();
4073 have_then_clause_p = have_else_clause_p = false;
4074 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4075 if (label_true
4076 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4077 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4078 have different locations, otherwise we end up with incorrect
4079 location information on the branches. */
4080 && (optimize
4081 || !EXPR_HAS_LOCATION (expr)
4082 || !rexpr_has_location (label_true)
4083 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4085 have_then_clause_p = true;
4086 label_true = GOTO_DESTINATION (label_true);
4088 else
4089 label_true = create_artificial_label (UNKNOWN_LOCATION);
4090 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4091 if (label_false
4092 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4093 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4094 have different locations, otherwise we end up with incorrect
4095 location information on the branches. */
4096 && (optimize
4097 || !EXPR_HAS_LOCATION (expr)
4098 || !rexpr_has_location (label_false)
4099 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4101 have_else_clause_p = true;
4102 label_false = GOTO_DESTINATION (label_false);
4104 else
4105 label_false = create_artificial_label (UNKNOWN_LOCATION);
4107 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4108 &arm2);
4109 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4110 label_false);
4111 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4112 gimplify_seq_add_stmt (&seq, cond_stmt);
4113 gimple_stmt_iterator gsi = gsi_last (seq);
4114 maybe_fold_stmt (&gsi);
4116 label_cont = NULL_TREE;
4117 if (!have_then_clause_p)
4119 /* For if (...) {} else { code; } put label_true after
4120 the else block. */
4121 if (TREE_OPERAND (expr, 1) == NULL_TREE
4122 && !have_else_clause_p
4123 && TREE_OPERAND (expr, 2) != NULL_TREE)
4124 label_cont = label_true;
4125 else
4127 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4128 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4129 /* For if (...) { code; } else {} or
4130 if (...) { code; } else goto label; or
4131 if (...) { code; return; } else { ... }
4132 label_cont isn't needed. */
4133 if (!have_else_clause_p
4134 && TREE_OPERAND (expr, 2) != NULL_TREE
4135 && gimple_seq_may_fallthru (seq))
4137 gimple *g;
4138 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4140 g = gimple_build_goto (label_cont);
4142 /* GIMPLE_COND's are very low level; they have embedded
4143 gotos. This particular embedded goto should not be marked
4144 with the location of the original COND_EXPR, as it would
4145 correspond to the COND_EXPR's condition, not the ELSE or the
4146 THEN arms. To avoid marking it with the wrong location, flag
4147 it as "no location". */
4148 gimple_set_do_not_emit_location (g);
4150 gimplify_seq_add_stmt (&seq, g);
4154 if (!have_else_clause_p)
4156 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4157 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4159 if (label_cont)
4160 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4162 gimple_pop_condition (pre_p);
4163 gimple_seq_add_seq (pre_p, seq);
4165 if (ret == GS_ERROR)
4166 ; /* Do nothing. */
4167 else if (have_then_clause_p || have_else_clause_p)
4168 ret = GS_ALL_DONE;
4169 else
4171 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4172 expr = TREE_OPERAND (expr, 0);
4173 gimplify_stmt (&expr, pre_p);
4176 *expr_p = NULL;
4177 return ret;
4180 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4181 to be marked addressable.
4183 We cannot rely on such an expression being directly markable if a temporary
4184 has been created by the gimplification. In this case, we create another
4185 temporary and initialize it with a copy, which will become a store after we
4186 mark it addressable. This can happen if the front-end passed us something
4187 that it could not mark addressable yet, like a Fortran pass-by-reference
4188 parameter (int) floatvar. */
4190 static void
4191 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4193 while (handled_component_p (*expr_p))
4194 expr_p = &TREE_OPERAND (*expr_p, 0);
4195 if (is_gimple_reg (*expr_p))
4197 /* Do not allow an SSA name as the temporary. */
4198 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4199 DECL_GIMPLE_REG_P (var) = 0;
4200 *expr_p = var;
4204 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4205 a call to __builtin_memcpy. */
4207 static enum gimplify_status
4208 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4209 gimple_seq *seq_p)
4211 tree t, to, to_ptr, from, from_ptr;
4212 gcall *gs;
4213 location_t loc = EXPR_LOCATION (*expr_p);
4215 to = TREE_OPERAND (*expr_p, 0);
4216 from = TREE_OPERAND (*expr_p, 1);
4218 /* Mark the RHS addressable. Beware that it may not be possible to do so
4219 directly if a temporary has been created by the gimplification. */
4220 prepare_gimple_addressable (&from, seq_p);
4222 mark_addressable (from);
4223 from_ptr = build_fold_addr_expr_loc (loc, from);
4224 gimplify_arg (&from_ptr, seq_p, loc);
4226 mark_addressable (to);
4227 to_ptr = build_fold_addr_expr_loc (loc, to);
4228 gimplify_arg (&to_ptr, seq_p, loc);
4230 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4232 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4234 if (want_value)
4236 /* tmp = memcpy() */
4237 t = create_tmp_var (TREE_TYPE (to_ptr));
4238 gimple_call_set_lhs (gs, t);
4239 gimplify_seq_add_stmt (seq_p, gs);
4241 *expr_p = build_simple_mem_ref (t);
4242 return GS_ALL_DONE;
4245 gimplify_seq_add_stmt (seq_p, gs);
4246 *expr_p = NULL;
4247 return GS_ALL_DONE;
4250 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4251 a call to __builtin_memset. In this case we know that the RHS is
4252 a CONSTRUCTOR with an empty element list. */
4254 static enum gimplify_status
4255 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4256 gimple_seq *seq_p)
4258 tree t, from, to, to_ptr;
4259 gcall *gs;
4260 location_t loc = EXPR_LOCATION (*expr_p);
4262 /* Assert our assumptions, to abort instead of producing wrong code
4263 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4264 not be immediately exposed. */
4265 from = TREE_OPERAND (*expr_p, 1);
4266 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4267 from = TREE_OPERAND (from, 0);
4269 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4270 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4272 /* Now proceed. */
4273 to = TREE_OPERAND (*expr_p, 0);
4275 to_ptr = build_fold_addr_expr_loc (loc, to);
4276 gimplify_arg (&to_ptr, seq_p, loc);
4277 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4279 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4281 if (want_value)
4283 /* tmp = memset() */
4284 t = create_tmp_var (TREE_TYPE (to_ptr));
4285 gimple_call_set_lhs (gs, t);
4286 gimplify_seq_add_stmt (seq_p, gs);
4288 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4289 return GS_ALL_DONE;
4292 gimplify_seq_add_stmt (seq_p, gs);
4293 *expr_p = NULL;
4294 return GS_ALL_DONE;
4297 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4298 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4299 assignment. Return non-null if we detect a potential overlap. */
4301 struct gimplify_init_ctor_preeval_data
4303 /* The base decl of the lhs object. May be NULL, in which case we
4304 have to assume the lhs is indirect. */
4305 tree lhs_base_decl;
4307 /* The alias set of the lhs object. */
4308 alias_set_type lhs_alias_set;
4311 static tree
4312 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4314 struct gimplify_init_ctor_preeval_data *data
4315 = (struct gimplify_init_ctor_preeval_data *) xdata;
4316 tree t = *tp;
4318 /* If we find the base object, obviously we have overlap. */
4319 if (data->lhs_base_decl == t)
4320 return t;
4322 /* If the constructor component is indirect, determine if we have a
4323 potential overlap with the lhs. The only bits of information we
4324 have to go on at this point are addressability and alias sets. */
4325 if ((INDIRECT_REF_P (t)
4326 || TREE_CODE (t) == MEM_REF)
4327 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4328 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4329 return t;
4331 /* If the constructor component is a call, determine if it can hide a
4332 potential overlap with the lhs through an INDIRECT_REF like above.
4333 ??? Ugh - this is completely broken. In fact this whole analysis
4334 doesn't look conservative. */
4335 if (TREE_CODE (t) == CALL_EXPR)
4337 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4339 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4340 if (POINTER_TYPE_P (TREE_VALUE (type))
4341 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4342 && alias_sets_conflict_p (data->lhs_alias_set,
4343 get_alias_set
4344 (TREE_TYPE (TREE_VALUE (type)))))
4345 return t;
4348 if (IS_TYPE_OR_DECL_P (t))
4349 *walk_subtrees = 0;
4350 return NULL;
4353 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4354 force values that overlap with the lhs (as described by *DATA)
4355 into temporaries. */
4357 static void
4358 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4359 struct gimplify_init_ctor_preeval_data *data)
4361 enum gimplify_status one;
4363 /* If the value is constant, then there's nothing to pre-evaluate. */
4364 if (TREE_CONSTANT (*expr_p))
4366 /* Ensure it does not have side effects, it might contain a reference to
4367 the object we're initializing. */
4368 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4369 return;
4372 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4373 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4374 return;
4376 /* Recurse for nested constructors. */
4377 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4379 unsigned HOST_WIDE_INT ix;
4380 constructor_elt *ce;
4381 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4383 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4384 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4386 return;
4389 /* If this is a variable sized type, we must remember the size. */
4390 maybe_with_size_expr (expr_p);
4392 /* Gimplify the constructor element to something appropriate for the rhs
4393 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4394 the gimplifier will consider this a store to memory. Doing this
4395 gimplification now means that we won't have to deal with complicated
4396 language-specific trees, nor trees like SAVE_EXPR that can induce
4397 exponential search behavior. */
4398 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4399 if (one == GS_ERROR)
4401 *expr_p = NULL;
4402 return;
4405 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4406 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4407 always be true for all scalars, since is_gimple_mem_rhs insists on a
4408 temporary variable for them. */
4409 if (DECL_P (*expr_p))
4410 return;
4412 /* If this is of variable size, we have no choice but to assume it doesn't
4413 overlap since we can't make a temporary for it. */
4414 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4415 return;
4417 /* Otherwise, we must search for overlap ... */
4418 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4419 return;
4421 /* ... and if found, force the value into a temporary. */
4422 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4425 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4426 a RANGE_EXPR in a CONSTRUCTOR for an array.
4428 var = lower;
4429 loop_entry:
4430 object[var] = value;
4431 if (var == upper)
4432 goto loop_exit;
4433 var = var + 1;
4434 goto loop_entry;
4435 loop_exit:
4437 We increment var _after_ the loop exit check because we might otherwise
4438 fail if upper == TYPE_MAX_VALUE (type for upper).
4440 Note that we never have to deal with SAVE_EXPRs here, because this has
4441 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4443 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4444 gimple_seq *, bool);
4446 static void
4447 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4448 tree value, tree array_elt_type,
4449 gimple_seq *pre_p, bool cleared)
4451 tree loop_entry_label, loop_exit_label, fall_thru_label;
4452 tree var, var_type, cref, tmp;
4454 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4455 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4456 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4458 /* Create and initialize the index variable. */
4459 var_type = TREE_TYPE (upper);
4460 var = create_tmp_var (var_type);
4461 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4463 /* Add the loop entry label. */
4464 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4466 /* Build the reference. */
4467 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4468 var, NULL_TREE, NULL_TREE);
4470 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4471 the store. Otherwise just assign value to the reference. */
4473 if (TREE_CODE (value) == CONSTRUCTOR)
4474 /* NB we might have to call ourself recursively through
4475 gimplify_init_ctor_eval if the value is a constructor. */
4476 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4477 pre_p, cleared);
4478 else
4479 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4481 /* We exit the loop when the index var is equal to the upper bound. */
4482 gimplify_seq_add_stmt (pre_p,
4483 gimple_build_cond (EQ_EXPR, var, upper,
4484 loop_exit_label, fall_thru_label));
4486 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4488 /* Otherwise, increment the index var... */
4489 tmp = build2 (PLUS_EXPR, var_type, var,
4490 fold_convert (var_type, integer_one_node));
4491 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4493 /* ...and jump back to the loop entry. */
4494 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4496 /* Add the loop exit label. */
4497 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4500 /* Return true if FDECL is accessing a field that is zero sized. */
4502 static bool
4503 zero_sized_field_decl (const_tree fdecl)
4505 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4506 && integer_zerop (DECL_SIZE (fdecl)))
4507 return true;
4508 return false;
4511 /* Return true if TYPE is zero sized. */
4513 static bool
4514 zero_sized_type (const_tree type)
4516 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4517 && integer_zerop (TYPE_SIZE (type)))
4518 return true;
4519 return false;
4522 /* A subroutine of gimplify_init_constructor. Generate individual
4523 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4524 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4525 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4526 zeroed first. */
4528 static void
4529 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4530 gimple_seq *pre_p, bool cleared)
4532 tree array_elt_type = NULL;
4533 unsigned HOST_WIDE_INT ix;
4534 tree purpose, value;
4536 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4537 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4539 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4541 tree cref;
4543 /* NULL values are created above for gimplification errors. */
4544 if (value == NULL)
4545 continue;
4547 if (cleared && initializer_zerop (value))
4548 continue;
4550 /* ??? Here's to hoping the front end fills in all of the indices,
4551 so we don't have to figure out what's missing ourselves. */
4552 gcc_assert (purpose);
4554 /* Skip zero-sized fields, unless value has side-effects. This can
4555 happen with calls to functions returning a zero-sized type, which
4556 we shouldn't discard. As a number of downstream passes don't
4557 expect sets of zero-sized fields, we rely on the gimplification of
4558 the MODIFY_EXPR we make below to drop the assignment statement. */
4559 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4560 continue;
4562 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4563 whole range. */
4564 if (TREE_CODE (purpose) == RANGE_EXPR)
4566 tree lower = TREE_OPERAND (purpose, 0);
4567 tree upper = TREE_OPERAND (purpose, 1);
4569 /* If the lower bound is equal to upper, just treat it as if
4570 upper was the index. */
4571 if (simple_cst_equal (lower, upper))
4572 purpose = upper;
4573 else
4575 gimplify_init_ctor_eval_range (object, lower, upper, value,
4576 array_elt_type, pre_p, cleared);
4577 continue;
4581 if (array_elt_type)
4583 /* Do not use bitsizetype for ARRAY_REF indices. */
4584 if (TYPE_DOMAIN (TREE_TYPE (object)))
4585 purpose
4586 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4587 purpose);
4588 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4589 purpose, NULL_TREE, NULL_TREE);
4591 else
4593 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4594 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4595 unshare_expr (object), purpose, NULL_TREE);
4598 if (TREE_CODE (value) == CONSTRUCTOR
4599 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4600 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4601 pre_p, cleared);
4602 else
4604 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4605 gimplify_and_add (init, pre_p);
4606 ggc_free (init);
4611 /* Return the appropriate RHS predicate for this LHS. */
4613 gimple_predicate
4614 rhs_predicate_for (tree lhs)
4616 if (is_gimple_reg (lhs))
4617 return is_gimple_reg_rhs_or_call;
4618 else
4619 return is_gimple_mem_rhs_or_call;
4622 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4623 before the LHS has been gimplified. */
4625 static gimple_predicate
4626 initial_rhs_predicate_for (tree lhs)
4628 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4629 return is_gimple_reg_rhs_or_call;
4630 else
4631 return is_gimple_mem_rhs_or_call;
4634 /* Gimplify a C99 compound literal expression. This just means adding
4635 the DECL_EXPR before the current statement and using its anonymous
4636 decl instead. */
4638 static enum gimplify_status
4639 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4640 bool (*gimple_test_f) (tree),
4641 fallback_t fallback)
4643 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4644 tree decl = DECL_EXPR_DECL (decl_s);
4645 tree init = DECL_INITIAL (decl);
4646 /* Mark the decl as addressable if the compound literal
4647 expression is addressable now, otherwise it is marked too late
4648 after we gimplify the initialization expression. */
4649 if (TREE_ADDRESSABLE (*expr_p))
4650 TREE_ADDRESSABLE (decl) = 1;
4651 /* Otherwise, if we don't need an lvalue and have a literal directly
4652 substitute it. Check if it matches the gimple predicate, as
4653 otherwise we'd generate a new temporary, and we can as well just
4654 use the decl we already have. */
4655 else if (!TREE_ADDRESSABLE (decl)
4656 && init
4657 && (fallback & fb_lvalue) == 0
4658 && gimple_test_f (init))
4660 *expr_p = init;
4661 return GS_OK;
4664 /* Preliminarily mark non-addressed complex variables as eligible
4665 for promotion to gimple registers. We'll transform their uses
4666 as we find them. */
4667 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4668 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4669 && !TREE_THIS_VOLATILE (decl)
4670 && !needs_to_live_in_memory (decl))
4671 DECL_GIMPLE_REG_P (decl) = 1;
4673 /* If the decl is not addressable, then it is being used in some
4674 expression or on the right hand side of a statement, and it can
4675 be put into a readonly data section. */
4676 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4677 TREE_READONLY (decl) = 1;
4679 /* This decl isn't mentioned in the enclosing block, so add it to the
4680 list of temps. FIXME it seems a bit of a kludge to say that
4681 anonymous artificial vars aren't pushed, but everything else is. */
4682 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4683 gimple_add_tmp_var (decl);
4685 gimplify_and_add (decl_s, pre_p);
4686 *expr_p = decl;
4687 return GS_OK;
4690 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4691 return a new CONSTRUCTOR if something changed. */
4693 static tree
4694 optimize_compound_literals_in_ctor (tree orig_ctor)
4696 tree ctor = orig_ctor;
4697 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4698 unsigned int idx, num = vec_safe_length (elts);
4700 for (idx = 0; idx < num; idx++)
4702 tree value = (*elts)[idx].value;
4703 tree newval = value;
4704 if (TREE_CODE (value) == CONSTRUCTOR)
4705 newval = optimize_compound_literals_in_ctor (value);
4706 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4708 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4709 tree decl = DECL_EXPR_DECL (decl_s);
4710 tree init = DECL_INITIAL (decl);
4712 if (!TREE_ADDRESSABLE (value)
4713 && !TREE_ADDRESSABLE (decl)
4714 && init
4715 && TREE_CODE (init) == CONSTRUCTOR)
4716 newval = optimize_compound_literals_in_ctor (init);
4718 if (newval == value)
4719 continue;
4721 if (ctor == orig_ctor)
4723 ctor = copy_node (orig_ctor);
4724 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4725 elts = CONSTRUCTOR_ELTS (ctor);
4727 (*elts)[idx].value = newval;
4729 return ctor;
4732 /* A subroutine of gimplify_modify_expr. Break out elements of a
4733 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4735 Note that we still need to clear any elements that don't have explicit
4736 initializers, so if not all elements are initialized we keep the
4737 original MODIFY_EXPR, we just remove all of the constructor elements.
4739 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4740 GS_ERROR if we would have to create a temporary when gimplifying
4741 this constructor. Otherwise, return GS_OK.
4743 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4745 static enum gimplify_status
4746 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4747 bool want_value, bool notify_temp_creation)
4749 tree object, ctor, type;
4750 enum gimplify_status ret;
4751 vec<constructor_elt, va_gc> *elts;
4753 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4755 if (!notify_temp_creation)
4757 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4758 is_gimple_lvalue, fb_lvalue);
4759 if (ret == GS_ERROR)
4760 return ret;
4763 object = TREE_OPERAND (*expr_p, 0);
4764 ctor = TREE_OPERAND (*expr_p, 1)
4765 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4766 type = TREE_TYPE (ctor);
4767 elts = CONSTRUCTOR_ELTS (ctor);
4768 ret = GS_ALL_DONE;
4770 switch (TREE_CODE (type))
4772 case RECORD_TYPE:
4773 case UNION_TYPE:
4774 case QUAL_UNION_TYPE:
4775 case ARRAY_TYPE:
4777 struct gimplify_init_ctor_preeval_data preeval_data;
4778 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4779 bool cleared, complete_p, valid_const_initializer;
4781 /* Aggregate types must lower constructors to initialization of
4782 individual elements. The exception is that a CONSTRUCTOR node
4783 with no elements indicates zero-initialization of the whole. */
4784 if (vec_safe_is_empty (elts))
4786 if (notify_temp_creation)
4787 return GS_OK;
4788 break;
4791 /* Fetch information about the constructor to direct later processing.
4792 We might want to make static versions of it in various cases, and
4793 can only do so if it known to be a valid constant initializer. */
4794 valid_const_initializer
4795 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4796 &num_ctor_elements, &complete_p);
4798 /* If a const aggregate variable is being initialized, then it
4799 should never be a lose to promote the variable to be static. */
4800 if (valid_const_initializer
4801 && num_nonzero_elements > 1
4802 && TREE_READONLY (object)
4803 && VAR_P (object)
4804 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4806 if (notify_temp_creation)
4807 return GS_ERROR;
4808 DECL_INITIAL (object) = ctor;
4809 TREE_STATIC (object) = 1;
4810 if (!DECL_NAME (object))
4811 DECL_NAME (object) = create_tmp_var_name ("C");
4812 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4814 /* ??? C++ doesn't automatically append a .<number> to the
4815 assembler name, and even when it does, it looks at FE private
4816 data structures to figure out what that number should be,
4817 which are not set for this variable. I suppose this is
4818 important for local statics for inline functions, which aren't
4819 "local" in the object file sense. So in order to get a unique
4820 TU-local symbol, we must invoke the lhd version now. */
4821 lhd_set_decl_assembler_name (object);
4823 *expr_p = NULL_TREE;
4824 break;
4827 /* If there are "lots" of initialized elements, even discounting
4828 those that are not address constants (and thus *must* be
4829 computed at runtime), then partition the constructor into
4830 constant and non-constant parts. Block copy the constant
4831 parts in, then generate code for the non-constant parts. */
4832 /* TODO. There's code in cp/typeck.c to do this. */
4834 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4835 /* store_constructor will ignore the clearing of variable-sized
4836 objects. Initializers for such objects must explicitly set
4837 every field that needs to be set. */
4838 cleared = false;
4839 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4840 /* If the constructor isn't complete, clear the whole object
4841 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4843 ??? This ought not to be needed. For any element not present
4844 in the initializer, we should simply set them to zero. Except
4845 we'd need to *find* the elements that are not present, and that
4846 requires trickery to avoid quadratic compile-time behavior in
4847 large cases or excessive memory use in small cases. */
4848 cleared = true;
4849 else if (num_ctor_elements - num_nonzero_elements
4850 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4851 && num_nonzero_elements < num_ctor_elements / 4)
4852 /* If there are "lots" of zeros, it's more efficient to clear
4853 the memory and then set the nonzero elements. */
4854 cleared = true;
4855 else
4856 cleared = false;
4858 /* If there are "lots" of initialized elements, and all of them
4859 are valid address constants, then the entire initializer can
4860 be dropped to memory, and then memcpy'd out. Don't do this
4861 for sparse arrays, though, as it's more efficient to follow
4862 the standard CONSTRUCTOR behavior of memset followed by
4863 individual element initialization. Also don't do this for small
4864 all-zero initializers (which aren't big enough to merit
4865 clearing), and don't try to make bitwise copies of
4866 TREE_ADDRESSABLE types.
4868 We cannot apply such transformation when compiling chkp static
4869 initializer because creation of initializer image in the memory
4870 will require static initialization of bounds for it. It should
4871 result in another gimplification of similar initializer and we
4872 may fall into infinite loop. */
4873 if (valid_const_initializer
4874 && !(cleared || num_nonzero_elements == 0)
4875 && !TREE_ADDRESSABLE (type)
4876 && (!current_function_decl
4877 || !lookup_attribute ("chkp ctor",
4878 DECL_ATTRIBUTES (current_function_decl))))
4880 HOST_WIDE_INT size = int_size_in_bytes (type);
4881 unsigned int align;
4883 /* ??? We can still get unbounded array types, at least
4884 from the C++ front end. This seems wrong, but attempt
4885 to work around it for now. */
4886 if (size < 0)
4888 size = int_size_in_bytes (TREE_TYPE (object));
4889 if (size >= 0)
4890 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4893 /* Find the maximum alignment we can assume for the object. */
4894 /* ??? Make use of DECL_OFFSET_ALIGN. */
4895 if (DECL_P (object))
4896 align = DECL_ALIGN (object);
4897 else
4898 align = TYPE_ALIGN (type);
4900 /* Do a block move either if the size is so small as to make
4901 each individual move a sub-unit move on average, or if it
4902 is so large as to make individual moves inefficient. */
4903 if (size > 0
4904 && num_nonzero_elements > 1
4905 && (size < num_nonzero_elements
4906 || !can_move_by_pieces (size, align)))
4908 if (notify_temp_creation)
4909 return GS_ERROR;
4911 walk_tree (&ctor, force_labels_r, NULL, NULL);
4912 ctor = tree_output_constant_def (ctor);
4913 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4914 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4915 TREE_OPERAND (*expr_p, 1) = ctor;
4917 /* This is no longer an assignment of a CONSTRUCTOR, but
4918 we still may have processing to do on the LHS. So
4919 pretend we didn't do anything here to let that happen. */
4920 return GS_UNHANDLED;
4924 /* If the target is volatile, we have non-zero elements and more than
4925 one field to assign, initialize the target from a temporary. */
4926 if (TREE_THIS_VOLATILE (object)
4927 && !TREE_ADDRESSABLE (type)
4928 && num_nonzero_elements > 0
4929 && vec_safe_length (elts) > 1)
4931 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4932 TREE_OPERAND (*expr_p, 0) = temp;
4933 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4934 *expr_p,
4935 build2 (MODIFY_EXPR, void_type_node,
4936 object, temp));
4937 return GS_OK;
4940 if (notify_temp_creation)
4941 return GS_OK;
4943 /* If there are nonzero elements and if needed, pre-evaluate to capture
4944 elements overlapping with the lhs into temporaries. We must do this
4945 before clearing to fetch the values before they are zeroed-out. */
4946 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4948 preeval_data.lhs_base_decl = get_base_address (object);
4949 if (!DECL_P (preeval_data.lhs_base_decl))
4950 preeval_data.lhs_base_decl = NULL;
4951 preeval_data.lhs_alias_set = get_alias_set (object);
4953 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4954 pre_p, post_p, &preeval_data);
4957 bool ctor_has_side_effects_p
4958 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4960 if (cleared)
4962 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4963 Note that we still have to gimplify, in order to handle the
4964 case of variable sized types. Avoid shared tree structures. */
4965 CONSTRUCTOR_ELTS (ctor) = NULL;
4966 TREE_SIDE_EFFECTS (ctor) = 0;
4967 object = unshare_expr (object);
4968 gimplify_stmt (expr_p, pre_p);
4971 /* If we have not block cleared the object, or if there are nonzero
4972 elements in the constructor, or if the constructor has side effects,
4973 add assignments to the individual scalar fields of the object. */
4974 if (!cleared
4975 || num_nonzero_elements > 0
4976 || ctor_has_side_effects_p)
4977 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4979 *expr_p = NULL_TREE;
4981 break;
4983 case COMPLEX_TYPE:
4985 tree r, i;
4987 if (notify_temp_creation)
4988 return GS_OK;
4990 /* Extract the real and imaginary parts out of the ctor. */
4991 gcc_assert (elts->length () == 2);
4992 r = (*elts)[0].value;
4993 i = (*elts)[1].value;
4994 if (r == NULL || i == NULL)
4996 tree zero = build_zero_cst (TREE_TYPE (type));
4997 if (r == NULL)
4998 r = zero;
4999 if (i == NULL)
5000 i = zero;
5003 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5004 represent creation of a complex value. */
5005 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5007 ctor = build_complex (type, r, i);
5008 TREE_OPERAND (*expr_p, 1) = ctor;
5010 else
5012 ctor = build2 (COMPLEX_EXPR, type, r, i);
5013 TREE_OPERAND (*expr_p, 1) = ctor;
5014 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5015 pre_p,
5016 post_p,
5017 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5018 fb_rvalue);
5021 break;
5023 case VECTOR_TYPE:
5025 unsigned HOST_WIDE_INT ix;
5026 constructor_elt *ce;
5028 if (notify_temp_creation)
5029 return GS_OK;
5031 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5032 if (TREE_CONSTANT (ctor))
5034 bool constant_p = true;
5035 tree value;
5037 /* Even when ctor is constant, it might contain non-*_CST
5038 elements, such as addresses or trapping values like
5039 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5040 in VECTOR_CST nodes. */
5041 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5042 if (!CONSTANT_CLASS_P (value))
5044 constant_p = false;
5045 break;
5048 if (constant_p)
5050 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5051 break;
5054 TREE_CONSTANT (ctor) = 0;
5057 /* Vector types use CONSTRUCTOR all the way through gimple
5058 compilation as a general initializer. */
5059 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5061 enum gimplify_status tret;
5062 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5063 fb_rvalue);
5064 if (tret == GS_ERROR)
5065 ret = GS_ERROR;
5066 else if (TREE_STATIC (ctor)
5067 && !initializer_constant_valid_p (ce->value,
5068 TREE_TYPE (ce->value)))
5069 TREE_STATIC (ctor) = 0;
5071 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5072 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5074 break;
5076 default:
5077 /* So how did we get a CONSTRUCTOR for a scalar type? */
5078 gcc_unreachable ();
5081 if (ret == GS_ERROR)
5082 return GS_ERROR;
5083 /* If we have gimplified both sides of the initializer but have
5084 not emitted an assignment, do so now. */
5085 if (*expr_p)
5087 tree lhs = TREE_OPERAND (*expr_p, 0);
5088 tree rhs = TREE_OPERAND (*expr_p, 1);
5089 if (want_value && object == lhs)
5090 lhs = unshare_expr (lhs);
5091 gassign *init = gimple_build_assign (lhs, rhs);
5092 gimplify_seq_add_stmt (pre_p, init);
5094 if (want_value)
5096 *expr_p = object;
5097 return GS_OK;
5099 else
5101 *expr_p = NULL;
5102 return GS_ALL_DONE;
5106 /* Given a pointer value OP0, return a simplified version of an
5107 indirection through OP0, or NULL_TREE if no simplification is
5108 possible. This may only be applied to a rhs of an expression.
5109 Note that the resulting type may be different from the type pointed
5110 to in the sense that it is still compatible from the langhooks
5111 point of view. */
5113 static tree
5114 gimple_fold_indirect_ref_rhs (tree t)
5116 return gimple_fold_indirect_ref (t);
5119 /* Subroutine of gimplify_modify_expr to do simplifications of
5120 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5121 something changes. */
5123 static enum gimplify_status
5124 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5125 gimple_seq *pre_p, gimple_seq *post_p,
5126 bool want_value)
5128 enum gimplify_status ret = GS_UNHANDLED;
5129 bool changed;
5133 changed = false;
5134 switch (TREE_CODE (*from_p))
5136 case VAR_DECL:
5137 /* If we're assigning from a read-only variable initialized with
5138 a constructor, do the direct assignment from the constructor,
5139 but only if neither source nor target are volatile since this
5140 latter assignment might end up being done on a per-field basis. */
5141 if (DECL_INITIAL (*from_p)
5142 && TREE_READONLY (*from_p)
5143 && !TREE_THIS_VOLATILE (*from_p)
5144 && !TREE_THIS_VOLATILE (*to_p)
5145 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5147 tree old_from = *from_p;
5148 enum gimplify_status subret;
5150 /* Move the constructor into the RHS. */
5151 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5153 /* Let's see if gimplify_init_constructor will need to put
5154 it in memory. */
5155 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5156 false, true);
5157 if (subret == GS_ERROR)
5159 /* If so, revert the change. */
5160 *from_p = old_from;
5162 else
5164 ret = GS_OK;
5165 changed = true;
5168 break;
5169 case INDIRECT_REF:
5171 /* If we have code like
5173 *(const A*)(A*)&x
5175 where the type of "x" is a (possibly cv-qualified variant
5176 of "A"), treat the entire expression as identical to "x".
5177 This kind of code arises in C++ when an object is bound
5178 to a const reference, and if "x" is a TARGET_EXPR we want
5179 to take advantage of the optimization below. */
5180 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5181 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5182 if (t)
5184 if (TREE_THIS_VOLATILE (t) != volatile_p)
5186 if (DECL_P (t))
5187 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5188 build_fold_addr_expr (t));
5189 if (REFERENCE_CLASS_P (t))
5190 TREE_THIS_VOLATILE (t) = volatile_p;
5192 *from_p = t;
5193 ret = GS_OK;
5194 changed = true;
5196 break;
5199 case TARGET_EXPR:
5201 /* If we are initializing something from a TARGET_EXPR, strip the
5202 TARGET_EXPR and initialize it directly, if possible. This can't
5203 be done if the initializer is void, since that implies that the
5204 temporary is set in some non-trivial way.
5206 ??? What about code that pulls out the temp and uses it
5207 elsewhere? I think that such code never uses the TARGET_EXPR as
5208 an initializer. If I'm wrong, we'll die because the temp won't
5209 have any RTL. In that case, I guess we'll need to replace
5210 references somehow. */
5211 tree init = TARGET_EXPR_INITIAL (*from_p);
5213 if (init
5214 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5215 || !TARGET_EXPR_NO_ELIDE (*from_p))
5216 && !VOID_TYPE_P (TREE_TYPE (init)))
5218 *from_p = init;
5219 ret = GS_OK;
5220 changed = true;
5223 break;
5225 case COMPOUND_EXPR:
5226 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5227 caught. */
5228 gimplify_compound_expr (from_p, pre_p, true);
5229 ret = GS_OK;
5230 changed = true;
5231 break;
5233 case CONSTRUCTOR:
5234 /* If we already made some changes, let the front end have a
5235 crack at this before we break it down. */
5236 if (ret != GS_UNHANDLED)
5237 break;
5238 /* If we're initializing from a CONSTRUCTOR, break this into
5239 individual MODIFY_EXPRs. */
5240 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5241 false);
5243 case COND_EXPR:
5244 /* If we're assigning to a non-register type, push the assignment
5245 down into the branches. This is mandatory for ADDRESSABLE types,
5246 since we cannot generate temporaries for such, but it saves a
5247 copy in other cases as well. */
5248 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5250 /* This code should mirror the code in gimplify_cond_expr. */
5251 enum tree_code code = TREE_CODE (*expr_p);
5252 tree cond = *from_p;
5253 tree result = *to_p;
5255 ret = gimplify_expr (&result, pre_p, post_p,
5256 is_gimple_lvalue, fb_lvalue);
5257 if (ret != GS_ERROR)
5258 ret = GS_OK;
5260 /* If we are going to write RESULT more than once, clear
5261 TREE_READONLY flag, otherwise we might incorrectly promote
5262 the variable to static const and initialize it at compile
5263 time in one of the branches. */
5264 if (VAR_P (result)
5265 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5266 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5267 TREE_READONLY (result) = 0;
5268 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5269 TREE_OPERAND (cond, 1)
5270 = build2 (code, void_type_node, result,
5271 TREE_OPERAND (cond, 1));
5272 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5273 TREE_OPERAND (cond, 2)
5274 = build2 (code, void_type_node, unshare_expr (result),
5275 TREE_OPERAND (cond, 2));
5277 TREE_TYPE (cond) = void_type_node;
5278 recalculate_side_effects (cond);
5280 if (want_value)
5282 gimplify_and_add (cond, pre_p);
5283 *expr_p = unshare_expr (result);
5285 else
5286 *expr_p = cond;
5287 return ret;
5289 break;
5291 case CALL_EXPR:
5292 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5293 return slot so that we don't generate a temporary. */
5294 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5295 && aggregate_value_p (*from_p, *from_p))
5297 bool use_target;
5299 if (!(rhs_predicate_for (*to_p))(*from_p))
5300 /* If we need a temporary, *to_p isn't accurate. */
5301 use_target = false;
5302 /* It's OK to use the return slot directly unless it's an NRV. */
5303 else if (TREE_CODE (*to_p) == RESULT_DECL
5304 && DECL_NAME (*to_p) == NULL_TREE
5305 && needs_to_live_in_memory (*to_p))
5306 use_target = true;
5307 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5308 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5309 /* Don't force regs into memory. */
5310 use_target = false;
5311 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5312 /* It's OK to use the target directly if it's being
5313 initialized. */
5314 use_target = true;
5315 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5316 != INTEGER_CST)
5317 /* Always use the target and thus RSO for variable-sized types.
5318 GIMPLE cannot deal with a variable-sized assignment
5319 embedded in a call statement. */
5320 use_target = true;
5321 else if (TREE_CODE (*to_p) != SSA_NAME
5322 && (!is_gimple_variable (*to_p)
5323 || needs_to_live_in_memory (*to_p)))
5324 /* Don't use the original target if it's already addressable;
5325 if its address escapes, and the called function uses the
5326 NRV optimization, a conforming program could see *to_p
5327 change before the called function returns; see c++/19317.
5328 When optimizing, the return_slot pass marks more functions
5329 as safe after we have escape info. */
5330 use_target = false;
5331 else
5332 use_target = true;
5334 if (use_target)
5336 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5337 mark_addressable (*to_p);
5340 break;
5342 case WITH_SIZE_EXPR:
5343 /* Likewise for calls that return an aggregate of non-constant size,
5344 since we would not be able to generate a temporary at all. */
5345 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5347 *from_p = TREE_OPERAND (*from_p, 0);
5348 /* We don't change ret in this case because the
5349 WITH_SIZE_EXPR might have been added in
5350 gimplify_modify_expr, so returning GS_OK would lead to an
5351 infinite loop. */
5352 changed = true;
5354 break;
5356 /* If we're initializing from a container, push the initialization
5357 inside it. */
5358 case CLEANUP_POINT_EXPR:
5359 case BIND_EXPR:
5360 case STATEMENT_LIST:
5362 tree wrap = *from_p;
5363 tree t;
5365 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5366 fb_lvalue);
5367 if (ret != GS_ERROR)
5368 ret = GS_OK;
5370 t = voidify_wrapper_expr (wrap, *expr_p);
5371 gcc_assert (t == *expr_p);
5373 if (want_value)
5375 gimplify_and_add (wrap, pre_p);
5376 *expr_p = unshare_expr (*to_p);
5378 else
5379 *expr_p = wrap;
5380 return GS_OK;
5383 case COMPOUND_LITERAL_EXPR:
5385 tree complit = TREE_OPERAND (*expr_p, 1);
5386 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5387 tree decl = DECL_EXPR_DECL (decl_s);
5388 tree init = DECL_INITIAL (decl);
5390 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5391 into struct T x = { 0, 1, 2 } if the address of the
5392 compound literal has never been taken. */
5393 if (!TREE_ADDRESSABLE (complit)
5394 && !TREE_ADDRESSABLE (decl)
5395 && init)
5397 *expr_p = copy_node (*expr_p);
5398 TREE_OPERAND (*expr_p, 1) = init;
5399 return GS_OK;
5403 default:
5404 break;
5407 while (changed);
5409 return ret;
5413 /* Return true if T looks like a valid GIMPLE statement. */
5415 static bool
5416 is_gimple_stmt (tree t)
5418 const enum tree_code code = TREE_CODE (t);
5420 switch (code)
5422 case NOP_EXPR:
5423 /* The only valid NOP_EXPR is the empty statement. */
5424 return IS_EMPTY_STMT (t);
5426 case BIND_EXPR:
5427 case COND_EXPR:
5428 /* These are only valid if they're void. */
5429 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5431 case SWITCH_EXPR:
5432 case GOTO_EXPR:
5433 case RETURN_EXPR:
5434 case LABEL_EXPR:
5435 case CASE_LABEL_EXPR:
5436 case TRY_CATCH_EXPR:
5437 case TRY_FINALLY_EXPR:
5438 case EH_FILTER_EXPR:
5439 case CATCH_EXPR:
5440 case ASM_EXPR:
5441 case STATEMENT_LIST:
5442 case OACC_PARALLEL:
5443 case OACC_KERNELS:
5444 case OACC_DATA:
5445 case OACC_HOST_DATA:
5446 case OACC_DECLARE:
5447 case OACC_UPDATE:
5448 case OACC_ENTER_DATA:
5449 case OACC_EXIT_DATA:
5450 case OACC_CACHE:
5451 case OMP_PARALLEL:
5452 case OMP_FOR:
5453 case OMP_SIMD:
5454 case OMP_DISTRIBUTE:
5455 case OACC_LOOP:
5456 case OMP_SECTIONS:
5457 case OMP_SECTION:
5458 case OMP_SINGLE:
5459 case OMP_MASTER:
5460 case OMP_TASKGROUP:
5461 case OMP_ORDERED:
5462 case OMP_CRITICAL:
5463 case OMP_TASK:
5464 case OMP_TARGET:
5465 case OMP_TARGET_DATA:
5466 case OMP_TARGET_UPDATE:
5467 case OMP_TARGET_ENTER_DATA:
5468 case OMP_TARGET_EXIT_DATA:
5469 case OMP_TASKLOOP:
5470 case OMP_TEAMS:
5471 /* These are always void. */
5472 return true;
5474 case CALL_EXPR:
5475 case MODIFY_EXPR:
5476 case PREDICT_EXPR:
5477 /* These are valid regardless of their type. */
5478 return true;
5480 default:
5481 return false;
5486 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5487 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5488 DECL_GIMPLE_REG_P set.
5490 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5491 other, unmodified part of the complex object just before the total store.
5492 As a consequence, if the object is still uninitialized, an undefined value
5493 will be loaded into a register, which may result in a spurious exception
5494 if the register is floating-point and the value happens to be a signaling
5495 NaN for example. Then the fully-fledged complex operations lowering pass
5496 followed by a DCE pass are necessary in order to fix things up. */
5498 static enum gimplify_status
5499 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5500 bool want_value)
5502 enum tree_code code, ocode;
5503 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5505 lhs = TREE_OPERAND (*expr_p, 0);
5506 rhs = TREE_OPERAND (*expr_p, 1);
5507 code = TREE_CODE (lhs);
5508 lhs = TREE_OPERAND (lhs, 0);
5510 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5511 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5512 TREE_NO_WARNING (other) = 1;
5513 other = get_formal_tmp_var (other, pre_p);
5515 realpart = code == REALPART_EXPR ? rhs : other;
5516 imagpart = code == REALPART_EXPR ? other : rhs;
5518 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5519 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5520 else
5521 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5523 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5524 *expr_p = (want_value) ? rhs : NULL_TREE;
5526 return GS_ALL_DONE;
5529 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5531 modify_expr
5532 : varname '=' rhs
5533 | '*' ID '=' rhs
5535 PRE_P points to the list where side effects that must happen before
5536 *EXPR_P should be stored.
5538 POST_P points to the list where side effects that must happen after
5539 *EXPR_P should be stored.
5541 WANT_VALUE is nonzero iff we want to use the value of this expression
5542 in another expression. */
5544 static enum gimplify_status
5545 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5546 bool want_value)
5548 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5549 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5550 enum gimplify_status ret = GS_UNHANDLED;
5551 gimple *assign;
5552 location_t loc = EXPR_LOCATION (*expr_p);
5553 gimple_stmt_iterator gsi;
5555 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5556 || TREE_CODE (*expr_p) == INIT_EXPR);
5558 /* Trying to simplify a clobber using normal logic doesn't work,
5559 so handle it here. */
5560 if (TREE_CLOBBER_P (*from_p))
5562 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5563 if (ret == GS_ERROR)
5564 return ret;
5565 gcc_assert (!want_value
5566 && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5567 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5568 *expr_p = NULL;
5569 return GS_ALL_DONE;
5572 /* Insert pointer conversions required by the middle-end that are not
5573 required by the frontend. This fixes middle-end type checking for
5574 for example gcc.dg/redecl-6.c. */
5575 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5577 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5578 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5579 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5582 /* See if any simplifications can be done based on what the RHS is. */
5583 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5584 want_value);
5585 if (ret != GS_UNHANDLED)
5586 return ret;
5588 /* For zero sized types only gimplify the left hand side and right hand
5589 side as statements and throw away the assignment. Do this after
5590 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5591 types properly. */
5592 if (zero_sized_type (TREE_TYPE (*from_p))
5593 && !want_value
5594 /* Don't do this for calls that return addressable types, expand_call
5595 relies on those having a lhs. */
5596 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5597 && TREE_CODE (*from_p) == CALL_EXPR))
5599 gimplify_stmt (from_p, pre_p);
5600 gimplify_stmt (to_p, pre_p);
5601 *expr_p = NULL_TREE;
5602 return GS_ALL_DONE;
5605 /* If the value being copied is of variable width, compute the length
5606 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5607 before gimplifying any of the operands so that we can resolve any
5608 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5609 the size of the expression to be copied, not of the destination, so
5610 that is what we must do here. */
5611 maybe_with_size_expr (from_p);
5613 /* As a special case, we have to temporarily allow for assignments
5614 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5615 a toplevel statement, when gimplifying the GENERIC expression
5616 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5617 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5619 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5620 prevent gimplify_expr from trying to create a new temporary for
5621 foo's LHS, we tell it that it should only gimplify until it
5622 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5623 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5624 and all we need to do here is set 'a' to be its LHS. */
5626 /* Gimplify the RHS first for C++17 and bug 71104. */
5627 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5628 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5629 if (ret == GS_ERROR)
5630 return ret;
5632 /* Then gimplify the LHS. */
5633 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5634 twice we have to make sure to gimplify into non-SSA as otherwise
5635 the abnormal edge added later will make those defs not dominate
5636 their uses.
5637 ??? Technically this applies only to the registers used in the
5638 resulting non-register *TO_P. */
5639 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5640 if (saved_into_ssa
5641 && TREE_CODE (*from_p) == CALL_EXPR
5642 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5643 gimplify_ctxp->into_ssa = false;
5644 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5645 gimplify_ctxp->into_ssa = saved_into_ssa;
5646 if (ret == GS_ERROR)
5647 return ret;
5649 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5650 guess for the predicate was wrong. */
5651 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5652 if (final_pred != initial_pred)
5654 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5655 if (ret == GS_ERROR)
5656 return ret;
5659 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5660 size as argument to the call. */
5661 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5663 tree call = TREE_OPERAND (*from_p, 0);
5664 tree vlasize = TREE_OPERAND (*from_p, 1);
5666 if (TREE_CODE (call) == CALL_EXPR
5667 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5669 int nargs = call_expr_nargs (call);
5670 tree type = TREE_TYPE (call);
5671 tree ap = CALL_EXPR_ARG (call, 0);
5672 tree tag = CALL_EXPR_ARG (call, 1);
5673 tree aptag = CALL_EXPR_ARG (call, 2);
5674 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5675 IFN_VA_ARG, type,
5676 nargs + 1, ap, tag,
5677 aptag, vlasize);
5678 TREE_OPERAND (*from_p, 0) = newcall;
5682 /* Now see if the above changed *from_p to something we handle specially. */
5683 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5684 want_value);
5685 if (ret != GS_UNHANDLED)
5686 return ret;
5688 /* If we've got a variable sized assignment between two lvalues (i.e. does
5689 not involve a call), then we can make things a bit more straightforward
5690 by converting the assignment to memcpy or memset. */
5691 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5693 tree from = TREE_OPERAND (*from_p, 0);
5694 tree size = TREE_OPERAND (*from_p, 1);
5696 if (TREE_CODE (from) == CONSTRUCTOR)
5697 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5699 if (is_gimple_addressable (from))
5701 *from_p = from;
5702 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5703 pre_p);
5707 /* Transform partial stores to non-addressable complex variables into
5708 total stores. This allows us to use real instead of virtual operands
5709 for these variables, which improves optimization. */
5710 if ((TREE_CODE (*to_p) == REALPART_EXPR
5711 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5712 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5713 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5715 /* Try to alleviate the effects of the gimplification creating artificial
5716 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5717 make sure not to create DECL_DEBUG_EXPR links across functions. */
5718 if (!gimplify_ctxp->into_ssa
5719 && VAR_P (*from_p)
5720 && DECL_IGNORED_P (*from_p)
5721 && DECL_P (*to_p)
5722 && !DECL_IGNORED_P (*to_p)
5723 && decl_function_context (*to_p) == current_function_decl
5724 && decl_function_context (*from_p) == current_function_decl)
5726 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5727 DECL_NAME (*from_p)
5728 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5729 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5730 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5733 if (want_value && TREE_THIS_VOLATILE (*to_p))
5734 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5736 if (TREE_CODE (*from_p) == CALL_EXPR)
5738 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5739 instead of a GIMPLE_ASSIGN. */
5740 gcall *call_stmt;
5741 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5743 /* Gimplify internal functions created in the FEs. */
5744 int nargs = call_expr_nargs (*from_p), i;
5745 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5746 auto_vec<tree> vargs (nargs);
5748 for (i = 0; i < nargs; i++)
5750 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5751 EXPR_LOCATION (*from_p));
5752 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5754 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5755 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5756 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5758 else
5760 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5761 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5762 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5763 tree fndecl = get_callee_fndecl (*from_p);
5764 if (fndecl
5765 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5766 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5767 && call_expr_nargs (*from_p) == 3)
5768 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5769 CALL_EXPR_ARG (*from_p, 0),
5770 CALL_EXPR_ARG (*from_p, 1),
5771 CALL_EXPR_ARG (*from_p, 2));
5772 else
5774 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5777 notice_special_calls (call_stmt);
5778 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5779 gimple_call_set_lhs (call_stmt, *to_p);
5780 else if (TREE_CODE (*to_p) == SSA_NAME)
5781 /* The above is somewhat premature, avoid ICEing later for a
5782 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5783 ??? This doesn't make it a default-def. */
5784 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5786 assign = call_stmt;
5788 else
5790 assign = gimple_build_assign (*to_p, *from_p);
5791 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5792 if (COMPARISON_CLASS_P (*from_p))
5793 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5796 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5798 /* We should have got an SSA name from the start. */
5799 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5800 || ! gimple_in_ssa_p (cfun));
5803 gimplify_seq_add_stmt (pre_p, assign);
5804 gsi = gsi_last (*pre_p);
5805 maybe_fold_stmt (&gsi);
5807 if (want_value)
5809 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5810 return GS_OK;
5812 else
5813 *expr_p = NULL;
5815 return GS_ALL_DONE;
5818 /* Gimplify a comparison between two variable-sized objects. Do this
5819 with a call to BUILT_IN_MEMCMP. */
5821 static enum gimplify_status
5822 gimplify_variable_sized_compare (tree *expr_p)
5824 location_t loc = EXPR_LOCATION (*expr_p);
5825 tree op0 = TREE_OPERAND (*expr_p, 0);
5826 tree op1 = TREE_OPERAND (*expr_p, 1);
5827 tree t, arg, dest, src, expr;
5829 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5830 arg = unshare_expr (arg);
5831 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5832 src = build_fold_addr_expr_loc (loc, op1);
5833 dest = build_fold_addr_expr_loc (loc, op0);
5834 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5835 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5837 expr
5838 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5839 SET_EXPR_LOCATION (expr, loc);
5840 *expr_p = expr;
5842 return GS_OK;
5845 /* Gimplify a comparison between two aggregate objects of integral scalar
5846 mode as a comparison between the bitwise equivalent scalar values. */
5848 static enum gimplify_status
5849 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5851 location_t loc = EXPR_LOCATION (*expr_p);
5852 tree op0 = TREE_OPERAND (*expr_p, 0);
5853 tree op1 = TREE_OPERAND (*expr_p, 1);
5855 tree type = TREE_TYPE (op0);
5856 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5858 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5859 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5861 *expr_p
5862 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5864 return GS_OK;
5867 /* Gimplify an expression sequence. This function gimplifies each
5868 expression and rewrites the original expression with the last
5869 expression of the sequence in GIMPLE form.
5871 PRE_P points to the list where the side effects for all the
5872 expressions in the sequence will be emitted.
5874 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5876 static enum gimplify_status
5877 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5879 tree t = *expr_p;
5883 tree *sub_p = &TREE_OPERAND (t, 0);
5885 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5886 gimplify_compound_expr (sub_p, pre_p, false);
5887 else
5888 gimplify_stmt (sub_p, pre_p);
5890 t = TREE_OPERAND (t, 1);
5892 while (TREE_CODE (t) == COMPOUND_EXPR);
5894 *expr_p = t;
5895 if (want_value)
5896 return GS_OK;
5897 else
5899 gimplify_stmt (expr_p, pre_p);
5900 return GS_ALL_DONE;
5904 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5905 gimplify. After gimplification, EXPR_P will point to a new temporary
5906 that holds the original value of the SAVE_EXPR node.
5908 PRE_P points to the list where side effects that must happen before
5909 *EXPR_P should be stored. */
5911 static enum gimplify_status
5912 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5914 enum gimplify_status ret = GS_ALL_DONE;
5915 tree val;
5917 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5918 val = TREE_OPERAND (*expr_p, 0);
5920 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5921 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5923 /* The operand may be a void-valued expression. It is
5924 being executed only for its side-effects. */
5925 if (TREE_TYPE (val) == void_type_node)
5927 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5928 is_gimple_stmt, fb_none);
5929 val = NULL;
5931 else
5932 /* The temporary may not be an SSA name as later abnormal and EH
5933 control flow may invalidate use/def domination. */
5934 val = get_initialized_tmp_var (val, pre_p, post_p, false);
5936 TREE_OPERAND (*expr_p, 0) = val;
5937 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5940 *expr_p = val;
5942 return ret;
5945 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5947 unary_expr
5948 : ...
5949 | '&' varname
5952 PRE_P points to the list where side effects that must happen before
5953 *EXPR_P should be stored.
5955 POST_P points to the list where side effects that must happen after
5956 *EXPR_P should be stored. */
5958 static enum gimplify_status
5959 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5961 tree expr = *expr_p;
5962 tree op0 = TREE_OPERAND (expr, 0);
5963 enum gimplify_status ret;
5964 location_t loc = EXPR_LOCATION (*expr_p);
5966 switch (TREE_CODE (op0))
5968 case INDIRECT_REF:
5969 do_indirect_ref:
5970 /* Check if we are dealing with an expression of the form '&*ptr'.
5971 While the front end folds away '&*ptr' into 'ptr', these
5972 expressions may be generated internally by the compiler (e.g.,
5973 builtins like __builtin_va_end). */
5974 /* Caution: the silent array decomposition semantics we allow for
5975 ADDR_EXPR means we can't always discard the pair. */
5976 /* Gimplification of the ADDR_EXPR operand may drop
5977 cv-qualification conversions, so make sure we add them if
5978 needed. */
5980 tree op00 = TREE_OPERAND (op0, 0);
5981 tree t_expr = TREE_TYPE (expr);
5982 tree t_op00 = TREE_TYPE (op00);
5984 if (!useless_type_conversion_p (t_expr, t_op00))
5985 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5986 *expr_p = op00;
5987 ret = GS_OK;
5989 break;
5991 case VIEW_CONVERT_EXPR:
5992 /* Take the address of our operand and then convert it to the type of
5993 this ADDR_EXPR.
5995 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5996 all clear. The impact of this transformation is even less clear. */
5998 /* If the operand is a useless conversion, look through it. Doing so
5999 guarantees that the ADDR_EXPR and its operand will remain of the
6000 same type. */
6001 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
6002 op0 = TREE_OPERAND (op0, 0);
6004 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6005 build_fold_addr_expr_loc (loc,
6006 TREE_OPERAND (op0, 0)));
6007 ret = GS_OK;
6008 break;
6010 case MEM_REF:
6011 if (integer_zerop (TREE_OPERAND (op0, 1)))
6012 goto do_indirect_ref;
6014 /* fall through */
6016 default:
6017 /* If we see a call to a declared builtin or see its address
6018 being taken (we can unify those cases here) then we can mark
6019 the builtin for implicit generation by GCC. */
6020 if (TREE_CODE (op0) == FUNCTION_DECL
6021 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
6022 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6023 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6025 /* We use fb_either here because the C frontend sometimes takes
6026 the address of a call that returns a struct; see
6027 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6028 the implied temporary explicit. */
6030 /* Make the operand addressable. */
6031 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6032 is_gimple_addressable, fb_either);
6033 if (ret == GS_ERROR)
6034 break;
6036 /* Then mark it. Beware that it may not be possible to do so directly
6037 if a temporary has been created by the gimplification. */
6038 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6040 op0 = TREE_OPERAND (expr, 0);
6042 /* For various reasons, the gimplification of the expression
6043 may have made a new INDIRECT_REF. */
6044 if (TREE_CODE (op0) == INDIRECT_REF)
6045 goto do_indirect_ref;
6047 mark_addressable (TREE_OPERAND (expr, 0));
6049 /* The FEs may end up building ADDR_EXPRs early on a decl with
6050 an incomplete type. Re-build ADDR_EXPRs in canonical form
6051 here. */
6052 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6053 *expr_p = build_fold_addr_expr (op0);
6055 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6056 recompute_tree_invariant_for_addr_expr (*expr_p);
6058 /* If we re-built the ADDR_EXPR add a conversion to the original type
6059 if required. */
6060 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6061 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6063 break;
6066 return ret;
6069 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6070 value; output operands should be a gimple lvalue. */
6072 static enum gimplify_status
6073 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6075 tree expr;
6076 int noutputs;
6077 const char **oconstraints;
6078 int i;
6079 tree link;
6080 const char *constraint;
6081 bool allows_mem, allows_reg, is_inout;
6082 enum gimplify_status ret, tret;
6083 gasm *stmt;
6084 vec<tree, va_gc> *inputs;
6085 vec<tree, va_gc> *outputs;
6086 vec<tree, va_gc> *clobbers;
6087 vec<tree, va_gc> *labels;
6088 tree link_next;
6090 expr = *expr_p;
6091 noutputs = list_length (ASM_OUTPUTS (expr));
6092 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6094 inputs = NULL;
6095 outputs = NULL;
6096 clobbers = NULL;
6097 labels = NULL;
6099 ret = GS_ALL_DONE;
6100 link_next = NULL_TREE;
6101 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6103 bool ok;
6104 size_t constraint_len;
6106 link_next = TREE_CHAIN (link);
6108 oconstraints[i]
6109 = constraint
6110 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6111 constraint_len = strlen (constraint);
6112 if (constraint_len == 0)
6113 continue;
6115 ok = parse_output_constraint (&constraint, i, 0, 0,
6116 &allows_mem, &allows_reg, &is_inout);
6117 if (!ok)
6119 ret = GS_ERROR;
6120 is_inout = false;
6123 if (!allows_reg && allows_mem)
6124 mark_addressable (TREE_VALUE (link));
6126 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6127 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6128 fb_lvalue | fb_mayfail);
6129 if (tret == GS_ERROR)
6131 error ("invalid lvalue in asm output %d", i);
6132 ret = tret;
6135 /* If the constraint does not allow memory make sure we gimplify
6136 it to a register if it is not already but its base is. This
6137 happens for complex and vector components. */
6138 if (!allows_mem)
6140 tree op = TREE_VALUE (link);
6141 if (! is_gimple_val (op)
6142 && is_gimple_reg_type (TREE_TYPE (op))
6143 && is_gimple_reg (get_base_address (op)))
6145 tree tem = create_tmp_reg (TREE_TYPE (op));
6146 tree ass;
6147 if (is_inout)
6149 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6150 tem, unshare_expr (op));
6151 gimplify_and_add (ass, pre_p);
6153 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6154 gimplify_and_add (ass, post_p);
6156 TREE_VALUE (link) = tem;
6157 tret = GS_OK;
6161 vec_safe_push (outputs, link);
6162 TREE_CHAIN (link) = NULL_TREE;
6164 if (is_inout)
6166 /* An input/output operand. To give the optimizers more
6167 flexibility, split it into separate input and output
6168 operands. */
6169 tree input;
6170 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6171 char buf[11];
6173 /* Turn the in/out constraint into an output constraint. */
6174 char *p = xstrdup (constraint);
6175 p[0] = '=';
6176 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6178 /* And add a matching input constraint. */
6179 if (allows_reg)
6181 sprintf (buf, "%u", i);
6183 /* If there are multiple alternatives in the constraint,
6184 handle each of them individually. Those that allow register
6185 will be replaced with operand number, the others will stay
6186 unchanged. */
6187 if (strchr (p, ',') != NULL)
6189 size_t len = 0, buflen = strlen (buf);
6190 char *beg, *end, *str, *dst;
6192 for (beg = p + 1;;)
6194 end = strchr (beg, ',');
6195 if (end == NULL)
6196 end = strchr (beg, '\0');
6197 if ((size_t) (end - beg) < buflen)
6198 len += buflen + 1;
6199 else
6200 len += end - beg + 1;
6201 if (*end)
6202 beg = end + 1;
6203 else
6204 break;
6207 str = (char *) alloca (len);
6208 for (beg = p + 1, dst = str;;)
6210 const char *tem;
6211 bool mem_p, reg_p, inout_p;
6213 end = strchr (beg, ',');
6214 if (end)
6215 *end = '\0';
6216 beg[-1] = '=';
6217 tem = beg - 1;
6218 parse_output_constraint (&tem, i, 0, 0,
6219 &mem_p, &reg_p, &inout_p);
6220 if (dst != str)
6221 *dst++ = ',';
6222 if (reg_p)
6224 memcpy (dst, buf, buflen);
6225 dst += buflen;
6227 else
6229 if (end)
6230 len = end - beg;
6231 else
6232 len = strlen (beg);
6233 memcpy (dst, beg, len);
6234 dst += len;
6236 if (end)
6237 beg = end + 1;
6238 else
6239 break;
6241 *dst = '\0';
6242 input = build_string (dst - str, str);
6244 else
6245 input = build_string (strlen (buf), buf);
6247 else
6248 input = build_string (constraint_len - 1, constraint + 1);
6250 free (p);
6252 input = build_tree_list (build_tree_list (NULL_TREE, input),
6253 unshare_expr (TREE_VALUE (link)));
6254 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6258 link_next = NULL_TREE;
6259 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6261 link_next = TREE_CHAIN (link);
6262 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6263 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6264 oconstraints, &allows_mem, &allows_reg);
6266 /* If we can't make copies, we can only accept memory. */
6267 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6269 if (allows_mem)
6270 allows_reg = 0;
6271 else
6273 error ("impossible constraint in %<asm%>");
6274 error ("non-memory input %d must stay in memory", i);
6275 return GS_ERROR;
6279 /* If the operand is a memory input, it should be an lvalue. */
6280 if (!allows_reg && allows_mem)
6282 tree inputv = TREE_VALUE (link);
6283 STRIP_NOPS (inputv);
6284 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6285 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6286 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6287 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6288 || TREE_CODE (inputv) == MODIFY_EXPR)
6289 TREE_VALUE (link) = error_mark_node;
6290 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6291 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6292 if (tret != GS_ERROR)
6294 /* Unlike output operands, memory inputs are not guaranteed
6295 to be lvalues by the FE, and while the expressions are
6296 marked addressable there, if it is e.g. a statement
6297 expression, temporaries in it might not end up being
6298 addressable. They might be already used in the IL and thus
6299 it is too late to make them addressable now though. */
6300 tree x = TREE_VALUE (link);
6301 while (handled_component_p (x))
6302 x = TREE_OPERAND (x, 0);
6303 if (TREE_CODE (x) == MEM_REF
6304 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6305 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6306 if ((VAR_P (x)
6307 || TREE_CODE (x) == PARM_DECL
6308 || TREE_CODE (x) == RESULT_DECL)
6309 && !TREE_ADDRESSABLE (x)
6310 && is_gimple_reg (x))
6312 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6313 input_location), 0,
6314 "memory input %d is not directly addressable",
6316 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6319 mark_addressable (TREE_VALUE (link));
6320 if (tret == GS_ERROR)
6322 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6323 "memory input %d is not directly addressable", i);
6324 ret = tret;
6327 else
6329 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6330 is_gimple_asm_val, fb_rvalue);
6331 if (tret == GS_ERROR)
6332 ret = tret;
6335 TREE_CHAIN (link) = NULL_TREE;
6336 vec_safe_push (inputs, link);
6339 link_next = NULL_TREE;
6340 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6342 link_next = TREE_CHAIN (link);
6343 TREE_CHAIN (link) = NULL_TREE;
6344 vec_safe_push (clobbers, link);
6347 link_next = NULL_TREE;
6348 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6350 link_next = TREE_CHAIN (link);
6351 TREE_CHAIN (link) = NULL_TREE;
6352 vec_safe_push (labels, link);
6355 /* Do not add ASMs with errors to the gimple IL stream. */
6356 if (ret != GS_ERROR)
6358 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6359 inputs, outputs, clobbers, labels);
6361 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6362 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6364 gimplify_seq_add_stmt (pre_p, stmt);
6367 return ret;
6370 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6371 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6372 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6373 return to this function.
6375 FIXME should we complexify the prequeue handling instead? Or use flags
6376 for all the cleanups and let the optimizer tighten them up? The current
6377 code seems pretty fragile; it will break on a cleanup within any
6378 non-conditional nesting. But any such nesting would be broken, anyway;
6379 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6380 and continues out of it. We can do that at the RTL level, though, so
6381 having an optimizer to tighten up try/finally regions would be a Good
6382 Thing. */
6384 static enum gimplify_status
6385 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6387 gimple_stmt_iterator iter;
6388 gimple_seq body_sequence = NULL;
6390 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6392 /* We only care about the number of conditions between the innermost
6393 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6394 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6395 int old_conds = gimplify_ctxp->conditions;
6396 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6397 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6398 gimplify_ctxp->conditions = 0;
6399 gimplify_ctxp->conditional_cleanups = NULL;
6400 gimplify_ctxp->in_cleanup_point_expr = true;
6402 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6404 gimplify_ctxp->conditions = old_conds;
6405 gimplify_ctxp->conditional_cleanups = old_cleanups;
6406 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6408 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6410 gimple *wce = gsi_stmt (iter);
6412 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6414 if (gsi_one_before_end_p (iter))
6416 /* Note that gsi_insert_seq_before and gsi_remove do not
6417 scan operands, unlike some other sequence mutators. */
6418 if (!gimple_wce_cleanup_eh_only (wce))
6419 gsi_insert_seq_before_without_update (&iter,
6420 gimple_wce_cleanup (wce),
6421 GSI_SAME_STMT);
6422 gsi_remove (&iter, true);
6423 break;
6425 else
6427 gtry *gtry;
6428 gimple_seq seq;
6429 enum gimple_try_flags kind;
6431 if (gimple_wce_cleanup_eh_only (wce))
6432 kind = GIMPLE_TRY_CATCH;
6433 else
6434 kind = GIMPLE_TRY_FINALLY;
6435 seq = gsi_split_seq_after (iter);
6437 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6438 /* Do not use gsi_replace here, as it may scan operands.
6439 We want to do a simple structural modification only. */
6440 gsi_set_stmt (&iter, gtry);
6441 iter = gsi_start (gtry->eval);
6444 else
6445 gsi_next (&iter);
6448 gimplify_seq_add_seq (pre_p, body_sequence);
6449 if (temp)
6451 *expr_p = temp;
6452 return GS_OK;
6454 else
6456 *expr_p = NULL;
6457 return GS_ALL_DONE;
6461 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6462 is the cleanup action required. EH_ONLY is true if the cleanup should
6463 only be executed if an exception is thrown, not on normal exit.
6464 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6465 only valid for clobbers. */
6467 static void
6468 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6469 bool force_uncond = false)
6471 gimple *wce;
6472 gimple_seq cleanup_stmts = NULL;
6474 /* Errors can result in improperly nested cleanups. Which results in
6475 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6476 if (seen_error ())
6477 return;
6479 if (gimple_conditional_context ())
6481 /* If we're in a conditional context, this is more complex. We only
6482 want to run the cleanup if we actually ran the initialization that
6483 necessitates it, but we want to run it after the end of the
6484 conditional context. So we wrap the try/finally around the
6485 condition and use a flag to determine whether or not to actually
6486 run the destructor. Thus
6488 test ? f(A()) : 0
6490 becomes (approximately)
6492 flag = 0;
6493 try {
6494 if (test) { A::A(temp); flag = 1; val = f(temp); }
6495 else { val = 0; }
6496 } finally {
6497 if (flag) A::~A(temp);
6501 if (force_uncond)
6503 gimplify_stmt (&cleanup, &cleanup_stmts);
6504 wce = gimple_build_wce (cleanup_stmts);
6505 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6507 else
6509 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6510 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6511 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6513 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6514 gimplify_stmt (&cleanup, &cleanup_stmts);
6515 wce = gimple_build_wce (cleanup_stmts);
6517 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6518 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6519 gimplify_seq_add_stmt (pre_p, ftrue);
6521 /* Because of this manipulation, and the EH edges that jump
6522 threading cannot redirect, the temporary (VAR) will appear
6523 to be used uninitialized. Don't warn. */
6524 TREE_NO_WARNING (var) = 1;
6527 else
6529 gimplify_stmt (&cleanup, &cleanup_stmts);
6530 wce = gimple_build_wce (cleanup_stmts);
6531 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6532 gimplify_seq_add_stmt (pre_p, wce);
6536 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6538 static enum gimplify_status
6539 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6541 tree targ = *expr_p;
6542 tree temp = TARGET_EXPR_SLOT (targ);
6543 tree init = TARGET_EXPR_INITIAL (targ);
6544 enum gimplify_status ret;
6546 bool unpoison_empty_seq = false;
6547 gimple_stmt_iterator unpoison_it;
6549 if (init)
6551 tree cleanup = NULL_TREE;
6553 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6554 to the temps list. Handle also variable length TARGET_EXPRs. */
6555 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6557 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6558 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6559 gimplify_vla_decl (temp, pre_p);
6561 else
6563 /* Save location where we need to place unpoisoning. It's possible
6564 that a variable will be converted to needs_to_live_in_memory. */
6565 unpoison_it = gsi_last (*pre_p);
6566 unpoison_empty_seq = gsi_end_p (unpoison_it);
6568 gimple_add_tmp_var (temp);
6571 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6572 expression is supposed to initialize the slot. */
6573 if (VOID_TYPE_P (TREE_TYPE (init)))
6574 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6575 else
6577 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6578 init = init_expr;
6579 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6580 init = NULL;
6581 ggc_free (init_expr);
6583 if (ret == GS_ERROR)
6585 /* PR c++/28266 Make sure this is expanded only once. */
6586 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6587 return GS_ERROR;
6589 if (init)
6590 gimplify_and_add (init, pre_p);
6592 /* If needed, push the cleanup for the temp. */
6593 if (TARGET_EXPR_CLEANUP (targ))
6595 if (CLEANUP_EH_ONLY (targ))
6596 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6597 CLEANUP_EH_ONLY (targ), pre_p);
6598 else
6599 cleanup = TARGET_EXPR_CLEANUP (targ);
6602 /* Add a clobber for the temporary going out of scope, like
6603 gimplify_bind_expr. */
6604 if (gimplify_ctxp->in_cleanup_point_expr
6605 && needs_to_live_in_memory (temp))
6607 if (flag_stack_reuse == SR_ALL)
6609 tree clobber = build_constructor (TREE_TYPE (temp),
6610 NULL);
6611 TREE_THIS_VOLATILE (clobber) = true;
6612 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6613 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6615 if (asan_poisoned_variables
6616 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6617 && dbg_cnt (asan_use_after_scope))
6619 tree asan_cleanup = build_asan_poison_call_expr (temp);
6620 if (asan_cleanup)
6622 if (unpoison_empty_seq)
6623 unpoison_it = gsi_start (*pre_p);
6625 asan_poison_variable (temp, false, &unpoison_it,
6626 unpoison_empty_seq);
6627 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6631 if (cleanup)
6632 gimple_push_cleanup (temp, cleanup, false, pre_p);
6634 /* Only expand this once. */
6635 TREE_OPERAND (targ, 3) = init;
6636 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6638 else
6639 /* We should have expanded this before. */
6640 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6642 *expr_p = temp;
6643 return GS_OK;
6646 /* Gimplification of expression trees. */
6648 /* Gimplify an expression which appears at statement context. The
6649 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6650 NULL, a new sequence is allocated.
6652 Return true if we actually added a statement to the queue. */
6654 bool
6655 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6657 gimple_seq_node last;
6659 last = gimple_seq_last (*seq_p);
6660 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6661 return last != gimple_seq_last (*seq_p);
6664 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6665 to CTX. If entries already exist, force them to be some flavor of private.
6666 If there is no enclosing parallel, do nothing. */
6668 void
6669 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6671 splay_tree_node n;
6673 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6674 return;
6678 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6679 if (n != NULL)
6681 if (n->value & GOVD_SHARED)
6682 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6683 else if (n->value & GOVD_MAP)
6684 n->value |= GOVD_MAP_TO_ONLY;
6685 else
6686 return;
6688 else if ((ctx->region_type & ORT_TARGET) != 0)
6690 if (ctx->target_map_scalars_firstprivate)
6691 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6692 else
6693 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6695 else if (ctx->region_type != ORT_WORKSHARE
6696 && ctx->region_type != ORT_SIMD
6697 && ctx->region_type != ORT_ACC
6698 && !(ctx->region_type & ORT_TARGET_DATA))
6699 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6701 ctx = ctx->outer_context;
6703 while (ctx);
6706 /* Similarly for each of the type sizes of TYPE. */
6708 static void
6709 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6711 if (type == NULL || type == error_mark_node)
6712 return;
6713 type = TYPE_MAIN_VARIANT (type);
6715 if (ctx->privatized_types->add (type))
6716 return;
6718 switch (TREE_CODE (type))
6720 case INTEGER_TYPE:
6721 case ENUMERAL_TYPE:
6722 case BOOLEAN_TYPE:
6723 case REAL_TYPE:
6724 case FIXED_POINT_TYPE:
6725 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6726 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6727 break;
6729 case ARRAY_TYPE:
6730 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6731 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6732 break;
6734 case RECORD_TYPE:
6735 case UNION_TYPE:
6736 case QUAL_UNION_TYPE:
6738 tree field;
6739 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6740 if (TREE_CODE (field) == FIELD_DECL)
6742 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6743 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6746 break;
6748 case POINTER_TYPE:
6749 case REFERENCE_TYPE:
6750 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6751 break;
6753 default:
6754 break;
6757 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6758 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6759 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6762 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6764 static void
6765 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6767 splay_tree_node n;
6768 unsigned int nflags;
6769 tree t;
6771 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6772 return;
6774 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6775 there are constructors involved somewhere. Exception is a shared clause,
6776 there is nothing privatized in that case. */
6777 if ((flags & GOVD_SHARED) == 0
6778 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6779 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6780 flags |= GOVD_SEEN;
6782 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6783 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6785 /* We shouldn't be re-adding the decl with the same data
6786 sharing class. */
6787 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6788 nflags = n->value | flags;
6789 /* The only combination of data sharing classes we should see is
6790 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6791 reduction variables to be used in data sharing clauses. */
6792 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6793 || ((nflags & GOVD_DATA_SHARE_CLASS)
6794 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6795 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6796 n->value = nflags;
6797 return;
6800 /* When adding a variable-sized variable, we have to handle all sorts
6801 of additional bits of data: the pointer replacement variable, and
6802 the parameters of the type. */
6803 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6805 /* Add the pointer replacement variable as PRIVATE if the variable
6806 replacement is private, else FIRSTPRIVATE since we'll need the
6807 address of the original variable either for SHARED, or for the
6808 copy into or out of the context. */
6809 if (!(flags & GOVD_LOCAL))
6811 if (flags & GOVD_MAP)
6812 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6813 else if (flags & GOVD_PRIVATE)
6814 nflags = GOVD_PRIVATE;
6815 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6816 && (flags & GOVD_FIRSTPRIVATE))
6817 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6818 else
6819 nflags = GOVD_FIRSTPRIVATE;
6820 nflags |= flags & GOVD_SEEN;
6821 t = DECL_VALUE_EXPR (decl);
6822 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6823 t = TREE_OPERAND (t, 0);
6824 gcc_assert (DECL_P (t));
6825 omp_add_variable (ctx, t, nflags);
6828 /* Add all of the variable and type parameters (which should have
6829 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6830 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6831 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6832 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6834 /* The variable-sized variable itself is never SHARED, only some form
6835 of PRIVATE. The sharing would take place via the pointer variable
6836 which we remapped above. */
6837 if (flags & GOVD_SHARED)
6838 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6839 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6841 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6842 alloca statement we generate for the variable, so make sure it
6843 is available. This isn't automatically needed for the SHARED
6844 case, since we won't be allocating local storage then.
6845 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6846 in this case omp_notice_variable will be called later
6847 on when it is gimplified. */
6848 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6849 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6850 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6852 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6853 && lang_hooks.decls.omp_privatize_by_reference (decl))
6855 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6857 /* Similar to the direct variable sized case above, we'll need the
6858 size of references being privatized. */
6859 if ((flags & GOVD_SHARED) == 0)
6861 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6862 if (DECL_P (t))
6863 omp_notice_variable (ctx, t, true);
6867 if (n != NULL)
6868 n->value |= flags;
6869 else
6870 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6872 /* For reductions clauses in OpenACC loop directives, by default create a
6873 copy clause on the enclosing parallel construct for carrying back the
6874 results. */
6875 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6877 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6878 while (outer_ctx)
6880 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6881 if (n != NULL)
6883 /* Ignore local variables and explicitly declared clauses. */
6884 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6885 break;
6886 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6888 /* According to the OpenACC spec, such a reduction variable
6889 should already have a copy map on a kernels construct,
6890 verify that here. */
6891 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6892 && (n->value & GOVD_MAP));
6894 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6896 /* Remove firstprivate and make it a copy map. */
6897 n->value &= ~GOVD_FIRSTPRIVATE;
6898 n->value |= GOVD_MAP;
6901 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6903 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6904 GOVD_MAP | GOVD_SEEN);
6905 break;
6907 outer_ctx = outer_ctx->outer_context;
6912 /* Notice a threadprivate variable DECL used in OMP context CTX.
6913 This just prints out diagnostics about threadprivate variable uses
6914 in untied tasks. If DECL2 is non-NULL, prevent this warning
6915 on that variable. */
6917 static bool
6918 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6919 tree decl2)
6921 splay_tree_node n;
6922 struct gimplify_omp_ctx *octx;
6924 for (octx = ctx; octx; octx = octx->outer_context)
6925 if ((octx->region_type & ORT_TARGET) != 0)
6927 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6928 if (n == NULL)
6930 error ("threadprivate variable %qE used in target region",
6931 DECL_NAME (decl));
6932 error_at (octx->location, "enclosing target region");
6933 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6935 if (decl2)
6936 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6939 if (ctx->region_type != ORT_UNTIED_TASK)
6940 return false;
6941 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6942 if (n == NULL)
6944 error ("threadprivate variable %qE used in untied task",
6945 DECL_NAME (decl));
6946 error_at (ctx->location, "enclosing task");
6947 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6949 if (decl2)
6950 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6951 return false;
6954 /* Return true if global var DECL is device resident. */
6956 static bool
6957 device_resident_p (tree decl)
6959 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6961 if (!attr)
6962 return false;
6964 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6966 tree c = TREE_VALUE (t);
6967 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6968 return true;
6971 return false;
6974 /* Return true if DECL has an ACC DECLARE attribute. */
6976 static bool
6977 is_oacc_declared (tree decl)
6979 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6980 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6981 return declared != NULL_TREE;
6984 /* Determine outer default flags for DECL mentioned in an OMP region
6985 but not declared in an enclosing clause.
6987 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6988 remapped firstprivate instead of shared. To some extent this is
6989 addressed in omp_firstprivatize_type_sizes, but not
6990 effectively. */
6992 static unsigned
6993 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6994 bool in_code, unsigned flags)
6996 enum omp_clause_default_kind default_kind = ctx->default_kind;
6997 enum omp_clause_default_kind kind;
6999 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7000 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7001 default_kind = kind;
7003 switch (default_kind)
7005 case OMP_CLAUSE_DEFAULT_NONE:
7007 const char *rtype;
7009 if (ctx->region_type & ORT_PARALLEL)
7010 rtype = "parallel";
7011 else if (ctx->region_type & ORT_TASK)
7012 rtype = "task";
7013 else if (ctx->region_type & ORT_TEAMS)
7014 rtype = "teams";
7015 else
7016 gcc_unreachable ();
7018 error ("%qE not specified in enclosing %qs",
7019 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7020 error_at (ctx->location, "enclosing %qs", rtype);
7022 /* FALLTHRU */
7023 case OMP_CLAUSE_DEFAULT_SHARED:
7024 flags |= GOVD_SHARED;
7025 break;
7026 case OMP_CLAUSE_DEFAULT_PRIVATE:
7027 flags |= GOVD_PRIVATE;
7028 break;
7029 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7030 flags |= GOVD_FIRSTPRIVATE;
7031 break;
7032 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7033 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7034 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7035 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7037 omp_notice_variable (octx, decl, in_code);
7038 for (; octx; octx = octx->outer_context)
7040 splay_tree_node n2;
7042 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7043 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7044 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7045 continue;
7046 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7048 flags |= GOVD_FIRSTPRIVATE;
7049 goto found_outer;
7051 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7053 flags |= GOVD_SHARED;
7054 goto found_outer;
7059 if (TREE_CODE (decl) == PARM_DECL
7060 || (!is_global_var (decl)
7061 && DECL_CONTEXT (decl) == current_function_decl))
7062 flags |= GOVD_FIRSTPRIVATE;
7063 else
7064 flags |= GOVD_SHARED;
7065 found_outer:
7066 break;
7068 default:
7069 gcc_unreachable ();
7072 return flags;
7076 /* Determine outer default flags for DECL mentioned in an OACC region
7077 but not declared in an enclosing clause. */
7079 static unsigned
7080 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7082 const char *rkind;
7083 bool on_device = false;
7084 bool declared = is_oacc_declared (decl);
7085 tree type = TREE_TYPE (decl);
7087 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7088 type = TREE_TYPE (type);
7090 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7091 && is_global_var (decl)
7092 && device_resident_p (decl))
7094 on_device = true;
7095 flags |= GOVD_MAP_TO_ONLY;
7098 switch (ctx->region_type)
7100 case ORT_ACC_KERNELS:
7101 rkind = "kernels";
7103 if (AGGREGATE_TYPE_P (type))
7105 /* Aggregates default to 'present_or_copy', or 'present'. */
7106 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7107 flags |= GOVD_MAP;
7108 else
7109 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7111 else
7112 /* Scalars default to 'copy'. */
7113 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7115 break;
7117 case ORT_ACC_PARALLEL:
7118 rkind = "parallel";
7120 if (on_device || declared)
7121 flags |= GOVD_MAP;
7122 else if (AGGREGATE_TYPE_P (type))
7124 /* Aggregates default to 'present_or_copy', or 'present'. */
7125 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7126 flags |= GOVD_MAP;
7127 else
7128 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7130 else
7131 /* Scalars default to 'firstprivate'. */
7132 flags |= GOVD_FIRSTPRIVATE;
7134 break;
7136 default:
7137 gcc_unreachable ();
7140 if (DECL_ARTIFICIAL (decl))
7141 ; /* We can get compiler-generated decls, and should not complain
7142 about them. */
7143 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7145 error ("%qE not specified in enclosing OpenACC %qs construct",
7146 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7147 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7149 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7150 ; /* Handled above. */
7151 else
7152 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7154 return flags;
7157 /* Record the fact that DECL was used within the OMP context CTX.
7158 IN_CODE is true when real code uses DECL, and false when we should
7159 merely emit default(none) errors. Return true if DECL is going to
7160 be remapped and thus DECL shouldn't be gimplified into its
7161 DECL_VALUE_EXPR (if any). */
7163 static bool
7164 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7166 splay_tree_node n;
7167 unsigned flags = in_code ? GOVD_SEEN : 0;
7168 bool ret = false, shared;
7170 if (error_operand_p (decl))
7171 return false;
7173 if (ctx->region_type == ORT_NONE)
7174 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7176 if (is_global_var (decl))
7178 /* Threadprivate variables are predetermined. */
7179 if (DECL_THREAD_LOCAL_P (decl))
7180 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7182 if (DECL_HAS_VALUE_EXPR_P (decl))
7184 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7186 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7187 return omp_notice_threadprivate_variable (ctx, decl, value);
7190 if (gimplify_omp_ctxp->outer_context == NULL
7191 && VAR_P (decl)
7192 && oacc_get_fn_attrib (current_function_decl))
7194 location_t loc = DECL_SOURCE_LOCATION (decl);
7196 if (lookup_attribute ("omp declare target link",
7197 DECL_ATTRIBUTES (decl)))
7199 error_at (loc,
7200 "%qE with %<link%> clause used in %<routine%> function",
7201 DECL_NAME (decl));
7202 return false;
7204 else if (!lookup_attribute ("omp declare target",
7205 DECL_ATTRIBUTES (decl)))
7207 error_at (loc,
7208 "%qE requires a %<declare%> directive for use "
7209 "in a %<routine%> function", DECL_NAME (decl));
7210 return false;
7215 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7216 if ((ctx->region_type & ORT_TARGET) != 0)
7218 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7219 if (n == NULL)
7221 unsigned nflags = flags;
7222 if (ctx->target_map_pointers_as_0len_arrays
7223 || ctx->target_map_scalars_firstprivate)
7225 bool is_declare_target = false;
7226 bool is_scalar = false;
7227 if (is_global_var (decl)
7228 && varpool_node::get_create (decl)->offloadable)
7230 struct gimplify_omp_ctx *octx;
7231 for (octx = ctx->outer_context;
7232 octx; octx = octx->outer_context)
7234 n = splay_tree_lookup (octx->variables,
7235 (splay_tree_key)decl);
7236 if (n
7237 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7238 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7239 break;
7241 is_declare_target = octx == NULL;
7243 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7244 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7245 if (is_declare_target)
7247 else if (ctx->target_map_pointers_as_0len_arrays
7248 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7249 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7250 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7251 == POINTER_TYPE)))
7252 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7253 else if (is_scalar)
7254 nflags |= GOVD_FIRSTPRIVATE;
7257 struct gimplify_omp_ctx *octx = ctx->outer_context;
7258 if ((ctx->region_type & ORT_ACC) && octx)
7260 /* Look in outer OpenACC contexts, to see if there's a
7261 data attribute for this variable. */
7262 omp_notice_variable (octx, decl, in_code);
7264 for (; octx; octx = octx->outer_context)
7266 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7267 break;
7268 splay_tree_node n2
7269 = splay_tree_lookup (octx->variables,
7270 (splay_tree_key) decl);
7271 if (n2)
7273 if (octx->region_type == ORT_ACC_HOST_DATA)
7274 error ("variable %qE declared in enclosing "
7275 "%<host_data%> region", DECL_NAME (decl));
7276 nflags |= GOVD_MAP;
7277 if (octx->region_type == ORT_ACC_DATA
7278 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7279 nflags |= GOVD_MAP_0LEN_ARRAY;
7280 goto found_outer;
7286 tree type = TREE_TYPE (decl);
7288 if (nflags == flags
7289 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7290 && lang_hooks.decls.omp_privatize_by_reference (decl))
7291 type = TREE_TYPE (type);
7292 if (nflags == flags
7293 && !lang_hooks.types.omp_mappable_type (type))
7295 error ("%qD referenced in target region does not have "
7296 "a mappable type", decl);
7297 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7299 else if (nflags == flags)
7301 if ((ctx->region_type & ORT_ACC) != 0)
7302 nflags = oacc_default_clause (ctx, decl, flags);
7303 else
7304 nflags |= GOVD_MAP;
7307 found_outer:
7308 omp_add_variable (ctx, decl, nflags);
7310 else
7312 /* If nothing changed, there's nothing left to do. */
7313 if ((n->value & flags) == flags)
7314 return ret;
7315 flags |= n->value;
7316 n->value = flags;
7318 goto do_outer;
7321 if (n == NULL)
7323 if (ctx->region_type == ORT_WORKSHARE
7324 || ctx->region_type == ORT_SIMD
7325 || ctx->region_type == ORT_ACC
7326 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7327 goto do_outer;
7329 flags = omp_default_clause (ctx, decl, in_code, flags);
7331 if ((flags & GOVD_PRIVATE)
7332 && lang_hooks.decls.omp_private_outer_ref (decl))
7333 flags |= GOVD_PRIVATE_OUTER_REF;
7335 omp_add_variable (ctx, decl, flags);
7337 shared = (flags & GOVD_SHARED) != 0;
7338 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7339 goto do_outer;
7342 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7343 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7344 && DECL_SIZE (decl))
7346 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7348 splay_tree_node n2;
7349 tree t = DECL_VALUE_EXPR (decl);
7350 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7351 t = TREE_OPERAND (t, 0);
7352 gcc_assert (DECL_P (t));
7353 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7354 n2->value |= GOVD_SEEN;
7356 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7357 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7358 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7359 != INTEGER_CST))
7361 splay_tree_node n2;
7362 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7363 gcc_assert (DECL_P (t));
7364 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7365 if (n2)
7366 omp_notice_variable (ctx, t, true);
7370 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7371 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7373 /* If nothing changed, there's nothing left to do. */
7374 if ((n->value & flags) == flags)
7375 return ret;
7376 flags |= n->value;
7377 n->value = flags;
7379 do_outer:
7380 /* If the variable is private in the current context, then we don't
7381 need to propagate anything to an outer context. */
7382 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7383 return ret;
7384 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7385 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7386 return ret;
7387 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7388 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7389 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7390 return ret;
7391 if (ctx->outer_context
7392 && omp_notice_variable (ctx->outer_context, decl, in_code))
7393 return true;
7394 return ret;
7397 /* Verify that DECL is private within CTX. If there's specific information
7398 to the contrary in the innermost scope, generate an error. */
7400 static bool
7401 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7403 splay_tree_node n;
7405 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7406 if (n != NULL)
7408 if (n->value & GOVD_SHARED)
7410 if (ctx == gimplify_omp_ctxp)
7412 if (simd)
7413 error ("iteration variable %qE is predetermined linear",
7414 DECL_NAME (decl));
7415 else
7416 error ("iteration variable %qE should be private",
7417 DECL_NAME (decl));
7418 n->value = GOVD_PRIVATE;
7419 return true;
7421 else
7422 return false;
7424 else if ((n->value & GOVD_EXPLICIT) != 0
7425 && (ctx == gimplify_omp_ctxp
7426 || (ctx->region_type == ORT_COMBINED_PARALLEL
7427 && gimplify_omp_ctxp->outer_context == ctx)))
7429 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7430 error ("iteration variable %qE should not be firstprivate",
7431 DECL_NAME (decl));
7432 else if ((n->value & GOVD_REDUCTION) != 0)
7433 error ("iteration variable %qE should not be reduction",
7434 DECL_NAME (decl));
7435 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7436 error ("iteration variable %qE should not be linear",
7437 DECL_NAME (decl));
7438 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7439 error ("iteration variable %qE should not be lastprivate",
7440 DECL_NAME (decl));
7441 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7442 error ("iteration variable %qE should not be private",
7443 DECL_NAME (decl));
7444 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7445 error ("iteration variable %qE is predetermined linear",
7446 DECL_NAME (decl));
7448 return (ctx == gimplify_omp_ctxp
7449 || (ctx->region_type == ORT_COMBINED_PARALLEL
7450 && gimplify_omp_ctxp->outer_context == ctx));
7453 if (ctx->region_type != ORT_WORKSHARE
7454 && ctx->region_type != ORT_SIMD
7455 && ctx->region_type != ORT_ACC)
7456 return false;
7457 else if (ctx->outer_context)
7458 return omp_is_private (ctx->outer_context, decl, simd);
7459 return false;
7462 /* Return true if DECL is private within a parallel region
7463 that binds to the current construct's context or in parallel
7464 region's REDUCTION clause. */
7466 static bool
7467 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7469 splay_tree_node n;
7473 ctx = ctx->outer_context;
7474 if (ctx == NULL)
7476 if (is_global_var (decl))
7477 return false;
7479 /* References might be private, but might be shared too,
7480 when checking for copyprivate, assume they might be
7481 private, otherwise assume they might be shared. */
7482 if (copyprivate)
7483 return true;
7485 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7486 return false;
7488 /* Treat C++ privatized non-static data members outside
7489 of the privatization the same. */
7490 if (omp_member_access_dummy_var (decl))
7491 return false;
7493 return true;
7496 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7498 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7499 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7500 continue;
7502 if (n != NULL)
7504 if ((n->value & GOVD_LOCAL) != 0
7505 && omp_member_access_dummy_var (decl))
7506 return false;
7507 return (n->value & GOVD_SHARED) == 0;
7510 while (ctx->region_type == ORT_WORKSHARE
7511 || ctx->region_type == ORT_SIMD
7512 || ctx->region_type == ORT_ACC);
7513 return false;
7516 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7518 static tree
7519 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7521 tree t = *tp;
7523 /* If this node has been visited, unmark it and keep looking. */
7524 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7525 return t;
7527 if (IS_TYPE_OR_DECL_P (t))
7528 *walk_subtrees = 0;
7529 return NULL_TREE;
7532 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7533 and previous omp contexts. */
7535 static void
7536 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7537 enum omp_region_type region_type,
7538 enum tree_code code)
7540 struct gimplify_omp_ctx *ctx, *outer_ctx;
7541 tree c;
7542 hash_map<tree, tree> *struct_map_to_clause = NULL;
7543 tree *prev_list_p = NULL;
7545 ctx = new_omp_context (region_type);
7546 outer_ctx = ctx->outer_context;
7547 if (code == OMP_TARGET)
7549 if (!lang_GNU_Fortran ())
7550 ctx->target_map_pointers_as_0len_arrays = true;
7551 ctx->target_map_scalars_firstprivate = true;
7553 if (!lang_GNU_Fortran ())
7554 switch (code)
7556 case OMP_TARGET:
7557 case OMP_TARGET_DATA:
7558 case OMP_TARGET_ENTER_DATA:
7559 case OMP_TARGET_EXIT_DATA:
7560 case OACC_DECLARE:
7561 case OACC_HOST_DATA:
7562 ctx->target_firstprivatize_array_bases = true;
7563 default:
7564 break;
7567 while ((c = *list_p) != NULL)
7569 bool remove = false;
7570 bool notice_outer = true;
7571 const char *check_non_private = NULL;
7572 unsigned int flags;
7573 tree decl;
7575 switch (OMP_CLAUSE_CODE (c))
7577 case OMP_CLAUSE_PRIVATE:
7578 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7579 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7581 flags |= GOVD_PRIVATE_OUTER_REF;
7582 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7584 else
7585 notice_outer = false;
7586 goto do_add;
7587 case OMP_CLAUSE_SHARED:
7588 flags = GOVD_SHARED | GOVD_EXPLICIT;
7589 goto do_add;
7590 case OMP_CLAUSE_FIRSTPRIVATE:
7591 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7592 check_non_private = "firstprivate";
7593 goto do_add;
7594 case OMP_CLAUSE_LASTPRIVATE:
7595 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7596 check_non_private = "lastprivate";
7597 decl = OMP_CLAUSE_DECL (c);
7598 if (error_operand_p (decl))
7599 goto do_add;
7600 else if (outer_ctx
7601 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7602 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7603 && splay_tree_lookup (outer_ctx->variables,
7604 (splay_tree_key) decl) == NULL)
7606 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7607 if (outer_ctx->outer_context)
7608 omp_notice_variable (outer_ctx->outer_context, decl, true);
7610 else if (outer_ctx
7611 && (outer_ctx->region_type & ORT_TASK) != 0
7612 && outer_ctx->combined_loop
7613 && splay_tree_lookup (outer_ctx->variables,
7614 (splay_tree_key) decl) == NULL)
7616 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7617 if (outer_ctx->outer_context)
7618 omp_notice_variable (outer_ctx->outer_context, decl, true);
7620 else if (outer_ctx
7621 && (outer_ctx->region_type == ORT_WORKSHARE
7622 || outer_ctx->region_type == ORT_ACC)
7623 && outer_ctx->combined_loop
7624 && splay_tree_lookup (outer_ctx->variables,
7625 (splay_tree_key) decl) == NULL
7626 && !omp_check_private (outer_ctx, decl, false))
7628 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7629 if (outer_ctx->outer_context
7630 && (outer_ctx->outer_context->region_type
7631 == ORT_COMBINED_PARALLEL)
7632 && splay_tree_lookup (outer_ctx->outer_context->variables,
7633 (splay_tree_key) decl) == NULL)
7635 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7636 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7637 if (octx->outer_context)
7639 octx = octx->outer_context;
7640 if (octx->region_type == ORT_WORKSHARE
7641 && octx->combined_loop
7642 && splay_tree_lookup (octx->variables,
7643 (splay_tree_key) decl) == NULL
7644 && !omp_check_private (octx, decl, false))
7646 omp_add_variable (octx, decl,
7647 GOVD_LASTPRIVATE | GOVD_SEEN);
7648 octx = octx->outer_context;
7649 if (octx
7650 && octx->region_type == ORT_COMBINED_TEAMS
7651 && (splay_tree_lookup (octx->variables,
7652 (splay_tree_key) decl)
7653 == NULL))
7655 omp_add_variable (octx, decl,
7656 GOVD_SHARED | GOVD_SEEN);
7657 octx = octx->outer_context;
7660 if (octx)
7661 omp_notice_variable (octx, decl, true);
7664 else if (outer_ctx->outer_context)
7665 omp_notice_variable (outer_ctx->outer_context, decl, true);
7667 goto do_add;
7668 case OMP_CLAUSE_REDUCTION:
7669 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7670 /* OpenACC permits reductions on private variables. */
7671 if (!(region_type & ORT_ACC))
7672 check_non_private = "reduction";
7673 decl = OMP_CLAUSE_DECL (c);
7674 if (TREE_CODE (decl) == MEM_REF)
7676 tree type = TREE_TYPE (decl);
7677 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7678 NULL, is_gimple_val, fb_rvalue, false)
7679 == GS_ERROR)
7681 remove = true;
7682 break;
7684 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7685 if (DECL_P (v))
7687 omp_firstprivatize_variable (ctx, v);
7688 omp_notice_variable (ctx, v, true);
7690 decl = TREE_OPERAND (decl, 0);
7691 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7693 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7694 NULL, is_gimple_val, fb_rvalue, false)
7695 == GS_ERROR)
7697 remove = true;
7698 break;
7700 v = TREE_OPERAND (decl, 1);
7701 if (DECL_P (v))
7703 omp_firstprivatize_variable (ctx, v);
7704 omp_notice_variable (ctx, v, true);
7706 decl = TREE_OPERAND (decl, 0);
7708 if (TREE_CODE (decl) == ADDR_EXPR
7709 || TREE_CODE (decl) == INDIRECT_REF)
7710 decl = TREE_OPERAND (decl, 0);
7712 goto do_add_decl;
7713 case OMP_CLAUSE_LINEAR:
7714 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7715 is_gimple_val, fb_rvalue) == GS_ERROR)
7717 remove = true;
7718 break;
7720 else
7722 if (code == OMP_SIMD
7723 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7725 struct gimplify_omp_ctx *octx = outer_ctx;
7726 if (octx
7727 && octx->region_type == ORT_WORKSHARE
7728 && octx->combined_loop
7729 && !octx->distribute)
7731 if (octx->outer_context
7732 && (octx->outer_context->region_type
7733 == ORT_COMBINED_PARALLEL))
7734 octx = octx->outer_context->outer_context;
7735 else
7736 octx = octx->outer_context;
7738 if (octx
7739 && octx->region_type == ORT_WORKSHARE
7740 && octx->combined_loop
7741 && octx->distribute)
7743 error_at (OMP_CLAUSE_LOCATION (c),
7744 "%<linear%> clause for variable other than "
7745 "loop iterator specified on construct "
7746 "combined with %<distribute%>");
7747 remove = true;
7748 break;
7751 /* For combined #pragma omp parallel for simd, need to put
7752 lastprivate and perhaps firstprivate too on the
7753 parallel. Similarly for #pragma omp for simd. */
7754 struct gimplify_omp_ctx *octx = outer_ctx;
7755 decl = NULL_TREE;
7758 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7759 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7760 break;
7761 decl = OMP_CLAUSE_DECL (c);
7762 if (error_operand_p (decl))
7764 decl = NULL_TREE;
7765 break;
7767 flags = GOVD_SEEN;
7768 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7769 flags |= GOVD_FIRSTPRIVATE;
7770 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7771 flags |= GOVD_LASTPRIVATE;
7772 if (octx
7773 && octx->region_type == ORT_WORKSHARE
7774 && octx->combined_loop)
7776 if (octx->outer_context
7777 && (octx->outer_context->region_type
7778 == ORT_COMBINED_PARALLEL))
7779 octx = octx->outer_context;
7780 else if (omp_check_private (octx, decl, false))
7781 break;
7783 else if (octx
7784 && (octx->region_type & ORT_TASK) != 0
7785 && octx->combined_loop)
7787 else if (octx
7788 && octx->region_type == ORT_COMBINED_PARALLEL
7789 && ctx->region_type == ORT_WORKSHARE
7790 && octx == outer_ctx)
7791 flags = GOVD_SEEN | GOVD_SHARED;
7792 else if (octx
7793 && octx->region_type == ORT_COMBINED_TEAMS)
7794 flags = GOVD_SEEN | GOVD_SHARED;
7795 else if (octx
7796 && octx->region_type == ORT_COMBINED_TARGET)
7798 flags &= ~GOVD_LASTPRIVATE;
7799 if (flags == GOVD_SEEN)
7800 break;
7802 else
7803 break;
7804 splay_tree_node on
7805 = splay_tree_lookup (octx->variables,
7806 (splay_tree_key) decl);
7807 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7809 octx = NULL;
7810 break;
7812 omp_add_variable (octx, decl, flags);
7813 if (octx->outer_context == NULL)
7814 break;
7815 octx = octx->outer_context;
7817 while (1);
7818 if (octx
7819 && decl
7820 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7821 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7822 omp_notice_variable (octx, decl, true);
7824 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7825 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7826 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7828 notice_outer = false;
7829 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7831 goto do_add;
7833 case OMP_CLAUSE_MAP:
7834 decl = OMP_CLAUSE_DECL (c);
7835 if (error_operand_p (decl))
7836 remove = true;
7837 switch (code)
7839 case OMP_TARGET:
7840 break;
7841 case OACC_DATA:
7842 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7843 break;
7844 /* FALLTHRU */
7845 case OMP_TARGET_DATA:
7846 case OMP_TARGET_ENTER_DATA:
7847 case OMP_TARGET_EXIT_DATA:
7848 case OACC_ENTER_DATA:
7849 case OACC_EXIT_DATA:
7850 case OACC_HOST_DATA:
7851 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7852 || (OMP_CLAUSE_MAP_KIND (c)
7853 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7854 /* For target {,enter ,exit }data only the array slice is
7855 mapped, but not the pointer to it. */
7856 remove = true;
7857 break;
7858 default:
7859 break;
7861 if (remove)
7862 break;
7863 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7865 struct gimplify_omp_ctx *octx;
7866 for (octx = outer_ctx; octx; octx = octx->outer_context)
7868 if (octx->region_type != ORT_ACC_HOST_DATA)
7869 break;
7870 splay_tree_node n2
7871 = splay_tree_lookup (octx->variables,
7872 (splay_tree_key) decl);
7873 if (n2)
7874 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7875 "declared in enclosing %<host_data%> region",
7876 DECL_NAME (decl));
7879 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7880 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7881 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7882 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7883 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7885 remove = true;
7886 break;
7888 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7889 || (OMP_CLAUSE_MAP_KIND (c)
7890 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7891 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7893 OMP_CLAUSE_SIZE (c)
7894 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7895 false);
7896 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7897 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7899 if (!DECL_P (decl))
7901 tree d = decl, *pd;
7902 if (TREE_CODE (d) == ARRAY_REF)
7904 while (TREE_CODE (d) == ARRAY_REF)
7905 d = TREE_OPERAND (d, 0);
7906 if (TREE_CODE (d) == COMPONENT_REF
7907 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7908 decl = d;
7910 pd = &OMP_CLAUSE_DECL (c);
7911 if (d == decl
7912 && TREE_CODE (decl) == INDIRECT_REF
7913 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7914 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7915 == REFERENCE_TYPE))
7917 pd = &TREE_OPERAND (decl, 0);
7918 decl = TREE_OPERAND (decl, 0);
7920 if (TREE_CODE (decl) == COMPONENT_REF)
7922 while (TREE_CODE (decl) == COMPONENT_REF)
7923 decl = TREE_OPERAND (decl, 0);
7924 if (TREE_CODE (decl) == INDIRECT_REF
7925 && DECL_P (TREE_OPERAND (decl, 0))
7926 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7927 == REFERENCE_TYPE))
7928 decl = TREE_OPERAND (decl, 0);
7930 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7931 == GS_ERROR)
7933 remove = true;
7934 break;
7936 if (DECL_P (decl))
7938 if (error_operand_p (decl))
7940 remove = true;
7941 break;
7944 tree stype = TREE_TYPE (decl);
7945 if (TREE_CODE (stype) == REFERENCE_TYPE)
7946 stype = TREE_TYPE (stype);
7947 if (TYPE_SIZE_UNIT (stype) == NULL
7948 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7950 error_at (OMP_CLAUSE_LOCATION (c),
7951 "mapping field %qE of variable length "
7952 "structure", OMP_CLAUSE_DECL (c));
7953 remove = true;
7954 break;
7957 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7959 /* Error recovery. */
7960 if (prev_list_p == NULL)
7962 remove = true;
7963 break;
7965 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7967 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7968 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7970 remove = true;
7971 break;
7976 tree offset;
7977 poly_int64 bitsize, bitpos;
7978 machine_mode mode;
7979 int unsignedp, reversep, volatilep = 0;
7980 tree base = OMP_CLAUSE_DECL (c);
7981 while (TREE_CODE (base) == ARRAY_REF)
7982 base = TREE_OPERAND (base, 0);
7983 if (TREE_CODE (base) == INDIRECT_REF)
7984 base = TREE_OPERAND (base, 0);
7985 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7986 &mode, &unsignedp, &reversep,
7987 &volatilep);
7988 tree orig_base = base;
7989 if ((TREE_CODE (base) == INDIRECT_REF
7990 || (TREE_CODE (base) == MEM_REF
7991 && integer_zerop (TREE_OPERAND (base, 1))))
7992 && DECL_P (TREE_OPERAND (base, 0))
7993 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7994 == REFERENCE_TYPE))
7995 base = TREE_OPERAND (base, 0);
7996 gcc_assert (base == decl
7997 && (offset == NULL_TREE
7998 || poly_int_tree_p (offset)));
8000 splay_tree_node n
8001 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8002 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8003 == GOMP_MAP_ALWAYS_POINTER);
8004 if (n == NULL || (n->value & GOVD_MAP) == 0)
8006 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8007 OMP_CLAUSE_MAP);
8008 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8009 if (orig_base != base)
8010 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8011 else
8012 OMP_CLAUSE_DECL (l) = decl;
8013 OMP_CLAUSE_SIZE (l) = size_int (1);
8014 if (struct_map_to_clause == NULL)
8015 struct_map_to_clause = new hash_map<tree, tree>;
8016 struct_map_to_clause->put (decl, l);
8017 if (ptr)
8019 enum gomp_map_kind mkind
8020 = code == OMP_TARGET_EXIT_DATA
8021 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8022 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8023 OMP_CLAUSE_MAP);
8024 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8025 OMP_CLAUSE_DECL (c2)
8026 = unshare_expr (OMP_CLAUSE_DECL (c));
8027 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8028 OMP_CLAUSE_SIZE (c2)
8029 = TYPE_SIZE_UNIT (ptr_type_node);
8030 OMP_CLAUSE_CHAIN (l) = c2;
8031 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8033 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8034 tree c3
8035 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8036 OMP_CLAUSE_MAP);
8037 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8038 OMP_CLAUSE_DECL (c3)
8039 = unshare_expr (OMP_CLAUSE_DECL (c4));
8040 OMP_CLAUSE_SIZE (c3)
8041 = TYPE_SIZE_UNIT (ptr_type_node);
8042 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8043 OMP_CLAUSE_CHAIN (c2) = c3;
8045 *prev_list_p = l;
8046 prev_list_p = NULL;
8048 else
8050 OMP_CLAUSE_CHAIN (l) = c;
8051 *list_p = l;
8052 list_p = &OMP_CLAUSE_CHAIN (l);
8054 if (orig_base != base && code == OMP_TARGET)
8056 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8057 OMP_CLAUSE_MAP);
8058 enum gomp_map_kind mkind
8059 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8060 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8061 OMP_CLAUSE_DECL (c2) = decl;
8062 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8063 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8064 OMP_CLAUSE_CHAIN (l) = c2;
8066 flags = GOVD_MAP | GOVD_EXPLICIT;
8067 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8068 flags |= GOVD_SEEN;
8069 goto do_add_decl;
8071 else
8073 tree *osc = struct_map_to_clause->get (decl);
8074 tree *sc = NULL, *scp = NULL;
8075 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8076 n->value |= GOVD_SEEN;
8077 poly_offset_int o1, o2;
8078 if (offset)
8079 o1 = wi::to_poly_offset (offset);
8080 else
8081 o1 = 0;
8082 if (maybe_ne (bitpos, 0))
8083 o1 += bits_to_bytes_round_down (bitpos);
8084 sc = &OMP_CLAUSE_CHAIN (*osc);
8085 if (*sc != c
8086 && (OMP_CLAUSE_MAP_KIND (*sc)
8087 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8088 sc = &OMP_CLAUSE_CHAIN (*sc);
8089 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8090 if (ptr && sc == prev_list_p)
8091 break;
8092 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8093 != COMPONENT_REF
8094 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8095 != INDIRECT_REF)
8096 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8097 != ARRAY_REF))
8098 break;
8099 else
8101 tree offset2;
8102 poly_int64 bitsize2, bitpos2;
8103 base = OMP_CLAUSE_DECL (*sc);
8104 if (TREE_CODE (base) == ARRAY_REF)
8106 while (TREE_CODE (base) == ARRAY_REF)
8107 base = TREE_OPERAND (base, 0);
8108 if (TREE_CODE (base) != COMPONENT_REF
8109 || (TREE_CODE (TREE_TYPE (base))
8110 != ARRAY_TYPE))
8111 break;
8113 else if (TREE_CODE (base) == INDIRECT_REF
8114 && (TREE_CODE (TREE_OPERAND (base, 0))
8115 == COMPONENT_REF)
8116 && (TREE_CODE (TREE_TYPE
8117 (TREE_OPERAND (base, 0)))
8118 == REFERENCE_TYPE))
8119 base = TREE_OPERAND (base, 0);
8120 base = get_inner_reference (base, &bitsize2,
8121 &bitpos2, &offset2,
8122 &mode, &unsignedp,
8123 &reversep, &volatilep);
8124 if ((TREE_CODE (base) == INDIRECT_REF
8125 || (TREE_CODE (base) == MEM_REF
8126 && integer_zerop (TREE_OPERAND (base,
8127 1))))
8128 && DECL_P (TREE_OPERAND (base, 0))
8129 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8130 0)))
8131 == REFERENCE_TYPE))
8132 base = TREE_OPERAND (base, 0);
8133 if (base != decl)
8134 break;
8135 if (scp)
8136 continue;
8137 gcc_assert (offset == NULL_TREE
8138 || poly_int_tree_p (offset));
8139 tree d1 = OMP_CLAUSE_DECL (*sc);
8140 tree d2 = OMP_CLAUSE_DECL (c);
8141 while (TREE_CODE (d1) == ARRAY_REF)
8142 d1 = TREE_OPERAND (d1, 0);
8143 while (TREE_CODE (d2) == ARRAY_REF)
8144 d2 = TREE_OPERAND (d2, 0);
8145 if (TREE_CODE (d1) == INDIRECT_REF)
8146 d1 = TREE_OPERAND (d1, 0);
8147 if (TREE_CODE (d2) == INDIRECT_REF)
8148 d2 = TREE_OPERAND (d2, 0);
8149 while (TREE_CODE (d1) == COMPONENT_REF)
8150 if (TREE_CODE (d2) == COMPONENT_REF
8151 && TREE_OPERAND (d1, 1)
8152 == TREE_OPERAND (d2, 1))
8154 d1 = TREE_OPERAND (d1, 0);
8155 d2 = TREE_OPERAND (d2, 0);
8157 else
8158 break;
8159 if (d1 == d2)
8161 error_at (OMP_CLAUSE_LOCATION (c),
8162 "%qE appears more than once in map "
8163 "clauses", OMP_CLAUSE_DECL (c));
8164 remove = true;
8165 break;
8167 if (offset2)
8168 o2 = wi::to_poly_offset (offset2);
8169 else
8170 o2 = 0;
8171 o2 += bits_to_bytes_round_down (bitpos2);
8172 if (maybe_lt (o1, o2)
8173 || (known_eq (o1, 2)
8174 && maybe_lt (bitpos, bitpos2)))
8176 if (ptr)
8177 scp = sc;
8178 else
8179 break;
8182 if (remove)
8183 break;
8184 OMP_CLAUSE_SIZE (*osc)
8185 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8186 size_one_node);
8187 if (ptr)
8189 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8190 OMP_CLAUSE_MAP);
8191 tree cl = NULL_TREE;
8192 enum gomp_map_kind mkind
8193 = code == OMP_TARGET_EXIT_DATA
8194 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8195 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8196 OMP_CLAUSE_DECL (c2)
8197 = unshare_expr (OMP_CLAUSE_DECL (c));
8198 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8199 OMP_CLAUSE_SIZE (c2)
8200 = TYPE_SIZE_UNIT (ptr_type_node);
8201 cl = scp ? *prev_list_p : c2;
8202 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8204 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8205 tree c3
8206 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8207 OMP_CLAUSE_MAP);
8208 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8209 OMP_CLAUSE_DECL (c3)
8210 = unshare_expr (OMP_CLAUSE_DECL (c4));
8211 OMP_CLAUSE_SIZE (c3)
8212 = TYPE_SIZE_UNIT (ptr_type_node);
8213 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8214 if (!scp)
8215 OMP_CLAUSE_CHAIN (c2) = c3;
8216 else
8217 cl = c3;
8219 if (scp)
8220 *scp = c2;
8221 if (sc == prev_list_p)
8223 *sc = cl;
8224 prev_list_p = NULL;
8226 else
8228 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8229 list_p = prev_list_p;
8230 prev_list_p = NULL;
8231 OMP_CLAUSE_CHAIN (c) = *sc;
8232 *sc = cl;
8233 continue;
8236 else if (*sc != c)
8238 *list_p = OMP_CLAUSE_CHAIN (c);
8239 OMP_CLAUSE_CHAIN (c) = *sc;
8240 *sc = c;
8241 continue;
8245 if (!remove
8246 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8247 && OMP_CLAUSE_CHAIN (c)
8248 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8249 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8250 == GOMP_MAP_ALWAYS_POINTER))
8251 prev_list_p = list_p;
8252 break;
8254 flags = GOVD_MAP | GOVD_EXPLICIT;
8255 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8256 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8257 flags |= GOVD_MAP_ALWAYS_TO;
8258 goto do_add;
8260 case OMP_CLAUSE_DEPEND:
8261 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8263 tree deps = OMP_CLAUSE_DECL (c);
8264 while (deps && TREE_CODE (deps) == TREE_LIST)
8266 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8267 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8268 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8269 pre_p, NULL, is_gimple_val, fb_rvalue);
8270 deps = TREE_CHAIN (deps);
8272 break;
8274 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8275 break;
8276 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8278 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8279 NULL, is_gimple_val, fb_rvalue);
8280 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8282 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8284 remove = true;
8285 break;
8287 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8288 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8289 is_gimple_val, fb_rvalue) == GS_ERROR)
8291 remove = true;
8292 break;
8294 break;
8296 case OMP_CLAUSE_TO:
8297 case OMP_CLAUSE_FROM:
8298 case OMP_CLAUSE__CACHE_:
8299 decl = OMP_CLAUSE_DECL (c);
8300 if (error_operand_p (decl))
8302 remove = true;
8303 break;
8305 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8306 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8307 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8308 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8309 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8311 remove = true;
8312 break;
8314 if (!DECL_P (decl))
8316 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8317 NULL, is_gimple_lvalue, fb_lvalue)
8318 == GS_ERROR)
8320 remove = true;
8321 break;
8323 break;
8325 goto do_notice;
8327 case OMP_CLAUSE_USE_DEVICE_PTR:
8328 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8329 goto do_add;
8330 case OMP_CLAUSE_IS_DEVICE_PTR:
8331 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8332 goto do_add;
8334 do_add:
8335 decl = OMP_CLAUSE_DECL (c);
8336 do_add_decl:
8337 if (error_operand_p (decl))
8339 remove = true;
8340 break;
8342 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8344 tree t = omp_member_access_dummy_var (decl);
8345 if (t)
8347 tree v = DECL_VALUE_EXPR (decl);
8348 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8349 if (outer_ctx)
8350 omp_notice_variable (outer_ctx, t, true);
8353 if (code == OACC_DATA
8354 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8355 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8356 flags |= GOVD_MAP_0LEN_ARRAY;
8357 omp_add_variable (ctx, decl, flags);
8358 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8359 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8361 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8362 GOVD_LOCAL | GOVD_SEEN);
8363 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8364 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8365 find_decl_expr,
8366 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8367 NULL) == NULL_TREE)
8368 omp_add_variable (ctx,
8369 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8370 GOVD_LOCAL | GOVD_SEEN);
8371 gimplify_omp_ctxp = ctx;
8372 push_gimplify_context ();
8374 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8375 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8377 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8378 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8379 pop_gimplify_context
8380 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8381 push_gimplify_context ();
8382 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8383 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8384 pop_gimplify_context
8385 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8386 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8387 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8389 gimplify_omp_ctxp = outer_ctx;
8391 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8392 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8394 gimplify_omp_ctxp = ctx;
8395 push_gimplify_context ();
8396 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8398 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8399 NULL, NULL);
8400 TREE_SIDE_EFFECTS (bind) = 1;
8401 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8402 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8404 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8405 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8406 pop_gimplify_context
8407 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8408 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8410 gimplify_omp_ctxp = outer_ctx;
8412 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8413 && OMP_CLAUSE_LINEAR_STMT (c))
8415 gimplify_omp_ctxp = ctx;
8416 push_gimplify_context ();
8417 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8419 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8420 NULL, NULL);
8421 TREE_SIDE_EFFECTS (bind) = 1;
8422 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8423 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8425 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8426 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8427 pop_gimplify_context
8428 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8429 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8431 gimplify_omp_ctxp = outer_ctx;
8433 if (notice_outer)
8434 goto do_notice;
8435 break;
8437 case OMP_CLAUSE_COPYIN:
8438 case OMP_CLAUSE_COPYPRIVATE:
8439 decl = OMP_CLAUSE_DECL (c);
8440 if (error_operand_p (decl))
8442 remove = true;
8443 break;
8445 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8446 && !remove
8447 && !omp_check_private (ctx, decl, true))
8449 remove = true;
8450 if (is_global_var (decl))
8452 if (DECL_THREAD_LOCAL_P (decl))
8453 remove = false;
8454 else if (DECL_HAS_VALUE_EXPR_P (decl))
8456 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8458 if (value
8459 && DECL_P (value)
8460 && DECL_THREAD_LOCAL_P (value))
8461 remove = false;
8464 if (remove)
8465 error_at (OMP_CLAUSE_LOCATION (c),
8466 "copyprivate variable %qE is not threadprivate"
8467 " or private in outer context", DECL_NAME (decl));
8469 do_notice:
8470 if (outer_ctx)
8471 omp_notice_variable (outer_ctx, decl, true);
8472 if (check_non_private
8473 && region_type == ORT_WORKSHARE
8474 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8475 || decl == OMP_CLAUSE_DECL (c)
8476 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8477 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8478 == ADDR_EXPR
8479 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8480 == POINTER_PLUS_EXPR
8481 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8482 (OMP_CLAUSE_DECL (c), 0), 0))
8483 == ADDR_EXPR)))))
8484 && omp_check_private (ctx, decl, false))
8486 error ("%s variable %qE is private in outer context",
8487 check_non_private, DECL_NAME (decl));
8488 remove = true;
8490 break;
8492 case OMP_CLAUSE_IF:
8493 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8494 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8496 const char *p[2];
8497 for (int i = 0; i < 2; i++)
8498 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8500 case OMP_PARALLEL: p[i] = "parallel"; break;
8501 case OMP_TASK: p[i] = "task"; break;
8502 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8503 case OMP_TARGET_DATA: p[i] = "target data"; break;
8504 case OMP_TARGET: p[i] = "target"; break;
8505 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8506 case OMP_TARGET_ENTER_DATA:
8507 p[i] = "target enter data"; break;
8508 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8509 default: gcc_unreachable ();
8511 error_at (OMP_CLAUSE_LOCATION (c),
8512 "expected %qs %<if%> clause modifier rather than %qs",
8513 p[0], p[1]);
8514 remove = true;
8516 /* Fall through. */
8518 case OMP_CLAUSE_FINAL:
8519 OMP_CLAUSE_OPERAND (c, 0)
8520 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8521 /* Fall through. */
8523 case OMP_CLAUSE_SCHEDULE:
8524 case OMP_CLAUSE_NUM_THREADS:
8525 case OMP_CLAUSE_NUM_TEAMS:
8526 case OMP_CLAUSE_THREAD_LIMIT:
8527 case OMP_CLAUSE_DIST_SCHEDULE:
8528 case OMP_CLAUSE_DEVICE:
8529 case OMP_CLAUSE_PRIORITY:
8530 case OMP_CLAUSE_GRAINSIZE:
8531 case OMP_CLAUSE_NUM_TASKS:
8532 case OMP_CLAUSE_HINT:
8533 case OMP_CLAUSE_ASYNC:
8534 case OMP_CLAUSE_WAIT:
8535 case OMP_CLAUSE_NUM_GANGS:
8536 case OMP_CLAUSE_NUM_WORKERS:
8537 case OMP_CLAUSE_VECTOR_LENGTH:
8538 case OMP_CLAUSE_WORKER:
8539 case OMP_CLAUSE_VECTOR:
8540 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8541 is_gimple_val, fb_rvalue) == GS_ERROR)
8542 remove = true;
8543 break;
8545 case OMP_CLAUSE_GANG:
8546 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8547 is_gimple_val, fb_rvalue) == GS_ERROR)
8548 remove = true;
8549 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8550 is_gimple_val, fb_rvalue) == GS_ERROR)
8551 remove = true;
8552 break;
8554 case OMP_CLAUSE_NOWAIT:
8555 case OMP_CLAUSE_ORDERED:
8556 case OMP_CLAUSE_UNTIED:
8557 case OMP_CLAUSE_COLLAPSE:
8558 case OMP_CLAUSE_TILE:
8559 case OMP_CLAUSE_AUTO:
8560 case OMP_CLAUSE_SEQ:
8561 case OMP_CLAUSE_INDEPENDENT:
8562 case OMP_CLAUSE_MERGEABLE:
8563 case OMP_CLAUSE_PROC_BIND:
8564 case OMP_CLAUSE_SAFELEN:
8565 case OMP_CLAUSE_SIMDLEN:
8566 case OMP_CLAUSE_NOGROUP:
8567 case OMP_CLAUSE_THREADS:
8568 case OMP_CLAUSE_SIMD:
8569 break;
8571 case OMP_CLAUSE_DEFAULTMAP:
8572 ctx->target_map_scalars_firstprivate = false;
8573 break;
8575 case OMP_CLAUSE_ALIGNED:
8576 decl = OMP_CLAUSE_DECL (c);
8577 if (error_operand_p (decl))
8579 remove = true;
8580 break;
8582 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8583 is_gimple_val, fb_rvalue) == GS_ERROR)
8585 remove = true;
8586 break;
8588 if (!is_global_var (decl)
8589 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8590 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8591 break;
8593 case OMP_CLAUSE_DEFAULT:
8594 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8595 break;
8597 default:
8598 gcc_unreachable ();
8601 if (code == OACC_DATA
8602 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8603 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8604 remove = true;
8605 if (remove)
8606 *list_p = OMP_CLAUSE_CHAIN (c);
8607 else
8608 list_p = &OMP_CLAUSE_CHAIN (c);
8611 gimplify_omp_ctxp = ctx;
8612 if (struct_map_to_clause)
8613 delete struct_map_to_clause;
8616 /* Return true if DECL is a candidate for shared to firstprivate
8617 optimization. We only consider non-addressable scalars, not
8618 too big, and not references. */
8620 static bool
8621 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8623 if (TREE_ADDRESSABLE (decl))
8624 return false;
8625 tree type = TREE_TYPE (decl);
8626 if (!is_gimple_reg_type (type)
8627 || TREE_CODE (type) == REFERENCE_TYPE
8628 || TREE_ADDRESSABLE (type))
8629 return false;
8630 /* Don't optimize too large decls, as each thread/task will have
8631 its own. */
8632 HOST_WIDE_INT len = int_size_in_bytes (type);
8633 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8634 return false;
8635 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8636 return false;
8637 return true;
8640 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8641 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8642 GOVD_WRITTEN in outer contexts. */
8644 static void
8645 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8647 for (; ctx; ctx = ctx->outer_context)
8649 splay_tree_node n = splay_tree_lookup (ctx->variables,
8650 (splay_tree_key) decl);
8651 if (n == NULL)
8652 continue;
8653 else if (n->value & GOVD_SHARED)
8655 n->value |= GOVD_WRITTEN;
8656 return;
8658 else if (n->value & GOVD_DATA_SHARE_CLASS)
8659 return;
8663 /* Helper callback for walk_gimple_seq to discover possible stores
8664 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8665 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8666 for those. */
8668 static tree
8669 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8671 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8673 *walk_subtrees = 0;
8674 if (!wi->is_lhs)
8675 return NULL_TREE;
8677 tree op = *tp;
8680 if (handled_component_p (op))
8681 op = TREE_OPERAND (op, 0);
8682 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8683 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8684 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8685 else
8686 break;
8688 while (1);
8689 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8690 return NULL_TREE;
8692 omp_mark_stores (gimplify_omp_ctxp, op);
8693 return NULL_TREE;
8696 /* Helper callback for walk_gimple_seq to discover possible stores
8697 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8698 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8699 for those. */
8701 static tree
8702 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8703 bool *handled_ops_p,
8704 struct walk_stmt_info *wi)
8706 gimple *stmt = gsi_stmt (*gsi_p);
8707 switch (gimple_code (stmt))
8709 /* Don't recurse on OpenMP constructs for which
8710 gimplify_adjust_omp_clauses already handled the bodies,
8711 except handle gimple_omp_for_pre_body. */
8712 case GIMPLE_OMP_FOR:
8713 *handled_ops_p = true;
8714 if (gimple_omp_for_pre_body (stmt))
8715 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8716 omp_find_stores_stmt, omp_find_stores_op, wi);
8717 break;
8718 case GIMPLE_OMP_PARALLEL:
8719 case GIMPLE_OMP_TASK:
8720 case GIMPLE_OMP_SECTIONS:
8721 case GIMPLE_OMP_SINGLE:
8722 case GIMPLE_OMP_TARGET:
8723 case GIMPLE_OMP_TEAMS:
8724 case GIMPLE_OMP_CRITICAL:
8725 *handled_ops_p = true;
8726 break;
8727 default:
8728 break;
8730 return NULL_TREE;
8733 struct gimplify_adjust_omp_clauses_data
8735 tree *list_p;
8736 gimple_seq *pre_p;
8739 /* For all variables that were not actually used within the context,
8740 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8742 static int
8743 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8745 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8746 gimple_seq *pre_p
8747 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8748 tree decl = (tree) n->key;
8749 unsigned flags = n->value;
8750 enum omp_clause_code code;
8751 tree clause;
8752 bool private_debug;
8754 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8755 return 0;
8756 if ((flags & GOVD_SEEN) == 0)
8757 return 0;
8758 if (flags & GOVD_DEBUG_PRIVATE)
8760 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8761 private_debug = true;
8763 else if (flags & GOVD_MAP)
8764 private_debug = false;
8765 else
8766 private_debug
8767 = lang_hooks.decls.omp_private_debug_clause (decl,
8768 !!(flags & GOVD_SHARED));
8769 if (private_debug)
8770 code = OMP_CLAUSE_PRIVATE;
8771 else if (flags & GOVD_MAP)
8773 code = OMP_CLAUSE_MAP;
8774 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8775 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8777 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8778 return 0;
8781 else if (flags & GOVD_SHARED)
8783 if (is_global_var (decl))
8785 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8786 while (ctx != NULL)
8788 splay_tree_node on
8789 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8790 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8791 | GOVD_PRIVATE | GOVD_REDUCTION
8792 | GOVD_LINEAR | GOVD_MAP)) != 0)
8793 break;
8794 ctx = ctx->outer_context;
8796 if (ctx == NULL)
8797 return 0;
8799 code = OMP_CLAUSE_SHARED;
8801 else if (flags & GOVD_PRIVATE)
8802 code = OMP_CLAUSE_PRIVATE;
8803 else if (flags & GOVD_FIRSTPRIVATE)
8805 code = OMP_CLAUSE_FIRSTPRIVATE;
8806 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8807 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8808 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8810 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8811 "%<target%> construct", decl);
8812 return 0;
8815 else if (flags & GOVD_LASTPRIVATE)
8816 code = OMP_CLAUSE_LASTPRIVATE;
8817 else if (flags & GOVD_ALIGNED)
8818 return 0;
8819 else
8820 gcc_unreachable ();
8822 if (((flags & GOVD_LASTPRIVATE)
8823 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8824 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8825 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8827 tree chain = *list_p;
8828 clause = build_omp_clause (input_location, code);
8829 OMP_CLAUSE_DECL (clause) = decl;
8830 OMP_CLAUSE_CHAIN (clause) = chain;
8831 if (private_debug)
8832 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8833 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8834 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8835 else if (code == OMP_CLAUSE_SHARED
8836 && (flags & GOVD_WRITTEN) == 0
8837 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8838 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8839 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8840 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8841 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8843 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8844 OMP_CLAUSE_DECL (nc) = decl;
8845 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8846 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8847 OMP_CLAUSE_DECL (clause)
8848 = build_simple_mem_ref_loc (input_location, decl);
8849 OMP_CLAUSE_DECL (clause)
8850 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8851 build_int_cst (build_pointer_type (char_type_node), 0));
8852 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8853 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8854 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8855 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8856 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8857 OMP_CLAUSE_CHAIN (nc) = chain;
8858 OMP_CLAUSE_CHAIN (clause) = nc;
8859 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8860 gimplify_omp_ctxp = ctx->outer_context;
8861 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8862 pre_p, NULL, is_gimple_val, fb_rvalue);
8863 gimplify_omp_ctxp = ctx;
8865 else if (code == OMP_CLAUSE_MAP)
8867 int kind;
8868 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8869 switch (flags & (GOVD_MAP_TO_ONLY
8870 | GOVD_MAP_FORCE
8871 | GOVD_MAP_FORCE_PRESENT))
8873 case 0:
8874 kind = GOMP_MAP_TOFROM;
8875 break;
8876 case GOVD_MAP_FORCE:
8877 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8878 break;
8879 case GOVD_MAP_TO_ONLY:
8880 kind = GOMP_MAP_TO;
8881 break;
8882 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8883 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8884 break;
8885 case GOVD_MAP_FORCE_PRESENT:
8886 kind = GOMP_MAP_FORCE_PRESENT;
8887 break;
8888 default:
8889 gcc_unreachable ();
8891 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8892 if (DECL_SIZE (decl)
8893 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8895 tree decl2 = DECL_VALUE_EXPR (decl);
8896 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8897 decl2 = TREE_OPERAND (decl2, 0);
8898 gcc_assert (DECL_P (decl2));
8899 tree mem = build_simple_mem_ref (decl2);
8900 OMP_CLAUSE_DECL (clause) = mem;
8901 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8902 if (gimplify_omp_ctxp->outer_context)
8904 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8905 omp_notice_variable (ctx, decl2, true);
8906 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8908 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8909 OMP_CLAUSE_MAP);
8910 OMP_CLAUSE_DECL (nc) = decl;
8911 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8912 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8913 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8914 else
8915 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8916 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8917 OMP_CLAUSE_CHAIN (clause) = nc;
8919 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8920 && lang_hooks.decls.omp_privatize_by_reference (decl))
8922 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8923 OMP_CLAUSE_SIZE (clause)
8924 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8925 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8926 gimplify_omp_ctxp = ctx->outer_context;
8927 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8928 pre_p, NULL, is_gimple_val, fb_rvalue);
8929 gimplify_omp_ctxp = ctx;
8930 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8931 OMP_CLAUSE_MAP);
8932 OMP_CLAUSE_DECL (nc) = decl;
8933 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8934 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8935 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8936 OMP_CLAUSE_CHAIN (clause) = nc;
8938 else
8939 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8941 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8943 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8944 OMP_CLAUSE_DECL (nc) = decl;
8945 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8946 OMP_CLAUSE_CHAIN (nc) = chain;
8947 OMP_CLAUSE_CHAIN (clause) = nc;
8948 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8949 gimplify_omp_ctxp = ctx->outer_context;
8950 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8951 gimplify_omp_ctxp = ctx;
8953 *list_p = clause;
8954 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8955 gimplify_omp_ctxp = ctx->outer_context;
8956 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8957 if (gimplify_omp_ctxp)
8958 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8959 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8960 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8961 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8962 true);
8963 gimplify_omp_ctxp = ctx;
8964 return 0;
8967 static void
8968 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8969 enum tree_code code)
8971 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8972 tree c, decl;
8974 if (body)
8976 struct gimplify_omp_ctx *octx;
8977 for (octx = ctx; octx; octx = octx->outer_context)
8978 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8979 break;
8980 if (octx)
8982 struct walk_stmt_info wi;
8983 memset (&wi, 0, sizeof (wi));
8984 walk_gimple_seq (body, omp_find_stores_stmt,
8985 omp_find_stores_op, &wi);
8988 while ((c = *list_p) != NULL)
8990 splay_tree_node n;
8991 bool remove = false;
8993 switch (OMP_CLAUSE_CODE (c))
8995 case OMP_CLAUSE_FIRSTPRIVATE:
8996 if ((ctx->region_type & ORT_TARGET)
8997 && (ctx->region_type & ORT_ACC) == 0
8998 && TYPE_ATOMIC (strip_array_types
8999 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9001 error_at (OMP_CLAUSE_LOCATION (c),
9002 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9003 "%<target%> construct", OMP_CLAUSE_DECL (c));
9004 remove = true;
9005 break;
9007 /* FALLTHRU */
9008 case OMP_CLAUSE_PRIVATE:
9009 case OMP_CLAUSE_SHARED:
9010 case OMP_CLAUSE_LINEAR:
9011 decl = OMP_CLAUSE_DECL (c);
9012 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9013 remove = !(n->value & GOVD_SEEN);
9014 if (! remove)
9016 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9017 if ((n->value & GOVD_DEBUG_PRIVATE)
9018 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9020 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9021 || ((n->value & GOVD_DATA_SHARE_CLASS)
9022 == GOVD_SHARED));
9023 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9024 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9027 && (n->value & GOVD_WRITTEN) == 0
9028 && DECL_P (decl)
9029 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9030 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9031 else if (DECL_P (decl)
9032 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9033 && (n->value & GOVD_WRITTEN) != 0)
9034 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9035 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9036 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9037 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9039 break;
9041 case OMP_CLAUSE_LASTPRIVATE:
9042 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9043 accurately reflect the presence of a FIRSTPRIVATE clause. */
9044 decl = OMP_CLAUSE_DECL (c);
9045 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9046 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9047 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9048 if (code == OMP_DISTRIBUTE
9049 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9051 remove = true;
9052 error_at (OMP_CLAUSE_LOCATION (c),
9053 "same variable used in %<firstprivate%> and "
9054 "%<lastprivate%> clauses on %<distribute%> "
9055 "construct");
9057 if (!remove
9058 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9059 && DECL_P (decl)
9060 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9061 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9062 break;
9064 case OMP_CLAUSE_ALIGNED:
9065 decl = OMP_CLAUSE_DECL (c);
9066 if (!is_global_var (decl))
9068 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9069 remove = n == NULL || !(n->value & GOVD_SEEN);
9070 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9072 struct gimplify_omp_ctx *octx;
9073 if (n != NULL
9074 && (n->value & (GOVD_DATA_SHARE_CLASS
9075 & ~GOVD_FIRSTPRIVATE)))
9076 remove = true;
9077 else
9078 for (octx = ctx->outer_context; octx;
9079 octx = octx->outer_context)
9081 n = splay_tree_lookup (octx->variables,
9082 (splay_tree_key) decl);
9083 if (n == NULL)
9084 continue;
9085 if (n->value & GOVD_LOCAL)
9086 break;
9087 /* We have to avoid assigning a shared variable
9088 to itself when trying to add
9089 __builtin_assume_aligned. */
9090 if (n->value & GOVD_SHARED)
9092 remove = true;
9093 break;
9098 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9100 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9101 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9102 remove = true;
9104 break;
9106 case OMP_CLAUSE_MAP:
9107 if (code == OMP_TARGET_EXIT_DATA
9108 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9110 remove = true;
9111 break;
9113 decl = OMP_CLAUSE_DECL (c);
9114 /* Data clauses associated with acc parallel reductions must be
9115 compatible with present_or_copy. Warn and adjust the clause
9116 if that is not the case. */
9117 if (ctx->region_type == ORT_ACC_PARALLEL)
9119 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9120 n = NULL;
9122 if (DECL_P (t))
9123 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9125 if (n && (n->value & GOVD_REDUCTION))
9127 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9129 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9130 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9131 && kind != GOMP_MAP_FORCE_PRESENT
9132 && kind != GOMP_MAP_POINTER)
9134 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9135 "incompatible data clause with reduction "
9136 "on %qE; promoting to present_or_copy",
9137 DECL_NAME (t));
9138 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9142 if (!DECL_P (decl))
9144 if ((ctx->region_type & ORT_TARGET) != 0
9145 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9147 if (TREE_CODE (decl) == INDIRECT_REF
9148 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9149 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9150 == REFERENCE_TYPE))
9151 decl = TREE_OPERAND (decl, 0);
9152 if (TREE_CODE (decl) == COMPONENT_REF)
9154 while (TREE_CODE (decl) == COMPONENT_REF)
9155 decl = TREE_OPERAND (decl, 0);
9156 if (DECL_P (decl))
9158 n = splay_tree_lookup (ctx->variables,
9159 (splay_tree_key) decl);
9160 if (!(n->value & GOVD_SEEN))
9161 remove = true;
9165 break;
9167 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9168 if ((ctx->region_type & ORT_TARGET) != 0
9169 && !(n->value & GOVD_SEEN)
9170 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9171 && (!is_global_var (decl)
9172 || !lookup_attribute ("omp declare target link",
9173 DECL_ATTRIBUTES (decl))))
9175 remove = true;
9176 /* For struct element mapping, if struct is never referenced
9177 in target block and none of the mapping has always modifier,
9178 remove all the struct element mappings, which immediately
9179 follow the GOMP_MAP_STRUCT map clause. */
9180 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9182 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9183 while (cnt--)
9184 OMP_CLAUSE_CHAIN (c)
9185 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9188 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9189 && code == OMP_TARGET_EXIT_DATA)
9190 remove = true;
9191 else if (DECL_SIZE (decl)
9192 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9193 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9194 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9195 && (OMP_CLAUSE_MAP_KIND (c)
9196 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9198 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9199 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9200 INTEGER_CST. */
9201 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9203 tree decl2 = DECL_VALUE_EXPR (decl);
9204 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9205 decl2 = TREE_OPERAND (decl2, 0);
9206 gcc_assert (DECL_P (decl2));
9207 tree mem = build_simple_mem_ref (decl2);
9208 OMP_CLAUSE_DECL (c) = mem;
9209 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9210 if (ctx->outer_context)
9212 omp_notice_variable (ctx->outer_context, decl2, true);
9213 omp_notice_variable (ctx->outer_context,
9214 OMP_CLAUSE_SIZE (c), true);
9216 if (((ctx->region_type & ORT_TARGET) != 0
9217 || !ctx->target_firstprivatize_array_bases)
9218 && ((n->value & GOVD_SEEN) == 0
9219 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9221 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9222 OMP_CLAUSE_MAP);
9223 OMP_CLAUSE_DECL (nc) = decl;
9224 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9225 if (ctx->target_firstprivatize_array_bases)
9226 OMP_CLAUSE_SET_MAP_KIND (nc,
9227 GOMP_MAP_FIRSTPRIVATE_POINTER);
9228 else
9229 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9230 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9231 OMP_CLAUSE_CHAIN (c) = nc;
9232 c = nc;
9235 else
9237 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9238 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9239 gcc_assert ((n->value & GOVD_SEEN) == 0
9240 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9241 == 0));
9243 break;
9245 case OMP_CLAUSE_TO:
9246 case OMP_CLAUSE_FROM:
9247 case OMP_CLAUSE__CACHE_:
9248 decl = OMP_CLAUSE_DECL (c);
9249 if (!DECL_P (decl))
9250 break;
9251 if (DECL_SIZE (decl)
9252 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9254 tree decl2 = DECL_VALUE_EXPR (decl);
9255 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9256 decl2 = TREE_OPERAND (decl2, 0);
9257 gcc_assert (DECL_P (decl2));
9258 tree mem = build_simple_mem_ref (decl2);
9259 OMP_CLAUSE_DECL (c) = mem;
9260 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9261 if (ctx->outer_context)
9263 omp_notice_variable (ctx->outer_context, decl2, true);
9264 omp_notice_variable (ctx->outer_context,
9265 OMP_CLAUSE_SIZE (c), true);
9268 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9269 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9270 break;
9272 case OMP_CLAUSE_REDUCTION:
9273 decl = OMP_CLAUSE_DECL (c);
9274 /* OpenACC reductions need a present_or_copy data clause.
9275 Add one if necessary. Error is the reduction is private. */
9276 if (ctx->region_type == ORT_ACC_PARALLEL)
9278 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9279 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9280 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9281 "reduction on %qE", DECL_NAME (decl));
9282 else if ((n->value & GOVD_MAP) == 0)
9284 tree next = OMP_CLAUSE_CHAIN (c);
9285 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9286 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9287 OMP_CLAUSE_DECL (nc) = decl;
9288 OMP_CLAUSE_CHAIN (c) = nc;
9289 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9290 while (1)
9292 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9293 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9294 break;
9295 nc = OMP_CLAUSE_CHAIN (nc);
9297 OMP_CLAUSE_CHAIN (nc) = next;
9298 n->value |= GOVD_MAP;
9301 if (DECL_P (decl)
9302 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9303 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9304 break;
9305 case OMP_CLAUSE_COPYIN:
9306 case OMP_CLAUSE_COPYPRIVATE:
9307 case OMP_CLAUSE_IF:
9308 case OMP_CLAUSE_NUM_THREADS:
9309 case OMP_CLAUSE_NUM_TEAMS:
9310 case OMP_CLAUSE_THREAD_LIMIT:
9311 case OMP_CLAUSE_DIST_SCHEDULE:
9312 case OMP_CLAUSE_DEVICE:
9313 case OMP_CLAUSE_SCHEDULE:
9314 case OMP_CLAUSE_NOWAIT:
9315 case OMP_CLAUSE_ORDERED:
9316 case OMP_CLAUSE_DEFAULT:
9317 case OMP_CLAUSE_UNTIED:
9318 case OMP_CLAUSE_COLLAPSE:
9319 case OMP_CLAUSE_FINAL:
9320 case OMP_CLAUSE_MERGEABLE:
9321 case OMP_CLAUSE_PROC_BIND:
9322 case OMP_CLAUSE_SAFELEN:
9323 case OMP_CLAUSE_SIMDLEN:
9324 case OMP_CLAUSE_DEPEND:
9325 case OMP_CLAUSE_PRIORITY:
9326 case OMP_CLAUSE_GRAINSIZE:
9327 case OMP_CLAUSE_NUM_TASKS:
9328 case OMP_CLAUSE_NOGROUP:
9329 case OMP_CLAUSE_THREADS:
9330 case OMP_CLAUSE_SIMD:
9331 case OMP_CLAUSE_HINT:
9332 case OMP_CLAUSE_DEFAULTMAP:
9333 case OMP_CLAUSE_USE_DEVICE_PTR:
9334 case OMP_CLAUSE_IS_DEVICE_PTR:
9335 case OMP_CLAUSE_ASYNC:
9336 case OMP_CLAUSE_WAIT:
9337 case OMP_CLAUSE_INDEPENDENT:
9338 case OMP_CLAUSE_NUM_GANGS:
9339 case OMP_CLAUSE_NUM_WORKERS:
9340 case OMP_CLAUSE_VECTOR_LENGTH:
9341 case OMP_CLAUSE_GANG:
9342 case OMP_CLAUSE_WORKER:
9343 case OMP_CLAUSE_VECTOR:
9344 case OMP_CLAUSE_AUTO:
9345 case OMP_CLAUSE_SEQ:
9346 case OMP_CLAUSE_TILE:
9347 break;
9349 default:
9350 gcc_unreachable ();
9353 if (remove)
9354 *list_p = OMP_CLAUSE_CHAIN (c);
9355 else
9356 list_p = &OMP_CLAUSE_CHAIN (c);
9359 /* Add in any implicit data sharing. */
9360 struct gimplify_adjust_omp_clauses_data data;
9361 data.list_p = list_p;
9362 data.pre_p = pre_p;
9363 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9365 gimplify_omp_ctxp = ctx->outer_context;
9366 delete_omp_context (ctx);
9369 /* Gimplify OACC_CACHE. */
9371 static void
9372 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9374 tree expr = *expr_p;
9376 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9377 OACC_CACHE);
9378 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9379 OACC_CACHE);
9381 /* TODO: Do something sensible with this information. */
9383 *expr_p = NULL_TREE;
9386 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9387 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9388 kind. The entry kind will replace the one in CLAUSE, while the exit
9389 kind will be used in a new omp_clause and returned to the caller. */
9391 static tree
9392 gimplify_oacc_declare_1 (tree clause)
9394 HOST_WIDE_INT kind, new_op;
9395 bool ret = false;
9396 tree c = NULL;
9398 kind = OMP_CLAUSE_MAP_KIND (clause);
9400 switch (kind)
9402 case GOMP_MAP_ALLOC:
9403 case GOMP_MAP_FORCE_ALLOC:
9404 case GOMP_MAP_FORCE_TO:
9405 new_op = GOMP_MAP_DELETE;
9406 ret = true;
9407 break;
9409 case GOMP_MAP_FORCE_FROM:
9410 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9411 new_op = GOMP_MAP_FORCE_FROM;
9412 ret = true;
9413 break;
9415 case GOMP_MAP_FORCE_TOFROM:
9416 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9417 new_op = GOMP_MAP_FORCE_FROM;
9418 ret = true;
9419 break;
9421 case GOMP_MAP_FROM:
9422 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9423 new_op = GOMP_MAP_FROM;
9424 ret = true;
9425 break;
9427 case GOMP_MAP_TOFROM:
9428 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9429 new_op = GOMP_MAP_FROM;
9430 ret = true;
9431 break;
9433 case GOMP_MAP_DEVICE_RESIDENT:
9434 case GOMP_MAP_FORCE_DEVICEPTR:
9435 case GOMP_MAP_FORCE_PRESENT:
9436 case GOMP_MAP_LINK:
9437 case GOMP_MAP_POINTER:
9438 case GOMP_MAP_TO:
9439 break;
9441 default:
9442 gcc_unreachable ();
9443 break;
9446 if (ret)
9448 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9449 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9450 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9453 return c;
9456 /* Gimplify OACC_DECLARE. */
9458 static void
9459 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9461 tree expr = *expr_p;
9462 gomp_target *stmt;
9463 tree clauses, t, decl;
9465 clauses = OACC_DECLARE_CLAUSES (expr);
9467 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9468 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9470 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9472 decl = OMP_CLAUSE_DECL (t);
9474 if (TREE_CODE (decl) == MEM_REF)
9475 decl = TREE_OPERAND (decl, 0);
9477 if (VAR_P (decl) && !is_oacc_declared (decl))
9479 tree attr = get_identifier ("oacc declare target");
9480 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9481 DECL_ATTRIBUTES (decl));
9484 if (VAR_P (decl)
9485 && !is_global_var (decl)
9486 && DECL_CONTEXT (decl) == current_function_decl)
9488 tree c = gimplify_oacc_declare_1 (t);
9489 if (c)
9491 if (oacc_declare_returns == NULL)
9492 oacc_declare_returns = new hash_map<tree, tree>;
9494 oacc_declare_returns->put (decl, c);
9498 if (gimplify_omp_ctxp)
9499 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9502 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9503 clauses);
9505 gimplify_seq_add_stmt (pre_p, stmt);
9507 *expr_p = NULL_TREE;
9510 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9511 gimplification of the body, as well as scanning the body for used
9512 variables. We need to do this scan now, because variable-sized
9513 decls will be decomposed during gimplification. */
9515 static void
9516 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9518 tree expr = *expr_p;
9519 gimple *g;
9520 gimple_seq body = NULL;
9522 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9523 OMP_PARALLEL_COMBINED (expr)
9524 ? ORT_COMBINED_PARALLEL
9525 : ORT_PARALLEL, OMP_PARALLEL);
9527 push_gimplify_context ();
9529 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9530 if (gimple_code (g) == GIMPLE_BIND)
9531 pop_gimplify_context (g);
9532 else
9533 pop_gimplify_context (NULL);
9535 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9536 OMP_PARALLEL);
9538 g = gimple_build_omp_parallel (body,
9539 OMP_PARALLEL_CLAUSES (expr),
9540 NULL_TREE, NULL_TREE);
9541 if (OMP_PARALLEL_COMBINED (expr))
9542 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9543 gimplify_seq_add_stmt (pre_p, g);
9544 *expr_p = NULL_TREE;
9547 /* Gimplify the contents of an OMP_TASK statement. This involves
9548 gimplification of the body, as well as scanning the body for used
9549 variables. We need to do this scan now, because variable-sized
9550 decls will be decomposed during gimplification. */
9552 static void
9553 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9555 tree expr = *expr_p;
9556 gimple *g;
9557 gimple_seq body = NULL;
9559 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9560 omp_find_clause (OMP_TASK_CLAUSES (expr),
9561 OMP_CLAUSE_UNTIED)
9562 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9564 push_gimplify_context ();
9566 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9567 if (gimple_code (g) == GIMPLE_BIND)
9568 pop_gimplify_context (g);
9569 else
9570 pop_gimplify_context (NULL);
9572 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9573 OMP_TASK);
9575 g = gimple_build_omp_task (body,
9576 OMP_TASK_CLAUSES (expr),
9577 NULL_TREE, NULL_TREE,
9578 NULL_TREE, NULL_TREE, NULL_TREE);
9579 gimplify_seq_add_stmt (pre_p, g);
9580 *expr_p = NULL_TREE;
9583 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9584 with non-NULL OMP_FOR_INIT. */
9586 static tree
9587 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9589 *walk_subtrees = 0;
9590 switch (TREE_CODE (*tp))
9592 case OMP_FOR:
9593 *walk_subtrees = 1;
9594 /* FALLTHRU */
9595 case OMP_SIMD:
9596 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9597 return *tp;
9598 break;
9599 case BIND_EXPR:
9600 case STATEMENT_LIST:
9601 case OMP_PARALLEL:
9602 *walk_subtrees = 1;
9603 break;
9604 default:
9605 break;
9607 return NULL_TREE;
9610 /* Gimplify the gross structure of an OMP_FOR statement. */
9612 static enum gimplify_status
9613 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9615 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9616 enum gimplify_status ret = GS_ALL_DONE;
9617 enum gimplify_status tret;
9618 gomp_for *gfor;
9619 gimple_seq for_body, for_pre_body;
9620 int i;
9621 bitmap has_decl_expr = NULL;
9622 enum omp_region_type ort = ORT_WORKSHARE;
9624 orig_for_stmt = for_stmt = *expr_p;
9626 switch (TREE_CODE (for_stmt))
9628 case OMP_FOR:
9629 case OMP_DISTRIBUTE:
9630 break;
9631 case OACC_LOOP:
9632 ort = ORT_ACC;
9633 break;
9634 case OMP_TASKLOOP:
9635 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9636 ort = ORT_UNTIED_TASK;
9637 else
9638 ort = ORT_TASK;
9639 break;
9640 case OMP_SIMD:
9641 ort = ORT_SIMD;
9642 break;
9643 default:
9644 gcc_unreachable ();
9647 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9648 clause for the IV. */
9649 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9651 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9652 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9653 decl = TREE_OPERAND (t, 0);
9654 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9655 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9656 && OMP_CLAUSE_DECL (c) == decl)
9658 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9659 break;
9663 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9665 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9666 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9667 find_combined_omp_for, NULL, NULL);
9668 if (inner_for_stmt == NULL_TREE)
9670 gcc_assert (seen_error ());
9671 *expr_p = NULL_TREE;
9672 return GS_ERROR;
9676 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9677 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9678 TREE_CODE (for_stmt));
9680 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9681 gimplify_omp_ctxp->distribute = true;
9683 /* Handle OMP_FOR_INIT. */
9684 for_pre_body = NULL;
9685 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9687 has_decl_expr = BITMAP_ALLOC (NULL);
9688 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9689 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9690 == VAR_DECL)
9692 t = OMP_FOR_PRE_BODY (for_stmt);
9693 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9695 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9697 tree_stmt_iterator si;
9698 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9699 tsi_next (&si))
9701 t = tsi_stmt (si);
9702 if (TREE_CODE (t) == DECL_EXPR
9703 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9704 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9708 if (OMP_FOR_PRE_BODY (for_stmt))
9710 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9711 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9712 else
9714 struct gimplify_omp_ctx ctx;
9715 memset (&ctx, 0, sizeof (ctx));
9716 ctx.region_type = ORT_NONE;
9717 gimplify_omp_ctxp = &ctx;
9718 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9719 gimplify_omp_ctxp = NULL;
9722 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9724 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9725 for_stmt = inner_for_stmt;
9727 /* For taskloop, need to gimplify the start, end and step before the
9728 taskloop, outside of the taskloop omp context. */
9729 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9731 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9733 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9734 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9736 TREE_OPERAND (t, 1)
9737 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9738 pre_p, NULL, false);
9739 tree c = build_omp_clause (input_location,
9740 OMP_CLAUSE_FIRSTPRIVATE);
9741 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9742 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9743 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9746 /* Handle OMP_FOR_COND. */
9747 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9748 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9750 TREE_OPERAND (t, 1)
9751 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9752 gimple_seq_empty_p (for_pre_body)
9753 ? pre_p : &for_pre_body, NULL,
9754 false);
9755 tree c = build_omp_clause (input_location,
9756 OMP_CLAUSE_FIRSTPRIVATE);
9757 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9758 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9759 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9762 /* Handle OMP_FOR_INCR. */
9763 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9764 if (TREE_CODE (t) == MODIFY_EXPR)
9766 decl = TREE_OPERAND (t, 0);
9767 t = TREE_OPERAND (t, 1);
9768 tree *tp = &TREE_OPERAND (t, 1);
9769 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9770 tp = &TREE_OPERAND (t, 0);
9772 if (!is_gimple_constant (*tp))
9774 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9775 ? pre_p : &for_pre_body;
9776 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9777 tree c = build_omp_clause (input_location,
9778 OMP_CLAUSE_FIRSTPRIVATE);
9779 OMP_CLAUSE_DECL (c) = *tp;
9780 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9781 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9786 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9787 OMP_TASKLOOP);
9790 if (orig_for_stmt != for_stmt)
9791 gimplify_omp_ctxp->combined_loop = true;
9793 for_body = NULL;
9794 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9795 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9796 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9797 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9799 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9800 bool is_doacross = false;
9801 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9803 is_doacross = true;
9804 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9805 (OMP_FOR_INIT (for_stmt))
9806 * 2);
9808 int collapse = 1, tile = 0;
9809 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9810 if (c)
9811 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9812 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9813 if (c)
9814 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9815 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9817 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9818 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9819 decl = TREE_OPERAND (t, 0);
9820 gcc_assert (DECL_P (decl));
9821 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9822 || POINTER_TYPE_P (TREE_TYPE (decl)));
9823 if (is_doacross)
9825 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9826 gimplify_omp_ctxp->loop_iter_var.quick_push
9827 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9828 else
9829 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9830 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9833 /* Make sure the iteration variable is private. */
9834 tree c = NULL_TREE;
9835 tree c2 = NULL_TREE;
9836 if (orig_for_stmt != for_stmt)
9837 /* Do this only on innermost construct for combined ones. */;
9838 else if (ort == ORT_SIMD)
9840 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9841 (splay_tree_key) decl);
9842 omp_is_private (gimplify_omp_ctxp, decl,
9843 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9844 != 1));
9845 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9846 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9847 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9849 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9850 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9851 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9852 if (has_decl_expr
9853 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9855 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9856 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9858 struct gimplify_omp_ctx *outer
9859 = gimplify_omp_ctxp->outer_context;
9860 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9862 if (outer->region_type == ORT_WORKSHARE
9863 && outer->combined_loop)
9865 n = splay_tree_lookup (outer->variables,
9866 (splay_tree_key)decl);
9867 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9869 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9870 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9872 else
9874 struct gimplify_omp_ctx *octx = outer->outer_context;
9875 if (octx
9876 && octx->region_type == ORT_COMBINED_PARALLEL
9877 && octx->outer_context
9878 && (octx->outer_context->region_type
9879 == ORT_WORKSHARE)
9880 && octx->outer_context->combined_loop)
9882 octx = octx->outer_context;
9883 n = splay_tree_lookup (octx->variables,
9884 (splay_tree_key)decl);
9885 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9887 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9888 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9895 OMP_CLAUSE_DECL (c) = decl;
9896 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9897 OMP_FOR_CLAUSES (for_stmt) = c;
9898 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9899 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9901 if (outer->region_type == ORT_WORKSHARE
9902 && outer->combined_loop)
9904 if (outer->outer_context
9905 && (outer->outer_context->region_type
9906 == ORT_COMBINED_PARALLEL))
9907 outer = outer->outer_context;
9908 else if (omp_check_private (outer, decl, false))
9909 outer = NULL;
9911 else if (((outer->region_type & ORT_TASK) != 0)
9912 && outer->combined_loop
9913 && !omp_check_private (gimplify_omp_ctxp,
9914 decl, false))
9916 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9918 omp_notice_variable (outer, decl, true);
9919 outer = NULL;
9921 if (outer)
9923 n = splay_tree_lookup (outer->variables,
9924 (splay_tree_key)decl);
9925 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9927 omp_add_variable (outer, decl,
9928 GOVD_LASTPRIVATE | GOVD_SEEN);
9929 if (outer->region_type == ORT_COMBINED_PARALLEL
9930 && outer->outer_context
9931 && (outer->outer_context->region_type
9932 == ORT_WORKSHARE)
9933 && outer->outer_context->combined_loop)
9935 outer = outer->outer_context;
9936 n = splay_tree_lookup (outer->variables,
9937 (splay_tree_key)decl);
9938 if (omp_check_private (outer, decl, false))
9939 outer = NULL;
9940 else if (n == NULL
9941 || ((n->value & GOVD_DATA_SHARE_CLASS)
9942 == 0))
9943 omp_add_variable (outer, decl,
9944 GOVD_LASTPRIVATE
9945 | GOVD_SEEN);
9946 else
9947 outer = NULL;
9949 if (outer && outer->outer_context
9950 && (outer->outer_context->region_type
9951 == ORT_COMBINED_TEAMS))
9953 outer = outer->outer_context;
9954 n = splay_tree_lookup (outer->variables,
9955 (splay_tree_key)decl);
9956 if (n == NULL
9957 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9958 omp_add_variable (outer, decl,
9959 GOVD_SHARED | GOVD_SEEN);
9960 else
9961 outer = NULL;
9963 if (outer && outer->outer_context)
9964 omp_notice_variable (outer->outer_context, decl,
9965 true);
9970 else
9972 bool lastprivate
9973 = (!has_decl_expr
9974 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
9975 struct gimplify_omp_ctx *outer
9976 = gimplify_omp_ctxp->outer_context;
9977 if (outer && lastprivate)
9979 if (outer->region_type == ORT_WORKSHARE
9980 && outer->combined_loop)
9982 n = splay_tree_lookup (outer->variables,
9983 (splay_tree_key)decl);
9984 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9986 lastprivate = false;
9987 outer = NULL;
9989 else if (outer->outer_context
9990 && (outer->outer_context->region_type
9991 == ORT_COMBINED_PARALLEL))
9992 outer = outer->outer_context;
9993 else if (omp_check_private (outer, decl, false))
9994 outer = NULL;
9996 else if (((outer->region_type & ORT_TASK) != 0)
9997 && outer->combined_loop
9998 && !omp_check_private (gimplify_omp_ctxp,
9999 decl, false))
10001 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10003 omp_notice_variable (outer, decl, true);
10004 outer = NULL;
10006 if (outer)
10008 n = splay_tree_lookup (outer->variables,
10009 (splay_tree_key)decl);
10010 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10012 omp_add_variable (outer, decl,
10013 GOVD_LASTPRIVATE | GOVD_SEEN);
10014 if (outer->region_type == ORT_COMBINED_PARALLEL
10015 && outer->outer_context
10016 && (outer->outer_context->region_type
10017 == ORT_WORKSHARE)
10018 && outer->outer_context->combined_loop)
10020 outer = outer->outer_context;
10021 n = splay_tree_lookup (outer->variables,
10022 (splay_tree_key)decl);
10023 if (omp_check_private (outer, decl, false))
10024 outer = NULL;
10025 else if (n == NULL
10026 || ((n->value & GOVD_DATA_SHARE_CLASS)
10027 == 0))
10028 omp_add_variable (outer, decl,
10029 GOVD_LASTPRIVATE
10030 | GOVD_SEEN);
10031 else
10032 outer = NULL;
10034 if (outer && outer->outer_context
10035 && (outer->outer_context->region_type
10036 == ORT_COMBINED_TEAMS))
10038 outer = outer->outer_context;
10039 n = splay_tree_lookup (outer->variables,
10040 (splay_tree_key)decl);
10041 if (n == NULL
10042 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10043 omp_add_variable (outer, decl,
10044 GOVD_SHARED | GOVD_SEEN);
10045 else
10046 outer = NULL;
10048 if (outer && outer->outer_context)
10049 omp_notice_variable (outer->outer_context, decl,
10050 true);
10055 c = build_omp_clause (input_location,
10056 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10057 : OMP_CLAUSE_PRIVATE);
10058 OMP_CLAUSE_DECL (c) = decl;
10059 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10060 OMP_FOR_CLAUSES (for_stmt) = c;
10061 omp_add_variable (gimplify_omp_ctxp, decl,
10062 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10063 | GOVD_EXPLICIT | GOVD_SEEN);
10064 c = NULL_TREE;
10067 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10068 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10069 else
10070 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10072 /* If DECL is not a gimple register, create a temporary variable to act
10073 as an iteration counter. This is valid, since DECL cannot be
10074 modified in the body of the loop. Similarly for any iteration vars
10075 in simd with collapse > 1 where the iterator vars must be
10076 lastprivate. */
10077 if (orig_for_stmt != for_stmt)
10078 var = decl;
10079 else if (!is_gimple_reg (decl)
10080 || (ort == ORT_SIMD
10081 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10083 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10084 /* Make sure omp_add_variable is not called on it prematurely.
10085 We call it ourselves a few lines later. */
10086 gimplify_omp_ctxp = NULL;
10087 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10088 gimplify_omp_ctxp = ctx;
10089 TREE_OPERAND (t, 0) = var;
10091 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10093 if (ort == ORT_SIMD
10094 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10096 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10097 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10098 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10099 OMP_CLAUSE_DECL (c2) = var;
10100 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10101 OMP_FOR_CLAUSES (for_stmt) = c2;
10102 omp_add_variable (gimplify_omp_ctxp, var,
10103 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10104 if (c == NULL_TREE)
10106 c = c2;
10107 c2 = NULL_TREE;
10110 else
10111 omp_add_variable (gimplify_omp_ctxp, var,
10112 GOVD_PRIVATE | GOVD_SEEN);
10114 else
10115 var = decl;
10117 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10118 is_gimple_val, fb_rvalue, false);
10119 ret = MIN (ret, tret);
10120 if (ret == GS_ERROR)
10121 return ret;
10123 /* Handle OMP_FOR_COND. */
10124 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10125 gcc_assert (COMPARISON_CLASS_P (t));
10126 gcc_assert (TREE_OPERAND (t, 0) == decl);
10128 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10129 is_gimple_val, fb_rvalue, false);
10130 ret = MIN (ret, tret);
10132 /* Handle OMP_FOR_INCR. */
10133 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10134 switch (TREE_CODE (t))
10136 case PREINCREMENT_EXPR:
10137 case POSTINCREMENT_EXPR:
10139 tree decl = TREE_OPERAND (t, 0);
10140 /* c_omp_for_incr_canonicalize_ptr() should have been
10141 called to massage things appropriately. */
10142 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10144 if (orig_for_stmt != for_stmt)
10145 break;
10146 t = build_int_cst (TREE_TYPE (decl), 1);
10147 if (c)
10148 OMP_CLAUSE_LINEAR_STEP (c) = t;
10149 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10150 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10151 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10152 break;
10155 case PREDECREMENT_EXPR:
10156 case POSTDECREMENT_EXPR:
10157 /* c_omp_for_incr_canonicalize_ptr() should have been
10158 called to massage things appropriately. */
10159 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10160 if (orig_for_stmt != for_stmt)
10161 break;
10162 t = build_int_cst (TREE_TYPE (decl), -1);
10163 if (c)
10164 OMP_CLAUSE_LINEAR_STEP (c) = t;
10165 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10166 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10167 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10168 break;
10170 case MODIFY_EXPR:
10171 gcc_assert (TREE_OPERAND (t, 0) == decl);
10172 TREE_OPERAND (t, 0) = var;
10174 t = TREE_OPERAND (t, 1);
10175 switch (TREE_CODE (t))
10177 case PLUS_EXPR:
10178 if (TREE_OPERAND (t, 1) == decl)
10180 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10181 TREE_OPERAND (t, 0) = var;
10182 break;
10185 /* Fallthru. */
10186 case MINUS_EXPR:
10187 case POINTER_PLUS_EXPR:
10188 gcc_assert (TREE_OPERAND (t, 0) == decl);
10189 TREE_OPERAND (t, 0) = var;
10190 break;
10191 default:
10192 gcc_unreachable ();
10195 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10196 is_gimple_val, fb_rvalue, false);
10197 ret = MIN (ret, tret);
10198 if (c)
10200 tree step = TREE_OPERAND (t, 1);
10201 tree stept = TREE_TYPE (decl);
10202 if (POINTER_TYPE_P (stept))
10203 stept = sizetype;
10204 step = fold_convert (stept, step);
10205 if (TREE_CODE (t) == MINUS_EXPR)
10206 step = fold_build1 (NEGATE_EXPR, stept, step);
10207 OMP_CLAUSE_LINEAR_STEP (c) = step;
10208 if (step != TREE_OPERAND (t, 1))
10210 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10211 &for_pre_body, NULL,
10212 is_gimple_val, fb_rvalue, false);
10213 ret = MIN (ret, tret);
10216 break;
10218 default:
10219 gcc_unreachable ();
10222 if (c2)
10224 gcc_assert (c);
10225 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10228 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10230 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10231 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10232 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10233 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10234 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10235 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10236 && OMP_CLAUSE_DECL (c) == decl)
10238 if (is_doacross && (collapse == 1 || i >= collapse))
10239 t = var;
10240 else
10242 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10243 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10244 gcc_assert (TREE_OPERAND (t, 0) == var);
10245 t = TREE_OPERAND (t, 1);
10246 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10247 || TREE_CODE (t) == MINUS_EXPR
10248 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10249 gcc_assert (TREE_OPERAND (t, 0) == var);
10250 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10251 is_doacross ? var : decl,
10252 TREE_OPERAND (t, 1));
10254 gimple_seq *seq;
10255 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10256 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10257 else
10258 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10259 gimplify_assign (decl, t, seq);
10264 BITMAP_FREE (has_decl_expr);
10266 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10268 push_gimplify_context ();
10269 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10271 OMP_FOR_BODY (orig_for_stmt)
10272 = build3 (BIND_EXPR, void_type_node, NULL,
10273 OMP_FOR_BODY (orig_for_stmt), NULL);
10274 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10278 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10279 &for_body);
10281 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10283 if (gimple_code (g) == GIMPLE_BIND)
10284 pop_gimplify_context (g);
10285 else
10286 pop_gimplify_context (NULL);
10289 if (orig_for_stmt != for_stmt)
10290 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10292 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10293 decl = TREE_OPERAND (t, 0);
10294 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10295 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10296 gimplify_omp_ctxp = ctx->outer_context;
10297 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10298 gimplify_omp_ctxp = ctx;
10299 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10300 TREE_OPERAND (t, 0) = var;
10301 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10302 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10303 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10306 gimplify_adjust_omp_clauses (pre_p, for_body,
10307 &OMP_FOR_CLAUSES (orig_for_stmt),
10308 TREE_CODE (orig_for_stmt));
10310 int kind;
10311 switch (TREE_CODE (orig_for_stmt))
10313 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10314 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10315 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10316 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10317 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10318 default:
10319 gcc_unreachable ();
10321 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10322 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10323 for_pre_body);
10324 if (orig_for_stmt != for_stmt)
10325 gimple_omp_for_set_combined_p (gfor, true);
10326 if (gimplify_omp_ctxp
10327 && (gimplify_omp_ctxp->combined_loop
10328 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10329 && gimplify_omp_ctxp->outer_context
10330 && gimplify_omp_ctxp->outer_context->combined_loop)))
10332 gimple_omp_for_set_combined_into_p (gfor, true);
10333 if (gimplify_omp_ctxp->combined_loop)
10334 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10335 else
10336 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10339 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10341 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10342 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10343 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10344 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10345 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10346 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10347 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10348 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10351 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10352 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10353 The outer taskloop stands for computing the number of iterations,
10354 counts for collapsed loops and holding taskloop specific clauses.
10355 The task construct stands for the effect of data sharing on the
10356 explicit task it creates and the inner taskloop stands for expansion
10357 of the static loop inside of the explicit task construct. */
10358 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10360 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10361 tree task_clauses = NULL_TREE;
10362 tree c = *gfor_clauses_ptr;
10363 tree *gtask_clauses_ptr = &task_clauses;
10364 tree outer_for_clauses = NULL_TREE;
10365 tree *gforo_clauses_ptr = &outer_for_clauses;
10366 for (; c; c = OMP_CLAUSE_CHAIN (c))
10367 switch (OMP_CLAUSE_CODE (c))
10369 /* These clauses are allowed on task, move them there. */
10370 case OMP_CLAUSE_SHARED:
10371 case OMP_CLAUSE_FIRSTPRIVATE:
10372 case OMP_CLAUSE_DEFAULT:
10373 case OMP_CLAUSE_IF:
10374 case OMP_CLAUSE_UNTIED:
10375 case OMP_CLAUSE_FINAL:
10376 case OMP_CLAUSE_MERGEABLE:
10377 case OMP_CLAUSE_PRIORITY:
10378 *gtask_clauses_ptr = c;
10379 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10380 break;
10381 case OMP_CLAUSE_PRIVATE:
10382 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10384 /* We want private on outer for and firstprivate
10385 on task. */
10386 *gtask_clauses_ptr
10387 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10388 OMP_CLAUSE_FIRSTPRIVATE);
10389 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10390 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10391 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10392 *gforo_clauses_ptr = c;
10393 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10395 else
10397 *gtask_clauses_ptr = c;
10398 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10400 break;
10401 /* These clauses go into outer taskloop clauses. */
10402 case OMP_CLAUSE_GRAINSIZE:
10403 case OMP_CLAUSE_NUM_TASKS:
10404 case OMP_CLAUSE_NOGROUP:
10405 *gforo_clauses_ptr = c;
10406 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10407 break;
10408 /* Taskloop clause we duplicate on both taskloops. */
10409 case OMP_CLAUSE_COLLAPSE:
10410 *gfor_clauses_ptr = c;
10411 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10412 *gforo_clauses_ptr = copy_node (c);
10413 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10414 break;
10415 /* For lastprivate, keep the clause on inner taskloop, and add
10416 a shared clause on task. If the same decl is also firstprivate,
10417 add also firstprivate clause on the inner taskloop. */
10418 case OMP_CLAUSE_LASTPRIVATE:
10419 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10421 /* For taskloop C++ lastprivate IVs, we want:
10422 1) private on outer taskloop
10423 2) firstprivate and shared on task
10424 3) lastprivate on inner taskloop */
10425 *gtask_clauses_ptr
10426 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10427 OMP_CLAUSE_FIRSTPRIVATE);
10428 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10429 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10430 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10431 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10432 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10433 OMP_CLAUSE_PRIVATE);
10434 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10435 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10436 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10437 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10439 *gfor_clauses_ptr = c;
10440 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10441 *gtask_clauses_ptr
10442 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10443 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10444 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10445 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10446 gtask_clauses_ptr
10447 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10448 break;
10449 default:
10450 gcc_unreachable ();
10452 *gfor_clauses_ptr = NULL_TREE;
10453 *gtask_clauses_ptr = NULL_TREE;
10454 *gforo_clauses_ptr = NULL_TREE;
10455 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10456 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10457 NULL_TREE, NULL_TREE, NULL_TREE);
10458 gimple_omp_task_set_taskloop_p (g, true);
10459 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10460 gomp_for *gforo
10461 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10462 gimple_omp_for_collapse (gfor),
10463 gimple_omp_for_pre_body (gfor));
10464 gimple_omp_for_set_pre_body (gfor, NULL);
10465 gimple_omp_for_set_combined_p (gforo, true);
10466 gimple_omp_for_set_combined_into_p (gfor, true);
10467 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10469 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10470 tree v = create_tmp_var (type);
10471 gimple_omp_for_set_index (gforo, i, v);
10472 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10473 gimple_omp_for_set_initial (gforo, i, t);
10474 gimple_omp_for_set_cond (gforo, i,
10475 gimple_omp_for_cond (gfor, i));
10476 t = unshare_expr (gimple_omp_for_final (gfor, i));
10477 gimple_omp_for_set_final (gforo, i, t);
10478 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10479 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10480 TREE_OPERAND (t, 0) = v;
10481 gimple_omp_for_set_incr (gforo, i, t);
10482 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10483 OMP_CLAUSE_DECL (t) = v;
10484 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10485 gimple_omp_for_set_clauses (gforo, t);
10487 gimplify_seq_add_stmt (pre_p, gforo);
10489 else
10490 gimplify_seq_add_stmt (pre_p, gfor);
10491 if (ret != GS_ALL_DONE)
10492 return GS_ERROR;
10493 *expr_p = NULL_TREE;
10494 return GS_ALL_DONE;
10497 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10498 of OMP_TARGET's body. */
10500 static tree
10501 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10503 *walk_subtrees = 0;
10504 switch (TREE_CODE (*tp))
10506 case OMP_TEAMS:
10507 return *tp;
10508 case BIND_EXPR:
10509 case STATEMENT_LIST:
10510 *walk_subtrees = 1;
10511 break;
10512 default:
10513 break;
10515 return NULL_TREE;
10518 /* Helper function of optimize_target_teams, determine if the expression
10519 can be computed safely before the target construct on the host. */
10521 static tree
10522 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10524 splay_tree_node n;
10526 if (TYPE_P (*tp))
10528 *walk_subtrees = 0;
10529 return NULL_TREE;
10531 switch (TREE_CODE (*tp))
10533 case VAR_DECL:
10534 case PARM_DECL:
10535 case RESULT_DECL:
10536 *walk_subtrees = 0;
10537 if (error_operand_p (*tp)
10538 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10539 || DECL_HAS_VALUE_EXPR_P (*tp)
10540 || DECL_THREAD_LOCAL_P (*tp)
10541 || TREE_SIDE_EFFECTS (*tp)
10542 || TREE_THIS_VOLATILE (*tp))
10543 return *tp;
10544 if (is_global_var (*tp)
10545 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10546 || lookup_attribute ("omp declare target link",
10547 DECL_ATTRIBUTES (*tp))))
10548 return *tp;
10549 if (VAR_P (*tp)
10550 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10551 && !is_global_var (*tp)
10552 && decl_function_context (*tp) == current_function_decl)
10553 return *tp;
10554 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10555 (splay_tree_key) *tp);
10556 if (n == NULL)
10558 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10559 return NULL_TREE;
10560 return *tp;
10562 else if (n->value & GOVD_LOCAL)
10563 return *tp;
10564 else if (n->value & GOVD_FIRSTPRIVATE)
10565 return NULL_TREE;
10566 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10567 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10568 return NULL_TREE;
10569 return *tp;
10570 case INTEGER_CST:
10571 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10572 return *tp;
10573 return NULL_TREE;
10574 case TARGET_EXPR:
10575 if (TARGET_EXPR_INITIAL (*tp)
10576 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10577 return *tp;
10578 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10579 walk_subtrees, NULL);
10580 /* Allow some reasonable subset of integral arithmetics. */
10581 case PLUS_EXPR:
10582 case MINUS_EXPR:
10583 case MULT_EXPR:
10584 case TRUNC_DIV_EXPR:
10585 case CEIL_DIV_EXPR:
10586 case FLOOR_DIV_EXPR:
10587 case ROUND_DIV_EXPR:
10588 case TRUNC_MOD_EXPR:
10589 case CEIL_MOD_EXPR:
10590 case FLOOR_MOD_EXPR:
10591 case ROUND_MOD_EXPR:
10592 case RDIV_EXPR:
10593 case EXACT_DIV_EXPR:
10594 case MIN_EXPR:
10595 case MAX_EXPR:
10596 case LSHIFT_EXPR:
10597 case RSHIFT_EXPR:
10598 case BIT_IOR_EXPR:
10599 case BIT_XOR_EXPR:
10600 case BIT_AND_EXPR:
10601 case NEGATE_EXPR:
10602 case ABS_EXPR:
10603 case BIT_NOT_EXPR:
10604 case NON_LVALUE_EXPR:
10605 CASE_CONVERT:
10606 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10607 return *tp;
10608 return NULL_TREE;
10609 /* And disallow anything else, except for comparisons. */
10610 default:
10611 if (COMPARISON_CLASS_P (*tp))
10612 return NULL_TREE;
10613 return *tp;
10617 /* Try to determine if the num_teams and/or thread_limit expressions
10618 can have their values determined already before entering the
10619 target construct.
10620 INTEGER_CSTs trivially are,
10621 integral decls that are firstprivate (explicitly or implicitly)
10622 or explicitly map(always, to:) or map(always, tofrom:) on the target
10623 region too, and expressions involving simple arithmetics on those
10624 too, function calls are not ok, dereferencing something neither etc.
10625 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10626 EXPR based on what we find:
10627 0 stands for clause not specified at all, use implementation default
10628 -1 stands for value that can't be determined easily before entering
10629 the target construct.
10630 If teams construct is not present at all, use 1 for num_teams
10631 and 0 for thread_limit (only one team is involved, and the thread
10632 limit is implementation defined. */
10634 static void
10635 optimize_target_teams (tree target, gimple_seq *pre_p)
10637 tree body = OMP_BODY (target);
10638 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10639 tree num_teams = integer_zero_node;
10640 tree thread_limit = integer_zero_node;
10641 location_t num_teams_loc = EXPR_LOCATION (target);
10642 location_t thread_limit_loc = EXPR_LOCATION (target);
10643 tree c, *p, expr;
10644 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10646 if (teams == NULL_TREE)
10647 num_teams = integer_one_node;
10648 else
10649 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10653 p = &num_teams;
10654 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10656 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10658 p = &thread_limit;
10659 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10661 else
10662 continue;
10663 expr = OMP_CLAUSE_OPERAND (c, 0);
10664 if (TREE_CODE (expr) == INTEGER_CST)
10666 *p = expr;
10667 continue;
10669 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10671 *p = integer_minus_one_node;
10672 continue;
10674 *p = expr;
10675 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10676 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10677 == GS_ERROR)
10679 gimplify_omp_ctxp = target_ctx;
10680 *p = integer_minus_one_node;
10681 continue;
10683 gimplify_omp_ctxp = target_ctx;
10684 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10685 OMP_CLAUSE_OPERAND (c, 0) = *p;
10687 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10688 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10689 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10690 OMP_TARGET_CLAUSES (target) = c;
10691 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10692 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10693 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10694 OMP_TARGET_CLAUSES (target) = c;
10697 /* Gimplify the gross structure of several OMP constructs. */
10699 static void
10700 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10702 tree expr = *expr_p;
10703 gimple *stmt;
10704 gimple_seq body = NULL;
10705 enum omp_region_type ort;
10707 switch (TREE_CODE (expr))
10709 case OMP_SECTIONS:
10710 case OMP_SINGLE:
10711 ort = ORT_WORKSHARE;
10712 break;
10713 case OMP_TARGET:
10714 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10715 break;
10716 case OACC_KERNELS:
10717 ort = ORT_ACC_KERNELS;
10718 break;
10719 case OACC_PARALLEL:
10720 ort = ORT_ACC_PARALLEL;
10721 break;
10722 case OACC_DATA:
10723 ort = ORT_ACC_DATA;
10724 break;
10725 case OMP_TARGET_DATA:
10726 ort = ORT_TARGET_DATA;
10727 break;
10728 case OMP_TEAMS:
10729 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10730 break;
10731 case OACC_HOST_DATA:
10732 ort = ORT_ACC_HOST_DATA;
10733 break;
10734 default:
10735 gcc_unreachable ();
10737 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10738 TREE_CODE (expr));
10739 if (TREE_CODE (expr) == OMP_TARGET)
10740 optimize_target_teams (expr, pre_p);
10741 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10743 push_gimplify_context ();
10744 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10745 if (gimple_code (g) == GIMPLE_BIND)
10746 pop_gimplify_context (g);
10747 else
10748 pop_gimplify_context (NULL);
10749 if ((ort & ORT_TARGET_DATA) != 0)
10751 enum built_in_function end_ix;
10752 switch (TREE_CODE (expr))
10754 case OACC_DATA:
10755 case OACC_HOST_DATA:
10756 end_ix = BUILT_IN_GOACC_DATA_END;
10757 break;
10758 case OMP_TARGET_DATA:
10759 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10760 break;
10761 default:
10762 gcc_unreachable ();
10764 tree fn = builtin_decl_explicit (end_ix);
10765 g = gimple_build_call (fn, 0);
10766 gimple_seq cleanup = NULL;
10767 gimple_seq_add_stmt (&cleanup, g);
10768 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10769 body = NULL;
10770 gimple_seq_add_stmt (&body, g);
10773 else
10774 gimplify_and_add (OMP_BODY (expr), &body);
10775 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10776 TREE_CODE (expr));
10778 switch (TREE_CODE (expr))
10780 case OACC_DATA:
10781 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10782 OMP_CLAUSES (expr));
10783 break;
10784 case OACC_KERNELS:
10785 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10786 OMP_CLAUSES (expr));
10787 break;
10788 case OACC_HOST_DATA:
10789 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10790 OMP_CLAUSES (expr));
10791 break;
10792 case OACC_PARALLEL:
10793 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10794 OMP_CLAUSES (expr));
10795 break;
10796 case OMP_SECTIONS:
10797 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10798 break;
10799 case OMP_SINGLE:
10800 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10801 break;
10802 case OMP_TARGET:
10803 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10804 OMP_CLAUSES (expr));
10805 break;
10806 case OMP_TARGET_DATA:
10807 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10808 OMP_CLAUSES (expr));
10809 break;
10810 case OMP_TEAMS:
10811 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10812 break;
10813 default:
10814 gcc_unreachable ();
10817 gimplify_seq_add_stmt (pre_p, stmt);
10818 *expr_p = NULL_TREE;
10821 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10822 target update constructs. */
10824 static void
10825 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10827 tree expr = *expr_p;
10828 int kind;
10829 gomp_target *stmt;
10830 enum omp_region_type ort = ORT_WORKSHARE;
10832 switch (TREE_CODE (expr))
10834 case OACC_ENTER_DATA:
10835 case OACC_EXIT_DATA:
10836 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10837 ort = ORT_ACC;
10838 break;
10839 case OACC_UPDATE:
10840 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10841 ort = ORT_ACC;
10842 break;
10843 case OMP_TARGET_UPDATE:
10844 kind = GF_OMP_TARGET_KIND_UPDATE;
10845 break;
10846 case OMP_TARGET_ENTER_DATA:
10847 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10848 break;
10849 case OMP_TARGET_EXIT_DATA:
10850 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10851 break;
10852 default:
10853 gcc_unreachable ();
10855 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10856 ort, TREE_CODE (expr));
10857 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10858 TREE_CODE (expr));
10859 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10861 gimplify_seq_add_stmt (pre_p, stmt);
10862 *expr_p = NULL_TREE;
10865 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10866 stabilized the lhs of the atomic operation as *ADDR. Return true if
10867 EXPR is this stabilized form. */
10869 static bool
10870 goa_lhs_expr_p (tree expr, tree addr)
10872 /* Also include casts to other type variants. The C front end is fond
10873 of adding these for e.g. volatile variables. This is like
10874 STRIP_TYPE_NOPS but includes the main variant lookup. */
10875 STRIP_USELESS_TYPE_CONVERSION (expr);
10877 if (TREE_CODE (expr) == INDIRECT_REF)
10879 expr = TREE_OPERAND (expr, 0);
10880 while (expr != addr
10881 && (CONVERT_EXPR_P (expr)
10882 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10883 && TREE_CODE (expr) == TREE_CODE (addr)
10884 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10886 expr = TREE_OPERAND (expr, 0);
10887 addr = TREE_OPERAND (addr, 0);
10889 if (expr == addr)
10890 return true;
10891 return (TREE_CODE (addr) == ADDR_EXPR
10892 && TREE_CODE (expr) == ADDR_EXPR
10893 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10895 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10896 return true;
10897 return false;
10900 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10901 expression does not involve the lhs, evaluate it into a temporary.
10902 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10903 or -1 if an error was encountered. */
10905 static int
10906 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10907 tree lhs_var)
10909 tree expr = *expr_p;
10910 int saw_lhs;
10912 if (goa_lhs_expr_p (expr, lhs_addr))
10914 *expr_p = lhs_var;
10915 return 1;
10917 if (is_gimple_val (expr))
10918 return 0;
10920 saw_lhs = 0;
10921 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10923 case tcc_binary:
10924 case tcc_comparison:
10925 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10926 lhs_var);
10927 /* FALLTHRU */
10928 case tcc_unary:
10929 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10930 lhs_var);
10931 break;
10932 case tcc_expression:
10933 switch (TREE_CODE (expr))
10935 case TRUTH_ANDIF_EXPR:
10936 case TRUTH_ORIF_EXPR:
10937 case TRUTH_AND_EXPR:
10938 case TRUTH_OR_EXPR:
10939 case TRUTH_XOR_EXPR:
10940 case BIT_INSERT_EXPR:
10941 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10942 lhs_addr, lhs_var);
10943 /* FALLTHRU */
10944 case TRUTH_NOT_EXPR:
10945 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10946 lhs_addr, lhs_var);
10947 break;
10948 case COMPOUND_EXPR:
10949 /* Break out any preevaluations from cp_build_modify_expr. */
10950 for (; TREE_CODE (expr) == COMPOUND_EXPR;
10951 expr = TREE_OPERAND (expr, 1))
10952 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10953 *expr_p = expr;
10954 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10955 default:
10956 break;
10958 break;
10959 case tcc_reference:
10960 if (TREE_CODE (expr) == BIT_FIELD_REF)
10961 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10962 lhs_addr, lhs_var);
10963 break;
10964 default:
10965 break;
10968 if (saw_lhs == 0)
10970 enum gimplify_status gs;
10971 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10972 if (gs != GS_ALL_DONE)
10973 saw_lhs = -1;
10976 return saw_lhs;
10979 /* Gimplify an OMP_ATOMIC statement. */
10981 static enum gimplify_status
10982 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10984 tree addr = TREE_OPERAND (*expr_p, 0);
10985 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10986 ? NULL : TREE_OPERAND (*expr_p, 1);
10987 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10988 tree tmp_load;
10989 gomp_atomic_load *loadstmt;
10990 gomp_atomic_store *storestmt;
10992 tmp_load = create_tmp_reg (type);
10993 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10994 return GS_ERROR;
10996 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10997 != GS_ALL_DONE)
10998 return GS_ERROR;
11000 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11001 gimplify_seq_add_stmt (pre_p, loadstmt);
11002 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11003 != GS_ALL_DONE)
11004 return GS_ERROR;
11006 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11007 rhs = tmp_load;
11008 storestmt = gimple_build_omp_atomic_store (rhs);
11009 gimplify_seq_add_stmt (pre_p, storestmt);
11010 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11012 gimple_omp_atomic_set_seq_cst (loadstmt);
11013 gimple_omp_atomic_set_seq_cst (storestmt);
11015 switch (TREE_CODE (*expr_p))
11017 case OMP_ATOMIC_READ:
11018 case OMP_ATOMIC_CAPTURE_OLD:
11019 *expr_p = tmp_load;
11020 gimple_omp_atomic_set_need_value (loadstmt);
11021 break;
11022 case OMP_ATOMIC_CAPTURE_NEW:
11023 *expr_p = rhs;
11024 gimple_omp_atomic_set_need_value (storestmt);
11025 break;
11026 default:
11027 *expr_p = NULL;
11028 break;
11031 return GS_ALL_DONE;
11034 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11035 body, and adding some EH bits. */
11037 static enum gimplify_status
11038 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11040 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11041 gimple *body_stmt;
11042 gtransaction *trans_stmt;
11043 gimple_seq body = NULL;
11044 int subcode = 0;
11046 /* Wrap the transaction body in a BIND_EXPR so we have a context
11047 where to put decls for OMP. */
11048 if (TREE_CODE (tbody) != BIND_EXPR)
11050 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11051 TREE_SIDE_EFFECTS (bind) = 1;
11052 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11053 TRANSACTION_EXPR_BODY (expr) = bind;
11056 push_gimplify_context ();
11057 temp = voidify_wrapper_expr (*expr_p, NULL);
11059 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11060 pop_gimplify_context (body_stmt);
11062 trans_stmt = gimple_build_transaction (body);
11063 if (TRANSACTION_EXPR_OUTER (expr))
11064 subcode = GTMA_IS_OUTER;
11065 else if (TRANSACTION_EXPR_RELAXED (expr))
11066 subcode = GTMA_IS_RELAXED;
11067 gimple_transaction_set_subcode (trans_stmt, subcode);
11069 gimplify_seq_add_stmt (pre_p, trans_stmt);
11071 if (temp)
11073 *expr_p = temp;
11074 return GS_OK;
11077 *expr_p = NULL_TREE;
11078 return GS_ALL_DONE;
11081 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11082 is the OMP_BODY of the original EXPR (which has already been
11083 gimplified so it's not present in the EXPR).
11085 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11087 static gimple *
11088 gimplify_omp_ordered (tree expr, gimple_seq body)
11090 tree c, decls;
11091 int failures = 0;
11092 unsigned int i;
11093 tree source_c = NULL_TREE;
11094 tree sink_c = NULL_TREE;
11096 if (gimplify_omp_ctxp)
11098 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11099 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11100 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11101 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11102 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11104 error_at (OMP_CLAUSE_LOCATION (c),
11105 "%<ordered%> construct with %<depend%> clause must be "
11106 "closely nested inside a loop with %<ordered%> clause "
11107 "with a parameter");
11108 failures++;
11110 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11111 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11113 bool fail = false;
11114 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11115 decls && TREE_CODE (decls) == TREE_LIST;
11116 decls = TREE_CHAIN (decls), ++i)
11117 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11118 continue;
11119 else if (TREE_VALUE (decls)
11120 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11122 error_at (OMP_CLAUSE_LOCATION (c),
11123 "variable %qE is not an iteration "
11124 "of outermost loop %d, expected %qE",
11125 TREE_VALUE (decls), i + 1,
11126 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11127 fail = true;
11128 failures++;
11130 else
11131 TREE_VALUE (decls)
11132 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11133 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11135 error_at (OMP_CLAUSE_LOCATION (c),
11136 "number of variables in %<depend(sink)%> "
11137 "clause does not match number of "
11138 "iteration variables");
11139 failures++;
11141 sink_c = c;
11143 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11144 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11146 if (source_c)
11148 error_at (OMP_CLAUSE_LOCATION (c),
11149 "more than one %<depend(source)%> clause on an "
11150 "%<ordered%> construct");
11151 failures++;
11153 else
11154 source_c = c;
11157 if (source_c && sink_c)
11159 error_at (OMP_CLAUSE_LOCATION (source_c),
11160 "%<depend(source)%> clause specified together with "
11161 "%<depend(sink:)%> clauses on the same construct");
11162 failures++;
11165 if (failures)
11166 return gimple_build_nop ();
11167 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11170 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11171 expression produces a value to be used as an operand inside a GIMPLE
11172 statement, the value will be stored back in *EXPR_P. This value will
11173 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11174 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11175 emitted in PRE_P and POST_P.
11177 Additionally, this process may overwrite parts of the input
11178 expression during gimplification. Ideally, it should be
11179 possible to do non-destructive gimplification.
11181 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11182 the expression needs to evaluate to a value to be used as
11183 an operand in a GIMPLE statement, this value will be stored in
11184 *EXPR_P on exit. This happens when the caller specifies one
11185 of fb_lvalue or fb_rvalue fallback flags.
11187 PRE_P will contain the sequence of GIMPLE statements corresponding
11188 to the evaluation of EXPR and all the side-effects that must
11189 be executed before the main expression. On exit, the last
11190 statement of PRE_P is the core statement being gimplified. For
11191 instance, when gimplifying 'if (++a)' the last statement in
11192 PRE_P will be 'if (t.1)' where t.1 is the result of
11193 pre-incrementing 'a'.
11195 POST_P will contain the sequence of GIMPLE statements corresponding
11196 to the evaluation of all the side-effects that must be executed
11197 after the main expression. If this is NULL, the post
11198 side-effects are stored at the end of PRE_P.
11200 The reason why the output is split in two is to handle post
11201 side-effects explicitly. In some cases, an expression may have
11202 inner and outer post side-effects which need to be emitted in
11203 an order different from the one given by the recursive
11204 traversal. For instance, for the expression (*p--)++ the post
11205 side-effects of '--' must actually occur *after* the post
11206 side-effects of '++'. However, gimplification will first visit
11207 the inner expression, so if a separate POST sequence was not
11208 used, the resulting sequence would be:
11210 1 t.1 = *p
11211 2 p = p - 1
11212 3 t.2 = t.1 + 1
11213 4 *p = t.2
11215 However, the post-decrement operation in line #2 must not be
11216 evaluated until after the store to *p at line #4, so the
11217 correct sequence should be:
11219 1 t.1 = *p
11220 2 t.2 = t.1 + 1
11221 3 *p = t.2
11222 4 p = p - 1
11224 So, by specifying a separate post queue, it is possible
11225 to emit the post side-effects in the correct order.
11226 If POST_P is NULL, an internal queue will be used. Before
11227 returning to the caller, the sequence POST_P is appended to
11228 the main output sequence PRE_P.
11230 GIMPLE_TEST_F points to a function that takes a tree T and
11231 returns nonzero if T is in the GIMPLE form requested by the
11232 caller. The GIMPLE predicates are in gimple.c.
11234 FALLBACK tells the function what sort of a temporary we want if
11235 gimplification cannot produce an expression that complies with
11236 GIMPLE_TEST_F.
11238 fb_none means that no temporary should be generated
11239 fb_rvalue means that an rvalue is OK to generate
11240 fb_lvalue means that an lvalue is OK to generate
11241 fb_either means that either is OK, but an lvalue is preferable.
11242 fb_mayfail means that gimplification may fail (in which case
11243 GS_ERROR will be returned)
11245 The return value is either GS_ERROR or GS_ALL_DONE, since this
11246 function iterates until EXPR is completely gimplified or an error
11247 occurs. */
11249 enum gimplify_status
11250 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11251 bool (*gimple_test_f) (tree), fallback_t fallback)
11253 tree tmp;
11254 gimple_seq internal_pre = NULL;
11255 gimple_seq internal_post = NULL;
11256 tree save_expr;
11257 bool is_statement;
11258 location_t saved_location;
11259 enum gimplify_status ret;
11260 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11261 tree label;
11263 save_expr = *expr_p;
11264 if (save_expr == NULL_TREE)
11265 return GS_ALL_DONE;
11267 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11268 is_statement = gimple_test_f == is_gimple_stmt;
11269 if (is_statement)
11270 gcc_assert (pre_p);
11272 /* Consistency checks. */
11273 if (gimple_test_f == is_gimple_reg)
11274 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11275 else if (gimple_test_f == is_gimple_val
11276 || gimple_test_f == is_gimple_call_addr
11277 || gimple_test_f == is_gimple_condexpr
11278 || gimple_test_f == is_gimple_mem_rhs
11279 || gimple_test_f == is_gimple_mem_rhs_or_call
11280 || gimple_test_f == is_gimple_reg_rhs
11281 || gimple_test_f == is_gimple_reg_rhs_or_call
11282 || gimple_test_f == is_gimple_asm_val
11283 || gimple_test_f == is_gimple_mem_ref_addr)
11284 gcc_assert (fallback & fb_rvalue);
11285 else if (gimple_test_f == is_gimple_min_lval
11286 || gimple_test_f == is_gimple_lvalue)
11287 gcc_assert (fallback & fb_lvalue);
11288 else if (gimple_test_f == is_gimple_addressable)
11289 gcc_assert (fallback & fb_either);
11290 else if (gimple_test_f == is_gimple_stmt)
11291 gcc_assert (fallback == fb_none);
11292 else
11294 /* We should have recognized the GIMPLE_TEST_F predicate to
11295 know what kind of fallback to use in case a temporary is
11296 needed to hold the value or address of *EXPR_P. */
11297 gcc_unreachable ();
11300 /* We used to check the predicate here and return immediately if it
11301 succeeds. This is wrong; the design is for gimplification to be
11302 idempotent, and for the predicates to only test for valid forms, not
11303 whether they are fully simplified. */
11304 if (pre_p == NULL)
11305 pre_p = &internal_pre;
11307 if (post_p == NULL)
11308 post_p = &internal_post;
11310 /* Remember the last statements added to PRE_P and POST_P. Every
11311 new statement added by the gimplification helpers needs to be
11312 annotated with location information. To centralize the
11313 responsibility, we remember the last statement that had been
11314 added to both queues before gimplifying *EXPR_P. If
11315 gimplification produces new statements in PRE_P and POST_P, those
11316 statements will be annotated with the same location information
11317 as *EXPR_P. */
11318 pre_last_gsi = gsi_last (*pre_p);
11319 post_last_gsi = gsi_last (*post_p);
11321 saved_location = input_location;
11322 if (save_expr != error_mark_node
11323 && EXPR_HAS_LOCATION (*expr_p))
11324 input_location = EXPR_LOCATION (*expr_p);
11326 /* Loop over the specific gimplifiers until the toplevel node
11327 remains the same. */
11330 /* Strip away as many useless type conversions as possible
11331 at the toplevel. */
11332 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11334 /* Remember the expr. */
11335 save_expr = *expr_p;
11337 /* Die, die, die, my darling. */
11338 if (error_operand_p (save_expr))
11340 ret = GS_ERROR;
11341 break;
11344 /* Do any language-specific gimplification. */
11345 ret = ((enum gimplify_status)
11346 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11347 if (ret == GS_OK)
11349 if (*expr_p == NULL_TREE)
11350 break;
11351 if (*expr_p != save_expr)
11352 continue;
11354 else if (ret != GS_UNHANDLED)
11355 break;
11357 /* Make sure that all the cases set 'ret' appropriately. */
11358 ret = GS_UNHANDLED;
11359 switch (TREE_CODE (*expr_p))
11361 /* First deal with the special cases. */
11363 case POSTINCREMENT_EXPR:
11364 case POSTDECREMENT_EXPR:
11365 case PREINCREMENT_EXPR:
11366 case PREDECREMENT_EXPR:
11367 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11368 fallback != fb_none,
11369 TREE_TYPE (*expr_p));
11370 break;
11372 case VIEW_CONVERT_EXPR:
11373 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11374 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11376 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11377 post_p, is_gimple_val, fb_rvalue);
11378 recalculate_side_effects (*expr_p);
11379 break;
11381 /* Fallthru. */
11383 case ARRAY_REF:
11384 case ARRAY_RANGE_REF:
11385 case REALPART_EXPR:
11386 case IMAGPART_EXPR:
11387 case COMPONENT_REF:
11388 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11389 fallback ? fallback : fb_rvalue);
11390 break;
11392 case COND_EXPR:
11393 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11395 /* C99 code may assign to an array in a structure value of a
11396 conditional expression, and this has undefined behavior
11397 only on execution, so create a temporary if an lvalue is
11398 required. */
11399 if (fallback == fb_lvalue)
11401 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11402 mark_addressable (*expr_p);
11403 ret = GS_OK;
11405 break;
11407 case CALL_EXPR:
11408 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11410 /* C99 code may assign to an array in a structure returned
11411 from a function, and this has undefined behavior only on
11412 execution, so create a temporary if an lvalue is
11413 required. */
11414 if (fallback == fb_lvalue)
11416 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11417 mark_addressable (*expr_p);
11418 ret = GS_OK;
11420 break;
11422 case TREE_LIST:
11423 gcc_unreachable ();
11425 case COMPOUND_EXPR:
11426 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11427 break;
11429 case COMPOUND_LITERAL_EXPR:
11430 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11431 gimple_test_f, fallback);
11432 break;
11434 case MODIFY_EXPR:
11435 case INIT_EXPR:
11436 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11437 fallback != fb_none);
11438 break;
11440 case TRUTH_ANDIF_EXPR:
11441 case TRUTH_ORIF_EXPR:
11443 /* Preserve the original type of the expression and the
11444 source location of the outer expression. */
11445 tree org_type = TREE_TYPE (*expr_p);
11446 *expr_p = gimple_boolify (*expr_p);
11447 *expr_p = build3_loc (input_location, COND_EXPR,
11448 org_type, *expr_p,
11449 fold_convert_loc
11450 (input_location,
11451 org_type, boolean_true_node),
11452 fold_convert_loc
11453 (input_location,
11454 org_type, boolean_false_node));
11455 ret = GS_OK;
11456 break;
11459 case TRUTH_NOT_EXPR:
11461 tree type = TREE_TYPE (*expr_p);
11462 /* The parsers are careful to generate TRUTH_NOT_EXPR
11463 only with operands that are always zero or one.
11464 We do not fold here but handle the only interesting case
11465 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11466 *expr_p = gimple_boolify (*expr_p);
11467 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11468 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11469 TREE_TYPE (*expr_p),
11470 TREE_OPERAND (*expr_p, 0));
11471 else
11472 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11473 TREE_TYPE (*expr_p),
11474 TREE_OPERAND (*expr_p, 0),
11475 build_int_cst (TREE_TYPE (*expr_p), 1));
11476 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11477 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11478 ret = GS_OK;
11479 break;
11482 case ADDR_EXPR:
11483 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11484 break;
11486 case ANNOTATE_EXPR:
11488 tree cond = TREE_OPERAND (*expr_p, 0);
11489 tree kind = TREE_OPERAND (*expr_p, 1);
11490 tree data = TREE_OPERAND (*expr_p, 2);
11491 tree type = TREE_TYPE (cond);
11492 if (!INTEGRAL_TYPE_P (type))
11494 *expr_p = cond;
11495 ret = GS_OK;
11496 break;
11498 tree tmp = create_tmp_var (type);
11499 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11500 gcall *call
11501 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11502 gimple_call_set_lhs (call, tmp);
11503 gimplify_seq_add_stmt (pre_p, call);
11504 *expr_p = tmp;
11505 ret = GS_ALL_DONE;
11506 break;
11509 case VA_ARG_EXPR:
11510 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11511 break;
11513 CASE_CONVERT:
11514 if (IS_EMPTY_STMT (*expr_p))
11516 ret = GS_ALL_DONE;
11517 break;
11520 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11521 || fallback == fb_none)
11523 /* Just strip a conversion to void (or in void context) and
11524 try again. */
11525 *expr_p = TREE_OPERAND (*expr_p, 0);
11526 ret = GS_OK;
11527 break;
11530 ret = gimplify_conversion (expr_p);
11531 if (ret == GS_ERROR)
11532 break;
11533 if (*expr_p != save_expr)
11534 break;
11535 /* FALLTHRU */
11537 case FIX_TRUNC_EXPR:
11538 /* unary_expr: ... | '(' cast ')' val | ... */
11539 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11540 is_gimple_val, fb_rvalue);
11541 recalculate_side_effects (*expr_p);
11542 break;
11544 case INDIRECT_REF:
11546 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11547 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11548 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11550 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11551 if (*expr_p != save_expr)
11553 ret = GS_OK;
11554 break;
11557 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11558 is_gimple_reg, fb_rvalue);
11559 if (ret == GS_ERROR)
11560 break;
11562 recalculate_side_effects (*expr_p);
11563 *expr_p = fold_build2_loc (input_location, MEM_REF,
11564 TREE_TYPE (*expr_p),
11565 TREE_OPERAND (*expr_p, 0),
11566 build_int_cst (saved_ptr_type, 0));
11567 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11568 TREE_THIS_NOTRAP (*expr_p) = notrap;
11569 ret = GS_OK;
11570 break;
11573 /* We arrive here through the various re-gimplifcation paths. */
11574 case MEM_REF:
11575 /* First try re-folding the whole thing. */
11576 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11577 TREE_OPERAND (*expr_p, 0),
11578 TREE_OPERAND (*expr_p, 1));
11579 if (tmp)
11581 REF_REVERSE_STORAGE_ORDER (tmp)
11582 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11583 *expr_p = tmp;
11584 recalculate_side_effects (*expr_p);
11585 ret = GS_OK;
11586 break;
11588 /* Avoid re-gimplifying the address operand if it is already
11589 in suitable form. Re-gimplifying would mark the address
11590 operand addressable. Always gimplify when not in SSA form
11591 as we still may have to gimplify decls with value-exprs. */
11592 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11593 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11595 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11596 is_gimple_mem_ref_addr, fb_rvalue);
11597 if (ret == GS_ERROR)
11598 break;
11600 recalculate_side_effects (*expr_p);
11601 ret = GS_ALL_DONE;
11602 break;
11604 /* Constants need not be gimplified. */
11605 case INTEGER_CST:
11606 case REAL_CST:
11607 case FIXED_CST:
11608 case STRING_CST:
11609 case COMPLEX_CST:
11610 case VECTOR_CST:
11611 /* Drop the overflow flag on constants, we do not want
11612 that in the GIMPLE IL. */
11613 if (TREE_OVERFLOW_P (*expr_p))
11614 *expr_p = drop_tree_overflow (*expr_p);
11615 ret = GS_ALL_DONE;
11616 break;
11618 case CONST_DECL:
11619 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11620 CONST_DECL node. Otherwise the decl is replaceable by its
11621 value. */
11622 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11623 if (fallback & fb_lvalue)
11624 ret = GS_ALL_DONE;
11625 else
11627 *expr_p = DECL_INITIAL (*expr_p);
11628 ret = GS_OK;
11630 break;
11632 case DECL_EXPR:
11633 ret = gimplify_decl_expr (expr_p, pre_p);
11634 break;
11636 case BIND_EXPR:
11637 ret = gimplify_bind_expr (expr_p, pre_p);
11638 break;
11640 case LOOP_EXPR:
11641 ret = gimplify_loop_expr (expr_p, pre_p);
11642 break;
11644 case SWITCH_EXPR:
11645 ret = gimplify_switch_expr (expr_p, pre_p);
11646 break;
11648 case EXIT_EXPR:
11649 ret = gimplify_exit_expr (expr_p);
11650 break;
11652 case GOTO_EXPR:
11653 /* If the target is not LABEL, then it is a computed jump
11654 and the target needs to be gimplified. */
11655 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11657 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11658 NULL, is_gimple_val, fb_rvalue);
11659 if (ret == GS_ERROR)
11660 break;
11662 gimplify_seq_add_stmt (pre_p,
11663 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11664 ret = GS_ALL_DONE;
11665 break;
11667 case PREDICT_EXPR:
11668 gimplify_seq_add_stmt (pre_p,
11669 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11670 PREDICT_EXPR_OUTCOME (*expr_p)));
11671 ret = GS_ALL_DONE;
11672 break;
11674 case LABEL_EXPR:
11675 ret = gimplify_label_expr (expr_p, pre_p);
11676 label = LABEL_EXPR_LABEL (*expr_p);
11677 gcc_assert (decl_function_context (label) == current_function_decl);
11679 /* If the label is used in a goto statement, or address of the label
11680 is taken, we need to unpoison all variables that were seen so far.
11681 Doing so would prevent us from reporting a false positives. */
11682 if (asan_poisoned_variables
11683 && asan_used_labels != NULL
11684 && asan_used_labels->contains (label))
11685 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11686 break;
11688 case CASE_LABEL_EXPR:
11689 ret = gimplify_case_label_expr (expr_p, pre_p);
11691 if (gimplify_ctxp->live_switch_vars)
11692 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11693 pre_p);
11694 break;
11696 case RETURN_EXPR:
11697 ret = gimplify_return_expr (*expr_p, pre_p);
11698 break;
11700 case CONSTRUCTOR:
11701 /* Don't reduce this in place; let gimplify_init_constructor work its
11702 magic. Buf if we're just elaborating this for side effects, just
11703 gimplify any element that has side-effects. */
11704 if (fallback == fb_none)
11706 unsigned HOST_WIDE_INT ix;
11707 tree val;
11708 tree temp = NULL_TREE;
11709 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11710 if (TREE_SIDE_EFFECTS (val))
11711 append_to_statement_list (val, &temp);
11713 *expr_p = temp;
11714 ret = temp ? GS_OK : GS_ALL_DONE;
11716 /* C99 code may assign to an array in a constructed
11717 structure or union, and this has undefined behavior only
11718 on execution, so create a temporary if an lvalue is
11719 required. */
11720 else if (fallback == fb_lvalue)
11722 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11723 mark_addressable (*expr_p);
11724 ret = GS_OK;
11726 else
11727 ret = GS_ALL_DONE;
11728 break;
11730 /* The following are special cases that are not handled by the
11731 original GIMPLE grammar. */
11733 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11734 eliminated. */
11735 case SAVE_EXPR:
11736 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11737 break;
11739 case BIT_FIELD_REF:
11740 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11741 post_p, is_gimple_lvalue, fb_either);
11742 recalculate_side_effects (*expr_p);
11743 break;
11745 case TARGET_MEM_REF:
11747 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11749 if (TMR_BASE (*expr_p))
11750 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11751 post_p, is_gimple_mem_ref_addr, fb_either);
11752 if (TMR_INDEX (*expr_p))
11753 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11754 post_p, is_gimple_val, fb_rvalue);
11755 if (TMR_INDEX2 (*expr_p))
11756 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11757 post_p, is_gimple_val, fb_rvalue);
11758 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11759 ret = MIN (r0, r1);
11761 break;
11763 case NON_LVALUE_EXPR:
11764 /* This should have been stripped above. */
11765 gcc_unreachable ();
11767 case ASM_EXPR:
11768 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11769 break;
11771 case TRY_FINALLY_EXPR:
11772 case TRY_CATCH_EXPR:
11774 gimple_seq eval, cleanup;
11775 gtry *try_;
11777 /* Calls to destructors are generated automatically in FINALLY/CATCH
11778 block. They should have location as UNKNOWN_LOCATION. However,
11779 gimplify_call_expr will reset these call stmts to input_location
11780 if it finds stmt's location is unknown. To prevent resetting for
11781 destructors, we set the input_location to unknown.
11782 Note that this only affects the destructor calls in FINALLY/CATCH
11783 block, and will automatically reset to its original value by the
11784 end of gimplify_expr. */
11785 input_location = UNKNOWN_LOCATION;
11786 eval = cleanup = NULL;
11787 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11788 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11789 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11790 if (gimple_seq_empty_p (cleanup))
11792 gimple_seq_add_seq (pre_p, eval);
11793 ret = GS_ALL_DONE;
11794 break;
11796 try_ = gimple_build_try (eval, cleanup,
11797 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11798 ? GIMPLE_TRY_FINALLY
11799 : GIMPLE_TRY_CATCH);
11800 if (EXPR_HAS_LOCATION (save_expr))
11801 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11802 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11803 gimple_set_location (try_, saved_location);
11804 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11805 gimple_try_set_catch_is_cleanup (try_,
11806 TRY_CATCH_IS_CLEANUP (*expr_p));
11807 gimplify_seq_add_stmt (pre_p, try_);
11808 ret = GS_ALL_DONE;
11809 break;
11812 case CLEANUP_POINT_EXPR:
11813 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11814 break;
11816 case TARGET_EXPR:
11817 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11818 break;
11820 case CATCH_EXPR:
11822 gimple *c;
11823 gimple_seq handler = NULL;
11824 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11825 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11826 gimplify_seq_add_stmt (pre_p, c);
11827 ret = GS_ALL_DONE;
11828 break;
11831 case EH_FILTER_EXPR:
11833 gimple *ehf;
11834 gimple_seq failure = NULL;
11836 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11837 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11838 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11839 gimplify_seq_add_stmt (pre_p, ehf);
11840 ret = GS_ALL_DONE;
11841 break;
11844 case OBJ_TYPE_REF:
11846 enum gimplify_status r0, r1;
11847 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11848 post_p, is_gimple_val, fb_rvalue);
11849 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11850 post_p, is_gimple_val, fb_rvalue);
11851 TREE_SIDE_EFFECTS (*expr_p) = 0;
11852 ret = MIN (r0, r1);
11854 break;
11856 case LABEL_DECL:
11857 /* We get here when taking the address of a label. We mark
11858 the label as "forced"; meaning it can never be removed and
11859 it is a potential target for any computed goto. */
11860 FORCED_LABEL (*expr_p) = 1;
11861 ret = GS_ALL_DONE;
11862 break;
11864 case STATEMENT_LIST:
11865 ret = gimplify_statement_list (expr_p, pre_p);
11866 break;
11868 case WITH_SIZE_EXPR:
11870 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11871 post_p == &internal_post ? NULL : post_p,
11872 gimple_test_f, fallback);
11873 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11874 is_gimple_val, fb_rvalue);
11875 ret = GS_ALL_DONE;
11877 break;
11879 case VAR_DECL:
11880 case PARM_DECL:
11881 ret = gimplify_var_or_parm_decl (expr_p);
11882 break;
11884 case RESULT_DECL:
11885 /* When within an OMP context, notice uses of variables. */
11886 if (gimplify_omp_ctxp)
11887 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11888 ret = GS_ALL_DONE;
11889 break;
11891 case DEBUG_EXPR_DECL:
11892 gcc_unreachable ();
11894 case DEBUG_BEGIN_STMT:
11895 gimplify_seq_add_stmt (pre_p,
11896 gimple_build_debug_begin_stmt
11897 (TREE_BLOCK (*expr_p),
11898 EXPR_LOCATION (*expr_p)));
11899 ret = GS_ALL_DONE;
11900 *expr_p = NULL;
11901 break;
11903 case SSA_NAME:
11904 /* Allow callbacks into the gimplifier during optimization. */
11905 ret = GS_ALL_DONE;
11906 break;
11908 case OMP_PARALLEL:
11909 gimplify_omp_parallel (expr_p, pre_p);
11910 ret = GS_ALL_DONE;
11911 break;
11913 case OMP_TASK:
11914 gimplify_omp_task (expr_p, pre_p);
11915 ret = GS_ALL_DONE;
11916 break;
11918 case OMP_FOR:
11919 case OMP_SIMD:
11920 case OMP_DISTRIBUTE:
11921 case OMP_TASKLOOP:
11922 case OACC_LOOP:
11923 ret = gimplify_omp_for (expr_p, pre_p);
11924 break;
11926 case OACC_CACHE:
11927 gimplify_oacc_cache (expr_p, pre_p);
11928 ret = GS_ALL_DONE;
11929 break;
11931 case OACC_DECLARE:
11932 gimplify_oacc_declare (expr_p, pre_p);
11933 ret = GS_ALL_DONE;
11934 break;
11936 case OACC_HOST_DATA:
11937 case OACC_DATA:
11938 case OACC_KERNELS:
11939 case OACC_PARALLEL:
11940 case OMP_SECTIONS:
11941 case OMP_SINGLE:
11942 case OMP_TARGET:
11943 case OMP_TARGET_DATA:
11944 case OMP_TEAMS:
11945 gimplify_omp_workshare (expr_p, pre_p);
11946 ret = GS_ALL_DONE;
11947 break;
11949 case OACC_ENTER_DATA:
11950 case OACC_EXIT_DATA:
11951 case OACC_UPDATE:
11952 case OMP_TARGET_UPDATE:
11953 case OMP_TARGET_ENTER_DATA:
11954 case OMP_TARGET_EXIT_DATA:
11955 gimplify_omp_target_update (expr_p, pre_p);
11956 ret = GS_ALL_DONE;
11957 break;
11959 case OMP_SECTION:
11960 case OMP_MASTER:
11961 case OMP_TASKGROUP:
11962 case OMP_ORDERED:
11963 case OMP_CRITICAL:
11965 gimple_seq body = NULL;
11966 gimple *g;
11968 gimplify_and_add (OMP_BODY (*expr_p), &body);
11969 switch (TREE_CODE (*expr_p))
11971 case OMP_SECTION:
11972 g = gimple_build_omp_section (body);
11973 break;
11974 case OMP_MASTER:
11975 g = gimple_build_omp_master (body);
11976 break;
11977 case OMP_TASKGROUP:
11979 gimple_seq cleanup = NULL;
11980 tree fn
11981 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11982 g = gimple_build_call (fn, 0);
11983 gimple_seq_add_stmt (&cleanup, g);
11984 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11985 body = NULL;
11986 gimple_seq_add_stmt (&body, g);
11987 g = gimple_build_omp_taskgroup (body);
11989 break;
11990 case OMP_ORDERED:
11991 g = gimplify_omp_ordered (*expr_p, body);
11992 break;
11993 case OMP_CRITICAL:
11994 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11995 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11996 gimplify_adjust_omp_clauses (pre_p, body,
11997 &OMP_CRITICAL_CLAUSES (*expr_p),
11998 OMP_CRITICAL);
11999 g = gimple_build_omp_critical (body,
12000 OMP_CRITICAL_NAME (*expr_p),
12001 OMP_CRITICAL_CLAUSES (*expr_p));
12002 break;
12003 default:
12004 gcc_unreachable ();
12006 gimplify_seq_add_stmt (pre_p, g);
12007 ret = GS_ALL_DONE;
12008 break;
12011 case OMP_ATOMIC:
12012 case OMP_ATOMIC_READ:
12013 case OMP_ATOMIC_CAPTURE_OLD:
12014 case OMP_ATOMIC_CAPTURE_NEW:
12015 ret = gimplify_omp_atomic (expr_p, pre_p);
12016 break;
12018 case TRANSACTION_EXPR:
12019 ret = gimplify_transaction (expr_p, pre_p);
12020 break;
12022 case TRUTH_AND_EXPR:
12023 case TRUTH_OR_EXPR:
12024 case TRUTH_XOR_EXPR:
12026 tree orig_type = TREE_TYPE (*expr_p);
12027 tree new_type, xop0, xop1;
12028 *expr_p = gimple_boolify (*expr_p);
12029 new_type = TREE_TYPE (*expr_p);
12030 if (!useless_type_conversion_p (orig_type, new_type))
12032 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12033 ret = GS_OK;
12034 break;
12037 /* Boolified binary truth expressions are semantically equivalent
12038 to bitwise binary expressions. Canonicalize them to the
12039 bitwise variant. */
12040 switch (TREE_CODE (*expr_p))
12042 case TRUTH_AND_EXPR:
12043 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12044 break;
12045 case TRUTH_OR_EXPR:
12046 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12047 break;
12048 case TRUTH_XOR_EXPR:
12049 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12050 break;
12051 default:
12052 break;
12054 /* Now make sure that operands have compatible type to
12055 expression's new_type. */
12056 xop0 = TREE_OPERAND (*expr_p, 0);
12057 xop1 = TREE_OPERAND (*expr_p, 1);
12058 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12059 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12060 new_type,
12061 xop0);
12062 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12063 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12064 new_type,
12065 xop1);
12066 /* Continue classified as tcc_binary. */
12067 goto expr_2;
12070 case VEC_COND_EXPR:
12072 enum gimplify_status r0, r1, r2;
12074 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12075 post_p, is_gimple_condexpr, fb_rvalue);
12076 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12077 post_p, is_gimple_val, fb_rvalue);
12078 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12079 post_p, is_gimple_val, fb_rvalue);
12081 ret = MIN (MIN (r0, r1), r2);
12082 recalculate_side_effects (*expr_p);
12084 break;
12086 case FMA_EXPR:
12087 case VEC_PERM_EXPR:
12088 /* Classified as tcc_expression. */
12089 goto expr_3;
12091 case BIT_INSERT_EXPR:
12092 /* Argument 3 is a constant. */
12093 goto expr_2;
12095 case POINTER_PLUS_EXPR:
12097 enum gimplify_status r0, r1;
12098 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12099 post_p, is_gimple_val, fb_rvalue);
12100 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12101 post_p, is_gimple_val, fb_rvalue);
12102 recalculate_side_effects (*expr_p);
12103 ret = MIN (r0, r1);
12104 break;
12107 default:
12108 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12110 case tcc_comparison:
12111 /* Handle comparison of objects of non scalar mode aggregates
12112 with a call to memcmp. It would be nice to only have to do
12113 this for variable-sized objects, but then we'd have to allow
12114 the same nest of reference nodes we allow for MODIFY_EXPR and
12115 that's too complex.
12117 Compare scalar mode aggregates as scalar mode values. Using
12118 memcmp for them would be very inefficient at best, and is
12119 plain wrong if bitfields are involved. */
12121 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12123 /* Vector comparisons need no boolification. */
12124 if (TREE_CODE (type) == VECTOR_TYPE)
12125 goto expr_2;
12126 else if (!AGGREGATE_TYPE_P (type))
12128 tree org_type = TREE_TYPE (*expr_p);
12129 *expr_p = gimple_boolify (*expr_p);
12130 if (!useless_type_conversion_p (org_type,
12131 TREE_TYPE (*expr_p)))
12133 *expr_p = fold_convert_loc (input_location,
12134 org_type, *expr_p);
12135 ret = GS_OK;
12137 else
12138 goto expr_2;
12140 else if (TYPE_MODE (type) != BLKmode)
12141 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12142 else
12143 ret = gimplify_variable_sized_compare (expr_p);
12145 break;
12148 /* If *EXPR_P does not need to be special-cased, handle it
12149 according to its class. */
12150 case tcc_unary:
12151 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12152 post_p, is_gimple_val, fb_rvalue);
12153 break;
12155 case tcc_binary:
12156 expr_2:
12158 enum gimplify_status r0, r1;
12160 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12161 post_p, is_gimple_val, fb_rvalue);
12162 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12163 post_p, is_gimple_val, fb_rvalue);
12165 ret = MIN (r0, r1);
12166 break;
12169 expr_3:
12171 enum gimplify_status r0, r1, r2;
12173 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12174 post_p, is_gimple_val, fb_rvalue);
12175 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12176 post_p, is_gimple_val, fb_rvalue);
12177 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12178 post_p, is_gimple_val, fb_rvalue);
12180 ret = MIN (MIN (r0, r1), r2);
12181 break;
12184 case tcc_declaration:
12185 case tcc_constant:
12186 ret = GS_ALL_DONE;
12187 goto dont_recalculate;
12189 default:
12190 gcc_unreachable ();
12193 recalculate_side_effects (*expr_p);
12195 dont_recalculate:
12196 break;
12199 gcc_assert (*expr_p || ret != GS_OK);
12201 while (ret == GS_OK);
12203 /* If we encountered an error_mark somewhere nested inside, either
12204 stub out the statement or propagate the error back out. */
12205 if (ret == GS_ERROR)
12207 if (is_statement)
12208 *expr_p = NULL;
12209 goto out;
12212 /* This was only valid as a return value from the langhook, which
12213 we handled. Make sure it doesn't escape from any other context. */
12214 gcc_assert (ret != GS_UNHANDLED);
12216 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12218 /* We aren't looking for a value, and we don't have a valid
12219 statement. If it doesn't have side-effects, throw it away.
12220 We can also get here with code such as "*&&L;", where L is
12221 a LABEL_DECL that is marked as FORCED_LABEL. */
12222 if (TREE_CODE (*expr_p) == LABEL_DECL
12223 || !TREE_SIDE_EFFECTS (*expr_p))
12224 *expr_p = NULL;
12225 else if (!TREE_THIS_VOLATILE (*expr_p))
12227 /* This is probably a _REF that contains something nested that
12228 has side effects. Recurse through the operands to find it. */
12229 enum tree_code code = TREE_CODE (*expr_p);
12231 switch (code)
12233 case COMPONENT_REF:
12234 case REALPART_EXPR:
12235 case IMAGPART_EXPR:
12236 case VIEW_CONVERT_EXPR:
12237 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12238 gimple_test_f, fallback);
12239 break;
12241 case ARRAY_REF:
12242 case ARRAY_RANGE_REF:
12243 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12244 gimple_test_f, fallback);
12245 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12246 gimple_test_f, fallback);
12247 break;
12249 default:
12250 /* Anything else with side-effects must be converted to
12251 a valid statement before we get here. */
12252 gcc_unreachable ();
12255 *expr_p = NULL;
12257 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12258 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12260 /* Historically, the compiler has treated a bare reference
12261 to a non-BLKmode volatile lvalue as forcing a load. */
12262 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12264 /* Normally, we do not want to create a temporary for a
12265 TREE_ADDRESSABLE type because such a type should not be
12266 copied by bitwise-assignment. However, we make an
12267 exception here, as all we are doing here is ensuring that
12268 we read the bytes that make up the type. We use
12269 create_tmp_var_raw because create_tmp_var will abort when
12270 given a TREE_ADDRESSABLE type. */
12271 tree tmp = create_tmp_var_raw (type, "vol");
12272 gimple_add_tmp_var (tmp);
12273 gimplify_assign (tmp, *expr_p, pre_p);
12274 *expr_p = NULL;
12276 else
12277 /* We can't do anything useful with a volatile reference to
12278 an incomplete type, so just throw it away. Likewise for
12279 a BLKmode type, since any implicit inner load should
12280 already have been turned into an explicit one by the
12281 gimplification process. */
12282 *expr_p = NULL;
12285 /* If we are gimplifying at the statement level, we're done. Tack
12286 everything together and return. */
12287 if (fallback == fb_none || is_statement)
12289 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12290 it out for GC to reclaim it. */
12291 *expr_p = NULL_TREE;
12293 if (!gimple_seq_empty_p (internal_pre)
12294 || !gimple_seq_empty_p (internal_post))
12296 gimplify_seq_add_seq (&internal_pre, internal_post);
12297 gimplify_seq_add_seq (pre_p, internal_pre);
12300 /* The result of gimplifying *EXPR_P is going to be the last few
12301 statements in *PRE_P and *POST_P. Add location information
12302 to all the statements that were added by the gimplification
12303 helpers. */
12304 if (!gimple_seq_empty_p (*pre_p))
12305 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12307 if (!gimple_seq_empty_p (*post_p))
12308 annotate_all_with_location_after (*post_p, post_last_gsi,
12309 input_location);
12311 goto out;
12314 #ifdef ENABLE_GIMPLE_CHECKING
12315 if (*expr_p)
12317 enum tree_code code = TREE_CODE (*expr_p);
12318 /* These expressions should already be in gimple IR form. */
12319 gcc_assert (code != MODIFY_EXPR
12320 && code != ASM_EXPR
12321 && code != BIND_EXPR
12322 && code != CATCH_EXPR
12323 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12324 && code != EH_FILTER_EXPR
12325 && code != GOTO_EXPR
12326 && code != LABEL_EXPR
12327 && code != LOOP_EXPR
12328 && code != SWITCH_EXPR
12329 && code != TRY_FINALLY_EXPR
12330 && code != OACC_PARALLEL
12331 && code != OACC_KERNELS
12332 && code != OACC_DATA
12333 && code != OACC_HOST_DATA
12334 && code != OACC_DECLARE
12335 && code != OACC_UPDATE
12336 && code != OACC_ENTER_DATA
12337 && code != OACC_EXIT_DATA
12338 && code != OACC_CACHE
12339 && code != OMP_CRITICAL
12340 && code != OMP_FOR
12341 && code != OACC_LOOP
12342 && code != OMP_MASTER
12343 && code != OMP_TASKGROUP
12344 && code != OMP_ORDERED
12345 && code != OMP_PARALLEL
12346 && code != OMP_SECTIONS
12347 && code != OMP_SECTION
12348 && code != OMP_SINGLE);
12350 #endif
12352 /* Otherwise we're gimplifying a subexpression, so the resulting
12353 value is interesting. If it's a valid operand that matches
12354 GIMPLE_TEST_F, we're done. Unless we are handling some
12355 post-effects internally; if that's the case, we need to copy into
12356 a temporary before adding the post-effects to POST_P. */
12357 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12358 goto out;
12360 /* Otherwise, we need to create a new temporary for the gimplified
12361 expression. */
12363 /* We can't return an lvalue if we have an internal postqueue. The
12364 object the lvalue refers to would (probably) be modified by the
12365 postqueue; we need to copy the value out first, which means an
12366 rvalue. */
12367 if ((fallback & fb_lvalue)
12368 && gimple_seq_empty_p (internal_post)
12369 && is_gimple_addressable (*expr_p))
12371 /* An lvalue will do. Take the address of the expression, store it
12372 in a temporary, and replace the expression with an INDIRECT_REF of
12373 that temporary. */
12374 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12375 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12376 *expr_p = build_simple_mem_ref (tmp);
12378 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12380 /* An rvalue will do. Assign the gimplified expression into a
12381 new temporary TMP and replace the original expression with
12382 TMP. First, make sure that the expression has a type so that
12383 it can be assigned into a temporary. */
12384 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12385 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12387 else
12389 #ifdef ENABLE_GIMPLE_CHECKING
12390 if (!(fallback & fb_mayfail))
12392 fprintf (stderr, "gimplification failed:\n");
12393 print_generic_expr (stderr, *expr_p);
12394 debug_tree (*expr_p);
12395 internal_error ("gimplification failed");
12397 #endif
12398 gcc_assert (fallback & fb_mayfail);
12400 /* If this is an asm statement, and the user asked for the
12401 impossible, don't die. Fail and let gimplify_asm_expr
12402 issue an error. */
12403 ret = GS_ERROR;
12404 goto out;
12407 /* Make sure the temporary matches our predicate. */
12408 gcc_assert ((*gimple_test_f) (*expr_p));
12410 if (!gimple_seq_empty_p (internal_post))
12412 annotate_all_with_location (internal_post, input_location);
12413 gimplify_seq_add_seq (pre_p, internal_post);
12416 out:
12417 input_location = saved_location;
12418 return ret;
12421 /* Like gimplify_expr but make sure the gimplified result is not itself
12422 a SSA name (but a decl if it were). Temporaries required by
12423 evaluating *EXPR_P may be still SSA names. */
12425 static enum gimplify_status
12426 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12427 bool (*gimple_test_f) (tree), fallback_t fallback,
12428 bool allow_ssa)
12430 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12431 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12432 gimple_test_f, fallback);
12433 if (! allow_ssa
12434 && TREE_CODE (*expr_p) == SSA_NAME)
12436 tree name = *expr_p;
12437 if (was_ssa_name_p)
12438 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12439 else
12441 /* Avoid the extra copy if possible. */
12442 *expr_p = create_tmp_reg (TREE_TYPE (name));
12443 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12444 release_ssa_name (name);
12447 return ret;
12450 /* Look through TYPE for variable-sized objects and gimplify each such
12451 size that we find. Add to LIST_P any statements generated. */
12453 void
12454 gimplify_type_sizes (tree type, gimple_seq *list_p)
12456 tree field, t;
12458 if (type == NULL || type == error_mark_node)
12459 return;
12461 /* We first do the main variant, then copy into any other variants. */
12462 type = TYPE_MAIN_VARIANT (type);
12464 /* Avoid infinite recursion. */
12465 if (TYPE_SIZES_GIMPLIFIED (type))
12466 return;
12468 TYPE_SIZES_GIMPLIFIED (type) = 1;
12470 switch (TREE_CODE (type))
12472 case INTEGER_TYPE:
12473 case ENUMERAL_TYPE:
12474 case BOOLEAN_TYPE:
12475 case REAL_TYPE:
12476 case FIXED_POINT_TYPE:
12477 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12478 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12480 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12482 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12483 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12485 break;
12487 case ARRAY_TYPE:
12488 /* These types may not have declarations, so handle them here. */
12489 gimplify_type_sizes (TREE_TYPE (type), list_p);
12490 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12491 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12492 with assigned stack slots, for -O1+ -g they should be tracked
12493 by VTA. */
12494 if (!(TYPE_NAME (type)
12495 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12496 && DECL_IGNORED_P (TYPE_NAME (type)))
12497 && TYPE_DOMAIN (type)
12498 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12500 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12501 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12502 DECL_IGNORED_P (t) = 0;
12503 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12504 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12505 DECL_IGNORED_P (t) = 0;
12507 break;
12509 case RECORD_TYPE:
12510 case UNION_TYPE:
12511 case QUAL_UNION_TYPE:
12512 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12513 if (TREE_CODE (field) == FIELD_DECL)
12515 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12516 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12517 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12518 gimplify_type_sizes (TREE_TYPE (field), list_p);
12520 break;
12522 case POINTER_TYPE:
12523 case REFERENCE_TYPE:
12524 /* We used to recurse on the pointed-to type here, which turned out to
12525 be incorrect because its definition might refer to variables not
12526 yet initialized at this point if a forward declaration is involved.
12528 It was actually useful for anonymous pointed-to types to ensure
12529 that the sizes evaluation dominates every possible later use of the
12530 values. Restricting to such types here would be safe since there
12531 is no possible forward declaration around, but would introduce an
12532 undesirable middle-end semantic to anonymity. We then defer to
12533 front-ends the responsibility of ensuring that the sizes are
12534 evaluated both early and late enough, e.g. by attaching artificial
12535 type declarations to the tree. */
12536 break;
12538 default:
12539 break;
12542 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12543 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12545 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12547 TYPE_SIZE (t) = TYPE_SIZE (type);
12548 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12549 TYPE_SIZES_GIMPLIFIED (t) = 1;
12553 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12554 a size or position, has had all of its SAVE_EXPRs evaluated.
12555 We add any required statements to *STMT_P. */
12557 void
12558 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12560 tree expr = *expr_p;
12562 /* We don't do anything if the value isn't there, is constant, or contains
12563 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12564 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12565 will want to replace it with a new variable, but that will cause problems
12566 if this type is from outside the function. It's OK to have that here. */
12567 if (expr == NULL_TREE
12568 || is_gimple_constant (expr)
12569 || TREE_CODE (expr) == VAR_DECL
12570 || CONTAINS_PLACEHOLDER_P (expr))
12571 return;
12573 *expr_p = unshare_expr (expr);
12575 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12576 if the def vanishes. */
12577 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12579 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12580 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12581 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12582 if (is_gimple_constant (*expr_p))
12583 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12586 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12587 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12588 is true, also gimplify the parameters. */
12590 gbind *
12591 gimplify_body (tree fndecl, bool do_parms)
12593 location_t saved_location = input_location;
12594 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12595 gimple *outer_stmt;
12596 gbind *outer_bind;
12597 struct cgraph_node *cgn;
12599 timevar_push (TV_TREE_GIMPLIFY);
12601 init_tree_ssa (cfun);
12603 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12604 gimplification. */
12605 default_rtl_profile ();
12607 gcc_assert (gimplify_ctxp == NULL);
12608 push_gimplify_context (true);
12610 if (flag_openacc || flag_openmp)
12612 gcc_assert (gimplify_omp_ctxp == NULL);
12613 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12614 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12617 /* Unshare most shared trees in the body and in that of any nested functions.
12618 It would seem we don't have to do this for nested functions because
12619 they are supposed to be output and then the outer function gimplified
12620 first, but the g++ front end doesn't always do it that way. */
12621 unshare_body (fndecl);
12622 unvisit_body (fndecl);
12624 cgn = cgraph_node::get (fndecl);
12625 if (cgn && cgn->origin)
12626 nonlocal_vlas = new hash_set<tree>;
12628 /* Make sure input_location isn't set to something weird. */
12629 input_location = DECL_SOURCE_LOCATION (fndecl);
12631 /* Resolve callee-copies. This has to be done before processing
12632 the body so that DECL_VALUE_EXPR gets processed correctly. */
12633 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12635 /* Gimplify the function's body. */
12636 seq = NULL;
12637 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12638 outer_stmt = gimple_seq_first_stmt (seq);
12639 if (!outer_stmt)
12641 outer_stmt = gimple_build_nop ();
12642 gimplify_seq_add_stmt (&seq, outer_stmt);
12645 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12646 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12647 if (gimple_code (outer_stmt) == GIMPLE_BIND
12648 && gimple_seq_first (seq) == gimple_seq_last (seq))
12649 outer_bind = as_a <gbind *> (outer_stmt);
12650 else
12651 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12653 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12655 /* If we had callee-copies statements, insert them at the beginning
12656 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12657 if (!gimple_seq_empty_p (parm_stmts))
12659 tree parm;
12661 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12662 if (parm_cleanup)
12664 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12665 GIMPLE_TRY_FINALLY);
12666 parm_stmts = NULL;
12667 gimple_seq_add_stmt (&parm_stmts, g);
12669 gimple_bind_set_body (outer_bind, parm_stmts);
12671 for (parm = DECL_ARGUMENTS (current_function_decl);
12672 parm; parm = DECL_CHAIN (parm))
12673 if (DECL_HAS_VALUE_EXPR_P (parm))
12675 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12676 DECL_IGNORED_P (parm) = 0;
12680 if (nonlocal_vlas)
12682 if (nonlocal_vla_vars)
12684 /* tree-nested.c may later on call declare_vars (..., true);
12685 which relies on BLOCK_VARS chain to be the tail of the
12686 gimple_bind_vars chain. Ensure we don't violate that
12687 assumption. */
12688 if (gimple_bind_block (outer_bind)
12689 == DECL_INITIAL (current_function_decl))
12690 declare_vars (nonlocal_vla_vars, outer_bind, true);
12691 else
12692 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12693 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12694 nonlocal_vla_vars);
12695 nonlocal_vla_vars = NULL_TREE;
12697 delete nonlocal_vlas;
12698 nonlocal_vlas = NULL;
12701 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12702 && gimplify_omp_ctxp)
12704 delete_omp_context (gimplify_omp_ctxp);
12705 gimplify_omp_ctxp = NULL;
12708 pop_gimplify_context (outer_bind);
12709 gcc_assert (gimplify_ctxp == NULL);
12711 if (flag_checking && !seen_error ())
12712 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12714 timevar_pop (TV_TREE_GIMPLIFY);
12715 input_location = saved_location;
12717 return outer_bind;
12720 typedef char *char_p; /* For DEF_VEC_P. */
12722 /* Return whether we should exclude FNDECL from instrumentation. */
12724 static bool
12725 flag_instrument_functions_exclude_p (tree fndecl)
12727 vec<char_p> *v;
12729 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12730 if (v && v->length () > 0)
12732 const char *name;
12733 int i;
12734 char *s;
12736 name = lang_hooks.decl_printable_name (fndecl, 0);
12737 FOR_EACH_VEC_ELT (*v, i, s)
12738 if (strstr (name, s) != NULL)
12739 return true;
12742 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12743 if (v && v->length () > 0)
12745 const char *name;
12746 int i;
12747 char *s;
12749 name = DECL_SOURCE_FILE (fndecl);
12750 FOR_EACH_VEC_ELT (*v, i, s)
12751 if (strstr (name, s) != NULL)
12752 return true;
12755 return false;
12758 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12759 node for the function we want to gimplify.
12761 Return the sequence of GIMPLE statements corresponding to the body
12762 of FNDECL. */
12764 void
12765 gimplify_function_tree (tree fndecl)
12767 tree parm, ret;
12768 gimple_seq seq;
12769 gbind *bind;
12771 gcc_assert (!gimple_body (fndecl));
12773 if (DECL_STRUCT_FUNCTION (fndecl))
12774 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12775 else
12776 push_struct_function (fndecl);
12778 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12779 if necessary. */
12780 cfun->curr_properties |= PROP_gimple_lva;
12782 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12784 /* Preliminarily mark non-addressed complex variables as eligible
12785 for promotion to gimple registers. We'll transform their uses
12786 as we find them. */
12787 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12788 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12789 && !TREE_THIS_VOLATILE (parm)
12790 && !needs_to_live_in_memory (parm))
12791 DECL_GIMPLE_REG_P (parm) = 1;
12794 ret = DECL_RESULT (fndecl);
12795 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12796 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12797 && !needs_to_live_in_memory (ret))
12798 DECL_GIMPLE_REG_P (ret) = 1;
12800 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12801 asan_poisoned_variables = new hash_set<tree> ();
12802 bind = gimplify_body (fndecl, true);
12803 if (asan_poisoned_variables)
12805 delete asan_poisoned_variables;
12806 asan_poisoned_variables = NULL;
12809 /* The tree body of the function is no longer needed, replace it
12810 with the new GIMPLE body. */
12811 seq = NULL;
12812 gimple_seq_add_stmt (&seq, bind);
12813 gimple_set_body (fndecl, seq);
12815 /* If we're instrumenting function entry/exit, then prepend the call to
12816 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12817 catch the exit hook. */
12818 /* ??? Add some way to ignore exceptions for this TFE. */
12819 if (flag_instrument_function_entry_exit
12820 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12821 /* Do not instrument extern inline functions. */
12822 && !(DECL_DECLARED_INLINE_P (fndecl)
12823 && DECL_EXTERNAL (fndecl)
12824 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12825 && !flag_instrument_functions_exclude_p (fndecl))
12827 tree x;
12828 gbind *new_bind;
12829 gimple *tf;
12830 gimple_seq cleanup = NULL, body = NULL;
12831 tree tmp_var;
12832 gcall *call;
12834 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12835 call = gimple_build_call (x, 1, integer_zero_node);
12836 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12837 gimple_call_set_lhs (call, tmp_var);
12838 gimplify_seq_add_stmt (&cleanup, call);
12839 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12840 call = gimple_build_call (x, 2,
12841 build_fold_addr_expr (current_function_decl),
12842 tmp_var);
12843 gimplify_seq_add_stmt (&cleanup, call);
12844 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12846 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12847 call = gimple_build_call (x, 1, integer_zero_node);
12848 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12849 gimple_call_set_lhs (call, tmp_var);
12850 gimplify_seq_add_stmt (&body, call);
12851 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12852 call = gimple_build_call (x, 2,
12853 build_fold_addr_expr (current_function_decl),
12854 tmp_var);
12855 gimplify_seq_add_stmt (&body, call);
12856 gimplify_seq_add_stmt (&body, tf);
12857 new_bind = gimple_build_bind (NULL, body, NULL);
12859 /* Replace the current function body with the body
12860 wrapped in the try/finally TF. */
12861 seq = NULL;
12862 gimple_seq_add_stmt (&seq, new_bind);
12863 gimple_set_body (fndecl, seq);
12864 bind = new_bind;
12867 if (sanitize_flags_p (SANITIZE_THREAD))
12869 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12870 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12871 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12872 /* Replace the current function body with the body
12873 wrapped in the try/finally TF. */
12874 seq = NULL;
12875 gimple_seq_add_stmt (&seq, new_bind);
12876 gimple_set_body (fndecl, seq);
12879 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12880 cfun->curr_properties |= PROP_gimple_any;
12882 pop_cfun ();
12884 dump_function (TDI_gimple, fndecl);
12887 /* Return a dummy expression of type TYPE in order to keep going after an
12888 error. */
12890 static tree
12891 dummy_object (tree type)
12893 tree t = build_int_cst (build_pointer_type (type), 0);
12894 return build2 (MEM_REF, type, t, t);
12897 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12898 builtin function, but a very special sort of operator. */
12900 enum gimplify_status
12901 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12902 gimple_seq *post_p ATTRIBUTE_UNUSED)
12904 tree promoted_type, have_va_type;
12905 tree valist = TREE_OPERAND (*expr_p, 0);
12906 tree type = TREE_TYPE (*expr_p);
12907 tree t, tag, aptag;
12908 location_t loc = EXPR_LOCATION (*expr_p);
12910 /* Verify that valist is of the proper type. */
12911 have_va_type = TREE_TYPE (valist);
12912 if (have_va_type == error_mark_node)
12913 return GS_ERROR;
12914 have_va_type = targetm.canonical_va_list_type (have_va_type);
12915 if (have_va_type == NULL_TREE
12916 && POINTER_TYPE_P (TREE_TYPE (valist)))
12917 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12918 have_va_type
12919 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12920 gcc_assert (have_va_type != NULL_TREE);
12922 /* Generate a diagnostic for requesting data of a type that cannot
12923 be passed through `...' due to type promotion at the call site. */
12924 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12925 != type)
12927 static bool gave_help;
12928 bool warned;
12929 /* Use the expansion point to handle cases such as passing bool (defined
12930 in a system header) through `...'. */
12931 source_location xloc
12932 = expansion_point_location_if_in_system_header (loc);
12934 /* Unfortunately, this is merely undefined, rather than a constraint
12935 violation, so we cannot make this an error. If this call is never
12936 executed, the program is still strictly conforming. */
12937 warned = warning_at (xloc, 0,
12938 "%qT is promoted to %qT when passed through %<...%>",
12939 type, promoted_type);
12940 if (!gave_help && warned)
12942 gave_help = true;
12943 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12944 promoted_type, type);
12947 /* We can, however, treat "undefined" any way we please.
12948 Call abort to encourage the user to fix the program. */
12949 if (warned)
12950 inform (xloc, "if this code is reached, the program will abort");
12951 /* Before the abort, allow the evaluation of the va_list
12952 expression to exit or longjmp. */
12953 gimplify_and_add (valist, pre_p);
12954 t = build_call_expr_loc (loc,
12955 builtin_decl_implicit (BUILT_IN_TRAP), 0);
12956 gimplify_and_add (t, pre_p);
12958 /* This is dead code, but go ahead and finish so that the
12959 mode of the result comes out right. */
12960 *expr_p = dummy_object (type);
12961 return GS_ALL_DONE;
12964 tag = build_int_cst (build_pointer_type (type), 0);
12965 aptag = build_int_cst (TREE_TYPE (valist), 0);
12967 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12968 valist, tag, aptag);
12970 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12971 needs to be expanded. */
12972 cfun->curr_properties &= ~PROP_gimple_lva;
12974 return GS_OK;
12977 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12979 DST/SRC are the destination and source respectively. You can pass
12980 ungimplified trees in DST or SRC, in which case they will be
12981 converted to a gimple operand if necessary.
12983 This function returns the newly created GIMPLE_ASSIGN tuple. */
12985 gimple *
12986 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12988 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12989 gimplify_and_add (t, seq_p);
12990 ggc_free (t);
12991 return gimple_seq_last_stmt (*seq_p);
12994 inline hashval_t
12995 gimplify_hasher::hash (const elt_t *p)
12997 tree t = p->val;
12998 return iterative_hash_expr (t, 0);
13001 inline bool
13002 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13004 tree t1 = p1->val;
13005 tree t2 = p2->val;
13006 enum tree_code code = TREE_CODE (t1);
13008 if (TREE_CODE (t2) != code
13009 || TREE_TYPE (t1) != TREE_TYPE (t2))
13010 return false;
13012 if (!operand_equal_p (t1, t2, 0))
13013 return false;
13015 /* Only allow them to compare equal if they also hash equal; otherwise
13016 results are nondeterminate, and we fail bootstrap comparison. */
13017 gcc_checking_assert (hash (p1) == hash (p2));
13019 return true;