Daily bump.
[official-gcc.git] / gcc / gimplify.c
blobe87eeb029c5711b7a013817bfa0f39372e1c0cec
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 tree unit_size = DECL_SIZE_UNIT (decl);
1172 tree base = build_fold_addr_expr (decl);
1174 /* Do not poison variables that have size equal to zero. */
1175 if (zerop (unit_size))
1176 return;
1178 /* It's necessary to have all stack variables aligned to ASAN granularity
1179 bytes. */
1180 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1181 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1183 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1185 gimple *g
1186 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1187 build_int_cst (integer_type_node, flags),
1188 base, unit_size);
1190 if (before)
1191 gsi_insert_before (it, g, GSI_NEW_STMT);
1192 else
1193 gsi_insert_after (it, g, GSI_NEW_STMT);
1196 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1197 either poisons or unpoisons a DECL. Created statement is appended
1198 to SEQ_P gimple sequence. */
1200 static void
1201 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1203 gimple_stmt_iterator it = gsi_last (*seq_p);
1204 bool before = false;
1206 if (gsi_end_p (it))
1207 before = true;
1209 asan_poison_variable (decl, poison, &it, before);
1212 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1214 static int
1215 sort_by_decl_uid (const void *a, const void *b)
1217 const tree *t1 = (const tree *)a;
1218 const tree *t2 = (const tree *)b;
1220 int uid1 = DECL_UID (*t1);
1221 int uid2 = DECL_UID (*t2);
1223 if (uid1 < uid2)
1224 return -1;
1225 else if (uid1 > uid2)
1226 return 1;
1227 else
1228 return 0;
1231 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1232 depending on POISON flag. Created statement is appended
1233 to SEQ_P gimple sequence. */
1235 static void
1236 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1238 unsigned c = variables->elements ();
1239 if (c == 0)
1240 return;
1242 auto_vec<tree> sorted_variables (c);
1244 for (hash_set<tree>::iterator it = variables->begin ();
1245 it != variables->end (); ++it)
1246 sorted_variables.safe_push (*it);
1248 sorted_variables.qsort (sort_by_decl_uid);
1250 unsigned i;
1251 tree var;
1252 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1254 asan_poison_variable (var, poison, seq_p);
1256 /* Add use_after_scope_memory attribute for the variable in order
1257 to prevent re-written into SSA. */
1258 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1259 DECL_ATTRIBUTES (var)))
1260 DECL_ATTRIBUTES (var)
1261 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1262 integer_one_node,
1263 DECL_ATTRIBUTES (var));
1267 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1269 static enum gimplify_status
1270 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1272 tree bind_expr = *expr_p;
1273 bool old_keep_stack = gimplify_ctxp->keep_stack;
1274 bool old_save_stack = gimplify_ctxp->save_stack;
1275 tree t;
1276 gbind *bind_stmt;
1277 gimple_seq body, cleanup;
1278 gcall *stack_save;
1279 location_t start_locus = 0, end_locus = 0;
1280 tree ret_clauses = NULL;
1282 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1284 /* Mark variables seen in this bind expr. */
1285 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1287 if (VAR_P (t))
1289 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1291 /* Mark variable as local. */
1292 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1293 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1294 || splay_tree_lookup (ctx->variables,
1295 (splay_tree_key) t) == NULL))
1297 if (ctx->region_type == ORT_SIMD
1298 && TREE_ADDRESSABLE (t)
1299 && !TREE_STATIC (t))
1300 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1301 else
1302 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1305 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1307 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1308 cfun->has_local_explicit_reg_vars = true;
1311 /* Preliminarily mark non-addressed complex variables as eligible
1312 for promotion to gimple registers. We'll transform their uses
1313 as we find them. */
1314 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1315 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1316 && !TREE_THIS_VOLATILE (t)
1317 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1318 && !needs_to_live_in_memory (t))
1319 DECL_GIMPLE_REG_P (t) = 1;
1322 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1323 BIND_EXPR_BLOCK (bind_expr));
1324 gimple_push_bind_expr (bind_stmt);
1326 gimplify_ctxp->keep_stack = false;
1327 gimplify_ctxp->save_stack = false;
1329 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1330 body = NULL;
1331 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1332 gimple_bind_set_body (bind_stmt, body);
1334 /* Source location wise, the cleanup code (stack_restore and clobbers)
1335 belongs to the end of the block, so propagate what we have. The
1336 stack_save operation belongs to the beginning of block, which we can
1337 infer from the bind_expr directly if the block has no explicit
1338 assignment. */
1339 if (BIND_EXPR_BLOCK (bind_expr))
1341 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1342 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1344 if (start_locus == 0)
1345 start_locus = EXPR_LOCATION (bind_expr);
1347 cleanup = NULL;
1348 stack_save = NULL;
1350 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1351 the stack space allocated to the VLAs. */
1352 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1354 gcall *stack_restore;
1356 /* Save stack on entry and restore it on exit. Add a try_finally
1357 block to achieve this. */
1358 build_stack_save_restore (&stack_save, &stack_restore);
1360 gimple_set_location (stack_save, start_locus);
1361 gimple_set_location (stack_restore, end_locus);
1363 gimplify_seq_add_stmt (&cleanup, stack_restore);
1366 /* Add clobbers for all variables that go out of scope. */
1367 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1369 if (VAR_P (t)
1370 && !is_global_var (t)
1371 && DECL_CONTEXT (t) == current_function_decl)
1373 if (!DECL_HARD_REGISTER (t)
1374 && !TREE_THIS_VOLATILE (t)
1375 && !DECL_HAS_VALUE_EXPR_P (t)
1376 /* Only care for variables that have to be in memory. Others
1377 will be rewritten into SSA names, hence moved to the
1378 top-level. */
1379 && !is_gimple_reg (t)
1380 && flag_stack_reuse != SR_NONE)
1382 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1383 gimple *clobber_stmt;
1384 TREE_THIS_VOLATILE (clobber) = 1;
1385 clobber_stmt = gimple_build_assign (t, clobber);
1386 gimple_set_location (clobber_stmt, end_locus);
1387 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1390 if (flag_openacc && oacc_declare_returns != NULL)
1392 tree *c = oacc_declare_returns->get (t);
1393 if (c != NULL)
1395 if (ret_clauses)
1396 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1398 ret_clauses = *c;
1400 oacc_declare_returns->remove (t);
1402 if (oacc_declare_returns->elements () == 0)
1404 delete oacc_declare_returns;
1405 oacc_declare_returns = NULL;
1411 if (asan_poisoned_variables != NULL
1412 && asan_poisoned_variables->contains (t))
1414 asan_poisoned_variables->remove (t);
1415 asan_poison_variable (t, true, &cleanup);
1418 if (gimplify_ctxp->live_switch_vars != NULL
1419 && gimplify_ctxp->live_switch_vars->contains (t))
1420 gimplify_ctxp->live_switch_vars->remove (t);
1423 if (ret_clauses)
1425 gomp_target *stmt;
1426 gimple_stmt_iterator si = gsi_start (cleanup);
1428 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1429 ret_clauses);
1430 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1433 if (cleanup)
1435 gtry *gs;
1436 gimple_seq new_body;
1438 new_body = NULL;
1439 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1440 GIMPLE_TRY_FINALLY);
1442 if (stack_save)
1443 gimplify_seq_add_stmt (&new_body, stack_save);
1444 gimplify_seq_add_stmt (&new_body, gs);
1445 gimple_bind_set_body (bind_stmt, new_body);
1448 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1449 if (!gimplify_ctxp->keep_stack)
1450 gimplify_ctxp->keep_stack = old_keep_stack;
1451 gimplify_ctxp->save_stack = old_save_stack;
1453 gimple_pop_bind_expr ();
1455 gimplify_seq_add_stmt (pre_p, bind_stmt);
1457 if (temp)
1459 *expr_p = temp;
1460 return GS_OK;
1463 *expr_p = NULL_TREE;
1464 return GS_ALL_DONE;
1467 /* Maybe add early return predict statement to PRE_P sequence. */
1469 static void
1470 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1472 /* If we are not in a conditional context, add PREDICT statement. */
1473 if (gimple_conditional_context ())
1475 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1476 NOT_TAKEN);
1477 gimplify_seq_add_stmt (pre_p, predict);
1481 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1482 GIMPLE value, it is assigned to a new temporary and the statement is
1483 re-written to return the temporary.
1485 PRE_P points to the sequence where side effects that must happen before
1486 STMT should be stored. */
1488 static enum gimplify_status
1489 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1491 greturn *ret;
1492 tree ret_expr = TREE_OPERAND (stmt, 0);
1493 tree result_decl, result;
1495 if (ret_expr == error_mark_node)
1496 return GS_ERROR;
1498 if (!ret_expr
1499 || TREE_CODE (ret_expr) == RESULT_DECL)
1501 maybe_add_early_return_predict_stmt (pre_p);
1502 greturn *ret = gimple_build_return (ret_expr);
1503 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1504 gimplify_seq_add_stmt (pre_p, ret);
1505 return GS_ALL_DONE;
1508 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1509 result_decl = NULL_TREE;
1510 else
1512 result_decl = TREE_OPERAND (ret_expr, 0);
1514 /* See through a return by reference. */
1515 if (TREE_CODE (result_decl) == INDIRECT_REF)
1516 result_decl = TREE_OPERAND (result_decl, 0);
1518 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1519 || TREE_CODE (ret_expr) == INIT_EXPR)
1520 && TREE_CODE (result_decl) == RESULT_DECL);
1523 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1524 Recall that aggregate_value_p is FALSE for any aggregate type that is
1525 returned in registers. If we're returning values in registers, then
1526 we don't want to extend the lifetime of the RESULT_DECL, particularly
1527 across another call. In addition, for those aggregates for which
1528 hard_function_value generates a PARALLEL, we'll die during normal
1529 expansion of structure assignments; there's special code in expand_return
1530 to handle this case that does not exist in expand_expr. */
1531 if (!result_decl)
1532 result = NULL_TREE;
1533 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1535 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1537 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1538 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1539 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1540 should be effectively allocated by the caller, i.e. all calls to
1541 this function must be subject to the Return Slot Optimization. */
1542 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1543 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1545 result = result_decl;
1547 else if (gimplify_ctxp->return_temp)
1548 result = gimplify_ctxp->return_temp;
1549 else
1551 result = create_tmp_reg (TREE_TYPE (result_decl));
1553 /* ??? With complex control flow (usually involving abnormal edges),
1554 we can wind up warning about an uninitialized value for this. Due
1555 to how this variable is constructed and initialized, this is never
1556 true. Give up and never warn. */
1557 TREE_NO_WARNING (result) = 1;
1559 gimplify_ctxp->return_temp = result;
1562 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1563 Then gimplify the whole thing. */
1564 if (result != result_decl)
1565 TREE_OPERAND (ret_expr, 0) = result;
1567 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1569 maybe_add_early_return_predict_stmt (pre_p);
1570 ret = gimple_build_return (result);
1571 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1572 gimplify_seq_add_stmt (pre_p, ret);
1574 return GS_ALL_DONE;
1577 /* Gimplify a variable-length array DECL. */
1579 static void
1580 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1582 /* This is a variable-sized decl. Simplify its size and mark it
1583 for deferred expansion. */
1584 tree t, addr, ptr_type;
1586 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1587 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1589 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1590 if (DECL_HAS_VALUE_EXPR_P (decl))
1591 return;
1593 /* All occurrences of this decl in final gimplified code will be
1594 replaced by indirection. Setting DECL_VALUE_EXPR does two
1595 things: First, it lets the rest of the gimplifier know what
1596 replacement to use. Second, it lets the debug info know
1597 where to find the value. */
1598 ptr_type = build_pointer_type (TREE_TYPE (decl));
1599 addr = create_tmp_var (ptr_type, get_name (decl));
1600 DECL_IGNORED_P (addr) = 0;
1601 t = build_fold_indirect_ref (addr);
1602 TREE_THIS_NOTRAP (t) = 1;
1603 SET_DECL_VALUE_EXPR (decl, t);
1604 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1606 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1607 max_int_size_in_bytes (TREE_TYPE (decl)));
1608 /* The call has been built for a variable-sized object. */
1609 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1610 t = fold_convert (ptr_type, t);
1611 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1613 gimplify_and_add (t, seq_p);
1616 /* A helper function to be called via walk_tree. Mark all labels under *TP
1617 as being forced. To be called for DECL_INITIAL of static variables. */
1619 static tree
1620 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1622 if (TYPE_P (*tp))
1623 *walk_subtrees = 0;
1624 if (TREE_CODE (*tp) == LABEL_DECL)
1626 FORCED_LABEL (*tp) = 1;
1627 cfun->has_forced_label_in_static = 1;
1630 return NULL_TREE;
1633 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1634 and initialization explicit. */
1636 static enum gimplify_status
1637 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1639 tree stmt = *stmt_p;
1640 tree decl = DECL_EXPR_DECL (stmt);
1642 *stmt_p = NULL_TREE;
1644 if (TREE_TYPE (decl) == error_mark_node)
1645 return GS_ERROR;
1647 if ((TREE_CODE (decl) == TYPE_DECL
1648 || VAR_P (decl))
1649 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1651 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1652 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1653 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1656 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1657 in case its size expressions contain problematic nodes like CALL_EXPR. */
1658 if (TREE_CODE (decl) == TYPE_DECL
1659 && DECL_ORIGINAL_TYPE (decl)
1660 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1662 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1663 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1664 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1667 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1669 tree init = DECL_INITIAL (decl);
1670 bool is_vla = false;
1672 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1673 || (!TREE_STATIC (decl)
1674 && flag_stack_check == GENERIC_STACK_CHECK
1675 && compare_tree_int (DECL_SIZE_UNIT (decl),
1676 STACK_CHECK_MAX_VAR_SIZE) > 0))
1678 gimplify_vla_decl (decl, seq_p);
1679 is_vla = true;
1682 if (asan_poisoned_variables
1683 && !is_vla
1684 && TREE_ADDRESSABLE (decl)
1685 && !TREE_STATIC (decl)
1686 && !DECL_HAS_VALUE_EXPR_P (decl)
1687 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1688 && dbg_cnt (asan_use_after_scope)
1689 && !gimplify_omp_ctxp)
1691 asan_poisoned_variables->add (decl);
1692 asan_poison_variable (decl, false, seq_p);
1693 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1694 gimplify_ctxp->live_switch_vars->add (decl);
1697 /* Some front ends do not explicitly declare all anonymous
1698 artificial variables. We compensate here by declaring the
1699 variables, though it would be better if the front ends would
1700 explicitly declare them. */
1701 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1702 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1703 gimple_add_tmp_var (decl);
1705 if (init && init != error_mark_node)
1707 if (!TREE_STATIC (decl))
1709 DECL_INITIAL (decl) = NULL_TREE;
1710 init = build2 (INIT_EXPR, void_type_node, decl, init);
1711 gimplify_and_add (init, seq_p);
1712 ggc_free (init);
1714 else
1715 /* We must still examine initializers for static variables
1716 as they may contain a label address. */
1717 walk_tree (&init, force_labels_r, NULL, NULL);
1721 return GS_ALL_DONE;
1724 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1725 and replacing the LOOP_EXPR with goto, but if the loop contains an
1726 EXIT_EXPR, we need to append a label for it to jump to. */
1728 static enum gimplify_status
1729 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1731 tree saved_label = gimplify_ctxp->exit_label;
1732 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1734 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1736 gimplify_ctxp->exit_label = NULL_TREE;
1738 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1740 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1742 if (gimplify_ctxp->exit_label)
1743 gimplify_seq_add_stmt (pre_p,
1744 gimple_build_label (gimplify_ctxp->exit_label));
1746 gimplify_ctxp->exit_label = saved_label;
1748 *expr_p = NULL;
1749 return GS_ALL_DONE;
1752 /* Gimplify a statement list onto a sequence. These may be created either
1753 by an enlightened front-end, or by shortcut_cond_expr. */
1755 static enum gimplify_status
1756 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1758 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1760 tree_stmt_iterator i = tsi_start (*expr_p);
1762 while (!tsi_end_p (i))
1764 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1765 tsi_delink (&i);
1768 if (temp)
1770 *expr_p = temp;
1771 return GS_OK;
1774 return GS_ALL_DONE;
1777 /* Callback for walk_gimple_seq. */
1779 static tree
1780 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1781 struct walk_stmt_info *wi)
1783 gimple *stmt = gsi_stmt (*gsi_p);
1785 *handled_ops_p = true;
1786 switch (gimple_code (stmt))
1788 case GIMPLE_TRY:
1789 /* A compiler-generated cleanup or a user-written try block.
1790 If it's empty, don't dive into it--that would result in
1791 worse location info. */
1792 if (gimple_try_eval (stmt) == NULL)
1794 wi->info = stmt;
1795 return integer_zero_node;
1797 /* Fall through. */
1798 case GIMPLE_BIND:
1799 case GIMPLE_CATCH:
1800 case GIMPLE_EH_FILTER:
1801 case GIMPLE_TRANSACTION:
1802 /* Walk the sub-statements. */
1803 *handled_ops_p = false;
1804 break;
1806 case GIMPLE_DEBUG:
1807 /* Ignore these. We may generate them before declarations that
1808 are never executed. If there's something to warn about,
1809 there will be non-debug stmts too, and we'll catch those. */
1810 break;
1812 case GIMPLE_CALL:
1813 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1815 *handled_ops_p = false;
1816 break;
1818 /* Fall through. */
1819 default:
1820 /* Save the first "real" statement (not a decl/lexical scope/...). */
1821 wi->info = stmt;
1822 return integer_zero_node;
1824 return NULL_TREE;
1827 /* Possibly warn about unreachable statements between switch's controlling
1828 expression and the first case. SEQ is the body of a switch expression. */
1830 static void
1831 maybe_warn_switch_unreachable (gimple_seq seq)
1833 if (!warn_switch_unreachable
1834 /* This warning doesn't play well with Fortran when optimizations
1835 are on. */
1836 || lang_GNU_Fortran ()
1837 || seq == NULL)
1838 return;
1840 struct walk_stmt_info wi;
1841 memset (&wi, 0, sizeof (wi));
1842 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1843 gimple *stmt = (gimple *) wi.info;
1845 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1847 if (gimple_code (stmt) == GIMPLE_GOTO
1848 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1849 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1850 /* Don't warn for compiler-generated gotos. These occur
1851 in Duff's devices, for example. */;
1852 else
1853 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1854 "statement will never be executed");
1859 /* A label entry that pairs label and a location. */
1860 struct label_entry
1862 tree label;
1863 location_t loc;
1866 /* Find LABEL in vector of label entries VEC. */
1868 static struct label_entry *
1869 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1871 unsigned int i;
1872 struct label_entry *l;
1874 FOR_EACH_VEC_ELT (*vec, i, l)
1875 if (l->label == label)
1876 return l;
1877 return NULL;
1880 /* Return true if LABEL, a LABEL_DECL, represents a case label
1881 in a vector of labels CASES. */
1883 static bool
1884 case_label_p (const vec<tree> *cases, tree label)
1886 unsigned int i;
1887 tree l;
1889 FOR_EACH_VEC_ELT (*cases, i, l)
1890 if (CASE_LABEL (l) == label)
1891 return true;
1892 return false;
1895 /* Find the last nondebug statement in a scope STMT. */
1897 static gimple *
1898 last_stmt_in_scope (gimple *stmt)
1900 if (!stmt)
1901 return NULL;
1903 switch (gimple_code (stmt))
1905 case GIMPLE_BIND:
1907 gbind *bind = as_a <gbind *> (stmt);
1908 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1909 return last_stmt_in_scope (stmt);
1912 case GIMPLE_TRY:
1914 gtry *try_stmt = as_a <gtry *> (stmt);
1915 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1916 gimple *last_eval = last_stmt_in_scope (stmt);
1917 if (gimple_stmt_may_fallthru (last_eval)
1918 && (last_eval == NULL
1919 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1920 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1922 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1923 return last_stmt_in_scope (stmt);
1925 else
1926 return last_eval;
1929 case GIMPLE_DEBUG:
1930 gcc_unreachable ();
1932 default:
1933 return stmt;
1937 /* Collect interesting labels in LABELS and return the statement preceding
1938 another case label, or a user-defined label. */
1940 static gimple *
1941 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1942 auto_vec <struct label_entry> *labels)
1944 gimple *prev = NULL;
1948 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1950 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1951 which starts on a GIMPLE_SWITCH and ends with a break label.
1952 Handle that as a single statement that can fall through. */
1953 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1954 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1955 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1956 if (last
1957 && gimple_code (first) == GIMPLE_SWITCH
1958 && gimple_code (last) == GIMPLE_LABEL)
1960 tree label = gimple_label_label (as_a <glabel *> (last));
1961 if (SWITCH_BREAK_LABEL_P (label))
1963 prev = bind;
1964 gsi_next (gsi_p);
1965 continue;
1969 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1970 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1972 /* Nested scope. Only look at the last statement of
1973 the innermost scope. */
1974 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1975 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1976 if (last)
1978 prev = last;
1979 /* It might be a label without a location. Use the
1980 location of the scope then. */
1981 if (!gimple_has_location (prev))
1982 gimple_set_location (prev, bind_loc);
1984 gsi_next (gsi_p);
1985 continue;
1988 /* Ifs are tricky. */
1989 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1991 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1992 tree false_lab = gimple_cond_false_label (cond_stmt);
1993 location_t if_loc = gimple_location (cond_stmt);
1995 /* If we have e.g.
1996 if (i > 1) goto <D.2259>; else goto D;
1997 we can't do much with the else-branch. */
1998 if (!DECL_ARTIFICIAL (false_lab))
1999 break;
2001 /* Go on until the false label, then one step back. */
2002 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2004 gimple *stmt = gsi_stmt (*gsi_p);
2005 if (gimple_code (stmt) == GIMPLE_LABEL
2006 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2007 break;
2010 /* Not found? Oops. */
2011 if (gsi_end_p (*gsi_p))
2012 break;
2014 struct label_entry l = { false_lab, if_loc };
2015 labels->safe_push (l);
2017 /* Go to the last statement of the then branch. */
2018 gsi_prev (gsi_p);
2020 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2021 <D.1759>:
2022 <stmt>;
2023 goto <D.1761>;
2024 <D.1760>:
2026 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2027 && !gimple_has_location (gsi_stmt (*gsi_p)))
2029 /* Look at the statement before, it might be
2030 attribute fallthrough, in which case don't warn. */
2031 gsi_prev (gsi_p);
2032 bool fallthru_before_dest
2033 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2034 gsi_next (gsi_p);
2035 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2036 if (!fallthru_before_dest)
2038 struct label_entry l = { goto_dest, if_loc };
2039 labels->safe_push (l);
2042 /* And move back. */
2043 gsi_next (gsi_p);
2046 /* Remember the last statement. Skip labels that are of no interest
2047 to us. */
2048 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2050 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2051 if (find_label_entry (labels, label))
2052 prev = gsi_stmt (*gsi_p);
2054 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2056 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2057 prev = gsi_stmt (*gsi_p);
2058 gsi_next (gsi_p);
2060 while (!gsi_end_p (*gsi_p)
2061 /* Stop if we find a case or a user-defined label. */
2062 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2063 || !gimple_has_location (gsi_stmt (*gsi_p))));
2065 return prev;
2068 /* Return true if the switch fallthough warning should occur. LABEL is
2069 the label statement that we're falling through to. */
2071 static bool
2072 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2074 gimple_stmt_iterator gsi = *gsi_p;
2076 /* Don't warn if the label is marked with a "falls through" comment. */
2077 if (FALLTHROUGH_LABEL_P (label))
2078 return false;
2080 /* Don't warn for non-case labels followed by a statement:
2081 case 0:
2082 foo ();
2083 label:
2084 bar ();
2085 as these are likely intentional. */
2086 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2088 tree l;
2089 while (!gsi_end_p (gsi)
2090 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2091 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2092 && !case_label_p (&gimplify_ctxp->case_labels, l))
2093 gsi_next_nondebug (&gsi);
2094 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2095 return false;
2098 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2099 immediately breaks. */
2100 gsi = *gsi_p;
2102 /* Skip all immediately following labels. */
2103 while (!gsi_end_p (gsi)
2104 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2105 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2106 gsi_next_nondebug (&gsi);
2108 /* { ... something; default:; } */
2109 if (gsi_end_p (gsi)
2110 /* { ... something; default: break; } or
2111 { ... something; default: goto L; } */
2112 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2113 /* { ... something; default: return; } */
2114 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2115 return false;
2117 return true;
2120 /* Callback for walk_gimple_seq. */
2122 static tree
2123 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2124 struct walk_stmt_info *)
2126 gimple *stmt = gsi_stmt (*gsi_p);
2128 *handled_ops_p = true;
2129 switch (gimple_code (stmt))
2131 case GIMPLE_TRY:
2132 case GIMPLE_BIND:
2133 case GIMPLE_CATCH:
2134 case GIMPLE_EH_FILTER:
2135 case GIMPLE_TRANSACTION:
2136 /* Walk the sub-statements. */
2137 *handled_ops_p = false;
2138 break;
2140 /* Find a sequence of form:
2142 GIMPLE_LABEL
2143 [...]
2144 <may fallthru stmt>
2145 GIMPLE_LABEL
2147 and possibly warn. */
2148 case GIMPLE_LABEL:
2150 /* Found a label. Skip all immediately following labels. */
2151 while (!gsi_end_p (*gsi_p)
2152 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2153 gsi_next_nondebug (gsi_p);
2155 /* There might be no more statements. */
2156 if (gsi_end_p (*gsi_p))
2157 return integer_zero_node;
2159 /* Vector of labels that fall through. */
2160 auto_vec <struct label_entry> labels;
2161 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2163 /* There might be no more statements. */
2164 if (gsi_end_p (*gsi_p))
2165 return integer_zero_node;
2167 gimple *next = gsi_stmt (*gsi_p);
2168 tree label;
2169 /* If what follows is a label, then we may have a fallthrough. */
2170 if (gimple_code (next) == GIMPLE_LABEL
2171 && gimple_has_location (next)
2172 && (label = gimple_label_label (as_a <glabel *> (next)))
2173 && prev != NULL)
2175 struct label_entry *l;
2176 bool warned_p = false;
2177 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2178 /* Quiet. */;
2179 else if (gimple_code (prev) == GIMPLE_LABEL
2180 && (label = gimple_label_label (as_a <glabel *> (prev)))
2181 && (l = find_label_entry (&labels, label)))
2182 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2183 "this statement may fall through");
2184 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2185 /* Try to be clever and don't warn when the statement
2186 can't actually fall through. */
2187 && gimple_stmt_may_fallthru (prev)
2188 && gimple_has_location (prev))
2189 warned_p = warning_at (gimple_location (prev),
2190 OPT_Wimplicit_fallthrough_,
2191 "this statement may fall through");
2192 if (warned_p)
2193 inform (gimple_location (next), "here");
2195 /* Mark this label as processed so as to prevent multiple
2196 warnings in nested switches. */
2197 FALLTHROUGH_LABEL_P (label) = true;
2199 /* So that next warn_implicit_fallthrough_r will start looking for
2200 a new sequence starting with this label. */
2201 gsi_prev (gsi_p);
2204 break;
2205 default:
2206 break;
2208 return NULL_TREE;
2211 /* Warn when a switch case falls through. */
2213 static void
2214 maybe_warn_implicit_fallthrough (gimple_seq seq)
2216 if (!warn_implicit_fallthrough)
2217 return;
2219 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2220 if (!(lang_GNU_C ()
2221 || lang_GNU_CXX ()
2222 || lang_GNU_OBJC ()))
2223 return;
2225 struct walk_stmt_info wi;
2226 memset (&wi, 0, sizeof (wi));
2227 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2230 /* Callback for walk_gimple_seq. */
2232 static tree
2233 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2234 struct walk_stmt_info *)
2236 gimple *stmt = gsi_stmt (*gsi_p);
2238 *handled_ops_p = true;
2239 switch (gimple_code (stmt))
2241 case GIMPLE_TRY:
2242 case GIMPLE_BIND:
2243 case GIMPLE_CATCH:
2244 case GIMPLE_EH_FILTER:
2245 case GIMPLE_TRANSACTION:
2246 /* Walk the sub-statements. */
2247 *handled_ops_p = false;
2248 break;
2249 case GIMPLE_CALL:
2250 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2252 gsi_remove (gsi_p, true);
2253 if (gsi_end_p (*gsi_p))
2254 return integer_zero_node;
2256 bool found = false;
2257 location_t loc = gimple_location (stmt);
2259 gimple_stmt_iterator gsi2 = *gsi_p;
2260 stmt = gsi_stmt (gsi2);
2261 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2263 /* Go on until the artificial label. */
2264 tree goto_dest = gimple_goto_dest (stmt);
2265 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2267 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2268 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2269 == goto_dest)
2270 break;
2273 /* Not found? Stop. */
2274 if (gsi_end_p (gsi2))
2275 break;
2277 /* Look one past it. */
2278 gsi_next (&gsi2);
2281 /* We're looking for a case label or default label here. */
2282 while (!gsi_end_p (gsi2))
2284 stmt = gsi_stmt (gsi2);
2285 if (gimple_code (stmt) == GIMPLE_LABEL)
2287 tree label = gimple_label_label (as_a <glabel *> (stmt));
2288 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2290 found = true;
2291 break;
2294 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2296 else if (!is_gimple_debug (stmt))
2297 /* Anything else is not expected. */
2298 break;
2299 gsi_next (&gsi2);
2301 if (!found)
2302 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2303 "a case label or default label");
2305 break;
2306 default:
2307 break;
2309 return NULL_TREE;
2312 /* Expand all FALLTHROUGH () calls in SEQ. */
2314 static void
2315 expand_FALLTHROUGH (gimple_seq *seq_p)
2317 struct walk_stmt_info wi;
2318 memset (&wi, 0, sizeof (wi));
2319 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2323 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2324 branch to. */
2326 static enum gimplify_status
2327 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2329 tree switch_expr = *expr_p;
2330 gimple_seq switch_body_seq = NULL;
2331 enum gimplify_status ret;
2332 tree index_type = TREE_TYPE (switch_expr);
2333 if (index_type == NULL_TREE)
2334 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2336 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2337 fb_rvalue);
2338 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2339 return ret;
2341 if (SWITCH_BODY (switch_expr))
2343 vec<tree> labels;
2344 vec<tree> saved_labels;
2345 hash_set<tree> *saved_live_switch_vars = NULL;
2346 tree default_case = NULL_TREE;
2347 gswitch *switch_stmt;
2349 /* Save old labels, get new ones from body, then restore the old
2350 labels. Save all the things from the switch body to append after. */
2351 saved_labels = gimplify_ctxp->case_labels;
2352 gimplify_ctxp->case_labels.create (8);
2354 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2355 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2356 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2357 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2358 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2359 else
2360 gimplify_ctxp->live_switch_vars = NULL;
2362 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2363 gimplify_ctxp->in_switch_expr = true;
2365 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2367 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2368 maybe_warn_switch_unreachable (switch_body_seq);
2369 maybe_warn_implicit_fallthrough (switch_body_seq);
2370 /* Only do this for the outermost GIMPLE_SWITCH. */
2371 if (!gimplify_ctxp->in_switch_expr)
2372 expand_FALLTHROUGH (&switch_body_seq);
2374 labels = gimplify_ctxp->case_labels;
2375 gimplify_ctxp->case_labels = saved_labels;
2377 if (gimplify_ctxp->live_switch_vars)
2379 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2380 delete gimplify_ctxp->live_switch_vars;
2382 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2384 preprocess_case_label_vec_for_gimple (labels, index_type,
2385 &default_case);
2387 bool add_bind = false;
2388 if (!default_case)
2390 glabel *new_default;
2392 default_case
2393 = build_case_label (NULL_TREE, NULL_TREE,
2394 create_artificial_label (UNKNOWN_LOCATION));
2395 if (old_in_switch_expr)
2397 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2398 add_bind = true;
2400 new_default = gimple_build_label (CASE_LABEL (default_case));
2401 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2403 else if (old_in_switch_expr)
2405 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2406 if (last && gimple_code (last) == GIMPLE_LABEL)
2408 tree label = gimple_label_label (as_a <glabel *> (last));
2409 if (SWITCH_BREAK_LABEL_P (label))
2410 add_bind = true;
2414 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2415 default_case, labels);
2416 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2417 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2418 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2419 so that we can easily find the start and end of the switch
2420 statement. */
2421 if (add_bind)
2423 gimple_seq bind_body = NULL;
2424 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2425 gimple_seq_add_seq (&bind_body, switch_body_seq);
2426 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2427 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2428 gimplify_seq_add_stmt (pre_p, bind);
2430 else
2432 gimplify_seq_add_stmt (pre_p, switch_stmt);
2433 gimplify_seq_add_seq (pre_p, switch_body_seq);
2435 labels.release ();
2437 else
2438 gcc_unreachable ();
2440 return GS_ALL_DONE;
2443 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2445 static enum gimplify_status
2446 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2448 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2449 == current_function_decl);
2451 tree label = LABEL_EXPR_LABEL (*expr_p);
2452 glabel *label_stmt = gimple_build_label (label);
2453 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2454 gimplify_seq_add_stmt (pre_p, label_stmt);
2456 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2457 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2458 NOT_TAKEN));
2459 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2460 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2461 TAKEN));
2463 return GS_ALL_DONE;
2466 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2468 static enum gimplify_status
2469 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2471 struct gimplify_ctx *ctxp;
2472 glabel *label_stmt;
2474 /* Invalid programs can play Duff's Device type games with, for example,
2475 #pragma omp parallel. At least in the C front end, we don't
2476 detect such invalid branches until after gimplification, in the
2477 diagnose_omp_blocks pass. */
2478 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2479 if (ctxp->case_labels.exists ())
2480 break;
2482 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2483 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2484 ctxp->case_labels.safe_push (*expr_p);
2485 gimplify_seq_add_stmt (pre_p, label_stmt);
2487 return GS_ALL_DONE;
2490 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2491 if necessary. */
2493 tree
2494 build_and_jump (tree *label_p)
2496 if (label_p == NULL)
2497 /* If there's nowhere to jump, just fall through. */
2498 return NULL_TREE;
2500 if (*label_p == NULL_TREE)
2502 tree label = create_artificial_label (UNKNOWN_LOCATION);
2503 *label_p = label;
2506 return build1 (GOTO_EXPR, void_type_node, *label_p);
2509 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2510 This also involves building a label to jump to and communicating it to
2511 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2513 static enum gimplify_status
2514 gimplify_exit_expr (tree *expr_p)
2516 tree cond = TREE_OPERAND (*expr_p, 0);
2517 tree expr;
2519 expr = build_and_jump (&gimplify_ctxp->exit_label);
2520 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2521 *expr_p = expr;
2523 return GS_OK;
2526 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2527 different from its canonical type, wrap the whole thing inside a
2528 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2529 type.
2531 The canonical type of a COMPONENT_REF is the type of the field being
2532 referenced--unless the field is a bit-field which can be read directly
2533 in a smaller mode, in which case the canonical type is the
2534 sign-appropriate type corresponding to that mode. */
2536 static void
2537 canonicalize_component_ref (tree *expr_p)
2539 tree expr = *expr_p;
2540 tree type;
2542 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2544 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2545 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2546 else
2547 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2549 /* One could argue that all the stuff below is not necessary for
2550 the non-bitfield case and declare it a FE error if type
2551 adjustment would be needed. */
2552 if (TREE_TYPE (expr) != type)
2554 #ifdef ENABLE_TYPES_CHECKING
2555 tree old_type = TREE_TYPE (expr);
2556 #endif
2557 int type_quals;
2559 /* We need to preserve qualifiers and propagate them from
2560 operand 0. */
2561 type_quals = TYPE_QUALS (type)
2562 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2563 if (TYPE_QUALS (type) != type_quals)
2564 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2566 /* Set the type of the COMPONENT_REF to the underlying type. */
2567 TREE_TYPE (expr) = type;
2569 #ifdef ENABLE_TYPES_CHECKING
2570 /* It is now a FE error, if the conversion from the canonical
2571 type to the original expression type is not useless. */
2572 gcc_assert (useless_type_conversion_p (old_type, type));
2573 #endif
2577 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2578 to foo, embed that change in the ADDR_EXPR by converting
2579 T array[U];
2580 (T *)&array
2582 &array[L]
2583 where L is the lower bound. For simplicity, only do this for constant
2584 lower bound.
2585 The constraint is that the type of &array[L] is trivially convertible
2586 to T *. */
2588 static void
2589 canonicalize_addr_expr (tree *expr_p)
2591 tree expr = *expr_p;
2592 tree addr_expr = TREE_OPERAND (expr, 0);
2593 tree datype, ddatype, pddatype;
2595 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2596 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2597 || TREE_CODE (addr_expr) != ADDR_EXPR)
2598 return;
2600 /* The addr_expr type should be a pointer to an array. */
2601 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2602 if (TREE_CODE (datype) != ARRAY_TYPE)
2603 return;
2605 /* The pointer to element type shall be trivially convertible to
2606 the expression pointer type. */
2607 ddatype = TREE_TYPE (datype);
2608 pddatype = build_pointer_type (ddatype);
2609 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2610 pddatype))
2611 return;
2613 /* The lower bound and element sizes must be constant. */
2614 if (!TYPE_SIZE_UNIT (ddatype)
2615 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2616 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2617 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2618 return;
2620 /* All checks succeeded. Build a new node to merge the cast. */
2621 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2622 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2623 NULL_TREE, NULL_TREE);
2624 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2626 /* We can have stripped a required restrict qualifier above. */
2627 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2628 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2631 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2632 underneath as appropriate. */
2634 static enum gimplify_status
2635 gimplify_conversion (tree *expr_p)
2637 location_t loc = EXPR_LOCATION (*expr_p);
2638 gcc_assert (CONVERT_EXPR_P (*expr_p));
2640 /* Then strip away all but the outermost conversion. */
2641 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2643 /* And remove the outermost conversion if it's useless. */
2644 if (tree_ssa_useless_type_conversion (*expr_p))
2645 *expr_p = TREE_OPERAND (*expr_p, 0);
2647 /* If we still have a conversion at the toplevel,
2648 then canonicalize some constructs. */
2649 if (CONVERT_EXPR_P (*expr_p))
2651 tree sub = TREE_OPERAND (*expr_p, 0);
2653 /* If a NOP conversion is changing the type of a COMPONENT_REF
2654 expression, then canonicalize its type now in order to expose more
2655 redundant conversions. */
2656 if (TREE_CODE (sub) == COMPONENT_REF)
2657 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2659 /* If a NOP conversion is changing a pointer to array of foo
2660 to a pointer to foo, embed that change in the ADDR_EXPR. */
2661 else if (TREE_CODE (sub) == ADDR_EXPR)
2662 canonicalize_addr_expr (expr_p);
2665 /* If we have a conversion to a non-register type force the
2666 use of a VIEW_CONVERT_EXPR instead. */
2667 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2668 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2669 TREE_OPERAND (*expr_p, 0));
2671 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2672 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2673 TREE_SET_CODE (*expr_p, NOP_EXPR);
2675 return GS_OK;
2678 /* Nonlocal VLAs seen in the current function. */
2679 static hash_set<tree> *nonlocal_vlas;
2681 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2682 static tree nonlocal_vla_vars;
2684 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2685 DECL_VALUE_EXPR, and it's worth re-examining things. */
2687 static enum gimplify_status
2688 gimplify_var_or_parm_decl (tree *expr_p)
2690 tree decl = *expr_p;
2692 /* ??? If this is a local variable, and it has not been seen in any
2693 outer BIND_EXPR, then it's probably the result of a duplicate
2694 declaration, for which we've already issued an error. It would
2695 be really nice if the front end wouldn't leak these at all.
2696 Currently the only known culprit is C++ destructors, as seen
2697 in g++.old-deja/g++.jason/binding.C. */
2698 if (VAR_P (decl)
2699 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2700 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2701 && decl_function_context (decl) == current_function_decl)
2703 gcc_assert (seen_error ());
2704 return GS_ERROR;
2707 /* When within an OMP context, notice uses of variables. */
2708 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2709 return GS_ALL_DONE;
2711 /* If the decl is an alias for another expression, substitute it now. */
2712 if (DECL_HAS_VALUE_EXPR_P (decl))
2714 tree value_expr = DECL_VALUE_EXPR (decl);
2716 /* For referenced nonlocal VLAs add a decl for debugging purposes
2717 to the current function. */
2718 if (VAR_P (decl)
2719 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2720 && nonlocal_vlas != NULL
2721 && TREE_CODE (value_expr) == INDIRECT_REF
2722 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2723 && decl_function_context (decl) != current_function_decl)
2725 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2726 while (ctx
2727 && (ctx->region_type == ORT_WORKSHARE
2728 || ctx->region_type == ORT_SIMD
2729 || ctx->region_type == ORT_ACC))
2730 ctx = ctx->outer_context;
2731 if (!ctx && !nonlocal_vlas->add (decl))
2733 tree copy = copy_node (decl);
2735 lang_hooks.dup_lang_specific_decl (copy);
2736 SET_DECL_RTL (copy, 0);
2737 TREE_USED (copy) = 1;
2738 DECL_CHAIN (copy) = nonlocal_vla_vars;
2739 nonlocal_vla_vars = copy;
2740 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2741 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2745 *expr_p = unshare_expr (value_expr);
2746 return GS_OK;
2749 return GS_ALL_DONE;
2752 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2754 static void
2755 recalculate_side_effects (tree t)
2757 enum tree_code code = TREE_CODE (t);
2758 int len = TREE_OPERAND_LENGTH (t);
2759 int i;
2761 switch (TREE_CODE_CLASS (code))
2763 case tcc_expression:
2764 switch (code)
2766 case INIT_EXPR:
2767 case MODIFY_EXPR:
2768 case VA_ARG_EXPR:
2769 case PREDECREMENT_EXPR:
2770 case PREINCREMENT_EXPR:
2771 case POSTDECREMENT_EXPR:
2772 case POSTINCREMENT_EXPR:
2773 /* All of these have side-effects, no matter what their
2774 operands are. */
2775 return;
2777 default:
2778 break;
2780 /* Fall through. */
2782 case tcc_comparison: /* a comparison expression */
2783 case tcc_unary: /* a unary arithmetic expression */
2784 case tcc_binary: /* a binary arithmetic expression */
2785 case tcc_reference: /* a reference */
2786 case tcc_vl_exp: /* a function call */
2787 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2788 for (i = 0; i < len; ++i)
2790 tree op = TREE_OPERAND (t, i);
2791 if (op && TREE_SIDE_EFFECTS (op))
2792 TREE_SIDE_EFFECTS (t) = 1;
2794 break;
2796 case tcc_constant:
2797 /* No side-effects. */
2798 return;
2800 default:
2801 gcc_unreachable ();
2805 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2806 node *EXPR_P.
2808 compound_lval
2809 : min_lval '[' val ']'
2810 | min_lval '.' ID
2811 | compound_lval '[' val ']'
2812 | compound_lval '.' ID
2814 This is not part of the original SIMPLE definition, which separates
2815 array and member references, but it seems reasonable to handle them
2816 together. Also, this way we don't run into problems with union
2817 aliasing; gcc requires that for accesses through a union to alias, the
2818 union reference must be explicit, which was not always the case when we
2819 were splitting up array and member refs.
2821 PRE_P points to the sequence where side effects that must happen before
2822 *EXPR_P should be stored.
2824 POST_P points to the sequence where side effects that must happen after
2825 *EXPR_P should be stored. */
2827 static enum gimplify_status
2828 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2829 fallback_t fallback)
2831 tree *p;
2832 enum gimplify_status ret = GS_ALL_DONE, tret;
2833 int i;
2834 location_t loc = EXPR_LOCATION (*expr_p);
2835 tree expr = *expr_p;
2837 /* Create a stack of the subexpressions so later we can walk them in
2838 order from inner to outer. */
2839 auto_vec<tree, 10> expr_stack;
2841 /* We can handle anything that get_inner_reference can deal with. */
2842 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2844 restart:
2845 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2846 if (TREE_CODE (*p) == INDIRECT_REF)
2847 *p = fold_indirect_ref_loc (loc, *p);
2849 if (handled_component_p (*p))
2851 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2852 additional COMPONENT_REFs. */
2853 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2854 && gimplify_var_or_parm_decl (p) == GS_OK)
2855 goto restart;
2856 else
2857 break;
2859 expr_stack.safe_push (*p);
2862 gcc_assert (expr_stack.length ());
2864 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2865 walked through and P points to the innermost expression.
2867 Java requires that we elaborated nodes in source order. That
2868 means we must gimplify the inner expression followed by each of
2869 the indices, in order. But we can't gimplify the inner
2870 expression until we deal with any variable bounds, sizes, or
2871 positions in order to deal with PLACEHOLDER_EXPRs.
2873 So we do this in three steps. First we deal with the annotations
2874 for any variables in the components, then we gimplify the base,
2875 then we gimplify any indices, from left to right. */
2876 for (i = expr_stack.length () - 1; i >= 0; i--)
2878 tree t = expr_stack[i];
2880 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2882 /* Gimplify the low bound and element type size and put them into
2883 the ARRAY_REF. If these values are set, they have already been
2884 gimplified. */
2885 if (TREE_OPERAND (t, 2) == NULL_TREE)
2887 tree low = unshare_expr (array_ref_low_bound (t));
2888 if (!is_gimple_min_invariant (low))
2890 TREE_OPERAND (t, 2) = low;
2891 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2892 post_p, is_gimple_reg,
2893 fb_rvalue);
2894 ret = MIN (ret, tret);
2897 else
2899 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2900 is_gimple_reg, fb_rvalue);
2901 ret = MIN (ret, tret);
2904 if (TREE_OPERAND (t, 3) == NULL_TREE)
2906 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2907 tree elmt_size = unshare_expr (array_ref_element_size (t));
2908 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2910 /* Divide the element size by the alignment of the element
2911 type (above). */
2912 elmt_size
2913 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2915 if (!is_gimple_min_invariant (elmt_size))
2917 TREE_OPERAND (t, 3) = elmt_size;
2918 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2919 post_p, is_gimple_reg,
2920 fb_rvalue);
2921 ret = MIN (ret, tret);
2924 else
2926 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2927 is_gimple_reg, fb_rvalue);
2928 ret = MIN (ret, tret);
2931 else if (TREE_CODE (t) == COMPONENT_REF)
2933 /* Set the field offset into T and gimplify it. */
2934 if (TREE_OPERAND (t, 2) == NULL_TREE)
2936 tree offset = unshare_expr (component_ref_field_offset (t));
2937 tree field = TREE_OPERAND (t, 1);
2938 tree factor
2939 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2941 /* Divide the offset by its alignment. */
2942 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2944 if (!is_gimple_min_invariant (offset))
2946 TREE_OPERAND (t, 2) = offset;
2947 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2948 post_p, is_gimple_reg,
2949 fb_rvalue);
2950 ret = MIN (ret, tret);
2953 else
2955 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2956 is_gimple_reg, fb_rvalue);
2957 ret = MIN (ret, tret);
2962 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2963 so as to match the min_lval predicate. Failure to do so may result
2964 in the creation of large aggregate temporaries. */
2965 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2966 fallback | fb_lvalue);
2967 ret = MIN (ret, tret);
2969 /* And finally, the indices and operands of ARRAY_REF. During this
2970 loop we also remove any useless conversions. */
2971 for (; expr_stack.length () > 0; )
2973 tree t = expr_stack.pop ();
2975 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2977 /* Gimplify the dimension. */
2978 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2980 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2981 is_gimple_val, fb_rvalue);
2982 ret = MIN (ret, tret);
2986 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2988 /* The innermost expression P may have originally had
2989 TREE_SIDE_EFFECTS set which would have caused all the outer
2990 expressions in *EXPR_P leading to P to also have had
2991 TREE_SIDE_EFFECTS set. */
2992 recalculate_side_effects (t);
2995 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2996 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2998 canonicalize_component_ref (expr_p);
3001 expr_stack.release ();
3003 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3005 return ret;
3008 /* Gimplify the self modifying expression pointed to by EXPR_P
3009 (++, --, +=, -=).
3011 PRE_P points to the list where side effects that must happen before
3012 *EXPR_P should be stored.
3014 POST_P points to the list where side effects that must happen after
3015 *EXPR_P should be stored.
3017 WANT_VALUE is nonzero iff we want to use the value of this expression
3018 in another expression.
3020 ARITH_TYPE is the type the computation should be performed in. */
3022 enum gimplify_status
3023 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3024 bool want_value, tree arith_type)
3026 enum tree_code code;
3027 tree lhs, lvalue, rhs, t1;
3028 gimple_seq post = NULL, *orig_post_p = post_p;
3029 bool postfix;
3030 enum tree_code arith_code;
3031 enum gimplify_status ret;
3032 location_t loc = EXPR_LOCATION (*expr_p);
3034 code = TREE_CODE (*expr_p);
3036 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3037 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3039 /* Prefix or postfix? */
3040 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3041 /* Faster to treat as prefix if result is not used. */
3042 postfix = want_value;
3043 else
3044 postfix = false;
3046 /* For postfix, make sure the inner expression's post side effects
3047 are executed after side effects from this expression. */
3048 if (postfix)
3049 post_p = &post;
3051 /* Add or subtract? */
3052 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3053 arith_code = PLUS_EXPR;
3054 else
3055 arith_code = MINUS_EXPR;
3057 /* Gimplify the LHS into a GIMPLE lvalue. */
3058 lvalue = TREE_OPERAND (*expr_p, 0);
3059 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3060 if (ret == GS_ERROR)
3061 return ret;
3063 /* Extract the operands to the arithmetic operation. */
3064 lhs = lvalue;
3065 rhs = TREE_OPERAND (*expr_p, 1);
3067 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3068 that as the result value and in the postqueue operation. */
3069 if (postfix)
3071 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3072 if (ret == GS_ERROR)
3073 return ret;
3075 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3078 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3079 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3081 rhs = convert_to_ptrofftype_loc (loc, rhs);
3082 if (arith_code == MINUS_EXPR)
3083 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3084 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3086 else
3087 t1 = fold_convert (TREE_TYPE (*expr_p),
3088 fold_build2 (arith_code, arith_type,
3089 fold_convert (arith_type, lhs),
3090 fold_convert (arith_type, rhs)));
3092 if (postfix)
3094 gimplify_assign (lvalue, t1, pre_p);
3095 gimplify_seq_add_seq (orig_post_p, post);
3096 *expr_p = lhs;
3097 return GS_ALL_DONE;
3099 else
3101 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3102 return GS_OK;
3106 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3108 static void
3109 maybe_with_size_expr (tree *expr_p)
3111 tree expr = *expr_p;
3112 tree type = TREE_TYPE (expr);
3113 tree size;
3115 /* If we've already wrapped this or the type is error_mark_node, we can't do
3116 anything. */
3117 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3118 || type == error_mark_node)
3119 return;
3121 /* If the size isn't known or is a constant, we have nothing to do. */
3122 size = TYPE_SIZE_UNIT (type);
3123 if (!size || poly_int_tree_p (size))
3124 return;
3126 /* Otherwise, make a WITH_SIZE_EXPR. */
3127 size = unshare_expr (size);
3128 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3129 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3132 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3133 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3134 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3135 gimplified to an SSA name. */
3137 enum gimplify_status
3138 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3139 bool allow_ssa)
3141 bool (*test) (tree);
3142 fallback_t fb;
3144 /* In general, we allow lvalues for function arguments to avoid
3145 extra overhead of copying large aggregates out of even larger
3146 aggregates into temporaries only to copy the temporaries to
3147 the argument list. Make optimizers happy by pulling out to
3148 temporaries those types that fit in registers. */
3149 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3150 test = is_gimple_val, fb = fb_rvalue;
3151 else
3153 test = is_gimple_lvalue, fb = fb_either;
3154 /* Also strip a TARGET_EXPR that would force an extra copy. */
3155 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3157 tree init = TARGET_EXPR_INITIAL (*arg_p);
3158 if (init
3159 && !VOID_TYPE_P (TREE_TYPE (init)))
3160 *arg_p = init;
3164 /* If this is a variable sized type, we must remember the size. */
3165 maybe_with_size_expr (arg_p);
3167 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3168 /* Make sure arguments have the same location as the function call
3169 itself. */
3170 protected_set_expr_location (*arg_p, call_location);
3172 /* There is a sequence point before a function call. Side effects in
3173 the argument list must occur before the actual call. So, when
3174 gimplifying arguments, force gimplify_expr to use an internal
3175 post queue which is then appended to the end of PRE_P. */
3176 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3179 /* Don't fold inside offloading or taskreg regions: it can break code by
3180 adding decl references that weren't in the source. We'll do it during
3181 omplower pass instead. */
3183 static bool
3184 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3186 struct gimplify_omp_ctx *ctx;
3187 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3188 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3189 return false;
3190 return fold_stmt (gsi);
3193 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3194 WANT_VALUE is true if the result of the call is desired. */
3196 static enum gimplify_status
3197 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3199 tree fndecl, parms, p, fnptrtype;
3200 enum gimplify_status ret;
3201 int i, nargs;
3202 gcall *call;
3203 bool builtin_va_start_p = false;
3204 location_t loc = EXPR_LOCATION (*expr_p);
3206 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3208 /* For reliable diagnostics during inlining, it is necessary that
3209 every call_expr be annotated with file and line. */
3210 if (! EXPR_HAS_LOCATION (*expr_p))
3211 SET_EXPR_LOCATION (*expr_p, input_location);
3213 /* Gimplify internal functions created in the FEs. */
3214 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3216 if (want_value)
3217 return GS_ALL_DONE;
3219 nargs = call_expr_nargs (*expr_p);
3220 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3221 auto_vec<tree> vargs (nargs);
3223 for (i = 0; i < nargs; i++)
3225 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3226 EXPR_LOCATION (*expr_p));
3227 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3230 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3231 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3232 gimplify_seq_add_stmt (pre_p, call);
3233 return GS_ALL_DONE;
3236 /* This may be a call to a builtin function.
3238 Builtin function calls may be transformed into different
3239 (and more efficient) builtin function calls under certain
3240 circumstances. Unfortunately, gimplification can muck things
3241 up enough that the builtin expanders are not aware that certain
3242 transformations are still valid.
3244 So we attempt transformation/gimplification of the call before
3245 we gimplify the CALL_EXPR. At this time we do not manage to
3246 transform all calls in the same manner as the expanders do, but
3247 we do transform most of them. */
3248 fndecl = get_callee_fndecl (*expr_p);
3249 if (fndecl
3250 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3251 switch (DECL_FUNCTION_CODE (fndecl))
3253 CASE_BUILT_IN_ALLOCA:
3254 /* If the call has been built for a variable-sized object, then we
3255 want to restore the stack level when the enclosing BIND_EXPR is
3256 exited to reclaim the allocated space; otherwise, we precisely
3257 need to do the opposite and preserve the latest stack level. */
3258 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3259 gimplify_ctxp->save_stack = true;
3260 else
3261 gimplify_ctxp->keep_stack = true;
3262 break;
3264 case BUILT_IN_VA_START:
3266 builtin_va_start_p = TRUE;
3267 if (call_expr_nargs (*expr_p) < 2)
3269 error ("too few arguments to function %<va_start%>");
3270 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3271 return GS_OK;
3274 if (fold_builtin_next_arg (*expr_p, true))
3276 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3277 return GS_OK;
3279 break;
3282 default:
3285 if (fndecl && DECL_BUILT_IN (fndecl))
3287 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3288 if (new_tree && new_tree != *expr_p)
3290 /* There was a transformation of this call which computes the
3291 same value, but in a more efficient way. Return and try
3292 again. */
3293 *expr_p = new_tree;
3294 return GS_OK;
3298 /* Remember the original function pointer type. */
3299 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3301 /* There is a sequence point before the call, so any side effects in
3302 the calling expression must occur before the actual call. Force
3303 gimplify_expr to use an internal post queue. */
3304 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3305 is_gimple_call_addr, fb_rvalue);
3307 nargs = call_expr_nargs (*expr_p);
3309 /* Get argument types for verification. */
3310 fndecl = get_callee_fndecl (*expr_p);
3311 parms = NULL_TREE;
3312 if (fndecl)
3313 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3314 else
3315 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3317 if (fndecl && DECL_ARGUMENTS (fndecl))
3318 p = DECL_ARGUMENTS (fndecl);
3319 else if (parms)
3320 p = parms;
3321 else
3322 p = NULL_TREE;
3323 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3326 /* If the last argument is __builtin_va_arg_pack () and it is not
3327 passed as a named argument, decrease the number of CALL_EXPR
3328 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3329 if (!p
3330 && i < nargs
3331 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3333 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3334 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3336 if (last_arg_fndecl
3337 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3338 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3339 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3341 tree call = *expr_p;
3343 --nargs;
3344 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3345 CALL_EXPR_FN (call),
3346 nargs, CALL_EXPR_ARGP (call));
3348 /* Copy all CALL_EXPR flags, location and block, except
3349 CALL_EXPR_VA_ARG_PACK flag. */
3350 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3351 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3352 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3353 = CALL_EXPR_RETURN_SLOT_OPT (call);
3354 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3355 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3357 /* Set CALL_EXPR_VA_ARG_PACK. */
3358 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3362 /* If the call returns twice then after building the CFG the call
3363 argument computations will no longer dominate the call because
3364 we add an abnormal incoming edge to the call. So do not use SSA
3365 vars there. */
3366 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3368 /* Gimplify the function arguments. */
3369 if (nargs > 0)
3371 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3372 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3373 PUSH_ARGS_REVERSED ? i-- : i++)
3375 enum gimplify_status t;
3377 /* Avoid gimplifying the second argument to va_start, which needs to
3378 be the plain PARM_DECL. */
3379 if ((i != 1) || !builtin_va_start_p)
3381 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3382 EXPR_LOCATION (*expr_p), ! returns_twice);
3384 if (t == GS_ERROR)
3385 ret = GS_ERROR;
3390 /* Gimplify the static chain. */
3391 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3393 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3394 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3395 else
3397 enum gimplify_status t;
3398 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3399 EXPR_LOCATION (*expr_p), ! returns_twice);
3400 if (t == GS_ERROR)
3401 ret = GS_ERROR;
3405 /* Verify the function result. */
3406 if (want_value && fndecl
3407 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3409 error_at (loc, "using result of function returning %<void%>");
3410 ret = GS_ERROR;
3413 /* Try this again in case gimplification exposed something. */
3414 if (ret != GS_ERROR)
3416 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3418 if (new_tree && new_tree != *expr_p)
3420 /* There was a transformation of this call which computes the
3421 same value, but in a more efficient way. Return and try
3422 again. */
3423 *expr_p = new_tree;
3424 return GS_OK;
3427 else
3429 *expr_p = error_mark_node;
3430 return GS_ERROR;
3433 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3434 decl. This allows us to eliminate redundant or useless
3435 calls to "const" functions. */
3436 if (TREE_CODE (*expr_p) == CALL_EXPR)
3438 int flags = call_expr_flags (*expr_p);
3439 if (flags & (ECF_CONST | ECF_PURE)
3440 /* An infinite loop is considered a side effect. */
3441 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3442 TREE_SIDE_EFFECTS (*expr_p) = 0;
3445 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3446 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3447 form and delegate the creation of a GIMPLE_CALL to
3448 gimplify_modify_expr. This is always possible because when
3449 WANT_VALUE is true, the caller wants the result of this call into
3450 a temporary, which means that we will emit an INIT_EXPR in
3451 internal_get_tmp_var which will then be handled by
3452 gimplify_modify_expr. */
3453 if (!want_value)
3455 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3456 have to do is replicate it as a GIMPLE_CALL tuple. */
3457 gimple_stmt_iterator gsi;
3458 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3459 notice_special_calls (call);
3460 gimplify_seq_add_stmt (pre_p, call);
3461 gsi = gsi_last (*pre_p);
3462 maybe_fold_stmt (&gsi);
3463 *expr_p = NULL_TREE;
3465 else
3466 /* Remember the original function type. */
3467 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3468 CALL_EXPR_FN (*expr_p));
3470 return ret;
3473 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3474 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3476 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3477 condition is true or false, respectively. If null, we should generate
3478 our own to skip over the evaluation of this specific expression.
3480 LOCUS is the source location of the COND_EXPR.
3482 This function is the tree equivalent of do_jump.
3484 shortcut_cond_r should only be called by shortcut_cond_expr. */
3486 static tree
3487 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3488 location_t locus)
3490 tree local_label = NULL_TREE;
3491 tree t, expr = NULL;
3493 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3494 retain the shortcut semantics. Just insert the gotos here;
3495 shortcut_cond_expr will append the real blocks later. */
3496 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3498 location_t new_locus;
3500 /* Turn if (a && b) into
3502 if (a); else goto no;
3503 if (b) goto yes; else goto no;
3504 (no:) */
3506 if (false_label_p == NULL)
3507 false_label_p = &local_label;
3509 /* Keep the original source location on the first 'if'. */
3510 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3511 append_to_statement_list (t, &expr);
3513 /* Set the source location of the && on the second 'if'. */
3514 new_locus = rexpr_location (pred, locus);
3515 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3516 new_locus);
3517 append_to_statement_list (t, &expr);
3519 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3521 location_t new_locus;
3523 /* Turn if (a || b) into
3525 if (a) goto yes;
3526 if (b) goto yes; else goto no;
3527 (yes:) */
3529 if (true_label_p == NULL)
3530 true_label_p = &local_label;
3532 /* Keep the original source location on the first 'if'. */
3533 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3534 append_to_statement_list (t, &expr);
3536 /* Set the source location of the || on the second 'if'. */
3537 new_locus = rexpr_location (pred, locus);
3538 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3539 new_locus);
3540 append_to_statement_list (t, &expr);
3542 else if (TREE_CODE (pred) == COND_EXPR
3543 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3544 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3546 location_t new_locus;
3548 /* As long as we're messing with gotos, turn if (a ? b : c) into
3549 if (a)
3550 if (b) goto yes; else goto no;
3551 else
3552 if (c) goto yes; else goto no;
3554 Don't do this if one of the arms has void type, which can happen
3555 in C++ when the arm is throw. */
3557 /* Keep the original source location on the first 'if'. Set the source
3558 location of the ? on the second 'if'. */
3559 new_locus = rexpr_location (pred, locus);
3560 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3561 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3562 false_label_p, locus),
3563 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3564 false_label_p, new_locus));
3566 else
3568 expr = build3 (COND_EXPR, void_type_node, pred,
3569 build_and_jump (true_label_p),
3570 build_and_jump (false_label_p));
3571 SET_EXPR_LOCATION (expr, locus);
3574 if (local_label)
3576 t = build1 (LABEL_EXPR, void_type_node, local_label);
3577 append_to_statement_list (t, &expr);
3580 return expr;
3583 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3584 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3585 statement, if it is the last one. Otherwise, return NULL. */
3587 static tree
3588 find_goto (tree expr)
3590 if (!expr)
3591 return NULL_TREE;
3593 if (TREE_CODE (expr) == GOTO_EXPR)
3594 return expr;
3596 if (TREE_CODE (expr) != STATEMENT_LIST)
3597 return NULL_TREE;
3599 tree_stmt_iterator i = tsi_start (expr);
3601 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3602 tsi_next (&i);
3604 if (!tsi_one_before_end_p (i))
3605 return NULL_TREE;
3607 return find_goto (tsi_stmt (i));
3610 /* Same as find_goto, except that it returns NULL if the destination
3611 is not a LABEL_DECL. */
3613 static inline tree
3614 find_goto_label (tree expr)
3616 tree dest = find_goto (expr);
3617 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3618 return dest;
3619 return NULL_TREE;
3622 /* Given a conditional expression EXPR with short-circuit boolean
3623 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3624 predicate apart into the equivalent sequence of conditionals. */
3626 static tree
3627 shortcut_cond_expr (tree expr)
3629 tree pred = TREE_OPERAND (expr, 0);
3630 tree then_ = TREE_OPERAND (expr, 1);
3631 tree else_ = TREE_OPERAND (expr, 2);
3632 tree true_label, false_label, end_label, t;
3633 tree *true_label_p;
3634 tree *false_label_p;
3635 bool emit_end, emit_false, jump_over_else;
3636 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3637 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3639 /* First do simple transformations. */
3640 if (!else_se)
3642 /* If there is no 'else', turn
3643 if (a && b) then c
3644 into
3645 if (a) if (b) then c. */
3646 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3648 /* Keep the original source location on the first 'if'. */
3649 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3650 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3651 /* Set the source location of the && on the second 'if'. */
3652 if (rexpr_has_location (pred))
3653 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3654 then_ = shortcut_cond_expr (expr);
3655 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3656 pred = TREE_OPERAND (pred, 0);
3657 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3658 SET_EXPR_LOCATION (expr, locus);
3662 if (!then_se)
3664 /* If there is no 'then', turn
3665 if (a || b); else d
3666 into
3667 if (a); else if (b); else d. */
3668 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3670 /* Keep the original source location on the first 'if'. */
3671 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3672 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3673 /* Set the source location of the || on the second 'if'. */
3674 if (rexpr_has_location (pred))
3675 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3676 else_ = shortcut_cond_expr (expr);
3677 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3678 pred = TREE_OPERAND (pred, 0);
3679 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3680 SET_EXPR_LOCATION (expr, locus);
3684 /* If we're done, great. */
3685 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3686 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3687 return expr;
3689 /* Otherwise we need to mess with gotos. Change
3690 if (a) c; else d;
3692 if (a); else goto no;
3693 c; goto end;
3694 no: d; end:
3695 and recursively gimplify the condition. */
3697 true_label = false_label = end_label = NULL_TREE;
3699 /* If our arms just jump somewhere, hijack those labels so we don't
3700 generate jumps to jumps. */
3702 if (tree then_goto = find_goto_label (then_))
3704 true_label = GOTO_DESTINATION (then_goto);
3705 then_ = NULL;
3706 then_se = false;
3709 if (tree else_goto = find_goto_label (else_))
3711 false_label = GOTO_DESTINATION (else_goto);
3712 else_ = NULL;
3713 else_se = false;
3716 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3717 if (true_label)
3718 true_label_p = &true_label;
3719 else
3720 true_label_p = NULL;
3722 /* The 'else' branch also needs a label if it contains interesting code. */
3723 if (false_label || else_se)
3724 false_label_p = &false_label;
3725 else
3726 false_label_p = NULL;
3728 /* If there was nothing else in our arms, just forward the label(s). */
3729 if (!then_se && !else_se)
3730 return shortcut_cond_r (pred, true_label_p, false_label_p,
3731 EXPR_LOC_OR_LOC (expr, input_location));
3733 /* If our last subexpression already has a terminal label, reuse it. */
3734 if (else_se)
3735 t = expr_last (else_);
3736 else if (then_se)
3737 t = expr_last (then_);
3738 else
3739 t = NULL;
3740 if (t && TREE_CODE (t) == LABEL_EXPR)
3741 end_label = LABEL_EXPR_LABEL (t);
3743 /* If we don't care about jumping to the 'else' branch, jump to the end
3744 if the condition is false. */
3745 if (!false_label_p)
3746 false_label_p = &end_label;
3748 /* We only want to emit these labels if we aren't hijacking them. */
3749 emit_end = (end_label == NULL_TREE);
3750 emit_false = (false_label == NULL_TREE);
3752 /* We only emit the jump over the else clause if we have to--if the
3753 then clause may fall through. Otherwise we can wind up with a
3754 useless jump and a useless label at the end of gimplified code,
3755 which will cause us to think that this conditional as a whole
3756 falls through even if it doesn't. If we then inline a function
3757 which ends with such a condition, that can cause us to issue an
3758 inappropriate warning about control reaching the end of a
3759 non-void function. */
3760 jump_over_else = block_may_fallthru (then_);
3762 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3763 EXPR_LOC_OR_LOC (expr, input_location));
3765 expr = NULL;
3766 append_to_statement_list (pred, &expr);
3768 append_to_statement_list (then_, &expr);
3769 if (else_se)
3771 if (jump_over_else)
3773 tree last = expr_last (expr);
3774 t = build_and_jump (&end_label);
3775 if (rexpr_has_location (last))
3776 SET_EXPR_LOCATION (t, rexpr_location (last));
3777 append_to_statement_list (t, &expr);
3779 if (emit_false)
3781 t = build1 (LABEL_EXPR, void_type_node, false_label);
3782 append_to_statement_list (t, &expr);
3784 append_to_statement_list (else_, &expr);
3786 if (emit_end && end_label)
3788 t = build1 (LABEL_EXPR, void_type_node, end_label);
3789 append_to_statement_list (t, &expr);
3792 return expr;
3795 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3797 tree
3798 gimple_boolify (tree expr)
3800 tree type = TREE_TYPE (expr);
3801 location_t loc = EXPR_LOCATION (expr);
3803 if (TREE_CODE (expr) == NE_EXPR
3804 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3805 && integer_zerop (TREE_OPERAND (expr, 1)))
3807 tree call = TREE_OPERAND (expr, 0);
3808 tree fn = get_callee_fndecl (call);
3810 /* For __builtin_expect ((long) (x), y) recurse into x as well
3811 if x is truth_value_p. */
3812 if (fn
3813 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3814 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3815 && call_expr_nargs (call) == 2)
3817 tree arg = CALL_EXPR_ARG (call, 0);
3818 if (arg)
3820 if (TREE_CODE (arg) == NOP_EXPR
3821 && TREE_TYPE (arg) == TREE_TYPE (call))
3822 arg = TREE_OPERAND (arg, 0);
3823 if (truth_value_p (TREE_CODE (arg)))
3825 arg = gimple_boolify (arg);
3826 CALL_EXPR_ARG (call, 0)
3827 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3833 switch (TREE_CODE (expr))
3835 case TRUTH_AND_EXPR:
3836 case TRUTH_OR_EXPR:
3837 case TRUTH_XOR_EXPR:
3838 case TRUTH_ANDIF_EXPR:
3839 case TRUTH_ORIF_EXPR:
3840 /* Also boolify the arguments of truth exprs. */
3841 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3842 /* FALLTHRU */
3844 case TRUTH_NOT_EXPR:
3845 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3847 /* These expressions always produce boolean results. */
3848 if (TREE_CODE (type) != BOOLEAN_TYPE)
3849 TREE_TYPE (expr) = boolean_type_node;
3850 return expr;
3852 case ANNOTATE_EXPR:
3853 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3855 case annot_expr_ivdep_kind:
3856 case annot_expr_unroll_kind:
3857 case annot_expr_no_vector_kind:
3858 case annot_expr_vector_kind:
3859 case annot_expr_parallel_kind:
3860 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3861 if (TREE_CODE (type) != BOOLEAN_TYPE)
3862 TREE_TYPE (expr) = boolean_type_node;
3863 return expr;
3864 default:
3865 gcc_unreachable ();
3868 default:
3869 if (COMPARISON_CLASS_P (expr))
3871 /* There expressions always prduce boolean results. */
3872 if (TREE_CODE (type) != BOOLEAN_TYPE)
3873 TREE_TYPE (expr) = boolean_type_node;
3874 return expr;
3876 /* Other expressions that get here must have boolean values, but
3877 might need to be converted to the appropriate mode. */
3878 if (TREE_CODE (type) == BOOLEAN_TYPE)
3879 return expr;
3880 return fold_convert_loc (loc, boolean_type_node, expr);
3884 /* Given a conditional expression *EXPR_P without side effects, gimplify
3885 its operands. New statements are inserted to PRE_P. */
3887 static enum gimplify_status
3888 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3890 tree expr = *expr_p, cond;
3891 enum gimplify_status ret, tret;
3892 enum tree_code code;
3894 cond = gimple_boolify (COND_EXPR_COND (expr));
3896 /* We need to handle && and || specially, as their gimplification
3897 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3898 code = TREE_CODE (cond);
3899 if (code == TRUTH_ANDIF_EXPR)
3900 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3901 else if (code == TRUTH_ORIF_EXPR)
3902 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3903 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3904 COND_EXPR_COND (*expr_p) = cond;
3906 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3907 is_gimple_val, fb_rvalue);
3908 ret = MIN (ret, tret);
3909 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3910 is_gimple_val, fb_rvalue);
3912 return MIN (ret, tret);
3915 /* Return true if evaluating EXPR could trap.
3916 EXPR is GENERIC, while tree_could_trap_p can be called
3917 only on GIMPLE. */
3919 static bool
3920 generic_expr_could_trap_p (tree expr)
3922 unsigned i, n;
3924 if (!expr || is_gimple_val (expr))
3925 return false;
3927 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3928 return true;
3930 n = TREE_OPERAND_LENGTH (expr);
3931 for (i = 0; i < n; i++)
3932 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3933 return true;
3935 return false;
3938 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3939 into
3941 if (p) if (p)
3942 t1 = a; a;
3943 else or else
3944 t1 = b; b;
3947 The second form is used when *EXPR_P is of type void.
3949 PRE_P points to the list where side effects that must happen before
3950 *EXPR_P should be stored. */
3952 static enum gimplify_status
3953 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3955 tree expr = *expr_p;
3956 tree type = TREE_TYPE (expr);
3957 location_t loc = EXPR_LOCATION (expr);
3958 tree tmp, arm1, arm2;
3959 enum gimplify_status ret;
3960 tree label_true, label_false, label_cont;
3961 bool have_then_clause_p, have_else_clause_p;
3962 gcond *cond_stmt;
3963 enum tree_code pred_code;
3964 gimple_seq seq = NULL;
3966 /* If this COND_EXPR has a value, copy the values into a temporary within
3967 the arms. */
3968 if (!VOID_TYPE_P (type))
3970 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3971 tree result;
3973 /* If either an rvalue is ok or we do not require an lvalue, create the
3974 temporary. But we cannot do that if the type is addressable. */
3975 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3976 && !TREE_ADDRESSABLE (type))
3978 if (gimplify_ctxp->allow_rhs_cond_expr
3979 /* If either branch has side effects or could trap, it can't be
3980 evaluated unconditionally. */
3981 && !TREE_SIDE_EFFECTS (then_)
3982 && !generic_expr_could_trap_p (then_)
3983 && !TREE_SIDE_EFFECTS (else_)
3984 && !generic_expr_could_trap_p (else_))
3985 return gimplify_pure_cond_expr (expr_p, pre_p);
3987 tmp = create_tmp_var (type, "iftmp");
3988 result = tmp;
3991 /* Otherwise, only create and copy references to the values. */
3992 else
3994 type = build_pointer_type (type);
3996 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3997 then_ = build_fold_addr_expr_loc (loc, then_);
3999 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4000 else_ = build_fold_addr_expr_loc (loc, else_);
4002 expr
4003 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4005 tmp = create_tmp_var (type, "iftmp");
4006 result = build_simple_mem_ref_loc (loc, tmp);
4009 /* Build the new then clause, `tmp = then_;'. But don't build the
4010 assignment if the value is void; in C++ it can be if it's a throw. */
4011 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4012 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4014 /* Similarly, build the new else clause, `tmp = else_;'. */
4015 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4016 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4018 TREE_TYPE (expr) = void_type_node;
4019 recalculate_side_effects (expr);
4021 /* Move the COND_EXPR to the prequeue. */
4022 gimplify_stmt (&expr, pre_p);
4024 *expr_p = result;
4025 return GS_ALL_DONE;
4028 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4029 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4030 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4031 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4033 /* Make sure the condition has BOOLEAN_TYPE. */
4034 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4036 /* Break apart && and || conditions. */
4037 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4038 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4040 expr = shortcut_cond_expr (expr);
4042 if (expr != *expr_p)
4044 *expr_p = expr;
4046 /* We can't rely on gimplify_expr to re-gimplify the expanded
4047 form properly, as cleanups might cause the target labels to be
4048 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4049 set up a conditional context. */
4050 gimple_push_condition ();
4051 gimplify_stmt (expr_p, &seq);
4052 gimple_pop_condition (pre_p);
4053 gimple_seq_add_seq (pre_p, seq);
4055 return GS_ALL_DONE;
4059 /* Now do the normal gimplification. */
4061 /* Gimplify condition. */
4062 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4063 fb_rvalue);
4064 if (ret == GS_ERROR)
4065 return GS_ERROR;
4066 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4068 gimple_push_condition ();
4070 have_then_clause_p = have_else_clause_p = false;
4071 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4072 if (label_true
4073 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4074 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4075 have different locations, otherwise we end up with incorrect
4076 location information on the branches. */
4077 && (optimize
4078 || !EXPR_HAS_LOCATION (expr)
4079 || !rexpr_has_location (label_true)
4080 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4082 have_then_clause_p = true;
4083 label_true = GOTO_DESTINATION (label_true);
4085 else
4086 label_true = create_artificial_label (UNKNOWN_LOCATION);
4087 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4088 if (label_false
4089 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4090 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4091 have different locations, otherwise we end up with incorrect
4092 location information on the branches. */
4093 && (optimize
4094 || !EXPR_HAS_LOCATION (expr)
4095 || !rexpr_has_location (label_false)
4096 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4098 have_else_clause_p = true;
4099 label_false = GOTO_DESTINATION (label_false);
4101 else
4102 label_false = create_artificial_label (UNKNOWN_LOCATION);
4104 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4105 &arm2);
4106 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4107 label_false);
4108 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4109 gimplify_seq_add_stmt (&seq, cond_stmt);
4110 gimple_stmt_iterator gsi = gsi_last (seq);
4111 maybe_fold_stmt (&gsi);
4113 label_cont = NULL_TREE;
4114 if (!have_then_clause_p)
4116 /* For if (...) {} else { code; } put label_true after
4117 the else block. */
4118 if (TREE_OPERAND (expr, 1) == NULL_TREE
4119 && !have_else_clause_p
4120 && TREE_OPERAND (expr, 2) != NULL_TREE)
4121 label_cont = label_true;
4122 else
4124 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4125 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4126 /* For if (...) { code; } else {} or
4127 if (...) { code; } else goto label; or
4128 if (...) { code; return; } else { ... }
4129 label_cont isn't needed. */
4130 if (!have_else_clause_p
4131 && TREE_OPERAND (expr, 2) != NULL_TREE
4132 && gimple_seq_may_fallthru (seq))
4134 gimple *g;
4135 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4137 g = gimple_build_goto (label_cont);
4139 /* GIMPLE_COND's are very low level; they have embedded
4140 gotos. This particular embedded goto should not be marked
4141 with the location of the original COND_EXPR, as it would
4142 correspond to the COND_EXPR's condition, not the ELSE or the
4143 THEN arms. To avoid marking it with the wrong location, flag
4144 it as "no location". */
4145 gimple_set_do_not_emit_location (g);
4147 gimplify_seq_add_stmt (&seq, g);
4151 if (!have_else_clause_p)
4153 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4154 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4156 if (label_cont)
4157 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4159 gimple_pop_condition (pre_p);
4160 gimple_seq_add_seq (pre_p, seq);
4162 if (ret == GS_ERROR)
4163 ; /* Do nothing. */
4164 else if (have_then_clause_p || have_else_clause_p)
4165 ret = GS_ALL_DONE;
4166 else
4168 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4169 expr = TREE_OPERAND (expr, 0);
4170 gimplify_stmt (&expr, pre_p);
4173 *expr_p = NULL;
4174 return ret;
4177 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4178 to be marked addressable.
4180 We cannot rely on such an expression being directly markable if a temporary
4181 has been created by the gimplification. In this case, we create another
4182 temporary and initialize it with a copy, which will become a store after we
4183 mark it addressable. This can happen if the front-end passed us something
4184 that it could not mark addressable yet, like a Fortran pass-by-reference
4185 parameter (int) floatvar. */
4187 static void
4188 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4190 while (handled_component_p (*expr_p))
4191 expr_p = &TREE_OPERAND (*expr_p, 0);
4192 if (is_gimple_reg (*expr_p))
4194 /* Do not allow an SSA name as the temporary. */
4195 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4196 DECL_GIMPLE_REG_P (var) = 0;
4197 *expr_p = var;
4201 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4202 a call to __builtin_memcpy. */
4204 static enum gimplify_status
4205 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4206 gimple_seq *seq_p)
4208 tree t, to, to_ptr, from, from_ptr;
4209 gcall *gs;
4210 location_t loc = EXPR_LOCATION (*expr_p);
4212 to = TREE_OPERAND (*expr_p, 0);
4213 from = TREE_OPERAND (*expr_p, 1);
4215 /* Mark the RHS addressable. Beware that it may not be possible to do so
4216 directly if a temporary has been created by the gimplification. */
4217 prepare_gimple_addressable (&from, seq_p);
4219 mark_addressable (from);
4220 from_ptr = build_fold_addr_expr_loc (loc, from);
4221 gimplify_arg (&from_ptr, seq_p, loc);
4223 mark_addressable (to);
4224 to_ptr = build_fold_addr_expr_loc (loc, to);
4225 gimplify_arg (&to_ptr, seq_p, loc);
4227 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4229 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4231 if (want_value)
4233 /* tmp = memcpy() */
4234 t = create_tmp_var (TREE_TYPE (to_ptr));
4235 gimple_call_set_lhs (gs, t);
4236 gimplify_seq_add_stmt (seq_p, gs);
4238 *expr_p = build_simple_mem_ref (t);
4239 return GS_ALL_DONE;
4242 gimplify_seq_add_stmt (seq_p, gs);
4243 *expr_p = NULL;
4244 return GS_ALL_DONE;
4247 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4248 a call to __builtin_memset. In this case we know that the RHS is
4249 a CONSTRUCTOR with an empty element list. */
4251 static enum gimplify_status
4252 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4253 gimple_seq *seq_p)
4255 tree t, from, to, to_ptr;
4256 gcall *gs;
4257 location_t loc = EXPR_LOCATION (*expr_p);
4259 /* Assert our assumptions, to abort instead of producing wrong code
4260 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4261 not be immediately exposed. */
4262 from = TREE_OPERAND (*expr_p, 1);
4263 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4264 from = TREE_OPERAND (from, 0);
4266 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4267 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4269 /* Now proceed. */
4270 to = TREE_OPERAND (*expr_p, 0);
4272 to_ptr = build_fold_addr_expr_loc (loc, to);
4273 gimplify_arg (&to_ptr, seq_p, loc);
4274 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4276 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4278 if (want_value)
4280 /* tmp = memset() */
4281 t = create_tmp_var (TREE_TYPE (to_ptr));
4282 gimple_call_set_lhs (gs, t);
4283 gimplify_seq_add_stmt (seq_p, gs);
4285 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4286 return GS_ALL_DONE;
4289 gimplify_seq_add_stmt (seq_p, gs);
4290 *expr_p = NULL;
4291 return GS_ALL_DONE;
4294 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4295 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4296 assignment. Return non-null if we detect a potential overlap. */
4298 struct gimplify_init_ctor_preeval_data
4300 /* The base decl of the lhs object. May be NULL, in which case we
4301 have to assume the lhs is indirect. */
4302 tree lhs_base_decl;
4304 /* The alias set of the lhs object. */
4305 alias_set_type lhs_alias_set;
4308 static tree
4309 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4311 struct gimplify_init_ctor_preeval_data *data
4312 = (struct gimplify_init_ctor_preeval_data *) xdata;
4313 tree t = *tp;
4315 /* If we find the base object, obviously we have overlap. */
4316 if (data->lhs_base_decl == t)
4317 return t;
4319 /* If the constructor component is indirect, determine if we have a
4320 potential overlap with the lhs. The only bits of information we
4321 have to go on at this point are addressability and alias sets. */
4322 if ((INDIRECT_REF_P (t)
4323 || TREE_CODE (t) == MEM_REF)
4324 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4325 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4326 return t;
4328 /* If the constructor component is a call, determine if it can hide a
4329 potential overlap with the lhs through an INDIRECT_REF like above.
4330 ??? Ugh - this is completely broken. In fact this whole analysis
4331 doesn't look conservative. */
4332 if (TREE_CODE (t) == CALL_EXPR)
4334 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4336 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4337 if (POINTER_TYPE_P (TREE_VALUE (type))
4338 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4339 && alias_sets_conflict_p (data->lhs_alias_set,
4340 get_alias_set
4341 (TREE_TYPE (TREE_VALUE (type)))))
4342 return t;
4345 if (IS_TYPE_OR_DECL_P (t))
4346 *walk_subtrees = 0;
4347 return NULL;
4350 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4351 force values that overlap with the lhs (as described by *DATA)
4352 into temporaries. */
4354 static void
4355 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4356 struct gimplify_init_ctor_preeval_data *data)
4358 enum gimplify_status one;
4360 /* If the value is constant, then there's nothing to pre-evaluate. */
4361 if (TREE_CONSTANT (*expr_p))
4363 /* Ensure it does not have side effects, it might contain a reference to
4364 the object we're initializing. */
4365 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4366 return;
4369 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4370 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4371 return;
4373 /* Recurse for nested constructors. */
4374 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4376 unsigned HOST_WIDE_INT ix;
4377 constructor_elt *ce;
4378 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4380 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4381 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4383 return;
4386 /* If this is a variable sized type, we must remember the size. */
4387 maybe_with_size_expr (expr_p);
4389 /* Gimplify the constructor element to something appropriate for the rhs
4390 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4391 the gimplifier will consider this a store to memory. Doing this
4392 gimplification now means that we won't have to deal with complicated
4393 language-specific trees, nor trees like SAVE_EXPR that can induce
4394 exponential search behavior. */
4395 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4396 if (one == GS_ERROR)
4398 *expr_p = NULL;
4399 return;
4402 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4403 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4404 always be true for all scalars, since is_gimple_mem_rhs insists on a
4405 temporary variable for them. */
4406 if (DECL_P (*expr_p))
4407 return;
4409 /* If this is of variable size, we have no choice but to assume it doesn't
4410 overlap since we can't make a temporary for it. */
4411 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4412 return;
4414 /* Otherwise, we must search for overlap ... */
4415 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4416 return;
4418 /* ... and if found, force the value into a temporary. */
4419 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4422 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4423 a RANGE_EXPR in a CONSTRUCTOR for an array.
4425 var = lower;
4426 loop_entry:
4427 object[var] = value;
4428 if (var == upper)
4429 goto loop_exit;
4430 var = var + 1;
4431 goto loop_entry;
4432 loop_exit:
4434 We increment var _after_ the loop exit check because we might otherwise
4435 fail if upper == TYPE_MAX_VALUE (type for upper).
4437 Note that we never have to deal with SAVE_EXPRs here, because this has
4438 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4440 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4441 gimple_seq *, bool);
4443 static void
4444 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4445 tree value, tree array_elt_type,
4446 gimple_seq *pre_p, bool cleared)
4448 tree loop_entry_label, loop_exit_label, fall_thru_label;
4449 tree var, var_type, cref, tmp;
4451 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4452 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4453 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4455 /* Create and initialize the index variable. */
4456 var_type = TREE_TYPE (upper);
4457 var = create_tmp_var (var_type);
4458 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4460 /* Add the loop entry label. */
4461 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4463 /* Build the reference. */
4464 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4465 var, NULL_TREE, NULL_TREE);
4467 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4468 the store. Otherwise just assign value to the reference. */
4470 if (TREE_CODE (value) == CONSTRUCTOR)
4471 /* NB we might have to call ourself recursively through
4472 gimplify_init_ctor_eval if the value is a constructor. */
4473 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4474 pre_p, cleared);
4475 else
4476 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4478 /* We exit the loop when the index var is equal to the upper bound. */
4479 gimplify_seq_add_stmt (pre_p,
4480 gimple_build_cond (EQ_EXPR, var, upper,
4481 loop_exit_label, fall_thru_label));
4483 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4485 /* Otherwise, increment the index var... */
4486 tmp = build2 (PLUS_EXPR, var_type, var,
4487 fold_convert (var_type, integer_one_node));
4488 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4490 /* ...and jump back to the loop entry. */
4491 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4493 /* Add the loop exit label. */
4494 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4497 /* Return true if FDECL is accessing a field that is zero sized. */
4499 static bool
4500 zero_sized_field_decl (const_tree fdecl)
4502 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4503 && integer_zerop (DECL_SIZE (fdecl)))
4504 return true;
4505 return false;
4508 /* Return true if TYPE is zero sized. */
4510 static bool
4511 zero_sized_type (const_tree type)
4513 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4514 && integer_zerop (TYPE_SIZE (type)))
4515 return true;
4516 return false;
4519 /* A subroutine of gimplify_init_constructor. Generate individual
4520 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4521 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4522 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4523 zeroed first. */
4525 static void
4526 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4527 gimple_seq *pre_p, bool cleared)
4529 tree array_elt_type = NULL;
4530 unsigned HOST_WIDE_INT ix;
4531 tree purpose, value;
4533 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4534 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4536 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4538 tree cref;
4540 /* NULL values are created above for gimplification errors. */
4541 if (value == NULL)
4542 continue;
4544 if (cleared && initializer_zerop (value))
4545 continue;
4547 /* ??? Here's to hoping the front end fills in all of the indices,
4548 so we don't have to figure out what's missing ourselves. */
4549 gcc_assert (purpose);
4551 /* Skip zero-sized fields, unless value has side-effects. This can
4552 happen with calls to functions returning a zero-sized type, which
4553 we shouldn't discard. As a number of downstream passes don't
4554 expect sets of zero-sized fields, we rely on the gimplification of
4555 the MODIFY_EXPR we make below to drop the assignment statement. */
4556 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4557 continue;
4559 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4560 whole range. */
4561 if (TREE_CODE (purpose) == RANGE_EXPR)
4563 tree lower = TREE_OPERAND (purpose, 0);
4564 tree upper = TREE_OPERAND (purpose, 1);
4566 /* If the lower bound is equal to upper, just treat it as if
4567 upper was the index. */
4568 if (simple_cst_equal (lower, upper))
4569 purpose = upper;
4570 else
4572 gimplify_init_ctor_eval_range (object, lower, upper, value,
4573 array_elt_type, pre_p, cleared);
4574 continue;
4578 if (array_elt_type)
4580 /* Do not use bitsizetype for ARRAY_REF indices. */
4581 if (TYPE_DOMAIN (TREE_TYPE (object)))
4582 purpose
4583 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4584 purpose);
4585 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4586 purpose, NULL_TREE, NULL_TREE);
4588 else
4590 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4591 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4592 unshare_expr (object), purpose, NULL_TREE);
4595 if (TREE_CODE (value) == CONSTRUCTOR
4596 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4597 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4598 pre_p, cleared);
4599 else
4601 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4602 gimplify_and_add (init, pre_p);
4603 ggc_free (init);
4608 /* Return the appropriate RHS predicate for this LHS. */
4610 gimple_predicate
4611 rhs_predicate_for (tree lhs)
4613 if (is_gimple_reg (lhs))
4614 return is_gimple_reg_rhs_or_call;
4615 else
4616 return is_gimple_mem_rhs_or_call;
4619 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4620 before the LHS has been gimplified. */
4622 static gimple_predicate
4623 initial_rhs_predicate_for (tree lhs)
4625 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4626 return is_gimple_reg_rhs_or_call;
4627 else
4628 return is_gimple_mem_rhs_or_call;
4631 /* Gimplify a C99 compound literal expression. This just means adding
4632 the DECL_EXPR before the current statement and using its anonymous
4633 decl instead. */
4635 static enum gimplify_status
4636 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4637 bool (*gimple_test_f) (tree),
4638 fallback_t fallback)
4640 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4641 tree decl = DECL_EXPR_DECL (decl_s);
4642 tree init = DECL_INITIAL (decl);
4643 /* Mark the decl as addressable if the compound literal
4644 expression is addressable now, otherwise it is marked too late
4645 after we gimplify the initialization expression. */
4646 if (TREE_ADDRESSABLE (*expr_p))
4647 TREE_ADDRESSABLE (decl) = 1;
4648 /* Otherwise, if we don't need an lvalue and have a literal directly
4649 substitute it. Check if it matches the gimple predicate, as
4650 otherwise we'd generate a new temporary, and we can as well just
4651 use the decl we already have. */
4652 else if (!TREE_ADDRESSABLE (decl)
4653 && init
4654 && (fallback & fb_lvalue) == 0
4655 && gimple_test_f (init))
4657 *expr_p = init;
4658 return GS_OK;
4661 /* Preliminarily mark non-addressed complex variables as eligible
4662 for promotion to gimple registers. We'll transform their uses
4663 as we find them. */
4664 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4665 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4666 && !TREE_THIS_VOLATILE (decl)
4667 && !needs_to_live_in_memory (decl))
4668 DECL_GIMPLE_REG_P (decl) = 1;
4670 /* If the decl is not addressable, then it is being used in some
4671 expression or on the right hand side of a statement, and it can
4672 be put into a readonly data section. */
4673 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4674 TREE_READONLY (decl) = 1;
4676 /* This decl isn't mentioned in the enclosing block, so add it to the
4677 list of temps. FIXME it seems a bit of a kludge to say that
4678 anonymous artificial vars aren't pushed, but everything else is. */
4679 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4680 gimple_add_tmp_var (decl);
4682 gimplify_and_add (decl_s, pre_p);
4683 *expr_p = decl;
4684 return GS_OK;
4687 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4688 return a new CONSTRUCTOR if something changed. */
4690 static tree
4691 optimize_compound_literals_in_ctor (tree orig_ctor)
4693 tree ctor = orig_ctor;
4694 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4695 unsigned int idx, num = vec_safe_length (elts);
4697 for (idx = 0; idx < num; idx++)
4699 tree value = (*elts)[idx].value;
4700 tree newval = value;
4701 if (TREE_CODE (value) == CONSTRUCTOR)
4702 newval = optimize_compound_literals_in_ctor (value);
4703 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4705 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4706 tree decl = DECL_EXPR_DECL (decl_s);
4707 tree init = DECL_INITIAL (decl);
4709 if (!TREE_ADDRESSABLE (value)
4710 && !TREE_ADDRESSABLE (decl)
4711 && init
4712 && TREE_CODE (init) == CONSTRUCTOR)
4713 newval = optimize_compound_literals_in_ctor (init);
4715 if (newval == value)
4716 continue;
4718 if (ctor == orig_ctor)
4720 ctor = copy_node (orig_ctor);
4721 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4722 elts = CONSTRUCTOR_ELTS (ctor);
4724 (*elts)[idx].value = newval;
4726 return ctor;
4729 /* A subroutine of gimplify_modify_expr. Break out elements of a
4730 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4732 Note that we still need to clear any elements that don't have explicit
4733 initializers, so if not all elements are initialized we keep the
4734 original MODIFY_EXPR, we just remove all of the constructor elements.
4736 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4737 GS_ERROR if we would have to create a temporary when gimplifying
4738 this constructor. Otherwise, return GS_OK.
4740 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4742 static enum gimplify_status
4743 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4744 bool want_value, bool notify_temp_creation)
4746 tree object, ctor, type;
4747 enum gimplify_status ret;
4748 vec<constructor_elt, va_gc> *elts;
4750 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4752 if (!notify_temp_creation)
4754 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4755 is_gimple_lvalue, fb_lvalue);
4756 if (ret == GS_ERROR)
4757 return ret;
4760 object = TREE_OPERAND (*expr_p, 0);
4761 ctor = TREE_OPERAND (*expr_p, 1)
4762 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4763 type = TREE_TYPE (ctor);
4764 elts = CONSTRUCTOR_ELTS (ctor);
4765 ret = GS_ALL_DONE;
4767 switch (TREE_CODE (type))
4769 case RECORD_TYPE:
4770 case UNION_TYPE:
4771 case QUAL_UNION_TYPE:
4772 case ARRAY_TYPE:
4774 struct gimplify_init_ctor_preeval_data preeval_data;
4775 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4776 bool cleared, complete_p, valid_const_initializer;
4778 /* Aggregate types must lower constructors to initialization of
4779 individual elements. The exception is that a CONSTRUCTOR node
4780 with no elements indicates zero-initialization of the whole. */
4781 if (vec_safe_is_empty (elts))
4783 if (notify_temp_creation)
4784 return GS_OK;
4785 break;
4788 /* Fetch information about the constructor to direct later processing.
4789 We might want to make static versions of it in various cases, and
4790 can only do so if it known to be a valid constant initializer. */
4791 valid_const_initializer
4792 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4793 &num_ctor_elements, &complete_p);
4795 /* If a const aggregate variable is being initialized, then it
4796 should never be a lose to promote the variable to be static. */
4797 if (valid_const_initializer
4798 && num_nonzero_elements > 1
4799 && TREE_READONLY (object)
4800 && VAR_P (object)
4801 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4803 if (notify_temp_creation)
4804 return GS_ERROR;
4805 DECL_INITIAL (object) = ctor;
4806 TREE_STATIC (object) = 1;
4807 if (!DECL_NAME (object))
4808 DECL_NAME (object) = create_tmp_var_name ("C");
4809 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4811 /* ??? C++ doesn't automatically append a .<number> to the
4812 assembler name, and even when it does, it looks at FE private
4813 data structures to figure out what that number should be,
4814 which are not set for this variable. I suppose this is
4815 important for local statics for inline functions, which aren't
4816 "local" in the object file sense. So in order to get a unique
4817 TU-local symbol, we must invoke the lhd version now. */
4818 lhd_set_decl_assembler_name (object);
4820 *expr_p = NULL_TREE;
4821 break;
4824 /* If there are "lots" of initialized elements, even discounting
4825 those that are not address constants (and thus *must* be
4826 computed at runtime), then partition the constructor into
4827 constant and non-constant parts. Block copy the constant
4828 parts in, then generate code for the non-constant parts. */
4829 /* TODO. There's code in cp/typeck.c to do this. */
4831 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4832 /* store_constructor will ignore the clearing of variable-sized
4833 objects. Initializers for such objects must explicitly set
4834 every field that needs to be set. */
4835 cleared = false;
4836 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4837 /* If the constructor isn't complete, clear the whole object
4838 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4840 ??? This ought not to be needed. For any element not present
4841 in the initializer, we should simply set them to zero. Except
4842 we'd need to *find* the elements that are not present, and that
4843 requires trickery to avoid quadratic compile-time behavior in
4844 large cases or excessive memory use in small cases. */
4845 cleared = true;
4846 else if (num_ctor_elements - num_nonzero_elements
4847 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4848 && num_nonzero_elements < num_ctor_elements / 4)
4849 /* If there are "lots" of zeros, it's more efficient to clear
4850 the memory and then set the nonzero elements. */
4851 cleared = true;
4852 else
4853 cleared = false;
4855 /* If there are "lots" of initialized elements, and all of them
4856 are valid address constants, then the entire initializer can
4857 be dropped to memory, and then memcpy'd out. Don't do this
4858 for sparse arrays, though, as it's more efficient to follow
4859 the standard CONSTRUCTOR behavior of memset followed by
4860 individual element initialization. Also don't do this for small
4861 all-zero initializers (which aren't big enough to merit
4862 clearing), and don't try to make bitwise copies of
4863 TREE_ADDRESSABLE types.
4865 We cannot apply such transformation when compiling chkp static
4866 initializer because creation of initializer image in the memory
4867 will require static initialization of bounds for it. It should
4868 result in another gimplification of similar initializer and we
4869 may fall into infinite loop. */
4870 if (valid_const_initializer
4871 && !(cleared || num_nonzero_elements == 0)
4872 && !TREE_ADDRESSABLE (type)
4873 && (!current_function_decl
4874 || !lookup_attribute ("chkp ctor",
4875 DECL_ATTRIBUTES (current_function_decl))))
4877 HOST_WIDE_INT size = int_size_in_bytes (type);
4878 unsigned int align;
4880 /* ??? We can still get unbounded array types, at least
4881 from the C++ front end. This seems wrong, but attempt
4882 to work around it for now. */
4883 if (size < 0)
4885 size = int_size_in_bytes (TREE_TYPE (object));
4886 if (size >= 0)
4887 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4890 /* Find the maximum alignment we can assume for the object. */
4891 /* ??? Make use of DECL_OFFSET_ALIGN. */
4892 if (DECL_P (object))
4893 align = DECL_ALIGN (object);
4894 else
4895 align = TYPE_ALIGN (type);
4897 /* Do a block move either if the size is so small as to make
4898 each individual move a sub-unit move on average, or if it
4899 is so large as to make individual moves inefficient. */
4900 if (size > 0
4901 && num_nonzero_elements > 1
4902 && (size < num_nonzero_elements
4903 || !can_move_by_pieces (size, align)))
4905 if (notify_temp_creation)
4906 return GS_ERROR;
4908 walk_tree (&ctor, force_labels_r, NULL, NULL);
4909 ctor = tree_output_constant_def (ctor);
4910 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4911 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4912 TREE_OPERAND (*expr_p, 1) = ctor;
4914 /* This is no longer an assignment of a CONSTRUCTOR, but
4915 we still may have processing to do on the LHS. So
4916 pretend we didn't do anything here to let that happen. */
4917 return GS_UNHANDLED;
4921 /* If the target is volatile, we have non-zero elements and more than
4922 one field to assign, initialize the target from a temporary. */
4923 if (TREE_THIS_VOLATILE (object)
4924 && !TREE_ADDRESSABLE (type)
4925 && num_nonzero_elements > 0
4926 && vec_safe_length (elts) > 1)
4928 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4929 TREE_OPERAND (*expr_p, 0) = temp;
4930 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4931 *expr_p,
4932 build2 (MODIFY_EXPR, void_type_node,
4933 object, temp));
4934 return GS_OK;
4937 if (notify_temp_creation)
4938 return GS_OK;
4940 /* If there are nonzero elements and if needed, pre-evaluate to capture
4941 elements overlapping with the lhs into temporaries. We must do this
4942 before clearing to fetch the values before they are zeroed-out. */
4943 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4945 preeval_data.lhs_base_decl = get_base_address (object);
4946 if (!DECL_P (preeval_data.lhs_base_decl))
4947 preeval_data.lhs_base_decl = NULL;
4948 preeval_data.lhs_alias_set = get_alias_set (object);
4950 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4951 pre_p, post_p, &preeval_data);
4954 bool ctor_has_side_effects_p
4955 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4957 if (cleared)
4959 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4960 Note that we still have to gimplify, in order to handle the
4961 case of variable sized types. Avoid shared tree structures. */
4962 CONSTRUCTOR_ELTS (ctor) = NULL;
4963 TREE_SIDE_EFFECTS (ctor) = 0;
4964 object = unshare_expr (object);
4965 gimplify_stmt (expr_p, pre_p);
4968 /* If we have not block cleared the object, or if there are nonzero
4969 elements in the constructor, or if the constructor has side effects,
4970 add assignments to the individual scalar fields of the object. */
4971 if (!cleared
4972 || num_nonzero_elements > 0
4973 || ctor_has_side_effects_p)
4974 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4976 *expr_p = NULL_TREE;
4978 break;
4980 case COMPLEX_TYPE:
4982 tree r, i;
4984 if (notify_temp_creation)
4985 return GS_OK;
4987 /* Extract the real and imaginary parts out of the ctor. */
4988 gcc_assert (elts->length () == 2);
4989 r = (*elts)[0].value;
4990 i = (*elts)[1].value;
4991 if (r == NULL || i == NULL)
4993 tree zero = build_zero_cst (TREE_TYPE (type));
4994 if (r == NULL)
4995 r = zero;
4996 if (i == NULL)
4997 i = zero;
5000 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5001 represent creation of a complex value. */
5002 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5004 ctor = build_complex (type, r, i);
5005 TREE_OPERAND (*expr_p, 1) = ctor;
5007 else
5009 ctor = build2 (COMPLEX_EXPR, type, r, i);
5010 TREE_OPERAND (*expr_p, 1) = ctor;
5011 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5012 pre_p,
5013 post_p,
5014 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5015 fb_rvalue);
5018 break;
5020 case VECTOR_TYPE:
5022 unsigned HOST_WIDE_INT ix;
5023 constructor_elt *ce;
5025 if (notify_temp_creation)
5026 return GS_OK;
5028 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5029 if (TREE_CONSTANT (ctor))
5031 bool constant_p = true;
5032 tree value;
5034 /* Even when ctor is constant, it might contain non-*_CST
5035 elements, such as addresses or trapping values like
5036 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5037 in VECTOR_CST nodes. */
5038 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5039 if (!CONSTANT_CLASS_P (value))
5041 constant_p = false;
5042 break;
5045 if (constant_p)
5047 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5048 break;
5051 TREE_CONSTANT (ctor) = 0;
5054 /* Vector types use CONSTRUCTOR all the way through gimple
5055 compilation as a general initializer. */
5056 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5058 enum gimplify_status tret;
5059 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5060 fb_rvalue);
5061 if (tret == GS_ERROR)
5062 ret = GS_ERROR;
5063 else if (TREE_STATIC (ctor)
5064 && !initializer_constant_valid_p (ce->value,
5065 TREE_TYPE (ce->value)))
5066 TREE_STATIC (ctor) = 0;
5068 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5069 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5071 break;
5073 default:
5074 /* So how did we get a CONSTRUCTOR for a scalar type? */
5075 gcc_unreachable ();
5078 if (ret == GS_ERROR)
5079 return GS_ERROR;
5080 /* If we have gimplified both sides of the initializer but have
5081 not emitted an assignment, do so now. */
5082 if (*expr_p)
5084 tree lhs = TREE_OPERAND (*expr_p, 0);
5085 tree rhs = TREE_OPERAND (*expr_p, 1);
5086 if (want_value && object == lhs)
5087 lhs = unshare_expr (lhs);
5088 gassign *init = gimple_build_assign (lhs, rhs);
5089 gimplify_seq_add_stmt (pre_p, init);
5091 if (want_value)
5093 *expr_p = object;
5094 return GS_OK;
5096 else
5098 *expr_p = NULL;
5099 return GS_ALL_DONE;
5103 /* Given a pointer value OP0, return a simplified version of an
5104 indirection through OP0, or NULL_TREE if no simplification is
5105 possible. This may only be applied to a rhs of an expression.
5106 Note that the resulting type may be different from the type pointed
5107 to in the sense that it is still compatible from the langhooks
5108 point of view. */
5110 static tree
5111 gimple_fold_indirect_ref_rhs (tree t)
5113 return gimple_fold_indirect_ref (t);
5116 /* Subroutine of gimplify_modify_expr to do simplifications of
5117 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5118 something changes. */
5120 static enum gimplify_status
5121 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5122 gimple_seq *pre_p, gimple_seq *post_p,
5123 bool want_value)
5125 enum gimplify_status ret = GS_UNHANDLED;
5126 bool changed;
5130 changed = false;
5131 switch (TREE_CODE (*from_p))
5133 case VAR_DECL:
5134 /* If we're assigning from a read-only variable initialized with
5135 a constructor, do the direct assignment from the constructor,
5136 but only if neither source nor target are volatile since this
5137 latter assignment might end up being done on a per-field basis. */
5138 if (DECL_INITIAL (*from_p)
5139 && TREE_READONLY (*from_p)
5140 && !TREE_THIS_VOLATILE (*from_p)
5141 && !TREE_THIS_VOLATILE (*to_p)
5142 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5144 tree old_from = *from_p;
5145 enum gimplify_status subret;
5147 /* Move the constructor into the RHS. */
5148 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5150 /* Let's see if gimplify_init_constructor will need to put
5151 it in memory. */
5152 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5153 false, true);
5154 if (subret == GS_ERROR)
5156 /* If so, revert the change. */
5157 *from_p = old_from;
5159 else
5161 ret = GS_OK;
5162 changed = true;
5165 break;
5166 case INDIRECT_REF:
5168 /* If we have code like
5170 *(const A*)(A*)&x
5172 where the type of "x" is a (possibly cv-qualified variant
5173 of "A"), treat the entire expression as identical to "x".
5174 This kind of code arises in C++ when an object is bound
5175 to a const reference, and if "x" is a TARGET_EXPR we want
5176 to take advantage of the optimization below. */
5177 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5178 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5179 if (t)
5181 if (TREE_THIS_VOLATILE (t) != volatile_p)
5183 if (DECL_P (t))
5184 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5185 build_fold_addr_expr (t));
5186 if (REFERENCE_CLASS_P (t))
5187 TREE_THIS_VOLATILE (t) = volatile_p;
5189 *from_p = t;
5190 ret = GS_OK;
5191 changed = true;
5193 break;
5196 case TARGET_EXPR:
5198 /* If we are initializing something from a TARGET_EXPR, strip the
5199 TARGET_EXPR and initialize it directly, if possible. This can't
5200 be done if the initializer is void, since that implies that the
5201 temporary is set in some non-trivial way.
5203 ??? What about code that pulls out the temp and uses it
5204 elsewhere? I think that such code never uses the TARGET_EXPR as
5205 an initializer. If I'm wrong, we'll die because the temp won't
5206 have any RTL. In that case, I guess we'll need to replace
5207 references somehow. */
5208 tree init = TARGET_EXPR_INITIAL (*from_p);
5210 if (init
5211 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5212 || !TARGET_EXPR_NO_ELIDE (*from_p))
5213 && !VOID_TYPE_P (TREE_TYPE (init)))
5215 *from_p = init;
5216 ret = GS_OK;
5217 changed = true;
5220 break;
5222 case COMPOUND_EXPR:
5223 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5224 caught. */
5225 gimplify_compound_expr (from_p, pre_p, true);
5226 ret = GS_OK;
5227 changed = true;
5228 break;
5230 case CONSTRUCTOR:
5231 /* If we already made some changes, let the front end have a
5232 crack at this before we break it down. */
5233 if (ret != GS_UNHANDLED)
5234 break;
5235 /* If we're initializing from a CONSTRUCTOR, break this into
5236 individual MODIFY_EXPRs. */
5237 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5238 false);
5240 case COND_EXPR:
5241 /* If we're assigning to a non-register type, push the assignment
5242 down into the branches. This is mandatory for ADDRESSABLE types,
5243 since we cannot generate temporaries for such, but it saves a
5244 copy in other cases as well. */
5245 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5247 /* This code should mirror the code in gimplify_cond_expr. */
5248 enum tree_code code = TREE_CODE (*expr_p);
5249 tree cond = *from_p;
5250 tree result = *to_p;
5252 ret = gimplify_expr (&result, pre_p, post_p,
5253 is_gimple_lvalue, fb_lvalue);
5254 if (ret != GS_ERROR)
5255 ret = GS_OK;
5257 /* If we are going to write RESULT more than once, clear
5258 TREE_READONLY flag, otherwise we might incorrectly promote
5259 the variable to static const and initialize it at compile
5260 time in one of the branches. */
5261 if (VAR_P (result)
5262 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5263 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5264 TREE_READONLY (result) = 0;
5265 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5266 TREE_OPERAND (cond, 1)
5267 = build2 (code, void_type_node, result,
5268 TREE_OPERAND (cond, 1));
5269 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5270 TREE_OPERAND (cond, 2)
5271 = build2 (code, void_type_node, unshare_expr (result),
5272 TREE_OPERAND (cond, 2));
5274 TREE_TYPE (cond) = void_type_node;
5275 recalculate_side_effects (cond);
5277 if (want_value)
5279 gimplify_and_add (cond, pre_p);
5280 *expr_p = unshare_expr (result);
5282 else
5283 *expr_p = cond;
5284 return ret;
5286 break;
5288 case CALL_EXPR:
5289 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5290 return slot so that we don't generate a temporary. */
5291 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5292 && aggregate_value_p (*from_p, *from_p))
5294 bool use_target;
5296 if (!(rhs_predicate_for (*to_p))(*from_p))
5297 /* If we need a temporary, *to_p isn't accurate. */
5298 use_target = false;
5299 /* It's OK to use the return slot directly unless it's an NRV. */
5300 else if (TREE_CODE (*to_p) == RESULT_DECL
5301 && DECL_NAME (*to_p) == NULL_TREE
5302 && needs_to_live_in_memory (*to_p))
5303 use_target = true;
5304 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5305 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5306 /* Don't force regs into memory. */
5307 use_target = false;
5308 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5309 /* It's OK to use the target directly if it's being
5310 initialized. */
5311 use_target = true;
5312 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5313 != INTEGER_CST)
5314 /* Always use the target and thus RSO for variable-sized types.
5315 GIMPLE cannot deal with a variable-sized assignment
5316 embedded in a call statement. */
5317 use_target = true;
5318 else if (TREE_CODE (*to_p) != SSA_NAME
5319 && (!is_gimple_variable (*to_p)
5320 || needs_to_live_in_memory (*to_p)))
5321 /* Don't use the original target if it's already addressable;
5322 if its address escapes, and the called function uses the
5323 NRV optimization, a conforming program could see *to_p
5324 change before the called function returns; see c++/19317.
5325 When optimizing, the return_slot pass marks more functions
5326 as safe after we have escape info. */
5327 use_target = false;
5328 else
5329 use_target = true;
5331 if (use_target)
5333 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5334 mark_addressable (*to_p);
5337 break;
5339 case WITH_SIZE_EXPR:
5340 /* Likewise for calls that return an aggregate of non-constant size,
5341 since we would not be able to generate a temporary at all. */
5342 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5344 *from_p = TREE_OPERAND (*from_p, 0);
5345 /* We don't change ret in this case because the
5346 WITH_SIZE_EXPR might have been added in
5347 gimplify_modify_expr, so returning GS_OK would lead to an
5348 infinite loop. */
5349 changed = true;
5351 break;
5353 /* If we're initializing from a container, push the initialization
5354 inside it. */
5355 case CLEANUP_POINT_EXPR:
5356 case BIND_EXPR:
5357 case STATEMENT_LIST:
5359 tree wrap = *from_p;
5360 tree t;
5362 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5363 fb_lvalue);
5364 if (ret != GS_ERROR)
5365 ret = GS_OK;
5367 t = voidify_wrapper_expr (wrap, *expr_p);
5368 gcc_assert (t == *expr_p);
5370 if (want_value)
5372 gimplify_and_add (wrap, pre_p);
5373 *expr_p = unshare_expr (*to_p);
5375 else
5376 *expr_p = wrap;
5377 return GS_OK;
5380 case COMPOUND_LITERAL_EXPR:
5382 tree complit = TREE_OPERAND (*expr_p, 1);
5383 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5384 tree decl = DECL_EXPR_DECL (decl_s);
5385 tree init = DECL_INITIAL (decl);
5387 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5388 into struct T x = { 0, 1, 2 } if the address of the
5389 compound literal has never been taken. */
5390 if (!TREE_ADDRESSABLE (complit)
5391 && !TREE_ADDRESSABLE (decl)
5392 && init)
5394 *expr_p = copy_node (*expr_p);
5395 TREE_OPERAND (*expr_p, 1) = init;
5396 return GS_OK;
5400 default:
5401 break;
5404 while (changed);
5406 return ret;
5410 /* Return true if T looks like a valid GIMPLE statement. */
5412 static bool
5413 is_gimple_stmt (tree t)
5415 const enum tree_code code = TREE_CODE (t);
5417 switch (code)
5419 case NOP_EXPR:
5420 /* The only valid NOP_EXPR is the empty statement. */
5421 return IS_EMPTY_STMT (t);
5423 case BIND_EXPR:
5424 case COND_EXPR:
5425 /* These are only valid if they're void. */
5426 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5428 case SWITCH_EXPR:
5429 case GOTO_EXPR:
5430 case RETURN_EXPR:
5431 case LABEL_EXPR:
5432 case CASE_LABEL_EXPR:
5433 case TRY_CATCH_EXPR:
5434 case TRY_FINALLY_EXPR:
5435 case EH_FILTER_EXPR:
5436 case CATCH_EXPR:
5437 case ASM_EXPR:
5438 case STATEMENT_LIST:
5439 case OACC_PARALLEL:
5440 case OACC_KERNELS:
5441 case OACC_DATA:
5442 case OACC_HOST_DATA:
5443 case OACC_DECLARE:
5444 case OACC_UPDATE:
5445 case OACC_ENTER_DATA:
5446 case OACC_EXIT_DATA:
5447 case OACC_CACHE:
5448 case OMP_PARALLEL:
5449 case OMP_FOR:
5450 case OMP_SIMD:
5451 case OMP_DISTRIBUTE:
5452 case OACC_LOOP:
5453 case OMP_SECTIONS:
5454 case OMP_SECTION:
5455 case OMP_SINGLE:
5456 case OMP_MASTER:
5457 case OMP_TASKGROUP:
5458 case OMP_ORDERED:
5459 case OMP_CRITICAL:
5460 case OMP_TASK:
5461 case OMP_TARGET:
5462 case OMP_TARGET_DATA:
5463 case OMP_TARGET_UPDATE:
5464 case OMP_TARGET_ENTER_DATA:
5465 case OMP_TARGET_EXIT_DATA:
5466 case OMP_TASKLOOP:
5467 case OMP_TEAMS:
5468 /* These are always void. */
5469 return true;
5471 case CALL_EXPR:
5472 case MODIFY_EXPR:
5473 case PREDICT_EXPR:
5474 /* These are valid regardless of their type. */
5475 return true;
5477 default:
5478 return false;
5483 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5484 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5485 DECL_GIMPLE_REG_P set.
5487 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5488 other, unmodified part of the complex object just before the total store.
5489 As a consequence, if the object is still uninitialized, an undefined value
5490 will be loaded into a register, which may result in a spurious exception
5491 if the register is floating-point and the value happens to be a signaling
5492 NaN for example. Then the fully-fledged complex operations lowering pass
5493 followed by a DCE pass are necessary in order to fix things up. */
5495 static enum gimplify_status
5496 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5497 bool want_value)
5499 enum tree_code code, ocode;
5500 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5502 lhs = TREE_OPERAND (*expr_p, 0);
5503 rhs = TREE_OPERAND (*expr_p, 1);
5504 code = TREE_CODE (lhs);
5505 lhs = TREE_OPERAND (lhs, 0);
5507 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5508 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5509 TREE_NO_WARNING (other) = 1;
5510 other = get_formal_tmp_var (other, pre_p);
5512 realpart = code == REALPART_EXPR ? rhs : other;
5513 imagpart = code == REALPART_EXPR ? other : rhs;
5515 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5516 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5517 else
5518 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5520 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5521 *expr_p = (want_value) ? rhs : NULL_TREE;
5523 return GS_ALL_DONE;
5526 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5528 modify_expr
5529 : varname '=' rhs
5530 | '*' ID '=' rhs
5532 PRE_P points to the list where side effects that must happen before
5533 *EXPR_P should be stored.
5535 POST_P points to the list where side effects that must happen after
5536 *EXPR_P should be stored.
5538 WANT_VALUE is nonzero iff we want to use the value of this expression
5539 in another expression. */
5541 static enum gimplify_status
5542 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5543 bool want_value)
5545 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5546 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5547 enum gimplify_status ret = GS_UNHANDLED;
5548 gimple *assign;
5549 location_t loc = EXPR_LOCATION (*expr_p);
5550 gimple_stmt_iterator gsi;
5552 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5553 || TREE_CODE (*expr_p) == INIT_EXPR);
5555 /* Trying to simplify a clobber using normal logic doesn't work,
5556 so handle it here. */
5557 if (TREE_CLOBBER_P (*from_p))
5559 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5560 if (ret == GS_ERROR)
5561 return ret;
5562 gcc_assert (!want_value
5563 && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5564 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5565 *expr_p = NULL;
5566 return GS_ALL_DONE;
5569 /* Insert pointer conversions required by the middle-end that are not
5570 required by the frontend. This fixes middle-end type checking for
5571 for example gcc.dg/redecl-6.c. */
5572 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5574 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5575 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5576 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5579 /* See if any simplifications can be done based on what the RHS is. */
5580 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5581 want_value);
5582 if (ret != GS_UNHANDLED)
5583 return ret;
5585 /* For zero sized types only gimplify the left hand side and right hand
5586 side as statements and throw away the assignment. Do this after
5587 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5588 types properly. */
5589 if (zero_sized_type (TREE_TYPE (*from_p))
5590 && !want_value
5591 /* Don't do this for calls that return addressable types, expand_call
5592 relies on those having a lhs. */
5593 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5594 && TREE_CODE (*from_p) == CALL_EXPR))
5596 gimplify_stmt (from_p, pre_p);
5597 gimplify_stmt (to_p, pre_p);
5598 *expr_p = NULL_TREE;
5599 return GS_ALL_DONE;
5602 /* If the value being copied is of variable width, compute the length
5603 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5604 before gimplifying any of the operands so that we can resolve any
5605 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5606 the size of the expression to be copied, not of the destination, so
5607 that is what we must do here. */
5608 maybe_with_size_expr (from_p);
5610 /* As a special case, we have to temporarily allow for assignments
5611 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5612 a toplevel statement, when gimplifying the GENERIC expression
5613 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5614 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5616 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5617 prevent gimplify_expr from trying to create a new temporary for
5618 foo's LHS, we tell it that it should only gimplify until it
5619 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5620 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5621 and all we need to do here is set 'a' to be its LHS. */
5623 /* Gimplify the RHS first for C++17 and bug 71104. */
5624 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5625 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5626 if (ret == GS_ERROR)
5627 return ret;
5629 /* Then gimplify the LHS. */
5630 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5631 twice we have to make sure to gimplify into non-SSA as otherwise
5632 the abnormal edge added later will make those defs not dominate
5633 their uses.
5634 ??? Technically this applies only to the registers used in the
5635 resulting non-register *TO_P. */
5636 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5637 if (saved_into_ssa
5638 && TREE_CODE (*from_p) == CALL_EXPR
5639 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5640 gimplify_ctxp->into_ssa = false;
5641 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5642 gimplify_ctxp->into_ssa = saved_into_ssa;
5643 if (ret == GS_ERROR)
5644 return ret;
5646 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5647 guess for the predicate was wrong. */
5648 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5649 if (final_pred != initial_pred)
5651 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5652 if (ret == GS_ERROR)
5653 return ret;
5656 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5657 size as argument to the call. */
5658 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5660 tree call = TREE_OPERAND (*from_p, 0);
5661 tree vlasize = TREE_OPERAND (*from_p, 1);
5663 if (TREE_CODE (call) == CALL_EXPR
5664 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5666 int nargs = call_expr_nargs (call);
5667 tree type = TREE_TYPE (call);
5668 tree ap = CALL_EXPR_ARG (call, 0);
5669 tree tag = CALL_EXPR_ARG (call, 1);
5670 tree aptag = CALL_EXPR_ARG (call, 2);
5671 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5672 IFN_VA_ARG, type,
5673 nargs + 1, ap, tag,
5674 aptag, vlasize);
5675 TREE_OPERAND (*from_p, 0) = newcall;
5679 /* Now see if the above changed *from_p to something we handle specially. */
5680 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5681 want_value);
5682 if (ret != GS_UNHANDLED)
5683 return ret;
5685 /* If we've got a variable sized assignment between two lvalues (i.e. does
5686 not involve a call), then we can make things a bit more straightforward
5687 by converting the assignment to memcpy or memset. */
5688 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5690 tree from = TREE_OPERAND (*from_p, 0);
5691 tree size = TREE_OPERAND (*from_p, 1);
5693 if (TREE_CODE (from) == CONSTRUCTOR)
5694 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5696 if (is_gimple_addressable (from))
5698 *from_p = from;
5699 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5700 pre_p);
5704 /* Transform partial stores to non-addressable complex variables into
5705 total stores. This allows us to use real instead of virtual operands
5706 for these variables, which improves optimization. */
5707 if ((TREE_CODE (*to_p) == REALPART_EXPR
5708 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5709 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5710 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5712 /* Try to alleviate the effects of the gimplification creating artificial
5713 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5714 make sure not to create DECL_DEBUG_EXPR links across functions. */
5715 if (!gimplify_ctxp->into_ssa
5716 && VAR_P (*from_p)
5717 && DECL_IGNORED_P (*from_p)
5718 && DECL_P (*to_p)
5719 && !DECL_IGNORED_P (*to_p)
5720 && decl_function_context (*to_p) == current_function_decl
5721 && decl_function_context (*from_p) == current_function_decl)
5723 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5724 DECL_NAME (*from_p)
5725 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5726 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5727 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5730 if (want_value && TREE_THIS_VOLATILE (*to_p))
5731 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5733 if (TREE_CODE (*from_p) == CALL_EXPR)
5735 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5736 instead of a GIMPLE_ASSIGN. */
5737 gcall *call_stmt;
5738 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5740 /* Gimplify internal functions created in the FEs. */
5741 int nargs = call_expr_nargs (*from_p), i;
5742 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5743 auto_vec<tree> vargs (nargs);
5745 for (i = 0; i < nargs; i++)
5747 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5748 EXPR_LOCATION (*from_p));
5749 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5751 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5752 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5753 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5755 else
5757 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5758 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5759 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5760 tree fndecl = get_callee_fndecl (*from_p);
5761 if (fndecl
5762 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5763 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5764 && call_expr_nargs (*from_p) == 3)
5765 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5766 CALL_EXPR_ARG (*from_p, 0),
5767 CALL_EXPR_ARG (*from_p, 1),
5768 CALL_EXPR_ARG (*from_p, 2));
5769 else
5771 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5774 notice_special_calls (call_stmt);
5775 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5776 gimple_call_set_lhs (call_stmt, *to_p);
5777 else if (TREE_CODE (*to_p) == SSA_NAME)
5778 /* The above is somewhat premature, avoid ICEing later for a
5779 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5780 ??? This doesn't make it a default-def. */
5781 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5783 assign = call_stmt;
5785 else
5787 assign = gimple_build_assign (*to_p, *from_p);
5788 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5789 if (COMPARISON_CLASS_P (*from_p))
5790 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5793 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5795 /* We should have got an SSA name from the start. */
5796 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5797 || ! gimple_in_ssa_p (cfun));
5800 gimplify_seq_add_stmt (pre_p, assign);
5801 gsi = gsi_last (*pre_p);
5802 maybe_fold_stmt (&gsi);
5804 if (want_value)
5806 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5807 return GS_OK;
5809 else
5810 *expr_p = NULL;
5812 return GS_ALL_DONE;
5815 /* Gimplify a comparison between two variable-sized objects. Do this
5816 with a call to BUILT_IN_MEMCMP. */
5818 static enum gimplify_status
5819 gimplify_variable_sized_compare (tree *expr_p)
5821 location_t loc = EXPR_LOCATION (*expr_p);
5822 tree op0 = TREE_OPERAND (*expr_p, 0);
5823 tree op1 = TREE_OPERAND (*expr_p, 1);
5824 tree t, arg, dest, src, expr;
5826 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5827 arg = unshare_expr (arg);
5828 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5829 src = build_fold_addr_expr_loc (loc, op1);
5830 dest = build_fold_addr_expr_loc (loc, op0);
5831 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5832 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5834 expr
5835 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5836 SET_EXPR_LOCATION (expr, loc);
5837 *expr_p = expr;
5839 return GS_OK;
5842 /* Gimplify a comparison between two aggregate objects of integral scalar
5843 mode as a comparison between the bitwise equivalent scalar values. */
5845 static enum gimplify_status
5846 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5848 location_t loc = EXPR_LOCATION (*expr_p);
5849 tree op0 = TREE_OPERAND (*expr_p, 0);
5850 tree op1 = TREE_OPERAND (*expr_p, 1);
5852 tree type = TREE_TYPE (op0);
5853 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5855 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5856 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5858 *expr_p
5859 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5861 return GS_OK;
5864 /* Gimplify an expression sequence. This function gimplifies each
5865 expression and rewrites the original expression with the last
5866 expression of the sequence in GIMPLE form.
5868 PRE_P points to the list where the side effects for all the
5869 expressions in the sequence will be emitted.
5871 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5873 static enum gimplify_status
5874 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5876 tree t = *expr_p;
5880 tree *sub_p = &TREE_OPERAND (t, 0);
5882 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5883 gimplify_compound_expr (sub_p, pre_p, false);
5884 else
5885 gimplify_stmt (sub_p, pre_p);
5887 t = TREE_OPERAND (t, 1);
5889 while (TREE_CODE (t) == COMPOUND_EXPR);
5891 *expr_p = t;
5892 if (want_value)
5893 return GS_OK;
5894 else
5896 gimplify_stmt (expr_p, pre_p);
5897 return GS_ALL_DONE;
5901 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5902 gimplify. After gimplification, EXPR_P will point to a new temporary
5903 that holds the original value of the SAVE_EXPR node.
5905 PRE_P points to the list where side effects that must happen before
5906 *EXPR_P should be stored. */
5908 static enum gimplify_status
5909 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5911 enum gimplify_status ret = GS_ALL_DONE;
5912 tree val;
5914 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5915 val = TREE_OPERAND (*expr_p, 0);
5917 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5918 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5920 /* The operand may be a void-valued expression. It is
5921 being executed only for its side-effects. */
5922 if (TREE_TYPE (val) == void_type_node)
5924 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5925 is_gimple_stmt, fb_none);
5926 val = NULL;
5928 else
5929 /* The temporary may not be an SSA name as later abnormal and EH
5930 control flow may invalidate use/def domination. When in SSA
5931 form then assume there are no such issues and SAVE_EXPRs only
5932 appear via GENERIC foldings. */
5933 val = get_initialized_tmp_var (val, pre_p, post_p,
5934 gimple_in_ssa_p (cfun));
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)
6618 && !gimplify_omp_ctxp)
6620 tree asan_cleanup = build_asan_poison_call_expr (temp);
6621 if (asan_cleanup)
6623 if (unpoison_empty_seq)
6624 unpoison_it = gsi_start (*pre_p);
6626 asan_poison_variable (temp, false, &unpoison_it,
6627 unpoison_empty_seq);
6628 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6632 if (cleanup)
6633 gimple_push_cleanup (temp, cleanup, false, pre_p);
6635 /* Only expand this once. */
6636 TREE_OPERAND (targ, 3) = init;
6637 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6639 else
6640 /* We should have expanded this before. */
6641 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6643 *expr_p = temp;
6644 return GS_OK;
6647 /* Gimplification of expression trees. */
6649 /* Gimplify an expression which appears at statement context. The
6650 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6651 NULL, a new sequence is allocated.
6653 Return true if we actually added a statement to the queue. */
6655 bool
6656 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6658 gimple_seq_node last;
6660 last = gimple_seq_last (*seq_p);
6661 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6662 return last != gimple_seq_last (*seq_p);
6665 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6666 to CTX. If entries already exist, force them to be some flavor of private.
6667 If there is no enclosing parallel, do nothing. */
6669 void
6670 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6672 splay_tree_node n;
6674 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6675 return;
6679 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6680 if (n != NULL)
6682 if (n->value & GOVD_SHARED)
6683 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6684 else if (n->value & GOVD_MAP)
6685 n->value |= GOVD_MAP_TO_ONLY;
6686 else
6687 return;
6689 else if ((ctx->region_type & ORT_TARGET) != 0)
6691 if (ctx->target_map_scalars_firstprivate)
6692 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6693 else
6694 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6696 else if (ctx->region_type != ORT_WORKSHARE
6697 && ctx->region_type != ORT_SIMD
6698 && ctx->region_type != ORT_ACC
6699 && !(ctx->region_type & ORT_TARGET_DATA))
6700 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6702 ctx = ctx->outer_context;
6704 while (ctx);
6707 /* Similarly for each of the type sizes of TYPE. */
6709 static void
6710 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6712 if (type == NULL || type == error_mark_node)
6713 return;
6714 type = TYPE_MAIN_VARIANT (type);
6716 if (ctx->privatized_types->add (type))
6717 return;
6719 switch (TREE_CODE (type))
6721 case INTEGER_TYPE:
6722 case ENUMERAL_TYPE:
6723 case BOOLEAN_TYPE:
6724 case REAL_TYPE:
6725 case FIXED_POINT_TYPE:
6726 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6727 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6728 break;
6730 case ARRAY_TYPE:
6731 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6732 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6733 break;
6735 case RECORD_TYPE:
6736 case UNION_TYPE:
6737 case QUAL_UNION_TYPE:
6739 tree field;
6740 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6741 if (TREE_CODE (field) == FIELD_DECL)
6743 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6744 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6747 break;
6749 case POINTER_TYPE:
6750 case REFERENCE_TYPE:
6751 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6752 break;
6754 default:
6755 break;
6758 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6759 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6760 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6763 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6765 static void
6766 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6768 splay_tree_node n;
6769 unsigned int nflags;
6770 tree t;
6772 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6773 return;
6775 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6776 there are constructors involved somewhere. Exception is a shared clause,
6777 there is nothing privatized in that case. */
6778 if ((flags & GOVD_SHARED) == 0
6779 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6780 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6781 flags |= GOVD_SEEN;
6783 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6784 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6786 /* We shouldn't be re-adding the decl with the same data
6787 sharing class. */
6788 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6789 nflags = n->value | flags;
6790 /* The only combination of data sharing classes we should see is
6791 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6792 reduction variables to be used in data sharing clauses. */
6793 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6794 || ((nflags & GOVD_DATA_SHARE_CLASS)
6795 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6796 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6797 n->value = nflags;
6798 return;
6801 /* When adding a variable-sized variable, we have to handle all sorts
6802 of additional bits of data: the pointer replacement variable, and
6803 the parameters of the type. */
6804 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6806 /* Add the pointer replacement variable as PRIVATE if the variable
6807 replacement is private, else FIRSTPRIVATE since we'll need the
6808 address of the original variable either for SHARED, or for the
6809 copy into or out of the context. */
6810 if (!(flags & GOVD_LOCAL))
6812 if (flags & GOVD_MAP)
6813 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6814 else if (flags & GOVD_PRIVATE)
6815 nflags = GOVD_PRIVATE;
6816 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6817 && (flags & GOVD_FIRSTPRIVATE))
6818 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6819 else
6820 nflags = GOVD_FIRSTPRIVATE;
6821 nflags |= flags & GOVD_SEEN;
6822 t = DECL_VALUE_EXPR (decl);
6823 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6824 t = TREE_OPERAND (t, 0);
6825 gcc_assert (DECL_P (t));
6826 omp_add_variable (ctx, t, nflags);
6829 /* Add all of the variable and type parameters (which should have
6830 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6831 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6832 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6833 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6835 /* The variable-sized variable itself is never SHARED, only some form
6836 of PRIVATE. The sharing would take place via the pointer variable
6837 which we remapped above. */
6838 if (flags & GOVD_SHARED)
6839 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6840 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6842 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6843 alloca statement we generate for the variable, so make sure it
6844 is available. This isn't automatically needed for the SHARED
6845 case, since we won't be allocating local storage then.
6846 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6847 in this case omp_notice_variable will be called later
6848 on when it is gimplified. */
6849 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6850 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6851 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6853 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6854 && lang_hooks.decls.omp_privatize_by_reference (decl))
6856 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6858 /* Similar to the direct variable sized case above, we'll need the
6859 size of references being privatized. */
6860 if ((flags & GOVD_SHARED) == 0)
6862 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6863 if (DECL_P (t))
6864 omp_notice_variable (ctx, t, true);
6868 if (n != NULL)
6869 n->value |= flags;
6870 else
6871 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6873 /* For reductions clauses in OpenACC loop directives, by default create a
6874 copy clause on the enclosing parallel construct for carrying back the
6875 results. */
6876 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6878 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6879 while (outer_ctx)
6881 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6882 if (n != NULL)
6884 /* Ignore local variables and explicitly declared clauses. */
6885 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6886 break;
6887 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6889 /* According to the OpenACC spec, such a reduction variable
6890 should already have a copy map on a kernels construct,
6891 verify that here. */
6892 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6893 && (n->value & GOVD_MAP));
6895 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6897 /* Remove firstprivate and make it a copy map. */
6898 n->value &= ~GOVD_FIRSTPRIVATE;
6899 n->value |= GOVD_MAP;
6902 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6904 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6905 GOVD_MAP | GOVD_SEEN);
6906 break;
6908 outer_ctx = outer_ctx->outer_context;
6913 /* Notice a threadprivate variable DECL used in OMP context CTX.
6914 This just prints out diagnostics about threadprivate variable uses
6915 in untied tasks. If DECL2 is non-NULL, prevent this warning
6916 on that variable. */
6918 static bool
6919 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6920 tree decl2)
6922 splay_tree_node n;
6923 struct gimplify_omp_ctx *octx;
6925 for (octx = ctx; octx; octx = octx->outer_context)
6926 if ((octx->region_type & ORT_TARGET) != 0)
6928 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6929 if (n == NULL)
6931 error ("threadprivate variable %qE used in target region",
6932 DECL_NAME (decl));
6933 error_at (octx->location, "enclosing target region");
6934 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6936 if (decl2)
6937 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6940 if (ctx->region_type != ORT_UNTIED_TASK)
6941 return false;
6942 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6943 if (n == NULL)
6945 error ("threadprivate variable %qE used in untied task",
6946 DECL_NAME (decl));
6947 error_at (ctx->location, "enclosing task");
6948 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6950 if (decl2)
6951 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6952 return false;
6955 /* Return true if global var DECL is device resident. */
6957 static bool
6958 device_resident_p (tree decl)
6960 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6962 if (!attr)
6963 return false;
6965 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6967 tree c = TREE_VALUE (t);
6968 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6969 return true;
6972 return false;
6975 /* Return true if DECL has an ACC DECLARE attribute. */
6977 static bool
6978 is_oacc_declared (tree decl)
6980 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6981 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6982 return declared != NULL_TREE;
6985 /* Determine outer default flags for DECL mentioned in an OMP region
6986 but not declared in an enclosing clause.
6988 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6989 remapped firstprivate instead of shared. To some extent this is
6990 addressed in omp_firstprivatize_type_sizes, but not
6991 effectively. */
6993 static unsigned
6994 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6995 bool in_code, unsigned flags)
6997 enum omp_clause_default_kind default_kind = ctx->default_kind;
6998 enum omp_clause_default_kind kind;
7000 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7001 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7002 default_kind = kind;
7004 switch (default_kind)
7006 case OMP_CLAUSE_DEFAULT_NONE:
7008 const char *rtype;
7010 if (ctx->region_type & ORT_PARALLEL)
7011 rtype = "parallel";
7012 else if (ctx->region_type & ORT_TASK)
7013 rtype = "task";
7014 else if (ctx->region_type & ORT_TEAMS)
7015 rtype = "teams";
7016 else
7017 gcc_unreachable ();
7019 error ("%qE not specified in enclosing %qs",
7020 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7021 error_at (ctx->location, "enclosing %qs", rtype);
7023 /* FALLTHRU */
7024 case OMP_CLAUSE_DEFAULT_SHARED:
7025 flags |= GOVD_SHARED;
7026 break;
7027 case OMP_CLAUSE_DEFAULT_PRIVATE:
7028 flags |= GOVD_PRIVATE;
7029 break;
7030 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7031 flags |= GOVD_FIRSTPRIVATE;
7032 break;
7033 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7034 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7035 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7036 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7038 omp_notice_variable (octx, decl, in_code);
7039 for (; octx; octx = octx->outer_context)
7041 splay_tree_node n2;
7043 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7044 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7045 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7046 continue;
7047 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7049 flags |= GOVD_FIRSTPRIVATE;
7050 goto found_outer;
7052 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7054 flags |= GOVD_SHARED;
7055 goto found_outer;
7060 if (TREE_CODE (decl) == PARM_DECL
7061 || (!is_global_var (decl)
7062 && DECL_CONTEXT (decl) == current_function_decl))
7063 flags |= GOVD_FIRSTPRIVATE;
7064 else
7065 flags |= GOVD_SHARED;
7066 found_outer:
7067 break;
7069 default:
7070 gcc_unreachable ();
7073 return flags;
7077 /* Determine outer default flags for DECL mentioned in an OACC region
7078 but not declared in an enclosing clause. */
7080 static unsigned
7081 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7083 const char *rkind;
7084 bool on_device = false;
7085 bool declared = is_oacc_declared (decl);
7086 tree type = TREE_TYPE (decl);
7088 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7089 type = TREE_TYPE (type);
7091 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7092 && is_global_var (decl)
7093 && device_resident_p (decl))
7095 on_device = true;
7096 flags |= GOVD_MAP_TO_ONLY;
7099 switch (ctx->region_type)
7101 case ORT_ACC_KERNELS:
7102 rkind = "kernels";
7104 if (AGGREGATE_TYPE_P (type))
7106 /* Aggregates default to 'present_or_copy', or 'present'. */
7107 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7108 flags |= GOVD_MAP;
7109 else
7110 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7112 else
7113 /* Scalars default to 'copy'. */
7114 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7116 break;
7118 case ORT_ACC_PARALLEL:
7119 rkind = "parallel";
7121 if (on_device || declared)
7122 flags |= GOVD_MAP;
7123 else if (AGGREGATE_TYPE_P (type))
7125 /* Aggregates default to 'present_or_copy', or 'present'. */
7126 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7127 flags |= GOVD_MAP;
7128 else
7129 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7131 else
7132 /* Scalars default to 'firstprivate'. */
7133 flags |= GOVD_FIRSTPRIVATE;
7135 break;
7137 default:
7138 gcc_unreachable ();
7141 if (DECL_ARTIFICIAL (decl))
7142 ; /* We can get compiler-generated decls, and should not complain
7143 about them. */
7144 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7146 error ("%qE not specified in enclosing OpenACC %qs construct",
7147 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7148 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7150 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7151 ; /* Handled above. */
7152 else
7153 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7155 return flags;
7158 /* Record the fact that DECL was used within the OMP context CTX.
7159 IN_CODE is true when real code uses DECL, and false when we should
7160 merely emit default(none) errors. Return true if DECL is going to
7161 be remapped and thus DECL shouldn't be gimplified into its
7162 DECL_VALUE_EXPR (if any). */
7164 static bool
7165 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7167 splay_tree_node n;
7168 unsigned flags = in_code ? GOVD_SEEN : 0;
7169 bool ret = false, shared;
7171 if (error_operand_p (decl))
7172 return false;
7174 if (ctx->region_type == ORT_NONE)
7175 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7177 if (is_global_var (decl))
7179 /* Threadprivate variables are predetermined. */
7180 if (DECL_THREAD_LOCAL_P (decl))
7181 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7183 if (DECL_HAS_VALUE_EXPR_P (decl))
7185 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7187 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7188 return omp_notice_threadprivate_variable (ctx, decl, value);
7191 if (gimplify_omp_ctxp->outer_context == NULL
7192 && VAR_P (decl)
7193 && oacc_get_fn_attrib (current_function_decl))
7195 location_t loc = DECL_SOURCE_LOCATION (decl);
7197 if (lookup_attribute ("omp declare target link",
7198 DECL_ATTRIBUTES (decl)))
7200 error_at (loc,
7201 "%qE with %<link%> clause used in %<routine%> function",
7202 DECL_NAME (decl));
7203 return false;
7205 else if (!lookup_attribute ("omp declare target",
7206 DECL_ATTRIBUTES (decl)))
7208 error_at (loc,
7209 "%qE requires a %<declare%> directive for use "
7210 "in a %<routine%> function", DECL_NAME (decl));
7211 return false;
7216 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7217 if ((ctx->region_type & ORT_TARGET) != 0)
7219 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7220 if (n == NULL)
7222 unsigned nflags = flags;
7223 if (ctx->target_map_pointers_as_0len_arrays
7224 || ctx->target_map_scalars_firstprivate)
7226 bool is_declare_target = false;
7227 bool is_scalar = false;
7228 if (is_global_var (decl)
7229 && varpool_node::get_create (decl)->offloadable)
7231 struct gimplify_omp_ctx *octx;
7232 for (octx = ctx->outer_context;
7233 octx; octx = octx->outer_context)
7235 n = splay_tree_lookup (octx->variables,
7236 (splay_tree_key)decl);
7237 if (n
7238 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7239 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7240 break;
7242 is_declare_target = octx == NULL;
7244 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7245 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7246 if (is_declare_target)
7248 else if (ctx->target_map_pointers_as_0len_arrays
7249 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7250 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7251 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7252 == POINTER_TYPE)))
7253 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7254 else if (is_scalar)
7255 nflags |= GOVD_FIRSTPRIVATE;
7258 struct gimplify_omp_ctx *octx = ctx->outer_context;
7259 if ((ctx->region_type & ORT_ACC) && octx)
7261 /* Look in outer OpenACC contexts, to see if there's a
7262 data attribute for this variable. */
7263 omp_notice_variable (octx, decl, in_code);
7265 for (; octx; octx = octx->outer_context)
7267 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7268 break;
7269 splay_tree_node n2
7270 = splay_tree_lookup (octx->variables,
7271 (splay_tree_key) decl);
7272 if (n2)
7274 if (octx->region_type == ORT_ACC_HOST_DATA)
7275 error ("variable %qE declared in enclosing "
7276 "%<host_data%> region", DECL_NAME (decl));
7277 nflags |= GOVD_MAP;
7278 if (octx->region_type == ORT_ACC_DATA
7279 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7280 nflags |= GOVD_MAP_0LEN_ARRAY;
7281 goto found_outer;
7287 tree type = TREE_TYPE (decl);
7289 if (nflags == flags
7290 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7291 && lang_hooks.decls.omp_privatize_by_reference (decl))
7292 type = TREE_TYPE (type);
7293 if (nflags == flags
7294 && !lang_hooks.types.omp_mappable_type (type))
7296 error ("%qD referenced in target region does not have "
7297 "a mappable type", decl);
7298 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7300 else if (nflags == flags)
7302 if ((ctx->region_type & ORT_ACC) != 0)
7303 nflags = oacc_default_clause (ctx, decl, flags);
7304 else
7305 nflags |= GOVD_MAP;
7308 found_outer:
7309 omp_add_variable (ctx, decl, nflags);
7311 else
7313 /* If nothing changed, there's nothing left to do. */
7314 if ((n->value & flags) == flags)
7315 return ret;
7316 flags |= n->value;
7317 n->value = flags;
7319 goto do_outer;
7322 if (n == NULL)
7324 if (ctx->region_type == ORT_WORKSHARE
7325 || ctx->region_type == ORT_SIMD
7326 || ctx->region_type == ORT_ACC
7327 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7328 goto do_outer;
7330 flags = omp_default_clause (ctx, decl, in_code, flags);
7332 if ((flags & GOVD_PRIVATE)
7333 && lang_hooks.decls.omp_private_outer_ref (decl))
7334 flags |= GOVD_PRIVATE_OUTER_REF;
7336 omp_add_variable (ctx, decl, flags);
7338 shared = (flags & GOVD_SHARED) != 0;
7339 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7340 goto do_outer;
7343 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7344 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7345 && DECL_SIZE (decl))
7347 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7349 splay_tree_node n2;
7350 tree t = DECL_VALUE_EXPR (decl);
7351 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7352 t = TREE_OPERAND (t, 0);
7353 gcc_assert (DECL_P (t));
7354 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7355 n2->value |= GOVD_SEEN;
7357 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7358 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7359 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7360 != INTEGER_CST))
7362 splay_tree_node n2;
7363 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7364 gcc_assert (DECL_P (t));
7365 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7366 if (n2)
7367 omp_notice_variable (ctx, t, true);
7371 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7372 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7374 /* If nothing changed, there's nothing left to do. */
7375 if ((n->value & flags) == flags)
7376 return ret;
7377 flags |= n->value;
7378 n->value = flags;
7380 do_outer:
7381 /* If the variable is private in the current context, then we don't
7382 need to propagate anything to an outer context. */
7383 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7384 return ret;
7385 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7386 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7387 return ret;
7388 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7389 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7390 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7391 return ret;
7392 if (ctx->outer_context
7393 && omp_notice_variable (ctx->outer_context, decl, in_code))
7394 return true;
7395 return ret;
7398 /* Verify that DECL is private within CTX. If there's specific information
7399 to the contrary in the innermost scope, generate an error. */
7401 static bool
7402 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7404 splay_tree_node n;
7406 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7407 if (n != NULL)
7409 if (n->value & GOVD_SHARED)
7411 if (ctx == gimplify_omp_ctxp)
7413 if (simd)
7414 error ("iteration variable %qE is predetermined linear",
7415 DECL_NAME (decl));
7416 else
7417 error ("iteration variable %qE should be private",
7418 DECL_NAME (decl));
7419 n->value = GOVD_PRIVATE;
7420 return true;
7422 else
7423 return false;
7425 else if ((n->value & GOVD_EXPLICIT) != 0
7426 && (ctx == gimplify_omp_ctxp
7427 || (ctx->region_type == ORT_COMBINED_PARALLEL
7428 && gimplify_omp_ctxp->outer_context == ctx)))
7430 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7431 error ("iteration variable %qE should not be firstprivate",
7432 DECL_NAME (decl));
7433 else if ((n->value & GOVD_REDUCTION) != 0)
7434 error ("iteration variable %qE should not be reduction",
7435 DECL_NAME (decl));
7436 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7437 error ("iteration variable %qE should not be linear",
7438 DECL_NAME (decl));
7439 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7440 error ("iteration variable %qE should not be lastprivate",
7441 DECL_NAME (decl));
7442 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7443 error ("iteration variable %qE should not be private",
7444 DECL_NAME (decl));
7445 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7446 error ("iteration variable %qE is predetermined linear",
7447 DECL_NAME (decl));
7449 return (ctx == gimplify_omp_ctxp
7450 || (ctx->region_type == ORT_COMBINED_PARALLEL
7451 && gimplify_omp_ctxp->outer_context == ctx));
7454 if (ctx->region_type != ORT_WORKSHARE
7455 && ctx->region_type != ORT_SIMD
7456 && ctx->region_type != ORT_ACC)
7457 return false;
7458 else if (ctx->outer_context)
7459 return omp_is_private (ctx->outer_context, decl, simd);
7460 return false;
7463 /* Return true if DECL is private within a parallel region
7464 that binds to the current construct's context or in parallel
7465 region's REDUCTION clause. */
7467 static bool
7468 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7470 splay_tree_node n;
7474 ctx = ctx->outer_context;
7475 if (ctx == NULL)
7477 if (is_global_var (decl))
7478 return false;
7480 /* References might be private, but might be shared too,
7481 when checking for copyprivate, assume they might be
7482 private, otherwise assume they might be shared. */
7483 if (copyprivate)
7484 return true;
7486 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7487 return false;
7489 /* Treat C++ privatized non-static data members outside
7490 of the privatization the same. */
7491 if (omp_member_access_dummy_var (decl))
7492 return false;
7494 return true;
7497 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7499 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7500 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7501 continue;
7503 if (n != NULL)
7505 if ((n->value & GOVD_LOCAL) != 0
7506 && omp_member_access_dummy_var (decl))
7507 return false;
7508 return (n->value & GOVD_SHARED) == 0;
7511 while (ctx->region_type == ORT_WORKSHARE
7512 || ctx->region_type == ORT_SIMD
7513 || ctx->region_type == ORT_ACC);
7514 return false;
7517 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7519 static tree
7520 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7522 tree t = *tp;
7524 /* If this node has been visited, unmark it and keep looking. */
7525 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7526 return t;
7528 if (IS_TYPE_OR_DECL_P (t))
7529 *walk_subtrees = 0;
7530 return NULL_TREE;
7533 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7534 and previous omp contexts. */
7536 static void
7537 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7538 enum omp_region_type region_type,
7539 enum tree_code code)
7541 struct gimplify_omp_ctx *ctx, *outer_ctx;
7542 tree c;
7543 hash_map<tree, tree> *struct_map_to_clause = NULL;
7544 tree *prev_list_p = NULL;
7546 ctx = new_omp_context (region_type);
7547 outer_ctx = ctx->outer_context;
7548 if (code == OMP_TARGET)
7550 if (!lang_GNU_Fortran ())
7551 ctx->target_map_pointers_as_0len_arrays = true;
7552 ctx->target_map_scalars_firstprivate = true;
7554 if (!lang_GNU_Fortran ())
7555 switch (code)
7557 case OMP_TARGET:
7558 case OMP_TARGET_DATA:
7559 case OMP_TARGET_ENTER_DATA:
7560 case OMP_TARGET_EXIT_DATA:
7561 case OACC_DECLARE:
7562 case OACC_HOST_DATA:
7563 ctx->target_firstprivatize_array_bases = true;
7564 default:
7565 break;
7568 while ((c = *list_p) != NULL)
7570 bool remove = false;
7571 bool notice_outer = true;
7572 const char *check_non_private = NULL;
7573 unsigned int flags;
7574 tree decl;
7576 switch (OMP_CLAUSE_CODE (c))
7578 case OMP_CLAUSE_PRIVATE:
7579 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7580 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7582 flags |= GOVD_PRIVATE_OUTER_REF;
7583 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7585 else
7586 notice_outer = false;
7587 goto do_add;
7588 case OMP_CLAUSE_SHARED:
7589 flags = GOVD_SHARED | GOVD_EXPLICIT;
7590 goto do_add;
7591 case OMP_CLAUSE_FIRSTPRIVATE:
7592 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7593 check_non_private = "firstprivate";
7594 goto do_add;
7595 case OMP_CLAUSE_LASTPRIVATE:
7596 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7597 check_non_private = "lastprivate";
7598 decl = OMP_CLAUSE_DECL (c);
7599 if (error_operand_p (decl))
7600 goto do_add;
7601 else if (outer_ctx
7602 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7603 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7604 && splay_tree_lookup (outer_ctx->variables,
7605 (splay_tree_key) decl) == NULL)
7607 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7608 if (outer_ctx->outer_context)
7609 omp_notice_variable (outer_ctx->outer_context, decl, true);
7611 else if (outer_ctx
7612 && (outer_ctx->region_type & ORT_TASK) != 0
7613 && outer_ctx->combined_loop
7614 && splay_tree_lookup (outer_ctx->variables,
7615 (splay_tree_key) decl) == NULL)
7617 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7618 if (outer_ctx->outer_context)
7619 omp_notice_variable (outer_ctx->outer_context, decl, true);
7621 else if (outer_ctx
7622 && (outer_ctx->region_type == ORT_WORKSHARE
7623 || outer_ctx->region_type == ORT_ACC)
7624 && outer_ctx->combined_loop
7625 && splay_tree_lookup (outer_ctx->variables,
7626 (splay_tree_key) decl) == NULL
7627 && !omp_check_private (outer_ctx, decl, false))
7629 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7630 if (outer_ctx->outer_context
7631 && (outer_ctx->outer_context->region_type
7632 == ORT_COMBINED_PARALLEL)
7633 && splay_tree_lookup (outer_ctx->outer_context->variables,
7634 (splay_tree_key) decl) == NULL)
7636 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7637 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7638 if (octx->outer_context)
7640 octx = octx->outer_context;
7641 if (octx->region_type == ORT_WORKSHARE
7642 && octx->combined_loop
7643 && splay_tree_lookup (octx->variables,
7644 (splay_tree_key) decl) == NULL
7645 && !omp_check_private (octx, decl, false))
7647 omp_add_variable (octx, decl,
7648 GOVD_LASTPRIVATE | GOVD_SEEN);
7649 octx = octx->outer_context;
7650 if (octx
7651 && octx->region_type == ORT_COMBINED_TEAMS
7652 && (splay_tree_lookup (octx->variables,
7653 (splay_tree_key) decl)
7654 == NULL))
7656 omp_add_variable (octx, decl,
7657 GOVD_SHARED | GOVD_SEEN);
7658 octx = octx->outer_context;
7661 if (octx)
7662 omp_notice_variable (octx, decl, true);
7665 else if (outer_ctx->outer_context)
7666 omp_notice_variable (outer_ctx->outer_context, decl, true);
7668 goto do_add;
7669 case OMP_CLAUSE_REDUCTION:
7670 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7671 /* OpenACC permits reductions on private variables. */
7672 if (!(region_type & ORT_ACC))
7673 check_non_private = "reduction";
7674 decl = OMP_CLAUSE_DECL (c);
7675 if (TREE_CODE (decl) == MEM_REF)
7677 tree type = TREE_TYPE (decl);
7678 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7679 NULL, is_gimple_val, fb_rvalue, false)
7680 == GS_ERROR)
7682 remove = true;
7683 break;
7685 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7686 if (DECL_P (v))
7688 omp_firstprivatize_variable (ctx, v);
7689 omp_notice_variable (ctx, v, true);
7691 decl = TREE_OPERAND (decl, 0);
7692 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7694 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7695 NULL, is_gimple_val, fb_rvalue, false)
7696 == GS_ERROR)
7698 remove = true;
7699 break;
7701 v = TREE_OPERAND (decl, 1);
7702 if (DECL_P (v))
7704 omp_firstprivatize_variable (ctx, v);
7705 omp_notice_variable (ctx, v, true);
7707 decl = TREE_OPERAND (decl, 0);
7709 if (TREE_CODE (decl) == ADDR_EXPR
7710 || TREE_CODE (decl) == INDIRECT_REF)
7711 decl = TREE_OPERAND (decl, 0);
7713 goto do_add_decl;
7714 case OMP_CLAUSE_LINEAR:
7715 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7716 is_gimple_val, fb_rvalue) == GS_ERROR)
7718 remove = true;
7719 break;
7721 else
7723 if (code == OMP_SIMD
7724 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7726 struct gimplify_omp_ctx *octx = outer_ctx;
7727 if (octx
7728 && octx->region_type == ORT_WORKSHARE
7729 && octx->combined_loop
7730 && !octx->distribute)
7732 if (octx->outer_context
7733 && (octx->outer_context->region_type
7734 == ORT_COMBINED_PARALLEL))
7735 octx = octx->outer_context->outer_context;
7736 else
7737 octx = octx->outer_context;
7739 if (octx
7740 && octx->region_type == ORT_WORKSHARE
7741 && octx->combined_loop
7742 && octx->distribute)
7744 error_at (OMP_CLAUSE_LOCATION (c),
7745 "%<linear%> clause for variable other than "
7746 "loop iterator specified on construct "
7747 "combined with %<distribute%>");
7748 remove = true;
7749 break;
7752 /* For combined #pragma omp parallel for simd, need to put
7753 lastprivate and perhaps firstprivate too on the
7754 parallel. Similarly for #pragma omp for simd. */
7755 struct gimplify_omp_ctx *octx = outer_ctx;
7756 decl = NULL_TREE;
7759 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7760 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7761 break;
7762 decl = OMP_CLAUSE_DECL (c);
7763 if (error_operand_p (decl))
7765 decl = NULL_TREE;
7766 break;
7768 flags = GOVD_SEEN;
7769 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7770 flags |= GOVD_FIRSTPRIVATE;
7771 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7772 flags |= GOVD_LASTPRIVATE;
7773 if (octx
7774 && octx->region_type == ORT_WORKSHARE
7775 && octx->combined_loop)
7777 if (octx->outer_context
7778 && (octx->outer_context->region_type
7779 == ORT_COMBINED_PARALLEL))
7780 octx = octx->outer_context;
7781 else if (omp_check_private (octx, decl, false))
7782 break;
7784 else if (octx
7785 && (octx->region_type & ORT_TASK) != 0
7786 && octx->combined_loop)
7788 else if (octx
7789 && octx->region_type == ORT_COMBINED_PARALLEL
7790 && ctx->region_type == ORT_WORKSHARE
7791 && octx == outer_ctx)
7792 flags = GOVD_SEEN | GOVD_SHARED;
7793 else if (octx
7794 && octx->region_type == ORT_COMBINED_TEAMS)
7795 flags = GOVD_SEEN | GOVD_SHARED;
7796 else if (octx
7797 && octx->region_type == ORT_COMBINED_TARGET)
7799 flags &= ~GOVD_LASTPRIVATE;
7800 if (flags == GOVD_SEEN)
7801 break;
7803 else
7804 break;
7805 splay_tree_node on
7806 = splay_tree_lookup (octx->variables,
7807 (splay_tree_key) decl);
7808 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7810 octx = NULL;
7811 break;
7813 omp_add_variable (octx, decl, flags);
7814 if (octx->outer_context == NULL)
7815 break;
7816 octx = octx->outer_context;
7818 while (1);
7819 if (octx
7820 && decl
7821 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7822 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7823 omp_notice_variable (octx, decl, true);
7825 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7826 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7827 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7829 notice_outer = false;
7830 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7832 goto do_add;
7834 case OMP_CLAUSE_MAP:
7835 decl = OMP_CLAUSE_DECL (c);
7836 if (error_operand_p (decl))
7837 remove = true;
7838 switch (code)
7840 case OMP_TARGET:
7841 break;
7842 case OACC_DATA:
7843 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7844 break;
7845 /* FALLTHRU */
7846 case OMP_TARGET_DATA:
7847 case OMP_TARGET_ENTER_DATA:
7848 case OMP_TARGET_EXIT_DATA:
7849 case OACC_ENTER_DATA:
7850 case OACC_EXIT_DATA:
7851 case OACC_HOST_DATA:
7852 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7853 || (OMP_CLAUSE_MAP_KIND (c)
7854 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7855 /* For target {,enter ,exit }data only the array slice is
7856 mapped, but not the pointer to it. */
7857 remove = true;
7858 break;
7859 default:
7860 break;
7862 if (remove)
7863 break;
7864 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7866 struct gimplify_omp_ctx *octx;
7867 for (octx = outer_ctx; octx; octx = octx->outer_context)
7869 if (octx->region_type != ORT_ACC_HOST_DATA)
7870 break;
7871 splay_tree_node n2
7872 = splay_tree_lookup (octx->variables,
7873 (splay_tree_key) decl);
7874 if (n2)
7875 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7876 "declared in enclosing %<host_data%> region",
7877 DECL_NAME (decl));
7880 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7881 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7882 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7883 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7884 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7886 remove = true;
7887 break;
7889 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7890 || (OMP_CLAUSE_MAP_KIND (c)
7891 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7892 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7894 OMP_CLAUSE_SIZE (c)
7895 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7896 false);
7897 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7898 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7900 if (!DECL_P (decl))
7902 tree d = decl, *pd;
7903 if (TREE_CODE (d) == ARRAY_REF)
7905 while (TREE_CODE (d) == ARRAY_REF)
7906 d = TREE_OPERAND (d, 0);
7907 if (TREE_CODE (d) == COMPONENT_REF
7908 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7909 decl = d;
7911 pd = &OMP_CLAUSE_DECL (c);
7912 if (d == decl
7913 && TREE_CODE (decl) == INDIRECT_REF
7914 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7915 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7916 == REFERENCE_TYPE))
7918 pd = &TREE_OPERAND (decl, 0);
7919 decl = TREE_OPERAND (decl, 0);
7921 if (TREE_CODE (decl) == COMPONENT_REF)
7923 while (TREE_CODE (decl) == COMPONENT_REF)
7924 decl = TREE_OPERAND (decl, 0);
7925 if (TREE_CODE (decl) == INDIRECT_REF
7926 && DECL_P (TREE_OPERAND (decl, 0))
7927 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7928 == REFERENCE_TYPE))
7929 decl = TREE_OPERAND (decl, 0);
7931 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7932 == GS_ERROR)
7934 remove = true;
7935 break;
7937 if (DECL_P (decl))
7939 if (error_operand_p (decl))
7941 remove = true;
7942 break;
7945 tree stype = TREE_TYPE (decl);
7946 if (TREE_CODE (stype) == REFERENCE_TYPE)
7947 stype = TREE_TYPE (stype);
7948 if (TYPE_SIZE_UNIT (stype) == NULL
7949 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7951 error_at (OMP_CLAUSE_LOCATION (c),
7952 "mapping field %qE of variable length "
7953 "structure", OMP_CLAUSE_DECL (c));
7954 remove = true;
7955 break;
7958 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7960 /* Error recovery. */
7961 if (prev_list_p == NULL)
7963 remove = true;
7964 break;
7966 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7968 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7969 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7971 remove = true;
7972 break;
7977 tree offset;
7978 poly_int64 bitsize, bitpos;
7979 machine_mode mode;
7980 int unsignedp, reversep, volatilep = 0;
7981 tree base = OMP_CLAUSE_DECL (c);
7982 while (TREE_CODE (base) == ARRAY_REF)
7983 base = TREE_OPERAND (base, 0);
7984 if (TREE_CODE (base) == INDIRECT_REF)
7985 base = TREE_OPERAND (base, 0);
7986 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7987 &mode, &unsignedp, &reversep,
7988 &volatilep);
7989 tree orig_base = base;
7990 if ((TREE_CODE (base) == INDIRECT_REF
7991 || (TREE_CODE (base) == MEM_REF
7992 && integer_zerop (TREE_OPERAND (base, 1))))
7993 && DECL_P (TREE_OPERAND (base, 0))
7994 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7995 == REFERENCE_TYPE))
7996 base = TREE_OPERAND (base, 0);
7997 gcc_assert (base == decl
7998 && (offset == NULL_TREE
7999 || poly_int_tree_p (offset)));
8001 splay_tree_node n
8002 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8003 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8004 == GOMP_MAP_ALWAYS_POINTER);
8005 if (n == NULL || (n->value & GOVD_MAP) == 0)
8007 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8008 OMP_CLAUSE_MAP);
8009 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8010 if (orig_base != base)
8011 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8012 else
8013 OMP_CLAUSE_DECL (l) = decl;
8014 OMP_CLAUSE_SIZE (l) = size_int (1);
8015 if (struct_map_to_clause == NULL)
8016 struct_map_to_clause = new hash_map<tree, tree>;
8017 struct_map_to_clause->put (decl, l);
8018 if (ptr)
8020 enum gomp_map_kind mkind
8021 = code == OMP_TARGET_EXIT_DATA
8022 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8023 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8024 OMP_CLAUSE_MAP);
8025 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8026 OMP_CLAUSE_DECL (c2)
8027 = unshare_expr (OMP_CLAUSE_DECL (c));
8028 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8029 OMP_CLAUSE_SIZE (c2)
8030 = TYPE_SIZE_UNIT (ptr_type_node);
8031 OMP_CLAUSE_CHAIN (l) = c2;
8032 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8034 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8035 tree c3
8036 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8037 OMP_CLAUSE_MAP);
8038 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8039 OMP_CLAUSE_DECL (c3)
8040 = unshare_expr (OMP_CLAUSE_DECL (c4));
8041 OMP_CLAUSE_SIZE (c3)
8042 = TYPE_SIZE_UNIT (ptr_type_node);
8043 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8044 OMP_CLAUSE_CHAIN (c2) = c3;
8046 *prev_list_p = l;
8047 prev_list_p = NULL;
8049 else
8051 OMP_CLAUSE_CHAIN (l) = c;
8052 *list_p = l;
8053 list_p = &OMP_CLAUSE_CHAIN (l);
8055 if (orig_base != base && code == OMP_TARGET)
8057 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8058 OMP_CLAUSE_MAP);
8059 enum gomp_map_kind mkind
8060 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8061 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8062 OMP_CLAUSE_DECL (c2) = decl;
8063 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8064 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8065 OMP_CLAUSE_CHAIN (l) = c2;
8067 flags = GOVD_MAP | GOVD_EXPLICIT;
8068 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8069 flags |= GOVD_SEEN;
8070 goto do_add_decl;
8072 else
8074 tree *osc = struct_map_to_clause->get (decl);
8075 tree *sc = NULL, *scp = NULL;
8076 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8077 n->value |= GOVD_SEEN;
8078 poly_offset_int o1, o2;
8079 if (offset)
8080 o1 = wi::to_poly_offset (offset);
8081 else
8082 o1 = 0;
8083 if (maybe_ne (bitpos, 0))
8084 o1 += bits_to_bytes_round_down (bitpos);
8085 sc = &OMP_CLAUSE_CHAIN (*osc);
8086 if (*sc != c
8087 && (OMP_CLAUSE_MAP_KIND (*sc)
8088 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8089 sc = &OMP_CLAUSE_CHAIN (*sc);
8090 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8091 if (ptr && sc == prev_list_p)
8092 break;
8093 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8094 != COMPONENT_REF
8095 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8096 != INDIRECT_REF)
8097 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8098 != ARRAY_REF))
8099 break;
8100 else
8102 tree offset2;
8103 poly_int64 bitsize2, bitpos2;
8104 base = OMP_CLAUSE_DECL (*sc);
8105 if (TREE_CODE (base) == ARRAY_REF)
8107 while (TREE_CODE (base) == ARRAY_REF)
8108 base = TREE_OPERAND (base, 0);
8109 if (TREE_CODE (base) != COMPONENT_REF
8110 || (TREE_CODE (TREE_TYPE (base))
8111 != ARRAY_TYPE))
8112 break;
8114 else if (TREE_CODE (base) == INDIRECT_REF
8115 && (TREE_CODE (TREE_OPERAND (base, 0))
8116 == COMPONENT_REF)
8117 && (TREE_CODE (TREE_TYPE
8118 (TREE_OPERAND (base, 0)))
8119 == REFERENCE_TYPE))
8120 base = TREE_OPERAND (base, 0);
8121 base = get_inner_reference (base, &bitsize2,
8122 &bitpos2, &offset2,
8123 &mode, &unsignedp,
8124 &reversep, &volatilep);
8125 if ((TREE_CODE (base) == INDIRECT_REF
8126 || (TREE_CODE (base) == MEM_REF
8127 && integer_zerop (TREE_OPERAND (base,
8128 1))))
8129 && DECL_P (TREE_OPERAND (base, 0))
8130 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8131 0)))
8132 == REFERENCE_TYPE))
8133 base = TREE_OPERAND (base, 0);
8134 if (base != decl)
8135 break;
8136 if (scp)
8137 continue;
8138 gcc_assert (offset == NULL_TREE
8139 || poly_int_tree_p (offset));
8140 tree d1 = OMP_CLAUSE_DECL (*sc);
8141 tree d2 = OMP_CLAUSE_DECL (c);
8142 while (TREE_CODE (d1) == ARRAY_REF)
8143 d1 = TREE_OPERAND (d1, 0);
8144 while (TREE_CODE (d2) == ARRAY_REF)
8145 d2 = TREE_OPERAND (d2, 0);
8146 if (TREE_CODE (d1) == INDIRECT_REF)
8147 d1 = TREE_OPERAND (d1, 0);
8148 if (TREE_CODE (d2) == INDIRECT_REF)
8149 d2 = TREE_OPERAND (d2, 0);
8150 while (TREE_CODE (d1) == COMPONENT_REF)
8151 if (TREE_CODE (d2) == COMPONENT_REF
8152 && TREE_OPERAND (d1, 1)
8153 == TREE_OPERAND (d2, 1))
8155 d1 = TREE_OPERAND (d1, 0);
8156 d2 = TREE_OPERAND (d2, 0);
8158 else
8159 break;
8160 if (d1 == d2)
8162 error_at (OMP_CLAUSE_LOCATION (c),
8163 "%qE appears more than once in map "
8164 "clauses", OMP_CLAUSE_DECL (c));
8165 remove = true;
8166 break;
8168 if (offset2)
8169 o2 = wi::to_poly_offset (offset2);
8170 else
8171 o2 = 0;
8172 o2 += bits_to_bytes_round_down (bitpos2);
8173 if (maybe_lt (o1, o2)
8174 || (known_eq (o1, 2)
8175 && maybe_lt (bitpos, bitpos2)))
8177 if (ptr)
8178 scp = sc;
8179 else
8180 break;
8183 if (remove)
8184 break;
8185 OMP_CLAUSE_SIZE (*osc)
8186 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8187 size_one_node);
8188 if (ptr)
8190 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8191 OMP_CLAUSE_MAP);
8192 tree cl = NULL_TREE;
8193 enum gomp_map_kind mkind
8194 = code == OMP_TARGET_EXIT_DATA
8195 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8196 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8197 OMP_CLAUSE_DECL (c2)
8198 = unshare_expr (OMP_CLAUSE_DECL (c));
8199 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8200 OMP_CLAUSE_SIZE (c2)
8201 = TYPE_SIZE_UNIT (ptr_type_node);
8202 cl = scp ? *prev_list_p : c2;
8203 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8205 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8206 tree c3
8207 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8208 OMP_CLAUSE_MAP);
8209 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8210 OMP_CLAUSE_DECL (c3)
8211 = unshare_expr (OMP_CLAUSE_DECL (c4));
8212 OMP_CLAUSE_SIZE (c3)
8213 = TYPE_SIZE_UNIT (ptr_type_node);
8214 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8215 if (!scp)
8216 OMP_CLAUSE_CHAIN (c2) = c3;
8217 else
8218 cl = c3;
8220 if (scp)
8221 *scp = c2;
8222 if (sc == prev_list_p)
8224 *sc = cl;
8225 prev_list_p = NULL;
8227 else
8229 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8230 list_p = prev_list_p;
8231 prev_list_p = NULL;
8232 OMP_CLAUSE_CHAIN (c) = *sc;
8233 *sc = cl;
8234 continue;
8237 else if (*sc != c)
8239 *list_p = OMP_CLAUSE_CHAIN (c);
8240 OMP_CLAUSE_CHAIN (c) = *sc;
8241 *sc = c;
8242 continue;
8246 if (!remove
8247 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8248 && OMP_CLAUSE_CHAIN (c)
8249 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8250 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8251 == GOMP_MAP_ALWAYS_POINTER))
8252 prev_list_p = list_p;
8253 break;
8255 flags = GOVD_MAP | GOVD_EXPLICIT;
8256 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8257 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8258 flags |= GOVD_MAP_ALWAYS_TO;
8259 goto do_add;
8261 case OMP_CLAUSE_DEPEND:
8262 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8264 tree deps = OMP_CLAUSE_DECL (c);
8265 while (deps && TREE_CODE (deps) == TREE_LIST)
8267 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8268 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8269 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8270 pre_p, NULL, is_gimple_val, fb_rvalue);
8271 deps = TREE_CHAIN (deps);
8273 break;
8275 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8276 break;
8277 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8279 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8280 NULL, is_gimple_val, fb_rvalue);
8281 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8283 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8285 remove = true;
8286 break;
8288 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8289 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8290 is_gimple_val, fb_rvalue) == GS_ERROR)
8292 remove = true;
8293 break;
8295 break;
8297 case OMP_CLAUSE_TO:
8298 case OMP_CLAUSE_FROM:
8299 case OMP_CLAUSE__CACHE_:
8300 decl = OMP_CLAUSE_DECL (c);
8301 if (error_operand_p (decl))
8303 remove = true;
8304 break;
8306 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8307 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8308 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8309 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8310 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8312 remove = true;
8313 break;
8315 if (!DECL_P (decl))
8317 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8318 NULL, is_gimple_lvalue, fb_lvalue)
8319 == GS_ERROR)
8321 remove = true;
8322 break;
8324 break;
8326 goto do_notice;
8328 case OMP_CLAUSE_USE_DEVICE_PTR:
8329 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8330 goto do_add;
8331 case OMP_CLAUSE_IS_DEVICE_PTR:
8332 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8333 goto do_add;
8335 do_add:
8336 decl = OMP_CLAUSE_DECL (c);
8337 do_add_decl:
8338 if (error_operand_p (decl))
8340 remove = true;
8341 break;
8343 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8345 tree t = omp_member_access_dummy_var (decl);
8346 if (t)
8348 tree v = DECL_VALUE_EXPR (decl);
8349 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8350 if (outer_ctx)
8351 omp_notice_variable (outer_ctx, t, true);
8354 if (code == OACC_DATA
8355 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8356 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8357 flags |= GOVD_MAP_0LEN_ARRAY;
8358 omp_add_variable (ctx, decl, flags);
8359 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8360 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8362 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8363 GOVD_LOCAL | GOVD_SEEN);
8364 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8365 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8366 find_decl_expr,
8367 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8368 NULL) == NULL_TREE)
8369 omp_add_variable (ctx,
8370 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8371 GOVD_LOCAL | GOVD_SEEN);
8372 gimplify_omp_ctxp = ctx;
8373 push_gimplify_context ();
8375 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8376 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8378 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8379 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8380 pop_gimplify_context
8381 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8382 push_gimplify_context ();
8383 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8384 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8385 pop_gimplify_context
8386 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8387 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8388 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8390 gimplify_omp_ctxp = outer_ctx;
8392 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8393 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8395 gimplify_omp_ctxp = ctx;
8396 push_gimplify_context ();
8397 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8399 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8400 NULL, NULL);
8401 TREE_SIDE_EFFECTS (bind) = 1;
8402 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8403 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8405 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8406 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8407 pop_gimplify_context
8408 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8409 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8411 gimplify_omp_ctxp = outer_ctx;
8413 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8414 && OMP_CLAUSE_LINEAR_STMT (c))
8416 gimplify_omp_ctxp = ctx;
8417 push_gimplify_context ();
8418 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8420 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8421 NULL, NULL);
8422 TREE_SIDE_EFFECTS (bind) = 1;
8423 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8424 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8426 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8427 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8428 pop_gimplify_context
8429 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8430 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8432 gimplify_omp_ctxp = outer_ctx;
8434 if (notice_outer)
8435 goto do_notice;
8436 break;
8438 case OMP_CLAUSE_COPYIN:
8439 case OMP_CLAUSE_COPYPRIVATE:
8440 decl = OMP_CLAUSE_DECL (c);
8441 if (error_operand_p (decl))
8443 remove = true;
8444 break;
8446 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8447 && !remove
8448 && !omp_check_private (ctx, decl, true))
8450 remove = true;
8451 if (is_global_var (decl))
8453 if (DECL_THREAD_LOCAL_P (decl))
8454 remove = false;
8455 else if (DECL_HAS_VALUE_EXPR_P (decl))
8457 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8459 if (value
8460 && DECL_P (value)
8461 && DECL_THREAD_LOCAL_P (value))
8462 remove = false;
8465 if (remove)
8466 error_at (OMP_CLAUSE_LOCATION (c),
8467 "copyprivate variable %qE is not threadprivate"
8468 " or private in outer context", DECL_NAME (decl));
8470 do_notice:
8471 if (outer_ctx)
8472 omp_notice_variable (outer_ctx, decl, true);
8473 if (check_non_private
8474 && region_type == ORT_WORKSHARE
8475 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8476 || decl == OMP_CLAUSE_DECL (c)
8477 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8478 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8479 == ADDR_EXPR
8480 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8481 == POINTER_PLUS_EXPR
8482 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8483 (OMP_CLAUSE_DECL (c), 0), 0))
8484 == ADDR_EXPR)))))
8485 && omp_check_private (ctx, decl, false))
8487 error ("%s variable %qE is private in outer context",
8488 check_non_private, DECL_NAME (decl));
8489 remove = true;
8491 break;
8493 case OMP_CLAUSE_IF:
8494 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8495 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8497 const char *p[2];
8498 for (int i = 0; i < 2; i++)
8499 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8501 case OMP_PARALLEL: p[i] = "parallel"; break;
8502 case OMP_TASK: p[i] = "task"; break;
8503 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8504 case OMP_TARGET_DATA: p[i] = "target data"; break;
8505 case OMP_TARGET: p[i] = "target"; break;
8506 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8507 case OMP_TARGET_ENTER_DATA:
8508 p[i] = "target enter data"; break;
8509 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8510 default: gcc_unreachable ();
8512 error_at (OMP_CLAUSE_LOCATION (c),
8513 "expected %qs %<if%> clause modifier rather than %qs",
8514 p[0], p[1]);
8515 remove = true;
8517 /* Fall through. */
8519 case OMP_CLAUSE_FINAL:
8520 OMP_CLAUSE_OPERAND (c, 0)
8521 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8522 /* Fall through. */
8524 case OMP_CLAUSE_SCHEDULE:
8525 case OMP_CLAUSE_NUM_THREADS:
8526 case OMP_CLAUSE_NUM_TEAMS:
8527 case OMP_CLAUSE_THREAD_LIMIT:
8528 case OMP_CLAUSE_DIST_SCHEDULE:
8529 case OMP_CLAUSE_DEVICE:
8530 case OMP_CLAUSE_PRIORITY:
8531 case OMP_CLAUSE_GRAINSIZE:
8532 case OMP_CLAUSE_NUM_TASKS:
8533 case OMP_CLAUSE_HINT:
8534 case OMP_CLAUSE_ASYNC:
8535 case OMP_CLAUSE_WAIT:
8536 case OMP_CLAUSE_NUM_GANGS:
8537 case OMP_CLAUSE_NUM_WORKERS:
8538 case OMP_CLAUSE_VECTOR_LENGTH:
8539 case OMP_CLAUSE_WORKER:
8540 case OMP_CLAUSE_VECTOR:
8541 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8542 is_gimple_val, fb_rvalue) == GS_ERROR)
8543 remove = true;
8544 break;
8546 case OMP_CLAUSE_GANG:
8547 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8548 is_gimple_val, fb_rvalue) == GS_ERROR)
8549 remove = true;
8550 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8551 is_gimple_val, fb_rvalue) == GS_ERROR)
8552 remove = true;
8553 break;
8555 case OMP_CLAUSE_NOWAIT:
8556 case OMP_CLAUSE_ORDERED:
8557 case OMP_CLAUSE_UNTIED:
8558 case OMP_CLAUSE_COLLAPSE:
8559 case OMP_CLAUSE_TILE:
8560 case OMP_CLAUSE_AUTO:
8561 case OMP_CLAUSE_SEQ:
8562 case OMP_CLAUSE_INDEPENDENT:
8563 case OMP_CLAUSE_MERGEABLE:
8564 case OMP_CLAUSE_PROC_BIND:
8565 case OMP_CLAUSE_SAFELEN:
8566 case OMP_CLAUSE_SIMDLEN:
8567 case OMP_CLAUSE_NOGROUP:
8568 case OMP_CLAUSE_THREADS:
8569 case OMP_CLAUSE_SIMD:
8570 break;
8572 case OMP_CLAUSE_DEFAULTMAP:
8573 ctx->target_map_scalars_firstprivate = false;
8574 break;
8576 case OMP_CLAUSE_ALIGNED:
8577 decl = OMP_CLAUSE_DECL (c);
8578 if (error_operand_p (decl))
8580 remove = true;
8581 break;
8583 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8584 is_gimple_val, fb_rvalue) == GS_ERROR)
8586 remove = true;
8587 break;
8589 if (!is_global_var (decl)
8590 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8591 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8592 break;
8594 case OMP_CLAUSE_DEFAULT:
8595 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8596 break;
8598 default:
8599 gcc_unreachable ();
8602 if (code == OACC_DATA
8603 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8604 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8605 remove = true;
8606 if (remove)
8607 *list_p = OMP_CLAUSE_CHAIN (c);
8608 else
8609 list_p = &OMP_CLAUSE_CHAIN (c);
8612 gimplify_omp_ctxp = ctx;
8613 if (struct_map_to_clause)
8614 delete struct_map_to_clause;
8617 /* Return true if DECL is a candidate for shared to firstprivate
8618 optimization. We only consider non-addressable scalars, not
8619 too big, and not references. */
8621 static bool
8622 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8624 if (TREE_ADDRESSABLE (decl))
8625 return false;
8626 tree type = TREE_TYPE (decl);
8627 if (!is_gimple_reg_type (type)
8628 || TREE_CODE (type) == REFERENCE_TYPE
8629 || TREE_ADDRESSABLE (type))
8630 return false;
8631 /* Don't optimize too large decls, as each thread/task will have
8632 its own. */
8633 HOST_WIDE_INT len = int_size_in_bytes (type);
8634 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8635 return false;
8636 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8637 return false;
8638 return true;
8641 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8642 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8643 GOVD_WRITTEN in outer contexts. */
8645 static void
8646 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8648 for (; ctx; ctx = ctx->outer_context)
8650 splay_tree_node n = splay_tree_lookup (ctx->variables,
8651 (splay_tree_key) decl);
8652 if (n == NULL)
8653 continue;
8654 else if (n->value & GOVD_SHARED)
8656 n->value |= GOVD_WRITTEN;
8657 return;
8659 else if (n->value & GOVD_DATA_SHARE_CLASS)
8660 return;
8664 /* Helper callback for walk_gimple_seq to discover possible stores
8665 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8666 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8667 for those. */
8669 static tree
8670 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8672 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8674 *walk_subtrees = 0;
8675 if (!wi->is_lhs)
8676 return NULL_TREE;
8678 tree op = *tp;
8681 if (handled_component_p (op))
8682 op = TREE_OPERAND (op, 0);
8683 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8684 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8685 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8686 else
8687 break;
8689 while (1);
8690 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8691 return NULL_TREE;
8693 omp_mark_stores (gimplify_omp_ctxp, op);
8694 return NULL_TREE;
8697 /* Helper callback for walk_gimple_seq to discover possible stores
8698 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8699 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8700 for those. */
8702 static tree
8703 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8704 bool *handled_ops_p,
8705 struct walk_stmt_info *wi)
8707 gimple *stmt = gsi_stmt (*gsi_p);
8708 switch (gimple_code (stmt))
8710 /* Don't recurse on OpenMP constructs for which
8711 gimplify_adjust_omp_clauses already handled the bodies,
8712 except handle gimple_omp_for_pre_body. */
8713 case GIMPLE_OMP_FOR:
8714 *handled_ops_p = true;
8715 if (gimple_omp_for_pre_body (stmt))
8716 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8717 omp_find_stores_stmt, omp_find_stores_op, wi);
8718 break;
8719 case GIMPLE_OMP_PARALLEL:
8720 case GIMPLE_OMP_TASK:
8721 case GIMPLE_OMP_SECTIONS:
8722 case GIMPLE_OMP_SINGLE:
8723 case GIMPLE_OMP_TARGET:
8724 case GIMPLE_OMP_TEAMS:
8725 case GIMPLE_OMP_CRITICAL:
8726 *handled_ops_p = true;
8727 break;
8728 default:
8729 break;
8731 return NULL_TREE;
8734 struct gimplify_adjust_omp_clauses_data
8736 tree *list_p;
8737 gimple_seq *pre_p;
8740 /* For all variables that were not actually used within the context,
8741 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8743 static int
8744 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8746 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8747 gimple_seq *pre_p
8748 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8749 tree decl = (tree) n->key;
8750 unsigned flags = n->value;
8751 enum omp_clause_code code;
8752 tree clause;
8753 bool private_debug;
8755 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8756 return 0;
8757 if ((flags & GOVD_SEEN) == 0)
8758 return 0;
8759 if (flags & GOVD_DEBUG_PRIVATE)
8761 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8762 private_debug = true;
8764 else if (flags & GOVD_MAP)
8765 private_debug = false;
8766 else
8767 private_debug
8768 = lang_hooks.decls.omp_private_debug_clause (decl,
8769 !!(flags & GOVD_SHARED));
8770 if (private_debug)
8771 code = OMP_CLAUSE_PRIVATE;
8772 else if (flags & GOVD_MAP)
8774 code = OMP_CLAUSE_MAP;
8775 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8776 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8778 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8779 return 0;
8782 else if (flags & GOVD_SHARED)
8784 if (is_global_var (decl))
8786 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8787 while (ctx != NULL)
8789 splay_tree_node on
8790 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8791 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8792 | GOVD_PRIVATE | GOVD_REDUCTION
8793 | GOVD_LINEAR | GOVD_MAP)) != 0)
8794 break;
8795 ctx = ctx->outer_context;
8797 if (ctx == NULL)
8798 return 0;
8800 code = OMP_CLAUSE_SHARED;
8802 else if (flags & GOVD_PRIVATE)
8803 code = OMP_CLAUSE_PRIVATE;
8804 else if (flags & GOVD_FIRSTPRIVATE)
8806 code = OMP_CLAUSE_FIRSTPRIVATE;
8807 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8808 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8809 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8811 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8812 "%<target%> construct", decl);
8813 return 0;
8816 else if (flags & GOVD_LASTPRIVATE)
8817 code = OMP_CLAUSE_LASTPRIVATE;
8818 else if (flags & GOVD_ALIGNED)
8819 return 0;
8820 else
8821 gcc_unreachable ();
8823 if (((flags & GOVD_LASTPRIVATE)
8824 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8825 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8826 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8828 tree chain = *list_p;
8829 clause = build_omp_clause (input_location, code);
8830 OMP_CLAUSE_DECL (clause) = decl;
8831 OMP_CLAUSE_CHAIN (clause) = chain;
8832 if (private_debug)
8833 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8834 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8835 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8836 else if (code == OMP_CLAUSE_SHARED
8837 && (flags & GOVD_WRITTEN) == 0
8838 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8839 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8840 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8841 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8842 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8844 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8845 OMP_CLAUSE_DECL (nc) = decl;
8846 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8847 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8848 OMP_CLAUSE_DECL (clause)
8849 = build_simple_mem_ref_loc (input_location, decl);
8850 OMP_CLAUSE_DECL (clause)
8851 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8852 build_int_cst (build_pointer_type (char_type_node), 0));
8853 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8854 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8855 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8856 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8857 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8858 OMP_CLAUSE_CHAIN (nc) = chain;
8859 OMP_CLAUSE_CHAIN (clause) = nc;
8860 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8861 gimplify_omp_ctxp = ctx->outer_context;
8862 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8863 pre_p, NULL, is_gimple_val, fb_rvalue);
8864 gimplify_omp_ctxp = ctx;
8866 else if (code == OMP_CLAUSE_MAP)
8868 int kind;
8869 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8870 switch (flags & (GOVD_MAP_TO_ONLY
8871 | GOVD_MAP_FORCE
8872 | GOVD_MAP_FORCE_PRESENT))
8874 case 0:
8875 kind = GOMP_MAP_TOFROM;
8876 break;
8877 case GOVD_MAP_FORCE:
8878 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8879 break;
8880 case GOVD_MAP_TO_ONLY:
8881 kind = GOMP_MAP_TO;
8882 break;
8883 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8884 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8885 break;
8886 case GOVD_MAP_FORCE_PRESENT:
8887 kind = GOMP_MAP_FORCE_PRESENT;
8888 break;
8889 default:
8890 gcc_unreachable ();
8892 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8893 if (DECL_SIZE (decl)
8894 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8896 tree decl2 = DECL_VALUE_EXPR (decl);
8897 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8898 decl2 = TREE_OPERAND (decl2, 0);
8899 gcc_assert (DECL_P (decl2));
8900 tree mem = build_simple_mem_ref (decl2);
8901 OMP_CLAUSE_DECL (clause) = mem;
8902 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8903 if (gimplify_omp_ctxp->outer_context)
8905 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8906 omp_notice_variable (ctx, decl2, true);
8907 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8909 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8910 OMP_CLAUSE_MAP);
8911 OMP_CLAUSE_DECL (nc) = decl;
8912 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8913 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8914 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8915 else
8916 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8917 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8918 OMP_CLAUSE_CHAIN (clause) = nc;
8920 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8921 && lang_hooks.decls.omp_privatize_by_reference (decl))
8923 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8924 OMP_CLAUSE_SIZE (clause)
8925 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8926 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8927 gimplify_omp_ctxp = ctx->outer_context;
8928 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8929 pre_p, NULL, is_gimple_val, fb_rvalue);
8930 gimplify_omp_ctxp = ctx;
8931 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8932 OMP_CLAUSE_MAP);
8933 OMP_CLAUSE_DECL (nc) = decl;
8934 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8935 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8936 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8937 OMP_CLAUSE_CHAIN (clause) = nc;
8939 else
8940 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8942 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8944 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8945 OMP_CLAUSE_DECL (nc) = decl;
8946 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8947 OMP_CLAUSE_CHAIN (nc) = chain;
8948 OMP_CLAUSE_CHAIN (clause) = nc;
8949 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8950 gimplify_omp_ctxp = ctx->outer_context;
8951 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8952 gimplify_omp_ctxp = ctx;
8954 *list_p = clause;
8955 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8956 gimplify_omp_ctxp = ctx->outer_context;
8957 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8958 if (gimplify_omp_ctxp)
8959 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8960 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8961 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8962 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8963 true);
8964 gimplify_omp_ctxp = ctx;
8965 return 0;
8968 static void
8969 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8970 enum tree_code code)
8972 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8973 tree c, decl;
8975 if (body)
8977 struct gimplify_omp_ctx *octx;
8978 for (octx = ctx; octx; octx = octx->outer_context)
8979 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8980 break;
8981 if (octx)
8983 struct walk_stmt_info wi;
8984 memset (&wi, 0, sizeof (wi));
8985 walk_gimple_seq (body, omp_find_stores_stmt,
8986 omp_find_stores_op, &wi);
8989 while ((c = *list_p) != NULL)
8991 splay_tree_node n;
8992 bool remove = false;
8994 switch (OMP_CLAUSE_CODE (c))
8996 case OMP_CLAUSE_FIRSTPRIVATE:
8997 if ((ctx->region_type & ORT_TARGET)
8998 && (ctx->region_type & ORT_ACC) == 0
8999 && TYPE_ATOMIC (strip_array_types
9000 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9002 error_at (OMP_CLAUSE_LOCATION (c),
9003 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9004 "%<target%> construct", OMP_CLAUSE_DECL (c));
9005 remove = true;
9006 break;
9008 /* FALLTHRU */
9009 case OMP_CLAUSE_PRIVATE:
9010 case OMP_CLAUSE_SHARED:
9011 case OMP_CLAUSE_LINEAR:
9012 decl = OMP_CLAUSE_DECL (c);
9013 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9014 remove = !(n->value & GOVD_SEEN);
9015 if (! remove)
9017 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9018 if ((n->value & GOVD_DEBUG_PRIVATE)
9019 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9021 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9022 || ((n->value & GOVD_DATA_SHARE_CLASS)
9023 == GOVD_SHARED));
9024 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9025 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9027 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9028 && (n->value & GOVD_WRITTEN) == 0
9029 && DECL_P (decl)
9030 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9031 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9032 else if (DECL_P (decl)
9033 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9034 && (n->value & GOVD_WRITTEN) != 0)
9035 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9036 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9037 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9038 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9040 break;
9042 case OMP_CLAUSE_LASTPRIVATE:
9043 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9044 accurately reflect the presence of a FIRSTPRIVATE clause. */
9045 decl = OMP_CLAUSE_DECL (c);
9046 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9047 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9048 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9049 if (code == OMP_DISTRIBUTE
9050 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9052 remove = true;
9053 error_at (OMP_CLAUSE_LOCATION (c),
9054 "same variable used in %<firstprivate%> and "
9055 "%<lastprivate%> clauses on %<distribute%> "
9056 "construct");
9058 if (!remove
9059 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9060 && DECL_P (decl)
9061 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9062 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9063 break;
9065 case OMP_CLAUSE_ALIGNED:
9066 decl = OMP_CLAUSE_DECL (c);
9067 if (!is_global_var (decl))
9069 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9070 remove = n == NULL || !(n->value & GOVD_SEEN);
9071 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9073 struct gimplify_omp_ctx *octx;
9074 if (n != NULL
9075 && (n->value & (GOVD_DATA_SHARE_CLASS
9076 & ~GOVD_FIRSTPRIVATE)))
9077 remove = true;
9078 else
9079 for (octx = ctx->outer_context; octx;
9080 octx = octx->outer_context)
9082 n = splay_tree_lookup (octx->variables,
9083 (splay_tree_key) decl);
9084 if (n == NULL)
9085 continue;
9086 if (n->value & GOVD_LOCAL)
9087 break;
9088 /* We have to avoid assigning a shared variable
9089 to itself when trying to add
9090 __builtin_assume_aligned. */
9091 if (n->value & GOVD_SHARED)
9093 remove = true;
9094 break;
9099 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9101 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9102 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9103 remove = true;
9105 break;
9107 case OMP_CLAUSE_MAP:
9108 if (code == OMP_TARGET_EXIT_DATA
9109 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9111 remove = true;
9112 break;
9114 decl = OMP_CLAUSE_DECL (c);
9115 /* Data clauses associated with acc parallel reductions must be
9116 compatible with present_or_copy. Warn and adjust the clause
9117 if that is not the case. */
9118 if (ctx->region_type == ORT_ACC_PARALLEL)
9120 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9121 n = NULL;
9123 if (DECL_P (t))
9124 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9126 if (n && (n->value & GOVD_REDUCTION))
9128 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9130 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9131 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9132 && kind != GOMP_MAP_FORCE_PRESENT
9133 && kind != GOMP_MAP_POINTER)
9135 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9136 "incompatible data clause with reduction "
9137 "on %qE; promoting to present_or_copy",
9138 DECL_NAME (t));
9139 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9143 if (!DECL_P (decl))
9145 if ((ctx->region_type & ORT_TARGET) != 0
9146 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9148 if (TREE_CODE (decl) == INDIRECT_REF
9149 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9150 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9151 == REFERENCE_TYPE))
9152 decl = TREE_OPERAND (decl, 0);
9153 if (TREE_CODE (decl) == COMPONENT_REF)
9155 while (TREE_CODE (decl) == COMPONENT_REF)
9156 decl = TREE_OPERAND (decl, 0);
9157 if (DECL_P (decl))
9159 n = splay_tree_lookup (ctx->variables,
9160 (splay_tree_key) decl);
9161 if (!(n->value & GOVD_SEEN))
9162 remove = true;
9166 break;
9168 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9169 if ((ctx->region_type & ORT_TARGET) != 0
9170 && !(n->value & GOVD_SEEN)
9171 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9172 && (!is_global_var (decl)
9173 || !lookup_attribute ("omp declare target link",
9174 DECL_ATTRIBUTES (decl))))
9176 remove = true;
9177 /* For struct element mapping, if struct is never referenced
9178 in target block and none of the mapping has always modifier,
9179 remove all the struct element mappings, which immediately
9180 follow the GOMP_MAP_STRUCT map clause. */
9181 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9183 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9184 while (cnt--)
9185 OMP_CLAUSE_CHAIN (c)
9186 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9189 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9190 && code == OMP_TARGET_EXIT_DATA)
9191 remove = true;
9192 else if (DECL_SIZE (decl)
9193 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9194 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9195 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9196 && (OMP_CLAUSE_MAP_KIND (c)
9197 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9199 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9200 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9201 INTEGER_CST. */
9202 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9204 tree decl2 = DECL_VALUE_EXPR (decl);
9205 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9206 decl2 = TREE_OPERAND (decl2, 0);
9207 gcc_assert (DECL_P (decl2));
9208 tree mem = build_simple_mem_ref (decl2);
9209 OMP_CLAUSE_DECL (c) = mem;
9210 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9211 if (ctx->outer_context)
9213 omp_notice_variable (ctx->outer_context, decl2, true);
9214 omp_notice_variable (ctx->outer_context,
9215 OMP_CLAUSE_SIZE (c), true);
9217 if (((ctx->region_type & ORT_TARGET) != 0
9218 || !ctx->target_firstprivatize_array_bases)
9219 && ((n->value & GOVD_SEEN) == 0
9220 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9222 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9223 OMP_CLAUSE_MAP);
9224 OMP_CLAUSE_DECL (nc) = decl;
9225 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9226 if (ctx->target_firstprivatize_array_bases)
9227 OMP_CLAUSE_SET_MAP_KIND (nc,
9228 GOMP_MAP_FIRSTPRIVATE_POINTER);
9229 else
9230 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9231 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9232 OMP_CLAUSE_CHAIN (c) = nc;
9233 c = nc;
9236 else
9238 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9239 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9240 gcc_assert ((n->value & GOVD_SEEN) == 0
9241 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9242 == 0));
9244 break;
9246 case OMP_CLAUSE_TO:
9247 case OMP_CLAUSE_FROM:
9248 case OMP_CLAUSE__CACHE_:
9249 decl = OMP_CLAUSE_DECL (c);
9250 if (!DECL_P (decl))
9251 break;
9252 if (DECL_SIZE (decl)
9253 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9255 tree decl2 = DECL_VALUE_EXPR (decl);
9256 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9257 decl2 = TREE_OPERAND (decl2, 0);
9258 gcc_assert (DECL_P (decl2));
9259 tree mem = build_simple_mem_ref (decl2);
9260 OMP_CLAUSE_DECL (c) = mem;
9261 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9262 if (ctx->outer_context)
9264 omp_notice_variable (ctx->outer_context, decl2, true);
9265 omp_notice_variable (ctx->outer_context,
9266 OMP_CLAUSE_SIZE (c), true);
9269 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9270 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9271 break;
9273 case OMP_CLAUSE_REDUCTION:
9274 decl = OMP_CLAUSE_DECL (c);
9275 /* OpenACC reductions need a present_or_copy data clause.
9276 Add one if necessary. Error is the reduction is private. */
9277 if (ctx->region_type == ORT_ACC_PARALLEL)
9279 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9280 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9281 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9282 "reduction on %qE", DECL_NAME (decl));
9283 else if ((n->value & GOVD_MAP) == 0)
9285 tree next = OMP_CLAUSE_CHAIN (c);
9286 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9287 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9288 OMP_CLAUSE_DECL (nc) = decl;
9289 OMP_CLAUSE_CHAIN (c) = nc;
9290 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9291 while (1)
9293 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9294 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9295 break;
9296 nc = OMP_CLAUSE_CHAIN (nc);
9298 OMP_CLAUSE_CHAIN (nc) = next;
9299 n->value |= GOVD_MAP;
9302 if (DECL_P (decl)
9303 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9304 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9305 break;
9306 case OMP_CLAUSE_COPYIN:
9307 case OMP_CLAUSE_COPYPRIVATE:
9308 case OMP_CLAUSE_IF:
9309 case OMP_CLAUSE_NUM_THREADS:
9310 case OMP_CLAUSE_NUM_TEAMS:
9311 case OMP_CLAUSE_THREAD_LIMIT:
9312 case OMP_CLAUSE_DIST_SCHEDULE:
9313 case OMP_CLAUSE_DEVICE:
9314 case OMP_CLAUSE_SCHEDULE:
9315 case OMP_CLAUSE_NOWAIT:
9316 case OMP_CLAUSE_ORDERED:
9317 case OMP_CLAUSE_DEFAULT:
9318 case OMP_CLAUSE_UNTIED:
9319 case OMP_CLAUSE_COLLAPSE:
9320 case OMP_CLAUSE_FINAL:
9321 case OMP_CLAUSE_MERGEABLE:
9322 case OMP_CLAUSE_PROC_BIND:
9323 case OMP_CLAUSE_SAFELEN:
9324 case OMP_CLAUSE_SIMDLEN:
9325 case OMP_CLAUSE_DEPEND:
9326 case OMP_CLAUSE_PRIORITY:
9327 case OMP_CLAUSE_GRAINSIZE:
9328 case OMP_CLAUSE_NUM_TASKS:
9329 case OMP_CLAUSE_NOGROUP:
9330 case OMP_CLAUSE_THREADS:
9331 case OMP_CLAUSE_SIMD:
9332 case OMP_CLAUSE_HINT:
9333 case OMP_CLAUSE_DEFAULTMAP:
9334 case OMP_CLAUSE_USE_DEVICE_PTR:
9335 case OMP_CLAUSE_IS_DEVICE_PTR:
9336 case OMP_CLAUSE_ASYNC:
9337 case OMP_CLAUSE_WAIT:
9338 case OMP_CLAUSE_INDEPENDENT:
9339 case OMP_CLAUSE_NUM_GANGS:
9340 case OMP_CLAUSE_NUM_WORKERS:
9341 case OMP_CLAUSE_VECTOR_LENGTH:
9342 case OMP_CLAUSE_GANG:
9343 case OMP_CLAUSE_WORKER:
9344 case OMP_CLAUSE_VECTOR:
9345 case OMP_CLAUSE_AUTO:
9346 case OMP_CLAUSE_SEQ:
9347 case OMP_CLAUSE_TILE:
9348 break;
9350 default:
9351 gcc_unreachable ();
9354 if (remove)
9355 *list_p = OMP_CLAUSE_CHAIN (c);
9356 else
9357 list_p = &OMP_CLAUSE_CHAIN (c);
9360 /* Add in any implicit data sharing. */
9361 struct gimplify_adjust_omp_clauses_data data;
9362 data.list_p = list_p;
9363 data.pre_p = pre_p;
9364 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9366 gimplify_omp_ctxp = ctx->outer_context;
9367 delete_omp_context (ctx);
9370 /* Gimplify OACC_CACHE. */
9372 static void
9373 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9375 tree expr = *expr_p;
9377 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9378 OACC_CACHE);
9379 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9380 OACC_CACHE);
9382 /* TODO: Do something sensible with this information. */
9384 *expr_p = NULL_TREE;
9387 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9388 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9389 kind. The entry kind will replace the one in CLAUSE, while the exit
9390 kind will be used in a new omp_clause and returned to the caller. */
9392 static tree
9393 gimplify_oacc_declare_1 (tree clause)
9395 HOST_WIDE_INT kind, new_op;
9396 bool ret = false;
9397 tree c = NULL;
9399 kind = OMP_CLAUSE_MAP_KIND (clause);
9401 switch (kind)
9403 case GOMP_MAP_ALLOC:
9404 case GOMP_MAP_FORCE_ALLOC:
9405 case GOMP_MAP_FORCE_TO:
9406 new_op = GOMP_MAP_DELETE;
9407 ret = true;
9408 break;
9410 case GOMP_MAP_FORCE_FROM:
9411 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9412 new_op = GOMP_MAP_FORCE_FROM;
9413 ret = true;
9414 break;
9416 case GOMP_MAP_FORCE_TOFROM:
9417 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9418 new_op = GOMP_MAP_FORCE_FROM;
9419 ret = true;
9420 break;
9422 case GOMP_MAP_FROM:
9423 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9424 new_op = GOMP_MAP_FROM;
9425 ret = true;
9426 break;
9428 case GOMP_MAP_TOFROM:
9429 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9430 new_op = GOMP_MAP_FROM;
9431 ret = true;
9432 break;
9434 case GOMP_MAP_DEVICE_RESIDENT:
9435 case GOMP_MAP_FORCE_DEVICEPTR:
9436 case GOMP_MAP_FORCE_PRESENT:
9437 case GOMP_MAP_LINK:
9438 case GOMP_MAP_POINTER:
9439 case GOMP_MAP_TO:
9440 break;
9442 default:
9443 gcc_unreachable ();
9444 break;
9447 if (ret)
9449 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9450 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9451 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9454 return c;
9457 /* Gimplify OACC_DECLARE. */
9459 static void
9460 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9462 tree expr = *expr_p;
9463 gomp_target *stmt;
9464 tree clauses, t, decl;
9466 clauses = OACC_DECLARE_CLAUSES (expr);
9468 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9469 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9471 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9473 decl = OMP_CLAUSE_DECL (t);
9475 if (TREE_CODE (decl) == MEM_REF)
9476 decl = TREE_OPERAND (decl, 0);
9478 if (VAR_P (decl) && !is_oacc_declared (decl))
9480 tree attr = get_identifier ("oacc declare target");
9481 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9482 DECL_ATTRIBUTES (decl));
9485 if (VAR_P (decl)
9486 && !is_global_var (decl)
9487 && DECL_CONTEXT (decl) == current_function_decl)
9489 tree c = gimplify_oacc_declare_1 (t);
9490 if (c)
9492 if (oacc_declare_returns == NULL)
9493 oacc_declare_returns = new hash_map<tree, tree>;
9495 oacc_declare_returns->put (decl, c);
9499 if (gimplify_omp_ctxp)
9500 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9503 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9504 clauses);
9506 gimplify_seq_add_stmt (pre_p, stmt);
9508 *expr_p = NULL_TREE;
9511 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9512 gimplification of the body, as well as scanning the body for used
9513 variables. We need to do this scan now, because variable-sized
9514 decls will be decomposed during gimplification. */
9516 static void
9517 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9519 tree expr = *expr_p;
9520 gimple *g;
9521 gimple_seq body = NULL;
9523 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9524 OMP_PARALLEL_COMBINED (expr)
9525 ? ORT_COMBINED_PARALLEL
9526 : ORT_PARALLEL, OMP_PARALLEL);
9528 push_gimplify_context ();
9530 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9531 if (gimple_code (g) == GIMPLE_BIND)
9532 pop_gimplify_context (g);
9533 else
9534 pop_gimplify_context (NULL);
9536 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9537 OMP_PARALLEL);
9539 g = gimple_build_omp_parallel (body,
9540 OMP_PARALLEL_CLAUSES (expr),
9541 NULL_TREE, NULL_TREE);
9542 if (OMP_PARALLEL_COMBINED (expr))
9543 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9544 gimplify_seq_add_stmt (pre_p, g);
9545 *expr_p = NULL_TREE;
9548 /* Gimplify the contents of an OMP_TASK statement. This involves
9549 gimplification of the body, as well as scanning the body for used
9550 variables. We need to do this scan now, because variable-sized
9551 decls will be decomposed during gimplification. */
9553 static void
9554 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9556 tree expr = *expr_p;
9557 gimple *g;
9558 gimple_seq body = NULL;
9560 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9561 omp_find_clause (OMP_TASK_CLAUSES (expr),
9562 OMP_CLAUSE_UNTIED)
9563 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9565 push_gimplify_context ();
9567 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9568 if (gimple_code (g) == GIMPLE_BIND)
9569 pop_gimplify_context (g);
9570 else
9571 pop_gimplify_context (NULL);
9573 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9574 OMP_TASK);
9576 g = gimple_build_omp_task (body,
9577 OMP_TASK_CLAUSES (expr),
9578 NULL_TREE, NULL_TREE,
9579 NULL_TREE, NULL_TREE, NULL_TREE);
9580 gimplify_seq_add_stmt (pre_p, g);
9581 *expr_p = NULL_TREE;
9584 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9585 with non-NULL OMP_FOR_INIT. */
9587 static tree
9588 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9590 *walk_subtrees = 0;
9591 switch (TREE_CODE (*tp))
9593 case OMP_FOR:
9594 *walk_subtrees = 1;
9595 /* FALLTHRU */
9596 case OMP_SIMD:
9597 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9598 return *tp;
9599 break;
9600 case BIND_EXPR:
9601 case STATEMENT_LIST:
9602 case OMP_PARALLEL:
9603 *walk_subtrees = 1;
9604 break;
9605 default:
9606 break;
9608 return NULL_TREE;
9611 /* Gimplify the gross structure of an OMP_FOR statement. */
9613 static enum gimplify_status
9614 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9616 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9617 enum gimplify_status ret = GS_ALL_DONE;
9618 enum gimplify_status tret;
9619 gomp_for *gfor;
9620 gimple_seq for_body, for_pre_body;
9621 int i;
9622 bitmap has_decl_expr = NULL;
9623 enum omp_region_type ort = ORT_WORKSHARE;
9625 orig_for_stmt = for_stmt = *expr_p;
9627 switch (TREE_CODE (for_stmt))
9629 case OMP_FOR:
9630 case OMP_DISTRIBUTE:
9631 break;
9632 case OACC_LOOP:
9633 ort = ORT_ACC;
9634 break;
9635 case OMP_TASKLOOP:
9636 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9637 ort = ORT_UNTIED_TASK;
9638 else
9639 ort = ORT_TASK;
9640 break;
9641 case OMP_SIMD:
9642 ort = ORT_SIMD;
9643 break;
9644 default:
9645 gcc_unreachable ();
9648 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9649 clause for the IV. */
9650 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9652 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9653 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9654 decl = TREE_OPERAND (t, 0);
9655 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9656 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9657 && OMP_CLAUSE_DECL (c) == decl)
9659 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9660 break;
9664 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9666 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9667 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9668 find_combined_omp_for, NULL, NULL);
9669 if (inner_for_stmt == NULL_TREE)
9671 gcc_assert (seen_error ());
9672 *expr_p = NULL_TREE;
9673 return GS_ERROR;
9677 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9678 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9679 TREE_CODE (for_stmt));
9681 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9682 gimplify_omp_ctxp->distribute = true;
9684 /* Handle OMP_FOR_INIT. */
9685 for_pre_body = NULL;
9686 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9688 has_decl_expr = BITMAP_ALLOC (NULL);
9689 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9690 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9691 == VAR_DECL)
9693 t = OMP_FOR_PRE_BODY (for_stmt);
9694 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9696 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9698 tree_stmt_iterator si;
9699 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9700 tsi_next (&si))
9702 t = tsi_stmt (si);
9703 if (TREE_CODE (t) == DECL_EXPR
9704 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9705 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9709 if (OMP_FOR_PRE_BODY (for_stmt))
9711 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9712 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9713 else
9715 struct gimplify_omp_ctx ctx;
9716 memset (&ctx, 0, sizeof (ctx));
9717 ctx.region_type = ORT_NONE;
9718 gimplify_omp_ctxp = &ctx;
9719 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9720 gimplify_omp_ctxp = NULL;
9723 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9725 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9726 for_stmt = inner_for_stmt;
9728 /* For taskloop, need to gimplify the start, end and step before the
9729 taskloop, outside of the taskloop omp context. */
9730 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9732 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9734 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9735 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9737 TREE_OPERAND (t, 1)
9738 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9739 pre_p, NULL, false);
9740 tree c = build_omp_clause (input_location,
9741 OMP_CLAUSE_FIRSTPRIVATE);
9742 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9743 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9744 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9747 /* Handle OMP_FOR_COND. */
9748 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9749 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9751 TREE_OPERAND (t, 1)
9752 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9753 gimple_seq_empty_p (for_pre_body)
9754 ? pre_p : &for_pre_body, NULL,
9755 false);
9756 tree c = build_omp_clause (input_location,
9757 OMP_CLAUSE_FIRSTPRIVATE);
9758 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9759 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9760 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9763 /* Handle OMP_FOR_INCR. */
9764 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9765 if (TREE_CODE (t) == MODIFY_EXPR)
9767 decl = TREE_OPERAND (t, 0);
9768 t = TREE_OPERAND (t, 1);
9769 tree *tp = &TREE_OPERAND (t, 1);
9770 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9771 tp = &TREE_OPERAND (t, 0);
9773 if (!is_gimple_constant (*tp))
9775 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9776 ? pre_p : &for_pre_body;
9777 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9778 tree c = build_omp_clause (input_location,
9779 OMP_CLAUSE_FIRSTPRIVATE);
9780 OMP_CLAUSE_DECL (c) = *tp;
9781 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9782 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9787 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9788 OMP_TASKLOOP);
9791 if (orig_for_stmt != for_stmt)
9792 gimplify_omp_ctxp->combined_loop = true;
9794 for_body = NULL;
9795 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9796 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9797 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9798 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9800 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9801 bool is_doacross = false;
9802 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9804 is_doacross = true;
9805 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9806 (OMP_FOR_INIT (for_stmt))
9807 * 2);
9809 int collapse = 1, tile = 0;
9810 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9811 if (c)
9812 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9813 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9814 if (c)
9815 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9816 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9818 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9819 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9820 decl = TREE_OPERAND (t, 0);
9821 gcc_assert (DECL_P (decl));
9822 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9823 || POINTER_TYPE_P (TREE_TYPE (decl)));
9824 if (is_doacross)
9826 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9827 gimplify_omp_ctxp->loop_iter_var.quick_push
9828 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9829 else
9830 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9831 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9834 /* Make sure the iteration variable is private. */
9835 tree c = NULL_TREE;
9836 tree c2 = NULL_TREE;
9837 if (orig_for_stmt != for_stmt)
9838 /* Do this only on innermost construct for combined ones. */;
9839 else if (ort == ORT_SIMD)
9841 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9842 (splay_tree_key) decl);
9843 omp_is_private (gimplify_omp_ctxp, decl,
9844 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9845 != 1));
9846 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9847 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9848 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9850 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9851 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9852 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9853 if (has_decl_expr
9854 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9856 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9857 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9859 struct gimplify_omp_ctx *outer
9860 = gimplify_omp_ctxp->outer_context;
9861 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9863 if (outer->region_type == ORT_WORKSHARE
9864 && outer->combined_loop)
9866 n = splay_tree_lookup (outer->variables,
9867 (splay_tree_key)decl);
9868 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9870 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9871 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9873 else
9875 struct gimplify_omp_ctx *octx = outer->outer_context;
9876 if (octx
9877 && octx->region_type == ORT_COMBINED_PARALLEL
9878 && octx->outer_context
9879 && (octx->outer_context->region_type
9880 == ORT_WORKSHARE)
9881 && octx->outer_context->combined_loop)
9883 octx = octx->outer_context;
9884 n = splay_tree_lookup (octx->variables,
9885 (splay_tree_key)decl);
9886 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9888 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9889 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9896 OMP_CLAUSE_DECL (c) = decl;
9897 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9898 OMP_FOR_CLAUSES (for_stmt) = c;
9899 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9900 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9902 if (outer->region_type == ORT_WORKSHARE
9903 && outer->combined_loop)
9905 if (outer->outer_context
9906 && (outer->outer_context->region_type
9907 == ORT_COMBINED_PARALLEL))
9908 outer = outer->outer_context;
9909 else if (omp_check_private (outer, decl, false))
9910 outer = NULL;
9912 else if (((outer->region_type & ORT_TASK) != 0)
9913 && outer->combined_loop
9914 && !omp_check_private (gimplify_omp_ctxp,
9915 decl, false))
9917 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9919 omp_notice_variable (outer, decl, true);
9920 outer = NULL;
9922 if (outer)
9924 n = splay_tree_lookup (outer->variables,
9925 (splay_tree_key)decl);
9926 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9928 omp_add_variable (outer, decl,
9929 GOVD_LASTPRIVATE | GOVD_SEEN);
9930 if (outer->region_type == ORT_COMBINED_PARALLEL
9931 && outer->outer_context
9932 && (outer->outer_context->region_type
9933 == ORT_WORKSHARE)
9934 && outer->outer_context->combined_loop)
9936 outer = outer->outer_context;
9937 n = splay_tree_lookup (outer->variables,
9938 (splay_tree_key)decl);
9939 if (omp_check_private (outer, decl, false))
9940 outer = NULL;
9941 else if (n == NULL
9942 || ((n->value & GOVD_DATA_SHARE_CLASS)
9943 == 0))
9944 omp_add_variable (outer, decl,
9945 GOVD_LASTPRIVATE
9946 | GOVD_SEEN);
9947 else
9948 outer = NULL;
9950 if (outer && outer->outer_context
9951 && (outer->outer_context->region_type
9952 == ORT_COMBINED_TEAMS))
9954 outer = outer->outer_context;
9955 n = splay_tree_lookup (outer->variables,
9956 (splay_tree_key)decl);
9957 if (n == NULL
9958 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9959 omp_add_variable (outer, decl,
9960 GOVD_SHARED | GOVD_SEEN);
9961 else
9962 outer = NULL;
9964 if (outer && outer->outer_context)
9965 omp_notice_variable (outer->outer_context, decl,
9966 true);
9971 else
9973 bool lastprivate
9974 = (!has_decl_expr
9975 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
9976 struct gimplify_omp_ctx *outer
9977 = gimplify_omp_ctxp->outer_context;
9978 if (outer && lastprivate)
9980 if (outer->region_type == ORT_WORKSHARE
9981 && outer->combined_loop)
9983 n = splay_tree_lookup (outer->variables,
9984 (splay_tree_key)decl);
9985 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9987 lastprivate = false;
9988 outer = NULL;
9990 else if (outer->outer_context
9991 && (outer->outer_context->region_type
9992 == ORT_COMBINED_PARALLEL))
9993 outer = outer->outer_context;
9994 else if (omp_check_private (outer, decl, false))
9995 outer = NULL;
9997 else if (((outer->region_type & ORT_TASK) != 0)
9998 && outer->combined_loop
9999 && !omp_check_private (gimplify_omp_ctxp,
10000 decl, false))
10002 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10004 omp_notice_variable (outer, decl, true);
10005 outer = NULL;
10007 if (outer)
10009 n = splay_tree_lookup (outer->variables,
10010 (splay_tree_key)decl);
10011 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10013 omp_add_variable (outer, decl,
10014 GOVD_LASTPRIVATE | GOVD_SEEN);
10015 if (outer->region_type == ORT_COMBINED_PARALLEL
10016 && outer->outer_context
10017 && (outer->outer_context->region_type
10018 == ORT_WORKSHARE)
10019 && outer->outer_context->combined_loop)
10021 outer = outer->outer_context;
10022 n = splay_tree_lookup (outer->variables,
10023 (splay_tree_key)decl);
10024 if (omp_check_private (outer, decl, false))
10025 outer = NULL;
10026 else if (n == NULL
10027 || ((n->value & GOVD_DATA_SHARE_CLASS)
10028 == 0))
10029 omp_add_variable (outer, decl,
10030 GOVD_LASTPRIVATE
10031 | GOVD_SEEN);
10032 else
10033 outer = NULL;
10035 if (outer && outer->outer_context
10036 && (outer->outer_context->region_type
10037 == ORT_COMBINED_TEAMS))
10039 outer = outer->outer_context;
10040 n = splay_tree_lookup (outer->variables,
10041 (splay_tree_key)decl);
10042 if (n == NULL
10043 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10044 omp_add_variable (outer, decl,
10045 GOVD_SHARED | GOVD_SEEN);
10046 else
10047 outer = NULL;
10049 if (outer && outer->outer_context)
10050 omp_notice_variable (outer->outer_context, decl,
10051 true);
10056 c = build_omp_clause (input_location,
10057 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10058 : OMP_CLAUSE_PRIVATE);
10059 OMP_CLAUSE_DECL (c) = decl;
10060 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10061 OMP_FOR_CLAUSES (for_stmt) = c;
10062 omp_add_variable (gimplify_omp_ctxp, decl,
10063 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10064 | GOVD_EXPLICIT | GOVD_SEEN);
10065 c = NULL_TREE;
10068 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10069 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10070 else
10071 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10073 /* If DECL is not a gimple register, create a temporary variable to act
10074 as an iteration counter. This is valid, since DECL cannot be
10075 modified in the body of the loop. Similarly for any iteration vars
10076 in simd with collapse > 1 where the iterator vars must be
10077 lastprivate. */
10078 if (orig_for_stmt != for_stmt)
10079 var = decl;
10080 else if (!is_gimple_reg (decl)
10081 || (ort == ORT_SIMD
10082 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10084 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10085 /* Make sure omp_add_variable is not called on it prematurely.
10086 We call it ourselves a few lines later. */
10087 gimplify_omp_ctxp = NULL;
10088 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10089 gimplify_omp_ctxp = ctx;
10090 TREE_OPERAND (t, 0) = var;
10092 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10094 if (ort == ORT_SIMD
10095 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10097 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10098 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10099 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10100 OMP_CLAUSE_DECL (c2) = var;
10101 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10102 OMP_FOR_CLAUSES (for_stmt) = c2;
10103 omp_add_variable (gimplify_omp_ctxp, var,
10104 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10105 if (c == NULL_TREE)
10107 c = c2;
10108 c2 = NULL_TREE;
10111 else
10112 omp_add_variable (gimplify_omp_ctxp, var,
10113 GOVD_PRIVATE | GOVD_SEEN);
10115 else
10116 var = decl;
10118 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10119 is_gimple_val, fb_rvalue, false);
10120 ret = MIN (ret, tret);
10121 if (ret == GS_ERROR)
10122 return ret;
10124 /* Handle OMP_FOR_COND. */
10125 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10126 gcc_assert (COMPARISON_CLASS_P (t));
10127 gcc_assert (TREE_OPERAND (t, 0) == decl);
10129 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10130 is_gimple_val, fb_rvalue, false);
10131 ret = MIN (ret, tret);
10133 /* Handle OMP_FOR_INCR. */
10134 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10135 switch (TREE_CODE (t))
10137 case PREINCREMENT_EXPR:
10138 case POSTINCREMENT_EXPR:
10140 tree decl = TREE_OPERAND (t, 0);
10141 /* c_omp_for_incr_canonicalize_ptr() should have been
10142 called to massage things appropriately. */
10143 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10145 if (orig_for_stmt != for_stmt)
10146 break;
10147 t = build_int_cst (TREE_TYPE (decl), 1);
10148 if (c)
10149 OMP_CLAUSE_LINEAR_STEP (c) = t;
10150 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10151 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10152 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10153 break;
10156 case PREDECREMENT_EXPR:
10157 case POSTDECREMENT_EXPR:
10158 /* c_omp_for_incr_canonicalize_ptr() should have been
10159 called to massage things appropriately. */
10160 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10161 if (orig_for_stmt != for_stmt)
10162 break;
10163 t = build_int_cst (TREE_TYPE (decl), -1);
10164 if (c)
10165 OMP_CLAUSE_LINEAR_STEP (c) = t;
10166 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10167 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10168 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10169 break;
10171 case MODIFY_EXPR:
10172 gcc_assert (TREE_OPERAND (t, 0) == decl);
10173 TREE_OPERAND (t, 0) = var;
10175 t = TREE_OPERAND (t, 1);
10176 switch (TREE_CODE (t))
10178 case PLUS_EXPR:
10179 if (TREE_OPERAND (t, 1) == decl)
10181 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10182 TREE_OPERAND (t, 0) = var;
10183 break;
10186 /* Fallthru. */
10187 case MINUS_EXPR:
10188 case POINTER_PLUS_EXPR:
10189 gcc_assert (TREE_OPERAND (t, 0) == decl);
10190 TREE_OPERAND (t, 0) = var;
10191 break;
10192 default:
10193 gcc_unreachable ();
10196 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10197 is_gimple_val, fb_rvalue, false);
10198 ret = MIN (ret, tret);
10199 if (c)
10201 tree step = TREE_OPERAND (t, 1);
10202 tree stept = TREE_TYPE (decl);
10203 if (POINTER_TYPE_P (stept))
10204 stept = sizetype;
10205 step = fold_convert (stept, step);
10206 if (TREE_CODE (t) == MINUS_EXPR)
10207 step = fold_build1 (NEGATE_EXPR, stept, step);
10208 OMP_CLAUSE_LINEAR_STEP (c) = step;
10209 if (step != TREE_OPERAND (t, 1))
10211 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10212 &for_pre_body, NULL,
10213 is_gimple_val, fb_rvalue, false);
10214 ret = MIN (ret, tret);
10217 break;
10219 default:
10220 gcc_unreachable ();
10223 if (c2)
10225 gcc_assert (c);
10226 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10229 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10231 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10232 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10233 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10234 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10235 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10236 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10237 && OMP_CLAUSE_DECL (c) == decl)
10239 if (is_doacross && (collapse == 1 || i >= collapse))
10240 t = var;
10241 else
10243 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10244 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10245 gcc_assert (TREE_OPERAND (t, 0) == var);
10246 t = TREE_OPERAND (t, 1);
10247 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10248 || TREE_CODE (t) == MINUS_EXPR
10249 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10250 gcc_assert (TREE_OPERAND (t, 0) == var);
10251 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10252 is_doacross ? var : decl,
10253 TREE_OPERAND (t, 1));
10255 gimple_seq *seq;
10256 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10257 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10258 else
10259 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10260 gimplify_assign (decl, t, seq);
10265 BITMAP_FREE (has_decl_expr);
10267 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10269 push_gimplify_context ();
10270 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10272 OMP_FOR_BODY (orig_for_stmt)
10273 = build3 (BIND_EXPR, void_type_node, NULL,
10274 OMP_FOR_BODY (orig_for_stmt), NULL);
10275 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10279 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10280 &for_body);
10282 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10284 if (gimple_code (g) == GIMPLE_BIND)
10285 pop_gimplify_context (g);
10286 else
10287 pop_gimplify_context (NULL);
10290 if (orig_for_stmt != for_stmt)
10291 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10293 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10294 decl = TREE_OPERAND (t, 0);
10295 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10296 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10297 gimplify_omp_ctxp = ctx->outer_context;
10298 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10299 gimplify_omp_ctxp = ctx;
10300 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10301 TREE_OPERAND (t, 0) = var;
10302 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10303 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10304 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10307 gimplify_adjust_omp_clauses (pre_p, for_body,
10308 &OMP_FOR_CLAUSES (orig_for_stmt),
10309 TREE_CODE (orig_for_stmt));
10311 int kind;
10312 switch (TREE_CODE (orig_for_stmt))
10314 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10315 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10316 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10317 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10318 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10319 default:
10320 gcc_unreachable ();
10322 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10323 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10324 for_pre_body);
10325 if (orig_for_stmt != for_stmt)
10326 gimple_omp_for_set_combined_p (gfor, true);
10327 if (gimplify_omp_ctxp
10328 && (gimplify_omp_ctxp->combined_loop
10329 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10330 && gimplify_omp_ctxp->outer_context
10331 && gimplify_omp_ctxp->outer_context->combined_loop)))
10333 gimple_omp_for_set_combined_into_p (gfor, true);
10334 if (gimplify_omp_ctxp->combined_loop)
10335 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10336 else
10337 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10340 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10342 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10343 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10344 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10345 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10346 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10347 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10348 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10349 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10352 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10353 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10354 The outer taskloop stands for computing the number of iterations,
10355 counts for collapsed loops and holding taskloop specific clauses.
10356 The task construct stands for the effect of data sharing on the
10357 explicit task it creates and the inner taskloop stands for expansion
10358 of the static loop inside of the explicit task construct. */
10359 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10361 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10362 tree task_clauses = NULL_TREE;
10363 tree c = *gfor_clauses_ptr;
10364 tree *gtask_clauses_ptr = &task_clauses;
10365 tree outer_for_clauses = NULL_TREE;
10366 tree *gforo_clauses_ptr = &outer_for_clauses;
10367 for (; c; c = OMP_CLAUSE_CHAIN (c))
10368 switch (OMP_CLAUSE_CODE (c))
10370 /* These clauses are allowed on task, move them there. */
10371 case OMP_CLAUSE_SHARED:
10372 case OMP_CLAUSE_FIRSTPRIVATE:
10373 case OMP_CLAUSE_DEFAULT:
10374 case OMP_CLAUSE_IF:
10375 case OMP_CLAUSE_UNTIED:
10376 case OMP_CLAUSE_FINAL:
10377 case OMP_CLAUSE_MERGEABLE:
10378 case OMP_CLAUSE_PRIORITY:
10379 *gtask_clauses_ptr = c;
10380 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10381 break;
10382 case OMP_CLAUSE_PRIVATE:
10383 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10385 /* We want private on outer for and firstprivate
10386 on task. */
10387 *gtask_clauses_ptr
10388 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10389 OMP_CLAUSE_FIRSTPRIVATE);
10390 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10391 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10392 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10393 *gforo_clauses_ptr = c;
10394 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10396 else
10398 *gtask_clauses_ptr = c;
10399 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10401 break;
10402 /* These clauses go into outer taskloop clauses. */
10403 case OMP_CLAUSE_GRAINSIZE:
10404 case OMP_CLAUSE_NUM_TASKS:
10405 case OMP_CLAUSE_NOGROUP:
10406 *gforo_clauses_ptr = c;
10407 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10408 break;
10409 /* Taskloop clause we duplicate on both taskloops. */
10410 case OMP_CLAUSE_COLLAPSE:
10411 *gfor_clauses_ptr = c;
10412 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10413 *gforo_clauses_ptr = copy_node (c);
10414 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10415 break;
10416 /* For lastprivate, keep the clause on inner taskloop, and add
10417 a shared clause on task. If the same decl is also firstprivate,
10418 add also firstprivate clause on the inner taskloop. */
10419 case OMP_CLAUSE_LASTPRIVATE:
10420 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10422 /* For taskloop C++ lastprivate IVs, we want:
10423 1) private on outer taskloop
10424 2) firstprivate and shared on task
10425 3) lastprivate on inner taskloop */
10426 *gtask_clauses_ptr
10427 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10428 OMP_CLAUSE_FIRSTPRIVATE);
10429 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10430 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10431 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10432 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10433 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10434 OMP_CLAUSE_PRIVATE);
10435 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10436 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10437 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10438 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10440 *gfor_clauses_ptr = c;
10441 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10442 *gtask_clauses_ptr
10443 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10444 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10445 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10446 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10447 gtask_clauses_ptr
10448 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10449 break;
10450 default:
10451 gcc_unreachable ();
10453 *gfor_clauses_ptr = NULL_TREE;
10454 *gtask_clauses_ptr = NULL_TREE;
10455 *gforo_clauses_ptr = NULL_TREE;
10456 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10457 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10458 NULL_TREE, NULL_TREE, NULL_TREE);
10459 gimple_omp_task_set_taskloop_p (g, true);
10460 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10461 gomp_for *gforo
10462 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10463 gimple_omp_for_collapse (gfor),
10464 gimple_omp_for_pre_body (gfor));
10465 gimple_omp_for_set_pre_body (gfor, NULL);
10466 gimple_omp_for_set_combined_p (gforo, true);
10467 gimple_omp_for_set_combined_into_p (gfor, true);
10468 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10470 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10471 tree v = create_tmp_var (type);
10472 gimple_omp_for_set_index (gforo, i, v);
10473 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10474 gimple_omp_for_set_initial (gforo, i, t);
10475 gimple_omp_for_set_cond (gforo, i,
10476 gimple_omp_for_cond (gfor, i));
10477 t = unshare_expr (gimple_omp_for_final (gfor, i));
10478 gimple_omp_for_set_final (gforo, i, t);
10479 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10480 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10481 TREE_OPERAND (t, 0) = v;
10482 gimple_omp_for_set_incr (gforo, i, t);
10483 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10484 OMP_CLAUSE_DECL (t) = v;
10485 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10486 gimple_omp_for_set_clauses (gforo, t);
10488 gimplify_seq_add_stmt (pre_p, gforo);
10490 else
10491 gimplify_seq_add_stmt (pre_p, gfor);
10492 if (ret != GS_ALL_DONE)
10493 return GS_ERROR;
10494 *expr_p = NULL_TREE;
10495 return GS_ALL_DONE;
10498 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10499 of OMP_TARGET's body. */
10501 static tree
10502 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10504 *walk_subtrees = 0;
10505 switch (TREE_CODE (*tp))
10507 case OMP_TEAMS:
10508 return *tp;
10509 case BIND_EXPR:
10510 case STATEMENT_LIST:
10511 *walk_subtrees = 1;
10512 break;
10513 default:
10514 break;
10516 return NULL_TREE;
10519 /* Helper function of optimize_target_teams, determine if the expression
10520 can be computed safely before the target construct on the host. */
10522 static tree
10523 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10525 splay_tree_node n;
10527 if (TYPE_P (*tp))
10529 *walk_subtrees = 0;
10530 return NULL_TREE;
10532 switch (TREE_CODE (*tp))
10534 case VAR_DECL:
10535 case PARM_DECL:
10536 case RESULT_DECL:
10537 *walk_subtrees = 0;
10538 if (error_operand_p (*tp)
10539 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10540 || DECL_HAS_VALUE_EXPR_P (*tp)
10541 || DECL_THREAD_LOCAL_P (*tp)
10542 || TREE_SIDE_EFFECTS (*tp)
10543 || TREE_THIS_VOLATILE (*tp))
10544 return *tp;
10545 if (is_global_var (*tp)
10546 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10547 || lookup_attribute ("omp declare target link",
10548 DECL_ATTRIBUTES (*tp))))
10549 return *tp;
10550 if (VAR_P (*tp)
10551 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10552 && !is_global_var (*tp)
10553 && decl_function_context (*tp) == current_function_decl)
10554 return *tp;
10555 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10556 (splay_tree_key) *tp);
10557 if (n == NULL)
10559 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10560 return NULL_TREE;
10561 return *tp;
10563 else if (n->value & GOVD_LOCAL)
10564 return *tp;
10565 else if (n->value & GOVD_FIRSTPRIVATE)
10566 return NULL_TREE;
10567 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10568 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10569 return NULL_TREE;
10570 return *tp;
10571 case INTEGER_CST:
10572 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10573 return *tp;
10574 return NULL_TREE;
10575 case TARGET_EXPR:
10576 if (TARGET_EXPR_INITIAL (*tp)
10577 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10578 return *tp;
10579 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10580 walk_subtrees, NULL);
10581 /* Allow some reasonable subset of integral arithmetics. */
10582 case PLUS_EXPR:
10583 case MINUS_EXPR:
10584 case MULT_EXPR:
10585 case TRUNC_DIV_EXPR:
10586 case CEIL_DIV_EXPR:
10587 case FLOOR_DIV_EXPR:
10588 case ROUND_DIV_EXPR:
10589 case TRUNC_MOD_EXPR:
10590 case CEIL_MOD_EXPR:
10591 case FLOOR_MOD_EXPR:
10592 case ROUND_MOD_EXPR:
10593 case RDIV_EXPR:
10594 case EXACT_DIV_EXPR:
10595 case MIN_EXPR:
10596 case MAX_EXPR:
10597 case LSHIFT_EXPR:
10598 case RSHIFT_EXPR:
10599 case BIT_IOR_EXPR:
10600 case BIT_XOR_EXPR:
10601 case BIT_AND_EXPR:
10602 case NEGATE_EXPR:
10603 case ABS_EXPR:
10604 case BIT_NOT_EXPR:
10605 case NON_LVALUE_EXPR:
10606 CASE_CONVERT:
10607 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10608 return *tp;
10609 return NULL_TREE;
10610 /* And disallow anything else, except for comparisons. */
10611 default:
10612 if (COMPARISON_CLASS_P (*tp))
10613 return NULL_TREE;
10614 return *tp;
10618 /* Try to determine if the num_teams and/or thread_limit expressions
10619 can have their values determined already before entering the
10620 target construct.
10621 INTEGER_CSTs trivially are,
10622 integral decls that are firstprivate (explicitly or implicitly)
10623 or explicitly map(always, to:) or map(always, tofrom:) on the target
10624 region too, and expressions involving simple arithmetics on those
10625 too, function calls are not ok, dereferencing something neither etc.
10626 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10627 EXPR based on what we find:
10628 0 stands for clause not specified at all, use implementation default
10629 -1 stands for value that can't be determined easily before entering
10630 the target construct.
10631 If teams construct is not present at all, use 1 for num_teams
10632 and 0 for thread_limit (only one team is involved, and the thread
10633 limit is implementation defined. */
10635 static void
10636 optimize_target_teams (tree target, gimple_seq *pre_p)
10638 tree body = OMP_BODY (target);
10639 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10640 tree num_teams = integer_zero_node;
10641 tree thread_limit = integer_zero_node;
10642 location_t num_teams_loc = EXPR_LOCATION (target);
10643 location_t thread_limit_loc = EXPR_LOCATION (target);
10644 tree c, *p, expr;
10645 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10647 if (teams == NULL_TREE)
10648 num_teams = integer_one_node;
10649 else
10650 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10652 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10654 p = &num_teams;
10655 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10657 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10659 p = &thread_limit;
10660 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10662 else
10663 continue;
10664 expr = OMP_CLAUSE_OPERAND (c, 0);
10665 if (TREE_CODE (expr) == INTEGER_CST)
10667 *p = expr;
10668 continue;
10670 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10672 *p = integer_minus_one_node;
10673 continue;
10675 *p = expr;
10676 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10677 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10678 == GS_ERROR)
10680 gimplify_omp_ctxp = target_ctx;
10681 *p = integer_minus_one_node;
10682 continue;
10684 gimplify_omp_ctxp = target_ctx;
10685 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10686 OMP_CLAUSE_OPERAND (c, 0) = *p;
10688 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10689 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10690 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10691 OMP_TARGET_CLAUSES (target) = c;
10692 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10693 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10694 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10695 OMP_TARGET_CLAUSES (target) = c;
10698 /* Gimplify the gross structure of several OMP constructs. */
10700 static void
10701 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10703 tree expr = *expr_p;
10704 gimple *stmt;
10705 gimple_seq body = NULL;
10706 enum omp_region_type ort;
10708 switch (TREE_CODE (expr))
10710 case OMP_SECTIONS:
10711 case OMP_SINGLE:
10712 ort = ORT_WORKSHARE;
10713 break;
10714 case OMP_TARGET:
10715 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10716 break;
10717 case OACC_KERNELS:
10718 ort = ORT_ACC_KERNELS;
10719 break;
10720 case OACC_PARALLEL:
10721 ort = ORT_ACC_PARALLEL;
10722 break;
10723 case OACC_DATA:
10724 ort = ORT_ACC_DATA;
10725 break;
10726 case OMP_TARGET_DATA:
10727 ort = ORT_TARGET_DATA;
10728 break;
10729 case OMP_TEAMS:
10730 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10731 break;
10732 case OACC_HOST_DATA:
10733 ort = ORT_ACC_HOST_DATA;
10734 break;
10735 default:
10736 gcc_unreachable ();
10738 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10739 TREE_CODE (expr));
10740 if (TREE_CODE (expr) == OMP_TARGET)
10741 optimize_target_teams (expr, pre_p);
10742 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10744 push_gimplify_context ();
10745 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10746 if (gimple_code (g) == GIMPLE_BIND)
10747 pop_gimplify_context (g);
10748 else
10749 pop_gimplify_context (NULL);
10750 if ((ort & ORT_TARGET_DATA) != 0)
10752 enum built_in_function end_ix;
10753 switch (TREE_CODE (expr))
10755 case OACC_DATA:
10756 case OACC_HOST_DATA:
10757 end_ix = BUILT_IN_GOACC_DATA_END;
10758 break;
10759 case OMP_TARGET_DATA:
10760 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10761 break;
10762 default:
10763 gcc_unreachable ();
10765 tree fn = builtin_decl_explicit (end_ix);
10766 g = gimple_build_call (fn, 0);
10767 gimple_seq cleanup = NULL;
10768 gimple_seq_add_stmt (&cleanup, g);
10769 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10770 body = NULL;
10771 gimple_seq_add_stmt (&body, g);
10774 else
10775 gimplify_and_add (OMP_BODY (expr), &body);
10776 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10777 TREE_CODE (expr));
10779 switch (TREE_CODE (expr))
10781 case OACC_DATA:
10782 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10783 OMP_CLAUSES (expr));
10784 break;
10785 case OACC_KERNELS:
10786 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10787 OMP_CLAUSES (expr));
10788 break;
10789 case OACC_HOST_DATA:
10790 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10791 OMP_CLAUSES (expr));
10792 break;
10793 case OACC_PARALLEL:
10794 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10795 OMP_CLAUSES (expr));
10796 break;
10797 case OMP_SECTIONS:
10798 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10799 break;
10800 case OMP_SINGLE:
10801 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10802 break;
10803 case OMP_TARGET:
10804 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10805 OMP_CLAUSES (expr));
10806 break;
10807 case OMP_TARGET_DATA:
10808 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10809 OMP_CLAUSES (expr));
10810 break;
10811 case OMP_TEAMS:
10812 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10813 break;
10814 default:
10815 gcc_unreachable ();
10818 gimplify_seq_add_stmt (pre_p, stmt);
10819 *expr_p = NULL_TREE;
10822 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10823 target update constructs. */
10825 static void
10826 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10828 tree expr = *expr_p;
10829 int kind;
10830 gomp_target *stmt;
10831 enum omp_region_type ort = ORT_WORKSHARE;
10833 switch (TREE_CODE (expr))
10835 case OACC_ENTER_DATA:
10836 case OACC_EXIT_DATA:
10837 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10838 ort = ORT_ACC;
10839 break;
10840 case OACC_UPDATE:
10841 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10842 ort = ORT_ACC;
10843 break;
10844 case OMP_TARGET_UPDATE:
10845 kind = GF_OMP_TARGET_KIND_UPDATE;
10846 break;
10847 case OMP_TARGET_ENTER_DATA:
10848 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10849 break;
10850 case OMP_TARGET_EXIT_DATA:
10851 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10852 break;
10853 default:
10854 gcc_unreachable ();
10856 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10857 ort, TREE_CODE (expr));
10858 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10859 TREE_CODE (expr));
10860 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10862 gimplify_seq_add_stmt (pre_p, stmt);
10863 *expr_p = NULL_TREE;
10866 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10867 stabilized the lhs of the atomic operation as *ADDR. Return true if
10868 EXPR is this stabilized form. */
10870 static bool
10871 goa_lhs_expr_p (tree expr, tree addr)
10873 /* Also include casts to other type variants. The C front end is fond
10874 of adding these for e.g. volatile variables. This is like
10875 STRIP_TYPE_NOPS but includes the main variant lookup. */
10876 STRIP_USELESS_TYPE_CONVERSION (expr);
10878 if (TREE_CODE (expr) == INDIRECT_REF)
10880 expr = TREE_OPERAND (expr, 0);
10881 while (expr != addr
10882 && (CONVERT_EXPR_P (expr)
10883 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10884 && TREE_CODE (expr) == TREE_CODE (addr)
10885 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10887 expr = TREE_OPERAND (expr, 0);
10888 addr = TREE_OPERAND (addr, 0);
10890 if (expr == addr)
10891 return true;
10892 return (TREE_CODE (addr) == ADDR_EXPR
10893 && TREE_CODE (expr) == ADDR_EXPR
10894 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10896 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10897 return true;
10898 return false;
10901 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10902 expression does not involve the lhs, evaluate it into a temporary.
10903 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10904 or -1 if an error was encountered. */
10906 static int
10907 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10908 tree lhs_var)
10910 tree expr = *expr_p;
10911 int saw_lhs;
10913 if (goa_lhs_expr_p (expr, lhs_addr))
10915 *expr_p = lhs_var;
10916 return 1;
10918 if (is_gimple_val (expr))
10919 return 0;
10921 saw_lhs = 0;
10922 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10924 case tcc_binary:
10925 case tcc_comparison:
10926 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10927 lhs_var);
10928 /* FALLTHRU */
10929 case tcc_unary:
10930 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10931 lhs_var);
10932 break;
10933 case tcc_expression:
10934 switch (TREE_CODE (expr))
10936 case TRUTH_ANDIF_EXPR:
10937 case TRUTH_ORIF_EXPR:
10938 case TRUTH_AND_EXPR:
10939 case TRUTH_OR_EXPR:
10940 case TRUTH_XOR_EXPR:
10941 case BIT_INSERT_EXPR:
10942 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10943 lhs_addr, lhs_var);
10944 /* FALLTHRU */
10945 case TRUTH_NOT_EXPR:
10946 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10947 lhs_addr, lhs_var);
10948 break;
10949 case COMPOUND_EXPR:
10950 /* Break out any preevaluations from cp_build_modify_expr. */
10951 for (; TREE_CODE (expr) == COMPOUND_EXPR;
10952 expr = TREE_OPERAND (expr, 1))
10953 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10954 *expr_p = expr;
10955 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10956 default:
10957 break;
10959 break;
10960 case tcc_reference:
10961 if (TREE_CODE (expr) == BIT_FIELD_REF)
10962 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10963 lhs_addr, lhs_var);
10964 break;
10965 default:
10966 break;
10969 if (saw_lhs == 0)
10971 enum gimplify_status gs;
10972 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10973 if (gs != GS_ALL_DONE)
10974 saw_lhs = -1;
10977 return saw_lhs;
10980 /* Gimplify an OMP_ATOMIC statement. */
10982 static enum gimplify_status
10983 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10985 tree addr = TREE_OPERAND (*expr_p, 0);
10986 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10987 ? NULL : TREE_OPERAND (*expr_p, 1);
10988 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10989 tree tmp_load;
10990 gomp_atomic_load *loadstmt;
10991 gomp_atomic_store *storestmt;
10993 tmp_load = create_tmp_reg (type);
10994 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10995 return GS_ERROR;
10997 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10998 != GS_ALL_DONE)
10999 return GS_ERROR;
11001 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11002 gimplify_seq_add_stmt (pre_p, loadstmt);
11003 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11004 != GS_ALL_DONE)
11005 return GS_ERROR;
11007 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11008 rhs = tmp_load;
11009 storestmt = gimple_build_omp_atomic_store (rhs);
11010 gimplify_seq_add_stmt (pre_p, storestmt);
11011 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11013 gimple_omp_atomic_set_seq_cst (loadstmt);
11014 gimple_omp_atomic_set_seq_cst (storestmt);
11016 switch (TREE_CODE (*expr_p))
11018 case OMP_ATOMIC_READ:
11019 case OMP_ATOMIC_CAPTURE_OLD:
11020 *expr_p = tmp_load;
11021 gimple_omp_atomic_set_need_value (loadstmt);
11022 break;
11023 case OMP_ATOMIC_CAPTURE_NEW:
11024 *expr_p = rhs;
11025 gimple_omp_atomic_set_need_value (storestmt);
11026 break;
11027 default:
11028 *expr_p = NULL;
11029 break;
11032 return GS_ALL_DONE;
11035 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11036 body, and adding some EH bits. */
11038 static enum gimplify_status
11039 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11041 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11042 gimple *body_stmt;
11043 gtransaction *trans_stmt;
11044 gimple_seq body = NULL;
11045 int subcode = 0;
11047 /* Wrap the transaction body in a BIND_EXPR so we have a context
11048 where to put decls for OMP. */
11049 if (TREE_CODE (tbody) != BIND_EXPR)
11051 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11052 TREE_SIDE_EFFECTS (bind) = 1;
11053 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11054 TRANSACTION_EXPR_BODY (expr) = bind;
11057 push_gimplify_context ();
11058 temp = voidify_wrapper_expr (*expr_p, NULL);
11060 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11061 pop_gimplify_context (body_stmt);
11063 trans_stmt = gimple_build_transaction (body);
11064 if (TRANSACTION_EXPR_OUTER (expr))
11065 subcode = GTMA_IS_OUTER;
11066 else if (TRANSACTION_EXPR_RELAXED (expr))
11067 subcode = GTMA_IS_RELAXED;
11068 gimple_transaction_set_subcode (trans_stmt, subcode);
11070 gimplify_seq_add_stmt (pre_p, trans_stmt);
11072 if (temp)
11074 *expr_p = temp;
11075 return GS_OK;
11078 *expr_p = NULL_TREE;
11079 return GS_ALL_DONE;
11082 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11083 is the OMP_BODY of the original EXPR (which has already been
11084 gimplified so it's not present in the EXPR).
11086 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11088 static gimple *
11089 gimplify_omp_ordered (tree expr, gimple_seq body)
11091 tree c, decls;
11092 int failures = 0;
11093 unsigned int i;
11094 tree source_c = NULL_TREE;
11095 tree sink_c = NULL_TREE;
11097 if (gimplify_omp_ctxp)
11099 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11100 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11101 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11102 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11103 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11105 error_at (OMP_CLAUSE_LOCATION (c),
11106 "%<ordered%> construct with %<depend%> clause must be "
11107 "closely nested inside a loop with %<ordered%> clause "
11108 "with a parameter");
11109 failures++;
11111 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11112 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11114 bool fail = false;
11115 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11116 decls && TREE_CODE (decls) == TREE_LIST;
11117 decls = TREE_CHAIN (decls), ++i)
11118 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11119 continue;
11120 else if (TREE_VALUE (decls)
11121 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11123 error_at (OMP_CLAUSE_LOCATION (c),
11124 "variable %qE is not an iteration "
11125 "of outermost loop %d, expected %qE",
11126 TREE_VALUE (decls), i + 1,
11127 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11128 fail = true;
11129 failures++;
11131 else
11132 TREE_VALUE (decls)
11133 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11134 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11136 error_at (OMP_CLAUSE_LOCATION (c),
11137 "number of variables in %<depend(sink)%> "
11138 "clause does not match number of "
11139 "iteration variables");
11140 failures++;
11142 sink_c = c;
11144 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11145 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11147 if (source_c)
11149 error_at (OMP_CLAUSE_LOCATION (c),
11150 "more than one %<depend(source)%> clause on an "
11151 "%<ordered%> construct");
11152 failures++;
11154 else
11155 source_c = c;
11158 if (source_c && sink_c)
11160 error_at (OMP_CLAUSE_LOCATION (source_c),
11161 "%<depend(source)%> clause specified together with "
11162 "%<depend(sink:)%> clauses on the same construct");
11163 failures++;
11166 if (failures)
11167 return gimple_build_nop ();
11168 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11171 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11172 expression produces a value to be used as an operand inside a GIMPLE
11173 statement, the value will be stored back in *EXPR_P. This value will
11174 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11175 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11176 emitted in PRE_P and POST_P.
11178 Additionally, this process may overwrite parts of the input
11179 expression during gimplification. Ideally, it should be
11180 possible to do non-destructive gimplification.
11182 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11183 the expression needs to evaluate to a value to be used as
11184 an operand in a GIMPLE statement, this value will be stored in
11185 *EXPR_P on exit. This happens when the caller specifies one
11186 of fb_lvalue or fb_rvalue fallback flags.
11188 PRE_P will contain the sequence of GIMPLE statements corresponding
11189 to the evaluation of EXPR and all the side-effects that must
11190 be executed before the main expression. On exit, the last
11191 statement of PRE_P is the core statement being gimplified. For
11192 instance, when gimplifying 'if (++a)' the last statement in
11193 PRE_P will be 'if (t.1)' where t.1 is the result of
11194 pre-incrementing 'a'.
11196 POST_P will contain the sequence of GIMPLE statements corresponding
11197 to the evaluation of all the side-effects that must be executed
11198 after the main expression. If this is NULL, the post
11199 side-effects are stored at the end of PRE_P.
11201 The reason why the output is split in two is to handle post
11202 side-effects explicitly. In some cases, an expression may have
11203 inner and outer post side-effects which need to be emitted in
11204 an order different from the one given by the recursive
11205 traversal. For instance, for the expression (*p--)++ the post
11206 side-effects of '--' must actually occur *after* the post
11207 side-effects of '++'. However, gimplification will first visit
11208 the inner expression, so if a separate POST sequence was not
11209 used, the resulting sequence would be:
11211 1 t.1 = *p
11212 2 p = p - 1
11213 3 t.2 = t.1 + 1
11214 4 *p = t.2
11216 However, the post-decrement operation in line #2 must not be
11217 evaluated until after the store to *p at line #4, so the
11218 correct sequence should be:
11220 1 t.1 = *p
11221 2 t.2 = t.1 + 1
11222 3 *p = t.2
11223 4 p = p - 1
11225 So, by specifying a separate post queue, it is possible
11226 to emit the post side-effects in the correct order.
11227 If POST_P is NULL, an internal queue will be used. Before
11228 returning to the caller, the sequence POST_P is appended to
11229 the main output sequence PRE_P.
11231 GIMPLE_TEST_F points to a function that takes a tree T and
11232 returns nonzero if T is in the GIMPLE form requested by the
11233 caller. The GIMPLE predicates are in gimple.c.
11235 FALLBACK tells the function what sort of a temporary we want if
11236 gimplification cannot produce an expression that complies with
11237 GIMPLE_TEST_F.
11239 fb_none means that no temporary should be generated
11240 fb_rvalue means that an rvalue is OK to generate
11241 fb_lvalue means that an lvalue is OK to generate
11242 fb_either means that either is OK, but an lvalue is preferable.
11243 fb_mayfail means that gimplification may fail (in which case
11244 GS_ERROR will be returned)
11246 The return value is either GS_ERROR or GS_ALL_DONE, since this
11247 function iterates until EXPR is completely gimplified or an error
11248 occurs. */
11250 enum gimplify_status
11251 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11252 bool (*gimple_test_f) (tree), fallback_t fallback)
11254 tree tmp;
11255 gimple_seq internal_pre = NULL;
11256 gimple_seq internal_post = NULL;
11257 tree save_expr;
11258 bool is_statement;
11259 location_t saved_location;
11260 enum gimplify_status ret;
11261 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11262 tree label;
11264 save_expr = *expr_p;
11265 if (save_expr == NULL_TREE)
11266 return GS_ALL_DONE;
11268 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11269 is_statement = gimple_test_f == is_gimple_stmt;
11270 if (is_statement)
11271 gcc_assert (pre_p);
11273 /* Consistency checks. */
11274 if (gimple_test_f == is_gimple_reg)
11275 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11276 else if (gimple_test_f == is_gimple_val
11277 || gimple_test_f == is_gimple_call_addr
11278 || gimple_test_f == is_gimple_condexpr
11279 || gimple_test_f == is_gimple_mem_rhs
11280 || gimple_test_f == is_gimple_mem_rhs_or_call
11281 || gimple_test_f == is_gimple_reg_rhs
11282 || gimple_test_f == is_gimple_reg_rhs_or_call
11283 || gimple_test_f == is_gimple_asm_val
11284 || gimple_test_f == is_gimple_mem_ref_addr)
11285 gcc_assert (fallback & fb_rvalue);
11286 else if (gimple_test_f == is_gimple_min_lval
11287 || gimple_test_f == is_gimple_lvalue)
11288 gcc_assert (fallback & fb_lvalue);
11289 else if (gimple_test_f == is_gimple_addressable)
11290 gcc_assert (fallback & fb_either);
11291 else if (gimple_test_f == is_gimple_stmt)
11292 gcc_assert (fallback == fb_none);
11293 else
11295 /* We should have recognized the GIMPLE_TEST_F predicate to
11296 know what kind of fallback to use in case a temporary is
11297 needed to hold the value or address of *EXPR_P. */
11298 gcc_unreachable ();
11301 /* We used to check the predicate here and return immediately if it
11302 succeeds. This is wrong; the design is for gimplification to be
11303 idempotent, and for the predicates to only test for valid forms, not
11304 whether they are fully simplified. */
11305 if (pre_p == NULL)
11306 pre_p = &internal_pre;
11308 if (post_p == NULL)
11309 post_p = &internal_post;
11311 /* Remember the last statements added to PRE_P and POST_P. Every
11312 new statement added by the gimplification helpers needs to be
11313 annotated with location information. To centralize the
11314 responsibility, we remember the last statement that had been
11315 added to both queues before gimplifying *EXPR_P. If
11316 gimplification produces new statements in PRE_P and POST_P, those
11317 statements will be annotated with the same location information
11318 as *EXPR_P. */
11319 pre_last_gsi = gsi_last (*pre_p);
11320 post_last_gsi = gsi_last (*post_p);
11322 saved_location = input_location;
11323 if (save_expr != error_mark_node
11324 && EXPR_HAS_LOCATION (*expr_p))
11325 input_location = EXPR_LOCATION (*expr_p);
11327 /* Loop over the specific gimplifiers until the toplevel node
11328 remains the same. */
11331 /* Strip away as many useless type conversions as possible
11332 at the toplevel. */
11333 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11335 /* Remember the expr. */
11336 save_expr = *expr_p;
11338 /* Die, die, die, my darling. */
11339 if (error_operand_p (save_expr))
11341 ret = GS_ERROR;
11342 break;
11345 /* Do any language-specific gimplification. */
11346 ret = ((enum gimplify_status)
11347 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11348 if (ret == GS_OK)
11350 if (*expr_p == NULL_TREE)
11351 break;
11352 if (*expr_p != save_expr)
11353 continue;
11355 else if (ret != GS_UNHANDLED)
11356 break;
11358 /* Make sure that all the cases set 'ret' appropriately. */
11359 ret = GS_UNHANDLED;
11360 switch (TREE_CODE (*expr_p))
11362 /* First deal with the special cases. */
11364 case POSTINCREMENT_EXPR:
11365 case POSTDECREMENT_EXPR:
11366 case PREINCREMENT_EXPR:
11367 case PREDECREMENT_EXPR:
11368 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11369 fallback != fb_none,
11370 TREE_TYPE (*expr_p));
11371 break;
11373 case VIEW_CONVERT_EXPR:
11374 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11375 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11377 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11378 post_p, is_gimple_val, fb_rvalue);
11379 recalculate_side_effects (*expr_p);
11380 break;
11382 /* Fallthru. */
11384 case ARRAY_REF:
11385 case ARRAY_RANGE_REF:
11386 case REALPART_EXPR:
11387 case IMAGPART_EXPR:
11388 case COMPONENT_REF:
11389 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11390 fallback ? fallback : fb_rvalue);
11391 break;
11393 case COND_EXPR:
11394 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11396 /* C99 code may assign to an array in a structure value of a
11397 conditional expression, and this has undefined behavior
11398 only on execution, so create a temporary if an lvalue is
11399 required. */
11400 if (fallback == fb_lvalue)
11402 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11403 mark_addressable (*expr_p);
11404 ret = GS_OK;
11406 break;
11408 case CALL_EXPR:
11409 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11411 /* C99 code may assign to an array in a structure returned
11412 from a function, and this has undefined behavior only on
11413 execution, so create a temporary if an lvalue is
11414 required. */
11415 if (fallback == fb_lvalue)
11417 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11418 mark_addressable (*expr_p);
11419 ret = GS_OK;
11421 break;
11423 case TREE_LIST:
11424 gcc_unreachable ();
11426 case COMPOUND_EXPR:
11427 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11428 break;
11430 case COMPOUND_LITERAL_EXPR:
11431 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11432 gimple_test_f, fallback);
11433 break;
11435 case MODIFY_EXPR:
11436 case INIT_EXPR:
11437 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11438 fallback != fb_none);
11439 break;
11441 case TRUTH_ANDIF_EXPR:
11442 case TRUTH_ORIF_EXPR:
11444 /* Preserve the original type of the expression and the
11445 source location of the outer expression. */
11446 tree org_type = TREE_TYPE (*expr_p);
11447 *expr_p = gimple_boolify (*expr_p);
11448 *expr_p = build3_loc (input_location, COND_EXPR,
11449 org_type, *expr_p,
11450 fold_convert_loc
11451 (input_location,
11452 org_type, boolean_true_node),
11453 fold_convert_loc
11454 (input_location,
11455 org_type, boolean_false_node));
11456 ret = GS_OK;
11457 break;
11460 case TRUTH_NOT_EXPR:
11462 tree type = TREE_TYPE (*expr_p);
11463 /* The parsers are careful to generate TRUTH_NOT_EXPR
11464 only with operands that are always zero or one.
11465 We do not fold here but handle the only interesting case
11466 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11467 *expr_p = gimple_boolify (*expr_p);
11468 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11469 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11470 TREE_TYPE (*expr_p),
11471 TREE_OPERAND (*expr_p, 0));
11472 else
11473 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11474 TREE_TYPE (*expr_p),
11475 TREE_OPERAND (*expr_p, 0),
11476 build_int_cst (TREE_TYPE (*expr_p), 1));
11477 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11478 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11479 ret = GS_OK;
11480 break;
11483 case ADDR_EXPR:
11484 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11485 break;
11487 case ANNOTATE_EXPR:
11489 tree cond = TREE_OPERAND (*expr_p, 0);
11490 tree kind = TREE_OPERAND (*expr_p, 1);
11491 tree data = TREE_OPERAND (*expr_p, 2);
11492 tree type = TREE_TYPE (cond);
11493 if (!INTEGRAL_TYPE_P (type))
11495 *expr_p = cond;
11496 ret = GS_OK;
11497 break;
11499 tree tmp = create_tmp_var (type);
11500 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11501 gcall *call
11502 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11503 gimple_call_set_lhs (call, tmp);
11504 gimplify_seq_add_stmt (pre_p, call);
11505 *expr_p = tmp;
11506 ret = GS_ALL_DONE;
11507 break;
11510 case VA_ARG_EXPR:
11511 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11512 break;
11514 CASE_CONVERT:
11515 if (IS_EMPTY_STMT (*expr_p))
11517 ret = GS_ALL_DONE;
11518 break;
11521 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11522 || fallback == fb_none)
11524 /* Just strip a conversion to void (or in void context) and
11525 try again. */
11526 *expr_p = TREE_OPERAND (*expr_p, 0);
11527 ret = GS_OK;
11528 break;
11531 ret = gimplify_conversion (expr_p);
11532 if (ret == GS_ERROR)
11533 break;
11534 if (*expr_p != save_expr)
11535 break;
11536 /* FALLTHRU */
11538 case FIX_TRUNC_EXPR:
11539 /* unary_expr: ... | '(' cast ')' val | ... */
11540 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11541 is_gimple_val, fb_rvalue);
11542 recalculate_side_effects (*expr_p);
11543 break;
11545 case INDIRECT_REF:
11547 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11548 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11549 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11551 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11552 if (*expr_p != save_expr)
11554 ret = GS_OK;
11555 break;
11558 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11559 is_gimple_reg, fb_rvalue);
11560 if (ret == GS_ERROR)
11561 break;
11563 recalculate_side_effects (*expr_p);
11564 *expr_p = fold_build2_loc (input_location, MEM_REF,
11565 TREE_TYPE (*expr_p),
11566 TREE_OPERAND (*expr_p, 0),
11567 build_int_cst (saved_ptr_type, 0));
11568 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11569 TREE_THIS_NOTRAP (*expr_p) = notrap;
11570 ret = GS_OK;
11571 break;
11574 /* We arrive here through the various re-gimplifcation paths. */
11575 case MEM_REF:
11576 /* First try re-folding the whole thing. */
11577 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11578 TREE_OPERAND (*expr_p, 0),
11579 TREE_OPERAND (*expr_p, 1));
11580 if (tmp)
11582 REF_REVERSE_STORAGE_ORDER (tmp)
11583 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11584 *expr_p = tmp;
11585 recalculate_side_effects (*expr_p);
11586 ret = GS_OK;
11587 break;
11589 /* Avoid re-gimplifying the address operand if it is already
11590 in suitable form. Re-gimplifying would mark the address
11591 operand addressable. Always gimplify when not in SSA form
11592 as we still may have to gimplify decls with value-exprs. */
11593 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11594 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11596 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11597 is_gimple_mem_ref_addr, fb_rvalue);
11598 if (ret == GS_ERROR)
11599 break;
11601 recalculate_side_effects (*expr_p);
11602 ret = GS_ALL_DONE;
11603 break;
11605 /* Constants need not be gimplified. */
11606 case INTEGER_CST:
11607 case REAL_CST:
11608 case FIXED_CST:
11609 case STRING_CST:
11610 case COMPLEX_CST:
11611 case VECTOR_CST:
11612 /* Drop the overflow flag on constants, we do not want
11613 that in the GIMPLE IL. */
11614 if (TREE_OVERFLOW_P (*expr_p))
11615 *expr_p = drop_tree_overflow (*expr_p);
11616 ret = GS_ALL_DONE;
11617 break;
11619 case CONST_DECL:
11620 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11621 CONST_DECL node. Otherwise the decl is replaceable by its
11622 value. */
11623 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11624 if (fallback & fb_lvalue)
11625 ret = GS_ALL_DONE;
11626 else
11628 *expr_p = DECL_INITIAL (*expr_p);
11629 ret = GS_OK;
11631 break;
11633 case DECL_EXPR:
11634 ret = gimplify_decl_expr (expr_p, pre_p);
11635 break;
11637 case BIND_EXPR:
11638 ret = gimplify_bind_expr (expr_p, pre_p);
11639 break;
11641 case LOOP_EXPR:
11642 ret = gimplify_loop_expr (expr_p, pre_p);
11643 break;
11645 case SWITCH_EXPR:
11646 ret = gimplify_switch_expr (expr_p, pre_p);
11647 break;
11649 case EXIT_EXPR:
11650 ret = gimplify_exit_expr (expr_p);
11651 break;
11653 case GOTO_EXPR:
11654 /* If the target is not LABEL, then it is a computed jump
11655 and the target needs to be gimplified. */
11656 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11658 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11659 NULL, is_gimple_val, fb_rvalue);
11660 if (ret == GS_ERROR)
11661 break;
11663 gimplify_seq_add_stmt (pre_p,
11664 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11665 ret = GS_ALL_DONE;
11666 break;
11668 case PREDICT_EXPR:
11669 gimplify_seq_add_stmt (pre_p,
11670 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11671 PREDICT_EXPR_OUTCOME (*expr_p)));
11672 ret = GS_ALL_DONE;
11673 break;
11675 case LABEL_EXPR:
11676 ret = gimplify_label_expr (expr_p, pre_p);
11677 label = LABEL_EXPR_LABEL (*expr_p);
11678 gcc_assert (decl_function_context (label) == current_function_decl);
11680 /* If the label is used in a goto statement, or address of the label
11681 is taken, we need to unpoison all variables that were seen so far.
11682 Doing so would prevent us from reporting a false positives. */
11683 if (asan_poisoned_variables
11684 && asan_used_labels != NULL
11685 && asan_used_labels->contains (label))
11686 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11687 break;
11689 case CASE_LABEL_EXPR:
11690 ret = gimplify_case_label_expr (expr_p, pre_p);
11692 if (gimplify_ctxp->live_switch_vars)
11693 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11694 pre_p);
11695 break;
11697 case RETURN_EXPR:
11698 ret = gimplify_return_expr (*expr_p, pre_p);
11699 break;
11701 case CONSTRUCTOR:
11702 /* Don't reduce this in place; let gimplify_init_constructor work its
11703 magic. Buf if we're just elaborating this for side effects, just
11704 gimplify any element that has side-effects. */
11705 if (fallback == fb_none)
11707 unsigned HOST_WIDE_INT ix;
11708 tree val;
11709 tree temp = NULL_TREE;
11710 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11711 if (TREE_SIDE_EFFECTS (val))
11712 append_to_statement_list (val, &temp);
11714 *expr_p = temp;
11715 ret = temp ? GS_OK : GS_ALL_DONE;
11717 /* C99 code may assign to an array in a constructed
11718 structure or union, and this has undefined behavior only
11719 on execution, so create a temporary if an lvalue is
11720 required. */
11721 else if (fallback == fb_lvalue)
11723 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11724 mark_addressable (*expr_p);
11725 ret = GS_OK;
11727 else
11728 ret = GS_ALL_DONE;
11729 break;
11731 /* The following are special cases that are not handled by the
11732 original GIMPLE grammar. */
11734 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11735 eliminated. */
11736 case SAVE_EXPR:
11737 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11738 break;
11740 case BIT_FIELD_REF:
11741 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11742 post_p, is_gimple_lvalue, fb_either);
11743 recalculate_side_effects (*expr_p);
11744 break;
11746 case TARGET_MEM_REF:
11748 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11750 if (TMR_BASE (*expr_p))
11751 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11752 post_p, is_gimple_mem_ref_addr, fb_either);
11753 if (TMR_INDEX (*expr_p))
11754 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11755 post_p, is_gimple_val, fb_rvalue);
11756 if (TMR_INDEX2 (*expr_p))
11757 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11758 post_p, is_gimple_val, fb_rvalue);
11759 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11760 ret = MIN (r0, r1);
11762 break;
11764 case NON_LVALUE_EXPR:
11765 /* This should have been stripped above. */
11766 gcc_unreachable ();
11768 case ASM_EXPR:
11769 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11770 break;
11772 case TRY_FINALLY_EXPR:
11773 case TRY_CATCH_EXPR:
11775 gimple_seq eval, cleanup;
11776 gtry *try_;
11778 /* Calls to destructors are generated automatically in FINALLY/CATCH
11779 block. They should have location as UNKNOWN_LOCATION. However,
11780 gimplify_call_expr will reset these call stmts to input_location
11781 if it finds stmt's location is unknown. To prevent resetting for
11782 destructors, we set the input_location to unknown.
11783 Note that this only affects the destructor calls in FINALLY/CATCH
11784 block, and will automatically reset to its original value by the
11785 end of gimplify_expr. */
11786 input_location = UNKNOWN_LOCATION;
11787 eval = cleanup = NULL;
11788 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11789 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11790 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11791 if (gimple_seq_empty_p (cleanup))
11793 gimple_seq_add_seq (pre_p, eval);
11794 ret = GS_ALL_DONE;
11795 break;
11797 try_ = gimple_build_try (eval, cleanup,
11798 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11799 ? GIMPLE_TRY_FINALLY
11800 : GIMPLE_TRY_CATCH);
11801 if (EXPR_HAS_LOCATION (save_expr))
11802 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11803 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11804 gimple_set_location (try_, saved_location);
11805 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11806 gimple_try_set_catch_is_cleanup (try_,
11807 TRY_CATCH_IS_CLEANUP (*expr_p));
11808 gimplify_seq_add_stmt (pre_p, try_);
11809 ret = GS_ALL_DONE;
11810 break;
11813 case CLEANUP_POINT_EXPR:
11814 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11815 break;
11817 case TARGET_EXPR:
11818 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11819 break;
11821 case CATCH_EXPR:
11823 gimple *c;
11824 gimple_seq handler = NULL;
11825 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11826 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11827 gimplify_seq_add_stmt (pre_p, c);
11828 ret = GS_ALL_DONE;
11829 break;
11832 case EH_FILTER_EXPR:
11834 gimple *ehf;
11835 gimple_seq failure = NULL;
11837 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11838 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11839 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11840 gimplify_seq_add_stmt (pre_p, ehf);
11841 ret = GS_ALL_DONE;
11842 break;
11845 case OBJ_TYPE_REF:
11847 enum gimplify_status r0, r1;
11848 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11849 post_p, is_gimple_val, fb_rvalue);
11850 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11851 post_p, is_gimple_val, fb_rvalue);
11852 TREE_SIDE_EFFECTS (*expr_p) = 0;
11853 ret = MIN (r0, r1);
11855 break;
11857 case LABEL_DECL:
11858 /* We get here when taking the address of a label. We mark
11859 the label as "forced"; meaning it can never be removed and
11860 it is a potential target for any computed goto. */
11861 FORCED_LABEL (*expr_p) = 1;
11862 ret = GS_ALL_DONE;
11863 break;
11865 case STATEMENT_LIST:
11866 ret = gimplify_statement_list (expr_p, pre_p);
11867 break;
11869 case WITH_SIZE_EXPR:
11871 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11872 post_p == &internal_post ? NULL : post_p,
11873 gimple_test_f, fallback);
11874 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11875 is_gimple_val, fb_rvalue);
11876 ret = GS_ALL_DONE;
11878 break;
11880 case VAR_DECL:
11881 case PARM_DECL:
11882 ret = gimplify_var_or_parm_decl (expr_p);
11883 break;
11885 case RESULT_DECL:
11886 /* When within an OMP context, notice uses of variables. */
11887 if (gimplify_omp_ctxp)
11888 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11889 ret = GS_ALL_DONE;
11890 break;
11892 case DEBUG_EXPR_DECL:
11893 gcc_unreachable ();
11895 case DEBUG_BEGIN_STMT:
11896 gimplify_seq_add_stmt (pre_p,
11897 gimple_build_debug_begin_stmt
11898 (TREE_BLOCK (*expr_p),
11899 EXPR_LOCATION (*expr_p)));
11900 ret = GS_ALL_DONE;
11901 *expr_p = NULL;
11902 break;
11904 case SSA_NAME:
11905 /* Allow callbacks into the gimplifier during optimization. */
11906 ret = GS_ALL_DONE;
11907 break;
11909 case OMP_PARALLEL:
11910 gimplify_omp_parallel (expr_p, pre_p);
11911 ret = GS_ALL_DONE;
11912 break;
11914 case OMP_TASK:
11915 gimplify_omp_task (expr_p, pre_p);
11916 ret = GS_ALL_DONE;
11917 break;
11919 case OMP_FOR:
11920 case OMP_SIMD:
11921 case OMP_DISTRIBUTE:
11922 case OMP_TASKLOOP:
11923 case OACC_LOOP:
11924 ret = gimplify_omp_for (expr_p, pre_p);
11925 break;
11927 case OACC_CACHE:
11928 gimplify_oacc_cache (expr_p, pre_p);
11929 ret = GS_ALL_DONE;
11930 break;
11932 case OACC_DECLARE:
11933 gimplify_oacc_declare (expr_p, pre_p);
11934 ret = GS_ALL_DONE;
11935 break;
11937 case OACC_HOST_DATA:
11938 case OACC_DATA:
11939 case OACC_KERNELS:
11940 case OACC_PARALLEL:
11941 case OMP_SECTIONS:
11942 case OMP_SINGLE:
11943 case OMP_TARGET:
11944 case OMP_TARGET_DATA:
11945 case OMP_TEAMS:
11946 gimplify_omp_workshare (expr_p, pre_p);
11947 ret = GS_ALL_DONE;
11948 break;
11950 case OACC_ENTER_DATA:
11951 case OACC_EXIT_DATA:
11952 case OACC_UPDATE:
11953 case OMP_TARGET_UPDATE:
11954 case OMP_TARGET_ENTER_DATA:
11955 case OMP_TARGET_EXIT_DATA:
11956 gimplify_omp_target_update (expr_p, pre_p);
11957 ret = GS_ALL_DONE;
11958 break;
11960 case OMP_SECTION:
11961 case OMP_MASTER:
11962 case OMP_TASKGROUP:
11963 case OMP_ORDERED:
11964 case OMP_CRITICAL:
11966 gimple_seq body = NULL;
11967 gimple *g;
11969 gimplify_and_add (OMP_BODY (*expr_p), &body);
11970 switch (TREE_CODE (*expr_p))
11972 case OMP_SECTION:
11973 g = gimple_build_omp_section (body);
11974 break;
11975 case OMP_MASTER:
11976 g = gimple_build_omp_master (body);
11977 break;
11978 case OMP_TASKGROUP:
11980 gimple_seq cleanup = NULL;
11981 tree fn
11982 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11983 g = gimple_build_call (fn, 0);
11984 gimple_seq_add_stmt (&cleanup, g);
11985 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11986 body = NULL;
11987 gimple_seq_add_stmt (&body, g);
11988 g = gimple_build_omp_taskgroup (body);
11990 break;
11991 case OMP_ORDERED:
11992 g = gimplify_omp_ordered (*expr_p, body);
11993 break;
11994 case OMP_CRITICAL:
11995 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11996 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11997 gimplify_adjust_omp_clauses (pre_p, body,
11998 &OMP_CRITICAL_CLAUSES (*expr_p),
11999 OMP_CRITICAL);
12000 g = gimple_build_omp_critical (body,
12001 OMP_CRITICAL_NAME (*expr_p),
12002 OMP_CRITICAL_CLAUSES (*expr_p));
12003 break;
12004 default:
12005 gcc_unreachable ();
12007 gimplify_seq_add_stmt (pre_p, g);
12008 ret = GS_ALL_DONE;
12009 break;
12012 case OMP_ATOMIC:
12013 case OMP_ATOMIC_READ:
12014 case OMP_ATOMIC_CAPTURE_OLD:
12015 case OMP_ATOMIC_CAPTURE_NEW:
12016 ret = gimplify_omp_atomic (expr_p, pre_p);
12017 break;
12019 case TRANSACTION_EXPR:
12020 ret = gimplify_transaction (expr_p, pre_p);
12021 break;
12023 case TRUTH_AND_EXPR:
12024 case TRUTH_OR_EXPR:
12025 case TRUTH_XOR_EXPR:
12027 tree orig_type = TREE_TYPE (*expr_p);
12028 tree new_type, xop0, xop1;
12029 *expr_p = gimple_boolify (*expr_p);
12030 new_type = TREE_TYPE (*expr_p);
12031 if (!useless_type_conversion_p (orig_type, new_type))
12033 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12034 ret = GS_OK;
12035 break;
12038 /* Boolified binary truth expressions are semantically equivalent
12039 to bitwise binary expressions. Canonicalize them to the
12040 bitwise variant. */
12041 switch (TREE_CODE (*expr_p))
12043 case TRUTH_AND_EXPR:
12044 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12045 break;
12046 case TRUTH_OR_EXPR:
12047 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12048 break;
12049 case TRUTH_XOR_EXPR:
12050 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12051 break;
12052 default:
12053 break;
12055 /* Now make sure that operands have compatible type to
12056 expression's new_type. */
12057 xop0 = TREE_OPERAND (*expr_p, 0);
12058 xop1 = TREE_OPERAND (*expr_p, 1);
12059 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12060 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12061 new_type,
12062 xop0);
12063 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12064 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12065 new_type,
12066 xop1);
12067 /* Continue classified as tcc_binary. */
12068 goto expr_2;
12071 case VEC_COND_EXPR:
12073 enum gimplify_status r0, r1, r2;
12075 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12076 post_p, is_gimple_condexpr, fb_rvalue);
12077 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12078 post_p, is_gimple_val, fb_rvalue);
12079 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12080 post_p, is_gimple_val, fb_rvalue);
12082 ret = MIN (MIN (r0, r1), r2);
12083 recalculate_side_effects (*expr_p);
12085 break;
12087 case FMA_EXPR:
12088 case VEC_PERM_EXPR:
12089 /* Classified as tcc_expression. */
12090 goto expr_3;
12092 case BIT_INSERT_EXPR:
12093 /* Argument 3 is a constant. */
12094 goto expr_2;
12096 case POINTER_PLUS_EXPR:
12098 enum gimplify_status r0, r1;
12099 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12100 post_p, is_gimple_val, fb_rvalue);
12101 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12102 post_p, is_gimple_val, fb_rvalue);
12103 recalculate_side_effects (*expr_p);
12104 ret = MIN (r0, r1);
12105 break;
12108 default:
12109 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12111 case tcc_comparison:
12112 /* Handle comparison of objects of non scalar mode aggregates
12113 with a call to memcmp. It would be nice to only have to do
12114 this for variable-sized objects, but then we'd have to allow
12115 the same nest of reference nodes we allow for MODIFY_EXPR and
12116 that's too complex.
12118 Compare scalar mode aggregates as scalar mode values. Using
12119 memcmp for them would be very inefficient at best, and is
12120 plain wrong if bitfields are involved. */
12122 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12124 /* Vector comparisons need no boolification. */
12125 if (TREE_CODE (type) == VECTOR_TYPE)
12126 goto expr_2;
12127 else if (!AGGREGATE_TYPE_P (type))
12129 tree org_type = TREE_TYPE (*expr_p);
12130 *expr_p = gimple_boolify (*expr_p);
12131 if (!useless_type_conversion_p (org_type,
12132 TREE_TYPE (*expr_p)))
12134 *expr_p = fold_convert_loc (input_location,
12135 org_type, *expr_p);
12136 ret = GS_OK;
12138 else
12139 goto expr_2;
12141 else if (TYPE_MODE (type) != BLKmode)
12142 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12143 else
12144 ret = gimplify_variable_sized_compare (expr_p);
12146 break;
12149 /* If *EXPR_P does not need to be special-cased, handle it
12150 according to its class. */
12151 case tcc_unary:
12152 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12153 post_p, is_gimple_val, fb_rvalue);
12154 break;
12156 case tcc_binary:
12157 expr_2:
12159 enum gimplify_status r0, r1;
12161 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12162 post_p, is_gimple_val, fb_rvalue);
12163 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12164 post_p, is_gimple_val, fb_rvalue);
12166 ret = MIN (r0, r1);
12167 break;
12170 expr_3:
12172 enum gimplify_status r0, r1, r2;
12174 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12175 post_p, is_gimple_val, fb_rvalue);
12176 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12177 post_p, is_gimple_val, fb_rvalue);
12178 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12179 post_p, is_gimple_val, fb_rvalue);
12181 ret = MIN (MIN (r0, r1), r2);
12182 break;
12185 case tcc_declaration:
12186 case tcc_constant:
12187 ret = GS_ALL_DONE;
12188 goto dont_recalculate;
12190 default:
12191 gcc_unreachable ();
12194 recalculate_side_effects (*expr_p);
12196 dont_recalculate:
12197 break;
12200 gcc_assert (*expr_p || ret != GS_OK);
12202 while (ret == GS_OK);
12204 /* If we encountered an error_mark somewhere nested inside, either
12205 stub out the statement or propagate the error back out. */
12206 if (ret == GS_ERROR)
12208 if (is_statement)
12209 *expr_p = NULL;
12210 goto out;
12213 /* This was only valid as a return value from the langhook, which
12214 we handled. Make sure it doesn't escape from any other context. */
12215 gcc_assert (ret != GS_UNHANDLED);
12217 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12219 /* We aren't looking for a value, and we don't have a valid
12220 statement. If it doesn't have side-effects, throw it away.
12221 We can also get here with code such as "*&&L;", where L is
12222 a LABEL_DECL that is marked as FORCED_LABEL. */
12223 if (TREE_CODE (*expr_p) == LABEL_DECL
12224 || !TREE_SIDE_EFFECTS (*expr_p))
12225 *expr_p = NULL;
12226 else if (!TREE_THIS_VOLATILE (*expr_p))
12228 /* This is probably a _REF that contains something nested that
12229 has side effects. Recurse through the operands to find it. */
12230 enum tree_code code = TREE_CODE (*expr_p);
12232 switch (code)
12234 case COMPONENT_REF:
12235 case REALPART_EXPR:
12236 case IMAGPART_EXPR:
12237 case VIEW_CONVERT_EXPR:
12238 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12239 gimple_test_f, fallback);
12240 break;
12242 case ARRAY_REF:
12243 case ARRAY_RANGE_REF:
12244 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12245 gimple_test_f, fallback);
12246 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12247 gimple_test_f, fallback);
12248 break;
12250 default:
12251 /* Anything else with side-effects must be converted to
12252 a valid statement before we get here. */
12253 gcc_unreachable ();
12256 *expr_p = NULL;
12258 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12259 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12261 /* Historically, the compiler has treated a bare reference
12262 to a non-BLKmode volatile lvalue as forcing a load. */
12263 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12265 /* Normally, we do not want to create a temporary for a
12266 TREE_ADDRESSABLE type because such a type should not be
12267 copied by bitwise-assignment. However, we make an
12268 exception here, as all we are doing here is ensuring that
12269 we read the bytes that make up the type. We use
12270 create_tmp_var_raw because create_tmp_var will abort when
12271 given a TREE_ADDRESSABLE type. */
12272 tree tmp = create_tmp_var_raw (type, "vol");
12273 gimple_add_tmp_var (tmp);
12274 gimplify_assign (tmp, *expr_p, pre_p);
12275 *expr_p = NULL;
12277 else
12278 /* We can't do anything useful with a volatile reference to
12279 an incomplete type, so just throw it away. Likewise for
12280 a BLKmode type, since any implicit inner load should
12281 already have been turned into an explicit one by the
12282 gimplification process. */
12283 *expr_p = NULL;
12286 /* If we are gimplifying at the statement level, we're done. Tack
12287 everything together and return. */
12288 if (fallback == fb_none || is_statement)
12290 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12291 it out for GC to reclaim it. */
12292 *expr_p = NULL_TREE;
12294 if (!gimple_seq_empty_p (internal_pre)
12295 || !gimple_seq_empty_p (internal_post))
12297 gimplify_seq_add_seq (&internal_pre, internal_post);
12298 gimplify_seq_add_seq (pre_p, internal_pre);
12301 /* The result of gimplifying *EXPR_P is going to be the last few
12302 statements in *PRE_P and *POST_P. Add location information
12303 to all the statements that were added by the gimplification
12304 helpers. */
12305 if (!gimple_seq_empty_p (*pre_p))
12306 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12308 if (!gimple_seq_empty_p (*post_p))
12309 annotate_all_with_location_after (*post_p, post_last_gsi,
12310 input_location);
12312 goto out;
12315 #ifdef ENABLE_GIMPLE_CHECKING
12316 if (*expr_p)
12318 enum tree_code code = TREE_CODE (*expr_p);
12319 /* These expressions should already be in gimple IR form. */
12320 gcc_assert (code != MODIFY_EXPR
12321 && code != ASM_EXPR
12322 && code != BIND_EXPR
12323 && code != CATCH_EXPR
12324 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12325 && code != EH_FILTER_EXPR
12326 && code != GOTO_EXPR
12327 && code != LABEL_EXPR
12328 && code != LOOP_EXPR
12329 && code != SWITCH_EXPR
12330 && code != TRY_FINALLY_EXPR
12331 && code != OACC_PARALLEL
12332 && code != OACC_KERNELS
12333 && code != OACC_DATA
12334 && code != OACC_HOST_DATA
12335 && code != OACC_DECLARE
12336 && code != OACC_UPDATE
12337 && code != OACC_ENTER_DATA
12338 && code != OACC_EXIT_DATA
12339 && code != OACC_CACHE
12340 && code != OMP_CRITICAL
12341 && code != OMP_FOR
12342 && code != OACC_LOOP
12343 && code != OMP_MASTER
12344 && code != OMP_TASKGROUP
12345 && code != OMP_ORDERED
12346 && code != OMP_PARALLEL
12347 && code != OMP_SECTIONS
12348 && code != OMP_SECTION
12349 && code != OMP_SINGLE);
12351 #endif
12353 /* Otherwise we're gimplifying a subexpression, so the resulting
12354 value is interesting. If it's a valid operand that matches
12355 GIMPLE_TEST_F, we're done. Unless we are handling some
12356 post-effects internally; if that's the case, we need to copy into
12357 a temporary before adding the post-effects to POST_P. */
12358 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12359 goto out;
12361 /* Otherwise, we need to create a new temporary for the gimplified
12362 expression. */
12364 /* We can't return an lvalue if we have an internal postqueue. The
12365 object the lvalue refers to would (probably) be modified by the
12366 postqueue; we need to copy the value out first, which means an
12367 rvalue. */
12368 if ((fallback & fb_lvalue)
12369 && gimple_seq_empty_p (internal_post)
12370 && is_gimple_addressable (*expr_p))
12372 /* An lvalue will do. Take the address of the expression, store it
12373 in a temporary, and replace the expression with an INDIRECT_REF of
12374 that temporary. */
12375 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12376 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12377 *expr_p = build_simple_mem_ref (tmp);
12379 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12381 /* An rvalue will do. Assign the gimplified expression into a
12382 new temporary TMP and replace the original expression with
12383 TMP. First, make sure that the expression has a type so that
12384 it can be assigned into a temporary. */
12385 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12386 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12388 else
12390 #ifdef ENABLE_GIMPLE_CHECKING
12391 if (!(fallback & fb_mayfail))
12393 fprintf (stderr, "gimplification failed:\n");
12394 print_generic_expr (stderr, *expr_p);
12395 debug_tree (*expr_p);
12396 internal_error ("gimplification failed");
12398 #endif
12399 gcc_assert (fallback & fb_mayfail);
12401 /* If this is an asm statement, and the user asked for the
12402 impossible, don't die. Fail and let gimplify_asm_expr
12403 issue an error. */
12404 ret = GS_ERROR;
12405 goto out;
12408 /* Make sure the temporary matches our predicate. */
12409 gcc_assert ((*gimple_test_f) (*expr_p));
12411 if (!gimple_seq_empty_p (internal_post))
12413 annotate_all_with_location (internal_post, input_location);
12414 gimplify_seq_add_seq (pre_p, internal_post);
12417 out:
12418 input_location = saved_location;
12419 return ret;
12422 /* Like gimplify_expr but make sure the gimplified result is not itself
12423 a SSA name (but a decl if it were). Temporaries required by
12424 evaluating *EXPR_P may be still SSA names. */
12426 static enum gimplify_status
12427 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12428 bool (*gimple_test_f) (tree), fallback_t fallback,
12429 bool allow_ssa)
12431 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12432 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12433 gimple_test_f, fallback);
12434 if (! allow_ssa
12435 && TREE_CODE (*expr_p) == SSA_NAME)
12437 tree name = *expr_p;
12438 if (was_ssa_name_p)
12439 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12440 else
12442 /* Avoid the extra copy if possible. */
12443 *expr_p = create_tmp_reg (TREE_TYPE (name));
12444 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12445 release_ssa_name (name);
12448 return ret;
12451 /* Look through TYPE for variable-sized objects and gimplify each such
12452 size that we find. Add to LIST_P any statements generated. */
12454 void
12455 gimplify_type_sizes (tree type, gimple_seq *list_p)
12457 tree field, t;
12459 if (type == NULL || type == error_mark_node)
12460 return;
12462 /* We first do the main variant, then copy into any other variants. */
12463 type = TYPE_MAIN_VARIANT (type);
12465 /* Avoid infinite recursion. */
12466 if (TYPE_SIZES_GIMPLIFIED (type))
12467 return;
12469 TYPE_SIZES_GIMPLIFIED (type) = 1;
12471 switch (TREE_CODE (type))
12473 case INTEGER_TYPE:
12474 case ENUMERAL_TYPE:
12475 case BOOLEAN_TYPE:
12476 case REAL_TYPE:
12477 case FIXED_POINT_TYPE:
12478 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12479 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12481 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12483 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12484 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12486 break;
12488 case ARRAY_TYPE:
12489 /* These types may not have declarations, so handle them here. */
12490 gimplify_type_sizes (TREE_TYPE (type), list_p);
12491 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12492 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12493 with assigned stack slots, for -O1+ -g they should be tracked
12494 by VTA. */
12495 if (!(TYPE_NAME (type)
12496 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12497 && DECL_IGNORED_P (TYPE_NAME (type)))
12498 && TYPE_DOMAIN (type)
12499 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12501 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12502 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12503 DECL_IGNORED_P (t) = 0;
12504 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12505 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12506 DECL_IGNORED_P (t) = 0;
12508 break;
12510 case RECORD_TYPE:
12511 case UNION_TYPE:
12512 case QUAL_UNION_TYPE:
12513 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12514 if (TREE_CODE (field) == FIELD_DECL)
12516 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12517 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12518 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12519 gimplify_type_sizes (TREE_TYPE (field), list_p);
12521 break;
12523 case POINTER_TYPE:
12524 case REFERENCE_TYPE:
12525 /* We used to recurse on the pointed-to type here, which turned out to
12526 be incorrect because its definition might refer to variables not
12527 yet initialized at this point if a forward declaration is involved.
12529 It was actually useful for anonymous pointed-to types to ensure
12530 that the sizes evaluation dominates every possible later use of the
12531 values. Restricting to such types here would be safe since there
12532 is no possible forward declaration around, but would introduce an
12533 undesirable middle-end semantic to anonymity. We then defer to
12534 front-ends the responsibility of ensuring that the sizes are
12535 evaluated both early and late enough, e.g. by attaching artificial
12536 type declarations to the tree. */
12537 break;
12539 default:
12540 break;
12543 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12544 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12546 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12548 TYPE_SIZE (t) = TYPE_SIZE (type);
12549 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12550 TYPE_SIZES_GIMPLIFIED (t) = 1;
12554 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12555 a size or position, has had all of its SAVE_EXPRs evaluated.
12556 We add any required statements to *STMT_P. */
12558 void
12559 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12561 tree expr = *expr_p;
12563 /* We don't do anything if the value isn't there, is constant, or contains
12564 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12565 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12566 will want to replace it with a new variable, but that will cause problems
12567 if this type is from outside the function. It's OK to have that here. */
12568 if (expr == NULL_TREE
12569 || is_gimple_constant (expr)
12570 || TREE_CODE (expr) == VAR_DECL
12571 || CONTAINS_PLACEHOLDER_P (expr))
12572 return;
12574 *expr_p = unshare_expr (expr);
12576 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12577 if the def vanishes. */
12578 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12580 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12581 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12582 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12583 if (is_gimple_constant (*expr_p))
12584 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12587 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12588 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12589 is true, also gimplify the parameters. */
12591 gbind *
12592 gimplify_body (tree fndecl, bool do_parms)
12594 location_t saved_location = input_location;
12595 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12596 gimple *outer_stmt;
12597 gbind *outer_bind;
12598 struct cgraph_node *cgn;
12600 timevar_push (TV_TREE_GIMPLIFY);
12602 init_tree_ssa (cfun);
12604 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12605 gimplification. */
12606 default_rtl_profile ();
12608 gcc_assert (gimplify_ctxp == NULL);
12609 push_gimplify_context (true);
12611 if (flag_openacc || flag_openmp)
12613 gcc_assert (gimplify_omp_ctxp == NULL);
12614 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12615 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12618 /* Unshare most shared trees in the body and in that of any nested functions.
12619 It would seem we don't have to do this for nested functions because
12620 they are supposed to be output and then the outer function gimplified
12621 first, but the g++ front end doesn't always do it that way. */
12622 unshare_body (fndecl);
12623 unvisit_body (fndecl);
12625 cgn = cgraph_node::get (fndecl);
12626 if (cgn && cgn->origin)
12627 nonlocal_vlas = new hash_set<tree>;
12629 /* Make sure input_location isn't set to something weird. */
12630 input_location = DECL_SOURCE_LOCATION (fndecl);
12632 /* Resolve callee-copies. This has to be done before processing
12633 the body so that DECL_VALUE_EXPR gets processed correctly. */
12634 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12636 /* Gimplify the function's body. */
12637 seq = NULL;
12638 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12639 outer_stmt = gimple_seq_first_stmt (seq);
12640 if (!outer_stmt)
12642 outer_stmt = gimple_build_nop ();
12643 gimplify_seq_add_stmt (&seq, outer_stmt);
12646 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12647 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12648 if (gimple_code (outer_stmt) == GIMPLE_BIND
12649 && gimple_seq_first (seq) == gimple_seq_last (seq))
12650 outer_bind = as_a <gbind *> (outer_stmt);
12651 else
12652 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12654 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12656 /* If we had callee-copies statements, insert them at the beginning
12657 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12658 if (!gimple_seq_empty_p (parm_stmts))
12660 tree parm;
12662 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12663 if (parm_cleanup)
12665 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12666 GIMPLE_TRY_FINALLY);
12667 parm_stmts = NULL;
12668 gimple_seq_add_stmt (&parm_stmts, g);
12670 gimple_bind_set_body (outer_bind, parm_stmts);
12672 for (parm = DECL_ARGUMENTS (current_function_decl);
12673 parm; parm = DECL_CHAIN (parm))
12674 if (DECL_HAS_VALUE_EXPR_P (parm))
12676 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12677 DECL_IGNORED_P (parm) = 0;
12681 if (nonlocal_vlas)
12683 if (nonlocal_vla_vars)
12685 /* tree-nested.c may later on call declare_vars (..., true);
12686 which relies on BLOCK_VARS chain to be the tail of the
12687 gimple_bind_vars chain. Ensure we don't violate that
12688 assumption. */
12689 if (gimple_bind_block (outer_bind)
12690 == DECL_INITIAL (current_function_decl))
12691 declare_vars (nonlocal_vla_vars, outer_bind, true);
12692 else
12693 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12694 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12695 nonlocal_vla_vars);
12696 nonlocal_vla_vars = NULL_TREE;
12698 delete nonlocal_vlas;
12699 nonlocal_vlas = NULL;
12702 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12703 && gimplify_omp_ctxp)
12705 delete_omp_context (gimplify_omp_ctxp);
12706 gimplify_omp_ctxp = NULL;
12709 pop_gimplify_context (outer_bind);
12710 gcc_assert (gimplify_ctxp == NULL);
12712 if (flag_checking && !seen_error ())
12713 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12715 timevar_pop (TV_TREE_GIMPLIFY);
12716 input_location = saved_location;
12718 return outer_bind;
12721 typedef char *char_p; /* For DEF_VEC_P. */
12723 /* Return whether we should exclude FNDECL from instrumentation. */
12725 static bool
12726 flag_instrument_functions_exclude_p (tree fndecl)
12728 vec<char_p> *v;
12730 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12731 if (v && v->length () > 0)
12733 const char *name;
12734 int i;
12735 char *s;
12737 name = lang_hooks.decl_printable_name (fndecl, 0);
12738 FOR_EACH_VEC_ELT (*v, i, s)
12739 if (strstr (name, s) != NULL)
12740 return true;
12743 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12744 if (v && v->length () > 0)
12746 const char *name;
12747 int i;
12748 char *s;
12750 name = DECL_SOURCE_FILE (fndecl);
12751 FOR_EACH_VEC_ELT (*v, i, s)
12752 if (strstr (name, s) != NULL)
12753 return true;
12756 return false;
12759 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12760 node for the function we want to gimplify.
12762 Return the sequence of GIMPLE statements corresponding to the body
12763 of FNDECL. */
12765 void
12766 gimplify_function_tree (tree fndecl)
12768 tree parm, ret;
12769 gimple_seq seq;
12770 gbind *bind;
12772 gcc_assert (!gimple_body (fndecl));
12774 if (DECL_STRUCT_FUNCTION (fndecl))
12775 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12776 else
12777 push_struct_function (fndecl);
12779 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12780 if necessary. */
12781 cfun->curr_properties |= PROP_gimple_lva;
12783 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12785 /* Preliminarily mark non-addressed complex variables as eligible
12786 for promotion to gimple registers. We'll transform their uses
12787 as we find them. */
12788 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12789 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12790 && !TREE_THIS_VOLATILE (parm)
12791 && !needs_to_live_in_memory (parm))
12792 DECL_GIMPLE_REG_P (parm) = 1;
12795 ret = DECL_RESULT (fndecl);
12796 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12797 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12798 && !needs_to_live_in_memory (ret))
12799 DECL_GIMPLE_REG_P (ret) = 1;
12801 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12802 asan_poisoned_variables = new hash_set<tree> ();
12803 bind = gimplify_body (fndecl, true);
12804 if (asan_poisoned_variables)
12806 delete asan_poisoned_variables;
12807 asan_poisoned_variables = NULL;
12810 /* The tree body of the function is no longer needed, replace it
12811 with the new GIMPLE body. */
12812 seq = NULL;
12813 gimple_seq_add_stmt (&seq, bind);
12814 gimple_set_body (fndecl, seq);
12816 /* If we're instrumenting function entry/exit, then prepend the call to
12817 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12818 catch the exit hook. */
12819 /* ??? Add some way to ignore exceptions for this TFE. */
12820 if (flag_instrument_function_entry_exit
12821 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12822 /* Do not instrument extern inline functions. */
12823 && !(DECL_DECLARED_INLINE_P (fndecl)
12824 && DECL_EXTERNAL (fndecl)
12825 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12826 && !flag_instrument_functions_exclude_p (fndecl))
12828 tree x;
12829 gbind *new_bind;
12830 gimple *tf;
12831 gimple_seq cleanup = NULL, body = NULL;
12832 tree tmp_var;
12833 gcall *call;
12835 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12836 call = gimple_build_call (x, 1, integer_zero_node);
12837 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12838 gimple_call_set_lhs (call, tmp_var);
12839 gimplify_seq_add_stmt (&cleanup, call);
12840 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12841 call = gimple_build_call (x, 2,
12842 build_fold_addr_expr (current_function_decl),
12843 tmp_var);
12844 gimplify_seq_add_stmt (&cleanup, call);
12845 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12847 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12848 call = gimple_build_call (x, 1, integer_zero_node);
12849 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12850 gimple_call_set_lhs (call, tmp_var);
12851 gimplify_seq_add_stmt (&body, call);
12852 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12853 call = gimple_build_call (x, 2,
12854 build_fold_addr_expr (current_function_decl),
12855 tmp_var);
12856 gimplify_seq_add_stmt (&body, call);
12857 gimplify_seq_add_stmt (&body, tf);
12858 new_bind = gimple_build_bind (NULL, body, NULL);
12860 /* Replace the current function body with the body
12861 wrapped in the try/finally TF. */
12862 seq = NULL;
12863 gimple_seq_add_stmt (&seq, new_bind);
12864 gimple_set_body (fndecl, seq);
12865 bind = new_bind;
12868 if (sanitize_flags_p (SANITIZE_THREAD))
12870 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12871 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12872 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12873 /* Replace the current function body with the body
12874 wrapped in the try/finally TF. */
12875 seq = NULL;
12876 gimple_seq_add_stmt (&seq, new_bind);
12877 gimple_set_body (fndecl, seq);
12880 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12881 cfun->curr_properties |= PROP_gimple_any;
12883 pop_cfun ();
12885 dump_function (TDI_gimple, fndecl);
12888 /* Return a dummy expression of type TYPE in order to keep going after an
12889 error. */
12891 static tree
12892 dummy_object (tree type)
12894 tree t = build_int_cst (build_pointer_type (type), 0);
12895 return build2 (MEM_REF, type, t, t);
12898 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12899 builtin function, but a very special sort of operator. */
12901 enum gimplify_status
12902 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12903 gimple_seq *post_p ATTRIBUTE_UNUSED)
12905 tree promoted_type, have_va_type;
12906 tree valist = TREE_OPERAND (*expr_p, 0);
12907 tree type = TREE_TYPE (*expr_p);
12908 tree t, tag, aptag;
12909 location_t loc = EXPR_LOCATION (*expr_p);
12911 /* Verify that valist is of the proper type. */
12912 have_va_type = TREE_TYPE (valist);
12913 if (have_va_type == error_mark_node)
12914 return GS_ERROR;
12915 have_va_type = targetm.canonical_va_list_type (have_va_type);
12916 if (have_va_type == NULL_TREE
12917 && POINTER_TYPE_P (TREE_TYPE (valist)))
12918 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12919 have_va_type
12920 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12921 gcc_assert (have_va_type != NULL_TREE);
12923 /* Generate a diagnostic for requesting data of a type that cannot
12924 be passed through `...' due to type promotion at the call site. */
12925 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12926 != type)
12928 static bool gave_help;
12929 bool warned;
12930 /* Use the expansion point to handle cases such as passing bool (defined
12931 in a system header) through `...'. */
12932 source_location xloc
12933 = expansion_point_location_if_in_system_header (loc);
12935 /* Unfortunately, this is merely undefined, rather than a constraint
12936 violation, so we cannot make this an error. If this call is never
12937 executed, the program is still strictly conforming. */
12938 warned = warning_at (xloc, 0,
12939 "%qT is promoted to %qT when passed through %<...%>",
12940 type, promoted_type);
12941 if (!gave_help && warned)
12943 gave_help = true;
12944 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12945 promoted_type, type);
12948 /* We can, however, treat "undefined" any way we please.
12949 Call abort to encourage the user to fix the program. */
12950 if (warned)
12951 inform (xloc, "if this code is reached, the program will abort");
12952 /* Before the abort, allow the evaluation of the va_list
12953 expression to exit or longjmp. */
12954 gimplify_and_add (valist, pre_p);
12955 t = build_call_expr_loc (loc,
12956 builtin_decl_implicit (BUILT_IN_TRAP), 0);
12957 gimplify_and_add (t, pre_p);
12959 /* This is dead code, but go ahead and finish so that the
12960 mode of the result comes out right. */
12961 *expr_p = dummy_object (type);
12962 return GS_ALL_DONE;
12965 tag = build_int_cst (build_pointer_type (type), 0);
12966 aptag = build_int_cst (TREE_TYPE (valist), 0);
12968 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12969 valist, tag, aptag);
12971 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12972 needs to be expanded. */
12973 cfun->curr_properties &= ~PROP_gimple_lva;
12975 return GS_OK;
12978 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12980 DST/SRC are the destination and source respectively. You can pass
12981 ungimplified trees in DST or SRC, in which case they will be
12982 converted to a gimple operand if necessary.
12984 This function returns the newly created GIMPLE_ASSIGN tuple. */
12986 gimple *
12987 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12989 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12990 gimplify_and_add (t, seq_p);
12991 ggc_free (t);
12992 return gimple_seq_last_stmt (*seq_p);
12995 inline hashval_t
12996 gimplify_hasher::hash (const elt_t *p)
12998 tree t = p->val;
12999 return iterative_hash_expr (t, 0);
13002 inline bool
13003 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13005 tree t1 = p1->val;
13006 tree t2 = p2->val;
13007 enum tree_code code = TREE_CODE (t1);
13009 if (TREE_CODE (t2) != code
13010 || TREE_TYPE (t1) != TREE_TYPE (t2))
13011 return false;
13013 if (!operand_equal_p (t1, t2, 0))
13014 return false;
13016 /* Only allow them to compare equal if they also hash equal; otherwise
13017 results are nondeterminate, and we fail bootstrap comparison. */
13018 gcc_checking_assert (hash (p1) == hash (p2));
13020 return true;