C++: suggestions for misspelled private members (PR c++/84993)
[official-gcc.git] / gcc / gimplify.c
blob509fc2f3f5be20985558e8b4f2e94a7cbf814567
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple.h"
33 #include "gimple-predict.h"
34 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "ssa.h"
36 #include "cgraph.h"
37 #include "tree-pretty-print.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
72 enum gimplify_omp_var_data
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
94 GOVD_MAP_0LEN_ARRAY = 32768,
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO = 65536,
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN = 131072,
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE = 262144,
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT = 524288,
108 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
109 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
110 | GOVD_LOCAL)
114 enum omp_region_type
116 ORT_WORKSHARE = 0x00,
117 ORT_SIMD = 0x01,
119 ORT_PARALLEL = 0x02,
120 ORT_COMBINED_PARALLEL = 0x03,
122 ORT_TASK = 0x04,
123 ORT_UNTIED_TASK = 0x05,
125 ORT_TEAMS = 0x08,
126 ORT_COMBINED_TEAMS = 0x09,
128 /* Data region. */
129 ORT_TARGET_DATA = 0x10,
131 /* Data region with offloading. */
132 ORT_TARGET = 0x20,
133 ORT_COMBINED_TARGET = 0x21,
135 /* OpenACC variants. */
136 ORT_ACC = 0x40, /* A generic OpenACC region. */
137 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
138 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
139 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
140 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
142 /* Dummy OpenMP region, used to disable expansion of
143 DECL_VALUE_EXPRs in taskloop pre body. */
144 ORT_NONE = 0x100
147 /* Gimplify hashtable helper. */
149 struct gimplify_hasher : free_ptr_hash <elt_t>
151 static inline hashval_t hash (const elt_t *);
152 static inline bool equal (const elt_t *, const elt_t *);
155 struct gimplify_ctx
157 struct gimplify_ctx *prev_context;
159 vec<gbind *> bind_expr_stack;
160 tree temps;
161 gimple_seq conditional_cleanups;
162 tree exit_label;
163 tree return_temp;
165 vec<tree> case_labels;
166 hash_set<tree> *live_switch_vars;
167 /* The formal temporary table. Should this be persistent? */
168 hash_table<gimplify_hasher> *temp_htab;
170 int conditions;
171 unsigned into_ssa : 1;
172 unsigned allow_rhs_cond_expr : 1;
173 unsigned in_cleanup_point_expr : 1;
174 unsigned keep_stack : 1;
175 unsigned save_stack : 1;
176 unsigned in_switch_expr : 1;
179 struct gimplify_omp_ctx
181 struct gimplify_omp_ctx *outer_context;
182 splay_tree variables;
183 hash_set<tree> *privatized_types;
184 /* Iteration variables in an OMP_FOR. */
185 vec<tree> loop_iter_var;
186 location_t location;
187 enum omp_clause_default_kind default_kind;
188 enum omp_region_type region_type;
189 bool combined_loop;
190 bool distribute;
191 bool target_map_scalars_firstprivate;
192 bool target_map_pointers_as_0len_arrays;
193 bool target_firstprivatize_array_bases;
196 static struct gimplify_ctx *gimplify_ctxp;
197 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
199 /* Forward declaration. */
200 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
201 static hash_map<tree, tree> *oacc_declare_returns;
202 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
203 bool (*) (tree), fallback_t, bool);
205 /* Shorter alias name for the above function for use in gimplify.c
206 only. */
208 static inline void
209 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
211 gimple_seq_add_stmt_without_update (seq_p, gs);
214 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
215 NULL, a new sequence is allocated. This function is
216 similar to gimple_seq_add_seq, but does not scan the operands.
217 During gimplification, we need to manipulate statement sequences
218 before the def/use vectors have been constructed. */
220 static void
221 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
223 gimple_stmt_iterator si;
225 if (src == NULL)
226 return;
228 si = gsi_last (*dst_p);
229 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
233 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
234 and popping gimplify contexts. */
236 static struct gimplify_ctx *ctx_pool = NULL;
238 /* Return a gimplify context struct from the pool. */
240 static inline struct gimplify_ctx *
241 ctx_alloc (void)
243 struct gimplify_ctx * c = ctx_pool;
245 if (c)
246 ctx_pool = c->prev_context;
247 else
248 c = XNEW (struct gimplify_ctx);
250 memset (c, '\0', sizeof (*c));
251 return c;
254 /* Put gimplify context C back into the pool. */
256 static inline void
257 ctx_free (struct gimplify_ctx *c)
259 c->prev_context = ctx_pool;
260 ctx_pool = c;
263 /* Free allocated ctx stack memory. */
265 void
266 free_gimplify_stack (void)
268 struct gimplify_ctx *c;
270 while ((c = ctx_pool))
272 ctx_pool = c->prev_context;
273 free (c);
278 /* Set up a context for the gimplifier. */
280 void
281 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
283 struct gimplify_ctx *c = ctx_alloc ();
285 c->prev_context = gimplify_ctxp;
286 gimplify_ctxp = c;
287 gimplify_ctxp->into_ssa = in_ssa;
288 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
291 /* Tear down a context for the gimplifier. If BODY is non-null, then
292 put the temporaries into the outer BIND_EXPR. Otherwise, put them
293 in the local_decls.
295 BODY is not a sequence, but the first tuple in a sequence. */
297 void
298 pop_gimplify_context (gimple *body)
300 struct gimplify_ctx *c = gimplify_ctxp;
302 gcc_assert (c
303 && (!c->bind_expr_stack.exists ()
304 || c->bind_expr_stack.is_empty ()));
305 c->bind_expr_stack.release ();
306 gimplify_ctxp = c->prev_context;
308 if (body)
309 declare_vars (c->temps, body, false);
310 else
311 record_vars (c->temps);
313 delete c->temp_htab;
314 c->temp_htab = NULL;
315 ctx_free (c);
318 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
320 static void
321 gimple_push_bind_expr (gbind *bind_stmt)
323 gimplify_ctxp->bind_expr_stack.reserve (8);
324 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
327 /* Pop the first element off the stack of bindings. */
329 static void
330 gimple_pop_bind_expr (void)
332 gimplify_ctxp->bind_expr_stack.pop ();
335 /* Return the first element of the stack of bindings. */
337 gbind *
338 gimple_current_bind_expr (void)
340 return gimplify_ctxp->bind_expr_stack.last ();
343 /* Return the stack of bindings created during gimplification. */
345 vec<gbind *>
346 gimple_bind_expr_stack (void)
348 return gimplify_ctxp->bind_expr_stack;
351 /* Return true iff there is a COND_EXPR between us and the innermost
352 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
354 static bool
355 gimple_conditional_context (void)
357 return gimplify_ctxp->conditions > 0;
360 /* Note that we've entered a COND_EXPR. */
362 static void
363 gimple_push_condition (void)
365 #ifdef ENABLE_GIMPLE_CHECKING
366 if (gimplify_ctxp->conditions == 0)
367 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
368 #endif
369 ++(gimplify_ctxp->conditions);
372 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
373 now, add any conditional cleanups we've seen to the prequeue. */
375 static void
376 gimple_pop_condition (gimple_seq *pre_p)
378 int conds = --(gimplify_ctxp->conditions);
380 gcc_assert (conds >= 0);
381 if (conds == 0)
383 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
384 gimplify_ctxp->conditional_cleanups = NULL;
388 /* A stable comparison routine for use with splay trees and DECLs. */
390 static int
391 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
393 tree a = (tree) xa;
394 tree b = (tree) xb;
396 return DECL_UID (a) - DECL_UID (b);
399 /* Create a new omp construct that deals with variable remapping. */
401 static struct gimplify_omp_ctx *
402 new_omp_context (enum omp_region_type region_type)
404 struct gimplify_omp_ctx *c;
406 c = XCNEW (struct gimplify_omp_ctx);
407 c->outer_context = gimplify_omp_ctxp;
408 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
409 c->privatized_types = new hash_set<tree>;
410 c->location = input_location;
411 c->region_type = region_type;
412 if ((region_type & ORT_TASK) == 0)
413 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
414 else
415 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
417 return c;
420 /* Destroy an omp construct that deals with variable remapping. */
422 static void
423 delete_omp_context (struct gimplify_omp_ctx *c)
425 splay_tree_delete (c->variables);
426 delete c->privatized_types;
427 c->loop_iter_var.release ();
428 XDELETE (c);
431 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
432 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
434 /* Both gimplify the statement T and append it to *SEQ_P. This function
435 behaves exactly as gimplify_stmt, but you don't have to pass T as a
436 reference. */
438 void
439 gimplify_and_add (tree t, gimple_seq *seq_p)
441 gimplify_stmt (&t, seq_p);
444 /* Gimplify statement T into sequence *SEQ_P, and return the first
445 tuple in the sequence of generated tuples for this statement.
446 Return NULL if gimplifying T produced no tuples. */
448 static gimple *
449 gimplify_and_return_first (tree t, gimple_seq *seq_p)
451 gimple_stmt_iterator last = gsi_last (*seq_p);
453 gimplify_and_add (t, seq_p);
455 if (!gsi_end_p (last))
457 gsi_next (&last);
458 return gsi_stmt (last);
460 else
461 return gimple_seq_first_stmt (*seq_p);
464 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
465 LHS, or for a call argument. */
467 static bool
468 is_gimple_mem_rhs (tree t)
470 /* If we're dealing with a renamable type, either source or dest must be
471 a renamed variable. */
472 if (is_gimple_reg_type (TREE_TYPE (t)))
473 return is_gimple_val (t);
474 else
475 return is_gimple_val (t) || is_gimple_lvalue (t);
478 /* Return true if T is a CALL_EXPR or an expression that can be
479 assigned to a temporary. Note that this predicate should only be
480 used during gimplification. See the rationale for this in
481 gimplify_modify_expr. */
483 static bool
484 is_gimple_reg_rhs_or_call (tree t)
486 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
487 || TREE_CODE (t) == CALL_EXPR);
490 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
491 this predicate should only be used during gimplification. See the
492 rationale for this in gimplify_modify_expr. */
494 static bool
495 is_gimple_mem_rhs_or_call (tree t)
497 /* If we're dealing with a renamable type, either source or dest must be
498 a renamed variable. */
499 if (is_gimple_reg_type (TREE_TYPE (t)))
500 return is_gimple_val (t);
501 else
502 return (is_gimple_val (t)
503 || is_gimple_lvalue (t)
504 || TREE_CLOBBER_P (t)
505 || TREE_CODE (t) == CALL_EXPR);
508 /* Create a temporary with a name derived from VAL. Subroutine of
509 lookup_tmp_var; nobody else should call this function. */
511 static inline tree
512 create_tmp_from_val (tree val)
514 /* Drop all qualifiers and address-space information from the value type. */
515 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
516 tree var = create_tmp_var (type, get_name (val));
517 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
518 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
519 DECL_GIMPLE_REG_P (var) = 1;
520 return var;
523 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
524 an existing expression temporary. */
526 static tree
527 lookup_tmp_var (tree val, bool is_formal)
529 tree ret;
531 /* If not optimizing, never really reuse a temporary. local-alloc
532 won't allocate any variable that is used in more than one basic
533 block, which means it will go into memory, causing much extra
534 work in reload and final and poorer code generation, outweighing
535 the extra memory allocation here. */
536 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
537 ret = create_tmp_from_val (val);
538 else
540 elt_t elt, *elt_p;
541 elt_t **slot;
543 elt.val = val;
544 if (!gimplify_ctxp->temp_htab)
545 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
546 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
547 if (*slot == NULL)
549 elt_p = XNEW (elt_t);
550 elt_p->val = val;
551 elt_p->temp = ret = create_tmp_from_val (val);
552 *slot = elt_p;
554 else
556 elt_p = *slot;
557 ret = elt_p->temp;
561 return ret;
564 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
566 static tree
567 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
568 bool is_formal, bool allow_ssa)
570 tree t, mod;
572 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
573 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
574 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
575 fb_rvalue);
577 if (allow_ssa
578 && gimplify_ctxp->into_ssa
579 && is_gimple_reg_type (TREE_TYPE (val)))
581 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
582 if (! gimple_in_ssa_p (cfun))
584 const char *name = get_name (val);
585 if (name)
586 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
589 else
590 t = lookup_tmp_var (val, is_formal);
592 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
594 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
596 /* gimplify_modify_expr might want to reduce this further. */
597 gimplify_and_add (mod, pre_p);
598 ggc_free (mod);
600 return t;
603 /* Return a formal temporary variable initialized with VAL. PRE_P is as
604 in gimplify_expr. Only use this function if:
606 1) The value of the unfactored expression represented by VAL will not
607 change between the initialization and use of the temporary, and
608 2) The temporary will not be otherwise modified.
610 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
611 and #2 means it is inappropriate for && temps.
613 For other cases, use get_initialized_tmp_var instead. */
615 tree
616 get_formal_tmp_var (tree val, gimple_seq *pre_p)
618 return internal_get_tmp_var (val, pre_p, NULL, true, true);
621 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
622 are as in gimplify_expr. */
624 tree
625 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
626 bool allow_ssa)
628 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
631 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
632 generate debug info for them; otherwise don't. */
634 void
635 declare_vars (tree vars, gimple *gs, bool debug_info)
637 tree last = vars;
638 if (last)
640 tree temps, block;
642 gbind *scope = as_a <gbind *> (gs);
644 temps = nreverse (last);
646 block = gimple_bind_block (scope);
647 gcc_assert (!block || TREE_CODE (block) == BLOCK);
648 if (!block || !debug_info)
650 DECL_CHAIN (last) = gimple_bind_vars (scope);
651 gimple_bind_set_vars (scope, temps);
653 else
655 /* We need to attach the nodes both to the BIND_EXPR and to its
656 associated BLOCK for debugging purposes. The key point here
657 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
658 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
659 if (BLOCK_VARS (block))
660 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
661 else
663 gimple_bind_set_vars (scope,
664 chainon (gimple_bind_vars (scope), temps));
665 BLOCK_VARS (block) = temps;
671 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
672 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
673 no such upper bound can be obtained. */
675 static void
676 force_constant_size (tree var)
678 /* The only attempt we make is by querying the maximum size of objects
679 of the variable's type. */
681 HOST_WIDE_INT max_size;
683 gcc_assert (VAR_P (var));
685 max_size = max_int_size_in_bytes (TREE_TYPE (var));
687 gcc_assert (max_size >= 0);
689 DECL_SIZE_UNIT (var)
690 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
691 DECL_SIZE (var)
692 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
695 /* Push the temporary variable TMP into the current binding. */
697 void
698 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
700 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
702 /* Later processing assumes that the object size is constant, which might
703 not be true at this point. Force the use of a constant upper bound in
704 this case. */
705 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
706 force_constant_size (tmp);
708 DECL_CONTEXT (tmp) = fn->decl;
709 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
711 record_vars_into (tmp, fn->decl);
714 /* Push the temporary variable TMP into the current binding. */
716 void
717 gimple_add_tmp_var (tree tmp)
719 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
721 /* Later processing assumes that the object size is constant, which might
722 not be true at this point. Force the use of a constant upper bound in
723 this case. */
724 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
725 force_constant_size (tmp);
727 DECL_CONTEXT (tmp) = current_function_decl;
728 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
730 if (gimplify_ctxp)
732 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
733 gimplify_ctxp->temps = tmp;
735 /* Mark temporaries local within the nearest enclosing parallel. */
736 if (gimplify_omp_ctxp)
738 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
739 while (ctx
740 && (ctx->region_type == ORT_WORKSHARE
741 || ctx->region_type == ORT_SIMD
742 || ctx->region_type == ORT_ACC))
743 ctx = ctx->outer_context;
744 if (ctx)
745 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
748 else if (cfun)
749 record_vars (tmp);
750 else
752 gimple_seq body_seq;
754 /* This case is for nested functions. We need to expose the locals
755 they create. */
756 body_seq = gimple_body (current_function_decl);
757 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
763 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
764 nodes that are referenced more than once in GENERIC functions. This is
765 necessary because gimplification (translation into GIMPLE) is performed
766 by modifying tree nodes in-place, so gimplication of a shared node in a
767 first context could generate an invalid GIMPLE form in a second context.
769 This is achieved with a simple mark/copy/unmark algorithm that walks the
770 GENERIC representation top-down, marks nodes with TREE_VISITED the first
771 time it encounters them, duplicates them if they already have TREE_VISITED
772 set, and finally removes the TREE_VISITED marks it has set.
774 The algorithm works only at the function level, i.e. it generates a GENERIC
775 representation of a function with no nodes shared within the function when
776 passed a GENERIC function (except for nodes that are allowed to be shared).
778 At the global level, it is also necessary to unshare tree nodes that are
779 referenced in more than one function, for the same aforementioned reason.
780 This requires some cooperation from the front-end. There are 2 strategies:
782 1. Manual unsharing. The front-end needs to call unshare_expr on every
783 expression that might end up being shared across functions.
785 2. Deep unsharing. This is an extension of regular unsharing. Instead
786 of calling unshare_expr on expressions that might be shared across
787 functions, the front-end pre-marks them with TREE_VISITED. This will
788 ensure that they are unshared on the first reference within functions
789 when the regular unsharing algorithm runs. The counterpart is that
790 this algorithm must look deeper than for manual unsharing, which is
791 specified by LANG_HOOKS_DEEP_UNSHARING.
793 If there are only few specific cases of node sharing across functions, it is
794 probably easier for a front-end to unshare the expressions manually. On the
795 contrary, if the expressions generated at the global level are as widespread
796 as expressions generated within functions, deep unsharing is very likely the
797 way to go. */
799 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that must be done once. If we were to
801 unshare something like SAVE_EXPR(i++), the gimplification process would
802 create wrong code. However, if DATA is non-null, it must hold a pointer
803 set that is used to unshare the subtrees of these nodes. */
805 static tree
806 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
808 tree t = *tp;
809 enum tree_code code = TREE_CODE (t);
811 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
812 copy their subtrees if we can make sure to do it only once. */
813 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
815 if (data && !((hash_set<tree> *)data)->add (t))
817 else
818 *walk_subtrees = 0;
821 /* Stop at types, decls, constants like copy_tree_r. */
822 else if (TREE_CODE_CLASS (code) == tcc_type
823 || TREE_CODE_CLASS (code) == tcc_declaration
824 || TREE_CODE_CLASS (code) == tcc_constant)
825 *walk_subtrees = 0;
827 /* Cope with the statement expression extension. */
828 else if (code == STATEMENT_LIST)
831 /* Leave the bulk of the work to copy_tree_r itself. */
832 else
833 copy_tree_r (tp, walk_subtrees, NULL);
835 return NULL_TREE;
838 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
839 If *TP has been visited already, then *TP is deeply copied by calling
840 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
842 static tree
843 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
845 tree t = *tp;
846 enum tree_code code = TREE_CODE (t);
848 /* Skip types, decls, and constants. But we do want to look at their
849 types and the bounds of types. Mark them as visited so we properly
850 unmark their subtrees on the unmark pass. If we've already seen them,
851 don't look down further. */
852 if (TREE_CODE_CLASS (code) == tcc_type
853 || TREE_CODE_CLASS (code) == tcc_declaration
854 || TREE_CODE_CLASS (code) == tcc_constant)
856 if (TREE_VISITED (t))
857 *walk_subtrees = 0;
858 else
859 TREE_VISITED (t) = 1;
862 /* If this node has been visited already, unshare it and don't look
863 any deeper. */
864 else if (TREE_VISITED (t))
866 walk_tree (tp, mostly_copy_tree_r, data, NULL);
867 *walk_subtrees = 0;
870 /* Otherwise, mark the node as visited and keep looking. */
871 else
872 TREE_VISITED (t) = 1;
874 return NULL_TREE;
877 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
878 copy_if_shared_r callback unmodified. */
880 static inline void
881 copy_if_shared (tree *tp, void *data)
883 walk_tree (tp, copy_if_shared_r, data, NULL);
886 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
887 any nested functions. */
889 static void
890 unshare_body (tree fndecl)
892 struct cgraph_node *cgn = cgraph_node::get (fndecl);
893 /* If the language requires deep unsharing, we need a pointer set to make
894 sure we don't repeatedly unshare subtrees of unshareable nodes. */
895 hash_set<tree> *visited
896 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
898 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
899 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
900 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
902 delete visited;
904 if (cgn)
905 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
906 unshare_body (cgn->decl);
909 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
910 Subtrees are walked until the first unvisited node is encountered. */
912 static tree
913 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
915 tree t = *tp;
917 /* If this node has been visited, unmark it and keep looking. */
918 if (TREE_VISITED (t))
919 TREE_VISITED (t) = 0;
921 /* Otherwise, don't look any deeper. */
922 else
923 *walk_subtrees = 0;
925 return NULL_TREE;
928 /* Unmark the visited trees rooted at *TP. */
930 static inline void
931 unmark_visited (tree *tp)
933 walk_tree (tp, unmark_visited_r, NULL, NULL);
936 /* Likewise, but mark all trees as not visited. */
938 static void
939 unvisit_body (tree fndecl)
941 struct cgraph_node *cgn = cgraph_node::get (fndecl);
943 unmark_visited (&DECL_SAVED_TREE (fndecl));
944 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
945 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
947 if (cgn)
948 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
949 unvisit_body (cgn->decl);
952 /* Unconditionally make an unshared copy of EXPR. This is used when using
953 stored expressions which span multiple functions, such as BINFO_VTABLE,
954 as the normal unsharing process can't tell that they're shared. */
956 tree
957 unshare_expr (tree expr)
959 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
960 return expr;
963 /* Worker for unshare_expr_without_location. */
965 static tree
966 prune_expr_location (tree *tp, int *walk_subtrees, void *)
968 if (EXPR_P (*tp))
969 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
970 else
971 *walk_subtrees = 0;
972 return NULL_TREE;
975 /* Similar to unshare_expr but also prune all expression locations
976 from EXPR. */
978 tree
979 unshare_expr_without_location (tree expr)
981 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
982 if (EXPR_P (expr))
983 walk_tree (&expr, prune_expr_location, NULL, NULL);
984 return expr;
987 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
988 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
989 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
990 EXPR is the location of the EXPR. */
992 static location_t
993 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
995 if (!expr)
996 return or_else;
998 if (EXPR_HAS_LOCATION (expr))
999 return EXPR_LOCATION (expr);
1001 if (TREE_CODE (expr) != STATEMENT_LIST)
1002 return or_else;
1004 tree_stmt_iterator i = tsi_start (expr);
1006 bool found = false;
1007 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1009 found = true;
1010 tsi_next (&i);
1013 if (!found || !tsi_one_before_end_p (i))
1014 return or_else;
1016 return rexpr_location (tsi_stmt (i), or_else);
1019 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1020 rexpr_location for the potential recursion. */
1022 static inline bool
1023 rexpr_has_location (tree expr)
1025 return rexpr_location (expr) != UNKNOWN_LOCATION;
1029 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1030 contain statements and have a value. Assign its value to a temporary
1031 and give it void_type_node. Return the temporary, or NULL_TREE if
1032 WRAPPER was already void. */
1034 tree
1035 voidify_wrapper_expr (tree wrapper, tree temp)
1037 tree type = TREE_TYPE (wrapper);
1038 if (type && !VOID_TYPE_P (type))
1040 tree *p;
1042 /* Set p to point to the body of the wrapper. Loop until we find
1043 something that isn't a wrapper. */
1044 for (p = &wrapper; p && *p; )
1046 switch (TREE_CODE (*p))
1048 case BIND_EXPR:
1049 TREE_SIDE_EFFECTS (*p) = 1;
1050 TREE_TYPE (*p) = void_type_node;
1051 /* For a BIND_EXPR, the body is operand 1. */
1052 p = &BIND_EXPR_BODY (*p);
1053 break;
1055 case CLEANUP_POINT_EXPR:
1056 case TRY_FINALLY_EXPR:
1057 case TRY_CATCH_EXPR:
1058 TREE_SIDE_EFFECTS (*p) = 1;
1059 TREE_TYPE (*p) = void_type_node;
1060 p = &TREE_OPERAND (*p, 0);
1061 break;
1063 case STATEMENT_LIST:
1065 tree_stmt_iterator i = tsi_last (*p);
1066 TREE_SIDE_EFFECTS (*p) = 1;
1067 TREE_TYPE (*p) = void_type_node;
1068 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1070 break;
1072 case COMPOUND_EXPR:
1073 /* Advance to the last statement. Set all container types to
1074 void. */
1075 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1077 TREE_SIDE_EFFECTS (*p) = 1;
1078 TREE_TYPE (*p) = void_type_node;
1080 break;
1082 case TRANSACTION_EXPR:
1083 TREE_SIDE_EFFECTS (*p) = 1;
1084 TREE_TYPE (*p) = void_type_node;
1085 p = &TRANSACTION_EXPR_BODY (*p);
1086 break;
1088 default:
1089 /* Assume that any tree upon which voidify_wrapper_expr is
1090 directly called is a wrapper, and that its body is op0. */
1091 if (p == &wrapper)
1093 TREE_SIDE_EFFECTS (*p) = 1;
1094 TREE_TYPE (*p) = void_type_node;
1095 p = &TREE_OPERAND (*p, 0);
1096 break;
1098 goto out;
1102 out:
1103 if (p == NULL || IS_EMPTY_STMT (*p))
1104 temp = NULL_TREE;
1105 else if (temp)
1107 /* The wrapper is on the RHS of an assignment that we're pushing
1108 down. */
1109 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1110 || TREE_CODE (temp) == MODIFY_EXPR);
1111 TREE_OPERAND (temp, 1) = *p;
1112 *p = temp;
1114 else
1116 temp = create_tmp_var (type, "retval");
1117 *p = build2 (INIT_EXPR, type, temp, *p);
1120 return temp;
1123 return NULL_TREE;
1126 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1127 a temporary through which they communicate. */
1129 static void
1130 build_stack_save_restore (gcall **save, gcall **restore)
1132 tree tmp_var;
1134 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1135 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1136 gimple_call_set_lhs (*save, tmp_var);
1138 *restore
1139 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1140 1, tmp_var);
1143 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1145 static tree
1146 build_asan_poison_call_expr (tree decl)
1148 /* Do not poison variables that have size equal to zero. */
1149 tree unit_size = DECL_SIZE_UNIT (decl);
1150 if (zerop (unit_size))
1151 return NULL_TREE;
1153 tree base = build_fold_addr_expr (decl);
1155 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1156 void_type_node, 3,
1157 build_int_cst (integer_type_node,
1158 ASAN_MARK_POISON),
1159 base, unit_size);
1162 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1163 on POISON flag, shadow memory of a DECL variable. The call will be
1164 put on location identified by IT iterator, where BEFORE flag drives
1165 position where the stmt will be put. */
1167 static void
1168 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1169 bool before)
1171 tree unit_size = DECL_SIZE_UNIT (decl);
1172 tree base = build_fold_addr_expr (decl);
1174 /* Do not poison variables that have size equal to zero. */
1175 if (zerop (unit_size))
1176 return;
1178 /* It's necessary to have all stack variables aligned to ASAN granularity
1179 bytes. */
1180 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1181 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1183 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1185 gimple *g
1186 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1187 build_int_cst (integer_type_node, flags),
1188 base, unit_size);
1190 if (before)
1191 gsi_insert_before (it, g, GSI_NEW_STMT);
1192 else
1193 gsi_insert_after (it, g, GSI_NEW_STMT);
1196 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1197 either poisons or unpoisons a DECL. Created statement is appended
1198 to SEQ_P gimple sequence. */
1200 static void
1201 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1203 gimple_stmt_iterator it = gsi_last (*seq_p);
1204 bool before = false;
1206 if (gsi_end_p (it))
1207 before = true;
1209 asan_poison_variable (decl, poison, &it, before);
1212 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1214 static int
1215 sort_by_decl_uid (const void *a, const void *b)
1217 const tree *t1 = (const tree *)a;
1218 const tree *t2 = (const tree *)b;
1220 int uid1 = DECL_UID (*t1);
1221 int uid2 = DECL_UID (*t2);
1223 if (uid1 < uid2)
1224 return -1;
1225 else if (uid1 > uid2)
1226 return 1;
1227 else
1228 return 0;
1231 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1232 depending on POISON flag. Created statement is appended
1233 to SEQ_P gimple sequence. */
1235 static void
1236 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1238 unsigned c = variables->elements ();
1239 if (c == 0)
1240 return;
1242 auto_vec<tree> sorted_variables (c);
1244 for (hash_set<tree>::iterator it = variables->begin ();
1245 it != variables->end (); ++it)
1246 sorted_variables.safe_push (*it);
1248 sorted_variables.qsort (sort_by_decl_uid);
1250 unsigned i;
1251 tree var;
1252 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1254 asan_poison_variable (var, poison, seq_p);
1256 /* Add use_after_scope_memory attribute for the variable in order
1257 to prevent re-written into SSA. */
1258 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1259 DECL_ATTRIBUTES (var)))
1260 DECL_ATTRIBUTES (var)
1261 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1262 integer_one_node,
1263 DECL_ATTRIBUTES (var));
1267 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1269 static enum gimplify_status
1270 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1272 tree bind_expr = *expr_p;
1273 bool old_keep_stack = gimplify_ctxp->keep_stack;
1274 bool old_save_stack = gimplify_ctxp->save_stack;
1275 tree t;
1276 gbind *bind_stmt;
1277 gimple_seq body, cleanup;
1278 gcall *stack_save;
1279 location_t start_locus = 0, end_locus = 0;
1280 tree ret_clauses = NULL;
1282 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1284 /* Mark variables seen in this bind expr. */
1285 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1287 if (VAR_P (t))
1289 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1291 /* Mark variable as local. */
1292 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1293 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1294 || splay_tree_lookup (ctx->variables,
1295 (splay_tree_key) t) == NULL))
1297 if (ctx->region_type == ORT_SIMD
1298 && TREE_ADDRESSABLE (t)
1299 && !TREE_STATIC (t))
1300 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1301 else
1302 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1305 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1307 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1308 cfun->has_local_explicit_reg_vars = true;
1311 /* Preliminarily mark non-addressed complex variables as eligible
1312 for promotion to gimple registers. We'll transform their uses
1313 as we find them. */
1314 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1315 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1316 && !TREE_THIS_VOLATILE (t)
1317 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1318 && !needs_to_live_in_memory (t))
1319 DECL_GIMPLE_REG_P (t) = 1;
1322 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1323 BIND_EXPR_BLOCK (bind_expr));
1324 gimple_push_bind_expr (bind_stmt);
1326 gimplify_ctxp->keep_stack = false;
1327 gimplify_ctxp->save_stack = false;
1329 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1330 body = NULL;
1331 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1332 gimple_bind_set_body (bind_stmt, body);
1334 /* Source location wise, the cleanup code (stack_restore and clobbers)
1335 belongs to the end of the block, so propagate what we have. The
1336 stack_save operation belongs to the beginning of block, which we can
1337 infer from the bind_expr directly if the block has no explicit
1338 assignment. */
1339 if (BIND_EXPR_BLOCK (bind_expr))
1341 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1342 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1344 if (start_locus == 0)
1345 start_locus = EXPR_LOCATION (bind_expr);
1347 cleanup = NULL;
1348 stack_save = NULL;
1350 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1351 the stack space allocated to the VLAs. */
1352 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1354 gcall *stack_restore;
1356 /* Save stack on entry and restore it on exit. Add a try_finally
1357 block to achieve this. */
1358 build_stack_save_restore (&stack_save, &stack_restore);
1360 gimple_set_location (stack_save, start_locus);
1361 gimple_set_location (stack_restore, end_locus);
1363 gimplify_seq_add_stmt (&cleanup, stack_restore);
1366 /* Add clobbers for all variables that go out of scope. */
1367 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1369 if (VAR_P (t)
1370 && !is_global_var (t)
1371 && DECL_CONTEXT (t) == current_function_decl)
1373 if (!DECL_HARD_REGISTER (t)
1374 && !TREE_THIS_VOLATILE (t)
1375 && !DECL_HAS_VALUE_EXPR_P (t)
1376 /* Only care for variables that have to be in memory. Others
1377 will be rewritten into SSA names, hence moved to the
1378 top-level. */
1379 && !is_gimple_reg (t)
1380 && flag_stack_reuse != SR_NONE)
1382 tree clobber = build_clobber (TREE_TYPE (t));
1383 gimple *clobber_stmt;
1384 clobber_stmt = gimple_build_assign (t, clobber);
1385 gimple_set_location (clobber_stmt, end_locus);
1386 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1389 if (flag_openacc && oacc_declare_returns != NULL)
1391 tree *c = oacc_declare_returns->get (t);
1392 if (c != NULL)
1394 if (ret_clauses)
1395 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1397 ret_clauses = *c;
1399 oacc_declare_returns->remove (t);
1401 if (oacc_declare_returns->elements () == 0)
1403 delete oacc_declare_returns;
1404 oacc_declare_returns = NULL;
1410 if (asan_poisoned_variables != NULL
1411 && asan_poisoned_variables->contains (t))
1413 asan_poisoned_variables->remove (t);
1414 asan_poison_variable (t, true, &cleanup);
1417 if (gimplify_ctxp->live_switch_vars != NULL
1418 && gimplify_ctxp->live_switch_vars->contains (t))
1419 gimplify_ctxp->live_switch_vars->remove (t);
1422 if (ret_clauses)
1424 gomp_target *stmt;
1425 gimple_stmt_iterator si = gsi_start (cleanup);
1427 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1428 ret_clauses);
1429 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1432 if (cleanup)
1434 gtry *gs;
1435 gimple_seq new_body;
1437 new_body = NULL;
1438 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1439 GIMPLE_TRY_FINALLY);
1441 if (stack_save)
1442 gimplify_seq_add_stmt (&new_body, stack_save);
1443 gimplify_seq_add_stmt (&new_body, gs);
1444 gimple_bind_set_body (bind_stmt, new_body);
1447 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1448 if (!gimplify_ctxp->keep_stack)
1449 gimplify_ctxp->keep_stack = old_keep_stack;
1450 gimplify_ctxp->save_stack = old_save_stack;
1452 gimple_pop_bind_expr ();
1454 gimplify_seq_add_stmt (pre_p, bind_stmt);
1456 if (temp)
1458 *expr_p = temp;
1459 return GS_OK;
1462 *expr_p = NULL_TREE;
1463 return GS_ALL_DONE;
1466 /* Maybe add early return predict statement to PRE_P sequence. */
1468 static void
1469 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1471 /* If we are not in a conditional context, add PREDICT statement. */
1472 if (gimple_conditional_context ())
1474 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1475 NOT_TAKEN);
1476 gimplify_seq_add_stmt (pre_p, predict);
1480 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1481 GIMPLE value, it is assigned to a new temporary and the statement is
1482 re-written to return the temporary.
1484 PRE_P points to the sequence where side effects that must happen before
1485 STMT should be stored. */
1487 static enum gimplify_status
1488 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1490 greturn *ret;
1491 tree ret_expr = TREE_OPERAND (stmt, 0);
1492 tree result_decl, result;
1494 if (ret_expr == error_mark_node)
1495 return GS_ERROR;
1497 if (!ret_expr
1498 || TREE_CODE (ret_expr) == RESULT_DECL)
1500 maybe_add_early_return_predict_stmt (pre_p);
1501 greturn *ret = gimple_build_return (ret_expr);
1502 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1503 gimplify_seq_add_stmt (pre_p, ret);
1504 return GS_ALL_DONE;
1507 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1508 result_decl = NULL_TREE;
1509 else
1511 result_decl = TREE_OPERAND (ret_expr, 0);
1513 /* See through a return by reference. */
1514 if (TREE_CODE (result_decl) == INDIRECT_REF)
1515 result_decl = TREE_OPERAND (result_decl, 0);
1517 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1518 || TREE_CODE (ret_expr) == INIT_EXPR)
1519 && TREE_CODE (result_decl) == RESULT_DECL);
1522 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1523 Recall that aggregate_value_p is FALSE for any aggregate type that is
1524 returned in registers. If we're returning values in registers, then
1525 we don't want to extend the lifetime of the RESULT_DECL, particularly
1526 across another call. In addition, for those aggregates for which
1527 hard_function_value generates a PARALLEL, we'll die during normal
1528 expansion of structure assignments; there's special code in expand_return
1529 to handle this case that does not exist in expand_expr. */
1530 if (!result_decl)
1531 result = NULL_TREE;
1532 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1534 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1536 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1537 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1538 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1539 should be effectively allocated by the caller, i.e. all calls to
1540 this function must be subject to the Return Slot Optimization. */
1541 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1542 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1544 result = result_decl;
1546 else if (gimplify_ctxp->return_temp)
1547 result = gimplify_ctxp->return_temp;
1548 else
1550 result = create_tmp_reg (TREE_TYPE (result_decl));
1552 /* ??? With complex control flow (usually involving abnormal edges),
1553 we can wind up warning about an uninitialized value for this. Due
1554 to how this variable is constructed and initialized, this is never
1555 true. Give up and never warn. */
1556 TREE_NO_WARNING (result) = 1;
1558 gimplify_ctxp->return_temp = result;
1561 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1562 Then gimplify the whole thing. */
1563 if (result != result_decl)
1564 TREE_OPERAND (ret_expr, 0) = result;
1566 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1568 maybe_add_early_return_predict_stmt (pre_p);
1569 ret = gimple_build_return (result);
1570 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1571 gimplify_seq_add_stmt (pre_p, ret);
1573 return GS_ALL_DONE;
1576 /* Gimplify a variable-length array DECL. */
1578 static void
1579 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1581 /* This is a variable-sized decl. Simplify its size and mark it
1582 for deferred expansion. */
1583 tree t, addr, ptr_type;
1585 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1586 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1588 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1589 if (DECL_HAS_VALUE_EXPR_P (decl))
1590 return;
1592 /* All occurrences of this decl in final gimplified code will be
1593 replaced by indirection. Setting DECL_VALUE_EXPR does two
1594 things: First, it lets the rest of the gimplifier know what
1595 replacement to use. Second, it lets the debug info know
1596 where to find the value. */
1597 ptr_type = build_pointer_type (TREE_TYPE (decl));
1598 addr = create_tmp_var (ptr_type, get_name (decl));
1599 DECL_IGNORED_P (addr) = 0;
1600 t = build_fold_indirect_ref (addr);
1601 TREE_THIS_NOTRAP (t) = 1;
1602 SET_DECL_VALUE_EXPR (decl, t);
1603 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1605 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1606 max_int_size_in_bytes (TREE_TYPE (decl)));
1607 /* The call has been built for a variable-sized object. */
1608 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1609 t = fold_convert (ptr_type, t);
1610 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1612 gimplify_and_add (t, seq_p);
1615 /* A helper function to be called via walk_tree. Mark all labels under *TP
1616 as being forced. To be called for DECL_INITIAL of static variables. */
1618 static tree
1619 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1621 if (TYPE_P (*tp))
1622 *walk_subtrees = 0;
1623 if (TREE_CODE (*tp) == LABEL_DECL)
1625 FORCED_LABEL (*tp) = 1;
1626 cfun->has_forced_label_in_static = 1;
1629 return NULL_TREE;
1632 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1633 and initialization explicit. */
1635 static enum gimplify_status
1636 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1638 tree stmt = *stmt_p;
1639 tree decl = DECL_EXPR_DECL (stmt);
1641 *stmt_p = NULL_TREE;
1643 if (TREE_TYPE (decl) == error_mark_node)
1644 return GS_ERROR;
1646 if ((TREE_CODE (decl) == TYPE_DECL
1647 || VAR_P (decl))
1648 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1650 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1651 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1652 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1655 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1656 in case its size expressions contain problematic nodes like CALL_EXPR. */
1657 if (TREE_CODE (decl) == TYPE_DECL
1658 && DECL_ORIGINAL_TYPE (decl)
1659 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1661 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1662 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1663 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1666 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1668 tree init = DECL_INITIAL (decl);
1669 bool is_vla = false;
1671 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1672 || (!TREE_STATIC (decl)
1673 && flag_stack_check == GENERIC_STACK_CHECK
1674 && compare_tree_int (DECL_SIZE_UNIT (decl),
1675 STACK_CHECK_MAX_VAR_SIZE) > 0))
1677 gimplify_vla_decl (decl, seq_p);
1678 is_vla = true;
1681 if (asan_poisoned_variables
1682 && !is_vla
1683 && TREE_ADDRESSABLE (decl)
1684 && !TREE_STATIC (decl)
1685 && !DECL_HAS_VALUE_EXPR_P (decl)
1686 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1687 && dbg_cnt (asan_use_after_scope)
1688 && !gimplify_omp_ctxp)
1690 asan_poisoned_variables->add (decl);
1691 asan_poison_variable (decl, false, seq_p);
1692 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1693 gimplify_ctxp->live_switch_vars->add (decl);
1696 /* Some front ends do not explicitly declare all anonymous
1697 artificial variables. We compensate here by declaring the
1698 variables, though it would be better if the front ends would
1699 explicitly declare them. */
1700 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1701 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1702 gimple_add_tmp_var (decl);
1704 if (init && init != error_mark_node)
1706 if (!TREE_STATIC (decl))
1708 DECL_INITIAL (decl) = NULL_TREE;
1709 init = build2 (INIT_EXPR, void_type_node, decl, init);
1710 gimplify_and_add (init, seq_p);
1711 ggc_free (init);
1713 else
1714 /* We must still examine initializers for static variables
1715 as they may contain a label address. */
1716 walk_tree (&init, force_labels_r, NULL, NULL);
1720 return GS_ALL_DONE;
1723 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1724 and replacing the LOOP_EXPR with goto, but if the loop contains an
1725 EXIT_EXPR, we need to append a label for it to jump to. */
1727 static enum gimplify_status
1728 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1730 tree saved_label = gimplify_ctxp->exit_label;
1731 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1733 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1735 gimplify_ctxp->exit_label = NULL_TREE;
1737 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1739 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1741 if (gimplify_ctxp->exit_label)
1742 gimplify_seq_add_stmt (pre_p,
1743 gimple_build_label (gimplify_ctxp->exit_label));
1745 gimplify_ctxp->exit_label = saved_label;
1747 *expr_p = NULL;
1748 return GS_ALL_DONE;
1751 /* Gimplify a statement list onto a sequence. These may be created either
1752 by an enlightened front-end, or by shortcut_cond_expr. */
1754 static enum gimplify_status
1755 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1757 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1759 tree_stmt_iterator i = tsi_start (*expr_p);
1761 while (!tsi_end_p (i))
1763 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1764 tsi_delink (&i);
1767 if (temp)
1769 *expr_p = temp;
1770 return GS_OK;
1773 return GS_ALL_DONE;
1776 /* Callback for walk_gimple_seq. */
1778 static tree
1779 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1780 struct walk_stmt_info *wi)
1782 gimple *stmt = gsi_stmt (*gsi_p);
1784 *handled_ops_p = true;
1785 switch (gimple_code (stmt))
1787 case GIMPLE_TRY:
1788 /* A compiler-generated cleanup or a user-written try block.
1789 If it's empty, don't dive into it--that would result in
1790 worse location info. */
1791 if (gimple_try_eval (stmt) == NULL)
1793 wi->info = stmt;
1794 return integer_zero_node;
1796 /* Fall through. */
1797 case GIMPLE_BIND:
1798 case GIMPLE_CATCH:
1799 case GIMPLE_EH_FILTER:
1800 case GIMPLE_TRANSACTION:
1801 /* Walk the sub-statements. */
1802 *handled_ops_p = false;
1803 break;
1805 case GIMPLE_DEBUG:
1806 /* Ignore these. We may generate them before declarations that
1807 are never executed. If there's something to warn about,
1808 there will be non-debug stmts too, and we'll catch those. */
1809 break;
1811 case GIMPLE_CALL:
1812 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1814 *handled_ops_p = false;
1815 break;
1817 /* Fall through. */
1818 default:
1819 /* Save the first "real" statement (not a decl/lexical scope/...). */
1820 wi->info = stmt;
1821 return integer_zero_node;
1823 return NULL_TREE;
1826 /* Possibly warn about unreachable statements between switch's controlling
1827 expression and the first case. SEQ is the body of a switch expression. */
1829 static void
1830 maybe_warn_switch_unreachable (gimple_seq seq)
1832 if (!warn_switch_unreachable
1833 /* This warning doesn't play well with Fortran when optimizations
1834 are on. */
1835 || lang_GNU_Fortran ()
1836 || seq == NULL)
1837 return;
1839 struct walk_stmt_info wi;
1840 memset (&wi, 0, sizeof (wi));
1841 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1842 gimple *stmt = (gimple *) wi.info;
1844 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1846 if (gimple_code (stmt) == GIMPLE_GOTO
1847 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1848 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1849 /* Don't warn for compiler-generated gotos. These occur
1850 in Duff's devices, for example. */;
1851 else
1852 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1853 "statement will never be executed");
1858 /* A label entry that pairs label and a location. */
1859 struct label_entry
1861 tree label;
1862 location_t loc;
1865 /* Find LABEL in vector of label entries VEC. */
1867 static struct label_entry *
1868 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1870 unsigned int i;
1871 struct label_entry *l;
1873 FOR_EACH_VEC_ELT (*vec, i, l)
1874 if (l->label == label)
1875 return l;
1876 return NULL;
1879 /* Return true if LABEL, a LABEL_DECL, represents a case label
1880 in a vector of labels CASES. */
1882 static bool
1883 case_label_p (const vec<tree> *cases, tree label)
1885 unsigned int i;
1886 tree l;
1888 FOR_EACH_VEC_ELT (*cases, i, l)
1889 if (CASE_LABEL (l) == label)
1890 return true;
1891 return false;
1894 /* Find the last nondebug statement in a scope STMT. */
1896 static gimple *
1897 last_stmt_in_scope (gimple *stmt)
1899 if (!stmt)
1900 return NULL;
1902 switch (gimple_code (stmt))
1904 case GIMPLE_BIND:
1906 gbind *bind = as_a <gbind *> (stmt);
1907 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1908 return last_stmt_in_scope (stmt);
1911 case GIMPLE_TRY:
1913 gtry *try_stmt = as_a <gtry *> (stmt);
1914 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1915 gimple *last_eval = last_stmt_in_scope (stmt);
1916 if (gimple_stmt_may_fallthru (last_eval)
1917 && (last_eval == NULL
1918 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1919 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1921 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1922 return last_stmt_in_scope (stmt);
1924 else
1925 return last_eval;
1928 case GIMPLE_DEBUG:
1929 gcc_unreachable ();
1931 default:
1932 return stmt;
1936 /* Collect interesting labels in LABELS and return the statement preceding
1937 another case label, or a user-defined label. */
1939 static gimple *
1940 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1941 auto_vec <struct label_entry> *labels)
1943 gimple *prev = NULL;
1947 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1949 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1950 which starts on a GIMPLE_SWITCH and ends with a break label.
1951 Handle that as a single statement that can fall through. */
1952 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1953 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1954 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1955 if (last
1956 && gimple_code (first) == GIMPLE_SWITCH
1957 && gimple_code (last) == GIMPLE_LABEL)
1959 tree label = gimple_label_label (as_a <glabel *> (last));
1960 if (SWITCH_BREAK_LABEL_P (label))
1962 prev = bind;
1963 gsi_next (gsi_p);
1964 continue;
1968 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1969 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1971 /* Nested scope. Only look at the last statement of
1972 the innermost scope. */
1973 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1974 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1975 if (last)
1977 prev = last;
1978 /* It might be a label without a location. Use the
1979 location of the scope then. */
1980 if (!gimple_has_location (prev))
1981 gimple_set_location (prev, bind_loc);
1983 gsi_next (gsi_p);
1984 continue;
1987 /* Ifs are tricky. */
1988 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1990 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1991 tree false_lab = gimple_cond_false_label (cond_stmt);
1992 location_t if_loc = gimple_location (cond_stmt);
1994 /* If we have e.g.
1995 if (i > 1) goto <D.2259>; else goto D;
1996 we can't do much with the else-branch. */
1997 if (!DECL_ARTIFICIAL (false_lab))
1998 break;
2000 /* Go on until the false label, then one step back. */
2001 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2003 gimple *stmt = gsi_stmt (*gsi_p);
2004 if (gimple_code (stmt) == GIMPLE_LABEL
2005 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2006 break;
2009 /* Not found? Oops. */
2010 if (gsi_end_p (*gsi_p))
2011 break;
2013 struct label_entry l = { false_lab, if_loc };
2014 labels->safe_push (l);
2016 /* Go to the last statement of the then branch. */
2017 gsi_prev (gsi_p);
2019 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2020 <D.1759>:
2021 <stmt>;
2022 goto <D.1761>;
2023 <D.1760>:
2025 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2026 && !gimple_has_location (gsi_stmt (*gsi_p)))
2028 /* Look at the statement before, it might be
2029 attribute fallthrough, in which case don't warn. */
2030 gsi_prev (gsi_p);
2031 bool fallthru_before_dest
2032 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2033 gsi_next (gsi_p);
2034 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2035 if (!fallthru_before_dest)
2037 struct label_entry l = { goto_dest, if_loc };
2038 labels->safe_push (l);
2041 /* And move back. */
2042 gsi_next (gsi_p);
2045 /* Remember the last statement. Skip labels that are of no interest
2046 to us. */
2047 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2049 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2050 if (find_label_entry (labels, label))
2051 prev = gsi_stmt (*gsi_p);
2053 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2055 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2056 prev = gsi_stmt (*gsi_p);
2057 gsi_next (gsi_p);
2059 while (!gsi_end_p (*gsi_p)
2060 /* Stop if we find a case or a user-defined label. */
2061 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2062 || !gimple_has_location (gsi_stmt (*gsi_p))));
2064 return prev;
2067 /* Return true if the switch fallthough warning should occur. LABEL is
2068 the label statement that we're falling through to. */
2070 static bool
2071 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2073 gimple_stmt_iterator gsi = *gsi_p;
2075 /* Don't warn if the label is marked with a "falls through" comment. */
2076 if (FALLTHROUGH_LABEL_P (label))
2077 return false;
2079 /* Don't warn for non-case labels followed by a statement:
2080 case 0:
2081 foo ();
2082 label:
2083 bar ();
2084 as these are likely intentional. */
2085 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2087 tree l;
2088 while (!gsi_end_p (gsi)
2089 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2090 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2091 && !case_label_p (&gimplify_ctxp->case_labels, l))
2092 gsi_next_nondebug (&gsi);
2093 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2094 return false;
2097 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2098 immediately breaks. */
2099 gsi = *gsi_p;
2101 /* Skip all immediately following labels. */
2102 while (!gsi_end_p (gsi)
2103 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2104 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2105 gsi_next_nondebug (&gsi);
2107 /* { ... something; default:; } */
2108 if (gsi_end_p (gsi)
2109 /* { ... something; default: break; } or
2110 { ... something; default: goto L; } */
2111 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2112 /* { ... something; default: return; } */
2113 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2114 return false;
2116 return true;
2119 /* Callback for walk_gimple_seq. */
2121 static tree
2122 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2123 struct walk_stmt_info *)
2125 gimple *stmt = gsi_stmt (*gsi_p);
2127 *handled_ops_p = true;
2128 switch (gimple_code (stmt))
2130 case GIMPLE_TRY:
2131 case GIMPLE_BIND:
2132 case GIMPLE_CATCH:
2133 case GIMPLE_EH_FILTER:
2134 case GIMPLE_TRANSACTION:
2135 /* Walk the sub-statements. */
2136 *handled_ops_p = false;
2137 break;
2139 /* Find a sequence of form:
2141 GIMPLE_LABEL
2142 [...]
2143 <may fallthru stmt>
2144 GIMPLE_LABEL
2146 and possibly warn. */
2147 case GIMPLE_LABEL:
2149 /* Found a label. Skip all immediately following labels. */
2150 while (!gsi_end_p (*gsi_p)
2151 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2152 gsi_next_nondebug (gsi_p);
2154 /* There might be no more statements. */
2155 if (gsi_end_p (*gsi_p))
2156 return integer_zero_node;
2158 /* Vector of labels that fall through. */
2159 auto_vec <struct label_entry> labels;
2160 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2162 /* There might be no more statements. */
2163 if (gsi_end_p (*gsi_p))
2164 return integer_zero_node;
2166 gimple *next = gsi_stmt (*gsi_p);
2167 tree label;
2168 /* If what follows is a label, then we may have a fallthrough. */
2169 if (gimple_code (next) == GIMPLE_LABEL
2170 && gimple_has_location (next)
2171 && (label = gimple_label_label (as_a <glabel *> (next)))
2172 && prev != NULL)
2174 struct label_entry *l;
2175 bool warned_p = false;
2176 auto_diagnostic_group d;
2177 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2178 /* Quiet. */;
2179 else if (gimple_code (prev) == GIMPLE_LABEL
2180 && (label = gimple_label_label (as_a <glabel *> (prev)))
2181 && (l = find_label_entry (&labels, label)))
2182 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2183 "this statement may fall through");
2184 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2185 /* Try to be clever and don't warn when the statement
2186 can't actually fall through. */
2187 && gimple_stmt_may_fallthru (prev)
2188 && gimple_has_location (prev))
2189 warned_p = warning_at (gimple_location (prev),
2190 OPT_Wimplicit_fallthrough_,
2191 "this statement may fall through");
2192 if (warned_p)
2193 inform (gimple_location (next), "here");
2195 /* Mark this label as processed so as to prevent multiple
2196 warnings in nested switches. */
2197 FALLTHROUGH_LABEL_P (label) = true;
2199 /* So that next warn_implicit_fallthrough_r will start looking for
2200 a new sequence starting with this label. */
2201 gsi_prev (gsi_p);
2204 break;
2205 default:
2206 break;
2208 return NULL_TREE;
2211 /* Warn when a switch case falls through. */
2213 static void
2214 maybe_warn_implicit_fallthrough (gimple_seq seq)
2216 if (!warn_implicit_fallthrough)
2217 return;
2219 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2220 if (!(lang_GNU_C ()
2221 || lang_GNU_CXX ()
2222 || lang_GNU_OBJC ()))
2223 return;
2225 struct walk_stmt_info wi;
2226 memset (&wi, 0, sizeof (wi));
2227 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2230 /* Callback for walk_gimple_seq. */
2232 static tree
2233 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2234 struct walk_stmt_info *)
2236 gimple *stmt = gsi_stmt (*gsi_p);
2238 *handled_ops_p = true;
2239 switch (gimple_code (stmt))
2241 case GIMPLE_TRY:
2242 case GIMPLE_BIND:
2243 case GIMPLE_CATCH:
2244 case GIMPLE_EH_FILTER:
2245 case GIMPLE_TRANSACTION:
2246 /* Walk the sub-statements. */
2247 *handled_ops_p = false;
2248 break;
2249 case GIMPLE_CALL:
2250 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2252 gsi_remove (gsi_p, true);
2253 if (gsi_end_p (*gsi_p))
2254 return integer_zero_node;
2256 bool found = false;
2257 location_t loc = gimple_location (stmt);
2259 gimple_stmt_iterator gsi2 = *gsi_p;
2260 stmt = gsi_stmt (gsi2);
2261 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2263 /* Go on until the artificial label. */
2264 tree goto_dest = gimple_goto_dest (stmt);
2265 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2267 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2268 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2269 == goto_dest)
2270 break;
2273 /* Not found? Stop. */
2274 if (gsi_end_p (gsi2))
2275 break;
2277 /* Look one past it. */
2278 gsi_next (&gsi2);
2281 /* We're looking for a case label or default label here. */
2282 while (!gsi_end_p (gsi2))
2284 stmt = gsi_stmt (gsi2);
2285 if (gimple_code (stmt) == GIMPLE_LABEL)
2287 tree label = gimple_label_label (as_a <glabel *> (stmt));
2288 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2290 found = true;
2291 break;
2294 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2296 else if (!is_gimple_debug (stmt))
2297 /* Anything else is not expected. */
2298 break;
2299 gsi_next (&gsi2);
2301 if (!found)
2302 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2303 "a case label or default label");
2305 break;
2306 default:
2307 break;
2309 return NULL_TREE;
2312 /* Expand all FALLTHROUGH () calls in SEQ. */
2314 static void
2315 expand_FALLTHROUGH (gimple_seq *seq_p)
2317 struct walk_stmt_info wi;
2318 memset (&wi, 0, sizeof (wi));
2319 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2323 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2324 branch to. */
2326 static enum gimplify_status
2327 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2329 tree switch_expr = *expr_p;
2330 gimple_seq switch_body_seq = NULL;
2331 enum gimplify_status ret;
2332 tree index_type = TREE_TYPE (switch_expr);
2333 if (index_type == NULL_TREE)
2334 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2336 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2337 fb_rvalue);
2338 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2339 return ret;
2341 if (SWITCH_BODY (switch_expr))
2343 vec<tree> labels;
2344 vec<tree> saved_labels;
2345 hash_set<tree> *saved_live_switch_vars = NULL;
2346 tree default_case = NULL_TREE;
2347 gswitch *switch_stmt;
2349 /* Save old labels, get new ones from body, then restore the old
2350 labels. Save all the things from the switch body to append after. */
2351 saved_labels = gimplify_ctxp->case_labels;
2352 gimplify_ctxp->case_labels.create (8);
2354 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2355 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2356 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2357 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2358 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2359 else
2360 gimplify_ctxp->live_switch_vars = NULL;
2362 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2363 gimplify_ctxp->in_switch_expr = true;
2365 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2367 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2368 maybe_warn_switch_unreachable (switch_body_seq);
2369 maybe_warn_implicit_fallthrough (switch_body_seq);
2370 /* Only do this for the outermost GIMPLE_SWITCH. */
2371 if (!gimplify_ctxp->in_switch_expr)
2372 expand_FALLTHROUGH (&switch_body_seq);
2374 labels = gimplify_ctxp->case_labels;
2375 gimplify_ctxp->case_labels = saved_labels;
2377 if (gimplify_ctxp->live_switch_vars)
2379 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2380 delete gimplify_ctxp->live_switch_vars;
2382 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2384 preprocess_case_label_vec_for_gimple (labels, index_type,
2385 &default_case);
2387 bool add_bind = false;
2388 if (!default_case)
2390 glabel *new_default;
2392 default_case
2393 = build_case_label (NULL_TREE, NULL_TREE,
2394 create_artificial_label (UNKNOWN_LOCATION));
2395 if (old_in_switch_expr)
2397 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2398 add_bind = true;
2400 new_default = gimple_build_label (CASE_LABEL (default_case));
2401 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2403 else if (old_in_switch_expr)
2405 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2406 if (last && gimple_code (last) == GIMPLE_LABEL)
2408 tree label = gimple_label_label (as_a <glabel *> (last));
2409 if (SWITCH_BREAK_LABEL_P (label))
2410 add_bind = true;
2414 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2415 default_case, labels);
2416 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2417 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2418 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2419 so that we can easily find the start and end of the switch
2420 statement. */
2421 if (add_bind)
2423 gimple_seq bind_body = NULL;
2424 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2425 gimple_seq_add_seq (&bind_body, switch_body_seq);
2426 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2427 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2428 gimplify_seq_add_stmt (pre_p, bind);
2430 else
2432 gimplify_seq_add_stmt (pre_p, switch_stmt);
2433 gimplify_seq_add_seq (pre_p, switch_body_seq);
2435 labels.release ();
2437 else
2438 gcc_unreachable ();
2440 return GS_ALL_DONE;
2443 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2445 static enum gimplify_status
2446 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2448 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2449 == current_function_decl);
2451 tree label = LABEL_EXPR_LABEL (*expr_p);
2452 glabel *label_stmt = gimple_build_label (label);
2453 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2454 gimplify_seq_add_stmt (pre_p, label_stmt);
2456 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2457 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2458 NOT_TAKEN));
2459 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2460 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2461 TAKEN));
2463 return GS_ALL_DONE;
2466 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2468 static enum gimplify_status
2469 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2471 struct gimplify_ctx *ctxp;
2472 glabel *label_stmt;
2474 /* Invalid programs can play Duff's Device type games with, for example,
2475 #pragma omp parallel. At least in the C front end, we don't
2476 detect such invalid branches until after gimplification, in the
2477 diagnose_omp_blocks pass. */
2478 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2479 if (ctxp->case_labels.exists ())
2480 break;
2482 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2483 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2484 ctxp->case_labels.safe_push (*expr_p);
2485 gimplify_seq_add_stmt (pre_p, label_stmt);
2487 return GS_ALL_DONE;
2490 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2491 if necessary. */
2493 tree
2494 build_and_jump (tree *label_p)
2496 if (label_p == NULL)
2497 /* If there's nowhere to jump, just fall through. */
2498 return NULL_TREE;
2500 if (*label_p == NULL_TREE)
2502 tree label = create_artificial_label (UNKNOWN_LOCATION);
2503 *label_p = label;
2506 return build1 (GOTO_EXPR, void_type_node, *label_p);
2509 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2510 This also involves building a label to jump to and communicating it to
2511 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2513 static enum gimplify_status
2514 gimplify_exit_expr (tree *expr_p)
2516 tree cond = TREE_OPERAND (*expr_p, 0);
2517 tree expr;
2519 expr = build_and_jump (&gimplify_ctxp->exit_label);
2520 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2521 *expr_p = expr;
2523 return GS_OK;
2526 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2527 different from its canonical type, wrap the whole thing inside a
2528 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2529 type.
2531 The canonical type of a COMPONENT_REF is the type of the field being
2532 referenced--unless the field is a bit-field which can be read directly
2533 in a smaller mode, in which case the canonical type is the
2534 sign-appropriate type corresponding to that mode. */
2536 static void
2537 canonicalize_component_ref (tree *expr_p)
2539 tree expr = *expr_p;
2540 tree type;
2542 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2544 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2545 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2546 else
2547 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2549 /* One could argue that all the stuff below is not necessary for
2550 the non-bitfield case and declare it a FE error if type
2551 adjustment would be needed. */
2552 if (TREE_TYPE (expr) != type)
2554 #ifdef ENABLE_TYPES_CHECKING
2555 tree old_type = TREE_TYPE (expr);
2556 #endif
2557 int type_quals;
2559 /* We need to preserve qualifiers and propagate them from
2560 operand 0. */
2561 type_quals = TYPE_QUALS (type)
2562 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2563 if (TYPE_QUALS (type) != type_quals)
2564 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2566 /* Set the type of the COMPONENT_REF to the underlying type. */
2567 TREE_TYPE (expr) = type;
2569 #ifdef ENABLE_TYPES_CHECKING
2570 /* It is now a FE error, if the conversion from the canonical
2571 type to the original expression type is not useless. */
2572 gcc_assert (useless_type_conversion_p (old_type, type));
2573 #endif
2577 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2578 to foo, embed that change in the ADDR_EXPR by converting
2579 T array[U];
2580 (T *)&array
2582 &array[L]
2583 where L is the lower bound. For simplicity, only do this for constant
2584 lower bound.
2585 The constraint is that the type of &array[L] is trivially convertible
2586 to T *. */
2588 static void
2589 canonicalize_addr_expr (tree *expr_p)
2591 tree expr = *expr_p;
2592 tree addr_expr = TREE_OPERAND (expr, 0);
2593 tree datype, ddatype, pddatype;
2595 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2596 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2597 || TREE_CODE (addr_expr) != ADDR_EXPR)
2598 return;
2600 /* The addr_expr type should be a pointer to an array. */
2601 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2602 if (TREE_CODE (datype) != ARRAY_TYPE)
2603 return;
2605 /* The pointer to element type shall be trivially convertible to
2606 the expression pointer type. */
2607 ddatype = TREE_TYPE (datype);
2608 pddatype = build_pointer_type (ddatype);
2609 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2610 pddatype))
2611 return;
2613 /* The lower bound and element sizes must be constant. */
2614 if (!TYPE_SIZE_UNIT (ddatype)
2615 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2616 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2617 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2618 return;
2620 /* All checks succeeded. Build a new node to merge the cast. */
2621 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2622 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2623 NULL_TREE, NULL_TREE);
2624 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2626 /* We can have stripped a required restrict qualifier above. */
2627 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2628 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2631 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2632 underneath as appropriate. */
2634 static enum gimplify_status
2635 gimplify_conversion (tree *expr_p)
2637 location_t loc = EXPR_LOCATION (*expr_p);
2638 gcc_assert (CONVERT_EXPR_P (*expr_p));
2640 /* Then strip away all but the outermost conversion. */
2641 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2643 /* And remove the outermost conversion if it's useless. */
2644 if (tree_ssa_useless_type_conversion (*expr_p))
2645 *expr_p = TREE_OPERAND (*expr_p, 0);
2647 /* If we still have a conversion at the toplevel,
2648 then canonicalize some constructs. */
2649 if (CONVERT_EXPR_P (*expr_p))
2651 tree sub = TREE_OPERAND (*expr_p, 0);
2653 /* If a NOP conversion is changing the type of a COMPONENT_REF
2654 expression, then canonicalize its type now in order to expose more
2655 redundant conversions. */
2656 if (TREE_CODE (sub) == COMPONENT_REF)
2657 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2659 /* If a NOP conversion is changing a pointer to array of foo
2660 to a pointer to foo, embed that change in the ADDR_EXPR. */
2661 else if (TREE_CODE (sub) == ADDR_EXPR)
2662 canonicalize_addr_expr (expr_p);
2665 /* If we have a conversion to a non-register type force the
2666 use of a VIEW_CONVERT_EXPR instead. */
2667 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2668 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2669 TREE_OPERAND (*expr_p, 0));
2671 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2672 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2673 TREE_SET_CODE (*expr_p, NOP_EXPR);
2675 return GS_OK;
2678 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2679 DECL_VALUE_EXPR, and it's worth re-examining things. */
2681 static enum gimplify_status
2682 gimplify_var_or_parm_decl (tree *expr_p)
2684 tree decl = *expr_p;
2686 /* ??? If this is a local variable, and it has not been seen in any
2687 outer BIND_EXPR, then it's probably the result of a duplicate
2688 declaration, for which we've already issued an error. It would
2689 be really nice if the front end wouldn't leak these at all.
2690 Currently the only known culprit is C++ destructors, as seen
2691 in g++.old-deja/g++.jason/binding.C. */
2692 if (VAR_P (decl)
2693 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2694 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2695 && decl_function_context (decl) == current_function_decl)
2697 gcc_assert (seen_error ());
2698 return GS_ERROR;
2701 /* When within an OMP context, notice uses of variables. */
2702 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2703 return GS_ALL_DONE;
2705 /* If the decl is an alias for another expression, substitute it now. */
2706 if (DECL_HAS_VALUE_EXPR_P (decl))
2708 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
2709 return GS_OK;
2712 return GS_ALL_DONE;
2715 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2717 static void
2718 recalculate_side_effects (tree t)
2720 enum tree_code code = TREE_CODE (t);
2721 int len = TREE_OPERAND_LENGTH (t);
2722 int i;
2724 switch (TREE_CODE_CLASS (code))
2726 case tcc_expression:
2727 switch (code)
2729 case INIT_EXPR:
2730 case MODIFY_EXPR:
2731 case VA_ARG_EXPR:
2732 case PREDECREMENT_EXPR:
2733 case PREINCREMENT_EXPR:
2734 case POSTDECREMENT_EXPR:
2735 case POSTINCREMENT_EXPR:
2736 /* All of these have side-effects, no matter what their
2737 operands are. */
2738 return;
2740 default:
2741 break;
2743 /* Fall through. */
2745 case tcc_comparison: /* a comparison expression */
2746 case tcc_unary: /* a unary arithmetic expression */
2747 case tcc_binary: /* a binary arithmetic expression */
2748 case tcc_reference: /* a reference */
2749 case tcc_vl_exp: /* a function call */
2750 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2751 for (i = 0; i < len; ++i)
2753 tree op = TREE_OPERAND (t, i);
2754 if (op && TREE_SIDE_EFFECTS (op))
2755 TREE_SIDE_EFFECTS (t) = 1;
2757 break;
2759 case tcc_constant:
2760 /* No side-effects. */
2761 return;
2763 default:
2764 gcc_unreachable ();
2768 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2769 node *EXPR_P.
2771 compound_lval
2772 : min_lval '[' val ']'
2773 | min_lval '.' ID
2774 | compound_lval '[' val ']'
2775 | compound_lval '.' ID
2777 This is not part of the original SIMPLE definition, which separates
2778 array and member references, but it seems reasonable to handle them
2779 together. Also, this way we don't run into problems with union
2780 aliasing; gcc requires that for accesses through a union to alias, the
2781 union reference must be explicit, which was not always the case when we
2782 were splitting up array and member refs.
2784 PRE_P points to the sequence where side effects that must happen before
2785 *EXPR_P should be stored.
2787 POST_P points to the sequence where side effects that must happen after
2788 *EXPR_P should be stored. */
2790 static enum gimplify_status
2791 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2792 fallback_t fallback)
2794 tree *p;
2795 enum gimplify_status ret = GS_ALL_DONE, tret;
2796 int i;
2797 location_t loc = EXPR_LOCATION (*expr_p);
2798 tree expr = *expr_p;
2800 /* Create a stack of the subexpressions so later we can walk them in
2801 order from inner to outer. */
2802 auto_vec<tree, 10> expr_stack;
2804 /* We can handle anything that get_inner_reference can deal with. */
2805 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2807 restart:
2808 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2809 if (TREE_CODE (*p) == INDIRECT_REF)
2810 *p = fold_indirect_ref_loc (loc, *p);
2812 if (handled_component_p (*p))
2814 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2815 additional COMPONENT_REFs. */
2816 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2817 && gimplify_var_or_parm_decl (p) == GS_OK)
2818 goto restart;
2819 else
2820 break;
2822 expr_stack.safe_push (*p);
2825 gcc_assert (expr_stack.length ());
2827 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2828 walked through and P points to the innermost expression.
2830 Java requires that we elaborated nodes in source order. That
2831 means we must gimplify the inner expression followed by each of
2832 the indices, in order. But we can't gimplify the inner
2833 expression until we deal with any variable bounds, sizes, or
2834 positions in order to deal with PLACEHOLDER_EXPRs.
2836 So we do this in three steps. First we deal with the annotations
2837 for any variables in the components, then we gimplify the base,
2838 then we gimplify any indices, from left to right. */
2839 for (i = expr_stack.length () - 1; i >= 0; i--)
2841 tree t = expr_stack[i];
2843 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2845 /* Gimplify the low bound and element type size and put them into
2846 the ARRAY_REF. If these values are set, they have already been
2847 gimplified. */
2848 if (TREE_OPERAND (t, 2) == NULL_TREE)
2850 tree low = unshare_expr (array_ref_low_bound (t));
2851 if (!is_gimple_min_invariant (low))
2853 TREE_OPERAND (t, 2) = low;
2854 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2855 post_p, is_gimple_reg,
2856 fb_rvalue);
2857 ret = MIN (ret, tret);
2860 else
2862 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2863 is_gimple_reg, fb_rvalue);
2864 ret = MIN (ret, tret);
2867 if (TREE_OPERAND (t, 3) == NULL_TREE)
2869 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2870 tree elmt_size = unshare_expr (array_ref_element_size (t));
2871 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2873 /* Divide the element size by the alignment of the element
2874 type (above). */
2875 elmt_size
2876 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2878 if (!is_gimple_min_invariant (elmt_size))
2880 TREE_OPERAND (t, 3) = elmt_size;
2881 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2882 post_p, is_gimple_reg,
2883 fb_rvalue);
2884 ret = MIN (ret, tret);
2887 else
2889 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2890 is_gimple_reg, fb_rvalue);
2891 ret = MIN (ret, tret);
2894 else if (TREE_CODE (t) == COMPONENT_REF)
2896 /* Set the field offset into T and gimplify it. */
2897 if (TREE_OPERAND (t, 2) == NULL_TREE)
2899 tree offset = unshare_expr (component_ref_field_offset (t));
2900 tree field = TREE_OPERAND (t, 1);
2901 tree factor
2902 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2904 /* Divide the offset by its alignment. */
2905 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2907 if (!is_gimple_min_invariant (offset))
2909 TREE_OPERAND (t, 2) = offset;
2910 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2911 post_p, is_gimple_reg,
2912 fb_rvalue);
2913 ret = MIN (ret, tret);
2916 else
2918 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2919 is_gimple_reg, fb_rvalue);
2920 ret = MIN (ret, tret);
2925 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2926 so as to match the min_lval predicate. Failure to do so may result
2927 in the creation of large aggregate temporaries. */
2928 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2929 fallback | fb_lvalue);
2930 ret = MIN (ret, tret);
2932 /* And finally, the indices and operands of ARRAY_REF. During this
2933 loop we also remove any useless conversions. */
2934 for (; expr_stack.length () > 0; )
2936 tree t = expr_stack.pop ();
2938 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2940 /* Gimplify the dimension. */
2941 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2943 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2944 is_gimple_val, fb_rvalue);
2945 ret = MIN (ret, tret);
2949 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2951 /* The innermost expression P may have originally had
2952 TREE_SIDE_EFFECTS set which would have caused all the outer
2953 expressions in *EXPR_P leading to P to also have had
2954 TREE_SIDE_EFFECTS set. */
2955 recalculate_side_effects (t);
2958 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2959 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2961 canonicalize_component_ref (expr_p);
2964 expr_stack.release ();
2966 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2968 return ret;
2971 /* Gimplify the self modifying expression pointed to by EXPR_P
2972 (++, --, +=, -=).
2974 PRE_P points to the list where side effects that must happen before
2975 *EXPR_P should be stored.
2977 POST_P points to the list where side effects that must happen after
2978 *EXPR_P should be stored.
2980 WANT_VALUE is nonzero iff we want to use the value of this expression
2981 in another expression.
2983 ARITH_TYPE is the type the computation should be performed in. */
2985 enum gimplify_status
2986 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2987 bool want_value, tree arith_type)
2989 enum tree_code code;
2990 tree lhs, lvalue, rhs, t1;
2991 gimple_seq post = NULL, *orig_post_p = post_p;
2992 bool postfix;
2993 enum tree_code arith_code;
2994 enum gimplify_status ret;
2995 location_t loc = EXPR_LOCATION (*expr_p);
2997 code = TREE_CODE (*expr_p);
2999 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3000 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3002 /* Prefix or postfix? */
3003 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3004 /* Faster to treat as prefix if result is not used. */
3005 postfix = want_value;
3006 else
3007 postfix = false;
3009 /* For postfix, make sure the inner expression's post side effects
3010 are executed after side effects from this expression. */
3011 if (postfix)
3012 post_p = &post;
3014 /* Add or subtract? */
3015 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3016 arith_code = PLUS_EXPR;
3017 else
3018 arith_code = MINUS_EXPR;
3020 /* Gimplify the LHS into a GIMPLE lvalue. */
3021 lvalue = TREE_OPERAND (*expr_p, 0);
3022 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3023 if (ret == GS_ERROR)
3024 return ret;
3026 /* Extract the operands to the arithmetic operation. */
3027 lhs = lvalue;
3028 rhs = TREE_OPERAND (*expr_p, 1);
3030 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3031 that as the result value and in the postqueue operation. */
3032 if (postfix)
3034 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3035 if (ret == GS_ERROR)
3036 return ret;
3038 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3041 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3042 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3044 rhs = convert_to_ptrofftype_loc (loc, rhs);
3045 if (arith_code == MINUS_EXPR)
3046 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3047 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3049 else
3050 t1 = fold_convert (TREE_TYPE (*expr_p),
3051 fold_build2 (arith_code, arith_type,
3052 fold_convert (arith_type, lhs),
3053 fold_convert (arith_type, rhs)));
3055 if (postfix)
3057 gimplify_assign (lvalue, t1, pre_p);
3058 gimplify_seq_add_seq (orig_post_p, post);
3059 *expr_p = lhs;
3060 return GS_ALL_DONE;
3062 else
3064 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3065 return GS_OK;
3069 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3071 static void
3072 maybe_with_size_expr (tree *expr_p)
3074 tree expr = *expr_p;
3075 tree type = TREE_TYPE (expr);
3076 tree size;
3078 /* If we've already wrapped this or the type is error_mark_node, we can't do
3079 anything. */
3080 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3081 || type == error_mark_node)
3082 return;
3084 /* If the size isn't known or is a constant, we have nothing to do. */
3085 size = TYPE_SIZE_UNIT (type);
3086 if (!size || poly_int_tree_p (size))
3087 return;
3089 /* Otherwise, make a WITH_SIZE_EXPR. */
3090 size = unshare_expr (size);
3091 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3092 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3095 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3096 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3097 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3098 gimplified to an SSA name. */
3100 enum gimplify_status
3101 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3102 bool allow_ssa)
3104 bool (*test) (tree);
3105 fallback_t fb;
3107 /* In general, we allow lvalues for function arguments to avoid
3108 extra overhead of copying large aggregates out of even larger
3109 aggregates into temporaries only to copy the temporaries to
3110 the argument list. Make optimizers happy by pulling out to
3111 temporaries those types that fit in registers. */
3112 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3113 test = is_gimple_val, fb = fb_rvalue;
3114 else
3116 test = is_gimple_lvalue, fb = fb_either;
3117 /* Also strip a TARGET_EXPR that would force an extra copy. */
3118 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3120 tree init = TARGET_EXPR_INITIAL (*arg_p);
3121 if (init
3122 && !VOID_TYPE_P (TREE_TYPE (init)))
3123 *arg_p = init;
3127 /* If this is a variable sized type, we must remember the size. */
3128 maybe_with_size_expr (arg_p);
3130 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3131 /* Make sure arguments have the same location as the function call
3132 itself. */
3133 protected_set_expr_location (*arg_p, call_location);
3135 /* There is a sequence point before a function call. Side effects in
3136 the argument list must occur before the actual call. So, when
3137 gimplifying arguments, force gimplify_expr to use an internal
3138 post queue which is then appended to the end of PRE_P. */
3139 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3142 /* Don't fold inside offloading or taskreg regions: it can break code by
3143 adding decl references that weren't in the source. We'll do it during
3144 omplower pass instead. */
3146 static bool
3147 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3149 struct gimplify_omp_ctx *ctx;
3150 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3151 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3152 return false;
3153 return fold_stmt (gsi);
3156 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3157 WANT_VALUE is true if the result of the call is desired. */
3159 static enum gimplify_status
3160 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3162 tree fndecl, parms, p, fnptrtype;
3163 enum gimplify_status ret;
3164 int i, nargs;
3165 gcall *call;
3166 bool builtin_va_start_p = false;
3167 location_t loc = EXPR_LOCATION (*expr_p);
3169 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3171 /* For reliable diagnostics during inlining, it is necessary that
3172 every call_expr be annotated with file and line. */
3173 if (! EXPR_HAS_LOCATION (*expr_p))
3174 SET_EXPR_LOCATION (*expr_p, input_location);
3176 /* Gimplify internal functions created in the FEs. */
3177 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3179 if (want_value)
3180 return GS_ALL_DONE;
3182 nargs = call_expr_nargs (*expr_p);
3183 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3184 auto_vec<tree> vargs (nargs);
3186 for (i = 0; i < nargs; i++)
3188 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3189 EXPR_LOCATION (*expr_p));
3190 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3193 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3194 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3195 gimplify_seq_add_stmt (pre_p, call);
3196 return GS_ALL_DONE;
3199 /* This may be a call to a builtin function.
3201 Builtin function calls may be transformed into different
3202 (and more efficient) builtin function calls under certain
3203 circumstances. Unfortunately, gimplification can muck things
3204 up enough that the builtin expanders are not aware that certain
3205 transformations are still valid.
3207 So we attempt transformation/gimplification of the call before
3208 we gimplify the CALL_EXPR. At this time we do not manage to
3209 transform all calls in the same manner as the expanders do, but
3210 we do transform most of them. */
3211 fndecl = get_callee_fndecl (*expr_p);
3212 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
3213 switch (DECL_FUNCTION_CODE (fndecl))
3215 CASE_BUILT_IN_ALLOCA:
3216 /* If the call has been built for a variable-sized object, then we
3217 want to restore the stack level when the enclosing BIND_EXPR is
3218 exited to reclaim the allocated space; otherwise, we precisely
3219 need to do the opposite and preserve the latest stack level. */
3220 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3221 gimplify_ctxp->save_stack = true;
3222 else
3223 gimplify_ctxp->keep_stack = true;
3224 break;
3226 case BUILT_IN_VA_START:
3228 builtin_va_start_p = TRUE;
3229 if (call_expr_nargs (*expr_p) < 2)
3231 error ("too few arguments to function %<va_start%>");
3232 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3233 return GS_OK;
3236 if (fold_builtin_next_arg (*expr_p, true))
3238 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3239 return GS_OK;
3241 break;
3244 default:
3247 if (fndecl && fndecl_built_in_p (fndecl))
3249 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3250 if (new_tree && new_tree != *expr_p)
3252 /* There was a transformation of this call which computes the
3253 same value, but in a more efficient way. Return and try
3254 again. */
3255 *expr_p = new_tree;
3256 return GS_OK;
3260 /* Remember the original function pointer type. */
3261 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3263 /* There is a sequence point before the call, so any side effects in
3264 the calling expression must occur before the actual call. Force
3265 gimplify_expr to use an internal post queue. */
3266 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3267 is_gimple_call_addr, fb_rvalue);
3269 nargs = call_expr_nargs (*expr_p);
3271 /* Get argument types for verification. */
3272 fndecl = get_callee_fndecl (*expr_p);
3273 parms = NULL_TREE;
3274 if (fndecl)
3275 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3276 else
3277 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3279 if (fndecl && DECL_ARGUMENTS (fndecl))
3280 p = DECL_ARGUMENTS (fndecl);
3281 else if (parms)
3282 p = parms;
3283 else
3284 p = NULL_TREE;
3285 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3288 /* If the last argument is __builtin_va_arg_pack () and it is not
3289 passed as a named argument, decrease the number of CALL_EXPR
3290 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3291 if (!p
3292 && i < nargs
3293 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3295 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3296 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3298 if (last_arg_fndecl
3299 && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
3301 tree call = *expr_p;
3303 --nargs;
3304 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3305 CALL_EXPR_FN (call),
3306 nargs, CALL_EXPR_ARGP (call));
3308 /* Copy all CALL_EXPR flags, location and block, except
3309 CALL_EXPR_VA_ARG_PACK flag. */
3310 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3311 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3312 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3313 = CALL_EXPR_RETURN_SLOT_OPT (call);
3314 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3315 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3317 /* Set CALL_EXPR_VA_ARG_PACK. */
3318 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3322 /* If the call returns twice then after building the CFG the call
3323 argument computations will no longer dominate the call because
3324 we add an abnormal incoming edge to the call. So do not use SSA
3325 vars there. */
3326 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3328 /* Gimplify the function arguments. */
3329 if (nargs > 0)
3331 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3332 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3333 PUSH_ARGS_REVERSED ? i-- : i++)
3335 enum gimplify_status t;
3337 /* Avoid gimplifying the second argument to va_start, which needs to
3338 be the plain PARM_DECL. */
3339 if ((i != 1) || !builtin_va_start_p)
3341 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3342 EXPR_LOCATION (*expr_p), ! returns_twice);
3344 if (t == GS_ERROR)
3345 ret = GS_ERROR;
3350 /* Gimplify the static chain. */
3351 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3353 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3354 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3355 else
3357 enum gimplify_status t;
3358 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3359 EXPR_LOCATION (*expr_p), ! returns_twice);
3360 if (t == GS_ERROR)
3361 ret = GS_ERROR;
3365 /* Verify the function result. */
3366 if (want_value && fndecl
3367 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3369 error_at (loc, "using result of function returning %<void%>");
3370 ret = GS_ERROR;
3373 /* Try this again in case gimplification exposed something. */
3374 if (ret != GS_ERROR)
3376 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3378 if (new_tree && new_tree != *expr_p)
3380 /* There was a transformation of this call which computes the
3381 same value, but in a more efficient way. Return and try
3382 again. */
3383 *expr_p = new_tree;
3384 return GS_OK;
3387 else
3389 *expr_p = error_mark_node;
3390 return GS_ERROR;
3393 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3394 decl. This allows us to eliminate redundant or useless
3395 calls to "const" functions. */
3396 if (TREE_CODE (*expr_p) == CALL_EXPR)
3398 int flags = call_expr_flags (*expr_p);
3399 if (flags & (ECF_CONST | ECF_PURE)
3400 /* An infinite loop is considered a side effect. */
3401 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3402 TREE_SIDE_EFFECTS (*expr_p) = 0;
3405 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3406 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3407 form and delegate the creation of a GIMPLE_CALL to
3408 gimplify_modify_expr. This is always possible because when
3409 WANT_VALUE is true, the caller wants the result of this call into
3410 a temporary, which means that we will emit an INIT_EXPR in
3411 internal_get_tmp_var which will then be handled by
3412 gimplify_modify_expr. */
3413 if (!want_value)
3415 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3416 have to do is replicate it as a GIMPLE_CALL tuple. */
3417 gimple_stmt_iterator gsi;
3418 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3419 notice_special_calls (call);
3420 gimplify_seq_add_stmt (pre_p, call);
3421 gsi = gsi_last (*pre_p);
3422 maybe_fold_stmt (&gsi);
3423 *expr_p = NULL_TREE;
3425 else
3426 /* Remember the original function type. */
3427 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3428 CALL_EXPR_FN (*expr_p));
3430 return ret;
3433 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3434 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3436 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3437 condition is true or false, respectively. If null, we should generate
3438 our own to skip over the evaluation of this specific expression.
3440 LOCUS is the source location of the COND_EXPR.
3442 This function is the tree equivalent of do_jump.
3444 shortcut_cond_r should only be called by shortcut_cond_expr. */
3446 static tree
3447 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3448 location_t locus)
3450 tree local_label = NULL_TREE;
3451 tree t, expr = NULL;
3453 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3454 retain the shortcut semantics. Just insert the gotos here;
3455 shortcut_cond_expr will append the real blocks later. */
3456 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3458 location_t new_locus;
3460 /* Turn if (a && b) into
3462 if (a); else goto no;
3463 if (b) goto yes; else goto no;
3464 (no:) */
3466 if (false_label_p == NULL)
3467 false_label_p = &local_label;
3469 /* Keep the original source location on the first 'if'. */
3470 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3471 append_to_statement_list (t, &expr);
3473 /* Set the source location of the && on the second 'if'. */
3474 new_locus = rexpr_location (pred, locus);
3475 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3476 new_locus);
3477 append_to_statement_list (t, &expr);
3479 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3481 location_t new_locus;
3483 /* Turn if (a || b) into
3485 if (a) goto yes;
3486 if (b) goto yes; else goto no;
3487 (yes:) */
3489 if (true_label_p == NULL)
3490 true_label_p = &local_label;
3492 /* Keep the original source location on the first 'if'. */
3493 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3494 append_to_statement_list (t, &expr);
3496 /* Set the source location of the || on the second 'if'. */
3497 new_locus = rexpr_location (pred, locus);
3498 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3499 new_locus);
3500 append_to_statement_list (t, &expr);
3502 else if (TREE_CODE (pred) == COND_EXPR
3503 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3504 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3506 location_t new_locus;
3508 /* As long as we're messing with gotos, turn if (a ? b : c) into
3509 if (a)
3510 if (b) goto yes; else goto no;
3511 else
3512 if (c) goto yes; else goto no;
3514 Don't do this if one of the arms has void type, which can happen
3515 in C++ when the arm is throw. */
3517 /* Keep the original source location on the first 'if'. Set the source
3518 location of the ? on the second 'if'. */
3519 new_locus = rexpr_location (pred, locus);
3520 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3521 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3522 false_label_p, locus),
3523 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3524 false_label_p, new_locus));
3526 else
3528 expr = build3 (COND_EXPR, void_type_node, pred,
3529 build_and_jump (true_label_p),
3530 build_and_jump (false_label_p));
3531 SET_EXPR_LOCATION (expr, locus);
3534 if (local_label)
3536 t = build1 (LABEL_EXPR, void_type_node, local_label);
3537 append_to_statement_list (t, &expr);
3540 return expr;
3543 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3544 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3545 statement, if it is the last one. Otherwise, return NULL. */
3547 static tree
3548 find_goto (tree expr)
3550 if (!expr)
3551 return NULL_TREE;
3553 if (TREE_CODE (expr) == GOTO_EXPR)
3554 return expr;
3556 if (TREE_CODE (expr) != STATEMENT_LIST)
3557 return NULL_TREE;
3559 tree_stmt_iterator i = tsi_start (expr);
3561 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3562 tsi_next (&i);
3564 if (!tsi_one_before_end_p (i))
3565 return NULL_TREE;
3567 return find_goto (tsi_stmt (i));
3570 /* Same as find_goto, except that it returns NULL if the destination
3571 is not a LABEL_DECL. */
3573 static inline tree
3574 find_goto_label (tree expr)
3576 tree dest = find_goto (expr);
3577 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3578 return dest;
3579 return NULL_TREE;
3582 /* Given a conditional expression EXPR with short-circuit boolean
3583 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3584 predicate apart into the equivalent sequence of conditionals. */
3586 static tree
3587 shortcut_cond_expr (tree expr)
3589 tree pred = TREE_OPERAND (expr, 0);
3590 tree then_ = TREE_OPERAND (expr, 1);
3591 tree else_ = TREE_OPERAND (expr, 2);
3592 tree true_label, false_label, end_label, t;
3593 tree *true_label_p;
3594 tree *false_label_p;
3595 bool emit_end, emit_false, jump_over_else;
3596 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3597 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3599 /* First do simple transformations. */
3600 if (!else_se)
3602 /* If there is no 'else', turn
3603 if (a && b) then c
3604 into
3605 if (a) if (b) then c. */
3606 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3608 /* Keep the original source location on the first 'if'. */
3609 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3610 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3611 /* Set the source location of the && on the second 'if'. */
3612 if (rexpr_has_location (pred))
3613 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3614 then_ = shortcut_cond_expr (expr);
3615 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3616 pred = TREE_OPERAND (pred, 0);
3617 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3618 SET_EXPR_LOCATION (expr, locus);
3622 if (!then_se)
3624 /* If there is no 'then', turn
3625 if (a || b); else d
3626 into
3627 if (a); else if (b); else d. */
3628 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3630 /* Keep the original source location on the first 'if'. */
3631 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3632 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3633 /* Set the source location of the || on the second 'if'. */
3634 if (rexpr_has_location (pred))
3635 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3636 else_ = shortcut_cond_expr (expr);
3637 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3638 pred = TREE_OPERAND (pred, 0);
3639 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3640 SET_EXPR_LOCATION (expr, locus);
3644 /* If we're done, great. */
3645 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3646 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3647 return expr;
3649 /* Otherwise we need to mess with gotos. Change
3650 if (a) c; else d;
3652 if (a); else goto no;
3653 c; goto end;
3654 no: d; end:
3655 and recursively gimplify the condition. */
3657 true_label = false_label = end_label = NULL_TREE;
3659 /* If our arms just jump somewhere, hijack those labels so we don't
3660 generate jumps to jumps. */
3662 if (tree then_goto = find_goto_label (then_))
3664 true_label = GOTO_DESTINATION (then_goto);
3665 then_ = NULL;
3666 then_se = false;
3669 if (tree else_goto = find_goto_label (else_))
3671 false_label = GOTO_DESTINATION (else_goto);
3672 else_ = NULL;
3673 else_se = false;
3676 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3677 if (true_label)
3678 true_label_p = &true_label;
3679 else
3680 true_label_p = NULL;
3682 /* The 'else' branch also needs a label if it contains interesting code. */
3683 if (false_label || else_se)
3684 false_label_p = &false_label;
3685 else
3686 false_label_p = NULL;
3688 /* If there was nothing else in our arms, just forward the label(s). */
3689 if (!then_se && !else_se)
3690 return shortcut_cond_r (pred, true_label_p, false_label_p,
3691 EXPR_LOC_OR_LOC (expr, input_location));
3693 /* If our last subexpression already has a terminal label, reuse it. */
3694 if (else_se)
3695 t = expr_last (else_);
3696 else if (then_se)
3697 t = expr_last (then_);
3698 else
3699 t = NULL;
3700 if (t && TREE_CODE (t) == LABEL_EXPR)
3701 end_label = LABEL_EXPR_LABEL (t);
3703 /* If we don't care about jumping to the 'else' branch, jump to the end
3704 if the condition is false. */
3705 if (!false_label_p)
3706 false_label_p = &end_label;
3708 /* We only want to emit these labels if we aren't hijacking them. */
3709 emit_end = (end_label == NULL_TREE);
3710 emit_false = (false_label == NULL_TREE);
3712 /* We only emit the jump over the else clause if we have to--if the
3713 then clause may fall through. Otherwise we can wind up with a
3714 useless jump and a useless label at the end of gimplified code,
3715 which will cause us to think that this conditional as a whole
3716 falls through even if it doesn't. If we then inline a function
3717 which ends with such a condition, that can cause us to issue an
3718 inappropriate warning about control reaching the end of a
3719 non-void function. */
3720 jump_over_else = block_may_fallthru (then_);
3722 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3723 EXPR_LOC_OR_LOC (expr, input_location));
3725 expr = NULL;
3726 append_to_statement_list (pred, &expr);
3728 append_to_statement_list (then_, &expr);
3729 if (else_se)
3731 if (jump_over_else)
3733 tree last = expr_last (expr);
3734 t = build_and_jump (&end_label);
3735 if (rexpr_has_location (last))
3736 SET_EXPR_LOCATION (t, rexpr_location (last));
3737 append_to_statement_list (t, &expr);
3739 if (emit_false)
3741 t = build1 (LABEL_EXPR, void_type_node, false_label);
3742 append_to_statement_list (t, &expr);
3744 append_to_statement_list (else_, &expr);
3746 if (emit_end && end_label)
3748 t = build1 (LABEL_EXPR, void_type_node, end_label);
3749 append_to_statement_list (t, &expr);
3752 return expr;
3755 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3757 tree
3758 gimple_boolify (tree expr)
3760 tree type = TREE_TYPE (expr);
3761 location_t loc = EXPR_LOCATION (expr);
3763 if (TREE_CODE (expr) == NE_EXPR
3764 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3765 && integer_zerop (TREE_OPERAND (expr, 1)))
3767 tree call = TREE_OPERAND (expr, 0);
3768 tree fn = get_callee_fndecl (call);
3770 /* For __builtin_expect ((long) (x), y) recurse into x as well
3771 if x is truth_value_p. */
3772 if (fn
3773 && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
3774 && call_expr_nargs (call) == 2)
3776 tree arg = CALL_EXPR_ARG (call, 0);
3777 if (arg)
3779 if (TREE_CODE (arg) == NOP_EXPR
3780 && TREE_TYPE (arg) == TREE_TYPE (call))
3781 arg = TREE_OPERAND (arg, 0);
3782 if (truth_value_p (TREE_CODE (arg)))
3784 arg = gimple_boolify (arg);
3785 CALL_EXPR_ARG (call, 0)
3786 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3792 switch (TREE_CODE (expr))
3794 case TRUTH_AND_EXPR:
3795 case TRUTH_OR_EXPR:
3796 case TRUTH_XOR_EXPR:
3797 case TRUTH_ANDIF_EXPR:
3798 case TRUTH_ORIF_EXPR:
3799 /* Also boolify the arguments of truth exprs. */
3800 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3801 /* FALLTHRU */
3803 case TRUTH_NOT_EXPR:
3804 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3806 /* These expressions always produce boolean results. */
3807 if (TREE_CODE (type) != BOOLEAN_TYPE)
3808 TREE_TYPE (expr) = boolean_type_node;
3809 return expr;
3811 case ANNOTATE_EXPR:
3812 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3814 case annot_expr_ivdep_kind:
3815 case annot_expr_unroll_kind:
3816 case annot_expr_no_vector_kind:
3817 case annot_expr_vector_kind:
3818 case annot_expr_parallel_kind:
3819 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3820 if (TREE_CODE (type) != BOOLEAN_TYPE)
3821 TREE_TYPE (expr) = boolean_type_node;
3822 return expr;
3823 default:
3824 gcc_unreachable ();
3827 default:
3828 if (COMPARISON_CLASS_P (expr))
3830 /* There expressions always prduce boolean results. */
3831 if (TREE_CODE (type) != BOOLEAN_TYPE)
3832 TREE_TYPE (expr) = boolean_type_node;
3833 return expr;
3835 /* Other expressions that get here must have boolean values, but
3836 might need to be converted to the appropriate mode. */
3837 if (TREE_CODE (type) == BOOLEAN_TYPE)
3838 return expr;
3839 return fold_convert_loc (loc, boolean_type_node, expr);
3843 /* Given a conditional expression *EXPR_P without side effects, gimplify
3844 its operands. New statements are inserted to PRE_P. */
3846 static enum gimplify_status
3847 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3849 tree expr = *expr_p, cond;
3850 enum gimplify_status ret, tret;
3851 enum tree_code code;
3853 cond = gimple_boolify (COND_EXPR_COND (expr));
3855 /* We need to handle && and || specially, as their gimplification
3856 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3857 code = TREE_CODE (cond);
3858 if (code == TRUTH_ANDIF_EXPR)
3859 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3860 else if (code == TRUTH_ORIF_EXPR)
3861 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3862 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3863 COND_EXPR_COND (*expr_p) = cond;
3865 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3866 is_gimple_val, fb_rvalue);
3867 ret = MIN (ret, tret);
3868 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3869 is_gimple_val, fb_rvalue);
3871 return MIN (ret, tret);
3874 /* Return true if evaluating EXPR could trap.
3875 EXPR is GENERIC, while tree_could_trap_p can be called
3876 only on GIMPLE. */
3878 bool
3879 generic_expr_could_trap_p (tree expr)
3881 unsigned i, n;
3883 if (!expr || is_gimple_val (expr))
3884 return false;
3886 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3887 return true;
3889 n = TREE_OPERAND_LENGTH (expr);
3890 for (i = 0; i < n; i++)
3891 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3892 return true;
3894 return false;
3897 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3898 into
3900 if (p) if (p)
3901 t1 = a; a;
3902 else or else
3903 t1 = b; b;
3906 The second form is used when *EXPR_P is of type void.
3908 PRE_P points to the list where side effects that must happen before
3909 *EXPR_P should be stored. */
3911 static enum gimplify_status
3912 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3914 tree expr = *expr_p;
3915 tree type = TREE_TYPE (expr);
3916 location_t loc = EXPR_LOCATION (expr);
3917 tree tmp, arm1, arm2;
3918 enum gimplify_status ret;
3919 tree label_true, label_false, label_cont;
3920 bool have_then_clause_p, have_else_clause_p;
3921 gcond *cond_stmt;
3922 enum tree_code pred_code;
3923 gimple_seq seq = NULL;
3925 /* If this COND_EXPR has a value, copy the values into a temporary within
3926 the arms. */
3927 if (!VOID_TYPE_P (type))
3929 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3930 tree result;
3932 /* If either an rvalue is ok or we do not require an lvalue, create the
3933 temporary. But we cannot do that if the type is addressable. */
3934 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3935 && !TREE_ADDRESSABLE (type))
3937 if (gimplify_ctxp->allow_rhs_cond_expr
3938 /* If either branch has side effects or could trap, it can't be
3939 evaluated unconditionally. */
3940 && !TREE_SIDE_EFFECTS (then_)
3941 && !generic_expr_could_trap_p (then_)
3942 && !TREE_SIDE_EFFECTS (else_)
3943 && !generic_expr_could_trap_p (else_))
3944 return gimplify_pure_cond_expr (expr_p, pre_p);
3946 tmp = create_tmp_var (type, "iftmp");
3947 result = tmp;
3950 /* Otherwise, only create and copy references to the values. */
3951 else
3953 type = build_pointer_type (type);
3955 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3956 then_ = build_fold_addr_expr_loc (loc, then_);
3958 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3959 else_ = build_fold_addr_expr_loc (loc, else_);
3961 expr
3962 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3964 tmp = create_tmp_var (type, "iftmp");
3965 result = build_simple_mem_ref_loc (loc, tmp);
3968 /* Build the new then clause, `tmp = then_;'. But don't build the
3969 assignment if the value is void; in C++ it can be if it's a throw. */
3970 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3971 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3973 /* Similarly, build the new else clause, `tmp = else_;'. */
3974 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3975 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3977 TREE_TYPE (expr) = void_type_node;
3978 recalculate_side_effects (expr);
3980 /* Move the COND_EXPR to the prequeue. */
3981 gimplify_stmt (&expr, pre_p);
3983 *expr_p = result;
3984 return GS_ALL_DONE;
3987 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3988 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3989 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3990 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3992 /* Make sure the condition has BOOLEAN_TYPE. */
3993 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3995 /* Break apart && and || conditions. */
3996 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3997 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3999 expr = shortcut_cond_expr (expr);
4001 if (expr != *expr_p)
4003 *expr_p = expr;
4005 /* We can't rely on gimplify_expr to re-gimplify the expanded
4006 form properly, as cleanups might cause the target labels to be
4007 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4008 set up a conditional context. */
4009 gimple_push_condition ();
4010 gimplify_stmt (expr_p, &seq);
4011 gimple_pop_condition (pre_p);
4012 gimple_seq_add_seq (pre_p, seq);
4014 return GS_ALL_DONE;
4018 /* Now do the normal gimplification. */
4020 /* Gimplify condition. */
4021 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4022 fb_rvalue);
4023 if (ret == GS_ERROR)
4024 return GS_ERROR;
4025 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4027 gimple_push_condition ();
4029 have_then_clause_p = have_else_clause_p = false;
4030 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4031 if (label_true
4032 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4033 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4034 have different locations, otherwise we end up with incorrect
4035 location information on the branches. */
4036 && (optimize
4037 || !EXPR_HAS_LOCATION (expr)
4038 || !rexpr_has_location (label_true)
4039 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4041 have_then_clause_p = true;
4042 label_true = GOTO_DESTINATION (label_true);
4044 else
4045 label_true = create_artificial_label (UNKNOWN_LOCATION);
4046 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4047 if (label_false
4048 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4049 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4050 have different locations, otherwise we end up with incorrect
4051 location information on the branches. */
4052 && (optimize
4053 || !EXPR_HAS_LOCATION (expr)
4054 || !rexpr_has_location (label_false)
4055 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4057 have_else_clause_p = true;
4058 label_false = GOTO_DESTINATION (label_false);
4060 else
4061 label_false = create_artificial_label (UNKNOWN_LOCATION);
4063 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4064 &arm2);
4065 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4066 label_false);
4067 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4068 gimplify_seq_add_stmt (&seq, cond_stmt);
4069 gimple_stmt_iterator gsi = gsi_last (seq);
4070 maybe_fold_stmt (&gsi);
4072 label_cont = NULL_TREE;
4073 if (!have_then_clause_p)
4075 /* For if (...) {} else { code; } put label_true after
4076 the else block. */
4077 if (TREE_OPERAND (expr, 1) == NULL_TREE
4078 && !have_else_clause_p
4079 && TREE_OPERAND (expr, 2) != NULL_TREE)
4080 label_cont = label_true;
4081 else
4083 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4084 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4085 /* For if (...) { code; } else {} or
4086 if (...) { code; } else goto label; or
4087 if (...) { code; return; } else { ... }
4088 label_cont isn't needed. */
4089 if (!have_else_clause_p
4090 && TREE_OPERAND (expr, 2) != NULL_TREE
4091 && gimple_seq_may_fallthru (seq))
4093 gimple *g;
4094 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4096 g = gimple_build_goto (label_cont);
4098 /* GIMPLE_COND's are very low level; they have embedded
4099 gotos. This particular embedded goto should not be marked
4100 with the location of the original COND_EXPR, as it would
4101 correspond to the COND_EXPR's condition, not the ELSE or the
4102 THEN arms. To avoid marking it with the wrong location, flag
4103 it as "no location". */
4104 gimple_set_do_not_emit_location (g);
4106 gimplify_seq_add_stmt (&seq, g);
4110 if (!have_else_clause_p)
4112 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4113 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4115 if (label_cont)
4116 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4118 gimple_pop_condition (pre_p);
4119 gimple_seq_add_seq (pre_p, seq);
4121 if (ret == GS_ERROR)
4122 ; /* Do nothing. */
4123 else if (have_then_clause_p || have_else_clause_p)
4124 ret = GS_ALL_DONE;
4125 else
4127 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4128 expr = TREE_OPERAND (expr, 0);
4129 gimplify_stmt (&expr, pre_p);
4132 *expr_p = NULL;
4133 return ret;
4136 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4137 to be marked addressable.
4139 We cannot rely on such an expression being directly markable if a temporary
4140 has been created by the gimplification. In this case, we create another
4141 temporary and initialize it with a copy, which will become a store after we
4142 mark it addressable. This can happen if the front-end passed us something
4143 that it could not mark addressable yet, like a Fortran pass-by-reference
4144 parameter (int) floatvar. */
4146 static void
4147 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4149 while (handled_component_p (*expr_p))
4150 expr_p = &TREE_OPERAND (*expr_p, 0);
4151 if (is_gimple_reg (*expr_p))
4153 /* Do not allow an SSA name as the temporary. */
4154 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4155 DECL_GIMPLE_REG_P (var) = 0;
4156 *expr_p = var;
4160 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4161 a call to __builtin_memcpy. */
4163 static enum gimplify_status
4164 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4165 gimple_seq *seq_p)
4167 tree t, to, to_ptr, from, from_ptr;
4168 gcall *gs;
4169 location_t loc = EXPR_LOCATION (*expr_p);
4171 to = TREE_OPERAND (*expr_p, 0);
4172 from = TREE_OPERAND (*expr_p, 1);
4174 /* Mark the RHS addressable. Beware that it may not be possible to do so
4175 directly if a temporary has been created by the gimplification. */
4176 prepare_gimple_addressable (&from, seq_p);
4178 mark_addressable (from);
4179 from_ptr = build_fold_addr_expr_loc (loc, from);
4180 gimplify_arg (&from_ptr, seq_p, loc);
4182 mark_addressable (to);
4183 to_ptr = build_fold_addr_expr_loc (loc, to);
4184 gimplify_arg (&to_ptr, seq_p, loc);
4186 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4188 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4190 if (want_value)
4192 /* tmp = memcpy() */
4193 t = create_tmp_var (TREE_TYPE (to_ptr));
4194 gimple_call_set_lhs (gs, t);
4195 gimplify_seq_add_stmt (seq_p, gs);
4197 *expr_p = build_simple_mem_ref (t);
4198 return GS_ALL_DONE;
4201 gimplify_seq_add_stmt (seq_p, gs);
4202 *expr_p = NULL;
4203 return GS_ALL_DONE;
4206 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4207 a call to __builtin_memset. In this case we know that the RHS is
4208 a CONSTRUCTOR with an empty element list. */
4210 static enum gimplify_status
4211 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4212 gimple_seq *seq_p)
4214 tree t, from, to, to_ptr;
4215 gcall *gs;
4216 location_t loc = EXPR_LOCATION (*expr_p);
4218 /* Assert our assumptions, to abort instead of producing wrong code
4219 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4220 not be immediately exposed. */
4221 from = TREE_OPERAND (*expr_p, 1);
4222 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4223 from = TREE_OPERAND (from, 0);
4225 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4226 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4228 /* Now proceed. */
4229 to = TREE_OPERAND (*expr_p, 0);
4231 to_ptr = build_fold_addr_expr_loc (loc, to);
4232 gimplify_arg (&to_ptr, seq_p, loc);
4233 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4235 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4237 if (want_value)
4239 /* tmp = memset() */
4240 t = create_tmp_var (TREE_TYPE (to_ptr));
4241 gimple_call_set_lhs (gs, t);
4242 gimplify_seq_add_stmt (seq_p, gs);
4244 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4245 return GS_ALL_DONE;
4248 gimplify_seq_add_stmt (seq_p, gs);
4249 *expr_p = NULL;
4250 return GS_ALL_DONE;
4253 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4254 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4255 assignment. Return non-null if we detect a potential overlap. */
4257 struct gimplify_init_ctor_preeval_data
4259 /* The base decl of the lhs object. May be NULL, in which case we
4260 have to assume the lhs is indirect. */
4261 tree lhs_base_decl;
4263 /* The alias set of the lhs object. */
4264 alias_set_type lhs_alias_set;
4267 static tree
4268 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4270 struct gimplify_init_ctor_preeval_data *data
4271 = (struct gimplify_init_ctor_preeval_data *) xdata;
4272 tree t = *tp;
4274 /* If we find the base object, obviously we have overlap. */
4275 if (data->lhs_base_decl == t)
4276 return t;
4278 /* If the constructor component is indirect, determine if we have a
4279 potential overlap with the lhs. The only bits of information we
4280 have to go on at this point are addressability and alias sets. */
4281 if ((INDIRECT_REF_P (t)
4282 || TREE_CODE (t) == MEM_REF)
4283 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4284 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4285 return t;
4287 /* If the constructor component is a call, determine if it can hide a
4288 potential overlap with the lhs through an INDIRECT_REF like above.
4289 ??? Ugh - this is completely broken. In fact this whole analysis
4290 doesn't look conservative. */
4291 if (TREE_CODE (t) == CALL_EXPR)
4293 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4295 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4296 if (POINTER_TYPE_P (TREE_VALUE (type))
4297 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4298 && alias_sets_conflict_p (data->lhs_alias_set,
4299 get_alias_set
4300 (TREE_TYPE (TREE_VALUE (type)))))
4301 return t;
4304 if (IS_TYPE_OR_DECL_P (t))
4305 *walk_subtrees = 0;
4306 return NULL;
4309 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4310 force values that overlap with the lhs (as described by *DATA)
4311 into temporaries. */
4313 static void
4314 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4315 struct gimplify_init_ctor_preeval_data *data)
4317 enum gimplify_status one;
4319 /* If the value is constant, then there's nothing to pre-evaluate. */
4320 if (TREE_CONSTANT (*expr_p))
4322 /* Ensure it does not have side effects, it might contain a reference to
4323 the object we're initializing. */
4324 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4325 return;
4328 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4329 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4330 return;
4332 /* Recurse for nested constructors. */
4333 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4335 unsigned HOST_WIDE_INT ix;
4336 constructor_elt *ce;
4337 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4339 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4340 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4342 return;
4345 /* If this is a variable sized type, we must remember the size. */
4346 maybe_with_size_expr (expr_p);
4348 /* Gimplify the constructor element to something appropriate for the rhs
4349 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4350 the gimplifier will consider this a store to memory. Doing this
4351 gimplification now means that we won't have to deal with complicated
4352 language-specific trees, nor trees like SAVE_EXPR that can induce
4353 exponential search behavior. */
4354 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4355 if (one == GS_ERROR)
4357 *expr_p = NULL;
4358 return;
4361 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4362 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4363 always be true for all scalars, since is_gimple_mem_rhs insists on a
4364 temporary variable for them. */
4365 if (DECL_P (*expr_p))
4366 return;
4368 /* If this is of variable size, we have no choice but to assume it doesn't
4369 overlap since we can't make a temporary for it. */
4370 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4371 return;
4373 /* Otherwise, we must search for overlap ... */
4374 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4375 return;
4377 /* ... and if found, force the value into a temporary. */
4378 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4381 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4382 a RANGE_EXPR in a CONSTRUCTOR for an array.
4384 var = lower;
4385 loop_entry:
4386 object[var] = value;
4387 if (var == upper)
4388 goto loop_exit;
4389 var = var + 1;
4390 goto loop_entry;
4391 loop_exit:
4393 We increment var _after_ the loop exit check because we might otherwise
4394 fail if upper == TYPE_MAX_VALUE (type for upper).
4396 Note that we never have to deal with SAVE_EXPRs here, because this has
4397 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4399 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4400 gimple_seq *, bool);
4402 static void
4403 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4404 tree value, tree array_elt_type,
4405 gimple_seq *pre_p, bool cleared)
4407 tree loop_entry_label, loop_exit_label, fall_thru_label;
4408 tree var, var_type, cref, tmp;
4410 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4411 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4412 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4414 /* Create and initialize the index variable. */
4415 var_type = TREE_TYPE (upper);
4416 var = create_tmp_var (var_type);
4417 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4419 /* Add the loop entry label. */
4420 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4422 /* Build the reference. */
4423 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4424 var, NULL_TREE, NULL_TREE);
4426 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4427 the store. Otherwise just assign value to the reference. */
4429 if (TREE_CODE (value) == CONSTRUCTOR)
4430 /* NB we might have to call ourself recursively through
4431 gimplify_init_ctor_eval if the value is a constructor. */
4432 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4433 pre_p, cleared);
4434 else
4435 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4437 /* We exit the loop when the index var is equal to the upper bound. */
4438 gimplify_seq_add_stmt (pre_p,
4439 gimple_build_cond (EQ_EXPR, var, upper,
4440 loop_exit_label, fall_thru_label));
4442 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4444 /* Otherwise, increment the index var... */
4445 tmp = build2 (PLUS_EXPR, var_type, var,
4446 fold_convert (var_type, integer_one_node));
4447 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4449 /* ...and jump back to the loop entry. */
4450 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4452 /* Add the loop exit label. */
4453 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4456 /* Return true if FDECL is accessing a field that is zero sized. */
4458 static bool
4459 zero_sized_field_decl (const_tree fdecl)
4461 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4462 && integer_zerop (DECL_SIZE (fdecl)))
4463 return true;
4464 return false;
4467 /* Return true if TYPE is zero sized. */
4469 static bool
4470 zero_sized_type (const_tree type)
4472 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4473 && integer_zerop (TYPE_SIZE (type)))
4474 return true;
4475 return false;
4478 /* A subroutine of gimplify_init_constructor. Generate individual
4479 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4480 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4481 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4482 zeroed first. */
4484 static void
4485 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4486 gimple_seq *pre_p, bool cleared)
4488 tree array_elt_type = NULL;
4489 unsigned HOST_WIDE_INT ix;
4490 tree purpose, value;
4492 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4493 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4495 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4497 tree cref;
4499 /* NULL values are created above for gimplification errors. */
4500 if (value == NULL)
4501 continue;
4503 if (cleared && initializer_zerop (value))
4504 continue;
4506 /* ??? Here's to hoping the front end fills in all of the indices,
4507 so we don't have to figure out what's missing ourselves. */
4508 gcc_assert (purpose);
4510 /* Skip zero-sized fields, unless value has side-effects. This can
4511 happen with calls to functions returning a zero-sized type, which
4512 we shouldn't discard. As a number of downstream passes don't
4513 expect sets of zero-sized fields, we rely on the gimplification of
4514 the MODIFY_EXPR we make below to drop the assignment statement. */
4515 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4516 continue;
4518 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4519 whole range. */
4520 if (TREE_CODE (purpose) == RANGE_EXPR)
4522 tree lower = TREE_OPERAND (purpose, 0);
4523 tree upper = TREE_OPERAND (purpose, 1);
4525 /* If the lower bound is equal to upper, just treat it as if
4526 upper was the index. */
4527 if (simple_cst_equal (lower, upper))
4528 purpose = upper;
4529 else
4531 gimplify_init_ctor_eval_range (object, lower, upper, value,
4532 array_elt_type, pre_p, cleared);
4533 continue;
4537 if (array_elt_type)
4539 /* Do not use bitsizetype for ARRAY_REF indices. */
4540 if (TYPE_DOMAIN (TREE_TYPE (object)))
4541 purpose
4542 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4543 purpose);
4544 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4545 purpose, NULL_TREE, NULL_TREE);
4547 else
4549 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4550 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4551 unshare_expr (object), purpose, NULL_TREE);
4554 if (TREE_CODE (value) == CONSTRUCTOR
4555 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4556 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4557 pre_p, cleared);
4558 else
4560 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4561 gimplify_and_add (init, pre_p);
4562 ggc_free (init);
4567 /* Return the appropriate RHS predicate for this LHS. */
4569 gimple_predicate
4570 rhs_predicate_for (tree lhs)
4572 if (is_gimple_reg (lhs))
4573 return is_gimple_reg_rhs_or_call;
4574 else
4575 return is_gimple_mem_rhs_or_call;
4578 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4579 before the LHS has been gimplified. */
4581 static gimple_predicate
4582 initial_rhs_predicate_for (tree lhs)
4584 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4585 return is_gimple_reg_rhs_or_call;
4586 else
4587 return is_gimple_mem_rhs_or_call;
4590 /* Gimplify a C99 compound literal expression. This just means adding
4591 the DECL_EXPR before the current statement and using its anonymous
4592 decl instead. */
4594 static enum gimplify_status
4595 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4596 bool (*gimple_test_f) (tree),
4597 fallback_t fallback)
4599 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4600 tree decl = DECL_EXPR_DECL (decl_s);
4601 tree init = DECL_INITIAL (decl);
4602 /* Mark the decl as addressable if the compound literal
4603 expression is addressable now, otherwise it is marked too late
4604 after we gimplify the initialization expression. */
4605 if (TREE_ADDRESSABLE (*expr_p))
4606 TREE_ADDRESSABLE (decl) = 1;
4607 /* Otherwise, if we don't need an lvalue and have a literal directly
4608 substitute it. Check if it matches the gimple predicate, as
4609 otherwise we'd generate a new temporary, and we can as well just
4610 use the decl we already have. */
4611 else if (!TREE_ADDRESSABLE (decl)
4612 && init
4613 && (fallback & fb_lvalue) == 0
4614 && gimple_test_f (init))
4616 *expr_p = init;
4617 return GS_OK;
4620 /* Preliminarily mark non-addressed complex variables as eligible
4621 for promotion to gimple registers. We'll transform their uses
4622 as we find them. */
4623 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4624 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4625 && !TREE_THIS_VOLATILE (decl)
4626 && !needs_to_live_in_memory (decl))
4627 DECL_GIMPLE_REG_P (decl) = 1;
4629 /* If the decl is not addressable, then it is being used in some
4630 expression or on the right hand side of a statement, and it can
4631 be put into a readonly data section. */
4632 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4633 TREE_READONLY (decl) = 1;
4635 /* This decl isn't mentioned in the enclosing block, so add it to the
4636 list of temps. FIXME it seems a bit of a kludge to say that
4637 anonymous artificial vars aren't pushed, but everything else is. */
4638 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4639 gimple_add_tmp_var (decl);
4641 gimplify_and_add (decl_s, pre_p);
4642 *expr_p = decl;
4643 return GS_OK;
4646 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4647 return a new CONSTRUCTOR if something changed. */
4649 static tree
4650 optimize_compound_literals_in_ctor (tree orig_ctor)
4652 tree ctor = orig_ctor;
4653 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4654 unsigned int idx, num = vec_safe_length (elts);
4656 for (idx = 0; idx < num; idx++)
4658 tree value = (*elts)[idx].value;
4659 tree newval = value;
4660 if (TREE_CODE (value) == CONSTRUCTOR)
4661 newval = optimize_compound_literals_in_ctor (value);
4662 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4664 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4665 tree decl = DECL_EXPR_DECL (decl_s);
4666 tree init = DECL_INITIAL (decl);
4668 if (!TREE_ADDRESSABLE (value)
4669 && !TREE_ADDRESSABLE (decl)
4670 && init
4671 && TREE_CODE (init) == CONSTRUCTOR)
4672 newval = optimize_compound_literals_in_ctor (init);
4674 if (newval == value)
4675 continue;
4677 if (ctor == orig_ctor)
4679 ctor = copy_node (orig_ctor);
4680 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4681 elts = CONSTRUCTOR_ELTS (ctor);
4683 (*elts)[idx].value = newval;
4685 return ctor;
4688 /* A subroutine of gimplify_modify_expr. Break out elements of a
4689 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4691 Note that we still need to clear any elements that don't have explicit
4692 initializers, so if not all elements are initialized we keep the
4693 original MODIFY_EXPR, we just remove all of the constructor elements.
4695 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4696 GS_ERROR if we would have to create a temporary when gimplifying
4697 this constructor. Otherwise, return GS_OK.
4699 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4701 static enum gimplify_status
4702 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4703 bool want_value, bool notify_temp_creation)
4705 tree object, ctor, type;
4706 enum gimplify_status ret;
4707 vec<constructor_elt, va_gc> *elts;
4709 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4711 if (!notify_temp_creation)
4713 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4714 is_gimple_lvalue, fb_lvalue);
4715 if (ret == GS_ERROR)
4716 return ret;
4719 object = TREE_OPERAND (*expr_p, 0);
4720 ctor = TREE_OPERAND (*expr_p, 1)
4721 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4722 type = TREE_TYPE (ctor);
4723 elts = CONSTRUCTOR_ELTS (ctor);
4724 ret = GS_ALL_DONE;
4726 switch (TREE_CODE (type))
4728 case RECORD_TYPE:
4729 case UNION_TYPE:
4730 case QUAL_UNION_TYPE:
4731 case ARRAY_TYPE:
4733 struct gimplify_init_ctor_preeval_data preeval_data;
4734 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4735 bool cleared, complete_p, valid_const_initializer;
4737 /* Aggregate types must lower constructors to initialization of
4738 individual elements. The exception is that a CONSTRUCTOR node
4739 with no elements indicates zero-initialization of the whole. */
4740 if (vec_safe_is_empty (elts))
4742 if (notify_temp_creation)
4743 return GS_OK;
4744 break;
4747 /* Fetch information about the constructor to direct later processing.
4748 We might want to make static versions of it in various cases, and
4749 can only do so if it known to be a valid constant initializer. */
4750 valid_const_initializer
4751 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4752 &num_ctor_elements, &complete_p);
4754 /* If a const aggregate variable is being initialized, then it
4755 should never be a lose to promote the variable to be static. */
4756 if (valid_const_initializer
4757 && num_nonzero_elements > 1
4758 && TREE_READONLY (object)
4759 && VAR_P (object)
4760 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4762 if (notify_temp_creation)
4763 return GS_ERROR;
4764 DECL_INITIAL (object) = ctor;
4765 TREE_STATIC (object) = 1;
4766 if (!DECL_NAME (object))
4767 DECL_NAME (object) = create_tmp_var_name ("C");
4768 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4770 /* ??? C++ doesn't automatically append a .<number> to the
4771 assembler name, and even when it does, it looks at FE private
4772 data structures to figure out what that number should be,
4773 which are not set for this variable. I suppose this is
4774 important for local statics for inline functions, which aren't
4775 "local" in the object file sense. So in order to get a unique
4776 TU-local symbol, we must invoke the lhd version now. */
4777 lhd_set_decl_assembler_name (object);
4779 *expr_p = NULL_TREE;
4780 break;
4783 /* If there are "lots" of initialized elements, even discounting
4784 those that are not address constants (and thus *must* be
4785 computed at runtime), then partition the constructor into
4786 constant and non-constant parts. Block copy the constant
4787 parts in, then generate code for the non-constant parts. */
4788 /* TODO. There's code in cp/typeck.c to do this. */
4790 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4791 /* store_constructor will ignore the clearing of variable-sized
4792 objects. Initializers for such objects must explicitly set
4793 every field that needs to be set. */
4794 cleared = false;
4795 else if (!complete_p)
4796 /* If the constructor isn't complete, clear the whole object
4797 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4799 ??? This ought not to be needed. For any element not present
4800 in the initializer, we should simply set them to zero. Except
4801 we'd need to *find* the elements that are not present, and that
4802 requires trickery to avoid quadratic compile-time behavior in
4803 large cases or excessive memory use in small cases. */
4804 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4805 else if (num_ctor_elements - num_nonzero_elements
4806 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4807 && num_nonzero_elements < num_ctor_elements / 4)
4808 /* If there are "lots" of zeros, it's more efficient to clear
4809 the memory and then set the nonzero elements. */
4810 cleared = true;
4811 else
4812 cleared = false;
4814 /* If there are "lots" of initialized elements, and all of them
4815 are valid address constants, then the entire initializer can
4816 be dropped to memory, and then memcpy'd out. Don't do this
4817 for sparse arrays, though, as it's more efficient to follow
4818 the standard CONSTRUCTOR behavior of memset followed by
4819 individual element initialization. Also don't do this for small
4820 all-zero initializers (which aren't big enough to merit
4821 clearing), and don't try to make bitwise copies of
4822 TREE_ADDRESSABLE types. */
4824 if (valid_const_initializer
4825 && !(cleared || num_nonzero_elements == 0)
4826 && !TREE_ADDRESSABLE (type))
4828 HOST_WIDE_INT size = int_size_in_bytes (type);
4829 unsigned int align;
4831 /* ??? We can still get unbounded array types, at least
4832 from the C++ front end. This seems wrong, but attempt
4833 to work around it for now. */
4834 if (size < 0)
4836 size = int_size_in_bytes (TREE_TYPE (object));
4837 if (size >= 0)
4838 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4841 /* Find the maximum alignment we can assume for the object. */
4842 /* ??? Make use of DECL_OFFSET_ALIGN. */
4843 if (DECL_P (object))
4844 align = DECL_ALIGN (object);
4845 else
4846 align = TYPE_ALIGN (type);
4848 /* Do a block move either if the size is so small as to make
4849 each individual move a sub-unit move on average, or if it
4850 is so large as to make individual moves inefficient. */
4851 if (size > 0
4852 && num_nonzero_elements > 1
4853 && (size < num_nonzero_elements
4854 || !can_move_by_pieces (size, align)))
4856 if (notify_temp_creation)
4857 return GS_ERROR;
4859 walk_tree (&ctor, force_labels_r, NULL, NULL);
4860 ctor = tree_output_constant_def (ctor);
4861 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4862 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4863 TREE_OPERAND (*expr_p, 1) = ctor;
4865 /* This is no longer an assignment of a CONSTRUCTOR, but
4866 we still may have processing to do on the LHS. So
4867 pretend we didn't do anything here to let that happen. */
4868 return GS_UNHANDLED;
4872 /* If the target is volatile, we have non-zero elements and more than
4873 one field to assign, initialize the target from a temporary. */
4874 if (TREE_THIS_VOLATILE (object)
4875 && !TREE_ADDRESSABLE (type)
4876 && num_nonzero_elements > 0
4877 && vec_safe_length (elts) > 1)
4879 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4880 TREE_OPERAND (*expr_p, 0) = temp;
4881 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4882 *expr_p,
4883 build2 (MODIFY_EXPR, void_type_node,
4884 object, temp));
4885 return GS_OK;
4888 if (notify_temp_creation)
4889 return GS_OK;
4891 /* If there are nonzero elements and if needed, pre-evaluate to capture
4892 elements overlapping with the lhs into temporaries. We must do this
4893 before clearing to fetch the values before they are zeroed-out. */
4894 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4896 preeval_data.lhs_base_decl = get_base_address (object);
4897 if (!DECL_P (preeval_data.lhs_base_decl))
4898 preeval_data.lhs_base_decl = NULL;
4899 preeval_data.lhs_alias_set = get_alias_set (object);
4901 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4902 pre_p, post_p, &preeval_data);
4905 bool ctor_has_side_effects_p
4906 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4908 if (cleared)
4910 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4911 Note that we still have to gimplify, in order to handle the
4912 case of variable sized types. Avoid shared tree structures. */
4913 CONSTRUCTOR_ELTS (ctor) = NULL;
4914 TREE_SIDE_EFFECTS (ctor) = 0;
4915 object = unshare_expr (object);
4916 gimplify_stmt (expr_p, pre_p);
4919 /* If we have not block cleared the object, or if there are nonzero
4920 elements in the constructor, or if the constructor has side effects,
4921 add assignments to the individual scalar fields of the object. */
4922 if (!cleared
4923 || num_nonzero_elements > 0
4924 || ctor_has_side_effects_p)
4925 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4927 *expr_p = NULL_TREE;
4929 break;
4931 case COMPLEX_TYPE:
4933 tree r, i;
4935 if (notify_temp_creation)
4936 return GS_OK;
4938 /* Extract the real and imaginary parts out of the ctor. */
4939 gcc_assert (elts->length () == 2);
4940 r = (*elts)[0].value;
4941 i = (*elts)[1].value;
4942 if (r == NULL || i == NULL)
4944 tree zero = build_zero_cst (TREE_TYPE (type));
4945 if (r == NULL)
4946 r = zero;
4947 if (i == NULL)
4948 i = zero;
4951 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4952 represent creation of a complex value. */
4953 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4955 ctor = build_complex (type, r, i);
4956 TREE_OPERAND (*expr_p, 1) = ctor;
4958 else
4960 ctor = build2 (COMPLEX_EXPR, type, r, i);
4961 TREE_OPERAND (*expr_p, 1) = ctor;
4962 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4963 pre_p,
4964 post_p,
4965 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4966 fb_rvalue);
4969 break;
4971 case VECTOR_TYPE:
4973 unsigned HOST_WIDE_INT ix;
4974 constructor_elt *ce;
4976 if (notify_temp_creation)
4977 return GS_OK;
4979 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4980 if (TREE_CONSTANT (ctor))
4982 bool constant_p = true;
4983 tree value;
4985 /* Even when ctor is constant, it might contain non-*_CST
4986 elements, such as addresses or trapping values like
4987 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4988 in VECTOR_CST nodes. */
4989 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4990 if (!CONSTANT_CLASS_P (value))
4992 constant_p = false;
4993 break;
4996 if (constant_p)
4998 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4999 break;
5002 TREE_CONSTANT (ctor) = 0;
5005 /* Vector types use CONSTRUCTOR all the way through gimple
5006 compilation as a general initializer. */
5007 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5009 enum gimplify_status tret;
5010 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5011 fb_rvalue);
5012 if (tret == GS_ERROR)
5013 ret = GS_ERROR;
5014 else if (TREE_STATIC (ctor)
5015 && !initializer_constant_valid_p (ce->value,
5016 TREE_TYPE (ce->value)))
5017 TREE_STATIC (ctor) = 0;
5019 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5020 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5022 break;
5024 default:
5025 /* So how did we get a CONSTRUCTOR for a scalar type? */
5026 gcc_unreachable ();
5029 if (ret == GS_ERROR)
5030 return GS_ERROR;
5031 /* If we have gimplified both sides of the initializer but have
5032 not emitted an assignment, do so now. */
5033 if (*expr_p)
5035 tree lhs = TREE_OPERAND (*expr_p, 0);
5036 tree rhs = TREE_OPERAND (*expr_p, 1);
5037 if (want_value && object == lhs)
5038 lhs = unshare_expr (lhs);
5039 gassign *init = gimple_build_assign (lhs, rhs);
5040 gimplify_seq_add_stmt (pre_p, init);
5042 if (want_value)
5044 *expr_p = object;
5045 return GS_OK;
5047 else
5049 *expr_p = NULL;
5050 return GS_ALL_DONE;
5054 /* Given a pointer value OP0, return a simplified version of an
5055 indirection through OP0, or NULL_TREE if no simplification is
5056 possible. This may only be applied to a rhs of an expression.
5057 Note that the resulting type may be different from the type pointed
5058 to in the sense that it is still compatible from the langhooks
5059 point of view. */
5061 static tree
5062 gimple_fold_indirect_ref_rhs (tree t)
5064 return gimple_fold_indirect_ref (t);
5067 /* Subroutine of gimplify_modify_expr to do simplifications of
5068 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5069 something changes. */
5071 static enum gimplify_status
5072 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5073 gimple_seq *pre_p, gimple_seq *post_p,
5074 bool want_value)
5076 enum gimplify_status ret = GS_UNHANDLED;
5077 bool changed;
5081 changed = false;
5082 switch (TREE_CODE (*from_p))
5084 case VAR_DECL:
5085 /* If we're assigning from a read-only variable initialized with
5086 a constructor, do the direct assignment from the constructor,
5087 but only if neither source nor target are volatile since this
5088 latter assignment might end up being done on a per-field basis. */
5089 if (DECL_INITIAL (*from_p)
5090 && TREE_READONLY (*from_p)
5091 && !TREE_THIS_VOLATILE (*from_p)
5092 && !TREE_THIS_VOLATILE (*to_p)
5093 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5095 tree old_from = *from_p;
5096 enum gimplify_status subret;
5098 /* Move the constructor into the RHS. */
5099 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5101 /* Let's see if gimplify_init_constructor will need to put
5102 it in memory. */
5103 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5104 false, true);
5105 if (subret == GS_ERROR)
5107 /* If so, revert the change. */
5108 *from_p = old_from;
5110 else
5112 ret = GS_OK;
5113 changed = true;
5116 break;
5117 case INDIRECT_REF:
5119 /* If we have code like
5121 *(const A*)(A*)&x
5123 where the type of "x" is a (possibly cv-qualified variant
5124 of "A"), treat the entire expression as identical to "x".
5125 This kind of code arises in C++ when an object is bound
5126 to a const reference, and if "x" is a TARGET_EXPR we want
5127 to take advantage of the optimization below. */
5128 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5129 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5130 if (t)
5132 if (TREE_THIS_VOLATILE (t) != volatile_p)
5134 if (DECL_P (t))
5135 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5136 build_fold_addr_expr (t));
5137 if (REFERENCE_CLASS_P (t))
5138 TREE_THIS_VOLATILE (t) = volatile_p;
5140 *from_p = t;
5141 ret = GS_OK;
5142 changed = true;
5144 break;
5147 case TARGET_EXPR:
5149 /* If we are initializing something from a TARGET_EXPR, strip the
5150 TARGET_EXPR and initialize it directly, if possible. This can't
5151 be done if the initializer is void, since that implies that the
5152 temporary is set in some non-trivial way.
5154 ??? What about code that pulls out the temp and uses it
5155 elsewhere? I think that such code never uses the TARGET_EXPR as
5156 an initializer. If I'm wrong, we'll die because the temp won't
5157 have any RTL. In that case, I guess we'll need to replace
5158 references somehow. */
5159 tree init = TARGET_EXPR_INITIAL (*from_p);
5161 if (init
5162 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5163 || !TARGET_EXPR_NO_ELIDE (*from_p))
5164 && !VOID_TYPE_P (TREE_TYPE (init)))
5166 *from_p = init;
5167 ret = GS_OK;
5168 changed = true;
5171 break;
5173 case COMPOUND_EXPR:
5174 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5175 caught. */
5176 gimplify_compound_expr (from_p, pre_p, true);
5177 ret = GS_OK;
5178 changed = true;
5179 break;
5181 case CONSTRUCTOR:
5182 /* If we already made some changes, let the front end have a
5183 crack at this before we break it down. */
5184 if (ret != GS_UNHANDLED)
5185 break;
5186 /* If we're initializing from a CONSTRUCTOR, break this into
5187 individual MODIFY_EXPRs. */
5188 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5189 false);
5191 case COND_EXPR:
5192 /* If we're assigning to a non-register type, push the assignment
5193 down into the branches. This is mandatory for ADDRESSABLE types,
5194 since we cannot generate temporaries for such, but it saves a
5195 copy in other cases as well. */
5196 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5198 /* This code should mirror the code in gimplify_cond_expr. */
5199 enum tree_code code = TREE_CODE (*expr_p);
5200 tree cond = *from_p;
5201 tree result = *to_p;
5203 ret = gimplify_expr (&result, pre_p, post_p,
5204 is_gimple_lvalue, fb_lvalue);
5205 if (ret != GS_ERROR)
5206 ret = GS_OK;
5208 /* If we are going to write RESULT more than once, clear
5209 TREE_READONLY flag, otherwise we might incorrectly promote
5210 the variable to static const and initialize it at compile
5211 time in one of the branches. */
5212 if (VAR_P (result)
5213 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5214 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5215 TREE_READONLY (result) = 0;
5216 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5217 TREE_OPERAND (cond, 1)
5218 = build2 (code, void_type_node, result,
5219 TREE_OPERAND (cond, 1));
5220 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5221 TREE_OPERAND (cond, 2)
5222 = build2 (code, void_type_node, unshare_expr (result),
5223 TREE_OPERAND (cond, 2));
5225 TREE_TYPE (cond) = void_type_node;
5226 recalculate_side_effects (cond);
5228 if (want_value)
5230 gimplify_and_add (cond, pre_p);
5231 *expr_p = unshare_expr (result);
5233 else
5234 *expr_p = cond;
5235 return ret;
5237 break;
5239 case CALL_EXPR:
5240 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5241 return slot so that we don't generate a temporary. */
5242 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5243 && aggregate_value_p (*from_p, *from_p))
5245 bool use_target;
5247 if (!(rhs_predicate_for (*to_p))(*from_p))
5248 /* If we need a temporary, *to_p isn't accurate. */
5249 use_target = false;
5250 /* It's OK to use the return slot directly unless it's an NRV. */
5251 else if (TREE_CODE (*to_p) == RESULT_DECL
5252 && DECL_NAME (*to_p) == NULL_TREE
5253 && needs_to_live_in_memory (*to_p))
5254 use_target = true;
5255 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5256 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5257 /* Don't force regs into memory. */
5258 use_target = false;
5259 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5260 /* It's OK to use the target directly if it's being
5261 initialized. */
5262 use_target = true;
5263 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5264 != INTEGER_CST)
5265 /* Always use the target and thus RSO for variable-sized types.
5266 GIMPLE cannot deal with a variable-sized assignment
5267 embedded in a call statement. */
5268 use_target = true;
5269 else if (TREE_CODE (*to_p) != SSA_NAME
5270 && (!is_gimple_variable (*to_p)
5271 || needs_to_live_in_memory (*to_p)))
5272 /* Don't use the original target if it's already addressable;
5273 if its address escapes, and the called function uses the
5274 NRV optimization, a conforming program could see *to_p
5275 change before the called function returns; see c++/19317.
5276 When optimizing, the return_slot pass marks more functions
5277 as safe after we have escape info. */
5278 use_target = false;
5279 else
5280 use_target = true;
5282 if (use_target)
5284 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5285 mark_addressable (*to_p);
5288 break;
5290 case WITH_SIZE_EXPR:
5291 /* Likewise for calls that return an aggregate of non-constant size,
5292 since we would not be able to generate a temporary at all. */
5293 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5295 *from_p = TREE_OPERAND (*from_p, 0);
5296 /* We don't change ret in this case because the
5297 WITH_SIZE_EXPR might have been added in
5298 gimplify_modify_expr, so returning GS_OK would lead to an
5299 infinite loop. */
5300 changed = true;
5302 break;
5304 /* If we're initializing from a container, push the initialization
5305 inside it. */
5306 case CLEANUP_POINT_EXPR:
5307 case BIND_EXPR:
5308 case STATEMENT_LIST:
5310 tree wrap = *from_p;
5311 tree t;
5313 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5314 fb_lvalue);
5315 if (ret != GS_ERROR)
5316 ret = GS_OK;
5318 t = voidify_wrapper_expr (wrap, *expr_p);
5319 gcc_assert (t == *expr_p);
5321 if (want_value)
5323 gimplify_and_add (wrap, pre_p);
5324 *expr_p = unshare_expr (*to_p);
5326 else
5327 *expr_p = wrap;
5328 return GS_OK;
5331 case COMPOUND_LITERAL_EXPR:
5333 tree complit = TREE_OPERAND (*expr_p, 1);
5334 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5335 tree decl = DECL_EXPR_DECL (decl_s);
5336 tree init = DECL_INITIAL (decl);
5338 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5339 into struct T x = { 0, 1, 2 } if the address of the
5340 compound literal has never been taken. */
5341 if (!TREE_ADDRESSABLE (complit)
5342 && !TREE_ADDRESSABLE (decl)
5343 && init)
5345 *expr_p = copy_node (*expr_p);
5346 TREE_OPERAND (*expr_p, 1) = init;
5347 return GS_OK;
5351 default:
5352 break;
5355 while (changed);
5357 return ret;
5361 /* Return true if T looks like a valid GIMPLE statement. */
5363 static bool
5364 is_gimple_stmt (tree t)
5366 const enum tree_code code = TREE_CODE (t);
5368 switch (code)
5370 case NOP_EXPR:
5371 /* The only valid NOP_EXPR is the empty statement. */
5372 return IS_EMPTY_STMT (t);
5374 case BIND_EXPR:
5375 case COND_EXPR:
5376 /* These are only valid if they're void. */
5377 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5379 case SWITCH_EXPR:
5380 case GOTO_EXPR:
5381 case RETURN_EXPR:
5382 case LABEL_EXPR:
5383 case CASE_LABEL_EXPR:
5384 case TRY_CATCH_EXPR:
5385 case TRY_FINALLY_EXPR:
5386 case EH_FILTER_EXPR:
5387 case CATCH_EXPR:
5388 case ASM_EXPR:
5389 case STATEMENT_LIST:
5390 case OACC_PARALLEL:
5391 case OACC_KERNELS:
5392 case OACC_DATA:
5393 case OACC_HOST_DATA:
5394 case OACC_DECLARE:
5395 case OACC_UPDATE:
5396 case OACC_ENTER_DATA:
5397 case OACC_EXIT_DATA:
5398 case OACC_CACHE:
5399 case OMP_PARALLEL:
5400 case OMP_FOR:
5401 case OMP_SIMD:
5402 case OMP_DISTRIBUTE:
5403 case OACC_LOOP:
5404 case OMP_SECTIONS:
5405 case OMP_SECTION:
5406 case OMP_SINGLE:
5407 case OMP_MASTER:
5408 case OMP_TASKGROUP:
5409 case OMP_ORDERED:
5410 case OMP_CRITICAL:
5411 case OMP_TASK:
5412 case OMP_TARGET:
5413 case OMP_TARGET_DATA:
5414 case OMP_TARGET_UPDATE:
5415 case OMP_TARGET_ENTER_DATA:
5416 case OMP_TARGET_EXIT_DATA:
5417 case OMP_TASKLOOP:
5418 case OMP_TEAMS:
5419 /* These are always void. */
5420 return true;
5422 case CALL_EXPR:
5423 case MODIFY_EXPR:
5424 case PREDICT_EXPR:
5425 /* These are valid regardless of their type. */
5426 return true;
5428 default:
5429 return false;
5434 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5435 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5436 DECL_GIMPLE_REG_P set.
5438 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5439 other, unmodified part of the complex object just before the total store.
5440 As a consequence, if the object is still uninitialized, an undefined value
5441 will be loaded into a register, which may result in a spurious exception
5442 if the register is floating-point and the value happens to be a signaling
5443 NaN for example. Then the fully-fledged complex operations lowering pass
5444 followed by a DCE pass are necessary in order to fix things up. */
5446 static enum gimplify_status
5447 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5448 bool want_value)
5450 enum tree_code code, ocode;
5451 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5453 lhs = TREE_OPERAND (*expr_p, 0);
5454 rhs = TREE_OPERAND (*expr_p, 1);
5455 code = TREE_CODE (lhs);
5456 lhs = TREE_OPERAND (lhs, 0);
5458 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5459 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5460 TREE_NO_WARNING (other) = 1;
5461 other = get_formal_tmp_var (other, pre_p);
5463 realpart = code == REALPART_EXPR ? rhs : other;
5464 imagpart = code == REALPART_EXPR ? other : rhs;
5466 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5467 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5468 else
5469 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5471 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5472 *expr_p = (want_value) ? rhs : NULL_TREE;
5474 return GS_ALL_DONE;
5477 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5479 modify_expr
5480 : varname '=' rhs
5481 | '*' ID '=' rhs
5483 PRE_P points to the list where side effects that must happen before
5484 *EXPR_P should be stored.
5486 POST_P points to the list where side effects that must happen after
5487 *EXPR_P should be stored.
5489 WANT_VALUE is nonzero iff we want to use the value of this expression
5490 in another expression. */
5492 static enum gimplify_status
5493 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5494 bool want_value)
5496 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5497 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5498 enum gimplify_status ret = GS_UNHANDLED;
5499 gimple *assign;
5500 location_t loc = EXPR_LOCATION (*expr_p);
5501 gimple_stmt_iterator gsi;
5503 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5504 || TREE_CODE (*expr_p) == INIT_EXPR);
5506 /* Trying to simplify a clobber using normal logic doesn't work,
5507 so handle it here. */
5508 if (TREE_CLOBBER_P (*from_p))
5510 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5511 if (ret == GS_ERROR)
5512 return ret;
5513 gcc_assert (!want_value);
5514 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5516 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5517 pre_p, post_p);
5518 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5520 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5521 *expr_p = NULL;
5522 return GS_ALL_DONE;
5525 /* Insert pointer conversions required by the middle-end that are not
5526 required by the frontend. This fixes middle-end type checking for
5527 for example gcc.dg/redecl-6.c. */
5528 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5530 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5531 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5532 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5535 /* See if any simplifications can be done based on what the RHS is. */
5536 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5537 want_value);
5538 if (ret != GS_UNHANDLED)
5539 return ret;
5541 /* For zero sized types only gimplify the left hand side and right hand
5542 side as statements and throw away the assignment. Do this after
5543 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5544 types properly. */
5545 if (zero_sized_type (TREE_TYPE (*from_p))
5546 && !want_value
5547 /* Don't do this for calls that return addressable types, expand_call
5548 relies on those having a lhs. */
5549 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5550 && TREE_CODE (*from_p) == CALL_EXPR))
5552 gimplify_stmt (from_p, pre_p);
5553 gimplify_stmt (to_p, pre_p);
5554 *expr_p = NULL_TREE;
5555 return GS_ALL_DONE;
5558 /* If the value being copied is of variable width, compute the length
5559 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5560 before gimplifying any of the operands so that we can resolve any
5561 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5562 the size of the expression to be copied, not of the destination, so
5563 that is what we must do here. */
5564 maybe_with_size_expr (from_p);
5566 /* As a special case, we have to temporarily allow for assignments
5567 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5568 a toplevel statement, when gimplifying the GENERIC expression
5569 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5570 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5572 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5573 prevent gimplify_expr from trying to create a new temporary for
5574 foo's LHS, we tell it that it should only gimplify until it
5575 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5576 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5577 and all we need to do here is set 'a' to be its LHS. */
5579 /* Gimplify the RHS first for C++17 and bug 71104. */
5580 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5581 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5582 if (ret == GS_ERROR)
5583 return ret;
5585 /* Then gimplify the LHS. */
5586 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5587 twice we have to make sure to gimplify into non-SSA as otherwise
5588 the abnormal edge added later will make those defs not dominate
5589 their uses.
5590 ??? Technically this applies only to the registers used in the
5591 resulting non-register *TO_P. */
5592 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5593 if (saved_into_ssa
5594 && TREE_CODE (*from_p) == CALL_EXPR
5595 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5596 gimplify_ctxp->into_ssa = false;
5597 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5598 gimplify_ctxp->into_ssa = saved_into_ssa;
5599 if (ret == GS_ERROR)
5600 return ret;
5602 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5603 guess for the predicate was wrong. */
5604 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5605 if (final_pred != initial_pred)
5607 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5608 if (ret == GS_ERROR)
5609 return ret;
5612 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5613 size as argument to the call. */
5614 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5616 tree call = TREE_OPERAND (*from_p, 0);
5617 tree vlasize = TREE_OPERAND (*from_p, 1);
5619 if (TREE_CODE (call) == CALL_EXPR
5620 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5622 int nargs = call_expr_nargs (call);
5623 tree type = TREE_TYPE (call);
5624 tree ap = CALL_EXPR_ARG (call, 0);
5625 tree tag = CALL_EXPR_ARG (call, 1);
5626 tree aptag = CALL_EXPR_ARG (call, 2);
5627 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5628 IFN_VA_ARG, type,
5629 nargs + 1, ap, tag,
5630 aptag, vlasize);
5631 TREE_OPERAND (*from_p, 0) = newcall;
5635 /* Now see if the above changed *from_p to something we handle specially. */
5636 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5637 want_value);
5638 if (ret != GS_UNHANDLED)
5639 return ret;
5641 /* If we've got a variable sized assignment between two lvalues (i.e. does
5642 not involve a call), then we can make things a bit more straightforward
5643 by converting the assignment to memcpy or memset. */
5644 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5646 tree from = TREE_OPERAND (*from_p, 0);
5647 tree size = TREE_OPERAND (*from_p, 1);
5649 if (TREE_CODE (from) == CONSTRUCTOR)
5650 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5652 if (is_gimple_addressable (from))
5654 *from_p = from;
5655 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5656 pre_p);
5660 /* Transform partial stores to non-addressable complex variables into
5661 total stores. This allows us to use real instead of virtual operands
5662 for these variables, which improves optimization. */
5663 if ((TREE_CODE (*to_p) == REALPART_EXPR
5664 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5665 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5666 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5668 /* Try to alleviate the effects of the gimplification creating artificial
5669 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5670 make sure not to create DECL_DEBUG_EXPR links across functions. */
5671 if (!gimplify_ctxp->into_ssa
5672 && VAR_P (*from_p)
5673 && DECL_IGNORED_P (*from_p)
5674 && DECL_P (*to_p)
5675 && !DECL_IGNORED_P (*to_p)
5676 && decl_function_context (*to_p) == current_function_decl
5677 && decl_function_context (*from_p) == current_function_decl)
5679 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5680 DECL_NAME (*from_p)
5681 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5682 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5683 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5686 if (want_value && TREE_THIS_VOLATILE (*to_p))
5687 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5689 if (TREE_CODE (*from_p) == CALL_EXPR)
5691 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5692 instead of a GIMPLE_ASSIGN. */
5693 gcall *call_stmt;
5694 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5696 /* Gimplify internal functions created in the FEs. */
5697 int nargs = call_expr_nargs (*from_p), i;
5698 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5699 auto_vec<tree> vargs (nargs);
5701 for (i = 0; i < nargs; i++)
5703 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5704 EXPR_LOCATION (*from_p));
5705 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5707 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5708 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5709 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5711 else
5713 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5714 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5715 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5716 tree fndecl = get_callee_fndecl (*from_p);
5717 if (fndecl
5718 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
5719 && call_expr_nargs (*from_p) == 3)
5720 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5721 CALL_EXPR_ARG (*from_p, 0),
5722 CALL_EXPR_ARG (*from_p, 1),
5723 CALL_EXPR_ARG (*from_p, 2));
5724 else
5726 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5729 notice_special_calls (call_stmt);
5730 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5731 gimple_call_set_lhs (call_stmt, *to_p);
5732 else if (TREE_CODE (*to_p) == SSA_NAME)
5733 /* The above is somewhat premature, avoid ICEing later for a
5734 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5735 ??? This doesn't make it a default-def. */
5736 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5738 assign = call_stmt;
5740 else
5742 assign = gimple_build_assign (*to_p, *from_p);
5743 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5744 if (COMPARISON_CLASS_P (*from_p))
5745 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5748 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5750 /* We should have got an SSA name from the start. */
5751 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5752 || ! gimple_in_ssa_p (cfun));
5755 gimplify_seq_add_stmt (pre_p, assign);
5756 gsi = gsi_last (*pre_p);
5757 maybe_fold_stmt (&gsi);
5759 if (want_value)
5761 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5762 return GS_OK;
5764 else
5765 *expr_p = NULL;
5767 return GS_ALL_DONE;
5770 /* Gimplify a comparison between two variable-sized objects. Do this
5771 with a call to BUILT_IN_MEMCMP. */
5773 static enum gimplify_status
5774 gimplify_variable_sized_compare (tree *expr_p)
5776 location_t loc = EXPR_LOCATION (*expr_p);
5777 tree op0 = TREE_OPERAND (*expr_p, 0);
5778 tree op1 = TREE_OPERAND (*expr_p, 1);
5779 tree t, arg, dest, src, expr;
5781 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5782 arg = unshare_expr (arg);
5783 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5784 src = build_fold_addr_expr_loc (loc, op1);
5785 dest = build_fold_addr_expr_loc (loc, op0);
5786 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5787 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5789 expr
5790 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5791 SET_EXPR_LOCATION (expr, loc);
5792 *expr_p = expr;
5794 return GS_OK;
5797 /* Gimplify a comparison between two aggregate objects of integral scalar
5798 mode as a comparison between the bitwise equivalent scalar values. */
5800 static enum gimplify_status
5801 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5803 location_t loc = EXPR_LOCATION (*expr_p);
5804 tree op0 = TREE_OPERAND (*expr_p, 0);
5805 tree op1 = TREE_OPERAND (*expr_p, 1);
5807 tree type = TREE_TYPE (op0);
5808 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5810 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5811 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5813 *expr_p
5814 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5816 return GS_OK;
5819 /* Gimplify an expression sequence. This function gimplifies each
5820 expression and rewrites the original expression with the last
5821 expression of the sequence in GIMPLE form.
5823 PRE_P points to the list where the side effects for all the
5824 expressions in the sequence will be emitted.
5826 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5828 static enum gimplify_status
5829 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5831 tree t = *expr_p;
5835 tree *sub_p = &TREE_OPERAND (t, 0);
5837 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5838 gimplify_compound_expr (sub_p, pre_p, false);
5839 else
5840 gimplify_stmt (sub_p, pre_p);
5842 t = TREE_OPERAND (t, 1);
5844 while (TREE_CODE (t) == COMPOUND_EXPR);
5846 *expr_p = t;
5847 if (want_value)
5848 return GS_OK;
5849 else
5851 gimplify_stmt (expr_p, pre_p);
5852 return GS_ALL_DONE;
5856 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5857 gimplify. After gimplification, EXPR_P will point to a new temporary
5858 that holds the original value of the SAVE_EXPR node.
5860 PRE_P points to the list where side effects that must happen before
5861 *EXPR_P should be stored. */
5863 static enum gimplify_status
5864 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5866 enum gimplify_status ret = GS_ALL_DONE;
5867 tree val;
5869 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5870 val = TREE_OPERAND (*expr_p, 0);
5872 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5873 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5875 /* The operand may be a void-valued expression. It is
5876 being executed only for its side-effects. */
5877 if (TREE_TYPE (val) == void_type_node)
5879 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5880 is_gimple_stmt, fb_none);
5881 val = NULL;
5883 else
5884 /* The temporary may not be an SSA name as later abnormal and EH
5885 control flow may invalidate use/def domination. When in SSA
5886 form then assume there are no such issues and SAVE_EXPRs only
5887 appear via GENERIC foldings. */
5888 val = get_initialized_tmp_var (val, pre_p, post_p,
5889 gimple_in_ssa_p (cfun));
5891 TREE_OPERAND (*expr_p, 0) = val;
5892 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5895 *expr_p = val;
5897 return ret;
5900 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5902 unary_expr
5903 : ...
5904 | '&' varname
5907 PRE_P points to the list where side effects that must happen before
5908 *EXPR_P should be stored.
5910 POST_P points to the list where side effects that must happen after
5911 *EXPR_P should be stored. */
5913 static enum gimplify_status
5914 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5916 tree expr = *expr_p;
5917 tree op0 = TREE_OPERAND (expr, 0);
5918 enum gimplify_status ret;
5919 location_t loc = EXPR_LOCATION (*expr_p);
5921 switch (TREE_CODE (op0))
5923 case INDIRECT_REF:
5924 do_indirect_ref:
5925 /* Check if we are dealing with an expression of the form '&*ptr'.
5926 While the front end folds away '&*ptr' into 'ptr', these
5927 expressions may be generated internally by the compiler (e.g.,
5928 builtins like __builtin_va_end). */
5929 /* Caution: the silent array decomposition semantics we allow for
5930 ADDR_EXPR means we can't always discard the pair. */
5931 /* Gimplification of the ADDR_EXPR operand may drop
5932 cv-qualification conversions, so make sure we add them if
5933 needed. */
5935 tree op00 = TREE_OPERAND (op0, 0);
5936 tree t_expr = TREE_TYPE (expr);
5937 tree t_op00 = TREE_TYPE (op00);
5939 if (!useless_type_conversion_p (t_expr, t_op00))
5940 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5941 *expr_p = op00;
5942 ret = GS_OK;
5944 break;
5946 case VIEW_CONVERT_EXPR:
5947 /* Take the address of our operand and then convert it to the type of
5948 this ADDR_EXPR.
5950 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5951 all clear. The impact of this transformation is even less clear. */
5953 /* If the operand is a useless conversion, look through it. Doing so
5954 guarantees that the ADDR_EXPR and its operand will remain of the
5955 same type. */
5956 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5957 op0 = TREE_OPERAND (op0, 0);
5959 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5960 build_fold_addr_expr_loc (loc,
5961 TREE_OPERAND (op0, 0)));
5962 ret = GS_OK;
5963 break;
5965 case MEM_REF:
5966 if (integer_zerop (TREE_OPERAND (op0, 1)))
5967 goto do_indirect_ref;
5969 /* fall through */
5971 default:
5972 /* If we see a call to a declared builtin or see its address
5973 being taken (we can unify those cases here) then we can mark
5974 the builtin for implicit generation by GCC. */
5975 if (TREE_CODE (op0) == FUNCTION_DECL
5976 && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
5977 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5978 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5980 /* We use fb_either here because the C frontend sometimes takes
5981 the address of a call that returns a struct; see
5982 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5983 the implied temporary explicit. */
5985 /* Make the operand addressable. */
5986 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5987 is_gimple_addressable, fb_either);
5988 if (ret == GS_ERROR)
5989 break;
5991 /* Then mark it. Beware that it may not be possible to do so directly
5992 if a temporary has been created by the gimplification. */
5993 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5995 op0 = TREE_OPERAND (expr, 0);
5997 /* For various reasons, the gimplification of the expression
5998 may have made a new INDIRECT_REF. */
5999 if (TREE_CODE (op0) == INDIRECT_REF)
6000 goto do_indirect_ref;
6002 mark_addressable (TREE_OPERAND (expr, 0));
6004 /* The FEs may end up building ADDR_EXPRs early on a decl with
6005 an incomplete type. Re-build ADDR_EXPRs in canonical form
6006 here. */
6007 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6008 *expr_p = build_fold_addr_expr (op0);
6010 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6011 recompute_tree_invariant_for_addr_expr (*expr_p);
6013 /* If we re-built the ADDR_EXPR add a conversion to the original type
6014 if required. */
6015 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6016 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6018 break;
6021 return ret;
6024 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6025 value; output operands should be a gimple lvalue. */
6027 static enum gimplify_status
6028 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6030 tree expr;
6031 int noutputs;
6032 const char **oconstraints;
6033 int i;
6034 tree link;
6035 const char *constraint;
6036 bool allows_mem, allows_reg, is_inout;
6037 enum gimplify_status ret, tret;
6038 gasm *stmt;
6039 vec<tree, va_gc> *inputs;
6040 vec<tree, va_gc> *outputs;
6041 vec<tree, va_gc> *clobbers;
6042 vec<tree, va_gc> *labels;
6043 tree link_next;
6045 expr = *expr_p;
6046 noutputs = list_length (ASM_OUTPUTS (expr));
6047 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6049 inputs = NULL;
6050 outputs = NULL;
6051 clobbers = NULL;
6052 labels = NULL;
6054 ret = GS_ALL_DONE;
6055 link_next = NULL_TREE;
6056 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6058 bool ok;
6059 size_t constraint_len;
6061 link_next = TREE_CHAIN (link);
6063 oconstraints[i]
6064 = constraint
6065 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6066 constraint_len = strlen (constraint);
6067 if (constraint_len == 0)
6068 continue;
6070 ok = parse_output_constraint (&constraint, i, 0, 0,
6071 &allows_mem, &allows_reg, &is_inout);
6072 if (!ok)
6074 ret = GS_ERROR;
6075 is_inout = false;
6078 if (!allows_reg && allows_mem)
6079 mark_addressable (TREE_VALUE (link));
6081 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6082 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6083 fb_lvalue | fb_mayfail);
6084 if (tret == GS_ERROR)
6086 error ("invalid lvalue in asm output %d", i);
6087 ret = tret;
6090 /* If the constraint does not allow memory make sure we gimplify
6091 it to a register if it is not already but its base is. This
6092 happens for complex and vector components. */
6093 if (!allows_mem)
6095 tree op = TREE_VALUE (link);
6096 if (! is_gimple_val (op)
6097 && is_gimple_reg_type (TREE_TYPE (op))
6098 && is_gimple_reg (get_base_address (op)))
6100 tree tem = create_tmp_reg (TREE_TYPE (op));
6101 tree ass;
6102 if (is_inout)
6104 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6105 tem, unshare_expr (op));
6106 gimplify_and_add (ass, pre_p);
6108 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6109 gimplify_and_add (ass, post_p);
6111 TREE_VALUE (link) = tem;
6112 tret = GS_OK;
6116 vec_safe_push (outputs, link);
6117 TREE_CHAIN (link) = NULL_TREE;
6119 if (is_inout)
6121 /* An input/output operand. To give the optimizers more
6122 flexibility, split it into separate input and output
6123 operands. */
6124 tree input;
6125 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6126 char buf[11];
6128 /* Turn the in/out constraint into an output constraint. */
6129 char *p = xstrdup (constraint);
6130 p[0] = '=';
6131 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6133 /* And add a matching input constraint. */
6134 if (allows_reg)
6136 sprintf (buf, "%u", i);
6138 /* If there are multiple alternatives in the constraint,
6139 handle each of them individually. Those that allow register
6140 will be replaced with operand number, the others will stay
6141 unchanged. */
6142 if (strchr (p, ',') != NULL)
6144 size_t len = 0, buflen = strlen (buf);
6145 char *beg, *end, *str, *dst;
6147 for (beg = p + 1;;)
6149 end = strchr (beg, ',');
6150 if (end == NULL)
6151 end = strchr (beg, '\0');
6152 if ((size_t) (end - beg) < buflen)
6153 len += buflen + 1;
6154 else
6155 len += end - beg + 1;
6156 if (*end)
6157 beg = end + 1;
6158 else
6159 break;
6162 str = (char *) alloca (len);
6163 for (beg = p + 1, dst = str;;)
6165 const char *tem;
6166 bool mem_p, reg_p, inout_p;
6168 end = strchr (beg, ',');
6169 if (end)
6170 *end = '\0';
6171 beg[-1] = '=';
6172 tem = beg - 1;
6173 parse_output_constraint (&tem, i, 0, 0,
6174 &mem_p, &reg_p, &inout_p);
6175 if (dst != str)
6176 *dst++ = ',';
6177 if (reg_p)
6179 memcpy (dst, buf, buflen);
6180 dst += buflen;
6182 else
6184 if (end)
6185 len = end - beg;
6186 else
6187 len = strlen (beg);
6188 memcpy (dst, beg, len);
6189 dst += len;
6191 if (end)
6192 beg = end + 1;
6193 else
6194 break;
6196 *dst = '\0';
6197 input = build_string (dst - str, str);
6199 else
6200 input = build_string (strlen (buf), buf);
6202 else
6203 input = build_string (constraint_len - 1, constraint + 1);
6205 free (p);
6207 input = build_tree_list (build_tree_list (NULL_TREE, input),
6208 unshare_expr (TREE_VALUE (link)));
6209 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6213 link_next = NULL_TREE;
6214 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6216 link_next = TREE_CHAIN (link);
6217 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6218 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6219 oconstraints, &allows_mem, &allows_reg);
6221 /* If we can't make copies, we can only accept memory. */
6222 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6224 if (allows_mem)
6225 allows_reg = 0;
6226 else
6228 error ("impossible constraint in %<asm%>");
6229 error ("non-memory input %d must stay in memory", i);
6230 return GS_ERROR;
6234 /* If the operand is a memory input, it should be an lvalue. */
6235 if (!allows_reg && allows_mem)
6237 tree inputv = TREE_VALUE (link);
6238 STRIP_NOPS (inputv);
6239 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6240 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6241 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6242 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6243 || TREE_CODE (inputv) == MODIFY_EXPR)
6244 TREE_VALUE (link) = error_mark_node;
6245 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6246 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6247 if (tret != GS_ERROR)
6249 /* Unlike output operands, memory inputs are not guaranteed
6250 to be lvalues by the FE, and while the expressions are
6251 marked addressable there, if it is e.g. a statement
6252 expression, temporaries in it might not end up being
6253 addressable. They might be already used in the IL and thus
6254 it is too late to make them addressable now though. */
6255 tree x = TREE_VALUE (link);
6256 while (handled_component_p (x))
6257 x = TREE_OPERAND (x, 0);
6258 if (TREE_CODE (x) == MEM_REF
6259 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6260 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6261 if ((VAR_P (x)
6262 || TREE_CODE (x) == PARM_DECL
6263 || TREE_CODE (x) == RESULT_DECL)
6264 && !TREE_ADDRESSABLE (x)
6265 && is_gimple_reg (x))
6267 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6268 input_location), 0,
6269 "memory input %d is not directly addressable",
6271 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6274 mark_addressable (TREE_VALUE (link));
6275 if (tret == GS_ERROR)
6277 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6278 "memory input %d is not directly addressable", i);
6279 ret = tret;
6282 else
6284 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6285 is_gimple_asm_val, fb_rvalue);
6286 if (tret == GS_ERROR)
6287 ret = tret;
6290 TREE_CHAIN (link) = NULL_TREE;
6291 vec_safe_push (inputs, link);
6294 link_next = NULL_TREE;
6295 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6297 link_next = TREE_CHAIN (link);
6298 TREE_CHAIN (link) = NULL_TREE;
6299 vec_safe_push (clobbers, link);
6302 link_next = NULL_TREE;
6303 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6305 link_next = TREE_CHAIN (link);
6306 TREE_CHAIN (link) = NULL_TREE;
6307 vec_safe_push (labels, link);
6310 /* Do not add ASMs with errors to the gimple IL stream. */
6311 if (ret != GS_ERROR)
6313 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6314 inputs, outputs, clobbers, labels);
6316 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6317 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6319 gimplify_seq_add_stmt (pre_p, stmt);
6322 return ret;
6325 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6326 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6327 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6328 return to this function.
6330 FIXME should we complexify the prequeue handling instead? Or use flags
6331 for all the cleanups and let the optimizer tighten them up? The current
6332 code seems pretty fragile; it will break on a cleanup within any
6333 non-conditional nesting. But any such nesting would be broken, anyway;
6334 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6335 and continues out of it. We can do that at the RTL level, though, so
6336 having an optimizer to tighten up try/finally regions would be a Good
6337 Thing. */
6339 static enum gimplify_status
6340 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6342 gimple_stmt_iterator iter;
6343 gimple_seq body_sequence = NULL;
6345 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6347 /* We only care about the number of conditions between the innermost
6348 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6349 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6350 int old_conds = gimplify_ctxp->conditions;
6351 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6352 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6353 gimplify_ctxp->conditions = 0;
6354 gimplify_ctxp->conditional_cleanups = NULL;
6355 gimplify_ctxp->in_cleanup_point_expr = true;
6357 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6359 gimplify_ctxp->conditions = old_conds;
6360 gimplify_ctxp->conditional_cleanups = old_cleanups;
6361 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6363 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6365 gimple *wce = gsi_stmt (iter);
6367 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6369 if (gsi_one_before_end_p (iter))
6371 /* Note that gsi_insert_seq_before and gsi_remove do not
6372 scan operands, unlike some other sequence mutators. */
6373 if (!gimple_wce_cleanup_eh_only (wce))
6374 gsi_insert_seq_before_without_update (&iter,
6375 gimple_wce_cleanup (wce),
6376 GSI_SAME_STMT);
6377 gsi_remove (&iter, true);
6378 break;
6380 else
6382 gtry *gtry;
6383 gimple_seq seq;
6384 enum gimple_try_flags kind;
6386 if (gimple_wce_cleanup_eh_only (wce))
6387 kind = GIMPLE_TRY_CATCH;
6388 else
6389 kind = GIMPLE_TRY_FINALLY;
6390 seq = gsi_split_seq_after (iter);
6392 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6393 /* Do not use gsi_replace here, as it may scan operands.
6394 We want to do a simple structural modification only. */
6395 gsi_set_stmt (&iter, gtry);
6396 iter = gsi_start (gtry->eval);
6399 else
6400 gsi_next (&iter);
6403 gimplify_seq_add_seq (pre_p, body_sequence);
6404 if (temp)
6406 *expr_p = temp;
6407 return GS_OK;
6409 else
6411 *expr_p = NULL;
6412 return GS_ALL_DONE;
6416 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6417 is the cleanup action required. EH_ONLY is true if the cleanup should
6418 only be executed if an exception is thrown, not on normal exit.
6419 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6420 only valid for clobbers. */
6422 static void
6423 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6424 bool force_uncond = false)
6426 gimple *wce;
6427 gimple_seq cleanup_stmts = NULL;
6429 /* Errors can result in improperly nested cleanups. Which results in
6430 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6431 if (seen_error ())
6432 return;
6434 if (gimple_conditional_context ())
6436 /* If we're in a conditional context, this is more complex. We only
6437 want to run the cleanup if we actually ran the initialization that
6438 necessitates it, but we want to run it after the end of the
6439 conditional context. So we wrap the try/finally around the
6440 condition and use a flag to determine whether or not to actually
6441 run the destructor. Thus
6443 test ? f(A()) : 0
6445 becomes (approximately)
6447 flag = 0;
6448 try {
6449 if (test) { A::A(temp); flag = 1; val = f(temp); }
6450 else { val = 0; }
6451 } finally {
6452 if (flag) A::~A(temp);
6456 if (force_uncond)
6458 gimplify_stmt (&cleanup, &cleanup_stmts);
6459 wce = gimple_build_wce (cleanup_stmts);
6460 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6462 else
6464 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6465 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6466 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6468 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6469 gimplify_stmt (&cleanup, &cleanup_stmts);
6470 wce = gimple_build_wce (cleanup_stmts);
6472 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6473 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6474 gimplify_seq_add_stmt (pre_p, ftrue);
6476 /* Because of this manipulation, and the EH edges that jump
6477 threading cannot redirect, the temporary (VAR) will appear
6478 to be used uninitialized. Don't warn. */
6479 TREE_NO_WARNING (var) = 1;
6482 else
6484 gimplify_stmt (&cleanup, &cleanup_stmts);
6485 wce = gimple_build_wce (cleanup_stmts);
6486 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6487 gimplify_seq_add_stmt (pre_p, wce);
6491 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6493 static enum gimplify_status
6494 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6496 tree targ = *expr_p;
6497 tree temp = TARGET_EXPR_SLOT (targ);
6498 tree init = TARGET_EXPR_INITIAL (targ);
6499 enum gimplify_status ret;
6501 bool unpoison_empty_seq = false;
6502 gimple_stmt_iterator unpoison_it;
6504 if (init)
6506 tree cleanup = NULL_TREE;
6508 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6509 to the temps list. Handle also variable length TARGET_EXPRs. */
6510 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6512 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6513 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6514 gimplify_vla_decl (temp, pre_p);
6516 else
6518 /* Save location where we need to place unpoisoning. It's possible
6519 that a variable will be converted to needs_to_live_in_memory. */
6520 unpoison_it = gsi_last (*pre_p);
6521 unpoison_empty_seq = gsi_end_p (unpoison_it);
6523 gimple_add_tmp_var (temp);
6526 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6527 expression is supposed to initialize the slot. */
6528 if (VOID_TYPE_P (TREE_TYPE (init)))
6529 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6530 else
6532 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6533 init = init_expr;
6534 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6535 init = NULL;
6536 ggc_free (init_expr);
6538 if (ret == GS_ERROR)
6540 /* PR c++/28266 Make sure this is expanded only once. */
6541 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6542 return GS_ERROR;
6544 if (init)
6545 gimplify_and_add (init, pre_p);
6547 /* If needed, push the cleanup for the temp. */
6548 if (TARGET_EXPR_CLEANUP (targ))
6550 if (CLEANUP_EH_ONLY (targ))
6551 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6552 CLEANUP_EH_ONLY (targ), pre_p);
6553 else
6554 cleanup = TARGET_EXPR_CLEANUP (targ);
6557 /* Add a clobber for the temporary going out of scope, like
6558 gimplify_bind_expr. */
6559 if (gimplify_ctxp->in_cleanup_point_expr
6560 && needs_to_live_in_memory (temp))
6562 if (flag_stack_reuse == SR_ALL)
6564 tree clobber = build_clobber (TREE_TYPE (temp));
6565 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6566 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6568 if (asan_poisoned_variables
6569 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6570 && dbg_cnt (asan_use_after_scope)
6571 && !gimplify_omp_ctxp)
6573 tree asan_cleanup = build_asan_poison_call_expr (temp);
6574 if (asan_cleanup)
6576 if (unpoison_empty_seq)
6577 unpoison_it = gsi_start (*pre_p);
6579 asan_poison_variable (temp, false, &unpoison_it,
6580 unpoison_empty_seq);
6581 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6585 if (cleanup)
6586 gimple_push_cleanup (temp, cleanup, false, pre_p);
6588 /* Only expand this once. */
6589 TREE_OPERAND (targ, 3) = init;
6590 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6592 else
6593 /* We should have expanded this before. */
6594 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6596 *expr_p = temp;
6597 return GS_OK;
6600 /* Gimplification of expression trees. */
6602 /* Gimplify an expression which appears at statement context. The
6603 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6604 NULL, a new sequence is allocated.
6606 Return true if we actually added a statement to the queue. */
6608 bool
6609 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6611 gimple_seq_node last;
6613 last = gimple_seq_last (*seq_p);
6614 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6615 return last != gimple_seq_last (*seq_p);
6618 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6619 to CTX. If entries already exist, force them to be some flavor of private.
6620 If there is no enclosing parallel, do nothing. */
6622 void
6623 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6625 splay_tree_node n;
6627 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6628 return;
6632 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6633 if (n != NULL)
6635 if (n->value & GOVD_SHARED)
6636 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6637 else if (n->value & GOVD_MAP)
6638 n->value |= GOVD_MAP_TO_ONLY;
6639 else
6640 return;
6642 else if ((ctx->region_type & ORT_TARGET) != 0)
6644 if (ctx->target_map_scalars_firstprivate)
6645 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6646 else
6647 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6649 else if (ctx->region_type != ORT_WORKSHARE
6650 && ctx->region_type != ORT_SIMD
6651 && ctx->region_type != ORT_ACC
6652 && !(ctx->region_type & ORT_TARGET_DATA))
6653 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6655 ctx = ctx->outer_context;
6657 while (ctx);
6660 /* Similarly for each of the type sizes of TYPE. */
6662 static void
6663 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6665 if (type == NULL || type == error_mark_node)
6666 return;
6667 type = TYPE_MAIN_VARIANT (type);
6669 if (ctx->privatized_types->add (type))
6670 return;
6672 switch (TREE_CODE (type))
6674 case INTEGER_TYPE:
6675 case ENUMERAL_TYPE:
6676 case BOOLEAN_TYPE:
6677 case REAL_TYPE:
6678 case FIXED_POINT_TYPE:
6679 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6680 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6681 break;
6683 case ARRAY_TYPE:
6684 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6685 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6686 break;
6688 case RECORD_TYPE:
6689 case UNION_TYPE:
6690 case QUAL_UNION_TYPE:
6692 tree field;
6693 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6694 if (TREE_CODE (field) == FIELD_DECL)
6696 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6697 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6700 break;
6702 case POINTER_TYPE:
6703 case REFERENCE_TYPE:
6704 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6705 break;
6707 default:
6708 break;
6711 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6712 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6713 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6716 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6718 static void
6719 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6721 splay_tree_node n;
6722 unsigned int nflags;
6723 tree t;
6725 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6726 return;
6728 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6729 there are constructors involved somewhere. Exception is a shared clause,
6730 there is nothing privatized in that case. */
6731 if ((flags & GOVD_SHARED) == 0
6732 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6733 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6734 flags |= GOVD_SEEN;
6736 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6737 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6739 /* We shouldn't be re-adding the decl with the same data
6740 sharing class. */
6741 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6742 nflags = n->value | flags;
6743 /* The only combination of data sharing classes we should see is
6744 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6745 reduction variables to be used in data sharing clauses. */
6746 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6747 || ((nflags & GOVD_DATA_SHARE_CLASS)
6748 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6749 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6750 n->value = nflags;
6751 return;
6754 /* When adding a variable-sized variable, we have to handle all sorts
6755 of additional bits of data: the pointer replacement variable, and
6756 the parameters of the type. */
6757 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6759 /* Add the pointer replacement variable as PRIVATE if the variable
6760 replacement is private, else FIRSTPRIVATE since we'll need the
6761 address of the original variable either for SHARED, or for the
6762 copy into or out of the context. */
6763 if (!(flags & GOVD_LOCAL))
6765 if (flags & GOVD_MAP)
6766 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6767 else if (flags & GOVD_PRIVATE)
6768 nflags = GOVD_PRIVATE;
6769 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6770 && (flags & GOVD_FIRSTPRIVATE))
6771 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6772 else
6773 nflags = GOVD_FIRSTPRIVATE;
6774 nflags |= flags & GOVD_SEEN;
6775 t = DECL_VALUE_EXPR (decl);
6776 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6777 t = TREE_OPERAND (t, 0);
6778 gcc_assert (DECL_P (t));
6779 omp_add_variable (ctx, t, nflags);
6782 /* Add all of the variable and type parameters (which should have
6783 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6784 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6785 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6786 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6788 /* The variable-sized variable itself is never SHARED, only some form
6789 of PRIVATE. The sharing would take place via the pointer variable
6790 which we remapped above. */
6791 if (flags & GOVD_SHARED)
6792 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6793 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6795 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6796 alloca statement we generate for the variable, so make sure it
6797 is available. This isn't automatically needed for the SHARED
6798 case, since we won't be allocating local storage then.
6799 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6800 in this case omp_notice_variable will be called later
6801 on when it is gimplified. */
6802 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6803 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6804 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6806 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6807 && lang_hooks.decls.omp_privatize_by_reference (decl))
6809 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6811 /* Similar to the direct variable sized case above, we'll need the
6812 size of references being privatized. */
6813 if ((flags & GOVD_SHARED) == 0)
6815 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6816 if (DECL_P (t))
6817 omp_notice_variable (ctx, t, true);
6821 if (n != NULL)
6822 n->value |= flags;
6823 else
6824 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6826 /* For reductions clauses in OpenACC loop directives, by default create a
6827 copy clause on the enclosing parallel construct for carrying back the
6828 results. */
6829 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6831 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6832 while (outer_ctx)
6834 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6835 if (n != NULL)
6837 /* Ignore local variables and explicitly declared clauses. */
6838 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6839 break;
6840 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6842 /* According to the OpenACC spec, such a reduction variable
6843 should already have a copy map on a kernels construct,
6844 verify that here. */
6845 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6846 && (n->value & GOVD_MAP));
6848 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6850 /* Remove firstprivate and make it a copy map. */
6851 n->value &= ~GOVD_FIRSTPRIVATE;
6852 n->value |= GOVD_MAP;
6855 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6857 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6858 GOVD_MAP | GOVD_SEEN);
6859 break;
6861 outer_ctx = outer_ctx->outer_context;
6866 /* Notice a threadprivate variable DECL used in OMP context CTX.
6867 This just prints out diagnostics about threadprivate variable uses
6868 in untied tasks. If DECL2 is non-NULL, prevent this warning
6869 on that variable. */
6871 static bool
6872 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6873 tree decl2)
6875 splay_tree_node n;
6876 struct gimplify_omp_ctx *octx;
6878 for (octx = ctx; octx; octx = octx->outer_context)
6879 if ((octx->region_type & ORT_TARGET) != 0)
6881 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6882 if (n == NULL)
6884 error ("threadprivate variable %qE used in target region",
6885 DECL_NAME (decl));
6886 error_at (octx->location, "enclosing target region");
6887 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6889 if (decl2)
6890 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6893 if (ctx->region_type != ORT_UNTIED_TASK)
6894 return false;
6895 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6896 if (n == NULL)
6898 error ("threadprivate variable %qE used in untied task",
6899 DECL_NAME (decl));
6900 error_at (ctx->location, "enclosing task");
6901 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6903 if (decl2)
6904 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6905 return false;
6908 /* Return true if global var DECL is device resident. */
6910 static bool
6911 device_resident_p (tree decl)
6913 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6915 if (!attr)
6916 return false;
6918 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6920 tree c = TREE_VALUE (t);
6921 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6922 return true;
6925 return false;
6928 /* Return true if DECL has an ACC DECLARE attribute. */
6930 static bool
6931 is_oacc_declared (tree decl)
6933 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6934 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6935 return declared != NULL_TREE;
6938 /* Determine outer default flags for DECL mentioned in an OMP region
6939 but not declared in an enclosing clause.
6941 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6942 remapped firstprivate instead of shared. To some extent this is
6943 addressed in omp_firstprivatize_type_sizes, but not
6944 effectively. */
6946 static unsigned
6947 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6948 bool in_code, unsigned flags)
6950 enum omp_clause_default_kind default_kind = ctx->default_kind;
6951 enum omp_clause_default_kind kind;
6953 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6954 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6955 default_kind = kind;
6957 switch (default_kind)
6959 case OMP_CLAUSE_DEFAULT_NONE:
6961 const char *rtype;
6963 if (ctx->region_type & ORT_PARALLEL)
6964 rtype = "parallel";
6965 else if (ctx->region_type & ORT_TASK)
6966 rtype = "task";
6967 else if (ctx->region_type & ORT_TEAMS)
6968 rtype = "teams";
6969 else
6970 gcc_unreachable ();
6972 error ("%qE not specified in enclosing %qs",
6973 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
6974 error_at (ctx->location, "enclosing %qs", rtype);
6976 /* FALLTHRU */
6977 case OMP_CLAUSE_DEFAULT_SHARED:
6978 flags |= GOVD_SHARED;
6979 break;
6980 case OMP_CLAUSE_DEFAULT_PRIVATE:
6981 flags |= GOVD_PRIVATE;
6982 break;
6983 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6984 flags |= GOVD_FIRSTPRIVATE;
6985 break;
6986 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6987 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6988 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6989 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
6991 omp_notice_variable (octx, decl, in_code);
6992 for (; octx; octx = octx->outer_context)
6994 splay_tree_node n2;
6996 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6997 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
6998 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
6999 continue;
7000 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7002 flags |= GOVD_FIRSTPRIVATE;
7003 goto found_outer;
7005 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7007 flags |= GOVD_SHARED;
7008 goto found_outer;
7013 if (TREE_CODE (decl) == PARM_DECL
7014 || (!is_global_var (decl)
7015 && DECL_CONTEXT (decl) == current_function_decl))
7016 flags |= GOVD_FIRSTPRIVATE;
7017 else
7018 flags |= GOVD_SHARED;
7019 found_outer:
7020 break;
7022 default:
7023 gcc_unreachable ();
7026 return flags;
7030 /* Determine outer default flags for DECL mentioned in an OACC region
7031 but not declared in an enclosing clause. */
7033 static unsigned
7034 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7036 const char *rkind;
7037 bool on_device = false;
7038 bool declared = is_oacc_declared (decl);
7039 tree type = TREE_TYPE (decl);
7041 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7042 type = TREE_TYPE (type);
7044 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7045 && is_global_var (decl)
7046 && device_resident_p (decl))
7048 on_device = true;
7049 flags |= GOVD_MAP_TO_ONLY;
7052 switch (ctx->region_type)
7054 case ORT_ACC_KERNELS:
7055 rkind = "kernels";
7057 if (AGGREGATE_TYPE_P (type))
7059 /* Aggregates default to 'present_or_copy', or 'present'. */
7060 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7061 flags |= GOVD_MAP;
7062 else
7063 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7065 else
7066 /* Scalars default to 'copy'. */
7067 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7069 break;
7071 case ORT_ACC_PARALLEL:
7072 rkind = "parallel";
7074 if (on_device || declared)
7075 flags |= GOVD_MAP;
7076 else if (AGGREGATE_TYPE_P (type))
7078 /* Aggregates default to 'present_or_copy', or 'present'. */
7079 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7080 flags |= GOVD_MAP;
7081 else
7082 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7084 else
7085 /* Scalars default to 'firstprivate'. */
7086 flags |= GOVD_FIRSTPRIVATE;
7088 break;
7090 default:
7091 gcc_unreachable ();
7094 if (DECL_ARTIFICIAL (decl))
7095 ; /* We can get compiler-generated decls, and should not complain
7096 about them. */
7097 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7099 error ("%qE not specified in enclosing OpenACC %qs construct",
7100 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7101 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7103 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7104 ; /* Handled above. */
7105 else
7106 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7108 return flags;
7111 /* Record the fact that DECL was used within the OMP context CTX.
7112 IN_CODE is true when real code uses DECL, and false when we should
7113 merely emit default(none) errors. Return true if DECL is going to
7114 be remapped and thus DECL shouldn't be gimplified into its
7115 DECL_VALUE_EXPR (if any). */
7117 static bool
7118 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7120 splay_tree_node n;
7121 unsigned flags = in_code ? GOVD_SEEN : 0;
7122 bool ret = false, shared;
7124 if (error_operand_p (decl))
7125 return false;
7127 if (ctx->region_type == ORT_NONE)
7128 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7130 if (is_global_var (decl))
7132 /* Threadprivate variables are predetermined. */
7133 if (DECL_THREAD_LOCAL_P (decl))
7134 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7136 if (DECL_HAS_VALUE_EXPR_P (decl))
7138 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7140 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7141 return omp_notice_threadprivate_variable (ctx, decl, value);
7144 if (gimplify_omp_ctxp->outer_context == NULL
7145 && VAR_P (decl)
7146 && oacc_get_fn_attrib (current_function_decl))
7148 location_t loc = DECL_SOURCE_LOCATION (decl);
7150 if (lookup_attribute ("omp declare target link",
7151 DECL_ATTRIBUTES (decl)))
7153 error_at (loc,
7154 "%qE with %<link%> clause used in %<routine%> function",
7155 DECL_NAME (decl));
7156 return false;
7158 else if (!lookup_attribute ("omp declare target",
7159 DECL_ATTRIBUTES (decl)))
7161 error_at (loc,
7162 "%qE requires a %<declare%> directive for use "
7163 "in a %<routine%> function", DECL_NAME (decl));
7164 return false;
7169 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7170 if ((ctx->region_type & ORT_TARGET) != 0)
7172 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7173 if (n == NULL)
7175 unsigned nflags = flags;
7176 if (ctx->target_map_pointers_as_0len_arrays
7177 || ctx->target_map_scalars_firstprivate)
7179 bool is_declare_target = false;
7180 bool is_scalar = false;
7181 if (is_global_var (decl)
7182 && varpool_node::get_create (decl)->offloadable)
7184 struct gimplify_omp_ctx *octx;
7185 for (octx = ctx->outer_context;
7186 octx; octx = octx->outer_context)
7188 n = splay_tree_lookup (octx->variables,
7189 (splay_tree_key)decl);
7190 if (n
7191 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7192 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7193 break;
7195 is_declare_target = octx == NULL;
7197 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7198 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7199 if (is_declare_target)
7201 else if (ctx->target_map_pointers_as_0len_arrays
7202 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7203 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7204 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7205 == POINTER_TYPE)))
7206 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7207 else if (is_scalar)
7208 nflags |= GOVD_FIRSTPRIVATE;
7211 struct gimplify_omp_ctx *octx = ctx->outer_context;
7212 if ((ctx->region_type & ORT_ACC) && octx)
7214 /* Look in outer OpenACC contexts, to see if there's a
7215 data attribute for this variable. */
7216 omp_notice_variable (octx, decl, in_code);
7218 for (; octx; octx = octx->outer_context)
7220 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7221 break;
7222 splay_tree_node n2
7223 = splay_tree_lookup (octx->variables,
7224 (splay_tree_key) decl);
7225 if (n2)
7227 if (octx->region_type == ORT_ACC_HOST_DATA)
7228 error ("variable %qE declared in enclosing "
7229 "%<host_data%> region", DECL_NAME (decl));
7230 nflags |= GOVD_MAP;
7231 if (octx->region_type == ORT_ACC_DATA
7232 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7233 nflags |= GOVD_MAP_0LEN_ARRAY;
7234 goto found_outer;
7240 tree type = TREE_TYPE (decl);
7242 if (nflags == flags
7243 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7244 && lang_hooks.decls.omp_privatize_by_reference (decl))
7245 type = TREE_TYPE (type);
7246 if (nflags == flags
7247 && !lang_hooks.types.omp_mappable_type (type))
7249 error ("%qD referenced in target region does not have "
7250 "a mappable type", decl);
7251 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7253 else if (nflags == flags)
7255 if ((ctx->region_type & ORT_ACC) != 0)
7256 nflags = oacc_default_clause (ctx, decl, flags);
7257 else
7258 nflags |= GOVD_MAP;
7261 found_outer:
7262 omp_add_variable (ctx, decl, nflags);
7264 else
7266 /* If nothing changed, there's nothing left to do. */
7267 if ((n->value & flags) == flags)
7268 return ret;
7269 flags |= n->value;
7270 n->value = flags;
7272 goto do_outer;
7275 if (n == NULL)
7277 if (ctx->region_type == ORT_WORKSHARE
7278 || ctx->region_type == ORT_SIMD
7279 || ctx->region_type == ORT_ACC
7280 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7281 goto do_outer;
7283 flags = omp_default_clause (ctx, decl, in_code, flags);
7285 if ((flags & GOVD_PRIVATE)
7286 && lang_hooks.decls.omp_private_outer_ref (decl))
7287 flags |= GOVD_PRIVATE_OUTER_REF;
7289 omp_add_variable (ctx, decl, flags);
7291 shared = (flags & GOVD_SHARED) != 0;
7292 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7293 goto do_outer;
7296 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7297 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7298 && DECL_SIZE (decl))
7300 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7302 splay_tree_node n2;
7303 tree t = DECL_VALUE_EXPR (decl);
7304 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7305 t = TREE_OPERAND (t, 0);
7306 gcc_assert (DECL_P (t));
7307 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7308 n2->value |= GOVD_SEEN;
7310 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7311 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7312 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7313 != INTEGER_CST))
7315 splay_tree_node n2;
7316 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7317 gcc_assert (DECL_P (t));
7318 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7319 if (n2)
7320 omp_notice_variable (ctx, t, true);
7324 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7325 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7327 /* If nothing changed, there's nothing left to do. */
7328 if ((n->value & flags) == flags)
7329 return ret;
7330 flags |= n->value;
7331 n->value = flags;
7333 do_outer:
7334 /* If the variable is private in the current context, then we don't
7335 need to propagate anything to an outer context. */
7336 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7337 return ret;
7338 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7339 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7340 return ret;
7341 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7342 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7343 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7344 return ret;
7345 if (ctx->outer_context
7346 && omp_notice_variable (ctx->outer_context, decl, in_code))
7347 return true;
7348 return ret;
7351 /* Verify that DECL is private within CTX. If there's specific information
7352 to the contrary in the innermost scope, generate an error. */
7354 static bool
7355 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7357 splay_tree_node n;
7359 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7360 if (n != NULL)
7362 if (n->value & GOVD_SHARED)
7364 if (ctx == gimplify_omp_ctxp)
7366 if (simd)
7367 error ("iteration variable %qE is predetermined linear",
7368 DECL_NAME (decl));
7369 else
7370 error ("iteration variable %qE should be private",
7371 DECL_NAME (decl));
7372 n->value = GOVD_PRIVATE;
7373 return true;
7375 else
7376 return false;
7378 else if ((n->value & GOVD_EXPLICIT) != 0
7379 && (ctx == gimplify_omp_ctxp
7380 || (ctx->region_type == ORT_COMBINED_PARALLEL
7381 && gimplify_omp_ctxp->outer_context == ctx)))
7383 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7384 error ("iteration variable %qE should not be firstprivate",
7385 DECL_NAME (decl));
7386 else if ((n->value & GOVD_REDUCTION) != 0)
7387 error ("iteration variable %qE should not be reduction",
7388 DECL_NAME (decl));
7389 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7390 error ("iteration variable %qE should not be linear",
7391 DECL_NAME (decl));
7392 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7393 error ("iteration variable %qE should not be lastprivate",
7394 DECL_NAME (decl));
7395 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7396 error ("iteration variable %qE should not be private",
7397 DECL_NAME (decl));
7398 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7399 error ("iteration variable %qE is predetermined linear",
7400 DECL_NAME (decl));
7402 return (ctx == gimplify_omp_ctxp
7403 || (ctx->region_type == ORT_COMBINED_PARALLEL
7404 && gimplify_omp_ctxp->outer_context == ctx));
7407 if (ctx->region_type != ORT_WORKSHARE
7408 && ctx->region_type != ORT_SIMD
7409 && ctx->region_type != ORT_ACC)
7410 return false;
7411 else if (ctx->outer_context)
7412 return omp_is_private (ctx->outer_context, decl, simd);
7413 return false;
7416 /* Return true if DECL is private within a parallel region
7417 that binds to the current construct's context or in parallel
7418 region's REDUCTION clause. */
7420 static bool
7421 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7423 splay_tree_node n;
7427 ctx = ctx->outer_context;
7428 if (ctx == NULL)
7430 if (is_global_var (decl))
7431 return false;
7433 /* References might be private, but might be shared too,
7434 when checking for copyprivate, assume they might be
7435 private, otherwise assume they might be shared. */
7436 if (copyprivate)
7437 return true;
7439 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7440 return false;
7442 /* Treat C++ privatized non-static data members outside
7443 of the privatization the same. */
7444 if (omp_member_access_dummy_var (decl))
7445 return false;
7447 return true;
7450 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7452 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7453 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7454 continue;
7456 if (n != NULL)
7458 if ((n->value & GOVD_LOCAL) != 0
7459 && omp_member_access_dummy_var (decl))
7460 return false;
7461 return (n->value & GOVD_SHARED) == 0;
7464 while (ctx->region_type == ORT_WORKSHARE
7465 || ctx->region_type == ORT_SIMD
7466 || ctx->region_type == ORT_ACC);
7467 return false;
7470 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7472 static tree
7473 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7475 tree t = *tp;
7477 /* If this node has been visited, unmark it and keep looking. */
7478 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7479 return t;
7481 if (IS_TYPE_OR_DECL_P (t))
7482 *walk_subtrees = 0;
7483 return NULL_TREE;
7486 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7487 and previous omp contexts. */
7489 static void
7490 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7491 enum omp_region_type region_type,
7492 enum tree_code code)
7494 struct gimplify_omp_ctx *ctx, *outer_ctx;
7495 tree c;
7496 hash_map<tree, tree> *struct_map_to_clause = NULL;
7497 tree *prev_list_p = NULL;
7499 ctx = new_omp_context (region_type);
7500 outer_ctx = ctx->outer_context;
7501 if (code == OMP_TARGET)
7503 if (!lang_GNU_Fortran ())
7504 ctx->target_map_pointers_as_0len_arrays = true;
7505 ctx->target_map_scalars_firstprivate = true;
7507 if (!lang_GNU_Fortran ())
7508 switch (code)
7510 case OMP_TARGET:
7511 case OMP_TARGET_DATA:
7512 case OMP_TARGET_ENTER_DATA:
7513 case OMP_TARGET_EXIT_DATA:
7514 case OACC_DECLARE:
7515 case OACC_HOST_DATA:
7516 case OACC_PARALLEL:
7517 case OACC_KERNELS:
7518 ctx->target_firstprivatize_array_bases = true;
7519 default:
7520 break;
7523 while ((c = *list_p) != NULL)
7525 bool remove = false;
7526 bool notice_outer = true;
7527 const char *check_non_private = NULL;
7528 unsigned int flags;
7529 tree decl;
7531 switch (OMP_CLAUSE_CODE (c))
7533 case OMP_CLAUSE_PRIVATE:
7534 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7535 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7537 flags |= GOVD_PRIVATE_OUTER_REF;
7538 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7540 else
7541 notice_outer = false;
7542 goto do_add;
7543 case OMP_CLAUSE_SHARED:
7544 flags = GOVD_SHARED | GOVD_EXPLICIT;
7545 goto do_add;
7546 case OMP_CLAUSE_FIRSTPRIVATE:
7547 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7548 check_non_private = "firstprivate";
7549 goto do_add;
7550 case OMP_CLAUSE_LASTPRIVATE:
7551 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7552 check_non_private = "lastprivate";
7553 decl = OMP_CLAUSE_DECL (c);
7554 if (error_operand_p (decl))
7555 goto do_add;
7556 else if (outer_ctx
7557 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7558 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7559 && splay_tree_lookup (outer_ctx->variables,
7560 (splay_tree_key) decl) == NULL)
7562 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7563 if (outer_ctx->outer_context)
7564 omp_notice_variable (outer_ctx->outer_context, decl, true);
7566 else if (outer_ctx
7567 && (outer_ctx->region_type & ORT_TASK) != 0
7568 && outer_ctx->combined_loop
7569 && splay_tree_lookup (outer_ctx->variables,
7570 (splay_tree_key) decl) == NULL)
7572 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7573 if (outer_ctx->outer_context)
7574 omp_notice_variable (outer_ctx->outer_context, decl, true);
7576 else if (outer_ctx
7577 && (outer_ctx->region_type == ORT_WORKSHARE
7578 || outer_ctx->region_type == ORT_ACC)
7579 && outer_ctx->combined_loop
7580 && splay_tree_lookup (outer_ctx->variables,
7581 (splay_tree_key) decl) == NULL
7582 && !omp_check_private (outer_ctx, decl, false))
7584 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7585 if (outer_ctx->outer_context
7586 && (outer_ctx->outer_context->region_type
7587 == ORT_COMBINED_PARALLEL)
7588 && splay_tree_lookup (outer_ctx->outer_context->variables,
7589 (splay_tree_key) decl) == NULL)
7591 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7592 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7593 if (octx->outer_context)
7595 octx = octx->outer_context;
7596 if (octx->region_type == ORT_WORKSHARE
7597 && octx->combined_loop
7598 && splay_tree_lookup (octx->variables,
7599 (splay_tree_key) decl) == NULL
7600 && !omp_check_private (octx, decl, false))
7602 omp_add_variable (octx, decl,
7603 GOVD_LASTPRIVATE | GOVD_SEEN);
7604 octx = octx->outer_context;
7605 if (octx
7606 && octx->region_type == ORT_COMBINED_TEAMS
7607 && (splay_tree_lookup (octx->variables,
7608 (splay_tree_key) decl)
7609 == NULL))
7611 omp_add_variable (octx, decl,
7612 GOVD_SHARED | GOVD_SEEN);
7613 octx = octx->outer_context;
7616 if (octx)
7617 omp_notice_variable (octx, decl, true);
7620 else if (outer_ctx->outer_context)
7621 omp_notice_variable (outer_ctx->outer_context, decl, true);
7623 goto do_add;
7624 case OMP_CLAUSE_REDUCTION:
7625 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7626 /* OpenACC permits reductions on private variables. */
7627 if (!(region_type & ORT_ACC))
7628 check_non_private = "reduction";
7629 decl = OMP_CLAUSE_DECL (c);
7630 if (TREE_CODE (decl) == MEM_REF)
7632 tree type = TREE_TYPE (decl);
7633 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7634 NULL, is_gimple_val, fb_rvalue, false)
7635 == GS_ERROR)
7637 remove = true;
7638 break;
7640 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7641 if (DECL_P (v))
7643 omp_firstprivatize_variable (ctx, v);
7644 omp_notice_variable (ctx, v, true);
7646 decl = TREE_OPERAND (decl, 0);
7647 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7649 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7650 NULL, is_gimple_val, fb_rvalue, false)
7651 == GS_ERROR)
7653 remove = true;
7654 break;
7656 v = TREE_OPERAND (decl, 1);
7657 if (DECL_P (v))
7659 omp_firstprivatize_variable (ctx, v);
7660 omp_notice_variable (ctx, v, true);
7662 decl = TREE_OPERAND (decl, 0);
7664 if (TREE_CODE (decl) == ADDR_EXPR
7665 || TREE_CODE (decl) == INDIRECT_REF)
7666 decl = TREE_OPERAND (decl, 0);
7668 goto do_add_decl;
7669 case OMP_CLAUSE_LINEAR:
7670 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7671 is_gimple_val, fb_rvalue) == GS_ERROR)
7673 remove = true;
7674 break;
7676 else
7678 if (code == OMP_SIMD
7679 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7681 struct gimplify_omp_ctx *octx = outer_ctx;
7682 if (octx
7683 && octx->region_type == ORT_WORKSHARE
7684 && octx->combined_loop
7685 && !octx->distribute)
7687 if (octx->outer_context
7688 && (octx->outer_context->region_type
7689 == ORT_COMBINED_PARALLEL))
7690 octx = octx->outer_context->outer_context;
7691 else
7692 octx = octx->outer_context;
7694 if (octx
7695 && octx->region_type == ORT_WORKSHARE
7696 && octx->combined_loop
7697 && octx->distribute)
7699 error_at (OMP_CLAUSE_LOCATION (c),
7700 "%<linear%> clause for variable other than "
7701 "loop iterator specified on construct "
7702 "combined with %<distribute%>");
7703 remove = true;
7704 break;
7707 /* For combined #pragma omp parallel for simd, need to put
7708 lastprivate and perhaps firstprivate too on the
7709 parallel. Similarly for #pragma omp for simd. */
7710 struct gimplify_omp_ctx *octx = outer_ctx;
7711 decl = NULL_TREE;
7714 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7715 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7716 break;
7717 decl = OMP_CLAUSE_DECL (c);
7718 if (error_operand_p (decl))
7720 decl = NULL_TREE;
7721 break;
7723 flags = GOVD_SEEN;
7724 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7725 flags |= GOVD_FIRSTPRIVATE;
7726 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7727 flags |= GOVD_LASTPRIVATE;
7728 if (octx
7729 && octx->region_type == ORT_WORKSHARE
7730 && octx->combined_loop)
7732 if (octx->outer_context
7733 && (octx->outer_context->region_type
7734 == ORT_COMBINED_PARALLEL))
7735 octx = octx->outer_context;
7736 else if (omp_check_private (octx, decl, false))
7737 break;
7739 else if (octx
7740 && (octx->region_type & ORT_TASK) != 0
7741 && octx->combined_loop)
7743 else if (octx
7744 && octx->region_type == ORT_COMBINED_PARALLEL
7745 && ctx->region_type == ORT_WORKSHARE
7746 && octx == outer_ctx)
7747 flags = GOVD_SEEN | GOVD_SHARED;
7748 else if (octx
7749 && octx->region_type == ORT_COMBINED_TEAMS)
7750 flags = GOVD_SEEN | GOVD_SHARED;
7751 else if (octx
7752 && octx->region_type == ORT_COMBINED_TARGET)
7754 flags &= ~GOVD_LASTPRIVATE;
7755 if (flags == GOVD_SEEN)
7756 break;
7758 else
7759 break;
7760 splay_tree_node on
7761 = splay_tree_lookup (octx->variables,
7762 (splay_tree_key) decl);
7763 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7765 octx = NULL;
7766 break;
7768 omp_add_variable (octx, decl, flags);
7769 if (octx->outer_context == NULL)
7770 break;
7771 octx = octx->outer_context;
7773 while (1);
7774 if (octx
7775 && decl
7776 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7777 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7778 omp_notice_variable (octx, decl, true);
7780 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7781 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7782 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7784 notice_outer = false;
7785 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7787 goto do_add;
7789 case OMP_CLAUSE_MAP:
7790 decl = OMP_CLAUSE_DECL (c);
7791 if (error_operand_p (decl))
7792 remove = true;
7793 switch (code)
7795 case OMP_TARGET:
7796 break;
7797 case OACC_DATA:
7798 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7799 break;
7800 /* FALLTHRU */
7801 case OMP_TARGET_DATA:
7802 case OMP_TARGET_ENTER_DATA:
7803 case OMP_TARGET_EXIT_DATA:
7804 case OACC_ENTER_DATA:
7805 case OACC_EXIT_DATA:
7806 case OACC_HOST_DATA:
7807 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7808 || (OMP_CLAUSE_MAP_KIND (c)
7809 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7810 /* For target {,enter ,exit }data only the array slice is
7811 mapped, but not the pointer to it. */
7812 remove = true;
7813 break;
7814 default:
7815 break;
7817 if (remove)
7818 break;
7819 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7821 struct gimplify_omp_ctx *octx;
7822 for (octx = outer_ctx; octx; octx = octx->outer_context)
7824 if (octx->region_type != ORT_ACC_HOST_DATA)
7825 break;
7826 splay_tree_node n2
7827 = splay_tree_lookup (octx->variables,
7828 (splay_tree_key) decl);
7829 if (n2)
7830 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7831 "declared in enclosing %<host_data%> region",
7832 DECL_NAME (decl));
7835 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7836 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7837 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7838 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7839 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7841 remove = true;
7842 break;
7844 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7845 || (OMP_CLAUSE_MAP_KIND (c)
7846 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7847 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7849 OMP_CLAUSE_SIZE (c)
7850 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7851 false);
7852 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7853 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7855 if (!DECL_P (decl))
7857 tree d = decl, *pd;
7858 if (TREE_CODE (d) == ARRAY_REF)
7860 while (TREE_CODE (d) == ARRAY_REF)
7861 d = TREE_OPERAND (d, 0);
7862 if (TREE_CODE (d) == COMPONENT_REF
7863 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7864 decl = d;
7866 pd = &OMP_CLAUSE_DECL (c);
7867 if (d == decl
7868 && TREE_CODE (decl) == INDIRECT_REF
7869 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7870 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7871 == REFERENCE_TYPE))
7873 pd = &TREE_OPERAND (decl, 0);
7874 decl = TREE_OPERAND (decl, 0);
7876 if (TREE_CODE (decl) == COMPONENT_REF)
7878 while (TREE_CODE (decl) == COMPONENT_REF)
7879 decl = TREE_OPERAND (decl, 0);
7880 if (TREE_CODE (decl) == INDIRECT_REF
7881 && DECL_P (TREE_OPERAND (decl, 0))
7882 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7883 == REFERENCE_TYPE))
7884 decl = TREE_OPERAND (decl, 0);
7886 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7887 == GS_ERROR)
7889 remove = true;
7890 break;
7892 if (DECL_P (decl))
7894 if (error_operand_p (decl))
7896 remove = true;
7897 break;
7900 tree stype = TREE_TYPE (decl);
7901 if (TREE_CODE (stype) == REFERENCE_TYPE)
7902 stype = TREE_TYPE (stype);
7903 if (TYPE_SIZE_UNIT (stype) == NULL
7904 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7906 error_at (OMP_CLAUSE_LOCATION (c),
7907 "mapping field %qE of variable length "
7908 "structure", OMP_CLAUSE_DECL (c));
7909 remove = true;
7910 break;
7913 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7915 /* Error recovery. */
7916 if (prev_list_p == NULL)
7918 remove = true;
7919 break;
7921 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7923 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7924 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7926 remove = true;
7927 break;
7932 tree offset;
7933 poly_int64 bitsize, bitpos;
7934 machine_mode mode;
7935 int unsignedp, reversep, volatilep = 0;
7936 tree base = OMP_CLAUSE_DECL (c);
7937 while (TREE_CODE (base) == ARRAY_REF)
7938 base = TREE_OPERAND (base, 0);
7939 if (TREE_CODE (base) == INDIRECT_REF)
7940 base = TREE_OPERAND (base, 0);
7941 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7942 &mode, &unsignedp, &reversep,
7943 &volatilep);
7944 tree orig_base = base;
7945 if ((TREE_CODE (base) == INDIRECT_REF
7946 || (TREE_CODE (base) == MEM_REF
7947 && integer_zerop (TREE_OPERAND (base, 1))))
7948 && DECL_P (TREE_OPERAND (base, 0))
7949 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7950 == REFERENCE_TYPE))
7951 base = TREE_OPERAND (base, 0);
7952 gcc_assert (base == decl
7953 && (offset == NULL_TREE
7954 || poly_int_tree_p (offset)));
7956 splay_tree_node n
7957 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7958 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7959 == GOMP_MAP_ALWAYS_POINTER);
7960 if (n == NULL || (n->value & GOVD_MAP) == 0)
7962 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7963 OMP_CLAUSE_MAP);
7964 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
7965 if (orig_base != base)
7966 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
7967 else
7968 OMP_CLAUSE_DECL (l) = decl;
7969 OMP_CLAUSE_SIZE (l) = size_int (1);
7970 if (struct_map_to_clause == NULL)
7971 struct_map_to_clause = new hash_map<tree, tree>;
7972 struct_map_to_clause->put (decl, l);
7973 if (ptr)
7975 enum gomp_map_kind mkind
7976 = code == OMP_TARGET_EXIT_DATA
7977 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7978 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7979 OMP_CLAUSE_MAP);
7980 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7981 OMP_CLAUSE_DECL (c2)
7982 = unshare_expr (OMP_CLAUSE_DECL (c));
7983 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
7984 OMP_CLAUSE_SIZE (c2)
7985 = TYPE_SIZE_UNIT (ptr_type_node);
7986 OMP_CLAUSE_CHAIN (l) = c2;
7987 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7989 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7990 tree c3
7991 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7992 OMP_CLAUSE_MAP);
7993 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7994 OMP_CLAUSE_DECL (c3)
7995 = unshare_expr (OMP_CLAUSE_DECL (c4));
7996 OMP_CLAUSE_SIZE (c3)
7997 = TYPE_SIZE_UNIT (ptr_type_node);
7998 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7999 OMP_CLAUSE_CHAIN (c2) = c3;
8001 *prev_list_p = l;
8002 prev_list_p = NULL;
8004 else
8006 OMP_CLAUSE_CHAIN (l) = c;
8007 *list_p = l;
8008 list_p = &OMP_CLAUSE_CHAIN (l);
8010 if (orig_base != base && code == OMP_TARGET)
8012 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8013 OMP_CLAUSE_MAP);
8014 enum gomp_map_kind mkind
8015 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8016 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8017 OMP_CLAUSE_DECL (c2) = decl;
8018 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8019 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8020 OMP_CLAUSE_CHAIN (l) = c2;
8022 flags = GOVD_MAP | GOVD_EXPLICIT;
8023 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8024 flags |= GOVD_SEEN;
8025 goto do_add_decl;
8027 else
8029 tree *osc = struct_map_to_clause->get (decl);
8030 tree *sc = NULL, *scp = NULL;
8031 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8032 n->value |= GOVD_SEEN;
8033 poly_offset_int o1, o2;
8034 if (offset)
8035 o1 = wi::to_poly_offset (offset);
8036 else
8037 o1 = 0;
8038 if (maybe_ne (bitpos, 0))
8039 o1 += bits_to_bytes_round_down (bitpos);
8040 sc = &OMP_CLAUSE_CHAIN (*osc);
8041 if (*sc != c
8042 && (OMP_CLAUSE_MAP_KIND (*sc)
8043 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8044 sc = &OMP_CLAUSE_CHAIN (*sc);
8045 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8046 if (ptr && sc == prev_list_p)
8047 break;
8048 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8049 != COMPONENT_REF
8050 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8051 != INDIRECT_REF)
8052 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8053 != ARRAY_REF))
8054 break;
8055 else
8057 tree offset2;
8058 poly_int64 bitsize2, bitpos2;
8059 base = OMP_CLAUSE_DECL (*sc);
8060 if (TREE_CODE (base) == ARRAY_REF)
8062 while (TREE_CODE (base) == ARRAY_REF)
8063 base = TREE_OPERAND (base, 0);
8064 if (TREE_CODE (base) != COMPONENT_REF
8065 || (TREE_CODE (TREE_TYPE (base))
8066 != ARRAY_TYPE))
8067 break;
8069 else if (TREE_CODE (base) == INDIRECT_REF
8070 && (TREE_CODE (TREE_OPERAND (base, 0))
8071 == COMPONENT_REF)
8072 && (TREE_CODE (TREE_TYPE
8073 (TREE_OPERAND (base, 0)))
8074 == REFERENCE_TYPE))
8075 base = TREE_OPERAND (base, 0);
8076 base = get_inner_reference (base, &bitsize2,
8077 &bitpos2, &offset2,
8078 &mode, &unsignedp,
8079 &reversep, &volatilep);
8080 if ((TREE_CODE (base) == INDIRECT_REF
8081 || (TREE_CODE (base) == MEM_REF
8082 && integer_zerop (TREE_OPERAND (base,
8083 1))))
8084 && DECL_P (TREE_OPERAND (base, 0))
8085 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8086 0)))
8087 == REFERENCE_TYPE))
8088 base = TREE_OPERAND (base, 0);
8089 if (base != decl)
8090 break;
8091 if (scp)
8092 continue;
8093 gcc_assert (offset == NULL_TREE
8094 || poly_int_tree_p (offset));
8095 tree d1 = OMP_CLAUSE_DECL (*sc);
8096 tree d2 = OMP_CLAUSE_DECL (c);
8097 while (TREE_CODE (d1) == ARRAY_REF)
8098 d1 = TREE_OPERAND (d1, 0);
8099 while (TREE_CODE (d2) == ARRAY_REF)
8100 d2 = TREE_OPERAND (d2, 0);
8101 if (TREE_CODE (d1) == INDIRECT_REF)
8102 d1 = TREE_OPERAND (d1, 0);
8103 if (TREE_CODE (d2) == INDIRECT_REF)
8104 d2 = TREE_OPERAND (d2, 0);
8105 while (TREE_CODE (d1) == COMPONENT_REF)
8106 if (TREE_CODE (d2) == COMPONENT_REF
8107 && TREE_OPERAND (d1, 1)
8108 == TREE_OPERAND (d2, 1))
8110 d1 = TREE_OPERAND (d1, 0);
8111 d2 = TREE_OPERAND (d2, 0);
8113 else
8114 break;
8115 if (d1 == d2)
8117 error_at (OMP_CLAUSE_LOCATION (c),
8118 "%qE appears more than once in map "
8119 "clauses", OMP_CLAUSE_DECL (c));
8120 remove = true;
8121 break;
8123 if (offset2)
8124 o2 = wi::to_poly_offset (offset2);
8125 else
8126 o2 = 0;
8127 o2 += bits_to_bytes_round_down (bitpos2);
8128 if (maybe_lt (o1, o2)
8129 || (known_eq (o1, 2)
8130 && maybe_lt (bitpos, bitpos2)))
8132 if (ptr)
8133 scp = sc;
8134 else
8135 break;
8138 if (remove)
8139 break;
8140 OMP_CLAUSE_SIZE (*osc)
8141 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8142 size_one_node);
8143 if (ptr)
8145 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8146 OMP_CLAUSE_MAP);
8147 tree cl = NULL_TREE;
8148 enum gomp_map_kind mkind
8149 = code == OMP_TARGET_EXIT_DATA
8150 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8151 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8152 OMP_CLAUSE_DECL (c2)
8153 = unshare_expr (OMP_CLAUSE_DECL (c));
8154 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8155 OMP_CLAUSE_SIZE (c2)
8156 = TYPE_SIZE_UNIT (ptr_type_node);
8157 cl = scp ? *prev_list_p : c2;
8158 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8160 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8161 tree c3
8162 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8163 OMP_CLAUSE_MAP);
8164 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8165 OMP_CLAUSE_DECL (c3)
8166 = unshare_expr (OMP_CLAUSE_DECL (c4));
8167 OMP_CLAUSE_SIZE (c3)
8168 = TYPE_SIZE_UNIT (ptr_type_node);
8169 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8170 if (!scp)
8171 OMP_CLAUSE_CHAIN (c2) = c3;
8172 else
8173 cl = c3;
8175 if (scp)
8176 *scp = c2;
8177 if (sc == prev_list_p)
8179 *sc = cl;
8180 prev_list_p = NULL;
8182 else
8184 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8185 list_p = prev_list_p;
8186 prev_list_p = NULL;
8187 OMP_CLAUSE_CHAIN (c) = *sc;
8188 *sc = cl;
8189 continue;
8192 else if (*sc != c)
8194 *list_p = OMP_CLAUSE_CHAIN (c);
8195 OMP_CLAUSE_CHAIN (c) = *sc;
8196 *sc = c;
8197 continue;
8201 if (!remove
8202 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8203 && OMP_CLAUSE_CHAIN (c)
8204 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8205 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8206 == GOMP_MAP_ALWAYS_POINTER))
8207 prev_list_p = list_p;
8208 break;
8210 flags = GOVD_MAP | GOVD_EXPLICIT;
8211 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8212 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8213 flags |= GOVD_MAP_ALWAYS_TO;
8214 goto do_add;
8216 case OMP_CLAUSE_DEPEND:
8217 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8219 tree deps = OMP_CLAUSE_DECL (c);
8220 while (deps && TREE_CODE (deps) == TREE_LIST)
8222 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8223 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8224 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8225 pre_p, NULL, is_gimple_val, fb_rvalue);
8226 deps = TREE_CHAIN (deps);
8228 break;
8230 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8231 break;
8232 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8234 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8235 NULL, is_gimple_val, fb_rvalue);
8236 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8238 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8240 remove = true;
8241 break;
8243 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8244 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8245 is_gimple_val, fb_rvalue) == GS_ERROR)
8247 remove = true;
8248 break;
8250 break;
8252 case OMP_CLAUSE_TO:
8253 case OMP_CLAUSE_FROM:
8254 case OMP_CLAUSE__CACHE_:
8255 decl = OMP_CLAUSE_DECL (c);
8256 if (error_operand_p (decl))
8258 remove = true;
8259 break;
8261 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8262 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8263 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8264 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8265 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8267 remove = true;
8268 break;
8270 if (!DECL_P (decl))
8272 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8273 NULL, is_gimple_lvalue, fb_lvalue)
8274 == GS_ERROR)
8276 remove = true;
8277 break;
8279 break;
8281 goto do_notice;
8283 case OMP_CLAUSE_USE_DEVICE_PTR:
8284 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8285 goto do_add;
8286 case OMP_CLAUSE_IS_DEVICE_PTR:
8287 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8288 goto do_add;
8290 do_add:
8291 decl = OMP_CLAUSE_DECL (c);
8292 do_add_decl:
8293 if (error_operand_p (decl))
8295 remove = true;
8296 break;
8298 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8300 tree t = omp_member_access_dummy_var (decl);
8301 if (t)
8303 tree v = DECL_VALUE_EXPR (decl);
8304 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8305 if (outer_ctx)
8306 omp_notice_variable (outer_ctx, t, true);
8309 if (code == OACC_DATA
8310 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8311 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8312 flags |= GOVD_MAP_0LEN_ARRAY;
8313 omp_add_variable (ctx, decl, flags);
8314 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8315 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8317 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8318 GOVD_LOCAL | GOVD_SEEN);
8319 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8320 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8321 find_decl_expr,
8322 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8323 NULL) == NULL_TREE)
8324 omp_add_variable (ctx,
8325 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8326 GOVD_LOCAL | GOVD_SEEN);
8327 gimplify_omp_ctxp = ctx;
8328 push_gimplify_context ();
8330 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8331 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8333 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8334 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8335 pop_gimplify_context
8336 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8337 push_gimplify_context ();
8338 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8339 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8340 pop_gimplify_context
8341 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8342 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8343 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8345 gimplify_omp_ctxp = outer_ctx;
8347 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8348 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8350 gimplify_omp_ctxp = ctx;
8351 push_gimplify_context ();
8352 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8354 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8355 NULL, NULL);
8356 TREE_SIDE_EFFECTS (bind) = 1;
8357 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8358 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8360 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8361 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8362 pop_gimplify_context
8363 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8364 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8366 gimplify_omp_ctxp = outer_ctx;
8368 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8369 && OMP_CLAUSE_LINEAR_STMT (c))
8371 gimplify_omp_ctxp = ctx;
8372 push_gimplify_context ();
8373 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8375 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8376 NULL, NULL);
8377 TREE_SIDE_EFFECTS (bind) = 1;
8378 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8379 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8381 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8382 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8383 pop_gimplify_context
8384 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8385 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8387 gimplify_omp_ctxp = outer_ctx;
8389 if (notice_outer)
8390 goto do_notice;
8391 break;
8393 case OMP_CLAUSE_COPYIN:
8394 case OMP_CLAUSE_COPYPRIVATE:
8395 decl = OMP_CLAUSE_DECL (c);
8396 if (error_operand_p (decl))
8398 remove = true;
8399 break;
8401 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8402 && !remove
8403 && !omp_check_private (ctx, decl, true))
8405 remove = true;
8406 if (is_global_var (decl))
8408 if (DECL_THREAD_LOCAL_P (decl))
8409 remove = false;
8410 else if (DECL_HAS_VALUE_EXPR_P (decl))
8412 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8414 if (value
8415 && DECL_P (value)
8416 && DECL_THREAD_LOCAL_P (value))
8417 remove = false;
8420 if (remove)
8421 error_at (OMP_CLAUSE_LOCATION (c),
8422 "copyprivate variable %qE is not threadprivate"
8423 " or private in outer context", DECL_NAME (decl));
8425 do_notice:
8426 if (outer_ctx)
8427 omp_notice_variable (outer_ctx, decl, true);
8428 if (check_non_private
8429 && region_type == ORT_WORKSHARE
8430 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8431 || decl == OMP_CLAUSE_DECL (c)
8432 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8433 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8434 == ADDR_EXPR
8435 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8436 == POINTER_PLUS_EXPR
8437 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8438 (OMP_CLAUSE_DECL (c), 0), 0))
8439 == ADDR_EXPR)))))
8440 && omp_check_private (ctx, decl, false))
8442 error ("%s variable %qE is private in outer context",
8443 check_non_private, DECL_NAME (decl));
8444 remove = true;
8446 break;
8448 case OMP_CLAUSE_IF:
8449 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8450 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8452 const char *p[2];
8453 for (int i = 0; i < 2; i++)
8454 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8456 case OMP_PARALLEL: p[i] = "parallel"; break;
8457 case OMP_TASK: p[i] = "task"; break;
8458 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8459 case OMP_TARGET_DATA: p[i] = "target data"; break;
8460 case OMP_TARGET: p[i] = "target"; break;
8461 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8462 case OMP_TARGET_ENTER_DATA:
8463 p[i] = "target enter data"; break;
8464 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8465 default: gcc_unreachable ();
8467 error_at (OMP_CLAUSE_LOCATION (c),
8468 "expected %qs %<if%> clause modifier rather than %qs",
8469 p[0], p[1]);
8470 remove = true;
8472 /* Fall through. */
8474 case OMP_CLAUSE_FINAL:
8475 OMP_CLAUSE_OPERAND (c, 0)
8476 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8477 /* Fall through. */
8479 case OMP_CLAUSE_SCHEDULE:
8480 case OMP_CLAUSE_NUM_THREADS:
8481 case OMP_CLAUSE_NUM_TEAMS:
8482 case OMP_CLAUSE_THREAD_LIMIT:
8483 case OMP_CLAUSE_DIST_SCHEDULE:
8484 case OMP_CLAUSE_DEVICE:
8485 case OMP_CLAUSE_PRIORITY:
8486 case OMP_CLAUSE_GRAINSIZE:
8487 case OMP_CLAUSE_NUM_TASKS:
8488 case OMP_CLAUSE_HINT:
8489 case OMP_CLAUSE_ASYNC:
8490 case OMP_CLAUSE_WAIT:
8491 case OMP_CLAUSE_NUM_GANGS:
8492 case OMP_CLAUSE_NUM_WORKERS:
8493 case OMP_CLAUSE_VECTOR_LENGTH:
8494 case OMP_CLAUSE_WORKER:
8495 case OMP_CLAUSE_VECTOR:
8496 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8497 is_gimple_val, fb_rvalue) == GS_ERROR)
8498 remove = true;
8499 break;
8501 case OMP_CLAUSE_GANG:
8502 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8503 is_gimple_val, fb_rvalue) == GS_ERROR)
8504 remove = true;
8505 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8506 is_gimple_val, fb_rvalue) == GS_ERROR)
8507 remove = true;
8508 break;
8510 case OMP_CLAUSE_NOWAIT:
8511 case OMP_CLAUSE_ORDERED:
8512 case OMP_CLAUSE_UNTIED:
8513 case OMP_CLAUSE_COLLAPSE:
8514 case OMP_CLAUSE_TILE:
8515 case OMP_CLAUSE_AUTO:
8516 case OMP_CLAUSE_SEQ:
8517 case OMP_CLAUSE_INDEPENDENT:
8518 case OMP_CLAUSE_MERGEABLE:
8519 case OMP_CLAUSE_PROC_BIND:
8520 case OMP_CLAUSE_SAFELEN:
8521 case OMP_CLAUSE_SIMDLEN:
8522 case OMP_CLAUSE_NOGROUP:
8523 case OMP_CLAUSE_THREADS:
8524 case OMP_CLAUSE_SIMD:
8525 case OMP_CLAUSE_IF_PRESENT:
8526 case OMP_CLAUSE_FINALIZE:
8527 break;
8529 case OMP_CLAUSE_DEFAULTMAP:
8530 ctx->target_map_scalars_firstprivate = false;
8531 break;
8533 case OMP_CLAUSE_ALIGNED:
8534 decl = OMP_CLAUSE_DECL (c);
8535 if (error_operand_p (decl))
8537 remove = true;
8538 break;
8540 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8541 is_gimple_val, fb_rvalue) == GS_ERROR)
8543 remove = true;
8544 break;
8546 if (!is_global_var (decl)
8547 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8548 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8549 break;
8551 case OMP_CLAUSE_DEFAULT:
8552 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8553 break;
8555 default:
8556 gcc_unreachable ();
8559 if (code == OACC_DATA
8560 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8561 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8562 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8563 remove = true;
8564 if (remove)
8565 *list_p = OMP_CLAUSE_CHAIN (c);
8566 else
8567 list_p = &OMP_CLAUSE_CHAIN (c);
8570 gimplify_omp_ctxp = ctx;
8571 if (struct_map_to_clause)
8572 delete struct_map_to_clause;
8575 /* Return true if DECL is a candidate for shared to firstprivate
8576 optimization. We only consider non-addressable scalars, not
8577 too big, and not references. */
8579 static bool
8580 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8582 if (TREE_ADDRESSABLE (decl))
8583 return false;
8584 tree type = TREE_TYPE (decl);
8585 if (!is_gimple_reg_type (type)
8586 || TREE_CODE (type) == REFERENCE_TYPE
8587 || TREE_ADDRESSABLE (type))
8588 return false;
8589 /* Don't optimize too large decls, as each thread/task will have
8590 its own. */
8591 HOST_WIDE_INT len = int_size_in_bytes (type);
8592 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8593 return false;
8594 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8595 return false;
8596 return true;
8599 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8600 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8601 GOVD_WRITTEN in outer contexts. */
8603 static void
8604 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8606 for (; ctx; ctx = ctx->outer_context)
8608 splay_tree_node n = splay_tree_lookup (ctx->variables,
8609 (splay_tree_key) decl);
8610 if (n == NULL)
8611 continue;
8612 else if (n->value & GOVD_SHARED)
8614 n->value |= GOVD_WRITTEN;
8615 return;
8617 else if (n->value & GOVD_DATA_SHARE_CLASS)
8618 return;
8622 /* Helper callback for walk_gimple_seq to discover possible stores
8623 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8624 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8625 for those. */
8627 static tree
8628 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8630 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8632 *walk_subtrees = 0;
8633 if (!wi->is_lhs)
8634 return NULL_TREE;
8636 tree op = *tp;
8639 if (handled_component_p (op))
8640 op = TREE_OPERAND (op, 0);
8641 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8642 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8643 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8644 else
8645 break;
8647 while (1);
8648 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8649 return NULL_TREE;
8651 omp_mark_stores (gimplify_omp_ctxp, op);
8652 return NULL_TREE;
8655 /* Helper callback for walk_gimple_seq to discover possible stores
8656 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8657 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8658 for those. */
8660 static tree
8661 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8662 bool *handled_ops_p,
8663 struct walk_stmt_info *wi)
8665 gimple *stmt = gsi_stmt (*gsi_p);
8666 switch (gimple_code (stmt))
8668 /* Don't recurse on OpenMP constructs for which
8669 gimplify_adjust_omp_clauses already handled the bodies,
8670 except handle gimple_omp_for_pre_body. */
8671 case GIMPLE_OMP_FOR:
8672 *handled_ops_p = true;
8673 if (gimple_omp_for_pre_body (stmt))
8674 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8675 omp_find_stores_stmt, omp_find_stores_op, wi);
8676 break;
8677 case GIMPLE_OMP_PARALLEL:
8678 case GIMPLE_OMP_TASK:
8679 case GIMPLE_OMP_SECTIONS:
8680 case GIMPLE_OMP_SINGLE:
8681 case GIMPLE_OMP_TARGET:
8682 case GIMPLE_OMP_TEAMS:
8683 case GIMPLE_OMP_CRITICAL:
8684 *handled_ops_p = true;
8685 break;
8686 default:
8687 break;
8689 return NULL_TREE;
8692 struct gimplify_adjust_omp_clauses_data
8694 tree *list_p;
8695 gimple_seq *pre_p;
8698 /* For all variables that were not actually used within the context,
8699 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8701 static int
8702 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8704 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8705 gimple_seq *pre_p
8706 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8707 tree decl = (tree) n->key;
8708 unsigned flags = n->value;
8709 enum omp_clause_code code;
8710 tree clause;
8711 bool private_debug;
8713 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8714 return 0;
8715 if ((flags & GOVD_SEEN) == 0)
8716 return 0;
8717 if (flags & GOVD_DEBUG_PRIVATE)
8719 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8720 private_debug = true;
8722 else if (flags & GOVD_MAP)
8723 private_debug = false;
8724 else
8725 private_debug
8726 = lang_hooks.decls.omp_private_debug_clause (decl,
8727 !!(flags & GOVD_SHARED));
8728 if (private_debug)
8729 code = OMP_CLAUSE_PRIVATE;
8730 else if (flags & GOVD_MAP)
8732 code = OMP_CLAUSE_MAP;
8733 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8734 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8736 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8737 return 0;
8740 else if (flags & GOVD_SHARED)
8742 if (is_global_var (decl))
8744 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8745 while (ctx != NULL)
8747 splay_tree_node on
8748 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8749 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8750 | GOVD_PRIVATE | GOVD_REDUCTION
8751 | GOVD_LINEAR | GOVD_MAP)) != 0)
8752 break;
8753 ctx = ctx->outer_context;
8755 if (ctx == NULL)
8756 return 0;
8758 code = OMP_CLAUSE_SHARED;
8760 else if (flags & GOVD_PRIVATE)
8761 code = OMP_CLAUSE_PRIVATE;
8762 else if (flags & GOVD_FIRSTPRIVATE)
8764 code = OMP_CLAUSE_FIRSTPRIVATE;
8765 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8766 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8767 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8769 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8770 "%<target%> construct", decl);
8771 return 0;
8774 else if (flags & GOVD_LASTPRIVATE)
8775 code = OMP_CLAUSE_LASTPRIVATE;
8776 else if (flags & GOVD_ALIGNED)
8777 return 0;
8778 else
8779 gcc_unreachable ();
8781 if (((flags & GOVD_LASTPRIVATE)
8782 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8783 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8784 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8786 tree chain = *list_p;
8787 clause = build_omp_clause (input_location, code);
8788 OMP_CLAUSE_DECL (clause) = decl;
8789 OMP_CLAUSE_CHAIN (clause) = chain;
8790 if (private_debug)
8791 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8792 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8793 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8794 else if (code == OMP_CLAUSE_SHARED
8795 && (flags & GOVD_WRITTEN) == 0
8796 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8797 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8798 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8799 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8800 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8802 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8803 OMP_CLAUSE_DECL (nc) = decl;
8804 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8805 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8806 OMP_CLAUSE_DECL (clause)
8807 = build_simple_mem_ref_loc (input_location, decl);
8808 OMP_CLAUSE_DECL (clause)
8809 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8810 build_int_cst (build_pointer_type (char_type_node), 0));
8811 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8812 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8813 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8814 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8815 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8816 OMP_CLAUSE_CHAIN (nc) = chain;
8817 OMP_CLAUSE_CHAIN (clause) = nc;
8818 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8819 gimplify_omp_ctxp = ctx->outer_context;
8820 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8821 pre_p, NULL, is_gimple_val, fb_rvalue);
8822 gimplify_omp_ctxp = ctx;
8824 else if (code == OMP_CLAUSE_MAP)
8826 int kind;
8827 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8828 switch (flags & (GOVD_MAP_TO_ONLY
8829 | GOVD_MAP_FORCE
8830 | GOVD_MAP_FORCE_PRESENT))
8832 case 0:
8833 kind = GOMP_MAP_TOFROM;
8834 break;
8835 case GOVD_MAP_FORCE:
8836 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8837 break;
8838 case GOVD_MAP_TO_ONLY:
8839 kind = GOMP_MAP_TO;
8840 break;
8841 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8842 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8843 break;
8844 case GOVD_MAP_FORCE_PRESENT:
8845 kind = GOMP_MAP_FORCE_PRESENT;
8846 break;
8847 default:
8848 gcc_unreachable ();
8850 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8851 if (DECL_SIZE (decl)
8852 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8854 tree decl2 = DECL_VALUE_EXPR (decl);
8855 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8856 decl2 = TREE_OPERAND (decl2, 0);
8857 gcc_assert (DECL_P (decl2));
8858 tree mem = build_simple_mem_ref (decl2);
8859 OMP_CLAUSE_DECL (clause) = mem;
8860 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8861 if (gimplify_omp_ctxp->outer_context)
8863 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8864 omp_notice_variable (ctx, decl2, true);
8865 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8867 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8868 OMP_CLAUSE_MAP);
8869 OMP_CLAUSE_DECL (nc) = decl;
8870 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8871 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8872 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8873 else
8874 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8875 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8876 OMP_CLAUSE_CHAIN (clause) = nc;
8878 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8879 && lang_hooks.decls.omp_privatize_by_reference (decl))
8881 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8882 OMP_CLAUSE_SIZE (clause)
8883 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8884 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8885 gimplify_omp_ctxp = ctx->outer_context;
8886 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8887 pre_p, NULL, is_gimple_val, fb_rvalue);
8888 gimplify_omp_ctxp = ctx;
8889 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8890 OMP_CLAUSE_MAP);
8891 OMP_CLAUSE_DECL (nc) = decl;
8892 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8893 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8894 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8895 OMP_CLAUSE_CHAIN (clause) = nc;
8897 else
8898 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8900 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8902 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8903 OMP_CLAUSE_DECL (nc) = decl;
8904 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8905 OMP_CLAUSE_CHAIN (nc) = chain;
8906 OMP_CLAUSE_CHAIN (clause) = nc;
8907 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8908 gimplify_omp_ctxp = ctx->outer_context;
8909 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8910 gimplify_omp_ctxp = ctx;
8912 *list_p = clause;
8913 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8914 gimplify_omp_ctxp = ctx->outer_context;
8915 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8916 if (gimplify_omp_ctxp)
8917 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8918 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8919 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8920 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8921 true);
8922 gimplify_omp_ctxp = ctx;
8923 return 0;
8926 static void
8927 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8928 enum tree_code code)
8930 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8931 tree c, decl;
8933 if (body)
8935 struct gimplify_omp_ctx *octx;
8936 for (octx = ctx; octx; octx = octx->outer_context)
8937 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8938 break;
8939 if (octx)
8941 struct walk_stmt_info wi;
8942 memset (&wi, 0, sizeof (wi));
8943 walk_gimple_seq (body, omp_find_stores_stmt,
8944 omp_find_stores_op, &wi);
8947 while ((c = *list_p) != NULL)
8949 splay_tree_node n;
8950 bool remove = false;
8952 switch (OMP_CLAUSE_CODE (c))
8954 case OMP_CLAUSE_FIRSTPRIVATE:
8955 if ((ctx->region_type & ORT_TARGET)
8956 && (ctx->region_type & ORT_ACC) == 0
8957 && TYPE_ATOMIC (strip_array_types
8958 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8960 error_at (OMP_CLAUSE_LOCATION (c),
8961 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8962 "%<target%> construct", OMP_CLAUSE_DECL (c));
8963 remove = true;
8964 break;
8966 /* FALLTHRU */
8967 case OMP_CLAUSE_PRIVATE:
8968 case OMP_CLAUSE_SHARED:
8969 case OMP_CLAUSE_LINEAR:
8970 decl = OMP_CLAUSE_DECL (c);
8971 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8972 remove = !(n->value & GOVD_SEEN);
8973 if (! remove)
8975 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
8976 if ((n->value & GOVD_DEBUG_PRIVATE)
8977 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
8979 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
8980 || ((n->value & GOVD_DATA_SHARE_CLASS)
8981 == GOVD_SHARED));
8982 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
8983 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
8985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8986 && (n->value & GOVD_WRITTEN) == 0
8987 && DECL_P (decl)
8988 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8989 OMP_CLAUSE_SHARED_READONLY (c) = 1;
8990 else if (DECL_P (decl)
8991 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8992 && (n->value & GOVD_WRITTEN) != 0)
8993 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8994 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8995 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8996 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8998 break;
9000 case OMP_CLAUSE_LASTPRIVATE:
9001 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9002 accurately reflect the presence of a FIRSTPRIVATE clause. */
9003 decl = OMP_CLAUSE_DECL (c);
9004 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9005 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9006 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9007 if (code == OMP_DISTRIBUTE
9008 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9010 remove = true;
9011 error_at (OMP_CLAUSE_LOCATION (c),
9012 "same variable used in %<firstprivate%> and "
9013 "%<lastprivate%> clauses on %<distribute%> "
9014 "construct");
9016 if (!remove
9017 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9018 && DECL_P (decl)
9019 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9020 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9021 break;
9023 case OMP_CLAUSE_ALIGNED:
9024 decl = OMP_CLAUSE_DECL (c);
9025 if (!is_global_var (decl))
9027 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9028 remove = n == NULL || !(n->value & GOVD_SEEN);
9029 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9031 struct gimplify_omp_ctx *octx;
9032 if (n != NULL
9033 && (n->value & (GOVD_DATA_SHARE_CLASS
9034 & ~GOVD_FIRSTPRIVATE)))
9035 remove = true;
9036 else
9037 for (octx = ctx->outer_context; octx;
9038 octx = octx->outer_context)
9040 n = splay_tree_lookup (octx->variables,
9041 (splay_tree_key) decl);
9042 if (n == NULL)
9043 continue;
9044 if (n->value & GOVD_LOCAL)
9045 break;
9046 /* We have to avoid assigning a shared variable
9047 to itself when trying to add
9048 __builtin_assume_aligned. */
9049 if (n->value & GOVD_SHARED)
9051 remove = true;
9052 break;
9057 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9059 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9060 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9061 remove = true;
9063 break;
9065 case OMP_CLAUSE_MAP:
9066 if (code == OMP_TARGET_EXIT_DATA
9067 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9069 remove = true;
9070 break;
9072 decl = OMP_CLAUSE_DECL (c);
9073 /* Data clauses associated with acc parallel reductions must be
9074 compatible with present_or_copy. Warn and adjust the clause
9075 if that is not the case. */
9076 if (ctx->region_type == ORT_ACC_PARALLEL)
9078 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9079 n = NULL;
9081 if (DECL_P (t))
9082 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9084 if (n && (n->value & GOVD_REDUCTION))
9086 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9088 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9089 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9090 && kind != GOMP_MAP_FORCE_PRESENT
9091 && kind != GOMP_MAP_POINTER)
9093 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9094 "incompatible data clause with reduction "
9095 "on %qE; promoting to present_or_copy",
9096 DECL_NAME (t));
9097 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9101 if (!DECL_P (decl))
9103 if ((ctx->region_type & ORT_TARGET) != 0
9104 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9106 if (TREE_CODE (decl) == INDIRECT_REF
9107 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9108 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9109 == REFERENCE_TYPE))
9110 decl = TREE_OPERAND (decl, 0);
9111 if (TREE_CODE (decl) == COMPONENT_REF)
9113 while (TREE_CODE (decl) == COMPONENT_REF)
9114 decl = TREE_OPERAND (decl, 0);
9115 if (DECL_P (decl))
9117 n = splay_tree_lookup (ctx->variables,
9118 (splay_tree_key) decl);
9119 if (!(n->value & GOVD_SEEN))
9120 remove = true;
9124 break;
9126 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9127 if ((ctx->region_type & ORT_TARGET) != 0
9128 && !(n->value & GOVD_SEEN)
9129 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9130 && (!is_global_var (decl)
9131 || !lookup_attribute ("omp declare target link",
9132 DECL_ATTRIBUTES (decl))))
9134 remove = true;
9135 /* For struct element mapping, if struct is never referenced
9136 in target block and none of the mapping has always modifier,
9137 remove all the struct element mappings, which immediately
9138 follow the GOMP_MAP_STRUCT map clause. */
9139 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9141 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9142 while (cnt--)
9143 OMP_CLAUSE_CHAIN (c)
9144 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9147 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9148 && code == OMP_TARGET_EXIT_DATA)
9149 remove = true;
9150 else if (DECL_SIZE (decl)
9151 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9152 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9153 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9154 && (OMP_CLAUSE_MAP_KIND (c)
9155 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9157 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9158 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9159 INTEGER_CST. */
9160 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9162 tree decl2 = DECL_VALUE_EXPR (decl);
9163 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9164 decl2 = TREE_OPERAND (decl2, 0);
9165 gcc_assert (DECL_P (decl2));
9166 tree mem = build_simple_mem_ref (decl2);
9167 OMP_CLAUSE_DECL (c) = mem;
9168 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9169 if (ctx->outer_context)
9171 omp_notice_variable (ctx->outer_context, decl2, true);
9172 omp_notice_variable (ctx->outer_context,
9173 OMP_CLAUSE_SIZE (c), true);
9175 if (((ctx->region_type & ORT_TARGET) != 0
9176 || !ctx->target_firstprivatize_array_bases)
9177 && ((n->value & GOVD_SEEN) == 0
9178 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9180 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9181 OMP_CLAUSE_MAP);
9182 OMP_CLAUSE_DECL (nc) = decl;
9183 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9184 if (ctx->target_firstprivatize_array_bases)
9185 OMP_CLAUSE_SET_MAP_KIND (nc,
9186 GOMP_MAP_FIRSTPRIVATE_POINTER);
9187 else
9188 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9189 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9190 OMP_CLAUSE_CHAIN (c) = nc;
9191 c = nc;
9194 else
9196 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9197 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9198 gcc_assert ((n->value & GOVD_SEEN) == 0
9199 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9200 == 0));
9202 break;
9204 case OMP_CLAUSE_TO:
9205 case OMP_CLAUSE_FROM:
9206 case OMP_CLAUSE__CACHE_:
9207 decl = OMP_CLAUSE_DECL (c);
9208 if (!DECL_P (decl))
9209 break;
9210 if (DECL_SIZE (decl)
9211 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9213 tree decl2 = DECL_VALUE_EXPR (decl);
9214 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9215 decl2 = TREE_OPERAND (decl2, 0);
9216 gcc_assert (DECL_P (decl2));
9217 tree mem = build_simple_mem_ref (decl2);
9218 OMP_CLAUSE_DECL (c) = mem;
9219 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9220 if (ctx->outer_context)
9222 omp_notice_variable (ctx->outer_context, decl2, true);
9223 omp_notice_variable (ctx->outer_context,
9224 OMP_CLAUSE_SIZE (c), true);
9227 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9228 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9229 break;
9231 case OMP_CLAUSE_REDUCTION:
9232 decl = OMP_CLAUSE_DECL (c);
9233 /* OpenACC reductions need a present_or_copy data clause.
9234 Add one if necessary. Emit error when the reduction is private. */
9235 if (ctx->region_type == ORT_ACC_PARALLEL)
9237 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9238 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9240 remove = true;
9241 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9242 "reduction on %qE", DECL_NAME (decl));
9244 else if ((n->value & GOVD_MAP) == 0)
9246 tree next = OMP_CLAUSE_CHAIN (c);
9247 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9248 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9249 OMP_CLAUSE_DECL (nc) = decl;
9250 OMP_CLAUSE_CHAIN (c) = nc;
9251 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9252 while (1)
9254 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9255 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9256 break;
9257 nc = OMP_CLAUSE_CHAIN (nc);
9259 OMP_CLAUSE_CHAIN (nc) = next;
9260 n->value |= GOVD_MAP;
9263 if (DECL_P (decl)
9264 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9265 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9266 break;
9267 case OMP_CLAUSE_COPYIN:
9268 case OMP_CLAUSE_COPYPRIVATE:
9269 case OMP_CLAUSE_IF:
9270 case OMP_CLAUSE_NUM_THREADS:
9271 case OMP_CLAUSE_NUM_TEAMS:
9272 case OMP_CLAUSE_THREAD_LIMIT:
9273 case OMP_CLAUSE_DIST_SCHEDULE:
9274 case OMP_CLAUSE_DEVICE:
9275 case OMP_CLAUSE_SCHEDULE:
9276 case OMP_CLAUSE_NOWAIT:
9277 case OMP_CLAUSE_ORDERED:
9278 case OMP_CLAUSE_DEFAULT:
9279 case OMP_CLAUSE_UNTIED:
9280 case OMP_CLAUSE_COLLAPSE:
9281 case OMP_CLAUSE_FINAL:
9282 case OMP_CLAUSE_MERGEABLE:
9283 case OMP_CLAUSE_PROC_BIND:
9284 case OMP_CLAUSE_SAFELEN:
9285 case OMP_CLAUSE_SIMDLEN:
9286 case OMP_CLAUSE_DEPEND:
9287 case OMP_CLAUSE_PRIORITY:
9288 case OMP_CLAUSE_GRAINSIZE:
9289 case OMP_CLAUSE_NUM_TASKS:
9290 case OMP_CLAUSE_NOGROUP:
9291 case OMP_CLAUSE_THREADS:
9292 case OMP_CLAUSE_SIMD:
9293 case OMP_CLAUSE_HINT:
9294 case OMP_CLAUSE_DEFAULTMAP:
9295 case OMP_CLAUSE_USE_DEVICE_PTR:
9296 case OMP_CLAUSE_IS_DEVICE_PTR:
9297 case OMP_CLAUSE_ASYNC:
9298 case OMP_CLAUSE_WAIT:
9299 case OMP_CLAUSE_INDEPENDENT:
9300 case OMP_CLAUSE_NUM_GANGS:
9301 case OMP_CLAUSE_NUM_WORKERS:
9302 case OMP_CLAUSE_VECTOR_LENGTH:
9303 case OMP_CLAUSE_GANG:
9304 case OMP_CLAUSE_WORKER:
9305 case OMP_CLAUSE_VECTOR:
9306 case OMP_CLAUSE_AUTO:
9307 case OMP_CLAUSE_SEQ:
9308 case OMP_CLAUSE_TILE:
9309 case OMP_CLAUSE_IF_PRESENT:
9310 case OMP_CLAUSE_FINALIZE:
9311 break;
9313 default:
9314 gcc_unreachable ();
9317 if (remove)
9318 *list_p = OMP_CLAUSE_CHAIN (c);
9319 else
9320 list_p = &OMP_CLAUSE_CHAIN (c);
9323 /* Add in any implicit data sharing. */
9324 struct gimplify_adjust_omp_clauses_data data;
9325 data.list_p = list_p;
9326 data.pre_p = pre_p;
9327 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9329 gimplify_omp_ctxp = ctx->outer_context;
9330 delete_omp_context (ctx);
9333 /* Gimplify OACC_CACHE. */
9335 static void
9336 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9338 tree expr = *expr_p;
9340 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9341 OACC_CACHE);
9342 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9343 OACC_CACHE);
9345 /* TODO: Do something sensible with this information. */
9347 *expr_p = NULL_TREE;
9350 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9351 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9352 kind. The entry kind will replace the one in CLAUSE, while the exit
9353 kind will be used in a new omp_clause and returned to the caller. */
9355 static tree
9356 gimplify_oacc_declare_1 (tree clause)
9358 HOST_WIDE_INT kind, new_op;
9359 bool ret = false;
9360 tree c = NULL;
9362 kind = OMP_CLAUSE_MAP_KIND (clause);
9364 switch (kind)
9366 case GOMP_MAP_ALLOC:
9367 new_op = GOMP_MAP_RELEASE;
9368 ret = true;
9369 break;
9371 case GOMP_MAP_FROM:
9372 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9373 new_op = GOMP_MAP_FROM;
9374 ret = true;
9375 break;
9377 case GOMP_MAP_TOFROM:
9378 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9379 new_op = GOMP_MAP_FROM;
9380 ret = true;
9381 break;
9383 case GOMP_MAP_DEVICE_RESIDENT:
9384 case GOMP_MAP_FORCE_DEVICEPTR:
9385 case GOMP_MAP_FORCE_PRESENT:
9386 case GOMP_MAP_LINK:
9387 case GOMP_MAP_POINTER:
9388 case GOMP_MAP_TO:
9389 break;
9391 default:
9392 gcc_unreachable ();
9393 break;
9396 if (ret)
9398 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9399 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9400 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9403 return c;
9406 /* Gimplify OACC_DECLARE. */
9408 static void
9409 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9411 tree expr = *expr_p;
9412 gomp_target *stmt;
9413 tree clauses, t, decl;
9415 clauses = OACC_DECLARE_CLAUSES (expr);
9417 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9418 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9420 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9422 decl = OMP_CLAUSE_DECL (t);
9424 if (TREE_CODE (decl) == MEM_REF)
9425 decl = TREE_OPERAND (decl, 0);
9427 if (VAR_P (decl) && !is_oacc_declared (decl))
9429 tree attr = get_identifier ("oacc declare target");
9430 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9431 DECL_ATTRIBUTES (decl));
9434 if (VAR_P (decl)
9435 && !is_global_var (decl)
9436 && DECL_CONTEXT (decl) == current_function_decl)
9438 tree c = gimplify_oacc_declare_1 (t);
9439 if (c)
9441 if (oacc_declare_returns == NULL)
9442 oacc_declare_returns = new hash_map<tree, tree>;
9444 oacc_declare_returns->put (decl, c);
9448 if (gimplify_omp_ctxp)
9449 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9452 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9453 clauses);
9455 gimplify_seq_add_stmt (pre_p, stmt);
9457 *expr_p = NULL_TREE;
9460 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9461 gimplification of the body, as well as scanning the body for used
9462 variables. We need to do this scan now, because variable-sized
9463 decls will be decomposed during gimplification. */
9465 static void
9466 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9468 tree expr = *expr_p;
9469 gimple *g;
9470 gimple_seq body = NULL;
9472 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9473 OMP_PARALLEL_COMBINED (expr)
9474 ? ORT_COMBINED_PARALLEL
9475 : ORT_PARALLEL, OMP_PARALLEL);
9477 push_gimplify_context ();
9479 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9480 if (gimple_code (g) == GIMPLE_BIND)
9481 pop_gimplify_context (g);
9482 else
9483 pop_gimplify_context (NULL);
9485 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9486 OMP_PARALLEL);
9488 g = gimple_build_omp_parallel (body,
9489 OMP_PARALLEL_CLAUSES (expr),
9490 NULL_TREE, NULL_TREE);
9491 if (OMP_PARALLEL_COMBINED (expr))
9492 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9493 gimplify_seq_add_stmt (pre_p, g);
9494 *expr_p = NULL_TREE;
9497 /* Gimplify the contents of an OMP_TASK statement. This involves
9498 gimplification of the body, as well as scanning the body for used
9499 variables. We need to do this scan now, because variable-sized
9500 decls will be decomposed during gimplification. */
9502 static void
9503 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9505 tree expr = *expr_p;
9506 gimple *g;
9507 gimple_seq body = NULL;
9509 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9510 omp_find_clause (OMP_TASK_CLAUSES (expr),
9511 OMP_CLAUSE_UNTIED)
9512 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9514 push_gimplify_context ();
9516 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9517 if (gimple_code (g) == GIMPLE_BIND)
9518 pop_gimplify_context (g);
9519 else
9520 pop_gimplify_context (NULL);
9522 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9523 OMP_TASK);
9525 g = gimple_build_omp_task (body,
9526 OMP_TASK_CLAUSES (expr),
9527 NULL_TREE, NULL_TREE,
9528 NULL_TREE, NULL_TREE, NULL_TREE);
9529 gimplify_seq_add_stmt (pre_p, g);
9530 *expr_p = NULL_TREE;
9533 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9534 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
9535 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
9536 is address of OMP_PARALLEL in between if any, pdata[2] is address of
9537 OMP_FOR in between if any and pdata[3] is address of the inner
9538 OMP_FOR/OMP_SIMD. */
9540 static tree
9541 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
9543 tree **pdata = (tree **) data;
9544 *walk_subtrees = 0;
9545 switch (TREE_CODE (*tp))
9547 case OMP_FOR:
9548 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9550 pdata[3] = tp;
9551 return *tp;
9553 pdata[2] = tp;
9554 *walk_subtrees = 1;
9555 break;
9556 case OMP_SIMD:
9557 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9559 pdata[3] = tp;
9560 return *tp;
9562 break;
9563 case BIND_EXPR:
9564 if (BIND_EXPR_VARS (*tp)
9565 || (BIND_EXPR_BLOCK (*tp)
9566 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
9567 pdata[0] = tp;
9568 *walk_subtrees = 1;
9569 break;
9570 case STATEMENT_LIST:
9571 if (!tsi_one_before_end_p (tsi_start (*tp)))
9572 pdata[0] = tp;
9573 *walk_subtrees = 1;
9574 break;
9575 case TRY_FINALLY_EXPR:
9576 pdata[0] = tp;
9577 *walk_subtrees = 1;
9578 break;
9579 case OMP_PARALLEL:
9580 pdata[1] = tp;
9581 *walk_subtrees = 1;
9582 break;
9583 default:
9584 break;
9586 return NULL_TREE;
9589 /* Gimplify the gross structure of an OMP_FOR statement. */
9591 static enum gimplify_status
9592 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9594 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9595 enum gimplify_status ret = GS_ALL_DONE;
9596 enum gimplify_status tret;
9597 gomp_for *gfor;
9598 gimple_seq for_body, for_pre_body;
9599 int i;
9600 bitmap has_decl_expr = NULL;
9601 enum omp_region_type ort = ORT_WORKSHARE;
9603 orig_for_stmt = for_stmt = *expr_p;
9605 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9607 tree *data[4] = { NULL, NULL, NULL, NULL };
9608 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9609 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9610 find_combined_omp_for, data, NULL);
9611 if (inner_for_stmt == NULL_TREE)
9613 gcc_assert (seen_error ());
9614 *expr_p = NULL_TREE;
9615 return GS_ERROR;
9617 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
9619 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
9620 &OMP_FOR_PRE_BODY (for_stmt));
9621 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
9623 if (OMP_FOR_PRE_BODY (inner_for_stmt))
9625 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
9626 &OMP_FOR_PRE_BODY (for_stmt));
9627 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
9630 if (data[0])
9632 /* We have some statements or variable declarations in between
9633 the composite construct directives. Move them around the
9634 inner_for_stmt. */
9635 data[0] = expr_p;
9636 for (i = 0; i < 3; i++)
9637 if (data[i])
9639 tree t = *data[i];
9640 if (i < 2 && data[i + 1] == &OMP_BODY (t))
9641 data[i + 1] = data[i];
9642 *data[i] = OMP_BODY (t);
9643 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9644 NULL_TREE, make_node (BLOCK));
9645 OMP_BODY (t) = body;
9646 append_to_statement_list_force (inner_for_stmt,
9647 &BIND_EXPR_BODY (body));
9648 *data[3] = t;
9649 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
9650 gcc_assert (*data[3] == inner_for_stmt);
9652 return GS_OK;
9655 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
9656 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
9657 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
9658 i)) == TREE_LIST)
9660 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
9661 /* Class iterators aren't allowed on OMP_SIMD, so the only
9662 case we need to solve is distribute parallel for. */
9663 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
9664 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
9665 && data[1]);
9666 tree orig_decl = TREE_PURPOSE (orig);
9667 tree last = TREE_VALUE (orig);
9668 tree *pc;
9669 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
9670 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
9671 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
9672 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
9673 && OMP_CLAUSE_DECL (*pc) == orig_decl)
9674 break;
9675 if (*pc == NULL_TREE)
9677 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
9679 /* private clause will appear only on inner_for_stmt.
9680 Change it into firstprivate, and add private clause
9681 on for_stmt. */
9682 tree c = copy_node (*pc);
9683 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9684 OMP_FOR_CLAUSES (for_stmt) = c;
9685 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
9686 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9688 else
9690 /* lastprivate clause will appear on both inner_for_stmt
9691 and for_stmt. Add firstprivate clause to
9692 inner_for_stmt. */
9693 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
9694 OMP_CLAUSE_FIRSTPRIVATE);
9695 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
9696 OMP_CLAUSE_CHAIN (c) = *pc;
9697 *pc = c;
9698 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9700 tree c = build_omp_clause (UNKNOWN_LOCATION,
9701 OMP_CLAUSE_FIRSTPRIVATE);
9702 OMP_CLAUSE_DECL (c) = last;
9703 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9704 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9705 c = build_omp_clause (UNKNOWN_LOCATION,
9706 *pc ? OMP_CLAUSE_SHARED
9707 : OMP_CLAUSE_FIRSTPRIVATE);
9708 OMP_CLAUSE_DECL (c) = orig_decl;
9709 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9710 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9714 switch (TREE_CODE (for_stmt))
9716 case OMP_FOR:
9717 case OMP_DISTRIBUTE:
9718 break;
9719 case OACC_LOOP:
9720 ort = ORT_ACC;
9721 break;
9722 case OMP_TASKLOOP:
9723 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9724 ort = ORT_UNTIED_TASK;
9725 else
9726 ort = ORT_TASK;
9727 break;
9728 case OMP_SIMD:
9729 ort = ORT_SIMD;
9730 break;
9731 default:
9732 gcc_unreachable ();
9735 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9736 clause for the IV. */
9737 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9739 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9740 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9741 decl = TREE_OPERAND (t, 0);
9742 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9743 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9744 && OMP_CLAUSE_DECL (c) == decl)
9746 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9747 break;
9751 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9752 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9753 TREE_CODE (for_stmt));
9755 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9756 gimplify_omp_ctxp->distribute = true;
9758 /* Handle OMP_FOR_INIT. */
9759 for_pre_body = NULL;
9760 if ((ort == ORT_SIMD
9761 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
9762 && OMP_FOR_PRE_BODY (for_stmt))
9764 has_decl_expr = BITMAP_ALLOC (NULL);
9765 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9766 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9767 == VAR_DECL)
9769 t = OMP_FOR_PRE_BODY (for_stmt);
9770 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9772 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9774 tree_stmt_iterator si;
9775 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9776 tsi_next (&si))
9778 t = tsi_stmt (si);
9779 if (TREE_CODE (t) == DECL_EXPR
9780 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9781 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9785 if (OMP_FOR_PRE_BODY (for_stmt))
9787 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9788 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9789 else
9791 struct gimplify_omp_ctx ctx;
9792 memset (&ctx, 0, sizeof (ctx));
9793 ctx.region_type = ORT_NONE;
9794 gimplify_omp_ctxp = &ctx;
9795 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9796 gimplify_omp_ctxp = NULL;
9799 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9801 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9802 for_stmt = inner_for_stmt;
9804 /* For taskloop, need to gimplify the start, end and step before the
9805 taskloop, outside of the taskloop omp context. */
9806 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9808 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9810 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9811 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9813 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9814 TREE_OPERAND (t, 1)
9815 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9816 gimple_seq_empty_p (for_pre_body)
9817 ? pre_p : &for_pre_body, NULL,
9818 false);
9819 /* Reference to pointer conversion is considered useless,
9820 but is significant for firstprivate clause. Force it
9821 here. */
9822 if (TREE_CODE (type) == POINTER_TYPE
9823 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9824 == REFERENCE_TYPE))
9826 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9827 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9828 TREE_OPERAND (t, 1));
9829 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9830 ? pre_p : &for_pre_body);
9831 TREE_OPERAND (t, 1) = v;
9833 tree c = build_omp_clause (input_location,
9834 OMP_CLAUSE_FIRSTPRIVATE);
9835 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9836 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9837 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9840 /* Handle OMP_FOR_COND. */
9841 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9842 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9844 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9845 TREE_OPERAND (t, 1)
9846 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9847 gimple_seq_empty_p (for_pre_body)
9848 ? pre_p : &for_pre_body, NULL,
9849 false);
9850 /* Reference to pointer conversion is considered useless,
9851 but is significant for firstprivate clause. Force it
9852 here. */
9853 if (TREE_CODE (type) == POINTER_TYPE
9854 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9855 == REFERENCE_TYPE))
9857 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9858 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9859 TREE_OPERAND (t, 1));
9860 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9861 ? pre_p : &for_pre_body);
9862 TREE_OPERAND (t, 1) = v;
9864 tree c = build_omp_clause (input_location,
9865 OMP_CLAUSE_FIRSTPRIVATE);
9866 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9867 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9868 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9871 /* Handle OMP_FOR_INCR. */
9872 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9873 if (TREE_CODE (t) == MODIFY_EXPR)
9875 decl = TREE_OPERAND (t, 0);
9876 t = TREE_OPERAND (t, 1);
9877 tree *tp = &TREE_OPERAND (t, 1);
9878 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9879 tp = &TREE_OPERAND (t, 0);
9881 if (!is_gimple_constant (*tp))
9883 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9884 ? pre_p : &for_pre_body;
9885 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9886 tree c = build_omp_clause (input_location,
9887 OMP_CLAUSE_FIRSTPRIVATE);
9888 OMP_CLAUSE_DECL (c) = *tp;
9889 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9890 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9895 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9896 OMP_TASKLOOP);
9899 if (orig_for_stmt != for_stmt)
9900 gimplify_omp_ctxp->combined_loop = true;
9902 for_body = NULL;
9903 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9904 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9905 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9906 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9908 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9909 bool is_doacross = false;
9910 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9912 is_doacross = true;
9913 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9914 (OMP_FOR_INIT (for_stmt))
9915 * 2);
9917 int collapse = 1, tile = 0;
9918 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9919 if (c)
9920 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9921 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9922 if (c)
9923 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9924 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9926 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9927 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9928 decl = TREE_OPERAND (t, 0);
9929 gcc_assert (DECL_P (decl));
9930 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9931 || POINTER_TYPE_P (TREE_TYPE (decl)));
9932 if (is_doacross)
9934 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9936 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
9937 if (TREE_CODE (orig_decl) == TREE_LIST)
9938 orig_decl = TREE_PURPOSE (orig_decl);
9939 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
9941 else
9942 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9943 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9946 /* Make sure the iteration variable is private. */
9947 tree c = NULL_TREE;
9948 tree c2 = NULL_TREE;
9949 if (orig_for_stmt != for_stmt)
9951 /* Preserve this information until we gimplify the inner simd. */
9952 if (has_decl_expr
9953 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9954 TREE_PRIVATE (t) = 1;
9956 else if (ort == ORT_SIMD)
9958 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9959 (splay_tree_key) decl);
9960 omp_is_private (gimplify_omp_ctxp, decl,
9961 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9962 != 1));
9963 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9964 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9965 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9967 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9968 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9969 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9970 if ((has_decl_expr
9971 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9972 || TREE_PRIVATE (t))
9974 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9975 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9977 struct gimplify_omp_ctx *outer
9978 = gimplify_omp_ctxp->outer_context;
9979 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9981 if (outer->region_type == ORT_WORKSHARE
9982 && outer->combined_loop)
9984 n = splay_tree_lookup (outer->variables,
9985 (splay_tree_key)decl);
9986 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9988 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9989 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9991 else
9993 struct gimplify_omp_ctx *octx = outer->outer_context;
9994 if (octx
9995 && octx->region_type == ORT_COMBINED_PARALLEL
9996 && octx->outer_context
9997 && (octx->outer_context->region_type
9998 == ORT_WORKSHARE)
9999 && octx->outer_context->combined_loop)
10001 octx = octx->outer_context;
10002 n = splay_tree_lookup (octx->variables,
10003 (splay_tree_key)decl);
10004 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10006 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10007 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10014 OMP_CLAUSE_DECL (c) = decl;
10015 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10016 OMP_FOR_CLAUSES (for_stmt) = c;
10017 omp_add_variable (gimplify_omp_ctxp, decl, flags);
10018 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10020 if (outer->region_type == ORT_WORKSHARE
10021 && outer->combined_loop)
10023 if (outer->outer_context
10024 && (outer->outer_context->region_type
10025 == ORT_COMBINED_PARALLEL))
10026 outer = outer->outer_context;
10027 else if (omp_check_private (outer, decl, false))
10028 outer = NULL;
10030 else if (((outer->region_type & ORT_TASK) != 0)
10031 && outer->combined_loop
10032 && !omp_check_private (gimplify_omp_ctxp,
10033 decl, false))
10035 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10037 omp_notice_variable (outer, decl, true);
10038 outer = NULL;
10040 if (outer)
10042 n = splay_tree_lookup (outer->variables,
10043 (splay_tree_key)decl);
10044 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10046 omp_add_variable (outer, decl,
10047 GOVD_LASTPRIVATE | GOVD_SEEN);
10048 if (outer->region_type == ORT_COMBINED_PARALLEL
10049 && outer->outer_context
10050 && (outer->outer_context->region_type
10051 == ORT_WORKSHARE)
10052 && outer->outer_context->combined_loop)
10054 outer = outer->outer_context;
10055 n = splay_tree_lookup (outer->variables,
10056 (splay_tree_key)decl);
10057 if (omp_check_private (outer, decl, false))
10058 outer = NULL;
10059 else if (n == NULL
10060 || ((n->value & GOVD_DATA_SHARE_CLASS)
10061 == 0))
10062 omp_add_variable (outer, decl,
10063 GOVD_LASTPRIVATE
10064 | GOVD_SEEN);
10065 else
10066 outer = NULL;
10068 if (outer && outer->outer_context
10069 && (outer->outer_context->region_type
10070 == ORT_COMBINED_TEAMS))
10072 outer = outer->outer_context;
10073 n = splay_tree_lookup (outer->variables,
10074 (splay_tree_key)decl);
10075 if (n == NULL
10076 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10077 omp_add_variable (outer, decl,
10078 GOVD_SHARED | GOVD_SEEN);
10079 else
10080 outer = NULL;
10082 if (outer && outer->outer_context)
10083 omp_notice_variable (outer->outer_context, decl,
10084 true);
10089 else
10091 bool lastprivate
10092 = (!has_decl_expr
10093 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10094 if (TREE_PRIVATE (t))
10095 lastprivate = false;
10096 struct gimplify_omp_ctx *outer
10097 = gimplify_omp_ctxp->outer_context;
10098 if (outer && lastprivate)
10100 if (outer->region_type == ORT_WORKSHARE
10101 && outer->combined_loop)
10103 n = splay_tree_lookup (outer->variables,
10104 (splay_tree_key)decl);
10105 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10107 lastprivate = false;
10108 outer = NULL;
10110 else if (outer->outer_context
10111 && (outer->outer_context->region_type
10112 == ORT_COMBINED_PARALLEL))
10113 outer = outer->outer_context;
10114 else if (omp_check_private (outer, decl, false))
10115 outer = NULL;
10117 else if (((outer->region_type & ORT_TASK) != 0)
10118 && outer->combined_loop
10119 && !omp_check_private (gimplify_omp_ctxp,
10120 decl, false))
10122 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10124 omp_notice_variable (outer, decl, true);
10125 outer = NULL;
10127 if (outer)
10129 n = splay_tree_lookup (outer->variables,
10130 (splay_tree_key)decl);
10131 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10133 omp_add_variable (outer, decl,
10134 GOVD_LASTPRIVATE | GOVD_SEEN);
10135 if (outer->region_type == ORT_COMBINED_PARALLEL
10136 && outer->outer_context
10137 && (outer->outer_context->region_type
10138 == ORT_WORKSHARE)
10139 && outer->outer_context->combined_loop)
10141 outer = outer->outer_context;
10142 n = splay_tree_lookup (outer->variables,
10143 (splay_tree_key)decl);
10144 if (omp_check_private (outer, decl, false))
10145 outer = NULL;
10146 else if (n == NULL
10147 || ((n->value & GOVD_DATA_SHARE_CLASS)
10148 == 0))
10149 omp_add_variable (outer, decl,
10150 GOVD_LASTPRIVATE
10151 | GOVD_SEEN);
10152 else
10153 outer = NULL;
10155 if (outer && outer->outer_context
10156 && (outer->outer_context->region_type
10157 == ORT_COMBINED_TEAMS))
10159 outer = outer->outer_context;
10160 n = splay_tree_lookup (outer->variables,
10161 (splay_tree_key)decl);
10162 if (n == NULL
10163 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10164 omp_add_variable (outer, decl,
10165 GOVD_SHARED | GOVD_SEEN);
10166 else
10167 outer = NULL;
10169 if (outer && outer->outer_context)
10170 omp_notice_variable (outer->outer_context, decl,
10171 true);
10176 c = build_omp_clause (input_location,
10177 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10178 : OMP_CLAUSE_PRIVATE);
10179 OMP_CLAUSE_DECL (c) = decl;
10180 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10181 OMP_FOR_CLAUSES (for_stmt) = c;
10182 omp_add_variable (gimplify_omp_ctxp, decl,
10183 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10184 | GOVD_EXPLICIT | GOVD_SEEN);
10185 c = NULL_TREE;
10188 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10189 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10190 else
10191 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10193 /* If DECL is not a gimple register, create a temporary variable to act
10194 as an iteration counter. This is valid, since DECL cannot be
10195 modified in the body of the loop. Similarly for any iteration vars
10196 in simd with collapse > 1 where the iterator vars must be
10197 lastprivate. */
10198 if (orig_for_stmt != for_stmt)
10199 var = decl;
10200 else if (!is_gimple_reg (decl)
10201 || (ort == ORT_SIMD
10202 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10204 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10205 /* Make sure omp_add_variable is not called on it prematurely.
10206 We call it ourselves a few lines later. */
10207 gimplify_omp_ctxp = NULL;
10208 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10209 gimplify_omp_ctxp = ctx;
10210 TREE_OPERAND (t, 0) = var;
10212 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10214 if (ort == ORT_SIMD
10215 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10217 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10218 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10219 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10220 OMP_CLAUSE_DECL (c2) = var;
10221 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10222 OMP_FOR_CLAUSES (for_stmt) = c2;
10223 omp_add_variable (gimplify_omp_ctxp, var,
10224 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10225 if (c == NULL_TREE)
10227 c = c2;
10228 c2 = NULL_TREE;
10231 else
10232 omp_add_variable (gimplify_omp_ctxp, var,
10233 GOVD_PRIVATE | GOVD_SEEN);
10235 else
10236 var = decl;
10238 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10239 is_gimple_val, fb_rvalue, false);
10240 ret = MIN (ret, tret);
10241 if (ret == GS_ERROR)
10242 return ret;
10244 /* Handle OMP_FOR_COND. */
10245 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10246 gcc_assert (COMPARISON_CLASS_P (t));
10247 gcc_assert (TREE_OPERAND (t, 0) == decl);
10249 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10250 is_gimple_val, fb_rvalue, false);
10251 ret = MIN (ret, tret);
10253 /* Handle OMP_FOR_INCR. */
10254 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10255 switch (TREE_CODE (t))
10257 case PREINCREMENT_EXPR:
10258 case POSTINCREMENT_EXPR:
10260 tree decl = TREE_OPERAND (t, 0);
10261 /* c_omp_for_incr_canonicalize_ptr() should have been
10262 called to massage things appropriately. */
10263 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10265 if (orig_for_stmt != for_stmt)
10266 break;
10267 t = build_int_cst (TREE_TYPE (decl), 1);
10268 if (c)
10269 OMP_CLAUSE_LINEAR_STEP (c) = t;
10270 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10271 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10272 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10273 break;
10276 case PREDECREMENT_EXPR:
10277 case POSTDECREMENT_EXPR:
10278 /* c_omp_for_incr_canonicalize_ptr() should have been
10279 called to massage things appropriately. */
10280 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10281 if (orig_for_stmt != for_stmt)
10282 break;
10283 t = build_int_cst (TREE_TYPE (decl), -1);
10284 if (c)
10285 OMP_CLAUSE_LINEAR_STEP (c) = t;
10286 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10287 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10288 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10289 break;
10291 case MODIFY_EXPR:
10292 gcc_assert (TREE_OPERAND (t, 0) == decl);
10293 TREE_OPERAND (t, 0) = var;
10295 t = TREE_OPERAND (t, 1);
10296 switch (TREE_CODE (t))
10298 case PLUS_EXPR:
10299 if (TREE_OPERAND (t, 1) == decl)
10301 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10302 TREE_OPERAND (t, 0) = var;
10303 break;
10306 /* Fallthru. */
10307 case MINUS_EXPR:
10308 case POINTER_PLUS_EXPR:
10309 gcc_assert (TREE_OPERAND (t, 0) == decl);
10310 TREE_OPERAND (t, 0) = var;
10311 break;
10312 default:
10313 gcc_unreachable ();
10316 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10317 is_gimple_val, fb_rvalue, false);
10318 ret = MIN (ret, tret);
10319 if (c)
10321 tree step = TREE_OPERAND (t, 1);
10322 tree stept = TREE_TYPE (decl);
10323 if (POINTER_TYPE_P (stept))
10324 stept = sizetype;
10325 step = fold_convert (stept, step);
10326 if (TREE_CODE (t) == MINUS_EXPR)
10327 step = fold_build1 (NEGATE_EXPR, stept, step);
10328 OMP_CLAUSE_LINEAR_STEP (c) = step;
10329 if (step != TREE_OPERAND (t, 1))
10331 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10332 &for_pre_body, NULL,
10333 is_gimple_val, fb_rvalue, false);
10334 ret = MIN (ret, tret);
10337 break;
10339 default:
10340 gcc_unreachable ();
10343 if (c2)
10345 gcc_assert (c);
10346 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10349 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10351 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10352 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10353 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10354 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10355 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10356 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10357 && OMP_CLAUSE_DECL (c) == decl)
10359 if (is_doacross && (collapse == 1 || i >= collapse))
10360 t = var;
10361 else
10363 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10364 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10365 gcc_assert (TREE_OPERAND (t, 0) == var);
10366 t = TREE_OPERAND (t, 1);
10367 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10368 || TREE_CODE (t) == MINUS_EXPR
10369 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10370 gcc_assert (TREE_OPERAND (t, 0) == var);
10371 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10372 is_doacross ? var : decl,
10373 TREE_OPERAND (t, 1));
10375 gimple_seq *seq;
10376 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10377 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10378 else
10379 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10380 gimplify_assign (decl, t, seq);
10385 BITMAP_FREE (has_decl_expr);
10387 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10389 push_gimplify_context ();
10390 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10392 OMP_FOR_BODY (orig_for_stmt)
10393 = build3 (BIND_EXPR, void_type_node, NULL,
10394 OMP_FOR_BODY (orig_for_stmt), NULL);
10395 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10399 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10400 &for_body);
10402 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10404 if (gimple_code (g) == GIMPLE_BIND)
10405 pop_gimplify_context (g);
10406 else
10407 pop_gimplify_context (NULL);
10410 if (orig_for_stmt != for_stmt)
10411 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10413 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10414 decl = TREE_OPERAND (t, 0);
10415 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10416 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10417 gimplify_omp_ctxp = ctx->outer_context;
10418 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10419 gimplify_omp_ctxp = ctx;
10420 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10421 TREE_OPERAND (t, 0) = var;
10422 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10423 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10424 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10427 gimplify_adjust_omp_clauses (pre_p, for_body,
10428 &OMP_FOR_CLAUSES (orig_for_stmt),
10429 TREE_CODE (orig_for_stmt));
10431 int kind;
10432 switch (TREE_CODE (orig_for_stmt))
10434 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10435 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10436 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10437 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10438 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10439 default:
10440 gcc_unreachable ();
10442 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10443 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10444 for_pre_body);
10445 if (orig_for_stmt != for_stmt)
10446 gimple_omp_for_set_combined_p (gfor, true);
10447 if (gimplify_omp_ctxp
10448 && (gimplify_omp_ctxp->combined_loop
10449 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10450 && gimplify_omp_ctxp->outer_context
10451 && gimplify_omp_ctxp->outer_context->combined_loop)))
10453 gimple_omp_for_set_combined_into_p (gfor, true);
10454 if (gimplify_omp_ctxp->combined_loop)
10455 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10456 else
10457 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10460 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10462 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10463 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10464 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10465 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10466 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10467 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10468 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10469 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10472 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10473 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10474 The outer taskloop stands for computing the number of iterations,
10475 counts for collapsed loops and holding taskloop specific clauses.
10476 The task construct stands for the effect of data sharing on the
10477 explicit task it creates and the inner taskloop stands for expansion
10478 of the static loop inside of the explicit task construct. */
10479 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10481 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10482 tree task_clauses = NULL_TREE;
10483 tree c = *gfor_clauses_ptr;
10484 tree *gtask_clauses_ptr = &task_clauses;
10485 tree outer_for_clauses = NULL_TREE;
10486 tree *gforo_clauses_ptr = &outer_for_clauses;
10487 for (; c; c = OMP_CLAUSE_CHAIN (c))
10488 switch (OMP_CLAUSE_CODE (c))
10490 /* These clauses are allowed on task, move them there. */
10491 case OMP_CLAUSE_SHARED:
10492 case OMP_CLAUSE_FIRSTPRIVATE:
10493 case OMP_CLAUSE_DEFAULT:
10494 case OMP_CLAUSE_IF:
10495 case OMP_CLAUSE_UNTIED:
10496 case OMP_CLAUSE_FINAL:
10497 case OMP_CLAUSE_MERGEABLE:
10498 case OMP_CLAUSE_PRIORITY:
10499 *gtask_clauses_ptr = c;
10500 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10501 break;
10502 case OMP_CLAUSE_PRIVATE:
10503 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10505 /* We want private on outer for and firstprivate
10506 on task. */
10507 *gtask_clauses_ptr
10508 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10509 OMP_CLAUSE_FIRSTPRIVATE);
10510 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10511 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10512 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10513 *gforo_clauses_ptr = c;
10514 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10516 else
10518 *gtask_clauses_ptr = c;
10519 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10521 break;
10522 /* These clauses go into outer taskloop clauses. */
10523 case OMP_CLAUSE_GRAINSIZE:
10524 case OMP_CLAUSE_NUM_TASKS:
10525 case OMP_CLAUSE_NOGROUP:
10526 *gforo_clauses_ptr = c;
10527 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10528 break;
10529 /* Taskloop clause we duplicate on both taskloops. */
10530 case OMP_CLAUSE_COLLAPSE:
10531 *gfor_clauses_ptr = c;
10532 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10533 *gforo_clauses_ptr = copy_node (c);
10534 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10535 break;
10536 /* For lastprivate, keep the clause on inner taskloop, and add
10537 a shared clause on task. If the same decl is also firstprivate,
10538 add also firstprivate clause on the inner taskloop. */
10539 case OMP_CLAUSE_LASTPRIVATE:
10540 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10542 /* For taskloop C++ lastprivate IVs, we want:
10543 1) private on outer taskloop
10544 2) firstprivate and shared on task
10545 3) lastprivate on inner taskloop */
10546 *gtask_clauses_ptr
10547 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10548 OMP_CLAUSE_FIRSTPRIVATE);
10549 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10550 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10551 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10552 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10553 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10554 OMP_CLAUSE_PRIVATE);
10555 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10556 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10557 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10558 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10560 *gfor_clauses_ptr = c;
10561 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10562 *gtask_clauses_ptr
10563 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10564 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10565 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10566 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10567 gtask_clauses_ptr
10568 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10569 break;
10570 default:
10571 gcc_unreachable ();
10573 *gfor_clauses_ptr = NULL_TREE;
10574 *gtask_clauses_ptr = NULL_TREE;
10575 *gforo_clauses_ptr = NULL_TREE;
10576 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10577 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10578 NULL_TREE, NULL_TREE, NULL_TREE);
10579 gimple_omp_task_set_taskloop_p (g, true);
10580 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10581 gomp_for *gforo
10582 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10583 gimple_omp_for_collapse (gfor),
10584 gimple_omp_for_pre_body (gfor));
10585 gimple_omp_for_set_pre_body (gfor, NULL);
10586 gimple_omp_for_set_combined_p (gforo, true);
10587 gimple_omp_for_set_combined_into_p (gfor, true);
10588 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10590 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10591 tree v = create_tmp_var (type);
10592 gimple_omp_for_set_index (gforo, i, v);
10593 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10594 gimple_omp_for_set_initial (gforo, i, t);
10595 gimple_omp_for_set_cond (gforo, i,
10596 gimple_omp_for_cond (gfor, i));
10597 t = unshare_expr (gimple_omp_for_final (gfor, i));
10598 gimple_omp_for_set_final (gforo, i, t);
10599 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10600 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10601 TREE_OPERAND (t, 0) = v;
10602 gimple_omp_for_set_incr (gforo, i, t);
10603 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10604 OMP_CLAUSE_DECL (t) = v;
10605 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10606 gimple_omp_for_set_clauses (gforo, t);
10608 gimplify_seq_add_stmt (pre_p, gforo);
10610 else
10611 gimplify_seq_add_stmt (pre_p, gfor);
10612 if (ret != GS_ALL_DONE)
10613 return GS_ERROR;
10614 *expr_p = NULL_TREE;
10615 return GS_ALL_DONE;
10618 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10619 of OMP_TARGET's body. */
10621 static tree
10622 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10624 *walk_subtrees = 0;
10625 switch (TREE_CODE (*tp))
10627 case OMP_TEAMS:
10628 return *tp;
10629 case BIND_EXPR:
10630 case STATEMENT_LIST:
10631 *walk_subtrees = 1;
10632 break;
10633 default:
10634 break;
10636 return NULL_TREE;
10639 /* Helper function of optimize_target_teams, determine if the expression
10640 can be computed safely before the target construct on the host. */
10642 static tree
10643 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10645 splay_tree_node n;
10647 if (TYPE_P (*tp))
10649 *walk_subtrees = 0;
10650 return NULL_TREE;
10652 switch (TREE_CODE (*tp))
10654 case VAR_DECL:
10655 case PARM_DECL:
10656 case RESULT_DECL:
10657 *walk_subtrees = 0;
10658 if (error_operand_p (*tp)
10659 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10660 || DECL_HAS_VALUE_EXPR_P (*tp)
10661 || DECL_THREAD_LOCAL_P (*tp)
10662 || TREE_SIDE_EFFECTS (*tp)
10663 || TREE_THIS_VOLATILE (*tp))
10664 return *tp;
10665 if (is_global_var (*tp)
10666 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10667 || lookup_attribute ("omp declare target link",
10668 DECL_ATTRIBUTES (*tp))))
10669 return *tp;
10670 if (VAR_P (*tp)
10671 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10672 && !is_global_var (*tp)
10673 && decl_function_context (*tp) == current_function_decl)
10674 return *tp;
10675 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10676 (splay_tree_key) *tp);
10677 if (n == NULL)
10679 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10680 return NULL_TREE;
10681 return *tp;
10683 else if (n->value & GOVD_LOCAL)
10684 return *tp;
10685 else if (n->value & GOVD_FIRSTPRIVATE)
10686 return NULL_TREE;
10687 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10688 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10689 return NULL_TREE;
10690 return *tp;
10691 case INTEGER_CST:
10692 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10693 return *tp;
10694 return NULL_TREE;
10695 case TARGET_EXPR:
10696 if (TARGET_EXPR_INITIAL (*tp)
10697 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10698 return *tp;
10699 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10700 walk_subtrees, NULL);
10701 /* Allow some reasonable subset of integral arithmetics. */
10702 case PLUS_EXPR:
10703 case MINUS_EXPR:
10704 case MULT_EXPR:
10705 case TRUNC_DIV_EXPR:
10706 case CEIL_DIV_EXPR:
10707 case FLOOR_DIV_EXPR:
10708 case ROUND_DIV_EXPR:
10709 case TRUNC_MOD_EXPR:
10710 case CEIL_MOD_EXPR:
10711 case FLOOR_MOD_EXPR:
10712 case ROUND_MOD_EXPR:
10713 case RDIV_EXPR:
10714 case EXACT_DIV_EXPR:
10715 case MIN_EXPR:
10716 case MAX_EXPR:
10717 case LSHIFT_EXPR:
10718 case RSHIFT_EXPR:
10719 case BIT_IOR_EXPR:
10720 case BIT_XOR_EXPR:
10721 case BIT_AND_EXPR:
10722 case NEGATE_EXPR:
10723 case ABS_EXPR:
10724 case BIT_NOT_EXPR:
10725 case NON_LVALUE_EXPR:
10726 CASE_CONVERT:
10727 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10728 return *tp;
10729 return NULL_TREE;
10730 /* And disallow anything else, except for comparisons. */
10731 default:
10732 if (COMPARISON_CLASS_P (*tp))
10733 return NULL_TREE;
10734 return *tp;
10738 /* Try to determine if the num_teams and/or thread_limit expressions
10739 can have their values determined already before entering the
10740 target construct.
10741 INTEGER_CSTs trivially are,
10742 integral decls that are firstprivate (explicitly or implicitly)
10743 or explicitly map(always, to:) or map(always, tofrom:) on the target
10744 region too, and expressions involving simple arithmetics on those
10745 too, function calls are not ok, dereferencing something neither etc.
10746 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10747 EXPR based on what we find:
10748 0 stands for clause not specified at all, use implementation default
10749 -1 stands for value that can't be determined easily before entering
10750 the target construct.
10751 If teams construct is not present at all, use 1 for num_teams
10752 and 0 for thread_limit (only one team is involved, and the thread
10753 limit is implementation defined. */
10755 static void
10756 optimize_target_teams (tree target, gimple_seq *pre_p)
10758 tree body = OMP_BODY (target);
10759 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10760 tree num_teams = integer_zero_node;
10761 tree thread_limit = integer_zero_node;
10762 location_t num_teams_loc = EXPR_LOCATION (target);
10763 location_t thread_limit_loc = EXPR_LOCATION (target);
10764 tree c, *p, expr;
10765 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10767 if (teams == NULL_TREE)
10768 num_teams = integer_one_node;
10769 else
10770 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10772 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10774 p = &num_teams;
10775 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10777 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10779 p = &thread_limit;
10780 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10782 else
10783 continue;
10784 expr = OMP_CLAUSE_OPERAND (c, 0);
10785 if (TREE_CODE (expr) == INTEGER_CST)
10787 *p = expr;
10788 continue;
10790 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10792 *p = integer_minus_one_node;
10793 continue;
10795 *p = expr;
10796 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10797 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10798 == GS_ERROR)
10800 gimplify_omp_ctxp = target_ctx;
10801 *p = integer_minus_one_node;
10802 continue;
10804 gimplify_omp_ctxp = target_ctx;
10805 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10806 OMP_CLAUSE_OPERAND (c, 0) = *p;
10808 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10809 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10810 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10811 OMP_TARGET_CLAUSES (target) = c;
10812 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10813 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10814 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10815 OMP_TARGET_CLAUSES (target) = c;
10818 /* Gimplify the gross structure of several OMP constructs. */
10820 static void
10821 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10823 tree expr = *expr_p;
10824 gimple *stmt;
10825 gimple_seq body = NULL;
10826 enum omp_region_type ort;
10828 switch (TREE_CODE (expr))
10830 case OMP_SECTIONS:
10831 case OMP_SINGLE:
10832 ort = ORT_WORKSHARE;
10833 break;
10834 case OMP_TARGET:
10835 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10836 break;
10837 case OACC_KERNELS:
10838 ort = ORT_ACC_KERNELS;
10839 break;
10840 case OACC_PARALLEL:
10841 ort = ORT_ACC_PARALLEL;
10842 break;
10843 case OACC_DATA:
10844 ort = ORT_ACC_DATA;
10845 break;
10846 case OMP_TARGET_DATA:
10847 ort = ORT_TARGET_DATA;
10848 break;
10849 case OMP_TEAMS:
10850 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10851 break;
10852 case OACC_HOST_DATA:
10853 ort = ORT_ACC_HOST_DATA;
10854 break;
10855 default:
10856 gcc_unreachable ();
10858 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10859 TREE_CODE (expr));
10860 if (TREE_CODE (expr) == OMP_TARGET)
10861 optimize_target_teams (expr, pre_p);
10862 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10864 push_gimplify_context ();
10865 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10866 if (gimple_code (g) == GIMPLE_BIND)
10867 pop_gimplify_context (g);
10868 else
10869 pop_gimplify_context (NULL);
10870 if ((ort & ORT_TARGET_DATA) != 0)
10872 enum built_in_function end_ix;
10873 switch (TREE_CODE (expr))
10875 case OACC_DATA:
10876 case OACC_HOST_DATA:
10877 end_ix = BUILT_IN_GOACC_DATA_END;
10878 break;
10879 case OMP_TARGET_DATA:
10880 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10881 break;
10882 default:
10883 gcc_unreachable ();
10885 tree fn = builtin_decl_explicit (end_ix);
10886 g = gimple_build_call (fn, 0);
10887 gimple_seq cleanup = NULL;
10888 gimple_seq_add_stmt (&cleanup, g);
10889 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10890 body = NULL;
10891 gimple_seq_add_stmt (&body, g);
10894 else
10895 gimplify_and_add (OMP_BODY (expr), &body);
10896 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10897 TREE_CODE (expr));
10899 switch (TREE_CODE (expr))
10901 case OACC_DATA:
10902 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10903 OMP_CLAUSES (expr));
10904 break;
10905 case OACC_KERNELS:
10906 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10907 OMP_CLAUSES (expr));
10908 break;
10909 case OACC_HOST_DATA:
10910 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10911 OMP_CLAUSES (expr));
10912 break;
10913 case OACC_PARALLEL:
10914 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10915 OMP_CLAUSES (expr));
10916 break;
10917 case OMP_SECTIONS:
10918 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10919 break;
10920 case OMP_SINGLE:
10921 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10922 break;
10923 case OMP_TARGET:
10924 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10925 OMP_CLAUSES (expr));
10926 break;
10927 case OMP_TARGET_DATA:
10928 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10929 OMP_CLAUSES (expr));
10930 break;
10931 case OMP_TEAMS:
10932 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10933 break;
10934 default:
10935 gcc_unreachable ();
10938 gimplify_seq_add_stmt (pre_p, stmt);
10939 *expr_p = NULL_TREE;
10942 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10943 target update constructs. */
10945 static void
10946 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10948 tree expr = *expr_p;
10949 int kind;
10950 gomp_target *stmt;
10951 enum omp_region_type ort = ORT_WORKSHARE;
10953 switch (TREE_CODE (expr))
10955 case OACC_ENTER_DATA:
10956 case OACC_EXIT_DATA:
10957 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10958 ort = ORT_ACC;
10959 break;
10960 case OACC_UPDATE:
10961 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10962 ort = ORT_ACC;
10963 break;
10964 case OMP_TARGET_UPDATE:
10965 kind = GF_OMP_TARGET_KIND_UPDATE;
10966 break;
10967 case OMP_TARGET_ENTER_DATA:
10968 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10969 break;
10970 case OMP_TARGET_EXIT_DATA:
10971 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10972 break;
10973 default:
10974 gcc_unreachable ();
10976 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10977 ort, TREE_CODE (expr));
10978 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10979 TREE_CODE (expr));
10980 if (TREE_CODE (expr) == OACC_UPDATE
10981 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
10982 OMP_CLAUSE_IF_PRESENT))
10984 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
10985 clause. */
10986 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10987 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10988 switch (OMP_CLAUSE_MAP_KIND (c))
10990 case GOMP_MAP_FORCE_TO:
10991 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
10992 break;
10993 case GOMP_MAP_FORCE_FROM:
10994 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
10995 break;
10996 default:
10997 break;
11000 else if (TREE_CODE (expr) == OACC_EXIT_DATA
11001 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11002 OMP_CLAUSE_FINALIZE))
11004 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
11005 semantics apply to all mappings of this OpenACC directive. */
11006 bool finalize_marked = false;
11007 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11008 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11009 switch (OMP_CLAUSE_MAP_KIND (c))
11011 case GOMP_MAP_FROM:
11012 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
11013 finalize_marked = true;
11014 break;
11015 case GOMP_MAP_RELEASE:
11016 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
11017 finalize_marked = true;
11018 break;
11019 default:
11020 /* Check consistency: libgomp relies on the very first data
11021 mapping clause being marked, so make sure we did that before
11022 any other mapping clauses. */
11023 gcc_assert (finalize_marked);
11024 break;
11027 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
11029 gimplify_seq_add_stmt (pre_p, stmt);
11030 *expr_p = NULL_TREE;
11033 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
11034 stabilized the lhs of the atomic operation as *ADDR. Return true if
11035 EXPR is this stabilized form. */
11037 static bool
11038 goa_lhs_expr_p (tree expr, tree addr)
11040 /* Also include casts to other type variants. The C front end is fond
11041 of adding these for e.g. volatile variables. This is like
11042 STRIP_TYPE_NOPS but includes the main variant lookup. */
11043 STRIP_USELESS_TYPE_CONVERSION (expr);
11045 if (TREE_CODE (expr) == INDIRECT_REF)
11047 expr = TREE_OPERAND (expr, 0);
11048 while (expr != addr
11049 && (CONVERT_EXPR_P (expr)
11050 || TREE_CODE (expr) == NON_LVALUE_EXPR)
11051 && TREE_CODE (expr) == TREE_CODE (addr)
11052 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
11054 expr = TREE_OPERAND (expr, 0);
11055 addr = TREE_OPERAND (addr, 0);
11057 if (expr == addr)
11058 return true;
11059 return (TREE_CODE (addr) == ADDR_EXPR
11060 && TREE_CODE (expr) == ADDR_EXPR
11061 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
11063 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
11064 return true;
11065 return false;
11068 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
11069 expression does not involve the lhs, evaluate it into a temporary.
11070 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
11071 or -1 if an error was encountered. */
11073 static int
11074 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
11075 tree lhs_var)
11077 tree expr = *expr_p;
11078 int saw_lhs;
11080 if (goa_lhs_expr_p (expr, lhs_addr))
11082 *expr_p = lhs_var;
11083 return 1;
11085 if (is_gimple_val (expr))
11086 return 0;
11088 saw_lhs = 0;
11089 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
11091 case tcc_binary:
11092 case tcc_comparison:
11093 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
11094 lhs_var);
11095 /* FALLTHRU */
11096 case tcc_unary:
11097 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
11098 lhs_var);
11099 break;
11100 case tcc_expression:
11101 switch (TREE_CODE (expr))
11103 case TRUTH_ANDIF_EXPR:
11104 case TRUTH_ORIF_EXPR:
11105 case TRUTH_AND_EXPR:
11106 case TRUTH_OR_EXPR:
11107 case TRUTH_XOR_EXPR:
11108 case BIT_INSERT_EXPR:
11109 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11110 lhs_addr, lhs_var);
11111 /* FALLTHRU */
11112 case TRUTH_NOT_EXPR:
11113 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11114 lhs_addr, lhs_var);
11115 break;
11116 case COMPOUND_EXPR:
11117 /* Break out any preevaluations from cp_build_modify_expr. */
11118 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11119 expr = TREE_OPERAND (expr, 1))
11120 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11121 *expr_p = expr;
11122 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11123 default:
11124 break;
11126 break;
11127 case tcc_reference:
11128 if (TREE_CODE (expr) == BIT_FIELD_REF)
11129 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11130 lhs_addr, lhs_var);
11131 break;
11132 default:
11133 break;
11136 if (saw_lhs == 0)
11138 enum gimplify_status gs;
11139 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11140 if (gs != GS_ALL_DONE)
11141 saw_lhs = -1;
11144 return saw_lhs;
11147 /* Gimplify an OMP_ATOMIC statement. */
11149 static enum gimplify_status
11150 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11152 tree addr = TREE_OPERAND (*expr_p, 0);
11153 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11154 ? NULL : TREE_OPERAND (*expr_p, 1);
11155 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11156 tree tmp_load;
11157 gomp_atomic_load *loadstmt;
11158 gomp_atomic_store *storestmt;
11160 tmp_load = create_tmp_reg (type);
11161 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11162 return GS_ERROR;
11164 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11165 != GS_ALL_DONE)
11166 return GS_ERROR;
11168 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11169 gimplify_seq_add_stmt (pre_p, loadstmt);
11170 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11171 != GS_ALL_DONE)
11172 return GS_ERROR;
11174 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11175 rhs = tmp_load;
11176 storestmt = gimple_build_omp_atomic_store (rhs);
11177 gimplify_seq_add_stmt (pre_p, storestmt);
11178 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11180 gimple_omp_atomic_set_seq_cst (loadstmt);
11181 gimple_omp_atomic_set_seq_cst (storestmt);
11183 switch (TREE_CODE (*expr_p))
11185 case OMP_ATOMIC_READ:
11186 case OMP_ATOMIC_CAPTURE_OLD:
11187 *expr_p = tmp_load;
11188 gimple_omp_atomic_set_need_value (loadstmt);
11189 break;
11190 case OMP_ATOMIC_CAPTURE_NEW:
11191 *expr_p = rhs;
11192 gimple_omp_atomic_set_need_value (storestmt);
11193 break;
11194 default:
11195 *expr_p = NULL;
11196 break;
11199 return GS_ALL_DONE;
11202 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11203 body, and adding some EH bits. */
11205 static enum gimplify_status
11206 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11208 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11209 gimple *body_stmt;
11210 gtransaction *trans_stmt;
11211 gimple_seq body = NULL;
11212 int subcode = 0;
11214 /* Wrap the transaction body in a BIND_EXPR so we have a context
11215 where to put decls for OMP. */
11216 if (TREE_CODE (tbody) != BIND_EXPR)
11218 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11219 TREE_SIDE_EFFECTS (bind) = 1;
11220 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11221 TRANSACTION_EXPR_BODY (expr) = bind;
11224 push_gimplify_context ();
11225 temp = voidify_wrapper_expr (*expr_p, NULL);
11227 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11228 pop_gimplify_context (body_stmt);
11230 trans_stmt = gimple_build_transaction (body);
11231 if (TRANSACTION_EXPR_OUTER (expr))
11232 subcode = GTMA_IS_OUTER;
11233 else if (TRANSACTION_EXPR_RELAXED (expr))
11234 subcode = GTMA_IS_RELAXED;
11235 gimple_transaction_set_subcode (trans_stmt, subcode);
11237 gimplify_seq_add_stmt (pre_p, trans_stmt);
11239 if (temp)
11241 *expr_p = temp;
11242 return GS_OK;
11245 *expr_p = NULL_TREE;
11246 return GS_ALL_DONE;
11249 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11250 is the OMP_BODY of the original EXPR (which has already been
11251 gimplified so it's not present in the EXPR).
11253 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11255 static gimple *
11256 gimplify_omp_ordered (tree expr, gimple_seq body)
11258 tree c, decls;
11259 int failures = 0;
11260 unsigned int i;
11261 tree source_c = NULL_TREE;
11262 tree sink_c = NULL_TREE;
11264 if (gimplify_omp_ctxp)
11266 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11268 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11269 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11270 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11272 error_at (OMP_CLAUSE_LOCATION (c),
11273 "%<ordered%> construct with %<depend%> clause must be "
11274 "closely nested inside a loop with %<ordered%> clause "
11275 "with a parameter");
11276 failures++;
11278 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11279 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11281 bool fail = false;
11282 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11283 decls && TREE_CODE (decls) == TREE_LIST;
11284 decls = TREE_CHAIN (decls), ++i)
11285 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11286 continue;
11287 else if (TREE_VALUE (decls)
11288 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11290 error_at (OMP_CLAUSE_LOCATION (c),
11291 "variable %qE is not an iteration "
11292 "of outermost loop %d, expected %qE",
11293 TREE_VALUE (decls), i + 1,
11294 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11295 fail = true;
11296 failures++;
11298 else
11299 TREE_VALUE (decls)
11300 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11301 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11303 error_at (OMP_CLAUSE_LOCATION (c),
11304 "number of variables in %<depend(sink)%> "
11305 "clause does not match number of "
11306 "iteration variables");
11307 failures++;
11309 sink_c = c;
11311 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11312 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11314 if (source_c)
11316 error_at (OMP_CLAUSE_LOCATION (c),
11317 "more than one %<depend(source)%> clause on an "
11318 "%<ordered%> construct");
11319 failures++;
11321 else
11322 source_c = c;
11325 if (source_c && sink_c)
11327 error_at (OMP_CLAUSE_LOCATION (source_c),
11328 "%<depend(source)%> clause specified together with "
11329 "%<depend(sink:)%> clauses on the same construct");
11330 failures++;
11333 if (failures)
11334 return gimple_build_nop ();
11335 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11338 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11339 expression produces a value to be used as an operand inside a GIMPLE
11340 statement, the value will be stored back in *EXPR_P. This value will
11341 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11342 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11343 emitted in PRE_P and POST_P.
11345 Additionally, this process may overwrite parts of the input
11346 expression during gimplification. Ideally, it should be
11347 possible to do non-destructive gimplification.
11349 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11350 the expression needs to evaluate to a value to be used as
11351 an operand in a GIMPLE statement, this value will be stored in
11352 *EXPR_P on exit. This happens when the caller specifies one
11353 of fb_lvalue or fb_rvalue fallback flags.
11355 PRE_P will contain the sequence of GIMPLE statements corresponding
11356 to the evaluation of EXPR and all the side-effects that must
11357 be executed before the main expression. On exit, the last
11358 statement of PRE_P is the core statement being gimplified. For
11359 instance, when gimplifying 'if (++a)' the last statement in
11360 PRE_P will be 'if (t.1)' where t.1 is the result of
11361 pre-incrementing 'a'.
11363 POST_P will contain the sequence of GIMPLE statements corresponding
11364 to the evaluation of all the side-effects that must be executed
11365 after the main expression. If this is NULL, the post
11366 side-effects are stored at the end of PRE_P.
11368 The reason why the output is split in two is to handle post
11369 side-effects explicitly. In some cases, an expression may have
11370 inner and outer post side-effects which need to be emitted in
11371 an order different from the one given by the recursive
11372 traversal. For instance, for the expression (*p--)++ the post
11373 side-effects of '--' must actually occur *after* the post
11374 side-effects of '++'. However, gimplification will first visit
11375 the inner expression, so if a separate POST sequence was not
11376 used, the resulting sequence would be:
11378 1 t.1 = *p
11379 2 p = p - 1
11380 3 t.2 = t.1 + 1
11381 4 *p = t.2
11383 However, the post-decrement operation in line #2 must not be
11384 evaluated until after the store to *p at line #4, so the
11385 correct sequence should be:
11387 1 t.1 = *p
11388 2 t.2 = t.1 + 1
11389 3 *p = t.2
11390 4 p = p - 1
11392 So, by specifying a separate post queue, it is possible
11393 to emit the post side-effects in the correct order.
11394 If POST_P is NULL, an internal queue will be used. Before
11395 returning to the caller, the sequence POST_P is appended to
11396 the main output sequence PRE_P.
11398 GIMPLE_TEST_F points to a function that takes a tree T and
11399 returns nonzero if T is in the GIMPLE form requested by the
11400 caller. The GIMPLE predicates are in gimple.c.
11402 FALLBACK tells the function what sort of a temporary we want if
11403 gimplification cannot produce an expression that complies with
11404 GIMPLE_TEST_F.
11406 fb_none means that no temporary should be generated
11407 fb_rvalue means that an rvalue is OK to generate
11408 fb_lvalue means that an lvalue is OK to generate
11409 fb_either means that either is OK, but an lvalue is preferable.
11410 fb_mayfail means that gimplification may fail (in which case
11411 GS_ERROR will be returned)
11413 The return value is either GS_ERROR or GS_ALL_DONE, since this
11414 function iterates until EXPR is completely gimplified or an error
11415 occurs. */
11417 enum gimplify_status
11418 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11419 bool (*gimple_test_f) (tree), fallback_t fallback)
11421 tree tmp;
11422 gimple_seq internal_pre = NULL;
11423 gimple_seq internal_post = NULL;
11424 tree save_expr;
11425 bool is_statement;
11426 location_t saved_location;
11427 enum gimplify_status ret;
11428 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11429 tree label;
11431 save_expr = *expr_p;
11432 if (save_expr == NULL_TREE)
11433 return GS_ALL_DONE;
11435 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11436 is_statement = gimple_test_f == is_gimple_stmt;
11437 if (is_statement)
11438 gcc_assert (pre_p);
11440 /* Consistency checks. */
11441 if (gimple_test_f == is_gimple_reg)
11442 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11443 else if (gimple_test_f == is_gimple_val
11444 || gimple_test_f == is_gimple_call_addr
11445 || gimple_test_f == is_gimple_condexpr
11446 || gimple_test_f == is_gimple_mem_rhs
11447 || gimple_test_f == is_gimple_mem_rhs_or_call
11448 || gimple_test_f == is_gimple_reg_rhs
11449 || gimple_test_f == is_gimple_reg_rhs_or_call
11450 || gimple_test_f == is_gimple_asm_val
11451 || gimple_test_f == is_gimple_mem_ref_addr)
11452 gcc_assert (fallback & fb_rvalue);
11453 else if (gimple_test_f == is_gimple_min_lval
11454 || gimple_test_f == is_gimple_lvalue)
11455 gcc_assert (fallback & fb_lvalue);
11456 else if (gimple_test_f == is_gimple_addressable)
11457 gcc_assert (fallback & fb_either);
11458 else if (gimple_test_f == is_gimple_stmt)
11459 gcc_assert (fallback == fb_none);
11460 else
11462 /* We should have recognized the GIMPLE_TEST_F predicate to
11463 know what kind of fallback to use in case a temporary is
11464 needed to hold the value or address of *EXPR_P. */
11465 gcc_unreachable ();
11468 /* We used to check the predicate here and return immediately if it
11469 succeeds. This is wrong; the design is for gimplification to be
11470 idempotent, and for the predicates to only test for valid forms, not
11471 whether they are fully simplified. */
11472 if (pre_p == NULL)
11473 pre_p = &internal_pre;
11475 if (post_p == NULL)
11476 post_p = &internal_post;
11478 /* Remember the last statements added to PRE_P and POST_P. Every
11479 new statement added by the gimplification helpers needs to be
11480 annotated with location information. To centralize the
11481 responsibility, we remember the last statement that had been
11482 added to both queues before gimplifying *EXPR_P. If
11483 gimplification produces new statements in PRE_P and POST_P, those
11484 statements will be annotated with the same location information
11485 as *EXPR_P. */
11486 pre_last_gsi = gsi_last (*pre_p);
11487 post_last_gsi = gsi_last (*post_p);
11489 saved_location = input_location;
11490 if (save_expr != error_mark_node
11491 && EXPR_HAS_LOCATION (*expr_p))
11492 input_location = EXPR_LOCATION (*expr_p);
11494 /* Loop over the specific gimplifiers until the toplevel node
11495 remains the same. */
11498 /* Strip away as many useless type conversions as possible
11499 at the toplevel. */
11500 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11502 /* Remember the expr. */
11503 save_expr = *expr_p;
11505 /* Die, die, die, my darling. */
11506 if (error_operand_p (save_expr))
11508 ret = GS_ERROR;
11509 break;
11512 /* Do any language-specific gimplification. */
11513 ret = ((enum gimplify_status)
11514 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11515 if (ret == GS_OK)
11517 if (*expr_p == NULL_TREE)
11518 break;
11519 if (*expr_p != save_expr)
11520 continue;
11522 else if (ret != GS_UNHANDLED)
11523 break;
11525 /* Make sure that all the cases set 'ret' appropriately. */
11526 ret = GS_UNHANDLED;
11527 switch (TREE_CODE (*expr_p))
11529 /* First deal with the special cases. */
11531 case POSTINCREMENT_EXPR:
11532 case POSTDECREMENT_EXPR:
11533 case PREINCREMENT_EXPR:
11534 case PREDECREMENT_EXPR:
11535 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11536 fallback != fb_none,
11537 TREE_TYPE (*expr_p));
11538 break;
11540 case VIEW_CONVERT_EXPR:
11541 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11542 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11544 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11545 post_p, is_gimple_val, fb_rvalue);
11546 recalculate_side_effects (*expr_p);
11547 break;
11549 /* Fallthru. */
11551 case ARRAY_REF:
11552 case ARRAY_RANGE_REF:
11553 case REALPART_EXPR:
11554 case IMAGPART_EXPR:
11555 case COMPONENT_REF:
11556 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11557 fallback ? fallback : fb_rvalue);
11558 break;
11560 case COND_EXPR:
11561 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11563 /* C99 code may assign to an array in a structure value of a
11564 conditional expression, and this has undefined behavior
11565 only on execution, so create a temporary if an lvalue is
11566 required. */
11567 if (fallback == fb_lvalue)
11569 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11570 mark_addressable (*expr_p);
11571 ret = GS_OK;
11573 break;
11575 case CALL_EXPR:
11576 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11578 /* C99 code may assign to an array in a structure returned
11579 from a function, and this has undefined behavior only on
11580 execution, so create a temporary if an lvalue is
11581 required. */
11582 if (fallback == fb_lvalue)
11584 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11585 mark_addressable (*expr_p);
11586 ret = GS_OK;
11588 break;
11590 case TREE_LIST:
11591 gcc_unreachable ();
11593 case COMPOUND_EXPR:
11594 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11595 break;
11597 case COMPOUND_LITERAL_EXPR:
11598 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11599 gimple_test_f, fallback);
11600 break;
11602 case MODIFY_EXPR:
11603 case INIT_EXPR:
11604 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11605 fallback != fb_none);
11606 break;
11608 case TRUTH_ANDIF_EXPR:
11609 case TRUTH_ORIF_EXPR:
11611 /* Preserve the original type of the expression and the
11612 source location of the outer expression. */
11613 tree org_type = TREE_TYPE (*expr_p);
11614 *expr_p = gimple_boolify (*expr_p);
11615 *expr_p = build3_loc (input_location, COND_EXPR,
11616 org_type, *expr_p,
11617 fold_convert_loc
11618 (input_location,
11619 org_type, boolean_true_node),
11620 fold_convert_loc
11621 (input_location,
11622 org_type, boolean_false_node));
11623 ret = GS_OK;
11624 break;
11627 case TRUTH_NOT_EXPR:
11629 tree type = TREE_TYPE (*expr_p);
11630 /* The parsers are careful to generate TRUTH_NOT_EXPR
11631 only with operands that are always zero or one.
11632 We do not fold here but handle the only interesting case
11633 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11634 *expr_p = gimple_boolify (*expr_p);
11635 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11636 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11637 TREE_TYPE (*expr_p),
11638 TREE_OPERAND (*expr_p, 0));
11639 else
11640 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11641 TREE_TYPE (*expr_p),
11642 TREE_OPERAND (*expr_p, 0),
11643 build_int_cst (TREE_TYPE (*expr_p), 1));
11644 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11645 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11646 ret = GS_OK;
11647 break;
11650 case ADDR_EXPR:
11651 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11652 break;
11654 case ANNOTATE_EXPR:
11656 tree cond = TREE_OPERAND (*expr_p, 0);
11657 tree kind = TREE_OPERAND (*expr_p, 1);
11658 tree data = TREE_OPERAND (*expr_p, 2);
11659 tree type = TREE_TYPE (cond);
11660 if (!INTEGRAL_TYPE_P (type))
11662 *expr_p = cond;
11663 ret = GS_OK;
11664 break;
11666 tree tmp = create_tmp_var (type);
11667 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11668 gcall *call
11669 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11670 gimple_call_set_lhs (call, tmp);
11671 gimplify_seq_add_stmt (pre_p, call);
11672 *expr_p = tmp;
11673 ret = GS_ALL_DONE;
11674 break;
11677 case VA_ARG_EXPR:
11678 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11679 break;
11681 CASE_CONVERT:
11682 if (IS_EMPTY_STMT (*expr_p))
11684 ret = GS_ALL_DONE;
11685 break;
11688 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11689 || fallback == fb_none)
11691 /* Just strip a conversion to void (or in void context) and
11692 try again. */
11693 *expr_p = TREE_OPERAND (*expr_p, 0);
11694 ret = GS_OK;
11695 break;
11698 ret = gimplify_conversion (expr_p);
11699 if (ret == GS_ERROR)
11700 break;
11701 if (*expr_p != save_expr)
11702 break;
11703 /* FALLTHRU */
11705 case FIX_TRUNC_EXPR:
11706 /* unary_expr: ... | '(' cast ')' val | ... */
11707 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11708 is_gimple_val, fb_rvalue);
11709 recalculate_side_effects (*expr_p);
11710 break;
11712 case INDIRECT_REF:
11714 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11715 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11716 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11718 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11719 if (*expr_p != save_expr)
11721 ret = GS_OK;
11722 break;
11725 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11726 is_gimple_reg, fb_rvalue);
11727 if (ret == GS_ERROR)
11728 break;
11730 recalculate_side_effects (*expr_p);
11731 *expr_p = fold_build2_loc (input_location, MEM_REF,
11732 TREE_TYPE (*expr_p),
11733 TREE_OPERAND (*expr_p, 0),
11734 build_int_cst (saved_ptr_type, 0));
11735 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11736 TREE_THIS_NOTRAP (*expr_p) = notrap;
11737 ret = GS_OK;
11738 break;
11741 /* We arrive here through the various re-gimplifcation paths. */
11742 case MEM_REF:
11743 /* First try re-folding the whole thing. */
11744 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11745 TREE_OPERAND (*expr_p, 0),
11746 TREE_OPERAND (*expr_p, 1));
11747 if (tmp)
11749 REF_REVERSE_STORAGE_ORDER (tmp)
11750 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11751 *expr_p = tmp;
11752 recalculate_side_effects (*expr_p);
11753 ret = GS_OK;
11754 break;
11756 /* Avoid re-gimplifying the address operand if it is already
11757 in suitable form. Re-gimplifying would mark the address
11758 operand addressable. Always gimplify when not in SSA form
11759 as we still may have to gimplify decls with value-exprs. */
11760 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11761 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11763 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11764 is_gimple_mem_ref_addr, fb_rvalue);
11765 if (ret == GS_ERROR)
11766 break;
11768 recalculate_side_effects (*expr_p);
11769 ret = GS_ALL_DONE;
11770 break;
11772 /* Constants need not be gimplified. */
11773 case INTEGER_CST:
11774 case REAL_CST:
11775 case FIXED_CST:
11776 case STRING_CST:
11777 case COMPLEX_CST:
11778 case VECTOR_CST:
11779 /* Drop the overflow flag on constants, we do not want
11780 that in the GIMPLE IL. */
11781 if (TREE_OVERFLOW_P (*expr_p))
11782 *expr_p = drop_tree_overflow (*expr_p);
11783 ret = GS_ALL_DONE;
11784 break;
11786 case CONST_DECL:
11787 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11788 CONST_DECL node. Otherwise the decl is replaceable by its
11789 value. */
11790 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11791 if (fallback & fb_lvalue)
11792 ret = GS_ALL_DONE;
11793 else
11795 *expr_p = DECL_INITIAL (*expr_p);
11796 ret = GS_OK;
11798 break;
11800 case DECL_EXPR:
11801 ret = gimplify_decl_expr (expr_p, pre_p);
11802 break;
11804 case BIND_EXPR:
11805 ret = gimplify_bind_expr (expr_p, pre_p);
11806 break;
11808 case LOOP_EXPR:
11809 ret = gimplify_loop_expr (expr_p, pre_p);
11810 break;
11812 case SWITCH_EXPR:
11813 ret = gimplify_switch_expr (expr_p, pre_p);
11814 break;
11816 case EXIT_EXPR:
11817 ret = gimplify_exit_expr (expr_p);
11818 break;
11820 case GOTO_EXPR:
11821 /* If the target is not LABEL, then it is a computed jump
11822 and the target needs to be gimplified. */
11823 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11825 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11826 NULL, is_gimple_val, fb_rvalue);
11827 if (ret == GS_ERROR)
11828 break;
11830 gimplify_seq_add_stmt (pre_p,
11831 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11832 ret = GS_ALL_DONE;
11833 break;
11835 case PREDICT_EXPR:
11836 gimplify_seq_add_stmt (pre_p,
11837 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11838 PREDICT_EXPR_OUTCOME (*expr_p)));
11839 ret = GS_ALL_DONE;
11840 break;
11842 case LABEL_EXPR:
11843 ret = gimplify_label_expr (expr_p, pre_p);
11844 label = LABEL_EXPR_LABEL (*expr_p);
11845 gcc_assert (decl_function_context (label) == current_function_decl);
11847 /* If the label is used in a goto statement, or address of the label
11848 is taken, we need to unpoison all variables that were seen so far.
11849 Doing so would prevent us from reporting a false positives. */
11850 if (asan_poisoned_variables
11851 && asan_used_labels != NULL
11852 && asan_used_labels->contains (label))
11853 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11854 break;
11856 case CASE_LABEL_EXPR:
11857 ret = gimplify_case_label_expr (expr_p, pre_p);
11859 if (gimplify_ctxp->live_switch_vars)
11860 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11861 pre_p);
11862 break;
11864 case RETURN_EXPR:
11865 ret = gimplify_return_expr (*expr_p, pre_p);
11866 break;
11868 case CONSTRUCTOR:
11869 /* Don't reduce this in place; let gimplify_init_constructor work its
11870 magic. Buf if we're just elaborating this for side effects, just
11871 gimplify any element that has side-effects. */
11872 if (fallback == fb_none)
11874 unsigned HOST_WIDE_INT ix;
11875 tree val;
11876 tree temp = NULL_TREE;
11877 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11878 if (TREE_SIDE_EFFECTS (val))
11879 append_to_statement_list (val, &temp);
11881 *expr_p = temp;
11882 ret = temp ? GS_OK : GS_ALL_DONE;
11884 /* C99 code may assign to an array in a constructed
11885 structure or union, and this has undefined behavior only
11886 on execution, so create a temporary if an lvalue is
11887 required. */
11888 else if (fallback == fb_lvalue)
11890 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11891 mark_addressable (*expr_p);
11892 ret = GS_OK;
11894 else
11895 ret = GS_ALL_DONE;
11896 break;
11898 /* The following are special cases that are not handled by the
11899 original GIMPLE grammar. */
11901 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11902 eliminated. */
11903 case SAVE_EXPR:
11904 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11905 break;
11907 case BIT_FIELD_REF:
11908 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11909 post_p, is_gimple_lvalue, fb_either);
11910 recalculate_side_effects (*expr_p);
11911 break;
11913 case TARGET_MEM_REF:
11915 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11917 if (TMR_BASE (*expr_p))
11918 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11919 post_p, is_gimple_mem_ref_addr, fb_either);
11920 if (TMR_INDEX (*expr_p))
11921 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11922 post_p, is_gimple_val, fb_rvalue);
11923 if (TMR_INDEX2 (*expr_p))
11924 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11925 post_p, is_gimple_val, fb_rvalue);
11926 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11927 ret = MIN (r0, r1);
11929 break;
11931 case NON_LVALUE_EXPR:
11932 /* This should have been stripped above. */
11933 gcc_unreachable ();
11935 case ASM_EXPR:
11936 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11937 break;
11939 case TRY_FINALLY_EXPR:
11940 case TRY_CATCH_EXPR:
11942 gimple_seq eval, cleanup;
11943 gtry *try_;
11945 /* Calls to destructors are generated automatically in FINALLY/CATCH
11946 block. They should have location as UNKNOWN_LOCATION. However,
11947 gimplify_call_expr will reset these call stmts to input_location
11948 if it finds stmt's location is unknown. To prevent resetting for
11949 destructors, we set the input_location to unknown.
11950 Note that this only affects the destructor calls in FINALLY/CATCH
11951 block, and will automatically reset to its original value by the
11952 end of gimplify_expr. */
11953 input_location = UNKNOWN_LOCATION;
11954 eval = cleanup = NULL;
11955 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11956 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11957 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11958 if (gimple_seq_empty_p (cleanup))
11960 gimple_seq_add_seq (pre_p, eval);
11961 ret = GS_ALL_DONE;
11962 break;
11964 try_ = gimple_build_try (eval, cleanup,
11965 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11966 ? GIMPLE_TRY_FINALLY
11967 : GIMPLE_TRY_CATCH);
11968 if (EXPR_HAS_LOCATION (save_expr))
11969 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11970 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11971 gimple_set_location (try_, saved_location);
11972 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11973 gimple_try_set_catch_is_cleanup (try_,
11974 TRY_CATCH_IS_CLEANUP (*expr_p));
11975 gimplify_seq_add_stmt (pre_p, try_);
11976 ret = GS_ALL_DONE;
11977 break;
11980 case CLEANUP_POINT_EXPR:
11981 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11982 break;
11984 case TARGET_EXPR:
11985 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11986 break;
11988 case CATCH_EXPR:
11990 gimple *c;
11991 gimple_seq handler = NULL;
11992 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11993 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11994 gimplify_seq_add_stmt (pre_p, c);
11995 ret = GS_ALL_DONE;
11996 break;
11999 case EH_FILTER_EXPR:
12001 gimple *ehf;
12002 gimple_seq failure = NULL;
12004 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
12005 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
12006 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
12007 gimplify_seq_add_stmt (pre_p, ehf);
12008 ret = GS_ALL_DONE;
12009 break;
12012 case OBJ_TYPE_REF:
12014 enum gimplify_status r0, r1;
12015 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
12016 post_p, is_gimple_val, fb_rvalue);
12017 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
12018 post_p, is_gimple_val, fb_rvalue);
12019 TREE_SIDE_EFFECTS (*expr_p) = 0;
12020 ret = MIN (r0, r1);
12022 break;
12024 case LABEL_DECL:
12025 /* We get here when taking the address of a label. We mark
12026 the label as "forced"; meaning it can never be removed and
12027 it is a potential target for any computed goto. */
12028 FORCED_LABEL (*expr_p) = 1;
12029 ret = GS_ALL_DONE;
12030 break;
12032 case STATEMENT_LIST:
12033 ret = gimplify_statement_list (expr_p, pre_p);
12034 break;
12036 case WITH_SIZE_EXPR:
12038 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12039 post_p == &internal_post ? NULL : post_p,
12040 gimple_test_f, fallback);
12041 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12042 is_gimple_val, fb_rvalue);
12043 ret = GS_ALL_DONE;
12045 break;
12047 case VAR_DECL:
12048 case PARM_DECL:
12049 ret = gimplify_var_or_parm_decl (expr_p);
12050 break;
12052 case RESULT_DECL:
12053 /* When within an OMP context, notice uses of variables. */
12054 if (gimplify_omp_ctxp)
12055 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
12056 ret = GS_ALL_DONE;
12057 break;
12059 case DEBUG_EXPR_DECL:
12060 gcc_unreachable ();
12062 case DEBUG_BEGIN_STMT:
12063 gimplify_seq_add_stmt (pre_p,
12064 gimple_build_debug_begin_stmt
12065 (TREE_BLOCK (*expr_p),
12066 EXPR_LOCATION (*expr_p)));
12067 ret = GS_ALL_DONE;
12068 *expr_p = NULL;
12069 break;
12071 case SSA_NAME:
12072 /* Allow callbacks into the gimplifier during optimization. */
12073 ret = GS_ALL_DONE;
12074 break;
12076 case OMP_PARALLEL:
12077 gimplify_omp_parallel (expr_p, pre_p);
12078 ret = GS_ALL_DONE;
12079 break;
12081 case OMP_TASK:
12082 gimplify_omp_task (expr_p, pre_p);
12083 ret = GS_ALL_DONE;
12084 break;
12086 case OMP_FOR:
12087 case OMP_SIMD:
12088 case OMP_DISTRIBUTE:
12089 case OMP_TASKLOOP:
12090 case OACC_LOOP:
12091 ret = gimplify_omp_for (expr_p, pre_p);
12092 break;
12094 case OACC_CACHE:
12095 gimplify_oacc_cache (expr_p, pre_p);
12096 ret = GS_ALL_DONE;
12097 break;
12099 case OACC_DECLARE:
12100 gimplify_oacc_declare (expr_p, pre_p);
12101 ret = GS_ALL_DONE;
12102 break;
12104 case OACC_HOST_DATA:
12105 case OACC_DATA:
12106 case OACC_KERNELS:
12107 case OACC_PARALLEL:
12108 case OMP_SECTIONS:
12109 case OMP_SINGLE:
12110 case OMP_TARGET:
12111 case OMP_TARGET_DATA:
12112 case OMP_TEAMS:
12113 gimplify_omp_workshare (expr_p, pre_p);
12114 ret = GS_ALL_DONE;
12115 break;
12117 case OACC_ENTER_DATA:
12118 case OACC_EXIT_DATA:
12119 case OACC_UPDATE:
12120 case OMP_TARGET_UPDATE:
12121 case OMP_TARGET_ENTER_DATA:
12122 case OMP_TARGET_EXIT_DATA:
12123 gimplify_omp_target_update (expr_p, pre_p);
12124 ret = GS_ALL_DONE;
12125 break;
12127 case OMP_SECTION:
12128 case OMP_MASTER:
12129 case OMP_TASKGROUP:
12130 case OMP_ORDERED:
12131 case OMP_CRITICAL:
12133 gimple_seq body = NULL;
12134 gimple *g;
12136 gimplify_and_add (OMP_BODY (*expr_p), &body);
12137 switch (TREE_CODE (*expr_p))
12139 case OMP_SECTION:
12140 g = gimple_build_omp_section (body);
12141 break;
12142 case OMP_MASTER:
12143 g = gimple_build_omp_master (body);
12144 break;
12145 case OMP_TASKGROUP:
12147 gimple_seq cleanup = NULL;
12148 tree fn
12149 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12150 g = gimple_build_call (fn, 0);
12151 gimple_seq_add_stmt (&cleanup, g);
12152 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12153 body = NULL;
12154 gimple_seq_add_stmt (&body, g);
12155 g = gimple_build_omp_taskgroup (body);
12157 break;
12158 case OMP_ORDERED:
12159 g = gimplify_omp_ordered (*expr_p, body);
12160 break;
12161 case OMP_CRITICAL:
12162 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12163 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12164 gimplify_adjust_omp_clauses (pre_p, body,
12165 &OMP_CRITICAL_CLAUSES (*expr_p),
12166 OMP_CRITICAL);
12167 g = gimple_build_omp_critical (body,
12168 OMP_CRITICAL_NAME (*expr_p),
12169 OMP_CRITICAL_CLAUSES (*expr_p));
12170 break;
12171 default:
12172 gcc_unreachable ();
12174 gimplify_seq_add_stmt (pre_p, g);
12175 ret = GS_ALL_DONE;
12176 break;
12179 case OMP_ATOMIC:
12180 case OMP_ATOMIC_READ:
12181 case OMP_ATOMIC_CAPTURE_OLD:
12182 case OMP_ATOMIC_CAPTURE_NEW:
12183 ret = gimplify_omp_atomic (expr_p, pre_p);
12184 break;
12186 case TRANSACTION_EXPR:
12187 ret = gimplify_transaction (expr_p, pre_p);
12188 break;
12190 case TRUTH_AND_EXPR:
12191 case TRUTH_OR_EXPR:
12192 case TRUTH_XOR_EXPR:
12194 tree orig_type = TREE_TYPE (*expr_p);
12195 tree new_type, xop0, xop1;
12196 *expr_p = gimple_boolify (*expr_p);
12197 new_type = TREE_TYPE (*expr_p);
12198 if (!useless_type_conversion_p (orig_type, new_type))
12200 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12201 ret = GS_OK;
12202 break;
12205 /* Boolified binary truth expressions are semantically equivalent
12206 to bitwise binary expressions. Canonicalize them to the
12207 bitwise variant. */
12208 switch (TREE_CODE (*expr_p))
12210 case TRUTH_AND_EXPR:
12211 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12212 break;
12213 case TRUTH_OR_EXPR:
12214 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12215 break;
12216 case TRUTH_XOR_EXPR:
12217 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12218 break;
12219 default:
12220 break;
12222 /* Now make sure that operands have compatible type to
12223 expression's new_type. */
12224 xop0 = TREE_OPERAND (*expr_p, 0);
12225 xop1 = TREE_OPERAND (*expr_p, 1);
12226 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12227 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12228 new_type,
12229 xop0);
12230 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12231 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12232 new_type,
12233 xop1);
12234 /* Continue classified as tcc_binary. */
12235 goto expr_2;
12238 case VEC_COND_EXPR:
12240 enum gimplify_status r0, r1, r2;
12242 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12243 post_p, is_gimple_condexpr, fb_rvalue);
12244 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12245 post_p, is_gimple_val, fb_rvalue);
12246 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12247 post_p, is_gimple_val, fb_rvalue);
12249 ret = MIN (MIN (r0, r1), r2);
12250 recalculate_side_effects (*expr_p);
12252 break;
12254 case VEC_PERM_EXPR:
12255 /* Classified as tcc_expression. */
12256 goto expr_3;
12258 case BIT_INSERT_EXPR:
12259 /* Argument 3 is a constant. */
12260 goto expr_2;
12262 case POINTER_PLUS_EXPR:
12264 enum gimplify_status r0, r1;
12265 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12266 post_p, is_gimple_val, fb_rvalue);
12267 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12268 post_p, is_gimple_val, fb_rvalue);
12269 recalculate_side_effects (*expr_p);
12270 ret = MIN (r0, r1);
12271 break;
12274 default:
12275 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12277 case tcc_comparison:
12278 /* Handle comparison of objects of non scalar mode aggregates
12279 with a call to memcmp. It would be nice to only have to do
12280 this for variable-sized objects, but then we'd have to allow
12281 the same nest of reference nodes we allow for MODIFY_EXPR and
12282 that's too complex.
12284 Compare scalar mode aggregates as scalar mode values. Using
12285 memcmp for them would be very inefficient at best, and is
12286 plain wrong if bitfields are involved. */
12288 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12290 /* Vector comparisons need no boolification. */
12291 if (TREE_CODE (type) == VECTOR_TYPE)
12292 goto expr_2;
12293 else if (!AGGREGATE_TYPE_P (type))
12295 tree org_type = TREE_TYPE (*expr_p);
12296 *expr_p = gimple_boolify (*expr_p);
12297 if (!useless_type_conversion_p (org_type,
12298 TREE_TYPE (*expr_p)))
12300 *expr_p = fold_convert_loc (input_location,
12301 org_type, *expr_p);
12302 ret = GS_OK;
12304 else
12305 goto expr_2;
12307 else if (TYPE_MODE (type) != BLKmode)
12308 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12309 else
12310 ret = gimplify_variable_sized_compare (expr_p);
12312 break;
12315 /* If *EXPR_P does not need to be special-cased, handle it
12316 according to its class. */
12317 case tcc_unary:
12318 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12319 post_p, is_gimple_val, fb_rvalue);
12320 break;
12322 case tcc_binary:
12323 expr_2:
12325 enum gimplify_status r0, r1;
12327 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12328 post_p, is_gimple_val, fb_rvalue);
12329 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12330 post_p, is_gimple_val, fb_rvalue);
12332 ret = MIN (r0, r1);
12333 break;
12336 expr_3:
12338 enum gimplify_status r0, r1, r2;
12340 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12341 post_p, is_gimple_val, fb_rvalue);
12342 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12343 post_p, is_gimple_val, fb_rvalue);
12344 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12345 post_p, is_gimple_val, fb_rvalue);
12347 ret = MIN (MIN (r0, r1), r2);
12348 break;
12351 case tcc_declaration:
12352 case tcc_constant:
12353 ret = GS_ALL_DONE;
12354 goto dont_recalculate;
12356 default:
12357 gcc_unreachable ();
12360 recalculate_side_effects (*expr_p);
12362 dont_recalculate:
12363 break;
12366 gcc_assert (*expr_p || ret != GS_OK);
12368 while (ret == GS_OK);
12370 /* If we encountered an error_mark somewhere nested inside, either
12371 stub out the statement or propagate the error back out. */
12372 if (ret == GS_ERROR)
12374 if (is_statement)
12375 *expr_p = NULL;
12376 goto out;
12379 /* This was only valid as a return value from the langhook, which
12380 we handled. Make sure it doesn't escape from any other context. */
12381 gcc_assert (ret != GS_UNHANDLED);
12383 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12385 /* We aren't looking for a value, and we don't have a valid
12386 statement. If it doesn't have side-effects, throw it away.
12387 We can also get here with code such as "*&&L;", where L is
12388 a LABEL_DECL that is marked as FORCED_LABEL. */
12389 if (TREE_CODE (*expr_p) == LABEL_DECL
12390 || !TREE_SIDE_EFFECTS (*expr_p))
12391 *expr_p = NULL;
12392 else if (!TREE_THIS_VOLATILE (*expr_p))
12394 /* This is probably a _REF that contains something nested that
12395 has side effects. Recurse through the operands to find it. */
12396 enum tree_code code = TREE_CODE (*expr_p);
12398 switch (code)
12400 case COMPONENT_REF:
12401 case REALPART_EXPR:
12402 case IMAGPART_EXPR:
12403 case VIEW_CONVERT_EXPR:
12404 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12405 gimple_test_f, fallback);
12406 break;
12408 case ARRAY_REF:
12409 case ARRAY_RANGE_REF:
12410 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12411 gimple_test_f, fallback);
12412 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12413 gimple_test_f, fallback);
12414 break;
12416 default:
12417 /* Anything else with side-effects must be converted to
12418 a valid statement before we get here. */
12419 gcc_unreachable ();
12422 *expr_p = NULL;
12424 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12425 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12427 /* Historically, the compiler has treated a bare reference
12428 to a non-BLKmode volatile lvalue as forcing a load. */
12429 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12431 /* Normally, we do not want to create a temporary for a
12432 TREE_ADDRESSABLE type because such a type should not be
12433 copied by bitwise-assignment. However, we make an
12434 exception here, as all we are doing here is ensuring that
12435 we read the bytes that make up the type. We use
12436 create_tmp_var_raw because create_tmp_var will abort when
12437 given a TREE_ADDRESSABLE type. */
12438 tree tmp = create_tmp_var_raw (type, "vol");
12439 gimple_add_tmp_var (tmp);
12440 gimplify_assign (tmp, *expr_p, pre_p);
12441 *expr_p = NULL;
12443 else
12444 /* We can't do anything useful with a volatile reference to
12445 an incomplete type, so just throw it away. Likewise for
12446 a BLKmode type, since any implicit inner load should
12447 already have been turned into an explicit one by the
12448 gimplification process. */
12449 *expr_p = NULL;
12452 /* If we are gimplifying at the statement level, we're done. Tack
12453 everything together and return. */
12454 if (fallback == fb_none || is_statement)
12456 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12457 it out for GC to reclaim it. */
12458 *expr_p = NULL_TREE;
12460 if (!gimple_seq_empty_p (internal_pre)
12461 || !gimple_seq_empty_p (internal_post))
12463 gimplify_seq_add_seq (&internal_pre, internal_post);
12464 gimplify_seq_add_seq (pre_p, internal_pre);
12467 /* The result of gimplifying *EXPR_P is going to be the last few
12468 statements in *PRE_P and *POST_P. Add location information
12469 to all the statements that were added by the gimplification
12470 helpers. */
12471 if (!gimple_seq_empty_p (*pre_p))
12472 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12474 if (!gimple_seq_empty_p (*post_p))
12475 annotate_all_with_location_after (*post_p, post_last_gsi,
12476 input_location);
12478 goto out;
12481 #ifdef ENABLE_GIMPLE_CHECKING
12482 if (*expr_p)
12484 enum tree_code code = TREE_CODE (*expr_p);
12485 /* These expressions should already be in gimple IR form. */
12486 gcc_assert (code != MODIFY_EXPR
12487 && code != ASM_EXPR
12488 && code != BIND_EXPR
12489 && code != CATCH_EXPR
12490 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12491 && code != EH_FILTER_EXPR
12492 && code != GOTO_EXPR
12493 && code != LABEL_EXPR
12494 && code != LOOP_EXPR
12495 && code != SWITCH_EXPR
12496 && code != TRY_FINALLY_EXPR
12497 && code != OACC_PARALLEL
12498 && code != OACC_KERNELS
12499 && code != OACC_DATA
12500 && code != OACC_HOST_DATA
12501 && code != OACC_DECLARE
12502 && code != OACC_UPDATE
12503 && code != OACC_ENTER_DATA
12504 && code != OACC_EXIT_DATA
12505 && code != OACC_CACHE
12506 && code != OMP_CRITICAL
12507 && code != OMP_FOR
12508 && code != OACC_LOOP
12509 && code != OMP_MASTER
12510 && code != OMP_TASKGROUP
12511 && code != OMP_ORDERED
12512 && code != OMP_PARALLEL
12513 && code != OMP_SECTIONS
12514 && code != OMP_SECTION
12515 && code != OMP_SINGLE);
12517 #endif
12519 /* Otherwise we're gimplifying a subexpression, so the resulting
12520 value is interesting. If it's a valid operand that matches
12521 GIMPLE_TEST_F, we're done. Unless we are handling some
12522 post-effects internally; if that's the case, we need to copy into
12523 a temporary before adding the post-effects to POST_P. */
12524 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12525 goto out;
12527 /* Otherwise, we need to create a new temporary for the gimplified
12528 expression. */
12530 /* We can't return an lvalue if we have an internal postqueue. The
12531 object the lvalue refers to would (probably) be modified by the
12532 postqueue; we need to copy the value out first, which means an
12533 rvalue. */
12534 if ((fallback & fb_lvalue)
12535 && gimple_seq_empty_p (internal_post)
12536 && is_gimple_addressable (*expr_p))
12538 /* An lvalue will do. Take the address of the expression, store it
12539 in a temporary, and replace the expression with an INDIRECT_REF of
12540 that temporary. */
12541 tree ref_alias_type = reference_alias_ptr_type (*expr_p);
12542 unsigned int ref_align = get_object_alignment (*expr_p);
12543 tree ref_type = TREE_TYPE (*expr_p);
12544 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12545 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12546 if (TYPE_ALIGN (ref_type) != ref_align)
12547 ref_type = build_aligned_type (ref_type, ref_align);
12548 *expr_p = build2 (MEM_REF, ref_type,
12549 tmp, build_zero_cst (ref_alias_type));
12551 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12553 /* An rvalue will do. Assign the gimplified expression into a
12554 new temporary TMP and replace the original expression with
12555 TMP. First, make sure that the expression has a type so that
12556 it can be assigned into a temporary. */
12557 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12558 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12560 else
12562 #ifdef ENABLE_GIMPLE_CHECKING
12563 if (!(fallback & fb_mayfail))
12565 fprintf (stderr, "gimplification failed:\n");
12566 print_generic_expr (stderr, *expr_p);
12567 debug_tree (*expr_p);
12568 internal_error ("gimplification failed");
12570 #endif
12571 gcc_assert (fallback & fb_mayfail);
12573 /* If this is an asm statement, and the user asked for the
12574 impossible, don't die. Fail and let gimplify_asm_expr
12575 issue an error. */
12576 ret = GS_ERROR;
12577 goto out;
12580 /* Make sure the temporary matches our predicate. */
12581 gcc_assert ((*gimple_test_f) (*expr_p));
12583 if (!gimple_seq_empty_p (internal_post))
12585 annotate_all_with_location (internal_post, input_location);
12586 gimplify_seq_add_seq (pre_p, internal_post);
12589 out:
12590 input_location = saved_location;
12591 return ret;
12594 /* Like gimplify_expr but make sure the gimplified result is not itself
12595 a SSA name (but a decl if it were). Temporaries required by
12596 evaluating *EXPR_P may be still SSA names. */
12598 static enum gimplify_status
12599 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12600 bool (*gimple_test_f) (tree), fallback_t fallback,
12601 bool allow_ssa)
12603 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12604 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12605 gimple_test_f, fallback);
12606 if (! allow_ssa
12607 && TREE_CODE (*expr_p) == SSA_NAME)
12609 tree name = *expr_p;
12610 if (was_ssa_name_p)
12611 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12612 else
12614 /* Avoid the extra copy if possible. */
12615 *expr_p = create_tmp_reg (TREE_TYPE (name));
12616 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12617 release_ssa_name (name);
12620 return ret;
12623 /* Look through TYPE for variable-sized objects and gimplify each such
12624 size that we find. Add to LIST_P any statements generated. */
12626 void
12627 gimplify_type_sizes (tree type, gimple_seq *list_p)
12629 tree field, t;
12631 if (type == NULL || type == error_mark_node)
12632 return;
12634 /* We first do the main variant, then copy into any other variants. */
12635 type = TYPE_MAIN_VARIANT (type);
12637 /* Avoid infinite recursion. */
12638 if (TYPE_SIZES_GIMPLIFIED (type))
12639 return;
12641 TYPE_SIZES_GIMPLIFIED (type) = 1;
12643 switch (TREE_CODE (type))
12645 case INTEGER_TYPE:
12646 case ENUMERAL_TYPE:
12647 case BOOLEAN_TYPE:
12648 case REAL_TYPE:
12649 case FIXED_POINT_TYPE:
12650 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12651 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12653 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12655 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12656 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12658 break;
12660 case ARRAY_TYPE:
12661 /* These types may not have declarations, so handle them here. */
12662 gimplify_type_sizes (TREE_TYPE (type), list_p);
12663 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12664 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12665 with assigned stack slots, for -O1+ -g they should be tracked
12666 by VTA. */
12667 if (!(TYPE_NAME (type)
12668 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12669 && DECL_IGNORED_P (TYPE_NAME (type)))
12670 && TYPE_DOMAIN (type)
12671 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12673 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12674 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12675 DECL_IGNORED_P (t) = 0;
12676 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12677 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12678 DECL_IGNORED_P (t) = 0;
12680 break;
12682 case RECORD_TYPE:
12683 case UNION_TYPE:
12684 case QUAL_UNION_TYPE:
12685 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12686 if (TREE_CODE (field) == FIELD_DECL)
12688 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12689 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12690 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12691 gimplify_type_sizes (TREE_TYPE (field), list_p);
12693 break;
12695 case POINTER_TYPE:
12696 case REFERENCE_TYPE:
12697 /* We used to recurse on the pointed-to type here, which turned out to
12698 be incorrect because its definition might refer to variables not
12699 yet initialized at this point if a forward declaration is involved.
12701 It was actually useful for anonymous pointed-to types to ensure
12702 that the sizes evaluation dominates every possible later use of the
12703 values. Restricting to such types here would be safe since there
12704 is no possible forward declaration around, but would introduce an
12705 undesirable middle-end semantic to anonymity. We then defer to
12706 front-ends the responsibility of ensuring that the sizes are
12707 evaluated both early and late enough, e.g. by attaching artificial
12708 type declarations to the tree. */
12709 break;
12711 default:
12712 break;
12715 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12716 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12718 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12720 TYPE_SIZE (t) = TYPE_SIZE (type);
12721 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12722 TYPE_SIZES_GIMPLIFIED (t) = 1;
12726 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12727 a size or position, has had all of its SAVE_EXPRs evaluated.
12728 We add any required statements to *STMT_P. */
12730 void
12731 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12733 tree expr = *expr_p;
12735 /* We don't do anything if the value isn't there, is constant, or contains
12736 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12737 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12738 will want to replace it with a new variable, but that will cause problems
12739 if this type is from outside the function. It's OK to have that here. */
12740 if (expr == NULL_TREE
12741 || is_gimple_constant (expr)
12742 || TREE_CODE (expr) == VAR_DECL
12743 || CONTAINS_PLACEHOLDER_P (expr))
12744 return;
12746 *expr_p = unshare_expr (expr);
12748 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12749 if the def vanishes. */
12750 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12752 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12753 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12754 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12755 if (is_gimple_constant (*expr_p))
12756 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12759 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12760 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12761 is true, also gimplify the parameters. */
12763 gbind *
12764 gimplify_body (tree fndecl, bool do_parms)
12766 location_t saved_location = input_location;
12767 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12768 gimple *outer_stmt;
12769 gbind *outer_bind;
12771 timevar_push (TV_TREE_GIMPLIFY);
12773 init_tree_ssa (cfun);
12775 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12776 gimplification. */
12777 default_rtl_profile ();
12779 gcc_assert (gimplify_ctxp == NULL);
12780 push_gimplify_context (true);
12782 if (flag_openacc || flag_openmp)
12784 gcc_assert (gimplify_omp_ctxp == NULL);
12785 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12786 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12789 /* Unshare most shared trees in the body and in that of any nested functions.
12790 It would seem we don't have to do this for nested functions because
12791 they are supposed to be output and then the outer function gimplified
12792 first, but the g++ front end doesn't always do it that way. */
12793 unshare_body (fndecl);
12794 unvisit_body (fndecl);
12796 /* Make sure input_location isn't set to something weird. */
12797 input_location = DECL_SOURCE_LOCATION (fndecl);
12799 /* Resolve callee-copies. This has to be done before processing
12800 the body so that DECL_VALUE_EXPR gets processed correctly. */
12801 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12803 /* Gimplify the function's body. */
12804 seq = NULL;
12805 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12806 outer_stmt = gimple_seq_first_stmt (seq);
12807 if (!outer_stmt)
12809 outer_stmt = gimple_build_nop ();
12810 gimplify_seq_add_stmt (&seq, outer_stmt);
12813 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12814 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12815 if (gimple_code (outer_stmt) == GIMPLE_BIND
12816 && gimple_seq_first (seq) == gimple_seq_last (seq))
12817 outer_bind = as_a <gbind *> (outer_stmt);
12818 else
12819 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12821 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12823 /* If we had callee-copies statements, insert them at the beginning
12824 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12825 if (!gimple_seq_empty_p (parm_stmts))
12827 tree parm;
12829 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12830 if (parm_cleanup)
12832 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12833 GIMPLE_TRY_FINALLY);
12834 parm_stmts = NULL;
12835 gimple_seq_add_stmt (&parm_stmts, g);
12837 gimple_bind_set_body (outer_bind, parm_stmts);
12839 for (parm = DECL_ARGUMENTS (current_function_decl);
12840 parm; parm = DECL_CHAIN (parm))
12841 if (DECL_HAS_VALUE_EXPR_P (parm))
12843 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12844 DECL_IGNORED_P (parm) = 0;
12848 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12849 && gimplify_omp_ctxp)
12851 delete_omp_context (gimplify_omp_ctxp);
12852 gimplify_omp_ctxp = NULL;
12855 pop_gimplify_context (outer_bind);
12856 gcc_assert (gimplify_ctxp == NULL);
12858 if (flag_checking && !seen_error ())
12859 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12861 timevar_pop (TV_TREE_GIMPLIFY);
12862 input_location = saved_location;
12864 return outer_bind;
12867 typedef char *char_p; /* For DEF_VEC_P. */
12869 /* Return whether we should exclude FNDECL from instrumentation. */
12871 static bool
12872 flag_instrument_functions_exclude_p (tree fndecl)
12874 vec<char_p> *v;
12876 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12877 if (v && v->length () > 0)
12879 const char *name;
12880 int i;
12881 char *s;
12883 name = lang_hooks.decl_printable_name (fndecl, 0);
12884 FOR_EACH_VEC_ELT (*v, i, s)
12885 if (strstr (name, s) != NULL)
12886 return true;
12889 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12890 if (v && v->length () > 0)
12892 const char *name;
12893 int i;
12894 char *s;
12896 name = DECL_SOURCE_FILE (fndecl);
12897 FOR_EACH_VEC_ELT (*v, i, s)
12898 if (strstr (name, s) != NULL)
12899 return true;
12902 return false;
12905 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12906 node for the function we want to gimplify.
12908 Return the sequence of GIMPLE statements corresponding to the body
12909 of FNDECL. */
12911 void
12912 gimplify_function_tree (tree fndecl)
12914 tree parm, ret;
12915 gimple_seq seq;
12916 gbind *bind;
12918 gcc_assert (!gimple_body (fndecl));
12920 if (DECL_STRUCT_FUNCTION (fndecl))
12921 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12922 else
12923 push_struct_function (fndecl);
12925 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12926 if necessary. */
12927 cfun->curr_properties |= PROP_gimple_lva;
12929 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12931 /* Preliminarily mark non-addressed complex variables as eligible
12932 for promotion to gimple registers. We'll transform their uses
12933 as we find them. */
12934 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12935 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12936 && !TREE_THIS_VOLATILE (parm)
12937 && !needs_to_live_in_memory (parm))
12938 DECL_GIMPLE_REG_P (parm) = 1;
12941 ret = DECL_RESULT (fndecl);
12942 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12943 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12944 && !needs_to_live_in_memory (ret))
12945 DECL_GIMPLE_REG_P (ret) = 1;
12947 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12948 asan_poisoned_variables = new hash_set<tree> ();
12949 bind = gimplify_body (fndecl, true);
12950 if (asan_poisoned_variables)
12952 delete asan_poisoned_variables;
12953 asan_poisoned_variables = NULL;
12956 /* The tree body of the function is no longer needed, replace it
12957 with the new GIMPLE body. */
12958 seq = NULL;
12959 gimple_seq_add_stmt (&seq, bind);
12960 gimple_set_body (fndecl, seq);
12962 /* If we're instrumenting function entry/exit, then prepend the call to
12963 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12964 catch the exit hook. */
12965 /* ??? Add some way to ignore exceptions for this TFE. */
12966 if (flag_instrument_function_entry_exit
12967 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12968 /* Do not instrument extern inline functions. */
12969 && !(DECL_DECLARED_INLINE_P (fndecl)
12970 && DECL_EXTERNAL (fndecl)
12971 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12972 && !flag_instrument_functions_exclude_p (fndecl))
12974 tree x;
12975 gbind *new_bind;
12976 gimple *tf;
12977 gimple_seq cleanup = NULL, body = NULL;
12978 tree tmp_var, this_fn_addr;
12979 gcall *call;
12981 /* The instrumentation hooks aren't going to call the instrumented
12982 function and the address they receive is expected to be matchable
12983 against symbol addresses. Make sure we don't create a trampoline,
12984 in case the current function is nested. */
12985 this_fn_addr = build_fold_addr_expr (current_function_decl);
12986 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
12988 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12989 call = gimple_build_call (x, 1, integer_zero_node);
12990 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12991 gimple_call_set_lhs (call, tmp_var);
12992 gimplify_seq_add_stmt (&cleanup, call);
12993 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12994 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
12995 gimplify_seq_add_stmt (&cleanup, call);
12996 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12998 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12999 call = gimple_build_call (x, 1, integer_zero_node);
13000 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
13001 gimple_call_set_lhs (call, tmp_var);
13002 gimplify_seq_add_stmt (&body, call);
13003 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
13004 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13005 gimplify_seq_add_stmt (&body, call);
13006 gimplify_seq_add_stmt (&body, tf);
13007 new_bind = gimple_build_bind (NULL, body, NULL);
13009 /* Replace the current function body with the body
13010 wrapped in the try/finally TF. */
13011 seq = NULL;
13012 gimple_seq_add_stmt (&seq, new_bind);
13013 gimple_set_body (fndecl, seq);
13014 bind = new_bind;
13017 if (sanitize_flags_p (SANITIZE_THREAD))
13019 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
13020 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
13021 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
13022 /* Replace the current function body with the body
13023 wrapped in the try/finally TF. */
13024 seq = NULL;
13025 gimple_seq_add_stmt (&seq, new_bind);
13026 gimple_set_body (fndecl, seq);
13029 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13030 cfun->curr_properties |= PROP_gimple_any;
13032 pop_cfun ();
13034 dump_function (TDI_gimple, fndecl);
13037 /* Return a dummy expression of type TYPE in order to keep going after an
13038 error. */
13040 static tree
13041 dummy_object (tree type)
13043 tree t = build_int_cst (build_pointer_type (type), 0);
13044 return build2 (MEM_REF, type, t, t);
13047 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
13048 builtin function, but a very special sort of operator. */
13050 enum gimplify_status
13051 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
13052 gimple_seq *post_p ATTRIBUTE_UNUSED)
13054 tree promoted_type, have_va_type;
13055 tree valist = TREE_OPERAND (*expr_p, 0);
13056 tree type = TREE_TYPE (*expr_p);
13057 tree t, tag, aptag;
13058 location_t loc = EXPR_LOCATION (*expr_p);
13060 /* Verify that valist is of the proper type. */
13061 have_va_type = TREE_TYPE (valist);
13062 if (have_va_type == error_mark_node)
13063 return GS_ERROR;
13064 have_va_type = targetm.canonical_va_list_type (have_va_type);
13065 if (have_va_type == NULL_TREE
13066 && POINTER_TYPE_P (TREE_TYPE (valist)))
13067 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13068 have_va_type
13069 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13070 gcc_assert (have_va_type != NULL_TREE);
13072 /* Generate a diagnostic for requesting data of a type that cannot
13073 be passed through `...' due to type promotion at the call site. */
13074 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13075 != type)
13077 static bool gave_help;
13078 bool warned;
13079 /* Use the expansion point to handle cases such as passing bool (defined
13080 in a system header) through `...'. */
13081 source_location xloc
13082 = expansion_point_location_if_in_system_header (loc);
13084 /* Unfortunately, this is merely undefined, rather than a constraint
13085 violation, so we cannot make this an error. If this call is never
13086 executed, the program is still strictly conforming. */
13087 auto_diagnostic_group d;
13088 warned = warning_at (xloc, 0,
13089 "%qT is promoted to %qT when passed through %<...%>",
13090 type, promoted_type);
13091 if (!gave_help && warned)
13093 gave_help = true;
13094 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13095 promoted_type, type);
13098 /* We can, however, treat "undefined" any way we please.
13099 Call abort to encourage the user to fix the program. */
13100 if (warned)
13101 inform (xloc, "if this code is reached, the program will abort");
13102 /* Before the abort, allow the evaluation of the va_list
13103 expression to exit or longjmp. */
13104 gimplify_and_add (valist, pre_p);
13105 t = build_call_expr_loc (loc,
13106 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13107 gimplify_and_add (t, pre_p);
13109 /* This is dead code, but go ahead and finish so that the
13110 mode of the result comes out right. */
13111 *expr_p = dummy_object (type);
13112 return GS_ALL_DONE;
13115 tag = build_int_cst (build_pointer_type (type), 0);
13116 aptag = build_int_cst (TREE_TYPE (valist), 0);
13118 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13119 valist, tag, aptag);
13121 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13122 needs to be expanded. */
13123 cfun->curr_properties &= ~PROP_gimple_lva;
13125 return GS_OK;
13128 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13130 DST/SRC are the destination and source respectively. You can pass
13131 ungimplified trees in DST or SRC, in which case they will be
13132 converted to a gimple operand if necessary.
13134 This function returns the newly created GIMPLE_ASSIGN tuple. */
13136 gimple *
13137 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13139 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13140 gimplify_and_add (t, seq_p);
13141 ggc_free (t);
13142 return gimple_seq_last_stmt (*seq_p);
13145 inline hashval_t
13146 gimplify_hasher::hash (const elt_t *p)
13148 tree t = p->val;
13149 return iterative_hash_expr (t, 0);
13152 inline bool
13153 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13155 tree t1 = p1->val;
13156 tree t2 = p2->val;
13157 enum tree_code code = TREE_CODE (t1);
13159 if (TREE_CODE (t2) != code
13160 || TREE_TYPE (t1) != TREE_TYPE (t2))
13161 return false;
13163 if (!operand_equal_p (t1, t2, 0))
13164 return false;
13166 /* Only allow them to compare equal if they also hash equal; otherwise
13167 results are nondeterminate, and we fail bootstrap comparison. */
13168 gcc_checking_assert (hash (p1) == hash (p2));
13170 return true;