Avoid is_constant calls in vectorizable_bswap
[official-gcc.git] / gcc / gimplify.c
blobe35137aec2c7193f8e345b20cd3ec3e79334ce20
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_clobber (TREE_TYPE (t));
1383 gimple *clobber_stmt;
1384 clobber_stmt = gimple_build_assign (t, clobber);
1385 gimple_set_location (clobber_stmt, end_locus);
1386 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1389 if (flag_openacc && oacc_declare_returns != NULL)
1391 tree *c = oacc_declare_returns->get (t);
1392 if (c != NULL)
1394 if (ret_clauses)
1395 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1397 ret_clauses = *c;
1399 oacc_declare_returns->remove (t);
1401 if (oacc_declare_returns->elements () == 0)
1403 delete oacc_declare_returns;
1404 oacc_declare_returns = NULL;
1410 if (asan_poisoned_variables != NULL
1411 && asan_poisoned_variables->contains (t))
1413 asan_poisoned_variables->remove (t);
1414 asan_poison_variable (t, true, &cleanup);
1417 if (gimplify_ctxp->live_switch_vars != NULL
1418 && gimplify_ctxp->live_switch_vars->contains (t))
1419 gimplify_ctxp->live_switch_vars->remove (t);
1422 if (ret_clauses)
1424 gomp_target *stmt;
1425 gimple_stmt_iterator si = gsi_start (cleanup);
1427 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1428 ret_clauses);
1429 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1432 if (cleanup)
1434 gtry *gs;
1435 gimple_seq new_body;
1437 new_body = NULL;
1438 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1439 GIMPLE_TRY_FINALLY);
1441 if (stack_save)
1442 gimplify_seq_add_stmt (&new_body, stack_save);
1443 gimplify_seq_add_stmt (&new_body, gs);
1444 gimple_bind_set_body (bind_stmt, new_body);
1447 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1448 if (!gimplify_ctxp->keep_stack)
1449 gimplify_ctxp->keep_stack = old_keep_stack;
1450 gimplify_ctxp->save_stack = old_save_stack;
1452 gimple_pop_bind_expr ();
1454 gimplify_seq_add_stmt (pre_p, bind_stmt);
1456 if (temp)
1458 *expr_p = temp;
1459 return GS_OK;
1462 *expr_p = NULL_TREE;
1463 return GS_ALL_DONE;
1466 /* Maybe add early return predict statement to PRE_P sequence. */
1468 static void
1469 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1471 /* If we are not in a conditional context, add PREDICT statement. */
1472 if (gimple_conditional_context ())
1474 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1475 NOT_TAKEN);
1476 gimplify_seq_add_stmt (pre_p, predict);
1480 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1481 GIMPLE value, it is assigned to a new temporary and the statement is
1482 re-written to return the temporary.
1484 PRE_P points to the sequence where side effects that must happen before
1485 STMT should be stored. */
1487 static enum gimplify_status
1488 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1490 greturn *ret;
1491 tree ret_expr = TREE_OPERAND (stmt, 0);
1492 tree result_decl, result;
1494 if (ret_expr == error_mark_node)
1495 return GS_ERROR;
1497 if (!ret_expr
1498 || TREE_CODE (ret_expr) == RESULT_DECL)
1500 maybe_add_early_return_predict_stmt (pre_p);
1501 greturn *ret = gimple_build_return (ret_expr);
1502 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1503 gimplify_seq_add_stmt (pre_p, ret);
1504 return GS_ALL_DONE;
1507 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1508 result_decl = NULL_TREE;
1509 else
1511 result_decl = TREE_OPERAND (ret_expr, 0);
1513 /* See through a return by reference. */
1514 if (TREE_CODE (result_decl) == INDIRECT_REF)
1515 result_decl = TREE_OPERAND (result_decl, 0);
1517 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1518 || TREE_CODE (ret_expr) == INIT_EXPR)
1519 && TREE_CODE (result_decl) == RESULT_DECL);
1522 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1523 Recall that aggregate_value_p is FALSE for any aggregate type that is
1524 returned in registers. If we're returning values in registers, then
1525 we don't want to extend the lifetime of the RESULT_DECL, particularly
1526 across another call. In addition, for those aggregates for which
1527 hard_function_value generates a PARALLEL, we'll die during normal
1528 expansion of structure assignments; there's special code in expand_return
1529 to handle this case that does not exist in expand_expr. */
1530 if (!result_decl)
1531 result = NULL_TREE;
1532 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1534 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1536 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1537 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1538 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1539 should be effectively allocated by the caller, i.e. all calls to
1540 this function must be subject to the Return Slot Optimization. */
1541 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1542 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1544 result = result_decl;
1546 else if (gimplify_ctxp->return_temp)
1547 result = gimplify_ctxp->return_temp;
1548 else
1550 result = create_tmp_reg (TREE_TYPE (result_decl));
1552 /* ??? With complex control flow (usually involving abnormal edges),
1553 we can wind up warning about an uninitialized value for this. Due
1554 to how this variable is constructed and initialized, this is never
1555 true. Give up and never warn. */
1556 TREE_NO_WARNING (result) = 1;
1558 gimplify_ctxp->return_temp = result;
1561 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1562 Then gimplify the whole thing. */
1563 if (result != result_decl)
1564 TREE_OPERAND (ret_expr, 0) = result;
1566 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1568 maybe_add_early_return_predict_stmt (pre_p);
1569 ret = gimple_build_return (result);
1570 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1571 gimplify_seq_add_stmt (pre_p, ret);
1573 return GS_ALL_DONE;
1576 /* Gimplify a variable-length array DECL. */
1578 static void
1579 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1581 /* This is a variable-sized decl. Simplify its size and mark it
1582 for deferred expansion. */
1583 tree t, addr, ptr_type;
1585 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1586 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1588 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1589 if (DECL_HAS_VALUE_EXPR_P (decl))
1590 return;
1592 /* All occurrences of this decl in final gimplified code will be
1593 replaced by indirection. Setting DECL_VALUE_EXPR does two
1594 things: First, it lets the rest of the gimplifier know what
1595 replacement to use. Second, it lets the debug info know
1596 where to find the value. */
1597 ptr_type = build_pointer_type (TREE_TYPE (decl));
1598 addr = create_tmp_var (ptr_type, get_name (decl));
1599 DECL_IGNORED_P (addr) = 0;
1600 t = build_fold_indirect_ref (addr);
1601 TREE_THIS_NOTRAP (t) = 1;
1602 SET_DECL_VALUE_EXPR (decl, t);
1603 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1605 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1606 max_int_size_in_bytes (TREE_TYPE (decl)));
1607 /* The call has been built for a variable-sized object. */
1608 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1609 t = fold_convert (ptr_type, t);
1610 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1612 gimplify_and_add (t, seq_p);
1615 /* A helper function to be called via walk_tree. Mark all labels under *TP
1616 as being forced. To be called for DECL_INITIAL of static variables. */
1618 static tree
1619 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1621 if (TYPE_P (*tp))
1622 *walk_subtrees = 0;
1623 if (TREE_CODE (*tp) == LABEL_DECL)
1625 FORCED_LABEL (*tp) = 1;
1626 cfun->has_forced_label_in_static = 1;
1629 return NULL_TREE;
1632 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1633 and initialization explicit. */
1635 static enum gimplify_status
1636 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1638 tree stmt = *stmt_p;
1639 tree decl = DECL_EXPR_DECL (stmt);
1641 *stmt_p = NULL_TREE;
1643 if (TREE_TYPE (decl) == error_mark_node)
1644 return GS_ERROR;
1646 if ((TREE_CODE (decl) == TYPE_DECL
1647 || VAR_P (decl))
1648 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1650 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1651 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1652 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1655 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1656 in case its size expressions contain problematic nodes like CALL_EXPR. */
1657 if (TREE_CODE (decl) == TYPE_DECL
1658 && DECL_ORIGINAL_TYPE (decl)
1659 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1661 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1662 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1663 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1666 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1668 tree init = DECL_INITIAL (decl);
1669 bool is_vla = false;
1671 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1672 || (!TREE_STATIC (decl)
1673 && flag_stack_check == GENERIC_STACK_CHECK
1674 && compare_tree_int (DECL_SIZE_UNIT (decl),
1675 STACK_CHECK_MAX_VAR_SIZE) > 0))
1677 gimplify_vla_decl (decl, seq_p);
1678 is_vla = true;
1681 if (asan_poisoned_variables
1682 && !is_vla
1683 && TREE_ADDRESSABLE (decl)
1684 && !TREE_STATIC (decl)
1685 && !DECL_HAS_VALUE_EXPR_P (decl)
1686 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1687 && dbg_cnt (asan_use_after_scope)
1688 && !gimplify_omp_ctxp)
1690 asan_poisoned_variables->add (decl);
1691 asan_poison_variable (decl, false, seq_p);
1692 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1693 gimplify_ctxp->live_switch_vars->add (decl);
1696 /* Some front ends do not explicitly declare all anonymous
1697 artificial variables. We compensate here by declaring the
1698 variables, though it would be better if the front ends would
1699 explicitly declare them. */
1700 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1701 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1702 gimple_add_tmp_var (decl);
1704 if (init && init != error_mark_node)
1706 if (!TREE_STATIC (decl))
1708 DECL_INITIAL (decl) = NULL_TREE;
1709 init = build2 (INIT_EXPR, void_type_node, decl, init);
1710 gimplify_and_add (init, seq_p);
1711 ggc_free (init);
1713 else
1714 /* We must still examine initializers for static variables
1715 as they may contain a label address. */
1716 walk_tree (&init, force_labels_r, NULL, NULL);
1720 return GS_ALL_DONE;
1723 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1724 and replacing the LOOP_EXPR with goto, but if the loop contains an
1725 EXIT_EXPR, we need to append a label for it to jump to. */
1727 static enum gimplify_status
1728 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1730 tree saved_label = gimplify_ctxp->exit_label;
1731 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1733 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1735 gimplify_ctxp->exit_label = NULL_TREE;
1737 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1739 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1741 if (gimplify_ctxp->exit_label)
1742 gimplify_seq_add_stmt (pre_p,
1743 gimple_build_label (gimplify_ctxp->exit_label));
1745 gimplify_ctxp->exit_label = saved_label;
1747 *expr_p = NULL;
1748 return GS_ALL_DONE;
1751 /* Gimplify a statement list onto a sequence. These may be created either
1752 by an enlightened front-end, or by shortcut_cond_expr. */
1754 static enum gimplify_status
1755 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1757 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1759 tree_stmt_iterator i = tsi_start (*expr_p);
1761 while (!tsi_end_p (i))
1763 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1764 tsi_delink (&i);
1767 if (temp)
1769 *expr_p = temp;
1770 return GS_OK;
1773 return GS_ALL_DONE;
1776 /* Callback for walk_gimple_seq. */
1778 static tree
1779 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1780 struct walk_stmt_info *wi)
1782 gimple *stmt = gsi_stmt (*gsi_p);
1784 *handled_ops_p = true;
1785 switch (gimple_code (stmt))
1787 case GIMPLE_TRY:
1788 /* A compiler-generated cleanup or a user-written try block.
1789 If it's empty, don't dive into it--that would result in
1790 worse location info. */
1791 if (gimple_try_eval (stmt) == NULL)
1793 wi->info = stmt;
1794 return integer_zero_node;
1796 /* Fall through. */
1797 case GIMPLE_BIND:
1798 case GIMPLE_CATCH:
1799 case GIMPLE_EH_FILTER:
1800 case GIMPLE_TRANSACTION:
1801 /* Walk the sub-statements. */
1802 *handled_ops_p = false;
1803 break;
1805 case GIMPLE_DEBUG:
1806 /* Ignore these. We may generate them before declarations that
1807 are never executed. If there's something to warn about,
1808 there will be non-debug stmts too, and we'll catch those. */
1809 break;
1811 case GIMPLE_CALL:
1812 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1814 *handled_ops_p = false;
1815 break;
1817 /* Fall through. */
1818 default:
1819 /* Save the first "real" statement (not a decl/lexical scope/...). */
1820 wi->info = stmt;
1821 return integer_zero_node;
1823 return NULL_TREE;
1826 /* Possibly warn about unreachable statements between switch's controlling
1827 expression and the first case. SEQ is the body of a switch expression. */
1829 static void
1830 maybe_warn_switch_unreachable (gimple_seq seq)
1832 if (!warn_switch_unreachable
1833 /* This warning doesn't play well with Fortran when optimizations
1834 are on. */
1835 || lang_GNU_Fortran ()
1836 || seq == NULL)
1837 return;
1839 struct walk_stmt_info wi;
1840 memset (&wi, 0, sizeof (wi));
1841 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1842 gimple *stmt = (gimple *) wi.info;
1844 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1846 if (gimple_code (stmt) == GIMPLE_GOTO
1847 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1848 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1849 /* Don't warn for compiler-generated gotos. These occur
1850 in Duff's devices, for example. */;
1851 else
1852 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1853 "statement will never be executed");
1858 /* A label entry that pairs label and a location. */
1859 struct label_entry
1861 tree label;
1862 location_t loc;
1865 /* Find LABEL in vector of label entries VEC. */
1867 static struct label_entry *
1868 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1870 unsigned int i;
1871 struct label_entry *l;
1873 FOR_EACH_VEC_ELT (*vec, i, l)
1874 if (l->label == label)
1875 return l;
1876 return NULL;
1879 /* Return true if LABEL, a LABEL_DECL, represents a case label
1880 in a vector of labels CASES. */
1882 static bool
1883 case_label_p (const vec<tree> *cases, tree label)
1885 unsigned int i;
1886 tree l;
1888 FOR_EACH_VEC_ELT (*cases, i, l)
1889 if (CASE_LABEL (l) == label)
1890 return true;
1891 return false;
1894 /* Find the last nondebug statement in a scope STMT. */
1896 static gimple *
1897 last_stmt_in_scope (gimple *stmt)
1899 if (!stmt)
1900 return NULL;
1902 switch (gimple_code (stmt))
1904 case GIMPLE_BIND:
1906 gbind *bind = as_a <gbind *> (stmt);
1907 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1908 return last_stmt_in_scope (stmt);
1911 case GIMPLE_TRY:
1913 gtry *try_stmt = as_a <gtry *> (stmt);
1914 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1915 gimple *last_eval = last_stmt_in_scope (stmt);
1916 if (gimple_stmt_may_fallthru (last_eval)
1917 && (last_eval == NULL
1918 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1919 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1921 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1922 return last_stmt_in_scope (stmt);
1924 else
1925 return last_eval;
1928 case GIMPLE_DEBUG:
1929 gcc_unreachable ();
1931 default:
1932 return stmt;
1936 /* Collect interesting labels in LABELS and return the statement preceding
1937 another case label, or a user-defined label. */
1939 static gimple *
1940 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1941 auto_vec <struct label_entry> *labels)
1943 gimple *prev = NULL;
1947 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1949 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1950 which starts on a GIMPLE_SWITCH and ends with a break label.
1951 Handle that as a single statement that can fall through. */
1952 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1953 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1954 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1955 if (last
1956 && gimple_code (first) == GIMPLE_SWITCH
1957 && gimple_code (last) == GIMPLE_LABEL)
1959 tree label = gimple_label_label (as_a <glabel *> (last));
1960 if (SWITCH_BREAK_LABEL_P (label))
1962 prev = bind;
1963 gsi_next (gsi_p);
1964 continue;
1968 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1969 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1971 /* Nested scope. Only look at the last statement of
1972 the innermost scope. */
1973 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1974 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1975 if (last)
1977 prev = last;
1978 /* It might be a label without a location. Use the
1979 location of the scope then. */
1980 if (!gimple_has_location (prev))
1981 gimple_set_location (prev, bind_loc);
1983 gsi_next (gsi_p);
1984 continue;
1987 /* Ifs are tricky. */
1988 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1990 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1991 tree false_lab = gimple_cond_false_label (cond_stmt);
1992 location_t if_loc = gimple_location (cond_stmt);
1994 /* If we have e.g.
1995 if (i > 1) goto <D.2259>; else goto D;
1996 we can't do much with the else-branch. */
1997 if (!DECL_ARTIFICIAL (false_lab))
1998 break;
2000 /* Go on until the false label, then one step back. */
2001 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2003 gimple *stmt = gsi_stmt (*gsi_p);
2004 if (gimple_code (stmt) == GIMPLE_LABEL
2005 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2006 break;
2009 /* Not found? Oops. */
2010 if (gsi_end_p (*gsi_p))
2011 break;
2013 struct label_entry l = { false_lab, if_loc };
2014 labels->safe_push (l);
2016 /* Go to the last statement of the then branch. */
2017 gsi_prev (gsi_p);
2019 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2020 <D.1759>:
2021 <stmt>;
2022 goto <D.1761>;
2023 <D.1760>:
2025 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2026 && !gimple_has_location (gsi_stmt (*gsi_p)))
2028 /* Look at the statement before, it might be
2029 attribute fallthrough, in which case don't warn. */
2030 gsi_prev (gsi_p);
2031 bool fallthru_before_dest
2032 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2033 gsi_next (gsi_p);
2034 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2035 if (!fallthru_before_dest)
2037 struct label_entry l = { goto_dest, if_loc };
2038 labels->safe_push (l);
2041 /* And move back. */
2042 gsi_next (gsi_p);
2045 /* Remember the last statement. Skip labels that are of no interest
2046 to us. */
2047 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2049 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2050 if (find_label_entry (labels, label))
2051 prev = gsi_stmt (*gsi_p);
2053 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2055 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2056 prev = gsi_stmt (*gsi_p);
2057 gsi_next (gsi_p);
2059 while (!gsi_end_p (*gsi_p)
2060 /* Stop if we find a case or a user-defined label. */
2061 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2062 || !gimple_has_location (gsi_stmt (*gsi_p))));
2064 return prev;
2067 /* Return true if the switch fallthough warning should occur. LABEL is
2068 the label statement that we're falling through to. */
2070 static bool
2071 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2073 gimple_stmt_iterator gsi = *gsi_p;
2075 /* Don't warn if the label is marked with a "falls through" comment. */
2076 if (FALLTHROUGH_LABEL_P (label))
2077 return false;
2079 /* Don't warn for non-case labels followed by a statement:
2080 case 0:
2081 foo ();
2082 label:
2083 bar ();
2084 as these are likely intentional. */
2085 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2087 tree l;
2088 while (!gsi_end_p (gsi)
2089 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2090 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2091 && !case_label_p (&gimplify_ctxp->case_labels, l))
2092 gsi_next_nondebug (&gsi);
2093 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2094 return false;
2097 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2098 immediately breaks. */
2099 gsi = *gsi_p;
2101 /* Skip all immediately following labels. */
2102 while (!gsi_end_p (gsi)
2103 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2104 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2105 gsi_next_nondebug (&gsi);
2107 /* { ... something; default:; } */
2108 if (gsi_end_p (gsi)
2109 /* { ... something; default: break; } or
2110 { ... something; default: goto L; } */
2111 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2112 /* { ... something; default: return; } */
2113 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2114 return false;
2116 return true;
2119 /* Callback for walk_gimple_seq. */
2121 static tree
2122 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2123 struct walk_stmt_info *)
2125 gimple *stmt = gsi_stmt (*gsi_p);
2127 *handled_ops_p = true;
2128 switch (gimple_code (stmt))
2130 case GIMPLE_TRY:
2131 case GIMPLE_BIND:
2132 case GIMPLE_CATCH:
2133 case GIMPLE_EH_FILTER:
2134 case GIMPLE_TRANSACTION:
2135 /* Walk the sub-statements. */
2136 *handled_ops_p = false;
2137 break;
2139 /* Find a sequence of form:
2141 GIMPLE_LABEL
2142 [...]
2143 <may fallthru stmt>
2144 GIMPLE_LABEL
2146 and possibly warn. */
2147 case GIMPLE_LABEL:
2149 /* Found a label. Skip all immediately following labels. */
2150 while (!gsi_end_p (*gsi_p)
2151 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2152 gsi_next_nondebug (gsi_p);
2154 /* There might be no more statements. */
2155 if (gsi_end_p (*gsi_p))
2156 return integer_zero_node;
2158 /* Vector of labels that fall through. */
2159 auto_vec <struct label_entry> labels;
2160 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2162 /* There might be no more statements. */
2163 if (gsi_end_p (*gsi_p))
2164 return integer_zero_node;
2166 gimple *next = gsi_stmt (*gsi_p);
2167 tree label;
2168 /* If what follows is a label, then we may have a fallthrough. */
2169 if (gimple_code (next) == GIMPLE_LABEL
2170 && gimple_has_location (next)
2171 && (label = gimple_label_label (as_a <glabel *> (next)))
2172 && prev != NULL)
2174 struct label_entry *l;
2175 bool warned_p = false;
2176 auto_diagnostic_group d;
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 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2679 DECL_VALUE_EXPR, and it's worth re-examining things. */
2681 static enum gimplify_status
2682 gimplify_var_or_parm_decl (tree *expr_p)
2684 tree decl = *expr_p;
2686 /* ??? If this is a local variable, and it has not been seen in any
2687 outer BIND_EXPR, then it's probably the result of a duplicate
2688 declaration, for which we've already issued an error. It would
2689 be really nice if the front end wouldn't leak these at all.
2690 Currently the only known culprit is C++ destructors, as seen
2691 in g++.old-deja/g++.jason/binding.C. */
2692 if (VAR_P (decl)
2693 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2694 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2695 && decl_function_context (decl) == current_function_decl)
2697 gcc_assert (seen_error ());
2698 return GS_ERROR;
2701 /* When within an OMP context, notice uses of variables. */
2702 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2703 return GS_ALL_DONE;
2705 /* If the decl is an alias for another expression, substitute it now. */
2706 if (DECL_HAS_VALUE_EXPR_P (decl))
2708 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
2709 return GS_OK;
2712 return GS_ALL_DONE;
2715 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2717 static void
2718 recalculate_side_effects (tree t)
2720 enum tree_code code = TREE_CODE (t);
2721 int len = TREE_OPERAND_LENGTH (t);
2722 int i;
2724 switch (TREE_CODE_CLASS (code))
2726 case tcc_expression:
2727 switch (code)
2729 case INIT_EXPR:
2730 case MODIFY_EXPR:
2731 case VA_ARG_EXPR:
2732 case PREDECREMENT_EXPR:
2733 case PREINCREMENT_EXPR:
2734 case POSTDECREMENT_EXPR:
2735 case POSTINCREMENT_EXPR:
2736 /* All of these have side-effects, no matter what their
2737 operands are. */
2738 return;
2740 default:
2741 break;
2743 /* Fall through. */
2745 case tcc_comparison: /* a comparison expression */
2746 case tcc_unary: /* a unary arithmetic expression */
2747 case tcc_binary: /* a binary arithmetic expression */
2748 case tcc_reference: /* a reference */
2749 case tcc_vl_exp: /* a function call */
2750 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2751 for (i = 0; i < len; ++i)
2753 tree op = TREE_OPERAND (t, i);
2754 if (op && TREE_SIDE_EFFECTS (op))
2755 TREE_SIDE_EFFECTS (t) = 1;
2757 break;
2759 case tcc_constant:
2760 /* No side-effects. */
2761 return;
2763 default:
2764 gcc_unreachable ();
2768 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2769 node *EXPR_P.
2771 compound_lval
2772 : min_lval '[' val ']'
2773 | min_lval '.' ID
2774 | compound_lval '[' val ']'
2775 | compound_lval '.' ID
2777 This is not part of the original SIMPLE definition, which separates
2778 array and member references, but it seems reasonable to handle them
2779 together. Also, this way we don't run into problems with union
2780 aliasing; gcc requires that for accesses through a union to alias, the
2781 union reference must be explicit, which was not always the case when we
2782 were splitting up array and member refs.
2784 PRE_P points to the sequence where side effects that must happen before
2785 *EXPR_P should be stored.
2787 POST_P points to the sequence where side effects that must happen after
2788 *EXPR_P should be stored. */
2790 static enum gimplify_status
2791 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2792 fallback_t fallback)
2794 tree *p;
2795 enum gimplify_status ret = GS_ALL_DONE, tret;
2796 int i;
2797 location_t loc = EXPR_LOCATION (*expr_p);
2798 tree expr = *expr_p;
2800 /* Create a stack of the subexpressions so later we can walk them in
2801 order from inner to outer. */
2802 auto_vec<tree, 10> expr_stack;
2804 /* We can handle anything that get_inner_reference can deal with. */
2805 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2807 restart:
2808 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2809 if (TREE_CODE (*p) == INDIRECT_REF)
2810 *p = fold_indirect_ref_loc (loc, *p);
2812 if (handled_component_p (*p))
2814 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2815 additional COMPONENT_REFs. */
2816 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2817 && gimplify_var_or_parm_decl (p) == GS_OK)
2818 goto restart;
2819 else
2820 break;
2822 expr_stack.safe_push (*p);
2825 gcc_assert (expr_stack.length ());
2827 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2828 walked through and P points to the innermost expression.
2830 Java requires that we elaborated nodes in source order. That
2831 means we must gimplify the inner expression followed by each of
2832 the indices, in order. But we can't gimplify the inner
2833 expression until we deal with any variable bounds, sizes, or
2834 positions in order to deal with PLACEHOLDER_EXPRs.
2836 So we do this in three steps. First we deal with the annotations
2837 for any variables in the components, then we gimplify the base,
2838 then we gimplify any indices, from left to right. */
2839 for (i = expr_stack.length () - 1; i >= 0; i--)
2841 tree t = expr_stack[i];
2843 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2845 /* Gimplify the low bound and element type size and put them into
2846 the ARRAY_REF. If these values are set, they have already been
2847 gimplified. */
2848 if (TREE_OPERAND (t, 2) == NULL_TREE)
2850 tree low = unshare_expr (array_ref_low_bound (t));
2851 if (!is_gimple_min_invariant (low))
2853 TREE_OPERAND (t, 2) = low;
2854 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2855 post_p, is_gimple_reg,
2856 fb_rvalue);
2857 ret = MIN (ret, tret);
2860 else
2862 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2863 is_gimple_reg, fb_rvalue);
2864 ret = MIN (ret, tret);
2867 if (TREE_OPERAND (t, 3) == NULL_TREE)
2869 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2870 tree elmt_size = unshare_expr (array_ref_element_size (t));
2871 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2873 /* Divide the element size by the alignment of the element
2874 type (above). */
2875 elmt_size
2876 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2878 if (!is_gimple_min_invariant (elmt_size))
2880 TREE_OPERAND (t, 3) = elmt_size;
2881 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2882 post_p, is_gimple_reg,
2883 fb_rvalue);
2884 ret = MIN (ret, tret);
2887 else
2889 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2890 is_gimple_reg, fb_rvalue);
2891 ret = MIN (ret, tret);
2894 else if (TREE_CODE (t) == COMPONENT_REF)
2896 /* Set the field offset into T and gimplify it. */
2897 if (TREE_OPERAND (t, 2) == NULL_TREE)
2899 tree offset = unshare_expr (component_ref_field_offset (t));
2900 tree field = TREE_OPERAND (t, 1);
2901 tree factor
2902 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2904 /* Divide the offset by its alignment. */
2905 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2907 if (!is_gimple_min_invariant (offset))
2909 TREE_OPERAND (t, 2) = offset;
2910 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2911 post_p, is_gimple_reg,
2912 fb_rvalue);
2913 ret = MIN (ret, tret);
2916 else
2918 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2919 is_gimple_reg, fb_rvalue);
2920 ret = MIN (ret, tret);
2925 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2926 so as to match the min_lval predicate. Failure to do so may result
2927 in the creation of large aggregate temporaries. */
2928 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2929 fallback | fb_lvalue);
2930 ret = MIN (ret, tret);
2932 /* And finally, the indices and operands of ARRAY_REF. During this
2933 loop we also remove any useless conversions. */
2934 for (; expr_stack.length () > 0; )
2936 tree t = expr_stack.pop ();
2938 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2940 /* Gimplify the dimension. */
2941 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2943 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2944 is_gimple_val, fb_rvalue);
2945 ret = MIN (ret, tret);
2949 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2951 /* The innermost expression P may have originally had
2952 TREE_SIDE_EFFECTS set which would have caused all the outer
2953 expressions in *EXPR_P leading to P to also have had
2954 TREE_SIDE_EFFECTS set. */
2955 recalculate_side_effects (t);
2958 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2959 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2961 canonicalize_component_ref (expr_p);
2964 expr_stack.release ();
2966 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2968 return ret;
2971 /* Gimplify the self modifying expression pointed to by EXPR_P
2972 (++, --, +=, -=).
2974 PRE_P points to the list where side effects that must happen before
2975 *EXPR_P should be stored.
2977 POST_P points to the list where side effects that must happen after
2978 *EXPR_P should be stored.
2980 WANT_VALUE is nonzero iff we want to use the value of this expression
2981 in another expression.
2983 ARITH_TYPE is the type the computation should be performed in. */
2985 enum gimplify_status
2986 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2987 bool want_value, tree arith_type)
2989 enum tree_code code;
2990 tree lhs, lvalue, rhs, t1;
2991 gimple_seq post = NULL, *orig_post_p = post_p;
2992 bool postfix;
2993 enum tree_code arith_code;
2994 enum gimplify_status ret;
2995 location_t loc = EXPR_LOCATION (*expr_p);
2997 code = TREE_CODE (*expr_p);
2999 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3000 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3002 /* Prefix or postfix? */
3003 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3004 /* Faster to treat as prefix if result is not used. */
3005 postfix = want_value;
3006 else
3007 postfix = false;
3009 /* For postfix, make sure the inner expression's post side effects
3010 are executed after side effects from this expression. */
3011 if (postfix)
3012 post_p = &post;
3014 /* Add or subtract? */
3015 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3016 arith_code = PLUS_EXPR;
3017 else
3018 arith_code = MINUS_EXPR;
3020 /* Gimplify the LHS into a GIMPLE lvalue. */
3021 lvalue = TREE_OPERAND (*expr_p, 0);
3022 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3023 if (ret == GS_ERROR)
3024 return ret;
3026 /* Extract the operands to the arithmetic operation. */
3027 lhs = lvalue;
3028 rhs = TREE_OPERAND (*expr_p, 1);
3030 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3031 that as the result value and in the postqueue operation. */
3032 if (postfix)
3034 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3035 if (ret == GS_ERROR)
3036 return ret;
3038 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3041 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3042 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3044 rhs = convert_to_ptrofftype_loc (loc, rhs);
3045 if (arith_code == MINUS_EXPR)
3046 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3047 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3049 else
3050 t1 = fold_convert (TREE_TYPE (*expr_p),
3051 fold_build2 (arith_code, arith_type,
3052 fold_convert (arith_type, lhs),
3053 fold_convert (arith_type, rhs)));
3055 if (postfix)
3057 gimplify_assign (lvalue, t1, pre_p);
3058 gimplify_seq_add_seq (orig_post_p, post);
3059 *expr_p = lhs;
3060 return GS_ALL_DONE;
3062 else
3064 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3065 return GS_OK;
3069 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3071 static void
3072 maybe_with_size_expr (tree *expr_p)
3074 tree expr = *expr_p;
3075 tree type = TREE_TYPE (expr);
3076 tree size;
3078 /* If we've already wrapped this or the type is error_mark_node, we can't do
3079 anything. */
3080 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3081 || type == error_mark_node)
3082 return;
3084 /* If the size isn't known or is a constant, we have nothing to do. */
3085 size = TYPE_SIZE_UNIT (type);
3086 if (!size || poly_int_tree_p (size))
3087 return;
3089 /* Otherwise, make a WITH_SIZE_EXPR. */
3090 size = unshare_expr (size);
3091 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3092 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3095 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3096 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3097 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3098 gimplified to an SSA name. */
3100 enum gimplify_status
3101 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3102 bool allow_ssa)
3104 bool (*test) (tree);
3105 fallback_t fb;
3107 /* In general, we allow lvalues for function arguments to avoid
3108 extra overhead of copying large aggregates out of even larger
3109 aggregates into temporaries only to copy the temporaries to
3110 the argument list. Make optimizers happy by pulling out to
3111 temporaries those types that fit in registers. */
3112 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3113 test = is_gimple_val, fb = fb_rvalue;
3114 else
3116 test = is_gimple_lvalue, fb = fb_either;
3117 /* Also strip a TARGET_EXPR that would force an extra copy. */
3118 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3120 tree init = TARGET_EXPR_INITIAL (*arg_p);
3121 if (init
3122 && !VOID_TYPE_P (TREE_TYPE (init)))
3123 *arg_p = init;
3127 /* If this is a variable sized type, we must remember the size. */
3128 maybe_with_size_expr (arg_p);
3130 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3131 /* Make sure arguments have the same location as the function call
3132 itself. */
3133 protected_set_expr_location (*arg_p, call_location);
3135 /* There is a sequence point before a function call. Side effects in
3136 the argument list must occur before the actual call. So, when
3137 gimplifying arguments, force gimplify_expr to use an internal
3138 post queue which is then appended to the end of PRE_P. */
3139 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3142 /* Don't fold inside offloading or taskreg regions: it can break code by
3143 adding decl references that weren't in the source. We'll do it during
3144 omplower pass instead. */
3146 static bool
3147 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3149 struct gimplify_omp_ctx *ctx;
3150 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3151 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3152 return false;
3153 return fold_stmt (gsi);
3156 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3157 WANT_VALUE is true if the result of the call is desired. */
3159 static enum gimplify_status
3160 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3162 tree fndecl, parms, p, fnptrtype;
3163 enum gimplify_status ret;
3164 int i, nargs;
3165 gcall *call;
3166 bool builtin_va_start_p = false;
3167 location_t loc = EXPR_LOCATION (*expr_p);
3169 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3171 /* For reliable diagnostics during inlining, it is necessary that
3172 every call_expr be annotated with file and line. */
3173 if (! EXPR_HAS_LOCATION (*expr_p))
3174 SET_EXPR_LOCATION (*expr_p, input_location);
3176 /* Gimplify internal functions created in the FEs. */
3177 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3179 if (want_value)
3180 return GS_ALL_DONE;
3182 nargs = call_expr_nargs (*expr_p);
3183 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3184 auto_vec<tree> vargs (nargs);
3186 for (i = 0; i < nargs; i++)
3188 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3189 EXPR_LOCATION (*expr_p));
3190 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3193 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3194 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3195 gimplify_seq_add_stmt (pre_p, call);
3196 return GS_ALL_DONE;
3199 /* This may be a call to a builtin function.
3201 Builtin function calls may be transformed into different
3202 (and more efficient) builtin function calls under certain
3203 circumstances. Unfortunately, gimplification can muck things
3204 up enough that the builtin expanders are not aware that certain
3205 transformations are still valid.
3207 So we attempt transformation/gimplification of the call before
3208 we gimplify the CALL_EXPR. At this time we do not manage to
3209 transform all calls in the same manner as the expanders do, but
3210 we do transform most of them. */
3211 fndecl = get_callee_fndecl (*expr_p);
3212 if (fndecl
3213 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3214 switch (DECL_FUNCTION_CODE (fndecl))
3216 CASE_BUILT_IN_ALLOCA:
3217 /* If the call has been built for a variable-sized object, then we
3218 want to restore the stack level when the enclosing BIND_EXPR is
3219 exited to reclaim the allocated space; otherwise, we precisely
3220 need to do the opposite and preserve the latest stack level. */
3221 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3222 gimplify_ctxp->save_stack = true;
3223 else
3224 gimplify_ctxp->keep_stack = true;
3225 break;
3227 case BUILT_IN_VA_START:
3229 builtin_va_start_p = TRUE;
3230 if (call_expr_nargs (*expr_p) < 2)
3232 error ("too few arguments to function %<va_start%>");
3233 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3234 return GS_OK;
3237 if (fold_builtin_next_arg (*expr_p, true))
3239 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3240 return GS_OK;
3242 break;
3245 default:
3248 if (fndecl && DECL_BUILT_IN (fndecl))
3250 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3251 if (new_tree && new_tree != *expr_p)
3253 /* There was a transformation of this call which computes the
3254 same value, but in a more efficient way. Return and try
3255 again. */
3256 *expr_p = new_tree;
3257 return GS_OK;
3261 /* Remember the original function pointer type. */
3262 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3264 /* There is a sequence point before the call, so any side effects in
3265 the calling expression must occur before the actual call. Force
3266 gimplify_expr to use an internal post queue. */
3267 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3268 is_gimple_call_addr, fb_rvalue);
3270 nargs = call_expr_nargs (*expr_p);
3272 /* Get argument types for verification. */
3273 fndecl = get_callee_fndecl (*expr_p);
3274 parms = NULL_TREE;
3275 if (fndecl)
3276 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3277 else
3278 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3280 if (fndecl && DECL_ARGUMENTS (fndecl))
3281 p = DECL_ARGUMENTS (fndecl);
3282 else if (parms)
3283 p = parms;
3284 else
3285 p = NULL_TREE;
3286 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3289 /* If the last argument is __builtin_va_arg_pack () and it is not
3290 passed as a named argument, decrease the number of CALL_EXPR
3291 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3292 if (!p
3293 && i < nargs
3294 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3296 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3297 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3299 if (last_arg_fndecl
3300 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3301 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3302 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3304 tree call = *expr_p;
3306 --nargs;
3307 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3308 CALL_EXPR_FN (call),
3309 nargs, CALL_EXPR_ARGP (call));
3311 /* Copy all CALL_EXPR flags, location and block, except
3312 CALL_EXPR_VA_ARG_PACK flag. */
3313 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3314 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3315 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3316 = CALL_EXPR_RETURN_SLOT_OPT (call);
3317 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3318 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3320 /* Set CALL_EXPR_VA_ARG_PACK. */
3321 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3325 /* If the call returns twice then after building the CFG the call
3326 argument computations will no longer dominate the call because
3327 we add an abnormal incoming edge to the call. So do not use SSA
3328 vars there. */
3329 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3331 /* Gimplify the function arguments. */
3332 if (nargs > 0)
3334 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3335 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3336 PUSH_ARGS_REVERSED ? i-- : i++)
3338 enum gimplify_status t;
3340 /* Avoid gimplifying the second argument to va_start, which needs to
3341 be the plain PARM_DECL. */
3342 if ((i != 1) || !builtin_va_start_p)
3344 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3345 EXPR_LOCATION (*expr_p), ! returns_twice);
3347 if (t == GS_ERROR)
3348 ret = GS_ERROR;
3353 /* Gimplify the static chain. */
3354 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3356 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3357 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3358 else
3360 enum gimplify_status t;
3361 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3362 EXPR_LOCATION (*expr_p), ! returns_twice);
3363 if (t == GS_ERROR)
3364 ret = GS_ERROR;
3368 /* Verify the function result. */
3369 if (want_value && fndecl
3370 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3372 error_at (loc, "using result of function returning %<void%>");
3373 ret = GS_ERROR;
3376 /* Try this again in case gimplification exposed something. */
3377 if (ret != GS_ERROR)
3379 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3381 if (new_tree && new_tree != *expr_p)
3383 /* There was a transformation of this call which computes the
3384 same value, but in a more efficient way. Return and try
3385 again. */
3386 *expr_p = new_tree;
3387 return GS_OK;
3390 else
3392 *expr_p = error_mark_node;
3393 return GS_ERROR;
3396 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3397 decl. This allows us to eliminate redundant or useless
3398 calls to "const" functions. */
3399 if (TREE_CODE (*expr_p) == CALL_EXPR)
3401 int flags = call_expr_flags (*expr_p);
3402 if (flags & (ECF_CONST | ECF_PURE)
3403 /* An infinite loop is considered a side effect. */
3404 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3405 TREE_SIDE_EFFECTS (*expr_p) = 0;
3408 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3409 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3410 form and delegate the creation of a GIMPLE_CALL to
3411 gimplify_modify_expr. This is always possible because when
3412 WANT_VALUE is true, the caller wants the result of this call into
3413 a temporary, which means that we will emit an INIT_EXPR in
3414 internal_get_tmp_var which will then be handled by
3415 gimplify_modify_expr. */
3416 if (!want_value)
3418 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3419 have to do is replicate it as a GIMPLE_CALL tuple. */
3420 gimple_stmt_iterator gsi;
3421 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3422 notice_special_calls (call);
3423 gimplify_seq_add_stmt (pre_p, call);
3424 gsi = gsi_last (*pre_p);
3425 maybe_fold_stmt (&gsi);
3426 *expr_p = NULL_TREE;
3428 else
3429 /* Remember the original function type. */
3430 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3431 CALL_EXPR_FN (*expr_p));
3433 return ret;
3436 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3437 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3439 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3440 condition is true or false, respectively. If null, we should generate
3441 our own to skip over the evaluation of this specific expression.
3443 LOCUS is the source location of the COND_EXPR.
3445 This function is the tree equivalent of do_jump.
3447 shortcut_cond_r should only be called by shortcut_cond_expr. */
3449 static tree
3450 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3451 location_t locus)
3453 tree local_label = NULL_TREE;
3454 tree t, expr = NULL;
3456 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3457 retain the shortcut semantics. Just insert the gotos here;
3458 shortcut_cond_expr will append the real blocks later. */
3459 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3461 location_t new_locus;
3463 /* Turn if (a && b) into
3465 if (a); else goto no;
3466 if (b) goto yes; else goto no;
3467 (no:) */
3469 if (false_label_p == NULL)
3470 false_label_p = &local_label;
3472 /* Keep the original source location on the first 'if'. */
3473 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3474 append_to_statement_list (t, &expr);
3476 /* Set the source location of the && on the second 'if'. */
3477 new_locus = rexpr_location (pred, locus);
3478 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3479 new_locus);
3480 append_to_statement_list (t, &expr);
3482 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3484 location_t new_locus;
3486 /* Turn if (a || b) into
3488 if (a) goto yes;
3489 if (b) goto yes; else goto no;
3490 (yes:) */
3492 if (true_label_p == NULL)
3493 true_label_p = &local_label;
3495 /* Keep the original source location on the first 'if'. */
3496 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3497 append_to_statement_list (t, &expr);
3499 /* Set the source location of the || on the second 'if'. */
3500 new_locus = rexpr_location (pred, locus);
3501 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3502 new_locus);
3503 append_to_statement_list (t, &expr);
3505 else if (TREE_CODE (pred) == COND_EXPR
3506 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3507 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3509 location_t new_locus;
3511 /* As long as we're messing with gotos, turn if (a ? b : c) into
3512 if (a)
3513 if (b) goto yes; else goto no;
3514 else
3515 if (c) goto yes; else goto no;
3517 Don't do this if one of the arms has void type, which can happen
3518 in C++ when the arm is throw. */
3520 /* Keep the original source location on the first 'if'. Set the source
3521 location of the ? on the second 'if'. */
3522 new_locus = rexpr_location (pred, locus);
3523 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3524 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3525 false_label_p, locus),
3526 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3527 false_label_p, new_locus));
3529 else
3531 expr = build3 (COND_EXPR, void_type_node, pred,
3532 build_and_jump (true_label_p),
3533 build_and_jump (false_label_p));
3534 SET_EXPR_LOCATION (expr, locus);
3537 if (local_label)
3539 t = build1 (LABEL_EXPR, void_type_node, local_label);
3540 append_to_statement_list (t, &expr);
3543 return expr;
3546 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3547 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3548 statement, if it is the last one. Otherwise, return NULL. */
3550 static tree
3551 find_goto (tree expr)
3553 if (!expr)
3554 return NULL_TREE;
3556 if (TREE_CODE (expr) == GOTO_EXPR)
3557 return expr;
3559 if (TREE_CODE (expr) != STATEMENT_LIST)
3560 return NULL_TREE;
3562 tree_stmt_iterator i = tsi_start (expr);
3564 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3565 tsi_next (&i);
3567 if (!tsi_one_before_end_p (i))
3568 return NULL_TREE;
3570 return find_goto (tsi_stmt (i));
3573 /* Same as find_goto, except that it returns NULL if the destination
3574 is not a LABEL_DECL. */
3576 static inline tree
3577 find_goto_label (tree expr)
3579 tree dest = find_goto (expr);
3580 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3581 return dest;
3582 return NULL_TREE;
3585 /* Given a conditional expression EXPR with short-circuit boolean
3586 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3587 predicate apart into the equivalent sequence of conditionals. */
3589 static tree
3590 shortcut_cond_expr (tree expr)
3592 tree pred = TREE_OPERAND (expr, 0);
3593 tree then_ = TREE_OPERAND (expr, 1);
3594 tree else_ = TREE_OPERAND (expr, 2);
3595 tree true_label, false_label, end_label, t;
3596 tree *true_label_p;
3597 tree *false_label_p;
3598 bool emit_end, emit_false, jump_over_else;
3599 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3600 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3602 /* First do simple transformations. */
3603 if (!else_se)
3605 /* If there is no 'else', turn
3606 if (a && b) then c
3607 into
3608 if (a) if (b) then c. */
3609 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3611 /* Keep the original source location on the first 'if'. */
3612 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3613 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3614 /* Set the source location of the && on the second 'if'. */
3615 if (rexpr_has_location (pred))
3616 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3617 then_ = shortcut_cond_expr (expr);
3618 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3619 pred = TREE_OPERAND (pred, 0);
3620 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3621 SET_EXPR_LOCATION (expr, locus);
3625 if (!then_se)
3627 /* If there is no 'then', turn
3628 if (a || b); else d
3629 into
3630 if (a); else if (b); else d. */
3631 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3633 /* Keep the original source location on the first 'if'. */
3634 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3635 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3636 /* Set the source location of the || on the second 'if'. */
3637 if (rexpr_has_location (pred))
3638 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3639 else_ = shortcut_cond_expr (expr);
3640 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3641 pred = TREE_OPERAND (pred, 0);
3642 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3643 SET_EXPR_LOCATION (expr, locus);
3647 /* If we're done, great. */
3648 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3649 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3650 return expr;
3652 /* Otherwise we need to mess with gotos. Change
3653 if (a) c; else d;
3655 if (a); else goto no;
3656 c; goto end;
3657 no: d; end:
3658 and recursively gimplify the condition. */
3660 true_label = false_label = end_label = NULL_TREE;
3662 /* If our arms just jump somewhere, hijack those labels so we don't
3663 generate jumps to jumps. */
3665 if (tree then_goto = find_goto_label (then_))
3667 true_label = GOTO_DESTINATION (then_goto);
3668 then_ = NULL;
3669 then_se = false;
3672 if (tree else_goto = find_goto_label (else_))
3674 false_label = GOTO_DESTINATION (else_goto);
3675 else_ = NULL;
3676 else_se = false;
3679 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3680 if (true_label)
3681 true_label_p = &true_label;
3682 else
3683 true_label_p = NULL;
3685 /* The 'else' branch also needs a label if it contains interesting code. */
3686 if (false_label || else_se)
3687 false_label_p = &false_label;
3688 else
3689 false_label_p = NULL;
3691 /* If there was nothing else in our arms, just forward the label(s). */
3692 if (!then_se && !else_se)
3693 return shortcut_cond_r (pred, true_label_p, false_label_p,
3694 EXPR_LOC_OR_LOC (expr, input_location));
3696 /* If our last subexpression already has a terminal label, reuse it. */
3697 if (else_se)
3698 t = expr_last (else_);
3699 else if (then_se)
3700 t = expr_last (then_);
3701 else
3702 t = NULL;
3703 if (t && TREE_CODE (t) == LABEL_EXPR)
3704 end_label = LABEL_EXPR_LABEL (t);
3706 /* If we don't care about jumping to the 'else' branch, jump to the end
3707 if the condition is false. */
3708 if (!false_label_p)
3709 false_label_p = &end_label;
3711 /* We only want to emit these labels if we aren't hijacking them. */
3712 emit_end = (end_label == NULL_TREE);
3713 emit_false = (false_label == NULL_TREE);
3715 /* We only emit the jump over the else clause if we have to--if the
3716 then clause may fall through. Otherwise we can wind up with a
3717 useless jump and a useless label at the end of gimplified code,
3718 which will cause us to think that this conditional as a whole
3719 falls through even if it doesn't. If we then inline a function
3720 which ends with such a condition, that can cause us to issue an
3721 inappropriate warning about control reaching the end of a
3722 non-void function. */
3723 jump_over_else = block_may_fallthru (then_);
3725 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3726 EXPR_LOC_OR_LOC (expr, input_location));
3728 expr = NULL;
3729 append_to_statement_list (pred, &expr);
3731 append_to_statement_list (then_, &expr);
3732 if (else_se)
3734 if (jump_over_else)
3736 tree last = expr_last (expr);
3737 t = build_and_jump (&end_label);
3738 if (rexpr_has_location (last))
3739 SET_EXPR_LOCATION (t, rexpr_location (last));
3740 append_to_statement_list (t, &expr);
3742 if (emit_false)
3744 t = build1 (LABEL_EXPR, void_type_node, false_label);
3745 append_to_statement_list (t, &expr);
3747 append_to_statement_list (else_, &expr);
3749 if (emit_end && end_label)
3751 t = build1 (LABEL_EXPR, void_type_node, end_label);
3752 append_to_statement_list (t, &expr);
3755 return expr;
3758 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3760 tree
3761 gimple_boolify (tree expr)
3763 tree type = TREE_TYPE (expr);
3764 location_t loc = EXPR_LOCATION (expr);
3766 if (TREE_CODE (expr) == NE_EXPR
3767 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3768 && integer_zerop (TREE_OPERAND (expr, 1)))
3770 tree call = TREE_OPERAND (expr, 0);
3771 tree fn = get_callee_fndecl (call);
3773 /* For __builtin_expect ((long) (x), y) recurse into x as well
3774 if x is truth_value_p. */
3775 if (fn
3776 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3777 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3778 && call_expr_nargs (call) == 2)
3780 tree arg = CALL_EXPR_ARG (call, 0);
3781 if (arg)
3783 if (TREE_CODE (arg) == NOP_EXPR
3784 && TREE_TYPE (arg) == TREE_TYPE (call))
3785 arg = TREE_OPERAND (arg, 0);
3786 if (truth_value_p (TREE_CODE (arg)))
3788 arg = gimple_boolify (arg);
3789 CALL_EXPR_ARG (call, 0)
3790 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3796 switch (TREE_CODE (expr))
3798 case TRUTH_AND_EXPR:
3799 case TRUTH_OR_EXPR:
3800 case TRUTH_XOR_EXPR:
3801 case TRUTH_ANDIF_EXPR:
3802 case TRUTH_ORIF_EXPR:
3803 /* Also boolify the arguments of truth exprs. */
3804 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3805 /* FALLTHRU */
3807 case TRUTH_NOT_EXPR:
3808 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3810 /* These expressions always produce boolean results. */
3811 if (TREE_CODE (type) != BOOLEAN_TYPE)
3812 TREE_TYPE (expr) = boolean_type_node;
3813 return expr;
3815 case ANNOTATE_EXPR:
3816 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3818 case annot_expr_ivdep_kind:
3819 case annot_expr_unroll_kind:
3820 case annot_expr_no_vector_kind:
3821 case annot_expr_vector_kind:
3822 case annot_expr_parallel_kind:
3823 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3824 if (TREE_CODE (type) != BOOLEAN_TYPE)
3825 TREE_TYPE (expr) = boolean_type_node;
3826 return expr;
3827 default:
3828 gcc_unreachable ();
3831 default:
3832 if (COMPARISON_CLASS_P (expr))
3834 /* There expressions always prduce boolean results. */
3835 if (TREE_CODE (type) != BOOLEAN_TYPE)
3836 TREE_TYPE (expr) = boolean_type_node;
3837 return expr;
3839 /* Other expressions that get here must have boolean values, but
3840 might need to be converted to the appropriate mode. */
3841 if (TREE_CODE (type) == BOOLEAN_TYPE)
3842 return expr;
3843 return fold_convert_loc (loc, boolean_type_node, expr);
3847 /* Given a conditional expression *EXPR_P without side effects, gimplify
3848 its operands. New statements are inserted to PRE_P. */
3850 static enum gimplify_status
3851 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3853 tree expr = *expr_p, cond;
3854 enum gimplify_status ret, tret;
3855 enum tree_code code;
3857 cond = gimple_boolify (COND_EXPR_COND (expr));
3859 /* We need to handle && and || specially, as their gimplification
3860 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3861 code = TREE_CODE (cond);
3862 if (code == TRUTH_ANDIF_EXPR)
3863 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3864 else if (code == TRUTH_ORIF_EXPR)
3865 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3866 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3867 COND_EXPR_COND (*expr_p) = cond;
3869 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3870 is_gimple_val, fb_rvalue);
3871 ret = MIN (ret, tret);
3872 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3873 is_gimple_val, fb_rvalue);
3875 return MIN (ret, tret);
3878 /* Return true if evaluating EXPR could trap.
3879 EXPR is GENERIC, while tree_could_trap_p can be called
3880 only on GIMPLE. */
3882 bool
3883 generic_expr_could_trap_p (tree expr)
3885 unsigned i, n;
3887 if (!expr || is_gimple_val (expr))
3888 return false;
3890 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3891 return true;
3893 n = TREE_OPERAND_LENGTH (expr);
3894 for (i = 0; i < n; i++)
3895 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3896 return true;
3898 return false;
3901 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3902 into
3904 if (p) if (p)
3905 t1 = a; a;
3906 else or else
3907 t1 = b; b;
3910 The second form is used when *EXPR_P is of type void.
3912 PRE_P points to the list where side effects that must happen before
3913 *EXPR_P should be stored. */
3915 static enum gimplify_status
3916 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3918 tree expr = *expr_p;
3919 tree type = TREE_TYPE (expr);
3920 location_t loc = EXPR_LOCATION (expr);
3921 tree tmp, arm1, arm2;
3922 enum gimplify_status ret;
3923 tree label_true, label_false, label_cont;
3924 bool have_then_clause_p, have_else_clause_p;
3925 gcond *cond_stmt;
3926 enum tree_code pred_code;
3927 gimple_seq seq = NULL;
3929 /* If this COND_EXPR has a value, copy the values into a temporary within
3930 the arms. */
3931 if (!VOID_TYPE_P (type))
3933 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3934 tree result;
3936 /* If either an rvalue is ok or we do not require an lvalue, create the
3937 temporary. But we cannot do that if the type is addressable. */
3938 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3939 && !TREE_ADDRESSABLE (type))
3941 if (gimplify_ctxp->allow_rhs_cond_expr
3942 /* If either branch has side effects or could trap, it can't be
3943 evaluated unconditionally. */
3944 && !TREE_SIDE_EFFECTS (then_)
3945 && !generic_expr_could_trap_p (then_)
3946 && !TREE_SIDE_EFFECTS (else_)
3947 && !generic_expr_could_trap_p (else_))
3948 return gimplify_pure_cond_expr (expr_p, pre_p);
3950 tmp = create_tmp_var (type, "iftmp");
3951 result = tmp;
3954 /* Otherwise, only create and copy references to the values. */
3955 else
3957 type = build_pointer_type (type);
3959 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3960 then_ = build_fold_addr_expr_loc (loc, then_);
3962 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3963 else_ = build_fold_addr_expr_loc (loc, else_);
3965 expr
3966 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3968 tmp = create_tmp_var (type, "iftmp");
3969 result = build_simple_mem_ref_loc (loc, tmp);
3972 /* Build the new then clause, `tmp = then_;'. But don't build the
3973 assignment if the value is void; in C++ it can be if it's a throw. */
3974 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3975 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3977 /* Similarly, build the new else clause, `tmp = else_;'. */
3978 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3979 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3981 TREE_TYPE (expr) = void_type_node;
3982 recalculate_side_effects (expr);
3984 /* Move the COND_EXPR to the prequeue. */
3985 gimplify_stmt (&expr, pre_p);
3987 *expr_p = result;
3988 return GS_ALL_DONE;
3991 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3992 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3993 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3994 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3996 /* Make sure the condition has BOOLEAN_TYPE. */
3997 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3999 /* Break apart && and || conditions. */
4000 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4001 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4003 expr = shortcut_cond_expr (expr);
4005 if (expr != *expr_p)
4007 *expr_p = expr;
4009 /* We can't rely on gimplify_expr to re-gimplify the expanded
4010 form properly, as cleanups might cause the target labels to be
4011 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4012 set up a conditional context. */
4013 gimple_push_condition ();
4014 gimplify_stmt (expr_p, &seq);
4015 gimple_pop_condition (pre_p);
4016 gimple_seq_add_seq (pre_p, seq);
4018 return GS_ALL_DONE;
4022 /* Now do the normal gimplification. */
4024 /* Gimplify condition. */
4025 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4026 fb_rvalue);
4027 if (ret == GS_ERROR)
4028 return GS_ERROR;
4029 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4031 gimple_push_condition ();
4033 have_then_clause_p = have_else_clause_p = false;
4034 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4035 if (label_true
4036 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4037 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4038 have different locations, otherwise we end up with incorrect
4039 location information on the branches. */
4040 && (optimize
4041 || !EXPR_HAS_LOCATION (expr)
4042 || !rexpr_has_location (label_true)
4043 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4045 have_then_clause_p = true;
4046 label_true = GOTO_DESTINATION (label_true);
4048 else
4049 label_true = create_artificial_label (UNKNOWN_LOCATION);
4050 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4051 if (label_false
4052 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4053 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4054 have different locations, otherwise we end up with incorrect
4055 location information on the branches. */
4056 && (optimize
4057 || !EXPR_HAS_LOCATION (expr)
4058 || !rexpr_has_location (label_false)
4059 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4061 have_else_clause_p = true;
4062 label_false = GOTO_DESTINATION (label_false);
4064 else
4065 label_false = create_artificial_label (UNKNOWN_LOCATION);
4067 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4068 &arm2);
4069 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4070 label_false);
4071 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4072 gimplify_seq_add_stmt (&seq, cond_stmt);
4073 gimple_stmt_iterator gsi = gsi_last (seq);
4074 maybe_fold_stmt (&gsi);
4076 label_cont = NULL_TREE;
4077 if (!have_then_clause_p)
4079 /* For if (...) {} else { code; } put label_true after
4080 the else block. */
4081 if (TREE_OPERAND (expr, 1) == NULL_TREE
4082 && !have_else_clause_p
4083 && TREE_OPERAND (expr, 2) != NULL_TREE)
4084 label_cont = label_true;
4085 else
4087 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4088 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4089 /* For if (...) { code; } else {} or
4090 if (...) { code; } else goto label; or
4091 if (...) { code; return; } else { ... }
4092 label_cont isn't needed. */
4093 if (!have_else_clause_p
4094 && TREE_OPERAND (expr, 2) != NULL_TREE
4095 && gimple_seq_may_fallthru (seq))
4097 gimple *g;
4098 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4100 g = gimple_build_goto (label_cont);
4102 /* GIMPLE_COND's are very low level; they have embedded
4103 gotos. This particular embedded goto should not be marked
4104 with the location of the original COND_EXPR, as it would
4105 correspond to the COND_EXPR's condition, not the ELSE or the
4106 THEN arms. To avoid marking it with the wrong location, flag
4107 it as "no location". */
4108 gimple_set_do_not_emit_location (g);
4110 gimplify_seq_add_stmt (&seq, g);
4114 if (!have_else_clause_p)
4116 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4117 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4119 if (label_cont)
4120 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4122 gimple_pop_condition (pre_p);
4123 gimple_seq_add_seq (pre_p, seq);
4125 if (ret == GS_ERROR)
4126 ; /* Do nothing. */
4127 else if (have_then_clause_p || have_else_clause_p)
4128 ret = GS_ALL_DONE;
4129 else
4131 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4132 expr = TREE_OPERAND (expr, 0);
4133 gimplify_stmt (&expr, pre_p);
4136 *expr_p = NULL;
4137 return ret;
4140 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4141 to be marked addressable.
4143 We cannot rely on such an expression being directly markable if a temporary
4144 has been created by the gimplification. In this case, we create another
4145 temporary and initialize it with a copy, which will become a store after we
4146 mark it addressable. This can happen if the front-end passed us something
4147 that it could not mark addressable yet, like a Fortran pass-by-reference
4148 parameter (int) floatvar. */
4150 static void
4151 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4153 while (handled_component_p (*expr_p))
4154 expr_p = &TREE_OPERAND (*expr_p, 0);
4155 if (is_gimple_reg (*expr_p))
4157 /* Do not allow an SSA name as the temporary. */
4158 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4159 DECL_GIMPLE_REG_P (var) = 0;
4160 *expr_p = var;
4164 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4165 a call to __builtin_memcpy. */
4167 static enum gimplify_status
4168 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4169 gimple_seq *seq_p)
4171 tree t, to, to_ptr, from, from_ptr;
4172 gcall *gs;
4173 location_t loc = EXPR_LOCATION (*expr_p);
4175 to = TREE_OPERAND (*expr_p, 0);
4176 from = TREE_OPERAND (*expr_p, 1);
4178 /* Mark the RHS addressable. Beware that it may not be possible to do so
4179 directly if a temporary has been created by the gimplification. */
4180 prepare_gimple_addressable (&from, seq_p);
4182 mark_addressable (from);
4183 from_ptr = build_fold_addr_expr_loc (loc, from);
4184 gimplify_arg (&from_ptr, seq_p, loc);
4186 mark_addressable (to);
4187 to_ptr = build_fold_addr_expr_loc (loc, to);
4188 gimplify_arg (&to_ptr, seq_p, loc);
4190 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4192 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4194 if (want_value)
4196 /* tmp = memcpy() */
4197 t = create_tmp_var (TREE_TYPE (to_ptr));
4198 gimple_call_set_lhs (gs, t);
4199 gimplify_seq_add_stmt (seq_p, gs);
4201 *expr_p = build_simple_mem_ref (t);
4202 return GS_ALL_DONE;
4205 gimplify_seq_add_stmt (seq_p, gs);
4206 *expr_p = NULL;
4207 return GS_ALL_DONE;
4210 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4211 a call to __builtin_memset. In this case we know that the RHS is
4212 a CONSTRUCTOR with an empty element list. */
4214 static enum gimplify_status
4215 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4216 gimple_seq *seq_p)
4218 tree t, from, to, to_ptr;
4219 gcall *gs;
4220 location_t loc = EXPR_LOCATION (*expr_p);
4222 /* Assert our assumptions, to abort instead of producing wrong code
4223 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4224 not be immediately exposed. */
4225 from = TREE_OPERAND (*expr_p, 1);
4226 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4227 from = TREE_OPERAND (from, 0);
4229 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4230 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4232 /* Now proceed. */
4233 to = TREE_OPERAND (*expr_p, 0);
4235 to_ptr = build_fold_addr_expr_loc (loc, to);
4236 gimplify_arg (&to_ptr, seq_p, loc);
4237 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4239 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4241 if (want_value)
4243 /* tmp = memset() */
4244 t = create_tmp_var (TREE_TYPE (to_ptr));
4245 gimple_call_set_lhs (gs, t);
4246 gimplify_seq_add_stmt (seq_p, gs);
4248 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4249 return GS_ALL_DONE;
4252 gimplify_seq_add_stmt (seq_p, gs);
4253 *expr_p = NULL;
4254 return GS_ALL_DONE;
4257 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4258 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4259 assignment. Return non-null if we detect a potential overlap. */
4261 struct gimplify_init_ctor_preeval_data
4263 /* The base decl of the lhs object. May be NULL, in which case we
4264 have to assume the lhs is indirect. */
4265 tree lhs_base_decl;
4267 /* The alias set of the lhs object. */
4268 alias_set_type lhs_alias_set;
4271 static tree
4272 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4274 struct gimplify_init_ctor_preeval_data *data
4275 = (struct gimplify_init_ctor_preeval_data *) xdata;
4276 tree t = *tp;
4278 /* If we find the base object, obviously we have overlap. */
4279 if (data->lhs_base_decl == t)
4280 return t;
4282 /* If the constructor component is indirect, determine if we have a
4283 potential overlap with the lhs. The only bits of information we
4284 have to go on at this point are addressability and alias sets. */
4285 if ((INDIRECT_REF_P (t)
4286 || TREE_CODE (t) == MEM_REF)
4287 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4288 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4289 return t;
4291 /* If the constructor component is a call, determine if it can hide a
4292 potential overlap with the lhs through an INDIRECT_REF like above.
4293 ??? Ugh - this is completely broken. In fact this whole analysis
4294 doesn't look conservative. */
4295 if (TREE_CODE (t) == CALL_EXPR)
4297 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4299 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4300 if (POINTER_TYPE_P (TREE_VALUE (type))
4301 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4302 && alias_sets_conflict_p (data->lhs_alias_set,
4303 get_alias_set
4304 (TREE_TYPE (TREE_VALUE (type)))))
4305 return t;
4308 if (IS_TYPE_OR_DECL_P (t))
4309 *walk_subtrees = 0;
4310 return NULL;
4313 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4314 force values that overlap with the lhs (as described by *DATA)
4315 into temporaries. */
4317 static void
4318 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4319 struct gimplify_init_ctor_preeval_data *data)
4321 enum gimplify_status one;
4323 /* If the value is constant, then there's nothing to pre-evaluate. */
4324 if (TREE_CONSTANT (*expr_p))
4326 /* Ensure it does not have side effects, it might contain a reference to
4327 the object we're initializing. */
4328 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4329 return;
4332 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4333 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4334 return;
4336 /* Recurse for nested constructors. */
4337 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4339 unsigned HOST_WIDE_INT ix;
4340 constructor_elt *ce;
4341 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4343 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4344 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4346 return;
4349 /* If this is a variable sized type, we must remember the size. */
4350 maybe_with_size_expr (expr_p);
4352 /* Gimplify the constructor element to something appropriate for the rhs
4353 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4354 the gimplifier will consider this a store to memory. Doing this
4355 gimplification now means that we won't have to deal with complicated
4356 language-specific trees, nor trees like SAVE_EXPR that can induce
4357 exponential search behavior. */
4358 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4359 if (one == GS_ERROR)
4361 *expr_p = NULL;
4362 return;
4365 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4366 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4367 always be true for all scalars, since is_gimple_mem_rhs insists on a
4368 temporary variable for them. */
4369 if (DECL_P (*expr_p))
4370 return;
4372 /* If this is of variable size, we have no choice but to assume it doesn't
4373 overlap since we can't make a temporary for it. */
4374 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4375 return;
4377 /* Otherwise, we must search for overlap ... */
4378 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4379 return;
4381 /* ... and if found, force the value into a temporary. */
4382 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4385 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4386 a RANGE_EXPR in a CONSTRUCTOR for an array.
4388 var = lower;
4389 loop_entry:
4390 object[var] = value;
4391 if (var == upper)
4392 goto loop_exit;
4393 var = var + 1;
4394 goto loop_entry;
4395 loop_exit:
4397 We increment var _after_ the loop exit check because we might otherwise
4398 fail if upper == TYPE_MAX_VALUE (type for upper).
4400 Note that we never have to deal with SAVE_EXPRs here, because this has
4401 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4403 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4404 gimple_seq *, bool);
4406 static void
4407 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4408 tree value, tree array_elt_type,
4409 gimple_seq *pre_p, bool cleared)
4411 tree loop_entry_label, loop_exit_label, fall_thru_label;
4412 tree var, var_type, cref, tmp;
4414 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4415 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4416 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4418 /* Create and initialize the index variable. */
4419 var_type = TREE_TYPE (upper);
4420 var = create_tmp_var (var_type);
4421 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4423 /* Add the loop entry label. */
4424 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4426 /* Build the reference. */
4427 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4428 var, NULL_TREE, NULL_TREE);
4430 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4431 the store. Otherwise just assign value to the reference. */
4433 if (TREE_CODE (value) == CONSTRUCTOR)
4434 /* NB we might have to call ourself recursively through
4435 gimplify_init_ctor_eval if the value is a constructor. */
4436 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4437 pre_p, cleared);
4438 else
4439 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4441 /* We exit the loop when the index var is equal to the upper bound. */
4442 gimplify_seq_add_stmt (pre_p,
4443 gimple_build_cond (EQ_EXPR, var, upper,
4444 loop_exit_label, fall_thru_label));
4446 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4448 /* Otherwise, increment the index var... */
4449 tmp = build2 (PLUS_EXPR, var_type, var,
4450 fold_convert (var_type, integer_one_node));
4451 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4453 /* ...and jump back to the loop entry. */
4454 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4456 /* Add the loop exit label. */
4457 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4460 /* Return true if FDECL is accessing a field that is zero sized. */
4462 static bool
4463 zero_sized_field_decl (const_tree fdecl)
4465 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4466 && integer_zerop (DECL_SIZE (fdecl)))
4467 return true;
4468 return false;
4471 /* Return true if TYPE is zero sized. */
4473 static bool
4474 zero_sized_type (const_tree type)
4476 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4477 && integer_zerop (TYPE_SIZE (type)))
4478 return true;
4479 return false;
4482 /* A subroutine of gimplify_init_constructor. Generate individual
4483 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4484 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4485 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4486 zeroed first. */
4488 static void
4489 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4490 gimple_seq *pre_p, bool cleared)
4492 tree array_elt_type = NULL;
4493 unsigned HOST_WIDE_INT ix;
4494 tree purpose, value;
4496 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4497 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4499 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4501 tree cref;
4503 /* NULL values are created above for gimplification errors. */
4504 if (value == NULL)
4505 continue;
4507 if (cleared && initializer_zerop (value))
4508 continue;
4510 /* ??? Here's to hoping the front end fills in all of the indices,
4511 so we don't have to figure out what's missing ourselves. */
4512 gcc_assert (purpose);
4514 /* Skip zero-sized fields, unless value has side-effects. This can
4515 happen with calls to functions returning a zero-sized type, which
4516 we shouldn't discard. As a number of downstream passes don't
4517 expect sets of zero-sized fields, we rely on the gimplification of
4518 the MODIFY_EXPR we make below to drop the assignment statement. */
4519 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4520 continue;
4522 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4523 whole range. */
4524 if (TREE_CODE (purpose) == RANGE_EXPR)
4526 tree lower = TREE_OPERAND (purpose, 0);
4527 tree upper = TREE_OPERAND (purpose, 1);
4529 /* If the lower bound is equal to upper, just treat it as if
4530 upper was the index. */
4531 if (simple_cst_equal (lower, upper))
4532 purpose = upper;
4533 else
4535 gimplify_init_ctor_eval_range (object, lower, upper, value,
4536 array_elt_type, pre_p, cleared);
4537 continue;
4541 if (array_elt_type)
4543 /* Do not use bitsizetype for ARRAY_REF indices. */
4544 if (TYPE_DOMAIN (TREE_TYPE (object)))
4545 purpose
4546 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4547 purpose);
4548 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4549 purpose, NULL_TREE, NULL_TREE);
4551 else
4553 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4554 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4555 unshare_expr (object), purpose, NULL_TREE);
4558 if (TREE_CODE (value) == CONSTRUCTOR
4559 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4560 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4561 pre_p, cleared);
4562 else
4564 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4565 gimplify_and_add (init, pre_p);
4566 ggc_free (init);
4571 /* Return the appropriate RHS predicate for this LHS. */
4573 gimple_predicate
4574 rhs_predicate_for (tree lhs)
4576 if (is_gimple_reg (lhs))
4577 return is_gimple_reg_rhs_or_call;
4578 else
4579 return is_gimple_mem_rhs_or_call;
4582 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4583 before the LHS has been gimplified. */
4585 static gimple_predicate
4586 initial_rhs_predicate_for (tree lhs)
4588 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4589 return is_gimple_reg_rhs_or_call;
4590 else
4591 return is_gimple_mem_rhs_or_call;
4594 /* Gimplify a C99 compound literal expression. This just means adding
4595 the DECL_EXPR before the current statement and using its anonymous
4596 decl instead. */
4598 static enum gimplify_status
4599 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4600 bool (*gimple_test_f) (tree),
4601 fallback_t fallback)
4603 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4604 tree decl = DECL_EXPR_DECL (decl_s);
4605 tree init = DECL_INITIAL (decl);
4606 /* Mark the decl as addressable if the compound literal
4607 expression is addressable now, otherwise it is marked too late
4608 after we gimplify the initialization expression. */
4609 if (TREE_ADDRESSABLE (*expr_p))
4610 TREE_ADDRESSABLE (decl) = 1;
4611 /* Otherwise, if we don't need an lvalue and have a literal directly
4612 substitute it. Check if it matches the gimple predicate, as
4613 otherwise we'd generate a new temporary, and we can as well just
4614 use the decl we already have. */
4615 else if (!TREE_ADDRESSABLE (decl)
4616 && init
4617 && (fallback & fb_lvalue) == 0
4618 && gimple_test_f (init))
4620 *expr_p = init;
4621 return GS_OK;
4624 /* Preliminarily mark non-addressed complex variables as eligible
4625 for promotion to gimple registers. We'll transform their uses
4626 as we find them. */
4627 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4628 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4629 && !TREE_THIS_VOLATILE (decl)
4630 && !needs_to_live_in_memory (decl))
4631 DECL_GIMPLE_REG_P (decl) = 1;
4633 /* If the decl is not addressable, then it is being used in some
4634 expression or on the right hand side of a statement, and it can
4635 be put into a readonly data section. */
4636 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4637 TREE_READONLY (decl) = 1;
4639 /* This decl isn't mentioned in the enclosing block, so add it to the
4640 list of temps. FIXME it seems a bit of a kludge to say that
4641 anonymous artificial vars aren't pushed, but everything else is. */
4642 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4643 gimple_add_tmp_var (decl);
4645 gimplify_and_add (decl_s, pre_p);
4646 *expr_p = decl;
4647 return GS_OK;
4650 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4651 return a new CONSTRUCTOR if something changed. */
4653 static tree
4654 optimize_compound_literals_in_ctor (tree orig_ctor)
4656 tree ctor = orig_ctor;
4657 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4658 unsigned int idx, num = vec_safe_length (elts);
4660 for (idx = 0; idx < num; idx++)
4662 tree value = (*elts)[idx].value;
4663 tree newval = value;
4664 if (TREE_CODE (value) == CONSTRUCTOR)
4665 newval = optimize_compound_literals_in_ctor (value);
4666 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4668 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4669 tree decl = DECL_EXPR_DECL (decl_s);
4670 tree init = DECL_INITIAL (decl);
4672 if (!TREE_ADDRESSABLE (value)
4673 && !TREE_ADDRESSABLE (decl)
4674 && init
4675 && TREE_CODE (init) == CONSTRUCTOR)
4676 newval = optimize_compound_literals_in_ctor (init);
4678 if (newval == value)
4679 continue;
4681 if (ctor == orig_ctor)
4683 ctor = copy_node (orig_ctor);
4684 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4685 elts = CONSTRUCTOR_ELTS (ctor);
4687 (*elts)[idx].value = newval;
4689 return ctor;
4692 /* A subroutine of gimplify_modify_expr. Break out elements of a
4693 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4695 Note that we still need to clear any elements that don't have explicit
4696 initializers, so if not all elements are initialized we keep the
4697 original MODIFY_EXPR, we just remove all of the constructor elements.
4699 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4700 GS_ERROR if we would have to create a temporary when gimplifying
4701 this constructor. Otherwise, return GS_OK.
4703 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4705 static enum gimplify_status
4706 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4707 bool want_value, bool notify_temp_creation)
4709 tree object, ctor, type;
4710 enum gimplify_status ret;
4711 vec<constructor_elt, va_gc> *elts;
4713 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4715 if (!notify_temp_creation)
4717 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4718 is_gimple_lvalue, fb_lvalue);
4719 if (ret == GS_ERROR)
4720 return ret;
4723 object = TREE_OPERAND (*expr_p, 0);
4724 ctor = TREE_OPERAND (*expr_p, 1)
4725 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4726 type = TREE_TYPE (ctor);
4727 elts = CONSTRUCTOR_ELTS (ctor);
4728 ret = GS_ALL_DONE;
4730 switch (TREE_CODE (type))
4732 case RECORD_TYPE:
4733 case UNION_TYPE:
4734 case QUAL_UNION_TYPE:
4735 case ARRAY_TYPE:
4737 struct gimplify_init_ctor_preeval_data preeval_data;
4738 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4739 bool cleared, complete_p, valid_const_initializer;
4741 /* Aggregate types must lower constructors to initialization of
4742 individual elements. The exception is that a CONSTRUCTOR node
4743 with no elements indicates zero-initialization of the whole. */
4744 if (vec_safe_is_empty (elts))
4746 if (notify_temp_creation)
4747 return GS_OK;
4748 break;
4751 /* Fetch information about the constructor to direct later processing.
4752 We might want to make static versions of it in various cases, and
4753 can only do so if it known to be a valid constant initializer. */
4754 valid_const_initializer
4755 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4756 &num_ctor_elements, &complete_p);
4758 /* If a const aggregate variable is being initialized, then it
4759 should never be a lose to promote the variable to be static. */
4760 if (valid_const_initializer
4761 && num_nonzero_elements > 1
4762 && TREE_READONLY (object)
4763 && VAR_P (object)
4764 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4766 if (notify_temp_creation)
4767 return GS_ERROR;
4768 DECL_INITIAL (object) = ctor;
4769 TREE_STATIC (object) = 1;
4770 if (!DECL_NAME (object))
4771 DECL_NAME (object) = create_tmp_var_name ("C");
4772 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4774 /* ??? C++ doesn't automatically append a .<number> to the
4775 assembler name, and even when it does, it looks at FE private
4776 data structures to figure out what that number should be,
4777 which are not set for this variable. I suppose this is
4778 important for local statics for inline functions, which aren't
4779 "local" in the object file sense. So in order to get a unique
4780 TU-local symbol, we must invoke the lhd version now. */
4781 lhd_set_decl_assembler_name (object);
4783 *expr_p = NULL_TREE;
4784 break;
4787 /* If there are "lots" of initialized elements, even discounting
4788 those that are not address constants (and thus *must* be
4789 computed at runtime), then partition the constructor into
4790 constant and non-constant parts. Block copy the constant
4791 parts in, then generate code for the non-constant parts. */
4792 /* TODO. There's code in cp/typeck.c to do this. */
4794 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4795 /* store_constructor will ignore the clearing of variable-sized
4796 objects. Initializers for such objects must explicitly set
4797 every field that needs to be set. */
4798 cleared = false;
4799 else if (!complete_p)
4800 /* If the constructor isn't complete, clear the whole object
4801 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4803 ??? This ought not to be needed. For any element not present
4804 in the initializer, we should simply set them to zero. Except
4805 we'd need to *find* the elements that are not present, and that
4806 requires trickery to avoid quadratic compile-time behavior in
4807 large cases or excessive memory use in small cases. */
4808 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4809 else if (num_ctor_elements - num_nonzero_elements
4810 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4811 && num_nonzero_elements < num_ctor_elements / 4)
4812 /* If there are "lots" of zeros, it's more efficient to clear
4813 the memory and then set the nonzero elements. */
4814 cleared = true;
4815 else
4816 cleared = false;
4818 /* If there are "lots" of initialized elements, and all of them
4819 are valid address constants, then the entire initializer can
4820 be dropped to memory, and then memcpy'd out. Don't do this
4821 for sparse arrays, though, as it's more efficient to follow
4822 the standard CONSTRUCTOR behavior of memset followed by
4823 individual element initialization. Also don't do this for small
4824 all-zero initializers (which aren't big enough to merit
4825 clearing), and don't try to make bitwise copies of
4826 TREE_ADDRESSABLE types. */
4828 if (valid_const_initializer
4829 && !(cleared || num_nonzero_elements == 0)
4830 && !TREE_ADDRESSABLE (type))
4832 HOST_WIDE_INT size = int_size_in_bytes (type);
4833 unsigned int align;
4835 /* ??? We can still get unbounded array types, at least
4836 from the C++ front end. This seems wrong, but attempt
4837 to work around it for now. */
4838 if (size < 0)
4840 size = int_size_in_bytes (TREE_TYPE (object));
4841 if (size >= 0)
4842 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4845 /* Find the maximum alignment we can assume for the object. */
4846 /* ??? Make use of DECL_OFFSET_ALIGN. */
4847 if (DECL_P (object))
4848 align = DECL_ALIGN (object);
4849 else
4850 align = TYPE_ALIGN (type);
4852 /* Do a block move either if the size is so small as to make
4853 each individual move a sub-unit move on average, or if it
4854 is so large as to make individual moves inefficient. */
4855 if (size > 0
4856 && num_nonzero_elements > 1
4857 && (size < num_nonzero_elements
4858 || !can_move_by_pieces (size, align)))
4860 if (notify_temp_creation)
4861 return GS_ERROR;
4863 walk_tree (&ctor, force_labels_r, NULL, NULL);
4864 ctor = tree_output_constant_def (ctor);
4865 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4866 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4867 TREE_OPERAND (*expr_p, 1) = ctor;
4869 /* This is no longer an assignment of a CONSTRUCTOR, but
4870 we still may have processing to do on the LHS. So
4871 pretend we didn't do anything here to let that happen. */
4872 return GS_UNHANDLED;
4876 /* If the target is volatile, we have non-zero elements and more than
4877 one field to assign, initialize the target from a temporary. */
4878 if (TREE_THIS_VOLATILE (object)
4879 && !TREE_ADDRESSABLE (type)
4880 && num_nonzero_elements > 0
4881 && vec_safe_length (elts) > 1)
4883 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4884 TREE_OPERAND (*expr_p, 0) = temp;
4885 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4886 *expr_p,
4887 build2 (MODIFY_EXPR, void_type_node,
4888 object, temp));
4889 return GS_OK;
4892 if (notify_temp_creation)
4893 return GS_OK;
4895 /* If there are nonzero elements and if needed, pre-evaluate to capture
4896 elements overlapping with the lhs into temporaries. We must do this
4897 before clearing to fetch the values before they are zeroed-out. */
4898 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4900 preeval_data.lhs_base_decl = get_base_address (object);
4901 if (!DECL_P (preeval_data.lhs_base_decl))
4902 preeval_data.lhs_base_decl = NULL;
4903 preeval_data.lhs_alias_set = get_alias_set (object);
4905 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4906 pre_p, post_p, &preeval_data);
4909 bool ctor_has_side_effects_p
4910 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4912 if (cleared)
4914 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4915 Note that we still have to gimplify, in order to handle the
4916 case of variable sized types. Avoid shared tree structures. */
4917 CONSTRUCTOR_ELTS (ctor) = NULL;
4918 TREE_SIDE_EFFECTS (ctor) = 0;
4919 object = unshare_expr (object);
4920 gimplify_stmt (expr_p, pre_p);
4923 /* If we have not block cleared the object, or if there are nonzero
4924 elements in the constructor, or if the constructor has side effects,
4925 add assignments to the individual scalar fields of the object. */
4926 if (!cleared
4927 || num_nonzero_elements > 0
4928 || ctor_has_side_effects_p)
4929 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4931 *expr_p = NULL_TREE;
4933 break;
4935 case COMPLEX_TYPE:
4937 tree r, i;
4939 if (notify_temp_creation)
4940 return GS_OK;
4942 /* Extract the real and imaginary parts out of the ctor. */
4943 gcc_assert (elts->length () == 2);
4944 r = (*elts)[0].value;
4945 i = (*elts)[1].value;
4946 if (r == NULL || i == NULL)
4948 tree zero = build_zero_cst (TREE_TYPE (type));
4949 if (r == NULL)
4950 r = zero;
4951 if (i == NULL)
4952 i = zero;
4955 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4956 represent creation of a complex value. */
4957 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4959 ctor = build_complex (type, r, i);
4960 TREE_OPERAND (*expr_p, 1) = ctor;
4962 else
4964 ctor = build2 (COMPLEX_EXPR, type, r, i);
4965 TREE_OPERAND (*expr_p, 1) = ctor;
4966 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4967 pre_p,
4968 post_p,
4969 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4970 fb_rvalue);
4973 break;
4975 case VECTOR_TYPE:
4977 unsigned HOST_WIDE_INT ix;
4978 constructor_elt *ce;
4980 if (notify_temp_creation)
4981 return GS_OK;
4983 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4984 if (TREE_CONSTANT (ctor))
4986 bool constant_p = true;
4987 tree value;
4989 /* Even when ctor is constant, it might contain non-*_CST
4990 elements, such as addresses or trapping values like
4991 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4992 in VECTOR_CST nodes. */
4993 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4994 if (!CONSTANT_CLASS_P (value))
4996 constant_p = false;
4997 break;
5000 if (constant_p)
5002 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5003 break;
5006 TREE_CONSTANT (ctor) = 0;
5009 /* Vector types use CONSTRUCTOR all the way through gimple
5010 compilation as a general initializer. */
5011 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5013 enum gimplify_status tret;
5014 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5015 fb_rvalue);
5016 if (tret == GS_ERROR)
5017 ret = GS_ERROR;
5018 else if (TREE_STATIC (ctor)
5019 && !initializer_constant_valid_p (ce->value,
5020 TREE_TYPE (ce->value)))
5021 TREE_STATIC (ctor) = 0;
5023 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5024 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5026 break;
5028 default:
5029 /* So how did we get a CONSTRUCTOR for a scalar type? */
5030 gcc_unreachable ();
5033 if (ret == GS_ERROR)
5034 return GS_ERROR;
5035 /* If we have gimplified both sides of the initializer but have
5036 not emitted an assignment, do so now. */
5037 if (*expr_p)
5039 tree lhs = TREE_OPERAND (*expr_p, 0);
5040 tree rhs = TREE_OPERAND (*expr_p, 1);
5041 if (want_value && object == lhs)
5042 lhs = unshare_expr (lhs);
5043 gassign *init = gimple_build_assign (lhs, rhs);
5044 gimplify_seq_add_stmt (pre_p, init);
5046 if (want_value)
5048 *expr_p = object;
5049 return GS_OK;
5051 else
5053 *expr_p = NULL;
5054 return GS_ALL_DONE;
5058 /* Given a pointer value OP0, return a simplified version of an
5059 indirection through OP0, or NULL_TREE if no simplification is
5060 possible. This may only be applied to a rhs of an expression.
5061 Note that the resulting type may be different from the type pointed
5062 to in the sense that it is still compatible from the langhooks
5063 point of view. */
5065 static tree
5066 gimple_fold_indirect_ref_rhs (tree t)
5068 return gimple_fold_indirect_ref (t);
5071 /* Subroutine of gimplify_modify_expr to do simplifications of
5072 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5073 something changes. */
5075 static enum gimplify_status
5076 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5077 gimple_seq *pre_p, gimple_seq *post_p,
5078 bool want_value)
5080 enum gimplify_status ret = GS_UNHANDLED;
5081 bool changed;
5085 changed = false;
5086 switch (TREE_CODE (*from_p))
5088 case VAR_DECL:
5089 /* If we're assigning from a read-only variable initialized with
5090 a constructor, do the direct assignment from the constructor,
5091 but only if neither source nor target are volatile since this
5092 latter assignment might end up being done on a per-field basis. */
5093 if (DECL_INITIAL (*from_p)
5094 && TREE_READONLY (*from_p)
5095 && !TREE_THIS_VOLATILE (*from_p)
5096 && !TREE_THIS_VOLATILE (*to_p)
5097 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5099 tree old_from = *from_p;
5100 enum gimplify_status subret;
5102 /* Move the constructor into the RHS. */
5103 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5105 /* Let's see if gimplify_init_constructor will need to put
5106 it in memory. */
5107 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5108 false, true);
5109 if (subret == GS_ERROR)
5111 /* If so, revert the change. */
5112 *from_p = old_from;
5114 else
5116 ret = GS_OK;
5117 changed = true;
5120 break;
5121 case INDIRECT_REF:
5123 /* If we have code like
5125 *(const A*)(A*)&x
5127 where the type of "x" is a (possibly cv-qualified variant
5128 of "A"), treat the entire expression as identical to "x".
5129 This kind of code arises in C++ when an object is bound
5130 to a const reference, and if "x" is a TARGET_EXPR we want
5131 to take advantage of the optimization below. */
5132 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5133 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5134 if (t)
5136 if (TREE_THIS_VOLATILE (t) != volatile_p)
5138 if (DECL_P (t))
5139 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5140 build_fold_addr_expr (t));
5141 if (REFERENCE_CLASS_P (t))
5142 TREE_THIS_VOLATILE (t) = volatile_p;
5144 *from_p = t;
5145 ret = GS_OK;
5146 changed = true;
5148 break;
5151 case TARGET_EXPR:
5153 /* If we are initializing something from a TARGET_EXPR, strip the
5154 TARGET_EXPR and initialize it directly, if possible. This can't
5155 be done if the initializer is void, since that implies that the
5156 temporary is set in some non-trivial way.
5158 ??? What about code that pulls out the temp and uses it
5159 elsewhere? I think that such code never uses the TARGET_EXPR as
5160 an initializer. If I'm wrong, we'll die because the temp won't
5161 have any RTL. In that case, I guess we'll need to replace
5162 references somehow. */
5163 tree init = TARGET_EXPR_INITIAL (*from_p);
5165 if (init
5166 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5167 || !TARGET_EXPR_NO_ELIDE (*from_p))
5168 && !VOID_TYPE_P (TREE_TYPE (init)))
5170 *from_p = init;
5171 ret = GS_OK;
5172 changed = true;
5175 break;
5177 case COMPOUND_EXPR:
5178 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5179 caught. */
5180 gimplify_compound_expr (from_p, pre_p, true);
5181 ret = GS_OK;
5182 changed = true;
5183 break;
5185 case CONSTRUCTOR:
5186 /* If we already made some changes, let the front end have a
5187 crack at this before we break it down. */
5188 if (ret != GS_UNHANDLED)
5189 break;
5190 /* If we're initializing from a CONSTRUCTOR, break this into
5191 individual MODIFY_EXPRs. */
5192 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5193 false);
5195 case COND_EXPR:
5196 /* If we're assigning to a non-register type, push the assignment
5197 down into the branches. This is mandatory for ADDRESSABLE types,
5198 since we cannot generate temporaries for such, but it saves a
5199 copy in other cases as well. */
5200 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5202 /* This code should mirror the code in gimplify_cond_expr. */
5203 enum tree_code code = TREE_CODE (*expr_p);
5204 tree cond = *from_p;
5205 tree result = *to_p;
5207 ret = gimplify_expr (&result, pre_p, post_p,
5208 is_gimple_lvalue, fb_lvalue);
5209 if (ret != GS_ERROR)
5210 ret = GS_OK;
5212 /* If we are going to write RESULT more than once, clear
5213 TREE_READONLY flag, otherwise we might incorrectly promote
5214 the variable to static const and initialize it at compile
5215 time in one of the branches. */
5216 if (VAR_P (result)
5217 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5218 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5219 TREE_READONLY (result) = 0;
5220 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5221 TREE_OPERAND (cond, 1)
5222 = build2 (code, void_type_node, result,
5223 TREE_OPERAND (cond, 1));
5224 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5225 TREE_OPERAND (cond, 2)
5226 = build2 (code, void_type_node, unshare_expr (result),
5227 TREE_OPERAND (cond, 2));
5229 TREE_TYPE (cond) = void_type_node;
5230 recalculate_side_effects (cond);
5232 if (want_value)
5234 gimplify_and_add (cond, pre_p);
5235 *expr_p = unshare_expr (result);
5237 else
5238 *expr_p = cond;
5239 return ret;
5241 break;
5243 case CALL_EXPR:
5244 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5245 return slot so that we don't generate a temporary. */
5246 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5247 && aggregate_value_p (*from_p, *from_p))
5249 bool use_target;
5251 if (!(rhs_predicate_for (*to_p))(*from_p))
5252 /* If we need a temporary, *to_p isn't accurate. */
5253 use_target = false;
5254 /* It's OK to use the return slot directly unless it's an NRV. */
5255 else if (TREE_CODE (*to_p) == RESULT_DECL
5256 && DECL_NAME (*to_p) == NULL_TREE
5257 && needs_to_live_in_memory (*to_p))
5258 use_target = true;
5259 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5260 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5261 /* Don't force regs into memory. */
5262 use_target = false;
5263 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5264 /* It's OK to use the target directly if it's being
5265 initialized. */
5266 use_target = true;
5267 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5268 != INTEGER_CST)
5269 /* Always use the target and thus RSO for variable-sized types.
5270 GIMPLE cannot deal with a variable-sized assignment
5271 embedded in a call statement. */
5272 use_target = true;
5273 else if (TREE_CODE (*to_p) != SSA_NAME
5274 && (!is_gimple_variable (*to_p)
5275 || needs_to_live_in_memory (*to_p)))
5276 /* Don't use the original target if it's already addressable;
5277 if its address escapes, and the called function uses the
5278 NRV optimization, a conforming program could see *to_p
5279 change before the called function returns; see c++/19317.
5280 When optimizing, the return_slot pass marks more functions
5281 as safe after we have escape info. */
5282 use_target = false;
5283 else
5284 use_target = true;
5286 if (use_target)
5288 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5289 mark_addressable (*to_p);
5292 break;
5294 case WITH_SIZE_EXPR:
5295 /* Likewise for calls that return an aggregate of non-constant size,
5296 since we would not be able to generate a temporary at all. */
5297 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5299 *from_p = TREE_OPERAND (*from_p, 0);
5300 /* We don't change ret in this case because the
5301 WITH_SIZE_EXPR might have been added in
5302 gimplify_modify_expr, so returning GS_OK would lead to an
5303 infinite loop. */
5304 changed = true;
5306 break;
5308 /* If we're initializing from a container, push the initialization
5309 inside it. */
5310 case CLEANUP_POINT_EXPR:
5311 case BIND_EXPR:
5312 case STATEMENT_LIST:
5314 tree wrap = *from_p;
5315 tree t;
5317 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5318 fb_lvalue);
5319 if (ret != GS_ERROR)
5320 ret = GS_OK;
5322 t = voidify_wrapper_expr (wrap, *expr_p);
5323 gcc_assert (t == *expr_p);
5325 if (want_value)
5327 gimplify_and_add (wrap, pre_p);
5328 *expr_p = unshare_expr (*to_p);
5330 else
5331 *expr_p = wrap;
5332 return GS_OK;
5335 case COMPOUND_LITERAL_EXPR:
5337 tree complit = TREE_OPERAND (*expr_p, 1);
5338 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5339 tree decl = DECL_EXPR_DECL (decl_s);
5340 tree init = DECL_INITIAL (decl);
5342 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5343 into struct T x = { 0, 1, 2 } if the address of the
5344 compound literal has never been taken. */
5345 if (!TREE_ADDRESSABLE (complit)
5346 && !TREE_ADDRESSABLE (decl)
5347 && init)
5349 *expr_p = copy_node (*expr_p);
5350 TREE_OPERAND (*expr_p, 1) = init;
5351 return GS_OK;
5355 default:
5356 break;
5359 while (changed);
5361 return ret;
5365 /* Return true if T looks like a valid GIMPLE statement. */
5367 static bool
5368 is_gimple_stmt (tree t)
5370 const enum tree_code code = TREE_CODE (t);
5372 switch (code)
5374 case NOP_EXPR:
5375 /* The only valid NOP_EXPR is the empty statement. */
5376 return IS_EMPTY_STMT (t);
5378 case BIND_EXPR:
5379 case COND_EXPR:
5380 /* These are only valid if they're void. */
5381 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5383 case SWITCH_EXPR:
5384 case GOTO_EXPR:
5385 case RETURN_EXPR:
5386 case LABEL_EXPR:
5387 case CASE_LABEL_EXPR:
5388 case TRY_CATCH_EXPR:
5389 case TRY_FINALLY_EXPR:
5390 case EH_FILTER_EXPR:
5391 case CATCH_EXPR:
5392 case ASM_EXPR:
5393 case STATEMENT_LIST:
5394 case OACC_PARALLEL:
5395 case OACC_KERNELS:
5396 case OACC_DATA:
5397 case OACC_HOST_DATA:
5398 case OACC_DECLARE:
5399 case OACC_UPDATE:
5400 case OACC_ENTER_DATA:
5401 case OACC_EXIT_DATA:
5402 case OACC_CACHE:
5403 case OMP_PARALLEL:
5404 case OMP_FOR:
5405 case OMP_SIMD:
5406 case OMP_DISTRIBUTE:
5407 case OACC_LOOP:
5408 case OMP_SECTIONS:
5409 case OMP_SECTION:
5410 case OMP_SINGLE:
5411 case OMP_MASTER:
5412 case OMP_TASKGROUP:
5413 case OMP_ORDERED:
5414 case OMP_CRITICAL:
5415 case OMP_TASK:
5416 case OMP_TARGET:
5417 case OMP_TARGET_DATA:
5418 case OMP_TARGET_UPDATE:
5419 case OMP_TARGET_ENTER_DATA:
5420 case OMP_TARGET_EXIT_DATA:
5421 case OMP_TASKLOOP:
5422 case OMP_TEAMS:
5423 /* These are always void. */
5424 return true;
5426 case CALL_EXPR:
5427 case MODIFY_EXPR:
5428 case PREDICT_EXPR:
5429 /* These are valid regardless of their type. */
5430 return true;
5432 default:
5433 return false;
5438 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5439 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5440 DECL_GIMPLE_REG_P set.
5442 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5443 other, unmodified part of the complex object just before the total store.
5444 As a consequence, if the object is still uninitialized, an undefined value
5445 will be loaded into a register, which may result in a spurious exception
5446 if the register is floating-point and the value happens to be a signaling
5447 NaN for example. Then the fully-fledged complex operations lowering pass
5448 followed by a DCE pass are necessary in order to fix things up. */
5450 static enum gimplify_status
5451 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5452 bool want_value)
5454 enum tree_code code, ocode;
5455 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5457 lhs = TREE_OPERAND (*expr_p, 0);
5458 rhs = TREE_OPERAND (*expr_p, 1);
5459 code = TREE_CODE (lhs);
5460 lhs = TREE_OPERAND (lhs, 0);
5462 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5463 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5464 TREE_NO_WARNING (other) = 1;
5465 other = get_formal_tmp_var (other, pre_p);
5467 realpart = code == REALPART_EXPR ? rhs : other;
5468 imagpart = code == REALPART_EXPR ? other : rhs;
5470 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5471 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5472 else
5473 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5475 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5476 *expr_p = (want_value) ? rhs : NULL_TREE;
5478 return GS_ALL_DONE;
5481 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5483 modify_expr
5484 : varname '=' rhs
5485 | '*' ID '=' rhs
5487 PRE_P points to the list where side effects that must happen before
5488 *EXPR_P should be stored.
5490 POST_P points to the list where side effects that must happen after
5491 *EXPR_P should be stored.
5493 WANT_VALUE is nonzero iff we want to use the value of this expression
5494 in another expression. */
5496 static enum gimplify_status
5497 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5498 bool want_value)
5500 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5501 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5502 enum gimplify_status ret = GS_UNHANDLED;
5503 gimple *assign;
5504 location_t loc = EXPR_LOCATION (*expr_p);
5505 gimple_stmt_iterator gsi;
5507 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5508 || TREE_CODE (*expr_p) == INIT_EXPR);
5510 /* Trying to simplify a clobber using normal logic doesn't work,
5511 so handle it here. */
5512 if (TREE_CLOBBER_P (*from_p))
5514 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5515 if (ret == GS_ERROR)
5516 return ret;
5517 gcc_assert (!want_value);
5518 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5520 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5521 pre_p, post_p);
5522 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5524 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5525 *expr_p = NULL;
5526 return GS_ALL_DONE;
5529 /* Insert pointer conversions required by the middle-end that are not
5530 required by the frontend. This fixes middle-end type checking for
5531 for example gcc.dg/redecl-6.c. */
5532 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5534 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5535 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5536 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5539 /* See if any simplifications can be done based on what the RHS is. */
5540 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5541 want_value);
5542 if (ret != GS_UNHANDLED)
5543 return ret;
5545 /* For zero sized types only gimplify the left hand side and right hand
5546 side as statements and throw away the assignment. Do this after
5547 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5548 types properly. */
5549 if (zero_sized_type (TREE_TYPE (*from_p))
5550 && !want_value
5551 /* Don't do this for calls that return addressable types, expand_call
5552 relies on those having a lhs. */
5553 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5554 && TREE_CODE (*from_p) == CALL_EXPR))
5556 gimplify_stmt (from_p, pre_p);
5557 gimplify_stmt (to_p, pre_p);
5558 *expr_p = NULL_TREE;
5559 return GS_ALL_DONE;
5562 /* If the value being copied is of variable width, compute the length
5563 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5564 before gimplifying any of the operands so that we can resolve any
5565 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5566 the size of the expression to be copied, not of the destination, so
5567 that is what we must do here. */
5568 maybe_with_size_expr (from_p);
5570 /* As a special case, we have to temporarily allow for assignments
5571 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5572 a toplevel statement, when gimplifying the GENERIC expression
5573 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5574 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5576 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5577 prevent gimplify_expr from trying to create a new temporary for
5578 foo's LHS, we tell it that it should only gimplify until it
5579 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5580 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5581 and all we need to do here is set 'a' to be its LHS. */
5583 /* Gimplify the RHS first for C++17 and bug 71104. */
5584 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5585 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5586 if (ret == GS_ERROR)
5587 return ret;
5589 /* Then gimplify the LHS. */
5590 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5591 twice we have to make sure to gimplify into non-SSA as otherwise
5592 the abnormal edge added later will make those defs not dominate
5593 their uses.
5594 ??? Technically this applies only to the registers used in the
5595 resulting non-register *TO_P. */
5596 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5597 if (saved_into_ssa
5598 && TREE_CODE (*from_p) == CALL_EXPR
5599 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5600 gimplify_ctxp->into_ssa = false;
5601 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5602 gimplify_ctxp->into_ssa = saved_into_ssa;
5603 if (ret == GS_ERROR)
5604 return ret;
5606 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5607 guess for the predicate was wrong. */
5608 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5609 if (final_pred != initial_pred)
5611 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5612 if (ret == GS_ERROR)
5613 return ret;
5616 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5617 size as argument to the call. */
5618 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5620 tree call = TREE_OPERAND (*from_p, 0);
5621 tree vlasize = TREE_OPERAND (*from_p, 1);
5623 if (TREE_CODE (call) == CALL_EXPR
5624 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5626 int nargs = call_expr_nargs (call);
5627 tree type = TREE_TYPE (call);
5628 tree ap = CALL_EXPR_ARG (call, 0);
5629 tree tag = CALL_EXPR_ARG (call, 1);
5630 tree aptag = CALL_EXPR_ARG (call, 2);
5631 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5632 IFN_VA_ARG, type,
5633 nargs + 1, ap, tag,
5634 aptag, vlasize);
5635 TREE_OPERAND (*from_p, 0) = newcall;
5639 /* Now see if the above changed *from_p to something we handle specially. */
5640 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5641 want_value);
5642 if (ret != GS_UNHANDLED)
5643 return ret;
5645 /* If we've got a variable sized assignment between two lvalues (i.e. does
5646 not involve a call), then we can make things a bit more straightforward
5647 by converting the assignment to memcpy or memset. */
5648 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5650 tree from = TREE_OPERAND (*from_p, 0);
5651 tree size = TREE_OPERAND (*from_p, 1);
5653 if (TREE_CODE (from) == CONSTRUCTOR)
5654 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5656 if (is_gimple_addressable (from))
5658 *from_p = from;
5659 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5660 pre_p);
5664 /* Transform partial stores to non-addressable complex variables into
5665 total stores. This allows us to use real instead of virtual operands
5666 for these variables, which improves optimization. */
5667 if ((TREE_CODE (*to_p) == REALPART_EXPR
5668 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5669 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5670 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5672 /* Try to alleviate the effects of the gimplification creating artificial
5673 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5674 make sure not to create DECL_DEBUG_EXPR links across functions. */
5675 if (!gimplify_ctxp->into_ssa
5676 && VAR_P (*from_p)
5677 && DECL_IGNORED_P (*from_p)
5678 && DECL_P (*to_p)
5679 && !DECL_IGNORED_P (*to_p)
5680 && decl_function_context (*to_p) == current_function_decl
5681 && decl_function_context (*from_p) == current_function_decl)
5683 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5684 DECL_NAME (*from_p)
5685 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5686 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5687 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5690 if (want_value && TREE_THIS_VOLATILE (*to_p))
5691 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5693 if (TREE_CODE (*from_p) == CALL_EXPR)
5695 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5696 instead of a GIMPLE_ASSIGN. */
5697 gcall *call_stmt;
5698 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5700 /* Gimplify internal functions created in the FEs. */
5701 int nargs = call_expr_nargs (*from_p), i;
5702 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5703 auto_vec<tree> vargs (nargs);
5705 for (i = 0; i < nargs; i++)
5707 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5708 EXPR_LOCATION (*from_p));
5709 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5711 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5712 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5713 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5715 else
5717 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5718 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5719 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5720 tree fndecl = get_callee_fndecl (*from_p);
5721 if (fndecl
5722 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5723 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5724 && call_expr_nargs (*from_p) == 3)
5725 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5726 CALL_EXPR_ARG (*from_p, 0),
5727 CALL_EXPR_ARG (*from_p, 1),
5728 CALL_EXPR_ARG (*from_p, 2));
5729 else
5731 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5734 notice_special_calls (call_stmt);
5735 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5736 gimple_call_set_lhs (call_stmt, *to_p);
5737 else if (TREE_CODE (*to_p) == SSA_NAME)
5738 /* The above is somewhat premature, avoid ICEing later for a
5739 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5740 ??? This doesn't make it a default-def. */
5741 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5743 assign = call_stmt;
5745 else
5747 assign = gimple_build_assign (*to_p, *from_p);
5748 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5749 if (COMPARISON_CLASS_P (*from_p))
5750 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5753 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5755 /* We should have got an SSA name from the start. */
5756 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5757 || ! gimple_in_ssa_p (cfun));
5760 gimplify_seq_add_stmt (pre_p, assign);
5761 gsi = gsi_last (*pre_p);
5762 maybe_fold_stmt (&gsi);
5764 if (want_value)
5766 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5767 return GS_OK;
5769 else
5770 *expr_p = NULL;
5772 return GS_ALL_DONE;
5775 /* Gimplify a comparison between two variable-sized objects. Do this
5776 with a call to BUILT_IN_MEMCMP. */
5778 static enum gimplify_status
5779 gimplify_variable_sized_compare (tree *expr_p)
5781 location_t loc = EXPR_LOCATION (*expr_p);
5782 tree op0 = TREE_OPERAND (*expr_p, 0);
5783 tree op1 = TREE_OPERAND (*expr_p, 1);
5784 tree t, arg, dest, src, expr;
5786 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5787 arg = unshare_expr (arg);
5788 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5789 src = build_fold_addr_expr_loc (loc, op1);
5790 dest = build_fold_addr_expr_loc (loc, op0);
5791 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5792 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5794 expr
5795 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5796 SET_EXPR_LOCATION (expr, loc);
5797 *expr_p = expr;
5799 return GS_OK;
5802 /* Gimplify a comparison between two aggregate objects of integral scalar
5803 mode as a comparison between the bitwise equivalent scalar values. */
5805 static enum gimplify_status
5806 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5808 location_t loc = EXPR_LOCATION (*expr_p);
5809 tree op0 = TREE_OPERAND (*expr_p, 0);
5810 tree op1 = TREE_OPERAND (*expr_p, 1);
5812 tree type = TREE_TYPE (op0);
5813 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5815 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5816 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5818 *expr_p
5819 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5821 return GS_OK;
5824 /* Gimplify an expression sequence. This function gimplifies each
5825 expression and rewrites the original expression with the last
5826 expression of the sequence in GIMPLE form.
5828 PRE_P points to the list where the side effects for all the
5829 expressions in the sequence will be emitted.
5831 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5833 static enum gimplify_status
5834 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5836 tree t = *expr_p;
5840 tree *sub_p = &TREE_OPERAND (t, 0);
5842 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5843 gimplify_compound_expr (sub_p, pre_p, false);
5844 else
5845 gimplify_stmt (sub_p, pre_p);
5847 t = TREE_OPERAND (t, 1);
5849 while (TREE_CODE (t) == COMPOUND_EXPR);
5851 *expr_p = t;
5852 if (want_value)
5853 return GS_OK;
5854 else
5856 gimplify_stmt (expr_p, pre_p);
5857 return GS_ALL_DONE;
5861 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5862 gimplify. After gimplification, EXPR_P will point to a new temporary
5863 that holds the original value of the SAVE_EXPR node.
5865 PRE_P points to the list where side effects that must happen before
5866 *EXPR_P should be stored. */
5868 static enum gimplify_status
5869 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5871 enum gimplify_status ret = GS_ALL_DONE;
5872 tree val;
5874 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5875 val = TREE_OPERAND (*expr_p, 0);
5877 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5878 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5880 /* The operand may be a void-valued expression. It is
5881 being executed only for its side-effects. */
5882 if (TREE_TYPE (val) == void_type_node)
5884 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5885 is_gimple_stmt, fb_none);
5886 val = NULL;
5888 else
5889 /* The temporary may not be an SSA name as later abnormal and EH
5890 control flow may invalidate use/def domination. When in SSA
5891 form then assume there are no such issues and SAVE_EXPRs only
5892 appear via GENERIC foldings. */
5893 val = get_initialized_tmp_var (val, pre_p, post_p,
5894 gimple_in_ssa_p (cfun));
5896 TREE_OPERAND (*expr_p, 0) = val;
5897 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5900 *expr_p = val;
5902 return ret;
5905 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5907 unary_expr
5908 : ...
5909 | '&' varname
5912 PRE_P points to the list where side effects that must happen before
5913 *EXPR_P should be stored.
5915 POST_P points to the list where side effects that must happen after
5916 *EXPR_P should be stored. */
5918 static enum gimplify_status
5919 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5921 tree expr = *expr_p;
5922 tree op0 = TREE_OPERAND (expr, 0);
5923 enum gimplify_status ret;
5924 location_t loc = EXPR_LOCATION (*expr_p);
5926 switch (TREE_CODE (op0))
5928 case INDIRECT_REF:
5929 do_indirect_ref:
5930 /* Check if we are dealing with an expression of the form '&*ptr'.
5931 While the front end folds away '&*ptr' into 'ptr', these
5932 expressions may be generated internally by the compiler (e.g.,
5933 builtins like __builtin_va_end). */
5934 /* Caution: the silent array decomposition semantics we allow for
5935 ADDR_EXPR means we can't always discard the pair. */
5936 /* Gimplification of the ADDR_EXPR operand may drop
5937 cv-qualification conversions, so make sure we add them if
5938 needed. */
5940 tree op00 = TREE_OPERAND (op0, 0);
5941 tree t_expr = TREE_TYPE (expr);
5942 tree t_op00 = TREE_TYPE (op00);
5944 if (!useless_type_conversion_p (t_expr, t_op00))
5945 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5946 *expr_p = op00;
5947 ret = GS_OK;
5949 break;
5951 case VIEW_CONVERT_EXPR:
5952 /* Take the address of our operand and then convert it to the type of
5953 this ADDR_EXPR.
5955 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5956 all clear. The impact of this transformation is even less clear. */
5958 /* If the operand is a useless conversion, look through it. Doing so
5959 guarantees that the ADDR_EXPR and its operand will remain of the
5960 same type. */
5961 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5962 op0 = TREE_OPERAND (op0, 0);
5964 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5965 build_fold_addr_expr_loc (loc,
5966 TREE_OPERAND (op0, 0)));
5967 ret = GS_OK;
5968 break;
5970 case MEM_REF:
5971 if (integer_zerop (TREE_OPERAND (op0, 1)))
5972 goto do_indirect_ref;
5974 /* fall through */
5976 default:
5977 /* If we see a call to a declared builtin or see its address
5978 being taken (we can unify those cases here) then we can mark
5979 the builtin for implicit generation by GCC. */
5980 if (TREE_CODE (op0) == FUNCTION_DECL
5981 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5982 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5983 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5985 /* We use fb_either here because the C frontend sometimes takes
5986 the address of a call that returns a struct; see
5987 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5988 the implied temporary explicit. */
5990 /* Make the operand addressable. */
5991 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5992 is_gimple_addressable, fb_either);
5993 if (ret == GS_ERROR)
5994 break;
5996 /* Then mark it. Beware that it may not be possible to do so directly
5997 if a temporary has been created by the gimplification. */
5998 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6000 op0 = TREE_OPERAND (expr, 0);
6002 /* For various reasons, the gimplification of the expression
6003 may have made a new INDIRECT_REF. */
6004 if (TREE_CODE (op0) == INDIRECT_REF)
6005 goto do_indirect_ref;
6007 mark_addressable (TREE_OPERAND (expr, 0));
6009 /* The FEs may end up building ADDR_EXPRs early on a decl with
6010 an incomplete type. Re-build ADDR_EXPRs in canonical form
6011 here. */
6012 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6013 *expr_p = build_fold_addr_expr (op0);
6015 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6016 recompute_tree_invariant_for_addr_expr (*expr_p);
6018 /* If we re-built the ADDR_EXPR add a conversion to the original type
6019 if required. */
6020 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6021 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6023 break;
6026 return ret;
6029 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6030 value; output operands should be a gimple lvalue. */
6032 static enum gimplify_status
6033 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6035 tree expr;
6036 int noutputs;
6037 const char **oconstraints;
6038 int i;
6039 tree link;
6040 const char *constraint;
6041 bool allows_mem, allows_reg, is_inout;
6042 enum gimplify_status ret, tret;
6043 gasm *stmt;
6044 vec<tree, va_gc> *inputs;
6045 vec<tree, va_gc> *outputs;
6046 vec<tree, va_gc> *clobbers;
6047 vec<tree, va_gc> *labels;
6048 tree link_next;
6050 expr = *expr_p;
6051 noutputs = list_length (ASM_OUTPUTS (expr));
6052 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6054 inputs = NULL;
6055 outputs = NULL;
6056 clobbers = NULL;
6057 labels = NULL;
6059 ret = GS_ALL_DONE;
6060 link_next = NULL_TREE;
6061 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6063 bool ok;
6064 size_t constraint_len;
6066 link_next = TREE_CHAIN (link);
6068 oconstraints[i]
6069 = constraint
6070 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6071 constraint_len = strlen (constraint);
6072 if (constraint_len == 0)
6073 continue;
6075 ok = parse_output_constraint (&constraint, i, 0, 0,
6076 &allows_mem, &allows_reg, &is_inout);
6077 if (!ok)
6079 ret = GS_ERROR;
6080 is_inout = false;
6083 if (!allows_reg && allows_mem)
6084 mark_addressable (TREE_VALUE (link));
6086 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6087 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6088 fb_lvalue | fb_mayfail);
6089 if (tret == GS_ERROR)
6091 error ("invalid lvalue in asm output %d", i);
6092 ret = tret;
6095 /* If the constraint does not allow memory make sure we gimplify
6096 it to a register if it is not already but its base is. This
6097 happens for complex and vector components. */
6098 if (!allows_mem)
6100 tree op = TREE_VALUE (link);
6101 if (! is_gimple_val (op)
6102 && is_gimple_reg_type (TREE_TYPE (op))
6103 && is_gimple_reg (get_base_address (op)))
6105 tree tem = create_tmp_reg (TREE_TYPE (op));
6106 tree ass;
6107 if (is_inout)
6109 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6110 tem, unshare_expr (op));
6111 gimplify_and_add (ass, pre_p);
6113 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6114 gimplify_and_add (ass, post_p);
6116 TREE_VALUE (link) = tem;
6117 tret = GS_OK;
6121 vec_safe_push (outputs, link);
6122 TREE_CHAIN (link) = NULL_TREE;
6124 if (is_inout)
6126 /* An input/output operand. To give the optimizers more
6127 flexibility, split it into separate input and output
6128 operands. */
6129 tree input;
6130 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6131 char buf[11];
6133 /* Turn the in/out constraint into an output constraint. */
6134 char *p = xstrdup (constraint);
6135 p[0] = '=';
6136 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6138 /* And add a matching input constraint. */
6139 if (allows_reg)
6141 sprintf (buf, "%u", i);
6143 /* If there are multiple alternatives in the constraint,
6144 handle each of them individually. Those that allow register
6145 will be replaced with operand number, the others will stay
6146 unchanged. */
6147 if (strchr (p, ',') != NULL)
6149 size_t len = 0, buflen = strlen (buf);
6150 char *beg, *end, *str, *dst;
6152 for (beg = p + 1;;)
6154 end = strchr (beg, ',');
6155 if (end == NULL)
6156 end = strchr (beg, '\0');
6157 if ((size_t) (end - beg) < buflen)
6158 len += buflen + 1;
6159 else
6160 len += end - beg + 1;
6161 if (*end)
6162 beg = end + 1;
6163 else
6164 break;
6167 str = (char *) alloca (len);
6168 for (beg = p + 1, dst = str;;)
6170 const char *tem;
6171 bool mem_p, reg_p, inout_p;
6173 end = strchr (beg, ',');
6174 if (end)
6175 *end = '\0';
6176 beg[-1] = '=';
6177 tem = beg - 1;
6178 parse_output_constraint (&tem, i, 0, 0,
6179 &mem_p, &reg_p, &inout_p);
6180 if (dst != str)
6181 *dst++ = ',';
6182 if (reg_p)
6184 memcpy (dst, buf, buflen);
6185 dst += buflen;
6187 else
6189 if (end)
6190 len = end - beg;
6191 else
6192 len = strlen (beg);
6193 memcpy (dst, beg, len);
6194 dst += len;
6196 if (end)
6197 beg = end + 1;
6198 else
6199 break;
6201 *dst = '\0';
6202 input = build_string (dst - str, str);
6204 else
6205 input = build_string (strlen (buf), buf);
6207 else
6208 input = build_string (constraint_len - 1, constraint + 1);
6210 free (p);
6212 input = build_tree_list (build_tree_list (NULL_TREE, input),
6213 unshare_expr (TREE_VALUE (link)));
6214 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6218 link_next = NULL_TREE;
6219 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6221 link_next = TREE_CHAIN (link);
6222 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6223 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6224 oconstraints, &allows_mem, &allows_reg);
6226 /* If we can't make copies, we can only accept memory. */
6227 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6229 if (allows_mem)
6230 allows_reg = 0;
6231 else
6233 error ("impossible constraint in %<asm%>");
6234 error ("non-memory input %d must stay in memory", i);
6235 return GS_ERROR;
6239 /* If the operand is a memory input, it should be an lvalue. */
6240 if (!allows_reg && allows_mem)
6242 tree inputv = TREE_VALUE (link);
6243 STRIP_NOPS (inputv);
6244 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6245 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6246 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6247 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6248 || TREE_CODE (inputv) == MODIFY_EXPR)
6249 TREE_VALUE (link) = error_mark_node;
6250 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6251 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6252 if (tret != GS_ERROR)
6254 /* Unlike output operands, memory inputs are not guaranteed
6255 to be lvalues by the FE, and while the expressions are
6256 marked addressable there, if it is e.g. a statement
6257 expression, temporaries in it might not end up being
6258 addressable. They might be already used in the IL and thus
6259 it is too late to make them addressable now though. */
6260 tree x = TREE_VALUE (link);
6261 while (handled_component_p (x))
6262 x = TREE_OPERAND (x, 0);
6263 if (TREE_CODE (x) == MEM_REF
6264 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6265 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6266 if ((VAR_P (x)
6267 || TREE_CODE (x) == PARM_DECL
6268 || TREE_CODE (x) == RESULT_DECL)
6269 && !TREE_ADDRESSABLE (x)
6270 && is_gimple_reg (x))
6272 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6273 input_location), 0,
6274 "memory input %d is not directly addressable",
6276 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6279 mark_addressable (TREE_VALUE (link));
6280 if (tret == GS_ERROR)
6282 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6283 "memory input %d is not directly addressable", i);
6284 ret = tret;
6287 else
6289 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6290 is_gimple_asm_val, fb_rvalue);
6291 if (tret == GS_ERROR)
6292 ret = tret;
6295 TREE_CHAIN (link) = NULL_TREE;
6296 vec_safe_push (inputs, link);
6299 link_next = NULL_TREE;
6300 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6302 link_next = TREE_CHAIN (link);
6303 TREE_CHAIN (link) = NULL_TREE;
6304 vec_safe_push (clobbers, link);
6307 link_next = NULL_TREE;
6308 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6310 link_next = TREE_CHAIN (link);
6311 TREE_CHAIN (link) = NULL_TREE;
6312 vec_safe_push (labels, link);
6315 /* Do not add ASMs with errors to the gimple IL stream. */
6316 if (ret != GS_ERROR)
6318 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6319 inputs, outputs, clobbers, labels);
6321 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6322 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6324 gimplify_seq_add_stmt (pre_p, stmt);
6327 return ret;
6330 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6331 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6332 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6333 return to this function.
6335 FIXME should we complexify the prequeue handling instead? Or use flags
6336 for all the cleanups and let the optimizer tighten them up? The current
6337 code seems pretty fragile; it will break on a cleanup within any
6338 non-conditional nesting. But any such nesting would be broken, anyway;
6339 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6340 and continues out of it. We can do that at the RTL level, though, so
6341 having an optimizer to tighten up try/finally regions would be a Good
6342 Thing. */
6344 static enum gimplify_status
6345 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6347 gimple_stmt_iterator iter;
6348 gimple_seq body_sequence = NULL;
6350 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6352 /* We only care about the number of conditions between the innermost
6353 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6354 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6355 int old_conds = gimplify_ctxp->conditions;
6356 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6357 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6358 gimplify_ctxp->conditions = 0;
6359 gimplify_ctxp->conditional_cleanups = NULL;
6360 gimplify_ctxp->in_cleanup_point_expr = true;
6362 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6364 gimplify_ctxp->conditions = old_conds;
6365 gimplify_ctxp->conditional_cleanups = old_cleanups;
6366 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6368 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6370 gimple *wce = gsi_stmt (iter);
6372 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6374 if (gsi_one_before_end_p (iter))
6376 /* Note that gsi_insert_seq_before and gsi_remove do not
6377 scan operands, unlike some other sequence mutators. */
6378 if (!gimple_wce_cleanup_eh_only (wce))
6379 gsi_insert_seq_before_without_update (&iter,
6380 gimple_wce_cleanup (wce),
6381 GSI_SAME_STMT);
6382 gsi_remove (&iter, true);
6383 break;
6385 else
6387 gtry *gtry;
6388 gimple_seq seq;
6389 enum gimple_try_flags kind;
6391 if (gimple_wce_cleanup_eh_only (wce))
6392 kind = GIMPLE_TRY_CATCH;
6393 else
6394 kind = GIMPLE_TRY_FINALLY;
6395 seq = gsi_split_seq_after (iter);
6397 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6398 /* Do not use gsi_replace here, as it may scan operands.
6399 We want to do a simple structural modification only. */
6400 gsi_set_stmt (&iter, gtry);
6401 iter = gsi_start (gtry->eval);
6404 else
6405 gsi_next (&iter);
6408 gimplify_seq_add_seq (pre_p, body_sequence);
6409 if (temp)
6411 *expr_p = temp;
6412 return GS_OK;
6414 else
6416 *expr_p = NULL;
6417 return GS_ALL_DONE;
6421 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6422 is the cleanup action required. EH_ONLY is true if the cleanup should
6423 only be executed if an exception is thrown, not on normal exit.
6424 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6425 only valid for clobbers. */
6427 static void
6428 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6429 bool force_uncond = false)
6431 gimple *wce;
6432 gimple_seq cleanup_stmts = NULL;
6434 /* Errors can result in improperly nested cleanups. Which results in
6435 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6436 if (seen_error ())
6437 return;
6439 if (gimple_conditional_context ())
6441 /* If we're in a conditional context, this is more complex. We only
6442 want to run the cleanup if we actually ran the initialization that
6443 necessitates it, but we want to run it after the end of the
6444 conditional context. So we wrap the try/finally around the
6445 condition and use a flag to determine whether or not to actually
6446 run the destructor. Thus
6448 test ? f(A()) : 0
6450 becomes (approximately)
6452 flag = 0;
6453 try {
6454 if (test) { A::A(temp); flag = 1; val = f(temp); }
6455 else { val = 0; }
6456 } finally {
6457 if (flag) A::~A(temp);
6461 if (force_uncond)
6463 gimplify_stmt (&cleanup, &cleanup_stmts);
6464 wce = gimple_build_wce (cleanup_stmts);
6465 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6467 else
6469 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6470 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6471 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6473 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6474 gimplify_stmt (&cleanup, &cleanup_stmts);
6475 wce = gimple_build_wce (cleanup_stmts);
6477 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6478 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6479 gimplify_seq_add_stmt (pre_p, ftrue);
6481 /* Because of this manipulation, and the EH edges that jump
6482 threading cannot redirect, the temporary (VAR) will appear
6483 to be used uninitialized. Don't warn. */
6484 TREE_NO_WARNING (var) = 1;
6487 else
6489 gimplify_stmt (&cleanup, &cleanup_stmts);
6490 wce = gimple_build_wce (cleanup_stmts);
6491 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6492 gimplify_seq_add_stmt (pre_p, wce);
6496 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6498 static enum gimplify_status
6499 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6501 tree targ = *expr_p;
6502 tree temp = TARGET_EXPR_SLOT (targ);
6503 tree init = TARGET_EXPR_INITIAL (targ);
6504 enum gimplify_status ret;
6506 bool unpoison_empty_seq = false;
6507 gimple_stmt_iterator unpoison_it;
6509 if (init)
6511 tree cleanup = NULL_TREE;
6513 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6514 to the temps list. Handle also variable length TARGET_EXPRs. */
6515 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6517 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6518 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6519 gimplify_vla_decl (temp, pre_p);
6521 else
6523 /* Save location where we need to place unpoisoning. It's possible
6524 that a variable will be converted to needs_to_live_in_memory. */
6525 unpoison_it = gsi_last (*pre_p);
6526 unpoison_empty_seq = gsi_end_p (unpoison_it);
6528 gimple_add_tmp_var (temp);
6531 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6532 expression is supposed to initialize the slot. */
6533 if (VOID_TYPE_P (TREE_TYPE (init)))
6534 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6535 else
6537 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6538 init = init_expr;
6539 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6540 init = NULL;
6541 ggc_free (init_expr);
6543 if (ret == GS_ERROR)
6545 /* PR c++/28266 Make sure this is expanded only once. */
6546 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6547 return GS_ERROR;
6549 if (init)
6550 gimplify_and_add (init, pre_p);
6552 /* If needed, push the cleanup for the temp. */
6553 if (TARGET_EXPR_CLEANUP (targ))
6555 if (CLEANUP_EH_ONLY (targ))
6556 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6557 CLEANUP_EH_ONLY (targ), pre_p);
6558 else
6559 cleanup = TARGET_EXPR_CLEANUP (targ);
6562 /* Add a clobber for the temporary going out of scope, like
6563 gimplify_bind_expr. */
6564 if (gimplify_ctxp->in_cleanup_point_expr
6565 && needs_to_live_in_memory (temp))
6567 if (flag_stack_reuse == SR_ALL)
6569 tree clobber = build_clobber (TREE_TYPE (temp));
6570 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6571 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6573 if (asan_poisoned_variables
6574 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6575 && dbg_cnt (asan_use_after_scope)
6576 && !gimplify_omp_ctxp)
6578 tree asan_cleanup = build_asan_poison_call_expr (temp);
6579 if (asan_cleanup)
6581 if (unpoison_empty_seq)
6582 unpoison_it = gsi_start (*pre_p);
6584 asan_poison_variable (temp, false, &unpoison_it,
6585 unpoison_empty_seq);
6586 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6590 if (cleanup)
6591 gimple_push_cleanup (temp, cleanup, false, pre_p);
6593 /* Only expand this once. */
6594 TREE_OPERAND (targ, 3) = init;
6595 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6597 else
6598 /* We should have expanded this before. */
6599 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6601 *expr_p = temp;
6602 return GS_OK;
6605 /* Gimplification of expression trees. */
6607 /* Gimplify an expression which appears at statement context. The
6608 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6609 NULL, a new sequence is allocated.
6611 Return true if we actually added a statement to the queue. */
6613 bool
6614 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6616 gimple_seq_node last;
6618 last = gimple_seq_last (*seq_p);
6619 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6620 return last != gimple_seq_last (*seq_p);
6623 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6624 to CTX. If entries already exist, force them to be some flavor of private.
6625 If there is no enclosing parallel, do nothing. */
6627 void
6628 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6630 splay_tree_node n;
6632 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6633 return;
6637 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6638 if (n != NULL)
6640 if (n->value & GOVD_SHARED)
6641 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6642 else if (n->value & GOVD_MAP)
6643 n->value |= GOVD_MAP_TO_ONLY;
6644 else
6645 return;
6647 else if ((ctx->region_type & ORT_TARGET) != 0)
6649 if (ctx->target_map_scalars_firstprivate)
6650 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6651 else
6652 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6654 else if (ctx->region_type != ORT_WORKSHARE
6655 && ctx->region_type != ORT_SIMD
6656 && ctx->region_type != ORT_ACC
6657 && !(ctx->region_type & ORT_TARGET_DATA))
6658 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6660 ctx = ctx->outer_context;
6662 while (ctx);
6665 /* Similarly for each of the type sizes of TYPE. */
6667 static void
6668 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6670 if (type == NULL || type == error_mark_node)
6671 return;
6672 type = TYPE_MAIN_VARIANT (type);
6674 if (ctx->privatized_types->add (type))
6675 return;
6677 switch (TREE_CODE (type))
6679 case INTEGER_TYPE:
6680 case ENUMERAL_TYPE:
6681 case BOOLEAN_TYPE:
6682 case REAL_TYPE:
6683 case FIXED_POINT_TYPE:
6684 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6685 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6686 break;
6688 case ARRAY_TYPE:
6689 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6690 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6691 break;
6693 case RECORD_TYPE:
6694 case UNION_TYPE:
6695 case QUAL_UNION_TYPE:
6697 tree field;
6698 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6699 if (TREE_CODE (field) == FIELD_DECL)
6701 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6702 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6705 break;
6707 case POINTER_TYPE:
6708 case REFERENCE_TYPE:
6709 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6710 break;
6712 default:
6713 break;
6716 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6717 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6718 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6721 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6723 static void
6724 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6726 splay_tree_node n;
6727 unsigned int nflags;
6728 tree t;
6730 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6731 return;
6733 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6734 there are constructors involved somewhere. Exception is a shared clause,
6735 there is nothing privatized in that case. */
6736 if ((flags & GOVD_SHARED) == 0
6737 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6738 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6739 flags |= GOVD_SEEN;
6741 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6742 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6744 /* We shouldn't be re-adding the decl with the same data
6745 sharing class. */
6746 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6747 nflags = n->value | flags;
6748 /* The only combination of data sharing classes we should see is
6749 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6750 reduction variables to be used in data sharing clauses. */
6751 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6752 || ((nflags & GOVD_DATA_SHARE_CLASS)
6753 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6754 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6755 n->value = nflags;
6756 return;
6759 /* When adding a variable-sized variable, we have to handle all sorts
6760 of additional bits of data: the pointer replacement variable, and
6761 the parameters of the type. */
6762 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6764 /* Add the pointer replacement variable as PRIVATE if the variable
6765 replacement is private, else FIRSTPRIVATE since we'll need the
6766 address of the original variable either for SHARED, or for the
6767 copy into or out of the context. */
6768 if (!(flags & GOVD_LOCAL))
6770 if (flags & GOVD_MAP)
6771 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6772 else if (flags & GOVD_PRIVATE)
6773 nflags = GOVD_PRIVATE;
6774 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6775 && (flags & GOVD_FIRSTPRIVATE))
6776 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6777 else
6778 nflags = GOVD_FIRSTPRIVATE;
6779 nflags |= flags & GOVD_SEEN;
6780 t = DECL_VALUE_EXPR (decl);
6781 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6782 t = TREE_OPERAND (t, 0);
6783 gcc_assert (DECL_P (t));
6784 omp_add_variable (ctx, t, nflags);
6787 /* Add all of the variable and type parameters (which should have
6788 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6789 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6790 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6791 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6793 /* The variable-sized variable itself is never SHARED, only some form
6794 of PRIVATE. The sharing would take place via the pointer variable
6795 which we remapped above. */
6796 if (flags & GOVD_SHARED)
6797 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6798 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6800 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6801 alloca statement we generate for the variable, so make sure it
6802 is available. This isn't automatically needed for the SHARED
6803 case, since we won't be allocating local storage then.
6804 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6805 in this case omp_notice_variable will be called later
6806 on when it is gimplified. */
6807 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6808 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6809 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6811 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6812 && lang_hooks.decls.omp_privatize_by_reference (decl))
6814 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6816 /* Similar to the direct variable sized case above, we'll need the
6817 size of references being privatized. */
6818 if ((flags & GOVD_SHARED) == 0)
6820 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6821 if (DECL_P (t))
6822 omp_notice_variable (ctx, t, true);
6826 if (n != NULL)
6827 n->value |= flags;
6828 else
6829 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6831 /* For reductions clauses in OpenACC loop directives, by default create a
6832 copy clause on the enclosing parallel construct for carrying back the
6833 results. */
6834 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6836 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6837 while (outer_ctx)
6839 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6840 if (n != NULL)
6842 /* Ignore local variables and explicitly declared clauses. */
6843 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6844 break;
6845 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6847 /* According to the OpenACC spec, such a reduction variable
6848 should already have a copy map on a kernels construct,
6849 verify that here. */
6850 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6851 && (n->value & GOVD_MAP));
6853 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6855 /* Remove firstprivate and make it a copy map. */
6856 n->value &= ~GOVD_FIRSTPRIVATE;
6857 n->value |= GOVD_MAP;
6860 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6862 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6863 GOVD_MAP | GOVD_SEEN);
6864 break;
6866 outer_ctx = outer_ctx->outer_context;
6871 /* Notice a threadprivate variable DECL used in OMP context CTX.
6872 This just prints out diagnostics about threadprivate variable uses
6873 in untied tasks. If DECL2 is non-NULL, prevent this warning
6874 on that variable. */
6876 static bool
6877 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6878 tree decl2)
6880 splay_tree_node n;
6881 struct gimplify_omp_ctx *octx;
6883 for (octx = ctx; octx; octx = octx->outer_context)
6884 if ((octx->region_type & ORT_TARGET) != 0)
6886 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6887 if (n == NULL)
6889 error ("threadprivate variable %qE used in target region",
6890 DECL_NAME (decl));
6891 error_at (octx->location, "enclosing target region");
6892 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6894 if (decl2)
6895 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6898 if (ctx->region_type != ORT_UNTIED_TASK)
6899 return false;
6900 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6901 if (n == NULL)
6903 error ("threadprivate variable %qE used in untied task",
6904 DECL_NAME (decl));
6905 error_at (ctx->location, "enclosing task");
6906 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6908 if (decl2)
6909 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6910 return false;
6913 /* Return true if global var DECL is device resident. */
6915 static bool
6916 device_resident_p (tree decl)
6918 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6920 if (!attr)
6921 return false;
6923 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6925 tree c = TREE_VALUE (t);
6926 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6927 return true;
6930 return false;
6933 /* Return true if DECL has an ACC DECLARE attribute. */
6935 static bool
6936 is_oacc_declared (tree decl)
6938 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6939 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6940 return declared != NULL_TREE;
6943 /* Determine outer default flags for DECL mentioned in an OMP region
6944 but not declared in an enclosing clause.
6946 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6947 remapped firstprivate instead of shared. To some extent this is
6948 addressed in omp_firstprivatize_type_sizes, but not
6949 effectively. */
6951 static unsigned
6952 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6953 bool in_code, unsigned flags)
6955 enum omp_clause_default_kind default_kind = ctx->default_kind;
6956 enum omp_clause_default_kind kind;
6958 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6959 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6960 default_kind = kind;
6962 switch (default_kind)
6964 case OMP_CLAUSE_DEFAULT_NONE:
6966 const char *rtype;
6968 if (ctx->region_type & ORT_PARALLEL)
6969 rtype = "parallel";
6970 else if (ctx->region_type & ORT_TASK)
6971 rtype = "task";
6972 else if (ctx->region_type & ORT_TEAMS)
6973 rtype = "teams";
6974 else
6975 gcc_unreachable ();
6977 error ("%qE not specified in enclosing %qs",
6978 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
6979 error_at (ctx->location, "enclosing %qs", rtype);
6981 /* FALLTHRU */
6982 case OMP_CLAUSE_DEFAULT_SHARED:
6983 flags |= GOVD_SHARED;
6984 break;
6985 case OMP_CLAUSE_DEFAULT_PRIVATE:
6986 flags |= GOVD_PRIVATE;
6987 break;
6988 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6989 flags |= GOVD_FIRSTPRIVATE;
6990 break;
6991 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6992 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6993 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6994 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
6996 omp_notice_variable (octx, decl, in_code);
6997 for (; octx; octx = octx->outer_context)
6999 splay_tree_node n2;
7001 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7002 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7003 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7004 continue;
7005 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7007 flags |= GOVD_FIRSTPRIVATE;
7008 goto found_outer;
7010 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7012 flags |= GOVD_SHARED;
7013 goto found_outer;
7018 if (TREE_CODE (decl) == PARM_DECL
7019 || (!is_global_var (decl)
7020 && DECL_CONTEXT (decl) == current_function_decl))
7021 flags |= GOVD_FIRSTPRIVATE;
7022 else
7023 flags |= GOVD_SHARED;
7024 found_outer:
7025 break;
7027 default:
7028 gcc_unreachable ();
7031 return flags;
7035 /* Determine outer default flags for DECL mentioned in an OACC region
7036 but not declared in an enclosing clause. */
7038 static unsigned
7039 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7041 const char *rkind;
7042 bool on_device = false;
7043 bool declared = is_oacc_declared (decl);
7044 tree type = TREE_TYPE (decl);
7046 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7047 type = TREE_TYPE (type);
7049 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7050 && is_global_var (decl)
7051 && device_resident_p (decl))
7053 on_device = true;
7054 flags |= GOVD_MAP_TO_ONLY;
7057 switch (ctx->region_type)
7059 case ORT_ACC_KERNELS:
7060 rkind = "kernels";
7062 if (AGGREGATE_TYPE_P (type))
7064 /* Aggregates default to 'present_or_copy', or 'present'. */
7065 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7066 flags |= GOVD_MAP;
7067 else
7068 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7070 else
7071 /* Scalars default to 'copy'. */
7072 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7074 break;
7076 case ORT_ACC_PARALLEL:
7077 rkind = "parallel";
7079 if (on_device || declared)
7080 flags |= GOVD_MAP;
7081 else if (AGGREGATE_TYPE_P (type))
7083 /* Aggregates default to 'present_or_copy', or 'present'. */
7084 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7085 flags |= GOVD_MAP;
7086 else
7087 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7089 else
7090 /* Scalars default to 'firstprivate'. */
7091 flags |= GOVD_FIRSTPRIVATE;
7093 break;
7095 default:
7096 gcc_unreachable ();
7099 if (DECL_ARTIFICIAL (decl))
7100 ; /* We can get compiler-generated decls, and should not complain
7101 about them. */
7102 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7104 error ("%qE not specified in enclosing OpenACC %qs construct",
7105 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7106 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7108 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7109 ; /* Handled above. */
7110 else
7111 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7113 return flags;
7116 /* Record the fact that DECL was used within the OMP context CTX.
7117 IN_CODE is true when real code uses DECL, and false when we should
7118 merely emit default(none) errors. Return true if DECL is going to
7119 be remapped and thus DECL shouldn't be gimplified into its
7120 DECL_VALUE_EXPR (if any). */
7122 static bool
7123 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7125 splay_tree_node n;
7126 unsigned flags = in_code ? GOVD_SEEN : 0;
7127 bool ret = false, shared;
7129 if (error_operand_p (decl))
7130 return false;
7132 if (ctx->region_type == ORT_NONE)
7133 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7135 if (is_global_var (decl))
7137 /* Threadprivate variables are predetermined. */
7138 if (DECL_THREAD_LOCAL_P (decl))
7139 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7141 if (DECL_HAS_VALUE_EXPR_P (decl))
7143 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7145 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7146 return omp_notice_threadprivate_variable (ctx, decl, value);
7149 if (gimplify_omp_ctxp->outer_context == NULL
7150 && VAR_P (decl)
7151 && oacc_get_fn_attrib (current_function_decl))
7153 location_t loc = DECL_SOURCE_LOCATION (decl);
7155 if (lookup_attribute ("omp declare target link",
7156 DECL_ATTRIBUTES (decl)))
7158 error_at (loc,
7159 "%qE with %<link%> clause used in %<routine%> function",
7160 DECL_NAME (decl));
7161 return false;
7163 else if (!lookup_attribute ("omp declare target",
7164 DECL_ATTRIBUTES (decl)))
7166 error_at (loc,
7167 "%qE requires a %<declare%> directive for use "
7168 "in a %<routine%> function", DECL_NAME (decl));
7169 return false;
7174 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7175 if ((ctx->region_type & ORT_TARGET) != 0)
7177 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7178 if (n == NULL)
7180 unsigned nflags = flags;
7181 if (ctx->target_map_pointers_as_0len_arrays
7182 || ctx->target_map_scalars_firstprivate)
7184 bool is_declare_target = false;
7185 bool is_scalar = false;
7186 if (is_global_var (decl)
7187 && varpool_node::get_create (decl)->offloadable)
7189 struct gimplify_omp_ctx *octx;
7190 for (octx = ctx->outer_context;
7191 octx; octx = octx->outer_context)
7193 n = splay_tree_lookup (octx->variables,
7194 (splay_tree_key)decl);
7195 if (n
7196 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7197 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7198 break;
7200 is_declare_target = octx == NULL;
7202 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7203 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7204 if (is_declare_target)
7206 else if (ctx->target_map_pointers_as_0len_arrays
7207 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7208 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7209 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7210 == POINTER_TYPE)))
7211 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7212 else if (is_scalar)
7213 nflags |= GOVD_FIRSTPRIVATE;
7216 struct gimplify_omp_ctx *octx = ctx->outer_context;
7217 if ((ctx->region_type & ORT_ACC) && octx)
7219 /* Look in outer OpenACC contexts, to see if there's a
7220 data attribute for this variable. */
7221 omp_notice_variable (octx, decl, in_code);
7223 for (; octx; octx = octx->outer_context)
7225 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7226 break;
7227 splay_tree_node n2
7228 = splay_tree_lookup (octx->variables,
7229 (splay_tree_key) decl);
7230 if (n2)
7232 if (octx->region_type == ORT_ACC_HOST_DATA)
7233 error ("variable %qE declared in enclosing "
7234 "%<host_data%> region", DECL_NAME (decl));
7235 nflags |= GOVD_MAP;
7236 if (octx->region_type == ORT_ACC_DATA
7237 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7238 nflags |= GOVD_MAP_0LEN_ARRAY;
7239 goto found_outer;
7245 tree type = TREE_TYPE (decl);
7247 if (nflags == flags
7248 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7249 && lang_hooks.decls.omp_privatize_by_reference (decl))
7250 type = TREE_TYPE (type);
7251 if (nflags == flags
7252 && !lang_hooks.types.omp_mappable_type (type))
7254 error ("%qD referenced in target region does not have "
7255 "a mappable type", decl);
7256 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7258 else if (nflags == flags)
7260 if ((ctx->region_type & ORT_ACC) != 0)
7261 nflags = oacc_default_clause (ctx, decl, flags);
7262 else
7263 nflags |= GOVD_MAP;
7266 found_outer:
7267 omp_add_variable (ctx, decl, nflags);
7269 else
7271 /* If nothing changed, there's nothing left to do. */
7272 if ((n->value & flags) == flags)
7273 return ret;
7274 flags |= n->value;
7275 n->value = flags;
7277 goto do_outer;
7280 if (n == NULL)
7282 if (ctx->region_type == ORT_WORKSHARE
7283 || ctx->region_type == ORT_SIMD
7284 || ctx->region_type == ORT_ACC
7285 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7286 goto do_outer;
7288 flags = omp_default_clause (ctx, decl, in_code, flags);
7290 if ((flags & GOVD_PRIVATE)
7291 && lang_hooks.decls.omp_private_outer_ref (decl))
7292 flags |= GOVD_PRIVATE_OUTER_REF;
7294 omp_add_variable (ctx, decl, flags);
7296 shared = (flags & GOVD_SHARED) != 0;
7297 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7298 goto do_outer;
7301 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7302 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7303 && DECL_SIZE (decl))
7305 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7307 splay_tree_node n2;
7308 tree t = DECL_VALUE_EXPR (decl);
7309 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7310 t = TREE_OPERAND (t, 0);
7311 gcc_assert (DECL_P (t));
7312 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7313 n2->value |= GOVD_SEEN;
7315 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7316 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7317 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7318 != INTEGER_CST))
7320 splay_tree_node n2;
7321 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7322 gcc_assert (DECL_P (t));
7323 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7324 if (n2)
7325 omp_notice_variable (ctx, t, true);
7329 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7330 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7332 /* If nothing changed, there's nothing left to do. */
7333 if ((n->value & flags) == flags)
7334 return ret;
7335 flags |= n->value;
7336 n->value = flags;
7338 do_outer:
7339 /* If the variable is private in the current context, then we don't
7340 need to propagate anything to an outer context. */
7341 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7342 return ret;
7343 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7344 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7345 return ret;
7346 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7347 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7348 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7349 return ret;
7350 if (ctx->outer_context
7351 && omp_notice_variable (ctx->outer_context, decl, in_code))
7352 return true;
7353 return ret;
7356 /* Verify that DECL is private within CTX. If there's specific information
7357 to the contrary in the innermost scope, generate an error. */
7359 static bool
7360 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7362 splay_tree_node n;
7364 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7365 if (n != NULL)
7367 if (n->value & GOVD_SHARED)
7369 if (ctx == gimplify_omp_ctxp)
7371 if (simd)
7372 error ("iteration variable %qE is predetermined linear",
7373 DECL_NAME (decl));
7374 else
7375 error ("iteration variable %qE should be private",
7376 DECL_NAME (decl));
7377 n->value = GOVD_PRIVATE;
7378 return true;
7380 else
7381 return false;
7383 else if ((n->value & GOVD_EXPLICIT) != 0
7384 && (ctx == gimplify_omp_ctxp
7385 || (ctx->region_type == ORT_COMBINED_PARALLEL
7386 && gimplify_omp_ctxp->outer_context == ctx)))
7388 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7389 error ("iteration variable %qE should not be firstprivate",
7390 DECL_NAME (decl));
7391 else if ((n->value & GOVD_REDUCTION) != 0)
7392 error ("iteration variable %qE should not be reduction",
7393 DECL_NAME (decl));
7394 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7395 error ("iteration variable %qE should not be linear",
7396 DECL_NAME (decl));
7397 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7398 error ("iteration variable %qE should not be lastprivate",
7399 DECL_NAME (decl));
7400 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7401 error ("iteration variable %qE should not be private",
7402 DECL_NAME (decl));
7403 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7404 error ("iteration variable %qE is predetermined linear",
7405 DECL_NAME (decl));
7407 return (ctx == gimplify_omp_ctxp
7408 || (ctx->region_type == ORT_COMBINED_PARALLEL
7409 && gimplify_omp_ctxp->outer_context == ctx));
7412 if (ctx->region_type != ORT_WORKSHARE
7413 && ctx->region_type != ORT_SIMD
7414 && ctx->region_type != ORT_ACC)
7415 return false;
7416 else if (ctx->outer_context)
7417 return omp_is_private (ctx->outer_context, decl, simd);
7418 return false;
7421 /* Return true if DECL is private within a parallel region
7422 that binds to the current construct's context or in parallel
7423 region's REDUCTION clause. */
7425 static bool
7426 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7428 splay_tree_node n;
7432 ctx = ctx->outer_context;
7433 if (ctx == NULL)
7435 if (is_global_var (decl))
7436 return false;
7438 /* References might be private, but might be shared too,
7439 when checking for copyprivate, assume they might be
7440 private, otherwise assume they might be shared. */
7441 if (copyprivate)
7442 return true;
7444 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7445 return false;
7447 /* Treat C++ privatized non-static data members outside
7448 of the privatization the same. */
7449 if (omp_member_access_dummy_var (decl))
7450 return false;
7452 return true;
7455 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7457 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7458 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7459 continue;
7461 if (n != NULL)
7463 if ((n->value & GOVD_LOCAL) != 0
7464 && omp_member_access_dummy_var (decl))
7465 return false;
7466 return (n->value & GOVD_SHARED) == 0;
7469 while (ctx->region_type == ORT_WORKSHARE
7470 || ctx->region_type == ORT_SIMD
7471 || ctx->region_type == ORT_ACC);
7472 return false;
7475 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7477 static tree
7478 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7480 tree t = *tp;
7482 /* If this node has been visited, unmark it and keep looking. */
7483 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7484 return t;
7486 if (IS_TYPE_OR_DECL_P (t))
7487 *walk_subtrees = 0;
7488 return NULL_TREE;
7491 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7492 and previous omp contexts. */
7494 static void
7495 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7496 enum omp_region_type region_type,
7497 enum tree_code code)
7499 struct gimplify_omp_ctx *ctx, *outer_ctx;
7500 tree c;
7501 hash_map<tree, tree> *struct_map_to_clause = NULL;
7502 tree *prev_list_p = NULL;
7504 ctx = new_omp_context (region_type);
7505 outer_ctx = ctx->outer_context;
7506 if (code == OMP_TARGET)
7508 if (!lang_GNU_Fortran ())
7509 ctx->target_map_pointers_as_0len_arrays = true;
7510 ctx->target_map_scalars_firstprivate = true;
7512 if (!lang_GNU_Fortran ())
7513 switch (code)
7515 case OMP_TARGET:
7516 case OMP_TARGET_DATA:
7517 case OMP_TARGET_ENTER_DATA:
7518 case OMP_TARGET_EXIT_DATA:
7519 case OACC_DECLARE:
7520 case OACC_HOST_DATA:
7521 ctx->target_firstprivatize_array_bases = true;
7522 default:
7523 break;
7526 while ((c = *list_p) != NULL)
7528 bool remove = false;
7529 bool notice_outer = true;
7530 const char *check_non_private = NULL;
7531 unsigned int flags;
7532 tree decl;
7534 switch (OMP_CLAUSE_CODE (c))
7536 case OMP_CLAUSE_PRIVATE:
7537 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7538 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7540 flags |= GOVD_PRIVATE_OUTER_REF;
7541 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7543 else
7544 notice_outer = false;
7545 goto do_add;
7546 case OMP_CLAUSE_SHARED:
7547 flags = GOVD_SHARED | GOVD_EXPLICIT;
7548 goto do_add;
7549 case OMP_CLAUSE_FIRSTPRIVATE:
7550 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7551 check_non_private = "firstprivate";
7552 goto do_add;
7553 case OMP_CLAUSE_LASTPRIVATE:
7554 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7555 check_non_private = "lastprivate";
7556 decl = OMP_CLAUSE_DECL (c);
7557 if (error_operand_p (decl))
7558 goto do_add;
7559 else if (outer_ctx
7560 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7561 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7562 && splay_tree_lookup (outer_ctx->variables,
7563 (splay_tree_key) decl) == NULL)
7565 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7566 if (outer_ctx->outer_context)
7567 omp_notice_variable (outer_ctx->outer_context, decl, true);
7569 else if (outer_ctx
7570 && (outer_ctx->region_type & ORT_TASK) != 0
7571 && outer_ctx->combined_loop
7572 && splay_tree_lookup (outer_ctx->variables,
7573 (splay_tree_key) decl) == NULL)
7575 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7576 if (outer_ctx->outer_context)
7577 omp_notice_variable (outer_ctx->outer_context, decl, true);
7579 else if (outer_ctx
7580 && (outer_ctx->region_type == ORT_WORKSHARE
7581 || outer_ctx->region_type == ORT_ACC)
7582 && outer_ctx->combined_loop
7583 && splay_tree_lookup (outer_ctx->variables,
7584 (splay_tree_key) decl) == NULL
7585 && !omp_check_private (outer_ctx, decl, false))
7587 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7588 if (outer_ctx->outer_context
7589 && (outer_ctx->outer_context->region_type
7590 == ORT_COMBINED_PARALLEL)
7591 && splay_tree_lookup (outer_ctx->outer_context->variables,
7592 (splay_tree_key) decl) == NULL)
7594 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7595 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7596 if (octx->outer_context)
7598 octx = octx->outer_context;
7599 if (octx->region_type == ORT_WORKSHARE
7600 && octx->combined_loop
7601 && splay_tree_lookup (octx->variables,
7602 (splay_tree_key) decl) == NULL
7603 && !omp_check_private (octx, decl, false))
7605 omp_add_variable (octx, decl,
7606 GOVD_LASTPRIVATE | GOVD_SEEN);
7607 octx = octx->outer_context;
7608 if (octx
7609 && octx->region_type == ORT_COMBINED_TEAMS
7610 && (splay_tree_lookup (octx->variables,
7611 (splay_tree_key) decl)
7612 == NULL))
7614 omp_add_variable (octx, decl,
7615 GOVD_SHARED | GOVD_SEEN);
7616 octx = octx->outer_context;
7619 if (octx)
7620 omp_notice_variable (octx, decl, true);
7623 else if (outer_ctx->outer_context)
7624 omp_notice_variable (outer_ctx->outer_context, decl, true);
7626 goto do_add;
7627 case OMP_CLAUSE_REDUCTION:
7628 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7629 /* OpenACC permits reductions on private variables. */
7630 if (!(region_type & ORT_ACC))
7631 check_non_private = "reduction";
7632 decl = OMP_CLAUSE_DECL (c);
7633 if (TREE_CODE (decl) == MEM_REF)
7635 tree type = TREE_TYPE (decl);
7636 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7637 NULL, is_gimple_val, fb_rvalue, false)
7638 == GS_ERROR)
7640 remove = true;
7641 break;
7643 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7644 if (DECL_P (v))
7646 omp_firstprivatize_variable (ctx, v);
7647 omp_notice_variable (ctx, v, true);
7649 decl = TREE_OPERAND (decl, 0);
7650 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7652 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7653 NULL, is_gimple_val, fb_rvalue, false)
7654 == GS_ERROR)
7656 remove = true;
7657 break;
7659 v = TREE_OPERAND (decl, 1);
7660 if (DECL_P (v))
7662 omp_firstprivatize_variable (ctx, v);
7663 omp_notice_variable (ctx, v, true);
7665 decl = TREE_OPERAND (decl, 0);
7667 if (TREE_CODE (decl) == ADDR_EXPR
7668 || TREE_CODE (decl) == INDIRECT_REF)
7669 decl = TREE_OPERAND (decl, 0);
7671 goto do_add_decl;
7672 case OMP_CLAUSE_LINEAR:
7673 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7674 is_gimple_val, fb_rvalue) == GS_ERROR)
7676 remove = true;
7677 break;
7679 else
7681 if (code == OMP_SIMD
7682 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7684 struct gimplify_omp_ctx *octx = outer_ctx;
7685 if (octx
7686 && octx->region_type == ORT_WORKSHARE
7687 && octx->combined_loop
7688 && !octx->distribute)
7690 if (octx->outer_context
7691 && (octx->outer_context->region_type
7692 == ORT_COMBINED_PARALLEL))
7693 octx = octx->outer_context->outer_context;
7694 else
7695 octx = octx->outer_context;
7697 if (octx
7698 && octx->region_type == ORT_WORKSHARE
7699 && octx->combined_loop
7700 && octx->distribute)
7702 error_at (OMP_CLAUSE_LOCATION (c),
7703 "%<linear%> clause for variable other than "
7704 "loop iterator specified on construct "
7705 "combined with %<distribute%>");
7706 remove = true;
7707 break;
7710 /* For combined #pragma omp parallel for simd, need to put
7711 lastprivate and perhaps firstprivate too on the
7712 parallel. Similarly for #pragma omp for simd. */
7713 struct gimplify_omp_ctx *octx = outer_ctx;
7714 decl = NULL_TREE;
7717 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7718 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7719 break;
7720 decl = OMP_CLAUSE_DECL (c);
7721 if (error_operand_p (decl))
7723 decl = NULL_TREE;
7724 break;
7726 flags = GOVD_SEEN;
7727 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7728 flags |= GOVD_FIRSTPRIVATE;
7729 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7730 flags |= GOVD_LASTPRIVATE;
7731 if (octx
7732 && octx->region_type == ORT_WORKSHARE
7733 && octx->combined_loop)
7735 if (octx->outer_context
7736 && (octx->outer_context->region_type
7737 == ORT_COMBINED_PARALLEL))
7738 octx = octx->outer_context;
7739 else if (omp_check_private (octx, decl, false))
7740 break;
7742 else if (octx
7743 && (octx->region_type & ORT_TASK) != 0
7744 && octx->combined_loop)
7746 else if (octx
7747 && octx->region_type == ORT_COMBINED_PARALLEL
7748 && ctx->region_type == ORT_WORKSHARE
7749 && octx == outer_ctx)
7750 flags = GOVD_SEEN | GOVD_SHARED;
7751 else if (octx
7752 && octx->region_type == ORT_COMBINED_TEAMS)
7753 flags = GOVD_SEEN | GOVD_SHARED;
7754 else if (octx
7755 && octx->region_type == ORT_COMBINED_TARGET)
7757 flags &= ~GOVD_LASTPRIVATE;
7758 if (flags == GOVD_SEEN)
7759 break;
7761 else
7762 break;
7763 splay_tree_node on
7764 = splay_tree_lookup (octx->variables,
7765 (splay_tree_key) decl);
7766 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7768 octx = NULL;
7769 break;
7771 omp_add_variable (octx, decl, flags);
7772 if (octx->outer_context == NULL)
7773 break;
7774 octx = octx->outer_context;
7776 while (1);
7777 if (octx
7778 && decl
7779 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7780 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7781 omp_notice_variable (octx, decl, true);
7783 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7784 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7785 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7787 notice_outer = false;
7788 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7790 goto do_add;
7792 case OMP_CLAUSE_MAP:
7793 decl = OMP_CLAUSE_DECL (c);
7794 if (error_operand_p (decl))
7795 remove = true;
7796 switch (code)
7798 case OMP_TARGET:
7799 break;
7800 case OACC_DATA:
7801 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7802 break;
7803 /* FALLTHRU */
7804 case OMP_TARGET_DATA:
7805 case OMP_TARGET_ENTER_DATA:
7806 case OMP_TARGET_EXIT_DATA:
7807 case OACC_ENTER_DATA:
7808 case OACC_EXIT_DATA:
7809 case OACC_HOST_DATA:
7810 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7811 || (OMP_CLAUSE_MAP_KIND (c)
7812 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7813 /* For target {,enter ,exit }data only the array slice is
7814 mapped, but not the pointer to it. */
7815 remove = true;
7816 break;
7817 default:
7818 break;
7820 if (remove)
7821 break;
7822 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7824 struct gimplify_omp_ctx *octx;
7825 for (octx = outer_ctx; octx; octx = octx->outer_context)
7827 if (octx->region_type != ORT_ACC_HOST_DATA)
7828 break;
7829 splay_tree_node n2
7830 = splay_tree_lookup (octx->variables,
7831 (splay_tree_key) decl);
7832 if (n2)
7833 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7834 "declared in enclosing %<host_data%> region",
7835 DECL_NAME (decl));
7838 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7839 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7840 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7841 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7842 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7844 remove = true;
7845 break;
7847 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7848 || (OMP_CLAUSE_MAP_KIND (c)
7849 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7850 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7852 OMP_CLAUSE_SIZE (c)
7853 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7854 false);
7855 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7856 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7858 if (!DECL_P (decl))
7860 tree d = decl, *pd;
7861 if (TREE_CODE (d) == ARRAY_REF)
7863 while (TREE_CODE (d) == ARRAY_REF)
7864 d = TREE_OPERAND (d, 0);
7865 if (TREE_CODE (d) == COMPONENT_REF
7866 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7867 decl = d;
7869 pd = &OMP_CLAUSE_DECL (c);
7870 if (d == decl
7871 && TREE_CODE (decl) == INDIRECT_REF
7872 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7873 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7874 == REFERENCE_TYPE))
7876 pd = &TREE_OPERAND (decl, 0);
7877 decl = TREE_OPERAND (decl, 0);
7879 if (TREE_CODE (decl) == COMPONENT_REF)
7881 while (TREE_CODE (decl) == COMPONENT_REF)
7882 decl = TREE_OPERAND (decl, 0);
7883 if (TREE_CODE (decl) == INDIRECT_REF
7884 && DECL_P (TREE_OPERAND (decl, 0))
7885 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7886 == REFERENCE_TYPE))
7887 decl = TREE_OPERAND (decl, 0);
7889 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7890 == GS_ERROR)
7892 remove = true;
7893 break;
7895 if (DECL_P (decl))
7897 if (error_operand_p (decl))
7899 remove = true;
7900 break;
7903 tree stype = TREE_TYPE (decl);
7904 if (TREE_CODE (stype) == REFERENCE_TYPE)
7905 stype = TREE_TYPE (stype);
7906 if (TYPE_SIZE_UNIT (stype) == NULL
7907 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7909 error_at (OMP_CLAUSE_LOCATION (c),
7910 "mapping field %qE of variable length "
7911 "structure", OMP_CLAUSE_DECL (c));
7912 remove = true;
7913 break;
7916 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7918 /* Error recovery. */
7919 if (prev_list_p == NULL)
7921 remove = true;
7922 break;
7924 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7926 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7927 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7929 remove = true;
7930 break;
7935 tree offset;
7936 poly_int64 bitsize, bitpos;
7937 machine_mode mode;
7938 int unsignedp, reversep, volatilep = 0;
7939 tree base = OMP_CLAUSE_DECL (c);
7940 while (TREE_CODE (base) == ARRAY_REF)
7941 base = TREE_OPERAND (base, 0);
7942 if (TREE_CODE (base) == INDIRECT_REF)
7943 base = TREE_OPERAND (base, 0);
7944 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7945 &mode, &unsignedp, &reversep,
7946 &volatilep);
7947 tree orig_base = base;
7948 if ((TREE_CODE (base) == INDIRECT_REF
7949 || (TREE_CODE (base) == MEM_REF
7950 && integer_zerop (TREE_OPERAND (base, 1))))
7951 && DECL_P (TREE_OPERAND (base, 0))
7952 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7953 == REFERENCE_TYPE))
7954 base = TREE_OPERAND (base, 0);
7955 gcc_assert (base == decl
7956 && (offset == NULL_TREE
7957 || poly_int_tree_p (offset)));
7959 splay_tree_node n
7960 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7961 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7962 == GOMP_MAP_ALWAYS_POINTER);
7963 if (n == NULL || (n->value & GOVD_MAP) == 0)
7965 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7966 OMP_CLAUSE_MAP);
7967 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
7968 if (orig_base != base)
7969 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
7970 else
7971 OMP_CLAUSE_DECL (l) = decl;
7972 OMP_CLAUSE_SIZE (l) = size_int (1);
7973 if (struct_map_to_clause == NULL)
7974 struct_map_to_clause = new hash_map<tree, tree>;
7975 struct_map_to_clause->put (decl, l);
7976 if (ptr)
7978 enum gomp_map_kind mkind
7979 = code == OMP_TARGET_EXIT_DATA
7980 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7981 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7982 OMP_CLAUSE_MAP);
7983 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7984 OMP_CLAUSE_DECL (c2)
7985 = unshare_expr (OMP_CLAUSE_DECL (c));
7986 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
7987 OMP_CLAUSE_SIZE (c2)
7988 = TYPE_SIZE_UNIT (ptr_type_node);
7989 OMP_CLAUSE_CHAIN (l) = c2;
7990 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7992 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7993 tree c3
7994 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7995 OMP_CLAUSE_MAP);
7996 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7997 OMP_CLAUSE_DECL (c3)
7998 = unshare_expr (OMP_CLAUSE_DECL (c4));
7999 OMP_CLAUSE_SIZE (c3)
8000 = TYPE_SIZE_UNIT (ptr_type_node);
8001 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8002 OMP_CLAUSE_CHAIN (c2) = c3;
8004 *prev_list_p = l;
8005 prev_list_p = NULL;
8007 else
8009 OMP_CLAUSE_CHAIN (l) = c;
8010 *list_p = l;
8011 list_p = &OMP_CLAUSE_CHAIN (l);
8013 if (orig_base != base && code == OMP_TARGET)
8015 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8016 OMP_CLAUSE_MAP);
8017 enum gomp_map_kind mkind
8018 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8019 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8020 OMP_CLAUSE_DECL (c2) = decl;
8021 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8022 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8023 OMP_CLAUSE_CHAIN (l) = c2;
8025 flags = GOVD_MAP | GOVD_EXPLICIT;
8026 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8027 flags |= GOVD_SEEN;
8028 goto do_add_decl;
8030 else
8032 tree *osc = struct_map_to_clause->get (decl);
8033 tree *sc = NULL, *scp = NULL;
8034 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8035 n->value |= GOVD_SEEN;
8036 poly_offset_int o1, o2;
8037 if (offset)
8038 o1 = wi::to_poly_offset (offset);
8039 else
8040 o1 = 0;
8041 if (maybe_ne (bitpos, 0))
8042 o1 += bits_to_bytes_round_down (bitpos);
8043 sc = &OMP_CLAUSE_CHAIN (*osc);
8044 if (*sc != c
8045 && (OMP_CLAUSE_MAP_KIND (*sc)
8046 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8047 sc = &OMP_CLAUSE_CHAIN (*sc);
8048 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8049 if (ptr && sc == prev_list_p)
8050 break;
8051 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8052 != COMPONENT_REF
8053 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8054 != INDIRECT_REF)
8055 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8056 != ARRAY_REF))
8057 break;
8058 else
8060 tree offset2;
8061 poly_int64 bitsize2, bitpos2;
8062 base = OMP_CLAUSE_DECL (*sc);
8063 if (TREE_CODE (base) == ARRAY_REF)
8065 while (TREE_CODE (base) == ARRAY_REF)
8066 base = TREE_OPERAND (base, 0);
8067 if (TREE_CODE (base) != COMPONENT_REF
8068 || (TREE_CODE (TREE_TYPE (base))
8069 != ARRAY_TYPE))
8070 break;
8072 else if (TREE_CODE (base) == INDIRECT_REF
8073 && (TREE_CODE (TREE_OPERAND (base, 0))
8074 == COMPONENT_REF)
8075 && (TREE_CODE (TREE_TYPE
8076 (TREE_OPERAND (base, 0)))
8077 == REFERENCE_TYPE))
8078 base = TREE_OPERAND (base, 0);
8079 base = get_inner_reference (base, &bitsize2,
8080 &bitpos2, &offset2,
8081 &mode, &unsignedp,
8082 &reversep, &volatilep);
8083 if ((TREE_CODE (base) == INDIRECT_REF
8084 || (TREE_CODE (base) == MEM_REF
8085 && integer_zerop (TREE_OPERAND (base,
8086 1))))
8087 && DECL_P (TREE_OPERAND (base, 0))
8088 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8089 0)))
8090 == REFERENCE_TYPE))
8091 base = TREE_OPERAND (base, 0);
8092 if (base != decl)
8093 break;
8094 if (scp)
8095 continue;
8096 gcc_assert (offset == NULL_TREE
8097 || poly_int_tree_p (offset));
8098 tree d1 = OMP_CLAUSE_DECL (*sc);
8099 tree d2 = OMP_CLAUSE_DECL (c);
8100 while (TREE_CODE (d1) == ARRAY_REF)
8101 d1 = TREE_OPERAND (d1, 0);
8102 while (TREE_CODE (d2) == ARRAY_REF)
8103 d2 = TREE_OPERAND (d2, 0);
8104 if (TREE_CODE (d1) == INDIRECT_REF)
8105 d1 = TREE_OPERAND (d1, 0);
8106 if (TREE_CODE (d2) == INDIRECT_REF)
8107 d2 = TREE_OPERAND (d2, 0);
8108 while (TREE_CODE (d1) == COMPONENT_REF)
8109 if (TREE_CODE (d2) == COMPONENT_REF
8110 && TREE_OPERAND (d1, 1)
8111 == TREE_OPERAND (d2, 1))
8113 d1 = TREE_OPERAND (d1, 0);
8114 d2 = TREE_OPERAND (d2, 0);
8116 else
8117 break;
8118 if (d1 == d2)
8120 error_at (OMP_CLAUSE_LOCATION (c),
8121 "%qE appears more than once in map "
8122 "clauses", OMP_CLAUSE_DECL (c));
8123 remove = true;
8124 break;
8126 if (offset2)
8127 o2 = wi::to_poly_offset (offset2);
8128 else
8129 o2 = 0;
8130 o2 += bits_to_bytes_round_down (bitpos2);
8131 if (maybe_lt (o1, o2)
8132 || (known_eq (o1, 2)
8133 && maybe_lt (bitpos, bitpos2)))
8135 if (ptr)
8136 scp = sc;
8137 else
8138 break;
8141 if (remove)
8142 break;
8143 OMP_CLAUSE_SIZE (*osc)
8144 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8145 size_one_node);
8146 if (ptr)
8148 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8149 OMP_CLAUSE_MAP);
8150 tree cl = NULL_TREE;
8151 enum gomp_map_kind mkind
8152 = code == OMP_TARGET_EXIT_DATA
8153 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8154 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8155 OMP_CLAUSE_DECL (c2)
8156 = unshare_expr (OMP_CLAUSE_DECL (c));
8157 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8158 OMP_CLAUSE_SIZE (c2)
8159 = TYPE_SIZE_UNIT (ptr_type_node);
8160 cl = scp ? *prev_list_p : c2;
8161 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8163 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8164 tree c3
8165 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8166 OMP_CLAUSE_MAP);
8167 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8168 OMP_CLAUSE_DECL (c3)
8169 = unshare_expr (OMP_CLAUSE_DECL (c4));
8170 OMP_CLAUSE_SIZE (c3)
8171 = TYPE_SIZE_UNIT (ptr_type_node);
8172 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8173 if (!scp)
8174 OMP_CLAUSE_CHAIN (c2) = c3;
8175 else
8176 cl = c3;
8178 if (scp)
8179 *scp = c2;
8180 if (sc == prev_list_p)
8182 *sc = cl;
8183 prev_list_p = NULL;
8185 else
8187 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8188 list_p = prev_list_p;
8189 prev_list_p = NULL;
8190 OMP_CLAUSE_CHAIN (c) = *sc;
8191 *sc = cl;
8192 continue;
8195 else if (*sc != c)
8197 *list_p = OMP_CLAUSE_CHAIN (c);
8198 OMP_CLAUSE_CHAIN (c) = *sc;
8199 *sc = c;
8200 continue;
8204 if (!remove
8205 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8206 && OMP_CLAUSE_CHAIN (c)
8207 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8208 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8209 == GOMP_MAP_ALWAYS_POINTER))
8210 prev_list_p = list_p;
8211 break;
8213 flags = GOVD_MAP | GOVD_EXPLICIT;
8214 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8215 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8216 flags |= GOVD_MAP_ALWAYS_TO;
8217 goto do_add;
8219 case OMP_CLAUSE_DEPEND:
8220 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8222 tree deps = OMP_CLAUSE_DECL (c);
8223 while (deps && TREE_CODE (deps) == TREE_LIST)
8225 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8226 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8227 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8228 pre_p, NULL, is_gimple_val, fb_rvalue);
8229 deps = TREE_CHAIN (deps);
8231 break;
8233 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8234 break;
8235 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8237 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8238 NULL, is_gimple_val, fb_rvalue);
8239 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8241 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8243 remove = true;
8244 break;
8246 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8247 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8248 is_gimple_val, fb_rvalue) == GS_ERROR)
8250 remove = true;
8251 break;
8253 break;
8255 case OMP_CLAUSE_TO:
8256 case OMP_CLAUSE_FROM:
8257 case OMP_CLAUSE__CACHE_:
8258 decl = OMP_CLAUSE_DECL (c);
8259 if (error_operand_p (decl))
8261 remove = true;
8262 break;
8264 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8265 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8266 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8267 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8268 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8270 remove = true;
8271 break;
8273 if (!DECL_P (decl))
8275 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8276 NULL, is_gimple_lvalue, fb_lvalue)
8277 == GS_ERROR)
8279 remove = true;
8280 break;
8282 break;
8284 goto do_notice;
8286 case OMP_CLAUSE_USE_DEVICE_PTR:
8287 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8288 goto do_add;
8289 case OMP_CLAUSE_IS_DEVICE_PTR:
8290 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8291 goto do_add;
8293 do_add:
8294 decl = OMP_CLAUSE_DECL (c);
8295 do_add_decl:
8296 if (error_operand_p (decl))
8298 remove = true;
8299 break;
8301 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8303 tree t = omp_member_access_dummy_var (decl);
8304 if (t)
8306 tree v = DECL_VALUE_EXPR (decl);
8307 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8308 if (outer_ctx)
8309 omp_notice_variable (outer_ctx, t, true);
8312 if (code == OACC_DATA
8313 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8314 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8315 flags |= GOVD_MAP_0LEN_ARRAY;
8316 omp_add_variable (ctx, decl, flags);
8317 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8318 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8320 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8321 GOVD_LOCAL | GOVD_SEEN);
8322 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8323 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8324 find_decl_expr,
8325 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8326 NULL) == NULL_TREE)
8327 omp_add_variable (ctx,
8328 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8329 GOVD_LOCAL | GOVD_SEEN);
8330 gimplify_omp_ctxp = ctx;
8331 push_gimplify_context ();
8333 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8334 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8336 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8337 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8338 pop_gimplify_context
8339 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8340 push_gimplify_context ();
8341 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8342 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8343 pop_gimplify_context
8344 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8345 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8346 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8348 gimplify_omp_ctxp = outer_ctx;
8350 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8351 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8353 gimplify_omp_ctxp = ctx;
8354 push_gimplify_context ();
8355 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8357 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8358 NULL, NULL);
8359 TREE_SIDE_EFFECTS (bind) = 1;
8360 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8361 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8363 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8364 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8365 pop_gimplify_context
8366 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8367 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8369 gimplify_omp_ctxp = outer_ctx;
8371 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8372 && OMP_CLAUSE_LINEAR_STMT (c))
8374 gimplify_omp_ctxp = ctx;
8375 push_gimplify_context ();
8376 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8378 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8379 NULL, NULL);
8380 TREE_SIDE_EFFECTS (bind) = 1;
8381 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8382 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8384 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8385 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8386 pop_gimplify_context
8387 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8388 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8390 gimplify_omp_ctxp = outer_ctx;
8392 if (notice_outer)
8393 goto do_notice;
8394 break;
8396 case OMP_CLAUSE_COPYIN:
8397 case OMP_CLAUSE_COPYPRIVATE:
8398 decl = OMP_CLAUSE_DECL (c);
8399 if (error_operand_p (decl))
8401 remove = true;
8402 break;
8404 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8405 && !remove
8406 && !omp_check_private (ctx, decl, true))
8408 remove = true;
8409 if (is_global_var (decl))
8411 if (DECL_THREAD_LOCAL_P (decl))
8412 remove = false;
8413 else if (DECL_HAS_VALUE_EXPR_P (decl))
8415 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8417 if (value
8418 && DECL_P (value)
8419 && DECL_THREAD_LOCAL_P (value))
8420 remove = false;
8423 if (remove)
8424 error_at (OMP_CLAUSE_LOCATION (c),
8425 "copyprivate variable %qE is not threadprivate"
8426 " or private in outer context", DECL_NAME (decl));
8428 do_notice:
8429 if (outer_ctx)
8430 omp_notice_variable (outer_ctx, decl, true);
8431 if (check_non_private
8432 && region_type == ORT_WORKSHARE
8433 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8434 || decl == OMP_CLAUSE_DECL (c)
8435 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8436 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8437 == ADDR_EXPR
8438 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8439 == POINTER_PLUS_EXPR
8440 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8441 (OMP_CLAUSE_DECL (c), 0), 0))
8442 == ADDR_EXPR)))))
8443 && omp_check_private (ctx, decl, false))
8445 error ("%s variable %qE is private in outer context",
8446 check_non_private, DECL_NAME (decl));
8447 remove = true;
8449 break;
8451 case OMP_CLAUSE_IF:
8452 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8453 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8455 const char *p[2];
8456 for (int i = 0; i < 2; i++)
8457 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8459 case OMP_PARALLEL: p[i] = "parallel"; break;
8460 case OMP_TASK: p[i] = "task"; break;
8461 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8462 case OMP_TARGET_DATA: p[i] = "target data"; break;
8463 case OMP_TARGET: p[i] = "target"; break;
8464 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8465 case OMP_TARGET_ENTER_DATA:
8466 p[i] = "target enter data"; break;
8467 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8468 default: gcc_unreachable ();
8470 error_at (OMP_CLAUSE_LOCATION (c),
8471 "expected %qs %<if%> clause modifier rather than %qs",
8472 p[0], p[1]);
8473 remove = true;
8475 /* Fall through. */
8477 case OMP_CLAUSE_FINAL:
8478 OMP_CLAUSE_OPERAND (c, 0)
8479 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8480 /* Fall through. */
8482 case OMP_CLAUSE_SCHEDULE:
8483 case OMP_CLAUSE_NUM_THREADS:
8484 case OMP_CLAUSE_NUM_TEAMS:
8485 case OMP_CLAUSE_THREAD_LIMIT:
8486 case OMP_CLAUSE_DIST_SCHEDULE:
8487 case OMP_CLAUSE_DEVICE:
8488 case OMP_CLAUSE_PRIORITY:
8489 case OMP_CLAUSE_GRAINSIZE:
8490 case OMP_CLAUSE_NUM_TASKS:
8491 case OMP_CLAUSE_HINT:
8492 case OMP_CLAUSE_ASYNC:
8493 case OMP_CLAUSE_WAIT:
8494 case OMP_CLAUSE_NUM_GANGS:
8495 case OMP_CLAUSE_NUM_WORKERS:
8496 case OMP_CLAUSE_VECTOR_LENGTH:
8497 case OMP_CLAUSE_WORKER:
8498 case OMP_CLAUSE_VECTOR:
8499 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8500 is_gimple_val, fb_rvalue) == GS_ERROR)
8501 remove = true;
8502 break;
8504 case OMP_CLAUSE_GANG:
8505 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8506 is_gimple_val, fb_rvalue) == GS_ERROR)
8507 remove = true;
8508 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8509 is_gimple_val, fb_rvalue) == GS_ERROR)
8510 remove = true;
8511 break;
8513 case OMP_CLAUSE_NOWAIT:
8514 case OMP_CLAUSE_ORDERED:
8515 case OMP_CLAUSE_UNTIED:
8516 case OMP_CLAUSE_COLLAPSE:
8517 case OMP_CLAUSE_TILE:
8518 case OMP_CLAUSE_AUTO:
8519 case OMP_CLAUSE_SEQ:
8520 case OMP_CLAUSE_INDEPENDENT:
8521 case OMP_CLAUSE_MERGEABLE:
8522 case OMP_CLAUSE_PROC_BIND:
8523 case OMP_CLAUSE_SAFELEN:
8524 case OMP_CLAUSE_SIMDLEN:
8525 case OMP_CLAUSE_NOGROUP:
8526 case OMP_CLAUSE_THREADS:
8527 case OMP_CLAUSE_SIMD:
8528 case OMP_CLAUSE_IF_PRESENT:
8529 case OMP_CLAUSE_FINALIZE:
8530 break;
8532 case OMP_CLAUSE_DEFAULTMAP:
8533 ctx->target_map_scalars_firstprivate = false;
8534 break;
8536 case OMP_CLAUSE_ALIGNED:
8537 decl = OMP_CLAUSE_DECL (c);
8538 if (error_operand_p (decl))
8540 remove = true;
8541 break;
8543 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8544 is_gimple_val, fb_rvalue) == GS_ERROR)
8546 remove = true;
8547 break;
8549 if (!is_global_var (decl)
8550 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8551 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8552 break;
8554 case OMP_CLAUSE_DEFAULT:
8555 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8556 break;
8558 default:
8559 gcc_unreachable ();
8562 if (code == OACC_DATA
8563 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8564 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8565 remove = true;
8566 if (remove)
8567 *list_p = OMP_CLAUSE_CHAIN (c);
8568 else
8569 list_p = &OMP_CLAUSE_CHAIN (c);
8572 gimplify_omp_ctxp = ctx;
8573 if (struct_map_to_clause)
8574 delete struct_map_to_clause;
8577 /* Return true if DECL is a candidate for shared to firstprivate
8578 optimization. We only consider non-addressable scalars, not
8579 too big, and not references. */
8581 static bool
8582 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8584 if (TREE_ADDRESSABLE (decl))
8585 return false;
8586 tree type = TREE_TYPE (decl);
8587 if (!is_gimple_reg_type (type)
8588 || TREE_CODE (type) == REFERENCE_TYPE
8589 || TREE_ADDRESSABLE (type))
8590 return false;
8591 /* Don't optimize too large decls, as each thread/task will have
8592 its own. */
8593 HOST_WIDE_INT len = int_size_in_bytes (type);
8594 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8595 return false;
8596 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8597 return false;
8598 return true;
8601 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8602 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8603 GOVD_WRITTEN in outer contexts. */
8605 static void
8606 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8608 for (; ctx; ctx = ctx->outer_context)
8610 splay_tree_node n = splay_tree_lookup (ctx->variables,
8611 (splay_tree_key) decl);
8612 if (n == NULL)
8613 continue;
8614 else if (n->value & GOVD_SHARED)
8616 n->value |= GOVD_WRITTEN;
8617 return;
8619 else if (n->value & GOVD_DATA_SHARE_CLASS)
8620 return;
8624 /* Helper callback for walk_gimple_seq to discover possible stores
8625 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8626 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8627 for those. */
8629 static tree
8630 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8632 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8634 *walk_subtrees = 0;
8635 if (!wi->is_lhs)
8636 return NULL_TREE;
8638 tree op = *tp;
8641 if (handled_component_p (op))
8642 op = TREE_OPERAND (op, 0);
8643 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8644 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8645 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8646 else
8647 break;
8649 while (1);
8650 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8651 return NULL_TREE;
8653 omp_mark_stores (gimplify_omp_ctxp, op);
8654 return NULL_TREE;
8657 /* Helper callback for walk_gimple_seq to discover possible stores
8658 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8659 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8660 for those. */
8662 static tree
8663 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8664 bool *handled_ops_p,
8665 struct walk_stmt_info *wi)
8667 gimple *stmt = gsi_stmt (*gsi_p);
8668 switch (gimple_code (stmt))
8670 /* Don't recurse on OpenMP constructs for which
8671 gimplify_adjust_omp_clauses already handled the bodies,
8672 except handle gimple_omp_for_pre_body. */
8673 case GIMPLE_OMP_FOR:
8674 *handled_ops_p = true;
8675 if (gimple_omp_for_pre_body (stmt))
8676 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8677 omp_find_stores_stmt, omp_find_stores_op, wi);
8678 break;
8679 case GIMPLE_OMP_PARALLEL:
8680 case GIMPLE_OMP_TASK:
8681 case GIMPLE_OMP_SECTIONS:
8682 case GIMPLE_OMP_SINGLE:
8683 case GIMPLE_OMP_TARGET:
8684 case GIMPLE_OMP_TEAMS:
8685 case GIMPLE_OMP_CRITICAL:
8686 *handled_ops_p = true;
8687 break;
8688 default:
8689 break;
8691 return NULL_TREE;
8694 struct gimplify_adjust_omp_clauses_data
8696 tree *list_p;
8697 gimple_seq *pre_p;
8700 /* For all variables that were not actually used within the context,
8701 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8703 static int
8704 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8706 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8707 gimple_seq *pre_p
8708 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8709 tree decl = (tree) n->key;
8710 unsigned flags = n->value;
8711 enum omp_clause_code code;
8712 tree clause;
8713 bool private_debug;
8715 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8716 return 0;
8717 if ((flags & GOVD_SEEN) == 0)
8718 return 0;
8719 if (flags & GOVD_DEBUG_PRIVATE)
8721 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8722 private_debug = true;
8724 else if (flags & GOVD_MAP)
8725 private_debug = false;
8726 else
8727 private_debug
8728 = lang_hooks.decls.omp_private_debug_clause (decl,
8729 !!(flags & GOVD_SHARED));
8730 if (private_debug)
8731 code = OMP_CLAUSE_PRIVATE;
8732 else if (flags & GOVD_MAP)
8734 code = OMP_CLAUSE_MAP;
8735 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8736 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8738 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8739 return 0;
8742 else if (flags & GOVD_SHARED)
8744 if (is_global_var (decl))
8746 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8747 while (ctx != NULL)
8749 splay_tree_node on
8750 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8751 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8752 | GOVD_PRIVATE | GOVD_REDUCTION
8753 | GOVD_LINEAR | GOVD_MAP)) != 0)
8754 break;
8755 ctx = ctx->outer_context;
8757 if (ctx == NULL)
8758 return 0;
8760 code = OMP_CLAUSE_SHARED;
8762 else if (flags & GOVD_PRIVATE)
8763 code = OMP_CLAUSE_PRIVATE;
8764 else if (flags & GOVD_FIRSTPRIVATE)
8766 code = OMP_CLAUSE_FIRSTPRIVATE;
8767 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8768 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8769 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8771 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8772 "%<target%> construct", decl);
8773 return 0;
8776 else if (flags & GOVD_LASTPRIVATE)
8777 code = OMP_CLAUSE_LASTPRIVATE;
8778 else if (flags & GOVD_ALIGNED)
8779 return 0;
8780 else
8781 gcc_unreachable ();
8783 if (((flags & GOVD_LASTPRIVATE)
8784 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8785 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8786 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8788 tree chain = *list_p;
8789 clause = build_omp_clause (input_location, code);
8790 OMP_CLAUSE_DECL (clause) = decl;
8791 OMP_CLAUSE_CHAIN (clause) = chain;
8792 if (private_debug)
8793 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8794 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8795 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8796 else if (code == OMP_CLAUSE_SHARED
8797 && (flags & GOVD_WRITTEN) == 0
8798 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8799 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8800 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8801 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8802 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8804 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8805 OMP_CLAUSE_DECL (nc) = decl;
8806 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8807 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8808 OMP_CLAUSE_DECL (clause)
8809 = build_simple_mem_ref_loc (input_location, decl);
8810 OMP_CLAUSE_DECL (clause)
8811 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8812 build_int_cst (build_pointer_type (char_type_node), 0));
8813 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8814 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8815 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8816 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8817 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8818 OMP_CLAUSE_CHAIN (nc) = chain;
8819 OMP_CLAUSE_CHAIN (clause) = nc;
8820 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8821 gimplify_omp_ctxp = ctx->outer_context;
8822 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8823 pre_p, NULL, is_gimple_val, fb_rvalue);
8824 gimplify_omp_ctxp = ctx;
8826 else if (code == OMP_CLAUSE_MAP)
8828 int kind;
8829 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8830 switch (flags & (GOVD_MAP_TO_ONLY
8831 | GOVD_MAP_FORCE
8832 | GOVD_MAP_FORCE_PRESENT))
8834 case 0:
8835 kind = GOMP_MAP_TOFROM;
8836 break;
8837 case GOVD_MAP_FORCE:
8838 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8839 break;
8840 case GOVD_MAP_TO_ONLY:
8841 kind = GOMP_MAP_TO;
8842 break;
8843 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8844 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8845 break;
8846 case GOVD_MAP_FORCE_PRESENT:
8847 kind = GOMP_MAP_FORCE_PRESENT;
8848 break;
8849 default:
8850 gcc_unreachable ();
8852 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8853 if (DECL_SIZE (decl)
8854 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8856 tree decl2 = DECL_VALUE_EXPR (decl);
8857 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8858 decl2 = TREE_OPERAND (decl2, 0);
8859 gcc_assert (DECL_P (decl2));
8860 tree mem = build_simple_mem_ref (decl2);
8861 OMP_CLAUSE_DECL (clause) = mem;
8862 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8863 if (gimplify_omp_ctxp->outer_context)
8865 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8866 omp_notice_variable (ctx, decl2, true);
8867 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8869 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8870 OMP_CLAUSE_MAP);
8871 OMP_CLAUSE_DECL (nc) = decl;
8872 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8873 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8874 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8875 else
8876 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8877 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8878 OMP_CLAUSE_CHAIN (clause) = nc;
8880 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8881 && lang_hooks.decls.omp_privatize_by_reference (decl))
8883 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8884 OMP_CLAUSE_SIZE (clause)
8885 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8886 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8887 gimplify_omp_ctxp = ctx->outer_context;
8888 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8889 pre_p, NULL, is_gimple_val, fb_rvalue);
8890 gimplify_omp_ctxp = ctx;
8891 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8892 OMP_CLAUSE_MAP);
8893 OMP_CLAUSE_DECL (nc) = decl;
8894 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8895 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8896 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8897 OMP_CLAUSE_CHAIN (clause) = nc;
8899 else
8900 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8902 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8904 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8905 OMP_CLAUSE_DECL (nc) = decl;
8906 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8907 OMP_CLAUSE_CHAIN (nc) = chain;
8908 OMP_CLAUSE_CHAIN (clause) = nc;
8909 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8910 gimplify_omp_ctxp = ctx->outer_context;
8911 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8912 gimplify_omp_ctxp = ctx;
8914 *list_p = clause;
8915 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8916 gimplify_omp_ctxp = ctx->outer_context;
8917 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8918 if (gimplify_omp_ctxp)
8919 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8920 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8921 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8922 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8923 true);
8924 gimplify_omp_ctxp = ctx;
8925 return 0;
8928 static void
8929 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8930 enum tree_code code)
8932 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8933 tree c, decl;
8935 if (body)
8937 struct gimplify_omp_ctx *octx;
8938 for (octx = ctx; octx; octx = octx->outer_context)
8939 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8940 break;
8941 if (octx)
8943 struct walk_stmt_info wi;
8944 memset (&wi, 0, sizeof (wi));
8945 walk_gimple_seq (body, omp_find_stores_stmt,
8946 omp_find_stores_op, &wi);
8949 while ((c = *list_p) != NULL)
8951 splay_tree_node n;
8952 bool remove = false;
8954 switch (OMP_CLAUSE_CODE (c))
8956 case OMP_CLAUSE_FIRSTPRIVATE:
8957 if ((ctx->region_type & ORT_TARGET)
8958 && (ctx->region_type & ORT_ACC) == 0
8959 && TYPE_ATOMIC (strip_array_types
8960 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8962 error_at (OMP_CLAUSE_LOCATION (c),
8963 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8964 "%<target%> construct", OMP_CLAUSE_DECL (c));
8965 remove = true;
8966 break;
8968 /* FALLTHRU */
8969 case OMP_CLAUSE_PRIVATE:
8970 case OMP_CLAUSE_SHARED:
8971 case OMP_CLAUSE_LINEAR:
8972 decl = OMP_CLAUSE_DECL (c);
8973 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8974 remove = !(n->value & GOVD_SEEN);
8975 if (! remove)
8977 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
8978 if ((n->value & GOVD_DEBUG_PRIVATE)
8979 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
8981 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
8982 || ((n->value & GOVD_DATA_SHARE_CLASS)
8983 == GOVD_SHARED));
8984 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
8985 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
8987 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8988 && (n->value & GOVD_WRITTEN) == 0
8989 && DECL_P (decl)
8990 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8991 OMP_CLAUSE_SHARED_READONLY (c) = 1;
8992 else if (DECL_P (decl)
8993 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8994 && (n->value & GOVD_WRITTEN) != 0)
8995 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8996 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8997 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8998 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9000 break;
9002 case OMP_CLAUSE_LASTPRIVATE:
9003 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9004 accurately reflect the presence of a FIRSTPRIVATE clause. */
9005 decl = OMP_CLAUSE_DECL (c);
9006 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9007 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9008 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9009 if (code == OMP_DISTRIBUTE
9010 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9012 remove = true;
9013 error_at (OMP_CLAUSE_LOCATION (c),
9014 "same variable used in %<firstprivate%> and "
9015 "%<lastprivate%> clauses on %<distribute%> "
9016 "construct");
9018 if (!remove
9019 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9020 && DECL_P (decl)
9021 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9022 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9023 break;
9025 case OMP_CLAUSE_ALIGNED:
9026 decl = OMP_CLAUSE_DECL (c);
9027 if (!is_global_var (decl))
9029 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9030 remove = n == NULL || !(n->value & GOVD_SEEN);
9031 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9033 struct gimplify_omp_ctx *octx;
9034 if (n != NULL
9035 && (n->value & (GOVD_DATA_SHARE_CLASS
9036 & ~GOVD_FIRSTPRIVATE)))
9037 remove = true;
9038 else
9039 for (octx = ctx->outer_context; octx;
9040 octx = octx->outer_context)
9042 n = splay_tree_lookup (octx->variables,
9043 (splay_tree_key) decl);
9044 if (n == NULL)
9045 continue;
9046 if (n->value & GOVD_LOCAL)
9047 break;
9048 /* We have to avoid assigning a shared variable
9049 to itself when trying to add
9050 __builtin_assume_aligned. */
9051 if (n->value & GOVD_SHARED)
9053 remove = true;
9054 break;
9059 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9061 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9062 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9063 remove = true;
9065 break;
9067 case OMP_CLAUSE_MAP:
9068 if (code == OMP_TARGET_EXIT_DATA
9069 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9071 remove = true;
9072 break;
9074 decl = OMP_CLAUSE_DECL (c);
9075 /* Data clauses associated with acc parallel reductions must be
9076 compatible with present_or_copy. Warn and adjust the clause
9077 if that is not the case. */
9078 if (ctx->region_type == ORT_ACC_PARALLEL)
9080 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9081 n = NULL;
9083 if (DECL_P (t))
9084 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9086 if (n && (n->value & GOVD_REDUCTION))
9088 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9090 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9091 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9092 && kind != GOMP_MAP_FORCE_PRESENT
9093 && kind != GOMP_MAP_POINTER)
9095 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9096 "incompatible data clause with reduction "
9097 "on %qE; promoting to present_or_copy",
9098 DECL_NAME (t));
9099 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9103 if (!DECL_P (decl))
9105 if ((ctx->region_type & ORT_TARGET) != 0
9106 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9108 if (TREE_CODE (decl) == INDIRECT_REF
9109 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9110 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9111 == REFERENCE_TYPE))
9112 decl = TREE_OPERAND (decl, 0);
9113 if (TREE_CODE (decl) == COMPONENT_REF)
9115 while (TREE_CODE (decl) == COMPONENT_REF)
9116 decl = TREE_OPERAND (decl, 0);
9117 if (DECL_P (decl))
9119 n = splay_tree_lookup (ctx->variables,
9120 (splay_tree_key) decl);
9121 if (!(n->value & GOVD_SEEN))
9122 remove = true;
9126 break;
9128 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9129 if ((ctx->region_type & ORT_TARGET) != 0
9130 && !(n->value & GOVD_SEEN)
9131 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9132 && (!is_global_var (decl)
9133 || !lookup_attribute ("omp declare target link",
9134 DECL_ATTRIBUTES (decl))))
9136 remove = true;
9137 /* For struct element mapping, if struct is never referenced
9138 in target block and none of the mapping has always modifier,
9139 remove all the struct element mappings, which immediately
9140 follow the GOMP_MAP_STRUCT map clause. */
9141 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9143 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9144 while (cnt--)
9145 OMP_CLAUSE_CHAIN (c)
9146 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9149 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9150 && code == OMP_TARGET_EXIT_DATA)
9151 remove = true;
9152 else if (DECL_SIZE (decl)
9153 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9154 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9155 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9156 && (OMP_CLAUSE_MAP_KIND (c)
9157 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9159 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9160 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9161 INTEGER_CST. */
9162 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9164 tree decl2 = DECL_VALUE_EXPR (decl);
9165 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9166 decl2 = TREE_OPERAND (decl2, 0);
9167 gcc_assert (DECL_P (decl2));
9168 tree mem = build_simple_mem_ref (decl2);
9169 OMP_CLAUSE_DECL (c) = mem;
9170 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9171 if (ctx->outer_context)
9173 omp_notice_variable (ctx->outer_context, decl2, true);
9174 omp_notice_variable (ctx->outer_context,
9175 OMP_CLAUSE_SIZE (c), true);
9177 if (((ctx->region_type & ORT_TARGET) != 0
9178 || !ctx->target_firstprivatize_array_bases)
9179 && ((n->value & GOVD_SEEN) == 0
9180 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9182 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9183 OMP_CLAUSE_MAP);
9184 OMP_CLAUSE_DECL (nc) = decl;
9185 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9186 if (ctx->target_firstprivatize_array_bases)
9187 OMP_CLAUSE_SET_MAP_KIND (nc,
9188 GOMP_MAP_FIRSTPRIVATE_POINTER);
9189 else
9190 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9191 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9192 OMP_CLAUSE_CHAIN (c) = nc;
9193 c = nc;
9196 else
9198 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9199 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9200 gcc_assert ((n->value & GOVD_SEEN) == 0
9201 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9202 == 0));
9204 break;
9206 case OMP_CLAUSE_TO:
9207 case OMP_CLAUSE_FROM:
9208 case OMP_CLAUSE__CACHE_:
9209 decl = OMP_CLAUSE_DECL (c);
9210 if (!DECL_P (decl))
9211 break;
9212 if (DECL_SIZE (decl)
9213 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9215 tree decl2 = DECL_VALUE_EXPR (decl);
9216 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9217 decl2 = TREE_OPERAND (decl2, 0);
9218 gcc_assert (DECL_P (decl2));
9219 tree mem = build_simple_mem_ref (decl2);
9220 OMP_CLAUSE_DECL (c) = mem;
9221 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9222 if (ctx->outer_context)
9224 omp_notice_variable (ctx->outer_context, decl2, true);
9225 omp_notice_variable (ctx->outer_context,
9226 OMP_CLAUSE_SIZE (c), true);
9229 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9230 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9231 break;
9233 case OMP_CLAUSE_REDUCTION:
9234 decl = OMP_CLAUSE_DECL (c);
9235 /* OpenACC reductions need a present_or_copy data clause.
9236 Add one if necessary. Emit error when the reduction is private. */
9237 if (ctx->region_type == ORT_ACC_PARALLEL)
9239 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9240 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9242 remove = true;
9243 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9244 "reduction on %qE", DECL_NAME (decl));
9246 else if ((n->value & GOVD_MAP) == 0)
9248 tree next = OMP_CLAUSE_CHAIN (c);
9249 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9250 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9251 OMP_CLAUSE_DECL (nc) = decl;
9252 OMP_CLAUSE_CHAIN (c) = nc;
9253 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9254 while (1)
9256 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9257 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9258 break;
9259 nc = OMP_CLAUSE_CHAIN (nc);
9261 OMP_CLAUSE_CHAIN (nc) = next;
9262 n->value |= GOVD_MAP;
9265 if (DECL_P (decl)
9266 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9267 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9268 break;
9269 case OMP_CLAUSE_COPYIN:
9270 case OMP_CLAUSE_COPYPRIVATE:
9271 case OMP_CLAUSE_IF:
9272 case OMP_CLAUSE_NUM_THREADS:
9273 case OMP_CLAUSE_NUM_TEAMS:
9274 case OMP_CLAUSE_THREAD_LIMIT:
9275 case OMP_CLAUSE_DIST_SCHEDULE:
9276 case OMP_CLAUSE_DEVICE:
9277 case OMP_CLAUSE_SCHEDULE:
9278 case OMP_CLAUSE_NOWAIT:
9279 case OMP_CLAUSE_ORDERED:
9280 case OMP_CLAUSE_DEFAULT:
9281 case OMP_CLAUSE_UNTIED:
9282 case OMP_CLAUSE_COLLAPSE:
9283 case OMP_CLAUSE_FINAL:
9284 case OMP_CLAUSE_MERGEABLE:
9285 case OMP_CLAUSE_PROC_BIND:
9286 case OMP_CLAUSE_SAFELEN:
9287 case OMP_CLAUSE_SIMDLEN:
9288 case OMP_CLAUSE_DEPEND:
9289 case OMP_CLAUSE_PRIORITY:
9290 case OMP_CLAUSE_GRAINSIZE:
9291 case OMP_CLAUSE_NUM_TASKS:
9292 case OMP_CLAUSE_NOGROUP:
9293 case OMP_CLAUSE_THREADS:
9294 case OMP_CLAUSE_SIMD:
9295 case OMP_CLAUSE_HINT:
9296 case OMP_CLAUSE_DEFAULTMAP:
9297 case OMP_CLAUSE_USE_DEVICE_PTR:
9298 case OMP_CLAUSE_IS_DEVICE_PTR:
9299 case OMP_CLAUSE_ASYNC:
9300 case OMP_CLAUSE_WAIT:
9301 case OMP_CLAUSE_INDEPENDENT:
9302 case OMP_CLAUSE_NUM_GANGS:
9303 case OMP_CLAUSE_NUM_WORKERS:
9304 case OMP_CLAUSE_VECTOR_LENGTH:
9305 case OMP_CLAUSE_GANG:
9306 case OMP_CLAUSE_WORKER:
9307 case OMP_CLAUSE_VECTOR:
9308 case OMP_CLAUSE_AUTO:
9309 case OMP_CLAUSE_SEQ:
9310 case OMP_CLAUSE_TILE:
9311 case OMP_CLAUSE_IF_PRESENT:
9312 case OMP_CLAUSE_FINALIZE:
9313 break;
9315 default:
9316 gcc_unreachable ();
9319 if (remove)
9320 *list_p = OMP_CLAUSE_CHAIN (c);
9321 else
9322 list_p = &OMP_CLAUSE_CHAIN (c);
9325 /* Add in any implicit data sharing. */
9326 struct gimplify_adjust_omp_clauses_data data;
9327 data.list_p = list_p;
9328 data.pre_p = pre_p;
9329 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9331 gimplify_omp_ctxp = ctx->outer_context;
9332 delete_omp_context (ctx);
9335 /* Gimplify OACC_CACHE. */
9337 static void
9338 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9340 tree expr = *expr_p;
9342 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9343 OACC_CACHE);
9344 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9345 OACC_CACHE);
9347 /* TODO: Do something sensible with this information. */
9349 *expr_p = NULL_TREE;
9352 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9353 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9354 kind. The entry kind will replace the one in CLAUSE, while the exit
9355 kind will be used in a new omp_clause and returned to the caller. */
9357 static tree
9358 gimplify_oacc_declare_1 (tree clause)
9360 HOST_WIDE_INT kind, new_op;
9361 bool ret = false;
9362 tree c = NULL;
9364 kind = OMP_CLAUSE_MAP_KIND (clause);
9366 switch (kind)
9368 case GOMP_MAP_ALLOC:
9369 new_op = GOMP_MAP_RELEASE;
9370 ret = true;
9371 break;
9373 case GOMP_MAP_FROM:
9374 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9375 new_op = GOMP_MAP_FROM;
9376 ret = true;
9377 break;
9379 case GOMP_MAP_TOFROM:
9380 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9381 new_op = GOMP_MAP_FROM;
9382 ret = true;
9383 break;
9385 case GOMP_MAP_DEVICE_RESIDENT:
9386 case GOMP_MAP_FORCE_DEVICEPTR:
9387 case GOMP_MAP_FORCE_PRESENT:
9388 case GOMP_MAP_LINK:
9389 case GOMP_MAP_POINTER:
9390 case GOMP_MAP_TO:
9391 break;
9393 default:
9394 gcc_unreachable ();
9395 break;
9398 if (ret)
9400 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9401 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9402 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9405 return c;
9408 /* Gimplify OACC_DECLARE. */
9410 static void
9411 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9413 tree expr = *expr_p;
9414 gomp_target *stmt;
9415 tree clauses, t, decl;
9417 clauses = OACC_DECLARE_CLAUSES (expr);
9419 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9420 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9422 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9424 decl = OMP_CLAUSE_DECL (t);
9426 if (TREE_CODE (decl) == MEM_REF)
9427 decl = TREE_OPERAND (decl, 0);
9429 if (VAR_P (decl) && !is_oacc_declared (decl))
9431 tree attr = get_identifier ("oacc declare target");
9432 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9433 DECL_ATTRIBUTES (decl));
9436 if (VAR_P (decl)
9437 && !is_global_var (decl)
9438 && DECL_CONTEXT (decl) == current_function_decl)
9440 tree c = gimplify_oacc_declare_1 (t);
9441 if (c)
9443 if (oacc_declare_returns == NULL)
9444 oacc_declare_returns = new hash_map<tree, tree>;
9446 oacc_declare_returns->put (decl, c);
9450 if (gimplify_omp_ctxp)
9451 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9454 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9455 clauses);
9457 gimplify_seq_add_stmt (pre_p, stmt);
9459 *expr_p = NULL_TREE;
9462 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9463 gimplification of the body, as well as scanning the body for used
9464 variables. We need to do this scan now, because variable-sized
9465 decls will be decomposed during gimplification. */
9467 static void
9468 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9470 tree expr = *expr_p;
9471 gimple *g;
9472 gimple_seq body = NULL;
9474 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9475 OMP_PARALLEL_COMBINED (expr)
9476 ? ORT_COMBINED_PARALLEL
9477 : ORT_PARALLEL, OMP_PARALLEL);
9479 push_gimplify_context ();
9481 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9482 if (gimple_code (g) == GIMPLE_BIND)
9483 pop_gimplify_context (g);
9484 else
9485 pop_gimplify_context (NULL);
9487 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9488 OMP_PARALLEL);
9490 g = gimple_build_omp_parallel (body,
9491 OMP_PARALLEL_CLAUSES (expr),
9492 NULL_TREE, NULL_TREE);
9493 if (OMP_PARALLEL_COMBINED (expr))
9494 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9495 gimplify_seq_add_stmt (pre_p, g);
9496 *expr_p = NULL_TREE;
9499 /* Gimplify the contents of an OMP_TASK statement. This involves
9500 gimplification of the body, as well as scanning the body for used
9501 variables. We need to do this scan now, because variable-sized
9502 decls will be decomposed during gimplification. */
9504 static void
9505 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9507 tree expr = *expr_p;
9508 gimple *g;
9509 gimple_seq body = NULL;
9511 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9512 omp_find_clause (OMP_TASK_CLAUSES (expr),
9513 OMP_CLAUSE_UNTIED)
9514 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9516 push_gimplify_context ();
9518 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9519 if (gimple_code (g) == GIMPLE_BIND)
9520 pop_gimplify_context (g);
9521 else
9522 pop_gimplify_context (NULL);
9524 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9525 OMP_TASK);
9527 g = gimple_build_omp_task (body,
9528 OMP_TASK_CLAUSES (expr),
9529 NULL_TREE, NULL_TREE,
9530 NULL_TREE, NULL_TREE, NULL_TREE);
9531 gimplify_seq_add_stmt (pre_p, g);
9532 *expr_p = NULL_TREE;
9535 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9536 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
9537 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
9538 is address of OMP_PARALLEL in between if any, pdata[2] is address of
9539 OMP_FOR in between if any and pdata[3] is address of the inner
9540 OMP_FOR/OMP_SIMD. */
9542 static tree
9543 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
9545 tree **pdata = (tree **) data;
9546 *walk_subtrees = 0;
9547 switch (TREE_CODE (*tp))
9549 case OMP_FOR:
9550 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9552 pdata[3] = tp;
9553 return *tp;
9555 pdata[2] = tp;
9556 *walk_subtrees = 1;
9557 break;
9558 case OMP_SIMD:
9559 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9561 pdata[3] = tp;
9562 return *tp;
9564 break;
9565 case BIND_EXPR:
9566 if (BIND_EXPR_VARS (*tp)
9567 || (BIND_EXPR_BLOCK (*tp)
9568 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
9569 pdata[0] = tp;
9570 *walk_subtrees = 1;
9571 break;
9572 case STATEMENT_LIST:
9573 if (!tsi_one_before_end_p (tsi_start (*tp)))
9574 pdata[0] = tp;
9575 *walk_subtrees = 1;
9576 break;
9577 case TRY_FINALLY_EXPR:
9578 pdata[0] = tp;
9579 *walk_subtrees = 1;
9580 break;
9581 case OMP_PARALLEL:
9582 pdata[1] = tp;
9583 *walk_subtrees = 1;
9584 break;
9585 default:
9586 break;
9588 return NULL_TREE;
9591 /* Gimplify the gross structure of an OMP_FOR statement. */
9593 static enum gimplify_status
9594 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9596 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9597 enum gimplify_status ret = GS_ALL_DONE;
9598 enum gimplify_status tret;
9599 gomp_for *gfor;
9600 gimple_seq for_body, for_pre_body;
9601 int i;
9602 bitmap has_decl_expr = NULL;
9603 enum omp_region_type ort = ORT_WORKSHARE;
9605 orig_for_stmt = for_stmt = *expr_p;
9607 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9609 tree *data[4] = { NULL, NULL, NULL, NULL };
9610 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9611 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9612 find_combined_omp_for, data, NULL);
9613 if (inner_for_stmt == NULL_TREE)
9615 gcc_assert (seen_error ());
9616 *expr_p = NULL_TREE;
9617 return GS_ERROR;
9619 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
9621 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
9622 &OMP_FOR_PRE_BODY (for_stmt));
9623 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
9625 if (OMP_FOR_PRE_BODY (inner_for_stmt))
9627 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
9628 &OMP_FOR_PRE_BODY (for_stmt));
9629 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
9632 if (data[0])
9634 /* We have some statements or variable declarations in between
9635 the composite construct directives. Move them around the
9636 inner_for_stmt. */
9637 data[0] = expr_p;
9638 for (i = 0; i < 3; i++)
9639 if (data[i])
9641 tree t = *data[i];
9642 if (i < 2 && data[i + 1] == &OMP_BODY (t))
9643 data[i + 1] = data[i];
9644 *data[i] = OMP_BODY (t);
9645 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9646 NULL_TREE, make_node (BLOCK));
9647 OMP_BODY (t) = body;
9648 append_to_statement_list_force (inner_for_stmt,
9649 &BIND_EXPR_BODY (body));
9650 *data[3] = t;
9651 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
9652 gcc_assert (*data[3] == inner_for_stmt);
9654 return GS_OK;
9657 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
9658 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
9659 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
9660 i)) == TREE_LIST)
9662 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
9663 /* Class iterators aren't allowed on OMP_SIMD, so the only
9664 case we need to solve is distribute parallel for. */
9665 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
9666 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
9667 && data[1]);
9668 tree orig_decl = TREE_PURPOSE (orig);
9669 tree last = TREE_VALUE (orig);
9670 tree *pc;
9671 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
9672 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
9673 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
9674 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
9675 && OMP_CLAUSE_DECL (*pc) == orig_decl)
9676 break;
9677 if (*pc == NULL_TREE)
9679 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
9681 /* private clause will appear only on inner_for_stmt.
9682 Change it into firstprivate, and add private clause
9683 on for_stmt. */
9684 tree c = copy_node (*pc);
9685 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9686 OMP_FOR_CLAUSES (for_stmt) = c;
9687 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
9688 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9690 else
9692 /* lastprivate clause will appear on both inner_for_stmt
9693 and for_stmt. Add firstprivate clause to
9694 inner_for_stmt. */
9695 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
9696 OMP_CLAUSE_FIRSTPRIVATE);
9697 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
9698 OMP_CLAUSE_CHAIN (c) = *pc;
9699 *pc = c;
9700 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9702 tree c = build_omp_clause (UNKNOWN_LOCATION,
9703 OMP_CLAUSE_FIRSTPRIVATE);
9704 OMP_CLAUSE_DECL (c) = last;
9705 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9706 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9707 c = build_omp_clause (UNKNOWN_LOCATION,
9708 *pc ? OMP_CLAUSE_SHARED
9709 : OMP_CLAUSE_FIRSTPRIVATE);
9710 OMP_CLAUSE_DECL (c) = orig_decl;
9711 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9712 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9716 switch (TREE_CODE (for_stmt))
9718 case OMP_FOR:
9719 case OMP_DISTRIBUTE:
9720 break;
9721 case OACC_LOOP:
9722 ort = ORT_ACC;
9723 break;
9724 case OMP_TASKLOOP:
9725 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9726 ort = ORT_UNTIED_TASK;
9727 else
9728 ort = ORT_TASK;
9729 break;
9730 case OMP_SIMD:
9731 ort = ORT_SIMD;
9732 break;
9733 default:
9734 gcc_unreachable ();
9737 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9738 clause for the IV. */
9739 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9741 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9742 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9743 decl = TREE_OPERAND (t, 0);
9744 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9745 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9746 && OMP_CLAUSE_DECL (c) == decl)
9748 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9749 break;
9753 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9754 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9755 TREE_CODE (for_stmt));
9757 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9758 gimplify_omp_ctxp->distribute = true;
9760 /* Handle OMP_FOR_INIT. */
9761 for_pre_body = NULL;
9762 if ((ort == ORT_SIMD
9763 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
9764 && OMP_FOR_PRE_BODY (for_stmt))
9766 has_decl_expr = BITMAP_ALLOC (NULL);
9767 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9768 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9769 == VAR_DECL)
9771 t = OMP_FOR_PRE_BODY (for_stmt);
9772 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9774 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9776 tree_stmt_iterator si;
9777 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9778 tsi_next (&si))
9780 t = tsi_stmt (si);
9781 if (TREE_CODE (t) == DECL_EXPR
9782 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9783 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9787 if (OMP_FOR_PRE_BODY (for_stmt))
9789 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9790 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9791 else
9793 struct gimplify_omp_ctx ctx;
9794 memset (&ctx, 0, sizeof (ctx));
9795 ctx.region_type = ORT_NONE;
9796 gimplify_omp_ctxp = &ctx;
9797 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9798 gimplify_omp_ctxp = NULL;
9801 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9803 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9804 for_stmt = inner_for_stmt;
9806 /* For taskloop, need to gimplify the start, end and step before the
9807 taskloop, outside of the taskloop omp context. */
9808 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9810 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9812 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9813 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9815 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9816 TREE_OPERAND (t, 1)
9817 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9818 gimple_seq_empty_p (for_pre_body)
9819 ? pre_p : &for_pre_body, NULL,
9820 false);
9821 /* Reference to pointer conversion is considered useless,
9822 but is significant for firstprivate clause. Force it
9823 here. */
9824 if (TREE_CODE (type) == POINTER_TYPE
9825 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9826 == REFERENCE_TYPE))
9828 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9829 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9830 TREE_OPERAND (t, 1));
9831 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9832 ? pre_p : &for_pre_body);
9833 TREE_OPERAND (t, 1) = v;
9835 tree c = build_omp_clause (input_location,
9836 OMP_CLAUSE_FIRSTPRIVATE);
9837 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9838 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9839 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9842 /* Handle OMP_FOR_COND. */
9843 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9844 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9846 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9847 TREE_OPERAND (t, 1)
9848 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9849 gimple_seq_empty_p (for_pre_body)
9850 ? pre_p : &for_pre_body, NULL,
9851 false);
9852 /* Reference to pointer conversion is considered useless,
9853 but is significant for firstprivate clause. Force it
9854 here. */
9855 if (TREE_CODE (type) == POINTER_TYPE
9856 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9857 == REFERENCE_TYPE))
9859 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9860 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9861 TREE_OPERAND (t, 1));
9862 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9863 ? pre_p : &for_pre_body);
9864 TREE_OPERAND (t, 1) = v;
9866 tree c = build_omp_clause (input_location,
9867 OMP_CLAUSE_FIRSTPRIVATE);
9868 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9869 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9870 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9873 /* Handle OMP_FOR_INCR. */
9874 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9875 if (TREE_CODE (t) == MODIFY_EXPR)
9877 decl = TREE_OPERAND (t, 0);
9878 t = TREE_OPERAND (t, 1);
9879 tree *tp = &TREE_OPERAND (t, 1);
9880 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9881 tp = &TREE_OPERAND (t, 0);
9883 if (!is_gimple_constant (*tp))
9885 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9886 ? pre_p : &for_pre_body;
9887 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9888 tree c = build_omp_clause (input_location,
9889 OMP_CLAUSE_FIRSTPRIVATE);
9890 OMP_CLAUSE_DECL (c) = *tp;
9891 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9892 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9897 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9898 OMP_TASKLOOP);
9901 if (orig_for_stmt != for_stmt)
9902 gimplify_omp_ctxp->combined_loop = true;
9904 for_body = NULL;
9905 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9906 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9907 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9908 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9910 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9911 bool is_doacross = false;
9912 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9914 is_doacross = true;
9915 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9916 (OMP_FOR_INIT (for_stmt))
9917 * 2);
9919 int collapse = 1, tile = 0;
9920 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9921 if (c)
9922 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9923 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9924 if (c)
9925 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9926 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9928 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9929 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9930 decl = TREE_OPERAND (t, 0);
9931 gcc_assert (DECL_P (decl));
9932 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9933 || POINTER_TYPE_P (TREE_TYPE (decl)));
9934 if (is_doacross)
9936 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9938 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
9939 if (TREE_CODE (orig_decl) == TREE_LIST)
9940 orig_decl = TREE_PURPOSE (orig_decl);
9941 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
9943 else
9944 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9945 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9948 /* Make sure the iteration variable is private. */
9949 tree c = NULL_TREE;
9950 tree c2 = NULL_TREE;
9951 if (orig_for_stmt != for_stmt)
9953 /* Preserve this information until we gimplify the inner simd. */
9954 if (has_decl_expr
9955 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9956 TREE_PRIVATE (t) = 1;
9958 else if (ort == ORT_SIMD)
9960 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9961 (splay_tree_key) decl);
9962 omp_is_private (gimplify_omp_ctxp, decl,
9963 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9964 != 1));
9965 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9966 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9967 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9969 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9970 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9971 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9972 if ((has_decl_expr
9973 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9974 || TREE_PRIVATE (t))
9976 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9977 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9979 struct gimplify_omp_ctx *outer
9980 = gimplify_omp_ctxp->outer_context;
9981 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9983 if (outer->region_type == ORT_WORKSHARE
9984 && outer->combined_loop)
9986 n = splay_tree_lookup (outer->variables,
9987 (splay_tree_key)decl);
9988 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9990 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9991 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9993 else
9995 struct gimplify_omp_ctx *octx = outer->outer_context;
9996 if (octx
9997 && octx->region_type == ORT_COMBINED_PARALLEL
9998 && octx->outer_context
9999 && (octx->outer_context->region_type
10000 == ORT_WORKSHARE)
10001 && octx->outer_context->combined_loop)
10003 octx = octx->outer_context;
10004 n = splay_tree_lookup (octx->variables,
10005 (splay_tree_key)decl);
10006 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10008 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10009 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10016 OMP_CLAUSE_DECL (c) = decl;
10017 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10018 OMP_FOR_CLAUSES (for_stmt) = c;
10019 omp_add_variable (gimplify_omp_ctxp, decl, flags);
10020 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10022 if (outer->region_type == ORT_WORKSHARE
10023 && outer->combined_loop)
10025 if (outer->outer_context
10026 && (outer->outer_context->region_type
10027 == ORT_COMBINED_PARALLEL))
10028 outer = outer->outer_context;
10029 else if (omp_check_private (outer, decl, false))
10030 outer = NULL;
10032 else if (((outer->region_type & ORT_TASK) != 0)
10033 && outer->combined_loop
10034 && !omp_check_private (gimplify_omp_ctxp,
10035 decl, false))
10037 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10039 omp_notice_variable (outer, decl, true);
10040 outer = NULL;
10042 if (outer)
10044 n = splay_tree_lookup (outer->variables,
10045 (splay_tree_key)decl);
10046 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10048 omp_add_variable (outer, decl,
10049 GOVD_LASTPRIVATE | GOVD_SEEN);
10050 if (outer->region_type == ORT_COMBINED_PARALLEL
10051 && outer->outer_context
10052 && (outer->outer_context->region_type
10053 == ORT_WORKSHARE)
10054 && outer->outer_context->combined_loop)
10056 outer = outer->outer_context;
10057 n = splay_tree_lookup (outer->variables,
10058 (splay_tree_key)decl);
10059 if (omp_check_private (outer, decl, false))
10060 outer = NULL;
10061 else if (n == NULL
10062 || ((n->value & GOVD_DATA_SHARE_CLASS)
10063 == 0))
10064 omp_add_variable (outer, decl,
10065 GOVD_LASTPRIVATE
10066 | GOVD_SEEN);
10067 else
10068 outer = NULL;
10070 if (outer && outer->outer_context
10071 && (outer->outer_context->region_type
10072 == ORT_COMBINED_TEAMS))
10074 outer = outer->outer_context;
10075 n = splay_tree_lookup (outer->variables,
10076 (splay_tree_key)decl);
10077 if (n == NULL
10078 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10079 omp_add_variable (outer, decl,
10080 GOVD_SHARED | GOVD_SEEN);
10081 else
10082 outer = NULL;
10084 if (outer && outer->outer_context)
10085 omp_notice_variable (outer->outer_context, decl,
10086 true);
10091 else
10093 bool lastprivate
10094 = (!has_decl_expr
10095 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10096 if (TREE_PRIVATE (t))
10097 lastprivate = false;
10098 struct gimplify_omp_ctx *outer
10099 = gimplify_omp_ctxp->outer_context;
10100 if (outer && lastprivate)
10102 if (outer->region_type == ORT_WORKSHARE
10103 && outer->combined_loop)
10105 n = splay_tree_lookup (outer->variables,
10106 (splay_tree_key)decl);
10107 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10109 lastprivate = false;
10110 outer = NULL;
10112 else if (outer->outer_context
10113 && (outer->outer_context->region_type
10114 == ORT_COMBINED_PARALLEL))
10115 outer = outer->outer_context;
10116 else if (omp_check_private (outer, decl, false))
10117 outer = NULL;
10119 else if (((outer->region_type & ORT_TASK) != 0)
10120 && outer->combined_loop
10121 && !omp_check_private (gimplify_omp_ctxp,
10122 decl, false))
10124 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10126 omp_notice_variable (outer, decl, true);
10127 outer = NULL;
10129 if (outer)
10131 n = splay_tree_lookup (outer->variables,
10132 (splay_tree_key)decl);
10133 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10135 omp_add_variable (outer, decl,
10136 GOVD_LASTPRIVATE | GOVD_SEEN);
10137 if (outer->region_type == ORT_COMBINED_PARALLEL
10138 && outer->outer_context
10139 && (outer->outer_context->region_type
10140 == ORT_WORKSHARE)
10141 && outer->outer_context->combined_loop)
10143 outer = outer->outer_context;
10144 n = splay_tree_lookup (outer->variables,
10145 (splay_tree_key)decl);
10146 if (omp_check_private (outer, decl, false))
10147 outer = NULL;
10148 else if (n == NULL
10149 || ((n->value & GOVD_DATA_SHARE_CLASS)
10150 == 0))
10151 omp_add_variable (outer, decl,
10152 GOVD_LASTPRIVATE
10153 | GOVD_SEEN);
10154 else
10155 outer = NULL;
10157 if (outer && outer->outer_context
10158 && (outer->outer_context->region_type
10159 == ORT_COMBINED_TEAMS))
10161 outer = outer->outer_context;
10162 n = splay_tree_lookup (outer->variables,
10163 (splay_tree_key)decl);
10164 if (n == NULL
10165 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10166 omp_add_variable (outer, decl,
10167 GOVD_SHARED | GOVD_SEEN);
10168 else
10169 outer = NULL;
10171 if (outer && outer->outer_context)
10172 omp_notice_variable (outer->outer_context, decl,
10173 true);
10178 c = build_omp_clause (input_location,
10179 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10180 : OMP_CLAUSE_PRIVATE);
10181 OMP_CLAUSE_DECL (c) = decl;
10182 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10183 OMP_FOR_CLAUSES (for_stmt) = c;
10184 omp_add_variable (gimplify_omp_ctxp, decl,
10185 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10186 | GOVD_EXPLICIT | GOVD_SEEN);
10187 c = NULL_TREE;
10190 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10191 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10192 else
10193 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10195 /* If DECL is not a gimple register, create a temporary variable to act
10196 as an iteration counter. This is valid, since DECL cannot be
10197 modified in the body of the loop. Similarly for any iteration vars
10198 in simd with collapse > 1 where the iterator vars must be
10199 lastprivate. */
10200 if (orig_for_stmt != for_stmt)
10201 var = decl;
10202 else if (!is_gimple_reg (decl)
10203 || (ort == ORT_SIMD
10204 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10206 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10207 /* Make sure omp_add_variable is not called on it prematurely.
10208 We call it ourselves a few lines later. */
10209 gimplify_omp_ctxp = NULL;
10210 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10211 gimplify_omp_ctxp = ctx;
10212 TREE_OPERAND (t, 0) = var;
10214 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10216 if (ort == ORT_SIMD
10217 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10219 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10220 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10221 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10222 OMP_CLAUSE_DECL (c2) = var;
10223 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10224 OMP_FOR_CLAUSES (for_stmt) = c2;
10225 omp_add_variable (gimplify_omp_ctxp, var,
10226 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10227 if (c == NULL_TREE)
10229 c = c2;
10230 c2 = NULL_TREE;
10233 else
10234 omp_add_variable (gimplify_omp_ctxp, var,
10235 GOVD_PRIVATE | GOVD_SEEN);
10237 else
10238 var = decl;
10240 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10241 is_gimple_val, fb_rvalue, false);
10242 ret = MIN (ret, tret);
10243 if (ret == GS_ERROR)
10244 return ret;
10246 /* Handle OMP_FOR_COND. */
10247 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10248 gcc_assert (COMPARISON_CLASS_P (t));
10249 gcc_assert (TREE_OPERAND (t, 0) == decl);
10251 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10252 is_gimple_val, fb_rvalue, false);
10253 ret = MIN (ret, tret);
10255 /* Handle OMP_FOR_INCR. */
10256 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10257 switch (TREE_CODE (t))
10259 case PREINCREMENT_EXPR:
10260 case POSTINCREMENT_EXPR:
10262 tree decl = TREE_OPERAND (t, 0);
10263 /* c_omp_for_incr_canonicalize_ptr() should have been
10264 called to massage things appropriately. */
10265 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10267 if (orig_for_stmt != for_stmt)
10268 break;
10269 t = build_int_cst (TREE_TYPE (decl), 1);
10270 if (c)
10271 OMP_CLAUSE_LINEAR_STEP (c) = t;
10272 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10273 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10274 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10275 break;
10278 case PREDECREMENT_EXPR:
10279 case POSTDECREMENT_EXPR:
10280 /* c_omp_for_incr_canonicalize_ptr() should have been
10281 called to massage things appropriately. */
10282 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10283 if (orig_for_stmt != for_stmt)
10284 break;
10285 t = build_int_cst (TREE_TYPE (decl), -1);
10286 if (c)
10287 OMP_CLAUSE_LINEAR_STEP (c) = t;
10288 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10289 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10290 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10291 break;
10293 case MODIFY_EXPR:
10294 gcc_assert (TREE_OPERAND (t, 0) == decl);
10295 TREE_OPERAND (t, 0) = var;
10297 t = TREE_OPERAND (t, 1);
10298 switch (TREE_CODE (t))
10300 case PLUS_EXPR:
10301 if (TREE_OPERAND (t, 1) == decl)
10303 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10304 TREE_OPERAND (t, 0) = var;
10305 break;
10308 /* Fallthru. */
10309 case MINUS_EXPR:
10310 case POINTER_PLUS_EXPR:
10311 gcc_assert (TREE_OPERAND (t, 0) == decl);
10312 TREE_OPERAND (t, 0) = var;
10313 break;
10314 default:
10315 gcc_unreachable ();
10318 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10319 is_gimple_val, fb_rvalue, false);
10320 ret = MIN (ret, tret);
10321 if (c)
10323 tree step = TREE_OPERAND (t, 1);
10324 tree stept = TREE_TYPE (decl);
10325 if (POINTER_TYPE_P (stept))
10326 stept = sizetype;
10327 step = fold_convert (stept, step);
10328 if (TREE_CODE (t) == MINUS_EXPR)
10329 step = fold_build1 (NEGATE_EXPR, stept, step);
10330 OMP_CLAUSE_LINEAR_STEP (c) = step;
10331 if (step != TREE_OPERAND (t, 1))
10333 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10334 &for_pre_body, NULL,
10335 is_gimple_val, fb_rvalue, false);
10336 ret = MIN (ret, tret);
10339 break;
10341 default:
10342 gcc_unreachable ();
10345 if (c2)
10347 gcc_assert (c);
10348 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10351 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10353 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10354 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10355 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10356 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10357 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10358 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10359 && OMP_CLAUSE_DECL (c) == decl)
10361 if (is_doacross && (collapse == 1 || i >= collapse))
10362 t = var;
10363 else
10365 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10366 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10367 gcc_assert (TREE_OPERAND (t, 0) == var);
10368 t = TREE_OPERAND (t, 1);
10369 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10370 || TREE_CODE (t) == MINUS_EXPR
10371 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10372 gcc_assert (TREE_OPERAND (t, 0) == var);
10373 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10374 is_doacross ? var : decl,
10375 TREE_OPERAND (t, 1));
10377 gimple_seq *seq;
10378 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10379 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10380 else
10381 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10382 gimplify_assign (decl, t, seq);
10387 BITMAP_FREE (has_decl_expr);
10389 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10391 push_gimplify_context ();
10392 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10394 OMP_FOR_BODY (orig_for_stmt)
10395 = build3 (BIND_EXPR, void_type_node, NULL,
10396 OMP_FOR_BODY (orig_for_stmt), NULL);
10397 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10401 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10402 &for_body);
10404 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10406 if (gimple_code (g) == GIMPLE_BIND)
10407 pop_gimplify_context (g);
10408 else
10409 pop_gimplify_context (NULL);
10412 if (orig_for_stmt != for_stmt)
10413 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10415 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10416 decl = TREE_OPERAND (t, 0);
10417 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10418 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10419 gimplify_omp_ctxp = ctx->outer_context;
10420 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10421 gimplify_omp_ctxp = ctx;
10422 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10423 TREE_OPERAND (t, 0) = var;
10424 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10425 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10426 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10429 gimplify_adjust_omp_clauses (pre_p, for_body,
10430 &OMP_FOR_CLAUSES (orig_for_stmt),
10431 TREE_CODE (orig_for_stmt));
10433 int kind;
10434 switch (TREE_CODE (orig_for_stmt))
10436 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10437 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10438 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10439 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10440 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10441 default:
10442 gcc_unreachable ();
10444 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10445 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10446 for_pre_body);
10447 if (orig_for_stmt != for_stmt)
10448 gimple_omp_for_set_combined_p (gfor, true);
10449 if (gimplify_omp_ctxp
10450 && (gimplify_omp_ctxp->combined_loop
10451 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10452 && gimplify_omp_ctxp->outer_context
10453 && gimplify_omp_ctxp->outer_context->combined_loop)))
10455 gimple_omp_for_set_combined_into_p (gfor, true);
10456 if (gimplify_omp_ctxp->combined_loop)
10457 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10458 else
10459 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10462 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10464 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10465 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10466 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10467 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10468 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10469 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10470 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10471 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10474 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10475 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10476 The outer taskloop stands for computing the number of iterations,
10477 counts for collapsed loops and holding taskloop specific clauses.
10478 The task construct stands for the effect of data sharing on the
10479 explicit task it creates and the inner taskloop stands for expansion
10480 of the static loop inside of the explicit task construct. */
10481 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10483 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10484 tree task_clauses = NULL_TREE;
10485 tree c = *gfor_clauses_ptr;
10486 tree *gtask_clauses_ptr = &task_clauses;
10487 tree outer_for_clauses = NULL_TREE;
10488 tree *gforo_clauses_ptr = &outer_for_clauses;
10489 for (; c; c = OMP_CLAUSE_CHAIN (c))
10490 switch (OMP_CLAUSE_CODE (c))
10492 /* These clauses are allowed on task, move them there. */
10493 case OMP_CLAUSE_SHARED:
10494 case OMP_CLAUSE_FIRSTPRIVATE:
10495 case OMP_CLAUSE_DEFAULT:
10496 case OMP_CLAUSE_IF:
10497 case OMP_CLAUSE_UNTIED:
10498 case OMP_CLAUSE_FINAL:
10499 case OMP_CLAUSE_MERGEABLE:
10500 case OMP_CLAUSE_PRIORITY:
10501 *gtask_clauses_ptr = c;
10502 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10503 break;
10504 case OMP_CLAUSE_PRIVATE:
10505 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10507 /* We want private on outer for and firstprivate
10508 on task. */
10509 *gtask_clauses_ptr
10510 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10511 OMP_CLAUSE_FIRSTPRIVATE);
10512 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10513 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10514 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10515 *gforo_clauses_ptr = c;
10516 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10518 else
10520 *gtask_clauses_ptr = c;
10521 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10523 break;
10524 /* These clauses go into outer taskloop clauses. */
10525 case OMP_CLAUSE_GRAINSIZE:
10526 case OMP_CLAUSE_NUM_TASKS:
10527 case OMP_CLAUSE_NOGROUP:
10528 *gforo_clauses_ptr = c;
10529 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10530 break;
10531 /* Taskloop clause we duplicate on both taskloops. */
10532 case OMP_CLAUSE_COLLAPSE:
10533 *gfor_clauses_ptr = c;
10534 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10535 *gforo_clauses_ptr = copy_node (c);
10536 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10537 break;
10538 /* For lastprivate, keep the clause on inner taskloop, and add
10539 a shared clause on task. If the same decl is also firstprivate,
10540 add also firstprivate clause on the inner taskloop. */
10541 case OMP_CLAUSE_LASTPRIVATE:
10542 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10544 /* For taskloop C++ lastprivate IVs, we want:
10545 1) private on outer taskloop
10546 2) firstprivate and shared on task
10547 3) lastprivate on inner taskloop */
10548 *gtask_clauses_ptr
10549 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10550 OMP_CLAUSE_FIRSTPRIVATE);
10551 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10552 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10553 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10554 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10555 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10556 OMP_CLAUSE_PRIVATE);
10557 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10558 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10559 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10560 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10562 *gfor_clauses_ptr = c;
10563 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10564 *gtask_clauses_ptr
10565 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10566 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10567 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10568 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10569 gtask_clauses_ptr
10570 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10571 break;
10572 default:
10573 gcc_unreachable ();
10575 *gfor_clauses_ptr = NULL_TREE;
10576 *gtask_clauses_ptr = NULL_TREE;
10577 *gforo_clauses_ptr = NULL_TREE;
10578 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10579 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10580 NULL_TREE, NULL_TREE, NULL_TREE);
10581 gimple_omp_task_set_taskloop_p (g, true);
10582 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10583 gomp_for *gforo
10584 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10585 gimple_omp_for_collapse (gfor),
10586 gimple_omp_for_pre_body (gfor));
10587 gimple_omp_for_set_pre_body (gfor, NULL);
10588 gimple_omp_for_set_combined_p (gforo, true);
10589 gimple_omp_for_set_combined_into_p (gfor, true);
10590 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10592 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10593 tree v = create_tmp_var (type);
10594 gimple_omp_for_set_index (gforo, i, v);
10595 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10596 gimple_omp_for_set_initial (gforo, i, t);
10597 gimple_omp_for_set_cond (gforo, i,
10598 gimple_omp_for_cond (gfor, i));
10599 t = unshare_expr (gimple_omp_for_final (gfor, i));
10600 gimple_omp_for_set_final (gforo, i, t);
10601 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10602 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10603 TREE_OPERAND (t, 0) = v;
10604 gimple_omp_for_set_incr (gforo, i, t);
10605 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10606 OMP_CLAUSE_DECL (t) = v;
10607 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10608 gimple_omp_for_set_clauses (gforo, t);
10610 gimplify_seq_add_stmt (pre_p, gforo);
10612 else
10613 gimplify_seq_add_stmt (pre_p, gfor);
10614 if (ret != GS_ALL_DONE)
10615 return GS_ERROR;
10616 *expr_p = NULL_TREE;
10617 return GS_ALL_DONE;
10620 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10621 of OMP_TARGET's body. */
10623 static tree
10624 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10626 *walk_subtrees = 0;
10627 switch (TREE_CODE (*tp))
10629 case OMP_TEAMS:
10630 return *tp;
10631 case BIND_EXPR:
10632 case STATEMENT_LIST:
10633 *walk_subtrees = 1;
10634 break;
10635 default:
10636 break;
10638 return NULL_TREE;
10641 /* Helper function of optimize_target_teams, determine if the expression
10642 can be computed safely before the target construct on the host. */
10644 static tree
10645 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10647 splay_tree_node n;
10649 if (TYPE_P (*tp))
10651 *walk_subtrees = 0;
10652 return NULL_TREE;
10654 switch (TREE_CODE (*tp))
10656 case VAR_DECL:
10657 case PARM_DECL:
10658 case RESULT_DECL:
10659 *walk_subtrees = 0;
10660 if (error_operand_p (*tp)
10661 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10662 || DECL_HAS_VALUE_EXPR_P (*tp)
10663 || DECL_THREAD_LOCAL_P (*tp)
10664 || TREE_SIDE_EFFECTS (*tp)
10665 || TREE_THIS_VOLATILE (*tp))
10666 return *tp;
10667 if (is_global_var (*tp)
10668 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10669 || lookup_attribute ("omp declare target link",
10670 DECL_ATTRIBUTES (*tp))))
10671 return *tp;
10672 if (VAR_P (*tp)
10673 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10674 && !is_global_var (*tp)
10675 && decl_function_context (*tp) == current_function_decl)
10676 return *tp;
10677 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10678 (splay_tree_key) *tp);
10679 if (n == NULL)
10681 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10682 return NULL_TREE;
10683 return *tp;
10685 else if (n->value & GOVD_LOCAL)
10686 return *tp;
10687 else if (n->value & GOVD_FIRSTPRIVATE)
10688 return NULL_TREE;
10689 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10690 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10691 return NULL_TREE;
10692 return *tp;
10693 case INTEGER_CST:
10694 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10695 return *tp;
10696 return NULL_TREE;
10697 case TARGET_EXPR:
10698 if (TARGET_EXPR_INITIAL (*tp)
10699 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10700 return *tp;
10701 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10702 walk_subtrees, NULL);
10703 /* Allow some reasonable subset of integral arithmetics. */
10704 case PLUS_EXPR:
10705 case MINUS_EXPR:
10706 case MULT_EXPR:
10707 case TRUNC_DIV_EXPR:
10708 case CEIL_DIV_EXPR:
10709 case FLOOR_DIV_EXPR:
10710 case ROUND_DIV_EXPR:
10711 case TRUNC_MOD_EXPR:
10712 case CEIL_MOD_EXPR:
10713 case FLOOR_MOD_EXPR:
10714 case ROUND_MOD_EXPR:
10715 case RDIV_EXPR:
10716 case EXACT_DIV_EXPR:
10717 case MIN_EXPR:
10718 case MAX_EXPR:
10719 case LSHIFT_EXPR:
10720 case RSHIFT_EXPR:
10721 case BIT_IOR_EXPR:
10722 case BIT_XOR_EXPR:
10723 case BIT_AND_EXPR:
10724 case NEGATE_EXPR:
10725 case ABS_EXPR:
10726 case BIT_NOT_EXPR:
10727 case NON_LVALUE_EXPR:
10728 CASE_CONVERT:
10729 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10730 return *tp;
10731 return NULL_TREE;
10732 /* And disallow anything else, except for comparisons. */
10733 default:
10734 if (COMPARISON_CLASS_P (*tp))
10735 return NULL_TREE;
10736 return *tp;
10740 /* Try to determine if the num_teams and/or thread_limit expressions
10741 can have their values determined already before entering the
10742 target construct.
10743 INTEGER_CSTs trivially are,
10744 integral decls that are firstprivate (explicitly or implicitly)
10745 or explicitly map(always, to:) or map(always, tofrom:) on the target
10746 region too, and expressions involving simple arithmetics on those
10747 too, function calls are not ok, dereferencing something neither etc.
10748 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10749 EXPR based on what we find:
10750 0 stands for clause not specified at all, use implementation default
10751 -1 stands for value that can't be determined easily before entering
10752 the target construct.
10753 If teams construct is not present at all, use 1 for num_teams
10754 and 0 for thread_limit (only one team is involved, and the thread
10755 limit is implementation defined. */
10757 static void
10758 optimize_target_teams (tree target, gimple_seq *pre_p)
10760 tree body = OMP_BODY (target);
10761 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10762 tree num_teams = integer_zero_node;
10763 tree thread_limit = integer_zero_node;
10764 location_t num_teams_loc = EXPR_LOCATION (target);
10765 location_t thread_limit_loc = EXPR_LOCATION (target);
10766 tree c, *p, expr;
10767 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10769 if (teams == NULL_TREE)
10770 num_teams = integer_one_node;
10771 else
10772 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10774 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10776 p = &num_teams;
10777 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10779 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10781 p = &thread_limit;
10782 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10784 else
10785 continue;
10786 expr = OMP_CLAUSE_OPERAND (c, 0);
10787 if (TREE_CODE (expr) == INTEGER_CST)
10789 *p = expr;
10790 continue;
10792 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10794 *p = integer_minus_one_node;
10795 continue;
10797 *p = expr;
10798 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10799 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10800 == GS_ERROR)
10802 gimplify_omp_ctxp = target_ctx;
10803 *p = integer_minus_one_node;
10804 continue;
10806 gimplify_omp_ctxp = target_ctx;
10807 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10808 OMP_CLAUSE_OPERAND (c, 0) = *p;
10810 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10811 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10812 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10813 OMP_TARGET_CLAUSES (target) = c;
10814 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10815 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10816 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10817 OMP_TARGET_CLAUSES (target) = c;
10820 /* Gimplify the gross structure of several OMP constructs. */
10822 static void
10823 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10825 tree expr = *expr_p;
10826 gimple *stmt;
10827 gimple_seq body = NULL;
10828 enum omp_region_type ort;
10830 switch (TREE_CODE (expr))
10832 case OMP_SECTIONS:
10833 case OMP_SINGLE:
10834 ort = ORT_WORKSHARE;
10835 break;
10836 case OMP_TARGET:
10837 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10838 break;
10839 case OACC_KERNELS:
10840 ort = ORT_ACC_KERNELS;
10841 break;
10842 case OACC_PARALLEL:
10843 ort = ORT_ACC_PARALLEL;
10844 break;
10845 case OACC_DATA:
10846 ort = ORT_ACC_DATA;
10847 break;
10848 case OMP_TARGET_DATA:
10849 ort = ORT_TARGET_DATA;
10850 break;
10851 case OMP_TEAMS:
10852 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10853 break;
10854 case OACC_HOST_DATA:
10855 ort = ORT_ACC_HOST_DATA;
10856 break;
10857 default:
10858 gcc_unreachable ();
10860 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10861 TREE_CODE (expr));
10862 if (TREE_CODE (expr) == OMP_TARGET)
10863 optimize_target_teams (expr, pre_p);
10864 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10866 push_gimplify_context ();
10867 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10868 if (gimple_code (g) == GIMPLE_BIND)
10869 pop_gimplify_context (g);
10870 else
10871 pop_gimplify_context (NULL);
10872 if ((ort & ORT_TARGET_DATA) != 0)
10874 enum built_in_function end_ix;
10875 switch (TREE_CODE (expr))
10877 case OACC_DATA:
10878 case OACC_HOST_DATA:
10879 end_ix = BUILT_IN_GOACC_DATA_END;
10880 break;
10881 case OMP_TARGET_DATA:
10882 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10883 break;
10884 default:
10885 gcc_unreachable ();
10887 tree fn = builtin_decl_explicit (end_ix);
10888 g = gimple_build_call (fn, 0);
10889 gimple_seq cleanup = NULL;
10890 gimple_seq_add_stmt (&cleanup, g);
10891 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10892 body = NULL;
10893 gimple_seq_add_stmt (&body, g);
10896 else
10897 gimplify_and_add (OMP_BODY (expr), &body);
10898 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10899 TREE_CODE (expr));
10901 switch (TREE_CODE (expr))
10903 case OACC_DATA:
10904 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10905 OMP_CLAUSES (expr));
10906 break;
10907 case OACC_KERNELS:
10908 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10909 OMP_CLAUSES (expr));
10910 break;
10911 case OACC_HOST_DATA:
10912 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10913 OMP_CLAUSES (expr));
10914 break;
10915 case OACC_PARALLEL:
10916 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10917 OMP_CLAUSES (expr));
10918 break;
10919 case OMP_SECTIONS:
10920 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10921 break;
10922 case OMP_SINGLE:
10923 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10924 break;
10925 case OMP_TARGET:
10926 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10927 OMP_CLAUSES (expr));
10928 break;
10929 case OMP_TARGET_DATA:
10930 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10931 OMP_CLAUSES (expr));
10932 break;
10933 case OMP_TEAMS:
10934 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10935 break;
10936 default:
10937 gcc_unreachable ();
10940 gimplify_seq_add_stmt (pre_p, stmt);
10941 *expr_p = NULL_TREE;
10944 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10945 target update constructs. */
10947 static void
10948 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10950 tree expr = *expr_p;
10951 int kind;
10952 gomp_target *stmt;
10953 enum omp_region_type ort = ORT_WORKSHARE;
10955 switch (TREE_CODE (expr))
10957 case OACC_ENTER_DATA:
10958 case OACC_EXIT_DATA:
10959 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10960 ort = ORT_ACC;
10961 break;
10962 case OACC_UPDATE:
10963 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10964 ort = ORT_ACC;
10965 break;
10966 case OMP_TARGET_UPDATE:
10967 kind = GF_OMP_TARGET_KIND_UPDATE;
10968 break;
10969 case OMP_TARGET_ENTER_DATA:
10970 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10971 break;
10972 case OMP_TARGET_EXIT_DATA:
10973 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10974 break;
10975 default:
10976 gcc_unreachable ();
10978 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10979 ort, TREE_CODE (expr));
10980 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10981 TREE_CODE (expr));
10982 if (TREE_CODE (expr) == OACC_UPDATE
10983 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
10984 OMP_CLAUSE_IF_PRESENT))
10986 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
10987 clause. */
10988 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10989 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10990 switch (OMP_CLAUSE_MAP_KIND (c))
10992 case GOMP_MAP_FORCE_TO:
10993 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
10994 break;
10995 case GOMP_MAP_FORCE_FROM:
10996 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
10997 break;
10998 default:
10999 break;
11002 else if (TREE_CODE (expr) == OACC_EXIT_DATA
11003 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11004 OMP_CLAUSE_FINALIZE))
11006 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
11007 semantics apply to all mappings of this OpenACC directive. */
11008 bool finalize_marked = false;
11009 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11010 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11011 switch (OMP_CLAUSE_MAP_KIND (c))
11013 case GOMP_MAP_FROM:
11014 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
11015 finalize_marked = true;
11016 break;
11017 case GOMP_MAP_RELEASE:
11018 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
11019 finalize_marked = true;
11020 break;
11021 default:
11022 /* Check consistency: libgomp relies on the very first data
11023 mapping clause being marked, so make sure we did that before
11024 any other mapping clauses. */
11025 gcc_assert (finalize_marked);
11026 break;
11029 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
11031 gimplify_seq_add_stmt (pre_p, stmt);
11032 *expr_p = NULL_TREE;
11035 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
11036 stabilized the lhs of the atomic operation as *ADDR. Return true if
11037 EXPR is this stabilized form. */
11039 static bool
11040 goa_lhs_expr_p (tree expr, tree addr)
11042 /* Also include casts to other type variants. The C front end is fond
11043 of adding these for e.g. volatile variables. This is like
11044 STRIP_TYPE_NOPS but includes the main variant lookup. */
11045 STRIP_USELESS_TYPE_CONVERSION (expr);
11047 if (TREE_CODE (expr) == INDIRECT_REF)
11049 expr = TREE_OPERAND (expr, 0);
11050 while (expr != addr
11051 && (CONVERT_EXPR_P (expr)
11052 || TREE_CODE (expr) == NON_LVALUE_EXPR)
11053 && TREE_CODE (expr) == TREE_CODE (addr)
11054 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
11056 expr = TREE_OPERAND (expr, 0);
11057 addr = TREE_OPERAND (addr, 0);
11059 if (expr == addr)
11060 return true;
11061 return (TREE_CODE (addr) == ADDR_EXPR
11062 && TREE_CODE (expr) == ADDR_EXPR
11063 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
11065 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
11066 return true;
11067 return false;
11070 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
11071 expression does not involve the lhs, evaluate it into a temporary.
11072 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
11073 or -1 if an error was encountered. */
11075 static int
11076 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
11077 tree lhs_var)
11079 tree expr = *expr_p;
11080 int saw_lhs;
11082 if (goa_lhs_expr_p (expr, lhs_addr))
11084 *expr_p = lhs_var;
11085 return 1;
11087 if (is_gimple_val (expr))
11088 return 0;
11090 saw_lhs = 0;
11091 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
11093 case tcc_binary:
11094 case tcc_comparison:
11095 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
11096 lhs_var);
11097 /* FALLTHRU */
11098 case tcc_unary:
11099 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
11100 lhs_var);
11101 break;
11102 case tcc_expression:
11103 switch (TREE_CODE (expr))
11105 case TRUTH_ANDIF_EXPR:
11106 case TRUTH_ORIF_EXPR:
11107 case TRUTH_AND_EXPR:
11108 case TRUTH_OR_EXPR:
11109 case TRUTH_XOR_EXPR:
11110 case BIT_INSERT_EXPR:
11111 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11112 lhs_addr, lhs_var);
11113 /* FALLTHRU */
11114 case TRUTH_NOT_EXPR:
11115 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11116 lhs_addr, lhs_var);
11117 break;
11118 case COMPOUND_EXPR:
11119 /* Break out any preevaluations from cp_build_modify_expr. */
11120 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11121 expr = TREE_OPERAND (expr, 1))
11122 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11123 *expr_p = expr;
11124 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11125 default:
11126 break;
11128 break;
11129 case tcc_reference:
11130 if (TREE_CODE (expr) == BIT_FIELD_REF)
11131 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11132 lhs_addr, lhs_var);
11133 break;
11134 default:
11135 break;
11138 if (saw_lhs == 0)
11140 enum gimplify_status gs;
11141 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11142 if (gs != GS_ALL_DONE)
11143 saw_lhs = -1;
11146 return saw_lhs;
11149 /* Gimplify an OMP_ATOMIC statement. */
11151 static enum gimplify_status
11152 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11154 tree addr = TREE_OPERAND (*expr_p, 0);
11155 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11156 ? NULL : TREE_OPERAND (*expr_p, 1);
11157 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11158 tree tmp_load;
11159 gomp_atomic_load *loadstmt;
11160 gomp_atomic_store *storestmt;
11162 tmp_load = create_tmp_reg (type);
11163 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11164 return GS_ERROR;
11166 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11167 != GS_ALL_DONE)
11168 return GS_ERROR;
11170 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11171 gimplify_seq_add_stmt (pre_p, loadstmt);
11172 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11173 != GS_ALL_DONE)
11174 return GS_ERROR;
11176 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11177 rhs = tmp_load;
11178 storestmt = gimple_build_omp_atomic_store (rhs);
11179 gimplify_seq_add_stmt (pre_p, storestmt);
11180 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11182 gimple_omp_atomic_set_seq_cst (loadstmt);
11183 gimple_omp_atomic_set_seq_cst (storestmt);
11185 switch (TREE_CODE (*expr_p))
11187 case OMP_ATOMIC_READ:
11188 case OMP_ATOMIC_CAPTURE_OLD:
11189 *expr_p = tmp_load;
11190 gimple_omp_atomic_set_need_value (loadstmt);
11191 break;
11192 case OMP_ATOMIC_CAPTURE_NEW:
11193 *expr_p = rhs;
11194 gimple_omp_atomic_set_need_value (storestmt);
11195 break;
11196 default:
11197 *expr_p = NULL;
11198 break;
11201 return GS_ALL_DONE;
11204 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11205 body, and adding some EH bits. */
11207 static enum gimplify_status
11208 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11210 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11211 gimple *body_stmt;
11212 gtransaction *trans_stmt;
11213 gimple_seq body = NULL;
11214 int subcode = 0;
11216 /* Wrap the transaction body in a BIND_EXPR so we have a context
11217 where to put decls for OMP. */
11218 if (TREE_CODE (tbody) != BIND_EXPR)
11220 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11221 TREE_SIDE_EFFECTS (bind) = 1;
11222 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11223 TRANSACTION_EXPR_BODY (expr) = bind;
11226 push_gimplify_context ();
11227 temp = voidify_wrapper_expr (*expr_p, NULL);
11229 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11230 pop_gimplify_context (body_stmt);
11232 trans_stmt = gimple_build_transaction (body);
11233 if (TRANSACTION_EXPR_OUTER (expr))
11234 subcode = GTMA_IS_OUTER;
11235 else if (TRANSACTION_EXPR_RELAXED (expr))
11236 subcode = GTMA_IS_RELAXED;
11237 gimple_transaction_set_subcode (trans_stmt, subcode);
11239 gimplify_seq_add_stmt (pre_p, trans_stmt);
11241 if (temp)
11243 *expr_p = temp;
11244 return GS_OK;
11247 *expr_p = NULL_TREE;
11248 return GS_ALL_DONE;
11251 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11252 is the OMP_BODY of the original EXPR (which has already been
11253 gimplified so it's not present in the EXPR).
11255 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11257 static gimple *
11258 gimplify_omp_ordered (tree expr, gimple_seq body)
11260 tree c, decls;
11261 int failures = 0;
11262 unsigned int i;
11263 tree source_c = NULL_TREE;
11264 tree sink_c = NULL_TREE;
11266 if (gimplify_omp_ctxp)
11268 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11269 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11270 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11271 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11272 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11274 error_at (OMP_CLAUSE_LOCATION (c),
11275 "%<ordered%> construct with %<depend%> clause must be "
11276 "closely nested inside a loop with %<ordered%> clause "
11277 "with a parameter");
11278 failures++;
11280 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11281 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11283 bool fail = false;
11284 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11285 decls && TREE_CODE (decls) == TREE_LIST;
11286 decls = TREE_CHAIN (decls), ++i)
11287 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11288 continue;
11289 else if (TREE_VALUE (decls)
11290 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11292 error_at (OMP_CLAUSE_LOCATION (c),
11293 "variable %qE is not an iteration "
11294 "of outermost loop %d, expected %qE",
11295 TREE_VALUE (decls), i + 1,
11296 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11297 fail = true;
11298 failures++;
11300 else
11301 TREE_VALUE (decls)
11302 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11303 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11305 error_at (OMP_CLAUSE_LOCATION (c),
11306 "number of variables in %<depend(sink)%> "
11307 "clause does not match number of "
11308 "iteration variables");
11309 failures++;
11311 sink_c = c;
11313 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11314 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11316 if (source_c)
11318 error_at (OMP_CLAUSE_LOCATION (c),
11319 "more than one %<depend(source)%> clause on an "
11320 "%<ordered%> construct");
11321 failures++;
11323 else
11324 source_c = c;
11327 if (source_c && sink_c)
11329 error_at (OMP_CLAUSE_LOCATION (source_c),
11330 "%<depend(source)%> clause specified together with "
11331 "%<depend(sink:)%> clauses on the same construct");
11332 failures++;
11335 if (failures)
11336 return gimple_build_nop ();
11337 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11340 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11341 expression produces a value to be used as an operand inside a GIMPLE
11342 statement, the value will be stored back in *EXPR_P. This value will
11343 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11344 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11345 emitted in PRE_P and POST_P.
11347 Additionally, this process may overwrite parts of the input
11348 expression during gimplification. Ideally, it should be
11349 possible to do non-destructive gimplification.
11351 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11352 the expression needs to evaluate to a value to be used as
11353 an operand in a GIMPLE statement, this value will be stored in
11354 *EXPR_P on exit. This happens when the caller specifies one
11355 of fb_lvalue or fb_rvalue fallback flags.
11357 PRE_P will contain the sequence of GIMPLE statements corresponding
11358 to the evaluation of EXPR and all the side-effects that must
11359 be executed before the main expression. On exit, the last
11360 statement of PRE_P is the core statement being gimplified. For
11361 instance, when gimplifying 'if (++a)' the last statement in
11362 PRE_P will be 'if (t.1)' where t.1 is the result of
11363 pre-incrementing 'a'.
11365 POST_P will contain the sequence of GIMPLE statements corresponding
11366 to the evaluation of all the side-effects that must be executed
11367 after the main expression. If this is NULL, the post
11368 side-effects are stored at the end of PRE_P.
11370 The reason why the output is split in two is to handle post
11371 side-effects explicitly. In some cases, an expression may have
11372 inner and outer post side-effects which need to be emitted in
11373 an order different from the one given by the recursive
11374 traversal. For instance, for the expression (*p--)++ the post
11375 side-effects of '--' must actually occur *after* the post
11376 side-effects of '++'. However, gimplification will first visit
11377 the inner expression, so if a separate POST sequence was not
11378 used, the resulting sequence would be:
11380 1 t.1 = *p
11381 2 p = p - 1
11382 3 t.2 = t.1 + 1
11383 4 *p = t.2
11385 However, the post-decrement operation in line #2 must not be
11386 evaluated until after the store to *p at line #4, so the
11387 correct sequence should be:
11389 1 t.1 = *p
11390 2 t.2 = t.1 + 1
11391 3 *p = t.2
11392 4 p = p - 1
11394 So, by specifying a separate post queue, it is possible
11395 to emit the post side-effects in the correct order.
11396 If POST_P is NULL, an internal queue will be used. Before
11397 returning to the caller, the sequence POST_P is appended to
11398 the main output sequence PRE_P.
11400 GIMPLE_TEST_F points to a function that takes a tree T and
11401 returns nonzero if T is in the GIMPLE form requested by the
11402 caller. The GIMPLE predicates are in gimple.c.
11404 FALLBACK tells the function what sort of a temporary we want if
11405 gimplification cannot produce an expression that complies with
11406 GIMPLE_TEST_F.
11408 fb_none means that no temporary should be generated
11409 fb_rvalue means that an rvalue is OK to generate
11410 fb_lvalue means that an lvalue is OK to generate
11411 fb_either means that either is OK, but an lvalue is preferable.
11412 fb_mayfail means that gimplification may fail (in which case
11413 GS_ERROR will be returned)
11415 The return value is either GS_ERROR or GS_ALL_DONE, since this
11416 function iterates until EXPR is completely gimplified or an error
11417 occurs. */
11419 enum gimplify_status
11420 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11421 bool (*gimple_test_f) (tree), fallback_t fallback)
11423 tree tmp;
11424 gimple_seq internal_pre = NULL;
11425 gimple_seq internal_post = NULL;
11426 tree save_expr;
11427 bool is_statement;
11428 location_t saved_location;
11429 enum gimplify_status ret;
11430 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11431 tree label;
11433 save_expr = *expr_p;
11434 if (save_expr == NULL_TREE)
11435 return GS_ALL_DONE;
11437 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11438 is_statement = gimple_test_f == is_gimple_stmt;
11439 if (is_statement)
11440 gcc_assert (pre_p);
11442 /* Consistency checks. */
11443 if (gimple_test_f == is_gimple_reg)
11444 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11445 else if (gimple_test_f == is_gimple_val
11446 || gimple_test_f == is_gimple_call_addr
11447 || gimple_test_f == is_gimple_condexpr
11448 || gimple_test_f == is_gimple_mem_rhs
11449 || gimple_test_f == is_gimple_mem_rhs_or_call
11450 || gimple_test_f == is_gimple_reg_rhs
11451 || gimple_test_f == is_gimple_reg_rhs_or_call
11452 || gimple_test_f == is_gimple_asm_val
11453 || gimple_test_f == is_gimple_mem_ref_addr)
11454 gcc_assert (fallback & fb_rvalue);
11455 else if (gimple_test_f == is_gimple_min_lval
11456 || gimple_test_f == is_gimple_lvalue)
11457 gcc_assert (fallback & fb_lvalue);
11458 else if (gimple_test_f == is_gimple_addressable)
11459 gcc_assert (fallback & fb_either);
11460 else if (gimple_test_f == is_gimple_stmt)
11461 gcc_assert (fallback == fb_none);
11462 else
11464 /* We should have recognized the GIMPLE_TEST_F predicate to
11465 know what kind of fallback to use in case a temporary is
11466 needed to hold the value or address of *EXPR_P. */
11467 gcc_unreachable ();
11470 /* We used to check the predicate here and return immediately if it
11471 succeeds. This is wrong; the design is for gimplification to be
11472 idempotent, and for the predicates to only test for valid forms, not
11473 whether they are fully simplified. */
11474 if (pre_p == NULL)
11475 pre_p = &internal_pre;
11477 if (post_p == NULL)
11478 post_p = &internal_post;
11480 /* Remember the last statements added to PRE_P and POST_P. Every
11481 new statement added by the gimplification helpers needs to be
11482 annotated with location information. To centralize the
11483 responsibility, we remember the last statement that had been
11484 added to both queues before gimplifying *EXPR_P. If
11485 gimplification produces new statements in PRE_P and POST_P, those
11486 statements will be annotated with the same location information
11487 as *EXPR_P. */
11488 pre_last_gsi = gsi_last (*pre_p);
11489 post_last_gsi = gsi_last (*post_p);
11491 saved_location = input_location;
11492 if (save_expr != error_mark_node
11493 && EXPR_HAS_LOCATION (*expr_p))
11494 input_location = EXPR_LOCATION (*expr_p);
11496 /* Loop over the specific gimplifiers until the toplevel node
11497 remains the same. */
11500 /* Strip away as many useless type conversions as possible
11501 at the toplevel. */
11502 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11504 /* Remember the expr. */
11505 save_expr = *expr_p;
11507 /* Die, die, die, my darling. */
11508 if (error_operand_p (save_expr))
11510 ret = GS_ERROR;
11511 break;
11514 /* Do any language-specific gimplification. */
11515 ret = ((enum gimplify_status)
11516 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11517 if (ret == GS_OK)
11519 if (*expr_p == NULL_TREE)
11520 break;
11521 if (*expr_p != save_expr)
11522 continue;
11524 else if (ret != GS_UNHANDLED)
11525 break;
11527 /* Make sure that all the cases set 'ret' appropriately. */
11528 ret = GS_UNHANDLED;
11529 switch (TREE_CODE (*expr_p))
11531 /* First deal with the special cases. */
11533 case POSTINCREMENT_EXPR:
11534 case POSTDECREMENT_EXPR:
11535 case PREINCREMENT_EXPR:
11536 case PREDECREMENT_EXPR:
11537 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11538 fallback != fb_none,
11539 TREE_TYPE (*expr_p));
11540 break;
11542 case VIEW_CONVERT_EXPR:
11543 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11544 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11546 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11547 post_p, is_gimple_val, fb_rvalue);
11548 recalculate_side_effects (*expr_p);
11549 break;
11551 /* Fallthru. */
11553 case ARRAY_REF:
11554 case ARRAY_RANGE_REF:
11555 case REALPART_EXPR:
11556 case IMAGPART_EXPR:
11557 case COMPONENT_REF:
11558 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11559 fallback ? fallback : fb_rvalue);
11560 break;
11562 case COND_EXPR:
11563 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11565 /* C99 code may assign to an array in a structure value of a
11566 conditional expression, and this has undefined behavior
11567 only on execution, so create a temporary if an lvalue is
11568 required. */
11569 if (fallback == fb_lvalue)
11571 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11572 mark_addressable (*expr_p);
11573 ret = GS_OK;
11575 break;
11577 case CALL_EXPR:
11578 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11580 /* C99 code may assign to an array in a structure returned
11581 from a function, and this has undefined behavior only on
11582 execution, so create a temporary if an lvalue is
11583 required. */
11584 if (fallback == fb_lvalue)
11586 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11587 mark_addressable (*expr_p);
11588 ret = GS_OK;
11590 break;
11592 case TREE_LIST:
11593 gcc_unreachable ();
11595 case COMPOUND_EXPR:
11596 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11597 break;
11599 case COMPOUND_LITERAL_EXPR:
11600 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11601 gimple_test_f, fallback);
11602 break;
11604 case MODIFY_EXPR:
11605 case INIT_EXPR:
11606 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11607 fallback != fb_none);
11608 break;
11610 case TRUTH_ANDIF_EXPR:
11611 case TRUTH_ORIF_EXPR:
11613 /* Preserve the original type of the expression and the
11614 source location of the outer expression. */
11615 tree org_type = TREE_TYPE (*expr_p);
11616 *expr_p = gimple_boolify (*expr_p);
11617 *expr_p = build3_loc (input_location, COND_EXPR,
11618 org_type, *expr_p,
11619 fold_convert_loc
11620 (input_location,
11621 org_type, boolean_true_node),
11622 fold_convert_loc
11623 (input_location,
11624 org_type, boolean_false_node));
11625 ret = GS_OK;
11626 break;
11629 case TRUTH_NOT_EXPR:
11631 tree type = TREE_TYPE (*expr_p);
11632 /* The parsers are careful to generate TRUTH_NOT_EXPR
11633 only with operands that are always zero or one.
11634 We do not fold here but handle the only interesting case
11635 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11636 *expr_p = gimple_boolify (*expr_p);
11637 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11638 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11639 TREE_TYPE (*expr_p),
11640 TREE_OPERAND (*expr_p, 0));
11641 else
11642 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11643 TREE_TYPE (*expr_p),
11644 TREE_OPERAND (*expr_p, 0),
11645 build_int_cst (TREE_TYPE (*expr_p), 1));
11646 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11647 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11648 ret = GS_OK;
11649 break;
11652 case ADDR_EXPR:
11653 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11654 break;
11656 case ANNOTATE_EXPR:
11658 tree cond = TREE_OPERAND (*expr_p, 0);
11659 tree kind = TREE_OPERAND (*expr_p, 1);
11660 tree data = TREE_OPERAND (*expr_p, 2);
11661 tree type = TREE_TYPE (cond);
11662 if (!INTEGRAL_TYPE_P (type))
11664 *expr_p = cond;
11665 ret = GS_OK;
11666 break;
11668 tree tmp = create_tmp_var (type);
11669 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11670 gcall *call
11671 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11672 gimple_call_set_lhs (call, tmp);
11673 gimplify_seq_add_stmt (pre_p, call);
11674 *expr_p = tmp;
11675 ret = GS_ALL_DONE;
11676 break;
11679 case VA_ARG_EXPR:
11680 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11681 break;
11683 CASE_CONVERT:
11684 if (IS_EMPTY_STMT (*expr_p))
11686 ret = GS_ALL_DONE;
11687 break;
11690 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11691 || fallback == fb_none)
11693 /* Just strip a conversion to void (or in void context) and
11694 try again. */
11695 *expr_p = TREE_OPERAND (*expr_p, 0);
11696 ret = GS_OK;
11697 break;
11700 ret = gimplify_conversion (expr_p);
11701 if (ret == GS_ERROR)
11702 break;
11703 if (*expr_p != save_expr)
11704 break;
11705 /* FALLTHRU */
11707 case FIX_TRUNC_EXPR:
11708 /* unary_expr: ... | '(' cast ')' val | ... */
11709 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11710 is_gimple_val, fb_rvalue);
11711 recalculate_side_effects (*expr_p);
11712 break;
11714 case INDIRECT_REF:
11716 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11717 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11718 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11720 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11721 if (*expr_p != save_expr)
11723 ret = GS_OK;
11724 break;
11727 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11728 is_gimple_reg, fb_rvalue);
11729 if (ret == GS_ERROR)
11730 break;
11732 recalculate_side_effects (*expr_p);
11733 *expr_p = fold_build2_loc (input_location, MEM_REF,
11734 TREE_TYPE (*expr_p),
11735 TREE_OPERAND (*expr_p, 0),
11736 build_int_cst (saved_ptr_type, 0));
11737 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11738 TREE_THIS_NOTRAP (*expr_p) = notrap;
11739 ret = GS_OK;
11740 break;
11743 /* We arrive here through the various re-gimplifcation paths. */
11744 case MEM_REF:
11745 /* First try re-folding the whole thing. */
11746 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11747 TREE_OPERAND (*expr_p, 0),
11748 TREE_OPERAND (*expr_p, 1));
11749 if (tmp)
11751 REF_REVERSE_STORAGE_ORDER (tmp)
11752 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11753 *expr_p = tmp;
11754 recalculate_side_effects (*expr_p);
11755 ret = GS_OK;
11756 break;
11758 /* Avoid re-gimplifying the address operand if it is already
11759 in suitable form. Re-gimplifying would mark the address
11760 operand addressable. Always gimplify when not in SSA form
11761 as we still may have to gimplify decls with value-exprs. */
11762 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11763 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11765 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11766 is_gimple_mem_ref_addr, fb_rvalue);
11767 if (ret == GS_ERROR)
11768 break;
11770 recalculate_side_effects (*expr_p);
11771 ret = GS_ALL_DONE;
11772 break;
11774 /* Constants need not be gimplified. */
11775 case INTEGER_CST:
11776 case REAL_CST:
11777 case FIXED_CST:
11778 case STRING_CST:
11779 case COMPLEX_CST:
11780 case VECTOR_CST:
11781 /* Drop the overflow flag on constants, we do not want
11782 that in the GIMPLE IL. */
11783 if (TREE_OVERFLOW_P (*expr_p))
11784 *expr_p = drop_tree_overflow (*expr_p);
11785 ret = GS_ALL_DONE;
11786 break;
11788 case CONST_DECL:
11789 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11790 CONST_DECL node. Otherwise the decl is replaceable by its
11791 value. */
11792 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11793 if (fallback & fb_lvalue)
11794 ret = GS_ALL_DONE;
11795 else
11797 *expr_p = DECL_INITIAL (*expr_p);
11798 ret = GS_OK;
11800 break;
11802 case DECL_EXPR:
11803 ret = gimplify_decl_expr (expr_p, pre_p);
11804 break;
11806 case BIND_EXPR:
11807 ret = gimplify_bind_expr (expr_p, pre_p);
11808 break;
11810 case LOOP_EXPR:
11811 ret = gimplify_loop_expr (expr_p, pre_p);
11812 break;
11814 case SWITCH_EXPR:
11815 ret = gimplify_switch_expr (expr_p, pre_p);
11816 break;
11818 case EXIT_EXPR:
11819 ret = gimplify_exit_expr (expr_p);
11820 break;
11822 case GOTO_EXPR:
11823 /* If the target is not LABEL, then it is a computed jump
11824 and the target needs to be gimplified. */
11825 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11827 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11828 NULL, is_gimple_val, fb_rvalue);
11829 if (ret == GS_ERROR)
11830 break;
11832 gimplify_seq_add_stmt (pre_p,
11833 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11834 ret = GS_ALL_DONE;
11835 break;
11837 case PREDICT_EXPR:
11838 gimplify_seq_add_stmt (pre_p,
11839 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11840 PREDICT_EXPR_OUTCOME (*expr_p)));
11841 ret = GS_ALL_DONE;
11842 break;
11844 case LABEL_EXPR:
11845 ret = gimplify_label_expr (expr_p, pre_p);
11846 label = LABEL_EXPR_LABEL (*expr_p);
11847 gcc_assert (decl_function_context (label) == current_function_decl);
11849 /* If the label is used in a goto statement, or address of the label
11850 is taken, we need to unpoison all variables that were seen so far.
11851 Doing so would prevent us from reporting a false positives. */
11852 if (asan_poisoned_variables
11853 && asan_used_labels != NULL
11854 && asan_used_labels->contains (label))
11855 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11856 break;
11858 case CASE_LABEL_EXPR:
11859 ret = gimplify_case_label_expr (expr_p, pre_p);
11861 if (gimplify_ctxp->live_switch_vars)
11862 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11863 pre_p);
11864 break;
11866 case RETURN_EXPR:
11867 ret = gimplify_return_expr (*expr_p, pre_p);
11868 break;
11870 case CONSTRUCTOR:
11871 /* Don't reduce this in place; let gimplify_init_constructor work its
11872 magic. Buf if we're just elaborating this for side effects, just
11873 gimplify any element that has side-effects. */
11874 if (fallback == fb_none)
11876 unsigned HOST_WIDE_INT ix;
11877 tree val;
11878 tree temp = NULL_TREE;
11879 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11880 if (TREE_SIDE_EFFECTS (val))
11881 append_to_statement_list (val, &temp);
11883 *expr_p = temp;
11884 ret = temp ? GS_OK : GS_ALL_DONE;
11886 /* C99 code may assign to an array in a constructed
11887 structure or union, and this has undefined behavior only
11888 on execution, so create a temporary if an lvalue is
11889 required. */
11890 else if (fallback == fb_lvalue)
11892 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11893 mark_addressable (*expr_p);
11894 ret = GS_OK;
11896 else
11897 ret = GS_ALL_DONE;
11898 break;
11900 /* The following are special cases that are not handled by the
11901 original GIMPLE grammar. */
11903 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11904 eliminated. */
11905 case SAVE_EXPR:
11906 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11907 break;
11909 case BIT_FIELD_REF:
11910 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11911 post_p, is_gimple_lvalue, fb_either);
11912 recalculate_side_effects (*expr_p);
11913 break;
11915 case TARGET_MEM_REF:
11917 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11919 if (TMR_BASE (*expr_p))
11920 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11921 post_p, is_gimple_mem_ref_addr, fb_either);
11922 if (TMR_INDEX (*expr_p))
11923 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11924 post_p, is_gimple_val, fb_rvalue);
11925 if (TMR_INDEX2 (*expr_p))
11926 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11927 post_p, is_gimple_val, fb_rvalue);
11928 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11929 ret = MIN (r0, r1);
11931 break;
11933 case NON_LVALUE_EXPR:
11934 /* This should have been stripped above. */
11935 gcc_unreachable ();
11937 case ASM_EXPR:
11938 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11939 break;
11941 case TRY_FINALLY_EXPR:
11942 case TRY_CATCH_EXPR:
11944 gimple_seq eval, cleanup;
11945 gtry *try_;
11947 /* Calls to destructors are generated automatically in FINALLY/CATCH
11948 block. They should have location as UNKNOWN_LOCATION. However,
11949 gimplify_call_expr will reset these call stmts to input_location
11950 if it finds stmt's location is unknown. To prevent resetting for
11951 destructors, we set the input_location to unknown.
11952 Note that this only affects the destructor calls in FINALLY/CATCH
11953 block, and will automatically reset to its original value by the
11954 end of gimplify_expr. */
11955 input_location = UNKNOWN_LOCATION;
11956 eval = cleanup = NULL;
11957 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11958 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11959 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11960 if (gimple_seq_empty_p (cleanup))
11962 gimple_seq_add_seq (pre_p, eval);
11963 ret = GS_ALL_DONE;
11964 break;
11966 try_ = gimple_build_try (eval, cleanup,
11967 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11968 ? GIMPLE_TRY_FINALLY
11969 : GIMPLE_TRY_CATCH);
11970 if (EXPR_HAS_LOCATION (save_expr))
11971 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11972 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11973 gimple_set_location (try_, saved_location);
11974 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11975 gimple_try_set_catch_is_cleanup (try_,
11976 TRY_CATCH_IS_CLEANUP (*expr_p));
11977 gimplify_seq_add_stmt (pre_p, try_);
11978 ret = GS_ALL_DONE;
11979 break;
11982 case CLEANUP_POINT_EXPR:
11983 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11984 break;
11986 case TARGET_EXPR:
11987 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11988 break;
11990 case CATCH_EXPR:
11992 gimple *c;
11993 gimple_seq handler = NULL;
11994 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11995 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11996 gimplify_seq_add_stmt (pre_p, c);
11997 ret = GS_ALL_DONE;
11998 break;
12001 case EH_FILTER_EXPR:
12003 gimple *ehf;
12004 gimple_seq failure = NULL;
12006 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
12007 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
12008 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
12009 gimplify_seq_add_stmt (pre_p, ehf);
12010 ret = GS_ALL_DONE;
12011 break;
12014 case OBJ_TYPE_REF:
12016 enum gimplify_status r0, r1;
12017 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
12018 post_p, is_gimple_val, fb_rvalue);
12019 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
12020 post_p, is_gimple_val, fb_rvalue);
12021 TREE_SIDE_EFFECTS (*expr_p) = 0;
12022 ret = MIN (r0, r1);
12024 break;
12026 case LABEL_DECL:
12027 /* We get here when taking the address of a label. We mark
12028 the label as "forced"; meaning it can never be removed and
12029 it is a potential target for any computed goto. */
12030 FORCED_LABEL (*expr_p) = 1;
12031 ret = GS_ALL_DONE;
12032 break;
12034 case STATEMENT_LIST:
12035 ret = gimplify_statement_list (expr_p, pre_p);
12036 break;
12038 case WITH_SIZE_EXPR:
12040 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12041 post_p == &internal_post ? NULL : post_p,
12042 gimple_test_f, fallback);
12043 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12044 is_gimple_val, fb_rvalue);
12045 ret = GS_ALL_DONE;
12047 break;
12049 case VAR_DECL:
12050 case PARM_DECL:
12051 ret = gimplify_var_or_parm_decl (expr_p);
12052 break;
12054 case RESULT_DECL:
12055 /* When within an OMP context, notice uses of variables. */
12056 if (gimplify_omp_ctxp)
12057 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
12058 ret = GS_ALL_DONE;
12059 break;
12061 case DEBUG_EXPR_DECL:
12062 gcc_unreachable ();
12064 case DEBUG_BEGIN_STMT:
12065 gimplify_seq_add_stmt (pre_p,
12066 gimple_build_debug_begin_stmt
12067 (TREE_BLOCK (*expr_p),
12068 EXPR_LOCATION (*expr_p)));
12069 ret = GS_ALL_DONE;
12070 *expr_p = NULL;
12071 break;
12073 case SSA_NAME:
12074 /* Allow callbacks into the gimplifier during optimization. */
12075 ret = GS_ALL_DONE;
12076 break;
12078 case OMP_PARALLEL:
12079 gimplify_omp_parallel (expr_p, pre_p);
12080 ret = GS_ALL_DONE;
12081 break;
12083 case OMP_TASK:
12084 gimplify_omp_task (expr_p, pre_p);
12085 ret = GS_ALL_DONE;
12086 break;
12088 case OMP_FOR:
12089 case OMP_SIMD:
12090 case OMP_DISTRIBUTE:
12091 case OMP_TASKLOOP:
12092 case OACC_LOOP:
12093 ret = gimplify_omp_for (expr_p, pre_p);
12094 break;
12096 case OACC_CACHE:
12097 gimplify_oacc_cache (expr_p, pre_p);
12098 ret = GS_ALL_DONE;
12099 break;
12101 case OACC_DECLARE:
12102 gimplify_oacc_declare (expr_p, pre_p);
12103 ret = GS_ALL_DONE;
12104 break;
12106 case OACC_HOST_DATA:
12107 case OACC_DATA:
12108 case OACC_KERNELS:
12109 case OACC_PARALLEL:
12110 case OMP_SECTIONS:
12111 case OMP_SINGLE:
12112 case OMP_TARGET:
12113 case OMP_TARGET_DATA:
12114 case OMP_TEAMS:
12115 gimplify_omp_workshare (expr_p, pre_p);
12116 ret = GS_ALL_DONE;
12117 break;
12119 case OACC_ENTER_DATA:
12120 case OACC_EXIT_DATA:
12121 case OACC_UPDATE:
12122 case OMP_TARGET_UPDATE:
12123 case OMP_TARGET_ENTER_DATA:
12124 case OMP_TARGET_EXIT_DATA:
12125 gimplify_omp_target_update (expr_p, pre_p);
12126 ret = GS_ALL_DONE;
12127 break;
12129 case OMP_SECTION:
12130 case OMP_MASTER:
12131 case OMP_TASKGROUP:
12132 case OMP_ORDERED:
12133 case OMP_CRITICAL:
12135 gimple_seq body = NULL;
12136 gimple *g;
12138 gimplify_and_add (OMP_BODY (*expr_p), &body);
12139 switch (TREE_CODE (*expr_p))
12141 case OMP_SECTION:
12142 g = gimple_build_omp_section (body);
12143 break;
12144 case OMP_MASTER:
12145 g = gimple_build_omp_master (body);
12146 break;
12147 case OMP_TASKGROUP:
12149 gimple_seq cleanup = NULL;
12150 tree fn
12151 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12152 g = gimple_build_call (fn, 0);
12153 gimple_seq_add_stmt (&cleanup, g);
12154 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12155 body = NULL;
12156 gimple_seq_add_stmt (&body, g);
12157 g = gimple_build_omp_taskgroup (body);
12159 break;
12160 case OMP_ORDERED:
12161 g = gimplify_omp_ordered (*expr_p, body);
12162 break;
12163 case OMP_CRITICAL:
12164 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12165 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12166 gimplify_adjust_omp_clauses (pre_p, body,
12167 &OMP_CRITICAL_CLAUSES (*expr_p),
12168 OMP_CRITICAL);
12169 g = gimple_build_omp_critical (body,
12170 OMP_CRITICAL_NAME (*expr_p),
12171 OMP_CRITICAL_CLAUSES (*expr_p));
12172 break;
12173 default:
12174 gcc_unreachable ();
12176 gimplify_seq_add_stmt (pre_p, g);
12177 ret = GS_ALL_DONE;
12178 break;
12181 case OMP_ATOMIC:
12182 case OMP_ATOMIC_READ:
12183 case OMP_ATOMIC_CAPTURE_OLD:
12184 case OMP_ATOMIC_CAPTURE_NEW:
12185 ret = gimplify_omp_atomic (expr_p, pre_p);
12186 break;
12188 case TRANSACTION_EXPR:
12189 ret = gimplify_transaction (expr_p, pre_p);
12190 break;
12192 case TRUTH_AND_EXPR:
12193 case TRUTH_OR_EXPR:
12194 case TRUTH_XOR_EXPR:
12196 tree orig_type = TREE_TYPE (*expr_p);
12197 tree new_type, xop0, xop1;
12198 *expr_p = gimple_boolify (*expr_p);
12199 new_type = TREE_TYPE (*expr_p);
12200 if (!useless_type_conversion_p (orig_type, new_type))
12202 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12203 ret = GS_OK;
12204 break;
12207 /* Boolified binary truth expressions are semantically equivalent
12208 to bitwise binary expressions. Canonicalize them to the
12209 bitwise variant. */
12210 switch (TREE_CODE (*expr_p))
12212 case TRUTH_AND_EXPR:
12213 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12214 break;
12215 case TRUTH_OR_EXPR:
12216 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12217 break;
12218 case TRUTH_XOR_EXPR:
12219 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12220 break;
12221 default:
12222 break;
12224 /* Now make sure that operands have compatible type to
12225 expression's new_type. */
12226 xop0 = TREE_OPERAND (*expr_p, 0);
12227 xop1 = TREE_OPERAND (*expr_p, 1);
12228 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12229 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12230 new_type,
12231 xop0);
12232 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12233 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12234 new_type,
12235 xop1);
12236 /* Continue classified as tcc_binary. */
12237 goto expr_2;
12240 case VEC_COND_EXPR:
12242 enum gimplify_status r0, r1, r2;
12244 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12245 post_p, is_gimple_condexpr, fb_rvalue);
12246 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12247 post_p, is_gimple_val, fb_rvalue);
12248 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12249 post_p, is_gimple_val, fb_rvalue);
12251 ret = MIN (MIN (r0, r1), r2);
12252 recalculate_side_effects (*expr_p);
12254 break;
12256 case VEC_PERM_EXPR:
12257 /* Classified as tcc_expression. */
12258 goto expr_3;
12260 case BIT_INSERT_EXPR:
12261 /* Argument 3 is a constant. */
12262 goto expr_2;
12264 case POINTER_PLUS_EXPR:
12266 enum gimplify_status r0, r1;
12267 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12268 post_p, is_gimple_val, fb_rvalue);
12269 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12270 post_p, is_gimple_val, fb_rvalue);
12271 recalculate_side_effects (*expr_p);
12272 ret = MIN (r0, r1);
12273 break;
12276 default:
12277 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12279 case tcc_comparison:
12280 /* Handle comparison of objects of non scalar mode aggregates
12281 with a call to memcmp. It would be nice to only have to do
12282 this for variable-sized objects, but then we'd have to allow
12283 the same nest of reference nodes we allow for MODIFY_EXPR and
12284 that's too complex.
12286 Compare scalar mode aggregates as scalar mode values. Using
12287 memcmp for them would be very inefficient at best, and is
12288 plain wrong if bitfields are involved. */
12290 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12292 /* Vector comparisons need no boolification. */
12293 if (TREE_CODE (type) == VECTOR_TYPE)
12294 goto expr_2;
12295 else if (!AGGREGATE_TYPE_P (type))
12297 tree org_type = TREE_TYPE (*expr_p);
12298 *expr_p = gimple_boolify (*expr_p);
12299 if (!useless_type_conversion_p (org_type,
12300 TREE_TYPE (*expr_p)))
12302 *expr_p = fold_convert_loc (input_location,
12303 org_type, *expr_p);
12304 ret = GS_OK;
12306 else
12307 goto expr_2;
12309 else if (TYPE_MODE (type) != BLKmode)
12310 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12311 else
12312 ret = gimplify_variable_sized_compare (expr_p);
12314 break;
12317 /* If *EXPR_P does not need to be special-cased, handle it
12318 according to its class. */
12319 case tcc_unary:
12320 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12321 post_p, is_gimple_val, fb_rvalue);
12322 break;
12324 case tcc_binary:
12325 expr_2:
12327 enum gimplify_status r0, r1;
12329 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12330 post_p, is_gimple_val, fb_rvalue);
12331 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12332 post_p, is_gimple_val, fb_rvalue);
12334 ret = MIN (r0, r1);
12335 break;
12338 expr_3:
12340 enum gimplify_status r0, r1, r2;
12342 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12343 post_p, is_gimple_val, fb_rvalue);
12344 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12345 post_p, is_gimple_val, fb_rvalue);
12346 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12347 post_p, is_gimple_val, fb_rvalue);
12349 ret = MIN (MIN (r0, r1), r2);
12350 break;
12353 case tcc_declaration:
12354 case tcc_constant:
12355 ret = GS_ALL_DONE;
12356 goto dont_recalculate;
12358 default:
12359 gcc_unreachable ();
12362 recalculate_side_effects (*expr_p);
12364 dont_recalculate:
12365 break;
12368 gcc_assert (*expr_p || ret != GS_OK);
12370 while (ret == GS_OK);
12372 /* If we encountered an error_mark somewhere nested inside, either
12373 stub out the statement or propagate the error back out. */
12374 if (ret == GS_ERROR)
12376 if (is_statement)
12377 *expr_p = NULL;
12378 goto out;
12381 /* This was only valid as a return value from the langhook, which
12382 we handled. Make sure it doesn't escape from any other context. */
12383 gcc_assert (ret != GS_UNHANDLED);
12385 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12387 /* We aren't looking for a value, and we don't have a valid
12388 statement. If it doesn't have side-effects, throw it away.
12389 We can also get here with code such as "*&&L;", where L is
12390 a LABEL_DECL that is marked as FORCED_LABEL. */
12391 if (TREE_CODE (*expr_p) == LABEL_DECL
12392 || !TREE_SIDE_EFFECTS (*expr_p))
12393 *expr_p = NULL;
12394 else if (!TREE_THIS_VOLATILE (*expr_p))
12396 /* This is probably a _REF that contains something nested that
12397 has side effects. Recurse through the operands to find it. */
12398 enum tree_code code = TREE_CODE (*expr_p);
12400 switch (code)
12402 case COMPONENT_REF:
12403 case REALPART_EXPR:
12404 case IMAGPART_EXPR:
12405 case VIEW_CONVERT_EXPR:
12406 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12407 gimple_test_f, fallback);
12408 break;
12410 case ARRAY_REF:
12411 case ARRAY_RANGE_REF:
12412 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12413 gimple_test_f, fallback);
12414 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12415 gimple_test_f, fallback);
12416 break;
12418 default:
12419 /* Anything else with side-effects must be converted to
12420 a valid statement before we get here. */
12421 gcc_unreachable ();
12424 *expr_p = NULL;
12426 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12427 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12429 /* Historically, the compiler has treated a bare reference
12430 to a non-BLKmode volatile lvalue as forcing a load. */
12431 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12433 /* Normally, we do not want to create a temporary for a
12434 TREE_ADDRESSABLE type because such a type should not be
12435 copied by bitwise-assignment. However, we make an
12436 exception here, as all we are doing here is ensuring that
12437 we read the bytes that make up the type. We use
12438 create_tmp_var_raw because create_tmp_var will abort when
12439 given a TREE_ADDRESSABLE type. */
12440 tree tmp = create_tmp_var_raw (type, "vol");
12441 gimple_add_tmp_var (tmp);
12442 gimplify_assign (tmp, *expr_p, pre_p);
12443 *expr_p = NULL;
12445 else
12446 /* We can't do anything useful with a volatile reference to
12447 an incomplete type, so just throw it away. Likewise for
12448 a BLKmode type, since any implicit inner load should
12449 already have been turned into an explicit one by the
12450 gimplification process. */
12451 *expr_p = NULL;
12454 /* If we are gimplifying at the statement level, we're done. Tack
12455 everything together and return. */
12456 if (fallback == fb_none || is_statement)
12458 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12459 it out for GC to reclaim it. */
12460 *expr_p = NULL_TREE;
12462 if (!gimple_seq_empty_p (internal_pre)
12463 || !gimple_seq_empty_p (internal_post))
12465 gimplify_seq_add_seq (&internal_pre, internal_post);
12466 gimplify_seq_add_seq (pre_p, internal_pre);
12469 /* The result of gimplifying *EXPR_P is going to be the last few
12470 statements in *PRE_P and *POST_P. Add location information
12471 to all the statements that were added by the gimplification
12472 helpers. */
12473 if (!gimple_seq_empty_p (*pre_p))
12474 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12476 if (!gimple_seq_empty_p (*post_p))
12477 annotate_all_with_location_after (*post_p, post_last_gsi,
12478 input_location);
12480 goto out;
12483 #ifdef ENABLE_GIMPLE_CHECKING
12484 if (*expr_p)
12486 enum tree_code code = TREE_CODE (*expr_p);
12487 /* These expressions should already be in gimple IR form. */
12488 gcc_assert (code != MODIFY_EXPR
12489 && code != ASM_EXPR
12490 && code != BIND_EXPR
12491 && code != CATCH_EXPR
12492 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12493 && code != EH_FILTER_EXPR
12494 && code != GOTO_EXPR
12495 && code != LABEL_EXPR
12496 && code != LOOP_EXPR
12497 && code != SWITCH_EXPR
12498 && code != TRY_FINALLY_EXPR
12499 && code != OACC_PARALLEL
12500 && code != OACC_KERNELS
12501 && code != OACC_DATA
12502 && code != OACC_HOST_DATA
12503 && code != OACC_DECLARE
12504 && code != OACC_UPDATE
12505 && code != OACC_ENTER_DATA
12506 && code != OACC_EXIT_DATA
12507 && code != OACC_CACHE
12508 && code != OMP_CRITICAL
12509 && code != OMP_FOR
12510 && code != OACC_LOOP
12511 && code != OMP_MASTER
12512 && code != OMP_TASKGROUP
12513 && code != OMP_ORDERED
12514 && code != OMP_PARALLEL
12515 && code != OMP_SECTIONS
12516 && code != OMP_SECTION
12517 && code != OMP_SINGLE);
12519 #endif
12521 /* Otherwise we're gimplifying a subexpression, so the resulting
12522 value is interesting. If it's a valid operand that matches
12523 GIMPLE_TEST_F, we're done. Unless we are handling some
12524 post-effects internally; if that's the case, we need to copy into
12525 a temporary before adding the post-effects to POST_P. */
12526 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12527 goto out;
12529 /* Otherwise, we need to create a new temporary for the gimplified
12530 expression. */
12532 /* We can't return an lvalue if we have an internal postqueue. The
12533 object the lvalue refers to would (probably) be modified by the
12534 postqueue; we need to copy the value out first, which means an
12535 rvalue. */
12536 if ((fallback & fb_lvalue)
12537 && gimple_seq_empty_p (internal_post)
12538 && is_gimple_addressable (*expr_p))
12540 /* An lvalue will do. Take the address of the expression, store it
12541 in a temporary, and replace the expression with an INDIRECT_REF of
12542 that temporary. */
12543 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12544 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12545 *expr_p = build_simple_mem_ref (tmp);
12547 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12549 /* An rvalue will do. Assign the gimplified expression into a
12550 new temporary TMP and replace the original expression with
12551 TMP. First, make sure that the expression has a type so that
12552 it can be assigned into a temporary. */
12553 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12554 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12556 else
12558 #ifdef ENABLE_GIMPLE_CHECKING
12559 if (!(fallback & fb_mayfail))
12561 fprintf (stderr, "gimplification failed:\n");
12562 print_generic_expr (stderr, *expr_p);
12563 debug_tree (*expr_p);
12564 internal_error ("gimplification failed");
12566 #endif
12567 gcc_assert (fallback & fb_mayfail);
12569 /* If this is an asm statement, and the user asked for the
12570 impossible, don't die. Fail and let gimplify_asm_expr
12571 issue an error. */
12572 ret = GS_ERROR;
12573 goto out;
12576 /* Make sure the temporary matches our predicate. */
12577 gcc_assert ((*gimple_test_f) (*expr_p));
12579 if (!gimple_seq_empty_p (internal_post))
12581 annotate_all_with_location (internal_post, input_location);
12582 gimplify_seq_add_seq (pre_p, internal_post);
12585 out:
12586 input_location = saved_location;
12587 return ret;
12590 /* Like gimplify_expr but make sure the gimplified result is not itself
12591 a SSA name (but a decl if it were). Temporaries required by
12592 evaluating *EXPR_P may be still SSA names. */
12594 static enum gimplify_status
12595 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12596 bool (*gimple_test_f) (tree), fallback_t fallback,
12597 bool allow_ssa)
12599 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12600 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12601 gimple_test_f, fallback);
12602 if (! allow_ssa
12603 && TREE_CODE (*expr_p) == SSA_NAME)
12605 tree name = *expr_p;
12606 if (was_ssa_name_p)
12607 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12608 else
12610 /* Avoid the extra copy if possible. */
12611 *expr_p = create_tmp_reg (TREE_TYPE (name));
12612 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12613 release_ssa_name (name);
12616 return ret;
12619 /* Look through TYPE for variable-sized objects and gimplify each such
12620 size that we find. Add to LIST_P any statements generated. */
12622 void
12623 gimplify_type_sizes (tree type, gimple_seq *list_p)
12625 tree field, t;
12627 if (type == NULL || type == error_mark_node)
12628 return;
12630 /* We first do the main variant, then copy into any other variants. */
12631 type = TYPE_MAIN_VARIANT (type);
12633 /* Avoid infinite recursion. */
12634 if (TYPE_SIZES_GIMPLIFIED (type))
12635 return;
12637 TYPE_SIZES_GIMPLIFIED (type) = 1;
12639 switch (TREE_CODE (type))
12641 case INTEGER_TYPE:
12642 case ENUMERAL_TYPE:
12643 case BOOLEAN_TYPE:
12644 case REAL_TYPE:
12645 case FIXED_POINT_TYPE:
12646 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12647 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12649 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12651 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12652 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12654 break;
12656 case ARRAY_TYPE:
12657 /* These types may not have declarations, so handle them here. */
12658 gimplify_type_sizes (TREE_TYPE (type), list_p);
12659 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12660 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12661 with assigned stack slots, for -O1+ -g they should be tracked
12662 by VTA. */
12663 if (!(TYPE_NAME (type)
12664 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12665 && DECL_IGNORED_P (TYPE_NAME (type)))
12666 && TYPE_DOMAIN (type)
12667 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12669 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12670 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12671 DECL_IGNORED_P (t) = 0;
12672 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12673 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12674 DECL_IGNORED_P (t) = 0;
12676 break;
12678 case RECORD_TYPE:
12679 case UNION_TYPE:
12680 case QUAL_UNION_TYPE:
12681 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12682 if (TREE_CODE (field) == FIELD_DECL)
12684 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12685 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12686 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12687 gimplify_type_sizes (TREE_TYPE (field), list_p);
12689 break;
12691 case POINTER_TYPE:
12692 case REFERENCE_TYPE:
12693 /* We used to recurse on the pointed-to type here, which turned out to
12694 be incorrect because its definition might refer to variables not
12695 yet initialized at this point if a forward declaration is involved.
12697 It was actually useful for anonymous pointed-to types to ensure
12698 that the sizes evaluation dominates every possible later use of the
12699 values. Restricting to such types here would be safe since there
12700 is no possible forward declaration around, but would introduce an
12701 undesirable middle-end semantic to anonymity. We then defer to
12702 front-ends the responsibility of ensuring that the sizes are
12703 evaluated both early and late enough, e.g. by attaching artificial
12704 type declarations to the tree. */
12705 break;
12707 default:
12708 break;
12711 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12712 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12714 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12716 TYPE_SIZE (t) = TYPE_SIZE (type);
12717 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12718 TYPE_SIZES_GIMPLIFIED (t) = 1;
12722 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12723 a size or position, has had all of its SAVE_EXPRs evaluated.
12724 We add any required statements to *STMT_P. */
12726 void
12727 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12729 tree expr = *expr_p;
12731 /* We don't do anything if the value isn't there, is constant, or contains
12732 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12733 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12734 will want to replace it with a new variable, but that will cause problems
12735 if this type is from outside the function. It's OK to have that here. */
12736 if (expr == NULL_TREE
12737 || is_gimple_constant (expr)
12738 || TREE_CODE (expr) == VAR_DECL
12739 || CONTAINS_PLACEHOLDER_P (expr))
12740 return;
12742 *expr_p = unshare_expr (expr);
12744 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12745 if the def vanishes. */
12746 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12748 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12749 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12750 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12751 if (is_gimple_constant (*expr_p))
12752 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12755 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12756 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12757 is true, also gimplify the parameters. */
12759 gbind *
12760 gimplify_body (tree fndecl, bool do_parms)
12762 location_t saved_location = input_location;
12763 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12764 gimple *outer_stmt;
12765 gbind *outer_bind;
12767 timevar_push (TV_TREE_GIMPLIFY);
12769 init_tree_ssa (cfun);
12771 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12772 gimplification. */
12773 default_rtl_profile ();
12775 gcc_assert (gimplify_ctxp == NULL);
12776 push_gimplify_context (true);
12778 if (flag_openacc || flag_openmp)
12780 gcc_assert (gimplify_omp_ctxp == NULL);
12781 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12782 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12785 /* Unshare most shared trees in the body and in that of any nested functions.
12786 It would seem we don't have to do this for nested functions because
12787 they are supposed to be output and then the outer function gimplified
12788 first, but the g++ front end doesn't always do it that way. */
12789 unshare_body (fndecl);
12790 unvisit_body (fndecl);
12792 /* Make sure input_location isn't set to something weird. */
12793 input_location = DECL_SOURCE_LOCATION (fndecl);
12795 /* Resolve callee-copies. This has to be done before processing
12796 the body so that DECL_VALUE_EXPR gets processed correctly. */
12797 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12799 /* Gimplify the function's body. */
12800 seq = NULL;
12801 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12802 outer_stmt = gimple_seq_first_stmt (seq);
12803 if (!outer_stmt)
12805 outer_stmt = gimple_build_nop ();
12806 gimplify_seq_add_stmt (&seq, outer_stmt);
12809 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12810 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12811 if (gimple_code (outer_stmt) == GIMPLE_BIND
12812 && gimple_seq_first (seq) == gimple_seq_last (seq))
12813 outer_bind = as_a <gbind *> (outer_stmt);
12814 else
12815 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12817 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12819 /* If we had callee-copies statements, insert them at the beginning
12820 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12821 if (!gimple_seq_empty_p (parm_stmts))
12823 tree parm;
12825 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12826 if (parm_cleanup)
12828 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12829 GIMPLE_TRY_FINALLY);
12830 parm_stmts = NULL;
12831 gimple_seq_add_stmt (&parm_stmts, g);
12833 gimple_bind_set_body (outer_bind, parm_stmts);
12835 for (parm = DECL_ARGUMENTS (current_function_decl);
12836 parm; parm = DECL_CHAIN (parm))
12837 if (DECL_HAS_VALUE_EXPR_P (parm))
12839 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12840 DECL_IGNORED_P (parm) = 0;
12844 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12845 && gimplify_omp_ctxp)
12847 delete_omp_context (gimplify_omp_ctxp);
12848 gimplify_omp_ctxp = NULL;
12851 pop_gimplify_context (outer_bind);
12852 gcc_assert (gimplify_ctxp == NULL);
12854 if (flag_checking && !seen_error ())
12855 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12857 timevar_pop (TV_TREE_GIMPLIFY);
12858 input_location = saved_location;
12860 return outer_bind;
12863 typedef char *char_p; /* For DEF_VEC_P. */
12865 /* Return whether we should exclude FNDECL from instrumentation. */
12867 static bool
12868 flag_instrument_functions_exclude_p (tree fndecl)
12870 vec<char_p> *v;
12872 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12873 if (v && v->length () > 0)
12875 const char *name;
12876 int i;
12877 char *s;
12879 name = lang_hooks.decl_printable_name (fndecl, 0);
12880 FOR_EACH_VEC_ELT (*v, i, s)
12881 if (strstr (name, s) != NULL)
12882 return true;
12885 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12886 if (v && v->length () > 0)
12888 const char *name;
12889 int i;
12890 char *s;
12892 name = DECL_SOURCE_FILE (fndecl);
12893 FOR_EACH_VEC_ELT (*v, i, s)
12894 if (strstr (name, s) != NULL)
12895 return true;
12898 return false;
12901 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12902 node for the function we want to gimplify.
12904 Return the sequence of GIMPLE statements corresponding to the body
12905 of FNDECL. */
12907 void
12908 gimplify_function_tree (tree fndecl)
12910 tree parm, ret;
12911 gimple_seq seq;
12912 gbind *bind;
12914 gcc_assert (!gimple_body (fndecl));
12916 if (DECL_STRUCT_FUNCTION (fndecl))
12917 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12918 else
12919 push_struct_function (fndecl);
12921 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12922 if necessary. */
12923 cfun->curr_properties |= PROP_gimple_lva;
12925 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12927 /* Preliminarily mark non-addressed complex variables as eligible
12928 for promotion to gimple registers. We'll transform their uses
12929 as we find them. */
12930 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12931 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12932 && !TREE_THIS_VOLATILE (parm)
12933 && !needs_to_live_in_memory (parm))
12934 DECL_GIMPLE_REG_P (parm) = 1;
12937 ret = DECL_RESULT (fndecl);
12938 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12939 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12940 && !needs_to_live_in_memory (ret))
12941 DECL_GIMPLE_REG_P (ret) = 1;
12943 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12944 asan_poisoned_variables = new hash_set<tree> ();
12945 bind = gimplify_body (fndecl, true);
12946 if (asan_poisoned_variables)
12948 delete asan_poisoned_variables;
12949 asan_poisoned_variables = NULL;
12952 /* The tree body of the function is no longer needed, replace it
12953 with the new GIMPLE body. */
12954 seq = NULL;
12955 gimple_seq_add_stmt (&seq, bind);
12956 gimple_set_body (fndecl, seq);
12958 /* If we're instrumenting function entry/exit, then prepend the call to
12959 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12960 catch the exit hook. */
12961 /* ??? Add some way to ignore exceptions for this TFE. */
12962 if (flag_instrument_function_entry_exit
12963 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12964 /* Do not instrument extern inline functions. */
12965 && !(DECL_DECLARED_INLINE_P (fndecl)
12966 && DECL_EXTERNAL (fndecl)
12967 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12968 && !flag_instrument_functions_exclude_p (fndecl))
12970 tree x;
12971 gbind *new_bind;
12972 gimple *tf;
12973 gimple_seq cleanup = NULL, body = NULL;
12974 tree tmp_var, this_fn_addr;
12975 gcall *call;
12977 /* The instrumentation hooks aren't going to call the instrumented
12978 function and the address they receive is expected to be matchable
12979 against symbol addresses. Make sure we don't create a trampoline,
12980 in case the current function is nested. */
12981 this_fn_addr = build_fold_addr_expr (current_function_decl);
12982 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
12984 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12985 call = gimple_build_call (x, 1, integer_zero_node);
12986 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12987 gimple_call_set_lhs (call, tmp_var);
12988 gimplify_seq_add_stmt (&cleanup, call);
12989 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12990 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
12991 gimplify_seq_add_stmt (&cleanup, call);
12992 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12994 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12995 call = gimple_build_call (x, 1, integer_zero_node);
12996 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12997 gimple_call_set_lhs (call, tmp_var);
12998 gimplify_seq_add_stmt (&body, call);
12999 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
13000 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13001 gimplify_seq_add_stmt (&body, call);
13002 gimplify_seq_add_stmt (&body, tf);
13003 new_bind = gimple_build_bind (NULL, body, NULL);
13005 /* Replace the current function body with the body
13006 wrapped in the try/finally TF. */
13007 seq = NULL;
13008 gimple_seq_add_stmt (&seq, new_bind);
13009 gimple_set_body (fndecl, seq);
13010 bind = new_bind;
13013 if (sanitize_flags_p (SANITIZE_THREAD))
13015 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
13016 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
13017 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
13018 /* Replace the current function body with the body
13019 wrapped in the try/finally TF. */
13020 seq = NULL;
13021 gimple_seq_add_stmt (&seq, new_bind);
13022 gimple_set_body (fndecl, seq);
13025 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13026 cfun->curr_properties |= PROP_gimple_any;
13028 pop_cfun ();
13030 dump_function (TDI_gimple, fndecl);
13033 /* Return a dummy expression of type TYPE in order to keep going after an
13034 error. */
13036 static tree
13037 dummy_object (tree type)
13039 tree t = build_int_cst (build_pointer_type (type), 0);
13040 return build2 (MEM_REF, type, t, t);
13043 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
13044 builtin function, but a very special sort of operator. */
13046 enum gimplify_status
13047 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
13048 gimple_seq *post_p ATTRIBUTE_UNUSED)
13050 tree promoted_type, have_va_type;
13051 tree valist = TREE_OPERAND (*expr_p, 0);
13052 tree type = TREE_TYPE (*expr_p);
13053 tree t, tag, aptag;
13054 location_t loc = EXPR_LOCATION (*expr_p);
13056 /* Verify that valist is of the proper type. */
13057 have_va_type = TREE_TYPE (valist);
13058 if (have_va_type == error_mark_node)
13059 return GS_ERROR;
13060 have_va_type = targetm.canonical_va_list_type (have_va_type);
13061 if (have_va_type == NULL_TREE
13062 && POINTER_TYPE_P (TREE_TYPE (valist)))
13063 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13064 have_va_type
13065 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13066 gcc_assert (have_va_type != NULL_TREE);
13068 /* Generate a diagnostic for requesting data of a type that cannot
13069 be passed through `...' due to type promotion at the call site. */
13070 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13071 != type)
13073 static bool gave_help;
13074 bool warned;
13075 /* Use the expansion point to handle cases such as passing bool (defined
13076 in a system header) through `...'. */
13077 source_location xloc
13078 = expansion_point_location_if_in_system_header (loc);
13080 /* Unfortunately, this is merely undefined, rather than a constraint
13081 violation, so we cannot make this an error. If this call is never
13082 executed, the program is still strictly conforming. */
13083 auto_diagnostic_group d;
13084 warned = warning_at (xloc, 0,
13085 "%qT is promoted to %qT when passed through %<...%>",
13086 type, promoted_type);
13087 if (!gave_help && warned)
13089 gave_help = true;
13090 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13091 promoted_type, type);
13094 /* We can, however, treat "undefined" any way we please.
13095 Call abort to encourage the user to fix the program. */
13096 if (warned)
13097 inform (xloc, "if this code is reached, the program will abort");
13098 /* Before the abort, allow the evaluation of the va_list
13099 expression to exit or longjmp. */
13100 gimplify_and_add (valist, pre_p);
13101 t = build_call_expr_loc (loc,
13102 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13103 gimplify_and_add (t, pre_p);
13105 /* This is dead code, but go ahead and finish so that the
13106 mode of the result comes out right. */
13107 *expr_p = dummy_object (type);
13108 return GS_ALL_DONE;
13111 tag = build_int_cst (build_pointer_type (type), 0);
13112 aptag = build_int_cst (TREE_TYPE (valist), 0);
13114 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13115 valist, tag, aptag);
13117 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13118 needs to be expanded. */
13119 cfun->curr_properties &= ~PROP_gimple_lva;
13121 return GS_OK;
13124 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13126 DST/SRC are the destination and source respectively. You can pass
13127 ungimplified trees in DST or SRC, in which case they will be
13128 converted to a gimple operand if necessary.
13130 This function returns the newly created GIMPLE_ASSIGN tuple. */
13132 gimple *
13133 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13135 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13136 gimplify_and_add (t, seq_p);
13137 ggc_free (t);
13138 return gimple_seq_last_stmt (*seq_p);
13141 inline hashval_t
13142 gimplify_hasher::hash (const elt_t *p)
13144 tree t = p->val;
13145 return iterative_hash_expr (t, 0);
13148 inline bool
13149 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13151 tree t1 = p1->val;
13152 tree t2 = p2->val;
13153 enum tree_code code = TREE_CODE (t1);
13155 if (TREE_CODE (t2) != code
13156 || TREE_TYPE (t1) != TREE_TYPE (t2))
13157 return false;
13159 if (!operand_equal_p (t1, t2, 0))
13160 return false;
13162 /* Only allow them to compare equal if they also hash equal; otherwise
13163 results are nondeterminate, and we fail bootstrap comparison. */
13164 gcc_checking_assert (hash (p1) == hash (p2));
13166 return true;