usr.sbin/makefs/hammer2: Remove redundant hammer2_inode_modify()
[dragonfly.git] / contrib / gcc-8.0 / gcc / gimplify.c
blob12041973d84f7ae4facb18659c656708cfe04e4a
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple.h"
33 #include "gimple-predict.h"
34 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "ssa.h"
36 #include "cgraph.h"
37 #include "tree-pretty-print.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
72 enum gimplify_omp_var_data
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
94 GOVD_MAP_0LEN_ARRAY = 32768,
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO = 65536,
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN = 131072,
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE = 262144,
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT = 524288,
108 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
109 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
110 | GOVD_LOCAL)
114 enum omp_region_type
116 ORT_WORKSHARE = 0x00,
117 ORT_SIMD = 0x01,
119 ORT_PARALLEL = 0x02,
120 ORT_COMBINED_PARALLEL = 0x03,
122 ORT_TASK = 0x04,
123 ORT_UNTIED_TASK = 0x05,
125 ORT_TEAMS = 0x08,
126 ORT_COMBINED_TEAMS = 0x09,
128 /* Data region. */
129 ORT_TARGET_DATA = 0x10,
131 /* Data region with offloading. */
132 ORT_TARGET = 0x20,
133 ORT_COMBINED_TARGET = 0x21,
135 /* OpenACC variants. */
136 ORT_ACC = 0x40, /* A generic OpenACC region. */
137 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
138 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
139 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
140 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
142 /* Dummy OpenMP region, used to disable expansion of
143 DECL_VALUE_EXPRs in taskloop pre body. */
144 ORT_NONE = 0x100
147 /* Gimplify hashtable helper. */
149 struct gimplify_hasher : free_ptr_hash <elt_t>
151 static inline hashval_t hash (const elt_t *);
152 static inline bool equal (const elt_t *, const elt_t *);
155 struct gimplify_ctx
157 struct gimplify_ctx *prev_context;
159 vec<gbind *> bind_expr_stack;
160 tree temps;
161 gimple_seq conditional_cleanups;
162 tree exit_label;
163 tree return_temp;
165 vec<tree> case_labels;
166 hash_set<tree> *live_switch_vars;
167 /* The formal temporary table. Should this be persistent? */
168 hash_table<gimplify_hasher> *temp_htab;
170 int conditions;
171 unsigned into_ssa : 1;
172 unsigned allow_rhs_cond_expr : 1;
173 unsigned in_cleanup_point_expr : 1;
174 unsigned keep_stack : 1;
175 unsigned save_stack : 1;
176 unsigned in_switch_expr : 1;
179 struct gimplify_omp_ctx
181 struct gimplify_omp_ctx *outer_context;
182 splay_tree variables;
183 hash_set<tree> *privatized_types;
184 /* Iteration variables in an OMP_FOR. */
185 vec<tree> loop_iter_var;
186 location_t location;
187 enum omp_clause_default_kind default_kind;
188 enum omp_region_type region_type;
189 bool combined_loop;
190 bool distribute;
191 bool target_map_scalars_firstprivate;
192 bool target_map_pointers_as_0len_arrays;
193 bool target_firstprivatize_array_bases;
196 static struct gimplify_ctx *gimplify_ctxp;
197 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
199 /* Forward declaration. */
200 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
201 static hash_map<tree, tree> *oacc_declare_returns;
202 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
203 bool (*) (tree), fallback_t, bool);
205 /* Shorter alias name for the above function for use in gimplify.c
206 only. */
208 static inline void
209 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
211 gimple_seq_add_stmt_without_update (seq_p, gs);
214 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
215 NULL, a new sequence is allocated. This function is
216 similar to gimple_seq_add_seq, but does not scan the operands.
217 During gimplification, we need to manipulate statement sequences
218 before the def/use vectors have been constructed. */
220 static void
221 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
223 gimple_stmt_iterator si;
225 if (src == NULL)
226 return;
228 si = gsi_last (*dst_p);
229 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234 and popping gimplify contexts. */
236 static struct gimplify_ctx *ctx_pool = NULL;
238 /* Return a gimplify context struct from the pool. */
240 static inline struct gimplify_ctx *
241 ctx_alloc (void)
243 struct gimplify_ctx * c = ctx_pool;
245 if (c)
246 ctx_pool = c->prev_context;
247 else
248 c = XNEW (struct gimplify_ctx);
250 memset (c, '\0', sizeof (*c));
251 return c;
254 /* Put gimplify context C back into the pool. */
256 static inline void
257 ctx_free (struct gimplify_ctx *c)
259 c->prev_context = ctx_pool;
260 ctx_pool = c;
263 /* Free allocated ctx stack memory. */
265 void
266 free_gimplify_stack (void)
268 struct gimplify_ctx *c;
270 while ((c = ctx_pool))
272 ctx_pool = c->prev_context;
273 free (c);
278 /* Set up a context for the gimplifier. */
280 void
281 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
283 struct gimplify_ctx *c = ctx_alloc ();
285 c->prev_context = gimplify_ctxp;
286 gimplify_ctxp = c;
287 gimplify_ctxp->into_ssa = in_ssa;
288 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
291 /* Tear down a context for the gimplifier. If BODY is non-null, then
292 put the temporaries into the outer BIND_EXPR. Otherwise, put them
293 in the local_decls.
295 BODY is not a sequence, but the first tuple in a sequence. */
297 void
298 pop_gimplify_context (gimple *body)
300 struct gimplify_ctx *c = gimplify_ctxp;
302 gcc_assert (c
303 && (!c->bind_expr_stack.exists ()
304 || c->bind_expr_stack.is_empty ()));
305 c->bind_expr_stack.release ();
306 gimplify_ctxp = c->prev_context;
308 if (body)
309 declare_vars (c->temps, body, false);
310 else
311 record_vars (c->temps);
313 delete c->temp_htab;
314 c->temp_htab = NULL;
315 ctx_free (c);
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
320 static void
321 gimple_push_bind_expr (gbind *bind_stmt)
323 gimplify_ctxp->bind_expr_stack.reserve (8);
324 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
327 /* Pop the first element off the stack of bindings. */
329 static void
330 gimple_pop_bind_expr (void)
332 gimplify_ctxp->bind_expr_stack.pop ();
335 /* Return the first element of the stack of bindings. */
337 gbind *
338 gimple_current_bind_expr (void)
340 return gimplify_ctxp->bind_expr_stack.last ();
343 /* Return the stack of bindings created during gimplification. */
345 vec<gbind *>
346 gimple_bind_expr_stack (void)
348 return gimplify_ctxp->bind_expr_stack;
351 /* Return true iff there is a COND_EXPR between us and the innermost
352 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
354 static bool
355 gimple_conditional_context (void)
357 return gimplify_ctxp->conditions > 0;
360 /* Note that we've entered a COND_EXPR. */
362 static void
363 gimple_push_condition (void)
365 #ifdef ENABLE_GIMPLE_CHECKING
366 if (gimplify_ctxp->conditions == 0)
367 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
368 #endif
369 ++(gimplify_ctxp->conditions);
372 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
373 now, add any conditional cleanups we've seen to the prequeue. */
375 static void
376 gimple_pop_condition (gimple_seq *pre_p)
378 int conds = --(gimplify_ctxp->conditions);
380 gcc_assert (conds >= 0);
381 if (conds == 0)
383 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
384 gimplify_ctxp->conditional_cleanups = NULL;
388 /* A stable comparison routine for use with splay trees and DECLs. */
390 static int
391 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
393 tree a = (tree) xa;
394 tree b = (tree) xb;
396 return DECL_UID (a) - DECL_UID (b);
399 /* Create a new omp construct that deals with variable remapping. */
401 static struct gimplify_omp_ctx *
402 new_omp_context (enum omp_region_type region_type)
404 struct gimplify_omp_ctx *c;
406 c = XCNEW (struct gimplify_omp_ctx);
407 c->outer_context = gimplify_omp_ctxp;
408 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
409 c->privatized_types = new hash_set<tree>;
410 c->location = input_location;
411 c->region_type = region_type;
412 if ((region_type & ORT_TASK) == 0)
413 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
414 else
415 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
417 return c;
420 /* Destroy an omp construct that deals with variable remapping. */
422 static void
423 delete_omp_context (struct gimplify_omp_ctx *c)
425 splay_tree_delete (c->variables);
426 delete c->privatized_types;
427 c->loop_iter_var.release ();
428 XDELETE (c);
431 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
434 /* Both gimplify the statement T and append it to *SEQ_P. This function
435 behaves exactly as gimplify_stmt, but you don't have to pass T as a
436 reference. */
438 void
439 gimplify_and_add (tree t, gimple_seq *seq_p)
441 gimplify_stmt (&t, seq_p);
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445 tuple in the sequence of generated tuples for this statement.
446 Return NULL if gimplifying T produced no tuples. */
448 static gimple *
449 gimplify_and_return_first (tree t, gimple_seq *seq_p)
451 gimple_stmt_iterator last = gsi_last (*seq_p);
453 gimplify_and_add (t, seq_p);
455 if (!gsi_end_p (last))
457 gsi_next (&last);
458 return gsi_stmt (last);
460 else
461 return gimple_seq_first_stmt (*seq_p);
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465 LHS, or for a call argument. */
467 static bool
468 is_gimple_mem_rhs (tree t)
470 /* If we're dealing with a renamable type, either source or dest must be
471 a renamed variable. */
472 if (is_gimple_reg_type (TREE_TYPE (t)))
473 return is_gimple_val (t);
474 else
475 return is_gimple_val (t) || is_gimple_lvalue (t);
478 /* Return true if T is a CALL_EXPR or an expression that can be
479 assigned to a temporary. Note that this predicate should only be
480 used during gimplification. See the rationale for this in
481 gimplify_modify_expr. */
483 static bool
484 is_gimple_reg_rhs_or_call (tree t)
486 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
487 || TREE_CODE (t) == CALL_EXPR);
490 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
491 this predicate should only be used during gimplification. See the
492 rationale for this in gimplify_modify_expr. */
494 static bool
495 is_gimple_mem_rhs_or_call (tree t)
497 /* If we're dealing with a renamable type, either source or dest must be
498 a renamed variable. */
499 if (is_gimple_reg_type (TREE_TYPE (t)))
500 return is_gimple_val (t);
501 else
502 return (is_gimple_val (t)
503 || is_gimple_lvalue (t)
504 || TREE_CLOBBER_P (t)
505 || TREE_CODE (t) == CALL_EXPR);
508 /* Create a temporary with a name derived from VAL. Subroutine of
509 lookup_tmp_var; nobody else should call this function. */
511 static inline tree
512 create_tmp_from_val (tree val)
514 /* Drop all qualifiers and address-space information from the value type. */
515 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
516 tree var = create_tmp_var (type, get_name (val));
517 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
518 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
519 DECL_GIMPLE_REG_P (var) = 1;
520 return var;
523 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
524 an existing expression temporary. */
526 static tree
527 lookup_tmp_var (tree val, bool is_formal)
529 tree ret;
531 /* If not optimizing, never really reuse a temporary. local-alloc
532 won't allocate any variable that is used in more than one basic
533 block, which means it will go into memory, causing much extra
534 work in reload and final and poorer code generation, outweighing
535 the extra memory allocation here. */
536 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
537 ret = create_tmp_from_val (val);
538 else
540 elt_t elt, *elt_p;
541 elt_t **slot;
543 elt.val = val;
544 if (!gimplify_ctxp->temp_htab)
545 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
546 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
547 if (*slot == NULL)
549 elt_p = XNEW (elt_t);
550 elt_p->val = val;
551 elt_p->temp = ret = create_tmp_from_val (val);
552 *slot = elt_p;
554 else
556 elt_p = *slot;
557 ret = elt_p->temp;
561 return ret;
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
566 static tree
567 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
568 bool is_formal, bool allow_ssa)
570 tree t, mod;
572 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
574 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
575 fb_rvalue);
577 if (allow_ssa
578 && gimplify_ctxp->into_ssa
579 && is_gimple_reg_type (TREE_TYPE (val)))
581 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
582 if (! gimple_in_ssa_p (cfun))
584 const char *name = get_name (val);
585 if (name)
586 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
589 else
590 t = lookup_tmp_var (val, is_formal);
592 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
594 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
596 /* gimplify_modify_expr might want to reduce this further. */
597 gimplify_and_add (mod, pre_p);
598 ggc_free (mod);
600 return t;
603 /* Return a formal temporary variable initialized with VAL. PRE_P is as
604 in gimplify_expr. Only use this function if:
606 1) The value of the unfactored expression represented by VAL will not
607 change between the initialization and use of the temporary, and
608 2) The temporary will not be otherwise modified.
610 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611 and #2 means it is inappropriate for && temps.
613 For other cases, use get_initialized_tmp_var instead. */
615 tree
616 get_formal_tmp_var (tree val, gimple_seq *pre_p)
618 return internal_get_tmp_var (val, pre_p, NULL, true, true);
621 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
622 are as in gimplify_expr. */
624 tree
625 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
626 bool allow_ssa)
628 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
631 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
632 generate debug info for them; otherwise don't. */
634 void
635 declare_vars (tree vars, gimple *gs, bool debug_info)
637 tree last = vars;
638 if (last)
640 tree temps, block;
642 gbind *scope = as_a <gbind *> (gs);
644 temps = nreverse (last);
646 block = gimple_bind_block (scope);
647 gcc_assert (!block || TREE_CODE (block) == BLOCK);
648 if (!block || !debug_info)
650 DECL_CHAIN (last) = gimple_bind_vars (scope);
651 gimple_bind_set_vars (scope, temps);
653 else
655 /* We need to attach the nodes both to the BIND_EXPR and to its
656 associated BLOCK for debugging purposes. The key point here
657 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
659 if (BLOCK_VARS (block))
660 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
661 else
663 gimple_bind_set_vars (scope,
664 chainon (gimple_bind_vars (scope), temps));
665 BLOCK_VARS (block) = temps;
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
673 no such upper bound can be obtained. */
675 static void
676 force_constant_size (tree var)
678 /* The only attempt we make is by querying the maximum size of objects
679 of the variable's type. */
681 HOST_WIDE_INT max_size;
683 gcc_assert (VAR_P (var));
685 max_size = max_int_size_in_bytes (TREE_TYPE (var));
687 gcc_assert (max_size >= 0);
689 DECL_SIZE_UNIT (var)
690 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
691 DECL_SIZE (var)
692 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
695 /* Push the temporary variable TMP into the current binding. */
697 void
698 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
700 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
702 /* Later processing assumes that the object size is constant, which might
703 not be true at this point. Force the use of a constant upper bound in
704 this case. */
705 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
706 force_constant_size (tmp);
708 DECL_CONTEXT (tmp) = fn->decl;
709 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
711 record_vars_into (tmp, fn->decl);
714 /* Push the temporary variable TMP into the current binding. */
716 void
717 gimple_add_tmp_var (tree tmp)
719 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
721 /* Later processing assumes that the object size is constant, which might
722 not be true at this point. Force the use of a constant upper bound in
723 this case. */
724 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
725 force_constant_size (tmp);
727 DECL_CONTEXT (tmp) = current_function_decl;
728 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
730 if (gimplify_ctxp)
732 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
733 gimplify_ctxp->temps = tmp;
735 /* Mark temporaries local within the nearest enclosing parallel. */
736 if (gimplify_omp_ctxp)
738 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
739 while (ctx
740 && (ctx->region_type == ORT_WORKSHARE
741 || ctx->region_type == ORT_SIMD
742 || ctx->region_type == ORT_ACC))
743 ctx = ctx->outer_context;
744 if (ctx)
745 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
748 else if (cfun)
749 record_vars (tmp);
750 else
752 gimple_seq body_seq;
754 /* This case is for nested functions. We need to expose the locals
755 they create. */
756 body_seq = gimple_body (current_function_decl);
757 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764 nodes that are referenced more than once in GENERIC functions. This is
765 necessary because gimplification (translation into GIMPLE) is performed
766 by modifying tree nodes in-place, so gimplication of a shared node in a
767 first context could generate an invalid GIMPLE form in a second context.
769 This is achieved with a simple mark/copy/unmark algorithm that walks the
770 GENERIC representation top-down, marks nodes with TREE_VISITED the first
771 time it encounters them, duplicates them if they already have TREE_VISITED
772 set, and finally removes the TREE_VISITED marks it has set.
774 The algorithm works only at the function level, i.e. it generates a GENERIC
775 representation of a function with no nodes shared within the function when
776 passed a GENERIC function (except for nodes that are allowed to be shared).
778 At the global level, it is also necessary to unshare tree nodes that are
779 referenced in more than one function, for the same aforementioned reason.
780 This requires some cooperation from the front-end. There are 2 strategies:
782 1. Manual unsharing. The front-end needs to call unshare_expr on every
783 expression that might end up being shared across functions.
785 2. Deep unsharing. This is an extension of regular unsharing. Instead
786 of calling unshare_expr on expressions that might be shared across
787 functions, the front-end pre-marks them with TREE_VISITED. This will
788 ensure that they are unshared on the first reference within functions
789 when the regular unsharing algorithm runs. The counterpart is that
790 this algorithm must look deeper than for manual unsharing, which is
791 specified by LANG_HOOKS_DEEP_UNSHARING.
793 If there are only few specific cases of node sharing across functions, it is
794 probably easier for a front-end to unshare the expressions manually. On the
795 contrary, if the expressions generated at the global level are as widespread
796 as expressions generated within functions, deep unsharing is very likely the
797 way to go. */
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that must be done once. If we were to
801 unshare something like SAVE_EXPR(i++), the gimplification process would
802 create wrong code. However, if DATA is non-null, it must hold a pointer
803 set that is used to unshare the subtrees of these nodes. */
805 static tree
806 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
808 tree t = *tp;
809 enum tree_code code = TREE_CODE (t);
811 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812 copy their subtrees if we can make sure to do it only once. */
813 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
815 if (data && !((hash_set<tree> *)data)->add (t))
817 else
818 *walk_subtrees = 0;
821 /* Stop at types, decls, constants like copy_tree_r. */
822 else if (TREE_CODE_CLASS (code) == tcc_type
823 || TREE_CODE_CLASS (code) == tcc_declaration
824 || TREE_CODE_CLASS (code) == tcc_constant)
825 *walk_subtrees = 0;
827 /* Cope with the statement expression extension. */
828 else if (code == STATEMENT_LIST)
831 /* Leave the bulk of the work to copy_tree_r itself. */
832 else
833 copy_tree_r (tp, walk_subtrees, NULL);
835 return NULL_TREE;
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839 If *TP has been visited already, then *TP is deeply copied by calling
840 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
842 static tree
843 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
845 tree t = *tp;
846 enum tree_code code = TREE_CODE (t);
848 /* Skip types, decls, and constants. But we do want to look at their
849 types and the bounds of types. Mark them as visited so we properly
850 unmark their subtrees on the unmark pass. If we've already seen them,
851 don't look down further. */
852 if (TREE_CODE_CLASS (code) == tcc_type
853 || TREE_CODE_CLASS (code) == tcc_declaration
854 || TREE_CODE_CLASS (code) == tcc_constant)
856 if (TREE_VISITED (t))
857 *walk_subtrees = 0;
858 else
859 TREE_VISITED (t) = 1;
862 /* If this node has been visited already, unshare it and don't look
863 any deeper. */
864 else if (TREE_VISITED (t))
866 walk_tree (tp, mostly_copy_tree_r, data, NULL);
867 *walk_subtrees = 0;
870 /* Otherwise, mark the node as visited and keep looking. */
871 else
872 TREE_VISITED (t) = 1;
874 return NULL_TREE;
877 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
878 copy_if_shared_r callback unmodified. */
880 static inline void
881 copy_if_shared (tree *tp, void *data)
883 walk_tree (tp, copy_if_shared_r, data, NULL);
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887 any nested functions. */
889 static void
890 unshare_body (tree fndecl)
892 struct cgraph_node *cgn = cgraph_node::get (fndecl);
893 /* If the language requires deep unsharing, we need a pointer set to make
894 sure we don't repeatedly unshare subtrees of unshareable nodes. */
895 hash_set<tree> *visited
896 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
898 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
899 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
900 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
902 delete visited;
904 if (cgn)
905 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
906 unshare_body (cgn->decl);
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910 Subtrees are walked until the first unvisited node is encountered. */
912 static tree
913 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
915 tree t = *tp;
917 /* If this node has been visited, unmark it and keep looking. */
918 if (TREE_VISITED (t))
919 TREE_VISITED (t) = 0;
921 /* Otherwise, don't look any deeper. */
922 else
923 *walk_subtrees = 0;
925 return NULL_TREE;
928 /* Unmark the visited trees rooted at *TP. */
930 static inline void
931 unmark_visited (tree *tp)
933 walk_tree (tp, unmark_visited_r, NULL, NULL);
936 /* Likewise, but mark all trees as not visited. */
938 static void
939 unvisit_body (tree fndecl)
941 struct cgraph_node *cgn = cgraph_node::get (fndecl);
943 unmark_visited (&DECL_SAVED_TREE (fndecl));
944 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
945 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
947 if (cgn)
948 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
949 unvisit_body (cgn->decl);
952 /* Unconditionally make an unshared copy of EXPR. This is used when using
953 stored expressions which span multiple functions, such as BINFO_VTABLE,
954 as the normal unsharing process can't tell that they're shared. */
956 tree
957 unshare_expr (tree expr)
959 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
960 return expr;
963 /* Worker for unshare_expr_without_location. */
965 static tree
966 prune_expr_location (tree *tp, int *walk_subtrees, void *)
968 if (EXPR_P (*tp))
969 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
970 else
971 *walk_subtrees = 0;
972 return NULL_TREE;
975 /* Similar to unshare_expr but also prune all expression locations
976 from EXPR. */
978 tree
979 unshare_expr_without_location (tree expr)
981 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
982 if (EXPR_P (expr))
983 walk_tree (&expr, prune_expr_location, NULL, NULL);
984 return expr;
987 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
988 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
989 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
990 EXPR is the location of the EXPR. */
992 static location_t
993 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
995 if (!expr)
996 return or_else;
998 if (EXPR_HAS_LOCATION (expr))
999 return EXPR_LOCATION (expr);
1001 if (TREE_CODE (expr) != STATEMENT_LIST)
1002 return or_else;
1004 tree_stmt_iterator i = tsi_start (expr);
1006 bool found = false;
1007 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1009 found = true;
1010 tsi_next (&i);
1013 if (!found || !tsi_one_before_end_p (i))
1014 return or_else;
1016 return rexpr_location (tsi_stmt (i), or_else);
1019 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1020 rexpr_location for the potential recursion. */
1022 static inline bool
1023 rexpr_has_location (tree expr)
1025 return rexpr_location (expr) != UNKNOWN_LOCATION;
1029 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1030 contain statements and have a value. Assign its value to a temporary
1031 and give it void_type_node. Return the temporary, or NULL_TREE if
1032 WRAPPER was already void. */
1034 tree
1035 voidify_wrapper_expr (tree wrapper, tree temp)
1037 tree type = TREE_TYPE (wrapper);
1038 if (type && !VOID_TYPE_P (type))
1040 tree *p;
1042 /* Set p to point to the body of the wrapper. Loop until we find
1043 something that isn't a wrapper. */
1044 for (p = &wrapper; p && *p; )
1046 switch (TREE_CODE (*p))
1048 case BIND_EXPR:
1049 TREE_SIDE_EFFECTS (*p) = 1;
1050 TREE_TYPE (*p) = void_type_node;
1051 /* For a BIND_EXPR, the body is operand 1. */
1052 p = &BIND_EXPR_BODY (*p);
1053 break;
1055 case CLEANUP_POINT_EXPR:
1056 case TRY_FINALLY_EXPR:
1057 case TRY_CATCH_EXPR:
1058 TREE_SIDE_EFFECTS (*p) = 1;
1059 TREE_TYPE (*p) = void_type_node;
1060 p = &TREE_OPERAND (*p, 0);
1061 break;
1063 case STATEMENT_LIST:
1065 tree_stmt_iterator i = tsi_last (*p);
1066 TREE_SIDE_EFFECTS (*p) = 1;
1067 TREE_TYPE (*p) = void_type_node;
1068 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1070 break;
1072 case COMPOUND_EXPR:
1073 /* Advance to the last statement. Set all container types to
1074 void. */
1075 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1077 TREE_SIDE_EFFECTS (*p) = 1;
1078 TREE_TYPE (*p) = void_type_node;
1080 break;
1082 case TRANSACTION_EXPR:
1083 TREE_SIDE_EFFECTS (*p) = 1;
1084 TREE_TYPE (*p) = void_type_node;
1085 p = &TRANSACTION_EXPR_BODY (*p);
1086 break;
1088 default:
1089 /* Assume that any tree upon which voidify_wrapper_expr is
1090 directly called is a wrapper, and that its body is op0. */
1091 if (p == &wrapper)
1093 TREE_SIDE_EFFECTS (*p) = 1;
1094 TREE_TYPE (*p) = void_type_node;
1095 p = &TREE_OPERAND (*p, 0);
1096 break;
1098 goto out;
1102 out:
1103 if (p == NULL || IS_EMPTY_STMT (*p))
1104 temp = NULL_TREE;
1105 else if (temp)
1107 /* The wrapper is on the RHS of an assignment that we're pushing
1108 down. */
1109 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1110 || TREE_CODE (temp) == MODIFY_EXPR);
1111 TREE_OPERAND (temp, 1) = *p;
1112 *p = temp;
1114 else
1116 temp = create_tmp_var (type, "retval");
1117 *p = build2 (INIT_EXPR, type, temp, *p);
1120 return temp;
1123 return NULL_TREE;
1126 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1127 a temporary through which they communicate. */
1129 static void
1130 build_stack_save_restore (gcall **save, gcall **restore)
1132 tree tmp_var;
1134 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1135 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1136 gimple_call_set_lhs (*save, tmp_var);
1138 *restore
1139 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1140 1, tmp_var);
1143 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1145 static tree
1146 build_asan_poison_call_expr (tree decl)
1148 /* Do not poison variables that have size equal to zero. */
1149 tree unit_size = DECL_SIZE_UNIT (decl);
1150 if (zerop (unit_size))
1151 return NULL_TREE;
1153 tree base = build_fold_addr_expr (decl);
1155 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1156 void_type_node, 3,
1157 build_int_cst (integer_type_node,
1158 ASAN_MARK_POISON),
1159 base, unit_size);
1162 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1163 on POISON flag, shadow memory of a DECL variable. The call will be
1164 put on location identified by IT iterator, where BEFORE flag drives
1165 position where the stmt will be put. */
1167 static void
1168 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1169 bool before)
1171 tree unit_size = DECL_SIZE_UNIT (decl);
1172 tree base = build_fold_addr_expr (decl);
1174 /* Do not poison variables that have size equal to zero. */
1175 if (zerop (unit_size))
1176 return;
1178 /* It's necessary to have all stack variables aligned to ASAN granularity
1179 bytes. */
1180 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1181 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1183 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1185 gimple *g
1186 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1187 build_int_cst (integer_type_node, flags),
1188 base, unit_size);
1190 if (before)
1191 gsi_insert_before (it, g, GSI_NEW_STMT);
1192 else
1193 gsi_insert_after (it, g, GSI_NEW_STMT);
1196 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1197 either poisons or unpoisons a DECL. Created statement is appended
1198 to SEQ_P gimple sequence. */
1200 static void
1201 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1203 gimple_stmt_iterator it = gsi_last (*seq_p);
1204 bool before = false;
1206 if (gsi_end_p (it))
1207 before = true;
1209 asan_poison_variable (decl, poison, &it, before);
1212 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1214 static int
1215 sort_by_decl_uid (const void *a, const void *b)
1217 const tree *t1 = (const tree *)a;
1218 const tree *t2 = (const tree *)b;
1220 int uid1 = DECL_UID (*t1);
1221 int uid2 = DECL_UID (*t2);
1223 if (uid1 < uid2)
1224 return -1;
1225 else if (uid1 > uid2)
1226 return 1;
1227 else
1228 return 0;
1231 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1232 depending on POISON flag. Created statement is appended
1233 to SEQ_P gimple sequence. */
1235 static void
1236 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1238 unsigned c = variables->elements ();
1239 if (c == 0)
1240 return;
1242 auto_vec<tree> sorted_variables (c);
1244 for (hash_set<tree>::iterator it = variables->begin ();
1245 it != variables->end (); ++it)
1246 sorted_variables.safe_push (*it);
1248 sorted_variables.qsort (sort_by_decl_uid);
1250 unsigned i;
1251 tree var;
1252 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1254 asan_poison_variable (var, poison, seq_p);
1256 /* Add use_after_scope_memory attribute for the variable in order
1257 to prevent re-written into SSA. */
1258 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1259 DECL_ATTRIBUTES (var)))
1260 DECL_ATTRIBUTES (var)
1261 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1262 integer_one_node,
1263 DECL_ATTRIBUTES (var));
1267 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1269 static enum gimplify_status
1270 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1272 tree bind_expr = *expr_p;
1273 bool old_keep_stack = gimplify_ctxp->keep_stack;
1274 bool old_save_stack = gimplify_ctxp->save_stack;
1275 tree t;
1276 gbind *bind_stmt;
1277 gimple_seq body, cleanup;
1278 gcall *stack_save;
1279 location_t start_locus = 0, end_locus = 0;
1280 tree ret_clauses = NULL;
1282 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1284 /* Mark variables seen in this bind expr. */
1285 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1287 if (VAR_P (t))
1289 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1291 /* Mark variable as local. */
1292 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1293 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1294 || splay_tree_lookup (ctx->variables,
1295 (splay_tree_key) t) == NULL))
1297 if (ctx->region_type == ORT_SIMD
1298 && TREE_ADDRESSABLE (t)
1299 && !TREE_STATIC (t))
1300 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1301 else
1302 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1305 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1307 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1308 cfun->has_local_explicit_reg_vars = true;
1311 /* Preliminarily mark non-addressed complex variables as eligible
1312 for promotion to gimple registers. We'll transform their uses
1313 as we find them. */
1314 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1315 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1316 && !TREE_THIS_VOLATILE (t)
1317 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1318 && !needs_to_live_in_memory (t))
1319 DECL_GIMPLE_REG_P (t) = 1;
1322 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1323 BIND_EXPR_BLOCK (bind_expr));
1324 gimple_push_bind_expr (bind_stmt);
1326 gimplify_ctxp->keep_stack = false;
1327 gimplify_ctxp->save_stack = false;
1329 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1330 body = NULL;
1331 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1332 gimple_bind_set_body (bind_stmt, body);
1334 /* Source location wise, the cleanup code (stack_restore and clobbers)
1335 belongs to the end of the block, so propagate what we have. The
1336 stack_save operation belongs to the beginning of block, which we can
1337 infer from the bind_expr directly if the block has no explicit
1338 assignment. */
1339 if (BIND_EXPR_BLOCK (bind_expr))
1341 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1342 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1344 if (start_locus == 0)
1345 start_locus = EXPR_LOCATION (bind_expr);
1347 cleanup = NULL;
1348 stack_save = NULL;
1350 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1351 the stack space allocated to the VLAs. */
1352 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1354 gcall *stack_restore;
1356 /* Save stack on entry and restore it on exit. Add a try_finally
1357 block to achieve this. */
1358 build_stack_save_restore (&stack_save, &stack_restore);
1360 gimple_set_location (stack_save, start_locus);
1361 gimple_set_location (stack_restore, end_locus);
1363 gimplify_seq_add_stmt (&cleanup, stack_restore);
1366 /* Add clobbers for all variables that go out of scope. */
1367 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1369 if (VAR_P (t)
1370 && !is_global_var (t)
1371 && DECL_CONTEXT (t) == current_function_decl)
1373 if (!DECL_HARD_REGISTER (t)
1374 && !TREE_THIS_VOLATILE (t)
1375 && !DECL_HAS_VALUE_EXPR_P (t)
1376 /* Only care for variables that have to be in memory. Others
1377 will be rewritten into SSA names, hence moved to the
1378 top-level. */
1379 && !is_gimple_reg (t)
1380 && flag_stack_reuse != SR_NONE)
1382 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1383 gimple *clobber_stmt;
1384 TREE_THIS_VOLATILE (clobber) = 1;
1385 clobber_stmt = gimple_build_assign (t, clobber);
1386 gimple_set_location (clobber_stmt, end_locus);
1387 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1390 if (flag_openacc && oacc_declare_returns != NULL)
1392 tree *c = oacc_declare_returns->get (t);
1393 if (c != NULL)
1395 if (ret_clauses)
1396 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1398 ret_clauses = *c;
1400 oacc_declare_returns->remove (t);
1402 if (oacc_declare_returns->elements () == 0)
1404 delete oacc_declare_returns;
1405 oacc_declare_returns = NULL;
1411 if (asan_poisoned_variables != NULL
1412 && asan_poisoned_variables->contains (t))
1414 asan_poisoned_variables->remove (t);
1415 asan_poison_variable (t, true, &cleanup);
1418 if (gimplify_ctxp->live_switch_vars != NULL
1419 && gimplify_ctxp->live_switch_vars->contains (t))
1420 gimplify_ctxp->live_switch_vars->remove (t);
1423 if (ret_clauses)
1425 gomp_target *stmt;
1426 gimple_stmt_iterator si = gsi_start (cleanup);
1428 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1429 ret_clauses);
1430 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1433 if (cleanup)
1435 gtry *gs;
1436 gimple_seq new_body;
1438 new_body = NULL;
1439 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1440 GIMPLE_TRY_FINALLY);
1442 if (stack_save)
1443 gimplify_seq_add_stmt (&new_body, stack_save);
1444 gimplify_seq_add_stmt (&new_body, gs);
1445 gimple_bind_set_body (bind_stmt, new_body);
1448 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1449 if (!gimplify_ctxp->keep_stack)
1450 gimplify_ctxp->keep_stack = old_keep_stack;
1451 gimplify_ctxp->save_stack = old_save_stack;
1453 gimple_pop_bind_expr ();
1455 gimplify_seq_add_stmt (pre_p, bind_stmt);
1457 if (temp)
1459 *expr_p = temp;
1460 return GS_OK;
1463 *expr_p = NULL_TREE;
1464 return GS_ALL_DONE;
1467 /* Maybe add early return predict statement to PRE_P sequence. */
1469 static void
1470 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1472 /* If we are not in a conditional context, add PREDICT statement. */
1473 if (gimple_conditional_context ())
1475 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1476 NOT_TAKEN);
1477 gimplify_seq_add_stmt (pre_p, predict);
1481 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1482 GIMPLE value, it is assigned to a new temporary and the statement is
1483 re-written to return the temporary.
1485 PRE_P points to the sequence where side effects that must happen before
1486 STMT should be stored. */
1488 static enum gimplify_status
1489 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1491 greturn *ret;
1492 tree ret_expr = TREE_OPERAND (stmt, 0);
1493 tree result_decl, result;
1495 if (ret_expr == error_mark_node)
1496 return GS_ERROR;
1498 if (!ret_expr
1499 || TREE_CODE (ret_expr) == RESULT_DECL)
1501 maybe_add_early_return_predict_stmt (pre_p);
1502 greturn *ret = gimple_build_return (ret_expr);
1503 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1504 gimplify_seq_add_stmt (pre_p, ret);
1505 return GS_ALL_DONE;
1508 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1509 result_decl = NULL_TREE;
1510 else
1512 result_decl = TREE_OPERAND (ret_expr, 0);
1514 /* See through a return by reference. */
1515 if (TREE_CODE (result_decl) == INDIRECT_REF)
1516 result_decl = TREE_OPERAND (result_decl, 0);
1518 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1519 || TREE_CODE (ret_expr) == INIT_EXPR)
1520 && TREE_CODE (result_decl) == RESULT_DECL);
1523 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1524 Recall that aggregate_value_p is FALSE for any aggregate type that is
1525 returned in registers. If we're returning values in registers, then
1526 we don't want to extend the lifetime of the RESULT_DECL, particularly
1527 across another call. In addition, for those aggregates for which
1528 hard_function_value generates a PARALLEL, we'll die during normal
1529 expansion of structure assignments; there's special code in expand_return
1530 to handle this case that does not exist in expand_expr. */
1531 if (!result_decl)
1532 result = NULL_TREE;
1533 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1535 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1537 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1538 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1539 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1540 should be effectively allocated by the caller, i.e. all calls to
1541 this function must be subject to the Return Slot Optimization. */
1542 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1543 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1545 result = result_decl;
1547 else if (gimplify_ctxp->return_temp)
1548 result = gimplify_ctxp->return_temp;
1549 else
1551 result = create_tmp_reg (TREE_TYPE (result_decl));
1553 /* ??? With complex control flow (usually involving abnormal edges),
1554 we can wind up warning about an uninitialized value for this. Due
1555 to how this variable is constructed and initialized, this is never
1556 true. Give up and never warn. */
1557 TREE_NO_WARNING (result) = 1;
1559 gimplify_ctxp->return_temp = result;
1562 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1563 Then gimplify the whole thing. */
1564 if (result != result_decl)
1565 TREE_OPERAND (ret_expr, 0) = result;
1567 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1569 maybe_add_early_return_predict_stmt (pre_p);
1570 ret = gimple_build_return (result);
1571 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1572 gimplify_seq_add_stmt (pre_p, ret);
1574 return GS_ALL_DONE;
1577 /* Gimplify a variable-length array DECL. */
1579 static void
1580 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1582 /* This is a variable-sized decl. Simplify its size and mark it
1583 for deferred expansion. */
1584 tree t, addr, ptr_type;
1586 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1587 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1589 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1590 if (DECL_HAS_VALUE_EXPR_P (decl))
1591 return;
1593 /* All occurrences of this decl in final gimplified code will be
1594 replaced by indirection. Setting DECL_VALUE_EXPR does two
1595 things: First, it lets the rest of the gimplifier know what
1596 replacement to use. Second, it lets the debug info know
1597 where to find the value. */
1598 ptr_type = build_pointer_type (TREE_TYPE (decl));
1599 addr = create_tmp_var (ptr_type, get_name (decl));
1600 DECL_IGNORED_P (addr) = 0;
1601 t = build_fold_indirect_ref (addr);
1602 TREE_THIS_NOTRAP (t) = 1;
1603 SET_DECL_VALUE_EXPR (decl, t);
1604 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1606 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1607 max_int_size_in_bytes (TREE_TYPE (decl)));
1608 /* The call has been built for a variable-sized object. */
1609 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1610 t = fold_convert (ptr_type, t);
1611 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1613 gimplify_and_add (t, seq_p);
1616 /* A helper function to be called via walk_tree. Mark all labels under *TP
1617 as being forced. To be called for DECL_INITIAL of static variables. */
1619 static tree
1620 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1622 if (TYPE_P (*tp))
1623 *walk_subtrees = 0;
1624 if (TREE_CODE (*tp) == LABEL_DECL)
1626 FORCED_LABEL (*tp) = 1;
1627 cfun->has_forced_label_in_static = 1;
1630 return NULL_TREE;
1633 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1634 and initialization explicit. */
1636 static enum gimplify_status
1637 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1639 tree stmt = *stmt_p;
1640 tree decl = DECL_EXPR_DECL (stmt);
1642 *stmt_p = NULL_TREE;
1644 if (TREE_TYPE (decl) == error_mark_node)
1645 return GS_ERROR;
1647 if ((TREE_CODE (decl) == TYPE_DECL
1648 || VAR_P (decl))
1649 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1651 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1652 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1653 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1656 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1657 in case its size expressions contain problematic nodes like CALL_EXPR. */
1658 if (TREE_CODE (decl) == TYPE_DECL
1659 && DECL_ORIGINAL_TYPE (decl)
1660 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1662 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1663 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1664 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1667 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1669 tree init = DECL_INITIAL (decl);
1670 bool is_vla = false;
1672 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1673 || (!TREE_STATIC (decl)
1674 && flag_stack_check == GENERIC_STACK_CHECK
1675 && compare_tree_int (DECL_SIZE_UNIT (decl),
1676 STACK_CHECK_MAX_VAR_SIZE) > 0))
1678 gimplify_vla_decl (decl, seq_p);
1679 is_vla = true;
1682 if (asan_poisoned_variables
1683 && !is_vla
1684 && TREE_ADDRESSABLE (decl)
1685 && !TREE_STATIC (decl)
1686 && !DECL_HAS_VALUE_EXPR_P (decl)
1687 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1688 && dbg_cnt (asan_use_after_scope)
1689 && !gimplify_omp_ctxp)
1691 asan_poisoned_variables->add (decl);
1692 asan_poison_variable (decl, false, seq_p);
1693 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1694 gimplify_ctxp->live_switch_vars->add (decl);
1697 /* Some front ends do not explicitly declare all anonymous
1698 artificial variables. We compensate here by declaring the
1699 variables, though it would be better if the front ends would
1700 explicitly declare them. */
1701 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1702 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1703 gimple_add_tmp_var (decl);
1705 if (init && init != error_mark_node)
1707 if (!TREE_STATIC (decl))
1709 DECL_INITIAL (decl) = NULL_TREE;
1710 init = build2 (INIT_EXPR, void_type_node, decl, init);
1711 gimplify_and_add (init, seq_p);
1712 ggc_free (init);
1714 else
1715 /* We must still examine initializers for static variables
1716 as they may contain a label address. */
1717 walk_tree (&init, force_labels_r, NULL, NULL);
1721 return GS_ALL_DONE;
1724 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1725 and replacing the LOOP_EXPR with goto, but if the loop contains an
1726 EXIT_EXPR, we need to append a label for it to jump to. */
1728 static enum gimplify_status
1729 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1731 tree saved_label = gimplify_ctxp->exit_label;
1732 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1734 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1736 gimplify_ctxp->exit_label = NULL_TREE;
1738 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1740 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1742 if (gimplify_ctxp->exit_label)
1743 gimplify_seq_add_stmt (pre_p,
1744 gimple_build_label (gimplify_ctxp->exit_label));
1746 gimplify_ctxp->exit_label = saved_label;
1748 *expr_p = NULL;
1749 return GS_ALL_DONE;
1752 /* Gimplify a statement list onto a sequence. These may be created either
1753 by an enlightened front-end, or by shortcut_cond_expr. */
1755 static enum gimplify_status
1756 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1758 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1760 tree_stmt_iterator i = tsi_start (*expr_p);
1762 while (!tsi_end_p (i))
1764 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1765 tsi_delink (&i);
1768 if (temp)
1770 *expr_p = temp;
1771 return GS_OK;
1774 return GS_ALL_DONE;
1777 /* Callback for walk_gimple_seq. */
1779 static tree
1780 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1781 struct walk_stmt_info *wi)
1783 gimple *stmt = gsi_stmt (*gsi_p);
1785 *handled_ops_p = true;
1786 switch (gimple_code (stmt))
1788 case GIMPLE_TRY:
1789 /* A compiler-generated cleanup or a user-written try block.
1790 If it's empty, don't dive into it--that would result in
1791 worse location info. */
1792 if (gimple_try_eval (stmt) == NULL)
1794 wi->info = stmt;
1795 return integer_zero_node;
1797 /* Fall through. */
1798 case GIMPLE_BIND:
1799 case GIMPLE_CATCH:
1800 case GIMPLE_EH_FILTER:
1801 case GIMPLE_TRANSACTION:
1802 /* Walk the sub-statements. */
1803 *handled_ops_p = false;
1804 break;
1806 case GIMPLE_DEBUG:
1807 /* Ignore these. We may generate them before declarations that
1808 are never executed. If there's something to warn about,
1809 there will be non-debug stmts too, and we'll catch those. */
1810 break;
1812 case GIMPLE_CALL:
1813 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1815 *handled_ops_p = false;
1816 break;
1818 /* Fall through. */
1819 default:
1820 /* Save the first "real" statement (not a decl/lexical scope/...). */
1821 wi->info = stmt;
1822 return integer_zero_node;
1824 return NULL_TREE;
1827 /* Possibly warn about unreachable statements between switch's controlling
1828 expression and the first case. SEQ is the body of a switch expression. */
1830 static void
1831 maybe_warn_switch_unreachable (gimple_seq seq)
1833 if (!warn_switch_unreachable
1834 /* This warning doesn't play well with Fortran when optimizations
1835 are on. */
1836 || lang_GNU_Fortran ()
1837 || seq == NULL)
1838 return;
1840 struct walk_stmt_info wi;
1841 memset (&wi, 0, sizeof (wi));
1842 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1843 gimple *stmt = (gimple *) wi.info;
1845 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1847 if (gimple_code (stmt) == GIMPLE_GOTO
1848 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1849 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1850 /* Don't warn for compiler-generated gotos. These occur
1851 in Duff's devices, for example. */;
1852 else
1853 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1854 "statement will never be executed");
1859 /* A label entry that pairs label and a location. */
1860 struct label_entry
1862 tree label;
1863 location_t loc;
1866 /* Find LABEL in vector of label entries VEC. */
1868 static struct label_entry *
1869 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1871 unsigned int i;
1872 struct label_entry *l;
1874 FOR_EACH_VEC_ELT (*vec, i, l)
1875 if (l->label == label)
1876 return l;
1877 return NULL;
1880 /* Return true if LABEL, a LABEL_DECL, represents a case label
1881 in a vector of labels CASES. */
1883 static bool
1884 case_label_p (const vec<tree> *cases, tree label)
1886 unsigned int i;
1887 tree l;
1889 FOR_EACH_VEC_ELT (*cases, i, l)
1890 if (CASE_LABEL (l) == label)
1891 return true;
1892 return false;
1895 /* Find the last nondebug statement in a scope STMT. */
1897 static gimple *
1898 last_stmt_in_scope (gimple *stmt)
1900 if (!stmt)
1901 return NULL;
1903 switch (gimple_code (stmt))
1905 case GIMPLE_BIND:
1907 gbind *bind = as_a <gbind *> (stmt);
1908 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1909 return last_stmt_in_scope (stmt);
1912 case GIMPLE_TRY:
1914 gtry *try_stmt = as_a <gtry *> (stmt);
1915 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1916 gimple *last_eval = last_stmt_in_scope (stmt);
1917 if (gimple_stmt_may_fallthru (last_eval)
1918 && (last_eval == NULL
1919 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1920 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1922 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1923 return last_stmt_in_scope (stmt);
1925 else
1926 return last_eval;
1929 case GIMPLE_DEBUG:
1930 gcc_unreachable ();
1932 default:
1933 return stmt;
1937 /* Collect interesting labels in LABELS and return the statement preceding
1938 another case label, or a user-defined label. */
1940 static gimple *
1941 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1942 auto_vec <struct label_entry> *labels)
1944 gimple *prev = NULL;
1948 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1950 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1951 which starts on a GIMPLE_SWITCH and ends with a break label.
1952 Handle that as a single statement that can fall through. */
1953 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1954 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1955 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1956 if (last
1957 && gimple_code (first) == GIMPLE_SWITCH
1958 && gimple_code (last) == GIMPLE_LABEL)
1960 tree label = gimple_label_label (as_a <glabel *> (last));
1961 if (SWITCH_BREAK_LABEL_P (label))
1963 prev = bind;
1964 gsi_next (gsi_p);
1965 continue;
1969 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1970 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1972 /* Nested scope. Only look at the last statement of
1973 the innermost scope. */
1974 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1975 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1976 if (last)
1978 prev = last;
1979 /* It might be a label without a location. Use the
1980 location of the scope then. */
1981 if (!gimple_has_location (prev))
1982 gimple_set_location (prev, bind_loc);
1984 gsi_next (gsi_p);
1985 continue;
1988 /* Ifs are tricky. */
1989 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1991 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1992 tree false_lab = gimple_cond_false_label (cond_stmt);
1993 location_t if_loc = gimple_location (cond_stmt);
1995 /* If we have e.g.
1996 if (i > 1) goto <D.2259>; else goto D;
1997 we can't do much with the else-branch. */
1998 if (!DECL_ARTIFICIAL (false_lab))
1999 break;
2001 /* Go on until the false label, then one step back. */
2002 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2004 gimple *stmt = gsi_stmt (*gsi_p);
2005 if (gimple_code (stmt) == GIMPLE_LABEL
2006 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2007 break;
2010 /* Not found? Oops. */
2011 if (gsi_end_p (*gsi_p))
2012 break;
2014 struct label_entry l = { false_lab, if_loc };
2015 labels->safe_push (l);
2017 /* Go to the last statement of the then branch. */
2018 gsi_prev (gsi_p);
2020 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2021 <D.1759>:
2022 <stmt>;
2023 goto <D.1761>;
2024 <D.1760>:
2026 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2027 && !gimple_has_location (gsi_stmt (*gsi_p)))
2029 /* Look at the statement before, it might be
2030 attribute fallthrough, in which case don't warn. */
2031 gsi_prev (gsi_p);
2032 bool fallthru_before_dest
2033 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2034 gsi_next (gsi_p);
2035 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2036 if (!fallthru_before_dest)
2038 struct label_entry l = { goto_dest, if_loc };
2039 labels->safe_push (l);
2042 /* And move back. */
2043 gsi_next (gsi_p);
2046 /* Remember the last statement. Skip labels that are of no interest
2047 to us. */
2048 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2050 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2051 if (find_label_entry (labels, label))
2052 prev = gsi_stmt (*gsi_p);
2054 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2056 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2057 prev = gsi_stmt (*gsi_p);
2058 gsi_next (gsi_p);
2060 while (!gsi_end_p (*gsi_p)
2061 /* Stop if we find a case or a user-defined label. */
2062 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2063 || !gimple_has_location (gsi_stmt (*gsi_p))));
2065 return prev;
2068 /* Return true if the switch fallthough warning should occur. LABEL is
2069 the label statement that we're falling through to. */
2071 static bool
2072 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2074 gimple_stmt_iterator gsi = *gsi_p;
2076 /* Don't warn if the label is marked with a "falls through" comment. */
2077 if (FALLTHROUGH_LABEL_P (label))
2078 return false;
2080 /* Don't warn for non-case labels followed by a statement:
2081 case 0:
2082 foo ();
2083 label:
2084 bar ();
2085 as these are likely intentional. */
2086 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2088 tree l;
2089 while (!gsi_end_p (gsi)
2090 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2091 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2092 && !case_label_p (&gimplify_ctxp->case_labels, l))
2093 gsi_next_nondebug (&gsi);
2094 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2095 return false;
2098 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2099 immediately breaks. */
2100 gsi = *gsi_p;
2102 /* Skip all immediately following labels. */
2103 while (!gsi_end_p (gsi)
2104 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2105 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2106 gsi_next_nondebug (&gsi);
2108 /* { ... something; default:; } */
2109 if (gsi_end_p (gsi)
2110 /* { ... something; default: break; } or
2111 { ... something; default: goto L; } */
2112 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2113 /* { ... something; default: return; } */
2114 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2115 return false;
2117 return true;
2120 /* Callback for walk_gimple_seq. */
2122 static tree
2123 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2124 struct walk_stmt_info *)
2126 gimple *stmt = gsi_stmt (*gsi_p);
2128 *handled_ops_p = true;
2129 switch (gimple_code (stmt))
2131 case GIMPLE_TRY:
2132 case GIMPLE_BIND:
2133 case GIMPLE_CATCH:
2134 case GIMPLE_EH_FILTER:
2135 case GIMPLE_TRANSACTION:
2136 /* Walk the sub-statements. */
2137 *handled_ops_p = false;
2138 break;
2140 /* Find a sequence of form:
2142 GIMPLE_LABEL
2143 [...]
2144 <may fallthru stmt>
2145 GIMPLE_LABEL
2147 and possibly warn. */
2148 case GIMPLE_LABEL:
2150 /* Found a label. Skip all immediately following labels. */
2151 while (!gsi_end_p (*gsi_p)
2152 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2153 gsi_next_nondebug (gsi_p);
2155 /* There might be no more statements. */
2156 if (gsi_end_p (*gsi_p))
2157 return integer_zero_node;
2159 /* Vector of labels that fall through. */
2160 auto_vec <struct label_entry> labels;
2161 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2163 /* There might be no more statements. */
2164 if (gsi_end_p (*gsi_p))
2165 return integer_zero_node;
2167 gimple *next = gsi_stmt (*gsi_p);
2168 tree label;
2169 /* If what follows is a label, then we may have a fallthrough. */
2170 if (gimple_code (next) == GIMPLE_LABEL
2171 && gimple_has_location (next)
2172 && (label = gimple_label_label (as_a <glabel *> (next)))
2173 && prev != NULL)
2175 struct label_entry *l;
2176 bool warned_p = false;
2177 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2178 /* Quiet. */;
2179 else if (gimple_code (prev) == GIMPLE_LABEL
2180 && (label = gimple_label_label (as_a <glabel *> (prev)))
2181 && (l = find_label_entry (&labels, label)))
2182 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2183 "this statement may fall through");
2184 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2185 /* Try to be clever and don't warn when the statement
2186 can't actually fall through. */
2187 && gimple_stmt_may_fallthru (prev)
2188 && gimple_has_location (prev))
2189 warned_p = warning_at (gimple_location (prev),
2190 OPT_Wimplicit_fallthrough_,
2191 "this statement may fall through");
2192 if (warned_p)
2193 inform (gimple_location (next), "here");
2195 /* Mark this label as processed so as to prevent multiple
2196 warnings in nested switches. */
2197 FALLTHROUGH_LABEL_P (label) = true;
2199 /* So that next warn_implicit_fallthrough_r will start looking for
2200 a new sequence starting with this label. */
2201 gsi_prev (gsi_p);
2204 break;
2205 default:
2206 break;
2208 return NULL_TREE;
2211 /* Warn when a switch case falls through. */
2213 static void
2214 maybe_warn_implicit_fallthrough (gimple_seq seq)
2216 if (!warn_implicit_fallthrough)
2217 return;
2219 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2220 if (!(lang_GNU_C ()
2221 || lang_GNU_CXX ()
2222 || lang_GNU_OBJC ()))
2223 return;
2225 struct walk_stmt_info wi;
2226 memset (&wi, 0, sizeof (wi));
2227 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2230 /* Callback for walk_gimple_seq. */
2232 static tree
2233 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2234 struct walk_stmt_info *)
2236 gimple *stmt = gsi_stmt (*gsi_p);
2238 *handled_ops_p = true;
2239 switch (gimple_code (stmt))
2241 case GIMPLE_TRY:
2242 case GIMPLE_BIND:
2243 case GIMPLE_CATCH:
2244 case GIMPLE_EH_FILTER:
2245 case GIMPLE_TRANSACTION:
2246 /* Walk the sub-statements. */
2247 *handled_ops_p = false;
2248 break;
2249 case GIMPLE_CALL:
2250 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2252 gsi_remove (gsi_p, true);
2253 if (gsi_end_p (*gsi_p))
2254 return integer_zero_node;
2256 bool found = false;
2257 location_t loc = gimple_location (stmt);
2259 gimple_stmt_iterator gsi2 = *gsi_p;
2260 stmt = gsi_stmt (gsi2);
2261 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2263 /* Go on until the artificial label. */
2264 tree goto_dest = gimple_goto_dest (stmt);
2265 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2267 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2268 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2269 == goto_dest)
2270 break;
2273 /* Not found? Stop. */
2274 if (gsi_end_p (gsi2))
2275 break;
2277 /* Look one past it. */
2278 gsi_next (&gsi2);
2281 /* We're looking for a case label or default label here. */
2282 while (!gsi_end_p (gsi2))
2284 stmt = gsi_stmt (gsi2);
2285 if (gimple_code (stmt) == GIMPLE_LABEL)
2287 tree label = gimple_label_label (as_a <glabel *> (stmt));
2288 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2290 found = true;
2291 break;
2294 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2296 else if (!is_gimple_debug (stmt))
2297 /* Anything else is not expected. */
2298 break;
2299 gsi_next (&gsi2);
2301 if (!found)
2302 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2303 "a case label or default label");
2305 break;
2306 default:
2307 break;
2309 return NULL_TREE;
2312 /* Expand all FALLTHROUGH () calls in SEQ. */
2314 static void
2315 expand_FALLTHROUGH (gimple_seq *seq_p)
2317 struct walk_stmt_info wi;
2318 memset (&wi, 0, sizeof (wi));
2319 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2323 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2324 branch to. */
2326 static enum gimplify_status
2327 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2329 tree switch_expr = *expr_p;
2330 gimple_seq switch_body_seq = NULL;
2331 enum gimplify_status ret;
2332 tree index_type = TREE_TYPE (switch_expr);
2333 if (index_type == NULL_TREE)
2334 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2336 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2337 fb_rvalue);
2338 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2339 return ret;
2341 if (SWITCH_BODY (switch_expr))
2343 vec<tree> labels;
2344 vec<tree> saved_labels;
2345 hash_set<tree> *saved_live_switch_vars = NULL;
2346 tree default_case = NULL_TREE;
2347 gswitch *switch_stmt;
2349 /* Save old labels, get new ones from body, then restore the old
2350 labels. Save all the things from the switch body to append after. */
2351 saved_labels = gimplify_ctxp->case_labels;
2352 gimplify_ctxp->case_labels.create (8);
2354 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2355 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2356 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2357 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2358 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2359 else
2360 gimplify_ctxp->live_switch_vars = NULL;
2362 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2363 gimplify_ctxp->in_switch_expr = true;
2365 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2367 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2368 maybe_warn_switch_unreachable (switch_body_seq);
2369 maybe_warn_implicit_fallthrough (switch_body_seq);
2370 /* Only do this for the outermost GIMPLE_SWITCH. */
2371 if (!gimplify_ctxp->in_switch_expr)
2372 expand_FALLTHROUGH (&switch_body_seq);
2374 labels = gimplify_ctxp->case_labels;
2375 gimplify_ctxp->case_labels = saved_labels;
2377 if (gimplify_ctxp->live_switch_vars)
2379 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2380 delete gimplify_ctxp->live_switch_vars;
2382 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2384 preprocess_case_label_vec_for_gimple (labels, index_type,
2385 &default_case);
2387 bool add_bind = false;
2388 if (!default_case)
2390 glabel *new_default;
2392 default_case
2393 = build_case_label (NULL_TREE, NULL_TREE,
2394 create_artificial_label (UNKNOWN_LOCATION));
2395 if (old_in_switch_expr)
2397 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2398 add_bind = true;
2400 new_default = gimple_build_label (CASE_LABEL (default_case));
2401 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2403 else if (old_in_switch_expr)
2405 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2406 if (last && gimple_code (last) == GIMPLE_LABEL)
2408 tree label = gimple_label_label (as_a <glabel *> (last));
2409 if (SWITCH_BREAK_LABEL_P (label))
2410 add_bind = true;
2414 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2415 default_case, labels);
2416 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2417 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2418 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2419 so that we can easily find the start and end of the switch
2420 statement. */
2421 if (add_bind)
2423 gimple_seq bind_body = NULL;
2424 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2425 gimple_seq_add_seq (&bind_body, switch_body_seq);
2426 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2427 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2428 gimplify_seq_add_stmt (pre_p, bind);
2430 else
2432 gimplify_seq_add_stmt (pre_p, switch_stmt);
2433 gimplify_seq_add_seq (pre_p, switch_body_seq);
2435 labels.release ();
2437 else
2438 gcc_unreachable ();
2440 return GS_ALL_DONE;
2443 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2445 static enum gimplify_status
2446 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2448 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2449 == current_function_decl);
2451 tree label = LABEL_EXPR_LABEL (*expr_p);
2452 glabel *label_stmt = gimple_build_label (label);
2453 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2454 gimplify_seq_add_stmt (pre_p, label_stmt);
2456 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2457 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2458 NOT_TAKEN));
2459 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2460 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2461 TAKEN));
2463 return GS_ALL_DONE;
2466 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2468 static enum gimplify_status
2469 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2471 struct gimplify_ctx *ctxp;
2472 glabel *label_stmt;
2474 /* Invalid programs can play Duff's Device type games with, for example,
2475 #pragma omp parallel. At least in the C front end, we don't
2476 detect such invalid branches until after gimplification, in the
2477 diagnose_omp_blocks pass. */
2478 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2479 if (ctxp->case_labels.exists ())
2480 break;
2482 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2483 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2484 ctxp->case_labels.safe_push (*expr_p);
2485 gimplify_seq_add_stmt (pre_p, label_stmt);
2487 return GS_ALL_DONE;
2490 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2491 if necessary. */
2493 tree
2494 build_and_jump (tree *label_p)
2496 if (label_p == NULL)
2497 /* If there's nowhere to jump, just fall through. */
2498 return NULL_TREE;
2500 if (*label_p == NULL_TREE)
2502 tree label = create_artificial_label (UNKNOWN_LOCATION);
2503 *label_p = label;
2506 return build1 (GOTO_EXPR, void_type_node, *label_p);
2509 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2510 This also involves building a label to jump to and communicating it to
2511 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2513 static enum gimplify_status
2514 gimplify_exit_expr (tree *expr_p)
2516 tree cond = TREE_OPERAND (*expr_p, 0);
2517 tree expr;
2519 expr = build_and_jump (&gimplify_ctxp->exit_label);
2520 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2521 *expr_p = expr;
2523 return GS_OK;
2526 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2527 different from its canonical type, wrap the whole thing inside a
2528 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2529 type.
2531 The canonical type of a COMPONENT_REF is the type of the field being
2532 referenced--unless the field is a bit-field which can be read directly
2533 in a smaller mode, in which case the canonical type is the
2534 sign-appropriate type corresponding to that mode. */
2536 static void
2537 canonicalize_component_ref (tree *expr_p)
2539 tree expr = *expr_p;
2540 tree type;
2542 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2544 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2545 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2546 else
2547 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2549 /* One could argue that all the stuff below is not necessary for
2550 the non-bitfield case and declare it a FE error if type
2551 adjustment would be needed. */
2552 if (TREE_TYPE (expr) != type)
2554 #ifdef ENABLE_TYPES_CHECKING
2555 tree old_type = TREE_TYPE (expr);
2556 #endif
2557 int type_quals;
2559 /* We need to preserve qualifiers and propagate them from
2560 operand 0. */
2561 type_quals = TYPE_QUALS (type)
2562 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2563 if (TYPE_QUALS (type) != type_quals)
2564 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2566 /* Set the type of the COMPONENT_REF to the underlying type. */
2567 TREE_TYPE (expr) = type;
2569 #ifdef ENABLE_TYPES_CHECKING
2570 /* It is now a FE error, if the conversion from the canonical
2571 type to the original expression type is not useless. */
2572 gcc_assert (useless_type_conversion_p (old_type, type));
2573 #endif
2577 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2578 to foo, embed that change in the ADDR_EXPR by converting
2579 T array[U];
2580 (T *)&array
2582 &array[L]
2583 where L is the lower bound. For simplicity, only do this for constant
2584 lower bound.
2585 The constraint is that the type of &array[L] is trivially convertible
2586 to T *. */
2588 static void
2589 canonicalize_addr_expr (tree *expr_p)
2591 tree expr = *expr_p;
2592 tree addr_expr = TREE_OPERAND (expr, 0);
2593 tree datype, ddatype, pddatype;
2595 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2596 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2597 || TREE_CODE (addr_expr) != ADDR_EXPR)
2598 return;
2600 /* The addr_expr type should be a pointer to an array. */
2601 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2602 if (TREE_CODE (datype) != ARRAY_TYPE)
2603 return;
2605 /* The pointer to element type shall be trivially convertible to
2606 the expression pointer type. */
2607 ddatype = TREE_TYPE (datype);
2608 pddatype = build_pointer_type (ddatype);
2609 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2610 pddatype))
2611 return;
2613 /* The lower bound and element sizes must be constant. */
2614 if (!TYPE_SIZE_UNIT (ddatype)
2615 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2616 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2617 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2618 return;
2620 /* All checks succeeded. Build a new node to merge the cast. */
2621 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2622 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2623 NULL_TREE, NULL_TREE);
2624 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2626 /* We can have stripped a required restrict qualifier above. */
2627 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2628 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2631 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2632 underneath as appropriate. */
2634 static enum gimplify_status
2635 gimplify_conversion (tree *expr_p)
2637 location_t loc = EXPR_LOCATION (*expr_p);
2638 gcc_assert (CONVERT_EXPR_P (*expr_p));
2640 /* Then strip away all but the outermost conversion. */
2641 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2643 /* And remove the outermost conversion if it's useless. */
2644 if (tree_ssa_useless_type_conversion (*expr_p))
2645 *expr_p = TREE_OPERAND (*expr_p, 0);
2647 /* If we still have a conversion at the toplevel,
2648 then canonicalize some constructs. */
2649 if (CONVERT_EXPR_P (*expr_p))
2651 tree sub = TREE_OPERAND (*expr_p, 0);
2653 /* If a NOP conversion is changing the type of a COMPONENT_REF
2654 expression, then canonicalize its type now in order to expose more
2655 redundant conversions. */
2656 if (TREE_CODE (sub) == COMPONENT_REF)
2657 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2659 /* If a NOP conversion is changing a pointer to array of foo
2660 to a pointer to foo, embed that change in the ADDR_EXPR. */
2661 else if (TREE_CODE (sub) == ADDR_EXPR)
2662 canonicalize_addr_expr (expr_p);
2665 /* If we have a conversion to a non-register type force the
2666 use of a VIEW_CONVERT_EXPR instead. */
2667 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2668 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2669 TREE_OPERAND (*expr_p, 0));
2671 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2672 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2673 TREE_SET_CODE (*expr_p, NOP_EXPR);
2675 return GS_OK;
2678 /* Nonlocal VLAs seen in the current function. */
2679 static hash_set<tree> *nonlocal_vlas;
2681 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2682 static tree nonlocal_vla_vars;
2684 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2685 DECL_VALUE_EXPR, and it's worth re-examining things. */
2687 static enum gimplify_status
2688 gimplify_var_or_parm_decl (tree *expr_p)
2690 tree decl = *expr_p;
2692 /* ??? If this is a local variable, and it has not been seen in any
2693 outer BIND_EXPR, then it's probably the result of a duplicate
2694 declaration, for which we've already issued an error. It would
2695 be really nice if the front end wouldn't leak these at all.
2696 Currently the only known culprit is C++ destructors, as seen
2697 in g++.old-deja/g++.jason/binding.C. */
2698 if (VAR_P (decl)
2699 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2700 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2701 && decl_function_context (decl) == current_function_decl)
2703 gcc_assert (seen_error ());
2704 return GS_ERROR;
2707 /* When within an OMP context, notice uses of variables. */
2708 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2709 return GS_ALL_DONE;
2711 /* If the decl is an alias for another expression, substitute it now. */
2712 if (DECL_HAS_VALUE_EXPR_P (decl))
2714 tree value_expr = DECL_VALUE_EXPR (decl);
2716 /* For referenced nonlocal VLAs add a decl for debugging purposes
2717 to the current function. */
2718 if (VAR_P (decl)
2719 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2720 && nonlocal_vlas != NULL
2721 && TREE_CODE (value_expr) == INDIRECT_REF
2722 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2723 && decl_function_context (decl) != current_function_decl)
2725 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2726 while (ctx
2727 && (ctx->region_type == ORT_WORKSHARE
2728 || ctx->region_type == ORT_SIMD
2729 || ctx->region_type == ORT_ACC))
2730 ctx = ctx->outer_context;
2731 if (!ctx && !nonlocal_vlas->add (decl))
2733 tree copy = copy_node (decl);
2735 lang_hooks.dup_lang_specific_decl (copy);
2736 SET_DECL_RTL (copy, 0);
2737 TREE_USED (copy) = 1;
2738 DECL_CHAIN (copy) = nonlocal_vla_vars;
2739 nonlocal_vla_vars = copy;
2740 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2741 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2745 *expr_p = unshare_expr (value_expr);
2746 return GS_OK;
2749 return GS_ALL_DONE;
2752 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2754 static void
2755 recalculate_side_effects (tree t)
2757 enum tree_code code = TREE_CODE (t);
2758 int len = TREE_OPERAND_LENGTH (t);
2759 int i;
2761 switch (TREE_CODE_CLASS (code))
2763 case tcc_expression:
2764 switch (code)
2766 case INIT_EXPR:
2767 case MODIFY_EXPR:
2768 case VA_ARG_EXPR:
2769 case PREDECREMENT_EXPR:
2770 case PREINCREMENT_EXPR:
2771 case POSTDECREMENT_EXPR:
2772 case POSTINCREMENT_EXPR:
2773 /* All of these have side-effects, no matter what their
2774 operands are. */
2775 return;
2777 default:
2778 break;
2780 /* Fall through. */
2782 case tcc_comparison: /* a comparison expression */
2783 case tcc_unary: /* a unary arithmetic expression */
2784 case tcc_binary: /* a binary arithmetic expression */
2785 case tcc_reference: /* a reference */
2786 case tcc_vl_exp: /* a function call */
2787 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2788 for (i = 0; i < len; ++i)
2790 tree op = TREE_OPERAND (t, i);
2791 if (op && TREE_SIDE_EFFECTS (op))
2792 TREE_SIDE_EFFECTS (t) = 1;
2794 break;
2796 case tcc_constant:
2797 /* No side-effects. */
2798 return;
2800 default:
2801 gcc_unreachable ();
2805 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2806 node *EXPR_P.
2808 compound_lval
2809 : min_lval '[' val ']'
2810 | min_lval '.' ID
2811 | compound_lval '[' val ']'
2812 | compound_lval '.' ID
2814 This is not part of the original SIMPLE definition, which separates
2815 array and member references, but it seems reasonable to handle them
2816 together. Also, this way we don't run into problems with union
2817 aliasing; gcc requires that for accesses through a union to alias, the
2818 union reference must be explicit, which was not always the case when we
2819 were splitting up array and member refs.
2821 PRE_P points to the sequence where side effects that must happen before
2822 *EXPR_P should be stored.
2824 POST_P points to the sequence where side effects that must happen after
2825 *EXPR_P should be stored. */
2827 static enum gimplify_status
2828 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2829 fallback_t fallback)
2831 tree *p;
2832 enum gimplify_status ret = GS_ALL_DONE, tret;
2833 int i;
2834 location_t loc = EXPR_LOCATION (*expr_p);
2835 tree expr = *expr_p;
2837 /* Create a stack of the subexpressions so later we can walk them in
2838 order from inner to outer. */
2839 auto_vec<tree, 10> expr_stack;
2841 /* We can handle anything that get_inner_reference can deal with. */
2842 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2844 restart:
2845 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2846 if (TREE_CODE (*p) == INDIRECT_REF)
2847 *p = fold_indirect_ref_loc (loc, *p);
2849 if (handled_component_p (*p))
2851 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2852 additional COMPONENT_REFs. */
2853 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2854 && gimplify_var_or_parm_decl (p) == GS_OK)
2855 goto restart;
2856 else
2857 break;
2859 expr_stack.safe_push (*p);
2862 gcc_assert (expr_stack.length ());
2864 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2865 walked through and P points to the innermost expression.
2867 Java requires that we elaborated nodes in source order. That
2868 means we must gimplify the inner expression followed by each of
2869 the indices, in order. But we can't gimplify the inner
2870 expression until we deal with any variable bounds, sizes, or
2871 positions in order to deal with PLACEHOLDER_EXPRs.
2873 So we do this in three steps. First we deal with the annotations
2874 for any variables in the components, then we gimplify the base,
2875 then we gimplify any indices, from left to right. */
2876 for (i = expr_stack.length () - 1; i >= 0; i--)
2878 tree t = expr_stack[i];
2880 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2882 /* Gimplify the low bound and element type size and put them into
2883 the ARRAY_REF. If these values are set, they have already been
2884 gimplified. */
2885 if (TREE_OPERAND (t, 2) == NULL_TREE)
2887 tree low = unshare_expr (array_ref_low_bound (t));
2888 if (!is_gimple_min_invariant (low))
2890 TREE_OPERAND (t, 2) = low;
2891 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2892 post_p, is_gimple_reg,
2893 fb_rvalue);
2894 ret = MIN (ret, tret);
2897 else
2899 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2900 is_gimple_reg, fb_rvalue);
2901 ret = MIN (ret, tret);
2904 if (TREE_OPERAND (t, 3) == NULL_TREE)
2906 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2907 tree elmt_size = unshare_expr (array_ref_element_size (t));
2908 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2910 /* Divide the element size by the alignment of the element
2911 type (above). */
2912 elmt_size
2913 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2915 if (!is_gimple_min_invariant (elmt_size))
2917 TREE_OPERAND (t, 3) = elmt_size;
2918 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2919 post_p, is_gimple_reg,
2920 fb_rvalue);
2921 ret = MIN (ret, tret);
2924 else
2926 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2927 is_gimple_reg, fb_rvalue);
2928 ret = MIN (ret, tret);
2931 else if (TREE_CODE (t) == COMPONENT_REF)
2933 /* Set the field offset into T and gimplify it. */
2934 if (TREE_OPERAND (t, 2) == NULL_TREE)
2936 tree offset = unshare_expr (component_ref_field_offset (t));
2937 tree field = TREE_OPERAND (t, 1);
2938 tree factor
2939 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2941 /* Divide the offset by its alignment. */
2942 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2944 if (!is_gimple_min_invariant (offset))
2946 TREE_OPERAND (t, 2) = offset;
2947 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2948 post_p, is_gimple_reg,
2949 fb_rvalue);
2950 ret = MIN (ret, tret);
2953 else
2955 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2956 is_gimple_reg, fb_rvalue);
2957 ret = MIN (ret, tret);
2962 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2963 so as to match the min_lval predicate. Failure to do so may result
2964 in the creation of large aggregate temporaries. */
2965 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2966 fallback | fb_lvalue);
2967 ret = MIN (ret, tret);
2969 /* And finally, the indices and operands of ARRAY_REF. During this
2970 loop we also remove any useless conversions. */
2971 for (; expr_stack.length () > 0; )
2973 tree t = expr_stack.pop ();
2975 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2977 /* Gimplify the dimension. */
2978 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2980 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2981 is_gimple_val, fb_rvalue);
2982 ret = MIN (ret, tret);
2986 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2988 /* The innermost expression P may have originally had
2989 TREE_SIDE_EFFECTS set which would have caused all the outer
2990 expressions in *EXPR_P leading to P to also have had
2991 TREE_SIDE_EFFECTS set. */
2992 recalculate_side_effects (t);
2995 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2996 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2998 canonicalize_component_ref (expr_p);
3001 expr_stack.release ();
3003 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3005 return ret;
3008 /* Gimplify the self modifying expression pointed to by EXPR_P
3009 (++, --, +=, -=).
3011 PRE_P points to the list where side effects that must happen before
3012 *EXPR_P should be stored.
3014 POST_P points to the list where side effects that must happen after
3015 *EXPR_P should be stored.
3017 WANT_VALUE is nonzero iff we want to use the value of this expression
3018 in another expression.
3020 ARITH_TYPE is the type the computation should be performed in. */
3022 enum gimplify_status
3023 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3024 bool want_value, tree arith_type)
3026 enum tree_code code;
3027 tree lhs, lvalue, rhs, t1;
3028 gimple_seq post = NULL, *orig_post_p = post_p;
3029 bool postfix;
3030 enum tree_code arith_code;
3031 enum gimplify_status ret;
3032 location_t loc = EXPR_LOCATION (*expr_p);
3034 code = TREE_CODE (*expr_p);
3036 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3037 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3039 /* Prefix or postfix? */
3040 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3041 /* Faster to treat as prefix if result is not used. */
3042 postfix = want_value;
3043 else
3044 postfix = false;
3046 /* For postfix, make sure the inner expression's post side effects
3047 are executed after side effects from this expression. */
3048 if (postfix)
3049 post_p = &post;
3051 /* Add or subtract? */
3052 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3053 arith_code = PLUS_EXPR;
3054 else
3055 arith_code = MINUS_EXPR;
3057 /* Gimplify the LHS into a GIMPLE lvalue. */
3058 lvalue = TREE_OPERAND (*expr_p, 0);
3059 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3060 if (ret == GS_ERROR)
3061 return ret;
3063 /* Extract the operands to the arithmetic operation. */
3064 lhs = lvalue;
3065 rhs = TREE_OPERAND (*expr_p, 1);
3067 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3068 that as the result value and in the postqueue operation. */
3069 if (postfix)
3071 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3072 if (ret == GS_ERROR)
3073 return ret;
3075 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3078 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3079 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3081 rhs = convert_to_ptrofftype_loc (loc, rhs);
3082 if (arith_code == MINUS_EXPR)
3083 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3084 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3086 else
3087 t1 = fold_convert (TREE_TYPE (*expr_p),
3088 fold_build2 (arith_code, arith_type,
3089 fold_convert (arith_type, lhs),
3090 fold_convert (arith_type, rhs)));
3092 if (postfix)
3094 gimplify_assign (lvalue, t1, pre_p);
3095 gimplify_seq_add_seq (orig_post_p, post);
3096 *expr_p = lhs;
3097 return GS_ALL_DONE;
3099 else
3101 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3102 return GS_OK;
3106 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3108 static void
3109 maybe_with_size_expr (tree *expr_p)
3111 tree expr = *expr_p;
3112 tree type = TREE_TYPE (expr);
3113 tree size;
3115 /* If we've already wrapped this or the type is error_mark_node, we can't do
3116 anything. */
3117 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3118 || type == error_mark_node)
3119 return;
3121 /* If the size isn't known or is a constant, we have nothing to do. */
3122 size = TYPE_SIZE_UNIT (type);
3123 if (!size || poly_int_tree_p (size))
3124 return;
3126 /* Otherwise, make a WITH_SIZE_EXPR. */
3127 size = unshare_expr (size);
3128 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3129 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3132 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3133 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3134 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3135 gimplified to an SSA name. */
3137 enum gimplify_status
3138 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3139 bool allow_ssa)
3141 bool (*test) (tree);
3142 fallback_t fb;
3144 /* In general, we allow lvalues for function arguments to avoid
3145 extra overhead of copying large aggregates out of even larger
3146 aggregates into temporaries only to copy the temporaries to
3147 the argument list. Make optimizers happy by pulling out to
3148 temporaries those types that fit in registers. */
3149 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3150 test = is_gimple_val, fb = fb_rvalue;
3151 else
3153 test = is_gimple_lvalue, fb = fb_either;
3154 /* Also strip a TARGET_EXPR that would force an extra copy. */
3155 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3157 tree init = TARGET_EXPR_INITIAL (*arg_p);
3158 if (init
3159 && !VOID_TYPE_P (TREE_TYPE (init)))
3160 *arg_p = init;
3164 /* If this is a variable sized type, we must remember the size. */
3165 maybe_with_size_expr (arg_p);
3167 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3168 /* Make sure arguments have the same location as the function call
3169 itself. */
3170 protected_set_expr_location (*arg_p, call_location);
3172 /* There is a sequence point before a function call. Side effects in
3173 the argument list must occur before the actual call. So, when
3174 gimplifying arguments, force gimplify_expr to use an internal
3175 post queue which is then appended to the end of PRE_P. */
3176 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3179 /* Don't fold inside offloading or taskreg regions: it can break code by
3180 adding decl references that weren't in the source. We'll do it during
3181 omplower pass instead. */
3183 static bool
3184 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3186 struct gimplify_omp_ctx *ctx;
3187 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3188 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3189 return false;
3190 return fold_stmt (gsi);
3193 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3194 WANT_VALUE is true if the result of the call is desired. */
3196 static enum gimplify_status
3197 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3199 tree fndecl, parms, p, fnptrtype;
3200 enum gimplify_status ret;
3201 int i, nargs;
3202 gcall *call;
3203 bool builtin_va_start_p = false;
3204 location_t loc = EXPR_LOCATION (*expr_p);
3206 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3208 /* For reliable diagnostics during inlining, it is necessary that
3209 every call_expr be annotated with file and line. */
3210 if (! EXPR_HAS_LOCATION (*expr_p))
3211 SET_EXPR_LOCATION (*expr_p, input_location);
3213 /* Gimplify internal functions created in the FEs. */
3214 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3216 if (want_value)
3217 return GS_ALL_DONE;
3219 nargs = call_expr_nargs (*expr_p);
3220 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3221 auto_vec<tree> vargs (nargs);
3223 for (i = 0; i < nargs; i++)
3225 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3226 EXPR_LOCATION (*expr_p));
3227 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3230 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3231 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3232 gimplify_seq_add_stmt (pre_p, call);
3233 return GS_ALL_DONE;
3236 /* This may be a call to a builtin function.
3238 Builtin function calls may be transformed into different
3239 (and more efficient) builtin function calls under certain
3240 circumstances. Unfortunately, gimplification can muck things
3241 up enough that the builtin expanders are not aware that certain
3242 transformations are still valid.
3244 So we attempt transformation/gimplification of the call before
3245 we gimplify the CALL_EXPR. At this time we do not manage to
3246 transform all calls in the same manner as the expanders do, but
3247 we do transform most of them. */
3248 fndecl = get_callee_fndecl (*expr_p);
3249 if (fndecl
3250 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3251 switch (DECL_FUNCTION_CODE (fndecl))
3253 CASE_BUILT_IN_ALLOCA:
3254 /* If the call has been built for a variable-sized object, then we
3255 want to restore the stack level when the enclosing BIND_EXPR is
3256 exited to reclaim the allocated space; otherwise, we precisely
3257 need to do the opposite and preserve the latest stack level. */
3258 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3259 gimplify_ctxp->save_stack = true;
3260 else
3261 gimplify_ctxp->keep_stack = true;
3262 break;
3264 case BUILT_IN_VA_START:
3266 builtin_va_start_p = TRUE;
3267 if (call_expr_nargs (*expr_p) < 2)
3269 error ("too few arguments to function %<va_start%>");
3270 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3271 return GS_OK;
3274 if (fold_builtin_next_arg (*expr_p, true))
3276 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3277 return GS_OK;
3279 break;
3282 default:
3285 if (fndecl && DECL_BUILT_IN (fndecl))
3287 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3288 if (new_tree && new_tree != *expr_p)
3290 /* There was a transformation of this call which computes the
3291 same value, but in a more efficient way. Return and try
3292 again. */
3293 *expr_p = new_tree;
3294 return GS_OK;
3298 /* Remember the original function pointer type. */
3299 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3301 /* There is a sequence point before the call, so any side effects in
3302 the calling expression must occur before the actual call. Force
3303 gimplify_expr to use an internal post queue. */
3304 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3305 is_gimple_call_addr, fb_rvalue);
3307 nargs = call_expr_nargs (*expr_p);
3309 /* Get argument types for verification. */
3310 fndecl = get_callee_fndecl (*expr_p);
3311 parms = NULL_TREE;
3312 if (fndecl)
3313 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3314 else
3315 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3317 if (fndecl && DECL_ARGUMENTS (fndecl))
3318 p = DECL_ARGUMENTS (fndecl);
3319 else if (parms)
3320 p = parms;
3321 else
3322 p = NULL_TREE;
3323 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3326 /* If the last argument is __builtin_va_arg_pack () and it is not
3327 passed as a named argument, decrease the number of CALL_EXPR
3328 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3329 if (!p
3330 && i < nargs
3331 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3333 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3334 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3336 if (last_arg_fndecl
3337 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3338 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3339 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3341 tree call = *expr_p;
3343 --nargs;
3344 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3345 CALL_EXPR_FN (call),
3346 nargs, CALL_EXPR_ARGP (call));
3348 /* Copy all CALL_EXPR flags, location and block, except
3349 CALL_EXPR_VA_ARG_PACK flag. */
3350 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3351 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3352 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3353 = CALL_EXPR_RETURN_SLOT_OPT (call);
3354 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3355 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3357 /* Set CALL_EXPR_VA_ARG_PACK. */
3358 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3362 /* If the call returns twice then after building the CFG the call
3363 argument computations will no longer dominate the call because
3364 we add an abnormal incoming edge to the call. So do not use SSA
3365 vars there. */
3366 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3368 /* Gimplify the function arguments. */
3369 if (nargs > 0)
3371 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3372 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3373 PUSH_ARGS_REVERSED ? i-- : i++)
3375 enum gimplify_status t;
3377 /* Avoid gimplifying the second argument to va_start, which needs to
3378 be the plain PARM_DECL. */
3379 if ((i != 1) || !builtin_va_start_p)
3381 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3382 EXPR_LOCATION (*expr_p), ! returns_twice);
3384 if (t == GS_ERROR)
3385 ret = GS_ERROR;
3390 /* Gimplify the static chain. */
3391 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3393 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3394 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3395 else
3397 enum gimplify_status t;
3398 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3399 EXPR_LOCATION (*expr_p), ! returns_twice);
3400 if (t == GS_ERROR)
3401 ret = GS_ERROR;
3405 /* Verify the function result. */
3406 if (want_value && fndecl
3407 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3409 error_at (loc, "using result of function returning %<void%>");
3410 ret = GS_ERROR;
3413 /* Try this again in case gimplification exposed something. */
3414 if (ret != GS_ERROR)
3416 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3418 if (new_tree && new_tree != *expr_p)
3420 /* There was a transformation of this call which computes the
3421 same value, but in a more efficient way. Return and try
3422 again. */
3423 *expr_p = new_tree;
3424 return GS_OK;
3427 else
3429 *expr_p = error_mark_node;
3430 return GS_ERROR;
3433 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3434 decl. This allows us to eliminate redundant or useless
3435 calls to "const" functions. */
3436 if (TREE_CODE (*expr_p) == CALL_EXPR)
3438 int flags = call_expr_flags (*expr_p);
3439 if (flags & (ECF_CONST | ECF_PURE)
3440 /* An infinite loop is considered a side effect. */
3441 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3442 TREE_SIDE_EFFECTS (*expr_p) = 0;
3445 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3446 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3447 form and delegate the creation of a GIMPLE_CALL to
3448 gimplify_modify_expr. This is always possible because when
3449 WANT_VALUE is true, the caller wants the result of this call into
3450 a temporary, which means that we will emit an INIT_EXPR in
3451 internal_get_tmp_var which will then be handled by
3452 gimplify_modify_expr. */
3453 if (!want_value)
3455 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3456 have to do is replicate it as a GIMPLE_CALL tuple. */
3457 gimple_stmt_iterator gsi;
3458 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3459 notice_special_calls (call);
3460 gimplify_seq_add_stmt (pre_p, call);
3461 gsi = gsi_last (*pre_p);
3462 maybe_fold_stmt (&gsi);
3463 *expr_p = NULL_TREE;
3465 else
3466 /* Remember the original function type. */
3467 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3468 CALL_EXPR_FN (*expr_p));
3470 return ret;
3473 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3474 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3476 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3477 condition is true or false, respectively. If null, we should generate
3478 our own to skip over the evaluation of this specific expression.
3480 LOCUS is the source location of the COND_EXPR.
3482 This function is the tree equivalent of do_jump.
3484 shortcut_cond_r should only be called by shortcut_cond_expr. */
3486 static tree
3487 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3488 location_t locus)
3490 tree local_label = NULL_TREE;
3491 tree t, expr = NULL;
3493 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3494 retain the shortcut semantics. Just insert the gotos here;
3495 shortcut_cond_expr will append the real blocks later. */
3496 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3498 location_t new_locus;
3500 /* Turn if (a && b) into
3502 if (a); else goto no;
3503 if (b) goto yes; else goto no;
3504 (no:) */
3506 if (false_label_p == NULL)
3507 false_label_p = &local_label;
3509 /* Keep the original source location on the first 'if'. */
3510 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3511 append_to_statement_list (t, &expr);
3513 /* Set the source location of the && on the second 'if'. */
3514 new_locus = rexpr_location (pred, locus);
3515 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3516 new_locus);
3517 append_to_statement_list (t, &expr);
3519 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3521 location_t new_locus;
3523 /* Turn if (a || b) into
3525 if (a) goto yes;
3526 if (b) goto yes; else goto no;
3527 (yes:) */
3529 if (true_label_p == NULL)
3530 true_label_p = &local_label;
3532 /* Keep the original source location on the first 'if'. */
3533 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3534 append_to_statement_list (t, &expr);
3536 /* Set the source location of the || on the second 'if'. */
3537 new_locus = rexpr_location (pred, locus);
3538 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3539 new_locus);
3540 append_to_statement_list (t, &expr);
3542 else if (TREE_CODE (pred) == COND_EXPR
3543 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3544 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3546 location_t new_locus;
3548 /* As long as we're messing with gotos, turn if (a ? b : c) into
3549 if (a)
3550 if (b) goto yes; else goto no;
3551 else
3552 if (c) goto yes; else goto no;
3554 Don't do this if one of the arms has void type, which can happen
3555 in C++ when the arm is throw. */
3557 /* Keep the original source location on the first 'if'. Set the source
3558 location of the ? on the second 'if'. */
3559 new_locus = rexpr_location (pred, locus);
3560 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3561 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3562 false_label_p, locus),
3563 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3564 false_label_p, new_locus));
3566 else
3568 expr = build3 (COND_EXPR, void_type_node, pred,
3569 build_and_jump (true_label_p),
3570 build_and_jump (false_label_p));
3571 SET_EXPR_LOCATION (expr, locus);
3574 if (local_label)
3576 t = build1 (LABEL_EXPR, void_type_node, local_label);
3577 append_to_statement_list (t, &expr);
3580 return expr;
3583 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3584 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3585 statement, if it is the last one. Otherwise, return NULL. */
3587 static tree
3588 find_goto (tree expr)
3590 if (!expr)
3591 return NULL_TREE;
3593 if (TREE_CODE (expr) == GOTO_EXPR)
3594 return expr;
3596 if (TREE_CODE (expr) != STATEMENT_LIST)
3597 return NULL_TREE;
3599 tree_stmt_iterator i = tsi_start (expr);
3601 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3602 tsi_next (&i);
3604 if (!tsi_one_before_end_p (i))
3605 return NULL_TREE;
3607 return find_goto (tsi_stmt (i));
3610 /* Same as find_goto, except that it returns NULL if the destination
3611 is not a LABEL_DECL. */
3613 static inline tree
3614 find_goto_label (tree expr)
3616 tree dest = find_goto (expr);
3617 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3618 return dest;
3619 return NULL_TREE;
3622 /* Given a conditional expression EXPR with short-circuit boolean
3623 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3624 predicate apart into the equivalent sequence of conditionals. */
3626 static tree
3627 shortcut_cond_expr (tree expr)
3629 tree pred = TREE_OPERAND (expr, 0);
3630 tree then_ = TREE_OPERAND (expr, 1);
3631 tree else_ = TREE_OPERAND (expr, 2);
3632 tree true_label, false_label, end_label, t;
3633 tree *true_label_p;
3634 tree *false_label_p;
3635 bool emit_end, emit_false, jump_over_else;
3636 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3637 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3639 /* First do simple transformations. */
3640 if (!else_se)
3642 /* If there is no 'else', turn
3643 if (a && b) then c
3644 into
3645 if (a) if (b) then c. */
3646 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3648 /* Keep the original source location on the first 'if'. */
3649 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3650 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3651 /* Set the source location of the && on the second 'if'. */
3652 if (rexpr_has_location (pred))
3653 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3654 then_ = shortcut_cond_expr (expr);
3655 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3656 pred = TREE_OPERAND (pred, 0);
3657 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3658 SET_EXPR_LOCATION (expr, locus);
3662 if (!then_se)
3664 /* If there is no 'then', turn
3665 if (a || b); else d
3666 into
3667 if (a); else if (b); else d. */
3668 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3670 /* Keep the original source location on the first 'if'. */
3671 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3672 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3673 /* Set the source location of the || on the second 'if'. */
3674 if (rexpr_has_location (pred))
3675 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3676 else_ = shortcut_cond_expr (expr);
3677 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3678 pred = TREE_OPERAND (pred, 0);
3679 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3680 SET_EXPR_LOCATION (expr, locus);
3684 /* If we're done, great. */
3685 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3686 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3687 return expr;
3689 /* Otherwise we need to mess with gotos. Change
3690 if (a) c; else d;
3692 if (a); else goto no;
3693 c; goto end;
3694 no: d; end:
3695 and recursively gimplify the condition. */
3697 true_label = false_label = end_label = NULL_TREE;
3699 /* If our arms just jump somewhere, hijack those labels so we don't
3700 generate jumps to jumps. */
3702 if (tree then_goto = find_goto_label (then_))
3704 true_label = GOTO_DESTINATION (then_goto);
3705 then_ = NULL;
3706 then_se = false;
3709 if (tree else_goto = find_goto_label (else_))
3711 false_label = GOTO_DESTINATION (else_goto);
3712 else_ = NULL;
3713 else_se = false;
3716 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3717 if (true_label)
3718 true_label_p = &true_label;
3719 else
3720 true_label_p = NULL;
3722 /* The 'else' branch also needs a label if it contains interesting code. */
3723 if (false_label || else_se)
3724 false_label_p = &false_label;
3725 else
3726 false_label_p = NULL;
3728 /* If there was nothing else in our arms, just forward the label(s). */
3729 if (!then_se && !else_se)
3730 return shortcut_cond_r (pred, true_label_p, false_label_p,
3731 EXPR_LOC_OR_LOC (expr, input_location));
3733 /* If our last subexpression already has a terminal label, reuse it. */
3734 if (else_se)
3735 t = expr_last (else_);
3736 else if (then_se)
3737 t = expr_last (then_);
3738 else
3739 t = NULL;
3740 if (t && TREE_CODE (t) == LABEL_EXPR)
3741 end_label = LABEL_EXPR_LABEL (t);
3743 /* If we don't care about jumping to the 'else' branch, jump to the end
3744 if the condition is false. */
3745 if (!false_label_p)
3746 false_label_p = &end_label;
3748 /* We only want to emit these labels if we aren't hijacking them. */
3749 emit_end = (end_label == NULL_TREE);
3750 emit_false = (false_label == NULL_TREE);
3752 /* We only emit the jump over the else clause if we have to--if the
3753 then clause may fall through. Otherwise we can wind up with a
3754 useless jump and a useless label at the end of gimplified code,
3755 which will cause us to think that this conditional as a whole
3756 falls through even if it doesn't. If we then inline a function
3757 which ends with such a condition, that can cause us to issue an
3758 inappropriate warning about control reaching the end of a
3759 non-void function. */
3760 jump_over_else = block_may_fallthru (then_);
3762 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3763 EXPR_LOC_OR_LOC (expr, input_location));
3765 expr = NULL;
3766 append_to_statement_list (pred, &expr);
3768 append_to_statement_list (then_, &expr);
3769 if (else_se)
3771 if (jump_over_else)
3773 tree last = expr_last (expr);
3774 t = build_and_jump (&end_label);
3775 if (rexpr_has_location (last))
3776 SET_EXPR_LOCATION (t, rexpr_location (last));
3777 append_to_statement_list (t, &expr);
3779 if (emit_false)
3781 t = build1 (LABEL_EXPR, void_type_node, false_label);
3782 append_to_statement_list (t, &expr);
3784 append_to_statement_list (else_, &expr);
3786 if (emit_end && end_label)
3788 t = build1 (LABEL_EXPR, void_type_node, end_label);
3789 append_to_statement_list (t, &expr);
3792 return expr;
3795 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3797 tree
3798 gimple_boolify (tree expr)
3800 tree type = TREE_TYPE (expr);
3801 location_t loc = EXPR_LOCATION (expr);
3803 if (TREE_CODE (expr) == NE_EXPR
3804 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3805 && integer_zerop (TREE_OPERAND (expr, 1)))
3807 tree call = TREE_OPERAND (expr, 0);
3808 tree fn = get_callee_fndecl (call);
3810 /* For __builtin_expect ((long) (x), y) recurse into x as well
3811 if x is truth_value_p. */
3812 if (fn
3813 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3814 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3815 && call_expr_nargs (call) == 2)
3817 tree arg = CALL_EXPR_ARG (call, 0);
3818 if (arg)
3820 if (TREE_CODE (arg) == NOP_EXPR
3821 && TREE_TYPE (arg) == TREE_TYPE (call))
3822 arg = TREE_OPERAND (arg, 0);
3823 if (truth_value_p (TREE_CODE (arg)))
3825 arg = gimple_boolify (arg);
3826 CALL_EXPR_ARG (call, 0)
3827 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3833 switch (TREE_CODE (expr))
3835 case TRUTH_AND_EXPR:
3836 case TRUTH_OR_EXPR:
3837 case TRUTH_XOR_EXPR:
3838 case TRUTH_ANDIF_EXPR:
3839 case TRUTH_ORIF_EXPR:
3840 /* Also boolify the arguments of truth exprs. */
3841 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3842 /* FALLTHRU */
3844 case TRUTH_NOT_EXPR:
3845 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3847 /* These expressions always produce boolean results. */
3848 if (TREE_CODE (type) != BOOLEAN_TYPE)
3849 TREE_TYPE (expr) = boolean_type_node;
3850 return expr;
3852 case ANNOTATE_EXPR:
3853 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3855 case annot_expr_ivdep_kind:
3856 case annot_expr_unroll_kind:
3857 case annot_expr_no_vector_kind:
3858 case annot_expr_vector_kind:
3859 case annot_expr_parallel_kind:
3860 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3861 if (TREE_CODE (type) != BOOLEAN_TYPE)
3862 TREE_TYPE (expr) = boolean_type_node;
3863 return expr;
3864 default:
3865 gcc_unreachable ();
3868 default:
3869 if (COMPARISON_CLASS_P (expr))
3871 /* There expressions always prduce boolean results. */
3872 if (TREE_CODE (type) != BOOLEAN_TYPE)
3873 TREE_TYPE (expr) = boolean_type_node;
3874 return expr;
3876 /* Other expressions that get here must have boolean values, but
3877 might need to be converted to the appropriate mode. */
3878 if (TREE_CODE (type) == BOOLEAN_TYPE)
3879 return expr;
3880 return fold_convert_loc (loc, boolean_type_node, expr);
3884 /* Given a conditional expression *EXPR_P without side effects, gimplify
3885 its operands. New statements are inserted to PRE_P. */
3887 static enum gimplify_status
3888 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3890 tree expr = *expr_p, cond;
3891 enum gimplify_status ret, tret;
3892 enum tree_code code;
3894 cond = gimple_boolify (COND_EXPR_COND (expr));
3896 /* We need to handle && and || specially, as their gimplification
3897 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3898 code = TREE_CODE (cond);
3899 if (code == TRUTH_ANDIF_EXPR)
3900 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3901 else if (code == TRUTH_ORIF_EXPR)
3902 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3903 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3904 COND_EXPR_COND (*expr_p) = cond;
3906 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3907 is_gimple_val, fb_rvalue);
3908 ret = MIN (ret, tret);
3909 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3910 is_gimple_val, fb_rvalue);
3912 return MIN (ret, tret);
3915 /* Return true if evaluating EXPR could trap.
3916 EXPR is GENERIC, while tree_could_trap_p can be called
3917 only on GIMPLE. */
3919 static bool
3920 generic_expr_could_trap_p (tree expr)
3922 unsigned i, n;
3924 if (!expr || is_gimple_val (expr))
3925 return false;
3927 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3928 return true;
3930 n = TREE_OPERAND_LENGTH (expr);
3931 for (i = 0; i < n; i++)
3932 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3933 return true;
3935 return false;
3938 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3939 into
3941 if (p) if (p)
3942 t1 = a; a;
3943 else or else
3944 t1 = b; b;
3947 The second form is used when *EXPR_P is of type void.
3949 PRE_P points to the list where side effects that must happen before
3950 *EXPR_P should be stored. */
3952 static enum gimplify_status
3953 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3955 tree expr = *expr_p;
3956 tree type = TREE_TYPE (expr);
3957 location_t loc = EXPR_LOCATION (expr);
3958 tree tmp, arm1, arm2;
3959 enum gimplify_status ret;
3960 tree label_true, label_false, label_cont;
3961 bool have_then_clause_p, have_else_clause_p;
3962 gcond *cond_stmt;
3963 enum tree_code pred_code;
3964 gimple_seq seq = NULL;
3966 /* If this COND_EXPR has a value, copy the values into a temporary within
3967 the arms. */
3968 if (!VOID_TYPE_P (type))
3970 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3971 tree result;
3973 /* If either an rvalue is ok or we do not require an lvalue, create the
3974 temporary. But we cannot do that if the type is addressable. */
3975 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3976 && !TREE_ADDRESSABLE (type))
3978 if (gimplify_ctxp->allow_rhs_cond_expr
3979 /* If either branch has side effects or could trap, it can't be
3980 evaluated unconditionally. */
3981 && !TREE_SIDE_EFFECTS (then_)
3982 && !generic_expr_could_trap_p (then_)
3983 && !TREE_SIDE_EFFECTS (else_)
3984 && !generic_expr_could_trap_p (else_))
3985 return gimplify_pure_cond_expr (expr_p, pre_p);
3987 tmp = create_tmp_var (type, "iftmp");
3988 result = tmp;
3991 /* Otherwise, only create and copy references to the values. */
3992 else
3994 type = build_pointer_type (type);
3996 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3997 then_ = build_fold_addr_expr_loc (loc, then_);
3999 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4000 else_ = build_fold_addr_expr_loc (loc, else_);
4002 expr
4003 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4005 tmp = create_tmp_var (type, "iftmp");
4006 result = build_simple_mem_ref_loc (loc, tmp);
4009 /* Build the new then clause, `tmp = then_;'. But don't build the
4010 assignment if the value is void; in C++ it can be if it's a throw. */
4011 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4012 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4014 /* Similarly, build the new else clause, `tmp = else_;'. */
4015 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4016 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4018 TREE_TYPE (expr) = void_type_node;
4019 recalculate_side_effects (expr);
4021 /* Move the COND_EXPR to the prequeue. */
4022 gimplify_stmt (&expr, pre_p);
4024 *expr_p = result;
4025 return GS_ALL_DONE;
4028 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4029 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4030 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4031 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4033 /* Make sure the condition has BOOLEAN_TYPE. */
4034 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4036 /* Break apart && and || conditions. */
4037 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4038 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4040 expr = shortcut_cond_expr (expr);
4042 if (expr != *expr_p)
4044 *expr_p = expr;
4046 /* We can't rely on gimplify_expr to re-gimplify the expanded
4047 form properly, as cleanups might cause the target labels to be
4048 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4049 set up a conditional context. */
4050 gimple_push_condition ();
4051 gimplify_stmt (expr_p, &seq);
4052 gimple_pop_condition (pre_p);
4053 gimple_seq_add_seq (pre_p, seq);
4055 return GS_ALL_DONE;
4059 /* Now do the normal gimplification. */
4061 /* Gimplify condition. */
4062 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4063 fb_rvalue);
4064 if (ret == GS_ERROR)
4065 return GS_ERROR;
4066 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4068 gimple_push_condition ();
4070 have_then_clause_p = have_else_clause_p = false;
4071 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4072 if (label_true
4073 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4074 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4075 have different locations, otherwise we end up with incorrect
4076 location information on the branches. */
4077 && (optimize
4078 || !EXPR_HAS_LOCATION (expr)
4079 || !rexpr_has_location (label_true)
4080 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4082 have_then_clause_p = true;
4083 label_true = GOTO_DESTINATION (label_true);
4085 else
4086 label_true = create_artificial_label (UNKNOWN_LOCATION);
4087 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4088 if (label_false
4089 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4090 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4091 have different locations, otherwise we end up with incorrect
4092 location information on the branches. */
4093 && (optimize
4094 || !EXPR_HAS_LOCATION (expr)
4095 || !rexpr_has_location (label_false)
4096 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4098 have_else_clause_p = true;
4099 label_false = GOTO_DESTINATION (label_false);
4101 else
4102 label_false = create_artificial_label (UNKNOWN_LOCATION);
4104 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4105 &arm2);
4106 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4107 label_false);
4108 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4109 gimplify_seq_add_stmt (&seq, cond_stmt);
4110 gimple_stmt_iterator gsi = gsi_last (seq);
4111 maybe_fold_stmt (&gsi);
4113 label_cont = NULL_TREE;
4114 if (!have_then_clause_p)
4116 /* For if (...) {} else { code; } put label_true after
4117 the else block. */
4118 if (TREE_OPERAND (expr, 1) == NULL_TREE
4119 && !have_else_clause_p
4120 && TREE_OPERAND (expr, 2) != NULL_TREE)
4121 label_cont = label_true;
4122 else
4124 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4125 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4126 /* For if (...) { code; } else {} or
4127 if (...) { code; } else goto label; or
4128 if (...) { code; return; } else { ... }
4129 label_cont isn't needed. */
4130 if (!have_else_clause_p
4131 && TREE_OPERAND (expr, 2) != NULL_TREE
4132 && gimple_seq_may_fallthru (seq))
4134 gimple *g;
4135 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4137 g = gimple_build_goto (label_cont);
4139 /* GIMPLE_COND's are very low level; they have embedded
4140 gotos. This particular embedded goto should not be marked
4141 with the location of the original COND_EXPR, as it would
4142 correspond to the COND_EXPR's condition, not the ELSE or the
4143 THEN arms. To avoid marking it with the wrong location, flag
4144 it as "no location". */
4145 gimple_set_do_not_emit_location (g);
4147 gimplify_seq_add_stmt (&seq, g);
4151 if (!have_else_clause_p)
4153 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4154 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4156 if (label_cont)
4157 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4159 gimple_pop_condition (pre_p);
4160 gimple_seq_add_seq (pre_p, seq);
4162 if (ret == GS_ERROR)
4163 ; /* Do nothing. */
4164 else if (have_then_clause_p || have_else_clause_p)
4165 ret = GS_ALL_DONE;
4166 else
4168 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4169 expr = TREE_OPERAND (expr, 0);
4170 gimplify_stmt (&expr, pre_p);
4173 *expr_p = NULL;
4174 return ret;
4177 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4178 to be marked addressable.
4180 We cannot rely on such an expression being directly markable if a temporary
4181 has been created by the gimplification. In this case, we create another
4182 temporary and initialize it with a copy, which will become a store after we
4183 mark it addressable. This can happen if the front-end passed us something
4184 that it could not mark addressable yet, like a Fortran pass-by-reference
4185 parameter (int) floatvar. */
4187 static void
4188 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4190 while (handled_component_p (*expr_p))
4191 expr_p = &TREE_OPERAND (*expr_p, 0);
4192 if (is_gimple_reg (*expr_p))
4194 /* Do not allow an SSA name as the temporary. */
4195 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4196 DECL_GIMPLE_REG_P (var) = 0;
4197 *expr_p = var;
4201 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4202 a call to __builtin_memcpy. */
4204 static enum gimplify_status
4205 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4206 gimple_seq *seq_p)
4208 tree t, to, to_ptr, from, from_ptr;
4209 gcall *gs;
4210 location_t loc = EXPR_LOCATION (*expr_p);
4212 to = TREE_OPERAND (*expr_p, 0);
4213 from = TREE_OPERAND (*expr_p, 1);
4215 /* Mark the RHS addressable. Beware that it may not be possible to do so
4216 directly if a temporary has been created by the gimplification. */
4217 prepare_gimple_addressable (&from, seq_p);
4219 mark_addressable (from);
4220 from_ptr = build_fold_addr_expr_loc (loc, from);
4221 gimplify_arg (&from_ptr, seq_p, loc);
4223 mark_addressable (to);
4224 to_ptr = build_fold_addr_expr_loc (loc, to);
4225 gimplify_arg (&to_ptr, seq_p, loc);
4227 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4229 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4231 if (want_value)
4233 /* tmp = memcpy() */
4234 t = create_tmp_var (TREE_TYPE (to_ptr));
4235 gimple_call_set_lhs (gs, t);
4236 gimplify_seq_add_stmt (seq_p, gs);
4238 *expr_p = build_simple_mem_ref (t);
4239 return GS_ALL_DONE;
4242 gimplify_seq_add_stmt (seq_p, gs);
4243 *expr_p = NULL;
4244 return GS_ALL_DONE;
4247 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4248 a call to __builtin_memset. In this case we know that the RHS is
4249 a CONSTRUCTOR with an empty element list. */
4251 static enum gimplify_status
4252 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4253 gimple_seq *seq_p)
4255 tree t, from, to, to_ptr;
4256 gcall *gs;
4257 location_t loc = EXPR_LOCATION (*expr_p);
4259 /* Assert our assumptions, to abort instead of producing wrong code
4260 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4261 not be immediately exposed. */
4262 from = TREE_OPERAND (*expr_p, 1);
4263 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4264 from = TREE_OPERAND (from, 0);
4266 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4267 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4269 /* Now proceed. */
4270 to = TREE_OPERAND (*expr_p, 0);
4272 to_ptr = build_fold_addr_expr_loc (loc, to);
4273 gimplify_arg (&to_ptr, seq_p, loc);
4274 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4276 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4278 if (want_value)
4280 /* tmp = memset() */
4281 t = create_tmp_var (TREE_TYPE (to_ptr));
4282 gimple_call_set_lhs (gs, t);
4283 gimplify_seq_add_stmt (seq_p, gs);
4285 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4286 return GS_ALL_DONE;
4289 gimplify_seq_add_stmt (seq_p, gs);
4290 *expr_p = NULL;
4291 return GS_ALL_DONE;
4294 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4295 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4296 assignment. Return non-null if we detect a potential overlap. */
4298 struct gimplify_init_ctor_preeval_data
4300 /* The base decl of the lhs object. May be NULL, in which case we
4301 have to assume the lhs is indirect. */
4302 tree lhs_base_decl;
4304 /* The alias set of the lhs object. */
4305 alias_set_type lhs_alias_set;
4308 static tree
4309 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4311 struct gimplify_init_ctor_preeval_data *data
4312 = (struct gimplify_init_ctor_preeval_data *) xdata;
4313 tree t = *tp;
4315 /* If we find the base object, obviously we have overlap. */
4316 if (data->lhs_base_decl == t)
4317 return t;
4319 /* If the constructor component is indirect, determine if we have a
4320 potential overlap with the lhs. The only bits of information we
4321 have to go on at this point are addressability and alias sets. */
4322 if ((INDIRECT_REF_P (t)
4323 || TREE_CODE (t) == MEM_REF)
4324 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4325 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4326 return t;
4328 /* If the constructor component is a call, determine if it can hide a
4329 potential overlap with the lhs through an INDIRECT_REF like above.
4330 ??? Ugh - this is completely broken. In fact this whole analysis
4331 doesn't look conservative. */
4332 if (TREE_CODE (t) == CALL_EXPR)
4334 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4336 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4337 if (POINTER_TYPE_P (TREE_VALUE (type))
4338 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4339 && alias_sets_conflict_p (data->lhs_alias_set,
4340 get_alias_set
4341 (TREE_TYPE (TREE_VALUE (type)))))
4342 return t;
4345 if (IS_TYPE_OR_DECL_P (t))
4346 *walk_subtrees = 0;
4347 return NULL;
4350 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4351 force values that overlap with the lhs (as described by *DATA)
4352 into temporaries. */
4354 static void
4355 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4356 struct gimplify_init_ctor_preeval_data *data)
4358 enum gimplify_status one;
4360 /* If the value is constant, then there's nothing to pre-evaluate. */
4361 if (TREE_CONSTANT (*expr_p))
4363 /* Ensure it does not have side effects, it might contain a reference to
4364 the object we're initializing. */
4365 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4366 return;
4369 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4370 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4371 return;
4373 /* Recurse for nested constructors. */
4374 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4376 unsigned HOST_WIDE_INT ix;
4377 constructor_elt *ce;
4378 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4380 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4381 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4383 return;
4386 /* If this is a variable sized type, we must remember the size. */
4387 maybe_with_size_expr (expr_p);
4389 /* Gimplify the constructor element to something appropriate for the rhs
4390 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4391 the gimplifier will consider this a store to memory. Doing this
4392 gimplification now means that we won't have to deal with complicated
4393 language-specific trees, nor trees like SAVE_EXPR that can induce
4394 exponential search behavior. */
4395 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4396 if (one == GS_ERROR)
4398 *expr_p = NULL;
4399 return;
4402 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4403 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4404 always be true for all scalars, since is_gimple_mem_rhs insists on a
4405 temporary variable for them. */
4406 if (DECL_P (*expr_p))
4407 return;
4409 /* If this is of variable size, we have no choice but to assume it doesn't
4410 overlap since we can't make a temporary for it. */
4411 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4412 return;
4414 /* Otherwise, we must search for overlap ... */
4415 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4416 return;
4418 /* ... and if found, force the value into a temporary. */
4419 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4422 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4423 a RANGE_EXPR in a CONSTRUCTOR for an array.
4425 var = lower;
4426 loop_entry:
4427 object[var] = value;
4428 if (var == upper)
4429 goto loop_exit;
4430 var = var + 1;
4431 goto loop_entry;
4432 loop_exit:
4434 We increment var _after_ the loop exit check because we might otherwise
4435 fail if upper == TYPE_MAX_VALUE (type for upper).
4437 Note that we never have to deal with SAVE_EXPRs here, because this has
4438 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4440 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4441 gimple_seq *, bool);
4443 static void
4444 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4445 tree value, tree array_elt_type,
4446 gimple_seq *pre_p, bool cleared)
4448 tree loop_entry_label, loop_exit_label, fall_thru_label;
4449 tree var, var_type, cref, tmp;
4451 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4452 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4453 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4455 /* Create and initialize the index variable. */
4456 var_type = TREE_TYPE (upper);
4457 var = create_tmp_var (var_type);
4458 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4460 /* Add the loop entry label. */
4461 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4463 /* Build the reference. */
4464 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4465 var, NULL_TREE, NULL_TREE);
4467 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4468 the store. Otherwise just assign value to the reference. */
4470 if (TREE_CODE (value) == CONSTRUCTOR)
4471 /* NB we might have to call ourself recursively through
4472 gimplify_init_ctor_eval if the value is a constructor. */
4473 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4474 pre_p, cleared);
4475 else
4476 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4478 /* We exit the loop when the index var is equal to the upper bound. */
4479 gimplify_seq_add_stmt (pre_p,
4480 gimple_build_cond (EQ_EXPR, var, upper,
4481 loop_exit_label, fall_thru_label));
4483 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4485 /* Otherwise, increment the index var... */
4486 tmp = build2 (PLUS_EXPR, var_type, var,
4487 fold_convert (var_type, integer_one_node));
4488 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4490 /* ...and jump back to the loop entry. */
4491 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4493 /* Add the loop exit label. */
4494 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4497 /* Return true if FDECL is accessing a field that is zero sized. */
4499 static bool
4500 zero_sized_field_decl (const_tree fdecl)
4502 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4503 && integer_zerop (DECL_SIZE (fdecl)))
4504 return true;
4505 return false;
4508 /* Return true if TYPE is zero sized. */
4510 static bool
4511 zero_sized_type (const_tree type)
4513 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4514 && integer_zerop (TYPE_SIZE (type)))
4515 return true;
4516 return false;
4519 /* A subroutine of gimplify_init_constructor. Generate individual
4520 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4521 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4522 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4523 zeroed first. */
4525 static void
4526 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4527 gimple_seq *pre_p, bool cleared)
4529 tree array_elt_type = NULL;
4530 unsigned HOST_WIDE_INT ix;
4531 tree purpose, value;
4533 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4534 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4536 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4538 tree cref;
4540 /* NULL values are created above for gimplification errors. */
4541 if (value == NULL)
4542 continue;
4544 if (cleared && initializer_zerop (value))
4545 continue;
4547 /* ??? Here's to hoping the front end fills in all of the indices,
4548 so we don't have to figure out what's missing ourselves. */
4549 gcc_assert (purpose);
4551 /* Skip zero-sized fields, unless value has side-effects. This can
4552 happen with calls to functions returning a zero-sized type, which
4553 we shouldn't discard. As a number of downstream passes don't
4554 expect sets of zero-sized fields, we rely on the gimplification of
4555 the MODIFY_EXPR we make below to drop the assignment statement. */
4556 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4557 continue;
4559 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4560 whole range. */
4561 if (TREE_CODE (purpose) == RANGE_EXPR)
4563 tree lower = TREE_OPERAND (purpose, 0);
4564 tree upper = TREE_OPERAND (purpose, 1);
4566 /* If the lower bound is equal to upper, just treat it as if
4567 upper was the index. */
4568 if (simple_cst_equal (lower, upper))
4569 purpose = upper;
4570 else
4572 gimplify_init_ctor_eval_range (object, lower, upper, value,
4573 array_elt_type, pre_p, cleared);
4574 continue;
4578 if (array_elt_type)
4580 /* Do not use bitsizetype for ARRAY_REF indices. */
4581 if (TYPE_DOMAIN (TREE_TYPE (object)))
4582 purpose
4583 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4584 purpose);
4585 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4586 purpose, NULL_TREE, NULL_TREE);
4588 else
4590 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4591 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4592 unshare_expr (object), purpose, NULL_TREE);
4595 if (TREE_CODE (value) == CONSTRUCTOR
4596 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4597 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4598 pre_p, cleared);
4599 else
4601 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4602 gimplify_and_add (init, pre_p);
4603 ggc_free (init);
4608 /* Return the appropriate RHS predicate for this LHS. */
4610 gimple_predicate
4611 rhs_predicate_for (tree lhs)
4613 if (is_gimple_reg (lhs))
4614 return is_gimple_reg_rhs_or_call;
4615 else
4616 return is_gimple_mem_rhs_or_call;
4619 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4620 before the LHS has been gimplified. */
4622 static gimple_predicate
4623 initial_rhs_predicate_for (tree lhs)
4625 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4626 return is_gimple_reg_rhs_or_call;
4627 else
4628 return is_gimple_mem_rhs_or_call;
4631 /* Gimplify a C99 compound literal expression. This just means adding
4632 the DECL_EXPR before the current statement and using its anonymous
4633 decl instead. */
4635 static enum gimplify_status
4636 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4637 bool (*gimple_test_f) (tree),
4638 fallback_t fallback)
4640 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4641 tree decl = DECL_EXPR_DECL (decl_s);
4642 tree init = DECL_INITIAL (decl);
4643 /* Mark the decl as addressable if the compound literal
4644 expression is addressable now, otherwise it is marked too late
4645 after we gimplify the initialization expression. */
4646 if (TREE_ADDRESSABLE (*expr_p))
4647 TREE_ADDRESSABLE (decl) = 1;
4648 /* Otherwise, if we don't need an lvalue and have a literal directly
4649 substitute it. Check if it matches the gimple predicate, as
4650 otherwise we'd generate a new temporary, and we can as well just
4651 use the decl we already have. */
4652 else if (!TREE_ADDRESSABLE (decl)
4653 && init
4654 && (fallback & fb_lvalue) == 0
4655 && gimple_test_f (init))
4657 *expr_p = init;
4658 return GS_OK;
4661 /* Preliminarily mark non-addressed complex variables as eligible
4662 for promotion to gimple registers. We'll transform their uses
4663 as we find them. */
4664 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4665 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4666 && !TREE_THIS_VOLATILE (decl)
4667 && !needs_to_live_in_memory (decl))
4668 DECL_GIMPLE_REG_P (decl) = 1;
4670 /* If the decl is not addressable, then it is being used in some
4671 expression or on the right hand side of a statement, and it can
4672 be put into a readonly data section. */
4673 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4674 TREE_READONLY (decl) = 1;
4676 /* This decl isn't mentioned in the enclosing block, so add it to the
4677 list of temps. FIXME it seems a bit of a kludge to say that
4678 anonymous artificial vars aren't pushed, but everything else is. */
4679 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4680 gimple_add_tmp_var (decl);
4682 gimplify_and_add (decl_s, pre_p);
4683 *expr_p = decl;
4684 return GS_OK;
4687 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4688 return a new CONSTRUCTOR if something changed. */
4690 static tree
4691 optimize_compound_literals_in_ctor (tree orig_ctor)
4693 tree ctor = orig_ctor;
4694 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4695 unsigned int idx, num = vec_safe_length (elts);
4697 for (idx = 0; idx < num; idx++)
4699 tree value = (*elts)[idx].value;
4700 tree newval = value;
4701 if (TREE_CODE (value) == CONSTRUCTOR)
4702 newval = optimize_compound_literals_in_ctor (value);
4703 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4705 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4706 tree decl = DECL_EXPR_DECL (decl_s);
4707 tree init = DECL_INITIAL (decl);
4709 if (!TREE_ADDRESSABLE (value)
4710 && !TREE_ADDRESSABLE (decl)
4711 && init
4712 && TREE_CODE (init) == CONSTRUCTOR)
4713 newval = optimize_compound_literals_in_ctor (init);
4715 if (newval == value)
4716 continue;
4718 if (ctor == orig_ctor)
4720 ctor = copy_node (orig_ctor);
4721 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4722 elts = CONSTRUCTOR_ELTS (ctor);
4724 (*elts)[idx].value = newval;
4726 return ctor;
4729 /* A subroutine of gimplify_modify_expr. Break out elements of a
4730 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4732 Note that we still need to clear any elements that don't have explicit
4733 initializers, so if not all elements are initialized we keep the
4734 original MODIFY_EXPR, we just remove all of the constructor elements.
4736 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4737 GS_ERROR if we would have to create a temporary when gimplifying
4738 this constructor. Otherwise, return GS_OK.
4740 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4742 static enum gimplify_status
4743 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4744 bool want_value, bool notify_temp_creation)
4746 tree object, ctor, type;
4747 enum gimplify_status ret;
4748 vec<constructor_elt, va_gc> *elts;
4750 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4752 if (!notify_temp_creation)
4754 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4755 is_gimple_lvalue, fb_lvalue);
4756 if (ret == GS_ERROR)
4757 return ret;
4760 object = TREE_OPERAND (*expr_p, 0);
4761 ctor = TREE_OPERAND (*expr_p, 1)
4762 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4763 type = TREE_TYPE (ctor);
4764 elts = CONSTRUCTOR_ELTS (ctor);
4765 ret = GS_ALL_DONE;
4767 switch (TREE_CODE (type))
4769 case RECORD_TYPE:
4770 case UNION_TYPE:
4771 case QUAL_UNION_TYPE:
4772 case ARRAY_TYPE:
4774 struct gimplify_init_ctor_preeval_data preeval_data;
4775 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4776 HOST_WIDE_INT num_unique_nonzero_elements;
4777 bool cleared, complete_p, valid_const_initializer;
4778 /* Use readonly data for initializers of this or smaller size
4779 regardless of the num_nonzero_elements / num_unique_nonzero_elements
4780 ratio. */
4781 const HOST_WIDE_INT min_unique_size = 64;
4782 /* If num_nonzero_elements / num_unique_nonzero_elements ratio
4783 is smaller than this, use readonly data. */
4784 const int unique_nonzero_ratio = 8;
4786 /* Aggregate types must lower constructors to initialization of
4787 individual elements. The exception is that a CONSTRUCTOR node
4788 with no elements indicates zero-initialization of the whole. */
4789 if (vec_safe_is_empty (elts))
4791 if (notify_temp_creation)
4792 return GS_OK;
4793 break;
4796 /* Fetch information about the constructor to direct later processing.
4797 We might want to make static versions of it in various cases, and
4798 can only do so if it known to be a valid constant initializer. */
4799 valid_const_initializer
4800 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4801 &num_unique_nonzero_elements,
4802 &num_ctor_elements, &complete_p);
4804 /* If a const aggregate variable is being initialized, then it
4805 should never be a lose to promote the variable to be static. */
4806 if (valid_const_initializer
4807 && num_nonzero_elements > 1
4808 && TREE_READONLY (object)
4809 && VAR_P (object)
4810 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object))
4811 /* For ctors that have many repeated nonzero elements
4812 represented through RANGE_EXPRs, prefer initializing
4813 those through runtime loops over copies of large amounts
4814 of data from readonly data section. */
4815 && (num_unique_nonzero_elements
4816 > num_nonzero_elements / unique_nonzero_ratio
4817 || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
4818 <= (unsigned HOST_WIDE_INT) min_unique_size)))
4820 if (notify_temp_creation)
4821 return GS_ERROR;
4822 DECL_INITIAL (object) = ctor;
4823 TREE_STATIC (object) = 1;
4824 if (!DECL_NAME (object))
4825 DECL_NAME (object) = create_tmp_var_name ("C");
4826 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4828 /* ??? C++ doesn't automatically append a .<number> to the
4829 assembler name, and even when it does, it looks at FE private
4830 data structures to figure out what that number should be,
4831 which are not set for this variable. I suppose this is
4832 important for local statics for inline functions, which aren't
4833 "local" in the object file sense. So in order to get a unique
4834 TU-local symbol, we must invoke the lhd version now. */
4835 lhd_set_decl_assembler_name (object);
4837 *expr_p = NULL_TREE;
4838 break;
4841 /* If there are "lots" of initialized elements, even discounting
4842 those that are not address constants (and thus *must* be
4843 computed at runtime), then partition the constructor into
4844 constant and non-constant parts. Block copy the constant
4845 parts in, then generate code for the non-constant parts. */
4846 /* TODO. There's code in cp/typeck.c to do this. */
4848 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4849 /* store_constructor will ignore the clearing of variable-sized
4850 objects. Initializers for such objects must explicitly set
4851 every field that needs to be set. */
4852 cleared = false;
4853 else if (!complete_p)
4854 /* If the constructor isn't complete, clear the whole object
4855 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4857 ??? This ought not to be needed. For any element not present
4858 in the initializer, we should simply set them to zero. Except
4859 we'd need to *find* the elements that are not present, and that
4860 requires trickery to avoid quadratic compile-time behavior in
4861 large cases or excessive memory use in small cases. */
4862 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4863 else if (num_ctor_elements - num_nonzero_elements
4864 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4865 && num_nonzero_elements < num_ctor_elements / 4)
4866 /* If there are "lots" of zeros, it's more efficient to clear
4867 the memory and then set the nonzero elements. */
4868 cleared = true;
4869 else
4870 cleared = false;
4872 /* If there are "lots" of initialized elements, and all of them
4873 are valid address constants, then the entire initializer can
4874 be dropped to memory, and then memcpy'd out. Don't do this
4875 for sparse arrays, though, as it's more efficient to follow
4876 the standard CONSTRUCTOR behavior of memset followed by
4877 individual element initialization. Also don't do this for small
4878 all-zero initializers (which aren't big enough to merit
4879 clearing), and don't try to make bitwise copies of
4880 TREE_ADDRESSABLE types.
4882 We cannot apply such transformation when compiling chkp static
4883 initializer because creation of initializer image in the memory
4884 will require static initialization of bounds for it. It should
4885 result in another gimplification of similar initializer and we
4886 may fall into infinite loop. */
4887 if (valid_const_initializer
4888 && !(cleared || num_nonzero_elements == 0)
4889 && !TREE_ADDRESSABLE (type)
4890 && (!current_function_decl
4891 || !lookup_attribute ("chkp ctor",
4892 DECL_ATTRIBUTES (current_function_decl))))
4894 HOST_WIDE_INT size = int_size_in_bytes (type);
4895 unsigned int align;
4897 /* ??? We can still get unbounded array types, at least
4898 from the C++ front end. This seems wrong, but attempt
4899 to work around it for now. */
4900 if (size < 0)
4902 size = int_size_in_bytes (TREE_TYPE (object));
4903 if (size >= 0)
4904 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4907 /* Find the maximum alignment we can assume for the object. */
4908 /* ??? Make use of DECL_OFFSET_ALIGN. */
4909 if (DECL_P (object))
4910 align = DECL_ALIGN (object);
4911 else
4912 align = TYPE_ALIGN (type);
4914 /* Do a block move either if the size is so small as to make
4915 each individual move a sub-unit move on average, or if it
4916 is so large as to make individual moves inefficient. */
4917 if (size > 0
4918 && num_nonzero_elements > 1
4919 /* For ctors that have many repeated nonzero elements
4920 represented through RANGE_EXPRs, prefer initializing
4921 those through runtime loops over copies of large amounts
4922 of data from readonly data section. */
4923 && (num_unique_nonzero_elements
4924 > num_nonzero_elements / unique_nonzero_ratio
4925 || size <= min_unique_size)
4926 && (size < num_nonzero_elements
4927 || !can_move_by_pieces (size, align)))
4929 if (notify_temp_creation)
4930 return GS_ERROR;
4932 walk_tree (&ctor, force_labels_r, NULL, NULL);
4933 ctor = tree_output_constant_def (ctor);
4934 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4935 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4936 TREE_OPERAND (*expr_p, 1) = ctor;
4938 /* This is no longer an assignment of a CONSTRUCTOR, but
4939 we still may have processing to do on the LHS. So
4940 pretend we didn't do anything here to let that happen. */
4941 return GS_UNHANDLED;
4945 /* If the target is volatile, we have non-zero elements and more than
4946 one field to assign, initialize the target from a temporary. */
4947 if (TREE_THIS_VOLATILE (object)
4948 && !TREE_ADDRESSABLE (type)
4949 && num_nonzero_elements > 0
4950 && vec_safe_length (elts) > 1)
4952 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4953 TREE_OPERAND (*expr_p, 0) = temp;
4954 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4955 *expr_p,
4956 build2 (MODIFY_EXPR, void_type_node,
4957 object, temp));
4958 return GS_OK;
4961 if (notify_temp_creation)
4962 return GS_OK;
4964 /* If there are nonzero elements and if needed, pre-evaluate to capture
4965 elements overlapping with the lhs into temporaries. We must do this
4966 before clearing to fetch the values before they are zeroed-out. */
4967 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4969 preeval_data.lhs_base_decl = get_base_address (object);
4970 if (!DECL_P (preeval_data.lhs_base_decl))
4971 preeval_data.lhs_base_decl = NULL;
4972 preeval_data.lhs_alias_set = get_alias_set (object);
4974 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4975 pre_p, post_p, &preeval_data);
4978 bool ctor_has_side_effects_p
4979 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4981 if (cleared)
4983 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4984 Note that we still have to gimplify, in order to handle the
4985 case of variable sized types. Avoid shared tree structures. */
4986 CONSTRUCTOR_ELTS (ctor) = NULL;
4987 TREE_SIDE_EFFECTS (ctor) = 0;
4988 object = unshare_expr (object);
4989 gimplify_stmt (expr_p, pre_p);
4992 /* If we have not block cleared the object, or if there are nonzero
4993 elements in the constructor, or if the constructor has side effects,
4994 add assignments to the individual scalar fields of the object. */
4995 if (!cleared
4996 || num_nonzero_elements > 0
4997 || ctor_has_side_effects_p)
4998 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
5000 *expr_p = NULL_TREE;
5002 break;
5004 case COMPLEX_TYPE:
5006 tree r, i;
5008 if (notify_temp_creation)
5009 return GS_OK;
5011 /* Extract the real and imaginary parts out of the ctor. */
5012 gcc_assert (elts->length () == 2);
5013 r = (*elts)[0].value;
5014 i = (*elts)[1].value;
5015 if (r == NULL || i == NULL)
5017 tree zero = build_zero_cst (TREE_TYPE (type));
5018 if (r == NULL)
5019 r = zero;
5020 if (i == NULL)
5021 i = zero;
5024 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5025 represent creation of a complex value. */
5026 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5028 ctor = build_complex (type, r, i);
5029 TREE_OPERAND (*expr_p, 1) = ctor;
5031 else
5033 ctor = build2 (COMPLEX_EXPR, type, r, i);
5034 TREE_OPERAND (*expr_p, 1) = ctor;
5035 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5036 pre_p,
5037 post_p,
5038 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5039 fb_rvalue);
5042 break;
5044 case VECTOR_TYPE:
5046 unsigned HOST_WIDE_INT ix;
5047 constructor_elt *ce;
5049 if (notify_temp_creation)
5050 return GS_OK;
5052 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5053 if (TREE_CONSTANT (ctor))
5055 bool constant_p = true;
5056 tree value;
5058 /* Even when ctor is constant, it might contain non-*_CST
5059 elements, such as addresses or trapping values like
5060 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5061 in VECTOR_CST nodes. */
5062 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5063 if (!CONSTANT_CLASS_P (value))
5065 constant_p = false;
5066 break;
5069 if (constant_p)
5071 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5072 break;
5075 TREE_CONSTANT (ctor) = 0;
5078 /* Vector types use CONSTRUCTOR all the way through gimple
5079 compilation as a general initializer. */
5080 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5082 enum gimplify_status tret;
5083 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5084 fb_rvalue);
5085 if (tret == GS_ERROR)
5086 ret = GS_ERROR;
5087 else if (TREE_STATIC (ctor)
5088 && !initializer_constant_valid_p (ce->value,
5089 TREE_TYPE (ce->value)))
5090 TREE_STATIC (ctor) = 0;
5092 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5093 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5095 break;
5097 default:
5098 /* So how did we get a CONSTRUCTOR for a scalar type? */
5099 gcc_unreachable ();
5102 if (ret == GS_ERROR)
5103 return GS_ERROR;
5104 /* If we have gimplified both sides of the initializer but have
5105 not emitted an assignment, do so now. */
5106 if (*expr_p)
5108 tree lhs = TREE_OPERAND (*expr_p, 0);
5109 tree rhs = TREE_OPERAND (*expr_p, 1);
5110 if (want_value && object == lhs)
5111 lhs = unshare_expr (lhs);
5112 gassign *init = gimple_build_assign (lhs, rhs);
5113 gimplify_seq_add_stmt (pre_p, init);
5115 if (want_value)
5117 *expr_p = object;
5118 return GS_OK;
5120 else
5122 *expr_p = NULL;
5123 return GS_ALL_DONE;
5127 /* Given a pointer value OP0, return a simplified version of an
5128 indirection through OP0, or NULL_TREE if no simplification is
5129 possible. This may only be applied to a rhs of an expression.
5130 Note that the resulting type may be different from the type pointed
5131 to in the sense that it is still compatible from the langhooks
5132 point of view. */
5134 static tree
5135 gimple_fold_indirect_ref_rhs (tree t)
5137 return gimple_fold_indirect_ref (t);
5140 /* Subroutine of gimplify_modify_expr to do simplifications of
5141 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5142 something changes. */
5144 static enum gimplify_status
5145 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5146 gimple_seq *pre_p, gimple_seq *post_p,
5147 bool want_value)
5149 enum gimplify_status ret = GS_UNHANDLED;
5150 bool changed;
5154 changed = false;
5155 switch (TREE_CODE (*from_p))
5157 case VAR_DECL:
5158 /* If we're assigning from a read-only variable initialized with
5159 a constructor, do the direct assignment from the constructor,
5160 but only if neither source nor target are volatile since this
5161 latter assignment might end up being done on a per-field basis. */
5162 if (DECL_INITIAL (*from_p)
5163 && TREE_READONLY (*from_p)
5164 && !TREE_THIS_VOLATILE (*from_p)
5165 && !TREE_THIS_VOLATILE (*to_p)
5166 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5168 tree old_from = *from_p;
5169 enum gimplify_status subret;
5171 /* Move the constructor into the RHS. */
5172 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5174 /* Let's see if gimplify_init_constructor will need to put
5175 it in memory. */
5176 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5177 false, true);
5178 if (subret == GS_ERROR)
5180 /* If so, revert the change. */
5181 *from_p = old_from;
5183 else
5185 ret = GS_OK;
5186 changed = true;
5189 break;
5190 case INDIRECT_REF:
5192 /* If we have code like
5194 *(const A*)(A*)&x
5196 where the type of "x" is a (possibly cv-qualified variant
5197 of "A"), treat the entire expression as identical to "x".
5198 This kind of code arises in C++ when an object is bound
5199 to a const reference, and if "x" is a TARGET_EXPR we want
5200 to take advantage of the optimization below. */
5201 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5202 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5203 if (t)
5205 if (TREE_THIS_VOLATILE (t) != volatile_p)
5207 if (DECL_P (t))
5208 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5209 build_fold_addr_expr (t));
5210 if (REFERENCE_CLASS_P (t))
5211 TREE_THIS_VOLATILE (t) = volatile_p;
5213 *from_p = t;
5214 ret = GS_OK;
5215 changed = true;
5217 break;
5220 case TARGET_EXPR:
5222 /* If we are initializing something from a TARGET_EXPR, strip the
5223 TARGET_EXPR and initialize it directly, if possible. This can't
5224 be done if the initializer is void, since that implies that the
5225 temporary is set in some non-trivial way.
5227 ??? What about code that pulls out the temp and uses it
5228 elsewhere? I think that such code never uses the TARGET_EXPR as
5229 an initializer. If I'm wrong, we'll die because the temp won't
5230 have any RTL. In that case, I guess we'll need to replace
5231 references somehow. */
5232 tree init = TARGET_EXPR_INITIAL (*from_p);
5234 if (init
5235 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5236 || !TARGET_EXPR_NO_ELIDE (*from_p))
5237 && !VOID_TYPE_P (TREE_TYPE (init)))
5239 *from_p = init;
5240 ret = GS_OK;
5241 changed = true;
5244 break;
5246 case COMPOUND_EXPR:
5247 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5248 caught. */
5249 gimplify_compound_expr (from_p, pre_p, true);
5250 ret = GS_OK;
5251 changed = true;
5252 break;
5254 case CONSTRUCTOR:
5255 /* If we already made some changes, let the front end have a
5256 crack at this before we break it down. */
5257 if (ret != GS_UNHANDLED)
5258 break;
5259 /* If we're initializing from a CONSTRUCTOR, break this into
5260 individual MODIFY_EXPRs. */
5261 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5262 false);
5264 case COND_EXPR:
5265 /* If we're assigning to a non-register type, push the assignment
5266 down into the branches. This is mandatory for ADDRESSABLE types,
5267 since we cannot generate temporaries for such, but it saves a
5268 copy in other cases as well. */
5269 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5271 /* This code should mirror the code in gimplify_cond_expr. */
5272 enum tree_code code = TREE_CODE (*expr_p);
5273 tree cond = *from_p;
5274 tree result = *to_p;
5276 ret = gimplify_expr (&result, pre_p, post_p,
5277 is_gimple_lvalue, fb_lvalue);
5278 if (ret != GS_ERROR)
5279 ret = GS_OK;
5281 /* If we are going to write RESULT more than once, clear
5282 TREE_READONLY flag, otherwise we might incorrectly promote
5283 the variable to static const and initialize it at compile
5284 time in one of the branches. */
5285 if (VAR_P (result)
5286 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5287 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5288 TREE_READONLY (result) = 0;
5289 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5290 TREE_OPERAND (cond, 1)
5291 = build2 (code, void_type_node, result,
5292 TREE_OPERAND (cond, 1));
5293 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5294 TREE_OPERAND (cond, 2)
5295 = build2 (code, void_type_node, unshare_expr (result),
5296 TREE_OPERAND (cond, 2));
5298 TREE_TYPE (cond) = void_type_node;
5299 recalculate_side_effects (cond);
5301 if (want_value)
5303 gimplify_and_add (cond, pre_p);
5304 *expr_p = unshare_expr (result);
5306 else
5307 *expr_p = cond;
5308 return ret;
5310 break;
5312 case CALL_EXPR:
5313 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5314 return slot so that we don't generate a temporary. */
5315 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5316 && aggregate_value_p (*from_p, *from_p))
5318 bool use_target;
5320 if (!(rhs_predicate_for (*to_p))(*from_p))
5321 /* If we need a temporary, *to_p isn't accurate. */
5322 use_target = false;
5323 /* It's OK to use the return slot directly unless it's an NRV. */
5324 else if (TREE_CODE (*to_p) == RESULT_DECL
5325 && DECL_NAME (*to_p) == NULL_TREE
5326 && needs_to_live_in_memory (*to_p))
5327 use_target = true;
5328 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5329 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5330 /* Don't force regs into memory. */
5331 use_target = false;
5332 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5333 /* It's OK to use the target directly if it's being
5334 initialized. */
5335 use_target = true;
5336 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5337 != INTEGER_CST)
5338 /* Always use the target and thus RSO for variable-sized types.
5339 GIMPLE cannot deal with a variable-sized assignment
5340 embedded in a call statement. */
5341 use_target = true;
5342 else if (TREE_CODE (*to_p) != SSA_NAME
5343 && (!is_gimple_variable (*to_p)
5344 || needs_to_live_in_memory (*to_p)))
5345 /* Don't use the original target if it's already addressable;
5346 if its address escapes, and the called function uses the
5347 NRV optimization, a conforming program could see *to_p
5348 change before the called function returns; see c++/19317.
5349 When optimizing, the return_slot pass marks more functions
5350 as safe after we have escape info. */
5351 use_target = false;
5352 else
5353 use_target = true;
5355 if (use_target)
5357 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5358 mark_addressable (*to_p);
5361 break;
5363 case WITH_SIZE_EXPR:
5364 /* Likewise for calls that return an aggregate of non-constant size,
5365 since we would not be able to generate a temporary at all. */
5366 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5368 *from_p = TREE_OPERAND (*from_p, 0);
5369 /* We don't change ret in this case because the
5370 WITH_SIZE_EXPR might have been added in
5371 gimplify_modify_expr, so returning GS_OK would lead to an
5372 infinite loop. */
5373 changed = true;
5375 break;
5377 /* If we're initializing from a container, push the initialization
5378 inside it. */
5379 case CLEANUP_POINT_EXPR:
5380 case BIND_EXPR:
5381 case STATEMENT_LIST:
5383 tree wrap = *from_p;
5384 tree t;
5386 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5387 fb_lvalue);
5388 if (ret != GS_ERROR)
5389 ret = GS_OK;
5391 t = voidify_wrapper_expr (wrap, *expr_p);
5392 gcc_assert (t == *expr_p);
5394 if (want_value)
5396 gimplify_and_add (wrap, pre_p);
5397 *expr_p = unshare_expr (*to_p);
5399 else
5400 *expr_p = wrap;
5401 return GS_OK;
5404 case COMPOUND_LITERAL_EXPR:
5406 tree complit = TREE_OPERAND (*expr_p, 1);
5407 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5408 tree decl = DECL_EXPR_DECL (decl_s);
5409 tree init = DECL_INITIAL (decl);
5411 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5412 into struct T x = { 0, 1, 2 } if the address of the
5413 compound literal has never been taken. */
5414 if (!TREE_ADDRESSABLE (complit)
5415 && !TREE_ADDRESSABLE (decl)
5416 && init)
5418 *expr_p = copy_node (*expr_p);
5419 TREE_OPERAND (*expr_p, 1) = init;
5420 return GS_OK;
5424 default:
5425 break;
5428 while (changed);
5430 return ret;
5434 /* Return true if T looks like a valid GIMPLE statement. */
5436 static bool
5437 is_gimple_stmt (tree t)
5439 const enum tree_code code = TREE_CODE (t);
5441 switch (code)
5443 case NOP_EXPR:
5444 /* The only valid NOP_EXPR is the empty statement. */
5445 return IS_EMPTY_STMT (t);
5447 case BIND_EXPR:
5448 case COND_EXPR:
5449 /* These are only valid if they're void. */
5450 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5452 case SWITCH_EXPR:
5453 case GOTO_EXPR:
5454 case RETURN_EXPR:
5455 case LABEL_EXPR:
5456 case CASE_LABEL_EXPR:
5457 case TRY_CATCH_EXPR:
5458 case TRY_FINALLY_EXPR:
5459 case EH_FILTER_EXPR:
5460 case CATCH_EXPR:
5461 case ASM_EXPR:
5462 case STATEMENT_LIST:
5463 case OACC_PARALLEL:
5464 case OACC_KERNELS:
5465 case OACC_DATA:
5466 case OACC_HOST_DATA:
5467 case OACC_DECLARE:
5468 case OACC_UPDATE:
5469 case OACC_ENTER_DATA:
5470 case OACC_EXIT_DATA:
5471 case OACC_CACHE:
5472 case OMP_PARALLEL:
5473 case OMP_FOR:
5474 case OMP_SIMD:
5475 case OMP_DISTRIBUTE:
5476 case OACC_LOOP:
5477 case OMP_SECTIONS:
5478 case OMP_SECTION:
5479 case OMP_SINGLE:
5480 case OMP_MASTER:
5481 case OMP_TASKGROUP:
5482 case OMP_ORDERED:
5483 case OMP_CRITICAL:
5484 case OMP_TASK:
5485 case OMP_TARGET:
5486 case OMP_TARGET_DATA:
5487 case OMP_TARGET_UPDATE:
5488 case OMP_TARGET_ENTER_DATA:
5489 case OMP_TARGET_EXIT_DATA:
5490 case OMP_TASKLOOP:
5491 case OMP_TEAMS:
5492 /* These are always void. */
5493 return true;
5495 case CALL_EXPR:
5496 case MODIFY_EXPR:
5497 case PREDICT_EXPR:
5498 /* These are valid regardless of their type. */
5499 return true;
5501 default:
5502 return false;
5507 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5508 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5509 DECL_GIMPLE_REG_P set.
5511 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5512 other, unmodified part of the complex object just before the total store.
5513 As a consequence, if the object is still uninitialized, an undefined value
5514 will be loaded into a register, which may result in a spurious exception
5515 if the register is floating-point and the value happens to be a signaling
5516 NaN for example. Then the fully-fledged complex operations lowering pass
5517 followed by a DCE pass are necessary in order to fix things up. */
5519 static enum gimplify_status
5520 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5521 bool want_value)
5523 enum tree_code code, ocode;
5524 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5526 lhs = TREE_OPERAND (*expr_p, 0);
5527 rhs = TREE_OPERAND (*expr_p, 1);
5528 code = TREE_CODE (lhs);
5529 lhs = TREE_OPERAND (lhs, 0);
5531 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5532 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5533 TREE_NO_WARNING (other) = 1;
5534 other = get_formal_tmp_var (other, pre_p);
5536 realpart = code == REALPART_EXPR ? rhs : other;
5537 imagpart = code == REALPART_EXPR ? other : rhs;
5539 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5540 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5541 else
5542 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5544 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5545 *expr_p = (want_value) ? rhs : NULL_TREE;
5547 return GS_ALL_DONE;
5550 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5552 modify_expr
5553 : varname '=' rhs
5554 | '*' ID '=' rhs
5556 PRE_P points to the list where side effects that must happen before
5557 *EXPR_P should be stored.
5559 POST_P points to the list where side effects that must happen after
5560 *EXPR_P should be stored.
5562 WANT_VALUE is nonzero iff we want to use the value of this expression
5563 in another expression. */
5565 static enum gimplify_status
5566 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5567 bool want_value)
5569 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5570 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5571 enum gimplify_status ret = GS_UNHANDLED;
5572 gimple *assign;
5573 location_t loc = EXPR_LOCATION (*expr_p);
5574 gimple_stmt_iterator gsi;
5576 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5577 || TREE_CODE (*expr_p) == INIT_EXPR);
5579 /* Trying to simplify a clobber using normal logic doesn't work,
5580 so handle it here. */
5581 if (TREE_CLOBBER_P (*from_p))
5583 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5584 if (ret == GS_ERROR)
5585 return ret;
5586 gcc_assert (!want_value
5587 && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5588 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5589 *expr_p = NULL;
5590 return GS_ALL_DONE;
5593 /* Insert pointer conversions required by the middle-end that are not
5594 required by the frontend. This fixes middle-end type checking for
5595 for example gcc.dg/redecl-6.c. */
5596 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5598 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5599 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5600 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5603 /* See if any simplifications can be done based on what the RHS is. */
5604 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5605 want_value);
5606 if (ret != GS_UNHANDLED)
5607 return ret;
5609 /* For zero sized types only gimplify the left hand side and right hand
5610 side as statements and throw away the assignment. Do this after
5611 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5612 types properly. */
5613 if (zero_sized_type (TREE_TYPE (*from_p))
5614 && !want_value
5615 /* Don't do this for calls that return addressable types, expand_call
5616 relies on those having a lhs. */
5617 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5618 && TREE_CODE (*from_p) == CALL_EXPR))
5620 gimplify_stmt (from_p, pre_p);
5621 gimplify_stmt (to_p, pre_p);
5622 *expr_p = NULL_TREE;
5623 return GS_ALL_DONE;
5626 /* If the value being copied is of variable width, compute the length
5627 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5628 before gimplifying any of the operands so that we can resolve any
5629 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5630 the size of the expression to be copied, not of the destination, so
5631 that is what we must do here. */
5632 maybe_with_size_expr (from_p);
5634 /* As a special case, we have to temporarily allow for assignments
5635 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5636 a toplevel statement, when gimplifying the GENERIC expression
5637 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5638 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5640 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5641 prevent gimplify_expr from trying to create a new temporary for
5642 foo's LHS, we tell it that it should only gimplify until it
5643 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5644 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5645 and all we need to do here is set 'a' to be its LHS. */
5647 /* Gimplify the RHS first for C++17 and bug 71104. */
5648 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5649 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5650 if (ret == GS_ERROR)
5651 return ret;
5653 /* Then gimplify the LHS. */
5654 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5655 twice we have to make sure to gimplify into non-SSA as otherwise
5656 the abnormal edge added later will make those defs not dominate
5657 their uses.
5658 ??? Technically this applies only to the registers used in the
5659 resulting non-register *TO_P. */
5660 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5661 if (saved_into_ssa
5662 && TREE_CODE (*from_p) == CALL_EXPR
5663 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5664 gimplify_ctxp->into_ssa = false;
5665 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5666 gimplify_ctxp->into_ssa = saved_into_ssa;
5667 if (ret == GS_ERROR)
5668 return ret;
5670 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5671 guess for the predicate was wrong. */
5672 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5673 if (final_pred != initial_pred)
5675 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5676 if (ret == GS_ERROR)
5677 return ret;
5680 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5681 size as argument to the call. */
5682 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5684 tree call = TREE_OPERAND (*from_p, 0);
5685 tree vlasize = TREE_OPERAND (*from_p, 1);
5687 if (TREE_CODE (call) == CALL_EXPR
5688 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5690 int nargs = call_expr_nargs (call);
5691 tree type = TREE_TYPE (call);
5692 tree ap = CALL_EXPR_ARG (call, 0);
5693 tree tag = CALL_EXPR_ARG (call, 1);
5694 tree aptag = CALL_EXPR_ARG (call, 2);
5695 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5696 IFN_VA_ARG, type,
5697 nargs + 1, ap, tag,
5698 aptag, vlasize);
5699 TREE_OPERAND (*from_p, 0) = newcall;
5703 /* Now see if the above changed *from_p to something we handle specially. */
5704 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5705 want_value);
5706 if (ret != GS_UNHANDLED)
5707 return ret;
5709 /* If we've got a variable sized assignment between two lvalues (i.e. does
5710 not involve a call), then we can make things a bit more straightforward
5711 by converting the assignment to memcpy or memset. */
5712 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5714 tree from = TREE_OPERAND (*from_p, 0);
5715 tree size = TREE_OPERAND (*from_p, 1);
5717 if (TREE_CODE (from) == CONSTRUCTOR)
5718 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5720 if (is_gimple_addressable (from))
5722 *from_p = from;
5723 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5724 pre_p);
5728 /* Transform partial stores to non-addressable complex variables into
5729 total stores. This allows us to use real instead of virtual operands
5730 for these variables, which improves optimization. */
5731 if ((TREE_CODE (*to_p) == REALPART_EXPR
5732 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5733 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5734 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5736 /* Try to alleviate the effects of the gimplification creating artificial
5737 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5738 make sure not to create DECL_DEBUG_EXPR links across functions. */
5739 if (!gimplify_ctxp->into_ssa
5740 && VAR_P (*from_p)
5741 && DECL_IGNORED_P (*from_p)
5742 && DECL_P (*to_p)
5743 && !DECL_IGNORED_P (*to_p)
5744 && decl_function_context (*to_p) == current_function_decl
5745 && decl_function_context (*from_p) == current_function_decl)
5747 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5748 DECL_NAME (*from_p)
5749 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5750 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5751 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5754 if (want_value && TREE_THIS_VOLATILE (*to_p))
5755 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5757 if (TREE_CODE (*from_p) == CALL_EXPR)
5759 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5760 instead of a GIMPLE_ASSIGN. */
5761 gcall *call_stmt;
5762 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5764 /* Gimplify internal functions created in the FEs. */
5765 int nargs = call_expr_nargs (*from_p), i;
5766 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5767 auto_vec<tree> vargs (nargs);
5769 for (i = 0; i < nargs; i++)
5771 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5772 EXPR_LOCATION (*from_p));
5773 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5775 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5776 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5777 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5779 else
5781 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5782 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5783 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5784 tree fndecl = get_callee_fndecl (*from_p);
5785 if (fndecl
5786 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5787 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5788 && call_expr_nargs (*from_p) == 3)
5789 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5790 CALL_EXPR_ARG (*from_p, 0),
5791 CALL_EXPR_ARG (*from_p, 1),
5792 CALL_EXPR_ARG (*from_p, 2));
5793 else
5795 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5798 notice_special_calls (call_stmt);
5799 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5800 gimple_call_set_lhs (call_stmt, *to_p);
5801 else if (TREE_CODE (*to_p) == SSA_NAME)
5802 /* The above is somewhat premature, avoid ICEing later for a
5803 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5804 ??? This doesn't make it a default-def. */
5805 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5807 assign = call_stmt;
5809 else
5811 assign = gimple_build_assign (*to_p, *from_p);
5812 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5813 if (COMPARISON_CLASS_P (*from_p))
5814 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5817 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5819 /* We should have got an SSA name from the start. */
5820 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5821 || ! gimple_in_ssa_p (cfun));
5824 gimplify_seq_add_stmt (pre_p, assign);
5825 gsi = gsi_last (*pre_p);
5826 maybe_fold_stmt (&gsi);
5828 if (want_value)
5830 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5831 return GS_OK;
5833 else
5834 *expr_p = NULL;
5836 return GS_ALL_DONE;
5839 /* Gimplify a comparison between two variable-sized objects. Do this
5840 with a call to BUILT_IN_MEMCMP. */
5842 static enum gimplify_status
5843 gimplify_variable_sized_compare (tree *expr_p)
5845 location_t loc = EXPR_LOCATION (*expr_p);
5846 tree op0 = TREE_OPERAND (*expr_p, 0);
5847 tree op1 = TREE_OPERAND (*expr_p, 1);
5848 tree t, arg, dest, src, expr;
5850 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5851 arg = unshare_expr (arg);
5852 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5853 src = build_fold_addr_expr_loc (loc, op1);
5854 dest = build_fold_addr_expr_loc (loc, op0);
5855 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5856 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5858 expr
5859 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5860 SET_EXPR_LOCATION (expr, loc);
5861 *expr_p = expr;
5863 return GS_OK;
5866 /* Gimplify a comparison between two aggregate objects of integral scalar
5867 mode as a comparison between the bitwise equivalent scalar values. */
5869 static enum gimplify_status
5870 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5872 location_t loc = EXPR_LOCATION (*expr_p);
5873 tree op0 = TREE_OPERAND (*expr_p, 0);
5874 tree op1 = TREE_OPERAND (*expr_p, 1);
5876 tree type = TREE_TYPE (op0);
5877 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5879 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5880 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5882 *expr_p
5883 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5885 return GS_OK;
5888 /* Gimplify an expression sequence. This function gimplifies each
5889 expression and rewrites the original expression with the last
5890 expression of the sequence in GIMPLE form.
5892 PRE_P points to the list where the side effects for all the
5893 expressions in the sequence will be emitted.
5895 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5897 static enum gimplify_status
5898 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5900 tree t = *expr_p;
5904 tree *sub_p = &TREE_OPERAND (t, 0);
5906 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5907 gimplify_compound_expr (sub_p, pre_p, false);
5908 else
5909 gimplify_stmt (sub_p, pre_p);
5911 t = TREE_OPERAND (t, 1);
5913 while (TREE_CODE (t) == COMPOUND_EXPR);
5915 *expr_p = t;
5916 if (want_value)
5917 return GS_OK;
5918 else
5920 gimplify_stmt (expr_p, pre_p);
5921 return GS_ALL_DONE;
5925 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5926 gimplify. After gimplification, EXPR_P will point to a new temporary
5927 that holds the original value of the SAVE_EXPR node.
5929 PRE_P points to the list where side effects that must happen before
5930 *EXPR_P should be stored. */
5932 static enum gimplify_status
5933 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5935 enum gimplify_status ret = GS_ALL_DONE;
5936 tree val;
5938 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5939 val = TREE_OPERAND (*expr_p, 0);
5941 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5942 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5944 /* The operand may be a void-valued expression. It is
5945 being executed only for its side-effects. */
5946 if (TREE_TYPE (val) == void_type_node)
5948 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5949 is_gimple_stmt, fb_none);
5950 val = NULL;
5952 else
5953 /* The temporary may not be an SSA name as later abnormal and EH
5954 control flow may invalidate use/def domination. When in SSA
5955 form then assume there are no such issues and SAVE_EXPRs only
5956 appear via GENERIC foldings. */
5957 val = get_initialized_tmp_var (val, pre_p, post_p,
5958 gimple_in_ssa_p (cfun));
5960 TREE_OPERAND (*expr_p, 0) = val;
5961 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5964 *expr_p = val;
5966 return ret;
5969 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5971 unary_expr
5972 : ...
5973 | '&' varname
5976 PRE_P points to the list where side effects that must happen before
5977 *EXPR_P should be stored.
5979 POST_P points to the list where side effects that must happen after
5980 *EXPR_P should be stored. */
5982 static enum gimplify_status
5983 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5985 tree expr = *expr_p;
5986 tree op0 = TREE_OPERAND (expr, 0);
5987 enum gimplify_status ret;
5988 location_t loc = EXPR_LOCATION (*expr_p);
5990 switch (TREE_CODE (op0))
5992 case INDIRECT_REF:
5993 do_indirect_ref:
5994 /* Check if we are dealing with an expression of the form '&*ptr'.
5995 While the front end folds away '&*ptr' into 'ptr', these
5996 expressions may be generated internally by the compiler (e.g.,
5997 builtins like __builtin_va_end). */
5998 /* Caution: the silent array decomposition semantics we allow for
5999 ADDR_EXPR means we can't always discard the pair. */
6000 /* Gimplification of the ADDR_EXPR operand may drop
6001 cv-qualification conversions, so make sure we add them if
6002 needed. */
6004 tree op00 = TREE_OPERAND (op0, 0);
6005 tree t_expr = TREE_TYPE (expr);
6006 tree t_op00 = TREE_TYPE (op00);
6008 if (!useless_type_conversion_p (t_expr, t_op00))
6009 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
6010 *expr_p = op00;
6011 ret = GS_OK;
6013 break;
6015 case VIEW_CONVERT_EXPR:
6016 /* Take the address of our operand and then convert it to the type of
6017 this ADDR_EXPR.
6019 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
6020 all clear. The impact of this transformation is even less clear. */
6022 /* If the operand is a useless conversion, look through it. Doing so
6023 guarantees that the ADDR_EXPR and its operand will remain of the
6024 same type. */
6025 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
6026 op0 = TREE_OPERAND (op0, 0);
6028 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6029 build_fold_addr_expr_loc (loc,
6030 TREE_OPERAND (op0, 0)));
6031 ret = GS_OK;
6032 break;
6034 case MEM_REF:
6035 if (integer_zerop (TREE_OPERAND (op0, 1)))
6036 goto do_indirect_ref;
6038 /* fall through */
6040 default:
6041 /* If we see a call to a declared builtin or see its address
6042 being taken (we can unify those cases here) then we can mark
6043 the builtin for implicit generation by GCC. */
6044 if (TREE_CODE (op0) == FUNCTION_DECL
6045 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
6046 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6047 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6049 /* We use fb_either here because the C frontend sometimes takes
6050 the address of a call that returns a struct; see
6051 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6052 the implied temporary explicit. */
6054 /* Make the operand addressable. */
6055 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6056 is_gimple_addressable, fb_either);
6057 if (ret == GS_ERROR)
6058 break;
6060 /* Then mark it. Beware that it may not be possible to do so directly
6061 if a temporary has been created by the gimplification. */
6062 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6064 op0 = TREE_OPERAND (expr, 0);
6066 /* For various reasons, the gimplification of the expression
6067 may have made a new INDIRECT_REF. */
6068 if (TREE_CODE (op0) == INDIRECT_REF)
6069 goto do_indirect_ref;
6071 mark_addressable (TREE_OPERAND (expr, 0));
6073 /* The FEs may end up building ADDR_EXPRs early on a decl with
6074 an incomplete type. Re-build ADDR_EXPRs in canonical form
6075 here. */
6076 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6077 *expr_p = build_fold_addr_expr (op0);
6079 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6080 recompute_tree_invariant_for_addr_expr (*expr_p);
6082 /* If we re-built the ADDR_EXPR add a conversion to the original type
6083 if required. */
6084 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6085 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6087 break;
6090 return ret;
6093 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6094 value; output operands should be a gimple lvalue. */
6096 static enum gimplify_status
6097 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6099 tree expr;
6100 int noutputs;
6101 const char **oconstraints;
6102 int i;
6103 tree link;
6104 const char *constraint;
6105 bool allows_mem, allows_reg, is_inout;
6106 enum gimplify_status ret, tret;
6107 gasm *stmt;
6108 vec<tree, va_gc> *inputs;
6109 vec<tree, va_gc> *outputs;
6110 vec<tree, va_gc> *clobbers;
6111 vec<tree, va_gc> *labels;
6112 tree link_next;
6114 expr = *expr_p;
6115 noutputs = list_length (ASM_OUTPUTS (expr));
6116 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6118 inputs = NULL;
6119 outputs = NULL;
6120 clobbers = NULL;
6121 labels = NULL;
6123 ret = GS_ALL_DONE;
6124 link_next = NULL_TREE;
6125 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6127 bool ok;
6128 size_t constraint_len;
6130 link_next = TREE_CHAIN (link);
6132 oconstraints[i]
6133 = constraint
6134 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6135 constraint_len = strlen (constraint);
6136 if (constraint_len == 0)
6137 continue;
6139 ok = parse_output_constraint (&constraint, i, 0, 0,
6140 &allows_mem, &allows_reg, &is_inout);
6141 if (!ok)
6143 ret = GS_ERROR;
6144 is_inout = false;
6147 if (!allows_reg && allows_mem)
6148 mark_addressable (TREE_VALUE (link));
6150 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6151 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6152 fb_lvalue | fb_mayfail);
6153 if (tret == GS_ERROR)
6155 error ("invalid lvalue in asm output %d", i);
6156 ret = tret;
6159 /* If the constraint does not allow memory make sure we gimplify
6160 it to a register if it is not already but its base is. This
6161 happens for complex and vector components. */
6162 if (!allows_mem)
6164 tree op = TREE_VALUE (link);
6165 if (! is_gimple_val (op)
6166 && is_gimple_reg_type (TREE_TYPE (op))
6167 && is_gimple_reg (get_base_address (op)))
6169 tree tem = create_tmp_reg (TREE_TYPE (op));
6170 tree ass;
6171 if (is_inout)
6173 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6174 tem, unshare_expr (op));
6175 gimplify_and_add (ass, pre_p);
6177 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6178 gimplify_and_add (ass, post_p);
6180 TREE_VALUE (link) = tem;
6181 tret = GS_OK;
6185 vec_safe_push (outputs, link);
6186 TREE_CHAIN (link) = NULL_TREE;
6188 if (is_inout)
6190 /* An input/output operand. To give the optimizers more
6191 flexibility, split it into separate input and output
6192 operands. */
6193 tree input;
6194 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6195 char buf[11];
6197 /* Turn the in/out constraint into an output constraint. */
6198 char *p = xstrdup (constraint);
6199 p[0] = '=';
6200 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6202 /* And add a matching input constraint. */
6203 if (allows_reg)
6205 sprintf (buf, "%u", i);
6207 /* If there are multiple alternatives in the constraint,
6208 handle each of them individually. Those that allow register
6209 will be replaced with operand number, the others will stay
6210 unchanged. */
6211 if (strchr (p, ',') != NULL)
6213 size_t len = 0, buflen = strlen (buf);
6214 char *beg, *end, *str, *dst;
6216 for (beg = p + 1;;)
6218 end = strchr (beg, ',');
6219 if (end == NULL)
6220 end = strchr (beg, '\0');
6221 if ((size_t) (end - beg) < buflen)
6222 len += buflen + 1;
6223 else
6224 len += end - beg + 1;
6225 if (*end)
6226 beg = end + 1;
6227 else
6228 break;
6231 str = (char *) alloca (len);
6232 for (beg = p + 1, dst = str;;)
6234 const char *tem;
6235 bool mem_p, reg_p, inout_p;
6237 end = strchr (beg, ',');
6238 if (end)
6239 *end = '\0';
6240 beg[-1] = '=';
6241 tem = beg - 1;
6242 parse_output_constraint (&tem, i, 0, 0,
6243 &mem_p, &reg_p, &inout_p);
6244 if (dst != str)
6245 *dst++ = ',';
6246 if (reg_p)
6248 memcpy (dst, buf, buflen);
6249 dst += buflen;
6251 else
6253 if (end)
6254 len = end - beg;
6255 else
6256 len = strlen (beg);
6257 memcpy (dst, beg, len);
6258 dst += len;
6260 if (end)
6261 beg = end + 1;
6262 else
6263 break;
6265 *dst = '\0';
6266 input = build_string (dst - str, str);
6268 else
6269 input = build_string (strlen (buf), buf);
6271 else
6272 input = build_string (constraint_len - 1, constraint + 1);
6274 free (p);
6276 input = build_tree_list (build_tree_list (NULL_TREE, input),
6277 unshare_expr (TREE_VALUE (link)));
6278 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6282 link_next = NULL_TREE;
6283 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6285 link_next = TREE_CHAIN (link);
6286 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6287 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6288 oconstraints, &allows_mem, &allows_reg);
6290 /* If we can't make copies, we can only accept memory. */
6291 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6293 if (allows_mem)
6294 allows_reg = 0;
6295 else
6297 error ("impossible constraint in %<asm%>");
6298 error ("non-memory input %d must stay in memory", i);
6299 return GS_ERROR;
6303 /* If the operand is a memory input, it should be an lvalue. */
6304 if (!allows_reg && allows_mem)
6306 tree inputv = TREE_VALUE (link);
6307 STRIP_NOPS (inputv);
6308 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6309 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6310 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6311 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6312 || TREE_CODE (inputv) == MODIFY_EXPR)
6313 TREE_VALUE (link) = error_mark_node;
6314 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6315 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6316 if (tret != GS_ERROR)
6318 /* Unlike output operands, memory inputs are not guaranteed
6319 to be lvalues by the FE, and while the expressions are
6320 marked addressable there, if it is e.g. a statement
6321 expression, temporaries in it might not end up being
6322 addressable. They might be already used in the IL and thus
6323 it is too late to make them addressable now though. */
6324 tree x = TREE_VALUE (link);
6325 while (handled_component_p (x))
6326 x = TREE_OPERAND (x, 0);
6327 if (TREE_CODE (x) == MEM_REF
6328 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6329 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6330 if ((VAR_P (x)
6331 || TREE_CODE (x) == PARM_DECL
6332 || TREE_CODE (x) == RESULT_DECL)
6333 && !TREE_ADDRESSABLE (x)
6334 && is_gimple_reg (x))
6336 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6337 input_location), 0,
6338 "memory input %d is not directly addressable",
6340 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6343 mark_addressable (TREE_VALUE (link));
6344 if (tret == GS_ERROR)
6346 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6347 "memory input %d is not directly addressable", i);
6348 ret = tret;
6351 else
6353 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6354 is_gimple_asm_val, fb_rvalue);
6355 if (tret == GS_ERROR)
6356 ret = tret;
6359 TREE_CHAIN (link) = NULL_TREE;
6360 vec_safe_push (inputs, link);
6363 link_next = NULL_TREE;
6364 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6366 link_next = TREE_CHAIN (link);
6367 TREE_CHAIN (link) = NULL_TREE;
6368 vec_safe_push (clobbers, link);
6371 link_next = NULL_TREE;
6372 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6374 link_next = TREE_CHAIN (link);
6375 TREE_CHAIN (link) = NULL_TREE;
6376 vec_safe_push (labels, link);
6379 /* Do not add ASMs with errors to the gimple IL stream. */
6380 if (ret != GS_ERROR)
6382 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6383 inputs, outputs, clobbers, labels);
6385 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6386 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6387 gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
6389 gimplify_seq_add_stmt (pre_p, stmt);
6392 return ret;
6395 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6396 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6397 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6398 return to this function.
6400 FIXME should we complexify the prequeue handling instead? Or use flags
6401 for all the cleanups and let the optimizer tighten them up? The current
6402 code seems pretty fragile; it will break on a cleanup within any
6403 non-conditional nesting. But any such nesting would be broken, anyway;
6404 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6405 and continues out of it. We can do that at the RTL level, though, so
6406 having an optimizer to tighten up try/finally regions would be a Good
6407 Thing. */
6409 static enum gimplify_status
6410 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6412 gimple_stmt_iterator iter;
6413 gimple_seq body_sequence = NULL;
6415 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6417 /* We only care about the number of conditions between the innermost
6418 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6419 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6420 int old_conds = gimplify_ctxp->conditions;
6421 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6422 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6423 gimplify_ctxp->conditions = 0;
6424 gimplify_ctxp->conditional_cleanups = NULL;
6425 gimplify_ctxp->in_cleanup_point_expr = true;
6427 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6429 gimplify_ctxp->conditions = old_conds;
6430 gimplify_ctxp->conditional_cleanups = old_cleanups;
6431 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6433 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6435 gimple *wce = gsi_stmt (iter);
6437 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6439 if (gsi_one_before_end_p (iter))
6441 /* Note that gsi_insert_seq_before and gsi_remove do not
6442 scan operands, unlike some other sequence mutators. */
6443 if (!gimple_wce_cleanup_eh_only (wce))
6444 gsi_insert_seq_before_without_update (&iter,
6445 gimple_wce_cleanup (wce),
6446 GSI_SAME_STMT);
6447 gsi_remove (&iter, true);
6448 break;
6450 else
6452 gtry *gtry;
6453 gimple_seq seq;
6454 enum gimple_try_flags kind;
6456 if (gimple_wce_cleanup_eh_only (wce))
6457 kind = GIMPLE_TRY_CATCH;
6458 else
6459 kind = GIMPLE_TRY_FINALLY;
6460 seq = gsi_split_seq_after (iter);
6462 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6463 /* Do not use gsi_replace here, as it may scan operands.
6464 We want to do a simple structural modification only. */
6465 gsi_set_stmt (&iter, gtry);
6466 iter = gsi_start (gtry->eval);
6469 else
6470 gsi_next (&iter);
6473 gimplify_seq_add_seq (pre_p, body_sequence);
6474 if (temp)
6476 *expr_p = temp;
6477 return GS_OK;
6479 else
6481 *expr_p = NULL;
6482 return GS_ALL_DONE;
6486 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6487 is the cleanup action required. EH_ONLY is true if the cleanup should
6488 only be executed if an exception is thrown, not on normal exit.
6489 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6490 only valid for clobbers. */
6492 static void
6493 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6494 bool force_uncond = false)
6496 gimple *wce;
6497 gimple_seq cleanup_stmts = NULL;
6499 /* Errors can result in improperly nested cleanups. Which results in
6500 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6501 if (seen_error ())
6502 return;
6504 if (gimple_conditional_context ())
6506 /* If we're in a conditional context, this is more complex. We only
6507 want to run the cleanup if we actually ran the initialization that
6508 necessitates it, but we want to run it after the end of the
6509 conditional context. So we wrap the try/finally around the
6510 condition and use a flag to determine whether or not to actually
6511 run the destructor. Thus
6513 test ? f(A()) : 0
6515 becomes (approximately)
6517 flag = 0;
6518 try {
6519 if (test) { A::A(temp); flag = 1; val = f(temp); }
6520 else { val = 0; }
6521 } finally {
6522 if (flag) A::~A(temp);
6526 if (force_uncond)
6528 gimplify_stmt (&cleanup, &cleanup_stmts);
6529 wce = gimple_build_wce (cleanup_stmts);
6530 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6532 else
6534 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6535 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6536 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6538 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6539 gimplify_stmt (&cleanup, &cleanup_stmts);
6540 wce = gimple_build_wce (cleanup_stmts);
6542 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6543 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6544 gimplify_seq_add_stmt (pre_p, ftrue);
6546 /* Because of this manipulation, and the EH edges that jump
6547 threading cannot redirect, the temporary (VAR) will appear
6548 to be used uninitialized. Don't warn. */
6549 TREE_NO_WARNING (var) = 1;
6552 else
6554 gimplify_stmt (&cleanup, &cleanup_stmts);
6555 wce = gimple_build_wce (cleanup_stmts);
6556 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6557 gimplify_seq_add_stmt (pre_p, wce);
6561 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6563 static enum gimplify_status
6564 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6566 tree targ = *expr_p;
6567 tree temp = TARGET_EXPR_SLOT (targ);
6568 tree init = TARGET_EXPR_INITIAL (targ);
6569 enum gimplify_status ret;
6571 bool unpoison_empty_seq = false;
6572 gimple_stmt_iterator unpoison_it;
6574 if (init)
6576 tree cleanup = NULL_TREE;
6578 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6579 to the temps list. Handle also variable length TARGET_EXPRs. */
6580 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6582 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6583 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6584 gimplify_vla_decl (temp, pre_p);
6586 else
6588 /* Save location where we need to place unpoisoning. It's possible
6589 that a variable will be converted to needs_to_live_in_memory. */
6590 unpoison_it = gsi_last (*pre_p);
6591 unpoison_empty_seq = gsi_end_p (unpoison_it);
6593 gimple_add_tmp_var (temp);
6596 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6597 expression is supposed to initialize the slot. */
6598 if (VOID_TYPE_P (TREE_TYPE (init)))
6599 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6600 else
6602 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6603 init = init_expr;
6604 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6605 init = NULL;
6606 ggc_free (init_expr);
6608 if (ret == GS_ERROR)
6610 /* PR c++/28266 Make sure this is expanded only once. */
6611 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6612 return GS_ERROR;
6614 if (init)
6615 gimplify_and_add (init, pre_p);
6617 /* If needed, push the cleanup for the temp. */
6618 if (TARGET_EXPR_CLEANUP (targ))
6620 if (CLEANUP_EH_ONLY (targ))
6621 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6622 CLEANUP_EH_ONLY (targ), pre_p);
6623 else
6624 cleanup = TARGET_EXPR_CLEANUP (targ);
6627 /* Add a clobber for the temporary going out of scope, like
6628 gimplify_bind_expr. */
6629 if (gimplify_ctxp->in_cleanup_point_expr
6630 && needs_to_live_in_memory (temp))
6632 if (flag_stack_reuse == SR_ALL)
6634 tree clobber = build_constructor (TREE_TYPE (temp),
6635 NULL);
6636 TREE_THIS_VOLATILE (clobber) = true;
6637 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6638 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6640 if (asan_poisoned_variables
6641 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6642 && dbg_cnt (asan_use_after_scope)
6643 && !gimplify_omp_ctxp)
6645 tree asan_cleanup = build_asan_poison_call_expr (temp);
6646 if (asan_cleanup)
6648 if (unpoison_empty_seq)
6649 unpoison_it = gsi_start (*pre_p);
6651 asan_poison_variable (temp, false, &unpoison_it,
6652 unpoison_empty_seq);
6653 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6657 if (cleanup)
6658 gimple_push_cleanup (temp, cleanup, false, pre_p);
6660 /* Only expand this once. */
6661 TREE_OPERAND (targ, 3) = init;
6662 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6664 else
6665 /* We should have expanded this before. */
6666 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6668 *expr_p = temp;
6669 return GS_OK;
6672 /* Gimplification of expression trees. */
6674 /* Gimplify an expression which appears at statement context. The
6675 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6676 NULL, a new sequence is allocated.
6678 Return true if we actually added a statement to the queue. */
6680 bool
6681 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6683 gimple_seq_node last;
6685 last = gimple_seq_last (*seq_p);
6686 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6687 return last != gimple_seq_last (*seq_p);
6690 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6691 to CTX. If entries already exist, force them to be some flavor of private.
6692 If there is no enclosing parallel, do nothing. */
6694 void
6695 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6697 splay_tree_node n;
6699 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6700 return;
6704 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6705 if (n != NULL)
6707 if (n->value & GOVD_SHARED)
6708 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6709 else if (n->value & GOVD_MAP)
6710 n->value |= GOVD_MAP_TO_ONLY;
6711 else
6712 return;
6714 else if ((ctx->region_type & ORT_TARGET) != 0)
6716 if (ctx->target_map_scalars_firstprivate)
6717 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6718 else
6719 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6721 else if (ctx->region_type != ORT_WORKSHARE
6722 && ctx->region_type != ORT_SIMD
6723 && ctx->region_type != ORT_ACC
6724 && !(ctx->region_type & ORT_TARGET_DATA))
6725 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6727 ctx = ctx->outer_context;
6729 while (ctx);
6732 /* Similarly for each of the type sizes of TYPE. */
6734 static void
6735 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6737 if (type == NULL || type == error_mark_node)
6738 return;
6739 type = TYPE_MAIN_VARIANT (type);
6741 if (ctx->privatized_types->add (type))
6742 return;
6744 switch (TREE_CODE (type))
6746 case INTEGER_TYPE:
6747 case ENUMERAL_TYPE:
6748 case BOOLEAN_TYPE:
6749 case REAL_TYPE:
6750 case FIXED_POINT_TYPE:
6751 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6752 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6753 break;
6755 case ARRAY_TYPE:
6756 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6757 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6758 break;
6760 case RECORD_TYPE:
6761 case UNION_TYPE:
6762 case QUAL_UNION_TYPE:
6764 tree field;
6765 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6766 if (TREE_CODE (field) == FIELD_DECL)
6768 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6769 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6772 break;
6774 case POINTER_TYPE:
6775 case REFERENCE_TYPE:
6776 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6777 break;
6779 default:
6780 break;
6783 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6784 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6785 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6788 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6790 static void
6791 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6793 splay_tree_node n;
6794 unsigned int nflags;
6795 tree t;
6797 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6798 return;
6800 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6801 there are constructors involved somewhere. Exception is a shared clause,
6802 there is nothing privatized in that case. */
6803 if ((flags & GOVD_SHARED) == 0
6804 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6805 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6806 flags |= GOVD_SEEN;
6808 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6809 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6811 /* We shouldn't be re-adding the decl with the same data
6812 sharing class. */
6813 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6814 nflags = n->value | flags;
6815 /* The only combination of data sharing classes we should see is
6816 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6817 reduction variables to be used in data sharing clauses. */
6818 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6819 || ((nflags & GOVD_DATA_SHARE_CLASS)
6820 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6821 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6822 n->value = nflags;
6823 return;
6826 /* When adding a variable-sized variable, we have to handle all sorts
6827 of additional bits of data: the pointer replacement variable, and
6828 the parameters of the type. */
6829 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6831 /* Add the pointer replacement variable as PRIVATE if the variable
6832 replacement is private, else FIRSTPRIVATE since we'll need the
6833 address of the original variable either for SHARED, or for the
6834 copy into or out of the context. */
6835 if (!(flags & GOVD_LOCAL))
6837 if (flags & GOVD_MAP)
6838 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6839 else if (flags & GOVD_PRIVATE)
6840 nflags = GOVD_PRIVATE;
6841 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6842 && (flags & GOVD_FIRSTPRIVATE))
6843 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6844 else
6845 nflags = GOVD_FIRSTPRIVATE;
6846 nflags |= flags & GOVD_SEEN;
6847 t = DECL_VALUE_EXPR (decl);
6848 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6849 t = TREE_OPERAND (t, 0);
6850 gcc_assert (DECL_P (t));
6851 omp_add_variable (ctx, t, nflags);
6854 /* Add all of the variable and type parameters (which should have
6855 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6856 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6857 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6858 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6860 /* The variable-sized variable itself is never SHARED, only some form
6861 of PRIVATE. The sharing would take place via the pointer variable
6862 which we remapped above. */
6863 if (flags & GOVD_SHARED)
6864 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6865 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6867 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6868 alloca statement we generate for the variable, so make sure it
6869 is available. This isn't automatically needed for the SHARED
6870 case, since we won't be allocating local storage then.
6871 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6872 in this case omp_notice_variable will be called later
6873 on when it is gimplified. */
6874 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6875 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6876 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6878 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6879 && lang_hooks.decls.omp_privatize_by_reference (decl))
6881 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6883 /* Similar to the direct variable sized case above, we'll need the
6884 size of references being privatized. */
6885 if ((flags & GOVD_SHARED) == 0)
6887 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6888 if (DECL_P (t))
6889 omp_notice_variable (ctx, t, true);
6893 if (n != NULL)
6894 n->value |= flags;
6895 else
6896 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6898 /* For reductions clauses in OpenACC loop directives, by default create a
6899 copy clause on the enclosing parallel construct for carrying back the
6900 results. */
6901 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6903 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6904 while (outer_ctx)
6906 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6907 if (n != NULL)
6909 /* Ignore local variables and explicitly declared clauses. */
6910 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6911 break;
6912 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6914 /* According to the OpenACC spec, such a reduction variable
6915 should already have a copy map on a kernels construct,
6916 verify that here. */
6917 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6918 && (n->value & GOVD_MAP));
6920 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6922 /* Remove firstprivate and make it a copy map. */
6923 n->value &= ~GOVD_FIRSTPRIVATE;
6924 n->value |= GOVD_MAP;
6927 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6929 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6930 GOVD_MAP | GOVD_SEEN);
6931 break;
6933 outer_ctx = outer_ctx->outer_context;
6938 /* Notice a threadprivate variable DECL used in OMP context CTX.
6939 This just prints out diagnostics about threadprivate variable uses
6940 in untied tasks. If DECL2 is non-NULL, prevent this warning
6941 on that variable. */
6943 static bool
6944 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6945 tree decl2)
6947 splay_tree_node n;
6948 struct gimplify_omp_ctx *octx;
6950 for (octx = ctx; octx; octx = octx->outer_context)
6951 if ((octx->region_type & ORT_TARGET) != 0)
6953 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6954 if (n == NULL)
6956 error ("threadprivate variable %qE used in target region",
6957 DECL_NAME (decl));
6958 error_at (octx->location, "enclosing target region");
6959 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6961 if (decl2)
6962 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6965 if (ctx->region_type != ORT_UNTIED_TASK)
6966 return false;
6967 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6968 if (n == NULL)
6970 error ("threadprivate variable %qE used in untied task",
6971 DECL_NAME (decl));
6972 error_at (ctx->location, "enclosing task");
6973 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6975 if (decl2)
6976 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6977 return false;
6980 /* Return true if global var DECL is device resident. */
6982 static bool
6983 device_resident_p (tree decl)
6985 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6987 if (!attr)
6988 return false;
6990 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6992 tree c = TREE_VALUE (t);
6993 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6994 return true;
6997 return false;
7000 /* Return true if DECL has an ACC DECLARE attribute. */
7002 static bool
7003 is_oacc_declared (tree decl)
7005 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
7006 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
7007 return declared != NULL_TREE;
7010 /* Determine outer default flags for DECL mentioned in an OMP region
7011 but not declared in an enclosing clause.
7013 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
7014 remapped firstprivate instead of shared. To some extent this is
7015 addressed in omp_firstprivatize_type_sizes, but not
7016 effectively. */
7018 static unsigned
7019 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
7020 bool in_code, unsigned flags)
7022 enum omp_clause_default_kind default_kind = ctx->default_kind;
7023 enum omp_clause_default_kind kind;
7025 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7026 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7027 default_kind = kind;
7029 switch (default_kind)
7031 case OMP_CLAUSE_DEFAULT_NONE:
7033 const char *rtype;
7035 if (ctx->region_type & ORT_PARALLEL)
7036 rtype = "parallel";
7037 else if (ctx->region_type & ORT_TASK)
7038 rtype = "task";
7039 else if (ctx->region_type & ORT_TEAMS)
7040 rtype = "teams";
7041 else
7042 gcc_unreachable ();
7044 error ("%qE not specified in enclosing %qs",
7045 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7046 error_at (ctx->location, "enclosing %qs", rtype);
7048 /* FALLTHRU */
7049 case OMP_CLAUSE_DEFAULT_SHARED:
7050 flags |= GOVD_SHARED;
7051 break;
7052 case OMP_CLAUSE_DEFAULT_PRIVATE:
7053 flags |= GOVD_PRIVATE;
7054 break;
7055 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7056 flags |= GOVD_FIRSTPRIVATE;
7057 break;
7058 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7059 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7060 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7061 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7063 omp_notice_variable (octx, decl, in_code);
7064 for (; octx; octx = octx->outer_context)
7066 splay_tree_node n2;
7068 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7069 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7070 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7071 continue;
7072 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7074 flags |= GOVD_FIRSTPRIVATE;
7075 goto found_outer;
7077 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7079 flags |= GOVD_SHARED;
7080 goto found_outer;
7085 if (TREE_CODE (decl) == PARM_DECL
7086 || (!is_global_var (decl)
7087 && DECL_CONTEXT (decl) == current_function_decl))
7088 flags |= GOVD_FIRSTPRIVATE;
7089 else
7090 flags |= GOVD_SHARED;
7091 found_outer:
7092 break;
7094 default:
7095 gcc_unreachable ();
7098 return flags;
7102 /* Determine outer default flags for DECL mentioned in an OACC region
7103 but not declared in an enclosing clause. */
7105 static unsigned
7106 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7108 const char *rkind;
7109 bool on_device = false;
7110 bool declared = is_oacc_declared (decl);
7111 tree type = TREE_TYPE (decl);
7113 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7114 type = TREE_TYPE (type);
7116 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7117 && is_global_var (decl)
7118 && device_resident_p (decl))
7120 on_device = true;
7121 flags |= GOVD_MAP_TO_ONLY;
7124 switch (ctx->region_type)
7126 case ORT_ACC_KERNELS:
7127 rkind = "kernels";
7129 if (AGGREGATE_TYPE_P (type))
7131 /* Aggregates default to 'present_or_copy', or 'present'. */
7132 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7133 flags |= GOVD_MAP;
7134 else
7135 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7137 else
7138 /* Scalars default to 'copy'. */
7139 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7141 break;
7143 case ORT_ACC_PARALLEL:
7144 rkind = "parallel";
7146 if (on_device || declared)
7147 flags |= GOVD_MAP;
7148 else if (AGGREGATE_TYPE_P (type))
7150 /* Aggregates default to 'present_or_copy', or 'present'. */
7151 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7152 flags |= GOVD_MAP;
7153 else
7154 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7156 else
7157 /* Scalars default to 'firstprivate'. */
7158 flags |= GOVD_FIRSTPRIVATE;
7160 break;
7162 default:
7163 gcc_unreachable ();
7166 if (DECL_ARTIFICIAL (decl))
7167 ; /* We can get compiler-generated decls, and should not complain
7168 about them. */
7169 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7171 error ("%qE not specified in enclosing OpenACC %qs construct",
7172 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7173 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7175 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7176 ; /* Handled above. */
7177 else
7178 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7180 return flags;
7183 /* Record the fact that DECL was used within the OMP context CTX.
7184 IN_CODE is true when real code uses DECL, and false when we should
7185 merely emit default(none) errors. Return true if DECL is going to
7186 be remapped and thus DECL shouldn't be gimplified into its
7187 DECL_VALUE_EXPR (if any). */
7189 static bool
7190 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7192 splay_tree_node n;
7193 unsigned flags = in_code ? GOVD_SEEN : 0;
7194 bool ret = false, shared;
7196 if (error_operand_p (decl))
7197 return false;
7199 if (ctx->region_type == ORT_NONE)
7200 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7202 if (is_global_var (decl))
7204 /* Threadprivate variables are predetermined. */
7205 if (DECL_THREAD_LOCAL_P (decl))
7206 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7208 if (DECL_HAS_VALUE_EXPR_P (decl))
7210 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7212 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7213 return omp_notice_threadprivate_variable (ctx, decl, value);
7216 if (gimplify_omp_ctxp->outer_context == NULL
7217 && VAR_P (decl)
7218 && oacc_get_fn_attrib (current_function_decl))
7220 location_t loc = DECL_SOURCE_LOCATION (decl);
7222 if (lookup_attribute ("omp declare target link",
7223 DECL_ATTRIBUTES (decl)))
7225 error_at (loc,
7226 "%qE with %<link%> clause used in %<routine%> function",
7227 DECL_NAME (decl));
7228 return false;
7230 else if (!lookup_attribute ("omp declare target",
7231 DECL_ATTRIBUTES (decl)))
7233 error_at (loc,
7234 "%qE requires a %<declare%> directive for use "
7235 "in a %<routine%> function", DECL_NAME (decl));
7236 return false;
7241 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7242 if ((ctx->region_type & ORT_TARGET) != 0)
7244 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7245 if (n == NULL)
7247 unsigned nflags = flags;
7248 if (ctx->target_map_pointers_as_0len_arrays
7249 || ctx->target_map_scalars_firstprivate)
7251 bool is_declare_target = false;
7252 bool is_scalar = false;
7253 if (is_global_var (decl)
7254 && varpool_node::get_create (decl)->offloadable)
7256 struct gimplify_omp_ctx *octx;
7257 for (octx = ctx->outer_context;
7258 octx; octx = octx->outer_context)
7260 n = splay_tree_lookup (octx->variables,
7261 (splay_tree_key)decl);
7262 if (n
7263 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7264 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7265 break;
7267 is_declare_target = octx == NULL;
7269 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7270 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7271 if (is_declare_target)
7273 else if (ctx->target_map_pointers_as_0len_arrays
7274 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7275 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7276 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7277 == POINTER_TYPE)))
7278 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7279 else if (is_scalar)
7280 nflags |= GOVD_FIRSTPRIVATE;
7283 struct gimplify_omp_ctx *octx = ctx->outer_context;
7284 if ((ctx->region_type & ORT_ACC) && octx)
7286 /* Look in outer OpenACC contexts, to see if there's a
7287 data attribute for this variable. */
7288 omp_notice_variable (octx, decl, in_code);
7290 for (; octx; octx = octx->outer_context)
7292 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7293 break;
7294 splay_tree_node n2
7295 = splay_tree_lookup (octx->variables,
7296 (splay_tree_key) decl);
7297 if (n2)
7299 if (octx->region_type == ORT_ACC_HOST_DATA)
7300 error ("variable %qE declared in enclosing "
7301 "%<host_data%> region", DECL_NAME (decl));
7302 nflags |= GOVD_MAP;
7303 if (octx->region_type == ORT_ACC_DATA
7304 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7305 nflags |= GOVD_MAP_0LEN_ARRAY;
7306 goto found_outer;
7312 tree type = TREE_TYPE (decl);
7314 if (nflags == flags
7315 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7316 && lang_hooks.decls.omp_privatize_by_reference (decl))
7317 type = TREE_TYPE (type);
7318 if (nflags == flags
7319 && !lang_hooks.types.omp_mappable_type (type))
7321 error ("%qD referenced in target region does not have "
7322 "a mappable type", decl);
7323 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7325 else if (nflags == flags)
7327 if ((ctx->region_type & ORT_ACC) != 0)
7328 nflags = oacc_default_clause (ctx, decl, flags);
7329 else
7330 nflags |= GOVD_MAP;
7333 found_outer:
7334 omp_add_variable (ctx, decl, nflags);
7336 else
7338 /* If nothing changed, there's nothing left to do. */
7339 if ((n->value & flags) == flags)
7340 return ret;
7341 flags |= n->value;
7342 n->value = flags;
7344 goto do_outer;
7347 if (n == NULL)
7349 if (ctx->region_type == ORT_WORKSHARE
7350 || ctx->region_type == ORT_SIMD
7351 || ctx->region_type == ORT_ACC
7352 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7353 goto do_outer;
7355 flags = omp_default_clause (ctx, decl, in_code, flags);
7357 if ((flags & GOVD_PRIVATE)
7358 && lang_hooks.decls.omp_private_outer_ref (decl))
7359 flags |= GOVD_PRIVATE_OUTER_REF;
7361 omp_add_variable (ctx, decl, flags);
7363 shared = (flags & GOVD_SHARED) != 0;
7364 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7365 goto do_outer;
7368 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7369 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7370 && DECL_SIZE (decl))
7372 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7374 splay_tree_node n2;
7375 tree t = DECL_VALUE_EXPR (decl);
7376 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7377 t = TREE_OPERAND (t, 0);
7378 gcc_assert (DECL_P (t));
7379 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7380 n2->value |= GOVD_SEEN;
7382 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7383 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7384 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7385 != INTEGER_CST))
7387 splay_tree_node n2;
7388 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7389 gcc_assert (DECL_P (t));
7390 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7391 if (n2)
7392 omp_notice_variable (ctx, t, true);
7396 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7397 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7399 /* If nothing changed, there's nothing left to do. */
7400 if ((n->value & flags) == flags)
7401 return ret;
7402 flags |= n->value;
7403 n->value = flags;
7405 do_outer:
7406 /* If the variable is private in the current context, then we don't
7407 need to propagate anything to an outer context. */
7408 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7409 return ret;
7410 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7411 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7412 return ret;
7413 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7414 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7415 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7416 return ret;
7417 if (ctx->outer_context
7418 && omp_notice_variable (ctx->outer_context, decl, in_code))
7419 return true;
7420 return ret;
7423 /* Verify that DECL is private within CTX. If there's specific information
7424 to the contrary in the innermost scope, generate an error. */
7426 static bool
7427 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7429 splay_tree_node n;
7431 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7432 if (n != NULL)
7434 if (n->value & GOVD_SHARED)
7436 if (ctx == gimplify_omp_ctxp)
7438 if (simd)
7439 error ("iteration variable %qE is predetermined linear",
7440 DECL_NAME (decl));
7441 else
7442 error ("iteration variable %qE should be private",
7443 DECL_NAME (decl));
7444 n->value = GOVD_PRIVATE;
7445 return true;
7447 else
7448 return false;
7450 else if ((n->value & GOVD_EXPLICIT) != 0
7451 && (ctx == gimplify_omp_ctxp
7452 || (ctx->region_type == ORT_COMBINED_PARALLEL
7453 && gimplify_omp_ctxp->outer_context == ctx)))
7455 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7456 error ("iteration variable %qE should not be firstprivate",
7457 DECL_NAME (decl));
7458 else if ((n->value & GOVD_REDUCTION) != 0)
7459 error ("iteration variable %qE should not be reduction",
7460 DECL_NAME (decl));
7461 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7462 error ("iteration variable %qE should not be linear",
7463 DECL_NAME (decl));
7464 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7465 error ("iteration variable %qE should not be lastprivate",
7466 DECL_NAME (decl));
7467 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7468 error ("iteration variable %qE should not be private",
7469 DECL_NAME (decl));
7470 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7471 error ("iteration variable %qE is predetermined linear",
7472 DECL_NAME (decl));
7474 return (ctx == gimplify_omp_ctxp
7475 || (ctx->region_type == ORT_COMBINED_PARALLEL
7476 && gimplify_omp_ctxp->outer_context == ctx));
7479 if (ctx->region_type != ORT_WORKSHARE
7480 && ctx->region_type != ORT_SIMD
7481 && ctx->region_type != ORT_ACC)
7482 return false;
7483 else if (ctx->outer_context)
7484 return omp_is_private (ctx->outer_context, decl, simd);
7485 return false;
7488 /* Return true if DECL is private within a parallel region
7489 that binds to the current construct's context or in parallel
7490 region's REDUCTION clause. */
7492 static bool
7493 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7495 splay_tree_node n;
7499 ctx = ctx->outer_context;
7500 if (ctx == NULL)
7502 if (is_global_var (decl))
7503 return false;
7505 /* References might be private, but might be shared too,
7506 when checking for copyprivate, assume they might be
7507 private, otherwise assume they might be shared. */
7508 if (copyprivate)
7509 return true;
7511 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7512 return false;
7514 /* Treat C++ privatized non-static data members outside
7515 of the privatization the same. */
7516 if (omp_member_access_dummy_var (decl))
7517 return false;
7519 return true;
7522 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7524 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7525 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7526 continue;
7528 if (n != NULL)
7530 if ((n->value & GOVD_LOCAL) != 0
7531 && omp_member_access_dummy_var (decl))
7532 return false;
7533 return (n->value & GOVD_SHARED) == 0;
7536 while (ctx->region_type == ORT_WORKSHARE
7537 || ctx->region_type == ORT_SIMD
7538 || ctx->region_type == ORT_ACC);
7539 return false;
7542 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7544 static tree
7545 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7547 tree t = *tp;
7549 /* If this node has been visited, unmark it and keep looking. */
7550 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7551 return t;
7553 if (IS_TYPE_OR_DECL_P (t))
7554 *walk_subtrees = 0;
7555 return NULL_TREE;
7558 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7559 and previous omp contexts. */
7561 static void
7562 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7563 enum omp_region_type region_type,
7564 enum tree_code code)
7566 struct gimplify_omp_ctx *ctx, *outer_ctx;
7567 tree c;
7568 hash_map<tree, tree> *struct_map_to_clause = NULL;
7569 tree *prev_list_p = NULL;
7571 ctx = new_omp_context (region_type);
7572 outer_ctx = ctx->outer_context;
7573 if (code == OMP_TARGET)
7575 if (!lang_GNU_Fortran ())
7576 ctx->target_map_pointers_as_0len_arrays = true;
7577 ctx->target_map_scalars_firstprivate = true;
7579 if (!lang_GNU_Fortran ())
7580 switch (code)
7582 case OMP_TARGET:
7583 case OMP_TARGET_DATA:
7584 case OMP_TARGET_ENTER_DATA:
7585 case OMP_TARGET_EXIT_DATA:
7586 case OACC_DECLARE:
7587 case OACC_HOST_DATA:
7588 ctx->target_firstprivatize_array_bases = true;
7589 default:
7590 break;
7593 while ((c = *list_p) != NULL)
7595 bool remove = false;
7596 bool notice_outer = true;
7597 const char *check_non_private = NULL;
7598 unsigned int flags;
7599 tree decl;
7601 switch (OMP_CLAUSE_CODE (c))
7603 case OMP_CLAUSE_PRIVATE:
7604 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7605 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7607 flags |= GOVD_PRIVATE_OUTER_REF;
7608 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7610 else
7611 notice_outer = false;
7612 goto do_add;
7613 case OMP_CLAUSE_SHARED:
7614 flags = GOVD_SHARED | GOVD_EXPLICIT;
7615 goto do_add;
7616 case OMP_CLAUSE_FIRSTPRIVATE:
7617 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7618 check_non_private = "firstprivate";
7619 goto do_add;
7620 case OMP_CLAUSE_LASTPRIVATE:
7621 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7622 check_non_private = "lastprivate";
7623 decl = OMP_CLAUSE_DECL (c);
7624 if (error_operand_p (decl))
7625 goto do_add;
7626 else if (outer_ctx
7627 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7628 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7629 && splay_tree_lookup (outer_ctx->variables,
7630 (splay_tree_key) decl) == NULL)
7632 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7633 if (outer_ctx->outer_context)
7634 omp_notice_variable (outer_ctx->outer_context, decl, true);
7636 else if (outer_ctx
7637 && (outer_ctx->region_type & ORT_TASK) != 0
7638 && outer_ctx->combined_loop
7639 && splay_tree_lookup (outer_ctx->variables,
7640 (splay_tree_key) decl) == NULL)
7642 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7643 if (outer_ctx->outer_context)
7644 omp_notice_variable (outer_ctx->outer_context, decl, true);
7646 else if (outer_ctx
7647 && (outer_ctx->region_type == ORT_WORKSHARE
7648 || outer_ctx->region_type == ORT_ACC)
7649 && outer_ctx->combined_loop
7650 && splay_tree_lookup (outer_ctx->variables,
7651 (splay_tree_key) decl) == NULL
7652 && !omp_check_private (outer_ctx, decl, false))
7654 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7655 if (outer_ctx->outer_context
7656 && (outer_ctx->outer_context->region_type
7657 == ORT_COMBINED_PARALLEL)
7658 && splay_tree_lookup (outer_ctx->outer_context->variables,
7659 (splay_tree_key) decl) == NULL)
7661 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7662 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7663 if (octx->outer_context)
7665 octx = octx->outer_context;
7666 if (octx->region_type == ORT_WORKSHARE
7667 && octx->combined_loop
7668 && splay_tree_lookup (octx->variables,
7669 (splay_tree_key) decl) == NULL
7670 && !omp_check_private (octx, decl, false))
7672 omp_add_variable (octx, decl,
7673 GOVD_LASTPRIVATE | GOVD_SEEN);
7674 octx = octx->outer_context;
7675 if (octx
7676 && octx->region_type == ORT_COMBINED_TEAMS
7677 && (splay_tree_lookup (octx->variables,
7678 (splay_tree_key) decl)
7679 == NULL))
7681 omp_add_variable (octx, decl,
7682 GOVD_SHARED | GOVD_SEEN);
7683 octx = octx->outer_context;
7686 if (octx)
7687 omp_notice_variable (octx, decl, true);
7690 else if (outer_ctx->outer_context)
7691 omp_notice_variable (outer_ctx->outer_context, decl, true);
7693 goto do_add;
7694 case OMP_CLAUSE_REDUCTION:
7695 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7696 /* OpenACC permits reductions on private variables. */
7697 if (!(region_type & ORT_ACC))
7698 check_non_private = "reduction";
7699 decl = OMP_CLAUSE_DECL (c);
7700 if (TREE_CODE (decl) == MEM_REF)
7702 tree type = TREE_TYPE (decl);
7703 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7704 NULL, is_gimple_val, fb_rvalue, false)
7705 == GS_ERROR)
7707 remove = true;
7708 break;
7710 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7711 if (DECL_P (v))
7713 omp_firstprivatize_variable (ctx, v);
7714 omp_notice_variable (ctx, v, true);
7716 decl = TREE_OPERAND (decl, 0);
7717 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7719 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7720 NULL, is_gimple_val, fb_rvalue, false)
7721 == GS_ERROR)
7723 remove = true;
7724 break;
7726 v = TREE_OPERAND (decl, 1);
7727 if (DECL_P (v))
7729 omp_firstprivatize_variable (ctx, v);
7730 omp_notice_variable (ctx, v, true);
7732 decl = TREE_OPERAND (decl, 0);
7734 if (TREE_CODE (decl) == ADDR_EXPR
7735 || TREE_CODE (decl) == INDIRECT_REF)
7736 decl = TREE_OPERAND (decl, 0);
7738 goto do_add_decl;
7739 case OMP_CLAUSE_LINEAR:
7740 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7741 is_gimple_val, fb_rvalue) == GS_ERROR)
7743 remove = true;
7744 break;
7746 else
7748 if (code == OMP_SIMD
7749 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7751 struct gimplify_omp_ctx *octx = outer_ctx;
7752 if (octx
7753 && octx->region_type == ORT_WORKSHARE
7754 && octx->combined_loop
7755 && !octx->distribute)
7757 if (octx->outer_context
7758 && (octx->outer_context->region_type
7759 == ORT_COMBINED_PARALLEL))
7760 octx = octx->outer_context->outer_context;
7761 else
7762 octx = octx->outer_context;
7764 if (octx
7765 && octx->region_type == ORT_WORKSHARE
7766 && octx->combined_loop
7767 && octx->distribute)
7769 error_at (OMP_CLAUSE_LOCATION (c),
7770 "%<linear%> clause for variable other than "
7771 "loop iterator specified on construct "
7772 "combined with %<distribute%>");
7773 remove = true;
7774 break;
7777 /* For combined #pragma omp parallel for simd, need to put
7778 lastprivate and perhaps firstprivate too on the
7779 parallel. Similarly for #pragma omp for simd. */
7780 struct gimplify_omp_ctx *octx = outer_ctx;
7781 decl = NULL_TREE;
7784 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7785 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7786 break;
7787 decl = OMP_CLAUSE_DECL (c);
7788 if (error_operand_p (decl))
7790 decl = NULL_TREE;
7791 break;
7793 flags = GOVD_SEEN;
7794 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7795 flags |= GOVD_FIRSTPRIVATE;
7796 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7797 flags |= GOVD_LASTPRIVATE;
7798 if (octx
7799 && octx->region_type == ORT_WORKSHARE
7800 && octx->combined_loop)
7802 if (octx->outer_context
7803 && (octx->outer_context->region_type
7804 == ORT_COMBINED_PARALLEL))
7805 octx = octx->outer_context;
7806 else if (omp_check_private (octx, decl, false))
7807 break;
7809 else if (octx
7810 && (octx->region_type & ORT_TASK) != 0
7811 && octx->combined_loop)
7813 else if (octx
7814 && octx->region_type == ORT_COMBINED_PARALLEL
7815 && ctx->region_type == ORT_WORKSHARE
7816 && octx == outer_ctx)
7817 flags = GOVD_SEEN | GOVD_SHARED;
7818 else if (octx
7819 && octx->region_type == ORT_COMBINED_TEAMS)
7820 flags = GOVD_SEEN | GOVD_SHARED;
7821 else if (octx
7822 && octx->region_type == ORT_COMBINED_TARGET)
7824 flags &= ~GOVD_LASTPRIVATE;
7825 if (flags == GOVD_SEEN)
7826 break;
7828 else
7829 break;
7830 splay_tree_node on
7831 = splay_tree_lookup (octx->variables,
7832 (splay_tree_key) decl);
7833 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7835 octx = NULL;
7836 break;
7838 omp_add_variable (octx, decl, flags);
7839 if (octx->outer_context == NULL)
7840 break;
7841 octx = octx->outer_context;
7843 while (1);
7844 if (octx
7845 && decl
7846 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7847 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7848 omp_notice_variable (octx, decl, true);
7850 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7851 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7852 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7854 notice_outer = false;
7855 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7857 goto do_add;
7859 case OMP_CLAUSE_MAP:
7860 decl = OMP_CLAUSE_DECL (c);
7861 if (error_operand_p (decl))
7862 remove = true;
7863 switch (code)
7865 case OMP_TARGET:
7866 break;
7867 case OACC_DATA:
7868 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7869 break;
7870 /* FALLTHRU */
7871 case OMP_TARGET_DATA:
7872 case OMP_TARGET_ENTER_DATA:
7873 case OMP_TARGET_EXIT_DATA:
7874 case OACC_ENTER_DATA:
7875 case OACC_EXIT_DATA:
7876 case OACC_HOST_DATA:
7877 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7878 || (OMP_CLAUSE_MAP_KIND (c)
7879 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7880 /* For target {,enter ,exit }data only the array slice is
7881 mapped, but not the pointer to it. */
7882 remove = true;
7883 break;
7884 default:
7885 break;
7887 if (remove)
7888 break;
7889 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7891 struct gimplify_omp_ctx *octx;
7892 for (octx = outer_ctx; octx; octx = octx->outer_context)
7894 if (octx->region_type != ORT_ACC_HOST_DATA)
7895 break;
7896 splay_tree_node n2
7897 = splay_tree_lookup (octx->variables,
7898 (splay_tree_key) decl);
7899 if (n2)
7900 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7901 "declared in enclosing %<host_data%> region",
7902 DECL_NAME (decl));
7905 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7906 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7907 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7908 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7909 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7911 remove = true;
7912 break;
7914 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7915 || (OMP_CLAUSE_MAP_KIND (c)
7916 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7917 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7919 OMP_CLAUSE_SIZE (c)
7920 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7921 false);
7922 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7923 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7925 if (!DECL_P (decl))
7927 tree d = decl, *pd;
7928 if (TREE_CODE (d) == ARRAY_REF)
7930 while (TREE_CODE (d) == ARRAY_REF)
7931 d = TREE_OPERAND (d, 0);
7932 if (TREE_CODE (d) == COMPONENT_REF
7933 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7934 decl = d;
7936 pd = &OMP_CLAUSE_DECL (c);
7937 if (d == decl
7938 && TREE_CODE (decl) == INDIRECT_REF
7939 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7940 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7941 == REFERENCE_TYPE))
7943 pd = &TREE_OPERAND (decl, 0);
7944 decl = TREE_OPERAND (decl, 0);
7946 if (TREE_CODE (decl) == COMPONENT_REF)
7948 while (TREE_CODE (decl) == COMPONENT_REF)
7949 decl = TREE_OPERAND (decl, 0);
7950 if (TREE_CODE (decl) == INDIRECT_REF
7951 && DECL_P (TREE_OPERAND (decl, 0))
7952 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7953 == REFERENCE_TYPE))
7954 decl = TREE_OPERAND (decl, 0);
7956 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7957 == GS_ERROR)
7959 remove = true;
7960 break;
7962 if (DECL_P (decl))
7964 if (error_operand_p (decl))
7966 remove = true;
7967 break;
7970 tree stype = TREE_TYPE (decl);
7971 if (TREE_CODE (stype) == REFERENCE_TYPE)
7972 stype = TREE_TYPE (stype);
7973 if (TYPE_SIZE_UNIT (stype) == NULL
7974 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7976 error_at (OMP_CLAUSE_LOCATION (c),
7977 "mapping field %qE of variable length "
7978 "structure", OMP_CLAUSE_DECL (c));
7979 remove = true;
7980 break;
7983 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7985 /* Error recovery. */
7986 if (prev_list_p == NULL)
7988 remove = true;
7989 break;
7991 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7993 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7994 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7996 remove = true;
7997 break;
8002 tree offset;
8003 poly_int64 bitsize, bitpos;
8004 machine_mode mode;
8005 int unsignedp, reversep, volatilep = 0;
8006 tree base = OMP_CLAUSE_DECL (c);
8007 while (TREE_CODE (base) == ARRAY_REF)
8008 base = TREE_OPERAND (base, 0);
8009 if (TREE_CODE (base) == INDIRECT_REF)
8010 base = TREE_OPERAND (base, 0);
8011 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8012 &mode, &unsignedp, &reversep,
8013 &volatilep);
8014 tree orig_base = base;
8015 if ((TREE_CODE (base) == INDIRECT_REF
8016 || (TREE_CODE (base) == MEM_REF
8017 && integer_zerop (TREE_OPERAND (base, 1))))
8018 && DECL_P (TREE_OPERAND (base, 0))
8019 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
8020 == REFERENCE_TYPE))
8021 base = TREE_OPERAND (base, 0);
8022 gcc_assert (base == decl
8023 && (offset == NULL_TREE
8024 || poly_int_tree_p (offset)));
8026 splay_tree_node n
8027 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8028 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8029 == GOMP_MAP_ALWAYS_POINTER);
8030 if (n == NULL || (n->value & GOVD_MAP) == 0)
8032 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8033 OMP_CLAUSE_MAP);
8034 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8035 if (orig_base != base)
8036 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8037 else
8038 OMP_CLAUSE_DECL (l) = decl;
8039 OMP_CLAUSE_SIZE (l) = size_int (1);
8040 if (struct_map_to_clause == NULL)
8041 struct_map_to_clause = new hash_map<tree, tree>;
8042 struct_map_to_clause->put (decl, l);
8043 if (ptr)
8045 enum gomp_map_kind mkind
8046 = code == OMP_TARGET_EXIT_DATA
8047 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8048 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8049 OMP_CLAUSE_MAP);
8050 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8051 OMP_CLAUSE_DECL (c2)
8052 = unshare_expr (OMP_CLAUSE_DECL (c));
8053 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8054 OMP_CLAUSE_SIZE (c2)
8055 = TYPE_SIZE_UNIT (ptr_type_node);
8056 OMP_CLAUSE_CHAIN (l) = c2;
8057 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8059 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8060 tree c3
8061 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8062 OMP_CLAUSE_MAP);
8063 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8064 OMP_CLAUSE_DECL (c3)
8065 = unshare_expr (OMP_CLAUSE_DECL (c4));
8066 OMP_CLAUSE_SIZE (c3)
8067 = TYPE_SIZE_UNIT (ptr_type_node);
8068 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8069 OMP_CLAUSE_CHAIN (c2) = c3;
8071 *prev_list_p = l;
8072 prev_list_p = NULL;
8074 else
8076 OMP_CLAUSE_CHAIN (l) = c;
8077 *list_p = l;
8078 list_p = &OMP_CLAUSE_CHAIN (l);
8080 if (orig_base != base && code == OMP_TARGET)
8082 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8083 OMP_CLAUSE_MAP);
8084 enum gomp_map_kind mkind
8085 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8086 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8087 OMP_CLAUSE_DECL (c2) = decl;
8088 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8089 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8090 OMP_CLAUSE_CHAIN (l) = c2;
8092 flags = GOVD_MAP | GOVD_EXPLICIT;
8093 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8094 flags |= GOVD_SEEN;
8095 goto do_add_decl;
8097 else
8099 tree *osc = struct_map_to_clause->get (decl);
8100 tree *sc = NULL, *scp = NULL;
8101 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8102 n->value |= GOVD_SEEN;
8103 poly_offset_int o1, o2;
8104 if (offset)
8105 o1 = wi::to_poly_offset (offset);
8106 else
8107 o1 = 0;
8108 if (maybe_ne (bitpos, 0))
8109 o1 += bits_to_bytes_round_down (bitpos);
8110 sc = &OMP_CLAUSE_CHAIN (*osc);
8111 if (*sc != c
8112 && (OMP_CLAUSE_MAP_KIND (*sc)
8113 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8114 sc = &OMP_CLAUSE_CHAIN (*sc);
8115 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8116 if (ptr && sc == prev_list_p)
8117 break;
8118 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8119 != COMPONENT_REF
8120 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8121 != INDIRECT_REF)
8122 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8123 != ARRAY_REF))
8124 break;
8125 else
8127 tree offset2;
8128 poly_int64 bitsize2, bitpos2;
8129 base = OMP_CLAUSE_DECL (*sc);
8130 if (TREE_CODE (base) == ARRAY_REF)
8132 while (TREE_CODE (base) == ARRAY_REF)
8133 base = TREE_OPERAND (base, 0);
8134 if (TREE_CODE (base) != COMPONENT_REF
8135 || (TREE_CODE (TREE_TYPE (base))
8136 != ARRAY_TYPE))
8137 break;
8139 else if (TREE_CODE (base) == INDIRECT_REF
8140 && (TREE_CODE (TREE_OPERAND (base, 0))
8141 == COMPONENT_REF)
8142 && (TREE_CODE (TREE_TYPE
8143 (TREE_OPERAND (base, 0)))
8144 == REFERENCE_TYPE))
8145 base = TREE_OPERAND (base, 0);
8146 base = get_inner_reference (base, &bitsize2,
8147 &bitpos2, &offset2,
8148 &mode, &unsignedp,
8149 &reversep, &volatilep);
8150 if ((TREE_CODE (base) == INDIRECT_REF
8151 || (TREE_CODE (base) == MEM_REF
8152 && integer_zerop (TREE_OPERAND (base,
8153 1))))
8154 && DECL_P (TREE_OPERAND (base, 0))
8155 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8156 0)))
8157 == REFERENCE_TYPE))
8158 base = TREE_OPERAND (base, 0);
8159 if (base != decl)
8160 break;
8161 if (scp)
8162 continue;
8163 gcc_assert (offset == NULL_TREE
8164 || poly_int_tree_p (offset));
8165 tree d1 = OMP_CLAUSE_DECL (*sc);
8166 tree d2 = OMP_CLAUSE_DECL (c);
8167 while (TREE_CODE (d1) == ARRAY_REF)
8168 d1 = TREE_OPERAND (d1, 0);
8169 while (TREE_CODE (d2) == ARRAY_REF)
8170 d2 = TREE_OPERAND (d2, 0);
8171 if (TREE_CODE (d1) == INDIRECT_REF)
8172 d1 = TREE_OPERAND (d1, 0);
8173 if (TREE_CODE (d2) == INDIRECT_REF)
8174 d2 = TREE_OPERAND (d2, 0);
8175 while (TREE_CODE (d1) == COMPONENT_REF)
8176 if (TREE_CODE (d2) == COMPONENT_REF
8177 && TREE_OPERAND (d1, 1)
8178 == TREE_OPERAND (d2, 1))
8180 d1 = TREE_OPERAND (d1, 0);
8181 d2 = TREE_OPERAND (d2, 0);
8183 else
8184 break;
8185 if (d1 == d2)
8187 error_at (OMP_CLAUSE_LOCATION (c),
8188 "%qE appears more than once in map "
8189 "clauses", OMP_CLAUSE_DECL (c));
8190 remove = true;
8191 break;
8193 if (offset2)
8194 o2 = wi::to_poly_offset (offset2);
8195 else
8196 o2 = 0;
8197 o2 += bits_to_bytes_round_down (bitpos2);
8198 if (maybe_lt (o1, o2)
8199 || (known_eq (o1, o2)
8200 && maybe_lt (bitpos, bitpos2)))
8202 if (ptr)
8203 scp = sc;
8204 else
8205 break;
8208 if (remove)
8209 break;
8210 OMP_CLAUSE_SIZE (*osc)
8211 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8212 size_one_node);
8213 if (ptr)
8215 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8216 OMP_CLAUSE_MAP);
8217 tree cl = NULL_TREE;
8218 enum gomp_map_kind mkind
8219 = code == OMP_TARGET_EXIT_DATA
8220 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8221 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8222 OMP_CLAUSE_DECL (c2)
8223 = unshare_expr (OMP_CLAUSE_DECL (c));
8224 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8225 OMP_CLAUSE_SIZE (c2)
8226 = TYPE_SIZE_UNIT (ptr_type_node);
8227 cl = scp ? *prev_list_p : c2;
8228 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8230 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8231 tree c3
8232 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8233 OMP_CLAUSE_MAP);
8234 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8235 OMP_CLAUSE_DECL (c3)
8236 = unshare_expr (OMP_CLAUSE_DECL (c4));
8237 OMP_CLAUSE_SIZE (c3)
8238 = TYPE_SIZE_UNIT (ptr_type_node);
8239 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8240 if (!scp)
8241 OMP_CLAUSE_CHAIN (c2) = c3;
8242 else
8243 cl = c3;
8245 if (scp)
8246 *scp = c2;
8247 if (sc == prev_list_p)
8249 *sc = cl;
8250 prev_list_p = NULL;
8252 else
8254 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8255 list_p = prev_list_p;
8256 prev_list_p = NULL;
8257 OMP_CLAUSE_CHAIN (c) = *sc;
8258 *sc = cl;
8259 continue;
8262 else if (*sc != c)
8264 *list_p = OMP_CLAUSE_CHAIN (c);
8265 OMP_CLAUSE_CHAIN (c) = *sc;
8266 *sc = c;
8267 continue;
8271 if (!remove
8272 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8273 && OMP_CLAUSE_CHAIN (c)
8274 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8275 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8276 == GOMP_MAP_ALWAYS_POINTER))
8277 prev_list_p = list_p;
8278 break;
8280 flags = GOVD_MAP | GOVD_EXPLICIT;
8281 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8282 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8283 flags |= GOVD_MAP_ALWAYS_TO;
8284 goto do_add;
8286 case OMP_CLAUSE_DEPEND:
8287 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8289 tree deps = OMP_CLAUSE_DECL (c);
8290 while (deps && TREE_CODE (deps) == TREE_LIST)
8292 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8293 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8294 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8295 pre_p, NULL, is_gimple_val, fb_rvalue);
8296 deps = TREE_CHAIN (deps);
8298 break;
8300 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8301 break;
8302 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8304 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8305 NULL, is_gimple_val, fb_rvalue);
8306 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8308 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8310 remove = true;
8311 break;
8313 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8314 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8315 is_gimple_val, fb_rvalue) == GS_ERROR)
8317 remove = true;
8318 break;
8320 break;
8322 case OMP_CLAUSE_TO:
8323 case OMP_CLAUSE_FROM:
8324 case OMP_CLAUSE__CACHE_:
8325 decl = OMP_CLAUSE_DECL (c);
8326 if (error_operand_p (decl))
8328 remove = true;
8329 break;
8331 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8332 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8333 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8334 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8335 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8337 remove = true;
8338 break;
8340 if (!DECL_P (decl))
8342 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8343 NULL, is_gimple_lvalue, fb_lvalue)
8344 == GS_ERROR)
8346 remove = true;
8347 break;
8349 break;
8351 goto do_notice;
8353 case OMP_CLAUSE_USE_DEVICE_PTR:
8354 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8355 goto do_add;
8356 case OMP_CLAUSE_IS_DEVICE_PTR:
8357 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8358 goto do_add;
8360 do_add:
8361 decl = OMP_CLAUSE_DECL (c);
8362 do_add_decl:
8363 if (error_operand_p (decl))
8365 remove = true;
8366 break;
8368 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8370 tree t = omp_member_access_dummy_var (decl);
8371 if (t)
8373 tree v = DECL_VALUE_EXPR (decl);
8374 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8375 if (outer_ctx)
8376 omp_notice_variable (outer_ctx, t, true);
8379 if (code == OACC_DATA
8380 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8381 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8382 flags |= GOVD_MAP_0LEN_ARRAY;
8383 omp_add_variable (ctx, decl, flags);
8384 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8385 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8387 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8388 GOVD_LOCAL | GOVD_SEEN);
8389 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8390 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8391 find_decl_expr,
8392 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8393 NULL) == NULL_TREE)
8394 omp_add_variable (ctx,
8395 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8396 GOVD_LOCAL | GOVD_SEEN);
8397 gimplify_omp_ctxp = ctx;
8398 push_gimplify_context ();
8400 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8401 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8403 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8404 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8405 pop_gimplify_context
8406 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8407 push_gimplify_context ();
8408 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8409 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8410 pop_gimplify_context
8411 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8412 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8413 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8415 gimplify_omp_ctxp = outer_ctx;
8417 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8418 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8420 gimplify_omp_ctxp = ctx;
8421 push_gimplify_context ();
8422 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8424 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8425 NULL, NULL);
8426 TREE_SIDE_EFFECTS (bind) = 1;
8427 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8428 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8430 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8431 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8432 pop_gimplify_context
8433 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8434 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8436 gimplify_omp_ctxp = outer_ctx;
8438 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8439 && OMP_CLAUSE_LINEAR_STMT (c))
8441 gimplify_omp_ctxp = ctx;
8442 push_gimplify_context ();
8443 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8445 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8446 NULL, NULL);
8447 TREE_SIDE_EFFECTS (bind) = 1;
8448 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8449 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8451 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8452 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8453 pop_gimplify_context
8454 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8455 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8457 gimplify_omp_ctxp = outer_ctx;
8459 if (notice_outer)
8460 goto do_notice;
8461 break;
8463 case OMP_CLAUSE_COPYIN:
8464 case OMP_CLAUSE_COPYPRIVATE:
8465 decl = OMP_CLAUSE_DECL (c);
8466 if (error_operand_p (decl))
8468 remove = true;
8469 break;
8471 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8472 && !remove
8473 && !omp_check_private (ctx, decl, true))
8475 remove = true;
8476 if (is_global_var (decl))
8478 if (DECL_THREAD_LOCAL_P (decl))
8479 remove = false;
8480 else if (DECL_HAS_VALUE_EXPR_P (decl))
8482 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8484 if (value
8485 && DECL_P (value)
8486 && DECL_THREAD_LOCAL_P (value))
8487 remove = false;
8490 if (remove)
8491 error_at (OMP_CLAUSE_LOCATION (c),
8492 "copyprivate variable %qE is not threadprivate"
8493 " or private in outer context", DECL_NAME (decl));
8495 do_notice:
8496 if (outer_ctx)
8497 omp_notice_variable (outer_ctx, decl, true);
8498 if (check_non_private
8499 && region_type == ORT_WORKSHARE
8500 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8501 || decl == OMP_CLAUSE_DECL (c)
8502 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8503 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8504 == ADDR_EXPR
8505 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8506 == POINTER_PLUS_EXPR
8507 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8508 (OMP_CLAUSE_DECL (c), 0), 0))
8509 == ADDR_EXPR)))))
8510 && omp_check_private (ctx, decl, false))
8512 error ("%s variable %qE is private in outer context",
8513 check_non_private, DECL_NAME (decl));
8514 remove = true;
8516 break;
8518 case OMP_CLAUSE_IF:
8519 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8520 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8522 const char *p[2];
8523 for (int i = 0; i < 2; i++)
8524 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8526 case OMP_PARALLEL: p[i] = "parallel"; break;
8527 case OMP_TASK: p[i] = "task"; break;
8528 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8529 case OMP_TARGET_DATA: p[i] = "target data"; break;
8530 case OMP_TARGET: p[i] = "target"; break;
8531 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8532 case OMP_TARGET_ENTER_DATA:
8533 p[i] = "target enter data"; break;
8534 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8535 default: gcc_unreachable ();
8537 error_at (OMP_CLAUSE_LOCATION (c),
8538 "expected %qs %<if%> clause modifier rather than %qs",
8539 p[0], p[1]);
8540 remove = true;
8542 /* Fall through. */
8544 case OMP_CLAUSE_FINAL:
8545 OMP_CLAUSE_OPERAND (c, 0)
8546 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8547 /* Fall through. */
8549 case OMP_CLAUSE_SCHEDULE:
8550 case OMP_CLAUSE_NUM_THREADS:
8551 case OMP_CLAUSE_NUM_TEAMS:
8552 case OMP_CLAUSE_THREAD_LIMIT:
8553 case OMP_CLAUSE_DIST_SCHEDULE:
8554 case OMP_CLAUSE_DEVICE:
8555 case OMP_CLAUSE_PRIORITY:
8556 case OMP_CLAUSE_GRAINSIZE:
8557 case OMP_CLAUSE_NUM_TASKS:
8558 case OMP_CLAUSE_HINT:
8559 case OMP_CLAUSE_ASYNC:
8560 case OMP_CLAUSE_WAIT:
8561 case OMP_CLAUSE_NUM_GANGS:
8562 case OMP_CLAUSE_NUM_WORKERS:
8563 case OMP_CLAUSE_VECTOR_LENGTH:
8564 case OMP_CLAUSE_WORKER:
8565 case OMP_CLAUSE_VECTOR:
8566 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8567 is_gimple_val, fb_rvalue) == GS_ERROR)
8568 remove = true;
8569 break;
8571 case OMP_CLAUSE_GANG:
8572 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8573 is_gimple_val, fb_rvalue) == GS_ERROR)
8574 remove = true;
8575 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8576 is_gimple_val, fb_rvalue) == GS_ERROR)
8577 remove = true;
8578 break;
8580 case OMP_CLAUSE_NOWAIT:
8581 case OMP_CLAUSE_ORDERED:
8582 case OMP_CLAUSE_UNTIED:
8583 case OMP_CLAUSE_COLLAPSE:
8584 case OMP_CLAUSE_TILE:
8585 case OMP_CLAUSE_AUTO:
8586 case OMP_CLAUSE_SEQ:
8587 case OMP_CLAUSE_INDEPENDENT:
8588 case OMP_CLAUSE_MERGEABLE:
8589 case OMP_CLAUSE_PROC_BIND:
8590 case OMP_CLAUSE_SAFELEN:
8591 case OMP_CLAUSE_SIMDLEN:
8592 case OMP_CLAUSE_NOGROUP:
8593 case OMP_CLAUSE_THREADS:
8594 case OMP_CLAUSE_SIMD:
8595 break;
8597 case OMP_CLAUSE_DEFAULTMAP:
8598 ctx->target_map_scalars_firstprivate = false;
8599 break;
8601 case OMP_CLAUSE_ALIGNED:
8602 decl = OMP_CLAUSE_DECL (c);
8603 if (error_operand_p (decl))
8605 remove = true;
8606 break;
8608 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8609 is_gimple_val, fb_rvalue) == GS_ERROR)
8611 remove = true;
8612 break;
8614 if (!is_global_var (decl)
8615 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8616 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8617 break;
8619 case OMP_CLAUSE_DEFAULT:
8620 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8621 break;
8623 default:
8624 gcc_unreachable ();
8627 if (code == OACC_DATA
8628 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8629 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8630 remove = true;
8631 if (remove)
8632 *list_p = OMP_CLAUSE_CHAIN (c);
8633 else
8634 list_p = &OMP_CLAUSE_CHAIN (c);
8637 gimplify_omp_ctxp = ctx;
8638 if (struct_map_to_clause)
8639 delete struct_map_to_clause;
8642 /* Return true if DECL is a candidate for shared to firstprivate
8643 optimization. We only consider non-addressable scalars, not
8644 too big, and not references. */
8646 static bool
8647 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8649 if (TREE_ADDRESSABLE (decl))
8650 return false;
8651 tree type = TREE_TYPE (decl);
8652 if (!is_gimple_reg_type (type)
8653 || TREE_CODE (type) == REFERENCE_TYPE
8654 || TREE_ADDRESSABLE (type))
8655 return false;
8656 /* Don't optimize too large decls, as each thread/task will have
8657 its own. */
8658 HOST_WIDE_INT len = int_size_in_bytes (type);
8659 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8660 return false;
8661 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8662 return false;
8663 return true;
8666 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8667 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8668 GOVD_WRITTEN in outer contexts. */
8670 static void
8671 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8673 for (; ctx; ctx = ctx->outer_context)
8675 splay_tree_node n = splay_tree_lookup (ctx->variables,
8676 (splay_tree_key) decl);
8677 if (n == NULL)
8678 continue;
8679 else if (n->value & GOVD_SHARED)
8681 n->value |= GOVD_WRITTEN;
8682 return;
8684 else if (n->value & GOVD_DATA_SHARE_CLASS)
8685 return;
8689 /* Helper callback for walk_gimple_seq to discover possible stores
8690 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8691 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8692 for those. */
8694 static tree
8695 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8697 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8699 *walk_subtrees = 0;
8700 if (!wi->is_lhs)
8701 return NULL_TREE;
8703 tree op = *tp;
8706 if (handled_component_p (op))
8707 op = TREE_OPERAND (op, 0);
8708 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8709 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8710 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8711 else
8712 break;
8714 while (1);
8715 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8716 return NULL_TREE;
8718 omp_mark_stores (gimplify_omp_ctxp, op);
8719 return NULL_TREE;
8722 /* Helper callback for walk_gimple_seq to discover possible stores
8723 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8724 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8725 for those. */
8727 static tree
8728 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8729 bool *handled_ops_p,
8730 struct walk_stmt_info *wi)
8732 gimple *stmt = gsi_stmt (*gsi_p);
8733 switch (gimple_code (stmt))
8735 /* Don't recurse on OpenMP constructs for which
8736 gimplify_adjust_omp_clauses already handled the bodies,
8737 except handle gimple_omp_for_pre_body. */
8738 case GIMPLE_OMP_FOR:
8739 *handled_ops_p = true;
8740 if (gimple_omp_for_pre_body (stmt))
8741 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8742 omp_find_stores_stmt, omp_find_stores_op, wi);
8743 break;
8744 case GIMPLE_OMP_PARALLEL:
8745 case GIMPLE_OMP_TASK:
8746 case GIMPLE_OMP_SECTIONS:
8747 case GIMPLE_OMP_SINGLE:
8748 case GIMPLE_OMP_TARGET:
8749 case GIMPLE_OMP_TEAMS:
8750 case GIMPLE_OMP_CRITICAL:
8751 *handled_ops_p = true;
8752 break;
8753 default:
8754 break;
8756 return NULL_TREE;
8759 struct gimplify_adjust_omp_clauses_data
8761 tree *list_p;
8762 gimple_seq *pre_p;
8765 /* For all variables that were not actually used within the context,
8766 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8768 static int
8769 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8771 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8772 gimple_seq *pre_p
8773 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8774 tree decl = (tree) n->key;
8775 unsigned flags = n->value;
8776 enum omp_clause_code code;
8777 tree clause;
8778 bool private_debug;
8780 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8781 return 0;
8782 if ((flags & GOVD_SEEN) == 0)
8783 return 0;
8784 if (flags & GOVD_DEBUG_PRIVATE)
8786 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8787 private_debug = true;
8789 else if (flags & GOVD_MAP)
8790 private_debug = false;
8791 else
8792 private_debug
8793 = lang_hooks.decls.omp_private_debug_clause (decl,
8794 !!(flags & GOVD_SHARED));
8795 if (private_debug)
8796 code = OMP_CLAUSE_PRIVATE;
8797 else if (flags & GOVD_MAP)
8799 code = OMP_CLAUSE_MAP;
8800 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8801 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8803 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8804 return 0;
8807 else if (flags & GOVD_SHARED)
8809 if (is_global_var (decl))
8811 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8812 while (ctx != NULL)
8814 splay_tree_node on
8815 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8816 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8817 | GOVD_PRIVATE | GOVD_REDUCTION
8818 | GOVD_LINEAR | GOVD_MAP)) != 0)
8819 break;
8820 ctx = ctx->outer_context;
8822 if (ctx == NULL)
8823 return 0;
8825 code = OMP_CLAUSE_SHARED;
8827 else if (flags & GOVD_PRIVATE)
8828 code = OMP_CLAUSE_PRIVATE;
8829 else if (flags & GOVD_FIRSTPRIVATE)
8831 code = OMP_CLAUSE_FIRSTPRIVATE;
8832 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8833 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8834 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8836 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8837 "%<target%> construct", decl);
8838 return 0;
8841 else if (flags & GOVD_LASTPRIVATE)
8842 code = OMP_CLAUSE_LASTPRIVATE;
8843 else if (flags & GOVD_ALIGNED)
8844 return 0;
8845 else
8846 gcc_unreachable ();
8848 if (((flags & GOVD_LASTPRIVATE)
8849 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8850 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8851 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8853 tree chain = *list_p;
8854 clause = build_omp_clause (input_location, code);
8855 OMP_CLAUSE_DECL (clause) = decl;
8856 OMP_CLAUSE_CHAIN (clause) = chain;
8857 if (private_debug)
8858 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8859 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8860 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8861 else if (code == OMP_CLAUSE_SHARED
8862 && (flags & GOVD_WRITTEN) == 0
8863 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8864 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8865 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8866 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8867 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8869 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8870 OMP_CLAUSE_DECL (nc) = decl;
8871 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8872 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8873 OMP_CLAUSE_DECL (clause)
8874 = build_simple_mem_ref_loc (input_location, decl);
8875 OMP_CLAUSE_DECL (clause)
8876 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8877 build_int_cst (build_pointer_type (char_type_node), 0));
8878 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8879 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8880 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8881 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8882 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8883 OMP_CLAUSE_CHAIN (nc) = chain;
8884 OMP_CLAUSE_CHAIN (clause) = nc;
8885 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8886 gimplify_omp_ctxp = ctx->outer_context;
8887 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8888 pre_p, NULL, is_gimple_val, fb_rvalue);
8889 gimplify_omp_ctxp = ctx;
8891 else if (code == OMP_CLAUSE_MAP)
8893 int kind;
8894 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8895 switch (flags & (GOVD_MAP_TO_ONLY
8896 | GOVD_MAP_FORCE
8897 | GOVD_MAP_FORCE_PRESENT))
8899 case 0:
8900 kind = GOMP_MAP_TOFROM;
8901 break;
8902 case GOVD_MAP_FORCE:
8903 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8904 break;
8905 case GOVD_MAP_TO_ONLY:
8906 kind = GOMP_MAP_TO;
8907 break;
8908 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8909 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8910 break;
8911 case GOVD_MAP_FORCE_PRESENT:
8912 kind = GOMP_MAP_FORCE_PRESENT;
8913 break;
8914 default:
8915 gcc_unreachable ();
8917 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8918 if (DECL_SIZE (decl)
8919 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8921 tree decl2 = DECL_VALUE_EXPR (decl);
8922 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8923 decl2 = TREE_OPERAND (decl2, 0);
8924 gcc_assert (DECL_P (decl2));
8925 tree mem = build_simple_mem_ref (decl2);
8926 OMP_CLAUSE_DECL (clause) = mem;
8927 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8928 if (gimplify_omp_ctxp->outer_context)
8930 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8931 omp_notice_variable (ctx, decl2, true);
8932 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8934 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8935 OMP_CLAUSE_MAP);
8936 OMP_CLAUSE_DECL (nc) = decl;
8937 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8938 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8939 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8940 else
8941 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8942 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8943 OMP_CLAUSE_CHAIN (clause) = nc;
8945 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8946 && lang_hooks.decls.omp_privatize_by_reference (decl))
8948 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8949 OMP_CLAUSE_SIZE (clause)
8950 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8951 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8952 gimplify_omp_ctxp = ctx->outer_context;
8953 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8954 pre_p, NULL, is_gimple_val, fb_rvalue);
8955 gimplify_omp_ctxp = ctx;
8956 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8957 OMP_CLAUSE_MAP);
8958 OMP_CLAUSE_DECL (nc) = decl;
8959 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8960 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8961 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8962 OMP_CLAUSE_CHAIN (clause) = nc;
8964 else
8965 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8967 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8969 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8970 OMP_CLAUSE_DECL (nc) = decl;
8971 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8972 OMP_CLAUSE_CHAIN (nc) = chain;
8973 OMP_CLAUSE_CHAIN (clause) = nc;
8974 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8975 gimplify_omp_ctxp = ctx->outer_context;
8976 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8977 gimplify_omp_ctxp = ctx;
8979 *list_p = clause;
8980 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8981 gimplify_omp_ctxp = ctx->outer_context;
8982 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8983 if (gimplify_omp_ctxp)
8984 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8985 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8986 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8987 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8988 true);
8989 gimplify_omp_ctxp = ctx;
8990 return 0;
8993 static void
8994 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8995 enum tree_code code)
8997 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8998 tree c, decl;
9000 if (body)
9002 struct gimplify_omp_ctx *octx;
9003 for (octx = ctx; octx; octx = octx->outer_context)
9004 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
9005 break;
9006 if (octx)
9008 struct walk_stmt_info wi;
9009 memset (&wi, 0, sizeof (wi));
9010 walk_gimple_seq (body, omp_find_stores_stmt,
9011 omp_find_stores_op, &wi);
9014 while ((c = *list_p) != NULL)
9016 splay_tree_node n;
9017 bool remove = false;
9019 switch (OMP_CLAUSE_CODE (c))
9021 case OMP_CLAUSE_FIRSTPRIVATE:
9022 if ((ctx->region_type & ORT_TARGET)
9023 && (ctx->region_type & ORT_ACC) == 0
9024 && TYPE_ATOMIC (strip_array_types
9025 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9027 error_at (OMP_CLAUSE_LOCATION (c),
9028 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9029 "%<target%> construct", OMP_CLAUSE_DECL (c));
9030 remove = true;
9031 break;
9033 /* FALLTHRU */
9034 case OMP_CLAUSE_PRIVATE:
9035 case OMP_CLAUSE_SHARED:
9036 case OMP_CLAUSE_LINEAR:
9037 decl = OMP_CLAUSE_DECL (c);
9038 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9039 remove = !(n->value & GOVD_SEEN);
9040 if (! remove)
9042 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9043 if ((n->value & GOVD_DEBUG_PRIVATE)
9044 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9046 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9047 || ((n->value & GOVD_DATA_SHARE_CLASS)
9048 == GOVD_SHARED));
9049 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9050 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9052 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9053 && (n->value & GOVD_WRITTEN) == 0
9054 && DECL_P (decl)
9055 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9056 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9057 else if (DECL_P (decl)
9058 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9059 && (n->value & GOVD_WRITTEN) != 0)
9060 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9061 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9062 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9063 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9065 break;
9067 case OMP_CLAUSE_LASTPRIVATE:
9068 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9069 accurately reflect the presence of a FIRSTPRIVATE clause. */
9070 decl = OMP_CLAUSE_DECL (c);
9071 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9072 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9073 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9074 if (code == OMP_DISTRIBUTE
9075 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9077 remove = true;
9078 error_at (OMP_CLAUSE_LOCATION (c),
9079 "same variable used in %<firstprivate%> and "
9080 "%<lastprivate%> clauses on %<distribute%> "
9081 "construct");
9083 if (!remove
9084 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9085 && DECL_P (decl)
9086 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9087 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9088 break;
9090 case OMP_CLAUSE_ALIGNED:
9091 decl = OMP_CLAUSE_DECL (c);
9092 if (!is_global_var (decl))
9094 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9095 remove = n == NULL || !(n->value & GOVD_SEEN);
9096 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9098 struct gimplify_omp_ctx *octx;
9099 if (n != NULL
9100 && (n->value & (GOVD_DATA_SHARE_CLASS
9101 & ~GOVD_FIRSTPRIVATE)))
9102 remove = true;
9103 else
9104 for (octx = ctx->outer_context; octx;
9105 octx = octx->outer_context)
9107 n = splay_tree_lookup (octx->variables,
9108 (splay_tree_key) decl);
9109 if (n == NULL)
9110 continue;
9111 if (n->value & GOVD_LOCAL)
9112 break;
9113 /* We have to avoid assigning a shared variable
9114 to itself when trying to add
9115 __builtin_assume_aligned. */
9116 if (n->value & GOVD_SHARED)
9118 remove = true;
9119 break;
9124 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9126 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9127 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9128 remove = true;
9130 break;
9132 case OMP_CLAUSE_MAP:
9133 if (code == OMP_TARGET_EXIT_DATA
9134 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9136 remove = true;
9137 break;
9139 decl = OMP_CLAUSE_DECL (c);
9140 /* Data clauses associated with acc parallel reductions must be
9141 compatible with present_or_copy. Warn and adjust the clause
9142 if that is not the case. */
9143 if (ctx->region_type == ORT_ACC_PARALLEL)
9145 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9146 n = NULL;
9148 if (DECL_P (t))
9149 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9151 if (n && (n->value & GOVD_REDUCTION))
9153 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9155 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9156 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9157 && kind != GOMP_MAP_FORCE_PRESENT
9158 && kind != GOMP_MAP_POINTER)
9160 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9161 "incompatible data clause with reduction "
9162 "on %qE; promoting to present_or_copy",
9163 DECL_NAME (t));
9164 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9168 if (!DECL_P (decl))
9170 if ((ctx->region_type & ORT_TARGET) != 0
9171 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9173 if (TREE_CODE (decl) == INDIRECT_REF
9174 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9175 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9176 == REFERENCE_TYPE))
9177 decl = TREE_OPERAND (decl, 0);
9178 if (TREE_CODE (decl) == COMPONENT_REF)
9180 while (TREE_CODE (decl) == COMPONENT_REF)
9181 decl = TREE_OPERAND (decl, 0);
9182 if (DECL_P (decl))
9184 n = splay_tree_lookup (ctx->variables,
9185 (splay_tree_key) decl);
9186 if (!(n->value & GOVD_SEEN))
9187 remove = true;
9191 break;
9193 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9194 if ((ctx->region_type & ORT_TARGET) != 0
9195 && !(n->value & GOVD_SEEN)
9196 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9197 && (!is_global_var (decl)
9198 || !lookup_attribute ("omp declare target link",
9199 DECL_ATTRIBUTES (decl))))
9201 remove = true;
9202 /* For struct element mapping, if struct is never referenced
9203 in target block and none of the mapping has always modifier,
9204 remove all the struct element mappings, which immediately
9205 follow the GOMP_MAP_STRUCT map clause. */
9206 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9208 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9209 while (cnt--)
9210 OMP_CLAUSE_CHAIN (c)
9211 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9214 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9215 && code == OMP_TARGET_EXIT_DATA)
9216 remove = true;
9217 else if (DECL_SIZE (decl)
9218 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9219 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9220 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9221 && (OMP_CLAUSE_MAP_KIND (c)
9222 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9224 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9225 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9226 INTEGER_CST. */
9227 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9229 tree decl2 = DECL_VALUE_EXPR (decl);
9230 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9231 decl2 = TREE_OPERAND (decl2, 0);
9232 gcc_assert (DECL_P (decl2));
9233 tree mem = build_simple_mem_ref (decl2);
9234 OMP_CLAUSE_DECL (c) = mem;
9235 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9236 if (ctx->outer_context)
9238 omp_notice_variable (ctx->outer_context, decl2, true);
9239 omp_notice_variable (ctx->outer_context,
9240 OMP_CLAUSE_SIZE (c), true);
9242 if (((ctx->region_type & ORT_TARGET) != 0
9243 || !ctx->target_firstprivatize_array_bases)
9244 && ((n->value & GOVD_SEEN) == 0
9245 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9247 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9248 OMP_CLAUSE_MAP);
9249 OMP_CLAUSE_DECL (nc) = decl;
9250 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9251 if (ctx->target_firstprivatize_array_bases)
9252 OMP_CLAUSE_SET_MAP_KIND (nc,
9253 GOMP_MAP_FIRSTPRIVATE_POINTER);
9254 else
9255 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9256 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9257 OMP_CLAUSE_CHAIN (c) = nc;
9258 c = nc;
9261 else
9263 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9264 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9265 gcc_assert ((n->value & GOVD_SEEN) == 0
9266 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9267 == 0));
9269 break;
9271 case OMP_CLAUSE_TO:
9272 case OMP_CLAUSE_FROM:
9273 case OMP_CLAUSE__CACHE_:
9274 decl = OMP_CLAUSE_DECL (c);
9275 if (!DECL_P (decl))
9276 break;
9277 if (DECL_SIZE (decl)
9278 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9280 tree decl2 = DECL_VALUE_EXPR (decl);
9281 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9282 decl2 = TREE_OPERAND (decl2, 0);
9283 gcc_assert (DECL_P (decl2));
9284 tree mem = build_simple_mem_ref (decl2);
9285 OMP_CLAUSE_DECL (c) = mem;
9286 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9287 if (ctx->outer_context)
9289 omp_notice_variable (ctx->outer_context, decl2, true);
9290 omp_notice_variable (ctx->outer_context,
9291 OMP_CLAUSE_SIZE (c), true);
9294 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9295 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9296 break;
9298 case OMP_CLAUSE_REDUCTION:
9299 decl = OMP_CLAUSE_DECL (c);
9300 /* OpenACC reductions need a present_or_copy data clause.
9301 Add one if necessary. Error is the reduction is private. */
9302 if (ctx->region_type == ORT_ACC_PARALLEL)
9304 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9305 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9306 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9307 "reduction on %qE", DECL_NAME (decl));
9308 else if ((n->value & GOVD_MAP) == 0)
9310 tree next = OMP_CLAUSE_CHAIN (c);
9311 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9312 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9313 OMP_CLAUSE_DECL (nc) = decl;
9314 OMP_CLAUSE_CHAIN (c) = nc;
9315 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9316 while (1)
9318 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9319 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9320 break;
9321 nc = OMP_CLAUSE_CHAIN (nc);
9323 OMP_CLAUSE_CHAIN (nc) = next;
9324 n->value |= GOVD_MAP;
9327 if (DECL_P (decl)
9328 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9329 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9330 break;
9331 case OMP_CLAUSE_COPYIN:
9332 case OMP_CLAUSE_COPYPRIVATE:
9333 case OMP_CLAUSE_IF:
9334 case OMP_CLAUSE_NUM_THREADS:
9335 case OMP_CLAUSE_NUM_TEAMS:
9336 case OMP_CLAUSE_THREAD_LIMIT:
9337 case OMP_CLAUSE_DIST_SCHEDULE:
9338 case OMP_CLAUSE_DEVICE:
9339 case OMP_CLAUSE_SCHEDULE:
9340 case OMP_CLAUSE_NOWAIT:
9341 case OMP_CLAUSE_ORDERED:
9342 case OMP_CLAUSE_DEFAULT:
9343 case OMP_CLAUSE_UNTIED:
9344 case OMP_CLAUSE_COLLAPSE:
9345 case OMP_CLAUSE_FINAL:
9346 case OMP_CLAUSE_MERGEABLE:
9347 case OMP_CLAUSE_PROC_BIND:
9348 case OMP_CLAUSE_SAFELEN:
9349 case OMP_CLAUSE_SIMDLEN:
9350 case OMP_CLAUSE_DEPEND:
9351 case OMP_CLAUSE_PRIORITY:
9352 case OMP_CLAUSE_GRAINSIZE:
9353 case OMP_CLAUSE_NUM_TASKS:
9354 case OMP_CLAUSE_NOGROUP:
9355 case OMP_CLAUSE_THREADS:
9356 case OMP_CLAUSE_SIMD:
9357 case OMP_CLAUSE_HINT:
9358 case OMP_CLAUSE_DEFAULTMAP:
9359 case OMP_CLAUSE_USE_DEVICE_PTR:
9360 case OMP_CLAUSE_IS_DEVICE_PTR:
9361 case OMP_CLAUSE_ASYNC:
9362 case OMP_CLAUSE_WAIT:
9363 case OMP_CLAUSE_INDEPENDENT:
9364 case OMP_CLAUSE_NUM_GANGS:
9365 case OMP_CLAUSE_NUM_WORKERS:
9366 case OMP_CLAUSE_VECTOR_LENGTH:
9367 case OMP_CLAUSE_GANG:
9368 case OMP_CLAUSE_WORKER:
9369 case OMP_CLAUSE_VECTOR:
9370 case OMP_CLAUSE_AUTO:
9371 case OMP_CLAUSE_SEQ:
9372 case OMP_CLAUSE_TILE:
9373 break;
9375 default:
9376 gcc_unreachable ();
9379 if (remove)
9380 *list_p = OMP_CLAUSE_CHAIN (c);
9381 else
9382 list_p = &OMP_CLAUSE_CHAIN (c);
9385 /* Add in any implicit data sharing. */
9386 struct gimplify_adjust_omp_clauses_data data;
9387 data.list_p = list_p;
9388 data.pre_p = pre_p;
9389 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9391 gimplify_omp_ctxp = ctx->outer_context;
9392 delete_omp_context (ctx);
9395 /* Gimplify OACC_CACHE. */
9397 static void
9398 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9400 tree expr = *expr_p;
9402 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9403 OACC_CACHE);
9404 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9405 OACC_CACHE);
9407 /* TODO: Do something sensible with this information. */
9409 *expr_p = NULL_TREE;
9412 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9413 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9414 kind. The entry kind will replace the one in CLAUSE, while the exit
9415 kind will be used in a new omp_clause and returned to the caller. */
9417 static tree
9418 gimplify_oacc_declare_1 (tree clause)
9420 HOST_WIDE_INT kind, new_op;
9421 bool ret = false;
9422 tree c = NULL;
9424 kind = OMP_CLAUSE_MAP_KIND (clause);
9426 switch (kind)
9428 case GOMP_MAP_ALLOC:
9429 case GOMP_MAP_FORCE_ALLOC:
9430 case GOMP_MAP_FORCE_TO:
9431 new_op = GOMP_MAP_DELETE;
9432 ret = true;
9433 break;
9435 case GOMP_MAP_FORCE_FROM:
9436 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9437 new_op = GOMP_MAP_FORCE_FROM;
9438 ret = true;
9439 break;
9441 case GOMP_MAP_FORCE_TOFROM:
9442 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9443 new_op = GOMP_MAP_FORCE_FROM;
9444 ret = true;
9445 break;
9447 case GOMP_MAP_FROM:
9448 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9449 new_op = GOMP_MAP_FROM;
9450 ret = true;
9451 break;
9453 case GOMP_MAP_TOFROM:
9454 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9455 new_op = GOMP_MAP_FROM;
9456 ret = true;
9457 break;
9459 case GOMP_MAP_DEVICE_RESIDENT:
9460 case GOMP_MAP_FORCE_DEVICEPTR:
9461 case GOMP_MAP_FORCE_PRESENT:
9462 case GOMP_MAP_LINK:
9463 case GOMP_MAP_POINTER:
9464 case GOMP_MAP_TO:
9465 break;
9467 default:
9468 gcc_unreachable ();
9469 break;
9472 if (ret)
9474 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9475 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9476 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9479 return c;
9482 /* Gimplify OACC_DECLARE. */
9484 static void
9485 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9487 tree expr = *expr_p;
9488 gomp_target *stmt;
9489 tree clauses, t, decl;
9491 clauses = OACC_DECLARE_CLAUSES (expr);
9493 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9494 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9496 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9498 decl = OMP_CLAUSE_DECL (t);
9500 if (TREE_CODE (decl) == MEM_REF)
9501 decl = TREE_OPERAND (decl, 0);
9503 if (VAR_P (decl) && !is_oacc_declared (decl))
9505 tree attr = get_identifier ("oacc declare target");
9506 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9507 DECL_ATTRIBUTES (decl));
9510 if (VAR_P (decl)
9511 && !is_global_var (decl)
9512 && DECL_CONTEXT (decl) == current_function_decl)
9514 tree c = gimplify_oacc_declare_1 (t);
9515 if (c)
9517 if (oacc_declare_returns == NULL)
9518 oacc_declare_returns = new hash_map<tree, tree>;
9520 oacc_declare_returns->put (decl, c);
9524 if (gimplify_omp_ctxp)
9525 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9528 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9529 clauses);
9531 gimplify_seq_add_stmt (pre_p, stmt);
9533 *expr_p = NULL_TREE;
9536 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9537 gimplification of the body, as well as scanning the body for used
9538 variables. We need to do this scan now, because variable-sized
9539 decls will be decomposed during gimplification. */
9541 static void
9542 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9544 tree expr = *expr_p;
9545 gimple *g;
9546 gimple_seq body = NULL;
9548 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9549 OMP_PARALLEL_COMBINED (expr)
9550 ? ORT_COMBINED_PARALLEL
9551 : ORT_PARALLEL, OMP_PARALLEL);
9553 push_gimplify_context ();
9555 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9556 if (gimple_code (g) == GIMPLE_BIND)
9557 pop_gimplify_context (g);
9558 else
9559 pop_gimplify_context (NULL);
9561 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9562 OMP_PARALLEL);
9564 g = gimple_build_omp_parallel (body,
9565 OMP_PARALLEL_CLAUSES (expr),
9566 NULL_TREE, NULL_TREE);
9567 if (OMP_PARALLEL_COMBINED (expr))
9568 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9569 gimplify_seq_add_stmt (pre_p, g);
9570 *expr_p = NULL_TREE;
9573 /* Gimplify the contents of an OMP_TASK statement. This involves
9574 gimplification of the body, as well as scanning the body for used
9575 variables. We need to do this scan now, because variable-sized
9576 decls will be decomposed during gimplification. */
9578 static void
9579 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9581 tree expr = *expr_p;
9582 gimple *g;
9583 gimple_seq body = NULL;
9585 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9586 omp_find_clause (OMP_TASK_CLAUSES (expr),
9587 OMP_CLAUSE_UNTIED)
9588 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9590 push_gimplify_context ();
9592 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9593 if (gimple_code (g) == GIMPLE_BIND)
9594 pop_gimplify_context (g);
9595 else
9596 pop_gimplify_context (NULL);
9598 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9599 OMP_TASK);
9601 g = gimple_build_omp_task (body,
9602 OMP_TASK_CLAUSES (expr),
9603 NULL_TREE, NULL_TREE,
9604 NULL_TREE, NULL_TREE, NULL_TREE);
9605 gimplify_seq_add_stmt (pre_p, g);
9606 *expr_p = NULL_TREE;
9609 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9610 with non-NULL OMP_FOR_INIT. */
9612 static tree
9613 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9615 *walk_subtrees = 0;
9616 switch (TREE_CODE (*tp))
9618 case OMP_FOR:
9619 *walk_subtrees = 1;
9620 /* FALLTHRU */
9621 case OMP_SIMD:
9622 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9623 return *tp;
9624 break;
9625 case BIND_EXPR:
9626 case STATEMENT_LIST:
9627 case OMP_PARALLEL:
9628 *walk_subtrees = 1;
9629 break;
9630 default:
9631 break;
9633 return NULL_TREE;
9636 /* Gimplify the gross structure of an OMP_FOR statement. */
9638 static enum gimplify_status
9639 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9641 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9642 enum gimplify_status ret = GS_ALL_DONE;
9643 enum gimplify_status tret;
9644 gomp_for *gfor;
9645 gimple_seq for_body, for_pre_body;
9646 int i;
9647 bitmap has_decl_expr = NULL;
9648 enum omp_region_type ort = ORT_WORKSHARE;
9650 orig_for_stmt = for_stmt = *expr_p;
9652 switch (TREE_CODE (for_stmt))
9654 case OMP_FOR:
9655 case OMP_DISTRIBUTE:
9656 break;
9657 case OACC_LOOP:
9658 ort = ORT_ACC;
9659 break;
9660 case OMP_TASKLOOP:
9661 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9662 ort = ORT_UNTIED_TASK;
9663 else
9664 ort = ORT_TASK;
9665 break;
9666 case OMP_SIMD:
9667 ort = ORT_SIMD;
9668 break;
9669 default:
9670 gcc_unreachable ();
9673 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9674 clause for the IV. */
9675 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9677 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9678 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9679 decl = TREE_OPERAND (t, 0);
9680 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9682 && OMP_CLAUSE_DECL (c) == decl)
9684 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9685 break;
9689 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9691 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9692 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9693 find_combined_omp_for, NULL, NULL);
9694 if (inner_for_stmt == NULL_TREE)
9696 gcc_assert (seen_error ());
9697 *expr_p = NULL_TREE;
9698 return GS_ERROR;
9702 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9703 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9704 TREE_CODE (for_stmt));
9706 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9707 gimplify_omp_ctxp->distribute = true;
9709 /* Handle OMP_FOR_INIT. */
9710 for_pre_body = NULL;
9711 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9713 has_decl_expr = BITMAP_ALLOC (NULL);
9714 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9715 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9716 == VAR_DECL)
9718 t = OMP_FOR_PRE_BODY (for_stmt);
9719 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9721 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9723 tree_stmt_iterator si;
9724 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9725 tsi_next (&si))
9727 t = tsi_stmt (si);
9728 if (TREE_CODE (t) == DECL_EXPR
9729 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9730 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9734 if (OMP_FOR_PRE_BODY (for_stmt))
9736 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9737 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9738 else
9740 struct gimplify_omp_ctx ctx;
9741 memset (&ctx, 0, sizeof (ctx));
9742 ctx.region_type = ORT_NONE;
9743 gimplify_omp_ctxp = &ctx;
9744 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9745 gimplify_omp_ctxp = NULL;
9748 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9750 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9751 for_stmt = inner_for_stmt;
9753 /* For taskloop, need to gimplify the start, end and step before the
9754 taskloop, outside of the taskloop omp context. */
9755 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9757 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9759 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9760 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9762 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9763 TREE_OPERAND (t, 1)
9764 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9765 gimple_seq_empty_p (for_pre_body)
9766 ? pre_p : &for_pre_body, NULL,
9767 false);
9768 /* Reference to pointer conversion is considered useless,
9769 but is significant for firstprivate clause. Force it
9770 here. */
9771 if (TREE_CODE (type) == POINTER_TYPE
9772 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9773 == REFERENCE_TYPE))
9775 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9776 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9777 TREE_OPERAND (t, 1));
9778 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9779 ? pre_p : &for_pre_body);
9780 TREE_OPERAND (t, 1) = v;
9782 tree c = build_omp_clause (input_location,
9783 OMP_CLAUSE_FIRSTPRIVATE);
9784 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9785 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9786 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9789 /* Handle OMP_FOR_COND. */
9790 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9791 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9793 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9794 TREE_OPERAND (t, 1)
9795 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9796 gimple_seq_empty_p (for_pre_body)
9797 ? pre_p : &for_pre_body, NULL,
9798 false);
9799 /* Reference to pointer conversion is considered useless,
9800 but is significant for firstprivate clause. Force it
9801 here. */
9802 if (TREE_CODE (type) == POINTER_TYPE
9803 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9804 == REFERENCE_TYPE))
9806 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9807 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9808 TREE_OPERAND (t, 1));
9809 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9810 ? pre_p : &for_pre_body);
9811 TREE_OPERAND (t, 1) = v;
9813 tree c = build_omp_clause (input_location,
9814 OMP_CLAUSE_FIRSTPRIVATE);
9815 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9816 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9817 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9820 /* Handle OMP_FOR_INCR. */
9821 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9822 if (TREE_CODE (t) == MODIFY_EXPR)
9824 decl = TREE_OPERAND (t, 0);
9825 t = TREE_OPERAND (t, 1);
9826 tree *tp = &TREE_OPERAND (t, 1);
9827 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9828 tp = &TREE_OPERAND (t, 0);
9830 if (!is_gimple_constant (*tp))
9832 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9833 ? pre_p : &for_pre_body;
9834 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9835 tree c = build_omp_clause (input_location,
9836 OMP_CLAUSE_FIRSTPRIVATE);
9837 OMP_CLAUSE_DECL (c) = *tp;
9838 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9839 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9844 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9845 OMP_TASKLOOP);
9848 if (orig_for_stmt != for_stmt)
9849 gimplify_omp_ctxp->combined_loop = true;
9851 for_body = NULL;
9852 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9853 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9854 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9855 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9857 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9858 bool is_doacross = false;
9859 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9861 is_doacross = true;
9862 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9863 (OMP_FOR_INIT (for_stmt))
9864 * 2);
9866 int collapse = 1, tile = 0;
9867 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9868 if (c)
9869 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9870 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9871 if (c)
9872 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9873 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9875 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9876 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9877 decl = TREE_OPERAND (t, 0);
9878 gcc_assert (DECL_P (decl));
9879 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9880 || POINTER_TYPE_P (TREE_TYPE (decl)));
9881 if (is_doacross)
9883 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9884 gimplify_omp_ctxp->loop_iter_var.quick_push
9885 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9886 else
9887 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9888 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9891 /* Make sure the iteration variable is private. */
9892 tree c = NULL_TREE;
9893 tree c2 = NULL_TREE;
9894 if (orig_for_stmt != for_stmt)
9895 /* Do this only on innermost construct for combined ones. */;
9896 else if (ort == ORT_SIMD)
9898 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9899 (splay_tree_key) decl);
9900 omp_is_private (gimplify_omp_ctxp, decl,
9901 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9902 != 1));
9903 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9904 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9905 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9907 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9908 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9909 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9910 if (has_decl_expr
9911 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9913 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9914 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9916 struct gimplify_omp_ctx *outer
9917 = gimplify_omp_ctxp->outer_context;
9918 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9920 if (outer->region_type == ORT_WORKSHARE
9921 && outer->combined_loop)
9923 n = splay_tree_lookup (outer->variables,
9924 (splay_tree_key)decl);
9925 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9927 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9928 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9930 else
9932 struct gimplify_omp_ctx *octx = outer->outer_context;
9933 if (octx
9934 && octx->region_type == ORT_COMBINED_PARALLEL
9935 && octx->outer_context
9936 && (octx->outer_context->region_type
9937 == ORT_WORKSHARE)
9938 && octx->outer_context->combined_loop)
9940 octx = octx->outer_context;
9941 n = splay_tree_lookup (octx->variables,
9942 (splay_tree_key)decl);
9943 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9945 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9946 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9953 OMP_CLAUSE_DECL (c) = decl;
9954 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9955 OMP_FOR_CLAUSES (for_stmt) = c;
9956 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9957 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9959 if (outer->region_type == ORT_WORKSHARE
9960 && outer->combined_loop)
9962 if (outer->outer_context
9963 && (outer->outer_context->region_type
9964 == ORT_COMBINED_PARALLEL))
9965 outer = outer->outer_context;
9966 else if (omp_check_private (outer, decl, false))
9967 outer = NULL;
9969 else if (((outer->region_type & ORT_TASK) != 0)
9970 && outer->combined_loop
9971 && !omp_check_private (gimplify_omp_ctxp,
9972 decl, false))
9974 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9976 omp_notice_variable (outer, decl, true);
9977 outer = NULL;
9979 if (outer)
9981 n = splay_tree_lookup (outer->variables,
9982 (splay_tree_key)decl);
9983 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9985 omp_add_variable (outer, decl,
9986 GOVD_LASTPRIVATE | GOVD_SEEN);
9987 if (outer->region_type == ORT_COMBINED_PARALLEL
9988 && outer->outer_context
9989 && (outer->outer_context->region_type
9990 == ORT_WORKSHARE)
9991 && outer->outer_context->combined_loop)
9993 outer = outer->outer_context;
9994 n = splay_tree_lookup (outer->variables,
9995 (splay_tree_key)decl);
9996 if (omp_check_private (outer, decl, false))
9997 outer = NULL;
9998 else if (n == NULL
9999 || ((n->value & GOVD_DATA_SHARE_CLASS)
10000 == 0))
10001 omp_add_variable (outer, decl,
10002 GOVD_LASTPRIVATE
10003 | GOVD_SEEN);
10004 else
10005 outer = NULL;
10007 if (outer && outer->outer_context
10008 && (outer->outer_context->region_type
10009 == ORT_COMBINED_TEAMS))
10011 outer = outer->outer_context;
10012 n = splay_tree_lookup (outer->variables,
10013 (splay_tree_key)decl);
10014 if (n == NULL
10015 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10016 omp_add_variable (outer, decl,
10017 GOVD_SHARED | GOVD_SEEN);
10018 else
10019 outer = NULL;
10021 if (outer && outer->outer_context)
10022 omp_notice_variable (outer->outer_context, decl,
10023 true);
10028 else
10030 bool lastprivate
10031 = (!has_decl_expr
10032 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10033 struct gimplify_omp_ctx *outer
10034 = gimplify_omp_ctxp->outer_context;
10035 if (outer && lastprivate)
10037 if (outer->region_type == ORT_WORKSHARE
10038 && outer->combined_loop)
10040 n = splay_tree_lookup (outer->variables,
10041 (splay_tree_key)decl);
10042 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10044 lastprivate = false;
10045 outer = NULL;
10047 else if (outer->outer_context
10048 && (outer->outer_context->region_type
10049 == ORT_COMBINED_PARALLEL))
10050 outer = outer->outer_context;
10051 else if (omp_check_private (outer, decl, false))
10052 outer = NULL;
10054 else if (((outer->region_type & ORT_TASK) != 0)
10055 && outer->combined_loop
10056 && !omp_check_private (gimplify_omp_ctxp,
10057 decl, false))
10059 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10061 omp_notice_variable (outer, decl, true);
10062 outer = NULL;
10064 if (outer)
10066 n = splay_tree_lookup (outer->variables,
10067 (splay_tree_key)decl);
10068 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10070 omp_add_variable (outer, decl,
10071 GOVD_LASTPRIVATE | GOVD_SEEN);
10072 if (outer->region_type == ORT_COMBINED_PARALLEL
10073 && outer->outer_context
10074 && (outer->outer_context->region_type
10075 == ORT_WORKSHARE)
10076 && outer->outer_context->combined_loop)
10078 outer = outer->outer_context;
10079 n = splay_tree_lookup (outer->variables,
10080 (splay_tree_key)decl);
10081 if (omp_check_private (outer, decl, false))
10082 outer = NULL;
10083 else if (n == NULL
10084 || ((n->value & GOVD_DATA_SHARE_CLASS)
10085 == 0))
10086 omp_add_variable (outer, decl,
10087 GOVD_LASTPRIVATE
10088 | GOVD_SEEN);
10089 else
10090 outer = NULL;
10092 if (outer && outer->outer_context
10093 && (outer->outer_context->region_type
10094 == ORT_COMBINED_TEAMS))
10096 outer = outer->outer_context;
10097 n = splay_tree_lookup (outer->variables,
10098 (splay_tree_key)decl);
10099 if (n == NULL
10100 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10101 omp_add_variable (outer, decl,
10102 GOVD_SHARED | GOVD_SEEN);
10103 else
10104 outer = NULL;
10106 if (outer && outer->outer_context)
10107 omp_notice_variable (outer->outer_context, decl,
10108 true);
10113 c = build_omp_clause (input_location,
10114 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10115 : OMP_CLAUSE_PRIVATE);
10116 OMP_CLAUSE_DECL (c) = decl;
10117 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10118 OMP_FOR_CLAUSES (for_stmt) = c;
10119 omp_add_variable (gimplify_omp_ctxp, decl,
10120 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10121 | GOVD_EXPLICIT | GOVD_SEEN);
10122 c = NULL_TREE;
10125 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10126 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10127 else
10128 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10130 /* If DECL is not a gimple register, create a temporary variable to act
10131 as an iteration counter. This is valid, since DECL cannot be
10132 modified in the body of the loop. Similarly for any iteration vars
10133 in simd with collapse > 1 where the iterator vars must be
10134 lastprivate. */
10135 if (orig_for_stmt != for_stmt)
10136 var = decl;
10137 else if (!is_gimple_reg (decl)
10138 || (ort == ORT_SIMD
10139 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10141 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10142 /* Make sure omp_add_variable is not called on it prematurely.
10143 We call it ourselves a few lines later. */
10144 gimplify_omp_ctxp = NULL;
10145 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10146 gimplify_omp_ctxp = ctx;
10147 TREE_OPERAND (t, 0) = var;
10149 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10151 if (ort == ORT_SIMD
10152 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10154 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10155 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10156 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10157 OMP_CLAUSE_DECL (c2) = var;
10158 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10159 OMP_FOR_CLAUSES (for_stmt) = c2;
10160 omp_add_variable (gimplify_omp_ctxp, var,
10161 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10162 if (c == NULL_TREE)
10164 c = c2;
10165 c2 = NULL_TREE;
10168 else
10169 omp_add_variable (gimplify_omp_ctxp, var,
10170 GOVD_PRIVATE | GOVD_SEEN);
10172 else
10173 var = decl;
10175 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10176 is_gimple_val, fb_rvalue, false);
10177 ret = MIN (ret, tret);
10178 if (ret == GS_ERROR)
10179 return ret;
10181 /* Handle OMP_FOR_COND. */
10182 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10183 gcc_assert (COMPARISON_CLASS_P (t));
10184 gcc_assert (TREE_OPERAND (t, 0) == decl);
10186 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10187 is_gimple_val, fb_rvalue, false);
10188 ret = MIN (ret, tret);
10190 /* Handle OMP_FOR_INCR. */
10191 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10192 switch (TREE_CODE (t))
10194 case PREINCREMENT_EXPR:
10195 case POSTINCREMENT_EXPR:
10197 tree decl = TREE_OPERAND (t, 0);
10198 /* c_omp_for_incr_canonicalize_ptr() should have been
10199 called to massage things appropriately. */
10200 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10202 if (orig_for_stmt != for_stmt)
10203 break;
10204 t = build_int_cst (TREE_TYPE (decl), 1);
10205 if (c)
10206 OMP_CLAUSE_LINEAR_STEP (c) = t;
10207 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10208 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10209 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10210 break;
10213 case PREDECREMENT_EXPR:
10214 case POSTDECREMENT_EXPR:
10215 /* c_omp_for_incr_canonicalize_ptr() should have been
10216 called to massage things appropriately. */
10217 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10218 if (orig_for_stmt != for_stmt)
10219 break;
10220 t = build_int_cst (TREE_TYPE (decl), -1);
10221 if (c)
10222 OMP_CLAUSE_LINEAR_STEP (c) = t;
10223 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10224 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10225 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10226 break;
10228 case MODIFY_EXPR:
10229 gcc_assert (TREE_OPERAND (t, 0) == decl);
10230 TREE_OPERAND (t, 0) = var;
10232 t = TREE_OPERAND (t, 1);
10233 switch (TREE_CODE (t))
10235 case PLUS_EXPR:
10236 if (TREE_OPERAND (t, 1) == decl)
10238 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10239 TREE_OPERAND (t, 0) = var;
10240 break;
10243 /* Fallthru. */
10244 case MINUS_EXPR:
10245 case POINTER_PLUS_EXPR:
10246 gcc_assert (TREE_OPERAND (t, 0) == decl);
10247 TREE_OPERAND (t, 0) = var;
10248 break;
10249 default:
10250 gcc_unreachable ();
10253 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10254 is_gimple_val, fb_rvalue, false);
10255 ret = MIN (ret, tret);
10256 if (c)
10258 tree step = TREE_OPERAND (t, 1);
10259 tree stept = TREE_TYPE (decl);
10260 if (POINTER_TYPE_P (stept))
10261 stept = sizetype;
10262 step = fold_convert (stept, step);
10263 if (TREE_CODE (t) == MINUS_EXPR)
10264 step = fold_build1 (NEGATE_EXPR, stept, step);
10265 OMP_CLAUSE_LINEAR_STEP (c) = step;
10266 if (step != TREE_OPERAND (t, 1))
10268 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10269 &for_pre_body, NULL,
10270 is_gimple_val, fb_rvalue, false);
10271 ret = MIN (ret, tret);
10274 break;
10276 default:
10277 gcc_unreachable ();
10280 if (c2)
10282 gcc_assert (c);
10283 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10286 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10288 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10289 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10290 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10291 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10292 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10293 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10294 && OMP_CLAUSE_DECL (c) == decl)
10296 if (is_doacross && (collapse == 1 || i >= collapse))
10297 t = var;
10298 else
10300 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10301 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10302 gcc_assert (TREE_OPERAND (t, 0) == var);
10303 t = TREE_OPERAND (t, 1);
10304 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10305 || TREE_CODE (t) == MINUS_EXPR
10306 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10307 gcc_assert (TREE_OPERAND (t, 0) == var);
10308 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10309 is_doacross ? var : decl,
10310 TREE_OPERAND (t, 1));
10312 gimple_seq *seq;
10313 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10314 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10315 else
10316 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10317 push_gimplify_context ();
10318 gimplify_assign (decl, t, seq);
10319 gimple *bind = NULL;
10320 if (gimplify_ctxp->temps)
10322 bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
10323 *seq = NULL;
10324 gimplify_seq_add_stmt (seq, bind);
10326 pop_gimplify_context (bind);
10331 BITMAP_FREE (has_decl_expr);
10333 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10335 push_gimplify_context ();
10336 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10338 OMP_FOR_BODY (orig_for_stmt)
10339 = build3 (BIND_EXPR, void_type_node, NULL,
10340 OMP_FOR_BODY (orig_for_stmt), NULL);
10341 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10345 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10346 &for_body);
10348 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10350 if (gimple_code (g) == GIMPLE_BIND)
10351 pop_gimplify_context (g);
10352 else
10353 pop_gimplify_context (NULL);
10356 if (orig_for_stmt != for_stmt)
10357 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10359 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10360 decl = TREE_OPERAND (t, 0);
10361 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10362 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10363 gimplify_omp_ctxp = ctx->outer_context;
10364 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10365 gimplify_omp_ctxp = ctx;
10366 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10367 TREE_OPERAND (t, 0) = var;
10368 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10369 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10370 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10373 gimplify_adjust_omp_clauses (pre_p, for_body,
10374 &OMP_FOR_CLAUSES (orig_for_stmt),
10375 TREE_CODE (orig_for_stmt));
10377 int kind;
10378 switch (TREE_CODE (orig_for_stmt))
10380 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10381 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10382 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10383 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10384 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10385 default:
10386 gcc_unreachable ();
10388 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10389 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10390 for_pre_body);
10391 if (orig_for_stmt != for_stmt)
10392 gimple_omp_for_set_combined_p (gfor, true);
10393 if (gimplify_omp_ctxp
10394 && (gimplify_omp_ctxp->combined_loop
10395 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10396 && gimplify_omp_ctxp->outer_context
10397 && gimplify_omp_ctxp->outer_context->combined_loop)))
10399 gimple_omp_for_set_combined_into_p (gfor, true);
10400 if (gimplify_omp_ctxp->combined_loop)
10401 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10402 else
10403 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10406 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10408 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10409 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10410 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10411 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10412 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10413 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10414 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10415 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10418 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10419 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10420 The outer taskloop stands for computing the number of iterations,
10421 counts for collapsed loops and holding taskloop specific clauses.
10422 The task construct stands for the effect of data sharing on the
10423 explicit task it creates and the inner taskloop stands for expansion
10424 of the static loop inside of the explicit task construct. */
10425 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10427 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10428 tree task_clauses = NULL_TREE;
10429 tree c = *gfor_clauses_ptr;
10430 tree *gtask_clauses_ptr = &task_clauses;
10431 tree outer_for_clauses = NULL_TREE;
10432 tree *gforo_clauses_ptr = &outer_for_clauses;
10433 for (; c; c = OMP_CLAUSE_CHAIN (c))
10434 switch (OMP_CLAUSE_CODE (c))
10436 /* These clauses are allowed on task, move them there. */
10437 case OMP_CLAUSE_SHARED:
10438 case OMP_CLAUSE_FIRSTPRIVATE:
10439 case OMP_CLAUSE_DEFAULT:
10440 case OMP_CLAUSE_IF:
10441 case OMP_CLAUSE_UNTIED:
10442 case OMP_CLAUSE_FINAL:
10443 case OMP_CLAUSE_MERGEABLE:
10444 case OMP_CLAUSE_PRIORITY:
10445 *gtask_clauses_ptr = c;
10446 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10447 break;
10448 case OMP_CLAUSE_PRIVATE:
10449 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10451 /* We want private on outer for and firstprivate
10452 on task. */
10453 *gtask_clauses_ptr
10454 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10455 OMP_CLAUSE_FIRSTPRIVATE);
10456 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10457 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10458 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10459 *gforo_clauses_ptr = c;
10460 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10462 else
10464 *gtask_clauses_ptr = c;
10465 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10467 break;
10468 /* These clauses go into outer taskloop clauses. */
10469 case OMP_CLAUSE_GRAINSIZE:
10470 case OMP_CLAUSE_NUM_TASKS:
10471 case OMP_CLAUSE_NOGROUP:
10472 *gforo_clauses_ptr = c;
10473 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10474 break;
10475 /* Taskloop clause we duplicate on both taskloops. */
10476 case OMP_CLAUSE_COLLAPSE:
10477 *gfor_clauses_ptr = c;
10478 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10479 *gforo_clauses_ptr = copy_node (c);
10480 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10481 break;
10482 /* For lastprivate, keep the clause on inner taskloop, and add
10483 a shared clause on task. If the same decl is also firstprivate,
10484 add also firstprivate clause on the inner taskloop. */
10485 case OMP_CLAUSE_LASTPRIVATE:
10486 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10488 /* For taskloop C++ lastprivate IVs, we want:
10489 1) private on outer taskloop
10490 2) firstprivate and shared on task
10491 3) lastprivate on inner taskloop */
10492 *gtask_clauses_ptr
10493 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10494 OMP_CLAUSE_FIRSTPRIVATE);
10495 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10496 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10497 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10498 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10499 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10500 OMP_CLAUSE_PRIVATE);
10501 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10502 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10503 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10504 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10506 *gfor_clauses_ptr = c;
10507 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10508 *gtask_clauses_ptr
10509 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10510 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10511 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10512 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10513 gtask_clauses_ptr
10514 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10515 break;
10516 default:
10517 gcc_unreachable ();
10519 *gfor_clauses_ptr = NULL_TREE;
10520 *gtask_clauses_ptr = NULL_TREE;
10521 *gforo_clauses_ptr = NULL_TREE;
10522 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10523 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10524 NULL_TREE, NULL_TREE, NULL_TREE);
10525 gimple_omp_task_set_taskloop_p (g, true);
10526 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10527 gomp_for *gforo
10528 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10529 gimple_omp_for_collapse (gfor),
10530 gimple_omp_for_pre_body (gfor));
10531 gimple_omp_for_set_pre_body (gfor, NULL);
10532 gimple_omp_for_set_combined_p (gforo, true);
10533 gimple_omp_for_set_combined_into_p (gfor, true);
10534 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10536 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10537 tree v = create_tmp_var (type);
10538 gimple_omp_for_set_index (gforo, i, v);
10539 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10540 gimple_omp_for_set_initial (gforo, i, t);
10541 gimple_omp_for_set_cond (gforo, i,
10542 gimple_omp_for_cond (gfor, i));
10543 t = unshare_expr (gimple_omp_for_final (gfor, i));
10544 gimple_omp_for_set_final (gforo, i, t);
10545 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10546 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10547 TREE_OPERAND (t, 0) = v;
10548 gimple_omp_for_set_incr (gforo, i, t);
10549 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10550 OMP_CLAUSE_DECL (t) = v;
10551 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10552 gimple_omp_for_set_clauses (gforo, t);
10554 gimplify_seq_add_stmt (pre_p, gforo);
10556 else
10557 gimplify_seq_add_stmt (pre_p, gfor);
10558 if (ret != GS_ALL_DONE)
10559 return GS_ERROR;
10560 *expr_p = NULL_TREE;
10561 return GS_ALL_DONE;
10564 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10565 of OMP_TARGET's body. */
10567 static tree
10568 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10570 *walk_subtrees = 0;
10571 switch (TREE_CODE (*tp))
10573 case OMP_TEAMS:
10574 return *tp;
10575 case BIND_EXPR:
10576 case STATEMENT_LIST:
10577 *walk_subtrees = 1;
10578 break;
10579 default:
10580 break;
10582 return NULL_TREE;
10585 /* Helper function of optimize_target_teams, determine if the expression
10586 can be computed safely before the target construct on the host. */
10588 static tree
10589 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10591 splay_tree_node n;
10593 if (TYPE_P (*tp))
10595 *walk_subtrees = 0;
10596 return NULL_TREE;
10598 switch (TREE_CODE (*tp))
10600 case VAR_DECL:
10601 case PARM_DECL:
10602 case RESULT_DECL:
10603 *walk_subtrees = 0;
10604 if (error_operand_p (*tp)
10605 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10606 || DECL_HAS_VALUE_EXPR_P (*tp)
10607 || DECL_THREAD_LOCAL_P (*tp)
10608 || TREE_SIDE_EFFECTS (*tp)
10609 || TREE_THIS_VOLATILE (*tp))
10610 return *tp;
10611 if (is_global_var (*tp)
10612 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10613 || lookup_attribute ("omp declare target link",
10614 DECL_ATTRIBUTES (*tp))))
10615 return *tp;
10616 if (VAR_P (*tp)
10617 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10618 && !is_global_var (*tp)
10619 && decl_function_context (*tp) == current_function_decl)
10620 return *tp;
10621 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10622 (splay_tree_key) *tp);
10623 if (n == NULL)
10625 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10626 return NULL_TREE;
10627 return *tp;
10629 else if (n->value & GOVD_LOCAL)
10630 return *tp;
10631 else if (n->value & GOVD_FIRSTPRIVATE)
10632 return NULL_TREE;
10633 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10634 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10635 return NULL_TREE;
10636 return *tp;
10637 case INTEGER_CST:
10638 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10639 return *tp;
10640 return NULL_TREE;
10641 case TARGET_EXPR:
10642 if (TARGET_EXPR_INITIAL (*tp)
10643 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10644 return *tp;
10645 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10646 walk_subtrees, NULL);
10647 /* Allow some reasonable subset of integral arithmetics. */
10648 case PLUS_EXPR:
10649 case MINUS_EXPR:
10650 case MULT_EXPR:
10651 case TRUNC_DIV_EXPR:
10652 case CEIL_DIV_EXPR:
10653 case FLOOR_DIV_EXPR:
10654 case ROUND_DIV_EXPR:
10655 case TRUNC_MOD_EXPR:
10656 case CEIL_MOD_EXPR:
10657 case FLOOR_MOD_EXPR:
10658 case ROUND_MOD_EXPR:
10659 case RDIV_EXPR:
10660 case EXACT_DIV_EXPR:
10661 case MIN_EXPR:
10662 case MAX_EXPR:
10663 case LSHIFT_EXPR:
10664 case RSHIFT_EXPR:
10665 case BIT_IOR_EXPR:
10666 case BIT_XOR_EXPR:
10667 case BIT_AND_EXPR:
10668 case NEGATE_EXPR:
10669 case ABS_EXPR:
10670 case BIT_NOT_EXPR:
10671 case NON_LVALUE_EXPR:
10672 CASE_CONVERT:
10673 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10674 return *tp;
10675 return NULL_TREE;
10676 /* And disallow anything else, except for comparisons. */
10677 default:
10678 if (COMPARISON_CLASS_P (*tp))
10679 return NULL_TREE;
10680 return *tp;
10684 /* Try to determine if the num_teams and/or thread_limit expressions
10685 can have their values determined already before entering the
10686 target construct.
10687 INTEGER_CSTs trivially are,
10688 integral decls that are firstprivate (explicitly or implicitly)
10689 or explicitly map(always, to:) or map(always, tofrom:) on the target
10690 region too, and expressions involving simple arithmetics on those
10691 too, function calls are not ok, dereferencing something neither etc.
10692 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10693 EXPR based on what we find:
10694 0 stands for clause not specified at all, use implementation default
10695 -1 stands for value that can't be determined easily before entering
10696 the target construct.
10697 If teams construct is not present at all, use 1 for num_teams
10698 and 0 for thread_limit (only one team is involved, and the thread
10699 limit is implementation defined. */
10701 static void
10702 optimize_target_teams (tree target, gimple_seq *pre_p)
10704 tree body = OMP_BODY (target);
10705 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10706 tree num_teams = integer_zero_node;
10707 tree thread_limit = integer_zero_node;
10708 location_t num_teams_loc = EXPR_LOCATION (target);
10709 location_t thread_limit_loc = EXPR_LOCATION (target);
10710 tree c, *p, expr;
10711 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10713 if (teams == NULL_TREE)
10714 num_teams = integer_one_node;
10715 else
10716 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10718 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10720 p = &num_teams;
10721 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10723 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10725 p = &thread_limit;
10726 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10728 else
10729 continue;
10730 expr = OMP_CLAUSE_OPERAND (c, 0);
10731 if (TREE_CODE (expr) == INTEGER_CST)
10733 *p = expr;
10734 continue;
10736 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10738 *p = integer_minus_one_node;
10739 continue;
10741 *p = expr;
10742 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10743 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10744 == GS_ERROR)
10746 gimplify_omp_ctxp = target_ctx;
10747 *p = integer_minus_one_node;
10748 continue;
10750 gimplify_omp_ctxp = target_ctx;
10751 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10752 OMP_CLAUSE_OPERAND (c, 0) = *p;
10754 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10755 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10756 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10757 OMP_TARGET_CLAUSES (target) = c;
10758 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10759 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10760 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10761 OMP_TARGET_CLAUSES (target) = c;
10764 /* Gimplify the gross structure of several OMP constructs. */
10766 static void
10767 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10769 tree expr = *expr_p;
10770 gimple *stmt;
10771 gimple_seq body = NULL;
10772 enum omp_region_type ort;
10774 switch (TREE_CODE (expr))
10776 case OMP_SECTIONS:
10777 case OMP_SINGLE:
10778 ort = ORT_WORKSHARE;
10779 break;
10780 case OMP_TARGET:
10781 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10782 break;
10783 case OACC_KERNELS:
10784 ort = ORT_ACC_KERNELS;
10785 break;
10786 case OACC_PARALLEL:
10787 ort = ORT_ACC_PARALLEL;
10788 break;
10789 case OACC_DATA:
10790 ort = ORT_ACC_DATA;
10791 break;
10792 case OMP_TARGET_DATA:
10793 ort = ORT_TARGET_DATA;
10794 break;
10795 case OMP_TEAMS:
10796 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10797 break;
10798 case OACC_HOST_DATA:
10799 ort = ORT_ACC_HOST_DATA;
10800 break;
10801 default:
10802 gcc_unreachable ();
10804 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10805 TREE_CODE (expr));
10806 if (TREE_CODE (expr) == OMP_TARGET)
10807 optimize_target_teams (expr, pre_p);
10808 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10810 push_gimplify_context ();
10811 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10812 if (gimple_code (g) == GIMPLE_BIND)
10813 pop_gimplify_context (g);
10814 else
10815 pop_gimplify_context (NULL);
10816 if ((ort & ORT_TARGET_DATA) != 0)
10818 enum built_in_function end_ix;
10819 switch (TREE_CODE (expr))
10821 case OACC_DATA:
10822 case OACC_HOST_DATA:
10823 end_ix = BUILT_IN_GOACC_DATA_END;
10824 break;
10825 case OMP_TARGET_DATA:
10826 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10827 break;
10828 default:
10829 gcc_unreachable ();
10831 tree fn = builtin_decl_explicit (end_ix);
10832 g = gimple_build_call (fn, 0);
10833 gimple_seq cleanup = NULL;
10834 gimple_seq_add_stmt (&cleanup, g);
10835 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10836 body = NULL;
10837 gimple_seq_add_stmt (&body, g);
10840 else
10841 gimplify_and_add (OMP_BODY (expr), &body);
10842 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10843 TREE_CODE (expr));
10845 switch (TREE_CODE (expr))
10847 case OACC_DATA:
10848 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10849 OMP_CLAUSES (expr));
10850 break;
10851 case OACC_KERNELS:
10852 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10853 OMP_CLAUSES (expr));
10854 break;
10855 case OACC_HOST_DATA:
10856 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10857 OMP_CLAUSES (expr));
10858 break;
10859 case OACC_PARALLEL:
10860 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10861 OMP_CLAUSES (expr));
10862 break;
10863 case OMP_SECTIONS:
10864 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10865 break;
10866 case OMP_SINGLE:
10867 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10868 break;
10869 case OMP_TARGET:
10870 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10871 OMP_CLAUSES (expr));
10872 break;
10873 case OMP_TARGET_DATA:
10874 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10875 OMP_CLAUSES (expr));
10876 break;
10877 case OMP_TEAMS:
10878 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10879 break;
10880 default:
10881 gcc_unreachable ();
10884 gimplify_seq_add_stmt (pre_p, stmt);
10885 *expr_p = NULL_TREE;
10888 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10889 target update constructs. */
10891 static void
10892 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10894 tree expr = *expr_p;
10895 int kind;
10896 gomp_target *stmt;
10897 enum omp_region_type ort = ORT_WORKSHARE;
10899 switch (TREE_CODE (expr))
10901 case OACC_ENTER_DATA:
10902 case OACC_EXIT_DATA:
10903 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10904 ort = ORT_ACC;
10905 break;
10906 case OACC_UPDATE:
10907 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10908 ort = ORT_ACC;
10909 break;
10910 case OMP_TARGET_UPDATE:
10911 kind = GF_OMP_TARGET_KIND_UPDATE;
10912 break;
10913 case OMP_TARGET_ENTER_DATA:
10914 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10915 break;
10916 case OMP_TARGET_EXIT_DATA:
10917 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10918 break;
10919 default:
10920 gcc_unreachable ();
10922 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10923 ort, TREE_CODE (expr));
10924 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10925 TREE_CODE (expr));
10926 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10928 gimplify_seq_add_stmt (pre_p, stmt);
10929 *expr_p = NULL_TREE;
10932 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10933 stabilized the lhs of the atomic operation as *ADDR. Return true if
10934 EXPR is this stabilized form. */
10936 static bool
10937 goa_lhs_expr_p (tree expr, tree addr)
10939 /* Also include casts to other type variants. The C front end is fond
10940 of adding these for e.g. volatile variables. This is like
10941 STRIP_TYPE_NOPS but includes the main variant lookup. */
10942 STRIP_USELESS_TYPE_CONVERSION (expr);
10944 if (TREE_CODE (expr) == INDIRECT_REF)
10946 expr = TREE_OPERAND (expr, 0);
10947 while (expr != addr
10948 && (CONVERT_EXPR_P (expr)
10949 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10950 && TREE_CODE (expr) == TREE_CODE (addr)
10951 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10953 expr = TREE_OPERAND (expr, 0);
10954 addr = TREE_OPERAND (addr, 0);
10956 if (expr == addr)
10957 return true;
10958 return (TREE_CODE (addr) == ADDR_EXPR
10959 && TREE_CODE (expr) == ADDR_EXPR
10960 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10962 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10963 return true;
10964 return false;
10967 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10968 expression does not involve the lhs, evaluate it into a temporary.
10969 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10970 or -1 if an error was encountered. */
10972 static int
10973 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10974 tree lhs_var)
10976 tree expr = *expr_p;
10977 int saw_lhs;
10979 if (goa_lhs_expr_p (expr, lhs_addr))
10981 *expr_p = lhs_var;
10982 return 1;
10984 if (is_gimple_val (expr))
10985 return 0;
10987 saw_lhs = 0;
10988 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10990 case tcc_binary:
10991 case tcc_comparison:
10992 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10993 lhs_var);
10994 /* FALLTHRU */
10995 case tcc_unary:
10996 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10997 lhs_var);
10998 break;
10999 case tcc_expression:
11000 switch (TREE_CODE (expr))
11002 case TRUTH_ANDIF_EXPR:
11003 case TRUTH_ORIF_EXPR:
11004 case TRUTH_AND_EXPR:
11005 case TRUTH_OR_EXPR:
11006 case TRUTH_XOR_EXPR:
11007 case BIT_INSERT_EXPR:
11008 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11009 lhs_addr, lhs_var);
11010 /* FALLTHRU */
11011 case TRUTH_NOT_EXPR:
11012 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11013 lhs_addr, lhs_var);
11014 break;
11015 case COMPOUND_EXPR:
11016 /* Break out any preevaluations from cp_build_modify_expr. */
11017 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11018 expr = TREE_OPERAND (expr, 1))
11019 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11020 *expr_p = expr;
11021 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11022 default:
11023 break;
11025 break;
11026 case tcc_reference:
11027 if (TREE_CODE (expr) == BIT_FIELD_REF)
11028 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11029 lhs_addr, lhs_var);
11030 break;
11031 default:
11032 break;
11035 if (saw_lhs == 0)
11037 enum gimplify_status gs;
11038 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11039 if (gs != GS_ALL_DONE)
11040 saw_lhs = -1;
11043 return saw_lhs;
11046 /* Gimplify an OMP_ATOMIC statement. */
11048 static enum gimplify_status
11049 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11051 tree addr = TREE_OPERAND (*expr_p, 0);
11052 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11053 ? NULL : TREE_OPERAND (*expr_p, 1);
11054 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11055 tree tmp_load;
11056 gomp_atomic_load *loadstmt;
11057 gomp_atomic_store *storestmt;
11059 tmp_load = create_tmp_reg (type);
11060 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11061 return GS_ERROR;
11063 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11064 != GS_ALL_DONE)
11065 return GS_ERROR;
11067 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11068 gimplify_seq_add_stmt (pre_p, loadstmt);
11069 if (rhs)
11071 /* BIT_INSERT_EXPR is not valid for non-integral bitfield
11072 representatives. Use BIT_FIELD_REF on the lhs instead. */
11073 if (TREE_CODE (rhs) == BIT_INSERT_EXPR
11074 && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
11076 tree bitpos = TREE_OPERAND (rhs, 2);
11077 tree op1 = TREE_OPERAND (rhs, 1);
11078 tree bitsize;
11079 tree tmp_store = tmp_load;
11080 if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
11081 tmp_store = get_initialized_tmp_var (tmp_load, pre_p, NULL);
11082 if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
11083 bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
11084 else
11085 bitsize = TYPE_SIZE (TREE_TYPE (op1));
11086 gcc_assert (TREE_OPERAND (rhs, 0) == tmp_load);
11087 tree t = build2_loc (EXPR_LOCATION (rhs),
11088 MODIFY_EXPR, void_type_node,
11089 build3_loc (EXPR_LOCATION (rhs), BIT_FIELD_REF,
11090 TREE_TYPE (op1), tmp_store, bitsize,
11091 bitpos), op1);
11092 gimplify_and_add (t, pre_p);
11093 rhs = tmp_store;
11095 if (gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11096 != GS_ALL_DONE)
11097 return GS_ERROR;
11100 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11101 rhs = tmp_load;
11102 storestmt = gimple_build_omp_atomic_store (rhs);
11103 gimplify_seq_add_stmt (pre_p, storestmt);
11104 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11106 gimple_omp_atomic_set_seq_cst (loadstmt);
11107 gimple_omp_atomic_set_seq_cst (storestmt);
11109 switch (TREE_CODE (*expr_p))
11111 case OMP_ATOMIC_READ:
11112 case OMP_ATOMIC_CAPTURE_OLD:
11113 *expr_p = tmp_load;
11114 gimple_omp_atomic_set_need_value (loadstmt);
11115 break;
11116 case OMP_ATOMIC_CAPTURE_NEW:
11117 *expr_p = rhs;
11118 gimple_omp_atomic_set_need_value (storestmt);
11119 break;
11120 default:
11121 *expr_p = NULL;
11122 break;
11125 return GS_ALL_DONE;
11128 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11129 body, and adding some EH bits. */
11131 static enum gimplify_status
11132 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11134 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11135 gimple *body_stmt;
11136 gtransaction *trans_stmt;
11137 gimple_seq body = NULL;
11138 int subcode = 0;
11140 /* Wrap the transaction body in a BIND_EXPR so we have a context
11141 where to put decls for OMP. */
11142 if (TREE_CODE (tbody) != BIND_EXPR)
11144 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11145 TREE_SIDE_EFFECTS (bind) = 1;
11146 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11147 TRANSACTION_EXPR_BODY (expr) = bind;
11150 push_gimplify_context ();
11151 temp = voidify_wrapper_expr (*expr_p, NULL);
11153 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11154 pop_gimplify_context (body_stmt);
11156 trans_stmt = gimple_build_transaction (body);
11157 if (TRANSACTION_EXPR_OUTER (expr))
11158 subcode = GTMA_IS_OUTER;
11159 else if (TRANSACTION_EXPR_RELAXED (expr))
11160 subcode = GTMA_IS_RELAXED;
11161 gimple_transaction_set_subcode (trans_stmt, subcode);
11163 gimplify_seq_add_stmt (pre_p, trans_stmt);
11165 if (temp)
11167 *expr_p = temp;
11168 return GS_OK;
11171 *expr_p = NULL_TREE;
11172 return GS_ALL_DONE;
11175 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11176 is the OMP_BODY of the original EXPR (which has already been
11177 gimplified so it's not present in the EXPR).
11179 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11181 static gimple *
11182 gimplify_omp_ordered (tree expr, gimple_seq body)
11184 tree c, decls;
11185 int failures = 0;
11186 unsigned int i;
11187 tree source_c = NULL_TREE;
11188 tree sink_c = NULL_TREE;
11190 if (gimplify_omp_ctxp)
11192 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11193 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11194 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11195 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11196 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11198 error_at (OMP_CLAUSE_LOCATION (c),
11199 "%<ordered%> construct with %<depend%> clause must be "
11200 "closely nested inside a loop with %<ordered%> clause "
11201 "with a parameter");
11202 failures++;
11204 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11205 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11207 bool fail = false;
11208 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11209 decls && TREE_CODE (decls) == TREE_LIST;
11210 decls = TREE_CHAIN (decls), ++i)
11211 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11212 continue;
11213 else if (TREE_VALUE (decls)
11214 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11216 error_at (OMP_CLAUSE_LOCATION (c),
11217 "variable %qE is not an iteration "
11218 "of outermost loop %d, expected %qE",
11219 TREE_VALUE (decls), i + 1,
11220 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11221 fail = true;
11222 failures++;
11224 else
11225 TREE_VALUE (decls)
11226 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11227 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11229 error_at (OMP_CLAUSE_LOCATION (c),
11230 "number of variables in %<depend(sink)%> "
11231 "clause does not match number of "
11232 "iteration variables");
11233 failures++;
11235 sink_c = c;
11237 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11238 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11240 if (source_c)
11242 error_at (OMP_CLAUSE_LOCATION (c),
11243 "more than one %<depend(source)%> clause on an "
11244 "%<ordered%> construct");
11245 failures++;
11247 else
11248 source_c = c;
11251 if (source_c && sink_c)
11253 error_at (OMP_CLAUSE_LOCATION (source_c),
11254 "%<depend(source)%> clause specified together with "
11255 "%<depend(sink:)%> clauses on the same construct");
11256 failures++;
11259 if (failures)
11260 return gimple_build_nop ();
11261 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11264 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11265 expression produces a value to be used as an operand inside a GIMPLE
11266 statement, the value will be stored back in *EXPR_P. This value will
11267 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11268 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11269 emitted in PRE_P and POST_P.
11271 Additionally, this process may overwrite parts of the input
11272 expression during gimplification. Ideally, it should be
11273 possible to do non-destructive gimplification.
11275 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11276 the expression needs to evaluate to a value to be used as
11277 an operand in a GIMPLE statement, this value will be stored in
11278 *EXPR_P on exit. This happens when the caller specifies one
11279 of fb_lvalue or fb_rvalue fallback flags.
11281 PRE_P will contain the sequence of GIMPLE statements corresponding
11282 to the evaluation of EXPR and all the side-effects that must
11283 be executed before the main expression. On exit, the last
11284 statement of PRE_P is the core statement being gimplified. For
11285 instance, when gimplifying 'if (++a)' the last statement in
11286 PRE_P will be 'if (t.1)' where t.1 is the result of
11287 pre-incrementing 'a'.
11289 POST_P will contain the sequence of GIMPLE statements corresponding
11290 to the evaluation of all the side-effects that must be executed
11291 after the main expression. If this is NULL, the post
11292 side-effects are stored at the end of PRE_P.
11294 The reason why the output is split in two is to handle post
11295 side-effects explicitly. In some cases, an expression may have
11296 inner and outer post side-effects which need to be emitted in
11297 an order different from the one given by the recursive
11298 traversal. For instance, for the expression (*p--)++ the post
11299 side-effects of '--' must actually occur *after* the post
11300 side-effects of '++'. However, gimplification will first visit
11301 the inner expression, so if a separate POST sequence was not
11302 used, the resulting sequence would be:
11304 1 t.1 = *p
11305 2 p = p - 1
11306 3 t.2 = t.1 + 1
11307 4 *p = t.2
11309 However, the post-decrement operation in line #2 must not be
11310 evaluated until after the store to *p at line #4, so the
11311 correct sequence should be:
11313 1 t.1 = *p
11314 2 t.2 = t.1 + 1
11315 3 *p = t.2
11316 4 p = p - 1
11318 So, by specifying a separate post queue, it is possible
11319 to emit the post side-effects in the correct order.
11320 If POST_P is NULL, an internal queue will be used. Before
11321 returning to the caller, the sequence POST_P is appended to
11322 the main output sequence PRE_P.
11324 GIMPLE_TEST_F points to a function that takes a tree T and
11325 returns nonzero if T is in the GIMPLE form requested by the
11326 caller. The GIMPLE predicates are in gimple.c.
11328 FALLBACK tells the function what sort of a temporary we want if
11329 gimplification cannot produce an expression that complies with
11330 GIMPLE_TEST_F.
11332 fb_none means that no temporary should be generated
11333 fb_rvalue means that an rvalue is OK to generate
11334 fb_lvalue means that an lvalue is OK to generate
11335 fb_either means that either is OK, but an lvalue is preferable.
11336 fb_mayfail means that gimplification may fail (in which case
11337 GS_ERROR will be returned)
11339 The return value is either GS_ERROR or GS_ALL_DONE, since this
11340 function iterates until EXPR is completely gimplified or an error
11341 occurs. */
11343 enum gimplify_status
11344 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11345 bool (*gimple_test_f) (tree), fallback_t fallback)
11347 tree tmp;
11348 gimple_seq internal_pre = NULL;
11349 gimple_seq internal_post = NULL;
11350 tree save_expr;
11351 bool is_statement;
11352 location_t saved_location;
11353 enum gimplify_status ret;
11354 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11355 tree label;
11357 save_expr = *expr_p;
11358 if (save_expr == NULL_TREE)
11359 return GS_ALL_DONE;
11361 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11362 is_statement = gimple_test_f == is_gimple_stmt;
11363 if (is_statement)
11364 gcc_assert (pre_p);
11366 /* Consistency checks. */
11367 if (gimple_test_f == is_gimple_reg)
11368 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11369 else if (gimple_test_f == is_gimple_val
11370 || gimple_test_f == is_gimple_call_addr
11371 || gimple_test_f == is_gimple_condexpr
11372 || gimple_test_f == is_gimple_mem_rhs
11373 || gimple_test_f == is_gimple_mem_rhs_or_call
11374 || gimple_test_f == is_gimple_reg_rhs
11375 || gimple_test_f == is_gimple_reg_rhs_or_call
11376 || gimple_test_f == is_gimple_asm_val
11377 || gimple_test_f == is_gimple_mem_ref_addr)
11378 gcc_assert (fallback & fb_rvalue);
11379 else if (gimple_test_f == is_gimple_min_lval
11380 || gimple_test_f == is_gimple_lvalue)
11381 gcc_assert (fallback & fb_lvalue);
11382 else if (gimple_test_f == is_gimple_addressable)
11383 gcc_assert (fallback & fb_either);
11384 else if (gimple_test_f == is_gimple_stmt)
11385 gcc_assert (fallback == fb_none);
11386 else
11388 /* We should have recognized the GIMPLE_TEST_F predicate to
11389 know what kind of fallback to use in case a temporary is
11390 needed to hold the value or address of *EXPR_P. */
11391 gcc_unreachable ();
11394 /* We used to check the predicate here and return immediately if it
11395 succeeds. This is wrong; the design is for gimplification to be
11396 idempotent, and for the predicates to only test for valid forms, not
11397 whether they are fully simplified. */
11398 if (pre_p == NULL)
11399 pre_p = &internal_pre;
11401 if (post_p == NULL)
11402 post_p = &internal_post;
11404 /* Remember the last statements added to PRE_P and POST_P. Every
11405 new statement added by the gimplification helpers needs to be
11406 annotated with location information. To centralize the
11407 responsibility, we remember the last statement that had been
11408 added to both queues before gimplifying *EXPR_P. If
11409 gimplification produces new statements in PRE_P and POST_P, those
11410 statements will be annotated with the same location information
11411 as *EXPR_P. */
11412 pre_last_gsi = gsi_last (*pre_p);
11413 post_last_gsi = gsi_last (*post_p);
11415 saved_location = input_location;
11416 if (save_expr != error_mark_node
11417 && EXPR_HAS_LOCATION (*expr_p))
11418 input_location = EXPR_LOCATION (*expr_p);
11420 /* Loop over the specific gimplifiers until the toplevel node
11421 remains the same. */
11424 /* Strip away as many useless type conversions as possible
11425 at the toplevel. */
11426 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11428 /* Remember the expr. */
11429 save_expr = *expr_p;
11431 /* Die, die, die, my darling. */
11432 if (error_operand_p (save_expr))
11434 ret = GS_ERROR;
11435 break;
11438 /* Do any language-specific gimplification. */
11439 ret = ((enum gimplify_status)
11440 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11441 if (ret == GS_OK)
11443 if (*expr_p == NULL_TREE)
11444 break;
11445 if (*expr_p != save_expr)
11446 continue;
11448 else if (ret != GS_UNHANDLED)
11449 break;
11451 /* Make sure that all the cases set 'ret' appropriately. */
11452 ret = GS_UNHANDLED;
11453 switch (TREE_CODE (*expr_p))
11455 /* First deal with the special cases. */
11457 case POSTINCREMENT_EXPR:
11458 case POSTDECREMENT_EXPR:
11459 case PREINCREMENT_EXPR:
11460 case PREDECREMENT_EXPR:
11461 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11462 fallback != fb_none,
11463 TREE_TYPE (*expr_p));
11464 break;
11466 case VIEW_CONVERT_EXPR:
11467 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11468 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11470 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11471 post_p, is_gimple_val, fb_rvalue);
11472 recalculate_side_effects (*expr_p);
11473 break;
11475 /* Fallthru. */
11477 case ARRAY_REF:
11478 case ARRAY_RANGE_REF:
11479 case REALPART_EXPR:
11480 case IMAGPART_EXPR:
11481 case COMPONENT_REF:
11482 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11483 fallback ? fallback : fb_rvalue);
11484 break;
11486 case COND_EXPR:
11487 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11489 /* C99 code may assign to an array in a structure value of a
11490 conditional expression, and this has undefined behavior
11491 only on execution, so create a temporary if an lvalue is
11492 required. */
11493 if (fallback == fb_lvalue)
11495 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11496 mark_addressable (*expr_p);
11497 ret = GS_OK;
11499 break;
11501 case CALL_EXPR:
11502 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11504 /* C99 code may assign to an array in a structure returned
11505 from a function, and this has undefined behavior only on
11506 execution, so create a temporary if an lvalue is
11507 required. */
11508 if (fallback == fb_lvalue)
11510 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11511 mark_addressable (*expr_p);
11512 ret = GS_OK;
11514 break;
11516 case TREE_LIST:
11517 gcc_unreachable ();
11519 case COMPOUND_EXPR:
11520 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11521 break;
11523 case COMPOUND_LITERAL_EXPR:
11524 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11525 gimple_test_f, fallback);
11526 break;
11528 case MODIFY_EXPR:
11529 case INIT_EXPR:
11530 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11531 fallback != fb_none);
11532 break;
11534 case TRUTH_ANDIF_EXPR:
11535 case TRUTH_ORIF_EXPR:
11537 /* Preserve the original type of the expression and the
11538 source location of the outer expression. */
11539 tree org_type = TREE_TYPE (*expr_p);
11540 *expr_p = gimple_boolify (*expr_p);
11541 *expr_p = build3_loc (input_location, COND_EXPR,
11542 org_type, *expr_p,
11543 fold_convert_loc
11544 (input_location,
11545 org_type, boolean_true_node),
11546 fold_convert_loc
11547 (input_location,
11548 org_type, boolean_false_node));
11549 ret = GS_OK;
11550 break;
11553 case TRUTH_NOT_EXPR:
11555 tree type = TREE_TYPE (*expr_p);
11556 /* The parsers are careful to generate TRUTH_NOT_EXPR
11557 only with operands that are always zero or one.
11558 We do not fold here but handle the only interesting case
11559 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11560 *expr_p = gimple_boolify (*expr_p);
11561 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11562 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11563 TREE_TYPE (*expr_p),
11564 TREE_OPERAND (*expr_p, 0));
11565 else
11566 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11567 TREE_TYPE (*expr_p),
11568 TREE_OPERAND (*expr_p, 0),
11569 build_int_cst (TREE_TYPE (*expr_p), 1));
11570 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11571 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11572 ret = GS_OK;
11573 break;
11576 case ADDR_EXPR:
11577 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11578 break;
11580 case ANNOTATE_EXPR:
11582 tree cond = TREE_OPERAND (*expr_p, 0);
11583 tree kind = TREE_OPERAND (*expr_p, 1);
11584 tree data = TREE_OPERAND (*expr_p, 2);
11585 tree type = TREE_TYPE (cond);
11586 if (!INTEGRAL_TYPE_P (type))
11588 *expr_p = cond;
11589 ret = GS_OK;
11590 break;
11592 tree tmp = create_tmp_var (type);
11593 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11594 gcall *call
11595 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11596 gimple_call_set_lhs (call, tmp);
11597 gimplify_seq_add_stmt (pre_p, call);
11598 *expr_p = tmp;
11599 ret = GS_ALL_DONE;
11600 break;
11603 case VA_ARG_EXPR:
11604 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11605 break;
11607 CASE_CONVERT:
11608 if (IS_EMPTY_STMT (*expr_p))
11610 ret = GS_ALL_DONE;
11611 break;
11614 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11615 || fallback == fb_none)
11617 /* Just strip a conversion to void (or in void context) and
11618 try again. */
11619 *expr_p = TREE_OPERAND (*expr_p, 0);
11620 ret = GS_OK;
11621 break;
11624 ret = gimplify_conversion (expr_p);
11625 if (ret == GS_ERROR)
11626 break;
11627 if (*expr_p != save_expr)
11628 break;
11629 /* FALLTHRU */
11631 case FIX_TRUNC_EXPR:
11632 /* unary_expr: ... | '(' cast ')' val | ... */
11633 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11634 is_gimple_val, fb_rvalue);
11635 recalculate_side_effects (*expr_p);
11636 break;
11638 case INDIRECT_REF:
11640 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11641 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11642 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11644 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11645 if (*expr_p != save_expr)
11647 ret = GS_OK;
11648 break;
11651 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11652 is_gimple_reg, fb_rvalue);
11653 if (ret == GS_ERROR)
11654 break;
11656 recalculate_side_effects (*expr_p);
11657 *expr_p = fold_build2_loc (input_location, MEM_REF,
11658 TREE_TYPE (*expr_p),
11659 TREE_OPERAND (*expr_p, 0),
11660 build_int_cst (saved_ptr_type, 0));
11661 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11662 TREE_THIS_NOTRAP (*expr_p) = notrap;
11663 ret = GS_OK;
11664 break;
11667 /* We arrive here through the various re-gimplifcation paths. */
11668 case MEM_REF:
11669 /* First try re-folding the whole thing. */
11670 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11671 TREE_OPERAND (*expr_p, 0),
11672 TREE_OPERAND (*expr_p, 1));
11673 if (tmp)
11675 REF_REVERSE_STORAGE_ORDER (tmp)
11676 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11677 *expr_p = tmp;
11678 recalculate_side_effects (*expr_p);
11679 ret = GS_OK;
11680 break;
11682 /* Avoid re-gimplifying the address operand if it is already
11683 in suitable form. Re-gimplifying would mark the address
11684 operand addressable. Always gimplify when not in SSA form
11685 as we still may have to gimplify decls with value-exprs. */
11686 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11687 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11689 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11690 is_gimple_mem_ref_addr, fb_rvalue);
11691 if (ret == GS_ERROR)
11692 break;
11694 recalculate_side_effects (*expr_p);
11695 ret = GS_ALL_DONE;
11696 break;
11698 /* Constants need not be gimplified. */
11699 case INTEGER_CST:
11700 case REAL_CST:
11701 case FIXED_CST:
11702 case STRING_CST:
11703 case COMPLEX_CST:
11704 case VECTOR_CST:
11705 /* Drop the overflow flag on constants, we do not want
11706 that in the GIMPLE IL. */
11707 if (TREE_OVERFLOW_P (*expr_p))
11708 *expr_p = drop_tree_overflow (*expr_p);
11709 ret = GS_ALL_DONE;
11710 break;
11712 case CONST_DECL:
11713 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11714 CONST_DECL node. Otherwise the decl is replaceable by its
11715 value. */
11716 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11717 if (fallback & fb_lvalue)
11718 ret = GS_ALL_DONE;
11719 else
11721 *expr_p = DECL_INITIAL (*expr_p);
11722 ret = GS_OK;
11724 break;
11726 case DECL_EXPR:
11727 ret = gimplify_decl_expr (expr_p, pre_p);
11728 break;
11730 case BIND_EXPR:
11731 ret = gimplify_bind_expr (expr_p, pre_p);
11732 break;
11734 case LOOP_EXPR:
11735 ret = gimplify_loop_expr (expr_p, pre_p);
11736 break;
11738 case SWITCH_EXPR:
11739 ret = gimplify_switch_expr (expr_p, pre_p);
11740 break;
11742 case EXIT_EXPR:
11743 ret = gimplify_exit_expr (expr_p);
11744 break;
11746 case GOTO_EXPR:
11747 /* If the target is not LABEL, then it is a computed jump
11748 and the target needs to be gimplified. */
11749 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11751 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11752 NULL, is_gimple_val, fb_rvalue);
11753 if (ret == GS_ERROR)
11754 break;
11756 gimplify_seq_add_stmt (pre_p,
11757 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11758 ret = GS_ALL_DONE;
11759 break;
11761 case PREDICT_EXPR:
11762 gimplify_seq_add_stmt (pre_p,
11763 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11764 PREDICT_EXPR_OUTCOME (*expr_p)));
11765 ret = GS_ALL_DONE;
11766 break;
11768 case LABEL_EXPR:
11769 ret = gimplify_label_expr (expr_p, pre_p);
11770 label = LABEL_EXPR_LABEL (*expr_p);
11771 gcc_assert (decl_function_context (label) == current_function_decl);
11773 /* If the label is used in a goto statement, or address of the label
11774 is taken, we need to unpoison all variables that were seen so far.
11775 Doing so would prevent us from reporting a false positives. */
11776 if (asan_poisoned_variables
11777 && asan_used_labels != NULL
11778 && asan_used_labels->contains (label))
11779 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11780 break;
11782 case CASE_LABEL_EXPR:
11783 ret = gimplify_case_label_expr (expr_p, pre_p);
11785 if (gimplify_ctxp->live_switch_vars)
11786 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11787 pre_p);
11788 break;
11790 case RETURN_EXPR:
11791 ret = gimplify_return_expr (*expr_p, pre_p);
11792 break;
11794 case CONSTRUCTOR:
11795 /* Don't reduce this in place; let gimplify_init_constructor work its
11796 magic. Buf if we're just elaborating this for side effects, just
11797 gimplify any element that has side-effects. */
11798 if (fallback == fb_none)
11800 unsigned HOST_WIDE_INT ix;
11801 tree val;
11802 tree temp = NULL_TREE;
11803 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11804 if (TREE_SIDE_EFFECTS (val))
11805 append_to_statement_list (val, &temp);
11807 *expr_p = temp;
11808 ret = temp ? GS_OK : GS_ALL_DONE;
11810 /* C99 code may assign to an array in a constructed
11811 structure or union, and this has undefined behavior only
11812 on execution, so create a temporary if an lvalue is
11813 required. */
11814 else if (fallback == fb_lvalue)
11816 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11817 mark_addressable (*expr_p);
11818 ret = GS_OK;
11820 else
11821 ret = GS_ALL_DONE;
11822 break;
11824 /* The following are special cases that are not handled by the
11825 original GIMPLE grammar. */
11827 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11828 eliminated. */
11829 case SAVE_EXPR:
11830 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11831 break;
11833 case BIT_FIELD_REF:
11834 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11835 post_p, is_gimple_lvalue, fb_either);
11836 recalculate_side_effects (*expr_p);
11837 break;
11839 case TARGET_MEM_REF:
11841 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11843 if (TMR_BASE (*expr_p))
11844 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11845 post_p, is_gimple_mem_ref_addr, fb_either);
11846 if (TMR_INDEX (*expr_p))
11847 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11848 post_p, is_gimple_val, fb_rvalue);
11849 if (TMR_INDEX2 (*expr_p))
11850 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11851 post_p, is_gimple_val, fb_rvalue);
11852 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11853 ret = MIN (r0, r1);
11855 break;
11857 case NON_LVALUE_EXPR:
11858 /* This should have been stripped above. */
11859 gcc_unreachable ();
11861 case ASM_EXPR:
11862 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11863 break;
11865 case TRY_FINALLY_EXPR:
11866 case TRY_CATCH_EXPR:
11868 gimple_seq eval, cleanup;
11869 gtry *try_;
11871 /* Calls to destructors are generated automatically in FINALLY/CATCH
11872 block. They should have location as UNKNOWN_LOCATION. However,
11873 gimplify_call_expr will reset these call stmts to input_location
11874 if it finds stmt's location is unknown. To prevent resetting for
11875 destructors, we set the input_location to unknown.
11876 Note that this only affects the destructor calls in FINALLY/CATCH
11877 block, and will automatically reset to its original value by the
11878 end of gimplify_expr. */
11879 input_location = UNKNOWN_LOCATION;
11880 eval = cleanup = NULL;
11881 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11882 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11883 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11884 if (gimple_seq_empty_p (cleanup))
11886 gimple_seq_add_seq (pre_p, eval);
11887 ret = GS_ALL_DONE;
11888 break;
11890 try_ = gimple_build_try (eval, cleanup,
11891 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11892 ? GIMPLE_TRY_FINALLY
11893 : GIMPLE_TRY_CATCH);
11894 if (EXPR_HAS_LOCATION (save_expr))
11895 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11896 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11897 gimple_set_location (try_, saved_location);
11898 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11899 gimple_try_set_catch_is_cleanup (try_,
11900 TRY_CATCH_IS_CLEANUP (*expr_p));
11901 gimplify_seq_add_stmt (pre_p, try_);
11902 ret = GS_ALL_DONE;
11903 break;
11906 case CLEANUP_POINT_EXPR:
11907 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11908 break;
11910 case TARGET_EXPR:
11911 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11912 break;
11914 case CATCH_EXPR:
11916 gimple *c;
11917 gimple_seq handler = NULL;
11918 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11919 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11920 gimplify_seq_add_stmt (pre_p, c);
11921 ret = GS_ALL_DONE;
11922 break;
11925 case EH_FILTER_EXPR:
11927 gimple *ehf;
11928 gimple_seq failure = NULL;
11930 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11931 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11932 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11933 gimplify_seq_add_stmt (pre_p, ehf);
11934 ret = GS_ALL_DONE;
11935 break;
11938 case OBJ_TYPE_REF:
11940 enum gimplify_status r0, r1;
11941 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11942 post_p, is_gimple_val, fb_rvalue);
11943 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11944 post_p, is_gimple_val, fb_rvalue);
11945 TREE_SIDE_EFFECTS (*expr_p) = 0;
11946 ret = MIN (r0, r1);
11948 break;
11950 case LABEL_DECL:
11951 /* We get here when taking the address of a label. We mark
11952 the label as "forced"; meaning it can never be removed and
11953 it is a potential target for any computed goto. */
11954 FORCED_LABEL (*expr_p) = 1;
11955 ret = GS_ALL_DONE;
11956 break;
11958 case STATEMENT_LIST:
11959 ret = gimplify_statement_list (expr_p, pre_p);
11960 break;
11962 case WITH_SIZE_EXPR:
11964 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11965 post_p == &internal_post ? NULL : post_p,
11966 gimple_test_f, fallback);
11967 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11968 is_gimple_val, fb_rvalue);
11969 ret = GS_ALL_DONE;
11971 break;
11973 case VAR_DECL:
11974 case PARM_DECL:
11975 ret = gimplify_var_or_parm_decl (expr_p);
11976 break;
11978 case RESULT_DECL:
11979 /* When within an OMP context, notice uses of variables. */
11980 if (gimplify_omp_ctxp)
11981 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11982 ret = GS_ALL_DONE;
11983 break;
11985 case DEBUG_EXPR_DECL:
11986 gcc_unreachable ();
11988 case DEBUG_BEGIN_STMT:
11989 gimplify_seq_add_stmt (pre_p,
11990 gimple_build_debug_begin_stmt
11991 (TREE_BLOCK (*expr_p),
11992 EXPR_LOCATION (*expr_p)));
11993 ret = GS_ALL_DONE;
11994 *expr_p = NULL;
11995 break;
11997 case SSA_NAME:
11998 /* Allow callbacks into the gimplifier during optimization. */
11999 ret = GS_ALL_DONE;
12000 break;
12002 case OMP_PARALLEL:
12003 gimplify_omp_parallel (expr_p, pre_p);
12004 ret = GS_ALL_DONE;
12005 break;
12007 case OMP_TASK:
12008 gimplify_omp_task (expr_p, pre_p);
12009 ret = GS_ALL_DONE;
12010 break;
12012 case OMP_FOR:
12013 case OMP_SIMD:
12014 case OMP_DISTRIBUTE:
12015 case OMP_TASKLOOP:
12016 case OACC_LOOP:
12017 ret = gimplify_omp_for (expr_p, pre_p);
12018 break;
12020 case OACC_CACHE:
12021 gimplify_oacc_cache (expr_p, pre_p);
12022 ret = GS_ALL_DONE;
12023 break;
12025 case OACC_DECLARE:
12026 gimplify_oacc_declare (expr_p, pre_p);
12027 ret = GS_ALL_DONE;
12028 break;
12030 case OACC_HOST_DATA:
12031 case OACC_DATA:
12032 case OACC_KERNELS:
12033 case OACC_PARALLEL:
12034 case OMP_SECTIONS:
12035 case OMP_SINGLE:
12036 case OMP_TARGET:
12037 case OMP_TARGET_DATA:
12038 case OMP_TEAMS:
12039 gimplify_omp_workshare (expr_p, pre_p);
12040 ret = GS_ALL_DONE;
12041 break;
12043 case OACC_ENTER_DATA:
12044 case OACC_EXIT_DATA:
12045 case OACC_UPDATE:
12046 case OMP_TARGET_UPDATE:
12047 case OMP_TARGET_ENTER_DATA:
12048 case OMP_TARGET_EXIT_DATA:
12049 gimplify_omp_target_update (expr_p, pre_p);
12050 ret = GS_ALL_DONE;
12051 break;
12053 case OMP_SECTION:
12054 case OMP_MASTER:
12055 case OMP_TASKGROUP:
12056 case OMP_ORDERED:
12057 case OMP_CRITICAL:
12059 gimple_seq body = NULL;
12060 gimple *g;
12062 gimplify_and_add (OMP_BODY (*expr_p), &body);
12063 switch (TREE_CODE (*expr_p))
12065 case OMP_SECTION:
12066 g = gimple_build_omp_section (body);
12067 break;
12068 case OMP_MASTER:
12069 g = gimple_build_omp_master (body);
12070 break;
12071 case OMP_TASKGROUP:
12073 gimple_seq cleanup = NULL;
12074 tree fn
12075 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12076 g = gimple_build_call (fn, 0);
12077 gimple_seq_add_stmt (&cleanup, g);
12078 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12079 body = NULL;
12080 gimple_seq_add_stmt (&body, g);
12081 g = gimple_build_omp_taskgroup (body);
12083 break;
12084 case OMP_ORDERED:
12085 g = gimplify_omp_ordered (*expr_p, body);
12086 break;
12087 case OMP_CRITICAL:
12088 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12089 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12090 gimplify_adjust_omp_clauses (pre_p, body,
12091 &OMP_CRITICAL_CLAUSES (*expr_p),
12092 OMP_CRITICAL);
12093 g = gimple_build_omp_critical (body,
12094 OMP_CRITICAL_NAME (*expr_p),
12095 OMP_CRITICAL_CLAUSES (*expr_p));
12096 break;
12097 default:
12098 gcc_unreachable ();
12100 gimplify_seq_add_stmt (pre_p, g);
12101 ret = GS_ALL_DONE;
12102 break;
12105 case OMP_ATOMIC:
12106 case OMP_ATOMIC_READ:
12107 case OMP_ATOMIC_CAPTURE_OLD:
12108 case OMP_ATOMIC_CAPTURE_NEW:
12109 ret = gimplify_omp_atomic (expr_p, pre_p);
12110 break;
12112 case TRANSACTION_EXPR:
12113 ret = gimplify_transaction (expr_p, pre_p);
12114 break;
12116 case TRUTH_AND_EXPR:
12117 case TRUTH_OR_EXPR:
12118 case TRUTH_XOR_EXPR:
12120 tree orig_type = TREE_TYPE (*expr_p);
12121 tree new_type, xop0, xop1;
12122 *expr_p = gimple_boolify (*expr_p);
12123 new_type = TREE_TYPE (*expr_p);
12124 if (!useless_type_conversion_p (orig_type, new_type))
12126 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12127 ret = GS_OK;
12128 break;
12131 /* Boolified binary truth expressions are semantically equivalent
12132 to bitwise binary expressions. Canonicalize them to the
12133 bitwise variant. */
12134 switch (TREE_CODE (*expr_p))
12136 case TRUTH_AND_EXPR:
12137 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12138 break;
12139 case TRUTH_OR_EXPR:
12140 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12141 break;
12142 case TRUTH_XOR_EXPR:
12143 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12144 break;
12145 default:
12146 break;
12148 /* Now make sure that operands have compatible type to
12149 expression's new_type. */
12150 xop0 = TREE_OPERAND (*expr_p, 0);
12151 xop1 = TREE_OPERAND (*expr_p, 1);
12152 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12153 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12154 new_type,
12155 xop0);
12156 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12157 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12158 new_type,
12159 xop1);
12160 /* Continue classified as tcc_binary. */
12161 goto expr_2;
12164 case VEC_COND_EXPR:
12166 enum gimplify_status r0, r1, r2;
12168 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12169 post_p, is_gimple_condexpr, fb_rvalue);
12170 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12171 post_p, is_gimple_val, fb_rvalue);
12172 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12173 post_p, is_gimple_val, fb_rvalue);
12175 ret = MIN (MIN (r0, r1), r2);
12176 recalculate_side_effects (*expr_p);
12178 break;
12180 case FMA_EXPR:
12181 case VEC_PERM_EXPR:
12182 /* Classified as tcc_expression. */
12183 goto expr_3;
12185 case BIT_INSERT_EXPR:
12186 /* Argument 3 is a constant. */
12187 goto expr_2;
12189 case POINTER_PLUS_EXPR:
12191 enum gimplify_status r0, r1;
12192 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12193 post_p, is_gimple_val, fb_rvalue);
12194 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12195 post_p, is_gimple_val, fb_rvalue);
12196 recalculate_side_effects (*expr_p);
12197 ret = MIN (r0, r1);
12198 break;
12201 default:
12202 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12204 case tcc_comparison:
12205 /* Handle comparison of objects of non scalar mode aggregates
12206 with a call to memcmp. It would be nice to only have to do
12207 this for variable-sized objects, but then we'd have to allow
12208 the same nest of reference nodes we allow for MODIFY_EXPR and
12209 that's too complex.
12211 Compare scalar mode aggregates as scalar mode values. Using
12212 memcmp for them would be very inefficient at best, and is
12213 plain wrong if bitfields are involved. */
12215 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12217 /* Vector comparisons need no boolification. */
12218 if (TREE_CODE (type) == VECTOR_TYPE)
12219 goto expr_2;
12220 else if (!AGGREGATE_TYPE_P (type))
12222 tree org_type = TREE_TYPE (*expr_p);
12223 *expr_p = gimple_boolify (*expr_p);
12224 if (!useless_type_conversion_p (org_type,
12225 TREE_TYPE (*expr_p)))
12227 *expr_p = fold_convert_loc (input_location,
12228 org_type, *expr_p);
12229 ret = GS_OK;
12231 else
12232 goto expr_2;
12234 else if (TYPE_MODE (type) != BLKmode)
12235 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12236 else
12237 ret = gimplify_variable_sized_compare (expr_p);
12239 break;
12242 /* If *EXPR_P does not need to be special-cased, handle it
12243 according to its class. */
12244 case tcc_unary:
12245 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12246 post_p, is_gimple_val, fb_rvalue);
12247 break;
12249 case tcc_binary:
12250 expr_2:
12252 enum gimplify_status r0, r1;
12254 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12255 post_p, is_gimple_val, fb_rvalue);
12256 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12257 post_p, is_gimple_val, fb_rvalue);
12259 ret = MIN (r0, r1);
12260 break;
12263 expr_3:
12265 enum gimplify_status r0, r1, r2;
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 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12272 post_p, is_gimple_val, fb_rvalue);
12274 ret = MIN (MIN (r0, r1), r2);
12275 break;
12278 case tcc_declaration:
12279 case tcc_constant:
12280 ret = GS_ALL_DONE;
12281 goto dont_recalculate;
12283 default:
12284 gcc_unreachable ();
12287 recalculate_side_effects (*expr_p);
12289 dont_recalculate:
12290 break;
12293 gcc_assert (*expr_p || ret != GS_OK);
12295 while (ret == GS_OK);
12297 /* If we encountered an error_mark somewhere nested inside, either
12298 stub out the statement or propagate the error back out. */
12299 if (ret == GS_ERROR)
12301 if (is_statement)
12302 *expr_p = NULL;
12303 goto out;
12306 /* This was only valid as a return value from the langhook, which
12307 we handled. Make sure it doesn't escape from any other context. */
12308 gcc_assert (ret != GS_UNHANDLED);
12310 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12312 /* We aren't looking for a value, and we don't have a valid
12313 statement. If it doesn't have side-effects, throw it away.
12314 We can also get here with code such as "*&&L;", where L is
12315 a LABEL_DECL that is marked as FORCED_LABEL. */
12316 if (TREE_CODE (*expr_p) == LABEL_DECL
12317 || !TREE_SIDE_EFFECTS (*expr_p))
12318 *expr_p = NULL;
12319 else if (!TREE_THIS_VOLATILE (*expr_p))
12321 /* This is probably a _REF that contains something nested that
12322 has side effects. Recurse through the operands to find it. */
12323 enum tree_code code = TREE_CODE (*expr_p);
12325 switch (code)
12327 case COMPONENT_REF:
12328 case REALPART_EXPR:
12329 case IMAGPART_EXPR:
12330 case VIEW_CONVERT_EXPR:
12331 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12332 gimple_test_f, fallback);
12333 break;
12335 case ARRAY_REF:
12336 case ARRAY_RANGE_REF:
12337 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12338 gimple_test_f, fallback);
12339 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12340 gimple_test_f, fallback);
12341 break;
12343 default:
12344 /* Anything else with side-effects must be converted to
12345 a valid statement before we get here. */
12346 gcc_unreachable ();
12349 *expr_p = NULL;
12351 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12352 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12354 /* Historically, the compiler has treated a bare reference
12355 to a non-BLKmode volatile lvalue as forcing a load. */
12356 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12358 /* Normally, we do not want to create a temporary for a
12359 TREE_ADDRESSABLE type because such a type should not be
12360 copied by bitwise-assignment. However, we make an
12361 exception here, as all we are doing here is ensuring that
12362 we read the bytes that make up the type. We use
12363 create_tmp_var_raw because create_tmp_var will abort when
12364 given a TREE_ADDRESSABLE type. */
12365 tree tmp = create_tmp_var_raw (type, "vol");
12366 gimple_add_tmp_var (tmp);
12367 gimplify_assign (tmp, *expr_p, pre_p);
12368 *expr_p = NULL;
12370 else
12371 /* We can't do anything useful with a volatile reference to
12372 an incomplete type, so just throw it away. Likewise for
12373 a BLKmode type, since any implicit inner load should
12374 already have been turned into an explicit one by the
12375 gimplification process. */
12376 *expr_p = NULL;
12379 /* If we are gimplifying at the statement level, we're done. Tack
12380 everything together and return. */
12381 if (fallback == fb_none || is_statement)
12383 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12384 it out for GC to reclaim it. */
12385 *expr_p = NULL_TREE;
12387 if (!gimple_seq_empty_p (internal_pre)
12388 || !gimple_seq_empty_p (internal_post))
12390 gimplify_seq_add_seq (&internal_pre, internal_post);
12391 gimplify_seq_add_seq (pre_p, internal_pre);
12394 /* The result of gimplifying *EXPR_P is going to be the last few
12395 statements in *PRE_P and *POST_P. Add location information
12396 to all the statements that were added by the gimplification
12397 helpers. */
12398 if (!gimple_seq_empty_p (*pre_p))
12399 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12401 if (!gimple_seq_empty_p (*post_p))
12402 annotate_all_with_location_after (*post_p, post_last_gsi,
12403 input_location);
12405 goto out;
12408 #ifdef ENABLE_GIMPLE_CHECKING
12409 if (*expr_p)
12411 enum tree_code code = TREE_CODE (*expr_p);
12412 /* These expressions should already be in gimple IR form. */
12413 gcc_assert (code != MODIFY_EXPR
12414 && code != ASM_EXPR
12415 && code != BIND_EXPR
12416 && code != CATCH_EXPR
12417 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12418 && code != EH_FILTER_EXPR
12419 && code != GOTO_EXPR
12420 && code != LABEL_EXPR
12421 && code != LOOP_EXPR
12422 && code != SWITCH_EXPR
12423 && code != TRY_FINALLY_EXPR
12424 && code != OACC_PARALLEL
12425 && code != OACC_KERNELS
12426 && code != OACC_DATA
12427 && code != OACC_HOST_DATA
12428 && code != OACC_DECLARE
12429 && code != OACC_UPDATE
12430 && code != OACC_ENTER_DATA
12431 && code != OACC_EXIT_DATA
12432 && code != OACC_CACHE
12433 && code != OMP_CRITICAL
12434 && code != OMP_FOR
12435 && code != OACC_LOOP
12436 && code != OMP_MASTER
12437 && code != OMP_TASKGROUP
12438 && code != OMP_ORDERED
12439 && code != OMP_PARALLEL
12440 && code != OMP_SECTIONS
12441 && code != OMP_SECTION
12442 && code != OMP_SINGLE);
12444 #endif
12446 /* Otherwise we're gimplifying a subexpression, so the resulting
12447 value is interesting. If it's a valid operand that matches
12448 GIMPLE_TEST_F, we're done. Unless we are handling some
12449 post-effects internally; if that's the case, we need to copy into
12450 a temporary before adding the post-effects to POST_P. */
12451 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12452 goto out;
12454 /* Otherwise, we need to create a new temporary for the gimplified
12455 expression. */
12457 /* We can't return an lvalue if we have an internal postqueue. The
12458 object the lvalue refers to would (probably) be modified by the
12459 postqueue; we need to copy the value out first, which means an
12460 rvalue. */
12461 if ((fallback & fb_lvalue)
12462 && gimple_seq_empty_p (internal_post)
12463 && is_gimple_addressable (*expr_p))
12465 /* An lvalue will do. Take the address of the expression, store it
12466 in a temporary, and replace the expression with an INDIRECT_REF of
12467 that temporary. */
12468 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12469 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12470 *expr_p = build_simple_mem_ref (tmp);
12472 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12474 /* An rvalue will do. Assign the gimplified expression into a
12475 new temporary TMP and replace the original expression with
12476 TMP. First, make sure that the expression has a type so that
12477 it can be assigned into a temporary. */
12478 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12479 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12481 else
12483 #ifdef ENABLE_GIMPLE_CHECKING
12484 if (!(fallback & fb_mayfail))
12486 fprintf (stderr, "gimplification failed:\n");
12487 print_generic_expr (stderr, *expr_p);
12488 debug_tree (*expr_p);
12489 internal_error ("gimplification failed");
12491 #endif
12492 gcc_assert (fallback & fb_mayfail);
12494 /* If this is an asm statement, and the user asked for the
12495 impossible, don't die. Fail and let gimplify_asm_expr
12496 issue an error. */
12497 ret = GS_ERROR;
12498 goto out;
12501 /* Make sure the temporary matches our predicate. */
12502 gcc_assert ((*gimple_test_f) (*expr_p));
12504 if (!gimple_seq_empty_p (internal_post))
12506 annotate_all_with_location (internal_post, input_location);
12507 gimplify_seq_add_seq (pre_p, internal_post);
12510 out:
12511 input_location = saved_location;
12512 return ret;
12515 /* Like gimplify_expr but make sure the gimplified result is not itself
12516 a SSA name (but a decl if it were). Temporaries required by
12517 evaluating *EXPR_P may be still SSA names. */
12519 static enum gimplify_status
12520 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12521 bool (*gimple_test_f) (tree), fallback_t fallback,
12522 bool allow_ssa)
12524 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12525 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12526 gimple_test_f, fallback);
12527 if (! allow_ssa
12528 && TREE_CODE (*expr_p) == SSA_NAME)
12530 tree name = *expr_p;
12531 if (was_ssa_name_p)
12532 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12533 else
12535 /* Avoid the extra copy if possible. */
12536 *expr_p = create_tmp_reg (TREE_TYPE (name));
12537 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12538 release_ssa_name (name);
12541 return ret;
12544 /* Look through TYPE for variable-sized objects and gimplify each such
12545 size that we find. Add to LIST_P any statements generated. */
12547 void
12548 gimplify_type_sizes (tree type, gimple_seq *list_p)
12550 tree field, t;
12552 if (type == NULL || type == error_mark_node)
12553 return;
12555 /* We first do the main variant, then copy into any other variants. */
12556 type = TYPE_MAIN_VARIANT (type);
12558 /* Avoid infinite recursion. */
12559 if (TYPE_SIZES_GIMPLIFIED (type))
12560 return;
12562 TYPE_SIZES_GIMPLIFIED (type) = 1;
12564 switch (TREE_CODE (type))
12566 case INTEGER_TYPE:
12567 case ENUMERAL_TYPE:
12568 case BOOLEAN_TYPE:
12569 case REAL_TYPE:
12570 case FIXED_POINT_TYPE:
12571 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12572 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12574 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12576 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12577 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12579 break;
12581 case ARRAY_TYPE:
12582 /* These types may not have declarations, so handle them here. */
12583 gimplify_type_sizes (TREE_TYPE (type), list_p);
12584 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12585 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12586 with assigned stack slots, for -O1+ -g they should be tracked
12587 by VTA. */
12588 if (!(TYPE_NAME (type)
12589 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12590 && DECL_IGNORED_P (TYPE_NAME (type)))
12591 && TYPE_DOMAIN (type)
12592 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12594 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12595 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12596 DECL_IGNORED_P (t) = 0;
12597 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12598 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12599 DECL_IGNORED_P (t) = 0;
12601 break;
12603 case RECORD_TYPE:
12604 case UNION_TYPE:
12605 case QUAL_UNION_TYPE:
12606 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12607 if (TREE_CODE (field) == FIELD_DECL)
12609 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12610 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12611 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12612 gimplify_type_sizes (TREE_TYPE (field), list_p);
12614 break;
12616 case POINTER_TYPE:
12617 case REFERENCE_TYPE:
12618 /* We used to recurse on the pointed-to type here, which turned out to
12619 be incorrect because its definition might refer to variables not
12620 yet initialized at this point if a forward declaration is involved.
12622 It was actually useful for anonymous pointed-to types to ensure
12623 that the sizes evaluation dominates every possible later use of the
12624 values. Restricting to such types here would be safe since there
12625 is no possible forward declaration around, but would introduce an
12626 undesirable middle-end semantic to anonymity. We then defer to
12627 front-ends the responsibility of ensuring that the sizes are
12628 evaluated both early and late enough, e.g. by attaching artificial
12629 type declarations to the tree. */
12630 break;
12632 default:
12633 break;
12636 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12637 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12639 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12641 TYPE_SIZE (t) = TYPE_SIZE (type);
12642 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12643 TYPE_SIZES_GIMPLIFIED (t) = 1;
12647 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12648 a size or position, has had all of its SAVE_EXPRs evaluated.
12649 We add any required statements to *STMT_P. */
12651 void
12652 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12654 tree expr = *expr_p;
12656 /* We don't do anything if the value isn't there, is constant, or contains
12657 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12658 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12659 will want to replace it with a new variable, but that will cause problems
12660 if this type is from outside the function. It's OK to have that here. */
12661 if (expr == NULL_TREE
12662 || is_gimple_constant (expr)
12663 || TREE_CODE (expr) == VAR_DECL
12664 || CONTAINS_PLACEHOLDER_P (expr))
12665 return;
12667 *expr_p = unshare_expr (expr);
12669 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12670 if the def vanishes. */
12671 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12673 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12674 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12675 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12676 if (is_gimple_constant (*expr_p))
12677 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12680 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12681 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12682 is true, also gimplify the parameters. */
12684 gbind *
12685 gimplify_body (tree fndecl, bool do_parms)
12687 location_t saved_location = input_location;
12688 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12689 gimple *outer_stmt;
12690 gbind *outer_bind;
12691 struct cgraph_node *cgn;
12693 timevar_push (TV_TREE_GIMPLIFY);
12695 init_tree_ssa (cfun);
12697 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12698 gimplification. */
12699 default_rtl_profile ();
12701 gcc_assert (gimplify_ctxp == NULL);
12702 push_gimplify_context (true);
12704 if (flag_openacc || flag_openmp)
12706 gcc_assert (gimplify_omp_ctxp == NULL);
12707 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12708 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12711 /* Unshare most shared trees in the body and in that of any nested functions.
12712 It would seem we don't have to do this for nested functions because
12713 they are supposed to be output and then the outer function gimplified
12714 first, but the g++ front end doesn't always do it that way. */
12715 unshare_body (fndecl);
12716 unvisit_body (fndecl);
12718 cgn = cgraph_node::get (fndecl);
12719 if (cgn && cgn->origin)
12720 nonlocal_vlas = new hash_set<tree>;
12722 /* Make sure input_location isn't set to something weird. */
12723 input_location = DECL_SOURCE_LOCATION (fndecl);
12725 /* Resolve callee-copies. This has to be done before processing
12726 the body so that DECL_VALUE_EXPR gets processed correctly. */
12727 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12729 /* Gimplify the function's body. */
12730 seq = NULL;
12731 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12732 outer_stmt = gimple_seq_first_stmt (seq);
12733 if (!outer_stmt)
12735 outer_stmt = gimple_build_nop ();
12736 gimplify_seq_add_stmt (&seq, outer_stmt);
12739 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12740 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12741 if (gimple_code (outer_stmt) == GIMPLE_BIND
12742 && gimple_seq_first (seq) == gimple_seq_last (seq))
12743 outer_bind = as_a <gbind *> (outer_stmt);
12744 else
12745 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12747 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12749 /* If we had callee-copies statements, insert them at the beginning
12750 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12751 if (!gimple_seq_empty_p (parm_stmts))
12753 tree parm;
12755 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12756 if (parm_cleanup)
12758 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12759 GIMPLE_TRY_FINALLY);
12760 parm_stmts = NULL;
12761 gimple_seq_add_stmt (&parm_stmts, g);
12763 gimple_bind_set_body (outer_bind, parm_stmts);
12765 for (parm = DECL_ARGUMENTS (current_function_decl);
12766 parm; parm = DECL_CHAIN (parm))
12767 if (DECL_HAS_VALUE_EXPR_P (parm))
12769 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12770 DECL_IGNORED_P (parm) = 0;
12774 if (nonlocal_vlas)
12776 if (nonlocal_vla_vars)
12778 /* tree-nested.c may later on call declare_vars (..., true);
12779 which relies on BLOCK_VARS chain to be the tail of the
12780 gimple_bind_vars chain. Ensure we don't violate that
12781 assumption. */
12782 if (gimple_bind_block (outer_bind)
12783 == DECL_INITIAL (current_function_decl))
12784 declare_vars (nonlocal_vla_vars, outer_bind, true);
12785 else
12786 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12787 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12788 nonlocal_vla_vars);
12789 nonlocal_vla_vars = NULL_TREE;
12791 delete nonlocal_vlas;
12792 nonlocal_vlas = NULL;
12795 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12796 && gimplify_omp_ctxp)
12798 delete_omp_context (gimplify_omp_ctxp);
12799 gimplify_omp_ctxp = NULL;
12802 pop_gimplify_context (outer_bind);
12803 gcc_assert (gimplify_ctxp == NULL);
12805 if (flag_checking && !seen_error ())
12806 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12808 timevar_pop (TV_TREE_GIMPLIFY);
12809 input_location = saved_location;
12811 return outer_bind;
12814 typedef char *char_p; /* For DEF_VEC_P. */
12816 /* Return whether we should exclude FNDECL from instrumentation. */
12818 static bool
12819 flag_instrument_functions_exclude_p (tree fndecl)
12821 vec<char_p> *v;
12823 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12824 if (v && v->length () > 0)
12826 const char *name;
12827 int i;
12828 char *s;
12830 name = lang_hooks.decl_printable_name (fndecl, 0);
12831 FOR_EACH_VEC_ELT (*v, i, s)
12832 if (strstr (name, s) != NULL)
12833 return true;
12836 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12837 if (v && v->length () > 0)
12839 const char *name;
12840 int i;
12841 char *s;
12843 name = DECL_SOURCE_FILE (fndecl);
12844 FOR_EACH_VEC_ELT (*v, i, s)
12845 if (strstr (name, s) != NULL)
12846 return true;
12849 return false;
12852 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12853 node for the function we want to gimplify.
12855 Return the sequence of GIMPLE statements corresponding to the body
12856 of FNDECL. */
12858 void
12859 gimplify_function_tree (tree fndecl)
12861 tree parm, ret;
12862 gimple_seq seq;
12863 gbind *bind;
12865 gcc_assert (!gimple_body (fndecl));
12867 if (DECL_STRUCT_FUNCTION (fndecl))
12868 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12869 else
12870 push_struct_function (fndecl);
12872 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12873 if necessary. */
12874 cfun->curr_properties |= PROP_gimple_lva;
12876 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12878 /* Preliminarily mark non-addressed complex variables as eligible
12879 for promotion to gimple registers. We'll transform their uses
12880 as we find them. */
12881 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12882 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12883 && !TREE_THIS_VOLATILE (parm)
12884 && !needs_to_live_in_memory (parm))
12885 DECL_GIMPLE_REG_P (parm) = 1;
12888 ret = DECL_RESULT (fndecl);
12889 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12890 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12891 && !needs_to_live_in_memory (ret))
12892 DECL_GIMPLE_REG_P (ret) = 1;
12894 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12895 asan_poisoned_variables = new hash_set<tree> ();
12896 bind = gimplify_body (fndecl, true);
12897 if (asan_poisoned_variables)
12899 delete asan_poisoned_variables;
12900 asan_poisoned_variables = NULL;
12903 /* The tree body of the function is no longer needed, replace it
12904 with the new GIMPLE body. */
12905 seq = NULL;
12906 gimple_seq_add_stmt (&seq, bind);
12907 gimple_set_body (fndecl, seq);
12909 /* If we're instrumenting function entry/exit, then prepend the call to
12910 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12911 catch the exit hook. */
12912 /* ??? Add some way to ignore exceptions for this TFE. */
12913 if (flag_instrument_function_entry_exit
12914 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12915 /* Do not instrument extern inline functions. */
12916 && !(DECL_DECLARED_INLINE_P (fndecl)
12917 && DECL_EXTERNAL (fndecl)
12918 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12919 && !flag_instrument_functions_exclude_p (fndecl))
12921 tree x;
12922 gbind *new_bind;
12923 gimple *tf;
12924 gimple_seq cleanup = NULL, body = NULL;
12925 tree tmp_var;
12926 gcall *call;
12928 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12929 call = gimple_build_call (x, 1, integer_zero_node);
12930 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12931 gimple_call_set_lhs (call, tmp_var);
12932 gimplify_seq_add_stmt (&cleanup, call);
12933 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12934 call = gimple_build_call (x, 2,
12935 build_fold_addr_expr (current_function_decl),
12936 tmp_var);
12937 gimplify_seq_add_stmt (&cleanup, call);
12938 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12940 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12941 call = gimple_build_call (x, 1, integer_zero_node);
12942 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12943 gimple_call_set_lhs (call, tmp_var);
12944 gimplify_seq_add_stmt (&body, call);
12945 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12946 call = gimple_build_call (x, 2,
12947 build_fold_addr_expr (current_function_decl),
12948 tmp_var);
12949 gimplify_seq_add_stmt (&body, call);
12950 gimplify_seq_add_stmt (&body, tf);
12951 new_bind = gimple_build_bind (NULL, body, NULL);
12953 /* Replace the current function body with the body
12954 wrapped in the try/finally TF. */
12955 seq = NULL;
12956 gimple_seq_add_stmt (&seq, new_bind);
12957 gimple_set_body (fndecl, seq);
12958 bind = new_bind;
12961 if (sanitize_flags_p (SANITIZE_THREAD))
12963 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12964 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12965 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12966 /* Replace the current function body with the body
12967 wrapped in the try/finally TF. */
12968 seq = NULL;
12969 gimple_seq_add_stmt (&seq, new_bind);
12970 gimple_set_body (fndecl, seq);
12973 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12974 cfun->curr_properties |= PROP_gimple_any;
12976 pop_cfun ();
12978 dump_function (TDI_gimple, fndecl);
12981 /* Return a dummy expression of type TYPE in order to keep going after an
12982 error. */
12984 static tree
12985 dummy_object (tree type)
12987 tree t = build_int_cst (build_pointer_type (type), 0);
12988 return build2 (MEM_REF, type, t, t);
12991 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12992 builtin function, but a very special sort of operator. */
12994 enum gimplify_status
12995 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12996 gimple_seq *post_p ATTRIBUTE_UNUSED)
12998 tree promoted_type, have_va_type;
12999 tree valist = TREE_OPERAND (*expr_p, 0);
13000 tree type = TREE_TYPE (*expr_p);
13001 tree t, tag, aptag;
13002 location_t loc = EXPR_LOCATION (*expr_p);
13004 /* Verify that valist is of the proper type. */
13005 have_va_type = TREE_TYPE (valist);
13006 if (have_va_type == error_mark_node)
13007 return GS_ERROR;
13008 have_va_type = targetm.canonical_va_list_type (have_va_type);
13009 if (have_va_type == NULL_TREE
13010 && POINTER_TYPE_P (TREE_TYPE (valist)))
13011 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13012 have_va_type
13013 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13014 gcc_assert (have_va_type != NULL_TREE);
13016 /* Generate a diagnostic for requesting data of a type that cannot
13017 be passed through `...' due to type promotion at the call site. */
13018 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13019 != type)
13021 static bool gave_help;
13022 bool warned;
13023 /* Use the expansion point to handle cases such as passing bool (defined
13024 in a system header) through `...'. */
13025 source_location xloc
13026 = expansion_point_location_if_in_system_header (loc);
13028 /* Unfortunately, this is merely undefined, rather than a constraint
13029 violation, so we cannot make this an error. If this call is never
13030 executed, the program is still strictly conforming. */
13031 warned = warning_at (xloc, 0,
13032 "%qT is promoted to %qT when passed through %<...%>",
13033 type, promoted_type);
13034 if (!gave_help && warned)
13036 gave_help = true;
13037 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13038 promoted_type, type);
13041 /* We can, however, treat "undefined" any way we please.
13042 Call abort to encourage the user to fix the program. */
13043 if (warned)
13044 inform (xloc, "if this code is reached, the program will abort");
13045 /* Before the abort, allow the evaluation of the va_list
13046 expression to exit or longjmp. */
13047 gimplify_and_add (valist, pre_p);
13048 t = build_call_expr_loc (loc,
13049 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13050 gimplify_and_add (t, pre_p);
13052 /* This is dead code, but go ahead and finish so that the
13053 mode of the result comes out right. */
13054 *expr_p = dummy_object (type);
13055 return GS_ALL_DONE;
13058 tag = build_int_cst (build_pointer_type (type), 0);
13059 aptag = build_int_cst (TREE_TYPE (valist), 0);
13061 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13062 valist, tag, aptag);
13064 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13065 needs to be expanded. */
13066 cfun->curr_properties &= ~PROP_gimple_lva;
13068 return GS_OK;
13071 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13073 DST/SRC are the destination and source respectively. You can pass
13074 ungimplified trees in DST or SRC, in which case they will be
13075 converted to a gimple operand if necessary.
13077 This function returns the newly created GIMPLE_ASSIGN tuple. */
13079 gimple *
13080 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13082 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13083 gimplify_and_add (t, seq_p);
13084 ggc_free (t);
13085 return gimple_seq_last_stmt (*seq_p);
13088 inline hashval_t
13089 gimplify_hasher::hash (const elt_t *p)
13091 tree t = p->val;
13092 return iterative_hash_expr (t, 0);
13095 inline bool
13096 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13098 tree t1 = p1->val;
13099 tree t2 = p2->val;
13100 enum tree_code code = TREE_CODE (t1);
13102 if (TREE_CODE (t2) != code
13103 || TREE_TYPE (t1) != TREE_TYPE (t2))
13104 return false;
13106 if (!operand_equal_p (t1, t2, 0))
13107 return false;
13109 /* Only allow them to compare equal if they also hash equal; otherwise
13110 results are nondeterminate, and we fail bootstrap comparison. */
13111 gcc_checking_assert (hash (p1) == hash (p2));
13113 return true;